2 * Copyright (c) 2018, 2019, 2020 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
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_opentemp.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
55 #include "got_worktree.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #define CTRL(x) ((x) & 0x1f)
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
113 struct commit_queue_head head;
117 SIMPLEQ_ENTRY(tog_color) entry;
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
222 if (strcasecmp(val, "red") == 0)
224 if (strcasecmp(val, "green") == 0)
226 if (strcasecmp(val, "yellow") == 0)
228 if (strcasecmp(val, "blue") == 0)
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
234 if (strcasecmp(val, "white") == 0)
236 if (strcasecmp(val, "default") == 0)
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
246 int first_displayed_line;
247 int last_displayed_line;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
259 /* passed from log view; may be NULL */
260 struct tog_view *log_view;
263 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 struct tog_log_thread_args {
266 pthread_cond_t need_commits;
268 struct got_commit_graph *graph;
269 struct commit_queue *commits;
270 const char *in_repo_path;
271 struct got_object_id *start_id;
272 struct got_repository *repo;
275 struct commit_queue_entry **first_displayed_entry;
276 struct commit_queue_entry **selected_entry;
278 int *search_next_done;
282 struct tog_log_view_state {
283 struct commit_queue commits;
284 struct commit_queue_entry *first_displayed_entry;
285 struct commit_queue_entry *last_displayed_entry;
286 struct commit_queue_entry *selected_entry;
289 const char *head_ref_name;
291 struct got_repository *repo;
292 struct got_reflist_head *refs;
293 struct got_object_id *start_id;
296 struct tog_log_thread_args thread_args;
297 struct commit_queue_entry *matched_entry;
298 struct commit_queue_entry *search_entry;
299 struct tog_colors colors;
302 #define TOG_COLOR_DIFF_MINUS 1
303 #define TOG_COLOR_DIFF_PLUS 2
304 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 #define TOG_COLOR_DIFF_META 4
306 #define TOG_COLOR_TREE_SUBMODULE 5
307 #define TOG_COLOR_TREE_SYMLINK 6
308 #define TOG_COLOR_TREE_DIRECTORY 7
309 #define TOG_COLOR_TREE_EXECUTABLE 8
310 #define TOG_COLOR_COMMIT 9
311 #define TOG_COLOR_AUTHOR 10
312 #define TOG_COLOR_DATE 11
314 struct tog_blame_cb_args {
315 struct tog_blame_line *lines; /* one per line */
318 struct tog_view *view;
319 struct got_object_id *commit_id;
323 struct tog_blame_thread_args {
325 struct got_repository *repo;
326 struct tog_blame_cb_args *cb_args;
328 got_cancel_cb cancel_cb;
335 struct tog_blame_line *lines;
339 struct tog_blame_thread_args thread_args;
340 struct tog_blame_cb_args cb_args;
344 struct tog_blame_view_state {
345 int first_displayed_line;
346 int last_displayed_line;
351 struct got_object_id_queue blamed_commits;
352 struct got_object_qid *blamed_commit;
354 struct got_repository *repo;
355 struct got_reflist_head *refs;
356 struct got_object_id *commit_id;
357 struct tog_blame blame;
359 struct tog_colors colors;
362 struct tog_parent_tree {
363 TAILQ_ENTRY(tog_parent_tree) entry;
364 struct got_tree_object *tree;
365 struct got_tree_entry *first_displayed_entry;
366 struct got_tree_entry *selected_entry;
370 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 struct tog_tree_view_state {
374 struct got_tree_object *root;
375 struct got_tree_object *tree;
376 struct got_tree_entry *first_displayed_entry;
377 struct got_tree_entry *last_displayed_entry;
378 struct got_tree_entry *selected_entry;
379 int ndisplayed, selected, show_ids;
380 struct tog_parent_trees parents;
381 struct got_object_id *commit_id;
382 struct got_repository *repo;
383 struct got_reflist_head *refs;
384 struct got_tree_entry *matched_entry;
385 struct tog_colors colors;
389 * We implement two types of views: parent views and child views.
391 * The 'Tab' key switches between a parent view and its child view.
392 * Child views are shown side-by-side to their parent view, provided
393 * there is enough screen estate.
395 * When a new view is opened from within a parent view, this new view
396 * becomes a child view of the parent view, replacing any existing child.
398 * When a new view is opened from within a child view, this new view
399 * becomes a parent view which will obscure the views below until the
400 * user quits the new parent view by typing 'q'.
402 * This list of views contains parent views only.
403 * Child views are only pointed to by their parent view.
405 TAILQ_HEAD(tog_view_list_head, tog_view);
408 TAILQ_ENTRY(tog_view) entry;
411 int nlines, ncols, begin_y, begin_x;
412 int lines, cols; /* copies of LINES and COLS */
414 struct tog_view *parent;
415 struct tog_view *child;
418 /* type-specific state */
419 enum tog_view_type type;
421 struct tog_diff_view_state diff;
422 struct tog_log_view_state log;
423 struct tog_blame_view_state blame;
424 struct tog_tree_view_state tree;
427 const struct got_error *(*show)(struct tog_view *);
428 const struct got_error *(*input)(struct tog_view **,
429 struct tog_view **, struct tog_view**, struct tog_view *, int);
430 const struct got_error *(*close)(struct tog_view *);
432 const struct got_error *(*search_start)(struct tog_view *);
433 const struct got_error *(*search_next)(struct tog_view *);
435 #define TOG_SEARCH_FORWARD 1
436 #define TOG_SEARCH_BACKWARD 2
437 int search_next_done;
441 static const struct got_error *open_diff_view(struct tog_view *,
442 struct got_object_id *, struct got_object_id *, struct tog_view *,
443 struct got_reflist_head *, struct got_repository *);
444 static const struct got_error *show_diff_view(struct tog_view *);
445 static const struct got_error *input_diff_view(struct tog_view **,
446 struct tog_view **, struct tog_view **, struct tog_view *, int);
447 static const struct got_error* close_diff_view(struct tog_view *);
448 static const struct got_error *search_start_diff_view(struct tog_view *);
449 static const struct got_error *search_next_diff_view(struct tog_view *);
451 static const struct got_error *open_log_view(struct tog_view *,
452 struct got_object_id *, struct got_reflist_head *,
453 struct got_repository *, const char *, const char *, int, int);
454 static const struct got_error * show_log_view(struct tog_view *);
455 static const struct got_error *input_log_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_log_view(struct tog_view *);
458 static const struct got_error *search_start_log_view(struct tog_view *);
459 static const struct got_error *search_next_log_view(struct tog_view *);
461 static const struct got_error *open_blame_view(struct tog_view *, char *,
462 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 static const struct got_error *show_blame_view(struct tog_view *);
464 static const struct got_error *input_blame_view(struct tog_view **,
465 struct tog_view **, struct tog_view **, struct tog_view *, int);
466 static const struct got_error *close_blame_view(struct tog_view *);
467 static const struct got_error *search_start_blame_view(struct tog_view *);
468 static const struct got_error *search_next_blame_view(struct tog_view *);
470 static const struct got_error *open_tree_view(struct tog_view *,
471 struct got_tree_object *, struct got_object_id *,
472 struct got_reflist_head *, struct got_repository *);
473 static const struct got_error *show_tree_view(struct tog_view *);
474 static const struct got_error *input_tree_view(struct tog_view **,
475 struct tog_view **, struct tog_view **, struct tog_view *, int);
476 static const struct got_error *close_tree_view(struct tog_view *);
477 static const struct got_error *search_start_tree_view(struct tog_view *);
478 static const struct got_error *search_next_tree_view(struct tog_view *);
480 static volatile sig_atomic_t tog_sigwinch_received;
481 static volatile sig_atomic_t tog_sigpipe_received;
482 static volatile sig_atomic_t tog_sigcont_received;
485 tog_sigwinch(int signo)
487 tog_sigwinch_received = 1;
491 tog_sigpipe(int signo)
493 tog_sigpipe_received = 1;
497 tog_sigcont(int signo)
499 tog_sigcont_received = 1;
502 static const struct got_error *
503 view_close(struct tog_view *view)
505 const struct got_error *err = NULL;
508 view_close(view->child);
512 err = view->close(view);
514 del_panel(view->panel);
516 delwin(view->window);
521 static struct tog_view *
522 view_open(int nlines, int ncols, int begin_y, int begin_x,
523 enum tog_view_type type)
525 struct tog_view *view = calloc(1, sizeof(*view));
533 view->nlines = nlines ? nlines : LINES - begin_y;
534 view->ncols = ncols ? ncols : COLS - begin_x;
535 view->begin_y = begin_y;
536 view->begin_x = begin_x;
537 view->window = newwin(nlines, ncols, begin_y, begin_x);
538 if (view->window == NULL) {
542 view->panel = new_panel(view->window);
543 if (view->panel == NULL ||
544 set_panel_userptr(view->panel, view) != OK) {
549 keypad(view->window, TRUE);
554 view_split_begin_x(int begin_x)
556 if (begin_x > 0 || COLS < 120)
558 return (COLS - MAX(COLS / 2, 80));
561 static const struct got_error *view_resize(struct tog_view *);
563 static const struct got_error *
564 view_splitscreen(struct tog_view *view)
566 const struct got_error *err = NULL;
569 view->begin_x = view_split_begin_x(0);
570 view->nlines = LINES;
571 view->ncols = COLS - view->begin_x;
574 err = view_resize(view);
578 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 return got_error_from_errno("mvwin");
584 static const struct got_error *
585 view_fullscreen(struct tog_view *view)
587 const struct got_error *err = NULL;
591 view->nlines = LINES;
595 err = view_resize(view);
599 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 return got_error_from_errno("mvwin");
606 view_is_parent_view(struct tog_view *view)
608 return view->parent == NULL;
611 static const struct got_error *
612 view_resize(struct tog_view *view)
616 if (view->lines > LINES)
617 nlines = view->nlines - (view->lines - LINES);
619 nlines = view->nlines + (LINES - view->lines);
621 if (view->cols > COLS)
622 ncols = view->ncols - (view->cols - COLS);
624 ncols = view->ncols + (COLS - view->cols);
626 if (wresize(view->window, nlines, ncols) == ERR)
627 return got_error_from_errno("wresize");
628 if (replace_panel(view->panel, view->window) == ERR)
629 return got_error_from_errno("replace_panel");
630 wclear(view->window);
632 view->nlines = nlines;
638 view->child->begin_x = view_split_begin_x(view->begin_x);
639 if (view->child->begin_x == 0) {
640 view_fullscreen(view->child);
641 if (view->child->focussed)
642 show_panel(view->child->panel);
644 show_panel(view->panel);
646 view_splitscreen(view->child);
647 show_panel(view->child->panel);
654 static const struct got_error *
655 view_close_child(struct tog_view *view)
657 const struct got_error *err = NULL;
659 if (view->child == NULL)
662 err = view_close(view->child);
667 static const struct got_error *
668 view_set_child(struct tog_view *view, struct tog_view *child)
670 const struct got_error *err = NULL;
673 child->parent = view;
678 view_is_splitscreen(struct tog_view *view)
680 return view->begin_x > 0;
689 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 cols = 80; /* Default */
696 resize_term(lines, cols);
699 static const struct got_error *
700 view_search_start(struct tog_view *view)
702 const struct got_error *err = NULL;
706 if (view->nlines < 1)
709 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
710 wclrtoeol(view->window);
714 ret = wgetnstr(view->window, pattern, sizeof(pattern));
720 if (view->searching) {
721 regfree(&view->regex);
725 if (regcomp(&view->regex, pattern,
726 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
727 err = view->search_start(view);
729 regfree(&view->regex);
732 view->searching = TOG_SEARCH_FORWARD;
733 view->search_next_done = 0;
734 view->search_next(view);
740 static const struct got_error *
741 view_input(struct tog_view **new, struct tog_view **dead,
742 struct tog_view **focus, int *done, struct tog_view *view,
743 struct tog_view_list_head *views)
745 const struct got_error *err = NULL;
753 if (view->searching && !view->search_next_done) {
754 errcode = pthread_mutex_unlock(&tog_mutex);
756 return got_error_set_errno(errcode,
757 "pthread_mutex_unlock");
759 errcode = pthread_mutex_lock(&tog_mutex);
761 return got_error_set_errno(errcode,
762 "pthread_mutex_lock");
763 view->search_next(view);
767 nodelay(stdscr, FALSE);
768 /* Allow threads to make progress while we are waiting for input. */
769 errcode = pthread_mutex_unlock(&tog_mutex);
771 return got_error_set_errno(errcode, "pthread_mutex_unlock");
772 ch = wgetch(view->window);
773 errcode = pthread_mutex_lock(&tog_mutex);
775 return got_error_set_errno(errcode, "pthread_mutex_lock");
776 nodelay(stdscr, TRUE);
778 if (tog_sigwinch_received || tog_sigcont_received) {
780 tog_sigwinch_received = 0;
781 tog_sigcont_received = 0;
782 TAILQ_FOREACH(v, views, entry) {
783 err = view_resize(v);
786 err = v->input(new, dead, focus, v, KEY_RESIZE);
797 *focus = view->child;
798 view->child_focussed = 1;
799 } else if (view->parent) {
800 *focus = view->parent;
801 view->parent->child_focussed = 0;
805 err = view->input(new, dead, focus, view, ch);
812 if (view_is_parent_view(view)) {
813 if (view->child == NULL)
815 if (view_is_splitscreen(view->child)) {
816 *focus = view->child;
817 view->child_focussed = 1;
818 err = view_fullscreen(view->child);
820 err = view_splitscreen(view->child);
823 err = view->child->input(new, dead, focus,
824 view->child, KEY_RESIZE);
826 if (view_is_splitscreen(view)) {
828 view->parent->child_focussed = 1;
829 err = view_fullscreen(view);
831 err = view_splitscreen(view);
835 err = view->input(new, dead, focus, view,
842 if (view->search_start)
843 view_search_start(view);
845 err = view->input(new, dead, focus, view, ch);
849 if (view->search_next && view->searching) {
850 view->searching = (ch == 'n' ?
851 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
852 view->search_next_done = 0;
853 view->search_next(view);
855 err = view->input(new, dead, focus, view, ch);
858 err = view->input(new, dead, focus, view, ch);
866 view_vborder(struct tog_view *view)
869 struct tog_view *view_above;
872 return view_vborder(view->parent);
874 panel = panel_above(view->panel);
878 view_above = panel_userptr(panel);
879 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
880 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
884 view_needs_focus_indication(struct tog_view *view)
886 if (view_is_parent_view(view)) {
887 if (view->child == NULL || view->child_focussed)
889 if (!view_is_splitscreen(view->child))
891 } else if (!view_is_splitscreen(view))
894 return view->focussed;
897 static const struct got_error *
898 view_loop(struct tog_view *view)
900 const struct got_error *err = NULL;
901 struct tog_view_list_head views;
902 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
903 int fast_refresh = 10;
904 int done = 0, errcode;
906 errcode = pthread_mutex_lock(&tog_mutex);
908 return got_error_set_errno(errcode, "pthread_mutex_lock");
911 TAILQ_INSERT_HEAD(&views, view, entry);
915 err = view->show(view);
920 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
921 /* Refresh fast during initialization, then become slower. */
922 if (fast_refresh && fast_refresh-- == 0)
923 halfdelay(10); /* switch to once per second */
925 err = view_input(&new_view, &dead_view, &focus_view, &done,
930 struct tog_view *prev = NULL;
932 if (view_is_parent_view(dead_view))
933 prev = TAILQ_PREV(dead_view,
934 tog_view_list_head, entry);
935 else if (view->parent != dead_view)
938 if (dead_view->parent)
939 dead_view->parent->child = NULL;
941 TAILQ_REMOVE(&views, dead_view, entry);
943 err = view_close(dead_view);
944 if (err || (dead_view == main_view && new_view == NULL))
947 if (view == dead_view) {
952 else if (!TAILQ_EMPTY(&views))
953 view = TAILQ_LAST(&views,
958 if (view->child && view->child_focussed)
959 focus_view = view->child;
966 struct tog_view *v, *t;
967 /* Only allow one parent view per type. */
968 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
969 if (v->type != new_view->type)
971 TAILQ_REMOVE(&views, v, entry);
977 TAILQ_INSERT_TAIL(&views, new_view, entry);
979 if (focus_view == NULL)
980 focus_view = new_view;
983 show_panel(focus_view->panel);
986 focus_view->focussed = 1;
989 show_panel(new_view->panel);
990 if (view->child && view_is_splitscreen(view->child))
991 show_panel(view->child->panel);
994 if (focus_view == NULL) {
996 show_panel(view->panel);
997 if (view->child && view_is_splitscreen(view->child))
998 show_panel(view->child->panel);
1002 err = view->parent->show(view->parent);
1006 err = view->show(view);
1010 err = view->child->show(view->child);
1019 while (!TAILQ_EMPTY(&views)) {
1020 view = TAILQ_FIRST(&views);
1021 TAILQ_REMOVE(&views, view, entry);
1025 errcode = pthread_mutex_unlock(&tog_mutex);
1026 if (errcode && err == NULL)
1027 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1037 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1042 /* Create newly allocated wide-character string equivalent to a byte string. */
1043 static const struct got_error *
1044 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1047 const struct got_error *err = NULL;
1050 *wlen = mbstowcs(NULL, s, 0);
1051 if (*wlen == (size_t)-1) {
1053 if (errno != EILSEQ)
1054 return got_error_from_errno("mbstowcs");
1056 /* byte string invalid in current encoding; try to "fix" it */
1057 err = got_mbsavis(&vis, &vislen, s);
1060 *wlen = mbstowcs(NULL, vis, 0);
1061 if (*wlen == (size_t)-1) {
1062 err = got_error_from_errno("mbstowcs"); /* give up */
1067 *ws = calloc(*wlen + 1, sizeof(**ws));
1069 err = got_error_from_errno("calloc");
1073 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1074 err = got_error_from_errno("mbstowcs");
1085 /* Format a line for display, ensuring that it won't overflow a width limit. */
1086 static const struct got_error *
1087 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1090 const struct got_error *err = NULL;
1092 wchar_t *wline = NULL;
1099 err = mbs2ws(&wline, &wlen, line);
1105 int width = wcwidth(wline[i]);
1112 if (width == 1 || width == 2) {
1113 if (cols + width > wlimit)
1117 } else if (width == -1) {
1118 if (wline[i] == L'\t') {
1120 ((cols + col_tab_align) % TABSIZE);
1121 if (cols + width > wlimit)
1127 err = got_error_from_errno("wcwidth");
1142 static const struct got_error*
1143 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1144 struct got_object_id *id, struct got_repository *repo)
1146 static const struct got_error *err = NULL;
1147 struct got_reflist_entry *re;
1153 SIMPLEQ_FOREACH(re, refs, entry) {
1154 struct got_tag_object *tag = NULL;
1157 name = got_ref_get_name(re->ref);
1158 if (strcmp(name, GOT_REF_HEAD) == 0)
1160 if (strncmp(name, "refs/", 5) == 0)
1162 if (strncmp(name, "got/", 4) == 0)
1164 if (strncmp(name, "heads/", 6) == 0)
1166 if (strncmp(name, "remotes/", 8) == 0)
1168 if (strncmp(name, "tags/", 5) == 0) {
1169 err = got_object_open_as_tag(&tag, repo, re->id);
1171 if (err->code != GOT_ERR_OBJ_TYPE)
1173 /* Ref points at something other than a tag. */
1178 cmp = got_object_id_cmp(tag ?
1179 got_object_tag_get_object_id(tag) : re->id, id);
1181 got_object_tag_close(tag);
1185 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1186 s ? ", " : "", name) == -1) {
1187 err = got_error_from_errno("asprintf");
1198 static const struct got_error *
1199 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1202 char *smallerthan, *at;
1204 smallerthan = strchr(author, '<');
1205 if (smallerthan && smallerthan[1] != '\0')
1206 author = smallerthan + 1;
1207 at = strchr(author, '@');
1210 return format_line(wauthor, author_width, author, limit, col_tab_align);
1213 static const struct got_error *
1214 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1215 struct got_object_id *id, struct got_reflist_head *refs,
1216 const size_t date_display_cols, int author_display_cols,
1217 struct tog_colors *colors)
1219 const struct got_error *err = NULL;
1220 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1221 char *logmsg0 = NULL, *logmsg = NULL;
1222 char *author = NULL;
1223 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1224 int author_width, logmsg_width;
1225 char *newline, *line = NULL;
1227 const int avail = view->ncols;
1229 time_t committer_time;
1230 struct tog_color *tc;
1232 committer_time = got_object_commit_get_committer_time(commit);
1233 if (localtime_r(&committer_time, &tm) == NULL)
1234 return got_error_from_errno("localtime_r");
1235 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1237 return got_error(GOT_ERR_NO_SPACE);
1239 if (avail <= date_display_cols)
1240 limit = MIN(sizeof(datebuf) - 1, avail);
1242 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1243 tc = get_color(colors, TOG_COLOR_DATE);
1245 wattr_on(view->window,
1246 COLOR_PAIR(tc->colorpair), NULL);
1247 waddnstr(view->window, datebuf, limit);
1249 wattr_off(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1257 err = got_object_id_str(&id_str, id);
1260 tc = get_color(colors, TOG_COLOR_COMMIT);
1262 wattr_on(view->window,
1263 COLOR_PAIR(tc->colorpair), NULL);
1264 wprintw(view->window, "%.8s ", id_str);
1266 wattr_off(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1274 author = strdup(got_object_commit_get_author(commit));
1275 if (author == NULL) {
1276 err = got_error_from_errno("strdup");
1279 err = format_author(&wauthor, &author_width, author, avail - col, col);
1282 tc = get_color(colors, TOG_COLOR_AUTHOR);
1284 wattr_on(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 waddwstr(view->window, wauthor);
1288 wattr_off(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 col += author_width;
1291 while (col < avail && author_width < author_display_cols + 2) {
1292 waddch(view->window, ' ');
1299 err = got_object_commit_get_logmsg(&logmsg0, commit);
1303 while (*logmsg == '\n')
1305 newline = strchr(logmsg, '\n');
1308 limit = avail - col;
1309 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1312 waddwstr(view->window, wlogmsg);
1313 col += logmsg_width;
1314 while (col < avail) {
1315 waddch(view->window, ' ');
1327 static struct commit_queue_entry *
1328 alloc_commit_queue_entry(struct got_commit_object *commit,
1329 struct got_object_id *id)
1331 struct commit_queue_entry *entry;
1333 entry = calloc(1, sizeof(*entry));
1338 entry->commit = commit;
1343 pop_commit(struct commit_queue *commits)
1345 struct commit_queue_entry *entry;
1347 entry = TAILQ_FIRST(&commits->head);
1348 TAILQ_REMOVE(&commits->head, entry, entry);
1349 got_object_commit_close(entry->commit);
1350 commits->ncommits--;
1351 /* Don't free entry->id! It is owned by the commit graph. */
1356 free_commits(struct commit_queue *commits)
1358 while (!TAILQ_EMPTY(&commits->head))
1359 pop_commit(commits);
1362 static const struct got_error *
1363 match_commit(int *have_match, struct got_object_id *id,
1364 struct got_commit_object *commit, regex_t *regex)
1366 const struct got_error *err = NULL;
1367 regmatch_t regmatch;
1368 char *id_str = NULL, *logmsg = NULL;
1372 err = got_object_id_str(&id_str, id);
1376 err = got_object_commit_get_logmsg(&logmsg, commit);
1380 if (regexec(regex, got_object_commit_get_author(commit), 1,
1381 ®match, 0) == 0 ||
1382 regexec(regex, got_object_commit_get_committer(commit), 1,
1383 ®match, 0) == 0 ||
1384 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1385 regexec(regex, logmsg, 1, ®match, 0) == 0)
1393 static const struct got_error *
1394 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1395 int minqueue, struct got_repository *repo, const char *path,
1396 int *searching, int *search_next_done, regex_t *regex)
1398 const struct got_error *err = NULL;
1399 int nqueued = 0, have_match = 0;
1402 * We keep all commits open throughout the lifetime of the log
1403 * view in order to avoid having to re-fetch commits from disk
1404 * while updating the display.
1406 while (nqueued < minqueue ||
1407 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1408 struct got_object_id *id;
1409 struct got_commit_object *commit;
1410 struct commit_queue_entry *entry;
1413 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1414 if (err || id == NULL)
1417 err = got_object_open_as_commit(&commit, repo, id);
1420 entry = alloc_commit_queue_entry(commit, id);
1421 if (entry == NULL) {
1422 err = got_error_from_errno("alloc_commit_queue_entry");
1426 errcode = pthread_mutex_lock(&tog_mutex);
1428 err = got_error_set_errno(errcode,
1429 "pthread_mutex_lock");
1433 entry->idx = commits->ncommits;
1434 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1436 commits->ncommits++;
1438 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1439 err = match_commit(&have_match, id, commit, regex);
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode,
1445 "pthread_mutex_unlock");
1447 if (err || have_match)
1454 static const struct got_error *
1455 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 struct commit_queue *commits, int selected_idx, int limit,
1458 struct got_reflist_head *refs, const char *path, int commits_needed,
1459 struct tog_colors *colors)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 struct commit_queue_entry *entry;
1465 int ncommits, author_cols = 4;
1466 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 char *refs_str = NULL;
1469 struct tog_color *tc;
1470 static const size_t date_display_cols = 12;
1475 if (ncommits == selected_idx) {
1479 entry = TAILQ_NEXT(entry, entry);
1483 if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 err = got_object_id_str(&id_str, (*selected)->id);
1488 err = build_refs_str(&refs_str, refs, (*selected)->id,
1495 if (commits_needed == 0)
1496 halfdelay(10); /* disable fast refresh */
1498 if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 entry ? entry->idx + 1 : 0, commits->ncommits,
1500 commits_needed > 0 ?
1501 (view->searching && view->search_next_done == 0
1502 ? "searching..." : "loading... ") :
1503 (refs_str ? refs_str : "")) == -1) {
1504 err = got_error_from_errno("asprintf");
1508 if (path && strcmp(path, "/") != 0) {
1509 if (asprintf(&header, "commit %s %s%s",
1510 id_str ? id_str : "........................................",
1511 path, ncommits_str) == -1) {
1512 err = got_error_from_errno("asprintf");
1516 } else if (asprintf(&header, "commit %s%s",
1517 id_str ? id_str : "........................................",
1518 ncommits_str) == -1) {
1519 err = got_error_from_errno("asprintf");
1523 err = format_line(&wline, &width, header, view->ncols, 0);
1527 werase(view->window);
1529 if (view_needs_focus_indication(view))
1530 wstandout(view->window);
1531 tc = get_color(colors, TOG_COLOR_COMMIT);
1533 wattr_on(view->window,
1534 COLOR_PAIR(tc->colorpair), NULL);
1535 waddwstr(view->window, wline);
1537 wattr_off(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 while (width < view->ncols) {
1540 waddch(view->window, ' ');
1543 if (view_needs_focus_indication(view))
1544 wstandend(view->window);
1549 /* Grow author column size if necessary. */
1556 if (ncommits >= limit - 1)
1558 author = strdup(got_object_commit_get_author(entry->commit));
1559 if (author == NULL) {
1560 err = got_error_from_errno("strdup");
1563 err = format_author(&wauthor, &width, author, COLS,
1565 if (author_cols < width)
1566 author_cols = width;
1570 entry = TAILQ_NEXT(entry, entry);
1577 if (ncommits >= limit - 1)
1579 if (ncommits == selected_idx)
1580 wstandout(view->window);
1581 err = draw_commit(view, entry->commit, entry->id, refs,
1582 date_display_cols, author_cols, colors);
1583 if (ncommits == selected_idx)
1584 wstandend(view->window);
1589 entry = TAILQ_NEXT(entry, entry);
1602 scroll_up(struct tog_view *view,
1603 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1609 entry = TAILQ_FIRST(&commits->head);
1610 if (*first_displayed_entry == entry)
1613 entry = *first_displayed_entry;
1614 while (entry && nscrolled < maxscroll) {
1615 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1617 *first_displayed_entry = entry;
1623 static const struct got_error *
1624 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 pthread_cond_t *need_commits)
1630 halfdelay(1); /* fast refresh while loading commits */
1632 while (*commits_needed > 0) {
1636 /* Wake the log thread. */
1637 errcode = pthread_cond_signal(need_commits);
1639 return got_error_set_errno(errcode,
1640 "pthread_cond_signal");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1643 return got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1646 errcode = pthread_mutex_lock(&tog_mutex);
1648 return got_error_set_errno(errcode,
1649 "pthread_mutex_lock");
1651 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 * Thread is not done yet; lose a key press
1654 * and let the user retry... this way the GUI
1655 * remains interactive while logging deep paths
1656 * with few commits in history.
1665 static const struct got_error *
1666 scroll_down(struct tog_view *view,
1667 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 struct commit_queue_entry **last_displayed_entry,
1669 struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 pthread_cond_t *need_commits)
1672 const struct got_error *err = NULL;
1673 struct commit_queue_entry *pentry;
1676 if (*last_displayed_entry == NULL)
1679 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 if (pentry == NULL && !*log_complete) {
1682 * Ask the log thread for required amount of commits
1683 * plus some amount of pre-fetching.
1685 (*commits_needed) += maxscroll + 20;
1686 err = trigger_log_thread(0, commits_needed, log_complete,
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1697 *last_displayed_entry = pentry;
1699 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1702 *first_displayed_entry = pentry;
1703 } while (++nscrolled < maxscroll);
1708 static const struct got_error *
1709 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 struct got_commit_object *commit, struct got_object_id *commit_id,
1711 struct tog_view *log_view, struct got_reflist_head *refs,
1712 struct got_repository *repo)
1714 const struct got_error *err;
1715 struct got_object_qid *parent_id;
1716 struct tog_view *diff_view;
1718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 if (diff_view == NULL)
1720 return got_error_from_errno("view_open");
1722 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 commit_id, log_view, refs, repo);
1726 *new_view = diff_view;
1730 static const struct got_error *
1731 tree_view_visit_subtree(struct got_tree_object *subtree,
1732 struct tog_tree_view_state *s)
1734 struct tog_parent_tree *parent;
1736 parent = calloc(1, sizeof(*parent));
1738 return got_error_from_errno("calloc");
1740 parent->tree = s->tree;
1741 parent->first_displayed_entry = s->first_displayed_entry;
1742 parent->selected_entry = s->selected_entry;
1743 parent->selected = s->selected;
1744 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1747 s->first_displayed_entry = NULL;
1752 static const struct got_error *
1753 browse_commit_tree(struct tog_view **new_view, int begin_x,
1754 struct commit_queue_entry *entry, const char *path,
1755 struct got_reflist_head *refs, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree;
1759 struct tog_tree_view_state *s;
1760 struct tog_view *tree_view;
1761 char *slash, *subpath = NULL;
1764 err = got_object_open_as_tree(&tree, repo,
1765 got_object_commit_get_tree_id(entry->commit));
1769 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1770 if (tree_view == NULL)
1771 return got_error_from_errno("view_open");
1773 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1775 got_object_tree_close(tree);
1778 s = &tree_view->state.tree;
1780 *new_view = tree_view;
1782 if (got_path_is_root_dir(path))
1785 /* Walk the path and open corresponding tree objects. */
1788 struct got_tree_entry *te;
1789 struct got_object_id *tree_id;
1795 /* Ensure the correct subtree entry is selected. */
1796 slash = strchr(p, '/');
1798 te_name = strdup(p);
1800 te_name = strndup(p, slash - p);
1801 if (te_name == NULL) {
1802 err = got_error_from_errno("strndup");
1805 te = got_object_tree_find_entry(s->tree, te_name);
1807 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1812 s->selected_entry = te;
1813 s->selected = got_tree_entry_get_index(te);
1814 if (s->tree != s->root)
1815 s->selected++; /* skip '..' */
1817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1818 /* Jump to this file's entry. */
1819 s->first_displayed_entry = s->selected_entry;
1824 slash = strchr(p, '/');
1826 subpath = strndup(path, slash - path);
1828 subpath = strdup(path);
1829 if (subpath == NULL) {
1830 err = got_error_from_errno("strdup");
1834 err = got_object_id_by_path(&tree_id, repo, entry->id,
1839 err = got_object_open_as_tree(&tree, repo, tree_id);
1844 err = tree_view_visit_subtree(tree, s);
1846 got_object_tree_close(tree);
1860 static const struct got_error *
1861 block_signals_used_by_main_thread(void)
1866 if (sigemptyset(&sigset) == -1)
1867 return got_error_from_errno("sigemptyset");
1869 /* tog handles SIGWINCH and SIGCONT */
1870 if (sigaddset(&sigset, SIGWINCH) == -1)
1871 return got_error_from_errno("sigaddset");
1872 if (sigaddset(&sigset, SIGCONT) == -1)
1873 return got_error_from_errno("sigaddset");
1875 /* ncurses handles SIGTSTP */
1876 if (sigaddset(&sigset, SIGTSTP) == -1)
1877 return got_error_from_errno("sigaddset");
1879 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1881 return got_error_set_errno(errcode, "pthread_sigmask");
1887 log_thread(void *arg)
1889 const struct got_error *err = NULL;
1891 struct tog_log_thread_args *a = arg;
1894 err = block_signals_used_by_main_thread();
1898 while (!done && !err && !tog_sigpipe_received) {
1899 err = queue_commits(a->graph, a->commits, 1, a->repo,
1900 a->in_repo_path, a->searching, a->search_next_done,
1903 if (err->code != GOT_ERR_ITER_COMPLETED)
1907 } else if (a->commits_needed > 0)
1908 a->commits_needed--;
1910 errcode = pthread_mutex_lock(&tog_mutex);
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_lock");
1915 } else if (*a->quit)
1917 else if (*a->first_displayed_entry == NULL) {
1918 *a->first_displayed_entry =
1919 TAILQ_FIRST(&a->commits->head);
1920 *a->selected_entry = *a->first_displayed_entry;
1924 a->commits_needed = 0;
1925 else if (a->commits_needed == 0) {
1926 errcode = pthread_cond_wait(&a->need_commits,
1929 err = got_error_set_errno(errcode,
1930 "pthread_cond_wait");
1933 errcode = pthread_mutex_unlock(&tog_mutex);
1934 if (errcode && err == NULL)
1935 err = got_error_set_errno(errcode,
1936 "pthread_mutex_unlock");
1938 a->log_complete = 1;
1942 static const struct got_error *
1943 stop_log_thread(struct tog_log_view_state *s)
1945 const struct got_error *err = NULL;
1950 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1952 return got_error_set_errno(errcode,
1953 "pthread_cond_signal");
1954 errcode = pthread_mutex_unlock(&tog_mutex);
1956 return got_error_set_errno(errcode,
1957 "pthread_mutex_unlock");
1958 errcode = pthread_join(s->thread, (void **)&err);
1960 return got_error_set_errno(errcode, "pthread_join");
1961 errcode = pthread_mutex_lock(&tog_mutex);
1963 return got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1968 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1969 if (errcode && err == NULL)
1970 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1972 if (s->thread_args.repo) {
1973 got_repo_close(s->thread_args.repo);
1974 s->thread_args.repo = NULL;
1977 if (s->thread_args.graph) {
1978 got_commit_graph_close(s->thread_args.graph);
1979 s->thread_args.graph = NULL;
1985 static const struct got_error *
1986 close_log_view(struct tog_view *view)
1988 const struct got_error *err = NULL;
1989 struct tog_log_view_state *s = &view->state.log;
1991 err = stop_log_thread(s);
1992 free_commits(&s->commits);
1993 free(s->in_repo_path);
1994 s->in_repo_path = NULL;
2000 static const struct got_error *
2001 search_start_log_view(struct tog_view *view)
2003 struct tog_log_view_state *s = &view->state.log;
2005 s->matched_entry = NULL;
2006 s->search_entry = NULL;
2010 static const struct got_error *
2011 search_next_log_view(struct tog_view *view)
2013 const struct got_error *err = NULL;
2014 struct tog_log_view_state *s = &view->state.log;
2015 struct commit_queue_entry *entry;
2017 if (!view->searching) {
2018 view->search_next_done = 1;
2022 if (s->search_entry) {
2024 errcode = pthread_mutex_unlock(&tog_mutex);
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_unlock");
2028 ch = wgetch(view->window);
2029 errcode = pthread_mutex_lock(&tog_mutex);
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2033 if (ch == KEY_BACKSPACE) {
2034 view->search_next_done = 1;
2037 if (view->searching == TOG_SEARCH_FORWARD)
2038 entry = TAILQ_NEXT(s->search_entry, entry);
2040 entry = TAILQ_PREV(s->search_entry,
2041 commit_queue_head, entry);
2042 } else if (s->matched_entry) {
2043 if (view->searching == TOG_SEARCH_FORWARD)
2044 entry = TAILQ_NEXT(s->selected_entry, entry);
2046 entry = TAILQ_PREV(s->selected_entry,
2047 commit_queue_head, entry);
2049 if (view->searching == TOG_SEARCH_FORWARD)
2050 entry = TAILQ_FIRST(&s->commits.head);
2052 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2058 if (entry == NULL) {
2059 if (s->thread_args.log_complete ||
2060 view->searching == TOG_SEARCH_BACKWARD) {
2061 view->search_next_done = 1;
2065 * Poke the log thread for more commits and return,
2066 * allowing the main loop to make progress. Search
2067 * will resume at s->search_entry once we come back.
2069 s->thread_args.commits_needed++;
2070 return trigger_log_thread(1,
2071 &s->thread_args.commits_needed,
2072 &s->thread_args.log_complete,
2073 &s->thread_args.need_commits);
2076 err = match_commit(&have_match, entry->id, entry->commit,
2081 view->search_next_done = 1;
2082 s->matched_entry = entry;
2086 s->search_entry = entry;
2087 if (view->searching == TOG_SEARCH_FORWARD)
2088 entry = TAILQ_NEXT(entry, entry);
2090 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2093 if (s->matched_entry) {
2094 int cur = s->selected_entry->idx;
2095 while (cur < s->matched_entry->idx) {
2096 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2101 while (cur > s->matched_entry->idx) {
2102 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2109 s->search_entry = NULL;
2114 static const struct got_error *
2115 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2116 struct got_reflist_head *refs, struct got_repository *repo,
2117 const char *head_ref_name, const char *path, int check_disk,
2120 const struct got_error *err = NULL;
2121 struct tog_log_view_state *s = &view->state.log;
2122 struct got_repository *thread_repo = NULL;
2123 struct got_commit_graph *thread_graph = NULL;
2126 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2130 /* The commit queue only contains commits being displayed. */
2131 TAILQ_INIT(&s->commits.head);
2132 s->commits.ncommits = 0;
2136 s->head_ref_name = head_ref_name;
2137 s->start_id = got_object_id_dup(start_id);
2138 if (s->start_id == NULL) {
2139 err = got_error_from_errno("got_object_id_dup");
2142 s->log_branches = log_branches;
2144 SIMPLEQ_INIT(&s->colors);
2145 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2146 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2147 get_color_value("TOG_COLOR_COMMIT"));
2150 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2151 get_color_value("TOG_COLOR_AUTHOR"));
2153 free_colors(&s->colors);
2156 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2157 get_color_value("TOG_COLOR_DATE"));
2159 free_colors(&s->colors);
2164 view->show = show_log_view;
2165 view->input = input_log_view;
2166 view->close = close_log_view;
2167 view->search_start = search_start_log_view;
2168 view->search_next = search_next_log_view;
2170 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2173 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2177 err = got_commit_graph_iter_start(thread_graph,
2178 s->start_id, s->repo, NULL, NULL);
2182 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2184 err = got_error_set_errno(errcode, "pthread_cond_init");
2188 s->thread_args.commits_needed = view->nlines;
2189 s->thread_args.graph = thread_graph;
2190 s->thread_args.commits = &s->commits;
2191 s->thread_args.in_repo_path = s->in_repo_path;
2192 s->thread_args.start_id = s->start_id;
2193 s->thread_args.repo = thread_repo;
2194 s->thread_args.log_complete = 0;
2195 s->thread_args.quit = &s->quit;
2196 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2197 s->thread_args.selected_entry = &s->selected_entry;
2198 s->thread_args.searching = &view->searching;
2199 s->thread_args.search_next_done = &view->search_next_done;
2200 s->thread_args.regex = &view->regex;
2203 close_log_view(view);
2207 static const struct got_error *
2208 show_log_view(struct tog_view *view)
2210 struct tog_log_view_state *s = &view->state.log;
2212 if (s->thread == NULL) {
2213 int errcode = pthread_create(&s->thread, NULL, log_thread,
2216 return got_error_set_errno(errcode, "pthread_create");
2219 return draw_commits(view, &s->last_displayed_entry,
2220 &s->selected_entry, s->first_displayed_entry,
2221 &s->commits, s->selected, view->nlines, s->refs,
2222 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2225 static const struct got_error *
2226 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2227 struct tog_view **focus_view, struct tog_view *view, int ch)
2229 const struct got_error *err = NULL;
2230 struct tog_log_view_state *s = &view->state.log;
2231 char *parent_path, *in_repo_path = NULL;
2232 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2234 struct got_object_id *start_id;
2244 if (s->first_displayed_entry == NULL)
2246 if (s->selected > 0)
2249 scroll_up(view, &s->first_displayed_entry, 1,
2254 if (s->first_displayed_entry == NULL)
2256 if (TAILQ_FIRST(&s->commits.head) ==
2257 s->first_displayed_entry) {
2261 scroll_up(view, &s->first_displayed_entry,
2262 view->nlines, &s->commits);
2268 if (s->first_displayed_entry == NULL)
2270 if (s->selected < MIN(view->nlines - 2,
2271 s->commits.ncommits - 1)) {
2275 err = scroll_down(view, &s->first_displayed_entry, 1,
2276 &s->last_displayed_entry, &s->commits,
2277 &s->thread_args.log_complete,
2278 &s->thread_args.commits_needed,
2279 &s->thread_args.need_commits);
2283 struct commit_queue_entry *first;
2284 first = s->first_displayed_entry;
2287 err = scroll_down(view, &s->first_displayed_entry,
2288 view->nlines, &s->last_displayed_entry,
2289 &s->commits, &s->thread_args.log_complete,
2290 &s->thread_args.commits_needed,
2291 &s->thread_args.need_commits);
2294 if (first == s->first_displayed_entry &&
2295 s->selected < MIN(view->nlines - 2,
2296 s->commits.ncommits - 1)) {
2297 /* can't scroll further down */
2298 s->selected = MIN(view->nlines - 2,
2299 s->commits.ncommits - 1);
2305 if (s->selected > view->nlines - 2)
2306 s->selected = view->nlines - 2;
2307 if (s->selected > s->commits.ncommits - 1)
2308 s->selected = s->commits.ncommits - 1;
2313 if (s->selected_entry == NULL)
2315 if (view_is_parent_view(view))
2316 begin_x = view_split_begin_x(view->begin_x);
2317 err = open_diff_view_for_commit(&diff_view, begin_x,
2318 s->selected_entry->commit, s->selected_entry->id,
2319 view, s->refs, s->repo);
2322 if (view_is_parent_view(view)) {
2323 err = view_close_child(view);
2326 err = view_set_child(view, diff_view);
2328 view_close(diff_view);
2331 *focus_view = diff_view;
2332 view->child_focussed = 1;
2334 *new_view = diff_view;
2337 if (s->selected_entry == NULL)
2339 if (view_is_parent_view(view))
2340 begin_x = view_split_begin_x(view->begin_x);
2341 err = browse_commit_tree(&tree_view, begin_x,
2342 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2345 if (view_is_parent_view(view)) {
2346 err = view_close_child(view);
2349 err = view_set_child(view, tree_view);
2351 view_close(tree_view);
2354 *focus_view = tree_view;
2355 view->child_focussed = 1;
2357 *new_view = tree_view;
2360 if (strcmp(s->in_repo_path, "/") == 0)
2362 parent_path = dirname(s->in_repo_path);
2363 if (parent_path && strcmp(parent_path, ".") != 0) {
2364 err = stop_log_thread(s);
2367 lv = view_open(view->nlines, view->ncols,
2368 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2370 return got_error_from_errno(
2372 err = open_log_view(lv, s->start_id, s->refs,
2373 s->repo, s->head_ref_name, parent_path, 0,
2377 if (view_is_parent_view(view))
2380 view_set_child(view->parent, lv);
2387 err = stop_log_thread(s);
2390 lv = view_open(view->nlines, view->ncols,
2391 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2393 return got_error_from_errno("view_open");
2394 err = got_repo_match_object_id(&start_id, NULL,
2395 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2396 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2401 in_repo_path = strdup(s->in_repo_path);
2402 if (in_repo_path == NULL) {
2405 return got_error_from_errno("strdup");
2407 got_ref_list_free(s->refs);
2408 err = got_ref_list(s->refs, s->repo, NULL,
2409 got_ref_cmp_by_name, NULL);
2415 err = open_log_view(lv, start_id, s->refs, s->repo,
2416 s->head_ref_name, in_repo_path, 0, s->log_branches);
2426 s->log_branches = !s->log_branches;
2427 err = stop_log_thread(s);
2430 lv = view_open(view->nlines, view->ncols,
2431 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2433 return got_error_from_errno("view_open");
2434 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2435 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2450 static const struct got_error *
2451 apply_unveil(const char *repo_path, const char *worktree_path)
2453 const struct got_error *error;
2456 if (unveil("gmon.out", "rwc") != 0)
2457 return got_error_from_errno2("unveil", "gmon.out");
2459 if (repo_path && unveil(repo_path, "r") != 0)
2460 return got_error_from_errno2("unveil", repo_path);
2462 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2463 return got_error_from_errno2("unveil", worktree_path);
2465 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2466 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2468 error = got_privsep_unveil_exec_helpers();
2472 if (unveil(NULL, NULL) != 0)
2473 return got_error_from_errno("unveil");
2483 halfdelay(1); /* Do fast refresh while initial view is loading. */
2486 intrflush(stdscr, FALSE);
2487 keypad(stdscr, TRUE);
2489 if (getenv("TOG_COLORS") != NULL) {
2491 use_default_colors();
2493 signal(SIGWINCH, tog_sigwinch);
2494 signal(SIGPIPE, tog_sigpipe);
2495 signal(SIGCONT, tog_sigcont);
2498 static const struct got_error *
2499 cmd_log(int argc, char *argv[])
2501 const struct got_error *error;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 struct got_reflist_head refs;
2505 struct got_object_id *start_id = NULL;
2506 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2507 char *start_commit = NULL, *head_ref_name = NULL;
2508 int ch, log_branches = 0, check_disk = 1;
2509 struct tog_view *view;
2511 SIMPLEQ_INIT(&refs);
2514 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2519 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2525 start_commit = optarg;
2528 repo_path = realpath(optarg, NULL);
2529 if (repo_path == NULL)
2530 return got_error_from_errno2("realpath",
2542 cwd = getcwd(NULL, 0);
2544 error = got_error_from_errno("getcwd");
2547 error = got_worktree_open(&worktree, cwd);
2548 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2555 error = got_error_from_errno("strdup");
2558 } else if (argc == 1) {
2560 error = got_worktree_resolve_path(&path, worktree,
2565 path = strdup(argv[0]);
2567 error = got_error_from_errno("strdup");
2574 if (repo_path == NULL) {
2577 got_worktree_get_repo_path(worktree));
2579 repo_path = strdup(cwd);
2581 if (repo_path == NULL) {
2582 error = got_error_from_errno("strdup");
2588 error = got_repo_open(&repo, repo_path, NULL);
2592 error = apply_unveil(got_repo_get_path(repo),
2593 worktree ? got_worktree_get_root_path(worktree) : NULL);
2597 if (start_commit == NULL)
2598 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2599 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2600 GOT_OBJ_TYPE_COMMIT, 1, repo);
2602 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2603 GOT_OBJ_TYPE_COMMIT, 1, repo);
2607 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2611 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2613 error = got_error_from_errno("view_open");
2617 const char *prefix = got_worktree_get_path_prefix(worktree);
2619 if (asprintf(&p, "%s%s%s", prefix,
2620 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2621 error = got_error_from_errno("asprintf");
2628 head_ref_name = strdup(
2629 got_worktree_get_head_ref_name(worktree));
2630 if (head_ref_name == NULL) {
2631 error = got_error_from_errno("strdup");
2635 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2636 path, check_disk, log_branches);
2640 /* Release work tree lock. */
2641 got_worktree_close(worktree);
2644 error = view_loop(view);
2650 free(head_ref_name);
2652 got_repo_close(repo);
2654 got_worktree_close(worktree);
2655 got_ref_list_free(&refs);
2663 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2669 parse_next_line(FILE *f, size_t *len)
2674 const char delim[3] = { '\0', '\0', '\0'};
2676 line = fparseln(f, &linelen, &lineno, delim, 0);
2683 match_line(const char *line, regex_t *regex)
2685 regmatch_t regmatch;
2687 return regexec(regex, line, 1, ®match, 0) == 0;
2691 match_color(struct tog_colors *colors, const char *line)
2693 struct tog_color *tc = NULL;
2695 SIMPLEQ_FOREACH(tc, colors, entry) {
2696 if (match_line(line, &tc->regex))
2703 static const struct got_error *
2704 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2705 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2706 char *header, struct tog_colors *colors)
2708 const struct got_error *err;
2709 int lineno = 0, nprinted = 0;
2711 struct tog_color *tc;
2717 werase(view->window);
2720 err = format_line(&wline, &width, header, view->ncols, 0);
2725 if (view_needs_focus_indication(view))
2726 wstandout(view->window);
2727 waddwstr(view->window, wline);
2728 if (view_needs_focus_indication(view))
2729 wstandend(view->window);
2730 if (width <= view->ncols - 1)
2731 waddch(view->window, '\n');
2739 while (nprinted < max_lines) {
2740 line = parse_next_line(f, &len);
2745 if (++lineno < *first_displayed_line) {
2750 err = format_line(&wline, &width, line, view->ncols, 0);
2756 tc = match_color(colors, line);
2758 wattr_on(view->window,
2759 COLOR_PAIR(tc->colorpair), NULL);
2760 waddwstr(view->window, wline);
2762 wattr_off(view->window,
2763 COLOR_PAIR(tc->colorpair), NULL);
2764 if (width <= view->ncols - 1)
2765 waddch(view->window, '\n');
2766 if (++nprinted == 1)
2767 *first_displayed_line = lineno;
2772 *last_displayed_line = lineno;
2777 while (nprinted < view->nlines) {
2778 waddch(view->window, '\n');
2782 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2787 wstandout(view->window);
2788 waddwstr(view->window, wline);
2789 wstandend(view->window);
2796 get_datestr(time_t *time, char *datebuf)
2798 struct tm mytm, *tm;
2801 tm = gmtime_r(time, &mytm);
2804 s = asctime_r(tm, datebuf);
2807 p = strchr(s, '\n');
2813 static const struct got_error *
2814 write_commit_info(struct got_object_id *commit_id,
2815 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2817 const struct got_error *err = NULL;
2818 char datebuf[26], *datestr;
2819 struct got_commit_object *commit;
2820 char *id_str = NULL, *logmsg = NULL;
2821 time_t committer_time;
2822 const char *author, *committer;
2823 char *refs_str = NULL;
2826 err = build_refs_str(&refs_str, refs, commit_id, repo);
2831 err = got_object_open_as_commit(&commit, repo, commit_id);
2835 err = got_object_id_str(&id_str, commit_id);
2837 err = got_error_from_errno("got_object_id_str");
2841 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2842 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2843 err = got_error_from_errno("fprintf");
2846 if (fprintf(outfile, "from: %s\n",
2847 got_object_commit_get_author(commit)) < 0) {
2848 err = got_error_from_errno("fprintf");
2851 committer_time = got_object_commit_get_committer_time(commit);
2852 datestr = get_datestr(&committer_time, datebuf);
2853 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2854 err = got_error_from_errno("fprintf");
2857 author = got_object_commit_get_author(commit);
2858 committer = got_object_commit_get_committer(commit);
2859 if (strcmp(author, committer) != 0 &&
2860 fprintf(outfile, "via: %s\n", committer) < 0) {
2861 err = got_error_from_errno("fprintf");
2864 err = got_object_commit_get_logmsg(&logmsg, commit);
2867 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2868 err = got_error_from_errno("fprintf");
2875 got_object_commit_close(commit);
2879 const struct got_error *
2880 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2886 size_t noffsets = 0;
2890 *line_offsets = NULL;
2896 if (fseek(infile, 0, SEEK_END) == -1)
2897 return got_error_from_errno("fseek");
2898 len = ftell(infile) + 1;
2900 return got_error_from_errno("ftell");
2901 if (fseek(infile, 0, SEEK_SET) == -1)
2902 return got_error_from_errno("fseek");
2906 if ((buf = calloc(len, sizeof(char *))) == NULL)
2907 return got_error_from_errno("calloc");
2909 fread(buf, 1, len, infile);
2911 return got_error_from_errno("fread");
2914 if (line_offsets && nlines) {
2915 if (*line_offsets == NULL) {
2916 /* Have some data but perhaps no '\n'. */
2919 *line_offsets = calloc(1, sizeof(**line_offsets));
2920 if (*line_offsets == NULL)
2921 return got_error_from_errno("calloc");
2922 /* Skip forward over end of first line. */
2929 /* Scan '\n' offsets in remaining chunk of data. */
2931 if (buf[i] != '\n') {
2936 if (noffsets < *nlines) {
2937 off_t *o = recallocarray(*line_offsets,
2939 sizeof(**line_offsets));
2941 free(*line_offsets);
2942 *line_offsets = NULL;
2943 return got_error_from_errno(
2950 (*line_offsets)[*nlines - 1] = off;
2955 if (fflush(infile) != 0)
2956 return got_error_from_errno("fflush");
2965 static const struct got_error *
2966 create_diff(struct tog_diff_view_state *s)
2968 const struct got_error *err = NULL;
2974 err = got_error_from_errno("got_opentemp");
2977 if (s->f && fclose(s->f) != 0) {
2978 err = got_error_from_errno("fclose");
2984 err = got_object_get_type(&obj_type, s->repo, s->id1);
2986 err = got_object_get_type(&obj_type, s->repo, s->id2);
2991 case GOT_OBJ_TYPE_BLOB:
2992 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2993 s->diff_context, 0, s->repo, s->f);
2995 case GOT_OBJ_TYPE_TREE:
2996 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2997 s->diff_context, 0, s->repo, s->f);
2999 case GOT_OBJ_TYPE_COMMIT: {
3000 const struct got_object_id_queue *parent_ids;
3001 struct got_object_qid *pid;
3002 struct got_commit_object *commit2;
3004 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3007 /* Show commit info if we're diffing to a parent/root commit. */
3008 if (s->id1 == NULL) {
3009 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3013 parent_ids = got_object_commit_get_parent_ids(commit2);
3014 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3015 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3016 err = write_commit_info(s->id2, s->refs,
3024 got_object_commit_close(commit2);
3026 err = got_diff_objects_as_commits(s->id1, s->id2,
3027 s->diff_context, 0, s->repo, s->f);
3031 err = got_error(GOT_ERR_OBJ_TYPE);
3036 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3039 if (s->f && fflush(s->f) != 0 && err == NULL)
3040 err = got_error_from_errno("fflush");
3045 diff_view_indicate_progress(struct tog_view *view)
3047 mvwaddstr(view->window, 0, 0, "diffing...");
3052 static const struct got_error *
3053 search_start_diff_view(struct tog_view *view)
3055 struct tog_diff_view_state *s = &view->state.diff;
3057 s->matched_line = 0;
3061 static const struct got_error *
3062 search_next_diff_view(struct tog_view *view)
3064 struct tog_diff_view_state *s = &view->state.diff;
3067 if (!view->searching) {
3068 view->search_next_done = 1;
3072 if (s->matched_line) {
3073 if (view->searching == TOG_SEARCH_FORWARD)
3074 lineno = s->matched_line + 1;
3076 lineno = s->matched_line - 1;
3078 if (view->searching == TOG_SEARCH_FORWARD)
3089 if (lineno <= 0 || lineno > s->nlines) {
3090 if (s->matched_line == 0) {
3091 view->search_next_done = 1;
3096 if (view->searching == TOG_SEARCH_FORWARD)
3102 offset = s->line_offsets[lineno - 1];
3103 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3105 return got_error_from_errno("fseeko");
3108 line = parse_next_line(s->f, &len);
3109 if (line && match_line(line, &view->regex)) {
3110 view->search_next_done = 1;
3111 s->matched_line = lineno;
3116 if (view->searching == TOG_SEARCH_FORWARD)
3122 if (s->matched_line) {
3123 s->first_displayed_line = s->matched_line;
3124 s->selected_line = 1;
3130 static const struct got_error *
3131 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3132 struct got_object_id *id2, struct tog_view *log_view,
3133 struct got_reflist_head *refs, struct got_repository *repo)
3135 const struct got_error *err;
3136 struct tog_diff_view_state *s = &view->state.diff;
3138 if (id1 != NULL && id2 != NULL) {
3140 err = got_object_get_type(&type1, repo, id1);
3143 err = got_object_get_type(&type2, repo, id2);
3148 return got_error(GOT_ERR_OBJ_TYPE);
3150 s->first_displayed_line = 1;
3151 s->last_displayed_line = view->nlines;
3152 s->selected_line = 1;
3159 s->id1 = got_object_id_dup(id1);
3161 return got_error_from_errno("got_object_id_dup");
3165 s->id2 = got_object_id_dup(id2);
3166 if (s->id2 == NULL) {
3169 return got_error_from_errno("got_object_id_dup");
3172 s->first_displayed_line = 1;
3173 s->last_displayed_line = view->nlines;
3174 s->diff_context = 3;
3175 s->log_view = log_view;
3179 SIMPLEQ_INIT(&s->colors);
3180 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3181 err = add_color(&s->colors,
3182 "^-", TOG_COLOR_DIFF_MINUS,
3183 get_color_value("TOG_COLOR_DIFF_MINUS"));
3186 err = add_color(&s->colors, "^\\+",
3187 TOG_COLOR_DIFF_PLUS,
3188 get_color_value("TOG_COLOR_DIFF_PLUS"));
3190 free_colors(&s->colors);
3193 err = add_color(&s->colors,
3194 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3195 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3197 free_colors(&s->colors);
3201 err = add_color(&s->colors,
3202 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3203 get_color_value("TOG_COLOR_DIFF_META"));
3205 free_colors(&s->colors);
3209 err = add_color(&s->colors,
3210 "^(from|via): ", TOG_COLOR_AUTHOR,
3211 get_color_value("TOG_COLOR_AUTHOR"));
3213 free_colors(&s->colors);
3217 err = add_color(&s->colors,
3218 "^date: ", TOG_COLOR_DATE,
3219 get_color_value("TOG_COLOR_DATE"));
3221 free_colors(&s->colors);
3226 if (log_view && view_is_splitscreen(view))
3227 show_log_view(log_view); /* draw vborder */
3228 diff_view_indicate_progress(view);
3230 err = create_diff(s);
3239 view->show = show_diff_view;
3240 view->input = input_diff_view;
3241 view->close = close_diff_view;
3242 view->search_start = search_start_diff_view;
3243 view->search_next = search_next_diff_view;
3248 static const struct got_error *
3249 close_diff_view(struct tog_view *view)
3251 const struct got_error *err = NULL;
3252 struct tog_diff_view_state *s = &view->state.diff;
3258 if (s->f && fclose(s->f) == EOF)
3259 err = got_error_from_errno("fclose");
3260 free_colors(&s->colors);
3261 free(s->line_offsets);
3265 static const struct got_error *
3266 show_diff_view(struct tog_view *view)
3268 const struct got_error *err;
3269 struct tog_diff_view_state *s = &view->state.diff;
3270 char *id_str1 = NULL, *id_str2, *header;
3273 err = got_object_id_str(&id_str1, s->id1);
3277 err = got_object_id_str(&id_str2, s->id2);
3281 if (asprintf(&header, "diff %s %s",
3282 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3283 err = got_error_from_errno("asprintf");
3291 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3292 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3293 header, &s->colors);
3296 static const struct got_error *
3297 set_selected_commit(struct tog_diff_view_state *s,
3298 struct commit_queue_entry *entry)
3300 const struct got_error *err;
3301 const struct got_object_id_queue *parent_ids;
3302 struct got_commit_object *selected_commit;
3303 struct got_object_qid *pid;
3306 s->id2 = got_object_id_dup(entry->id);
3308 return got_error_from_errno("got_object_id_dup");
3310 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3313 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3315 pid = SIMPLEQ_FIRST(parent_ids);
3316 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3317 got_object_commit_close(selected_commit);
3321 static const struct got_error *
3322 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3323 struct tog_view **focus_view, struct tog_view *view, int ch)
3325 const struct got_error *err = NULL;
3326 struct tog_diff_view_state *s = &view->state.diff;
3327 struct tog_log_view_state *ls;
3328 struct commit_queue_entry *entry;
3334 if (s->first_displayed_line > 1)
3335 s->first_displayed_line--;
3339 if (s->first_displayed_line == 1)
3342 while (i++ < view->nlines - 1 &&
3343 s->first_displayed_line > 1)
3344 s->first_displayed_line--;
3349 s->first_displayed_line++;
3357 while (!s->eof && i++ < view->nlines - 1) {
3359 line = parse_next_line(s->f, NULL);
3360 s->first_displayed_line++;
3366 if (s->diff_context > 0) {
3368 diff_view_indicate_progress(view);
3369 err = create_diff(s);
3373 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3375 diff_view_indicate_progress(view);
3376 err = create_diff(s);
3381 if (s->log_view == NULL)
3383 ls = &s->log_view->state.log;
3384 entry = TAILQ_PREV(ls->selected_entry,
3385 commit_queue_head, entry);
3389 err = input_log_view(NULL, NULL, NULL, s->log_view,
3394 err = set_selected_commit(s, entry);
3398 s->first_displayed_line = 1;
3399 s->last_displayed_line = view->nlines;
3401 diff_view_indicate_progress(view);
3402 err = create_diff(s);
3406 if (s->log_view == NULL)
3408 ls = &s->log_view->state.log;
3410 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3411 ls->thread_args.commits_needed++;
3413 /* Display "loading..." in log view. */
3414 show_log_view(s->log_view);
3418 err = trigger_log_thread(1 /* load_all */,
3419 &ls->thread_args.commits_needed,
3420 &ls->thread_args.log_complete,
3421 &ls->thread_args.need_commits);
3425 err = input_log_view(NULL, NULL, NULL, s->log_view,
3430 entry = TAILQ_NEXT(ls->selected_entry, entry);
3434 err = set_selected_commit(s, entry);
3438 s->first_displayed_line = 1;
3439 s->last_displayed_line = view->nlines;
3441 diff_view_indicate_progress(view);
3442 err = create_diff(s);
3451 static const struct got_error *
3452 cmd_diff(int argc, char *argv[])
3454 const struct got_error *error = NULL;
3455 struct got_repository *repo = NULL;
3456 struct got_reflist_head refs;
3457 struct got_object_id *id1 = NULL, *id2 = NULL;
3458 char *repo_path = NULL;
3459 char *id_str1 = NULL, *id_str2 = NULL;
3461 struct tog_view *view;
3463 SIMPLEQ_INIT(&refs);
3466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3471 while ((ch = getopt(argc, argv, "")) != -1) {
3483 usage_diff(); /* TODO show local worktree changes */
3484 } else if (argc == 2) {
3485 repo_path = getcwd(NULL, 0);
3486 if (repo_path == NULL)
3487 return got_error_from_errno("getcwd");
3490 } else if (argc == 3) {
3491 repo_path = realpath(argv[0], NULL);
3492 if (repo_path == NULL)
3493 return got_error_from_errno2("realpath", argv[0]);
3501 error = got_repo_open(&repo, repo_path, NULL);
3505 error = apply_unveil(got_repo_get_path(repo), NULL);
3509 error = got_repo_match_object_id_prefix(&id1, id_str1,
3510 GOT_OBJ_TYPE_ANY, repo);
3514 error = got_repo_match_object_id_prefix(&id2, id_str2,
3515 GOT_OBJ_TYPE_ANY, repo);
3519 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3523 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3525 error = got_error_from_errno("view_open");
3528 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3531 error = view_loop(view);
3535 got_repo_close(repo);
3536 got_ref_list_free(&refs);
3544 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3549 struct tog_blame_line {
3551 struct got_object_id *id;
3554 static const struct got_error *
3555 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3556 const char *path, struct tog_blame_line *lines, int nlines,
3557 int blame_complete, int selected_line, int *first_displayed_line,
3558 int *last_displayed_line, int *eof, int max_lines,
3559 struct tog_colors *colors)
3561 const struct got_error *err;
3562 int lineno = 0, nprinted = 0;
3567 struct tog_blame_line *blame_line;
3568 struct got_object_id *prev_id = NULL;
3570 struct tog_color *tc;
3572 err = got_object_id_str(&id_str, id);
3577 werase(view->window);
3579 if (asprintf(&line, "commit %s", id_str) == -1) {
3580 err = got_error_from_errno("asprintf");
3585 err = format_line(&wline, &width, line, view->ncols, 0);
3590 if (view_needs_focus_indication(view))
3591 wstandout(view->window);
3592 tc = get_color(colors, TOG_COLOR_COMMIT);
3594 wattr_on(view->window,
3595 COLOR_PAIR(tc->colorpair), NULL);
3596 waddwstr(view->window, wline);
3598 wattr_off(view->window,
3599 COLOR_PAIR(tc->colorpair), NULL);
3600 if (view_needs_focus_indication(view))
3601 wstandend(view->window);
3604 if (width < view->ncols - 1)
3605 waddch(view->window, '\n');
3607 if (asprintf(&line, "[%d/%d] %s%s",
3608 *first_displayed_line - 1 + selected_line, nlines,
3609 blame_complete ? "" : "annotating... ", path) == -1) {
3611 return got_error_from_errno("asprintf");
3614 err = format_line(&wline, &width, line, view->ncols, 0);
3619 waddwstr(view->window, wline);
3622 if (width < view->ncols - 1)
3623 waddch(view->window, '\n');
3626 while (nprinted < max_lines - 2) {
3627 line = parse_next_line(f, &len);
3632 if (++lineno < *first_displayed_line) {
3637 if (view->ncols <= 9) {
3639 wline = wcsdup(L"");
3641 err = got_error_from_errno("wcsdup");
3643 err = format_line(&wline, &width, line,
3644 view->ncols - 9, 9);
3652 if (view->focussed && nprinted == selected_line - 1)
3653 wstandout(view->window);
3656 blame_line = &lines[lineno - 1];
3657 if (blame_line->annotated && prev_id &&
3658 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3660 nprinted == selected_line - 1)) {
3661 waddstr(view->window, " ");
3662 } else if (blame_line->annotated) {
3664 err = got_object_id_str(&id_str, blame_line->id);
3670 tc = get_color(colors, TOG_COLOR_COMMIT);
3672 wattr_on(view->window,
3673 COLOR_PAIR(tc->colorpair), NULL);
3674 wprintw(view->window, "%.8s", id_str);
3676 wattr_off(view->window,
3677 COLOR_PAIR(tc->colorpair), NULL);
3679 prev_id = blame_line->id;
3681 waddstr(view->window, "........");
3685 waddstr(view->window, "........");
3689 if (view->focussed && nprinted == selected_line - 1)
3690 wstandend(view->window);
3691 waddstr(view->window, " ");
3693 waddwstr(view->window, wline);
3694 if (width <= view->ncols - 1)
3695 waddch(view->window, '\n');
3696 if (++nprinted == 1)
3697 *first_displayed_line = lineno;
3702 *last_displayed_line = lineno;
3709 static const struct got_error *
3710 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3712 const struct got_error *err = NULL;
3713 struct tog_blame_cb_args *a = arg;
3714 struct tog_blame_line *line;
3717 if (nlines != a->nlines ||
3718 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3719 return got_error(GOT_ERR_RANGE);
3721 errcode = pthread_mutex_lock(&tog_mutex);
3723 return got_error_set_errno(errcode, "pthread_mutex_lock");
3725 if (*a->quit) { /* user has quit the blame view */
3726 err = got_error(GOT_ERR_ITER_COMPLETED);
3731 goto done; /* no change in this commit */
3733 line = &a->lines[lineno - 1];
3734 if (line->annotated)
3737 line->id = got_object_id_dup(id);
3738 if (line->id == NULL) {
3739 err = got_error_from_errno("got_object_id_dup");
3742 line->annotated = 1;
3744 errcode = pthread_mutex_unlock(&tog_mutex);
3746 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3751 blame_thread(void *arg)
3753 const struct got_error *err;
3754 struct tog_blame_thread_args *ta = arg;
3755 struct tog_blame_cb_args *a = ta->cb_args;
3758 err = block_signals_used_by_main_thread();
3762 err = got_blame(ta->path, a->commit_id, ta->repo,
3763 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3764 if (err && err->code == GOT_ERR_CANCELLED)
3767 errcode = pthread_mutex_lock(&tog_mutex);
3769 return (void *)got_error_set_errno(errcode,
3770 "pthread_mutex_lock");
3772 got_repo_close(ta->repo);
3776 errcode = pthread_mutex_unlock(&tog_mutex);
3777 if (errcode && err == NULL)
3778 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3783 static struct got_object_id *
3784 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3785 int first_displayed_line, int selected_line)
3787 struct tog_blame_line *line;
3792 line = &lines[first_displayed_line - 1 + selected_line - 1];
3793 if (!line->annotated)
3799 static const struct got_error *
3800 stop_blame(struct tog_blame *blame)
3802 const struct got_error *err = NULL;
3805 if (blame->thread) {
3807 errcode = pthread_mutex_unlock(&tog_mutex);
3809 return got_error_set_errno(errcode,
3810 "pthread_mutex_unlock");
3811 errcode = pthread_join(blame->thread, (void **)&err);
3813 return got_error_set_errno(errcode, "pthread_join");
3814 errcode = pthread_mutex_lock(&tog_mutex);
3816 return got_error_set_errno(errcode,
3817 "pthread_mutex_lock");
3818 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3820 blame->thread = NULL;
3822 if (blame->thread_args.repo) {
3823 got_repo_close(blame->thread_args.repo);
3824 blame->thread_args.repo = NULL;
3827 if (fclose(blame->f) != 0 && err == NULL)
3828 err = got_error_from_errno("fclose");
3832 for (i = 0; i < blame->nlines; i++)
3833 free(blame->lines[i].id);
3835 blame->lines = NULL;
3837 free(blame->cb_args.commit_id);
3838 blame->cb_args.commit_id = NULL;
3843 static const struct got_error *
3844 cancel_blame_view(void *arg)
3846 const struct got_error *err = NULL;
3850 errcode = pthread_mutex_lock(&tog_mutex);
3852 return got_error_set_errno(errcode,
3853 "pthread_mutex_unlock");
3856 err = got_error(GOT_ERR_CANCELLED);
3858 errcode = pthread_mutex_unlock(&tog_mutex);
3860 return got_error_set_errno(errcode,
3861 "pthread_mutex_lock");
3866 static const struct got_error *
3867 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3868 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3869 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3870 struct got_repository *repo)
3872 const struct got_error *err = NULL;
3873 struct got_blob_object *blob = NULL;
3874 struct got_repository *thread_repo = NULL;
3875 struct got_object_id *obj_id = NULL;
3878 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3882 err = got_object_get_type(&obj_type, repo, obj_id);
3886 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3887 err = got_error(GOT_ERR_OBJ_TYPE);
3891 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3894 blame->f = got_opentemp();
3895 if (blame->f == NULL) {
3896 err = got_error_from_errno("got_opentemp");
3899 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3900 &blame->line_offsets, blame->f, blob);
3901 if (err || blame->nlines == 0)
3904 /* Don't include \n at EOF in the blame line count. */
3905 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3908 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3909 if (blame->lines == NULL) {
3910 err = got_error_from_errno("calloc");
3914 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3918 blame->cb_args.view = view;
3919 blame->cb_args.lines = blame->lines;
3920 blame->cb_args.nlines = blame->nlines;
3921 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3922 if (blame->cb_args.commit_id == NULL) {
3923 err = got_error_from_errno("got_object_id_dup");
3926 blame->cb_args.quit = done;
3928 blame->thread_args.path = path;
3929 blame->thread_args.repo = thread_repo;
3930 blame->thread_args.cb_args = &blame->cb_args;
3931 blame->thread_args.complete = blame_complete;
3932 blame->thread_args.cancel_cb = cancel_blame_view;
3933 blame->thread_args.cancel_arg = done;
3934 *blame_complete = 0;
3938 got_object_blob_close(blob);
3945 static const struct got_error *
3946 open_blame_view(struct tog_view *view, char *path,
3947 struct got_object_id *commit_id, struct got_reflist_head *refs,
3948 struct got_repository *repo)
3950 const struct got_error *err = NULL;
3951 struct tog_blame_view_state *s = &view->state.blame;
3953 SIMPLEQ_INIT(&s->blamed_commits);
3955 s->path = strdup(path);
3956 if (s->path == NULL)
3957 return got_error_from_errno("strdup");
3959 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3965 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3966 s->first_displayed_line = 1;
3967 s->last_displayed_line = view->nlines;
3968 s->selected_line = 1;
3969 s->blame_complete = 0;
3972 s->commit_id = commit_id;
3973 memset(&s->blame, 0, sizeof(s->blame));
3975 SIMPLEQ_INIT(&s->colors);
3976 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3977 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3978 get_color_value("TOG_COLOR_COMMIT"));
3983 view->show = show_blame_view;
3984 view->input = input_blame_view;
3985 view->close = close_blame_view;
3986 view->search_start = search_start_blame_view;
3987 view->search_next = search_next_blame_view;
3989 return run_blame(&s->blame, view, &s->blame_complete,
3990 &s->first_displayed_line, &s->last_displayed_line,
3991 &s->selected_line, &s->done, &s->eof, s->path,
3992 s->blamed_commit->id, s->repo);
3995 static const struct got_error *
3996 close_blame_view(struct tog_view *view)
3998 const struct got_error *err = NULL;
3999 struct tog_blame_view_state *s = &view->state.blame;
4001 if (s->blame.thread)
4002 err = stop_blame(&s->blame);
4004 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4005 struct got_object_qid *blamed_commit;
4006 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4007 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4008 got_object_qid_free(blamed_commit);
4012 free_colors(&s->colors);
4017 static const struct got_error *
4018 search_start_blame_view(struct tog_view *view)
4020 struct tog_blame_view_state *s = &view->state.blame;
4022 s->matched_line = 0;
4026 static const struct got_error *
4027 search_next_blame_view(struct tog_view *view)
4029 struct tog_blame_view_state *s = &view->state.blame;
4032 if (!view->searching) {
4033 view->search_next_done = 1;
4037 if (s->matched_line) {
4038 if (view->searching == TOG_SEARCH_FORWARD)
4039 lineno = s->matched_line + 1;
4041 lineno = s->matched_line - 1;
4043 if (view->searching == TOG_SEARCH_FORWARD)
4046 lineno = s->blame.nlines;
4054 if (lineno <= 0 || lineno > s->blame.nlines) {
4055 if (s->matched_line == 0) {
4056 view->search_next_done = 1;
4061 if (view->searching == TOG_SEARCH_FORWARD)
4064 lineno = s->blame.nlines;
4067 offset = s->blame.line_offsets[lineno - 1];
4068 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4070 return got_error_from_errno("fseeko");
4073 line = parse_next_line(s->blame.f, &len);
4074 if (line && match_line(line, &view->regex)) {
4075 view->search_next_done = 1;
4076 s->matched_line = lineno;
4081 if (view->searching == TOG_SEARCH_FORWARD)
4087 if (s->matched_line) {
4088 s->first_displayed_line = s->matched_line;
4089 s->selected_line = 1;
4095 static const struct got_error *
4096 show_blame_view(struct tog_view *view)
4098 const struct got_error *err = NULL;
4099 struct tog_blame_view_state *s = &view->state.blame;
4102 if (s->blame.thread == NULL) {
4103 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4104 &s->blame.thread_args);
4106 return got_error_set_errno(errcode, "pthread_create");
4108 halfdelay(1); /* fast refresh while annotating */
4111 if (s->blame_complete)
4112 halfdelay(10); /* disable fast refresh */
4114 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4115 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4116 s->selected_line, &s->first_displayed_line,
4117 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4123 static const struct got_error *
4124 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4125 struct tog_view **focus_view, struct tog_view *view, int ch)
4127 const struct got_error *err = NULL, *thread_err = NULL;
4128 struct tog_view *diff_view;
4129 struct tog_blame_view_state *s = &view->state.blame;
4138 if (s->selected_line > 1)
4140 else if (s->selected_line == 1 &&
4141 s->first_displayed_line > 1)
4142 s->first_displayed_line--;
4145 if (s->first_displayed_line == 1) {
4146 s->selected_line = 1;
4149 if (s->first_displayed_line > view->nlines - 2)
4150 s->first_displayed_line -=
4153 s->first_displayed_line = 1;
4157 if (s->selected_line < view->nlines - 2 &&
4158 s->first_displayed_line +
4159 s->selected_line <= s->blame.nlines)
4161 else if (s->last_displayed_line <
4163 s->first_displayed_line++;
4167 struct got_object_id *id = NULL;
4168 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4169 s->first_displayed_line, s->selected_line);
4173 struct got_commit_object *commit;
4174 struct got_object_qid *pid;
4175 struct got_object_id *blob_id = NULL;
4177 err = got_object_open_as_commit(&commit,
4181 pid = SIMPLEQ_FIRST(
4182 got_object_commit_get_parent_ids(commit));
4184 got_object_commit_close(commit);
4187 /* Check if path history ends here. */
4188 err = got_object_id_by_path(&blob_id, s->repo,
4191 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4193 got_object_commit_close(commit);
4196 err = got_object_get_type(&obj_type, s->repo,
4199 /* Can't blame non-blob type objects. */
4200 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4201 got_object_commit_close(commit);
4204 err = got_object_qid_alloc(&s->blamed_commit,
4206 got_object_commit_close(commit);
4208 if (got_object_id_cmp(id,
4209 s->blamed_commit->id) == 0)
4211 err = got_object_qid_alloc(&s->blamed_commit,
4217 thread_err = stop_blame(&s->blame);
4221 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4222 s->blamed_commit, entry);
4223 err = run_blame(&s->blame, view, &s->blame_complete,
4224 &s->first_displayed_line, &s->last_displayed_line,
4225 &s->selected_line, &s->done, &s->eof,
4226 s->path, s->blamed_commit->id, s->repo);
4232 struct got_object_qid *first;
4233 first = SIMPLEQ_FIRST(&s->blamed_commits);
4234 if (!got_object_id_cmp(first->id, s->commit_id))
4237 thread_err = stop_blame(&s->blame);
4241 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4242 got_object_qid_free(s->blamed_commit);
4244 SIMPLEQ_FIRST(&s->blamed_commits);
4245 err = run_blame(&s->blame, view, &s->blame_complete,
4246 &s->first_displayed_line, &s->last_displayed_line,
4247 &s->selected_line, &s->done, &s->eof, s->path,
4248 s->blamed_commit->id, s->repo);
4255 struct got_object_id *id = NULL;
4256 struct got_object_qid *pid;
4257 struct got_commit_object *commit = NULL;
4258 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4259 s->first_displayed_line, s->selected_line);
4262 err = got_object_open_as_commit(&commit, s->repo, id);
4265 pid = SIMPLEQ_FIRST(
4266 got_object_commit_get_parent_ids(commit));
4267 if (view_is_parent_view(view))
4268 begin_x = view_split_begin_x(view->begin_x);
4269 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4270 if (diff_view == NULL) {
4271 got_object_commit_close(commit);
4272 err = got_error_from_errno("view_open");
4275 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4276 id, NULL, s->refs, s->repo);
4277 got_object_commit_close(commit);
4279 view_close(diff_view);
4282 if (view_is_parent_view(view)) {
4283 err = view_close_child(view);
4286 err = view_set_child(view, diff_view);
4288 view_close(diff_view);
4291 *focus_view = diff_view;
4292 view->child_focussed = 1;
4294 *new_view = diff_view;
4301 if (s->last_displayed_line >= s->blame.nlines &&
4302 s->selected_line >= MIN(s->blame.nlines,
4303 view->nlines - 2)) {
4306 if (s->last_displayed_line >= s->blame.nlines &&
4307 s->selected_line < view->nlines - 2) {
4308 s->selected_line = MIN(s->blame.nlines,
4312 if (s->last_displayed_line + view->nlines - 2
4314 s->first_displayed_line +=
4317 s->first_displayed_line =
4322 if (s->selected_line > view->nlines - 2) {
4323 s->selected_line = MIN(s->blame.nlines,
4330 return thread_err ? thread_err : err;
4333 static const struct got_error *
4334 cmd_blame(int argc, char *argv[])
4336 const struct got_error *error;
4337 struct got_repository *repo = NULL;
4338 struct got_reflist_head refs;
4339 struct got_worktree *worktree = NULL;
4340 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4341 struct got_object_id *commit_id = NULL;
4342 char *commit_id_str = NULL;
4344 struct tog_view *view;
4346 SIMPLEQ_INIT(&refs);
4349 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4354 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4357 commit_id_str = optarg;
4360 repo_path = realpath(optarg, NULL);
4361 if (repo_path == NULL)
4362 return got_error_from_errno2("realpath",
4379 cwd = getcwd(NULL, 0);
4381 error = got_error_from_errno("getcwd");
4384 if (repo_path == NULL) {
4385 error = got_worktree_open(&worktree, cwd);
4386 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4392 strdup(got_worktree_get_repo_path(worktree));
4393 if (repo_path == NULL)
4394 error = got_error_from_errno("strdup");
4398 repo_path = strdup(cwd);
4399 if (repo_path == NULL) {
4400 error = got_error_from_errno("strdup");
4408 error = got_repo_open(&repo, repo_path, NULL);
4412 error = apply_unveil(got_repo_get_path(repo), NULL);
4417 const char *prefix = got_worktree_get_path_prefix(worktree);
4418 char *p, *worktree_subdir = cwd +
4419 strlen(got_worktree_get_root_path(worktree));
4420 if (asprintf(&p, "%s%s%s%s%s",
4421 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4422 worktree_subdir, worktree_subdir[0] ? "/" : "",
4424 error = got_error_from_errno("asprintf");
4427 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4430 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4435 if (commit_id_str == NULL) {
4436 struct got_reference *head_ref;
4437 error = got_ref_open(&head_ref, repo, worktree ?
4438 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4441 error = got_ref_resolve(&commit_id, repo, head_ref);
4442 got_ref_close(head_ref);
4444 error = got_repo_match_object_id(&commit_id, NULL,
4445 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4450 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4454 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4456 error = got_error_from_errno("view_open");
4459 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4463 /* Release work tree lock. */
4464 got_worktree_close(worktree);
4467 error = view_loop(view);
4473 got_worktree_close(worktree);
4475 got_repo_close(repo);
4476 got_ref_list_free(&refs);
4480 static const struct got_error *
4481 draw_tree_entries(struct tog_view *view,
4482 struct got_tree_entry **first_displayed_entry,
4483 struct got_tree_entry **last_displayed_entry,
4484 struct got_tree_entry **selected_entry, int *ndisplayed,
4485 const char *label, int show_ids, const char *parent_path,
4486 struct got_tree_object *tree, int selected, int limit,
4487 int isroot, struct tog_colors *colors)
4489 const struct got_error *err = NULL;
4490 struct got_tree_entry *te;
4492 struct tog_color *tc;
4493 int width, n, i, nentries;
4497 werase(view->window);
4502 err = format_line(&wline, &width, label, view->ncols, 0);
4505 if (view_needs_focus_indication(view))
4506 wstandout(view->window);
4507 tc = get_color(colors, TOG_COLOR_COMMIT);
4509 wattr_on(view->window,
4510 COLOR_PAIR(tc->colorpair), NULL);
4511 waddwstr(view->window, wline);
4513 wattr_off(view->window,
4514 COLOR_PAIR(tc->colorpair), NULL);
4515 if (view_needs_focus_indication(view))
4516 wstandend(view->window);
4519 if (width < view->ncols - 1)
4520 waddch(view->window, '\n');
4523 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4526 waddwstr(view->window, wline);
4529 if (width < view->ncols - 1)
4530 waddch(view->window, '\n');
4533 waddch(view->window, '\n');
4537 if (*first_displayed_entry == NULL) {
4538 te = got_object_tree_get_first_entry(tree);
4539 if (selected == 0) {
4541 wstandout(view->window);
4542 *selected_entry = NULL;
4544 waddstr(view->window, " ..\n"); /* parent directory */
4545 if (selected == 0 && view->focussed)
4546 wstandend(view->window);
4553 te = *first_displayed_entry;
4556 nentries = got_object_tree_get_nentries(tree);
4557 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4558 char *line = NULL, *id_str = NULL;
4559 const char *modestr = "";
4562 te = got_object_tree_get_entry(tree, i);
4563 mode = got_tree_entry_get_mode(te);
4566 err = got_object_id_str(&id_str,
4567 got_tree_entry_get_id(te));
4569 return got_error_from_errno(
4570 "got_object_id_str");
4572 if (got_object_tree_entry_is_submodule(te))
4574 else if (S_ISLNK(mode))
4576 else if (S_ISDIR(mode))
4578 else if (mode & S_IXUSR)
4580 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4581 got_tree_entry_get_name(te), modestr) == -1) {
4583 return got_error_from_errno("asprintf");
4586 err = format_line(&wline, &width, line, view->ncols, 0);
4591 if (n == selected) {
4593 wstandout(view->window);
4594 *selected_entry = te;
4596 tc = match_color(colors, line);
4598 wattr_on(view->window,
4599 COLOR_PAIR(tc->colorpair), NULL);
4600 waddwstr(view->window, wline);
4602 wattr_off(view->window,
4603 COLOR_PAIR(tc->colorpair), NULL);
4604 if (width < view->ncols - 1)
4605 waddch(view->window, '\n');
4606 if (n == selected && view->focussed)
4607 wstandend(view->window);
4613 *last_displayed_entry = te;
4622 tree_scroll_up(struct tog_view *view,
4623 struct got_tree_entry **first_displayed_entry, int maxscroll,
4624 struct got_tree_object *tree, int isroot)
4626 struct got_tree_entry *te;
4629 if (*first_displayed_entry == NULL)
4632 te = got_object_tree_get_entry(tree, 0);
4633 if (*first_displayed_entry == te) {
4635 *first_displayed_entry = NULL;
4640 while (*first_displayed_entry && i < maxscroll) {
4641 *first_displayed_entry = got_tree_entry_get_prev(tree,
4642 *first_displayed_entry);
4645 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4646 *first_displayed_entry = NULL;
4650 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4651 struct got_tree_entry *last_displayed_entry,
4652 struct got_tree_object *tree)
4654 struct got_tree_entry *next, *last;
4657 if (*first_displayed_entry)
4658 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4660 next = got_object_tree_get_first_entry(tree);
4662 last = last_displayed_entry;
4663 while (next && last && n++ < maxscroll) {
4664 last = got_tree_entry_get_next(tree, last);
4666 *first_displayed_entry = next;
4667 next = got_tree_entry_get_next(tree, next);
4673 static const struct got_error *
4674 tree_entry_path(char **path, struct tog_parent_trees *parents,
4675 struct got_tree_entry *te)
4677 const struct got_error *err = NULL;
4678 struct tog_parent_tree *pt;
4679 size_t len = 2; /* for leading slash and NUL */
4681 TAILQ_FOREACH(pt, parents, entry)
4682 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4685 len += strlen(got_tree_entry_get_name(te));
4687 *path = calloc(1, len);
4689 return got_error_from_errno("calloc");
4692 pt = TAILQ_LAST(parents, tog_parent_trees);
4694 const char *name = got_tree_entry_get_name(pt->selected_entry);
4695 if (strlcat(*path, name, len) >= len) {
4696 err = got_error(GOT_ERR_NO_SPACE);
4699 if (strlcat(*path, "/", len) >= len) {
4700 err = got_error(GOT_ERR_NO_SPACE);
4703 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4706 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4707 err = got_error(GOT_ERR_NO_SPACE);
4719 static const struct got_error *
4720 blame_tree_entry(struct tog_view **new_view, int begin_x,
4721 struct got_tree_entry *te, struct tog_parent_trees *parents,
4722 struct got_object_id *commit_id, struct got_reflist_head *refs,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL;
4727 struct tog_view *blame_view;
4731 err = tree_entry_path(&path, parents, te);
4735 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4736 if (blame_view == NULL) {
4737 err = got_error_from_errno("view_open");
4741 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4743 if (err->code == GOT_ERR_CANCELLED)
4745 view_close(blame_view);
4747 *new_view = blame_view;
4753 static const struct got_error *
4754 log_tree_entry(struct tog_view **new_view, int begin_x,
4755 struct got_tree_entry *te, struct tog_parent_trees *parents,
4756 struct got_object_id *commit_id, struct got_reflist_head *refs,
4757 struct got_repository *repo)
4759 struct tog_view *log_view;
4760 const struct got_error *err = NULL;
4765 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4766 if (log_view == NULL)
4767 return got_error_from_errno("view_open");
4769 err = tree_entry_path(&path, parents, te);
4773 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4775 view_close(log_view);
4777 *new_view = log_view;
4782 static const struct got_error *
4783 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4784 struct got_object_id *commit_id, struct got_reflist_head *refs,
4785 struct got_repository *repo)
4787 const struct got_error *err = NULL;
4788 char *commit_id_str = NULL;
4789 struct tog_tree_view_state *s = &view->state.tree;
4791 TAILQ_INIT(&s->parents);
4793 err = got_object_id_str(&commit_id_str, commit_id);
4797 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4798 err = got_error_from_errno("asprintf");
4802 s->root = s->tree = root;
4803 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4804 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4805 s->commit_id = got_object_id_dup(commit_id);
4806 if (s->commit_id == NULL) {
4807 err = got_error_from_errno("got_object_id_dup");
4813 SIMPLEQ_INIT(&s->colors);
4815 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4816 err = add_color(&s->colors, "\\$$",
4817 TOG_COLOR_TREE_SUBMODULE,
4818 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4821 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4822 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4824 free_colors(&s->colors);
4827 err = add_color(&s->colors, "/$",
4828 TOG_COLOR_TREE_DIRECTORY,
4829 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4831 free_colors(&s->colors);
4835 err = add_color(&s->colors, "\\*$",
4836 TOG_COLOR_TREE_EXECUTABLE,
4837 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4839 free_colors(&s->colors);
4843 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4844 get_color_value("TOG_COLOR_COMMIT"));
4846 free_colors(&s->colors);
4851 view->show = show_tree_view;
4852 view->input = input_tree_view;
4853 view->close = close_tree_view;
4854 view->search_start = search_start_tree_view;
4855 view->search_next = search_next_tree_view;
4857 free(commit_id_str);
4859 free(s->tree_label);
4860 s->tree_label = NULL;
4865 static const struct got_error *
4866 close_tree_view(struct tog_view *view)
4868 struct tog_tree_view_state *s = &view->state.tree;
4870 free_colors(&s->colors);
4871 free(s->tree_label);
4872 s->tree_label = NULL;
4874 s->commit_id = NULL;
4875 while (!TAILQ_EMPTY(&s->parents)) {
4876 struct tog_parent_tree *parent;
4877 parent = TAILQ_FIRST(&s->parents);
4878 TAILQ_REMOVE(&s->parents, parent, entry);
4882 if (s->tree != s->root)
4883 got_object_tree_close(s->tree);
4884 got_object_tree_close(s->root);
4889 static const struct got_error *
4890 search_start_tree_view(struct tog_view *view)
4892 struct tog_tree_view_state *s = &view->state.tree;
4894 s->matched_entry = NULL;
4899 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4901 regmatch_t regmatch;
4903 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4907 static const struct got_error *
4908 search_next_tree_view(struct tog_view *view)
4910 struct tog_tree_view_state *s = &view->state.tree;
4911 struct got_tree_entry *te = NULL;
4913 if (!view->searching) {
4914 view->search_next_done = 1;
4918 if (s->matched_entry) {
4919 if (view->searching == TOG_SEARCH_FORWARD) {
4920 if (s->selected_entry)
4921 te = got_tree_entry_get_next(s->tree,
4924 te = got_object_tree_get_first_entry(s->tree);
4926 if (s->selected_entry == NULL)
4927 te = got_object_tree_get_last_entry(s->tree);
4929 te = got_tree_entry_get_prev(s->tree,
4933 if (view->searching == TOG_SEARCH_FORWARD)
4934 te = got_object_tree_get_first_entry(s->tree);
4936 te = got_object_tree_get_last_entry(s->tree);
4941 if (s->matched_entry == NULL) {
4942 view->search_next_done = 1;
4945 if (view->searching == TOG_SEARCH_FORWARD)
4946 te = got_object_tree_get_first_entry(s->tree);
4948 te = got_object_tree_get_last_entry(s->tree);
4951 if (match_tree_entry(te, &view->regex)) {
4952 view->search_next_done = 1;
4953 s->matched_entry = te;
4957 if (view->searching == TOG_SEARCH_FORWARD)
4958 te = got_tree_entry_get_next(s->tree, te);
4960 te = got_tree_entry_get_prev(s->tree, te);
4963 if (s->matched_entry) {
4964 s->first_displayed_entry = s->matched_entry;
4971 static const struct got_error *
4972 show_tree_view(struct tog_view *view)
4974 const struct got_error *err = NULL;
4975 struct tog_tree_view_state *s = &view->state.tree;
4978 err = tree_entry_path(&parent_path, &s->parents, NULL);
4982 err = draw_tree_entries(view, &s->first_displayed_entry,
4983 &s->last_displayed_entry, &s->selected_entry,
4984 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4985 s->tree, s->selected, view->nlines, s->tree == s->root,
4993 static const struct got_error *
4994 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4995 struct tog_view **focus_view, struct tog_view *view, int ch)
4997 const struct got_error *err = NULL;
4998 struct tog_tree_view_state *s = &view->state.tree;
4999 struct tog_view *log_view;
5000 int begin_x = 0, nscrolled;
5004 s->show_ids = !s->show_ids;
5007 if (!s->selected_entry)
5009 if (view_is_parent_view(view))
5010 begin_x = view_split_begin_x(view->begin_x);
5011 err = log_tree_entry(&log_view, begin_x,
5012 s->selected_entry, &s->parents,
5013 s->commit_id, s->refs, s->repo);
5014 if (view_is_parent_view(view)) {
5015 err = view_close_child(view);
5018 err = view_set_child(view, log_view);
5020 view_close(log_view);
5023 *focus_view = log_view;
5024 view->child_focussed = 1;
5026 *new_view = log_view;
5030 if (s->selected > 0) {
5032 if (s->selected == 0)
5035 if (s->selected > 0)
5037 tree_scroll_up(view, &s->first_displayed_entry, 1,
5038 s->tree, s->tree == s->root);
5041 tree_scroll_up(view, &s->first_displayed_entry,
5042 MAX(0, view->nlines - 4 - s->selected), s->tree,
5043 s->tree == s->root);
5045 if (got_object_tree_get_first_entry(s->tree) ==
5046 s->first_displayed_entry && s->tree != s->root)
5047 s->first_displayed_entry = NULL;
5051 if (s->selected < s->ndisplayed - 1) {
5055 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5057 /* can't scroll any further */
5059 tree_scroll_down(&s->first_displayed_entry, 1,
5060 s->last_displayed_entry, s->tree);
5063 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5065 /* can't scroll any further; move cursor down */
5066 if (s->selected < s->ndisplayed - 1)
5067 s->selected = s->ndisplayed - 1;
5070 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5071 view->nlines, s->last_displayed_entry, s->tree);
5072 if (nscrolled < view->nlines) {
5074 struct got_tree_entry *te;
5075 te = s->first_displayed_entry;
5078 te = got_tree_entry_get_next(s->tree, te);
5080 s->selected = ndisplayed - 1;
5086 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5087 struct tog_parent_tree *parent;
5088 /* user selected '..' */
5089 if (s->tree == s->root)
5091 parent = TAILQ_FIRST(&s->parents);
5092 TAILQ_REMOVE(&s->parents, parent,
5094 got_object_tree_close(s->tree);
5095 s->tree = parent->tree;
5096 s->first_displayed_entry =
5097 parent->first_displayed_entry;
5099 parent->selected_entry;
5100 s->selected = parent->selected;
5102 } else if (S_ISDIR(got_tree_entry_get_mode(
5103 s->selected_entry))) {
5104 struct got_tree_object *subtree;
5105 err = got_object_open_as_tree(&subtree, s->repo,
5106 got_tree_entry_get_id(s->selected_entry));
5109 err = tree_view_visit_subtree(subtree, s);
5111 got_object_tree_close(subtree);
5114 } else if (S_ISREG(got_tree_entry_get_mode(
5115 s->selected_entry))) {
5116 struct tog_view *blame_view;
5117 int begin_x = view_is_parent_view(view) ?
5118 view_split_begin_x(view->begin_x) : 0;
5120 err = blame_tree_entry(&blame_view, begin_x,
5121 s->selected_entry, &s->parents,
5122 s->commit_id, s->refs, s->repo);
5125 if (view_is_parent_view(view)) {
5126 err = view_close_child(view);
5129 err = view_set_child(view, blame_view);
5131 view_close(blame_view);
5134 *focus_view = blame_view;
5135 view->child_focussed = 1;
5137 *new_view = blame_view;
5141 if (s->selected > view->nlines)
5142 s->selected = s->ndisplayed - 1;
5155 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path]\n",
5160 static const struct got_error *
5161 cmd_tree(int argc, char *argv[])
5163 const struct got_error *error;
5164 struct got_repository *repo = NULL;
5165 struct got_reflist_head refs;
5166 char *repo_path = NULL;
5167 struct got_object_id *commit_id = NULL;
5168 char *commit_id_arg = NULL;
5169 struct got_commit_object *commit = NULL;
5170 struct got_tree_object *tree = NULL;
5172 struct tog_view *view;
5174 SIMPLEQ_INIT(&refs);
5177 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5182 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5185 commit_id_arg = optarg;
5188 repo_path = realpath(optarg, NULL);
5189 if (repo_path == NULL)
5190 return got_error_from_errno2("realpath",
5205 if (repo_path == NULL) {
5206 struct got_worktree *worktree;
5207 char *cwd = getcwd(NULL, 0);
5209 return got_error_from_errno("getcwd");
5210 error = got_worktree_open(&worktree, cwd);
5211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5218 strdup(got_worktree_get_repo_path(worktree));
5219 got_worktree_close(worktree);
5222 if (repo_path == NULL) {
5223 error = got_error_from_errno("strdup");
5230 error = got_repo_open(&repo, repo_path, NULL);
5234 error = apply_unveil(got_repo_get_path(repo), NULL);
5238 error = got_repo_match_object_id(&commit_id, NULL,
5239 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5240 GOT_OBJ_TYPE_COMMIT, 1, repo);
5244 error = got_object_open_as_commit(&commit, repo, commit_id);
5248 error = got_object_open_as_tree(&tree, repo,
5249 got_object_commit_get_tree_id(commit));
5253 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5257 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5259 error = got_error_from_errno("view_open");
5262 error = open_tree_view(view, tree, commit_id, &refs, repo);
5265 error = view_loop(view);
5270 got_object_commit_close(commit);
5272 got_object_tree_close(tree);
5274 got_repo_close(repo);
5275 got_ref_list_free(&refs);
5284 fprintf(stderr, "commands:");
5285 for (i = 0; i < nitems(tog_commands); i++) {
5286 struct tog_cmd *cmd = &tog_commands[i];
5287 fprintf(stderr, " %s", cmd->name);
5289 fputc('\n', stderr);
5295 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5303 make_argv(const char *arg0, const char *arg1)
5306 int argc = (arg1 == NULL ? 1 : 2);
5308 argv = calloc(argc, sizeof(char *));
5311 argv[0] = strdup(arg0);
5312 if (argv[0] == NULL)
5315 argv[1] = strdup(arg1);
5316 if (argv[1] == NULL)
5324 main(int argc, char *argv[])
5326 const struct got_error *error = NULL;
5327 struct tog_cmd *cmd = NULL;
5328 int ch, hflag = 0, Vflag = 0;
5329 char **cmd_argv = NULL;
5330 static struct option longopts[] = {
5331 { "version", no_argument, NULL, 'V' },
5335 setlocale(LC_CTYPE, "");
5337 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5357 got_version_print_str();
5364 /* Build an argument vector which runs a default command. */
5365 cmd = &tog_commands[0];
5366 cmd_argv = make_argv(cmd->name, NULL);
5371 /* Did the user specific a command? */
5372 for (i = 0; i < nitems(tog_commands); i++) {
5373 if (strncmp(tog_commands[i].name, argv[0],
5374 strlen(argv[0])) == 0) {
5375 cmd = &tog_commands[i];
5381 fprintf(stderr, "%s: unknown command '%s'\n",
5382 getprogname(), argv[0]);
5391 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5395 if (error && error->code != GOT_ERR_CANCELLED)
5396 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);