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);
1004 cmp = got_object_id_cmp(tag ?
1005 got_object_tag_get_object_id(tag) : re->id, id);
1007 got_object_tag_close(tag);
1011 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1012 s ? ", " : "", name) == -1) {
1013 err = got_error_from_errno("asprintf");
1024 static const struct got_error *
1025 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1027 char *smallerthan, *at;
1029 smallerthan = strchr(author, '<');
1030 if (smallerthan && smallerthan[1] != '\0')
1031 author = smallerthan + 1;
1032 at = strchr(author, '@');
1035 return format_line(wauthor, author_width, author, limit);
1038 static const struct got_error *
1039 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1040 struct got_object_id *id, struct got_reflist_head *refs,
1041 int author_display_cols)
1043 const struct got_error *err = NULL;
1044 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1045 char *logmsg0 = NULL, *logmsg = NULL;
1046 char *author = NULL;
1047 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1048 int author_width, logmsg_width;
1049 char *newline, *line = NULL;
1051 static const size_t date_display_cols = 9;
1052 const int avail = view->ncols;
1054 time_t committer_time;
1056 committer_time = got_object_commit_get_committer_time(commit);
1057 if (localtime_r(&committer_time, &tm) == NULL)
1058 return got_error_from_errno("localtime_r");
1059 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1061 return got_error(GOT_ERR_NO_SPACE);
1063 if (avail < date_display_cols)
1064 limit = MIN(sizeof(datebuf) - 1, avail);
1066 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1067 waddnstr(view->window, datebuf, limit);
1072 author = strdup(got_object_commit_get_author(commit));
1073 if (author == NULL) {
1074 err = got_error_from_errno("strdup");
1077 err = format_author(&wauthor, &author_width, author, avail - col);
1080 waddwstr(view->window, wauthor);
1081 col += author_width;
1082 while (col <= avail && author_width < author_display_cols + 2) {
1083 waddch(view->window, ' ');
1090 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1091 if (logmsg0 == NULL) {
1092 err = got_error_from_errno("strdup");
1096 while (*logmsg == '\n')
1098 newline = strchr(logmsg, '\n');
1101 limit = avail - col;
1102 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1105 waddwstr(view->window, wlogmsg);
1106 col += logmsg_width;
1107 while (col <= avail) {
1108 waddch(view->window, ' ');
1120 static struct commit_queue_entry *
1121 alloc_commit_queue_entry(struct got_commit_object *commit,
1122 struct got_object_id *id)
1124 struct commit_queue_entry *entry;
1126 entry = calloc(1, sizeof(*entry));
1131 entry->commit = commit;
1136 pop_commit(struct commit_queue *commits)
1138 struct commit_queue_entry *entry;
1140 entry = TAILQ_FIRST(&commits->head);
1141 TAILQ_REMOVE(&commits->head, entry, entry);
1142 got_object_commit_close(entry->commit);
1143 commits->ncommits--;
1144 /* Don't free entry->id! It is owned by the commit graph. */
1149 free_commits(struct commit_queue *commits)
1151 while (!TAILQ_EMPTY(&commits->head))
1152 pop_commit(commits);
1155 static const struct got_error *
1156 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1157 int minqueue, struct got_repository *repo, const char *path)
1159 const struct got_error *err = NULL;
1163 * We keep all commits open throughout the lifetime of the log
1164 * view in order to avoid having to re-fetch commits from disk
1165 * while updating the display.
1167 while (nqueued < minqueue) {
1168 struct got_object_id *id;
1169 struct got_commit_object *commit;
1170 struct commit_queue_entry *entry;
1173 err = got_commit_graph_iter_next(&id, graph);
1175 if (err->code != GOT_ERR_ITER_NEED_MORE)
1177 err = got_commit_graph_fetch_commits(graph,
1187 err = got_object_open_as_commit(&commit, repo, id);
1190 entry = alloc_commit_queue_entry(commit, id);
1191 if (entry == NULL) {
1192 err = got_error_from_errno("alloc_commit_queue_entry");
1196 errcode = pthread_mutex_lock(&tog_mutex);
1198 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1202 entry->idx = commits->ncommits;
1203 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1205 commits->ncommits++;
1207 errcode = pthread_mutex_unlock(&tog_mutex);
1208 if (errcode && err == NULL)
1209 err = got_error_set_errno(errcode,
1210 "pthread_mutex_unlock");
1216 static const struct got_error *
1217 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1218 struct got_repository *repo)
1220 const struct got_error *err = NULL;
1221 struct got_reference *head_ref;
1225 err = got_ref_open(&head_ref, repo, branch_name, 0);
1229 err = got_ref_resolve(head_id, repo, head_ref);
1230 got_ref_close(head_ref);
1239 static const struct got_error *
1240 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1241 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1242 struct commit_queue *commits, int selected_idx, int limit,
1243 struct got_reflist_head *refs, const char *path, int commits_needed)
1245 const struct got_error *err = NULL;
1246 struct tog_log_view_state *s = &view->state.log;
1247 struct commit_queue_entry *entry;
1249 int ncommits, author_cols = 10;
1250 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1251 char *refs_str = NULL;
1257 if (ncommits == selected_idx) {
1261 entry = TAILQ_NEXT(entry, entry);
1265 if (*selected && !(view->searching && view->search_next_done == 0)) {
1266 err = got_object_id_str(&id_str, (*selected)->id);
1270 err = build_refs_str(&refs_str, refs, (*selected)->id,
1277 if (commits_needed == 0)
1278 halfdelay(10); /* disable fast refresh */
1280 if (asprintf(&ncommits_str, " [%d/%d] %s",
1281 entry ? entry->idx + 1 : 0, commits->ncommits,
1282 commits_needed > 0 ?
1283 (view->searching && view->search_next_done == 0
1284 ? "searching..." : "loading... ") :
1285 (refs_str ? refs_str : "")) == -1) {
1286 err = got_error_from_errno("asprintf");
1290 if (path && strcmp(path, "/") != 0) {
1291 if (asprintf(&header, "commit %s %s%s",
1292 id_str ? id_str : "........................................",
1293 path, ncommits_str) == -1) {
1294 err = got_error_from_errno("asprintf");
1298 } else if (asprintf(&header, "commit %s%s",
1299 id_str ? id_str : "........................................",
1300 ncommits_str) == -1) {
1301 err = got_error_from_errno("asprintf");
1305 err = format_line(&wline, &width, header, view->ncols);
1309 werase(view->window);
1311 if (view_needs_focus_indication(view))
1312 wstandout(view->window);
1313 waddwstr(view->window, wline);
1314 while (width < view->ncols) {
1315 waddch(view->window, ' ');
1318 if (view_needs_focus_indication(view))
1319 wstandend(view->window);
1324 /* Grow author column size if necessary. */
1331 if (ncommits >= limit - 1)
1333 author = strdup(got_object_commit_get_author(entry->commit));
1334 if (author == NULL) {
1335 err = got_error_from_errno("strdup");
1338 err = format_author(&wauthor, &width, author, COLS);
1339 if (author_cols < width)
1340 author_cols = width;
1343 entry = TAILQ_NEXT(entry, entry);
1350 if (ncommits >= limit - 1)
1352 if (ncommits == selected_idx)
1353 wstandout(view->window);
1354 err = draw_commit(view, entry->commit, entry->id, refs,
1356 if (ncommits == selected_idx)
1357 wstandend(view->window);
1362 entry = TAILQ_NEXT(entry, entry);
1375 scroll_up(struct tog_view *view,
1376 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1377 struct commit_queue *commits)
1379 struct commit_queue_entry *entry;
1382 entry = TAILQ_FIRST(&commits->head);
1383 if (*first_displayed_entry == entry)
1386 entry = *first_displayed_entry;
1387 while (entry && nscrolled < maxscroll) {
1388 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1390 *first_displayed_entry = entry;
1396 static const struct got_error *
1397 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1398 pthread_cond_t *need_commits)
1403 halfdelay(1); /* fast refresh while loading commits */
1405 while (*commits_needed > 0) {
1409 /* Wake the log thread. */
1410 errcode = pthread_cond_signal(need_commits);
1412 return got_error_set_errno(errcode,
1413 "pthread_cond_signal");
1414 errcode = pthread_mutex_unlock(&tog_mutex);
1416 return got_error_set_errno(errcode,
1417 "pthread_mutex_unlock");
1419 errcode = pthread_mutex_lock(&tog_mutex);
1421 return got_error_set_errno(errcode,
1422 "pthread_mutex_lock");
1424 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1426 * Thread is not done yet; lose a key press
1427 * and let the user retry... this way the GUI
1428 * remains interactive while logging deep paths
1429 * with few commits in history.
1438 static const struct got_error *
1439 scroll_down(struct tog_view *view,
1440 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1441 struct commit_queue_entry **last_displayed_entry,
1442 struct commit_queue *commits, int *log_complete, int *commits_needed,
1443 pthread_cond_t *need_commits)
1445 const struct got_error *err = NULL;
1446 struct commit_queue_entry *pentry;
1449 if (*last_displayed_entry == NULL)
1452 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1453 if (pentry == NULL && !*log_complete) {
1455 * Ask the log thread for required amount of commits
1456 * plus some amount of pre-fetching.
1458 (*commits_needed) += maxscroll + 20;
1459 err = trigger_log_thread(0, commits_needed, log_complete,
1466 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1470 *last_displayed_entry = pentry;
1472 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1475 *first_displayed_entry = pentry;
1476 } while (++nscrolled < maxscroll);
1481 static const struct got_error *
1482 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1483 struct got_commit_object *commit, struct got_object_id *commit_id,
1484 struct tog_view *log_view, struct got_reflist_head *refs,
1485 struct got_repository *repo)
1487 const struct got_error *err;
1488 struct got_object_qid *parent_id;
1489 struct tog_view *diff_view;
1491 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1492 if (diff_view == NULL)
1493 return got_error_from_errno("view_open");
1495 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1496 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1497 commit_id, log_view, refs, repo);
1499 *new_view = diff_view;
1503 static const struct got_error *
1504 tree_view_visit_subtree(struct got_tree_object *subtree,
1505 struct tog_tree_view_state *s)
1507 struct tog_parent_tree *parent;
1509 parent = calloc(1, sizeof(*parent));
1511 return got_error_from_errno("calloc");
1513 parent->tree = s->tree;
1514 parent->first_displayed_entry = s->first_displayed_entry;
1515 parent->selected_entry = s->selected_entry;
1516 parent->selected = s->selected;
1517 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1519 s->entries = got_object_tree_get_entries(s->tree);
1521 s->first_displayed_entry = NULL;
1526 static const struct got_error *
1527 browse_commit_tree(struct tog_view **new_view, int begin_x,
1528 struct commit_queue_entry *entry, const char *path,
1529 struct got_reflist_head *refs, struct got_repository *repo)
1531 const struct got_error *err = NULL;
1532 struct got_tree_object *tree;
1533 struct got_tree_entry *te;
1534 struct tog_tree_view_state *s;
1535 struct tog_view *tree_view;
1536 char *slash, *subpath = NULL;
1539 err = got_object_open_as_tree(&tree, repo,
1540 got_object_commit_get_tree_id(entry->commit));
1544 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1545 if (tree_view == NULL)
1546 return got_error_from_errno("view_open");
1548 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1550 got_object_tree_close(tree);
1553 s = &tree_view->state.tree;
1555 *new_view = tree_view;
1557 /* Walk the path and open corresponding tree objects. */
1562 struct got_object_id *tree_id;
1564 /* Ensure the correct subtree entry is selected. */
1565 slash = strchr(p, '/');
1567 slash = strchr(p, '\0');
1568 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1569 if (strncmp(p, te->name, slash - p) == 0) {
1570 s->selected_entry = te;
1575 if (s->selected_entry == NULL) {
1576 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1579 if (s->tree != s->root)
1580 s->selected++; /* skip '..' */
1582 if (!S_ISDIR(s->selected_entry->mode)) {
1583 /* Jump to this file's entry. */
1584 s->first_displayed_entry = s->selected_entry;
1589 slash = strchr(p, '/');
1591 subpath = strndup(path, slash - path);
1593 subpath = strdup(path);
1594 if (subpath == NULL) {
1595 err = got_error_from_errno("strdup");
1599 err = got_object_id_by_path(&tree_id, repo, entry->id,
1604 err = got_object_open_as_tree(&tree, repo, tree_id);
1609 err = tree_view_visit_subtree(tree, s);
1611 got_object_tree_close(tree);
1626 log_thread(void *arg)
1628 const struct got_error *err = NULL;
1630 struct tog_log_thread_args *a = arg;
1633 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1637 while (!done && !err && !tog_sigpipe_received) {
1638 err = queue_commits(a->graph, a->commits, 1, a->repo,
1641 if (err->code != GOT_ERR_ITER_COMPLETED)
1645 } else if (a->commits_needed > 0)
1646 a->commits_needed--;
1648 errcode = pthread_mutex_lock(&tog_mutex);
1650 err = got_error_set_errno(errcode,
1651 "pthread_mutex_lock");
1653 } else if (*a->quit)
1655 else if (*a->first_displayed_entry == NULL) {
1656 *a->first_displayed_entry =
1657 TAILQ_FIRST(&a->commits->head);
1658 *a->selected_entry = *a->first_displayed_entry;
1662 a->commits_needed = 0;
1663 else if (a->commits_needed == 0) {
1664 errcode = pthread_cond_wait(&a->need_commits,
1667 err = got_error_set_errno(errcode,
1668 "pthread_cond_wait");
1671 errcode = pthread_mutex_unlock(&tog_mutex);
1672 if (errcode && err == NULL)
1673 err = got_error_set_errno(errcode,
1674 "pthread_mutex_unlock");
1676 a->log_complete = 1;
1680 static const struct got_error *
1681 stop_log_thread(struct tog_log_view_state *s)
1683 const struct got_error *err = NULL;
1688 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1690 return got_error_set_errno(errcode,
1691 "pthread_cond_signal");
1692 errcode = pthread_mutex_unlock(&tog_mutex);
1694 return got_error_set_errno(errcode,
1695 "pthread_mutex_unlock");
1696 errcode = pthread_join(s->thread, (void **)&err);
1698 return got_error_set_errno(errcode, "pthread_join");
1699 errcode = pthread_mutex_lock(&tog_mutex);
1701 return got_error_set_errno(errcode,
1702 "pthread_mutex_lock");
1706 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1707 if (errcode && err == NULL)
1708 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1710 if (s->thread_args.repo) {
1711 got_repo_close(s->thread_args.repo);
1712 s->thread_args.repo = NULL;
1715 if (s->thread_args.graph) {
1716 got_commit_graph_close(s->thread_args.graph);
1717 s->thread_args.graph = NULL;
1723 static const struct got_error *
1724 close_log_view(struct tog_view *view)
1726 const struct got_error *err = NULL;
1727 struct tog_log_view_state *s = &view->state.log;
1729 err = stop_log_thread(s);
1730 free_commits(&s->commits);
1731 free(s->in_repo_path);
1732 s->in_repo_path = NULL;
1738 static const struct got_error *
1739 search_start_log_view(struct tog_view *view)
1741 struct tog_log_view_state *s = &view->state.log;
1743 s->matched_entry = NULL;
1744 s->search_entry = NULL;
1749 match_commit(struct got_commit_object *commit, const char *id_str,
1752 regmatch_t regmatch;
1754 if (regexec(regex, got_object_commit_get_author(commit), 1,
1755 ®match, 0) == 0 ||
1756 regexec(regex, got_object_commit_get_committer(commit), 1,
1757 ®match, 0) == 0 ||
1758 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1759 ®match, 0) == 0 ||
1760 regexec(regex, id_str, 1, ®match, 0) == 0)
1766 static const struct got_error *
1767 search_next_log_view(struct tog_view *view)
1769 const struct got_error *err = NULL;
1770 struct tog_log_view_state *s = &view->state.log;
1771 struct commit_queue_entry *entry;
1773 if (!view->searching) {
1774 view->search_next_done = 1;
1778 if (s->search_entry) {
1779 if (wgetch(view->window) == KEY_BACKSPACE) {
1780 view->search_next_done = 1;
1783 if (view->searching == TOG_SEARCH_FORWARD)
1784 entry = TAILQ_NEXT(s->search_entry, entry);
1786 entry = TAILQ_PREV(s->search_entry,
1787 commit_queue_head, entry);
1788 } else if (s->matched_entry) {
1789 if (view->searching == TOG_SEARCH_FORWARD)
1790 entry = TAILQ_NEXT(s->selected_entry, entry);
1792 entry = TAILQ_PREV(s->selected_entry,
1793 commit_queue_head, entry);
1795 if (view->searching == TOG_SEARCH_FORWARD)
1796 entry = TAILQ_FIRST(&s->commits.head);
1798 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1803 if (entry == NULL) {
1804 if (s->thread_args.log_complete ||
1805 view->searching == TOG_SEARCH_BACKWARD) {
1806 view->search_next_done = 1;
1810 * Poke the log thread for more commits and return,
1811 * allowing the main loop to make progress. Search
1812 * will resume at s->search_entry once we come back.
1814 s->thread_args.commits_needed++;
1815 return trigger_log_thread(1,
1816 &s->thread_args.commits_needed,
1817 &s->thread_args.log_complete,
1818 &s->thread_args.need_commits);
1821 err = got_object_id_str(&id_str, entry->id);
1825 if (match_commit(entry->commit, id_str, &view->regex)) {
1826 view->search_next_done = 1;
1827 s->matched_entry = entry;
1832 s->search_entry = entry;
1833 if (view->searching == TOG_SEARCH_FORWARD)
1834 entry = TAILQ_NEXT(entry, entry);
1836 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1839 if (s->matched_entry) {
1840 int cur = s->selected_entry->idx;
1841 while (cur < s->matched_entry->idx) {
1842 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1847 while (cur > s->matched_entry->idx) {
1848 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1855 s->search_entry = NULL;
1860 static const struct got_error *
1861 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1862 struct got_reflist_head *refs, struct got_repository *repo,
1863 const char *head_ref_name, const char *path, int check_disk)
1865 const struct got_error *err = NULL;
1866 struct tog_log_view_state *s = &view->state.log;
1867 struct got_repository *thread_repo = NULL;
1868 struct got_commit_graph *thread_graph = NULL;
1871 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1875 /* The commit queue only contains commits being displayed. */
1876 TAILQ_INIT(&s->commits.head);
1877 s->commits.ncommits = 0;
1881 s->head_ref_name = head_ref_name;
1882 s->start_id = got_object_id_dup(start_id);
1883 if (s->start_id == NULL) {
1884 err = got_error_from_errno("got_object_id_dup");
1888 view->show = show_log_view;
1889 view->input = input_log_view;
1890 view->close = close_log_view;
1891 view->search_start = search_start_log_view;
1892 view->search_next = search_next_log_view;
1894 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1897 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1902 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1904 err = got_error_set_errno(errcode, "pthread_cond_init");
1908 s->thread_args.commits_needed = view->nlines;
1909 s->thread_args.graph = thread_graph;
1910 s->thread_args.commits = &s->commits;
1911 s->thread_args.in_repo_path = s->in_repo_path;
1912 s->thread_args.start_id = s->start_id;
1913 s->thread_args.repo = thread_repo;
1914 s->thread_args.log_complete = 0;
1915 s->thread_args.quit = &s->quit;
1916 s->thread_args.view = view;
1917 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1918 s->thread_args.selected_entry = &s->selected_entry;
1921 close_log_view(view);
1925 static const struct got_error *
1926 show_log_view(struct tog_view *view)
1928 struct tog_log_view_state *s = &view->state.log;
1930 if (s->thread == NULL) {
1931 int errcode = pthread_create(&s->thread, NULL, log_thread,
1934 return got_error_set_errno(errcode, "pthread_create");
1937 return draw_commits(view, &s->last_displayed_entry,
1938 &s->selected_entry, s->first_displayed_entry,
1939 &s->commits, s->selected, view->nlines, s->refs,
1940 s->in_repo_path, s->thread_args.commits_needed);
1943 static const struct got_error *
1944 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1945 struct tog_view **focus_view, struct tog_view *view, int ch)
1947 const struct got_error *err = NULL;
1948 struct tog_log_view_state *s = &view->state.log;
1949 char *parent_path, *in_repo_path = NULL;
1950 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1952 struct got_object_id *start_id;
1962 if (s->first_displayed_entry == NULL)
1964 if (s->selected > 0)
1967 scroll_up(view, &s->first_displayed_entry, 1,
1972 if (s->first_displayed_entry == NULL)
1974 if (TAILQ_FIRST(&s->commits.head) ==
1975 s->first_displayed_entry) {
1979 scroll_up(view, &s->first_displayed_entry,
1980 view->nlines, &s->commits);
1986 if (s->first_displayed_entry == NULL)
1988 if (s->selected < MIN(view->nlines - 2,
1989 s->commits.ncommits - 1)) {
1993 err = scroll_down(view, &s->first_displayed_entry, 1,
1994 &s->last_displayed_entry, &s->commits,
1995 &s->thread_args.log_complete,
1996 &s->thread_args.commits_needed,
1997 &s->thread_args.need_commits);
2001 struct commit_queue_entry *first;
2002 first = s->first_displayed_entry;
2005 err = scroll_down(view, &s->first_displayed_entry,
2006 view->nlines, &s->last_displayed_entry,
2007 &s->commits, &s->thread_args.log_complete,
2008 &s->thread_args.commits_needed,
2009 &s->thread_args.need_commits);
2010 if (first == s->first_displayed_entry &&
2011 s->selected < MIN(view->nlines - 2,
2012 s->commits.ncommits - 1)) {
2013 /* can't scroll further down */
2014 s->selected = MIN(view->nlines - 2,
2015 s->commits.ncommits - 1);
2021 if (s->selected > view->nlines - 2)
2022 s->selected = view->nlines - 2;
2023 if (s->selected > s->commits.ncommits - 1)
2024 s->selected = s->commits.ncommits - 1;
2029 if (s->selected_entry == NULL)
2031 if (view_is_parent_view(view))
2032 begin_x = view_split_begin_x(view->begin_x);
2033 err = open_diff_view_for_commit(&diff_view, begin_x,
2034 s->selected_entry->commit, s->selected_entry->id,
2035 view, s->refs, s->repo);
2038 if (view_is_parent_view(view)) {
2039 err = view_close_child(view);
2042 err = view_set_child(view, diff_view);
2044 view_close(diff_view);
2047 *focus_view = diff_view;
2048 view->child_focussed = 1;
2050 *new_view = diff_view;
2053 if (s->selected_entry == NULL)
2055 if (view_is_parent_view(view))
2056 begin_x = view_split_begin_x(view->begin_x);
2057 err = browse_commit_tree(&tree_view, begin_x,
2058 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2061 if (view_is_parent_view(view)) {
2062 err = view_close_child(view);
2065 err = view_set_child(view, tree_view);
2067 view_close(tree_view);
2070 *focus_view = tree_view;
2071 view->child_focussed = 1;
2073 *new_view = tree_view;
2076 if (strcmp(s->in_repo_path, "/") == 0)
2078 parent_path = dirname(s->in_repo_path);
2079 if (parent_path && strcmp(parent_path, ".") != 0) {
2080 err = stop_log_thread(s);
2083 lv = view_open(view->nlines, view->ncols,
2084 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2086 return got_error_from_errno(
2088 err = open_log_view(lv, s->start_id, s->refs,
2089 s->repo, s->head_ref_name, parent_path, 0);
2092 if (view_is_parent_view(view))
2095 view_set_child(view->parent, lv);
2102 err = stop_log_thread(s);
2105 lv = view_open(view->nlines, view->ncols,
2106 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2108 return got_error_from_errno("view_open");
2109 err = get_head_commit_id(&start_id, s->head_ref_name ?
2110 s->head_ref_name : GOT_REF_HEAD, s->repo);
2115 in_repo_path = strdup(s->in_repo_path);
2116 if (in_repo_path == NULL) {
2119 return got_error_from_errno("strdup");
2121 err = open_log_view(lv, start_id, s->refs, s->repo,
2122 s->head_ref_name, in_repo_path, 0);
2138 static const struct got_error *
2139 apply_unveil(const char *repo_path, const char *worktree_path)
2141 const struct got_error *error;
2144 if (unveil("gmon.out", "rwc") != 0)
2145 return got_error_from_errno2("unveil", "gmon.out");
2147 if (repo_path && unveil(repo_path, "r") != 0)
2148 return got_error_from_errno2("unveil", repo_path);
2150 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2151 return got_error_from_errno2("unveil", worktree_path);
2153 if (unveil("/tmp", "rwc") != 0)
2154 return got_error_from_errno2("unveil", "/tmp");
2156 error = got_privsep_unveil_exec_helpers();
2160 if (unveil(NULL, NULL) != 0)
2161 return got_error_from_errno("unveil");
2171 halfdelay(1); /* Do fast refresh while initial view is loading. */
2174 intrflush(stdscr, FALSE);
2175 keypad(stdscr, TRUE);
2177 signal(SIGWINCH, tog_sigwinch);
2178 signal(SIGPIPE, tog_sigpipe);
2181 static const struct got_error *
2182 cmd_log(int argc, char *argv[])
2184 const struct got_error *error;
2185 struct got_repository *repo = NULL;
2186 struct got_worktree *worktree = NULL;
2187 struct got_reflist_head refs;
2188 struct got_object_id *start_id = NULL;
2189 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2190 char *start_commit = NULL;
2192 struct tog_view *view;
2194 SIMPLEQ_INIT(&refs);
2197 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2202 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2205 start_commit = optarg;
2208 repo_path = realpath(optarg, NULL);
2209 if (repo_path == NULL)
2210 err(1, "-r option");
2221 cwd = getcwd(NULL, 0);
2223 error = got_error_from_errno("getcwd");
2226 error = got_worktree_open(&worktree, cwd);
2227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2234 error = got_error_from_errno("strdup");
2237 } else if (argc == 1) {
2239 error = got_worktree_resolve_path(&path, worktree,
2244 path = strdup(argv[0]);
2246 error = got_error_from_errno("strdup");
2253 if (repo_path == NULL) {
2256 got_worktree_get_repo_path(worktree));
2258 repo_path = strdup(cwd);
2260 if (repo_path == NULL) {
2261 error = got_error_from_errno("strdup");
2267 error = got_repo_open(&repo, repo_path);
2271 error = apply_unveil(got_repo_get_path(repo),
2272 worktree ? got_worktree_get_root_path(worktree) : NULL);
2276 if (start_commit == NULL)
2277 error = get_head_commit_id(&start_id, worktree ?
2278 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2281 error = get_head_commit_id(&start_id, start_commit, repo);
2283 if (error->code != GOT_ERR_NOT_REF)
2285 error = got_repo_match_object_id_prefix(&start_id,
2286 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2292 error = got_ref_list(&refs, repo);
2296 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2298 error = got_error_from_errno("view_open");
2301 error = open_log_view(view, start_id, &refs, repo, worktree ?
2302 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2305 error = view_loop(view);
2312 got_repo_close(repo);
2314 got_worktree_close(worktree);
2315 got_ref_list_free(&refs);
2323 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2329 parse_next_line(FILE *f, size_t *len)
2334 const char delim[3] = { '\0', '\0', '\0'};
2336 line = fparseln(f, &linelen, &lineno, delim, 0);
2342 static const struct got_error *
2343 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2344 int *last_displayed_line, int *eof, int max_lines,
2347 const struct got_error *err;
2348 int nlines = 0, nprinted = 0;
2355 werase(view->window);
2358 err = format_line(&wline, &width, header, view->ncols);
2363 if (view_needs_focus_indication(view))
2364 wstandout(view->window);
2365 waddwstr(view->window, wline);
2366 if (view_needs_focus_indication(view))
2367 wstandend(view->window);
2368 if (width < view->ncols - 1)
2369 waddch(view->window, '\n');
2377 while (nprinted < max_lines) {
2378 line = parse_next_line(f, &len);
2383 if (++nlines < *first_displayed_line) {
2388 err = format_line(&wline, &width, line, view->ncols);
2393 waddwstr(view->window, wline);
2394 if (width < view->ncols - 1)
2395 waddch(view->window, '\n');
2396 if (++nprinted == 1)
2397 *first_displayed_line = nlines;
2402 *last_displayed_line = nlines;
2407 while (nprinted < view->nlines) {
2408 waddch(view->window, '\n');
2412 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2417 wstandout(view->window);
2418 waddwstr(view->window, wline);
2419 wstandend(view->window);
2426 get_datestr(time_t *time, char *datebuf)
2428 char *p, *s = ctime_r(time, datebuf);
2429 p = strchr(s, '\n');
2435 static const struct got_error *
2436 write_commit_info(struct got_object_id *commit_id,
2437 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2439 const struct got_error *err = NULL;
2441 struct got_commit_object *commit;
2442 char *id_str = NULL;
2443 time_t committer_time;
2444 const char *author, *committer;
2445 char *refs_str = NULL;
2448 err = build_refs_str(&refs_str, refs, commit_id, repo);
2453 err = got_object_open_as_commit(&commit, repo, commit_id);
2457 err = got_object_id_str(&id_str, commit_id);
2459 err = got_error_from_errno("got_object_id_str");
2463 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2464 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2465 err = got_error_from_errno("fprintf");
2468 if (fprintf(outfile, "from: %s\n",
2469 got_object_commit_get_author(commit)) < 0) {
2470 err = got_error_from_errno("fprintf");
2473 committer_time = got_object_commit_get_committer_time(commit);
2474 if (fprintf(outfile, "date: %s UTC\n",
2475 get_datestr(&committer_time, datebuf)) < 0) {
2476 err = got_error_from_errno("fprintf");
2479 author = got_object_commit_get_author(commit);
2480 committer = got_object_commit_get_committer(commit);
2481 if (strcmp(author, committer) != 0 &&
2482 fprintf(outfile, "via: %s\n", committer) < 0) {
2483 err = got_error_from_errno("fprintf");
2486 if (fprintf(outfile, "%s\n",
2487 got_object_commit_get_logmsg(commit)) < 0) {
2488 err = got_error_from_errno("fprintf");
2494 got_object_commit_close(commit);
2498 static const struct got_error *
2499 create_diff(struct tog_diff_view_state *s)
2501 const struct got_error *err = NULL;
2507 err = got_error_from_errno("got_opentemp");
2510 if (s->f && fclose(s->f) != 0) {
2511 err = got_error_from_errno("fclose");
2517 err = got_object_get_type(&obj_type, s->repo, s->id1);
2519 err = got_object_get_type(&obj_type, s->repo, s->id2);
2524 case GOT_OBJ_TYPE_BLOB:
2525 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2526 s->diff_context, s->repo, f);
2528 case GOT_OBJ_TYPE_TREE:
2529 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2530 s->diff_context, s->repo, f);
2532 case GOT_OBJ_TYPE_COMMIT: {
2533 const struct got_object_id_queue *parent_ids;
2534 struct got_object_qid *pid;
2535 struct got_commit_object *commit2;
2537 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2540 /* Show commit info if we're diffing to a parent/root commit. */
2542 write_commit_info(s->id2, s->refs, s->repo, f);
2544 parent_ids = got_object_commit_get_parent_ids(commit2);
2545 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2546 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2547 write_commit_info(s->id2, s->refs,
2553 got_object_commit_close(commit2);
2555 err = got_diff_objects_as_commits(s->id1, s->id2,
2556 s->diff_context, s->repo, f);
2560 err = got_error(GOT_ERR_OBJ_TYPE);
2564 if (f && fflush(f) != 0 && err == NULL)
2565 err = got_error_from_errno("fflush");
2570 diff_view_indicate_progress(struct tog_view *view)
2572 mvwaddstr(view->window, 0, 0, "diffing...");
2577 static const struct got_error *
2578 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2579 struct got_object_id *id2, struct tog_view *log_view,
2580 struct got_reflist_head *refs, struct got_repository *repo)
2582 const struct got_error *err;
2584 if (id1 != NULL && id2 != NULL) {
2586 err = got_object_get_type(&type1, repo, id1);
2589 err = got_object_get_type(&type2, repo, id2);
2594 return got_error(GOT_ERR_OBJ_TYPE);
2598 view->state.diff.id1 = got_object_id_dup(id1);
2599 if (view->state.diff.id1 == NULL)
2600 return got_error_from_errno("got_object_id_dup");
2602 view->state.diff.id1 = NULL;
2604 view->state.diff.id2 = got_object_id_dup(id2);
2605 if (view->state.diff.id2 == NULL) {
2606 free(view->state.diff.id1);
2607 view->state.diff.id1 = NULL;
2608 return got_error_from_errno("got_object_id_dup");
2610 view->state.diff.f = NULL;
2611 view->state.diff.first_displayed_line = 1;
2612 view->state.diff.last_displayed_line = view->nlines;
2613 view->state.diff.diff_context = 3;
2614 view->state.diff.log_view = log_view;
2615 view->state.diff.repo = repo;
2616 view->state.diff.refs = refs;
2618 if (log_view && view_is_splitscreen(view))
2619 show_log_view(log_view); /* draw vborder */
2620 diff_view_indicate_progress(view);
2622 err = create_diff(&view->state.diff);
2624 free(view->state.diff.id1);
2625 view->state.diff.id1 = NULL;
2626 free(view->state.diff.id2);
2627 view->state.diff.id2 = NULL;
2631 view->show = show_diff_view;
2632 view->input = input_diff_view;
2633 view->close = close_diff_view;
2638 static const struct got_error *
2639 close_diff_view(struct tog_view *view)
2641 const struct got_error *err = NULL;
2643 free(view->state.diff.id1);
2644 view->state.diff.id1 = NULL;
2645 free(view->state.diff.id2);
2646 view->state.diff.id2 = NULL;
2647 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2648 err = got_error_from_errno("fclose");
2652 static const struct got_error *
2653 show_diff_view(struct tog_view *view)
2655 const struct got_error *err;
2656 struct tog_diff_view_state *s = &view->state.diff;
2657 char *id_str1 = NULL, *id_str2, *header;
2660 err = got_object_id_str(&id_str1, s->id1);
2664 err = got_object_id_str(&id_str2, s->id2);
2668 if (asprintf(&header, "diff %s %s",
2669 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2670 err = got_error_from_errno("asprintf");
2678 return draw_file(view, s->f, &s->first_displayed_line,
2679 &s->last_displayed_line, &s->eof, view->nlines,
2683 static const struct got_error *
2684 set_selected_commit(struct tog_diff_view_state *s,
2685 struct commit_queue_entry *entry)
2687 const struct got_error *err;
2688 const struct got_object_id_queue *parent_ids;
2689 struct got_commit_object *selected_commit;
2690 struct got_object_qid *pid;
2693 s->id2 = got_object_id_dup(entry->id);
2695 return got_error_from_errno("got_object_id_dup");
2697 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2700 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2702 pid = SIMPLEQ_FIRST(parent_ids);
2703 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2704 got_object_commit_close(selected_commit);
2708 static const struct got_error *
2709 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2710 struct tog_view **focus_view, struct tog_view *view, int ch)
2712 const struct got_error *err = NULL;
2713 struct tog_diff_view_state *s = &view->state.diff;
2714 struct tog_log_view_state *ls;
2715 struct commit_queue_entry *entry;
2721 if (s->first_displayed_line > 1)
2722 s->first_displayed_line--;
2726 if (s->first_displayed_line == 1)
2729 while (i++ < view->nlines - 1 &&
2730 s->first_displayed_line > 1)
2731 s->first_displayed_line--;
2736 s->first_displayed_line++;
2744 while (!s->eof && i++ < view->nlines - 1) {
2746 line = parse_next_line(s->f, NULL);
2747 s->first_displayed_line++;
2753 if (s->diff_context > 0) {
2755 diff_view_indicate_progress(view);
2756 err = create_diff(s);
2760 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2762 diff_view_indicate_progress(view);
2763 err = create_diff(s);
2768 if (s->log_view == NULL)
2770 ls = &s->log_view->state.log;
2771 entry = TAILQ_PREV(ls->selected_entry,
2772 commit_queue_head, entry);
2776 err = input_log_view(NULL, NULL, NULL, s->log_view,
2781 err = set_selected_commit(s, entry);
2785 s->first_displayed_line = 1;
2786 s->last_displayed_line = view->nlines;
2788 diff_view_indicate_progress(view);
2789 err = create_diff(s);
2793 if (s->log_view == NULL)
2795 ls = &s->log_view->state.log;
2797 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2798 ls->thread_args.commits_needed++;
2800 /* Display "loading..." in log view. */
2801 show_log_view(s->log_view);
2805 err = trigger_log_thread(1 /* load_all */,
2806 &ls->thread_args.commits_needed,
2807 &ls->thread_args.log_complete,
2808 &ls->thread_args.need_commits);
2812 err = input_log_view(NULL, NULL, NULL, s->log_view,
2817 entry = TAILQ_NEXT(ls->selected_entry, entry);
2821 err = set_selected_commit(s, entry);
2825 s->first_displayed_line = 1;
2826 s->last_displayed_line = view->nlines;
2828 diff_view_indicate_progress(view);
2829 err = create_diff(s);
2838 static const struct got_error *
2839 cmd_diff(int argc, char *argv[])
2841 const struct got_error *error = NULL;
2842 struct got_repository *repo = NULL;
2843 struct got_reflist_head refs;
2844 struct got_object_id *id1 = NULL, *id2 = NULL;
2845 char *repo_path = NULL;
2846 char *id_str1 = NULL, *id_str2 = NULL;
2848 struct tog_view *view;
2850 SIMPLEQ_INIT(&refs);
2853 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2858 while ((ch = getopt(argc, argv, "")) != -1) {
2870 usage_diff(); /* TODO show local worktree changes */
2871 } else if (argc == 2) {
2872 repo_path = getcwd(NULL, 0);
2873 if (repo_path == NULL)
2874 return got_error_from_errno("getcwd");
2877 } else if (argc == 3) {
2878 repo_path = realpath(argv[0], NULL);
2879 if (repo_path == NULL)
2880 return got_error_from_errno2("realpath", argv[0]);
2888 error = got_repo_open(&repo, repo_path);
2892 error = apply_unveil(got_repo_get_path(repo), NULL);
2896 error = got_repo_match_object_id_prefix(&id1, id_str1,
2897 GOT_OBJ_TYPE_ANY, repo);
2901 error = got_repo_match_object_id_prefix(&id2, id_str2,
2902 GOT_OBJ_TYPE_ANY, repo);
2906 error = got_ref_list(&refs, repo);
2910 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2912 error = got_error_from_errno("view_open");
2915 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2918 error = view_loop(view);
2922 got_repo_close(repo);
2923 got_ref_list_free(&refs);
2931 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2936 struct tog_blame_line {
2938 struct got_object_id *id;
2941 static const struct got_error *
2942 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2943 const char *path, struct tog_blame_line *lines, int nlines,
2944 int blame_complete, int selected_line, int *first_displayed_line,
2945 int *last_displayed_line, int *eof, int max_lines)
2947 const struct got_error *err;
2948 int lineno = 0, nprinted = 0;
2953 struct tog_blame_line *blame_line;
2954 struct got_object_id *prev_id = NULL;
2957 err = got_object_id_str(&id_str, id);
2962 werase(view->window);
2964 if (asprintf(&line, "commit %s", id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2970 err = format_line(&wline, &width, line, view->ncols);
2973 if (view_needs_focus_indication(view))
2974 wstandout(view->window);
2975 waddwstr(view->window, wline);
2976 if (view_needs_focus_indication(view))
2977 wstandend(view->window);
2980 if (width < view->ncols - 1)
2981 waddch(view->window, '\n');
2983 if (asprintf(&line, "[%d/%d] %s%s",
2984 *first_displayed_line - 1 + selected_line, nlines,
2985 blame_complete ? "" : "annotating... ", path) == -1) {
2987 return got_error_from_errno("asprintf");
2990 err = format_line(&wline, &width, line, view->ncols);
2995 waddwstr(view->window, wline);
2998 if (width < view->ncols - 1)
2999 waddch(view->window, '\n');
3002 while (nprinted < max_lines - 2) {
3003 line = parse_next_line(f, &len);
3008 if (++lineno < *first_displayed_line) {
3013 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3014 err = format_line(&wline, &width, line, wlimit);
3020 if (view->focussed && nprinted == selected_line - 1)
3021 wstandout(view->window);
3024 blame_line = &lines[lineno - 1];
3025 if (blame_line->annotated && prev_id &&
3026 got_object_id_cmp(prev_id, blame_line->id) == 0)
3027 waddstr(view->window, " ");
3028 else if (blame_line->annotated) {
3030 err = got_object_id_str(&id_str, blame_line->id);
3036 wprintw(view->window, "%.8s ", id_str);
3038 prev_id = blame_line->id;
3040 waddstr(view->window, "........ ");
3044 waddstr(view->window, "........ ");
3048 waddwstr(view->window, wline);
3049 while (width < wlimit) {
3050 waddch(view->window, ' ');
3053 if (view->focussed && nprinted == selected_line - 1)
3054 wstandend(view->window);
3055 if (++nprinted == 1)
3056 *first_displayed_line = lineno;
3061 *last_displayed_line = lineno;
3068 static const struct got_error *
3069 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3071 const struct got_error *err = NULL;
3072 struct tog_blame_cb_args *a = arg;
3073 struct tog_blame_line *line;
3076 if (nlines != a->nlines ||
3077 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3078 return got_error(GOT_ERR_RANGE);
3080 errcode = pthread_mutex_lock(&tog_mutex);
3082 return got_error_set_errno(errcode, "pthread_mutex_lock");
3084 if (*a->quit) { /* user has quit the blame view */
3085 err = got_error(GOT_ERR_ITER_COMPLETED);
3090 goto done; /* no change in this commit */
3092 line = &a->lines[lineno - 1];
3093 if (line->annotated)
3096 line->id = got_object_id_dup(id);
3097 if (line->id == NULL) {
3098 err = got_error_from_errno("got_object_id_dup");
3101 line->annotated = 1;
3103 errcode = pthread_mutex_unlock(&tog_mutex);
3105 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3110 blame_thread(void *arg)
3112 const struct got_error *err;
3113 struct tog_blame_thread_args *ta = arg;
3114 struct tog_blame_cb_args *a = ta->cb_args;
3117 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3118 blame_cb, ta->cb_args);
3120 errcode = pthread_mutex_lock(&tog_mutex);
3122 return (void *)got_error_set_errno(errcode,
3123 "pthread_mutex_lock");
3125 got_repo_close(ta->repo);
3129 errcode = pthread_mutex_unlock(&tog_mutex);
3130 if (errcode && err == NULL)
3131 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3136 static struct got_object_id *
3137 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3138 int first_displayed_line, int selected_line)
3140 struct tog_blame_line *line;
3145 line = &lines[first_displayed_line - 1 + selected_line - 1];
3146 if (!line->annotated)
3152 static const struct got_error *
3153 stop_blame(struct tog_blame *blame)
3155 const struct got_error *err = NULL;
3158 if (blame->thread) {
3160 errcode = pthread_mutex_unlock(&tog_mutex);
3162 return got_error_set_errno(errcode,
3163 "pthread_mutex_unlock");
3164 errcode = pthread_join(blame->thread, (void **)&err);
3166 return got_error_set_errno(errcode, "pthread_join");
3167 errcode = pthread_mutex_lock(&tog_mutex);
3169 return got_error_set_errno(errcode,
3170 "pthread_mutex_lock");
3171 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3173 blame->thread = NULL;
3175 if (blame->thread_args.repo) {
3176 got_repo_close(blame->thread_args.repo);
3177 blame->thread_args.repo = NULL;
3180 if (fclose(blame->f) != 0 && err == NULL)
3181 err = got_error_from_errno("fclose");
3185 for (i = 0; i < blame->nlines; i++)
3186 free(blame->lines[i].id);
3188 blame->lines = NULL;
3190 free(blame->cb_args.commit_id);
3191 blame->cb_args.commit_id = NULL;
3196 static const struct got_error *
3197 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3198 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3199 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3200 struct got_repository *repo)
3202 const struct got_error *err = NULL;
3203 struct got_blob_object *blob = NULL;
3204 struct got_repository *thread_repo = NULL;
3205 struct got_object_id *obj_id = NULL;
3208 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3212 return got_error(GOT_ERR_NO_OBJ);
3214 err = got_object_get_type(&obj_type, repo, obj_id);
3218 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3219 err = got_error(GOT_ERR_OBJ_TYPE);
3223 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3226 blame->f = got_opentemp();
3227 if (blame->f == NULL) {
3228 err = got_error_from_errno("got_opentemp");
3231 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3232 &blame->line_offsets, blame->f, blob);
3236 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3237 if (blame->lines == NULL) {
3238 err = got_error_from_errno("calloc");
3242 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3246 blame->cb_args.view = view;
3247 blame->cb_args.lines = blame->lines;
3248 blame->cb_args.nlines = blame->nlines;
3249 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3250 if (blame->cb_args.commit_id == NULL) {
3251 err = got_error_from_errno("got_object_id_dup");
3254 blame->cb_args.quit = done;
3256 blame->thread_args.path = path;
3257 blame->thread_args.repo = thread_repo;
3258 blame->thread_args.cb_args = &blame->cb_args;
3259 blame->thread_args.complete = blame_complete;
3260 *blame_complete = 0;
3264 got_object_blob_close(blob);
3271 static const struct got_error *
3272 open_blame_view(struct tog_view *view, char *path,
3273 struct got_object_id *commit_id, struct got_reflist_head *refs,
3274 struct got_repository *repo)
3276 const struct got_error *err = NULL;
3277 struct tog_blame_view_state *s = &view->state.blame;
3279 SIMPLEQ_INIT(&s->blamed_commits);
3281 s->path = strdup(path);
3282 if (s->path == NULL)
3283 return got_error_from_errno("strdup");
3285 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3291 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3292 s->first_displayed_line = 1;
3293 s->last_displayed_line = view->nlines;
3294 s->selected_line = 1;
3295 s->blame_complete = 0;
3298 s->commit_id = commit_id;
3299 memset(&s->blame, 0, sizeof(s->blame));
3301 view->show = show_blame_view;
3302 view->input = input_blame_view;
3303 view->close = close_blame_view;
3304 view->search_start = search_start_blame_view;
3305 view->search_next = search_next_blame_view;
3307 return run_blame(&s->blame, view, &s->blame_complete,
3308 &s->first_displayed_line, &s->last_displayed_line,
3309 &s->selected_line, &s->done, &s->eof, s->path,
3310 s->blamed_commit->id, s->repo);
3313 static const struct got_error *
3314 close_blame_view(struct tog_view *view)
3316 const struct got_error *err = NULL;
3317 struct tog_blame_view_state *s = &view->state.blame;
3319 if (s->blame.thread)
3320 err = stop_blame(&s->blame);
3322 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3323 struct got_object_qid *blamed_commit;
3324 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3325 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3326 got_object_qid_free(blamed_commit);
3334 static const struct got_error *
3335 search_start_blame_view(struct tog_view *view)
3337 struct tog_blame_view_state *s = &view->state.blame;
3339 s->matched_line = 0;
3344 match_line(const char *line, regex_t *regex)
3346 regmatch_t regmatch;
3348 return regexec(regex, line, 1, ®match, 0) == 0;
3352 static const struct got_error *
3353 search_next_blame_view(struct tog_view *view)
3355 struct tog_blame_view_state *s = &view->state.blame;
3358 if (!view->searching) {
3359 view->search_next_done = 1;
3363 if (s->matched_line) {
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 lineno = s->matched_line + 1;
3367 lineno = s->matched_line - 1;
3369 if (view->searching == TOG_SEARCH_FORWARD)
3372 lineno = s->blame.nlines;
3380 if (lineno <= 0 || lineno > s->blame.nlines) {
3381 if (s->matched_line == 0) {
3382 view->search_next_done = 1;
3387 if (view->searching == TOG_SEARCH_FORWARD)
3390 lineno = s->blame.nlines;
3393 offset = s->blame.line_offsets[lineno - 1];
3394 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3396 return got_error_from_errno("fseeko");
3399 line = parse_next_line(s->blame.f, &len);
3400 if (line && match_line(line, &view->regex)) {
3401 view->search_next_done = 1;
3402 s->matched_line = lineno;
3407 if (view->searching == TOG_SEARCH_FORWARD)
3413 if (s->matched_line) {
3414 s->first_displayed_line = s->matched_line;
3415 s->selected_line = 1;
3421 static const struct got_error *
3422 show_blame_view(struct tog_view *view)
3424 const struct got_error *err = NULL;
3425 struct tog_blame_view_state *s = &view->state.blame;
3428 if (s->blame.thread == NULL) {
3429 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3430 &s->blame.thread_args);
3432 return got_error_set_errno(errcode, "pthread_create");
3435 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3436 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3437 s->selected_line, &s->first_displayed_line,
3438 &s->last_displayed_line, &s->eof, view->nlines);
3444 static const struct got_error *
3445 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3446 struct tog_view **focus_view, struct tog_view *view, int ch)
3448 const struct got_error *err = NULL, *thread_err = NULL;
3449 struct tog_view *diff_view;
3450 struct tog_blame_view_state *s = &view->state.blame;
3459 if (s->selected_line > 1)
3461 else if (s->selected_line == 1 &&
3462 s->first_displayed_line > 1)
3463 s->first_displayed_line--;
3466 if (s->first_displayed_line == 1) {
3467 s->selected_line = 1;
3470 if (s->first_displayed_line > view->nlines - 2)
3471 s->first_displayed_line -=
3474 s->first_displayed_line = 1;
3478 if (s->selected_line < view->nlines - 2 &&
3479 s->first_displayed_line +
3480 s->selected_line <= s->blame.nlines)
3482 else if (s->last_displayed_line <
3484 s->first_displayed_line++;
3488 struct got_object_id *id = NULL;
3489 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3490 s->first_displayed_line, s->selected_line);
3494 struct got_commit_object *commit;
3495 struct got_object_qid *pid;
3496 struct got_object_id *blob_id = NULL;
3498 err = got_object_open_as_commit(&commit,
3502 pid = SIMPLEQ_FIRST(
3503 got_object_commit_get_parent_ids(commit));
3505 got_object_commit_close(commit);
3508 /* Check if path history ends here. */
3509 err = got_object_id_by_path(&blob_id, s->repo,
3512 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3514 got_object_commit_close(commit);
3517 err = got_object_get_type(&obj_type, s->repo,
3520 /* Can't blame non-blob type objects. */
3521 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3522 got_object_commit_close(commit);
3525 err = got_object_qid_alloc(&s->blamed_commit,
3527 got_object_commit_close(commit);
3529 if (got_object_id_cmp(id,
3530 s->blamed_commit->id) == 0)
3532 err = got_object_qid_alloc(&s->blamed_commit,
3538 thread_err = stop_blame(&s->blame);
3542 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3543 s->blamed_commit, entry);
3544 err = run_blame(&s->blame, view, &s->blame_complete,
3545 &s->first_displayed_line, &s->last_displayed_line,
3546 &s->selected_line, &s->done, &s->eof,
3547 s->path, s->blamed_commit->id, s->repo);
3553 struct got_object_qid *first;
3554 first = SIMPLEQ_FIRST(&s->blamed_commits);
3555 if (!got_object_id_cmp(first->id, s->commit_id))
3558 thread_err = stop_blame(&s->blame);
3562 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3563 got_object_qid_free(s->blamed_commit);
3565 SIMPLEQ_FIRST(&s->blamed_commits);
3566 err = run_blame(&s->blame, view, &s->blame_complete,
3567 &s->first_displayed_line, &s->last_displayed_line,
3568 &s->selected_line, &s->done, &s->eof, s->path,
3569 s->blamed_commit->id, s->repo);
3576 struct got_object_id *id = NULL;
3577 struct got_object_qid *pid;
3578 struct got_commit_object *commit = NULL;
3579 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3580 s->first_displayed_line, s->selected_line);
3583 err = got_object_open_as_commit(&commit, s->repo, id);
3586 pid = SIMPLEQ_FIRST(
3587 got_object_commit_get_parent_ids(commit));
3588 if (view_is_parent_view(view))
3589 begin_x = view_split_begin_x(view->begin_x);
3590 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3591 if (diff_view == NULL) {
3592 got_object_commit_close(commit);
3593 err = got_error_from_errno("view_open");
3596 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3597 id, NULL, s->refs, s->repo);
3598 got_object_commit_close(commit);
3600 view_close(diff_view);
3603 if (view_is_parent_view(view)) {
3604 err = view_close_child(view);
3607 err = view_set_child(view, diff_view);
3609 view_close(diff_view);
3612 *focus_view = diff_view;
3613 view->child_focussed = 1;
3615 *new_view = diff_view;
3622 if (s->last_displayed_line >= s->blame.nlines &&
3623 s->selected_line >= MIN(s->blame.nlines,
3624 view->nlines - 2)) {
3627 if (s->last_displayed_line >= s->blame.nlines &&
3628 s->selected_line < view->nlines - 2) {
3629 s->selected_line = MIN(s->blame.nlines,
3633 if (s->last_displayed_line + view->nlines - 2
3635 s->first_displayed_line +=
3638 s->first_displayed_line =
3643 if (s->selected_line > view->nlines - 2) {
3644 s->selected_line = MIN(s->blame.nlines,
3651 return thread_err ? thread_err : err;
3654 static const struct got_error *
3655 cmd_blame(int argc, char *argv[])
3657 const struct got_error *error;
3658 struct got_repository *repo = NULL;
3659 struct got_reflist_head refs;
3660 struct got_worktree *worktree = NULL;
3661 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3662 struct got_object_id *commit_id = NULL;
3663 char *commit_id_str = NULL;
3665 struct tog_view *view;
3667 SIMPLEQ_INIT(&refs);
3670 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3675 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3678 commit_id_str = optarg;
3681 repo_path = realpath(optarg, NULL);
3682 if (repo_path == NULL)
3683 err(1, "-r option");
3699 cwd = getcwd(NULL, 0);
3701 error = got_error_from_errno("getcwd");
3704 if (repo_path == NULL) {
3705 error = got_worktree_open(&worktree, cwd);
3706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3712 strdup(got_worktree_get_repo_path(worktree));
3713 if (repo_path == NULL)
3714 error = got_error_from_errno("strdup");
3718 repo_path = strdup(cwd);
3719 if (repo_path == NULL) {
3720 error = got_error_from_errno("strdup");
3728 error = got_repo_open(&repo, repo_path);
3732 error = apply_unveil(got_repo_get_path(repo), NULL);
3737 const char *prefix = got_worktree_get_path_prefix(worktree);
3738 char *p, *worktree_subdir = cwd +
3739 strlen(got_worktree_get_root_path(worktree));
3740 if (asprintf(&p, "%s%s%s%s%s",
3741 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3742 worktree_subdir, worktree_subdir[0] ? "/" : "",
3744 error = got_error_from_errno("asprintf");
3747 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3750 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3755 if (commit_id_str == NULL) {
3756 struct got_reference *head_ref;
3757 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3760 error = got_ref_resolve(&commit_id, repo, head_ref);
3761 got_ref_close(head_ref);
3763 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3765 if (error->code != GOT_ERR_NOT_REF)
3767 error = got_repo_match_object_id_prefix(&commit_id,
3768 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3774 error = got_ref_list(&refs, repo);
3778 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3780 error = got_error_from_errno("view_open");
3783 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3786 error = view_loop(view);
3792 got_worktree_close(worktree);
3794 got_repo_close(repo);
3795 got_ref_list_free(&refs);
3799 static const struct got_error *
3800 draw_tree_entries(struct tog_view *view,
3801 struct got_tree_entry **first_displayed_entry,
3802 struct got_tree_entry **last_displayed_entry,
3803 struct got_tree_entry **selected_entry, int *ndisplayed,
3804 const char *label, int show_ids, const char *parent_path,
3805 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3807 const struct got_error *err = NULL;
3808 struct got_tree_entry *te;
3814 werase(view->window);
3819 err = format_line(&wline, &width, label, view->ncols);
3822 if (view_needs_focus_indication(view))
3823 wstandout(view->window);
3824 waddwstr(view->window, wline);
3825 if (view_needs_focus_indication(view))
3826 wstandend(view->window);
3829 if (width < view->ncols - 1)
3830 waddch(view->window, '\n');
3833 err = format_line(&wline, &width, parent_path, view->ncols);
3836 waddwstr(view->window, wline);
3839 if (width < view->ncols - 1)
3840 waddch(view->window, '\n');
3843 waddch(view->window, '\n');
3847 te = SIMPLEQ_FIRST(&entries->head);
3848 if (*first_displayed_entry == NULL) {
3849 if (selected == 0) {
3851 wstandout(view->window);
3852 *selected_entry = NULL;
3854 waddstr(view->window, " ..\n"); /* parent directory */
3855 if (selected == 0 && view->focussed)
3856 wstandend(view->window);
3863 while (te != *first_displayed_entry)
3864 te = SIMPLEQ_NEXT(te, entry);
3868 char *line = NULL, *id_str = NULL;
3869 const char *modestr = "";
3872 err = got_object_id_str(&id_str, te->id);
3874 return got_error_from_errno(
3875 "got_object_id_str");
3877 if (S_ISLNK(te->mode))
3879 else if (S_ISDIR(te->mode))
3881 else if (te->mode & S_IXUSR)
3883 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3884 te->name, modestr) == -1) {
3886 return got_error_from_errno("asprintf");
3889 err = format_line(&wline, &width, line, view->ncols);
3894 if (n == selected) {
3896 wstandout(view->window);
3897 *selected_entry = te;
3899 waddwstr(view->window, wline);
3900 if (width < view->ncols - 1)
3901 waddch(view->window, '\n');
3902 if (n == selected && view->focussed)
3903 wstandend(view->window);
3909 *last_displayed_entry = te;
3912 te = SIMPLEQ_NEXT(te, entry);
3919 tree_scroll_up(struct tog_view *view,
3920 struct got_tree_entry **first_displayed_entry, int maxscroll,
3921 const struct got_tree_entries *entries, int isroot)
3923 struct got_tree_entry *te, *prev;
3926 if (*first_displayed_entry == NULL)
3929 te = SIMPLEQ_FIRST(&entries->head);
3930 if (*first_displayed_entry == te) {
3932 *first_displayed_entry = NULL;
3936 /* XXX this is stupid... switch to TAILQ? */
3937 for (i = 0; i < maxscroll; i++) {
3938 while (te != *first_displayed_entry) {
3940 te = SIMPLEQ_NEXT(te, entry);
3942 *first_displayed_entry = prev;
3943 te = SIMPLEQ_FIRST(&entries->head);
3945 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3946 *first_displayed_entry = NULL;
3950 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3951 struct got_tree_entry *last_displayed_entry,
3952 const struct got_tree_entries *entries)
3954 struct got_tree_entry *next, *last;
3957 if (*first_displayed_entry)
3958 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3960 next = SIMPLEQ_FIRST(&entries->head);
3961 last = last_displayed_entry;
3962 while (next && last && n++ < maxscroll) {
3963 last = SIMPLEQ_NEXT(last, entry);
3965 *first_displayed_entry = next;
3966 next = SIMPLEQ_NEXT(next, entry);
3972 static const struct got_error *
3973 tree_entry_path(char **path, struct tog_parent_trees *parents,
3974 struct got_tree_entry *te)
3976 const struct got_error *err = NULL;
3977 struct tog_parent_tree *pt;
3978 size_t len = 2; /* for leading slash and NUL */
3980 TAILQ_FOREACH(pt, parents, entry)
3981 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3983 len += strlen(te->name);
3985 *path = calloc(1, len);
3987 return got_error_from_errno("calloc");
3990 pt = TAILQ_LAST(parents, tog_parent_trees);
3992 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3993 err = got_error(GOT_ERR_NO_SPACE);
3996 if (strlcat(*path, "/", len) >= len) {
3997 err = got_error(GOT_ERR_NO_SPACE);
4000 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4003 if (strlcat(*path, te->name, len) >= len) {
4004 err = got_error(GOT_ERR_NO_SPACE);
4016 static const struct got_error *
4017 blame_tree_entry(struct tog_view **new_view, int begin_x,
4018 struct got_tree_entry *te, struct tog_parent_trees *parents,
4019 struct got_object_id *commit_id, struct got_reflist_head *refs,
4020 struct got_repository *repo)
4022 const struct got_error *err = NULL;
4024 struct tog_view *blame_view;
4028 err = tree_entry_path(&path, parents, te);
4032 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4033 if (blame_view == NULL) {
4034 err = got_error_from_errno("view_open");
4038 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4040 view_close(blame_view);
4042 *new_view = blame_view;
4048 static const struct got_error *
4049 log_tree_entry(struct tog_view **new_view, int begin_x,
4050 struct got_tree_entry *te, struct tog_parent_trees *parents,
4051 struct got_object_id *commit_id, struct got_reflist_head *refs,
4052 struct got_repository *repo)
4054 struct tog_view *log_view;
4055 const struct got_error *err = NULL;
4060 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4061 if (log_view == NULL)
4062 return got_error_from_errno("view_open");
4064 err = tree_entry_path(&path, parents, te);
4068 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4070 view_close(log_view);
4072 *new_view = log_view;
4077 static const struct got_error *
4078 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4079 struct got_object_id *commit_id, struct got_reflist_head *refs,
4080 struct got_repository *repo)
4082 const struct got_error *err = NULL;
4083 char *commit_id_str = NULL;
4084 struct tog_tree_view_state *s = &view->state.tree;
4086 TAILQ_INIT(&s->parents);
4088 err = got_object_id_str(&commit_id_str, commit_id);
4092 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4093 err = got_error_from_errno("asprintf");
4097 s->root = s->tree = root;
4098 s->entries = got_object_tree_get_entries(root);
4099 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4100 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4101 s->commit_id = got_object_id_dup(commit_id);
4102 if (s->commit_id == NULL) {
4103 err = got_error_from_errno("got_object_id_dup");
4109 view->show = show_tree_view;
4110 view->input = input_tree_view;
4111 view->close = close_tree_view;
4112 view->search_start = search_start_tree_view;
4113 view->search_next = search_next_tree_view;
4115 free(commit_id_str);
4117 free(s->tree_label);
4118 s->tree_label = NULL;
4123 static const struct got_error *
4124 close_tree_view(struct tog_view *view)
4126 struct tog_tree_view_state *s = &view->state.tree;
4128 free(s->tree_label);
4129 s->tree_label = NULL;
4131 s->commit_id = NULL;
4132 while (!TAILQ_EMPTY(&s->parents)) {
4133 struct tog_parent_tree *parent;
4134 parent = TAILQ_FIRST(&s->parents);
4135 TAILQ_REMOVE(&s->parents, parent, entry);
4139 if (s->tree != s->root)
4140 got_object_tree_close(s->tree);
4141 got_object_tree_close(s->root);
4146 static const struct got_error *
4147 search_start_tree_view(struct tog_view *view)
4149 struct tog_tree_view_state *s = &view->state.tree;
4151 s->matched_entry = NULL;
4156 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4158 regmatch_t regmatch;
4160 return regexec(regex, te->name, 1, ®match, 0) == 0;
4163 static const struct got_error *
4164 search_next_tree_view(struct tog_view *view)
4166 struct tog_tree_view_state *s = &view->state.tree;
4167 struct got_tree_entry *entry = NULL, *te;
4169 if (!view->searching) {
4170 view->search_next_done = 1;
4174 if (s->matched_entry) {
4175 if (view->searching == TOG_SEARCH_FORWARD) {
4176 if (s->selected_entry)
4177 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4179 entry = SIMPLEQ_FIRST(&s->entries->head);
4182 if (s->selected_entry == NULL) {
4183 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4186 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4188 if (SIMPLEQ_NEXT(te, entry) ==
4195 if (view->searching == TOG_SEARCH_FORWARD)
4196 entry = SIMPLEQ_FIRST(&s->entries->head);
4198 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4204 if (entry == NULL) {
4205 if (s->matched_entry == NULL) {
4206 view->search_next_done = 1;
4209 if (view->searching == TOG_SEARCH_FORWARD)
4210 entry = SIMPLEQ_FIRST(&s->entries->head);
4212 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4217 if (match_tree_entry(entry, &view->regex)) {
4218 view->search_next_done = 1;
4219 s->matched_entry = entry;
4223 if (view->searching == TOG_SEARCH_FORWARD)
4224 entry = SIMPLEQ_NEXT(entry, entry);
4226 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4229 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4230 if (SIMPLEQ_NEXT(te, entry) == entry) {
4239 if (s->matched_entry) {
4240 s->first_displayed_entry = s->matched_entry;
4247 static const struct got_error *
4248 show_tree_view(struct tog_view *view)
4250 const struct got_error *err = NULL;
4251 struct tog_tree_view_state *s = &view->state.tree;
4254 err = tree_entry_path(&parent_path, &s->parents, NULL);
4258 err = draw_tree_entries(view, &s->first_displayed_entry,
4259 &s->last_displayed_entry, &s->selected_entry,
4260 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4261 s->entries, s->selected, view->nlines, s->tree == s->root);
4268 static const struct got_error *
4269 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4270 struct tog_view **focus_view, struct tog_view *view, int ch)
4272 const struct got_error *err = NULL;
4273 struct tog_tree_view_state *s = &view->state.tree;
4274 struct tog_view *log_view;
4275 int begin_x = 0, nscrolled;
4279 s->show_ids = !s->show_ids;
4282 if (!s->selected_entry)
4284 if (view_is_parent_view(view))
4285 begin_x = view_split_begin_x(view->begin_x);
4286 err = log_tree_entry(&log_view, begin_x,
4287 s->selected_entry, &s->parents,
4288 s->commit_id, s->refs, s->repo);
4289 if (view_is_parent_view(view)) {
4290 err = view_close_child(view);
4293 err = view_set_child(view, log_view);
4295 view_close(log_view);
4298 *focus_view = log_view;
4299 view->child_focussed = 1;
4301 *new_view = log_view;
4305 if (s->selected > 0) {
4307 if (s->selected == 0)
4310 if (s->selected > 0)
4312 tree_scroll_up(view, &s->first_displayed_entry, 1,
4313 s->entries, s->tree == s->root);
4316 tree_scroll_up(view, &s->first_displayed_entry,
4317 MAX(0, view->nlines - 4 - s->selected), s->entries,
4318 s->tree == s->root);
4320 if (SIMPLEQ_FIRST(&s->entries->head) ==
4321 s->first_displayed_entry && s->tree != s->root)
4322 s->first_displayed_entry = NULL;
4326 if (s->selected < s->ndisplayed - 1) {
4330 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4331 /* can't scroll any further */
4333 tree_scroll_down(&s->first_displayed_entry, 1,
4334 s->last_displayed_entry, s->entries);
4337 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4339 /* can't scroll any further; move cursor down */
4340 if (s->selected < s->ndisplayed - 1)
4341 s->selected = s->ndisplayed - 1;
4344 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4345 view->nlines, s->last_displayed_entry, s->entries);
4346 if (nscrolled < view->nlines) {
4348 struct got_tree_entry *te;
4349 te = s->first_displayed_entry;
4352 te = SIMPLEQ_NEXT(te, entry);
4354 s->selected = ndisplayed - 1;
4360 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4361 struct tog_parent_tree *parent;
4362 /* user selected '..' */
4363 if (s->tree == s->root)
4365 parent = TAILQ_FIRST(&s->parents);
4366 TAILQ_REMOVE(&s->parents, parent,
4368 got_object_tree_close(s->tree);
4369 s->tree = parent->tree;
4371 got_object_tree_get_entries(s->tree);
4372 s->first_displayed_entry =
4373 parent->first_displayed_entry;
4375 parent->selected_entry;
4376 s->selected = parent->selected;
4378 } else if (S_ISDIR(s->selected_entry->mode)) {
4379 struct got_tree_object *subtree;
4380 err = got_object_open_as_tree(&subtree,
4381 s->repo, s->selected_entry->id);
4384 err = tree_view_visit_subtree(subtree, s);
4386 got_object_tree_close(subtree);
4389 } else if (S_ISREG(s->selected_entry->mode)) {
4390 struct tog_view *blame_view;
4391 int begin_x = view_is_parent_view(view) ?
4392 view_split_begin_x(view->begin_x) : 0;
4394 err = blame_tree_entry(&blame_view, begin_x,
4395 s->selected_entry, &s->parents,
4396 s->commit_id, s->refs, s->repo);
4399 if (view_is_parent_view(view)) {
4400 err = view_close_child(view);
4403 err = view_set_child(view, blame_view);
4405 view_close(blame_view);
4408 *focus_view = blame_view;
4409 view->child_focussed = 1;
4411 *new_view = blame_view;
4415 if (s->selected > view->nlines)
4416 s->selected = s->ndisplayed - 1;
4429 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4434 static const struct got_error *
4435 cmd_tree(int argc, char *argv[])
4437 const struct got_error *error;
4438 struct got_repository *repo = NULL;
4439 struct got_reflist_head refs;
4440 char *repo_path = NULL;
4441 struct got_object_id *commit_id = NULL;
4442 char *commit_id_arg = NULL;
4443 struct got_commit_object *commit = NULL;
4444 struct got_tree_object *tree = NULL;
4446 struct tog_view *view;
4448 SIMPLEQ_INIT(&refs);
4451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4456 while ((ch = getopt(argc, argv, "c:")) != -1) {
4459 commit_id_arg = optarg;
4471 struct got_worktree *worktree;
4472 char *cwd = getcwd(NULL, 0);
4474 return got_error_from_errno("getcwd");
4475 error = got_worktree_open(&worktree, cwd);
4476 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4481 strdup(got_worktree_get_repo_path(worktree));
4482 got_worktree_close(worktree);
4485 if (repo_path == NULL) {
4486 error = got_error_from_errno("strdup");
4489 } else if (argc == 1) {
4490 repo_path = realpath(argv[0], NULL);
4491 if (repo_path == NULL)
4492 return got_error_from_errno2("realpath", argv[0]);
4498 error = got_repo_open(&repo, repo_path);
4502 error = apply_unveil(got_repo_get_path(repo), NULL);
4506 if (commit_id_arg == NULL)
4507 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4509 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4511 if (error->code != GOT_ERR_NOT_REF)
4513 error = got_repo_match_object_id_prefix(&commit_id,
4514 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4520 error = got_object_open_as_commit(&commit, repo, commit_id);
4524 error = got_object_open_as_tree(&tree, repo,
4525 got_object_commit_get_tree_id(commit));
4529 error = got_ref_list(&refs, repo);
4533 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4535 error = got_error_from_errno("view_open");
4538 error = open_tree_view(view, tree, commit_id, &refs, repo);
4541 error = view_loop(view);
4546 got_object_commit_close(commit);
4548 got_object_tree_close(tree);
4550 got_repo_close(repo);
4551 got_ref_list_free(&refs);
4560 fprintf(stderr, "commands:");
4561 for (i = 0; i < nitems(tog_commands); i++) {
4562 struct tog_cmd *cmd = &tog_commands[i];
4563 fprintf(stderr, " %s", cmd->name);
4565 fputc('\n', stderr);
4571 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4579 make_argv(const char *arg0, const char *arg1)
4582 int argc = (arg1 == NULL ? 1 : 2);
4584 argv = calloc(argc, sizeof(char *));
4587 argv[0] = strdup(arg0);
4588 if (argv[0] == NULL)
4591 argv[1] = strdup(arg1);
4592 if (argv[1] == NULL)
4600 main(int argc, char *argv[])
4602 const struct got_error *error = NULL;
4603 struct tog_cmd *cmd = NULL;
4604 int ch, hflag = 0, Vflag = 0;
4605 char **cmd_argv = NULL;
4607 setlocale(LC_CTYPE, "");
4609 while ((ch = getopt(argc, argv, "hV")) != -1) {
4629 got_version_print_str();
4636 /* Build an argument vector which runs a default command. */
4637 cmd = &tog_commands[0];
4638 cmd_argv = make_argv(cmd->name, NULL);
4643 /* Did the user specific a command? */
4644 for (i = 0; i < nitems(tog_commands); i++) {
4645 if (strncmp(tog_commands[i].name, argv[0],
4646 strlen(argv[0])) == 0) {
4647 cmd = &tog_commands[i];
4653 fprintf(stderr, "%s: unknown command '%s'\n",
4654 getprogname(), argv[0]);
4663 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4668 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);