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"
48 #include "got_commit_graph.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
53 #include "got_worktree.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #define CTRL(x) ((x) & 0x1f)
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
75 __dead static void usage(int);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log },
88 { "diff", cmd_diff, usage_diff },
89 { "blame", cmd_blame, usage_blame },
90 { "tree", cmd_tree, usage_tree },
100 #define TOG_EOF_STRING "(END)"
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
111 struct commit_queue_head head;
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
117 int first_displayed_line;
118 int last_displayed_line;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
152 const char *head_ref_name;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
158 struct tog_log_thread_args thread_args;
159 struct commit_queue_entry *matched_entry;
160 struct commit_queue_entry *search_entry;
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
167 struct tog_view *view;
168 struct got_object_id *commit_id;
172 struct tog_blame_thread_args {
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
182 struct tog_blame_line *lines;
186 struct tog_blame_thread_args thread_args;
187 struct tog_blame_cb_args cb_args;
191 struct tog_blame_view_state {
192 int first_displayed_line;
193 int last_displayed_line;
198 struct got_object_id_queue blamed_commits;
199 struct got_object_qid *blamed_commit;
201 struct got_repository *repo;
202 struct got_reflist_head *refs;
203 struct got_object_id *commit_id;
204 struct tog_blame blame;
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 struct got_tree_entry *matched_entry;
235 * We implement two types of views: parent views and child views.
237 * The 'Tab' key switches between a parent view and its child view.
238 * Child views are shown side-by-side to their parent view, provided
239 * there is enough screen estate.
241 * When a new view is opened from within a parent view, this new view
242 * becomes a child view of the parent view, replacing any existing child.
244 * When a new view is opened from within a child view, this new view
245 * becomes a parent view which will obscure the views below until the
246 * user quits the new parent view by typing 'q'.
248 * This list of views contains parent views only.
249 * Child views are only pointed to by their parent view.
251 TAILQ_HEAD(tog_view_list_head, tog_view);
254 TAILQ_ENTRY(tog_view) entry;
257 int nlines, ncols, begin_y, begin_x;
258 int lines, cols; /* copies of LINES and COLS */
260 struct tog_view *parent;
261 struct tog_view *child;
264 /* type-specific state */
265 enum tog_view_type type;
267 struct tog_diff_view_state diff;
268 struct tog_log_view_state log;
269 struct tog_blame_view_state blame;
270 struct tog_tree_view_state tree;
273 const struct got_error *(*show)(struct tog_view *);
274 const struct got_error *(*input)(struct tog_view **,
275 struct tog_view **, struct tog_view**, struct tog_view *, int);
276 const struct got_error *(*close)(struct tog_view *);
278 const struct got_error *(*search_start)(struct tog_view *);
279 const struct got_error *(*search_next)(struct tog_view *);
281 #define TOG_SEARCH_FORWARD 1
282 #define TOG_SEARCH_BACKWARD 2
283 int search_next_done;
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *search_start_blame_view(struct tog_view *);
312 static const struct got_error *search_next_blame_view(struct tog_view *);
314 static const struct got_error *open_tree_view(struct tog_view *,
315 struct got_tree_object *, struct got_object_id *,
316 struct got_reflist_head *, struct got_repository *);
317 static const struct got_error *show_tree_view(struct tog_view *);
318 static const struct got_error *input_tree_view(struct tog_view **,
319 struct tog_view **, struct tog_view **, struct tog_view *, int);
320 static const struct got_error *close_tree_view(struct tog_view *);
321 static const struct got_error *search_start_tree_view(struct tog_view *);
322 static const struct got_error *search_next_tree_view(struct tog_view *);
324 static volatile sig_atomic_t tog_sigwinch_received;
325 static volatile sig_atomic_t tog_sigpipe_received;
328 tog_sigwinch(int signo)
330 tog_sigwinch_received = 1;
334 tog_sigpipe(int signo)
336 tog_sigpipe_received = 1;
339 static const struct got_error *
340 view_close(struct tog_view *view)
342 const struct got_error *err = NULL;
345 view_close(view->child);
349 err = view->close(view);
351 del_panel(view->panel);
353 delwin(view->window);
358 static struct tog_view *
359 view_open(int nlines, int ncols, int begin_y, int begin_x,
360 enum tog_view_type type)
362 struct tog_view *view = calloc(1, sizeof(*view));
370 view->nlines = nlines ? nlines : LINES - begin_y;
371 view->ncols = ncols ? ncols : COLS - begin_x;
372 view->begin_y = begin_y;
373 view->begin_x = begin_x;
374 view->window = newwin(nlines, ncols, begin_y, begin_x);
375 if (view->window == NULL) {
379 view->panel = new_panel(view->window);
380 if (view->panel == NULL ||
381 set_panel_userptr(view->panel, view) != OK) {
386 keypad(view->window, TRUE);
391 view_split_begin_x(int begin_x)
393 if (begin_x > 0 || COLS < 120)
395 return (COLS - MAX(COLS / 2, 80));
398 static const struct got_error *view_resize(struct tog_view *);
400 static const struct got_error *
401 view_splitscreen(struct tog_view *view)
403 const struct got_error *err = NULL;
406 view->begin_x = view_split_begin_x(0);
407 view->nlines = LINES;
408 view->ncols = COLS - view->begin_x;
411 err = view_resize(view);
415 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
416 return got_error_from_errno("mvwin");
421 static const struct got_error *
422 view_fullscreen(struct tog_view *view)
424 const struct got_error *err = NULL;
428 view->nlines = LINES;
432 err = view_resize(view);
436 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
437 return got_error_from_errno("mvwin");
443 view_is_parent_view(struct tog_view *view)
445 return view->parent == NULL;
448 static const struct got_error *
449 view_resize(struct tog_view *view)
453 if (view->lines > LINES)
454 nlines = view->nlines - (view->lines - LINES);
456 nlines = view->nlines + (LINES - view->lines);
458 if (view->cols > COLS)
459 ncols = view->ncols - (view->cols - COLS);
461 ncols = view->ncols + (COLS - view->cols);
463 if (wresize(view->window, nlines, ncols) == ERR)
464 return got_error_from_errno("wresize");
465 if (replace_panel(view->panel, view->window) == ERR)
466 return got_error_from_errno("replace_panel");
467 wclear(view->window);
469 view->nlines = nlines;
475 view->child->begin_x = view_split_begin_x(view->begin_x);
476 if (view->child->begin_x == 0) {
477 view_fullscreen(view->child);
478 if (view->child->focussed)
479 show_panel(view->child->panel);
481 show_panel(view->panel);
483 view_splitscreen(view->child);
484 show_panel(view->child->panel);
491 static const struct got_error *
492 view_close_child(struct tog_view *view)
494 const struct got_error *err = NULL;
496 if (view->child == NULL)
499 err = view_close(view->child);
504 static const struct got_error *
505 view_set_child(struct tog_view *view, struct tog_view *child)
507 const struct got_error *err = NULL;
510 child->parent = view;
515 view_is_splitscreen(struct tog_view *view)
517 return view->begin_x > 0;
526 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
527 cols = 80; /* Default */
533 resize_term(lines, cols);
536 static const struct got_error *
537 view_search_start(struct tog_view *view)
539 const struct got_error *err = NULL;
544 if (view->nlines < 1)
547 if (!view_is_parent_view(view))
548 begin_x = view_split_begin_x(view->begin_x);
549 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
551 wclrtoeol(view->window);
555 ret = wgetnstr(view->window, pattern, sizeof(pattern));
561 if (view->searching) {
562 regfree(&view->regex);
566 if (regcomp(&view->regex, pattern,
567 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
568 err = view->search_start(view);
570 regfree(&view->regex);
573 view->searching = TOG_SEARCH_FORWARD;
574 view->search_next_done = 0;
575 view->search_next(view);
581 static const struct got_error *
582 view_input(struct tog_view **new, struct tog_view **dead,
583 struct tog_view **focus, int *done, struct tog_view *view,
584 struct tog_view_list_head *views)
586 const struct got_error *err = NULL;
594 if (view->searching && !view->search_next_done) {
595 errcode = pthread_mutex_unlock(&tog_mutex);
597 return got_error_set_errno(errcode,
598 "pthread_mutex_unlock");
600 errcode = pthread_mutex_lock(&tog_mutex);
602 return got_error_set_errno(errcode,
603 "pthread_mutex_lock");
604 view->search_next(view);
608 nodelay(stdscr, FALSE);
609 /* Allow threads to make progress while we are waiting for input. */
610 errcode = pthread_mutex_unlock(&tog_mutex);
612 return got_error_set_errno(errcode, "pthread_mutex_unlock");
613 ch = wgetch(view->window);
614 errcode = pthread_mutex_lock(&tog_mutex);
616 return got_error_set_errno(errcode, "pthread_mutex_lock");
617 nodelay(stdscr, TRUE);
619 if (tog_sigwinch_received) {
621 tog_sigwinch_received = 0;
622 TAILQ_FOREACH(v, views, entry) {
623 err = view_resize(v);
626 err = v->input(new, dead, focus, v, KEY_RESIZE);
637 *focus = view->child;
638 view->child_focussed = 1;
639 } else if (view->parent) {
640 *focus = view->parent;
641 view->parent->child_focussed = 0;
645 err = view->input(new, dead, focus, view, ch);
652 if (view_is_parent_view(view)) {
653 if (view->child == NULL)
655 if (view_is_splitscreen(view->child)) {
656 *focus = view->child;
657 view->child_focussed = 1;
658 err = view_fullscreen(view->child);
660 err = view_splitscreen(view->child);
663 err = view->child->input(new, dead, focus,
664 view->child, KEY_RESIZE);
666 if (view_is_splitscreen(view)) {
668 view->parent->child_focussed = 1;
669 err = view_fullscreen(view);
671 err = view_splitscreen(view);
675 err = view->input(new, dead, focus, view,
682 if (view->search_start)
683 view_search_start(view);
685 err = view->input(new, dead, focus, view, ch);
689 if (view->search_next && view->searching) {
690 view->searching = (ch == 'n' ?
691 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
692 view->search_next_done = 0;
693 view->search_next(view);
695 err = view->input(new, dead, focus, view, ch);
698 err = view->input(new, dead, focus, view, ch);
706 view_vborder(struct tog_view *view)
709 struct tog_view *view_above;
712 return view_vborder(view->parent);
714 panel = panel_above(view->panel);
718 view_above = panel_userptr(panel);
719 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
720 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
724 view_needs_focus_indication(struct tog_view *view)
726 if (view_is_parent_view(view)) {
727 if (view->child == NULL || view->child_focussed)
729 if (!view_is_splitscreen(view->child))
731 } else if (!view_is_splitscreen(view))
734 return view->focussed;
737 static const struct got_error *
738 view_loop(struct tog_view *view)
740 const struct got_error *err = NULL;
741 struct tog_view_list_head views;
742 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
743 int fast_refresh = 10;
744 int done = 0, errcode;
746 errcode = pthread_mutex_lock(&tog_mutex);
748 return got_error_set_errno(errcode, "pthread_mutex_lock");
751 TAILQ_INSERT_HEAD(&views, view, entry);
755 err = view->show(view);
760 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
761 /* Refresh fast during initialization, then become slower. */
762 if (fast_refresh && fast_refresh-- == 0)
763 halfdelay(10); /* switch to once per second */
765 err = view_input(&new_view, &dead_view, &focus_view, &done,
770 struct tog_view *prev = NULL;
772 if (view_is_parent_view(dead_view))
773 prev = TAILQ_PREV(dead_view,
774 tog_view_list_head, entry);
775 else if (view->parent != dead_view)
778 if (dead_view->parent)
779 dead_view->parent->child = NULL;
781 TAILQ_REMOVE(&views, dead_view, entry);
783 err = view_close(dead_view);
784 if (err || (dead_view == main_view && new_view == NULL))
787 if (view == dead_view) {
792 else if (!TAILQ_EMPTY(&views))
793 view = TAILQ_LAST(&views,
798 if (view->child && view->child_focussed)
799 focus_view = view->child;
806 struct tog_view *v, *t;
807 /* Only allow one parent view per type. */
808 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
809 if (v->type != new_view->type)
811 TAILQ_REMOVE(&views, v, entry);
817 TAILQ_INSERT_TAIL(&views, new_view, entry);
819 if (focus_view == NULL)
820 focus_view = new_view;
823 show_panel(focus_view->panel);
826 focus_view->focussed = 1;
829 show_panel(new_view->panel);
830 if (view->child && view_is_splitscreen(view->child))
831 show_panel(view->child->panel);
834 if (focus_view == NULL) {
836 show_panel(view->panel);
837 if (view->child && view_is_splitscreen(view->child))
838 show_panel(view->child->panel);
842 err = view->parent->show(view->parent);
846 err = view->show(view);
850 err = view->child->show(view->child);
859 while (!TAILQ_EMPTY(&views)) {
860 view = TAILQ_FIRST(&views);
861 TAILQ_REMOVE(&views, view, entry);
865 errcode = pthread_mutex_unlock(&tog_mutex);
866 if (errcode && err == NULL)
867 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
877 "usage: %s log [-c commit] [-r repository-path] [path]\n",
882 /* Create newly allocated wide-character string equivalent to a byte string. */
883 static const struct got_error *
884 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
887 const struct got_error *err = NULL;
890 *wlen = mbstowcs(NULL, s, 0);
891 if (*wlen == (size_t)-1) {
894 return got_error_from_errno("mbstowcs");
896 /* byte string invalid in current encoding; try to "fix" it */
897 err = got_mbsavis(&vis, &vislen, s);
900 *wlen = mbstowcs(NULL, vis, 0);
901 if (*wlen == (size_t)-1) {
902 err = got_error_from_errno("mbstowcs"); /* give up */
907 *ws = calloc(*wlen + 1, sizeof(*ws));
909 err = got_error_from_errno("calloc");
913 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
914 err = got_error_from_errno("mbstowcs");
925 /* Format a line for display, ensuring that it won't overflow a width limit. */
926 static const struct got_error *
927 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
929 const struct got_error *err = NULL;
931 wchar_t *wline = NULL;
938 err = mbs2ws(&wline, &wlen, line);
943 while (i < wlen && cols < wlimit) {
944 int width = wcwidth(wline[i]);
951 if (cols + width <= wlimit)
956 if (wline[i] == L'\t')
957 cols += TABSIZE - ((cols + 1) % TABSIZE);
961 err = got_error_from_errno("wcwidth");
976 static const struct got_error*
977 build_refs_str(char **refs_str, struct got_reflist_head *refs,
978 struct got_object_id *id, struct got_repository *repo)
980 static const struct got_error *err = NULL;
981 struct got_reflist_entry *re;
987 SIMPLEQ_FOREACH(re, refs, entry) {
988 struct got_tag_object *tag = NULL;
991 name = got_ref_get_name(re->ref);
992 if (strcmp(name, GOT_REF_HEAD) == 0)
994 if (strncmp(name, "refs/", 5) == 0)
996 if (strncmp(name, "got/", 4) == 0)
998 if (strncmp(name, "heads/", 6) == 0)
1000 if (strncmp(name, "remotes/", 8) == 0)
1002 if (strncmp(name, "tags/", 5) == 0) {
1003 err = got_object_open_as_tag(&tag, repo, re->id);
1005 if (err->code != GOT_ERR_OBJ_TYPE)
1007 /* Ref points at something other than a tag. */
1012 cmp = got_object_id_cmp(tag ?
1013 got_object_tag_get_object_id(tag) : re->id, id);
1015 got_object_tag_close(tag);
1019 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1020 s ? ", " : "", name) == -1) {
1021 err = got_error_from_errno("asprintf");
1032 static const struct got_error *
1033 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1035 char *smallerthan, *at;
1037 smallerthan = strchr(author, '<');
1038 if (smallerthan && smallerthan[1] != '\0')
1039 author = smallerthan + 1;
1040 at = strchr(author, '@');
1043 return format_line(wauthor, author_width, author, limit);
1046 static const struct got_error *
1047 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1048 struct got_object_id *id, struct got_reflist_head *refs,
1049 int author_display_cols)
1051 const struct got_error *err = NULL;
1052 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1053 char *logmsg0 = NULL, *logmsg = NULL;
1054 char *author = NULL;
1055 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1056 int author_width, logmsg_width;
1057 char *newline, *line = NULL;
1059 static const size_t date_display_cols = 9;
1060 const int avail = view->ncols;
1062 time_t committer_time;
1064 committer_time = got_object_commit_get_committer_time(commit);
1065 if (localtime_r(&committer_time, &tm) == NULL)
1066 return got_error_from_errno("localtime_r");
1067 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1069 return got_error(GOT_ERR_NO_SPACE);
1071 if (avail < date_display_cols)
1072 limit = MIN(sizeof(datebuf) - 1, avail);
1074 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1075 waddnstr(view->window, datebuf, limit);
1080 author = strdup(got_object_commit_get_author(commit));
1081 if (author == NULL) {
1082 err = got_error_from_errno("strdup");
1085 err = format_author(&wauthor, &author_width, author, avail - col);
1088 waddwstr(view->window, wauthor);
1089 col += author_width;
1090 while (col <= avail && author_width < author_display_cols + 2) {
1091 waddch(view->window, ' ');
1098 err = got_object_commit_get_logmsg(&logmsg0, commit);
1102 while (*logmsg == '\n')
1104 newline = strchr(logmsg, '\n');
1107 limit = avail - col;
1108 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1111 waddwstr(view->window, wlogmsg);
1112 col += logmsg_width;
1113 while (col <= avail) {
1114 waddch(view->window, ' ');
1126 static struct commit_queue_entry *
1127 alloc_commit_queue_entry(struct got_commit_object *commit,
1128 struct got_object_id *id)
1130 struct commit_queue_entry *entry;
1132 entry = calloc(1, sizeof(*entry));
1137 entry->commit = commit;
1142 pop_commit(struct commit_queue *commits)
1144 struct commit_queue_entry *entry;
1146 entry = TAILQ_FIRST(&commits->head);
1147 TAILQ_REMOVE(&commits->head, entry, entry);
1148 got_object_commit_close(entry->commit);
1149 commits->ncommits--;
1150 /* Don't free entry->id! It is owned by the commit graph. */
1155 free_commits(struct commit_queue *commits)
1157 while (!TAILQ_EMPTY(&commits->head))
1158 pop_commit(commits);
1161 static const struct got_error *
1162 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1163 int minqueue, struct got_repository *repo, const char *path)
1165 const struct got_error *err = NULL;
1169 * We keep all commits open throughout the lifetime of the log
1170 * view in order to avoid having to re-fetch commits from disk
1171 * while updating the display.
1173 while (nqueued < minqueue) {
1174 struct got_object_id *id;
1175 struct got_commit_object *commit;
1176 struct commit_queue_entry *entry;
1179 err = got_commit_graph_iter_next(&id, graph);
1181 if (err->code != GOT_ERR_ITER_NEED_MORE)
1183 err = got_commit_graph_fetch_commits(graph,
1193 err = got_object_open_as_commit(&commit, repo, id);
1196 entry = alloc_commit_queue_entry(commit, id);
1197 if (entry == NULL) {
1198 err = got_error_from_errno("alloc_commit_queue_entry");
1202 errcode = pthread_mutex_lock(&tog_mutex);
1204 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1208 entry->idx = commits->ncommits;
1209 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1211 commits->ncommits++;
1213 errcode = pthread_mutex_unlock(&tog_mutex);
1214 if (errcode && err == NULL)
1215 err = got_error_set_errno(errcode,
1216 "pthread_mutex_unlock");
1222 static const struct got_error *
1223 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1224 struct got_repository *repo)
1226 const struct got_error *err = NULL;
1227 struct got_reference *head_ref;
1231 err = got_ref_open(&head_ref, repo, branch_name, 0);
1235 err = got_ref_resolve(head_id, repo, head_ref);
1236 got_ref_close(head_ref);
1245 static const struct got_error *
1246 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1247 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1248 struct commit_queue *commits, int selected_idx, int limit,
1249 struct got_reflist_head *refs, const char *path, int commits_needed)
1251 const struct got_error *err = NULL;
1252 struct tog_log_view_state *s = &view->state.log;
1253 struct commit_queue_entry *entry;
1255 int ncommits, author_cols = 10;
1256 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1257 char *refs_str = NULL;
1263 if (ncommits == selected_idx) {
1267 entry = TAILQ_NEXT(entry, entry);
1271 if (*selected && !(view->searching && view->search_next_done == 0)) {
1272 err = got_object_id_str(&id_str, (*selected)->id);
1276 err = build_refs_str(&refs_str, refs, (*selected)->id,
1283 if (commits_needed == 0)
1284 halfdelay(10); /* disable fast refresh */
1286 if (asprintf(&ncommits_str, " [%d/%d] %s",
1287 entry ? entry->idx + 1 : 0, commits->ncommits,
1288 commits_needed > 0 ?
1289 (view->searching && view->search_next_done == 0
1290 ? "searching..." : "loading... ") :
1291 (refs_str ? refs_str : "")) == -1) {
1292 err = got_error_from_errno("asprintf");
1296 if (path && strcmp(path, "/") != 0) {
1297 if (asprintf(&header, "commit %s %s%s",
1298 id_str ? id_str : "........................................",
1299 path, ncommits_str) == -1) {
1300 err = got_error_from_errno("asprintf");
1304 } else if (asprintf(&header, "commit %s%s",
1305 id_str ? id_str : "........................................",
1306 ncommits_str) == -1) {
1307 err = got_error_from_errno("asprintf");
1311 err = format_line(&wline, &width, header, view->ncols);
1315 werase(view->window);
1317 if (view_needs_focus_indication(view))
1318 wstandout(view->window);
1319 waddwstr(view->window, wline);
1320 while (width < view->ncols) {
1321 waddch(view->window, ' ');
1324 if (view_needs_focus_indication(view))
1325 wstandend(view->window);
1330 /* Grow author column size if necessary. */
1337 if (ncommits >= limit - 1)
1339 author = strdup(got_object_commit_get_author(entry->commit));
1340 if (author == NULL) {
1341 err = got_error_from_errno("strdup");
1344 err = format_author(&wauthor, &width, author, COLS);
1345 if (author_cols < width)
1346 author_cols = width;
1349 entry = TAILQ_NEXT(entry, entry);
1356 if (ncommits >= limit - 1)
1358 if (ncommits == selected_idx)
1359 wstandout(view->window);
1360 err = draw_commit(view, entry->commit, entry->id, refs,
1362 if (ncommits == selected_idx)
1363 wstandend(view->window);
1368 entry = TAILQ_NEXT(entry, entry);
1381 scroll_up(struct tog_view *view,
1382 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1383 struct commit_queue *commits)
1385 struct commit_queue_entry *entry;
1388 entry = TAILQ_FIRST(&commits->head);
1389 if (*first_displayed_entry == entry)
1392 entry = *first_displayed_entry;
1393 while (entry && nscrolled < maxscroll) {
1394 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1396 *first_displayed_entry = entry;
1402 static const struct got_error *
1403 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1404 pthread_cond_t *need_commits)
1409 halfdelay(1); /* fast refresh while loading commits */
1411 while (*commits_needed > 0) {
1415 /* Wake the log thread. */
1416 errcode = pthread_cond_signal(need_commits);
1418 return got_error_set_errno(errcode,
1419 "pthread_cond_signal");
1420 errcode = pthread_mutex_unlock(&tog_mutex);
1422 return got_error_set_errno(errcode,
1423 "pthread_mutex_unlock");
1425 errcode = pthread_mutex_lock(&tog_mutex);
1427 return got_error_set_errno(errcode,
1428 "pthread_mutex_lock");
1430 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1432 * Thread is not done yet; lose a key press
1433 * and let the user retry... this way the GUI
1434 * remains interactive while logging deep paths
1435 * with few commits in history.
1444 static const struct got_error *
1445 scroll_down(struct tog_view *view,
1446 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1447 struct commit_queue_entry **last_displayed_entry,
1448 struct commit_queue *commits, int *log_complete, int *commits_needed,
1449 pthread_cond_t *need_commits)
1451 const struct got_error *err = NULL;
1452 struct commit_queue_entry *pentry;
1455 if (*last_displayed_entry == NULL)
1458 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1459 if (pentry == NULL && !*log_complete) {
1461 * Ask the log thread for required amount of commits
1462 * plus some amount of pre-fetching.
1464 (*commits_needed) += maxscroll + 20;
1465 err = trigger_log_thread(0, commits_needed, log_complete,
1472 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1476 *last_displayed_entry = pentry;
1478 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1481 *first_displayed_entry = pentry;
1482 } while (++nscrolled < maxscroll);
1487 static const struct got_error *
1488 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1489 struct got_commit_object *commit, struct got_object_id *commit_id,
1490 struct tog_view *log_view, struct got_reflist_head *refs,
1491 struct got_repository *repo)
1493 const struct got_error *err;
1494 struct got_object_qid *parent_id;
1495 struct tog_view *diff_view;
1497 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1498 if (diff_view == NULL)
1499 return got_error_from_errno("view_open");
1501 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1502 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1503 commit_id, log_view, refs, repo);
1505 *new_view = diff_view;
1509 static const struct got_error *
1510 tree_view_visit_subtree(struct got_tree_object *subtree,
1511 struct tog_tree_view_state *s)
1513 struct tog_parent_tree *parent;
1515 parent = calloc(1, sizeof(*parent));
1517 return got_error_from_errno("calloc");
1519 parent->tree = s->tree;
1520 parent->first_displayed_entry = s->first_displayed_entry;
1521 parent->selected_entry = s->selected_entry;
1522 parent->selected = s->selected;
1523 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1525 s->entries = got_object_tree_get_entries(s->tree);
1527 s->first_displayed_entry = NULL;
1532 static const struct got_error *
1533 browse_commit_tree(struct tog_view **new_view, int begin_x,
1534 struct commit_queue_entry *entry, const char *path,
1535 struct got_reflist_head *refs, struct got_repository *repo)
1537 const struct got_error *err = NULL;
1538 struct got_tree_object *tree;
1539 struct got_tree_entry *te;
1540 struct tog_tree_view_state *s;
1541 struct tog_view *tree_view;
1542 char *slash, *subpath = NULL;
1545 err = got_object_open_as_tree(&tree, repo,
1546 got_object_commit_get_tree_id(entry->commit));
1550 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1551 if (tree_view == NULL)
1552 return got_error_from_errno("view_open");
1554 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1556 got_object_tree_close(tree);
1559 s = &tree_view->state.tree;
1561 *new_view = tree_view;
1563 /* Walk the path and open corresponding tree objects. */
1568 struct got_object_id *tree_id;
1570 /* Ensure the correct subtree entry is selected. */
1571 slash = strchr(p, '/');
1573 slash = strchr(p, '\0');
1574 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1575 if (strncmp(p, te->name, slash - p) == 0) {
1576 s->selected_entry = te;
1581 if (s->selected_entry == NULL) {
1582 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1585 if (s->tree != s->root)
1586 s->selected++; /* skip '..' */
1588 if (!S_ISDIR(s->selected_entry->mode)) {
1589 /* Jump to this file's entry. */
1590 s->first_displayed_entry = s->selected_entry;
1595 slash = strchr(p, '/');
1597 subpath = strndup(path, slash - path);
1599 subpath = strdup(path);
1600 if (subpath == NULL) {
1601 err = got_error_from_errno("strdup");
1605 err = got_object_id_by_path(&tree_id, repo, entry->id,
1610 err = got_object_open_as_tree(&tree, repo, tree_id);
1615 err = tree_view_visit_subtree(tree, s);
1617 got_object_tree_close(tree);
1632 log_thread(void *arg)
1634 const struct got_error *err = NULL;
1636 struct tog_log_thread_args *a = arg;
1639 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1643 while (!done && !err && !tog_sigpipe_received) {
1644 err = queue_commits(a->graph, a->commits, 1, a->repo,
1647 if (err->code != GOT_ERR_ITER_COMPLETED)
1651 } else if (a->commits_needed > 0)
1652 a->commits_needed--;
1654 errcode = pthread_mutex_lock(&tog_mutex);
1656 err = got_error_set_errno(errcode,
1657 "pthread_mutex_lock");
1659 } else if (*a->quit)
1661 else if (*a->first_displayed_entry == NULL) {
1662 *a->first_displayed_entry =
1663 TAILQ_FIRST(&a->commits->head);
1664 *a->selected_entry = *a->first_displayed_entry;
1668 a->commits_needed = 0;
1669 else if (a->commits_needed == 0) {
1670 errcode = pthread_cond_wait(&a->need_commits,
1673 err = got_error_set_errno(errcode,
1674 "pthread_cond_wait");
1677 errcode = pthread_mutex_unlock(&tog_mutex);
1678 if (errcode && err == NULL)
1679 err = got_error_set_errno(errcode,
1680 "pthread_mutex_unlock");
1682 a->log_complete = 1;
1686 static const struct got_error *
1687 stop_log_thread(struct tog_log_view_state *s)
1689 const struct got_error *err = NULL;
1694 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1696 return got_error_set_errno(errcode,
1697 "pthread_cond_signal");
1698 errcode = pthread_mutex_unlock(&tog_mutex);
1700 return got_error_set_errno(errcode,
1701 "pthread_mutex_unlock");
1702 errcode = pthread_join(s->thread, (void **)&err);
1704 return got_error_set_errno(errcode, "pthread_join");
1705 errcode = pthread_mutex_lock(&tog_mutex);
1707 return got_error_set_errno(errcode,
1708 "pthread_mutex_lock");
1712 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1713 if (errcode && err == NULL)
1714 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1716 if (s->thread_args.repo) {
1717 got_repo_close(s->thread_args.repo);
1718 s->thread_args.repo = NULL;
1721 if (s->thread_args.graph) {
1722 got_commit_graph_close(s->thread_args.graph);
1723 s->thread_args.graph = NULL;
1729 static const struct got_error *
1730 close_log_view(struct tog_view *view)
1732 const struct got_error *err = NULL;
1733 struct tog_log_view_state *s = &view->state.log;
1735 err = stop_log_thread(s);
1736 free_commits(&s->commits);
1737 free(s->in_repo_path);
1738 s->in_repo_path = NULL;
1744 static const struct got_error *
1745 search_start_log_view(struct tog_view *view)
1747 struct tog_log_view_state *s = &view->state.log;
1749 s->matched_entry = NULL;
1750 s->search_entry = NULL;
1755 match_commit(struct got_commit_object *commit, const char *id_str,
1756 const char *logmsg, regex_t *regex)
1758 regmatch_t regmatch;
1760 if (regexec(regex, got_object_commit_get_author(commit), 1,
1761 ®match, 0) == 0 ||
1762 regexec(regex, got_object_commit_get_committer(commit), 1,
1763 ®match, 0) == 0 ||
1764 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1765 regexec(regex, logmsg, 1, ®match, 0) == 0)
1771 static const struct got_error *
1772 search_next_log_view(struct tog_view *view)
1774 const struct got_error *err = NULL;
1775 struct tog_log_view_state *s = &view->state.log;
1776 struct commit_queue_entry *entry;
1778 if (!view->searching) {
1779 view->search_next_done = 1;
1783 if (s->search_entry) {
1784 if (wgetch(view->window) == KEY_BACKSPACE) {
1785 view->search_next_done = 1;
1788 if (view->searching == TOG_SEARCH_FORWARD)
1789 entry = TAILQ_NEXT(s->search_entry, entry);
1791 entry = TAILQ_PREV(s->search_entry,
1792 commit_queue_head, entry);
1793 } else if (s->matched_entry) {
1794 if (view->searching == TOG_SEARCH_FORWARD)
1795 entry = TAILQ_NEXT(s->selected_entry, entry);
1797 entry = TAILQ_PREV(s->selected_entry,
1798 commit_queue_head, entry);
1800 if (view->searching == TOG_SEARCH_FORWARD)
1801 entry = TAILQ_FIRST(&s->commits.head);
1803 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1807 char *id_str, *logmsg;
1808 if (entry == NULL) {
1809 if (s->thread_args.log_complete ||
1810 view->searching == TOG_SEARCH_BACKWARD) {
1811 view->search_next_done = 1;
1815 * Poke the log thread for more commits and return,
1816 * allowing the main loop to make progress. Search
1817 * will resume at s->search_entry once we come back.
1819 s->thread_args.commits_needed++;
1820 return trigger_log_thread(1,
1821 &s->thread_args.commits_needed,
1822 &s->thread_args.log_complete,
1823 &s->thread_args.need_commits);
1826 err = got_object_id_str(&id_str, entry->id);
1830 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1833 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1835 view->search_next_done = 1;
1836 s->matched_entry = entry;
1842 s->search_entry = entry;
1843 if (view->searching == TOG_SEARCH_FORWARD)
1844 entry = TAILQ_NEXT(entry, entry);
1846 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1849 if (s->matched_entry) {
1850 int cur = s->selected_entry->idx;
1851 while (cur < s->matched_entry->idx) {
1852 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1857 while (cur > s->matched_entry->idx) {
1858 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1865 s->search_entry = NULL;
1870 static const struct got_error *
1871 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1872 struct got_reflist_head *refs, struct got_repository *repo,
1873 const char *head_ref_name, const char *path, int check_disk)
1875 const struct got_error *err = NULL;
1876 struct tog_log_view_state *s = &view->state.log;
1877 struct got_repository *thread_repo = NULL;
1878 struct got_commit_graph *thread_graph = NULL;
1881 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1885 /* The commit queue only contains commits being displayed. */
1886 TAILQ_INIT(&s->commits.head);
1887 s->commits.ncommits = 0;
1891 s->head_ref_name = head_ref_name;
1892 s->start_id = got_object_id_dup(start_id);
1893 if (s->start_id == NULL) {
1894 err = got_error_from_errno("got_object_id_dup");
1898 view->show = show_log_view;
1899 view->input = input_log_view;
1900 view->close = close_log_view;
1901 view->search_start = search_start_log_view;
1902 view->search_next = search_next_log_view;
1904 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1907 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1912 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1914 err = got_error_set_errno(errcode, "pthread_cond_init");
1918 s->thread_args.commits_needed = view->nlines;
1919 s->thread_args.graph = thread_graph;
1920 s->thread_args.commits = &s->commits;
1921 s->thread_args.in_repo_path = s->in_repo_path;
1922 s->thread_args.start_id = s->start_id;
1923 s->thread_args.repo = thread_repo;
1924 s->thread_args.log_complete = 0;
1925 s->thread_args.quit = &s->quit;
1926 s->thread_args.view = view;
1927 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1928 s->thread_args.selected_entry = &s->selected_entry;
1931 close_log_view(view);
1935 static const struct got_error *
1936 show_log_view(struct tog_view *view)
1938 struct tog_log_view_state *s = &view->state.log;
1940 if (s->thread == NULL) {
1941 int errcode = pthread_create(&s->thread, NULL, log_thread,
1944 return got_error_set_errno(errcode, "pthread_create");
1947 return draw_commits(view, &s->last_displayed_entry,
1948 &s->selected_entry, s->first_displayed_entry,
1949 &s->commits, s->selected, view->nlines, s->refs,
1950 s->in_repo_path, s->thread_args.commits_needed);
1953 static const struct got_error *
1954 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1955 struct tog_view **focus_view, struct tog_view *view, int ch)
1957 const struct got_error *err = NULL;
1958 struct tog_log_view_state *s = &view->state.log;
1959 char *parent_path, *in_repo_path = NULL;
1960 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1962 struct got_object_id *start_id;
1972 if (s->first_displayed_entry == NULL)
1974 if (s->selected > 0)
1977 scroll_up(view, &s->first_displayed_entry, 1,
1982 if (s->first_displayed_entry == NULL)
1984 if (TAILQ_FIRST(&s->commits.head) ==
1985 s->first_displayed_entry) {
1989 scroll_up(view, &s->first_displayed_entry,
1990 view->nlines, &s->commits);
1996 if (s->first_displayed_entry == NULL)
1998 if (s->selected < MIN(view->nlines - 2,
1999 s->commits.ncommits - 1)) {
2003 err = scroll_down(view, &s->first_displayed_entry, 1,
2004 &s->last_displayed_entry, &s->commits,
2005 &s->thread_args.log_complete,
2006 &s->thread_args.commits_needed,
2007 &s->thread_args.need_commits);
2011 struct commit_queue_entry *first;
2012 first = s->first_displayed_entry;
2015 err = scroll_down(view, &s->first_displayed_entry,
2016 view->nlines, &s->last_displayed_entry,
2017 &s->commits, &s->thread_args.log_complete,
2018 &s->thread_args.commits_needed,
2019 &s->thread_args.need_commits);
2020 if (first == s->first_displayed_entry &&
2021 s->selected < MIN(view->nlines - 2,
2022 s->commits.ncommits - 1)) {
2023 /* can't scroll further down */
2024 s->selected = MIN(view->nlines - 2,
2025 s->commits.ncommits - 1);
2031 if (s->selected > view->nlines - 2)
2032 s->selected = view->nlines - 2;
2033 if (s->selected > s->commits.ncommits - 1)
2034 s->selected = s->commits.ncommits - 1;
2039 if (s->selected_entry == NULL)
2041 if (view_is_parent_view(view))
2042 begin_x = view_split_begin_x(view->begin_x);
2043 err = open_diff_view_for_commit(&diff_view, begin_x,
2044 s->selected_entry->commit, s->selected_entry->id,
2045 view, s->refs, s->repo);
2048 if (view_is_parent_view(view)) {
2049 err = view_close_child(view);
2052 err = view_set_child(view, diff_view);
2054 view_close(diff_view);
2057 *focus_view = diff_view;
2058 view->child_focussed = 1;
2060 *new_view = diff_view;
2063 if (s->selected_entry == NULL)
2065 if (view_is_parent_view(view))
2066 begin_x = view_split_begin_x(view->begin_x);
2067 err = browse_commit_tree(&tree_view, begin_x,
2068 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2071 if (view_is_parent_view(view)) {
2072 err = view_close_child(view);
2075 err = view_set_child(view, tree_view);
2077 view_close(tree_view);
2080 *focus_view = tree_view;
2081 view->child_focussed = 1;
2083 *new_view = tree_view;
2086 if (strcmp(s->in_repo_path, "/") == 0)
2088 parent_path = dirname(s->in_repo_path);
2089 if (parent_path && strcmp(parent_path, ".") != 0) {
2090 err = stop_log_thread(s);
2093 lv = view_open(view->nlines, view->ncols,
2094 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2096 return got_error_from_errno(
2098 err = open_log_view(lv, s->start_id, s->refs,
2099 s->repo, s->head_ref_name, parent_path, 0);
2102 if (view_is_parent_view(view))
2105 view_set_child(view->parent, lv);
2112 err = stop_log_thread(s);
2115 lv = view_open(view->nlines, view->ncols,
2116 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2118 return got_error_from_errno("view_open");
2119 err = get_head_commit_id(&start_id, s->head_ref_name ?
2120 s->head_ref_name : GOT_REF_HEAD, s->repo);
2125 in_repo_path = strdup(s->in_repo_path);
2126 if (in_repo_path == NULL) {
2129 return got_error_from_errno("strdup");
2131 err = open_log_view(lv, start_id, s->refs, s->repo,
2132 s->head_ref_name, in_repo_path, 0);
2148 static const struct got_error *
2149 apply_unveil(const char *repo_path, const char *worktree_path)
2151 const struct got_error *error;
2154 if (unveil("gmon.out", "rwc") != 0)
2155 return got_error_from_errno2("unveil", "gmon.out");
2157 if (repo_path && unveil(repo_path, "r") != 0)
2158 return got_error_from_errno2("unveil", repo_path);
2160 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2161 return got_error_from_errno2("unveil", worktree_path);
2163 if (unveil("/tmp", "rwc") != 0)
2164 return got_error_from_errno2("unveil", "/tmp");
2166 error = got_privsep_unveil_exec_helpers();
2170 if (unveil(NULL, NULL) != 0)
2171 return got_error_from_errno("unveil");
2181 halfdelay(1); /* Do fast refresh while initial view is loading. */
2184 intrflush(stdscr, FALSE);
2185 keypad(stdscr, TRUE);
2187 signal(SIGWINCH, tog_sigwinch);
2188 signal(SIGPIPE, tog_sigpipe);
2191 static const struct got_error *
2192 cmd_log(int argc, char *argv[])
2194 const struct got_error *error;
2195 struct got_repository *repo = NULL;
2196 struct got_worktree *worktree = NULL;
2197 struct got_reflist_head refs;
2198 struct got_object_id *start_id = NULL;
2199 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2200 char *start_commit = NULL;
2202 struct tog_view *view;
2204 SIMPLEQ_INIT(&refs);
2207 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2212 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2215 start_commit = optarg;
2218 repo_path = realpath(optarg, NULL);
2219 if (repo_path == NULL)
2220 err(1, "-r option");
2231 cwd = getcwd(NULL, 0);
2233 error = got_error_from_errno("getcwd");
2236 error = got_worktree_open(&worktree, cwd);
2237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2244 error = got_error_from_errno("strdup");
2247 } else if (argc == 1) {
2249 error = got_worktree_resolve_path(&path, worktree,
2254 path = strdup(argv[0]);
2256 error = got_error_from_errno("strdup");
2263 if (repo_path == NULL) {
2266 got_worktree_get_repo_path(worktree));
2268 repo_path = strdup(cwd);
2270 if (repo_path == NULL) {
2271 error = got_error_from_errno("strdup");
2277 error = got_repo_open(&repo, repo_path);
2281 error = apply_unveil(got_repo_get_path(repo),
2282 worktree ? got_worktree_get_root_path(worktree) : NULL);
2286 if (start_commit == NULL)
2287 error = get_head_commit_id(&start_id, worktree ?
2288 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2291 error = get_head_commit_id(&start_id, start_commit, repo);
2293 if (error->code != GOT_ERR_NOT_REF)
2295 error = got_repo_match_object_id_prefix(&start_id,
2296 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2302 error = got_ref_list(&refs, repo);
2306 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2308 error = got_error_from_errno("view_open");
2311 error = open_log_view(view, start_id, &refs, repo, worktree ?
2312 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2315 error = view_loop(view);
2322 got_repo_close(repo);
2324 got_worktree_close(worktree);
2325 got_ref_list_free(&refs);
2333 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2339 parse_next_line(FILE *f, size_t *len)
2344 const char delim[3] = { '\0', '\0', '\0'};
2346 line = fparseln(f, &linelen, &lineno, delim, 0);
2352 static const struct got_error *
2353 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2354 int *last_displayed_line, int *eof, int max_lines,
2357 const struct got_error *err;
2358 int nlines = 0, nprinted = 0;
2365 werase(view->window);
2368 err = format_line(&wline, &width, header, view->ncols);
2373 if (view_needs_focus_indication(view))
2374 wstandout(view->window);
2375 waddwstr(view->window, wline);
2376 if (view_needs_focus_indication(view))
2377 wstandend(view->window);
2378 if (width < view->ncols - 1)
2379 waddch(view->window, '\n');
2387 while (nprinted < max_lines) {
2388 line = parse_next_line(f, &len);
2393 if (++nlines < *first_displayed_line) {
2398 err = format_line(&wline, &width, line, view->ncols);
2403 waddwstr(view->window, wline);
2404 if (width < view->ncols - 1)
2405 waddch(view->window, '\n');
2406 if (++nprinted == 1)
2407 *first_displayed_line = nlines;
2412 *last_displayed_line = nlines;
2417 while (nprinted < view->nlines) {
2418 waddch(view->window, '\n');
2422 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2427 wstandout(view->window);
2428 waddwstr(view->window, wline);
2429 wstandend(view->window);
2436 get_datestr(time_t *time, char *datebuf)
2438 struct tm mytm, *tm;
2441 tm = gmtime_r(time, &mytm);
2444 s = asctime_r(tm, datebuf);
2447 p = strchr(s, '\n');
2453 static const struct got_error *
2454 write_commit_info(struct got_object_id *commit_id,
2455 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2457 const struct got_error *err = NULL;
2458 char datebuf[26], *datestr;
2459 struct got_commit_object *commit;
2460 char *id_str = NULL, *logmsg = NULL;
2461 time_t committer_time;
2462 const char *author, *committer;
2463 char *refs_str = NULL;
2466 err = build_refs_str(&refs_str, refs, commit_id, repo);
2471 err = got_object_open_as_commit(&commit, repo, commit_id);
2475 err = got_object_id_str(&id_str, commit_id);
2477 err = got_error_from_errno("got_object_id_str");
2481 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2482 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2483 err = got_error_from_errno("fprintf");
2486 if (fprintf(outfile, "from: %s\n",
2487 got_object_commit_get_author(commit)) < 0) {
2488 err = got_error_from_errno("fprintf");
2491 committer_time = got_object_commit_get_committer_time(commit);
2492 datestr = get_datestr(&committer_time, datebuf);
2493 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2494 err = got_error_from_errno("fprintf");
2497 author = got_object_commit_get_author(commit);
2498 committer = got_object_commit_get_committer(commit);
2499 if (strcmp(author, committer) != 0 &&
2500 fprintf(outfile, "via: %s\n", committer) < 0) {
2501 err = got_error_from_errno("fprintf");
2504 err = got_object_commit_get_logmsg(&logmsg, commit);
2507 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2508 err = got_error_from_errno("fprintf");
2515 got_object_commit_close(commit);
2519 static const struct got_error *
2520 create_diff(struct tog_diff_view_state *s)
2522 const struct got_error *err = NULL;
2528 err = got_error_from_errno("got_opentemp");
2531 if (s->f && fclose(s->f) != 0) {
2532 err = got_error_from_errno("fclose");
2538 err = got_object_get_type(&obj_type, s->repo, s->id1);
2540 err = got_object_get_type(&obj_type, s->repo, s->id2);
2545 case GOT_OBJ_TYPE_BLOB:
2546 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2547 s->diff_context, s->repo, f);
2549 case GOT_OBJ_TYPE_TREE:
2550 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2551 s->diff_context, s->repo, f);
2553 case GOT_OBJ_TYPE_COMMIT: {
2554 const struct got_object_id_queue *parent_ids;
2555 struct got_object_qid *pid;
2556 struct got_commit_object *commit2;
2558 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2561 /* Show commit info if we're diffing to a parent/root commit. */
2563 write_commit_info(s->id2, s->refs, s->repo, f);
2565 parent_ids = got_object_commit_get_parent_ids(commit2);
2566 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2567 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2568 write_commit_info(s->id2, s->refs,
2574 got_object_commit_close(commit2);
2576 err = got_diff_objects_as_commits(s->id1, s->id2,
2577 s->diff_context, s->repo, f);
2581 err = got_error(GOT_ERR_OBJ_TYPE);
2585 if (f && fflush(f) != 0 && err == NULL)
2586 err = got_error_from_errno("fflush");
2591 diff_view_indicate_progress(struct tog_view *view)
2593 mvwaddstr(view->window, 0, 0, "diffing...");
2598 static const struct got_error *
2599 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2600 struct got_object_id *id2, struct tog_view *log_view,
2601 struct got_reflist_head *refs, struct got_repository *repo)
2603 const struct got_error *err;
2605 if (id1 != NULL && id2 != NULL) {
2607 err = got_object_get_type(&type1, repo, id1);
2610 err = got_object_get_type(&type2, repo, id2);
2615 return got_error(GOT_ERR_OBJ_TYPE);
2619 view->state.diff.id1 = got_object_id_dup(id1);
2620 if (view->state.diff.id1 == NULL)
2621 return got_error_from_errno("got_object_id_dup");
2623 view->state.diff.id1 = NULL;
2625 view->state.diff.id2 = got_object_id_dup(id2);
2626 if (view->state.diff.id2 == NULL) {
2627 free(view->state.diff.id1);
2628 view->state.diff.id1 = NULL;
2629 return got_error_from_errno("got_object_id_dup");
2631 view->state.diff.f = NULL;
2632 view->state.diff.first_displayed_line = 1;
2633 view->state.diff.last_displayed_line = view->nlines;
2634 view->state.diff.diff_context = 3;
2635 view->state.diff.log_view = log_view;
2636 view->state.diff.repo = repo;
2637 view->state.diff.refs = refs;
2639 if (log_view && view_is_splitscreen(view))
2640 show_log_view(log_view); /* draw vborder */
2641 diff_view_indicate_progress(view);
2643 err = create_diff(&view->state.diff);
2645 free(view->state.diff.id1);
2646 view->state.diff.id1 = NULL;
2647 free(view->state.diff.id2);
2648 view->state.diff.id2 = NULL;
2652 view->show = show_diff_view;
2653 view->input = input_diff_view;
2654 view->close = close_diff_view;
2659 static const struct got_error *
2660 close_diff_view(struct tog_view *view)
2662 const struct got_error *err = NULL;
2664 free(view->state.diff.id1);
2665 view->state.diff.id1 = NULL;
2666 free(view->state.diff.id2);
2667 view->state.diff.id2 = NULL;
2668 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2669 err = got_error_from_errno("fclose");
2673 static const struct got_error *
2674 show_diff_view(struct tog_view *view)
2676 const struct got_error *err;
2677 struct tog_diff_view_state *s = &view->state.diff;
2678 char *id_str1 = NULL, *id_str2, *header;
2681 err = got_object_id_str(&id_str1, s->id1);
2685 err = got_object_id_str(&id_str2, s->id2);
2689 if (asprintf(&header, "diff %s %s",
2690 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2691 err = got_error_from_errno("asprintf");
2699 return draw_file(view, s->f, &s->first_displayed_line,
2700 &s->last_displayed_line, &s->eof, view->nlines,
2704 static const struct got_error *
2705 set_selected_commit(struct tog_diff_view_state *s,
2706 struct commit_queue_entry *entry)
2708 const struct got_error *err;
2709 const struct got_object_id_queue *parent_ids;
2710 struct got_commit_object *selected_commit;
2711 struct got_object_qid *pid;
2714 s->id2 = got_object_id_dup(entry->id);
2716 return got_error_from_errno("got_object_id_dup");
2718 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2721 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2723 pid = SIMPLEQ_FIRST(parent_ids);
2724 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2725 got_object_commit_close(selected_commit);
2729 static const struct got_error *
2730 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2731 struct tog_view **focus_view, struct tog_view *view, int ch)
2733 const struct got_error *err = NULL;
2734 struct tog_diff_view_state *s = &view->state.diff;
2735 struct tog_log_view_state *ls;
2736 struct commit_queue_entry *entry;
2742 if (s->first_displayed_line > 1)
2743 s->first_displayed_line--;
2747 if (s->first_displayed_line == 1)
2750 while (i++ < view->nlines - 1 &&
2751 s->first_displayed_line > 1)
2752 s->first_displayed_line--;
2757 s->first_displayed_line++;
2765 while (!s->eof && i++ < view->nlines - 1) {
2767 line = parse_next_line(s->f, NULL);
2768 s->first_displayed_line++;
2774 if (s->diff_context > 0) {
2776 diff_view_indicate_progress(view);
2777 err = create_diff(s);
2781 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2783 diff_view_indicate_progress(view);
2784 err = create_diff(s);
2789 if (s->log_view == NULL)
2791 ls = &s->log_view->state.log;
2792 entry = TAILQ_PREV(ls->selected_entry,
2793 commit_queue_head, entry);
2797 err = input_log_view(NULL, NULL, NULL, s->log_view,
2802 err = set_selected_commit(s, entry);
2806 s->first_displayed_line = 1;
2807 s->last_displayed_line = view->nlines;
2809 diff_view_indicate_progress(view);
2810 err = create_diff(s);
2814 if (s->log_view == NULL)
2816 ls = &s->log_view->state.log;
2818 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2819 ls->thread_args.commits_needed++;
2821 /* Display "loading..." in log view. */
2822 show_log_view(s->log_view);
2826 err = trigger_log_thread(1 /* load_all */,
2827 &ls->thread_args.commits_needed,
2828 &ls->thread_args.log_complete,
2829 &ls->thread_args.need_commits);
2833 err = input_log_view(NULL, NULL, NULL, s->log_view,
2838 entry = TAILQ_NEXT(ls->selected_entry, entry);
2842 err = set_selected_commit(s, entry);
2846 s->first_displayed_line = 1;
2847 s->last_displayed_line = view->nlines;
2849 diff_view_indicate_progress(view);
2850 err = create_diff(s);
2859 static const struct got_error *
2860 cmd_diff(int argc, char *argv[])
2862 const struct got_error *error = NULL;
2863 struct got_repository *repo = NULL;
2864 struct got_reflist_head refs;
2865 struct got_object_id *id1 = NULL, *id2 = NULL;
2866 char *repo_path = NULL;
2867 char *id_str1 = NULL, *id_str2 = NULL;
2869 struct tog_view *view;
2871 SIMPLEQ_INIT(&refs);
2874 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2879 while ((ch = getopt(argc, argv, "")) != -1) {
2891 usage_diff(); /* TODO show local worktree changes */
2892 } else if (argc == 2) {
2893 repo_path = getcwd(NULL, 0);
2894 if (repo_path == NULL)
2895 return got_error_from_errno("getcwd");
2898 } else if (argc == 3) {
2899 repo_path = realpath(argv[0], NULL);
2900 if (repo_path == NULL)
2901 return got_error_from_errno2("realpath", argv[0]);
2909 error = got_repo_open(&repo, repo_path);
2913 error = apply_unveil(got_repo_get_path(repo), NULL);
2917 error = got_repo_match_object_id_prefix(&id1, id_str1,
2918 GOT_OBJ_TYPE_ANY, repo);
2922 error = got_repo_match_object_id_prefix(&id2, id_str2,
2923 GOT_OBJ_TYPE_ANY, repo);
2927 error = got_ref_list(&refs, repo);
2931 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2933 error = got_error_from_errno("view_open");
2936 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2939 error = view_loop(view);
2943 got_repo_close(repo);
2944 got_ref_list_free(&refs);
2952 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2957 struct tog_blame_line {
2959 struct got_object_id *id;
2962 static const struct got_error *
2963 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2964 const char *path, struct tog_blame_line *lines, int nlines,
2965 int blame_complete, int selected_line, int *first_displayed_line,
2966 int *last_displayed_line, int *eof, int max_lines)
2968 const struct got_error *err;
2969 int lineno = 0, nprinted = 0;
2974 struct tog_blame_line *blame_line;
2975 struct got_object_id *prev_id = NULL;
2978 err = got_object_id_str(&id_str, id);
2983 werase(view->window);
2985 if (asprintf(&line, "commit %s", id_str) == -1) {
2986 err = got_error_from_errno("asprintf");
2991 err = format_line(&wline, &width, line, view->ncols);
2994 if (view_needs_focus_indication(view))
2995 wstandout(view->window);
2996 waddwstr(view->window, wline);
2997 if (view_needs_focus_indication(view))
2998 wstandend(view->window);
3001 if (width < view->ncols - 1)
3002 waddch(view->window, '\n');
3004 if (asprintf(&line, "[%d/%d] %s%s",
3005 *first_displayed_line - 1 + selected_line, nlines,
3006 blame_complete ? "" : "annotating... ", path) == -1) {
3008 return got_error_from_errno("asprintf");
3011 err = format_line(&wline, &width, line, view->ncols);
3016 waddwstr(view->window, wline);
3019 if (width < view->ncols - 1)
3020 waddch(view->window, '\n');
3023 while (nprinted < max_lines - 2) {
3024 line = parse_next_line(f, &len);
3029 if (++lineno < *first_displayed_line) {
3034 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3035 err = format_line(&wline, &width, line, wlimit);
3041 if (view->focussed && nprinted == selected_line - 1)
3042 wstandout(view->window);
3045 blame_line = &lines[lineno - 1];
3046 if (blame_line->annotated && prev_id &&
3047 got_object_id_cmp(prev_id, blame_line->id) == 0)
3048 waddstr(view->window, " ");
3049 else if (blame_line->annotated) {
3051 err = got_object_id_str(&id_str, blame_line->id);
3057 wprintw(view->window, "%.8s ", id_str);
3059 prev_id = blame_line->id;
3061 waddstr(view->window, "........ ");
3065 waddstr(view->window, "........ ");
3069 waddwstr(view->window, wline);
3070 while (width < wlimit) {
3071 waddch(view->window, ' ');
3074 if (view->focussed && nprinted == selected_line - 1)
3075 wstandend(view->window);
3076 if (++nprinted == 1)
3077 *first_displayed_line = lineno;
3082 *last_displayed_line = lineno;
3089 static const struct got_error *
3090 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3092 const struct got_error *err = NULL;
3093 struct tog_blame_cb_args *a = arg;
3094 struct tog_blame_line *line;
3097 if (nlines != a->nlines ||
3098 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3099 return got_error(GOT_ERR_RANGE);
3101 errcode = pthread_mutex_lock(&tog_mutex);
3103 return got_error_set_errno(errcode, "pthread_mutex_lock");
3105 if (*a->quit) { /* user has quit the blame view */
3106 err = got_error(GOT_ERR_ITER_COMPLETED);
3111 goto done; /* no change in this commit */
3113 line = &a->lines[lineno - 1];
3114 if (line->annotated)
3117 line->id = got_object_id_dup(id);
3118 if (line->id == NULL) {
3119 err = got_error_from_errno("got_object_id_dup");
3122 line->annotated = 1;
3124 errcode = pthread_mutex_unlock(&tog_mutex);
3126 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3131 blame_thread(void *arg)
3133 const struct got_error *err;
3134 struct tog_blame_thread_args *ta = arg;
3135 struct tog_blame_cb_args *a = ta->cb_args;
3138 err = got_blame(ta->path, a->commit_id, ta->repo,
3139 blame_cb, ta->cb_args);
3141 errcode = pthread_mutex_lock(&tog_mutex);
3143 return (void *)got_error_set_errno(errcode,
3144 "pthread_mutex_lock");
3146 got_repo_close(ta->repo);
3150 errcode = pthread_mutex_unlock(&tog_mutex);
3151 if (errcode && err == NULL)
3152 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3157 static struct got_object_id *
3158 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3159 int first_displayed_line, int selected_line)
3161 struct tog_blame_line *line;
3166 line = &lines[first_displayed_line - 1 + selected_line - 1];
3167 if (!line->annotated)
3173 static const struct got_error *
3174 stop_blame(struct tog_blame *blame)
3176 const struct got_error *err = NULL;
3179 if (blame->thread) {
3181 errcode = pthread_mutex_unlock(&tog_mutex);
3183 return got_error_set_errno(errcode,
3184 "pthread_mutex_unlock");
3185 errcode = pthread_join(blame->thread, (void **)&err);
3187 return got_error_set_errno(errcode, "pthread_join");
3188 errcode = pthread_mutex_lock(&tog_mutex);
3190 return got_error_set_errno(errcode,
3191 "pthread_mutex_lock");
3192 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3194 blame->thread = NULL;
3196 if (blame->thread_args.repo) {
3197 got_repo_close(blame->thread_args.repo);
3198 blame->thread_args.repo = NULL;
3201 if (fclose(blame->f) != 0 && err == NULL)
3202 err = got_error_from_errno("fclose");
3206 for (i = 0; i < blame->nlines; i++)
3207 free(blame->lines[i].id);
3209 blame->lines = NULL;
3211 free(blame->cb_args.commit_id);
3212 blame->cb_args.commit_id = NULL;
3217 static const struct got_error *
3218 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3219 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3220 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3221 struct got_repository *repo)
3223 const struct got_error *err = NULL;
3224 struct got_blob_object *blob = NULL;
3225 struct got_repository *thread_repo = NULL;
3226 struct got_object_id *obj_id = NULL;
3229 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3233 return got_error(GOT_ERR_NO_OBJ);
3235 err = got_object_get_type(&obj_type, repo, obj_id);
3239 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3240 err = got_error(GOT_ERR_OBJ_TYPE);
3244 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3247 blame->f = got_opentemp();
3248 if (blame->f == NULL) {
3249 err = got_error_from_errno("got_opentemp");
3252 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3253 &blame->line_offsets, blame->f, blob);
3254 if (err || blame->nlines == 0)
3257 /* Don't include \n at EOF in the blame line count. */
3258 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3261 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3262 if (blame->lines == NULL) {
3263 err = got_error_from_errno("calloc");
3267 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3271 blame->cb_args.view = view;
3272 blame->cb_args.lines = blame->lines;
3273 blame->cb_args.nlines = blame->nlines;
3274 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3275 if (blame->cb_args.commit_id == NULL) {
3276 err = got_error_from_errno("got_object_id_dup");
3279 blame->cb_args.quit = done;
3281 blame->thread_args.path = path;
3282 blame->thread_args.repo = thread_repo;
3283 blame->thread_args.cb_args = &blame->cb_args;
3284 blame->thread_args.complete = blame_complete;
3285 *blame_complete = 0;
3289 got_object_blob_close(blob);
3296 static const struct got_error *
3297 open_blame_view(struct tog_view *view, char *path,
3298 struct got_object_id *commit_id, struct got_reflist_head *refs,
3299 struct got_repository *repo)
3301 const struct got_error *err = NULL;
3302 struct tog_blame_view_state *s = &view->state.blame;
3304 SIMPLEQ_INIT(&s->blamed_commits);
3306 s->path = strdup(path);
3307 if (s->path == NULL)
3308 return got_error_from_errno("strdup");
3310 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3316 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3317 s->first_displayed_line = 1;
3318 s->last_displayed_line = view->nlines;
3319 s->selected_line = 1;
3320 s->blame_complete = 0;
3323 s->commit_id = commit_id;
3324 memset(&s->blame, 0, sizeof(s->blame));
3326 view->show = show_blame_view;
3327 view->input = input_blame_view;
3328 view->close = close_blame_view;
3329 view->search_start = search_start_blame_view;
3330 view->search_next = search_next_blame_view;
3332 return run_blame(&s->blame, view, &s->blame_complete,
3333 &s->first_displayed_line, &s->last_displayed_line,
3334 &s->selected_line, &s->done, &s->eof, s->path,
3335 s->blamed_commit->id, s->repo);
3338 static const struct got_error *
3339 close_blame_view(struct tog_view *view)
3341 const struct got_error *err = NULL;
3342 struct tog_blame_view_state *s = &view->state.blame;
3344 if (s->blame.thread)
3345 err = stop_blame(&s->blame);
3347 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3348 struct got_object_qid *blamed_commit;
3349 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3350 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3351 got_object_qid_free(blamed_commit);
3359 static const struct got_error *
3360 search_start_blame_view(struct tog_view *view)
3362 struct tog_blame_view_state *s = &view->state.blame;
3364 s->matched_line = 0;
3369 match_line(const char *line, regex_t *regex)
3371 regmatch_t regmatch;
3373 return regexec(regex, line, 1, ®match, 0) == 0;
3377 static const struct got_error *
3378 search_next_blame_view(struct tog_view *view)
3380 struct tog_blame_view_state *s = &view->state.blame;
3383 if (!view->searching) {
3384 view->search_next_done = 1;
3388 if (s->matched_line) {
3389 if (view->searching == TOG_SEARCH_FORWARD)
3390 lineno = s->matched_line + 1;
3392 lineno = s->matched_line - 1;
3394 if (view->searching == TOG_SEARCH_FORWARD)
3397 lineno = s->blame.nlines;
3405 if (lineno <= 0 || lineno > s->blame.nlines) {
3406 if (s->matched_line == 0) {
3407 view->search_next_done = 1;
3412 if (view->searching == TOG_SEARCH_FORWARD)
3415 lineno = s->blame.nlines;
3418 offset = s->blame.line_offsets[lineno - 1];
3419 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3421 return got_error_from_errno("fseeko");
3424 line = parse_next_line(s->blame.f, &len);
3425 if (line && match_line(line, &view->regex)) {
3426 view->search_next_done = 1;
3427 s->matched_line = lineno;
3432 if (view->searching == TOG_SEARCH_FORWARD)
3438 if (s->matched_line) {
3439 s->first_displayed_line = s->matched_line;
3440 s->selected_line = 1;
3446 static const struct got_error *
3447 show_blame_view(struct tog_view *view)
3449 const struct got_error *err = NULL;
3450 struct tog_blame_view_state *s = &view->state.blame;
3453 if (s->blame.thread == NULL) {
3454 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3455 &s->blame.thread_args);
3457 return got_error_set_errno(errcode, "pthread_create");
3459 halfdelay(1); /* fast refresh while annotating */
3462 if (s->blame_complete)
3463 halfdelay(10); /* disable fast refresh */
3465 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3466 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3467 s->selected_line, &s->first_displayed_line,
3468 &s->last_displayed_line, &s->eof, view->nlines);
3474 static const struct got_error *
3475 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3476 struct tog_view **focus_view, struct tog_view *view, int ch)
3478 const struct got_error *err = NULL, *thread_err = NULL;
3479 struct tog_view *diff_view;
3480 struct tog_blame_view_state *s = &view->state.blame;
3489 if (s->selected_line > 1)
3491 else if (s->selected_line == 1 &&
3492 s->first_displayed_line > 1)
3493 s->first_displayed_line--;
3496 if (s->first_displayed_line == 1) {
3497 s->selected_line = 1;
3500 if (s->first_displayed_line > view->nlines - 2)
3501 s->first_displayed_line -=
3504 s->first_displayed_line = 1;
3508 if (s->selected_line < view->nlines - 2 &&
3509 s->first_displayed_line +
3510 s->selected_line <= s->blame.nlines)
3512 else if (s->last_displayed_line <
3514 s->first_displayed_line++;
3518 struct got_object_id *id = NULL;
3519 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3520 s->first_displayed_line, s->selected_line);
3524 struct got_commit_object *commit;
3525 struct got_object_qid *pid;
3526 struct got_object_id *blob_id = NULL;
3528 err = got_object_open_as_commit(&commit,
3532 pid = SIMPLEQ_FIRST(
3533 got_object_commit_get_parent_ids(commit));
3535 got_object_commit_close(commit);
3538 /* Check if path history ends here. */
3539 err = got_object_id_by_path(&blob_id, s->repo,
3542 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3544 got_object_commit_close(commit);
3547 err = got_object_get_type(&obj_type, s->repo,
3550 /* Can't blame non-blob type objects. */
3551 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3552 got_object_commit_close(commit);
3555 err = got_object_qid_alloc(&s->blamed_commit,
3557 got_object_commit_close(commit);
3559 if (got_object_id_cmp(id,
3560 s->blamed_commit->id) == 0)
3562 err = got_object_qid_alloc(&s->blamed_commit,
3568 thread_err = stop_blame(&s->blame);
3572 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3573 s->blamed_commit, entry);
3574 err = run_blame(&s->blame, view, &s->blame_complete,
3575 &s->first_displayed_line, &s->last_displayed_line,
3576 &s->selected_line, &s->done, &s->eof,
3577 s->path, s->blamed_commit->id, s->repo);
3583 struct got_object_qid *first;
3584 first = SIMPLEQ_FIRST(&s->blamed_commits);
3585 if (!got_object_id_cmp(first->id, s->commit_id))
3588 thread_err = stop_blame(&s->blame);
3592 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3593 got_object_qid_free(s->blamed_commit);
3595 SIMPLEQ_FIRST(&s->blamed_commits);
3596 err = run_blame(&s->blame, view, &s->blame_complete,
3597 &s->first_displayed_line, &s->last_displayed_line,
3598 &s->selected_line, &s->done, &s->eof, s->path,
3599 s->blamed_commit->id, s->repo);
3606 struct got_object_id *id = NULL;
3607 struct got_object_qid *pid;
3608 struct got_commit_object *commit = NULL;
3609 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3610 s->first_displayed_line, s->selected_line);
3613 err = got_object_open_as_commit(&commit, s->repo, id);
3616 pid = SIMPLEQ_FIRST(
3617 got_object_commit_get_parent_ids(commit));
3618 if (view_is_parent_view(view))
3619 begin_x = view_split_begin_x(view->begin_x);
3620 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3621 if (diff_view == NULL) {
3622 got_object_commit_close(commit);
3623 err = got_error_from_errno("view_open");
3626 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3627 id, NULL, s->refs, s->repo);
3628 got_object_commit_close(commit);
3630 view_close(diff_view);
3633 if (view_is_parent_view(view)) {
3634 err = view_close_child(view);
3637 err = view_set_child(view, diff_view);
3639 view_close(diff_view);
3642 *focus_view = diff_view;
3643 view->child_focussed = 1;
3645 *new_view = diff_view;
3652 if (s->last_displayed_line >= s->blame.nlines &&
3653 s->selected_line >= MIN(s->blame.nlines,
3654 view->nlines - 2)) {
3657 if (s->last_displayed_line >= s->blame.nlines &&
3658 s->selected_line < view->nlines - 2) {
3659 s->selected_line = MIN(s->blame.nlines,
3663 if (s->last_displayed_line + view->nlines - 2
3665 s->first_displayed_line +=
3668 s->first_displayed_line =
3673 if (s->selected_line > view->nlines - 2) {
3674 s->selected_line = MIN(s->blame.nlines,
3681 return thread_err ? thread_err : err;
3684 static const struct got_error *
3685 cmd_blame(int argc, char *argv[])
3687 const struct got_error *error;
3688 struct got_repository *repo = NULL;
3689 struct got_reflist_head refs;
3690 struct got_worktree *worktree = NULL;
3691 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3692 struct got_object_id *commit_id = NULL;
3693 char *commit_id_str = NULL;
3695 struct tog_view *view;
3697 SIMPLEQ_INIT(&refs);
3700 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3705 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3708 commit_id_str = optarg;
3711 repo_path = realpath(optarg, NULL);
3712 if (repo_path == NULL)
3713 err(1, "-r option");
3729 cwd = getcwd(NULL, 0);
3731 error = got_error_from_errno("getcwd");
3734 if (repo_path == NULL) {
3735 error = got_worktree_open(&worktree, cwd);
3736 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3742 strdup(got_worktree_get_repo_path(worktree));
3743 if (repo_path == NULL)
3744 error = got_error_from_errno("strdup");
3748 repo_path = strdup(cwd);
3749 if (repo_path == NULL) {
3750 error = got_error_from_errno("strdup");
3758 error = got_repo_open(&repo, repo_path);
3762 error = apply_unveil(got_repo_get_path(repo), NULL);
3767 const char *prefix = got_worktree_get_path_prefix(worktree);
3768 char *p, *worktree_subdir = cwd +
3769 strlen(got_worktree_get_root_path(worktree));
3770 if (asprintf(&p, "%s%s%s%s%s",
3771 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3772 worktree_subdir, worktree_subdir[0] ? "/" : "",
3774 error = got_error_from_errno("asprintf");
3777 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3780 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3785 if (commit_id_str == NULL) {
3786 struct got_reference *head_ref;
3787 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3790 error = got_ref_resolve(&commit_id, repo, head_ref);
3791 got_ref_close(head_ref);
3793 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3795 if (error->code != GOT_ERR_NOT_REF)
3797 error = got_repo_match_object_id_prefix(&commit_id,
3798 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3804 error = got_ref_list(&refs, repo);
3808 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3810 error = got_error_from_errno("view_open");
3813 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3816 error = view_loop(view);
3822 got_worktree_close(worktree);
3824 got_repo_close(repo);
3825 got_ref_list_free(&refs);
3829 static const struct got_error *
3830 draw_tree_entries(struct tog_view *view,
3831 struct got_tree_entry **first_displayed_entry,
3832 struct got_tree_entry **last_displayed_entry,
3833 struct got_tree_entry **selected_entry, int *ndisplayed,
3834 const char *label, int show_ids, const char *parent_path,
3835 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3837 const struct got_error *err = NULL;
3838 struct got_tree_entry *te;
3844 werase(view->window);
3849 err = format_line(&wline, &width, label, view->ncols);
3852 if (view_needs_focus_indication(view))
3853 wstandout(view->window);
3854 waddwstr(view->window, wline);
3855 if (view_needs_focus_indication(view))
3856 wstandend(view->window);
3859 if (width < view->ncols - 1)
3860 waddch(view->window, '\n');
3863 err = format_line(&wline, &width, parent_path, view->ncols);
3866 waddwstr(view->window, wline);
3869 if (width < view->ncols - 1)
3870 waddch(view->window, '\n');
3873 waddch(view->window, '\n');
3877 te = SIMPLEQ_FIRST(&entries->head);
3878 if (*first_displayed_entry == NULL) {
3879 if (selected == 0) {
3881 wstandout(view->window);
3882 *selected_entry = NULL;
3884 waddstr(view->window, " ..\n"); /* parent directory */
3885 if (selected == 0 && view->focussed)
3886 wstandend(view->window);
3893 while (te != *first_displayed_entry)
3894 te = SIMPLEQ_NEXT(te, entry);
3898 char *line = NULL, *id_str = NULL;
3899 const char *modestr = "";
3902 err = got_object_id_str(&id_str, te->id);
3904 return got_error_from_errno(
3905 "got_object_id_str");
3907 if (S_ISLNK(te->mode))
3909 else if (S_ISDIR(te->mode))
3911 else if (te->mode & S_IXUSR)
3913 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3914 te->name, modestr) == -1) {
3916 return got_error_from_errno("asprintf");
3919 err = format_line(&wline, &width, line, view->ncols);
3924 if (n == selected) {
3926 wstandout(view->window);
3927 *selected_entry = te;
3929 waddwstr(view->window, wline);
3930 if (width < view->ncols - 1)
3931 waddch(view->window, '\n');
3932 if (n == selected && view->focussed)
3933 wstandend(view->window);
3939 *last_displayed_entry = te;
3942 te = SIMPLEQ_NEXT(te, entry);
3949 tree_scroll_up(struct tog_view *view,
3950 struct got_tree_entry **first_displayed_entry, int maxscroll,
3951 const struct got_tree_entries *entries, int isroot)
3953 struct got_tree_entry *te, *prev;
3956 if (*first_displayed_entry == NULL)
3959 te = SIMPLEQ_FIRST(&entries->head);
3960 if (*first_displayed_entry == te) {
3962 *first_displayed_entry = NULL;
3966 /* XXX this is stupid... switch to TAILQ? */
3967 for (i = 0; i < maxscroll; i++) {
3968 while (te != *first_displayed_entry) {
3970 te = SIMPLEQ_NEXT(te, entry);
3972 *first_displayed_entry = prev;
3973 te = SIMPLEQ_FIRST(&entries->head);
3975 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3976 *first_displayed_entry = NULL;
3980 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3981 struct got_tree_entry *last_displayed_entry,
3982 const struct got_tree_entries *entries)
3984 struct got_tree_entry *next, *last;
3987 if (*first_displayed_entry)
3988 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3990 next = SIMPLEQ_FIRST(&entries->head);
3991 last = last_displayed_entry;
3992 while (next && last && n++ < maxscroll) {
3993 last = SIMPLEQ_NEXT(last, entry);
3995 *first_displayed_entry = next;
3996 next = SIMPLEQ_NEXT(next, entry);
4002 static const struct got_error *
4003 tree_entry_path(char **path, struct tog_parent_trees *parents,
4004 struct got_tree_entry *te)
4006 const struct got_error *err = NULL;
4007 struct tog_parent_tree *pt;
4008 size_t len = 2; /* for leading slash and NUL */
4010 TAILQ_FOREACH(pt, parents, entry)
4011 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4013 len += strlen(te->name);
4015 *path = calloc(1, len);
4017 return got_error_from_errno("calloc");
4020 pt = TAILQ_LAST(parents, tog_parent_trees);
4022 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4023 err = got_error(GOT_ERR_NO_SPACE);
4026 if (strlcat(*path, "/", len) >= len) {
4027 err = got_error(GOT_ERR_NO_SPACE);
4030 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4033 if (strlcat(*path, te->name, len) >= len) {
4034 err = got_error(GOT_ERR_NO_SPACE);
4046 static const struct got_error *
4047 blame_tree_entry(struct tog_view **new_view, int begin_x,
4048 struct got_tree_entry *te, struct tog_parent_trees *parents,
4049 struct got_object_id *commit_id, struct got_reflist_head *refs,
4050 struct got_repository *repo)
4052 const struct got_error *err = NULL;
4054 struct tog_view *blame_view;
4058 err = tree_entry_path(&path, parents, te);
4062 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4063 if (blame_view == NULL) {
4064 err = got_error_from_errno("view_open");
4068 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4070 view_close(blame_view);
4072 *new_view = blame_view;
4078 static const struct got_error *
4079 log_tree_entry(struct tog_view **new_view, int begin_x,
4080 struct got_tree_entry *te, struct tog_parent_trees *parents,
4081 struct got_object_id *commit_id, struct got_reflist_head *refs,
4082 struct got_repository *repo)
4084 struct tog_view *log_view;
4085 const struct got_error *err = NULL;
4090 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4091 if (log_view == NULL)
4092 return got_error_from_errno("view_open");
4094 err = tree_entry_path(&path, parents, te);
4098 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4100 view_close(log_view);
4102 *new_view = log_view;
4107 static const struct got_error *
4108 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4109 struct got_object_id *commit_id, struct got_reflist_head *refs,
4110 struct got_repository *repo)
4112 const struct got_error *err = NULL;
4113 char *commit_id_str = NULL;
4114 struct tog_tree_view_state *s = &view->state.tree;
4116 TAILQ_INIT(&s->parents);
4118 err = got_object_id_str(&commit_id_str, commit_id);
4122 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4123 err = got_error_from_errno("asprintf");
4127 s->root = s->tree = root;
4128 s->entries = got_object_tree_get_entries(root);
4129 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4130 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4131 s->commit_id = got_object_id_dup(commit_id);
4132 if (s->commit_id == NULL) {
4133 err = got_error_from_errno("got_object_id_dup");
4139 view->show = show_tree_view;
4140 view->input = input_tree_view;
4141 view->close = close_tree_view;
4142 view->search_start = search_start_tree_view;
4143 view->search_next = search_next_tree_view;
4145 free(commit_id_str);
4147 free(s->tree_label);
4148 s->tree_label = NULL;
4153 static const struct got_error *
4154 close_tree_view(struct tog_view *view)
4156 struct tog_tree_view_state *s = &view->state.tree;
4158 free(s->tree_label);
4159 s->tree_label = NULL;
4161 s->commit_id = NULL;
4162 while (!TAILQ_EMPTY(&s->parents)) {
4163 struct tog_parent_tree *parent;
4164 parent = TAILQ_FIRST(&s->parents);
4165 TAILQ_REMOVE(&s->parents, parent, entry);
4169 if (s->tree != s->root)
4170 got_object_tree_close(s->tree);
4171 got_object_tree_close(s->root);
4176 static const struct got_error *
4177 search_start_tree_view(struct tog_view *view)
4179 struct tog_tree_view_state *s = &view->state.tree;
4181 s->matched_entry = NULL;
4186 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4188 regmatch_t regmatch;
4190 return regexec(regex, te->name, 1, ®match, 0) == 0;
4193 static const struct got_error *
4194 search_next_tree_view(struct tog_view *view)
4196 struct tog_tree_view_state *s = &view->state.tree;
4197 struct got_tree_entry *entry = NULL, *te;
4199 if (!view->searching) {
4200 view->search_next_done = 1;
4204 if (s->matched_entry) {
4205 if (view->searching == TOG_SEARCH_FORWARD) {
4206 if (s->selected_entry)
4207 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4209 entry = SIMPLEQ_FIRST(&s->entries->head);
4212 if (s->selected_entry == NULL) {
4213 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4216 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4218 if (SIMPLEQ_NEXT(te, entry) ==
4225 if (view->searching == TOG_SEARCH_FORWARD)
4226 entry = SIMPLEQ_FIRST(&s->entries->head);
4228 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4234 if (entry == NULL) {
4235 if (s->matched_entry == NULL) {
4236 view->search_next_done = 1;
4239 if (view->searching == TOG_SEARCH_FORWARD)
4240 entry = SIMPLEQ_FIRST(&s->entries->head);
4242 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4247 if (match_tree_entry(entry, &view->regex)) {
4248 view->search_next_done = 1;
4249 s->matched_entry = entry;
4253 if (view->searching == TOG_SEARCH_FORWARD)
4254 entry = SIMPLEQ_NEXT(entry, entry);
4256 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4259 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4260 if (SIMPLEQ_NEXT(te, entry) == entry) {
4269 if (s->matched_entry) {
4270 s->first_displayed_entry = s->matched_entry;
4277 static const struct got_error *
4278 show_tree_view(struct tog_view *view)
4280 const struct got_error *err = NULL;
4281 struct tog_tree_view_state *s = &view->state.tree;
4284 err = tree_entry_path(&parent_path, &s->parents, NULL);
4288 err = draw_tree_entries(view, &s->first_displayed_entry,
4289 &s->last_displayed_entry, &s->selected_entry,
4290 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4291 s->entries, s->selected, view->nlines, s->tree == s->root);
4298 static const struct got_error *
4299 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4300 struct tog_view **focus_view, struct tog_view *view, int ch)
4302 const struct got_error *err = NULL;
4303 struct tog_tree_view_state *s = &view->state.tree;
4304 struct tog_view *log_view;
4305 int begin_x = 0, nscrolled;
4309 s->show_ids = !s->show_ids;
4312 if (!s->selected_entry)
4314 if (view_is_parent_view(view))
4315 begin_x = view_split_begin_x(view->begin_x);
4316 err = log_tree_entry(&log_view, begin_x,
4317 s->selected_entry, &s->parents,
4318 s->commit_id, s->refs, s->repo);
4319 if (view_is_parent_view(view)) {
4320 err = view_close_child(view);
4323 err = view_set_child(view, log_view);
4325 view_close(log_view);
4328 *focus_view = log_view;
4329 view->child_focussed = 1;
4331 *new_view = log_view;
4335 if (s->selected > 0) {
4337 if (s->selected == 0)
4340 if (s->selected > 0)
4342 tree_scroll_up(view, &s->first_displayed_entry, 1,
4343 s->entries, s->tree == s->root);
4346 tree_scroll_up(view, &s->first_displayed_entry,
4347 MAX(0, view->nlines - 4 - s->selected), s->entries,
4348 s->tree == s->root);
4350 if (SIMPLEQ_FIRST(&s->entries->head) ==
4351 s->first_displayed_entry && s->tree != s->root)
4352 s->first_displayed_entry = NULL;
4356 if (s->selected < s->ndisplayed - 1) {
4360 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4361 /* can't scroll any further */
4363 tree_scroll_down(&s->first_displayed_entry, 1,
4364 s->last_displayed_entry, s->entries);
4367 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4369 /* can't scroll any further; move cursor down */
4370 if (s->selected < s->ndisplayed - 1)
4371 s->selected = s->ndisplayed - 1;
4374 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4375 view->nlines, s->last_displayed_entry, s->entries);
4376 if (nscrolled < view->nlines) {
4378 struct got_tree_entry *te;
4379 te = s->first_displayed_entry;
4382 te = SIMPLEQ_NEXT(te, entry);
4384 s->selected = ndisplayed - 1;
4390 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4391 struct tog_parent_tree *parent;
4392 /* user selected '..' */
4393 if (s->tree == s->root)
4395 parent = TAILQ_FIRST(&s->parents);
4396 TAILQ_REMOVE(&s->parents, parent,
4398 got_object_tree_close(s->tree);
4399 s->tree = parent->tree;
4401 got_object_tree_get_entries(s->tree);
4402 s->first_displayed_entry =
4403 parent->first_displayed_entry;
4405 parent->selected_entry;
4406 s->selected = parent->selected;
4408 } else if (S_ISDIR(s->selected_entry->mode)) {
4409 struct got_tree_object *subtree;
4410 err = got_object_open_as_tree(&subtree,
4411 s->repo, s->selected_entry->id);
4414 err = tree_view_visit_subtree(subtree, s);
4416 got_object_tree_close(subtree);
4419 } else if (S_ISREG(s->selected_entry->mode)) {
4420 struct tog_view *blame_view;
4421 int begin_x = view_is_parent_view(view) ?
4422 view_split_begin_x(view->begin_x) : 0;
4424 err = blame_tree_entry(&blame_view, begin_x,
4425 s->selected_entry, &s->parents,
4426 s->commit_id, s->refs, s->repo);
4429 if (view_is_parent_view(view)) {
4430 err = view_close_child(view);
4433 err = view_set_child(view, blame_view);
4435 view_close(blame_view);
4438 *focus_view = blame_view;
4439 view->child_focussed = 1;
4441 *new_view = blame_view;
4445 if (s->selected > view->nlines)
4446 s->selected = s->ndisplayed - 1;
4459 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4464 static const struct got_error *
4465 cmd_tree(int argc, char *argv[])
4467 const struct got_error *error;
4468 struct got_repository *repo = NULL;
4469 struct got_reflist_head refs;
4470 char *repo_path = NULL;
4471 struct got_object_id *commit_id = NULL;
4472 char *commit_id_arg = NULL;
4473 struct got_commit_object *commit = NULL;
4474 struct got_tree_object *tree = NULL;
4476 struct tog_view *view;
4478 SIMPLEQ_INIT(&refs);
4481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4486 while ((ch = getopt(argc, argv, "c:")) != -1) {
4489 commit_id_arg = optarg;
4501 struct got_worktree *worktree;
4502 char *cwd = getcwd(NULL, 0);
4504 return got_error_from_errno("getcwd");
4505 error = got_worktree_open(&worktree, cwd);
4506 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4511 strdup(got_worktree_get_repo_path(worktree));
4512 got_worktree_close(worktree);
4515 if (repo_path == NULL) {
4516 error = got_error_from_errno("strdup");
4519 } else if (argc == 1) {
4520 repo_path = realpath(argv[0], NULL);
4521 if (repo_path == NULL)
4522 return got_error_from_errno2("realpath", argv[0]);
4528 error = got_repo_open(&repo, repo_path);
4532 error = apply_unveil(got_repo_get_path(repo), NULL);
4536 if (commit_id_arg == NULL)
4537 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4539 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4541 if (error->code != GOT_ERR_NOT_REF)
4543 error = got_repo_match_object_id_prefix(&commit_id,
4544 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4550 error = got_object_open_as_commit(&commit, repo, commit_id);
4554 error = got_object_open_as_tree(&tree, repo,
4555 got_object_commit_get_tree_id(commit));
4559 error = got_ref_list(&refs, repo);
4563 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4565 error = got_error_from_errno("view_open");
4568 error = open_tree_view(view, tree, commit_id, &refs, repo);
4571 error = view_loop(view);
4576 got_object_commit_close(commit);
4578 got_object_tree_close(tree);
4580 got_repo_close(repo);
4581 got_ref_list_free(&refs);
4590 fprintf(stderr, "commands:");
4591 for (i = 0; i < nitems(tog_commands); i++) {
4592 struct tog_cmd *cmd = &tog_commands[i];
4593 fprintf(stderr, " %s", cmd->name);
4595 fputc('\n', stderr);
4601 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4609 make_argv(const char *arg0, const char *arg1)
4612 int argc = (arg1 == NULL ? 1 : 2);
4614 argv = calloc(argc, sizeof(char *));
4617 argv[0] = strdup(arg0);
4618 if (argv[0] == NULL)
4621 argv[1] = strdup(arg1);
4622 if (argv[1] == NULL)
4630 main(int argc, char *argv[])
4632 const struct got_error *error = NULL;
4633 struct tog_cmd *cmd = NULL;
4634 int ch, hflag = 0, Vflag = 0;
4635 char **cmd_argv = NULL;
4637 setlocale(LC_CTYPE, "");
4639 while ((ch = getopt(argc, argv, "hV")) != -1) {
4659 got_version_print_str();
4666 /* Build an argument vector which runs a default command. */
4667 cmd = &tog_commands[0];
4668 cmd_argv = make_argv(cmd->name, NULL);
4673 /* Did the user specific a command? */
4674 for (i = 0; i < nitems(tog_commands); i++) {
4675 if (strncmp(tog_commands[i].name, argv[0],
4676 strlen(argv[0])) == 0) {
4677 cmd = &tog_commands[i];
4683 fprintf(stderr, "%s: unknown command '%s'\n",
4684 getprogname(), argv[0]);
4693 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4698 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);