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 errcode = pthread_mutex_unlock(&tog_mutex);
752 return got_error_set_errno(errcode,
753 "pthread_mutex_unlock");
755 errcode = pthread_mutex_lock(&tog_mutex);
757 return got_error_set_errno(errcode,
758 "pthread_mutex_lock");
759 view->search_next(view);
763 nodelay(stdscr, FALSE);
764 /* Allow threads to make progress while we are waiting for input. */
765 errcode = pthread_mutex_unlock(&tog_mutex);
767 return got_error_set_errno(errcode, "pthread_mutex_unlock");
768 ch = wgetch(view->window);
769 errcode = pthread_mutex_lock(&tog_mutex);
771 return got_error_set_errno(errcode, "pthread_mutex_lock");
772 nodelay(stdscr, TRUE);
774 if (tog_sigwinch_received || tog_sigcont_received) {
776 tog_sigwinch_received = 0;
777 tog_sigcont_received = 0;
778 TAILQ_FOREACH(v, views, entry) {
779 err = view_resize(v);
782 err = v->input(new, dead, focus, v, KEY_RESIZE);
793 *focus = view->child;
794 view->child_focussed = 1;
795 } else if (view->parent) {
796 *focus = view->parent;
797 view->parent->child_focussed = 0;
801 err = view->input(new, dead, focus, view, ch);
808 if (view_is_parent_view(view)) {
809 if (view->child == NULL)
811 if (view_is_splitscreen(view->child)) {
812 *focus = view->child;
813 view->child_focussed = 1;
814 err = view_fullscreen(view->child);
816 err = view_splitscreen(view->child);
819 err = view->child->input(new, dead, focus,
820 view->child, KEY_RESIZE);
822 if (view_is_splitscreen(view)) {
824 view->parent->child_focussed = 1;
825 err = view_fullscreen(view);
827 err = view_splitscreen(view);
831 err = view->input(new, dead, focus, view,
838 if (view->search_start)
839 view_search_start(view);
841 err = view->input(new, dead, focus, view, ch);
845 if (view->search_next && view->searching) {
846 view->searching = (ch == 'n' ?
847 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
848 view->search_next_done = 0;
849 view->search_next(view);
851 err = view->input(new, dead, focus, view, ch);
854 err = view->input(new, dead, focus, view, ch);
862 view_vborder(struct tog_view *view)
865 struct tog_view *view_above;
868 return view_vborder(view->parent);
870 panel = panel_above(view->panel);
874 view_above = panel_userptr(panel);
875 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
876 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
880 view_needs_focus_indication(struct tog_view *view)
882 if (view_is_parent_view(view)) {
883 if (view->child == NULL || view->child_focussed)
885 if (!view_is_splitscreen(view->child))
887 } else if (!view_is_splitscreen(view))
890 return view->focussed;
893 static const struct got_error *
894 view_loop(struct tog_view *view)
896 const struct got_error *err = NULL;
897 struct tog_view_list_head views;
898 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
899 int fast_refresh = 10;
900 int done = 0, errcode;
902 errcode = pthread_mutex_lock(&tog_mutex);
904 return got_error_set_errno(errcode, "pthread_mutex_lock");
907 TAILQ_INSERT_HEAD(&views, view, entry);
911 err = view->show(view);
916 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
917 /* Refresh fast during initialization, then become slower. */
918 if (fast_refresh && fast_refresh-- == 0)
919 halfdelay(10); /* switch to once per second */
921 err = view_input(&new_view, &dead_view, &focus_view, &done,
926 struct tog_view *prev = NULL;
928 if (view_is_parent_view(dead_view))
929 prev = TAILQ_PREV(dead_view,
930 tog_view_list_head, entry);
931 else if (view->parent != dead_view)
934 if (dead_view->parent)
935 dead_view->parent->child = NULL;
937 TAILQ_REMOVE(&views, dead_view, entry);
939 err = view_close(dead_view);
940 if (err || (dead_view == main_view && new_view == NULL))
943 if (view == dead_view) {
948 else if (!TAILQ_EMPTY(&views))
949 view = TAILQ_LAST(&views,
954 if (view->child && view->child_focussed)
955 focus_view = view->child;
962 struct tog_view *v, *t;
963 /* Only allow one parent view per type. */
964 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
965 if (v->type != new_view->type)
967 TAILQ_REMOVE(&views, v, entry);
973 TAILQ_INSERT_TAIL(&views, new_view, entry);
975 if (focus_view == NULL)
976 focus_view = new_view;
979 show_panel(focus_view->panel);
982 focus_view->focussed = 1;
985 show_panel(new_view->panel);
986 if (view->child && view_is_splitscreen(view->child))
987 show_panel(view->child->panel);
990 if (focus_view == NULL) {
992 show_panel(view->panel);
993 if (view->child && view_is_splitscreen(view->child))
994 show_panel(view->child->panel);
998 err = view->parent->show(view->parent);
1002 err = view->show(view);
1006 err = view->child->show(view->child);
1015 while (!TAILQ_EMPTY(&views)) {
1016 view = TAILQ_FIRST(&views);
1017 TAILQ_REMOVE(&views, view, entry);
1021 errcode = pthread_mutex_unlock(&tog_mutex);
1022 if (errcode && err == NULL)
1023 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1033 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1038 /* Create newly allocated wide-character string equivalent to a byte string. */
1039 static const struct got_error *
1040 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1043 const struct got_error *err = NULL;
1046 *wlen = mbstowcs(NULL, s, 0);
1047 if (*wlen == (size_t)-1) {
1049 if (errno != EILSEQ)
1050 return got_error_from_errno("mbstowcs");
1052 /* byte string invalid in current encoding; try to "fix" it */
1053 err = got_mbsavis(&vis, &vislen, s);
1056 *wlen = mbstowcs(NULL, vis, 0);
1057 if (*wlen == (size_t)-1) {
1058 err = got_error_from_errno("mbstowcs"); /* give up */
1063 *ws = calloc(*wlen + 1, sizeof(**ws));
1065 err = got_error_from_errno("calloc");
1069 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1070 err = got_error_from_errno("mbstowcs");
1081 /* Format a line for display, ensuring that it won't overflow a width limit. */
1082 static const struct got_error *
1083 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1086 const struct got_error *err = NULL;
1088 wchar_t *wline = NULL;
1095 err = mbs2ws(&wline, &wlen, line);
1101 int width = wcwidth(wline[i]);
1108 if (width == 1 || width == 2) {
1109 if (cols + width > wlimit)
1113 } else if (width == -1) {
1114 if (wline[i] == L'\t') {
1116 ((cols + col_tab_align) % TABSIZE);
1117 if (cols + width > wlimit)
1123 err = got_error_from_errno("wcwidth");
1138 static const struct got_error*
1139 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1140 struct got_object_id *id, struct got_repository *repo)
1142 static const struct got_error *err = NULL;
1143 struct got_reflist_entry *re;
1149 SIMPLEQ_FOREACH(re, refs, entry) {
1150 struct got_tag_object *tag = NULL;
1153 name = got_ref_get_name(re->ref);
1154 if (strcmp(name, GOT_REF_HEAD) == 0)
1156 if (strncmp(name, "refs/", 5) == 0)
1158 if (strncmp(name, "got/", 4) == 0)
1160 if (strncmp(name, "heads/", 6) == 0)
1162 if (strncmp(name, "remotes/", 8) == 0)
1164 if (strncmp(name, "tags/", 5) == 0) {
1165 err = got_object_open_as_tag(&tag, repo, re->id);
1167 if (err->code != GOT_ERR_OBJ_TYPE)
1169 /* Ref points at something other than a tag. */
1174 cmp = got_object_id_cmp(tag ?
1175 got_object_tag_get_object_id(tag) : re->id, id);
1177 got_object_tag_close(tag);
1181 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1182 s ? ", " : "", name) == -1) {
1183 err = got_error_from_errno("asprintf");
1194 static const struct got_error *
1195 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1198 char *smallerthan, *at;
1200 smallerthan = strchr(author, '<');
1201 if (smallerthan && smallerthan[1] != '\0')
1202 author = smallerthan + 1;
1203 at = strchr(author, '@');
1206 return format_line(wauthor, author_width, author, limit, col_tab_align);
1209 static const struct got_error *
1210 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1211 struct got_object_id *id, struct got_reflist_head *refs,
1212 const size_t date_display_cols, int author_display_cols,
1213 struct tog_colors *colors)
1215 const struct got_error *err = NULL;
1216 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1217 char *logmsg0 = NULL, *logmsg = NULL;
1218 char *author = NULL;
1219 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1220 int author_width, logmsg_width;
1221 char *newline, *line = NULL;
1223 const int avail = view->ncols;
1225 time_t committer_time;
1226 struct tog_color *tc;
1228 committer_time = got_object_commit_get_committer_time(commit);
1229 if (localtime_r(&committer_time, &tm) == NULL)
1230 return got_error_from_errno("localtime_r");
1231 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1233 return got_error(GOT_ERR_NO_SPACE);
1235 if (avail <= date_display_cols)
1236 limit = MIN(sizeof(datebuf) - 1, avail);
1238 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1239 tc = get_color(colors, TOG_COLOR_DATE);
1241 wattr_on(view->window,
1242 COLOR_PAIR(tc->colorpair), NULL);
1243 waddnstr(view->window, datebuf, limit);
1245 wattr_off(view->window,
1246 COLOR_PAIR(tc->colorpair), NULL);
1253 err = got_object_id_str(&id_str, id);
1256 tc = get_color(colors, TOG_COLOR_COMMIT);
1258 wattr_on(view->window,
1259 COLOR_PAIR(tc->colorpair), NULL);
1260 wprintw(view->window, "%.8s ", id_str);
1262 wattr_off(view->window,
1263 COLOR_PAIR(tc->colorpair), NULL);
1270 author = strdup(got_object_commit_get_author(commit));
1271 if (author == NULL) {
1272 err = got_error_from_errno("strdup");
1275 err = format_author(&wauthor, &author_width, author, avail - col, col);
1278 tc = get_color(colors, TOG_COLOR_AUTHOR);
1280 wattr_on(view->window,
1281 COLOR_PAIR(tc->colorpair), NULL);
1282 waddwstr(view->window, wauthor);
1284 wattr_off(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 col += author_width;
1287 while (col < avail && author_width < author_display_cols + 2) {
1288 waddch(view->window, ' ');
1295 err = got_object_commit_get_logmsg(&logmsg0, commit);
1299 while (*logmsg == '\n')
1301 newline = strchr(logmsg, '\n');
1304 limit = avail - col;
1305 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1308 waddwstr(view->window, wlogmsg);
1309 col += logmsg_width;
1310 while (col < avail) {
1311 waddch(view->window, ' ');
1323 static struct commit_queue_entry *
1324 alloc_commit_queue_entry(struct got_commit_object *commit,
1325 struct got_object_id *id)
1327 struct commit_queue_entry *entry;
1329 entry = calloc(1, sizeof(*entry));
1334 entry->commit = commit;
1339 pop_commit(struct commit_queue *commits)
1341 struct commit_queue_entry *entry;
1343 entry = TAILQ_FIRST(&commits->head);
1344 TAILQ_REMOVE(&commits->head, entry, entry);
1345 got_object_commit_close(entry->commit);
1346 commits->ncommits--;
1347 /* Don't free entry->id! It is owned by the commit graph. */
1352 free_commits(struct commit_queue *commits)
1354 while (!TAILQ_EMPTY(&commits->head))
1355 pop_commit(commits);
1358 static const struct got_error *
1359 match_commit(int *have_match, struct got_object_id *id,
1360 struct got_commit_object *commit, regex_t *regex)
1362 const struct got_error *err = NULL;
1363 regmatch_t regmatch;
1364 char *id_str = NULL, *logmsg = NULL;
1368 err = got_object_id_str(&id_str, id);
1372 err = got_object_commit_get_logmsg(&logmsg, commit);
1376 if (regexec(regex, got_object_commit_get_author(commit), 1,
1377 ®match, 0) == 0 ||
1378 regexec(regex, got_object_commit_get_committer(commit), 1,
1379 ®match, 0) == 0 ||
1380 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1381 regexec(regex, logmsg, 1, ®match, 0) == 0)
1389 static const struct got_error *
1390 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1391 int minqueue, struct got_repository *repo, const char *path,
1392 int *searching, int *search_next_done, regex_t *regex)
1394 const struct got_error *err = NULL;
1395 int nqueued = 0, have_match = 0;
1398 * We keep all commits open throughout the lifetime of the log
1399 * view in order to avoid having to re-fetch commits from disk
1400 * while updating the display.
1402 while (nqueued < minqueue ||
1403 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1404 struct got_object_id *id;
1405 struct got_commit_object *commit;
1406 struct commit_queue_entry *entry;
1409 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1410 if (err || id == NULL)
1413 err = got_object_open_as_commit(&commit, repo, id);
1416 entry = alloc_commit_queue_entry(commit, id);
1417 if (entry == NULL) {
1418 err = got_error_from_errno("alloc_commit_queue_entry");
1422 errcode = pthread_mutex_lock(&tog_mutex);
1424 err = got_error_set_errno(errcode,
1425 "pthread_mutex_lock");
1429 entry->idx = commits->ncommits;
1430 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1432 commits->ncommits++;
1434 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1435 err = match_commit(&have_match, id, commit, regex);
1437 pthread_mutex_lock(&tog_mutex);
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode,
1445 "pthread_mutex_unlock");
1454 static const struct got_error *
1455 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 struct commit_queue *commits, int selected_idx, int limit,
1458 struct got_reflist_head *refs, const char *path, int commits_needed,
1459 struct tog_colors *colors)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 struct commit_queue_entry *entry;
1465 int ncommits, author_cols = 4;
1466 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 char *refs_str = NULL;
1469 struct tog_color *tc;
1470 static const size_t date_display_cols = 12;
1475 if (ncommits == selected_idx) {
1479 entry = TAILQ_NEXT(entry, entry);
1483 if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 err = got_object_id_str(&id_str, (*selected)->id);
1488 err = build_refs_str(&refs_str, refs, (*selected)->id,
1495 if (commits_needed == 0)
1496 halfdelay(10); /* disable fast refresh */
1498 if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 entry ? entry->idx + 1 : 0, commits->ncommits,
1500 commits_needed > 0 ?
1501 (view->searching && view->search_next_done == 0
1502 ? "searching..." : "loading... ") :
1503 (refs_str ? refs_str : "")) == -1) {
1504 err = got_error_from_errno("asprintf");
1508 if (path && strcmp(path, "/") != 0) {
1509 if (asprintf(&header, "commit %s %s%s",
1510 id_str ? id_str : "........................................",
1511 path, ncommits_str) == -1) {
1512 err = got_error_from_errno("asprintf");
1516 } else if (asprintf(&header, "commit %s%s",
1517 id_str ? id_str : "........................................",
1518 ncommits_str) == -1) {
1519 err = got_error_from_errno("asprintf");
1523 err = format_line(&wline, &width, header, view->ncols, 0);
1527 werase(view->window);
1529 if (view_needs_focus_indication(view))
1530 wstandout(view->window);
1531 tc = get_color(colors, TOG_COLOR_COMMIT);
1533 wattr_on(view->window,
1534 COLOR_PAIR(tc->colorpair), NULL);
1535 waddwstr(view->window, wline);
1537 wattr_off(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 while (width < view->ncols) {
1540 waddch(view->window, ' ');
1543 if (view_needs_focus_indication(view))
1544 wstandend(view->window);
1549 /* Grow author column size if necessary. */
1556 if (ncommits >= limit - 1)
1558 author = strdup(got_object_commit_get_author(entry->commit));
1559 if (author == NULL) {
1560 err = got_error_from_errno("strdup");
1563 err = format_author(&wauthor, &width, author, COLS,
1565 if (author_cols < width)
1566 author_cols = width;
1570 entry = TAILQ_NEXT(entry, entry);
1577 if (ncommits >= limit - 1)
1579 if (ncommits == selected_idx)
1580 wstandout(view->window);
1581 err = draw_commit(view, entry->commit, entry->id, refs,
1582 date_display_cols, author_cols, colors);
1583 if (ncommits == selected_idx)
1584 wstandend(view->window);
1589 entry = TAILQ_NEXT(entry, entry);
1602 scroll_up(struct tog_view *view,
1603 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1609 entry = TAILQ_FIRST(&commits->head);
1610 if (*first_displayed_entry == entry)
1613 entry = *first_displayed_entry;
1614 while (entry && nscrolled < maxscroll) {
1615 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1617 *first_displayed_entry = entry;
1623 static const struct got_error *
1624 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 pthread_cond_t *need_commits)
1630 halfdelay(1); /* fast refresh while loading commits */
1632 while (*commits_needed > 0) {
1636 /* Wake the log thread. */
1637 errcode = pthread_cond_signal(need_commits);
1639 return got_error_set_errno(errcode,
1640 "pthread_cond_signal");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1643 return got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1646 errcode = pthread_mutex_lock(&tog_mutex);
1648 return got_error_set_errno(errcode,
1649 "pthread_mutex_lock");
1651 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 * Thread is not done yet; lose a key press
1654 * and let the user retry... this way the GUI
1655 * remains interactive while logging deep paths
1656 * with few commits in history.
1665 static const struct got_error *
1666 scroll_down(struct tog_view *view,
1667 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 struct commit_queue_entry **last_displayed_entry,
1669 struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 pthread_cond_t *need_commits)
1672 const struct got_error *err = NULL;
1673 struct commit_queue_entry *pentry;
1676 if (*last_displayed_entry == NULL)
1679 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 if (pentry == NULL && !*log_complete) {
1682 * Ask the log thread for required amount of commits
1683 * plus some amount of pre-fetching.
1685 (*commits_needed) += maxscroll + 20;
1686 err = trigger_log_thread(0, commits_needed, log_complete,
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1697 *last_displayed_entry = pentry;
1699 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1702 *first_displayed_entry = pentry;
1703 } while (++nscrolled < maxscroll);
1708 static const struct got_error *
1709 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 struct got_commit_object *commit, struct got_object_id *commit_id,
1711 struct tog_view *log_view, struct got_reflist_head *refs,
1712 struct got_repository *repo)
1714 const struct got_error *err;
1715 struct got_object_qid *parent_id;
1716 struct tog_view *diff_view;
1718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 if (diff_view == NULL)
1720 return got_error_from_errno("view_open");
1722 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 commit_id, log_view, refs, repo);
1726 *new_view = diff_view;
1730 static const struct got_error *
1731 tree_view_visit_subtree(struct got_tree_object *subtree,
1732 struct tog_tree_view_state *s)
1734 struct tog_parent_tree *parent;
1736 parent = calloc(1, sizeof(*parent));
1738 return got_error_from_errno("calloc");
1740 parent->tree = s->tree;
1741 parent->first_displayed_entry = s->first_displayed_entry;
1742 parent->selected_entry = s->selected_entry;
1743 parent->selected = s->selected;
1744 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1747 s->first_displayed_entry = NULL;
1752 static const struct got_error *
1753 browse_commit_tree(struct tog_view **new_view, int begin_x,
1754 struct commit_queue_entry *entry, const char *path,
1755 struct got_reflist_head *refs, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree;
1759 struct tog_tree_view_state *s;
1760 struct tog_view *tree_view;
1761 char *slash, *subpath = NULL;
1764 err = got_object_open_as_tree(&tree, repo,
1765 got_object_commit_get_tree_id(entry->commit));
1769 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1770 if (tree_view == NULL)
1771 return got_error_from_errno("view_open");
1773 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1775 got_object_tree_close(tree);
1778 s = &tree_view->state.tree;
1780 *new_view = tree_view;
1782 if (got_path_is_root_dir(path))
1785 /* Walk the path and open corresponding tree objects. */
1788 struct got_tree_entry *te;
1789 struct got_object_id *tree_id;
1795 /* Ensure the correct subtree entry is selected. */
1796 slash = strchr(p, '/');
1798 te_name = strdup(p);
1800 te_name = strndup(p, slash - p);
1801 if (te_name == NULL) {
1802 err = got_error_from_errno("strndup");
1805 te = got_object_tree_find_entry(s->tree, te_name);
1807 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1812 s->selected_entry = te;
1813 s->selected = got_tree_entry_get_index(te);
1814 if (s->tree != s->root)
1815 s->selected++; /* skip '..' */
1817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1818 /* Jump to this file's entry. */
1819 s->first_displayed_entry = s->selected_entry;
1824 slash = strchr(p, '/');
1826 subpath = strndup(path, slash - path);
1828 subpath = strdup(path);
1829 if (subpath == NULL) {
1830 err = got_error_from_errno("strdup");
1834 err = got_object_id_by_path(&tree_id, repo, entry->id,
1839 err = got_object_open_as_tree(&tree, repo, tree_id);
1844 err = tree_view_visit_subtree(tree, s);
1846 got_object_tree_close(tree);
1860 static const struct got_error *
1861 block_signals_used_by_main_thread(void)
1866 if (sigemptyset(&sigset) == -1)
1867 return got_error_from_errno("sigemptyset");
1869 /* tog handles SIGWINCH and SIGCONT */
1870 if (sigaddset(&sigset, SIGWINCH) == -1)
1871 return got_error_from_errno("sigaddset");
1872 if (sigaddset(&sigset, SIGCONT) == -1)
1873 return got_error_from_errno("sigaddset");
1875 /* ncurses handles SIGTSTP */
1876 if (sigaddset(&sigset, SIGTSTP) == -1)
1877 return got_error_from_errno("sigaddset");
1879 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1881 return got_error_set_errno(errcode, "pthread_sigmask");
1887 log_thread(void *arg)
1889 const struct got_error *err = NULL;
1891 struct tog_log_thread_args *a = arg;
1894 err = block_signals_used_by_main_thread();
1898 while (!done && !err && !tog_sigpipe_received) {
1899 err = queue_commits(a->graph, a->commits, 1, a->repo,
1900 a->in_repo_path, a->searching, a->search_next_done,
1903 if (err->code != GOT_ERR_ITER_COMPLETED)
1907 } else if (a->commits_needed > 0)
1908 a->commits_needed--;
1910 errcode = pthread_mutex_lock(&tog_mutex);
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_lock");
1915 } else if (*a->quit)
1917 else if (*a->first_displayed_entry == NULL) {
1918 *a->first_displayed_entry =
1919 TAILQ_FIRST(&a->commits->head);
1920 *a->selected_entry = *a->first_displayed_entry;
1924 a->commits_needed = 0;
1925 else if (a->commits_needed == 0) {
1926 errcode = pthread_cond_wait(&a->need_commits,
1929 err = got_error_set_errno(errcode,
1930 "pthread_cond_wait");
1933 errcode = pthread_mutex_unlock(&tog_mutex);
1934 if (errcode && err == NULL)
1935 err = got_error_set_errno(errcode,
1936 "pthread_mutex_unlock");
1938 a->log_complete = 1;
1942 static const struct got_error *
1943 stop_log_thread(struct tog_log_view_state *s)
1945 const struct got_error *err = NULL;
1950 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1952 return got_error_set_errno(errcode,
1953 "pthread_cond_signal");
1954 errcode = pthread_mutex_unlock(&tog_mutex);
1956 return got_error_set_errno(errcode,
1957 "pthread_mutex_unlock");
1958 errcode = pthread_join(s->thread, (void **)&err);
1960 return got_error_set_errno(errcode, "pthread_join");
1961 errcode = pthread_mutex_lock(&tog_mutex);
1963 return got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1968 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1969 if (errcode && err == NULL)
1970 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1972 if (s->thread_args.repo) {
1973 got_repo_close(s->thread_args.repo);
1974 s->thread_args.repo = NULL;
1977 if (s->thread_args.graph) {
1978 got_commit_graph_close(s->thread_args.graph);
1979 s->thread_args.graph = NULL;
1985 static const struct got_error *
1986 close_log_view(struct tog_view *view)
1988 const struct got_error *err = NULL;
1989 struct tog_log_view_state *s = &view->state.log;
1991 err = stop_log_thread(s);
1992 free_commits(&s->commits);
1993 free(s->in_repo_path);
1994 s->in_repo_path = NULL;
2000 static const struct got_error *
2001 search_start_log_view(struct tog_view *view)
2003 struct tog_log_view_state *s = &view->state.log;
2005 s->matched_entry = NULL;
2006 s->search_entry = NULL;
2010 static const struct got_error *
2011 search_next_log_view(struct tog_view *view)
2013 const struct got_error *err = NULL;
2014 struct tog_log_view_state *s = &view->state.log;
2015 struct commit_queue_entry *entry;
2017 if (!view->searching) {
2018 view->search_next_done = 1;
2022 if (s->search_entry) {
2024 errcode = pthread_mutex_unlock(&tog_mutex);
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_unlock");
2028 ch = wgetch(view->window);
2029 errcode = pthread_mutex_lock(&tog_mutex);
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2033 if (ch == KEY_BACKSPACE) {
2034 view->search_next_done = 1;
2037 if (view->searching == TOG_SEARCH_FORWARD)
2038 entry = TAILQ_NEXT(s->search_entry, entry);
2040 entry = TAILQ_PREV(s->search_entry,
2041 commit_queue_head, entry);
2042 } else if (s->matched_entry) {
2043 if (view->searching == TOG_SEARCH_FORWARD)
2044 entry = TAILQ_NEXT(s->selected_entry, entry);
2046 entry = TAILQ_PREV(s->selected_entry,
2047 commit_queue_head, entry);
2049 if (view->searching == TOG_SEARCH_FORWARD)
2050 entry = TAILQ_FIRST(&s->commits.head);
2052 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2058 if (entry == NULL) {
2059 if (s->thread_args.log_complete ||
2060 view->searching == TOG_SEARCH_BACKWARD) {
2061 view->search_next_done = 1;
2065 * Poke the log thread for more commits and return,
2066 * allowing the main loop to make progress. Search
2067 * will resume at s->search_entry once we come back.
2069 s->thread_args.commits_needed++;
2070 return trigger_log_thread(1,
2071 &s->thread_args.commits_needed,
2072 &s->thread_args.log_complete,
2073 &s->thread_args.need_commits);
2076 err = match_commit(&have_match, entry->id, entry->commit,
2081 view->search_next_done = 1;
2082 s->matched_entry = entry;
2086 s->search_entry = entry;
2087 if (view->searching == TOG_SEARCH_FORWARD)
2088 entry = TAILQ_NEXT(entry, entry);
2090 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2093 if (s->matched_entry) {
2094 int cur = s->selected_entry->idx;
2095 while (cur < s->matched_entry->idx) {
2096 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2101 while (cur > s->matched_entry->idx) {
2102 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2109 s->search_entry = NULL;
2114 static const struct got_error *
2115 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2116 struct got_reflist_head *refs, struct got_repository *repo,
2117 const char *head_ref_name, const char *path, int check_disk)
2119 const struct got_error *err = NULL;
2120 struct tog_log_view_state *s = &view->state.log;
2121 struct got_repository *thread_repo = NULL;
2122 struct got_commit_graph *thread_graph = NULL;
2125 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2129 /* The commit queue only contains commits being displayed. */
2130 TAILQ_INIT(&s->commits.head);
2131 s->commits.ncommits = 0;
2135 s->head_ref_name = head_ref_name;
2136 s->start_id = got_object_id_dup(start_id);
2137 if (s->start_id == NULL) {
2138 err = got_error_from_errno("got_object_id_dup");
2142 SIMPLEQ_INIT(&s->colors);
2143 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2144 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2145 get_color_value("TOG_COLOR_COMMIT"));
2148 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2149 get_color_value("TOG_COLOR_AUTHOR"));
2151 free_colors(&s->colors);
2154 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2155 get_color_value("TOG_COLOR_DATE"));
2157 free_colors(&s->colors);
2162 view->show = show_log_view;
2163 view->input = input_log_view;
2164 view->close = close_log_view;
2165 view->search_start = search_start_log_view;
2166 view->search_next = search_next_log_view;
2168 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2171 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2174 err = got_commit_graph_iter_start(thread_graph,
2175 s->start_id, s->repo, NULL, NULL);
2179 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2181 err = got_error_set_errno(errcode, "pthread_cond_init");
2185 s->thread_args.commits_needed = view->nlines;
2186 s->thread_args.graph = thread_graph;
2187 s->thread_args.commits = &s->commits;
2188 s->thread_args.in_repo_path = s->in_repo_path;
2189 s->thread_args.start_id = s->start_id;
2190 s->thread_args.repo = thread_repo;
2191 s->thread_args.log_complete = 0;
2192 s->thread_args.quit = &s->quit;
2193 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2194 s->thread_args.selected_entry = &s->selected_entry;
2195 s->thread_args.searching = &view->searching;
2196 s->thread_args.search_next_done = &view->search_next_done;
2197 s->thread_args.regex = &view->regex;
2200 close_log_view(view);
2204 static const struct got_error *
2205 show_log_view(struct tog_view *view)
2207 struct tog_log_view_state *s = &view->state.log;
2209 if (s->thread == NULL) {
2210 int errcode = pthread_create(&s->thread, NULL, log_thread,
2213 return got_error_set_errno(errcode, "pthread_create");
2216 return draw_commits(view, &s->last_displayed_entry,
2217 &s->selected_entry, s->first_displayed_entry,
2218 &s->commits, s->selected, view->nlines, s->refs,
2219 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2222 static const struct got_error *
2223 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2224 struct tog_view **focus_view, struct tog_view *view, int ch)
2226 const struct got_error *err = NULL;
2227 struct tog_log_view_state *s = &view->state.log;
2228 char *parent_path, *in_repo_path = NULL;
2229 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2231 struct got_object_id *start_id;
2241 if (s->first_displayed_entry == NULL)
2243 if (s->selected > 0)
2246 scroll_up(view, &s->first_displayed_entry, 1,
2251 if (s->first_displayed_entry == NULL)
2253 if (TAILQ_FIRST(&s->commits.head) ==
2254 s->first_displayed_entry) {
2258 scroll_up(view, &s->first_displayed_entry,
2259 view->nlines, &s->commits);
2265 if (s->first_displayed_entry == NULL)
2267 if (s->selected < MIN(view->nlines - 2,
2268 s->commits.ncommits - 1)) {
2272 err = scroll_down(view, &s->first_displayed_entry, 1,
2273 &s->last_displayed_entry, &s->commits,
2274 &s->thread_args.log_complete,
2275 &s->thread_args.commits_needed,
2276 &s->thread_args.need_commits);
2280 struct commit_queue_entry *first;
2281 first = s->first_displayed_entry;
2284 err = scroll_down(view, &s->first_displayed_entry,
2285 view->nlines, &s->last_displayed_entry,
2286 &s->commits, &s->thread_args.log_complete,
2287 &s->thread_args.commits_needed,
2288 &s->thread_args.need_commits);
2291 if (first == s->first_displayed_entry &&
2292 s->selected < MIN(view->nlines - 2,
2293 s->commits.ncommits - 1)) {
2294 /* can't scroll further down */
2295 s->selected = MIN(view->nlines - 2,
2296 s->commits.ncommits - 1);
2302 if (s->selected > view->nlines - 2)
2303 s->selected = view->nlines - 2;
2304 if (s->selected > s->commits.ncommits - 1)
2305 s->selected = s->commits.ncommits - 1;
2310 if (s->selected_entry == NULL)
2312 if (view_is_parent_view(view))
2313 begin_x = view_split_begin_x(view->begin_x);
2314 err = open_diff_view_for_commit(&diff_view, begin_x,
2315 s->selected_entry->commit, s->selected_entry->id,
2316 view, s->refs, s->repo);
2319 if (view_is_parent_view(view)) {
2320 err = view_close_child(view);
2323 err = view_set_child(view, diff_view);
2325 view_close(diff_view);
2328 *focus_view = diff_view;
2329 view->child_focussed = 1;
2331 *new_view = diff_view;
2334 if (s->selected_entry == NULL)
2336 if (view_is_parent_view(view))
2337 begin_x = view_split_begin_x(view->begin_x);
2338 err = browse_commit_tree(&tree_view, begin_x,
2339 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2342 if (view_is_parent_view(view)) {
2343 err = view_close_child(view);
2346 err = view_set_child(view, tree_view);
2348 view_close(tree_view);
2351 *focus_view = tree_view;
2352 view->child_focussed = 1;
2354 *new_view = tree_view;
2357 if (strcmp(s->in_repo_path, "/") == 0)
2359 parent_path = dirname(s->in_repo_path);
2360 if (parent_path && strcmp(parent_path, ".") != 0) {
2361 err = stop_log_thread(s);
2364 lv = view_open(view->nlines, view->ncols,
2365 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2367 return got_error_from_errno(
2369 err = open_log_view(lv, s->start_id, s->refs,
2370 s->repo, s->head_ref_name, parent_path, 0);
2373 if (view_is_parent_view(view))
2376 view_set_child(view->parent, lv);
2383 err = stop_log_thread(s);
2386 lv = view_open(view->nlines, view->ncols,
2387 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2389 return got_error_from_errno("view_open");
2390 err = got_repo_match_object_id(&start_id, NULL,
2391 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2392 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2397 in_repo_path = strdup(s->in_repo_path);
2398 if (in_repo_path == NULL) {
2401 return got_error_from_errno("strdup");
2403 got_ref_list_free(s->refs);
2404 err = got_ref_list(s->refs, s->repo, NULL,
2405 got_ref_cmp_by_name, NULL);
2411 err = open_log_view(lv, start_id, s->refs, s->repo,
2412 s->head_ref_name, in_repo_path, 0);
2428 static const struct got_error *
2429 apply_unveil(const char *repo_path, const char *worktree_path)
2431 const struct got_error *error;
2434 if (unveil("gmon.out", "rwc") != 0)
2435 return got_error_from_errno2("unveil", "gmon.out");
2437 if (repo_path && unveil(repo_path, "r") != 0)
2438 return got_error_from_errno2("unveil", repo_path);
2440 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2441 return got_error_from_errno2("unveil", worktree_path);
2443 if (unveil("/tmp", "rwc") != 0)
2444 return got_error_from_errno2("unveil", "/tmp");
2446 error = got_privsep_unveil_exec_helpers();
2450 if (unveil(NULL, NULL) != 0)
2451 return got_error_from_errno("unveil");
2461 halfdelay(1); /* Do fast refresh while initial view is loading. */
2464 intrflush(stdscr, FALSE);
2465 keypad(stdscr, TRUE);
2467 if (getenv("TOG_COLORS") != NULL) {
2469 use_default_colors();
2471 signal(SIGWINCH, tog_sigwinch);
2472 signal(SIGPIPE, tog_sigpipe);
2473 signal(SIGCONT, tog_sigcont);
2476 static const struct got_error *
2477 cmd_log(int argc, char *argv[])
2479 const struct got_error *error;
2480 struct got_repository *repo = NULL;
2481 struct got_worktree *worktree = NULL;
2482 struct got_reflist_head refs;
2483 struct got_object_id *start_id = NULL;
2484 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2485 char *start_commit = NULL, *head_ref_name = NULL;
2487 struct tog_view *view;
2489 SIMPLEQ_INIT(&refs);
2492 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2497 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2500 start_commit = optarg;
2503 repo_path = realpath(optarg, NULL);
2504 if (repo_path == NULL)
2505 return got_error_from_errno2("realpath",
2517 cwd = getcwd(NULL, 0);
2519 error = got_error_from_errno("getcwd");
2522 error = got_worktree_open(&worktree, cwd);
2523 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2530 error = got_error_from_errno("strdup");
2533 } else if (argc == 1) {
2535 error = got_worktree_resolve_path(&path, worktree,
2540 path = strdup(argv[0]);
2542 error = got_error_from_errno("strdup");
2549 if (repo_path == NULL) {
2552 got_worktree_get_repo_path(worktree));
2554 repo_path = strdup(cwd);
2556 if (repo_path == NULL) {
2557 error = got_error_from_errno("strdup");
2563 error = got_repo_open(&repo, repo_path, NULL);
2567 error = apply_unveil(got_repo_get_path(repo),
2568 worktree ? got_worktree_get_root_path(worktree) : NULL);
2572 if (start_commit == NULL)
2573 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2574 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2575 GOT_OBJ_TYPE_COMMIT, 1, repo);
2577 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2578 GOT_OBJ_TYPE_COMMIT, 1, repo);
2582 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2586 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2588 error = got_error_from_errno("view_open");
2592 head_ref_name = strdup(
2593 got_worktree_get_head_ref_name(worktree));
2594 if (head_ref_name == NULL) {
2595 error = got_error_from_errno("strdup");
2599 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2604 /* Release work tree lock. */
2605 got_worktree_close(worktree);
2608 error = view_loop(view);
2614 free(head_ref_name);
2616 got_repo_close(repo);
2618 got_worktree_close(worktree);
2619 got_ref_list_free(&refs);
2627 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2633 parse_next_line(FILE *f, size_t *len)
2638 const char delim[3] = { '\0', '\0', '\0'};
2640 line = fparseln(f, &linelen, &lineno, delim, 0);
2647 match_line(const char *line, regex_t *regex)
2649 regmatch_t regmatch;
2651 return regexec(regex, line, 1, ®match, 0) == 0;
2655 match_color(struct tog_colors *colors, const char *line)
2657 struct tog_color *tc = NULL;
2659 SIMPLEQ_FOREACH(tc, colors, entry) {
2660 if (match_line(line, &tc->regex))
2667 static const struct got_error *
2668 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2669 int *last_displayed_line, int *eof, int max_lines, char *header,
2670 struct tog_colors *colors)
2672 const struct got_error *err;
2673 int nlines = 0, nprinted = 0;
2675 struct tog_color *tc;
2681 werase(view->window);
2684 err = format_line(&wline, &width, header, view->ncols, 0);
2689 if (view_needs_focus_indication(view))
2690 wstandout(view->window);
2691 waddwstr(view->window, wline);
2692 if (view_needs_focus_indication(view))
2693 wstandend(view->window);
2694 if (width <= view->ncols - 1)
2695 waddch(view->window, '\n');
2703 while (nprinted < max_lines) {
2704 line = parse_next_line(f, &len);
2709 if (++nlines < *first_displayed_line) {
2714 err = format_line(&wline, &width, line, view->ncols, 0);
2720 tc = match_color(colors, line);
2722 wattr_on(view->window,
2723 COLOR_PAIR(tc->colorpair), NULL);
2724 waddwstr(view->window, wline);
2726 wattr_off(view->window,
2727 COLOR_PAIR(tc->colorpair), NULL);
2728 if (width <= view->ncols - 1)
2729 waddch(view->window, '\n');
2730 if (++nprinted == 1)
2731 *first_displayed_line = nlines;
2736 *last_displayed_line = nlines;
2741 while (nprinted < view->nlines) {
2742 waddch(view->window, '\n');
2746 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2751 wstandout(view->window);
2752 waddwstr(view->window, wline);
2753 wstandend(view->window);
2760 get_datestr(time_t *time, char *datebuf)
2762 struct tm mytm, *tm;
2765 tm = gmtime_r(time, &mytm);
2768 s = asctime_r(tm, datebuf);
2771 p = strchr(s, '\n');
2777 static const struct got_error *
2778 write_commit_info(struct got_object_id *commit_id,
2779 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2781 const struct got_error *err = NULL;
2782 char datebuf[26], *datestr;
2783 struct got_commit_object *commit;
2784 char *id_str = NULL, *logmsg = NULL;
2785 time_t committer_time;
2786 const char *author, *committer;
2787 char *refs_str = NULL;
2790 err = build_refs_str(&refs_str, refs, commit_id, repo);
2795 err = got_object_open_as_commit(&commit, repo, commit_id);
2799 err = got_object_id_str(&id_str, commit_id);
2801 err = got_error_from_errno("got_object_id_str");
2805 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2806 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2807 err = got_error_from_errno("fprintf");
2810 if (fprintf(outfile, "from: %s\n",
2811 got_object_commit_get_author(commit)) < 0) {
2812 err = got_error_from_errno("fprintf");
2815 committer_time = got_object_commit_get_committer_time(commit);
2816 datestr = get_datestr(&committer_time, datebuf);
2817 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2818 err = got_error_from_errno("fprintf");
2821 author = got_object_commit_get_author(commit);
2822 committer = got_object_commit_get_committer(commit);
2823 if (strcmp(author, committer) != 0 &&
2824 fprintf(outfile, "via: %s\n", committer) < 0) {
2825 err = got_error_from_errno("fprintf");
2828 err = got_object_commit_get_logmsg(&logmsg, commit);
2831 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2832 err = got_error_from_errno("fprintf");
2839 got_object_commit_close(commit);
2843 static const struct got_error *
2844 create_diff(struct tog_diff_view_state *s)
2846 const struct got_error *err = NULL;
2852 err = got_error_from_errno("got_opentemp");
2855 if (s->f && fclose(s->f) != 0) {
2856 err = got_error_from_errno("fclose");
2862 err = got_object_get_type(&obj_type, s->repo, s->id1);
2864 err = got_object_get_type(&obj_type, s->repo, s->id2);
2869 case GOT_OBJ_TYPE_BLOB:
2870 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2871 s->diff_context, 0, s->repo, f);
2873 case GOT_OBJ_TYPE_TREE:
2874 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2875 s->diff_context, 0, s->repo, f);
2877 case GOT_OBJ_TYPE_COMMIT: {
2878 const struct got_object_id_queue *parent_ids;
2879 struct got_object_qid *pid;
2880 struct got_commit_object *commit2;
2882 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2885 /* Show commit info if we're diffing to a parent/root commit. */
2887 write_commit_info(s->id2, s->refs, s->repo, f);
2889 parent_ids = got_object_commit_get_parent_ids(commit2);
2890 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2891 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2892 write_commit_info(s->id2, s->refs,
2898 got_object_commit_close(commit2);
2900 err = got_diff_objects_as_commits(s->id1, s->id2,
2901 s->diff_context, 0, s->repo, f);
2905 err = got_error(GOT_ERR_OBJ_TYPE);
2909 if (f && fflush(f) != 0 && err == NULL)
2910 err = got_error_from_errno("fflush");
2915 diff_view_indicate_progress(struct tog_view *view)
2917 mvwaddstr(view->window, 0, 0, "diffing...");
2922 static const struct got_error *
2923 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2924 struct got_object_id *id2, struct tog_view *log_view,
2925 struct got_reflist_head *refs, struct got_repository *repo)
2927 const struct got_error *err;
2929 if (id1 != NULL && id2 != NULL) {
2931 err = got_object_get_type(&type1, repo, id1);
2934 err = got_object_get_type(&type2, repo, id2);
2939 return got_error(GOT_ERR_OBJ_TYPE);
2943 view->state.diff.id1 = got_object_id_dup(id1);
2944 if (view->state.diff.id1 == NULL)
2945 return got_error_from_errno("got_object_id_dup");
2947 view->state.diff.id1 = NULL;
2949 view->state.diff.id2 = got_object_id_dup(id2);
2950 if (view->state.diff.id2 == NULL) {
2951 free(view->state.diff.id1);
2952 view->state.diff.id1 = NULL;
2953 return got_error_from_errno("got_object_id_dup");
2955 view->state.diff.f = NULL;
2956 view->state.diff.first_displayed_line = 1;
2957 view->state.diff.last_displayed_line = view->nlines;
2958 view->state.diff.diff_context = 3;
2959 view->state.diff.log_view = log_view;
2960 view->state.diff.repo = repo;
2961 view->state.diff.refs = refs;
2962 SIMPLEQ_INIT(&view->state.diff.colors);
2964 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2965 err = add_color(&view->state.diff.colors,
2966 "^-", TOG_COLOR_DIFF_MINUS,
2967 get_color_value("TOG_COLOR_DIFF_MINUS"));
2970 err = add_color(&view->state.diff.colors, "^\\+",
2971 TOG_COLOR_DIFF_PLUS,
2972 get_color_value("TOG_COLOR_DIFF_PLUS"));
2974 free_colors(&view->state.diff.colors);
2977 err = add_color(&view->state.diff.colors,
2978 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2979 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2981 free_colors(&view->state.diff.colors);
2985 err = add_color(&view->state.diff.colors,
2986 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2987 get_color_value("TOG_COLOR_DIFF_META"));
2989 free_colors(&view->state.diff.colors);
2993 err = add_color(&view->state.diff.colors,
2994 "^(from|via): ", TOG_COLOR_AUTHOR,
2995 get_color_value("TOG_COLOR_AUTHOR"));
2997 free_colors(&view->state.diff.colors);
3001 err = add_color(&view->state.diff.colors,
3002 "^date: ", TOG_COLOR_DATE,
3003 get_color_value("TOG_COLOR_DATE"));
3005 free_colors(&view->state.diff.colors);
3010 if (log_view && view_is_splitscreen(view))
3011 show_log_view(log_view); /* draw vborder */
3012 diff_view_indicate_progress(view);
3014 err = create_diff(&view->state.diff);
3016 free(view->state.diff.id1);
3017 view->state.diff.id1 = NULL;
3018 free(view->state.diff.id2);
3019 view->state.diff.id2 = NULL;
3023 view->show = show_diff_view;
3024 view->input = input_diff_view;
3025 view->close = close_diff_view;
3030 static const struct got_error *
3031 close_diff_view(struct tog_view *view)
3033 const struct got_error *err = NULL;
3035 free(view->state.diff.id1);
3036 view->state.diff.id1 = NULL;
3037 free(view->state.diff.id2);
3038 view->state.diff.id2 = NULL;
3039 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3040 err = got_error_from_errno("fclose");
3041 free_colors(&view->state.diff.colors);
3045 static const struct got_error *
3046 show_diff_view(struct tog_view *view)
3048 const struct got_error *err;
3049 struct tog_diff_view_state *s = &view->state.diff;
3050 char *id_str1 = NULL, *id_str2, *header;
3053 err = got_object_id_str(&id_str1, s->id1);
3057 err = got_object_id_str(&id_str2, s->id2);
3061 if (asprintf(&header, "diff %s %s",
3062 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3063 err = got_error_from_errno("asprintf");
3071 return draw_file(view, s->f, &s->first_displayed_line,
3072 &s->last_displayed_line, &s->eof, view->nlines,
3073 header, &s->colors);
3076 static const struct got_error *
3077 set_selected_commit(struct tog_diff_view_state *s,
3078 struct commit_queue_entry *entry)
3080 const struct got_error *err;
3081 const struct got_object_id_queue *parent_ids;
3082 struct got_commit_object *selected_commit;
3083 struct got_object_qid *pid;
3086 s->id2 = got_object_id_dup(entry->id);
3088 return got_error_from_errno("got_object_id_dup");
3090 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3093 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3095 pid = SIMPLEQ_FIRST(parent_ids);
3096 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3097 got_object_commit_close(selected_commit);
3101 static const struct got_error *
3102 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3103 struct tog_view **focus_view, struct tog_view *view, int ch)
3105 const struct got_error *err = NULL;
3106 struct tog_diff_view_state *s = &view->state.diff;
3107 struct tog_log_view_state *ls;
3108 struct commit_queue_entry *entry;
3114 if (s->first_displayed_line > 1)
3115 s->first_displayed_line--;
3119 if (s->first_displayed_line == 1)
3122 while (i++ < view->nlines - 1 &&
3123 s->first_displayed_line > 1)
3124 s->first_displayed_line--;
3129 s->first_displayed_line++;
3137 while (!s->eof && i++ < view->nlines - 1) {
3139 line = parse_next_line(s->f, NULL);
3140 s->first_displayed_line++;
3146 if (s->diff_context > 0) {
3148 diff_view_indicate_progress(view);
3149 err = create_diff(s);
3153 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3155 diff_view_indicate_progress(view);
3156 err = create_diff(s);
3161 if (s->log_view == NULL)
3163 ls = &s->log_view->state.log;
3164 entry = TAILQ_PREV(ls->selected_entry,
3165 commit_queue_head, entry);
3169 err = input_log_view(NULL, NULL, NULL, s->log_view,
3174 err = set_selected_commit(s, entry);
3178 s->first_displayed_line = 1;
3179 s->last_displayed_line = view->nlines;
3181 diff_view_indicate_progress(view);
3182 err = create_diff(s);
3186 if (s->log_view == NULL)
3188 ls = &s->log_view->state.log;
3190 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3191 ls->thread_args.commits_needed++;
3193 /* Display "loading..." in log view. */
3194 show_log_view(s->log_view);
3198 err = trigger_log_thread(1 /* load_all */,
3199 &ls->thread_args.commits_needed,
3200 &ls->thread_args.log_complete,
3201 &ls->thread_args.need_commits);
3205 err = input_log_view(NULL, NULL, NULL, s->log_view,
3210 entry = TAILQ_NEXT(ls->selected_entry, entry);
3214 err = set_selected_commit(s, entry);
3218 s->first_displayed_line = 1;
3219 s->last_displayed_line = view->nlines;
3221 diff_view_indicate_progress(view);
3222 err = create_diff(s);
3231 static const struct got_error *
3232 cmd_diff(int argc, char *argv[])
3234 const struct got_error *error = NULL;
3235 struct got_repository *repo = NULL;
3236 struct got_reflist_head refs;
3237 struct got_object_id *id1 = NULL, *id2 = NULL;
3238 char *repo_path = NULL;
3239 char *id_str1 = NULL, *id_str2 = NULL;
3241 struct tog_view *view;
3243 SIMPLEQ_INIT(&refs);
3246 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3251 while ((ch = getopt(argc, argv, "")) != -1) {
3263 usage_diff(); /* TODO show local worktree changes */
3264 } else if (argc == 2) {
3265 repo_path = getcwd(NULL, 0);
3266 if (repo_path == NULL)
3267 return got_error_from_errno("getcwd");
3270 } else if (argc == 3) {
3271 repo_path = realpath(argv[0], NULL);
3272 if (repo_path == NULL)
3273 return got_error_from_errno2("realpath", argv[0]);
3281 error = got_repo_open(&repo, repo_path, NULL);
3285 error = apply_unveil(got_repo_get_path(repo), NULL);
3289 error = got_repo_match_object_id_prefix(&id1, id_str1,
3290 GOT_OBJ_TYPE_ANY, repo);
3294 error = got_repo_match_object_id_prefix(&id2, id_str2,
3295 GOT_OBJ_TYPE_ANY, repo);
3299 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3303 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3305 error = got_error_from_errno("view_open");
3308 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3311 error = view_loop(view);
3315 got_repo_close(repo);
3316 got_ref_list_free(&refs);
3324 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3329 struct tog_blame_line {
3331 struct got_object_id *id;
3334 static const struct got_error *
3335 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3336 const char *path, struct tog_blame_line *lines, int nlines,
3337 int blame_complete, int selected_line, int *first_displayed_line,
3338 int *last_displayed_line, int *eof, int max_lines,
3339 struct tog_colors *colors)
3341 const struct got_error *err;
3342 int lineno = 0, nprinted = 0;
3347 struct tog_blame_line *blame_line;
3348 struct got_object_id *prev_id = NULL;
3350 struct tog_color *tc;
3352 err = got_object_id_str(&id_str, id);
3357 werase(view->window);
3359 if (asprintf(&line, "commit %s", id_str) == -1) {
3360 err = got_error_from_errno("asprintf");
3365 err = format_line(&wline, &width, line, view->ncols, 0);
3370 if (view_needs_focus_indication(view))
3371 wstandout(view->window);
3372 tc = get_color(colors, TOG_COLOR_COMMIT);
3374 wattr_on(view->window,
3375 COLOR_PAIR(tc->colorpair), NULL);
3376 waddwstr(view->window, wline);
3378 wattr_off(view->window,
3379 COLOR_PAIR(tc->colorpair), NULL);
3380 if (view_needs_focus_indication(view))
3381 wstandend(view->window);
3384 if (width < view->ncols - 1)
3385 waddch(view->window, '\n');
3387 if (asprintf(&line, "[%d/%d] %s%s",
3388 *first_displayed_line - 1 + selected_line, nlines,
3389 blame_complete ? "" : "annotating... ", path) == -1) {
3391 return got_error_from_errno("asprintf");
3394 err = format_line(&wline, &width, line, view->ncols, 0);
3399 waddwstr(view->window, wline);
3402 if (width < view->ncols - 1)
3403 waddch(view->window, '\n');
3406 while (nprinted < max_lines - 2) {
3407 line = parse_next_line(f, &len);
3412 if (++lineno < *first_displayed_line) {
3417 if (view->ncols <= 9) {
3419 wline = wcsdup(L"");
3421 err = got_error_from_errno("wcsdup");
3423 err = format_line(&wline, &width, line,
3424 view->ncols - 9, 9);
3432 if (view->focussed && nprinted == selected_line - 1)
3433 wstandout(view->window);
3436 blame_line = &lines[lineno - 1];
3437 if (blame_line->annotated && prev_id &&
3438 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3440 nprinted == selected_line - 1)) {
3441 waddstr(view->window, " ");
3442 } else if (blame_line->annotated) {
3444 err = got_object_id_str(&id_str, blame_line->id);
3450 tc = get_color(colors, TOG_COLOR_COMMIT);
3452 wattr_on(view->window,
3453 COLOR_PAIR(tc->colorpair), NULL);
3454 wprintw(view->window, "%.8s", id_str);
3456 wattr_off(view->window,
3457 COLOR_PAIR(tc->colorpair), NULL);
3459 prev_id = blame_line->id;
3461 waddstr(view->window, "........");
3465 waddstr(view->window, "........");
3469 if (view->focussed && nprinted == selected_line - 1)
3470 wstandend(view->window);
3471 waddstr(view->window, " ");
3473 waddwstr(view->window, wline);
3474 if (width <= view->ncols - 1)
3475 waddch(view->window, '\n');
3476 if (++nprinted == 1)
3477 *first_displayed_line = lineno;
3482 *last_displayed_line = lineno;
3489 static const struct got_error *
3490 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3492 const struct got_error *err = NULL;
3493 struct tog_blame_cb_args *a = arg;
3494 struct tog_blame_line *line;
3497 if (nlines != a->nlines ||
3498 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3499 return got_error(GOT_ERR_RANGE);
3501 errcode = pthread_mutex_lock(&tog_mutex);
3503 return got_error_set_errno(errcode, "pthread_mutex_lock");
3505 if (*a->quit) { /* user has quit the blame view */
3506 err = got_error(GOT_ERR_ITER_COMPLETED);
3511 goto done; /* no change in this commit */
3513 line = &a->lines[lineno - 1];
3514 if (line->annotated)
3517 line->id = got_object_id_dup(id);
3518 if (line->id == NULL) {
3519 err = got_error_from_errno("got_object_id_dup");
3522 line->annotated = 1;
3524 errcode = pthread_mutex_unlock(&tog_mutex);
3526 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3531 blame_thread(void *arg)
3533 const struct got_error *err;
3534 struct tog_blame_thread_args *ta = arg;
3535 struct tog_blame_cb_args *a = ta->cb_args;
3538 err = block_signals_used_by_main_thread();
3542 err = got_blame(ta->path, a->commit_id, ta->repo,
3543 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3544 if (err && err->code == GOT_ERR_CANCELLED)
3547 errcode = pthread_mutex_lock(&tog_mutex);
3549 return (void *)got_error_set_errno(errcode,
3550 "pthread_mutex_lock");
3552 got_repo_close(ta->repo);
3556 errcode = pthread_mutex_unlock(&tog_mutex);
3557 if (errcode && err == NULL)
3558 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3563 static struct got_object_id *
3564 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3565 int first_displayed_line, int selected_line)
3567 struct tog_blame_line *line;
3572 line = &lines[first_displayed_line - 1 + selected_line - 1];
3573 if (!line->annotated)
3579 static const struct got_error *
3580 stop_blame(struct tog_blame *blame)
3582 const struct got_error *err = NULL;
3585 if (blame->thread) {
3587 errcode = pthread_mutex_unlock(&tog_mutex);
3589 return got_error_set_errno(errcode,
3590 "pthread_mutex_unlock");
3591 errcode = pthread_join(blame->thread, (void **)&err);
3593 return got_error_set_errno(errcode, "pthread_join");
3594 errcode = pthread_mutex_lock(&tog_mutex);
3596 return got_error_set_errno(errcode,
3597 "pthread_mutex_lock");
3598 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3600 blame->thread = NULL;
3602 if (blame->thread_args.repo) {
3603 got_repo_close(blame->thread_args.repo);
3604 blame->thread_args.repo = NULL;
3607 if (fclose(blame->f) != 0 && err == NULL)
3608 err = got_error_from_errno("fclose");
3612 for (i = 0; i < blame->nlines; i++)
3613 free(blame->lines[i].id);
3615 blame->lines = NULL;
3617 free(blame->cb_args.commit_id);
3618 blame->cb_args.commit_id = NULL;
3623 static const struct got_error *
3624 cancel_blame_view(void *arg)
3626 const struct got_error *err = NULL;
3630 errcode = pthread_mutex_lock(&tog_mutex);
3632 return got_error_set_errno(errcode,
3633 "pthread_mutex_unlock");
3636 err = got_error(GOT_ERR_CANCELLED);
3638 errcode = pthread_mutex_unlock(&tog_mutex);
3640 return got_error_set_errno(errcode,
3641 "pthread_mutex_lock");
3646 static const struct got_error *
3647 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3648 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3649 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3650 struct got_repository *repo)
3652 const struct got_error *err = NULL;
3653 struct got_blob_object *blob = NULL;
3654 struct got_repository *thread_repo = NULL;
3655 struct got_object_id *obj_id = NULL;
3658 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3662 return got_error(GOT_ERR_NO_OBJ);
3664 err = got_object_get_type(&obj_type, repo, obj_id);
3668 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3669 err = got_error(GOT_ERR_OBJ_TYPE);
3673 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3676 blame->f = got_opentemp();
3677 if (blame->f == NULL) {
3678 err = got_error_from_errno("got_opentemp");
3681 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3682 &blame->line_offsets, blame->f, blob);
3683 if (err || blame->nlines == 0)
3686 /* Don't include \n at EOF in the blame line count. */
3687 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3690 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3691 if (blame->lines == NULL) {
3692 err = got_error_from_errno("calloc");
3696 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3700 blame->cb_args.view = view;
3701 blame->cb_args.lines = blame->lines;
3702 blame->cb_args.nlines = blame->nlines;
3703 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3704 if (blame->cb_args.commit_id == NULL) {
3705 err = got_error_from_errno("got_object_id_dup");
3708 blame->cb_args.quit = done;
3710 blame->thread_args.path = path;
3711 blame->thread_args.repo = thread_repo;
3712 blame->thread_args.cb_args = &blame->cb_args;
3713 blame->thread_args.complete = blame_complete;
3714 blame->thread_args.cancel_cb = cancel_blame_view;
3715 blame->thread_args.cancel_arg = done;
3716 *blame_complete = 0;
3720 got_object_blob_close(blob);
3727 static const struct got_error *
3728 open_blame_view(struct tog_view *view, char *path,
3729 struct got_object_id *commit_id, struct got_reflist_head *refs,
3730 struct got_repository *repo)
3732 const struct got_error *err = NULL;
3733 struct tog_blame_view_state *s = &view->state.blame;
3735 SIMPLEQ_INIT(&s->blamed_commits);
3737 s->path = strdup(path);
3738 if (s->path == NULL)
3739 return got_error_from_errno("strdup");
3741 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3747 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3748 s->first_displayed_line = 1;
3749 s->last_displayed_line = view->nlines;
3750 s->selected_line = 1;
3751 s->blame_complete = 0;
3754 s->commit_id = commit_id;
3755 memset(&s->blame, 0, sizeof(s->blame));
3757 SIMPLEQ_INIT(&s->colors);
3758 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3759 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3760 get_color_value("TOG_COLOR_COMMIT"));
3765 view->show = show_blame_view;
3766 view->input = input_blame_view;
3767 view->close = close_blame_view;
3768 view->search_start = search_start_blame_view;
3769 view->search_next = search_next_blame_view;
3771 return run_blame(&s->blame, view, &s->blame_complete,
3772 &s->first_displayed_line, &s->last_displayed_line,
3773 &s->selected_line, &s->done, &s->eof, s->path,
3774 s->blamed_commit->id, s->repo);
3777 static const struct got_error *
3778 close_blame_view(struct tog_view *view)
3780 const struct got_error *err = NULL;
3781 struct tog_blame_view_state *s = &view->state.blame;
3783 if (s->blame.thread)
3784 err = stop_blame(&s->blame);
3786 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3787 struct got_object_qid *blamed_commit;
3788 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3789 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3790 got_object_qid_free(blamed_commit);
3794 free_colors(&s->colors);
3799 static const struct got_error *
3800 search_start_blame_view(struct tog_view *view)
3802 struct tog_blame_view_state *s = &view->state.blame;
3804 s->matched_line = 0;
3808 static const struct got_error *
3809 search_next_blame_view(struct tog_view *view)
3811 struct tog_blame_view_state *s = &view->state.blame;
3814 if (!view->searching) {
3815 view->search_next_done = 1;
3819 if (s->matched_line) {
3820 if (view->searching == TOG_SEARCH_FORWARD)
3821 lineno = s->matched_line + 1;
3823 lineno = s->matched_line - 1;
3825 if (view->searching == TOG_SEARCH_FORWARD)
3828 lineno = s->blame.nlines;
3836 if (lineno <= 0 || lineno > s->blame.nlines) {
3837 if (s->matched_line == 0) {
3838 view->search_next_done = 1;
3843 if (view->searching == TOG_SEARCH_FORWARD)
3846 lineno = s->blame.nlines;
3849 offset = s->blame.line_offsets[lineno - 1];
3850 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3852 return got_error_from_errno("fseeko");
3855 line = parse_next_line(s->blame.f, &len);
3856 if (line && match_line(line, &view->regex)) {
3857 view->search_next_done = 1;
3858 s->matched_line = lineno;
3863 if (view->searching == TOG_SEARCH_FORWARD)
3869 if (s->matched_line) {
3870 s->first_displayed_line = s->matched_line;
3871 s->selected_line = 1;
3877 static const struct got_error *
3878 show_blame_view(struct tog_view *view)
3880 const struct got_error *err = NULL;
3881 struct tog_blame_view_state *s = &view->state.blame;
3884 if (s->blame.thread == NULL) {
3885 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3886 &s->blame.thread_args);
3888 return got_error_set_errno(errcode, "pthread_create");
3890 halfdelay(1); /* fast refresh while annotating */
3893 if (s->blame_complete)
3894 halfdelay(10); /* disable fast refresh */
3896 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3897 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3898 s->selected_line, &s->first_displayed_line,
3899 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3905 static const struct got_error *
3906 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3907 struct tog_view **focus_view, struct tog_view *view, int ch)
3909 const struct got_error *err = NULL, *thread_err = NULL;
3910 struct tog_view *diff_view;
3911 struct tog_blame_view_state *s = &view->state.blame;
3920 if (s->selected_line > 1)
3922 else if (s->selected_line == 1 &&
3923 s->first_displayed_line > 1)
3924 s->first_displayed_line--;
3927 if (s->first_displayed_line == 1) {
3928 s->selected_line = 1;
3931 if (s->first_displayed_line > view->nlines - 2)
3932 s->first_displayed_line -=
3935 s->first_displayed_line = 1;
3939 if (s->selected_line < view->nlines - 2 &&
3940 s->first_displayed_line +
3941 s->selected_line <= s->blame.nlines)
3943 else if (s->last_displayed_line <
3945 s->first_displayed_line++;
3949 struct got_object_id *id = NULL;
3950 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3951 s->first_displayed_line, s->selected_line);
3955 struct got_commit_object *commit;
3956 struct got_object_qid *pid;
3957 struct got_object_id *blob_id = NULL;
3959 err = got_object_open_as_commit(&commit,
3963 pid = SIMPLEQ_FIRST(
3964 got_object_commit_get_parent_ids(commit));
3966 got_object_commit_close(commit);
3969 /* Check if path history ends here. */
3970 err = got_object_id_by_path(&blob_id, s->repo,
3973 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3975 got_object_commit_close(commit);
3978 err = got_object_get_type(&obj_type, s->repo,
3981 /* Can't blame non-blob type objects. */
3982 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3983 got_object_commit_close(commit);
3986 err = got_object_qid_alloc(&s->blamed_commit,
3988 got_object_commit_close(commit);
3990 if (got_object_id_cmp(id,
3991 s->blamed_commit->id) == 0)
3993 err = got_object_qid_alloc(&s->blamed_commit,
3999 thread_err = stop_blame(&s->blame);
4003 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4004 s->blamed_commit, entry);
4005 err = run_blame(&s->blame, view, &s->blame_complete,
4006 &s->first_displayed_line, &s->last_displayed_line,
4007 &s->selected_line, &s->done, &s->eof,
4008 s->path, s->blamed_commit->id, s->repo);
4014 struct got_object_qid *first;
4015 first = SIMPLEQ_FIRST(&s->blamed_commits);
4016 if (!got_object_id_cmp(first->id, s->commit_id))
4019 thread_err = stop_blame(&s->blame);
4023 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4024 got_object_qid_free(s->blamed_commit);
4026 SIMPLEQ_FIRST(&s->blamed_commits);
4027 err = run_blame(&s->blame, view, &s->blame_complete,
4028 &s->first_displayed_line, &s->last_displayed_line,
4029 &s->selected_line, &s->done, &s->eof, s->path,
4030 s->blamed_commit->id, s->repo);
4037 struct got_object_id *id = NULL;
4038 struct got_object_qid *pid;
4039 struct got_commit_object *commit = NULL;
4040 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4041 s->first_displayed_line, s->selected_line);
4044 err = got_object_open_as_commit(&commit, s->repo, id);
4047 pid = SIMPLEQ_FIRST(
4048 got_object_commit_get_parent_ids(commit));
4049 if (view_is_parent_view(view))
4050 begin_x = view_split_begin_x(view->begin_x);
4051 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4052 if (diff_view == NULL) {
4053 got_object_commit_close(commit);
4054 err = got_error_from_errno("view_open");
4057 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4058 id, NULL, s->refs, s->repo);
4059 got_object_commit_close(commit);
4061 view_close(diff_view);
4064 if (view_is_parent_view(view)) {
4065 err = view_close_child(view);
4068 err = view_set_child(view, diff_view);
4070 view_close(diff_view);
4073 *focus_view = diff_view;
4074 view->child_focussed = 1;
4076 *new_view = diff_view;
4083 if (s->last_displayed_line >= s->blame.nlines &&
4084 s->selected_line >= MIN(s->blame.nlines,
4085 view->nlines - 2)) {
4088 if (s->last_displayed_line >= s->blame.nlines &&
4089 s->selected_line < view->nlines - 2) {
4090 s->selected_line = MIN(s->blame.nlines,
4094 if (s->last_displayed_line + view->nlines - 2
4096 s->first_displayed_line +=
4099 s->first_displayed_line =
4104 if (s->selected_line > view->nlines - 2) {
4105 s->selected_line = MIN(s->blame.nlines,
4112 return thread_err ? thread_err : err;
4115 static const struct got_error *
4116 cmd_blame(int argc, char *argv[])
4118 const struct got_error *error;
4119 struct got_repository *repo = NULL;
4120 struct got_reflist_head refs;
4121 struct got_worktree *worktree = NULL;
4122 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4123 struct got_object_id *commit_id = NULL;
4124 char *commit_id_str = NULL;
4126 struct tog_view *view;
4128 SIMPLEQ_INIT(&refs);
4131 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4136 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4139 commit_id_str = optarg;
4142 repo_path = realpath(optarg, NULL);
4143 if (repo_path == NULL)
4144 return got_error_from_errno2("realpath",
4161 cwd = getcwd(NULL, 0);
4163 error = got_error_from_errno("getcwd");
4166 if (repo_path == NULL) {
4167 error = got_worktree_open(&worktree, cwd);
4168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4174 strdup(got_worktree_get_repo_path(worktree));
4175 if (repo_path == NULL)
4176 error = got_error_from_errno("strdup");
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4190 error = got_repo_open(&repo, repo_path, NULL);
4194 error = apply_unveil(got_repo_get_path(repo), NULL);
4199 const char *prefix = got_worktree_get_path_prefix(worktree);
4200 char *p, *worktree_subdir = cwd +
4201 strlen(got_worktree_get_root_path(worktree));
4202 if (asprintf(&p, "%s%s%s%s%s",
4203 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4204 worktree_subdir, worktree_subdir[0] ? "/" : "",
4206 error = got_error_from_errno("asprintf");
4209 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4212 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4217 if (commit_id_str == NULL) {
4218 struct got_reference *head_ref;
4219 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4222 error = got_ref_resolve(&commit_id, repo, head_ref);
4223 got_ref_close(head_ref);
4225 error = got_repo_match_object_id(&commit_id, NULL,
4226 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4231 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4235 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4237 error = got_error_from_errno("view_open");
4240 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4244 /* Release work tree lock. */
4245 got_worktree_close(worktree);
4248 error = view_loop(view);
4254 got_worktree_close(worktree);
4256 got_repo_close(repo);
4257 got_ref_list_free(&refs);
4261 static const struct got_error *
4262 draw_tree_entries(struct tog_view *view,
4263 struct got_tree_entry **first_displayed_entry,
4264 struct got_tree_entry **last_displayed_entry,
4265 struct got_tree_entry **selected_entry, int *ndisplayed,
4266 const char *label, int show_ids, const char *parent_path,
4267 struct got_tree_object *tree, int selected, int limit,
4268 int isroot, struct tog_colors *colors)
4270 const struct got_error *err = NULL;
4271 struct got_tree_entry *te;
4273 struct tog_color *tc;
4274 int width, n, i, nentries;
4278 werase(view->window);
4283 err = format_line(&wline, &width, label, view->ncols, 0);
4286 if (view_needs_focus_indication(view))
4287 wstandout(view->window);
4288 tc = get_color(colors, TOG_COLOR_COMMIT);
4290 wattr_on(view->window,
4291 COLOR_PAIR(tc->colorpair), NULL);
4292 waddwstr(view->window, wline);
4294 wattr_off(view->window,
4295 COLOR_PAIR(tc->colorpair), NULL);
4296 if (view_needs_focus_indication(view))
4297 wstandend(view->window);
4300 if (width < view->ncols - 1)
4301 waddch(view->window, '\n');
4304 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4307 waddwstr(view->window, wline);
4310 if (width < view->ncols - 1)
4311 waddch(view->window, '\n');
4314 waddch(view->window, '\n');
4318 if (*first_displayed_entry == NULL) {
4319 te = got_object_tree_get_first_entry(tree);
4320 if (selected == 0) {
4322 wstandout(view->window);
4323 *selected_entry = NULL;
4325 waddstr(view->window, " ..\n"); /* parent directory */
4326 if (selected == 0 && view->focussed)
4327 wstandend(view->window);
4334 te = *first_displayed_entry;
4337 nentries = got_object_tree_get_nentries(tree);
4338 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4339 char *line = NULL, *id_str = NULL;
4340 const char *modestr = "";
4343 te = got_object_tree_get_entry(tree, i);
4344 mode = got_tree_entry_get_mode(te);
4347 err = got_object_id_str(&id_str,
4348 got_tree_entry_get_id(te));
4350 return got_error_from_errno(
4351 "got_object_id_str");
4353 if (got_object_tree_entry_is_submodule(te))
4355 else if (S_ISLNK(mode))
4357 else if (S_ISDIR(mode))
4359 else if (mode & S_IXUSR)
4361 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4362 got_tree_entry_get_name(te), modestr) == -1) {
4364 return got_error_from_errno("asprintf");
4367 err = format_line(&wline, &width, line, view->ncols, 0);
4372 if (n == selected) {
4374 wstandout(view->window);
4375 *selected_entry = te;
4377 tc = match_color(colors, line);
4379 wattr_on(view->window,
4380 COLOR_PAIR(tc->colorpair), NULL);
4381 waddwstr(view->window, wline);
4383 wattr_off(view->window,
4384 COLOR_PAIR(tc->colorpair), NULL);
4385 if (width < view->ncols - 1)
4386 waddch(view->window, '\n');
4387 if (n == selected && view->focussed)
4388 wstandend(view->window);
4394 *last_displayed_entry = te;
4403 tree_scroll_up(struct tog_view *view,
4404 struct got_tree_entry **first_displayed_entry, int maxscroll,
4405 struct got_tree_object *tree, int isroot)
4407 struct got_tree_entry *te;
4410 if (*first_displayed_entry == NULL)
4413 te = got_object_tree_get_entry(tree, 0);
4414 if (*first_displayed_entry == te) {
4416 *first_displayed_entry = NULL;
4421 while (*first_displayed_entry && i < maxscroll) {
4422 *first_displayed_entry = got_tree_entry_get_prev(tree,
4423 *first_displayed_entry);
4426 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4427 *first_displayed_entry = NULL;
4431 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4432 struct got_tree_entry *last_displayed_entry,
4433 struct got_tree_object *tree)
4435 struct got_tree_entry *next, *last;
4438 if (*first_displayed_entry)
4439 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4441 next = got_object_tree_get_first_entry(tree);
4443 last = last_displayed_entry;
4444 while (next && last && n++ < maxscroll) {
4445 last = got_tree_entry_get_next(tree, last);
4447 *first_displayed_entry = next;
4448 next = got_tree_entry_get_next(tree, next);
4454 static const struct got_error *
4455 tree_entry_path(char **path, struct tog_parent_trees *parents,
4456 struct got_tree_entry *te)
4458 const struct got_error *err = NULL;
4459 struct tog_parent_tree *pt;
4460 size_t len = 2; /* for leading slash and NUL */
4462 TAILQ_FOREACH(pt, parents, entry)
4463 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4466 len += strlen(got_tree_entry_get_name(te));
4468 *path = calloc(1, len);
4470 return got_error_from_errno("calloc");
4473 pt = TAILQ_LAST(parents, tog_parent_trees);
4475 const char *name = got_tree_entry_get_name(pt->selected_entry);
4476 if (strlcat(*path, name, len) >= len) {
4477 err = got_error(GOT_ERR_NO_SPACE);
4480 if (strlcat(*path, "/", len) >= len) {
4481 err = got_error(GOT_ERR_NO_SPACE);
4484 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4487 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4488 err = got_error(GOT_ERR_NO_SPACE);
4500 static const struct got_error *
4501 blame_tree_entry(struct tog_view **new_view, int begin_x,
4502 struct got_tree_entry *te, struct tog_parent_trees *parents,
4503 struct got_object_id *commit_id, struct got_reflist_head *refs,
4504 struct got_repository *repo)
4506 const struct got_error *err = NULL;
4508 struct tog_view *blame_view;
4512 err = tree_entry_path(&path, parents, te);
4516 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4517 if (blame_view == NULL) {
4518 err = got_error_from_errno("view_open");
4522 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4524 if (err->code == GOT_ERR_CANCELLED)
4526 view_close(blame_view);
4528 *new_view = blame_view;
4534 static const struct got_error *
4535 log_tree_entry(struct tog_view **new_view, int begin_x,
4536 struct got_tree_entry *te, struct tog_parent_trees *parents,
4537 struct got_object_id *commit_id, struct got_reflist_head *refs,
4538 struct got_repository *repo)
4540 struct tog_view *log_view;
4541 const struct got_error *err = NULL;
4546 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4547 if (log_view == NULL)
4548 return got_error_from_errno("view_open");
4550 err = tree_entry_path(&path, parents, te);
4554 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4556 view_close(log_view);
4558 *new_view = log_view;
4563 static const struct got_error *
4564 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4565 struct got_object_id *commit_id, struct got_reflist_head *refs,
4566 struct got_repository *repo)
4568 const struct got_error *err = NULL;
4569 char *commit_id_str = NULL;
4570 struct tog_tree_view_state *s = &view->state.tree;
4572 TAILQ_INIT(&s->parents);
4574 err = got_object_id_str(&commit_id_str, commit_id);
4578 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4579 err = got_error_from_errno("asprintf");
4583 s->root = s->tree = root;
4584 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4585 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4586 s->commit_id = got_object_id_dup(commit_id);
4587 if (s->commit_id == NULL) {
4588 err = got_error_from_errno("got_object_id_dup");
4594 SIMPLEQ_INIT(&s->colors);
4596 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4597 err = add_color(&s->colors, "\\$$",
4598 TOG_COLOR_TREE_SUBMODULE,
4599 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4602 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4603 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4605 free_colors(&s->colors);
4608 err = add_color(&s->colors, "/$",
4609 TOG_COLOR_TREE_DIRECTORY,
4610 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4612 free_colors(&s->colors);
4616 err = add_color(&s->colors, "\\*$",
4617 TOG_COLOR_TREE_EXECUTABLE,
4618 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4620 free_colors(&s->colors);
4624 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4625 get_color_value("TOG_COLOR_COMMIT"));
4627 free_colors(&s->colors);
4632 view->show = show_tree_view;
4633 view->input = input_tree_view;
4634 view->close = close_tree_view;
4635 view->search_start = search_start_tree_view;
4636 view->search_next = search_next_tree_view;
4638 free(commit_id_str);
4640 free(s->tree_label);
4641 s->tree_label = NULL;
4646 static const struct got_error *
4647 close_tree_view(struct tog_view *view)
4649 struct tog_tree_view_state *s = &view->state.tree;
4651 free_colors(&s->colors);
4652 free(s->tree_label);
4653 s->tree_label = NULL;
4655 s->commit_id = NULL;
4656 while (!TAILQ_EMPTY(&s->parents)) {
4657 struct tog_parent_tree *parent;
4658 parent = TAILQ_FIRST(&s->parents);
4659 TAILQ_REMOVE(&s->parents, parent, entry);
4663 if (s->tree != s->root)
4664 got_object_tree_close(s->tree);
4665 got_object_tree_close(s->root);
4670 static const struct got_error *
4671 search_start_tree_view(struct tog_view *view)
4673 struct tog_tree_view_state *s = &view->state.tree;
4675 s->matched_entry = NULL;
4680 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4682 regmatch_t regmatch;
4684 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4688 static const struct got_error *
4689 search_next_tree_view(struct tog_view *view)
4691 struct tog_tree_view_state *s = &view->state.tree;
4692 struct got_tree_entry *te = NULL;
4694 if (!view->searching) {
4695 view->search_next_done = 1;
4699 if (s->matched_entry) {
4700 if (view->searching == TOG_SEARCH_FORWARD) {
4701 if (s->selected_entry)
4702 te = got_tree_entry_get_next(s->tree,
4705 te = got_object_tree_get_first_entry(s->tree);
4707 if (s->selected_entry == NULL)
4708 te = got_object_tree_get_last_entry(s->tree);
4710 te = got_tree_entry_get_prev(s->tree,
4714 if (view->searching == TOG_SEARCH_FORWARD)
4715 te = got_object_tree_get_first_entry(s->tree);
4717 te = got_object_tree_get_last_entry(s->tree);
4722 if (s->matched_entry == NULL) {
4723 view->search_next_done = 1;
4726 if (view->searching == TOG_SEARCH_FORWARD)
4727 te = got_object_tree_get_first_entry(s->tree);
4729 te = got_object_tree_get_last_entry(s->tree);
4732 if (match_tree_entry(te, &view->regex)) {
4733 view->search_next_done = 1;
4734 s->matched_entry = te;
4738 if (view->searching == TOG_SEARCH_FORWARD)
4739 te = got_tree_entry_get_next(s->tree, te);
4741 te = got_tree_entry_get_prev(s->tree, te);
4744 if (s->matched_entry) {
4745 s->first_displayed_entry = s->matched_entry;
4752 static const struct got_error *
4753 show_tree_view(struct tog_view *view)
4755 const struct got_error *err = NULL;
4756 struct tog_tree_view_state *s = &view->state.tree;
4759 err = tree_entry_path(&parent_path, &s->parents, NULL);
4763 err = draw_tree_entries(view, &s->first_displayed_entry,
4764 &s->last_displayed_entry, &s->selected_entry,
4765 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4766 s->tree, s->selected, view->nlines, s->tree == s->root,
4774 static const struct got_error *
4775 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4776 struct tog_view **focus_view, struct tog_view *view, int ch)
4778 const struct got_error *err = NULL;
4779 struct tog_tree_view_state *s = &view->state.tree;
4780 struct tog_view *log_view;
4781 int begin_x = 0, nscrolled;
4785 s->show_ids = !s->show_ids;
4788 if (!s->selected_entry)
4790 if (view_is_parent_view(view))
4791 begin_x = view_split_begin_x(view->begin_x);
4792 err = log_tree_entry(&log_view, begin_x,
4793 s->selected_entry, &s->parents,
4794 s->commit_id, s->refs, s->repo);
4795 if (view_is_parent_view(view)) {
4796 err = view_close_child(view);
4799 err = view_set_child(view, log_view);
4801 view_close(log_view);
4804 *focus_view = log_view;
4805 view->child_focussed = 1;
4807 *new_view = log_view;
4811 if (s->selected > 0) {
4813 if (s->selected == 0)
4816 if (s->selected > 0)
4818 tree_scroll_up(view, &s->first_displayed_entry, 1,
4819 s->tree, s->tree == s->root);
4822 tree_scroll_up(view, &s->first_displayed_entry,
4823 MAX(0, view->nlines - 4 - s->selected), s->tree,
4824 s->tree == s->root);
4826 if (got_object_tree_get_first_entry(s->tree) ==
4827 s->first_displayed_entry && s->tree != s->root)
4828 s->first_displayed_entry = NULL;
4832 if (s->selected < s->ndisplayed - 1) {
4836 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4838 /* can't scroll any further */
4840 tree_scroll_down(&s->first_displayed_entry, 1,
4841 s->last_displayed_entry, s->tree);
4844 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4846 /* can't scroll any further; move cursor down */
4847 if (s->selected < s->ndisplayed - 1)
4848 s->selected = s->ndisplayed - 1;
4851 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4852 view->nlines, s->last_displayed_entry, s->tree);
4853 if (nscrolled < view->nlines) {
4855 struct got_tree_entry *te;
4856 te = s->first_displayed_entry;
4859 te = got_tree_entry_get_next(s->tree, te);
4861 s->selected = ndisplayed - 1;
4867 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4868 struct tog_parent_tree *parent;
4869 /* user selected '..' */
4870 if (s->tree == s->root)
4872 parent = TAILQ_FIRST(&s->parents);
4873 TAILQ_REMOVE(&s->parents, parent,
4875 got_object_tree_close(s->tree);
4876 s->tree = parent->tree;
4877 s->first_displayed_entry =
4878 parent->first_displayed_entry;
4880 parent->selected_entry;
4881 s->selected = parent->selected;
4883 } else if (S_ISDIR(got_tree_entry_get_mode(
4884 s->selected_entry))) {
4885 struct got_tree_object *subtree;
4886 err = got_object_open_as_tree(&subtree, s->repo,
4887 got_tree_entry_get_id(s->selected_entry));
4890 err = tree_view_visit_subtree(subtree, s);
4892 got_object_tree_close(subtree);
4895 } else if (S_ISREG(got_tree_entry_get_mode(
4896 s->selected_entry))) {
4897 struct tog_view *blame_view;
4898 int begin_x = view_is_parent_view(view) ?
4899 view_split_begin_x(view->begin_x) : 0;
4901 err = blame_tree_entry(&blame_view, begin_x,
4902 s->selected_entry, &s->parents,
4903 s->commit_id, s->refs, s->repo);
4906 if (view_is_parent_view(view)) {
4907 err = view_close_child(view);
4910 err = view_set_child(view, blame_view);
4912 view_close(blame_view);
4915 *focus_view = blame_view;
4916 view->child_focussed = 1;
4918 *new_view = blame_view;
4922 if (s->selected > view->nlines)
4923 s->selected = s->ndisplayed - 1;
4936 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4941 static const struct got_error *
4942 cmd_tree(int argc, char *argv[])
4944 const struct got_error *error;
4945 struct got_repository *repo = NULL;
4946 struct got_reflist_head refs;
4947 char *repo_path = NULL;
4948 struct got_object_id *commit_id = NULL;
4949 char *commit_id_arg = NULL;
4950 struct got_commit_object *commit = NULL;
4951 struct got_tree_object *tree = NULL;
4953 struct tog_view *view;
4955 SIMPLEQ_INIT(&refs);
4958 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4963 while ((ch = getopt(argc, argv, "c:")) != -1) {
4966 commit_id_arg = optarg;
4978 struct got_worktree *worktree;
4979 char *cwd = getcwd(NULL, 0);
4981 return got_error_from_errno("getcwd");
4982 error = got_worktree_open(&worktree, cwd);
4983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4988 strdup(got_worktree_get_repo_path(worktree));
4989 got_worktree_close(worktree);
4992 if (repo_path == NULL) {
4993 error = got_error_from_errno("strdup");
4996 } else if (argc == 1) {
4997 repo_path = realpath(argv[0], NULL);
4998 if (repo_path == NULL)
4999 return got_error_from_errno2("realpath", argv[0]);
5005 error = got_repo_open(&repo, repo_path, NULL);
5009 error = apply_unveil(got_repo_get_path(repo), NULL);
5013 error = got_repo_match_object_id(&commit_id, NULL,
5014 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5015 GOT_OBJ_TYPE_COMMIT, 1, repo);
5019 error = got_object_open_as_commit(&commit, repo, commit_id);
5023 error = got_object_open_as_tree(&tree, repo,
5024 got_object_commit_get_tree_id(commit));
5028 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5032 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5034 error = got_error_from_errno("view_open");
5037 error = open_tree_view(view, tree, commit_id, &refs, repo);
5040 error = view_loop(view);
5045 got_object_commit_close(commit);
5047 got_object_tree_close(tree);
5049 got_repo_close(repo);
5050 got_ref_list_free(&refs);
5059 fprintf(stderr, "commands:");
5060 for (i = 0; i < nitems(tog_commands); i++) {
5061 struct tog_cmd *cmd = &tog_commands[i];
5062 fprintf(stderr, " %s", cmd->name);
5064 fputc('\n', stderr);
5070 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5078 make_argv(const char *arg0, const char *arg1)
5081 int argc = (arg1 == NULL ? 1 : 2);
5083 argv = calloc(argc, sizeof(char *));
5086 argv[0] = strdup(arg0);
5087 if (argv[0] == NULL)
5090 argv[1] = strdup(arg1);
5091 if (argv[1] == NULL)
5099 main(int argc, char *argv[])
5101 const struct got_error *error = NULL;
5102 struct tog_cmd *cmd = NULL;
5103 int ch, hflag = 0, Vflag = 0;
5104 char **cmd_argv = NULL;
5105 static struct option longopts[] = {
5106 { "version", no_argument, NULL, 'V' },
5110 setlocale(LC_CTYPE, "");
5112 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5132 got_version_print_str();
5139 /* Build an argument vector which runs a default command. */
5140 cmd = &tog_commands[0];
5141 cmd_argv = make_argv(cmd->name, NULL);
5146 /* Did the user specific a command? */
5147 for (i = 0; i < nitems(tog_commands); i++) {
5148 if (strncmp(tog_commands[i].name, argv[0],
5149 strlen(argv[0])) == 0) {
5150 cmd = &tog_commands[i];
5156 fprintf(stderr, "%s: unknown command '%s'\n",
5157 getprogname(), argv[0]);
5166 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5170 if (error && error->code != GOT_ERR_CANCELLED)
5171 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);