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;
2943 if (id1 != NULL && id2 != NULL) {
2945 err = got_object_get_type(&type1, repo, id1);
2948 err = got_object_get_type(&type2, repo, id2);
2953 return got_error(GOT_ERR_OBJ_TYPE);
2957 view->state.diff.id1 = got_object_id_dup(id1);
2958 if (view->state.diff.id1 == NULL)
2959 return got_error_from_errno("got_object_id_dup");
2961 view->state.diff.id1 = NULL;
2963 view->state.diff.id2 = got_object_id_dup(id2);
2964 if (view->state.diff.id2 == NULL) {
2965 free(view->state.diff.id1);
2966 view->state.diff.id1 = NULL;
2967 return got_error_from_errno("got_object_id_dup");
2969 view->state.diff.f = NULL;
2970 view->state.diff.first_displayed_line = 1;
2971 view->state.diff.last_displayed_line = view->nlines;
2972 view->state.diff.diff_context = 3;
2973 view->state.diff.log_view = log_view;
2974 view->state.diff.repo = repo;
2975 view->state.diff.refs = refs;
2976 SIMPLEQ_INIT(&view->state.diff.colors);
2978 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2979 err = add_color(&view->state.diff.colors,
2980 "^-", TOG_COLOR_DIFF_MINUS,
2981 get_color_value("TOG_COLOR_DIFF_MINUS"));
2984 err = add_color(&view->state.diff.colors, "^\\+",
2985 TOG_COLOR_DIFF_PLUS,
2986 get_color_value("TOG_COLOR_DIFF_PLUS"));
2988 free_colors(&view->state.diff.colors);
2991 err = add_color(&view->state.diff.colors,
2992 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2993 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2995 free_colors(&view->state.diff.colors);
2999 err = add_color(&view->state.diff.colors,
3000 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3001 get_color_value("TOG_COLOR_DIFF_META"));
3003 free_colors(&view->state.diff.colors);
3007 err = add_color(&view->state.diff.colors,
3008 "^(from|via): ", TOG_COLOR_AUTHOR,
3009 get_color_value("TOG_COLOR_AUTHOR"));
3011 free_colors(&view->state.diff.colors);
3015 err = add_color(&view->state.diff.colors,
3016 "^date: ", TOG_COLOR_DATE,
3017 get_color_value("TOG_COLOR_DATE"));
3019 free_colors(&view->state.diff.colors);
3024 if (log_view && view_is_splitscreen(view))
3025 show_log_view(log_view); /* draw vborder */
3026 diff_view_indicate_progress(view);
3028 err = create_diff(&view->state.diff);
3030 free(view->state.diff.id1);
3031 view->state.diff.id1 = NULL;
3032 free(view->state.diff.id2);
3033 view->state.diff.id2 = NULL;
3037 view->show = show_diff_view;
3038 view->input = input_diff_view;
3039 view->close = close_diff_view;
3044 static const struct got_error *
3045 close_diff_view(struct tog_view *view)
3047 const struct got_error *err = NULL;
3049 free(view->state.diff.id1);
3050 view->state.diff.id1 = NULL;
3051 free(view->state.diff.id2);
3052 view->state.diff.id2 = NULL;
3053 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3054 err = got_error_from_errno("fclose");
3055 free_colors(&view->state.diff.colors);
3059 static const struct got_error *
3060 show_diff_view(struct tog_view *view)
3062 const struct got_error *err;
3063 struct tog_diff_view_state *s = &view->state.diff;
3064 char *id_str1 = NULL, *id_str2, *header;
3067 err = got_object_id_str(&id_str1, s->id1);
3071 err = got_object_id_str(&id_str2, s->id2);
3075 if (asprintf(&header, "diff %s %s",
3076 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3077 err = got_error_from_errno("asprintf");
3085 return draw_file(view, s->f, &s->first_displayed_line,
3086 &s->last_displayed_line, &s->eof, view->nlines,
3087 header, &s->colors);
3090 static const struct got_error *
3091 set_selected_commit(struct tog_diff_view_state *s,
3092 struct commit_queue_entry *entry)
3094 const struct got_error *err;
3095 const struct got_object_id_queue *parent_ids;
3096 struct got_commit_object *selected_commit;
3097 struct got_object_qid *pid;
3100 s->id2 = got_object_id_dup(entry->id);
3102 return got_error_from_errno("got_object_id_dup");
3104 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3107 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3109 pid = SIMPLEQ_FIRST(parent_ids);
3110 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3111 got_object_commit_close(selected_commit);
3115 static const struct got_error *
3116 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3117 struct tog_view **focus_view, struct tog_view *view, int ch)
3119 const struct got_error *err = NULL;
3120 struct tog_diff_view_state *s = &view->state.diff;
3121 struct tog_log_view_state *ls;
3122 struct commit_queue_entry *entry;
3128 if (s->first_displayed_line > 1)
3129 s->first_displayed_line--;
3133 if (s->first_displayed_line == 1)
3136 while (i++ < view->nlines - 1 &&
3137 s->first_displayed_line > 1)
3138 s->first_displayed_line--;
3143 s->first_displayed_line++;
3151 while (!s->eof && i++ < view->nlines - 1) {
3153 line = parse_next_line(s->f, NULL);
3154 s->first_displayed_line++;
3160 if (s->diff_context > 0) {
3162 diff_view_indicate_progress(view);
3163 err = create_diff(s);
3167 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3169 diff_view_indicate_progress(view);
3170 err = create_diff(s);
3175 if (s->log_view == NULL)
3177 ls = &s->log_view->state.log;
3178 entry = TAILQ_PREV(ls->selected_entry,
3179 commit_queue_head, entry);
3183 err = input_log_view(NULL, NULL, NULL, s->log_view,
3188 err = set_selected_commit(s, entry);
3192 s->first_displayed_line = 1;
3193 s->last_displayed_line = view->nlines;
3195 diff_view_indicate_progress(view);
3196 err = create_diff(s);
3200 if (s->log_view == NULL)
3202 ls = &s->log_view->state.log;
3204 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3205 ls->thread_args.commits_needed++;
3207 /* Display "loading..." in log view. */
3208 show_log_view(s->log_view);
3212 err = trigger_log_thread(1 /* load_all */,
3213 &ls->thread_args.commits_needed,
3214 &ls->thread_args.log_complete,
3215 &ls->thread_args.need_commits);
3219 err = input_log_view(NULL, NULL, NULL, s->log_view,
3224 entry = TAILQ_NEXT(ls->selected_entry, entry);
3228 err = set_selected_commit(s, entry);
3232 s->first_displayed_line = 1;
3233 s->last_displayed_line = view->nlines;
3235 diff_view_indicate_progress(view);
3236 err = create_diff(s);
3245 static const struct got_error *
3246 cmd_diff(int argc, char *argv[])
3248 const struct got_error *error = NULL;
3249 struct got_repository *repo = NULL;
3250 struct got_reflist_head refs;
3251 struct got_object_id *id1 = NULL, *id2 = NULL;
3252 char *repo_path = NULL;
3253 char *id_str1 = NULL, *id_str2 = NULL;
3255 struct tog_view *view;
3257 SIMPLEQ_INIT(&refs);
3260 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3265 while ((ch = getopt(argc, argv, "")) != -1) {
3277 usage_diff(); /* TODO show local worktree changes */
3278 } else if (argc == 2) {
3279 repo_path = getcwd(NULL, 0);
3280 if (repo_path == NULL)
3281 return got_error_from_errno("getcwd");
3284 } else if (argc == 3) {
3285 repo_path = realpath(argv[0], NULL);
3286 if (repo_path == NULL)
3287 return got_error_from_errno2("realpath", argv[0]);
3295 error = got_repo_open(&repo, repo_path, NULL);
3299 error = apply_unveil(got_repo_get_path(repo), NULL);
3303 error = got_repo_match_object_id_prefix(&id1, id_str1,
3304 GOT_OBJ_TYPE_ANY, repo);
3308 error = got_repo_match_object_id_prefix(&id2, id_str2,
3309 GOT_OBJ_TYPE_ANY, repo);
3313 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3317 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3319 error = got_error_from_errno("view_open");
3322 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3325 error = view_loop(view);
3329 got_repo_close(repo);
3330 got_ref_list_free(&refs);
3338 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3343 struct tog_blame_line {
3345 struct got_object_id *id;
3348 static const struct got_error *
3349 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3350 const char *path, struct tog_blame_line *lines, int nlines,
3351 int blame_complete, int selected_line, int *first_displayed_line,
3352 int *last_displayed_line, int *eof, int max_lines,
3353 struct tog_colors *colors)
3355 const struct got_error *err;
3356 int lineno = 0, nprinted = 0;
3361 struct tog_blame_line *blame_line;
3362 struct got_object_id *prev_id = NULL;
3364 struct tog_color *tc;
3366 err = got_object_id_str(&id_str, id);
3371 werase(view->window);
3373 if (asprintf(&line, "commit %s", id_str) == -1) {
3374 err = got_error_from_errno("asprintf");
3379 err = format_line(&wline, &width, line, view->ncols, 0);
3384 if (view_needs_focus_indication(view))
3385 wstandout(view->window);
3386 tc = get_color(colors, TOG_COLOR_COMMIT);
3388 wattr_on(view->window,
3389 COLOR_PAIR(tc->colorpair), NULL);
3390 waddwstr(view->window, wline);
3392 wattr_off(view->window,
3393 COLOR_PAIR(tc->colorpair), NULL);
3394 if (view_needs_focus_indication(view))
3395 wstandend(view->window);
3398 if (width < view->ncols - 1)
3399 waddch(view->window, '\n');
3401 if (asprintf(&line, "[%d/%d] %s%s",
3402 *first_displayed_line - 1 + selected_line, nlines,
3403 blame_complete ? "" : "annotating... ", path) == -1) {
3405 return got_error_from_errno("asprintf");
3408 err = format_line(&wline, &width, line, view->ncols, 0);
3413 waddwstr(view->window, wline);
3416 if (width < view->ncols - 1)
3417 waddch(view->window, '\n');
3420 while (nprinted < max_lines - 2) {
3421 line = parse_next_line(f, &len);
3426 if (++lineno < *first_displayed_line) {
3431 if (view->ncols <= 9) {
3433 wline = wcsdup(L"");
3435 err = got_error_from_errno("wcsdup");
3437 err = format_line(&wline, &width, line,
3438 view->ncols - 9, 9);
3446 if (view->focussed && nprinted == selected_line - 1)
3447 wstandout(view->window);
3450 blame_line = &lines[lineno - 1];
3451 if (blame_line->annotated && prev_id &&
3452 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3454 nprinted == selected_line - 1)) {
3455 waddstr(view->window, " ");
3456 } else if (blame_line->annotated) {
3458 err = got_object_id_str(&id_str, blame_line->id);
3464 tc = get_color(colors, TOG_COLOR_COMMIT);
3466 wattr_on(view->window,
3467 COLOR_PAIR(tc->colorpair), NULL);
3468 wprintw(view->window, "%.8s", id_str);
3470 wattr_off(view->window,
3471 COLOR_PAIR(tc->colorpair), NULL);
3473 prev_id = blame_line->id;
3475 waddstr(view->window, "........");
3479 waddstr(view->window, "........");
3483 if (view->focussed && nprinted == selected_line - 1)
3484 wstandend(view->window);
3485 waddstr(view->window, " ");
3487 waddwstr(view->window, wline);
3488 if (width <= view->ncols - 1)
3489 waddch(view->window, '\n');
3490 if (++nprinted == 1)
3491 *first_displayed_line = lineno;
3496 *last_displayed_line = lineno;
3503 static const struct got_error *
3504 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3506 const struct got_error *err = NULL;
3507 struct tog_blame_cb_args *a = arg;
3508 struct tog_blame_line *line;
3511 if (nlines != a->nlines ||
3512 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3513 return got_error(GOT_ERR_RANGE);
3515 errcode = pthread_mutex_lock(&tog_mutex);
3517 return got_error_set_errno(errcode, "pthread_mutex_lock");
3519 if (*a->quit) { /* user has quit the blame view */
3520 err = got_error(GOT_ERR_ITER_COMPLETED);
3525 goto done; /* no change in this commit */
3527 line = &a->lines[lineno - 1];
3528 if (line->annotated)
3531 line->id = got_object_id_dup(id);
3532 if (line->id == NULL) {
3533 err = got_error_from_errno("got_object_id_dup");
3536 line->annotated = 1;
3538 errcode = pthread_mutex_unlock(&tog_mutex);
3540 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3545 blame_thread(void *arg)
3547 const struct got_error *err;
3548 struct tog_blame_thread_args *ta = arg;
3549 struct tog_blame_cb_args *a = ta->cb_args;
3552 err = block_signals_used_by_main_thread();
3556 err = got_blame(ta->path, a->commit_id, ta->repo,
3557 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3558 if (err && err->code == GOT_ERR_CANCELLED)
3561 errcode = pthread_mutex_lock(&tog_mutex);
3563 return (void *)got_error_set_errno(errcode,
3564 "pthread_mutex_lock");
3566 got_repo_close(ta->repo);
3570 errcode = pthread_mutex_unlock(&tog_mutex);
3571 if (errcode && err == NULL)
3572 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3577 static struct got_object_id *
3578 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3579 int first_displayed_line, int selected_line)
3581 struct tog_blame_line *line;
3586 line = &lines[first_displayed_line - 1 + selected_line - 1];
3587 if (!line->annotated)
3593 static const struct got_error *
3594 stop_blame(struct tog_blame *blame)
3596 const struct got_error *err = NULL;
3599 if (blame->thread) {
3601 errcode = pthread_mutex_unlock(&tog_mutex);
3603 return got_error_set_errno(errcode,
3604 "pthread_mutex_unlock");
3605 errcode = pthread_join(blame->thread, (void **)&err);
3607 return got_error_set_errno(errcode, "pthread_join");
3608 errcode = pthread_mutex_lock(&tog_mutex);
3610 return got_error_set_errno(errcode,
3611 "pthread_mutex_lock");
3612 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3614 blame->thread = NULL;
3616 if (blame->thread_args.repo) {
3617 got_repo_close(blame->thread_args.repo);
3618 blame->thread_args.repo = NULL;
3621 if (fclose(blame->f) != 0 && err == NULL)
3622 err = got_error_from_errno("fclose");
3626 for (i = 0; i < blame->nlines; i++)
3627 free(blame->lines[i].id);
3629 blame->lines = NULL;
3631 free(blame->cb_args.commit_id);
3632 blame->cb_args.commit_id = NULL;
3637 static const struct got_error *
3638 cancel_blame_view(void *arg)
3640 const struct got_error *err = NULL;
3644 errcode = pthread_mutex_lock(&tog_mutex);
3646 return got_error_set_errno(errcode,
3647 "pthread_mutex_unlock");
3650 err = got_error(GOT_ERR_CANCELLED);
3652 errcode = pthread_mutex_unlock(&tog_mutex);
3654 return got_error_set_errno(errcode,
3655 "pthread_mutex_lock");
3660 static const struct got_error *
3661 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3662 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3663 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3664 struct got_repository *repo)
3666 const struct got_error *err = NULL;
3667 struct got_blob_object *blob = NULL;
3668 struct got_repository *thread_repo = NULL;
3669 struct got_object_id *obj_id = NULL;
3672 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3676 err = got_object_get_type(&obj_type, repo, obj_id);
3680 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3681 err = got_error(GOT_ERR_OBJ_TYPE);
3685 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3688 blame->f = got_opentemp();
3689 if (blame->f == NULL) {
3690 err = got_error_from_errno("got_opentemp");
3693 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3694 &blame->line_offsets, blame->f, blob);
3695 if (err || blame->nlines == 0)
3698 /* Don't include \n at EOF in the blame line count. */
3699 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3702 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3703 if (blame->lines == NULL) {
3704 err = got_error_from_errno("calloc");
3708 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3712 blame->cb_args.view = view;
3713 blame->cb_args.lines = blame->lines;
3714 blame->cb_args.nlines = blame->nlines;
3715 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3716 if (blame->cb_args.commit_id == NULL) {
3717 err = got_error_from_errno("got_object_id_dup");
3720 blame->cb_args.quit = done;
3722 blame->thread_args.path = path;
3723 blame->thread_args.repo = thread_repo;
3724 blame->thread_args.cb_args = &blame->cb_args;
3725 blame->thread_args.complete = blame_complete;
3726 blame->thread_args.cancel_cb = cancel_blame_view;
3727 blame->thread_args.cancel_arg = done;
3728 *blame_complete = 0;
3732 got_object_blob_close(blob);
3739 static const struct got_error *
3740 open_blame_view(struct tog_view *view, char *path,
3741 struct got_object_id *commit_id, struct got_reflist_head *refs,
3742 struct got_repository *repo)
3744 const struct got_error *err = NULL;
3745 struct tog_blame_view_state *s = &view->state.blame;
3747 SIMPLEQ_INIT(&s->blamed_commits);
3749 s->path = strdup(path);
3750 if (s->path == NULL)
3751 return got_error_from_errno("strdup");
3753 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3759 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3760 s->first_displayed_line = 1;
3761 s->last_displayed_line = view->nlines;
3762 s->selected_line = 1;
3763 s->blame_complete = 0;
3766 s->commit_id = commit_id;
3767 memset(&s->blame, 0, sizeof(s->blame));
3769 SIMPLEQ_INIT(&s->colors);
3770 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3771 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3772 get_color_value("TOG_COLOR_COMMIT"));
3777 view->show = show_blame_view;
3778 view->input = input_blame_view;
3779 view->close = close_blame_view;
3780 view->search_start = search_start_blame_view;
3781 view->search_next = search_next_blame_view;
3783 return run_blame(&s->blame, view, &s->blame_complete,
3784 &s->first_displayed_line, &s->last_displayed_line,
3785 &s->selected_line, &s->done, &s->eof, s->path,
3786 s->blamed_commit->id, s->repo);
3789 static const struct got_error *
3790 close_blame_view(struct tog_view *view)
3792 const struct got_error *err = NULL;
3793 struct tog_blame_view_state *s = &view->state.blame;
3795 if (s->blame.thread)
3796 err = stop_blame(&s->blame);
3798 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3799 struct got_object_qid *blamed_commit;
3800 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3801 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3802 got_object_qid_free(blamed_commit);
3806 free_colors(&s->colors);
3811 static const struct got_error *
3812 search_start_blame_view(struct tog_view *view)
3814 struct tog_blame_view_state *s = &view->state.blame;
3816 s->matched_line = 0;
3820 static const struct got_error *
3821 search_next_blame_view(struct tog_view *view)
3823 struct tog_blame_view_state *s = &view->state.blame;
3826 if (!view->searching) {
3827 view->search_next_done = 1;
3831 if (s->matched_line) {
3832 if (view->searching == TOG_SEARCH_FORWARD)
3833 lineno = s->matched_line + 1;
3835 lineno = s->matched_line - 1;
3837 if (view->searching == TOG_SEARCH_FORWARD)
3840 lineno = s->blame.nlines;
3848 if (lineno <= 0 || lineno > s->blame.nlines) {
3849 if (s->matched_line == 0) {
3850 view->search_next_done = 1;
3855 if (view->searching == TOG_SEARCH_FORWARD)
3858 lineno = s->blame.nlines;
3861 offset = s->blame.line_offsets[lineno - 1];
3862 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3864 return got_error_from_errno("fseeko");
3867 line = parse_next_line(s->blame.f, &len);
3868 if (line && match_line(line, &view->regex)) {
3869 view->search_next_done = 1;
3870 s->matched_line = lineno;
3875 if (view->searching == TOG_SEARCH_FORWARD)
3881 if (s->matched_line) {
3882 s->first_displayed_line = s->matched_line;
3883 s->selected_line = 1;
3889 static const struct got_error *
3890 show_blame_view(struct tog_view *view)
3892 const struct got_error *err = NULL;
3893 struct tog_blame_view_state *s = &view->state.blame;
3896 if (s->blame.thread == NULL) {
3897 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3898 &s->blame.thread_args);
3900 return got_error_set_errno(errcode, "pthread_create");
3902 halfdelay(1); /* fast refresh while annotating */
3905 if (s->blame_complete)
3906 halfdelay(10); /* disable fast refresh */
3908 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3909 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3910 s->selected_line, &s->first_displayed_line,
3911 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3917 static const struct got_error *
3918 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3919 struct tog_view **focus_view, struct tog_view *view, int ch)
3921 const struct got_error *err = NULL, *thread_err = NULL;
3922 struct tog_view *diff_view;
3923 struct tog_blame_view_state *s = &view->state.blame;
3932 if (s->selected_line > 1)
3934 else if (s->selected_line == 1 &&
3935 s->first_displayed_line > 1)
3936 s->first_displayed_line--;
3939 if (s->first_displayed_line == 1) {
3940 s->selected_line = 1;
3943 if (s->first_displayed_line > view->nlines - 2)
3944 s->first_displayed_line -=
3947 s->first_displayed_line = 1;
3951 if (s->selected_line < view->nlines - 2 &&
3952 s->first_displayed_line +
3953 s->selected_line <= s->blame.nlines)
3955 else if (s->last_displayed_line <
3957 s->first_displayed_line++;
3961 struct got_object_id *id = NULL;
3962 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3963 s->first_displayed_line, s->selected_line);
3967 struct got_commit_object *commit;
3968 struct got_object_qid *pid;
3969 struct got_object_id *blob_id = NULL;
3971 err = got_object_open_as_commit(&commit,
3975 pid = SIMPLEQ_FIRST(
3976 got_object_commit_get_parent_ids(commit));
3978 got_object_commit_close(commit);
3981 /* Check if path history ends here. */
3982 err = got_object_id_by_path(&blob_id, s->repo,
3985 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3987 got_object_commit_close(commit);
3990 err = got_object_get_type(&obj_type, s->repo,
3993 /* Can't blame non-blob type objects. */
3994 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3995 got_object_commit_close(commit);
3998 err = got_object_qid_alloc(&s->blamed_commit,
4000 got_object_commit_close(commit);
4002 if (got_object_id_cmp(id,
4003 s->blamed_commit->id) == 0)
4005 err = got_object_qid_alloc(&s->blamed_commit,
4011 thread_err = stop_blame(&s->blame);
4015 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4016 s->blamed_commit, entry);
4017 err = run_blame(&s->blame, view, &s->blame_complete,
4018 &s->first_displayed_line, &s->last_displayed_line,
4019 &s->selected_line, &s->done, &s->eof,
4020 s->path, s->blamed_commit->id, s->repo);
4026 struct got_object_qid *first;
4027 first = SIMPLEQ_FIRST(&s->blamed_commits);
4028 if (!got_object_id_cmp(first->id, s->commit_id))
4031 thread_err = stop_blame(&s->blame);
4035 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4036 got_object_qid_free(s->blamed_commit);
4038 SIMPLEQ_FIRST(&s->blamed_commits);
4039 err = run_blame(&s->blame, view, &s->blame_complete,
4040 &s->first_displayed_line, &s->last_displayed_line,
4041 &s->selected_line, &s->done, &s->eof, s->path,
4042 s->blamed_commit->id, s->repo);
4049 struct got_object_id *id = NULL;
4050 struct got_object_qid *pid;
4051 struct got_commit_object *commit = NULL;
4052 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4053 s->first_displayed_line, s->selected_line);
4056 err = got_object_open_as_commit(&commit, s->repo, id);
4059 pid = SIMPLEQ_FIRST(
4060 got_object_commit_get_parent_ids(commit));
4061 if (view_is_parent_view(view))
4062 begin_x = view_split_begin_x(view->begin_x);
4063 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4064 if (diff_view == NULL) {
4065 got_object_commit_close(commit);
4066 err = got_error_from_errno("view_open");
4069 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4070 id, NULL, s->refs, s->repo);
4071 got_object_commit_close(commit);
4073 view_close(diff_view);
4076 if (view_is_parent_view(view)) {
4077 err = view_close_child(view);
4080 err = view_set_child(view, diff_view);
4082 view_close(diff_view);
4085 *focus_view = diff_view;
4086 view->child_focussed = 1;
4088 *new_view = diff_view;
4095 if (s->last_displayed_line >= s->blame.nlines &&
4096 s->selected_line >= MIN(s->blame.nlines,
4097 view->nlines - 2)) {
4100 if (s->last_displayed_line >= s->blame.nlines &&
4101 s->selected_line < view->nlines - 2) {
4102 s->selected_line = MIN(s->blame.nlines,
4106 if (s->last_displayed_line + view->nlines - 2
4108 s->first_displayed_line +=
4111 s->first_displayed_line =
4116 if (s->selected_line > view->nlines - 2) {
4117 s->selected_line = MIN(s->blame.nlines,
4124 return thread_err ? thread_err : err;
4127 static const struct got_error *
4128 cmd_blame(int argc, char *argv[])
4130 const struct got_error *error;
4131 struct got_repository *repo = NULL;
4132 struct got_reflist_head refs;
4133 struct got_worktree *worktree = NULL;
4134 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4135 struct got_object_id *commit_id = NULL;
4136 char *commit_id_str = NULL;
4138 struct tog_view *view;
4140 SIMPLEQ_INIT(&refs);
4143 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4148 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4151 commit_id_str = optarg;
4154 repo_path = realpath(optarg, NULL);
4155 if (repo_path == NULL)
4156 return got_error_from_errno2("realpath",
4173 cwd = getcwd(NULL, 0);
4175 error = got_error_from_errno("getcwd");
4178 if (repo_path == NULL) {
4179 error = got_worktree_open(&worktree, cwd);
4180 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4186 strdup(got_worktree_get_repo_path(worktree));
4187 if (repo_path == NULL)
4188 error = got_error_from_errno("strdup");
4192 repo_path = strdup(cwd);
4193 if (repo_path == NULL) {
4194 error = got_error_from_errno("strdup");
4202 error = got_repo_open(&repo, repo_path, NULL);
4206 error = apply_unveil(got_repo_get_path(repo), NULL);
4211 const char *prefix = got_worktree_get_path_prefix(worktree);
4212 char *p, *worktree_subdir = cwd +
4213 strlen(got_worktree_get_root_path(worktree));
4214 if (asprintf(&p, "%s%s%s%s%s",
4215 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4216 worktree_subdir, worktree_subdir[0] ? "/" : "",
4218 error = got_error_from_errno("asprintf");
4221 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4224 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4229 if (commit_id_str == NULL) {
4230 struct got_reference *head_ref;
4231 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4234 error = got_ref_resolve(&commit_id, repo, head_ref);
4235 got_ref_close(head_ref);
4237 error = got_repo_match_object_id(&commit_id, NULL,
4238 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4243 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4247 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4249 error = got_error_from_errno("view_open");
4252 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4256 /* Release work tree lock. */
4257 got_worktree_close(worktree);
4260 error = view_loop(view);
4266 got_worktree_close(worktree);
4268 got_repo_close(repo);
4269 got_ref_list_free(&refs);
4273 static const struct got_error *
4274 draw_tree_entries(struct tog_view *view,
4275 struct got_tree_entry **first_displayed_entry,
4276 struct got_tree_entry **last_displayed_entry,
4277 struct got_tree_entry **selected_entry, int *ndisplayed,
4278 const char *label, int show_ids, const char *parent_path,
4279 struct got_tree_object *tree, int selected, int limit,
4280 int isroot, struct tog_colors *colors)
4282 const struct got_error *err = NULL;
4283 struct got_tree_entry *te;
4285 struct tog_color *tc;
4286 int width, n, i, nentries;
4290 werase(view->window);
4295 err = format_line(&wline, &width, label, view->ncols, 0);
4298 if (view_needs_focus_indication(view))
4299 wstandout(view->window);
4300 tc = get_color(colors, TOG_COLOR_COMMIT);
4302 wattr_on(view->window,
4303 COLOR_PAIR(tc->colorpair), NULL);
4304 waddwstr(view->window, wline);
4306 wattr_off(view->window,
4307 COLOR_PAIR(tc->colorpair), NULL);
4308 if (view_needs_focus_indication(view))
4309 wstandend(view->window);
4312 if (width < view->ncols - 1)
4313 waddch(view->window, '\n');
4316 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4319 waddwstr(view->window, wline);
4322 if (width < view->ncols - 1)
4323 waddch(view->window, '\n');
4326 waddch(view->window, '\n');
4330 if (*first_displayed_entry == NULL) {
4331 te = got_object_tree_get_first_entry(tree);
4332 if (selected == 0) {
4334 wstandout(view->window);
4335 *selected_entry = NULL;
4337 waddstr(view->window, " ..\n"); /* parent directory */
4338 if (selected == 0 && view->focussed)
4339 wstandend(view->window);
4346 te = *first_displayed_entry;
4349 nentries = got_object_tree_get_nentries(tree);
4350 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4351 char *line = NULL, *id_str = NULL;
4352 const char *modestr = "";
4355 te = got_object_tree_get_entry(tree, i);
4356 mode = got_tree_entry_get_mode(te);
4359 err = got_object_id_str(&id_str,
4360 got_tree_entry_get_id(te));
4362 return got_error_from_errno(
4363 "got_object_id_str");
4365 if (got_object_tree_entry_is_submodule(te))
4367 else if (S_ISLNK(mode))
4369 else if (S_ISDIR(mode))
4371 else if (mode & S_IXUSR)
4373 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4374 got_tree_entry_get_name(te), modestr) == -1) {
4376 return got_error_from_errno("asprintf");
4379 err = format_line(&wline, &width, line, view->ncols, 0);
4384 if (n == selected) {
4386 wstandout(view->window);
4387 *selected_entry = te;
4389 tc = match_color(colors, line);
4391 wattr_on(view->window,
4392 COLOR_PAIR(tc->colorpair), NULL);
4393 waddwstr(view->window, wline);
4395 wattr_off(view->window,
4396 COLOR_PAIR(tc->colorpair), NULL);
4397 if (width < view->ncols - 1)
4398 waddch(view->window, '\n');
4399 if (n == selected && view->focussed)
4400 wstandend(view->window);
4406 *last_displayed_entry = te;
4415 tree_scroll_up(struct tog_view *view,
4416 struct got_tree_entry **first_displayed_entry, int maxscroll,
4417 struct got_tree_object *tree, int isroot)
4419 struct got_tree_entry *te;
4422 if (*first_displayed_entry == NULL)
4425 te = got_object_tree_get_entry(tree, 0);
4426 if (*first_displayed_entry == te) {
4428 *first_displayed_entry = NULL;
4433 while (*first_displayed_entry && i < maxscroll) {
4434 *first_displayed_entry = got_tree_entry_get_prev(tree,
4435 *first_displayed_entry);
4438 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4439 *first_displayed_entry = NULL;
4443 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4444 struct got_tree_entry *last_displayed_entry,
4445 struct got_tree_object *tree)
4447 struct got_tree_entry *next, *last;
4450 if (*first_displayed_entry)
4451 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4453 next = got_object_tree_get_first_entry(tree);
4455 last = last_displayed_entry;
4456 while (next && last && n++ < maxscroll) {
4457 last = got_tree_entry_get_next(tree, last);
4459 *first_displayed_entry = next;
4460 next = got_tree_entry_get_next(tree, next);
4466 static const struct got_error *
4467 tree_entry_path(char **path, struct tog_parent_trees *parents,
4468 struct got_tree_entry *te)
4470 const struct got_error *err = NULL;
4471 struct tog_parent_tree *pt;
4472 size_t len = 2; /* for leading slash and NUL */
4474 TAILQ_FOREACH(pt, parents, entry)
4475 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4478 len += strlen(got_tree_entry_get_name(te));
4480 *path = calloc(1, len);
4482 return got_error_from_errno("calloc");
4485 pt = TAILQ_LAST(parents, tog_parent_trees);
4487 const char *name = got_tree_entry_get_name(pt->selected_entry);
4488 if (strlcat(*path, name, len) >= len) {
4489 err = got_error(GOT_ERR_NO_SPACE);
4492 if (strlcat(*path, "/", len) >= len) {
4493 err = got_error(GOT_ERR_NO_SPACE);
4496 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4499 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4500 err = got_error(GOT_ERR_NO_SPACE);
4512 static const struct got_error *
4513 blame_tree_entry(struct tog_view **new_view, int begin_x,
4514 struct got_tree_entry *te, struct tog_parent_trees *parents,
4515 struct got_object_id *commit_id, struct got_reflist_head *refs,
4516 struct got_repository *repo)
4518 const struct got_error *err = NULL;
4520 struct tog_view *blame_view;
4524 err = tree_entry_path(&path, parents, te);
4528 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4529 if (blame_view == NULL) {
4530 err = got_error_from_errno("view_open");
4534 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4536 if (err->code == GOT_ERR_CANCELLED)
4538 view_close(blame_view);
4540 *new_view = blame_view;
4546 static const struct got_error *
4547 log_tree_entry(struct tog_view **new_view, int begin_x,
4548 struct got_tree_entry *te, struct tog_parent_trees *parents,
4549 struct got_object_id *commit_id, struct got_reflist_head *refs,
4550 struct got_repository *repo)
4552 struct tog_view *log_view;
4553 const struct got_error *err = NULL;
4558 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4559 if (log_view == NULL)
4560 return got_error_from_errno("view_open");
4562 err = tree_entry_path(&path, parents, te);
4566 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4568 view_close(log_view);
4570 *new_view = log_view;
4575 static const struct got_error *
4576 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4577 struct got_object_id *commit_id, struct got_reflist_head *refs,
4578 struct got_repository *repo)
4580 const struct got_error *err = NULL;
4581 char *commit_id_str = NULL;
4582 struct tog_tree_view_state *s = &view->state.tree;
4584 TAILQ_INIT(&s->parents);
4586 err = got_object_id_str(&commit_id_str, commit_id);
4590 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4591 err = got_error_from_errno("asprintf");
4595 s->root = s->tree = root;
4596 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4597 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4598 s->commit_id = got_object_id_dup(commit_id);
4599 if (s->commit_id == NULL) {
4600 err = got_error_from_errno("got_object_id_dup");
4606 SIMPLEQ_INIT(&s->colors);
4608 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4609 err = add_color(&s->colors, "\\$$",
4610 TOG_COLOR_TREE_SUBMODULE,
4611 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4614 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4615 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4617 free_colors(&s->colors);
4620 err = add_color(&s->colors, "/$",
4621 TOG_COLOR_TREE_DIRECTORY,
4622 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4624 free_colors(&s->colors);
4628 err = add_color(&s->colors, "\\*$",
4629 TOG_COLOR_TREE_EXECUTABLE,
4630 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4632 free_colors(&s->colors);
4636 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4637 get_color_value("TOG_COLOR_COMMIT"));
4639 free_colors(&s->colors);
4644 view->show = show_tree_view;
4645 view->input = input_tree_view;
4646 view->close = close_tree_view;
4647 view->search_start = search_start_tree_view;
4648 view->search_next = search_next_tree_view;
4650 free(commit_id_str);
4652 free(s->tree_label);
4653 s->tree_label = NULL;
4658 static const struct got_error *
4659 close_tree_view(struct tog_view *view)
4661 struct tog_tree_view_state *s = &view->state.tree;
4663 free_colors(&s->colors);
4664 free(s->tree_label);
4665 s->tree_label = NULL;
4667 s->commit_id = NULL;
4668 while (!TAILQ_EMPTY(&s->parents)) {
4669 struct tog_parent_tree *parent;
4670 parent = TAILQ_FIRST(&s->parents);
4671 TAILQ_REMOVE(&s->parents, parent, entry);
4675 if (s->tree != s->root)
4676 got_object_tree_close(s->tree);
4677 got_object_tree_close(s->root);
4682 static const struct got_error *
4683 search_start_tree_view(struct tog_view *view)
4685 struct tog_tree_view_state *s = &view->state.tree;
4687 s->matched_entry = NULL;
4692 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4694 regmatch_t regmatch;
4696 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4700 static const struct got_error *
4701 search_next_tree_view(struct tog_view *view)
4703 struct tog_tree_view_state *s = &view->state.tree;
4704 struct got_tree_entry *te = NULL;
4706 if (!view->searching) {
4707 view->search_next_done = 1;
4711 if (s->matched_entry) {
4712 if (view->searching == TOG_SEARCH_FORWARD) {
4713 if (s->selected_entry)
4714 te = got_tree_entry_get_next(s->tree,
4717 te = got_object_tree_get_first_entry(s->tree);
4719 if (s->selected_entry == NULL)
4720 te = got_object_tree_get_last_entry(s->tree);
4722 te = got_tree_entry_get_prev(s->tree,
4726 if (view->searching == TOG_SEARCH_FORWARD)
4727 te = got_object_tree_get_first_entry(s->tree);
4729 te = got_object_tree_get_last_entry(s->tree);
4734 if (s->matched_entry == NULL) {
4735 view->search_next_done = 1;
4738 if (view->searching == TOG_SEARCH_FORWARD)
4739 te = got_object_tree_get_first_entry(s->tree);
4741 te = got_object_tree_get_last_entry(s->tree);
4744 if (match_tree_entry(te, &view->regex)) {
4745 view->search_next_done = 1;
4746 s->matched_entry = te;
4750 if (view->searching == TOG_SEARCH_FORWARD)
4751 te = got_tree_entry_get_next(s->tree, te);
4753 te = got_tree_entry_get_prev(s->tree, te);
4756 if (s->matched_entry) {
4757 s->first_displayed_entry = s->matched_entry;
4764 static const struct got_error *
4765 show_tree_view(struct tog_view *view)
4767 const struct got_error *err = NULL;
4768 struct tog_tree_view_state *s = &view->state.tree;
4771 err = tree_entry_path(&parent_path, &s->parents, NULL);
4775 err = draw_tree_entries(view, &s->first_displayed_entry,
4776 &s->last_displayed_entry, &s->selected_entry,
4777 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4778 s->tree, s->selected, view->nlines, s->tree == s->root,
4786 static const struct got_error *
4787 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4788 struct tog_view **focus_view, struct tog_view *view, int ch)
4790 const struct got_error *err = NULL;
4791 struct tog_tree_view_state *s = &view->state.tree;
4792 struct tog_view *log_view;
4793 int begin_x = 0, nscrolled;
4797 s->show_ids = !s->show_ids;
4800 if (!s->selected_entry)
4802 if (view_is_parent_view(view))
4803 begin_x = view_split_begin_x(view->begin_x);
4804 err = log_tree_entry(&log_view, begin_x,
4805 s->selected_entry, &s->parents,
4806 s->commit_id, s->refs, s->repo);
4807 if (view_is_parent_view(view)) {
4808 err = view_close_child(view);
4811 err = view_set_child(view, log_view);
4813 view_close(log_view);
4816 *focus_view = log_view;
4817 view->child_focussed = 1;
4819 *new_view = log_view;
4823 if (s->selected > 0) {
4825 if (s->selected == 0)
4828 if (s->selected > 0)
4830 tree_scroll_up(view, &s->first_displayed_entry, 1,
4831 s->tree, s->tree == s->root);
4834 tree_scroll_up(view, &s->first_displayed_entry,
4835 MAX(0, view->nlines - 4 - s->selected), s->tree,
4836 s->tree == s->root);
4838 if (got_object_tree_get_first_entry(s->tree) ==
4839 s->first_displayed_entry && s->tree != s->root)
4840 s->first_displayed_entry = NULL;
4844 if (s->selected < s->ndisplayed - 1) {
4848 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4850 /* can't scroll any further */
4852 tree_scroll_down(&s->first_displayed_entry, 1,
4853 s->last_displayed_entry, s->tree);
4856 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4858 /* can't scroll any further; move cursor down */
4859 if (s->selected < s->ndisplayed - 1)
4860 s->selected = s->ndisplayed - 1;
4863 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4864 view->nlines, s->last_displayed_entry, s->tree);
4865 if (nscrolled < view->nlines) {
4867 struct got_tree_entry *te;
4868 te = s->first_displayed_entry;
4871 te = got_tree_entry_get_next(s->tree, te);
4873 s->selected = ndisplayed - 1;
4879 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4880 struct tog_parent_tree *parent;
4881 /* user selected '..' */
4882 if (s->tree == s->root)
4884 parent = TAILQ_FIRST(&s->parents);
4885 TAILQ_REMOVE(&s->parents, parent,
4887 got_object_tree_close(s->tree);
4888 s->tree = parent->tree;
4889 s->first_displayed_entry =
4890 parent->first_displayed_entry;
4892 parent->selected_entry;
4893 s->selected = parent->selected;
4895 } else if (S_ISDIR(got_tree_entry_get_mode(
4896 s->selected_entry))) {
4897 struct got_tree_object *subtree;
4898 err = got_object_open_as_tree(&subtree, s->repo,
4899 got_tree_entry_get_id(s->selected_entry));
4902 err = tree_view_visit_subtree(subtree, s);
4904 got_object_tree_close(subtree);
4907 } else if (S_ISREG(got_tree_entry_get_mode(
4908 s->selected_entry))) {
4909 struct tog_view *blame_view;
4910 int begin_x = view_is_parent_view(view) ?
4911 view_split_begin_x(view->begin_x) : 0;
4913 err = blame_tree_entry(&blame_view, begin_x,
4914 s->selected_entry, &s->parents,
4915 s->commit_id, s->refs, s->repo);
4918 if (view_is_parent_view(view)) {
4919 err = view_close_child(view);
4922 err = view_set_child(view, blame_view);
4924 view_close(blame_view);
4927 *focus_view = blame_view;
4928 view->child_focussed = 1;
4930 *new_view = blame_view;
4934 if (s->selected > view->nlines)
4935 s->selected = s->ndisplayed - 1;
4948 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4953 static const struct got_error *
4954 cmd_tree(int argc, char *argv[])
4956 const struct got_error *error;
4957 struct got_repository *repo = NULL;
4958 struct got_reflist_head refs;
4959 char *repo_path = NULL;
4960 struct got_object_id *commit_id = NULL;
4961 char *commit_id_arg = NULL;
4962 struct got_commit_object *commit = NULL;
4963 struct got_tree_object *tree = NULL;
4965 struct tog_view *view;
4967 SIMPLEQ_INIT(&refs);
4970 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4975 while ((ch = getopt(argc, argv, "c:")) != -1) {
4978 commit_id_arg = optarg;
4990 struct got_worktree *worktree;
4991 char *cwd = getcwd(NULL, 0);
4993 return got_error_from_errno("getcwd");
4994 error = got_worktree_open(&worktree, cwd);
4995 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5000 strdup(got_worktree_get_repo_path(worktree));
5001 got_worktree_close(worktree);
5004 if (repo_path == NULL) {
5005 error = got_error_from_errno("strdup");
5008 } else if (argc == 1) {
5009 repo_path = realpath(argv[0], NULL);
5010 if (repo_path == NULL)
5011 return got_error_from_errno2("realpath", argv[0]);
5017 error = got_repo_open(&repo, repo_path, NULL);
5021 error = apply_unveil(got_repo_get_path(repo), NULL);
5025 error = got_repo_match_object_id(&commit_id, NULL,
5026 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5027 GOT_OBJ_TYPE_COMMIT, 1, repo);
5031 error = got_object_open_as_commit(&commit, repo, commit_id);
5035 error = got_object_open_as_tree(&tree, repo,
5036 got_object_commit_get_tree_id(commit));
5040 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5044 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5046 error = got_error_from_errno("view_open");
5049 error = open_tree_view(view, tree, commit_id, &refs, repo);
5052 error = view_loop(view);
5057 got_object_commit_close(commit);
5059 got_object_tree_close(tree);
5061 got_repo_close(repo);
5062 got_ref_list_free(&refs);
5071 fprintf(stderr, "commands:");
5072 for (i = 0; i < nitems(tog_commands); i++) {
5073 struct tog_cmd *cmd = &tog_commands[i];
5074 fprintf(stderr, " %s", cmd->name);
5076 fputc('\n', stderr);
5082 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5090 make_argv(const char *arg0, const char *arg1)
5093 int argc = (arg1 == NULL ? 1 : 2);
5095 argv = calloc(argc, sizeof(char *));
5098 argv[0] = strdup(arg0);
5099 if (argv[0] == NULL)
5102 argv[1] = strdup(arg1);
5103 if (argv[1] == NULL)
5111 main(int argc, char *argv[])
5113 const struct got_error *error = NULL;
5114 struct tog_cmd *cmd = NULL;
5115 int ch, hflag = 0, Vflag = 0;
5116 char **cmd_argv = NULL;
5117 static struct option longopts[] = {
5118 { "version", no_argument, NULL, 'V' },
5122 setlocale(LC_CTYPE, "");
5124 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5144 got_version_print_str();
5151 /* Build an argument vector which runs a default command. */
5152 cmd = &tog_commands[0];
5153 cmd_argv = make_argv(cmd->name, NULL);
5158 /* Did the user specific a command? */
5159 for (i = 0; i < nitems(tog_commands); i++) {
5160 if (strncmp(tog_commands[i].name, argv[0],
5161 strlen(argv[0])) == 0) {
5162 cmd = &tog_commands[i];
5168 fprintf(stderr, "%s: unknown command '%s'\n",
5169 getprogname(), argv[0]);
5178 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5182 if (error && error->code != GOT_ERR_CANCELLED)
5183 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);