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);
549 nodelay(stdscr, TRUE);
553 nodelay(stdscr, FALSE);
554 /* Allow threads to make progress while we are waiting for input. */
555 errcode = pthread_mutex_unlock(&tog_mutex);
557 return got_error_set_errno(errcode, "pthread_mutex_unlock");
558 ch = wgetch(view->window);
559 errcode = pthread_mutex_lock(&tog_mutex);
561 return got_error_set_errno(errcode, "pthread_mutex_lock");
562 nodelay(stdscr, TRUE);
564 if (tog_sigwinch_received) {
566 tog_sigwinch_received = 0;
567 TAILQ_FOREACH(v, views, entry) {
568 err = view_resize(v);
571 err = v->input(new, dead, focus, v, KEY_RESIZE);
582 *focus = view->child;
583 view->child_focussed = 1;
584 } else if (view->parent) {
585 *focus = view->parent;
586 view->parent->child_focussed = 0;
590 err = view->input(new, dead, focus, view, ch);
597 if (view_is_parent_view(view)) {
598 if (view->child == NULL)
600 if (view_is_splitscreen(view->child)) {
601 *focus = view->child;
602 view->child_focussed = 1;
603 err = view_fullscreen(view->child);
605 err = view_splitscreen(view->child);
608 err = view->child->input(new, dead, focus,
609 view->child, KEY_RESIZE);
611 if (view_is_splitscreen(view)) {
613 view->parent->child_focussed = 1;
614 err = view_fullscreen(view);
616 err = view_splitscreen(view);
620 err = view->input(new, dead, focus, view,
627 if (view->search_start)
628 view->search_start(view);
630 err = view->input(new, dead, focus, view, ch);
634 if (view->search_next && view->searching) {
635 view->searching = (ch == 'n' ?
636 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
637 view->search_next_done = 0;
638 view->search_next(view);
640 err = view->input(new, dead, focus, view, ch);
643 err = view->input(new, dead, focus, view, ch);
651 view_vborder(struct tog_view *view)
654 struct tog_view *view_above;
657 return view_vborder(view->parent);
659 panel = panel_above(view->panel);
663 view_above = panel_userptr(panel);
664 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
665 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
669 view_needs_focus_indication(struct tog_view *view)
671 if (view_is_parent_view(view)) {
672 if (view->child == NULL || view->child_focussed)
674 if (!view_is_splitscreen(view->child))
676 } else if (!view_is_splitscreen(view))
679 return view->focussed;
682 static const struct got_error *
683 view_loop(struct tog_view *view)
685 const struct got_error *err = NULL;
686 struct tog_view_list_head views;
687 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
688 int fast_refresh = 10;
689 int done = 0, errcode;
691 errcode = pthread_mutex_lock(&tog_mutex);
693 return got_error_set_errno(errcode, "pthread_mutex_lock");
696 TAILQ_INSERT_HEAD(&views, view, entry);
700 err = view->show(view);
705 while (!TAILQ_EMPTY(&views) && !done) {
706 /* Refresh fast during initialization, then become slower. */
707 if (fast_refresh && fast_refresh-- == 0)
708 halfdelay(10); /* switch to once per second */
710 err = view_input(&new_view, &dead_view, &focus_view, &done,
715 struct tog_view *prev = NULL;
717 if (view_is_parent_view(dead_view))
718 prev = TAILQ_PREV(dead_view,
719 tog_view_list_head, entry);
720 else if (view->parent != dead_view)
723 if (dead_view->parent)
724 dead_view->parent->child = NULL;
726 TAILQ_REMOVE(&views, dead_view, entry);
728 err = view_close(dead_view);
729 if (err || dead_view == main_view)
732 if (view == dead_view) {
737 else if (!TAILQ_EMPTY(&views))
738 view = TAILQ_LAST(&views,
743 if (view->child && view->child_focussed)
744 focus_view = view->child;
751 struct tog_view *v, *t;
752 /* Only allow one parent view per type. */
753 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
754 if (v->type != new_view->type)
756 TAILQ_REMOVE(&views, v, entry);
762 TAILQ_INSERT_TAIL(&views, new_view, entry);
764 if (focus_view == NULL)
765 focus_view = new_view;
768 show_panel(focus_view->panel);
771 focus_view->focussed = 1;
774 show_panel(new_view->panel);
775 if (view->child && view_is_splitscreen(view->child))
776 show_panel(view->child->panel);
779 if (focus_view == NULL) {
781 show_panel(view->panel);
782 if (view->child && view_is_splitscreen(view->child))
783 show_panel(view->child->panel);
787 err = view->parent->show(view->parent);
791 err = view->show(view);
795 err = view->child->show(view->child);
804 while (!TAILQ_EMPTY(&views)) {
805 view = TAILQ_FIRST(&views);
806 TAILQ_REMOVE(&views, view, entry);
810 errcode = pthread_mutex_unlock(&tog_mutex);
812 return got_error_set_errno(errcode, "pthread_mutex_unlock");
822 "usage: %s log [-c commit] [-r repository-path] [path]\n",
827 /* Create newly allocated wide-character string equivalent to a byte string. */
828 static const struct got_error *
829 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
832 const struct got_error *err = NULL;
835 *wlen = mbstowcs(NULL, s, 0);
836 if (*wlen == (size_t)-1) {
839 return got_error_from_errno("mbstowcs");
841 /* byte string invalid in current encoding; try to "fix" it */
842 err = got_mbsavis(&vis, &vislen, s);
845 *wlen = mbstowcs(NULL, vis, 0);
846 if (*wlen == (size_t)-1) {
847 err = got_error_from_errno("mbstowcs"); /* give up */
852 *ws = calloc(*wlen + 1, sizeof(*ws));
854 err = got_error_from_errno("calloc");
858 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
859 err = got_error_from_errno("mbstowcs");
870 /* Format a line for display, ensuring that it won't overflow a width limit. */
871 static const struct got_error *
872 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
874 const struct got_error *err = NULL;
876 wchar_t *wline = NULL;
883 err = mbs2ws(&wline, &wlen, line);
888 while (i < wlen && cols < wlimit) {
889 int width = wcwidth(wline[i]);
896 if (cols + width <= wlimit)
901 if (wline[i] == L'\t')
902 cols += TABSIZE - ((cols + 1) % TABSIZE);
906 err = got_error_from_errno("wcwidth");
921 static const struct got_error*
922 build_refs_str(char **refs_str, struct got_reflist_head *refs,
923 struct got_object_id *id)
925 static const struct got_error *err = NULL;
926 struct got_reflist_entry *re;
932 SIMPLEQ_FOREACH(re, refs, entry) {
933 if (got_object_id_cmp(re->id, id) != 0)
935 name = got_ref_get_name(re->ref);
936 if (strcmp(name, GOT_REF_HEAD) == 0)
938 if (strncmp(name, "refs/", 5) == 0)
940 if (strncmp(name, "got/", 4) == 0)
942 if (strncmp(name, "heads/", 6) == 0)
944 if (strncmp(name, "remotes/", 8) == 0)
947 if (asprintf(refs_str, "%s%s%s", s ? s : "",
948 s ? ", " : "", name) == -1) {
949 err = got_error_from_errno("asprintf");
960 static const struct got_error *
961 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
963 char *smallerthan, *at;
965 smallerthan = strchr(author, '<');
966 if (smallerthan && smallerthan[1] != '\0')
967 author = smallerthan + 1;
968 at = strchr(author, '@');
971 return format_line(wauthor, author_width, author, limit);
974 static const struct got_error *
975 draw_commit(struct tog_view *view, struct got_commit_object *commit,
976 struct got_object_id *id, struct got_reflist_head *refs,
977 int author_display_cols)
979 const struct got_error *err = NULL;
980 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
981 char *logmsg0 = NULL, *logmsg = NULL;
983 wchar_t *wlogmsg = NULL, *wauthor = NULL;
984 int author_width, logmsg_width;
985 char *newline, *line = NULL;
987 static const size_t date_display_cols = 9;
988 const int avail = view->ncols;
990 time_t committer_time;
992 committer_time = got_object_commit_get_committer_time(commit);
993 if (localtime_r(&committer_time, &tm) == NULL)
994 return got_error_from_errno("localtime_r");
995 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
997 return got_error(GOT_ERR_NO_SPACE);
999 if (avail < date_display_cols)
1000 limit = MIN(sizeof(datebuf) - 1, avail);
1002 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1003 waddnstr(view->window, datebuf, limit);
1008 author = strdup(got_object_commit_get_author(commit));
1009 if (author == NULL) {
1010 err = got_error_from_errno("strdup");
1013 err = format_author(&wauthor, &author_width, author, avail - col);
1016 waddwstr(view->window, wauthor);
1017 col += author_width;
1018 while (col <= avail && author_width < author_display_cols + 2) {
1019 waddch(view->window, ' ');
1026 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1027 if (logmsg0 == NULL) {
1028 err = got_error_from_errno("strdup");
1032 while (*logmsg == '\n')
1034 newline = strchr(logmsg, '\n');
1037 limit = avail - col;
1038 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1041 waddwstr(view->window, wlogmsg);
1042 col += logmsg_width;
1043 while (col <= avail) {
1044 waddch(view->window, ' ');
1056 static struct commit_queue_entry *
1057 alloc_commit_queue_entry(struct got_commit_object *commit,
1058 struct got_object_id *id)
1060 struct commit_queue_entry *entry;
1062 entry = calloc(1, sizeof(*entry));
1067 entry->commit = commit;
1072 pop_commit(struct commit_queue *commits)
1074 struct commit_queue_entry *entry;
1076 entry = TAILQ_FIRST(&commits->head);
1077 TAILQ_REMOVE(&commits->head, entry, entry);
1078 got_object_commit_close(entry->commit);
1079 commits->ncommits--;
1080 /* Don't free entry->id! It is owned by the commit graph. */
1085 free_commits(struct commit_queue *commits)
1087 while (!TAILQ_EMPTY(&commits->head))
1088 pop_commit(commits);
1091 static const struct got_error *
1092 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1093 int minqueue, struct got_repository *repo, const char *path)
1095 const struct got_error *err = NULL;
1099 * We keep all commits open throughout the lifetime of the log
1100 * view in order to avoid having to re-fetch commits from disk
1101 * while updating the display.
1103 while (nqueued < minqueue) {
1104 struct got_object_id *id;
1105 struct got_commit_object *commit;
1106 struct commit_queue_entry *entry;
1109 err = got_commit_graph_iter_next(&id, graph);
1111 if (err->code != GOT_ERR_ITER_NEED_MORE)
1113 err = got_commit_graph_fetch_commits(graph,
1123 err = got_object_open_as_commit(&commit, repo, id);
1126 entry = alloc_commit_queue_entry(commit, id);
1127 if (entry == NULL) {
1128 err = got_error_from_errno("alloc_commit_queue_entry");
1132 errcode = pthread_mutex_lock(&tog_mutex);
1134 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1138 entry->idx = commits->ncommits;
1139 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1141 commits->ncommits++;
1143 errcode = pthread_mutex_unlock(&tog_mutex);
1144 if (errcode && err == NULL)
1145 err = got_error_set_errno(errcode,
1146 "pthread_mutex_unlock");
1152 static const struct got_error *
1153 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1154 struct got_repository *repo)
1156 const struct got_error *err = NULL;
1157 struct got_reference *head_ref;
1161 err = got_ref_open(&head_ref, repo, branch_name, 0);
1165 err = got_ref_resolve(head_id, repo, head_ref);
1166 got_ref_close(head_ref);
1175 static const struct got_error *
1176 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1177 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1178 struct commit_queue *commits, int selected_idx, int limit,
1179 struct got_reflist_head *refs, const char *path, int commits_needed)
1181 const struct got_error *err = NULL;
1182 struct commit_queue_entry *entry;
1184 int ncommits, author_cols = 10;
1185 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1186 char *refs_str = NULL;
1192 if (ncommits == selected_idx) {
1196 entry = TAILQ_NEXT(entry, entry);
1200 if (*selected && !(view->searching && view->search_next_done == 0)) {
1201 err = got_object_id_str(&id_str, (*selected)->id);
1205 err = build_refs_str(&refs_str, refs, (*selected)->id);
1211 if (commits_needed == 0)
1212 halfdelay(10); /* disable fast refresh */
1214 if (asprintf(&ncommits_str, " [%d/%d] %s",
1215 entry ? entry->idx + 1 : 0, commits->ncommits,
1216 commits_needed > 0 ?
1217 (view->searching && view->search_next_done == 0
1218 ? "searching..." : "loading... ") :
1219 (refs_str ? refs_str : "")) == -1) {
1220 err = got_error_from_errno("asprintf");
1224 if (path && strcmp(path, "/") != 0) {
1225 if (asprintf(&header, "commit %s %s%s",
1226 id_str ? id_str : "........................................",
1227 path, ncommits_str) == -1) {
1228 err = got_error_from_errno("asprintf");
1232 } else if (asprintf(&header, "commit %s%s",
1233 id_str ? id_str : "........................................",
1234 ncommits_str) == -1) {
1235 err = got_error_from_errno("asprintf");
1239 err = format_line(&wline, &width, header, view->ncols);
1243 werase(view->window);
1245 if (view_needs_focus_indication(view))
1246 wstandout(view->window);
1247 waddwstr(view->window, wline);
1248 while (width < view->ncols) {
1249 waddch(view->window, ' ');
1252 if (view_needs_focus_indication(view))
1253 wstandend(view->window);
1258 /* Grow author column size if necessary. */
1265 if (ncommits >= limit - 1)
1267 author = strdup(got_object_commit_get_author(entry->commit));
1268 if (author == NULL) {
1269 err = got_error_from_errno("strdup");
1272 err = format_author(&wauthor, &width, author, COLS);
1273 if (author_cols < width)
1274 author_cols = width;
1277 entry = TAILQ_NEXT(entry, entry);
1284 if (ncommits >= limit - 1)
1286 if (ncommits == selected_idx)
1287 wstandout(view->window);
1288 err = draw_commit(view, entry->commit, entry->id, refs,
1290 if (ncommits == selected_idx)
1291 wstandend(view->window);
1296 entry = TAILQ_NEXT(entry, entry);
1309 scroll_up(struct tog_view *view,
1310 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1311 struct commit_queue *commits)
1313 struct commit_queue_entry *entry;
1316 entry = TAILQ_FIRST(&commits->head);
1317 if (*first_displayed_entry == entry)
1320 entry = *first_displayed_entry;
1321 while (entry && nscrolled < maxscroll) {
1322 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1324 *first_displayed_entry = entry;
1330 static const struct got_error *
1331 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1332 pthread_cond_t *need_commits)
1337 halfdelay(1); /* fast refresh while loading commits */
1339 while (*commits_needed > 0) {
1343 /* Wake the log thread. */
1344 errcode = pthread_cond_signal(need_commits);
1346 return got_error_set_errno(errcode,
1347 "pthread_cond_signal");
1348 errcode = pthread_mutex_unlock(&tog_mutex);
1350 return got_error_set_errno(errcode,
1351 "pthread_mutex_unlock");
1353 errcode = pthread_mutex_lock(&tog_mutex);
1355 return got_error_set_errno(errcode,
1356 "pthread_mutex_lock");
1358 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1360 * Thread is not done yet; lose a key press
1361 * and let the user retry... this way the GUI
1362 * remains interactive while logging deep paths
1363 * with few commits in history.
1372 static const struct got_error *
1373 scroll_down(struct tog_view *view,
1374 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1375 struct commit_queue_entry **last_displayed_entry,
1376 struct commit_queue *commits, int *log_complete, int *commits_needed,
1377 pthread_cond_t *need_commits)
1379 const struct got_error *err = NULL;
1380 struct commit_queue_entry *pentry;
1383 if (*last_displayed_entry == NULL)
1386 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1387 if (pentry == NULL && !*log_complete) {
1389 * Ask the log thread for required amount of commits
1390 * plus some amount of pre-fetching.
1392 (*commits_needed) += maxscroll + 20;
1393 err = trigger_log_thread(0, commits_needed, log_complete,
1400 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1404 *last_displayed_entry = pentry;
1406 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1409 *first_displayed_entry = pentry;
1410 } while (++nscrolled < maxscroll);
1415 static const struct got_error *
1416 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1417 struct got_commit_object *commit, struct got_object_id *commit_id,
1418 struct tog_view *log_view, struct got_reflist_head *refs,
1419 struct got_repository *repo)
1421 const struct got_error *err;
1422 struct got_object_qid *parent_id;
1423 struct tog_view *diff_view;
1425 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1426 if (diff_view == NULL)
1427 return got_error_from_errno("view_open");
1429 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1430 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1431 commit_id, log_view, refs, repo);
1433 *new_view = diff_view;
1437 static const struct got_error *
1438 tree_view_visit_subtree(struct got_tree_object *subtree,
1439 struct tog_tree_view_state *s)
1441 struct tog_parent_tree *parent;
1443 parent = calloc(1, sizeof(*parent));
1445 return got_error_from_errno("calloc");
1447 parent->tree = s->tree;
1448 parent->first_displayed_entry = s->first_displayed_entry;
1449 parent->selected_entry = s->selected_entry;
1450 parent->selected = s->selected;
1451 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1453 s->entries = got_object_tree_get_entries(s->tree);
1455 s->first_displayed_entry = NULL;
1460 static const struct got_error *
1461 browse_commit_tree(struct tog_view **new_view, int begin_x,
1462 struct commit_queue_entry *entry, const char *path,
1463 struct got_reflist_head *refs, struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_tree_object *tree;
1467 struct got_tree_entry *te;
1468 struct tog_tree_view_state *s;
1469 struct tog_view *tree_view;
1470 char *slash, *subpath = NULL;
1473 err = got_object_open_as_tree(&tree, repo,
1474 got_object_commit_get_tree_id(entry->commit));
1478 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1479 if (tree_view == NULL)
1480 return got_error_from_errno("view_open");
1482 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1484 got_object_tree_close(tree);
1487 s = &tree_view->state.tree;
1489 *new_view = tree_view;
1491 /* Walk the path and open corresponding tree objects. */
1494 struct got_object_id *tree_id;
1499 /* Ensure the correct subtree entry is selected. */
1500 slash = strchr(p, '/');
1502 slash = strchr(p, '\0');
1503 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1504 if (strncmp(p, te->name, slash - p) == 0) {
1505 s->selected_entry = te;
1510 if (s->selected_entry == NULL) {
1511 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1514 if (s->tree != s->root)
1515 s->selected++; /* skip '..' */
1517 if (!S_ISDIR(s->selected_entry->mode)) {
1518 /* Jump to this file's entry. */
1519 s->first_displayed_entry = s->selected_entry;
1524 slash = strchr(p, '/');
1526 subpath = strndup(path, slash - path);
1528 subpath = strdup(path);
1529 if (subpath == NULL) {
1530 err = got_error_from_errno("strdup");
1534 err = got_object_id_by_path(&tree_id, repo, entry->id,
1539 err = got_object_open_as_tree(&tree, repo, tree_id);
1544 err = tree_view_visit_subtree(tree, s);
1546 got_object_tree_close(tree);
1561 log_thread(void *arg)
1563 const struct got_error *err = NULL;
1565 struct tog_log_thread_args *a = arg;
1568 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1572 while (!done && !err) {
1573 err = queue_commits(a->graph, a->commits, 1, a->repo,
1576 if (err->code != GOT_ERR_ITER_COMPLETED)
1580 } else if (a->commits_needed > 0)
1581 a->commits_needed--;
1583 errcode = pthread_mutex_lock(&tog_mutex);
1585 err = got_error_set_errno(errcode,
1586 "pthread_mutex_lock");
1588 } else if (*a->quit)
1590 else if (*a->first_displayed_entry == NULL) {
1591 *a->first_displayed_entry =
1592 TAILQ_FIRST(&a->commits->head);
1593 *a->selected_entry = *a->first_displayed_entry;
1597 a->commits_needed = 0;
1598 else if (a->commits_needed == 0) {
1599 errcode = pthread_cond_wait(&a->need_commits,
1602 err = got_error_set_errno(errcode,
1603 "pthread_cond_wait");
1606 errcode = pthread_mutex_unlock(&tog_mutex);
1607 if (errcode && err == NULL)
1608 err = got_error_set_errno(errcode,
1609 "pthread_mutex_unlock");
1611 a->log_complete = 1;
1615 static const struct got_error *
1616 stop_log_thread(struct tog_log_view_state *s)
1618 const struct got_error *err = NULL;
1623 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1625 return got_error_set_errno(errcode,
1626 "pthread_cond_signal");
1627 errcode = pthread_mutex_unlock(&tog_mutex);
1629 return got_error_set_errno(errcode,
1630 "pthread_mutex_unlock");
1631 errcode = pthread_join(s->thread, (void **)&err);
1633 return got_error_set_errno(errcode, "pthread_join");
1634 errcode = pthread_mutex_lock(&tog_mutex);
1636 return got_error_set_errno(errcode,
1637 "pthread_mutex_lock");
1641 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1642 if (errcode && err == NULL)
1643 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1645 if (s->thread_args.repo) {
1646 got_repo_close(s->thread_args.repo);
1647 s->thread_args.repo = NULL;
1650 if (s->thread_args.graph) {
1651 got_commit_graph_close(s->thread_args.graph);
1652 s->thread_args.graph = NULL;
1658 static const struct got_error *
1659 close_log_view(struct tog_view *view)
1661 const struct got_error *err = NULL;
1662 struct tog_log_view_state *s = &view->state.log;
1664 err = stop_log_thread(s);
1665 free_commits(&s->commits);
1666 free(s->in_repo_path);
1667 s->in_repo_path = NULL;
1673 static const struct got_error *
1674 search_start_log_view(struct tog_view *view)
1676 struct tog_log_view_state *s = &view->state.log;
1680 if (view->nlines < 1)
1683 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
1684 view->begin_x, "/");
1685 wclrtoeol(view->window);
1689 ret = wgetnstr(view->window, pattern, sizeof(pattern));
1695 if (view->searching) {
1697 view->searching = 0;
1700 s->matched_entry = NULL;
1701 if (regcomp(&s->regex, pattern, REG_EXTENDED | REG_ICASE) == 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->matched_entry, entry);
1742 entry = TAILQ_PREV(s->matched_entry,
1743 commit_queue_head, entry);
1745 entry = TAILQ_FIRST(&s->commits.head);
1748 if (entry == NULL) {
1749 if (s->thread_args.log_complete) {
1750 if (s->matched_entry == NULL) {
1751 view->search_next_done = 1;
1754 if (view->searching == TOG_SEARCH_FORWARD)
1755 entry = TAILQ_FIRST(&s->commits.head);
1757 entry = TAILQ_LAST(&s->commits.head,
1760 s->thread_args.commits_needed = 1;
1761 return trigger_log_thread(0,
1762 &s->thread_args.commits_needed,
1763 &s->thread_args.log_complete,
1764 &s->thread_args.need_commits);
1768 if (match_commit(entry->commit, &s->regex)) {
1769 view->search_next_done = 1;
1772 if (view->searching == TOG_SEARCH_FORWARD)
1773 entry = TAILQ_NEXT(entry, entry);
1775 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1779 int cur = s->selected_entry->idx;
1780 s->matched_entry = entry;
1781 while (cur < s->matched_entry->idx) {
1782 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1787 while (cur > s->matched_entry->idx) {
1788 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1799 static const struct got_error *
1800 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1801 struct got_reflist_head *refs, struct got_repository *repo,
1802 const char *path, int check_disk)
1804 const struct got_error *err = NULL;
1805 struct tog_log_view_state *s = &view->state.log;
1806 struct got_repository *thread_repo = NULL;
1807 struct got_commit_graph *thread_graph = NULL;
1810 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1814 /* The commit queue only contains commits being displayed. */
1815 TAILQ_INIT(&s->commits.head);
1816 s->commits.ncommits = 0;
1820 s->start_id = got_object_id_dup(start_id);
1821 if (s->start_id == NULL) {
1822 err = got_error_from_errno("got_object_id_dup");
1826 view->show = show_log_view;
1827 view->input = input_log_view;
1828 view->close = close_log_view;
1829 view->search_start = search_start_log_view;
1830 view->search_next = search_next_log_view;
1832 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1835 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1840 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1842 err = got_error_set_errno(errcode, "pthread_cond_init");
1846 s->thread_args.commits_needed = view->nlines;
1847 s->thread_args.graph = thread_graph;
1848 s->thread_args.commits = &s->commits;
1849 s->thread_args.in_repo_path = s->in_repo_path;
1850 s->thread_args.start_id = s->start_id;
1851 s->thread_args.repo = thread_repo;
1852 s->thread_args.log_complete = 0;
1853 s->thread_args.quit = &s->quit;
1854 s->thread_args.view = view;
1855 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1856 s->thread_args.selected_entry = &s->selected_entry;
1859 close_log_view(view);
1863 static const struct got_error *
1864 show_log_view(struct tog_view *view)
1866 struct tog_log_view_state *s = &view->state.log;
1868 if (s->thread == NULL) {
1869 int errcode = pthread_create(&s->thread, NULL, log_thread,
1872 return got_error_set_errno(errcode, "pthread_create");
1875 return draw_commits(view, &s->last_displayed_entry,
1876 &s->selected_entry, s->first_displayed_entry,
1877 &s->commits, s->selected, view->nlines, s->refs,
1878 s->in_repo_path, s->thread_args.commits_needed);
1881 static const struct got_error *
1882 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1883 struct tog_view **focus_view, struct tog_view *view, int ch)
1885 const struct got_error *err = NULL;
1886 struct tog_log_view_state *s = &view->state.log;
1888 struct tog_view *diff_view = NULL, *tree_view = NULL;
1899 if (s->first_displayed_entry == NULL)
1901 if (s->selected > 0)
1903 if (s->selected > 0)
1905 scroll_up(view, &s->first_displayed_entry, 1,
1910 if (s->first_displayed_entry == NULL)
1912 if (TAILQ_FIRST(&s->commits.head) ==
1913 s->first_displayed_entry) {
1917 scroll_up(view, &s->first_displayed_entry,
1918 view->nlines, &s->commits);
1924 if (s->first_displayed_entry == NULL)
1926 if (s->selected < MIN(view->nlines - 2,
1927 s->commits.ncommits - 1)) {
1931 err = scroll_down(view, &s->first_displayed_entry, 1,
1932 &s->last_displayed_entry, &s->commits,
1933 &s->thread_args.log_complete,
1934 &s->thread_args.commits_needed,
1935 &s->thread_args.need_commits);
1939 struct commit_queue_entry *first;
1940 first = s->first_displayed_entry;
1943 err = scroll_down(view, &s->first_displayed_entry,
1944 view->nlines, &s->last_displayed_entry,
1945 &s->commits, &s->thread_args.log_complete,
1946 &s->thread_args.commits_needed,
1947 &s->thread_args.need_commits);
1948 if (first == s->first_displayed_entry &&
1949 s->selected < MIN(view->nlines - 2,
1950 s->commits.ncommits - 1)) {
1951 /* can't scroll further down */
1952 s->selected = MIN(view->nlines - 2,
1953 s->commits.ncommits - 1);
1959 if (s->selected > view->nlines - 2)
1960 s->selected = view->nlines - 2;
1961 if (s->selected > s->commits.ncommits - 1)
1962 s->selected = s->commits.ncommits - 1;
1967 if (s->selected_entry == NULL)
1969 if (view_is_parent_view(view))
1970 begin_x = view_split_begin_x(view->begin_x);
1971 err = open_diff_view_for_commit(&diff_view, begin_x,
1972 s->selected_entry->commit, s->selected_entry->id,
1973 view, s->refs, s->repo);
1976 if (view_is_parent_view(view)) {
1977 err = view_close_child(view);
1980 err = view_set_child(view, diff_view);
1982 view_close(diff_view);
1985 *focus_view = diff_view;
1986 view->child_focussed = 1;
1988 *new_view = diff_view;
1991 if (s->selected_entry == NULL)
1993 if (view_is_parent_view(view))
1994 begin_x = view_split_begin_x(view->begin_x);
1995 err = browse_commit_tree(&tree_view, begin_x,
1996 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1999 if (view_is_parent_view(view)) {
2000 err = view_close_child(view);
2003 err = view_set_child(view, tree_view);
2005 view_close(tree_view);
2008 *focus_view = tree_view;
2009 view->child_focussed = 1;
2011 *new_view = tree_view;
2014 if (strcmp(s->in_repo_path, "/") == 0)
2016 parent_path = dirname(s->in_repo_path);
2017 if (parent_path && strcmp(parent_path, ".") != 0) {
2018 struct tog_view *lv;
2019 err = stop_log_thread(s);
2022 lv = view_open(view->nlines, view->ncols,
2023 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2025 return got_error_from_errno(
2027 err = open_log_view(lv, s->start_id, s->refs,
2028 s->repo, parent_path, 0);
2031 if (view_is_parent_view(view))
2034 view_set_child(view->parent, lv);
2047 static const struct got_error *
2048 apply_unveil(const char *repo_path, const char *worktree_path)
2050 const struct got_error *error;
2052 if (repo_path && unveil(repo_path, "r") != 0)
2053 return got_error_from_errno2("unveil", repo_path);
2055 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2056 return got_error_from_errno2("unveil", worktree_path);
2058 if (unveil("/tmp", "rwc") != 0)
2059 return got_error_from_errno2("unveil", "/tmp");
2061 error = got_privsep_unveil_exec_helpers();
2065 if (unveil(NULL, NULL) != 0)
2066 return got_error_from_errno("unveil");
2076 halfdelay(1); /* Do fast refresh while initial view is loading. */
2079 intrflush(stdscr, FALSE);
2080 keypad(stdscr, TRUE);
2082 signal(SIGWINCH, tog_sigwinch);
2085 static const struct got_error *
2086 cmd_log(int argc, char *argv[])
2088 const struct got_error *error;
2089 struct got_repository *repo = NULL;
2090 struct got_worktree *worktree = NULL;
2091 struct got_reflist_head refs;
2092 struct got_object_id *start_id = NULL;
2093 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2094 char *start_commit = NULL;
2096 struct tog_view *view;
2098 SIMPLEQ_INIT(&refs);
2101 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2106 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2109 start_commit = optarg;
2112 repo_path = realpath(optarg, NULL);
2113 if (repo_path == NULL)
2114 err(1, "-r option");
2125 cwd = getcwd(NULL, 0);
2127 error = got_error_from_errno("getcwd");
2130 error = got_worktree_open(&worktree, cwd);
2131 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2138 error = got_error_from_errno("strdup");
2141 } else if (argc == 1) {
2143 error = got_worktree_resolve_path(&path, worktree,
2148 path = strdup(argv[0]);
2150 error = got_error_from_errno("strdup");
2157 repo_path = worktree ?
2158 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2159 if (repo_path == NULL) {
2160 error = got_error_from_errno("strdup");
2166 error = got_repo_open(&repo, repo_path);
2170 error = apply_unveil(got_repo_get_path(repo),
2171 worktree ? got_worktree_get_root_path(worktree) : NULL);
2175 if (start_commit == NULL)
2176 error = get_head_commit_id(&start_id, worktree ?
2177 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2180 error = got_object_resolve_id_str(&start_id, repo,
2185 error = got_ref_list(&refs, repo);
2189 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2191 error = got_error_from_errno("view_open");
2194 error = open_log_view(view, start_id, &refs, repo, path, 1);
2197 error = view_loop(view);
2204 got_repo_close(repo);
2206 got_worktree_close(worktree);
2207 got_ref_list_free(&refs);
2215 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2221 parse_next_line(FILE *f, size_t *len)
2226 const char delim[3] = { '\0', '\0', '\0'};
2228 line = fparseln(f, &linelen, &lineno, delim, 0);
2234 static const struct got_error *
2235 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2236 int *last_displayed_line, int *eof, int max_lines,
2239 const struct got_error *err;
2240 int nlines = 0, nprinted = 0;
2247 werase(view->window);
2250 err = format_line(&wline, &width, header, view->ncols);
2255 if (view_needs_focus_indication(view))
2256 wstandout(view->window);
2257 waddwstr(view->window, wline);
2258 if (view_needs_focus_indication(view))
2259 wstandend(view->window);
2260 if (width < view->ncols - 1)
2261 waddch(view->window, '\n');
2269 while (nprinted < max_lines) {
2270 line = parse_next_line(f, &len);
2275 if (++nlines < *first_displayed_line) {
2280 err = format_line(&wline, &width, line, view->ncols);
2285 waddwstr(view->window, wline);
2286 if (width < view->ncols - 1)
2287 waddch(view->window, '\n');
2288 if (++nprinted == 1)
2289 *first_displayed_line = nlines;
2294 *last_displayed_line = nlines;
2299 while (nprinted < view->nlines) {
2300 waddch(view->window, '\n');
2304 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2309 wstandout(view->window);
2310 waddwstr(view->window, wline);
2311 wstandend(view->window);
2318 get_datestr(time_t *time, char *datebuf)
2320 char *p, *s = ctime_r(time, datebuf);
2321 p = strchr(s, '\n');
2327 static const struct got_error *
2328 write_commit_info(struct got_object_id *commit_id,
2329 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2331 const struct got_error *err = NULL;
2333 struct got_commit_object *commit;
2334 char *id_str = NULL;
2335 time_t committer_time;
2336 const char *author, *committer;
2337 char *refs_str = NULL;
2340 err = build_refs_str(&refs_str, refs, commit_id);
2345 err = got_object_open_as_commit(&commit, repo, commit_id);
2349 err = got_object_id_str(&id_str, commit_id);
2351 err = got_error_from_errno("got_object_id_str");
2355 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2356 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2357 err = got_error_from_errno("fprintf");
2360 if (fprintf(outfile, "from: %s\n",
2361 got_object_commit_get_author(commit)) < 0) {
2362 err = got_error_from_errno("fprintf");
2365 committer_time = got_object_commit_get_committer_time(commit);
2366 if (fprintf(outfile, "date: %s UTC\n",
2367 get_datestr(&committer_time, datebuf)) < 0) {
2368 err = got_error_from_errno("fprintf");
2371 author = got_object_commit_get_author(commit);
2372 committer = got_object_commit_get_committer(commit);
2373 if (strcmp(author, committer) != 0 &&
2374 fprintf(outfile, "via: %s\n", committer) < 0) {
2375 err = got_error_from_errno("fprintf");
2378 if (fprintf(outfile, "%s\n",
2379 got_object_commit_get_logmsg(commit)) < 0) {
2380 err = got_error_from_errno("fprintf");
2386 got_object_commit_close(commit);
2390 static const struct got_error *
2391 create_diff(struct tog_diff_view_state *s)
2393 const struct got_error *err = NULL;
2399 err = got_error_from_errno("got_opentemp");
2402 if (s->f && fclose(s->f) != 0) {
2403 err = got_error_from_errno("fclose");
2409 err = got_object_get_type(&obj_type, s->repo, s->id1);
2411 err = got_object_get_type(&obj_type, s->repo, s->id2);
2416 case GOT_OBJ_TYPE_BLOB:
2417 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2418 s->diff_context, s->repo, f);
2420 case GOT_OBJ_TYPE_TREE:
2421 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2422 s->diff_context, s->repo, f);
2424 case GOT_OBJ_TYPE_COMMIT: {
2425 const struct got_object_id_queue *parent_ids;
2426 struct got_object_qid *pid;
2427 struct got_commit_object *commit2;
2429 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2432 /* Show commit info if we're diffing to a parent/root commit. */
2434 write_commit_info(s->id2, s->refs, s->repo, f);
2436 parent_ids = got_object_commit_get_parent_ids(commit2);
2437 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2438 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2439 write_commit_info(s->id2, s->refs,
2445 got_object_commit_close(commit2);
2447 err = got_diff_objects_as_commits(s->id1, s->id2,
2448 s->diff_context, s->repo, f);
2452 err = got_error(GOT_ERR_OBJ_TYPE);
2456 if (f && fflush(f) != 0 && err == NULL)
2457 err = got_error_from_errno("fflush");
2462 diff_view_indicate_progress(struct tog_view *view)
2464 mvwaddstr(view->window, 0, 0, "diffing...");
2469 static const struct got_error *
2470 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2471 struct got_object_id *id2, struct tog_view *log_view,
2472 struct got_reflist_head *refs, struct got_repository *repo)
2474 const struct got_error *err;
2476 if (id1 != NULL && id2 != NULL) {
2478 err = got_object_get_type(&type1, repo, id1);
2481 err = got_object_get_type(&type2, repo, id2);
2486 return got_error(GOT_ERR_OBJ_TYPE);
2490 view->state.diff.id1 = got_object_id_dup(id1);
2491 if (view->state.diff.id1 == NULL)
2492 return got_error_from_errno("got_object_id_dup");
2494 view->state.diff.id1 = NULL;
2496 view->state.diff.id2 = got_object_id_dup(id2);
2497 if (view->state.diff.id2 == NULL) {
2498 free(view->state.diff.id1);
2499 view->state.diff.id1 = NULL;
2500 return got_error_from_errno("got_object_id_dup");
2502 view->state.diff.f = NULL;
2503 view->state.diff.first_displayed_line = 1;
2504 view->state.diff.last_displayed_line = view->nlines;
2505 view->state.diff.diff_context = 3;
2506 view->state.diff.log_view = log_view;
2507 view->state.diff.repo = repo;
2508 view->state.diff.refs = refs;
2510 if (log_view && view_is_splitscreen(view))
2511 show_log_view(log_view); /* draw vborder */
2512 diff_view_indicate_progress(view);
2514 err = create_diff(&view->state.diff);
2516 free(view->state.diff.id1);
2517 view->state.diff.id1 = NULL;
2518 free(view->state.diff.id2);
2519 view->state.diff.id2 = NULL;
2523 view->show = show_diff_view;
2524 view->input = input_diff_view;
2525 view->close = close_diff_view;
2530 static const struct got_error *
2531 close_diff_view(struct tog_view *view)
2533 const struct got_error *err = NULL;
2535 free(view->state.diff.id1);
2536 view->state.diff.id1 = NULL;
2537 free(view->state.diff.id2);
2538 view->state.diff.id2 = NULL;
2539 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2540 err = got_error_from_errno("fclose");
2544 static const struct got_error *
2545 show_diff_view(struct tog_view *view)
2547 const struct got_error *err;
2548 struct tog_diff_view_state *s = &view->state.diff;
2549 char *id_str1 = NULL, *id_str2, *header;
2552 err = got_object_id_str(&id_str1, s->id1);
2556 err = got_object_id_str(&id_str2, s->id2);
2560 if (asprintf(&header, "diff %s %s",
2561 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2562 err = got_error_from_errno("asprintf");
2570 return draw_file(view, s->f, &s->first_displayed_line,
2571 &s->last_displayed_line, &s->eof, view->nlines,
2575 static const struct got_error *
2576 set_selected_commit(struct tog_diff_view_state *s,
2577 struct commit_queue_entry *entry)
2579 const struct got_error *err;
2580 const struct got_object_id_queue *parent_ids;
2581 struct got_commit_object *selected_commit;
2582 struct got_object_qid *pid;
2585 s->id2 = got_object_id_dup(entry->id);
2587 return got_error_from_errno("got_object_id_dup");
2589 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2592 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2594 pid = SIMPLEQ_FIRST(parent_ids);
2595 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2596 got_object_commit_close(selected_commit);
2600 static const struct got_error *
2601 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2602 struct tog_view **focus_view, struct tog_view *view, int ch)
2604 const struct got_error *err = NULL;
2605 struct tog_diff_view_state *s = &view->state.diff;
2606 struct tog_log_view_state *ls;
2607 struct commit_queue_entry *entry;
2613 if (s->first_displayed_line > 1)
2614 s->first_displayed_line--;
2618 if (s->first_displayed_line == 1)
2621 while (i++ < view->nlines - 1 &&
2622 s->first_displayed_line > 1)
2623 s->first_displayed_line--;
2628 s->first_displayed_line++;
2636 while (!s->eof && i++ < view->nlines - 1) {
2638 line = parse_next_line(s->f, NULL);
2639 s->first_displayed_line++;
2645 if (s->diff_context > 0) {
2647 diff_view_indicate_progress(view);
2648 err = create_diff(s);
2652 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2654 diff_view_indicate_progress(view);
2655 err = create_diff(s);
2660 if (s->log_view == NULL)
2662 ls = &s->log_view->state.log;
2663 entry = TAILQ_PREV(ls->selected_entry,
2664 commit_queue_head, entry);
2668 err = input_log_view(NULL, NULL, NULL, s->log_view,
2673 err = set_selected_commit(s, entry);
2677 s->first_displayed_line = 1;
2678 s->last_displayed_line = view->nlines;
2680 diff_view_indicate_progress(view);
2681 err = create_diff(s);
2685 if (s->log_view == NULL)
2687 ls = &s->log_view->state.log;
2689 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2690 ls->thread_args.commits_needed++;
2692 /* Display "loading..." in log view. */
2693 show_log_view(s->log_view);
2697 err = trigger_log_thread(1 /* load_all */,
2698 &ls->thread_args.commits_needed,
2699 &ls->thread_args.log_complete,
2700 &ls->thread_args.need_commits);
2704 err = input_log_view(NULL, NULL, NULL, s->log_view,
2709 entry = TAILQ_NEXT(ls->selected_entry, entry);
2713 err = set_selected_commit(s, entry);
2717 s->first_displayed_line = 1;
2718 s->last_displayed_line = view->nlines;
2720 diff_view_indicate_progress(view);
2721 err = create_diff(s);
2730 static const struct got_error *
2731 cmd_diff(int argc, char *argv[])
2733 const struct got_error *error = NULL;
2734 struct got_repository *repo = NULL;
2735 struct got_reflist_head refs;
2736 struct got_object_id *id1 = NULL, *id2 = NULL;
2737 char *repo_path = NULL;
2738 char *id_str1 = NULL, *id_str2 = NULL;
2740 struct tog_view *view;
2742 SIMPLEQ_INIT(&refs);
2745 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2750 while ((ch = getopt(argc, argv, "")) != -1) {
2762 usage_diff(); /* TODO show local worktree changes */
2763 } else if (argc == 2) {
2764 repo_path = getcwd(NULL, 0);
2765 if (repo_path == NULL)
2766 return got_error_from_errno("getcwd");
2769 } else if (argc == 3) {
2770 repo_path = realpath(argv[0], NULL);
2771 if (repo_path == NULL)
2772 return got_error_from_errno2("realpath", argv[0]);
2780 error = got_repo_open(&repo, repo_path);
2784 error = apply_unveil(got_repo_get_path(repo), NULL);
2788 error = got_object_resolve_id_str(&id1, repo, id_str1);
2792 error = got_object_resolve_id_str(&id2, repo, id_str2);
2796 error = got_ref_list(&refs, repo);
2800 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2802 error = got_error_from_errno("view_open");
2805 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2808 error = view_loop(view);
2811 got_repo_close(repo);
2812 got_ref_list_free(&refs);
2820 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2825 struct tog_blame_line {
2827 struct got_object_id *id;
2830 static const struct got_error *
2831 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2832 const char *path, struct tog_blame_line *lines, int nlines,
2833 int blame_complete, int selected_line, int *first_displayed_line,
2834 int *last_displayed_line, int *eof, int max_lines)
2836 const struct got_error *err;
2837 int lineno = 0, nprinted = 0;
2842 struct tog_blame_line *blame_line;
2843 struct got_object_id *prev_id = NULL;
2846 err = got_object_id_str(&id_str, id);
2851 werase(view->window);
2853 if (asprintf(&line, "commit %s", id_str) == -1) {
2854 err = got_error_from_errno("asprintf");
2859 err = format_line(&wline, &width, line, view->ncols);
2862 if (view_needs_focus_indication(view))
2863 wstandout(view->window);
2864 waddwstr(view->window, wline);
2865 if (view_needs_focus_indication(view))
2866 wstandend(view->window);
2869 if (width < view->ncols - 1)
2870 waddch(view->window, '\n');
2872 if (asprintf(&line, "[%d/%d] %s%s",
2873 *first_displayed_line - 1 + selected_line, nlines,
2874 blame_complete ? "" : "annotating... ", path) == -1) {
2876 return got_error_from_errno("asprintf");
2879 err = format_line(&wline, &width, line, view->ncols);
2884 waddwstr(view->window, wline);
2887 if (width < view->ncols - 1)
2888 waddch(view->window, '\n');
2891 while (nprinted < max_lines - 2) {
2892 line = parse_next_line(f, &len);
2897 if (++lineno < *first_displayed_line) {
2902 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2903 err = format_line(&wline, &width, line, wlimit);
2909 if (view->focussed && nprinted == selected_line - 1)
2910 wstandout(view->window);
2912 blame_line = &lines[lineno - 1];
2913 if (blame_line->annotated && prev_id &&
2914 got_object_id_cmp(prev_id, blame_line->id) == 0)
2915 waddstr(view->window, " ");
2916 else if (blame_line->annotated) {
2918 err = got_object_id_str(&id_str, blame_line->id);
2924 wprintw(view->window, "%.8s ", id_str);
2926 prev_id = blame_line->id;
2928 waddstr(view->window, "........ ");
2932 waddwstr(view->window, wline);
2933 while (width < wlimit) {
2934 waddch(view->window, ' ');
2937 if (view->focussed && nprinted == selected_line - 1)
2938 wstandend(view->window);
2939 if (++nprinted == 1)
2940 *first_displayed_line = lineno;
2945 *last_displayed_line = lineno;
2952 static const struct got_error *
2953 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2955 const struct got_error *err = NULL;
2956 struct tog_blame_cb_args *a = arg;
2957 struct tog_blame_line *line;
2960 if (nlines != a->nlines ||
2961 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2962 return got_error(GOT_ERR_RANGE);
2964 errcode = pthread_mutex_lock(&tog_mutex);
2966 return got_error_set_errno(errcode, "pthread_mutex_lock");
2968 if (*a->quit) { /* user has quit the blame view */
2969 err = got_error(GOT_ERR_ITER_COMPLETED);
2974 goto done; /* no change in this commit */
2976 line = &a->lines[lineno - 1];
2977 if (line->annotated)
2980 line->id = got_object_id_dup(id);
2981 if (line->id == NULL) {
2982 err = got_error_from_errno("got_object_id_dup");
2985 line->annotated = 1;
2987 errcode = pthread_mutex_unlock(&tog_mutex);
2989 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2994 blame_thread(void *arg)
2996 const struct got_error *err;
2997 struct tog_blame_thread_args *ta = arg;
2998 struct tog_blame_cb_args *a = ta->cb_args;
3001 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3002 blame_cb, ta->cb_args);
3004 errcode = pthread_mutex_lock(&tog_mutex);
3006 return (void *)got_error_set_errno(errcode,
3007 "pthread_mutex_lock");
3009 got_repo_close(ta->repo);
3013 errcode = pthread_mutex_unlock(&tog_mutex);
3014 if (errcode && err == NULL)
3015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3020 static struct got_object_id *
3021 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3024 struct tog_blame_line *line;
3026 line = &lines[first_displayed_line - 1 + selected_line - 1];
3027 if (!line->annotated)
3033 static const struct got_error *
3034 stop_blame(struct tog_blame *blame)
3036 const struct got_error *err = NULL;
3039 if (blame->thread) {
3041 errcode = pthread_mutex_unlock(&tog_mutex);
3043 return got_error_set_errno(errcode,
3044 "pthread_mutex_unlock");
3045 errcode = pthread_join(blame->thread, (void **)&err);
3047 return got_error_set_errno(errcode, "pthread_join");
3048 errcode = pthread_mutex_lock(&tog_mutex);
3050 return got_error_set_errno(errcode,
3051 "pthread_mutex_lock");
3052 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3054 blame->thread = NULL;
3056 if (blame->thread_args.repo) {
3057 got_repo_close(blame->thread_args.repo);
3058 blame->thread_args.repo = NULL;
3061 if (fclose(blame->f) != 0 && err == NULL)
3062 err = got_error_from_errno("fclose");
3066 for (i = 0; i < blame->nlines; i++)
3067 free(blame->lines[i].id);
3069 blame->lines = NULL;
3071 free(blame->cb_args.commit_id);
3072 blame->cb_args.commit_id = NULL;
3077 static const struct got_error *
3078 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3079 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3080 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3081 struct got_repository *repo)
3083 const struct got_error *err = NULL;
3084 struct got_blob_object *blob = NULL;
3085 struct got_repository *thread_repo = NULL;
3086 struct got_object_id *obj_id = NULL;
3089 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3093 return got_error(GOT_ERR_NO_OBJ);
3095 err = got_object_get_type(&obj_type, repo, obj_id);
3099 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3100 err = got_error(GOT_ERR_OBJ_TYPE);
3104 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3107 blame->f = got_opentemp();
3108 if (blame->f == NULL) {
3109 err = got_error_from_errno("got_opentemp");
3112 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3117 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3118 if (blame->lines == NULL) {
3119 err = got_error_from_errno("calloc");
3123 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3127 blame->cb_args.view = view;
3128 blame->cb_args.lines = blame->lines;
3129 blame->cb_args.nlines = blame->nlines;
3130 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3131 if (blame->cb_args.commit_id == NULL) {
3132 err = got_error_from_errno("got_object_id_dup");
3135 blame->cb_args.quit = done;
3137 blame->thread_args.path = path;
3138 blame->thread_args.repo = thread_repo;
3139 blame->thread_args.cb_args = &blame->cb_args;
3140 blame->thread_args.complete = blame_complete;
3141 *blame_complete = 0;
3145 got_object_blob_close(blob);
3152 static const struct got_error *
3153 open_blame_view(struct tog_view *view, char *path,
3154 struct got_object_id *commit_id, struct got_reflist_head *refs,
3155 struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 struct tog_blame_view_state *s = &view->state.blame;
3160 SIMPLEQ_INIT(&s->blamed_commits);
3162 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3166 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3167 s->first_displayed_line = 1;
3168 s->last_displayed_line = view->nlines;
3169 s->selected_line = 1;
3170 s->blame_complete = 0;
3172 if (s->path == NULL)
3173 return got_error_from_errno("open_blame_view");
3176 s->commit_id = commit_id;
3177 memset(&s->blame, 0, sizeof(s->blame));
3179 view->show = show_blame_view;
3180 view->input = input_blame_view;
3181 view->close = close_blame_view;
3183 return run_blame(&s->blame, view, &s->blame_complete,
3184 &s->first_displayed_line, &s->last_displayed_line,
3185 &s->selected_line, &s->done, &s->eof, s->path,
3186 s->blamed_commit->id, s->repo);
3189 static const struct got_error *
3190 close_blame_view(struct tog_view *view)
3192 const struct got_error *err = NULL;
3193 struct tog_blame_view_state *s = &view->state.blame;
3195 if (s->blame.thread)
3196 err = stop_blame(&s->blame);
3198 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3199 struct got_object_qid *blamed_commit;
3200 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3201 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3202 got_object_qid_free(blamed_commit);
3210 static const struct got_error *
3211 show_blame_view(struct tog_view *view)
3213 const struct got_error *err = NULL;
3214 struct tog_blame_view_state *s = &view->state.blame;
3217 if (s->blame.thread == NULL) {
3218 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3219 &s->blame.thread_args);
3221 return got_error_set_errno(errcode, "pthread_create");
3224 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3225 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3226 s->selected_line, &s->first_displayed_line,
3227 &s->last_displayed_line, &s->eof, view->nlines);
3233 static const struct got_error *
3234 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3235 struct tog_view **focus_view, struct tog_view *view, int ch)
3237 const struct got_error *err = NULL, *thread_err = NULL;
3238 struct tog_view *diff_view;
3239 struct tog_blame_view_state *s = &view->state.blame;
3248 if (s->selected_line > 1)
3250 else if (s->selected_line == 1 &&
3251 s->first_displayed_line > 1)
3252 s->first_displayed_line--;
3255 if (s->first_displayed_line == 1) {
3256 s->selected_line = 1;
3259 if (s->first_displayed_line > view->nlines - 2)
3260 s->first_displayed_line -=
3263 s->first_displayed_line = 1;
3267 if (s->selected_line < view->nlines - 2 &&
3268 s->first_displayed_line +
3269 s->selected_line <= s->blame.nlines)
3271 else if (s->last_displayed_line <
3273 s->first_displayed_line++;
3277 struct got_object_id *id = NULL;
3278 id = get_selected_commit_id(s->blame.lines,
3279 s->first_displayed_line, s->selected_line);
3283 struct got_commit_object *commit;
3284 struct got_object_qid *pid;
3285 struct got_object_id *blob_id = NULL;
3287 err = got_object_open_as_commit(&commit,
3291 pid = SIMPLEQ_FIRST(
3292 got_object_commit_get_parent_ids(commit));
3294 got_object_commit_close(commit);
3297 /* Check if path history ends here. */
3298 err = got_object_id_by_path(&blob_id, s->repo,
3301 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3303 got_object_commit_close(commit);
3306 err = got_object_get_type(&obj_type, s->repo,
3309 /* Can't blame non-blob type objects. */
3310 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3311 got_object_commit_close(commit);
3314 err = got_object_qid_alloc(&s->blamed_commit,
3316 got_object_commit_close(commit);
3318 if (got_object_id_cmp(id,
3319 s->blamed_commit->id) == 0)
3321 err = got_object_qid_alloc(&s->blamed_commit,
3327 thread_err = stop_blame(&s->blame);
3331 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3332 s->blamed_commit, entry);
3333 err = run_blame(&s->blame, view, &s->blame_complete,
3334 &s->first_displayed_line, &s->last_displayed_line,
3335 &s->selected_line, &s->done, &s->eof,
3336 s->path, s->blamed_commit->id, s->repo);
3342 struct got_object_qid *first;
3343 first = SIMPLEQ_FIRST(&s->blamed_commits);
3344 if (!got_object_id_cmp(first->id, s->commit_id))
3347 thread_err = stop_blame(&s->blame);
3351 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3352 got_object_qid_free(s->blamed_commit);
3354 SIMPLEQ_FIRST(&s->blamed_commits);
3355 err = run_blame(&s->blame, view, &s->blame_complete,
3356 &s->first_displayed_line, &s->last_displayed_line,
3357 &s->selected_line, &s->done, &s->eof, s->path,
3358 s->blamed_commit->id, s->repo);
3365 struct got_object_id *id = NULL;
3366 struct got_object_qid *pid;
3367 struct got_commit_object *commit = NULL;
3368 id = get_selected_commit_id(s->blame.lines,
3369 s->first_displayed_line, s->selected_line);
3372 err = got_object_open_as_commit(&commit, s->repo, id);
3375 pid = SIMPLEQ_FIRST(
3376 got_object_commit_get_parent_ids(commit));
3377 if (view_is_parent_view(view))
3378 begin_x = view_split_begin_x(view->begin_x);
3379 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3380 if (diff_view == NULL) {
3381 got_object_commit_close(commit);
3382 err = got_error_from_errno("view_open");
3385 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3386 id, NULL, s->refs, s->repo);
3387 got_object_commit_close(commit);
3389 view_close(diff_view);
3392 if (view_is_parent_view(view)) {
3393 err = view_close_child(view);
3396 err = view_set_child(view, diff_view);
3398 view_close(diff_view);
3401 *focus_view = diff_view;
3402 view->child_focussed = 1;
3404 *new_view = diff_view;
3411 if (s->last_displayed_line >= s->blame.nlines &&
3412 s->selected_line >= MIN(s->blame.nlines,
3413 view->nlines - 2)) {
3416 if (s->last_displayed_line >= s->blame.nlines &&
3417 s->selected_line < view->nlines - 2) {
3418 s->selected_line = MIN(s->blame.nlines,
3422 if (s->last_displayed_line + view->nlines - 2
3424 s->first_displayed_line +=
3427 s->first_displayed_line =
3432 if (s->selected_line > view->nlines - 2) {
3433 s->selected_line = MIN(s->blame.nlines,
3440 return thread_err ? thread_err : err;
3443 static const struct got_error *
3444 cmd_blame(int argc, char *argv[])
3446 const struct got_error *error;
3447 struct got_repository *repo = NULL;
3448 struct got_reflist_head refs;
3449 struct got_worktree *worktree = NULL;
3450 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3451 struct got_object_id *commit_id = NULL;
3452 char *commit_id_str = NULL;
3454 struct tog_view *view;
3456 SIMPLEQ_INIT(&refs);
3459 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3464 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3467 commit_id_str = optarg;
3470 repo_path = realpath(optarg, NULL);
3471 if (repo_path == NULL)
3472 err(1, "-r option");
3488 cwd = getcwd(NULL, 0);
3490 error = got_error_from_errno("getcwd");
3493 if (repo_path == NULL) {
3494 error = got_worktree_open(&worktree, cwd);
3495 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3501 strdup(got_worktree_get_repo_path(worktree));
3502 if (repo_path == NULL)
3503 error = got_error_from_errno("strdup");
3507 repo_path = strdup(cwd);
3508 if (repo_path == NULL) {
3509 error = got_error_from_errno("strdup");
3517 error = got_repo_open(&repo, repo_path);
3521 error = apply_unveil(got_repo_get_path(repo), NULL);
3526 const char *prefix = got_worktree_get_path_prefix(worktree);
3527 char *p, *worktree_subdir = cwd +
3528 strlen(got_worktree_get_root_path(worktree));
3529 if (asprintf(&p, "%s%s%s%s%s",
3530 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3531 worktree_subdir, worktree_subdir[0] ? "/" : "",
3533 error = got_error_from_errno("asprintf");
3536 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3539 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3544 if (commit_id_str == NULL) {
3545 struct got_reference *head_ref;
3546 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3549 error = got_ref_resolve(&commit_id, repo, head_ref);
3550 got_ref_close(head_ref);
3552 error = got_object_resolve_id_str(&commit_id, repo,
3558 error = got_ref_list(&refs, repo);
3562 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3564 error = got_error_from_errno("view_open");
3567 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3570 error = view_loop(view);
3576 got_worktree_close(worktree);
3578 got_repo_close(repo);
3579 got_ref_list_free(&refs);
3583 static const struct got_error *
3584 draw_tree_entries(struct tog_view *view,
3585 struct got_tree_entry **first_displayed_entry,
3586 struct got_tree_entry **last_displayed_entry,
3587 struct got_tree_entry **selected_entry, int *ndisplayed,
3588 const char *label, int show_ids, const char *parent_path,
3589 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3591 const struct got_error *err = NULL;
3592 struct got_tree_entry *te;
3598 werase(view->window);
3603 err = format_line(&wline, &width, label, view->ncols);
3606 if (view_needs_focus_indication(view))
3607 wstandout(view->window);
3608 waddwstr(view->window, wline);
3609 if (view_needs_focus_indication(view))
3610 wstandend(view->window);
3613 if (width < view->ncols - 1)
3614 waddch(view->window, '\n');
3617 err = format_line(&wline, &width, parent_path, view->ncols);
3620 waddwstr(view->window, wline);
3623 if (width < view->ncols - 1)
3624 waddch(view->window, '\n');
3627 waddch(view->window, '\n');
3631 te = SIMPLEQ_FIRST(&entries->head);
3632 if (*first_displayed_entry == NULL) {
3633 if (selected == 0) {
3635 wstandout(view->window);
3636 *selected_entry = NULL;
3638 waddstr(view->window, " ..\n"); /* parent directory */
3639 if (selected == 0 && view->focussed)
3640 wstandend(view->window);
3647 while (te != *first_displayed_entry)
3648 te = SIMPLEQ_NEXT(te, entry);
3652 char *line = NULL, *id_str = NULL;
3655 err = got_object_id_str(&id_str, te->id);
3657 return got_error_from_errno(
3658 "got_object_id_str");
3660 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3661 te->name, S_ISDIR(te->mode) ? "/" :
3662 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3664 return got_error_from_errno("asprintf");
3667 err = format_line(&wline, &width, line, view->ncols);
3672 if (n == selected) {
3674 wstandout(view->window);
3675 *selected_entry = te;
3677 waddwstr(view->window, wline);
3678 if (width < view->ncols - 1)
3679 waddch(view->window, '\n');
3680 if (n == selected && view->focussed)
3681 wstandend(view->window);
3687 *last_displayed_entry = te;
3690 te = SIMPLEQ_NEXT(te, entry);
3697 tree_scroll_up(struct tog_view *view,
3698 struct got_tree_entry **first_displayed_entry, int maxscroll,
3699 const struct got_tree_entries *entries, int isroot)
3701 struct got_tree_entry *te, *prev;
3704 if (*first_displayed_entry == NULL)
3707 te = SIMPLEQ_FIRST(&entries->head);
3708 if (*first_displayed_entry == te) {
3710 *first_displayed_entry = NULL;
3714 /* XXX this is stupid... switch to TAILQ? */
3715 for (i = 0; i < maxscroll; i++) {
3716 while (te != *first_displayed_entry) {
3718 te = SIMPLEQ_NEXT(te, entry);
3720 *first_displayed_entry = prev;
3721 te = SIMPLEQ_FIRST(&entries->head);
3723 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3724 *first_displayed_entry = NULL;
3728 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3729 struct got_tree_entry *last_displayed_entry,
3730 const struct got_tree_entries *entries)
3732 struct got_tree_entry *next, *last;
3735 if (*first_displayed_entry)
3736 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3738 next = SIMPLEQ_FIRST(&entries->head);
3739 last = last_displayed_entry;
3740 while (next && last && n++ < maxscroll) {
3741 last = SIMPLEQ_NEXT(last, entry);
3743 *first_displayed_entry = next;
3744 next = SIMPLEQ_NEXT(next, entry);
3750 static const struct got_error *
3751 tree_entry_path(char **path, struct tog_parent_trees *parents,
3752 struct got_tree_entry *te)
3754 const struct got_error *err = NULL;
3755 struct tog_parent_tree *pt;
3756 size_t len = 2; /* for leading slash and NUL */
3758 TAILQ_FOREACH(pt, parents, entry)
3759 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3761 len += strlen(te->name);
3763 *path = calloc(1, len);
3765 return got_error_from_errno("calloc");
3768 pt = TAILQ_LAST(parents, tog_parent_trees);
3770 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3771 err = got_error(GOT_ERR_NO_SPACE);
3774 if (strlcat(*path, "/", len) >= len) {
3775 err = got_error(GOT_ERR_NO_SPACE);
3778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3781 if (strlcat(*path, te->name, len) >= len) {
3782 err = got_error(GOT_ERR_NO_SPACE);
3794 static const struct got_error *
3795 blame_tree_entry(struct tog_view **new_view, int begin_x,
3796 struct got_tree_entry *te, struct tog_parent_trees *parents,
3797 struct got_object_id *commit_id, struct got_reflist_head *refs,
3798 struct got_repository *repo)
3800 const struct got_error *err = NULL;
3802 struct tog_view *blame_view;
3804 err = tree_entry_path(&path, parents, te);
3808 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3809 if (blame_view == NULL)
3810 return got_error_from_errno("view_open");
3812 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3814 view_close(blame_view);
3817 *new_view = blame_view;
3821 static const struct got_error *
3822 log_tree_entry(struct tog_view **new_view, int begin_x,
3823 struct got_tree_entry *te, struct tog_parent_trees *parents,
3824 struct got_object_id *commit_id, struct got_reflist_head *refs,
3825 struct got_repository *repo)
3827 struct tog_view *log_view;
3828 const struct got_error *err = NULL;
3831 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3832 if (log_view == NULL)
3833 return got_error_from_errno("view_open");
3835 err = tree_entry_path(&path, parents, te);
3839 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3841 view_close(log_view);
3843 *new_view = log_view;
3848 static const struct got_error *
3849 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3850 struct got_object_id *commit_id, struct got_reflist_head *refs,
3851 struct got_repository *repo)
3853 const struct got_error *err = NULL;
3854 char *commit_id_str = NULL;
3855 struct tog_tree_view_state *s = &view->state.tree;
3857 TAILQ_INIT(&s->parents);
3859 err = got_object_id_str(&commit_id_str, commit_id);
3863 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3864 err = got_error_from_errno("asprintf");
3868 s->root = s->tree = root;
3869 s->entries = got_object_tree_get_entries(root);
3870 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3871 s->commit_id = got_object_id_dup(commit_id);
3872 if (s->commit_id == NULL) {
3873 err = got_error_from_errno("got_object_id_dup");
3879 view->show = show_tree_view;
3880 view->input = input_tree_view;
3881 view->close = close_tree_view;
3883 free(commit_id_str);
3885 free(s->tree_label);
3886 s->tree_label = NULL;
3891 static const struct got_error *
3892 close_tree_view(struct tog_view *view)
3894 struct tog_tree_view_state *s = &view->state.tree;
3896 free(s->tree_label);
3897 s->tree_label = NULL;
3899 s->commit_id = NULL;
3900 while (!TAILQ_EMPTY(&s->parents)) {
3901 struct tog_parent_tree *parent;
3902 parent = TAILQ_FIRST(&s->parents);
3903 TAILQ_REMOVE(&s->parents, parent, entry);
3907 if (s->tree != s->root)
3908 got_object_tree_close(s->tree);
3909 got_object_tree_close(s->root);
3914 static const struct got_error *
3915 show_tree_view(struct tog_view *view)
3917 const struct got_error *err = NULL;
3918 struct tog_tree_view_state *s = &view->state.tree;
3921 err = tree_entry_path(&parent_path, &s->parents, NULL);
3925 err = draw_tree_entries(view, &s->first_displayed_entry,
3926 &s->last_displayed_entry, &s->selected_entry,
3927 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3928 s->entries, s->selected, view->nlines, s->tree == s->root);
3935 static const struct got_error *
3936 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3937 struct tog_view **focus_view, struct tog_view *view, int ch)
3939 const struct got_error *err = NULL;
3940 struct tog_tree_view_state *s = &view->state.tree;
3941 struct tog_view *log_view;
3942 int begin_x = 0, nscrolled;
3946 s->show_ids = !s->show_ids;
3949 if (!s->selected_entry)
3951 if (view_is_parent_view(view))
3952 begin_x = view_split_begin_x(view->begin_x);
3953 err = log_tree_entry(&log_view, begin_x,
3954 s->selected_entry, &s->parents,
3955 s->commit_id, s->refs, s->repo);
3956 if (view_is_parent_view(view)) {
3957 err = view_close_child(view);
3960 err = view_set_child(view, log_view);
3962 view_close(log_view);
3965 *focus_view = log_view;
3966 view->child_focussed = 1;
3968 *new_view = log_view;
3972 if (s->selected > 0) {
3974 if (s->selected == 0)
3977 if (s->selected > 0)
3979 tree_scroll_up(view, &s->first_displayed_entry, 1,
3980 s->entries, s->tree == s->root);
3983 tree_scroll_up(view, &s->first_displayed_entry,
3984 MAX(0, view->nlines - 4 - s->selected), s->entries,
3985 s->tree == s->root);
3987 if (SIMPLEQ_FIRST(&s->entries->head) ==
3988 s->first_displayed_entry && s->tree != s->root)
3989 s->first_displayed_entry = NULL;
3993 if (s->selected < s->ndisplayed - 1) {
3997 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3998 /* can't scroll any further */
4000 tree_scroll_down(&s->first_displayed_entry, 1,
4001 s->last_displayed_entry, s->entries);
4004 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4006 /* can't scroll any further; move cursor down */
4007 if (s->selected < s->ndisplayed - 1)
4008 s->selected = s->ndisplayed - 1;
4011 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4012 view->nlines, s->last_displayed_entry, s->entries);
4013 if (nscrolled < view->nlines) {
4015 struct got_tree_entry *te;
4016 te = s->first_displayed_entry;
4019 te = SIMPLEQ_NEXT(te, entry);
4021 s->selected = ndisplayed - 1;
4027 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4028 struct tog_parent_tree *parent;
4029 /* user selected '..' */
4030 if (s->tree == s->root)
4032 parent = TAILQ_FIRST(&s->parents);
4033 TAILQ_REMOVE(&s->parents, parent,
4035 got_object_tree_close(s->tree);
4036 s->tree = parent->tree;
4038 got_object_tree_get_entries(s->tree);
4039 s->first_displayed_entry =
4040 parent->first_displayed_entry;
4042 parent->selected_entry;
4043 s->selected = parent->selected;
4045 } else if (S_ISDIR(s->selected_entry->mode)) {
4046 struct got_tree_object *subtree;
4047 err = got_object_open_as_tree(&subtree,
4048 s->repo, s->selected_entry->id);
4051 err = tree_view_visit_subtree(subtree, s);
4053 got_object_tree_close(subtree);
4056 } else if (S_ISREG(s->selected_entry->mode)) {
4057 struct tog_view *blame_view;
4058 int begin_x = view_is_parent_view(view) ?
4059 view_split_begin_x(view->begin_x) : 0;
4061 err = blame_tree_entry(&blame_view, begin_x,
4062 s->selected_entry, &s->parents,
4063 s->commit_id, s->refs, s->repo);
4066 if (view_is_parent_view(view)) {
4067 err = view_close_child(view);
4070 err = view_set_child(view, blame_view);
4072 view_close(blame_view);
4075 *focus_view = blame_view;
4076 view->child_focussed = 1;
4078 *new_view = blame_view;
4082 if (s->selected > view->nlines)
4083 s->selected = s->ndisplayed - 1;
4096 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4101 static const struct got_error *
4102 cmd_tree(int argc, char *argv[])
4104 const struct got_error *error;
4105 struct got_repository *repo = NULL;
4106 struct got_reflist_head refs;
4107 char *repo_path = NULL;
4108 struct got_object_id *commit_id = NULL;
4109 char *commit_id_arg = NULL;
4110 struct got_commit_object *commit = NULL;
4111 struct got_tree_object *tree = NULL;
4113 struct tog_view *view;
4115 SIMPLEQ_INIT(&refs);
4118 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4123 while ((ch = getopt(argc, argv, "c:")) != -1) {
4126 commit_id_arg = optarg;
4138 struct got_worktree *worktree;
4139 char *cwd = getcwd(NULL, 0);
4141 return got_error_from_errno("getcwd");
4142 error = got_worktree_open(&worktree, cwd);
4143 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4148 strdup(got_worktree_get_repo_path(worktree));
4149 got_worktree_close(worktree);
4152 if (repo_path == NULL) {
4153 error = got_error_from_errno("strdup");
4156 } else if (argc == 1) {
4157 repo_path = realpath(argv[0], NULL);
4158 if (repo_path == NULL)
4159 return got_error_from_errno2("realpath", argv[0]);
4165 error = got_repo_open(&repo, repo_path);
4169 error = apply_unveil(got_repo_get_path(repo), NULL);
4173 if (commit_id_arg == NULL)
4174 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4176 error = got_object_resolve_id_str(&commit_id, repo,
4181 error = got_object_open_as_commit(&commit, repo, commit_id);
4185 error = got_object_open_as_tree(&tree, repo,
4186 got_object_commit_get_tree_id(commit));
4190 error = got_ref_list(&refs, repo);
4194 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4196 error = got_error_from_errno("view_open");
4199 error = open_tree_view(view, tree, commit_id, &refs, repo);
4202 error = view_loop(view);
4207 got_object_commit_close(commit);
4209 got_object_tree_close(tree);
4211 got_repo_close(repo);
4212 got_ref_list_free(&refs);
4221 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4222 "Available commands:\n", getprogname());
4223 for (i = 0; i < nitems(tog_commands); i++) {
4224 struct tog_cmd *cmd = &tog_commands[i];
4225 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4231 make_argv(const char *arg0, const char *arg1)
4234 int argc = (arg1 == NULL ? 1 : 2);
4236 argv = calloc(argc, sizeof(char *));
4239 argv[0] = strdup(arg0);
4240 if (argv[0] == NULL)
4243 argv[1] = strdup(arg1);
4244 if (argv[1] == NULL)
4252 main(int argc, char *argv[])
4254 const struct got_error *error = NULL;
4255 struct tog_cmd *cmd = NULL;
4257 char **cmd_argv = NULL;
4259 setlocale(LC_CTYPE, "");
4261 while ((ch = getopt(argc, argv, "h")) != -1) {
4280 /* Build an argument vector which runs a default command. */
4281 cmd = &tog_commands[0];
4282 cmd_argv = make_argv(cmd->name, NULL);
4287 /* Did the user specific a command? */
4288 for (i = 0; i < nitems(tog_commands); i++) {
4289 if (strncmp(tog_commands[i].name, argv[0],
4290 strlen(argv[0])) == 0) {
4291 cmd = &tog_commands[i];
4293 tog_commands[i].cmd_usage();
4298 /* Did the user specify a repository? */
4299 char *repo_path = realpath(argv[0], NULL);
4301 struct got_repository *repo;
4302 error = got_repo_open(&repo, repo_path);
4304 got_repo_close(repo);
4306 error = got_error_from_errno2("realpath",
4310 fprintf(stderr, "%s: '%s' is not a "
4311 "known command\n", getprogname(),
4315 fprintf(stderr, "%s: '%s' is neither a known "
4316 "command nor a path to a repository\n",
4317 getprogname(), argv[0]);
4321 cmd = &tog_commands[0];
4322 cmd_argv = make_argv(cmd->name, repo_path);
4328 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4335 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);