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_diff_view_state {
116 struct got_object_id *id1, *id2;
118 int first_displayed_line;
119 int last_displayed_line;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
144 int *search_next_done;
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
155 const char *head_ref_name;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
161 struct tog_log_thread_args thread_args;
162 struct commit_queue_entry *matched_entry;
163 struct commit_queue_entry *search_entry;
166 struct tog_blame_cb_args {
167 struct tog_blame_line *lines; /* one per line */
170 struct tog_view *view;
171 struct got_object_id *commit_id;
175 struct tog_blame_thread_args {
177 struct got_repository *repo;
178 struct tog_blame_cb_args *cb_args;
180 got_cancel_cb cancel_cb;
187 struct tog_blame_line *lines;
191 struct tog_blame_thread_args thread_args;
192 struct tog_blame_cb_args cb_args;
196 struct tog_blame_view_state {
197 int first_displayed_line;
198 int last_displayed_line;
203 struct got_object_id_queue blamed_commits;
204 struct got_object_qid *blamed_commit;
206 struct got_repository *repo;
207 struct got_reflist_head *refs;
208 struct got_object_id *commit_id;
209 struct tog_blame blame;
213 struct tog_parent_tree {
214 TAILQ_ENTRY(tog_parent_tree) entry;
215 struct got_tree_object *tree;
216 struct got_tree_entry *first_displayed_entry;
217 struct got_tree_entry *selected_entry;
221 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
223 struct tog_tree_view_state {
225 struct got_tree_object *root;
226 struct got_tree_object *tree;
227 const struct got_tree_entries *entries;
228 struct got_tree_entry *first_displayed_entry;
229 struct got_tree_entry *last_displayed_entry;
230 struct got_tree_entry *selected_entry;
231 int ndisplayed, selected, show_ids;
232 struct tog_parent_trees parents;
233 struct got_object_id *commit_id;
234 struct got_repository *repo;
235 struct got_reflist_head *refs;
236 struct got_tree_entry *matched_entry;
240 * We implement two types of views: parent views and child views.
242 * The 'Tab' key switches between a parent view and its child view.
243 * Child views are shown side-by-side to their parent view, provided
244 * there is enough screen estate.
246 * When a new view is opened from within a parent view, this new view
247 * becomes a child view of the parent view, replacing any existing child.
249 * When a new view is opened from within a child view, this new view
250 * becomes a parent view which will obscure the views below until the
251 * user quits the new parent view by typing 'q'.
253 * This list of views contains parent views only.
254 * Child views are only pointed to by their parent view.
256 TAILQ_HEAD(tog_view_list_head, tog_view);
259 TAILQ_ENTRY(tog_view) entry;
262 int nlines, ncols, begin_y, begin_x;
263 int lines, cols; /* copies of LINES and COLS */
265 struct tog_view *parent;
266 struct tog_view *child;
269 /* type-specific state */
270 enum tog_view_type type;
272 struct tog_diff_view_state diff;
273 struct tog_log_view_state log;
274 struct tog_blame_view_state blame;
275 struct tog_tree_view_state tree;
278 const struct got_error *(*show)(struct tog_view *);
279 const struct got_error *(*input)(struct tog_view **,
280 struct tog_view **, struct tog_view**, struct tog_view *, int);
281 const struct got_error *(*close)(struct tog_view *);
283 const struct got_error *(*search_start)(struct tog_view *);
284 const struct got_error *(*search_next)(struct tog_view *);
286 #define TOG_SEARCH_FORWARD 1
287 #define TOG_SEARCH_BACKWARD 2
288 int search_next_done;
292 static const struct got_error *open_diff_view(struct tog_view *,
293 struct got_object_id *, struct got_object_id *, struct tog_view *,
294 struct got_reflist_head *, struct got_repository *);
295 static const struct got_error *show_diff_view(struct tog_view *);
296 static const struct got_error *input_diff_view(struct tog_view **,
297 struct tog_view **, struct tog_view **, struct tog_view *, int);
298 static const struct got_error* close_diff_view(struct tog_view *);
300 static const struct got_error *open_log_view(struct tog_view *,
301 struct got_object_id *, struct got_reflist_head *,
302 struct got_repository *, const char *, const char *, int);
303 static const struct got_error * show_log_view(struct tog_view *);
304 static const struct got_error *input_log_view(struct tog_view **,
305 struct tog_view **, struct tog_view **, struct tog_view *, int);
306 static const struct got_error *close_log_view(struct tog_view *);
307 static const struct got_error *search_start_log_view(struct tog_view *);
308 static const struct got_error *search_next_log_view(struct tog_view *);
310 static const struct got_error *open_blame_view(struct tog_view *, char *,
311 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
312 static const struct got_error *show_blame_view(struct tog_view *);
313 static const struct got_error *input_blame_view(struct tog_view **,
314 struct tog_view **, struct tog_view **, struct tog_view *, int);
315 static const struct got_error *close_blame_view(struct tog_view *);
316 static const struct got_error *search_start_blame_view(struct tog_view *);
317 static const struct got_error *search_next_blame_view(struct tog_view *);
319 static const struct got_error *open_tree_view(struct tog_view *,
320 struct got_tree_object *, struct got_object_id *,
321 struct got_reflist_head *, struct got_repository *);
322 static const struct got_error *show_tree_view(struct tog_view *);
323 static const struct got_error *input_tree_view(struct tog_view **,
324 struct tog_view **, struct tog_view **, struct tog_view *, int);
325 static const struct got_error *close_tree_view(struct tog_view *);
326 static const struct got_error *search_start_tree_view(struct tog_view *);
327 static const struct got_error *search_next_tree_view(struct tog_view *);
329 static volatile sig_atomic_t tog_sigwinch_received;
330 static volatile sig_atomic_t tog_sigpipe_received;
333 tog_sigwinch(int signo)
335 tog_sigwinch_received = 1;
339 tog_sigpipe(int signo)
341 tog_sigpipe_received = 1;
344 static const struct got_error *
345 view_close(struct tog_view *view)
347 const struct got_error *err = NULL;
350 view_close(view->child);
354 err = view->close(view);
356 del_panel(view->panel);
358 delwin(view->window);
363 static struct tog_view *
364 view_open(int nlines, int ncols, int begin_y, int begin_x,
365 enum tog_view_type type)
367 struct tog_view *view = calloc(1, sizeof(*view));
375 view->nlines = nlines ? nlines : LINES - begin_y;
376 view->ncols = ncols ? ncols : COLS - begin_x;
377 view->begin_y = begin_y;
378 view->begin_x = begin_x;
379 view->window = newwin(nlines, ncols, begin_y, begin_x);
380 if (view->window == NULL) {
384 view->panel = new_panel(view->window);
385 if (view->panel == NULL ||
386 set_panel_userptr(view->panel, view) != OK) {
391 keypad(view->window, TRUE);
396 view_split_begin_x(int begin_x)
398 if (begin_x > 0 || COLS < 120)
400 return (COLS - MAX(COLS / 2, 80));
403 static const struct got_error *view_resize(struct tog_view *);
405 static const struct got_error *
406 view_splitscreen(struct tog_view *view)
408 const struct got_error *err = NULL;
411 view->begin_x = view_split_begin_x(0);
412 view->nlines = LINES;
413 view->ncols = COLS - view->begin_x;
416 err = view_resize(view);
420 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
421 return got_error_from_errno("mvwin");
426 static const struct got_error *
427 view_fullscreen(struct tog_view *view)
429 const struct got_error *err = NULL;
433 view->nlines = LINES;
437 err = view_resize(view);
441 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
442 return got_error_from_errno("mvwin");
448 view_is_parent_view(struct tog_view *view)
450 return view->parent == NULL;
453 static const struct got_error *
454 view_resize(struct tog_view *view)
458 if (view->lines > LINES)
459 nlines = view->nlines - (view->lines - LINES);
461 nlines = view->nlines + (LINES - view->lines);
463 if (view->cols > COLS)
464 ncols = view->ncols - (view->cols - COLS);
466 ncols = view->ncols + (COLS - view->cols);
468 if (wresize(view->window, nlines, ncols) == ERR)
469 return got_error_from_errno("wresize");
470 if (replace_panel(view->panel, view->window) == ERR)
471 return got_error_from_errno("replace_panel");
472 wclear(view->window);
474 view->nlines = nlines;
480 view->child->begin_x = view_split_begin_x(view->begin_x);
481 if (view->child->begin_x == 0) {
482 view_fullscreen(view->child);
483 if (view->child->focussed)
484 show_panel(view->child->panel);
486 show_panel(view->panel);
488 view_splitscreen(view->child);
489 show_panel(view->child->panel);
496 static const struct got_error *
497 view_close_child(struct tog_view *view)
499 const struct got_error *err = NULL;
501 if (view->child == NULL)
504 err = view_close(view->child);
509 static const struct got_error *
510 view_set_child(struct tog_view *view, struct tog_view *child)
512 const struct got_error *err = NULL;
515 child->parent = view;
520 view_is_splitscreen(struct tog_view *view)
522 return view->begin_x > 0;
531 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
532 cols = 80; /* Default */
538 resize_term(lines, cols);
541 static const struct got_error *
542 view_search_start(struct tog_view *view)
544 const struct got_error *err = NULL;
549 if (view->nlines < 1)
552 if (!view_is_parent_view(view))
553 begin_x = view_split_begin_x(view->begin_x);
554 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
556 wclrtoeol(view->window);
560 ret = wgetnstr(view->window, pattern, sizeof(pattern));
566 if (view->searching) {
567 regfree(&view->regex);
571 if (regcomp(&view->regex, pattern,
572 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
573 err = view->search_start(view);
575 regfree(&view->regex);
578 view->searching = TOG_SEARCH_FORWARD;
579 view->search_next_done = 0;
580 view->search_next(view);
586 static const struct got_error *
587 view_input(struct tog_view **new, struct tog_view **dead,
588 struct tog_view **focus, int *done, struct tog_view *view,
589 struct tog_view_list_head *views)
591 const struct got_error *err = NULL;
599 if (view->searching && !view->search_next_done) {
600 errcode = pthread_mutex_unlock(&tog_mutex);
602 return got_error_set_errno(errcode,
603 "pthread_mutex_unlock");
605 errcode = pthread_mutex_lock(&tog_mutex);
607 return got_error_set_errno(errcode,
608 "pthread_mutex_lock");
609 view->search_next(view);
613 nodelay(stdscr, FALSE);
614 /* Allow threads to make progress while we are waiting for input. */
615 errcode = pthread_mutex_unlock(&tog_mutex);
617 return got_error_set_errno(errcode, "pthread_mutex_unlock");
618 ch = wgetch(view->window);
619 errcode = pthread_mutex_lock(&tog_mutex);
621 return got_error_set_errno(errcode, "pthread_mutex_lock");
622 nodelay(stdscr, TRUE);
624 if (tog_sigwinch_received) {
626 tog_sigwinch_received = 0;
627 TAILQ_FOREACH(v, views, entry) {
628 err = view_resize(v);
631 err = v->input(new, dead, focus, v, KEY_RESIZE);
642 *focus = view->child;
643 view->child_focussed = 1;
644 } else if (view->parent) {
645 *focus = view->parent;
646 view->parent->child_focussed = 0;
650 err = view->input(new, dead, focus, view, ch);
657 if (view_is_parent_view(view)) {
658 if (view->child == NULL)
660 if (view_is_splitscreen(view->child)) {
661 *focus = view->child;
662 view->child_focussed = 1;
663 err = view_fullscreen(view->child);
665 err = view_splitscreen(view->child);
668 err = view->child->input(new, dead, focus,
669 view->child, KEY_RESIZE);
671 if (view_is_splitscreen(view)) {
673 view->parent->child_focussed = 1;
674 err = view_fullscreen(view);
676 err = view_splitscreen(view);
680 err = view->input(new, dead, focus, view,
687 if (view->search_start)
688 view_search_start(view);
690 err = view->input(new, dead, focus, view, ch);
694 if (view->search_next && view->searching) {
695 view->searching = (ch == 'n' ?
696 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
697 view->search_next_done = 0;
698 view->search_next(view);
700 err = view->input(new, dead, focus, view, ch);
703 err = view->input(new, dead, focus, view, ch);
711 view_vborder(struct tog_view *view)
714 struct tog_view *view_above;
717 return view_vborder(view->parent);
719 panel = panel_above(view->panel);
723 view_above = panel_userptr(panel);
724 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
725 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
729 view_needs_focus_indication(struct tog_view *view)
731 if (view_is_parent_view(view)) {
732 if (view->child == NULL || view->child_focussed)
734 if (!view_is_splitscreen(view->child))
736 } else if (!view_is_splitscreen(view))
739 return view->focussed;
742 static const struct got_error *
743 view_loop(struct tog_view *view)
745 const struct got_error *err = NULL;
746 struct tog_view_list_head views;
747 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
748 int fast_refresh = 10;
749 int done = 0, errcode;
751 errcode = pthread_mutex_lock(&tog_mutex);
753 return got_error_set_errno(errcode, "pthread_mutex_lock");
756 TAILQ_INSERT_HEAD(&views, view, entry);
760 err = view->show(view);
765 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
766 /* Refresh fast during initialization, then become slower. */
767 if (fast_refresh && fast_refresh-- == 0)
768 halfdelay(10); /* switch to once per second */
770 err = view_input(&new_view, &dead_view, &focus_view, &done,
775 struct tog_view *prev = NULL;
777 if (view_is_parent_view(dead_view))
778 prev = TAILQ_PREV(dead_view,
779 tog_view_list_head, entry);
780 else if (view->parent != dead_view)
783 if (dead_view->parent)
784 dead_view->parent->child = NULL;
786 TAILQ_REMOVE(&views, dead_view, entry);
788 err = view_close(dead_view);
789 if (err || (dead_view == main_view && new_view == NULL))
792 if (view == dead_view) {
797 else if (!TAILQ_EMPTY(&views))
798 view = TAILQ_LAST(&views,
803 if (view->child && view->child_focussed)
804 focus_view = view->child;
811 struct tog_view *v, *t;
812 /* Only allow one parent view per type. */
813 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
814 if (v->type != new_view->type)
816 TAILQ_REMOVE(&views, v, entry);
822 TAILQ_INSERT_TAIL(&views, new_view, entry);
824 if (focus_view == NULL)
825 focus_view = new_view;
828 show_panel(focus_view->panel);
831 focus_view->focussed = 1;
834 show_panel(new_view->panel);
835 if (view->child && view_is_splitscreen(view->child))
836 show_panel(view->child->panel);
839 if (focus_view == NULL) {
841 show_panel(view->panel);
842 if (view->child && view_is_splitscreen(view->child))
843 show_panel(view->child->panel);
847 err = view->parent->show(view->parent);
851 err = view->show(view);
855 err = view->child->show(view->child);
864 while (!TAILQ_EMPTY(&views)) {
865 view = TAILQ_FIRST(&views);
866 TAILQ_REMOVE(&views, view, entry);
870 errcode = pthread_mutex_unlock(&tog_mutex);
871 if (errcode && err == NULL)
872 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
882 "usage: %s log [-c commit] [-r repository-path] [path]\n",
887 /* Create newly allocated wide-character string equivalent to a byte string. */
888 static const struct got_error *
889 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
892 const struct got_error *err = NULL;
895 *wlen = mbstowcs(NULL, s, 0);
896 if (*wlen == (size_t)-1) {
899 return got_error_from_errno("mbstowcs");
901 /* byte string invalid in current encoding; try to "fix" it */
902 err = got_mbsavis(&vis, &vislen, s);
905 *wlen = mbstowcs(NULL, vis, 0);
906 if (*wlen == (size_t)-1) {
907 err = got_error_from_errno("mbstowcs"); /* give up */
912 *ws = calloc(*wlen + 1, sizeof(**ws));
914 err = got_error_from_errno("calloc");
918 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
919 err = got_error_from_errno("mbstowcs");
930 /* Format a line for display, ensuring that it won't overflow a width limit. */
931 static const struct got_error *
932 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
935 const struct got_error *err = NULL;
937 wchar_t *wline = NULL;
944 err = mbs2ws(&wline, &wlen, line);
950 int width = wcwidth(wline[i]);
957 if (width == 1 || width == 2) {
958 if (cols + width > wlimit)
962 } else if (width == -1) {
963 if (wline[i] == L'\t') {
965 ((cols + col_tab_align) % TABSIZE);
966 if (cols + width > wlimit)
972 err = got_error_from_errno("wcwidth");
987 static const struct got_error*
988 build_refs_str(char **refs_str, struct got_reflist_head *refs,
989 struct got_object_id *id, struct got_repository *repo)
991 static const struct got_error *err = NULL;
992 struct got_reflist_entry *re;
998 SIMPLEQ_FOREACH(re, refs, entry) {
999 struct got_tag_object *tag = NULL;
1002 name = got_ref_get_name(re->ref);
1003 if (strcmp(name, GOT_REF_HEAD) == 0)
1005 if (strncmp(name, "refs/", 5) == 0)
1007 if (strncmp(name, "got/", 4) == 0)
1009 if (strncmp(name, "heads/", 6) == 0)
1011 if (strncmp(name, "remotes/", 8) == 0)
1013 if (strncmp(name, "tags/", 5) == 0) {
1014 err = got_object_open_as_tag(&tag, repo, re->id);
1016 if (err->code != GOT_ERR_OBJ_TYPE)
1018 /* Ref points at something other than a tag. */
1023 cmp = got_object_id_cmp(tag ?
1024 got_object_tag_get_object_id(tag) : re->id, id);
1026 got_object_tag_close(tag);
1030 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1031 s ? ", " : "", name) == -1) {
1032 err = got_error_from_errno("asprintf");
1043 static const struct got_error *
1044 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1047 char *smallerthan, *at;
1049 smallerthan = strchr(author, '<');
1050 if (smallerthan && smallerthan[1] != '\0')
1051 author = smallerthan + 1;
1052 at = strchr(author, '@');
1055 return format_line(wauthor, author_width, author, limit, col_tab_align);
1058 static const struct got_error *
1059 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1060 struct got_object_id *id, struct got_reflist_head *refs,
1061 const size_t date_display_cols, int author_display_cols)
1063 const struct got_error *err = NULL;
1064 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1065 char *logmsg0 = NULL, *logmsg = NULL;
1066 char *author = NULL;
1067 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1068 int author_width, logmsg_width;
1069 char *newline, *line = NULL;
1071 const int avail = view->ncols;
1073 time_t committer_time;
1075 committer_time = got_object_commit_get_committer_time(commit);
1076 if (localtime_r(&committer_time, &tm) == NULL)
1077 return got_error_from_errno("localtime_r");
1078 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1080 return got_error(GOT_ERR_NO_SPACE);
1082 if (avail <= date_display_cols)
1083 limit = MIN(sizeof(datebuf) - 1, avail);
1085 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1086 waddnstr(view->window, datebuf, limit);
1093 err = got_object_id_str(&id_str, id);
1096 wprintw(view->window, "%.8s ", id_str);
1103 author = strdup(got_object_commit_get_author(commit));
1104 if (author == NULL) {
1105 err = got_error_from_errno("strdup");
1108 err = format_author(&wauthor, &author_width, author, avail - col, col);
1111 waddwstr(view->window, wauthor);
1112 col += author_width;
1113 while (col < avail && author_width < author_display_cols + 2) {
1114 waddch(view->window, ' ');
1121 err = got_object_commit_get_logmsg(&logmsg0, commit);
1125 while (*logmsg == '\n')
1127 newline = strchr(logmsg, '\n');
1130 limit = avail - col;
1131 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1134 waddwstr(view->window, wlogmsg);
1135 col += logmsg_width;
1136 while (col < avail) {
1137 waddch(view->window, ' ');
1149 static struct commit_queue_entry *
1150 alloc_commit_queue_entry(struct got_commit_object *commit,
1151 struct got_object_id *id)
1153 struct commit_queue_entry *entry;
1155 entry = calloc(1, sizeof(*entry));
1160 entry->commit = commit;
1165 pop_commit(struct commit_queue *commits)
1167 struct commit_queue_entry *entry;
1169 entry = TAILQ_FIRST(&commits->head);
1170 TAILQ_REMOVE(&commits->head, entry, entry);
1171 got_object_commit_close(entry->commit);
1172 commits->ncommits--;
1173 /* Don't free entry->id! It is owned by the commit graph. */
1178 free_commits(struct commit_queue *commits)
1180 while (!TAILQ_EMPTY(&commits->head))
1181 pop_commit(commits);
1184 static const struct got_error *
1185 match_commit(int *have_match, struct got_object_id *id,
1186 struct got_commit_object *commit, regex_t *regex)
1188 const struct got_error *err = NULL;
1189 regmatch_t regmatch;
1190 char *id_str = NULL, *logmsg = NULL;
1194 err = got_object_id_str(&id_str, id);
1198 err = got_object_commit_get_logmsg(&logmsg, commit);
1202 if (regexec(regex, got_object_commit_get_author(commit), 1,
1203 ®match, 0) == 0 ||
1204 regexec(regex, got_object_commit_get_committer(commit), 1,
1205 ®match, 0) == 0 ||
1206 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1207 regexec(regex, logmsg, 1, ®match, 0) == 0)
1215 static const struct got_error *
1216 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1217 int minqueue, struct got_repository *repo, const char *path,
1218 int *searching, int *search_next_done, regex_t *regex)
1220 const struct got_error *err = NULL;
1221 int nqueued = 0, have_match = 0;
1224 * We keep all commits open throughout the lifetime of the log
1225 * view in order to avoid having to re-fetch commits from disk
1226 * while updating the display.
1228 while (nqueued < minqueue ||
1229 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1230 struct got_object_id *id;
1231 struct got_commit_object *commit;
1232 struct commit_queue_entry *entry;
1235 err = got_commit_graph_iter_next(&id, graph);
1237 if (err->code != GOT_ERR_ITER_NEED_MORE)
1239 err = got_commit_graph_fetch_commits(graph,
1240 minqueue, repo, NULL, NULL);
1249 err = got_object_open_as_commit(&commit, repo, id);
1252 entry = alloc_commit_queue_entry(commit, id);
1253 if (entry == NULL) {
1254 err = got_error_from_errno("alloc_commit_queue_entry");
1258 errcode = pthread_mutex_lock(&tog_mutex);
1260 err = got_error_set_errno(errcode,
1261 "pthread_mutex_lock");
1265 entry->idx = commits->ncommits;
1266 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1268 commits->ncommits++;
1270 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1271 err = match_commit(&have_match, id, commit, regex);
1273 pthread_mutex_lock(&tog_mutex);
1278 errcode = pthread_mutex_unlock(&tog_mutex);
1279 if (errcode && err == NULL)
1280 err = got_error_set_errno(errcode,
1281 "pthread_mutex_unlock");
1290 static const struct got_error *
1291 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1292 struct got_repository *repo)
1294 const struct got_error *err = NULL;
1295 struct got_reference *head_ref;
1299 err = got_ref_open(&head_ref, repo, branch_name, 0);
1303 err = got_ref_resolve(head_id, repo, head_ref);
1304 got_ref_close(head_ref);
1313 static const struct got_error *
1314 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1315 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1316 struct commit_queue *commits, int selected_idx, int limit,
1317 struct got_reflist_head *refs, const char *path, int commits_needed)
1319 const struct got_error *err = NULL;
1320 struct tog_log_view_state *s = &view->state.log;
1321 struct commit_queue_entry *entry;
1323 int ncommits, author_cols = 10;
1324 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1325 char *refs_str = NULL;
1327 static const size_t date_display_cols = 9;
1332 if (ncommits == selected_idx) {
1336 entry = TAILQ_NEXT(entry, entry);
1340 if (*selected && !(view->searching && view->search_next_done == 0)) {
1341 err = got_object_id_str(&id_str, (*selected)->id);
1345 err = build_refs_str(&refs_str, refs, (*selected)->id,
1352 if (commits_needed == 0)
1353 halfdelay(10); /* disable fast refresh */
1355 if (asprintf(&ncommits_str, " [%d/%d] %s",
1356 entry ? entry->idx + 1 : 0, commits->ncommits,
1357 commits_needed > 0 ?
1358 (view->searching && view->search_next_done == 0
1359 ? "searching..." : "loading... ") :
1360 (refs_str ? refs_str : "")) == -1) {
1361 err = got_error_from_errno("asprintf");
1365 if (path && strcmp(path, "/") != 0) {
1366 if (asprintf(&header, "commit %s %s%s",
1367 id_str ? id_str : "........................................",
1368 path, ncommits_str) == -1) {
1369 err = got_error_from_errno("asprintf");
1373 } else if (asprintf(&header, "commit %s%s",
1374 id_str ? id_str : "........................................",
1375 ncommits_str) == -1) {
1376 err = got_error_from_errno("asprintf");
1380 err = format_line(&wline, &width, header, view->ncols, 0);
1384 werase(view->window);
1386 if (view_needs_focus_indication(view))
1387 wstandout(view->window);
1388 waddwstr(view->window, wline);
1389 while (width < view->ncols) {
1390 waddch(view->window, ' ');
1393 if (view_needs_focus_indication(view))
1394 wstandend(view->window);
1399 /* Grow author column size if necessary. */
1406 if (ncommits >= limit - 1)
1408 author = strdup(got_object_commit_get_author(entry->commit));
1409 if (author == NULL) {
1410 err = got_error_from_errno("strdup");
1413 err = format_author(&wauthor, &width, author, COLS,
1415 if (author_cols < width)
1416 author_cols = width;
1420 entry = TAILQ_NEXT(entry, entry);
1427 if (ncommits >= limit - 1)
1429 if (ncommits == selected_idx)
1430 wstandout(view->window);
1431 err = draw_commit(view, entry->commit, entry->id, refs,
1432 date_display_cols, author_cols);
1433 if (ncommits == selected_idx)
1434 wstandend(view->window);
1439 entry = TAILQ_NEXT(entry, entry);
1452 scroll_up(struct tog_view *view,
1453 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1454 struct commit_queue *commits)
1456 struct commit_queue_entry *entry;
1459 entry = TAILQ_FIRST(&commits->head);
1460 if (*first_displayed_entry == entry)
1463 entry = *first_displayed_entry;
1464 while (entry && nscrolled < maxscroll) {
1465 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1467 *first_displayed_entry = entry;
1473 static const struct got_error *
1474 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1475 pthread_cond_t *need_commits)
1480 halfdelay(1); /* fast refresh while loading commits */
1482 while (*commits_needed > 0) {
1486 /* Wake the log thread. */
1487 errcode = pthread_cond_signal(need_commits);
1489 return got_error_set_errno(errcode,
1490 "pthread_cond_signal");
1491 errcode = pthread_mutex_unlock(&tog_mutex);
1493 return got_error_set_errno(errcode,
1494 "pthread_mutex_unlock");
1496 errcode = pthread_mutex_lock(&tog_mutex);
1498 return got_error_set_errno(errcode,
1499 "pthread_mutex_lock");
1501 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1503 * Thread is not done yet; lose a key press
1504 * and let the user retry... this way the GUI
1505 * remains interactive while logging deep paths
1506 * with few commits in history.
1515 static const struct got_error *
1516 scroll_down(struct tog_view *view,
1517 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1518 struct commit_queue_entry **last_displayed_entry,
1519 struct commit_queue *commits, int *log_complete, int *commits_needed,
1520 pthread_cond_t *need_commits)
1522 const struct got_error *err = NULL;
1523 struct commit_queue_entry *pentry;
1526 if (*last_displayed_entry == NULL)
1529 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1530 if (pentry == NULL && !*log_complete) {
1532 * Ask the log thread for required amount of commits
1533 * plus some amount of pre-fetching.
1535 (*commits_needed) += maxscroll + 20;
1536 err = trigger_log_thread(0, commits_needed, log_complete,
1543 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1547 *last_displayed_entry = pentry;
1549 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1552 *first_displayed_entry = pentry;
1553 } while (++nscrolled < maxscroll);
1558 static const struct got_error *
1559 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1560 struct got_commit_object *commit, struct got_object_id *commit_id,
1561 struct tog_view *log_view, struct got_reflist_head *refs,
1562 struct got_repository *repo)
1564 const struct got_error *err;
1565 struct got_object_qid *parent_id;
1566 struct tog_view *diff_view;
1568 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1569 if (diff_view == NULL)
1570 return got_error_from_errno("view_open");
1572 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1573 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1574 commit_id, log_view, refs, repo);
1576 *new_view = diff_view;
1580 static const struct got_error *
1581 tree_view_visit_subtree(struct got_tree_object *subtree,
1582 struct tog_tree_view_state *s)
1584 struct tog_parent_tree *parent;
1586 parent = calloc(1, sizeof(*parent));
1588 return got_error_from_errno("calloc");
1590 parent->tree = s->tree;
1591 parent->first_displayed_entry = s->first_displayed_entry;
1592 parent->selected_entry = s->selected_entry;
1593 parent->selected = s->selected;
1594 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1596 s->entries = got_object_tree_get_entries(s->tree);
1598 s->first_displayed_entry = NULL;
1603 static const struct got_error *
1604 browse_commit_tree(struct tog_view **new_view, int begin_x,
1605 struct commit_queue_entry *entry, const char *path,
1606 struct got_reflist_head *refs, struct got_repository *repo)
1608 const struct got_error *err = NULL;
1609 struct got_tree_object *tree;
1610 struct got_tree_entry *te;
1611 struct tog_tree_view_state *s;
1612 struct tog_view *tree_view;
1613 char *slash, *subpath = NULL;
1616 err = got_object_open_as_tree(&tree, repo,
1617 got_object_commit_get_tree_id(entry->commit));
1621 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1622 if (tree_view == NULL)
1623 return got_error_from_errno("view_open");
1625 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1627 got_object_tree_close(tree);
1630 s = &tree_view->state.tree;
1632 *new_view = tree_view;
1634 /* Walk the path and open corresponding tree objects. */
1639 struct got_object_id *tree_id;
1641 /* Ensure the correct subtree entry is selected. */
1642 slash = strchr(p, '/');
1644 slash = strchr(p, '\0');
1645 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1646 if (strncmp(p, te->name, slash - p) == 0) {
1647 s->selected_entry = te;
1652 if (s->selected_entry == NULL) {
1653 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1656 if (s->tree != s->root)
1657 s->selected++; /* skip '..' */
1659 if (!S_ISDIR(s->selected_entry->mode)) {
1660 /* Jump to this file's entry. */
1661 s->first_displayed_entry = s->selected_entry;
1666 slash = strchr(p, '/');
1668 subpath = strndup(path, slash - path);
1670 subpath = strdup(path);
1671 if (subpath == NULL) {
1672 err = got_error_from_errno("strdup");
1676 err = got_object_id_by_path(&tree_id, repo, entry->id,
1681 err = got_object_open_as_tree(&tree, repo, tree_id);
1686 err = tree_view_visit_subtree(tree, s);
1688 got_object_tree_close(tree);
1703 log_thread(void *arg)
1705 const struct got_error *err = NULL;
1707 struct tog_log_thread_args *a = arg;
1710 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1715 while (!done && !err && !tog_sigpipe_received) {
1716 err = queue_commits(a->graph, a->commits, 1, a->repo,
1717 a->in_repo_path, a->searching, a->search_next_done,
1720 if (err->code != GOT_ERR_ITER_COMPLETED)
1724 } else if (a->commits_needed > 0)
1725 a->commits_needed--;
1727 errcode = pthread_mutex_lock(&tog_mutex);
1729 err = got_error_set_errno(errcode,
1730 "pthread_mutex_lock");
1732 } else if (*a->quit)
1734 else if (*a->first_displayed_entry == NULL) {
1735 *a->first_displayed_entry =
1736 TAILQ_FIRST(&a->commits->head);
1737 *a->selected_entry = *a->first_displayed_entry;
1741 a->commits_needed = 0;
1742 else if (a->commits_needed == 0) {
1743 errcode = pthread_cond_wait(&a->need_commits,
1746 err = got_error_set_errno(errcode,
1747 "pthread_cond_wait");
1750 errcode = pthread_mutex_unlock(&tog_mutex);
1751 if (errcode && err == NULL)
1752 err = got_error_set_errno(errcode,
1753 "pthread_mutex_unlock");
1755 a->log_complete = 1;
1759 static const struct got_error *
1760 stop_log_thread(struct tog_log_view_state *s)
1762 const struct got_error *err = NULL;
1767 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1769 return got_error_set_errno(errcode,
1770 "pthread_cond_signal");
1771 errcode = pthread_mutex_unlock(&tog_mutex);
1773 return got_error_set_errno(errcode,
1774 "pthread_mutex_unlock");
1775 errcode = pthread_join(s->thread, (void **)&err);
1777 return got_error_set_errno(errcode, "pthread_join");
1778 errcode = pthread_mutex_lock(&tog_mutex);
1780 return got_error_set_errno(errcode,
1781 "pthread_mutex_lock");
1785 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1786 if (errcode && err == NULL)
1787 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1789 if (s->thread_args.repo) {
1790 got_repo_close(s->thread_args.repo);
1791 s->thread_args.repo = NULL;
1794 if (s->thread_args.graph) {
1795 got_commit_graph_close(s->thread_args.graph);
1796 s->thread_args.graph = NULL;
1802 static const struct got_error *
1803 close_log_view(struct tog_view *view)
1805 const struct got_error *err = NULL;
1806 struct tog_log_view_state *s = &view->state.log;
1808 err = stop_log_thread(s);
1809 free_commits(&s->commits);
1810 free(s->in_repo_path);
1811 s->in_repo_path = NULL;
1817 static const struct got_error *
1818 search_start_log_view(struct tog_view *view)
1820 struct tog_log_view_state *s = &view->state.log;
1822 s->matched_entry = NULL;
1823 s->search_entry = NULL;
1827 static const struct got_error *
1828 search_next_log_view(struct tog_view *view)
1830 const struct got_error *err = NULL;
1831 struct tog_log_view_state *s = &view->state.log;
1832 struct commit_queue_entry *entry;
1834 if (!view->searching) {
1835 view->search_next_done = 1;
1839 if (s->search_entry) {
1841 errcode = pthread_mutex_unlock(&tog_mutex);
1843 return got_error_set_errno(errcode,
1844 "pthread_mutex_unlock");
1845 ch = wgetch(view->window);
1846 errcode = pthread_mutex_lock(&tog_mutex);
1848 return got_error_set_errno(errcode,
1849 "pthread_mutex_lock");
1850 if (ch == KEY_BACKSPACE) {
1851 view->search_next_done = 1;
1854 if (view->searching == TOG_SEARCH_FORWARD)
1855 entry = TAILQ_NEXT(s->search_entry, entry);
1857 entry = TAILQ_PREV(s->search_entry,
1858 commit_queue_head, entry);
1859 } else if (s->matched_entry) {
1860 if (view->searching == TOG_SEARCH_FORWARD)
1861 entry = TAILQ_NEXT(s->selected_entry, entry);
1863 entry = TAILQ_PREV(s->selected_entry,
1864 commit_queue_head, entry);
1866 if (view->searching == TOG_SEARCH_FORWARD)
1867 entry = TAILQ_FIRST(&s->commits.head);
1869 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1875 if (entry == NULL) {
1876 if (s->thread_args.log_complete ||
1877 view->searching == TOG_SEARCH_BACKWARD) {
1878 view->search_next_done = 1;
1882 * Poke the log thread for more commits and return,
1883 * allowing the main loop to make progress. Search
1884 * will resume at s->search_entry once we come back.
1886 s->thread_args.commits_needed++;
1887 return trigger_log_thread(1,
1888 &s->thread_args.commits_needed,
1889 &s->thread_args.log_complete,
1890 &s->thread_args.need_commits);
1893 err = match_commit(&have_match, entry->id, entry->commit,
1898 view->search_next_done = 1;
1899 s->matched_entry = entry;
1903 s->search_entry = entry;
1904 if (view->searching == TOG_SEARCH_FORWARD)
1905 entry = TAILQ_NEXT(entry, entry);
1907 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1910 if (s->matched_entry) {
1911 int cur = s->selected_entry->idx;
1912 while (cur < s->matched_entry->idx) {
1913 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1918 while (cur > s->matched_entry->idx) {
1919 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1926 s->search_entry = NULL;
1931 static const struct got_error *
1932 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1933 struct got_reflist_head *refs, struct got_repository *repo,
1934 const char *head_ref_name, const char *path, int check_disk)
1936 const struct got_error *err = NULL;
1937 struct tog_log_view_state *s = &view->state.log;
1938 struct got_repository *thread_repo = NULL;
1939 struct got_commit_graph *thread_graph = NULL;
1942 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1946 /* The commit queue only contains commits being displayed. */
1947 TAILQ_INIT(&s->commits.head);
1948 s->commits.ncommits = 0;
1952 s->head_ref_name = head_ref_name;
1953 s->start_id = got_object_id_dup(start_id);
1954 if (s->start_id == NULL) {
1955 err = got_error_from_errno("got_object_id_dup");
1959 view->show = show_log_view;
1960 view->input = input_log_view;
1961 view->close = close_log_view;
1962 view->search_start = search_start_log_view;
1963 view->search_next = search_next_log_view;
1965 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1968 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1973 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1975 err = got_error_set_errno(errcode, "pthread_cond_init");
1979 s->thread_args.commits_needed = view->nlines;
1980 s->thread_args.graph = thread_graph;
1981 s->thread_args.commits = &s->commits;
1982 s->thread_args.in_repo_path = s->in_repo_path;
1983 s->thread_args.start_id = s->start_id;
1984 s->thread_args.repo = thread_repo;
1985 s->thread_args.log_complete = 0;
1986 s->thread_args.quit = &s->quit;
1987 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1988 s->thread_args.selected_entry = &s->selected_entry;
1989 s->thread_args.searching = &view->searching;
1990 s->thread_args.search_next_done = &view->search_next_done;
1991 s->thread_args.regex = &view->regex;
1994 close_log_view(view);
1998 static const struct got_error *
1999 show_log_view(struct tog_view *view)
2001 struct tog_log_view_state *s = &view->state.log;
2003 if (s->thread == NULL) {
2004 int errcode = pthread_create(&s->thread, NULL, log_thread,
2007 return got_error_set_errno(errcode, "pthread_create");
2010 return draw_commits(view, &s->last_displayed_entry,
2011 &s->selected_entry, s->first_displayed_entry,
2012 &s->commits, s->selected, view->nlines, s->refs,
2013 s->in_repo_path, s->thread_args.commits_needed);
2016 static const struct got_error *
2017 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2018 struct tog_view **focus_view, struct tog_view *view, int ch)
2020 const struct got_error *err = NULL;
2021 struct tog_log_view_state *s = &view->state.log;
2022 char *parent_path, *in_repo_path = NULL;
2023 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2025 struct got_object_id *start_id;
2035 if (s->first_displayed_entry == NULL)
2037 if (s->selected > 0)
2040 scroll_up(view, &s->first_displayed_entry, 1,
2045 if (s->first_displayed_entry == NULL)
2047 if (TAILQ_FIRST(&s->commits.head) ==
2048 s->first_displayed_entry) {
2052 scroll_up(view, &s->first_displayed_entry,
2053 view->nlines, &s->commits);
2059 if (s->first_displayed_entry == NULL)
2061 if (s->selected < MIN(view->nlines - 2,
2062 s->commits.ncommits - 1)) {
2066 err = scroll_down(view, &s->first_displayed_entry, 1,
2067 &s->last_displayed_entry, &s->commits,
2068 &s->thread_args.log_complete,
2069 &s->thread_args.commits_needed,
2070 &s->thread_args.need_commits);
2074 struct commit_queue_entry *first;
2075 first = s->first_displayed_entry;
2078 err = scroll_down(view, &s->first_displayed_entry,
2079 view->nlines, &s->last_displayed_entry,
2080 &s->commits, &s->thread_args.log_complete,
2081 &s->thread_args.commits_needed,
2082 &s->thread_args.need_commits);
2085 if (first == s->first_displayed_entry &&
2086 s->selected < MIN(view->nlines - 2,
2087 s->commits.ncommits - 1)) {
2088 /* can't scroll further down */
2089 s->selected = MIN(view->nlines - 2,
2090 s->commits.ncommits - 1);
2096 if (s->selected > view->nlines - 2)
2097 s->selected = view->nlines - 2;
2098 if (s->selected > s->commits.ncommits - 1)
2099 s->selected = s->commits.ncommits - 1;
2104 if (s->selected_entry == NULL)
2106 if (view_is_parent_view(view))
2107 begin_x = view_split_begin_x(view->begin_x);
2108 err = open_diff_view_for_commit(&diff_view, begin_x,
2109 s->selected_entry->commit, s->selected_entry->id,
2110 view, s->refs, s->repo);
2113 if (view_is_parent_view(view)) {
2114 err = view_close_child(view);
2117 err = view_set_child(view, diff_view);
2119 view_close(diff_view);
2122 *focus_view = diff_view;
2123 view->child_focussed = 1;
2125 *new_view = diff_view;
2128 if (s->selected_entry == NULL)
2130 if (view_is_parent_view(view))
2131 begin_x = view_split_begin_x(view->begin_x);
2132 err = browse_commit_tree(&tree_view, begin_x,
2133 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2136 if (view_is_parent_view(view)) {
2137 err = view_close_child(view);
2140 err = view_set_child(view, tree_view);
2142 view_close(tree_view);
2145 *focus_view = tree_view;
2146 view->child_focussed = 1;
2148 *new_view = tree_view;
2151 if (strcmp(s->in_repo_path, "/") == 0)
2153 parent_path = dirname(s->in_repo_path);
2154 if (parent_path && strcmp(parent_path, ".") != 0) {
2155 err = stop_log_thread(s);
2158 lv = view_open(view->nlines, view->ncols,
2159 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2161 return got_error_from_errno(
2163 err = open_log_view(lv, s->start_id, s->refs,
2164 s->repo, s->head_ref_name, parent_path, 0);
2167 if (view_is_parent_view(view))
2170 view_set_child(view->parent, lv);
2177 err = stop_log_thread(s);
2180 lv = view_open(view->nlines, view->ncols,
2181 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2183 return got_error_from_errno("view_open");
2184 err = get_head_commit_id(&start_id, s->head_ref_name ?
2185 s->head_ref_name : GOT_REF_HEAD, s->repo);
2190 in_repo_path = strdup(s->in_repo_path);
2191 if (in_repo_path == NULL) {
2194 return got_error_from_errno("strdup");
2196 got_ref_list_free(s->refs);
2197 err = got_ref_list(s->refs, s->repo, NULL,
2198 got_ref_cmp_by_name, NULL);
2204 err = open_log_view(lv, start_id, s->refs, s->repo,
2205 s->head_ref_name, in_repo_path, 0);
2221 static const struct got_error *
2222 apply_unveil(const char *repo_path, const char *worktree_path)
2224 const struct got_error *error;
2227 if (unveil("gmon.out", "rwc") != 0)
2228 return got_error_from_errno2("unveil", "gmon.out");
2230 if (repo_path && unveil(repo_path, "r") != 0)
2231 return got_error_from_errno2("unveil", repo_path);
2233 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2234 return got_error_from_errno2("unveil", worktree_path);
2236 if (unveil("/tmp", "rwc") != 0)
2237 return got_error_from_errno2("unveil", "/tmp");
2239 error = got_privsep_unveil_exec_helpers();
2243 if (unveil(NULL, NULL) != 0)
2244 return got_error_from_errno("unveil");
2254 halfdelay(1); /* Do fast refresh while initial view is loading. */
2257 intrflush(stdscr, FALSE);
2258 keypad(stdscr, TRUE);
2260 signal(SIGWINCH, tog_sigwinch);
2261 signal(SIGPIPE, tog_sigpipe);
2264 static const struct got_error *
2265 cmd_log(int argc, char *argv[])
2267 const struct got_error *error;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2270 struct got_reflist_head refs;
2271 struct got_object_id *start_id = NULL;
2272 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2273 char *start_commit = NULL, *head_ref_name = NULL;
2275 struct tog_view *view;
2277 SIMPLEQ_INIT(&refs);
2280 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2285 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2288 start_commit = optarg;
2291 repo_path = realpath(optarg, NULL);
2292 if (repo_path == NULL)
2293 return got_error_from_errno2("realpath",
2305 cwd = getcwd(NULL, 0);
2307 error = got_error_from_errno("getcwd");
2310 error = got_worktree_open(&worktree, cwd);
2311 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2318 error = got_error_from_errno("strdup");
2321 } else if (argc == 1) {
2323 error = got_worktree_resolve_path(&path, worktree,
2328 path = strdup(argv[0]);
2330 error = got_error_from_errno("strdup");
2337 if (repo_path == NULL) {
2340 got_worktree_get_repo_path(worktree));
2342 repo_path = strdup(cwd);
2344 if (repo_path == NULL) {
2345 error = got_error_from_errno("strdup");
2351 error = got_repo_open(&repo, repo_path, NULL);
2355 error = apply_unveil(got_repo_get_path(repo),
2356 worktree ? got_worktree_get_root_path(worktree) : NULL);
2360 if (start_commit == NULL)
2361 error = get_head_commit_id(&start_id, worktree ?
2362 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2365 error = get_head_commit_id(&start_id, start_commit, repo);
2367 if (error->code != GOT_ERR_NOT_REF)
2369 error = got_repo_match_object_id_prefix(&start_id,
2370 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2376 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2380 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2382 error = got_error_from_errno("view_open");
2386 head_ref_name = strdup(
2387 got_worktree_get_head_ref_name(worktree));
2388 if (head_ref_name == NULL) {
2389 error = got_error_from_errno("strdup");
2393 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2398 /* Release work tree lock. */
2399 got_worktree_close(worktree);
2402 error = view_loop(view);
2408 free(head_ref_name);
2410 got_repo_close(repo);
2412 got_worktree_close(worktree);
2413 got_ref_list_free(&refs);
2421 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2427 parse_next_line(FILE *f, size_t *len)
2432 const char delim[3] = { '\0', '\0', '\0'};
2434 line = fparseln(f, &linelen, &lineno, delim, 0);
2440 static const struct got_error *
2441 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2442 int *last_displayed_line, int *eof, int max_lines,
2445 const struct got_error *err;
2446 int nlines = 0, nprinted = 0;
2453 werase(view->window);
2456 err = format_line(&wline, &width, header, view->ncols, 0);
2461 if (view_needs_focus_indication(view))
2462 wstandout(view->window);
2463 waddwstr(view->window, wline);
2464 if (view_needs_focus_indication(view))
2465 wstandend(view->window);
2466 if (width <= view->ncols - 1)
2467 waddch(view->window, '\n');
2475 while (nprinted < max_lines) {
2476 line = parse_next_line(f, &len);
2481 if (++nlines < *first_displayed_line) {
2486 err = format_line(&wline, &width, line, view->ncols, 0);
2491 waddwstr(view->window, wline);
2492 if (width <= view->ncols - 1)
2493 waddch(view->window, '\n');
2494 if (++nprinted == 1)
2495 *first_displayed_line = nlines;
2500 *last_displayed_line = nlines;
2505 while (nprinted < view->nlines) {
2506 waddch(view->window, '\n');
2510 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2515 wstandout(view->window);
2516 waddwstr(view->window, wline);
2517 wstandend(view->window);
2524 get_datestr(time_t *time, char *datebuf)
2526 struct tm mytm, *tm;
2529 tm = gmtime_r(time, &mytm);
2532 s = asctime_r(tm, datebuf);
2535 p = strchr(s, '\n');
2541 static const struct got_error *
2542 write_commit_info(struct got_object_id *commit_id,
2543 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2545 const struct got_error *err = NULL;
2546 char datebuf[26], *datestr;
2547 struct got_commit_object *commit;
2548 char *id_str = NULL, *logmsg = NULL;
2549 time_t committer_time;
2550 const char *author, *committer;
2551 char *refs_str = NULL;
2554 err = build_refs_str(&refs_str, refs, commit_id, repo);
2559 err = got_object_open_as_commit(&commit, repo, commit_id);
2563 err = got_object_id_str(&id_str, commit_id);
2565 err = got_error_from_errno("got_object_id_str");
2569 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2570 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2571 err = got_error_from_errno("fprintf");
2574 if (fprintf(outfile, "from: %s\n",
2575 got_object_commit_get_author(commit)) < 0) {
2576 err = got_error_from_errno("fprintf");
2579 committer_time = got_object_commit_get_committer_time(commit);
2580 datestr = get_datestr(&committer_time, datebuf);
2581 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2582 err = got_error_from_errno("fprintf");
2585 author = got_object_commit_get_author(commit);
2586 committer = got_object_commit_get_committer(commit);
2587 if (strcmp(author, committer) != 0 &&
2588 fprintf(outfile, "via: %s\n", committer) < 0) {
2589 err = got_error_from_errno("fprintf");
2592 err = got_object_commit_get_logmsg(&logmsg, commit);
2595 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2596 err = got_error_from_errno("fprintf");
2603 got_object_commit_close(commit);
2607 static const struct got_error *
2608 create_diff(struct tog_diff_view_state *s)
2610 const struct got_error *err = NULL;
2616 err = got_error_from_errno("got_opentemp");
2619 if (s->f && fclose(s->f) != 0) {
2620 err = got_error_from_errno("fclose");
2626 err = got_object_get_type(&obj_type, s->repo, s->id1);
2628 err = got_object_get_type(&obj_type, s->repo, s->id2);
2633 case GOT_OBJ_TYPE_BLOB:
2634 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2635 s->diff_context, 0, s->repo, f);
2637 case GOT_OBJ_TYPE_TREE:
2638 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2639 s->diff_context, 0, s->repo, f);
2641 case GOT_OBJ_TYPE_COMMIT: {
2642 const struct got_object_id_queue *parent_ids;
2643 struct got_object_qid *pid;
2644 struct got_commit_object *commit2;
2646 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2649 /* Show commit info if we're diffing to a parent/root commit. */
2651 write_commit_info(s->id2, s->refs, s->repo, f);
2653 parent_ids = got_object_commit_get_parent_ids(commit2);
2654 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2655 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2656 write_commit_info(s->id2, s->refs,
2662 got_object_commit_close(commit2);
2664 err = got_diff_objects_as_commits(s->id1, s->id2,
2665 s->diff_context, 0, s->repo, f);
2669 err = got_error(GOT_ERR_OBJ_TYPE);
2673 if (f && fflush(f) != 0 && err == NULL)
2674 err = got_error_from_errno("fflush");
2679 diff_view_indicate_progress(struct tog_view *view)
2681 mvwaddstr(view->window, 0, 0, "diffing...");
2686 static const struct got_error *
2687 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2688 struct got_object_id *id2, struct tog_view *log_view,
2689 struct got_reflist_head *refs, struct got_repository *repo)
2691 const struct got_error *err;
2693 if (id1 != NULL && id2 != NULL) {
2695 err = got_object_get_type(&type1, repo, id1);
2698 err = got_object_get_type(&type2, repo, id2);
2703 return got_error(GOT_ERR_OBJ_TYPE);
2707 view->state.diff.id1 = got_object_id_dup(id1);
2708 if (view->state.diff.id1 == NULL)
2709 return got_error_from_errno("got_object_id_dup");
2711 view->state.diff.id1 = NULL;
2713 view->state.diff.id2 = got_object_id_dup(id2);
2714 if (view->state.diff.id2 == NULL) {
2715 free(view->state.diff.id1);
2716 view->state.diff.id1 = NULL;
2717 return got_error_from_errno("got_object_id_dup");
2719 view->state.diff.f = NULL;
2720 view->state.diff.first_displayed_line = 1;
2721 view->state.diff.last_displayed_line = view->nlines;
2722 view->state.diff.diff_context = 3;
2723 view->state.diff.log_view = log_view;
2724 view->state.diff.repo = repo;
2725 view->state.diff.refs = refs;
2727 if (log_view && view_is_splitscreen(view))
2728 show_log_view(log_view); /* draw vborder */
2729 diff_view_indicate_progress(view);
2731 err = create_diff(&view->state.diff);
2733 free(view->state.diff.id1);
2734 view->state.diff.id1 = NULL;
2735 free(view->state.diff.id2);
2736 view->state.diff.id2 = NULL;
2740 view->show = show_diff_view;
2741 view->input = input_diff_view;
2742 view->close = close_diff_view;
2747 static const struct got_error *
2748 close_diff_view(struct tog_view *view)
2750 const struct got_error *err = NULL;
2752 free(view->state.diff.id1);
2753 view->state.diff.id1 = NULL;
2754 free(view->state.diff.id2);
2755 view->state.diff.id2 = NULL;
2756 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2757 err = got_error_from_errno("fclose");
2761 static const struct got_error *
2762 show_diff_view(struct tog_view *view)
2764 const struct got_error *err;
2765 struct tog_diff_view_state *s = &view->state.diff;
2766 char *id_str1 = NULL, *id_str2, *header;
2769 err = got_object_id_str(&id_str1, s->id1);
2773 err = got_object_id_str(&id_str2, s->id2);
2777 if (asprintf(&header, "diff %s %s",
2778 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2779 err = got_error_from_errno("asprintf");
2787 return draw_file(view, s->f, &s->first_displayed_line,
2788 &s->last_displayed_line, &s->eof, view->nlines,
2792 static const struct got_error *
2793 set_selected_commit(struct tog_diff_view_state *s,
2794 struct commit_queue_entry *entry)
2796 const struct got_error *err;
2797 const struct got_object_id_queue *parent_ids;
2798 struct got_commit_object *selected_commit;
2799 struct got_object_qid *pid;
2802 s->id2 = got_object_id_dup(entry->id);
2804 return got_error_from_errno("got_object_id_dup");
2806 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2809 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2811 pid = SIMPLEQ_FIRST(parent_ids);
2812 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2813 got_object_commit_close(selected_commit);
2817 static const struct got_error *
2818 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2819 struct tog_view **focus_view, struct tog_view *view, int ch)
2821 const struct got_error *err = NULL;
2822 struct tog_diff_view_state *s = &view->state.diff;
2823 struct tog_log_view_state *ls;
2824 struct commit_queue_entry *entry;
2830 if (s->first_displayed_line > 1)
2831 s->first_displayed_line--;
2835 if (s->first_displayed_line == 1)
2838 while (i++ < view->nlines - 1 &&
2839 s->first_displayed_line > 1)
2840 s->first_displayed_line--;
2845 s->first_displayed_line++;
2853 while (!s->eof && i++ < view->nlines - 1) {
2855 line = parse_next_line(s->f, NULL);
2856 s->first_displayed_line++;
2862 if (s->diff_context > 0) {
2864 diff_view_indicate_progress(view);
2865 err = create_diff(s);
2869 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2871 diff_view_indicate_progress(view);
2872 err = create_diff(s);
2877 if (s->log_view == NULL)
2879 ls = &s->log_view->state.log;
2880 entry = TAILQ_PREV(ls->selected_entry,
2881 commit_queue_head, entry);
2885 err = input_log_view(NULL, NULL, NULL, s->log_view,
2890 err = set_selected_commit(s, entry);
2894 s->first_displayed_line = 1;
2895 s->last_displayed_line = view->nlines;
2897 diff_view_indicate_progress(view);
2898 err = create_diff(s);
2902 if (s->log_view == NULL)
2904 ls = &s->log_view->state.log;
2906 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2907 ls->thread_args.commits_needed++;
2909 /* Display "loading..." in log view. */
2910 show_log_view(s->log_view);
2914 err = trigger_log_thread(1 /* load_all */,
2915 &ls->thread_args.commits_needed,
2916 &ls->thread_args.log_complete,
2917 &ls->thread_args.need_commits);
2921 err = input_log_view(NULL, NULL, NULL, s->log_view,
2926 entry = TAILQ_NEXT(ls->selected_entry, entry);
2930 err = set_selected_commit(s, entry);
2934 s->first_displayed_line = 1;
2935 s->last_displayed_line = view->nlines;
2937 diff_view_indicate_progress(view);
2938 err = create_diff(s);
2947 static const struct got_error *
2948 cmd_diff(int argc, char *argv[])
2950 const struct got_error *error = NULL;
2951 struct got_repository *repo = NULL;
2952 struct got_reflist_head refs;
2953 struct got_object_id *id1 = NULL, *id2 = NULL;
2954 char *repo_path = NULL;
2955 char *id_str1 = NULL, *id_str2 = NULL;
2957 struct tog_view *view;
2959 SIMPLEQ_INIT(&refs);
2962 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2967 while ((ch = getopt(argc, argv, "")) != -1) {
2979 usage_diff(); /* TODO show local worktree changes */
2980 } else if (argc == 2) {
2981 repo_path = getcwd(NULL, 0);
2982 if (repo_path == NULL)
2983 return got_error_from_errno("getcwd");
2986 } else if (argc == 3) {
2987 repo_path = realpath(argv[0], NULL);
2988 if (repo_path == NULL)
2989 return got_error_from_errno2("realpath", argv[0]);
2997 error = got_repo_open(&repo, repo_path, NULL);
3001 error = apply_unveil(got_repo_get_path(repo), NULL);
3005 error = got_repo_match_object_id_prefix(&id1, id_str1,
3006 GOT_OBJ_TYPE_ANY, repo);
3010 error = got_repo_match_object_id_prefix(&id2, id_str2,
3011 GOT_OBJ_TYPE_ANY, repo);
3015 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3019 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3021 error = got_error_from_errno("view_open");
3024 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3027 error = view_loop(view);
3031 got_repo_close(repo);
3032 got_ref_list_free(&refs);
3040 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3045 struct tog_blame_line {
3047 struct got_object_id *id;
3050 static const struct got_error *
3051 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3052 const char *path, struct tog_blame_line *lines, int nlines,
3053 int blame_complete, int selected_line, int *first_displayed_line,
3054 int *last_displayed_line, int *eof, int max_lines)
3056 const struct got_error *err;
3057 int lineno = 0, nprinted = 0;
3062 struct tog_blame_line *blame_line;
3063 struct got_object_id *prev_id = NULL;
3066 err = got_object_id_str(&id_str, id);
3071 werase(view->window);
3073 if (asprintf(&line, "commit %s", id_str) == -1) {
3074 err = got_error_from_errno("asprintf");
3079 err = format_line(&wline, &width, line, view->ncols, 0);
3084 if (view_needs_focus_indication(view))
3085 wstandout(view->window);
3086 waddwstr(view->window, wline);
3087 if (view_needs_focus_indication(view))
3088 wstandend(view->window);
3091 if (width < view->ncols - 1)
3092 waddch(view->window, '\n');
3094 if (asprintf(&line, "[%d/%d] %s%s",
3095 *first_displayed_line - 1 + selected_line, nlines,
3096 blame_complete ? "" : "annotating... ", path) == -1) {
3098 return got_error_from_errno("asprintf");
3101 err = format_line(&wline, &width, line, view->ncols, 0);
3106 waddwstr(view->window, wline);
3109 if (width < view->ncols - 1)
3110 waddch(view->window, '\n');
3113 while (nprinted < max_lines - 2) {
3114 line = parse_next_line(f, &len);
3119 if (++lineno < *first_displayed_line) {
3124 if (view->ncols <= 9) {
3126 wline = wcsdup(L"");
3128 err = got_error_from_errno("wcsdup");
3130 err = format_line(&wline, &width, line,
3131 view->ncols - 9, 9);
3139 if (view->focussed && nprinted == selected_line - 1)
3140 wstandout(view->window);
3143 blame_line = &lines[lineno - 1];
3144 if (blame_line->annotated && prev_id &&
3145 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3147 nprinted == selected_line - 1)) {
3148 waddstr(view->window, " ");
3149 } else if (blame_line->annotated) {
3151 err = got_object_id_str(&id_str, blame_line->id);
3157 wprintw(view->window, "%.8s", id_str);
3159 prev_id = blame_line->id;
3161 waddstr(view->window, "........");
3165 waddstr(view->window, "........");
3169 if (view->focussed && nprinted == selected_line - 1)
3170 wstandend(view->window);
3171 waddstr(view->window, " ");
3173 waddwstr(view->window, wline);
3174 if (width <= view->ncols - 1)
3175 waddch(view->window, '\n');
3176 if (++nprinted == 1)
3177 *first_displayed_line = lineno;
3182 *last_displayed_line = lineno;
3189 static const struct got_error *
3190 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3192 const struct got_error *err = NULL;
3193 struct tog_blame_cb_args *a = arg;
3194 struct tog_blame_line *line;
3197 if (nlines != a->nlines ||
3198 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3199 return got_error(GOT_ERR_RANGE);
3201 errcode = pthread_mutex_lock(&tog_mutex);
3203 return got_error_set_errno(errcode, "pthread_mutex_lock");
3205 if (*a->quit) { /* user has quit the blame view */
3206 err = got_error(GOT_ERR_ITER_COMPLETED);
3211 goto done; /* no change in this commit */
3213 line = &a->lines[lineno - 1];
3214 if (line->annotated)
3217 line->id = got_object_id_dup(id);
3218 if (line->id == NULL) {
3219 err = got_error_from_errno("got_object_id_dup");
3222 line->annotated = 1;
3224 errcode = pthread_mutex_unlock(&tog_mutex);
3226 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3231 blame_thread(void *arg)
3233 const struct got_error *err;
3234 struct tog_blame_thread_args *ta = arg;
3235 struct tog_blame_cb_args *a = ta->cb_args;
3238 err = got_blame(ta->path, a->commit_id, ta->repo,
3239 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3240 if (err && err->code == GOT_ERR_CANCELLED)
3243 errcode = pthread_mutex_lock(&tog_mutex);
3245 return (void *)got_error_set_errno(errcode,
3246 "pthread_mutex_lock");
3248 got_repo_close(ta->repo);
3252 errcode = pthread_mutex_unlock(&tog_mutex);
3253 if (errcode && err == NULL)
3254 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3259 static struct got_object_id *
3260 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3261 int first_displayed_line, int selected_line)
3263 struct tog_blame_line *line;
3268 line = &lines[first_displayed_line - 1 + selected_line - 1];
3269 if (!line->annotated)
3275 static const struct got_error *
3276 stop_blame(struct tog_blame *blame)
3278 const struct got_error *err = NULL;
3281 if (blame->thread) {
3283 errcode = pthread_mutex_unlock(&tog_mutex);
3285 return got_error_set_errno(errcode,
3286 "pthread_mutex_unlock");
3287 errcode = pthread_join(blame->thread, (void **)&err);
3289 return got_error_set_errno(errcode, "pthread_join");
3290 errcode = pthread_mutex_lock(&tog_mutex);
3292 return got_error_set_errno(errcode,
3293 "pthread_mutex_lock");
3294 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3296 blame->thread = NULL;
3298 if (blame->thread_args.repo) {
3299 got_repo_close(blame->thread_args.repo);
3300 blame->thread_args.repo = NULL;
3303 if (fclose(blame->f) != 0 && err == NULL)
3304 err = got_error_from_errno("fclose");
3308 for (i = 0; i < blame->nlines; i++)
3309 free(blame->lines[i].id);
3311 blame->lines = NULL;
3313 free(blame->cb_args.commit_id);
3314 blame->cb_args.commit_id = NULL;
3319 static const struct got_error *
3320 cancel_blame_view(void *arg)
3322 const struct got_error *err = NULL;
3326 errcode = pthread_mutex_lock(&tog_mutex);
3328 return got_error_set_errno(errcode,
3329 "pthread_mutex_unlock");
3332 err = got_error(GOT_ERR_CANCELLED);
3334 errcode = pthread_mutex_unlock(&tog_mutex);
3336 return got_error_set_errno(errcode,
3337 "pthread_mutex_lock");
3342 static const struct got_error *
3343 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3344 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3345 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3346 struct got_repository *repo)
3348 const struct got_error *err = NULL;
3349 struct got_blob_object *blob = NULL;
3350 struct got_repository *thread_repo = NULL;
3351 struct got_object_id *obj_id = NULL;
3354 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3358 return got_error(GOT_ERR_NO_OBJ);
3360 err = got_object_get_type(&obj_type, repo, obj_id);
3364 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 err = got_error(GOT_ERR_OBJ_TYPE);
3369 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3372 blame->f = got_opentemp();
3373 if (blame->f == NULL) {
3374 err = got_error_from_errno("got_opentemp");
3377 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3378 &blame->line_offsets, blame->f, blob);
3379 if (err || blame->nlines == 0)
3382 /* Don't include \n at EOF in the blame line count. */
3383 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3386 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3387 if (blame->lines == NULL) {
3388 err = got_error_from_errno("calloc");
3392 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3396 blame->cb_args.view = view;
3397 blame->cb_args.lines = blame->lines;
3398 blame->cb_args.nlines = blame->nlines;
3399 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3400 if (blame->cb_args.commit_id == NULL) {
3401 err = got_error_from_errno("got_object_id_dup");
3404 blame->cb_args.quit = done;
3406 blame->thread_args.path = path;
3407 blame->thread_args.repo = thread_repo;
3408 blame->thread_args.cb_args = &blame->cb_args;
3409 blame->thread_args.complete = blame_complete;
3410 blame->thread_args.cancel_cb = cancel_blame_view;
3411 blame->thread_args.cancel_arg = done;
3412 *blame_complete = 0;
3416 got_object_blob_close(blob);
3423 static const struct got_error *
3424 open_blame_view(struct tog_view *view, char *path,
3425 struct got_object_id *commit_id, struct got_reflist_head *refs,
3426 struct got_repository *repo)
3428 const struct got_error *err = NULL;
3429 struct tog_blame_view_state *s = &view->state.blame;
3431 SIMPLEQ_INIT(&s->blamed_commits);
3433 s->path = strdup(path);
3434 if (s->path == NULL)
3435 return got_error_from_errno("strdup");
3437 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3443 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3444 s->first_displayed_line = 1;
3445 s->last_displayed_line = view->nlines;
3446 s->selected_line = 1;
3447 s->blame_complete = 0;
3450 s->commit_id = commit_id;
3451 memset(&s->blame, 0, sizeof(s->blame));
3453 view->show = show_blame_view;
3454 view->input = input_blame_view;
3455 view->close = close_blame_view;
3456 view->search_start = search_start_blame_view;
3457 view->search_next = search_next_blame_view;
3459 return run_blame(&s->blame, view, &s->blame_complete,
3460 &s->first_displayed_line, &s->last_displayed_line,
3461 &s->selected_line, &s->done, &s->eof, s->path,
3462 s->blamed_commit->id, s->repo);
3465 static const struct got_error *
3466 close_blame_view(struct tog_view *view)
3468 const struct got_error *err = NULL;
3469 struct tog_blame_view_state *s = &view->state.blame;
3471 if (s->blame.thread)
3472 err = stop_blame(&s->blame);
3474 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3475 struct got_object_qid *blamed_commit;
3476 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3477 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3478 got_object_qid_free(blamed_commit);
3486 static const struct got_error *
3487 search_start_blame_view(struct tog_view *view)
3489 struct tog_blame_view_state *s = &view->state.blame;
3491 s->matched_line = 0;
3496 match_line(const char *line, regex_t *regex)
3498 regmatch_t regmatch;
3500 return regexec(regex, line, 1, ®match, 0) == 0;
3504 static const struct got_error *
3505 search_next_blame_view(struct tog_view *view)
3507 struct tog_blame_view_state *s = &view->state.blame;
3510 if (!view->searching) {
3511 view->search_next_done = 1;
3515 if (s->matched_line) {
3516 if (view->searching == TOG_SEARCH_FORWARD)
3517 lineno = s->matched_line + 1;
3519 lineno = s->matched_line - 1;
3521 if (view->searching == TOG_SEARCH_FORWARD)
3524 lineno = s->blame.nlines;
3532 if (lineno <= 0 || lineno > s->blame.nlines) {
3533 if (s->matched_line == 0) {
3534 view->search_next_done = 1;
3539 if (view->searching == TOG_SEARCH_FORWARD)
3542 lineno = s->blame.nlines;
3545 offset = s->blame.line_offsets[lineno - 1];
3546 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3548 return got_error_from_errno("fseeko");
3551 line = parse_next_line(s->blame.f, &len);
3552 if (line && match_line(line, &view->regex)) {
3553 view->search_next_done = 1;
3554 s->matched_line = lineno;
3559 if (view->searching == TOG_SEARCH_FORWARD)
3565 if (s->matched_line) {
3566 s->first_displayed_line = s->matched_line;
3567 s->selected_line = 1;
3573 static const struct got_error *
3574 show_blame_view(struct tog_view *view)
3576 const struct got_error *err = NULL;
3577 struct tog_blame_view_state *s = &view->state.blame;
3580 if (s->blame.thread == NULL) {
3581 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3582 &s->blame.thread_args);
3584 return got_error_set_errno(errcode, "pthread_create");
3586 halfdelay(1); /* fast refresh while annotating */
3589 if (s->blame_complete)
3590 halfdelay(10); /* disable fast refresh */
3592 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3593 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3594 s->selected_line, &s->first_displayed_line,
3595 &s->last_displayed_line, &s->eof, view->nlines);
3601 static const struct got_error *
3602 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3603 struct tog_view **focus_view, struct tog_view *view, int ch)
3605 const struct got_error *err = NULL, *thread_err = NULL;
3606 struct tog_view *diff_view;
3607 struct tog_blame_view_state *s = &view->state.blame;
3616 if (s->selected_line > 1)
3618 else if (s->selected_line == 1 &&
3619 s->first_displayed_line > 1)
3620 s->first_displayed_line--;
3623 if (s->first_displayed_line == 1) {
3624 s->selected_line = 1;
3627 if (s->first_displayed_line > view->nlines - 2)
3628 s->first_displayed_line -=
3631 s->first_displayed_line = 1;
3635 if (s->selected_line < view->nlines - 2 &&
3636 s->first_displayed_line +
3637 s->selected_line <= s->blame.nlines)
3639 else if (s->last_displayed_line <
3641 s->first_displayed_line++;
3645 struct got_object_id *id = NULL;
3646 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3647 s->first_displayed_line, s->selected_line);
3651 struct got_commit_object *commit;
3652 struct got_object_qid *pid;
3653 struct got_object_id *blob_id = NULL;
3655 err = got_object_open_as_commit(&commit,
3659 pid = SIMPLEQ_FIRST(
3660 got_object_commit_get_parent_ids(commit));
3662 got_object_commit_close(commit);
3665 /* Check if path history ends here. */
3666 err = got_object_id_by_path(&blob_id, s->repo,
3669 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3671 got_object_commit_close(commit);
3674 err = got_object_get_type(&obj_type, s->repo,
3677 /* Can't blame non-blob type objects. */
3678 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3679 got_object_commit_close(commit);
3682 err = got_object_qid_alloc(&s->blamed_commit,
3684 got_object_commit_close(commit);
3686 if (got_object_id_cmp(id,
3687 s->blamed_commit->id) == 0)
3689 err = got_object_qid_alloc(&s->blamed_commit,
3695 thread_err = stop_blame(&s->blame);
3699 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3700 s->blamed_commit, entry);
3701 err = run_blame(&s->blame, view, &s->blame_complete,
3702 &s->first_displayed_line, &s->last_displayed_line,
3703 &s->selected_line, &s->done, &s->eof,
3704 s->path, s->blamed_commit->id, s->repo);
3710 struct got_object_qid *first;
3711 first = SIMPLEQ_FIRST(&s->blamed_commits);
3712 if (!got_object_id_cmp(first->id, s->commit_id))
3715 thread_err = stop_blame(&s->blame);
3719 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3720 got_object_qid_free(s->blamed_commit);
3722 SIMPLEQ_FIRST(&s->blamed_commits);
3723 err = run_blame(&s->blame, view, &s->blame_complete,
3724 &s->first_displayed_line, &s->last_displayed_line,
3725 &s->selected_line, &s->done, &s->eof, s->path,
3726 s->blamed_commit->id, s->repo);
3733 struct got_object_id *id = NULL;
3734 struct got_object_qid *pid;
3735 struct got_commit_object *commit = NULL;
3736 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3737 s->first_displayed_line, s->selected_line);
3740 err = got_object_open_as_commit(&commit, s->repo, id);
3743 pid = SIMPLEQ_FIRST(
3744 got_object_commit_get_parent_ids(commit));
3745 if (view_is_parent_view(view))
3746 begin_x = view_split_begin_x(view->begin_x);
3747 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3748 if (diff_view == NULL) {
3749 got_object_commit_close(commit);
3750 err = got_error_from_errno("view_open");
3753 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3754 id, NULL, s->refs, s->repo);
3755 got_object_commit_close(commit);
3757 view_close(diff_view);
3760 if (view_is_parent_view(view)) {
3761 err = view_close_child(view);
3764 err = view_set_child(view, diff_view);
3766 view_close(diff_view);
3769 *focus_view = diff_view;
3770 view->child_focussed = 1;
3772 *new_view = diff_view;
3779 if (s->last_displayed_line >= s->blame.nlines &&
3780 s->selected_line >= MIN(s->blame.nlines,
3781 view->nlines - 2)) {
3784 if (s->last_displayed_line >= s->blame.nlines &&
3785 s->selected_line < view->nlines - 2) {
3786 s->selected_line = MIN(s->blame.nlines,
3790 if (s->last_displayed_line + view->nlines - 2
3792 s->first_displayed_line +=
3795 s->first_displayed_line =
3800 if (s->selected_line > view->nlines - 2) {
3801 s->selected_line = MIN(s->blame.nlines,
3808 return thread_err ? thread_err : err;
3811 static const struct got_error *
3812 cmd_blame(int argc, char *argv[])
3814 const struct got_error *error;
3815 struct got_repository *repo = NULL;
3816 struct got_reflist_head refs;
3817 struct got_worktree *worktree = NULL;
3818 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3819 struct got_object_id *commit_id = NULL;
3820 char *commit_id_str = NULL;
3822 struct tog_view *view;
3824 SIMPLEQ_INIT(&refs);
3827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3832 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3835 commit_id_str = optarg;
3838 repo_path = realpath(optarg, NULL);
3839 if (repo_path == NULL)
3840 return got_error_from_errno2("realpath",
3857 cwd = getcwd(NULL, 0);
3859 error = got_error_from_errno("getcwd");
3862 if (repo_path == NULL) {
3863 error = got_worktree_open(&worktree, cwd);
3864 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3870 strdup(got_worktree_get_repo_path(worktree));
3871 if (repo_path == NULL)
3872 error = got_error_from_errno("strdup");
3876 repo_path = strdup(cwd);
3877 if (repo_path == NULL) {
3878 error = got_error_from_errno("strdup");
3886 error = got_repo_open(&repo, repo_path, NULL);
3890 error = apply_unveil(got_repo_get_path(repo), NULL);
3895 const char *prefix = got_worktree_get_path_prefix(worktree);
3896 char *p, *worktree_subdir = cwd +
3897 strlen(got_worktree_get_root_path(worktree));
3898 if (asprintf(&p, "%s%s%s%s%s",
3899 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3900 worktree_subdir, worktree_subdir[0] ? "/" : "",
3902 error = got_error_from_errno("asprintf");
3905 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3908 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3913 if (commit_id_str == NULL) {
3914 struct got_reference *head_ref;
3915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3918 error = got_ref_resolve(&commit_id, repo, head_ref);
3919 got_ref_close(head_ref);
3921 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3923 if (error->code != GOT_ERR_NOT_REF)
3925 error = got_repo_match_object_id_prefix(&commit_id,
3926 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3932 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3936 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3938 error = got_error_from_errno("view_open");
3941 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3945 /* Release work tree lock. */
3946 got_worktree_close(worktree);
3949 error = view_loop(view);
3955 got_worktree_close(worktree);
3957 got_repo_close(repo);
3958 got_ref_list_free(&refs);
3962 static const struct got_error *
3963 draw_tree_entries(struct tog_view *view,
3964 struct got_tree_entry **first_displayed_entry,
3965 struct got_tree_entry **last_displayed_entry,
3966 struct got_tree_entry **selected_entry, int *ndisplayed,
3967 const char *label, int show_ids, const char *parent_path,
3968 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3970 const struct got_error *err = NULL;
3971 struct got_tree_entry *te;
3977 werase(view->window);
3982 err = format_line(&wline, &width, label, view->ncols, 0);
3985 if (view_needs_focus_indication(view))
3986 wstandout(view->window);
3987 waddwstr(view->window, wline);
3988 if (view_needs_focus_indication(view))
3989 wstandend(view->window);
3992 if (width < view->ncols - 1)
3993 waddch(view->window, '\n');
3996 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3999 waddwstr(view->window, wline);
4002 if (width < view->ncols - 1)
4003 waddch(view->window, '\n');
4006 waddch(view->window, '\n');
4010 te = SIMPLEQ_FIRST(&entries->head);
4011 if (*first_displayed_entry == NULL) {
4012 if (selected == 0) {
4014 wstandout(view->window);
4015 *selected_entry = NULL;
4017 waddstr(view->window, " ..\n"); /* parent directory */
4018 if (selected == 0 && view->focussed)
4019 wstandend(view->window);
4026 while (te != *first_displayed_entry)
4027 te = SIMPLEQ_NEXT(te, entry);
4031 char *line = NULL, *id_str = NULL;
4032 const char *modestr = "";
4035 err = got_object_id_str(&id_str, te->id);
4037 return got_error_from_errno(
4038 "got_object_id_str");
4040 if (got_object_tree_entry_is_submodule(te))
4042 else if (S_ISLNK(te->mode))
4044 else if (S_ISDIR(te->mode))
4046 else if (te->mode & S_IXUSR)
4048 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4049 te->name, modestr) == -1) {
4051 return got_error_from_errno("asprintf");
4054 err = format_line(&wline, &width, line, view->ncols, 0);
4059 if (n == selected) {
4061 wstandout(view->window);
4062 *selected_entry = te;
4064 waddwstr(view->window, wline);
4065 if (width < view->ncols - 1)
4066 waddch(view->window, '\n');
4067 if (n == selected && view->focussed)
4068 wstandend(view->window);
4074 *last_displayed_entry = te;
4077 te = SIMPLEQ_NEXT(te, entry);
4084 tree_scroll_up(struct tog_view *view,
4085 struct got_tree_entry **first_displayed_entry, int maxscroll,
4086 const struct got_tree_entries *entries, int isroot)
4088 struct got_tree_entry *te, *prev;
4091 if (*first_displayed_entry == NULL)
4094 te = SIMPLEQ_FIRST(&entries->head);
4095 if (*first_displayed_entry == te) {
4097 *first_displayed_entry = NULL;
4101 /* XXX this is stupid... switch to TAILQ? */
4102 for (i = 0; i < maxscroll; i++) {
4103 while (te != *first_displayed_entry) {
4105 te = SIMPLEQ_NEXT(te, entry);
4107 *first_displayed_entry = prev;
4108 te = SIMPLEQ_FIRST(&entries->head);
4110 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4111 *first_displayed_entry = NULL;
4115 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4116 struct got_tree_entry *last_displayed_entry,
4117 const struct got_tree_entries *entries)
4119 struct got_tree_entry *next, *last;
4122 if (*first_displayed_entry)
4123 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4125 next = SIMPLEQ_FIRST(&entries->head);
4126 last = last_displayed_entry;
4127 while (next && last && n++ < maxscroll) {
4128 last = SIMPLEQ_NEXT(last, entry);
4130 *first_displayed_entry = next;
4131 next = SIMPLEQ_NEXT(next, entry);
4137 static const struct got_error *
4138 tree_entry_path(char **path, struct tog_parent_trees *parents,
4139 struct got_tree_entry *te)
4141 const struct got_error *err = NULL;
4142 struct tog_parent_tree *pt;
4143 size_t len = 2; /* for leading slash and NUL */
4145 TAILQ_FOREACH(pt, parents, entry)
4146 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4148 len += strlen(te->name);
4150 *path = calloc(1, len);
4152 return got_error_from_errno("calloc");
4155 pt = TAILQ_LAST(parents, tog_parent_trees);
4157 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4158 err = got_error(GOT_ERR_NO_SPACE);
4161 if (strlcat(*path, "/", len) >= len) {
4162 err = got_error(GOT_ERR_NO_SPACE);
4165 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4168 if (strlcat(*path, te->name, len) >= len) {
4169 err = got_error(GOT_ERR_NO_SPACE);
4181 static const struct got_error *
4182 blame_tree_entry(struct tog_view **new_view, int begin_x,
4183 struct got_tree_entry *te, struct tog_parent_trees *parents,
4184 struct got_object_id *commit_id, struct got_reflist_head *refs,
4185 struct got_repository *repo)
4187 const struct got_error *err = NULL;
4189 struct tog_view *blame_view;
4193 err = tree_entry_path(&path, parents, te);
4197 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4198 if (blame_view == NULL) {
4199 err = got_error_from_errno("view_open");
4203 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4205 if (err->code == GOT_ERR_CANCELLED)
4207 view_close(blame_view);
4209 *new_view = blame_view;
4215 static const struct got_error *
4216 log_tree_entry(struct tog_view **new_view, int begin_x,
4217 struct got_tree_entry *te, struct tog_parent_trees *parents,
4218 struct got_object_id *commit_id, struct got_reflist_head *refs,
4219 struct got_repository *repo)
4221 struct tog_view *log_view;
4222 const struct got_error *err = NULL;
4227 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4228 if (log_view == NULL)
4229 return got_error_from_errno("view_open");
4231 err = tree_entry_path(&path, parents, te);
4235 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4237 view_close(log_view);
4239 *new_view = log_view;
4244 static const struct got_error *
4245 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4246 struct got_object_id *commit_id, struct got_reflist_head *refs,
4247 struct got_repository *repo)
4249 const struct got_error *err = NULL;
4250 char *commit_id_str = NULL;
4251 struct tog_tree_view_state *s = &view->state.tree;
4253 TAILQ_INIT(&s->parents);
4255 err = got_object_id_str(&commit_id_str, commit_id);
4259 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4260 err = got_error_from_errno("asprintf");
4264 s->root = s->tree = root;
4265 s->entries = got_object_tree_get_entries(root);
4266 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4267 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4268 s->commit_id = got_object_id_dup(commit_id);
4269 if (s->commit_id == NULL) {
4270 err = got_error_from_errno("got_object_id_dup");
4276 view->show = show_tree_view;
4277 view->input = input_tree_view;
4278 view->close = close_tree_view;
4279 view->search_start = search_start_tree_view;
4280 view->search_next = search_next_tree_view;
4282 free(commit_id_str);
4284 free(s->tree_label);
4285 s->tree_label = NULL;
4290 static const struct got_error *
4291 close_tree_view(struct tog_view *view)
4293 struct tog_tree_view_state *s = &view->state.tree;
4295 free(s->tree_label);
4296 s->tree_label = NULL;
4298 s->commit_id = NULL;
4299 while (!TAILQ_EMPTY(&s->parents)) {
4300 struct tog_parent_tree *parent;
4301 parent = TAILQ_FIRST(&s->parents);
4302 TAILQ_REMOVE(&s->parents, parent, entry);
4306 if (s->tree != s->root)
4307 got_object_tree_close(s->tree);
4308 got_object_tree_close(s->root);
4313 static const struct got_error *
4314 search_start_tree_view(struct tog_view *view)
4316 struct tog_tree_view_state *s = &view->state.tree;
4318 s->matched_entry = NULL;
4323 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4325 regmatch_t regmatch;
4327 return regexec(regex, te->name, 1, ®match, 0) == 0;
4330 static const struct got_error *
4331 search_next_tree_view(struct tog_view *view)
4333 struct tog_tree_view_state *s = &view->state.tree;
4334 struct got_tree_entry *entry = NULL, *te;
4336 if (!view->searching) {
4337 view->search_next_done = 1;
4341 if (s->matched_entry) {
4342 if (view->searching == TOG_SEARCH_FORWARD) {
4343 if (s->selected_entry)
4344 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4346 entry = SIMPLEQ_FIRST(&s->entries->head);
4349 if (s->selected_entry == NULL) {
4350 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4353 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4355 if (SIMPLEQ_NEXT(te, entry) ==
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 entry = SIMPLEQ_FIRST(&s->entries->head);
4365 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4371 if (entry == NULL) {
4372 if (s->matched_entry == NULL) {
4373 view->search_next_done = 1;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4377 entry = SIMPLEQ_FIRST(&s->entries->head);
4379 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4384 if (match_tree_entry(entry, &view->regex)) {
4385 view->search_next_done = 1;
4386 s->matched_entry = entry;
4390 if (view->searching == TOG_SEARCH_FORWARD)
4391 entry = SIMPLEQ_NEXT(entry, entry);
4393 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4396 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4397 if (SIMPLEQ_NEXT(te, entry) == entry) {
4406 if (s->matched_entry) {
4407 s->first_displayed_entry = s->matched_entry;
4414 static const struct got_error *
4415 show_tree_view(struct tog_view *view)
4417 const struct got_error *err = NULL;
4418 struct tog_tree_view_state *s = &view->state.tree;
4421 err = tree_entry_path(&parent_path, &s->parents, NULL);
4425 err = draw_tree_entries(view, &s->first_displayed_entry,
4426 &s->last_displayed_entry, &s->selected_entry,
4427 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4428 s->entries, s->selected, view->nlines, s->tree == s->root);
4435 static const struct got_error *
4436 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4437 struct tog_view **focus_view, struct tog_view *view, int ch)
4439 const struct got_error *err = NULL;
4440 struct tog_tree_view_state *s = &view->state.tree;
4441 struct tog_view *log_view;
4442 int begin_x = 0, nscrolled;
4446 s->show_ids = !s->show_ids;
4449 if (!s->selected_entry)
4451 if (view_is_parent_view(view))
4452 begin_x = view_split_begin_x(view->begin_x);
4453 err = log_tree_entry(&log_view, begin_x,
4454 s->selected_entry, &s->parents,
4455 s->commit_id, s->refs, s->repo);
4456 if (view_is_parent_view(view)) {
4457 err = view_close_child(view);
4460 err = view_set_child(view, log_view);
4462 view_close(log_view);
4465 *focus_view = log_view;
4466 view->child_focussed = 1;
4468 *new_view = log_view;
4472 if (s->selected > 0) {
4474 if (s->selected == 0)
4477 if (s->selected > 0)
4479 tree_scroll_up(view, &s->first_displayed_entry, 1,
4480 s->entries, s->tree == s->root);
4483 tree_scroll_up(view, &s->first_displayed_entry,
4484 MAX(0, view->nlines - 4 - s->selected), s->entries,
4485 s->tree == s->root);
4487 if (SIMPLEQ_FIRST(&s->entries->head) ==
4488 s->first_displayed_entry && s->tree != s->root)
4489 s->first_displayed_entry = NULL;
4493 if (s->selected < s->ndisplayed - 1) {
4497 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4498 /* can't scroll any further */
4500 tree_scroll_down(&s->first_displayed_entry, 1,
4501 s->last_displayed_entry, s->entries);
4504 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4506 /* can't scroll any further; move cursor down */
4507 if (s->selected < s->ndisplayed - 1)
4508 s->selected = s->ndisplayed - 1;
4511 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4512 view->nlines, s->last_displayed_entry, s->entries);
4513 if (nscrolled < view->nlines) {
4515 struct got_tree_entry *te;
4516 te = s->first_displayed_entry;
4519 te = SIMPLEQ_NEXT(te, entry);
4521 s->selected = ndisplayed - 1;
4527 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4528 struct tog_parent_tree *parent;
4529 /* user selected '..' */
4530 if (s->tree == s->root)
4532 parent = TAILQ_FIRST(&s->parents);
4533 TAILQ_REMOVE(&s->parents, parent,
4535 got_object_tree_close(s->tree);
4536 s->tree = parent->tree;
4538 got_object_tree_get_entries(s->tree);
4539 s->first_displayed_entry =
4540 parent->first_displayed_entry;
4542 parent->selected_entry;
4543 s->selected = parent->selected;
4545 } else if (S_ISDIR(s->selected_entry->mode)) {
4546 struct got_tree_object *subtree;
4547 err = got_object_open_as_tree(&subtree,
4548 s->repo, s->selected_entry->id);
4551 err = tree_view_visit_subtree(subtree, s);
4553 got_object_tree_close(subtree);
4556 } else if (S_ISREG(s->selected_entry->mode)) {
4557 struct tog_view *blame_view;
4558 int begin_x = view_is_parent_view(view) ?
4559 view_split_begin_x(view->begin_x) : 0;
4561 err = blame_tree_entry(&blame_view, begin_x,
4562 s->selected_entry, &s->parents,
4563 s->commit_id, s->refs, s->repo);
4566 if (view_is_parent_view(view)) {
4567 err = view_close_child(view);
4570 err = view_set_child(view, blame_view);
4572 view_close(blame_view);
4575 *focus_view = blame_view;
4576 view->child_focussed = 1;
4578 *new_view = blame_view;
4582 if (s->selected > view->nlines)
4583 s->selected = s->ndisplayed - 1;
4596 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4601 static const struct got_error *
4602 cmd_tree(int argc, char *argv[])
4604 const struct got_error *error;
4605 struct got_repository *repo = NULL;
4606 struct got_reflist_head refs;
4607 char *repo_path = NULL;
4608 struct got_object_id *commit_id = NULL;
4609 char *commit_id_arg = NULL;
4610 struct got_commit_object *commit = NULL;
4611 struct got_tree_object *tree = NULL;
4613 struct tog_view *view;
4615 SIMPLEQ_INIT(&refs);
4618 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4623 while ((ch = getopt(argc, argv, "c:")) != -1) {
4626 commit_id_arg = optarg;
4638 struct got_worktree *worktree;
4639 char *cwd = getcwd(NULL, 0);
4641 return got_error_from_errno("getcwd");
4642 error = got_worktree_open(&worktree, cwd);
4643 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4648 strdup(got_worktree_get_repo_path(worktree));
4649 got_worktree_close(worktree);
4652 if (repo_path == NULL) {
4653 error = got_error_from_errno("strdup");
4656 } else if (argc == 1) {
4657 repo_path = realpath(argv[0], NULL);
4658 if (repo_path == NULL)
4659 return got_error_from_errno2("realpath", argv[0]);
4665 error = got_repo_open(&repo, repo_path, NULL);
4669 error = apply_unveil(got_repo_get_path(repo), NULL);
4673 if (commit_id_arg == NULL)
4674 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4676 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4678 if (error->code != GOT_ERR_NOT_REF)
4680 error = got_repo_match_object_id_prefix(&commit_id,
4681 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4687 error = got_object_open_as_commit(&commit, repo, commit_id);
4691 error = got_object_open_as_tree(&tree, repo,
4692 got_object_commit_get_tree_id(commit));
4696 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4700 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4702 error = got_error_from_errno("view_open");
4705 error = open_tree_view(view, tree, commit_id, &refs, repo);
4708 error = view_loop(view);
4713 got_object_commit_close(commit);
4715 got_object_tree_close(tree);
4717 got_repo_close(repo);
4718 got_ref_list_free(&refs);
4727 fprintf(stderr, "commands:");
4728 for (i = 0; i < nitems(tog_commands); i++) {
4729 struct tog_cmd *cmd = &tog_commands[i];
4730 fprintf(stderr, " %s", cmd->name);
4732 fputc('\n', stderr);
4738 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4746 make_argv(const char *arg0, const char *arg1)
4749 int argc = (arg1 == NULL ? 1 : 2);
4751 argv = calloc(argc, sizeof(char *));
4754 argv[0] = strdup(arg0);
4755 if (argv[0] == NULL)
4758 argv[1] = strdup(arg1);
4759 if (argv[1] == NULL)
4767 main(int argc, char *argv[])
4769 const struct got_error *error = NULL;
4770 struct tog_cmd *cmd = NULL;
4771 int ch, hflag = 0, Vflag = 0;
4772 char **cmd_argv = NULL;
4774 setlocale(LC_CTYPE, "");
4776 while ((ch = getopt(argc, argv, "hV")) != -1) {
4796 got_version_print_str();
4803 /* Build an argument vector which runs a default command. */
4804 cmd = &tog_commands[0];
4805 cmd_argv = make_argv(cmd->name, NULL);
4810 /* Did the user specific a command? */
4811 for (i = 0; i < nitems(tog_commands); i++) {
4812 if (strncmp(tog_commands[i].name, argv[0],
4813 strlen(argv[0])) == 0) {
4814 cmd = &tog_commands[i];
4820 fprintf(stderr, "%s: unknown command '%s'\n",
4821 getprogname(), argv[0]);
4830 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4834 if (error && error->code != GOT_ERR_CANCELLED)
4835 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);