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;
707 if (view->nlines < 1)
710 if (!view_is_parent_view(view))
711 begin_x = view_split_begin_x(view->begin_x);
712 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
714 wclrtoeol(view->window);
718 ret = wgetnstr(view->window, pattern, sizeof(pattern));
724 if (view->searching) {
725 regfree(&view->regex);
729 if (regcomp(&view->regex, pattern,
730 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 err = view->search_start(view);
733 regfree(&view->regex);
736 view->searching = TOG_SEARCH_FORWARD;
737 view->search_next_done = 0;
738 view->search_next(view);
744 static const struct got_error *
745 view_input(struct tog_view **new, struct tog_view **dead,
746 struct tog_view **focus, int *done, struct tog_view *view,
747 struct tog_view_list_head *views)
749 const struct got_error *err = NULL;
757 if (view->searching && !view->search_next_done) {
758 errcode = pthread_mutex_unlock(&tog_mutex);
760 return got_error_set_errno(errcode,
761 "pthread_mutex_unlock");
763 errcode = pthread_mutex_lock(&tog_mutex);
765 return got_error_set_errno(errcode,
766 "pthread_mutex_lock");
767 view->search_next(view);
771 nodelay(stdscr, FALSE);
772 /* Allow threads to make progress while we are waiting for input. */
773 errcode = pthread_mutex_unlock(&tog_mutex);
775 return got_error_set_errno(errcode, "pthread_mutex_unlock");
776 ch = wgetch(view->window);
777 errcode = pthread_mutex_lock(&tog_mutex);
779 return got_error_set_errno(errcode, "pthread_mutex_lock");
780 nodelay(stdscr, TRUE);
782 if (tog_sigwinch_received || tog_sigcont_received) {
784 tog_sigwinch_received = 0;
785 tog_sigcont_received = 0;
786 TAILQ_FOREACH(v, views, entry) {
787 err = view_resize(v);
790 err = v->input(new, dead, focus, v, KEY_RESIZE);
801 *focus = view->child;
802 view->child_focussed = 1;
803 } else if (view->parent) {
804 *focus = view->parent;
805 view->parent->child_focussed = 0;
809 err = view->input(new, dead, focus, view, ch);
816 if (view_is_parent_view(view)) {
817 if (view->child == NULL)
819 if (view_is_splitscreen(view->child)) {
820 *focus = view->child;
821 view->child_focussed = 1;
822 err = view_fullscreen(view->child);
824 err = view_splitscreen(view->child);
827 err = view->child->input(new, dead, focus,
828 view->child, KEY_RESIZE);
830 if (view_is_splitscreen(view)) {
832 view->parent->child_focussed = 1;
833 err = view_fullscreen(view);
835 err = view_splitscreen(view);
839 err = view->input(new, dead, focus, view,
846 if (view->search_start)
847 view_search_start(view);
849 err = view->input(new, dead, focus, view, ch);
853 if (view->search_next && view->searching) {
854 view->searching = (ch == 'n' ?
855 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
856 view->search_next_done = 0;
857 view->search_next(view);
859 err = view->input(new, dead, focus, view, ch);
862 err = view->input(new, dead, focus, view, ch);
870 view_vborder(struct tog_view *view)
873 struct tog_view *view_above;
876 return view_vborder(view->parent);
878 panel = panel_above(view->panel);
882 view_above = panel_userptr(panel);
883 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
884 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
888 view_needs_focus_indication(struct tog_view *view)
890 if (view_is_parent_view(view)) {
891 if (view->child == NULL || view->child_focussed)
893 if (!view_is_splitscreen(view->child))
895 } else if (!view_is_splitscreen(view))
898 return view->focussed;
901 static const struct got_error *
902 view_loop(struct tog_view *view)
904 const struct got_error *err = NULL;
905 struct tog_view_list_head views;
906 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
907 int fast_refresh = 10;
908 int done = 0, errcode;
910 errcode = pthread_mutex_lock(&tog_mutex);
912 return got_error_set_errno(errcode, "pthread_mutex_lock");
915 TAILQ_INSERT_HEAD(&views, view, entry);
919 err = view->show(view);
924 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
925 /* Refresh fast during initialization, then become slower. */
926 if (fast_refresh && fast_refresh-- == 0)
927 halfdelay(10); /* switch to once per second */
929 err = view_input(&new_view, &dead_view, &focus_view, &done,
934 struct tog_view *prev = NULL;
936 if (view_is_parent_view(dead_view))
937 prev = TAILQ_PREV(dead_view,
938 tog_view_list_head, entry);
939 else if (view->parent != dead_view)
942 if (dead_view->parent)
943 dead_view->parent->child = NULL;
945 TAILQ_REMOVE(&views, dead_view, entry);
947 err = view_close(dead_view);
948 if (err || (dead_view == main_view && new_view == NULL))
951 if (view == dead_view) {
956 else if (!TAILQ_EMPTY(&views))
957 view = TAILQ_LAST(&views,
962 if (view->child && view->child_focussed)
963 focus_view = view->child;
970 struct tog_view *v, *t;
971 /* Only allow one parent view per type. */
972 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
973 if (v->type != new_view->type)
975 TAILQ_REMOVE(&views, v, entry);
981 TAILQ_INSERT_TAIL(&views, new_view, entry);
983 if (focus_view == NULL)
984 focus_view = new_view;
987 show_panel(focus_view->panel);
990 focus_view->focussed = 1;
993 show_panel(new_view->panel);
994 if (view->child && view_is_splitscreen(view->child))
995 show_panel(view->child->panel);
998 if (focus_view == NULL) {
1000 show_panel(view->panel);
1001 if (view->child && view_is_splitscreen(view->child))
1002 show_panel(view->child->panel);
1006 err = view->parent->show(view->parent);
1010 err = view->show(view);
1014 err = view->child->show(view->child);
1023 while (!TAILQ_EMPTY(&views)) {
1024 view = TAILQ_FIRST(&views);
1025 TAILQ_REMOVE(&views, view, entry);
1029 errcode = pthread_mutex_unlock(&tog_mutex);
1030 if (errcode && err == NULL)
1031 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1041 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1046 /* Create newly allocated wide-character string equivalent to a byte string. */
1047 static const struct got_error *
1048 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1051 const struct got_error *err = NULL;
1054 *wlen = mbstowcs(NULL, s, 0);
1055 if (*wlen == (size_t)-1) {
1057 if (errno != EILSEQ)
1058 return got_error_from_errno("mbstowcs");
1060 /* byte string invalid in current encoding; try to "fix" it */
1061 err = got_mbsavis(&vis, &vislen, s);
1064 *wlen = mbstowcs(NULL, vis, 0);
1065 if (*wlen == (size_t)-1) {
1066 err = got_error_from_errno("mbstowcs"); /* give up */
1071 *ws = calloc(*wlen + 1, sizeof(**ws));
1073 err = got_error_from_errno("calloc");
1077 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1078 err = got_error_from_errno("mbstowcs");
1089 /* Format a line for display, ensuring that it won't overflow a width limit. */
1090 static const struct got_error *
1091 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1094 const struct got_error *err = NULL;
1096 wchar_t *wline = NULL;
1103 err = mbs2ws(&wline, &wlen, line);
1109 int width = wcwidth(wline[i]);
1116 if (width == 1 || width == 2) {
1117 if (cols + width > wlimit)
1121 } else if (width == -1) {
1122 if (wline[i] == L'\t') {
1124 ((cols + col_tab_align) % TABSIZE);
1125 if (cols + width > wlimit)
1131 err = got_error_from_errno("wcwidth");
1146 static const struct got_error*
1147 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1148 struct got_object_id *id, struct got_repository *repo)
1150 static const struct got_error *err = NULL;
1151 struct got_reflist_entry *re;
1157 SIMPLEQ_FOREACH(re, refs, entry) {
1158 struct got_tag_object *tag = NULL;
1161 name = got_ref_get_name(re->ref);
1162 if (strcmp(name, GOT_REF_HEAD) == 0)
1164 if (strncmp(name, "refs/", 5) == 0)
1166 if (strncmp(name, "got/", 4) == 0)
1168 if (strncmp(name, "heads/", 6) == 0)
1170 if (strncmp(name, "remotes/", 8) == 0)
1172 if (strncmp(name, "tags/", 5) == 0) {
1173 err = got_object_open_as_tag(&tag, repo, re->id);
1175 if (err->code != GOT_ERR_OBJ_TYPE)
1177 /* Ref points at something other than a tag. */
1182 cmp = got_object_id_cmp(tag ?
1183 got_object_tag_get_object_id(tag) : re->id, id);
1185 got_object_tag_close(tag);
1189 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1190 s ? ", " : "", name) == -1) {
1191 err = got_error_from_errno("asprintf");
1202 static const struct got_error *
1203 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1206 char *smallerthan, *at;
1208 smallerthan = strchr(author, '<');
1209 if (smallerthan && smallerthan[1] != '\0')
1210 author = smallerthan + 1;
1211 at = strchr(author, '@');
1214 return format_line(wauthor, author_width, author, limit, col_tab_align);
1217 static const struct got_error *
1218 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1219 struct got_object_id *id, struct got_reflist_head *refs,
1220 const size_t date_display_cols, int author_display_cols,
1221 struct tog_colors *colors)
1223 const struct got_error *err = NULL;
1224 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1225 char *logmsg0 = NULL, *logmsg = NULL;
1226 char *author = NULL;
1227 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1228 int author_width, logmsg_width;
1229 char *newline, *line = NULL;
1231 const int avail = view->ncols;
1233 time_t committer_time;
1234 struct tog_color *tc;
1236 committer_time = got_object_commit_get_committer_time(commit);
1237 if (localtime_r(&committer_time, &tm) == NULL)
1238 return got_error_from_errno("localtime_r");
1239 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1241 return got_error(GOT_ERR_NO_SPACE);
1243 if (avail <= date_display_cols)
1244 limit = MIN(sizeof(datebuf) - 1, avail);
1246 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1247 tc = get_color(colors, TOG_COLOR_DATE);
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 waddnstr(view->window, datebuf, limit);
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1261 err = got_object_id_str(&id_str, id);
1264 tc = get_color(colors, TOG_COLOR_COMMIT);
1266 wattr_on(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1268 wprintw(view->window, "%.8s ", id_str);
1270 wattr_off(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1278 author = strdup(got_object_commit_get_author(commit));
1279 if (author == NULL) {
1280 err = got_error_from_errno("strdup");
1283 err = format_author(&wauthor, &author_width, author, avail - col, col);
1286 tc = get_color(colors, TOG_COLOR_AUTHOR);
1288 wattr_on(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 waddwstr(view->window, wauthor);
1292 wattr_off(view->window,
1293 COLOR_PAIR(tc->colorpair), NULL);
1294 col += author_width;
1295 while (col < avail && author_width < author_display_cols + 2) {
1296 waddch(view->window, ' ');
1303 err = got_object_commit_get_logmsg(&logmsg0, commit);
1307 while (*logmsg == '\n')
1309 newline = strchr(logmsg, '\n');
1312 limit = avail - col;
1313 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1316 waddwstr(view->window, wlogmsg);
1317 col += logmsg_width;
1318 while (col < avail) {
1319 waddch(view->window, ' ');
1331 static struct commit_queue_entry *
1332 alloc_commit_queue_entry(struct got_commit_object *commit,
1333 struct got_object_id *id)
1335 struct commit_queue_entry *entry;
1337 entry = calloc(1, sizeof(*entry));
1342 entry->commit = commit;
1347 pop_commit(struct commit_queue *commits)
1349 struct commit_queue_entry *entry;
1351 entry = TAILQ_FIRST(&commits->head);
1352 TAILQ_REMOVE(&commits->head, entry, entry);
1353 got_object_commit_close(entry->commit);
1354 commits->ncommits--;
1355 /* Don't free entry->id! It is owned by the commit graph. */
1360 free_commits(struct commit_queue *commits)
1362 while (!TAILQ_EMPTY(&commits->head))
1363 pop_commit(commits);
1366 static const struct got_error *
1367 match_commit(int *have_match, struct got_object_id *id,
1368 struct got_commit_object *commit, regex_t *regex)
1370 const struct got_error *err = NULL;
1371 regmatch_t regmatch;
1372 char *id_str = NULL, *logmsg = NULL;
1376 err = got_object_id_str(&id_str, id);
1380 err = got_object_commit_get_logmsg(&logmsg, commit);
1384 if (regexec(regex, got_object_commit_get_author(commit), 1,
1385 ®match, 0) == 0 ||
1386 regexec(regex, got_object_commit_get_committer(commit), 1,
1387 ®match, 0) == 0 ||
1388 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1389 regexec(regex, logmsg, 1, ®match, 0) == 0)
1397 static const struct got_error *
1398 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1399 int minqueue, struct got_repository *repo, const char *path,
1400 int *searching, int *search_next_done, regex_t *regex)
1402 const struct got_error *err = NULL;
1403 int nqueued = 0, have_match = 0;
1406 * We keep all commits open throughout the lifetime of the log
1407 * view in order to avoid having to re-fetch commits from disk
1408 * while updating the display.
1410 while (nqueued < minqueue ||
1411 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1412 struct got_object_id *id;
1413 struct got_commit_object *commit;
1414 struct commit_queue_entry *entry;
1417 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1418 if (err || id == NULL)
1421 err = got_object_open_as_commit(&commit, repo, id);
1424 entry = alloc_commit_queue_entry(commit, id);
1425 if (entry == NULL) {
1426 err = got_error_from_errno("alloc_commit_queue_entry");
1430 errcode = pthread_mutex_lock(&tog_mutex);
1432 err = got_error_set_errno(errcode,
1433 "pthread_mutex_lock");
1437 entry->idx = commits->ncommits;
1438 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1440 commits->ncommits++;
1442 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1443 err = match_commit(&have_match, id, commit, regex);
1446 errcode = pthread_mutex_unlock(&tog_mutex);
1447 if (errcode && err == NULL)
1448 err = got_error_set_errno(errcode,
1449 "pthread_mutex_unlock");
1451 if (err || have_match)
1458 static const struct got_error *
1459 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1460 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1461 struct commit_queue *commits, int selected_idx, int limit,
1462 struct got_reflist_head *refs, const char *path, int commits_needed,
1463 struct tog_colors *colors)
1465 const struct got_error *err = NULL;
1466 struct tog_log_view_state *s = &view->state.log;
1467 struct commit_queue_entry *entry;
1469 int ncommits, author_cols = 4;
1470 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1471 char *refs_str = NULL;
1473 struct tog_color *tc;
1474 static const size_t date_display_cols = 12;
1479 if (ncommits == selected_idx) {
1483 entry = TAILQ_NEXT(entry, entry);
1487 if (*selected && !(view->searching && view->search_next_done == 0)) {
1488 err = got_object_id_str(&id_str, (*selected)->id);
1492 err = build_refs_str(&refs_str, refs, (*selected)->id,
1499 if (commits_needed == 0)
1500 halfdelay(10); /* disable fast refresh */
1502 if (asprintf(&ncommits_str, " [%d/%d] %s",
1503 entry ? entry->idx + 1 : 0, commits->ncommits,
1504 commits_needed > 0 ?
1505 (view->searching && view->search_next_done == 0
1506 ? "searching..." : "loading... ") :
1507 (refs_str ? refs_str : "")) == -1) {
1508 err = got_error_from_errno("asprintf");
1512 if (path && strcmp(path, "/") != 0) {
1513 if (asprintf(&header, "commit %s %s%s",
1514 id_str ? id_str : "........................................",
1515 path, ncommits_str) == -1) {
1516 err = got_error_from_errno("asprintf");
1520 } else if (asprintf(&header, "commit %s%s",
1521 id_str ? id_str : "........................................",
1522 ncommits_str) == -1) {
1523 err = got_error_from_errno("asprintf");
1527 err = format_line(&wline, &width, header, view->ncols, 0);
1531 werase(view->window);
1533 if (view_needs_focus_indication(view))
1534 wstandout(view->window);
1535 tc = get_color(colors, TOG_COLOR_COMMIT);
1537 wattr_on(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 waddwstr(view->window, wline);
1541 wattr_off(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 while (width < view->ncols) {
1544 waddch(view->window, ' ');
1547 if (view_needs_focus_indication(view))
1548 wstandend(view->window);
1553 /* Grow author column size if necessary. */
1560 if (ncommits >= limit - 1)
1562 author = strdup(got_object_commit_get_author(entry->commit));
1563 if (author == NULL) {
1564 err = got_error_from_errno("strdup");
1567 err = format_author(&wauthor, &width, author, COLS,
1569 if (author_cols < width)
1570 author_cols = width;
1574 entry = TAILQ_NEXT(entry, entry);
1581 if (ncommits >= limit - 1)
1583 if (ncommits == selected_idx)
1584 wstandout(view->window);
1585 err = draw_commit(view, entry->commit, entry->id, refs,
1586 date_display_cols, author_cols, colors);
1587 if (ncommits == selected_idx)
1588 wstandend(view->window);
1593 entry = TAILQ_NEXT(entry, entry);
1606 scroll_up(struct tog_view *view,
1607 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1608 struct commit_queue *commits)
1610 struct commit_queue_entry *entry;
1613 entry = TAILQ_FIRST(&commits->head);
1614 if (*first_displayed_entry == entry)
1617 entry = *first_displayed_entry;
1618 while (entry && nscrolled < maxscroll) {
1619 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1621 *first_displayed_entry = entry;
1627 static const struct got_error *
1628 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1629 pthread_cond_t *need_commits)
1634 halfdelay(1); /* fast refresh while loading commits */
1636 while (*commits_needed > 0) {
1640 /* Wake the log thread. */
1641 errcode = pthread_cond_signal(need_commits);
1643 return got_error_set_errno(errcode,
1644 "pthread_cond_signal");
1645 errcode = pthread_mutex_unlock(&tog_mutex);
1647 return got_error_set_errno(errcode,
1648 "pthread_mutex_unlock");
1650 errcode = pthread_mutex_lock(&tog_mutex);
1652 return got_error_set_errno(errcode,
1653 "pthread_mutex_lock");
1655 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1657 * Thread is not done yet; lose a key press
1658 * and let the user retry... this way the GUI
1659 * remains interactive while logging deep paths
1660 * with few commits in history.
1669 static const struct got_error *
1670 scroll_down(struct tog_view *view,
1671 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1672 struct commit_queue_entry **last_displayed_entry,
1673 struct commit_queue *commits, int *log_complete, int *commits_needed,
1674 pthread_cond_t *need_commits)
1676 const struct got_error *err = NULL;
1677 struct commit_queue_entry *pentry;
1680 if (*last_displayed_entry == NULL)
1683 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1684 if (pentry == NULL && !*log_complete) {
1686 * Ask the log thread for required amount of commits
1687 * plus some amount of pre-fetching.
1689 (*commits_needed) += maxscroll + 20;
1690 err = trigger_log_thread(0, commits_needed, log_complete,
1697 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1701 *last_displayed_entry = pentry;
1703 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1706 *first_displayed_entry = pentry;
1707 } while (++nscrolled < maxscroll);
1712 static const struct got_error *
1713 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1714 struct got_commit_object *commit, struct got_object_id *commit_id,
1715 struct tog_view *log_view, struct got_reflist_head *refs,
1716 struct got_repository *repo)
1718 const struct got_error *err;
1719 struct got_object_qid *parent_id;
1720 struct tog_view *diff_view;
1722 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1723 if (diff_view == NULL)
1724 return got_error_from_errno("view_open");
1726 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1727 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1728 commit_id, log_view, refs, repo);
1730 *new_view = diff_view;
1734 static const struct got_error *
1735 tree_view_visit_subtree(struct got_tree_object *subtree,
1736 struct tog_tree_view_state *s)
1738 struct tog_parent_tree *parent;
1740 parent = calloc(1, sizeof(*parent));
1742 return got_error_from_errno("calloc");
1744 parent->tree = s->tree;
1745 parent->first_displayed_entry = s->first_displayed_entry;
1746 parent->selected_entry = s->selected_entry;
1747 parent->selected = s->selected;
1748 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1751 s->first_displayed_entry = NULL;
1756 static const struct got_error *
1757 browse_commit_tree(struct tog_view **new_view, int begin_x,
1758 struct commit_queue_entry *entry, const char *path,
1759 struct got_reflist_head *refs, struct got_repository *repo)
1761 const struct got_error *err = NULL;
1762 struct got_tree_object *tree;
1763 struct tog_tree_view_state *s;
1764 struct tog_view *tree_view;
1765 char *slash, *subpath = NULL;
1768 err = got_object_open_as_tree(&tree, repo,
1769 got_object_commit_get_tree_id(entry->commit));
1773 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1774 if (tree_view == NULL)
1775 return got_error_from_errno("view_open");
1777 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1779 got_object_tree_close(tree);
1782 s = &tree_view->state.tree;
1784 *new_view = tree_view;
1786 if (got_path_is_root_dir(path))
1789 /* Walk the path and open corresponding tree objects. */
1792 struct got_tree_entry *te;
1793 struct got_object_id *tree_id;
1799 /* Ensure the correct subtree entry is selected. */
1800 slash = strchr(p, '/');
1802 te_name = strdup(p);
1804 te_name = strndup(p, slash - p);
1805 if (te_name == NULL) {
1806 err = got_error_from_errno("strndup");
1809 te = got_object_tree_find_entry(s->tree, te_name);
1811 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1816 s->selected_entry = te;
1817 s->selected = got_tree_entry_get_index(te);
1818 if (s->tree != s->root)
1819 s->selected++; /* skip '..' */
1821 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1822 /* Jump to this file's entry. */
1823 s->first_displayed_entry = s->selected_entry;
1828 slash = strchr(p, '/');
1830 subpath = strndup(path, slash - path);
1832 subpath = strdup(path);
1833 if (subpath == NULL) {
1834 err = got_error_from_errno("strdup");
1838 err = got_object_id_by_path(&tree_id, repo, entry->id,
1843 err = got_object_open_as_tree(&tree, repo, tree_id);
1848 err = tree_view_visit_subtree(tree, s);
1850 got_object_tree_close(tree);
1864 static const struct got_error *
1865 block_signals_used_by_main_thread(void)
1870 if (sigemptyset(&sigset) == -1)
1871 return got_error_from_errno("sigemptyset");
1873 /* tog handles SIGWINCH and SIGCONT */
1874 if (sigaddset(&sigset, SIGWINCH) == -1)
1875 return got_error_from_errno("sigaddset");
1876 if (sigaddset(&sigset, SIGCONT) == -1)
1877 return got_error_from_errno("sigaddset");
1879 /* ncurses handles SIGTSTP */
1880 if (sigaddset(&sigset, SIGTSTP) == -1)
1881 return got_error_from_errno("sigaddset");
1883 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1885 return got_error_set_errno(errcode, "pthread_sigmask");
1891 log_thread(void *arg)
1893 const struct got_error *err = NULL;
1895 struct tog_log_thread_args *a = arg;
1898 err = block_signals_used_by_main_thread();
1902 while (!done && !err && !tog_sigpipe_received) {
1903 err = queue_commits(a->graph, a->commits, 1, a->repo,
1904 a->in_repo_path, a->searching, a->search_next_done,
1907 if (err->code != GOT_ERR_ITER_COMPLETED)
1911 } else if (a->commits_needed > 0)
1912 a->commits_needed--;
1914 errcode = pthread_mutex_lock(&tog_mutex);
1916 err = got_error_set_errno(errcode,
1917 "pthread_mutex_lock");
1919 } else if (*a->quit)
1921 else if (*a->first_displayed_entry == NULL) {
1922 *a->first_displayed_entry =
1923 TAILQ_FIRST(&a->commits->head);
1924 *a->selected_entry = *a->first_displayed_entry;
1928 a->commits_needed = 0;
1929 else if (a->commits_needed == 0) {
1930 errcode = pthread_cond_wait(&a->need_commits,
1933 err = got_error_set_errno(errcode,
1934 "pthread_cond_wait");
1937 errcode = pthread_mutex_unlock(&tog_mutex);
1938 if (errcode && err == NULL)
1939 err = got_error_set_errno(errcode,
1940 "pthread_mutex_unlock");
1942 a->log_complete = 1;
1946 static const struct got_error *
1947 stop_log_thread(struct tog_log_view_state *s)
1949 const struct got_error *err = NULL;
1954 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1956 return got_error_set_errno(errcode,
1957 "pthread_cond_signal");
1958 errcode = pthread_mutex_unlock(&tog_mutex);
1960 return got_error_set_errno(errcode,
1961 "pthread_mutex_unlock");
1962 errcode = pthread_join(s->thread, (void **)&err);
1964 return got_error_set_errno(errcode, "pthread_join");
1965 errcode = pthread_mutex_lock(&tog_mutex);
1967 return got_error_set_errno(errcode,
1968 "pthread_mutex_lock");
1972 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1973 if (errcode && err == NULL)
1974 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1976 if (s->thread_args.repo) {
1977 got_repo_close(s->thread_args.repo);
1978 s->thread_args.repo = NULL;
1981 if (s->thread_args.graph) {
1982 got_commit_graph_close(s->thread_args.graph);
1983 s->thread_args.graph = NULL;
1989 static const struct got_error *
1990 close_log_view(struct tog_view *view)
1992 const struct got_error *err = NULL;
1993 struct tog_log_view_state *s = &view->state.log;
1995 err = stop_log_thread(s);
1996 free_commits(&s->commits);
1997 free(s->in_repo_path);
1998 s->in_repo_path = NULL;
2004 static const struct got_error *
2005 search_start_log_view(struct tog_view *view)
2007 struct tog_log_view_state *s = &view->state.log;
2009 s->matched_entry = NULL;
2010 s->search_entry = NULL;
2014 static const struct got_error *
2015 search_next_log_view(struct tog_view *view)
2017 const struct got_error *err = NULL;
2018 struct tog_log_view_state *s = &view->state.log;
2019 struct commit_queue_entry *entry;
2021 if (!view->searching) {
2022 view->search_next_done = 1;
2026 if (s->search_entry) {
2028 errcode = pthread_mutex_unlock(&tog_mutex);
2030 return got_error_set_errno(errcode,
2031 "pthread_mutex_unlock");
2032 ch = wgetch(view->window);
2033 errcode = pthread_mutex_lock(&tog_mutex);
2035 return got_error_set_errno(errcode,
2036 "pthread_mutex_lock");
2037 if (ch == KEY_BACKSPACE) {
2038 view->search_next_done = 1;
2041 if (view->searching == TOG_SEARCH_FORWARD)
2042 entry = TAILQ_NEXT(s->search_entry, entry);
2044 entry = TAILQ_PREV(s->search_entry,
2045 commit_queue_head, entry);
2046 } else if (s->matched_entry) {
2047 if (view->searching == TOG_SEARCH_FORWARD)
2048 entry = TAILQ_NEXT(s->selected_entry, entry);
2050 entry = TAILQ_PREV(s->selected_entry,
2051 commit_queue_head, entry);
2053 if (view->searching == TOG_SEARCH_FORWARD)
2054 entry = TAILQ_FIRST(&s->commits.head);
2056 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2062 if (entry == NULL) {
2063 if (s->thread_args.log_complete ||
2064 view->searching == TOG_SEARCH_BACKWARD) {
2065 view->search_next_done = 1;
2069 * Poke the log thread for more commits and return,
2070 * allowing the main loop to make progress. Search
2071 * will resume at s->search_entry once we come back.
2073 s->thread_args.commits_needed++;
2074 return trigger_log_thread(1,
2075 &s->thread_args.commits_needed,
2076 &s->thread_args.log_complete,
2077 &s->thread_args.need_commits);
2080 err = match_commit(&have_match, entry->id, entry->commit,
2085 view->search_next_done = 1;
2086 s->matched_entry = entry;
2090 s->search_entry = entry;
2091 if (view->searching == TOG_SEARCH_FORWARD)
2092 entry = TAILQ_NEXT(entry, entry);
2094 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2097 if (s->matched_entry) {
2098 int cur = s->selected_entry->idx;
2099 while (cur < s->matched_entry->idx) {
2100 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2105 while (cur > s->matched_entry->idx) {
2106 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2113 s->search_entry = NULL;
2118 static const struct got_error *
2119 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2120 struct got_reflist_head *refs, struct got_repository *repo,
2121 const char *head_ref_name, const char *path, int check_disk,
2124 const struct got_error *err = NULL;
2125 struct tog_log_view_state *s = &view->state.log;
2126 struct got_repository *thread_repo = NULL;
2127 struct got_commit_graph *thread_graph = NULL;
2130 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2134 /* The commit queue only contains commits being displayed. */
2135 TAILQ_INIT(&s->commits.head);
2136 s->commits.ncommits = 0;
2140 s->head_ref_name = head_ref_name;
2141 s->start_id = got_object_id_dup(start_id);
2142 if (s->start_id == NULL) {
2143 err = got_error_from_errno("got_object_id_dup");
2146 s->log_branches = log_branches;
2148 SIMPLEQ_INIT(&s->colors);
2149 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2150 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2151 get_color_value("TOG_COLOR_COMMIT"));
2154 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2155 get_color_value("TOG_COLOR_AUTHOR"));
2157 free_colors(&s->colors);
2160 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2161 get_color_value("TOG_COLOR_DATE"));
2163 free_colors(&s->colors);
2168 view->show = show_log_view;
2169 view->input = input_log_view;
2170 view->close = close_log_view;
2171 view->search_start = search_start_log_view;
2172 view->search_next = search_next_log_view;
2174 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2177 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2181 err = got_commit_graph_iter_start(thread_graph,
2182 s->start_id, s->repo, NULL, NULL);
2186 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2188 err = got_error_set_errno(errcode, "pthread_cond_init");
2192 s->thread_args.commits_needed = view->nlines;
2193 s->thread_args.graph = thread_graph;
2194 s->thread_args.commits = &s->commits;
2195 s->thread_args.in_repo_path = s->in_repo_path;
2196 s->thread_args.start_id = s->start_id;
2197 s->thread_args.repo = thread_repo;
2198 s->thread_args.log_complete = 0;
2199 s->thread_args.quit = &s->quit;
2200 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2201 s->thread_args.selected_entry = &s->selected_entry;
2202 s->thread_args.searching = &view->searching;
2203 s->thread_args.search_next_done = &view->search_next_done;
2204 s->thread_args.regex = &view->regex;
2207 close_log_view(view);
2211 static const struct got_error *
2212 show_log_view(struct tog_view *view)
2214 struct tog_log_view_state *s = &view->state.log;
2216 if (s->thread == NULL) {
2217 int errcode = pthread_create(&s->thread, NULL, log_thread,
2220 return got_error_set_errno(errcode, "pthread_create");
2223 return draw_commits(view, &s->last_displayed_entry,
2224 &s->selected_entry, s->first_displayed_entry,
2225 &s->commits, s->selected, view->nlines, s->refs,
2226 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2229 static const struct got_error *
2230 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2231 struct tog_view **focus_view, struct tog_view *view, int ch)
2233 const struct got_error *err = NULL;
2234 struct tog_log_view_state *s = &view->state.log;
2235 char *parent_path, *in_repo_path = NULL;
2236 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2238 struct got_object_id *start_id;
2248 if (s->first_displayed_entry == NULL)
2250 if (s->selected > 0)
2253 scroll_up(view, &s->first_displayed_entry, 1,
2258 if (s->first_displayed_entry == NULL)
2260 if (TAILQ_FIRST(&s->commits.head) ==
2261 s->first_displayed_entry) {
2265 scroll_up(view, &s->first_displayed_entry,
2266 view->nlines, &s->commits);
2272 if (s->first_displayed_entry == NULL)
2274 if (s->selected < MIN(view->nlines - 2,
2275 s->commits.ncommits - 1)) {
2279 err = scroll_down(view, &s->first_displayed_entry, 1,
2280 &s->last_displayed_entry, &s->commits,
2281 &s->thread_args.log_complete,
2282 &s->thread_args.commits_needed,
2283 &s->thread_args.need_commits);
2287 struct commit_queue_entry *first;
2288 first = s->first_displayed_entry;
2291 err = scroll_down(view, &s->first_displayed_entry,
2292 view->nlines, &s->last_displayed_entry,
2293 &s->commits, &s->thread_args.log_complete,
2294 &s->thread_args.commits_needed,
2295 &s->thread_args.need_commits);
2298 if (first == s->first_displayed_entry &&
2299 s->selected < MIN(view->nlines - 2,
2300 s->commits.ncommits - 1)) {
2301 /* can't scroll further down */
2302 s->selected = MIN(view->nlines - 2,
2303 s->commits.ncommits - 1);
2309 if (s->selected > view->nlines - 2)
2310 s->selected = view->nlines - 2;
2311 if (s->selected > s->commits.ncommits - 1)
2312 s->selected = s->commits.ncommits - 1;
2317 if (s->selected_entry == NULL)
2319 if (view_is_parent_view(view))
2320 begin_x = view_split_begin_x(view->begin_x);
2321 err = open_diff_view_for_commit(&diff_view, begin_x,
2322 s->selected_entry->commit, s->selected_entry->id,
2323 view, s->refs, s->repo);
2326 if (view_is_parent_view(view)) {
2327 err = view_close_child(view);
2330 err = view_set_child(view, diff_view);
2332 view_close(diff_view);
2335 *focus_view = diff_view;
2336 view->child_focussed = 1;
2338 *new_view = diff_view;
2341 if (s->selected_entry == NULL)
2343 if (view_is_parent_view(view))
2344 begin_x = view_split_begin_x(view->begin_x);
2345 err = browse_commit_tree(&tree_view, begin_x,
2346 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2349 if (view_is_parent_view(view)) {
2350 err = view_close_child(view);
2353 err = view_set_child(view, tree_view);
2355 view_close(tree_view);
2358 *focus_view = tree_view;
2359 view->child_focussed = 1;
2361 *new_view = tree_view;
2364 if (strcmp(s->in_repo_path, "/") == 0)
2366 parent_path = dirname(s->in_repo_path);
2367 if (parent_path && strcmp(parent_path, ".") != 0) {
2368 err = stop_log_thread(s);
2371 lv = view_open(view->nlines, view->ncols,
2372 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2374 return got_error_from_errno(
2376 err = open_log_view(lv, s->start_id, s->refs,
2377 s->repo, s->head_ref_name, parent_path, 0,
2381 if (view_is_parent_view(view))
2384 view_set_child(view->parent, lv);
2391 err = stop_log_thread(s);
2394 lv = view_open(view->nlines, view->ncols,
2395 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2397 return got_error_from_errno("view_open");
2398 err = got_repo_match_object_id(&start_id, NULL,
2399 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2400 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2405 in_repo_path = strdup(s->in_repo_path);
2406 if (in_repo_path == NULL) {
2409 return got_error_from_errno("strdup");
2411 got_ref_list_free(s->refs);
2412 err = got_ref_list(s->refs, s->repo, NULL,
2413 got_ref_cmp_by_name, NULL);
2419 err = open_log_view(lv, start_id, s->refs, s->repo,
2420 s->head_ref_name, in_repo_path, 0, s->log_branches);
2430 s->log_branches = !s->log_branches;
2431 err = stop_log_thread(s);
2434 lv = view_open(view->nlines, view->ncols,
2435 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2437 return got_error_from_errno("view_open");
2438 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2439 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2454 static const struct got_error *
2455 apply_unveil(const char *repo_path, const char *worktree_path)
2457 const struct got_error *error;
2460 if (unveil("gmon.out", "rwc") != 0)
2461 return got_error_from_errno2("unveil", "gmon.out");
2463 if (repo_path && unveil(repo_path, "r") != 0)
2464 return got_error_from_errno2("unveil", repo_path);
2466 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2467 return got_error_from_errno2("unveil", worktree_path);
2469 if (unveil("/tmp", "rwc") != 0)
2470 return got_error_from_errno2("unveil", "/tmp");
2472 error = got_privsep_unveil_exec_helpers();
2476 if (unveil(NULL, NULL) != 0)
2477 return got_error_from_errno("unveil");
2487 halfdelay(1); /* Do fast refresh while initial view is loading. */
2490 intrflush(stdscr, FALSE);
2491 keypad(stdscr, TRUE);
2493 if (getenv("TOG_COLORS") != NULL) {
2495 use_default_colors();
2497 signal(SIGWINCH, tog_sigwinch);
2498 signal(SIGPIPE, tog_sigpipe);
2499 signal(SIGCONT, tog_sigcont);
2502 static const struct got_error *
2503 cmd_log(int argc, char *argv[])
2505 const struct got_error *error;
2506 struct got_repository *repo = NULL;
2507 struct got_worktree *worktree = NULL;
2508 struct got_reflist_head refs;
2509 struct got_object_id *start_id = NULL;
2510 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2511 char *start_commit = NULL, *head_ref_name = NULL;
2512 int ch, log_branches = 0, check_disk = 1;
2513 struct tog_view *view;
2515 SIMPLEQ_INIT(&refs);
2518 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2523 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2529 start_commit = optarg;
2532 repo_path = realpath(optarg, NULL);
2533 if (repo_path == NULL)
2534 return got_error_from_errno2("realpath",
2546 cwd = getcwd(NULL, 0);
2548 error = got_error_from_errno("getcwd");
2551 error = got_worktree_open(&worktree, cwd);
2552 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2559 error = got_error_from_errno("strdup");
2562 } else if (argc == 1) {
2564 error = got_worktree_resolve_path(&path, worktree,
2569 path = strdup(argv[0]);
2571 error = got_error_from_errno("strdup");
2578 if (repo_path == NULL) {
2581 got_worktree_get_repo_path(worktree));
2583 repo_path = strdup(cwd);
2585 if (repo_path == NULL) {
2586 error = got_error_from_errno("strdup");
2592 error = got_repo_open(&repo, repo_path, NULL);
2596 error = apply_unveil(got_repo_get_path(repo),
2597 worktree ? got_worktree_get_root_path(worktree) : NULL);
2601 if (start_commit == NULL)
2602 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2603 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2604 GOT_OBJ_TYPE_COMMIT, 1, repo);
2606 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2607 GOT_OBJ_TYPE_COMMIT, 1, repo);
2611 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2615 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2617 error = got_error_from_errno("view_open");
2621 const char *prefix = got_worktree_get_path_prefix(worktree);
2623 if (asprintf(&p, "%s%s%s", prefix,
2624 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2625 error = got_error_from_errno("asprintf");
2632 head_ref_name = strdup(
2633 got_worktree_get_head_ref_name(worktree));
2634 if (head_ref_name == NULL) {
2635 error = got_error_from_errno("strdup");
2639 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2640 path, check_disk, log_branches);
2644 /* Release work tree lock. */
2645 got_worktree_close(worktree);
2648 error = view_loop(view);
2654 free(head_ref_name);
2656 got_repo_close(repo);
2658 got_worktree_close(worktree);
2659 got_ref_list_free(&refs);
2667 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2673 parse_next_line(FILE *f, size_t *len)
2678 const char delim[3] = { '\0', '\0', '\0'};
2680 line = fparseln(f, &linelen, &lineno, delim, 0);
2687 match_line(const char *line, regex_t *regex)
2689 regmatch_t regmatch;
2691 return regexec(regex, line, 1, ®match, 0) == 0;
2695 match_color(struct tog_colors *colors, const char *line)
2697 struct tog_color *tc = NULL;
2699 SIMPLEQ_FOREACH(tc, colors, entry) {
2700 if (match_line(line, &tc->regex))
2707 static const struct got_error *
2708 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2709 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2710 char *header, struct tog_colors *colors)
2712 const struct got_error *err;
2713 int lineno = 0, nprinted = 0;
2715 struct tog_color *tc;
2721 werase(view->window);
2724 err = format_line(&wline, &width, header, view->ncols, 0);
2729 if (view_needs_focus_indication(view))
2730 wstandout(view->window);
2731 waddwstr(view->window, wline);
2732 if (view_needs_focus_indication(view))
2733 wstandend(view->window);
2734 if (width <= view->ncols - 1)
2735 waddch(view->window, '\n');
2743 while (nprinted < max_lines) {
2744 line = parse_next_line(f, &len);
2749 if (++lineno < *first_displayed_line) {
2754 err = format_line(&wline, &width, line, view->ncols, 0);
2760 tc = match_color(colors, line);
2762 wattr_on(view->window,
2763 COLOR_PAIR(tc->colorpair), NULL);
2764 waddwstr(view->window, wline);
2766 wattr_off(view->window,
2767 COLOR_PAIR(tc->colorpair), NULL);
2768 if (width <= view->ncols - 1)
2769 waddch(view->window, '\n');
2770 if (++nprinted == 1)
2771 *first_displayed_line = lineno;
2776 *last_displayed_line = lineno;
2781 while (nprinted < view->nlines) {
2782 waddch(view->window, '\n');
2786 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2791 wstandout(view->window);
2792 waddwstr(view->window, wline);
2793 wstandend(view->window);
2800 get_datestr(time_t *time, char *datebuf)
2802 struct tm mytm, *tm;
2805 tm = gmtime_r(time, &mytm);
2808 s = asctime_r(tm, datebuf);
2811 p = strchr(s, '\n');
2817 static const struct got_error *
2818 write_commit_info(struct got_object_id *commit_id,
2819 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2821 const struct got_error *err = NULL;
2822 char datebuf[26], *datestr;
2823 struct got_commit_object *commit;
2824 char *id_str = NULL, *logmsg = NULL;
2825 time_t committer_time;
2826 const char *author, *committer;
2827 char *refs_str = NULL;
2830 err = build_refs_str(&refs_str, refs, commit_id, repo);
2835 err = got_object_open_as_commit(&commit, repo, commit_id);
2839 err = got_object_id_str(&id_str, commit_id);
2841 err = got_error_from_errno("got_object_id_str");
2845 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2846 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2847 err = got_error_from_errno("fprintf");
2850 if (fprintf(outfile, "from: %s\n",
2851 got_object_commit_get_author(commit)) < 0) {
2852 err = got_error_from_errno("fprintf");
2855 committer_time = got_object_commit_get_committer_time(commit);
2856 datestr = get_datestr(&committer_time, datebuf);
2857 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2858 err = got_error_from_errno("fprintf");
2861 author = got_object_commit_get_author(commit);
2862 committer = got_object_commit_get_committer(commit);
2863 if (strcmp(author, committer) != 0 &&
2864 fprintf(outfile, "via: %s\n", committer) < 0) {
2865 err = got_error_from_errno("fprintf");
2868 err = got_object_commit_get_logmsg(&logmsg, commit);
2871 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2872 err = got_error_from_errno("fprintf");
2879 got_object_commit_close(commit);
2883 const struct got_error *
2884 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2890 size_t noffsets = 0;
2894 *line_offsets = NULL;
2900 if (fseek(infile, 0, SEEK_END) == -1)
2901 return got_error_from_errno("fseek");
2902 len = ftell(infile) + 1;
2904 return got_error_from_errno("ftell");
2905 if (fseek(infile, 0, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2910 if ((buf = calloc(len, sizeof(char *))) == NULL)
2911 return got_error_from_errno("calloc");
2913 fread(buf, 1, len, infile);
2915 return got_error_from_errno("fread");
2918 if (line_offsets && nlines) {
2919 if (*line_offsets == NULL) {
2920 /* Have some data but perhaps no '\n'. */
2923 *line_offsets = calloc(1, sizeof(**line_offsets));
2924 if (*line_offsets == NULL)
2925 return got_error_from_errno("calloc");
2926 /* Skip forward over end of first line. */
2933 /* Scan '\n' offsets in remaining chunk of data. */
2935 if (buf[i] != '\n') {
2940 if (noffsets < *nlines) {
2941 off_t *o = recallocarray(*line_offsets,
2943 sizeof(**line_offsets));
2945 free(*line_offsets);
2946 *line_offsets = NULL;
2947 return got_error_from_errno(
2954 (*line_offsets)[*nlines - 1] = off;
2959 if (fflush(infile) != 0)
2960 return got_error_from_errno("fflush");
2969 static const struct got_error *
2970 create_diff(struct tog_diff_view_state *s)
2972 const struct got_error *err = NULL;
2978 err = got_error_from_errno("got_opentemp");
2981 if (s->f && fclose(s->f) != 0) {
2982 err = got_error_from_errno("fclose");
2988 err = got_object_get_type(&obj_type, s->repo, s->id1);
2990 err = got_object_get_type(&obj_type, s->repo, s->id2);
2995 case GOT_OBJ_TYPE_BLOB:
2996 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2997 s->diff_context, 0, s->repo, s->f);
2999 case GOT_OBJ_TYPE_TREE:
3000 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3001 s->diff_context, 0, s->repo, s->f);
3003 case GOT_OBJ_TYPE_COMMIT: {
3004 const struct got_object_id_queue *parent_ids;
3005 struct got_object_qid *pid;
3006 struct got_commit_object *commit2;
3008 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3011 /* Show commit info if we're diffing to a parent/root commit. */
3012 if (s->id1 == NULL) {
3013 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3017 parent_ids = got_object_commit_get_parent_ids(commit2);
3018 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3019 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3020 err = write_commit_info(s->id2, s->refs,
3028 got_object_commit_close(commit2);
3030 err = got_diff_objects_as_commits(s->id1, s->id2,
3031 s->diff_context, 0, s->repo, s->f);
3035 err = got_error(GOT_ERR_OBJ_TYPE);
3040 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3043 if (s->f && fflush(s->f) != 0 && err == NULL)
3044 err = got_error_from_errno("fflush");
3049 diff_view_indicate_progress(struct tog_view *view)
3051 mvwaddstr(view->window, 0, 0, "diffing...");
3056 static const struct got_error *
3057 search_start_diff_view(struct tog_view *view)
3059 struct tog_diff_view_state *s = &view->state.diff;
3061 s->matched_line = 0;
3065 static const struct got_error *
3066 search_next_diff_view(struct tog_view *view)
3068 struct tog_diff_view_state *s = &view->state.diff;
3071 if (!view->searching) {
3072 view->search_next_done = 1;
3076 if (s->matched_line) {
3077 if (view->searching == TOG_SEARCH_FORWARD)
3078 lineno = s->matched_line + 1;
3080 lineno = s->matched_line - 1;
3082 if (view->searching == TOG_SEARCH_FORWARD)
3093 if (lineno <= 0 || lineno > s->nlines) {
3094 if (s->matched_line == 0) {
3095 view->search_next_done = 1;
3100 if (view->searching == TOG_SEARCH_FORWARD)
3106 offset = s->line_offsets[lineno - 1];
3107 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3109 return got_error_from_errno("fseeko");
3112 line = parse_next_line(s->f, &len);
3113 if (line && match_line(line, &view->regex)) {
3114 view->search_next_done = 1;
3115 s->matched_line = lineno;
3120 if (view->searching == TOG_SEARCH_FORWARD)
3126 if (s->matched_line) {
3127 s->first_displayed_line = s->matched_line;
3128 s->selected_line = 1;
3134 static const struct got_error *
3135 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3136 struct got_object_id *id2, struct tog_view *log_view,
3137 struct got_reflist_head *refs, struct got_repository *repo)
3139 const struct got_error *err;
3140 struct tog_diff_view_state *s = &view->state.diff;
3142 if (id1 != NULL && id2 != NULL) {
3144 err = got_object_get_type(&type1, repo, id1);
3147 err = got_object_get_type(&type2, repo, id2);
3152 return got_error(GOT_ERR_OBJ_TYPE);
3154 s->first_displayed_line = 1;
3155 s->last_displayed_line = view->nlines;
3156 s->selected_line = 1;
3163 s->id1 = got_object_id_dup(id1);
3165 return got_error_from_errno("got_object_id_dup");
3169 s->id2 = got_object_id_dup(id2);
3170 if (s->id2 == NULL) {
3173 return got_error_from_errno("got_object_id_dup");
3176 s->first_displayed_line = 1;
3177 s->last_displayed_line = view->nlines;
3178 s->diff_context = 3;
3179 s->log_view = log_view;
3183 SIMPLEQ_INIT(&s->colors);
3184 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3185 err = add_color(&s->colors,
3186 "^-", TOG_COLOR_DIFF_MINUS,
3187 get_color_value("TOG_COLOR_DIFF_MINUS"));
3190 err = add_color(&s->colors, "^\\+",
3191 TOG_COLOR_DIFF_PLUS,
3192 get_color_value("TOG_COLOR_DIFF_PLUS"));
3194 free_colors(&s->colors);
3197 err = add_color(&s->colors,
3198 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3199 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3201 free_colors(&s->colors);
3205 err = add_color(&s->colors,
3206 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3207 get_color_value("TOG_COLOR_DIFF_META"));
3209 free_colors(&s->colors);
3213 err = add_color(&s->colors,
3214 "^(from|via): ", TOG_COLOR_AUTHOR,
3215 get_color_value("TOG_COLOR_AUTHOR"));
3217 free_colors(&s->colors);
3221 err = add_color(&s->colors,
3222 "^date: ", TOG_COLOR_DATE,
3223 get_color_value("TOG_COLOR_DATE"));
3225 free_colors(&s->colors);
3230 if (log_view && view_is_splitscreen(view))
3231 show_log_view(log_view); /* draw vborder */
3232 diff_view_indicate_progress(view);
3234 err = create_diff(s);
3243 view->show = show_diff_view;
3244 view->input = input_diff_view;
3245 view->close = close_diff_view;
3246 view->search_start = search_start_diff_view;
3247 view->search_next = search_next_diff_view;
3252 static const struct got_error *
3253 close_diff_view(struct tog_view *view)
3255 const struct got_error *err = NULL;
3256 struct tog_diff_view_state *s = &view->state.diff;
3262 if (s->f && fclose(s->f) == EOF)
3263 err = got_error_from_errno("fclose");
3264 free_colors(&s->colors);
3265 free(s->line_offsets);
3269 static const struct got_error *
3270 show_diff_view(struct tog_view *view)
3272 const struct got_error *err;
3273 struct tog_diff_view_state *s = &view->state.diff;
3274 char *id_str1 = NULL, *id_str2, *header;
3277 err = got_object_id_str(&id_str1, s->id1);
3281 err = got_object_id_str(&id_str2, s->id2);
3285 if (asprintf(&header, "diff %s %s",
3286 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3287 err = got_error_from_errno("asprintf");
3295 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3296 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3297 header, &s->colors);
3300 static const struct got_error *
3301 set_selected_commit(struct tog_diff_view_state *s,
3302 struct commit_queue_entry *entry)
3304 const struct got_error *err;
3305 const struct got_object_id_queue *parent_ids;
3306 struct got_commit_object *selected_commit;
3307 struct got_object_qid *pid;
3310 s->id2 = got_object_id_dup(entry->id);
3312 return got_error_from_errno("got_object_id_dup");
3314 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3317 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3319 pid = SIMPLEQ_FIRST(parent_ids);
3320 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3321 got_object_commit_close(selected_commit);
3325 static const struct got_error *
3326 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3327 struct tog_view **focus_view, struct tog_view *view, int ch)
3329 const struct got_error *err = NULL;
3330 struct tog_diff_view_state *s = &view->state.diff;
3331 struct tog_log_view_state *ls;
3332 struct commit_queue_entry *entry;
3338 if (s->first_displayed_line > 1)
3339 s->first_displayed_line--;
3343 if (s->first_displayed_line == 1)
3346 while (i++ < view->nlines - 1 &&
3347 s->first_displayed_line > 1)
3348 s->first_displayed_line--;
3353 s->first_displayed_line++;
3361 while (!s->eof && i++ < view->nlines - 1) {
3363 line = parse_next_line(s->f, NULL);
3364 s->first_displayed_line++;
3370 if (s->diff_context > 0) {
3372 diff_view_indicate_progress(view);
3373 err = create_diff(s);
3377 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3379 diff_view_indicate_progress(view);
3380 err = create_diff(s);
3385 if (s->log_view == NULL)
3387 ls = &s->log_view->state.log;
3388 entry = TAILQ_PREV(ls->selected_entry,
3389 commit_queue_head, entry);
3393 err = input_log_view(NULL, NULL, NULL, s->log_view,
3398 err = set_selected_commit(s, entry);
3402 s->first_displayed_line = 1;
3403 s->last_displayed_line = view->nlines;
3405 diff_view_indicate_progress(view);
3406 err = create_diff(s);
3410 if (s->log_view == NULL)
3412 ls = &s->log_view->state.log;
3414 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3415 ls->thread_args.commits_needed++;
3417 /* Display "loading..." in log view. */
3418 show_log_view(s->log_view);
3422 err = trigger_log_thread(1 /* load_all */,
3423 &ls->thread_args.commits_needed,
3424 &ls->thread_args.log_complete,
3425 &ls->thread_args.need_commits);
3429 err = input_log_view(NULL, NULL, NULL, s->log_view,
3434 entry = TAILQ_NEXT(ls->selected_entry, entry);
3438 err = set_selected_commit(s, entry);
3442 s->first_displayed_line = 1;
3443 s->last_displayed_line = view->nlines;
3445 diff_view_indicate_progress(view);
3446 err = create_diff(s);
3455 static const struct got_error *
3456 cmd_diff(int argc, char *argv[])
3458 const struct got_error *error = NULL;
3459 struct got_repository *repo = NULL;
3460 struct got_reflist_head refs;
3461 struct got_object_id *id1 = NULL, *id2 = NULL;
3462 char *repo_path = NULL;
3463 char *id_str1 = NULL, *id_str2 = NULL;
3465 struct tog_view *view;
3467 SIMPLEQ_INIT(&refs);
3470 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3475 while ((ch = getopt(argc, argv, "")) != -1) {
3487 usage_diff(); /* TODO show local worktree changes */
3488 } else if (argc == 2) {
3489 repo_path = getcwd(NULL, 0);
3490 if (repo_path == NULL)
3491 return got_error_from_errno("getcwd");
3494 } else if (argc == 3) {
3495 repo_path = realpath(argv[0], NULL);
3496 if (repo_path == NULL)
3497 return got_error_from_errno2("realpath", argv[0]);
3505 error = got_repo_open(&repo, repo_path, NULL);
3509 error = apply_unveil(got_repo_get_path(repo), NULL);
3513 error = got_repo_match_object_id_prefix(&id1, id_str1,
3514 GOT_OBJ_TYPE_ANY, repo);
3518 error = got_repo_match_object_id_prefix(&id2, id_str2,
3519 GOT_OBJ_TYPE_ANY, repo);
3523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3527 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3529 error = got_error_from_errno("view_open");
3532 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3535 error = view_loop(view);
3539 got_repo_close(repo);
3540 got_ref_list_free(&refs);
3548 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3553 struct tog_blame_line {
3555 struct got_object_id *id;
3558 static const struct got_error *
3559 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3560 const char *path, struct tog_blame_line *lines, int nlines,
3561 int blame_complete, int selected_line, int *first_displayed_line,
3562 int *last_displayed_line, int *eof, int max_lines,
3563 struct tog_colors *colors)
3565 const struct got_error *err;
3566 int lineno = 0, nprinted = 0;
3571 struct tog_blame_line *blame_line;
3572 struct got_object_id *prev_id = NULL;
3574 struct tog_color *tc;
3576 err = got_object_id_str(&id_str, id);
3581 werase(view->window);
3583 if (asprintf(&line, "commit %s", id_str) == -1) {
3584 err = got_error_from_errno("asprintf");
3589 err = format_line(&wline, &width, line, view->ncols, 0);
3594 if (view_needs_focus_indication(view))
3595 wstandout(view->window);
3596 tc = get_color(colors, TOG_COLOR_COMMIT);
3598 wattr_on(view->window,
3599 COLOR_PAIR(tc->colorpair), NULL);
3600 waddwstr(view->window, wline);
3602 wattr_off(view->window,
3603 COLOR_PAIR(tc->colorpair), NULL);
3604 if (view_needs_focus_indication(view))
3605 wstandend(view->window);
3608 if (width < view->ncols - 1)
3609 waddch(view->window, '\n');
3611 if (asprintf(&line, "[%d/%d] %s%s",
3612 *first_displayed_line - 1 + selected_line, nlines,
3613 blame_complete ? "" : "annotating... ", path) == -1) {
3615 return got_error_from_errno("asprintf");
3618 err = format_line(&wline, &width, line, view->ncols, 0);
3623 waddwstr(view->window, wline);
3626 if (width < view->ncols - 1)
3627 waddch(view->window, '\n');
3630 while (nprinted < max_lines - 2) {
3631 line = parse_next_line(f, &len);
3636 if (++lineno < *first_displayed_line) {
3641 if (view->ncols <= 9) {
3643 wline = wcsdup(L"");
3645 err = got_error_from_errno("wcsdup");
3647 err = format_line(&wline, &width, line,
3648 view->ncols - 9, 9);
3656 if (view->focussed && nprinted == selected_line - 1)
3657 wstandout(view->window);
3660 blame_line = &lines[lineno - 1];
3661 if (blame_line->annotated && prev_id &&
3662 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3664 nprinted == selected_line - 1)) {
3665 waddstr(view->window, " ");
3666 } else if (blame_line->annotated) {
3668 err = got_object_id_str(&id_str, blame_line->id);
3674 tc = get_color(colors, TOG_COLOR_COMMIT);
3676 wattr_on(view->window,
3677 COLOR_PAIR(tc->colorpair), NULL);
3678 wprintw(view->window, "%.8s", id_str);
3680 wattr_off(view->window,
3681 COLOR_PAIR(tc->colorpair), NULL);
3683 prev_id = blame_line->id;
3685 waddstr(view->window, "........");
3689 waddstr(view->window, "........");
3693 if (view->focussed && nprinted == selected_line - 1)
3694 wstandend(view->window);
3695 waddstr(view->window, " ");
3697 waddwstr(view->window, wline);
3698 if (width <= view->ncols - 1)
3699 waddch(view->window, '\n');
3700 if (++nprinted == 1)
3701 *first_displayed_line = lineno;
3706 *last_displayed_line = lineno;
3713 static const struct got_error *
3714 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3716 const struct got_error *err = NULL;
3717 struct tog_blame_cb_args *a = arg;
3718 struct tog_blame_line *line;
3721 if (nlines != a->nlines ||
3722 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3723 return got_error(GOT_ERR_RANGE);
3725 errcode = pthread_mutex_lock(&tog_mutex);
3727 return got_error_set_errno(errcode, "pthread_mutex_lock");
3729 if (*a->quit) { /* user has quit the blame view */
3730 err = got_error(GOT_ERR_ITER_COMPLETED);
3735 goto done; /* no change in this commit */
3737 line = &a->lines[lineno - 1];
3738 if (line->annotated)
3741 line->id = got_object_id_dup(id);
3742 if (line->id == NULL) {
3743 err = got_error_from_errno("got_object_id_dup");
3746 line->annotated = 1;
3748 errcode = pthread_mutex_unlock(&tog_mutex);
3750 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3755 blame_thread(void *arg)
3757 const struct got_error *err;
3758 struct tog_blame_thread_args *ta = arg;
3759 struct tog_blame_cb_args *a = ta->cb_args;
3762 err = block_signals_used_by_main_thread();
3766 err = got_blame(ta->path, a->commit_id, ta->repo,
3767 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3768 if (err && err->code == GOT_ERR_CANCELLED)
3771 errcode = pthread_mutex_lock(&tog_mutex);
3773 return (void *)got_error_set_errno(errcode,
3774 "pthread_mutex_lock");
3776 got_repo_close(ta->repo);
3780 errcode = pthread_mutex_unlock(&tog_mutex);
3781 if (errcode && err == NULL)
3782 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3787 static struct got_object_id *
3788 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3789 int first_displayed_line, int selected_line)
3791 struct tog_blame_line *line;
3796 line = &lines[first_displayed_line - 1 + selected_line - 1];
3797 if (!line->annotated)
3803 static const struct got_error *
3804 stop_blame(struct tog_blame *blame)
3806 const struct got_error *err = NULL;
3809 if (blame->thread) {
3811 errcode = pthread_mutex_unlock(&tog_mutex);
3813 return got_error_set_errno(errcode,
3814 "pthread_mutex_unlock");
3815 errcode = pthread_join(blame->thread, (void **)&err);
3817 return got_error_set_errno(errcode, "pthread_join");
3818 errcode = pthread_mutex_lock(&tog_mutex);
3820 return got_error_set_errno(errcode,
3821 "pthread_mutex_lock");
3822 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3824 blame->thread = NULL;
3826 if (blame->thread_args.repo) {
3827 got_repo_close(blame->thread_args.repo);
3828 blame->thread_args.repo = NULL;
3831 if (fclose(blame->f) != 0 && err == NULL)
3832 err = got_error_from_errno("fclose");
3836 for (i = 0; i < blame->nlines; i++)
3837 free(blame->lines[i].id);
3839 blame->lines = NULL;
3841 free(blame->cb_args.commit_id);
3842 blame->cb_args.commit_id = NULL;
3847 static const struct got_error *
3848 cancel_blame_view(void *arg)
3850 const struct got_error *err = NULL;
3854 errcode = pthread_mutex_lock(&tog_mutex);
3856 return got_error_set_errno(errcode,
3857 "pthread_mutex_unlock");
3860 err = got_error(GOT_ERR_CANCELLED);
3862 errcode = pthread_mutex_unlock(&tog_mutex);
3864 return got_error_set_errno(errcode,
3865 "pthread_mutex_lock");
3870 static const struct got_error *
3871 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3872 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3873 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3874 struct got_repository *repo)
3876 const struct got_error *err = NULL;
3877 struct got_blob_object *blob = NULL;
3878 struct got_repository *thread_repo = NULL;
3879 struct got_object_id *obj_id = NULL;
3882 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3886 err = got_object_get_type(&obj_type, repo, obj_id);
3890 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3891 err = got_error(GOT_ERR_OBJ_TYPE);
3895 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3898 blame->f = got_opentemp();
3899 if (blame->f == NULL) {
3900 err = got_error_from_errno("got_opentemp");
3903 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3904 &blame->line_offsets, blame->f, blob);
3905 if (err || blame->nlines == 0)
3908 /* Don't include \n at EOF in the blame line count. */
3909 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3912 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3913 if (blame->lines == NULL) {
3914 err = got_error_from_errno("calloc");
3918 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3922 blame->cb_args.view = view;
3923 blame->cb_args.lines = blame->lines;
3924 blame->cb_args.nlines = blame->nlines;
3925 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3926 if (blame->cb_args.commit_id == NULL) {
3927 err = got_error_from_errno("got_object_id_dup");
3930 blame->cb_args.quit = done;
3932 blame->thread_args.path = path;
3933 blame->thread_args.repo = thread_repo;
3934 blame->thread_args.cb_args = &blame->cb_args;
3935 blame->thread_args.complete = blame_complete;
3936 blame->thread_args.cancel_cb = cancel_blame_view;
3937 blame->thread_args.cancel_arg = done;
3938 *blame_complete = 0;
3942 got_object_blob_close(blob);
3949 static const struct got_error *
3950 open_blame_view(struct tog_view *view, char *path,
3951 struct got_object_id *commit_id, struct got_reflist_head *refs,
3952 struct got_repository *repo)
3954 const struct got_error *err = NULL;
3955 struct tog_blame_view_state *s = &view->state.blame;
3957 SIMPLEQ_INIT(&s->blamed_commits);
3959 s->path = strdup(path);
3960 if (s->path == NULL)
3961 return got_error_from_errno("strdup");
3963 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3969 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3970 s->first_displayed_line = 1;
3971 s->last_displayed_line = view->nlines;
3972 s->selected_line = 1;
3973 s->blame_complete = 0;
3976 s->commit_id = commit_id;
3977 memset(&s->blame, 0, sizeof(s->blame));
3979 SIMPLEQ_INIT(&s->colors);
3980 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3981 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3982 get_color_value("TOG_COLOR_COMMIT"));
3987 view->show = show_blame_view;
3988 view->input = input_blame_view;
3989 view->close = close_blame_view;
3990 view->search_start = search_start_blame_view;
3991 view->search_next = search_next_blame_view;
3993 return run_blame(&s->blame, view, &s->blame_complete,
3994 &s->first_displayed_line, &s->last_displayed_line,
3995 &s->selected_line, &s->done, &s->eof, s->path,
3996 s->blamed_commit->id, s->repo);
3999 static const struct got_error *
4000 close_blame_view(struct tog_view *view)
4002 const struct got_error *err = NULL;
4003 struct tog_blame_view_state *s = &view->state.blame;
4005 if (s->blame.thread)
4006 err = stop_blame(&s->blame);
4008 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4009 struct got_object_qid *blamed_commit;
4010 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4011 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4012 got_object_qid_free(blamed_commit);
4016 free_colors(&s->colors);
4021 static const struct got_error *
4022 search_start_blame_view(struct tog_view *view)
4024 struct tog_blame_view_state *s = &view->state.blame;
4026 s->matched_line = 0;
4030 static const struct got_error *
4031 search_next_blame_view(struct tog_view *view)
4033 struct tog_blame_view_state *s = &view->state.blame;
4036 if (!view->searching) {
4037 view->search_next_done = 1;
4041 if (s->matched_line) {
4042 if (view->searching == TOG_SEARCH_FORWARD)
4043 lineno = s->matched_line + 1;
4045 lineno = s->matched_line - 1;
4047 if (view->searching == TOG_SEARCH_FORWARD)
4050 lineno = s->blame.nlines;
4058 if (lineno <= 0 || lineno > s->blame.nlines) {
4059 if (s->matched_line == 0) {
4060 view->search_next_done = 1;
4065 if (view->searching == TOG_SEARCH_FORWARD)
4068 lineno = s->blame.nlines;
4071 offset = s->blame.line_offsets[lineno - 1];
4072 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4074 return got_error_from_errno("fseeko");
4077 line = parse_next_line(s->blame.f, &len);
4078 if (line && match_line(line, &view->regex)) {
4079 view->search_next_done = 1;
4080 s->matched_line = lineno;
4085 if (view->searching == TOG_SEARCH_FORWARD)
4091 if (s->matched_line) {
4092 s->first_displayed_line = s->matched_line;
4093 s->selected_line = 1;
4099 static const struct got_error *
4100 show_blame_view(struct tog_view *view)
4102 const struct got_error *err = NULL;
4103 struct tog_blame_view_state *s = &view->state.blame;
4106 if (s->blame.thread == NULL) {
4107 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4108 &s->blame.thread_args);
4110 return got_error_set_errno(errcode, "pthread_create");
4112 halfdelay(1); /* fast refresh while annotating */
4115 if (s->blame_complete)
4116 halfdelay(10); /* disable fast refresh */
4118 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4119 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4120 s->selected_line, &s->first_displayed_line,
4121 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4127 static const struct got_error *
4128 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4129 struct tog_view **focus_view, struct tog_view *view, int ch)
4131 const struct got_error *err = NULL, *thread_err = NULL;
4132 struct tog_view *diff_view;
4133 struct tog_blame_view_state *s = &view->state.blame;
4142 if (s->selected_line > 1)
4144 else if (s->selected_line == 1 &&
4145 s->first_displayed_line > 1)
4146 s->first_displayed_line--;
4149 if (s->first_displayed_line == 1) {
4150 s->selected_line = 1;
4153 if (s->first_displayed_line > view->nlines - 2)
4154 s->first_displayed_line -=
4157 s->first_displayed_line = 1;
4161 if (s->selected_line < view->nlines - 2 &&
4162 s->first_displayed_line +
4163 s->selected_line <= s->blame.nlines)
4165 else if (s->last_displayed_line <
4167 s->first_displayed_line++;
4171 struct got_object_id *id = NULL;
4172 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4173 s->first_displayed_line, s->selected_line);
4177 struct got_commit_object *commit;
4178 struct got_object_qid *pid;
4179 struct got_object_id *blob_id = NULL;
4181 err = got_object_open_as_commit(&commit,
4185 pid = SIMPLEQ_FIRST(
4186 got_object_commit_get_parent_ids(commit));
4188 got_object_commit_close(commit);
4191 /* Check if path history ends here. */
4192 err = got_object_id_by_path(&blob_id, s->repo,
4195 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4197 got_object_commit_close(commit);
4200 err = got_object_get_type(&obj_type, s->repo,
4203 /* Can't blame non-blob type objects. */
4204 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4205 got_object_commit_close(commit);
4208 err = got_object_qid_alloc(&s->blamed_commit,
4210 got_object_commit_close(commit);
4212 if (got_object_id_cmp(id,
4213 s->blamed_commit->id) == 0)
4215 err = got_object_qid_alloc(&s->blamed_commit,
4221 thread_err = stop_blame(&s->blame);
4225 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4226 s->blamed_commit, entry);
4227 err = run_blame(&s->blame, view, &s->blame_complete,
4228 &s->first_displayed_line, &s->last_displayed_line,
4229 &s->selected_line, &s->done, &s->eof,
4230 s->path, s->blamed_commit->id, s->repo);
4236 struct got_object_qid *first;
4237 first = SIMPLEQ_FIRST(&s->blamed_commits);
4238 if (!got_object_id_cmp(first->id, s->commit_id))
4241 thread_err = stop_blame(&s->blame);
4245 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4246 got_object_qid_free(s->blamed_commit);
4248 SIMPLEQ_FIRST(&s->blamed_commits);
4249 err = run_blame(&s->blame, view, &s->blame_complete,
4250 &s->first_displayed_line, &s->last_displayed_line,
4251 &s->selected_line, &s->done, &s->eof, s->path,
4252 s->blamed_commit->id, s->repo);
4259 struct got_object_id *id = NULL;
4260 struct got_object_qid *pid;
4261 struct got_commit_object *commit = NULL;
4262 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4263 s->first_displayed_line, s->selected_line);
4266 err = got_object_open_as_commit(&commit, s->repo, id);
4269 pid = SIMPLEQ_FIRST(
4270 got_object_commit_get_parent_ids(commit));
4271 if (view_is_parent_view(view))
4272 begin_x = view_split_begin_x(view->begin_x);
4273 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4274 if (diff_view == NULL) {
4275 got_object_commit_close(commit);
4276 err = got_error_from_errno("view_open");
4279 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4280 id, NULL, s->refs, s->repo);
4281 got_object_commit_close(commit);
4283 view_close(diff_view);
4286 if (view_is_parent_view(view)) {
4287 err = view_close_child(view);
4290 err = view_set_child(view, diff_view);
4292 view_close(diff_view);
4295 *focus_view = diff_view;
4296 view->child_focussed = 1;
4298 *new_view = diff_view;
4305 if (s->last_displayed_line >= s->blame.nlines &&
4306 s->selected_line >= MIN(s->blame.nlines,
4307 view->nlines - 2)) {
4310 if (s->last_displayed_line >= s->blame.nlines &&
4311 s->selected_line < view->nlines - 2) {
4312 s->selected_line = MIN(s->blame.nlines,
4316 if (s->last_displayed_line + view->nlines - 2
4318 s->first_displayed_line +=
4321 s->first_displayed_line =
4326 if (s->selected_line > view->nlines - 2) {
4327 s->selected_line = MIN(s->blame.nlines,
4334 return thread_err ? thread_err : err;
4337 static const struct got_error *
4338 cmd_blame(int argc, char *argv[])
4340 const struct got_error *error;
4341 struct got_repository *repo = NULL;
4342 struct got_reflist_head refs;
4343 struct got_worktree *worktree = NULL;
4344 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4345 struct got_object_id *commit_id = NULL;
4346 char *commit_id_str = NULL;
4348 struct tog_view *view;
4350 SIMPLEQ_INIT(&refs);
4353 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4358 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4361 commit_id_str = optarg;
4364 repo_path = realpath(optarg, NULL);
4365 if (repo_path == NULL)
4366 return got_error_from_errno2("realpath",
4383 cwd = getcwd(NULL, 0);
4385 error = got_error_from_errno("getcwd");
4388 if (repo_path == NULL) {
4389 error = got_worktree_open(&worktree, cwd);
4390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4396 strdup(got_worktree_get_repo_path(worktree));
4397 if (repo_path == NULL)
4398 error = got_error_from_errno("strdup");
4402 repo_path = strdup(cwd);
4403 if (repo_path == NULL) {
4404 error = got_error_from_errno("strdup");
4412 error = got_repo_open(&repo, repo_path, NULL);
4416 error = apply_unveil(got_repo_get_path(repo), NULL);
4421 const char *prefix = got_worktree_get_path_prefix(worktree);
4422 char *p, *worktree_subdir = cwd +
4423 strlen(got_worktree_get_root_path(worktree));
4424 if (asprintf(&p, "%s%s%s%s%s",
4425 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4426 worktree_subdir, worktree_subdir[0] ? "/" : "",
4428 error = got_error_from_errno("asprintf");
4431 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4434 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4439 if (commit_id_str == NULL) {
4440 struct got_reference *head_ref;
4441 error = got_ref_open(&head_ref, repo, worktree ?
4442 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4445 error = got_ref_resolve(&commit_id, repo, head_ref);
4446 got_ref_close(head_ref);
4448 error = got_repo_match_object_id(&commit_id, NULL,
4449 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4454 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4458 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4460 error = got_error_from_errno("view_open");
4463 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4467 /* Release work tree lock. */
4468 got_worktree_close(worktree);
4471 error = view_loop(view);
4477 got_worktree_close(worktree);
4479 got_repo_close(repo);
4480 got_ref_list_free(&refs);
4484 static const struct got_error *
4485 draw_tree_entries(struct tog_view *view,
4486 struct got_tree_entry **first_displayed_entry,
4487 struct got_tree_entry **last_displayed_entry,
4488 struct got_tree_entry **selected_entry, int *ndisplayed,
4489 const char *label, int show_ids, const char *parent_path,
4490 struct got_tree_object *tree, int selected, int limit,
4491 int isroot, struct tog_colors *colors)
4493 const struct got_error *err = NULL;
4494 struct got_tree_entry *te;
4496 struct tog_color *tc;
4497 int width, n, i, nentries;
4501 werase(view->window);
4506 err = format_line(&wline, &width, label, view->ncols, 0);
4509 if (view_needs_focus_indication(view))
4510 wstandout(view->window);
4511 tc = get_color(colors, TOG_COLOR_COMMIT);
4513 wattr_on(view->window,
4514 COLOR_PAIR(tc->colorpair), NULL);
4515 waddwstr(view->window, wline);
4517 wattr_off(view->window,
4518 COLOR_PAIR(tc->colorpair), NULL);
4519 if (view_needs_focus_indication(view))
4520 wstandend(view->window);
4523 if (width < view->ncols - 1)
4524 waddch(view->window, '\n');
4527 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4530 waddwstr(view->window, wline);
4533 if (width < view->ncols - 1)
4534 waddch(view->window, '\n');
4537 waddch(view->window, '\n');
4541 if (*first_displayed_entry == NULL) {
4542 te = got_object_tree_get_first_entry(tree);
4543 if (selected == 0) {
4545 wstandout(view->window);
4546 *selected_entry = NULL;
4548 waddstr(view->window, " ..\n"); /* parent directory */
4549 if (selected == 0 && view->focussed)
4550 wstandend(view->window);
4557 te = *first_displayed_entry;
4560 nentries = got_object_tree_get_nentries(tree);
4561 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4562 char *line = NULL, *id_str = NULL;
4563 const char *modestr = "";
4566 te = got_object_tree_get_entry(tree, i);
4567 mode = got_tree_entry_get_mode(te);
4570 err = got_object_id_str(&id_str,
4571 got_tree_entry_get_id(te));
4573 return got_error_from_errno(
4574 "got_object_id_str");
4576 if (got_object_tree_entry_is_submodule(te))
4578 else if (S_ISLNK(mode))
4580 else if (S_ISDIR(mode))
4582 else if (mode & S_IXUSR)
4584 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4585 got_tree_entry_get_name(te), modestr) == -1) {
4587 return got_error_from_errno("asprintf");
4590 err = format_line(&wline, &width, line, view->ncols, 0);
4595 if (n == selected) {
4597 wstandout(view->window);
4598 *selected_entry = te;
4600 tc = match_color(colors, line);
4602 wattr_on(view->window,
4603 COLOR_PAIR(tc->colorpair), NULL);
4604 waddwstr(view->window, wline);
4606 wattr_off(view->window,
4607 COLOR_PAIR(tc->colorpair), NULL);
4608 if (width < view->ncols - 1)
4609 waddch(view->window, '\n');
4610 if (n == selected && view->focussed)
4611 wstandend(view->window);
4617 *last_displayed_entry = te;
4626 tree_scroll_up(struct tog_view *view,
4627 struct got_tree_entry **first_displayed_entry, int maxscroll,
4628 struct got_tree_object *tree, int isroot)
4630 struct got_tree_entry *te;
4633 if (*first_displayed_entry == NULL)
4636 te = got_object_tree_get_entry(tree, 0);
4637 if (*first_displayed_entry == te) {
4639 *first_displayed_entry = NULL;
4644 while (*first_displayed_entry && i < maxscroll) {
4645 *first_displayed_entry = got_tree_entry_get_prev(tree,
4646 *first_displayed_entry);
4649 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4650 *first_displayed_entry = NULL;
4654 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4655 struct got_tree_entry *last_displayed_entry,
4656 struct got_tree_object *tree)
4658 struct got_tree_entry *next, *last;
4661 if (*first_displayed_entry)
4662 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4664 next = got_object_tree_get_first_entry(tree);
4666 last = last_displayed_entry;
4667 while (next && last && n++ < maxscroll) {
4668 last = got_tree_entry_get_next(tree, last);
4670 *first_displayed_entry = next;
4671 next = got_tree_entry_get_next(tree, next);
4677 static const struct got_error *
4678 tree_entry_path(char **path, struct tog_parent_trees *parents,
4679 struct got_tree_entry *te)
4681 const struct got_error *err = NULL;
4682 struct tog_parent_tree *pt;
4683 size_t len = 2; /* for leading slash and NUL */
4685 TAILQ_FOREACH(pt, parents, entry)
4686 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4689 len += strlen(got_tree_entry_get_name(te));
4691 *path = calloc(1, len);
4693 return got_error_from_errno("calloc");
4696 pt = TAILQ_LAST(parents, tog_parent_trees);
4698 const char *name = got_tree_entry_get_name(pt->selected_entry);
4699 if (strlcat(*path, name, len) >= len) {
4700 err = got_error(GOT_ERR_NO_SPACE);
4703 if (strlcat(*path, "/", len) >= len) {
4704 err = got_error(GOT_ERR_NO_SPACE);
4707 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4710 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4711 err = got_error(GOT_ERR_NO_SPACE);
4723 static const struct got_error *
4724 blame_tree_entry(struct tog_view **new_view, int begin_x,
4725 struct got_tree_entry *te, struct tog_parent_trees *parents,
4726 struct got_object_id *commit_id, struct got_reflist_head *refs,
4727 struct got_repository *repo)
4729 const struct got_error *err = NULL;
4731 struct tog_view *blame_view;
4735 err = tree_entry_path(&path, parents, te);
4739 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4740 if (blame_view == NULL) {
4741 err = got_error_from_errno("view_open");
4745 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4747 if (err->code == GOT_ERR_CANCELLED)
4749 view_close(blame_view);
4751 *new_view = blame_view;
4757 static const struct got_error *
4758 log_tree_entry(struct tog_view **new_view, int begin_x,
4759 struct got_tree_entry *te, struct tog_parent_trees *parents,
4760 struct got_object_id *commit_id, struct got_reflist_head *refs,
4761 struct got_repository *repo)
4763 struct tog_view *log_view;
4764 const struct got_error *err = NULL;
4769 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4770 if (log_view == NULL)
4771 return got_error_from_errno("view_open");
4773 err = tree_entry_path(&path, parents, te);
4777 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4779 view_close(log_view);
4781 *new_view = log_view;
4786 static const struct got_error *
4787 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4788 struct got_object_id *commit_id, struct got_reflist_head *refs,
4789 struct got_repository *repo)
4791 const struct got_error *err = NULL;
4792 char *commit_id_str = NULL;
4793 struct tog_tree_view_state *s = &view->state.tree;
4795 TAILQ_INIT(&s->parents);
4797 err = got_object_id_str(&commit_id_str, commit_id);
4801 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4802 err = got_error_from_errno("asprintf");
4806 s->root = s->tree = root;
4807 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4808 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4809 s->commit_id = got_object_id_dup(commit_id);
4810 if (s->commit_id == NULL) {
4811 err = got_error_from_errno("got_object_id_dup");
4817 SIMPLEQ_INIT(&s->colors);
4819 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4820 err = add_color(&s->colors, "\\$$",
4821 TOG_COLOR_TREE_SUBMODULE,
4822 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4825 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4826 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4828 free_colors(&s->colors);
4831 err = add_color(&s->colors, "/$",
4832 TOG_COLOR_TREE_DIRECTORY,
4833 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4835 free_colors(&s->colors);
4839 err = add_color(&s->colors, "\\*$",
4840 TOG_COLOR_TREE_EXECUTABLE,
4841 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4843 free_colors(&s->colors);
4847 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4848 get_color_value("TOG_COLOR_COMMIT"));
4850 free_colors(&s->colors);
4855 view->show = show_tree_view;
4856 view->input = input_tree_view;
4857 view->close = close_tree_view;
4858 view->search_start = search_start_tree_view;
4859 view->search_next = search_next_tree_view;
4861 free(commit_id_str);
4863 free(s->tree_label);
4864 s->tree_label = NULL;
4869 static const struct got_error *
4870 close_tree_view(struct tog_view *view)
4872 struct tog_tree_view_state *s = &view->state.tree;
4874 free_colors(&s->colors);
4875 free(s->tree_label);
4876 s->tree_label = NULL;
4878 s->commit_id = NULL;
4879 while (!TAILQ_EMPTY(&s->parents)) {
4880 struct tog_parent_tree *parent;
4881 parent = TAILQ_FIRST(&s->parents);
4882 TAILQ_REMOVE(&s->parents, parent, entry);
4886 if (s->tree != s->root)
4887 got_object_tree_close(s->tree);
4888 got_object_tree_close(s->root);
4893 static const struct got_error *
4894 search_start_tree_view(struct tog_view *view)
4896 struct tog_tree_view_state *s = &view->state.tree;
4898 s->matched_entry = NULL;
4903 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4905 regmatch_t regmatch;
4907 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4911 static const struct got_error *
4912 search_next_tree_view(struct tog_view *view)
4914 struct tog_tree_view_state *s = &view->state.tree;
4915 struct got_tree_entry *te = NULL;
4917 if (!view->searching) {
4918 view->search_next_done = 1;
4922 if (s->matched_entry) {
4923 if (view->searching == TOG_SEARCH_FORWARD) {
4924 if (s->selected_entry)
4925 te = got_tree_entry_get_next(s->tree,
4928 te = got_object_tree_get_first_entry(s->tree);
4930 if (s->selected_entry == NULL)
4931 te = got_object_tree_get_last_entry(s->tree);
4933 te = got_tree_entry_get_prev(s->tree,
4937 if (view->searching == TOG_SEARCH_FORWARD)
4938 te = got_object_tree_get_first_entry(s->tree);
4940 te = got_object_tree_get_last_entry(s->tree);
4945 if (s->matched_entry == NULL) {
4946 view->search_next_done = 1;
4949 if (view->searching == TOG_SEARCH_FORWARD)
4950 te = got_object_tree_get_first_entry(s->tree);
4952 te = got_object_tree_get_last_entry(s->tree);
4955 if (match_tree_entry(te, &view->regex)) {
4956 view->search_next_done = 1;
4957 s->matched_entry = te;
4961 if (view->searching == TOG_SEARCH_FORWARD)
4962 te = got_tree_entry_get_next(s->tree, te);
4964 te = got_tree_entry_get_prev(s->tree, te);
4967 if (s->matched_entry) {
4968 s->first_displayed_entry = s->matched_entry;
4975 static const struct got_error *
4976 show_tree_view(struct tog_view *view)
4978 const struct got_error *err = NULL;
4979 struct tog_tree_view_state *s = &view->state.tree;
4982 err = tree_entry_path(&parent_path, &s->parents, NULL);
4986 err = draw_tree_entries(view, &s->first_displayed_entry,
4987 &s->last_displayed_entry, &s->selected_entry,
4988 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4989 s->tree, s->selected, view->nlines, s->tree == s->root,
4997 static const struct got_error *
4998 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4999 struct tog_view **focus_view, struct tog_view *view, int ch)
5001 const struct got_error *err = NULL;
5002 struct tog_tree_view_state *s = &view->state.tree;
5003 struct tog_view *log_view;
5004 int begin_x = 0, nscrolled;
5008 s->show_ids = !s->show_ids;
5011 if (!s->selected_entry)
5013 if (view_is_parent_view(view))
5014 begin_x = view_split_begin_x(view->begin_x);
5015 err = log_tree_entry(&log_view, begin_x,
5016 s->selected_entry, &s->parents,
5017 s->commit_id, s->refs, s->repo);
5018 if (view_is_parent_view(view)) {
5019 err = view_close_child(view);
5022 err = view_set_child(view, log_view);
5024 view_close(log_view);
5027 *focus_view = log_view;
5028 view->child_focussed = 1;
5030 *new_view = log_view;
5034 if (s->selected > 0) {
5036 if (s->selected == 0)
5039 if (s->selected > 0)
5041 tree_scroll_up(view, &s->first_displayed_entry, 1,
5042 s->tree, s->tree == s->root);
5045 tree_scroll_up(view, &s->first_displayed_entry,
5046 MAX(0, view->nlines - 4 - s->selected), s->tree,
5047 s->tree == s->root);
5049 if (got_object_tree_get_first_entry(s->tree) ==
5050 s->first_displayed_entry && s->tree != s->root)
5051 s->first_displayed_entry = NULL;
5055 if (s->selected < s->ndisplayed - 1) {
5059 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5061 /* can't scroll any further */
5063 tree_scroll_down(&s->first_displayed_entry, 1,
5064 s->last_displayed_entry, s->tree);
5067 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5069 /* can't scroll any further; move cursor down */
5070 if (s->selected < s->ndisplayed - 1)
5071 s->selected = s->ndisplayed - 1;
5074 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5075 view->nlines, s->last_displayed_entry, s->tree);
5076 if (nscrolled < view->nlines) {
5078 struct got_tree_entry *te;
5079 te = s->first_displayed_entry;
5082 te = got_tree_entry_get_next(s->tree, te);
5084 s->selected = ndisplayed - 1;
5090 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5091 struct tog_parent_tree *parent;
5092 /* user selected '..' */
5093 if (s->tree == s->root)
5095 parent = TAILQ_FIRST(&s->parents);
5096 TAILQ_REMOVE(&s->parents, parent,
5098 got_object_tree_close(s->tree);
5099 s->tree = parent->tree;
5100 s->first_displayed_entry =
5101 parent->first_displayed_entry;
5103 parent->selected_entry;
5104 s->selected = parent->selected;
5106 } else if (S_ISDIR(got_tree_entry_get_mode(
5107 s->selected_entry))) {
5108 struct got_tree_object *subtree;
5109 err = got_object_open_as_tree(&subtree, s->repo,
5110 got_tree_entry_get_id(s->selected_entry));
5113 err = tree_view_visit_subtree(subtree, s);
5115 got_object_tree_close(subtree);
5118 } else if (S_ISREG(got_tree_entry_get_mode(
5119 s->selected_entry))) {
5120 struct tog_view *blame_view;
5121 int begin_x = view_is_parent_view(view) ?
5122 view_split_begin_x(view->begin_x) : 0;
5124 err = blame_tree_entry(&blame_view, begin_x,
5125 s->selected_entry, &s->parents,
5126 s->commit_id, s->refs, s->repo);
5129 if (view_is_parent_view(view)) {
5130 err = view_close_child(view);
5133 err = view_set_child(view, blame_view);
5135 view_close(blame_view);
5138 *focus_view = blame_view;
5139 view->child_focussed = 1;
5141 *new_view = blame_view;
5145 if (s->selected > view->nlines)
5146 s->selected = s->ndisplayed - 1;
5159 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
5164 static const struct got_error *
5165 cmd_tree(int argc, char *argv[])
5167 const struct got_error *error;
5168 struct got_repository *repo = NULL;
5169 struct got_reflist_head refs;
5170 char *repo_path = NULL;
5171 struct got_object_id *commit_id = NULL;
5172 char *commit_id_arg = NULL;
5173 struct got_commit_object *commit = NULL;
5174 struct got_tree_object *tree = NULL;
5176 struct tog_view *view;
5178 SIMPLEQ_INIT(&refs);
5181 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5186 while ((ch = getopt(argc, argv, "c:")) != -1) {
5189 commit_id_arg = optarg;
5201 struct got_worktree *worktree;
5202 char *cwd = getcwd(NULL, 0);
5204 return got_error_from_errno("getcwd");
5205 error = got_worktree_open(&worktree, cwd);
5206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5211 strdup(got_worktree_get_repo_path(worktree));
5212 got_worktree_close(worktree);
5215 if (repo_path == NULL) {
5216 error = got_error_from_errno("strdup");
5219 } else if (argc == 1) {
5220 repo_path = realpath(argv[0], NULL);
5221 if (repo_path == NULL)
5222 return got_error_from_errno2("realpath", argv[0]);
5228 error = got_repo_open(&repo, repo_path, NULL);
5232 error = apply_unveil(got_repo_get_path(repo), NULL);
5236 error = got_repo_match_object_id(&commit_id, NULL,
5237 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5238 GOT_OBJ_TYPE_COMMIT, 1, repo);
5242 error = got_object_open_as_commit(&commit, repo, commit_id);
5246 error = got_object_open_as_tree(&tree, repo,
5247 got_object_commit_get_tree_id(commit));
5251 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5255 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5257 error = got_error_from_errno("view_open");
5260 error = open_tree_view(view, tree, commit_id, &refs, repo);
5263 error = view_loop(view);
5268 got_object_commit_close(commit);
5270 got_object_tree_close(tree);
5272 got_repo_close(repo);
5273 got_ref_list_free(&refs);
5282 fprintf(stderr, "commands:");
5283 for (i = 0; i < nitems(tog_commands); i++) {
5284 struct tog_cmd *cmd = &tog_commands[i];
5285 fprintf(stderr, " %s", cmd->name);
5287 fputc('\n', stderr);
5293 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5301 make_argv(const char *arg0, const char *arg1)
5304 int argc = (arg1 == NULL ? 1 : 2);
5306 argv = calloc(argc, sizeof(char *));
5309 argv[0] = strdup(arg0);
5310 if (argv[0] == NULL)
5313 argv[1] = strdup(arg1);
5314 if (argv[1] == NULL)
5322 main(int argc, char *argv[])
5324 const struct got_error *error = NULL;
5325 struct tog_cmd *cmd = NULL;
5326 int ch, hflag = 0, Vflag = 0;
5327 char **cmd_argv = NULL;
5328 static struct option longopts[] = {
5329 { "version", no_argument, NULL, 'V' },
5333 setlocale(LC_CTYPE, "");
5335 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5355 got_version_print_str();
5362 /* Build an argument vector which runs a default command. */
5363 cmd = &tog_commands[0];
5364 cmd_argv = make_argv(cmd->name, NULL);
5369 /* Did the user specific a command? */
5370 for (i = 0; i < nitems(tog_commands); i++) {
5371 if (strncmp(tog_commands[i].name, argv[0],
5372 strlen(argv[0])) == 0) {
5373 cmd = &tog_commands[i];
5379 fprintf(stderr, "%s: unknown command '%s'\n",
5380 getprogname(), argv[0]);
5389 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5393 if (error && error->code != GOT_ERR_CANCELLED)
5394 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);