2 * Copyright (c) 2018, 2019 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
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_opentemp.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
54 #include "got_worktree.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #define CTRL(x) ((x) & 0x1f)
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
112 struct commit_queue_head head;
116 SIMPLEQ_ENTRY(tog_color) entry;
120 SIMPLEQ_HEAD(tog_colors, tog_color);
122 static const struct got_error *
123 add_color(struct tog_colors *colors, const char *pattern,
124 int idx, short color)
126 const struct got_error *err = NULL;
127 struct tog_color *tc;
130 if (idx < 1 || idx > COLOR_PAIRS - 1)
133 init_pair(idx, color, -1);
135 tc = calloc(1, sizeof(*tc));
137 return got_error_from_errno("calloc");
138 regerr = regcomp(&tc->regex, pattern,
139 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 static char regerr_msg[512];
142 static char err_msg[512];
143 regerror(regerr, &tc->regex, regerr_msg,
145 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 err = got_error_msg(GOT_ERR_REGEX, err_msg);
152 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
157 free_colors(struct tog_colors *colors)
159 struct tog_color *tc;
161 while (!SIMPLEQ_EMPTY(colors)) {
162 tc = SIMPLEQ_FIRST(colors);
163 SIMPLEQ_REMOVE_HEAD(colors, entry);
170 get_color(struct tog_colors *colors, int colorpair)
172 struct tog_color *tc = NULL;
174 SIMPLEQ_FOREACH(tc, colors, entry) {
175 if (tc->colorpair == colorpair)
183 default_color_value(const char *envvar)
185 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
186 return COLOR_MAGENTA;
187 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
194 return COLOR_MAGENTA;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
212 get_color_value(const char *envvar)
214 const char *val = getenv(envvar);
217 return default_color_value(envvar);
219 if (strcasecmp(val, "black") == 0)
221 if (strcasecmp(val, "red") == 0)
223 if (strcasecmp(val, "green") == 0)
225 if (strcasecmp(val, "yellow") == 0)
227 if (strcasecmp(val, "blue") == 0)
229 if (strcasecmp(val, "magenta") == 0)
230 return COLOR_MAGENTA;
231 if (strcasecmp(val, "cyan") == 0)
233 if (strcasecmp(val, "white") == 0)
235 if (strcasecmp(val, "default") == 0)
238 return default_color_value(envvar);
242 struct tog_diff_view_state {
243 struct got_object_id *id1, *id2;
245 int first_displayed_line;
246 int last_displayed_line;
249 struct got_repository *repo;
250 struct got_reflist_head *refs;
251 struct tog_colors colors;
253 /* passed from log view; may be NULL */
254 struct tog_view *log_view;
257 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
259 struct tog_log_thread_args {
260 pthread_cond_t need_commits;
262 struct got_commit_graph *graph;
263 struct commit_queue *commits;
264 const char *in_repo_path;
265 struct got_object_id *start_id;
266 struct got_repository *repo;
269 struct commit_queue_entry **first_displayed_entry;
270 struct commit_queue_entry **selected_entry;
272 int *search_next_done;
276 struct tog_log_view_state {
277 struct commit_queue commits;
278 struct commit_queue_entry *first_displayed_entry;
279 struct commit_queue_entry *last_displayed_entry;
280 struct commit_queue_entry *selected_entry;
283 const char *head_ref_name;
284 struct got_repository *repo;
285 struct got_reflist_head *refs;
286 struct got_object_id *start_id;
289 struct tog_log_thread_args thread_args;
290 struct commit_queue_entry *matched_entry;
291 struct commit_queue_entry *search_entry;
292 struct tog_colors colors;
295 #define TOG_COLOR_DIFF_MINUS 1
296 #define TOG_COLOR_DIFF_PLUS 2
297 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
298 #define TOG_COLOR_DIFF_META 4
299 #define TOG_COLOR_TREE_SUBMODULE 5
300 #define TOG_COLOR_TREE_SYMLINK 6
301 #define TOG_COLOR_TREE_DIRECTORY 7
302 #define TOG_COLOR_TREE_EXECUTABLE 8
303 #define TOG_COLOR_COMMIT 9
304 #define TOG_COLOR_AUTHOR 10
305 #define TOG_COLOR_DATE 11
307 struct tog_blame_cb_args {
308 struct tog_blame_line *lines; /* one per line */
311 struct tog_view *view;
312 struct got_object_id *commit_id;
316 struct tog_blame_thread_args {
318 struct got_repository *repo;
319 struct tog_blame_cb_args *cb_args;
321 got_cancel_cb cancel_cb;
328 struct tog_blame_line *lines;
332 struct tog_blame_thread_args thread_args;
333 struct tog_blame_cb_args cb_args;
337 struct tog_blame_view_state {
338 int first_displayed_line;
339 int last_displayed_line;
344 struct got_object_id_queue blamed_commits;
345 struct got_object_qid *blamed_commit;
347 struct got_repository *repo;
348 struct got_reflist_head *refs;
349 struct got_object_id *commit_id;
350 struct tog_blame blame;
352 struct tog_colors colors;
355 struct tog_parent_tree {
356 TAILQ_ENTRY(tog_parent_tree) entry;
357 struct got_tree_object *tree;
358 struct got_tree_entry *first_displayed_entry;
359 struct got_tree_entry *selected_entry;
363 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
365 struct tog_tree_view_state {
367 struct got_tree_object *root;
368 struct got_tree_object *tree;
369 struct got_tree_entry *first_displayed_entry;
370 struct got_tree_entry *last_displayed_entry;
371 struct got_tree_entry *selected_entry;
372 int ndisplayed, selected, show_ids;
373 struct tog_parent_trees parents;
374 struct got_object_id *commit_id;
375 struct got_repository *repo;
376 struct got_reflist_head *refs;
377 struct got_tree_entry *matched_entry;
378 struct tog_colors colors;
382 * We implement two types of views: parent views and child views.
384 * The 'Tab' key switches between a parent view and its child view.
385 * Child views are shown side-by-side to their parent view, provided
386 * there is enough screen estate.
388 * When a new view is opened from within a parent view, this new view
389 * becomes a child view of the parent view, replacing any existing child.
391 * When a new view is opened from within a child view, this new view
392 * becomes a parent view which will obscure the views below until the
393 * user quits the new parent view by typing 'q'.
395 * This list of views contains parent views only.
396 * Child views are only pointed to by their parent view.
398 TAILQ_HEAD(tog_view_list_head, tog_view);
401 TAILQ_ENTRY(tog_view) entry;
404 int nlines, ncols, begin_y, begin_x;
405 int lines, cols; /* copies of LINES and COLS */
407 struct tog_view *parent;
408 struct tog_view *child;
411 /* type-specific state */
412 enum tog_view_type type;
414 struct tog_diff_view_state diff;
415 struct tog_log_view_state log;
416 struct tog_blame_view_state blame;
417 struct tog_tree_view_state tree;
420 const struct got_error *(*show)(struct tog_view *);
421 const struct got_error *(*input)(struct tog_view **,
422 struct tog_view **, struct tog_view**, struct tog_view *, int);
423 const struct got_error *(*close)(struct tog_view *);
425 const struct got_error *(*search_start)(struct tog_view *);
426 const struct got_error *(*search_next)(struct tog_view *);
428 #define TOG_SEARCH_FORWARD 1
429 #define TOG_SEARCH_BACKWARD 2
430 int search_next_done;
434 static const struct got_error *open_diff_view(struct tog_view *,
435 struct got_object_id *, struct got_object_id *, struct tog_view *,
436 struct got_reflist_head *, struct got_repository *);
437 static const struct got_error *show_diff_view(struct tog_view *);
438 static const struct got_error *input_diff_view(struct tog_view **,
439 struct tog_view **, struct tog_view **, struct tog_view *, int);
440 static const struct got_error* close_diff_view(struct tog_view *);
442 static const struct got_error *open_log_view(struct tog_view *,
443 struct got_object_id *, struct got_reflist_head *,
444 struct got_repository *, const char *, const char *, int);
445 static const struct got_error * show_log_view(struct tog_view *);
446 static const struct got_error *input_log_view(struct tog_view **,
447 struct tog_view **, struct tog_view **, struct tog_view *, int);
448 static const struct got_error *close_log_view(struct tog_view *);
449 static const struct got_error *search_start_log_view(struct tog_view *);
450 static const struct got_error *search_next_log_view(struct tog_view *);
452 static const struct got_error *open_blame_view(struct tog_view *, char *,
453 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
454 static const struct got_error *show_blame_view(struct tog_view *);
455 static const struct got_error *input_blame_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_blame_view(struct tog_view *);
458 static const struct got_error *search_start_blame_view(struct tog_view *);
459 static const struct got_error *search_next_blame_view(struct tog_view *);
461 static const struct got_error *open_tree_view(struct tog_view *,
462 struct got_tree_object *, struct got_object_id *,
463 struct got_reflist_head *, struct got_repository *);
464 static const struct got_error *show_tree_view(struct tog_view *);
465 static const struct got_error *input_tree_view(struct tog_view **,
466 struct tog_view **, struct tog_view **, struct tog_view *, int);
467 static const struct got_error *close_tree_view(struct tog_view *);
468 static const struct got_error *search_start_tree_view(struct tog_view *);
469 static const struct got_error *search_next_tree_view(struct tog_view *);
471 static volatile sig_atomic_t tog_sigwinch_received;
472 static volatile sig_atomic_t tog_sigpipe_received;
475 tog_sigwinch(int signo)
477 tog_sigwinch_received = 1;
481 tog_sigpipe(int signo)
483 tog_sigpipe_received = 1;
486 static const struct got_error *
487 view_close(struct tog_view *view)
489 const struct got_error *err = NULL;
492 view_close(view->child);
496 err = view->close(view);
498 del_panel(view->panel);
500 delwin(view->window);
505 static struct tog_view *
506 view_open(int nlines, int ncols, int begin_y, int begin_x,
507 enum tog_view_type type)
509 struct tog_view *view = calloc(1, sizeof(*view));
517 view->nlines = nlines ? nlines : LINES - begin_y;
518 view->ncols = ncols ? ncols : COLS - begin_x;
519 view->begin_y = begin_y;
520 view->begin_x = begin_x;
521 view->window = newwin(nlines, ncols, begin_y, begin_x);
522 if (view->window == NULL) {
526 view->panel = new_panel(view->window);
527 if (view->panel == NULL ||
528 set_panel_userptr(view->panel, view) != OK) {
533 keypad(view->window, TRUE);
538 view_split_begin_x(int begin_x)
540 if (begin_x > 0 || COLS < 120)
542 return (COLS - MAX(COLS / 2, 80));
545 static const struct got_error *view_resize(struct tog_view *);
547 static const struct got_error *
548 view_splitscreen(struct tog_view *view)
550 const struct got_error *err = NULL;
553 view->begin_x = view_split_begin_x(0);
554 view->nlines = LINES;
555 view->ncols = COLS - view->begin_x;
558 err = view_resize(view);
562 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
563 return got_error_from_errno("mvwin");
568 static const struct got_error *
569 view_fullscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
575 view->nlines = LINES;
579 err = view_resize(view);
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
590 view_is_parent_view(struct tog_view *view)
592 return view->parent == NULL;
595 static const struct got_error *
596 view_resize(struct tog_view *view)
600 if (view->lines > LINES)
601 nlines = view->nlines - (view->lines - LINES);
603 nlines = view->nlines + (LINES - view->lines);
605 if (view->cols > COLS)
606 ncols = view->ncols - (view->cols - COLS);
608 ncols = view->ncols + (COLS - view->cols);
610 if (wresize(view->window, nlines, ncols) == ERR)
611 return got_error_from_errno("wresize");
612 if (replace_panel(view->panel, view->window) == ERR)
613 return got_error_from_errno("replace_panel");
614 wclear(view->window);
616 view->nlines = nlines;
622 view->child->begin_x = view_split_begin_x(view->begin_x);
623 if (view->child->begin_x == 0) {
624 view_fullscreen(view->child);
625 if (view->child->focussed)
626 show_panel(view->child->panel);
628 show_panel(view->panel);
630 view_splitscreen(view->child);
631 show_panel(view->child->panel);
638 static const struct got_error *
639 view_close_child(struct tog_view *view)
641 const struct got_error *err = NULL;
643 if (view->child == NULL)
646 err = view_close(view->child);
651 static const struct got_error *
652 view_set_child(struct tog_view *view, struct tog_view *child)
654 const struct got_error *err = NULL;
657 child->parent = view;
662 view_is_splitscreen(struct tog_view *view)
664 return view->begin_x > 0;
673 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
674 cols = 80; /* Default */
680 resize_term(lines, cols);
683 static const struct got_error *
684 view_search_start(struct tog_view *view)
686 const struct got_error *err = NULL;
691 if (view->nlines < 1)
694 if (!view_is_parent_view(view))
695 begin_x = view_split_begin_x(view->begin_x);
696 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
698 wclrtoeol(view->window);
702 ret = wgetnstr(view->window, pattern, sizeof(pattern));
708 if (view->searching) {
709 regfree(&view->regex);
713 if (regcomp(&view->regex, pattern,
714 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
715 err = view->search_start(view);
717 regfree(&view->regex);
720 view->searching = TOG_SEARCH_FORWARD;
721 view->search_next_done = 0;
722 view->search_next(view);
728 static const struct got_error *
729 view_input(struct tog_view **new, struct tog_view **dead,
730 struct tog_view **focus, int *done, struct tog_view *view,
731 struct tog_view_list_head *views)
733 const struct got_error *err = NULL;
741 if (view->searching && !view->search_next_done) {
742 errcode = pthread_mutex_unlock(&tog_mutex);
744 return got_error_set_errno(errcode,
745 "pthread_mutex_unlock");
747 errcode = pthread_mutex_lock(&tog_mutex);
749 return got_error_set_errno(errcode,
750 "pthread_mutex_lock");
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) {
768 tog_sigwinch_received = 0;
769 TAILQ_FOREACH(v, views, entry) {
770 err = view_resize(v);
773 err = v->input(new, dead, focus, v, KEY_RESIZE);
784 *focus = view->child;
785 view->child_focussed = 1;
786 } else if (view->parent) {
787 *focus = view->parent;
788 view->parent->child_focussed = 0;
792 err = view->input(new, dead, focus, view, ch);
799 if (view_is_parent_view(view)) {
800 if (view->child == NULL)
802 if (view_is_splitscreen(view->child)) {
803 *focus = view->child;
804 view->child_focussed = 1;
805 err = view_fullscreen(view->child);
807 err = view_splitscreen(view->child);
810 err = view->child->input(new, dead, focus,
811 view->child, KEY_RESIZE);
813 if (view_is_splitscreen(view)) {
815 view->parent->child_focussed = 1;
816 err = view_fullscreen(view);
818 err = view_splitscreen(view);
822 err = view->input(new, dead, focus, view,
829 if (view->search_start)
830 view_search_start(view);
832 err = view->input(new, dead, focus, view, ch);
836 if (view->search_next && view->searching) {
837 view->searching = (ch == 'n' ?
838 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 view->search_next_done = 0;
840 view->search_next(view);
842 err = view->input(new, dead, focus, view, ch);
845 err = view->input(new, dead, focus, view, ch);
853 view_vborder(struct tog_view *view)
856 struct tog_view *view_above;
859 return view_vborder(view->parent);
861 panel = panel_above(view->panel);
865 view_above = panel_userptr(panel);
866 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 view_needs_focus_indication(struct tog_view *view)
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL || view->child_focussed)
876 if (!view_is_splitscreen(view->child))
878 } else if (!view_is_splitscreen(view))
881 return view->focussed;
884 static const struct got_error *
885 view_loop(struct tog_view *view)
887 const struct got_error *err = NULL;
888 struct tog_view_list_head views;
889 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 int fast_refresh = 10;
891 int done = 0, errcode;
893 errcode = pthread_mutex_lock(&tog_mutex);
895 return got_error_set_errno(errcode, "pthread_mutex_lock");
898 TAILQ_INSERT_HEAD(&views, view, entry);
902 err = view->show(view);
907 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 /* Refresh fast during initialization, then become slower. */
909 if (fast_refresh && fast_refresh-- == 0)
910 halfdelay(10); /* switch to once per second */
912 err = view_input(&new_view, &dead_view, &focus_view, &done,
917 struct tog_view *prev = NULL;
919 if (view_is_parent_view(dead_view))
920 prev = TAILQ_PREV(dead_view,
921 tog_view_list_head, entry);
922 else if (view->parent != dead_view)
925 if (dead_view->parent)
926 dead_view->parent->child = NULL;
928 TAILQ_REMOVE(&views, dead_view, entry);
930 err = view_close(dead_view);
931 if (err || (dead_view == main_view && new_view == NULL))
934 if (view == dead_view) {
939 else if (!TAILQ_EMPTY(&views))
940 view = TAILQ_LAST(&views,
945 if (view->child && view->child_focussed)
946 focus_view = view->child;
953 struct tog_view *v, *t;
954 /* Only allow one parent view per type. */
955 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 if (v->type != new_view->type)
958 TAILQ_REMOVE(&views, v, entry);
964 TAILQ_INSERT_TAIL(&views, new_view, entry);
966 if (focus_view == NULL)
967 focus_view = new_view;
970 show_panel(focus_view->panel);
973 focus_view->focussed = 1;
976 show_panel(new_view->panel);
977 if (view->child && view_is_splitscreen(view->child))
978 show_panel(view->child->panel);
981 if (focus_view == NULL) {
983 show_panel(view->panel);
984 if (view->child && view_is_splitscreen(view->child))
985 show_panel(view->child->panel);
989 err = view->parent->show(view->parent);
993 err = view->show(view);
997 err = view->child->show(view->child);
1006 while (!TAILQ_EMPTY(&views)) {
1007 view = TAILQ_FIRST(&views);
1008 TAILQ_REMOVE(&views, view, entry);
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1024 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1029 /* Create newly allocated wide-character string equivalent to a byte string. */
1030 static const struct got_error *
1031 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1034 const struct got_error *err = NULL;
1037 *wlen = mbstowcs(NULL, s, 0);
1038 if (*wlen == (size_t)-1) {
1040 if (errno != EILSEQ)
1041 return got_error_from_errno("mbstowcs");
1043 /* byte string invalid in current encoding; try to "fix" it */
1044 err = got_mbsavis(&vis, &vislen, s);
1047 *wlen = mbstowcs(NULL, vis, 0);
1048 if (*wlen == (size_t)-1) {
1049 err = got_error_from_errno("mbstowcs"); /* give up */
1054 *ws = calloc(*wlen + 1, sizeof(**ws));
1056 err = got_error_from_errno("calloc");
1060 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 err = got_error_from_errno("mbstowcs");
1072 /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 static const struct got_error *
1074 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1077 const struct got_error *err = NULL;
1079 wchar_t *wline = NULL;
1086 err = mbs2ws(&wline, &wlen, line);
1092 int width = wcwidth(wline[i]);
1099 if (width == 1 || width == 2) {
1100 if (cols + width > wlimit)
1104 } else if (width == -1) {
1105 if (wline[i] == L'\t') {
1107 ((cols + col_tab_align) % TABSIZE);
1108 if (cols + width > wlimit)
1114 err = got_error_from_errno("wcwidth");
1129 static const struct got_error*
1130 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 struct got_object_id *id, struct got_repository *repo)
1133 static const struct got_error *err = NULL;
1134 struct got_reflist_entry *re;
1140 SIMPLEQ_FOREACH(re, refs, entry) {
1141 struct got_tag_object *tag = NULL;
1144 name = got_ref_get_name(re->ref);
1145 if (strcmp(name, GOT_REF_HEAD) == 0)
1147 if (strncmp(name, "refs/", 5) == 0)
1149 if (strncmp(name, "got/", 4) == 0)
1151 if (strncmp(name, "heads/", 6) == 0)
1153 if (strncmp(name, "remotes/", 8) == 0)
1155 if (strncmp(name, "tags/", 5) == 0) {
1156 err = got_object_open_as_tag(&tag, repo, re->id);
1158 if (err->code != GOT_ERR_OBJ_TYPE)
1160 /* Ref points at something other than a tag. */
1165 cmp = got_object_id_cmp(tag ?
1166 got_object_tag_get_object_id(tag) : re->id, id);
1168 got_object_tag_close(tag);
1172 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 s ? ", " : "", name) == -1) {
1174 err = got_error_from_errno("asprintf");
1185 static const struct got_error *
1186 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1189 char *smallerthan, *at;
1191 smallerthan = strchr(author, '<');
1192 if (smallerthan && smallerthan[1] != '\0')
1193 author = smallerthan + 1;
1194 at = strchr(author, '@');
1197 return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 static const struct got_error *
1201 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 struct got_object_id *id, struct got_reflist_head *refs,
1203 const size_t date_display_cols, int author_display_cols,
1204 struct tog_colors *colors)
1206 const struct got_error *err = NULL;
1207 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 char *logmsg0 = NULL, *logmsg = NULL;
1209 char *author = NULL;
1210 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 int author_width, logmsg_width;
1212 char *newline, *line = NULL;
1214 const int avail = view->ncols;
1216 time_t committer_time;
1217 struct tog_color *tc;
1219 committer_time = got_object_commit_get_committer_time(commit);
1220 if (localtime_r(&committer_time, &tm) == NULL)
1221 return got_error_from_errno("localtime_r");
1222 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1224 return got_error(GOT_ERR_NO_SPACE);
1226 if (avail <= date_display_cols)
1227 limit = MIN(sizeof(datebuf) - 1, avail);
1229 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 tc = get_color(colors, TOG_COLOR_DATE);
1232 wattr_on(view->window,
1233 COLOR_PAIR(tc->colorpair), NULL);
1234 waddnstr(view->window, datebuf, limit);
1236 wattr_off(view->window,
1237 COLOR_PAIR(tc->colorpair), NULL);
1244 err = got_object_id_str(&id_str, id);
1247 tc = get_color(colors, TOG_COLOR_COMMIT);
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 wprintw(view->window, "%.8s ", id_str);
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1261 author = strdup(got_object_commit_get_author(commit));
1262 if (author == NULL) {
1263 err = got_error_from_errno("strdup");
1266 err = format_author(&wauthor, &author_width, author, avail - col, col);
1269 tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 wattr_on(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1273 waddwstr(view->window, wauthor);
1275 wattr_off(view->window,
1276 COLOR_PAIR(tc->colorpair), NULL);
1277 col += author_width;
1278 while (col < avail && author_width < author_display_cols + 2) {
1279 waddch(view->window, ' ');
1286 err = got_object_commit_get_logmsg(&logmsg0, commit);
1290 while (*logmsg == '\n')
1292 newline = strchr(logmsg, '\n');
1295 limit = avail - col;
1296 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1299 waddwstr(view->window, wlogmsg);
1300 col += logmsg_width;
1301 while (col < avail) {
1302 waddch(view->window, ' ');
1314 static struct commit_queue_entry *
1315 alloc_commit_queue_entry(struct got_commit_object *commit,
1316 struct got_object_id *id)
1318 struct commit_queue_entry *entry;
1320 entry = calloc(1, sizeof(*entry));
1325 entry->commit = commit;
1330 pop_commit(struct commit_queue *commits)
1332 struct commit_queue_entry *entry;
1334 entry = TAILQ_FIRST(&commits->head);
1335 TAILQ_REMOVE(&commits->head, entry, entry);
1336 got_object_commit_close(entry->commit);
1337 commits->ncommits--;
1338 /* Don't free entry->id! It is owned by the commit graph. */
1343 free_commits(struct commit_queue *commits)
1345 while (!TAILQ_EMPTY(&commits->head))
1346 pop_commit(commits);
1349 static const struct got_error *
1350 match_commit(int *have_match, struct got_object_id *id,
1351 struct got_commit_object *commit, regex_t *regex)
1353 const struct got_error *err = NULL;
1354 regmatch_t regmatch;
1355 char *id_str = NULL, *logmsg = NULL;
1359 err = got_object_id_str(&id_str, id);
1363 err = got_object_commit_get_logmsg(&logmsg, commit);
1367 if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 ®match, 0) == 0 ||
1369 regexec(regex, got_object_commit_get_committer(commit), 1,
1370 ®match, 0) == 0 ||
1371 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1372 regexec(regex, logmsg, 1, ®match, 0) == 0)
1380 static const struct got_error *
1381 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 int minqueue, struct got_repository *repo, const char *path,
1383 int *searching, int *search_next_done, regex_t *regex)
1385 const struct got_error *err = NULL;
1386 int nqueued = 0, have_match = 0;
1389 * We keep all commits open throughout the lifetime of the log
1390 * view in order to avoid having to re-fetch commits from disk
1391 * while updating the display.
1393 while (nqueued < minqueue ||
1394 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 struct got_object_id *id;
1396 struct got_commit_object *commit;
1397 struct commit_queue_entry *entry;
1400 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 if (err || id == NULL)
1404 err = got_object_open_as_commit(&commit, repo, id);
1407 entry = alloc_commit_queue_entry(commit, id);
1408 if (entry == NULL) {
1409 err = got_error_from_errno("alloc_commit_queue_entry");
1413 errcode = pthread_mutex_lock(&tog_mutex);
1415 err = got_error_set_errno(errcode,
1416 "pthread_mutex_lock");
1420 entry->idx = commits->ncommits;
1421 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1423 commits->ncommits++;
1425 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 err = match_commit(&have_match, id, commit, regex);
1428 pthread_mutex_lock(&tog_mutex);
1433 errcode = pthread_mutex_unlock(&tog_mutex);
1434 if (errcode && err == NULL)
1435 err = got_error_set_errno(errcode,
1436 "pthread_mutex_unlock");
1445 static const struct got_error *
1446 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1447 struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_reference *head_ref;
1454 err = got_ref_open(&head_ref, repo, branch_name, 0);
1458 err = got_ref_resolve(head_id, repo, head_ref);
1459 got_ref_close(head_ref);
1468 static const struct got_error *
1469 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 struct commit_queue *commits, int selected_idx, int limit,
1472 struct got_reflist_head *refs, const char *path, int commits_needed,
1473 struct tog_colors *colors)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 struct commit_queue_entry *entry;
1479 int ncommits, author_cols = 4;
1480 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 char *refs_str = NULL;
1483 struct tog_color *tc;
1484 static const size_t date_display_cols = 12;
1489 if (ncommits == selected_idx) {
1493 entry = TAILQ_NEXT(entry, entry);
1497 if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 err = got_object_id_str(&id_str, (*selected)->id);
1502 err = build_refs_str(&refs_str, refs, (*selected)->id,
1509 if (commits_needed == 0)
1510 halfdelay(10); /* disable fast refresh */
1512 if (asprintf(&ncommits_str, " [%d/%d] %s",
1513 entry ? entry->idx + 1 : 0, commits->ncommits,
1514 commits_needed > 0 ?
1515 (view->searching && view->search_next_done == 0
1516 ? "searching..." : "loading... ") :
1517 (refs_str ? refs_str : "")) == -1) {
1518 err = got_error_from_errno("asprintf");
1522 if (path && strcmp(path, "/") != 0) {
1523 if (asprintf(&header, "commit %s %s%s",
1524 id_str ? id_str : "........................................",
1525 path, ncommits_str) == -1) {
1526 err = got_error_from_errno("asprintf");
1530 } else if (asprintf(&header, "commit %s%s",
1531 id_str ? id_str : "........................................",
1532 ncommits_str) == -1) {
1533 err = got_error_from_errno("asprintf");
1537 err = format_line(&wline, &width, header, view->ncols, 0);
1541 werase(view->window);
1543 if (view_needs_focus_indication(view))
1544 wstandout(view->window);
1545 tc = get_color(colors, TOG_COLOR_COMMIT);
1547 wattr_on(view->window,
1548 COLOR_PAIR(tc->colorpair), NULL);
1549 waddwstr(view->window, wline);
1551 wattr_off(view->window,
1552 COLOR_PAIR(tc->colorpair), NULL);
1553 while (width < view->ncols) {
1554 waddch(view->window, ' ');
1557 if (view_needs_focus_indication(view))
1558 wstandend(view->window);
1563 /* Grow author column size if necessary. */
1570 if (ncommits >= limit - 1)
1572 author = strdup(got_object_commit_get_author(entry->commit));
1573 if (author == NULL) {
1574 err = got_error_from_errno("strdup");
1577 err = format_author(&wauthor, &width, author, COLS,
1579 if (author_cols < width)
1580 author_cols = width;
1584 entry = TAILQ_NEXT(entry, entry);
1591 if (ncommits >= limit - 1)
1593 if (ncommits == selected_idx)
1594 wstandout(view->window);
1595 err = draw_commit(view, entry->commit, entry->id, refs,
1596 date_display_cols, author_cols, colors);
1597 if (ncommits == selected_idx)
1598 wstandend(view->window);
1603 entry = TAILQ_NEXT(entry, entry);
1616 scroll_up(struct tog_view *view,
1617 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1618 struct commit_queue *commits)
1620 struct commit_queue_entry *entry;
1623 entry = TAILQ_FIRST(&commits->head);
1624 if (*first_displayed_entry == entry)
1627 entry = *first_displayed_entry;
1628 while (entry && nscrolled < maxscroll) {
1629 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1631 *first_displayed_entry = entry;
1637 static const struct got_error *
1638 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1639 pthread_cond_t *need_commits)
1644 halfdelay(1); /* fast refresh while loading commits */
1646 while (*commits_needed > 0) {
1650 /* Wake the log thread. */
1651 errcode = pthread_cond_signal(need_commits);
1653 return got_error_set_errno(errcode,
1654 "pthread_cond_signal");
1655 errcode = pthread_mutex_unlock(&tog_mutex);
1657 return got_error_set_errno(errcode,
1658 "pthread_mutex_unlock");
1660 errcode = pthread_mutex_lock(&tog_mutex);
1662 return got_error_set_errno(errcode,
1663 "pthread_mutex_lock");
1665 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1667 * Thread is not done yet; lose a key press
1668 * and let the user retry... this way the GUI
1669 * remains interactive while logging deep paths
1670 * with few commits in history.
1679 static const struct got_error *
1680 scroll_down(struct tog_view *view,
1681 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1682 struct commit_queue_entry **last_displayed_entry,
1683 struct commit_queue *commits, int *log_complete, int *commits_needed,
1684 pthread_cond_t *need_commits)
1686 const struct got_error *err = NULL;
1687 struct commit_queue_entry *pentry;
1690 if (*last_displayed_entry == NULL)
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 if (pentry == NULL && !*log_complete) {
1696 * Ask the log thread for required amount of commits
1697 * plus some amount of pre-fetching.
1699 (*commits_needed) += maxscroll + 20;
1700 err = trigger_log_thread(0, commits_needed, log_complete,
1707 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1711 *last_displayed_entry = pentry;
1713 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1716 *first_displayed_entry = pentry;
1717 } while (++nscrolled < maxscroll);
1722 static const struct got_error *
1723 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1724 struct got_commit_object *commit, struct got_object_id *commit_id,
1725 struct tog_view *log_view, struct got_reflist_head *refs,
1726 struct got_repository *repo)
1728 const struct got_error *err;
1729 struct got_object_qid *parent_id;
1730 struct tog_view *diff_view;
1732 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1733 if (diff_view == NULL)
1734 return got_error_from_errno("view_open");
1736 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1737 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1738 commit_id, log_view, refs, repo);
1740 *new_view = diff_view;
1744 static const struct got_error *
1745 tree_view_visit_subtree(struct got_tree_object *subtree,
1746 struct tog_tree_view_state *s)
1748 struct tog_parent_tree *parent;
1750 parent = calloc(1, sizeof(*parent));
1752 return got_error_from_errno("calloc");
1754 parent->tree = s->tree;
1755 parent->first_displayed_entry = s->first_displayed_entry;
1756 parent->selected_entry = s->selected_entry;
1757 parent->selected = s->selected;
1758 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1761 s->first_displayed_entry = NULL;
1766 static const struct got_error *
1767 browse_commit_tree(struct tog_view **new_view, int begin_x,
1768 struct commit_queue_entry *entry, const char *path,
1769 struct got_reflist_head *refs, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_tree_object *tree;
1773 struct tog_tree_view_state *s;
1774 struct tog_view *tree_view;
1775 char *slash, *subpath = NULL;
1778 err = got_object_open_as_tree(&tree, repo,
1779 got_object_commit_get_tree_id(entry->commit));
1783 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1784 if (tree_view == NULL)
1785 return got_error_from_errno("view_open");
1787 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1789 got_object_tree_close(tree);
1792 s = &tree_view->state.tree;
1794 *new_view = tree_view;
1796 /* Walk the path and open corresponding tree objects. */
1801 struct got_tree_entry *te;
1802 struct got_object_id *tree_id;
1805 /* Ensure the correct subtree entry is selected. */
1806 slash = strchr(p, '/');
1808 slash = strchr(p, '\0');
1810 te_name = strndup(p, slash -p);
1811 if (te_name == NULL) {
1812 err = got_error_from_errno("strndup");
1815 te = got_object_tree_find_entry(s->tree, te_name);
1817 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1822 s->selected_entry = te;
1823 s->selected = got_tree_entry_get_index(te);
1824 if (s->tree != s->root)
1825 s->selected++; /* skip '..' */
1827 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1828 /* Jump to this file's entry. */
1829 s->first_displayed_entry = s->selected_entry;
1834 slash = strchr(p, '/');
1836 subpath = strndup(path, slash - path);
1838 subpath = strdup(path);
1839 if (subpath == NULL) {
1840 err = got_error_from_errno("strdup");
1844 err = got_object_id_by_path(&tree_id, repo, entry->id,
1849 err = got_object_open_as_tree(&tree, repo, tree_id);
1854 err = tree_view_visit_subtree(tree, s);
1856 got_object_tree_close(tree);
1871 log_thread(void *arg)
1873 const struct got_error *err = NULL;
1875 struct tog_log_thread_args *a = arg;
1878 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1883 while (!done && !err && !tog_sigpipe_received) {
1884 err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 a->in_repo_path, a->searching, a->search_next_done,
1888 if (err->code != GOT_ERR_ITER_COMPLETED)
1892 } else if (a->commits_needed > 0)
1893 a->commits_needed--;
1895 errcode = pthread_mutex_lock(&tog_mutex);
1897 err = got_error_set_errno(errcode,
1898 "pthread_mutex_lock");
1900 } else if (*a->quit)
1902 else if (*a->first_displayed_entry == NULL) {
1903 *a->first_displayed_entry =
1904 TAILQ_FIRST(&a->commits->head);
1905 *a->selected_entry = *a->first_displayed_entry;
1909 a->commits_needed = 0;
1910 else if (a->commits_needed == 0) {
1911 errcode = pthread_cond_wait(&a->need_commits,
1914 err = got_error_set_errno(errcode,
1915 "pthread_cond_wait");
1918 errcode = pthread_mutex_unlock(&tog_mutex);
1919 if (errcode && err == NULL)
1920 err = got_error_set_errno(errcode,
1921 "pthread_mutex_unlock");
1923 a->log_complete = 1;
1927 static const struct got_error *
1928 stop_log_thread(struct tog_log_view_state *s)
1930 const struct got_error *err = NULL;
1935 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1937 return got_error_set_errno(errcode,
1938 "pthread_cond_signal");
1939 errcode = pthread_mutex_unlock(&tog_mutex);
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_unlock");
1943 errcode = pthread_join(s->thread, (void **)&err);
1945 return got_error_set_errno(errcode, "pthread_join");
1946 errcode = pthread_mutex_lock(&tog_mutex);
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_lock");
1953 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 if (errcode && err == NULL)
1955 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 if (s->thread_args.repo) {
1958 got_repo_close(s->thread_args.repo);
1959 s->thread_args.repo = NULL;
1962 if (s->thread_args.graph) {
1963 got_commit_graph_close(s->thread_args.graph);
1964 s->thread_args.graph = NULL;
1970 static const struct got_error *
1971 close_log_view(struct tog_view *view)
1973 const struct got_error *err = NULL;
1974 struct tog_log_view_state *s = &view->state.log;
1976 err = stop_log_thread(s);
1977 free_commits(&s->commits);
1978 free(s->in_repo_path);
1979 s->in_repo_path = NULL;
1985 static const struct got_error *
1986 search_start_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 s->matched_entry = NULL;
1991 s->search_entry = NULL;
1995 static const struct got_error *
1996 search_next_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2000 struct commit_queue_entry *entry;
2002 if (!view->searching) {
2003 view->search_next_done = 1;
2007 if (s->search_entry) {
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 ch = wgetch(view->window);
2014 errcode = pthread_mutex_lock(&tog_mutex);
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 if (ch == KEY_BACKSPACE) {
2019 view->search_next_done = 1;
2022 if (view->searching == TOG_SEARCH_FORWARD)
2023 entry = TAILQ_NEXT(s->search_entry, entry);
2025 entry = TAILQ_PREV(s->search_entry,
2026 commit_queue_head, entry);
2027 } else if (s->matched_entry) {
2028 if (view->searching == TOG_SEARCH_FORWARD)
2029 entry = TAILQ_NEXT(s->selected_entry, entry);
2031 entry = TAILQ_PREV(s->selected_entry,
2032 commit_queue_head, entry);
2034 if (view->searching == TOG_SEARCH_FORWARD)
2035 entry = TAILQ_FIRST(&s->commits.head);
2037 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2043 if (entry == NULL) {
2044 if (s->thread_args.log_complete ||
2045 view->searching == TOG_SEARCH_BACKWARD) {
2046 view->search_next_done = 1;
2050 * Poke the log thread for more commits and return,
2051 * allowing the main loop to make progress. Search
2052 * will resume at s->search_entry once we come back.
2054 s->thread_args.commits_needed++;
2055 return trigger_log_thread(1,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.log_complete,
2058 &s->thread_args.need_commits);
2061 err = match_commit(&have_match, entry->id, entry->commit,
2066 view->search_next_done = 1;
2067 s->matched_entry = entry;
2071 s->search_entry = entry;
2072 if (view->searching == TOG_SEARCH_FORWARD)
2073 entry = TAILQ_NEXT(entry, entry);
2075 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 if (s->matched_entry) {
2079 int cur = s->selected_entry->idx;
2080 while (cur < s->matched_entry->idx) {
2081 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2086 while (cur > s->matched_entry->idx) {
2087 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2094 s->search_entry = NULL;
2099 static const struct got_error *
2100 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 struct got_reflist_head *refs, struct got_repository *repo,
2102 const char *head_ref_name, const char *path, int check_disk)
2104 const struct got_error *err = NULL;
2105 struct tog_log_view_state *s = &view->state.log;
2106 struct got_repository *thread_repo = NULL;
2107 struct got_commit_graph *thread_graph = NULL;
2110 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2114 /* The commit queue only contains commits being displayed. */
2115 TAILQ_INIT(&s->commits.head);
2116 s->commits.ncommits = 0;
2120 s->head_ref_name = head_ref_name;
2121 s->start_id = got_object_id_dup(start_id);
2122 if (s->start_id == NULL) {
2123 err = got_error_from_errno("got_object_id_dup");
2127 SIMPLEQ_INIT(&s->colors);
2128 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2130 get_color_value("TOG_COLOR_COMMIT"));
2133 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2134 get_color_value("TOG_COLOR_AUTHOR"));
2136 free_colors(&s->colors);
2139 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2140 get_color_value("TOG_COLOR_DATE"));
2142 free_colors(&s->colors);
2147 view->show = show_log_view;
2148 view->input = input_log_view;
2149 view->close = close_log_view;
2150 view->search_start = search_start_log_view;
2151 view->search_next = search_next_log_view;
2153 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2156 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
2161 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2163 err = got_error_set_errno(errcode, "pthread_cond_init");
2167 s->thread_args.commits_needed = view->nlines;
2168 s->thread_args.graph = thread_graph;
2169 s->thread_args.commits = &s->commits;
2170 s->thread_args.in_repo_path = s->in_repo_path;
2171 s->thread_args.start_id = s->start_id;
2172 s->thread_args.repo = thread_repo;
2173 s->thread_args.log_complete = 0;
2174 s->thread_args.quit = &s->quit;
2175 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2176 s->thread_args.selected_entry = &s->selected_entry;
2177 s->thread_args.searching = &view->searching;
2178 s->thread_args.search_next_done = &view->search_next_done;
2179 s->thread_args.regex = &view->regex;
2182 close_log_view(view);
2186 static const struct got_error *
2187 show_log_view(struct tog_view *view)
2189 struct tog_log_view_state *s = &view->state.log;
2191 if (s->thread == NULL) {
2192 int errcode = pthread_create(&s->thread, NULL, log_thread,
2195 return got_error_set_errno(errcode, "pthread_create");
2198 return draw_commits(view, &s->last_displayed_entry,
2199 &s->selected_entry, s->first_displayed_entry,
2200 &s->commits, s->selected, view->nlines, s->refs,
2201 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2204 static const struct got_error *
2205 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2206 struct tog_view **focus_view, struct tog_view *view, int ch)
2208 const struct got_error *err = NULL;
2209 struct tog_log_view_state *s = &view->state.log;
2210 char *parent_path, *in_repo_path = NULL;
2211 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2213 struct got_object_id *start_id;
2223 if (s->first_displayed_entry == NULL)
2225 if (s->selected > 0)
2228 scroll_up(view, &s->first_displayed_entry, 1,
2233 if (s->first_displayed_entry == NULL)
2235 if (TAILQ_FIRST(&s->commits.head) ==
2236 s->first_displayed_entry) {
2240 scroll_up(view, &s->first_displayed_entry,
2241 view->nlines, &s->commits);
2247 if (s->first_displayed_entry == NULL)
2249 if (s->selected < MIN(view->nlines - 2,
2250 s->commits.ncommits - 1)) {
2254 err = scroll_down(view, &s->first_displayed_entry, 1,
2255 &s->last_displayed_entry, &s->commits,
2256 &s->thread_args.log_complete,
2257 &s->thread_args.commits_needed,
2258 &s->thread_args.need_commits);
2262 struct commit_queue_entry *first;
2263 first = s->first_displayed_entry;
2266 err = scroll_down(view, &s->first_displayed_entry,
2267 view->nlines, &s->last_displayed_entry,
2268 &s->commits, &s->thread_args.log_complete,
2269 &s->thread_args.commits_needed,
2270 &s->thread_args.need_commits);
2273 if (first == s->first_displayed_entry &&
2274 s->selected < MIN(view->nlines - 2,
2275 s->commits.ncommits - 1)) {
2276 /* can't scroll further down */
2277 s->selected = MIN(view->nlines - 2,
2278 s->commits.ncommits - 1);
2284 if (s->selected > view->nlines - 2)
2285 s->selected = view->nlines - 2;
2286 if (s->selected > s->commits.ncommits - 1)
2287 s->selected = s->commits.ncommits - 1;
2292 if (s->selected_entry == NULL)
2294 if (view_is_parent_view(view))
2295 begin_x = view_split_begin_x(view->begin_x);
2296 err = open_diff_view_for_commit(&diff_view, begin_x,
2297 s->selected_entry->commit, s->selected_entry->id,
2298 view, s->refs, s->repo);
2301 if (view_is_parent_view(view)) {
2302 err = view_close_child(view);
2305 err = view_set_child(view, diff_view);
2307 view_close(diff_view);
2310 *focus_view = diff_view;
2311 view->child_focussed = 1;
2313 *new_view = diff_view;
2316 if (s->selected_entry == NULL)
2318 if (view_is_parent_view(view))
2319 begin_x = view_split_begin_x(view->begin_x);
2320 err = browse_commit_tree(&tree_view, begin_x,
2321 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2324 if (view_is_parent_view(view)) {
2325 err = view_close_child(view);
2328 err = view_set_child(view, tree_view);
2330 view_close(tree_view);
2333 *focus_view = tree_view;
2334 view->child_focussed = 1;
2336 *new_view = tree_view;
2339 if (strcmp(s->in_repo_path, "/") == 0)
2341 parent_path = dirname(s->in_repo_path);
2342 if (parent_path && strcmp(parent_path, ".") != 0) {
2343 err = stop_log_thread(s);
2346 lv = view_open(view->nlines, view->ncols,
2347 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2349 return got_error_from_errno(
2351 err = open_log_view(lv, s->start_id, s->refs,
2352 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 = get_head_commit_id(&start_id, s->head_ref_name ?
2373 s->head_ref_name : GOT_REF_HEAD, s->repo);
2378 in_repo_path = strdup(s->in_repo_path);
2379 if (in_repo_path == NULL) {
2382 return got_error_from_errno("strdup");
2384 got_ref_list_free(s->refs);
2385 err = got_ref_list(s->refs, s->repo, NULL,
2386 got_ref_cmp_by_name, NULL);
2392 err = open_log_view(lv, start_id, s->refs, s->repo,
2393 s->head_ref_name, in_repo_path, 0);
2409 static const struct got_error *
2410 apply_unveil(const char *repo_path, const char *worktree_path)
2412 const struct got_error *error;
2415 if (unveil("gmon.out", "rwc") != 0)
2416 return got_error_from_errno2("unveil", "gmon.out");
2418 if (repo_path && unveil(repo_path, "r") != 0)
2419 return got_error_from_errno2("unveil", repo_path);
2421 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2422 return got_error_from_errno2("unveil", worktree_path);
2424 if (unveil("/tmp", "rwc") != 0)
2425 return got_error_from_errno2("unveil", "/tmp");
2427 error = got_privsep_unveil_exec_helpers();
2431 if (unveil(NULL, NULL) != 0)
2432 return got_error_from_errno("unveil");
2442 halfdelay(1); /* Do fast refresh while initial view is loading. */
2445 intrflush(stdscr, FALSE);
2446 keypad(stdscr, TRUE);
2448 if (getenv("TOG_COLORS") != NULL) {
2450 use_default_colors();
2452 signal(SIGWINCH, tog_sigwinch);
2453 signal(SIGPIPE, tog_sigpipe);
2456 static const struct got_error *
2457 cmd_log(int argc, char *argv[])
2459 const struct got_error *error;
2460 struct got_repository *repo = NULL;
2461 struct got_worktree *worktree = NULL;
2462 struct got_reflist_head refs;
2463 struct got_object_id *start_id = NULL;
2464 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2465 char *start_commit = NULL, *head_ref_name = NULL;
2467 struct tog_view *view;
2469 SIMPLEQ_INIT(&refs);
2472 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2477 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2480 start_commit = optarg;
2483 repo_path = realpath(optarg, NULL);
2484 if (repo_path == NULL)
2485 return got_error_from_errno2("realpath",
2497 cwd = getcwd(NULL, 0);
2499 error = got_error_from_errno("getcwd");
2502 error = got_worktree_open(&worktree, cwd);
2503 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2510 error = got_error_from_errno("strdup");
2513 } else if (argc == 1) {
2515 error = got_worktree_resolve_path(&path, worktree,
2520 path = strdup(argv[0]);
2522 error = got_error_from_errno("strdup");
2529 if (repo_path == NULL) {
2532 got_worktree_get_repo_path(worktree));
2534 repo_path = strdup(cwd);
2536 if (repo_path == NULL) {
2537 error = got_error_from_errno("strdup");
2543 error = got_repo_open(&repo, repo_path, NULL);
2547 error = apply_unveil(got_repo_get_path(repo),
2548 worktree ? got_worktree_get_root_path(worktree) : NULL);
2552 if (start_commit == NULL)
2553 error = get_head_commit_id(&start_id, worktree ?
2554 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2557 error = get_head_commit_id(&start_id, start_commit, repo);
2559 if (error->code != GOT_ERR_NOT_REF)
2561 error = got_repo_match_object_id_prefix(&start_id,
2562 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2568 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2572 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2574 error = got_error_from_errno("view_open");
2578 head_ref_name = strdup(
2579 got_worktree_get_head_ref_name(worktree));
2580 if (head_ref_name == NULL) {
2581 error = got_error_from_errno("strdup");
2585 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2590 /* Release work tree lock. */
2591 got_worktree_close(worktree);
2594 error = view_loop(view);
2600 free(head_ref_name);
2602 got_repo_close(repo);
2604 got_worktree_close(worktree);
2605 got_ref_list_free(&refs);
2613 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2619 parse_next_line(FILE *f, size_t *len)
2624 const char delim[3] = { '\0', '\0', '\0'};
2626 line = fparseln(f, &linelen, &lineno, delim, 0);
2633 match_line(const char *line, regex_t *regex)
2635 regmatch_t regmatch;
2637 return regexec(regex, line, 1, ®match, 0) == 0;
2641 match_color(struct tog_colors *colors, const char *line)
2643 struct tog_color *tc = NULL;
2645 SIMPLEQ_FOREACH(tc, colors, entry) {
2646 if (match_line(line, &tc->regex))
2653 static const struct got_error *
2654 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2655 int *last_displayed_line, int *eof, int max_lines, char *header,
2656 struct tog_colors *colors)
2658 const struct got_error *err;
2659 int nlines = 0, nprinted = 0;
2661 struct tog_color *tc;
2667 werase(view->window);
2670 err = format_line(&wline, &width, header, view->ncols, 0);
2675 if (view_needs_focus_indication(view))
2676 wstandout(view->window);
2677 waddwstr(view->window, wline);
2678 if (view_needs_focus_indication(view))
2679 wstandend(view->window);
2680 if (width <= view->ncols - 1)
2681 waddch(view->window, '\n');
2689 while (nprinted < max_lines) {
2690 line = parse_next_line(f, &len);
2695 if (++nlines < *first_displayed_line) {
2700 err = format_line(&wline, &width, line, view->ncols, 0);
2706 tc = match_color(colors, line);
2708 wattr_on(view->window,
2709 COLOR_PAIR(tc->colorpair), NULL);
2710 waddwstr(view->window, wline);
2712 wattr_off(view->window,
2713 COLOR_PAIR(tc->colorpair), NULL);
2714 if (width <= view->ncols - 1)
2715 waddch(view->window, '\n');
2716 if (++nprinted == 1)
2717 *first_displayed_line = nlines;
2722 *last_displayed_line = nlines;
2727 while (nprinted < view->nlines) {
2728 waddch(view->window, '\n');
2732 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2737 wstandout(view->window);
2738 waddwstr(view->window, wline);
2739 wstandend(view->window);
2746 get_datestr(time_t *time, char *datebuf)
2748 struct tm mytm, *tm;
2751 tm = gmtime_r(time, &mytm);
2754 s = asctime_r(tm, datebuf);
2757 p = strchr(s, '\n');
2763 static const struct got_error *
2764 write_commit_info(struct got_object_id *commit_id,
2765 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2767 const struct got_error *err = NULL;
2768 char datebuf[26], *datestr;
2769 struct got_commit_object *commit;
2770 char *id_str = NULL, *logmsg = NULL;
2771 time_t committer_time;
2772 const char *author, *committer;
2773 char *refs_str = NULL;
2776 err = build_refs_str(&refs_str, refs, commit_id, repo);
2781 err = got_object_open_as_commit(&commit, repo, commit_id);
2785 err = got_object_id_str(&id_str, commit_id);
2787 err = got_error_from_errno("got_object_id_str");
2791 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2792 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2793 err = got_error_from_errno("fprintf");
2796 if (fprintf(outfile, "from: %s\n",
2797 got_object_commit_get_author(commit)) < 0) {
2798 err = got_error_from_errno("fprintf");
2801 committer_time = got_object_commit_get_committer_time(commit);
2802 datestr = get_datestr(&committer_time, datebuf);
2803 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2804 err = got_error_from_errno("fprintf");
2807 author = got_object_commit_get_author(commit);
2808 committer = got_object_commit_get_committer(commit);
2809 if (strcmp(author, committer) != 0 &&
2810 fprintf(outfile, "via: %s\n", committer) < 0) {
2811 err = got_error_from_errno("fprintf");
2814 err = got_object_commit_get_logmsg(&logmsg, commit);
2817 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2818 err = got_error_from_errno("fprintf");
2825 got_object_commit_close(commit);
2829 static const struct got_error *
2830 create_diff(struct tog_diff_view_state *s)
2832 const struct got_error *err = NULL;
2838 err = got_error_from_errno("got_opentemp");
2841 if (s->f && fclose(s->f) != 0) {
2842 err = got_error_from_errno("fclose");
2848 err = got_object_get_type(&obj_type, s->repo, s->id1);
2850 err = got_object_get_type(&obj_type, s->repo, s->id2);
2855 case GOT_OBJ_TYPE_BLOB:
2856 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2857 s->diff_context, 0, s->repo, f);
2859 case GOT_OBJ_TYPE_TREE:
2860 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2861 s->diff_context, 0, s->repo, f);
2863 case GOT_OBJ_TYPE_COMMIT: {
2864 const struct got_object_id_queue *parent_ids;
2865 struct got_object_qid *pid;
2866 struct got_commit_object *commit2;
2868 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2871 /* Show commit info if we're diffing to a parent/root commit. */
2873 write_commit_info(s->id2, s->refs, s->repo, f);
2875 parent_ids = got_object_commit_get_parent_ids(commit2);
2876 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2877 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2878 write_commit_info(s->id2, s->refs,
2884 got_object_commit_close(commit2);
2886 err = got_diff_objects_as_commits(s->id1, s->id2,
2887 s->diff_context, 0, s->repo, f);
2891 err = got_error(GOT_ERR_OBJ_TYPE);
2895 if (f && fflush(f) != 0 && err == NULL)
2896 err = got_error_from_errno("fflush");
2901 diff_view_indicate_progress(struct tog_view *view)
2903 mvwaddstr(view->window, 0, 0, "diffing...");
2908 static const struct got_error *
2909 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2910 struct got_object_id *id2, struct tog_view *log_view,
2911 struct got_reflist_head *refs, struct got_repository *repo)
2913 const struct got_error *err;
2915 if (id1 != NULL && id2 != NULL) {
2917 err = got_object_get_type(&type1, repo, id1);
2920 err = got_object_get_type(&type2, repo, id2);
2925 return got_error(GOT_ERR_OBJ_TYPE);
2929 view->state.diff.id1 = got_object_id_dup(id1);
2930 if (view->state.diff.id1 == NULL)
2931 return got_error_from_errno("got_object_id_dup");
2933 view->state.diff.id1 = NULL;
2935 view->state.diff.id2 = got_object_id_dup(id2);
2936 if (view->state.diff.id2 == NULL) {
2937 free(view->state.diff.id1);
2938 view->state.diff.id1 = NULL;
2939 return got_error_from_errno("got_object_id_dup");
2941 view->state.diff.f = NULL;
2942 view->state.diff.first_displayed_line = 1;
2943 view->state.diff.last_displayed_line = view->nlines;
2944 view->state.diff.diff_context = 3;
2945 view->state.diff.log_view = log_view;
2946 view->state.diff.repo = repo;
2947 view->state.diff.refs = refs;
2948 SIMPLEQ_INIT(&view->state.diff.colors);
2950 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2951 err = add_color(&view->state.diff.colors,
2952 "^-", TOG_COLOR_DIFF_MINUS,
2953 get_color_value("TOG_COLOR_DIFF_MINUS"));
2956 err = add_color(&view->state.diff.colors, "^\\+",
2957 TOG_COLOR_DIFF_PLUS,
2958 get_color_value("TOG_COLOR_DIFF_PLUS"));
2960 free_colors(&view->state.diff.colors);
2963 err = add_color(&view->state.diff.colors,
2964 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2965 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2967 free_colors(&view->state.diff.colors);
2971 err = add_color(&view->state.diff.colors,
2972 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2973 get_color_value("TOG_COLOR_DIFF_META"));
2975 free_colors(&view->state.diff.colors);
2979 err = add_color(&view->state.diff.colors,
2980 "^(from|via): ", TOG_COLOR_AUTHOR,
2981 get_color_value("TOG_COLOR_AUTHOR"));
2983 free_colors(&view->state.diff.colors);
2987 err = add_color(&view->state.diff.colors,
2988 "^date: ", TOG_COLOR_DATE,
2989 get_color_value("TOG_COLOR_DATE"));
2991 free_colors(&view->state.diff.colors);
2996 if (log_view && view_is_splitscreen(view))
2997 show_log_view(log_view); /* draw vborder */
2998 diff_view_indicate_progress(view);
3000 err = create_diff(&view->state.diff);
3002 free(view->state.diff.id1);
3003 view->state.diff.id1 = NULL;
3004 free(view->state.diff.id2);
3005 view->state.diff.id2 = NULL;
3009 view->show = show_diff_view;
3010 view->input = input_diff_view;
3011 view->close = close_diff_view;
3016 static const struct got_error *
3017 close_diff_view(struct tog_view *view)
3019 const struct got_error *err = NULL;
3021 free(view->state.diff.id1);
3022 view->state.diff.id1 = NULL;
3023 free(view->state.diff.id2);
3024 view->state.diff.id2 = NULL;
3025 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3026 err = got_error_from_errno("fclose");
3027 free_colors(&view->state.diff.colors);
3031 static const struct got_error *
3032 show_diff_view(struct tog_view *view)
3034 const struct got_error *err;
3035 struct tog_diff_view_state *s = &view->state.diff;
3036 char *id_str1 = NULL, *id_str2, *header;
3039 err = got_object_id_str(&id_str1, s->id1);
3043 err = got_object_id_str(&id_str2, s->id2);
3047 if (asprintf(&header, "diff %s %s",
3048 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3049 err = got_error_from_errno("asprintf");
3057 return draw_file(view, s->f, &s->first_displayed_line,
3058 &s->last_displayed_line, &s->eof, view->nlines,
3059 header, &s->colors);
3062 static const struct got_error *
3063 set_selected_commit(struct tog_diff_view_state *s,
3064 struct commit_queue_entry *entry)
3066 const struct got_error *err;
3067 const struct got_object_id_queue *parent_ids;
3068 struct got_commit_object *selected_commit;
3069 struct got_object_qid *pid;
3072 s->id2 = got_object_id_dup(entry->id);
3074 return got_error_from_errno("got_object_id_dup");
3076 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3079 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3081 pid = SIMPLEQ_FIRST(parent_ids);
3082 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3083 got_object_commit_close(selected_commit);
3087 static const struct got_error *
3088 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3089 struct tog_view **focus_view, struct tog_view *view, int ch)
3091 const struct got_error *err = NULL;
3092 struct tog_diff_view_state *s = &view->state.diff;
3093 struct tog_log_view_state *ls;
3094 struct commit_queue_entry *entry;
3100 if (s->first_displayed_line > 1)
3101 s->first_displayed_line--;
3105 if (s->first_displayed_line == 1)
3108 while (i++ < view->nlines - 1 &&
3109 s->first_displayed_line > 1)
3110 s->first_displayed_line--;
3115 s->first_displayed_line++;
3123 while (!s->eof && i++ < view->nlines - 1) {
3125 line = parse_next_line(s->f, NULL);
3126 s->first_displayed_line++;
3132 if (s->diff_context > 0) {
3134 diff_view_indicate_progress(view);
3135 err = create_diff(s);
3139 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3141 diff_view_indicate_progress(view);
3142 err = create_diff(s);
3147 if (s->log_view == NULL)
3149 ls = &s->log_view->state.log;
3150 entry = TAILQ_PREV(ls->selected_entry,
3151 commit_queue_head, entry);
3155 err = input_log_view(NULL, NULL, NULL, s->log_view,
3160 err = set_selected_commit(s, entry);
3164 s->first_displayed_line = 1;
3165 s->last_displayed_line = view->nlines;
3167 diff_view_indicate_progress(view);
3168 err = create_diff(s);
3172 if (s->log_view == NULL)
3174 ls = &s->log_view->state.log;
3176 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3177 ls->thread_args.commits_needed++;
3179 /* Display "loading..." in log view. */
3180 show_log_view(s->log_view);
3184 err = trigger_log_thread(1 /* load_all */,
3185 &ls->thread_args.commits_needed,
3186 &ls->thread_args.log_complete,
3187 &ls->thread_args.need_commits);
3191 err = input_log_view(NULL, NULL, NULL, s->log_view,
3196 entry = TAILQ_NEXT(ls->selected_entry, entry);
3200 err = set_selected_commit(s, entry);
3204 s->first_displayed_line = 1;
3205 s->last_displayed_line = view->nlines;
3207 diff_view_indicate_progress(view);
3208 err = create_diff(s);
3217 static const struct got_error *
3218 cmd_diff(int argc, char *argv[])
3220 const struct got_error *error = NULL;
3221 struct got_repository *repo = NULL;
3222 struct got_reflist_head refs;
3223 struct got_object_id *id1 = NULL, *id2 = NULL;
3224 char *repo_path = NULL;
3225 char *id_str1 = NULL, *id_str2 = NULL;
3227 struct tog_view *view;
3229 SIMPLEQ_INIT(&refs);
3232 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3237 while ((ch = getopt(argc, argv, "")) != -1) {
3249 usage_diff(); /* TODO show local worktree changes */
3250 } else if (argc == 2) {
3251 repo_path = getcwd(NULL, 0);
3252 if (repo_path == NULL)
3253 return got_error_from_errno("getcwd");
3256 } else if (argc == 3) {
3257 repo_path = realpath(argv[0], NULL);
3258 if (repo_path == NULL)
3259 return got_error_from_errno2("realpath", argv[0]);
3267 error = got_repo_open(&repo, repo_path, NULL);
3271 error = apply_unveil(got_repo_get_path(repo), NULL);
3275 error = got_repo_match_object_id_prefix(&id1, id_str1,
3276 GOT_OBJ_TYPE_ANY, repo);
3280 error = got_repo_match_object_id_prefix(&id2, id_str2,
3281 GOT_OBJ_TYPE_ANY, repo);
3285 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3289 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3291 error = got_error_from_errno("view_open");
3294 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3297 error = view_loop(view);
3301 got_repo_close(repo);
3302 got_ref_list_free(&refs);
3310 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3315 struct tog_blame_line {
3317 struct got_object_id *id;
3320 static const struct got_error *
3321 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3322 const char *path, struct tog_blame_line *lines, int nlines,
3323 int blame_complete, int selected_line, int *first_displayed_line,
3324 int *last_displayed_line, int *eof, int max_lines,
3325 struct tog_colors *colors)
3327 const struct got_error *err;
3328 int lineno = 0, nprinted = 0;
3333 struct tog_blame_line *blame_line;
3334 struct got_object_id *prev_id = NULL;
3336 struct tog_color *tc;
3338 err = got_object_id_str(&id_str, id);
3343 werase(view->window);
3345 if (asprintf(&line, "commit %s", id_str) == -1) {
3346 err = got_error_from_errno("asprintf");
3351 err = format_line(&wline, &width, line, view->ncols, 0);
3356 if (view_needs_focus_indication(view))
3357 wstandout(view->window);
3358 tc = get_color(colors, TOG_COLOR_COMMIT);
3360 wattr_on(view->window,
3361 COLOR_PAIR(tc->colorpair), NULL);
3362 waddwstr(view->window, wline);
3364 wattr_off(view->window,
3365 COLOR_PAIR(tc->colorpair), NULL);
3366 if (view_needs_focus_indication(view))
3367 wstandend(view->window);
3370 if (width < view->ncols - 1)
3371 waddch(view->window, '\n');
3373 if (asprintf(&line, "[%d/%d] %s%s",
3374 *first_displayed_line - 1 + selected_line, nlines,
3375 blame_complete ? "" : "annotating... ", path) == -1) {
3377 return got_error_from_errno("asprintf");
3380 err = format_line(&wline, &width, line, view->ncols, 0);
3385 waddwstr(view->window, wline);
3388 if (width < view->ncols - 1)
3389 waddch(view->window, '\n');
3392 while (nprinted < max_lines - 2) {
3393 line = parse_next_line(f, &len);
3398 if (++lineno < *first_displayed_line) {
3403 if (view->ncols <= 9) {
3405 wline = wcsdup(L"");
3407 err = got_error_from_errno("wcsdup");
3409 err = format_line(&wline, &width, line,
3410 view->ncols - 9, 9);
3418 if (view->focussed && nprinted == selected_line - 1)
3419 wstandout(view->window);
3422 blame_line = &lines[lineno - 1];
3423 if (blame_line->annotated && prev_id &&
3424 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3426 nprinted == selected_line - 1)) {
3427 waddstr(view->window, " ");
3428 } else if (blame_line->annotated) {
3430 err = got_object_id_str(&id_str, blame_line->id);
3436 tc = get_color(colors, TOG_COLOR_COMMIT);
3438 wattr_on(view->window,
3439 COLOR_PAIR(tc->colorpair), NULL);
3440 wprintw(view->window, "%.8s", id_str);
3442 wattr_off(view->window,
3443 COLOR_PAIR(tc->colorpair), NULL);
3445 prev_id = blame_line->id;
3447 waddstr(view->window, "........");
3451 waddstr(view->window, "........");
3455 if (view->focussed && nprinted == selected_line - 1)
3456 wstandend(view->window);
3457 waddstr(view->window, " ");
3459 waddwstr(view->window, wline);
3460 if (width <= view->ncols - 1)
3461 waddch(view->window, '\n');
3462 if (++nprinted == 1)
3463 *first_displayed_line = lineno;
3468 *last_displayed_line = lineno;
3475 static const struct got_error *
3476 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3478 const struct got_error *err = NULL;
3479 struct tog_blame_cb_args *a = arg;
3480 struct tog_blame_line *line;
3483 if (nlines != a->nlines ||
3484 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3485 return got_error(GOT_ERR_RANGE);
3487 errcode = pthread_mutex_lock(&tog_mutex);
3489 return got_error_set_errno(errcode, "pthread_mutex_lock");
3491 if (*a->quit) { /* user has quit the blame view */
3492 err = got_error(GOT_ERR_ITER_COMPLETED);
3497 goto done; /* no change in this commit */
3499 line = &a->lines[lineno - 1];
3500 if (line->annotated)
3503 line->id = got_object_id_dup(id);
3504 if (line->id == NULL) {
3505 err = got_error_from_errno("got_object_id_dup");
3508 line->annotated = 1;
3510 errcode = pthread_mutex_unlock(&tog_mutex);
3512 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3517 blame_thread(void *arg)
3519 const struct got_error *err;
3520 struct tog_blame_thread_args *ta = arg;
3521 struct tog_blame_cb_args *a = ta->cb_args;
3524 err = got_blame(ta->path, a->commit_id, ta->repo,
3525 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3526 if (err && err->code == GOT_ERR_CANCELLED)
3529 errcode = pthread_mutex_lock(&tog_mutex);
3531 return (void *)got_error_set_errno(errcode,
3532 "pthread_mutex_lock");
3534 got_repo_close(ta->repo);
3538 errcode = pthread_mutex_unlock(&tog_mutex);
3539 if (errcode && err == NULL)
3540 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3545 static struct got_object_id *
3546 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3547 int first_displayed_line, int selected_line)
3549 struct tog_blame_line *line;
3554 line = &lines[first_displayed_line - 1 + selected_line - 1];
3555 if (!line->annotated)
3561 static const struct got_error *
3562 stop_blame(struct tog_blame *blame)
3564 const struct got_error *err = NULL;
3567 if (blame->thread) {
3569 errcode = pthread_mutex_unlock(&tog_mutex);
3571 return got_error_set_errno(errcode,
3572 "pthread_mutex_unlock");
3573 errcode = pthread_join(blame->thread, (void **)&err);
3575 return got_error_set_errno(errcode, "pthread_join");
3576 errcode = pthread_mutex_lock(&tog_mutex);
3578 return got_error_set_errno(errcode,
3579 "pthread_mutex_lock");
3580 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3582 blame->thread = NULL;
3584 if (blame->thread_args.repo) {
3585 got_repo_close(blame->thread_args.repo);
3586 blame->thread_args.repo = NULL;
3589 if (fclose(blame->f) != 0 && err == NULL)
3590 err = got_error_from_errno("fclose");
3594 for (i = 0; i < blame->nlines; i++)
3595 free(blame->lines[i].id);
3597 blame->lines = NULL;
3599 free(blame->cb_args.commit_id);
3600 blame->cb_args.commit_id = NULL;
3605 static const struct got_error *
3606 cancel_blame_view(void *arg)
3608 const struct got_error *err = NULL;
3612 errcode = pthread_mutex_lock(&tog_mutex);
3614 return got_error_set_errno(errcode,
3615 "pthread_mutex_unlock");
3618 err = got_error(GOT_ERR_CANCELLED);
3620 errcode = pthread_mutex_unlock(&tog_mutex);
3622 return got_error_set_errno(errcode,
3623 "pthread_mutex_lock");
3628 static const struct got_error *
3629 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3630 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3631 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3632 struct got_repository *repo)
3634 const struct got_error *err = NULL;
3635 struct got_blob_object *blob = NULL;
3636 struct got_repository *thread_repo = NULL;
3637 struct got_object_id *obj_id = NULL;
3640 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3644 return got_error(GOT_ERR_NO_OBJ);
3646 err = got_object_get_type(&obj_type, repo, obj_id);
3650 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3651 err = got_error(GOT_ERR_OBJ_TYPE);
3655 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3658 blame->f = got_opentemp();
3659 if (blame->f == NULL) {
3660 err = got_error_from_errno("got_opentemp");
3663 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3664 &blame->line_offsets, blame->f, blob);
3665 if (err || blame->nlines == 0)
3668 /* Don't include \n at EOF in the blame line count. */
3669 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3672 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3673 if (blame->lines == NULL) {
3674 err = got_error_from_errno("calloc");
3678 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3682 blame->cb_args.view = view;
3683 blame->cb_args.lines = blame->lines;
3684 blame->cb_args.nlines = blame->nlines;
3685 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3686 if (blame->cb_args.commit_id == NULL) {
3687 err = got_error_from_errno("got_object_id_dup");
3690 blame->cb_args.quit = done;
3692 blame->thread_args.path = path;
3693 blame->thread_args.repo = thread_repo;
3694 blame->thread_args.cb_args = &blame->cb_args;
3695 blame->thread_args.complete = blame_complete;
3696 blame->thread_args.cancel_cb = cancel_blame_view;
3697 blame->thread_args.cancel_arg = done;
3698 *blame_complete = 0;
3702 got_object_blob_close(blob);
3709 static const struct got_error *
3710 open_blame_view(struct tog_view *view, char *path,
3711 struct got_object_id *commit_id, struct got_reflist_head *refs,
3712 struct got_repository *repo)
3714 const struct got_error *err = NULL;
3715 struct tog_blame_view_state *s = &view->state.blame;
3717 SIMPLEQ_INIT(&s->blamed_commits);
3719 s->path = strdup(path);
3720 if (s->path == NULL)
3721 return got_error_from_errno("strdup");
3723 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3729 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3730 s->first_displayed_line = 1;
3731 s->last_displayed_line = view->nlines;
3732 s->selected_line = 1;
3733 s->blame_complete = 0;
3736 s->commit_id = commit_id;
3737 memset(&s->blame, 0, sizeof(s->blame));
3739 SIMPLEQ_INIT(&s->colors);
3740 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3741 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3742 get_color_value("TOG_COLOR_COMMIT"));
3747 view->show = show_blame_view;
3748 view->input = input_blame_view;
3749 view->close = close_blame_view;
3750 view->search_start = search_start_blame_view;
3751 view->search_next = search_next_blame_view;
3753 return run_blame(&s->blame, view, &s->blame_complete,
3754 &s->first_displayed_line, &s->last_displayed_line,
3755 &s->selected_line, &s->done, &s->eof, s->path,
3756 s->blamed_commit->id, s->repo);
3759 static const struct got_error *
3760 close_blame_view(struct tog_view *view)
3762 const struct got_error *err = NULL;
3763 struct tog_blame_view_state *s = &view->state.blame;
3765 if (s->blame.thread)
3766 err = stop_blame(&s->blame);
3768 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3769 struct got_object_qid *blamed_commit;
3770 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3771 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3772 got_object_qid_free(blamed_commit);
3776 free_colors(&s->colors);
3781 static const struct got_error *
3782 search_start_blame_view(struct tog_view *view)
3784 struct tog_blame_view_state *s = &view->state.blame;
3786 s->matched_line = 0;
3790 static const struct got_error *
3791 search_next_blame_view(struct tog_view *view)
3793 struct tog_blame_view_state *s = &view->state.blame;
3796 if (!view->searching) {
3797 view->search_next_done = 1;
3801 if (s->matched_line) {
3802 if (view->searching == TOG_SEARCH_FORWARD)
3803 lineno = s->matched_line + 1;
3805 lineno = s->matched_line - 1;
3807 if (view->searching == TOG_SEARCH_FORWARD)
3810 lineno = s->blame.nlines;
3818 if (lineno <= 0 || lineno > s->blame.nlines) {
3819 if (s->matched_line == 0) {
3820 view->search_next_done = 1;
3825 if (view->searching == TOG_SEARCH_FORWARD)
3828 lineno = s->blame.nlines;
3831 offset = s->blame.line_offsets[lineno - 1];
3832 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3834 return got_error_from_errno("fseeko");
3837 line = parse_next_line(s->blame.f, &len);
3838 if (line && match_line(line, &view->regex)) {
3839 view->search_next_done = 1;
3840 s->matched_line = lineno;
3845 if (view->searching == TOG_SEARCH_FORWARD)
3851 if (s->matched_line) {
3852 s->first_displayed_line = s->matched_line;
3853 s->selected_line = 1;
3859 static const struct got_error *
3860 show_blame_view(struct tog_view *view)
3862 const struct got_error *err = NULL;
3863 struct tog_blame_view_state *s = &view->state.blame;
3866 if (s->blame.thread == NULL) {
3867 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3868 &s->blame.thread_args);
3870 return got_error_set_errno(errcode, "pthread_create");
3872 halfdelay(1); /* fast refresh while annotating */
3875 if (s->blame_complete)
3876 halfdelay(10); /* disable fast refresh */
3878 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3879 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3880 s->selected_line, &s->first_displayed_line,
3881 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3887 static const struct got_error *
3888 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3889 struct tog_view **focus_view, struct tog_view *view, int ch)
3891 const struct got_error *err = NULL, *thread_err = NULL;
3892 struct tog_view *diff_view;
3893 struct tog_blame_view_state *s = &view->state.blame;
3902 if (s->selected_line > 1)
3904 else if (s->selected_line == 1 &&
3905 s->first_displayed_line > 1)
3906 s->first_displayed_line--;
3909 if (s->first_displayed_line == 1) {
3910 s->selected_line = 1;
3913 if (s->first_displayed_line > view->nlines - 2)
3914 s->first_displayed_line -=
3917 s->first_displayed_line = 1;
3921 if (s->selected_line < view->nlines - 2 &&
3922 s->first_displayed_line +
3923 s->selected_line <= s->blame.nlines)
3925 else if (s->last_displayed_line <
3927 s->first_displayed_line++;
3931 struct got_object_id *id = NULL;
3932 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3933 s->first_displayed_line, s->selected_line);
3937 struct got_commit_object *commit;
3938 struct got_object_qid *pid;
3939 struct got_object_id *blob_id = NULL;
3941 err = got_object_open_as_commit(&commit,
3945 pid = SIMPLEQ_FIRST(
3946 got_object_commit_get_parent_ids(commit));
3948 got_object_commit_close(commit);
3951 /* Check if path history ends here. */
3952 err = got_object_id_by_path(&blob_id, s->repo,
3955 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3957 got_object_commit_close(commit);
3960 err = got_object_get_type(&obj_type, s->repo,
3963 /* Can't blame non-blob type objects. */
3964 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3965 got_object_commit_close(commit);
3968 err = got_object_qid_alloc(&s->blamed_commit,
3970 got_object_commit_close(commit);
3972 if (got_object_id_cmp(id,
3973 s->blamed_commit->id) == 0)
3975 err = got_object_qid_alloc(&s->blamed_commit,
3981 thread_err = stop_blame(&s->blame);
3985 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3986 s->blamed_commit, entry);
3987 err = run_blame(&s->blame, view, &s->blame_complete,
3988 &s->first_displayed_line, &s->last_displayed_line,
3989 &s->selected_line, &s->done, &s->eof,
3990 s->path, s->blamed_commit->id, s->repo);
3996 struct got_object_qid *first;
3997 first = SIMPLEQ_FIRST(&s->blamed_commits);
3998 if (!got_object_id_cmp(first->id, s->commit_id))
4001 thread_err = stop_blame(&s->blame);
4005 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4006 got_object_qid_free(s->blamed_commit);
4008 SIMPLEQ_FIRST(&s->blamed_commits);
4009 err = run_blame(&s->blame, view, &s->blame_complete,
4010 &s->first_displayed_line, &s->last_displayed_line,
4011 &s->selected_line, &s->done, &s->eof, s->path,
4012 s->blamed_commit->id, s->repo);
4019 struct got_object_id *id = NULL;
4020 struct got_object_qid *pid;
4021 struct got_commit_object *commit = NULL;
4022 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4023 s->first_displayed_line, s->selected_line);
4026 err = got_object_open_as_commit(&commit, s->repo, id);
4029 pid = SIMPLEQ_FIRST(
4030 got_object_commit_get_parent_ids(commit));
4031 if (view_is_parent_view(view))
4032 begin_x = view_split_begin_x(view->begin_x);
4033 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4034 if (diff_view == NULL) {
4035 got_object_commit_close(commit);
4036 err = got_error_from_errno("view_open");
4039 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4040 id, NULL, s->refs, s->repo);
4041 got_object_commit_close(commit);
4043 view_close(diff_view);
4046 if (view_is_parent_view(view)) {
4047 err = view_close_child(view);
4050 err = view_set_child(view, diff_view);
4052 view_close(diff_view);
4055 *focus_view = diff_view;
4056 view->child_focussed = 1;
4058 *new_view = diff_view;
4065 if (s->last_displayed_line >= s->blame.nlines &&
4066 s->selected_line >= MIN(s->blame.nlines,
4067 view->nlines - 2)) {
4070 if (s->last_displayed_line >= s->blame.nlines &&
4071 s->selected_line < view->nlines - 2) {
4072 s->selected_line = MIN(s->blame.nlines,
4076 if (s->last_displayed_line + view->nlines - 2
4078 s->first_displayed_line +=
4081 s->first_displayed_line =
4086 if (s->selected_line > view->nlines - 2) {
4087 s->selected_line = MIN(s->blame.nlines,
4094 return thread_err ? thread_err : err;
4097 static const struct got_error *
4098 cmd_blame(int argc, char *argv[])
4100 const struct got_error *error;
4101 struct got_repository *repo = NULL;
4102 struct got_reflist_head refs;
4103 struct got_worktree *worktree = NULL;
4104 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4105 struct got_object_id *commit_id = NULL;
4106 char *commit_id_str = NULL;
4108 struct tog_view *view;
4110 SIMPLEQ_INIT(&refs);
4113 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4118 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4121 commit_id_str = optarg;
4124 repo_path = realpath(optarg, NULL);
4125 if (repo_path == NULL)
4126 return got_error_from_errno2("realpath",
4143 cwd = getcwd(NULL, 0);
4145 error = got_error_from_errno("getcwd");
4148 if (repo_path == NULL) {
4149 error = got_worktree_open(&worktree, cwd);
4150 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4156 strdup(got_worktree_get_repo_path(worktree));
4157 if (repo_path == NULL)
4158 error = got_error_from_errno("strdup");
4162 repo_path = strdup(cwd);
4163 if (repo_path == NULL) {
4164 error = got_error_from_errno("strdup");
4172 error = got_repo_open(&repo, repo_path, NULL);
4176 error = apply_unveil(got_repo_get_path(repo), NULL);
4181 const char *prefix = got_worktree_get_path_prefix(worktree);
4182 char *p, *worktree_subdir = cwd +
4183 strlen(got_worktree_get_root_path(worktree));
4184 if (asprintf(&p, "%s%s%s%s%s",
4185 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4186 worktree_subdir, worktree_subdir[0] ? "/" : "",
4188 error = got_error_from_errno("asprintf");
4191 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4194 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4199 if (commit_id_str == NULL) {
4200 struct got_reference *head_ref;
4201 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4204 error = got_ref_resolve(&commit_id, repo, head_ref);
4205 got_ref_close(head_ref);
4207 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4209 if (error->code != GOT_ERR_NOT_REF)
4211 error = got_repo_match_object_id_prefix(&commit_id,
4212 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4218 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4222 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4224 error = got_error_from_errno("view_open");
4227 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4231 /* Release work tree lock. */
4232 got_worktree_close(worktree);
4235 error = view_loop(view);
4241 got_worktree_close(worktree);
4243 got_repo_close(repo);
4244 got_ref_list_free(&refs);
4248 static const struct got_error *
4249 draw_tree_entries(struct tog_view *view,
4250 struct got_tree_entry **first_displayed_entry,
4251 struct got_tree_entry **last_displayed_entry,
4252 struct got_tree_entry **selected_entry, int *ndisplayed,
4253 const char *label, int show_ids, const char *parent_path,
4254 struct got_tree_object *tree, int selected, int limit,
4255 int isroot, struct tog_colors *colors)
4257 const struct got_error *err = NULL;
4258 struct got_tree_entry *te;
4260 struct tog_color *tc;
4261 int width, n, i, nentries;
4265 werase(view->window);
4270 err = format_line(&wline, &width, label, view->ncols, 0);
4273 if (view_needs_focus_indication(view))
4274 wstandout(view->window);
4275 tc = get_color(colors, TOG_COLOR_COMMIT);
4277 wattr_on(view->window,
4278 COLOR_PAIR(tc->colorpair), NULL);
4279 waddwstr(view->window, wline);
4281 wattr_off(view->window,
4282 COLOR_PAIR(tc->colorpair), NULL);
4283 if (view_needs_focus_indication(view))
4284 wstandend(view->window);
4287 if (width < view->ncols - 1)
4288 waddch(view->window, '\n');
4291 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4294 waddwstr(view->window, wline);
4297 if (width < view->ncols - 1)
4298 waddch(view->window, '\n');
4301 waddch(view->window, '\n');
4305 if (*first_displayed_entry == NULL) {
4306 te = got_object_tree_get_first_entry(tree);
4307 if (selected == 0) {
4309 wstandout(view->window);
4310 *selected_entry = NULL;
4312 waddstr(view->window, " ..\n"); /* parent directory */
4313 if (selected == 0 && view->focussed)
4314 wstandend(view->window);
4321 te = *first_displayed_entry;
4324 nentries = got_object_tree_get_nentries(tree);
4325 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4326 char *line = NULL, *id_str = NULL;
4327 const char *modestr = "";
4330 te = got_object_tree_get_entry(tree, i);
4331 mode = got_tree_entry_get_mode(te);
4334 err = got_object_id_str(&id_str,
4335 got_tree_entry_get_id(te));
4337 return got_error_from_errno(
4338 "got_object_id_str");
4340 if (got_object_tree_entry_is_submodule(te))
4342 else if (S_ISLNK(mode))
4344 else if (S_ISDIR(mode))
4346 else if (mode & S_IXUSR)
4348 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4349 got_tree_entry_get_name(te), modestr) == -1) {
4351 return got_error_from_errno("asprintf");
4354 err = format_line(&wline, &width, line, view->ncols, 0);
4359 if (n == selected) {
4361 wstandout(view->window);
4362 *selected_entry = te;
4364 tc = match_color(colors, line);
4366 wattr_on(view->window,
4367 COLOR_PAIR(tc->colorpair), NULL);
4368 waddwstr(view->window, wline);
4370 wattr_off(view->window,
4371 COLOR_PAIR(tc->colorpair), NULL);
4372 if (width < view->ncols - 1)
4373 waddch(view->window, '\n');
4374 if (n == selected && view->focussed)
4375 wstandend(view->window);
4381 *last_displayed_entry = te;
4390 tree_scroll_up(struct tog_view *view,
4391 struct got_tree_entry **first_displayed_entry, int maxscroll,
4392 struct got_tree_object *tree, int isroot)
4394 struct got_tree_entry *te;
4397 if (*first_displayed_entry == NULL)
4400 te = got_object_tree_get_entry(tree, 0);
4401 if (*first_displayed_entry == te) {
4403 *first_displayed_entry = NULL;
4408 while (*first_displayed_entry && i < maxscroll) {
4409 *first_displayed_entry = got_tree_entry_get_prev(tree,
4410 *first_displayed_entry);
4413 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4414 *first_displayed_entry = NULL;
4418 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4419 struct got_tree_entry *last_displayed_entry,
4420 struct got_tree_object *tree)
4422 struct got_tree_entry *next, *last;
4425 if (*first_displayed_entry)
4426 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4428 next = got_object_tree_get_first_entry(tree);
4430 last = last_displayed_entry;
4431 while (next && last && n++ < maxscroll) {
4432 last = got_tree_entry_get_next(tree, last);
4434 *first_displayed_entry = next;
4435 next = got_tree_entry_get_next(tree, next);
4441 static const struct got_error *
4442 tree_entry_path(char **path, struct tog_parent_trees *parents,
4443 struct got_tree_entry *te)
4445 const struct got_error *err = NULL;
4446 struct tog_parent_tree *pt;
4447 size_t len = 2; /* for leading slash and NUL */
4449 TAILQ_FOREACH(pt, parents, entry)
4450 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4453 len += strlen(got_tree_entry_get_name(te));
4455 *path = calloc(1, len);
4457 return got_error_from_errno("calloc");
4460 pt = TAILQ_LAST(parents, tog_parent_trees);
4462 const char *name = got_tree_entry_get_name(pt->selected_entry);
4463 if (strlcat(*path, name, len) >= len) {
4464 err = got_error(GOT_ERR_NO_SPACE);
4467 if (strlcat(*path, "/", len) >= len) {
4468 err = got_error(GOT_ERR_NO_SPACE);
4471 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4474 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4475 err = got_error(GOT_ERR_NO_SPACE);
4487 static const struct got_error *
4488 blame_tree_entry(struct tog_view **new_view, int begin_x,
4489 struct got_tree_entry *te, struct tog_parent_trees *parents,
4490 struct got_object_id *commit_id, struct got_reflist_head *refs,
4491 struct got_repository *repo)
4493 const struct got_error *err = NULL;
4495 struct tog_view *blame_view;
4499 err = tree_entry_path(&path, parents, te);
4503 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4504 if (blame_view == NULL) {
4505 err = got_error_from_errno("view_open");
4509 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4511 if (err->code == GOT_ERR_CANCELLED)
4513 view_close(blame_view);
4515 *new_view = blame_view;
4521 static const struct got_error *
4522 log_tree_entry(struct tog_view **new_view, int begin_x,
4523 struct got_tree_entry *te, struct tog_parent_trees *parents,
4524 struct got_object_id *commit_id, struct got_reflist_head *refs,
4525 struct got_repository *repo)
4527 struct tog_view *log_view;
4528 const struct got_error *err = NULL;
4533 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4534 if (log_view == NULL)
4535 return got_error_from_errno("view_open");
4537 err = tree_entry_path(&path, parents, te);
4541 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4543 view_close(log_view);
4545 *new_view = log_view;
4550 static const struct got_error *
4551 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4552 struct got_object_id *commit_id, struct got_reflist_head *refs,
4553 struct got_repository *repo)
4555 const struct got_error *err = NULL;
4556 char *commit_id_str = NULL;
4557 struct tog_tree_view_state *s = &view->state.tree;
4559 TAILQ_INIT(&s->parents);
4561 err = got_object_id_str(&commit_id_str, commit_id);
4565 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4566 err = got_error_from_errno("asprintf");
4570 s->root = s->tree = root;
4571 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4572 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4573 s->commit_id = got_object_id_dup(commit_id);
4574 if (s->commit_id == NULL) {
4575 err = got_error_from_errno("got_object_id_dup");
4581 SIMPLEQ_INIT(&s->colors);
4583 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4584 err = add_color(&s->colors, "\\$$",
4585 TOG_COLOR_TREE_SUBMODULE,
4586 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4589 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4590 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4592 free_colors(&s->colors);
4595 err = add_color(&s->colors, "/$",
4596 TOG_COLOR_TREE_DIRECTORY,
4597 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4599 free_colors(&s->colors);
4603 err = add_color(&s->colors, "\\*$",
4604 TOG_COLOR_TREE_EXECUTABLE,
4605 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4607 free_colors(&s->colors);
4611 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4612 get_color_value("TOG_COLOR_COMMIT"));
4614 free_colors(&s->colors);
4619 view->show = show_tree_view;
4620 view->input = input_tree_view;
4621 view->close = close_tree_view;
4622 view->search_start = search_start_tree_view;
4623 view->search_next = search_next_tree_view;
4625 free(commit_id_str);
4627 free(s->tree_label);
4628 s->tree_label = NULL;
4633 static const struct got_error *
4634 close_tree_view(struct tog_view *view)
4636 struct tog_tree_view_state *s = &view->state.tree;
4638 free_colors(&s->colors);
4639 free(s->tree_label);
4640 s->tree_label = NULL;
4642 s->commit_id = NULL;
4643 while (!TAILQ_EMPTY(&s->parents)) {
4644 struct tog_parent_tree *parent;
4645 parent = TAILQ_FIRST(&s->parents);
4646 TAILQ_REMOVE(&s->parents, parent, entry);
4650 if (s->tree != s->root)
4651 got_object_tree_close(s->tree);
4652 got_object_tree_close(s->root);
4657 static const struct got_error *
4658 search_start_tree_view(struct tog_view *view)
4660 struct tog_tree_view_state *s = &view->state.tree;
4662 s->matched_entry = NULL;
4667 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4669 regmatch_t regmatch;
4671 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4675 static const struct got_error *
4676 search_next_tree_view(struct tog_view *view)
4678 struct tog_tree_view_state *s = &view->state.tree;
4679 struct got_tree_entry *te = NULL;
4681 if (!view->searching) {
4682 view->search_next_done = 1;
4686 if (s->matched_entry) {
4687 if (view->searching == TOG_SEARCH_FORWARD) {
4688 if (s->selected_entry)
4689 te = got_tree_entry_get_next(s->tree,
4692 te = got_object_tree_get_first_entry(s->tree);
4694 if (s->selected_entry == NULL)
4695 te = got_object_tree_get_last_entry(s->tree);
4697 te = got_tree_entry_get_prev(s->tree,
4701 if (view->searching == TOG_SEARCH_FORWARD)
4702 te = got_object_tree_get_first_entry(s->tree);
4704 te = got_object_tree_get_last_entry(s->tree);
4709 if (s->matched_entry == NULL) {
4710 view->search_next_done = 1;
4713 if (view->searching == TOG_SEARCH_FORWARD)
4714 te = got_object_tree_get_first_entry(s->tree);
4716 te = got_object_tree_get_last_entry(s->tree);
4719 if (match_tree_entry(te, &view->regex)) {
4720 view->search_next_done = 1;
4721 s->matched_entry = te;
4725 if (view->searching == TOG_SEARCH_FORWARD)
4726 te = got_tree_entry_get_next(s->tree, te);
4728 te = got_tree_entry_get_prev(s->tree, te);
4731 if (s->matched_entry) {
4732 s->first_displayed_entry = s->matched_entry;
4739 static const struct got_error *
4740 show_tree_view(struct tog_view *view)
4742 const struct got_error *err = NULL;
4743 struct tog_tree_view_state *s = &view->state.tree;
4746 err = tree_entry_path(&parent_path, &s->parents, NULL);
4750 err = draw_tree_entries(view, &s->first_displayed_entry,
4751 &s->last_displayed_entry, &s->selected_entry,
4752 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4753 s->tree, s->selected, view->nlines, s->tree == s->root,
4761 static const struct got_error *
4762 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4763 struct tog_view **focus_view, struct tog_view *view, int ch)
4765 const struct got_error *err = NULL;
4766 struct tog_tree_view_state *s = &view->state.tree;
4767 struct tog_view *log_view;
4768 int begin_x = 0, nscrolled;
4772 s->show_ids = !s->show_ids;
4775 if (!s->selected_entry)
4777 if (view_is_parent_view(view))
4778 begin_x = view_split_begin_x(view->begin_x);
4779 err = log_tree_entry(&log_view, begin_x,
4780 s->selected_entry, &s->parents,
4781 s->commit_id, s->refs, s->repo);
4782 if (view_is_parent_view(view)) {
4783 err = view_close_child(view);
4786 err = view_set_child(view, log_view);
4788 view_close(log_view);
4791 *focus_view = log_view;
4792 view->child_focussed = 1;
4794 *new_view = log_view;
4798 if (s->selected > 0) {
4800 if (s->selected == 0)
4803 if (s->selected > 0)
4805 tree_scroll_up(view, &s->first_displayed_entry, 1,
4806 s->tree, s->tree == s->root);
4809 tree_scroll_up(view, &s->first_displayed_entry,
4810 MAX(0, view->nlines - 4 - s->selected), s->tree,
4811 s->tree == s->root);
4813 if (got_object_tree_get_first_entry(s->tree) ==
4814 s->first_displayed_entry && s->tree != s->root)
4815 s->first_displayed_entry = NULL;
4819 if (s->selected < s->ndisplayed - 1) {
4823 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4825 /* can't scroll any further */
4827 tree_scroll_down(&s->first_displayed_entry, 1,
4828 s->last_displayed_entry, s->tree);
4831 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4833 /* can't scroll any further; move cursor down */
4834 if (s->selected < s->ndisplayed - 1)
4835 s->selected = s->ndisplayed - 1;
4838 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4839 view->nlines, s->last_displayed_entry, s->tree);
4840 if (nscrolled < view->nlines) {
4842 struct got_tree_entry *te;
4843 te = s->first_displayed_entry;
4846 te = got_tree_entry_get_next(s->tree, te);
4848 s->selected = ndisplayed - 1;
4854 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4855 struct tog_parent_tree *parent;
4856 /* user selected '..' */
4857 if (s->tree == s->root)
4859 parent = TAILQ_FIRST(&s->parents);
4860 TAILQ_REMOVE(&s->parents, parent,
4862 got_object_tree_close(s->tree);
4863 s->tree = parent->tree;
4864 s->first_displayed_entry =
4865 parent->first_displayed_entry;
4867 parent->selected_entry;
4868 s->selected = parent->selected;
4870 } else if (S_ISDIR(got_tree_entry_get_mode(
4871 s->selected_entry))) {
4872 struct got_tree_object *subtree;
4873 err = got_object_open_as_tree(&subtree, s->repo,
4874 got_tree_entry_get_id(s->selected_entry));
4877 err = tree_view_visit_subtree(subtree, s);
4879 got_object_tree_close(subtree);
4882 } else if (S_ISREG(got_tree_entry_get_mode(
4883 s->selected_entry))) {
4884 struct tog_view *blame_view;
4885 int begin_x = view_is_parent_view(view) ?
4886 view_split_begin_x(view->begin_x) : 0;
4888 err = blame_tree_entry(&blame_view, begin_x,
4889 s->selected_entry, &s->parents,
4890 s->commit_id, s->refs, s->repo);
4893 if (view_is_parent_view(view)) {
4894 err = view_close_child(view);
4897 err = view_set_child(view, blame_view);
4899 view_close(blame_view);
4902 *focus_view = blame_view;
4903 view->child_focussed = 1;
4905 *new_view = blame_view;
4909 if (s->selected > view->nlines)
4910 s->selected = s->ndisplayed - 1;
4923 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4928 static const struct got_error *
4929 cmd_tree(int argc, char *argv[])
4931 const struct got_error *error;
4932 struct got_repository *repo = NULL;
4933 struct got_reflist_head refs;
4934 char *repo_path = NULL;
4935 struct got_object_id *commit_id = NULL;
4936 char *commit_id_arg = NULL;
4937 struct got_commit_object *commit = NULL;
4938 struct got_tree_object *tree = NULL;
4940 struct tog_view *view;
4942 SIMPLEQ_INIT(&refs);
4945 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4950 while ((ch = getopt(argc, argv, "c:")) != -1) {
4953 commit_id_arg = optarg;
4965 struct got_worktree *worktree;
4966 char *cwd = getcwd(NULL, 0);
4968 return got_error_from_errno("getcwd");
4969 error = got_worktree_open(&worktree, cwd);
4970 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4975 strdup(got_worktree_get_repo_path(worktree));
4976 got_worktree_close(worktree);
4979 if (repo_path == NULL) {
4980 error = got_error_from_errno("strdup");
4983 } else if (argc == 1) {
4984 repo_path = realpath(argv[0], NULL);
4985 if (repo_path == NULL)
4986 return got_error_from_errno2("realpath", argv[0]);
4992 error = got_repo_open(&repo, repo_path, NULL);
4996 error = apply_unveil(got_repo_get_path(repo), NULL);
5000 if (commit_id_arg == NULL)
5001 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5003 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5005 if (error->code != GOT_ERR_NOT_REF)
5007 error = got_repo_match_object_id_prefix(&commit_id,
5008 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5014 error = got_object_open_as_commit(&commit, repo, commit_id);
5018 error = got_object_open_as_tree(&tree, repo,
5019 got_object_commit_get_tree_id(commit));
5023 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5027 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5029 error = got_error_from_errno("view_open");
5032 error = open_tree_view(view, tree, commit_id, &refs, repo);
5035 error = view_loop(view);
5040 got_object_commit_close(commit);
5042 got_object_tree_close(tree);
5044 got_repo_close(repo);
5045 got_ref_list_free(&refs);
5054 fprintf(stderr, "commands:");
5055 for (i = 0; i < nitems(tog_commands); i++) {
5056 struct tog_cmd *cmd = &tog_commands[i];
5057 fprintf(stderr, " %s", cmd->name);
5059 fputc('\n', stderr);
5065 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5073 make_argv(const char *arg0, const char *arg1)
5076 int argc = (arg1 == NULL ? 1 : 2);
5078 argv = calloc(argc, sizeof(char *));
5081 argv[0] = strdup(arg0);
5082 if (argv[0] == NULL)
5085 argv[1] = strdup(arg1);
5086 if (argv[1] == NULL)
5094 main(int argc, char *argv[])
5096 const struct got_error *error = NULL;
5097 struct tog_cmd *cmd = NULL;
5098 int ch, hflag = 0, Vflag = 0;
5099 char **cmd_argv = NULL;
5101 setlocale(LC_CTYPE, "");
5103 while ((ch = getopt(argc, argv, "hV")) != -1) {
5123 got_version_print_str();
5130 /* Build an argument vector which runs a default command. */
5131 cmd = &tog_commands[0];
5132 cmd_argv = make_argv(cmd->name, NULL);
5137 /* Did the user specific a command? */
5138 for (i = 0; i < nitems(tog_commands); i++) {
5139 if (strncmp(tog_commands[i].name, argv[0],
5140 strlen(argv[0])) == 0) {
5141 cmd = &tog_commands[i];
5147 fprintf(stderr, "%s: unknown command '%s'\n",
5148 getprogname(), argv[0]);
5157 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5161 if (error && error->code != GOT_ERR_CANCELLED)
5162 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);