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);
1091 author = strdup(got_object_commit_get_author(commit));
1092 if (author == NULL) {
1093 err = got_error_from_errno("strdup");
1096 err = format_author(&wauthor, &author_width, author, avail - col, col);
1099 waddwstr(view->window, wauthor);
1100 col += author_width;
1101 while (col < avail && author_width < author_display_cols + 2) {
1102 waddch(view->window, ' ');
1109 err = got_object_commit_get_logmsg(&logmsg0, commit);
1113 while (*logmsg == '\n')
1115 newline = strchr(logmsg, '\n');
1118 limit = avail - col;
1119 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1122 waddwstr(view->window, wlogmsg);
1123 col += logmsg_width;
1124 while (col < avail) {
1125 waddch(view->window, ' ');
1137 static struct commit_queue_entry *
1138 alloc_commit_queue_entry(struct got_commit_object *commit,
1139 struct got_object_id *id)
1141 struct commit_queue_entry *entry;
1143 entry = calloc(1, sizeof(*entry));
1148 entry->commit = commit;
1153 pop_commit(struct commit_queue *commits)
1155 struct commit_queue_entry *entry;
1157 entry = TAILQ_FIRST(&commits->head);
1158 TAILQ_REMOVE(&commits->head, entry, entry);
1159 got_object_commit_close(entry->commit);
1160 commits->ncommits--;
1161 /* Don't free entry->id! It is owned by the commit graph. */
1166 free_commits(struct commit_queue *commits)
1168 while (!TAILQ_EMPTY(&commits->head))
1169 pop_commit(commits);
1172 static const struct got_error *
1173 match_commit(int *have_match, struct got_object_id *id,
1174 struct got_commit_object *commit, regex_t *regex)
1176 const struct got_error *err = NULL;
1177 regmatch_t regmatch;
1178 char *id_str = NULL, *logmsg = NULL;
1182 err = got_object_id_str(&id_str, id);
1186 err = got_object_commit_get_logmsg(&logmsg, commit);
1190 if (regexec(regex, got_object_commit_get_author(commit), 1,
1191 ®match, 0) == 0 ||
1192 regexec(regex, got_object_commit_get_committer(commit), 1,
1193 ®match, 0) == 0 ||
1194 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1195 regexec(regex, logmsg, 1, ®match, 0) == 0)
1203 static const struct got_error *
1204 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1205 int minqueue, struct got_repository *repo, const char *path,
1206 int *searching, int *search_next_done, regex_t *regex)
1208 const struct got_error *err = NULL;
1209 int nqueued = 0, have_match = 0;
1212 * We keep all commits open throughout the lifetime of the log
1213 * view in order to avoid having to re-fetch commits from disk
1214 * while updating the display.
1216 while (nqueued < minqueue ||
1217 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1218 struct got_object_id *id;
1219 struct got_commit_object *commit;
1220 struct commit_queue_entry *entry;
1223 err = got_commit_graph_iter_next(&id, graph);
1225 if (err->code != GOT_ERR_ITER_NEED_MORE)
1227 err = got_commit_graph_fetch_commits(graph,
1228 minqueue, repo, NULL, NULL);
1237 err = got_object_open_as_commit(&commit, repo, id);
1240 entry = alloc_commit_queue_entry(commit, id);
1241 if (entry == NULL) {
1242 err = got_error_from_errno("alloc_commit_queue_entry");
1246 errcode = pthread_mutex_lock(&tog_mutex);
1248 err = got_error_set_errno(errcode,
1249 "pthread_mutex_lock");
1253 entry->idx = commits->ncommits;
1254 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1256 commits->ncommits++;
1258 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1259 err = match_commit(&have_match, id, commit, regex);
1261 pthread_mutex_lock(&tog_mutex);
1266 errcode = pthread_mutex_unlock(&tog_mutex);
1267 if (errcode && err == NULL)
1268 err = got_error_set_errno(errcode,
1269 "pthread_mutex_unlock");
1278 static const struct got_error *
1279 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1280 struct got_repository *repo)
1282 const struct got_error *err = NULL;
1283 struct got_reference *head_ref;
1287 err = got_ref_open(&head_ref, repo, branch_name, 0);
1291 err = got_ref_resolve(head_id, repo, head_ref);
1292 got_ref_close(head_ref);
1301 static const struct got_error *
1302 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1303 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1304 struct commit_queue *commits, int selected_idx, int limit,
1305 struct got_reflist_head *refs, const char *path, int commits_needed)
1307 const struct got_error *err = NULL;
1308 struct tog_log_view_state *s = &view->state.log;
1309 struct commit_queue_entry *entry;
1311 int ncommits, author_cols = 10;
1312 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1313 char *refs_str = NULL;
1315 static const size_t date_display_cols = 9;
1320 if (ncommits == selected_idx) {
1324 entry = TAILQ_NEXT(entry, entry);
1328 if (*selected && !(view->searching && view->search_next_done == 0)) {
1329 err = got_object_id_str(&id_str, (*selected)->id);
1333 err = build_refs_str(&refs_str, refs, (*selected)->id,
1340 if (commits_needed == 0)
1341 halfdelay(10); /* disable fast refresh */
1343 if (asprintf(&ncommits_str, " [%d/%d] %s",
1344 entry ? entry->idx + 1 : 0, commits->ncommits,
1345 commits_needed > 0 ?
1346 (view->searching && view->search_next_done == 0
1347 ? "searching..." : "loading... ") :
1348 (refs_str ? refs_str : "")) == -1) {
1349 err = got_error_from_errno("asprintf");
1353 if (path && strcmp(path, "/") != 0) {
1354 if (asprintf(&header, "commit %s %s%s",
1355 id_str ? id_str : "........................................",
1356 path, ncommits_str) == -1) {
1357 err = got_error_from_errno("asprintf");
1361 } else if (asprintf(&header, "commit %s%s",
1362 id_str ? id_str : "........................................",
1363 ncommits_str) == -1) {
1364 err = got_error_from_errno("asprintf");
1368 err = format_line(&wline, &width, header, view->ncols, 0);
1372 werase(view->window);
1374 if (view_needs_focus_indication(view))
1375 wstandout(view->window);
1376 waddwstr(view->window, wline);
1377 while (width < view->ncols) {
1378 waddch(view->window, ' ');
1381 if (view_needs_focus_indication(view))
1382 wstandend(view->window);
1387 /* Grow author column size if necessary. */
1394 if (ncommits >= limit - 1)
1396 author = strdup(got_object_commit_get_author(entry->commit));
1397 if (author == NULL) {
1398 err = got_error_from_errno("strdup");
1401 err = format_author(&wauthor, &width, author, COLS,
1403 if (author_cols < width)
1404 author_cols = width;
1407 entry = TAILQ_NEXT(entry, entry);
1414 if (ncommits >= limit - 1)
1416 if (ncommits == selected_idx)
1417 wstandout(view->window);
1418 err = draw_commit(view, entry->commit, entry->id, refs,
1419 date_display_cols, author_cols);
1420 if (ncommits == selected_idx)
1421 wstandend(view->window);
1426 entry = TAILQ_NEXT(entry, entry);
1439 scroll_up(struct tog_view *view,
1440 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1441 struct commit_queue *commits)
1443 struct commit_queue_entry *entry;
1446 entry = TAILQ_FIRST(&commits->head);
1447 if (*first_displayed_entry == entry)
1450 entry = *first_displayed_entry;
1451 while (entry && nscrolled < maxscroll) {
1452 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1454 *first_displayed_entry = entry;
1460 static const struct got_error *
1461 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1462 pthread_cond_t *need_commits)
1467 halfdelay(1); /* fast refresh while loading commits */
1469 while (*commits_needed > 0) {
1473 /* Wake the log thread. */
1474 errcode = pthread_cond_signal(need_commits);
1476 return got_error_set_errno(errcode,
1477 "pthread_cond_signal");
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1480 return got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1483 errcode = pthread_mutex_lock(&tog_mutex);
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_lock");
1488 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1490 * Thread is not done yet; lose a key press
1491 * and let the user retry... this way the GUI
1492 * remains interactive while logging deep paths
1493 * with few commits in history.
1502 static const struct got_error *
1503 scroll_down(struct tog_view *view,
1504 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1505 struct commit_queue_entry **last_displayed_entry,
1506 struct commit_queue *commits, int *log_complete, int *commits_needed,
1507 pthread_cond_t *need_commits)
1509 const struct got_error *err = NULL;
1510 struct commit_queue_entry *pentry;
1513 if (*last_displayed_entry == NULL)
1516 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1517 if (pentry == NULL && !*log_complete) {
1519 * Ask the log thread for required amount of commits
1520 * plus some amount of pre-fetching.
1522 (*commits_needed) += maxscroll + 20;
1523 err = trigger_log_thread(0, commits_needed, log_complete,
1530 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1534 *last_displayed_entry = pentry;
1536 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1539 *first_displayed_entry = pentry;
1540 } while (++nscrolled < maxscroll);
1545 static const struct got_error *
1546 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1547 struct got_commit_object *commit, struct got_object_id *commit_id,
1548 struct tog_view *log_view, struct got_reflist_head *refs,
1549 struct got_repository *repo)
1551 const struct got_error *err;
1552 struct got_object_qid *parent_id;
1553 struct tog_view *diff_view;
1555 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1556 if (diff_view == NULL)
1557 return got_error_from_errno("view_open");
1559 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1560 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1561 commit_id, log_view, refs, repo);
1563 *new_view = diff_view;
1567 static const struct got_error *
1568 tree_view_visit_subtree(struct got_tree_object *subtree,
1569 struct tog_tree_view_state *s)
1571 struct tog_parent_tree *parent;
1573 parent = calloc(1, sizeof(*parent));
1575 return got_error_from_errno("calloc");
1577 parent->tree = s->tree;
1578 parent->first_displayed_entry = s->first_displayed_entry;
1579 parent->selected_entry = s->selected_entry;
1580 parent->selected = s->selected;
1581 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1583 s->entries = got_object_tree_get_entries(s->tree);
1585 s->first_displayed_entry = NULL;
1590 static const struct got_error *
1591 browse_commit_tree(struct tog_view **new_view, int begin_x,
1592 struct commit_queue_entry *entry, const char *path,
1593 struct got_reflist_head *refs, struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_tree_object *tree;
1597 struct got_tree_entry *te;
1598 struct tog_tree_view_state *s;
1599 struct tog_view *tree_view;
1600 char *slash, *subpath = NULL;
1603 err = got_object_open_as_tree(&tree, repo,
1604 got_object_commit_get_tree_id(entry->commit));
1608 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1609 if (tree_view == NULL)
1610 return got_error_from_errno("view_open");
1612 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1614 got_object_tree_close(tree);
1617 s = &tree_view->state.tree;
1619 *new_view = tree_view;
1621 /* Walk the path and open corresponding tree objects. */
1626 struct got_object_id *tree_id;
1628 /* Ensure the correct subtree entry is selected. */
1629 slash = strchr(p, '/');
1631 slash = strchr(p, '\0');
1632 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1633 if (strncmp(p, te->name, slash - p) == 0) {
1634 s->selected_entry = te;
1639 if (s->selected_entry == NULL) {
1640 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1643 if (s->tree != s->root)
1644 s->selected++; /* skip '..' */
1646 if (!S_ISDIR(s->selected_entry->mode)) {
1647 /* Jump to this file's entry. */
1648 s->first_displayed_entry = s->selected_entry;
1653 slash = strchr(p, '/');
1655 subpath = strndup(path, slash - path);
1657 subpath = strdup(path);
1658 if (subpath == NULL) {
1659 err = got_error_from_errno("strdup");
1663 err = got_object_id_by_path(&tree_id, repo, entry->id,
1668 err = got_object_open_as_tree(&tree, repo, tree_id);
1673 err = tree_view_visit_subtree(tree, s);
1675 got_object_tree_close(tree);
1690 log_thread(void *arg)
1692 const struct got_error *err = NULL;
1694 struct tog_log_thread_args *a = arg;
1697 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1702 while (!done && !err && !tog_sigpipe_received) {
1703 err = queue_commits(a->graph, a->commits, 1, a->repo,
1704 a->in_repo_path, a->searching, a->search_next_done,
1707 if (err->code != GOT_ERR_ITER_COMPLETED)
1711 } else if (a->commits_needed > 0)
1712 a->commits_needed--;
1714 errcode = pthread_mutex_lock(&tog_mutex);
1716 err = got_error_set_errno(errcode,
1717 "pthread_mutex_lock");
1719 } else if (*a->quit)
1721 else if (*a->first_displayed_entry == NULL) {
1722 *a->first_displayed_entry =
1723 TAILQ_FIRST(&a->commits->head);
1724 *a->selected_entry = *a->first_displayed_entry;
1728 a->commits_needed = 0;
1729 else if (a->commits_needed == 0) {
1730 errcode = pthread_cond_wait(&a->need_commits,
1733 err = got_error_set_errno(errcode,
1734 "pthread_cond_wait");
1737 errcode = pthread_mutex_unlock(&tog_mutex);
1738 if (errcode && err == NULL)
1739 err = got_error_set_errno(errcode,
1740 "pthread_mutex_unlock");
1742 a->log_complete = 1;
1746 static const struct got_error *
1747 stop_log_thread(struct tog_log_view_state *s)
1749 const struct got_error *err = NULL;
1754 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1756 return got_error_set_errno(errcode,
1757 "pthread_cond_signal");
1758 errcode = pthread_mutex_unlock(&tog_mutex);
1760 return got_error_set_errno(errcode,
1761 "pthread_mutex_unlock");
1762 errcode = pthread_join(s->thread, (void **)&err);
1764 return got_error_set_errno(errcode, "pthread_join");
1765 errcode = pthread_mutex_lock(&tog_mutex);
1767 return got_error_set_errno(errcode,
1768 "pthread_mutex_lock");
1772 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1773 if (errcode && err == NULL)
1774 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1776 if (s->thread_args.repo) {
1777 got_repo_close(s->thread_args.repo);
1778 s->thread_args.repo = NULL;
1781 if (s->thread_args.graph) {
1782 got_commit_graph_close(s->thread_args.graph);
1783 s->thread_args.graph = NULL;
1789 static const struct got_error *
1790 close_log_view(struct tog_view *view)
1792 const struct got_error *err = NULL;
1793 struct tog_log_view_state *s = &view->state.log;
1795 err = stop_log_thread(s);
1796 free_commits(&s->commits);
1797 free(s->in_repo_path);
1798 s->in_repo_path = NULL;
1804 static const struct got_error *
1805 search_start_log_view(struct tog_view *view)
1807 struct tog_log_view_state *s = &view->state.log;
1809 s->matched_entry = NULL;
1810 s->search_entry = NULL;
1814 static const struct got_error *
1815 search_next_log_view(struct tog_view *view)
1817 const struct got_error *err = NULL;
1818 struct tog_log_view_state *s = &view->state.log;
1819 struct commit_queue_entry *entry;
1821 if (!view->searching) {
1822 view->search_next_done = 1;
1826 if (s->search_entry) {
1828 errcode = pthread_mutex_unlock(&tog_mutex);
1830 return got_error_set_errno(errcode,
1831 "pthread_mutex_unlock");
1832 ch = wgetch(view->window);
1833 errcode = pthread_mutex_lock(&tog_mutex);
1835 return got_error_set_errno(errcode,
1836 "pthread_mutex_lock");
1837 if (ch == KEY_BACKSPACE) {
1838 view->search_next_done = 1;
1841 if (view->searching == TOG_SEARCH_FORWARD)
1842 entry = TAILQ_NEXT(s->search_entry, entry);
1844 entry = TAILQ_PREV(s->search_entry,
1845 commit_queue_head, entry);
1846 } else if (s->matched_entry) {
1847 if (view->searching == TOG_SEARCH_FORWARD)
1848 entry = TAILQ_NEXT(s->selected_entry, entry);
1850 entry = TAILQ_PREV(s->selected_entry,
1851 commit_queue_head, entry);
1853 if (view->searching == TOG_SEARCH_FORWARD)
1854 entry = TAILQ_FIRST(&s->commits.head);
1856 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1862 if (entry == NULL) {
1863 if (s->thread_args.log_complete ||
1864 view->searching == TOG_SEARCH_BACKWARD) {
1865 view->search_next_done = 1;
1869 * Poke the log thread for more commits and return,
1870 * allowing the main loop to make progress. Search
1871 * will resume at s->search_entry once we come back.
1873 s->thread_args.commits_needed++;
1874 return trigger_log_thread(1,
1875 &s->thread_args.commits_needed,
1876 &s->thread_args.log_complete,
1877 &s->thread_args.need_commits);
1880 err = match_commit(&have_match, entry->id, entry->commit,
1885 view->search_next_done = 1;
1886 s->matched_entry = entry;
1890 s->search_entry = entry;
1891 if (view->searching == TOG_SEARCH_FORWARD)
1892 entry = TAILQ_NEXT(entry, entry);
1894 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1897 if (s->matched_entry) {
1898 int cur = s->selected_entry->idx;
1899 while (cur < s->matched_entry->idx) {
1900 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1905 while (cur > s->matched_entry->idx) {
1906 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1913 s->search_entry = NULL;
1918 static const struct got_error *
1919 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1920 struct got_reflist_head *refs, struct got_repository *repo,
1921 const char *head_ref_name, const char *path, int check_disk)
1923 const struct got_error *err = NULL;
1924 struct tog_log_view_state *s = &view->state.log;
1925 struct got_repository *thread_repo = NULL;
1926 struct got_commit_graph *thread_graph = NULL;
1929 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1933 /* The commit queue only contains commits being displayed. */
1934 TAILQ_INIT(&s->commits.head);
1935 s->commits.ncommits = 0;
1939 s->head_ref_name = head_ref_name;
1940 s->start_id = got_object_id_dup(start_id);
1941 if (s->start_id == NULL) {
1942 err = got_error_from_errno("got_object_id_dup");
1946 view->show = show_log_view;
1947 view->input = input_log_view;
1948 view->close = close_log_view;
1949 view->search_start = search_start_log_view;
1950 view->search_next = search_next_log_view;
1952 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1955 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1960 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1962 err = got_error_set_errno(errcode, "pthread_cond_init");
1966 s->thread_args.commits_needed = view->nlines;
1967 s->thread_args.graph = thread_graph;
1968 s->thread_args.commits = &s->commits;
1969 s->thread_args.in_repo_path = s->in_repo_path;
1970 s->thread_args.start_id = s->start_id;
1971 s->thread_args.repo = thread_repo;
1972 s->thread_args.log_complete = 0;
1973 s->thread_args.quit = &s->quit;
1974 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1975 s->thread_args.selected_entry = &s->selected_entry;
1976 s->thread_args.searching = &view->searching;
1977 s->thread_args.search_next_done = &view->search_next_done;
1978 s->thread_args.regex = &view->regex;
1981 close_log_view(view);
1985 static const struct got_error *
1986 show_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 if (s->thread == NULL) {
1991 int errcode = pthread_create(&s->thread, NULL, log_thread,
1994 return got_error_set_errno(errcode, "pthread_create");
1997 return draw_commits(view, &s->last_displayed_entry,
1998 &s->selected_entry, s->first_displayed_entry,
1999 &s->commits, s->selected, view->nlines, s->refs,
2000 s->in_repo_path, s->thread_args.commits_needed);
2003 static const struct got_error *
2004 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2005 struct tog_view **focus_view, struct tog_view *view, int ch)
2007 const struct got_error *err = NULL;
2008 struct tog_log_view_state *s = &view->state.log;
2009 char *parent_path, *in_repo_path = NULL;
2010 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2012 struct got_object_id *start_id;
2022 if (s->first_displayed_entry == NULL)
2024 if (s->selected > 0)
2027 scroll_up(view, &s->first_displayed_entry, 1,
2032 if (s->first_displayed_entry == NULL)
2034 if (TAILQ_FIRST(&s->commits.head) ==
2035 s->first_displayed_entry) {
2039 scroll_up(view, &s->first_displayed_entry,
2040 view->nlines, &s->commits);
2046 if (s->first_displayed_entry == NULL)
2048 if (s->selected < MIN(view->nlines - 2,
2049 s->commits.ncommits - 1)) {
2053 err = scroll_down(view, &s->first_displayed_entry, 1,
2054 &s->last_displayed_entry, &s->commits,
2055 &s->thread_args.log_complete,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.need_commits);
2061 struct commit_queue_entry *first;
2062 first = s->first_displayed_entry;
2065 err = scroll_down(view, &s->first_displayed_entry,
2066 view->nlines, &s->last_displayed_entry,
2067 &s->commits, &s->thread_args.log_complete,
2068 &s->thread_args.commits_needed,
2069 &s->thread_args.need_commits);
2072 if (first == s->first_displayed_entry &&
2073 s->selected < MIN(view->nlines - 2,
2074 s->commits.ncommits - 1)) {
2075 /* can't scroll further down */
2076 s->selected = MIN(view->nlines - 2,
2077 s->commits.ncommits - 1);
2083 if (s->selected > view->nlines - 2)
2084 s->selected = view->nlines - 2;
2085 if (s->selected > s->commits.ncommits - 1)
2086 s->selected = s->commits.ncommits - 1;
2091 if (s->selected_entry == NULL)
2093 if (view_is_parent_view(view))
2094 begin_x = view_split_begin_x(view->begin_x);
2095 err = open_diff_view_for_commit(&diff_view, begin_x,
2096 s->selected_entry->commit, s->selected_entry->id,
2097 view, s->refs, s->repo);
2100 if (view_is_parent_view(view)) {
2101 err = view_close_child(view);
2104 err = view_set_child(view, diff_view);
2106 view_close(diff_view);
2109 *focus_view = diff_view;
2110 view->child_focussed = 1;
2112 *new_view = diff_view;
2115 if (s->selected_entry == NULL)
2117 if (view_is_parent_view(view))
2118 begin_x = view_split_begin_x(view->begin_x);
2119 err = browse_commit_tree(&tree_view, begin_x,
2120 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2123 if (view_is_parent_view(view)) {
2124 err = view_close_child(view);
2127 err = view_set_child(view, tree_view);
2129 view_close(tree_view);
2132 *focus_view = tree_view;
2133 view->child_focussed = 1;
2135 *new_view = tree_view;
2138 if (strcmp(s->in_repo_path, "/") == 0)
2140 parent_path = dirname(s->in_repo_path);
2141 if (parent_path && strcmp(parent_path, ".") != 0) {
2142 err = stop_log_thread(s);
2145 lv = view_open(view->nlines, view->ncols,
2146 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2148 return got_error_from_errno(
2150 err = open_log_view(lv, s->start_id, s->refs,
2151 s->repo, s->head_ref_name, parent_path, 0);
2154 if (view_is_parent_view(view))
2157 view_set_child(view->parent, lv);
2164 err = stop_log_thread(s);
2167 lv = view_open(view->nlines, view->ncols,
2168 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2170 return got_error_from_errno("view_open");
2171 err = get_head_commit_id(&start_id, s->head_ref_name ?
2172 s->head_ref_name : GOT_REF_HEAD, s->repo);
2177 in_repo_path = strdup(s->in_repo_path);
2178 if (in_repo_path == NULL) {
2181 return got_error_from_errno("strdup");
2183 got_ref_list_free(s->refs);
2184 err = got_ref_list(s->refs, s->repo, NULL,
2185 got_ref_cmp_by_name, NULL);
2191 err = open_log_view(lv, start_id, s->refs, s->repo,
2192 s->head_ref_name, in_repo_path, 0);
2208 static const struct got_error *
2209 apply_unveil(const char *repo_path, const char *worktree_path)
2211 const struct got_error *error;
2214 if (unveil("gmon.out", "rwc") != 0)
2215 return got_error_from_errno2("unveil", "gmon.out");
2217 if (repo_path && unveil(repo_path, "r") != 0)
2218 return got_error_from_errno2("unveil", repo_path);
2220 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2221 return got_error_from_errno2("unveil", worktree_path);
2223 if (unveil("/tmp", "rwc") != 0)
2224 return got_error_from_errno2("unveil", "/tmp");
2226 error = got_privsep_unveil_exec_helpers();
2230 if (unveil(NULL, NULL) != 0)
2231 return got_error_from_errno("unveil");
2241 halfdelay(1); /* Do fast refresh while initial view is loading. */
2244 intrflush(stdscr, FALSE);
2245 keypad(stdscr, TRUE);
2247 signal(SIGWINCH, tog_sigwinch);
2248 signal(SIGPIPE, tog_sigpipe);
2251 static const struct got_error *
2252 cmd_log(int argc, char *argv[])
2254 const struct got_error *error;
2255 struct got_repository *repo = NULL;
2256 struct got_worktree *worktree = NULL;
2257 struct got_reflist_head refs;
2258 struct got_object_id *start_id = NULL;
2259 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2260 char *start_commit = NULL, *head_ref_name = NULL;
2262 struct tog_view *view;
2264 SIMPLEQ_INIT(&refs);
2267 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2272 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2275 start_commit = optarg;
2278 repo_path = realpath(optarg, NULL);
2279 if (repo_path == NULL)
2280 err(1, "-r option");
2291 cwd = getcwd(NULL, 0);
2293 error = got_error_from_errno("getcwd");
2296 error = got_worktree_open(&worktree, cwd);
2297 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2304 error = got_error_from_errno("strdup");
2307 } else if (argc == 1) {
2309 error = got_worktree_resolve_path(&path, worktree,
2314 path = strdup(argv[0]);
2316 error = got_error_from_errno("strdup");
2323 if (repo_path == NULL) {
2326 got_worktree_get_repo_path(worktree));
2328 repo_path = strdup(cwd);
2330 if (repo_path == NULL) {
2331 error = got_error_from_errno("strdup");
2337 error = got_repo_open(&repo, repo_path, NULL);
2341 error = apply_unveil(got_repo_get_path(repo),
2342 worktree ? got_worktree_get_root_path(worktree) : NULL);
2346 if (start_commit == NULL)
2347 error = get_head_commit_id(&start_id, worktree ?
2348 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2351 error = get_head_commit_id(&start_id, start_commit, repo);
2353 if (error->code != GOT_ERR_NOT_REF)
2355 error = got_repo_match_object_id_prefix(&start_id,
2356 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2362 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2366 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2368 error = got_error_from_errno("view_open");
2372 head_ref_name = strdup(
2373 got_worktree_get_head_ref_name(worktree));
2374 if (head_ref_name == NULL) {
2375 error = got_error_from_errno("strdup");
2379 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2384 /* Release work tree lock. */
2385 got_worktree_close(worktree);
2388 error = view_loop(view);
2394 free(head_ref_name);
2396 got_repo_close(repo);
2398 got_worktree_close(worktree);
2399 got_ref_list_free(&refs);
2407 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2413 parse_next_line(FILE *f, size_t *len)
2418 const char delim[3] = { '\0', '\0', '\0'};
2420 line = fparseln(f, &linelen, &lineno, delim, 0);
2426 static const struct got_error *
2427 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2428 int *last_displayed_line, int *eof, int max_lines,
2431 const struct got_error *err;
2432 int nlines = 0, nprinted = 0;
2439 werase(view->window);
2442 err = format_line(&wline, &width, header, view->ncols, 0);
2447 if (view_needs_focus_indication(view))
2448 wstandout(view->window);
2449 waddwstr(view->window, wline);
2450 if (view_needs_focus_indication(view))
2451 wstandend(view->window);
2452 if (width <= view->ncols - 1)
2453 waddch(view->window, '\n');
2461 while (nprinted < max_lines) {
2462 line = parse_next_line(f, &len);
2467 if (++nlines < *first_displayed_line) {
2472 err = format_line(&wline, &width, line, view->ncols, 0);
2477 waddwstr(view->window, wline);
2478 if (width <= view->ncols - 1)
2479 waddch(view->window, '\n');
2480 if (++nprinted == 1)
2481 *first_displayed_line = nlines;
2486 *last_displayed_line = nlines;
2491 while (nprinted < view->nlines) {
2492 waddch(view->window, '\n');
2496 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2501 wstandout(view->window);
2502 waddwstr(view->window, wline);
2503 wstandend(view->window);
2510 get_datestr(time_t *time, char *datebuf)
2512 struct tm mytm, *tm;
2515 tm = gmtime_r(time, &mytm);
2518 s = asctime_r(tm, datebuf);
2521 p = strchr(s, '\n');
2527 static const struct got_error *
2528 write_commit_info(struct got_object_id *commit_id,
2529 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2531 const struct got_error *err = NULL;
2532 char datebuf[26], *datestr;
2533 struct got_commit_object *commit;
2534 char *id_str = NULL, *logmsg = NULL;
2535 time_t committer_time;
2536 const char *author, *committer;
2537 char *refs_str = NULL;
2540 err = build_refs_str(&refs_str, refs, commit_id, repo);
2545 err = got_object_open_as_commit(&commit, repo, commit_id);
2549 err = got_object_id_str(&id_str, commit_id);
2551 err = got_error_from_errno("got_object_id_str");
2555 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2556 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2557 err = got_error_from_errno("fprintf");
2560 if (fprintf(outfile, "from: %s\n",
2561 got_object_commit_get_author(commit)) < 0) {
2562 err = got_error_from_errno("fprintf");
2565 committer_time = got_object_commit_get_committer_time(commit);
2566 datestr = get_datestr(&committer_time, datebuf);
2567 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2568 err = got_error_from_errno("fprintf");
2571 author = got_object_commit_get_author(commit);
2572 committer = got_object_commit_get_committer(commit);
2573 if (strcmp(author, committer) != 0 &&
2574 fprintf(outfile, "via: %s\n", committer) < 0) {
2575 err = got_error_from_errno("fprintf");
2578 err = got_object_commit_get_logmsg(&logmsg, commit);
2581 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2582 err = got_error_from_errno("fprintf");
2589 got_object_commit_close(commit);
2593 static const struct got_error *
2594 create_diff(struct tog_diff_view_state *s)
2596 const struct got_error *err = NULL;
2602 err = got_error_from_errno("got_opentemp");
2605 if (s->f && fclose(s->f) != 0) {
2606 err = got_error_from_errno("fclose");
2612 err = got_object_get_type(&obj_type, s->repo, s->id1);
2614 err = got_object_get_type(&obj_type, s->repo, s->id2);
2619 case GOT_OBJ_TYPE_BLOB:
2620 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2621 s->diff_context, 0, s->repo, f);
2623 case GOT_OBJ_TYPE_TREE:
2624 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2625 s->diff_context, 0, s->repo, f);
2627 case GOT_OBJ_TYPE_COMMIT: {
2628 const struct got_object_id_queue *parent_ids;
2629 struct got_object_qid *pid;
2630 struct got_commit_object *commit2;
2632 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2635 /* Show commit info if we're diffing to a parent/root commit. */
2637 write_commit_info(s->id2, s->refs, s->repo, f);
2639 parent_ids = got_object_commit_get_parent_ids(commit2);
2640 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2641 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2642 write_commit_info(s->id2, s->refs,
2648 got_object_commit_close(commit2);
2650 err = got_diff_objects_as_commits(s->id1, s->id2,
2651 s->diff_context, 0, s->repo, f);
2655 err = got_error(GOT_ERR_OBJ_TYPE);
2659 if (f && fflush(f) != 0 && err == NULL)
2660 err = got_error_from_errno("fflush");
2665 diff_view_indicate_progress(struct tog_view *view)
2667 mvwaddstr(view->window, 0, 0, "diffing...");
2672 static const struct got_error *
2673 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2674 struct got_object_id *id2, struct tog_view *log_view,
2675 struct got_reflist_head *refs, struct got_repository *repo)
2677 const struct got_error *err;
2679 if (id1 != NULL && id2 != NULL) {
2681 err = got_object_get_type(&type1, repo, id1);
2684 err = got_object_get_type(&type2, repo, id2);
2689 return got_error(GOT_ERR_OBJ_TYPE);
2693 view->state.diff.id1 = got_object_id_dup(id1);
2694 if (view->state.diff.id1 == NULL)
2695 return got_error_from_errno("got_object_id_dup");
2697 view->state.diff.id1 = NULL;
2699 view->state.diff.id2 = got_object_id_dup(id2);
2700 if (view->state.diff.id2 == NULL) {
2701 free(view->state.diff.id1);
2702 view->state.diff.id1 = NULL;
2703 return got_error_from_errno("got_object_id_dup");
2705 view->state.diff.f = NULL;
2706 view->state.diff.first_displayed_line = 1;
2707 view->state.diff.last_displayed_line = view->nlines;
2708 view->state.diff.diff_context = 3;
2709 view->state.diff.log_view = log_view;
2710 view->state.diff.repo = repo;
2711 view->state.diff.refs = refs;
2713 if (log_view && view_is_splitscreen(view))
2714 show_log_view(log_view); /* draw vborder */
2715 diff_view_indicate_progress(view);
2717 err = create_diff(&view->state.diff);
2719 free(view->state.diff.id1);
2720 view->state.diff.id1 = NULL;
2721 free(view->state.diff.id2);
2722 view->state.diff.id2 = NULL;
2726 view->show = show_diff_view;
2727 view->input = input_diff_view;
2728 view->close = close_diff_view;
2733 static const struct got_error *
2734 close_diff_view(struct tog_view *view)
2736 const struct got_error *err = NULL;
2738 free(view->state.diff.id1);
2739 view->state.diff.id1 = NULL;
2740 free(view->state.diff.id2);
2741 view->state.diff.id2 = NULL;
2742 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2743 err = got_error_from_errno("fclose");
2747 static const struct got_error *
2748 show_diff_view(struct tog_view *view)
2750 const struct got_error *err;
2751 struct tog_diff_view_state *s = &view->state.diff;
2752 char *id_str1 = NULL, *id_str2, *header;
2755 err = got_object_id_str(&id_str1, s->id1);
2759 err = got_object_id_str(&id_str2, s->id2);
2763 if (asprintf(&header, "diff %s %s",
2764 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2765 err = got_error_from_errno("asprintf");
2773 return draw_file(view, s->f, &s->first_displayed_line,
2774 &s->last_displayed_line, &s->eof, view->nlines,
2778 static const struct got_error *
2779 set_selected_commit(struct tog_diff_view_state *s,
2780 struct commit_queue_entry *entry)
2782 const struct got_error *err;
2783 const struct got_object_id_queue *parent_ids;
2784 struct got_commit_object *selected_commit;
2785 struct got_object_qid *pid;
2788 s->id2 = got_object_id_dup(entry->id);
2790 return got_error_from_errno("got_object_id_dup");
2792 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2795 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2797 pid = SIMPLEQ_FIRST(parent_ids);
2798 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2799 got_object_commit_close(selected_commit);
2803 static const struct got_error *
2804 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2805 struct tog_view **focus_view, struct tog_view *view, int ch)
2807 const struct got_error *err = NULL;
2808 struct tog_diff_view_state *s = &view->state.diff;
2809 struct tog_log_view_state *ls;
2810 struct commit_queue_entry *entry;
2816 if (s->first_displayed_line > 1)
2817 s->first_displayed_line--;
2821 if (s->first_displayed_line == 1)
2824 while (i++ < view->nlines - 1 &&
2825 s->first_displayed_line > 1)
2826 s->first_displayed_line--;
2831 s->first_displayed_line++;
2839 while (!s->eof && i++ < view->nlines - 1) {
2841 line = parse_next_line(s->f, NULL);
2842 s->first_displayed_line++;
2848 if (s->diff_context > 0) {
2850 diff_view_indicate_progress(view);
2851 err = create_diff(s);
2855 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2857 diff_view_indicate_progress(view);
2858 err = create_diff(s);
2863 if (s->log_view == NULL)
2865 ls = &s->log_view->state.log;
2866 entry = TAILQ_PREV(ls->selected_entry,
2867 commit_queue_head, entry);
2871 err = input_log_view(NULL, NULL, NULL, s->log_view,
2876 err = set_selected_commit(s, entry);
2880 s->first_displayed_line = 1;
2881 s->last_displayed_line = view->nlines;
2883 diff_view_indicate_progress(view);
2884 err = create_diff(s);
2888 if (s->log_view == NULL)
2890 ls = &s->log_view->state.log;
2892 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2893 ls->thread_args.commits_needed++;
2895 /* Display "loading..." in log view. */
2896 show_log_view(s->log_view);
2900 err = trigger_log_thread(1 /* load_all */,
2901 &ls->thread_args.commits_needed,
2902 &ls->thread_args.log_complete,
2903 &ls->thread_args.need_commits);
2907 err = input_log_view(NULL, NULL, NULL, s->log_view,
2912 entry = TAILQ_NEXT(ls->selected_entry, entry);
2916 err = set_selected_commit(s, entry);
2920 s->first_displayed_line = 1;
2921 s->last_displayed_line = view->nlines;
2923 diff_view_indicate_progress(view);
2924 err = create_diff(s);
2933 static const struct got_error *
2934 cmd_diff(int argc, char *argv[])
2936 const struct got_error *error = NULL;
2937 struct got_repository *repo = NULL;
2938 struct got_reflist_head refs;
2939 struct got_object_id *id1 = NULL, *id2 = NULL;
2940 char *repo_path = NULL;
2941 char *id_str1 = NULL, *id_str2 = NULL;
2943 struct tog_view *view;
2945 SIMPLEQ_INIT(&refs);
2948 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2953 while ((ch = getopt(argc, argv, "")) != -1) {
2965 usage_diff(); /* TODO show local worktree changes */
2966 } else if (argc == 2) {
2967 repo_path = getcwd(NULL, 0);
2968 if (repo_path == NULL)
2969 return got_error_from_errno("getcwd");
2972 } else if (argc == 3) {
2973 repo_path = realpath(argv[0], NULL);
2974 if (repo_path == NULL)
2975 return got_error_from_errno2("realpath", argv[0]);
2983 error = got_repo_open(&repo, repo_path, NULL);
2987 error = apply_unveil(got_repo_get_path(repo), NULL);
2991 error = got_repo_match_object_id_prefix(&id1, id_str1,
2992 GOT_OBJ_TYPE_ANY, repo);
2996 error = got_repo_match_object_id_prefix(&id2, id_str2,
2997 GOT_OBJ_TYPE_ANY, repo);
3001 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3005 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3007 error = got_error_from_errno("view_open");
3010 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3013 error = view_loop(view);
3017 got_repo_close(repo);
3018 got_ref_list_free(&refs);
3026 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3031 struct tog_blame_line {
3033 struct got_object_id *id;
3036 static const struct got_error *
3037 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3038 const char *path, struct tog_blame_line *lines, int nlines,
3039 int blame_complete, int selected_line, int *first_displayed_line,
3040 int *last_displayed_line, int *eof, int max_lines)
3042 const struct got_error *err;
3043 int lineno = 0, nprinted = 0;
3048 struct tog_blame_line *blame_line;
3049 struct got_object_id *prev_id = NULL;
3052 err = got_object_id_str(&id_str, id);
3057 werase(view->window);
3059 if (asprintf(&line, "commit %s", id_str) == -1) {
3060 err = got_error_from_errno("asprintf");
3065 err = format_line(&wline, &width, line, view->ncols, 0);
3070 if (view_needs_focus_indication(view))
3071 wstandout(view->window);
3072 waddwstr(view->window, wline);
3073 if (view_needs_focus_indication(view))
3074 wstandend(view->window);
3077 if (width < view->ncols - 1)
3078 waddch(view->window, '\n');
3080 if (asprintf(&line, "[%d/%d] %s%s",
3081 *first_displayed_line - 1 + selected_line, nlines,
3082 blame_complete ? "" : "annotating... ", path) == -1) {
3084 return got_error_from_errno("asprintf");
3087 err = format_line(&wline, &width, line, view->ncols, 0);
3092 waddwstr(view->window, wline);
3095 if (width < view->ncols - 1)
3096 waddch(view->window, '\n');
3099 while (nprinted < max_lines - 2) {
3100 line = parse_next_line(f, &len);
3105 if (++lineno < *first_displayed_line) {
3110 if (view->ncols <= 9) {
3112 wline = wcsdup(L"");
3114 err = got_error_from_errno("wcsdup");
3116 err = format_line(&wline, &width, line,
3117 view->ncols - 9, 9);
3125 if (view->focussed && nprinted == selected_line - 1)
3126 wstandout(view->window);
3129 blame_line = &lines[lineno - 1];
3130 if (blame_line->annotated && prev_id &&
3131 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3133 nprinted == selected_line - 1)) {
3134 waddstr(view->window, " ");
3135 } else if (blame_line->annotated) {
3137 err = got_object_id_str(&id_str, blame_line->id);
3143 wprintw(view->window, "%.8s", id_str);
3145 prev_id = blame_line->id;
3147 waddstr(view->window, "........");
3151 waddstr(view->window, "........");
3155 if (view->focussed && nprinted == selected_line - 1)
3156 wstandend(view->window);
3157 waddstr(view->window, " ");
3159 waddwstr(view->window, wline);
3160 if (width <= view->ncols - 1)
3161 waddch(view->window, '\n');
3162 if (++nprinted == 1)
3163 *first_displayed_line = lineno;
3168 *last_displayed_line = lineno;
3175 static const struct got_error *
3176 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3178 const struct got_error *err = NULL;
3179 struct tog_blame_cb_args *a = arg;
3180 struct tog_blame_line *line;
3183 if (nlines != a->nlines ||
3184 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3185 return got_error(GOT_ERR_RANGE);
3187 errcode = pthread_mutex_lock(&tog_mutex);
3189 return got_error_set_errno(errcode, "pthread_mutex_lock");
3191 if (*a->quit) { /* user has quit the blame view */
3192 err = got_error(GOT_ERR_ITER_COMPLETED);
3197 goto done; /* no change in this commit */
3199 line = &a->lines[lineno - 1];
3200 if (line->annotated)
3203 line->id = got_object_id_dup(id);
3204 if (line->id == NULL) {
3205 err = got_error_from_errno("got_object_id_dup");
3208 line->annotated = 1;
3210 errcode = pthread_mutex_unlock(&tog_mutex);
3212 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3217 blame_thread(void *arg)
3219 const struct got_error *err;
3220 struct tog_blame_thread_args *ta = arg;
3221 struct tog_blame_cb_args *a = ta->cb_args;
3224 err = got_blame(ta->path, a->commit_id, ta->repo,
3225 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3226 if (err && err->code == GOT_ERR_CANCELLED)
3229 errcode = pthread_mutex_lock(&tog_mutex);
3231 return (void *)got_error_set_errno(errcode,
3232 "pthread_mutex_lock");
3234 got_repo_close(ta->repo);
3238 errcode = pthread_mutex_unlock(&tog_mutex);
3239 if (errcode && err == NULL)
3240 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3245 static struct got_object_id *
3246 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3247 int first_displayed_line, int selected_line)
3249 struct tog_blame_line *line;
3254 line = &lines[first_displayed_line - 1 + selected_line - 1];
3255 if (!line->annotated)
3261 static const struct got_error *
3262 stop_blame(struct tog_blame *blame)
3264 const struct got_error *err = NULL;
3267 if (blame->thread) {
3269 errcode = pthread_mutex_unlock(&tog_mutex);
3271 return got_error_set_errno(errcode,
3272 "pthread_mutex_unlock");
3273 errcode = pthread_join(blame->thread, (void **)&err);
3275 return got_error_set_errno(errcode, "pthread_join");
3276 errcode = pthread_mutex_lock(&tog_mutex);
3278 return got_error_set_errno(errcode,
3279 "pthread_mutex_lock");
3280 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3282 blame->thread = NULL;
3284 if (blame->thread_args.repo) {
3285 got_repo_close(blame->thread_args.repo);
3286 blame->thread_args.repo = NULL;
3289 if (fclose(blame->f) != 0 && err == NULL)
3290 err = got_error_from_errno("fclose");
3294 for (i = 0; i < blame->nlines; i++)
3295 free(blame->lines[i].id);
3297 blame->lines = NULL;
3299 free(blame->cb_args.commit_id);
3300 blame->cb_args.commit_id = NULL;
3305 static const struct got_error *
3306 cancel_blame_view(void *arg)
3308 const struct got_error *err = NULL;
3312 errcode = pthread_mutex_lock(&tog_mutex);
3314 return got_error_set_errno(errcode,
3315 "pthread_mutex_unlock");
3318 err = got_error(GOT_ERR_CANCELLED);
3320 errcode = pthread_mutex_unlock(&tog_mutex);
3322 return got_error_set_errno(errcode,
3323 "pthread_mutex_lock");
3328 static const struct got_error *
3329 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3330 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3331 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3332 struct got_repository *repo)
3334 const struct got_error *err = NULL;
3335 struct got_blob_object *blob = NULL;
3336 struct got_repository *thread_repo = NULL;
3337 struct got_object_id *obj_id = NULL;
3340 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3344 return got_error(GOT_ERR_NO_OBJ);
3346 err = got_object_get_type(&obj_type, repo, obj_id);
3350 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3351 err = got_error(GOT_ERR_OBJ_TYPE);
3355 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3358 blame->f = got_opentemp();
3359 if (blame->f == NULL) {
3360 err = got_error_from_errno("got_opentemp");
3363 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3364 &blame->line_offsets, blame->f, blob);
3365 if (err || blame->nlines == 0)
3368 /* Don't include \n at EOF in the blame line count. */
3369 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3372 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3373 if (blame->lines == NULL) {
3374 err = got_error_from_errno("calloc");
3378 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3382 blame->cb_args.view = view;
3383 blame->cb_args.lines = blame->lines;
3384 blame->cb_args.nlines = blame->nlines;
3385 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3386 if (blame->cb_args.commit_id == NULL) {
3387 err = got_error_from_errno("got_object_id_dup");
3390 blame->cb_args.quit = done;
3392 blame->thread_args.path = path;
3393 blame->thread_args.repo = thread_repo;
3394 blame->thread_args.cb_args = &blame->cb_args;
3395 blame->thread_args.complete = blame_complete;
3396 blame->thread_args.cancel_cb = cancel_blame_view;
3397 blame->thread_args.cancel_arg = done;
3398 *blame_complete = 0;
3402 got_object_blob_close(blob);
3409 static const struct got_error *
3410 open_blame_view(struct tog_view *view, char *path,
3411 struct got_object_id *commit_id, struct got_reflist_head *refs,
3412 struct got_repository *repo)
3414 const struct got_error *err = NULL;
3415 struct tog_blame_view_state *s = &view->state.blame;
3417 SIMPLEQ_INIT(&s->blamed_commits);
3419 s->path = strdup(path);
3420 if (s->path == NULL)
3421 return got_error_from_errno("strdup");
3423 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3429 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3430 s->first_displayed_line = 1;
3431 s->last_displayed_line = view->nlines;
3432 s->selected_line = 1;
3433 s->blame_complete = 0;
3436 s->commit_id = commit_id;
3437 memset(&s->blame, 0, sizeof(s->blame));
3439 view->show = show_blame_view;
3440 view->input = input_blame_view;
3441 view->close = close_blame_view;
3442 view->search_start = search_start_blame_view;
3443 view->search_next = search_next_blame_view;
3445 return run_blame(&s->blame, view, &s->blame_complete,
3446 &s->first_displayed_line, &s->last_displayed_line,
3447 &s->selected_line, &s->done, &s->eof, s->path,
3448 s->blamed_commit->id, s->repo);
3451 static const struct got_error *
3452 close_blame_view(struct tog_view *view)
3454 const struct got_error *err = NULL;
3455 struct tog_blame_view_state *s = &view->state.blame;
3457 if (s->blame.thread)
3458 err = stop_blame(&s->blame);
3460 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3461 struct got_object_qid *blamed_commit;
3462 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3463 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3464 got_object_qid_free(blamed_commit);
3472 static const struct got_error *
3473 search_start_blame_view(struct tog_view *view)
3475 struct tog_blame_view_state *s = &view->state.blame;
3477 s->matched_line = 0;
3482 match_line(const char *line, regex_t *regex)
3484 regmatch_t regmatch;
3486 return regexec(regex, line, 1, ®match, 0) == 0;
3490 static const struct got_error *
3491 search_next_blame_view(struct tog_view *view)
3493 struct tog_blame_view_state *s = &view->state.blame;
3496 if (!view->searching) {
3497 view->search_next_done = 1;
3501 if (s->matched_line) {
3502 if (view->searching == TOG_SEARCH_FORWARD)
3503 lineno = s->matched_line + 1;
3505 lineno = s->matched_line - 1;
3507 if (view->searching == TOG_SEARCH_FORWARD)
3510 lineno = s->blame.nlines;
3518 if (lineno <= 0 || lineno > s->blame.nlines) {
3519 if (s->matched_line == 0) {
3520 view->search_next_done = 1;
3525 if (view->searching == TOG_SEARCH_FORWARD)
3528 lineno = s->blame.nlines;
3531 offset = s->blame.line_offsets[lineno - 1];
3532 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3534 return got_error_from_errno("fseeko");
3537 line = parse_next_line(s->blame.f, &len);
3538 if (line && match_line(line, &view->regex)) {
3539 view->search_next_done = 1;
3540 s->matched_line = lineno;
3545 if (view->searching == TOG_SEARCH_FORWARD)
3551 if (s->matched_line) {
3552 s->first_displayed_line = s->matched_line;
3553 s->selected_line = 1;
3559 static const struct got_error *
3560 show_blame_view(struct tog_view *view)
3562 const struct got_error *err = NULL;
3563 struct tog_blame_view_state *s = &view->state.blame;
3566 if (s->blame.thread == NULL) {
3567 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3568 &s->blame.thread_args);
3570 return got_error_set_errno(errcode, "pthread_create");
3572 halfdelay(1); /* fast refresh while annotating */
3575 if (s->blame_complete)
3576 halfdelay(10); /* disable fast refresh */
3578 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3579 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3580 s->selected_line, &s->first_displayed_line,
3581 &s->last_displayed_line, &s->eof, view->nlines);
3587 static const struct got_error *
3588 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3589 struct tog_view **focus_view, struct tog_view *view, int ch)
3591 const struct got_error *err = NULL, *thread_err = NULL;
3592 struct tog_view *diff_view;
3593 struct tog_blame_view_state *s = &view->state.blame;
3602 if (s->selected_line > 1)
3604 else if (s->selected_line == 1 &&
3605 s->first_displayed_line > 1)
3606 s->first_displayed_line--;
3609 if (s->first_displayed_line == 1) {
3610 s->selected_line = 1;
3613 if (s->first_displayed_line > view->nlines - 2)
3614 s->first_displayed_line -=
3617 s->first_displayed_line = 1;
3621 if (s->selected_line < view->nlines - 2 &&
3622 s->first_displayed_line +
3623 s->selected_line <= s->blame.nlines)
3625 else if (s->last_displayed_line <
3627 s->first_displayed_line++;
3631 struct got_object_id *id = NULL;
3632 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3633 s->first_displayed_line, s->selected_line);
3637 struct got_commit_object *commit;
3638 struct got_object_qid *pid;
3639 struct got_object_id *blob_id = NULL;
3641 err = got_object_open_as_commit(&commit,
3645 pid = SIMPLEQ_FIRST(
3646 got_object_commit_get_parent_ids(commit));
3648 got_object_commit_close(commit);
3651 /* Check if path history ends here. */
3652 err = got_object_id_by_path(&blob_id, s->repo,
3655 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3657 got_object_commit_close(commit);
3660 err = got_object_get_type(&obj_type, s->repo,
3663 /* Can't blame non-blob type objects. */
3664 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3665 got_object_commit_close(commit);
3668 err = got_object_qid_alloc(&s->blamed_commit,
3670 got_object_commit_close(commit);
3672 if (got_object_id_cmp(id,
3673 s->blamed_commit->id) == 0)
3675 err = got_object_qid_alloc(&s->blamed_commit,
3681 thread_err = stop_blame(&s->blame);
3685 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3686 s->blamed_commit, entry);
3687 err = run_blame(&s->blame, view, &s->blame_complete,
3688 &s->first_displayed_line, &s->last_displayed_line,
3689 &s->selected_line, &s->done, &s->eof,
3690 s->path, s->blamed_commit->id, s->repo);
3696 struct got_object_qid *first;
3697 first = SIMPLEQ_FIRST(&s->blamed_commits);
3698 if (!got_object_id_cmp(first->id, s->commit_id))
3701 thread_err = stop_blame(&s->blame);
3705 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3706 got_object_qid_free(s->blamed_commit);
3708 SIMPLEQ_FIRST(&s->blamed_commits);
3709 err = run_blame(&s->blame, view, &s->blame_complete,
3710 &s->first_displayed_line, &s->last_displayed_line,
3711 &s->selected_line, &s->done, &s->eof, s->path,
3712 s->blamed_commit->id, s->repo);
3719 struct got_object_id *id = NULL;
3720 struct got_object_qid *pid;
3721 struct got_commit_object *commit = NULL;
3722 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3723 s->first_displayed_line, s->selected_line);
3726 err = got_object_open_as_commit(&commit, s->repo, id);
3729 pid = SIMPLEQ_FIRST(
3730 got_object_commit_get_parent_ids(commit));
3731 if (view_is_parent_view(view))
3732 begin_x = view_split_begin_x(view->begin_x);
3733 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3734 if (diff_view == NULL) {
3735 got_object_commit_close(commit);
3736 err = got_error_from_errno("view_open");
3739 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3740 id, NULL, s->refs, s->repo);
3741 got_object_commit_close(commit);
3743 view_close(diff_view);
3746 if (view_is_parent_view(view)) {
3747 err = view_close_child(view);
3750 err = view_set_child(view, diff_view);
3752 view_close(diff_view);
3755 *focus_view = diff_view;
3756 view->child_focussed = 1;
3758 *new_view = diff_view;
3765 if (s->last_displayed_line >= s->blame.nlines &&
3766 s->selected_line >= MIN(s->blame.nlines,
3767 view->nlines - 2)) {
3770 if (s->last_displayed_line >= s->blame.nlines &&
3771 s->selected_line < view->nlines - 2) {
3772 s->selected_line = MIN(s->blame.nlines,
3776 if (s->last_displayed_line + view->nlines - 2
3778 s->first_displayed_line +=
3781 s->first_displayed_line =
3786 if (s->selected_line > view->nlines - 2) {
3787 s->selected_line = MIN(s->blame.nlines,
3794 return thread_err ? thread_err : err;
3797 static const struct got_error *
3798 cmd_blame(int argc, char *argv[])
3800 const struct got_error *error;
3801 struct got_repository *repo = NULL;
3802 struct got_reflist_head refs;
3803 struct got_worktree *worktree = NULL;
3804 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3805 struct got_object_id *commit_id = NULL;
3806 char *commit_id_str = NULL;
3808 struct tog_view *view;
3810 SIMPLEQ_INIT(&refs);
3813 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3818 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3821 commit_id_str = optarg;
3824 repo_path = realpath(optarg, NULL);
3825 if (repo_path == NULL)
3826 err(1, "-r option");
3842 cwd = getcwd(NULL, 0);
3844 error = got_error_from_errno("getcwd");
3847 if (repo_path == NULL) {
3848 error = got_worktree_open(&worktree, cwd);
3849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3855 strdup(got_worktree_get_repo_path(worktree));
3856 if (repo_path == NULL)
3857 error = got_error_from_errno("strdup");
3861 repo_path = strdup(cwd);
3862 if (repo_path == NULL) {
3863 error = got_error_from_errno("strdup");
3871 error = got_repo_open(&repo, repo_path, NULL);
3875 error = apply_unveil(got_repo_get_path(repo), NULL);
3880 const char *prefix = got_worktree_get_path_prefix(worktree);
3881 char *p, *worktree_subdir = cwd +
3882 strlen(got_worktree_get_root_path(worktree));
3883 if (asprintf(&p, "%s%s%s%s%s",
3884 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3885 worktree_subdir, worktree_subdir[0] ? "/" : "",
3887 error = got_error_from_errno("asprintf");
3890 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3893 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3898 if (commit_id_str == NULL) {
3899 struct got_reference *head_ref;
3900 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3903 error = got_ref_resolve(&commit_id, repo, head_ref);
3904 got_ref_close(head_ref);
3906 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3908 if (error->code != GOT_ERR_NOT_REF)
3910 error = got_repo_match_object_id_prefix(&commit_id,
3911 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3917 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3921 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3923 error = got_error_from_errno("view_open");
3926 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3930 /* Release work tree lock. */
3931 got_worktree_close(worktree);
3934 error = view_loop(view);
3940 got_worktree_close(worktree);
3942 got_repo_close(repo);
3943 got_ref_list_free(&refs);
3947 static const struct got_error *
3948 draw_tree_entries(struct tog_view *view,
3949 struct got_tree_entry **first_displayed_entry,
3950 struct got_tree_entry **last_displayed_entry,
3951 struct got_tree_entry **selected_entry, int *ndisplayed,
3952 const char *label, int show_ids, const char *parent_path,
3953 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3955 const struct got_error *err = NULL;
3956 struct got_tree_entry *te;
3962 werase(view->window);
3967 err = format_line(&wline, &width, label, view->ncols, 0);
3970 if (view_needs_focus_indication(view))
3971 wstandout(view->window);
3972 waddwstr(view->window, wline);
3973 if (view_needs_focus_indication(view))
3974 wstandend(view->window);
3977 if (width < view->ncols - 1)
3978 waddch(view->window, '\n');
3981 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3984 waddwstr(view->window, wline);
3987 if (width < view->ncols - 1)
3988 waddch(view->window, '\n');
3991 waddch(view->window, '\n');
3995 te = SIMPLEQ_FIRST(&entries->head);
3996 if (*first_displayed_entry == NULL) {
3997 if (selected == 0) {
3999 wstandout(view->window);
4000 *selected_entry = NULL;
4002 waddstr(view->window, " ..\n"); /* parent directory */
4003 if (selected == 0 && view->focussed)
4004 wstandend(view->window);
4011 while (te != *first_displayed_entry)
4012 te = SIMPLEQ_NEXT(te, entry);
4016 char *line = NULL, *id_str = NULL;
4017 const char *modestr = "";
4020 err = got_object_id_str(&id_str, te->id);
4022 return got_error_from_errno(
4023 "got_object_id_str");
4025 if (got_object_tree_entry_is_submodule(te))
4027 else if (S_ISLNK(te->mode))
4029 else if (S_ISDIR(te->mode))
4031 else if (te->mode & S_IXUSR)
4033 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4034 te->name, modestr) == -1) {
4036 return got_error_from_errno("asprintf");
4039 err = format_line(&wline, &width, line, view->ncols, 0);
4044 if (n == selected) {
4046 wstandout(view->window);
4047 *selected_entry = te;
4049 waddwstr(view->window, wline);
4050 if (width < view->ncols - 1)
4051 waddch(view->window, '\n');
4052 if (n == selected && view->focussed)
4053 wstandend(view->window);
4059 *last_displayed_entry = te;
4062 te = SIMPLEQ_NEXT(te, entry);
4069 tree_scroll_up(struct tog_view *view,
4070 struct got_tree_entry **first_displayed_entry, int maxscroll,
4071 const struct got_tree_entries *entries, int isroot)
4073 struct got_tree_entry *te, *prev;
4076 if (*first_displayed_entry == NULL)
4079 te = SIMPLEQ_FIRST(&entries->head);
4080 if (*first_displayed_entry == te) {
4082 *first_displayed_entry = NULL;
4086 /* XXX this is stupid... switch to TAILQ? */
4087 for (i = 0; i < maxscroll; i++) {
4088 while (te != *first_displayed_entry) {
4090 te = SIMPLEQ_NEXT(te, entry);
4092 *first_displayed_entry = prev;
4093 te = SIMPLEQ_FIRST(&entries->head);
4095 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4096 *first_displayed_entry = NULL;
4100 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4101 struct got_tree_entry *last_displayed_entry,
4102 const struct got_tree_entries *entries)
4104 struct got_tree_entry *next, *last;
4107 if (*first_displayed_entry)
4108 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4110 next = SIMPLEQ_FIRST(&entries->head);
4111 last = last_displayed_entry;
4112 while (next && last && n++ < maxscroll) {
4113 last = SIMPLEQ_NEXT(last, entry);
4115 *first_displayed_entry = next;
4116 next = SIMPLEQ_NEXT(next, entry);
4122 static const struct got_error *
4123 tree_entry_path(char **path, struct tog_parent_trees *parents,
4124 struct got_tree_entry *te)
4126 const struct got_error *err = NULL;
4127 struct tog_parent_tree *pt;
4128 size_t len = 2; /* for leading slash and NUL */
4130 TAILQ_FOREACH(pt, parents, entry)
4131 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4133 len += strlen(te->name);
4135 *path = calloc(1, len);
4137 return got_error_from_errno("calloc");
4140 pt = TAILQ_LAST(parents, tog_parent_trees);
4142 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4143 err = got_error(GOT_ERR_NO_SPACE);
4146 if (strlcat(*path, "/", len) >= len) {
4147 err = got_error(GOT_ERR_NO_SPACE);
4150 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4153 if (strlcat(*path, te->name, len) >= len) {
4154 err = got_error(GOT_ERR_NO_SPACE);
4166 static const struct got_error *
4167 blame_tree_entry(struct tog_view **new_view, int begin_x,
4168 struct got_tree_entry *te, struct tog_parent_trees *parents,
4169 struct got_object_id *commit_id, struct got_reflist_head *refs,
4170 struct got_repository *repo)
4172 const struct got_error *err = NULL;
4174 struct tog_view *blame_view;
4178 err = tree_entry_path(&path, parents, te);
4182 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4183 if (blame_view == NULL) {
4184 err = got_error_from_errno("view_open");
4188 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4190 if (err->code == GOT_ERR_CANCELLED)
4192 view_close(blame_view);
4194 *new_view = blame_view;
4200 static const struct got_error *
4201 log_tree_entry(struct tog_view **new_view, int begin_x,
4202 struct got_tree_entry *te, struct tog_parent_trees *parents,
4203 struct got_object_id *commit_id, struct got_reflist_head *refs,
4204 struct got_repository *repo)
4206 struct tog_view *log_view;
4207 const struct got_error *err = NULL;
4212 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4213 if (log_view == NULL)
4214 return got_error_from_errno("view_open");
4216 err = tree_entry_path(&path, parents, te);
4220 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4222 view_close(log_view);
4224 *new_view = log_view;
4229 static const struct got_error *
4230 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4231 struct got_object_id *commit_id, struct got_reflist_head *refs,
4232 struct got_repository *repo)
4234 const struct got_error *err = NULL;
4235 char *commit_id_str = NULL;
4236 struct tog_tree_view_state *s = &view->state.tree;
4238 TAILQ_INIT(&s->parents);
4240 err = got_object_id_str(&commit_id_str, commit_id);
4244 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4245 err = got_error_from_errno("asprintf");
4249 s->root = s->tree = root;
4250 s->entries = got_object_tree_get_entries(root);
4251 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4252 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4253 s->commit_id = got_object_id_dup(commit_id);
4254 if (s->commit_id == NULL) {
4255 err = got_error_from_errno("got_object_id_dup");
4261 view->show = show_tree_view;
4262 view->input = input_tree_view;
4263 view->close = close_tree_view;
4264 view->search_start = search_start_tree_view;
4265 view->search_next = search_next_tree_view;
4267 free(commit_id_str);
4269 free(s->tree_label);
4270 s->tree_label = NULL;
4275 static const struct got_error *
4276 close_tree_view(struct tog_view *view)
4278 struct tog_tree_view_state *s = &view->state.tree;
4280 free(s->tree_label);
4281 s->tree_label = NULL;
4283 s->commit_id = NULL;
4284 while (!TAILQ_EMPTY(&s->parents)) {
4285 struct tog_parent_tree *parent;
4286 parent = TAILQ_FIRST(&s->parents);
4287 TAILQ_REMOVE(&s->parents, parent, entry);
4291 if (s->tree != s->root)
4292 got_object_tree_close(s->tree);
4293 got_object_tree_close(s->root);
4298 static const struct got_error *
4299 search_start_tree_view(struct tog_view *view)
4301 struct tog_tree_view_state *s = &view->state.tree;
4303 s->matched_entry = NULL;
4308 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4310 regmatch_t regmatch;
4312 return regexec(regex, te->name, 1, ®match, 0) == 0;
4315 static const struct got_error *
4316 search_next_tree_view(struct tog_view *view)
4318 struct tog_tree_view_state *s = &view->state.tree;
4319 struct got_tree_entry *entry = NULL, *te;
4321 if (!view->searching) {
4322 view->search_next_done = 1;
4326 if (s->matched_entry) {
4327 if (view->searching == TOG_SEARCH_FORWARD) {
4328 if (s->selected_entry)
4329 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4331 entry = SIMPLEQ_FIRST(&s->entries->head);
4334 if (s->selected_entry == NULL) {
4335 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4338 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4340 if (SIMPLEQ_NEXT(te, entry) ==
4347 if (view->searching == TOG_SEARCH_FORWARD)
4348 entry = SIMPLEQ_FIRST(&s->entries->head);
4350 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4356 if (entry == NULL) {
4357 if (s->matched_entry == NULL) {
4358 view->search_next_done = 1;
4361 if (view->searching == TOG_SEARCH_FORWARD)
4362 entry = SIMPLEQ_FIRST(&s->entries->head);
4364 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4369 if (match_tree_entry(entry, &view->regex)) {
4370 view->search_next_done = 1;
4371 s->matched_entry = entry;
4375 if (view->searching == TOG_SEARCH_FORWARD)
4376 entry = SIMPLEQ_NEXT(entry, entry);
4378 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4381 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4382 if (SIMPLEQ_NEXT(te, entry) == entry) {
4391 if (s->matched_entry) {
4392 s->first_displayed_entry = s->matched_entry;
4399 static const struct got_error *
4400 show_tree_view(struct tog_view *view)
4402 const struct got_error *err = NULL;
4403 struct tog_tree_view_state *s = &view->state.tree;
4406 err = tree_entry_path(&parent_path, &s->parents, NULL);
4410 err = draw_tree_entries(view, &s->first_displayed_entry,
4411 &s->last_displayed_entry, &s->selected_entry,
4412 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4413 s->entries, s->selected, view->nlines, s->tree == s->root);
4420 static const struct got_error *
4421 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4422 struct tog_view **focus_view, struct tog_view *view, int ch)
4424 const struct got_error *err = NULL;
4425 struct tog_tree_view_state *s = &view->state.tree;
4426 struct tog_view *log_view;
4427 int begin_x = 0, nscrolled;
4431 s->show_ids = !s->show_ids;
4434 if (!s->selected_entry)
4436 if (view_is_parent_view(view))
4437 begin_x = view_split_begin_x(view->begin_x);
4438 err = log_tree_entry(&log_view, begin_x,
4439 s->selected_entry, &s->parents,
4440 s->commit_id, s->refs, s->repo);
4441 if (view_is_parent_view(view)) {
4442 err = view_close_child(view);
4445 err = view_set_child(view, log_view);
4447 view_close(log_view);
4450 *focus_view = log_view;
4451 view->child_focussed = 1;
4453 *new_view = log_view;
4457 if (s->selected > 0) {
4459 if (s->selected == 0)
4462 if (s->selected > 0)
4464 tree_scroll_up(view, &s->first_displayed_entry, 1,
4465 s->entries, s->tree == s->root);
4468 tree_scroll_up(view, &s->first_displayed_entry,
4469 MAX(0, view->nlines - 4 - s->selected), s->entries,
4470 s->tree == s->root);
4472 if (SIMPLEQ_FIRST(&s->entries->head) ==
4473 s->first_displayed_entry && s->tree != s->root)
4474 s->first_displayed_entry = NULL;
4478 if (s->selected < s->ndisplayed - 1) {
4482 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4483 /* can't scroll any further */
4485 tree_scroll_down(&s->first_displayed_entry, 1,
4486 s->last_displayed_entry, s->entries);
4489 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4491 /* can't scroll any further; move cursor down */
4492 if (s->selected < s->ndisplayed - 1)
4493 s->selected = s->ndisplayed - 1;
4496 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4497 view->nlines, s->last_displayed_entry, s->entries);
4498 if (nscrolled < view->nlines) {
4500 struct got_tree_entry *te;
4501 te = s->first_displayed_entry;
4504 te = SIMPLEQ_NEXT(te, entry);
4506 s->selected = ndisplayed - 1;
4512 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4513 struct tog_parent_tree *parent;
4514 /* user selected '..' */
4515 if (s->tree == s->root)
4517 parent = TAILQ_FIRST(&s->parents);
4518 TAILQ_REMOVE(&s->parents, parent,
4520 got_object_tree_close(s->tree);
4521 s->tree = parent->tree;
4523 got_object_tree_get_entries(s->tree);
4524 s->first_displayed_entry =
4525 parent->first_displayed_entry;
4527 parent->selected_entry;
4528 s->selected = parent->selected;
4530 } else if (S_ISDIR(s->selected_entry->mode)) {
4531 struct got_tree_object *subtree;
4532 err = got_object_open_as_tree(&subtree,
4533 s->repo, s->selected_entry->id);
4536 err = tree_view_visit_subtree(subtree, s);
4538 got_object_tree_close(subtree);
4541 } else if (S_ISREG(s->selected_entry->mode)) {
4542 struct tog_view *blame_view;
4543 int begin_x = view_is_parent_view(view) ?
4544 view_split_begin_x(view->begin_x) : 0;
4546 err = blame_tree_entry(&blame_view, begin_x,
4547 s->selected_entry, &s->parents,
4548 s->commit_id, s->refs, s->repo);
4551 if (view_is_parent_view(view)) {
4552 err = view_close_child(view);
4555 err = view_set_child(view, blame_view);
4557 view_close(blame_view);
4560 *focus_view = blame_view;
4561 view->child_focussed = 1;
4563 *new_view = blame_view;
4567 if (s->selected > view->nlines)
4568 s->selected = s->ndisplayed - 1;
4581 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4586 static const struct got_error *
4587 cmd_tree(int argc, char *argv[])
4589 const struct got_error *error;
4590 struct got_repository *repo = NULL;
4591 struct got_reflist_head refs;
4592 char *repo_path = NULL;
4593 struct got_object_id *commit_id = NULL;
4594 char *commit_id_arg = NULL;
4595 struct got_commit_object *commit = NULL;
4596 struct got_tree_object *tree = NULL;
4598 struct tog_view *view;
4600 SIMPLEQ_INIT(&refs);
4603 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4608 while ((ch = getopt(argc, argv, "c:")) != -1) {
4611 commit_id_arg = optarg;
4623 struct got_worktree *worktree;
4624 char *cwd = getcwd(NULL, 0);
4626 return got_error_from_errno("getcwd");
4627 error = got_worktree_open(&worktree, cwd);
4628 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4633 strdup(got_worktree_get_repo_path(worktree));
4634 got_worktree_close(worktree);
4637 if (repo_path == NULL) {
4638 error = got_error_from_errno("strdup");
4641 } else if (argc == 1) {
4642 repo_path = realpath(argv[0], NULL);
4643 if (repo_path == NULL)
4644 return got_error_from_errno2("realpath", argv[0]);
4650 error = got_repo_open(&repo, repo_path, NULL);
4654 error = apply_unveil(got_repo_get_path(repo), NULL);
4658 if (commit_id_arg == NULL)
4659 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4661 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4663 if (error->code != GOT_ERR_NOT_REF)
4665 error = got_repo_match_object_id_prefix(&commit_id,
4666 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4672 error = got_object_open_as_commit(&commit, repo, commit_id);
4676 error = got_object_open_as_tree(&tree, repo,
4677 got_object_commit_get_tree_id(commit));
4681 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4685 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4687 error = got_error_from_errno("view_open");
4690 error = open_tree_view(view, tree, commit_id, &refs, repo);
4693 error = view_loop(view);
4698 got_object_commit_close(commit);
4700 got_object_tree_close(tree);
4702 got_repo_close(repo);
4703 got_ref_list_free(&refs);
4712 fprintf(stderr, "commands:");
4713 for (i = 0; i < nitems(tog_commands); i++) {
4714 struct tog_cmd *cmd = &tog_commands[i];
4715 fprintf(stderr, " %s", cmd->name);
4717 fputc('\n', stderr);
4723 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4731 make_argv(const char *arg0, const char *arg1)
4734 int argc = (arg1 == NULL ? 1 : 2);
4736 argv = calloc(argc, sizeof(char *));
4739 argv[0] = strdup(arg0);
4740 if (argv[0] == NULL)
4743 argv[1] = strdup(arg1);
4744 if (argv[1] == NULL)
4752 main(int argc, char *argv[])
4754 const struct got_error *error = NULL;
4755 struct tog_cmd *cmd = NULL;
4756 int ch, hflag = 0, Vflag = 0;
4757 char **cmd_argv = NULL;
4759 setlocale(LC_CTYPE, "");
4761 while ((ch = getopt(argc, argv, "hV")) != -1) {
4781 got_version_print_str();
4788 /* Build an argument vector which runs a default command. */
4789 cmd = &tog_commands[0];
4790 cmd_argv = make_argv(cmd->name, NULL);
4795 /* Did the user specific a command? */
4796 for (i = 0; i < nitems(tog_commands); i++) {
4797 if (strncmp(tog_commands[i].name, argv[0],
4798 strlen(argv[0])) == 0) {
4799 cmd = &tog_commands[i];
4805 fprintf(stderr, "%s: unknown command '%s'\n",
4806 getprogname(), argv[0]);
4815 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4819 if (error && error->code != GOT_ERR_CANCELLED)
4820 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);