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_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
52 #include "got_worktree.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #define CTRL(x) ((x) & 0x1f)
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
74 __dead static void usage(int);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "diff", cmd_diff, usage_diff },
88 { "blame", cmd_blame, usage_blame },
89 { "tree", cmd_tree, usage_tree },
99 #define TOG_EOF_STRING "(END)"
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
110 struct commit_queue_head head;
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
116 int first_displayed_line;
117 int last_displayed_line;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
151 const char *head_ref_name;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
157 struct tog_log_thread_args thread_args;
158 struct commit_queue_entry *matched_entry;
159 struct commit_queue_entry *search_entry;
162 struct tog_blame_cb_args {
163 struct tog_blame_line *lines; /* one per line */
166 struct tog_view *view;
167 struct got_object_id *commit_id;
171 struct tog_blame_thread_args {
173 struct got_repository *repo;
174 struct tog_blame_cb_args *cb_args;
181 struct tog_blame_line *lines;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
207 struct tog_parent_tree {
208 TAILQ_ENTRY(tog_parent_tree) entry;
209 struct got_tree_object *tree;
210 struct got_tree_entry *first_displayed_entry;
211 struct got_tree_entry *selected_entry;
215 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
217 struct tog_tree_view_state {
219 struct got_tree_object *root;
220 struct got_tree_object *tree;
221 const struct got_tree_entries *entries;
222 struct got_tree_entry *first_displayed_entry;
223 struct got_tree_entry *last_displayed_entry;
224 struct got_tree_entry *selected_entry;
225 int ndisplayed, selected, show_ids;
226 struct tog_parent_trees parents;
227 struct got_object_id *commit_id;
228 struct got_repository *repo;
229 struct got_reflist_head *refs;
230 struct got_tree_entry *matched_entry;
234 * We implement two types of views: parent views and child views.
236 * The 'Tab' key switches between a parent view and its child view.
237 * Child views are shown side-by-side to their parent view, provided
238 * there is enough screen estate.
240 * When a new view is opened from within a parent view, this new view
241 * becomes a child view of the parent view, replacing any existing child.
243 * When a new view is opened from within a child view, this new view
244 * becomes a parent view which will obscure the views below until the
245 * user quits the new parent view by typing 'q'.
247 * This list of views contains parent views only.
248 * Child views are only pointed to by their parent view.
250 TAILQ_HEAD(tog_view_list_head, tog_view);
253 TAILQ_ENTRY(tog_view) entry;
256 int nlines, ncols, begin_y, begin_x;
257 int lines, cols; /* copies of LINES and COLS */
259 struct tog_view *parent;
260 struct tog_view *child;
263 /* type-specific state */
264 enum tog_view_type type;
266 struct tog_diff_view_state diff;
267 struct tog_log_view_state log;
268 struct tog_blame_view_state blame;
269 struct tog_tree_view_state tree;
272 const struct got_error *(*show)(struct tog_view *);
273 const struct got_error *(*input)(struct tog_view **,
274 struct tog_view **, struct tog_view**, struct tog_view *, int);
275 const struct got_error *(*close)(struct tog_view *);
277 const struct got_error *(*search_start)(struct tog_view *);
278 const struct got_error *(*search_next)(struct tog_view *);
280 #define TOG_SEARCH_FORWARD 1
281 #define TOG_SEARCH_BACKWARD 2
282 int search_next_done;
286 static const struct got_error *open_diff_view(struct tog_view *,
287 struct got_object_id *, struct got_object_id *, struct tog_view *,
288 struct got_reflist_head *, struct got_repository *);
289 static const struct got_error *show_diff_view(struct tog_view *);
290 static const struct got_error *input_diff_view(struct tog_view **,
291 struct tog_view **, struct tog_view **, struct tog_view *, int);
292 static const struct got_error* close_diff_view(struct tog_view *);
294 static const struct got_error *open_log_view(struct tog_view *,
295 struct got_object_id *, struct got_reflist_head *,
296 struct got_repository *, const char *, const char *, int);
297 static const struct got_error * show_log_view(struct tog_view *);
298 static const struct got_error *input_log_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_log_view(struct tog_view *);
301 static const struct got_error *search_start_log_view(struct tog_view *);
302 static const struct got_error *search_next_log_view(struct tog_view *);
304 static const struct got_error *open_blame_view(struct tog_view *, char *,
305 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 static const struct got_error *show_blame_view(struct tog_view *);
307 static const struct got_error *input_blame_view(struct tog_view **,
308 struct tog_view **, struct tog_view **, struct tog_view *, int);
309 static const struct got_error *close_blame_view(struct tog_view *);
310 static const struct got_error *search_start_blame_view(struct tog_view *);
311 static const struct got_error *search_next_blame_view(struct tog_view *);
313 static const struct got_error *open_tree_view(struct tog_view *,
314 struct got_tree_object *, struct got_object_id *,
315 struct got_reflist_head *, struct got_repository *);
316 static const struct got_error *show_tree_view(struct tog_view *);
317 static const struct got_error *input_tree_view(struct tog_view **,
318 struct tog_view **, struct tog_view **, struct tog_view *, int);
319 static const struct got_error *close_tree_view(struct tog_view *);
320 static const struct got_error *search_start_tree_view(struct tog_view *);
321 static const struct got_error *search_next_tree_view(struct tog_view *);
323 static volatile sig_atomic_t tog_sigwinch_received;
326 tog_sigwinch(int signo)
328 tog_sigwinch_received = 1;
331 static const struct got_error *
332 view_close(struct tog_view *view)
334 const struct got_error *err = NULL;
337 view_close(view->child);
341 err = view->close(view);
343 del_panel(view->panel);
345 delwin(view->window);
350 static struct tog_view *
351 view_open(int nlines, int ncols, int begin_y, int begin_x,
352 enum tog_view_type type)
354 struct tog_view *view = calloc(1, sizeof(*view));
362 view->nlines = nlines ? nlines : LINES - begin_y;
363 view->ncols = ncols ? ncols : COLS - begin_x;
364 view->begin_y = begin_y;
365 view->begin_x = begin_x;
366 view->window = newwin(nlines, ncols, begin_y, begin_x);
367 if (view->window == NULL) {
371 view->panel = new_panel(view->window);
372 if (view->panel == NULL ||
373 set_panel_userptr(view->panel, view) != OK) {
378 keypad(view->window, TRUE);
383 view_split_begin_x(int begin_x)
385 if (begin_x > 0 || COLS < 120)
387 return (COLS - MAX(COLS / 2, 80));
390 static const struct got_error *view_resize(struct tog_view *);
392 static const struct got_error *
393 view_splitscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
398 view->begin_x = view_split_begin_x(0);
399 view->nlines = LINES;
400 view->ncols = COLS - view->begin_x;
403 err = view_resize(view);
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno("mvwin");
413 static const struct got_error *
414 view_fullscreen(struct tog_view *view)
416 const struct got_error *err = NULL;
420 view->nlines = LINES;
424 err = view_resize(view);
428 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 return got_error_from_errno("mvwin");
435 view_is_parent_view(struct tog_view *view)
437 return view->parent == NULL;
440 static const struct got_error *
441 view_resize(struct tog_view *view)
445 if (view->lines > LINES)
446 nlines = view->nlines - (view->lines - LINES);
448 nlines = view->nlines + (LINES - view->lines);
450 if (view->cols > COLS)
451 ncols = view->ncols - (view->cols - COLS);
453 ncols = view->ncols + (COLS - view->cols);
455 if (wresize(view->window, nlines, ncols) == ERR)
456 return got_error_from_errno("wresize");
457 if (replace_panel(view->panel, view->window) == ERR)
458 return got_error_from_errno("replace_panel");
459 wclear(view->window);
461 view->nlines = nlines;
467 view->child->begin_x = view_split_begin_x(view->begin_x);
468 if (view->child->begin_x == 0) {
469 view_fullscreen(view->child);
470 if (view->child->focussed)
471 show_panel(view->child->panel);
473 show_panel(view->panel);
475 view_splitscreen(view->child);
476 show_panel(view->child->panel);
483 static const struct got_error *
484 view_close_child(struct tog_view *view)
486 const struct got_error *err = NULL;
488 if (view->child == NULL)
491 err = view_close(view->child);
496 static const struct got_error *
497 view_set_child(struct tog_view *view, struct tog_view *child)
499 const struct got_error *err = NULL;
502 child->parent = view;
507 view_is_splitscreen(struct tog_view *view)
509 return view->begin_x > 0;
518 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
519 cols = 80; /* Default */
525 resize_term(lines, cols);
528 static const struct got_error *
529 view_search_start(struct tog_view *view)
531 const struct got_error *err = NULL;
535 if (view->nlines < 1)
538 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
540 wclrtoeol(view->window);
544 ret = wgetnstr(view->window, pattern, sizeof(pattern));
550 if (view->searching) {
551 regfree(&view->regex);
555 if (regcomp(&view->regex, pattern,
556 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
557 err = view->search_start(view);
559 regfree(&view->regex);
562 view->searching = TOG_SEARCH_FORWARD;
563 view->search_next_done = 0;
564 view->search_next(view);
570 static const struct got_error *
571 view_input(struct tog_view **new, struct tog_view **dead,
572 struct tog_view **focus, int *done, struct tog_view *view,
573 struct tog_view_list_head *views)
575 const struct got_error *err = NULL;
583 if (view->searching && !view->search_next_done) {
584 errcode = pthread_mutex_unlock(&tog_mutex);
586 return got_error_set_errno(errcode,
587 "pthread_mutex_unlock");
589 errcode = pthread_mutex_lock(&tog_mutex);
591 return got_error_set_errno(errcode,
592 "pthread_mutex_lock");
593 view->search_next(view);
597 nodelay(stdscr, FALSE);
598 /* Allow threads to make progress while we are waiting for input. */
599 errcode = pthread_mutex_unlock(&tog_mutex);
601 return got_error_set_errno(errcode, "pthread_mutex_unlock");
602 ch = wgetch(view->window);
603 errcode = pthread_mutex_lock(&tog_mutex);
605 return got_error_set_errno(errcode, "pthread_mutex_lock");
606 nodelay(stdscr, TRUE);
608 if (tog_sigwinch_received) {
610 tog_sigwinch_received = 0;
611 TAILQ_FOREACH(v, views, entry) {
612 err = view_resize(v);
615 err = v->input(new, dead, focus, v, KEY_RESIZE);
626 *focus = view->child;
627 view->child_focussed = 1;
628 } else if (view->parent) {
629 *focus = view->parent;
630 view->parent->child_focussed = 0;
634 err = view->input(new, dead, focus, view, ch);
641 if (view_is_parent_view(view)) {
642 if (view->child == NULL)
644 if (view_is_splitscreen(view->child)) {
645 *focus = view->child;
646 view->child_focussed = 1;
647 err = view_fullscreen(view->child);
649 err = view_splitscreen(view->child);
652 err = view->child->input(new, dead, focus,
653 view->child, KEY_RESIZE);
655 if (view_is_splitscreen(view)) {
657 view->parent->child_focussed = 1;
658 err = view_fullscreen(view);
660 err = view_splitscreen(view);
664 err = view->input(new, dead, focus, view,
671 if (view->search_start)
672 view_search_start(view);
674 err = view->input(new, dead, focus, view, ch);
678 if (view->search_next && view->searching) {
679 view->searching = (ch == 'n' ?
680 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
681 view->search_next_done = 0;
682 view->search_next(view);
684 err = view->input(new, dead, focus, view, ch);
687 err = view->input(new, dead, focus, view, ch);
695 view_vborder(struct tog_view *view)
698 struct tog_view *view_above;
701 return view_vborder(view->parent);
703 panel = panel_above(view->panel);
707 view_above = panel_userptr(panel);
708 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
709 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
713 view_needs_focus_indication(struct tog_view *view)
715 if (view_is_parent_view(view)) {
716 if (view->child == NULL || view->child_focussed)
718 if (!view_is_splitscreen(view->child))
720 } else if (!view_is_splitscreen(view))
723 return view->focussed;
726 static const struct got_error *
727 view_loop(struct tog_view *view)
729 const struct got_error *err = NULL;
730 struct tog_view_list_head views;
731 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
732 int fast_refresh = 10;
733 int done = 0, errcode;
735 errcode = pthread_mutex_lock(&tog_mutex);
737 return got_error_set_errno(errcode, "pthread_mutex_lock");
740 TAILQ_INSERT_HEAD(&views, view, entry);
744 err = view->show(view);
749 while (!TAILQ_EMPTY(&views) && !done) {
750 /* Refresh fast during initialization, then become slower. */
751 if (fast_refresh && fast_refresh-- == 0)
752 halfdelay(10); /* switch to once per second */
754 err = view_input(&new_view, &dead_view, &focus_view, &done,
759 struct tog_view *prev = NULL;
761 if (view_is_parent_view(dead_view))
762 prev = TAILQ_PREV(dead_view,
763 tog_view_list_head, entry);
764 else if (view->parent != dead_view)
767 if (dead_view->parent)
768 dead_view->parent->child = NULL;
770 TAILQ_REMOVE(&views, dead_view, entry);
772 err = view_close(dead_view);
773 if (err || (dead_view == main_view && new_view == NULL))
776 if (view == dead_view) {
781 else if (!TAILQ_EMPTY(&views))
782 view = TAILQ_LAST(&views,
787 if (view->child && view->child_focussed)
788 focus_view = view->child;
795 struct tog_view *v, *t;
796 /* Only allow one parent view per type. */
797 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
798 if (v->type != new_view->type)
800 TAILQ_REMOVE(&views, v, entry);
806 TAILQ_INSERT_TAIL(&views, new_view, entry);
808 if (focus_view == NULL)
809 focus_view = new_view;
812 show_panel(focus_view->panel);
815 focus_view->focussed = 1;
818 show_panel(new_view->panel);
819 if (view->child && view_is_splitscreen(view->child))
820 show_panel(view->child->panel);
823 if (focus_view == NULL) {
825 show_panel(view->panel);
826 if (view->child && view_is_splitscreen(view->child))
827 show_panel(view->child->panel);
831 err = view->parent->show(view->parent);
835 err = view->show(view);
839 err = view->child->show(view->child);
848 while (!TAILQ_EMPTY(&views)) {
849 view = TAILQ_FIRST(&views);
850 TAILQ_REMOVE(&views, view, entry);
854 errcode = pthread_mutex_unlock(&tog_mutex);
856 return got_error_set_errno(errcode, "pthread_mutex_unlock");
866 "usage: %s log [-c commit] [-r repository-path] [path]\n",
871 /* Create newly allocated wide-character string equivalent to a byte string. */
872 static const struct got_error *
873 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
876 const struct got_error *err = NULL;
879 *wlen = mbstowcs(NULL, s, 0);
880 if (*wlen == (size_t)-1) {
883 return got_error_from_errno("mbstowcs");
885 /* byte string invalid in current encoding; try to "fix" it */
886 err = got_mbsavis(&vis, &vislen, s);
889 *wlen = mbstowcs(NULL, vis, 0);
890 if (*wlen == (size_t)-1) {
891 err = got_error_from_errno("mbstowcs"); /* give up */
896 *ws = calloc(*wlen + 1, sizeof(*ws));
898 err = got_error_from_errno("calloc");
902 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
903 err = got_error_from_errno("mbstowcs");
914 /* Format a line for display, ensuring that it won't overflow a width limit. */
915 static const struct got_error *
916 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
918 const struct got_error *err = NULL;
920 wchar_t *wline = NULL;
927 err = mbs2ws(&wline, &wlen, line);
932 while (i < wlen && cols < wlimit) {
933 int width = wcwidth(wline[i]);
940 if (cols + width <= wlimit)
945 if (wline[i] == L'\t')
946 cols += TABSIZE - ((cols + 1) % TABSIZE);
950 err = got_error_from_errno("wcwidth");
965 static const struct got_error*
966 build_refs_str(char **refs_str, struct got_reflist_head *refs,
967 struct got_object_id *id)
969 static const struct got_error *err = NULL;
970 struct got_reflist_entry *re;
976 SIMPLEQ_FOREACH(re, refs, entry) {
977 if (got_object_id_cmp(re->id, id) != 0)
979 name = got_ref_get_name(re->ref);
980 if (strcmp(name, GOT_REF_HEAD) == 0)
982 if (strncmp(name, "refs/", 5) == 0)
984 if (strncmp(name, "got/", 4) == 0)
986 if (strncmp(name, "heads/", 6) == 0)
988 if (strncmp(name, "remotes/", 8) == 0)
991 if (asprintf(refs_str, "%s%s%s", s ? s : "",
992 s ? ", " : "", name) == -1) {
993 err = got_error_from_errno("asprintf");
1004 static const struct got_error *
1005 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1007 char *smallerthan, *at;
1009 smallerthan = strchr(author, '<');
1010 if (smallerthan && smallerthan[1] != '\0')
1011 author = smallerthan + 1;
1012 at = strchr(author, '@');
1015 return format_line(wauthor, author_width, author, limit);
1018 static const struct got_error *
1019 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1020 struct got_object_id *id, struct got_reflist_head *refs,
1021 int author_display_cols)
1023 const struct got_error *err = NULL;
1024 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1025 char *logmsg0 = NULL, *logmsg = NULL;
1026 char *author = NULL;
1027 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1028 int author_width, logmsg_width;
1029 char *newline, *line = NULL;
1031 static const size_t date_display_cols = 9;
1032 const int avail = view->ncols;
1034 time_t committer_time;
1036 committer_time = got_object_commit_get_committer_time(commit);
1037 if (localtime_r(&committer_time, &tm) == NULL)
1038 return got_error_from_errno("localtime_r");
1039 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1041 return got_error(GOT_ERR_NO_SPACE);
1043 if (avail < date_display_cols)
1044 limit = MIN(sizeof(datebuf) - 1, avail);
1046 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1047 waddnstr(view->window, datebuf, limit);
1052 author = strdup(got_object_commit_get_author(commit));
1053 if (author == NULL) {
1054 err = got_error_from_errno("strdup");
1057 err = format_author(&wauthor, &author_width, author, avail - col);
1060 waddwstr(view->window, wauthor);
1061 col += author_width;
1062 while (col <= avail && author_width < author_display_cols + 2) {
1063 waddch(view->window, ' ');
1070 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1071 if (logmsg0 == NULL) {
1072 err = got_error_from_errno("strdup");
1076 while (*logmsg == '\n')
1078 newline = strchr(logmsg, '\n');
1081 limit = avail - col;
1082 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1085 waddwstr(view->window, wlogmsg);
1086 col += logmsg_width;
1087 while (col <= avail) {
1088 waddch(view->window, ' ');
1100 static struct commit_queue_entry *
1101 alloc_commit_queue_entry(struct got_commit_object *commit,
1102 struct got_object_id *id)
1104 struct commit_queue_entry *entry;
1106 entry = calloc(1, sizeof(*entry));
1111 entry->commit = commit;
1116 pop_commit(struct commit_queue *commits)
1118 struct commit_queue_entry *entry;
1120 entry = TAILQ_FIRST(&commits->head);
1121 TAILQ_REMOVE(&commits->head, entry, entry);
1122 got_object_commit_close(entry->commit);
1123 commits->ncommits--;
1124 /* Don't free entry->id! It is owned by the commit graph. */
1129 free_commits(struct commit_queue *commits)
1131 while (!TAILQ_EMPTY(&commits->head))
1132 pop_commit(commits);
1135 static const struct got_error *
1136 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1137 int minqueue, struct got_repository *repo, const char *path)
1139 const struct got_error *err = NULL;
1143 * We keep all commits open throughout the lifetime of the log
1144 * view in order to avoid having to re-fetch commits from disk
1145 * while updating the display.
1147 while (nqueued < minqueue) {
1148 struct got_object_id *id;
1149 struct got_commit_object *commit;
1150 struct commit_queue_entry *entry;
1153 err = got_commit_graph_iter_next(&id, graph);
1155 if (err->code != GOT_ERR_ITER_NEED_MORE)
1157 err = got_commit_graph_fetch_commits(graph,
1167 err = got_object_open_as_commit(&commit, repo, id);
1170 entry = alloc_commit_queue_entry(commit, id);
1171 if (entry == NULL) {
1172 err = got_error_from_errno("alloc_commit_queue_entry");
1176 errcode = pthread_mutex_lock(&tog_mutex);
1178 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1182 entry->idx = commits->ncommits;
1183 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1185 commits->ncommits++;
1187 errcode = pthread_mutex_unlock(&tog_mutex);
1188 if (errcode && err == NULL)
1189 err = got_error_set_errno(errcode,
1190 "pthread_mutex_unlock");
1196 static const struct got_error *
1197 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1198 struct got_repository *repo)
1200 const struct got_error *err = NULL;
1201 struct got_reference *head_ref;
1205 err = got_ref_open(&head_ref, repo, branch_name, 0);
1209 err = got_ref_resolve(head_id, repo, head_ref);
1210 got_ref_close(head_ref);
1219 static const struct got_error *
1220 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1221 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1222 struct commit_queue *commits, int selected_idx, int limit,
1223 struct got_reflist_head *refs, const char *path, int commits_needed)
1225 const struct got_error *err = NULL;
1226 struct commit_queue_entry *entry;
1228 int ncommits, author_cols = 10;
1229 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1230 char *refs_str = NULL;
1236 if (ncommits == selected_idx) {
1240 entry = TAILQ_NEXT(entry, entry);
1244 if (*selected && !(view->searching && view->search_next_done == 0)) {
1245 err = got_object_id_str(&id_str, (*selected)->id);
1249 err = build_refs_str(&refs_str, refs, (*selected)->id);
1255 if (commits_needed == 0)
1256 halfdelay(10); /* disable fast refresh */
1258 if (asprintf(&ncommits_str, " [%d/%d] %s",
1259 entry ? entry->idx + 1 : 0, commits->ncommits,
1260 commits_needed > 0 ?
1261 (view->searching && view->search_next_done == 0
1262 ? "searching..." : "loading... ") :
1263 (refs_str ? refs_str : "")) == -1) {
1264 err = got_error_from_errno("asprintf");
1268 if (path && strcmp(path, "/") != 0) {
1269 if (asprintf(&header, "commit %s %s%s",
1270 id_str ? id_str : "........................................",
1271 path, ncommits_str) == -1) {
1272 err = got_error_from_errno("asprintf");
1276 } else if (asprintf(&header, "commit %s%s",
1277 id_str ? id_str : "........................................",
1278 ncommits_str) == -1) {
1279 err = got_error_from_errno("asprintf");
1283 err = format_line(&wline, &width, header, view->ncols);
1287 werase(view->window);
1289 if (view_needs_focus_indication(view))
1290 wstandout(view->window);
1291 waddwstr(view->window, wline);
1292 while (width < view->ncols) {
1293 waddch(view->window, ' ');
1296 if (view_needs_focus_indication(view))
1297 wstandend(view->window);
1302 /* Grow author column size if necessary. */
1309 if (ncommits >= limit - 1)
1311 author = strdup(got_object_commit_get_author(entry->commit));
1312 if (author == NULL) {
1313 err = got_error_from_errno("strdup");
1316 err = format_author(&wauthor, &width, author, COLS);
1317 if (author_cols < width)
1318 author_cols = width;
1321 entry = TAILQ_NEXT(entry, entry);
1328 if (ncommits >= limit - 1)
1330 if (ncommits == selected_idx)
1331 wstandout(view->window);
1332 err = draw_commit(view, entry->commit, entry->id, refs,
1334 if (ncommits == selected_idx)
1335 wstandend(view->window);
1340 entry = TAILQ_NEXT(entry, entry);
1353 scroll_up(struct tog_view *view,
1354 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1355 struct commit_queue *commits)
1357 struct commit_queue_entry *entry;
1360 entry = TAILQ_FIRST(&commits->head);
1361 if (*first_displayed_entry == entry)
1364 entry = *first_displayed_entry;
1365 while (entry && nscrolled < maxscroll) {
1366 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1368 *first_displayed_entry = entry;
1374 static const struct got_error *
1375 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1376 pthread_cond_t *need_commits)
1381 halfdelay(1); /* fast refresh while loading commits */
1383 while (*commits_needed > 0) {
1387 /* Wake the log thread. */
1388 errcode = pthread_cond_signal(need_commits);
1390 return got_error_set_errno(errcode,
1391 "pthread_cond_signal");
1392 errcode = pthread_mutex_unlock(&tog_mutex);
1394 return got_error_set_errno(errcode,
1395 "pthread_mutex_unlock");
1397 errcode = pthread_mutex_lock(&tog_mutex);
1399 return got_error_set_errno(errcode,
1400 "pthread_mutex_lock");
1402 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1404 * Thread is not done yet; lose a key press
1405 * and let the user retry... this way the GUI
1406 * remains interactive while logging deep paths
1407 * with few commits in history.
1416 static const struct got_error *
1417 scroll_down(struct tog_view *view,
1418 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1419 struct commit_queue_entry **last_displayed_entry,
1420 struct commit_queue *commits, int *log_complete, int *commits_needed,
1421 pthread_cond_t *need_commits)
1423 const struct got_error *err = NULL;
1424 struct commit_queue_entry *pentry;
1427 if (*last_displayed_entry == NULL)
1430 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1431 if (pentry == NULL && !*log_complete) {
1433 * Ask the log thread for required amount of commits
1434 * plus some amount of pre-fetching.
1436 (*commits_needed) += maxscroll + 20;
1437 err = trigger_log_thread(0, commits_needed, log_complete,
1444 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1448 *last_displayed_entry = pentry;
1450 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1453 *first_displayed_entry = pentry;
1454 } while (++nscrolled < maxscroll);
1459 static const struct got_error *
1460 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1461 struct got_commit_object *commit, struct got_object_id *commit_id,
1462 struct tog_view *log_view, struct got_reflist_head *refs,
1463 struct got_repository *repo)
1465 const struct got_error *err;
1466 struct got_object_qid *parent_id;
1467 struct tog_view *diff_view;
1469 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1470 if (diff_view == NULL)
1471 return got_error_from_errno("view_open");
1473 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1474 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1475 commit_id, log_view, refs, repo);
1477 *new_view = diff_view;
1481 static const struct got_error *
1482 tree_view_visit_subtree(struct got_tree_object *subtree,
1483 struct tog_tree_view_state *s)
1485 struct tog_parent_tree *parent;
1487 parent = calloc(1, sizeof(*parent));
1489 return got_error_from_errno("calloc");
1491 parent->tree = s->tree;
1492 parent->first_displayed_entry = s->first_displayed_entry;
1493 parent->selected_entry = s->selected_entry;
1494 parent->selected = s->selected;
1495 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1497 s->entries = got_object_tree_get_entries(s->tree);
1499 s->first_displayed_entry = NULL;
1504 static const struct got_error *
1505 browse_commit_tree(struct tog_view **new_view, int begin_x,
1506 struct commit_queue_entry *entry, const char *path,
1507 struct got_reflist_head *refs, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 struct got_tree_object *tree;
1511 struct got_tree_entry *te;
1512 struct tog_tree_view_state *s;
1513 struct tog_view *tree_view;
1514 char *slash, *subpath = NULL;
1517 err = got_object_open_as_tree(&tree, repo,
1518 got_object_commit_get_tree_id(entry->commit));
1522 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1523 if (tree_view == NULL)
1524 return got_error_from_errno("view_open");
1526 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1528 got_object_tree_close(tree);
1531 s = &tree_view->state.tree;
1533 *new_view = tree_view;
1535 /* Walk the path and open corresponding tree objects. */
1538 struct got_object_id *tree_id;
1543 /* Ensure the correct subtree entry is selected. */
1544 slash = strchr(p, '/');
1546 slash = strchr(p, '\0');
1547 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1548 if (strncmp(p, te->name, slash - p) == 0) {
1549 s->selected_entry = te;
1554 if (s->selected_entry == NULL) {
1555 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1558 if (s->tree != s->root)
1559 s->selected++; /* skip '..' */
1561 if (!S_ISDIR(s->selected_entry->mode)) {
1562 /* Jump to this file's entry. */
1563 s->first_displayed_entry = s->selected_entry;
1568 slash = strchr(p, '/');
1570 subpath = strndup(path, slash - path);
1572 subpath = strdup(path);
1573 if (subpath == NULL) {
1574 err = got_error_from_errno("strdup");
1578 err = got_object_id_by_path(&tree_id, repo, entry->id,
1583 err = got_object_open_as_tree(&tree, repo, tree_id);
1588 err = tree_view_visit_subtree(tree, s);
1590 got_object_tree_close(tree);
1605 log_thread(void *arg)
1607 const struct got_error *err = NULL;
1609 struct tog_log_thread_args *a = arg;
1612 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1616 while (!done && !err) {
1617 err = queue_commits(a->graph, a->commits, 1, a->repo,
1620 if (err->code != GOT_ERR_ITER_COMPLETED)
1624 } else if (a->commits_needed > 0)
1625 a->commits_needed--;
1627 errcode = pthread_mutex_lock(&tog_mutex);
1629 err = got_error_set_errno(errcode,
1630 "pthread_mutex_lock");
1632 } else if (*a->quit)
1634 else if (*a->first_displayed_entry == NULL) {
1635 *a->first_displayed_entry =
1636 TAILQ_FIRST(&a->commits->head);
1637 *a->selected_entry = *a->first_displayed_entry;
1641 a->commits_needed = 0;
1642 else if (a->commits_needed == 0) {
1643 errcode = pthread_cond_wait(&a->need_commits,
1646 err = got_error_set_errno(errcode,
1647 "pthread_cond_wait");
1650 errcode = pthread_mutex_unlock(&tog_mutex);
1651 if (errcode && err == NULL)
1652 err = got_error_set_errno(errcode,
1653 "pthread_mutex_unlock");
1655 a->log_complete = 1;
1659 static const struct got_error *
1660 stop_log_thread(struct tog_log_view_state *s)
1662 const struct got_error *err = NULL;
1667 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1669 return got_error_set_errno(errcode,
1670 "pthread_cond_signal");
1671 errcode = pthread_mutex_unlock(&tog_mutex);
1673 return got_error_set_errno(errcode,
1674 "pthread_mutex_unlock");
1675 errcode = pthread_join(s->thread, (void **)&err);
1677 return got_error_set_errno(errcode, "pthread_join");
1678 errcode = pthread_mutex_lock(&tog_mutex);
1680 return got_error_set_errno(errcode,
1681 "pthread_mutex_lock");
1685 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1686 if (errcode && err == NULL)
1687 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1689 if (s->thread_args.repo) {
1690 got_repo_close(s->thread_args.repo);
1691 s->thread_args.repo = NULL;
1694 if (s->thread_args.graph) {
1695 got_commit_graph_close(s->thread_args.graph);
1696 s->thread_args.graph = NULL;
1702 static const struct got_error *
1703 close_log_view(struct tog_view *view)
1705 const struct got_error *err = NULL;
1706 struct tog_log_view_state *s = &view->state.log;
1708 err = stop_log_thread(s);
1709 free_commits(&s->commits);
1710 free(s->in_repo_path);
1711 s->in_repo_path = NULL;
1717 static const struct got_error *
1718 search_start_log_view(struct tog_view *view)
1720 struct tog_log_view_state *s = &view->state.log;
1722 s->matched_entry = NULL;
1723 s->search_entry = NULL;
1728 match_commit(struct got_commit_object *commit, const char *id_str,
1731 regmatch_t regmatch;
1733 if (regexec(regex, got_object_commit_get_author(commit), 1,
1734 ®match, 0) == 0 ||
1735 regexec(regex, got_object_commit_get_committer(commit), 1,
1736 ®match, 0) == 0 ||
1737 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1738 ®match, 0) == 0 ||
1739 regexec(regex, id_str, 1, ®match, 0) == 0)
1745 static const struct got_error *
1746 search_next_log_view(struct tog_view *view)
1748 const struct got_error *err = NULL;
1749 struct tog_log_view_state *s = &view->state.log;
1750 struct commit_queue_entry *entry;
1752 if (!view->searching) {
1753 view->search_next_done = 1;
1757 if (s->search_entry) {
1758 if (view->searching == TOG_SEARCH_FORWARD)
1759 entry = TAILQ_NEXT(s->search_entry, entry);
1761 entry = TAILQ_PREV(s->search_entry,
1762 commit_queue_head, entry);
1763 } else if (s->matched_entry) {
1764 if (view->searching == TOG_SEARCH_FORWARD)
1765 entry = TAILQ_NEXT(s->selected_entry, entry);
1767 entry = TAILQ_PREV(s->selected_entry,
1768 commit_queue_head, entry);
1770 if (view->searching == TOG_SEARCH_FORWARD)
1771 entry = TAILQ_FIRST(&s->commits.head);
1773 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1778 if (entry == NULL) {
1779 if (s->thread_args.log_complete ||
1780 view->searching == TOG_SEARCH_BACKWARD) {
1781 view->search_next_done = 1;
1785 * Poke the log thread for more commits and return,
1786 * allowing the main loop to make progress. Search
1787 * will resume at s->search_entry once we come back.
1789 s->thread_args.commits_needed++;
1790 return trigger_log_thread(1,
1791 &s->thread_args.commits_needed,
1792 &s->thread_args.log_complete,
1793 &s->thread_args.need_commits);
1796 err = got_object_id_str(&id_str, entry->id);
1800 if (match_commit(entry->commit, id_str, &view->regex)) {
1801 view->search_next_done = 1;
1802 s->matched_entry = entry;
1807 s->search_entry = entry;
1808 if (view->searching == TOG_SEARCH_FORWARD)
1809 entry = TAILQ_NEXT(entry, entry);
1811 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1814 if (s->matched_entry) {
1815 int cur = s->selected_entry->idx;
1816 while (cur < s->matched_entry->idx) {
1817 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1822 while (cur > s->matched_entry->idx) {
1823 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1830 s->search_entry = NULL;
1835 static const struct got_error *
1836 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1837 struct got_reflist_head *refs, struct got_repository *repo,
1838 const char *head_ref_name, const char *path, int check_disk)
1840 const struct got_error *err = NULL;
1841 struct tog_log_view_state *s = &view->state.log;
1842 struct got_repository *thread_repo = NULL;
1843 struct got_commit_graph *thread_graph = NULL;
1846 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1850 /* The commit queue only contains commits being displayed. */
1851 TAILQ_INIT(&s->commits.head);
1852 s->commits.ncommits = 0;
1856 s->head_ref_name = head_ref_name;
1857 s->start_id = got_object_id_dup(start_id);
1858 if (s->start_id == NULL) {
1859 err = got_error_from_errno("got_object_id_dup");
1863 view->show = show_log_view;
1864 view->input = input_log_view;
1865 view->close = close_log_view;
1866 view->search_start = search_start_log_view;
1867 view->search_next = search_next_log_view;
1869 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1872 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1877 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1879 err = got_error_set_errno(errcode, "pthread_cond_init");
1883 s->thread_args.commits_needed = view->nlines;
1884 s->thread_args.graph = thread_graph;
1885 s->thread_args.commits = &s->commits;
1886 s->thread_args.in_repo_path = s->in_repo_path;
1887 s->thread_args.start_id = s->start_id;
1888 s->thread_args.repo = thread_repo;
1889 s->thread_args.log_complete = 0;
1890 s->thread_args.quit = &s->quit;
1891 s->thread_args.view = view;
1892 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1893 s->thread_args.selected_entry = &s->selected_entry;
1896 close_log_view(view);
1900 static const struct got_error *
1901 show_log_view(struct tog_view *view)
1903 struct tog_log_view_state *s = &view->state.log;
1905 if (s->thread == NULL) {
1906 int errcode = pthread_create(&s->thread, NULL, log_thread,
1909 return got_error_set_errno(errcode, "pthread_create");
1912 return draw_commits(view, &s->last_displayed_entry,
1913 &s->selected_entry, s->first_displayed_entry,
1914 &s->commits, s->selected, view->nlines, s->refs,
1915 s->in_repo_path, s->thread_args.commits_needed);
1918 static const struct got_error *
1919 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1920 struct tog_view **focus_view, struct tog_view *view, int ch)
1922 const struct got_error *err = NULL;
1923 struct tog_log_view_state *s = &view->state.log;
1924 char *parent_path, *in_repo_path = NULL;
1925 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1927 struct got_object_id *start_id;
1937 if (s->first_displayed_entry == NULL)
1939 if (s->selected > 0)
1942 scroll_up(view, &s->first_displayed_entry, 1,
1947 if (s->first_displayed_entry == NULL)
1949 if (TAILQ_FIRST(&s->commits.head) ==
1950 s->first_displayed_entry) {
1954 scroll_up(view, &s->first_displayed_entry,
1955 view->nlines, &s->commits);
1961 if (s->first_displayed_entry == NULL)
1963 if (s->selected < MIN(view->nlines - 2,
1964 s->commits.ncommits - 1)) {
1968 err = scroll_down(view, &s->first_displayed_entry, 1,
1969 &s->last_displayed_entry, &s->commits,
1970 &s->thread_args.log_complete,
1971 &s->thread_args.commits_needed,
1972 &s->thread_args.need_commits);
1976 struct commit_queue_entry *first;
1977 first = s->first_displayed_entry;
1980 err = scroll_down(view, &s->first_displayed_entry,
1981 view->nlines, &s->last_displayed_entry,
1982 &s->commits, &s->thread_args.log_complete,
1983 &s->thread_args.commits_needed,
1984 &s->thread_args.need_commits);
1985 if (first == s->first_displayed_entry &&
1986 s->selected < MIN(view->nlines - 2,
1987 s->commits.ncommits - 1)) {
1988 /* can't scroll further down */
1989 s->selected = MIN(view->nlines - 2,
1990 s->commits.ncommits - 1);
1996 if (s->selected > view->nlines - 2)
1997 s->selected = view->nlines - 2;
1998 if (s->selected > s->commits.ncommits - 1)
1999 s->selected = s->commits.ncommits - 1;
2004 if (s->selected_entry == NULL)
2006 if (view_is_parent_view(view))
2007 begin_x = view_split_begin_x(view->begin_x);
2008 err = open_diff_view_for_commit(&diff_view, begin_x,
2009 s->selected_entry->commit, s->selected_entry->id,
2010 view, s->refs, s->repo);
2013 if (view_is_parent_view(view)) {
2014 err = view_close_child(view);
2017 err = view_set_child(view, diff_view);
2019 view_close(diff_view);
2022 *focus_view = diff_view;
2023 view->child_focussed = 1;
2025 *new_view = diff_view;
2028 if (s->selected_entry == NULL)
2030 if (view_is_parent_view(view))
2031 begin_x = view_split_begin_x(view->begin_x);
2032 err = browse_commit_tree(&tree_view, begin_x,
2033 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2036 if (view_is_parent_view(view)) {
2037 err = view_close_child(view);
2040 err = view_set_child(view, tree_view);
2042 view_close(tree_view);
2045 *focus_view = tree_view;
2046 view->child_focussed = 1;
2048 *new_view = tree_view;
2051 if (strcmp(s->in_repo_path, "/") == 0)
2053 parent_path = dirname(s->in_repo_path);
2054 if (parent_path && strcmp(parent_path, ".") != 0) {
2055 err = stop_log_thread(s);
2058 lv = view_open(view->nlines, view->ncols,
2059 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2061 return got_error_from_errno(
2063 err = open_log_view(lv, s->start_id, s->refs,
2064 s->repo, s->head_ref_name, parent_path, 0);
2067 if (view_is_parent_view(view))
2070 view_set_child(view->parent, lv);
2077 err = stop_log_thread(s);
2080 lv = view_open(view->nlines, view->ncols,
2081 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2083 return got_error_from_errno("view_open");
2084 err = get_head_commit_id(&start_id, s->head_ref_name ?
2085 s->head_ref_name : GOT_REF_HEAD, s->repo);
2088 in_repo_path = strdup(s->in_repo_path);
2089 if (in_repo_path == NULL) {
2091 return got_error_from_errno("strdup");
2093 err = open_log_view(lv, start_id, s->refs, s->repo,
2094 s->head_ref_name, in_repo_path, 0);
2107 static const struct got_error *
2108 apply_unveil(const char *repo_path, const char *worktree_path)
2110 const struct got_error *error;
2113 if (unveil("gmon.out", "rwc") != 0)
2114 return got_error_from_errno2("unveil", "gmon.out");
2116 if (repo_path && unveil(repo_path, "r") != 0)
2117 return got_error_from_errno2("unveil", repo_path);
2119 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2120 return got_error_from_errno2("unveil", worktree_path);
2122 if (unveil("/tmp", "rwc") != 0)
2123 return got_error_from_errno2("unveil", "/tmp");
2125 error = got_privsep_unveil_exec_helpers();
2129 if (unveil(NULL, NULL) != 0)
2130 return got_error_from_errno("unveil");
2140 halfdelay(1); /* Do fast refresh while initial view is loading. */
2143 intrflush(stdscr, FALSE);
2144 keypad(stdscr, TRUE);
2146 signal(SIGWINCH, tog_sigwinch);
2149 static const struct got_error *
2150 cmd_log(int argc, char *argv[])
2152 const struct got_error *error;
2153 struct got_repository *repo = NULL;
2154 struct got_worktree *worktree = NULL;
2155 struct got_reflist_head refs;
2156 struct got_object_id *start_id = NULL;
2157 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2158 char *start_commit = NULL;
2160 struct tog_view *view;
2162 SIMPLEQ_INIT(&refs);
2165 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2170 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2173 start_commit = optarg;
2176 repo_path = realpath(optarg, NULL);
2177 if (repo_path == NULL)
2178 err(1, "-r option");
2189 cwd = getcwd(NULL, 0);
2191 error = got_error_from_errno("getcwd");
2194 error = got_worktree_open(&worktree, cwd);
2195 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2202 error = got_error_from_errno("strdup");
2205 } else if (argc == 1) {
2207 error = got_worktree_resolve_path(&path, worktree,
2212 path = strdup(argv[0]);
2214 error = got_error_from_errno("strdup");
2221 repo_path = worktree ?
2222 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2223 if (repo_path == NULL) {
2224 error = got_error_from_errno("strdup");
2230 error = got_repo_open(&repo, repo_path);
2234 error = apply_unveil(got_repo_get_path(repo),
2235 worktree ? got_worktree_get_root_path(worktree) : NULL);
2239 if (start_commit == NULL)
2240 error = get_head_commit_id(&start_id, worktree ?
2241 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2244 error = get_head_commit_id(&start_id, start_commit, repo);
2246 if (error->code != GOT_ERR_NOT_REF)
2248 error = got_repo_match_object_id_prefix(&start_id,
2249 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2255 error = got_ref_list(&refs, repo);
2259 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2261 error = got_error_from_errno("view_open");
2264 error = open_log_view(view, start_id, &refs, repo, worktree ?
2265 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2268 error = view_loop(view);
2275 got_repo_close(repo);
2277 got_worktree_close(worktree);
2278 got_ref_list_free(&refs);
2286 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2292 parse_next_line(FILE *f, size_t *len)
2297 const char delim[3] = { '\0', '\0', '\0'};
2299 line = fparseln(f, &linelen, &lineno, delim, 0);
2305 static const struct got_error *
2306 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2307 int *last_displayed_line, int *eof, int max_lines,
2310 const struct got_error *err;
2311 int nlines = 0, nprinted = 0;
2318 werase(view->window);
2321 err = format_line(&wline, &width, header, view->ncols);
2326 if (view_needs_focus_indication(view))
2327 wstandout(view->window);
2328 waddwstr(view->window, wline);
2329 if (view_needs_focus_indication(view))
2330 wstandend(view->window);
2331 if (width < view->ncols - 1)
2332 waddch(view->window, '\n');
2340 while (nprinted < max_lines) {
2341 line = parse_next_line(f, &len);
2346 if (++nlines < *first_displayed_line) {
2351 err = format_line(&wline, &width, line, view->ncols);
2356 waddwstr(view->window, wline);
2357 if (width < view->ncols - 1)
2358 waddch(view->window, '\n');
2359 if (++nprinted == 1)
2360 *first_displayed_line = nlines;
2365 *last_displayed_line = nlines;
2370 while (nprinted < view->nlines) {
2371 waddch(view->window, '\n');
2375 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2380 wstandout(view->window);
2381 waddwstr(view->window, wline);
2382 wstandend(view->window);
2389 get_datestr(time_t *time, char *datebuf)
2391 char *p, *s = ctime_r(time, datebuf);
2392 p = strchr(s, '\n');
2398 static const struct got_error *
2399 write_commit_info(struct got_object_id *commit_id,
2400 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2402 const struct got_error *err = NULL;
2404 struct got_commit_object *commit;
2405 char *id_str = NULL;
2406 time_t committer_time;
2407 const char *author, *committer;
2408 char *refs_str = NULL;
2411 err = build_refs_str(&refs_str, refs, commit_id);
2416 err = got_object_open_as_commit(&commit, repo, commit_id);
2420 err = got_object_id_str(&id_str, commit_id);
2422 err = got_error_from_errno("got_object_id_str");
2426 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2427 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2428 err = got_error_from_errno("fprintf");
2431 if (fprintf(outfile, "from: %s\n",
2432 got_object_commit_get_author(commit)) < 0) {
2433 err = got_error_from_errno("fprintf");
2436 committer_time = got_object_commit_get_committer_time(commit);
2437 if (fprintf(outfile, "date: %s UTC\n",
2438 get_datestr(&committer_time, datebuf)) < 0) {
2439 err = got_error_from_errno("fprintf");
2442 author = got_object_commit_get_author(commit);
2443 committer = got_object_commit_get_committer(commit);
2444 if (strcmp(author, committer) != 0 &&
2445 fprintf(outfile, "via: %s\n", committer) < 0) {
2446 err = got_error_from_errno("fprintf");
2449 if (fprintf(outfile, "%s\n",
2450 got_object_commit_get_logmsg(commit)) < 0) {
2451 err = got_error_from_errno("fprintf");
2457 got_object_commit_close(commit);
2461 static const struct got_error *
2462 create_diff(struct tog_diff_view_state *s)
2464 const struct got_error *err = NULL;
2470 err = got_error_from_errno("got_opentemp");
2473 if (s->f && fclose(s->f) != 0) {
2474 err = got_error_from_errno("fclose");
2480 err = got_object_get_type(&obj_type, s->repo, s->id1);
2482 err = got_object_get_type(&obj_type, s->repo, s->id2);
2487 case GOT_OBJ_TYPE_BLOB:
2488 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2489 s->diff_context, s->repo, f);
2491 case GOT_OBJ_TYPE_TREE:
2492 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2493 s->diff_context, s->repo, f);
2495 case GOT_OBJ_TYPE_COMMIT: {
2496 const struct got_object_id_queue *parent_ids;
2497 struct got_object_qid *pid;
2498 struct got_commit_object *commit2;
2500 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2503 /* Show commit info if we're diffing to a parent/root commit. */
2505 write_commit_info(s->id2, s->refs, s->repo, f);
2507 parent_ids = got_object_commit_get_parent_ids(commit2);
2508 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2509 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2510 write_commit_info(s->id2, s->refs,
2516 got_object_commit_close(commit2);
2518 err = got_diff_objects_as_commits(s->id1, s->id2,
2519 s->diff_context, s->repo, f);
2523 err = got_error(GOT_ERR_OBJ_TYPE);
2527 if (f && fflush(f) != 0 && err == NULL)
2528 err = got_error_from_errno("fflush");
2533 diff_view_indicate_progress(struct tog_view *view)
2535 mvwaddstr(view->window, 0, 0, "diffing...");
2540 static const struct got_error *
2541 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2542 struct got_object_id *id2, struct tog_view *log_view,
2543 struct got_reflist_head *refs, struct got_repository *repo)
2545 const struct got_error *err;
2547 if (id1 != NULL && id2 != NULL) {
2549 err = got_object_get_type(&type1, repo, id1);
2552 err = got_object_get_type(&type2, repo, id2);
2557 return got_error(GOT_ERR_OBJ_TYPE);
2561 view->state.diff.id1 = got_object_id_dup(id1);
2562 if (view->state.diff.id1 == NULL)
2563 return got_error_from_errno("got_object_id_dup");
2565 view->state.diff.id1 = NULL;
2567 view->state.diff.id2 = got_object_id_dup(id2);
2568 if (view->state.diff.id2 == NULL) {
2569 free(view->state.diff.id1);
2570 view->state.diff.id1 = NULL;
2571 return got_error_from_errno("got_object_id_dup");
2573 view->state.diff.f = NULL;
2574 view->state.diff.first_displayed_line = 1;
2575 view->state.diff.last_displayed_line = view->nlines;
2576 view->state.diff.diff_context = 3;
2577 view->state.diff.log_view = log_view;
2578 view->state.diff.repo = repo;
2579 view->state.diff.refs = refs;
2581 if (log_view && view_is_splitscreen(view))
2582 show_log_view(log_view); /* draw vborder */
2583 diff_view_indicate_progress(view);
2585 err = create_diff(&view->state.diff);
2587 free(view->state.diff.id1);
2588 view->state.diff.id1 = NULL;
2589 free(view->state.diff.id2);
2590 view->state.diff.id2 = NULL;
2594 view->show = show_diff_view;
2595 view->input = input_diff_view;
2596 view->close = close_diff_view;
2601 static const struct got_error *
2602 close_diff_view(struct tog_view *view)
2604 const struct got_error *err = NULL;
2606 free(view->state.diff.id1);
2607 view->state.diff.id1 = NULL;
2608 free(view->state.diff.id2);
2609 view->state.diff.id2 = NULL;
2610 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2611 err = got_error_from_errno("fclose");
2615 static const struct got_error *
2616 show_diff_view(struct tog_view *view)
2618 const struct got_error *err;
2619 struct tog_diff_view_state *s = &view->state.diff;
2620 char *id_str1 = NULL, *id_str2, *header;
2623 err = got_object_id_str(&id_str1, s->id1);
2627 err = got_object_id_str(&id_str2, s->id2);
2631 if (asprintf(&header, "diff %s %s",
2632 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2633 err = got_error_from_errno("asprintf");
2641 return draw_file(view, s->f, &s->first_displayed_line,
2642 &s->last_displayed_line, &s->eof, view->nlines,
2646 static const struct got_error *
2647 set_selected_commit(struct tog_diff_view_state *s,
2648 struct commit_queue_entry *entry)
2650 const struct got_error *err;
2651 const struct got_object_id_queue *parent_ids;
2652 struct got_commit_object *selected_commit;
2653 struct got_object_qid *pid;
2656 s->id2 = got_object_id_dup(entry->id);
2658 return got_error_from_errno("got_object_id_dup");
2660 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2663 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2665 pid = SIMPLEQ_FIRST(parent_ids);
2666 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2667 got_object_commit_close(selected_commit);
2671 static const struct got_error *
2672 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2673 struct tog_view **focus_view, struct tog_view *view, int ch)
2675 const struct got_error *err = NULL;
2676 struct tog_diff_view_state *s = &view->state.diff;
2677 struct tog_log_view_state *ls;
2678 struct commit_queue_entry *entry;
2684 if (s->first_displayed_line > 1)
2685 s->first_displayed_line--;
2689 if (s->first_displayed_line == 1)
2692 while (i++ < view->nlines - 1 &&
2693 s->first_displayed_line > 1)
2694 s->first_displayed_line--;
2699 s->first_displayed_line++;
2707 while (!s->eof && i++ < view->nlines - 1) {
2709 line = parse_next_line(s->f, NULL);
2710 s->first_displayed_line++;
2716 if (s->diff_context > 0) {
2718 diff_view_indicate_progress(view);
2719 err = create_diff(s);
2723 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2725 diff_view_indicate_progress(view);
2726 err = create_diff(s);
2731 if (s->log_view == NULL)
2733 ls = &s->log_view->state.log;
2734 entry = TAILQ_PREV(ls->selected_entry,
2735 commit_queue_head, entry);
2739 err = input_log_view(NULL, NULL, NULL, s->log_view,
2744 err = set_selected_commit(s, entry);
2748 s->first_displayed_line = 1;
2749 s->last_displayed_line = view->nlines;
2751 diff_view_indicate_progress(view);
2752 err = create_diff(s);
2756 if (s->log_view == NULL)
2758 ls = &s->log_view->state.log;
2760 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2761 ls->thread_args.commits_needed++;
2763 /* Display "loading..." in log view. */
2764 show_log_view(s->log_view);
2768 err = trigger_log_thread(1 /* load_all */,
2769 &ls->thread_args.commits_needed,
2770 &ls->thread_args.log_complete,
2771 &ls->thread_args.need_commits);
2775 err = input_log_view(NULL, NULL, NULL, s->log_view,
2780 entry = TAILQ_NEXT(ls->selected_entry, entry);
2784 err = set_selected_commit(s, entry);
2788 s->first_displayed_line = 1;
2789 s->last_displayed_line = view->nlines;
2791 diff_view_indicate_progress(view);
2792 err = create_diff(s);
2801 static const struct got_error *
2802 cmd_diff(int argc, char *argv[])
2804 const struct got_error *error = NULL;
2805 struct got_repository *repo = NULL;
2806 struct got_reflist_head refs;
2807 struct got_object_id *id1 = NULL, *id2 = NULL;
2808 char *repo_path = NULL;
2809 char *id_str1 = NULL, *id_str2 = NULL;
2811 struct tog_view *view;
2813 SIMPLEQ_INIT(&refs);
2816 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2821 while ((ch = getopt(argc, argv, "")) != -1) {
2833 usage_diff(); /* TODO show local worktree changes */
2834 } else if (argc == 2) {
2835 repo_path = getcwd(NULL, 0);
2836 if (repo_path == NULL)
2837 return got_error_from_errno("getcwd");
2840 } else if (argc == 3) {
2841 repo_path = realpath(argv[0], NULL);
2842 if (repo_path == NULL)
2843 return got_error_from_errno2("realpath", argv[0]);
2851 error = got_repo_open(&repo, repo_path);
2855 error = apply_unveil(got_repo_get_path(repo), NULL);
2859 error = got_repo_match_object_id_prefix(&id1, id_str1,
2860 GOT_OBJ_TYPE_ANY, repo);
2864 error = got_repo_match_object_id_prefix(&id2, id_str2,
2865 GOT_OBJ_TYPE_ANY, repo);
2869 error = got_ref_list(&refs, repo);
2873 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2875 error = got_error_from_errno("view_open");
2878 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2881 error = view_loop(view);
2885 got_repo_close(repo);
2886 got_ref_list_free(&refs);
2894 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2899 struct tog_blame_line {
2901 struct got_object_id *id;
2904 static const struct got_error *
2905 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2906 const char *path, struct tog_blame_line *lines, int nlines,
2907 int blame_complete, int selected_line, int *first_displayed_line,
2908 int *last_displayed_line, int *eof, int max_lines)
2910 const struct got_error *err;
2911 int lineno = 0, nprinted = 0;
2916 struct tog_blame_line *blame_line;
2917 struct got_object_id *prev_id = NULL;
2920 err = got_object_id_str(&id_str, id);
2925 werase(view->window);
2927 if (asprintf(&line, "commit %s", id_str) == -1) {
2928 err = got_error_from_errno("asprintf");
2933 err = format_line(&wline, &width, line, view->ncols);
2936 if (view_needs_focus_indication(view))
2937 wstandout(view->window);
2938 waddwstr(view->window, wline);
2939 if (view_needs_focus_indication(view))
2940 wstandend(view->window);
2943 if (width < view->ncols - 1)
2944 waddch(view->window, '\n');
2946 if (asprintf(&line, "[%d/%d] %s%s",
2947 *first_displayed_line - 1 + selected_line, nlines,
2948 blame_complete ? "" : "annotating... ", path) == -1) {
2950 return got_error_from_errno("asprintf");
2953 err = format_line(&wline, &width, line, view->ncols);
2958 waddwstr(view->window, wline);
2961 if (width < view->ncols - 1)
2962 waddch(view->window, '\n');
2965 while (nprinted < max_lines - 2) {
2966 line = parse_next_line(f, &len);
2971 if (++lineno < *first_displayed_line) {
2976 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2977 err = format_line(&wline, &width, line, wlimit);
2983 if (view->focussed && nprinted == selected_line - 1)
2984 wstandout(view->window);
2986 blame_line = &lines[lineno - 1];
2987 if (blame_line->annotated && prev_id &&
2988 got_object_id_cmp(prev_id, blame_line->id) == 0)
2989 waddstr(view->window, " ");
2990 else if (blame_line->annotated) {
2992 err = got_object_id_str(&id_str, blame_line->id);
2998 wprintw(view->window, "%.8s ", id_str);
3000 prev_id = blame_line->id;
3002 waddstr(view->window, "........ ");
3006 waddwstr(view->window, wline);
3007 while (width < wlimit) {
3008 waddch(view->window, ' ');
3011 if (view->focussed && nprinted == selected_line - 1)
3012 wstandend(view->window);
3013 if (++nprinted == 1)
3014 *first_displayed_line = lineno;
3019 *last_displayed_line = lineno;
3026 static const struct got_error *
3027 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3029 const struct got_error *err = NULL;
3030 struct tog_blame_cb_args *a = arg;
3031 struct tog_blame_line *line;
3034 if (nlines != a->nlines ||
3035 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3036 return got_error(GOT_ERR_RANGE);
3038 errcode = pthread_mutex_lock(&tog_mutex);
3040 return got_error_set_errno(errcode, "pthread_mutex_lock");
3042 if (*a->quit) { /* user has quit the blame view */
3043 err = got_error(GOT_ERR_ITER_COMPLETED);
3048 goto done; /* no change in this commit */
3050 line = &a->lines[lineno - 1];
3051 if (line->annotated)
3054 line->id = got_object_id_dup(id);
3055 if (line->id == NULL) {
3056 err = got_error_from_errno("got_object_id_dup");
3059 line->annotated = 1;
3061 errcode = pthread_mutex_unlock(&tog_mutex);
3063 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3068 blame_thread(void *arg)
3070 const struct got_error *err;
3071 struct tog_blame_thread_args *ta = arg;
3072 struct tog_blame_cb_args *a = ta->cb_args;
3075 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3076 blame_cb, ta->cb_args);
3078 errcode = pthread_mutex_lock(&tog_mutex);
3080 return (void *)got_error_set_errno(errcode,
3081 "pthread_mutex_lock");
3083 got_repo_close(ta->repo);
3087 errcode = pthread_mutex_unlock(&tog_mutex);
3088 if (errcode && err == NULL)
3089 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3094 static struct got_object_id *
3095 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3098 struct tog_blame_line *line;
3100 line = &lines[first_displayed_line - 1 + selected_line - 1];
3101 if (!line->annotated)
3107 static const struct got_error *
3108 stop_blame(struct tog_blame *blame)
3110 const struct got_error *err = NULL;
3113 if (blame->thread) {
3115 errcode = pthread_mutex_unlock(&tog_mutex);
3117 return got_error_set_errno(errcode,
3118 "pthread_mutex_unlock");
3119 errcode = pthread_join(blame->thread, (void **)&err);
3121 return got_error_set_errno(errcode, "pthread_join");
3122 errcode = pthread_mutex_lock(&tog_mutex);
3124 return got_error_set_errno(errcode,
3125 "pthread_mutex_lock");
3126 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3128 blame->thread = NULL;
3130 if (blame->thread_args.repo) {
3131 got_repo_close(blame->thread_args.repo);
3132 blame->thread_args.repo = NULL;
3135 if (fclose(blame->f) != 0 && err == NULL)
3136 err = got_error_from_errno("fclose");
3140 for (i = 0; i < blame->nlines; i++)
3141 free(blame->lines[i].id);
3143 blame->lines = NULL;
3145 free(blame->cb_args.commit_id);
3146 blame->cb_args.commit_id = NULL;
3151 static const struct got_error *
3152 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3153 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3154 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3155 struct got_repository *repo)
3157 const struct got_error *err = NULL;
3158 struct got_blob_object *blob = NULL;
3159 struct got_repository *thread_repo = NULL;
3160 struct got_object_id *obj_id = NULL;
3163 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3167 return got_error(GOT_ERR_NO_OBJ);
3169 err = got_object_get_type(&obj_type, repo, obj_id);
3173 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3174 err = got_error(GOT_ERR_OBJ_TYPE);
3178 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3181 blame->f = got_opentemp();
3182 if (blame->f == NULL) {
3183 err = got_error_from_errno("got_opentemp");
3186 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3187 &blame->line_offsets, blame->f, blob);
3191 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3192 if (blame->lines == NULL) {
3193 err = got_error_from_errno("calloc");
3197 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3201 blame->cb_args.view = view;
3202 blame->cb_args.lines = blame->lines;
3203 blame->cb_args.nlines = blame->nlines;
3204 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3205 if (blame->cb_args.commit_id == NULL) {
3206 err = got_error_from_errno("got_object_id_dup");
3209 blame->cb_args.quit = done;
3211 blame->thread_args.path = path;
3212 blame->thread_args.repo = thread_repo;
3213 blame->thread_args.cb_args = &blame->cb_args;
3214 blame->thread_args.complete = blame_complete;
3215 *blame_complete = 0;
3219 got_object_blob_close(blob);
3226 static const struct got_error *
3227 open_blame_view(struct tog_view *view, char *path,
3228 struct got_object_id *commit_id, struct got_reflist_head *refs,
3229 struct got_repository *repo)
3231 const struct got_error *err = NULL;
3232 struct tog_blame_view_state *s = &view->state.blame;
3234 SIMPLEQ_INIT(&s->blamed_commits);
3236 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3240 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3241 s->first_displayed_line = 1;
3242 s->last_displayed_line = view->nlines;
3243 s->selected_line = 1;
3244 s->blame_complete = 0;
3246 if (s->path == NULL)
3247 return got_error_from_errno("open_blame_view");
3250 s->commit_id = commit_id;
3251 memset(&s->blame, 0, sizeof(s->blame));
3253 view->show = show_blame_view;
3254 view->input = input_blame_view;
3255 view->close = close_blame_view;
3256 view->search_start = search_start_blame_view;
3257 view->search_next = search_next_blame_view;
3259 return run_blame(&s->blame, view, &s->blame_complete,
3260 &s->first_displayed_line, &s->last_displayed_line,
3261 &s->selected_line, &s->done, &s->eof, s->path,
3262 s->blamed_commit->id, s->repo);
3265 static const struct got_error *
3266 close_blame_view(struct tog_view *view)
3268 const struct got_error *err = NULL;
3269 struct tog_blame_view_state *s = &view->state.blame;
3271 if (s->blame.thread)
3272 err = stop_blame(&s->blame);
3274 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3275 struct got_object_qid *blamed_commit;
3276 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3277 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3278 got_object_qid_free(blamed_commit);
3286 static const struct got_error *
3287 search_start_blame_view(struct tog_view *view)
3289 struct tog_blame_view_state *s = &view->state.blame;
3291 s->matched_line = 0;
3296 match_line(const char *line, regex_t *regex)
3298 regmatch_t regmatch;
3300 return regexec(regex, line, 1, ®match, 0) == 0;
3304 static const struct got_error *
3305 search_next_blame_view(struct tog_view *view)
3307 struct tog_blame_view_state *s = &view->state.blame;
3310 if (!view->searching) {
3311 view->search_next_done = 1;
3315 if (s->matched_line) {
3316 if (view->searching == TOG_SEARCH_FORWARD)
3317 lineno = s->matched_line + 1;
3319 lineno = s->matched_line - 1;
3321 if (view->searching == TOG_SEARCH_FORWARD)
3324 lineno = s->blame.nlines;
3332 if (lineno <= 0 || lineno > s->blame.nlines) {
3333 if (s->matched_line == 0) {
3334 view->search_next_done = 1;
3339 if (view->searching == TOG_SEARCH_FORWARD)
3342 lineno = s->blame.nlines;
3345 offset = s->blame.line_offsets[lineno - 1];
3346 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3348 return got_error_from_errno("fseeko");
3351 line = parse_next_line(s->blame.f, &len);
3352 if (line && match_line(line, &view->regex)) {
3353 view->search_next_done = 1;
3354 s->matched_line = lineno;
3359 if (view->searching == TOG_SEARCH_FORWARD)
3365 if (s->matched_line) {
3366 s->first_displayed_line = s->matched_line;
3367 s->selected_line = 1;
3373 static const struct got_error *
3374 show_blame_view(struct tog_view *view)
3376 const struct got_error *err = NULL;
3377 struct tog_blame_view_state *s = &view->state.blame;
3380 if (s->blame.thread == NULL) {
3381 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3382 &s->blame.thread_args);
3384 return got_error_set_errno(errcode, "pthread_create");
3387 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3388 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3389 s->selected_line, &s->first_displayed_line,
3390 &s->last_displayed_line, &s->eof, view->nlines);
3396 static const struct got_error *
3397 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3398 struct tog_view **focus_view, struct tog_view *view, int ch)
3400 const struct got_error *err = NULL, *thread_err = NULL;
3401 struct tog_view *diff_view;
3402 struct tog_blame_view_state *s = &view->state.blame;
3411 if (s->selected_line > 1)
3413 else if (s->selected_line == 1 &&
3414 s->first_displayed_line > 1)
3415 s->first_displayed_line--;
3418 if (s->first_displayed_line == 1) {
3419 s->selected_line = 1;
3422 if (s->first_displayed_line > view->nlines - 2)
3423 s->first_displayed_line -=
3426 s->first_displayed_line = 1;
3430 if (s->selected_line < view->nlines - 2 &&
3431 s->first_displayed_line +
3432 s->selected_line <= s->blame.nlines)
3434 else if (s->last_displayed_line <
3436 s->first_displayed_line++;
3440 struct got_object_id *id = NULL;
3441 id = get_selected_commit_id(s->blame.lines,
3442 s->first_displayed_line, s->selected_line);
3446 struct got_commit_object *commit;
3447 struct got_object_qid *pid;
3448 struct got_object_id *blob_id = NULL;
3450 err = got_object_open_as_commit(&commit,
3454 pid = SIMPLEQ_FIRST(
3455 got_object_commit_get_parent_ids(commit));
3457 got_object_commit_close(commit);
3460 /* Check if path history ends here. */
3461 err = got_object_id_by_path(&blob_id, s->repo,
3464 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3466 got_object_commit_close(commit);
3469 err = got_object_get_type(&obj_type, s->repo,
3472 /* Can't blame non-blob type objects. */
3473 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3474 got_object_commit_close(commit);
3477 err = got_object_qid_alloc(&s->blamed_commit,
3479 got_object_commit_close(commit);
3481 if (got_object_id_cmp(id,
3482 s->blamed_commit->id) == 0)
3484 err = got_object_qid_alloc(&s->blamed_commit,
3490 thread_err = stop_blame(&s->blame);
3494 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3495 s->blamed_commit, entry);
3496 err = run_blame(&s->blame, view, &s->blame_complete,
3497 &s->first_displayed_line, &s->last_displayed_line,
3498 &s->selected_line, &s->done, &s->eof,
3499 s->path, s->blamed_commit->id, s->repo);
3505 struct got_object_qid *first;
3506 first = SIMPLEQ_FIRST(&s->blamed_commits);
3507 if (!got_object_id_cmp(first->id, s->commit_id))
3510 thread_err = stop_blame(&s->blame);
3514 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3515 got_object_qid_free(s->blamed_commit);
3517 SIMPLEQ_FIRST(&s->blamed_commits);
3518 err = run_blame(&s->blame, view, &s->blame_complete,
3519 &s->first_displayed_line, &s->last_displayed_line,
3520 &s->selected_line, &s->done, &s->eof, s->path,
3521 s->blamed_commit->id, s->repo);
3528 struct got_object_id *id = NULL;
3529 struct got_object_qid *pid;
3530 struct got_commit_object *commit = NULL;
3531 id = get_selected_commit_id(s->blame.lines,
3532 s->first_displayed_line, s->selected_line);
3535 err = got_object_open_as_commit(&commit, s->repo, id);
3538 pid = SIMPLEQ_FIRST(
3539 got_object_commit_get_parent_ids(commit));
3540 if (view_is_parent_view(view))
3541 begin_x = view_split_begin_x(view->begin_x);
3542 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3543 if (diff_view == NULL) {
3544 got_object_commit_close(commit);
3545 err = got_error_from_errno("view_open");
3548 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3549 id, NULL, s->refs, s->repo);
3550 got_object_commit_close(commit);
3552 view_close(diff_view);
3555 if (view_is_parent_view(view)) {
3556 err = view_close_child(view);
3559 err = view_set_child(view, diff_view);
3561 view_close(diff_view);
3564 *focus_view = diff_view;
3565 view->child_focussed = 1;
3567 *new_view = diff_view;
3574 if (s->last_displayed_line >= s->blame.nlines &&
3575 s->selected_line >= MIN(s->blame.nlines,
3576 view->nlines - 2)) {
3579 if (s->last_displayed_line >= s->blame.nlines &&
3580 s->selected_line < view->nlines - 2) {
3581 s->selected_line = MIN(s->blame.nlines,
3585 if (s->last_displayed_line + view->nlines - 2
3587 s->first_displayed_line +=
3590 s->first_displayed_line =
3595 if (s->selected_line > view->nlines - 2) {
3596 s->selected_line = MIN(s->blame.nlines,
3603 return thread_err ? thread_err : err;
3606 static const struct got_error *
3607 cmd_blame(int argc, char *argv[])
3609 const struct got_error *error;
3610 struct got_repository *repo = NULL;
3611 struct got_reflist_head refs;
3612 struct got_worktree *worktree = NULL;
3613 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3614 struct got_object_id *commit_id = NULL;
3615 char *commit_id_str = NULL;
3617 struct tog_view *view;
3619 SIMPLEQ_INIT(&refs);
3622 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3627 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3630 commit_id_str = optarg;
3633 repo_path = realpath(optarg, NULL);
3634 if (repo_path == NULL)
3635 err(1, "-r option");
3651 cwd = getcwd(NULL, 0);
3653 error = got_error_from_errno("getcwd");
3656 if (repo_path == NULL) {
3657 error = got_worktree_open(&worktree, cwd);
3658 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3664 strdup(got_worktree_get_repo_path(worktree));
3665 if (repo_path == NULL)
3666 error = got_error_from_errno("strdup");
3670 repo_path = strdup(cwd);
3671 if (repo_path == NULL) {
3672 error = got_error_from_errno("strdup");
3680 error = got_repo_open(&repo, repo_path);
3684 error = apply_unveil(got_repo_get_path(repo), NULL);
3689 const char *prefix = got_worktree_get_path_prefix(worktree);
3690 char *p, *worktree_subdir = cwd +
3691 strlen(got_worktree_get_root_path(worktree));
3692 if (asprintf(&p, "%s%s%s%s%s",
3693 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3694 worktree_subdir, worktree_subdir[0] ? "/" : "",
3696 error = got_error_from_errno("asprintf");
3699 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3702 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3707 if (commit_id_str == NULL) {
3708 struct got_reference *head_ref;
3709 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3712 error = got_ref_resolve(&commit_id, repo, head_ref);
3713 got_ref_close(head_ref);
3715 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3717 if (error->code != GOT_ERR_NOT_REF)
3719 error = got_repo_match_object_id_prefix(&commit_id,
3720 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3726 error = got_ref_list(&refs, repo);
3730 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3732 error = got_error_from_errno("view_open");
3735 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3738 error = view_loop(view);
3744 got_worktree_close(worktree);
3746 got_repo_close(repo);
3747 got_ref_list_free(&refs);
3751 static const struct got_error *
3752 draw_tree_entries(struct tog_view *view,
3753 struct got_tree_entry **first_displayed_entry,
3754 struct got_tree_entry **last_displayed_entry,
3755 struct got_tree_entry **selected_entry, int *ndisplayed,
3756 const char *label, int show_ids, const char *parent_path,
3757 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3759 const struct got_error *err = NULL;
3760 struct got_tree_entry *te;
3766 werase(view->window);
3771 err = format_line(&wline, &width, label, view->ncols);
3774 if (view_needs_focus_indication(view))
3775 wstandout(view->window);
3776 waddwstr(view->window, wline);
3777 if (view_needs_focus_indication(view))
3778 wstandend(view->window);
3781 if (width < view->ncols - 1)
3782 waddch(view->window, '\n');
3785 err = format_line(&wline, &width, parent_path, view->ncols);
3788 waddwstr(view->window, wline);
3791 if (width < view->ncols - 1)
3792 waddch(view->window, '\n');
3795 waddch(view->window, '\n');
3799 te = SIMPLEQ_FIRST(&entries->head);
3800 if (*first_displayed_entry == NULL) {
3801 if (selected == 0) {
3803 wstandout(view->window);
3804 *selected_entry = NULL;
3806 waddstr(view->window, " ..\n"); /* parent directory */
3807 if (selected == 0 && view->focussed)
3808 wstandend(view->window);
3815 while (te != *first_displayed_entry)
3816 te = SIMPLEQ_NEXT(te, entry);
3820 char *line = NULL, *id_str = NULL;
3823 err = got_object_id_str(&id_str, te->id);
3825 return got_error_from_errno(
3826 "got_object_id_str");
3828 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3829 te->name, S_ISDIR(te->mode) ? "/" :
3830 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3832 return got_error_from_errno("asprintf");
3835 err = format_line(&wline, &width, line, view->ncols);
3840 if (n == selected) {
3842 wstandout(view->window);
3843 *selected_entry = te;
3845 waddwstr(view->window, wline);
3846 if (width < view->ncols - 1)
3847 waddch(view->window, '\n');
3848 if (n == selected && view->focussed)
3849 wstandend(view->window);
3855 *last_displayed_entry = te;
3858 te = SIMPLEQ_NEXT(te, entry);
3865 tree_scroll_up(struct tog_view *view,
3866 struct got_tree_entry **first_displayed_entry, int maxscroll,
3867 const struct got_tree_entries *entries, int isroot)
3869 struct got_tree_entry *te, *prev;
3872 if (*first_displayed_entry == NULL)
3875 te = SIMPLEQ_FIRST(&entries->head);
3876 if (*first_displayed_entry == te) {
3878 *first_displayed_entry = NULL;
3882 /* XXX this is stupid... switch to TAILQ? */
3883 for (i = 0; i < maxscroll; i++) {
3884 while (te != *first_displayed_entry) {
3886 te = SIMPLEQ_NEXT(te, entry);
3888 *first_displayed_entry = prev;
3889 te = SIMPLEQ_FIRST(&entries->head);
3891 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3892 *first_displayed_entry = NULL;
3896 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3897 struct got_tree_entry *last_displayed_entry,
3898 const struct got_tree_entries *entries)
3900 struct got_tree_entry *next, *last;
3903 if (*first_displayed_entry)
3904 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3906 next = SIMPLEQ_FIRST(&entries->head);
3907 last = last_displayed_entry;
3908 while (next && last && n++ < maxscroll) {
3909 last = SIMPLEQ_NEXT(last, entry);
3911 *first_displayed_entry = next;
3912 next = SIMPLEQ_NEXT(next, entry);
3918 static const struct got_error *
3919 tree_entry_path(char **path, struct tog_parent_trees *parents,
3920 struct got_tree_entry *te)
3922 const struct got_error *err = NULL;
3923 struct tog_parent_tree *pt;
3924 size_t len = 2; /* for leading slash and NUL */
3926 TAILQ_FOREACH(pt, parents, entry)
3927 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3929 len += strlen(te->name);
3931 *path = calloc(1, len);
3933 return got_error_from_errno("calloc");
3936 pt = TAILQ_LAST(parents, tog_parent_trees);
3938 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3939 err = got_error(GOT_ERR_NO_SPACE);
3942 if (strlcat(*path, "/", len) >= len) {
3943 err = got_error(GOT_ERR_NO_SPACE);
3946 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3949 if (strlcat(*path, te->name, len) >= len) {
3950 err = got_error(GOT_ERR_NO_SPACE);
3962 static const struct got_error *
3963 blame_tree_entry(struct tog_view **new_view, int begin_x,
3964 struct got_tree_entry *te, struct tog_parent_trees *parents,
3965 struct got_object_id *commit_id, struct got_reflist_head *refs,
3966 struct got_repository *repo)
3968 const struct got_error *err = NULL;
3970 struct tog_view *blame_view;
3972 err = tree_entry_path(&path, parents, te);
3976 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3977 if (blame_view == NULL)
3978 return got_error_from_errno("view_open");
3980 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3982 view_close(blame_view);
3985 *new_view = blame_view;
3989 static const struct got_error *
3990 log_tree_entry(struct tog_view **new_view, int begin_x,
3991 struct got_tree_entry *te, struct tog_parent_trees *parents,
3992 struct got_object_id *commit_id, struct got_reflist_head *refs,
3993 struct got_repository *repo)
3995 struct tog_view *log_view;
3996 const struct got_error *err = NULL;
3999 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4000 if (log_view == NULL)
4001 return got_error_from_errno("view_open");
4003 err = tree_entry_path(&path, parents, te);
4007 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4009 view_close(log_view);
4011 *new_view = log_view;
4016 static const struct got_error *
4017 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4018 struct got_object_id *commit_id, struct got_reflist_head *refs,
4019 struct got_repository *repo)
4021 const struct got_error *err = NULL;
4022 char *commit_id_str = NULL;
4023 struct tog_tree_view_state *s = &view->state.tree;
4025 TAILQ_INIT(&s->parents);
4027 err = got_object_id_str(&commit_id_str, commit_id);
4031 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4032 err = got_error_from_errno("asprintf");
4036 s->root = s->tree = root;
4037 s->entries = got_object_tree_get_entries(root);
4038 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4039 s->commit_id = got_object_id_dup(commit_id);
4040 if (s->commit_id == NULL) {
4041 err = got_error_from_errno("got_object_id_dup");
4047 view->show = show_tree_view;
4048 view->input = input_tree_view;
4049 view->close = close_tree_view;
4050 view->search_start = search_start_tree_view;
4051 view->search_next = search_next_tree_view;
4053 free(commit_id_str);
4055 free(s->tree_label);
4056 s->tree_label = NULL;
4061 static const struct got_error *
4062 close_tree_view(struct tog_view *view)
4064 struct tog_tree_view_state *s = &view->state.tree;
4066 free(s->tree_label);
4067 s->tree_label = NULL;
4069 s->commit_id = NULL;
4070 while (!TAILQ_EMPTY(&s->parents)) {
4071 struct tog_parent_tree *parent;
4072 parent = TAILQ_FIRST(&s->parents);
4073 TAILQ_REMOVE(&s->parents, parent, entry);
4077 if (s->tree != s->root)
4078 got_object_tree_close(s->tree);
4079 got_object_tree_close(s->root);
4084 static const struct got_error *
4085 search_start_tree_view(struct tog_view *view)
4087 struct tog_tree_view_state *s = &view->state.tree;
4089 s->matched_entry = NULL;
4094 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4096 regmatch_t regmatch;
4098 return regexec(regex, te->name, 1, ®match, 0) == 0;
4101 static const struct got_error *
4102 search_next_tree_view(struct tog_view *view)
4104 struct tog_tree_view_state *s = &view->state.tree;
4105 struct got_tree_entry *entry, *te;
4107 if (!view->searching) {
4108 view->search_next_done = 1;
4112 if (s->matched_entry) {
4113 if (view->searching == TOG_SEARCH_FORWARD) {
4114 if (s->selected_entry)
4115 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4117 entry = SIMPLEQ_FIRST(&s->entries->head);
4120 if (s->selected_entry == NULL) {
4121 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4124 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4126 if (SIMPLEQ_NEXT(te, entry) ==
4133 if (view->searching == TOG_SEARCH_FORWARD)
4134 entry = SIMPLEQ_FIRST(&s->entries->head);
4136 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4142 if (entry == NULL) {
4143 if (s->matched_entry == NULL) {
4144 view->search_next_done = 1;
4147 if (view->searching == TOG_SEARCH_FORWARD)
4148 entry = SIMPLEQ_FIRST(&s->entries->head);
4150 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4155 if (match_tree_entry(entry, &view->regex)) {
4156 view->search_next_done = 1;
4157 s->matched_entry = entry;
4161 if (view->searching == TOG_SEARCH_FORWARD)
4162 entry = SIMPLEQ_NEXT(entry, entry);
4164 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4167 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4168 if (SIMPLEQ_NEXT(te, entry) == entry) {
4177 if (s->matched_entry) {
4178 s->first_displayed_entry = s->matched_entry;
4185 static const struct got_error *
4186 show_tree_view(struct tog_view *view)
4188 const struct got_error *err = NULL;
4189 struct tog_tree_view_state *s = &view->state.tree;
4192 err = tree_entry_path(&parent_path, &s->parents, NULL);
4196 err = draw_tree_entries(view, &s->first_displayed_entry,
4197 &s->last_displayed_entry, &s->selected_entry,
4198 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4199 s->entries, s->selected, view->nlines, s->tree == s->root);
4206 static const struct got_error *
4207 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4208 struct tog_view **focus_view, struct tog_view *view, int ch)
4210 const struct got_error *err = NULL;
4211 struct tog_tree_view_state *s = &view->state.tree;
4212 struct tog_view *log_view;
4213 int begin_x = 0, nscrolled;
4217 s->show_ids = !s->show_ids;
4220 if (!s->selected_entry)
4222 if (view_is_parent_view(view))
4223 begin_x = view_split_begin_x(view->begin_x);
4224 err = log_tree_entry(&log_view, begin_x,
4225 s->selected_entry, &s->parents,
4226 s->commit_id, s->refs, s->repo);
4227 if (view_is_parent_view(view)) {
4228 err = view_close_child(view);
4231 err = view_set_child(view, log_view);
4233 view_close(log_view);
4236 *focus_view = log_view;
4237 view->child_focussed = 1;
4239 *new_view = log_view;
4243 if (s->selected > 0) {
4245 if (s->selected == 0)
4248 if (s->selected > 0)
4250 tree_scroll_up(view, &s->first_displayed_entry, 1,
4251 s->entries, s->tree == s->root);
4254 tree_scroll_up(view, &s->first_displayed_entry,
4255 MAX(0, view->nlines - 4 - s->selected), s->entries,
4256 s->tree == s->root);
4258 if (SIMPLEQ_FIRST(&s->entries->head) ==
4259 s->first_displayed_entry && s->tree != s->root)
4260 s->first_displayed_entry = NULL;
4264 if (s->selected < s->ndisplayed - 1) {
4268 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4269 /* can't scroll any further */
4271 tree_scroll_down(&s->first_displayed_entry, 1,
4272 s->last_displayed_entry, s->entries);
4275 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4277 /* can't scroll any further; move cursor down */
4278 if (s->selected < s->ndisplayed - 1)
4279 s->selected = s->ndisplayed - 1;
4282 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4283 view->nlines, s->last_displayed_entry, s->entries);
4284 if (nscrolled < view->nlines) {
4286 struct got_tree_entry *te;
4287 te = s->first_displayed_entry;
4290 te = SIMPLEQ_NEXT(te, entry);
4292 s->selected = ndisplayed - 1;
4298 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4299 struct tog_parent_tree *parent;
4300 /* user selected '..' */
4301 if (s->tree == s->root)
4303 parent = TAILQ_FIRST(&s->parents);
4304 TAILQ_REMOVE(&s->parents, parent,
4306 got_object_tree_close(s->tree);
4307 s->tree = parent->tree;
4309 got_object_tree_get_entries(s->tree);
4310 s->first_displayed_entry =
4311 parent->first_displayed_entry;
4313 parent->selected_entry;
4314 s->selected = parent->selected;
4316 } else if (S_ISDIR(s->selected_entry->mode)) {
4317 struct got_tree_object *subtree;
4318 err = got_object_open_as_tree(&subtree,
4319 s->repo, s->selected_entry->id);
4322 err = tree_view_visit_subtree(subtree, s);
4324 got_object_tree_close(subtree);
4327 } else if (S_ISREG(s->selected_entry->mode)) {
4328 struct tog_view *blame_view;
4329 int begin_x = view_is_parent_view(view) ?
4330 view_split_begin_x(view->begin_x) : 0;
4332 err = blame_tree_entry(&blame_view, begin_x,
4333 s->selected_entry, &s->parents,
4334 s->commit_id, s->refs, s->repo);
4337 if (view_is_parent_view(view)) {
4338 err = view_close_child(view);
4341 err = view_set_child(view, blame_view);
4343 view_close(blame_view);
4346 *focus_view = blame_view;
4347 view->child_focussed = 1;
4349 *new_view = blame_view;
4353 if (s->selected > view->nlines)
4354 s->selected = s->ndisplayed - 1;
4367 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4372 static const struct got_error *
4373 cmd_tree(int argc, char *argv[])
4375 const struct got_error *error;
4376 struct got_repository *repo = NULL;
4377 struct got_reflist_head refs;
4378 char *repo_path = NULL;
4379 struct got_object_id *commit_id = NULL;
4380 char *commit_id_arg = NULL;
4381 struct got_commit_object *commit = NULL;
4382 struct got_tree_object *tree = NULL;
4384 struct tog_view *view;
4386 SIMPLEQ_INIT(&refs);
4389 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4394 while ((ch = getopt(argc, argv, "c:")) != -1) {
4397 commit_id_arg = optarg;
4409 struct got_worktree *worktree;
4410 char *cwd = getcwd(NULL, 0);
4412 return got_error_from_errno("getcwd");
4413 error = got_worktree_open(&worktree, cwd);
4414 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4419 strdup(got_worktree_get_repo_path(worktree));
4420 got_worktree_close(worktree);
4423 if (repo_path == NULL) {
4424 error = got_error_from_errno("strdup");
4427 } else if (argc == 1) {
4428 repo_path = realpath(argv[0], NULL);
4429 if (repo_path == NULL)
4430 return got_error_from_errno2("realpath", argv[0]);
4436 error = got_repo_open(&repo, repo_path);
4440 error = apply_unveil(got_repo_get_path(repo), NULL);
4444 if (commit_id_arg == NULL)
4445 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4447 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4449 if (error->code != GOT_ERR_NOT_REF)
4451 error = got_repo_match_object_id_prefix(&commit_id,
4452 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4458 error = got_object_open_as_commit(&commit, repo, commit_id);
4462 error = got_object_open_as_tree(&tree, repo,
4463 got_object_commit_get_tree_id(commit));
4467 error = got_ref_list(&refs, repo);
4471 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4473 error = got_error_from_errno("view_open");
4476 error = open_tree_view(view, tree, commit_id, &refs, repo);
4479 error = view_loop(view);
4484 got_object_commit_close(commit);
4486 got_object_tree_close(tree);
4488 got_repo_close(repo);
4489 got_ref_list_free(&refs);
4498 fprintf(stderr, "commands:");
4499 for (i = 0; i < nitems(tog_commands); i++) {
4500 struct tog_cmd *cmd = &tog_commands[i];
4501 fprintf(stderr, " %s", cmd->name);
4503 fputc('\n', stderr);
4509 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4516 make_argv(const char *arg0, const char *arg1)
4519 int argc = (arg1 == NULL ? 1 : 2);
4521 argv = calloc(argc, sizeof(char *));
4524 argv[0] = strdup(arg0);
4525 if (argv[0] == NULL)
4528 argv[1] = strdup(arg1);
4529 if (argv[1] == NULL)
4537 main(int argc, char *argv[])
4539 const struct got_error *error = NULL;
4540 struct tog_cmd *cmd = NULL;
4542 char **cmd_argv = NULL;
4544 setlocale(LC_CTYPE, "");
4546 while ((ch = getopt(argc, argv, "h")) != -1) {
4565 /* Build an argument vector which runs a default command. */
4566 cmd = &tog_commands[0];
4567 cmd_argv = make_argv(cmd->name, NULL);
4572 /* Did the user specific a command? */
4573 for (i = 0; i < nitems(tog_commands); i++) {
4574 if (strncmp(tog_commands[i].name, argv[0],
4575 strlen(argv[0])) == 0) {
4576 cmd = &tog_commands[i];
4582 fprintf(stderr, "%s: unknown command '%s'\n",
4583 getprogname(), argv[0]);
4592 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4597 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);