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[10]; /* YY-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);
1402 if (err->code != GOT_ERR_ITER_NEED_MORE)
1404 err = got_commit_graph_fetch_commits(graph,
1405 minqueue, repo, NULL, NULL);
1414 err = got_object_open_as_commit(&commit, repo, id);
1417 entry = alloc_commit_queue_entry(commit, id);
1418 if (entry == NULL) {
1419 err = got_error_from_errno("alloc_commit_queue_entry");
1423 errcode = pthread_mutex_lock(&tog_mutex);
1425 err = got_error_set_errno(errcode,
1426 "pthread_mutex_lock");
1430 entry->idx = commits->ncommits;
1431 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1433 commits->ncommits++;
1435 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1436 err = match_commit(&have_match, id, commit, regex);
1438 pthread_mutex_lock(&tog_mutex);
1443 errcode = pthread_mutex_unlock(&tog_mutex);
1444 if (errcode && err == NULL)
1445 err = got_error_set_errno(errcode,
1446 "pthread_mutex_unlock");
1455 static const struct got_error *
1456 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1457 struct got_repository *repo)
1459 const struct got_error *err = NULL;
1460 struct got_reference *head_ref;
1464 err = got_ref_open(&head_ref, repo, branch_name, 0);
1468 err = got_ref_resolve(head_id, repo, head_ref);
1469 got_ref_close(head_ref);
1478 static const struct got_error *
1479 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1480 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1481 struct commit_queue *commits, int selected_idx, int limit,
1482 struct got_reflist_head *refs, const char *path, int commits_needed,
1483 struct tog_colors *colors)
1485 const struct got_error *err = NULL;
1486 struct tog_log_view_state *s = &view->state.log;
1487 struct commit_queue_entry *entry;
1489 int ncommits, author_cols = 4;
1490 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1491 char *refs_str = NULL;
1493 struct tog_color *tc;
1494 static const size_t date_display_cols = 9;
1499 if (ncommits == selected_idx) {
1503 entry = TAILQ_NEXT(entry, entry);
1507 if (*selected && !(view->searching && view->search_next_done == 0)) {
1508 err = got_object_id_str(&id_str, (*selected)->id);
1512 err = build_refs_str(&refs_str, refs, (*selected)->id,
1519 if (commits_needed == 0)
1520 halfdelay(10); /* disable fast refresh */
1522 if (asprintf(&ncommits_str, " [%d/%d] %s",
1523 entry ? entry->idx + 1 : 0, commits->ncommits,
1524 commits_needed > 0 ?
1525 (view->searching && view->search_next_done == 0
1526 ? "searching..." : "loading... ") :
1527 (refs_str ? refs_str : "")) == -1) {
1528 err = got_error_from_errno("asprintf");
1532 if (path && strcmp(path, "/") != 0) {
1533 if (asprintf(&header, "commit %s %s%s",
1534 id_str ? id_str : "........................................",
1535 path, ncommits_str) == -1) {
1536 err = got_error_from_errno("asprintf");
1540 } else if (asprintf(&header, "commit %s%s",
1541 id_str ? id_str : "........................................",
1542 ncommits_str) == -1) {
1543 err = got_error_from_errno("asprintf");
1547 err = format_line(&wline, &width, header, view->ncols, 0);
1551 werase(view->window);
1553 if (view_needs_focus_indication(view))
1554 wstandout(view->window);
1555 tc = get_color(colors, TOG_COLOR_COMMIT);
1557 wattr_on(view->window,
1558 COLOR_PAIR(tc->colorpair), NULL);
1559 waddwstr(view->window, wline);
1561 wattr_off(view->window,
1562 COLOR_PAIR(tc->colorpair), NULL);
1563 while (width < view->ncols) {
1564 waddch(view->window, ' ');
1567 if (view_needs_focus_indication(view))
1568 wstandend(view->window);
1573 /* Grow author column size if necessary. */
1580 if (ncommits >= limit - 1)
1582 author = strdup(got_object_commit_get_author(entry->commit));
1583 if (author == NULL) {
1584 err = got_error_from_errno("strdup");
1587 err = format_author(&wauthor, &width, author, COLS,
1589 if (author_cols < width)
1590 author_cols = width;
1594 entry = TAILQ_NEXT(entry, entry);
1601 if (ncommits >= limit - 1)
1603 if (ncommits == selected_idx)
1604 wstandout(view->window);
1605 err = draw_commit(view, entry->commit, entry->id, refs,
1606 date_display_cols, author_cols, colors);
1607 if (ncommits == selected_idx)
1608 wstandend(view->window);
1613 entry = TAILQ_NEXT(entry, entry);
1626 scroll_up(struct tog_view *view,
1627 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1628 struct commit_queue *commits)
1630 struct commit_queue_entry *entry;
1633 entry = TAILQ_FIRST(&commits->head);
1634 if (*first_displayed_entry == entry)
1637 entry = *first_displayed_entry;
1638 while (entry && nscrolled < maxscroll) {
1639 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1641 *first_displayed_entry = entry;
1647 static const struct got_error *
1648 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1649 pthread_cond_t *need_commits)
1654 halfdelay(1); /* fast refresh while loading commits */
1656 while (*commits_needed > 0) {
1660 /* Wake the log thread. */
1661 errcode = pthread_cond_signal(need_commits);
1663 return got_error_set_errno(errcode,
1664 "pthread_cond_signal");
1665 errcode = pthread_mutex_unlock(&tog_mutex);
1667 return got_error_set_errno(errcode,
1668 "pthread_mutex_unlock");
1670 errcode = pthread_mutex_lock(&tog_mutex);
1672 return got_error_set_errno(errcode,
1673 "pthread_mutex_lock");
1675 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1677 * Thread is not done yet; lose a key press
1678 * and let the user retry... this way the GUI
1679 * remains interactive while logging deep paths
1680 * with few commits in history.
1689 static const struct got_error *
1690 scroll_down(struct tog_view *view,
1691 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1692 struct commit_queue_entry **last_displayed_entry,
1693 struct commit_queue *commits, int *log_complete, int *commits_needed,
1694 pthread_cond_t *need_commits)
1696 const struct got_error *err = NULL;
1697 struct commit_queue_entry *pentry;
1700 if (*last_displayed_entry == NULL)
1703 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1704 if (pentry == NULL && !*log_complete) {
1706 * Ask the log thread for required amount of commits
1707 * plus some amount of pre-fetching.
1709 (*commits_needed) += maxscroll + 20;
1710 err = trigger_log_thread(0, commits_needed, log_complete,
1717 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1721 *last_displayed_entry = pentry;
1723 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1726 *first_displayed_entry = pentry;
1727 } while (++nscrolled < maxscroll);
1732 static const struct got_error *
1733 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1734 struct got_commit_object *commit, struct got_object_id *commit_id,
1735 struct tog_view *log_view, struct got_reflist_head *refs,
1736 struct got_repository *repo)
1738 const struct got_error *err;
1739 struct got_object_qid *parent_id;
1740 struct tog_view *diff_view;
1742 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1743 if (diff_view == NULL)
1744 return got_error_from_errno("view_open");
1746 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1747 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1748 commit_id, log_view, refs, repo);
1750 *new_view = diff_view;
1754 static const struct got_error *
1755 tree_view_visit_subtree(struct got_tree_object *subtree,
1756 struct tog_tree_view_state *s)
1758 struct tog_parent_tree *parent;
1760 parent = calloc(1, sizeof(*parent));
1762 return got_error_from_errno("calloc");
1764 parent->tree = s->tree;
1765 parent->first_displayed_entry = s->first_displayed_entry;
1766 parent->selected_entry = s->selected_entry;
1767 parent->selected = s->selected;
1768 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1771 s->first_displayed_entry = NULL;
1776 static const struct got_error *
1777 browse_commit_tree(struct tog_view **new_view, int begin_x,
1778 struct commit_queue_entry *entry, const char *path,
1779 struct got_reflist_head *refs, struct got_repository *repo)
1781 const struct got_error *err = NULL;
1782 struct got_tree_object *tree;
1783 struct tog_tree_view_state *s;
1784 struct tog_view *tree_view;
1785 char *slash, *subpath = NULL;
1788 err = got_object_open_as_tree(&tree, repo,
1789 got_object_commit_get_tree_id(entry->commit));
1793 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1794 if (tree_view == NULL)
1795 return got_error_from_errno("view_open");
1797 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1799 got_object_tree_close(tree);
1802 s = &tree_view->state.tree;
1804 *new_view = tree_view;
1806 /* Walk the path and open corresponding tree objects. */
1811 struct got_tree_entry *te;
1812 struct got_object_id *tree_id;
1815 /* Ensure the correct subtree entry is selected. */
1816 slash = strchr(p, '/');
1818 slash = strchr(p, '\0');
1820 te_name = strndup(p, slash -p);
1821 if (te_name == NULL) {
1822 err = got_error_from_errno("strndup");
1825 te = got_object_tree_find_entry(s->tree, te_name);
1827 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1832 s->selected_entry = te;
1833 s->selected = got_tree_entry_get_index(te);
1834 if (s->tree != s->root)
1835 s->selected++; /* skip '..' */
1837 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1838 /* Jump to this file's entry. */
1839 s->first_displayed_entry = s->selected_entry;
1844 slash = strchr(p, '/');
1846 subpath = strndup(path, slash - path);
1848 subpath = strdup(path);
1849 if (subpath == NULL) {
1850 err = got_error_from_errno("strdup");
1854 err = got_object_id_by_path(&tree_id, repo, entry->id,
1859 err = got_object_open_as_tree(&tree, repo, tree_id);
1864 err = tree_view_visit_subtree(tree, s);
1866 got_object_tree_close(tree);
1881 log_thread(void *arg)
1883 const struct got_error *err = NULL;
1885 struct tog_log_thread_args *a = arg;
1888 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1893 while (!done && !err && !tog_sigpipe_received) {
1894 err = queue_commits(a->graph, a->commits, 1, a->repo,
1895 a->in_repo_path, a->searching, a->search_next_done,
1898 if (err->code != GOT_ERR_ITER_COMPLETED)
1902 } else if (a->commits_needed > 0)
1903 a->commits_needed--;
1905 errcode = pthread_mutex_lock(&tog_mutex);
1907 err = got_error_set_errno(errcode,
1908 "pthread_mutex_lock");
1910 } else if (*a->quit)
1912 else if (*a->first_displayed_entry == NULL) {
1913 *a->first_displayed_entry =
1914 TAILQ_FIRST(&a->commits->head);
1915 *a->selected_entry = *a->first_displayed_entry;
1919 a->commits_needed = 0;
1920 else if (a->commits_needed == 0) {
1921 errcode = pthread_cond_wait(&a->need_commits,
1924 err = got_error_set_errno(errcode,
1925 "pthread_cond_wait");
1928 errcode = pthread_mutex_unlock(&tog_mutex);
1929 if (errcode && err == NULL)
1930 err = got_error_set_errno(errcode,
1931 "pthread_mutex_unlock");
1933 a->log_complete = 1;
1937 static const struct got_error *
1938 stop_log_thread(struct tog_log_view_state *s)
1940 const struct got_error *err = NULL;
1945 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1947 return got_error_set_errno(errcode,
1948 "pthread_cond_signal");
1949 errcode = pthread_mutex_unlock(&tog_mutex);
1951 return got_error_set_errno(errcode,
1952 "pthread_mutex_unlock");
1953 errcode = pthread_join(s->thread, (void **)&err);
1955 return got_error_set_errno(errcode, "pthread_join");
1956 errcode = pthread_mutex_lock(&tog_mutex);
1958 return got_error_set_errno(errcode,
1959 "pthread_mutex_lock");
1963 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1964 if (errcode && err == NULL)
1965 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1967 if (s->thread_args.repo) {
1968 got_repo_close(s->thread_args.repo);
1969 s->thread_args.repo = NULL;
1972 if (s->thread_args.graph) {
1973 got_commit_graph_close(s->thread_args.graph);
1974 s->thread_args.graph = NULL;
1980 static const struct got_error *
1981 close_log_view(struct tog_view *view)
1983 const struct got_error *err = NULL;
1984 struct tog_log_view_state *s = &view->state.log;
1986 err = stop_log_thread(s);
1987 free_commits(&s->commits);
1988 free(s->in_repo_path);
1989 s->in_repo_path = NULL;
1995 static const struct got_error *
1996 search_start_log_view(struct tog_view *view)
1998 struct tog_log_view_state *s = &view->state.log;
2000 s->matched_entry = NULL;
2001 s->search_entry = NULL;
2005 static const struct got_error *
2006 search_next_log_view(struct tog_view *view)
2008 const struct got_error *err = NULL;
2009 struct tog_log_view_state *s = &view->state.log;
2010 struct commit_queue_entry *entry;
2012 if (!view->searching) {
2013 view->search_next_done = 1;
2017 if (s->search_entry) {
2019 errcode = pthread_mutex_unlock(&tog_mutex);
2021 return got_error_set_errno(errcode,
2022 "pthread_mutex_unlock");
2023 ch = wgetch(view->window);
2024 errcode = pthread_mutex_lock(&tog_mutex);
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_lock");
2028 if (ch == KEY_BACKSPACE) {
2029 view->search_next_done = 1;
2032 if (view->searching == TOG_SEARCH_FORWARD)
2033 entry = TAILQ_NEXT(s->search_entry, entry);
2035 entry = TAILQ_PREV(s->search_entry,
2036 commit_queue_head, entry);
2037 } else if (s->matched_entry) {
2038 if (view->searching == TOG_SEARCH_FORWARD)
2039 entry = TAILQ_NEXT(s->selected_entry, entry);
2041 entry = TAILQ_PREV(s->selected_entry,
2042 commit_queue_head, entry);
2044 if (view->searching == TOG_SEARCH_FORWARD)
2045 entry = TAILQ_FIRST(&s->commits.head);
2047 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2053 if (entry == NULL) {
2054 if (s->thread_args.log_complete ||
2055 view->searching == TOG_SEARCH_BACKWARD) {
2056 view->search_next_done = 1;
2060 * Poke the log thread for more commits and return,
2061 * allowing the main loop to make progress. Search
2062 * will resume at s->search_entry once we come back.
2064 s->thread_args.commits_needed++;
2065 return trigger_log_thread(1,
2066 &s->thread_args.commits_needed,
2067 &s->thread_args.log_complete,
2068 &s->thread_args.need_commits);
2071 err = match_commit(&have_match, entry->id, entry->commit,
2076 view->search_next_done = 1;
2077 s->matched_entry = entry;
2081 s->search_entry = entry;
2082 if (view->searching == TOG_SEARCH_FORWARD)
2083 entry = TAILQ_NEXT(entry, entry);
2085 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2088 if (s->matched_entry) {
2089 int cur = s->selected_entry->idx;
2090 while (cur < s->matched_entry->idx) {
2091 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2096 while (cur > s->matched_entry->idx) {
2097 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2104 s->search_entry = NULL;
2109 static const struct got_error *
2110 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2111 struct got_reflist_head *refs, struct got_repository *repo,
2112 const char *head_ref_name, const char *path, int check_disk)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct got_repository *thread_repo = NULL;
2117 struct got_commit_graph *thread_graph = NULL;
2120 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2124 /* The commit queue only contains commits being displayed. */
2125 TAILQ_INIT(&s->commits.head);
2126 s->commits.ncommits = 0;
2130 s->head_ref_name = head_ref_name;
2131 s->start_id = got_object_id_dup(start_id);
2132 if (s->start_id == NULL) {
2133 err = got_error_from_errno("got_object_id_dup");
2137 SIMPLEQ_INIT(&s->colors);
2138 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2139 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2140 get_color_value("TOG_COLOR_COMMIT"));
2143 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2144 get_color_value("TOG_COLOR_AUTHOR"));
2146 free_colors(&s->colors);
2149 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2150 get_color_value("TOG_COLOR_DATE"));
2152 free_colors(&s->colors);
2157 view->show = show_log_view;
2158 view->input = input_log_view;
2159 view->close = close_log_view;
2160 view->search_start = search_start_log_view;
2161 view->search_next = search_next_log_view;
2163 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2166 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
2171 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2173 err = got_error_set_errno(errcode, "pthread_cond_init");
2177 s->thread_args.commits_needed = view->nlines;
2178 s->thread_args.graph = thread_graph;
2179 s->thread_args.commits = &s->commits;
2180 s->thread_args.in_repo_path = s->in_repo_path;
2181 s->thread_args.start_id = s->start_id;
2182 s->thread_args.repo = thread_repo;
2183 s->thread_args.log_complete = 0;
2184 s->thread_args.quit = &s->quit;
2185 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2186 s->thread_args.selected_entry = &s->selected_entry;
2187 s->thread_args.searching = &view->searching;
2188 s->thread_args.search_next_done = &view->search_next_done;
2189 s->thread_args.regex = &view->regex;
2192 close_log_view(view);
2196 static const struct got_error *
2197 show_log_view(struct tog_view *view)
2199 struct tog_log_view_state *s = &view->state.log;
2201 if (s->thread == NULL) {
2202 int errcode = pthread_create(&s->thread, NULL, log_thread,
2205 return got_error_set_errno(errcode, "pthread_create");
2208 return draw_commits(view, &s->last_displayed_entry,
2209 &s->selected_entry, s->first_displayed_entry,
2210 &s->commits, s->selected, view->nlines, s->refs,
2211 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2214 static const struct got_error *
2215 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2216 struct tog_view **focus_view, struct tog_view *view, int ch)
2218 const struct got_error *err = NULL;
2219 struct tog_log_view_state *s = &view->state.log;
2220 char *parent_path, *in_repo_path = NULL;
2221 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2223 struct got_object_id *start_id;
2233 if (s->first_displayed_entry == NULL)
2235 if (s->selected > 0)
2238 scroll_up(view, &s->first_displayed_entry, 1,
2243 if (s->first_displayed_entry == NULL)
2245 if (TAILQ_FIRST(&s->commits.head) ==
2246 s->first_displayed_entry) {
2250 scroll_up(view, &s->first_displayed_entry,
2251 view->nlines, &s->commits);
2257 if (s->first_displayed_entry == NULL)
2259 if (s->selected < MIN(view->nlines - 2,
2260 s->commits.ncommits - 1)) {
2264 err = scroll_down(view, &s->first_displayed_entry, 1,
2265 &s->last_displayed_entry, &s->commits,
2266 &s->thread_args.log_complete,
2267 &s->thread_args.commits_needed,
2268 &s->thread_args.need_commits);
2272 struct commit_queue_entry *first;
2273 first = s->first_displayed_entry;
2276 err = scroll_down(view, &s->first_displayed_entry,
2277 view->nlines, &s->last_displayed_entry,
2278 &s->commits, &s->thread_args.log_complete,
2279 &s->thread_args.commits_needed,
2280 &s->thread_args.need_commits);
2283 if (first == s->first_displayed_entry &&
2284 s->selected < MIN(view->nlines - 2,
2285 s->commits.ncommits - 1)) {
2286 /* can't scroll further down */
2287 s->selected = MIN(view->nlines - 2,
2288 s->commits.ncommits - 1);
2294 if (s->selected > view->nlines - 2)
2295 s->selected = view->nlines - 2;
2296 if (s->selected > s->commits.ncommits - 1)
2297 s->selected = s->commits.ncommits - 1;
2302 if (s->selected_entry == NULL)
2304 if (view_is_parent_view(view))
2305 begin_x = view_split_begin_x(view->begin_x);
2306 err = open_diff_view_for_commit(&diff_view, begin_x,
2307 s->selected_entry->commit, s->selected_entry->id,
2308 view, s->refs, s->repo);
2311 if (view_is_parent_view(view)) {
2312 err = view_close_child(view);
2315 err = view_set_child(view, diff_view);
2317 view_close(diff_view);
2320 *focus_view = diff_view;
2321 view->child_focussed = 1;
2323 *new_view = diff_view;
2326 if (s->selected_entry == NULL)
2328 if (view_is_parent_view(view))
2329 begin_x = view_split_begin_x(view->begin_x);
2330 err = browse_commit_tree(&tree_view, begin_x,
2331 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2334 if (view_is_parent_view(view)) {
2335 err = view_close_child(view);
2338 err = view_set_child(view, tree_view);
2340 view_close(tree_view);
2343 *focus_view = tree_view;
2344 view->child_focussed = 1;
2346 *new_view = tree_view;
2349 if (strcmp(s->in_repo_path, "/") == 0)
2351 parent_path = dirname(s->in_repo_path);
2352 if (parent_path && strcmp(parent_path, ".") != 0) {
2353 err = stop_log_thread(s);
2356 lv = view_open(view->nlines, view->ncols,
2357 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2359 return got_error_from_errno(
2361 err = open_log_view(lv, s->start_id, s->refs,
2362 s->repo, s->head_ref_name, parent_path, 0);
2365 if (view_is_parent_view(view))
2368 view_set_child(view->parent, lv);
2375 err = stop_log_thread(s);
2378 lv = view_open(view->nlines, view->ncols,
2379 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2381 return got_error_from_errno("view_open");
2382 err = get_head_commit_id(&start_id, s->head_ref_name ?
2383 s->head_ref_name : GOT_REF_HEAD, s->repo);
2388 in_repo_path = strdup(s->in_repo_path);
2389 if (in_repo_path == NULL) {
2392 return got_error_from_errno("strdup");
2394 got_ref_list_free(s->refs);
2395 err = got_ref_list(s->refs, s->repo, NULL,
2396 got_ref_cmp_by_name, NULL);
2402 err = open_log_view(lv, start_id, s->refs, s->repo,
2403 s->head_ref_name, in_repo_path, 0);
2419 static const struct got_error *
2420 apply_unveil(const char *repo_path, const char *worktree_path)
2422 const struct got_error *error;
2425 if (unveil("gmon.out", "rwc") != 0)
2426 return got_error_from_errno2("unveil", "gmon.out");
2428 if (repo_path && unveil(repo_path, "r") != 0)
2429 return got_error_from_errno2("unveil", repo_path);
2431 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2432 return got_error_from_errno2("unveil", worktree_path);
2434 if (unveil("/tmp", "rwc") != 0)
2435 return got_error_from_errno2("unveil", "/tmp");
2437 error = got_privsep_unveil_exec_helpers();
2441 if (unveil(NULL, NULL) != 0)
2442 return got_error_from_errno("unveil");
2452 halfdelay(1); /* Do fast refresh while initial view is loading. */
2455 intrflush(stdscr, FALSE);
2456 keypad(stdscr, TRUE);
2458 if (getenv("TOG_COLORS") != NULL) {
2460 use_default_colors();
2462 signal(SIGWINCH, tog_sigwinch);
2463 signal(SIGPIPE, tog_sigpipe);
2466 static const struct got_error *
2467 cmd_log(int argc, char *argv[])
2469 const struct got_error *error;
2470 struct got_repository *repo = NULL;
2471 struct got_worktree *worktree = NULL;
2472 struct got_reflist_head refs;
2473 struct got_object_id *start_id = NULL;
2474 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2475 char *start_commit = NULL, *head_ref_name = NULL;
2477 struct tog_view *view;
2479 SIMPLEQ_INIT(&refs);
2482 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2490 start_commit = optarg;
2493 repo_path = realpath(optarg, NULL);
2494 if (repo_path == NULL)
2495 return got_error_from_errno2("realpath",
2507 cwd = getcwd(NULL, 0);
2509 error = got_error_from_errno("getcwd");
2512 error = got_worktree_open(&worktree, cwd);
2513 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2520 error = got_error_from_errno("strdup");
2523 } else if (argc == 1) {
2525 error = got_worktree_resolve_path(&path, worktree,
2530 path = strdup(argv[0]);
2532 error = got_error_from_errno("strdup");
2539 if (repo_path == NULL) {
2542 got_worktree_get_repo_path(worktree));
2544 repo_path = strdup(cwd);
2546 if (repo_path == NULL) {
2547 error = got_error_from_errno("strdup");
2553 error = got_repo_open(&repo, repo_path, NULL);
2557 error = apply_unveil(got_repo_get_path(repo),
2558 worktree ? got_worktree_get_root_path(worktree) : NULL);
2562 if (start_commit == NULL)
2563 error = get_head_commit_id(&start_id, worktree ?
2564 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2567 error = get_head_commit_id(&start_id, start_commit, repo);
2569 if (error->code != GOT_ERR_NOT_REF)
2571 error = got_repo_match_object_id_prefix(&start_id,
2572 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2578 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2582 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2584 error = got_error_from_errno("view_open");
2588 head_ref_name = strdup(
2589 got_worktree_get_head_ref_name(worktree));
2590 if (head_ref_name == NULL) {
2591 error = got_error_from_errno("strdup");
2595 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2600 /* Release work tree lock. */
2601 got_worktree_close(worktree);
2604 error = view_loop(view);
2610 free(head_ref_name);
2612 got_repo_close(repo);
2614 got_worktree_close(worktree);
2615 got_ref_list_free(&refs);
2623 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2629 parse_next_line(FILE *f, size_t *len)
2634 const char delim[3] = { '\0', '\0', '\0'};
2636 line = fparseln(f, &linelen, &lineno, delim, 0);
2643 match_line(const char *line, regex_t *regex)
2645 regmatch_t regmatch;
2647 return regexec(regex, line, 1, ®match, 0) == 0;
2651 match_color(struct tog_colors *colors, const char *line)
2653 struct tog_color *tc = NULL;
2655 SIMPLEQ_FOREACH(tc, colors, entry) {
2656 if (match_line(line, &tc->regex))
2663 static const struct got_error *
2664 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2665 int *last_displayed_line, int *eof, int max_lines, char *header,
2666 struct tog_colors *colors)
2668 const struct got_error *err;
2669 int nlines = 0, nprinted = 0;
2671 struct tog_color *tc;
2677 werase(view->window);
2680 err = format_line(&wline, &width, header, view->ncols, 0);
2685 if (view_needs_focus_indication(view))
2686 wstandout(view->window);
2687 waddwstr(view->window, wline);
2688 if (view_needs_focus_indication(view))
2689 wstandend(view->window);
2690 if (width <= view->ncols - 1)
2691 waddch(view->window, '\n');
2699 while (nprinted < max_lines) {
2700 line = parse_next_line(f, &len);
2705 if (++nlines < *first_displayed_line) {
2710 err = format_line(&wline, &width, line, view->ncols, 0);
2716 tc = match_color(colors, line);
2718 wattr_on(view->window,
2719 COLOR_PAIR(tc->colorpair), NULL);
2720 waddwstr(view->window, wline);
2722 wattr_off(view->window,
2723 COLOR_PAIR(tc->colorpair), NULL);
2724 if (width <= view->ncols - 1)
2725 waddch(view->window, '\n');
2726 if (++nprinted == 1)
2727 *first_displayed_line = nlines;
2732 *last_displayed_line = nlines;
2737 while (nprinted < view->nlines) {
2738 waddch(view->window, '\n');
2742 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2747 wstandout(view->window);
2748 waddwstr(view->window, wline);
2749 wstandend(view->window);
2756 get_datestr(time_t *time, char *datebuf)
2758 struct tm mytm, *tm;
2761 tm = gmtime_r(time, &mytm);
2764 s = asctime_r(tm, datebuf);
2767 p = strchr(s, '\n');
2773 static const struct got_error *
2774 write_commit_info(struct got_object_id *commit_id,
2775 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2777 const struct got_error *err = NULL;
2778 char datebuf[26], *datestr;
2779 struct got_commit_object *commit;
2780 char *id_str = NULL, *logmsg = NULL;
2781 time_t committer_time;
2782 const char *author, *committer;
2783 char *refs_str = NULL;
2786 err = build_refs_str(&refs_str, refs, commit_id, repo);
2791 err = got_object_open_as_commit(&commit, repo, commit_id);
2795 err = got_object_id_str(&id_str, commit_id);
2797 err = got_error_from_errno("got_object_id_str");
2801 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2802 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2803 err = got_error_from_errno("fprintf");
2806 if (fprintf(outfile, "from: %s\n",
2807 got_object_commit_get_author(commit)) < 0) {
2808 err = got_error_from_errno("fprintf");
2811 committer_time = got_object_commit_get_committer_time(commit);
2812 datestr = get_datestr(&committer_time, datebuf);
2813 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2814 err = got_error_from_errno("fprintf");
2817 author = got_object_commit_get_author(commit);
2818 committer = got_object_commit_get_committer(commit);
2819 if (strcmp(author, committer) != 0 &&
2820 fprintf(outfile, "via: %s\n", committer) < 0) {
2821 err = got_error_from_errno("fprintf");
2824 err = got_object_commit_get_logmsg(&logmsg, commit);
2827 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2828 err = got_error_from_errno("fprintf");
2835 got_object_commit_close(commit);
2839 static const struct got_error *
2840 create_diff(struct tog_diff_view_state *s)
2842 const struct got_error *err = NULL;
2848 err = got_error_from_errno("got_opentemp");
2851 if (s->f && fclose(s->f) != 0) {
2852 err = got_error_from_errno("fclose");
2858 err = got_object_get_type(&obj_type, s->repo, s->id1);
2860 err = got_object_get_type(&obj_type, s->repo, s->id2);
2865 case GOT_OBJ_TYPE_BLOB:
2866 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2867 s->diff_context, 0, s->repo, f);
2869 case GOT_OBJ_TYPE_TREE:
2870 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2871 s->diff_context, 0, s->repo, f);
2873 case GOT_OBJ_TYPE_COMMIT: {
2874 const struct got_object_id_queue *parent_ids;
2875 struct got_object_qid *pid;
2876 struct got_commit_object *commit2;
2878 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2881 /* Show commit info if we're diffing to a parent/root commit. */
2883 write_commit_info(s->id2, s->refs, s->repo, f);
2885 parent_ids = got_object_commit_get_parent_ids(commit2);
2886 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2887 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2888 write_commit_info(s->id2, s->refs,
2894 got_object_commit_close(commit2);
2896 err = got_diff_objects_as_commits(s->id1, s->id2,
2897 s->diff_context, 0, s->repo, f);
2901 err = got_error(GOT_ERR_OBJ_TYPE);
2905 if (f && fflush(f) != 0 && err == NULL)
2906 err = got_error_from_errno("fflush");
2911 diff_view_indicate_progress(struct tog_view *view)
2913 mvwaddstr(view->window, 0, 0, "diffing...");
2918 static const struct got_error *
2919 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2920 struct got_object_id *id2, struct tog_view *log_view,
2921 struct got_reflist_head *refs, struct got_repository *repo)
2923 const struct got_error *err;
2925 if (id1 != NULL && id2 != NULL) {
2927 err = got_object_get_type(&type1, repo, id1);
2930 err = got_object_get_type(&type2, repo, id2);
2935 return got_error(GOT_ERR_OBJ_TYPE);
2939 view->state.diff.id1 = got_object_id_dup(id1);
2940 if (view->state.diff.id1 == NULL)
2941 return got_error_from_errno("got_object_id_dup");
2943 view->state.diff.id1 = NULL;
2945 view->state.diff.id2 = got_object_id_dup(id2);
2946 if (view->state.diff.id2 == NULL) {
2947 free(view->state.diff.id1);
2948 view->state.diff.id1 = NULL;
2949 return got_error_from_errno("got_object_id_dup");
2951 view->state.diff.f = NULL;
2952 view->state.diff.first_displayed_line = 1;
2953 view->state.diff.last_displayed_line = view->nlines;
2954 view->state.diff.diff_context = 3;
2955 view->state.diff.log_view = log_view;
2956 view->state.diff.repo = repo;
2957 view->state.diff.refs = refs;
2958 SIMPLEQ_INIT(&view->state.diff.colors);
2960 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2961 err = add_color(&view->state.diff.colors,
2962 "^-", TOG_COLOR_DIFF_MINUS,
2963 get_color_value("TOG_COLOR_DIFF_MINUS"));
2966 err = add_color(&view->state.diff.colors, "^\\+",
2967 TOG_COLOR_DIFF_PLUS,
2968 get_color_value("TOG_COLOR_DIFF_PLUS"));
2970 free_colors(&view->state.diff.colors);
2973 err = add_color(&view->state.diff.colors,
2974 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2975 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2977 free_colors(&view->state.diff.colors);
2981 err = add_color(&view->state.diff.colors,
2982 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2983 get_color_value("TOG_COLOR_DIFF_META"));
2985 free_colors(&view->state.diff.colors);
2989 err = add_color(&view->state.diff.colors,
2990 "^(from|via): ", TOG_COLOR_AUTHOR,
2991 get_color_value("TOG_COLOR_AUTHOR"));
2993 free_colors(&view->state.diff.colors);
2997 err = add_color(&view->state.diff.colors,
2998 "^date: ", TOG_COLOR_DATE,
2999 get_color_value("TOG_COLOR_DATE"));
3001 free_colors(&view->state.diff.colors);
3006 if (log_view && view_is_splitscreen(view))
3007 show_log_view(log_view); /* draw vborder */
3008 diff_view_indicate_progress(view);
3010 err = create_diff(&view->state.diff);
3012 free(view->state.diff.id1);
3013 view->state.diff.id1 = NULL;
3014 free(view->state.diff.id2);
3015 view->state.diff.id2 = NULL;
3019 view->show = show_diff_view;
3020 view->input = input_diff_view;
3021 view->close = close_diff_view;
3026 static const struct got_error *
3027 close_diff_view(struct tog_view *view)
3029 const struct got_error *err = NULL;
3031 free(view->state.diff.id1);
3032 view->state.diff.id1 = NULL;
3033 free(view->state.diff.id2);
3034 view->state.diff.id2 = NULL;
3035 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3036 err = got_error_from_errno("fclose");
3037 free_colors(&view->state.diff.colors);
3041 static const struct got_error *
3042 show_diff_view(struct tog_view *view)
3044 const struct got_error *err;
3045 struct tog_diff_view_state *s = &view->state.diff;
3046 char *id_str1 = NULL, *id_str2, *header;
3049 err = got_object_id_str(&id_str1, s->id1);
3053 err = got_object_id_str(&id_str2, s->id2);
3057 if (asprintf(&header, "diff %s %s",
3058 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3059 err = got_error_from_errno("asprintf");
3067 return draw_file(view, s->f, &s->first_displayed_line,
3068 &s->last_displayed_line, &s->eof, view->nlines,
3069 header, &s->colors);
3072 static const struct got_error *
3073 set_selected_commit(struct tog_diff_view_state *s,
3074 struct commit_queue_entry *entry)
3076 const struct got_error *err;
3077 const struct got_object_id_queue *parent_ids;
3078 struct got_commit_object *selected_commit;
3079 struct got_object_qid *pid;
3082 s->id2 = got_object_id_dup(entry->id);
3084 return got_error_from_errno("got_object_id_dup");
3086 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3089 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3091 pid = SIMPLEQ_FIRST(parent_ids);
3092 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3093 got_object_commit_close(selected_commit);
3097 static const struct got_error *
3098 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3099 struct tog_view **focus_view, struct tog_view *view, int ch)
3101 const struct got_error *err = NULL;
3102 struct tog_diff_view_state *s = &view->state.diff;
3103 struct tog_log_view_state *ls;
3104 struct commit_queue_entry *entry;
3110 if (s->first_displayed_line > 1)
3111 s->first_displayed_line--;
3115 if (s->first_displayed_line == 1)
3118 while (i++ < view->nlines - 1 &&
3119 s->first_displayed_line > 1)
3120 s->first_displayed_line--;
3125 s->first_displayed_line++;
3133 while (!s->eof && i++ < view->nlines - 1) {
3135 line = parse_next_line(s->f, NULL);
3136 s->first_displayed_line++;
3142 if (s->diff_context > 0) {
3144 diff_view_indicate_progress(view);
3145 err = create_diff(s);
3149 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3151 diff_view_indicate_progress(view);
3152 err = create_diff(s);
3157 if (s->log_view == NULL)
3159 ls = &s->log_view->state.log;
3160 entry = TAILQ_PREV(ls->selected_entry,
3161 commit_queue_head, entry);
3165 err = input_log_view(NULL, NULL, NULL, s->log_view,
3170 err = set_selected_commit(s, entry);
3174 s->first_displayed_line = 1;
3175 s->last_displayed_line = view->nlines;
3177 diff_view_indicate_progress(view);
3178 err = create_diff(s);
3182 if (s->log_view == NULL)
3184 ls = &s->log_view->state.log;
3186 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3187 ls->thread_args.commits_needed++;
3189 /* Display "loading..." in log view. */
3190 show_log_view(s->log_view);
3194 err = trigger_log_thread(1 /* load_all */,
3195 &ls->thread_args.commits_needed,
3196 &ls->thread_args.log_complete,
3197 &ls->thread_args.need_commits);
3201 err = input_log_view(NULL, NULL, NULL, s->log_view,
3206 entry = TAILQ_NEXT(ls->selected_entry, entry);
3210 err = set_selected_commit(s, entry);
3214 s->first_displayed_line = 1;
3215 s->last_displayed_line = view->nlines;
3217 diff_view_indicate_progress(view);
3218 err = create_diff(s);
3227 static const struct got_error *
3228 cmd_diff(int argc, char *argv[])
3230 const struct got_error *error = NULL;
3231 struct got_repository *repo = NULL;
3232 struct got_reflist_head refs;
3233 struct got_object_id *id1 = NULL, *id2 = NULL;
3234 char *repo_path = NULL;
3235 char *id_str1 = NULL, *id_str2 = NULL;
3237 struct tog_view *view;
3239 SIMPLEQ_INIT(&refs);
3242 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3247 while ((ch = getopt(argc, argv, "")) != -1) {
3259 usage_diff(); /* TODO show local worktree changes */
3260 } else if (argc == 2) {
3261 repo_path = getcwd(NULL, 0);
3262 if (repo_path == NULL)
3263 return got_error_from_errno("getcwd");
3266 } else if (argc == 3) {
3267 repo_path = realpath(argv[0], NULL);
3268 if (repo_path == NULL)
3269 return got_error_from_errno2("realpath", argv[0]);
3277 error = got_repo_open(&repo, repo_path, NULL);
3281 error = apply_unveil(got_repo_get_path(repo), NULL);
3285 error = got_repo_match_object_id_prefix(&id1, id_str1,
3286 GOT_OBJ_TYPE_ANY, repo);
3290 error = got_repo_match_object_id_prefix(&id2, id_str2,
3291 GOT_OBJ_TYPE_ANY, repo);
3295 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3299 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3301 error = got_error_from_errno("view_open");
3304 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3307 error = view_loop(view);
3311 got_repo_close(repo);
3312 got_ref_list_free(&refs);
3320 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3325 struct tog_blame_line {
3327 struct got_object_id *id;
3330 static const struct got_error *
3331 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3332 const char *path, struct tog_blame_line *lines, int nlines,
3333 int blame_complete, int selected_line, int *first_displayed_line,
3334 int *last_displayed_line, int *eof, int max_lines,
3335 struct tog_colors *colors)
3337 const struct got_error *err;
3338 int lineno = 0, nprinted = 0;
3343 struct tog_blame_line *blame_line;
3344 struct got_object_id *prev_id = NULL;
3346 struct tog_color *tc;
3348 err = got_object_id_str(&id_str, id);
3353 werase(view->window);
3355 if (asprintf(&line, "commit %s", id_str) == -1) {
3356 err = got_error_from_errno("asprintf");
3361 err = format_line(&wline, &width, line, view->ncols, 0);
3366 if (view_needs_focus_indication(view))
3367 wstandout(view->window);
3368 tc = get_color(colors, TOG_COLOR_COMMIT);
3370 wattr_on(view->window,
3371 COLOR_PAIR(tc->colorpair), NULL);
3372 waddwstr(view->window, wline);
3374 wattr_off(view->window,
3375 COLOR_PAIR(tc->colorpair), NULL);
3376 if (view_needs_focus_indication(view))
3377 wstandend(view->window);
3380 if (width < view->ncols - 1)
3381 waddch(view->window, '\n');
3383 if (asprintf(&line, "[%d/%d] %s%s",
3384 *first_displayed_line - 1 + selected_line, nlines,
3385 blame_complete ? "" : "annotating... ", path) == -1) {
3387 return got_error_from_errno("asprintf");
3390 err = format_line(&wline, &width, line, view->ncols, 0);
3395 waddwstr(view->window, wline);
3398 if (width < view->ncols - 1)
3399 waddch(view->window, '\n');
3402 while (nprinted < max_lines - 2) {
3403 line = parse_next_line(f, &len);
3408 if (++lineno < *first_displayed_line) {
3413 if (view->ncols <= 9) {
3415 wline = wcsdup(L"");
3417 err = got_error_from_errno("wcsdup");
3419 err = format_line(&wline, &width, line,
3420 view->ncols - 9, 9);
3428 if (view->focussed && nprinted == selected_line - 1)
3429 wstandout(view->window);
3432 blame_line = &lines[lineno - 1];
3433 if (blame_line->annotated && prev_id &&
3434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3436 nprinted == selected_line - 1)) {
3437 waddstr(view->window, " ");
3438 } else if (blame_line->annotated) {
3440 err = got_object_id_str(&id_str, blame_line->id);
3446 tc = get_color(colors, TOG_COLOR_COMMIT);
3448 wattr_on(view->window,
3449 COLOR_PAIR(tc->colorpair), NULL);
3450 wprintw(view->window, "%.8s", id_str);
3452 wattr_off(view->window,
3453 COLOR_PAIR(tc->colorpair), NULL);
3455 prev_id = blame_line->id;
3457 waddstr(view->window, "........");
3461 waddstr(view->window, "........");
3465 if (view->focussed && nprinted == selected_line - 1)
3466 wstandend(view->window);
3467 waddstr(view->window, " ");
3469 waddwstr(view->window, wline);
3470 if (width <= view->ncols - 1)
3471 waddch(view->window, '\n');
3472 if (++nprinted == 1)
3473 *first_displayed_line = lineno;
3478 *last_displayed_line = lineno;
3485 static const struct got_error *
3486 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3488 const struct got_error *err = NULL;
3489 struct tog_blame_cb_args *a = arg;
3490 struct tog_blame_line *line;
3493 if (nlines != a->nlines ||
3494 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3495 return got_error(GOT_ERR_RANGE);
3497 errcode = pthread_mutex_lock(&tog_mutex);
3499 return got_error_set_errno(errcode, "pthread_mutex_lock");
3501 if (*a->quit) { /* user has quit the blame view */
3502 err = got_error(GOT_ERR_ITER_COMPLETED);
3507 goto done; /* no change in this commit */
3509 line = &a->lines[lineno - 1];
3510 if (line->annotated)
3513 line->id = got_object_id_dup(id);
3514 if (line->id == NULL) {
3515 err = got_error_from_errno("got_object_id_dup");
3518 line->annotated = 1;
3520 errcode = pthread_mutex_unlock(&tog_mutex);
3522 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3527 blame_thread(void *arg)
3529 const struct got_error *err;
3530 struct tog_blame_thread_args *ta = arg;
3531 struct tog_blame_cb_args *a = ta->cb_args;
3534 err = got_blame(ta->path, a->commit_id, ta->repo,
3535 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3536 if (err && err->code == GOT_ERR_CANCELLED)
3539 errcode = pthread_mutex_lock(&tog_mutex);
3541 return (void *)got_error_set_errno(errcode,
3542 "pthread_mutex_lock");
3544 got_repo_close(ta->repo);
3548 errcode = pthread_mutex_unlock(&tog_mutex);
3549 if (errcode && err == NULL)
3550 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3555 static struct got_object_id *
3556 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3557 int first_displayed_line, int selected_line)
3559 struct tog_blame_line *line;
3564 line = &lines[first_displayed_line - 1 + selected_line - 1];
3565 if (!line->annotated)
3571 static const struct got_error *
3572 stop_blame(struct tog_blame *blame)
3574 const struct got_error *err = NULL;
3577 if (blame->thread) {
3579 errcode = pthread_mutex_unlock(&tog_mutex);
3581 return got_error_set_errno(errcode,
3582 "pthread_mutex_unlock");
3583 errcode = pthread_join(blame->thread, (void **)&err);
3585 return got_error_set_errno(errcode, "pthread_join");
3586 errcode = pthread_mutex_lock(&tog_mutex);
3588 return got_error_set_errno(errcode,
3589 "pthread_mutex_lock");
3590 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3592 blame->thread = NULL;
3594 if (blame->thread_args.repo) {
3595 got_repo_close(blame->thread_args.repo);
3596 blame->thread_args.repo = NULL;
3599 if (fclose(blame->f) != 0 && err == NULL)
3600 err = got_error_from_errno("fclose");
3604 for (i = 0; i < blame->nlines; i++)
3605 free(blame->lines[i].id);
3607 blame->lines = NULL;
3609 free(blame->cb_args.commit_id);
3610 blame->cb_args.commit_id = NULL;
3615 static const struct got_error *
3616 cancel_blame_view(void *arg)
3618 const struct got_error *err = NULL;
3622 errcode = pthread_mutex_lock(&tog_mutex);
3624 return got_error_set_errno(errcode,
3625 "pthread_mutex_unlock");
3628 err = got_error(GOT_ERR_CANCELLED);
3630 errcode = pthread_mutex_unlock(&tog_mutex);
3632 return got_error_set_errno(errcode,
3633 "pthread_mutex_lock");
3638 static const struct got_error *
3639 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3640 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3641 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3642 struct got_repository *repo)
3644 const struct got_error *err = NULL;
3645 struct got_blob_object *blob = NULL;
3646 struct got_repository *thread_repo = NULL;
3647 struct got_object_id *obj_id = NULL;
3650 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3654 return got_error(GOT_ERR_NO_OBJ);
3656 err = got_object_get_type(&obj_type, repo, obj_id);
3660 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3661 err = got_error(GOT_ERR_OBJ_TYPE);
3665 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3668 blame->f = got_opentemp();
3669 if (blame->f == NULL) {
3670 err = got_error_from_errno("got_opentemp");
3673 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3674 &blame->line_offsets, blame->f, blob);
3675 if (err || blame->nlines == 0)
3678 /* Don't include \n at EOF in the blame line count. */
3679 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3682 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3683 if (blame->lines == NULL) {
3684 err = got_error_from_errno("calloc");
3688 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3692 blame->cb_args.view = view;
3693 blame->cb_args.lines = blame->lines;
3694 blame->cb_args.nlines = blame->nlines;
3695 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3696 if (blame->cb_args.commit_id == NULL) {
3697 err = got_error_from_errno("got_object_id_dup");
3700 blame->cb_args.quit = done;
3702 blame->thread_args.path = path;
3703 blame->thread_args.repo = thread_repo;
3704 blame->thread_args.cb_args = &blame->cb_args;
3705 blame->thread_args.complete = blame_complete;
3706 blame->thread_args.cancel_cb = cancel_blame_view;
3707 blame->thread_args.cancel_arg = done;
3708 *blame_complete = 0;
3712 got_object_blob_close(blob);
3719 static const struct got_error *
3720 open_blame_view(struct tog_view *view, char *path,
3721 struct got_object_id *commit_id, struct got_reflist_head *refs,
3722 struct got_repository *repo)
3724 const struct got_error *err = NULL;
3725 struct tog_blame_view_state *s = &view->state.blame;
3727 SIMPLEQ_INIT(&s->blamed_commits);
3729 s->path = strdup(path);
3730 if (s->path == NULL)
3731 return got_error_from_errno("strdup");
3733 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3739 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3740 s->first_displayed_line = 1;
3741 s->last_displayed_line = view->nlines;
3742 s->selected_line = 1;
3743 s->blame_complete = 0;
3746 s->commit_id = commit_id;
3747 memset(&s->blame, 0, sizeof(s->blame));
3749 SIMPLEQ_INIT(&s->colors);
3750 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3751 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3752 get_color_value("TOG_COLOR_COMMIT"));
3757 view->show = show_blame_view;
3758 view->input = input_blame_view;
3759 view->close = close_blame_view;
3760 view->search_start = search_start_blame_view;
3761 view->search_next = search_next_blame_view;
3763 return run_blame(&s->blame, view, &s->blame_complete,
3764 &s->first_displayed_line, &s->last_displayed_line,
3765 &s->selected_line, &s->done, &s->eof, s->path,
3766 s->blamed_commit->id, s->repo);
3769 static const struct got_error *
3770 close_blame_view(struct tog_view *view)
3772 const struct got_error *err = NULL;
3773 struct tog_blame_view_state *s = &view->state.blame;
3775 if (s->blame.thread)
3776 err = stop_blame(&s->blame);
3778 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3779 struct got_object_qid *blamed_commit;
3780 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3781 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3782 got_object_qid_free(blamed_commit);
3786 free_colors(&s->colors);
3791 static const struct got_error *
3792 search_start_blame_view(struct tog_view *view)
3794 struct tog_blame_view_state *s = &view->state.blame;
3796 s->matched_line = 0;
3800 static const struct got_error *
3801 search_next_blame_view(struct tog_view *view)
3803 struct tog_blame_view_state *s = &view->state.blame;
3806 if (!view->searching) {
3807 view->search_next_done = 1;
3811 if (s->matched_line) {
3812 if (view->searching == TOG_SEARCH_FORWARD)
3813 lineno = s->matched_line + 1;
3815 lineno = s->matched_line - 1;
3817 if (view->searching == TOG_SEARCH_FORWARD)
3820 lineno = s->blame.nlines;
3828 if (lineno <= 0 || lineno > s->blame.nlines) {
3829 if (s->matched_line == 0) {
3830 view->search_next_done = 1;
3835 if (view->searching == TOG_SEARCH_FORWARD)
3838 lineno = s->blame.nlines;
3841 offset = s->blame.line_offsets[lineno - 1];
3842 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3844 return got_error_from_errno("fseeko");
3847 line = parse_next_line(s->blame.f, &len);
3848 if (line && match_line(line, &view->regex)) {
3849 view->search_next_done = 1;
3850 s->matched_line = lineno;
3855 if (view->searching == TOG_SEARCH_FORWARD)
3861 if (s->matched_line) {
3862 s->first_displayed_line = s->matched_line;
3863 s->selected_line = 1;
3869 static const struct got_error *
3870 show_blame_view(struct tog_view *view)
3872 const struct got_error *err = NULL;
3873 struct tog_blame_view_state *s = &view->state.blame;
3876 if (s->blame.thread == NULL) {
3877 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3878 &s->blame.thread_args);
3880 return got_error_set_errno(errcode, "pthread_create");
3882 halfdelay(1); /* fast refresh while annotating */
3885 if (s->blame_complete)
3886 halfdelay(10); /* disable fast refresh */
3888 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3889 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3890 s->selected_line, &s->first_displayed_line,
3891 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3897 static const struct got_error *
3898 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3899 struct tog_view **focus_view, struct tog_view *view, int ch)
3901 const struct got_error *err = NULL, *thread_err = NULL;
3902 struct tog_view *diff_view;
3903 struct tog_blame_view_state *s = &view->state.blame;
3912 if (s->selected_line > 1)
3914 else if (s->selected_line == 1 &&
3915 s->first_displayed_line > 1)
3916 s->first_displayed_line--;
3919 if (s->first_displayed_line == 1) {
3920 s->selected_line = 1;
3923 if (s->first_displayed_line > view->nlines - 2)
3924 s->first_displayed_line -=
3927 s->first_displayed_line = 1;
3931 if (s->selected_line < view->nlines - 2 &&
3932 s->first_displayed_line +
3933 s->selected_line <= s->blame.nlines)
3935 else if (s->last_displayed_line <
3937 s->first_displayed_line++;
3941 struct got_object_id *id = NULL;
3942 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3943 s->first_displayed_line, s->selected_line);
3947 struct got_commit_object *commit;
3948 struct got_object_qid *pid;
3949 struct got_object_id *blob_id = NULL;
3951 err = got_object_open_as_commit(&commit,
3955 pid = SIMPLEQ_FIRST(
3956 got_object_commit_get_parent_ids(commit));
3958 got_object_commit_close(commit);
3961 /* Check if path history ends here. */
3962 err = got_object_id_by_path(&blob_id, s->repo,
3965 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3967 got_object_commit_close(commit);
3970 err = got_object_get_type(&obj_type, s->repo,
3973 /* Can't blame non-blob type objects. */
3974 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3975 got_object_commit_close(commit);
3978 err = got_object_qid_alloc(&s->blamed_commit,
3980 got_object_commit_close(commit);
3982 if (got_object_id_cmp(id,
3983 s->blamed_commit->id) == 0)
3985 err = got_object_qid_alloc(&s->blamed_commit,
3991 thread_err = stop_blame(&s->blame);
3995 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3996 s->blamed_commit, entry);
3997 err = run_blame(&s->blame, view, &s->blame_complete,
3998 &s->first_displayed_line, &s->last_displayed_line,
3999 &s->selected_line, &s->done, &s->eof,
4000 s->path, s->blamed_commit->id, s->repo);
4006 struct got_object_qid *first;
4007 first = SIMPLEQ_FIRST(&s->blamed_commits);
4008 if (!got_object_id_cmp(first->id, s->commit_id))
4011 thread_err = stop_blame(&s->blame);
4015 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4016 got_object_qid_free(s->blamed_commit);
4018 SIMPLEQ_FIRST(&s->blamed_commits);
4019 err = run_blame(&s->blame, view, &s->blame_complete,
4020 &s->first_displayed_line, &s->last_displayed_line,
4021 &s->selected_line, &s->done, &s->eof, s->path,
4022 s->blamed_commit->id, s->repo);
4029 struct got_object_id *id = NULL;
4030 struct got_object_qid *pid;
4031 struct got_commit_object *commit = NULL;
4032 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4033 s->first_displayed_line, s->selected_line);
4036 err = got_object_open_as_commit(&commit, s->repo, id);
4039 pid = SIMPLEQ_FIRST(
4040 got_object_commit_get_parent_ids(commit));
4041 if (view_is_parent_view(view))
4042 begin_x = view_split_begin_x(view->begin_x);
4043 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4044 if (diff_view == NULL) {
4045 got_object_commit_close(commit);
4046 err = got_error_from_errno("view_open");
4049 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4050 id, NULL, s->refs, s->repo);
4051 got_object_commit_close(commit);
4053 view_close(diff_view);
4056 if (view_is_parent_view(view)) {
4057 err = view_close_child(view);
4060 err = view_set_child(view, diff_view);
4062 view_close(diff_view);
4065 *focus_view = diff_view;
4066 view->child_focussed = 1;
4068 *new_view = diff_view;
4075 if (s->last_displayed_line >= s->blame.nlines &&
4076 s->selected_line >= MIN(s->blame.nlines,
4077 view->nlines - 2)) {
4080 if (s->last_displayed_line >= s->blame.nlines &&
4081 s->selected_line < view->nlines - 2) {
4082 s->selected_line = MIN(s->blame.nlines,
4086 if (s->last_displayed_line + view->nlines - 2
4088 s->first_displayed_line +=
4091 s->first_displayed_line =
4096 if (s->selected_line > view->nlines - 2) {
4097 s->selected_line = MIN(s->blame.nlines,
4104 return thread_err ? thread_err : err;
4107 static const struct got_error *
4108 cmd_blame(int argc, char *argv[])
4110 const struct got_error *error;
4111 struct got_repository *repo = NULL;
4112 struct got_reflist_head refs;
4113 struct got_worktree *worktree = NULL;
4114 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4115 struct got_object_id *commit_id = NULL;
4116 char *commit_id_str = NULL;
4118 struct tog_view *view;
4120 SIMPLEQ_INIT(&refs);
4123 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4128 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4131 commit_id_str = optarg;
4134 repo_path = realpath(optarg, NULL);
4135 if (repo_path == NULL)
4136 return got_error_from_errno2("realpath",
4153 cwd = getcwd(NULL, 0);
4155 error = got_error_from_errno("getcwd");
4158 if (repo_path == NULL) {
4159 error = got_worktree_open(&worktree, cwd);
4160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4166 strdup(got_worktree_get_repo_path(worktree));
4167 if (repo_path == NULL)
4168 error = got_error_from_errno("strdup");
4172 repo_path = strdup(cwd);
4173 if (repo_path == NULL) {
4174 error = got_error_from_errno("strdup");
4182 error = got_repo_open(&repo, repo_path, NULL);
4186 error = apply_unveil(got_repo_get_path(repo), NULL);
4191 const char *prefix = got_worktree_get_path_prefix(worktree);
4192 char *p, *worktree_subdir = cwd +
4193 strlen(got_worktree_get_root_path(worktree));
4194 if (asprintf(&p, "%s%s%s%s%s",
4195 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4196 worktree_subdir, worktree_subdir[0] ? "/" : "",
4198 error = got_error_from_errno("asprintf");
4201 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4204 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4209 if (commit_id_str == NULL) {
4210 struct got_reference *head_ref;
4211 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4214 error = got_ref_resolve(&commit_id, repo, head_ref);
4215 got_ref_close(head_ref);
4217 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4219 if (error->code != GOT_ERR_NOT_REF)
4221 error = got_repo_match_object_id_prefix(&commit_id,
4222 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4228 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4232 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4234 error = got_error_from_errno("view_open");
4237 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4241 /* Release work tree lock. */
4242 got_worktree_close(worktree);
4245 error = view_loop(view);
4251 got_worktree_close(worktree);
4253 got_repo_close(repo);
4254 got_ref_list_free(&refs);
4258 static const struct got_error *
4259 draw_tree_entries(struct tog_view *view,
4260 struct got_tree_entry **first_displayed_entry,
4261 struct got_tree_entry **last_displayed_entry,
4262 struct got_tree_entry **selected_entry, int *ndisplayed,
4263 const char *label, int show_ids, const char *parent_path,
4264 struct got_tree_object *tree, int selected, int limit,
4265 int isroot, struct tog_colors *colors)
4267 const struct got_error *err = NULL;
4268 struct got_tree_entry *te;
4270 struct tog_color *tc;
4271 int width, n, i, nentries;
4275 werase(view->window);
4280 err = format_line(&wline, &width, label, view->ncols, 0);
4283 if (view_needs_focus_indication(view))
4284 wstandout(view->window);
4285 tc = get_color(colors, TOG_COLOR_COMMIT);
4287 wattr_on(view->window,
4288 COLOR_PAIR(tc->colorpair), NULL);
4289 waddwstr(view->window, wline);
4291 wattr_off(view->window,
4292 COLOR_PAIR(tc->colorpair), NULL);
4293 if (view_needs_focus_indication(view))
4294 wstandend(view->window);
4297 if (width < view->ncols - 1)
4298 waddch(view->window, '\n');
4301 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4304 waddwstr(view->window, wline);
4307 if (width < view->ncols - 1)
4308 waddch(view->window, '\n');
4311 waddch(view->window, '\n');
4315 if (*first_displayed_entry == NULL) {
4316 te = got_object_tree_get_first_entry(tree);
4317 if (selected == 0) {
4319 wstandout(view->window);
4320 *selected_entry = NULL;
4322 waddstr(view->window, " ..\n"); /* parent directory */
4323 if (selected == 0 && view->focussed)
4324 wstandend(view->window);
4331 te = *first_displayed_entry;
4334 nentries = got_object_tree_get_nentries(tree);
4335 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4336 char *line = NULL, *id_str = NULL;
4337 const char *modestr = "";
4340 te = got_object_tree_get_entry(tree, i);
4341 mode = got_tree_entry_get_mode(te);
4344 err = got_object_id_str(&id_str,
4345 got_tree_entry_get_id(te));
4347 return got_error_from_errno(
4348 "got_object_id_str");
4350 if (got_object_tree_entry_is_submodule(te))
4352 else if (S_ISLNK(mode))
4354 else if (S_ISDIR(mode))
4356 else if (mode & S_IXUSR)
4358 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4359 got_tree_entry_get_name(te), modestr) == -1) {
4361 return got_error_from_errno("asprintf");
4364 err = format_line(&wline, &width, line, view->ncols, 0);
4369 if (n == selected) {
4371 wstandout(view->window);
4372 *selected_entry = te;
4374 tc = match_color(colors, line);
4376 wattr_on(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 waddwstr(view->window, wline);
4380 wattr_off(view->window,
4381 COLOR_PAIR(tc->colorpair), NULL);
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4384 if (n == selected && view->focussed)
4385 wstandend(view->window);
4391 *last_displayed_entry = te;
4400 tree_scroll_up(struct tog_view *view,
4401 struct got_tree_entry **first_displayed_entry, int maxscroll,
4402 struct got_tree_object *tree, int isroot)
4404 struct got_tree_entry *te;
4407 if (*first_displayed_entry == NULL)
4410 te = got_object_tree_get_entry(tree, 0);
4411 if (*first_displayed_entry == te) {
4413 *first_displayed_entry = NULL;
4418 while (*first_displayed_entry && i < maxscroll) {
4419 *first_displayed_entry = got_tree_entry_get_prev(tree,
4420 *first_displayed_entry);
4423 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4424 *first_displayed_entry = NULL;
4428 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4429 struct got_tree_entry *last_displayed_entry,
4430 struct got_tree_object *tree)
4432 struct got_tree_entry *next, *last;
4435 if (*first_displayed_entry)
4436 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4438 next = got_object_tree_get_first_entry(tree);
4440 last = last_displayed_entry;
4441 while (next && last && n++ < maxscroll) {
4442 last = got_tree_entry_get_next(tree, last);
4444 *first_displayed_entry = next;
4445 next = got_tree_entry_get_next(tree, next);
4451 static const struct got_error *
4452 tree_entry_path(char **path, struct tog_parent_trees *parents,
4453 struct got_tree_entry *te)
4455 const struct got_error *err = NULL;
4456 struct tog_parent_tree *pt;
4457 size_t len = 2; /* for leading slash and NUL */
4459 TAILQ_FOREACH(pt, parents, entry)
4460 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4463 len += strlen(got_tree_entry_get_name(te));
4465 *path = calloc(1, len);
4467 return got_error_from_errno("calloc");
4470 pt = TAILQ_LAST(parents, tog_parent_trees);
4472 const char *name = got_tree_entry_get_name(pt->selected_entry);
4473 if (strlcat(*path, name, len) >= len) {
4474 err = got_error(GOT_ERR_NO_SPACE);
4477 if (strlcat(*path, "/", len) >= len) {
4478 err = got_error(GOT_ERR_NO_SPACE);
4481 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4484 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4485 err = got_error(GOT_ERR_NO_SPACE);
4497 static const struct got_error *
4498 blame_tree_entry(struct tog_view **new_view, int begin_x,
4499 struct got_tree_entry *te, struct tog_parent_trees *parents,
4500 struct got_object_id *commit_id, struct got_reflist_head *refs,
4501 struct got_repository *repo)
4503 const struct got_error *err = NULL;
4505 struct tog_view *blame_view;
4509 err = tree_entry_path(&path, parents, te);
4513 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4514 if (blame_view == NULL) {
4515 err = got_error_from_errno("view_open");
4519 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4521 if (err->code == GOT_ERR_CANCELLED)
4523 view_close(blame_view);
4525 *new_view = blame_view;
4531 static const struct got_error *
4532 log_tree_entry(struct tog_view **new_view, int begin_x,
4533 struct got_tree_entry *te, struct tog_parent_trees *parents,
4534 struct got_object_id *commit_id, struct got_reflist_head *refs,
4535 struct got_repository *repo)
4537 struct tog_view *log_view;
4538 const struct got_error *err = NULL;
4543 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4544 if (log_view == NULL)
4545 return got_error_from_errno("view_open");
4547 err = tree_entry_path(&path, parents, te);
4551 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4553 view_close(log_view);
4555 *new_view = log_view;
4560 static const struct got_error *
4561 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4562 struct got_object_id *commit_id, struct got_reflist_head *refs,
4563 struct got_repository *repo)
4565 const struct got_error *err = NULL;
4566 char *commit_id_str = NULL;
4567 struct tog_tree_view_state *s = &view->state.tree;
4569 TAILQ_INIT(&s->parents);
4571 err = got_object_id_str(&commit_id_str, commit_id);
4575 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4576 err = got_error_from_errno("asprintf");
4580 s->root = s->tree = root;
4581 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4582 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4583 s->commit_id = got_object_id_dup(commit_id);
4584 if (s->commit_id == NULL) {
4585 err = got_error_from_errno("got_object_id_dup");
4591 SIMPLEQ_INIT(&s->colors);
4593 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4594 err = add_color(&s->colors, "\\$$",
4595 TOG_COLOR_TREE_SUBMODULE,
4596 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4599 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4600 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4602 free_colors(&s->colors);
4605 err = add_color(&s->colors, "/$",
4606 TOG_COLOR_TREE_DIRECTORY,
4607 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4609 free_colors(&s->colors);
4613 err = add_color(&s->colors, "\\*$",
4614 TOG_COLOR_TREE_EXECUTABLE,
4615 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4617 free_colors(&s->colors);
4621 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4622 get_color_value("TOG_COLOR_COMMIT"));
4624 free_colors(&s->colors);
4629 view->show = show_tree_view;
4630 view->input = input_tree_view;
4631 view->close = close_tree_view;
4632 view->search_start = search_start_tree_view;
4633 view->search_next = search_next_tree_view;
4635 free(commit_id_str);
4637 free(s->tree_label);
4638 s->tree_label = NULL;
4643 static const struct got_error *
4644 close_tree_view(struct tog_view *view)
4646 struct tog_tree_view_state *s = &view->state.tree;
4648 free_colors(&s->colors);
4649 free(s->tree_label);
4650 s->tree_label = NULL;
4652 s->commit_id = NULL;
4653 while (!TAILQ_EMPTY(&s->parents)) {
4654 struct tog_parent_tree *parent;
4655 parent = TAILQ_FIRST(&s->parents);
4656 TAILQ_REMOVE(&s->parents, parent, entry);
4660 if (s->tree != s->root)
4661 got_object_tree_close(s->tree);
4662 got_object_tree_close(s->root);
4667 static const struct got_error *
4668 search_start_tree_view(struct tog_view *view)
4670 struct tog_tree_view_state *s = &view->state.tree;
4672 s->matched_entry = NULL;
4677 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4679 regmatch_t regmatch;
4681 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
4685 static const struct got_error *
4686 search_next_tree_view(struct tog_view *view)
4688 struct tog_tree_view_state *s = &view->state.tree;
4689 struct got_tree_entry *te = NULL;
4691 if (!view->searching) {
4692 view->search_next_done = 1;
4696 if (s->matched_entry) {
4697 if (view->searching == TOG_SEARCH_FORWARD) {
4698 if (s->selected_entry)
4699 te = got_tree_entry_get_next(s->tree,
4702 te = got_object_tree_get_first_entry(s->tree);
4704 if (s->selected_entry == NULL)
4705 te = got_object_tree_get_last_entry(s->tree);
4707 te = got_tree_entry_get_prev(s->tree,
4711 if (view->searching == TOG_SEARCH_FORWARD)
4712 te = got_object_tree_get_first_entry(s->tree);
4714 te = got_object_tree_get_last_entry(s->tree);
4719 if (s->matched_entry == NULL) {
4720 view->search_next_done = 1;
4723 if (view->searching == TOG_SEARCH_FORWARD)
4724 te = got_object_tree_get_first_entry(s->tree);
4726 te = got_object_tree_get_last_entry(s->tree);
4729 if (match_tree_entry(te, &view->regex)) {
4730 view->search_next_done = 1;
4731 s->matched_entry = te;
4735 if (view->searching == TOG_SEARCH_FORWARD)
4736 te = got_tree_entry_get_next(s->tree, te);
4738 te = got_tree_entry_get_prev(s->tree, te);
4741 if (s->matched_entry) {
4742 s->first_displayed_entry = s->matched_entry;
4749 static const struct got_error *
4750 show_tree_view(struct tog_view *view)
4752 const struct got_error *err = NULL;
4753 struct tog_tree_view_state *s = &view->state.tree;
4756 err = tree_entry_path(&parent_path, &s->parents, NULL);
4760 err = draw_tree_entries(view, &s->first_displayed_entry,
4761 &s->last_displayed_entry, &s->selected_entry,
4762 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4763 s->tree, s->selected, view->nlines, s->tree == s->root,
4771 static const struct got_error *
4772 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4773 struct tog_view **focus_view, struct tog_view *view, int ch)
4775 const struct got_error *err = NULL;
4776 struct tog_tree_view_state *s = &view->state.tree;
4777 struct tog_view *log_view;
4778 int begin_x = 0, nscrolled;
4782 s->show_ids = !s->show_ids;
4785 if (!s->selected_entry)
4787 if (view_is_parent_view(view))
4788 begin_x = view_split_begin_x(view->begin_x);
4789 err = log_tree_entry(&log_view, begin_x,
4790 s->selected_entry, &s->parents,
4791 s->commit_id, s->refs, s->repo);
4792 if (view_is_parent_view(view)) {
4793 err = view_close_child(view);
4796 err = view_set_child(view, log_view);
4798 view_close(log_view);
4801 *focus_view = log_view;
4802 view->child_focussed = 1;
4804 *new_view = log_view;
4808 if (s->selected > 0) {
4810 if (s->selected == 0)
4813 if (s->selected > 0)
4815 tree_scroll_up(view, &s->first_displayed_entry, 1,
4816 s->tree, s->tree == s->root);
4819 tree_scroll_up(view, &s->first_displayed_entry,
4820 MAX(0, view->nlines - 4 - s->selected), s->tree,
4821 s->tree == s->root);
4823 if (got_object_tree_get_first_entry(s->tree) ==
4824 s->first_displayed_entry && s->tree != s->root)
4825 s->first_displayed_entry = NULL;
4829 if (s->selected < s->ndisplayed - 1) {
4833 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4835 /* can't scroll any further */
4837 tree_scroll_down(&s->first_displayed_entry, 1,
4838 s->last_displayed_entry, s->tree);
4841 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4843 /* can't scroll any further; move cursor down */
4844 if (s->selected < s->ndisplayed - 1)
4845 s->selected = s->ndisplayed - 1;
4848 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4849 view->nlines, s->last_displayed_entry, s->tree);
4850 if (nscrolled < view->nlines) {
4852 struct got_tree_entry *te;
4853 te = s->first_displayed_entry;
4856 te = got_tree_entry_get_next(s->tree, te);
4858 s->selected = ndisplayed - 1;
4864 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4865 struct tog_parent_tree *parent;
4866 /* user selected '..' */
4867 if (s->tree == s->root)
4869 parent = TAILQ_FIRST(&s->parents);
4870 TAILQ_REMOVE(&s->parents, parent,
4872 got_object_tree_close(s->tree);
4873 s->tree = parent->tree;
4874 s->first_displayed_entry =
4875 parent->first_displayed_entry;
4877 parent->selected_entry;
4878 s->selected = parent->selected;
4880 } else if (S_ISDIR(got_tree_entry_get_mode(
4881 s->selected_entry))) {
4882 struct got_tree_object *subtree;
4883 err = got_object_open_as_tree(&subtree, s->repo,
4884 got_tree_entry_get_id(s->selected_entry));
4887 err = tree_view_visit_subtree(subtree, s);
4889 got_object_tree_close(subtree);
4892 } else if (S_ISREG(got_tree_entry_get_mode(
4893 s->selected_entry))) {
4894 struct tog_view *blame_view;
4895 int begin_x = view_is_parent_view(view) ?
4896 view_split_begin_x(view->begin_x) : 0;
4898 err = blame_tree_entry(&blame_view, begin_x,
4899 s->selected_entry, &s->parents,
4900 s->commit_id, s->refs, s->repo);
4903 if (view_is_parent_view(view)) {
4904 err = view_close_child(view);
4907 err = view_set_child(view, blame_view);
4909 view_close(blame_view);
4912 *focus_view = blame_view;
4913 view->child_focussed = 1;
4915 *new_view = blame_view;
4919 if (s->selected > view->nlines)
4920 s->selected = s->ndisplayed - 1;
4933 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4938 static const struct got_error *
4939 cmd_tree(int argc, char *argv[])
4941 const struct got_error *error;
4942 struct got_repository *repo = NULL;
4943 struct got_reflist_head refs;
4944 char *repo_path = NULL;
4945 struct got_object_id *commit_id = NULL;
4946 char *commit_id_arg = NULL;
4947 struct got_commit_object *commit = NULL;
4948 struct got_tree_object *tree = NULL;
4950 struct tog_view *view;
4952 SIMPLEQ_INIT(&refs);
4955 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4960 while ((ch = getopt(argc, argv, "c:")) != -1) {
4963 commit_id_arg = optarg;
4975 struct got_worktree *worktree;
4976 char *cwd = getcwd(NULL, 0);
4978 return got_error_from_errno("getcwd");
4979 error = got_worktree_open(&worktree, cwd);
4980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4985 strdup(got_worktree_get_repo_path(worktree));
4986 got_worktree_close(worktree);
4989 if (repo_path == NULL) {
4990 error = got_error_from_errno("strdup");
4993 } else if (argc == 1) {
4994 repo_path = realpath(argv[0], NULL);
4995 if (repo_path == NULL)
4996 return got_error_from_errno2("realpath", argv[0]);
5002 error = got_repo_open(&repo, repo_path, NULL);
5006 error = apply_unveil(got_repo_get_path(repo), NULL);
5010 if (commit_id_arg == NULL)
5011 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5013 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5015 if (error->code != GOT_ERR_NOT_REF)
5017 error = got_repo_match_object_id_prefix(&commit_id,
5018 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5024 error = got_object_open_as_commit(&commit, repo, commit_id);
5028 error = got_object_open_as_tree(&tree, repo,
5029 got_object_commit_get_tree_id(commit));
5033 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5037 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5039 error = got_error_from_errno("view_open");
5042 error = open_tree_view(view, tree, commit_id, &refs, repo);
5045 error = view_loop(view);
5050 got_object_commit_close(commit);
5052 got_object_tree_close(tree);
5054 got_repo_close(repo);
5055 got_ref_list_free(&refs);
5064 fprintf(stderr, "commands:");
5065 for (i = 0; i < nitems(tog_commands); i++) {
5066 struct tog_cmd *cmd = &tog_commands[i];
5067 fprintf(stderr, " %s", cmd->name);
5069 fputc('\n', stderr);
5075 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5083 make_argv(const char *arg0, const char *arg1)
5086 int argc = (arg1 == NULL ? 1 : 2);
5088 argv = calloc(argc, sizeof(char *));
5091 argv[0] = strdup(arg0);
5092 if (argv[0] == NULL)
5095 argv[1] = strdup(arg1);
5096 if (argv[1] == NULL)
5104 main(int argc, char *argv[])
5106 const struct got_error *error = NULL;
5107 struct tog_cmd *cmd = NULL;
5108 int ch, hflag = 0, Vflag = 0;
5109 char **cmd_argv = NULL;
5111 setlocale(LC_CTYPE, "");
5113 while ((ch = getopt(argc, argv, "hV")) != -1) {
5133 got_version_print_str();
5140 /* Build an argument vector which runs a default command. */
5141 cmd = &tog_commands[0];
5142 cmd_argv = make_argv(cmd->name, NULL);
5147 /* Did the user specific a command? */
5148 for (i = 0; i < nitems(tog_commands); i++) {
5149 if (strncmp(tog_commands[i].name, argv[0],
5150 strlen(argv[0])) == 0) {
5151 cmd = &tog_commands[i];
5157 fprintf(stderr, "%s: unknown command '%s'\n",
5158 getprogname(), argv[0]);
5167 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5171 if (error && error->code != GOT_ERR_CANCELLED)
5172 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);