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)
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;
254 /* passed from log view; may be NULL */
255 struct tog_view *log_view;
258 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
260 struct tog_log_thread_args {
261 pthread_cond_t need_commits;
263 struct got_commit_graph *graph;
264 struct commit_queue *commits;
265 const char *in_repo_path;
266 struct got_object_id *start_id;
267 struct got_repository *repo;
270 struct commit_queue_entry **first_displayed_entry;
271 struct commit_queue_entry **selected_entry;
273 int *search_next_done;
277 struct tog_log_view_state {
278 struct commit_queue commits;
279 struct commit_queue_entry *first_displayed_entry;
280 struct commit_queue_entry *last_displayed_entry;
281 struct commit_queue_entry *selected_entry;
284 const char *head_ref_name;
285 struct got_repository *repo;
286 struct got_reflist_head *refs;
287 struct got_object_id *start_id;
290 struct tog_log_thread_args thread_args;
291 struct commit_queue_entry *matched_entry;
292 struct commit_queue_entry *search_entry;
293 struct tog_colors colors;
296 #define TOG_COLOR_DIFF_MINUS 1
297 #define TOG_COLOR_DIFF_PLUS 2
298 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
299 #define TOG_COLOR_DIFF_META 4
300 #define TOG_COLOR_TREE_SUBMODULE 5
301 #define TOG_COLOR_TREE_SYMLINK 6
302 #define TOG_COLOR_TREE_DIRECTORY 7
303 #define TOG_COLOR_TREE_EXECUTABLE 8
304 #define TOG_COLOR_COMMIT 9
305 #define TOG_COLOR_AUTHOR 10
306 #define TOG_COLOR_DATE 11
308 struct tog_blame_cb_args {
309 struct tog_blame_line *lines; /* one per line */
312 struct tog_view *view;
313 struct got_object_id *commit_id;
317 struct tog_blame_thread_args {
319 struct got_repository *repo;
320 struct tog_blame_cb_args *cb_args;
322 got_cancel_cb cancel_cb;
329 struct tog_blame_line *lines;
333 struct tog_blame_thread_args thread_args;
334 struct tog_blame_cb_args cb_args;
338 struct tog_blame_view_state {
339 int first_displayed_line;
340 int last_displayed_line;
345 struct got_object_id_queue blamed_commits;
346 struct got_object_qid *blamed_commit;
348 struct got_repository *repo;
349 struct got_reflist_head *refs;
350 struct got_object_id *commit_id;
351 struct tog_blame blame;
353 struct tog_colors colors;
356 struct tog_parent_tree {
357 TAILQ_ENTRY(tog_parent_tree) entry;
358 struct got_tree_object *tree;
359 struct got_tree_entry *first_displayed_entry;
360 struct got_tree_entry *selected_entry;
364 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
366 struct tog_tree_view_state {
368 struct got_tree_object *root;
369 struct got_tree_object *tree;
370 struct got_tree_entry *first_displayed_entry;
371 struct got_tree_entry *last_displayed_entry;
372 struct got_tree_entry *selected_entry;
373 int ndisplayed, selected, show_ids;
374 struct tog_parent_trees parents;
375 struct got_object_id *commit_id;
376 struct got_repository *repo;
377 struct got_reflist_head *refs;
378 struct got_tree_entry *matched_entry;
379 struct tog_colors colors;
383 * We implement two types of views: parent views and child views.
385 * The 'Tab' key switches between a parent view and its child view.
386 * Child views are shown side-by-side to their parent view, provided
387 * there is enough screen estate.
389 * When a new view is opened from within a parent view, this new view
390 * becomes a child view of the parent view, replacing any existing child.
392 * When a new view is opened from within a child view, this new view
393 * becomes a parent view which will obscure the views below until the
394 * user quits the new parent view by typing 'q'.
396 * This list of views contains parent views only.
397 * Child views are only pointed to by their parent view.
399 TAILQ_HEAD(tog_view_list_head, tog_view);
402 TAILQ_ENTRY(tog_view) entry;
405 int nlines, ncols, begin_y, begin_x;
406 int lines, cols; /* copies of LINES and COLS */
408 struct tog_view *parent;
409 struct tog_view *child;
412 /* type-specific state */
413 enum tog_view_type type;
415 struct tog_diff_view_state diff;
416 struct tog_log_view_state log;
417 struct tog_blame_view_state blame;
418 struct tog_tree_view_state tree;
421 const struct got_error *(*show)(struct tog_view *);
422 const struct got_error *(*input)(struct tog_view **,
423 struct tog_view **, struct tog_view**, struct tog_view *, int);
424 const struct got_error *(*close)(struct tog_view *);
426 const struct got_error *(*search_start)(struct tog_view *);
427 const struct got_error *(*search_next)(struct tog_view *);
429 #define TOG_SEARCH_FORWARD 1
430 #define TOG_SEARCH_BACKWARD 2
431 int search_next_done;
435 static const struct got_error *open_diff_view(struct tog_view *,
436 struct got_object_id *, struct got_object_id *, struct tog_view *,
437 struct got_reflist_head *, struct got_repository *);
438 static const struct got_error *show_diff_view(struct tog_view *);
439 static const struct got_error *input_diff_view(struct tog_view **,
440 struct tog_view **, struct tog_view **, struct tog_view *, int);
441 static const struct got_error* close_diff_view(struct tog_view *);
443 static const struct got_error *open_log_view(struct tog_view *,
444 struct got_object_id *, struct got_reflist_head *,
445 struct got_repository *, const char *, const char *, int);
446 static const struct got_error * show_log_view(struct tog_view *);
447 static const struct got_error *input_log_view(struct tog_view **,
448 struct tog_view **, struct tog_view **, struct tog_view *, int);
449 static const struct got_error *close_log_view(struct tog_view *);
450 static const struct got_error *search_start_log_view(struct tog_view *);
451 static const struct got_error *search_next_log_view(struct tog_view *);
453 static const struct got_error *open_blame_view(struct tog_view *, char *,
454 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 static const struct got_error *show_blame_view(struct tog_view *);
456 static const struct got_error *input_blame_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_blame_view(struct tog_view *);
459 static const struct got_error *search_start_blame_view(struct tog_view *);
460 static const struct got_error *search_next_blame_view(struct tog_view *);
462 static const struct got_error *open_tree_view(struct tog_view *,
463 struct got_tree_object *, struct got_object_id *,
464 struct got_reflist_head *, struct got_repository *);
465 static const struct got_error *show_tree_view(struct tog_view *);
466 static const struct got_error *input_tree_view(struct tog_view **,
467 struct tog_view **, struct tog_view **, struct tog_view *, int);
468 static const struct got_error *close_tree_view(struct tog_view *);
469 static const struct got_error *search_start_tree_view(struct tog_view *);
470 static const struct got_error *search_next_tree_view(struct tog_view *);
472 static volatile sig_atomic_t tog_sigwinch_received;
473 static volatile sig_atomic_t tog_sigpipe_received;
474 static volatile sig_atomic_t tog_sigcont_received;
477 tog_sigwinch(int signo)
479 tog_sigwinch_received = 1;
483 tog_sigpipe(int signo)
485 tog_sigpipe_received = 1;
489 tog_sigcont(int signo)
491 tog_sigcont_received = 1;
494 static const struct got_error *
495 view_close(struct tog_view *view)
497 const struct got_error *err = NULL;
500 view_close(view->child);
504 err = view->close(view);
506 del_panel(view->panel);
508 delwin(view->window);
513 static struct tog_view *
514 view_open(int nlines, int ncols, int begin_y, int begin_x,
515 enum tog_view_type type)
517 struct tog_view *view = calloc(1, sizeof(*view));
525 view->nlines = nlines ? nlines : LINES - begin_y;
526 view->ncols = ncols ? ncols : COLS - begin_x;
527 view->begin_y = begin_y;
528 view->begin_x = begin_x;
529 view->window = newwin(nlines, ncols, begin_y, begin_x);
530 if (view->window == NULL) {
534 view->panel = new_panel(view->window);
535 if (view->panel == NULL ||
536 set_panel_userptr(view->panel, view) != OK) {
541 keypad(view->window, TRUE);
546 view_split_begin_x(int begin_x)
548 if (begin_x > 0 || COLS < 120)
550 return (COLS - MAX(COLS / 2, 80));
553 static const struct got_error *view_resize(struct tog_view *);
555 static const struct got_error *
556 view_splitscreen(struct tog_view *view)
558 const struct got_error *err = NULL;
561 view->begin_x = view_split_begin_x(0);
562 view->nlines = LINES;
563 view->ncols = COLS - view->begin_x;
566 err = view_resize(view);
570 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
571 return got_error_from_errno("mvwin");
576 static const struct got_error *
577 view_fullscreen(struct tog_view *view)
579 const struct got_error *err = NULL;
583 view->nlines = LINES;
587 err = view_resize(view);
591 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
592 return got_error_from_errno("mvwin");
598 view_is_parent_view(struct tog_view *view)
600 return view->parent == NULL;
603 static const struct got_error *
604 view_resize(struct tog_view *view)
608 if (view->lines > LINES)
609 nlines = view->nlines - (view->lines - LINES);
611 nlines = view->nlines + (LINES - view->lines);
613 if (view->cols > COLS)
614 ncols = view->ncols - (view->cols - COLS);
616 ncols = view->ncols + (COLS - view->cols);
618 if (wresize(view->window, nlines, ncols) == ERR)
619 return got_error_from_errno("wresize");
620 if (replace_panel(view->panel, view->window) == ERR)
621 return got_error_from_errno("replace_panel");
622 wclear(view->window);
624 view->nlines = nlines;
630 view->child->begin_x = view_split_begin_x(view->begin_x);
631 if (view->child->begin_x == 0) {
632 view_fullscreen(view->child);
633 if (view->child->focussed)
634 show_panel(view->child->panel);
636 show_panel(view->panel);
638 view_splitscreen(view->child);
639 show_panel(view->child->panel);
646 static const struct got_error *
647 view_close_child(struct tog_view *view)
649 const struct got_error *err = NULL;
651 if (view->child == NULL)
654 err = view_close(view->child);
659 static const struct got_error *
660 view_set_child(struct tog_view *view, struct tog_view *child)
662 const struct got_error *err = NULL;
665 child->parent = view;
670 view_is_splitscreen(struct tog_view *view)
672 return view->begin_x > 0;
681 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
682 cols = 80; /* Default */
688 resize_term(lines, cols);
691 static const struct got_error *
692 view_search_start(struct tog_view *view)
694 const struct got_error *err = NULL;
699 if (view->nlines < 1)
702 if (!view_is_parent_view(view))
703 begin_x = view_split_begin_x(view->begin_x);
704 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
706 wclrtoeol(view->window);
710 ret = wgetnstr(view->window, pattern, sizeof(pattern));
716 if (view->searching) {
717 regfree(&view->regex);
721 if (regcomp(&view->regex, pattern,
722 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
723 err = view->search_start(view);
725 regfree(&view->regex);
728 view->searching = TOG_SEARCH_FORWARD;
729 view->search_next_done = 0;
730 view->search_next(view);
736 static const struct got_error *
737 view_input(struct tog_view **new, struct tog_view **dead,
738 struct tog_view **focus, int *done, struct tog_view *view,
739 struct tog_view_list_head *views)
741 const struct got_error *err = NULL;
749 if (view->searching && !view->search_next_done) {
750 view->search_next(view);
754 nodelay(stdscr, FALSE);
755 /* Allow threads to make progress while we are waiting for input. */
756 errcode = pthread_mutex_unlock(&tog_mutex);
758 return got_error_set_errno(errcode, "pthread_mutex_unlock");
759 ch = wgetch(view->window);
760 errcode = pthread_mutex_lock(&tog_mutex);
762 return got_error_set_errno(errcode, "pthread_mutex_lock");
763 nodelay(stdscr, TRUE);
765 if (tog_sigwinch_received || tog_sigcont_received) {
767 tog_sigwinch_received = 0;
768 tog_sigcont_received = 0;
769 TAILQ_FOREACH(v, views, entry) {
770 err = view_resize(v);
773 err = v->input(new, dead, focus, v, KEY_RESIZE);
784 *focus = view->child;
785 view->child_focussed = 1;
786 } else if (view->parent) {
787 *focus = view->parent;
788 view->parent->child_focussed = 0;
792 err = view->input(new, dead, focus, view, ch);
799 if (view_is_parent_view(view)) {
800 if (view->child == NULL)
802 if (view_is_splitscreen(view->child)) {
803 *focus = view->child;
804 view->child_focussed = 1;
805 err = view_fullscreen(view->child);
807 err = view_splitscreen(view->child);
810 err = view->child->input(new, dead, focus,
811 view->child, KEY_RESIZE);
813 if (view_is_splitscreen(view)) {
815 view->parent->child_focussed = 1;
816 err = view_fullscreen(view);
818 err = view_splitscreen(view);
822 err = view->input(new, dead, focus, view,
829 if (view->search_start)
830 view_search_start(view);
832 err = view->input(new, dead, focus, view, ch);
836 if (view->search_next && view->searching) {
837 view->searching = (ch == 'n' ?
838 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 view->search_next_done = 0;
840 view->search_next(view);
842 err = view->input(new, dead, focus, view, ch);
845 err = view->input(new, dead, focus, view, ch);
853 view_vborder(struct tog_view *view)
856 struct tog_view *view_above;
859 return view_vborder(view->parent);
861 panel = panel_above(view->panel);
865 view_above = panel_userptr(panel);
866 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 view_needs_focus_indication(struct tog_view *view)
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL || view->child_focussed)
876 if (!view_is_splitscreen(view->child))
878 } else if (!view_is_splitscreen(view))
881 return view->focussed;
884 static const struct got_error *
885 view_loop(struct tog_view *view)
887 const struct got_error *err = NULL;
888 struct tog_view_list_head views;
889 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 int fast_refresh = 10;
891 int done = 0, errcode;
893 errcode = pthread_mutex_lock(&tog_mutex);
895 return got_error_set_errno(errcode, "pthread_mutex_lock");
898 TAILQ_INSERT_HEAD(&views, view, entry);
902 err = view->show(view);
907 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 /* Refresh fast during initialization, then become slower. */
909 if (fast_refresh && fast_refresh-- == 0)
910 halfdelay(10); /* switch to once per second */
912 err = view_input(&new_view, &dead_view, &focus_view, &done,
917 struct tog_view *prev = NULL;
919 if (view_is_parent_view(dead_view))
920 prev = TAILQ_PREV(dead_view,
921 tog_view_list_head, entry);
922 else if (view->parent != dead_view)
925 if (dead_view->parent)
926 dead_view->parent->child = NULL;
928 TAILQ_REMOVE(&views, dead_view, entry);
930 err = view_close(dead_view);
931 if (err || (dead_view == main_view && new_view == NULL))
934 if (view == dead_view) {
939 else if (!TAILQ_EMPTY(&views))
940 view = TAILQ_LAST(&views,
945 if (view->child && view->child_focussed)
946 focus_view = view->child;
953 struct tog_view *v, *t;
954 /* Only allow one parent view per type. */
955 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 if (v->type != new_view->type)
958 TAILQ_REMOVE(&views, v, entry);
964 TAILQ_INSERT_TAIL(&views, new_view, entry);
966 if (focus_view == NULL)
967 focus_view = new_view;
970 show_panel(focus_view->panel);
973 focus_view->focussed = 1;
976 show_panel(new_view->panel);
977 if (view->child && view_is_splitscreen(view->child))
978 show_panel(view->child->panel);
981 if (focus_view == NULL) {
983 show_panel(view->panel);
984 if (view->child && view_is_splitscreen(view->child))
985 show_panel(view->child->panel);
989 err = view->parent->show(view->parent);
993 err = view->show(view);
997 err = view->child->show(view->child);
1006 while (!TAILQ_EMPTY(&views)) {
1007 view = TAILQ_FIRST(&views);
1008 TAILQ_REMOVE(&views, view, entry);
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1024 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1029 /* Create newly allocated wide-character string equivalent to a byte string. */
1030 static const struct got_error *
1031 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1034 const struct got_error *err = NULL;
1037 *wlen = mbstowcs(NULL, s, 0);
1038 if (*wlen == (size_t)-1) {
1040 if (errno != EILSEQ)
1041 return got_error_from_errno("mbstowcs");
1043 /* byte string invalid in current encoding; try to "fix" it */
1044 err = got_mbsavis(&vis, &vislen, s);
1047 *wlen = mbstowcs(NULL, vis, 0);
1048 if (*wlen == (size_t)-1) {
1049 err = got_error_from_errno("mbstowcs"); /* give up */
1054 *ws = calloc(*wlen + 1, sizeof(**ws));
1056 err = got_error_from_errno("calloc");
1060 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 err = got_error_from_errno("mbstowcs");
1072 /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 static const struct got_error *
1074 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1077 const struct got_error *err = NULL;
1079 wchar_t *wline = NULL;
1086 err = mbs2ws(&wline, &wlen, line);
1092 int width = wcwidth(wline[i]);
1099 if (width == 1 || width == 2) {
1100 if (cols + width > wlimit)
1104 } else if (width == -1) {
1105 if (wline[i] == L'\t') {
1107 ((cols + col_tab_align) % TABSIZE);
1108 if (cols + width > wlimit)
1114 err = got_error_from_errno("wcwidth");
1129 static const struct got_error*
1130 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 struct got_object_id *id, struct got_repository *repo)
1133 static const struct got_error *err = NULL;
1134 struct got_reflist_entry *re;
1140 SIMPLEQ_FOREACH(re, refs, entry) {
1141 struct got_tag_object *tag = NULL;
1144 name = got_ref_get_name(re->ref);
1145 if (strcmp(name, GOT_REF_HEAD) == 0)
1147 if (strncmp(name, "refs/", 5) == 0)
1149 if (strncmp(name, "got/", 4) == 0)
1151 if (strncmp(name, "heads/", 6) == 0)
1153 if (strncmp(name, "remotes/", 8) == 0)
1155 if (strncmp(name, "tags/", 5) == 0) {
1156 err = got_object_open_as_tag(&tag, repo, re->id);
1158 if (err->code != GOT_ERR_OBJ_TYPE)
1160 /* Ref points at something other than a tag. */
1165 cmp = got_object_id_cmp(tag ?
1166 got_object_tag_get_object_id(tag) : re->id, id);
1168 got_object_tag_close(tag);
1172 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 s ? ", " : "", name) == -1) {
1174 err = got_error_from_errno("asprintf");
1185 static const struct got_error *
1186 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1189 char *smallerthan, *at;
1191 smallerthan = strchr(author, '<');
1192 if (smallerthan && smallerthan[1] != '\0')
1193 author = smallerthan + 1;
1194 at = strchr(author, '@');
1197 return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 static const struct got_error *
1201 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 struct got_object_id *id, struct got_reflist_head *refs,
1203 const size_t date_display_cols, int author_display_cols,
1204 struct tog_colors *colors)
1206 const struct got_error *err = NULL;
1207 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 char *logmsg0 = NULL, *logmsg = NULL;
1209 char *author = NULL;
1210 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 int author_width, logmsg_width;
1212 char *newline, *line = NULL;
1214 const int avail = view->ncols;
1216 time_t committer_time;
1217 struct tog_color *tc;
1219 committer_time = got_object_commit_get_committer_time(commit);
1220 if (localtime_r(&committer_time, &tm) == NULL)
1221 return got_error_from_errno("localtime_r");
1222 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1224 return got_error(GOT_ERR_NO_SPACE);
1226 if (avail <= date_display_cols)
1227 limit = MIN(sizeof(datebuf) - 1, avail);
1229 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 tc = get_color(colors, TOG_COLOR_DATE);
1232 wattr_on(view->window,
1233 COLOR_PAIR(tc->colorpair), NULL);
1234 waddnstr(view->window, datebuf, limit);
1236 wattr_off(view->window,
1237 COLOR_PAIR(tc->colorpair), NULL);
1244 err = got_object_id_str(&id_str, id);
1247 tc = get_color(colors, TOG_COLOR_COMMIT);
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 wprintw(view->window, "%.8s ", id_str);
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1261 author = strdup(got_object_commit_get_author(commit));
1262 if (author == NULL) {
1263 err = got_error_from_errno("strdup");
1266 err = format_author(&wauthor, &author_width, author, avail - col, col);
1269 tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 wattr_on(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1273 waddwstr(view->window, wauthor);
1275 wattr_off(view->window,
1276 COLOR_PAIR(tc->colorpair), NULL);
1277 col += author_width;
1278 while (col < avail && author_width < author_display_cols + 2) {
1279 waddch(view->window, ' ');
1286 err = got_object_commit_get_logmsg(&logmsg0, commit);
1290 while (*logmsg == '\n')
1292 newline = strchr(logmsg, '\n');
1295 limit = avail - col;
1296 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1299 waddwstr(view->window, wlogmsg);
1300 col += logmsg_width;
1301 while (col < avail) {
1302 waddch(view->window, ' ');
1314 static struct commit_queue_entry *
1315 alloc_commit_queue_entry(struct got_commit_object *commit,
1316 struct got_object_id *id)
1318 struct commit_queue_entry *entry;
1320 entry = calloc(1, sizeof(*entry));
1325 entry->commit = commit;
1330 pop_commit(struct commit_queue *commits)
1332 struct commit_queue_entry *entry;
1334 entry = TAILQ_FIRST(&commits->head);
1335 TAILQ_REMOVE(&commits->head, entry, entry);
1336 got_object_commit_close(entry->commit);
1337 commits->ncommits--;
1338 /* Don't free entry->id! It is owned by the commit graph. */
1343 free_commits(struct commit_queue *commits)
1345 while (!TAILQ_EMPTY(&commits->head))
1346 pop_commit(commits);
1349 static const struct got_error *
1350 match_commit(int *have_match, struct got_object_id *id,
1351 struct got_commit_object *commit, regex_t *regex)
1353 const struct got_error *err = NULL;
1354 regmatch_t regmatch;
1355 char *id_str = NULL, *logmsg = NULL;
1359 err = got_object_id_str(&id_str, id);
1363 err = got_object_commit_get_logmsg(&logmsg, commit);
1367 if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 ®match, 0) == 0 ||
1369 regexec(regex, got_object_commit_get_committer(commit), 1,
1370 ®match, 0) == 0 ||
1371 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1372 regexec(regex, logmsg, 1, ®match, 0) == 0)
1380 static const struct got_error *
1381 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 int minqueue, struct got_repository *repo, const char *path,
1383 int *searching, int *search_next_done, regex_t *regex)
1385 const struct got_error *err = NULL;
1386 int nqueued = 0, have_match = 0;
1389 * We keep all commits open throughout the lifetime of the log
1390 * view in order to avoid having to re-fetch commits from disk
1391 * while updating the display.
1393 while (nqueued < minqueue ||
1394 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 struct got_object_id *id;
1396 struct got_commit_object *commit;
1397 struct commit_queue_entry *entry;
1400 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 if (err || id == NULL)
1404 err = got_object_open_as_commit(&commit, repo, id);
1407 entry = alloc_commit_queue_entry(commit, id);
1408 if (entry == NULL) {
1409 err = got_error_from_errno("alloc_commit_queue_entry");
1413 errcode = pthread_mutex_lock(&tog_mutex);
1415 err = got_error_set_errno(errcode,
1416 "pthread_mutex_lock");
1420 entry->idx = commits->ncommits;
1421 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1423 commits->ncommits++;
1425 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 err = match_commit(&have_match, id, commit, regex);
1429 errcode = pthread_mutex_unlock(&tog_mutex);
1430 if (errcode && err == NULL)
1431 err = got_error_set_errno(errcode,
1432 "pthread_mutex_unlock");
1434 if (err || have_match)
1441 static const struct got_error *
1442 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1443 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1444 struct commit_queue *commits, int selected_idx, int limit,
1445 struct got_reflist_head *refs, const char *path, int commits_needed,
1446 struct tog_colors *colors)
1448 const struct got_error *err = NULL;
1449 struct tog_log_view_state *s = &view->state.log;
1450 struct commit_queue_entry *entry;
1452 int ncommits, author_cols = 4;
1453 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1454 char *refs_str = NULL;
1456 struct tog_color *tc;
1457 static const size_t date_display_cols = 12;
1462 if (ncommits == selected_idx) {
1466 entry = TAILQ_NEXT(entry, entry);
1470 if (*selected && !(view->searching && view->search_next_done == 0)) {
1471 err = got_object_id_str(&id_str, (*selected)->id);
1475 err = build_refs_str(&refs_str, refs, (*selected)->id,
1482 if (commits_needed == 0)
1483 halfdelay(10); /* disable fast refresh */
1485 if (asprintf(&ncommits_str, " [%d/%d] %s",
1486 entry ? entry->idx + 1 : 0, commits->ncommits,
1487 commits_needed > 0 ?
1488 (view->searching && view->search_next_done == 0
1489 ? "searching..." : "loading... ") :
1490 (refs_str ? refs_str : "")) == -1) {
1491 err = got_error_from_errno("asprintf");
1495 if (path && strcmp(path, "/") != 0) {
1496 if (asprintf(&header, "commit %s %s%s",
1497 id_str ? id_str : "........................................",
1498 path, ncommits_str) == -1) {
1499 err = got_error_from_errno("asprintf");
1503 } else if (asprintf(&header, "commit %s%s",
1504 id_str ? id_str : "........................................",
1505 ncommits_str) == -1) {
1506 err = got_error_from_errno("asprintf");
1510 err = format_line(&wline, &width, header, view->ncols, 0);
1514 werase(view->window);
1516 if (view_needs_focus_indication(view))
1517 wstandout(view->window);
1518 tc = get_color(colors, TOG_COLOR_COMMIT);
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddwstr(view->window, wline);
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 while (width < view->ncols) {
1527 waddch(view->window, ' ');
1530 if (view_needs_focus_indication(view))
1531 wstandend(view->window);
1536 /* Grow author column size if necessary. */
1543 if (ncommits >= limit - 1)
1545 author = strdup(got_object_commit_get_author(entry->commit));
1546 if (author == NULL) {
1547 err = got_error_from_errno("strdup");
1550 err = format_author(&wauthor, &width, author, COLS,
1552 if (author_cols < width)
1553 author_cols = width;
1557 entry = TAILQ_NEXT(entry, entry);
1564 if (ncommits >= limit - 1)
1566 if (ncommits == selected_idx)
1567 wstandout(view->window);
1568 err = draw_commit(view, entry->commit, entry->id, refs,
1569 date_display_cols, author_cols, colors);
1570 if (ncommits == selected_idx)
1571 wstandend(view->window);
1576 entry = TAILQ_NEXT(entry, entry);
1589 scroll_up(struct tog_view *view,
1590 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1591 struct commit_queue *commits)
1593 struct commit_queue_entry *entry;
1596 entry = TAILQ_FIRST(&commits->head);
1597 if (*first_displayed_entry == entry)
1600 entry = *first_displayed_entry;
1601 while (entry && nscrolled < maxscroll) {
1602 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1604 *first_displayed_entry = entry;
1610 static const struct got_error *
1611 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1612 pthread_cond_t *need_commits)
1617 halfdelay(1); /* fast refresh while loading commits */
1619 while (*commits_needed > 0) {
1623 /* Wake the log thread. */
1624 errcode = pthread_cond_signal(need_commits);
1626 return got_error_set_errno(errcode,
1627 "pthread_cond_signal");
1628 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1630 * Thread is not done yet; lose a key press
1631 * and let the user retry... this way the GUI
1632 * remains interactive while logging deep paths
1633 * with few commits in history.
1642 static const struct got_error *
1643 scroll_down(struct tog_view *view,
1644 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1645 struct commit_queue_entry **last_displayed_entry,
1646 struct commit_queue *commits, int *log_complete, int *commits_needed,
1647 pthread_cond_t *need_commits)
1649 const struct got_error *err = NULL;
1650 struct commit_queue_entry *pentry;
1653 if (*last_displayed_entry == NULL)
1656 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1657 if (pentry == NULL && !*log_complete) {
1659 * Ask the log thread for required amount of commits
1660 * plus some amount of pre-fetching.
1662 (*commits_needed) += maxscroll + 20;
1663 err = trigger_log_thread(0, commits_needed, log_complete,
1670 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1674 *last_displayed_entry = pentry;
1676 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1679 *first_displayed_entry = pentry;
1680 } while (++nscrolled < maxscroll);
1685 static const struct got_error *
1686 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1687 struct got_commit_object *commit, struct got_object_id *commit_id,
1688 struct tog_view *log_view, struct got_reflist_head *refs,
1689 struct got_repository *repo)
1691 const struct got_error *err;
1692 struct got_object_qid *parent_id;
1693 struct tog_view *diff_view;
1695 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1696 if (diff_view == NULL)
1697 return got_error_from_errno("view_open");
1699 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1700 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1701 commit_id, log_view, refs, repo);
1703 *new_view = diff_view;
1707 static const struct got_error *
1708 tree_view_visit_subtree(struct got_tree_object *subtree,
1709 struct tog_tree_view_state *s)
1711 struct tog_parent_tree *parent;
1713 parent = calloc(1, sizeof(*parent));
1715 return got_error_from_errno("calloc");
1717 parent->tree = s->tree;
1718 parent->first_displayed_entry = s->first_displayed_entry;
1719 parent->selected_entry = s->selected_entry;
1720 parent->selected = s->selected;
1721 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1724 s->first_displayed_entry = NULL;
1729 static const struct got_error *
1730 browse_commit_tree(struct tog_view **new_view, int begin_x,
1731 struct commit_queue_entry *entry, const char *path,
1732 struct got_reflist_head *refs, struct got_repository *repo)
1734 const struct got_error *err = NULL;
1735 struct got_tree_object *tree;
1736 struct tog_tree_view_state *s;
1737 struct tog_view *tree_view;
1738 char *slash, *subpath = NULL;
1741 err = got_object_open_as_tree(&tree, repo,
1742 got_object_commit_get_tree_id(entry->commit));
1746 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1747 if (tree_view == NULL)
1748 return got_error_from_errno("view_open");
1750 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1752 got_object_tree_close(tree);
1755 s = &tree_view->state.tree;
1757 *new_view = tree_view;
1759 if (got_path_is_root_dir(path))
1762 /* Walk the path and open corresponding tree objects. */
1765 struct got_tree_entry *te;
1766 struct got_object_id *tree_id;
1772 /* Ensure the correct subtree entry is selected. */
1773 slash = strchr(p, '/');
1775 te_name = strdup(p);
1777 te_name = strndup(p, slash - p);
1778 if (te_name == NULL) {
1779 err = got_error_from_errno("strndup");
1782 te = got_object_tree_find_entry(s->tree, te_name);
1784 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1789 s->selected_entry = te;
1790 s->selected = got_tree_entry_get_index(te);
1791 if (s->tree != s->root)
1792 s->selected++; /* skip '..' */
1794 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1795 /* Jump to this file's entry. */
1796 s->first_displayed_entry = s->selected_entry;
1801 slash = strchr(p, '/');
1803 subpath = strndup(path, slash - path);
1805 subpath = strdup(path);
1806 if (subpath == NULL) {
1807 err = got_error_from_errno("strdup");
1811 err = got_object_id_by_path(&tree_id, repo, entry->id,
1816 err = got_object_open_as_tree(&tree, repo, tree_id);
1821 err = tree_view_visit_subtree(tree, s);
1823 got_object_tree_close(tree);
1837 static const struct got_error *
1838 block_signals_used_by_main_thread(void)
1843 if (sigemptyset(&sigset) == -1)
1844 return got_error_from_errno("sigemptyset");
1846 /* tog handles SIGWINCH and SIGCONT */
1847 if (sigaddset(&sigset, SIGWINCH) == -1)
1848 return got_error_from_errno("sigaddset");
1849 if (sigaddset(&sigset, SIGCONT) == -1)
1850 return got_error_from_errno("sigaddset");
1852 /* ncurses handles SIGTSTP */
1853 if (sigaddset(&sigset, SIGTSTP) == -1)
1854 return got_error_from_errno("sigaddset");
1856 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1858 return got_error_set_errno(errcode, "pthread_sigmask");
1864 log_thread(void *arg)
1866 const struct got_error *err = NULL;
1868 struct tog_log_thread_args *a = arg;
1871 err = block_signals_used_by_main_thread();
1875 while (!done && !err && !tog_sigpipe_received) {
1876 err = queue_commits(a->graph, a->commits, 1, a->repo,
1877 a->in_repo_path, a->searching, a->search_next_done,
1880 if (err->code != GOT_ERR_ITER_COMPLETED)
1884 } else if (a->commits_needed > 0)
1885 a->commits_needed--;
1887 errcode = pthread_mutex_lock(&tog_mutex);
1889 err = got_error_set_errno(errcode,
1890 "pthread_mutex_lock");
1892 } else if (*a->quit)
1894 else if (*a->first_displayed_entry == NULL) {
1895 *a->first_displayed_entry =
1896 TAILQ_FIRST(&a->commits->head);
1897 *a->selected_entry = *a->first_displayed_entry;
1901 a->commits_needed = 0;
1902 else if (a->commits_needed == 0) {
1903 errcode = pthread_cond_wait(&a->need_commits,
1906 err = got_error_set_errno(errcode,
1907 "pthread_cond_wait");
1910 errcode = pthread_mutex_unlock(&tog_mutex);
1911 if (errcode && err == NULL)
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_unlock");
1915 a->log_complete = 1;
1919 static const struct got_error *
1920 stop_log_thread(struct tog_log_view_state *s)
1922 const struct got_error *err = NULL;
1927 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1929 return got_error_set_errno(errcode,
1930 "pthread_cond_signal");
1931 errcode = pthread_mutex_unlock(&tog_mutex);
1933 return got_error_set_errno(errcode,
1934 "pthread_mutex_unlock");
1935 errcode = pthread_join(s->thread, (void **)&err);
1937 return got_error_set_errno(errcode, "pthread_join");
1938 errcode = pthread_mutex_lock(&tog_mutex);
1940 return got_error_set_errno(errcode,
1941 "pthread_mutex_lock");
1945 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1946 if (errcode && err == NULL)
1947 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1949 if (s->thread_args.repo) {
1950 got_repo_close(s->thread_args.repo);
1951 s->thread_args.repo = NULL;
1954 if (s->thread_args.graph) {
1955 got_commit_graph_close(s->thread_args.graph);
1956 s->thread_args.graph = NULL;
1962 static const struct got_error *
1963 close_log_view(struct tog_view *view)
1965 const struct got_error *err = NULL;
1966 struct tog_log_view_state *s = &view->state.log;
1968 err = stop_log_thread(s);
1969 free_commits(&s->commits);
1970 free(s->in_repo_path);
1971 s->in_repo_path = NULL;
1977 static const struct got_error *
1978 search_start_log_view(struct tog_view *view)
1980 struct tog_log_view_state *s = &view->state.log;
1982 s->matched_entry = NULL;
1983 s->search_entry = NULL;
1987 static const struct got_error *
1988 search_next_log_view(struct tog_view *view)
1990 const struct got_error *err = NULL;
1991 struct tog_log_view_state *s = &view->state.log;
1992 struct commit_queue_entry *entry;
1994 if (!view->searching) {
1995 view->search_next_done = 1;
1999 if (s->search_entry) {
2001 errcode = pthread_mutex_unlock(&tog_mutex);
2003 return got_error_set_errno(errcode,
2004 "pthread_mutex_unlock");
2005 ch = wgetch(view->window);
2006 errcode = pthread_mutex_lock(&tog_mutex);
2008 return got_error_set_errno(errcode,
2009 "pthread_mutex_lock");
2010 if (ch == KEY_BACKSPACE) {
2011 view->search_next_done = 1;
2014 if (view->searching == TOG_SEARCH_FORWARD)
2015 entry = TAILQ_NEXT(s->search_entry, entry);
2017 entry = TAILQ_PREV(s->search_entry,
2018 commit_queue_head, entry);
2019 } else if (s->matched_entry) {
2020 if (view->searching == TOG_SEARCH_FORWARD)
2021 entry = TAILQ_NEXT(s->selected_entry, entry);
2023 entry = TAILQ_PREV(s->selected_entry,
2024 commit_queue_head, entry);
2026 if (view->searching == TOG_SEARCH_FORWARD)
2027 entry = TAILQ_FIRST(&s->commits.head);
2029 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2035 if (entry == NULL) {
2036 if (s->thread_args.log_complete ||
2037 view->searching == TOG_SEARCH_BACKWARD) {
2038 view->search_next_done = 1;
2042 * Poke the log thread for more commits and return,
2043 * allowing the main loop to make progress. Search
2044 * will resume at s->search_entry once we come back.
2046 s->thread_args.commits_needed++;
2047 return trigger_log_thread(1,
2048 &s->thread_args.commits_needed,
2049 &s->thread_args.log_complete,
2050 &s->thread_args.need_commits);
2053 err = match_commit(&have_match, entry->id, entry->commit,
2058 view->search_next_done = 1;
2059 s->matched_entry = entry;
2063 s->search_entry = entry;
2064 if (view->searching == TOG_SEARCH_FORWARD)
2065 entry = TAILQ_NEXT(entry, entry);
2067 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2070 if (s->matched_entry) {
2071 int cur = s->selected_entry->idx;
2072 while (cur < s->matched_entry->idx) {
2073 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2078 while (cur > s->matched_entry->idx) {
2079 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2086 s->search_entry = NULL;
2091 static const struct got_error *
2092 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2093 struct got_reflist_head *refs, struct got_repository *repo,
2094 const char *head_ref_name, const char *path, int check_disk)
2096 const struct got_error *err = NULL;
2097 struct tog_log_view_state *s = &view->state.log;
2098 struct got_repository *thread_repo = NULL;
2099 struct got_commit_graph *thread_graph = NULL;
2102 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2106 /* The commit queue only contains commits being displayed. */
2107 TAILQ_INIT(&s->commits.head);
2108 s->commits.ncommits = 0;
2112 s->head_ref_name = head_ref_name;
2113 s->start_id = got_object_id_dup(start_id);
2114 if (s->start_id == NULL) {
2115 err = got_error_from_errno("got_object_id_dup");
2119 SIMPLEQ_INIT(&s->colors);
2120 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2121 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2122 get_color_value("TOG_COLOR_COMMIT"));
2125 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2126 get_color_value("TOG_COLOR_AUTHOR"));
2128 free_colors(&s->colors);
2131 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2132 get_color_value("TOG_COLOR_DATE"));
2134 free_colors(&s->colors);
2139 view->show = show_log_view;
2140 view->input = input_log_view;
2141 view->close = close_log_view;
2142 view->search_start = search_start_log_view;
2143 view->search_next = search_next_log_view;
2145 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2148 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2151 err = got_commit_graph_iter_start(thread_graph,
2152 s->start_id, s->repo, NULL, NULL);
2156 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2158 err = got_error_set_errno(errcode, "pthread_cond_init");
2162 s->thread_args.commits_needed = view->nlines;
2163 s->thread_args.graph = thread_graph;
2164 s->thread_args.commits = &s->commits;
2165 s->thread_args.in_repo_path = s->in_repo_path;
2166 s->thread_args.start_id = s->start_id;
2167 s->thread_args.repo = thread_repo;
2168 s->thread_args.log_complete = 0;
2169 s->thread_args.quit = &s->quit;
2170 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2171 s->thread_args.selected_entry = &s->selected_entry;
2172 s->thread_args.searching = &view->searching;
2173 s->thread_args.search_next_done = &view->search_next_done;
2174 s->thread_args.regex = &view->regex;
2177 close_log_view(view);
2181 static const struct got_error *
2182 show_log_view(struct tog_view *view)
2184 struct tog_log_view_state *s = &view->state.log;
2186 if (s->thread == NULL) {
2187 int errcode = pthread_create(&s->thread, NULL, log_thread,
2190 return got_error_set_errno(errcode, "pthread_create");
2193 return draw_commits(view, &s->last_displayed_entry,
2194 &s->selected_entry, s->first_displayed_entry,
2195 &s->commits, s->selected, view->nlines, s->refs,
2196 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2199 static const struct got_error *
2200 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 struct tog_view **focus_view, struct tog_view *view, int ch)
2203 const struct got_error *err = NULL;
2204 struct tog_log_view_state *s = &view->state.log;
2205 char *parent_path, *in_repo_path = NULL;
2206 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2208 struct got_object_id *start_id;
2218 if (s->first_displayed_entry == NULL)
2220 if (s->selected > 0)
2223 scroll_up(view, &s->first_displayed_entry, 1,
2228 if (s->first_displayed_entry == NULL)
2230 if (TAILQ_FIRST(&s->commits.head) ==
2231 s->first_displayed_entry) {
2235 scroll_up(view, &s->first_displayed_entry,
2236 view->nlines, &s->commits);
2242 if (s->first_displayed_entry == NULL)
2244 if (s->selected < MIN(view->nlines - 2,
2245 s->commits.ncommits - 1)) {
2249 err = scroll_down(view, &s->first_displayed_entry, 1,
2250 &s->last_displayed_entry, &s->commits,
2251 &s->thread_args.log_complete,
2252 &s->thread_args.commits_needed,
2253 &s->thread_args.need_commits);
2257 struct commit_queue_entry *first;
2258 first = s->first_displayed_entry;
2261 err = scroll_down(view, &s->first_displayed_entry,
2262 view->nlines, &s->last_displayed_entry,
2263 &s->commits, &s->thread_args.log_complete,
2264 &s->thread_args.commits_needed,
2265 &s->thread_args.need_commits);
2268 if (first == s->first_displayed_entry &&
2269 s->selected < MIN(view->nlines - 2,
2270 s->commits.ncommits - 1)) {
2271 /* can't scroll further down */
2272 s->selected = MIN(view->nlines - 2,
2273 s->commits.ncommits - 1);
2279 if (s->selected > view->nlines - 2)
2280 s->selected = view->nlines - 2;
2281 if (s->selected > s->commits.ncommits - 1)
2282 s->selected = s->commits.ncommits - 1;
2287 if (s->selected_entry == NULL)
2289 if (view_is_parent_view(view))
2290 begin_x = view_split_begin_x(view->begin_x);
2291 err = open_diff_view_for_commit(&diff_view, begin_x,
2292 s->selected_entry->commit, s->selected_entry->id,
2293 view, s->refs, s->repo);
2296 if (view_is_parent_view(view)) {
2297 err = view_close_child(view);
2300 err = view_set_child(view, diff_view);
2302 view_close(diff_view);
2305 *focus_view = diff_view;
2306 view->child_focussed = 1;
2308 *new_view = diff_view;
2311 if (s->selected_entry == NULL)
2313 if (view_is_parent_view(view))
2314 begin_x = view_split_begin_x(view->begin_x);
2315 err = browse_commit_tree(&tree_view, begin_x,
2316 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2319 if (view_is_parent_view(view)) {
2320 err = view_close_child(view);
2323 err = view_set_child(view, tree_view);
2325 view_close(tree_view);
2328 *focus_view = tree_view;
2329 view->child_focussed = 1;
2331 *new_view = tree_view;
2334 if (strcmp(s->in_repo_path, "/") == 0)
2336 parent_path = dirname(s->in_repo_path);
2337 if (parent_path && strcmp(parent_path, ".") != 0) {
2338 err = stop_log_thread(s);
2341 lv = view_open(view->nlines, view->ncols,
2342 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2344 return got_error_from_errno(
2346 err = open_log_view(lv, s->start_id, s->refs,
2347 s->repo, s->head_ref_name, parent_path, 0);
2350 if (view_is_parent_view(view))
2353 view_set_child(view->parent, lv);
2360 err = stop_log_thread(s);
2363 lv = view_open(view->nlines, view->ncols,
2364 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2366 return got_error_from_errno("view_open");
2367 err = got_repo_match_object_id(&start_id, NULL,
2368 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2369 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2374 in_repo_path = strdup(s->in_repo_path);
2375 if (in_repo_path == NULL) {
2378 return got_error_from_errno("strdup");
2380 got_ref_list_free(s->refs);
2381 err = got_ref_list(s->refs, s->repo, NULL,
2382 got_ref_cmp_by_name, NULL);
2388 err = open_log_view(lv, start_id, s->refs, s->repo,
2389 s->head_ref_name, in_repo_path, 0);
2405 static const struct got_error *
2406 apply_unveil(const char *repo_path, const char *worktree_path)
2408 const struct got_error *error;
2411 if (unveil("gmon.out", "rwc") != 0)
2412 return got_error_from_errno2("unveil", "gmon.out");
2414 if (repo_path && unveil(repo_path, "r") != 0)
2415 return got_error_from_errno2("unveil", repo_path);
2417 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2418 return got_error_from_errno2("unveil", worktree_path);
2420 if (unveil("/tmp", "rwc") != 0)
2421 return got_error_from_errno2("unveil", "/tmp");
2423 error = got_privsep_unveil_exec_helpers();
2427 if (unveil(NULL, NULL) != 0)
2428 return got_error_from_errno("unveil");
2438 halfdelay(1); /* Do fast refresh while initial view is loading. */
2441 intrflush(stdscr, FALSE);
2442 keypad(stdscr, TRUE);
2444 if (getenv("TOG_COLORS") != NULL) {
2446 use_default_colors();
2448 signal(SIGWINCH, tog_sigwinch);
2449 signal(SIGPIPE, tog_sigpipe);
2450 signal(SIGCONT, tog_sigcont);
2453 static const struct got_error *
2454 cmd_log(int argc, char *argv[])
2456 const struct got_error *error;
2457 struct got_repository *repo = NULL;
2458 struct got_worktree *worktree = NULL;
2459 struct got_reflist_head refs;
2460 struct got_object_id *start_id = NULL;
2461 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2462 char *start_commit = NULL, *head_ref_name = NULL;
2464 struct tog_view *view;
2466 SIMPLEQ_INIT(&refs);
2469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2474 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2477 start_commit = optarg;
2480 repo_path = realpath(optarg, NULL);
2481 if (repo_path == NULL)
2482 return got_error_from_errno2("realpath",
2494 cwd = getcwd(NULL, 0);
2496 error = got_error_from_errno("getcwd");
2499 error = got_worktree_open(&worktree, cwd);
2500 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2507 error = got_error_from_errno("strdup");
2510 } else if (argc == 1) {
2512 error = got_worktree_resolve_path(&path, worktree,
2517 path = strdup(argv[0]);
2519 error = got_error_from_errno("strdup");
2526 if (repo_path == NULL) {
2529 got_worktree_get_repo_path(worktree));
2531 repo_path = strdup(cwd);
2533 if (repo_path == NULL) {
2534 error = got_error_from_errno("strdup");
2540 error = got_repo_open(&repo, repo_path, NULL);
2544 error = apply_unveil(got_repo_get_path(repo),
2545 worktree ? got_worktree_get_root_path(worktree) : NULL);
2549 if (start_commit == NULL)
2550 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2551 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2552 GOT_OBJ_TYPE_COMMIT, 1, repo);
2554 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2555 GOT_OBJ_TYPE_COMMIT, 1, repo);
2559 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2563 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2565 error = got_error_from_errno("view_open");
2569 head_ref_name = strdup(
2570 got_worktree_get_head_ref_name(worktree));
2571 if (head_ref_name == NULL) {
2572 error = got_error_from_errno("strdup");
2576 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2581 /* Release work tree lock. */
2582 got_worktree_close(worktree);
2585 error = view_loop(view);
2591 free(head_ref_name);
2593 got_repo_close(repo);
2595 got_worktree_close(worktree);
2596 got_ref_list_free(&refs);
2604 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2610 parse_next_line(FILE *f, size_t *len)
2615 const char delim[3] = { '\0', '\0', '\0'};
2617 line = fparseln(f, &linelen, &lineno, delim, 0);
2624 match_line(const char *line, regex_t *regex)
2626 regmatch_t regmatch;
2628 return regexec(regex, line, 1, ®match, 0) == 0;
2632 match_color(struct tog_colors *colors, const char *line)
2634 struct tog_color *tc = NULL;
2636 SIMPLEQ_FOREACH(tc, colors, entry) {
2637 if (match_line(line, &tc->regex))
2644 static const struct got_error *
2645 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2646 int *last_displayed_line, int *eof, int max_lines, char *header,
2647 struct tog_colors *colors)
2649 const struct got_error *err;
2650 int nlines = 0, nprinted = 0;
2652 struct tog_color *tc;
2658 werase(view->window);
2661 err = format_line(&wline, &width, header, view->ncols, 0);
2666 if (view_needs_focus_indication(view))
2667 wstandout(view->window);
2668 waddwstr(view->window, wline);
2669 if (view_needs_focus_indication(view))
2670 wstandend(view->window);
2671 if (width <= view->ncols - 1)
2672 waddch(view->window, '\n');
2680 while (nprinted < max_lines) {
2681 line = parse_next_line(f, &len);
2686 if (++nlines < *first_displayed_line) {
2691 err = format_line(&wline, &width, line, view->ncols, 0);
2697 tc = match_color(colors, line);
2699 wattr_on(view->window,
2700 COLOR_PAIR(tc->colorpair), NULL);
2701 waddwstr(view->window, wline);
2703 wattr_off(view->window,
2704 COLOR_PAIR(tc->colorpair), NULL);
2705 if (width <= view->ncols - 1)
2706 waddch(view->window, '\n');
2707 if (++nprinted == 1)
2708 *first_displayed_line = nlines;
2713 *last_displayed_line = nlines;
2718 while (nprinted < view->nlines) {
2719 waddch(view->window, '\n');
2723 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2728 wstandout(view->window);
2729 waddwstr(view->window, wline);
2730 wstandend(view->window);
2737 get_datestr(time_t *time, char *datebuf)
2739 struct tm mytm, *tm;
2742 tm = gmtime_r(time, &mytm);
2745 s = asctime_r(tm, datebuf);
2748 p = strchr(s, '\n');
2754 static const struct got_error *
2755 write_commit_info(struct got_object_id *commit_id,
2756 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2758 const struct got_error *err = NULL;
2759 char datebuf[26], *datestr;
2760 struct got_commit_object *commit;
2761 char *id_str = NULL, *logmsg = NULL;
2762 time_t committer_time;
2763 const char *author, *committer;
2764 char *refs_str = NULL;
2767 err = build_refs_str(&refs_str, refs, commit_id, repo);
2772 err = got_object_open_as_commit(&commit, repo, commit_id);
2776 err = got_object_id_str(&id_str, commit_id);
2778 err = got_error_from_errno("got_object_id_str");
2782 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2783 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2784 err = got_error_from_errno("fprintf");
2787 if (fprintf(outfile, "from: %s\n",
2788 got_object_commit_get_author(commit)) < 0) {
2789 err = got_error_from_errno("fprintf");
2792 committer_time = got_object_commit_get_committer_time(commit);
2793 datestr = get_datestr(&committer_time, datebuf);
2794 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2795 err = got_error_from_errno("fprintf");
2798 author = got_object_commit_get_author(commit);
2799 committer = got_object_commit_get_committer(commit);
2800 if (strcmp(author, committer) != 0 &&
2801 fprintf(outfile, "via: %s\n", committer) < 0) {
2802 err = got_error_from_errno("fprintf");
2805 err = got_object_commit_get_logmsg(&logmsg, commit);
2808 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2809 err = got_error_from_errno("fprintf");
2816 got_object_commit_close(commit);
2820 static const struct got_error *
2821 create_diff(struct tog_diff_view_state *s)
2823 const struct got_error *err = NULL;
2829 err = got_error_from_errno("got_opentemp");
2832 if (s->f && fclose(s->f) != 0) {
2833 err = got_error_from_errno("fclose");
2839 err = got_object_get_type(&obj_type, s->repo, s->id1);
2841 err = got_object_get_type(&obj_type, s->repo, s->id2);
2846 case GOT_OBJ_TYPE_BLOB:
2847 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2848 s->diff_context, 0, s->repo, f);
2850 case GOT_OBJ_TYPE_TREE:
2851 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2852 s->diff_context, 0, s->repo, f);
2854 case GOT_OBJ_TYPE_COMMIT: {
2855 const struct got_object_id_queue *parent_ids;
2856 struct got_object_qid *pid;
2857 struct got_commit_object *commit2;
2859 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2862 /* Show commit info if we're diffing to a parent/root commit. */
2864 write_commit_info(s->id2, s->refs, s->repo, f);
2866 parent_ids = got_object_commit_get_parent_ids(commit2);
2867 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2868 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2869 write_commit_info(s->id2, s->refs,
2875 got_object_commit_close(commit2);
2877 err = got_diff_objects_as_commits(s->id1, s->id2,
2878 s->diff_context, 0, s->repo, f);
2882 err = got_error(GOT_ERR_OBJ_TYPE);
2886 if (f && fflush(f) != 0 && err == NULL)
2887 err = got_error_from_errno("fflush");
2892 diff_view_indicate_progress(struct tog_view *view)
2894 mvwaddstr(view->window, 0, 0, "diffing...");
2899 static const struct got_error *
2900 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2901 struct got_object_id *id2, struct tog_view *log_view,
2902 struct got_reflist_head *refs, struct got_repository *repo)
2904 const struct got_error *err;
2906 if (id1 != NULL && id2 != NULL) {
2908 err = got_object_get_type(&type1, repo, id1);
2911 err = got_object_get_type(&type2, repo, id2);
2916 return got_error(GOT_ERR_OBJ_TYPE);
2920 view->state.diff.id1 = got_object_id_dup(id1);
2921 if (view->state.diff.id1 == NULL)
2922 return got_error_from_errno("got_object_id_dup");
2924 view->state.diff.id1 = NULL;
2926 view->state.diff.id2 = got_object_id_dup(id2);
2927 if (view->state.diff.id2 == NULL) {
2928 free(view->state.diff.id1);
2929 view->state.diff.id1 = NULL;
2930 return got_error_from_errno("got_object_id_dup");
2932 view->state.diff.f = NULL;
2933 view->state.diff.first_displayed_line = 1;
2934 view->state.diff.last_displayed_line = view->nlines;
2935 view->state.diff.diff_context = 3;
2936 view->state.diff.log_view = log_view;
2937 view->state.diff.repo = repo;
2938 view->state.diff.refs = refs;
2939 SIMPLEQ_INIT(&view->state.diff.colors);
2941 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2942 err = add_color(&view->state.diff.colors,
2943 "^-", TOG_COLOR_DIFF_MINUS,
2944 get_color_value("TOG_COLOR_DIFF_MINUS"));
2947 err = add_color(&view->state.diff.colors, "^\\+",
2948 TOG_COLOR_DIFF_PLUS,
2949 get_color_value("TOG_COLOR_DIFF_PLUS"));
2951 free_colors(&view->state.diff.colors);
2954 err = add_color(&view->state.diff.colors,
2955 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2956 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2958 free_colors(&view->state.diff.colors);
2962 err = add_color(&view->state.diff.colors,
2963 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2964 get_color_value("TOG_COLOR_DIFF_META"));
2966 free_colors(&view->state.diff.colors);
2970 err = add_color(&view->state.diff.colors,
2971 "^(from|via): ", TOG_COLOR_AUTHOR,
2972 get_color_value("TOG_COLOR_AUTHOR"));
2974 free_colors(&view->state.diff.colors);
2978 err = add_color(&view->state.diff.colors,
2979 "^date: ", TOG_COLOR_DATE,
2980 get_color_value("TOG_COLOR_DATE"));
2982 free_colors(&view->state.diff.colors);
2987 if (log_view && view_is_splitscreen(view))
2988 show_log_view(log_view); /* draw vborder */
2989 diff_view_indicate_progress(view);
2991 err = create_diff(&view->state.diff);
2993 free(view->state.diff.id1);
2994 view->state.diff.id1 = NULL;
2995 free(view->state.diff.id2);
2996 view->state.diff.id2 = NULL;
3000 view->show = show_diff_view;
3001 view->input = input_diff_view;
3002 view->close = close_diff_view;
3007 static const struct got_error *
3008 close_diff_view(struct tog_view *view)
3010 const struct got_error *err = NULL;
3012 free(view->state.diff.id1);
3013 view->state.diff.id1 = NULL;
3014 free(view->state.diff.id2);
3015 view->state.diff.id2 = NULL;
3016 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3017 err = got_error_from_errno("fclose");
3018 free_colors(&view->state.diff.colors);
3022 static const struct got_error *
3023 show_diff_view(struct tog_view *view)
3025 const struct got_error *err;
3026 struct tog_diff_view_state *s = &view->state.diff;
3027 char *id_str1 = NULL, *id_str2, *header;
3030 err = got_object_id_str(&id_str1, s->id1);
3034 err = got_object_id_str(&id_str2, s->id2);
3038 if (asprintf(&header, "diff %s %s",
3039 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3040 err = got_error_from_errno("asprintf");
3048 return draw_file(view, s->f, &s->first_displayed_line,
3049 &s->last_displayed_line, &s->eof, view->nlines,
3050 header, &s->colors);
3053 static const struct got_error *
3054 set_selected_commit(struct tog_diff_view_state *s,
3055 struct commit_queue_entry *entry)
3057 const struct got_error *err;
3058 const struct got_object_id_queue *parent_ids;
3059 struct got_commit_object *selected_commit;
3060 struct got_object_qid *pid;
3063 s->id2 = got_object_id_dup(entry->id);
3065 return got_error_from_errno("got_object_id_dup");
3067 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3070 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3072 pid = SIMPLEQ_FIRST(parent_ids);
3073 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3074 got_object_commit_close(selected_commit);
3078 static const struct got_error *
3079 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3080 struct tog_view **focus_view, struct tog_view *view, int ch)
3082 const struct got_error *err = NULL;
3083 struct tog_diff_view_state *s = &view->state.diff;
3084 struct tog_log_view_state *ls;
3085 struct commit_queue_entry *entry;
3091 if (s->first_displayed_line > 1)
3092 s->first_displayed_line--;
3096 if (s->first_displayed_line == 1)
3099 while (i++ < view->nlines - 1 &&
3100 s->first_displayed_line > 1)
3101 s->first_displayed_line--;
3106 s->first_displayed_line++;
3114 while (!s->eof && i++ < view->nlines - 1) {
3116 line = parse_next_line(s->f, NULL);
3117 s->first_displayed_line++;
3123 if (s->diff_context > 0) {
3125 diff_view_indicate_progress(view);
3126 err = create_diff(s);
3130 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3132 diff_view_indicate_progress(view);
3133 err = create_diff(s);
3138 if (s->log_view == NULL)
3140 ls = &s->log_view->state.log;
3141 entry = TAILQ_PREV(ls->selected_entry,
3142 commit_queue_head, entry);
3146 err = input_log_view(NULL, NULL, NULL, s->log_view,
3151 err = set_selected_commit(s, entry);
3155 s->first_displayed_line = 1;
3156 s->last_displayed_line = view->nlines;
3158 diff_view_indicate_progress(view);
3159 err = create_diff(s);
3163 if (s->log_view == NULL)
3165 ls = &s->log_view->state.log;
3167 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3168 ls->thread_args.commits_needed++;
3170 /* Display "loading..." in log view. */
3171 show_log_view(s->log_view);
3175 err = trigger_log_thread(1 /* load_all */,
3176 &ls->thread_args.commits_needed,
3177 &ls->thread_args.log_complete,
3178 &ls->thread_args.need_commits);
3182 err = input_log_view(NULL, NULL, NULL, s->log_view,
3187 entry = TAILQ_NEXT(ls->selected_entry, entry);
3191 err = set_selected_commit(s, entry);
3195 s->first_displayed_line = 1;
3196 s->last_displayed_line = view->nlines;
3198 diff_view_indicate_progress(view);
3199 err = create_diff(s);
3208 static const struct got_error *
3209 cmd_diff(int argc, char *argv[])
3211 const struct got_error *error = NULL;
3212 struct got_repository *repo = NULL;
3213 struct got_reflist_head refs;
3214 struct got_object_id *id1 = NULL, *id2 = NULL;
3215 char *repo_path = NULL;
3216 char *id_str1 = NULL, *id_str2 = NULL;
3218 struct tog_view *view;
3220 SIMPLEQ_INIT(&refs);
3223 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3228 while ((ch = getopt(argc, argv, "")) != -1) {
3240 usage_diff(); /* TODO show local worktree changes */
3241 } else if (argc == 2) {
3242 repo_path = getcwd(NULL, 0);
3243 if (repo_path == NULL)
3244 return got_error_from_errno("getcwd");
3247 } else if (argc == 3) {
3248 repo_path = realpath(argv[0], NULL);
3249 if (repo_path == NULL)
3250 return got_error_from_errno2("realpath", argv[0]);
3258 error = got_repo_open(&repo, repo_path, NULL);
3262 error = apply_unveil(got_repo_get_path(repo), NULL);
3266 error = got_repo_match_object_id_prefix(&id1, id_str1,
3267 GOT_OBJ_TYPE_ANY, repo);
3271 error = got_repo_match_object_id_prefix(&id2, id_str2,
3272 GOT_OBJ_TYPE_ANY, repo);
3276 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3280 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3282 error = got_error_from_errno("view_open");
3285 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3288 error = view_loop(view);
3292 got_repo_close(repo);
3293 got_ref_list_free(&refs);
3301 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3306 struct tog_blame_line {
3308 struct got_object_id *id;
3311 static const struct got_error *
3312 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3313 const char *path, struct tog_blame_line *lines, int nlines,
3314 int blame_complete, int selected_line, int *first_displayed_line,
3315 int *last_displayed_line, int *eof, int max_lines,
3316 struct tog_colors *colors)
3318 const struct got_error *err;
3319 int lineno = 0, nprinted = 0;
3324 struct tog_blame_line *blame_line;
3325 struct got_object_id *prev_id = NULL;
3327 struct tog_color *tc;
3329 err = got_object_id_str(&id_str, id);
3334 werase(view->window);
3336 if (asprintf(&line, "commit %s", id_str) == -1) {
3337 err = got_error_from_errno("asprintf");
3342 err = format_line(&wline, &width, line, view->ncols, 0);
3347 if (view_needs_focus_indication(view))
3348 wstandout(view->window);
3349 tc = get_color(colors, TOG_COLOR_COMMIT);
3351 wattr_on(view->window,
3352 COLOR_PAIR(tc->colorpair), NULL);
3353 waddwstr(view->window, wline);
3355 wattr_off(view->window,
3356 COLOR_PAIR(tc->colorpair), NULL);
3357 if (view_needs_focus_indication(view))
3358 wstandend(view->window);
3361 if (width < view->ncols - 1)
3362 waddch(view->window, '\n');
3364 if (asprintf(&line, "[%d/%d] %s%s",
3365 *first_displayed_line - 1 + selected_line, nlines,
3366 blame_complete ? "" : "annotating... ", path) == -1) {
3368 return got_error_from_errno("asprintf");
3371 err = format_line(&wline, &width, line, view->ncols, 0);
3376 waddwstr(view->window, wline);
3379 if (width < view->ncols - 1)
3380 waddch(view->window, '\n');
3383 while (nprinted < max_lines - 2) {
3384 line = parse_next_line(f, &len);
3389 if (++lineno < *first_displayed_line) {
3394 if (view->ncols <= 9) {
3396 wline = wcsdup(L"");
3398 err = got_error_from_errno("wcsdup");
3400 err = format_line(&wline, &width, line,
3401 view->ncols - 9, 9);
3409 if (view->focussed && nprinted == selected_line - 1)
3410 wstandout(view->window);
3413 blame_line = &lines[lineno - 1];
3414 if (blame_line->annotated && prev_id &&
3415 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3417 nprinted == selected_line - 1)) {
3418 waddstr(view->window, " ");
3419 } else if (blame_line->annotated) {
3421 err = got_object_id_str(&id_str, blame_line->id);
3427 tc = get_color(colors, TOG_COLOR_COMMIT);
3429 wattr_on(view->window,
3430 COLOR_PAIR(tc->colorpair), NULL);
3431 wprintw(view->window, "%.8s", id_str);
3433 wattr_off(view->window,
3434 COLOR_PAIR(tc->colorpair), NULL);
3436 prev_id = blame_line->id;
3438 waddstr(view->window, "........");
3442 waddstr(view->window, "........");
3446 if (view->focussed && nprinted == selected_line - 1)
3447 wstandend(view->window);
3448 waddstr(view->window, " ");
3450 waddwstr(view->window, wline);
3451 if (width <= view->ncols - 1)
3452 waddch(view->window, '\n');
3453 if (++nprinted == 1)
3454 *first_displayed_line = lineno;
3459 *last_displayed_line = lineno;
3466 static const struct got_error *
3467 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3469 const struct got_error *err = NULL;
3470 struct tog_blame_cb_args *a = arg;
3471 struct tog_blame_line *line;
3474 if (nlines != a->nlines ||
3475 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3476 return got_error(GOT_ERR_RANGE);
3478 errcode = pthread_mutex_lock(&tog_mutex);
3480 return got_error_set_errno(errcode, "pthread_mutex_lock");
3482 if (*a->quit) { /* user has quit the blame view */
3483 err = got_error(GOT_ERR_ITER_COMPLETED);
3488 goto done; /* no change in this commit */
3490 line = &a->lines[lineno - 1];
3491 if (line->annotated)
3494 line->id = got_object_id_dup(id);
3495 if (line->id == NULL) {
3496 err = got_error_from_errno("got_object_id_dup");
3499 line->annotated = 1;
3501 errcode = pthread_mutex_unlock(&tog_mutex);
3503 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3508 blame_thread(void *arg)
3510 const struct got_error *err;
3511 struct tog_blame_thread_args *ta = arg;
3512 struct tog_blame_cb_args *a = ta->cb_args;
3515 err = block_signals_used_by_main_thread();
3519 err = got_blame(ta->path, a->commit_id, ta->repo,
3520 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3521 if (err && err->code == GOT_ERR_CANCELLED)
3524 errcode = pthread_mutex_lock(&tog_mutex);
3526 return (void *)got_error_set_errno(errcode,
3527 "pthread_mutex_lock");
3529 got_repo_close(ta->repo);
3533 errcode = pthread_mutex_unlock(&tog_mutex);
3534 if (errcode && err == NULL)
3535 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3540 static struct got_object_id *
3541 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3542 int first_displayed_line, int selected_line)
3544 struct tog_blame_line *line;
3549 line = &lines[first_displayed_line - 1 + selected_line - 1];
3550 if (!line->annotated)
3556 static const struct got_error *
3557 stop_blame(struct tog_blame *blame)
3559 const struct got_error *err = NULL;
3562 if (blame->thread) {
3564 errcode = pthread_mutex_unlock(&tog_mutex);
3566 return got_error_set_errno(errcode,
3567 "pthread_mutex_unlock");
3568 errcode = pthread_join(blame->thread, (void **)&err);
3570 return got_error_set_errno(errcode, "pthread_join");
3571 errcode = pthread_mutex_lock(&tog_mutex);
3573 return got_error_set_errno(errcode,
3574 "pthread_mutex_lock");
3575 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3577 blame->thread = NULL;
3579 if (blame->thread_args.repo) {
3580 got_repo_close(blame->thread_args.repo);
3581 blame->thread_args.repo = NULL;
3584 if (fclose(blame->f) != 0 && err == NULL)
3585 err = got_error_from_errno("fclose");
3589 for (i = 0; i < blame->nlines; i++)
3590 free(blame->lines[i].id);
3592 blame->lines = NULL;
3594 free(blame->cb_args.commit_id);
3595 blame->cb_args.commit_id = NULL;
3600 static const struct got_error *
3601 cancel_blame_view(void *arg)
3603 const struct got_error *err = NULL;
3607 errcode = pthread_mutex_lock(&tog_mutex);
3609 return got_error_set_errno(errcode,
3610 "pthread_mutex_unlock");
3613 err = got_error(GOT_ERR_CANCELLED);
3615 errcode = pthread_mutex_unlock(&tog_mutex);
3617 return got_error_set_errno(errcode,
3618 "pthread_mutex_lock");
3623 static const struct got_error *
3624 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3625 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3626 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3627 struct got_repository *repo)
3629 const struct got_error *err = NULL;
3630 struct got_blob_object *blob = NULL;
3631 struct got_repository *thread_repo = NULL;
3632 struct got_object_id *obj_id = NULL;
3635 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3639 err = got_object_get_type(&obj_type, repo, obj_id);
3643 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3644 err = got_error(GOT_ERR_OBJ_TYPE);
3648 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3651 blame->f = got_opentemp();
3652 if (blame->f == NULL) {
3653 err = got_error_from_errno("got_opentemp");
3656 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3657 &blame->line_offsets, blame->f, blob);
3658 if (err || blame->nlines == 0)
3661 /* Don't include \n at EOF in the blame line count. */
3662 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3665 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3666 if (blame->lines == NULL) {
3667 err = got_error_from_errno("calloc");
3671 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3675 blame->cb_args.view = view;
3676 blame->cb_args.lines = blame->lines;
3677 blame->cb_args.nlines = blame->nlines;
3678 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3679 if (blame->cb_args.commit_id == NULL) {
3680 err = got_error_from_errno("got_object_id_dup");
3683 blame->cb_args.quit = done;
3685 blame->thread_args.path = path;
3686 blame->thread_args.repo = thread_repo;
3687 blame->thread_args.cb_args = &blame->cb_args;
3688 blame->thread_args.complete = blame_complete;
3689 blame->thread_args.cancel_cb = cancel_blame_view;
3690 blame->thread_args.cancel_arg = done;
3691 *blame_complete = 0;
3695 got_object_blob_close(blob);
3702 static const struct got_error *
3703 open_blame_view(struct tog_view *view, char *path,
3704 struct got_object_id *commit_id, struct got_reflist_head *refs,
3705 struct got_repository *repo)
3707 const struct got_error *err = NULL;
3708 struct tog_blame_view_state *s = &view->state.blame;
3710 SIMPLEQ_INIT(&s->blamed_commits);
3712 s->path = strdup(path);
3713 if (s->path == NULL)
3714 return got_error_from_errno("strdup");
3716 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3722 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3723 s->first_displayed_line = 1;
3724 s->last_displayed_line = view->nlines;
3725 s->selected_line = 1;
3726 s->blame_complete = 0;
3729 s->commit_id = commit_id;
3730 memset(&s->blame, 0, sizeof(s->blame));
3732 SIMPLEQ_INIT(&s->colors);
3733 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3734 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3735 get_color_value("TOG_COLOR_COMMIT"));
3740 view->show = show_blame_view;
3741 view->input = input_blame_view;
3742 view->close = close_blame_view;
3743 view->search_start = search_start_blame_view;
3744 view->search_next = search_next_blame_view;
3746 return run_blame(&s->blame, view, &s->blame_complete,
3747 &s->first_displayed_line, &s->last_displayed_line,
3748 &s->selected_line, &s->done, &s->eof, s->path,
3749 s->blamed_commit->id, s->repo);
3752 static const struct got_error *
3753 close_blame_view(struct tog_view *view)
3755 const struct got_error *err = NULL;
3756 struct tog_blame_view_state *s = &view->state.blame;
3758 if (s->blame.thread)
3759 err = stop_blame(&s->blame);
3761 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3762 struct got_object_qid *blamed_commit;
3763 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3764 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3765 got_object_qid_free(blamed_commit);
3769 free_colors(&s->colors);
3774 static const struct got_error *
3775 search_start_blame_view(struct tog_view *view)
3777 struct tog_blame_view_state *s = &view->state.blame;
3779 s->matched_line = 0;
3783 static const struct got_error *
3784 search_next_blame_view(struct tog_view *view)
3786 struct tog_blame_view_state *s = &view->state.blame;
3789 if (!view->searching) {
3790 view->search_next_done = 1;
3794 if (s->matched_line) {
3795 if (view->searching == TOG_SEARCH_FORWARD)
3796 lineno = s->matched_line + 1;
3798 lineno = s->matched_line - 1;
3800 if (view->searching == TOG_SEARCH_FORWARD)
3803 lineno = s->blame.nlines;
3811 if (lineno <= 0 || lineno > s->blame.nlines) {
3812 if (s->matched_line == 0) {
3813 view->search_next_done = 1;
3818 if (view->searching == TOG_SEARCH_FORWARD)
3821 lineno = s->blame.nlines;
3824 offset = s->blame.line_offsets[lineno - 1];
3825 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3827 return got_error_from_errno("fseeko");
3830 line = parse_next_line(s->blame.f, &len);
3831 if (line && match_line(line, &view->regex)) {
3832 view->search_next_done = 1;
3833 s->matched_line = lineno;
3838 if (view->searching == TOG_SEARCH_FORWARD)
3844 if (s->matched_line) {
3845 s->first_displayed_line = s->matched_line;
3846 s->selected_line = 1;
3852 static const struct got_error *
3853 show_blame_view(struct tog_view *view)
3855 const struct got_error *err = NULL;
3856 struct tog_blame_view_state *s = &view->state.blame;
3859 if (s->blame.thread == NULL) {
3860 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3861 &s->blame.thread_args);
3863 return got_error_set_errno(errcode, "pthread_create");
3865 halfdelay(1); /* fast refresh while annotating */
3868 if (s->blame_complete)
3869 halfdelay(10); /* disable fast refresh */
3871 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3872 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3873 s->selected_line, &s->first_displayed_line,
3874 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3880 static const struct got_error *
3881 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3882 struct tog_view **focus_view, struct tog_view *view, int ch)
3884 const struct got_error *err = NULL, *thread_err = NULL;
3885 struct tog_view *diff_view;
3886 struct tog_blame_view_state *s = &view->state.blame;
3895 if (s->selected_line > 1)
3897 else if (s->selected_line == 1 &&
3898 s->first_displayed_line > 1)
3899 s->first_displayed_line--;
3902 if (s->first_displayed_line == 1) {
3903 s->selected_line = 1;
3906 if (s->first_displayed_line > view->nlines - 2)
3907 s->first_displayed_line -=
3910 s->first_displayed_line = 1;
3914 if (s->selected_line < view->nlines - 2 &&
3915 s->first_displayed_line +
3916 s->selected_line <= s->blame.nlines)
3918 else if (s->last_displayed_line <
3920 s->first_displayed_line++;
3924 struct got_object_id *id = NULL;
3925 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3926 s->first_displayed_line, s->selected_line);
3930 struct got_commit_object *commit;
3931 struct got_object_qid *pid;
3932 struct got_object_id *blob_id = NULL;
3934 err = got_object_open_as_commit(&commit,
3938 pid = SIMPLEQ_FIRST(
3939 got_object_commit_get_parent_ids(commit));
3941 got_object_commit_close(commit);
3944 /* Check if path history ends here. */
3945 err = got_object_id_by_path(&blob_id, s->repo,
3948 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3950 got_object_commit_close(commit);
3953 err = got_object_get_type(&obj_type, s->repo,
3956 /* Can't blame non-blob type objects. */
3957 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3958 got_object_commit_close(commit);
3961 err = got_object_qid_alloc(&s->blamed_commit,
3963 got_object_commit_close(commit);
3965 if (got_object_id_cmp(id,
3966 s->blamed_commit->id) == 0)
3968 err = got_object_qid_alloc(&s->blamed_commit,
3974 thread_err = stop_blame(&s->blame);
3978 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3979 s->blamed_commit, entry);
3980 err = run_blame(&s->blame, view, &s->blame_complete,
3981 &s->first_displayed_line, &s->last_displayed_line,
3982 &s->selected_line, &s->done, &s->eof,
3983 s->path, s->blamed_commit->id, s->repo);
3989 struct got_object_qid *first;
3990 first = SIMPLEQ_FIRST(&s->blamed_commits);
3991 if (!got_object_id_cmp(first->id, s->commit_id))
3994 thread_err = stop_blame(&s->blame);
3998 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3999 got_object_qid_free(s->blamed_commit);
4001 SIMPLEQ_FIRST(&s->blamed_commits);
4002 err = run_blame(&s->blame, view, &s->blame_complete,
4003 &s->first_displayed_line, &s->last_displayed_line,
4004 &s->selected_line, &s->done, &s->eof, s->path,
4005 s->blamed_commit->id, s->repo);
4012 struct got_object_id *id = NULL;
4013 struct got_object_qid *pid;
4014 struct got_commit_object *commit = NULL;
4015 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4016 s->first_displayed_line, s->selected_line);
4019 err = got_object_open_as_commit(&commit, s->repo, id);
4022 pid = SIMPLEQ_FIRST(
4023 got_object_commit_get_parent_ids(commit));
4024 if (view_is_parent_view(view))
4025 begin_x = view_split_begin_x(view->begin_x);
4026 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4027 if (diff_view == NULL) {
4028 got_object_commit_close(commit);
4029 err = got_error_from_errno("view_open");
4032 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4033 id, NULL, s->refs, s->repo);
4034 got_object_commit_close(commit);
4036 view_close(diff_view);
4039 if (view_is_parent_view(view)) {
4040 err = view_close_child(view);
4043 err = view_set_child(view, diff_view);
4045 view_close(diff_view);
4048 *focus_view = diff_view;
4049 view->child_focussed = 1;
4051 *new_view = diff_view;
4058 if (s->last_displayed_line >= s->blame.nlines &&
4059 s->selected_line >= MIN(s->blame.nlines,
4060 view->nlines - 2)) {
4063 if (s->last_displayed_line >= s->blame.nlines &&
4064 s->selected_line < view->nlines - 2) {
4065 s->selected_line = MIN(s->blame.nlines,
4069 if (s->last_displayed_line + view->nlines - 2
4071 s->first_displayed_line +=
4074 s->first_displayed_line =
4079 if (s->selected_line > view->nlines - 2) {
4080 s->selected_line = MIN(s->blame.nlines,
4087 return thread_err ? thread_err : err;
4090 static const struct got_error *
4091 cmd_blame(int argc, char *argv[])
4093 const struct got_error *error;
4094 struct got_repository *repo = NULL;
4095 struct got_reflist_head refs;
4096 struct got_worktree *worktree = NULL;
4097 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4098 struct got_object_id *commit_id = NULL;
4099 char *commit_id_str = NULL;
4101 struct tog_view *view;
4103 SIMPLEQ_INIT(&refs);
4106 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4111 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4114 commit_id_str = optarg;
4117 repo_path = realpath(optarg, NULL);
4118 if (repo_path == NULL)
4119 return got_error_from_errno2("realpath",
4136 cwd = getcwd(NULL, 0);
4138 error = got_error_from_errno("getcwd");
4141 if (repo_path == NULL) {
4142 error = got_worktree_open(&worktree, cwd);
4143 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4149 strdup(got_worktree_get_repo_path(worktree));
4150 if (repo_path == NULL)
4151 error = got_error_from_errno("strdup");
4155 repo_path = strdup(cwd);
4156 if (repo_path == NULL) {
4157 error = got_error_from_errno("strdup");
4165 error = got_repo_open(&repo, repo_path, NULL);
4169 error = apply_unveil(got_repo_get_path(repo), NULL);
4174 const char *prefix = got_worktree_get_path_prefix(worktree);
4175 char *p, *worktree_subdir = cwd +
4176 strlen(got_worktree_get_root_path(worktree));
4177 if (asprintf(&p, "%s%s%s%s%s",
4178 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4179 worktree_subdir, worktree_subdir[0] ? "/" : "",
4181 error = got_error_from_errno("asprintf");
4184 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4187 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4192 if (commit_id_str == NULL) {
4193 struct got_reference *head_ref;
4194 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4197 error = got_ref_resolve(&commit_id, repo, head_ref);
4198 got_ref_close(head_ref);
4200 error = got_repo_match_object_id(&commit_id, NULL,
4201 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4206 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4210 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4212 error = got_error_from_errno("view_open");
4215 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4219 /* Release work tree lock. */
4220 got_worktree_close(worktree);
4223 error = view_loop(view);
4229 got_worktree_close(worktree);
4231 got_repo_close(repo);
4232 got_ref_list_free(&refs);
4236 static const struct got_error *
4237 draw_tree_entries(struct tog_view *view,
4238 struct got_tree_entry **first_displayed_entry,
4239 struct got_tree_entry **last_displayed_entry,
4240 struct got_tree_entry **selected_entry, int *ndisplayed,
4241 const char *label, int show_ids, const char *parent_path,
4242 struct got_tree_object *tree, int selected, int limit,
4243 int isroot, struct tog_colors *colors)
4245 const struct got_error *err = NULL;
4246 struct got_tree_entry *te;
4248 struct tog_color *tc;
4249 int width, n, i, nentries;
4253 werase(view->window);
4258 err = format_line(&wline, &width, label, view->ncols, 0);
4261 if (view_needs_focus_indication(view))
4262 wstandout(view->window);
4263 tc = get_color(colors, TOG_COLOR_COMMIT);
4265 wattr_on(view->window,
4266 COLOR_PAIR(tc->colorpair), NULL);
4267 waddwstr(view->window, wline);
4269 wattr_off(view->window,
4270 COLOR_PAIR(tc->colorpair), NULL);
4271 if (view_needs_focus_indication(view))
4272 wstandend(view->window);
4275 if (width < view->ncols - 1)
4276 waddch(view->window, '\n');
4279 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4282 waddwstr(view->window, wline);
4285 if (width < view->ncols - 1)
4286 waddch(view->window, '\n');
4289 waddch(view->window, '\n');
4293 if (*first_displayed_entry == NULL) {
4294 te = got_object_tree_get_first_entry(tree);
4295 if (selected == 0) {
4297 wstandout(view->window);
4298 *selected_entry = NULL;
4300 waddstr(view->window, " ..\n"); /* parent directory */
4301 if (selected == 0 && view->focussed)
4302 wstandend(view->window);
4309 te = *first_displayed_entry;
4312 nentries = got_object_tree_get_nentries(tree);
4313 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4314 char *line = NULL, *id_str = NULL;
4315 const char *modestr = "";
4318 te = got_object_tree_get_entry(tree, i);
4319 mode = got_tree_entry_get_mode(te);
4322 err = got_object_id_str(&id_str,
4323 got_tree_entry_get_id(te));
4325 return got_error_from_errno(
4326 "got_object_id_str");
4328 if (got_object_tree_entry_is_submodule(te))
4330 else if (S_ISLNK(mode))
4332 else if (S_ISDIR(mode))
4334 else if (mode & S_IXUSR)
4336 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4337 got_tree_entry_get_name(te), modestr) == -1) {
4339 return got_error_from_errno("asprintf");
4342 err = format_line(&wline, &width, line, view->ncols, 0);
4347 if (n == selected) {
4349 wstandout(view->window);
4350 *selected_entry = te;
4352 tc = match_color(colors, line);
4354 wattr_on(view->window,
4355 COLOR_PAIR(tc->colorpair), NULL);
4356 waddwstr(view->window, wline);
4358 wattr_off(view->window,
4359 COLOR_PAIR(tc->colorpair), NULL);
4360 if (width < view->ncols - 1)
4361 waddch(view->window, '\n');
4362 if (n == selected && view->focussed)
4363 wstandend(view->window);
4369 *last_displayed_entry = te;
4378 tree_scroll_up(struct tog_view *view,
4379 struct got_tree_entry **first_displayed_entry, int maxscroll,
4380 struct got_tree_object *tree, int isroot)
4382 struct got_tree_entry *te;
4385 if (*first_displayed_entry == NULL)
4388 te = got_object_tree_get_entry(tree, 0);
4389 if (*first_displayed_entry == te) {
4391 *first_displayed_entry = NULL;
4396 while (*first_displayed_entry && i < maxscroll) {
4397 *first_displayed_entry = got_tree_entry_get_prev(tree,
4398 *first_displayed_entry);
4401 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4402 *first_displayed_entry = NULL;
4406 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4407 struct got_tree_entry *last_displayed_entry,
4408 struct got_tree_object *tree)
4410 struct got_tree_entry *next, *last;
4413 if (*first_displayed_entry)
4414 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4416 next = got_object_tree_get_first_entry(tree);
4418 last = last_displayed_entry;
4419 while (next && last && n++ < maxscroll) {
4420 last = got_tree_entry_get_next(tree, last);
4422 *first_displayed_entry = next;
4423 next = got_tree_entry_get_next(tree, next);
4429 static const struct got_error *
4430 tree_entry_path(char **path, struct tog_parent_trees *parents,
4431 struct got_tree_entry *te)
4433 const struct got_error *err = NULL;
4434 struct tog_parent_tree *pt;
4435 size_t len = 2; /* for leading slash and NUL */
4437 TAILQ_FOREACH(pt, parents, entry)
4438 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4441 len += strlen(got_tree_entry_get_name(te));
4443 *path = calloc(1, len);
4445 return got_error_from_errno("calloc");
4448 pt = TAILQ_LAST(parents, tog_parent_trees);
4450 const char *name = got_tree_entry_get_name(pt->selected_entry);
4451 if (strlcat(*path, name, len) >= len) {
4452 err = got_error(GOT_ERR_NO_SPACE);
4455 if (strlcat(*path, "/", len) >= len) {
4456 err = got_error(GOT_ERR_NO_SPACE);
4459 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4462 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4463 err = got_error(GOT_ERR_NO_SPACE);
4475 static const struct got_error *
4476 blame_tree_entry(struct tog_view **new_view, int begin_x,
4477 struct got_tree_entry *te, struct tog_parent_trees *parents,
4478 struct got_object_id *commit_id, struct got_reflist_head *refs,
4479 struct got_repository *repo)
4481 const struct got_error *err = NULL;
4483 struct tog_view *blame_view;
4487 err = tree_entry_path(&path, parents, te);
4491 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4492 if (blame_view == NULL) {
4493 err = got_error_from_errno("view_open");
4497 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4499 if (err->code == GOT_ERR_CANCELLED)
4501 view_close(blame_view);
4503 *new_view = blame_view;
4509 static const struct got_error *
4510 log_tree_entry(struct tog_view **new_view, int begin_x,
4511 struct got_tree_entry *te, struct tog_parent_trees *parents,
4512 struct got_object_id *commit_id, struct got_reflist_head *refs,
4513 struct got_repository *repo)
4515 struct tog_view *log_view;
4516 const struct got_error *err = NULL;
4521 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4522 if (log_view == NULL)
4523 return got_error_from_errno("view_open");
4525 err = tree_entry_path(&path, parents, te);
4529 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4531 view_close(log_view);
4533 *new_view = log_view;
4538 static const struct got_error *
4539 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4540 struct got_object_id *commit_id, struct got_reflist_head *refs,
4541 struct got_repository *repo)
4543 const struct got_error *err = NULL;
4544 char *commit_id_str = NULL;
4545 struct tog_tree_view_state *s = &view->state.tree;
4547 TAILQ_INIT(&s->parents);
4549 err = got_object_id_str(&commit_id_str, commit_id);
4553 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4554 err = got_error_from_errno("asprintf");
4558 s->root = s->tree = root;
4559 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4560 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4561 s->commit_id = got_object_id_dup(commit_id);
4562 if (s->commit_id == NULL) {
4563 err = got_error_from_errno("got_object_id_dup");
4569 SIMPLEQ_INIT(&s->colors);
4571 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4572 err = add_color(&s->colors, "\\$$",
4573 TOG_COLOR_TREE_SUBMODULE,
4574 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4577 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4578 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4580 free_colors(&s->colors);
4583 err = add_color(&s->colors, "/$",
4584 TOG_COLOR_TREE_DIRECTORY,
4585 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4587 free_colors(&s->colors);
4591 err = add_color(&s->colors, "\\*$",
4592 TOG_COLOR_TREE_EXECUTABLE,
4593 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4595 free_colors(&s->colors);
4599 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4600 get_color_value("TOG_COLOR_COMMIT"));
4602 free_colors(&s->colors);
4607 view->show = show_tree_view;
4608 view->input = input_tree_view;
4609 view->close = close_tree_view;
4610 view->search_start = search_start_tree_view;
4611 view->search_next = search_next_tree_view;
4613 free(commit_id_str);
4615 free(s->tree_label);
4616 s->tree_label = NULL;
4621 static const struct got_error *
4622 close_tree_view(struct tog_view *view)
4624 struct tog_tree_view_state *s = &view->state.tree;
4626 free_colors(&s->colors);
4627 free(s->tree_label);
4628 s->tree_label = NULL;
4630 s->commit_id = NULL;
4631 while (!TAILQ_EMPTY(&s->parents)) {
4632 struct tog_parent_tree *parent;
4633 parent = TAILQ_FIRST(&s->parents);
4634 TAILQ_REMOVE(&s->parents, parent, entry);
4638 if (s->tree != s->root)
4639 got_object_tree_close(s->tree);
4640 got_object_tree_close(s->root);
4645 static const struct got_error *
4646 search_start_tree_view(struct tog_view *view)
4648 struct tog_tree_view_state *s = &view->state.tree;
4650 s->matched_entry = NULL;
4655 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4657 regmatch_t regmatch;
4659 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4663 static const struct got_error *
4664 search_next_tree_view(struct tog_view *view)
4666 struct tog_tree_view_state *s = &view->state.tree;
4667 struct got_tree_entry *te = NULL;
4669 if (!view->searching) {
4670 view->search_next_done = 1;
4674 if (s->matched_entry) {
4675 if (view->searching == TOG_SEARCH_FORWARD) {
4676 if (s->selected_entry)
4677 te = got_tree_entry_get_next(s->tree,
4680 te = got_object_tree_get_first_entry(s->tree);
4682 if (s->selected_entry == NULL)
4683 te = got_object_tree_get_last_entry(s->tree);
4685 te = got_tree_entry_get_prev(s->tree,
4689 if (view->searching == TOG_SEARCH_FORWARD)
4690 te = got_object_tree_get_first_entry(s->tree);
4692 te = got_object_tree_get_last_entry(s->tree);
4697 if (s->matched_entry == NULL) {
4698 view->search_next_done = 1;
4701 if (view->searching == TOG_SEARCH_FORWARD)
4702 te = got_object_tree_get_first_entry(s->tree);
4704 te = got_object_tree_get_last_entry(s->tree);
4707 if (match_tree_entry(te, &view->regex)) {
4708 view->search_next_done = 1;
4709 s->matched_entry = te;
4713 if (view->searching == TOG_SEARCH_FORWARD)
4714 te = got_tree_entry_get_next(s->tree, te);
4716 te = got_tree_entry_get_prev(s->tree, te);
4719 if (s->matched_entry) {
4720 s->first_displayed_entry = s->matched_entry;
4727 static const struct got_error *
4728 show_tree_view(struct tog_view *view)
4730 const struct got_error *err = NULL;
4731 struct tog_tree_view_state *s = &view->state.tree;
4734 err = tree_entry_path(&parent_path, &s->parents, NULL);
4738 err = draw_tree_entries(view, &s->first_displayed_entry,
4739 &s->last_displayed_entry, &s->selected_entry,
4740 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4741 s->tree, s->selected, view->nlines, s->tree == s->root,
4749 static const struct got_error *
4750 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4751 struct tog_view **focus_view, struct tog_view *view, int ch)
4753 const struct got_error *err = NULL;
4754 struct tog_tree_view_state *s = &view->state.tree;
4755 struct tog_view *log_view;
4756 int begin_x = 0, nscrolled;
4760 s->show_ids = !s->show_ids;
4763 if (!s->selected_entry)
4765 if (view_is_parent_view(view))
4766 begin_x = view_split_begin_x(view->begin_x);
4767 err = log_tree_entry(&log_view, begin_x,
4768 s->selected_entry, &s->parents,
4769 s->commit_id, s->refs, s->repo);
4770 if (view_is_parent_view(view)) {
4771 err = view_close_child(view);
4774 err = view_set_child(view, log_view);
4776 view_close(log_view);
4779 *focus_view = log_view;
4780 view->child_focussed = 1;
4782 *new_view = log_view;
4786 if (s->selected > 0) {
4788 if (s->selected == 0)
4791 if (s->selected > 0)
4793 tree_scroll_up(view, &s->first_displayed_entry, 1,
4794 s->tree, s->tree == s->root);
4797 tree_scroll_up(view, &s->first_displayed_entry,
4798 MAX(0, view->nlines - 4 - s->selected), s->tree,
4799 s->tree == s->root);
4801 if (got_object_tree_get_first_entry(s->tree) ==
4802 s->first_displayed_entry && s->tree != s->root)
4803 s->first_displayed_entry = NULL;
4807 if (s->selected < s->ndisplayed - 1) {
4811 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4813 /* can't scroll any further */
4815 tree_scroll_down(&s->first_displayed_entry, 1,
4816 s->last_displayed_entry, s->tree);
4819 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4821 /* can't scroll any further; move cursor down */
4822 if (s->selected < s->ndisplayed - 1)
4823 s->selected = s->ndisplayed - 1;
4826 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4827 view->nlines, s->last_displayed_entry, s->tree);
4828 if (nscrolled < view->nlines) {
4830 struct got_tree_entry *te;
4831 te = s->first_displayed_entry;
4834 te = got_tree_entry_get_next(s->tree, te);
4836 s->selected = ndisplayed - 1;
4842 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4843 struct tog_parent_tree *parent;
4844 /* user selected '..' */
4845 if (s->tree == s->root)
4847 parent = TAILQ_FIRST(&s->parents);
4848 TAILQ_REMOVE(&s->parents, parent,
4850 got_object_tree_close(s->tree);
4851 s->tree = parent->tree;
4852 s->first_displayed_entry =
4853 parent->first_displayed_entry;
4855 parent->selected_entry;
4856 s->selected = parent->selected;
4858 } else if (S_ISDIR(got_tree_entry_get_mode(
4859 s->selected_entry))) {
4860 struct got_tree_object *subtree;
4861 err = got_object_open_as_tree(&subtree, s->repo,
4862 got_tree_entry_get_id(s->selected_entry));
4865 err = tree_view_visit_subtree(subtree, s);
4867 got_object_tree_close(subtree);
4870 } else if (S_ISREG(got_tree_entry_get_mode(
4871 s->selected_entry))) {
4872 struct tog_view *blame_view;
4873 int begin_x = view_is_parent_view(view) ?
4874 view_split_begin_x(view->begin_x) : 0;
4876 err = blame_tree_entry(&blame_view, begin_x,
4877 s->selected_entry, &s->parents,
4878 s->commit_id, s->refs, s->repo);
4881 if (view_is_parent_view(view)) {
4882 err = view_close_child(view);
4885 err = view_set_child(view, blame_view);
4887 view_close(blame_view);
4890 *focus_view = blame_view;
4891 view->child_focussed = 1;
4893 *new_view = blame_view;
4897 if (s->selected > view->nlines)
4898 s->selected = s->ndisplayed - 1;
4911 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4916 static const struct got_error *
4917 cmd_tree(int argc, char *argv[])
4919 const struct got_error *error;
4920 struct got_repository *repo = NULL;
4921 struct got_reflist_head refs;
4922 char *repo_path = NULL;
4923 struct got_object_id *commit_id = NULL;
4924 char *commit_id_arg = NULL;
4925 struct got_commit_object *commit = NULL;
4926 struct got_tree_object *tree = NULL;
4928 struct tog_view *view;
4930 SIMPLEQ_INIT(&refs);
4933 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4938 while ((ch = getopt(argc, argv, "c:")) != -1) {
4941 commit_id_arg = optarg;
4953 struct got_worktree *worktree;
4954 char *cwd = getcwd(NULL, 0);
4956 return got_error_from_errno("getcwd");
4957 error = got_worktree_open(&worktree, cwd);
4958 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4963 strdup(got_worktree_get_repo_path(worktree));
4964 got_worktree_close(worktree);
4967 if (repo_path == NULL) {
4968 error = got_error_from_errno("strdup");
4971 } else if (argc == 1) {
4972 repo_path = realpath(argv[0], NULL);
4973 if (repo_path == NULL)
4974 return got_error_from_errno2("realpath", argv[0]);
4980 error = got_repo_open(&repo, repo_path, NULL);
4984 error = apply_unveil(got_repo_get_path(repo), NULL);
4988 error = got_repo_match_object_id(&commit_id, NULL,
4989 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
4990 GOT_OBJ_TYPE_COMMIT, 1, repo);
4994 error = got_object_open_as_commit(&commit, repo, commit_id);
4998 error = got_object_open_as_tree(&tree, repo,
4999 got_object_commit_get_tree_id(commit));
5003 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5007 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5009 error = got_error_from_errno("view_open");
5012 error = open_tree_view(view, tree, commit_id, &refs, repo);
5015 error = view_loop(view);
5020 got_object_commit_close(commit);
5022 got_object_tree_close(tree);
5024 got_repo_close(repo);
5025 got_ref_list_free(&refs);
5034 fprintf(stderr, "commands:");
5035 for (i = 0; i < nitems(tog_commands); i++) {
5036 struct tog_cmd *cmd = &tog_commands[i];
5037 fprintf(stderr, " %s", cmd->name);
5039 fputc('\n', stderr);
5045 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5053 make_argv(const char *arg0, const char *arg1)
5056 int argc = (arg1 == NULL ? 1 : 2);
5058 argv = calloc(argc, sizeof(char *));
5061 argv[0] = strdup(arg0);
5062 if (argv[0] == NULL)
5065 argv[1] = strdup(arg1);
5066 if (argv[1] == NULL)
5074 main(int argc, char *argv[])
5076 const struct got_error *error = NULL;
5077 struct tog_cmd *cmd = NULL;
5078 int ch, hflag = 0, Vflag = 0;
5079 char **cmd_argv = NULL;
5080 static struct option longopts[] = {
5081 { "version", no_argument, NULL, 'V' },
5085 setlocale(LC_CTYPE, "");
5087 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5107 got_version_print_str();
5114 /* Build an argument vector which runs a default command. */
5115 cmd = &tog_commands[0];
5116 cmd_argv = make_argv(cmd->name, NULL);
5121 /* Did the user specific a command? */
5122 for (i = 0; i < nitems(tog_commands); i++) {
5123 if (strncmp(tog_commands[i].name, argv[0],
5124 strlen(argv[0])) == 0) {
5125 cmd = &tog_commands[i];
5131 fprintf(stderr, "%s: unknown command '%s'\n",
5132 getprogname(), argv[0]);
5141 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5145 if (error && error->code != GOT_ERR_CANCELLED)
5146 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);