2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_opentemp.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
55 #include "got_worktree.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 #define CTRL(x) ((x) & 0x1f)
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
113 struct commit_queue_head head;
117 SIMPLEQ_ENTRY(tog_color) entry;
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
222 if (strcasecmp(val, "red") == 0)
224 if (strcasecmp(val, "green") == 0)
226 if (strcasecmp(val, "yellow") == 0)
228 if (strcasecmp(val, "blue") == 0)
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
234 if (strcasecmp(val, "white") == 0)
236 if (strcasecmp(val, "default") == 0)
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
246 int first_displayed_line;
247 int last_displayed_line;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
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;
286 struct got_repository *repo;
287 struct got_reflist_head *refs;
288 struct got_object_id *start_id;
291 struct tog_log_thread_args thread_args;
292 struct commit_queue_entry *matched_entry;
293 struct commit_queue_entry *search_entry;
294 struct tog_colors colors;
297 #define TOG_COLOR_DIFF_MINUS 1
298 #define TOG_COLOR_DIFF_PLUS 2
299 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
300 #define TOG_COLOR_DIFF_META 4
301 #define TOG_COLOR_TREE_SUBMODULE 5
302 #define TOG_COLOR_TREE_SYMLINK 6
303 #define TOG_COLOR_TREE_DIRECTORY 7
304 #define TOG_COLOR_TREE_EXECUTABLE 8
305 #define TOG_COLOR_COMMIT 9
306 #define TOG_COLOR_AUTHOR 10
307 #define TOG_COLOR_DATE 11
309 struct tog_blame_cb_args {
310 struct tog_blame_line *lines; /* one per line */
313 struct tog_view *view;
314 struct got_object_id *commit_id;
318 struct tog_blame_thread_args {
320 struct got_repository *repo;
321 struct tog_blame_cb_args *cb_args;
323 got_cancel_cb cancel_cb;
330 struct tog_blame_line *lines;
334 struct tog_blame_thread_args thread_args;
335 struct tog_blame_cb_args cb_args;
339 struct tog_blame_view_state {
340 int first_displayed_line;
341 int last_displayed_line;
346 struct got_object_id_queue blamed_commits;
347 struct got_object_qid *blamed_commit;
349 struct got_repository *repo;
350 struct got_reflist_head *refs;
351 struct got_object_id *commit_id;
352 struct tog_blame blame;
354 struct tog_colors colors;
357 struct tog_parent_tree {
358 TAILQ_ENTRY(tog_parent_tree) entry;
359 struct got_tree_object *tree;
360 struct got_tree_entry *first_displayed_entry;
361 struct got_tree_entry *selected_entry;
365 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
367 struct tog_tree_view_state {
369 struct got_tree_object *root;
370 struct got_tree_object *tree;
371 struct got_tree_entry *first_displayed_entry;
372 struct got_tree_entry *last_displayed_entry;
373 struct got_tree_entry *selected_entry;
374 int ndisplayed, selected, show_ids;
375 struct tog_parent_trees parents;
376 struct got_object_id *commit_id;
377 struct got_repository *repo;
378 struct got_reflist_head *refs;
379 struct got_tree_entry *matched_entry;
380 struct tog_colors colors;
384 * We implement two types of views: parent views and child views.
386 * The 'Tab' key switches between a parent view and its child view.
387 * Child views are shown side-by-side to their parent view, provided
388 * there is enough screen estate.
390 * When a new view is opened from within a parent view, this new view
391 * becomes a child view of the parent view, replacing any existing child.
393 * When a new view is opened from within a child view, this new view
394 * becomes a parent view which will obscure the views below until the
395 * user quits the new parent view by typing 'q'.
397 * This list of views contains parent views only.
398 * Child views are only pointed to by their parent view.
400 TAILQ_HEAD(tog_view_list_head, tog_view);
403 TAILQ_ENTRY(tog_view) entry;
406 int nlines, ncols, begin_y, begin_x;
407 int lines, cols; /* copies of LINES and COLS */
409 struct tog_view *parent;
410 struct tog_view *child;
413 /* type-specific state */
414 enum tog_view_type type;
416 struct tog_diff_view_state diff;
417 struct tog_log_view_state log;
418 struct tog_blame_view_state blame;
419 struct tog_tree_view_state tree;
422 const struct got_error *(*show)(struct tog_view *);
423 const struct got_error *(*input)(struct tog_view **,
424 struct tog_view **, struct tog_view**, struct tog_view *, int);
425 const struct got_error *(*close)(struct tog_view *);
427 const struct got_error *(*search_start)(struct tog_view *);
428 const struct got_error *(*search_next)(struct tog_view *);
430 #define TOG_SEARCH_FORWARD 1
431 #define TOG_SEARCH_BACKWARD 2
432 int search_next_done;
436 static const struct got_error *open_diff_view(struct tog_view *,
437 struct got_object_id *, struct got_object_id *, struct tog_view *,
438 struct got_reflist_head *, struct got_repository *);
439 static const struct got_error *show_diff_view(struct tog_view *);
440 static const struct got_error *input_diff_view(struct tog_view **,
441 struct tog_view **, struct tog_view **, struct tog_view *, int);
442 static const struct got_error* close_diff_view(struct tog_view *);
444 static const struct got_error *open_log_view(struct tog_view *,
445 struct got_object_id *, struct got_reflist_head *,
446 struct got_repository *, const char *, const char *, int, int);
447 static const struct got_error * show_log_view(struct tog_view *);
448 static const struct got_error *input_log_view(struct tog_view **,
449 struct tog_view **, struct tog_view **, struct tog_view *, int);
450 static const struct got_error *close_log_view(struct tog_view *);
451 static const struct got_error *search_start_log_view(struct tog_view *);
452 static const struct got_error *search_next_log_view(struct tog_view *);
454 static const struct got_error *open_blame_view(struct tog_view *, char *,
455 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
456 static const struct got_error *show_blame_view(struct tog_view *);
457 static const struct got_error *input_blame_view(struct tog_view **,
458 struct tog_view **, struct tog_view **, struct tog_view *, int);
459 static const struct got_error *close_blame_view(struct tog_view *);
460 static const struct got_error *search_start_blame_view(struct tog_view *);
461 static const struct got_error *search_next_blame_view(struct tog_view *);
463 static const struct got_error *open_tree_view(struct tog_view *,
464 struct got_tree_object *, struct got_object_id *,
465 struct got_reflist_head *, struct got_repository *);
466 static const struct got_error *show_tree_view(struct tog_view *);
467 static const struct got_error *input_tree_view(struct tog_view **,
468 struct tog_view **, struct tog_view **, struct tog_view *, int);
469 static const struct got_error *close_tree_view(struct tog_view *);
470 static const struct got_error *search_start_tree_view(struct tog_view *);
471 static const struct got_error *search_next_tree_view(struct tog_view *);
473 static volatile sig_atomic_t tog_sigwinch_received;
474 static volatile sig_atomic_t tog_sigpipe_received;
475 static volatile sig_atomic_t tog_sigcont_received;
478 tog_sigwinch(int signo)
480 tog_sigwinch_received = 1;
484 tog_sigpipe(int signo)
486 tog_sigpipe_received = 1;
490 tog_sigcont(int signo)
492 tog_sigcont_received = 1;
495 static const struct got_error *
496 view_close(struct tog_view *view)
498 const struct got_error *err = NULL;
501 view_close(view->child);
505 err = view->close(view);
507 del_panel(view->panel);
509 delwin(view->window);
514 static struct tog_view *
515 view_open(int nlines, int ncols, int begin_y, int begin_x,
516 enum tog_view_type type)
518 struct tog_view *view = calloc(1, sizeof(*view));
526 view->nlines = nlines ? nlines : LINES - begin_y;
527 view->ncols = ncols ? ncols : COLS - begin_x;
528 view->begin_y = begin_y;
529 view->begin_x = begin_x;
530 view->window = newwin(nlines, ncols, begin_y, begin_x);
531 if (view->window == NULL) {
535 view->panel = new_panel(view->window);
536 if (view->panel == NULL ||
537 set_panel_userptr(view->panel, view) != OK) {
542 keypad(view->window, TRUE);
547 view_split_begin_x(int begin_x)
549 if (begin_x > 0 || COLS < 120)
551 return (COLS - MAX(COLS / 2, 80));
554 static const struct got_error *view_resize(struct tog_view *);
556 static const struct got_error *
557 view_splitscreen(struct tog_view *view)
559 const struct got_error *err = NULL;
562 view->begin_x = view_split_begin_x(0);
563 view->nlines = LINES;
564 view->ncols = COLS - view->begin_x;
567 err = view_resize(view);
571 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
572 return got_error_from_errno("mvwin");
577 static const struct got_error *
578 view_fullscreen(struct tog_view *view)
580 const struct got_error *err = NULL;
584 view->nlines = LINES;
588 err = view_resize(view);
592 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
593 return got_error_from_errno("mvwin");
599 view_is_parent_view(struct tog_view *view)
601 return view->parent == NULL;
604 static const struct got_error *
605 view_resize(struct tog_view *view)
609 if (view->lines > LINES)
610 nlines = view->nlines - (view->lines - LINES);
612 nlines = view->nlines + (LINES - view->lines);
614 if (view->cols > COLS)
615 ncols = view->ncols - (view->cols - COLS);
617 ncols = view->ncols + (COLS - view->cols);
619 if (wresize(view->window, nlines, ncols) == ERR)
620 return got_error_from_errno("wresize");
621 if (replace_panel(view->panel, view->window) == ERR)
622 return got_error_from_errno("replace_panel");
623 wclear(view->window);
625 view->nlines = nlines;
631 view->child->begin_x = view_split_begin_x(view->begin_x);
632 if (view->child->begin_x == 0) {
633 view_fullscreen(view->child);
634 if (view->child->focussed)
635 show_panel(view->child->panel);
637 show_panel(view->panel);
639 view_splitscreen(view->child);
640 show_panel(view->child->panel);
647 static const struct got_error *
648 view_close_child(struct tog_view *view)
650 const struct got_error *err = NULL;
652 if (view->child == NULL)
655 err = view_close(view->child);
660 static const struct got_error *
661 view_set_child(struct tog_view *view, struct tog_view *child)
663 const struct got_error *err = NULL;
666 child->parent = view;
671 view_is_splitscreen(struct tog_view *view)
673 return view->begin_x > 0;
682 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
683 cols = 80; /* Default */
689 resize_term(lines, cols);
692 static const struct got_error *
693 view_search_start(struct tog_view *view)
695 const struct got_error *err = NULL;
700 if (view->nlines < 1)
703 if (!view_is_parent_view(view))
704 begin_x = view_split_begin_x(view->begin_x);
705 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
707 wclrtoeol(view->window);
711 ret = wgetnstr(view->window, pattern, sizeof(pattern));
717 if (view->searching) {
718 regfree(&view->regex);
722 if (regcomp(&view->regex, pattern,
723 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
724 err = view->search_start(view);
726 regfree(&view->regex);
729 view->searching = TOG_SEARCH_FORWARD;
730 view->search_next_done = 0;
731 view->search_next(view);
737 static const struct got_error *
738 view_input(struct tog_view **new, struct tog_view **dead,
739 struct tog_view **focus, int *done, struct tog_view *view,
740 struct tog_view_list_head *views)
742 const struct got_error *err = NULL;
750 if (view->searching && !view->search_next_done) {
751 view->search_next(view);
755 nodelay(stdscr, FALSE);
756 /* Allow threads to make progress while we are waiting for input. */
757 errcode = pthread_mutex_unlock(&tog_mutex);
759 return got_error_set_errno(errcode, "pthread_mutex_unlock");
760 ch = wgetch(view->window);
761 errcode = pthread_mutex_lock(&tog_mutex);
763 return got_error_set_errno(errcode, "pthread_mutex_lock");
764 nodelay(stdscr, TRUE);
766 if (tog_sigwinch_received || tog_sigcont_received) {
768 tog_sigwinch_received = 0;
769 tog_sigcont_received = 0;
770 TAILQ_FOREACH(v, views, entry) {
771 err = view_resize(v);
774 err = v->input(new, dead, focus, v, KEY_RESIZE);
785 *focus = view->child;
786 view->child_focussed = 1;
787 } else if (view->parent) {
788 *focus = view->parent;
789 view->parent->child_focussed = 0;
793 err = view->input(new, dead, focus, view, ch);
800 if (view_is_parent_view(view)) {
801 if (view->child == NULL)
803 if (view_is_splitscreen(view->child)) {
804 *focus = view->child;
805 view->child_focussed = 1;
806 err = view_fullscreen(view->child);
808 err = view_splitscreen(view->child);
811 err = view->child->input(new, dead, focus,
812 view->child, KEY_RESIZE);
814 if (view_is_splitscreen(view)) {
816 view->parent->child_focussed = 1;
817 err = view_fullscreen(view);
819 err = view_splitscreen(view);
823 err = view->input(new, dead, focus, view,
830 if (view->search_start)
831 view_search_start(view);
833 err = view->input(new, dead, focus, view, ch);
837 if (view->search_next && view->searching) {
838 view->searching = (ch == 'n' ?
839 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
840 view->search_next_done = 0;
841 view->search_next(view);
843 err = view->input(new, dead, focus, view, ch);
846 err = view->input(new, dead, focus, view, ch);
854 view_vborder(struct tog_view *view)
857 struct tog_view *view_above;
860 return view_vborder(view->parent);
862 panel = panel_above(view->panel);
866 view_above = panel_userptr(panel);
867 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
868 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
872 view_needs_focus_indication(struct tog_view *view)
874 if (view_is_parent_view(view)) {
875 if (view->child == NULL || view->child_focussed)
877 if (!view_is_splitscreen(view->child))
879 } else if (!view_is_splitscreen(view))
882 return view->focussed;
885 static const struct got_error *
886 view_loop(struct tog_view *view)
888 const struct got_error *err = NULL;
889 struct tog_view_list_head views;
890 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
891 int fast_refresh = 10;
892 int done = 0, errcode;
894 errcode = pthread_mutex_lock(&tog_mutex);
896 return got_error_set_errno(errcode, "pthread_mutex_lock");
899 TAILQ_INSERT_HEAD(&views, view, entry);
903 err = view->show(view);
908 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
909 /* Refresh fast during initialization, then become slower. */
910 if (fast_refresh && fast_refresh-- == 0)
911 halfdelay(10); /* switch to once per second */
913 err = view_input(&new_view, &dead_view, &focus_view, &done,
918 struct tog_view *prev = NULL;
920 if (view_is_parent_view(dead_view))
921 prev = TAILQ_PREV(dead_view,
922 tog_view_list_head, entry);
923 else if (view->parent != dead_view)
926 if (dead_view->parent)
927 dead_view->parent->child = NULL;
929 TAILQ_REMOVE(&views, dead_view, entry);
931 err = view_close(dead_view);
932 if (err || (dead_view == main_view && new_view == NULL))
935 if (view == dead_view) {
940 else if (!TAILQ_EMPTY(&views))
941 view = TAILQ_LAST(&views,
946 if (view->child && view->child_focussed)
947 focus_view = view->child;
954 struct tog_view *v, *t;
955 /* Only allow one parent view per type. */
956 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
957 if (v->type != new_view->type)
959 TAILQ_REMOVE(&views, v, entry);
965 TAILQ_INSERT_TAIL(&views, new_view, entry);
967 if (focus_view == NULL)
968 focus_view = new_view;
971 show_panel(focus_view->panel);
974 focus_view->focussed = 1;
977 show_panel(new_view->panel);
978 if (view->child && view_is_splitscreen(view->child))
979 show_panel(view->child->panel);
982 if (focus_view == NULL) {
984 show_panel(view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
990 err = view->parent->show(view->parent);
994 err = view->show(view);
998 err = view->child->show(view->child);
1007 while (!TAILQ_EMPTY(&views)) {
1008 view = TAILQ_FIRST(&views);
1009 TAILQ_REMOVE(&views, view, entry);
1013 errcode = pthread_mutex_unlock(&tog_mutex);
1014 if (errcode && err == NULL)
1015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1025 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1030 /* Create newly allocated wide-character string equivalent to a byte string. */
1031 static const struct got_error *
1032 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1035 const struct got_error *err = NULL;
1038 *wlen = mbstowcs(NULL, s, 0);
1039 if (*wlen == (size_t)-1) {
1041 if (errno != EILSEQ)
1042 return got_error_from_errno("mbstowcs");
1044 /* byte string invalid in current encoding; try to "fix" it */
1045 err = got_mbsavis(&vis, &vislen, s);
1048 *wlen = mbstowcs(NULL, vis, 0);
1049 if (*wlen == (size_t)-1) {
1050 err = got_error_from_errno("mbstowcs"); /* give up */
1055 *ws = calloc(*wlen + 1, sizeof(**ws));
1057 err = got_error_from_errno("calloc");
1061 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1062 err = got_error_from_errno("mbstowcs");
1073 /* Format a line for display, ensuring that it won't overflow a width limit. */
1074 static const struct got_error *
1075 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1078 const struct got_error *err = NULL;
1080 wchar_t *wline = NULL;
1087 err = mbs2ws(&wline, &wlen, line);
1093 int width = wcwidth(wline[i]);
1100 if (width == 1 || width == 2) {
1101 if (cols + width > wlimit)
1105 } else if (width == -1) {
1106 if (wline[i] == L'\t') {
1108 ((cols + col_tab_align) % TABSIZE);
1109 if (cols + width > wlimit)
1115 err = got_error_from_errno("wcwidth");
1130 static const struct got_error*
1131 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1132 struct got_object_id *id, struct got_repository *repo)
1134 static const struct got_error *err = NULL;
1135 struct got_reflist_entry *re;
1141 SIMPLEQ_FOREACH(re, refs, entry) {
1142 struct got_tag_object *tag = NULL;
1145 name = got_ref_get_name(re->ref);
1146 if (strcmp(name, GOT_REF_HEAD) == 0)
1148 if (strncmp(name, "refs/", 5) == 0)
1150 if (strncmp(name, "got/", 4) == 0)
1152 if (strncmp(name, "heads/", 6) == 0)
1154 if (strncmp(name, "remotes/", 8) == 0)
1156 if (strncmp(name, "tags/", 5) == 0) {
1157 err = got_object_open_as_tag(&tag, repo, re->id);
1159 if (err->code != GOT_ERR_OBJ_TYPE)
1161 /* Ref points at something other than a tag. */
1166 cmp = got_object_id_cmp(tag ?
1167 got_object_tag_get_object_id(tag) : re->id, id);
1169 got_object_tag_close(tag);
1173 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1174 s ? ", " : "", name) == -1) {
1175 err = got_error_from_errno("asprintf");
1186 static const struct got_error *
1187 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1190 char *smallerthan, *at;
1192 smallerthan = strchr(author, '<');
1193 if (smallerthan && smallerthan[1] != '\0')
1194 author = smallerthan + 1;
1195 at = strchr(author, '@');
1198 return format_line(wauthor, author_width, author, limit, col_tab_align);
1201 static const struct got_error *
1202 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1203 struct got_object_id *id, struct got_reflist_head *refs,
1204 const size_t date_display_cols, int author_display_cols,
1205 struct tog_colors *colors)
1207 const struct got_error *err = NULL;
1208 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1209 char *logmsg0 = NULL, *logmsg = NULL;
1210 char *author = NULL;
1211 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1212 int author_width, logmsg_width;
1213 char *newline, *line = NULL;
1215 const int avail = view->ncols;
1217 time_t committer_time;
1218 struct tog_color *tc;
1220 committer_time = got_object_commit_get_committer_time(commit);
1221 if (localtime_r(&committer_time, &tm) == NULL)
1222 return got_error_from_errno("localtime_r");
1223 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1225 return got_error(GOT_ERR_NO_SPACE);
1227 if (avail <= date_display_cols)
1228 limit = MIN(sizeof(datebuf) - 1, avail);
1230 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1231 tc = get_color(colors, TOG_COLOR_DATE);
1233 wattr_on(view->window,
1234 COLOR_PAIR(tc->colorpair), NULL);
1235 waddnstr(view->window, datebuf, limit);
1237 wattr_off(view->window,
1238 COLOR_PAIR(tc->colorpair), NULL);
1245 err = got_object_id_str(&id_str, id);
1248 tc = get_color(colors, TOG_COLOR_COMMIT);
1250 wattr_on(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1252 wprintw(view->window, "%.8s ", id_str);
1254 wattr_off(view->window,
1255 COLOR_PAIR(tc->colorpair), NULL);
1262 author = strdup(got_object_commit_get_author(commit));
1263 if (author == NULL) {
1264 err = got_error_from_errno("strdup");
1267 err = format_author(&wauthor, &author_width, author, avail - col, col);
1270 tc = get_color(colors, TOG_COLOR_AUTHOR);
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 waddwstr(view->window, wauthor);
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 col += author_width;
1279 while (col < avail && author_width < author_display_cols + 2) {
1280 waddch(view->window, ' ');
1287 err = got_object_commit_get_logmsg(&logmsg0, commit);
1291 while (*logmsg == '\n')
1293 newline = strchr(logmsg, '\n');
1296 limit = avail - col;
1297 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1300 waddwstr(view->window, wlogmsg);
1301 col += logmsg_width;
1302 while (col < avail) {
1303 waddch(view->window, ' ');
1315 static struct commit_queue_entry *
1316 alloc_commit_queue_entry(struct got_commit_object *commit,
1317 struct got_object_id *id)
1319 struct commit_queue_entry *entry;
1321 entry = calloc(1, sizeof(*entry));
1326 entry->commit = commit;
1331 pop_commit(struct commit_queue *commits)
1333 struct commit_queue_entry *entry;
1335 entry = TAILQ_FIRST(&commits->head);
1336 TAILQ_REMOVE(&commits->head, entry, entry);
1337 got_object_commit_close(entry->commit);
1338 commits->ncommits--;
1339 /* Don't free entry->id! It is owned by the commit graph. */
1344 free_commits(struct commit_queue *commits)
1346 while (!TAILQ_EMPTY(&commits->head))
1347 pop_commit(commits);
1350 static const struct got_error *
1351 match_commit(int *have_match, struct got_object_id *id,
1352 struct got_commit_object *commit, regex_t *regex)
1354 const struct got_error *err = NULL;
1355 regmatch_t regmatch;
1356 char *id_str = NULL, *logmsg = NULL;
1360 err = got_object_id_str(&id_str, id);
1364 err = got_object_commit_get_logmsg(&logmsg, commit);
1368 if (regexec(regex, got_object_commit_get_author(commit), 1,
1369 ®match, 0) == 0 ||
1370 regexec(regex, got_object_commit_get_committer(commit), 1,
1371 ®match, 0) == 0 ||
1372 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1373 regexec(regex, logmsg, 1, ®match, 0) == 0)
1381 static const struct got_error *
1382 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1383 int minqueue, struct got_repository *repo, const char *path,
1384 int *searching, int *search_next_done, regex_t *regex)
1386 const struct got_error *err = NULL;
1387 int nqueued = 0, have_match = 0;
1390 * We keep all commits open throughout the lifetime of the log
1391 * view in order to avoid having to re-fetch commits from disk
1392 * while updating the display.
1394 while (nqueued < minqueue ||
1395 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1396 struct got_object_id *id;
1397 struct got_commit_object *commit;
1398 struct commit_queue_entry *entry;
1401 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1402 if (err || id == NULL)
1405 err = got_object_open_as_commit(&commit, repo, id);
1408 entry = alloc_commit_queue_entry(commit, id);
1409 if (entry == NULL) {
1410 err = got_error_from_errno("alloc_commit_queue_entry");
1414 errcode = pthread_mutex_lock(&tog_mutex);
1416 err = got_error_set_errno(errcode,
1417 "pthread_mutex_lock");
1421 entry->idx = commits->ncommits;
1422 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1424 commits->ncommits++;
1426 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1427 err = match_commit(&have_match, id, commit, regex);
1430 errcode = pthread_mutex_unlock(&tog_mutex);
1431 if (errcode && err == NULL)
1432 err = got_error_set_errno(errcode,
1433 "pthread_mutex_unlock");
1435 if (err || have_match)
1442 static const struct got_error *
1443 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1444 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1445 struct commit_queue *commits, int selected_idx, int limit,
1446 struct got_reflist_head *refs, const char *path, int commits_needed,
1447 struct tog_colors *colors)
1449 const struct got_error *err = NULL;
1450 struct tog_log_view_state *s = &view->state.log;
1451 struct commit_queue_entry *entry;
1453 int ncommits, author_cols = 4;
1454 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1455 char *refs_str = NULL;
1457 struct tog_color *tc;
1458 static const size_t date_display_cols = 12;
1463 if (ncommits == selected_idx) {
1467 entry = TAILQ_NEXT(entry, entry);
1471 if (*selected && !(view->searching && view->search_next_done == 0)) {
1472 err = got_object_id_str(&id_str, (*selected)->id);
1476 err = build_refs_str(&refs_str, refs, (*selected)->id,
1483 if (commits_needed == 0)
1484 halfdelay(10); /* disable fast refresh */
1486 if (asprintf(&ncommits_str, " [%d/%d] %s",
1487 entry ? entry->idx + 1 : 0, commits->ncommits,
1488 commits_needed > 0 ?
1489 (view->searching && view->search_next_done == 0
1490 ? "searching..." : "loading... ") :
1491 (refs_str ? refs_str : "")) == -1) {
1492 err = got_error_from_errno("asprintf");
1496 if (path && strcmp(path, "/") != 0) {
1497 if (asprintf(&header, "commit %s %s%s",
1498 id_str ? id_str : "........................................",
1499 path, ncommits_str) == -1) {
1500 err = got_error_from_errno("asprintf");
1504 } else if (asprintf(&header, "commit %s%s",
1505 id_str ? id_str : "........................................",
1506 ncommits_str) == -1) {
1507 err = got_error_from_errno("asprintf");
1511 err = format_line(&wline, &width, header, view->ncols, 0);
1515 werase(view->window);
1517 if (view_needs_focus_indication(view))
1518 wstandout(view->window);
1519 tc = get_color(colors, TOG_COLOR_COMMIT);
1521 wattr_on(view->window,
1522 COLOR_PAIR(tc->colorpair), NULL);
1523 waddwstr(view->window, wline);
1525 wattr_off(view->window,
1526 COLOR_PAIR(tc->colorpair), NULL);
1527 while (width < view->ncols) {
1528 waddch(view->window, ' ');
1531 if (view_needs_focus_indication(view))
1532 wstandend(view->window);
1537 /* Grow author column size if necessary. */
1544 if (ncommits >= limit - 1)
1546 author = strdup(got_object_commit_get_author(entry->commit));
1547 if (author == NULL) {
1548 err = got_error_from_errno("strdup");
1551 err = format_author(&wauthor, &width, author, COLS,
1553 if (author_cols < width)
1554 author_cols = width;
1558 entry = TAILQ_NEXT(entry, entry);
1565 if (ncommits >= limit - 1)
1567 if (ncommits == selected_idx)
1568 wstandout(view->window);
1569 err = draw_commit(view, entry->commit, entry->id, refs,
1570 date_display_cols, author_cols, colors);
1571 if (ncommits == selected_idx)
1572 wstandend(view->window);
1577 entry = TAILQ_NEXT(entry, entry);
1590 scroll_up(struct tog_view *view,
1591 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1592 struct commit_queue *commits)
1594 struct commit_queue_entry *entry;
1597 entry = TAILQ_FIRST(&commits->head);
1598 if (*first_displayed_entry == entry)
1601 entry = *first_displayed_entry;
1602 while (entry && nscrolled < maxscroll) {
1603 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1605 *first_displayed_entry = entry;
1611 static const struct got_error *
1612 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1613 pthread_cond_t *need_commits)
1618 halfdelay(1); /* fast refresh while loading commits */
1620 while (*commits_needed > 0) {
1624 /* Wake the log thread. */
1625 errcode = pthread_cond_signal(need_commits);
1627 return got_error_set_errno(errcode,
1628 "pthread_cond_signal");
1629 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1631 * Thread is not done yet; lose a key press
1632 * and let the user retry... this way the GUI
1633 * remains interactive while logging deep paths
1634 * with few commits in history.
1643 static const struct got_error *
1644 scroll_down(struct tog_view *view,
1645 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1646 struct commit_queue_entry **last_displayed_entry,
1647 struct commit_queue *commits, int *log_complete, int *commits_needed,
1648 pthread_cond_t *need_commits)
1650 const struct got_error *err = NULL;
1651 struct commit_queue_entry *pentry;
1654 if (*last_displayed_entry == NULL)
1657 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1658 if (pentry == NULL && !*log_complete) {
1660 * Ask the log thread for required amount of commits
1661 * plus some amount of pre-fetching.
1663 (*commits_needed) += maxscroll + 20;
1664 err = trigger_log_thread(0, commits_needed, log_complete,
1671 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1675 *last_displayed_entry = pentry;
1677 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1680 *first_displayed_entry = pentry;
1681 } while (++nscrolled < maxscroll);
1686 static const struct got_error *
1687 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1688 struct got_commit_object *commit, struct got_object_id *commit_id,
1689 struct tog_view *log_view, struct got_reflist_head *refs,
1690 struct got_repository *repo)
1692 const struct got_error *err;
1693 struct got_object_qid *parent_id;
1694 struct tog_view *diff_view;
1696 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1697 if (diff_view == NULL)
1698 return got_error_from_errno("view_open");
1700 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1701 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1702 commit_id, log_view, refs, repo);
1704 *new_view = diff_view;
1708 static const struct got_error *
1709 tree_view_visit_subtree(struct got_tree_object *subtree,
1710 struct tog_tree_view_state *s)
1712 struct tog_parent_tree *parent;
1714 parent = calloc(1, sizeof(*parent));
1716 return got_error_from_errno("calloc");
1718 parent->tree = s->tree;
1719 parent->first_displayed_entry = s->first_displayed_entry;
1720 parent->selected_entry = s->selected_entry;
1721 parent->selected = s->selected;
1722 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1725 s->first_displayed_entry = NULL;
1730 static const struct got_error *
1731 browse_commit_tree(struct tog_view **new_view, int begin_x,
1732 struct commit_queue_entry *entry, const char *path,
1733 struct got_reflist_head *refs, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_tree_object *tree;
1737 struct tog_tree_view_state *s;
1738 struct tog_view *tree_view;
1739 char *slash, *subpath = NULL;
1742 err = got_object_open_as_tree(&tree, repo,
1743 got_object_commit_get_tree_id(entry->commit));
1747 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1748 if (tree_view == NULL)
1749 return got_error_from_errno("view_open");
1751 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1753 got_object_tree_close(tree);
1756 s = &tree_view->state.tree;
1758 *new_view = tree_view;
1760 if (got_path_is_root_dir(path))
1763 /* Walk the path and open corresponding tree objects. */
1766 struct got_tree_entry *te;
1767 struct got_object_id *tree_id;
1773 /* Ensure the correct subtree entry is selected. */
1774 slash = strchr(p, '/');
1776 te_name = strdup(p);
1778 te_name = strndup(p, slash - p);
1779 if (te_name == NULL) {
1780 err = got_error_from_errno("strndup");
1783 te = got_object_tree_find_entry(s->tree, te_name);
1785 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1790 s->selected_entry = te;
1791 s->selected = got_tree_entry_get_index(te);
1792 if (s->tree != s->root)
1793 s->selected++; /* skip '..' */
1795 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1796 /* Jump to this file's entry. */
1797 s->first_displayed_entry = s->selected_entry;
1802 slash = strchr(p, '/');
1804 subpath = strndup(path, slash - path);
1806 subpath = strdup(path);
1807 if (subpath == NULL) {
1808 err = got_error_from_errno("strdup");
1812 err = got_object_id_by_path(&tree_id, repo, entry->id,
1817 err = got_object_open_as_tree(&tree, repo, tree_id);
1822 err = tree_view_visit_subtree(tree, s);
1824 got_object_tree_close(tree);
1838 static const struct got_error *
1839 block_signals_used_by_main_thread(void)
1844 if (sigemptyset(&sigset) == -1)
1845 return got_error_from_errno("sigemptyset");
1847 /* tog handles SIGWINCH and SIGCONT */
1848 if (sigaddset(&sigset, SIGWINCH) == -1)
1849 return got_error_from_errno("sigaddset");
1850 if (sigaddset(&sigset, SIGCONT) == -1)
1851 return got_error_from_errno("sigaddset");
1853 /* ncurses handles SIGTSTP */
1854 if (sigaddset(&sigset, SIGTSTP) == -1)
1855 return got_error_from_errno("sigaddset");
1857 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1859 return got_error_set_errno(errcode, "pthread_sigmask");
1865 log_thread(void *arg)
1867 const struct got_error *err = NULL;
1869 struct tog_log_thread_args *a = arg;
1872 err = block_signals_used_by_main_thread();
1876 while (!done && !err && !tog_sigpipe_received) {
1877 err = queue_commits(a->graph, a->commits, 1, a->repo,
1878 a->in_repo_path, a->searching, a->search_next_done,
1881 if (err->code != GOT_ERR_ITER_COMPLETED)
1885 } else if (a->commits_needed > 0)
1886 a->commits_needed--;
1888 errcode = pthread_mutex_lock(&tog_mutex);
1890 err = got_error_set_errno(errcode,
1891 "pthread_mutex_lock");
1893 } else if (*a->quit)
1895 else if (*a->first_displayed_entry == NULL) {
1896 *a->first_displayed_entry =
1897 TAILQ_FIRST(&a->commits->head);
1898 *a->selected_entry = *a->first_displayed_entry;
1902 a->commits_needed = 0;
1903 else if (a->commits_needed == 0) {
1904 errcode = pthread_cond_wait(&a->need_commits,
1907 err = got_error_set_errno(errcode,
1908 "pthread_cond_wait");
1911 errcode = pthread_mutex_unlock(&tog_mutex);
1912 if (errcode && err == NULL)
1913 err = got_error_set_errno(errcode,
1914 "pthread_mutex_unlock");
1916 a->log_complete = 1;
1920 static const struct got_error *
1921 stop_log_thread(struct tog_log_view_state *s)
1923 const struct got_error *err = NULL;
1928 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1930 return got_error_set_errno(errcode,
1931 "pthread_cond_signal");
1932 errcode = pthread_mutex_unlock(&tog_mutex);
1934 return got_error_set_errno(errcode,
1935 "pthread_mutex_unlock");
1936 errcode = pthread_join(s->thread, (void **)&err);
1938 return got_error_set_errno(errcode, "pthread_join");
1939 errcode = pthread_mutex_lock(&tog_mutex);
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_lock");
1946 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1947 if (errcode && err == NULL)
1948 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1950 if (s->thread_args.repo) {
1951 got_repo_close(s->thread_args.repo);
1952 s->thread_args.repo = NULL;
1955 if (s->thread_args.graph) {
1956 got_commit_graph_close(s->thread_args.graph);
1957 s->thread_args.graph = NULL;
1963 static const struct got_error *
1964 close_log_view(struct tog_view *view)
1966 const struct got_error *err = NULL;
1967 struct tog_log_view_state *s = &view->state.log;
1969 err = stop_log_thread(s);
1970 free_commits(&s->commits);
1971 free(s->in_repo_path);
1972 s->in_repo_path = NULL;
1978 static const struct got_error *
1979 search_start_log_view(struct tog_view *view)
1981 struct tog_log_view_state *s = &view->state.log;
1983 s->matched_entry = NULL;
1984 s->search_entry = NULL;
1988 static const struct got_error *
1989 search_next_log_view(struct tog_view *view)
1991 const struct got_error *err = NULL;
1992 struct tog_log_view_state *s = &view->state.log;
1993 struct commit_queue_entry *entry;
1995 if (!view->searching) {
1996 view->search_next_done = 1;
2000 if (s->search_entry) {
2002 errcode = pthread_mutex_unlock(&tog_mutex);
2004 return got_error_set_errno(errcode,
2005 "pthread_mutex_unlock");
2006 ch = wgetch(view->window);
2007 errcode = pthread_mutex_lock(&tog_mutex);
2009 return got_error_set_errno(errcode,
2010 "pthread_mutex_lock");
2011 if (ch == KEY_BACKSPACE) {
2012 view->search_next_done = 1;
2015 if (view->searching == TOG_SEARCH_FORWARD)
2016 entry = TAILQ_NEXT(s->search_entry, entry);
2018 entry = TAILQ_PREV(s->search_entry,
2019 commit_queue_head, entry);
2020 } else if (s->matched_entry) {
2021 if (view->searching == TOG_SEARCH_FORWARD)
2022 entry = TAILQ_NEXT(s->selected_entry, entry);
2024 entry = TAILQ_PREV(s->selected_entry,
2025 commit_queue_head, entry);
2027 if (view->searching == TOG_SEARCH_FORWARD)
2028 entry = TAILQ_FIRST(&s->commits.head);
2030 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2036 if (entry == NULL) {
2037 if (s->thread_args.log_complete ||
2038 view->searching == TOG_SEARCH_BACKWARD) {
2039 view->search_next_done = 1;
2043 * Poke the log thread for more commits and return,
2044 * allowing the main loop to make progress. Search
2045 * will resume at s->search_entry once we come back.
2047 s->thread_args.commits_needed++;
2048 return trigger_log_thread(1,
2049 &s->thread_args.commits_needed,
2050 &s->thread_args.log_complete,
2051 &s->thread_args.need_commits);
2054 err = match_commit(&have_match, entry->id, entry->commit,
2059 view->search_next_done = 1;
2060 s->matched_entry = entry;
2064 s->search_entry = entry;
2065 if (view->searching == TOG_SEARCH_FORWARD)
2066 entry = TAILQ_NEXT(entry, entry);
2068 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2071 if (s->matched_entry) {
2072 int cur = s->selected_entry->idx;
2073 while (cur < s->matched_entry->idx) {
2074 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2079 while (cur > s->matched_entry->idx) {
2080 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2087 s->search_entry = NULL;
2092 static const struct got_error *
2093 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2094 struct got_reflist_head *refs, struct got_repository *repo,
2095 const char *head_ref_name, const char *path, int check_disk,
2098 const struct got_error *err = NULL;
2099 struct tog_log_view_state *s = &view->state.log;
2100 struct got_repository *thread_repo = NULL;
2101 struct got_commit_graph *thread_graph = NULL;
2104 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2108 /* The commit queue only contains commits being displayed. */
2109 TAILQ_INIT(&s->commits.head);
2110 s->commits.ncommits = 0;
2114 s->head_ref_name = head_ref_name;
2115 s->start_id = got_object_id_dup(start_id);
2116 if (s->start_id == NULL) {
2117 err = got_error_from_errno("got_object_id_dup");
2120 s->log_branches = log_branches;
2122 SIMPLEQ_INIT(&s->colors);
2123 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2124 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2125 get_color_value("TOG_COLOR_COMMIT"));
2128 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2129 get_color_value("TOG_COLOR_AUTHOR"));
2131 free_colors(&s->colors);
2134 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2135 get_color_value("TOG_COLOR_DATE"));
2137 free_colors(&s->colors);
2142 view->show = show_log_view;
2143 view->input = input_log_view;
2144 view->close = close_log_view;
2145 view->search_start = search_start_log_view;
2146 view->search_next = search_next_log_view;
2148 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2151 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2155 err = got_commit_graph_iter_start(thread_graph,
2156 s->start_id, s->repo, NULL, NULL);
2160 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2162 err = got_error_set_errno(errcode, "pthread_cond_init");
2166 s->thread_args.commits_needed = view->nlines;
2167 s->thread_args.graph = thread_graph;
2168 s->thread_args.commits = &s->commits;
2169 s->thread_args.in_repo_path = s->in_repo_path;
2170 s->thread_args.start_id = s->start_id;
2171 s->thread_args.repo = thread_repo;
2172 s->thread_args.log_complete = 0;
2173 s->thread_args.quit = &s->quit;
2174 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2175 s->thread_args.selected_entry = &s->selected_entry;
2176 s->thread_args.searching = &view->searching;
2177 s->thread_args.search_next_done = &view->search_next_done;
2178 s->thread_args.regex = &view->regex;
2181 close_log_view(view);
2185 static const struct got_error *
2186 show_log_view(struct tog_view *view)
2188 struct tog_log_view_state *s = &view->state.log;
2190 if (s->thread == NULL) {
2191 int errcode = pthread_create(&s->thread, NULL, log_thread,
2194 return got_error_set_errno(errcode, "pthread_create");
2197 return draw_commits(view, &s->last_displayed_entry,
2198 &s->selected_entry, s->first_displayed_entry,
2199 &s->commits, s->selected, view->nlines, s->refs,
2200 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2203 static const struct got_error *
2204 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2205 struct tog_view **focus_view, struct tog_view *view, int ch)
2207 const struct got_error *err = NULL;
2208 struct tog_log_view_state *s = &view->state.log;
2209 char *parent_path, *in_repo_path = NULL;
2210 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2212 struct got_object_id *start_id;
2222 if (s->first_displayed_entry == NULL)
2224 if (s->selected > 0)
2227 scroll_up(view, &s->first_displayed_entry, 1,
2232 if (s->first_displayed_entry == NULL)
2234 if (TAILQ_FIRST(&s->commits.head) ==
2235 s->first_displayed_entry) {
2239 scroll_up(view, &s->first_displayed_entry,
2240 view->nlines, &s->commits);
2246 if (s->first_displayed_entry == NULL)
2248 if (s->selected < MIN(view->nlines - 2,
2249 s->commits.ncommits - 1)) {
2253 err = scroll_down(view, &s->first_displayed_entry, 1,
2254 &s->last_displayed_entry, &s->commits,
2255 &s->thread_args.log_complete,
2256 &s->thread_args.commits_needed,
2257 &s->thread_args.need_commits);
2261 struct commit_queue_entry *first;
2262 first = s->first_displayed_entry;
2265 err = scroll_down(view, &s->first_displayed_entry,
2266 view->nlines, &s->last_displayed_entry,
2267 &s->commits, &s->thread_args.log_complete,
2268 &s->thread_args.commits_needed,
2269 &s->thread_args.need_commits);
2272 if (first == s->first_displayed_entry &&
2273 s->selected < MIN(view->nlines - 2,
2274 s->commits.ncommits - 1)) {
2275 /* can't scroll further down */
2276 s->selected = MIN(view->nlines - 2,
2277 s->commits.ncommits - 1);
2283 if (s->selected > view->nlines - 2)
2284 s->selected = view->nlines - 2;
2285 if (s->selected > s->commits.ncommits - 1)
2286 s->selected = s->commits.ncommits - 1;
2291 if (s->selected_entry == NULL)
2293 if (view_is_parent_view(view))
2294 begin_x = view_split_begin_x(view->begin_x);
2295 err = open_diff_view_for_commit(&diff_view, begin_x,
2296 s->selected_entry->commit, s->selected_entry->id,
2297 view, s->refs, s->repo);
2300 if (view_is_parent_view(view)) {
2301 err = view_close_child(view);
2304 err = view_set_child(view, diff_view);
2306 view_close(diff_view);
2309 *focus_view = diff_view;
2310 view->child_focussed = 1;
2312 *new_view = diff_view;
2315 if (s->selected_entry == NULL)
2317 if (view_is_parent_view(view))
2318 begin_x = view_split_begin_x(view->begin_x);
2319 err = browse_commit_tree(&tree_view, begin_x,
2320 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2323 if (view_is_parent_view(view)) {
2324 err = view_close_child(view);
2327 err = view_set_child(view, tree_view);
2329 view_close(tree_view);
2332 *focus_view = tree_view;
2333 view->child_focussed = 1;
2335 *new_view = tree_view;
2338 if (strcmp(s->in_repo_path, "/") == 0)
2340 parent_path = dirname(s->in_repo_path);
2341 if (parent_path && strcmp(parent_path, ".") != 0) {
2342 err = stop_log_thread(s);
2345 lv = view_open(view->nlines, view->ncols,
2346 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2348 return got_error_from_errno(
2350 err = open_log_view(lv, s->start_id, s->refs,
2351 s->repo, s->head_ref_name, parent_path, 0,
2355 if (view_is_parent_view(view))
2358 view_set_child(view->parent, lv);
2365 err = stop_log_thread(s);
2368 lv = view_open(view->nlines, view->ncols,
2369 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2371 return got_error_from_errno("view_open");
2372 err = got_repo_match_object_id(&start_id, NULL,
2373 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2374 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2379 in_repo_path = strdup(s->in_repo_path);
2380 if (in_repo_path == NULL) {
2383 return got_error_from_errno("strdup");
2385 got_ref_list_free(s->refs);
2386 err = got_ref_list(s->refs, s->repo, NULL,
2387 got_ref_cmp_by_name, NULL);
2393 err = open_log_view(lv, start_id, s->refs, s->repo,
2394 s->head_ref_name, in_repo_path, 0, s->log_branches);
2404 s->log_branches = !s->log_branches;
2405 err = stop_log_thread(s);
2408 lv = view_open(view->nlines, view->ncols,
2409 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2411 return got_error_from_errno("view_open");
2412 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2413 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
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;
2486 int ch, log_branches = 0, check_disk = 1;
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, "bc:r:")) != -1) {
2503 start_commit = optarg;
2506 repo_path = realpath(optarg, NULL);
2507 if (repo_path == NULL)
2508 return got_error_from_errno2("realpath",
2520 cwd = getcwd(NULL, 0);
2522 error = got_error_from_errno("getcwd");
2525 error = got_worktree_open(&worktree, cwd);
2526 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2533 error = got_error_from_errno("strdup");
2536 } else if (argc == 1) {
2538 error = got_worktree_resolve_path(&path, worktree,
2543 path = strdup(argv[0]);
2545 error = got_error_from_errno("strdup");
2552 if (repo_path == NULL) {
2555 got_worktree_get_repo_path(worktree));
2557 repo_path = strdup(cwd);
2559 if (repo_path == NULL) {
2560 error = got_error_from_errno("strdup");
2566 error = got_repo_open(&repo, repo_path, NULL);
2570 error = apply_unveil(got_repo_get_path(repo),
2571 worktree ? got_worktree_get_root_path(worktree) : NULL);
2575 if (start_commit == NULL)
2576 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2577 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2578 GOT_OBJ_TYPE_COMMIT, 1, repo);
2580 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2581 GOT_OBJ_TYPE_COMMIT, 1, repo);
2585 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2589 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2591 error = got_error_from_errno("view_open");
2595 const char *prefix = got_worktree_get_path_prefix(worktree);
2597 if (asprintf(&p, "%s%s%s", prefix,
2598 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2599 error = got_error_from_errno("asprintf");
2606 head_ref_name = strdup(
2607 got_worktree_get_head_ref_name(worktree));
2608 if (head_ref_name == NULL) {
2609 error = got_error_from_errno("strdup");
2613 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2614 path, check_disk, log_branches);
2618 /* Release work tree lock. */
2619 got_worktree_close(worktree);
2622 error = view_loop(view);
2628 free(head_ref_name);
2630 got_repo_close(repo);
2632 got_worktree_close(worktree);
2633 got_ref_list_free(&refs);
2641 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2647 parse_next_line(FILE *f, size_t *len)
2652 const char delim[3] = { '\0', '\0', '\0'};
2654 line = fparseln(f, &linelen, &lineno, delim, 0);
2661 match_line(const char *line, regex_t *regex)
2663 regmatch_t regmatch;
2665 return regexec(regex, line, 1, ®match, 0) == 0;
2669 match_color(struct tog_colors *colors, const char *line)
2671 struct tog_color *tc = NULL;
2673 SIMPLEQ_FOREACH(tc, colors, entry) {
2674 if (match_line(line, &tc->regex))
2681 static const struct got_error *
2682 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2683 int *last_displayed_line, int *eof, int max_lines, char *header,
2684 struct tog_colors *colors)
2686 const struct got_error *err;
2687 int nlines = 0, nprinted = 0;
2689 struct tog_color *tc;
2695 werase(view->window);
2698 err = format_line(&wline, &width, header, view->ncols, 0);
2703 if (view_needs_focus_indication(view))
2704 wstandout(view->window);
2705 waddwstr(view->window, wline);
2706 if (view_needs_focus_indication(view))
2707 wstandend(view->window);
2708 if (width <= view->ncols - 1)
2709 waddch(view->window, '\n');
2717 while (nprinted < max_lines) {
2718 line = parse_next_line(f, &len);
2723 if (++nlines < *first_displayed_line) {
2728 err = format_line(&wline, &width, line, view->ncols, 0);
2734 tc = match_color(colors, line);
2736 wattr_on(view->window,
2737 COLOR_PAIR(tc->colorpair), NULL);
2738 waddwstr(view->window, wline);
2740 wattr_off(view->window,
2741 COLOR_PAIR(tc->colorpair), NULL);
2742 if (width <= view->ncols - 1)
2743 waddch(view->window, '\n');
2744 if (++nprinted == 1)
2745 *first_displayed_line = nlines;
2750 *last_displayed_line = nlines;
2755 while (nprinted < view->nlines) {
2756 waddch(view->window, '\n');
2760 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2765 wstandout(view->window);
2766 waddwstr(view->window, wline);
2767 wstandend(view->window);
2774 get_datestr(time_t *time, char *datebuf)
2776 struct tm mytm, *tm;
2779 tm = gmtime_r(time, &mytm);
2782 s = asctime_r(tm, datebuf);
2785 p = strchr(s, '\n');
2791 static const struct got_error *
2792 write_commit_info(struct got_object_id *commit_id,
2793 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2795 const struct got_error *err = NULL;
2796 char datebuf[26], *datestr;
2797 struct got_commit_object *commit;
2798 char *id_str = NULL, *logmsg = NULL;
2799 time_t committer_time;
2800 const char *author, *committer;
2801 char *refs_str = NULL;
2804 err = build_refs_str(&refs_str, refs, commit_id, repo);
2809 err = got_object_open_as_commit(&commit, repo, commit_id);
2813 err = got_object_id_str(&id_str, commit_id);
2815 err = got_error_from_errno("got_object_id_str");
2819 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2820 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2821 err = got_error_from_errno("fprintf");
2824 if (fprintf(outfile, "from: %s\n",
2825 got_object_commit_get_author(commit)) < 0) {
2826 err = got_error_from_errno("fprintf");
2829 committer_time = got_object_commit_get_committer_time(commit);
2830 datestr = get_datestr(&committer_time, datebuf);
2831 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2832 err = got_error_from_errno("fprintf");
2835 author = got_object_commit_get_author(commit);
2836 committer = got_object_commit_get_committer(commit);
2837 if (strcmp(author, committer) != 0 &&
2838 fprintf(outfile, "via: %s\n", committer) < 0) {
2839 err = got_error_from_errno("fprintf");
2842 err = got_object_commit_get_logmsg(&logmsg, commit);
2845 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2846 err = got_error_from_errno("fprintf");
2853 got_object_commit_close(commit);
2857 static const struct got_error *
2858 create_diff(struct tog_diff_view_state *s)
2860 const struct got_error *err = NULL;
2866 err = got_error_from_errno("got_opentemp");
2869 if (s->f && fclose(s->f) != 0) {
2870 err = got_error_from_errno("fclose");
2876 err = got_object_get_type(&obj_type, s->repo, s->id1);
2878 err = got_object_get_type(&obj_type, s->repo, s->id2);
2883 case GOT_OBJ_TYPE_BLOB:
2884 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2885 s->diff_context, 0, s->repo, f);
2887 case GOT_OBJ_TYPE_TREE:
2888 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2889 s->diff_context, 0, s->repo, f);
2891 case GOT_OBJ_TYPE_COMMIT: {
2892 const struct got_object_id_queue *parent_ids;
2893 struct got_object_qid *pid;
2894 struct got_commit_object *commit2;
2896 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2899 /* Show commit info if we're diffing to a parent/root commit. */
2901 write_commit_info(s->id2, s->refs, s->repo, f);
2903 parent_ids = got_object_commit_get_parent_ids(commit2);
2904 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2905 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2906 write_commit_info(s->id2, s->refs,
2912 got_object_commit_close(commit2);
2914 err = got_diff_objects_as_commits(s->id1, s->id2,
2915 s->diff_context, 0, s->repo, f);
2919 err = got_error(GOT_ERR_OBJ_TYPE);
2923 if (f && fflush(f) != 0 && err == NULL)
2924 err = got_error_from_errno("fflush");
2929 diff_view_indicate_progress(struct tog_view *view)
2931 mvwaddstr(view->window, 0, 0, "diffing...");
2936 static const struct got_error *
2937 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2938 struct got_object_id *id2, struct tog_view *log_view,
2939 struct got_reflist_head *refs, struct got_repository *repo)
2941 const struct got_error *err;
2942 struct tog_diff_view_state *s = &view->state.diff;
2944 if (id1 != NULL && id2 != NULL) {
2946 err = got_object_get_type(&type1, repo, id1);
2949 err = got_object_get_type(&type2, repo, id2);
2954 return got_error(GOT_ERR_OBJ_TYPE);
2958 s->id1 = got_object_id_dup(id1);
2960 return got_error_from_errno("got_object_id_dup");
2964 s->id2 = got_object_id_dup(id2);
2965 if (s->id2 == NULL) {
2968 return got_error_from_errno("got_object_id_dup");
2971 s->first_displayed_line = 1;
2972 s->last_displayed_line = view->nlines;
2973 s->diff_context = 3;
2974 s->log_view = log_view;
2977 SIMPLEQ_INIT(&s->colors);
2979 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2980 err = add_color(&s->colors,
2981 "^-", TOG_COLOR_DIFF_MINUS,
2982 get_color_value("TOG_COLOR_DIFF_MINUS"));
2985 err = add_color(&s->colors, "^\\+",
2986 TOG_COLOR_DIFF_PLUS,
2987 get_color_value("TOG_COLOR_DIFF_PLUS"));
2989 free_colors(&s->colors);
2992 err = add_color(&s->colors,
2993 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2994 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2996 free_colors(&s->colors);
3000 err = add_color(&s->colors,
3001 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3002 get_color_value("TOG_COLOR_DIFF_META"));
3004 free_colors(&s->colors);
3008 err = add_color(&s->colors,
3009 "^(from|via): ", TOG_COLOR_AUTHOR,
3010 get_color_value("TOG_COLOR_AUTHOR"));
3012 free_colors(&s->colors);
3016 err = add_color(&s->colors,
3017 "^date: ", TOG_COLOR_DATE,
3018 get_color_value("TOG_COLOR_DATE"));
3020 free_colors(&s->colors);
3025 if (log_view && view_is_splitscreen(view))
3026 show_log_view(log_view); /* draw vborder */
3027 diff_view_indicate_progress(view);
3029 err = create_diff(s);
3038 view->show = show_diff_view;
3039 view->input = input_diff_view;
3040 view->close = close_diff_view;
3045 static const struct got_error *
3046 close_diff_view(struct tog_view *view)
3048 const struct got_error *err = NULL;
3049 struct tog_diff_view_state *s = &view->state.diff;
3055 if (s->f && fclose(s->f) == EOF)
3056 err = got_error_from_errno("fclose");
3057 free_colors(&s->colors);
3061 static const struct got_error *
3062 show_diff_view(struct tog_view *view)
3064 const struct got_error *err;
3065 struct tog_diff_view_state *s = &view->state.diff;
3066 char *id_str1 = NULL, *id_str2, *header;
3069 err = got_object_id_str(&id_str1, s->id1);
3073 err = got_object_id_str(&id_str2, s->id2);
3077 if (asprintf(&header, "diff %s %s",
3078 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3079 err = got_error_from_errno("asprintf");
3087 return draw_file(view, s->f, &s->first_displayed_line,
3088 &s->last_displayed_line, &s->eof, view->nlines,
3089 header, &s->colors);
3092 static const struct got_error *
3093 set_selected_commit(struct tog_diff_view_state *s,
3094 struct commit_queue_entry *entry)
3096 const struct got_error *err;
3097 const struct got_object_id_queue *parent_ids;
3098 struct got_commit_object *selected_commit;
3099 struct got_object_qid *pid;
3102 s->id2 = got_object_id_dup(entry->id);
3104 return got_error_from_errno("got_object_id_dup");
3106 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3109 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3111 pid = SIMPLEQ_FIRST(parent_ids);
3112 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3113 got_object_commit_close(selected_commit);
3117 static const struct got_error *
3118 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3119 struct tog_view **focus_view, struct tog_view *view, int ch)
3121 const struct got_error *err = NULL;
3122 struct tog_diff_view_state *s = &view->state.diff;
3123 struct tog_log_view_state *ls;
3124 struct commit_queue_entry *entry;
3130 if (s->first_displayed_line > 1)
3131 s->first_displayed_line--;
3135 if (s->first_displayed_line == 1)
3138 while (i++ < view->nlines - 1 &&
3139 s->first_displayed_line > 1)
3140 s->first_displayed_line--;
3145 s->first_displayed_line++;
3153 while (!s->eof && i++ < view->nlines - 1) {
3155 line = parse_next_line(s->f, NULL);
3156 s->first_displayed_line++;
3162 if (s->diff_context > 0) {
3164 diff_view_indicate_progress(view);
3165 err = create_diff(s);
3169 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3171 diff_view_indicate_progress(view);
3172 err = create_diff(s);
3177 if (s->log_view == NULL)
3179 ls = &s->log_view->state.log;
3180 entry = TAILQ_PREV(ls->selected_entry,
3181 commit_queue_head, entry);
3185 err = input_log_view(NULL, NULL, NULL, s->log_view,
3190 err = set_selected_commit(s, entry);
3194 s->first_displayed_line = 1;
3195 s->last_displayed_line = view->nlines;
3197 diff_view_indicate_progress(view);
3198 err = create_diff(s);
3202 if (s->log_view == NULL)
3204 ls = &s->log_view->state.log;
3206 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3207 ls->thread_args.commits_needed++;
3209 /* Display "loading..." in log view. */
3210 show_log_view(s->log_view);
3214 err = trigger_log_thread(1 /* load_all */,
3215 &ls->thread_args.commits_needed,
3216 &ls->thread_args.log_complete,
3217 &ls->thread_args.need_commits);
3221 err = input_log_view(NULL, NULL, NULL, s->log_view,
3226 entry = TAILQ_NEXT(ls->selected_entry, entry);
3230 err = set_selected_commit(s, entry);
3234 s->first_displayed_line = 1;
3235 s->last_displayed_line = view->nlines;
3237 diff_view_indicate_progress(view);
3238 err = create_diff(s);
3247 static const struct got_error *
3248 cmd_diff(int argc, char *argv[])
3250 const struct got_error *error = NULL;
3251 struct got_repository *repo = NULL;
3252 struct got_reflist_head refs;
3253 struct got_object_id *id1 = NULL, *id2 = NULL;
3254 char *repo_path = NULL;
3255 char *id_str1 = NULL, *id_str2 = NULL;
3257 struct tog_view *view;
3259 SIMPLEQ_INIT(&refs);
3262 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3267 while ((ch = getopt(argc, argv, "")) != -1) {
3279 usage_diff(); /* TODO show local worktree changes */
3280 } else if (argc == 2) {
3281 repo_path = getcwd(NULL, 0);
3282 if (repo_path == NULL)
3283 return got_error_from_errno("getcwd");
3286 } else if (argc == 3) {
3287 repo_path = realpath(argv[0], NULL);
3288 if (repo_path == NULL)
3289 return got_error_from_errno2("realpath", argv[0]);
3297 error = got_repo_open(&repo, repo_path, NULL);
3301 error = apply_unveil(got_repo_get_path(repo), NULL);
3305 error = got_repo_match_object_id_prefix(&id1, id_str1,
3306 GOT_OBJ_TYPE_ANY, repo);
3310 error = got_repo_match_object_id_prefix(&id2, id_str2,
3311 GOT_OBJ_TYPE_ANY, repo);
3315 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3319 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3321 error = got_error_from_errno("view_open");
3324 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3327 error = view_loop(view);
3331 got_repo_close(repo);
3332 got_ref_list_free(&refs);
3340 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3345 struct tog_blame_line {
3347 struct got_object_id *id;
3350 static const struct got_error *
3351 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3352 const char *path, struct tog_blame_line *lines, int nlines,
3353 int blame_complete, int selected_line, int *first_displayed_line,
3354 int *last_displayed_line, int *eof, int max_lines,
3355 struct tog_colors *colors)
3357 const struct got_error *err;
3358 int lineno = 0, nprinted = 0;
3363 struct tog_blame_line *blame_line;
3364 struct got_object_id *prev_id = NULL;
3366 struct tog_color *tc;
3368 err = got_object_id_str(&id_str, id);
3373 werase(view->window);
3375 if (asprintf(&line, "commit %s", id_str) == -1) {
3376 err = got_error_from_errno("asprintf");
3381 err = format_line(&wline, &width, line, view->ncols, 0);
3386 if (view_needs_focus_indication(view))
3387 wstandout(view->window);
3388 tc = get_color(colors, TOG_COLOR_COMMIT);
3390 wattr_on(view->window,
3391 COLOR_PAIR(tc->colorpair), NULL);
3392 waddwstr(view->window, wline);
3394 wattr_off(view->window,
3395 COLOR_PAIR(tc->colorpair), NULL);
3396 if (view_needs_focus_indication(view))
3397 wstandend(view->window);
3400 if (width < view->ncols - 1)
3401 waddch(view->window, '\n');
3403 if (asprintf(&line, "[%d/%d] %s%s",
3404 *first_displayed_line - 1 + selected_line, nlines,
3405 blame_complete ? "" : "annotating... ", path) == -1) {
3407 return got_error_from_errno("asprintf");
3410 err = format_line(&wline, &width, line, view->ncols, 0);
3415 waddwstr(view->window, wline);
3418 if (width < view->ncols - 1)
3419 waddch(view->window, '\n');
3422 while (nprinted < max_lines - 2) {
3423 line = parse_next_line(f, &len);
3428 if (++lineno < *first_displayed_line) {
3433 if (view->ncols <= 9) {
3435 wline = wcsdup(L"");
3437 err = got_error_from_errno("wcsdup");
3439 err = format_line(&wline, &width, line,
3440 view->ncols - 9, 9);
3448 if (view->focussed && nprinted == selected_line - 1)
3449 wstandout(view->window);
3452 blame_line = &lines[lineno - 1];
3453 if (blame_line->annotated && prev_id &&
3454 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3456 nprinted == selected_line - 1)) {
3457 waddstr(view->window, " ");
3458 } else if (blame_line->annotated) {
3460 err = got_object_id_str(&id_str, blame_line->id);
3466 tc = get_color(colors, TOG_COLOR_COMMIT);
3468 wattr_on(view->window,
3469 COLOR_PAIR(tc->colorpair), NULL);
3470 wprintw(view->window, "%.8s", id_str);
3472 wattr_off(view->window,
3473 COLOR_PAIR(tc->colorpair), NULL);
3475 prev_id = blame_line->id;
3477 waddstr(view->window, "........");
3481 waddstr(view->window, "........");
3485 if (view->focussed && nprinted == selected_line - 1)
3486 wstandend(view->window);
3487 waddstr(view->window, " ");
3489 waddwstr(view->window, wline);
3490 if (width <= view->ncols - 1)
3491 waddch(view->window, '\n');
3492 if (++nprinted == 1)
3493 *first_displayed_line = lineno;
3498 *last_displayed_line = lineno;
3505 static const struct got_error *
3506 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3508 const struct got_error *err = NULL;
3509 struct tog_blame_cb_args *a = arg;
3510 struct tog_blame_line *line;
3513 if (nlines != a->nlines ||
3514 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3515 return got_error(GOT_ERR_RANGE);
3517 errcode = pthread_mutex_lock(&tog_mutex);
3519 return got_error_set_errno(errcode, "pthread_mutex_lock");
3521 if (*a->quit) { /* user has quit the blame view */
3522 err = got_error(GOT_ERR_ITER_COMPLETED);
3527 goto done; /* no change in this commit */
3529 line = &a->lines[lineno - 1];
3530 if (line->annotated)
3533 line->id = got_object_id_dup(id);
3534 if (line->id == NULL) {
3535 err = got_error_from_errno("got_object_id_dup");
3538 line->annotated = 1;
3540 errcode = pthread_mutex_unlock(&tog_mutex);
3542 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3547 blame_thread(void *arg)
3549 const struct got_error *err;
3550 struct tog_blame_thread_args *ta = arg;
3551 struct tog_blame_cb_args *a = ta->cb_args;
3554 err = block_signals_used_by_main_thread();
3558 err = got_blame(ta->path, a->commit_id, ta->repo,
3559 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3560 if (err && err->code == GOT_ERR_CANCELLED)
3563 errcode = pthread_mutex_lock(&tog_mutex);
3565 return (void *)got_error_set_errno(errcode,
3566 "pthread_mutex_lock");
3568 got_repo_close(ta->repo);
3572 errcode = pthread_mutex_unlock(&tog_mutex);
3573 if (errcode && err == NULL)
3574 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3579 static struct got_object_id *
3580 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3581 int first_displayed_line, int selected_line)
3583 struct tog_blame_line *line;
3588 line = &lines[first_displayed_line - 1 + selected_line - 1];
3589 if (!line->annotated)
3595 static const struct got_error *
3596 stop_blame(struct tog_blame *blame)
3598 const struct got_error *err = NULL;
3601 if (blame->thread) {
3603 errcode = pthread_mutex_unlock(&tog_mutex);
3605 return got_error_set_errno(errcode,
3606 "pthread_mutex_unlock");
3607 errcode = pthread_join(blame->thread, (void **)&err);
3609 return got_error_set_errno(errcode, "pthread_join");
3610 errcode = pthread_mutex_lock(&tog_mutex);
3612 return got_error_set_errno(errcode,
3613 "pthread_mutex_lock");
3614 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3616 blame->thread = NULL;
3618 if (blame->thread_args.repo) {
3619 got_repo_close(blame->thread_args.repo);
3620 blame->thread_args.repo = NULL;
3623 if (fclose(blame->f) != 0 && err == NULL)
3624 err = got_error_from_errno("fclose");
3628 for (i = 0; i < blame->nlines; i++)
3629 free(blame->lines[i].id);
3631 blame->lines = NULL;
3633 free(blame->cb_args.commit_id);
3634 blame->cb_args.commit_id = NULL;
3639 static const struct got_error *
3640 cancel_blame_view(void *arg)
3642 const struct got_error *err = NULL;
3646 errcode = pthread_mutex_lock(&tog_mutex);
3648 return got_error_set_errno(errcode,
3649 "pthread_mutex_unlock");
3652 err = got_error(GOT_ERR_CANCELLED);
3654 errcode = pthread_mutex_unlock(&tog_mutex);
3656 return got_error_set_errno(errcode,
3657 "pthread_mutex_lock");
3662 static const struct got_error *
3663 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3664 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3665 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3666 struct got_repository *repo)
3668 const struct got_error *err = NULL;
3669 struct got_blob_object *blob = NULL;
3670 struct got_repository *thread_repo = NULL;
3671 struct got_object_id *obj_id = NULL;
3674 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3678 err = got_object_get_type(&obj_type, repo, obj_id);
3682 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3683 err = got_error(GOT_ERR_OBJ_TYPE);
3687 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3690 blame->f = got_opentemp();
3691 if (blame->f == NULL) {
3692 err = got_error_from_errno("got_opentemp");
3695 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3696 &blame->line_offsets, blame->f, blob);
3697 if (err || blame->nlines == 0)
3700 /* Don't include \n at EOF in the blame line count. */
3701 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3704 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3705 if (blame->lines == NULL) {
3706 err = got_error_from_errno("calloc");
3710 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3714 blame->cb_args.view = view;
3715 blame->cb_args.lines = blame->lines;
3716 blame->cb_args.nlines = blame->nlines;
3717 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3718 if (blame->cb_args.commit_id == NULL) {
3719 err = got_error_from_errno("got_object_id_dup");
3722 blame->cb_args.quit = done;
3724 blame->thread_args.path = path;
3725 blame->thread_args.repo = thread_repo;
3726 blame->thread_args.cb_args = &blame->cb_args;
3727 blame->thread_args.complete = blame_complete;
3728 blame->thread_args.cancel_cb = cancel_blame_view;
3729 blame->thread_args.cancel_arg = done;
3730 *blame_complete = 0;
3734 got_object_blob_close(blob);
3741 static const struct got_error *
3742 open_blame_view(struct tog_view *view, char *path,
3743 struct got_object_id *commit_id, struct got_reflist_head *refs,
3744 struct got_repository *repo)
3746 const struct got_error *err = NULL;
3747 struct tog_blame_view_state *s = &view->state.blame;
3749 SIMPLEQ_INIT(&s->blamed_commits);
3751 s->path = strdup(path);
3752 if (s->path == NULL)
3753 return got_error_from_errno("strdup");
3755 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3761 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3762 s->first_displayed_line = 1;
3763 s->last_displayed_line = view->nlines;
3764 s->selected_line = 1;
3765 s->blame_complete = 0;
3768 s->commit_id = commit_id;
3769 memset(&s->blame, 0, sizeof(s->blame));
3771 SIMPLEQ_INIT(&s->colors);
3772 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3773 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3774 get_color_value("TOG_COLOR_COMMIT"));
3779 view->show = show_blame_view;
3780 view->input = input_blame_view;
3781 view->close = close_blame_view;
3782 view->search_start = search_start_blame_view;
3783 view->search_next = search_next_blame_view;
3785 return run_blame(&s->blame, view, &s->blame_complete,
3786 &s->first_displayed_line, &s->last_displayed_line,
3787 &s->selected_line, &s->done, &s->eof, s->path,
3788 s->blamed_commit->id, s->repo);
3791 static const struct got_error *
3792 close_blame_view(struct tog_view *view)
3794 const struct got_error *err = NULL;
3795 struct tog_blame_view_state *s = &view->state.blame;
3797 if (s->blame.thread)
3798 err = stop_blame(&s->blame);
3800 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3801 struct got_object_qid *blamed_commit;
3802 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3803 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3804 got_object_qid_free(blamed_commit);
3808 free_colors(&s->colors);
3813 static const struct got_error *
3814 search_start_blame_view(struct tog_view *view)
3816 struct tog_blame_view_state *s = &view->state.blame;
3818 s->matched_line = 0;
3822 static const struct got_error *
3823 search_next_blame_view(struct tog_view *view)
3825 struct tog_blame_view_state *s = &view->state.blame;
3828 if (!view->searching) {
3829 view->search_next_done = 1;
3833 if (s->matched_line) {
3834 if (view->searching == TOG_SEARCH_FORWARD)
3835 lineno = s->matched_line + 1;
3837 lineno = s->matched_line - 1;
3839 if (view->searching == TOG_SEARCH_FORWARD)
3842 lineno = s->blame.nlines;
3850 if (lineno <= 0 || lineno > s->blame.nlines) {
3851 if (s->matched_line == 0) {
3852 view->search_next_done = 1;
3857 if (view->searching == TOG_SEARCH_FORWARD)
3860 lineno = s->blame.nlines;
3863 offset = s->blame.line_offsets[lineno - 1];
3864 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3866 return got_error_from_errno("fseeko");
3869 line = parse_next_line(s->blame.f, &len);
3870 if (line && match_line(line, &view->regex)) {
3871 view->search_next_done = 1;
3872 s->matched_line = lineno;
3877 if (view->searching == TOG_SEARCH_FORWARD)
3883 if (s->matched_line) {
3884 s->first_displayed_line = s->matched_line;
3885 s->selected_line = 1;
3891 static const struct got_error *
3892 show_blame_view(struct tog_view *view)
3894 const struct got_error *err = NULL;
3895 struct tog_blame_view_state *s = &view->state.blame;
3898 if (s->blame.thread == NULL) {
3899 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3900 &s->blame.thread_args);
3902 return got_error_set_errno(errcode, "pthread_create");
3904 halfdelay(1); /* fast refresh while annotating */
3907 if (s->blame_complete)
3908 halfdelay(10); /* disable fast refresh */
3910 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3911 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3912 s->selected_line, &s->first_displayed_line,
3913 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3919 static const struct got_error *
3920 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3921 struct tog_view **focus_view, struct tog_view *view, int ch)
3923 const struct got_error *err = NULL, *thread_err = NULL;
3924 struct tog_view *diff_view;
3925 struct tog_blame_view_state *s = &view->state.blame;
3934 if (s->selected_line > 1)
3936 else if (s->selected_line == 1 &&
3937 s->first_displayed_line > 1)
3938 s->first_displayed_line--;
3941 if (s->first_displayed_line == 1) {
3942 s->selected_line = 1;
3945 if (s->first_displayed_line > view->nlines - 2)
3946 s->first_displayed_line -=
3949 s->first_displayed_line = 1;
3953 if (s->selected_line < view->nlines - 2 &&
3954 s->first_displayed_line +
3955 s->selected_line <= s->blame.nlines)
3957 else if (s->last_displayed_line <
3959 s->first_displayed_line++;
3963 struct got_object_id *id = NULL;
3964 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3965 s->first_displayed_line, s->selected_line);
3969 struct got_commit_object *commit;
3970 struct got_object_qid *pid;
3971 struct got_object_id *blob_id = NULL;
3973 err = got_object_open_as_commit(&commit,
3977 pid = SIMPLEQ_FIRST(
3978 got_object_commit_get_parent_ids(commit));
3980 got_object_commit_close(commit);
3983 /* Check if path history ends here. */
3984 err = got_object_id_by_path(&blob_id, s->repo,
3987 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3989 got_object_commit_close(commit);
3992 err = got_object_get_type(&obj_type, s->repo,
3995 /* Can't blame non-blob type objects. */
3996 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3997 got_object_commit_close(commit);
4000 err = got_object_qid_alloc(&s->blamed_commit,
4002 got_object_commit_close(commit);
4004 if (got_object_id_cmp(id,
4005 s->blamed_commit->id) == 0)
4007 err = got_object_qid_alloc(&s->blamed_commit,
4013 thread_err = stop_blame(&s->blame);
4017 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4018 s->blamed_commit, entry);
4019 err = run_blame(&s->blame, view, &s->blame_complete,
4020 &s->first_displayed_line, &s->last_displayed_line,
4021 &s->selected_line, &s->done, &s->eof,
4022 s->path, s->blamed_commit->id, s->repo);
4028 struct got_object_qid *first;
4029 first = SIMPLEQ_FIRST(&s->blamed_commits);
4030 if (!got_object_id_cmp(first->id, s->commit_id))
4033 thread_err = stop_blame(&s->blame);
4037 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4038 got_object_qid_free(s->blamed_commit);
4040 SIMPLEQ_FIRST(&s->blamed_commits);
4041 err = run_blame(&s->blame, view, &s->blame_complete,
4042 &s->first_displayed_line, &s->last_displayed_line,
4043 &s->selected_line, &s->done, &s->eof, s->path,
4044 s->blamed_commit->id, s->repo);
4051 struct got_object_id *id = NULL;
4052 struct got_object_qid *pid;
4053 struct got_commit_object *commit = NULL;
4054 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4055 s->first_displayed_line, s->selected_line);
4058 err = got_object_open_as_commit(&commit, s->repo, id);
4061 pid = SIMPLEQ_FIRST(
4062 got_object_commit_get_parent_ids(commit));
4063 if (view_is_parent_view(view))
4064 begin_x = view_split_begin_x(view->begin_x);
4065 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4066 if (diff_view == NULL) {
4067 got_object_commit_close(commit);
4068 err = got_error_from_errno("view_open");
4071 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4072 id, NULL, s->refs, s->repo);
4073 got_object_commit_close(commit);
4075 view_close(diff_view);
4078 if (view_is_parent_view(view)) {
4079 err = view_close_child(view);
4082 err = view_set_child(view, diff_view);
4084 view_close(diff_view);
4087 *focus_view = diff_view;
4088 view->child_focussed = 1;
4090 *new_view = diff_view;
4097 if (s->last_displayed_line >= s->blame.nlines &&
4098 s->selected_line >= MIN(s->blame.nlines,
4099 view->nlines - 2)) {
4102 if (s->last_displayed_line >= s->blame.nlines &&
4103 s->selected_line < view->nlines - 2) {
4104 s->selected_line = MIN(s->blame.nlines,
4108 if (s->last_displayed_line + view->nlines - 2
4110 s->first_displayed_line +=
4113 s->first_displayed_line =
4118 if (s->selected_line > view->nlines - 2) {
4119 s->selected_line = MIN(s->blame.nlines,
4126 return thread_err ? thread_err : err;
4129 static const struct got_error *
4130 cmd_blame(int argc, char *argv[])
4132 const struct got_error *error;
4133 struct got_repository *repo = NULL;
4134 struct got_reflist_head refs;
4135 struct got_worktree *worktree = NULL;
4136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4137 struct got_object_id *commit_id = NULL;
4138 char *commit_id_str = NULL;
4140 struct tog_view *view;
4142 SIMPLEQ_INIT(&refs);
4145 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4150 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4153 commit_id_str = optarg;
4156 repo_path = realpath(optarg, NULL);
4157 if (repo_path == NULL)
4158 return got_error_from_errno2("realpath",
4175 cwd = getcwd(NULL, 0);
4177 error = got_error_from_errno("getcwd");
4180 if (repo_path == NULL) {
4181 error = got_worktree_open(&worktree, cwd);
4182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4188 strdup(got_worktree_get_repo_path(worktree));
4189 if (repo_path == NULL)
4190 error = got_error_from_errno("strdup");
4194 repo_path = strdup(cwd);
4195 if (repo_path == NULL) {
4196 error = got_error_from_errno("strdup");
4204 error = got_repo_open(&repo, repo_path, NULL);
4208 error = apply_unveil(got_repo_get_path(repo), NULL);
4213 const char *prefix = got_worktree_get_path_prefix(worktree);
4214 char *p, *worktree_subdir = cwd +
4215 strlen(got_worktree_get_root_path(worktree));
4216 if (asprintf(&p, "%s%s%s%s%s",
4217 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4218 worktree_subdir, worktree_subdir[0] ? "/" : "",
4220 error = got_error_from_errno("asprintf");
4223 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4226 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4231 if (commit_id_str == NULL) {
4232 struct got_reference *head_ref;
4233 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4236 error = got_ref_resolve(&commit_id, repo, head_ref);
4237 got_ref_close(head_ref);
4239 error = got_repo_match_object_id(&commit_id, NULL,
4240 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4245 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4249 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4251 error = got_error_from_errno("view_open");
4254 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4258 /* Release work tree lock. */
4259 got_worktree_close(worktree);
4262 error = view_loop(view);
4268 got_worktree_close(worktree);
4270 got_repo_close(repo);
4271 got_ref_list_free(&refs);
4275 static const struct got_error *
4276 draw_tree_entries(struct tog_view *view,
4277 struct got_tree_entry **first_displayed_entry,
4278 struct got_tree_entry **last_displayed_entry,
4279 struct got_tree_entry **selected_entry, int *ndisplayed,
4280 const char *label, int show_ids, const char *parent_path,
4281 struct got_tree_object *tree, int selected, int limit,
4282 int isroot, struct tog_colors *colors)
4284 const struct got_error *err = NULL;
4285 struct got_tree_entry *te;
4287 struct tog_color *tc;
4288 int width, n, i, nentries;
4292 werase(view->window);
4297 err = format_line(&wline, &width, label, view->ncols, 0);
4300 if (view_needs_focus_indication(view))
4301 wstandout(view->window);
4302 tc = get_color(colors, TOG_COLOR_COMMIT);
4304 wattr_on(view->window,
4305 COLOR_PAIR(tc->colorpair), NULL);
4306 waddwstr(view->window, wline);
4308 wattr_off(view->window,
4309 COLOR_PAIR(tc->colorpair), NULL);
4310 if (view_needs_focus_indication(view))
4311 wstandend(view->window);
4314 if (width < view->ncols - 1)
4315 waddch(view->window, '\n');
4318 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4321 waddwstr(view->window, wline);
4324 if (width < view->ncols - 1)
4325 waddch(view->window, '\n');
4328 waddch(view->window, '\n');
4332 if (*first_displayed_entry == NULL) {
4333 te = got_object_tree_get_first_entry(tree);
4334 if (selected == 0) {
4336 wstandout(view->window);
4337 *selected_entry = NULL;
4339 waddstr(view->window, " ..\n"); /* parent directory */
4340 if (selected == 0 && view->focussed)
4341 wstandend(view->window);
4348 te = *first_displayed_entry;
4351 nentries = got_object_tree_get_nentries(tree);
4352 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4353 char *line = NULL, *id_str = NULL;
4354 const char *modestr = "";
4357 te = got_object_tree_get_entry(tree, i);
4358 mode = got_tree_entry_get_mode(te);
4361 err = got_object_id_str(&id_str,
4362 got_tree_entry_get_id(te));
4364 return got_error_from_errno(
4365 "got_object_id_str");
4367 if (got_object_tree_entry_is_submodule(te))
4369 else if (S_ISLNK(mode))
4371 else if (S_ISDIR(mode))
4373 else if (mode & S_IXUSR)
4375 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4376 got_tree_entry_get_name(te), modestr) == -1) {
4378 return got_error_from_errno("asprintf");
4381 err = format_line(&wline, &width, line, view->ncols, 0);
4386 if (n == selected) {
4388 wstandout(view->window);
4389 *selected_entry = te;
4391 tc = match_color(colors, line);
4393 wattr_on(view->window,
4394 COLOR_PAIR(tc->colorpair), NULL);
4395 waddwstr(view->window, wline);
4397 wattr_off(view->window,
4398 COLOR_PAIR(tc->colorpair), NULL);
4399 if (width < view->ncols - 1)
4400 waddch(view->window, '\n');
4401 if (n == selected && view->focussed)
4402 wstandend(view->window);
4408 *last_displayed_entry = te;
4417 tree_scroll_up(struct tog_view *view,
4418 struct got_tree_entry **first_displayed_entry, int maxscroll,
4419 struct got_tree_object *tree, int isroot)
4421 struct got_tree_entry *te;
4424 if (*first_displayed_entry == NULL)
4427 te = got_object_tree_get_entry(tree, 0);
4428 if (*first_displayed_entry == te) {
4430 *first_displayed_entry = NULL;
4435 while (*first_displayed_entry && i < maxscroll) {
4436 *first_displayed_entry = got_tree_entry_get_prev(tree,
4437 *first_displayed_entry);
4440 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4441 *first_displayed_entry = NULL;
4445 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4446 struct got_tree_entry *last_displayed_entry,
4447 struct got_tree_object *tree)
4449 struct got_tree_entry *next, *last;
4452 if (*first_displayed_entry)
4453 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4455 next = got_object_tree_get_first_entry(tree);
4457 last = last_displayed_entry;
4458 while (next && last && n++ < maxscroll) {
4459 last = got_tree_entry_get_next(tree, last);
4461 *first_displayed_entry = next;
4462 next = got_tree_entry_get_next(tree, next);
4468 static const struct got_error *
4469 tree_entry_path(char **path, struct tog_parent_trees *parents,
4470 struct got_tree_entry *te)
4472 const struct got_error *err = NULL;
4473 struct tog_parent_tree *pt;
4474 size_t len = 2; /* for leading slash and NUL */
4476 TAILQ_FOREACH(pt, parents, entry)
4477 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4480 len += strlen(got_tree_entry_get_name(te));
4482 *path = calloc(1, len);
4484 return got_error_from_errno("calloc");
4487 pt = TAILQ_LAST(parents, tog_parent_trees);
4489 const char *name = got_tree_entry_get_name(pt->selected_entry);
4490 if (strlcat(*path, name, len) >= len) {
4491 err = got_error(GOT_ERR_NO_SPACE);
4494 if (strlcat(*path, "/", len) >= len) {
4495 err = got_error(GOT_ERR_NO_SPACE);
4498 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4501 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4502 err = got_error(GOT_ERR_NO_SPACE);
4514 static const struct got_error *
4515 blame_tree_entry(struct tog_view **new_view, int begin_x,
4516 struct got_tree_entry *te, struct tog_parent_trees *parents,
4517 struct got_object_id *commit_id, struct got_reflist_head *refs,
4518 struct got_repository *repo)
4520 const struct got_error *err = NULL;
4522 struct tog_view *blame_view;
4526 err = tree_entry_path(&path, parents, te);
4530 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4531 if (blame_view == NULL) {
4532 err = got_error_from_errno("view_open");
4536 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4538 if (err->code == GOT_ERR_CANCELLED)
4540 view_close(blame_view);
4542 *new_view = blame_view;
4548 static const struct got_error *
4549 log_tree_entry(struct tog_view **new_view, int begin_x,
4550 struct got_tree_entry *te, struct tog_parent_trees *parents,
4551 struct got_object_id *commit_id, struct got_reflist_head *refs,
4552 struct got_repository *repo)
4554 struct tog_view *log_view;
4555 const struct got_error *err = NULL;
4560 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4561 if (log_view == NULL)
4562 return got_error_from_errno("view_open");
4564 err = tree_entry_path(&path, parents, te);
4568 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4570 view_close(log_view);
4572 *new_view = log_view;
4577 static const struct got_error *
4578 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4579 struct got_object_id *commit_id, struct got_reflist_head *refs,
4580 struct got_repository *repo)
4582 const struct got_error *err = NULL;
4583 char *commit_id_str = NULL;
4584 struct tog_tree_view_state *s = &view->state.tree;
4586 TAILQ_INIT(&s->parents);
4588 err = got_object_id_str(&commit_id_str, commit_id);
4592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4593 err = got_error_from_errno("asprintf");
4597 s->root = s->tree = root;
4598 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4599 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4600 s->commit_id = got_object_id_dup(commit_id);
4601 if (s->commit_id == NULL) {
4602 err = got_error_from_errno("got_object_id_dup");
4608 SIMPLEQ_INIT(&s->colors);
4610 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4611 err = add_color(&s->colors, "\\$$",
4612 TOG_COLOR_TREE_SUBMODULE,
4613 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4616 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4617 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4619 free_colors(&s->colors);
4622 err = add_color(&s->colors, "/$",
4623 TOG_COLOR_TREE_DIRECTORY,
4624 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4626 free_colors(&s->colors);
4630 err = add_color(&s->colors, "\\*$",
4631 TOG_COLOR_TREE_EXECUTABLE,
4632 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4634 free_colors(&s->colors);
4638 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4639 get_color_value("TOG_COLOR_COMMIT"));
4641 free_colors(&s->colors);
4646 view->show = show_tree_view;
4647 view->input = input_tree_view;
4648 view->close = close_tree_view;
4649 view->search_start = search_start_tree_view;
4650 view->search_next = search_next_tree_view;
4652 free(commit_id_str);
4654 free(s->tree_label);
4655 s->tree_label = NULL;
4660 static const struct got_error *
4661 close_tree_view(struct tog_view *view)
4663 struct tog_tree_view_state *s = &view->state.tree;
4665 free_colors(&s->colors);
4666 free(s->tree_label);
4667 s->tree_label = NULL;
4669 s->commit_id = NULL;
4670 while (!TAILQ_EMPTY(&s->parents)) {
4671 struct tog_parent_tree *parent;
4672 parent = TAILQ_FIRST(&s->parents);
4673 TAILQ_REMOVE(&s->parents, parent, entry);
4677 if (s->tree != s->root)
4678 got_object_tree_close(s->tree);
4679 got_object_tree_close(s->root);
4684 static const struct got_error *
4685 search_start_tree_view(struct tog_view *view)
4687 struct tog_tree_view_state *s = &view->state.tree;
4689 s->matched_entry = NULL;
4694 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4696 regmatch_t regmatch;
4698 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4702 static const struct got_error *
4703 search_next_tree_view(struct tog_view *view)
4705 struct tog_tree_view_state *s = &view->state.tree;
4706 struct got_tree_entry *te = NULL;
4708 if (!view->searching) {
4709 view->search_next_done = 1;
4713 if (s->matched_entry) {
4714 if (view->searching == TOG_SEARCH_FORWARD) {
4715 if (s->selected_entry)
4716 te = got_tree_entry_get_next(s->tree,
4719 te = got_object_tree_get_first_entry(s->tree);
4721 if (s->selected_entry == NULL)
4722 te = got_object_tree_get_last_entry(s->tree);
4724 te = got_tree_entry_get_prev(s->tree,
4728 if (view->searching == TOG_SEARCH_FORWARD)
4729 te = got_object_tree_get_first_entry(s->tree);
4731 te = got_object_tree_get_last_entry(s->tree);
4736 if (s->matched_entry == NULL) {
4737 view->search_next_done = 1;
4740 if (view->searching == TOG_SEARCH_FORWARD)
4741 te = got_object_tree_get_first_entry(s->tree);
4743 te = got_object_tree_get_last_entry(s->tree);
4746 if (match_tree_entry(te, &view->regex)) {
4747 view->search_next_done = 1;
4748 s->matched_entry = te;
4752 if (view->searching == TOG_SEARCH_FORWARD)
4753 te = got_tree_entry_get_next(s->tree, te);
4755 te = got_tree_entry_get_prev(s->tree, te);
4758 if (s->matched_entry) {
4759 s->first_displayed_entry = s->matched_entry;
4766 static const struct got_error *
4767 show_tree_view(struct tog_view *view)
4769 const struct got_error *err = NULL;
4770 struct tog_tree_view_state *s = &view->state.tree;
4773 err = tree_entry_path(&parent_path, &s->parents, NULL);
4777 err = draw_tree_entries(view, &s->first_displayed_entry,
4778 &s->last_displayed_entry, &s->selected_entry,
4779 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4780 s->tree, s->selected, view->nlines, s->tree == s->root,
4788 static const struct got_error *
4789 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4790 struct tog_view **focus_view, struct tog_view *view, int ch)
4792 const struct got_error *err = NULL;
4793 struct tog_tree_view_state *s = &view->state.tree;
4794 struct tog_view *log_view;
4795 int begin_x = 0, nscrolled;
4799 s->show_ids = !s->show_ids;
4802 if (!s->selected_entry)
4804 if (view_is_parent_view(view))
4805 begin_x = view_split_begin_x(view->begin_x);
4806 err = log_tree_entry(&log_view, begin_x,
4807 s->selected_entry, &s->parents,
4808 s->commit_id, s->refs, s->repo);
4809 if (view_is_parent_view(view)) {
4810 err = view_close_child(view);
4813 err = view_set_child(view, log_view);
4815 view_close(log_view);
4818 *focus_view = log_view;
4819 view->child_focussed = 1;
4821 *new_view = log_view;
4825 if (s->selected > 0) {
4827 if (s->selected == 0)
4830 if (s->selected > 0)
4832 tree_scroll_up(view, &s->first_displayed_entry, 1,
4833 s->tree, s->tree == s->root);
4836 tree_scroll_up(view, &s->first_displayed_entry,
4837 MAX(0, view->nlines - 4 - s->selected), s->tree,
4838 s->tree == s->root);
4840 if (got_object_tree_get_first_entry(s->tree) ==
4841 s->first_displayed_entry && s->tree != s->root)
4842 s->first_displayed_entry = NULL;
4846 if (s->selected < s->ndisplayed - 1) {
4850 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4852 /* can't scroll any further */
4854 tree_scroll_down(&s->first_displayed_entry, 1,
4855 s->last_displayed_entry, s->tree);
4858 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4860 /* can't scroll any further; move cursor down */
4861 if (s->selected < s->ndisplayed - 1)
4862 s->selected = s->ndisplayed - 1;
4865 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4866 view->nlines, s->last_displayed_entry, s->tree);
4867 if (nscrolled < view->nlines) {
4869 struct got_tree_entry *te;
4870 te = s->first_displayed_entry;
4873 te = got_tree_entry_get_next(s->tree, te);
4875 s->selected = ndisplayed - 1;
4881 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4882 struct tog_parent_tree *parent;
4883 /* user selected '..' */
4884 if (s->tree == s->root)
4886 parent = TAILQ_FIRST(&s->parents);
4887 TAILQ_REMOVE(&s->parents, parent,
4889 got_object_tree_close(s->tree);
4890 s->tree = parent->tree;
4891 s->first_displayed_entry =
4892 parent->first_displayed_entry;
4894 parent->selected_entry;
4895 s->selected = parent->selected;
4897 } else if (S_ISDIR(got_tree_entry_get_mode(
4898 s->selected_entry))) {
4899 struct got_tree_object *subtree;
4900 err = got_object_open_as_tree(&subtree, s->repo,
4901 got_tree_entry_get_id(s->selected_entry));
4904 err = tree_view_visit_subtree(subtree, s);
4906 got_object_tree_close(subtree);
4909 } else if (S_ISREG(got_tree_entry_get_mode(
4910 s->selected_entry))) {
4911 struct tog_view *blame_view;
4912 int begin_x = view_is_parent_view(view) ?
4913 view_split_begin_x(view->begin_x) : 0;
4915 err = blame_tree_entry(&blame_view, begin_x,
4916 s->selected_entry, &s->parents,
4917 s->commit_id, s->refs, s->repo);
4920 if (view_is_parent_view(view)) {
4921 err = view_close_child(view);
4924 err = view_set_child(view, blame_view);
4926 view_close(blame_view);
4929 *focus_view = blame_view;
4930 view->child_focussed = 1;
4932 *new_view = blame_view;
4936 if (s->selected > view->nlines)
4937 s->selected = s->ndisplayed - 1;
4950 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4955 static const struct got_error *
4956 cmd_tree(int argc, char *argv[])
4958 const struct got_error *error;
4959 struct got_repository *repo = NULL;
4960 struct got_reflist_head refs;
4961 char *repo_path = NULL;
4962 struct got_object_id *commit_id = NULL;
4963 char *commit_id_arg = NULL;
4964 struct got_commit_object *commit = NULL;
4965 struct got_tree_object *tree = NULL;
4967 struct tog_view *view;
4969 SIMPLEQ_INIT(&refs);
4972 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4977 while ((ch = getopt(argc, argv, "c:")) != -1) {
4980 commit_id_arg = optarg;
4992 struct got_worktree *worktree;
4993 char *cwd = getcwd(NULL, 0);
4995 return got_error_from_errno("getcwd");
4996 error = got_worktree_open(&worktree, cwd);
4997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5002 strdup(got_worktree_get_repo_path(worktree));
5003 got_worktree_close(worktree);
5006 if (repo_path == NULL) {
5007 error = got_error_from_errno("strdup");
5010 } else if (argc == 1) {
5011 repo_path = realpath(argv[0], NULL);
5012 if (repo_path == NULL)
5013 return got_error_from_errno2("realpath", argv[0]);
5019 error = got_repo_open(&repo, repo_path, NULL);
5023 error = apply_unveil(got_repo_get_path(repo), NULL);
5027 error = got_repo_match_object_id(&commit_id, NULL,
5028 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5029 GOT_OBJ_TYPE_COMMIT, 1, repo);
5033 error = got_object_open_as_commit(&commit, repo, commit_id);
5037 error = got_object_open_as_tree(&tree, repo,
5038 got_object_commit_get_tree_id(commit));
5042 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5046 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5048 error = got_error_from_errno("view_open");
5051 error = open_tree_view(view, tree, commit_id, &refs, repo);
5054 error = view_loop(view);
5059 got_object_commit_close(commit);
5061 got_object_tree_close(tree);
5063 got_repo_close(repo);
5064 got_ref_list_free(&refs);
5073 fprintf(stderr, "commands:");
5074 for (i = 0; i < nitems(tog_commands); i++) {
5075 struct tog_cmd *cmd = &tog_commands[i];
5076 fprintf(stderr, " %s", cmd->name);
5078 fputc('\n', stderr);
5084 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5092 make_argv(const char *arg0, const char *arg1)
5095 int argc = (arg1 == NULL ? 1 : 2);
5097 argv = calloc(argc, sizeof(char *));
5100 argv[0] = strdup(arg0);
5101 if (argv[0] == NULL)
5104 argv[1] = strdup(arg1);
5105 if (argv[1] == NULL)
5113 main(int argc, char *argv[])
5115 const struct got_error *error = NULL;
5116 struct tog_cmd *cmd = NULL;
5117 int ch, hflag = 0, Vflag = 0;
5118 char **cmd_argv = NULL;
5119 static struct option longopts[] = {
5120 { "version", no_argument, NULL, 'V' },
5124 setlocale(LC_CTYPE, "");
5126 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5146 got_version_print_str();
5153 /* Build an argument vector which runs a default command. */
5154 cmd = &tog_commands[0];
5155 cmd_argv = make_argv(cmd->name, NULL);
5160 /* Did the user specific a command? */
5161 for (i = 0; i < nitems(tog_commands); i++) {
5162 if (strncmp(tog_commands[i].name, argv[0],
5163 strlen(argv[0])) == 0) {
5164 cmd = &tog_commands[i];
5170 fprintf(stderr, "%s: unknown command '%s'\n",
5171 getprogname(), argv[0]);
5180 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5184 if (error && error->code != GOT_ERR_CANCELLED)
5185 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);