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;
543 if (view->nlines < 1)
546 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
548 wclrtoeol(view->window);
552 ret = wgetnstr(view->window, pattern, sizeof(pattern));
558 if (view->searching) {
559 regfree(&view->regex);
563 if (regcomp(&view->regex, pattern,
564 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
565 err = view->search_start(view);
567 regfree(&view->regex);
570 view->searching = TOG_SEARCH_FORWARD;
571 view->search_next_done = 0;
572 view->search_next(view);
578 static const struct got_error *
579 view_input(struct tog_view **new, struct tog_view **dead,
580 struct tog_view **focus, int *done, struct tog_view *view,
581 struct tog_view_list_head *views)
583 const struct got_error *err = NULL;
591 if (view->searching && !view->search_next_done) {
592 errcode = pthread_mutex_unlock(&tog_mutex);
594 return got_error_set_errno(errcode,
595 "pthread_mutex_unlock");
597 errcode = pthread_mutex_lock(&tog_mutex);
599 return got_error_set_errno(errcode,
600 "pthread_mutex_lock");
601 view->search_next(view);
605 nodelay(stdscr, FALSE);
606 /* Allow threads to make progress while we are waiting for input. */
607 errcode = pthread_mutex_unlock(&tog_mutex);
609 return got_error_set_errno(errcode, "pthread_mutex_unlock");
610 ch = wgetch(view->window);
611 errcode = pthread_mutex_lock(&tog_mutex);
613 return got_error_set_errno(errcode, "pthread_mutex_lock");
614 nodelay(stdscr, TRUE);
616 if (tog_sigwinch_received) {
618 tog_sigwinch_received = 0;
619 TAILQ_FOREACH(v, views, entry) {
620 err = view_resize(v);
623 err = v->input(new, dead, focus, v, KEY_RESIZE);
634 *focus = view->child;
635 view->child_focussed = 1;
636 } else if (view->parent) {
637 *focus = view->parent;
638 view->parent->child_focussed = 0;
642 err = view->input(new, dead, focus, view, ch);
649 if (view_is_parent_view(view)) {
650 if (view->child == NULL)
652 if (view_is_splitscreen(view->child)) {
653 *focus = view->child;
654 view->child_focussed = 1;
655 err = view_fullscreen(view->child);
657 err = view_splitscreen(view->child);
660 err = view->child->input(new, dead, focus,
661 view->child, KEY_RESIZE);
663 if (view_is_splitscreen(view)) {
665 view->parent->child_focussed = 1;
666 err = view_fullscreen(view);
668 err = view_splitscreen(view);
672 err = view->input(new, dead, focus, view,
679 if (view->search_start)
680 view_search_start(view);
682 err = view->input(new, dead, focus, view, ch);
686 if (view->search_next && view->searching) {
687 view->searching = (ch == 'n' ?
688 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
689 view->search_next_done = 0;
690 view->search_next(view);
692 err = view->input(new, dead, focus, view, ch);
695 err = view->input(new, dead, focus, view, ch);
703 view_vborder(struct tog_view *view)
706 struct tog_view *view_above;
709 return view_vborder(view->parent);
711 panel = panel_above(view->panel);
715 view_above = panel_userptr(panel);
716 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
717 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
721 view_needs_focus_indication(struct tog_view *view)
723 if (view_is_parent_view(view)) {
724 if (view->child == NULL || view->child_focussed)
726 if (!view_is_splitscreen(view->child))
728 } else if (!view_is_splitscreen(view))
731 return view->focussed;
734 static const struct got_error *
735 view_loop(struct tog_view *view)
737 const struct got_error *err = NULL;
738 struct tog_view_list_head views;
739 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
740 int fast_refresh = 10;
741 int done = 0, errcode;
743 errcode = pthread_mutex_lock(&tog_mutex);
745 return got_error_set_errno(errcode, "pthread_mutex_lock");
748 TAILQ_INSERT_HEAD(&views, view, entry);
752 err = view->show(view);
757 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
758 /* Refresh fast during initialization, then become slower. */
759 if (fast_refresh && fast_refresh-- == 0)
760 halfdelay(10); /* switch to once per second */
762 err = view_input(&new_view, &dead_view, &focus_view, &done,
767 struct tog_view *prev = NULL;
769 if (view_is_parent_view(dead_view))
770 prev = TAILQ_PREV(dead_view,
771 tog_view_list_head, entry);
772 else if (view->parent != dead_view)
775 if (dead_view->parent)
776 dead_view->parent->child = NULL;
778 TAILQ_REMOVE(&views, dead_view, entry);
780 err = view_close(dead_view);
781 if (err || (dead_view == main_view && new_view == NULL))
784 if (view == dead_view) {
789 else if (!TAILQ_EMPTY(&views))
790 view = TAILQ_LAST(&views,
795 if (view->child && view->child_focussed)
796 focus_view = view->child;
803 struct tog_view *v, *t;
804 /* Only allow one parent view per type. */
805 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
806 if (v->type != new_view->type)
808 TAILQ_REMOVE(&views, v, entry);
814 TAILQ_INSERT_TAIL(&views, new_view, entry);
816 if (focus_view == NULL)
817 focus_view = new_view;
820 show_panel(focus_view->panel);
823 focus_view->focussed = 1;
826 show_panel(new_view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
831 if (focus_view == NULL) {
833 show_panel(view->panel);
834 if (view->child && view_is_splitscreen(view->child))
835 show_panel(view->child->panel);
839 err = view->parent->show(view->parent);
843 err = view->show(view);
847 err = view->child->show(view->child);
856 while (!TAILQ_EMPTY(&views)) {
857 view = TAILQ_FIRST(&views);
858 TAILQ_REMOVE(&views, view, entry);
862 errcode = pthread_mutex_unlock(&tog_mutex);
863 if (errcode && err == NULL)
864 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
874 "usage: %s log [-c commit] [-r repository-path] [path]\n",
879 /* Create newly allocated wide-character string equivalent to a byte string. */
880 static const struct got_error *
881 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
884 const struct got_error *err = NULL;
887 *wlen = mbstowcs(NULL, s, 0);
888 if (*wlen == (size_t)-1) {
891 return got_error_from_errno("mbstowcs");
893 /* byte string invalid in current encoding; try to "fix" it */
894 err = got_mbsavis(&vis, &vislen, s);
897 *wlen = mbstowcs(NULL, vis, 0);
898 if (*wlen == (size_t)-1) {
899 err = got_error_from_errno("mbstowcs"); /* give up */
904 *ws = calloc(*wlen + 1, sizeof(*ws));
906 err = got_error_from_errno("calloc");
910 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
911 err = got_error_from_errno("mbstowcs");
922 /* Format a line for display, ensuring that it won't overflow a width limit. */
923 static const struct got_error *
924 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
926 const struct got_error *err = NULL;
928 wchar_t *wline = NULL;
935 err = mbs2ws(&wline, &wlen, line);
940 while (i < wlen && cols < wlimit) {
941 int width = wcwidth(wline[i]);
948 if (cols + width <= wlimit)
953 if (wline[i] == L'\t')
954 cols += TABSIZE - ((cols + 1) % TABSIZE);
958 err = got_error_from_errno("wcwidth");
973 static const struct got_error*
974 build_refs_str(char **refs_str, struct got_reflist_head *refs,
975 struct got_object_id *id, struct got_repository *repo)
977 static const struct got_error *err = NULL;
978 struct got_reflist_entry *re;
984 SIMPLEQ_FOREACH(re, refs, entry) {
985 struct got_tag_object *tag = NULL;
988 name = got_ref_get_name(re->ref);
989 if (strcmp(name, GOT_REF_HEAD) == 0)
991 if (strncmp(name, "refs/", 5) == 0)
993 if (strncmp(name, "got/", 4) == 0)
995 if (strncmp(name, "heads/", 6) == 0)
997 if (strncmp(name, "remotes/", 8) == 0)
999 if (strncmp(name, "tags/", 5) == 0) {
1000 err = got_object_open_as_tag(&tag, repo, re->id);
1002 if (err->code != GOT_ERR_OBJ_TYPE)
1004 /* Ref points at something other than a tag. */
1009 cmp = got_object_id_cmp(tag ?
1010 got_object_tag_get_object_id(tag) : re->id, id);
1012 got_object_tag_close(tag);
1016 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1017 s ? ", " : "", name) == -1) {
1018 err = got_error_from_errno("asprintf");
1029 static const struct got_error *
1030 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1032 char *smallerthan, *at;
1034 smallerthan = strchr(author, '<');
1035 if (smallerthan && smallerthan[1] != '\0')
1036 author = smallerthan + 1;
1037 at = strchr(author, '@');
1040 return format_line(wauthor, author_width, author, limit);
1043 static const struct got_error *
1044 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1045 struct got_object_id *id, struct got_reflist_head *refs,
1046 int author_display_cols)
1048 const struct got_error *err = NULL;
1049 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1050 char *logmsg0 = NULL, *logmsg = NULL;
1051 char *author = NULL;
1052 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1053 int author_width, logmsg_width;
1054 char *newline, *line = NULL;
1056 static const size_t date_display_cols = 9;
1057 const int avail = view->ncols;
1059 time_t committer_time;
1061 committer_time = got_object_commit_get_committer_time(commit);
1062 if (localtime_r(&committer_time, &tm) == NULL)
1063 return got_error_from_errno("localtime_r");
1064 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1066 return got_error(GOT_ERR_NO_SPACE);
1068 if (avail < date_display_cols)
1069 limit = MIN(sizeof(datebuf) - 1, avail);
1071 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1072 waddnstr(view->window, datebuf, limit);
1077 author = strdup(got_object_commit_get_author(commit));
1078 if (author == NULL) {
1079 err = got_error_from_errno("strdup");
1082 err = format_author(&wauthor, &author_width, author, avail - col);
1085 waddwstr(view->window, wauthor);
1086 col += author_width;
1087 while (col <= avail && author_width < author_display_cols + 2) {
1088 waddch(view->window, ' ');
1095 err = got_object_commit_get_logmsg(&logmsg0, commit);
1099 while (*logmsg == '\n')
1101 newline = strchr(logmsg, '\n');
1104 limit = avail - col;
1105 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1108 waddwstr(view->window, wlogmsg);
1109 col += logmsg_width;
1110 while (col <= avail) {
1111 waddch(view->window, ' ');
1123 static struct commit_queue_entry *
1124 alloc_commit_queue_entry(struct got_commit_object *commit,
1125 struct got_object_id *id)
1127 struct commit_queue_entry *entry;
1129 entry = calloc(1, sizeof(*entry));
1134 entry->commit = commit;
1139 pop_commit(struct commit_queue *commits)
1141 struct commit_queue_entry *entry;
1143 entry = TAILQ_FIRST(&commits->head);
1144 TAILQ_REMOVE(&commits->head, entry, entry);
1145 got_object_commit_close(entry->commit);
1146 commits->ncommits--;
1147 /* Don't free entry->id! It is owned by the commit graph. */
1152 free_commits(struct commit_queue *commits)
1154 while (!TAILQ_EMPTY(&commits->head))
1155 pop_commit(commits);
1158 static const struct got_error *
1159 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1160 int minqueue, struct got_repository *repo, const char *path)
1162 const struct got_error *err = NULL;
1166 * We keep all commits open throughout the lifetime of the log
1167 * view in order to avoid having to re-fetch commits from disk
1168 * while updating the display.
1170 while (nqueued < minqueue) {
1171 struct got_object_id *id;
1172 struct got_commit_object *commit;
1173 struct commit_queue_entry *entry;
1176 err = got_commit_graph_iter_next(&id, graph);
1178 if (err->code != GOT_ERR_ITER_NEED_MORE)
1180 err = got_commit_graph_fetch_commits(graph,
1190 err = got_object_open_as_commit(&commit, repo, id);
1193 entry = alloc_commit_queue_entry(commit, id);
1194 if (entry == NULL) {
1195 err = got_error_from_errno("alloc_commit_queue_entry");
1199 errcode = pthread_mutex_lock(&tog_mutex);
1201 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1205 entry->idx = commits->ncommits;
1206 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1208 commits->ncommits++;
1210 errcode = pthread_mutex_unlock(&tog_mutex);
1211 if (errcode && err == NULL)
1212 err = got_error_set_errno(errcode,
1213 "pthread_mutex_unlock");
1219 static const struct got_error *
1220 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1221 struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct got_reference *head_ref;
1228 err = got_ref_open(&head_ref, repo, branch_name, 0);
1232 err = got_ref_resolve(head_id, repo, head_ref);
1233 got_ref_close(head_ref);
1242 static const struct got_error *
1243 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1244 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1245 struct commit_queue *commits, int selected_idx, int limit,
1246 struct got_reflist_head *refs, const char *path, int commits_needed)
1248 const struct got_error *err = NULL;
1249 struct tog_log_view_state *s = &view->state.log;
1250 struct commit_queue_entry *entry;
1252 int ncommits, author_cols = 10;
1253 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1254 char *refs_str = NULL;
1260 if (ncommits == selected_idx) {
1264 entry = TAILQ_NEXT(entry, entry);
1268 if (*selected && !(view->searching && view->search_next_done == 0)) {
1269 err = got_object_id_str(&id_str, (*selected)->id);
1273 err = build_refs_str(&refs_str, refs, (*selected)->id,
1280 if (commits_needed == 0)
1281 halfdelay(10); /* disable fast refresh */
1283 if (asprintf(&ncommits_str, " [%d/%d] %s",
1284 entry ? entry->idx + 1 : 0, commits->ncommits,
1285 commits_needed > 0 ?
1286 (view->searching && view->search_next_done == 0
1287 ? "searching..." : "loading... ") :
1288 (refs_str ? refs_str : "")) == -1) {
1289 err = got_error_from_errno("asprintf");
1293 if (path && strcmp(path, "/") != 0) {
1294 if (asprintf(&header, "commit %s %s%s",
1295 id_str ? id_str : "........................................",
1296 path, ncommits_str) == -1) {
1297 err = got_error_from_errno("asprintf");
1301 } else if (asprintf(&header, "commit %s%s",
1302 id_str ? id_str : "........................................",
1303 ncommits_str) == -1) {
1304 err = got_error_from_errno("asprintf");
1308 err = format_line(&wline, &width, header, view->ncols);
1312 werase(view->window);
1314 if (view_needs_focus_indication(view))
1315 wstandout(view->window);
1316 waddwstr(view->window, wline);
1317 while (width < view->ncols) {
1318 waddch(view->window, ' ');
1321 if (view_needs_focus_indication(view))
1322 wstandend(view->window);
1327 /* Grow author column size if necessary. */
1334 if (ncommits >= limit - 1)
1336 author = strdup(got_object_commit_get_author(entry->commit));
1337 if (author == NULL) {
1338 err = got_error_from_errno("strdup");
1341 err = format_author(&wauthor, &width, author, COLS);
1342 if (author_cols < width)
1343 author_cols = width;
1346 entry = TAILQ_NEXT(entry, entry);
1353 if (ncommits >= limit - 1)
1355 if (ncommits == selected_idx)
1356 wstandout(view->window);
1357 err = draw_commit(view, entry->commit, entry->id, refs,
1359 if (ncommits == selected_idx)
1360 wstandend(view->window);
1365 entry = TAILQ_NEXT(entry, entry);
1378 scroll_up(struct tog_view *view,
1379 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1380 struct commit_queue *commits)
1382 struct commit_queue_entry *entry;
1385 entry = TAILQ_FIRST(&commits->head);
1386 if (*first_displayed_entry == entry)
1389 entry = *first_displayed_entry;
1390 while (entry && nscrolled < maxscroll) {
1391 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1393 *first_displayed_entry = entry;
1399 static const struct got_error *
1400 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1401 pthread_cond_t *need_commits)
1406 halfdelay(1); /* fast refresh while loading commits */
1408 while (*commits_needed > 0) {
1412 /* Wake the log thread. */
1413 errcode = pthread_cond_signal(need_commits);
1415 return got_error_set_errno(errcode,
1416 "pthread_cond_signal");
1417 errcode = pthread_mutex_unlock(&tog_mutex);
1419 return got_error_set_errno(errcode,
1420 "pthread_mutex_unlock");
1422 errcode = pthread_mutex_lock(&tog_mutex);
1424 return got_error_set_errno(errcode,
1425 "pthread_mutex_lock");
1427 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1429 * Thread is not done yet; lose a key press
1430 * and let the user retry... this way the GUI
1431 * remains interactive while logging deep paths
1432 * with few commits in history.
1441 static const struct got_error *
1442 scroll_down(struct tog_view *view,
1443 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1444 struct commit_queue_entry **last_displayed_entry,
1445 struct commit_queue *commits, int *log_complete, int *commits_needed,
1446 pthread_cond_t *need_commits)
1448 const struct got_error *err = NULL;
1449 struct commit_queue_entry *pentry;
1452 if (*last_displayed_entry == NULL)
1455 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1456 if (pentry == NULL && !*log_complete) {
1458 * Ask the log thread for required amount of commits
1459 * plus some amount of pre-fetching.
1461 (*commits_needed) += maxscroll + 20;
1462 err = trigger_log_thread(0, commits_needed, log_complete,
1469 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1473 *last_displayed_entry = pentry;
1475 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1478 *first_displayed_entry = pentry;
1479 } while (++nscrolled < maxscroll);
1484 static const struct got_error *
1485 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1486 struct got_commit_object *commit, struct got_object_id *commit_id,
1487 struct tog_view *log_view, struct got_reflist_head *refs,
1488 struct got_repository *repo)
1490 const struct got_error *err;
1491 struct got_object_qid *parent_id;
1492 struct tog_view *diff_view;
1494 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1495 if (diff_view == NULL)
1496 return got_error_from_errno("view_open");
1498 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1499 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1500 commit_id, log_view, refs, repo);
1502 *new_view = diff_view;
1506 static const struct got_error *
1507 tree_view_visit_subtree(struct got_tree_object *subtree,
1508 struct tog_tree_view_state *s)
1510 struct tog_parent_tree *parent;
1512 parent = calloc(1, sizeof(*parent));
1514 return got_error_from_errno("calloc");
1516 parent->tree = s->tree;
1517 parent->first_displayed_entry = s->first_displayed_entry;
1518 parent->selected_entry = s->selected_entry;
1519 parent->selected = s->selected;
1520 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1522 s->entries = got_object_tree_get_entries(s->tree);
1524 s->first_displayed_entry = NULL;
1529 static const struct got_error *
1530 browse_commit_tree(struct tog_view **new_view, int begin_x,
1531 struct commit_queue_entry *entry, const char *path,
1532 struct got_reflist_head *refs, struct got_repository *repo)
1534 const struct got_error *err = NULL;
1535 struct got_tree_object *tree;
1536 struct got_tree_entry *te;
1537 struct tog_tree_view_state *s;
1538 struct tog_view *tree_view;
1539 char *slash, *subpath = NULL;
1542 err = got_object_open_as_tree(&tree, repo,
1543 got_object_commit_get_tree_id(entry->commit));
1547 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1548 if (tree_view == NULL)
1549 return got_error_from_errno("view_open");
1551 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1553 got_object_tree_close(tree);
1556 s = &tree_view->state.tree;
1558 *new_view = tree_view;
1560 /* Walk the path and open corresponding tree objects. */
1565 struct got_object_id *tree_id;
1567 /* Ensure the correct subtree entry is selected. */
1568 slash = strchr(p, '/');
1570 slash = strchr(p, '\0');
1571 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1572 if (strncmp(p, te->name, slash - p) == 0) {
1573 s->selected_entry = te;
1578 if (s->selected_entry == NULL) {
1579 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1582 if (s->tree != s->root)
1583 s->selected++; /* skip '..' */
1585 if (!S_ISDIR(s->selected_entry->mode)) {
1586 /* Jump to this file's entry. */
1587 s->first_displayed_entry = s->selected_entry;
1592 slash = strchr(p, '/');
1594 subpath = strndup(path, slash - path);
1596 subpath = strdup(path);
1597 if (subpath == NULL) {
1598 err = got_error_from_errno("strdup");
1602 err = got_object_id_by_path(&tree_id, repo, entry->id,
1607 err = got_object_open_as_tree(&tree, repo, tree_id);
1612 err = tree_view_visit_subtree(tree, s);
1614 got_object_tree_close(tree);
1629 log_thread(void *arg)
1631 const struct got_error *err = NULL;
1633 struct tog_log_thread_args *a = arg;
1636 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1640 while (!done && !err && !tog_sigpipe_received) {
1641 err = queue_commits(a->graph, a->commits, 1, a->repo,
1644 if (err->code != GOT_ERR_ITER_COMPLETED)
1648 } else if (a->commits_needed > 0)
1649 a->commits_needed--;
1651 errcode = pthread_mutex_lock(&tog_mutex);
1653 err = got_error_set_errno(errcode,
1654 "pthread_mutex_lock");
1656 } else if (*a->quit)
1658 else if (*a->first_displayed_entry == NULL) {
1659 *a->first_displayed_entry =
1660 TAILQ_FIRST(&a->commits->head);
1661 *a->selected_entry = *a->first_displayed_entry;
1665 a->commits_needed = 0;
1666 else if (a->commits_needed == 0) {
1667 errcode = pthread_cond_wait(&a->need_commits,
1670 err = got_error_set_errno(errcode,
1671 "pthread_cond_wait");
1674 errcode = pthread_mutex_unlock(&tog_mutex);
1675 if (errcode && err == NULL)
1676 err = got_error_set_errno(errcode,
1677 "pthread_mutex_unlock");
1679 a->log_complete = 1;
1683 static const struct got_error *
1684 stop_log_thread(struct tog_log_view_state *s)
1686 const struct got_error *err = NULL;
1691 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1693 return got_error_set_errno(errcode,
1694 "pthread_cond_signal");
1695 errcode = pthread_mutex_unlock(&tog_mutex);
1697 return got_error_set_errno(errcode,
1698 "pthread_mutex_unlock");
1699 errcode = pthread_join(s->thread, (void **)&err);
1701 return got_error_set_errno(errcode, "pthread_join");
1702 errcode = pthread_mutex_lock(&tog_mutex);
1704 return got_error_set_errno(errcode,
1705 "pthread_mutex_lock");
1709 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1710 if (errcode && err == NULL)
1711 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1713 if (s->thread_args.repo) {
1714 got_repo_close(s->thread_args.repo);
1715 s->thread_args.repo = NULL;
1718 if (s->thread_args.graph) {
1719 got_commit_graph_close(s->thread_args.graph);
1720 s->thread_args.graph = NULL;
1726 static const struct got_error *
1727 close_log_view(struct tog_view *view)
1729 const struct got_error *err = NULL;
1730 struct tog_log_view_state *s = &view->state.log;
1732 err = stop_log_thread(s);
1733 free_commits(&s->commits);
1734 free(s->in_repo_path);
1735 s->in_repo_path = NULL;
1741 static const struct got_error *
1742 search_start_log_view(struct tog_view *view)
1744 struct tog_log_view_state *s = &view->state.log;
1746 s->matched_entry = NULL;
1747 s->search_entry = NULL;
1752 match_commit(struct got_commit_object *commit, const char *id_str,
1753 const char *logmsg, regex_t *regex)
1755 regmatch_t regmatch;
1757 if (regexec(regex, got_object_commit_get_author(commit), 1,
1758 ®match, 0) == 0 ||
1759 regexec(regex, got_object_commit_get_committer(commit), 1,
1760 ®match, 0) == 0 ||
1761 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1762 regexec(regex, logmsg, 1, ®match, 0) == 0)
1768 static const struct got_error *
1769 search_next_log_view(struct tog_view *view)
1771 const struct got_error *err = NULL;
1772 struct tog_log_view_state *s = &view->state.log;
1773 struct commit_queue_entry *entry;
1775 if (!view->searching) {
1776 view->search_next_done = 1;
1780 if (s->search_entry) {
1781 if (wgetch(view->window) == KEY_BACKSPACE) {
1782 view->search_next_done = 1;
1785 if (view->searching == TOG_SEARCH_FORWARD)
1786 entry = TAILQ_NEXT(s->search_entry, entry);
1788 entry = TAILQ_PREV(s->search_entry,
1789 commit_queue_head, entry);
1790 } else if (s->matched_entry) {
1791 if (view->searching == TOG_SEARCH_FORWARD)
1792 entry = TAILQ_NEXT(s->selected_entry, entry);
1794 entry = TAILQ_PREV(s->selected_entry,
1795 commit_queue_head, entry);
1797 if (view->searching == TOG_SEARCH_FORWARD)
1798 entry = TAILQ_FIRST(&s->commits.head);
1800 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1804 char *id_str, *logmsg;
1805 if (entry == NULL) {
1806 if (s->thread_args.log_complete ||
1807 view->searching == TOG_SEARCH_BACKWARD) {
1808 view->search_next_done = 1;
1812 * Poke the log thread for more commits and return,
1813 * allowing the main loop to make progress. Search
1814 * will resume at s->search_entry once we come back.
1816 s->thread_args.commits_needed++;
1817 return trigger_log_thread(1,
1818 &s->thread_args.commits_needed,
1819 &s->thread_args.log_complete,
1820 &s->thread_args.need_commits);
1823 err = got_object_id_str(&id_str, entry->id);
1827 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1830 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1832 view->search_next_done = 1;
1833 s->matched_entry = entry;
1839 s->search_entry = entry;
1840 if (view->searching == TOG_SEARCH_FORWARD)
1841 entry = TAILQ_NEXT(entry, entry);
1843 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1846 if (s->matched_entry) {
1847 int cur = s->selected_entry->idx;
1848 while (cur < s->matched_entry->idx) {
1849 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1854 while (cur > s->matched_entry->idx) {
1855 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1862 s->search_entry = NULL;
1867 static const struct got_error *
1868 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1869 struct got_reflist_head *refs, struct got_repository *repo,
1870 const char *head_ref_name, const char *path, int check_disk)
1872 const struct got_error *err = NULL;
1873 struct tog_log_view_state *s = &view->state.log;
1874 struct got_repository *thread_repo = NULL;
1875 struct got_commit_graph *thread_graph = NULL;
1878 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1882 /* The commit queue only contains commits being displayed. */
1883 TAILQ_INIT(&s->commits.head);
1884 s->commits.ncommits = 0;
1888 s->head_ref_name = head_ref_name;
1889 s->start_id = got_object_id_dup(start_id);
1890 if (s->start_id == NULL) {
1891 err = got_error_from_errno("got_object_id_dup");
1895 view->show = show_log_view;
1896 view->input = input_log_view;
1897 view->close = close_log_view;
1898 view->search_start = search_start_log_view;
1899 view->search_next = search_next_log_view;
1901 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1904 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1909 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1911 err = got_error_set_errno(errcode, "pthread_cond_init");
1915 s->thread_args.commits_needed = view->nlines;
1916 s->thread_args.graph = thread_graph;
1917 s->thread_args.commits = &s->commits;
1918 s->thread_args.in_repo_path = s->in_repo_path;
1919 s->thread_args.start_id = s->start_id;
1920 s->thread_args.repo = thread_repo;
1921 s->thread_args.log_complete = 0;
1922 s->thread_args.quit = &s->quit;
1923 s->thread_args.view = view;
1924 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1925 s->thread_args.selected_entry = &s->selected_entry;
1928 close_log_view(view);
1932 static const struct got_error *
1933 show_log_view(struct tog_view *view)
1935 struct tog_log_view_state *s = &view->state.log;
1937 if (s->thread == NULL) {
1938 int errcode = pthread_create(&s->thread, NULL, log_thread,
1941 return got_error_set_errno(errcode, "pthread_create");
1944 return draw_commits(view, &s->last_displayed_entry,
1945 &s->selected_entry, s->first_displayed_entry,
1946 &s->commits, s->selected, view->nlines, s->refs,
1947 s->in_repo_path, s->thread_args.commits_needed);
1950 static const struct got_error *
1951 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1952 struct tog_view **focus_view, struct tog_view *view, int ch)
1954 const struct got_error *err = NULL;
1955 struct tog_log_view_state *s = &view->state.log;
1956 char *parent_path, *in_repo_path = NULL;
1957 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1959 struct got_object_id *start_id;
1969 if (s->first_displayed_entry == NULL)
1971 if (s->selected > 0)
1974 scroll_up(view, &s->first_displayed_entry, 1,
1979 if (s->first_displayed_entry == NULL)
1981 if (TAILQ_FIRST(&s->commits.head) ==
1982 s->first_displayed_entry) {
1986 scroll_up(view, &s->first_displayed_entry,
1987 view->nlines, &s->commits);
1993 if (s->first_displayed_entry == NULL)
1995 if (s->selected < MIN(view->nlines - 2,
1996 s->commits.ncommits - 1)) {
2000 err = scroll_down(view, &s->first_displayed_entry, 1,
2001 &s->last_displayed_entry, &s->commits,
2002 &s->thread_args.log_complete,
2003 &s->thread_args.commits_needed,
2004 &s->thread_args.need_commits);
2008 struct commit_queue_entry *first;
2009 first = s->first_displayed_entry;
2012 err = scroll_down(view, &s->first_displayed_entry,
2013 view->nlines, &s->last_displayed_entry,
2014 &s->commits, &s->thread_args.log_complete,
2015 &s->thread_args.commits_needed,
2016 &s->thread_args.need_commits);
2017 if (first == s->first_displayed_entry &&
2018 s->selected < MIN(view->nlines - 2,
2019 s->commits.ncommits - 1)) {
2020 /* can't scroll further down */
2021 s->selected = MIN(view->nlines - 2,
2022 s->commits.ncommits - 1);
2028 if (s->selected > view->nlines - 2)
2029 s->selected = view->nlines - 2;
2030 if (s->selected > s->commits.ncommits - 1)
2031 s->selected = s->commits.ncommits - 1;
2036 if (s->selected_entry == NULL)
2038 if (view_is_parent_view(view))
2039 begin_x = view_split_begin_x(view->begin_x);
2040 err = open_diff_view_for_commit(&diff_view, begin_x,
2041 s->selected_entry->commit, s->selected_entry->id,
2042 view, s->refs, s->repo);
2045 if (view_is_parent_view(view)) {
2046 err = view_close_child(view);
2049 err = view_set_child(view, diff_view);
2051 view_close(diff_view);
2054 *focus_view = diff_view;
2055 view->child_focussed = 1;
2057 *new_view = diff_view;
2060 if (s->selected_entry == NULL)
2062 if (view_is_parent_view(view))
2063 begin_x = view_split_begin_x(view->begin_x);
2064 err = browse_commit_tree(&tree_view, begin_x,
2065 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2068 if (view_is_parent_view(view)) {
2069 err = view_close_child(view);
2072 err = view_set_child(view, tree_view);
2074 view_close(tree_view);
2077 *focus_view = tree_view;
2078 view->child_focussed = 1;
2080 *new_view = tree_view;
2083 if (strcmp(s->in_repo_path, "/") == 0)
2085 parent_path = dirname(s->in_repo_path);
2086 if (parent_path && strcmp(parent_path, ".") != 0) {
2087 err = stop_log_thread(s);
2090 lv = view_open(view->nlines, view->ncols,
2091 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2093 return got_error_from_errno(
2095 err = open_log_view(lv, s->start_id, s->refs,
2096 s->repo, s->head_ref_name, parent_path, 0);
2099 if (view_is_parent_view(view))
2102 view_set_child(view->parent, lv);
2109 err = stop_log_thread(s);
2112 lv = view_open(view->nlines, view->ncols,
2113 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2115 return got_error_from_errno("view_open");
2116 err = get_head_commit_id(&start_id, s->head_ref_name ?
2117 s->head_ref_name : GOT_REF_HEAD, s->repo);
2122 in_repo_path = strdup(s->in_repo_path);
2123 if (in_repo_path == NULL) {
2126 return got_error_from_errno("strdup");
2128 err = open_log_view(lv, start_id, s->refs, s->repo,
2129 s->head_ref_name, in_repo_path, 0);
2145 static const struct got_error *
2146 apply_unveil(const char *repo_path, const char *worktree_path)
2148 const struct got_error *error;
2151 if (unveil("gmon.out", "rwc") != 0)
2152 return got_error_from_errno2("unveil", "gmon.out");
2154 if (repo_path && unveil(repo_path, "r") != 0)
2155 return got_error_from_errno2("unveil", repo_path);
2157 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2158 return got_error_from_errno2("unveil", worktree_path);
2160 if (unveil("/tmp", "rwc") != 0)
2161 return got_error_from_errno2("unveil", "/tmp");
2163 error = got_privsep_unveil_exec_helpers();
2167 if (unveil(NULL, NULL) != 0)
2168 return got_error_from_errno("unveil");
2178 halfdelay(1); /* Do fast refresh while initial view is loading. */
2181 intrflush(stdscr, FALSE);
2182 keypad(stdscr, TRUE);
2184 signal(SIGWINCH, tog_sigwinch);
2185 signal(SIGPIPE, tog_sigpipe);
2188 static const struct got_error *
2189 cmd_log(int argc, char *argv[])
2191 const struct got_error *error;
2192 struct got_repository *repo = NULL;
2193 struct got_worktree *worktree = NULL;
2194 struct got_reflist_head refs;
2195 struct got_object_id *start_id = NULL;
2196 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2197 char *start_commit = NULL;
2199 struct tog_view *view;
2201 SIMPLEQ_INIT(&refs);
2204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2212 start_commit = optarg;
2215 repo_path = realpath(optarg, NULL);
2216 if (repo_path == NULL)
2217 err(1, "-r option");
2228 cwd = getcwd(NULL, 0);
2230 error = got_error_from_errno("getcwd");
2233 error = got_worktree_open(&worktree, cwd);
2234 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2241 error = got_error_from_errno("strdup");
2244 } else if (argc == 1) {
2246 error = got_worktree_resolve_path(&path, worktree,
2251 path = strdup(argv[0]);
2253 error = got_error_from_errno("strdup");
2260 if (repo_path == NULL) {
2263 got_worktree_get_repo_path(worktree));
2265 repo_path = strdup(cwd);
2267 if (repo_path == NULL) {
2268 error = got_error_from_errno("strdup");
2274 error = got_repo_open(&repo, repo_path);
2278 error = apply_unveil(got_repo_get_path(repo),
2279 worktree ? got_worktree_get_root_path(worktree) : NULL);
2283 if (start_commit == NULL)
2284 error = get_head_commit_id(&start_id, worktree ?
2285 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2288 error = get_head_commit_id(&start_id, start_commit, repo);
2290 if (error->code != GOT_ERR_NOT_REF)
2292 error = got_repo_match_object_id_prefix(&start_id,
2293 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2299 error = got_ref_list(&refs, repo);
2303 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2305 error = got_error_from_errno("view_open");
2308 error = open_log_view(view, start_id, &refs, repo, worktree ?
2309 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2312 error = view_loop(view);
2319 got_repo_close(repo);
2321 got_worktree_close(worktree);
2322 got_ref_list_free(&refs);
2330 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2336 parse_next_line(FILE *f, size_t *len)
2341 const char delim[3] = { '\0', '\0', '\0'};
2343 line = fparseln(f, &linelen, &lineno, delim, 0);
2349 static const struct got_error *
2350 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2351 int *last_displayed_line, int *eof, int max_lines,
2354 const struct got_error *err;
2355 int nlines = 0, nprinted = 0;
2362 werase(view->window);
2365 err = format_line(&wline, &width, header, view->ncols);
2370 if (view_needs_focus_indication(view))
2371 wstandout(view->window);
2372 waddwstr(view->window, wline);
2373 if (view_needs_focus_indication(view))
2374 wstandend(view->window);
2375 if (width < view->ncols - 1)
2376 waddch(view->window, '\n');
2384 while (nprinted < max_lines) {
2385 line = parse_next_line(f, &len);
2390 if (++nlines < *first_displayed_line) {
2395 err = format_line(&wline, &width, line, view->ncols);
2400 waddwstr(view->window, wline);
2401 if (width < view->ncols - 1)
2402 waddch(view->window, '\n');
2403 if (++nprinted == 1)
2404 *first_displayed_line = nlines;
2409 *last_displayed_line = nlines;
2414 while (nprinted < view->nlines) {
2415 waddch(view->window, '\n');
2419 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2424 wstandout(view->window);
2425 waddwstr(view->window, wline);
2426 wstandend(view->window);
2433 get_datestr(time_t *time, char *datebuf)
2435 struct tm mytm, *tm;
2438 tm = gmtime_r(time, &mytm);
2441 s = asctime_r(tm, datebuf);
2444 p = strchr(s, '\n');
2450 static const struct got_error *
2451 write_commit_info(struct got_object_id *commit_id,
2452 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2454 const struct got_error *err = NULL;
2455 char datebuf[26], *datestr;
2456 struct got_commit_object *commit;
2457 char *id_str = NULL, *logmsg = NULL;
2458 time_t committer_time;
2459 const char *author, *committer;
2460 char *refs_str = NULL;
2463 err = build_refs_str(&refs_str, refs, commit_id, repo);
2468 err = got_object_open_as_commit(&commit, repo, commit_id);
2472 err = got_object_id_str(&id_str, commit_id);
2474 err = got_error_from_errno("got_object_id_str");
2478 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2479 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2480 err = got_error_from_errno("fprintf");
2483 if (fprintf(outfile, "from: %s\n",
2484 got_object_commit_get_author(commit)) < 0) {
2485 err = got_error_from_errno("fprintf");
2488 committer_time = got_object_commit_get_committer_time(commit);
2489 datestr = get_datestr(&committer_time, datebuf);
2490 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2491 err = got_error_from_errno("fprintf");
2494 author = got_object_commit_get_author(commit);
2495 committer = got_object_commit_get_committer(commit);
2496 if (strcmp(author, committer) != 0 &&
2497 fprintf(outfile, "via: %s\n", committer) < 0) {
2498 err = got_error_from_errno("fprintf");
2501 err = got_object_commit_get_logmsg(&logmsg, commit);
2504 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2505 err = got_error_from_errno("fprintf");
2512 got_object_commit_close(commit);
2516 static const struct got_error *
2517 create_diff(struct tog_diff_view_state *s)
2519 const struct got_error *err = NULL;
2525 err = got_error_from_errno("got_opentemp");
2528 if (s->f && fclose(s->f) != 0) {
2529 err = got_error_from_errno("fclose");
2535 err = got_object_get_type(&obj_type, s->repo, s->id1);
2537 err = got_object_get_type(&obj_type, s->repo, s->id2);
2542 case GOT_OBJ_TYPE_BLOB:
2543 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2544 s->diff_context, s->repo, f);
2546 case GOT_OBJ_TYPE_TREE:
2547 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2548 s->diff_context, s->repo, f);
2550 case GOT_OBJ_TYPE_COMMIT: {
2551 const struct got_object_id_queue *parent_ids;
2552 struct got_object_qid *pid;
2553 struct got_commit_object *commit2;
2555 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2558 /* Show commit info if we're diffing to a parent/root commit. */
2560 write_commit_info(s->id2, s->refs, s->repo, f);
2562 parent_ids = got_object_commit_get_parent_ids(commit2);
2563 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2564 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2565 write_commit_info(s->id2, s->refs,
2571 got_object_commit_close(commit2);
2573 err = got_diff_objects_as_commits(s->id1, s->id2,
2574 s->diff_context, s->repo, f);
2578 err = got_error(GOT_ERR_OBJ_TYPE);
2582 if (f && fflush(f) != 0 && err == NULL)
2583 err = got_error_from_errno("fflush");
2588 diff_view_indicate_progress(struct tog_view *view)
2590 mvwaddstr(view->window, 0, 0, "diffing...");
2595 static const struct got_error *
2596 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2597 struct got_object_id *id2, struct tog_view *log_view,
2598 struct got_reflist_head *refs, struct got_repository *repo)
2600 const struct got_error *err;
2602 if (id1 != NULL && id2 != NULL) {
2604 err = got_object_get_type(&type1, repo, id1);
2607 err = got_object_get_type(&type2, repo, id2);
2612 return got_error(GOT_ERR_OBJ_TYPE);
2616 view->state.diff.id1 = got_object_id_dup(id1);
2617 if (view->state.diff.id1 == NULL)
2618 return got_error_from_errno("got_object_id_dup");
2620 view->state.diff.id1 = NULL;
2622 view->state.diff.id2 = got_object_id_dup(id2);
2623 if (view->state.diff.id2 == NULL) {
2624 free(view->state.diff.id1);
2625 view->state.diff.id1 = NULL;
2626 return got_error_from_errno("got_object_id_dup");
2628 view->state.diff.f = NULL;
2629 view->state.diff.first_displayed_line = 1;
2630 view->state.diff.last_displayed_line = view->nlines;
2631 view->state.diff.diff_context = 3;
2632 view->state.diff.log_view = log_view;
2633 view->state.diff.repo = repo;
2634 view->state.diff.refs = refs;
2636 if (log_view && view_is_splitscreen(view))
2637 show_log_view(log_view); /* draw vborder */
2638 diff_view_indicate_progress(view);
2640 err = create_diff(&view->state.diff);
2642 free(view->state.diff.id1);
2643 view->state.diff.id1 = NULL;
2644 free(view->state.diff.id2);
2645 view->state.diff.id2 = NULL;
2649 view->show = show_diff_view;
2650 view->input = input_diff_view;
2651 view->close = close_diff_view;
2656 static const struct got_error *
2657 close_diff_view(struct tog_view *view)
2659 const struct got_error *err = NULL;
2661 free(view->state.diff.id1);
2662 view->state.diff.id1 = NULL;
2663 free(view->state.diff.id2);
2664 view->state.diff.id2 = NULL;
2665 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2666 err = got_error_from_errno("fclose");
2670 static const struct got_error *
2671 show_diff_view(struct tog_view *view)
2673 const struct got_error *err;
2674 struct tog_diff_view_state *s = &view->state.diff;
2675 char *id_str1 = NULL, *id_str2, *header;
2678 err = got_object_id_str(&id_str1, s->id1);
2682 err = got_object_id_str(&id_str2, s->id2);
2686 if (asprintf(&header, "diff %s %s",
2687 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2688 err = got_error_from_errno("asprintf");
2696 return draw_file(view, s->f, &s->first_displayed_line,
2697 &s->last_displayed_line, &s->eof, view->nlines,
2701 static const struct got_error *
2702 set_selected_commit(struct tog_diff_view_state *s,
2703 struct commit_queue_entry *entry)
2705 const struct got_error *err;
2706 const struct got_object_id_queue *parent_ids;
2707 struct got_commit_object *selected_commit;
2708 struct got_object_qid *pid;
2711 s->id2 = got_object_id_dup(entry->id);
2713 return got_error_from_errno("got_object_id_dup");
2715 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2718 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2720 pid = SIMPLEQ_FIRST(parent_ids);
2721 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2722 got_object_commit_close(selected_commit);
2726 static const struct got_error *
2727 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2728 struct tog_view **focus_view, struct tog_view *view, int ch)
2730 const struct got_error *err = NULL;
2731 struct tog_diff_view_state *s = &view->state.diff;
2732 struct tog_log_view_state *ls;
2733 struct commit_queue_entry *entry;
2739 if (s->first_displayed_line > 1)
2740 s->first_displayed_line--;
2744 if (s->first_displayed_line == 1)
2747 while (i++ < view->nlines - 1 &&
2748 s->first_displayed_line > 1)
2749 s->first_displayed_line--;
2754 s->first_displayed_line++;
2762 while (!s->eof && i++ < view->nlines - 1) {
2764 line = parse_next_line(s->f, NULL);
2765 s->first_displayed_line++;
2771 if (s->diff_context > 0) {
2773 diff_view_indicate_progress(view);
2774 err = create_diff(s);
2778 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2780 diff_view_indicate_progress(view);
2781 err = create_diff(s);
2786 if (s->log_view == NULL)
2788 ls = &s->log_view->state.log;
2789 entry = TAILQ_PREV(ls->selected_entry,
2790 commit_queue_head, entry);
2794 err = input_log_view(NULL, NULL, NULL, s->log_view,
2799 err = set_selected_commit(s, entry);
2803 s->first_displayed_line = 1;
2804 s->last_displayed_line = view->nlines;
2806 diff_view_indicate_progress(view);
2807 err = create_diff(s);
2811 if (s->log_view == NULL)
2813 ls = &s->log_view->state.log;
2815 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2816 ls->thread_args.commits_needed++;
2818 /* Display "loading..." in log view. */
2819 show_log_view(s->log_view);
2823 err = trigger_log_thread(1 /* load_all */,
2824 &ls->thread_args.commits_needed,
2825 &ls->thread_args.log_complete,
2826 &ls->thread_args.need_commits);
2830 err = input_log_view(NULL, NULL, NULL, s->log_view,
2835 entry = TAILQ_NEXT(ls->selected_entry, entry);
2839 err = set_selected_commit(s, entry);
2843 s->first_displayed_line = 1;
2844 s->last_displayed_line = view->nlines;
2846 diff_view_indicate_progress(view);
2847 err = create_diff(s);
2856 static const struct got_error *
2857 cmd_diff(int argc, char *argv[])
2859 const struct got_error *error = NULL;
2860 struct got_repository *repo = NULL;
2861 struct got_reflist_head refs;
2862 struct got_object_id *id1 = NULL, *id2 = NULL;
2863 char *repo_path = NULL;
2864 char *id_str1 = NULL, *id_str2 = NULL;
2866 struct tog_view *view;
2868 SIMPLEQ_INIT(&refs);
2871 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2876 while ((ch = getopt(argc, argv, "")) != -1) {
2888 usage_diff(); /* TODO show local worktree changes */
2889 } else if (argc == 2) {
2890 repo_path = getcwd(NULL, 0);
2891 if (repo_path == NULL)
2892 return got_error_from_errno("getcwd");
2895 } else if (argc == 3) {
2896 repo_path = realpath(argv[0], NULL);
2897 if (repo_path == NULL)
2898 return got_error_from_errno2("realpath", argv[0]);
2906 error = got_repo_open(&repo, repo_path);
2910 error = apply_unveil(got_repo_get_path(repo), NULL);
2914 error = got_repo_match_object_id_prefix(&id1, id_str1,
2915 GOT_OBJ_TYPE_ANY, repo);
2919 error = got_repo_match_object_id_prefix(&id2, id_str2,
2920 GOT_OBJ_TYPE_ANY, repo);
2924 error = got_ref_list(&refs, repo);
2928 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2930 error = got_error_from_errno("view_open");
2933 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2936 error = view_loop(view);
2940 got_repo_close(repo);
2941 got_ref_list_free(&refs);
2949 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2954 struct tog_blame_line {
2956 struct got_object_id *id;
2959 static const struct got_error *
2960 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2961 const char *path, struct tog_blame_line *lines, int nlines,
2962 int blame_complete, int selected_line, int *first_displayed_line,
2963 int *last_displayed_line, int *eof, int max_lines)
2965 const struct got_error *err;
2966 int lineno = 0, nprinted = 0;
2971 struct tog_blame_line *blame_line;
2972 struct got_object_id *prev_id = NULL;
2975 err = got_object_id_str(&id_str, id);
2980 werase(view->window);
2982 if (asprintf(&line, "commit %s", id_str) == -1) {
2983 err = got_error_from_errno("asprintf");
2988 err = format_line(&wline, &width, line, view->ncols);
2991 if (view_needs_focus_indication(view))
2992 wstandout(view->window);
2993 waddwstr(view->window, wline);
2994 if (view_needs_focus_indication(view))
2995 wstandend(view->window);
2998 if (width < view->ncols - 1)
2999 waddch(view->window, '\n');
3001 if (asprintf(&line, "[%d/%d] %s%s",
3002 *first_displayed_line - 1 + selected_line, nlines,
3003 blame_complete ? "" : "annotating... ", path) == -1) {
3005 return got_error_from_errno("asprintf");
3008 err = format_line(&wline, &width, line, view->ncols);
3013 waddwstr(view->window, wline);
3016 if (width < view->ncols - 1)
3017 waddch(view->window, '\n');
3020 while (nprinted < max_lines - 2) {
3021 line = parse_next_line(f, &len);
3026 if (++lineno < *first_displayed_line) {
3031 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3032 err = format_line(&wline, &width, line, wlimit);
3038 if (view->focussed && nprinted == selected_line - 1)
3039 wstandout(view->window);
3042 blame_line = &lines[lineno - 1];
3043 if (blame_line->annotated && prev_id &&
3044 got_object_id_cmp(prev_id, blame_line->id) == 0)
3045 waddstr(view->window, " ");
3046 else if (blame_line->annotated) {
3048 err = got_object_id_str(&id_str, blame_line->id);
3054 wprintw(view->window, "%.8s ", id_str);
3056 prev_id = blame_line->id;
3058 waddstr(view->window, "........ ");
3062 waddstr(view->window, "........ ");
3066 waddwstr(view->window, wline);
3067 while (width < wlimit) {
3068 waddch(view->window, ' ');
3071 if (view->focussed && nprinted == selected_line - 1)
3072 wstandend(view->window);
3073 if (++nprinted == 1)
3074 *first_displayed_line = lineno;
3079 *last_displayed_line = lineno;
3086 static const struct got_error *
3087 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3089 const struct got_error *err = NULL;
3090 struct tog_blame_cb_args *a = arg;
3091 struct tog_blame_line *line;
3094 if (nlines != a->nlines ||
3095 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3096 return got_error(GOT_ERR_RANGE);
3098 errcode = pthread_mutex_lock(&tog_mutex);
3100 return got_error_set_errno(errcode, "pthread_mutex_lock");
3102 if (*a->quit) { /* user has quit the blame view */
3103 err = got_error(GOT_ERR_ITER_COMPLETED);
3108 goto done; /* no change in this commit */
3110 line = &a->lines[lineno - 1];
3111 if (line->annotated)
3114 line->id = got_object_id_dup(id);
3115 if (line->id == NULL) {
3116 err = got_error_from_errno("got_object_id_dup");
3119 line->annotated = 1;
3121 errcode = pthread_mutex_unlock(&tog_mutex);
3123 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3128 blame_thread(void *arg)
3130 const struct got_error *err;
3131 struct tog_blame_thread_args *ta = arg;
3132 struct tog_blame_cb_args *a = ta->cb_args;
3135 err = got_blame(ta->path, a->commit_id, ta->repo,
3136 blame_cb, ta->cb_args);
3138 errcode = pthread_mutex_lock(&tog_mutex);
3140 return (void *)got_error_set_errno(errcode,
3141 "pthread_mutex_lock");
3143 got_repo_close(ta->repo);
3147 errcode = pthread_mutex_unlock(&tog_mutex);
3148 if (errcode && err == NULL)
3149 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3154 static struct got_object_id *
3155 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3156 int first_displayed_line, int selected_line)
3158 struct tog_blame_line *line;
3163 line = &lines[first_displayed_line - 1 + selected_line - 1];
3164 if (!line->annotated)
3170 static const struct got_error *
3171 stop_blame(struct tog_blame *blame)
3173 const struct got_error *err = NULL;
3176 if (blame->thread) {
3178 errcode = pthread_mutex_unlock(&tog_mutex);
3180 return got_error_set_errno(errcode,
3181 "pthread_mutex_unlock");
3182 errcode = pthread_join(blame->thread, (void **)&err);
3184 return got_error_set_errno(errcode, "pthread_join");
3185 errcode = pthread_mutex_lock(&tog_mutex);
3187 return got_error_set_errno(errcode,
3188 "pthread_mutex_lock");
3189 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3191 blame->thread = NULL;
3193 if (blame->thread_args.repo) {
3194 got_repo_close(blame->thread_args.repo);
3195 blame->thread_args.repo = NULL;
3198 if (fclose(blame->f) != 0 && err == NULL)
3199 err = got_error_from_errno("fclose");
3203 for (i = 0; i < blame->nlines; i++)
3204 free(blame->lines[i].id);
3206 blame->lines = NULL;
3208 free(blame->cb_args.commit_id);
3209 blame->cb_args.commit_id = NULL;
3214 static const struct got_error *
3215 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3216 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3217 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3218 struct got_repository *repo)
3220 const struct got_error *err = NULL;
3221 struct got_blob_object *blob = NULL;
3222 struct got_repository *thread_repo = NULL;
3223 struct got_object_id *obj_id = NULL;
3226 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3230 return got_error(GOT_ERR_NO_OBJ);
3232 err = got_object_get_type(&obj_type, repo, obj_id);
3236 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3237 err = got_error(GOT_ERR_OBJ_TYPE);
3241 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3244 blame->f = got_opentemp();
3245 if (blame->f == NULL) {
3246 err = got_error_from_errno("got_opentemp");
3249 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3250 &blame->line_offsets, blame->f, blob);
3254 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3255 if (blame->lines == NULL) {
3256 err = got_error_from_errno("calloc");
3260 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3264 blame->cb_args.view = view;
3265 blame->cb_args.lines = blame->lines;
3266 blame->cb_args.nlines = blame->nlines;
3267 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3268 if (blame->cb_args.commit_id == NULL) {
3269 err = got_error_from_errno("got_object_id_dup");
3272 blame->cb_args.quit = done;
3274 blame->thread_args.path = path;
3275 blame->thread_args.repo = thread_repo;
3276 blame->thread_args.cb_args = &blame->cb_args;
3277 blame->thread_args.complete = blame_complete;
3278 *blame_complete = 0;
3282 got_object_blob_close(blob);
3289 static const struct got_error *
3290 open_blame_view(struct tog_view *view, char *path,
3291 struct got_object_id *commit_id, struct got_reflist_head *refs,
3292 struct got_repository *repo)
3294 const struct got_error *err = NULL;
3295 struct tog_blame_view_state *s = &view->state.blame;
3297 SIMPLEQ_INIT(&s->blamed_commits);
3299 s->path = strdup(path);
3300 if (s->path == NULL)
3301 return got_error_from_errno("strdup");
3303 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3309 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3310 s->first_displayed_line = 1;
3311 s->last_displayed_line = view->nlines;
3312 s->selected_line = 1;
3313 s->blame_complete = 0;
3316 s->commit_id = commit_id;
3317 memset(&s->blame, 0, sizeof(s->blame));
3319 view->show = show_blame_view;
3320 view->input = input_blame_view;
3321 view->close = close_blame_view;
3322 view->search_start = search_start_blame_view;
3323 view->search_next = search_next_blame_view;
3325 return run_blame(&s->blame, view, &s->blame_complete,
3326 &s->first_displayed_line, &s->last_displayed_line,
3327 &s->selected_line, &s->done, &s->eof, s->path,
3328 s->blamed_commit->id, s->repo);
3331 static const struct got_error *
3332 close_blame_view(struct tog_view *view)
3334 const struct got_error *err = NULL;
3335 struct tog_blame_view_state *s = &view->state.blame;
3337 if (s->blame.thread)
3338 err = stop_blame(&s->blame);
3340 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3341 struct got_object_qid *blamed_commit;
3342 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3343 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3344 got_object_qid_free(blamed_commit);
3352 static const struct got_error *
3353 search_start_blame_view(struct tog_view *view)
3355 struct tog_blame_view_state *s = &view->state.blame;
3357 s->matched_line = 0;
3362 match_line(const char *line, regex_t *regex)
3364 regmatch_t regmatch;
3366 return regexec(regex, line, 1, ®match, 0) == 0;
3370 static const struct got_error *
3371 search_next_blame_view(struct tog_view *view)
3373 struct tog_blame_view_state *s = &view->state.blame;
3376 if (!view->searching) {
3377 view->search_next_done = 1;
3381 if (s->matched_line) {
3382 if (view->searching == TOG_SEARCH_FORWARD)
3383 lineno = s->matched_line + 1;
3385 lineno = s->matched_line - 1;
3387 if (view->searching == TOG_SEARCH_FORWARD)
3390 lineno = s->blame.nlines;
3398 if (lineno <= 0 || lineno > s->blame.nlines) {
3399 if (s->matched_line == 0) {
3400 view->search_next_done = 1;
3405 if (view->searching == TOG_SEARCH_FORWARD)
3408 lineno = s->blame.nlines;
3411 offset = s->blame.line_offsets[lineno - 1];
3412 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3414 return got_error_from_errno("fseeko");
3417 line = parse_next_line(s->blame.f, &len);
3418 if (line && match_line(line, &view->regex)) {
3419 view->search_next_done = 1;
3420 s->matched_line = lineno;
3425 if (view->searching == TOG_SEARCH_FORWARD)
3431 if (s->matched_line) {
3432 s->first_displayed_line = s->matched_line;
3433 s->selected_line = 1;
3439 static const struct got_error *
3440 show_blame_view(struct tog_view *view)
3442 const struct got_error *err = NULL;
3443 struct tog_blame_view_state *s = &view->state.blame;
3446 if (s->blame.thread == NULL) {
3447 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3448 &s->blame.thread_args);
3450 return got_error_set_errno(errcode, "pthread_create");
3453 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3454 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3455 s->selected_line, &s->first_displayed_line,
3456 &s->last_displayed_line, &s->eof, view->nlines);
3462 static const struct got_error *
3463 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3464 struct tog_view **focus_view, struct tog_view *view, int ch)
3466 const struct got_error *err = NULL, *thread_err = NULL;
3467 struct tog_view *diff_view;
3468 struct tog_blame_view_state *s = &view->state.blame;
3477 if (s->selected_line > 1)
3479 else if (s->selected_line == 1 &&
3480 s->first_displayed_line > 1)
3481 s->first_displayed_line--;
3484 if (s->first_displayed_line == 1) {
3485 s->selected_line = 1;
3488 if (s->first_displayed_line > view->nlines - 2)
3489 s->first_displayed_line -=
3492 s->first_displayed_line = 1;
3496 if (s->selected_line < view->nlines - 2 &&
3497 s->first_displayed_line +
3498 s->selected_line <= s->blame.nlines)
3500 else if (s->last_displayed_line <
3502 s->first_displayed_line++;
3506 struct got_object_id *id = NULL;
3507 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3508 s->first_displayed_line, s->selected_line);
3512 struct got_commit_object *commit;
3513 struct got_object_qid *pid;
3514 struct got_object_id *blob_id = NULL;
3516 err = got_object_open_as_commit(&commit,
3520 pid = SIMPLEQ_FIRST(
3521 got_object_commit_get_parent_ids(commit));
3523 got_object_commit_close(commit);
3526 /* Check if path history ends here. */
3527 err = got_object_id_by_path(&blob_id, s->repo,
3530 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3532 got_object_commit_close(commit);
3535 err = got_object_get_type(&obj_type, s->repo,
3538 /* Can't blame non-blob type objects. */
3539 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3540 got_object_commit_close(commit);
3543 err = got_object_qid_alloc(&s->blamed_commit,
3545 got_object_commit_close(commit);
3547 if (got_object_id_cmp(id,
3548 s->blamed_commit->id) == 0)
3550 err = got_object_qid_alloc(&s->blamed_commit,
3556 thread_err = stop_blame(&s->blame);
3560 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3561 s->blamed_commit, entry);
3562 err = run_blame(&s->blame, view, &s->blame_complete,
3563 &s->first_displayed_line, &s->last_displayed_line,
3564 &s->selected_line, &s->done, &s->eof,
3565 s->path, s->blamed_commit->id, s->repo);
3571 struct got_object_qid *first;
3572 first = SIMPLEQ_FIRST(&s->blamed_commits);
3573 if (!got_object_id_cmp(first->id, s->commit_id))
3576 thread_err = stop_blame(&s->blame);
3580 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3581 got_object_qid_free(s->blamed_commit);
3583 SIMPLEQ_FIRST(&s->blamed_commits);
3584 err = run_blame(&s->blame, view, &s->blame_complete,
3585 &s->first_displayed_line, &s->last_displayed_line,
3586 &s->selected_line, &s->done, &s->eof, s->path,
3587 s->blamed_commit->id, s->repo);
3594 struct got_object_id *id = NULL;
3595 struct got_object_qid *pid;
3596 struct got_commit_object *commit = NULL;
3597 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3598 s->first_displayed_line, s->selected_line);
3601 err = got_object_open_as_commit(&commit, s->repo, id);
3604 pid = SIMPLEQ_FIRST(
3605 got_object_commit_get_parent_ids(commit));
3606 if (view_is_parent_view(view))
3607 begin_x = view_split_begin_x(view->begin_x);
3608 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3609 if (diff_view == NULL) {
3610 got_object_commit_close(commit);
3611 err = got_error_from_errno("view_open");
3614 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3615 id, NULL, s->refs, s->repo);
3616 got_object_commit_close(commit);
3618 view_close(diff_view);
3621 if (view_is_parent_view(view)) {
3622 err = view_close_child(view);
3625 err = view_set_child(view, diff_view);
3627 view_close(diff_view);
3630 *focus_view = diff_view;
3631 view->child_focussed = 1;
3633 *new_view = diff_view;
3640 if (s->last_displayed_line >= s->blame.nlines &&
3641 s->selected_line >= MIN(s->blame.nlines,
3642 view->nlines - 2)) {
3645 if (s->last_displayed_line >= s->blame.nlines &&
3646 s->selected_line < view->nlines - 2) {
3647 s->selected_line = MIN(s->blame.nlines,
3651 if (s->last_displayed_line + view->nlines - 2
3653 s->first_displayed_line +=
3656 s->first_displayed_line =
3661 if (s->selected_line > view->nlines - 2) {
3662 s->selected_line = MIN(s->blame.nlines,
3669 return thread_err ? thread_err : err;
3672 static const struct got_error *
3673 cmd_blame(int argc, char *argv[])
3675 const struct got_error *error;
3676 struct got_repository *repo = NULL;
3677 struct got_reflist_head refs;
3678 struct got_worktree *worktree = NULL;
3679 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3680 struct got_object_id *commit_id = NULL;
3681 char *commit_id_str = NULL;
3683 struct tog_view *view;
3685 SIMPLEQ_INIT(&refs);
3688 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3693 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3696 commit_id_str = optarg;
3699 repo_path = realpath(optarg, NULL);
3700 if (repo_path == NULL)
3701 err(1, "-r option");
3717 cwd = getcwd(NULL, 0);
3719 error = got_error_from_errno("getcwd");
3722 if (repo_path == NULL) {
3723 error = got_worktree_open(&worktree, cwd);
3724 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3730 strdup(got_worktree_get_repo_path(worktree));
3731 if (repo_path == NULL)
3732 error = got_error_from_errno("strdup");
3736 repo_path = strdup(cwd);
3737 if (repo_path == NULL) {
3738 error = got_error_from_errno("strdup");
3746 error = got_repo_open(&repo, repo_path);
3750 error = apply_unveil(got_repo_get_path(repo), NULL);
3755 const char *prefix = got_worktree_get_path_prefix(worktree);
3756 char *p, *worktree_subdir = cwd +
3757 strlen(got_worktree_get_root_path(worktree));
3758 if (asprintf(&p, "%s%s%s%s%s",
3759 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3760 worktree_subdir, worktree_subdir[0] ? "/" : "",
3762 error = got_error_from_errno("asprintf");
3765 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3768 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3773 if (commit_id_str == NULL) {
3774 struct got_reference *head_ref;
3775 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3778 error = got_ref_resolve(&commit_id, repo, head_ref);
3779 got_ref_close(head_ref);
3781 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3783 if (error->code != GOT_ERR_NOT_REF)
3785 error = got_repo_match_object_id_prefix(&commit_id,
3786 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3792 error = got_ref_list(&refs, repo);
3796 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3798 error = got_error_from_errno("view_open");
3801 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3804 error = view_loop(view);
3810 got_worktree_close(worktree);
3812 got_repo_close(repo);
3813 got_ref_list_free(&refs);
3817 static const struct got_error *
3818 draw_tree_entries(struct tog_view *view,
3819 struct got_tree_entry **first_displayed_entry,
3820 struct got_tree_entry **last_displayed_entry,
3821 struct got_tree_entry **selected_entry, int *ndisplayed,
3822 const char *label, int show_ids, const char *parent_path,
3823 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3825 const struct got_error *err = NULL;
3826 struct got_tree_entry *te;
3832 werase(view->window);
3837 err = format_line(&wline, &width, label, view->ncols);
3840 if (view_needs_focus_indication(view))
3841 wstandout(view->window);
3842 waddwstr(view->window, wline);
3843 if (view_needs_focus_indication(view))
3844 wstandend(view->window);
3847 if (width < view->ncols - 1)
3848 waddch(view->window, '\n');
3851 err = format_line(&wline, &width, parent_path, view->ncols);
3854 waddwstr(view->window, wline);
3857 if (width < view->ncols - 1)
3858 waddch(view->window, '\n');
3861 waddch(view->window, '\n');
3865 te = SIMPLEQ_FIRST(&entries->head);
3866 if (*first_displayed_entry == NULL) {
3867 if (selected == 0) {
3869 wstandout(view->window);
3870 *selected_entry = NULL;
3872 waddstr(view->window, " ..\n"); /* parent directory */
3873 if (selected == 0 && view->focussed)
3874 wstandend(view->window);
3881 while (te != *first_displayed_entry)
3882 te = SIMPLEQ_NEXT(te, entry);
3886 char *line = NULL, *id_str = NULL;
3887 const char *modestr = "";
3890 err = got_object_id_str(&id_str, te->id);
3892 return got_error_from_errno(
3893 "got_object_id_str");
3895 if (S_ISLNK(te->mode))
3897 else if (S_ISDIR(te->mode))
3899 else if (te->mode & S_IXUSR)
3901 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3902 te->name, modestr) == -1) {
3904 return got_error_from_errno("asprintf");
3907 err = format_line(&wline, &width, line, view->ncols);
3912 if (n == selected) {
3914 wstandout(view->window);
3915 *selected_entry = te;
3917 waddwstr(view->window, wline);
3918 if (width < view->ncols - 1)
3919 waddch(view->window, '\n');
3920 if (n == selected && view->focussed)
3921 wstandend(view->window);
3927 *last_displayed_entry = te;
3930 te = SIMPLEQ_NEXT(te, entry);
3937 tree_scroll_up(struct tog_view *view,
3938 struct got_tree_entry **first_displayed_entry, int maxscroll,
3939 const struct got_tree_entries *entries, int isroot)
3941 struct got_tree_entry *te, *prev;
3944 if (*first_displayed_entry == NULL)
3947 te = SIMPLEQ_FIRST(&entries->head);
3948 if (*first_displayed_entry == te) {
3950 *first_displayed_entry = NULL;
3954 /* XXX this is stupid... switch to TAILQ? */
3955 for (i = 0; i < maxscroll; i++) {
3956 while (te != *first_displayed_entry) {
3958 te = SIMPLEQ_NEXT(te, entry);
3960 *first_displayed_entry = prev;
3961 te = SIMPLEQ_FIRST(&entries->head);
3963 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3964 *first_displayed_entry = NULL;
3968 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3969 struct got_tree_entry *last_displayed_entry,
3970 const struct got_tree_entries *entries)
3972 struct got_tree_entry *next, *last;
3975 if (*first_displayed_entry)
3976 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3978 next = SIMPLEQ_FIRST(&entries->head);
3979 last = last_displayed_entry;
3980 while (next && last && n++ < maxscroll) {
3981 last = SIMPLEQ_NEXT(last, entry);
3983 *first_displayed_entry = next;
3984 next = SIMPLEQ_NEXT(next, entry);
3990 static const struct got_error *
3991 tree_entry_path(char **path, struct tog_parent_trees *parents,
3992 struct got_tree_entry *te)
3994 const struct got_error *err = NULL;
3995 struct tog_parent_tree *pt;
3996 size_t len = 2; /* for leading slash and NUL */
3998 TAILQ_FOREACH(pt, parents, entry)
3999 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4001 len += strlen(te->name);
4003 *path = calloc(1, len);
4005 return got_error_from_errno("calloc");
4008 pt = TAILQ_LAST(parents, tog_parent_trees);
4010 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4011 err = got_error(GOT_ERR_NO_SPACE);
4014 if (strlcat(*path, "/", len) >= len) {
4015 err = got_error(GOT_ERR_NO_SPACE);
4018 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4021 if (strlcat(*path, te->name, len) >= len) {
4022 err = got_error(GOT_ERR_NO_SPACE);
4034 static const struct got_error *
4035 blame_tree_entry(struct tog_view **new_view, int begin_x,
4036 struct got_tree_entry *te, struct tog_parent_trees *parents,
4037 struct got_object_id *commit_id, struct got_reflist_head *refs,
4038 struct got_repository *repo)
4040 const struct got_error *err = NULL;
4042 struct tog_view *blame_view;
4046 err = tree_entry_path(&path, parents, te);
4050 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4051 if (blame_view == NULL) {
4052 err = got_error_from_errno("view_open");
4056 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4058 view_close(blame_view);
4060 *new_view = blame_view;
4066 static const struct got_error *
4067 log_tree_entry(struct tog_view **new_view, int begin_x,
4068 struct got_tree_entry *te, struct tog_parent_trees *parents,
4069 struct got_object_id *commit_id, struct got_reflist_head *refs,
4070 struct got_repository *repo)
4072 struct tog_view *log_view;
4073 const struct got_error *err = NULL;
4078 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4079 if (log_view == NULL)
4080 return got_error_from_errno("view_open");
4082 err = tree_entry_path(&path, parents, te);
4086 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4088 view_close(log_view);
4090 *new_view = log_view;
4095 static const struct got_error *
4096 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4097 struct got_object_id *commit_id, struct got_reflist_head *refs,
4098 struct got_repository *repo)
4100 const struct got_error *err = NULL;
4101 char *commit_id_str = NULL;
4102 struct tog_tree_view_state *s = &view->state.tree;
4104 TAILQ_INIT(&s->parents);
4106 err = got_object_id_str(&commit_id_str, commit_id);
4110 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4111 err = got_error_from_errno("asprintf");
4115 s->root = s->tree = root;
4116 s->entries = got_object_tree_get_entries(root);
4117 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4118 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4119 s->commit_id = got_object_id_dup(commit_id);
4120 if (s->commit_id == NULL) {
4121 err = got_error_from_errno("got_object_id_dup");
4127 view->show = show_tree_view;
4128 view->input = input_tree_view;
4129 view->close = close_tree_view;
4130 view->search_start = search_start_tree_view;
4131 view->search_next = search_next_tree_view;
4133 free(commit_id_str);
4135 free(s->tree_label);
4136 s->tree_label = NULL;
4141 static const struct got_error *
4142 close_tree_view(struct tog_view *view)
4144 struct tog_tree_view_state *s = &view->state.tree;
4146 free(s->tree_label);
4147 s->tree_label = NULL;
4149 s->commit_id = NULL;
4150 while (!TAILQ_EMPTY(&s->parents)) {
4151 struct tog_parent_tree *parent;
4152 parent = TAILQ_FIRST(&s->parents);
4153 TAILQ_REMOVE(&s->parents, parent, entry);
4157 if (s->tree != s->root)
4158 got_object_tree_close(s->tree);
4159 got_object_tree_close(s->root);
4164 static const struct got_error *
4165 search_start_tree_view(struct tog_view *view)
4167 struct tog_tree_view_state *s = &view->state.tree;
4169 s->matched_entry = NULL;
4174 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4176 regmatch_t regmatch;
4178 return regexec(regex, te->name, 1, ®match, 0) == 0;
4181 static const struct got_error *
4182 search_next_tree_view(struct tog_view *view)
4184 struct tog_tree_view_state *s = &view->state.tree;
4185 struct got_tree_entry *entry = NULL, *te;
4187 if (!view->searching) {
4188 view->search_next_done = 1;
4192 if (s->matched_entry) {
4193 if (view->searching == TOG_SEARCH_FORWARD) {
4194 if (s->selected_entry)
4195 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4197 entry = SIMPLEQ_FIRST(&s->entries->head);
4200 if (s->selected_entry == NULL) {
4201 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4204 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4206 if (SIMPLEQ_NEXT(te, entry) ==
4213 if (view->searching == TOG_SEARCH_FORWARD)
4214 entry = SIMPLEQ_FIRST(&s->entries->head);
4216 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4222 if (entry == NULL) {
4223 if (s->matched_entry == NULL) {
4224 view->search_next_done = 1;
4227 if (view->searching == TOG_SEARCH_FORWARD)
4228 entry = SIMPLEQ_FIRST(&s->entries->head);
4230 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4235 if (match_tree_entry(entry, &view->regex)) {
4236 view->search_next_done = 1;
4237 s->matched_entry = entry;
4241 if (view->searching == TOG_SEARCH_FORWARD)
4242 entry = SIMPLEQ_NEXT(entry, entry);
4244 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4247 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4248 if (SIMPLEQ_NEXT(te, entry) == entry) {
4257 if (s->matched_entry) {
4258 s->first_displayed_entry = s->matched_entry;
4265 static const struct got_error *
4266 show_tree_view(struct tog_view *view)
4268 const struct got_error *err = NULL;
4269 struct tog_tree_view_state *s = &view->state.tree;
4272 err = tree_entry_path(&parent_path, &s->parents, NULL);
4276 err = draw_tree_entries(view, &s->first_displayed_entry,
4277 &s->last_displayed_entry, &s->selected_entry,
4278 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4279 s->entries, s->selected, view->nlines, s->tree == s->root);
4286 static const struct got_error *
4287 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4288 struct tog_view **focus_view, struct tog_view *view, int ch)
4290 const struct got_error *err = NULL;
4291 struct tog_tree_view_state *s = &view->state.tree;
4292 struct tog_view *log_view;
4293 int begin_x = 0, nscrolled;
4297 s->show_ids = !s->show_ids;
4300 if (!s->selected_entry)
4302 if (view_is_parent_view(view))
4303 begin_x = view_split_begin_x(view->begin_x);
4304 err = log_tree_entry(&log_view, begin_x,
4305 s->selected_entry, &s->parents,
4306 s->commit_id, s->refs, s->repo);
4307 if (view_is_parent_view(view)) {
4308 err = view_close_child(view);
4311 err = view_set_child(view, log_view);
4313 view_close(log_view);
4316 *focus_view = log_view;
4317 view->child_focussed = 1;
4319 *new_view = log_view;
4323 if (s->selected > 0) {
4325 if (s->selected == 0)
4328 if (s->selected > 0)
4330 tree_scroll_up(view, &s->first_displayed_entry, 1,
4331 s->entries, s->tree == s->root);
4334 tree_scroll_up(view, &s->first_displayed_entry,
4335 MAX(0, view->nlines - 4 - s->selected), s->entries,
4336 s->tree == s->root);
4338 if (SIMPLEQ_FIRST(&s->entries->head) ==
4339 s->first_displayed_entry && s->tree != s->root)
4340 s->first_displayed_entry = NULL;
4344 if (s->selected < s->ndisplayed - 1) {
4348 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4349 /* can't scroll any further */
4351 tree_scroll_down(&s->first_displayed_entry, 1,
4352 s->last_displayed_entry, s->entries);
4355 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4357 /* can't scroll any further; move cursor down */
4358 if (s->selected < s->ndisplayed - 1)
4359 s->selected = s->ndisplayed - 1;
4362 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4363 view->nlines, s->last_displayed_entry, s->entries);
4364 if (nscrolled < view->nlines) {
4366 struct got_tree_entry *te;
4367 te = s->first_displayed_entry;
4370 te = SIMPLEQ_NEXT(te, entry);
4372 s->selected = ndisplayed - 1;
4378 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4379 struct tog_parent_tree *parent;
4380 /* user selected '..' */
4381 if (s->tree == s->root)
4383 parent = TAILQ_FIRST(&s->parents);
4384 TAILQ_REMOVE(&s->parents, parent,
4386 got_object_tree_close(s->tree);
4387 s->tree = parent->tree;
4389 got_object_tree_get_entries(s->tree);
4390 s->first_displayed_entry =
4391 parent->first_displayed_entry;
4393 parent->selected_entry;
4394 s->selected = parent->selected;
4396 } else if (S_ISDIR(s->selected_entry->mode)) {
4397 struct got_tree_object *subtree;
4398 err = got_object_open_as_tree(&subtree,
4399 s->repo, s->selected_entry->id);
4402 err = tree_view_visit_subtree(subtree, s);
4404 got_object_tree_close(subtree);
4407 } else if (S_ISREG(s->selected_entry->mode)) {
4408 struct tog_view *blame_view;
4409 int begin_x = view_is_parent_view(view) ?
4410 view_split_begin_x(view->begin_x) : 0;
4412 err = blame_tree_entry(&blame_view, begin_x,
4413 s->selected_entry, &s->parents,
4414 s->commit_id, s->refs, s->repo);
4417 if (view_is_parent_view(view)) {
4418 err = view_close_child(view);
4421 err = view_set_child(view, blame_view);
4423 view_close(blame_view);
4426 *focus_view = blame_view;
4427 view->child_focussed = 1;
4429 *new_view = blame_view;
4433 if (s->selected > view->nlines)
4434 s->selected = s->ndisplayed - 1;
4447 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4452 static const struct got_error *
4453 cmd_tree(int argc, char *argv[])
4455 const struct got_error *error;
4456 struct got_repository *repo = NULL;
4457 struct got_reflist_head refs;
4458 char *repo_path = NULL;
4459 struct got_object_id *commit_id = NULL;
4460 char *commit_id_arg = NULL;
4461 struct got_commit_object *commit = NULL;
4462 struct got_tree_object *tree = NULL;
4464 struct tog_view *view;
4466 SIMPLEQ_INIT(&refs);
4469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4474 while ((ch = getopt(argc, argv, "c:")) != -1) {
4477 commit_id_arg = optarg;
4489 struct got_worktree *worktree;
4490 char *cwd = getcwd(NULL, 0);
4492 return got_error_from_errno("getcwd");
4493 error = got_worktree_open(&worktree, cwd);
4494 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4499 strdup(got_worktree_get_repo_path(worktree));
4500 got_worktree_close(worktree);
4503 if (repo_path == NULL) {
4504 error = got_error_from_errno("strdup");
4507 } else if (argc == 1) {
4508 repo_path = realpath(argv[0], NULL);
4509 if (repo_path == NULL)
4510 return got_error_from_errno2("realpath", argv[0]);
4516 error = got_repo_open(&repo, repo_path);
4520 error = apply_unveil(got_repo_get_path(repo), NULL);
4524 if (commit_id_arg == NULL)
4525 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4527 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4529 if (error->code != GOT_ERR_NOT_REF)
4531 error = got_repo_match_object_id_prefix(&commit_id,
4532 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4538 error = got_object_open_as_commit(&commit, repo, commit_id);
4542 error = got_object_open_as_tree(&tree, repo,
4543 got_object_commit_get_tree_id(commit));
4547 error = got_ref_list(&refs, repo);
4551 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4553 error = got_error_from_errno("view_open");
4556 error = open_tree_view(view, tree, commit_id, &refs, repo);
4559 error = view_loop(view);
4564 got_object_commit_close(commit);
4566 got_object_tree_close(tree);
4568 got_repo_close(repo);
4569 got_ref_list_free(&refs);
4578 fprintf(stderr, "commands:");
4579 for (i = 0; i < nitems(tog_commands); i++) {
4580 struct tog_cmd *cmd = &tog_commands[i];
4581 fprintf(stderr, " %s", cmd->name);
4583 fputc('\n', stderr);
4589 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4597 make_argv(const char *arg0, const char *arg1)
4600 int argc = (arg1 == NULL ? 1 : 2);
4602 argv = calloc(argc, sizeof(char *));
4605 argv[0] = strdup(arg0);
4606 if (argv[0] == NULL)
4609 argv[1] = strdup(arg1);
4610 if (argv[1] == NULL)
4618 main(int argc, char *argv[])
4620 const struct got_error *error = NULL;
4621 struct tog_cmd *cmd = NULL;
4622 int ch, hflag = 0, Vflag = 0;
4623 char **cmd_argv = NULL;
4625 setlocale(LC_CTYPE, "");
4627 while ((ch = getopt(argc, argv, "hV")) != -1) {
4647 got_version_print_str();
4654 /* Build an argument vector which runs a default command. */
4655 cmd = &tog_commands[0];
4656 cmd_argv = make_argv(cmd->name, NULL);
4661 /* Did the user specific a command? */
4662 for (i = 0; i < nitems(tog_commands); i++) {
4663 if (strncmp(tog_commands[i].name, argv[0],
4664 strlen(argv[0])) == 0) {
4665 cmd = &tog_commands[i];
4671 fprintf(stderr, "%s: unknown command '%s'\n",
4672 getprogname(), argv[0]);
4681 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4686 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);