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;
115 struct tog_line_color {
116 SIMPLEQ_ENTRY(tog_line_color) entry;
120 SIMPLEQ_HEAD(tog_line_colors, tog_line_color);
122 struct tog_diff_view_state {
123 struct got_object_id *id1, *id2;
125 int first_displayed_line;
126 int last_displayed_line;
129 struct got_repository *repo;
130 struct got_reflist_head *refs;
131 struct tog_line_colors line_colors;
133 /* passed from log view; may be NULL */
134 struct tog_view *log_view;
137 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
139 struct tog_log_thread_args {
140 pthread_cond_t need_commits;
142 struct got_commit_graph *graph;
143 struct commit_queue *commits;
144 const char *in_repo_path;
145 struct got_object_id *start_id;
146 struct got_repository *repo;
149 struct commit_queue_entry **first_displayed_entry;
150 struct commit_queue_entry **selected_entry;
152 int *search_next_done;
156 struct tog_log_view_state {
157 struct commit_queue commits;
158 struct commit_queue_entry *first_displayed_entry;
159 struct commit_queue_entry *last_displayed_entry;
160 struct commit_queue_entry *selected_entry;
163 const char *head_ref_name;
164 struct got_repository *repo;
165 struct got_reflist_head *refs;
166 struct got_object_id *start_id;
169 struct tog_log_thread_args thread_args;
170 struct commit_queue_entry *matched_entry;
171 struct commit_queue_entry *search_entry;
174 struct tog_blame_cb_args {
175 struct tog_blame_line *lines; /* one per line */
178 struct tog_view *view;
179 struct got_object_id *commit_id;
183 struct tog_blame_thread_args {
185 struct got_repository *repo;
186 struct tog_blame_cb_args *cb_args;
188 got_cancel_cb cancel_cb;
195 struct tog_blame_line *lines;
199 struct tog_blame_thread_args thread_args;
200 struct tog_blame_cb_args cb_args;
204 struct tog_blame_view_state {
205 int first_displayed_line;
206 int last_displayed_line;
211 struct got_object_id_queue blamed_commits;
212 struct got_object_qid *blamed_commit;
214 struct got_repository *repo;
215 struct got_reflist_head *refs;
216 struct got_object_id *commit_id;
217 struct tog_blame blame;
221 struct tog_parent_tree {
222 TAILQ_ENTRY(tog_parent_tree) entry;
223 struct got_tree_object *tree;
224 struct got_tree_entry *first_displayed_entry;
225 struct got_tree_entry *selected_entry;
229 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
231 struct tog_tree_view_state {
233 struct got_tree_object *root;
234 struct got_tree_object *tree;
235 const struct got_tree_entries *entries;
236 struct got_tree_entry *first_displayed_entry;
237 struct got_tree_entry *last_displayed_entry;
238 struct got_tree_entry *selected_entry;
239 int ndisplayed, selected, show_ids;
240 struct tog_parent_trees parents;
241 struct got_object_id *commit_id;
242 struct got_repository *repo;
243 struct got_reflist_head *refs;
244 struct got_tree_entry *matched_entry;
245 struct tog_line_colors line_colors;
249 * We implement two types of views: parent views and child views.
251 * The 'Tab' key switches between a parent view and its child view.
252 * Child views are shown side-by-side to their parent view, provided
253 * there is enough screen estate.
255 * When a new view is opened from within a parent view, this new view
256 * becomes a child view of the parent view, replacing any existing child.
258 * When a new view is opened from within a child view, this new view
259 * becomes a parent view which will obscure the views below until the
260 * user quits the new parent view by typing 'q'.
262 * This list of views contains parent views only.
263 * Child views are only pointed to by their parent view.
265 TAILQ_HEAD(tog_view_list_head, tog_view);
268 TAILQ_ENTRY(tog_view) entry;
271 int nlines, ncols, begin_y, begin_x;
272 int lines, cols; /* copies of LINES and COLS */
274 struct tog_view *parent;
275 struct tog_view *child;
278 /* type-specific state */
279 enum tog_view_type type;
281 struct tog_diff_view_state diff;
282 struct tog_log_view_state log;
283 struct tog_blame_view_state blame;
284 struct tog_tree_view_state tree;
287 const struct got_error *(*show)(struct tog_view *);
288 const struct got_error *(*input)(struct tog_view **,
289 struct tog_view **, struct tog_view**, struct tog_view *, int);
290 const struct got_error *(*close)(struct tog_view *);
292 const struct got_error *(*search_start)(struct tog_view *);
293 const struct got_error *(*search_next)(struct tog_view *);
295 #define TOG_SEARCH_FORWARD 1
296 #define TOG_SEARCH_BACKWARD 2
297 int search_next_done;
301 static const struct got_error *open_diff_view(struct tog_view *,
302 struct got_object_id *, struct got_object_id *, struct tog_view *,
303 struct got_reflist_head *, struct got_repository *);
304 static const struct got_error *show_diff_view(struct tog_view *);
305 static const struct got_error *input_diff_view(struct tog_view **,
306 struct tog_view **, struct tog_view **, struct tog_view *, int);
307 static const struct got_error* close_diff_view(struct tog_view *);
309 static const struct got_error *open_log_view(struct tog_view *,
310 struct got_object_id *, struct got_reflist_head *,
311 struct got_repository *, const char *, const char *, int);
312 static const struct got_error * show_log_view(struct tog_view *);
313 static const struct got_error *input_log_view(struct tog_view **,
314 struct tog_view **, struct tog_view **, struct tog_view *, int);
315 static const struct got_error *close_log_view(struct tog_view *);
316 static const struct got_error *search_start_log_view(struct tog_view *);
317 static const struct got_error *search_next_log_view(struct tog_view *);
319 static const struct got_error *open_blame_view(struct tog_view *, char *,
320 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
321 static const struct got_error *show_blame_view(struct tog_view *);
322 static const struct got_error *input_blame_view(struct tog_view **,
323 struct tog_view **, struct tog_view **, struct tog_view *, int);
324 static const struct got_error *close_blame_view(struct tog_view *);
325 static const struct got_error *search_start_blame_view(struct tog_view *);
326 static const struct got_error *search_next_blame_view(struct tog_view *);
328 static const struct got_error *open_tree_view(struct tog_view *,
329 struct got_tree_object *, struct got_object_id *,
330 struct got_reflist_head *, struct got_repository *);
331 static const struct got_error *show_tree_view(struct tog_view *);
332 static const struct got_error *input_tree_view(struct tog_view **,
333 struct tog_view **, struct tog_view **, struct tog_view *, int);
334 static const struct got_error *close_tree_view(struct tog_view *);
335 static const struct got_error *search_start_tree_view(struct tog_view *);
336 static const struct got_error *search_next_tree_view(struct tog_view *);
338 static volatile sig_atomic_t tog_sigwinch_received;
339 static volatile sig_atomic_t tog_sigpipe_received;
342 tog_sigwinch(int signo)
344 tog_sigwinch_received = 1;
348 tog_sigpipe(int signo)
350 tog_sigpipe_received = 1;
353 static const struct got_error *
354 view_close(struct tog_view *view)
356 const struct got_error *err = NULL;
359 view_close(view->child);
363 err = view->close(view);
365 del_panel(view->panel);
367 delwin(view->window);
372 static struct tog_view *
373 view_open(int nlines, int ncols, int begin_y, int begin_x,
374 enum tog_view_type type)
376 struct tog_view *view = calloc(1, sizeof(*view));
384 view->nlines = nlines ? nlines : LINES - begin_y;
385 view->ncols = ncols ? ncols : COLS - begin_x;
386 view->begin_y = begin_y;
387 view->begin_x = begin_x;
388 view->window = newwin(nlines, ncols, begin_y, begin_x);
389 if (view->window == NULL) {
393 view->panel = new_panel(view->window);
394 if (view->panel == NULL ||
395 set_panel_userptr(view->panel, view) != OK) {
400 keypad(view->window, TRUE);
405 view_split_begin_x(int begin_x)
407 if (begin_x > 0 || COLS < 120)
409 return (COLS - MAX(COLS / 2, 80));
412 static const struct got_error *view_resize(struct tog_view *);
414 static const struct got_error *
415 view_splitscreen(struct tog_view *view)
417 const struct got_error *err = NULL;
420 view->begin_x = view_split_begin_x(0);
421 view->nlines = LINES;
422 view->ncols = COLS - view->begin_x;
425 err = view_resize(view);
429 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
430 return got_error_from_errno("mvwin");
435 static const struct got_error *
436 view_fullscreen(struct tog_view *view)
438 const struct got_error *err = NULL;
442 view->nlines = LINES;
446 err = view_resize(view);
450 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
451 return got_error_from_errno("mvwin");
457 view_is_parent_view(struct tog_view *view)
459 return view->parent == NULL;
462 static const struct got_error *
463 view_resize(struct tog_view *view)
467 if (view->lines > LINES)
468 nlines = view->nlines - (view->lines - LINES);
470 nlines = view->nlines + (LINES - view->lines);
472 if (view->cols > COLS)
473 ncols = view->ncols - (view->cols - COLS);
475 ncols = view->ncols + (COLS - view->cols);
477 if (wresize(view->window, nlines, ncols) == ERR)
478 return got_error_from_errno("wresize");
479 if (replace_panel(view->panel, view->window) == ERR)
480 return got_error_from_errno("replace_panel");
481 wclear(view->window);
483 view->nlines = nlines;
489 view->child->begin_x = view_split_begin_x(view->begin_x);
490 if (view->child->begin_x == 0) {
491 view_fullscreen(view->child);
492 if (view->child->focussed)
493 show_panel(view->child->panel);
495 show_panel(view->panel);
497 view_splitscreen(view->child);
498 show_panel(view->child->panel);
505 static const struct got_error *
506 view_close_child(struct tog_view *view)
508 const struct got_error *err = NULL;
510 if (view->child == NULL)
513 err = view_close(view->child);
518 static const struct got_error *
519 view_set_child(struct tog_view *view, struct tog_view *child)
521 const struct got_error *err = NULL;
524 child->parent = view;
529 view_is_splitscreen(struct tog_view *view)
531 return view->begin_x > 0;
540 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
541 cols = 80; /* Default */
547 resize_term(lines, cols);
550 static const struct got_error *
551 view_search_start(struct tog_view *view)
553 const struct got_error *err = NULL;
558 if (view->nlines < 1)
561 if (!view_is_parent_view(view))
562 begin_x = view_split_begin_x(view->begin_x);
563 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
565 wclrtoeol(view->window);
569 ret = wgetnstr(view->window, pattern, sizeof(pattern));
575 if (view->searching) {
576 regfree(&view->regex);
580 if (regcomp(&view->regex, pattern,
581 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
582 err = view->search_start(view);
584 regfree(&view->regex);
587 view->searching = TOG_SEARCH_FORWARD;
588 view->search_next_done = 0;
589 view->search_next(view);
595 static const struct got_error *
596 view_input(struct tog_view **new, struct tog_view **dead,
597 struct tog_view **focus, int *done, struct tog_view *view,
598 struct tog_view_list_head *views)
600 const struct got_error *err = NULL;
608 if (view->searching && !view->search_next_done) {
609 errcode = pthread_mutex_unlock(&tog_mutex);
611 return got_error_set_errno(errcode,
612 "pthread_mutex_unlock");
614 errcode = pthread_mutex_lock(&tog_mutex);
616 return got_error_set_errno(errcode,
617 "pthread_mutex_lock");
618 view->search_next(view);
622 nodelay(stdscr, FALSE);
623 /* Allow threads to make progress while we are waiting for input. */
624 errcode = pthread_mutex_unlock(&tog_mutex);
626 return got_error_set_errno(errcode, "pthread_mutex_unlock");
627 ch = wgetch(view->window);
628 errcode = pthread_mutex_lock(&tog_mutex);
630 return got_error_set_errno(errcode, "pthread_mutex_lock");
631 nodelay(stdscr, TRUE);
633 if (tog_sigwinch_received) {
635 tog_sigwinch_received = 0;
636 TAILQ_FOREACH(v, views, entry) {
637 err = view_resize(v);
640 err = v->input(new, dead, focus, v, KEY_RESIZE);
651 *focus = view->child;
652 view->child_focussed = 1;
653 } else if (view->parent) {
654 *focus = view->parent;
655 view->parent->child_focussed = 0;
659 err = view->input(new, dead, focus, view, ch);
666 if (view_is_parent_view(view)) {
667 if (view->child == NULL)
669 if (view_is_splitscreen(view->child)) {
670 *focus = view->child;
671 view->child_focussed = 1;
672 err = view_fullscreen(view->child);
674 err = view_splitscreen(view->child);
677 err = view->child->input(new, dead, focus,
678 view->child, KEY_RESIZE);
680 if (view_is_splitscreen(view)) {
682 view->parent->child_focussed = 1;
683 err = view_fullscreen(view);
685 err = view_splitscreen(view);
689 err = view->input(new, dead, focus, view,
696 if (view->search_start)
697 view_search_start(view);
699 err = view->input(new, dead, focus, view, ch);
703 if (view->search_next && view->searching) {
704 view->searching = (ch == 'n' ?
705 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
706 view->search_next_done = 0;
707 view->search_next(view);
709 err = view->input(new, dead, focus, view, ch);
712 err = view->input(new, dead, focus, view, ch);
720 view_vborder(struct tog_view *view)
723 struct tog_view *view_above;
726 return view_vborder(view->parent);
728 panel = panel_above(view->panel);
732 view_above = panel_userptr(panel);
733 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
734 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
738 view_needs_focus_indication(struct tog_view *view)
740 if (view_is_parent_view(view)) {
741 if (view->child == NULL || view->child_focussed)
743 if (!view_is_splitscreen(view->child))
745 } else if (!view_is_splitscreen(view))
748 return view->focussed;
751 static const struct got_error *
752 view_loop(struct tog_view *view)
754 const struct got_error *err = NULL;
755 struct tog_view_list_head views;
756 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
757 int fast_refresh = 10;
758 int done = 0, errcode;
760 errcode = pthread_mutex_lock(&tog_mutex);
762 return got_error_set_errno(errcode, "pthread_mutex_lock");
765 TAILQ_INSERT_HEAD(&views, view, entry);
769 err = view->show(view);
774 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
775 /* Refresh fast during initialization, then become slower. */
776 if (fast_refresh && fast_refresh-- == 0)
777 halfdelay(10); /* switch to once per second */
779 err = view_input(&new_view, &dead_view, &focus_view, &done,
784 struct tog_view *prev = NULL;
786 if (view_is_parent_view(dead_view))
787 prev = TAILQ_PREV(dead_view,
788 tog_view_list_head, entry);
789 else if (view->parent != dead_view)
792 if (dead_view->parent)
793 dead_view->parent->child = NULL;
795 TAILQ_REMOVE(&views, dead_view, entry);
797 err = view_close(dead_view);
798 if (err || (dead_view == main_view && new_view == NULL))
801 if (view == dead_view) {
806 else if (!TAILQ_EMPTY(&views))
807 view = TAILQ_LAST(&views,
812 if (view->child && view->child_focussed)
813 focus_view = view->child;
820 struct tog_view *v, *t;
821 /* Only allow one parent view per type. */
822 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
823 if (v->type != new_view->type)
825 TAILQ_REMOVE(&views, v, entry);
831 TAILQ_INSERT_TAIL(&views, new_view, entry);
833 if (focus_view == NULL)
834 focus_view = new_view;
837 show_panel(focus_view->panel);
840 focus_view->focussed = 1;
843 show_panel(new_view->panel);
844 if (view->child && view_is_splitscreen(view->child))
845 show_panel(view->child->panel);
848 if (focus_view == NULL) {
850 show_panel(view->panel);
851 if (view->child && view_is_splitscreen(view->child))
852 show_panel(view->child->panel);
856 err = view->parent->show(view->parent);
860 err = view->show(view);
864 err = view->child->show(view->child);
873 while (!TAILQ_EMPTY(&views)) {
874 view = TAILQ_FIRST(&views);
875 TAILQ_REMOVE(&views, view, entry);
879 errcode = pthread_mutex_unlock(&tog_mutex);
880 if (errcode && err == NULL)
881 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
891 "usage: %s log [-c commit] [-r repository-path] [path]\n",
896 /* Create newly allocated wide-character string equivalent to a byte string. */
897 static const struct got_error *
898 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
901 const struct got_error *err = NULL;
904 *wlen = mbstowcs(NULL, s, 0);
905 if (*wlen == (size_t)-1) {
908 return got_error_from_errno("mbstowcs");
910 /* byte string invalid in current encoding; try to "fix" it */
911 err = got_mbsavis(&vis, &vislen, s);
914 *wlen = mbstowcs(NULL, vis, 0);
915 if (*wlen == (size_t)-1) {
916 err = got_error_from_errno("mbstowcs"); /* give up */
921 *ws = calloc(*wlen + 1, sizeof(**ws));
923 err = got_error_from_errno("calloc");
927 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
928 err = got_error_from_errno("mbstowcs");
939 /* Format a line for display, ensuring that it won't overflow a width limit. */
940 static const struct got_error *
941 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
944 const struct got_error *err = NULL;
946 wchar_t *wline = NULL;
953 err = mbs2ws(&wline, &wlen, line);
959 int width = wcwidth(wline[i]);
966 if (width == 1 || width == 2) {
967 if (cols + width > wlimit)
971 } else if (width == -1) {
972 if (wline[i] == L'\t') {
974 ((cols + col_tab_align) % TABSIZE);
975 if (cols + width > wlimit)
981 err = got_error_from_errno("wcwidth");
996 static const struct got_error*
997 build_refs_str(char **refs_str, struct got_reflist_head *refs,
998 struct got_object_id *id, struct got_repository *repo)
1000 static const struct got_error *err = NULL;
1001 struct got_reflist_entry *re;
1007 SIMPLEQ_FOREACH(re, refs, entry) {
1008 struct got_tag_object *tag = NULL;
1011 name = got_ref_get_name(re->ref);
1012 if (strcmp(name, GOT_REF_HEAD) == 0)
1014 if (strncmp(name, "refs/", 5) == 0)
1016 if (strncmp(name, "got/", 4) == 0)
1018 if (strncmp(name, "heads/", 6) == 0)
1020 if (strncmp(name, "remotes/", 8) == 0)
1022 if (strncmp(name, "tags/", 5) == 0) {
1023 err = got_object_open_as_tag(&tag, repo, re->id);
1025 if (err->code != GOT_ERR_OBJ_TYPE)
1027 /* Ref points at something other than a tag. */
1032 cmp = got_object_id_cmp(tag ?
1033 got_object_tag_get_object_id(tag) : re->id, id);
1035 got_object_tag_close(tag);
1039 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1040 s ? ", " : "", name) == -1) {
1041 err = got_error_from_errno("asprintf");
1052 static const struct got_error *
1053 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1056 char *smallerthan, *at;
1058 smallerthan = strchr(author, '<');
1059 if (smallerthan && smallerthan[1] != '\0')
1060 author = smallerthan + 1;
1061 at = strchr(author, '@');
1064 return format_line(wauthor, author_width, author, limit, col_tab_align);
1067 static const struct got_error *
1068 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1069 struct got_object_id *id, struct got_reflist_head *refs,
1070 const size_t date_display_cols, int author_display_cols)
1072 const struct got_error *err = NULL;
1073 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1074 char *logmsg0 = NULL, *logmsg = NULL;
1075 char *author = NULL;
1076 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1077 int author_width, logmsg_width;
1078 char *newline, *line = NULL;
1080 const int avail = view->ncols;
1082 time_t committer_time;
1084 committer_time = got_object_commit_get_committer_time(commit);
1085 if (localtime_r(&committer_time, &tm) == NULL)
1086 return got_error_from_errno("localtime_r");
1087 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1089 return got_error(GOT_ERR_NO_SPACE);
1091 if (avail <= date_display_cols)
1092 limit = MIN(sizeof(datebuf) - 1, avail);
1094 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1095 waddnstr(view->window, datebuf, limit);
1102 err = got_object_id_str(&id_str, id);
1105 wprintw(view->window, "%.8s ", id_str);
1112 author = strdup(got_object_commit_get_author(commit));
1113 if (author == NULL) {
1114 err = got_error_from_errno("strdup");
1117 err = format_author(&wauthor, &author_width, author, avail - col, col);
1120 waddwstr(view->window, wauthor);
1121 col += author_width;
1122 while (col < avail && author_width < author_display_cols + 2) {
1123 waddch(view->window, ' ');
1130 err = got_object_commit_get_logmsg(&logmsg0, commit);
1134 while (*logmsg == '\n')
1136 newline = strchr(logmsg, '\n');
1139 limit = avail - col;
1140 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1143 waddwstr(view->window, wlogmsg);
1144 col += logmsg_width;
1145 while (col < avail) {
1146 waddch(view->window, ' ');
1158 static struct commit_queue_entry *
1159 alloc_commit_queue_entry(struct got_commit_object *commit,
1160 struct got_object_id *id)
1162 struct commit_queue_entry *entry;
1164 entry = calloc(1, sizeof(*entry));
1169 entry->commit = commit;
1174 pop_commit(struct commit_queue *commits)
1176 struct commit_queue_entry *entry;
1178 entry = TAILQ_FIRST(&commits->head);
1179 TAILQ_REMOVE(&commits->head, entry, entry);
1180 got_object_commit_close(entry->commit);
1181 commits->ncommits--;
1182 /* Don't free entry->id! It is owned by the commit graph. */
1187 free_commits(struct commit_queue *commits)
1189 while (!TAILQ_EMPTY(&commits->head))
1190 pop_commit(commits);
1193 static const struct got_error *
1194 match_commit(int *have_match, struct got_object_id *id,
1195 struct got_commit_object *commit, regex_t *regex)
1197 const struct got_error *err = NULL;
1198 regmatch_t regmatch;
1199 char *id_str = NULL, *logmsg = NULL;
1203 err = got_object_id_str(&id_str, id);
1207 err = got_object_commit_get_logmsg(&logmsg, commit);
1211 if (regexec(regex, got_object_commit_get_author(commit), 1,
1212 ®match, 0) == 0 ||
1213 regexec(regex, got_object_commit_get_committer(commit), 1,
1214 ®match, 0) == 0 ||
1215 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1216 regexec(regex, logmsg, 1, ®match, 0) == 0)
1224 static const struct got_error *
1225 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1226 int minqueue, struct got_repository *repo, const char *path,
1227 int *searching, int *search_next_done, regex_t *regex)
1229 const struct got_error *err = NULL;
1230 int nqueued = 0, have_match = 0;
1233 * We keep all commits open throughout the lifetime of the log
1234 * view in order to avoid having to re-fetch commits from disk
1235 * while updating the display.
1237 while (nqueued < minqueue ||
1238 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1239 struct got_object_id *id;
1240 struct got_commit_object *commit;
1241 struct commit_queue_entry *entry;
1244 err = got_commit_graph_iter_next(&id, graph);
1246 if (err->code != GOT_ERR_ITER_NEED_MORE)
1248 err = got_commit_graph_fetch_commits(graph,
1249 minqueue, repo, NULL, NULL);
1258 err = got_object_open_as_commit(&commit, repo, id);
1261 entry = alloc_commit_queue_entry(commit, id);
1262 if (entry == NULL) {
1263 err = got_error_from_errno("alloc_commit_queue_entry");
1267 errcode = pthread_mutex_lock(&tog_mutex);
1269 err = got_error_set_errno(errcode,
1270 "pthread_mutex_lock");
1274 entry->idx = commits->ncommits;
1275 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1277 commits->ncommits++;
1279 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1280 err = match_commit(&have_match, id, commit, regex);
1282 pthread_mutex_lock(&tog_mutex);
1287 errcode = pthread_mutex_unlock(&tog_mutex);
1288 if (errcode && err == NULL)
1289 err = got_error_set_errno(errcode,
1290 "pthread_mutex_unlock");
1299 static const struct got_error *
1300 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1301 struct got_repository *repo)
1303 const struct got_error *err = NULL;
1304 struct got_reference *head_ref;
1308 err = got_ref_open(&head_ref, repo, branch_name, 0);
1312 err = got_ref_resolve(head_id, repo, head_ref);
1313 got_ref_close(head_ref);
1322 static const struct got_error *
1323 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1324 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1325 struct commit_queue *commits, int selected_idx, int limit,
1326 struct got_reflist_head *refs, const char *path, int commits_needed)
1328 const struct got_error *err = NULL;
1329 struct tog_log_view_state *s = &view->state.log;
1330 struct commit_queue_entry *entry;
1332 int ncommits, author_cols = 10;
1333 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1334 char *refs_str = NULL;
1336 static const size_t date_display_cols = 9;
1341 if (ncommits == selected_idx) {
1345 entry = TAILQ_NEXT(entry, entry);
1349 if (*selected && !(view->searching && view->search_next_done == 0)) {
1350 err = got_object_id_str(&id_str, (*selected)->id);
1354 err = build_refs_str(&refs_str, refs, (*selected)->id,
1361 if (commits_needed == 0)
1362 halfdelay(10); /* disable fast refresh */
1364 if (asprintf(&ncommits_str, " [%d/%d] %s",
1365 entry ? entry->idx + 1 : 0, commits->ncommits,
1366 commits_needed > 0 ?
1367 (view->searching && view->search_next_done == 0
1368 ? "searching..." : "loading... ") :
1369 (refs_str ? refs_str : "")) == -1) {
1370 err = got_error_from_errno("asprintf");
1374 if (path && strcmp(path, "/") != 0) {
1375 if (asprintf(&header, "commit %s %s%s",
1376 id_str ? id_str : "........................................",
1377 path, ncommits_str) == -1) {
1378 err = got_error_from_errno("asprintf");
1382 } else if (asprintf(&header, "commit %s%s",
1383 id_str ? id_str : "........................................",
1384 ncommits_str) == -1) {
1385 err = got_error_from_errno("asprintf");
1389 err = format_line(&wline, &width, header, view->ncols, 0);
1393 werase(view->window);
1395 if (view_needs_focus_indication(view))
1396 wstandout(view->window);
1397 waddwstr(view->window, wline);
1398 while (width < view->ncols) {
1399 waddch(view->window, ' ');
1402 if (view_needs_focus_indication(view))
1403 wstandend(view->window);
1408 /* Grow author column size if necessary. */
1415 if (ncommits >= limit - 1)
1417 author = strdup(got_object_commit_get_author(entry->commit));
1418 if (author == NULL) {
1419 err = got_error_from_errno("strdup");
1422 err = format_author(&wauthor, &width, author, COLS,
1424 if (author_cols < width)
1425 author_cols = width;
1429 entry = TAILQ_NEXT(entry, entry);
1436 if (ncommits >= limit - 1)
1438 if (ncommits == selected_idx)
1439 wstandout(view->window);
1440 err = draw_commit(view, entry->commit, entry->id, refs,
1441 date_display_cols, author_cols);
1442 if (ncommits == selected_idx)
1443 wstandend(view->window);
1448 entry = TAILQ_NEXT(entry, entry);
1461 scroll_up(struct tog_view *view,
1462 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1463 struct commit_queue *commits)
1465 struct commit_queue_entry *entry;
1468 entry = TAILQ_FIRST(&commits->head);
1469 if (*first_displayed_entry == entry)
1472 entry = *first_displayed_entry;
1473 while (entry && nscrolled < maxscroll) {
1474 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1476 *first_displayed_entry = entry;
1482 static const struct got_error *
1483 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1484 pthread_cond_t *need_commits)
1489 halfdelay(1); /* fast refresh while loading commits */
1491 while (*commits_needed > 0) {
1495 /* Wake the log thread. */
1496 errcode = pthread_cond_signal(need_commits);
1498 return got_error_set_errno(errcode,
1499 "pthread_cond_signal");
1500 errcode = pthread_mutex_unlock(&tog_mutex);
1502 return got_error_set_errno(errcode,
1503 "pthread_mutex_unlock");
1505 errcode = pthread_mutex_lock(&tog_mutex);
1507 return got_error_set_errno(errcode,
1508 "pthread_mutex_lock");
1510 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1512 * Thread is not done yet; lose a key press
1513 * and let the user retry... this way the GUI
1514 * remains interactive while logging deep paths
1515 * with few commits in history.
1524 static const struct got_error *
1525 scroll_down(struct tog_view *view,
1526 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1527 struct commit_queue_entry **last_displayed_entry,
1528 struct commit_queue *commits, int *log_complete, int *commits_needed,
1529 pthread_cond_t *need_commits)
1531 const struct got_error *err = NULL;
1532 struct commit_queue_entry *pentry;
1535 if (*last_displayed_entry == NULL)
1538 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1539 if (pentry == NULL && !*log_complete) {
1541 * Ask the log thread for required amount of commits
1542 * plus some amount of pre-fetching.
1544 (*commits_needed) += maxscroll + 20;
1545 err = trigger_log_thread(0, commits_needed, log_complete,
1552 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1556 *last_displayed_entry = pentry;
1558 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1561 *first_displayed_entry = pentry;
1562 } while (++nscrolled < maxscroll);
1567 static const struct got_error *
1568 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1569 struct got_commit_object *commit, struct got_object_id *commit_id,
1570 struct tog_view *log_view, struct got_reflist_head *refs,
1571 struct got_repository *repo)
1573 const struct got_error *err;
1574 struct got_object_qid *parent_id;
1575 struct tog_view *diff_view;
1577 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1578 if (diff_view == NULL)
1579 return got_error_from_errno("view_open");
1581 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1582 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1583 commit_id, log_view, refs, repo);
1585 *new_view = diff_view;
1589 static const struct got_error *
1590 tree_view_visit_subtree(struct got_tree_object *subtree,
1591 struct tog_tree_view_state *s)
1593 struct tog_parent_tree *parent;
1595 parent = calloc(1, sizeof(*parent));
1597 return got_error_from_errno("calloc");
1599 parent->tree = s->tree;
1600 parent->first_displayed_entry = s->first_displayed_entry;
1601 parent->selected_entry = s->selected_entry;
1602 parent->selected = s->selected;
1603 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1605 s->entries = got_object_tree_get_entries(s->tree);
1607 s->first_displayed_entry = NULL;
1612 static const struct got_error *
1613 browse_commit_tree(struct tog_view **new_view, int begin_x,
1614 struct commit_queue_entry *entry, const char *path,
1615 struct got_reflist_head *refs, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_tree_object *tree;
1619 struct got_tree_entry *te;
1620 struct tog_tree_view_state *s;
1621 struct tog_view *tree_view;
1622 char *slash, *subpath = NULL;
1625 err = got_object_open_as_tree(&tree, repo,
1626 got_object_commit_get_tree_id(entry->commit));
1630 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1631 if (tree_view == NULL)
1632 return got_error_from_errno("view_open");
1634 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1636 got_object_tree_close(tree);
1639 s = &tree_view->state.tree;
1641 *new_view = tree_view;
1643 /* Walk the path and open corresponding tree objects. */
1648 struct got_object_id *tree_id;
1650 /* Ensure the correct subtree entry is selected. */
1651 slash = strchr(p, '/');
1653 slash = strchr(p, '\0');
1654 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1655 if (strncmp(p, te->name, slash - p) == 0) {
1656 s->selected_entry = te;
1661 if (s->selected_entry == NULL) {
1662 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1665 if (s->tree != s->root)
1666 s->selected++; /* skip '..' */
1668 if (!S_ISDIR(s->selected_entry->mode)) {
1669 /* Jump to this file's entry. */
1670 s->first_displayed_entry = s->selected_entry;
1675 slash = strchr(p, '/');
1677 subpath = strndup(path, slash - path);
1679 subpath = strdup(path);
1680 if (subpath == NULL) {
1681 err = got_error_from_errno("strdup");
1685 err = got_object_id_by_path(&tree_id, repo, entry->id,
1690 err = got_object_open_as_tree(&tree, repo, tree_id);
1695 err = tree_view_visit_subtree(tree, s);
1697 got_object_tree_close(tree);
1712 log_thread(void *arg)
1714 const struct got_error *err = NULL;
1716 struct tog_log_thread_args *a = arg;
1719 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1724 while (!done && !err && !tog_sigpipe_received) {
1725 err = queue_commits(a->graph, a->commits, 1, a->repo,
1726 a->in_repo_path, a->searching, a->search_next_done,
1729 if (err->code != GOT_ERR_ITER_COMPLETED)
1733 } else if (a->commits_needed > 0)
1734 a->commits_needed--;
1736 errcode = pthread_mutex_lock(&tog_mutex);
1738 err = got_error_set_errno(errcode,
1739 "pthread_mutex_lock");
1741 } else if (*a->quit)
1743 else if (*a->first_displayed_entry == NULL) {
1744 *a->first_displayed_entry =
1745 TAILQ_FIRST(&a->commits->head);
1746 *a->selected_entry = *a->first_displayed_entry;
1750 a->commits_needed = 0;
1751 else if (a->commits_needed == 0) {
1752 errcode = pthread_cond_wait(&a->need_commits,
1755 err = got_error_set_errno(errcode,
1756 "pthread_cond_wait");
1759 errcode = pthread_mutex_unlock(&tog_mutex);
1760 if (errcode && err == NULL)
1761 err = got_error_set_errno(errcode,
1762 "pthread_mutex_unlock");
1764 a->log_complete = 1;
1768 static const struct got_error *
1769 stop_log_thread(struct tog_log_view_state *s)
1771 const struct got_error *err = NULL;
1776 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1780 errcode = pthread_mutex_unlock(&tog_mutex);
1782 return got_error_set_errno(errcode,
1783 "pthread_mutex_unlock");
1784 errcode = pthread_join(s->thread, (void **)&err);
1786 return got_error_set_errno(errcode, "pthread_join");
1787 errcode = pthread_mutex_lock(&tog_mutex);
1789 return got_error_set_errno(errcode,
1790 "pthread_mutex_lock");
1794 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1795 if (errcode && err == NULL)
1796 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1798 if (s->thread_args.repo) {
1799 got_repo_close(s->thread_args.repo);
1800 s->thread_args.repo = NULL;
1803 if (s->thread_args.graph) {
1804 got_commit_graph_close(s->thread_args.graph);
1805 s->thread_args.graph = NULL;
1811 static const struct got_error *
1812 close_log_view(struct tog_view *view)
1814 const struct got_error *err = NULL;
1815 struct tog_log_view_state *s = &view->state.log;
1817 err = stop_log_thread(s);
1818 free_commits(&s->commits);
1819 free(s->in_repo_path);
1820 s->in_repo_path = NULL;
1826 static const struct got_error *
1827 search_start_log_view(struct tog_view *view)
1829 struct tog_log_view_state *s = &view->state.log;
1831 s->matched_entry = NULL;
1832 s->search_entry = NULL;
1836 static const struct got_error *
1837 search_next_log_view(struct tog_view *view)
1839 const struct got_error *err = NULL;
1840 struct tog_log_view_state *s = &view->state.log;
1841 struct commit_queue_entry *entry;
1843 if (!view->searching) {
1844 view->search_next_done = 1;
1848 if (s->search_entry) {
1850 errcode = pthread_mutex_unlock(&tog_mutex);
1852 return got_error_set_errno(errcode,
1853 "pthread_mutex_unlock");
1854 ch = wgetch(view->window);
1855 errcode = pthread_mutex_lock(&tog_mutex);
1857 return got_error_set_errno(errcode,
1858 "pthread_mutex_lock");
1859 if (ch == KEY_BACKSPACE) {
1860 view->search_next_done = 1;
1863 if (view->searching == TOG_SEARCH_FORWARD)
1864 entry = TAILQ_NEXT(s->search_entry, entry);
1866 entry = TAILQ_PREV(s->search_entry,
1867 commit_queue_head, entry);
1868 } else if (s->matched_entry) {
1869 if (view->searching == TOG_SEARCH_FORWARD)
1870 entry = TAILQ_NEXT(s->selected_entry, entry);
1872 entry = TAILQ_PREV(s->selected_entry,
1873 commit_queue_head, entry);
1875 if (view->searching == TOG_SEARCH_FORWARD)
1876 entry = TAILQ_FIRST(&s->commits.head);
1878 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1884 if (entry == NULL) {
1885 if (s->thread_args.log_complete ||
1886 view->searching == TOG_SEARCH_BACKWARD) {
1887 view->search_next_done = 1;
1891 * Poke the log thread for more commits and return,
1892 * allowing the main loop to make progress. Search
1893 * will resume at s->search_entry once we come back.
1895 s->thread_args.commits_needed++;
1896 return trigger_log_thread(1,
1897 &s->thread_args.commits_needed,
1898 &s->thread_args.log_complete,
1899 &s->thread_args.need_commits);
1902 err = match_commit(&have_match, entry->id, entry->commit,
1907 view->search_next_done = 1;
1908 s->matched_entry = entry;
1912 s->search_entry = entry;
1913 if (view->searching == TOG_SEARCH_FORWARD)
1914 entry = TAILQ_NEXT(entry, entry);
1916 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1919 if (s->matched_entry) {
1920 int cur = s->selected_entry->idx;
1921 while (cur < s->matched_entry->idx) {
1922 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1927 while (cur > s->matched_entry->idx) {
1928 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1935 s->search_entry = NULL;
1940 static const struct got_error *
1941 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1942 struct got_reflist_head *refs, struct got_repository *repo,
1943 const char *head_ref_name, const char *path, int check_disk)
1945 const struct got_error *err = NULL;
1946 struct tog_log_view_state *s = &view->state.log;
1947 struct got_repository *thread_repo = NULL;
1948 struct got_commit_graph *thread_graph = NULL;
1951 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1955 /* The commit queue only contains commits being displayed. */
1956 TAILQ_INIT(&s->commits.head);
1957 s->commits.ncommits = 0;
1961 s->head_ref_name = head_ref_name;
1962 s->start_id = got_object_id_dup(start_id);
1963 if (s->start_id == NULL) {
1964 err = got_error_from_errno("got_object_id_dup");
1968 view->show = show_log_view;
1969 view->input = input_log_view;
1970 view->close = close_log_view;
1971 view->search_start = search_start_log_view;
1972 view->search_next = search_next_log_view;
1974 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1977 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1982 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1984 err = got_error_set_errno(errcode, "pthread_cond_init");
1988 s->thread_args.commits_needed = view->nlines;
1989 s->thread_args.graph = thread_graph;
1990 s->thread_args.commits = &s->commits;
1991 s->thread_args.in_repo_path = s->in_repo_path;
1992 s->thread_args.start_id = s->start_id;
1993 s->thread_args.repo = thread_repo;
1994 s->thread_args.log_complete = 0;
1995 s->thread_args.quit = &s->quit;
1996 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1997 s->thread_args.selected_entry = &s->selected_entry;
1998 s->thread_args.searching = &view->searching;
1999 s->thread_args.search_next_done = &view->search_next_done;
2000 s->thread_args.regex = &view->regex;
2003 close_log_view(view);
2007 static const struct got_error *
2008 show_log_view(struct tog_view *view)
2010 struct tog_log_view_state *s = &view->state.log;
2012 if (s->thread == NULL) {
2013 int errcode = pthread_create(&s->thread, NULL, log_thread,
2016 return got_error_set_errno(errcode, "pthread_create");
2019 return draw_commits(view, &s->last_displayed_entry,
2020 &s->selected_entry, s->first_displayed_entry,
2021 &s->commits, s->selected, view->nlines, s->refs,
2022 s->in_repo_path, s->thread_args.commits_needed);
2025 static const struct got_error *
2026 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2027 struct tog_view **focus_view, struct tog_view *view, int ch)
2029 const struct got_error *err = NULL;
2030 struct tog_log_view_state *s = &view->state.log;
2031 char *parent_path, *in_repo_path = NULL;
2032 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2034 struct got_object_id *start_id;
2044 if (s->first_displayed_entry == NULL)
2046 if (s->selected > 0)
2049 scroll_up(view, &s->first_displayed_entry, 1,
2054 if (s->first_displayed_entry == NULL)
2056 if (TAILQ_FIRST(&s->commits.head) ==
2057 s->first_displayed_entry) {
2061 scroll_up(view, &s->first_displayed_entry,
2062 view->nlines, &s->commits);
2068 if (s->first_displayed_entry == NULL)
2070 if (s->selected < MIN(view->nlines - 2,
2071 s->commits.ncommits - 1)) {
2075 err = scroll_down(view, &s->first_displayed_entry, 1,
2076 &s->last_displayed_entry, &s->commits,
2077 &s->thread_args.log_complete,
2078 &s->thread_args.commits_needed,
2079 &s->thread_args.need_commits);
2083 struct commit_queue_entry *first;
2084 first = s->first_displayed_entry;
2087 err = scroll_down(view, &s->first_displayed_entry,
2088 view->nlines, &s->last_displayed_entry,
2089 &s->commits, &s->thread_args.log_complete,
2090 &s->thread_args.commits_needed,
2091 &s->thread_args.need_commits);
2094 if (first == s->first_displayed_entry &&
2095 s->selected < MIN(view->nlines - 2,
2096 s->commits.ncommits - 1)) {
2097 /* can't scroll further down */
2098 s->selected = MIN(view->nlines - 2,
2099 s->commits.ncommits - 1);
2105 if (s->selected > view->nlines - 2)
2106 s->selected = view->nlines - 2;
2107 if (s->selected > s->commits.ncommits - 1)
2108 s->selected = s->commits.ncommits - 1;
2113 if (s->selected_entry == NULL)
2115 if (view_is_parent_view(view))
2116 begin_x = view_split_begin_x(view->begin_x);
2117 err = open_diff_view_for_commit(&diff_view, begin_x,
2118 s->selected_entry->commit, s->selected_entry->id,
2119 view, s->refs, s->repo);
2122 if (view_is_parent_view(view)) {
2123 err = view_close_child(view);
2126 err = view_set_child(view, diff_view);
2128 view_close(diff_view);
2131 *focus_view = diff_view;
2132 view->child_focussed = 1;
2134 *new_view = diff_view;
2137 if (s->selected_entry == NULL)
2139 if (view_is_parent_view(view))
2140 begin_x = view_split_begin_x(view->begin_x);
2141 err = browse_commit_tree(&tree_view, begin_x,
2142 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2145 if (view_is_parent_view(view)) {
2146 err = view_close_child(view);
2149 err = view_set_child(view, tree_view);
2151 view_close(tree_view);
2154 *focus_view = tree_view;
2155 view->child_focussed = 1;
2157 *new_view = tree_view;
2160 if (strcmp(s->in_repo_path, "/") == 0)
2162 parent_path = dirname(s->in_repo_path);
2163 if (parent_path && strcmp(parent_path, ".") != 0) {
2164 err = stop_log_thread(s);
2167 lv = view_open(view->nlines, view->ncols,
2168 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2170 return got_error_from_errno(
2172 err = open_log_view(lv, s->start_id, s->refs,
2173 s->repo, s->head_ref_name, parent_path, 0);
2176 if (view_is_parent_view(view))
2179 view_set_child(view->parent, lv);
2186 err = stop_log_thread(s);
2189 lv = view_open(view->nlines, view->ncols,
2190 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2192 return got_error_from_errno("view_open");
2193 err = get_head_commit_id(&start_id, s->head_ref_name ?
2194 s->head_ref_name : GOT_REF_HEAD, s->repo);
2199 in_repo_path = strdup(s->in_repo_path);
2200 if (in_repo_path == NULL) {
2203 return got_error_from_errno("strdup");
2205 got_ref_list_free(s->refs);
2206 err = got_ref_list(s->refs, s->repo, NULL,
2207 got_ref_cmp_by_name, NULL);
2213 err = open_log_view(lv, start_id, s->refs, s->repo,
2214 s->head_ref_name, in_repo_path, 0);
2230 static const struct got_error *
2231 apply_unveil(const char *repo_path, const char *worktree_path)
2233 const struct got_error *error;
2236 if (unveil("gmon.out", "rwc") != 0)
2237 return got_error_from_errno2("unveil", "gmon.out");
2239 if (repo_path && unveil(repo_path, "r") != 0)
2240 return got_error_from_errno2("unveil", repo_path);
2242 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2243 return got_error_from_errno2("unveil", worktree_path);
2245 if (unveil("/tmp", "rwc") != 0)
2246 return got_error_from_errno2("unveil", "/tmp");
2248 error = got_privsep_unveil_exec_helpers();
2252 if (unveil(NULL, NULL) != 0)
2253 return got_error_from_errno("unveil");
2263 halfdelay(1); /* Do fast refresh while initial view is loading. */
2266 intrflush(stdscr, FALSE);
2267 keypad(stdscr, TRUE);
2269 if (getenv("TOG_COLORS") != NULL) {
2271 use_default_colors();
2273 signal(SIGWINCH, tog_sigwinch);
2274 signal(SIGPIPE, tog_sigpipe);
2277 static const struct got_error *
2278 cmd_log(int argc, char *argv[])
2280 const struct got_error *error;
2281 struct got_repository *repo = NULL;
2282 struct got_worktree *worktree = NULL;
2283 struct got_reflist_head refs;
2284 struct got_object_id *start_id = NULL;
2285 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2286 char *start_commit = NULL, *head_ref_name = NULL;
2288 struct tog_view *view;
2290 SIMPLEQ_INIT(&refs);
2293 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2301 start_commit = optarg;
2304 repo_path = realpath(optarg, NULL);
2305 if (repo_path == NULL)
2306 return got_error_from_errno2("realpath",
2318 cwd = getcwd(NULL, 0);
2320 error = got_error_from_errno("getcwd");
2323 error = got_worktree_open(&worktree, cwd);
2324 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2331 error = got_error_from_errno("strdup");
2334 } else if (argc == 1) {
2336 error = got_worktree_resolve_path(&path, worktree,
2341 path = strdup(argv[0]);
2343 error = got_error_from_errno("strdup");
2350 if (repo_path == NULL) {
2353 got_worktree_get_repo_path(worktree));
2355 repo_path = strdup(cwd);
2357 if (repo_path == NULL) {
2358 error = got_error_from_errno("strdup");
2364 error = got_repo_open(&repo, repo_path, NULL);
2368 error = apply_unveil(got_repo_get_path(repo),
2369 worktree ? got_worktree_get_root_path(worktree) : NULL);
2373 if (start_commit == NULL)
2374 error = get_head_commit_id(&start_id, worktree ?
2375 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2378 error = get_head_commit_id(&start_id, start_commit, repo);
2380 if (error->code != GOT_ERR_NOT_REF)
2382 error = got_repo_match_object_id_prefix(&start_id,
2383 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2389 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2393 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2395 error = got_error_from_errno("view_open");
2399 head_ref_name = strdup(
2400 got_worktree_get_head_ref_name(worktree));
2401 if (head_ref_name == NULL) {
2402 error = got_error_from_errno("strdup");
2406 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2411 /* Release work tree lock. */
2412 got_worktree_close(worktree);
2415 error = view_loop(view);
2421 free(head_ref_name);
2423 got_repo_close(repo);
2425 got_worktree_close(worktree);
2426 got_ref_list_free(&refs);
2434 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2440 parse_next_line(FILE *f, size_t *len)
2445 const char delim[3] = { '\0', '\0', '\0'};
2447 line = fparseln(f, &linelen, &lineno, delim, 0);
2454 match_line(const char *line, regex_t *regex)
2456 regmatch_t regmatch;
2458 return regexec(regex, line, 1, ®match, 0) == 0;
2461 struct tog_line_color *
2462 match_line_color(struct tog_line_colors *colors, const char *line)
2464 struct tog_line_color *tc = NULL;
2466 SIMPLEQ_FOREACH(tc, colors, entry) {
2467 if (match_line(line, &tc->regex))
2474 static const struct got_error *
2475 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2476 int *last_displayed_line, int *eof, int max_lines, char *header,
2477 struct tog_line_colors *colors)
2479 const struct got_error *err;
2480 int nlines = 0, nprinted = 0;
2482 struct tog_line_color *lc;
2488 werase(view->window);
2491 err = format_line(&wline, &width, header, view->ncols, 0);
2496 if (view_needs_focus_indication(view))
2497 wstandout(view->window);
2498 waddwstr(view->window, wline);
2499 if (view_needs_focus_indication(view))
2500 wstandend(view->window);
2501 if (width <= view->ncols - 1)
2502 waddch(view->window, '\n');
2510 while (nprinted < max_lines) {
2511 line = parse_next_line(f, &len);
2516 if (++nlines < *first_displayed_line) {
2521 err = format_line(&wline, &width, line, view->ncols, 0);
2527 lc = match_line_color(colors, line);
2529 wattr_on(view->window,
2530 COLOR_PAIR(lc->colorpair), NULL);
2531 waddwstr(view->window, wline);
2533 wattr_off(view->window,
2534 COLOR_PAIR(lc->colorpair), NULL);
2535 if (width <= view->ncols - 1)
2536 waddch(view->window, '\n');
2537 if (++nprinted == 1)
2538 *first_displayed_line = nlines;
2543 *last_displayed_line = nlines;
2548 while (nprinted < view->nlines) {
2549 waddch(view->window, '\n');
2553 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2558 wstandout(view->window);
2559 waddwstr(view->window, wline);
2560 wstandend(view->window);
2567 get_datestr(time_t *time, char *datebuf)
2569 struct tm mytm, *tm;
2572 tm = gmtime_r(time, &mytm);
2575 s = asctime_r(tm, datebuf);
2578 p = strchr(s, '\n');
2584 static const struct got_error *
2585 write_commit_info(struct got_object_id *commit_id,
2586 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2588 const struct got_error *err = NULL;
2589 char datebuf[26], *datestr;
2590 struct got_commit_object *commit;
2591 char *id_str = NULL, *logmsg = NULL;
2592 time_t committer_time;
2593 const char *author, *committer;
2594 char *refs_str = NULL;
2597 err = build_refs_str(&refs_str, refs, commit_id, repo);
2602 err = got_object_open_as_commit(&commit, repo, commit_id);
2606 err = got_object_id_str(&id_str, commit_id);
2608 err = got_error_from_errno("got_object_id_str");
2612 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2613 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2614 err = got_error_from_errno("fprintf");
2617 if (fprintf(outfile, "from: %s\n",
2618 got_object_commit_get_author(commit)) < 0) {
2619 err = got_error_from_errno("fprintf");
2622 committer_time = got_object_commit_get_committer_time(commit);
2623 datestr = get_datestr(&committer_time, datebuf);
2624 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2625 err = got_error_from_errno("fprintf");
2628 author = got_object_commit_get_author(commit);
2629 committer = got_object_commit_get_committer(commit);
2630 if (strcmp(author, committer) != 0 &&
2631 fprintf(outfile, "via: %s\n", committer) < 0) {
2632 err = got_error_from_errno("fprintf");
2635 err = got_object_commit_get_logmsg(&logmsg, commit);
2638 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2639 err = got_error_from_errno("fprintf");
2646 got_object_commit_close(commit);
2650 static const struct got_error *
2651 create_diff(struct tog_diff_view_state *s)
2653 const struct got_error *err = NULL;
2659 err = got_error_from_errno("got_opentemp");
2662 if (s->f && fclose(s->f) != 0) {
2663 err = got_error_from_errno("fclose");
2669 err = got_object_get_type(&obj_type, s->repo, s->id1);
2671 err = got_object_get_type(&obj_type, s->repo, s->id2);
2676 case GOT_OBJ_TYPE_BLOB:
2677 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2678 s->diff_context, 0, s->repo, f);
2680 case GOT_OBJ_TYPE_TREE:
2681 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2682 s->diff_context, 0, s->repo, f);
2684 case GOT_OBJ_TYPE_COMMIT: {
2685 const struct got_object_id_queue *parent_ids;
2686 struct got_object_qid *pid;
2687 struct got_commit_object *commit2;
2689 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2692 /* Show commit info if we're diffing to a parent/root commit. */
2694 write_commit_info(s->id2, s->refs, s->repo, f);
2696 parent_ids = got_object_commit_get_parent_ids(commit2);
2697 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2698 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2699 write_commit_info(s->id2, s->refs,
2705 got_object_commit_close(commit2);
2707 err = got_diff_objects_as_commits(s->id1, s->id2,
2708 s->diff_context, 0, s->repo, f);
2712 err = got_error(GOT_ERR_OBJ_TYPE);
2716 if (f && fflush(f) != 0 && err == NULL)
2717 err = got_error_from_errno("fflush");
2722 diff_view_indicate_progress(struct tog_view *view)
2724 mvwaddstr(view->window, 0, 0, "diffing...");
2729 static const struct got_error *
2730 add_line_color(struct tog_line_colors *colors, const char *pattern,
2731 int idx, short color)
2733 const struct got_error *err = NULL;
2734 struct tog_line_color *lc;
2737 init_pair(idx, color, -1);
2739 lc = calloc(1, sizeof(*lc));
2741 return got_error_from_errno("calloc");
2742 regerr = regcomp(&lc->regex, pattern,
2743 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
2745 static char regerr_msg[512];
2746 static char err_msg[512];
2747 regerror(regerr, &lc->regex, regerr_msg,
2748 sizeof(regerr_msg));
2749 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
2751 err = got_error_msg(GOT_ERR_REGEX, err_msg);
2755 lc->colorpair = idx;
2756 SIMPLEQ_INSERT_HEAD(colors, lc, entry);
2761 free_line_colors(struct tog_line_colors *colors)
2763 struct tog_line_color *lc;
2765 while (!SIMPLEQ_EMPTY(colors)) {
2766 lc = SIMPLEQ_FIRST(colors);
2767 SIMPLEQ_REMOVE_HEAD(colors, entry);
2768 regfree(&lc->regex);
2774 default_color_value(const char *envvar)
2776 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
2777 return COLOR_MAGENTA;
2778 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
2780 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
2781 return COLOR_YELLOW;
2782 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
2784 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
2785 return COLOR_MAGENTA;
2786 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
2788 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
2790 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
2797 get_color_value(const char *envvar)
2799 const char *val = getenv(envvar);
2802 return default_color_value(envvar);
2804 if (strcasecmp(val, "black") == 0)
2806 if (strcasecmp(val, "red") == 0)
2808 if (strcasecmp(val, "green") == 0)
2810 if (strcasecmp(val, "yellow") == 0)
2811 return COLOR_YELLOW;
2812 if (strcasecmp(val, "blue") == 0)
2814 if (strcasecmp(val, "magenta") == 0)
2815 return COLOR_MAGENTA;
2816 if (strcasecmp(val, "cyan") == 0)
2818 if (strcasecmp(val, "white") == 0)
2821 return default_color_value(envvar);
2824 static const struct got_error *
2825 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2826 struct got_object_id *id2, struct tog_view *log_view,
2827 struct got_reflist_head *refs, struct got_repository *repo)
2829 const struct got_error *err;
2831 if (id1 != NULL && id2 != NULL) {
2833 err = got_object_get_type(&type1, repo, id1);
2836 err = got_object_get_type(&type2, repo, id2);
2841 return got_error(GOT_ERR_OBJ_TYPE);
2845 view->state.diff.id1 = got_object_id_dup(id1);
2846 if (view->state.diff.id1 == NULL)
2847 return got_error_from_errno("got_object_id_dup");
2849 view->state.diff.id1 = NULL;
2851 view->state.diff.id2 = got_object_id_dup(id2);
2852 if (view->state.diff.id2 == NULL) {
2853 free(view->state.diff.id1);
2854 view->state.diff.id1 = NULL;
2855 return got_error_from_errno("got_object_id_dup");
2857 view->state.diff.f = NULL;
2858 view->state.diff.first_displayed_line = 1;
2859 view->state.diff.last_displayed_line = view->nlines;
2860 view->state.diff.diff_context = 3;
2861 view->state.diff.log_view = log_view;
2862 view->state.diff.repo = repo;
2863 view->state.diff.refs = refs;
2864 SIMPLEQ_INIT(&view->state.diff.line_colors);
2866 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2867 err = add_line_color(&view->state.diff.line_colors,
2868 "^-", 1, get_color_value("TOG_COLOR_DIFF_MINUS"));
2871 err = add_line_color(&view->state.diff.line_colors,
2872 "^\\+", 2, get_color_value("TOG_COLOR_DIFF_PLUS"));
2874 free_line_colors(&view->state.diff.line_colors);
2877 err = add_line_color(&view->state.diff.line_colors,
2879 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2881 free_line_colors(&view->state.diff.line_colors);
2885 err = add_line_color(&view->state.diff.line_colors,
2886 "^(commit|(blob|file) [-+] )", 4,
2887 get_color_value("TOG_COLOR_DIFF_META"));
2889 free_line_colors(&view->state.diff.line_colors);
2894 if (log_view && view_is_splitscreen(view))
2895 show_log_view(log_view); /* draw vborder */
2896 diff_view_indicate_progress(view);
2898 err = create_diff(&view->state.diff);
2900 free(view->state.diff.id1);
2901 view->state.diff.id1 = NULL;
2902 free(view->state.diff.id2);
2903 view->state.diff.id2 = NULL;
2907 view->show = show_diff_view;
2908 view->input = input_diff_view;
2909 view->close = close_diff_view;
2914 static const struct got_error *
2915 close_diff_view(struct tog_view *view)
2917 const struct got_error *err = NULL;
2919 free(view->state.diff.id1);
2920 view->state.diff.id1 = NULL;
2921 free(view->state.diff.id2);
2922 view->state.diff.id2 = NULL;
2923 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2924 err = got_error_from_errno("fclose");
2925 free_line_colors(&view->state.diff.line_colors);
2929 static const struct got_error *
2930 show_diff_view(struct tog_view *view)
2932 const struct got_error *err;
2933 struct tog_diff_view_state *s = &view->state.diff;
2934 char *id_str1 = NULL, *id_str2, *header;
2937 err = got_object_id_str(&id_str1, s->id1);
2941 err = got_object_id_str(&id_str2, s->id2);
2945 if (asprintf(&header, "diff %s %s",
2946 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2947 err = got_error_from_errno("asprintf");
2955 return draw_file(view, s->f, &s->first_displayed_line,
2956 &s->last_displayed_line, &s->eof, view->nlines,
2957 header, &s->line_colors);
2960 static const struct got_error *
2961 set_selected_commit(struct tog_diff_view_state *s,
2962 struct commit_queue_entry *entry)
2964 const struct got_error *err;
2965 const struct got_object_id_queue *parent_ids;
2966 struct got_commit_object *selected_commit;
2967 struct got_object_qid *pid;
2970 s->id2 = got_object_id_dup(entry->id);
2972 return got_error_from_errno("got_object_id_dup");
2974 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2977 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2979 pid = SIMPLEQ_FIRST(parent_ids);
2980 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2981 got_object_commit_close(selected_commit);
2985 static const struct got_error *
2986 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2987 struct tog_view **focus_view, struct tog_view *view, int ch)
2989 const struct got_error *err = NULL;
2990 struct tog_diff_view_state *s = &view->state.diff;
2991 struct tog_log_view_state *ls;
2992 struct commit_queue_entry *entry;
2998 if (s->first_displayed_line > 1)
2999 s->first_displayed_line--;
3003 if (s->first_displayed_line == 1)
3006 while (i++ < view->nlines - 1 &&
3007 s->first_displayed_line > 1)
3008 s->first_displayed_line--;
3013 s->first_displayed_line++;
3021 while (!s->eof && i++ < view->nlines - 1) {
3023 line = parse_next_line(s->f, NULL);
3024 s->first_displayed_line++;
3030 if (s->diff_context > 0) {
3032 diff_view_indicate_progress(view);
3033 err = create_diff(s);
3037 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3039 diff_view_indicate_progress(view);
3040 err = create_diff(s);
3045 if (s->log_view == NULL)
3047 ls = &s->log_view->state.log;
3048 entry = TAILQ_PREV(ls->selected_entry,
3049 commit_queue_head, entry);
3053 err = input_log_view(NULL, NULL, NULL, s->log_view,
3058 err = set_selected_commit(s, entry);
3062 s->first_displayed_line = 1;
3063 s->last_displayed_line = view->nlines;
3065 diff_view_indicate_progress(view);
3066 err = create_diff(s);
3070 if (s->log_view == NULL)
3072 ls = &s->log_view->state.log;
3074 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3075 ls->thread_args.commits_needed++;
3077 /* Display "loading..." in log view. */
3078 show_log_view(s->log_view);
3082 err = trigger_log_thread(1 /* load_all */,
3083 &ls->thread_args.commits_needed,
3084 &ls->thread_args.log_complete,
3085 &ls->thread_args.need_commits);
3089 err = input_log_view(NULL, NULL, NULL, s->log_view,
3094 entry = TAILQ_NEXT(ls->selected_entry, entry);
3098 err = set_selected_commit(s, entry);
3102 s->first_displayed_line = 1;
3103 s->last_displayed_line = view->nlines;
3105 diff_view_indicate_progress(view);
3106 err = create_diff(s);
3115 static const struct got_error *
3116 cmd_diff(int argc, char *argv[])
3118 const struct got_error *error = NULL;
3119 struct got_repository *repo = NULL;
3120 struct got_reflist_head refs;
3121 struct got_object_id *id1 = NULL, *id2 = NULL;
3122 char *repo_path = NULL;
3123 char *id_str1 = NULL, *id_str2 = NULL;
3125 struct tog_view *view;
3127 SIMPLEQ_INIT(&refs);
3130 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3135 while ((ch = getopt(argc, argv, "")) != -1) {
3147 usage_diff(); /* TODO show local worktree changes */
3148 } else if (argc == 2) {
3149 repo_path = getcwd(NULL, 0);
3150 if (repo_path == NULL)
3151 return got_error_from_errno("getcwd");
3154 } else if (argc == 3) {
3155 repo_path = realpath(argv[0], NULL);
3156 if (repo_path == NULL)
3157 return got_error_from_errno2("realpath", argv[0]);
3165 error = got_repo_open(&repo, repo_path, NULL);
3169 error = apply_unveil(got_repo_get_path(repo), NULL);
3173 error = got_repo_match_object_id_prefix(&id1, id_str1,
3174 GOT_OBJ_TYPE_ANY, repo);
3178 error = got_repo_match_object_id_prefix(&id2, id_str2,
3179 GOT_OBJ_TYPE_ANY, repo);
3183 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3187 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3189 error = got_error_from_errno("view_open");
3192 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3195 error = view_loop(view);
3199 got_repo_close(repo);
3200 got_ref_list_free(&refs);
3208 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3213 struct tog_blame_line {
3215 struct got_object_id *id;
3218 static const struct got_error *
3219 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3220 const char *path, struct tog_blame_line *lines, int nlines,
3221 int blame_complete, int selected_line, int *first_displayed_line,
3222 int *last_displayed_line, int *eof, int max_lines)
3224 const struct got_error *err;
3225 int lineno = 0, nprinted = 0;
3230 struct tog_blame_line *blame_line;
3231 struct got_object_id *prev_id = NULL;
3234 err = got_object_id_str(&id_str, id);
3239 werase(view->window);
3241 if (asprintf(&line, "commit %s", id_str) == -1) {
3242 err = got_error_from_errno("asprintf");
3247 err = format_line(&wline, &width, line, view->ncols, 0);
3252 if (view_needs_focus_indication(view))
3253 wstandout(view->window);
3254 waddwstr(view->window, wline);
3255 if (view_needs_focus_indication(view))
3256 wstandend(view->window);
3259 if (width < view->ncols - 1)
3260 waddch(view->window, '\n');
3262 if (asprintf(&line, "[%d/%d] %s%s",
3263 *first_displayed_line - 1 + selected_line, nlines,
3264 blame_complete ? "" : "annotating... ", path) == -1) {
3266 return got_error_from_errno("asprintf");
3269 err = format_line(&wline, &width, line, view->ncols, 0);
3274 waddwstr(view->window, wline);
3277 if (width < view->ncols - 1)
3278 waddch(view->window, '\n');
3281 while (nprinted < max_lines - 2) {
3282 line = parse_next_line(f, &len);
3287 if (++lineno < *first_displayed_line) {
3292 if (view->ncols <= 9) {
3294 wline = wcsdup(L"");
3296 err = got_error_from_errno("wcsdup");
3298 err = format_line(&wline, &width, line,
3299 view->ncols - 9, 9);
3307 if (view->focussed && nprinted == selected_line - 1)
3308 wstandout(view->window);
3311 blame_line = &lines[lineno - 1];
3312 if (blame_line->annotated && prev_id &&
3313 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3315 nprinted == selected_line - 1)) {
3316 waddstr(view->window, " ");
3317 } else if (blame_line->annotated) {
3319 err = got_object_id_str(&id_str, blame_line->id);
3325 wprintw(view->window, "%.8s", id_str);
3327 prev_id = blame_line->id;
3329 waddstr(view->window, "........");
3333 waddstr(view->window, "........");
3337 if (view->focussed && nprinted == selected_line - 1)
3338 wstandend(view->window);
3339 waddstr(view->window, " ");
3341 waddwstr(view->window, wline);
3342 if (width <= view->ncols - 1)
3343 waddch(view->window, '\n');
3344 if (++nprinted == 1)
3345 *first_displayed_line = lineno;
3350 *last_displayed_line = lineno;
3357 static const struct got_error *
3358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3360 const struct got_error *err = NULL;
3361 struct tog_blame_cb_args *a = arg;
3362 struct tog_blame_line *line;
3365 if (nlines != a->nlines ||
3366 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3367 return got_error(GOT_ERR_RANGE);
3369 errcode = pthread_mutex_lock(&tog_mutex);
3371 return got_error_set_errno(errcode, "pthread_mutex_lock");
3373 if (*a->quit) { /* user has quit the blame view */
3374 err = got_error(GOT_ERR_ITER_COMPLETED);
3379 goto done; /* no change in this commit */
3381 line = &a->lines[lineno - 1];
3382 if (line->annotated)
3385 line->id = got_object_id_dup(id);
3386 if (line->id == NULL) {
3387 err = got_error_from_errno("got_object_id_dup");
3390 line->annotated = 1;
3392 errcode = pthread_mutex_unlock(&tog_mutex);
3394 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3399 blame_thread(void *arg)
3401 const struct got_error *err;
3402 struct tog_blame_thread_args *ta = arg;
3403 struct tog_blame_cb_args *a = ta->cb_args;
3406 err = got_blame(ta->path, a->commit_id, ta->repo,
3407 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3408 if (err && err->code == GOT_ERR_CANCELLED)
3411 errcode = pthread_mutex_lock(&tog_mutex);
3413 return (void *)got_error_set_errno(errcode,
3414 "pthread_mutex_lock");
3416 got_repo_close(ta->repo);
3420 errcode = pthread_mutex_unlock(&tog_mutex);
3421 if (errcode && err == NULL)
3422 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3427 static struct got_object_id *
3428 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3429 int first_displayed_line, int selected_line)
3431 struct tog_blame_line *line;
3436 line = &lines[first_displayed_line - 1 + selected_line - 1];
3437 if (!line->annotated)
3443 static const struct got_error *
3444 stop_blame(struct tog_blame *blame)
3446 const struct got_error *err = NULL;
3449 if (blame->thread) {
3451 errcode = pthread_mutex_unlock(&tog_mutex);
3453 return got_error_set_errno(errcode,
3454 "pthread_mutex_unlock");
3455 errcode = pthread_join(blame->thread, (void **)&err);
3457 return got_error_set_errno(errcode, "pthread_join");
3458 errcode = pthread_mutex_lock(&tog_mutex);
3460 return got_error_set_errno(errcode,
3461 "pthread_mutex_lock");
3462 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3464 blame->thread = NULL;
3466 if (blame->thread_args.repo) {
3467 got_repo_close(blame->thread_args.repo);
3468 blame->thread_args.repo = NULL;
3471 if (fclose(blame->f) != 0 && err == NULL)
3472 err = got_error_from_errno("fclose");
3476 for (i = 0; i < blame->nlines; i++)
3477 free(blame->lines[i].id);
3479 blame->lines = NULL;
3481 free(blame->cb_args.commit_id);
3482 blame->cb_args.commit_id = NULL;
3487 static const struct got_error *
3488 cancel_blame_view(void *arg)
3490 const struct got_error *err = NULL;
3494 errcode = pthread_mutex_lock(&tog_mutex);
3496 return got_error_set_errno(errcode,
3497 "pthread_mutex_unlock");
3500 err = got_error(GOT_ERR_CANCELLED);
3502 errcode = pthread_mutex_unlock(&tog_mutex);
3504 return got_error_set_errno(errcode,
3505 "pthread_mutex_lock");
3510 static const struct got_error *
3511 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3512 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3513 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3514 struct got_repository *repo)
3516 const struct got_error *err = NULL;
3517 struct got_blob_object *blob = NULL;
3518 struct got_repository *thread_repo = NULL;
3519 struct got_object_id *obj_id = NULL;
3522 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3526 return got_error(GOT_ERR_NO_OBJ);
3528 err = got_object_get_type(&obj_type, repo, obj_id);
3532 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3533 err = got_error(GOT_ERR_OBJ_TYPE);
3537 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3540 blame->f = got_opentemp();
3541 if (blame->f == NULL) {
3542 err = got_error_from_errno("got_opentemp");
3545 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3546 &blame->line_offsets, blame->f, blob);
3547 if (err || blame->nlines == 0)
3550 /* Don't include \n at EOF in the blame line count. */
3551 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3554 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3555 if (blame->lines == NULL) {
3556 err = got_error_from_errno("calloc");
3560 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3564 blame->cb_args.view = view;
3565 blame->cb_args.lines = blame->lines;
3566 blame->cb_args.nlines = blame->nlines;
3567 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3568 if (blame->cb_args.commit_id == NULL) {
3569 err = got_error_from_errno("got_object_id_dup");
3572 blame->cb_args.quit = done;
3574 blame->thread_args.path = path;
3575 blame->thread_args.repo = thread_repo;
3576 blame->thread_args.cb_args = &blame->cb_args;
3577 blame->thread_args.complete = blame_complete;
3578 blame->thread_args.cancel_cb = cancel_blame_view;
3579 blame->thread_args.cancel_arg = done;
3580 *blame_complete = 0;
3584 got_object_blob_close(blob);
3591 static const struct got_error *
3592 open_blame_view(struct tog_view *view, char *path,
3593 struct got_object_id *commit_id, struct got_reflist_head *refs,
3594 struct got_repository *repo)
3596 const struct got_error *err = NULL;
3597 struct tog_blame_view_state *s = &view->state.blame;
3599 SIMPLEQ_INIT(&s->blamed_commits);
3601 s->path = strdup(path);
3602 if (s->path == NULL)
3603 return got_error_from_errno("strdup");
3605 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3611 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3612 s->first_displayed_line = 1;
3613 s->last_displayed_line = view->nlines;
3614 s->selected_line = 1;
3615 s->blame_complete = 0;
3618 s->commit_id = commit_id;
3619 memset(&s->blame, 0, sizeof(s->blame));
3621 view->show = show_blame_view;
3622 view->input = input_blame_view;
3623 view->close = close_blame_view;
3624 view->search_start = search_start_blame_view;
3625 view->search_next = search_next_blame_view;
3627 return run_blame(&s->blame, view, &s->blame_complete,
3628 &s->first_displayed_line, &s->last_displayed_line,
3629 &s->selected_line, &s->done, &s->eof, s->path,
3630 s->blamed_commit->id, s->repo);
3633 static const struct got_error *
3634 close_blame_view(struct tog_view *view)
3636 const struct got_error *err = NULL;
3637 struct tog_blame_view_state *s = &view->state.blame;
3639 if (s->blame.thread)
3640 err = stop_blame(&s->blame);
3642 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3643 struct got_object_qid *blamed_commit;
3644 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3645 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3646 got_object_qid_free(blamed_commit);
3654 static const struct got_error *
3655 search_start_blame_view(struct tog_view *view)
3657 struct tog_blame_view_state *s = &view->state.blame;
3659 s->matched_line = 0;
3663 static const struct got_error *
3664 search_next_blame_view(struct tog_view *view)
3666 struct tog_blame_view_state *s = &view->state.blame;
3669 if (!view->searching) {
3670 view->search_next_done = 1;
3674 if (s->matched_line) {
3675 if (view->searching == TOG_SEARCH_FORWARD)
3676 lineno = s->matched_line + 1;
3678 lineno = s->matched_line - 1;
3680 if (view->searching == TOG_SEARCH_FORWARD)
3683 lineno = s->blame.nlines;
3691 if (lineno <= 0 || lineno > s->blame.nlines) {
3692 if (s->matched_line == 0) {
3693 view->search_next_done = 1;
3698 if (view->searching == TOG_SEARCH_FORWARD)
3701 lineno = s->blame.nlines;
3704 offset = s->blame.line_offsets[lineno - 1];
3705 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3707 return got_error_from_errno("fseeko");
3710 line = parse_next_line(s->blame.f, &len);
3711 if (line && match_line(line, &view->regex)) {
3712 view->search_next_done = 1;
3713 s->matched_line = lineno;
3718 if (view->searching == TOG_SEARCH_FORWARD)
3724 if (s->matched_line) {
3725 s->first_displayed_line = s->matched_line;
3726 s->selected_line = 1;
3732 static const struct got_error *
3733 show_blame_view(struct tog_view *view)
3735 const struct got_error *err = NULL;
3736 struct tog_blame_view_state *s = &view->state.blame;
3739 if (s->blame.thread == NULL) {
3740 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3741 &s->blame.thread_args);
3743 return got_error_set_errno(errcode, "pthread_create");
3745 halfdelay(1); /* fast refresh while annotating */
3748 if (s->blame_complete)
3749 halfdelay(10); /* disable fast refresh */
3751 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3752 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3753 s->selected_line, &s->first_displayed_line,
3754 &s->last_displayed_line, &s->eof, view->nlines);
3760 static const struct got_error *
3761 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3762 struct tog_view **focus_view, struct tog_view *view, int ch)
3764 const struct got_error *err = NULL, *thread_err = NULL;
3765 struct tog_view *diff_view;
3766 struct tog_blame_view_state *s = &view->state.blame;
3775 if (s->selected_line > 1)
3777 else if (s->selected_line == 1 &&
3778 s->first_displayed_line > 1)
3779 s->first_displayed_line--;
3782 if (s->first_displayed_line == 1) {
3783 s->selected_line = 1;
3786 if (s->first_displayed_line > view->nlines - 2)
3787 s->first_displayed_line -=
3790 s->first_displayed_line = 1;
3794 if (s->selected_line < view->nlines - 2 &&
3795 s->first_displayed_line +
3796 s->selected_line <= s->blame.nlines)
3798 else if (s->last_displayed_line <
3800 s->first_displayed_line++;
3804 struct got_object_id *id = NULL;
3805 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3806 s->first_displayed_line, s->selected_line);
3810 struct got_commit_object *commit;
3811 struct got_object_qid *pid;
3812 struct got_object_id *blob_id = NULL;
3814 err = got_object_open_as_commit(&commit,
3818 pid = SIMPLEQ_FIRST(
3819 got_object_commit_get_parent_ids(commit));
3821 got_object_commit_close(commit);
3824 /* Check if path history ends here. */
3825 err = got_object_id_by_path(&blob_id, s->repo,
3828 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3830 got_object_commit_close(commit);
3833 err = got_object_get_type(&obj_type, s->repo,
3836 /* Can't blame non-blob type objects. */
3837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3838 got_object_commit_close(commit);
3841 err = got_object_qid_alloc(&s->blamed_commit,
3843 got_object_commit_close(commit);
3845 if (got_object_id_cmp(id,
3846 s->blamed_commit->id) == 0)
3848 err = got_object_qid_alloc(&s->blamed_commit,
3854 thread_err = stop_blame(&s->blame);
3858 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3859 s->blamed_commit, entry);
3860 err = run_blame(&s->blame, view, &s->blame_complete,
3861 &s->first_displayed_line, &s->last_displayed_line,
3862 &s->selected_line, &s->done, &s->eof,
3863 s->path, s->blamed_commit->id, s->repo);
3869 struct got_object_qid *first;
3870 first = SIMPLEQ_FIRST(&s->blamed_commits);
3871 if (!got_object_id_cmp(first->id, s->commit_id))
3874 thread_err = stop_blame(&s->blame);
3878 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3879 got_object_qid_free(s->blamed_commit);
3881 SIMPLEQ_FIRST(&s->blamed_commits);
3882 err = run_blame(&s->blame, view, &s->blame_complete,
3883 &s->first_displayed_line, &s->last_displayed_line,
3884 &s->selected_line, &s->done, &s->eof, s->path,
3885 s->blamed_commit->id, s->repo);
3892 struct got_object_id *id = NULL;
3893 struct got_object_qid *pid;
3894 struct got_commit_object *commit = NULL;
3895 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3896 s->first_displayed_line, s->selected_line);
3899 err = got_object_open_as_commit(&commit, s->repo, id);
3902 pid = SIMPLEQ_FIRST(
3903 got_object_commit_get_parent_ids(commit));
3904 if (view_is_parent_view(view))
3905 begin_x = view_split_begin_x(view->begin_x);
3906 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3907 if (diff_view == NULL) {
3908 got_object_commit_close(commit);
3909 err = got_error_from_errno("view_open");
3912 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3913 id, NULL, s->refs, s->repo);
3914 got_object_commit_close(commit);
3916 view_close(diff_view);
3919 if (view_is_parent_view(view)) {
3920 err = view_close_child(view);
3923 err = view_set_child(view, diff_view);
3925 view_close(diff_view);
3928 *focus_view = diff_view;
3929 view->child_focussed = 1;
3931 *new_view = diff_view;
3938 if (s->last_displayed_line >= s->blame.nlines &&
3939 s->selected_line >= MIN(s->blame.nlines,
3940 view->nlines - 2)) {
3943 if (s->last_displayed_line >= s->blame.nlines &&
3944 s->selected_line < view->nlines - 2) {
3945 s->selected_line = MIN(s->blame.nlines,
3949 if (s->last_displayed_line + view->nlines - 2
3951 s->first_displayed_line +=
3954 s->first_displayed_line =
3959 if (s->selected_line > view->nlines - 2) {
3960 s->selected_line = MIN(s->blame.nlines,
3967 return thread_err ? thread_err : err;
3970 static const struct got_error *
3971 cmd_blame(int argc, char *argv[])
3973 const struct got_error *error;
3974 struct got_repository *repo = NULL;
3975 struct got_reflist_head refs;
3976 struct got_worktree *worktree = NULL;
3977 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3978 struct got_object_id *commit_id = NULL;
3979 char *commit_id_str = NULL;
3981 struct tog_view *view;
3983 SIMPLEQ_INIT(&refs);
3986 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3991 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3994 commit_id_str = optarg;
3997 repo_path = realpath(optarg, NULL);
3998 if (repo_path == NULL)
3999 return got_error_from_errno2("realpath",
4016 cwd = getcwd(NULL, 0);
4018 error = got_error_from_errno("getcwd");
4021 if (repo_path == NULL) {
4022 error = got_worktree_open(&worktree, cwd);
4023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4029 strdup(got_worktree_get_repo_path(worktree));
4030 if (repo_path == NULL)
4031 error = got_error_from_errno("strdup");
4035 repo_path = strdup(cwd);
4036 if (repo_path == NULL) {
4037 error = got_error_from_errno("strdup");
4045 error = got_repo_open(&repo, repo_path, NULL);
4049 error = apply_unveil(got_repo_get_path(repo), NULL);
4054 const char *prefix = got_worktree_get_path_prefix(worktree);
4055 char *p, *worktree_subdir = cwd +
4056 strlen(got_worktree_get_root_path(worktree));
4057 if (asprintf(&p, "%s%s%s%s%s",
4058 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4059 worktree_subdir, worktree_subdir[0] ? "/" : "",
4061 error = got_error_from_errno("asprintf");
4064 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4067 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4072 if (commit_id_str == NULL) {
4073 struct got_reference *head_ref;
4074 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4077 error = got_ref_resolve(&commit_id, repo, head_ref);
4078 got_ref_close(head_ref);
4080 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4082 if (error->code != GOT_ERR_NOT_REF)
4084 error = got_repo_match_object_id_prefix(&commit_id,
4085 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4091 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4095 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4097 error = got_error_from_errno("view_open");
4100 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4104 /* Release work tree lock. */
4105 got_worktree_close(worktree);
4108 error = view_loop(view);
4114 got_worktree_close(worktree);
4116 got_repo_close(repo);
4117 got_ref_list_free(&refs);
4121 static const struct got_error *
4122 draw_tree_entries(struct tog_view *view,
4123 struct got_tree_entry **first_displayed_entry,
4124 struct got_tree_entry **last_displayed_entry,
4125 struct got_tree_entry **selected_entry, int *ndisplayed,
4126 const char *label, int show_ids, const char *parent_path,
4127 const struct got_tree_entries *entries, int selected, int limit,
4128 int isroot, struct tog_line_colors *colors)
4130 const struct got_error *err = NULL;
4131 struct got_tree_entry *te;
4133 struct tog_line_color *lc;
4138 werase(view->window);
4143 err = format_line(&wline, &width, label, view->ncols, 0);
4146 if (view_needs_focus_indication(view))
4147 wstandout(view->window);
4148 waddwstr(view->window, wline);
4149 if (view_needs_focus_indication(view))
4150 wstandend(view->window);
4153 if (width < view->ncols - 1)
4154 waddch(view->window, '\n');
4157 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4160 waddwstr(view->window, wline);
4163 if (width < view->ncols - 1)
4164 waddch(view->window, '\n');
4167 waddch(view->window, '\n');
4171 te = SIMPLEQ_FIRST(&entries->head);
4172 if (*first_displayed_entry == NULL) {
4173 if (selected == 0) {
4175 wstandout(view->window);
4176 *selected_entry = NULL;
4178 waddstr(view->window, " ..\n"); /* parent directory */
4179 if (selected == 0 && view->focussed)
4180 wstandend(view->window);
4187 while (te != *first_displayed_entry)
4188 te = SIMPLEQ_NEXT(te, entry);
4192 char *line = NULL, *id_str = NULL;
4193 const char *modestr = "";
4196 err = got_object_id_str(&id_str, te->id);
4198 return got_error_from_errno(
4199 "got_object_id_str");
4201 if (got_object_tree_entry_is_submodule(te))
4203 else if (S_ISLNK(te->mode))
4205 else if (S_ISDIR(te->mode))
4207 else if (te->mode & S_IXUSR)
4209 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4210 te->name, modestr) == -1) {
4212 return got_error_from_errno("asprintf");
4215 err = format_line(&wline, &width, line, view->ncols, 0);
4220 if (n == selected) {
4222 wstandout(view->window);
4223 *selected_entry = te;
4225 lc = match_line_color(colors, line);
4227 wattr_on(view->window,
4228 COLOR_PAIR(lc->colorpair), NULL);
4229 waddwstr(view->window, wline);
4231 wattr_off(view->window,
4232 COLOR_PAIR(lc->colorpair), NULL);
4233 if (width < view->ncols - 1)
4234 waddch(view->window, '\n');
4235 if (n == selected && view->focussed)
4236 wstandend(view->window);
4242 *last_displayed_entry = te;
4245 te = SIMPLEQ_NEXT(te, entry);
4252 tree_scroll_up(struct tog_view *view,
4253 struct got_tree_entry **first_displayed_entry, int maxscroll,
4254 const struct got_tree_entries *entries, int isroot)
4256 struct got_tree_entry *te, *prev;
4259 if (*first_displayed_entry == NULL)
4262 te = SIMPLEQ_FIRST(&entries->head);
4263 if (*first_displayed_entry == te) {
4265 *first_displayed_entry = NULL;
4269 /* XXX this is stupid... switch to TAILQ? */
4270 for (i = 0; i < maxscroll; i++) {
4271 while (te != *first_displayed_entry) {
4273 te = SIMPLEQ_NEXT(te, entry);
4275 *first_displayed_entry = prev;
4276 te = SIMPLEQ_FIRST(&entries->head);
4278 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4279 *first_displayed_entry = NULL;
4283 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4284 struct got_tree_entry *last_displayed_entry,
4285 const struct got_tree_entries *entries)
4287 struct got_tree_entry *next, *last;
4290 if (*first_displayed_entry)
4291 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4293 next = SIMPLEQ_FIRST(&entries->head);
4294 last = last_displayed_entry;
4295 while (next && last && n++ < maxscroll) {
4296 last = SIMPLEQ_NEXT(last, entry);
4298 *first_displayed_entry = next;
4299 next = SIMPLEQ_NEXT(next, entry);
4305 static const struct got_error *
4306 tree_entry_path(char **path, struct tog_parent_trees *parents,
4307 struct got_tree_entry *te)
4309 const struct got_error *err = NULL;
4310 struct tog_parent_tree *pt;
4311 size_t len = 2; /* for leading slash and NUL */
4313 TAILQ_FOREACH(pt, parents, entry)
4314 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4316 len += strlen(te->name);
4318 *path = calloc(1, len);
4320 return got_error_from_errno("calloc");
4323 pt = TAILQ_LAST(parents, tog_parent_trees);
4325 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4326 err = got_error(GOT_ERR_NO_SPACE);
4329 if (strlcat(*path, "/", len) >= len) {
4330 err = got_error(GOT_ERR_NO_SPACE);
4333 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4336 if (strlcat(*path, te->name, len) >= len) {
4337 err = got_error(GOT_ERR_NO_SPACE);
4349 static const struct got_error *
4350 blame_tree_entry(struct tog_view **new_view, int begin_x,
4351 struct got_tree_entry *te, struct tog_parent_trees *parents,
4352 struct got_object_id *commit_id, struct got_reflist_head *refs,
4353 struct got_repository *repo)
4355 const struct got_error *err = NULL;
4357 struct tog_view *blame_view;
4361 err = tree_entry_path(&path, parents, te);
4365 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4366 if (blame_view == NULL) {
4367 err = got_error_from_errno("view_open");
4371 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4373 if (err->code == GOT_ERR_CANCELLED)
4375 view_close(blame_view);
4377 *new_view = blame_view;
4383 static const struct got_error *
4384 log_tree_entry(struct tog_view **new_view, int begin_x,
4385 struct got_tree_entry *te, struct tog_parent_trees *parents,
4386 struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 struct got_repository *repo)
4389 struct tog_view *log_view;
4390 const struct got_error *err = NULL;
4395 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4396 if (log_view == NULL)
4397 return got_error_from_errno("view_open");
4399 err = tree_entry_path(&path, parents, te);
4403 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4405 view_close(log_view);
4407 *new_view = log_view;
4412 static const struct got_error *
4413 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4414 struct got_object_id *commit_id, struct got_reflist_head *refs,
4415 struct got_repository *repo)
4417 const struct got_error *err = NULL;
4418 char *commit_id_str = NULL;
4419 struct tog_tree_view_state *s = &view->state.tree;
4421 TAILQ_INIT(&s->parents);
4423 err = got_object_id_str(&commit_id_str, commit_id);
4427 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4428 err = got_error_from_errno("asprintf");
4432 s->root = s->tree = root;
4433 s->entries = got_object_tree_get_entries(root);
4434 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4435 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4436 s->commit_id = got_object_id_dup(commit_id);
4437 if (s->commit_id == NULL) {
4438 err = got_error_from_errno("got_object_id_dup");
4444 SIMPLEQ_INIT(&s->line_colors);
4446 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4447 err = add_line_color(&s->line_colors,
4448 "\\$$", 1, get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4451 err = add_line_color(&s->line_colors,
4452 "@$", 2, get_color_value("TOG_COLOR_TREE_SYMLINK"));
4454 free_line_colors(&s->line_colors);
4457 err = add_line_color(&s->line_colors,
4458 "/$", 3, get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4460 free_line_colors(&s->line_colors);
4464 err = add_line_color(&s->line_colors,
4465 "\\*$", 4, get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4467 free_line_colors(&s->line_colors);
4472 view->show = show_tree_view;
4473 view->input = input_tree_view;
4474 view->close = close_tree_view;
4475 view->search_start = search_start_tree_view;
4476 view->search_next = search_next_tree_view;
4478 free(commit_id_str);
4480 free(s->tree_label);
4481 s->tree_label = NULL;
4486 static const struct got_error *
4487 close_tree_view(struct tog_view *view)
4489 struct tog_tree_view_state *s = &view->state.tree;
4491 free_line_colors(&s->line_colors);
4492 free(s->tree_label);
4493 s->tree_label = NULL;
4495 s->commit_id = NULL;
4496 while (!TAILQ_EMPTY(&s->parents)) {
4497 struct tog_parent_tree *parent;
4498 parent = TAILQ_FIRST(&s->parents);
4499 TAILQ_REMOVE(&s->parents, parent, entry);
4503 if (s->tree != s->root)
4504 got_object_tree_close(s->tree);
4505 got_object_tree_close(s->root);
4510 static const struct got_error *
4511 search_start_tree_view(struct tog_view *view)
4513 struct tog_tree_view_state *s = &view->state.tree;
4515 s->matched_entry = NULL;
4520 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4522 regmatch_t regmatch;
4524 return regexec(regex, te->name, 1, ®match, 0) == 0;
4527 static const struct got_error *
4528 search_next_tree_view(struct tog_view *view)
4530 struct tog_tree_view_state *s = &view->state.tree;
4531 struct got_tree_entry *entry = NULL, *te;
4533 if (!view->searching) {
4534 view->search_next_done = 1;
4538 if (s->matched_entry) {
4539 if (view->searching == TOG_SEARCH_FORWARD) {
4540 if (s->selected_entry)
4541 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4543 entry = SIMPLEQ_FIRST(&s->entries->head);
4546 if (s->selected_entry == NULL) {
4547 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4550 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4552 if (SIMPLEQ_NEXT(te, entry) ==
4559 if (view->searching == TOG_SEARCH_FORWARD)
4560 entry = SIMPLEQ_FIRST(&s->entries->head);
4562 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4568 if (entry == NULL) {
4569 if (s->matched_entry == NULL) {
4570 view->search_next_done = 1;
4573 if (view->searching == TOG_SEARCH_FORWARD)
4574 entry = SIMPLEQ_FIRST(&s->entries->head);
4576 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4581 if (match_tree_entry(entry, &view->regex)) {
4582 view->search_next_done = 1;
4583 s->matched_entry = entry;
4587 if (view->searching == TOG_SEARCH_FORWARD)
4588 entry = SIMPLEQ_NEXT(entry, entry);
4590 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4593 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4594 if (SIMPLEQ_NEXT(te, entry) == entry) {
4603 if (s->matched_entry) {
4604 s->first_displayed_entry = s->matched_entry;
4611 static const struct got_error *
4612 show_tree_view(struct tog_view *view)
4614 const struct got_error *err = NULL;
4615 struct tog_tree_view_state *s = &view->state.tree;
4618 err = tree_entry_path(&parent_path, &s->parents, NULL);
4622 err = draw_tree_entries(view, &s->first_displayed_entry,
4623 &s->last_displayed_entry, &s->selected_entry,
4624 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4625 s->entries, s->selected, view->nlines, s->tree == s->root,
4633 static const struct got_error *
4634 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4635 struct tog_view **focus_view, struct tog_view *view, int ch)
4637 const struct got_error *err = NULL;
4638 struct tog_tree_view_state *s = &view->state.tree;
4639 struct tog_view *log_view;
4640 int begin_x = 0, nscrolled;
4644 s->show_ids = !s->show_ids;
4647 if (!s->selected_entry)
4649 if (view_is_parent_view(view))
4650 begin_x = view_split_begin_x(view->begin_x);
4651 err = log_tree_entry(&log_view, begin_x,
4652 s->selected_entry, &s->parents,
4653 s->commit_id, s->refs, s->repo);
4654 if (view_is_parent_view(view)) {
4655 err = view_close_child(view);
4658 err = view_set_child(view, log_view);
4660 view_close(log_view);
4663 *focus_view = log_view;
4664 view->child_focussed = 1;
4666 *new_view = log_view;
4670 if (s->selected > 0) {
4672 if (s->selected == 0)
4675 if (s->selected > 0)
4677 tree_scroll_up(view, &s->first_displayed_entry, 1,
4678 s->entries, s->tree == s->root);
4681 tree_scroll_up(view, &s->first_displayed_entry,
4682 MAX(0, view->nlines - 4 - s->selected), s->entries,
4683 s->tree == s->root);
4685 if (SIMPLEQ_FIRST(&s->entries->head) ==
4686 s->first_displayed_entry && s->tree != s->root)
4687 s->first_displayed_entry = NULL;
4691 if (s->selected < s->ndisplayed - 1) {
4695 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4696 /* can't scroll any further */
4698 tree_scroll_down(&s->first_displayed_entry, 1,
4699 s->last_displayed_entry, s->entries);
4702 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4704 /* can't scroll any further; move cursor down */
4705 if (s->selected < s->ndisplayed - 1)
4706 s->selected = s->ndisplayed - 1;
4709 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4710 view->nlines, s->last_displayed_entry, s->entries);
4711 if (nscrolled < view->nlines) {
4713 struct got_tree_entry *te;
4714 te = s->first_displayed_entry;
4717 te = SIMPLEQ_NEXT(te, entry);
4719 s->selected = ndisplayed - 1;
4725 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4726 struct tog_parent_tree *parent;
4727 /* user selected '..' */
4728 if (s->tree == s->root)
4730 parent = TAILQ_FIRST(&s->parents);
4731 TAILQ_REMOVE(&s->parents, parent,
4733 got_object_tree_close(s->tree);
4734 s->tree = parent->tree;
4736 got_object_tree_get_entries(s->tree);
4737 s->first_displayed_entry =
4738 parent->first_displayed_entry;
4740 parent->selected_entry;
4741 s->selected = parent->selected;
4743 } else if (S_ISDIR(s->selected_entry->mode)) {
4744 struct got_tree_object *subtree;
4745 err = got_object_open_as_tree(&subtree,
4746 s->repo, s->selected_entry->id);
4749 err = tree_view_visit_subtree(subtree, s);
4751 got_object_tree_close(subtree);
4754 } else if (S_ISREG(s->selected_entry->mode)) {
4755 struct tog_view *blame_view;
4756 int begin_x = view_is_parent_view(view) ?
4757 view_split_begin_x(view->begin_x) : 0;
4759 err = blame_tree_entry(&blame_view, begin_x,
4760 s->selected_entry, &s->parents,
4761 s->commit_id, s->refs, s->repo);
4764 if (view_is_parent_view(view)) {
4765 err = view_close_child(view);
4768 err = view_set_child(view, blame_view);
4770 view_close(blame_view);
4773 *focus_view = blame_view;
4774 view->child_focussed = 1;
4776 *new_view = blame_view;
4780 if (s->selected > view->nlines)
4781 s->selected = s->ndisplayed - 1;
4794 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4799 static const struct got_error *
4800 cmd_tree(int argc, char *argv[])
4802 const struct got_error *error;
4803 struct got_repository *repo = NULL;
4804 struct got_reflist_head refs;
4805 char *repo_path = NULL;
4806 struct got_object_id *commit_id = NULL;
4807 char *commit_id_arg = NULL;
4808 struct got_commit_object *commit = NULL;
4809 struct got_tree_object *tree = NULL;
4811 struct tog_view *view;
4813 SIMPLEQ_INIT(&refs);
4816 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4821 while ((ch = getopt(argc, argv, "c:")) != -1) {
4824 commit_id_arg = optarg;
4836 struct got_worktree *worktree;
4837 char *cwd = getcwd(NULL, 0);
4839 return got_error_from_errno("getcwd");
4840 error = got_worktree_open(&worktree, cwd);
4841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4846 strdup(got_worktree_get_repo_path(worktree));
4847 got_worktree_close(worktree);
4850 if (repo_path == NULL) {
4851 error = got_error_from_errno("strdup");
4854 } else if (argc == 1) {
4855 repo_path = realpath(argv[0], NULL);
4856 if (repo_path == NULL)
4857 return got_error_from_errno2("realpath", argv[0]);
4863 error = got_repo_open(&repo, repo_path, NULL);
4867 error = apply_unveil(got_repo_get_path(repo), NULL);
4871 if (commit_id_arg == NULL)
4872 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4874 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4876 if (error->code != GOT_ERR_NOT_REF)
4878 error = got_repo_match_object_id_prefix(&commit_id,
4879 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4885 error = got_object_open_as_commit(&commit, repo, commit_id);
4889 error = got_object_open_as_tree(&tree, repo,
4890 got_object_commit_get_tree_id(commit));
4894 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4898 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4900 error = got_error_from_errno("view_open");
4903 error = open_tree_view(view, tree, commit_id, &refs, repo);
4906 error = view_loop(view);
4911 got_object_commit_close(commit);
4913 got_object_tree_close(tree);
4915 got_repo_close(repo);
4916 got_ref_list_free(&refs);
4925 fprintf(stderr, "commands:");
4926 for (i = 0; i < nitems(tog_commands); i++) {
4927 struct tog_cmd *cmd = &tog_commands[i];
4928 fprintf(stderr, " %s", cmd->name);
4930 fputc('\n', stderr);
4936 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4944 make_argv(const char *arg0, const char *arg1)
4947 int argc = (arg1 == NULL ? 1 : 2);
4949 argv = calloc(argc, sizeof(char *));
4952 argv[0] = strdup(arg0);
4953 if (argv[0] == NULL)
4956 argv[1] = strdup(arg1);
4957 if (argv[1] == NULL)
4965 main(int argc, char *argv[])
4967 const struct got_error *error = NULL;
4968 struct tog_cmd *cmd = NULL;
4969 int ch, hflag = 0, Vflag = 0;
4970 char **cmd_argv = NULL;
4972 setlocale(LC_CTYPE, "");
4974 while ((ch = getopt(argc, argv, "hV")) != -1) {
4994 got_version_print_str();
5001 /* Build an argument vector which runs a default command. */
5002 cmd = &tog_commands[0];
5003 cmd_argv = make_argv(cmd->name, NULL);
5008 /* Did the user specific a command? */
5009 for (i = 0; i < nitems(tog_commands); i++) {
5010 if (strncmp(tog_commands[i].name, argv[0],
5011 strlen(argv[0])) == 0) {
5012 cmd = &tog_commands[i];
5018 fprintf(stderr, "%s: unknown command '%s'\n",
5019 getprogname(), argv[0]);
5028 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5032 if (error && error->code != GOT_ERR_CANCELLED)
5033 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);