2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 #include <sys/queue.h>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
47 #include "got_blame.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
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" },
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
96 int first_displayed_line;
97 int last_displayed_line;
100 struct got_repository *repo;
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
111 struct commit_queue_head head;
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;
122 struct got_repository *repo;
123 struct got_object_id *start_id;
126 struct tog_blame_cb_args {
127 pthread_mutex_t *mutex;
128 struct tog_blame_line *lines; /* one per line */
131 struct tog_view *view;
132 struct got_object_id *commit_id;
135 int *first_displayed_line;
136 int *last_displayed_line;
142 struct tog_blame_thread_args {
144 struct got_repository *repo;
145 struct tog_blame_cb_args *cb_args;
152 struct tog_blame_line *lines;
155 struct tog_blame_thread_args thread_args;
156 struct tog_blame_cb_args cb_args;
160 struct tog_blame_view_state {
161 int first_displayed_line;
162 int last_displayed_line;
167 pthread_mutex_t mutex;
168 struct got_object_id_queue blamed_commits;
169 struct got_object_qid *blamed_commit;
171 struct got_repository *repo;
172 struct got_object_id *commit_id;
173 struct tog_blame blame;
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;
184 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
186 struct tog_tree_view_state {
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;
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.
217 TAILQ_HEAD(tog_view_list_head, tog_view);
220 TAILQ_ENTRY(tog_view) entry;
223 int nlines, ncols, begin_y, begin_x;
224 int lines, cols; /* copies of LINES and COLS */
226 struct tog_view *parent;
227 struct tog_view *child;
230 /* type-specific state */
231 enum tog_view_type type;
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;
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 *);
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;
279 view_close(view->child);
283 err = view->close(view);
285 del_panel(view->panel);
287 delwin(view->window);
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));
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) {
313 view->panel = new_panel(view->window);
314 if (view->panel == NULL ||
315 set_panel_userptr(view->panel, view) != OK) {
320 keypad(view->window, TRUE);
325 view_split_begin_x(int begin_x)
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;
340 view->begin_x = view_split_begin_x(0);
341 view->nlines = LINES;
342 view->ncols = COLS - view->begin_x;
345 err = view_resize(view);
349 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
350 return got_error_from_errno();
355 static const struct got_error *
356 view_fullscreen(struct tog_view *view)
358 const struct got_error *err = NULL;
362 view->nlines = LINES;
366 err = view_resize(view);
370 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
371 return got_error_from_errno();
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)
387 if (view->lines > LINES)
388 nlines = view->nlines - (view->lines - LINES);
390 nlines = view->nlines + (LINES - view->lines);
392 if (view->cols > COLS)
393 ncols = view->ncols - (view->cols - COLS);
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;
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);
413 show_panel(view->panel);
415 view_splitscreen(view->child);
416 show_panel(view->child->panel);
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)
431 err = view_close(view->child);
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;
442 child->parent = view;
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;
465 nodelay(stdscr, FALSE);
466 ch = wgetch(view->window);
467 nodelay(stdscr, TRUE);
473 *focus = view->child;
474 view->child_focussed = 1;
475 } else if (view->parent) {
476 *focus = view->parent;
477 view->parent->child_focussed = 0;
481 err = view->input(new, dead, focus, view, ch);
488 if (view_is_parent_view(view)) {
489 if (view->child == NULL)
491 if (view_is_splitscreen(view->child)) {
492 *focus = view->child;
493 view->child_focussed = 1;
494 err = view_fullscreen(view->child);
496 err = view_splitscreen(view->child);
499 err = view->child->input(new, dead, focus,
500 view->child, KEY_RESIZE);
502 if (view_is_splitscreen(view)) {
504 view->parent->child_focussed = 1;
505 err = view_fullscreen(view);
507 err = view_splitscreen(view);
511 err = view->input(new, dead, focus, view,
516 TAILQ_FOREACH(v, views, entry) {
517 err = view_resize(v);
520 err = v->input(new, dead, focus, v, ch);
524 err = view->input(new, dead, focus, view, ch);
532 view_vborder(struct tog_view *view)
535 struct tog_view *view_above;
538 return view_vborder(view->parent);
540 panel = panel_above(view->panel);
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);
550 view_needs_focus_indication(struct tog_view *view)
552 if (view_is_parent_view(view)) {
553 if (view->child == NULL || view->child_focussed)
555 if (!view_is_splitscreen(view->child))
557 } else if (!view_is_splitscreen(view))
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;
572 TAILQ_INSERT_HEAD(&views, view, entry);
576 err = view->show(view);
581 while (!TAILQ_EMPTY(&views) && !done) {
582 err = view_input(&new_view, &dead_view, &focus_view, &done,
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);
595 if (dead_view->parent)
596 dead_view->parent->child = NULL;
598 TAILQ_REMOVE(&views, dead_view, entry);
600 err = view_close(dead_view);
601 if (err || dead_view == main_view)
604 if (view == dead_view) {
609 else if (!TAILQ_EMPTY(&views))
610 view = TAILQ_LAST(&views,
615 if (view->child && view->child_focussed)
616 focus_view = view->child;
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)
628 TAILQ_REMOVE(&views, v, entry);
636 TAILQ_INSERT_TAIL(&views, new_view, entry);
637 if (focus_view == NULL)
638 focus_view = new_view;
641 show_panel(focus_view->panel);
644 focus_view->focussed = 1;
647 show_panel(new_view->panel);
648 if (view->child && view_is_splitscreen(view->child))
649 show_panel(view->child->panel);
653 err = view->parent->show(view->parent);
657 err = view->show(view);
661 err = view->child->show(view->child);
670 while (!TAILQ_EMPTY(&views)) {
671 view = TAILQ_FIRST(&views);
672 TAILQ_REMOVE(&views, view, entry);
683 "usage: %s log [-c commit] [-r repository-path] [path]\n",
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)
693 const struct got_error *err = NULL;
696 *wlen = mbstowcs(NULL, s, 0);
697 if (*wlen == (size_t)-1) {
700 return got_error_from_errno();
702 /* byte string invalid in current encoding; try to "fix" it */
703 err = got_mbsavis(&vis, &vislen, s);
706 *wlen = mbstowcs(NULL, vis, 0);
707 if (*wlen == (size_t)-1) {
708 err = got_error_from_errno(); /* give up */
713 *ws = calloc(*wlen + 1, sizeof(*ws));
715 err = got_error_from_errno();
719 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
720 err = got_error_from_errno();
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;
737 wchar_t *wline = NULL;
744 err = mbs2ws(&wline, &wlen, line);
749 while (i < wlen && cols < wlimit) {
750 int width = wcwidth(wline[i]);
757 if (cols + width <= wlimit)
762 if (wline[i] == L'\t')
763 cols += TABSIZE - ((cols + 1) % TABSIZE);
767 err = got_error_from_errno();
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;
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);
806 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
807 waddnstr(view->window, datebuf, limit);
812 author0 = strdup(commit->author);
813 if (author0 == NULL) {
814 err = got_error_from_errno();
818 smallerthan = strchr(author, '<');
822 char *at = strchr(author, '@');
827 err = format_line(&wauthor, &author_width, author, limit);
830 waddwstr(view->window, wauthor);
832 while (col <= avail && author_width < author_display_cols + 1) {
833 waddch(view->window, ' ');
840 logmsg0 = strdup(commit->logmsg);
841 if (logmsg0 == NULL) {
842 err = got_error_from_errno();
846 while (*logmsg == '\n')
848 newline = strchr(logmsg, '\n');
852 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
855 waddwstr(view->window, wlogmsg);
857 while (col <= avail) {
858 waddch(view->window, ' ');
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));
881 entry->commit = commit;
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);
894 /* Don't free entry->id! It is owned by the commit graph. */
899 free_commits(struct commit_queue *commits)
901 while (!TAILQ_EMPTY(&commits->head))
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;
914 err = got_commit_graph_iter_start(graph, start_id, repo);
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);
926 if (err->code == GOT_ERR_ITER_COMPLETED) {
930 if (err->code != GOT_ERR_ITER_NEED_MORE)
932 err = got_commit_graph_fetch_commits(graph,
942 err = got_object_open_as_commit(&commit, repo, id);
945 entry = alloc_commit_queue_entry(commit, id);
947 err = got_error_from_errno();
951 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
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,
965 const struct got_error *err = NULL;
969 err = queue_commits(graph, commits, NULL, 1, repo, path);
973 /* Next entry to display should now be available. */
974 *pentry = TAILQ_NEXT(entry, entry);
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;
986 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
990 err = got_ref_resolve(head_id, repo, head_ref);
991 got_ref_close(head_ref);
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,
1007 const struct got_error *err = NULL;
1008 struct commit_queue_entry *entry;
1009 int ncommits, width;
1010 char *id_str, *header;
1016 if (ncommits == selected_idx) {
1020 entry = TAILQ_NEXT(entry, entry);
1024 err = got_object_id_str(&id_str, (*selected)->id);
1028 if (path && strcmp(path, "/") != 0) {
1029 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
1030 err = got_error_from_errno();
1034 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1035 err = got_error_from_errno();
1040 err = format_line(&wline, &width, header, view->ncols);
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');
1064 if (ncommits >= limit - 1)
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);
1075 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1076 err = queue_commits(graph, commits, NULL, 1,
1079 if (err->code != GOT_ERR_ITER_COMPLETED)
1084 entry = TAILQ_NEXT(entry, entry);
1093 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1094 struct commit_queue *commits)
1096 struct commit_queue_entry *entry;
1099 entry = TAILQ_FIRST(&commits->head);
1100 if (*first_displayed_entry == entry)
1103 entry = *first_displayed_entry;
1104 while (entry && nscrolled < maxscroll) {
1105 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1107 *first_displayed_entry = entry;
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;
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)
1131 *last_displayed_entry = pentry;
1133 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1136 *first_displayed_entry = pentry;
1137 } while (++nscrolled < maxscroll);
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);
1156 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1158 err = got_object_open(&obj1, repo, parent_id->id);
1163 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1164 if (diff_view == NULL) {
1165 err = got_error_from_errno();
1169 err = open_diff_view(diff_view, obj1, obj2, repo);
1171 *new_view = diff_view;
1174 got_object_close(obj1);
1176 got_object_close(obj2);
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);
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);
1198 got_object_tree_close(tree);
1200 *new_view = tree_view;
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);
1215 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
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);
1232 if (err->code != GOT_ERR_ITER_COMPLETED)
1237 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1238 s->selected_entry = s->first_displayed_entry;
1240 s->start_id = got_object_id_dup(start_id);
1241 if (s->start_id == NULL) {
1242 err = got_error_from_errno();
1246 view->show = show_log_view;
1247 view->input = input_log_view;
1248 view->close = close_log_view;
1253 static const struct got_error *
1254 close_log_view(struct tog_view *view)
1256 struct tog_log_view_state *s = &view->state.log;
1259 got_commit_graph_close(s->graph);
1260 free_commits(&s->commits);
1261 free(s->in_repo_path);
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);
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;
1287 struct tog_view *diff_view = NULL, *tree_view = NULL;
1293 if (s->selected > 0)
1295 if (s->selected > 0)
1297 scroll_up(&s->first_displayed_entry, 1,
1301 if (TAILQ_FIRST(&s->commits.head) ==
1302 s->first_displayed_entry) {
1306 scroll_up(&s->first_displayed_entry,
1307 view->nlines, &s->commits);
1311 if (s->selected < MIN(view->nlines - 2,
1312 s->commits.ncommits - 1)) {
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);
1320 if (err->code != GOT_ERR_ITER_COMPLETED)
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,
1332 if (err && err->code != GOT_ERR_ITER_COMPLETED)
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);
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;
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,
1359 if (view_is_parent_view(view)) {
1360 err = view_close_child(view);
1363 err = view_set_child(view, diff_view);
1365 view_close(diff_view);
1368 if (!view_is_splitscreen(diff_view)) {
1369 *focus_view = diff_view;
1370 view->child_focussed = 1;
1373 *new_view = diff_view;
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);
1384 err = view_set_child(view, tree_view);
1386 view_close(tree_view);
1389 *focus_view = tree_view;
1390 view->child_focussed = 1;
1392 *new_view = tree_view;
1395 if (strcmp(s->in_repo_path, "/") == 0)
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);
1403 return got_error_from_errno();
1404 err = open_log_view(lv, s->start_id, s->repo,
1408 if (view_is_parent_view(view))
1411 view_set_child(view->parent, lv);
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;
1432 struct tog_view *view;
1435 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1440 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1443 start_commit = optarg;
1446 repo_path = realpath(optarg, NULL);
1447 if (repo_path == NULL)
1448 err(1, "-r option");
1462 path = strdup(argv[0]);
1466 return got_error_from_errno();
1468 cwd = getcwd(NULL, 0);
1470 error = got_error_from_errno();
1473 if (repo_path == NULL) {
1474 repo_path = strdup(cwd);
1475 if (repo_path == NULL) {
1476 error = got_error_from_errno();
1481 error = got_repo_open(&repo, repo_path);
1485 if (start_commit == NULL) {
1486 error = get_head_commit_id(&start_id, repo);
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();
1502 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1504 error = got_error_from_errno();
1507 error = open_log_view(view, start_id, repo, path);
1510 error = view_loop(view);
1517 got_repo_close(repo);
1525 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1531 parse_next_line(FILE *f, size_t *len)
1536 const char delim[3] = { '\0', '\0', '\0'};
1538 line = fparseln(f, &linelen, &lineno, delim, 0);
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,
1549 const struct got_error *err;
1550 int nlines = 0, nprinted = 0;
1557 werase(view->window);
1560 err = format_line(&wline, &width, header, view->ncols);
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');
1579 while (nprinted < max_lines) {
1580 line = parse_next_line(f, &len);
1585 if (++nlines < *first_displayed_line) {
1590 err = format_line(&wline, &width, line, view->ncols);
1595 waddwstr(view->window, wline);
1596 if (width < view->ncols)
1597 waddch(view->window, '\n');
1598 if (++nprinted == 1)
1599 *first_displayed_line = nlines;
1604 *last_displayed_line = nlines;
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;
1619 err = got_object_open(&obj1, s->repo, s->id1);
1624 err = got_object_open(&obj2, s->repo, s->id2);
1630 err = got_error_from_errno();
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);
1642 case GOT_OBJ_TYPE_TREE:
1643 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1644 s->diff_context, s->repo, f);
1646 case GOT_OBJ_TYPE_COMMIT:
1647 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1651 err = got_error(GOT_ERR_OBJ_TYPE);
1656 got_object_close(obj1);
1657 got_object_close(obj2);
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);
1674 struct got_object_id *id1;
1675 id1 = got_object_id_dup(got_object_get_id(obj1));
1677 return got_error_from_errno();
1678 view->state.diff.id1 = id1;
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);
1696 free(view->state.diff.id1);
1697 view->state.diff.id1 = NULL;
1698 free(view->state.diff.id2);
1699 view->state.diff.id2 = NULL;
1703 view->show = show_diff_view;
1704 view->input = input_diff_view;
1705 view->close = close_diff_view;
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();
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;
1732 err = got_object_id_str(&id_str1, s->id1);
1736 err = got_object_id_str(&id_str2, s->id2);
1740 if (asprintf(&header, "diff: %s %s",
1741 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1742 err = got_error_from_errno();
1750 return draw_file(view, s->f, &s->first_displayed_line,
1751 &s->last_displayed_line, &s->eof, view->nlines,
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;
1766 if (s->first_displayed_line > 1)
1767 s->first_displayed_line--;
1771 while (i++ < view->nlines - 1 &&
1772 s->first_displayed_line > 1)
1773 s->first_displayed_line--;
1778 s->first_displayed_line++;
1783 while (!s->eof && i++ < view->nlines - 1) {
1785 line = parse_next_line(s->f, NULL);
1786 s->first_displayed_line++;
1792 if (s->diff_context > 0) {
1794 err = create_diff(s);
1798 if (s->diff_context < INT_MAX) {
1800 err = create_diff(s);
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;
1819 struct tog_view *view;
1822 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1827 while ((ch = getopt(argc, argv, "")) != -1) {
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];
1855 error = got_repo_open(&repo, repo_path);
1860 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1864 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1868 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1870 error = got_error_from_errno();
1873 error = open_diff_view(view, obj1, obj2, repo);
1876 error = view_loop(view);
1878 got_repo_close(repo);
1880 got_object_close(obj1);
1882 got_object_close(obj2);
1890 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1895 struct tog_blame_line {
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;
1912 struct tog_blame_line *blame_line;
1913 struct got_object_id *prev_id = NULL;
1916 err = got_object_id_str(&id_str, id);
1921 werase(view->window);
1923 if (asprintf(&line, "commit: %s", id_str) == -1) {
1924 err = got_error_from_errno();
1929 err = format_line(&wline, &width, line, view->ncols);
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);
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) {
1946 return got_error_from_errno();
1949 err = format_line(&wline, &width, line, view->ncols);
1954 waddwstr(view->window, wline);
1957 if (width < view->ncols)
1958 waddch(view->window, '\n');
1961 while (nprinted < max_lines - 2) {
1962 line = parse_next_line(f, &len);
1967 if (++lineno < *first_displayed_line) {
1972 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1973 err = format_line(&wline, &width, line, wlimit);
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) {
1988 err = got_object_id_str(&id_str, blame_line->id);
1994 wprintw(view->window, "%.8s ", id_str);
1996 prev_id = blame_line->id;
1998 waddstr(view->window, "........ ");
2002 waddwstr(view->window, wline);
2003 while (width < wlimit) {
2004 waddch(view->window, ' ');
2007 if (view->focussed && nprinted == selected_line - 1)
2008 wstandend(view->window);
2009 if (++nprinted == 1)
2010 *first_displayed_line = lineno;
2015 *last_displayed_line = lineno;
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);
2042 goto done; /* no change in this commit */
2044 line = &a->lines[lineno - 1];
2045 if (line->annotated)
2048 line->id = got_object_id_dup(id);
2049 if (line->id == NULL) {
2050 err = got_error_from_errno();
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);
2059 if (pthread_mutex_unlock(a->mutex) != 0)
2060 return got_error_from_errno();
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);
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,
2086 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2087 err = got_error_from_errno();
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)
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;
2118 selected_id = get_selected_commit_id(lines,
2119 first_displayed_line, selected_line);
2120 if (selected_id == NULL)
2123 err = got_object_open(obj, repo, selected_id);
2127 err = got_object_commit_open(&commit, repo, *obj);
2131 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2133 err = got_object_open(pobj, repo, pid->id);
2139 got_object_commit_close(commit);
2143 static const struct got_error *
2144 stop_blame(struct tog_blame *blame)
2146 const struct got_error *err = NULL;
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)
2154 blame->thread = NULL;
2156 if (blame->thread_args.repo) {
2157 got_repo_close(blame->thread_args.repo);
2158 blame->thread_args.repo = NULL;
2164 for (i = 0; i < blame->nlines; i++)
2165 free(blame->lines[i].id);
2167 blame->lines = NULL;
2168 free(blame->cb_args.commit_id);
2169 blame->cb_args.commit_id = NULL;
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);
2192 err = got_object_open(&obj, repo, obj_id);
2196 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2197 err = got_error(GOT_ERR_OBJ_TYPE);
2201 err = got_object_blob_open(&blob, repo, obj, 8192);
2204 blame->f = got_opentemp();
2205 if (blame->f == NULL) {
2206 err = got_error_from_errno();
2209 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2214 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2215 if (blame->lines == NULL) {
2216 err = got_error_from_errno();
2220 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
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();
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();
2255 got_object_blob_close(blob);
2258 got_object_close(obj);
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);
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;
2286 if (s->path == NULL)
2287 return got_error_from_errno();
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);
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();
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;
2354 if (pthread_mutex_lock(&s->mutex) != 0) {
2355 err = got_error_from_errno();
2362 if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 err = got_error_from_errno();
2366 return stop_blame(&s->blame);
2369 if (s->selected_line > 1)
2371 else if (s->selected_line == 1 &&
2372 s->first_displayed_line > 1)
2373 s->first_displayed_line--;
2376 if (s->first_displayed_line == 1) {
2377 s->selected_line = 1;
2380 if (s->first_displayed_line > view->nlines - 2)
2381 s->first_displayed_line -=
2384 s->first_displayed_line = 1;
2388 if (s->selected_line < view->nlines - 2 &&
2389 s->first_displayed_line +
2390 s->selected_line <= s->blame.nlines)
2392 else if (s->last_displayed_line <
2394 s->first_displayed_line++;
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)
2404 err = open_selected_commit(&pobj, &obj,
2405 s->blame.lines, s->first_displayed_line,
2406 s->selected_line, s->repo);
2409 if (pobj == NULL && obj == NULL)
2411 if (ch == 'p' && pobj == NULL)
2414 if (pthread_mutex_unlock(&s->mutex) != 0) {
2415 err = got_error_from_errno();
2418 thread_err = stop_blame(&s->blame);
2420 if (pthread_mutex_lock(&s->mutex) != 0) {
2421 err = got_error_from_errno();
2426 id = got_object_get_id(ch == 'b' ? obj : pobj);
2427 got_object_close(obj);
2430 got_object_close(pobj);
2433 err = got_object_qid_alloc(&s->blamed_commit, id);
2436 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2437 s->blamed_commit, entry);
2438 err = run_blame(&s->blame, &s->mutex, view,
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);
2449 struct got_object_qid *first;
2450 first = SIMPLEQ_FIRST(&s->blamed_commits);
2451 if (!got_object_id_cmp(first->id, s->commit_id))
2454 if (pthread_mutex_unlock(&s->mutex) != 0) {
2455 err = got_error_from_errno();
2458 thread_err = stop_blame(&s->blame);
2460 if (pthread_mutex_lock(&s->mutex) != 0) {
2461 err = got_error_from_errno();
2466 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2467 got_object_qid_free(s->blamed_commit);
2469 SIMPLEQ_FIRST(&s->blamed_commits);
2470 err = run_blame(&s->blame, &s->mutex, view,
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);
2482 err = open_selected_commit(&pobj, &obj,
2483 s->blame.lines, s->first_displayed_line,
2484 s->selected_line, s->repo);
2487 if (pobj == NULL && obj == NULL)
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();
2497 err = open_diff_view(diff_view, pobj, obj, s->repo);
2499 view_close(diff_view);
2502 if (view_is_parent_view(view)) {
2503 err = view_close_child(view);
2506 err = view_set_child(view, diff_view);
2508 view_close(diff_view);
2511 if (!view_is_splitscreen(diff_view)) {
2512 *focus_view = diff_view;
2513 view->child_focussed = 1;
2516 *new_view = diff_view;
2518 got_object_close(pobj);
2521 got_object_close(obj);
2528 if (s->last_displayed_line >= s->blame.nlines &&
2529 s->selected_line < view->nlines - 2) {
2530 s->selected_line = MIN(s->blame.nlines,
2534 if (s->last_displayed_line + view->nlines - 2
2536 s->first_displayed_line +=
2539 s->first_displayed_line =
2544 if (s->selected_line > view->nlines - 2) {
2545 s->selected_line = MIN(s->blame.nlines,
2553 if (pthread_mutex_unlock(&s->mutex) != 0)
2554 err = got_error_from_errno();
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;
2570 struct tog_view *view;
2573 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2578 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2581 commit_id_str = optarg;
2584 repo_path = realpath(optarg, NULL);
2585 if (repo_path == NULL)
2586 err(1, "-r option");
2602 cwd = getcwd(NULL, 0);
2604 error = got_error_from_errno();
2607 if (repo_path == NULL) {
2608 repo_path = strdup(cwd);
2609 if (repo_path == NULL) {
2610 error = got_error_from_errno();
2616 error = got_repo_open(&repo, repo_path);
2620 error = got_repo_map_path(&in_repo_path, repo, path);
2624 if (commit_id_str == NULL) {
2625 struct got_reference *head_ref;
2626 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2629 error = got_ref_resolve(&commit_id, repo, head_ref);
2630 got_ref_close(head_ref);
2632 struct got_object *obj;
2633 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
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);
2644 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2646 error = got_error_from_errno();
2649 error = open_blame_view(view, in_repo_path, commit_id, repo);
2652 error = view_loop(view);
2658 got_repo_close(repo);
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;
2677 werase(view->window);
2682 err = format_line(&wline, &width, label, view->ncols);
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);
2692 if (width < view->ncols)
2693 waddch(view->window, '\n');
2696 err = format_line(&wline, &width, parent_path, view->ncols);
2699 waddwstr(view->window, wline);
2702 if (width < view->ncols)
2703 waddch(view->window, '\n');
2706 waddch(view->window, '\n');
2710 te = SIMPLEQ_FIRST(&entries->head);
2711 if (*first_displayed_entry == NULL) {
2712 if (selected == 0) {
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);
2726 while (te != *first_displayed_entry)
2727 te = SIMPLEQ_NEXT(te, entry);
2731 char *line = NULL, *id_str = NULL;
2734 err = got_object_id_str(&id_str, te->id);
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) {
2741 return got_error_from_errno();
2744 err = format_line(&wline, &width, line, view->ncols);
2749 if (n == selected) {
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);
2764 *last_displayed_entry = te;
2767 te = SIMPLEQ_NEXT(te, entry);
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;
2780 if (*first_displayed_entry == NULL)
2783 te = SIMPLEQ_FIRST(&entries->head);
2784 if (*first_displayed_entry == te) {
2786 *first_displayed_entry = NULL;
2790 /* XXX this is stupid... switch to TAILQ? */
2791 for (i = 0; i < maxscroll; i++) {
2792 while (te != *first_displayed_entry) {
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;
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;
2811 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2814 if (*first_displayed_entry)
2815 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2817 next = SIMPLEQ_FIRST(&entries->head);
2819 *first_displayed_entry = next;
2820 if (++n >= maxscroll)
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 */;
2837 len += strlen(te->name);
2839 *path = calloc(1, len);
2841 return got_error_from_errno();
2844 pt = TAILQ_LAST(parents, tog_parent_trees);
2846 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2847 err = got_error(GOT_ERR_NO_SPACE);
2850 if (strlcat(*path, "/", len) >= len) {
2851 err = got_error(GOT_ERR_NO_SPACE);
2854 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2857 if (strlcat(*path, te->name, len) >= len) {
2858 err = got_error(GOT_ERR_NO_SPACE);
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;
2877 struct tog_view *blame_view;
2879 err = tree_entry_path(&path, parents, te);
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);
2889 view_close(blame_view);
2892 *new_view = blame_view;
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;
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);
2913 err = open_log_view(log_view, commit_id, repo, path);
2915 view_close(log_view);
2917 *new_view = log_view;
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);
2936 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2937 err = got_error_from_errno();
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();
2951 view->show = show_tree_view;
2952 view->input = input_tree_view;
2953 view->close = close_tree_view;
2955 free(commit_id_str);
2957 free(s->tree_label);
2958 s->tree_label = NULL;
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;
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);
2979 if (s->tree != s->root)
2980 got_object_tree_close(s->tree);
2981 got_object_tree_close(s->root);
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;
2993 err = tree_entry_path(&parent_path, &s->parents, NULL);
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);
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;
3018 s->show_ids = !s->show_ids;
3021 if (!s->selected_entry)
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);
3032 err = view_set_child(view, log_view);
3034 view_close(log_view);
3037 *focus_view = log_view;
3038 view->child_focussed = 1;
3040 *new_view = log_view;
3044 if (s->selected > 0)
3046 if (s->selected > 0)
3048 tree_scroll_up(&s->first_displayed_entry, 1,
3049 s->entries, s->tree == s->root);
3052 if (SIMPLEQ_FIRST(&s->entries->head) ==
3053 s->first_displayed_entry) {
3054 if (s->tree != s->root)
3055 s->first_displayed_entry = NULL;
3059 tree_scroll_up(&s->first_displayed_entry,
3060 view->nlines, s->entries,
3061 s->tree == s->root);
3065 if (s->selected < s->ndisplayed - 1) {
3069 tree_scroll_down(&s->first_displayed_entry, 1,
3070 s->last_displayed_entry, s->entries);
3073 tree_scroll_down(&s->first_displayed_entry,
3074 view->nlines, s->last_displayed_entry,
3076 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3079 /* can't scroll any further; move cursor down */
3080 if (s->selected < s->ndisplayed - 1)
3081 s->selected = s->ndisplayed - 1;
3085 if (s->selected_entry == NULL) {
3086 struct tog_parent_tree *parent;
3088 /* user selected '..' */
3089 if (s->tree == s->root)
3091 parent = TAILQ_FIRST(&s->parents);
3092 TAILQ_REMOVE(&s->parents, parent,
3094 got_object_tree_close(s->tree);
3095 s->tree = parent->tree;
3097 got_object_tree_get_entries(s->tree);
3098 s->first_displayed_entry =
3099 parent->first_displayed_entry;
3101 parent->selected_entry;
3102 s->selected = parent->selected;
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);
3111 parent = calloc(1, sizeof(*parent));
3112 if (parent == NULL) {
3113 err = got_error_from_errno();
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);
3124 got_object_tree_get_entries(s->tree);
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,
3137 if (view_is_parent_view(view)) {
3138 err = view_close_child(view);
3141 err = view_set_child(view, blame_view);
3143 view_close(blame_view);
3146 *focus_view = blame_view;
3147 view->child_focussed = 1;
3149 *new_view = blame_view;
3153 if (s->selected > view->nlines)
3154 s->selected = s->ndisplayed - 1;
3167 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
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;
3183 struct tog_view *view;
3186 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3191 while ((ch = getopt(argc, argv, "c:")) != -1) {
3194 commit_id_arg = optarg;
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();
3216 error = got_repo_open(&repo, repo_path);
3221 if (commit_id_arg == NULL) {
3222 error = get_head_commit_id(&commit_id, repo);
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();
3237 error = got_object_open_as_commit(&commit, repo, commit_id);
3241 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3245 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3247 error = got_error_from_errno();
3250 error = open_tree_view(view, tree, commit_id, repo);
3253 error = view_loop(view);
3257 got_object_commit_close(commit);
3259 got_object_tree_close(tree);
3261 got_repo_close(repo);
3271 intrflush(stdscr, FALSE);
3272 keypad(stdscr, TRUE);
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);
3291 make_argv(const char *arg0, const char *arg1)
3294 int argc = (arg1 == NULL ? 1 : 2);
3296 argv = calloc(argc, sizeof(char *));
3299 argv[0] = strdup(arg0);
3300 if (argv[0] == NULL)
3303 argv[1] = strdup(arg1);
3304 if (argv[1] == NULL)
3312 main(int argc, char *argv[])
3314 const struct got_error *error = NULL;
3315 struct tog_cmd *cmd = NULL;
3317 char **cmd_argv = NULL;
3319 setlocale(LC_ALL, "");
3321 while ((ch = getopt(argc, argv, "h")) != -1) {
3340 /* Build an argument vector which runs a default command. */
3341 cmd = &tog_commands[0];
3342 cmd_argv = make_argv(cmd->name, NULL);
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];
3353 tog_commands[i].cmd_usage();
3358 /* Did the user specify a repository? */
3359 char *repo_path = realpath(argv[0], NULL);
3361 struct got_repository *repo;
3362 error = got_repo_open(&repo, repo_path);
3364 got_repo_close(repo);
3366 error = got_error_from_errno();
3369 fprintf(stderr, "%s: '%s' is not a "
3370 "known command\n", getprogname(),
3374 fprintf(stderr, "%s: '%s' is neither a known "
3375 "command nor a path to a repository\n",
3376 getprogname(), argv[0]);
3380 cmd = &tog_commands[0];
3381 cmd_argv = make_argv(cmd->name, repo_path);
3389 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3396 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);