Blob


1 /*
2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
20 #include <errno.h>
21 #define _XOPEN_SOURCE_EXTENDED
22 #include <curses.h>
23 #undef _XOPEN_SOURCE_EXTENDED
24 #include <panel.h>
25 #include <locale.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <getopt.h>
29 #include <string.h>
30 #include <err.h>
31 #include <unistd.h>
32 #include <util.h>
33 #include <limits.h>
34 #include <wchar.h>
35 #include <time.h>
36 #include <pthread.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
42 #include "got_diff.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
45 #include "got_utf8.h"
46 #include "got_blame.h"
48 #ifndef MIN
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
50 #endif
52 #ifndef nitems
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 #endif
56 struct tog_cmd {
57 const char *name;
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
60 const char *descr;
61 };
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
83 };
85 struct tog_view {
86 WINDOW *window;
87 PANEL *panel;
88 int nlines, ncols, begin_y, begin_x;
89 int lines, cols; /* copies of LINES and COLS */
90 struct tog_view *parent;
91 };
93 static const struct got_error *
94 show_diff_view(struct tog_view *, struct got_object *, struct got_object *,
95 struct got_repository *);
96 static const struct got_error *
97 show_log_view(struct tog_view *, struct got_object_id *,
98 struct got_repository *, const char *);
99 static const struct got_error *
100 show_blame_view(struct tog_view *, const char *, struct got_object_id *,
101 struct got_repository *);
102 static const struct got_error *
103 show_tree_view(struct tog_view *, struct got_tree_object *,
104 struct got_object_id *, struct got_repository *);
106 static void
107 close_view(struct tog_view *view)
109 if (view->panel)
110 del_panel(view->panel);
111 if (view->window)
112 delwin(view->window);
113 free(view);
116 static struct tog_view *
117 open_view(int nlines, int ncols, int begin_y, int begin_x,
118 struct tog_view *parent)
120 struct tog_view *view = malloc(sizeof(*view));
122 if (view == NULL)
123 return NULL;
125 view->parent = parent;
126 view->lines = LINES;
127 view->cols = COLS;
128 view->nlines = nlines ? nlines : LINES - begin_y;
129 view->ncols = ncols ? ncols : COLS - begin_x;
130 view->begin_y = begin_y;
131 view->begin_x = begin_x;
132 view->window = newwin(nlines, ncols, begin_y, begin_x);
133 if (view->window == NULL) {
134 close_view(view);
135 return NULL;
137 view->panel = new_panel(view->window);
138 if (view->panel == NULL) {
139 close_view(view);
140 return NULL;
143 keypad(view->window, TRUE);
144 return view;
147 void
148 show_view(struct tog_view *view)
150 show_panel(view->panel);
151 update_panels();
154 const struct got_error *
155 view_resize(struct tog_view *view)
157 int nlines, ncols;
159 while (view) {
160 if (view->lines > LINES)
161 nlines = view->nlines - (view->lines - LINES);
162 else
163 nlines = view->nlines + (LINES - view->lines);
165 if (view->cols > COLS)
166 ncols = view->ncols - (view->cols - COLS);
167 else
168 ncols = view->ncols + (COLS - view->cols);
170 if (wresize(view->window, nlines, ncols) == ERR)
171 return got_error_from_errno();
172 replace_panel(view->panel, view->window);
174 view->nlines = nlines;
175 view->ncols = ncols;
176 view->lines = LINES;
177 view->cols = COLS;
179 view = view->parent;
182 return NULL;
185 __dead static void
186 usage_log(void)
188 endwin();
189 fprintf(stderr,
190 "usage: %s log [-c commit] [-r repository-path] [path]\n",
191 getprogname());
192 exit(1);
195 /* Create newly allocated wide-character string equivalent to a byte string. */
196 static const struct got_error *
197 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
199 char *vis = NULL;
200 const struct got_error *err = NULL;
202 *ws = NULL;
203 *wlen = mbstowcs(NULL, s, 0);
204 if (*wlen == (size_t)-1) {
205 int vislen;
206 if (errno != EILSEQ)
207 return got_error_from_errno();
209 /* byte string invalid in current encoding; try to "fix" it */
210 err = got_mbsavis(&vis, &vislen, s);
211 if (err)
212 return err;
213 *wlen = mbstowcs(NULL, vis, 0);
214 if (*wlen == (size_t)-1) {
215 err = got_error_from_errno(); /* give up */
216 goto done;
220 *ws = calloc(*wlen + 1, sizeof(*ws));
221 if (*ws == NULL) {
222 err = got_error_from_errno();
223 goto done;
226 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
227 err = got_error_from_errno();
228 done:
229 free(vis);
230 if (err) {
231 free(*ws);
232 *ws = NULL;
233 *wlen = 0;
235 return err;
238 /* Format a line for display, ensuring that it won't overflow a width limit. */
239 static const struct got_error *
240 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
242 const struct got_error *err = NULL;
243 int cols = 0;
244 wchar_t *wline = NULL;
245 size_t wlen;
246 int i;
248 *wlinep = NULL;
249 *widthp = 0;
251 err = mbs2ws(&wline, &wlen, line);
252 if (err)
253 return err;
255 i = 0;
256 while (i < wlen && cols < wlimit) {
257 int width = wcwidth(wline[i]);
258 switch (width) {
259 case 0:
260 i++;
261 break;
262 case 1:
263 case 2:
264 if (cols + width <= wlimit) {
265 cols += width;
266 i++;
268 break;
269 case -1:
270 if (wline[i] == L'\t')
271 cols += TABSIZE - ((cols + 1) % TABSIZE);
272 i++;
273 break;
274 default:
275 err = got_error_from_errno();
276 goto done;
279 wline[i] = L'\0';
280 if (widthp)
281 *widthp = cols;
282 done:
283 if (err)
284 free(wline);
285 else
286 *wlinep = wline;
287 return err;
290 static const struct got_error *
291 draw_commit(struct tog_view *view, struct got_commit_object *commit,
292 struct got_object_id *id)
294 const struct got_error *err = NULL;
295 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
296 char *logmsg0 = NULL, *logmsg = NULL;
297 char *author0 = NULL, *author = NULL;
298 wchar_t *wlogmsg = NULL, *wauthor = NULL;
299 int author_width, logmsg_width;
300 char *newline, *smallerthan;
301 char *line = NULL;
302 int col, limit;
303 static const size_t date_display_cols = 9;
304 static const size_t author_display_cols = 16;
305 const int avail = view->ncols;
307 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
308 &commit->tm_committer) >= sizeof(datebuf))
309 return got_error(GOT_ERR_NO_SPACE);
311 if (avail < date_display_cols)
312 limit = MIN(sizeof(datebuf) - 1, avail);
313 else
314 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
315 waddnstr(view->window, datebuf, limit);
316 col = limit + 1;
317 if (col > avail)
318 goto done;
320 author0 = strdup(commit->author);
321 if (author0 == NULL) {
322 err = got_error_from_errno();
323 goto done;
325 author = author0;
326 smallerthan = strchr(author, '<');
327 if (smallerthan)
328 *smallerthan = '\0';
329 else {
330 char *at = strchr(author, '@');
331 if (at)
332 *at = '\0';
334 limit = avail - col;
335 err = format_line(&wauthor, &author_width, author, limit);
336 if (err)
337 goto done;
338 waddwstr(view->window, wauthor);
339 col += author_width;
340 while (col <= avail && author_width < author_display_cols + 1) {
341 waddch(view->window, ' ');
342 col++;
343 author_width++;
345 if (col > avail)
346 goto done;
348 logmsg0 = strdup(commit->logmsg);
349 if (logmsg0 == NULL) {
350 err = got_error_from_errno();
351 goto done;
353 logmsg = logmsg0;
354 while (*logmsg == '\n')
355 logmsg++;
356 newline = strchr(logmsg, '\n');
357 if (newline)
358 *newline = '\0';
359 limit = avail - col;
360 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
361 if (err)
362 goto done;
363 waddwstr(view->window, wlogmsg);
364 col += logmsg_width;
365 while (col <= avail) {
366 waddch(view->window, ' ');
367 col++;
369 done:
370 free(logmsg0);
371 free(wlogmsg);
372 free(author0);
373 free(wauthor);
374 free(line);
375 return err;
378 struct commit_queue_entry {
379 TAILQ_ENTRY(commit_queue_entry) entry;
380 struct got_object_id *id;
381 struct got_commit_object *commit;
382 };
383 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
384 struct commit_queue {
385 int ncommits;
386 struct commit_queue_head head;
387 };
389 static struct commit_queue_entry *
390 alloc_commit_queue_entry(struct got_commit_object *commit,
391 struct got_object_id *id)
393 struct commit_queue_entry *entry;
395 entry = calloc(1, sizeof(*entry));
396 if (entry == NULL)
397 return NULL;
399 entry->id = id;
400 entry->commit = commit;
401 return entry;
404 static void
405 pop_commit(struct commit_queue *commits)
407 struct commit_queue_entry *entry;
409 entry = TAILQ_FIRST(&commits->head);
410 TAILQ_REMOVE(&commits->head, entry, entry);
411 got_object_commit_close(entry->commit);
412 commits->ncommits--;
413 /* Don't free entry->id! It is owned by the commit graph. */
414 free(entry);
417 static void
418 free_commits(struct commit_queue *commits)
420 while (!TAILQ_EMPTY(&commits->head))
421 pop_commit(commits);
424 static const struct got_error *
425 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
426 struct got_object_id *start_id, int minqueue, int init,
427 struct got_repository *repo, const char *path)
429 const struct got_error *err = NULL;
430 struct got_object_id *id;
431 struct commit_queue_entry *entry;
432 int nfetched, nqueued = 0, found_obj = 0;
433 int is_root_path = strcmp(path, "/") == 0;
435 err = got_commit_graph_iter_start(graph, start_id);
436 if (err)
437 return err;
439 entry = TAILQ_LAST(&commits->head, commit_queue_head);
440 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
441 int nfetched;
443 /* Start ID's commit is already on the queue; skip over it. */
444 err = got_commit_graph_iter_next(&id, graph);
445 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
446 return err;
448 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
449 if (err)
450 return err;
453 while (1) {
454 struct got_commit_object *commit;
456 err = got_commit_graph_iter_next(&id, graph);
457 if (err) {
458 if (err->code != GOT_ERR_ITER_NEED_MORE)
459 break;
460 if (nqueued >= minqueue) {
461 err = NULL;
462 break;
464 err = got_commit_graph_fetch_commits(&nfetched,
465 graph, 1, repo);
466 if (err)
467 return err;
468 continue;
470 if (id == NULL)
471 break;
473 err = got_object_open_as_commit(&commit, repo, id);
474 if (err)
475 break;
477 if (!is_root_path) {
478 struct got_object *obj;
479 struct got_object_qid *pid;
480 int changed = 0;
482 err = got_object_open_by_path(&obj, repo, id, path);
483 if (err) {
484 got_object_commit_close(commit);
485 if (err->code == GOT_ERR_NO_OBJ &&
486 (found_obj || !init)) {
487 /* History stops here. */
488 err = got_error(GOT_ERR_ITER_COMPLETED);
490 break;
492 found_obj = 1;
494 pid = SIMPLEQ_FIRST(&commit->parent_ids);
495 if (pid != NULL) {
496 struct got_object *pobj;
497 err = got_object_open_by_path(&pobj, repo,
498 pid->id, path);
499 if (err) {
500 if (err->code != GOT_ERR_NO_OBJ) {
501 got_object_close(obj);
502 got_object_commit_close(commit);
503 break;
505 err = NULL;
506 changed = 1;
507 } else {
508 struct got_object_id *id, *pid;
509 id = got_object_get_id(obj);
510 if (id == NULL) {
511 err = got_error_from_errno();
512 got_object_close(obj);
513 got_object_close(pobj);
514 break;
516 pid = got_object_get_id(pobj);
517 if (pid == NULL) {
518 err = got_error_from_errno();
519 free(id);
520 got_object_close(obj);
521 got_object_close(pobj);
522 break;
524 changed =
525 (got_object_id_cmp(id, pid) != 0);
526 got_object_close(pobj);
527 free(id);
528 free(pid);
531 got_object_close(obj);
532 if (!changed) {
533 got_object_commit_close(commit);
534 continue;
538 entry = alloc_commit_queue_entry(commit, id);
539 if (entry == NULL) {
540 err = got_error_from_errno();
541 break;
543 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
544 nqueued++;
545 commits->ncommits++;
548 return err;
551 static const struct got_error *
552 fetch_next_commit(struct commit_queue_entry **pentry,
553 struct commit_queue_entry *entry, struct commit_queue *commits,
554 struct got_commit_graph *graph, struct got_repository *repo,
555 const char *path)
557 const struct got_error *err = NULL;
559 *pentry = NULL;
561 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
562 if (err)
563 return err;
565 /* Next entry to display should now be available. */
566 *pentry = TAILQ_NEXT(entry, entry);
567 if (*pentry == NULL)
568 return got_error(GOT_ERR_NO_OBJ);
570 return NULL;
573 static const struct got_error *
574 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
576 const struct got_error *err = NULL;
577 struct got_reference *head_ref;
579 *head_id = NULL;
581 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
582 if (err)
583 return err;
585 err = got_ref_resolve(head_id, repo, head_ref);
586 got_ref_close(head_ref);
587 if (err) {
588 *head_id = NULL;
589 return err;
592 return NULL;
595 static const struct got_error *
596 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
597 struct commit_queue_entry **selected, struct commit_queue_entry *first,
598 struct commit_queue *commits, int selected_idx, int limit,
599 struct got_commit_graph *graph, struct got_repository *repo,
600 const char *path)
602 const struct got_error *err = NULL;
603 struct commit_queue_entry *entry;
604 int ncommits, width;
605 char *id_str, *header;
606 wchar_t *wline;
608 entry = first;
609 ncommits = 0;
610 while (entry) {
611 if (ncommits == selected_idx) {
612 *selected = entry;
613 break;
615 entry = TAILQ_NEXT(entry, entry);
616 ncommits++;
619 err = got_object_id_str(&id_str, (*selected)->id);
620 if (err)
621 return err;
623 if (path && strcmp(path, "/") != 0) {
624 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
625 err = got_error_from_errno();
626 free(id_str);
627 return err;
629 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
630 err = got_error_from_errno();
631 free(id_str);
632 return err;
634 free(id_str);
635 err = format_line(&wline, &width, header, view->ncols);
636 if (err) {
637 free(header);
638 return err;
640 free(header);
642 werase(view->window);
644 waddwstr(view->window, wline);
645 if (width < view->ncols)
646 waddch(view->window, '\n');
647 free(wline);
648 if (limit <= 1)
649 return NULL;
651 entry = first;
652 *last = first;
653 ncommits = 0;
654 while (entry) {
655 if (ncommits >= limit - 1)
656 break;
657 if (ncommits == selected_idx)
658 wstandout(view->window);
659 err = draw_commit(view, entry->commit, entry->id);
660 if (ncommits == selected_idx)
661 wstandend(view->window);
662 if (err)
663 break;
664 ncommits++;
665 *last = entry;
666 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
667 err = queue_commits(graph, commits, entry->id, 1,
668 0, repo, path);
669 if (err) {
670 if (err->code != GOT_ERR_ITER_COMPLETED)
671 return err;
672 err = NULL;
675 entry = TAILQ_NEXT(entry, entry);
678 update_panels();
679 doupdate();
681 return err;
684 static void
685 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
686 struct commit_queue *commits)
688 struct commit_queue_entry *entry;
689 int nscrolled = 0;
691 entry = TAILQ_FIRST(&commits->head);
692 if (*first_displayed_entry == entry)
693 return;
695 entry = *first_displayed_entry;
696 while (entry && nscrolled < maxscroll) {
697 entry = TAILQ_PREV(entry, commit_queue_head, entry);
698 if (entry) {
699 *first_displayed_entry = entry;
700 nscrolled++;
705 static const struct got_error *
706 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
707 struct commit_queue_entry *last_displayed_entry,
708 struct commit_queue *commits, struct got_commit_graph *graph,
709 struct got_repository *repo, const char *path)
711 const struct got_error *err = NULL;
712 struct commit_queue_entry *pentry;
713 int nscrolled = 0;
715 do {
716 pentry = TAILQ_NEXT(last_displayed_entry, entry);
717 if (pentry == NULL) {
718 err = fetch_next_commit(&pentry, last_displayed_entry,
719 commits, graph, repo, path);
720 if (err || pentry == NULL)
721 break;
723 last_displayed_entry = pentry;
725 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
726 if (pentry == NULL)
727 break;
728 *first_displayed_entry = pentry;
729 } while (++nscrolled < maxscroll);
731 return err;
734 static const struct got_error *
735 show_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
736 struct got_repository *repo)
738 const struct got_error *err;
739 struct got_object *obj1 = NULL, *obj2 = NULL;
740 struct got_object_qid *parent_id;
741 struct tog_view *view;
743 err = got_object_open(&obj2, repo, entry->id);
744 if (err)
745 return err;
747 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
748 if (parent_id) {
749 err = got_object_open(&obj1, repo, parent_id->id);
750 if (err)
751 goto done;
754 view = open_view(0, 0, 0, 0, parent_view);
755 if (view == NULL) {
756 err = got_error_from_errno();
757 goto done;
760 err = show_diff_view(view, obj1, obj2, repo);
761 close_view(view);
762 show_view(parent_view);
763 done:
764 if (obj1)
765 got_object_close(obj1);
766 if (obj2)
767 got_object_close(obj2);
768 return err;
771 static const struct got_error *
772 browse_commit(struct tog_view *parent_view, struct commit_queue_entry *entry,
773 struct got_repository *repo)
775 const struct got_error *err = NULL;
776 struct got_tree_object *tree;
777 struct tog_view *view;
779 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
780 if (err)
781 return err;
783 view = open_view(0, 0, 0, 0, parent_view);
784 if (view == NULL) {
785 err = got_error_from_errno();
786 goto done;
788 err = show_tree_view(view, tree, entry->id, repo);
789 close_view(view);
790 show_view(parent_view);
791 done:
792 got_object_tree_close(tree);
793 return err;
796 static const struct got_error *
797 show_log_view(struct tog_view *view, struct got_object_id *start_id,
798 struct got_repository *repo, const char *path)
800 const struct got_error *err = NULL;
801 struct got_object_id *head_id = NULL;
802 int ch, done = 0, selected = 0, nfetched;
803 struct got_commit_graph *graph = NULL;
804 struct commit_queue commits;
805 struct commit_queue_entry *first_displayed_entry = NULL;
806 struct commit_queue_entry *last_displayed_entry = NULL;
807 struct commit_queue_entry *selected_entry = NULL;
808 char *in_repo_path = NULL;
810 err = got_repo_map_path(&in_repo_path, repo, path);
811 if (err != NULL)
812 goto done;
814 err = get_head_commit_id(&head_id, repo);
815 if (err)
816 return err;
818 /* The graph contains all commits. */
819 err = got_commit_graph_open(&graph, head_id, 0, repo);
820 if (err)
821 goto done;
822 /* The commit queue contains a subset of commits filtered by path. */
823 TAILQ_INIT(&commits.head);
824 commits.ncommits = 0;
826 /* Populate commit graph with a sufficient number of commits. */
827 err = got_commit_graph_fetch_commits_up_to(&nfetched, graph, start_id,
828 repo);
829 if (err)
830 goto done;
832 /*
833 * Open the initial batch of commits, sorted in commit graph order.
834 * We keep all commits open throughout the lifetime of the log view
835 * in order to avoid having to re-fetch commits from disk while
836 * updating the display.
837 */
838 err = queue_commits(graph, &commits, start_id, view->nlines, 1, repo,
839 in_repo_path);
840 if (err) {
841 if (err->code != GOT_ERR_ITER_COMPLETED)
842 goto done;
843 err = NULL;
846 show_view(view);
848 first_displayed_entry = TAILQ_FIRST(&commits.head);
849 selected_entry = first_displayed_entry;
850 while (!done) {
851 err = draw_commits(view, &last_displayed_entry, &selected_entry,
852 first_displayed_entry, &commits, selected, view->nlines,
853 graph, repo, in_repo_path);
854 if (err)
855 goto done;
857 nodelay(stdscr, FALSE);
858 ch = wgetch(view->window);
859 nodelay(stdscr, TRUE);
860 switch (ch) {
861 case ERR:
862 break;
863 case 'q':
864 done = 1;
865 break;
866 case 'k':
867 case KEY_UP:
868 if (selected > 0)
869 selected--;
870 if (selected > 0)
871 break;
872 scroll_up(&first_displayed_entry, 1, &commits);
873 break;
874 case KEY_PPAGE:
875 if (TAILQ_FIRST(&commits.head) ==
876 first_displayed_entry) {
877 selected = 0;
878 break;
880 scroll_up(&first_displayed_entry, view->nlines,
881 &commits);
882 break;
883 case 'j':
884 case KEY_DOWN:
885 if (selected < MIN(view->nlines - 2,
886 commits.ncommits - 1)) {
887 selected++;
888 break;
890 err = scroll_down(&first_displayed_entry, 1,
891 last_displayed_entry, &commits, graph,
892 repo, in_repo_path);
893 if (err) {
894 if (err->code != GOT_ERR_ITER_COMPLETED)
895 goto done;
896 err = NULL;
898 break;
899 case KEY_NPAGE: {
900 struct commit_queue_entry *first;
901 first = first_displayed_entry;
902 err = scroll_down(&first_displayed_entry,
903 view->nlines, last_displayed_entry,
904 &commits, graph, repo, in_repo_path);
905 if (err) {
906 if (err->code != GOT_ERR_ITER_COMPLETED)
907 goto done;
908 if (first == first_displayed_entry &&
909 selected < MIN(view->nlines - 2,
910 commits.ncommits - 1)) {
911 /* can't scroll further down */
912 selected = MIN(view->nlines - 2,
913 commits.ncommits - 1);
915 err = NULL;
917 break;
919 case KEY_RESIZE:
920 err = view_resize(view);
921 if (err)
922 goto done;
923 if (selected > view->nlines - 2)
924 selected = view->nlines - 2;
925 if (selected > commits.ncommits - 1)
926 selected = commits.ncommits - 1;
927 break;
928 case KEY_ENTER:
929 case '\r':
930 err = show_commit(view, selected_entry, repo);
931 if (err)
932 goto done;
933 show_view(view);
934 break;
935 case 't':
936 err = browse_commit(view, selected_entry, repo);
937 if (err)
938 goto done;
939 show_view(view);
940 break;
941 default:
942 break;
945 done:
946 free(head_id);
947 if (graph)
948 got_commit_graph_close(graph);
949 free_commits(&commits);
950 free(in_repo_path);
951 return err;
954 static const struct got_error *
955 cmd_log(int argc, char *argv[])
957 const struct got_error *error;
958 struct got_repository *repo = NULL;
959 struct got_object_id *start_id = NULL;
960 char *path = NULL, *repo_path = NULL, *cwd = NULL;
961 char *start_commit = NULL;
962 int ch;
963 struct tog_view *view;
965 #ifndef PROFILE
966 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
967 err(1, "pledge");
968 #endif
970 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
971 switch (ch) {
972 case 'c':
973 start_commit = optarg;
974 break;
975 case 'r':
976 repo_path = realpath(optarg, NULL);
977 if (repo_path == NULL)
978 err(1, "-r option");
979 break;
980 default:
981 usage();
982 /* NOTREACHED */
986 argc -= optind;
987 argv += optind;
989 if (argc == 0)
990 path = strdup("");
991 else if (argc == 1)
992 path = strdup(argv[0]);
993 else
994 usage_log();
995 if (path == NULL)
996 return got_error_from_errno();
998 cwd = getcwd(NULL, 0);
999 if (cwd == NULL) {
1000 error = got_error_from_errno();
1001 goto done;
1003 if (repo_path == NULL) {
1004 repo_path = strdup(cwd);
1005 if (repo_path == NULL) {
1006 error = got_error_from_errno();
1007 goto done;
1011 error = got_repo_open(&repo, repo_path);
1012 if (error != NULL)
1013 goto done;
1015 if (start_commit == NULL) {
1016 error = get_head_commit_id(&start_id, repo);
1017 if (error != NULL)
1018 goto done;
1019 } else {
1020 struct got_object *obj;
1021 error = got_object_open_by_id_str(&obj, repo, start_commit);
1022 if (error == NULL) {
1023 start_id = got_object_get_id(obj);
1024 if (start_id == NULL)
1025 error = got_error_from_errno();
1026 goto done;
1029 if (error != NULL)
1030 goto done;
1032 view = open_view(0, 0, 0, 0, NULL);
1033 if (view == NULL) {
1034 error = got_error_from_errno();
1035 goto done;
1037 error = show_log_view(view, start_id, repo, path);
1038 close_view(view);
1039 done:
1040 free(repo_path);
1041 free(cwd);
1042 free(path);
1043 free(start_id);
1044 if (repo)
1045 got_repo_close(repo);
1046 return error;
1049 __dead static void
1050 usage_diff(void)
1052 endwin();
1053 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1054 getprogname());
1055 exit(1);
1058 static char *
1059 parse_next_line(FILE *f, size_t *len)
1061 char *line;
1062 size_t linelen;
1063 size_t lineno;
1064 const char delim[3] = { '\0', '\0', '\0'};
1066 line = fparseln(f, &linelen, &lineno, delim, 0);
1067 if (len)
1068 *len = linelen;
1069 return line;
1072 static const struct got_error *
1073 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1074 int *last_displayed_line, int *eof, int max_lines)
1076 const struct got_error *err;
1077 int nlines = 0, nprinted = 0;
1078 char *line;
1079 size_t len;
1080 wchar_t *wline;
1081 int width;
1083 rewind(f);
1084 werase(view->window);
1086 *eof = 0;
1087 while (nprinted < max_lines) {
1088 line = parse_next_line(f, &len);
1089 if (line == NULL) {
1090 *eof = 1;
1091 break;
1093 if (++nlines < *first_displayed_line) {
1094 free(line);
1095 continue;
1098 err = format_line(&wline, &width, line, view->ncols);
1099 if (err) {
1100 free(line);
1101 free(wline);
1102 return err;
1104 waddwstr(view->window, wline);
1105 if (width < view->ncols)
1106 waddch(view->window, '\n');
1107 if (++nprinted == 1)
1108 *first_displayed_line = nlines;
1109 free(line);
1110 free(wline);
1111 wline = NULL;
1113 *last_displayed_line = nlines;
1115 update_panels();
1116 doupdate();
1118 return NULL;
1121 static const struct got_error *
1122 show_diff_view(struct tog_view *view, struct got_object *obj1,
1123 struct got_object *obj2, struct got_repository *repo)
1125 const struct got_error *err;
1126 FILE *f;
1127 int ch, done = 0;
1128 int first_displayed_line = 1, last_displayed_line = view->nlines;
1129 int eof, i;
1131 if (obj1 != NULL && obj2 != NULL &&
1132 got_object_get_type(obj1) != got_object_get_type(obj2))
1133 return got_error(GOT_ERR_OBJ_TYPE);
1135 f = got_opentemp();
1136 if (f == NULL)
1137 return got_error_from_errno();
1139 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1140 case GOT_OBJ_TYPE_BLOB:
1141 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1142 break;
1143 case GOT_OBJ_TYPE_TREE:
1144 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1145 break;
1146 case GOT_OBJ_TYPE_COMMIT:
1147 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1148 break;
1149 default:
1150 return got_error(GOT_ERR_OBJ_TYPE);
1153 fflush(f);
1155 show_view(view);
1157 while (!done) {
1158 err = draw_file(view, f, &first_displayed_line,
1159 &last_displayed_line, &eof, view->nlines);
1160 if (err)
1161 break;
1162 nodelay(stdscr, FALSE);
1163 ch = wgetch(view->window);
1164 nodelay(stdscr, TRUE);
1165 switch (ch) {
1166 case 'q':
1167 done = 1;
1168 break;
1169 case 'k':
1170 case KEY_UP:
1171 if (first_displayed_line > 1)
1172 first_displayed_line--;
1173 break;
1174 case KEY_PPAGE:
1175 case KEY_BACKSPACE:
1176 i = 0;
1177 while (i++ < view->nlines - 1 &&
1178 first_displayed_line > 1)
1179 first_displayed_line--;
1180 break;
1181 case 'j':
1182 case KEY_DOWN:
1183 if (!eof)
1184 first_displayed_line++;
1185 break;
1186 case KEY_NPAGE:
1187 case ' ':
1188 i = 0;
1189 while (!eof && i++ < view->nlines - 1) {
1190 char *line = parse_next_line(f, NULL);
1191 first_displayed_line++;
1192 if (line == NULL)
1193 break;
1195 break;
1196 case KEY_RESIZE:
1197 err = view_resize(view);
1198 if (err)
1199 goto done;
1200 break;
1201 default:
1202 break;
1205 done:
1206 fclose(f);
1207 return err;
1210 static const struct got_error *
1211 cmd_diff(int argc, char *argv[])
1213 const struct got_error *error = NULL;
1214 struct got_repository *repo = NULL;
1215 struct got_object *obj1 = NULL, *obj2 = NULL;
1216 char *repo_path = NULL;
1217 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1218 int ch;
1219 struct tog_view *view;
1221 #ifndef PROFILE
1222 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1223 err(1, "pledge");
1224 #endif
1226 while ((ch = getopt(argc, argv, "")) != -1) {
1227 switch (ch) {
1228 default:
1229 usage();
1230 /* NOTREACHED */
1234 argc -= optind;
1235 argv += optind;
1237 if (argc == 0) {
1238 usage_diff(); /* TODO show local worktree changes */
1239 } else if (argc == 2) {
1240 repo_path = getcwd(NULL, 0);
1241 if (repo_path == NULL)
1242 return got_error_from_errno();
1243 obj_id_str1 = argv[0];
1244 obj_id_str2 = argv[1];
1245 } else if (argc == 3) {
1246 repo_path = realpath(argv[0], NULL);
1247 if (repo_path == NULL)
1248 return got_error_from_errno();
1249 obj_id_str1 = argv[1];
1250 obj_id_str2 = argv[2];
1251 } else
1252 usage_diff();
1254 error = got_repo_open(&repo, repo_path);
1255 free(repo_path);
1256 if (error)
1257 goto done;
1259 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1260 if (error)
1261 goto done;
1263 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1264 if (error)
1265 goto done;
1267 view = open_view(0, 0, 0, 0, NULL);
1268 if (view == NULL) {
1269 error = got_error_from_errno();
1270 goto done;
1272 error = show_diff_view(view, obj1, obj2, repo);
1273 close_view(view);
1274 done:
1275 got_repo_close(repo);
1276 if (obj1)
1277 got_object_close(obj1);
1278 if (obj2)
1279 got_object_close(obj2);
1280 return error;
1283 __dead static void
1284 usage_blame(void)
1286 endwin();
1287 fprintf(stderr, "usage: %s blame [-c commit] [repository-path] path\n",
1288 getprogname());
1289 exit(1);
1292 struct tog_blame_line {
1293 int annotated;
1294 struct got_object_id *id;
1297 static const struct got_error *
1298 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1299 const char *path, struct tog_blame_line *lines, int nlines,
1300 int blame_complete, int selected_line, int *first_displayed_line,
1301 int *last_displayed_line, int *eof, int max_lines)
1303 const struct got_error *err;
1304 int lineno = 0, nprinted = 0;
1305 char *line;
1306 size_t len;
1307 wchar_t *wline;
1308 int width, wlimit;
1309 struct tog_blame_line *blame_line;
1310 struct got_object_id *prev_id = NULL;
1311 char *id_str;
1313 err = got_object_id_str(&id_str, id);
1314 if (err)
1315 return err;
1317 rewind(f);
1318 werase(view->window);
1320 if (asprintf(&line, "commit: %s", id_str) == -1) {
1321 err = got_error_from_errno();
1322 free(id_str);
1323 return err;
1326 err = format_line(&wline, &width, line, view->ncols);
1327 free(line);
1328 line = NULL;
1329 waddwstr(view->window, wline);
1330 free(wline);
1331 wline = NULL;
1332 if (width < view->ncols)
1333 waddch(view->window, '\n');
1335 if (asprintf(&line, "[%d/%d] %s%s",
1336 *first_displayed_line - 1 + selected_line, nlines,
1337 blame_complete ? "" : "annotating ", path) == -1) {
1338 free(id_str);
1339 return got_error_from_errno();
1341 free(id_str);
1342 err = format_line(&wline, &width, line, view->ncols);
1343 free(line);
1344 line = NULL;
1345 if (err)
1346 return err;
1347 waddwstr(view->window, wline);
1348 free(wline);
1349 wline = NULL;
1350 if (width < view->ncols)
1351 waddch(view->window, '\n');
1353 *eof = 0;
1354 while (nprinted < max_lines - 2) {
1355 line = parse_next_line(f, &len);
1356 if (line == NULL) {
1357 *eof = 1;
1358 break;
1360 if (++lineno < *first_displayed_line) {
1361 free(line);
1362 continue;
1365 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1366 err = format_line(&wline, &width, line, wlimit);
1367 if (err) {
1368 free(line);
1369 return err;
1372 if (nprinted == selected_line - 1)
1373 wstandout(view->window);
1375 blame_line = &lines[lineno - 1];
1376 if (blame_line->annotated && prev_id &&
1377 got_object_id_cmp(prev_id, blame_line->id) == 0)
1378 waddstr(view->window, " ");
1379 else if (blame_line->annotated) {
1380 char *id_str;
1381 err = got_object_id_str(&id_str, blame_line->id);
1382 if (err) {
1383 free(line);
1384 free(wline);
1385 return err;
1387 wprintw(view->window, "%.8s ", id_str);
1388 free(id_str);
1389 prev_id = blame_line->id;
1390 } else {
1391 waddstr(view->window, "........ ");
1392 prev_id = NULL;
1395 waddwstr(view->window, wline);
1396 while (width < wlimit) {
1397 waddch(view->window, ' ');
1398 width++;
1400 if (nprinted == selected_line - 1)
1401 wstandend(view->window);
1402 if (++nprinted == 1)
1403 *first_displayed_line = lineno;
1404 free(line);
1405 free(wline);
1406 wline = NULL;
1408 *last_displayed_line = lineno;
1410 update_panels();
1411 doupdate();
1413 return NULL;
1416 struct tog_blame_cb_args {
1417 pthread_mutex_t *mutex;
1418 struct tog_blame_line *lines; /* one per line */
1419 int nlines;
1421 struct tog_view *view;
1422 struct got_object_id *commit_id;
1423 FILE *f;
1424 const char *path;
1425 int *first_displayed_line;
1426 int *last_displayed_line;
1427 int *selected_line;
1428 int *quit;
1431 static const struct got_error *
1432 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1434 const struct got_error *err = NULL;
1435 struct tog_blame_cb_args *a = arg;
1436 struct tog_blame_line *line;
1437 int eof;
1439 if (nlines != a->nlines ||
1440 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1441 return got_error(GOT_ERR_RANGE);
1443 if (pthread_mutex_lock(a->mutex) != 0)
1444 return got_error_from_errno();
1446 if (*a->quit) { /* user has quit the blame view */
1447 err = got_error(GOT_ERR_ITER_COMPLETED);
1448 goto done;
1451 if (lineno == -1)
1452 goto done; /* no change in this commit */
1454 line = &a->lines[lineno - 1];
1455 if (line->annotated)
1456 goto done;
1458 line->id = got_object_id_dup(id);
1459 if (line->id == NULL) {
1460 err = got_error_from_errno();
1461 goto done;
1463 line->annotated = 1;
1465 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1466 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1467 a->last_displayed_line, &eof, a->view->nlines);
1468 done:
1469 if (pthread_mutex_unlock(a->mutex) != 0)
1470 return got_error_from_errno();
1471 return err;
1474 struct tog_blame_thread_args {
1475 const char *path;
1476 struct got_repository *repo;
1477 struct tog_blame_cb_args *cb_args;
1478 int *complete;
1481 static void *
1482 blame_thread(void *arg)
1484 const struct got_error *err;
1485 struct tog_blame_thread_args *ta = arg;
1486 struct tog_blame_cb_args *a = ta->cb_args;
1487 int eof;
1489 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1490 blame_cb, ta->cb_args);
1492 if (pthread_mutex_lock(a->mutex) != 0)
1493 return (void *)got_error_from_errno();
1495 got_repo_close(ta->repo);
1496 ta->repo = NULL;
1497 *ta->complete = 1;
1498 if (!err)
1499 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1500 a->lines, a->nlines, 1, *a->selected_line,
1501 a->first_displayed_line, a->last_displayed_line, &eof,
1502 a->view->nlines);
1504 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1505 err = got_error_from_errno();
1507 return (void *)err;
1510 static struct got_object_id *
1511 get_selected_commit_id(struct tog_blame_line *lines,
1512 int first_displayed_line, int selected_line)
1514 struct tog_blame_line *line;
1516 line = &lines[first_displayed_line - 1 + selected_line - 1];
1517 if (!line->annotated)
1518 return NULL;
1520 return line->id;
1523 static const struct got_error *
1524 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1525 struct tog_blame_line *lines, int first_displayed_line,
1526 int selected_line, struct got_repository *repo)
1528 const struct got_error *err = NULL;
1529 struct got_commit_object *commit = NULL;
1530 struct got_object_id *selected_id;
1531 struct got_object_qid *pid;
1533 *pobj = NULL;
1534 *obj = NULL;
1536 selected_id = get_selected_commit_id(lines,
1537 first_displayed_line, selected_line);
1538 if (selected_id == NULL)
1539 return NULL;
1541 err = got_object_open(obj, repo, selected_id);
1542 if (err)
1543 goto done;
1545 err = got_object_commit_open(&commit, repo, *obj);
1546 if (err)
1547 goto done;
1549 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1550 if (pid) {
1551 err = got_object_open(pobj, repo, pid->id);
1552 if (err)
1553 goto done;
1555 done:
1556 if (commit)
1557 got_object_commit_close(commit);
1558 return err;
1561 struct tog_blame {
1562 FILE *f;
1563 size_t filesize;
1564 struct tog_blame_line *lines;
1565 size_t nlines;
1566 pthread_t thread;
1567 struct tog_blame_thread_args thread_args;
1568 struct tog_blame_cb_args cb_args;
1569 const char *path;
1572 static const struct got_error *
1573 stop_blame(struct tog_blame *blame)
1575 const struct got_error *err = NULL;
1576 int i;
1578 if (blame->thread) {
1579 if (pthread_join(blame->thread, (void **)&err) != 0)
1580 err = got_error_from_errno();
1581 if (err && err->code == GOT_ERR_ITER_COMPLETED)
1582 err = NULL;
1583 blame->thread = NULL;
1585 if (blame->thread_args.repo) {
1586 got_repo_close(blame->thread_args.repo);
1587 blame->thread_args.repo = NULL;
1589 if (blame->f) {
1590 fclose(blame->f);
1591 blame->f = NULL;
1593 for (i = 0; i < blame->nlines; i++)
1594 free(blame->lines[i].id);
1595 free(blame->lines);
1596 blame->lines = NULL;
1597 free(blame->cb_args.commit_id);
1598 blame->cb_args.commit_id = NULL;
1600 return err;
1603 static const struct got_error *
1604 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
1605 struct tog_view *view, int *blame_complete,
1606 int *first_displayed_line, int *last_displayed_line,
1607 int *selected_line, int *done, const char *path,
1608 struct got_object_id *commit_id,
1609 struct got_repository *repo)
1611 const struct got_error *err = NULL;
1612 struct got_blob_object *blob = NULL;
1613 struct got_repository *thread_repo = NULL;
1614 struct got_object *obj;
1616 err = got_object_open_by_path(&obj, repo, commit_id, path);
1617 if (err)
1618 goto done;
1619 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1620 err = got_error(GOT_ERR_OBJ_TYPE);
1621 goto done;
1624 err = got_object_blob_open(&blob, repo, obj, 8192);
1625 if (err)
1626 goto done;
1627 blame->f = got_opentemp();
1628 if (blame->f == NULL) {
1629 err = got_error_from_errno();
1630 goto done;
1632 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
1633 blame->f, blob);
1634 if (err)
1635 goto done;
1637 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
1638 if (blame->lines == NULL) {
1639 err = got_error_from_errno();
1640 goto done;
1643 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1644 if (err)
1645 goto done;
1647 blame->cb_args.view = view;
1648 blame->cb_args.lines = blame->lines;
1649 blame->cb_args.nlines = blame->nlines;
1650 blame->cb_args.mutex = mutex;
1651 blame->cb_args.commit_id = got_object_id_dup(commit_id);
1652 if (blame->cb_args.commit_id == NULL) {
1653 err = got_error_from_errno();
1654 goto done;
1656 blame->cb_args.f = blame->f;
1657 blame->cb_args.path = path;
1658 blame->cb_args.first_displayed_line = first_displayed_line;
1659 blame->cb_args.selected_line = selected_line;
1660 blame->cb_args.last_displayed_line = last_displayed_line;
1661 blame->cb_args.quit = done;
1663 blame->thread_args.path = path;
1664 blame->thread_args.repo = thread_repo;
1665 blame->thread_args.cb_args = &blame->cb_args;
1666 blame->thread_args.complete = blame_complete;
1667 *blame_complete = 0;
1669 if (pthread_create(&blame->thread, NULL, blame_thread,
1670 &blame->thread_args) != 0) {
1671 err = got_error_from_errno();
1672 goto done;
1675 done:
1676 if (blob)
1677 got_object_blob_close(blob);
1678 if (obj)
1679 got_object_close(obj);
1680 if (err)
1681 stop_blame(blame);
1682 return err;
1685 static const struct got_error *
1686 show_blame_view(struct tog_view *view, const char *path,
1687 struct got_object_id *commit_id, struct got_repository *repo)
1689 const struct got_error *err = NULL, *thread_err = NULL;
1690 int ch, done = 0, first_displayed_line = 1, last_displayed_line;
1691 int selected_line = first_displayed_line;
1692 int eof, blame_complete = 0;
1693 struct got_object *obj = NULL, *pobj = NULL;
1694 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
1695 struct tog_blame blame;
1696 struct got_object_id_queue blamed_commits;
1697 struct got_object_qid *blamed_commit = NULL;
1698 struct tog_view *diff_view;
1700 SIMPLEQ_INIT(&blamed_commits);
1702 if (pthread_mutex_init(&mutex, NULL) != 0) {
1703 err = got_error_from_errno();
1704 goto done;
1707 err = got_object_qid_alloc(&blamed_commit, commit_id);
1708 if (err)
1709 goto done;
1710 SIMPLEQ_INSERT_HEAD(&blamed_commits, blamed_commit, entry);
1712 show_view(view);
1713 last_displayed_line = view->nlines;
1715 memset(&blame, 0, sizeof(blame));
1716 err = run_blame(&blame, &mutex, view, &blame_complete,
1717 &first_displayed_line, &last_displayed_line,
1718 &selected_line, &done, path, blamed_commit->id, repo);
1719 if (err)
1720 return err;
1722 while (!done) {
1723 if (pthread_mutex_lock(&mutex) != 0) {
1724 err = got_error_from_errno();
1725 goto done;
1727 err = draw_blame(view, blamed_commit->id, blame.f, path,
1728 blame.lines, blame.nlines, blame_complete, selected_line,
1729 &first_displayed_line, &last_displayed_line, &eof,
1730 view->nlines);
1731 if (pthread_mutex_unlock(&mutex) != 0) {
1732 err = got_error_from_errno();
1733 goto done;
1735 if (err)
1736 break;
1737 nodelay(stdscr, FALSE);
1738 ch = wgetch(view->window);
1739 nodelay(stdscr, TRUE);
1740 if (pthread_mutex_lock(&mutex) != 0) {
1741 err = got_error_from_errno();
1742 goto done;
1744 switch (ch) {
1745 case 'q':
1746 done = 1;
1747 break;
1748 case 'k':
1749 case KEY_UP:
1750 if (selected_line > 1)
1751 selected_line--;
1752 else if (selected_line == 1 &&
1753 first_displayed_line > 1)
1754 first_displayed_line--;
1755 break;
1756 case KEY_PPAGE:
1757 case KEY_BACKSPACE:
1758 if (first_displayed_line == 1) {
1759 selected_line = 1;
1760 break;
1762 if (first_displayed_line > view->nlines - 2)
1763 first_displayed_line -=
1764 (view->nlines - 2);
1765 else
1766 first_displayed_line = 1;
1767 break;
1768 case 'j':
1769 case KEY_DOWN:
1770 if (selected_line < view->nlines - 2 &&
1771 first_displayed_line + selected_line <=
1772 blame.nlines)
1773 selected_line++;
1774 else if (last_displayed_line < blame.nlines)
1775 first_displayed_line++;
1776 break;
1777 case 'b':
1778 case 'p': {
1779 struct got_object_id *id;
1780 id = get_selected_commit_id(blame.lines,
1781 first_displayed_line, selected_line);
1782 if (id == NULL || got_object_id_cmp(id,
1783 blamed_commit->id) == 0)
1784 break;
1785 err = open_selected_commit(&pobj, &obj,
1786 blame.lines, first_displayed_line,
1787 selected_line, repo);
1788 if (err)
1789 break;
1790 if (pobj == NULL && obj == NULL)
1791 break;
1792 if (ch == 'p' && pobj == NULL)
1793 break;
1794 done = 1;
1795 if (pthread_mutex_unlock(&mutex) != 0) {
1796 err = got_error_from_errno();
1797 goto done;
1799 thread_err = stop_blame(&blame);
1800 done = 0;
1801 if (pthread_mutex_lock(&mutex) != 0) {
1802 err = got_error_from_errno();
1803 goto done;
1805 if (thread_err)
1806 break;
1807 id = got_object_get_id(ch == 'b' ? obj : pobj);
1808 got_object_close(obj);
1809 obj = NULL;
1810 if (pobj) {
1811 got_object_close(pobj);
1812 pobj = NULL;
1814 if (id == NULL) {
1815 err = got_error_from_errno();
1816 break;
1818 err = got_object_qid_alloc(&blamed_commit, id);
1819 free(id);
1820 if (err)
1821 goto done;
1822 SIMPLEQ_INSERT_HEAD(&blamed_commits,
1823 blamed_commit, entry);
1824 err = run_blame(&blame, &mutex, view,
1825 &blame_complete, &first_displayed_line,
1826 &last_displayed_line, &selected_line,
1827 &done, path, blamed_commit->id, repo);
1828 if (err)
1829 break;
1830 break;
1832 case 'B': {
1833 struct got_object_qid *first;
1834 first = SIMPLEQ_FIRST(&blamed_commits);
1835 if (!got_object_id_cmp(first->id, commit_id))
1836 break;
1837 done = 1;
1838 if (pthread_mutex_unlock(&mutex) != 0) {
1839 err = got_error_from_errno();
1840 goto done;
1842 thread_err = stop_blame(&blame);
1843 done = 0;
1844 if (pthread_mutex_lock(&mutex) != 0) {
1845 err = got_error_from_errno();
1846 goto done;
1848 if (thread_err)
1849 break;
1850 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1851 got_object_qid_free(blamed_commit);
1852 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1853 err = run_blame(&blame, &mutex, view,
1854 &blame_complete, &first_displayed_line,
1855 &last_displayed_line, &selected_line,
1856 &done, path, blamed_commit->id, repo);
1857 if (err)
1858 break;
1859 break;
1861 case KEY_ENTER:
1862 case '\r':
1863 err = open_selected_commit(&pobj, &obj,
1864 blame.lines, first_displayed_line,
1865 selected_line, repo);
1866 if (err)
1867 break;
1868 if (pobj == NULL && obj == NULL)
1869 break;
1870 diff_view = open_view(0, 0, 0, 0, view);
1871 if (diff_view == NULL) {
1872 err = got_error_from_errno();
1873 break;
1875 err = show_diff_view(diff_view, pobj, obj, repo);
1876 close_view(diff_view);
1877 show_view(view);
1878 if (pobj) {
1879 got_object_close(pobj);
1880 pobj = NULL;
1882 got_object_close(obj);
1883 obj = NULL;
1884 if (err)
1885 break;
1886 break;
1887 case KEY_NPAGE:
1888 case ' ':
1889 if (last_displayed_line >= blame.nlines &&
1890 selected_line < view->nlines - 2) {
1891 selected_line = MIN(blame.nlines,
1892 view->nlines - 2);
1893 break;
1895 if (last_displayed_line + view->nlines - 2 <=
1896 blame.nlines)
1897 first_displayed_line +=
1898 view->nlines - 2;
1899 else
1900 first_displayed_line =
1901 blame.nlines - (view->nlines - 3);
1902 break;
1903 case KEY_RESIZE:
1904 err = view_resize(view);
1905 if (err)
1906 break;
1907 if (selected_line > view->nlines - 2) {
1908 selected_line = MIN(blame.nlines,
1909 view->nlines - 2);
1911 break;
1912 default:
1913 break;
1915 if (pthread_mutex_unlock(&mutex) != 0)
1916 err = got_error_from_errno();
1917 if (err || thread_err)
1918 break;
1920 done:
1921 if (pobj)
1922 got_object_close(pobj);
1923 if (blame.thread)
1924 thread_err = stop_blame(&blame);
1925 while (!SIMPLEQ_EMPTY(&blamed_commits)) {
1926 blamed_commit = SIMPLEQ_FIRST(&blamed_commits);
1927 SIMPLEQ_REMOVE_HEAD(&blamed_commits, entry);
1928 got_object_qid_free(blamed_commit);
1930 return thread_err ? thread_err : err;
1933 static const struct got_error *
1934 cmd_blame(int argc, char *argv[])
1936 const struct got_error *error;
1937 struct got_repository *repo = NULL;
1938 char *repo_path = NULL;
1939 char *path = NULL;
1940 struct got_object_id *commit_id = NULL;
1941 char *commit_id_str = NULL;
1942 int ch;
1943 struct tog_view *view;
1945 #ifndef PROFILE
1946 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1947 err(1, "pledge");
1948 #endif
1950 while ((ch = getopt(argc, argv, "c:")) != -1) {
1951 switch (ch) {
1952 case 'c':
1953 commit_id_str = optarg;
1954 break;
1955 default:
1956 usage();
1957 /* NOTREACHED */
1961 argc -= optind;
1962 argv += optind;
1964 if (argc == 0) {
1965 usage_blame();
1966 } else if (argc == 1) {
1967 repo_path = getcwd(NULL, 0);
1968 if (repo_path == NULL)
1969 return got_error_from_errno();
1970 path = argv[0];
1971 } else if (argc == 2) {
1972 repo_path = realpath(argv[0], NULL);
1973 if (repo_path == NULL)
1974 return got_error_from_errno();
1975 path = argv[1];
1976 } else
1977 usage_blame();
1979 error = got_repo_open(&repo, repo_path);
1980 free(repo_path);
1981 if (error != NULL)
1982 return error;
1984 if (commit_id_str == NULL) {
1985 struct got_reference *head_ref;
1986 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1987 if (error != NULL)
1988 goto done;
1989 error = got_ref_resolve(&commit_id, repo, head_ref);
1990 got_ref_close(head_ref);
1991 } else {
1992 struct got_object *obj;
1993 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
1994 if (error != NULL)
1995 goto done;
1996 commit_id = got_object_get_id(obj);
1997 if (commit_id == NULL)
1998 error = got_error_from_errno();
1999 got_object_close(obj);
2001 if (error != NULL)
2002 goto done;
2004 view = open_view(0, 0, 0, 0, NULL);
2005 if (view == NULL) {
2006 error = got_error_from_errno();
2007 goto done;
2009 error = show_blame_view(view, path, commit_id, repo);
2010 close_view(view);
2011 done:
2012 free(commit_id);
2013 if (repo)
2014 got_repo_close(repo);
2015 return error;
2018 static const struct got_error *
2019 draw_tree_entries(struct tog_view *view,
2020 struct got_tree_entry **first_displayed_entry,
2021 struct got_tree_entry **last_displayed_entry,
2022 struct got_tree_entry **selected_entry, int *ndisplayed,
2023 const char *label, int show_ids, const char *parent_path,
2024 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2026 const struct got_error *err = NULL;
2027 struct got_tree_entry *te;
2028 wchar_t *wline;
2029 int width, n;
2031 *ndisplayed = 0;
2033 werase(view->window);
2035 if (limit == 0)
2036 return NULL;
2038 err = format_line(&wline, &width, label, view->ncols);
2039 if (err)
2040 return err;
2041 waddwstr(view->window, wline);
2042 free(wline);
2043 wline = NULL;
2044 if (width < view->ncols)
2045 waddch(view->window, '\n');
2046 if (--limit <= 0)
2047 return NULL;
2048 err = format_line(&wline, &width, parent_path, view->ncols);
2049 if (err)
2050 return err;
2051 waddwstr(view->window, wline);
2052 free(wline);
2053 wline = NULL;
2054 if (width < view->ncols)
2055 waddch(view->window, '\n');
2056 if (--limit <= 0)
2057 return NULL;
2058 waddch(view->window, '\n');
2059 if (--limit <= 0)
2060 return NULL;
2062 te = SIMPLEQ_FIRST(&entries->head);
2063 if (*first_displayed_entry == NULL) {
2064 if (selected == 0) {
2065 wstandout(view->window);
2066 *selected_entry = NULL;
2068 waddstr(view->window, " ..\n"); /* parent directory */
2069 if (selected == 0)
2070 wstandend(view->window);
2071 (*ndisplayed)++;
2072 if (--limit <= 0)
2073 return NULL;
2074 n = 1;
2075 } else {
2076 n = 0;
2077 while (te != *first_displayed_entry)
2078 te = SIMPLEQ_NEXT(te, entry);
2081 while (te) {
2082 char *line = NULL, *id_str = NULL;
2084 if (show_ids) {
2085 err = got_object_id_str(&id_str, te->id);
2086 if (err)
2087 return got_error_from_errno();
2089 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2090 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2091 free(id_str);
2092 return got_error_from_errno();
2094 free(id_str);
2095 err = format_line(&wline, &width, line, view->ncols);
2096 if (err) {
2097 free(line);
2098 break;
2100 if (n == selected) {
2101 wstandout(view->window);
2102 *selected_entry = te;
2104 waddwstr(view->window, wline);
2105 if (width < view->ncols)
2106 waddch(view->window, '\n');
2107 if (n == selected)
2108 wstandend(view->window);
2109 free(line);
2110 free(wline);
2111 wline = NULL;
2112 n++;
2113 (*ndisplayed)++;
2114 *last_displayed_entry = te;
2115 if (--limit <= 0)
2116 break;
2117 te = SIMPLEQ_NEXT(te, entry);
2120 return err;
2123 static void
2124 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2125 const struct got_tree_entries *entries, int isroot)
2127 struct got_tree_entry *te, *prev;
2128 int i;
2130 if (*first_displayed_entry == NULL)
2131 return;
2133 te = SIMPLEQ_FIRST(&entries->head);
2134 if (*first_displayed_entry == te) {
2135 if (!isroot)
2136 *first_displayed_entry = NULL;
2137 return;
2140 /* XXX this is stupid... switch to TAILQ? */
2141 for (i = 0; i < maxscroll; i++) {
2142 while (te != *first_displayed_entry) {
2143 prev = te;
2144 te = SIMPLEQ_NEXT(te, entry);
2146 *first_displayed_entry = prev;
2147 te = SIMPLEQ_FIRST(&entries->head);
2149 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2150 *first_displayed_entry = NULL;
2153 static void
2154 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2155 struct got_tree_entry *last_displayed_entry,
2156 const struct got_tree_entries *entries)
2158 struct got_tree_entry *next;
2159 int n = 0;
2161 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2162 return;
2164 if (*first_displayed_entry)
2165 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2166 else
2167 next = SIMPLEQ_FIRST(&entries->head);
2168 while (next) {
2169 *first_displayed_entry = next;
2170 if (++n >= maxscroll)
2171 break;
2172 next = SIMPLEQ_NEXT(next, entry);
2176 struct tog_parent_tree {
2177 TAILQ_ENTRY(tog_parent_tree) entry;
2178 struct got_tree_object *tree;
2179 struct got_tree_entry *first_displayed_entry;
2180 struct got_tree_entry *selected_entry;
2181 int selected;
2184 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
2186 static const struct got_error *
2187 tree_entry_path(char **path, struct tog_parent_trees *parents,
2188 struct got_tree_entry *te)
2190 const struct got_error *err = NULL;
2191 struct tog_parent_tree *pt;
2192 size_t len = 2; /* for leading slash and NUL */
2194 TAILQ_FOREACH(pt, parents, entry)
2195 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2196 if (te)
2197 len += strlen(te->name);
2199 *path = calloc(1, len);
2200 if (path == NULL)
2201 return got_error_from_errno();
2203 (*path)[0] = '/';
2204 pt = TAILQ_LAST(parents, tog_parent_trees);
2205 while (pt) {
2206 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2207 err = got_error(GOT_ERR_NO_SPACE);
2208 goto done;
2210 if (strlcat(*path, "/", len) >= len) {
2211 err = got_error(GOT_ERR_NO_SPACE);
2212 goto done;
2214 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2216 if (te) {
2217 if (strlcat(*path, te->name, len) >= len) {
2218 err = got_error(GOT_ERR_NO_SPACE);
2219 goto done;
2222 done:
2223 if (err) {
2224 free(*path);
2225 *path = NULL;
2227 return err;
2230 static const struct got_error *
2231 blame_tree_entry(struct tog_view *parent_view, struct got_tree_entry *te,
2232 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2233 struct got_repository *repo)
2235 const struct got_error *err = NULL;
2236 char *path;
2237 struct tog_view *view;
2239 err = tree_entry_path(&path, parents, te);
2240 if (err)
2241 return err;
2243 view = open_view(0, 0, 0, 0, parent_view);
2244 if (view) {
2245 err = show_blame_view(view, path, commit_id, repo);
2246 close_view(view);
2247 } else
2248 err = got_error_from_errno();
2250 show_view(parent_view);
2251 free(path);
2252 return err;
2255 static const struct got_error *
2256 log_tree_entry(struct tog_view *view, struct got_tree_entry *te,
2257 struct tog_parent_trees *parents, struct got_object_id *commit_id,
2258 struct got_repository *repo)
2260 const struct got_error *err = NULL;
2261 char *path;
2263 err = tree_entry_path(&path, parents, te);
2264 if (err)
2265 return err;
2267 err = show_log_view(view, commit_id, repo, path);
2268 free(path);
2269 return err;
2272 static const struct got_error *
2273 show_tree_view(struct tog_view *view, struct got_tree_object *root,
2274 struct got_object_id *commit_id, struct got_repository *repo)
2276 const struct got_error *err = NULL;
2277 int ch, done = 0, selected = 0, show_ids = 0;
2278 struct got_tree_object *tree = root;
2279 const struct got_tree_entries *entries;
2280 struct got_tree_entry *first_displayed_entry = NULL;
2281 struct got_tree_entry *last_displayed_entry = NULL;
2282 struct got_tree_entry *selected_entry = NULL;
2283 char *commit_id_str = NULL, *tree_label = NULL;
2284 int nentries, ndisplayed;
2285 struct tog_parent_trees parents;
2287 TAILQ_INIT(&parents);
2289 err = got_object_id_str(&commit_id_str, commit_id);
2290 if (err != NULL)
2291 goto done;
2293 if (asprintf(&tree_label, "commit: %s", commit_id_str) == -1) {
2294 err = got_error_from_errno();
2295 goto done;
2298 show_view(view);
2300 entries = got_object_tree_get_entries(root);
2301 first_displayed_entry = SIMPLEQ_FIRST(&entries->head);
2302 while (!done) {
2303 char *parent_path;
2304 entries = got_object_tree_get_entries(tree);
2305 nentries = entries->nentries;
2306 if (tree != root)
2307 nentries++; /* '..' directory */
2309 err = tree_entry_path(&parent_path, &parents, NULL);
2310 if (err)
2311 goto done;
2313 err = draw_tree_entries(view, &first_displayed_entry,
2314 &last_displayed_entry, &selected_entry, &ndisplayed,
2315 tree_label, show_ids, parent_path, entries, selected,
2316 view->nlines, tree == root);
2317 free(parent_path);
2318 if (err)
2319 break;
2321 nodelay(stdscr, FALSE);
2322 ch = wgetch(view->window);
2323 nodelay(stdscr, TRUE);
2324 switch (ch) {
2325 case 'q':
2326 done = 1;
2327 break;
2328 case 'i':
2329 show_ids = !show_ids;
2330 break;
2331 case 'l':
2332 if (selected_entry) {
2333 struct tog_view *log_view;
2334 log_view = open_view(0, 0, 0, 0, view);
2335 if (log_view == NULL) {
2336 err = got_error_from_errno();
2337 goto done;
2339 err = log_tree_entry(log_view,
2340 selected_entry, &parents,
2341 commit_id, repo);
2342 close_view(log_view);
2343 show_view(view);
2344 if (err)
2345 goto done;
2347 break;
2348 case 'k':
2349 case KEY_UP:
2350 if (selected > 0)
2351 selected--;
2352 if (selected > 0)
2353 break;
2354 tree_scroll_up(&first_displayed_entry, 1,
2355 entries, tree == root);
2356 break;
2357 case KEY_PPAGE:
2358 if (SIMPLEQ_FIRST(&entries->head) ==
2359 first_displayed_entry) {
2360 if (tree != root)
2361 first_displayed_entry = NULL;
2362 selected = 0;
2363 break;
2365 tree_scroll_up(&first_displayed_entry,
2366 view->nlines, entries, tree == root);
2367 break;
2368 case 'j':
2369 case KEY_DOWN:
2370 if (selected < ndisplayed - 1) {
2371 selected++;
2372 break;
2374 tree_scroll_down(&first_displayed_entry, 1,
2375 last_displayed_entry, entries);
2376 break;
2377 case KEY_NPAGE:
2378 tree_scroll_down(&first_displayed_entry,
2379 view->nlines, last_displayed_entry,
2380 entries);
2381 if (SIMPLEQ_NEXT(last_displayed_entry, entry))
2382 break;
2383 /* can't scroll any further; move cursor down */
2384 if (selected < ndisplayed - 1)
2385 selected = ndisplayed - 1;
2386 break;
2387 case KEY_ENTER:
2388 case '\r':
2389 if (selected_entry == NULL) {
2390 struct tog_parent_tree *parent;
2391 case KEY_BACKSPACE:
2392 /* user selected '..' */
2393 if (tree == root)
2394 break;
2395 parent = TAILQ_FIRST(&parents);
2396 TAILQ_REMOVE(&parents, parent, entry);
2397 got_object_tree_close(tree);
2398 tree = parent->tree;
2399 first_displayed_entry =
2400 parent->first_displayed_entry;
2401 selected_entry = parent->selected_entry;
2402 selected = parent->selected;
2403 free(parent);
2404 } else if (S_ISDIR(selected_entry->mode)) {
2405 struct tog_parent_tree *parent;
2406 struct got_tree_object *child;
2407 err = got_object_open_as_tree(
2408 &child, repo, selected_entry->id);
2409 if (err)
2410 goto done;
2411 parent = calloc(1, sizeof(*parent));
2412 if (parent == NULL) {
2413 err = got_error_from_errno();
2414 goto done;
2416 parent->tree = tree;
2417 parent->first_displayed_entry =
2418 first_displayed_entry;
2419 parent->selected_entry = selected_entry;
2420 parent->selected = selected;
2421 TAILQ_INSERT_HEAD(&parents, parent,
2422 entry);
2423 tree = child;
2424 selected = 0;
2425 first_displayed_entry = NULL;
2426 } else if (S_ISREG(selected_entry->mode)) {
2427 err = blame_tree_entry(view,
2428 selected_entry, &parents,
2429 commit_id, repo);
2430 if (err)
2431 goto done;
2433 break;
2434 case KEY_RESIZE:
2435 err = view_resize(view);
2436 if (err)
2437 goto done;
2438 if (selected > view->nlines)
2439 selected = ndisplayed - 1;
2440 break;
2441 default:
2442 break;
2445 done:
2446 free(tree_label);
2447 free(commit_id_str);
2448 while (!TAILQ_EMPTY(&parents)) {
2449 struct tog_parent_tree *parent;
2450 parent = TAILQ_FIRST(&parents);
2451 TAILQ_REMOVE(&parents, parent, entry);
2452 free(parent);
2455 if (tree != root)
2456 got_object_tree_close(tree);
2457 return err;
2460 __dead static void
2461 usage_tree(void)
2463 endwin();
2464 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2465 getprogname());
2466 exit(1);
2469 static const struct got_error *
2470 cmd_tree(int argc, char *argv[])
2472 const struct got_error *error;
2473 struct got_repository *repo = NULL;
2474 char *repo_path = NULL;
2475 struct got_object_id *commit_id = NULL;
2476 char *commit_id_arg = NULL;
2477 struct got_commit_object *commit = NULL;
2478 struct got_tree_object *tree = NULL;
2479 int ch;
2480 struct tog_view *view;
2482 #ifndef PROFILE
2483 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2484 err(1, "pledge");
2485 #endif
2487 while ((ch = getopt(argc, argv, "c:")) != -1) {
2488 switch (ch) {
2489 case 'c':
2490 commit_id_arg = optarg;
2491 break;
2492 default:
2493 usage();
2494 /* NOTREACHED */
2498 argc -= optind;
2499 argv += optind;
2501 if (argc == 0) {
2502 repo_path = getcwd(NULL, 0);
2503 if (repo_path == NULL)
2504 return got_error_from_errno();
2505 } else if (argc == 1) {
2506 repo_path = realpath(argv[0], NULL);
2507 if (repo_path == NULL)
2508 return got_error_from_errno();
2509 } else
2510 usage_log();
2512 error = got_repo_open(&repo, repo_path);
2513 free(repo_path);
2514 if (error != NULL)
2515 return error;
2517 if (commit_id_arg == NULL) {
2518 error = get_head_commit_id(&commit_id, repo);
2519 if (error != NULL)
2520 goto done;
2521 } else {
2522 struct got_object *obj;
2523 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
2524 if (error == NULL) {
2525 commit_id = got_object_get_id(obj);
2526 if (commit_id == NULL)
2527 error = got_error_from_errno();
2530 if (error != NULL)
2531 goto done;
2533 error = got_object_open_as_commit(&commit, repo, commit_id);
2534 if (error != NULL)
2535 goto done;
2537 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
2538 if (error != NULL)
2539 goto done;
2541 view = open_view(0, 0, 0, 0, NULL);
2542 if (view == NULL) {
2543 error = got_error_from_errno();
2544 goto done;
2546 error = show_tree_view(view, tree, commit_id, repo);
2547 close_view(view);
2548 done:
2549 free(commit_id);
2550 if (commit)
2551 got_object_commit_close(commit);
2552 if (tree)
2553 got_object_tree_close(tree);
2554 if (repo)
2555 got_repo_close(repo);
2556 return error;
2558 static void
2559 init_curses(void)
2561 initscr();
2562 cbreak();
2563 noecho();
2564 nonl();
2565 intrflush(stdscr, FALSE);
2566 keypad(stdscr, TRUE);
2567 curs_set(0);
2570 __dead static void
2571 usage(void)
2573 int i;
2575 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
2576 "Available commands:\n", getprogname());
2577 for (i = 0; i < nitems(tog_commands); i++) {
2578 struct tog_cmd *cmd = &tog_commands[i];
2579 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
2581 exit(1);
2584 static char **
2585 make_argv(const char *arg0, const char *arg1)
2587 char **argv;
2588 int argc = (arg1 == NULL ? 1 : 2);
2590 argv = calloc(argc, sizeof(char *));
2591 if (argv == NULL)
2592 err(1, "calloc");
2593 argv[0] = strdup(arg0);
2594 if (argv[0] == NULL)
2595 err(1, "calloc");
2596 if (arg1) {
2597 argv[1] = strdup(arg1);
2598 if (argv[1] == NULL)
2599 err(1, "calloc");
2602 return argv;
2605 int
2606 main(int argc, char *argv[])
2608 const struct got_error *error = NULL;
2609 struct tog_cmd *cmd = NULL;
2610 int ch, hflag = 0;
2611 char **cmd_argv = NULL;
2613 setlocale(LC_ALL, "");
2615 while ((ch = getopt(argc, argv, "h")) != -1) {
2616 switch (ch) {
2617 case 'h':
2618 hflag = 1;
2619 break;
2620 default:
2621 usage();
2622 /* NOTREACHED */
2626 argc -= optind;
2627 argv += optind;
2628 optind = 0;
2629 optreset = 1;
2631 if (argc == 0) {
2632 if (hflag)
2633 usage();
2634 /* Build an argument vector which runs a default command. */
2635 cmd = &tog_commands[0];
2636 cmd_argv = make_argv(cmd->name, NULL);
2637 argc = 1;
2638 } else {
2639 int i;
2641 /* Did the user specific a command? */
2642 for (i = 0; i < nitems(tog_commands); i++) {
2643 if (strncmp(tog_commands[i].name, argv[0],
2644 strlen(argv[0])) == 0) {
2645 cmd = &tog_commands[i];
2646 if (hflag)
2647 tog_commands[i].cmd_usage();
2648 break;
2651 if (cmd == NULL) {
2652 /* Did the user specify a repository? */
2653 char *repo_path = realpath(argv[0], NULL);
2654 if (repo_path) {
2655 struct got_repository *repo;
2656 error = got_repo_open(&repo, repo_path);
2657 if (error == NULL)
2658 got_repo_close(repo);
2659 } else
2660 error = got_error_from_errno();
2661 if (error) {
2662 if (hflag) {
2663 fprintf(stderr, "%s: '%s' is not a "
2664 "known command\n", getprogname(),
2665 argv[0]);
2666 usage();
2668 fprintf(stderr, "%s: '%s' is neither a known "
2669 "command nor a path to a repository\n",
2670 getprogname(), argv[0]);
2671 free(repo_path);
2672 return 1;
2674 cmd = &tog_commands[0];
2675 cmd_argv = make_argv(cmd->name, repo_path);
2676 argc = 2;
2677 free(repo_path);
2681 init_curses();
2683 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
2684 if (error)
2685 goto done;
2686 done:
2687 endwin();
2688 free(cmd_argv);
2689 if (error)
2690 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
2691 return 0;