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;
164 struct commit_queue_entry *matched_entry;
167 struct tog_blame_cb_args {
168 struct tog_blame_line *lines; /* one per line */
171 struct tog_view *view;
172 struct got_object_id *commit_id;
176 struct tog_blame_thread_args {
178 struct got_repository *repo;
179 struct tog_blame_cb_args *cb_args;
186 struct tog_blame_line *lines;
189 struct tog_blame_thread_args thread_args;
190 struct tog_blame_cb_args cb_args;
194 struct tog_blame_view_state {
195 int first_displayed_line;
196 int last_displayed_line;
201 struct got_object_id_queue blamed_commits;
202 struct got_object_qid *blamed_commit;
204 struct got_repository *repo;
205 struct got_reflist_head *refs;
206 struct got_object_id *commit_id;
207 struct tog_blame blame;
210 struct tog_parent_tree {
211 TAILQ_ENTRY(tog_parent_tree) entry;
212 struct got_tree_object *tree;
213 struct got_tree_entry *first_displayed_entry;
214 struct got_tree_entry *selected_entry;
218 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
220 struct tog_tree_view_state {
222 struct got_tree_object *root;
223 struct got_tree_object *tree;
224 const struct got_tree_entries *entries;
225 struct got_tree_entry *first_displayed_entry;
226 struct got_tree_entry *last_displayed_entry;
227 struct got_tree_entry *selected_entry;
228 int ndisplayed, selected, show_ids;
229 struct tog_parent_trees parents;
230 struct got_object_id *commit_id;
231 struct got_repository *repo;
232 struct got_reflist_head *refs;
236 * We implement two types of views: parent views and child views.
238 * The 'Tab' key switches between a parent view and its child view.
239 * Child views are shown side-by-side to their parent view, provided
240 * there is enough screen estate.
242 * When a new view is opened from within a parent view, this new view
243 * becomes a child view of the parent view, replacing any existing child.
245 * When a new view is opened from within a child view, this new view
246 * becomes a parent view which will obscure the views below until the
247 * user quits the new parent view by typing 'q'.
249 * This list of views contains parent views only.
250 * Child views are only pointed to by their parent view.
252 TAILQ_HEAD(tog_view_list_head, tog_view);
255 TAILQ_ENTRY(tog_view) entry;
258 int nlines, ncols, begin_y, begin_x;
259 int lines, cols; /* copies of LINES and COLS */
261 struct tog_view *parent;
262 struct tog_view *child;
265 /* type-specific state */
266 enum tog_view_type type;
268 struct tog_diff_view_state diff;
269 struct tog_log_view_state log;
270 struct tog_blame_view_state blame;
271 struct tog_tree_view_state tree;
274 const struct got_error *(*show)(struct tog_view *);
275 const struct got_error *(*input)(struct tog_view **,
276 struct tog_view **, struct tog_view**, struct tog_view *, int);
277 const struct got_error *(*close)(struct tog_view *);
279 const struct got_error *(*search_start)(struct tog_view *);
280 const struct got_error *(*search_next)(struct tog_view *);
282 #define TOG_SEARCH_FORWARD 1
283 #define TOG_SEARCH_BACKWARD 2
284 int search_next_done;
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
312 static const struct got_error *open_tree_view(struct tog_view *,
313 struct got_tree_object *, struct got_object_id *,
314 struct got_reflist_head *, struct got_repository *);
315 static const struct got_error *show_tree_view(struct tog_view *);
316 static const struct got_error *input_tree_view(struct tog_view **,
317 struct tog_view **, struct tog_view **, struct tog_view *, int);
318 static const struct got_error *close_tree_view(struct tog_view *);
320 static volatile sig_atomic_t tog_sigwinch_received;
323 tog_sigwinch(int signo)
325 tog_sigwinch_received = 1;
328 static const struct got_error *
329 view_close(struct tog_view *view)
331 const struct got_error *err = NULL;
334 view_close(view->child);
338 err = view->close(view);
340 del_panel(view->panel);
342 delwin(view->window);
347 static struct tog_view *
348 view_open(int nlines, int ncols, int begin_y, int begin_x,
349 enum tog_view_type type)
351 struct tog_view *view = calloc(1, sizeof(*view));
359 view->nlines = nlines ? nlines : LINES - begin_y;
360 view->ncols = ncols ? ncols : COLS - begin_x;
361 view->begin_y = begin_y;
362 view->begin_x = begin_x;
363 view->window = newwin(nlines, ncols, begin_y, begin_x);
364 if (view->window == NULL) {
368 view->panel = new_panel(view->window);
369 if (view->panel == NULL ||
370 set_panel_userptr(view->panel, view) != OK) {
375 keypad(view->window, TRUE);
380 view_split_begin_x(int begin_x)
382 if (begin_x > 0 || COLS < 120)
384 return (COLS - MAX(COLS / 2, 80));
387 static const struct got_error *view_resize(struct tog_view *);
389 static const struct got_error *
390 view_splitscreen(struct tog_view *view)
392 const struct got_error *err = NULL;
395 view->begin_x = view_split_begin_x(0);
396 view->nlines = LINES;
397 view->ncols = COLS - view->begin_x;
400 err = view_resize(view);
404 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
405 return got_error_from_errno("mvwin");
410 static const struct got_error *
411 view_fullscreen(struct tog_view *view)
413 const struct got_error *err = NULL;
417 view->nlines = LINES;
421 err = view_resize(view);
425 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
426 return got_error_from_errno("mvwin");
432 view_is_parent_view(struct tog_view *view)
434 return view->parent == NULL;
437 static const struct got_error *
438 view_resize(struct tog_view *view)
442 if (view->lines > LINES)
443 nlines = view->nlines - (view->lines - LINES);
445 nlines = view->nlines + (LINES - view->lines);
447 if (view->cols > COLS)
448 ncols = view->ncols - (view->cols - COLS);
450 ncols = view->ncols + (COLS - view->cols);
452 if (wresize(view->window, nlines, ncols) == ERR)
453 return got_error_from_errno("wresize");
454 if (replace_panel(view->panel, view->window) == ERR)
455 return got_error_from_errno("replace_panel");
456 wclear(view->window);
458 view->nlines = nlines;
464 view->child->begin_x = view_split_begin_x(view->begin_x);
465 if (view->child->begin_x == 0) {
466 view_fullscreen(view->child);
467 if (view->child->focussed)
468 show_panel(view->child->panel);
470 show_panel(view->panel);
472 view_splitscreen(view->child);
473 show_panel(view->child->panel);
480 static const struct got_error *
481 view_close_child(struct tog_view *view)
483 const struct got_error *err = NULL;
485 if (view->child == NULL)
488 err = view_close(view->child);
493 static const struct got_error *
494 view_set_child(struct tog_view *view, struct tog_view *child)
496 const struct got_error *err = NULL;
499 child->parent = view;
504 view_is_splitscreen(struct tog_view *view)
506 return view->begin_x > 0;
515 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
516 cols = 80; /* Default */
522 resize_term(lines, cols);
525 static const struct got_error *
526 view_input(struct tog_view **new, struct tog_view **dead,
527 struct tog_view **focus, int *done, struct tog_view *view,
528 struct tog_view_list_head *views)
530 const struct got_error *err = NULL;
538 if (view->searching && !view->search_next_done) {
539 errcode = pthread_mutex_unlock(&tog_mutex);
541 return got_error_set_errno(errcode,
542 "pthread_mutex_unlock");
544 errcode = pthread_mutex_lock(&tog_mutex);
546 return got_error_set_errno(errcode,
547 "pthread_mutex_lock");
548 view->search_next(view);
552 nodelay(stdscr, FALSE);
553 /* Allow threads to make progress while we are waiting for input. */
554 errcode = pthread_mutex_unlock(&tog_mutex);
556 return got_error_set_errno(errcode, "pthread_mutex_unlock");
557 ch = wgetch(view->window);
558 errcode = pthread_mutex_lock(&tog_mutex);
560 return got_error_set_errno(errcode, "pthread_mutex_lock");
561 nodelay(stdscr, TRUE);
563 if (tog_sigwinch_received) {
565 tog_sigwinch_received = 0;
566 TAILQ_FOREACH(v, views, entry) {
567 err = view_resize(v);
570 err = v->input(new, dead, focus, v, KEY_RESIZE);
581 *focus = view->child;
582 view->child_focussed = 1;
583 } else if (view->parent) {
584 *focus = view->parent;
585 view->parent->child_focussed = 0;
589 err = view->input(new, dead, focus, view, ch);
596 if (view_is_parent_view(view)) {
597 if (view->child == NULL)
599 if (view_is_splitscreen(view->child)) {
600 *focus = view->child;
601 view->child_focussed = 1;
602 err = view_fullscreen(view->child);
604 err = view_splitscreen(view->child);
607 err = view->child->input(new, dead, focus,
608 view->child, KEY_RESIZE);
610 if (view_is_splitscreen(view)) {
612 view->parent->child_focussed = 1;
613 err = view_fullscreen(view);
615 err = view_splitscreen(view);
619 err = view->input(new, dead, focus, view,
626 if (view->search_start)
627 view->search_start(view);
629 err = view->input(new, dead, focus, view, ch);
633 if (view->search_next && view->searching) {
634 view->searching = (ch == 'n' ?
635 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
636 view->search_next_done = 0;
637 view->search_next(view);
639 err = view->input(new, dead, focus, view, ch);
642 err = view->input(new, dead, focus, view, ch);
650 view_vborder(struct tog_view *view)
653 struct tog_view *view_above;
656 return view_vborder(view->parent);
658 panel = panel_above(view->panel);
662 view_above = panel_userptr(panel);
663 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
664 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
668 view_needs_focus_indication(struct tog_view *view)
670 if (view_is_parent_view(view)) {
671 if (view->child == NULL || view->child_focussed)
673 if (!view_is_splitscreen(view->child))
675 } else if (!view_is_splitscreen(view))
678 return view->focussed;
681 static const struct got_error *
682 view_loop(struct tog_view *view)
684 const struct got_error *err = NULL;
685 struct tog_view_list_head views;
686 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
687 int fast_refresh = 10;
688 int done = 0, errcode;
690 errcode = pthread_mutex_lock(&tog_mutex);
692 return got_error_set_errno(errcode, "pthread_mutex_lock");
695 TAILQ_INSERT_HEAD(&views, view, entry);
699 err = view->show(view);
704 while (!TAILQ_EMPTY(&views) && !done) {
705 /* Refresh fast during initialization, then become slower. */
706 if (fast_refresh && fast_refresh-- == 0)
707 halfdelay(10); /* switch to once per second */
709 err = view_input(&new_view, &dead_view, &focus_view, &done,
714 struct tog_view *prev = NULL;
716 if (view_is_parent_view(dead_view))
717 prev = TAILQ_PREV(dead_view,
718 tog_view_list_head, entry);
719 else if (view->parent != dead_view)
722 if (dead_view->parent)
723 dead_view->parent->child = NULL;
725 TAILQ_REMOVE(&views, dead_view, entry);
727 err = view_close(dead_view);
728 if (err || dead_view == main_view)
731 if (view == dead_view) {
736 else if (!TAILQ_EMPTY(&views))
737 view = TAILQ_LAST(&views,
742 if (view->child && view->child_focussed)
743 focus_view = view->child;
750 struct tog_view *v, *t;
751 /* Only allow one parent view per type. */
752 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
753 if (v->type != new_view->type)
755 TAILQ_REMOVE(&views, v, entry);
761 TAILQ_INSERT_TAIL(&views, new_view, entry);
763 if (focus_view == NULL)
764 focus_view = new_view;
767 show_panel(focus_view->panel);
770 focus_view->focussed = 1;
773 show_panel(new_view->panel);
774 if (view->child && view_is_splitscreen(view->child))
775 show_panel(view->child->panel);
778 if (focus_view == NULL) {
780 show_panel(view->panel);
781 if (view->child && view_is_splitscreen(view->child))
782 show_panel(view->child->panel);
786 err = view->parent->show(view->parent);
790 err = view->show(view);
794 err = view->child->show(view->child);
803 while (!TAILQ_EMPTY(&views)) {
804 view = TAILQ_FIRST(&views);
805 TAILQ_REMOVE(&views, view, entry);
809 errcode = pthread_mutex_unlock(&tog_mutex);
811 return got_error_set_errno(errcode, "pthread_mutex_unlock");
821 "usage: %s log [-c commit] [-r repository-path] [path]\n",
826 /* Create newly allocated wide-character string equivalent to a byte string. */
827 static const struct got_error *
828 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
831 const struct got_error *err = NULL;
834 *wlen = mbstowcs(NULL, s, 0);
835 if (*wlen == (size_t)-1) {
838 return got_error_from_errno("mbstowcs");
840 /* byte string invalid in current encoding; try to "fix" it */
841 err = got_mbsavis(&vis, &vislen, s);
844 *wlen = mbstowcs(NULL, vis, 0);
845 if (*wlen == (size_t)-1) {
846 err = got_error_from_errno("mbstowcs"); /* give up */
851 *ws = calloc(*wlen + 1, sizeof(*ws));
853 err = got_error_from_errno("calloc");
857 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
858 err = got_error_from_errno("mbstowcs");
869 /* Format a line for display, ensuring that it won't overflow a width limit. */
870 static const struct got_error *
871 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
873 const struct got_error *err = NULL;
875 wchar_t *wline = NULL;
882 err = mbs2ws(&wline, &wlen, line);
887 while (i < wlen && cols < wlimit) {
888 int width = wcwidth(wline[i]);
895 if (cols + width <= wlimit)
900 if (wline[i] == L'\t')
901 cols += TABSIZE - ((cols + 1) % TABSIZE);
905 err = got_error_from_errno("wcwidth");
920 static const struct got_error*
921 build_refs_str(char **refs_str, struct got_reflist_head *refs,
922 struct got_object_id *id)
924 static const struct got_error *err = NULL;
925 struct got_reflist_entry *re;
931 SIMPLEQ_FOREACH(re, refs, entry) {
932 if (got_object_id_cmp(re->id, id) != 0)
934 name = got_ref_get_name(re->ref);
935 if (strcmp(name, GOT_REF_HEAD) == 0)
937 if (strncmp(name, "refs/", 5) == 0)
939 if (strncmp(name, "got/", 4) == 0)
941 if (strncmp(name, "heads/", 6) == 0)
943 if (strncmp(name, "remotes/", 8) == 0)
946 if (asprintf(refs_str, "%s%s%s", s ? s : "",
947 s ? ", " : "", name) == -1) {
948 err = got_error_from_errno("asprintf");
959 static const struct got_error *
960 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
962 char *smallerthan, *at;
964 smallerthan = strchr(author, '<');
965 if (smallerthan && smallerthan[1] != '\0')
966 author = smallerthan + 1;
967 at = strchr(author, '@');
970 return format_line(wauthor, author_width, author, limit);
973 static const struct got_error *
974 draw_commit(struct tog_view *view, struct got_commit_object *commit,
975 struct got_object_id *id, struct got_reflist_head *refs,
976 int author_display_cols)
978 const struct got_error *err = NULL;
979 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
980 char *logmsg0 = NULL, *logmsg = NULL;
982 wchar_t *wlogmsg = NULL, *wauthor = NULL;
983 int author_width, logmsg_width;
984 char *newline, *line = NULL;
986 static const size_t date_display_cols = 9;
987 const int avail = view->ncols;
989 time_t committer_time;
991 committer_time = got_object_commit_get_committer_time(commit);
992 if (localtime_r(&committer_time, &tm) == NULL)
993 return got_error_from_errno("localtime_r");
994 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
996 return got_error(GOT_ERR_NO_SPACE);
998 if (avail < date_display_cols)
999 limit = MIN(sizeof(datebuf) - 1, avail);
1001 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1002 waddnstr(view->window, datebuf, limit);
1007 author = strdup(got_object_commit_get_author(commit));
1008 if (author == NULL) {
1009 err = got_error_from_errno("strdup");
1012 err = format_author(&wauthor, &author_width, author, avail - col);
1015 waddwstr(view->window, wauthor);
1016 col += author_width;
1017 while (col <= avail && author_width < author_display_cols + 2) {
1018 waddch(view->window, ' ');
1025 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1026 if (logmsg0 == NULL) {
1027 err = got_error_from_errno("strdup");
1031 while (*logmsg == '\n')
1033 newline = strchr(logmsg, '\n');
1036 limit = avail - col;
1037 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1040 waddwstr(view->window, wlogmsg);
1041 col += logmsg_width;
1042 while (col <= avail) {
1043 waddch(view->window, ' ');
1055 static struct commit_queue_entry *
1056 alloc_commit_queue_entry(struct got_commit_object *commit,
1057 struct got_object_id *id)
1059 struct commit_queue_entry *entry;
1061 entry = calloc(1, sizeof(*entry));
1066 entry->commit = commit;
1071 pop_commit(struct commit_queue *commits)
1073 struct commit_queue_entry *entry;
1075 entry = TAILQ_FIRST(&commits->head);
1076 TAILQ_REMOVE(&commits->head, entry, entry);
1077 got_object_commit_close(entry->commit);
1078 commits->ncommits--;
1079 /* Don't free entry->id! It is owned by the commit graph. */
1084 free_commits(struct commit_queue *commits)
1086 while (!TAILQ_EMPTY(&commits->head))
1087 pop_commit(commits);
1090 static const struct got_error *
1091 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1092 int minqueue, struct got_repository *repo, const char *path)
1094 const struct got_error *err = NULL;
1098 * We keep all commits open throughout the lifetime of the log
1099 * view in order to avoid having to re-fetch commits from disk
1100 * while updating the display.
1102 while (nqueued < minqueue) {
1103 struct got_object_id *id;
1104 struct got_commit_object *commit;
1105 struct commit_queue_entry *entry;
1108 err = got_commit_graph_iter_next(&id, graph);
1110 if (err->code != GOT_ERR_ITER_NEED_MORE)
1112 err = got_commit_graph_fetch_commits(graph,
1122 err = got_object_open_as_commit(&commit, repo, id);
1125 entry = alloc_commit_queue_entry(commit, id);
1126 if (entry == NULL) {
1127 err = got_error_from_errno("alloc_commit_queue_entry");
1131 errcode = pthread_mutex_lock(&tog_mutex);
1133 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1137 entry->idx = commits->ncommits;
1138 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1140 commits->ncommits++;
1142 errcode = pthread_mutex_unlock(&tog_mutex);
1143 if (errcode && err == NULL)
1144 err = got_error_set_errno(errcode,
1145 "pthread_mutex_unlock");
1151 static const struct got_error *
1152 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1153 struct got_repository *repo)
1155 const struct got_error *err = NULL;
1156 struct got_reference *head_ref;
1160 err = got_ref_open(&head_ref, repo, branch_name, 0);
1164 err = got_ref_resolve(head_id, repo, head_ref);
1165 got_ref_close(head_ref);
1174 static const struct got_error *
1175 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1176 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1177 struct commit_queue *commits, int selected_idx, int limit,
1178 struct got_reflist_head *refs, const char *path, int commits_needed)
1180 const struct got_error *err = NULL;
1181 struct commit_queue_entry *entry;
1183 int ncommits, author_cols = 10;
1184 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1185 char *refs_str = NULL;
1191 if (ncommits == selected_idx) {
1195 entry = TAILQ_NEXT(entry, entry);
1199 if (*selected && !(view->searching && view->search_next_done == 0)) {
1200 err = got_object_id_str(&id_str, (*selected)->id);
1204 err = build_refs_str(&refs_str, refs, (*selected)->id);
1210 if (commits_needed == 0)
1211 halfdelay(10); /* disable fast refresh */
1213 if (asprintf(&ncommits_str, " [%d/%d] %s",
1214 entry ? entry->idx + 1 : 0, commits->ncommits,
1215 commits_needed > 0 ?
1216 (view->searching && view->search_next_done == 0
1217 ? "searching..." : "loading... ") :
1218 (refs_str ? refs_str : "")) == -1) {
1219 err = got_error_from_errno("asprintf");
1223 if (path && strcmp(path, "/") != 0) {
1224 if (asprintf(&header, "commit %s %s%s",
1225 id_str ? id_str : "........................................",
1226 path, ncommits_str) == -1) {
1227 err = got_error_from_errno("asprintf");
1231 } else if (asprintf(&header, "commit %s%s",
1232 id_str ? id_str : "........................................",
1233 ncommits_str) == -1) {
1234 err = got_error_from_errno("asprintf");
1238 err = format_line(&wline, &width, header, view->ncols);
1242 werase(view->window);
1244 if (view_needs_focus_indication(view))
1245 wstandout(view->window);
1246 waddwstr(view->window, wline);
1247 while (width < view->ncols) {
1248 waddch(view->window, ' ');
1251 if (view_needs_focus_indication(view))
1252 wstandend(view->window);
1257 /* Grow author column size if necessary. */
1264 if (ncommits >= limit - 1)
1266 author = strdup(got_object_commit_get_author(entry->commit));
1267 if (author == NULL) {
1268 err = got_error_from_errno("strdup");
1271 err = format_author(&wauthor, &width, author, COLS);
1272 if (author_cols < width)
1273 author_cols = width;
1276 entry = TAILQ_NEXT(entry, entry);
1283 if (ncommits >= limit - 1)
1285 if (ncommits == selected_idx)
1286 wstandout(view->window);
1287 err = draw_commit(view, entry->commit, entry->id, refs,
1289 if (ncommits == selected_idx)
1290 wstandend(view->window);
1295 entry = TAILQ_NEXT(entry, entry);
1308 scroll_up(struct tog_view *view,
1309 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1310 struct commit_queue *commits)
1312 struct commit_queue_entry *entry;
1315 entry = TAILQ_FIRST(&commits->head);
1316 if (*first_displayed_entry == entry)
1319 entry = *first_displayed_entry;
1320 while (entry && nscrolled < maxscroll) {
1321 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1323 *first_displayed_entry = entry;
1329 static const struct got_error *
1330 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1331 pthread_cond_t *need_commits)
1336 halfdelay(1); /* fast refresh while loading commits */
1338 while (*commits_needed > 0) {
1342 /* Wake the log thread. */
1343 errcode = pthread_cond_signal(need_commits);
1345 return got_error_set_errno(errcode,
1346 "pthread_cond_signal");
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1349 return got_error_set_errno(errcode,
1350 "pthread_mutex_unlock");
1352 errcode = pthread_mutex_lock(&tog_mutex);
1354 return got_error_set_errno(errcode,
1355 "pthread_mutex_lock");
1357 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1359 * Thread is not done yet; lose a key press
1360 * and let the user retry... this way the GUI
1361 * remains interactive while logging deep paths
1362 * with few commits in history.
1371 static const struct got_error *
1372 scroll_down(struct tog_view *view,
1373 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1374 struct commit_queue_entry **last_displayed_entry,
1375 struct commit_queue *commits, int *log_complete, int *commits_needed,
1376 pthread_cond_t *need_commits)
1378 const struct got_error *err = NULL;
1379 struct commit_queue_entry *pentry;
1382 if (*last_displayed_entry == NULL)
1385 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1386 if (pentry == NULL && !*log_complete) {
1388 * Ask the log thread for required amount of commits
1389 * plus some amount of pre-fetching.
1391 (*commits_needed) += maxscroll + 20;
1392 err = trigger_log_thread(0, commits_needed, log_complete,
1399 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1403 *last_displayed_entry = pentry;
1405 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1408 *first_displayed_entry = pentry;
1409 } while (++nscrolled < maxscroll);
1414 static const struct got_error *
1415 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1416 struct got_commit_object *commit, struct got_object_id *commit_id,
1417 struct tog_view *log_view, struct got_reflist_head *refs,
1418 struct got_repository *repo)
1420 const struct got_error *err;
1421 struct got_object_qid *parent_id;
1422 struct tog_view *diff_view;
1424 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1425 if (diff_view == NULL)
1426 return got_error_from_errno("view_open");
1428 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1429 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1430 commit_id, log_view, refs, repo);
1432 *new_view = diff_view;
1436 static const struct got_error *
1437 tree_view_visit_subtree(struct got_tree_object *subtree,
1438 struct tog_tree_view_state *s)
1440 struct tog_parent_tree *parent;
1442 parent = calloc(1, sizeof(*parent));
1444 return got_error_from_errno("calloc");
1446 parent->tree = s->tree;
1447 parent->first_displayed_entry = s->first_displayed_entry;
1448 parent->selected_entry = s->selected_entry;
1449 parent->selected = s->selected;
1450 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1452 s->entries = got_object_tree_get_entries(s->tree);
1454 s->first_displayed_entry = NULL;
1459 static const struct got_error *
1460 browse_commit_tree(struct tog_view **new_view, int begin_x,
1461 struct commit_queue_entry *entry, const char *path,
1462 struct got_reflist_head *refs, struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree;
1466 struct got_tree_entry *te;
1467 struct tog_tree_view_state *s;
1468 struct tog_view *tree_view;
1469 char *slash, *subpath = NULL;
1472 err = got_object_open_as_tree(&tree, repo,
1473 got_object_commit_get_tree_id(entry->commit));
1477 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1478 if (tree_view == NULL)
1479 return got_error_from_errno("view_open");
1481 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1483 got_object_tree_close(tree);
1486 s = &tree_view->state.tree;
1488 *new_view = tree_view;
1490 /* Walk the path and open corresponding tree objects. */
1493 struct got_object_id *tree_id;
1498 /* Ensure the correct subtree entry is selected. */
1499 slash = strchr(p, '/');
1501 slash = strchr(p, '\0');
1502 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1503 if (strncmp(p, te->name, slash - p) == 0) {
1504 s->selected_entry = te;
1509 if (s->selected_entry == NULL) {
1510 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1513 if (s->tree != s->root)
1514 s->selected++; /* skip '..' */
1516 if (!S_ISDIR(s->selected_entry->mode)) {
1517 /* Jump to this file's entry. */
1518 s->first_displayed_entry = s->selected_entry;
1523 slash = strchr(p, '/');
1525 subpath = strndup(path, slash - path);
1527 subpath = strdup(path);
1528 if (subpath == NULL) {
1529 err = got_error_from_errno("strdup");
1533 err = got_object_id_by_path(&tree_id, repo, entry->id,
1538 err = got_object_open_as_tree(&tree, repo, tree_id);
1543 err = tree_view_visit_subtree(tree, s);
1545 got_object_tree_close(tree);
1560 log_thread(void *arg)
1562 const struct got_error *err = NULL;
1564 struct tog_log_thread_args *a = arg;
1567 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1571 while (!done && !err) {
1572 err = queue_commits(a->graph, a->commits, 1, a->repo,
1575 if (err->code != GOT_ERR_ITER_COMPLETED)
1579 } else if (a->commits_needed > 0)
1580 a->commits_needed--;
1582 errcode = pthread_mutex_lock(&tog_mutex);
1584 err = got_error_set_errno(errcode,
1585 "pthread_mutex_lock");
1587 } else if (*a->quit)
1589 else if (*a->first_displayed_entry == NULL) {
1590 *a->first_displayed_entry =
1591 TAILQ_FIRST(&a->commits->head);
1592 *a->selected_entry = *a->first_displayed_entry;
1596 a->commits_needed = 0;
1597 else if (a->commits_needed == 0) {
1598 errcode = pthread_cond_wait(&a->need_commits,
1601 err = got_error_set_errno(errcode,
1602 "pthread_cond_wait");
1605 errcode = pthread_mutex_unlock(&tog_mutex);
1606 if (errcode && err == NULL)
1607 err = got_error_set_errno(errcode,
1608 "pthread_mutex_unlock");
1610 a->log_complete = 1;
1614 static const struct got_error *
1615 stop_log_thread(struct tog_log_view_state *s)
1617 const struct got_error *err = NULL;
1622 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1624 return got_error_set_errno(errcode,
1625 "pthread_cond_signal");
1626 errcode = pthread_mutex_unlock(&tog_mutex);
1628 return got_error_set_errno(errcode,
1629 "pthread_mutex_unlock");
1630 errcode = pthread_join(s->thread, (void **)&err);
1632 return got_error_set_errno(errcode, "pthread_join");
1633 errcode = pthread_mutex_lock(&tog_mutex);
1635 return got_error_set_errno(errcode,
1636 "pthread_mutex_lock");
1640 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1641 if (errcode && err == NULL)
1642 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1644 if (s->thread_args.repo) {
1645 got_repo_close(s->thread_args.repo);
1646 s->thread_args.repo = NULL;
1649 if (s->thread_args.graph) {
1650 got_commit_graph_close(s->thread_args.graph);
1651 s->thread_args.graph = NULL;
1657 static const struct got_error *
1658 close_log_view(struct tog_view *view)
1660 const struct got_error *err = NULL;
1661 struct tog_log_view_state *s = &view->state.log;
1663 err = stop_log_thread(s);
1664 free_commits(&s->commits);
1665 free(s->in_repo_path);
1666 s->in_repo_path = NULL;
1672 static const struct got_error *
1673 search_start_log_view(struct tog_view *view)
1675 struct tog_log_view_state *s = &view->state.log;
1679 if (view->nlines < 1)
1682 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1683 view->begin_x, "/");
1684 wclrtoeol(view->window);
1688 ret = wgetnstr(view->window, pattern, sizeof(pattern));
1694 if (view->searching) {
1696 view->searching = 0;
1699 s->matched_entry = NULL;
1700 if (regcomp(&s->regex, pattern,
1701 REG_EXTENDED | REG_ICASE | REG_NOSUB | REG_NEWLINE) == 0) {
1702 view->searching = TOG_SEARCH_FORWARD;
1703 view->search_next_done = 0;
1704 view->search_next(view);
1711 match_commit(struct got_commit_object *commit, regex_t *regex)
1713 regmatch_t regmatch;
1715 if (regexec(regex, got_object_commit_get_author(commit), 1,
1716 ®match, 0) == 0 ||
1717 regexec(regex, got_object_commit_get_committer(commit), 1,
1718 ®match, 0) == 0 ||
1719 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1726 static const struct got_error *
1727 search_next_log_view(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1731 struct commit_queue_entry *entry;
1733 if (!view->searching) {
1734 view->search_next_done = 1;
1738 if (s->matched_entry) {
1739 if (view->searching == TOG_SEARCH_FORWARD)
1740 entry = TAILQ_NEXT(s->selected_entry, entry);
1742 entry = TAILQ_PREV(s->selected_entry,
1743 commit_queue_head, entry);
1745 if (view->searching == TOG_SEARCH_FORWARD)
1746 entry = TAILQ_FIRST(&s->commits.head);
1748 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1752 if (entry == NULL) {
1753 if (s->thread_args.log_complete) {
1754 if (s->matched_entry == NULL) {
1755 view->search_next_done = 1;
1758 if (view->searching == TOG_SEARCH_FORWARD)
1759 entry = TAILQ_FIRST(&s->commits.head);
1761 entry = TAILQ_LAST(&s->commits.head,
1764 s->thread_args.commits_needed = 1;
1765 return trigger_log_thread(0,
1766 &s->thread_args.commits_needed,
1767 &s->thread_args.log_complete,
1768 &s->thread_args.need_commits);
1772 if (match_commit(entry->commit, &s->regex)) {
1773 view->search_next_done = 1;
1776 if (view->searching == TOG_SEARCH_FORWARD)
1777 entry = TAILQ_NEXT(entry, entry);
1779 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1783 int cur = s->selected_entry->idx;
1784 s->matched_entry = entry;
1785 while (cur < s->matched_entry->idx) {
1786 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1791 while (cur > s->matched_entry->idx) {
1792 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1803 static const struct got_error *
1804 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1805 struct got_reflist_head *refs, struct got_repository *repo,
1806 const char *path, int check_disk)
1808 const struct got_error *err = NULL;
1809 struct tog_log_view_state *s = &view->state.log;
1810 struct got_repository *thread_repo = NULL;
1811 struct got_commit_graph *thread_graph = NULL;
1814 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1818 /* The commit queue only contains commits being displayed. */
1819 TAILQ_INIT(&s->commits.head);
1820 s->commits.ncommits = 0;
1824 s->start_id = got_object_id_dup(start_id);
1825 if (s->start_id == NULL) {
1826 err = got_error_from_errno("got_object_id_dup");
1830 view->show = show_log_view;
1831 view->input = input_log_view;
1832 view->close = close_log_view;
1833 view->search_start = search_start_log_view;
1834 view->search_next = search_next_log_view;
1836 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1839 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1844 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1846 err = got_error_set_errno(errcode, "pthread_cond_init");
1850 s->thread_args.commits_needed = view->nlines;
1851 s->thread_args.graph = thread_graph;
1852 s->thread_args.commits = &s->commits;
1853 s->thread_args.in_repo_path = s->in_repo_path;
1854 s->thread_args.start_id = s->start_id;
1855 s->thread_args.repo = thread_repo;
1856 s->thread_args.log_complete = 0;
1857 s->thread_args.quit = &s->quit;
1858 s->thread_args.view = view;
1859 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1860 s->thread_args.selected_entry = &s->selected_entry;
1863 close_log_view(view);
1867 static const struct got_error *
1868 show_log_view(struct tog_view *view)
1870 struct tog_log_view_state *s = &view->state.log;
1872 if (s->thread == NULL) {
1873 int errcode = pthread_create(&s->thread, NULL, log_thread,
1876 return got_error_set_errno(errcode, "pthread_create");
1879 return draw_commits(view, &s->last_displayed_entry,
1880 &s->selected_entry, s->first_displayed_entry,
1881 &s->commits, s->selected, view->nlines, s->refs,
1882 s->in_repo_path, s->thread_args.commits_needed);
1885 static const struct got_error *
1886 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1887 struct tog_view **focus_view, struct tog_view *view, int ch)
1889 const struct got_error *err = NULL;
1890 struct tog_log_view_state *s = &view->state.log;
1892 struct tog_view *diff_view = NULL, *tree_view = NULL;
1903 if (s->first_displayed_entry == NULL)
1905 if (s->selected > 0)
1908 scroll_up(view, &s->first_displayed_entry, 1,
1913 if (s->first_displayed_entry == NULL)
1915 if (TAILQ_FIRST(&s->commits.head) ==
1916 s->first_displayed_entry) {
1920 scroll_up(view, &s->first_displayed_entry,
1921 view->nlines, &s->commits);
1927 if (s->first_displayed_entry == NULL)
1929 if (s->selected < MIN(view->nlines - 2,
1930 s->commits.ncommits - 1)) {
1934 err = scroll_down(view, &s->first_displayed_entry, 1,
1935 &s->last_displayed_entry, &s->commits,
1936 &s->thread_args.log_complete,
1937 &s->thread_args.commits_needed,
1938 &s->thread_args.need_commits);
1942 struct commit_queue_entry *first;
1943 first = s->first_displayed_entry;
1946 err = scroll_down(view, &s->first_displayed_entry,
1947 view->nlines, &s->last_displayed_entry,
1948 &s->commits, &s->thread_args.log_complete,
1949 &s->thread_args.commits_needed,
1950 &s->thread_args.need_commits);
1951 if (first == s->first_displayed_entry &&
1952 s->selected < MIN(view->nlines - 2,
1953 s->commits.ncommits - 1)) {
1954 /* can't scroll further down */
1955 s->selected = MIN(view->nlines - 2,
1956 s->commits.ncommits - 1);
1962 if (s->selected > view->nlines - 2)
1963 s->selected = view->nlines - 2;
1964 if (s->selected > s->commits.ncommits - 1)
1965 s->selected = s->commits.ncommits - 1;
1970 if (s->selected_entry == NULL)
1972 if (view_is_parent_view(view))
1973 begin_x = view_split_begin_x(view->begin_x);
1974 err = open_diff_view_for_commit(&diff_view, begin_x,
1975 s->selected_entry->commit, s->selected_entry->id,
1976 view, s->refs, s->repo);
1979 if (view_is_parent_view(view)) {
1980 err = view_close_child(view);
1983 err = view_set_child(view, diff_view);
1985 view_close(diff_view);
1988 *focus_view = diff_view;
1989 view->child_focussed = 1;
1991 *new_view = diff_view;
1994 if (s->selected_entry == NULL)
1996 if (view_is_parent_view(view))
1997 begin_x = view_split_begin_x(view->begin_x);
1998 err = browse_commit_tree(&tree_view, begin_x,
1999 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2002 if (view_is_parent_view(view)) {
2003 err = view_close_child(view);
2006 err = view_set_child(view, tree_view);
2008 view_close(tree_view);
2011 *focus_view = tree_view;
2012 view->child_focussed = 1;
2014 *new_view = tree_view;
2017 if (strcmp(s->in_repo_path, "/") == 0)
2019 parent_path = dirname(s->in_repo_path);
2020 if (parent_path && strcmp(parent_path, ".") != 0) {
2021 struct tog_view *lv;
2022 err = stop_log_thread(s);
2025 lv = view_open(view->nlines, view->ncols,
2026 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2028 return got_error_from_errno(
2030 err = open_log_view(lv, s->start_id, s->refs,
2031 s->repo, parent_path, 0);
2034 if (view_is_parent_view(view))
2037 view_set_child(view->parent, lv);
2050 static const struct got_error *
2051 apply_unveil(const char *repo_path, const char *worktree_path)
2053 const struct got_error *error;
2055 if (repo_path && unveil(repo_path, "r") != 0)
2056 return got_error_from_errno2("unveil", repo_path);
2058 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2059 return got_error_from_errno2("unveil", worktree_path);
2061 if (unveil("/tmp", "rwc") != 0)
2062 return got_error_from_errno2("unveil", "/tmp");
2064 error = got_privsep_unveil_exec_helpers();
2068 if (unveil(NULL, NULL) != 0)
2069 return got_error_from_errno("unveil");
2079 halfdelay(1); /* Do fast refresh while initial view is loading. */
2082 intrflush(stdscr, FALSE);
2083 keypad(stdscr, TRUE);
2085 signal(SIGWINCH, tog_sigwinch);
2088 static const struct got_error *
2089 cmd_log(int argc, char *argv[])
2091 const struct got_error *error;
2092 struct got_repository *repo = NULL;
2093 struct got_worktree *worktree = NULL;
2094 struct got_reflist_head refs;
2095 struct got_object_id *start_id = NULL;
2096 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2097 char *start_commit = NULL;
2099 struct tog_view *view;
2101 SIMPLEQ_INIT(&refs);
2104 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2109 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2112 start_commit = optarg;
2115 repo_path = realpath(optarg, NULL);
2116 if (repo_path == NULL)
2117 err(1, "-r option");
2128 cwd = getcwd(NULL, 0);
2130 error = got_error_from_errno("getcwd");
2133 error = got_worktree_open(&worktree, cwd);
2134 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2141 error = got_error_from_errno("strdup");
2144 } else if (argc == 1) {
2146 error = got_worktree_resolve_path(&path, worktree,
2151 path = strdup(argv[0]);
2153 error = got_error_from_errno("strdup");
2160 repo_path = worktree ?
2161 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2162 if (repo_path == NULL) {
2163 error = got_error_from_errno("strdup");
2169 error = got_repo_open(&repo, repo_path);
2173 error = apply_unveil(got_repo_get_path(repo),
2174 worktree ? got_worktree_get_root_path(worktree) : NULL);
2178 if (start_commit == NULL)
2179 error = get_head_commit_id(&start_id, worktree ?
2180 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2183 error = got_object_resolve_id_str(&start_id, repo,
2188 error = got_ref_list(&refs, repo);
2192 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2194 error = got_error_from_errno("view_open");
2197 error = open_log_view(view, start_id, &refs, repo, path, 1);
2200 error = view_loop(view);
2207 got_repo_close(repo);
2209 got_worktree_close(worktree);
2210 got_ref_list_free(&refs);
2218 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2224 parse_next_line(FILE *f, size_t *len)
2229 const char delim[3] = { '\0', '\0', '\0'};
2231 line = fparseln(f, &linelen, &lineno, delim, 0);
2237 static const struct got_error *
2238 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2239 int *last_displayed_line, int *eof, int max_lines,
2242 const struct got_error *err;
2243 int nlines = 0, nprinted = 0;
2250 werase(view->window);
2253 err = format_line(&wline, &width, header, view->ncols);
2258 if (view_needs_focus_indication(view))
2259 wstandout(view->window);
2260 waddwstr(view->window, wline);
2261 if (view_needs_focus_indication(view))
2262 wstandend(view->window);
2263 if (width < view->ncols - 1)
2264 waddch(view->window, '\n');
2272 while (nprinted < max_lines) {
2273 line = parse_next_line(f, &len);
2278 if (++nlines < *first_displayed_line) {
2283 err = format_line(&wline, &width, line, view->ncols);
2288 waddwstr(view->window, wline);
2289 if (width < view->ncols - 1)
2290 waddch(view->window, '\n');
2291 if (++nprinted == 1)
2292 *first_displayed_line = nlines;
2297 *last_displayed_line = nlines;
2302 while (nprinted < view->nlines) {
2303 waddch(view->window, '\n');
2307 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2312 wstandout(view->window);
2313 waddwstr(view->window, wline);
2314 wstandend(view->window);
2321 get_datestr(time_t *time, char *datebuf)
2323 char *p, *s = ctime_r(time, datebuf);
2324 p = strchr(s, '\n');
2330 static const struct got_error *
2331 write_commit_info(struct got_object_id *commit_id,
2332 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2334 const struct got_error *err = NULL;
2336 struct got_commit_object *commit;
2337 char *id_str = NULL;
2338 time_t committer_time;
2339 const char *author, *committer;
2340 char *refs_str = NULL;
2343 err = build_refs_str(&refs_str, refs, commit_id);
2348 err = got_object_open_as_commit(&commit, repo, commit_id);
2352 err = got_object_id_str(&id_str, commit_id);
2354 err = got_error_from_errno("got_object_id_str");
2358 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2359 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2360 err = got_error_from_errno("fprintf");
2363 if (fprintf(outfile, "from: %s\n",
2364 got_object_commit_get_author(commit)) < 0) {
2365 err = got_error_from_errno("fprintf");
2368 committer_time = got_object_commit_get_committer_time(commit);
2369 if (fprintf(outfile, "date: %s UTC\n",
2370 get_datestr(&committer_time, datebuf)) < 0) {
2371 err = got_error_from_errno("fprintf");
2374 author = got_object_commit_get_author(commit);
2375 committer = got_object_commit_get_committer(commit);
2376 if (strcmp(author, committer) != 0 &&
2377 fprintf(outfile, "via: %s\n", committer) < 0) {
2378 err = got_error_from_errno("fprintf");
2381 if (fprintf(outfile, "%s\n",
2382 got_object_commit_get_logmsg(commit)) < 0) {
2383 err = got_error_from_errno("fprintf");
2389 got_object_commit_close(commit);
2393 static const struct got_error *
2394 create_diff(struct tog_diff_view_state *s)
2396 const struct got_error *err = NULL;
2402 err = got_error_from_errno("got_opentemp");
2405 if (s->f && fclose(s->f) != 0) {
2406 err = got_error_from_errno("fclose");
2412 err = got_object_get_type(&obj_type, s->repo, s->id1);
2414 err = got_object_get_type(&obj_type, s->repo, s->id2);
2419 case GOT_OBJ_TYPE_BLOB:
2420 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2421 s->diff_context, s->repo, f);
2423 case GOT_OBJ_TYPE_TREE:
2424 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2425 s->diff_context, s->repo, f);
2427 case GOT_OBJ_TYPE_COMMIT: {
2428 const struct got_object_id_queue *parent_ids;
2429 struct got_object_qid *pid;
2430 struct got_commit_object *commit2;
2432 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2435 /* Show commit info if we're diffing to a parent/root commit. */
2437 write_commit_info(s->id2, s->refs, s->repo, f);
2439 parent_ids = got_object_commit_get_parent_ids(commit2);
2440 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2441 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2442 write_commit_info(s->id2, s->refs,
2448 got_object_commit_close(commit2);
2450 err = got_diff_objects_as_commits(s->id1, s->id2,
2451 s->diff_context, s->repo, f);
2455 err = got_error(GOT_ERR_OBJ_TYPE);
2459 if (f && fflush(f) != 0 && err == NULL)
2460 err = got_error_from_errno("fflush");
2465 diff_view_indicate_progress(struct tog_view *view)
2467 mvwaddstr(view->window, 0, 0, "diffing...");
2472 static const struct got_error *
2473 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2474 struct got_object_id *id2, struct tog_view *log_view,
2475 struct got_reflist_head *refs, struct got_repository *repo)
2477 const struct got_error *err;
2479 if (id1 != NULL && id2 != NULL) {
2481 err = got_object_get_type(&type1, repo, id1);
2484 err = got_object_get_type(&type2, repo, id2);
2489 return got_error(GOT_ERR_OBJ_TYPE);
2493 view->state.diff.id1 = got_object_id_dup(id1);
2494 if (view->state.diff.id1 == NULL)
2495 return got_error_from_errno("got_object_id_dup");
2497 view->state.diff.id1 = NULL;
2499 view->state.diff.id2 = got_object_id_dup(id2);
2500 if (view->state.diff.id2 == NULL) {
2501 free(view->state.diff.id1);
2502 view->state.diff.id1 = NULL;
2503 return got_error_from_errno("got_object_id_dup");
2505 view->state.diff.f = NULL;
2506 view->state.diff.first_displayed_line = 1;
2507 view->state.diff.last_displayed_line = view->nlines;
2508 view->state.diff.diff_context = 3;
2509 view->state.diff.log_view = log_view;
2510 view->state.diff.repo = repo;
2511 view->state.diff.refs = refs;
2513 if (log_view && view_is_splitscreen(view))
2514 show_log_view(log_view); /* draw vborder */
2515 diff_view_indicate_progress(view);
2517 err = create_diff(&view->state.diff);
2519 free(view->state.diff.id1);
2520 view->state.diff.id1 = NULL;
2521 free(view->state.diff.id2);
2522 view->state.diff.id2 = NULL;
2526 view->show = show_diff_view;
2527 view->input = input_diff_view;
2528 view->close = close_diff_view;
2533 static const struct got_error *
2534 close_diff_view(struct tog_view *view)
2536 const struct got_error *err = NULL;
2538 free(view->state.diff.id1);
2539 view->state.diff.id1 = NULL;
2540 free(view->state.diff.id2);
2541 view->state.diff.id2 = NULL;
2542 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2543 err = got_error_from_errno("fclose");
2547 static const struct got_error *
2548 show_diff_view(struct tog_view *view)
2550 const struct got_error *err;
2551 struct tog_diff_view_state *s = &view->state.diff;
2552 char *id_str1 = NULL, *id_str2, *header;
2555 err = got_object_id_str(&id_str1, s->id1);
2559 err = got_object_id_str(&id_str2, s->id2);
2563 if (asprintf(&header, "diff %s %s",
2564 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2565 err = got_error_from_errno("asprintf");
2573 return draw_file(view, s->f, &s->first_displayed_line,
2574 &s->last_displayed_line, &s->eof, view->nlines,
2578 static const struct got_error *
2579 set_selected_commit(struct tog_diff_view_state *s,
2580 struct commit_queue_entry *entry)
2582 const struct got_error *err;
2583 const struct got_object_id_queue *parent_ids;
2584 struct got_commit_object *selected_commit;
2585 struct got_object_qid *pid;
2588 s->id2 = got_object_id_dup(entry->id);
2590 return got_error_from_errno("got_object_id_dup");
2592 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2595 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2597 pid = SIMPLEQ_FIRST(parent_ids);
2598 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2599 got_object_commit_close(selected_commit);
2603 static const struct got_error *
2604 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2605 struct tog_view **focus_view, struct tog_view *view, int ch)
2607 const struct got_error *err = NULL;
2608 struct tog_diff_view_state *s = &view->state.diff;
2609 struct tog_log_view_state *ls;
2610 struct commit_queue_entry *entry;
2616 if (s->first_displayed_line > 1)
2617 s->first_displayed_line--;
2621 if (s->first_displayed_line == 1)
2624 while (i++ < view->nlines - 1 &&
2625 s->first_displayed_line > 1)
2626 s->first_displayed_line--;
2631 s->first_displayed_line++;
2639 while (!s->eof && i++ < view->nlines - 1) {
2641 line = parse_next_line(s->f, NULL);
2642 s->first_displayed_line++;
2648 if (s->diff_context > 0) {
2650 diff_view_indicate_progress(view);
2651 err = create_diff(s);
2655 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2657 diff_view_indicate_progress(view);
2658 err = create_diff(s);
2663 if (s->log_view == NULL)
2665 ls = &s->log_view->state.log;
2666 entry = TAILQ_PREV(ls->selected_entry,
2667 commit_queue_head, entry);
2671 err = input_log_view(NULL, NULL, NULL, s->log_view,
2676 err = set_selected_commit(s, entry);
2680 s->first_displayed_line = 1;
2681 s->last_displayed_line = view->nlines;
2683 diff_view_indicate_progress(view);
2684 err = create_diff(s);
2688 if (s->log_view == NULL)
2690 ls = &s->log_view->state.log;
2692 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2693 ls->thread_args.commits_needed++;
2695 /* Display "loading..." in log view. */
2696 show_log_view(s->log_view);
2700 err = trigger_log_thread(1 /* load_all */,
2701 &ls->thread_args.commits_needed,
2702 &ls->thread_args.log_complete,
2703 &ls->thread_args.need_commits);
2707 err = input_log_view(NULL, NULL, NULL, s->log_view,
2712 entry = TAILQ_NEXT(ls->selected_entry, entry);
2716 err = set_selected_commit(s, entry);
2720 s->first_displayed_line = 1;
2721 s->last_displayed_line = view->nlines;
2723 diff_view_indicate_progress(view);
2724 err = create_diff(s);
2733 static const struct got_error *
2734 cmd_diff(int argc, char *argv[])
2736 const struct got_error *error = NULL;
2737 struct got_repository *repo = NULL;
2738 struct got_reflist_head refs;
2739 struct got_object_id *id1 = NULL, *id2 = NULL;
2740 char *repo_path = NULL;
2741 char *id_str1 = NULL, *id_str2 = NULL;
2743 struct tog_view *view;
2745 SIMPLEQ_INIT(&refs);
2748 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2753 while ((ch = getopt(argc, argv, "")) != -1) {
2765 usage_diff(); /* TODO show local worktree changes */
2766 } else if (argc == 2) {
2767 repo_path = getcwd(NULL, 0);
2768 if (repo_path == NULL)
2769 return got_error_from_errno("getcwd");
2772 } else if (argc == 3) {
2773 repo_path = realpath(argv[0], NULL);
2774 if (repo_path == NULL)
2775 return got_error_from_errno2("realpath", argv[0]);
2783 error = got_repo_open(&repo, repo_path);
2787 error = apply_unveil(got_repo_get_path(repo), NULL);
2791 error = got_object_resolve_id_str(&id1, repo, id_str1);
2795 error = got_object_resolve_id_str(&id2, repo, id_str2);
2799 error = got_ref_list(&refs, repo);
2803 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2805 error = got_error_from_errno("view_open");
2808 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2811 error = view_loop(view);
2814 got_repo_close(repo);
2815 got_ref_list_free(&refs);
2823 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2828 struct tog_blame_line {
2830 struct got_object_id *id;
2833 static const struct got_error *
2834 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2835 const char *path, struct tog_blame_line *lines, int nlines,
2836 int blame_complete, int selected_line, int *first_displayed_line,
2837 int *last_displayed_line, int *eof, int max_lines)
2839 const struct got_error *err;
2840 int lineno = 0, nprinted = 0;
2845 struct tog_blame_line *blame_line;
2846 struct got_object_id *prev_id = NULL;
2849 err = got_object_id_str(&id_str, id);
2854 werase(view->window);
2856 if (asprintf(&line, "commit %s", id_str) == -1) {
2857 err = got_error_from_errno("asprintf");
2862 err = format_line(&wline, &width, line, view->ncols);
2865 if (view_needs_focus_indication(view))
2866 wstandout(view->window);
2867 waddwstr(view->window, wline);
2868 if (view_needs_focus_indication(view))
2869 wstandend(view->window);
2872 if (width < view->ncols - 1)
2873 waddch(view->window, '\n');
2875 if (asprintf(&line, "[%d/%d] %s%s",
2876 *first_displayed_line - 1 + selected_line, nlines,
2877 blame_complete ? "" : "annotating... ", path) == -1) {
2879 return got_error_from_errno("asprintf");
2882 err = format_line(&wline, &width, line, view->ncols);
2887 waddwstr(view->window, wline);
2890 if (width < view->ncols - 1)
2891 waddch(view->window, '\n');
2894 while (nprinted < max_lines - 2) {
2895 line = parse_next_line(f, &len);
2900 if (++lineno < *first_displayed_line) {
2905 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2906 err = format_line(&wline, &width, line, wlimit);
2912 if (view->focussed && nprinted == selected_line - 1)
2913 wstandout(view->window);
2915 blame_line = &lines[lineno - 1];
2916 if (blame_line->annotated && prev_id &&
2917 got_object_id_cmp(prev_id, blame_line->id) == 0)
2918 waddstr(view->window, " ");
2919 else if (blame_line->annotated) {
2921 err = got_object_id_str(&id_str, blame_line->id);
2927 wprintw(view->window, "%.8s ", id_str);
2929 prev_id = blame_line->id;
2931 waddstr(view->window, "........ ");
2935 waddwstr(view->window, wline);
2936 while (width < wlimit) {
2937 waddch(view->window, ' ');
2940 if (view->focussed && nprinted == selected_line - 1)
2941 wstandend(view->window);
2942 if (++nprinted == 1)
2943 *first_displayed_line = lineno;
2948 *last_displayed_line = lineno;
2955 static const struct got_error *
2956 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2958 const struct got_error *err = NULL;
2959 struct tog_blame_cb_args *a = arg;
2960 struct tog_blame_line *line;
2963 if (nlines != a->nlines ||
2964 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2965 return got_error(GOT_ERR_RANGE);
2967 errcode = pthread_mutex_lock(&tog_mutex);
2969 return got_error_set_errno(errcode, "pthread_mutex_lock");
2971 if (*a->quit) { /* user has quit the blame view */
2972 err = got_error(GOT_ERR_ITER_COMPLETED);
2977 goto done; /* no change in this commit */
2979 line = &a->lines[lineno - 1];
2980 if (line->annotated)
2983 line->id = got_object_id_dup(id);
2984 if (line->id == NULL) {
2985 err = got_error_from_errno("got_object_id_dup");
2988 line->annotated = 1;
2990 errcode = pthread_mutex_unlock(&tog_mutex);
2992 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2997 blame_thread(void *arg)
2999 const struct got_error *err;
3000 struct tog_blame_thread_args *ta = arg;
3001 struct tog_blame_cb_args *a = ta->cb_args;
3004 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3005 blame_cb, ta->cb_args);
3007 errcode = pthread_mutex_lock(&tog_mutex);
3009 return (void *)got_error_set_errno(errcode,
3010 "pthread_mutex_lock");
3012 got_repo_close(ta->repo);
3016 errcode = pthread_mutex_unlock(&tog_mutex);
3017 if (errcode && err == NULL)
3018 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3023 static struct got_object_id *
3024 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3027 struct tog_blame_line *line;
3029 line = &lines[first_displayed_line - 1 + selected_line - 1];
3030 if (!line->annotated)
3036 static const struct got_error *
3037 stop_blame(struct tog_blame *blame)
3039 const struct got_error *err = NULL;
3042 if (blame->thread) {
3044 errcode = pthread_mutex_unlock(&tog_mutex);
3046 return got_error_set_errno(errcode,
3047 "pthread_mutex_unlock");
3048 errcode = pthread_join(blame->thread, (void **)&err);
3050 return got_error_set_errno(errcode, "pthread_join");
3051 errcode = pthread_mutex_lock(&tog_mutex);
3053 return got_error_set_errno(errcode,
3054 "pthread_mutex_lock");
3055 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3057 blame->thread = NULL;
3059 if (blame->thread_args.repo) {
3060 got_repo_close(blame->thread_args.repo);
3061 blame->thread_args.repo = NULL;
3064 if (fclose(blame->f) != 0 && err == NULL)
3065 err = got_error_from_errno("fclose");
3069 for (i = 0; i < blame->nlines; i++)
3070 free(blame->lines[i].id);
3072 blame->lines = NULL;
3074 free(blame->cb_args.commit_id);
3075 blame->cb_args.commit_id = NULL;
3080 static const struct got_error *
3081 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3082 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3083 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3084 struct got_repository *repo)
3086 const struct got_error *err = NULL;
3087 struct got_blob_object *blob = NULL;
3088 struct got_repository *thread_repo = NULL;
3089 struct got_object_id *obj_id = NULL;
3092 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3096 return got_error(GOT_ERR_NO_OBJ);
3098 err = got_object_get_type(&obj_type, repo, obj_id);
3102 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3103 err = got_error(GOT_ERR_OBJ_TYPE);
3107 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3110 blame->f = got_opentemp();
3111 if (blame->f == NULL) {
3112 err = got_error_from_errno("got_opentemp");
3115 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3120 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3121 if (blame->lines == NULL) {
3122 err = got_error_from_errno("calloc");
3126 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3130 blame->cb_args.view = view;
3131 blame->cb_args.lines = blame->lines;
3132 blame->cb_args.nlines = blame->nlines;
3133 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3134 if (blame->cb_args.commit_id == NULL) {
3135 err = got_error_from_errno("got_object_id_dup");
3138 blame->cb_args.quit = done;
3140 blame->thread_args.path = path;
3141 blame->thread_args.repo = thread_repo;
3142 blame->thread_args.cb_args = &blame->cb_args;
3143 blame->thread_args.complete = blame_complete;
3144 *blame_complete = 0;
3148 got_object_blob_close(blob);
3155 static const struct got_error *
3156 open_blame_view(struct tog_view *view, char *path,
3157 struct got_object_id *commit_id, struct got_reflist_head *refs,
3158 struct got_repository *repo)
3160 const struct got_error *err = NULL;
3161 struct tog_blame_view_state *s = &view->state.blame;
3163 SIMPLEQ_INIT(&s->blamed_commits);
3165 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3169 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3170 s->first_displayed_line = 1;
3171 s->last_displayed_line = view->nlines;
3172 s->selected_line = 1;
3173 s->blame_complete = 0;
3175 if (s->path == NULL)
3176 return got_error_from_errno("open_blame_view");
3179 s->commit_id = commit_id;
3180 memset(&s->blame, 0, sizeof(s->blame));
3182 view->show = show_blame_view;
3183 view->input = input_blame_view;
3184 view->close = close_blame_view;
3186 return run_blame(&s->blame, view, &s->blame_complete,
3187 &s->first_displayed_line, &s->last_displayed_line,
3188 &s->selected_line, &s->done, &s->eof, s->path,
3189 s->blamed_commit->id, s->repo);
3192 static const struct got_error *
3193 close_blame_view(struct tog_view *view)
3195 const struct got_error *err = NULL;
3196 struct tog_blame_view_state *s = &view->state.blame;
3198 if (s->blame.thread)
3199 err = stop_blame(&s->blame);
3201 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3202 struct got_object_qid *blamed_commit;
3203 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3204 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3205 got_object_qid_free(blamed_commit);
3213 static const struct got_error *
3214 show_blame_view(struct tog_view *view)
3216 const struct got_error *err = NULL;
3217 struct tog_blame_view_state *s = &view->state.blame;
3220 if (s->blame.thread == NULL) {
3221 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3222 &s->blame.thread_args);
3224 return got_error_set_errno(errcode, "pthread_create");
3227 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3228 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3229 s->selected_line, &s->first_displayed_line,
3230 &s->last_displayed_line, &s->eof, view->nlines);
3236 static const struct got_error *
3237 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3238 struct tog_view **focus_view, struct tog_view *view, int ch)
3240 const struct got_error *err = NULL, *thread_err = NULL;
3241 struct tog_view *diff_view;
3242 struct tog_blame_view_state *s = &view->state.blame;
3251 if (s->selected_line > 1)
3253 else if (s->selected_line == 1 &&
3254 s->first_displayed_line > 1)
3255 s->first_displayed_line--;
3258 if (s->first_displayed_line == 1) {
3259 s->selected_line = 1;
3262 if (s->first_displayed_line > view->nlines - 2)
3263 s->first_displayed_line -=
3266 s->first_displayed_line = 1;
3270 if (s->selected_line < view->nlines - 2 &&
3271 s->first_displayed_line +
3272 s->selected_line <= s->blame.nlines)
3274 else if (s->last_displayed_line <
3276 s->first_displayed_line++;
3280 struct got_object_id *id = NULL;
3281 id = get_selected_commit_id(s->blame.lines,
3282 s->first_displayed_line, s->selected_line);
3286 struct got_commit_object *commit;
3287 struct got_object_qid *pid;
3288 struct got_object_id *blob_id = NULL;
3290 err = got_object_open_as_commit(&commit,
3294 pid = SIMPLEQ_FIRST(
3295 got_object_commit_get_parent_ids(commit));
3297 got_object_commit_close(commit);
3300 /* Check if path history ends here. */
3301 err = got_object_id_by_path(&blob_id, s->repo,
3304 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3306 got_object_commit_close(commit);
3309 err = got_object_get_type(&obj_type, s->repo,
3312 /* Can't blame non-blob type objects. */
3313 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3314 got_object_commit_close(commit);
3317 err = got_object_qid_alloc(&s->blamed_commit,
3319 got_object_commit_close(commit);
3321 if (got_object_id_cmp(id,
3322 s->blamed_commit->id) == 0)
3324 err = got_object_qid_alloc(&s->blamed_commit,
3330 thread_err = stop_blame(&s->blame);
3334 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3335 s->blamed_commit, entry);
3336 err = run_blame(&s->blame, view, &s->blame_complete,
3337 &s->first_displayed_line, &s->last_displayed_line,
3338 &s->selected_line, &s->done, &s->eof,
3339 s->path, s->blamed_commit->id, s->repo);
3345 struct got_object_qid *first;
3346 first = SIMPLEQ_FIRST(&s->blamed_commits);
3347 if (!got_object_id_cmp(first->id, s->commit_id))
3350 thread_err = stop_blame(&s->blame);
3354 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3355 got_object_qid_free(s->blamed_commit);
3357 SIMPLEQ_FIRST(&s->blamed_commits);
3358 err = run_blame(&s->blame, view, &s->blame_complete,
3359 &s->first_displayed_line, &s->last_displayed_line,
3360 &s->selected_line, &s->done, &s->eof, s->path,
3361 s->blamed_commit->id, s->repo);
3368 struct got_object_id *id = NULL;
3369 struct got_object_qid *pid;
3370 struct got_commit_object *commit = NULL;
3371 id = get_selected_commit_id(s->blame.lines,
3372 s->first_displayed_line, s->selected_line);
3375 err = got_object_open_as_commit(&commit, s->repo, id);
3378 pid = SIMPLEQ_FIRST(
3379 got_object_commit_get_parent_ids(commit));
3380 if (view_is_parent_view(view))
3381 begin_x = view_split_begin_x(view->begin_x);
3382 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3383 if (diff_view == NULL) {
3384 got_object_commit_close(commit);
3385 err = got_error_from_errno("view_open");
3388 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3389 id, NULL, s->refs, s->repo);
3390 got_object_commit_close(commit);
3392 view_close(diff_view);
3395 if (view_is_parent_view(view)) {
3396 err = view_close_child(view);
3399 err = view_set_child(view, diff_view);
3401 view_close(diff_view);
3404 *focus_view = diff_view;
3405 view->child_focussed = 1;
3407 *new_view = diff_view;
3414 if (s->last_displayed_line >= s->blame.nlines &&
3415 s->selected_line >= MIN(s->blame.nlines,
3416 view->nlines - 2)) {
3419 if (s->last_displayed_line >= s->blame.nlines &&
3420 s->selected_line < view->nlines - 2) {
3421 s->selected_line = MIN(s->blame.nlines,
3425 if (s->last_displayed_line + view->nlines - 2
3427 s->first_displayed_line +=
3430 s->first_displayed_line =
3435 if (s->selected_line > view->nlines - 2) {
3436 s->selected_line = MIN(s->blame.nlines,
3443 return thread_err ? thread_err : err;
3446 static const struct got_error *
3447 cmd_blame(int argc, char *argv[])
3449 const struct got_error *error;
3450 struct got_repository *repo = NULL;
3451 struct got_reflist_head refs;
3452 struct got_worktree *worktree = NULL;
3453 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3454 struct got_object_id *commit_id = NULL;
3455 char *commit_id_str = NULL;
3457 struct tog_view *view;
3459 SIMPLEQ_INIT(&refs);
3462 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3467 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3470 commit_id_str = optarg;
3473 repo_path = realpath(optarg, NULL);
3474 if (repo_path == NULL)
3475 err(1, "-r option");
3491 cwd = getcwd(NULL, 0);
3493 error = got_error_from_errno("getcwd");
3496 if (repo_path == NULL) {
3497 error = got_worktree_open(&worktree, cwd);
3498 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3504 strdup(got_worktree_get_repo_path(worktree));
3505 if (repo_path == NULL)
3506 error = got_error_from_errno("strdup");
3510 repo_path = strdup(cwd);
3511 if (repo_path == NULL) {
3512 error = got_error_from_errno("strdup");
3520 error = got_repo_open(&repo, repo_path);
3524 error = apply_unveil(got_repo_get_path(repo), NULL);
3529 const char *prefix = got_worktree_get_path_prefix(worktree);
3530 char *p, *worktree_subdir = cwd +
3531 strlen(got_worktree_get_root_path(worktree));
3532 if (asprintf(&p, "%s%s%s%s%s",
3533 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3534 worktree_subdir, worktree_subdir[0] ? "/" : "",
3536 error = got_error_from_errno("asprintf");
3539 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3542 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3547 if (commit_id_str == NULL) {
3548 struct got_reference *head_ref;
3549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3552 error = got_ref_resolve(&commit_id, repo, head_ref);
3553 got_ref_close(head_ref);
3555 error = got_object_resolve_id_str(&commit_id, repo,
3561 error = got_ref_list(&refs, repo);
3565 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3567 error = got_error_from_errno("view_open");
3570 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3573 error = view_loop(view);
3579 got_worktree_close(worktree);
3581 got_repo_close(repo);
3582 got_ref_list_free(&refs);
3586 static const struct got_error *
3587 draw_tree_entries(struct tog_view *view,
3588 struct got_tree_entry **first_displayed_entry,
3589 struct got_tree_entry **last_displayed_entry,
3590 struct got_tree_entry **selected_entry, int *ndisplayed,
3591 const char *label, int show_ids, const char *parent_path,
3592 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3594 const struct got_error *err = NULL;
3595 struct got_tree_entry *te;
3601 werase(view->window);
3606 err = format_line(&wline, &width, label, view->ncols);
3609 if (view_needs_focus_indication(view))
3610 wstandout(view->window);
3611 waddwstr(view->window, wline);
3612 if (view_needs_focus_indication(view))
3613 wstandend(view->window);
3616 if (width < view->ncols - 1)
3617 waddch(view->window, '\n');
3620 err = format_line(&wline, &width, parent_path, view->ncols);
3623 waddwstr(view->window, wline);
3626 if (width < view->ncols - 1)
3627 waddch(view->window, '\n');
3630 waddch(view->window, '\n');
3634 te = SIMPLEQ_FIRST(&entries->head);
3635 if (*first_displayed_entry == NULL) {
3636 if (selected == 0) {
3638 wstandout(view->window);
3639 *selected_entry = NULL;
3641 waddstr(view->window, " ..\n"); /* parent directory */
3642 if (selected == 0 && view->focussed)
3643 wstandend(view->window);
3650 while (te != *first_displayed_entry)
3651 te = SIMPLEQ_NEXT(te, entry);
3655 char *line = NULL, *id_str = NULL;
3658 err = got_object_id_str(&id_str, te->id);
3660 return got_error_from_errno(
3661 "got_object_id_str");
3663 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3664 te->name, S_ISDIR(te->mode) ? "/" :
3665 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3667 return got_error_from_errno("asprintf");
3670 err = format_line(&wline, &width, line, view->ncols);
3675 if (n == selected) {
3677 wstandout(view->window);
3678 *selected_entry = te;
3680 waddwstr(view->window, wline);
3681 if (width < view->ncols - 1)
3682 waddch(view->window, '\n');
3683 if (n == selected && view->focussed)
3684 wstandend(view->window);
3690 *last_displayed_entry = te;
3693 te = SIMPLEQ_NEXT(te, entry);
3700 tree_scroll_up(struct tog_view *view,
3701 struct got_tree_entry **first_displayed_entry, int maxscroll,
3702 const struct got_tree_entries *entries, int isroot)
3704 struct got_tree_entry *te, *prev;
3707 if (*first_displayed_entry == NULL)
3710 te = SIMPLEQ_FIRST(&entries->head);
3711 if (*first_displayed_entry == te) {
3713 *first_displayed_entry = NULL;
3717 /* XXX this is stupid... switch to TAILQ? */
3718 for (i = 0; i < maxscroll; i++) {
3719 while (te != *first_displayed_entry) {
3721 te = SIMPLEQ_NEXT(te, entry);
3723 *first_displayed_entry = prev;
3724 te = SIMPLEQ_FIRST(&entries->head);
3726 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3727 *first_displayed_entry = NULL;
3731 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3732 struct got_tree_entry *last_displayed_entry,
3733 const struct got_tree_entries *entries)
3735 struct got_tree_entry *next, *last;
3738 if (*first_displayed_entry)
3739 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3741 next = SIMPLEQ_FIRST(&entries->head);
3742 last = last_displayed_entry;
3743 while (next && last && n++ < maxscroll) {
3744 last = SIMPLEQ_NEXT(last, entry);
3746 *first_displayed_entry = next;
3747 next = SIMPLEQ_NEXT(next, entry);
3753 static const struct got_error *
3754 tree_entry_path(char **path, struct tog_parent_trees *parents,
3755 struct got_tree_entry *te)
3757 const struct got_error *err = NULL;
3758 struct tog_parent_tree *pt;
3759 size_t len = 2; /* for leading slash and NUL */
3761 TAILQ_FOREACH(pt, parents, entry)
3762 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3764 len += strlen(te->name);
3766 *path = calloc(1, len);
3768 return got_error_from_errno("calloc");
3771 pt = TAILQ_LAST(parents, tog_parent_trees);
3773 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3774 err = got_error(GOT_ERR_NO_SPACE);
3777 if (strlcat(*path, "/", len) >= len) {
3778 err = got_error(GOT_ERR_NO_SPACE);
3781 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3784 if (strlcat(*path, te->name, len) >= len) {
3785 err = got_error(GOT_ERR_NO_SPACE);
3797 static const struct got_error *
3798 blame_tree_entry(struct tog_view **new_view, int begin_x,
3799 struct got_tree_entry *te, struct tog_parent_trees *parents,
3800 struct got_object_id *commit_id, struct got_reflist_head *refs,
3801 struct got_repository *repo)
3803 const struct got_error *err = NULL;
3805 struct tog_view *blame_view;
3807 err = tree_entry_path(&path, parents, te);
3811 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3812 if (blame_view == NULL)
3813 return got_error_from_errno("view_open");
3815 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3817 view_close(blame_view);
3820 *new_view = blame_view;
3824 static const struct got_error *
3825 log_tree_entry(struct tog_view **new_view, int begin_x,
3826 struct got_tree_entry *te, struct tog_parent_trees *parents,
3827 struct got_object_id *commit_id, struct got_reflist_head *refs,
3828 struct got_repository *repo)
3830 struct tog_view *log_view;
3831 const struct got_error *err = NULL;
3834 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3835 if (log_view == NULL)
3836 return got_error_from_errno("view_open");
3838 err = tree_entry_path(&path, parents, te);
3842 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3844 view_close(log_view);
3846 *new_view = log_view;
3851 static const struct got_error *
3852 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3853 struct got_object_id *commit_id, struct got_reflist_head *refs,
3854 struct got_repository *repo)
3856 const struct got_error *err = NULL;
3857 char *commit_id_str = NULL;
3858 struct tog_tree_view_state *s = &view->state.tree;
3860 TAILQ_INIT(&s->parents);
3862 err = got_object_id_str(&commit_id_str, commit_id);
3866 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3867 err = got_error_from_errno("asprintf");
3871 s->root = s->tree = root;
3872 s->entries = got_object_tree_get_entries(root);
3873 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3874 s->commit_id = got_object_id_dup(commit_id);
3875 if (s->commit_id == NULL) {
3876 err = got_error_from_errno("got_object_id_dup");
3882 view->show = show_tree_view;
3883 view->input = input_tree_view;
3884 view->close = close_tree_view;
3886 free(commit_id_str);
3888 free(s->tree_label);
3889 s->tree_label = NULL;
3894 static const struct got_error *
3895 close_tree_view(struct tog_view *view)
3897 struct tog_tree_view_state *s = &view->state.tree;
3899 free(s->tree_label);
3900 s->tree_label = NULL;
3902 s->commit_id = NULL;
3903 while (!TAILQ_EMPTY(&s->parents)) {
3904 struct tog_parent_tree *parent;
3905 parent = TAILQ_FIRST(&s->parents);
3906 TAILQ_REMOVE(&s->parents, parent, entry);
3910 if (s->tree != s->root)
3911 got_object_tree_close(s->tree);
3912 got_object_tree_close(s->root);
3917 static const struct got_error *
3918 show_tree_view(struct tog_view *view)
3920 const struct got_error *err = NULL;
3921 struct tog_tree_view_state *s = &view->state.tree;
3924 err = tree_entry_path(&parent_path, &s->parents, NULL);
3928 err = draw_tree_entries(view, &s->first_displayed_entry,
3929 &s->last_displayed_entry, &s->selected_entry,
3930 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3931 s->entries, s->selected, view->nlines, s->tree == s->root);
3938 static const struct got_error *
3939 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3940 struct tog_view **focus_view, struct tog_view *view, int ch)
3942 const struct got_error *err = NULL;
3943 struct tog_tree_view_state *s = &view->state.tree;
3944 struct tog_view *log_view;
3945 int begin_x = 0, nscrolled;
3949 s->show_ids = !s->show_ids;
3952 if (!s->selected_entry)
3954 if (view_is_parent_view(view))
3955 begin_x = view_split_begin_x(view->begin_x);
3956 err = log_tree_entry(&log_view, begin_x,
3957 s->selected_entry, &s->parents,
3958 s->commit_id, s->refs, s->repo);
3959 if (view_is_parent_view(view)) {
3960 err = view_close_child(view);
3963 err = view_set_child(view, log_view);
3965 view_close(log_view);
3968 *focus_view = log_view;
3969 view->child_focussed = 1;
3971 *new_view = log_view;
3975 if (s->selected > 0) {
3977 if (s->selected == 0)
3980 if (s->selected > 0)
3982 tree_scroll_up(view, &s->first_displayed_entry, 1,
3983 s->entries, s->tree == s->root);
3986 tree_scroll_up(view, &s->first_displayed_entry,
3987 MAX(0, view->nlines - 4 - s->selected), s->entries,
3988 s->tree == s->root);
3990 if (SIMPLEQ_FIRST(&s->entries->head) ==
3991 s->first_displayed_entry && s->tree != s->root)
3992 s->first_displayed_entry = NULL;
3996 if (s->selected < s->ndisplayed - 1) {
4000 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4001 /* can't scroll any further */
4003 tree_scroll_down(&s->first_displayed_entry, 1,
4004 s->last_displayed_entry, s->entries);
4007 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4009 /* can't scroll any further; move cursor down */
4010 if (s->selected < s->ndisplayed - 1)
4011 s->selected = s->ndisplayed - 1;
4014 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4015 view->nlines, s->last_displayed_entry, s->entries);
4016 if (nscrolled < view->nlines) {
4018 struct got_tree_entry *te;
4019 te = s->first_displayed_entry;
4022 te = SIMPLEQ_NEXT(te, entry);
4024 s->selected = ndisplayed - 1;
4030 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4031 struct tog_parent_tree *parent;
4032 /* user selected '..' */
4033 if (s->tree == s->root)
4035 parent = TAILQ_FIRST(&s->parents);
4036 TAILQ_REMOVE(&s->parents, parent,
4038 got_object_tree_close(s->tree);
4039 s->tree = parent->tree;
4041 got_object_tree_get_entries(s->tree);
4042 s->first_displayed_entry =
4043 parent->first_displayed_entry;
4045 parent->selected_entry;
4046 s->selected = parent->selected;
4048 } else if (S_ISDIR(s->selected_entry->mode)) {
4049 struct got_tree_object *subtree;
4050 err = got_object_open_as_tree(&subtree,
4051 s->repo, s->selected_entry->id);
4054 err = tree_view_visit_subtree(subtree, s);
4056 got_object_tree_close(subtree);
4059 } else if (S_ISREG(s->selected_entry->mode)) {
4060 struct tog_view *blame_view;
4061 int begin_x = view_is_parent_view(view) ?
4062 view_split_begin_x(view->begin_x) : 0;
4064 err = blame_tree_entry(&blame_view, begin_x,
4065 s->selected_entry, &s->parents,
4066 s->commit_id, s->refs, s->repo);
4069 if (view_is_parent_view(view)) {
4070 err = view_close_child(view);
4073 err = view_set_child(view, blame_view);
4075 view_close(blame_view);
4078 *focus_view = blame_view;
4079 view->child_focussed = 1;
4081 *new_view = blame_view;
4085 if (s->selected > view->nlines)
4086 s->selected = s->ndisplayed - 1;
4099 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4104 static const struct got_error *
4105 cmd_tree(int argc, char *argv[])
4107 const struct got_error *error;
4108 struct got_repository *repo = NULL;
4109 struct got_reflist_head refs;
4110 char *repo_path = NULL;
4111 struct got_object_id *commit_id = NULL;
4112 char *commit_id_arg = NULL;
4113 struct got_commit_object *commit = NULL;
4114 struct got_tree_object *tree = NULL;
4116 struct tog_view *view;
4118 SIMPLEQ_INIT(&refs);
4121 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4126 while ((ch = getopt(argc, argv, "c:")) != -1) {
4129 commit_id_arg = optarg;
4141 struct got_worktree *worktree;
4142 char *cwd = getcwd(NULL, 0);
4144 return got_error_from_errno("getcwd");
4145 error = got_worktree_open(&worktree, cwd);
4146 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4151 strdup(got_worktree_get_repo_path(worktree));
4152 got_worktree_close(worktree);
4155 if (repo_path == NULL) {
4156 error = got_error_from_errno("strdup");
4159 } else if (argc == 1) {
4160 repo_path = realpath(argv[0], NULL);
4161 if (repo_path == NULL)
4162 return got_error_from_errno2("realpath", argv[0]);
4168 error = got_repo_open(&repo, repo_path);
4172 error = apply_unveil(got_repo_get_path(repo), NULL);
4176 if (commit_id_arg == NULL)
4177 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4179 error = got_object_resolve_id_str(&commit_id, repo,
4184 error = got_object_open_as_commit(&commit, repo, commit_id);
4188 error = got_object_open_as_tree(&tree, repo,
4189 got_object_commit_get_tree_id(commit));
4193 error = got_ref_list(&refs, repo);
4197 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4199 error = got_error_from_errno("view_open");
4202 error = open_tree_view(view, tree, commit_id, &refs, repo);
4205 error = view_loop(view);
4210 got_object_commit_close(commit);
4212 got_object_tree_close(tree);
4214 got_repo_close(repo);
4215 got_ref_list_free(&refs);
4224 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4225 "Available commands:\n", getprogname());
4226 for (i = 0; i < nitems(tog_commands); i++) {
4227 struct tog_cmd *cmd = &tog_commands[i];
4228 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4234 make_argv(const char *arg0, const char *arg1)
4237 int argc = (arg1 == NULL ? 1 : 2);
4239 argv = calloc(argc, sizeof(char *));
4242 argv[0] = strdup(arg0);
4243 if (argv[0] == NULL)
4246 argv[1] = strdup(arg1);
4247 if (argv[1] == NULL)
4255 main(int argc, char *argv[])
4257 const struct got_error *error = NULL;
4258 struct tog_cmd *cmd = NULL;
4260 char **cmd_argv = NULL;
4262 setlocale(LC_CTYPE, "");
4264 while ((ch = getopt(argc, argv, "h")) != -1) {
4283 /* Build an argument vector which runs a default command. */
4284 cmd = &tog_commands[0];
4285 cmd_argv = make_argv(cmd->name, NULL);
4290 /* Did the user specific a command? */
4291 for (i = 0; i < nitems(tog_commands); i++) {
4292 if (strncmp(tog_commands[i].name, argv[0],
4293 strlen(argv[0])) == 0) {
4294 cmd = &tog_commands[i];
4296 tog_commands[i].cmd_usage();
4301 /* Did the user specify a repository? */
4302 char *repo_path = realpath(argv[0], NULL);
4304 struct got_repository *repo;
4305 error = got_repo_open(&repo, repo_path);
4307 got_repo_close(repo);
4309 error = got_error_from_errno2("realpath",
4313 fprintf(stderr, "%s: '%s' is not a "
4314 "known command\n", getprogname(),
4318 fprintf(stderr, "%s: '%s' is neither a known "
4319 "command nor a path to a repository\n",
4320 getprogname(), argv[0]);
4324 cmd = &tog_commands[0];
4325 cmd_argv = make_argv(cmd->name, repo_path);
4331 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4338 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);