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>
37 #include <libgen.h>
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
43 #include "got_diff.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
46 #include "got_utf8.h"
47 #include "got_blame.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef nitems
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 #endif
57 struct tog_cmd {
58 const char *name;
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
61 const char *descr;
62 };
64 __dead static void usage(void);
65 __dead static void usage_log(void);
66 __dead static void usage_diff(void);
67 __dead static void usage_blame(void);
68 __dead static void usage_tree(void);
70 static const struct got_error* cmd_log(int, char *[]);
71 static const struct got_error* cmd_diff(int, char *[]);
72 static const struct got_error* cmd_blame(int, char *[]);
73 static const struct got_error* cmd_tree(int, char *[]);
75 static struct tog_cmd tog_commands[] = {
76 { "log", cmd_log, usage_log,
77 "show repository history" },
78 { "diff", cmd_diff, usage_diff,
79 "compare files and directories" },
80 { "blame", cmd_blame, usage_blame,
81 "show line-by-line file history" },
82 { "tree", cmd_tree, usage_tree,
83 "browse trees in repository" },
84 };
86 enum tog_view_type {
87 TOG_VIEW_DIFF,
88 TOG_VIEW_LOG,
89 TOG_VIEW_BLAME,
90 TOG_VIEW_TREE
91 };
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
95 FILE *f;
96 int first_displayed_line;
97 int last_displayed_line;
98 int eof;
99 int diff_context;
100 struct got_repository *repo;
101 };
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_log_view_state {
115 struct got_commit_graph *graph;
116 struct commit_queue commits;
117 struct commit_queue_entry *first_displayed_entry;
118 struct commit_queue_entry *last_displayed_entry;
119 struct commit_queue_entry *selected_entry;
120 int selected;
121 char *in_repo_path;
122 struct got_repository *repo;
123 struct got_object_id *start_id;
124 };
126 struct tog_blame_cb_args {
127 pthread_mutex_t *mutex;
128 struct tog_blame_line *lines; /* one per line */
129 int nlines;
131 struct tog_view *view;
132 struct got_object_id *commit_id;
133 FILE *f;
134 const char *path;
135 int *first_displayed_line;
136 int *last_displayed_line;
137 int *selected_line;
138 int *quit;
139 int *eof;
140 };
142 struct tog_blame_thread_args {
143 const char *path;
144 struct got_repository *repo;
145 struct tog_blame_cb_args *cb_args;
146 int *complete;
147 };
149 struct tog_blame {
150 FILE *f;
151 size_t filesize;
152 struct tog_blame_line *lines;
153 size_t nlines;
154 pthread_t thread;
155 struct tog_blame_thread_args thread_args;
156 struct tog_blame_cb_args cb_args;
157 const char *path;
158 };
160 struct tog_blame_view_state {
161 int first_displayed_line;
162 int last_displayed_line;
163 int selected_line;
164 int blame_complete;
165 int eof;
166 int done;
167 pthread_mutex_t mutex;
168 struct got_object_id_queue blamed_commits;
169 struct got_object_qid *blamed_commit;
170 char *path;
171 struct got_repository *repo;
172 struct got_object_id *commit_id;
173 struct tog_blame blame;
174 };
176 struct tog_parent_tree {
177 TAILQ_ENTRY(tog_parent_tree) entry;
178 struct got_tree_object *tree;
179 struct got_tree_entry *first_displayed_entry;
180 struct got_tree_entry *selected_entry;
181 int selected;
182 };
184 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
186 struct tog_tree_view_state {
187 char *tree_label;
188 struct got_tree_object *root;
189 struct got_tree_object *tree;
190 const struct got_tree_entries *entries;
191 struct got_tree_entry *first_displayed_entry;
192 struct got_tree_entry *last_displayed_entry;
193 struct got_tree_entry *selected_entry;
194 int nentries, ndisplayed, selected, show_ids;
195 struct tog_parent_trees parents;
196 struct got_object_id *commit_id;
197 struct got_repository *repo;
198 };
200 /*
201 * We implement two types of views: parent views and child views.
203 * The 'Tab' key switches between a parent view and its child view.
204 * Child views are shown side-by-side to their parent view, provided
205 * there is enough screen estate.
207 * When a new view is opened from within a parent view, this new view
208 * becomes a child view of the parent view, replacing any existing child.
210 * When a new view is opened from within a child view, this new view
211 * becomes a parent view which will obscure the views below until the
212 * user quits the new parent view by typing 'q'.
214 * This list of views contains parent views only.
215 * Child views are only pointed to by their parent view.
216 */
217 TAILQ_HEAD(tog_view_list_head, tog_view);
219 struct tog_view {
220 TAILQ_ENTRY(tog_view) entry;
221 WINDOW *window;
222 PANEL *panel;
223 int nlines, ncols, begin_y, begin_x;
224 int lines, cols; /* copies of LINES and COLS */
225 int focussed;
226 struct tog_view *parent;
227 struct tog_view *child;
228 int child_focussed;
230 /* type-specific state */
231 enum tog_view_type type;
232 union {
233 struct tog_diff_view_state diff;
234 struct tog_log_view_state log;
235 struct tog_blame_view_state blame;
236 struct tog_tree_view_state tree;
237 } state;
239 const struct got_error *(*show)(struct tog_view *);
240 const struct got_error *(*input)(struct tog_view **,
241 struct tog_view **, struct tog_view**, struct tog_view *, int);
242 const struct got_error *(*close)(struct tog_view *);
243 };
245 static const struct got_error *open_diff_view(struct tog_view *,
246 struct got_object *, struct got_object *, struct got_repository *);
247 static const struct got_error *show_diff_view(struct tog_view *);
248 static const struct got_error *input_diff_view(struct tog_view **,
249 struct tog_view **, struct tog_view **, struct tog_view *, int);
250 static const struct got_error* close_diff_view(struct tog_view *);
252 static const struct got_error *open_log_view(struct tog_view *,
253 struct got_object_id *, struct got_repository *, const char *);
254 static const struct got_error * show_log_view(struct tog_view *);
255 static const struct got_error *input_log_view(struct tog_view **,
256 struct tog_view **, struct tog_view **, struct tog_view *, int);
257 static const struct got_error *close_log_view(struct tog_view *);
259 static const struct got_error *open_blame_view(struct tog_view *, char *,
260 struct got_object_id *, struct got_repository *);
261 static const struct got_error *show_blame_view(struct tog_view *);
262 static const struct got_error *input_blame_view(struct tog_view **,
263 struct tog_view **, struct tog_view **, struct tog_view *, int);
264 static const struct got_error *close_blame_view(struct tog_view *);
266 static const struct got_error *open_tree_view(struct tog_view *,
267 struct got_tree_object *, struct got_object_id *, struct got_repository *);
268 static const struct got_error *show_tree_view(struct tog_view *);
269 static const struct got_error *input_tree_view(struct tog_view **,
270 struct tog_view **, struct tog_view **, struct tog_view *, int);
271 static const struct got_error *close_tree_view(struct tog_view *);
273 static const struct got_error *
274 view_close(struct tog_view *view)
276 const struct got_error *err = NULL;
278 if (view->child) {
279 view_close(view->child);
280 view->child = NULL;
282 if (view->close)
283 err = view->close(view);
284 if (view->panel)
285 del_panel(view->panel);
286 if (view->window)
287 delwin(view->window);
288 free(view);
289 return err;
292 static struct tog_view *
293 view_open(int nlines, int ncols, int begin_y, int begin_x,
294 enum tog_view_type type)
296 struct tog_view *view = calloc(1, sizeof(*view));
298 if (view == NULL)
299 return NULL;
301 view->type = type;
302 view->lines = LINES;
303 view->cols = COLS;
304 view->nlines = nlines ? nlines : LINES - begin_y;
305 view->ncols = ncols ? ncols : COLS - begin_x;
306 view->begin_y = begin_y;
307 view->begin_x = begin_x;
308 view->window = newwin(nlines, ncols, begin_y, begin_x);
309 if (view->window == NULL) {
310 view_close(view);
311 return NULL;
313 view->panel = new_panel(view->window);
314 if (view->panel == NULL ||
315 set_panel_userptr(view->panel, view) != OK) {
316 view_close(view);
317 return NULL;
320 keypad(view->window, TRUE);
321 return view;
324 static int
325 view_split_begin_x(int begin_x)
327 if (begin_x > 0)
328 return 0;
329 return (COLS >= 120 ? COLS/2 : 0);
332 static const struct got_error *view_resize(struct tog_view *);
334 static const struct got_error *
335 view_splitscreen(struct tog_view *view)
337 const struct got_error *err = NULL;
339 view->begin_y = 0;
340 view->begin_x = view_split_begin_x(0);
341 view->nlines = LINES;
342 view->ncols = COLS - view->begin_x;
343 view->lines = LINES;
344 view->cols = COLS;
345 err = view_resize(view);
346 if (err)
347 return err;
349 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
350 return got_error_from_errno();
352 return NULL;
355 static const struct got_error *
356 view_fullscreen(struct tog_view *view)
358 const struct got_error *err = NULL;
360 view->begin_x = 0;
361 view->begin_y = 0;
362 view->nlines = LINES;
363 view->ncols = COLS;
364 view->lines = LINES;
365 view->cols = COLS;
366 err = view_resize(view);
367 if (err)
368 return err;
370 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
371 return got_error_from_errno();
373 return NULL;
376 static int
377 view_is_parent_view(struct tog_view *view)
379 return view->parent == NULL;
382 static const struct got_error *
383 view_resize(struct tog_view *view)
385 int nlines, ncols;
387 if (view->lines > LINES)
388 nlines = view->nlines - (view->lines - LINES);
389 else
390 nlines = view->nlines + (LINES - view->lines);
392 if (view->cols > COLS)
393 ncols = view->ncols - (view->cols - COLS);
394 else
395 ncols = view->ncols + (COLS - view->cols);
397 if (wresize(view->window, nlines, ncols) == ERR)
398 return got_error_from_errno();
399 replace_panel(view->panel, view->window);
401 view->nlines = nlines;
402 view->ncols = ncols;
403 view->lines = LINES;
404 view->cols = COLS;
406 if (view->child) {
407 view->child->begin_x = view_split_begin_x(view->begin_x);
408 if (view->child->begin_x == 0) {
409 view_fullscreen(view->child);
410 if (view->child->focussed)
411 show_panel(view->child->panel);
412 else
413 show_panel(view->panel);
414 } else {
415 view_splitscreen(view->child);
416 show_panel(view->child->panel);
420 return NULL;
423 static const struct got_error *
424 view_close_child(struct tog_view *view)
426 const struct got_error *err;
428 if (view->child == NULL)
429 return NULL;
431 err = view_close(view->child);
432 view->child = NULL;
433 return err;
436 static const struct got_error *
437 view_set_child(struct tog_view *view, struct tog_view *child)
439 const struct got_error *err = NULL;
441 view->child = child;
442 child->parent = view;
443 return err;
446 static int
447 view_is_splitscreen(struct tog_view *view)
449 return !view_is_parent_view(view) && view->begin_x > 0;
452 static const struct got_error *
453 view_input(struct tog_view **new, struct tog_view **dead,
454 struct tog_view **focus, int *done, struct tog_view *view,
455 struct tog_view_list_head *views)
457 const struct got_error *err = NULL;
458 struct tog_view *v;
459 int ch;
461 *new = NULL;
462 *dead = NULL;
463 *focus = NULL;
465 nodelay(stdscr, FALSE);
466 ch = wgetch(view->window);
467 nodelay(stdscr, TRUE);
468 switch (ch) {
469 case ERR:
470 break;
471 case '\t':
472 if (view->child) {
473 *focus = view->child;
474 view->child_focussed = 1;
475 } else if (view->parent) {
476 *focus = view->parent;
477 view->parent->child_focussed = 0;
479 break;
480 case 'q':
481 err = view->input(new, dead, focus, view, ch);
482 *dead = view;
483 break;
484 case 'Q':
485 *done = 1;
486 break;
487 case 'f':
488 if (view_is_parent_view(view)) {
489 if (view->child == NULL)
490 break;
491 if (view_is_splitscreen(view->child)) {
492 *focus = view->child;
493 view->child_focussed = 1;
494 err = view_fullscreen(view->child);
495 } else
496 err = view_splitscreen(view->child);
497 if (err)
498 break;
499 err = view->child->input(new, dead, focus,
500 view->child, KEY_RESIZE);
501 } else {
502 if (view_is_splitscreen(view)) {
503 *focus = view;
504 view->parent->child_focussed = 1;
505 err = view_fullscreen(view);
506 } else {
507 err = view_splitscreen(view);
509 if (err)
510 break;
511 err = view->input(new, dead, focus, view,
512 KEY_RESIZE);
514 break;
515 case KEY_RESIZE:
516 TAILQ_FOREACH(v, views, entry) {
517 err = view_resize(v);
518 if (err)
519 return err;
520 err = v->input(new, dead, focus, v, ch);
522 break;
523 default:
524 err = view->input(new, dead, focus, view, ch);
525 break;
528 return err;
531 void
532 view_vborder(struct tog_view *view)
534 PANEL *panel;
535 struct tog_view *view_above;
537 if (view->parent)
538 return view_vborder(view->parent);
540 panel = panel_above(view->panel);
541 if (panel == NULL)
542 return;
544 view_above = panel_userptr(panel);
545 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
546 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
549 int
550 view_needs_focus_indication(struct tog_view *view)
552 if (view_is_parent_view(view)) {
553 if (view->child == NULL || view->child_focussed)
554 return 0;
555 if (!view_is_splitscreen(view->child))
556 return 0;
557 } else if (!view_is_splitscreen(view))
558 return 0;
560 return view->focussed;
563 static const struct got_error *
564 view_loop(struct tog_view *view)
566 const struct got_error *err = NULL;
567 struct tog_view_list_head views;
568 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
569 int done = 0;
571 TAILQ_INIT(&views);
572 TAILQ_INSERT_HEAD(&views, view, entry);
574 main_view = view;
575 view->focussed = 1;
576 err = view->show(view);
577 if (err)
578 return err;
579 update_panels();
580 doupdate();
581 while (!TAILQ_EMPTY(&views) && !done) {
582 err = view_input(&new_view, &dead_view, &focus_view, &done,
583 view, &views);
584 if (err)
585 break;
586 if (dead_view) {
587 struct tog_view *prev = NULL;
589 if (view_is_parent_view(dead_view))
590 prev = TAILQ_PREV(dead_view,
591 tog_view_list_head, entry);
592 else
593 prev = view->parent;
595 if (dead_view->parent)
596 dead_view->parent->child = NULL;
597 else
598 TAILQ_REMOVE(&views, dead_view, entry);
600 err = view_close(dead_view);
601 if (err || dead_view == main_view)
602 goto done;
604 if (view == dead_view) {
605 if (focus_view)
606 view = focus_view;
607 else if (prev)
608 view = prev;
609 else if (!TAILQ_EMPTY(&views))
610 view = TAILQ_LAST(&views,
611 tog_view_list_head);
612 else
613 view = NULL;
614 if (view) {
615 if (view->child && view->child_focussed)
616 focus_view = view->child;
617 else
618 focus_view = view;
622 if (new_view) {
623 struct tog_view *v, *t;
624 /* Only allow one parent view per type. */
625 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
626 if (v->type != new_view->type)
627 continue;
628 TAILQ_REMOVE(&views, v, entry);
629 err = view_close(v);
630 if (err)
631 goto done;
632 if (v == view)
633 view = new_view;
634 break;
636 TAILQ_INSERT_TAIL(&views, new_view, entry);
637 if (focus_view == NULL)
638 focus_view = new_view;
640 if (focus_view) {
641 show_panel(focus_view->panel);
642 if (view)
643 view->focussed = 0;
644 focus_view->focussed = 1;
645 view = focus_view;
646 if (new_view)
647 show_panel(new_view->panel);
648 if (view->child && view_is_splitscreen(view->child))
649 show_panel(view->child->panel);
651 if (view) {
652 if (view->parent) {
653 err = view->parent->show(view->parent);
654 if (err)
655 return err;
657 err = view->show(view);
658 if (err)
659 return err;
660 if (view->child) {
661 err = view->child->show(view->child);
662 if (err)
663 return err;
666 update_panels();
667 doupdate();
669 done:
670 while (!TAILQ_EMPTY(&views)) {
671 view = TAILQ_FIRST(&views);
672 TAILQ_REMOVE(&views, view, entry);
673 view_close(view);
675 return err;
678 __dead static void
679 usage_log(void)
681 endwin();
682 fprintf(stderr,
683 "usage: %s log [-c commit] [-r repository-path] [path]\n",
684 getprogname());
685 exit(1);
688 /* Create newly allocated wide-character string equivalent to a byte string. */
689 static const struct got_error *
690 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
692 char *vis = NULL;
693 const struct got_error *err = NULL;
695 *ws = NULL;
696 *wlen = mbstowcs(NULL, s, 0);
697 if (*wlen == (size_t)-1) {
698 int vislen;
699 if (errno != EILSEQ)
700 return got_error_from_errno();
702 /* byte string invalid in current encoding; try to "fix" it */
703 err = got_mbsavis(&vis, &vislen, s);
704 if (err)
705 return err;
706 *wlen = mbstowcs(NULL, vis, 0);
707 if (*wlen == (size_t)-1) {
708 err = got_error_from_errno(); /* give up */
709 goto done;
713 *ws = calloc(*wlen + 1, sizeof(*ws));
714 if (*ws == NULL) {
715 err = got_error_from_errno();
716 goto done;
719 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
720 err = got_error_from_errno();
721 done:
722 free(vis);
723 if (err) {
724 free(*ws);
725 *ws = NULL;
726 *wlen = 0;
728 return err;
731 /* Format a line for display, ensuring that it won't overflow a width limit. */
732 static const struct got_error *
733 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
735 const struct got_error *err = NULL;
736 int cols = 0;
737 wchar_t *wline = NULL;
738 size_t wlen;
739 int i;
741 *wlinep = NULL;
742 *widthp = 0;
744 err = mbs2ws(&wline, &wlen, line);
745 if (err)
746 return err;
748 i = 0;
749 while (i < wlen && cols < wlimit) {
750 int width = wcwidth(wline[i]);
751 switch (width) {
752 case 0:
753 i++;
754 break;
755 case 1:
756 case 2:
757 if (cols + width <= wlimit)
758 cols += width;
759 i++;
760 break;
761 case -1:
762 if (wline[i] == L'\t')
763 cols += TABSIZE - ((cols + 1) % TABSIZE);
764 i++;
765 break;
766 default:
767 err = got_error_from_errno();
768 goto done;
771 wline[i] = L'\0';
772 if (widthp)
773 *widthp = cols;
774 done:
775 if (err)
776 free(wline);
777 else
778 *wlinep = wline;
779 return err;
782 static const struct got_error *
783 draw_commit(struct tog_view *view, struct got_commit_object *commit,
784 struct got_object_id *id)
786 const struct got_error *err = NULL;
787 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
788 char *logmsg0 = NULL, *logmsg = NULL;
789 char *author0 = NULL, *author = NULL;
790 wchar_t *wlogmsg = NULL, *wauthor = NULL;
791 int author_width, logmsg_width;
792 char *newline, *smallerthan;
793 char *line = NULL;
794 int col, limit;
795 static const size_t date_display_cols = 9;
796 static const size_t author_display_cols = 16;
797 const int avail = view->ncols;
799 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
800 &commit->tm_committer) >= sizeof(datebuf))
801 return got_error(GOT_ERR_NO_SPACE);
803 if (avail < date_display_cols)
804 limit = MIN(sizeof(datebuf) - 1, avail);
805 else
806 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
807 waddnstr(view->window, datebuf, limit);
808 col = limit + 1;
809 if (col > avail)
810 goto done;
812 author0 = strdup(commit->author);
813 if (author0 == NULL) {
814 err = got_error_from_errno();
815 goto done;
817 author = author0;
818 smallerthan = strchr(author, '<');
819 if (smallerthan)
820 *smallerthan = '\0';
821 else {
822 char *at = strchr(author, '@');
823 if (at)
824 *at = '\0';
826 limit = avail - col;
827 err = format_line(&wauthor, &author_width, author, limit);
828 if (err)
829 goto done;
830 waddwstr(view->window, wauthor);
831 col += author_width;
832 while (col <= avail && author_width < author_display_cols + 1) {
833 waddch(view->window, ' ');
834 col++;
835 author_width++;
837 if (col > avail)
838 goto done;
840 logmsg0 = strdup(commit->logmsg);
841 if (logmsg0 == NULL) {
842 err = got_error_from_errno();
843 goto done;
845 logmsg = logmsg0;
846 while (*logmsg == '\n')
847 logmsg++;
848 newline = strchr(logmsg, '\n');
849 if (newline)
850 *newline = '\0';
851 limit = avail - col;
852 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
853 if (err)
854 goto done;
855 waddwstr(view->window, wlogmsg);
856 col += logmsg_width;
857 while (col <= avail) {
858 waddch(view->window, ' ');
859 col++;
861 done:
862 free(logmsg0);
863 free(wlogmsg);
864 free(author0);
865 free(wauthor);
866 free(line);
867 return err;
870 static struct commit_queue_entry *
871 alloc_commit_queue_entry(struct got_commit_object *commit,
872 struct got_object_id *id)
874 struct commit_queue_entry *entry;
876 entry = calloc(1, sizeof(*entry));
877 if (entry == NULL)
878 return NULL;
880 entry->id = id;
881 entry->commit = commit;
882 return entry;
885 static void
886 pop_commit(struct commit_queue *commits)
888 struct commit_queue_entry *entry;
890 entry = TAILQ_FIRST(&commits->head);
891 TAILQ_REMOVE(&commits->head, entry, entry);
892 got_object_commit_close(entry->commit);
893 commits->ncommits--;
894 /* Don't free entry->id! It is owned by the commit graph. */
895 free(entry);
898 static void
899 free_commits(struct commit_queue *commits)
901 while (!TAILQ_EMPTY(&commits->head))
902 pop_commit(commits);
905 static const struct got_error *
906 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
907 struct got_object_id *start_id, int minqueue,
908 struct got_repository *repo, const char *path)
910 const struct got_error *err = NULL;
911 int nqueued = 0;
913 if (start_id) {
914 err = got_commit_graph_iter_start(graph, start_id, repo);
915 if (err)
916 return err;
919 while (nqueued < minqueue) {
920 struct got_object_id *id;
921 struct got_commit_object *commit;
922 struct commit_queue_entry *entry;
924 err = got_commit_graph_iter_next(&id, graph);
925 if (err) {
926 if (err->code == GOT_ERR_ITER_COMPLETED) {
927 err = NULL;
928 break;
930 if (err->code != GOT_ERR_ITER_NEED_MORE)
931 break;
932 err = got_commit_graph_fetch_commits(graph,
933 minqueue, repo);
934 if (err)
935 return err;
936 continue;
939 if (id == NULL)
940 break;
942 err = got_object_open_as_commit(&commit, repo, id);
943 if (err)
944 break;
945 entry = alloc_commit_queue_entry(commit, id);
946 if (entry == NULL) {
947 err = got_error_from_errno();
948 break;
951 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
952 nqueued++;
953 commits->ncommits++;
956 return err;
959 static const struct got_error *
960 fetch_next_commit(struct commit_queue_entry **pentry,
961 struct commit_queue_entry *entry, struct commit_queue *commits,
962 struct got_commit_graph *graph, struct got_repository *repo,
963 const char *path)
965 const struct got_error *err = NULL;
967 *pentry = NULL;
969 err = queue_commits(graph, commits, NULL, 1, repo, path);
970 if (err)
971 return err;
973 /* Next entry to display should now be available. */
974 *pentry = TAILQ_NEXT(entry, entry);
975 return NULL;
978 static const struct got_error *
979 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
981 const struct got_error *err = NULL;
982 struct got_reference *head_ref;
984 *head_id = NULL;
986 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
987 if (err)
988 return err;
990 err = got_ref_resolve(head_id, repo, head_ref);
991 got_ref_close(head_ref);
992 if (err) {
993 *head_id = NULL;
994 return err;
997 return NULL;
1000 static const struct got_error *
1001 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1002 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1003 struct commit_queue *commits, int selected_idx, int limit,
1004 struct got_commit_graph *graph, struct got_repository *repo,
1005 const char *path)
1007 const struct got_error *err = NULL;
1008 struct commit_queue_entry *entry;
1009 int ncommits, width;
1010 char *id_str, *header;
1011 wchar_t *wline;
1013 entry = first;
1014 ncommits = 0;
1015 while (entry) {
1016 if (ncommits == selected_idx) {
1017 *selected = entry;
1018 break;
1020 entry = TAILQ_NEXT(entry, entry);
1021 ncommits++;
1024 err = got_object_id_str(&id_str, (*selected)->id);
1025 if (err)
1026 return err;
1028 if (path && strcmp(path, "/") != 0) {
1029 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
1030 err = got_error_from_errno();
1031 free(id_str);
1032 return err;
1034 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1035 err = got_error_from_errno();
1036 free(id_str);
1037 return err;
1039 free(id_str);
1040 err = format_line(&wline, &width, header, view->ncols);
1041 if (err) {
1042 free(header);
1043 return err;
1045 free(header);
1047 werase(view->window);
1049 if (view_needs_focus_indication(view))
1050 wstandout(view->window);
1051 waddwstr(view->window, wline);
1052 if (view_needs_focus_indication(view))
1053 wstandend(view->window);
1054 if (width < view->ncols)
1055 waddch(view->window, '\n');
1056 free(wline);
1057 if (limit <= 1)
1058 return NULL;
1060 entry = first;
1061 *last = first;
1062 ncommits = 0;
1063 while (entry) {
1064 if (ncommits >= limit - 1)
1065 break;
1066 if (view->focussed && ncommits == selected_idx)
1067 wstandout(view->window);
1068 err = draw_commit(view, entry->commit, entry->id);
1069 if (view->focussed && ncommits == selected_idx)
1070 wstandend(view->window);
1071 if (err)
1072 break;
1073 ncommits++;
1074 *last = entry;
1075 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1076 err = queue_commits(graph, commits, NULL, 1,
1077 repo, path);
1078 if (err) {
1079 if (err->code != GOT_ERR_ITER_COMPLETED)
1080 return err;
1081 err = NULL;
1084 entry = TAILQ_NEXT(entry, entry);
1087 view_vborder(view);
1089 return err;
1092 static void
1093 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1094 struct commit_queue *commits)
1096 struct commit_queue_entry *entry;
1097 int nscrolled = 0;
1099 entry = TAILQ_FIRST(&commits->head);
1100 if (*first_displayed_entry == entry)
1101 return;
1103 entry = *first_displayed_entry;
1104 while (entry && nscrolled < maxscroll) {
1105 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1106 if (entry) {
1107 *first_displayed_entry = entry;
1108 nscrolled++;
1113 static const struct got_error *
1114 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1115 struct commit_queue_entry **last_displayed_entry,
1116 struct commit_queue *commits, struct got_commit_graph *graph,
1117 struct got_repository *repo, const char *path)
1119 const struct got_error *err = NULL;
1120 struct commit_queue_entry *pentry;
1121 int nscrolled = 0;
1123 do {
1124 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1125 if (pentry == NULL) {
1126 err = fetch_next_commit(&pentry, *last_displayed_entry,
1127 commits, graph, repo, path);
1128 if (err || pentry == NULL)
1129 break;
1131 *last_displayed_entry = pentry;
1133 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1134 if (pentry == NULL)
1135 break;
1136 *first_displayed_entry = pentry;
1137 } while (++nscrolled < maxscroll);
1139 return err;
1142 static const struct got_error *
1143 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1144 struct got_object_id *commit_id, struct got_commit_object *commit,
1145 struct got_repository *repo)
1147 const struct got_error *err;
1148 struct got_object *obj1 = NULL, *obj2 = NULL;
1149 struct got_object_qid *parent_id;
1150 struct tog_view *diff_view;
1152 err = got_object_open(&obj2, repo, commit_id);
1153 if (err)
1154 return err;
1156 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1157 if (parent_id) {
1158 err = got_object_open(&obj1, repo, parent_id->id);
1159 if (err)
1160 goto done;
1163 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1164 if (diff_view == NULL) {
1165 err = got_error_from_errno();
1166 goto done;
1169 err = open_diff_view(diff_view, obj1, obj2, repo);
1170 if (err == NULL)
1171 *new_view = diff_view;
1172 done:
1173 if (obj1)
1174 got_object_close(obj1);
1175 if (obj2)
1176 got_object_close(obj2);
1177 return err;
1180 static const struct got_error *
1181 browse_commit(struct tog_view **new_view, int begin_x,
1182 struct commit_queue_entry *entry, struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 struct got_tree_object *tree;
1186 struct tog_view *tree_view;
1188 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1189 if (err)
1190 return err;
1192 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1193 if (tree_view == NULL)
1194 return got_error_from_errno();
1196 err = open_tree_view(tree_view, tree, entry->id, repo);
1197 if (err)
1198 got_object_tree_close(tree);
1199 else
1200 *new_view = tree_view;
1201 return err;
1204 static const struct got_error *
1205 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1206 struct got_repository *repo, const char *path)
1208 const struct got_error *err = NULL;
1209 struct tog_log_view_state *s = &view->state.log;
1211 err = got_repo_map_path(&s->in_repo_path, repo, path);
1212 if (err != NULL)
1213 goto done;
1215 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1216 0, repo);
1217 if (err)
1218 goto done;
1219 /* The commit queue only contains commits being displayed. */
1220 TAILQ_INIT(&s->commits.head);
1221 s->commits.ncommits = 0;
1224 * Open the initial batch of commits, sorted in commit graph order.
1225 * We keep all commits open throughout the lifetime of the log view
1226 * in order to avoid having to re-fetch commits from disk while
1227 * updating the display.
1229 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1230 repo, s->in_repo_path);
1231 if (err) {
1232 if (err->code != GOT_ERR_ITER_COMPLETED)
1233 goto done;
1234 err = NULL;
1237 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1238 s->selected_entry = s->first_displayed_entry;
1239 s->repo = repo;
1240 s->start_id = got_object_id_dup(start_id);
1241 if (s->start_id == NULL) {
1242 err = got_error_from_errno();
1243 goto done;
1246 view->show = show_log_view;
1247 view->input = input_log_view;
1248 view->close = close_log_view;
1249 done:
1250 return err;
1253 static const struct got_error *
1254 close_log_view(struct tog_view *view)
1256 struct tog_log_view_state *s = &view->state.log;
1258 if (s->graph)
1259 got_commit_graph_close(s->graph);
1260 free_commits(&s->commits);
1261 free(s->in_repo_path);
1262 free(s->start_id);
1263 return NULL;
1266 static const struct got_error *
1267 show_log_view(struct tog_view *view)
1269 const struct got_error *err = NULL;
1270 struct tog_log_view_state *s = &view->state.log;
1272 return draw_commits(view, &s->last_displayed_entry,
1273 &s->selected_entry, s->first_displayed_entry,
1274 &s->commits, s->selected, view->nlines, s->graph,
1275 s->repo, s->in_repo_path);
1276 if (err)
1277 return err;
1280 static const struct got_error *
1281 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 struct tog_view **focus_view, struct tog_view *view, int ch)
1284 const struct got_error *err = NULL;
1285 struct tog_log_view_state *s = &view->state.log;
1286 char *parent_path;
1287 struct tog_view *diff_view = NULL, *tree_view = NULL;
1288 int begin_x = 0;
1290 switch (ch) {
1291 case 'k':
1292 case KEY_UP:
1293 if (s->selected > 0)
1294 s->selected--;
1295 if (s->selected > 0)
1296 break;
1297 scroll_up(&s->first_displayed_entry, 1,
1298 &s->commits);
1299 break;
1300 case KEY_PPAGE:
1301 if (TAILQ_FIRST(&s->commits.head) ==
1302 s->first_displayed_entry) {
1303 s->selected = 0;
1304 break;
1306 scroll_up(&s->first_displayed_entry,
1307 view->nlines, &s->commits);
1308 break;
1309 case 'j':
1310 case KEY_DOWN:
1311 if (s->selected < MIN(view->nlines - 2,
1312 s->commits.ncommits - 1)) {
1313 s->selected++;
1314 break;
1316 err = scroll_down(&s->first_displayed_entry, 1,
1317 &s->last_displayed_entry, &s->commits,
1318 s->graph, s->repo, s->in_repo_path);
1319 if (err) {
1320 if (err->code != GOT_ERR_ITER_COMPLETED)
1321 break;
1322 err = NULL;
1324 break;
1325 case KEY_NPAGE: {
1326 struct commit_queue_entry *first;
1327 first = s->first_displayed_entry;
1328 err = scroll_down(&s->first_displayed_entry,
1329 view->nlines, &s->last_displayed_entry,
1330 &s->commits, s->graph, s->repo,
1331 s->in_repo_path);
1332 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1333 break;
1334 if (first == s->first_displayed_entry &&
1335 s->selected < MIN(view->nlines - 2,
1336 s->commits.ncommits - 1)) {
1337 /* can't scroll further down */
1338 s->selected = MIN(view->nlines - 2,
1339 s->commits.ncommits - 1);
1341 err = NULL;
1342 break;
1344 case KEY_RESIZE:
1345 if (s->selected > view->nlines - 2)
1346 s->selected = view->nlines - 2;
1347 if (s->selected > s->commits.ncommits - 1)
1348 s->selected = s->commits.ncommits - 1;
1349 break;
1350 case KEY_ENTER:
1351 case '\r':
1352 if (view_is_parent_view(view))
1353 begin_x = view_split_begin_x(view->begin_x);
1354 err = open_diff_view_for_commit(&diff_view, begin_x,
1355 s->selected_entry->id, s->selected_entry->commit,
1356 s->repo);
1357 if (err)
1358 break;
1359 if (view_is_parent_view(view)) {
1360 err = view_close_child(view);
1361 if (err)
1362 return err;
1363 err = view_set_child(view, diff_view);
1364 if (err) {
1365 view_close(diff_view);
1366 break;
1368 if (!view_is_splitscreen(diff_view)) {
1369 *focus_view = diff_view;
1370 view->child_focussed = 1;
1372 } else
1373 *new_view = diff_view;
1374 break;
1375 case 't':
1376 if (view_is_parent_view(view))
1377 begin_x = view_split_begin_x(view->begin_x);
1378 err = browse_commit(&tree_view, begin_x,
1379 s->selected_entry, s->repo);
1380 if (view_is_parent_view(view)) {
1381 err = view_close_child(view);
1382 if (err)
1383 return err;
1384 err = view_set_child(view, tree_view);
1385 if (err) {
1386 view_close(tree_view);
1387 break;
1389 *focus_view = tree_view;
1390 view->child_focussed = 1;
1391 } else
1392 *new_view = tree_view;
1393 break;
1394 case KEY_BACKSPACE:
1395 if (strcmp(s->in_repo_path, "/") == 0)
1396 break;
1397 parent_path = dirname(s->in_repo_path);
1398 if (parent_path && strcmp(parent_path, ".") != 0) {
1399 struct tog_view *lv;
1400 lv = view_open(view->nlines, view->ncols,
1401 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1402 if (lv == NULL)
1403 return got_error_from_errno();
1404 err = open_log_view(lv, s->start_id, s->repo,
1405 parent_path);
1406 if (err)
1407 break;
1408 if (view_is_parent_view(view))
1409 *new_view = lv;
1410 else {
1411 view_set_child(view->parent, lv);
1412 *dead_view = view;
1415 break;
1416 default:
1417 break;
1420 return err;
1423 static const struct got_error *
1424 cmd_log(int argc, char *argv[])
1426 const struct got_error *error;
1427 struct got_repository *repo = NULL;
1428 struct got_object_id *start_id = NULL;
1429 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1430 char *start_commit = NULL;
1431 int ch;
1432 struct tog_view *view;
1434 #ifndef PROFILE
1435 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1436 == -1)
1437 err(1, "pledge");
1438 #endif
1440 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1441 switch (ch) {
1442 case 'c':
1443 start_commit = optarg;
1444 break;
1445 case 'r':
1446 repo_path = realpath(optarg, NULL);
1447 if (repo_path == NULL)
1448 err(1, "-r option");
1449 break;
1450 default:
1451 usage();
1452 /* NOTREACHED */
1456 argc -= optind;
1457 argv += optind;
1459 if (argc == 0)
1460 path = strdup("");
1461 else if (argc == 1)
1462 path = strdup(argv[0]);
1463 else
1464 usage_log();
1465 if (path == NULL)
1466 return got_error_from_errno();
1468 cwd = getcwd(NULL, 0);
1469 if (cwd == NULL) {
1470 error = got_error_from_errno();
1471 goto done;
1473 if (repo_path == NULL) {
1474 repo_path = strdup(cwd);
1475 if (repo_path == NULL) {
1476 error = got_error_from_errno();
1477 goto done;
1481 error = got_repo_open(&repo, repo_path);
1482 if (error != NULL)
1483 goto done;
1485 if (start_commit == NULL) {
1486 error = get_head_commit_id(&start_id, repo);
1487 if (error != NULL)
1488 goto done;
1489 } else {
1490 struct got_object *obj;
1491 error = got_object_open_by_id_str(&obj, repo, start_commit);
1492 if (error == NULL) {
1493 start_id = got_object_id_dup(got_object_get_id(obj));
1494 if (start_id == NULL)
1495 error = got_error_from_errno();
1496 goto done;
1499 if (error != NULL)
1500 goto done;
1502 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1503 if (view == NULL) {
1504 error = got_error_from_errno();
1505 goto done;
1507 error = open_log_view(view, start_id, repo, path);
1508 if (error)
1509 goto done;
1510 error = view_loop(view);
1511 done:
1512 free(repo_path);
1513 free(cwd);
1514 free(path);
1515 free(start_id);
1516 if (repo)
1517 got_repo_close(repo);
1518 return error;
1521 __dead static void
1522 usage_diff(void)
1524 endwin();
1525 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1526 getprogname());
1527 exit(1);
1530 static char *
1531 parse_next_line(FILE *f, size_t *len)
1533 char *line;
1534 size_t linelen;
1535 size_t lineno;
1536 const char delim[3] = { '\0', '\0', '\0'};
1538 line = fparseln(f, &linelen, &lineno, delim, 0);
1539 if (len)
1540 *len = linelen;
1541 return line;
1544 static const struct got_error *
1545 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1546 int *last_displayed_line, int *eof, int max_lines,
1547 char * header)
1549 const struct got_error *err;
1550 int nlines = 0, nprinted = 0;
1551 char *line;
1552 size_t len;
1553 wchar_t *wline;
1554 int width;
1556 rewind(f);
1557 werase(view->window);
1559 if (header) {
1560 err = format_line(&wline, &width, header, view->ncols);
1561 if (err) {
1562 return err;
1565 if (view_needs_focus_indication(view))
1566 wstandout(view->window);
1567 waddwstr(view->window, wline);
1568 if (view_needs_focus_indication(view))
1569 wstandend(view->window);
1570 if (width < view->ncols)
1571 waddch(view->window, '\n');
1573 if (max_lines <= 1)
1574 return NULL;
1575 max_lines--;
1578 *eof = 0;
1579 while (nprinted < max_lines) {
1580 line = parse_next_line(f, &len);
1581 if (line == NULL) {
1582 *eof = 1;
1583 break;
1585 if (++nlines < *first_displayed_line) {
1586 free(line);
1587 continue;
1590 err = format_line(&wline, &width, line, view->ncols);
1591 if (err) {
1592 free(line);
1593 return err;
1595 waddwstr(view->window, wline);
1596 if (width < view->ncols)
1597 waddch(view->window, '\n');
1598 if (++nprinted == 1)
1599 *first_displayed_line = nlines;
1600 free(line);
1601 free(wline);
1602 wline = NULL;
1604 *last_displayed_line = nlines;
1606 view_vborder(view);
1608 return NULL;
1611 static const struct got_error *
1612 create_diff(struct tog_diff_view_state *s)
1614 const struct got_error *err = NULL;
1615 struct got_object *obj1 = NULL, *obj2 = NULL;
1616 FILE *f = NULL;
1618 if (s->id1) {
1619 err = got_object_open(&obj1, s->repo, s->id1);
1620 if (err)
1621 return err;
1624 err = got_object_open(&obj2, s->repo, s->id2);
1625 if (err)
1626 goto done;
1628 f = got_opentemp();
1629 if (f == NULL) {
1630 err = got_error_from_errno();
1631 goto done;
1633 if (s->f)
1634 fclose(s->f);
1635 s->f = f;
1637 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1638 case GOT_OBJ_TYPE_BLOB:
1639 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1640 s->diff_context, s->repo, f);
1641 break;
1642 case GOT_OBJ_TYPE_TREE:
1643 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1644 s->diff_context, s->repo, f);
1645 break;
1646 case GOT_OBJ_TYPE_COMMIT:
1647 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1648 s->repo, f);
1649 break;
1650 default:
1651 err = got_error(GOT_ERR_OBJ_TYPE);
1652 break;
1654 done:
1655 if (obj1)
1656 got_object_close(obj1);
1657 got_object_close(obj2);
1658 if (f)
1659 fflush(f);
1660 return err;
1663 static const struct got_error *
1664 open_diff_view(struct tog_view *view, struct got_object *obj1,
1665 struct got_object *obj2, struct got_repository *repo)
1667 const struct got_error *err;
1669 if (obj1 != NULL && obj2 != NULL &&
1670 got_object_get_type(obj1) != got_object_get_type(obj2))
1671 return got_error(GOT_ERR_OBJ_TYPE);
1673 if (obj1) {
1674 struct got_object_id *id1;
1675 id1 = got_object_id_dup(got_object_get_id(obj1));
1676 if (id1 == NULL)
1677 return got_error_from_errno();
1678 view->state.diff.id1 = id1;
1679 } else
1680 view->state.diff.id1 = NULL;
1682 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1683 if (view->state.diff.id2 == NULL) {
1684 free(view->state.diff.id1);
1685 view->state.diff.id1 = NULL;
1686 return got_error_from_errno();
1688 view->state.diff.f = NULL;
1689 view->state.diff.first_displayed_line = 1;
1690 view->state.diff.last_displayed_line = view->nlines;
1691 view->state.diff.diff_context = 3;
1692 view->state.diff.repo = repo;
1694 err = create_diff(&view->state.diff);
1695 if (err) {
1696 free(view->state.diff.id1);
1697 view->state.diff.id1 = NULL;
1698 free(view->state.diff.id2);
1699 view->state.diff.id2 = NULL;
1700 return err;
1703 view->show = show_diff_view;
1704 view->input = input_diff_view;
1705 view->close = close_diff_view;
1707 return NULL;
1710 static const struct got_error *
1711 close_diff_view(struct tog_view *view)
1713 const struct got_error *err = NULL;
1715 free(view->state.diff.id1);
1716 view->state.diff.id1 = NULL;
1717 free(view->state.diff.id2);
1718 view->state.diff.id2 = NULL;
1719 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1720 err = got_error_from_errno();
1721 return err;
1724 static const struct got_error *
1725 show_diff_view(struct tog_view *view)
1727 const struct got_error *err;
1728 struct tog_diff_view_state *s = &view->state.diff;
1729 char *id_str1 = NULL, *id_str2, *header;
1731 if (s->id1) {
1732 err = got_object_id_str(&id_str1, s->id1);
1733 if (err)
1734 return err;
1736 err = got_object_id_str(&id_str2, s->id2);
1737 if (err)
1738 return err;
1740 if (asprintf(&header, "diff: %s %s",
1741 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1742 err = got_error_from_errno();
1743 free(id_str1);
1744 free(id_str2);
1745 return err;
1747 free(id_str1);
1748 free(id_str2);
1750 return draw_file(view, s->f, &s->first_displayed_line,
1751 &s->last_displayed_line, &s->eof, view->nlines,
1752 header);
1755 static const struct got_error *
1756 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1757 struct tog_view **focus_view, struct tog_view *view, int ch)
1759 const struct got_error *err = NULL;
1760 struct tog_diff_view_state *s = &view->state.diff;
1761 int i;
1763 switch (ch) {
1764 case 'k':
1765 case KEY_UP:
1766 if (s->first_displayed_line > 1)
1767 s->first_displayed_line--;
1768 break;
1769 case KEY_PPAGE:
1770 i = 0;
1771 while (i++ < view->nlines - 1 &&
1772 s->first_displayed_line > 1)
1773 s->first_displayed_line--;
1774 break;
1775 case 'j':
1776 case KEY_DOWN:
1777 if (!s->eof)
1778 s->first_displayed_line++;
1779 break;
1780 case KEY_NPAGE:
1781 case ' ':
1782 i = 0;
1783 while (!s->eof && i++ < view->nlines - 1) {
1784 char *line;
1785 line = parse_next_line(s->f, NULL);
1786 s->first_displayed_line++;
1787 if (line == NULL)
1788 break;
1790 break;
1791 case '[':
1792 if (s->diff_context > 0) {
1793 s->diff_context--;
1794 err = create_diff(s);
1796 break;
1797 case ']':
1798 if (s->diff_context < INT_MAX) {
1799 s->diff_context++;
1800 err = create_diff(s);
1802 break;
1803 default:
1804 break;
1807 return err;
1810 static const struct got_error *
1811 cmd_diff(int argc, char *argv[])
1813 const struct got_error *error = NULL;
1814 struct got_repository *repo = NULL;
1815 struct got_object *obj1 = NULL, *obj2 = NULL;
1816 char *repo_path = NULL;
1817 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1818 int ch;
1819 struct tog_view *view;
1821 #ifndef PROFILE
1822 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1823 == -1)
1824 err(1, "pledge");
1825 #endif
1827 while ((ch = getopt(argc, argv, "")) != -1) {
1828 switch (ch) {
1829 default:
1830 usage();
1831 /* NOTREACHED */
1835 argc -= optind;
1836 argv += optind;
1838 if (argc == 0) {
1839 usage_diff(); /* TODO show local worktree changes */
1840 } else if (argc == 2) {
1841 repo_path = getcwd(NULL, 0);
1842 if (repo_path == NULL)
1843 return got_error_from_errno();
1844 obj_id_str1 = argv[0];
1845 obj_id_str2 = argv[1];
1846 } else if (argc == 3) {
1847 repo_path = realpath(argv[0], NULL);
1848 if (repo_path == NULL)
1849 return got_error_from_errno();
1850 obj_id_str1 = argv[1];
1851 obj_id_str2 = argv[2];
1852 } else
1853 usage_diff();
1855 error = got_repo_open(&repo, repo_path);
1856 free(repo_path);
1857 if (error)
1858 goto done;
1860 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1861 if (error)
1862 goto done;
1864 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1865 if (error)
1866 goto done;
1868 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1869 if (view == NULL) {
1870 error = got_error_from_errno();
1871 goto done;
1873 error = open_diff_view(view, obj1, obj2, repo);
1874 if (error)
1875 goto done;
1876 error = view_loop(view);
1877 done:
1878 got_repo_close(repo);
1879 if (obj1)
1880 got_object_close(obj1);
1881 if (obj2)
1882 got_object_close(obj2);
1883 return error;
1886 __dead static void
1887 usage_blame(void)
1889 endwin();
1890 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1891 getprogname());
1892 exit(1);
1895 struct tog_blame_line {
1896 int annotated;
1897 struct got_object_id *id;
1900 static const struct got_error *
1901 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1902 const char *path, struct tog_blame_line *lines, int nlines,
1903 int blame_complete, int selected_line, int *first_displayed_line,
1904 int *last_displayed_line, int *eof, int max_lines)
1906 const struct got_error *err;
1907 int lineno = 0, nprinted = 0;
1908 char *line;
1909 size_t len;
1910 wchar_t *wline;
1911 int width, wlimit;
1912 struct tog_blame_line *blame_line;
1913 struct got_object_id *prev_id = NULL;
1914 char *id_str;
1916 err = got_object_id_str(&id_str, id);
1917 if (err)
1918 return err;
1920 rewind(f);
1921 werase(view->window);
1923 if (asprintf(&line, "commit: %s", id_str) == -1) {
1924 err = got_error_from_errno();
1925 free(id_str);
1926 return err;
1929 err = format_line(&wline, &width, line, view->ncols);
1930 free(line);
1931 line = NULL;
1932 if (view_needs_focus_indication(view))
1933 wstandout(view->window);
1934 waddwstr(view->window, wline);
1935 if (view_needs_focus_indication(view))
1936 wstandend(view->window);
1937 free(wline);
1938 wline = NULL;
1939 if (width < view->ncols)
1940 waddch(view->window, '\n');
1942 if (asprintf(&line, "[%d/%d] %s%s",
1943 *first_displayed_line - 1 + selected_line, nlines,
1944 blame_complete ? "" : "annotating ", path) == -1) {
1945 free(id_str);
1946 return got_error_from_errno();
1948 free(id_str);
1949 err = format_line(&wline, &width, line, view->ncols);
1950 free(line);
1951 line = NULL;
1952 if (err)
1953 return err;
1954 waddwstr(view->window, wline);
1955 free(wline);
1956 wline = NULL;
1957 if (width < view->ncols)
1958 waddch(view->window, '\n');
1960 *eof = 0;
1961 while (nprinted < max_lines - 2) {
1962 line = parse_next_line(f, &len);
1963 if (line == NULL) {
1964 *eof = 1;
1965 break;
1967 if (++lineno < *first_displayed_line) {
1968 free(line);
1969 continue;
1972 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1973 err = format_line(&wline, &width, line, wlimit);
1974 if (err) {
1975 free(line);
1976 return err;
1979 if (view->focussed && nprinted == selected_line - 1)
1980 wstandout(view->window);
1982 blame_line = &lines[lineno - 1];
1983 if (blame_line->annotated && prev_id &&
1984 got_object_id_cmp(prev_id, blame_line->id) == 0)
1985 waddstr(view->window, " ");
1986 else if (blame_line->annotated) {
1987 char *id_str;
1988 err = got_object_id_str(&id_str, blame_line->id);
1989 if (err) {
1990 free(line);
1991 free(wline);
1992 return err;
1994 wprintw(view->window, "%.8s ", id_str);
1995 free(id_str);
1996 prev_id = blame_line->id;
1997 } else {
1998 waddstr(view->window, "........ ");
1999 prev_id = NULL;
2002 waddwstr(view->window, wline);
2003 while (width < wlimit) {
2004 waddch(view->window, ' ');
2005 width++;
2007 if (view->focussed && nprinted == selected_line - 1)
2008 wstandend(view->window);
2009 if (++nprinted == 1)
2010 *first_displayed_line = lineno;
2011 free(line);
2012 free(wline);
2013 wline = NULL;
2015 *last_displayed_line = lineno;
2017 view_vborder(view);
2019 return NULL;
2022 static const struct got_error *
2023 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2025 const struct got_error *err = NULL;
2026 struct tog_blame_cb_args *a = arg;
2027 struct tog_blame_line *line;
2029 if (nlines != a->nlines ||
2030 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2031 return got_error(GOT_ERR_RANGE);
2033 if (pthread_mutex_lock(a->mutex) != 0)
2034 return got_error_from_errno();
2036 if (*a->quit) { /* user has quit the blame view */
2037 err = got_error(GOT_ERR_ITER_COMPLETED);
2038 goto done;
2041 if (lineno == -1)
2042 goto done; /* no change in this commit */
2044 line = &a->lines[lineno - 1];
2045 if (line->annotated)
2046 goto done;
2048 line->id = got_object_id_dup(id);
2049 if (line->id == NULL) {
2050 err = got_error_from_errno();
2051 goto done;
2053 line->annotated = 1;
2055 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2056 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2057 a->last_displayed_line, a->eof, a->view->nlines);
2058 done:
2059 if (pthread_mutex_unlock(a->mutex) != 0)
2060 return got_error_from_errno();
2061 return err;
2064 static void *
2065 blame_thread(void *arg)
2067 const struct got_error *err;
2068 struct tog_blame_thread_args *ta = arg;
2069 struct tog_blame_cb_args *a = ta->cb_args;
2071 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2072 blame_cb, ta->cb_args);
2074 if (pthread_mutex_lock(a->mutex) != 0)
2075 return (void *)got_error_from_errno();
2077 got_repo_close(ta->repo);
2078 ta->repo = NULL;
2079 *ta->complete = 1;
2080 if (!err)
2081 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2082 a->lines, a->nlines, 1, *a->selected_line,
2083 a->first_displayed_line, a->last_displayed_line, a->eof,
2084 a->view->nlines);
2086 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2087 err = got_error_from_errno();
2089 return (void *)err;
2092 static struct got_object_id *
2093 get_selected_commit_id(struct tog_blame_line *lines,
2094 int first_displayed_line, int selected_line)
2096 struct tog_blame_line *line;
2098 line = &lines[first_displayed_line - 1 + selected_line - 1];
2099 if (!line->annotated)
2100 return NULL;
2102 return line->id;
2105 static const struct got_error *
2106 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2107 struct tog_blame_line *lines, int first_displayed_line,
2108 int selected_line, struct got_repository *repo)
2110 const struct got_error *err = NULL;
2111 struct got_commit_object *commit = NULL;
2112 struct got_object_id *selected_id;
2113 struct got_object_qid *pid;
2115 *pobj = NULL;
2116 *obj = NULL;
2118 selected_id = get_selected_commit_id(lines,
2119 first_displayed_line, selected_line);
2120 if (selected_id == NULL)
2121 return NULL;
2123 err = got_object_open(obj, repo, selected_id);
2124 if (err)
2125 goto done;
2127 err = got_object_commit_open(&commit, repo, *obj);
2128 if (err)
2129 goto done;
2131 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2132 if (pid) {
2133 err = got_object_open(pobj, repo, pid->id);
2134 if (err)
2135 goto done;
2137 done:
2138 if (commit)
2139 got_object_commit_close(commit);
2140 return err;
2143 static const struct got_error *
2144 stop_blame(struct tog_blame *blame)
2146 const struct got_error *err = NULL;
2147 int i;
2149 if (blame->thread) {
2150 if (pthread_join(blame->thread, (void **)&err) != 0)
2151 err = got_error_from_errno();
2152 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2153 err = NULL;
2154 blame->thread = NULL;
2156 if (blame->thread_args.repo) {
2157 got_repo_close(blame->thread_args.repo);
2158 blame->thread_args.repo = NULL;
2160 if (blame->f) {
2161 fclose(blame->f);
2162 blame->f = NULL;
2164 for (i = 0; i < blame->nlines; i++)
2165 free(blame->lines[i].id);
2166 free(blame->lines);
2167 blame->lines = NULL;
2168 free(blame->cb_args.commit_id);
2169 blame->cb_args.commit_id = NULL;
2171 return err;
2174 static const struct got_error *
2175 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2176 struct tog_view *view, int *blame_complete,
2177 int *first_displayed_line, int *last_displayed_line,
2178 int *selected_line, int *done, int *eof, const char *path,
2179 struct got_object_id *commit_id,
2180 struct got_repository *repo)
2182 const struct got_error *err = NULL;
2183 struct got_blob_object *blob = NULL;
2184 struct got_repository *thread_repo = NULL;
2185 struct got_object_id *obj_id = NULL;
2186 struct got_object *obj = NULL;
2188 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2189 if (err)
2190 goto done;
2192 err = got_object_open(&obj, repo, obj_id);
2193 if (err)
2194 goto done;
2196 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2197 err = got_error(GOT_ERR_OBJ_TYPE);
2198 goto done;
2201 err = got_object_blob_open(&blob, repo, obj, 8192);
2202 if (err)
2203 goto done;
2204 blame->f = got_opentemp();
2205 if (blame->f == NULL) {
2206 err = got_error_from_errno();
2207 goto done;
2209 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2210 blame->f, blob);
2211 if (err)
2212 goto done;
2214 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2215 if (blame->lines == NULL) {
2216 err = got_error_from_errno();
2217 goto done;
2220 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2221 if (err)
2222 goto done;
2224 blame->cb_args.view = view;
2225 blame->cb_args.lines = blame->lines;
2226 blame->cb_args.nlines = blame->nlines;
2227 blame->cb_args.mutex = mutex;
2228 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2229 if (blame->cb_args.commit_id == NULL) {
2230 err = got_error_from_errno();
2231 goto done;
2233 blame->cb_args.f = blame->f;
2234 blame->cb_args.path = path;
2235 blame->cb_args.first_displayed_line = first_displayed_line;
2236 blame->cb_args.selected_line = selected_line;
2237 blame->cb_args.last_displayed_line = last_displayed_line;
2238 blame->cb_args.quit = done;
2239 blame->cb_args.eof = eof;
2241 blame->thread_args.path = path;
2242 blame->thread_args.repo = thread_repo;
2243 blame->thread_args.cb_args = &blame->cb_args;
2244 blame->thread_args.complete = blame_complete;
2245 *blame_complete = 0;
2247 if (pthread_create(&blame->thread, NULL, blame_thread,
2248 &blame->thread_args) != 0) {
2249 err = got_error_from_errno();
2250 goto done;
2253 done:
2254 if (blob)
2255 got_object_blob_close(blob);
2256 free(obj_id);
2257 if (obj)
2258 got_object_close(obj);
2259 if (err)
2260 stop_blame(blame);
2261 return err;
2264 static const struct got_error *
2265 open_blame_view(struct tog_view *view, char *path,
2266 struct got_object_id *commit_id, struct got_repository *repo)
2268 const struct got_error *err = NULL;
2269 struct tog_blame_view_state *s = &view->state.blame;
2271 SIMPLEQ_INIT(&s->blamed_commits);
2273 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2274 return got_error_from_errno();
2276 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2277 if (err)
2278 return err;
2280 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2281 s->first_displayed_line = 1;
2282 s->last_displayed_line = view->nlines;
2283 s->selected_line = 1;
2284 s->blame_complete = 0;
2285 s->path = path;
2286 if (s->path == NULL)
2287 return got_error_from_errno();
2288 s->repo = repo;
2289 s->commit_id = commit_id;
2290 memset(&s->blame, 0, sizeof(s->blame));
2292 view->show = show_blame_view;
2293 view->input = input_blame_view;
2294 view->close = close_blame_view;
2296 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2297 &s->first_displayed_line, &s->last_displayed_line,
2298 &s->selected_line, &s->done, &s->eof, s->path,
2299 s->blamed_commit->id, s->repo);
2302 static const struct got_error *
2303 close_blame_view(struct tog_view *view)
2305 const struct got_error *err = NULL;
2306 struct tog_blame_view_state *s = &view->state.blame;
2308 if (s->blame.thread)
2309 err = stop_blame(&s->blame);
2311 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2312 struct got_object_qid *blamed_commit;
2313 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2314 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2315 got_object_qid_free(blamed_commit);
2318 free(s->path);
2320 return err;
2323 static const struct got_error *
2324 show_blame_view(struct tog_view *view)
2326 const struct got_error *err = NULL;
2327 struct tog_blame_view_state *s = &view->state.blame;
2329 if (pthread_mutex_lock(&s->mutex) != 0)
2330 return got_error_from_errno();
2332 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2333 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2334 s->selected_line, &s->first_displayed_line,
2335 &s->last_displayed_line, &s->eof, view->nlines);
2337 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2338 err = got_error_from_errno();
2340 view_vborder(view);
2341 return err;
2344 static const struct got_error *
2345 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2346 struct tog_view **focus_view, struct tog_view *view, int ch)
2348 const struct got_error *err = NULL, *thread_err = NULL;
2349 struct got_object *obj = NULL, *pobj = NULL;
2350 struct tog_view *diff_view;
2351 struct tog_blame_view_state *s = &view->state.blame;
2352 int begin_x = 0;
2354 if (pthread_mutex_lock(&s->mutex) != 0) {
2355 err = got_error_from_errno();
2356 goto done;
2359 switch (ch) {
2360 case 'q':
2361 s->done = 1;
2362 if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 err = got_error_from_errno();
2364 goto done;
2366 return stop_blame(&s->blame);
2367 case 'k':
2368 case KEY_UP:
2369 if (s->selected_line > 1)
2370 s->selected_line--;
2371 else if (s->selected_line == 1 &&
2372 s->first_displayed_line > 1)
2373 s->first_displayed_line--;
2374 break;
2375 case KEY_PPAGE:
2376 if (s->first_displayed_line == 1) {
2377 s->selected_line = 1;
2378 break;
2380 if (s->first_displayed_line > view->nlines - 2)
2381 s->first_displayed_line -=
2382 (view->nlines - 2);
2383 else
2384 s->first_displayed_line = 1;
2385 break;
2386 case 'j':
2387 case KEY_DOWN:
2388 if (s->selected_line < view->nlines - 2 &&
2389 s->first_displayed_line +
2390 s->selected_line <= s->blame.nlines)
2391 s->selected_line++;
2392 else if (s->last_displayed_line <
2393 s->blame.nlines)
2394 s->first_displayed_line++;
2395 break;
2396 case 'b':
2397 case 'p': {
2398 struct got_object_id *id;
2399 id = get_selected_commit_id(s->blame.lines,
2400 s->first_displayed_line, s->selected_line);
2401 if (id == NULL || got_object_id_cmp(id,
2402 s->blamed_commit->id) == 0)
2403 break;
2404 err = open_selected_commit(&pobj, &obj,
2405 s->blame.lines, s->first_displayed_line,
2406 s->selected_line, s->repo);
2407 if (err)
2408 break;
2409 if (pobj == NULL && obj == NULL)
2410 break;
2411 if (ch == 'p' && pobj == NULL)
2412 break;
2413 s->done = 1;
2414 if (pthread_mutex_unlock(&s->mutex) != 0) {
2415 err = got_error_from_errno();
2416 goto done;
2418 thread_err = stop_blame(&s->blame);
2419 s->done = 0;
2420 if (pthread_mutex_lock(&s->mutex) != 0) {
2421 err = got_error_from_errno();
2422 goto done;
2424 if (thread_err)
2425 break;
2426 id = got_object_get_id(ch == 'b' ? obj : pobj);
2427 got_object_close(obj);
2428 obj = NULL;
2429 if (pobj) {
2430 got_object_close(pobj);
2431 pobj = NULL;
2433 err = got_object_qid_alloc(&s->blamed_commit, id);
2434 if (err)
2435 goto done;
2436 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2437 s->blamed_commit, entry);
2438 err = run_blame(&s->blame, &s->mutex, view,
2439 &s->blame_complete,
2440 &s->first_displayed_line,
2441 &s->last_displayed_line,
2442 &s->selected_line, &s->done, &s->eof,
2443 s->path, s->blamed_commit->id, s->repo);
2444 if (err)
2445 break;
2446 break;
2448 case 'B': {
2449 struct got_object_qid *first;
2450 first = SIMPLEQ_FIRST(&s->blamed_commits);
2451 if (!got_object_id_cmp(first->id, s->commit_id))
2452 break;
2453 s->done = 1;
2454 if (pthread_mutex_unlock(&s->mutex) != 0) {
2455 err = got_error_from_errno();
2456 goto done;
2458 thread_err = stop_blame(&s->blame);
2459 s->done = 0;
2460 if (pthread_mutex_lock(&s->mutex) != 0) {
2461 err = got_error_from_errno();
2462 goto done;
2464 if (thread_err)
2465 break;
2466 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2467 got_object_qid_free(s->blamed_commit);
2468 s->blamed_commit =
2469 SIMPLEQ_FIRST(&s->blamed_commits);
2470 err = run_blame(&s->blame, &s->mutex, view,
2471 &s->blame_complete,
2472 &s->first_displayed_line,
2473 &s->last_displayed_line,
2474 &s->selected_line, &s->done, &s->eof, s->path,
2475 s->blamed_commit->id, s->repo);
2476 if (err)
2477 break;
2478 break;
2480 case KEY_ENTER:
2481 case '\r':
2482 err = open_selected_commit(&pobj, &obj,
2483 s->blame.lines, s->first_displayed_line,
2484 s->selected_line, s->repo);
2485 if (err)
2486 break;
2487 if (pobj == NULL && obj == NULL)
2488 break;
2490 if (view_is_parent_view(view))
2491 begin_x = view_split_begin_x(view->begin_x);
2492 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2493 if (diff_view == NULL) {
2494 err = got_error_from_errno();
2495 break;
2497 err = open_diff_view(diff_view, pobj, obj, s->repo);
2498 if (err) {
2499 view_close(diff_view);
2500 break;
2502 if (view_is_parent_view(view)) {
2503 err = view_close_child(view);
2504 if (err)
2505 return err;
2506 err = view_set_child(view, diff_view);
2507 if (err) {
2508 view_close(diff_view);
2509 break;
2511 if (!view_is_splitscreen(diff_view)) {
2512 *focus_view = diff_view;
2513 view->child_focussed = 1;
2515 } else
2516 *new_view = diff_view;
2517 if (pobj) {
2518 got_object_close(pobj);
2519 pobj = NULL;
2521 got_object_close(obj);
2522 obj = NULL;
2523 if (err)
2524 break;
2525 break;
2526 case KEY_NPAGE:
2527 case ' ':
2528 if (s->last_displayed_line >= s->blame.nlines &&
2529 s->selected_line < view->nlines - 2) {
2530 s->selected_line = MIN(s->blame.nlines,
2531 view->nlines - 2);
2532 break;
2534 if (s->last_displayed_line + view->nlines - 2
2535 <= s->blame.nlines)
2536 s->first_displayed_line +=
2537 view->nlines - 2;
2538 else
2539 s->first_displayed_line =
2540 s->blame.nlines -
2541 (view->nlines - 3);
2542 break;
2543 case KEY_RESIZE:
2544 if (s->selected_line > view->nlines - 2) {
2545 s->selected_line = MIN(s->blame.nlines,
2546 view->nlines - 2);
2548 break;
2549 default:
2550 break;
2553 if (pthread_mutex_unlock(&s->mutex) != 0)
2554 err = got_error_from_errno();
2555 done:
2556 if (pobj)
2557 got_object_close(pobj);
2558 return thread_err ? thread_err : err;
2561 static const struct got_error *
2562 cmd_blame(int argc, char *argv[])
2564 const struct got_error *error;
2565 struct got_repository *repo = NULL;
2566 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2567 struct got_object_id *commit_id = NULL;
2568 char *commit_id_str = NULL;
2569 int ch;
2570 struct tog_view *view;
2572 #ifndef PROFILE
2573 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2574 == -1)
2575 err(1, "pledge");
2576 #endif
2578 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2579 switch (ch) {
2580 case 'c':
2581 commit_id_str = optarg;
2582 break;
2583 case 'r':
2584 repo_path = realpath(optarg, NULL);
2585 if (repo_path == NULL)
2586 err(1, "-r option");
2587 break;
2588 default:
2589 usage();
2590 /* NOTREACHED */
2594 argc -= optind;
2595 argv += optind;
2597 if (argc == 1)
2598 path = argv[0];
2599 else
2600 usage_blame();
2602 cwd = getcwd(NULL, 0);
2603 if (cwd == NULL) {
2604 error = got_error_from_errno();
2605 goto done;
2607 if (repo_path == NULL) {
2608 repo_path = strdup(cwd);
2609 if (repo_path == NULL) {
2610 error = got_error_from_errno();
2611 goto done;
2616 error = got_repo_open(&repo, repo_path);
2617 if (error != NULL)
2618 return error;
2620 error = got_repo_map_path(&in_repo_path, repo, path);
2621 if (error != NULL)
2622 goto done;
2624 if (commit_id_str == NULL) {
2625 struct got_reference *head_ref;
2626 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2627 if (error != NULL)
2628 goto done;
2629 error = got_ref_resolve(&commit_id, repo, head_ref);
2630 got_ref_close(head_ref);
2631 } else {
2632 struct got_object *obj;
2633 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2634 if (error != NULL)
2635 goto done;
2636 commit_id = got_object_id_dup(got_object_get_id(obj));
2637 if (commit_id == NULL)
2638 error = got_error_from_errno();
2639 got_object_close(obj);
2641 if (error != NULL)
2642 goto done;
2644 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2645 if (view == NULL) {
2646 error = got_error_from_errno();
2647 goto done;
2649 error = open_blame_view(view, in_repo_path, commit_id, repo);
2650 if (error)
2651 goto done;
2652 error = view_loop(view);
2653 done:
2654 free(repo_path);
2655 free(cwd);
2656 free(commit_id);
2657 if (repo)
2658 got_repo_close(repo);
2659 return error;
2662 static const struct got_error *
2663 draw_tree_entries(struct tog_view *view,
2664 struct got_tree_entry **first_displayed_entry,
2665 struct got_tree_entry **last_displayed_entry,
2666 struct got_tree_entry **selected_entry, int *ndisplayed,
2667 const char *label, int show_ids, const char *parent_path,
2668 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2670 const struct got_error *err = NULL;
2671 struct got_tree_entry *te;
2672 wchar_t *wline;
2673 int width, n;
2675 *ndisplayed = 0;
2677 werase(view->window);
2679 if (limit == 0)
2680 return NULL;
2682 err = format_line(&wline, &width, label, view->ncols);
2683 if (err)
2684 return err;
2685 if (view_needs_focus_indication(view))
2686 wstandout(view->window);
2687 waddwstr(view->window, wline);
2688 if (view_needs_focus_indication(view))
2689 wstandend(view->window);
2690 free(wline);
2691 wline = NULL;
2692 if (width < view->ncols)
2693 waddch(view->window, '\n');
2694 if (--limit <= 0)
2695 return NULL;
2696 err = format_line(&wline, &width, parent_path, view->ncols);
2697 if (err)
2698 return err;
2699 waddwstr(view->window, wline);
2700 free(wline);
2701 wline = NULL;
2702 if (width < view->ncols)
2703 waddch(view->window, '\n');
2704 if (--limit <= 0)
2705 return NULL;
2706 waddch(view->window, '\n');
2707 if (--limit <= 0)
2708 return NULL;
2710 te = SIMPLEQ_FIRST(&entries->head);
2711 if (*first_displayed_entry == NULL) {
2712 if (selected == 0) {
2713 if (view->focussed)
2714 wstandout(view->window);
2715 *selected_entry = NULL;
2717 waddstr(view->window, " ..\n"); /* parent directory */
2718 if (selected == 0 && view->focussed)
2719 wstandend(view->window);
2720 (*ndisplayed)++;
2721 if (--limit <= 0)
2722 return NULL;
2723 n = 1;
2724 } else {
2725 n = 0;
2726 while (te != *first_displayed_entry)
2727 te = SIMPLEQ_NEXT(te, entry);
2730 while (te) {
2731 char *line = NULL, *id_str = NULL;
2733 if (show_ids) {
2734 err = got_object_id_str(&id_str, te->id);
2735 if (err)
2736 return got_error_from_errno();
2738 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2739 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2740 free(id_str);
2741 return got_error_from_errno();
2743 free(id_str);
2744 err = format_line(&wline, &width, line, view->ncols);
2745 if (err) {
2746 free(line);
2747 break;
2749 if (n == selected) {
2750 if (view->focussed)
2751 wstandout(view->window);
2752 *selected_entry = te;
2754 waddwstr(view->window, wline);
2755 if (width < view->ncols)
2756 waddch(view->window, '\n');
2757 if (n == selected && view->focussed)
2758 wstandend(view->window);
2759 free(line);
2760 free(wline);
2761 wline = NULL;
2762 n++;
2763 (*ndisplayed)++;
2764 *last_displayed_entry = te;
2765 if (--limit <= 0)
2766 break;
2767 te = SIMPLEQ_NEXT(te, entry);
2770 return err;
2773 static void
2774 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2775 const struct got_tree_entries *entries, int isroot)
2777 struct got_tree_entry *te, *prev;
2778 int i;
2780 if (*first_displayed_entry == NULL)
2781 return;
2783 te = SIMPLEQ_FIRST(&entries->head);
2784 if (*first_displayed_entry == te) {
2785 if (!isroot)
2786 *first_displayed_entry = NULL;
2787 return;
2790 /* XXX this is stupid... switch to TAILQ? */
2791 for (i = 0; i < maxscroll; i++) {
2792 while (te != *first_displayed_entry) {
2793 prev = te;
2794 te = SIMPLEQ_NEXT(te, entry);
2796 *first_displayed_entry = prev;
2797 te = SIMPLEQ_FIRST(&entries->head);
2799 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2800 *first_displayed_entry = NULL;
2803 static void
2804 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2805 struct got_tree_entry *last_displayed_entry,
2806 const struct got_tree_entries *entries)
2808 struct got_tree_entry *next;
2809 int n = 0;
2811 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2812 return;
2814 if (*first_displayed_entry)
2815 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2816 else
2817 next = SIMPLEQ_FIRST(&entries->head);
2818 while (next) {
2819 *first_displayed_entry = next;
2820 if (++n >= maxscroll)
2821 break;
2822 next = SIMPLEQ_NEXT(next, entry);
2826 static const struct got_error *
2827 tree_entry_path(char **path, struct tog_parent_trees *parents,
2828 struct got_tree_entry *te)
2830 const struct got_error *err = NULL;
2831 struct tog_parent_tree *pt;
2832 size_t len = 2; /* for leading slash and NUL */
2834 TAILQ_FOREACH(pt, parents, entry)
2835 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2836 if (te)
2837 len += strlen(te->name);
2839 *path = calloc(1, len);
2840 if (path == NULL)
2841 return got_error_from_errno();
2843 (*path)[0] = '/';
2844 pt = TAILQ_LAST(parents, tog_parent_trees);
2845 while (pt) {
2846 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2847 err = got_error(GOT_ERR_NO_SPACE);
2848 goto done;
2850 if (strlcat(*path, "/", len) >= len) {
2851 err = got_error(GOT_ERR_NO_SPACE);
2852 goto done;
2854 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2856 if (te) {
2857 if (strlcat(*path, te->name, len) >= len) {
2858 err = got_error(GOT_ERR_NO_SPACE);
2859 goto done;
2862 done:
2863 if (err) {
2864 free(*path);
2865 *path = NULL;
2867 return err;
2870 static const struct got_error *
2871 blame_tree_entry(struct tog_view **new_view, int begin_x,
2872 struct got_tree_entry *te, struct tog_parent_trees *parents,
2873 struct got_object_id *commit_id, struct got_repository *repo)
2875 const struct got_error *err = NULL;
2876 char *path;
2877 struct tog_view *blame_view;
2879 err = tree_entry_path(&path, parents, te);
2880 if (err)
2881 return err;
2883 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2884 if (blame_view == NULL)
2885 return got_error_from_errno();
2887 err = open_blame_view(blame_view, path, commit_id, repo);
2888 if (err) {
2889 view_close(blame_view);
2890 free(path);
2891 } else
2892 *new_view = blame_view;
2893 return err;
2896 static const struct got_error *
2897 log_tree_entry(struct tog_view **new_view, int begin_x,
2898 struct got_tree_entry *te, struct tog_parent_trees *parents,
2899 struct got_object_id *commit_id, struct got_repository *repo)
2901 struct tog_view *log_view;
2902 const struct got_error *err = NULL;
2903 char *path;
2905 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2906 if (log_view == NULL)
2907 return got_error_from_errno();
2909 err = tree_entry_path(&path, parents, te);
2910 if (err)
2911 return err;
2913 err = open_log_view(log_view, commit_id, repo, path);
2914 if (err)
2915 view_close(log_view);
2916 else
2917 *new_view = log_view;
2918 free(path);
2919 return err;
2922 static const struct got_error *
2923 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2924 struct got_object_id *commit_id, struct got_repository *repo)
2926 const struct got_error *err = NULL;
2927 char *commit_id_str = NULL;
2928 struct tog_tree_view_state *s = &view->state.tree;
2930 TAILQ_INIT(&s->parents);
2932 err = got_object_id_str(&commit_id_str, commit_id);
2933 if (err != NULL)
2934 goto done;
2936 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2937 err = got_error_from_errno();
2938 goto done;
2941 s->root = s->tree = root;
2942 s->entries = got_object_tree_get_entries(root);
2943 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2944 s->commit_id = got_object_id_dup(commit_id);
2945 if (s->commit_id == NULL) {
2946 err = got_error_from_errno();
2947 goto done;
2949 s->repo = repo;
2951 view->show = show_tree_view;
2952 view->input = input_tree_view;
2953 view->close = close_tree_view;
2954 done:
2955 free(commit_id_str);
2956 if (err) {
2957 free(s->tree_label);
2958 s->tree_label = NULL;
2960 return err;
2963 static const struct got_error *
2964 close_tree_view(struct tog_view *view)
2966 struct tog_tree_view_state *s = &view->state.tree;
2968 free(s->tree_label);
2969 s->tree_label = NULL;
2970 free(s->commit_id);
2971 s->commit_id = NULL;
2972 while (!TAILQ_EMPTY(&s->parents)) {
2973 struct tog_parent_tree *parent;
2974 parent = TAILQ_FIRST(&s->parents);
2975 TAILQ_REMOVE(&s->parents, parent, entry);
2976 free(parent);
2979 if (s->tree != s->root)
2980 got_object_tree_close(s->tree);
2981 got_object_tree_close(s->root);
2983 return NULL;
2986 static const struct got_error *
2987 show_tree_view(struct tog_view *view)
2989 const struct got_error *err = NULL;
2990 struct tog_tree_view_state *s = &view->state.tree;
2991 char *parent_path;
2993 err = tree_entry_path(&parent_path, &s->parents, NULL);
2994 if (err)
2995 return err;
2997 err = draw_tree_entries(view, &s->first_displayed_entry,
2998 &s->last_displayed_entry, &s->selected_entry,
2999 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3000 s->entries, s->selected, view->nlines, s->tree == s->root);
3001 free(parent_path);
3003 view_vborder(view);
3004 return err;
3007 static const struct got_error *
3008 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3009 struct tog_view **focus_view, struct tog_view *view, int ch)
3011 const struct got_error *err = NULL;
3012 struct tog_tree_view_state *s = &view->state.tree;
3013 struct tog_view *log_view;
3014 int begin_x = 0;
3016 switch (ch) {
3017 case 'i':
3018 s->show_ids = !s->show_ids;
3019 break;
3020 case 'l':
3021 if (!s->selected_entry)
3022 break;
3023 if (view_is_parent_view(view))
3024 begin_x = view_split_begin_x(view->begin_x);
3025 err = log_tree_entry(&log_view, begin_x,
3026 s->selected_entry, &s->parents,
3027 s->commit_id, s->repo);
3028 if (view_is_parent_view(view)) {
3029 err = view_close_child(view);
3030 if (err)
3031 return err;
3032 err = view_set_child(view, log_view);
3033 if (err) {
3034 view_close(log_view);
3035 break;
3037 *focus_view = log_view;
3038 view->child_focussed = 1;
3039 } else
3040 *new_view = log_view;
3041 break;
3042 case 'k':
3043 case KEY_UP:
3044 if (s->selected > 0)
3045 s->selected--;
3046 if (s->selected > 0)
3047 break;
3048 tree_scroll_up(&s->first_displayed_entry, 1,
3049 s->entries, s->tree == s->root);
3050 break;
3051 case KEY_PPAGE:
3052 if (SIMPLEQ_FIRST(&s->entries->head) ==
3053 s->first_displayed_entry) {
3054 if (s->tree != s->root)
3055 s->first_displayed_entry = NULL;
3056 s->selected = 0;
3057 break;
3059 tree_scroll_up(&s->first_displayed_entry,
3060 view->nlines, s->entries,
3061 s->tree == s->root);
3062 break;
3063 case 'j':
3064 case KEY_DOWN:
3065 if (s->selected < s->ndisplayed - 1) {
3066 s->selected++;
3067 break;
3069 tree_scroll_down(&s->first_displayed_entry, 1,
3070 s->last_displayed_entry, s->entries);
3071 break;
3072 case KEY_NPAGE:
3073 tree_scroll_down(&s->first_displayed_entry,
3074 view->nlines, s->last_displayed_entry,
3075 s->entries);
3076 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3077 entry))
3078 break;
3079 /* can't scroll any further; move cursor down */
3080 if (s->selected < s->ndisplayed - 1)
3081 s->selected = s->ndisplayed - 1;
3082 break;
3083 case KEY_ENTER:
3084 case '\r':
3085 if (s->selected_entry == NULL) {
3086 struct tog_parent_tree *parent;
3087 case KEY_BACKSPACE:
3088 /* user selected '..' */
3089 if (s->tree == s->root)
3090 break;
3091 parent = TAILQ_FIRST(&s->parents);
3092 TAILQ_REMOVE(&s->parents, parent,
3093 entry);
3094 got_object_tree_close(s->tree);
3095 s->tree = parent->tree;
3096 s->entries =
3097 got_object_tree_get_entries(s->tree);
3098 s->first_displayed_entry =
3099 parent->first_displayed_entry;
3100 s->selected_entry =
3101 parent->selected_entry;
3102 s->selected = parent->selected;
3103 free(parent);
3104 } else if (S_ISDIR(s->selected_entry->mode)) {
3105 struct tog_parent_tree *parent;
3106 struct got_tree_object *child;
3107 err = got_object_open_as_tree(&child,
3108 s->repo, s->selected_entry->id);
3109 if (err)
3110 break;
3111 parent = calloc(1, sizeof(*parent));
3112 if (parent == NULL) {
3113 err = got_error_from_errno();
3114 break;
3116 parent->tree = s->tree;
3117 parent->first_displayed_entry =
3118 s->first_displayed_entry;
3119 parent->selected_entry = s->selected_entry;
3120 parent->selected = s->selected;
3121 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3122 s->tree = child;
3123 s->entries =
3124 got_object_tree_get_entries(s->tree);
3125 s->selected = 0;
3126 s->first_displayed_entry = NULL;
3127 } else if (S_ISREG(s->selected_entry->mode)) {
3128 struct tog_view *blame_view;
3129 int begin_x = view_is_parent_view(view) ?
3130 view_split_begin_x(view->begin_x) : 0;
3132 err = blame_tree_entry(&blame_view, begin_x,
3133 s->selected_entry, &s->parents, s->commit_id,
3134 s->repo);
3135 if (err)
3136 break;
3137 if (view_is_parent_view(view)) {
3138 err = view_close_child(view);
3139 if (err)
3140 return err;
3141 err = view_set_child(view, blame_view);
3142 if (err) {
3143 view_close(blame_view);
3144 break;
3146 *focus_view = blame_view;
3147 view->child_focussed = 1;
3148 } else
3149 *new_view = blame_view;
3151 break;
3152 case KEY_RESIZE:
3153 if (s->selected > view->nlines)
3154 s->selected = s->ndisplayed - 1;
3155 break;
3156 default:
3157 break;
3160 return err;
3163 __dead static void
3164 usage_tree(void)
3166 endwin();
3167 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3168 getprogname());
3169 exit(1);
3172 static const struct got_error *
3173 cmd_tree(int argc, char *argv[])
3175 const struct got_error *error;
3176 struct got_repository *repo = NULL;
3177 char *repo_path = NULL;
3178 struct got_object_id *commit_id = NULL;
3179 char *commit_id_arg = NULL;
3180 struct got_commit_object *commit = NULL;
3181 struct got_tree_object *tree = NULL;
3182 int ch;
3183 struct tog_view *view;
3185 #ifndef PROFILE
3186 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3187 == -1)
3188 err(1, "pledge");
3189 #endif
3191 while ((ch = getopt(argc, argv, "c:")) != -1) {
3192 switch (ch) {
3193 case 'c':
3194 commit_id_arg = optarg;
3195 break;
3196 default:
3197 usage();
3198 /* NOTREACHED */
3202 argc -= optind;
3203 argv += optind;
3205 if (argc == 0) {
3206 repo_path = getcwd(NULL, 0);
3207 if (repo_path == NULL)
3208 return got_error_from_errno();
3209 } else if (argc == 1) {
3210 repo_path = realpath(argv[0], NULL);
3211 if (repo_path == NULL)
3212 return got_error_from_errno();
3213 } else
3214 usage_log();
3216 error = got_repo_open(&repo, repo_path);
3217 free(repo_path);
3218 if (error != NULL)
3219 return error;
3221 if (commit_id_arg == NULL) {
3222 error = get_head_commit_id(&commit_id, repo);
3223 if (error != NULL)
3224 goto done;
3225 } else {
3226 struct got_object *obj;
3227 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3228 if (error == NULL) {
3229 commit_id = got_object_id_dup(got_object_get_id(obj));
3230 if (commit_id == NULL)
3231 error = got_error_from_errno();
3234 if (error != NULL)
3235 goto done;
3237 error = got_object_open_as_commit(&commit, repo, commit_id);
3238 if (error != NULL)
3239 goto done;
3241 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3242 if (error != NULL)
3243 goto done;
3245 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3246 if (view == NULL) {
3247 error = got_error_from_errno();
3248 goto done;
3250 error = open_tree_view(view, tree, commit_id, repo);
3251 if (error)
3252 goto done;
3253 error = view_loop(view);
3254 done:
3255 free(commit_id);
3256 if (commit)
3257 got_object_commit_close(commit);
3258 if (tree)
3259 got_object_tree_close(tree);
3260 if (repo)
3261 got_repo_close(repo);
3262 return error;
3264 static void
3265 init_curses(void)
3267 initscr();
3268 cbreak();
3269 noecho();
3270 nonl();
3271 intrflush(stdscr, FALSE);
3272 keypad(stdscr, TRUE);
3273 curs_set(0);
3276 __dead static void
3277 usage(void)
3279 int i;
3281 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3282 "Available commands:\n", getprogname());
3283 for (i = 0; i < nitems(tog_commands); i++) {
3284 struct tog_cmd *cmd = &tog_commands[i];
3285 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3287 exit(1);
3290 static char **
3291 make_argv(const char *arg0, const char *arg1)
3293 char **argv;
3294 int argc = (arg1 == NULL ? 1 : 2);
3296 argv = calloc(argc, sizeof(char *));
3297 if (argv == NULL)
3298 err(1, "calloc");
3299 argv[0] = strdup(arg0);
3300 if (argv[0] == NULL)
3301 err(1, "calloc");
3302 if (arg1) {
3303 argv[1] = strdup(arg1);
3304 if (argv[1] == NULL)
3305 err(1, "calloc");
3308 return argv;
3311 int
3312 main(int argc, char *argv[])
3314 const struct got_error *error = NULL;
3315 struct tog_cmd *cmd = NULL;
3316 int ch, hflag = 0;
3317 char **cmd_argv = NULL;
3319 setlocale(LC_ALL, "");
3321 while ((ch = getopt(argc, argv, "h")) != -1) {
3322 switch (ch) {
3323 case 'h':
3324 hflag = 1;
3325 break;
3326 default:
3327 usage();
3328 /* NOTREACHED */
3332 argc -= optind;
3333 argv += optind;
3334 optind = 0;
3335 optreset = 1;
3337 if (argc == 0) {
3338 if (hflag)
3339 usage();
3340 /* Build an argument vector which runs a default command. */
3341 cmd = &tog_commands[0];
3342 cmd_argv = make_argv(cmd->name, NULL);
3343 argc = 1;
3344 } else {
3345 int i;
3347 /* Did the user specific a command? */
3348 for (i = 0; i < nitems(tog_commands); i++) {
3349 if (strncmp(tog_commands[i].name, argv[0],
3350 strlen(argv[0])) == 0) {
3351 cmd = &tog_commands[i];
3352 if (hflag)
3353 tog_commands[i].cmd_usage();
3354 break;
3357 if (cmd == NULL) {
3358 /* Did the user specify a repository? */
3359 char *repo_path = realpath(argv[0], NULL);
3360 if (repo_path) {
3361 struct got_repository *repo;
3362 error = got_repo_open(&repo, repo_path);
3363 if (error == NULL)
3364 got_repo_close(repo);
3365 } else
3366 error = got_error_from_errno();
3367 if (error) {
3368 if (hflag) {
3369 fprintf(stderr, "%s: '%s' is not a "
3370 "known command\n", getprogname(),
3371 argv[0]);
3372 usage();
3374 fprintf(stderr, "%s: '%s' is neither a known "
3375 "command nor a path to a repository\n",
3376 getprogname(), argv[0]);
3377 free(repo_path);
3378 return 1;
3380 cmd = &tog_commands[0];
3381 cmd_argv = make_argv(cmd->name, repo_path);
3382 argc = 2;
3383 free(repo_path);
3387 init_curses();
3389 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3390 if (error)
3391 goto done;
3392 done:
3393 endwin();
3394 free(cmd_argv);
3395 if (error)
3396 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
3397 return 0;