2 * Copyright (c) 2018, 2019 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>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "got_worktree.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #define CTRL(x) ((x) & 0x1f)
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
75 __dead static void usage(void);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log,
88 "show repository history" },
89 { "diff", cmd_diff, usage_diff,
90 "compare files and directories" },
91 { "blame", cmd_blame, usage_blame,
92 "show line-by-line file history" },
93 { "tree", cmd_tree, usage_tree,
94 "browse trees in repository" },
104 #define TOG_EOF_STRING "(END)"
106 struct commit_queue_entry {
107 TAILQ_ENTRY(commit_queue_entry) entry;
108 struct got_object_id *id;
109 struct got_commit_object *commit;
112 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
113 struct commit_queue {
115 struct commit_queue_head head;
118 struct tog_diff_view_state {
119 struct got_object_id *id1, *id2;
121 int first_displayed_line;
122 int last_displayed_line;
125 struct got_repository *repo;
126 struct got_reflist_head *refs;
128 /* passed from log view; may be NULL */
129 struct tog_view *log_view;
132 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
134 struct tog_log_thread_args {
135 pthread_cond_t need_commits;
137 struct got_commit_graph *graph;
138 struct commit_queue *commits;
139 const char *in_repo_path;
140 struct got_object_id *start_id;
141 struct got_repository *repo;
144 struct tog_view *view;
145 struct commit_queue_entry **first_displayed_entry;
146 struct commit_queue_entry **selected_entry;
149 struct tog_log_view_state {
150 struct commit_queue commits;
151 struct commit_queue_entry *first_displayed_entry;
152 struct commit_queue_entry *last_displayed_entry;
153 struct commit_queue_entry *selected_entry;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
161 struct tog_log_thread_args thread_args;
162 struct commit_queue_entry *matched_entry;
165 struct tog_blame_cb_args {
166 struct tog_blame_line *lines; /* one per line */
169 struct tog_view *view;
170 struct got_object_id *commit_id;
174 struct tog_blame_thread_args {
176 struct got_repository *repo;
177 struct tog_blame_cb_args *cb_args;
184 struct tog_blame_line *lines;
187 struct tog_blame_thread_args thread_args;
188 struct tog_blame_cb_args cb_args;
192 struct tog_blame_view_state {
193 int first_displayed_line;
194 int last_displayed_line;
199 struct got_object_id_queue blamed_commits;
200 struct got_object_qid *blamed_commit;
202 struct got_repository *repo;
203 struct got_reflist_head *refs;
204 struct got_object_id *commit_id;
205 struct tog_blame blame;
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
234 * We implement two types of views: parent views and child views.
236 * The 'Tab' key switches between a parent view and its child view.
237 * Child views are shown side-by-side to their parent view, provided
238 * there is enough screen estate.
240 * When a new view is opened from within a parent view, this new view
241 * becomes a child view of the parent view, replacing any existing child.
243 * When a new view is opened from within a child view, this new view
244 * becomes a parent view which will obscure the views below until the
245 * user quits the new parent view by typing 'q'.
247 * This list of views contains parent views only.
248 * Child views are only pointed to by their parent view.
250 TAILQ_HEAD(tog_view_list_head, tog_view);
253 TAILQ_ENTRY(tog_view) entry;
256 int nlines, ncols, begin_y, begin_x;
257 int lines, cols; /* copies of LINES and COLS */
259 struct tog_view *parent;
260 struct tog_view *child;
263 /* type-specific state */
264 enum tog_view_type type;
266 struct tog_diff_view_state diff;
267 struct tog_log_view_state log;
268 struct tog_blame_view_state blame;
269 struct tog_tree_view_state tree;
272 const struct got_error *(*show)(struct tog_view *);
273 const struct got_error *(*input)(struct tog_view **,
274 struct tog_view **, struct tog_view**, struct tog_view *, int);
275 const struct got_error *(*close)(struct tog_view *);
277 const struct got_error *(*search_start)(struct tog_view *);
278 const struct got_error *(*search_next)(struct tog_view *);
280 #define TOG_SEARCH_FORWARD 1
281 #define TOG_SEARCH_BACKWARD 2
282 int search_next_done;
286 static const struct got_error *open_diff_view(struct tog_view *,
287 struct got_object_id *, struct got_object_id *, struct tog_view *,
288 struct got_reflist_head *, struct got_repository *);
289 static const struct got_error *show_diff_view(struct tog_view *);
290 static const struct got_error *input_diff_view(struct tog_view **,
291 struct tog_view **, struct tog_view **, struct tog_view *, int);
292 static const struct got_error* close_diff_view(struct tog_view *);
294 static const struct got_error *open_log_view(struct tog_view *,
295 struct got_object_id *, struct got_reflist_head *,
296 struct got_repository *, const char *, int);
297 static const struct got_error * show_log_view(struct tog_view *);
298 static const struct got_error *input_log_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_log_view(struct tog_view *);
301 static const struct got_error *search_start_log_view(struct tog_view *);
302 static const struct got_error *search_next_log_view(struct tog_view *);
304 static const struct got_error *open_blame_view(struct tog_view *, char *,
305 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 static const struct got_error *show_blame_view(struct tog_view *);
307 static const struct got_error *input_blame_view(struct tog_view **,
308 struct tog_view **, struct tog_view **, struct tog_view *, int);
309 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *open_tree_view(struct tog_view *,
312 struct got_tree_object *, struct got_object_id *,
313 struct got_reflist_head *, struct got_repository *);
314 static const struct got_error *show_tree_view(struct tog_view *);
315 static const struct got_error *input_tree_view(struct tog_view **,
316 struct tog_view **, struct tog_view **, struct tog_view *, int);
317 static const struct got_error *close_tree_view(struct tog_view *);
319 static volatile sig_atomic_t tog_sigwinch_received;
322 tog_sigwinch(int signo)
324 tog_sigwinch_received = 1;
327 static const struct got_error *
328 view_close(struct tog_view *view)
330 const struct got_error *err = NULL;
333 view_close(view->child);
337 err = view->close(view);
339 del_panel(view->panel);
341 delwin(view->window);
346 static struct tog_view *
347 view_open(int nlines, int ncols, int begin_y, int begin_x,
348 enum tog_view_type type)
350 struct tog_view *view = calloc(1, sizeof(*view));
358 view->nlines = nlines ? nlines : LINES - begin_y;
359 view->ncols = ncols ? ncols : COLS - begin_x;
360 view->begin_y = begin_y;
361 view->begin_x = begin_x;
362 view->window = newwin(nlines, ncols, begin_y, begin_x);
363 if (view->window == NULL) {
367 view->panel = new_panel(view->window);
368 if (view->panel == NULL ||
369 set_panel_userptr(view->panel, view) != OK) {
374 keypad(view->window, TRUE);
379 view_split_begin_x(int begin_x)
381 if (begin_x > 0 || COLS < 120)
383 return (COLS - MAX(COLS / 2, 80));
386 static const struct got_error *view_resize(struct tog_view *);
388 static const struct got_error *
389 view_splitscreen(struct tog_view *view)
391 const struct got_error *err = NULL;
394 view->begin_x = view_split_begin_x(0);
395 view->nlines = LINES;
396 view->ncols = COLS - view->begin_x;
399 err = view_resize(view);
403 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
404 return got_error_from_errno("mvwin");
409 static const struct got_error *
410 view_fullscreen(struct tog_view *view)
412 const struct got_error *err = NULL;
416 view->nlines = LINES;
420 err = view_resize(view);
424 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
425 return got_error_from_errno("mvwin");
431 view_is_parent_view(struct tog_view *view)
433 return view->parent == NULL;
436 static const struct got_error *
437 view_resize(struct tog_view *view)
441 if (view->lines > LINES)
442 nlines = view->nlines - (view->lines - LINES);
444 nlines = view->nlines + (LINES - view->lines);
446 if (view->cols > COLS)
447 ncols = view->ncols - (view->cols - COLS);
449 ncols = view->ncols + (COLS - view->cols);
451 if (wresize(view->window, nlines, ncols) == ERR)
452 return got_error_from_errno("wresize");
453 if (replace_panel(view->panel, view->window) == ERR)
454 return got_error_from_errno("replace_panel");
455 wclear(view->window);
457 view->nlines = nlines;
463 view->child->begin_x = view_split_begin_x(view->begin_x);
464 if (view->child->begin_x == 0) {
465 view_fullscreen(view->child);
466 if (view->child->focussed)
467 show_panel(view->child->panel);
469 show_panel(view->panel);
471 view_splitscreen(view->child);
472 show_panel(view->child->panel);
479 static const struct got_error *
480 view_close_child(struct tog_view *view)
482 const struct got_error *err = NULL;
484 if (view->child == NULL)
487 err = view_close(view->child);
492 static const struct got_error *
493 view_set_child(struct tog_view *view, struct tog_view *child)
495 const struct got_error *err = NULL;
498 child->parent = view;
503 view_is_splitscreen(struct tog_view *view)
505 return view->begin_x > 0;
514 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
515 cols = 80; /* Default */
521 resize_term(lines, cols);
524 static const struct got_error *
525 view_search_start(struct tog_view *view)
530 if (view->nlines < 1)
533 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
535 wclrtoeol(view->window);
539 ret = wgetnstr(view->window, pattern, sizeof(pattern));
545 if (view->searching) {
546 regfree(&view->regex);
550 if (regcomp(&view->regex, pattern,
551 REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 0) {
552 view->search_start(view);
553 view->searching = TOG_SEARCH_FORWARD;
554 view->search_next_done = 0;
555 view->search_next(view);
561 static const struct got_error *
562 view_input(struct tog_view **new, struct tog_view **dead,
563 struct tog_view **focus, int *done, struct tog_view *view,
564 struct tog_view_list_head *views)
566 const struct got_error *err = NULL;
574 if (view->searching && !view->search_next_done) {
575 errcode = pthread_mutex_unlock(&tog_mutex);
577 return got_error_set_errno(errcode,
578 "pthread_mutex_unlock");
580 errcode = pthread_mutex_lock(&tog_mutex);
582 return got_error_set_errno(errcode,
583 "pthread_mutex_lock");
584 view->search_next(view);
588 nodelay(stdscr, FALSE);
589 /* Allow threads to make progress while we are waiting for input. */
590 errcode = pthread_mutex_unlock(&tog_mutex);
592 return got_error_set_errno(errcode, "pthread_mutex_unlock");
593 ch = wgetch(view->window);
594 errcode = pthread_mutex_lock(&tog_mutex);
596 return got_error_set_errno(errcode, "pthread_mutex_lock");
597 nodelay(stdscr, TRUE);
599 if (tog_sigwinch_received) {
601 tog_sigwinch_received = 0;
602 TAILQ_FOREACH(v, views, entry) {
603 err = view_resize(v);
606 err = v->input(new, dead, focus, v, KEY_RESIZE);
617 *focus = view->child;
618 view->child_focussed = 1;
619 } else if (view->parent) {
620 *focus = view->parent;
621 view->parent->child_focussed = 0;
625 err = view->input(new, dead, focus, view, ch);
632 if (view_is_parent_view(view)) {
633 if (view->child == NULL)
635 if (view_is_splitscreen(view->child)) {
636 *focus = view->child;
637 view->child_focussed = 1;
638 err = view_fullscreen(view->child);
640 err = view_splitscreen(view->child);
643 err = view->child->input(new, dead, focus,
644 view->child, KEY_RESIZE);
646 if (view_is_splitscreen(view)) {
648 view->parent->child_focussed = 1;
649 err = view_fullscreen(view);
651 err = view_splitscreen(view);
655 err = view->input(new, dead, focus, view,
662 if (view->search_start)
663 view_search_start(view);
665 err = view->input(new, dead, focus, view, ch);
669 if (view->search_next && view->searching) {
670 view->searching = (ch == 'n' ?
671 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
672 view->search_next_done = 0;
673 view->search_next(view);
675 err = view->input(new, dead, focus, view, ch);
678 err = view->input(new, dead, focus, view, ch);
686 view_vborder(struct tog_view *view)
689 struct tog_view *view_above;
692 return view_vborder(view->parent);
694 panel = panel_above(view->panel);
698 view_above = panel_userptr(panel);
699 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
700 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
704 view_needs_focus_indication(struct tog_view *view)
706 if (view_is_parent_view(view)) {
707 if (view->child == NULL || view->child_focussed)
709 if (!view_is_splitscreen(view->child))
711 } else if (!view_is_splitscreen(view))
714 return view->focussed;
717 static const struct got_error *
718 view_loop(struct tog_view *view)
720 const struct got_error *err = NULL;
721 struct tog_view_list_head views;
722 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
723 int fast_refresh = 10;
724 int done = 0, errcode;
726 errcode = pthread_mutex_lock(&tog_mutex);
728 return got_error_set_errno(errcode, "pthread_mutex_lock");
731 TAILQ_INSERT_HEAD(&views, view, entry);
735 err = view->show(view);
740 while (!TAILQ_EMPTY(&views) && !done) {
741 /* Refresh fast during initialization, then become slower. */
742 if (fast_refresh && fast_refresh-- == 0)
743 halfdelay(10); /* switch to once per second */
745 err = view_input(&new_view, &dead_view, &focus_view, &done,
750 struct tog_view *prev = NULL;
752 if (view_is_parent_view(dead_view))
753 prev = TAILQ_PREV(dead_view,
754 tog_view_list_head, entry);
755 else if (view->parent != dead_view)
758 if (dead_view->parent)
759 dead_view->parent->child = NULL;
761 TAILQ_REMOVE(&views, dead_view, entry);
763 err = view_close(dead_view);
764 if (err || dead_view == main_view)
767 if (view == dead_view) {
772 else if (!TAILQ_EMPTY(&views))
773 view = TAILQ_LAST(&views,
778 if (view->child && view->child_focussed)
779 focus_view = view->child;
786 struct tog_view *v, *t;
787 /* Only allow one parent view per type. */
788 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
789 if (v->type != new_view->type)
791 TAILQ_REMOVE(&views, v, entry);
797 TAILQ_INSERT_TAIL(&views, new_view, entry);
799 if (focus_view == NULL)
800 focus_view = new_view;
803 show_panel(focus_view->panel);
806 focus_view->focussed = 1;
809 show_panel(new_view->panel);
810 if (view->child && view_is_splitscreen(view->child))
811 show_panel(view->child->panel);
814 if (focus_view == NULL) {
816 show_panel(view->panel);
817 if (view->child && view_is_splitscreen(view->child))
818 show_panel(view->child->panel);
822 err = view->parent->show(view->parent);
826 err = view->show(view);
830 err = view->child->show(view->child);
839 while (!TAILQ_EMPTY(&views)) {
840 view = TAILQ_FIRST(&views);
841 TAILQ_REMOVE(&views, view, entry);
845 errcode = pthread_mutex_unlock(&tog_mutex);
847 return got_error_set_errno(errcode, "pthread_mutex_unlock");
857 "usage: %s log [-c commit] [-r repository-path] [path]\n",
862 /* Create newly allocated wide-character string equivalent to a byte string. */
863 static const struct got_error *
864 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
867 const struct got_error *err = NULL;
870 *wlen = mbstowcs(NULL, s, 0);
871 if (*wlen == (size_t)-1) {
874 return got_error_from_errno("mbstowcs");
876 /* byte string invalid in current encoding; try to "fix" it */
877 err = got_mbsavis(&vis, &vislen, s);
880 *wlen = mbstowcs(NULL, vis, 0);
881 if (*wlen == (size_t)-1) {
882 err = got_error_from_errno("mbstowcs"); /* give up */
887 *ws = calloc(*wlen + 1, sizeof(*ws));
889 err = got_error_from_errno("calloc");
893 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
894 err = got_error_from_errno("mbstowcs");
905 /* Format a line for display, ensuring that it won't overflow a width limit. */
906 static const struct got_error *
907 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
909 const struct got_error *err = NULL;
911 wchar_t *wline = NULL;
918 err = mbs2ws(&wline, &wlen, line);
923 while (i < wlen && cols < wlimit) {
924 int width = wcwidth(wline[i]);
931 if (cols + width <= wlimit)
936 if (wline[i] == L'\t')
937 cols += TABSIZE - ((cols + 1) % TABSIZE);
941 err = got_error_from_errno("wcwidth");
956 static const struct got_error*
957 build_refs_str(char **refs_str, struct got_reflist_head *refs,
958 struct got_object_id *id)
960 static const struct got_error *err = NULL;
961 struct got_reflist_entry *re;
967 SIMPLEQ_FOREACH(re, refs, entry) {
968 if (got_object_id_cmp(re->id, id) != 0)
970 name = got_ref_get_name(re->ref);
971 if (strcmp(name, GOT_REF_HEAD) == 0)
973 if (strncmp(name, "refs/", 5) == 0)
975 if (strncmp(name, "got/", 4) == 0)
977 if (strncmp(name, "heads/", 6) == 0)
979 if (strncmp(name, "remotes/", 8) == 0)
982 if (asprintf(refs_str, "%s%s%s", s ? s : "",
983 s ? ", " : "", name) == -1) {
984 err = got_error_from_errno("asprintf");
995 static const struct got_error *
996 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
998 char *smallerthan, *at;
1000 smallerthan = strchr(author, '<');
1001 if (smallerthan && smallerthan[1] != '\0')
1002 author = smallerthan + 1;
1003 at = strchr(author, '@');
1006 return format_line(wauthor, author_width, author, limit);
1009 static const struct got_error *
1010 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1011 struct got_object_id *id, struct got_reflist_head *refs,
1012 int author_display_cols)
1014 const struct got_error *err = NULL;
1015 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1016 char *logmsg0 = NULL, *logmsg = NULL;
1017 char *author = NULL;
1018 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1019 int author_width, logmsg_width;
1020 char *newline, *line = NULL;
1022 static const size_t date_display_cols = 9;
1023 const int avail = view->ncols;
1025 time_t committer_time;
1027 committer_time = got_object_commit_get_committer_time(commit);
1028 if (localtime_r(&committer_time, &tm) == NULL)
1029 return got_error_from_errno("localtime_r");
1030 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1032 return got_error(GOT_ERR_NO_SPACE);
1034 if (avail < date_display_cols)
1035 limit = MIN(sizeof(datebuf) - 1, avail);
1037 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1038 waddnstr(view->window, datebuf, limit);
1043 author = strdup(got_object_commit_get_author(commit));
1044 if (author == NULL) {
1045 err = got_error_from_errno("strdup");
1048 err = format_author(&wauthor, &author_width, author, avail - col);
1051 waddwstr(view->window, wauthor);
1052 col += author_width;
1053 while (col <= avail && author_width < author_display_cols + 2) {
1054 waddch(view->window, ' ');
1061 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1062 if (logmsg0 == NULL) {
1063 err = got_error_from_errno("strdup");
1067 while (*logmsg == '\n')
1069 newline = strchr(logmsg, '\n');
1072 limit = avail - col;
1073 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1076 waddwstr(view->window, wlogmsg);
1077 col += logmsg_width;
1078 while (col <= avail) {
1079 waddch(view->window, ' ');
1091 static struct commit_queue_entry *
1092 alloc_commit_queue_entry(struct got_commit_object *commit,
1093 struct got_object_id *id)
1095 struct commit_queue_entry *entry;
1097 entry = calloc(1, sizeof(*entry));
1102 entry->commit = commit;
1107 pop_commit(struct commit_queue *commits)
1109 struct commit_queue_entry *entry;
1111 entry = TAILQ_FIRST(&commits->head);
1112 TAILQ_REMOVE(&commits->head, entry, entry);
1113 got_object_commit_close(entry->commit);
1114 commits->ncommits--;
1115 /* Don't free entry->id! It is owned by the commit graph. */
1120 free_commits(struct commit_queue *commits)
1122 while (!TAILQ_EMPTY(&commits->head))
1123 pop_commit(commits);
1126 static const struct got_error *
1127 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1128 int minqueue, struct got_repository *repo, const char *path)
1130 const struct got_error *err = NULL;
1134 * We keep all commits open throughout the lifetime of the log
1135 * view in order to avoid having to re-fetch commits from disk
1136 * while updating the display.
1138 while (nqueued < minqueue) {
1139 struct got_object_id *id;
1140 struct got_commit_object *commit;
1141 struct commit_queue_entry *entry;
1144 err = got_commit_graph_iter_next(&id, graph);
1146 if (err->code != GOT_ERR_ITER_NEED_MORE)
1148 err = got_commit_graph_fetch_commits(graph,
1158 err = got_object_open_as_commit(&commit, repo, id);
1161 entry = alloc_commit_queue_entry(commit, id);
1162 if (entry == NULL) {
1163 err = got_error_from_errno("alloc_commit_queue_entry");
1167 errcode = pthread_mutex_lock(&tog_mutex);
1169 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1173 entry->idx = commits->ncommits;
1174 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1176 commits->ncommits++;
1178 errcode = pthread_mutex_unlock(&tog_mutex);
1179 if (errcode && err == NULL)
1180 err = got_error_set_errno(errcode,
1181 "pthread_mutex_unlock");
1187 static const struct got_error *
1188 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 struct got_reference *head_ref;
1196 err = got_ref_open(&head_ref, repo, branch_name, 0);
1200 err = got_ref_resolve(head_id, repo, head_ref);
1201 got_ref_close(head_ref);
1210 static const struct got_error *
1211 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1212 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1213 struct commit_queue *commits, int selected_idx, int limit,
1214 struct got_reflist_head *refs, const char *path, int commits_needed)
1216 const struct got_error *err = NULL;
1217 struct commit_queue_entry *entry;
1219 int ncommits, author_cols = 10;
1220 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1221 char *refs_str = NULL;
1227 if (ncommits == selected_idx) {
1231 entry = TAILQ_NEXT(entry, entry);
1235 if (*selected && !(view->searching && view->search_next_done == 0)) {
1236 err = got_object_id_str(&id_str, (*selected)->id);
1240 err = build_refs_str(&refs_str, refs, (*selected)->id);
1246 if (commits_needed == 0)
1247 halfdelay(10); /* disable fast refresh */
1249 if (asprintf(&ncommits_str, " [%d/%d] %s",
1250 entry ? entry->idx + 1 : 0, commits->ncommits,
1251 commits_needed > 0 ?
1252 (view->searching && view->search_next_done == 0
1253 ? "searching..." : "loading... ") :
1254 (refs_str ? refs_str : "")) == -1) {
1255 err = got_error_from_errno("asprintf");
1259 if (path && strcmp(path, "/") != 0) {
1260 if (asprintf(&header, "commit %s %s%s",
1261 id_str ? id_str : "........................................",
1262 path, ncommits_str) == -1) {
1263 err = got_error_from_errno("asprintf");
1267 } else if (asprintf(&header, "commit %s%s",
1268 id_str ? id_str : "........................................",
1269 ncommits_str) == -1) {
1270 err = got_error_from_errno("asprintf");
1274 err = format_line(&wline, &width, header, view->ncols);
1278 werase(view->window);
1280 if (view_needs_focus_indication(view))
1281 wstandout(view->window);
1282 waddwstr(view->window, wline);
1283 while (width < view->ncols) {
1284 waddch(view->window, ' ');
1287 if (view_needs_focus_indication(view))
1288 wstandend(view->window);
1293 /* Grow author column size if necessary. */
1300 if (ncommits >= limit - 1)
1302 author = strdup(got_object_commit_get_author(entry->commit));
1303 if (author == NULL) {
1304 err = got_error_from_errno("strdup");
1307 err = format_author(&wauthor, &width, author, COLS);
1308 if (author_cols < width)
1309 author_cols = width;
1312 entry = TAILQ_NEXT(entry, entry);
1319 if (ncommits >= limit - 1)
1321 if (ncommits == selected_idx)
1322 wstandout(view->window);
1323 err = draw_commit(view, entry->commit, entry->id, refs,
1325 if (ncommits == selected_idx)
1326 wstandend(view->window);
1331 entry = TAILQ_NEXT(entry, entry);
1344 scroll_up(struct tog_view *view,
1345 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1346 struct commit_queue *commits)
1348 struct commit_queue_entry *entry;
1351 entry = TAILQ_FIRST(&commits->head);
1352 if (*first_displayed_entry == entry)
1355 entry = *first_displayed_entry;
1356 while (entry && nscrolled < maxscroll) {
1357 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1359 *first_displayed_entry = entry;
1365 static const struct got_error *
1366 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1367 pthread_cond_t *need_commits)
1372 halfdelay(1); /* fast refresh while loading commits */
1374 while (*commits_needed > 0) {
1378 /* Wake the log thread. */
1379 errcode = pthread_cond_signal(need_commits);
1381 return got_error_set_errno(errcode,
1382 "pthread_cond_signal");
1383 errcode = pthread_mutex_unlock(&tog_mutex);
1385 return got_error_set_errno(errcode,
1386 "pthread_mutex_unlock");
1388 errcode = pthread_mutex_lock(&tog_mutex);
1390 return got_error_set_errno(errcode,
1391 "pthread_mutex_lock");
1393 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1395 * Thread is not done yet; lose a key press
1396 * and let the user retry... this way the GUI
1397 * remains interactive while logging deep paths
1398 * with few commits in history.
1407 static const struct got_error *
1408 scroll_down(struct tog_view *view,
1409 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1410 struct commit_queue_entry **last_displayed_entry,
1411 struct commit_queue *commits, int *log_complete, int *commits_needed,
1412 pthread_cond_t *need_commits)
1414 const struct got_error *err = NULL;
1415 struct commit_queue_entry *pentry;
1418 if (*last_displayed_entry == NULL)
1421 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1422 if (pentry == NULL && !*log_complete) {
1424 * Ask the log thread for required amount of commits
1425 * plus some amount of pre-fetching.
1427 (*commits_needed) += maxscroll + 20;
1428 err = trigger_log_thread(0, commits_needed, log_complete,
1435 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1439 *last_displayed_entry = pentry;
1441 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1444 *first_displayed_entry = pentry;
1445 } while (++nscrolled < maxscroll);
1450 static const struct got_error *
1451 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1452 struct got_commit_object *commit, struct got_object_id *commit_id,
1453 struct tog_view *log_view, struct got_reflist_head *refs,
1454 struct got_repository *repo)
1456 const struct got_error *err;
1457 struct got_object_qid *parent_id;
1458 struct tog_view *diff_view;
1460 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1461 if (diff_view == NULL)
1462 return got_error_from_errno("view_open");
1464 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1465 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1466 commit_id, log_view, refs, repo);
1468 *new_view = diff_view;
1472 static const struct got_error *
1473 tree_view_visit_subtree(struct got_tree_object *subtree,
1474 struct tog_tree_view_state *s)
1476 struct tog_parent_tree *parent;
1478 parent = calloc(1, sizeof(*parent));
1480 return got_error_from_errno("calloc");
1482 parent->tree = s->tree;
1483 parent->first_displayed_entry = s->first_displayed_entry;
1484 parent->selected_entry = s->selected_entry;
1485 parent->selected = s->selected;
1486 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1488 s->entries = got_object_tree_get_entries(s->tree);
1490 s->first_displayed_entry = NULL;
1495 static const struct got_error *
1496 browse_commit_tree(struct tog_view **new_view, int begin_x,
1497 struct commit_queue_entry *entry, const char *path,
1498 struct got_reflist_head *refs, struct got_repository *repo)
1500 const struct got_error *err = NULL;
1501 struct got_tree_object *tree;
1502 struct got_tree_entry *te;
1503 struct tog_tree_view_state *s;
1504 struct tog_view *tree_view;
1505 char *slash, *subpath = NULL;
1508 err = got_object_open_as_tree(&tree, repo,
1509 got_object_commit_get_tree_id(entry->commit));
1513 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1514 if (tree_view == NULL)
1515 return got_error_from_errno("view_open");
1517 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1519 got_object_tree_close(tree);
1522 s = &tree_view->state.tree;
1524 *new_view = tree_view;
1526 /* Walk the path and open corresponding tree objects. */
1529 struct got_object_id *tree_id;
1534 /* Ensure the correct subtree entry is selected. */
1535 slash = strchr(p, '/');
1537 slash = strchr(p, '\0');
1538 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1539 if (strncmp(p, te->name, slash - p) == 0) {
1540 s->selected_entry = te;
1545 if (s->selected_entry == NULL) {
1546 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1549 if (s->tree != s->root)
1550 s->selected++; /* skip '..' */
1552 if (!S_ISDIR(s->selected_entry->mode)) {
1553 /* Jump to this file's entry. */
1554 s->first_displayed_entry = s->selected_entry;
1559 slash = strchr(p, '/');
1561 subpath = strndup(path, slash - path);
1563 subpath = strdup(path);
1564 if (subpath == NULL) {
1565 err = got_error_from_errno("strdup");
1569 err = got_object_id_by_path(&tree_id, repo, entry->id,
1574 err = got_object_open_as_tree(&tree, repo, tree_id);
1579 err = tree_view_visit_subtree(tree, s);
1581 got_object_tree_close(tree);
1596 log_thread(void *arg)
1598 const struct got_error *err = NULL;
1600 struct tog_log_thread_args *a = arg;
1603 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1607 while (!done && !err) {
1608 err = queue_commits(a->graph, a->commits, 1, a->repo,
1611 if (err->code != GOT_ERR_ITER_COMPLETED)
1615 } else if (a->commits_needed > 0)
1616 a->commits_needed--;
1618 errcode = pthread_mutex_lock(&tog_mutex);
1620 err = got_error_set_errno(errcode,
1621 "pthread_mutex_lock");
1623 } else if (*a->quit)
1625 else if (*a->first_displayed_entry == NULL) {
1626 *a->first_displayed_entry =
1627 TAILQ_FIRST(&a->commits->head);
1628 *a->selected_entry = *a->first_displayed_entry;
1632 a->commits_needed = 0;
1633 else if (a->commits_needed == 0) {
1634 errcode = pthread_cond_wait(&a->need_commits,
1637 err = got_error_set_errno(errcode,
1638 "pthread_cond_wait");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1642 if (errcode && err == NULL)
1643 err = got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1646 a->log_complete = 1;
1650 static const struct got_error *
1651 stop_log_thread(struct tog_log_view_state *s)
1653 const struct got_error *err = NULL;
1658 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1660 return got_error_set_errno(errcode,
1661 "pthread_cond_signal");
1662 errcode = pthread_mutex_unlock(&tog_mutex);
1664 return got_error_set_errno(errcode,
1665 "pthread_mutex_unlock");
1666 errcode = pthread_join(s->thread, (void **)&err);
1668 return got_error_set_errno(errcode, "pthread_join");
1669 errcode = pthread_mutex_lock(&tog_mutex);
1671 return got_error_set_errno(errcode,
1672 "pthread_mutex_lock");
1676 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1677 if (errcode && err == NULL)
1678 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1680 if (s->thread_args.repo) {
1681 got_repo_close(s->thread_args.repo);
1682 s->thread_args.repo = NULL;
1685 if (s->thread_args.graph) {
1686 got_commit_graph_close(s->thread_args.graph);
1687 s->thread_args.graph = NULL;
1693 static const struct got_error *
1694 close_log_view(struct tog_view *view)
1696 const struct got_error *err = NULL;
1697 struct tog_log_view_state *s = &view->state.log;
1699 err = stop_log_thread(s);
1700 free_commits(&s->commits);
1701 free(s->in_repo_path);
1702 s->in_repo_path = NULL;
1708 static const struct got_error *
1709 search_start_log_view(struct tog_view *view)
1711 struct tog_log_view_state *s = &view->state.log;
1713 s->matched_entry = NULL;
1718 match_commit(struct got_commit_object *commit, regex_t *regex)
1720 regmatch_t regmatch;
1722 if (regexec(regex, got_object_commit_get_author(commit), 1,
1723 ®match, 0) == 0 ||
1724 regexec(regex, got_object_commit_get_committer(commit), 1,
1725 ®match, 0) == 0 ||
1726 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1733 static const struct got_error *
1734 search_next_log_view(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1738 struct commit_queue_entry *entry;
1740 if (!view->searching) {
1741 view->search_next_done = 1;
1745 if (s->matched_entry) {
1746 if (view->searching == TOG_SEARCH_FORWARD)
1747 entry = TAILQ_NEXT(s->selected_entry, entry);
1749 entry = TAILQ_PREV(s->selected_entry,
1750 commit_queue_head, entry);
1752 if (view->searching == TOG_SEARCH_FORWARD)
1753 entry = TAILQ_FIRST(&s->commits.head);
1755 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1759 if (entry == NULL) {
1760 if (s->thread_args.log_complete) {
1761 if (s->matched_entry == NULL) {
1762 view->search_next_done = 1;
1765 if (view->searching == TOG_SEARCH_FORWARD)
1766 entry = TAILQ_FIRST(&s->commits.head);
1768 entry = TAILQ_LAST(&s->commits.head,
1771 s->thread_args.commits_needed = 1;
1772 return trigger_log_thread(0,
1773 &s->thread_args.commits_needed,
1774 &s->thread_args.log_complete,
1775 &s->thread_args.need_commits);
1779 if (match_commit(entry->commit, &view->regex)) {
1780 view->search_next_done = 1;
1781 s->matched_entry = entry;
1784 if (view->searching == TOG_SEARCH_FORWARD)
1785 entry = TAILQ_NEXT(entry, entry);
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1790 if (s->matched_entry) {
1791 int cur = s->selected_entry->idx;
1792 while (cur < s->matched_entry->idx) {
1793 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1798 while (cur > s->matched_entry->idx) {
1799 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1810 static const struct got_error *
1811 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 struct got_reflist_head *refs, struct got_repository *repo,
1813 const char *path, int check_disk)
1815 const struct got_error *err = NULL;
1816 struct tog_log_view_state *s = &view->state.log;
1817 struct got_repository *thread_repo = NULL;
1818 struct got_commit_graph *thread_graph = NULL;
1821 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1825 /* The commit queue only contains commits being displayed. */
1826 TAILQ_INIT(&s->commits.head);
1827 s->commits.ncommits = 0;
1831 s->start_id = got_object_id_dup(start_id);
1832 if (s->start_id == NULL) {
1833 err = got_error_from_errno("got_object_id_dup");
1837 view->show = show_log_view;
1838 view->input = input_log_view;
1839 view->close = close_log_view;
1840 view->search_start = search_start_log_view;
1841 view->search_next = search_next_log_view;
1843 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1846 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1851 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1853 err = got_error_set_errno(errcode, "pthread_cond_init");
1857 s->thread_args.commits_needed = view->nlines;
1858 s->thread_args.graph = thread_graph;
1859 s->thread_args.commits = &s->commits;
1860 s->thread_args.in_repo_path = s->in_repo_path;
1861 s->thread_args.start_id = s->start_id;
1862 s->thread_args.repo = thread_repo;
1863 s->thread_args.log_complete = 0;
1864 s->thread_args.quit = &s->quit;
1865 s->thread_args.view = view;
1866 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1867 s->thread_args.selected_entry = &s->selected_entry;
1870 close_log_view(view);
1874 static const struct got_error *
1875 show_log_view(struct tog_view *view)
1877 struct tog_log_view_state *s = &view->state.log;
1879 if (s->thread == NULL) {
1880 int errcode = pthread_create(&s->thread, NULL, log_thread,
1883 return got_error_set_errno(errcode, "pthread_create");
1886 return draw_commits(view, &s->last_displayed_entry,
1887 &s->selected_entry, s->first_displayed_entry,
1888 &s->commits, s->selected, view->nlines, s->refs,
1889 s->in_repo_path, s->thread_args.commits_needed);
1892 static const struct got_error *
1893 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1894 struct tog_view **focus_view, struct tog_view *view, int ch)
1896 const struct got_error *err = NULL;
1897 struct tog_log_view_state *s = &view->state.log;
1899 struct tog_view *diff_view = NULL, *tree_view = NULL;
1910 if (s->first_displayed_entry == NULL)
1912 if (s->selected > 0)
1915 scroll_up(view, &s->first_displayed_entry, 1,
1920 if (s->first_displayed_entry == NULL)
1922 if (TAILQ_FIRST(&s->commits.head) ==
1923 s->first_displayed_entry) {
1927 scroll_up(view, &s->first_displayed_entry,
1928 view->nlines, &s->commits);
1934 if (s->first_displayed_entry == NULL)
1936 if (s->selected < MIN(view->nlines - 2,
1937 s->commits.ncommits - 1)) {
1941 err = scroll_down(view, &s->first_displayed_entry, 1,
1942 &s->last_displayed_entry, &s->commits,
1943 &s->thread_args.log_complete,
1944 &s->thread_args.commits_needed,
1945 &s->thread_args.need_commits);
1949 struct commit_queue_entry *first;
1950 first = s->first_displayed_entry;
1953 err = scroll_down(view, &s->first_displayed_entry,
1954 view->nlines, &s->last_displayed_entry,
1955 &s->commits, &s->thread_args.log_complete,
1956 &s->thread_args.commits_needed,
1957 &s->thread_args.need_commits);
1958 if (first == s->first_displayed_entry &&
1959 s->selected < MIN(view->nlines - 2,
1960 s->commits.ncommits - 1)) {
1961 /* can't scroll further down */
1962 s->selected = MIN(view->nlines - 2,
1963 s->commits.ncommits - 1);
1969 if (s->selected > view->nlines - 2)
1970 s->selected = view->nlines - 2;
1971 if (s->selected > s->commits.ncommits - 1)
1972 s->selected = s->commits.ncommits - 1;
1977 if (s->selected_entry == NULL)
1979 if (view_is_parent_view(view))
1980 begin_x = view_split_begin_x(view->begin_x);
1981 err = open_diff_view_for_commit(&diff_view, begin_x,
1982 s->selected_entry->commit, s->selected_entry->id,
1983 view, s->refs, s->repo);
1986 if (view_is_parent_view(view)) {
1987 err = view_close_child(view);
1990 err = view_set_child(view, diff_view);
1992 view_close(diff_view);
1995 *focus_view = diff_view;
1996 view->child_focussed = 1;
1998 *new_view = diff_view;
2001 if (s->selected_entry == NULL)
2003 if (view_is_parent_view(view))
2004 begin_x = view_split_begin_x(view->begin_x);
2005 err = browse_commit_tree(&tree_view, begin_x,
2006 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2009 if (view_is_parent_view(view)) {
2010 err = view_close_child(view);
2013 err = view_set_child(view, tree_view);
2015 view_close(tree_view);
2018 *focus_view = tree_view;
2019 view->child_focussed = 1;
2021 *new_view = tree_view;
2024 if (strcmp(s->in_repo_path, "/") == 0)
2026 parent_path = dirname(s->in_repo_path);
2027 if (parent_path && strcmp(parent_path, ".") != 0) {
2028 struct tog_view *lv;
2029 err = stop_log_thread(s);
2032 lv = view_open(view->nlines, view->ncols,
2033 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2035 return got_error_from_errno(
2037 err = open_log_view(lv, s->start_id, s->refs,
2038 s->repo, parent_path, 0);
2041 if (view_is_parent_view(view))
2044 view_set_child(view->parent, lv);
2057 static const struct got_error *
2058 apply_unveil(const char *repo_path, const char *worktree_path)
2060 const struct got_error *error;
2062 if (repo_path && unveil(repo_path, "r") != 0)
2063 return got_error_from_errno2("unveil", repo_path);
2065 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2066 return got_error_from_errno2("unveil", worktree_path);
2068 if (unveil("/tmp", "rwc") != 0)
2069 return got_error_from_errno2("unveil", "/tmp");
2071 error = got_privsep_unveil_exec_helpers();
2075 if (unveil(NULL, NULL) != 0)
2076 return got_error_from_errno("unveil");
2086 halfdelay(1); /* Do fast refresh while initial view is loading. */
2089 intrflush(stdscr, FALSE);
2090 keypad(stdscr, TRUE);
2092 signal(SIGWINCH, tog_sigwinch);
2095 static const struct got_error *
2096 cmd_log(int argc, char *argv[])
2098 const struct got_error *error;
2099 struct got_repository *repo = NULL;
2100 struct got_worktree *worktree = NULL;
2101 struct got_reflist_head refs;
2102 struct got_object_id *start_id = NULL;
2103 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2104 char *start_commit = NULL;
2106 struct tog_view *view;
2108 SIMPLEQ_INIT(&refs);
2111 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2116 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2119 start_commit = optarg;
2122 repo_path = realpath(optarg, NULL);
2123 if (repo_path == NULL)
2124 err(1, "-r option");
2135 cwd = getcwd(NULL, 0);
2137 error = got_error_from_errno("getcwd");
2140 error = got_worktree_open(&worktree, cwd);
2141 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2148 error = got_error_from_errno("strdup");
2151 } else if (argc == 1) {
2153 error = got_worktree_resolve_path(&path, worktree,
2158 path = strdup(argv[0]);
2160 error = got_error_from_errno("strdup");
2167 repo_path = worktree ?
2168 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2169 if (repo_path == NULL) {
2170 error = got_error_from_errno("strdup");
2176 error = got_repo_open(&repo, repo_path);
2180 error = apply_unveil(got_repo_get_path(repo),
2181 worktree ? got_worktree_get_root_path(worktree) : NULL);
2185 if (start_commit == NULL)
2186 error = get_head_commit_id(&start_id, worktree ?
2187 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2190 error = got_object_resolve_id_str(&start_id, repo,
2195 error = got_ref_list(&refs, repo);
2199 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2201 error = got_error_from_errno("view_open");
2204 error = open_log_view(view, start_id, &refs, repo, path, 1);
2207 error = view_loop(view);
2214 got_repo_close(repo);
2216 got_worktree_close(worktree);
2217 got_ref_list_free(&refs);
2225 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2231 parse_next_line(FILE *f, size_t *len)
2236 const char delim[3] = { '\0', '\0', '\0'};
2238 line = fparseln(f, &linelen, &lineno, delim, 0);
2244 static const struct got_error *
2245 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2246 int *last_displayed_line, int *eof, int max_lines,
2249 const struct got_error *err;
2250 int nlines = 0, nprinted = 0;
2257 werase(view->window);
2260 err = format_line(&wline, &width, header, view->ncols);
2265 if (view_needs_focus_indication(view))
2266 wstandout(view->window);
2267 waddwstr(view->window, wline);
2268 if (view_needs_focus_indication(view))
2269 wstandend(view->window);
2270 if (width < view->ncols - 1)
2271 waddch(view->window, '\n');
2279 while (nprinted < max_lines) {
2280 line = parse_next_line(f, &len);
2285 if (++nlines < *first_displayed_line) {
2290 err = format_line(&wline, &width, line, view->ncols);
2295 waddwstr(view->window, wline);
2296 if (width < view->ncols - 1)
2297 waddch(view->window, '\n');
2298 if (++nprinted == 1)
2299 *first_displayed_line = nlines;
2304 *last_displayed_line = nlines;
2309 while (nprinted < view->nlines) {
2310 waddch(view->window, '\n');
2314 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2319 wstandout(view->window);
2320 waddwstr(view->window, wline);
2321 wstandend(view->window);
2328 get_datestr(time_t *time, char *datebuf)
2330 char *p, *s = ctime_r(time, datebuf);
2331 p = strchr(s, '\n');
2337 static const struct got_error *
2338 write_commit_info(struct got_object_id *commit_id,
2339 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2341 const struct got_error *err = NULL;
2343 struct got_commit_object *commit;
2344 char *id_str = NULL;
2345 time_t committer_time;
2346 const char *author, *committer;
2347 char *refs_str = NULL;
2350 err = build_refs_str(&refs_str, refs, commit_id);
2355 err = got_object_open_as_commit(&commit, repo, commit_id);
2359 err = got_object_id_str(&id_str, commit_id);
2361 err = got_error_from_errno("got_object_id_str");
2365 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2366 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2367 err = got_error_from_errno("fprintf");
2370 if (fprintf(outfile, "from: %s\n",
2371 got_object_commit_get_author(commit)) < 0) {
2372 err = got_error_from_errno("fprintf");
2375 committer_time = got_object_commit_get_committer_time(commit);
2376 if (fprintf(outfile, "date: %s UTC\n",
2377 get_datestr(&committer_time, datebuf)) < 0) {
2378 err = got_error_from_errno("fprintf");
2381 author = got_object_commit_get_author(commit);
2382 committer = got_object_commit_get_committer(commit);
2383 if (strcmp(author, committer) != 0 &&
2384 fprintf(outfile, "via: %s\n", committer) < 0) {
2385 err = got_error_from_errno("fprintf");
2388 if (fprintf(outfile, "%s\n",
2389 got_object_commit_get_logmsg(commit)) < 0) {
2390 err = got_error_from_errno("fprintf");
2396 got_object_commit_close(commit);
2400 static const struct got_error *
2401 create_diff(struct tog_diff_view_state *s)
2403 const struct got_error *err = NULL;
2409 err = got_error_from_errno("got_opentemp");
2412 if (s->f && fclose(s->f) != 0) {
2413 err = got_error_from_errno("fclose");
2419 err = got_object_get_type(&obj_type, s->repo, s->id1);
2421 err = got_object_get_type(&obj_type, s->repo, s->id2);
2426 case GOT_OBJ_TYPE_BLOB:
2427 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2428 s->diff_context, s->repo, f);
2430 case GOT_OBJ_TYPE_TREE:
2431 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2432 s->diff_context, s->repo, f);
2434 case GOT_OBJ_TYPE_COMMIT: {
2435 const struct got_object_id_queue *parent_ids;
2436 struct got_object_qid *pid;
2437 struct got_commit_object *commit2;
2439 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2442 /* Show commit info if we're diffing to a parent/root commit. */
2444 write_commit_info(s->id2, s->refs, s->repo, f);
2446 parent_ids = got_object_commit_get_parent_ids(commit2);
2447 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2448 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2449 write_commit_info(s->id2, s->refs,
2455 got_object_commit_close(commit2);
2457 err = got_diff_objects_as_commits(s->id1, s->id2,
2458 s->diff_context, s->repo, f);
2462 err = got_error(GOT_ERR_OBJ_TYPE);
2466 if (f && fflush(f) != 0 && err == NULL)
2467 err = got_error_from_errno("fflush");
2472 diff_view_indicate_progress(struct tog_view *view)
2474 mvwaddstr(view->window, 0, 0, "diffing...");
2479 static const struct got_error *
2480 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2481 struct got_object_id *id2, struct tog_view *log_view,
2482 struct got_reflist_head *refs, struct got_repository *repo)
2484 const struct got_error *err;
2486 if (id1 != NULL && id2 != NULL) {
2488 err = got_object_get_type(&type1, repo, id1);
2491 err = got_object_get_type(&type2, repo, id2);
2496 return got_error(GOT_ERR_OBJ_TYPE);
2500 view->state.diff.id1 = got_object_id_dup(id1);
2501 if (view->state.diff.id1 == NULL)
2502 return got_error_from_errno("got_object_id_dup");
2504 view->state.diff.id1 = NULL;
2506 view->state.diff.id2 = got_object_id_dup(id2);
2507 if (view->state.diff.id2 == NULL) {
2508 free(view->state.diff.id1);
2509 view->state.diff.id1 = NULL;
2510 return got_error_from_errno("got_object_id_dup");
2512 view->state.diff.f = NULL;
2513 view->state.diff.first_displayed_line = 1;
2514 view->state.diff.last_displayed_line = view->nlines;
2515 view->state.diff.diff_context = 3;
2516 view->state.diff.log_view = log_view;
2517 view->state.diff.repo = repo;
2518 view->state.diff.refs = refs;
2520 if (log_view && view_is_splitscreen(view))
2521 show_log_view(log_view); /* draw vborder */
2522 diff_view_indicate_progress(view);
2524 err = create_diff(&view->state.diff);
2526 free(view->state.diff.id1);
2527 view->state.diff.id1 = NULL;
2528 free(view->state.diff.id2);
2529 view->state.diff.id2 = NULL;
2533 view->show = show_diff_view;
2534 view->input = input_diff_view;
2535 view->close = close_diff_view;
2540 static const struct got_error *
2541 close_diff_view(struct tog_view *view)
2543 const struct got_error *err = NULL;
2545 free(view->state.diff.id1);
2546 view->state.diff.id1 = NULL;
2547 free(view->state.diff.id2);
2548 view->state.diff.id2 = NULL;
2549 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2550 err = got_error_from_errno("fclose");
2554 static const struct got_error *
2555 show_diff_view(struct tog_view *view)
2557 const struct got_error *err;
2558 struct tog_diff_view_state *s = &view->state.diff;
2559 char *id_str1 = NULL, *id_str2, *header;
2562 err = got_object_id_str(&id_str1, s->id1);
2566 err = got_object_id_str(&id_str2, s->id2);
2570 if (asprintf(&header, "diff %s %s",
2571 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2572 err = got_error_from_errno("asprintf");
2580 return draw_file(view, s->f, &s->first_displayed_line,
2581 &s->last_displayed_line, &s->eof, view->nlines,
2585 static const struct got_error *
2586 set_selected_commit(struct tog_diff_view_state *s,
2587 struct commit_queue_entry *entry)
2589 const struct got_error *err;
2590 const struct got_object_id_queue *parent_ids;
2591 struct got_commit_object *selected_commit;
2592 struct got_object_qid *pid;
2595 s->id2 = got_object_id_dup(entry->id);
2597 return got_error_from_errno("got_object_id_dup");
2599 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2602 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2604 pid = SIMPLEQ_FIRST(parent_ids);
2605 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2606 got_object_commit_close(selected_commit);
2610 static const struct got_error *
2611 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2612 struct tog_view **focus_view, struct tog_view *view, int ch)
2614 const struct got_error *err = NULL;
2615 struct tog_diff_view_state *s = &view->state.diff;
2616 struct tog_log_view_state *ls;
2617 struct commit_queue_entry *entry;
2623 if (s->first_displayed_line > 1)
2624 s->first_displayed_line--;
2628 if (s->first_displayed_line == 1)
2631 while (i++ < view->nlines - 1 &&
2632 s->first_displayed_line > 1)
2633 s->first_displayed_line--;
2638 s->first_displayed_line++;
2646 while (!s->eof && i++ < view->nlines - 1) {
2648 line = parse_next_line(s->f, NULL);
2649 s->first_displayed_line++;
2655 if (s->diff_context > 0) {
2657 diff_view_indicate_progress(view);
2658 err = create_diff(s);
2662 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2664 diff_view_indicate_progress(view);
2665 err = create_diff(s);
2670 if (s->log_view == NULL)
2672 ls = &s->log_view->state.log;
2673 entry = TAILQ_PREV(ls->selected_entry,
2674 commit_queue_head, entry);
2678 err = input_log_view(NULL, NULL, NULL, s->log_view,
2683 err = set_selected_commit(s, entry);
2687 s->first_displayed_line = 1;
2688 s->last_displayed_line = view->nlines;
2690 diff_view_indicate_progress(view);
2691 err = create_diff(s);
2695 if (s->log_view == NULL)
2697 ls = &s->log_view->state.log;
2699 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2700 ls->thread_args.commits_needed++;
2702 /* Display "loading..." in log view. */
2703 show_log_view(s->log_view);
2707 err = trigger_log_thread(1 /* load_all */,
2708 &ls->thread_args.commits_needed,
2709 &ls->thread_args.log_complete,
2710 &ls->thread_args.need_commits);
2714 err = input_log_view(NULL, NULL, NULL, s->log_view,
2719 entry = TAILQ_NEXT(ls->selected_entry, entry);
2723 err = set_selected_commit(s, entry);
2727 s->first_displayed_line = 1;
2728 s->last_displayed_line = view->nlines;
2730 diff_view_indicate_progress(view);
2731 err = create_diff(s);
2740 static const struct got_error *
2741 cmd_diff(int argc, char *argv[])
2743 const struct got_error *error = NULL;
2744 struct got_repository *repo = NULL;
2745 struct got_reflist_head refs;
2746 struct got_object_id *id1 = NULL, *id2 = NULL;
2747 char *repo_path = NULL;
2748 char *id_str1 = NULL, *id_str2 = NULL;
2750 struct tog_view *view;
2752 SIMPLEQ_INIT(&refs);
2755 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2760 while ((ch = getopt(argc, argv, "")) != -1) {
2772 usage_diff(); /* TODO show local worktree changes */
2773 } else if (argc == 2) {
2774 repo_path = getcwd(NULL, 0);
2775 if (repo_path == NULL)
2776 return got_error_from_errno("getcwd");
2779 } else if (argc == 3) {
2780 repo_path = realpath(argv[0], NULL);
2781 if (repo_path == NULL)
2782 return got_error_from_errno2("realpath", argv[0]);
2790 error = got_repo_open(&repo, repo_path);
2794 error = apply_unveil(got_repo_get_path(repo), NULL);
2798 error = got_object_resolve_id_str(&id1, repo, id_str1);
2802 error = got_object_resolve_id_str(&id2, repo, id_str2);
2806 error = got_ref_list(&refs, repo);
2810 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2812 error = got_error_from_errno("view_open");
2815 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2818 error = view_loop(view);
2821 got_repo_close(repo);
2822 got_ref_list_free(&refs);
2830 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2835 struct tog_blame_line {
2837 struct got_object_id *id;
2840 static const struct got_error *
2841 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2842 const char *path, struct tog_blame_line *lines, int nlines,
2843 int blame_complete, int selected_line, int *first_displayed_line,
2844 int *last_displayed_line, int *eof, int max_lines)
2846 const struct got_error *err;
2847 int lineno = 0, nprinted = 0;
2852 struct tog_blame_line *blame_line;
2853 struct got_object_id *prev_id = NULL;
2856 err = got_object_id_str(&id_str, id);
2861 werase(view->window);
2863 if (asprintf(&line, "commit %s", id_str) == -1) {
2864 err = got_error_from_errno("asprintf");
2869 err = format_line(&wline, &width, line, view->ncols);
2872 if (view_needs_focus_indication(view))
2873 wstandout(view->window);
2874 waddwstr(view->window, wline);
2875 if (view_needs_focus_indication(view))
2876 wstandend(view->window);
2879 if (width < view->ncols - 1)
2880 waddch(view->window, '\n');
2882 if (asprintf(&line, "[%d/%d] %s%s",
2883 *first_displayed_line - 1 + selected_line, nlines,
2884 blame_complete ? "" : "annotating... ", path) == -1) {
2886 return got_error_from_errno("asprintf");
2889 err = format_line(&wline, &width, line, view->ncols);
2894 waddwstr(view->window, wline);
2897 if (width < view->ncols - 1)
2898 waddch(view->window, '\n');
2901 while (nprinted < max_lines - 2) {
2902 line = parse_next_line(f, &len);
2907 if (++lineno < *first_displayed_line) {
2912 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2913 err = format_line(&wline, &width, line, wlimit);
2919 if (view->focussed && nprinted == selected_line - 1)
2920 wstandout(view->window);
2922 blame_line = &lines[lineno - 1];
2923 if (blame_line->annotated && prev_id &&
2924 got_object_id_cmp(prev_id, blame_line->id) == 0)
2925 waddstr(view->window, " ");
2926 else if (blame_line->annotated) {
2928 err = got_object_id_str(&id_str, blame_line->id);
2934 wprintw(view->window, "%.8s ", id_str);
2936 prev_id = blame_line->id;
2938 waddstr(view->window, "........ ");
2942 waddwstr(view->window, wline);
2943 while (width < wlimit) {
2944 waddch(view->window, ' ');
2947 if (view->focussed && nprinted == selected_line - 1)
2948 wstandend(view->window);
2949 if (++nprinted == 1)
2950 *first_displayed_line = lineno;
2955 *last_displayed_line = lineno;
2962 static const struct got_error *
2963 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2965 const struct got_error *err = NULL;
2966 struct tog_blame_cb_args *a = arg;
2967 struct tog_blame_line *line;
2970 if (nlines != a->nlines ||
2971 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2972 return got_error(GOT_ERR_RANGE);
2974 errcode = pthread_mutex_lock(&tog_mutex);
2976 return got_error_set_errno(errcode, "pthread_mutex_lock");
2978 if (*a->quit) { /* user has quit the blame view */
2979 err = got_error(GOT_ERR_ITER_COMPLETED);
2984 goto done; /* no change in this commit */
2986 line = &a->lines[lineno - 1];
2987 if (line->annotated)
2990 line->id = got_object_id_dup(id);
2991 if (line->id == NULL) {
2992 err = got_error_from_errno("got_object_id_dup");
2995 line->annotated = 1;
2997 errcode = pthread_mutex_unlock(&tog_mutex);
2999 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3004 blame_thread(void *arg)
3006 const struct got_error *err;
3007 struct tog_blame_thread_args *ta = arg;
3008 struct tog_blame_cb_args *a = ta->cb_args;
3011 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3012 blame_cb, ta->cb_args);
3014 errcode = pthread_mutex_lock(&tog_mutex);
3016 return (void *)got_error_set_errno(errcode,
3017 "pthread_mutex_lock");
3019 got_repo_close(ta->repo);
3023 errcode = pthread_mutex_unlock(&tog_mutex);
3024 if (errcode && err == NULL)
3025 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3030 static struct got_object_id *
3031 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3034 struct tog_blame_line *line;
3036 line = &lines[first_displayed_line - 1 + selected_line - 1];
3037 if (!line->annotated)
3043 static const struct got_error *
3044 stop_blame(struct tog_blame *blame)
3046 const struct got_error *err = NULL;
3049 if (blame->thread) {
3051 errcode = pthread_mutex_unlock(&tog_mutex);
3053 return got_error_set_errno(errcode,
3054 "pthread_mutex_unlock");
3055 errcode = pthread_join(blame->thread, (void **)&err);
3057 return got_error_set_errno(errcode, "pthread_join");
3058 errcode = pthread_mutex_lock(&tog_mutex);
3060 return got_error_set_errno(errcode,
3061 "pthread_mutex_lock");
3062 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3064 blame->thread = NULL;
3066 if (blame->thread_args.repo) {
3067 got_repo_close(blame->thread_args.repo);
3068 blame->thread_args.repo = NULL;
3071 if (fclose(blame->f) != 0 && err == NULL)
3072 err = got_error_from_errno("fclose");
3076 for (i = 0; i < blame->nlines; i++)
3077 free(blame->lines[i].id);
3079 blame->lines = NULL;
3081 free(blame->cb_args.commit_id);
3082 blame->cb_args.commit_id = NULL;
3087 static const struct got_error *
3088 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3089 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3090 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3091 struct got_repository *repo)
3093 const struct got_error *err = NULL;
3094 struct got_blob_object *blob = NULL;
3095 struct got_repository *thread_repo = NULL;
3096 struct got_object_id *obj_id = NULL;
3099 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3103 return got_error(GOT_ERR_NO_OBJ);
3105 err = got_object_get_type(&obj_type, repo, obj_id);
3109 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3110 err = got_error(GOT_ERR_OBJ_TYPE);
3114 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3117 blame->f = got_opentemp();
3118 if (blame->f == NULL) {
3119 err = got_error_from_errno("got_opentemp");
3122 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3127 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3128 if (blame->lines == NULL) {
3129 err = got_error_from_errno("calloc");
3133 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3137 blame->cb_args.view = view;
3138 blame->cb_args.lines = blame->lines;
3139 blame->cb_args.nlines = blame->nlines;
3140 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3141 if (blame->cb_args.commit_id == NULL) {
3142 err = got_error_from_errno("got_object_id_dup");
3145 blame->cb_args.quit = done;
3147 blame->thread_args.path = path;
3148 blame->thread_args.repo = thread_repo;
3149 blame->thread_args.cb_args = &blame->cb_args;
3150 blame->thread_args.complete = blame_complete;
3151 *blame_complete = 0;
3155 got_object_blob_close(blob);
3162 static const struct got_error *
3163 open_blame_view(struct tog_view *view, char *path,
3164 struct got_object_id *commit_id, struct got_reflist_head *refs,
3165 struct got_repository *repo)
3167 const struct got_error *err = NULL;
3168 struct tog_blame_view_state *s = &view->state.blame;
3170 SIMPLEQ_INIT(&s->blamed_commits);
3172 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3176 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3177 s->first_displayed_line = 1;
3178 s->last_displayed_line = view->nlines;
3179 s->selected_line = 1;
3180 s->blame_complete = 0;
3182 if (s->path == NULL)
3183 return got_error_from_errno("open_blame_view");
3186 s->commit_id = commit_id;
3187 memset(&s->blame, 0, sizeof(s->blame));
3189 view->show = show_blame_view;
3190 view->input = input_blame_view;
3191 view->close = close_blame_view;
3193 return run_blame(&s->blame, view, &s->blame_complete,
3194 &s->first_displayed_line, &s->last_displayed_line,
3195 &s->selected_line, &s->done, &s->eof, s->path,
3196 s->blamed_commit->id, s->repo);
3199 static const struct got_error *
3200 close_blame_view(struct tog_view *view)
3202 const struct got_error *err = NULL;
3203 struct tog_blame_view_state *s = &view->state.blame;
3205 if (s->blame.thread)
3206 err = stop_blame(&s->blame);
3208 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3209 struct got_object_qid *blamed_commit;
3210 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3211 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3212 got_object_qid_free(blamed_commit);
3220 static const struct got_error *
3221 show_blame_view(struct tog_view *view)
3223 const struct got_error *err = NULL;
3224 struct tog_blame_view_state *s = &view->state.blame;
3227 if (s->blame.thread == NULL) {
3228 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3229 &s->blame.thread_args);
3231 return got_error_set_errno(errcode, "pthread_create");
3234 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3235 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3236 s->selected_line, &s->first_displayed_line,
3237 &s->last_displayed_line, &s->eof, view->nlines);
3243 static const struct got_error *
3244 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3245 struct tog_view **focus_view, struct tog_view *view, int ch)
3247 const struct got_error *err = NULL, *thread_err = NULL;
3248 struct tog_view *diff_view;
3249 struct tog_blame_view_state *s = &view->state.blame;
3258 if (s->selected_line > 1)
3260 else if (s->selected_line == 1 &&
3261 s->first_displayed_line > 1)
3262 s->first_displayed_line--;
3265 if (s->first_displayed_line == 1) {
3266 s->selected_line = 1;
3269 if (s->first_displayed_line > view->nlines - 2)
3270 s->first_displayed_line -=
3273 s->first_displayed_line = 1;
3277 if (s->selected_line < view->nlines - 2 &&
3278 s->first_displayed_line +
3279 s->selected_line <= s->blame.nlines)
3281 else if (s->last_displayed_line <
3283 s->first_displayed_line++;
3287 struct got_object_id *id = NULL;
3288 id = get_selected_commit_id(s->blame.lines,
3289 s->first_displayed_line, s->selected_line);
3293 struct got_commit_object *commit;
3294 struct got_object_qid *pid;
3295 struct got_object_id *blob_id = NULL;
3297 err = got_object_open_as_commit(&commit,
3301 pid = SIMPLEQ_FIRST(
3302 got_object_commit_get_parent_ids(commit));
3304 got_object_commit_close(commit);
3307 /* Check if path history ends here. */
3308 err = got_object_id_by_path(&blob_id, s->repo,
3311 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3313 got_object_commit_close(commit);
3316 err = got_object_get_type(&obj_type, s->repo,
3319 /* Can't blame non-blob type objects. */
3320 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3321 got_object_commit_close(commit);
3324 err = got_object_qid_alloc(&s->blamed_commit,
3326 got_object_commit_close(commit);
3328 if (got_object_id_cmp(id,
3329 s->blamed_commit->id) == 0)
3331 err = got_object_qid_alloc(&s->blamed_commit,
3337 thread_err = stop_blame(&s->blame);
3341 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3342 s->blamed_commit, entry);
3343 err = run_blame(&s->blame, view, &s->blame_complete,
3344 &s->first_displayed_line, &s->last_displayed_line,
3345 &s->selected_line, &s->done, &s->eof,
3346 s->path, s->blamed_commit->id, s->repo);
3352 struct got_object_qid *first;
3353 first = SIMPLEQ_FIRST(&s->blamed_commits);
3354 if (!got_object_id_cmp(first->id, s->commit_id))
3357 thread_err = stop_blame(&s->blame);
3361 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3362 got_object_qid_free(s->blamed_commit);
3364 SIMPLEQ_FIRST(&s->blamed_commits);
3365 err = run_blame(&s->blame, view, &s->blame_complete,
3366 &s->first_displayed_line, &s->last_displayed_line,
3367 &s->selected_line, &s->done, &s->eof, s->path,
3368 s->blamed_commit->id, s->repo);
3375 struct got_object_id *id = NULL;
3376 struct got_object_qid *pid;
3377 struct got_commit_object *commit = NULL;
3378 id = get_selected_commit_id(s->blame.lines,
3379 s->first_displayed_line, s->selected_line);
3382 err = got_object_open_as_commit(&commit, s->repo, id);
3385 pid = SIMPLEQ_FIRST(
3386 got_object_commit_get_parent_ids(commit));
3387 if (view_is_parent_view(view))
3388 begin_x = view_split_begin_x(view->begin_x);
3389 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3390 if (diff_view == NULL) {
3391 got_object_commit_close(commit);
3392 err = got_error_from_errno("view_open");
3395 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3396 id, NULL, s->refs, s->repo);
3397 got_object_commit_close(commit);
3399 view_close(diff_view);
3402 if (view_is_parent_view(view)) {
3403 err = view_close_child(view);
3406 err = view_set_child(view, diff_view);
3408 view_close(diff_view);
3411 *focus_view = diff_view;
3412 view->child_focussed = 1;
3414 *new_view = diff_view;
3421 if (s->last_displayed_line >= s->blame.nlines &&
3422 s->selected_line >= MIN(s->blame.nlines,
3423 view->nlines - 2)) {
3426 if (s->last_displayed_line >= s->blame.nlines &&
3427 s->selected_line < view->nlines - 2) {
3428 s->selected_line = MIN(s->blame.nlines,
3432 if (s->last_displayed_line + view->nlines - 2
3434 s->first_displayed_line +=
3437 s->first_displayed_line =
3442 if (s->selected_line > view->nlines - 2) {
3443 s->selected_line = MIN(s->blame.nlines,
3450 return thread_err ? thread_err : err;
3453 static const struct got_error *
3454 cmd_blame(int argc, char *argv[])
3456 const struct got_error *error;
3457 struct got_repository *repo = NULL;
3458 struct got_reflist_head refs;
3459 struct got_worktree *worktree = NULL;
3460 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3461 struct got_object_id *commit_id = NULL;
3462 char *commit_id_str = NULL;
3464 struct tog_view *view;
3466 SIMPLEQ_INIT(&refs);
3469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3474 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3477 commit_id_str = optarg;
3480 repo_path = realpath(optarg, NULL);
3481 if (repo_path == NULL)
3482 err(1, "-r option");
3498 cwd = getcwd(NULL, 0);
3500 error = got_error_from_errno("getcwd");
3503 if (repo_path == NULL) {
3504 error = got_worktree_open(&worktree, cwd);
3505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3511 strdup(got_worktree_get_repo_path(worktree));
3512 if (repo_path == NULL)
3513 error = got_error_from_errno("strdup");
3517 repo_path = strdup(cwd);
3518 if (repo_path == NULL) {
3519 error = got_error_from_errno("strdup");
3527 error = got_repo_open(&repo, repo_path);
3531 error = apply_unveil(got_repo_get_path(repo), NULL);
3536 const char *prefix = got_worktree_get_path_prefix(worktree);
3537 char *p, *worktree_subdir = cwd +
3538 strlen(got_worktree_get_root_path(worktree));
3539 if (asprintf(&p, "%s%s%s%s%s",
3540 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3541 worktree_subdir, worktree_subdir[0] ? "/" : "",
3543 error = got_error_from_errno("asprintf");
3546 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3549 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3554 if (commit_id_str == NULL) {
3555 struct got_reference *head_ref;
3556 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3559 error = got_ref_resolve(&commit_id, repo, head_ref);
3560 got_ref_close(head_ref);
3562 error = got_object_resolve_id_str(&commit_id, repo,
3568 error = got_ref_list(&refs, repo);
3572 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3574 error = got_error_from_errno("view_open");
3577 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3580 error = view_loop(view);
3586 got_worktree_close(worktree);
3588 got_repo_close(repo);
3589 got_ref_list_free(&refs);
3593 static const struct got_error *
3594 draw_tree_entries(struct tog_view *view,
3595 struct got_tree_entry **first_displayed_entry,
3596 struct got_tree_entry **last_displayed_entry,
3597 struct got_tree_entry **selected_entry, int *ndisplayed,
3598 const char *label, int show_ids, const char *parent_path,
3599 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3601 const struct got_error *err = NULL;
3602 struct got_tree_entry *te;
3608 werase(view->window);
3613 err = format_line(&wline, &width, label, view->ncols);
3616 if (view_needs_focus_indication(view))
3617 wstandout(view->window);
3618 waddwstr(view->window, wline);
3619 if (view_needs_focus_indication(view))
3620 wstandend(view->window);
3623 if (width < view->ncols - 1)
3624 waddch(view->window, '\n');
3627 err = format_line(&wline, &width, parent_path, view->ncols);
3630 waddwstr(view->window, wline);
3633 if (width < view->ncols - 1)
3634 waddch(view->window, '\n');
3637 waddch(view->window, '\n');
3641 te = SIMPLEQ_FIRST(&entries->head);
3642 if (*first_displayed_entry == NULL) {
3643 if (selected == 0) {
3645 wstandout(view->window);
3646 *selected_entry = NULL;
3648 waddstr(view->window, " ..\n"); /* parent directory */
3649 if (selected == 0 && view->focussed)
3650 wstandend(view->window);
3657 while (te != *first_displayed_entry)
3658 te = SIMPLEQ_NEXT(te, entry);
3662 char *line = NULL, *id_str = NULL;
3665 err = got_object_id_str(&id_str, te->id);
3667 return got_error_from_errno(
3668 "got_object_id_str");
3670 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3671 te->name, S_ISDIR(te->mode) ? "/" :
3672 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3674 return got_error_from_errno("asprintf");
3677 err = format_line(&wline, &width, line, view->ncols);
3682 if (n == selected) {
3684 wstandout(view->window);
3685 *selected_entry = te;
3687 waddwstr(view->window, wline);
3688 if (width < view->ncols - 1)
3689 waddch(view->window, '\n');
3690 if (n == selected && view->focussed)
3691 wstandend(view->window);
3697 *last_displayed_entry = te;
3700 te = SIMPLEQ_NEXT(te, entry);
3707 tree_scroll_up(struct tog_view *view,
3708 struct got_tree_entry **first_displayed_entry, int maxscroll,
3709 const struct got_tree_entries *entries, int isroot)
3711 struct got_tree_entry *te, *prev;
3714 if (*first_displayed_entry == NULL)
3717 te = SIMPLEQ_FIRST(&entries->head);
3718 if (*first_displayed_entry == te) {
3720 *first_displayed_entry = NULL;
3724 /* XXX this is stupid... switch to TAILQ? */
3725 for (i = 0; i < maxscroll; i++) {
3726 while (te != *first_displayed_entry) {
3728 te = SIMPLEQ_NEXT(te, entry);
3730 *first_displayed_entry = prev;
3731 te = SIMPLEQ_FIRST(&entries->head);
3733 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3734 *first_displayed_entry = NULL;
3738 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3739 struct got_tree_entry *last_displayed_entry,
3740 const struct got_tree_entries *entries)
3742 struct got_tree_entry *next, *last;
3745 if (*first_displayed_entry)
3746 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3748 next = SIMPLEQ_FIRST(&entries->head);
3749 last = last_displayed_entry;
3750 while (next && last && n++ < maxscroll) {
3751 last = SIMPLEQ_NEXT(last, entry);
3753 *first_displayed_entry = next;
3754 next = SIMPLEQ_NEXT(next, entry);
3760 static const struct got_error *
3761 tree_entry_path(char **path, struct tog_parent_trees *parents,
3762 struct got_tree_entry *te)
3764 const struct got_error *err = NULL;
3765 struct tog_parent_tree *pt;
3766 size_t len = 2; /* for leading slash and NUL */
3768 TAILQ_FOREACH(pt, parents, entry)
3769 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3771 len += strlen(te->name);
3773 *path = calloc(1, len);
3775 return got_error_from_errno("calloc");
3778 pt = TAILQ_LAST(parents, tog_parent_trees);
3780 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3781 err = got_error(GOT_ERR_NO_SPACE);
3784 if (strlcat(*path, "/", len) >= len) {
3785 err = got_error(GOT_ERR_NO_SPACE);
3788 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3791 if (strlcat(*path, te->name, len) >= len) {
3792 err = got_error(GOT_ERR_NO_SPACE);
3804 static const struct got_error *
3805 blame_tree_entry(struct tog_view **new_view, int begin_x,
3806 struct got_tree_entry *te, struct tog_parent_trees *parents,
3807 struct got_object_id *commit_id, struct got_reflist_head *refs,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3812 struct tog_view *blame_view;
3814 err = tree_entry_path(&path, parents, te);
3818 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3819 if (blame_view == NULL)
3820 return got_error_from_errno("view_open");
3822 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3824 view_close(blame_view);
3827 *new_view = blame_view;
3831 static const struct got_error *
3832 log_tree_entry(struct tog_view **new_view, int begin_x,
3833 struct got_tree_entry *te, struct tog_parent_trees *parents,
3834 struct got_object_id *commit_id, struct got_reflist_head *refs,
3835 struct got_repository *repo)
3837 struct tog_view *log_view;
3838 const struct got_error *err = NULL;
3841 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3842 if (log_view == NULL)
3843 return got_error_from_errno("view_open");
3845 err = tree_entry_path(&path, parents, te);
3849 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3851 view_close(log_view);
3853 *new_view = log_view;
3858 static const struct got_error *
3859 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3860 struct got_object_id *commit_id, struct got_reflist_head *refs,
3861 struct got_repository *repo)
3863 const struct got_error *err = NULL;
3864 char *commit_id_str = NULL;
3865 struct tog_tree_view_state *s = &view->state.tree;
3867 TAILQ_INIT(&s->parents);
3869 err = got_object_id_str(&commit_id_str, commit_id);
3873 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3874 err = got_error_from_errno("asprintf");
3878 s->root = s->tree = root;
3879 s->entries = got_object_tree_get_entries(root);
3880 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3881 s->commit_id = got_object_id_dup(commit_id);
3882 if (s->commit_id == NULL) {
3883 err = got_error_from_errno("got_object_id_dup");
3889 view->show = show_tree_view;
3890 view->input = input_tree_view;
3891 view->close = close_tree_view;
3893 free(commit_id_str);
3895 free(s->tree_label);
3896 s->tree_label = NULL;
3901 static const struct got_error *
3902 close_tree_view(struct tog_view *view)
3904 struct tog_tree_view_state *s = &view->state.tree;
3906 free(s->tree_label);
3907 s->tree_label = NULL;
3909 s->commit_id = NULL;
3910 while (!TAILQ_EMPTY(&s->parents)) {
3911 struct tog_parent_tree *parent;
3912 parent = TAILQ_FIRST(&s->parents);
3913 TAILQ_REMOVE(&s->parents, parent, entry);
3917 if (s->tree != s->root)
3918 got_object_tree_close(s->tree);
3919 got_object_tree_close(s->root);
3924 static const struct got_error *
3925 show_tree_view(struct tog_view *view)
3927 const struct got_error *err = NULL;
3928 struct tog_tree_view_state *s = &view->state.tree;
3931 err = tree_entry_path(&parent_path, &s->parents, NULL);
3935 err = draw_tree_entries(view, &s->first_displayed_entry,
3936 &s->last_displayed_entry, &s->selected_entry,
3937 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3938 s->entries, s->selected, view->nlines, s->tree == s->root);
3945 static const struct got_error *
3946 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3947 struct tog_view **focus_view, struct tog_view *view, int ch)
3949 const struct got_error *err = NULL;
3950 struct tog_tree_view_state *s = &view->state.tree;
3951 struct tog_view *log_view;
3952 int begin_x = 0, nscrolled;
3956 s->show_ids = !s->show_ids;
3959 if (!s->selected_entry)
3961 if (view_is_parent_view(view))
3962 begin_x = view_split_begin_x(view->begin_x);
3963 err = log_tree_entry(&log_view, begin_x,
3964 s->selected_entry, &s->parents,
3965 s->commit_id, s->refs, s->repo);
3966 if (view_is_parent_view(view)) {
3967 err = view_close_child(view);
3970 err = view_set_child(view, log_view);
3972 view_close(log_view);
3975 *focus_view = log_view;
3976 view->child_focussed = 1;
3978 *new_view = log_view;
3982 if (s->selected > 0) {
3984 if (s->selected == 0)
3987 if (s->selected > 0)
3989 tree_scroll_up(view, &s->first_displayed_entry, 1,
3990 s->entries, s->tree == s->root);
3993 tree_scroll_up(view, &s->first_displayed_entry,
3994 MAX(0, view->nlines - 4 - s->selected), s->entries,
3995 s->tree == s->root);
3997 if (SIMPLEQ_FIRST(&s->entries->head) ==
3998 s->first_displayed_entry && s->tree != s->root)
3999 s->first_displayed_entry = NULL;
4003 if (s->selected < s->ndisplayed - 1) {
4007 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4008 /* can't scroll any further */
4010 tree_scroll_down(&s->first_displayed_entry, 1,
4011 s->last_displayed_entry, s->entries);
4014 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4016 /* can't scroll any further; move cursor down */
4017 if (s->selected < s->ndisplayed - 1)
4018 s->selected = s->ndisplayed - 1;
4021 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4022 view->nlines, s->last_displayed_entry, s->entries);
4023 if (nscrolled < view->nlines) {
4025 struct got_tree_entry *te;
4026 te = s->first_displayed_entry;
4029 te = SIMPLEQ_NEXT(te, entry);
4031 s->selected = ndisplayed - 1;
4037 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4038 struct tog_parent_tree *parent;
4039 /* user selected '..' */
4040 if (s->tree == s->root)
4042 parent = TAILQ_FIRST(&s->parents);
4043 TAILQ_REMOVE(&s->parents, parent,
4045 got_object_tree_close(s->tree);
4046 s->tree = parent->tree;
4048 got_object_tree_get_entries(s->tree);
4049 s->first_displayed_entry =
4050 parent->first_displayed_entry;
4052 parent->selected_entry;
4053 s->selected = parent->selected;
4055 } else if (S_ISDIR(s->selected_entry->mode)) {
4056 struct got_tree_object *subtree;
4057 err = got_object_open_as_tree(&subtree,
4058 s->repo, s->selected_entry->id);
4061 err = tree_view_visit_subtree(subtree, s);
4063 got_object_tree_close(subtree);
4066 } else if (S_ISREG(s->selected_entry->mode)) {
4067 struct tog_view *blame_view;
4068 int begin_x = view_is_parent_view(view) ?
4069 view_split_begin_x(view->begin_x) : 0;
4071 err = blame_tree_entry(&blame_view, begin_x,
4072 s->selected_entry, &s->parents,
4073 s->commit_id, s->refs, s->repo);
4076 if (view_is_parent_view(view)) {
4077 err = view_close_child(view);
4080 err = view_set_child(view, blame_view);
4082 view_close(blame_view);
4085 *focus_view = blame_view;
4086 view->child_focussed = 1;
4088 *new_view = blame_view;
4092 if (s->selected > view->nlines)
4093 s->selected = s->ndisplayed - 1;
4106 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4111 static const struct got_error *
4112 cmd_tree(int argc, char *argv[])
4114 const struct got_error *error;
4115 struct got_repository *repo = NULL;
4116 struct got_reflist_head refs;
4117 char *repo_path = NULL;
4118 struct got_object_id *commit_id = NULL;
4119 char *commit_id_arg = NULL;
4120 struct got_commit_object *commit = NULL;
4121 struct got_tree_object *tree = NULL;
4123 struct tog_view *view;
4125 SIMPLEQ_INIT(&refs);
4128 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4133 while ((ch = getopt(argc, argv, "c:")) != -1) {
4136 commit_id_arg = optarg;
4148 struct got_worktree *worktree;
4149 char *cwd = getcwd(NULL, 0);
4151 return got_error_from_errno("getcwd");
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4158 strdup(got_worktree_get_repo_path(worktree));
4159 got_worktree_close(worktree);
4162 if (repo_path == NULL) {
4163 error = got_error_from_errno("strdup");
4166 } else if (argc == 1) {
4167 repo_path = realpath(argv[0], NULL);
4168 if (repo_path == NULL)
4169 return got_error_from_errno2("realpath", argv[0]);
4175 error = got_repo_open(&repo, repo_path);
4179 error = apply_unveil(got_repo_get_path(repo), NULL);
4183 if (commit_id_arg == NULL)
4184 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4186 error = got_object_resolve_id_str(&commit_id, repo,
4191 error = got_object_open_as_commit(&commit, repo, commit_id);
4195 error = got_object_open_as_tree(&tree, repo,
4196 got_object_commit_get_tree_id(commit));
4200 error = got_ref_list(&refs, repo);
4204 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4206 error = got_error_from_errno("view_open");
4209 error = open_tree_view(view, tree, commit_id, &refs, repo);
4212 error = view_loop(view);
4217 got_object_commit_close(commit);
4219 got_object_tree_close(tree);
4221 got_repo_close(repo);
4222 got_ref_list_free(&refs);
4231 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4232 "Available commands:\n", getprogname());
4233 for (i = 0; i < nitems(tog_commands); i++) {
4234 struct tog_cmd *cmd = &tog_commands[i];
4235 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4241 make_argv(const char *arg0, const char *arg1)
4244 int argc = (arg1 == NULL ? 1 : 2);
4246 argv = calloc(argc, sizeof(char *));
4249 argv[0] = strdup(arg0);
4250 if (argv[0] == NULL)
4253 argv[1] = strdup(arg1);
4254 if (argv[1] == NULL)
4262 main(int argc, char *argv[])
4264 const struct got_error *error = NULL;
4265 struct tog_cmd *cmd = NULL;
4267 char **cmd_argv = NULL;
4269 setlocale(LC_CTYPE, "");
4271 while ((ch = getopt(argc, argv, "h")) != -1) {
4290 /* Build an argument vector which runs a default command. */
4291 cmd = &tog_commands[0];
4292 cmd_argv = make_argv(cmd->name, NULL);
4297 /* Did the user specific a command? */
4298 for (i = 0; i < nitems(tog_commands); i++) {
4299 if (strncmp(tog_commands[i].name, argv[0],
4300 strlen(argv[0])) == 0) {
4301 cmd = &tog_commands[i];
4303 tog_commands[i].cmd_usage();
4308 /* Did the user specify a repository? */
4309 char *repo_path = realpath(argv[0], NULL);
4311 struct got_repository *repo;
4312 error = got_repo_open(&repo, repo_path);
4314 got_repo_close(repo);
4316 error = got_error_from_errno2("realpath",
4320 fprintf(stderr, "%s: '%s' is not a "
4321 "known command\n", getprogname(),
4325 fprintf(stderr, "%s: '%s' is neither a known "
4326 "command nor a path to a repository\n",
4327 getprogname(), argv[0]);
4331 cmd = &tog_commands[0];
4332 cmd_argv = make_argv(cmd->name, repo_path);
4338 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4345 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);