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;
327 tog_sigwinch(int signo)
329 tog_sigwinch_received = 1;
332 static const struct got_error *
333 view_close(struct tog_view *view)
335 const struct got_error *err = NULL;
338 view_close(view->child);
342 err = view->close(view);
344 del_panel(view->panel);
346 delwin(view->window);
351 static struct tog_view *
352 view_open(int nlines, int ncols, int begin_y, int begin_x,
353 enum tog_view_type type)
355 struct tog_view *view = calloc(1, sizeof(*view));
363 view->nlines = nlines ? nlines : LINES - begin_y;
364 view->ncols = ncols ? ncols : COLS - begin_x;
365 view->begin_y = begin_y;
366 view->begin_x = begin_x;
367 view->window = newwin(nlines, ncols, begin_y, begin_x);
368 if (view->window == NULL) {
372 view->panel = new_panel(view->window);
373 if (view->panel == NULL ||
374 set_panel_userptr(view->panel, view) != OK) {
379 keypad(view->window, TRUE);
384 view_split_begin_x(int begin_x)
386 if (begin_x > 0 || COLS < 120)
388 return (COLS - MAX(COLS / 2, 80));
391 static const struct got_error *view_resize(struct tog_view *);
393 static const struct got_error *
394 view_splitscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
399 view->begin_x = view_split_begin_x(0);
400 view->nlines = LINES;
401 view->ncols = COLS - view->begin_x;
404 err = view_resize(view);
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno("mvwin");
414 static const struct got_error *
415 view_fullscreen(struct tog_view *view)
417 const struct got_error *err = NULL;
421 view->nlines = LINES;
425 err = view_resize(view);
429 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
430 return got_error_from_errno("mvwin");
436 view_is_parent_view(struct tog_view *view)
438 return view->parent == NULL;
441 static const struct got_error *
442 view_resize(struct tog_view *view)
446 if (view->lines > LINES)
447 nlines = view->nlines - (view->lines - LINES);
449 nlines = view->nlines + (LINES - view->lines);
451 if (view->cols > COLS)
452 ncols = view->ncols - (view->cols - COLS);
454 ncols = view->ncols + (COLS - view->cols);
456 if (wresize(view->window, nlines, ncols) == ERR)
457 return got_error_from_errno("wresize");
458 if (replace_panel(view->panel, view->window) == ERR)
459 return got_error_from_errno("replace_panel");
460 wclear(view->window);
462 view->nlines = nlines;
468 view->child->begin_x = view_split_begin_x(view->begin_x);
469 if (view->child->begin_x == 0) {
470 view_fullscreen(view->child);
471 if (view->child->focussed)
472 show_panel(view->child->panel);
474 show_panel(view->panel);
476 view_splitscreen(view->child);
477 show_panel(view->child->panel);
484 static const struct got_error *
485 view_close_child(struct tog_view *view)
487 const struct got_error *err = NULL;
489 if (view->child == NULL)
492 err = view_close(view->child);
497 static const struct got_error *
498 view_set_child(struct tog_view *view, struct tog_view *child)
500 const struct got_error *err = NULL;
503 child->parent = view;
508 view_is_splitscreen(struct tog_view *view)
510 return view->begin_x > 0;
519 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
520 cols = 80; /* Default */
526 resize_term(lines, cols);
529 static const struct got_error *
530 view_search_start(struct tog_view *view)
532 const struct got_error *err = NULL;
536 if (view->nlines < 1)
539 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
541 wclrtoeol(view->window);
545 ret = wgetnstr(view->window, pattern, sizeof(pattern));
551 if (view->searching) {
552 regfree(&view->regex);
556 if (regcomp(&view->regex, pattern,
557 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
558 err = view->search_start(view);
560 regfree(&view->regex);
563 view->searching = TOG_SEARCH_FORWARD;
564 view->search_next_done = 0;
565 view->search_next(view);
571 static const struct got_error *
572 view_input(struct tog_view **new, struct tog_view **dead,
573 struct tog_view **focus, int *done, struct tog_view *view,
574 struct tog_view_list_head *views)
576 const struct got_error *err = NULL;
584 if (view->searching && !view->search_next_done) {
585 errcode = pthread_mutex_unlock(&tog_mutex);
587 return got_error_set_errno(errcode,
588 "pthread_mutex_unlock");
590 errcode = pthread_mutex_lock(&tog_mutex);
592 return got_error_set_errno(errcode,
593 "pthread_mutex_lock");
594 view->search_next(view);
598 nodelay(stdscr, FALSE);
599 /* Allow threads to make progress while we are waiting for input. */
600 errcode = pthread_mutex_unlock(&tog_mutex);
602 return got_error_set_errno(errcode, "pthread_mutex_unlock");
603 ch = wgetch(view->window);
604 errcode = pthread_mutex_lock(&tog_mutex);
606 return got_error_set_errno(errcode, "pthread_mutex_lock");
607 nodelay(stdscr, TRUE);
609 if (tog_sigwinch_received) {
611 tog_sigwinch_received = 0;
612 TAILQ_FOREACH(v, views, entry) {
613 err = view_resize(v);
616 err = v->input(new, dead, focus, v, KEY_RESIZE);
627 *focus = view->child;
628 view->child_focussed = 1;
629 } else if (view->parent) {
630 *focus = view->parent;
631 view->parent->child_focussed = 0;
635 err = view->input(new, dead, focus, view, ch);
642 if (view_is_parent_view(view)) {
643 if (view->child == NULL)
645 if (view_is_splitscreen(view->child)) {
646 *focus = view->child;
647 view->child_focussed = 1;
648 err = view_fullscreen(view->child);
650 err = view_splitscreen(view->child);
653 err = view->child->input(new, dead, focus,
654 view->child, KEY_RESIZE);
656 if (view_is_splitscreen(view)) {
658 view->parent->child_focussed = 1;
659 err = view_fullscreen(view);
661 err = view_splitscreen(view);
665 err = view->input(new, dead, focus, view,
672 if (view->search_start)
673 view_search_start(view);
675 err = view->input(new, dead, focus, view, ch);
679 if (view->search_next && view->searching) {
680 view->searching = (ch == 'n' ?
681 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
682 view->search_next_done = 0;
683 view->search_next(view);
685 err = view->input(new, dead, focus, view, ch);
688 err = view->input(new, dead, focus, view, ch);
696 view_vborder(struct tog_view *view)
699 struct tog_view *view_above;
702 return view_vborder(view->parent);
704 panel = panel_above(view->panel);
708 view_above = panel_userptr(panel);
709 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
710 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
714 view_needs_focus_indication(struct tog_view *view)
716 if (view_is_parent_view(view)) {
717 if (view->child == NULL || view->child_focussed)
719 if (!view_is_splitscreen(view->child))
721 } else if (!view_is_splitscreen(view))
724 return view->focussed;
727 static const struct got_error *
728 view_loop(struct tog_view *view)
730 const struct got_error *err = NULL;
731 struct tog_view_list_head views;
732 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
733 int fast_refresh = 10;
734 int done = 0, errcode;
736 errcode = pthread_mutex_lock(&tog_mutex);
738 return got_error_set_errno(errcode, "pthread_mutex_lock");
741 TAILQ_INSERT_HEAD(&views, view, entry);
745 err = view->show(view);
750 while (!TAILQ_EMPTY(&views) && !done) {
751 /* Refresh fast during initialization, then become slower. */
752 if (fast_refresh && fast_refresh-- == 0)
753 halfdelay(10); /* switch to once per second */
755 err = view_input(&new_view, &dead_view, &focus_view, &done,
760 struct tog_view *prev = NULL;
762 if (view_is_parent_view(dead_view))
763 prev = TAILQ_PREV(dead_view,
764 tog_view_list_head, entry);
765 else if (view->parent != dead_view)
768 if (dead_view->parent)
769 dead_view->parent->child = NULL;
771 TAILQ_REMOVE(&views, dead_view, entry);
773 err = view_close(dead_view);
774 if (err || (dead_view == main_view && new_view == NULL))
777 if (view == dead_view) {
782 else if (!TAILQ_EMPTY(&views))
783 view = TAILQ_LAST(&views,
788 if (view->child && view->child_focussed)
789 focus_view = view->child;
796 struct tog_view *v, *t;
797 /* Only allow one parent view per type. */
798 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
799 if (v->type != new_view->type)
801 TAILQ_REMOVE(&views, v, entry);
807 TAILQ_INSERT_TAIL(&views, new_view, entry);
809 if (focus_view == NULL)
810 focus_view = new_view;
813 show_panel(focus_view->panel);
816 focus_view->focussed = 1;
819 show_panel(new_view->panel);
820 if (view->child && view_is_splitscreen(view->child))
821 show_panel(view->child->panel);
824 if (focus_view == NULL) {
826 show_panel(view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
832 err = view->parent->show(view->parent);
836 err = view->show(view);
840 err = view->child->show(view->child);
849 while (!TAILQ_EMPTY(&views)) {
850 view = TAILQ_FIRST(&views);
851 TAILQ_REMOVE(&views, view, entry);
855 errcode = pthread_mutex_unlock(&tog_mutex);
857 return got_error_set_errno(errcode, "pthread_mutex_unlock");
867 "usage: %s log [-c commit] [-r repository-path] [path]\n",
872 /* Create newly allocated wide-character string equivalent to a byte string. */
873 static const struct got_error *
874 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
877 const struct got_error *err = NULL;
880 *wlen = mbstowcs(NULL, s, 0);
881 if (*wlen == (size_t)-1) {
884 return got_error_from_errno("mbstowcs");
886 /* byte string invalid in current encoding; try to "fix" it */
887 err = got_mbsavis(&vis, &vislen, s);
890 *wlen = mbstowcs(NULL, vis, 0);
891 if (*wlen == (size_t)-1) {
892 err = got_error_from_errno("mbstowcs"); /* give up */
897 *ws = calloc(*wlen + 1, sizeof(*ws));
899 err = got_error_from_errno("calloc");
903 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
904 err = got_error_from_errno("mbstowcs");
915 /* Format a line for display, ensuring that it won't overflow a width limit. */
916 static const struct got_error *
917 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
919 const struct got_error *err = NULL;
921 wchar_t *wline = NULL;
928 err = mbs2ws(&wline, &wlen, line);
933 while (i < wlen && cols < wlimit) {
934 int width = wcwidth(wline[i]);
941 if (cols + width <= wlimit)
946 if (wline[i] == L'\t')
947 cols += TABSIZE - ((cols + 1) % TABSIZE);
951 err = got_error_from_errno("wcwidth");
966 static const struct got_error*
967 build_refs_str(char **refs_str, struct got_reflist_head *refs,
968 struct got_object_id *id)
970 static const struct got_error *err = NULL;
971 struct got_reflist_entry *re;
977 SIMPLEQ_FOREACH(re, refs, entry) {
978 if (got_object_id_cmp(re->id, id) != 0)
980 name = got_ref_get_name(re->ref);
981 if (strcmp(name, GOT_REF_HEAD) == 0)
983 if (strncmp(name, "refs/", 5) == 0)
985 if (strncmp(name, "got/", 4) == 0)
987 if (strncmp(name, "heads/", 6) == 0)
989 if (strncmp(name, "remotes/", 8) == 0)
992 if (asprintf(refs_str, "%s%s%s", s ? s : "",
993 s ? ", " : "", name) == -1) {
994 err = got_error_from_errno("asprintf");
1005 static const struct got_error *
1006 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1008 char *smallerthan, *at;
1010 smallerthan = strchr(author, '<');
1011 if (smallerthan && smallerthan[1] != '\0')
1012 author = smallerthan + 1;
1013 at = strchr(author, '@');
1016 return format_line(wauthor, author_width, author, limit);
1019 static const struct got_error *
1020 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1021 struct got_object_id *id, struct got_reflist_head *refs,
1022 int author_display_cols)
1024 const struct got_error *err = NULL;
1025 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1026 char *logmsg0 = NULL, *logmsg = NULL;
1027 char *author = NULL;
1028 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1029 int author_width, logmsg_width;
1030 char *newline, *line = NULL;
1032 static const size_t date_display_cols = 9;
1033 const int avail = view->ncols;
1035 time_t committer_time;
1037 committer_time = got_object_commit_get_committer_time(commit);
1038 if (localtime_r(&committer_time, &tm) == NULL)
1039 return got_error_from_errno("localtime_r");
1040 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1042 return got_error(GOT_ERR_NO_SPACE);
1044 if (avail < date_display_cols)
1045 limit = MIN(sizeof(datebuf) - 1, avail);
1047 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1048 waddnstr(view->window, datebuf, limit);
1053 author = strdup(got_object_commit_get_author(commit));
1054 if (author == NULL) {
1055 err = got_error_from_errno("strdup");
1058 err = format_author(&wauthor, &author_width, author, avail - col);
1061 waddwstr(view->window, wauthor);
1062 col += author_width;
1063 while (col <= avail && author_width < author_display_cols + 2) {
1064 waddch(view->window, ' ');
1071 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1072 if (logmsg0 == NULL) {
1073 err = got_error_from_errno("strdup");
1077 while (*logmsg == '\n')
1079 newline = strchr(logmsg, '\n');
1082 limit = avail - col;
1083 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1086 waddwstr(view->window, wlogmsg);
1087 col += logmsg_width;
1088 while (col <= avail) {
1089 waddch(view->window, ' ');
1101 static struct commit_queue_entry *
1102 alloc_commit_queue_entry(struct got_commit_object *commit,
1103 struct got_object_id *id)
1105 struct commit_queue_entry *entry;
1107 entry = calloc(1, sizeof(*entry));
1112 entry->commit = commit;
1117 pop_commit(struct commit_queue *commits)
1119 struct commit_queue_entry *entry;
1121 entry = TAILQ_FIRST(&commits->head);
1122 TAILQ_REMOVE(&commits->head, entry, entry);
1123 got_object_commit_close(entry->commit);
1124 commits->ncommits--;
1125 /* Don't free entry->id! It is owned by the commit graph. */
1130 free_commits(struct commit_queue *commits)
1132 while (!TAILQ_EMPTY(&commits->head))
1133 pop_commit(commits);
1136 static const struct got_error *
1137 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1138 int minqueue, struct got_repository *repo, const char *path)
1140 const struct got_error *err = NULL;
1144 * We keep all commits open throughout the lifetime of the log
1145 * view in order to avoid having to re-fetch commits from disk
1146 * while updating the display.
1148 while (nqueued < minqueue) {
1149 struct got_object_id *id;
1150 struct got_commit_object *commit;
1151 struct commit_queue_entry *entry;
1154 err = got_commit_graph_iter_next(&id, graph);
1156 if (err->code != GOT_ERR_ITER_NEED_MORE)
1158 err = got_commit_graph_fetch_commits(graph,
1168 err = got_object_open_as_commit(&commit, repo, id);
1171 entry = alloc_commit_queue_entry(commit, id);
1172 if (entry == NULL) {
1173 err = got_error_from_errno("alloc_commit_queue_entry");
1177 errcode = pthread_mutex_lock(&tog_mutex);
1179 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1183 entry->idx = commits->ncommits;
1184 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1186 commits->ncommits++;
1188 errcode = pthread_mutex_unlock(&tog_mutex);
1189 if (errcode && err == NULL)
1190 err = got_error_set_errno(errcode,
1191 "pthread_mutex_unlock");
1197 static const struct got_error *
1198 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1202 struct got_reference *head_ref;
1206 err = got_ref_open(&head_ref, repo, branch_name, 0);
1210 err = got_ref_resolve(head_id, repo, head_ref);
1211 got_ref_close(head_ref);
1220 static const struct got_error *
1221 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1222 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1223 struct commit_queue *commits, int selected_idx, int limit,
1224 struct got_reflist_head *refs, const char *path, int commits_needed)
1226 const struct got_error *err = NULL;
1227 struct commit_queue_entry *entry;
1229 int ncommits, author_cols = 10;
1230 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1231 char *refs_str = NULL;
1237 if (ncommits == selected_idx) {
1241 entry = TAILQ_NEXT(entry, entry);
1245 if (*selected && !(view->searching && view->search_next_done == 0)) {
1246 err = got_object_id_str(&id_str, (*selected)->id);
1250 err = build_refs_str(&refs_str, refs, (*selected)->id);
1256 if (commits_needed == 0)
1257 halfdelay(10); /* disable fast refresh */
1259 if (asprintf(&ncommits_str, " [%d/%d] %s",
1260 entry ? entry->idx + 1 : 0, commits->ncommits,
1261 commits_needed > 0 ?
1262 (view->searching && view->search_next_done == 0
1263 ? "searching..." : "loading... ") :
1264 (refs_str ? refs_str : "")) == -1) {
1265 err = got_error_from_errno("asprintf");
1269 if (path && strcmp(path, "/") != 0) {
1270 if (asprintf(&header, "commit %s %s%s",
1271 id_str ? id_str : "........................................",
1272 path, ncommits_str) == -1) {
1273 err = got_error_from_errno("asprintf");
1277 } else if (asprintf(&header, "commit %s%s",
1278 id_str ? id_str : "........................................",
1279 ncommits_str) == -1) {
1280 err = got_error_from_errno("asprintf");
1284 err = format_line(&wline, &width, header, view->ncols);
1288 werase(view->window);
1290 if (view_needs_focus_indication(view))
1291 wstandout(view->window);
1292 waddwstr(view->window, wline);
1293 while (width < view->ncols) {
1294 waddch(view->window, ' ');
1297 if (view_needs_focus_indication(view))
1298 wstandend(view->window);
1303 /* Grow author column size if necessary. */
1310 if (ncommits >= limit - 1)
1312 author = strdup(got_object_commit_get_author(entry->commit));
1313 if (author == NULL) {
1314 err = got_error_from_errno("strdup");
1317 err = format_author(&wauthor, &width, author, COLS);
1318 if (author_cols < width)
1319 author_cols = width;
1322 entry = TAILQ_NEXT(entry, entry);
1329 if (ncommits >= limit - 1)
1331 if (ncommits == selected_idx)
1332 wstandout(view->window);
1333 err = draw_commit(view, entry->commit, entry->id, refs,
1335 if (ncommits == selected_idx)
1336 wstandend(view->window);
1341 entry = TAILQ_NEXT(entry, entry);
1354 scroll_up(struct tog_view *view,
1355 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1356 struct commit_queue *commits)
1358 struct commit_queue_entry *entry;
1361 entry = TAILQ_FIRST(&commits->head);
1362 if (*first_displayed_entry == entry)
1365 entry = *first_displayed_entry;
1366 while (entry && nscrolled < maxscroll) {
1367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1369 *first_displayed_entry = entry;
1375 static const struct got_error *
1376 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1377 pthread_cond_t *need_commits)
1382 halfdelay(1); /* fast refresh while loading commits */
1384 while (*commits_needed > 0) {
1388 /* Wake the log thread. */
1389 errcode = pthread_cond_signal(need_commits);
1391 return got_error_set_errno(errcode,
1392 "pthread_cond_signal");
1393 errcode = pthread_mutex_unlock(&tog_mutex);
1395 return got_error_set_errno(errcode,
1396 "pthread_mutex_unlock");
1398 errcode = pthread_mutex_lock(&tog_mutex);
1400 return got_error_set_errno(errcode,
1401 "pthread_mutex_lock");
1403 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1405 * Thread is not done yet; lose a key press
1406 * and let the user retry... this way the GUI
1407 * remains interactive while logging deep paths
1408 * with few commits in history.
1417 static const struct got_error *
1418 scroll_down(struct tog_view *view,
1419 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1420 struct commit_queue_entry **last_displayed_entry,
1421 struct commit_queue *commits, int *log_complete, int *commits_needed,
1422 pthread_cond_t *need_commits)
1424 const struct got_error *err = NULL;
1425 struct commit_queue_entry *pentry;
1428 if (*last_displayed_entry == NULL)
1431 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1432 if (pentry == NULL && !*log_complete) {
1434 * Ask the log thread for required amount of commits
1435 * plus some amount of pre-fetching.
1437 (*commits_needed) += maxscroll + 20;
1438 err = trigger_log_thread(0, commits_needed, log_complete,
1445 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1449 *last_displayed_entry = pentry;
1451 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1454 *first_displayed_entry = pentry;
1455 } while (++nscrolled < maxscroll);
1460 static const struct got_error *
1461 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1462 struct got_commit_object *commit, struct got_object_id *commit_id,
1463 struct tog_view *log_view, struct got_reflist_head *refs,
1464 struct got_repository *repo)
1466 const struct got_error *err;
1467 struct got_object_qid *parent_id;
1468 struct tog_view *diff_view;
1470 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1471 if (diff_view == NULL)
1472 return got_error_from_errno("view_open");
1474 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1475 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1476 commit_id, log_view, refs, repo);
1478 *new_view = diff_view;
1482 static const struct got_error *
1483 tree_view_visit_subtree(struct got_tree_object *subtree,
1484 struct tog_tree_view_state *s)
1486 struct tog_parent_tree *parent;
1488 parent = calloc(1, sizeof(*parent));
1490 return got_error_from_errno("calloc");
1492 parent->tree = s->tree;
1493 parent->first_displayed_entry = s->first_displayed_entry;
1494 parent->selected_entry = s->selected_entry;
1495 parent->selected = s->selected;
1496 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1498 s->entries = got_object_tree_get_entries(s->tree);
1500 s->first_displayed_entry = NULL;
1505 static const struct got_error *
1506 browse_commit_tree(struct tog_view **new_view, int begin_x,
1507 struct commit_queue_entry *entry, const char *path,
1508 struct got_reflist_head *refs, struct got_repository *repo)
1510 const struct got_error *err = NULL;
1511 struct got_tree_object *tree;
1512 struct got_tree_entry *te;
1513 struct tog_tree_view_state *s;
1514 struct tog_view *tree_view;
1515 char *slash, *subpath = NULL;
1518 err = got_object_open_as_tree(&tree, repo,
1519 got_object_commit_get_tree_id(entry->commit));
1523 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1524 if (tree_view == NULL)
1525 return got_error_from_errno("view_open");
1527 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1529 got_object_tree_close(tree);
1532 s = &tree_view->state.tree;
1534 *new_view = tree_view;
1536 /* Walk the path and open corresponding tree objects. */
1539 struct got_object_id *tree_id;
1544 /* Ensure the correct subtree entry is selected. */
1545 slash = strchr(p, '/');
1547 slash = strchr(p, '\0');
1548 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1549 if (strncmp(p, te->name, slash - p) == 0) {
1550 s->selected_entry = te;
1555 if (s->selected_entry == NULL) {
1556 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1559 if (s->tree != s->root)
1560 s->selected++; /* skip '..' */
1562 if (!S_ISDIR(s->selected_entry->mode)) {
1563 /* Jump to this file's entry. */
1564 s->first_displayed_entry = s->selected_entry;
1569 slash = strchr(p, '/');
1571 subpath = strndup(path, slash - path);
1573 subpath = strdup(path);
1574 if (subpath == NULL) {
1575 err = got_error_from_errno("strdup");
1579 err = got_object_id_by_path(&tree_id, repo, entry->id,
1584 err = got_object_open_as_tree(&tree, repo, tree_id);
1589 err = tree_view_visit_subtree(tree, s);
1591 got_object_tree_close(tree);
1606 log_thread(void *arg)
1608 const struct got_error *err = NULL;
1610 struct tog_log_thread_args *a = arg;
1613 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1617 while (!done && !err) {
1618 err = queue_commits(a->graph, a->commits, 1, a->repo,
1621 if (err->code != GOT_ERR_ITER_COMPLETED)
1625 } else if (a->commits_needed > 0)
1626 a->commits_needed--;
1628 errcode = pthread_mutex_lock(&tog_mutex);
1630 err = got_error_set_errno(errcode,
1631 "pthread_mutex_lock");
1633 } else if (*a->quit)
1635 else if (*a->first_displayed_entry == NULL) {
1636 *a->first_displayed_entry =
1637 TAILQ_FIRST(&a->commits->head);
1638 *a->selected_entry = *a->first_displayed_entry;
1642 a->commits_needed = 0;
1643 else if (a->commits_needed == 0) {
1644 errcode = pthread_cond_wait(&a->need_commits,
1647 err = got_error_set_errno(errcode,
1648 "pthread_cond_wait");
1651 errcode = pthread_mutex_unlock(&tog_mutex);
1652 if (errcode && err == NULL)
1653 err = got_error_set_errno(errcode,
1654 "pthread_mutex_unlock");
1656 a->log_complete = 1;
1660 static const struct got_error *
1661 stop_log_thread(struct tog_log_view_state *s)
1663 const struct got_error *err = NULL;
1668 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1670 return got_error_set_errno(errcode,
1671 "pthread_cond_signal");
1672 errcode = pthread_mutex_unlock(&tog_mutex);
1674 return got_error_set_errno(errcode,
1675 "pthread_mutex_unlock");
1676 errcode = pthread_join(s->thread, (void **)&err);
1678 return got_error_set_errno(errcode, "pthread_join");
1679 errcode = pthread_mutex_lock(&tog_mutex);
1681 return got_error_set_errno(errcode,
1682 "pthread_mutex_lock");
1686 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1687 if (errcode && err == NULL)
1688 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1690 if (s->thread_args.repo) {
1691 got_repo_close(s->thread_args.repo);
1692 s->thread_args.repo = NULL;
1695 if (s->thread_args.graph) {
1696 got_commit_graph_close(s->thread_args.graph);
1697 s->thread_args.graph = NULL;
1703 static const struct got_error *
1704 close_log_view(struct tog_view *view)
1706 const struct got_error *err = NULL;
1707 struct tog_log_view_state *s = &view->state.log;
1709 err = stop_log_thread(s);
1710 free_commits(&s->commits);
1711 free(s->in_repo_path);
1712 s->in_repo_path = NULL;
1718 static const struct got_error *
1719 search_start_log_view(struct tog_view *view)
1721 struct tog_log_view_state *s = &view->state.log;
1723 s->matched_entry = NULL;
1724 s->search_entry = NULL;
1729 match_commit(struct got_commit_object *commit, const char *id_str,
1732 regmatch_t regmatch;
1734 if (regexec(regex, got_object_commit_get_author(commit), 1,
1735 ®match, 0) == 0 ||
1736 regexec(regex, got_object_commit_get_committer(commit), 1,
1737 ®match, 0) == 0 ||
1738 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1739 ®match, 0) == 0 ||
1740 regexec(regex, id_str, 1, ®match, 0) == 0)
1746 static const struct got_error *
1747 search_next_log_view(struct tog_view *view)
1749 const struct got_error *err = NULL;
1750 struct tog_log_view_state *s = &view->state.log;
1751 struct commit_queue_entry *entry;
1753 if (!view->searching) {
1754 view->search_next_done = 1;
1758 if (s->search_entry) {
1759 if (wgetch(view->window) == KEY_BACKSPACE) {
1760 view->search_next_done = 1;
1763 if (view->searching == TOG_SEARCH_FORWARD)
1764 entry = TAILQ_NEXT(s->search_entry, entry);
1766 entry = TAILQ_PREV(s->search_entry,
1767 commit_queue_head, entry);
1768 } else if (s->matched_entry) {
1769 if (view->searching == TOG_SEARCH_FORWARD)
1770 entry = TAILQ_NEXT(s->selected_entry, entry);
1772 entry = TAILQ_PREV(s->selected_entry,
1773 commit_queue_head, entry);
1775 if (view->searching == TOG_SEARCH_FORWARD)
1776 entry = TAILQ_FIRST(&s->commits.head);
1778 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1783 if (entry == NULL) {
1784 if (s->thread_args.log_complete ||
1785 view->searching == TOG_SEARCH_BACKWARD) {
1786 view->search_next_done = 1;
1790 * Poke the log thread for more commits and return,
1791 * allowing the main loop to make progress. Search
1792 * will resume at s->search_entry once we come back.
1794 s->thread_args.commits_needed++;
1795 return trigger_log_thread(1,
1796 &s->thread_args.commits_needed,
1797 &s->thread_args.log_complete,
1798 &s->thread_args.need_commits);
1801 err = got_object_id_str(&id_str, entry->id);
1805 if (match_commit(entry->commit, id_str, &view->regex)) {
1806 view->search_next_done = 1;
1807 s->matched_entry = entry;
1812 s->search_entry = entry;
1813 if (view->searching == TOG_SEARCH_FORWARD)
1814 entry = TAILQ_NEXT(entry, entry);
1816 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1819 if (s->matched_entry) {
1820 int cur = s->selected_entry->idx;
1821 while (cur < s->matched_entry->idx) {
1822 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1827 while (cur > s->matched_entry->idx) {
1828 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1835 s->search_entry = NULL;
1840 static const struct got_error *
1841 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1842 struct got_reflist_head *refs, struct got_repository *repo,
1843 const char *head_ref_name, const char *path, int check_disk)
1845 const struct got_error *err = NULL;
1846 struct tog_log_view_state *s = &view->state.log;
1847 struct got_repository *thread_repo = NULL;
1848 struct got_commit_graph *thread_graph = NULL;
1851 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1855 /* The commit queue only contains commits being displayed. */
1856 TAILQ_INIT(&s->commits.head);
1857 s->commits.ncommits = 0;
1861 s->head_ref_name = head_ref_name;
1862 s->start_id = got_object_id_dup(start_id);
1863 if (s->start_id == NULL) {
1864 err = got_error_from_errno("got_object_id_dup");
1868 view->show = show_log_view;
1869 view->input = input_log_view;
1870 view->close = close_log_view;
1871 view->search_start = search_start_log_view;
1872 view->search_next = search_next_log_view;
1874 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1877 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1882 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1884 err = got_error_set_errno(errcode, "pthread_cond_init");
1888 s->thread_args.commits_needed = view->nlines;
1889 s->thread_args.graph = thread_graph;
1890 s->thread_args.commits = &s->commits;
1891 s->thread_args.in_repo_path = s->in_repo_path;
1892 s->thread_args.start_id = s->start_id;
1893 s->thread_args.repo = thread_repo;
1894 s->thread_args.log_complete = 0;
1895 s->thread_args.quit = &s->quit;
1896 s->thread_args.view = view;
1897 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1898 s->thread_args.selected_entry = &s->selected_entry;
1901 close_log_view(view);
1905 static const struct got_error *
1906 show_log_view(struct tog_view *view)
1908 struct tog_log_view_state *s = &view->state.log;
1910 if (s->thread == NULL) {
1911 int errcode = pthread_create(&s->thread, NULL, log_thread,
1914 return got_error_set_errno(errcode, "pthread_create");
1917 return draw_commits(view, &s->last_displayed_entry,
1918 &s->selected_entry, s->first_displayed_entry,
1919 &s->commits, s->selected, view->nlines, s->refs,
1920 s->in_repo_path, s->thread_args.commits_needed);
1923 static const struct got_error *
1924 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1925 struct tog_view **focus_view, struct tog_view *view, int ch)
1927 const struct got_error *err = NULL;
1928 struct tog_log_view_state *s = &view->state.log;
1929 char *parent_path, *in_repo_path = NULL;
1930 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1932 struct got_object_id *start_id;
1942 if (s->first_displayed_entry == NULL)
1944 if (s->selected > 0)
1947 scroll_up(view, &s->first_displayed_entry, 1,
1952 if (s->first_displayed_entry == NULL)
1954 if (TAILQ_FIRST(&s->commits.head) ==
1955 s->first_displayed_entry) {
1959 scroll_up(view, &s->first_displayed_entry,
1960 view->nlines, &s->commits);
1966 if (s->first_displayed_entry == NULL)
1968 if (s->selected < MIN(view->nlines - 2,
1969 s->commits.ncommits - 1)) {
1973 err = scroll_down(view, &s->first_displayed_entry, 1,
1974 &s->last_displayed_entry, &s->commits,
1975 &s->thread_args.log_complete,
1976 &s->thread_args.commits_needed,
1977 &s->thread_args.need_commits);
1981 struct commit_queue_entry *first;
1982 first = s->first_displayed_entry;
1985 err = scroll_down(view, &s->first_displayed_entry,
1986 view->nlines, &s->last_displayed_entry,
1987 &s->commits, &s->thread_args.log_complete,
1988 &s->thread_args.commits_needed,
1989 &s->thread_args.need_commits);
1990 if (first == s->first_displayed_entry &&
1991 s->selected < MIN(view->nlines - 2,
1992 s->commits.ncommits - 1)) {
1993 /* can't scroll further down */
1994 s->selected = MIN(view->nlines - 2,
1995 s->commits.ncommits - 1);
2001 if (s->selected > view->nlines - 2)
2002 s->selected = view->nlines - 2;
2003 if (s->selected > s->commits.ncommits - 1)
2004 s->selected = s->commits.ncommits - 1;
2009 if (s->selected_entry == NULL)
2011 if (view_is_parent_view(view))
2012 begin_x = view_split_begin_x(view->begin_x);
2013 err = open_diff_view_for_commit(&diff_view, begin_x,
2014 s->selected_entry->commit, s->selected_entry->id,
2015 view, s->refs, s->repo);
2018 if (view_is_parent_view(view)) {
2019 err = view_close_child(view);
2022 err = view_set_child(view, diff_view);
2024 view_close(diff_view);
2027 *focus_view = diff_view;
2028 view->child_focussed = 1;
2030 *new_view = diff_view;
2033 if (s->selected_entry == NULL)
2035 if (view_is_parent_view(view))
2036 begin_x = view_split_begin_x(view->begin_x);
2037 err = browse_commit_tree(&tree_view, begin_x,
2038 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2041 if (view_is_parent_view(view)) {
2042 err = view_close_child(view);
2045 err = view_set_child(view, tree_view);
2047 view_close(tree_view);
2050 *focus_view = tree_view;
2051 view->child_focussed = 1;
2053 *new_view = tree_view;
2056 if (strcmp(s->in_repo_path, "/") == 0)
2058 parent_path = dirname(s->in_repo_path);
2059 if (parent_path && strcmp(parent_path, ".") != 0) {
2060 err = stop_log_thread(s);
2063 lv = view_open(view->nlines, view->ncols,
2064 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2066 return got_error_from_errno(
2068 err = open_log_view(lv, s->start_id, s->refs,
2069 s->repo, s->head_ref_name, parent_path, 0);
2072 if (view_is_parent_view(view))
2075 view_set_child(view->parent, lv);
2082 err = stop_log_thread(s);
2085 lv = view_open(view->nlines, view->ncols,
2086 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2088 return got_error_from_errno("view_open");
2089 err = get_head_commit_id(&start_id, s->head_ref_name ?
2090 s->head_ref_name : GOT_REF_HEAD, s->repo);
2095 in_repo_path = strdup(s->in_repo_path);
2096 if (in_repo_path == NULL) {
2099 return got_error_from_errno("strdup");
2101 err = open_log_view(lv, start_id, s->refs, s->repo,
2102 s->head_ref_name, in_repo_path, 0);
2118 static const struct got_error *
2119 apply_unveil(const char *repo_path, const char *worktree_path)
2121 const struct got_error *error;
2124 if (unveil("gmon.out", "rwc") != 0)
2125 return got_error_from_errno2("unveil", "gmon.out");
2127 if (repo_path && unveil(repo_path, "r") != 0)
2128 return got_error_from_errno2("unveil", repo_path);
2130 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2131 return got_error_from_errno2("unveil", worktree_path);
2133 if (unveil("/tmp", "rwc") != 0)
2134 return got_error_from_errno2("unveil", "/tmp");
2136 error = got_privsep_unveil_exec_helpers();
2140 if (unveil(NULL, NULL) != 0)
2141 return got_error_from_errno("unveil");
2151 halfdelay(1); /* Do fast refresh while initial view is loading. */
2154 intrflush(stdscr, FALSE);
2155 keypad(stdscr, TRUE);
2157 signal(SIGWINCH, tog_sigwinch);
2160 static const struct got_error *
2161 cmd_log(int argc, char *argv[])
2163 const struct got_error *error;
2164 struct got_repository *repo = NULL;
2165 struct got_worktree *worktree = NULL;
2166 struct got_reflist_head refs;
2167 struct got_object_id *start_id = NULL;
2168 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2169 char *start_commit = NULL;
2171 struct tog_view *view;
2173 SIMPLEQ_INIT(&refs);
2176 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2181 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2184 start_commit = optarg;
2187 repo_path = realpath(optarg, NULL);
2188 if (repo_path == NULL)
2189 err(1, "-r option");
2200 cwd = getcwd(NULL, 0);
2202 error = got_error_from_errno("getcwd");
2205 error = got_worktree_open(&worktree, cwd);
2206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2213 error = got_error_from_errno("strdup");
2216 } else if (argc == 1) {
2218 error = got_worktree_resolve_path(&path, worktree,
2223 path = strdup(argv[0]);
2225 error = got_error_from_errno("strdup");
2232 if (repo_path == NULL) {
2234 repo_path = strdup(got_worktree_get_repo_path(worktree));
2236 repo_path = strdup(cwd);
2238 if (repo_path == NULL) {
2239 error = got_error_from_errno("strdup");
2245 error = got_repo_open(&repo, repo_path);
2249 error = apply_unveil(got_repo_get_path(repo),
2250 worktree ? got_worktree_get_root_path(worktree) : NULL);
2254 if (start_commit == NULL)
2255 error = get_head_commit_id(&start_id, worktree ?
2256 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2259 error = get_head_commit_id(&start_id, start_commit, repo);
2261 if (error->code != GOT_ERR_NOT_REF)
2263 error = got_repo_match_object_id_prefix(&start_id,
2264 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2270 error = got_ref_list(&refs, repo);
2274 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2276 error = got_error_from_errno("view_open");
2279 error = open_log_view(view, start_id, &refs, repo, worktree ?
2280 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2283 error = view_loop(view);
2290 got_repo_close(repo);
2292 got_worktree_close(worktree);
2293 got_ref_list_free(&refs);
2301 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2307 parse_next_line(FILE *f, size_t *len)
2312 const char delim[3] = { '\0', '\0', '\0'};
2314 line = fparseln(f, &linelen, &lineno, delim, 0);
2320 static const struct got_error *
2321 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2322 int *last_displayed_line, int *eof, int max_lines,
2325 const struct got_error *err;
2326 int nlines = 0, nprinted = 0;
2333 werase(view->window);
2336 err = format_line(&wline, &width, header, view->ncols);
2341 if (view_needs_focus_indication(view))
2342 wstandout(view->window);
2343 waddwstr(view->window, wline);
2344 if (view_needs_focus_indication(view))
2345 wstandend(view->window);
2346 if (width < view->ncols - 1)
2347 waddch(view->window, '\n');
2355 while (nprinted < max_lines) {
2356 line = parse_next_line(f, &len);
2361 if (++nlines < *first_displayed_line) {
2366 err = format_line(&wline, &width, line, view->ncols);
2371 waddwstr(view->window, wline);
2372 if (width < view->ncols - 1)
2373 waddch(view->window, '\n');
2374 if (++nprinted == 1)
2375 *first_displayed_line = nlines;
2380 *last_displayed_line = nlines;
2385 while (nprinted < view->nlines) {
2386 waddch(view->window, '\n');
2390 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2395 wstandout(view->window);
2396 waddwstr(view->window, wline);
2397 wstandend(view->window);
2404 get_datestr(time_t *time, char *datebuf)
2406 char *p, *s = ctime_r(time, datebuf);
2407 p = strchr(s, '\n');
2413 static const struct got_error *
2414 write_commit_info(struct got_object_id *commit_id,
2415 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2417 const struct got_error *err = NULL;
2419 struct got_commit_object *commit;
2420 char *id_str = NULL;
2421 time_t committer_time;
2422 const char *author, *committer;
2423 char *refs_str = NULL;
2426 err = build_refs_str(&refs_str, refs, commit_id);
2431 err = got_object_open_as_commit(&commit, repo, commit_id);
2435 err = got_object_id_str(&id_str, commit_id);
2437 err = got_error_from_errno("got_object_id_str");
2441 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2442 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2443 err = got_error_from_errno("fprintf");
2446 if (fprintf(outfile, "from: %s\n",
2447 got_object_commit_get_author(commit)) < 0) {
2448 err = got_error_from_errno("fprintf");
2451 committer_time = got_object_commit_get_committer_time(commit);
2452 if (fprintf(outfile, "date: %s UTC\n",
2453 get_datestr(&committer_time, datebuf)) < 0) {
2454 err = got_error_from_errno("fprintf");
2457 author = got_object_commit_get_author(commit);
2458 committer = got_object_commit_get_committer(commit);
2459 if (strcmp(author, committer) != 0 &&
2460 fprintf(outfile, "via: %s\n", committer) < 0) {
2461 err = got_error_from_errno("fprintf");
2464 if (fprintf(outfile, "%s\n",
2465 got_object_commit_get_logmsg(commit)) < 0) {
2466 err = got_error_from_errno("fprintf");
2472 got_object_commit_close(commit);
2476 static const struct got_error *
2477 create_diff(struct tog_diff_view_state *s)
2479 const struct got_error *err = NULL;
2485 err = got_error_from_errno("got_opentemp");
2488 if (s->f && fclose(s->f) != 0) {
2489 err = got_error_from_errno("fclose");
2495 err = got_object_get_type(&obj_type, s->repo, s->id1);
2497 err = got_object_get_type(&obj_type, s->repo, s->id2);
2502 case GOT_OBJ_TYPE_BLOB:
2503 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2504 s->diff_context, s->repo, f);
2506 case GOT_OBJ_TYPE_TREE:
2507 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2508 s->diff_context, s->repo, f);
2510 case GOT_OBJ_TYPE_COMMIT: {
2511 const struct got_object_id_queue *parent_ids;
2512 struct got_object_qid *pid;
2513 struct got_commit_object *commit2;
2515 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2518 /* Show commit info if we're diffing to a parent/root commit. */
2520 write_commit_info(s->id2, s->refs, s->repo, f);
2522 parent_ids = got_object_commit_get_parent_ids(commit2);
2523 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2524 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2525 write_commit_info(s->id2, s->refs,
2531 got_object_commit_close(commit2);
2533 err = got_diff_objects_as_commits(s->id1, s->id2,
2534 s->diff_context, s->repo, f);
2538 err = got_error(GOT_ERR_OBJ_TYPE);
2542 if (f && fflush(f) != 0 && err == NULL)
2543 err = got_error_from_errno("fflush");
2548 diff_view_indicate_progress(struct tog_view *view)
2550 mvwaddstr(view->window, 0, 0, "diffing...");
2555 static const struct got_error *
2556 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2557 struct got_object_id *id2, struct tog_view *log_view,
2558 struct got_reflist_head *refs, struct got_repository *repo)
2560 const struct got_error *err;
2562 if (id1 != NULL && id2 != NULL) {
2564 err = got_object_get_type(&type1, repo, id1);
2567 err = got_object_get_type(&type2, repo, id2);
2572 return got_error(GOT_ERR_OBJ_TYPE);
2576 view->state.diff.id1 = got_object_id_dup(id1);
2577 if (view->state.diff.id1 == NULL)
2578 return got_error_from_errno("got_object_id_dup");
2580 view->state.diff.id1 = NULL;
2582 view->state.diff.id2 = got_object_id_dup(id2);
2583 if (view->state.diff.id2 == NULL) {
2584 free(view->state.diff.id1);
2585 view->state.diff.id1 = NULL;
2586 return got_error_from_errno("got_object_id_dup");
2588 view->state.diff.f = NULL;
2589 view->state.diff.first_displayed_line = 1;
2590 view->state.diff.last_displayed_line = view->nlines;
2591 view->state.diff.diff_context = 3;
2592 view->state.diff.log_view = log_view;
2593 view->state.diff.repo = repo;
2594 view->state.diff.refs = refs;
2596 if (log_view && view_is_splitscreen(view))
2597 show_log_view(log_view); /* draw vborder */
2598 diff_view_indicate_progress(view);
2600 err = create_diff(&view->state.diff);
2602 free(view->state.diff.id1);
2603 view->state.diff.id1 = NULL;
2604 free(view->state.diff.id2);
2605 view->state.diff.id2 = NULL;
2609 view->show = show_diff_view;
2610 view->input = input_diff_view;
2611 view->close = close_diff_view;
2616 static const struct got_error *
2617 close_diff_view(struct tog_view *view)
2619 const struct got_error *err = NULL;
2621 free(view->state.diff.id1);
2622 view->state.diff.id1 = NULL;
2623 free(view->state.diff.id2);
2624 view->state.diff.id2 = NULL;
2625 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2626 err = got_error_from_errno("fclose");
2630 static const struct got_error *
2631 show_diff_view(struct tog_view *view)
2633 const struct got_error *err;
2634 struct tog_diff_view_state *s = &view->state.diff;
2635 char *id_str1 = NULL, *id_str2, *header;
2638 err = got_object_id_str(&id_str1, s->id1);
2642 err = got_object_id_str(&id_str2, s->id2);
2646 if (asprintf(&header, "diff %s %s",
2647 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2648 err = got_error_from_errno("asprintf");
2656 return draw_file(view, s->f, &s->first_displayed_line,
2657 &s->last_displayed_line, &s->eof, view->nlines,
2661 static const struct got_error *
2662 set_selected_commit(struct tog_diff_view_state *s,
2663 struct commit_queue_entry *entry)
2665 const struct got_error *err;
2666 const struct got_object_id_queue *parent_ids;
2667 struct got_commit_object *selected_commit;
2668 struct got_object_qid *pid;
2671 s->id2 = got_object_id_dup(entry->id);
2673 return got_error_from_errno("got_object_id_dup");
2675 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2678 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2680 pid = SIMPLEQ_FIRST(parent_ids);
2681 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2682 got_object_commit_close(selected_commit);
2686 static const struct got_error *
2687 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2688 struct tog_view **focus_view, struct tog_view *view, int ch)
2690 const struct got_error *err = NULL;
2691 struct tog_diff_view_state *s = &view->state.diff;
2692 struct tog_log_view_state *ls;
2693 struct commit_queue_entry *entry;
2699 if (s->first_displayed_line > 1)
2700 s->first_displayed_line--;
2704 if (s->first_displayed_line == 1)
2707 while (i++ < view->nlines - 1 &&
2708 s->first_displayed_line > 1)
2709 s->first_displayed_line--;
2714 s->first_displayed_line++;
2722 while (!s->eof && i++ < view->nlines - 1) {
2724 line = parse_next_line(s->f, NULL);
2725 s->first_displayed_line++;
2731 if (s->diff_context > 0) {
2733 diff_view_indicate_progress(view);
2734 err = create_diff(s);
2738 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2740 diff_view_indicate_progress(view);
2741 err = create_diff(s);
2746 if (s->log_view == NULL)
2748 ls = &s->log_view->state.log;
2749 entry = TAILQ_PREV(ls->selected_entry,
2750 commit_queue_head, entry);
2754 err = input_log_view(NULL, NULL, NULL, s->log_view,
2759 err = set_selected_commit(s, entry);
2763 s->first_displayed_line = 1;
2764 s->last_displayed_line = view->nlines;
2766 diff_view_indicate_progress(view);
2767 err = create_diff(s);
2771 if (s->log_view == NULL)
2773 ls = &s->log_view->state.log;
2775 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2776 ls->thread_args.commits_needed++;
2778 /* Display "loading..." in log view. */
2779 show_log_view(s->log_view);
2783 err = trigger_log_thread(1 /* load_all */,
2784 &ls->thread_args.commits_needed,
2785 &ls->thread_args.log_complete,
2786 &ls->thread_args.need_commits);
2790 err = input_log_view(NULL, NULL, NULL, s->log_view,
2795 entry = TAILQ_NEXT(ls->selected_entry, entry);
2799 err = set_selected_commit(s, entry);
2803 s->first_displayed_line = 1;
2804 s->last_displayed_line = view->nlines;
2806 diff_view_indicate_progress(view);
2807 err = create_diff(s);
2816 static const struct got_error *
2817 cmd_diff(int argc, char *argv[])
2819 const struct got_error *error = NULL;
2820 struct got_repository *repo = NULL;
2821 struct got_reflist_head refs;
2822 struct got_object_id *id1 = NULL, *id2 = NULL;
2823 char *repo_path = NULL;
2824 char *id_str1 = NULL, *id_str2 = NULL;
2826 struct tog_view *view;
2828 SIMPLEQ_INIT(&refs);
2831 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2836 while ((ch = getopt(argc, argv, "")) != -1) {
2848 usage_diff(); /* TODO show local worktree changes */
2849 } else if (argc == 2) {
2850 repo_path = getcwd(NULL, 0);
2851 if (repo_path == NULL)
2852 return got_error_from_errno("getcwd");
2855 } else if (argc == 3) {
2856 repo_path = realpath(argv[0], NULL);
2857 if (repo_path == NULL)
2858 return got_error_from_errno2("realpath", argv[0]);
2866 error = got_repo_open(&repo, repo_path);
2870 error = apply_unveil(got_repo_get_path(repo), NULL);
2874 error = got_repo_match_object_id_prefix(&id1, id_str1,
2875 GOT_OBJ_TYPE_ANY, repo);
2879 error = got_repo_match_object_id_prefix(&id2, id_str2,
2880 GOT_OBJ_TYPE_ANY, repo);
2884 error = got_ref_list(&refs, repo);
2888 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2890 error = got_error_from_errno("view_open");
2893 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2896 error = view_loop(view);
2900 got_repo_close(repo);
2901 got_ref_list_free(&refs);
2909 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2914 struct tog_blame_line {
2916 struct got_object_id *id;
2919 static const struct got_error *
2920 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2921 const char *path, struct tog_blame_line *lines, int nlines,
2922 int blame_complete, int selected_line, int *first_displayed_line,
2923 int *last_displayed_line, int *eof, int max_lines)
2925 const struct got_error *err;
2926 int lineno = 0, nprinted = 0;
2931 struct tog_blame_line *blame_line;
2932 struct got_object_id *prev_id = NULL;
2935 err = got_object_id_str(&id_str, id);
2940 werase(view->window);
2942 if (asprintf(&line, "commit %s", id_str) == -1) {
2943 err = got_error_from_errno("asprintf");
2948 err = format_line(&wline, &width, line, view->ncols);
2951 if (view_needs_focus_indication(view))
2952 wstandout(view->window);
2953 waddwstr(view->window, wline);
2954 if (view_needs_focus_indication(view))
2955 wstandend(view->window);
2958 if (width < view->ncols - 1)
2959 waddch(view->window, '\n');
2961 if (asprintf(&line, "[%d/%d] %s%s",
2962 *first_displayed_line - 1 + selected_line, nlines,
2963 blame_complete ? "" : "annotating... ", path) == -1) {
2965 return got_error_from_errno("asprintf");
2968 err = format_line(&wline, &width, line, view->ncols);
2973 waddwstr(view->window, wline);
2976 if (width < view->ncols - 1)
2977 waddch(view->window, '\n');
2980 while (nprinted < max_lines - 2) {
2981 line = parse_next_line(f, &len);
2986 if (++lineno < *first_displayed_line) {
2991 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2992 err = format_line(&wline, &width, line, wlimit);
2998 if (view->focussed && nprinted == selected_line - 1)
2999 wstandout(view->window);
3001 blame_line = &lines[lineno - 1];
3002 if (blame_line->annotated && prev_id &&
3003 got_object_id_cmp(prev_id, blame_line->id) == 0)
3004 waddstr(view->window, " ");
3005 else if (blame_line->annotated) {
3007 err = got_object_id_str(&id_str, blame_line->id);
3013 wprintw(view->window, "%.8s ", id_str);
3015 prev_id = blame_line->id;
3017 waddstr(view->window, "........ ");
3021 waddwstr(view->window, wline);
3022 while (width < wlimit) {
3023 waddch(view->window, ' ');
3026 if (view->focussed && nprinted == selected_line - 1)
3027 wstandend(view->window);
3028 if (++nprinted == 1)
3029 *first_displayed_line = lineno;
3034 *last_displayed_line = lineno;
3041 static const struct got_error *
3042 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3044 const struct got_error *err = NULL;
3045 struct tog_blame_cb_args *a = arg;
3046 struct tog_blame_line *line;
3049 if (nlines != a->nlines ||
3050 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3051 return got_error(GOT_ERR_RANGE);
3053 errcode = pthread_mutex_lock(&tog_mutex);
3055 return got_error_set_errno(errcode, "pthread_mutex_lock");
3057 if (*a->quit) { /* user has quit the blame view */
3058 err = got_error(GOT_ERR_ITER_COMPLETED);
3063 goto done; /* no change in this commit */
3065 line = &a->lines[lineno - 1];
3066 if (line->annotated)
3069 line->id = got_object_id_dup(id);
3070 if (line->id == NULL) {
3071 err = got_error_from_errno("got_object_id_dup");
3074 line->annotated = 1;
3076 errcode = pthread_mutex_unlock(&tog_mutex);
3078 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3083 blame_thread(void *arg)
3085 const struct got_error *err;
3086 struct tog_blame_thread_args *ta = arg;
3087 struct tog_blame_cb_args *a = ta->cb_args;
3090 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3091 blame_cb, ta->cb_args);
3093 errcode = pthread_mutex_lock(&tog_mutex);
3095 return (void *)got_error_set_errno(errcode,
3096 "pthread_mutex_lock");
3098 got_repo_close(ta->repo);
3102 errcode = pthread_mutex_unlock(&tog_mutex);
3103 if (errcode && err == NULL)
3104 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3109 static struct got_object_id *
3110 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3113 struct tog_blame_line *line;
3115 line = &lines[first_displayed_line - 1 + selected_line - 1];
3116 if (!line->annotated)
3122 static const struct got_error *
3123 stop_blame(struct tog_blame *blame)
3125 const struct got_error *err = NULL;
3128 if (blame->thread) {
3130 errcode = pthread_mutex_unlock(&tog_mutex);
3132 return got_error_set_errno(errcode,
3133 "pthread_mutex_unlock");
3134 errcode = pthread_join(blame->thread, (void **)&err);
3136 return got_error_set_errno(errcode, "pthread_join");
3137 errcode = pthread_mutex_lock(&tog_mutex);
3139 return got_error_set_errno(errcode,
3140 "pthread_mutex_lock");
3141 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3143 blame->thread = NULL;
3145 if (blame->thread_args.repo) {
3146 got_repo_close(blame->thread_args.repo);
3147 blame->thread_args.repo = NULL;
3150 if (fclose(blame->f) != 0 && err == NULL)
3151 err = got_error_from_errno("fclose");
3155 for (i = 0; i < blame->nlines; i++)
3156 free(blame->lines[i].id);
3158 blame->lines = NULL;
3160 free(blame->cb_args.commit_id);
3161 blame->cb_args.commit_id = NULL;
3166 static const struct got_error *
3167 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3168 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3169 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3170 struct got_repository *repo)
3172 const struct got_error *err = NULL;
3173 struct got_blob_object *blob = NULL;
3174 struct got_repository *thread_repo = NULL;
3175 struct got_object_id *obj_id = NULL;
3178 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3182 return got_error(GOT_ERR_NO_OBJ);
3184 err = got_object_get_type(&obj_type, repo, obj_id);
3188 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3189 err = got_error(GOT_ERR_OBJ_TYPE);
3193 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3196 blame->f = got_opentemp();
3197 if (blame->f == NULL) {
3198 err = got_error_from_errno("got_opentemp");
3201 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3202 &blame->line_offsets, blame->f, blob);
3206 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3207 if (blame->lines == NULL) {
3208 err = got_error_from_errno("calloc");
3212 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3216 blame->cb_args.view = view;
3217 blame->cb_args.lines = blame->lines;
3218 blame->cb_args.nlines = blame->nlines;
3219 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3220 if (blame->cb_args.commit_id == NULL) {
3221 err = got_error_from_errno("got_object_id_dup");
3224 blame->cb_args.quit = done;
3226 blame->thread_args.path = path;
3227 blame->thread_args.repo = thread_repo;
3228 blame->thread_args.cb_args = &blame->cb_args;
3229 blame->thread_args.complete = blame_complete;
3230 *blame_complete = 0;
3234 got_object_blob_close(blob);
3241 static const struct got_error *
3242 open_blame_view(struct tog_view *view, char *path,
3243 struct got_object_id *commit_id, struct got_reflist_head *refs,
3244 struct got_repository *repo)
3246 const struct got_error *err = NULL;
3247 struct tog_blame_view_state *s = &view->state.blame;
3249 SIMPLEQ_INIT(&s->blamed_commits);
3251 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3255 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3256 s->first_displayed_line = 1;
3257 s->last_displayed_line = view->nlines;
3258 s->selected_line = 1;
3259 s->blame_complete = 0;
3261 if (s->path == NULL)
3262 return got_error_from_errno("open_blame_view");
3265 s->commit_id = commit_id;
3266 memset(&s->blame, 0, sizeof(s->blame));
3268 view->show = show_blame_view;
3269 view->input = input_blame_view;
3270 view->close = close_blame_view;
3271 view->search_start = search_start_blame_view;
3272 view->search_next = search_next_blame_view;
3274 return run_blame(&s->blame, view, &s->blame_complete,
3275 &s->first_displayed_line, &s->last_displayed_line,
3276 &s->selected_line, &s->done, &s->eof, s->path,
3277 s->blamed_commit->id, s->repo);
3280 static const struct got_error *
3281 close_blame_view(struct tog_view *view)
3283 const struct got_error *err = NULL;
3284 struct tog_blame_view_state *s = &view->state.blame;
3286 if (s->blame.thread)
3287 err = stop_blame(&s->blame);
3289 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3290 struct got_object_qid *blamed_commit;
3291 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3292 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3293 got_object_qid_free(blamed_commit);
3301 static const struct got_error *
3302 search_start_blame_view(struct tog_view *view)
3304 struct tog_blame_view_state *s = &view->state.blame;
3306 s->matched_line = 0;
3311 match_line(const char *line, regex_t *regex)
3313 regmatch_t regmatch;
3315 return regexec(regex, line, 1, ®match, 0) == 0;
3319 static const struct got_error *
3320 search_next_blame_view(struct tog_view *view)
3322 struct tog_blame_view_state *s = &view->state.blame;
3325 if (!view->searching) {
3326 view->search_next_done = 1;
3330 if (s->matched_line) {
3331 if (view->searching == TOG_SEARCH_FORWARD)
3332 lineno = s->matched_line + 1;
3334 lineno = s->matched_line - 1;
3336 if (view->searching == TOG_SEARCH_FORWARD)
3339 lineno = s->blame.nlines;
3347 if (lineno <= 0 || lineno > s->blame.nlines) {
3348 if (s->matched_line == 0) {
3349 view->search_next_done = 1;
3354 if (view->searching == TOG_SEARCH_FORWARD)
3357 lineno = s->blame.nlines;
3360 offset = s->blame.line_offsets[lineno - 1];
3361 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3363 return got_error_from_errno("fseeko");
3366 line = parse_next_line(s->blame.f, &len);
3367 if (line && match_line(line, &view->regex)) {
3368 view->search_next_done = 1;
3369 s->matched_line = lineno;
3374 if (view->searching == TOG_SEARCH_FORWARD)
3380 if (s->matched_line) {
3381 s->first_displayed_line = s->matched_line;
3382 s->selected_line = 1;
3388 static const struct got_error *
3389 show_blame_view(struct tog_view *view)
3391 const struct got_error *err = NULL;
3392 struct tog_blame_view_state *s = &view->state.blame;
3395 if (s->blame.thread == NULL) {
3396 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3397 &s->blame.thread_args);
3399 return got_error_set_errno(errcode, "pthread_create");
3402 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3403 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3404 s->selected_line, &s->first_displayed_line,
3405 &s->last_displayed_line, &s->eof, view->nlines);
3411 static const struct got_error *
3412 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3413 struct tog_view **focus_view, struct tog_view *view, int ch)
3415 const struct got_error *err = NULL, *thread_err = NULL;
3416 struct tog_view *diff_view;
3417 struct tog_blame_view_state *s = &view->state.blame;
3426 if (s->selected_line > 1)
3428 else if (s->selected_line == 1 &&
3429 s->first_displayed_line > 1)
3430 s->first_displayed_line--;
3433 if (s->first_displayed_line == 1) {
3434 s->selected_line = 1;
3437 if (s->first_displayed_line > view->nlines - 2)
3438 s->first_displayed_line -=
3441 s->first_displayed_line = 1;
3445 if (s->selected_line < view->nlines - 2 &&
3446 s->first_displayed_line +
3447 s->selected_line <= s->blame.nlines)
3449 else if (s->last_displayed_line <
3451 s->first_displayed_line++;
3455 struct got_object_id *id = NULL;
3456 id = get_selected_commit_id(s->blame.lines,
3457 s->first_displayed_line, s->selected_line);
3461 struct got_commit_object *commit;
3462 struct got_object_qid *pid;
3463 struct got_object_id *blob_id = NULL;
3465 err = got_object_open_as_commit(&commit,
3469 pid = SIMPLEQ_FIRST(
3470 got_object_commit_get_parent_ids(commit));
3472 got_object_commit_close(commit);
3475 /* Check if path history ends here. */
3476 err = got_object_id_by_path(&blob_id, s->repo,
3479 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3481 got_object_commit_close(commit);
3484 err = got_object_get_type(&obj_type, s->repo,
3487 /* Can't blame non-blob type objects. */
3488 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3489 got_object_commit_close(commit);
3492 err = got_object_qid_alloc(&s->blamed_commit,
3494 got_object_commit_close(commit);
3496 if (got_object_id_cmp(id,
3497 s->blamed_commit->id) == 0)
3499 err = got_object_qid_alloc(&s->blamed_commit,
3505 thread_err = stop_blame(&s->blame);
3509 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3510 s->blamed_commit, entry);
3511 err = run_blame(&s->blame, view, &s->blame_complete,
3512 &s->first_displayed_line, &s->last_displayed_line,
3513 &s->selected_line, &s->done, &s->eof,
3514 s->path, s->blamed_commit->id, s->repo);
3520 struct got_object_qid *first;
3521 first = SIMPLEQ_FIRST(&s->blamed_commits);
3522 if (!got_object_id_cmp(first->id, s->commit_id))
3525 thread_err = stop_blame(&s->blame);
3529 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3530 got_object_qid_free(s->blamed_commit);
3532 SIMPLEQ_FIRST(&s->blamed_commits);
3533 err = run_blame(&s->blame, view, &s->blame_complete,
3534 &s->first_displayed_line, &s->last_displayed_line,
3535 &s->selected_line, &s->done, &s->eof, s->path,
3536 s->blamed_commit->id, s->repo);
3543 struct got_object_id *id = NULL;
3544 struct got_object_qid *pid;
3545 struct got_commit_object *commit = NULL;
3546 id = get_selected_commit_id(s->blame.lines,
3547 s->first_displayed_line, s->selected_line);
3550 err = got_object_open_as_commit(&commit, s->repo, id);
3553 pid = SIMPLEQ_FIRST(
3554 got_object_commit_get_parent_ids(commit));
3555 if (view_is_parent_view(view))
3556 begin_x = view_split_begin_x(view->begin_x);
3557 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3558 if (diff_view == NULL) {
3559 got_object_commit_close(commit);
3560 err = got_error_from_errno("view_open");
3563 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3564 id, NULL, s->refs, s->repo);
3565 got_object_commit_close(commit);
3567 view_close(diff_view);
3570 if (view_is_parent_view(view)) {
3571 err = view_close_child(view);
3574 err = view_set_child(view, diff_view);
3576 view_close(diff_view);
3579 *focus_view = diff_view;
3580 view->child_focussed = 1;
3582 *new_view = diff_view;
3589 if (s->last_displayed_line >= s->blame.nlines &&
3590 s->selected_line >= MIN(s->blame.nlines,
3591 view->nlines - 2)) {
3594 if (s->last_displayed_line >= s->blame.nlines &&
3595 s->selected_line < view->nlines - 2) {
3596 s->selected_line = MIN(s->blame.nlines,
3600 if (s->last_displayed_line + view->nlines - 2
3602 s->first_displayed_line +=
3605 s->first_displayed_line =
3610 if (s->selected_line > view->nlines - 2) {
3611 s->selected_line = MIN(s->blame.nlines,
3618 return thread_err ? thread_err : err;
3621 static const struct got_error *
3622 cmd_blame(int argc, char *argv[])
3624 const struct got_error *error;
3625 struct got_repository *repo = NULL;
3626 struct got_reflist_head refs;
3627 struct got_worktree *worktree = NULL;
3628 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3629 struct got_object_id *commit_id = NULL;
3630 char *commit_id_str = NULL;
3632 struct tog_view *view;
3634 SIMPLEQ_INIT(&refs);
3637 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3642 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3645 commit_id_str = optarg;
3648 repo_path = realpath(optarg, NULL);
3649 if (repo_path == NULL)
3650 err(1, "-r option");
3666 cwd = getcwd(NULL, 0);
3668 error = got_error_from_errno("getcwd");
3671 if (repo_path == NULL) {
3672 error = got_worktree_open(&worktree, cwd);
3673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3679 strdup(got_worktree_get_repo_path(worktree));
3680 if (repo_path == NULL)
3681 error = got_error_from_errno("strdup");
3685 repo_path = strdup(cwd);
3686 if (repo_path == NULL) {
3687 error = got_error_from_errno("strdup");
3695 error = got_repo_open(&repo, repo_path);
3699 error = apply_unveil(got_repo_get_path(repo), NULL);
3704 const char *prefix = got_worktree_get_path_prefix(worktree);
3705 char *p, *worktree_subdir = cwd +
3706 strlen(got_worktree_get_root_path(worktree));
3707 if (asprintf(&p, "%s%s%s%s%s",
3708 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3709 worktree_subdir, worktree_subdir[0] ? "/" : "",
3711 error = got_error_from_errno("asprintf");
3714 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3717 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3722 if (commit_id_str == NULL) {
3723 struct got_reference *head_ref;
3724 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3727 error = got_ref_resolve(&commit_id, repo, head_ref);
3728 got_ref_close(head_ref);
3730 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3732 if (error->code != GOT_ERR_NOT_REF)
3734 error = got_repo_match_object_id_prefix(&commit_id,
3735 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3741 error = got_ref_list(&refs, repo);
3745 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3747 error = got_error_from_errno("view_open");
3750 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3753 error = view_loop(view);
3759 got_worktree_close(worktree);
3761 got_repo_close(repo);
3762 got_ref_list_free(&refs);
3766 static const struct got_error *
3767 draw_tree_entries(struct tog_view *view,
3768 struct got_tree_entry **first_displayed_entry,
3769 struct got_tree_entry **last_displayed_entry,
3770 struct got_tree_entry **selected_entry, int *ndisplayed,
3771 const char *label, int show_ids, const char *parent_path,
3772 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3774 const struct got_error *err = NULL;
3775 struct got_tree_entry *te;
3781 werase(view->window);
3786 err = format_line(&wline, &width, label, view->ncols);
3789 if (view_needs_focus_indication(view))
3790 wstandout(view->window);
3791 waddwstr(view->window, wline);
3792 if (view_needs_focus_indication(view))
3793 wstandend(view->window);
3796 if (width < view->ncols - 1)
3797 waddch(view->window, '\n');
3800 err = format_line(&wline, &width, parent_path, view->ncols);
3803 waddwstr(view->window, wline);
3806 if (width < view->ncols - 1)
3807 waddch(view->window, '\n');
3810 waddch(view->window, '\n');
3814 te = SIMPLEQ_FIRST(&entries->head);
3815 if (*first_displayed_entry == NULL) {
3816 if (selected == 0) {
3818 wstandout(view->window);
3819 *selected_entry = NULL;
3821 waddstr(view->window, " ..\n"); /* parent directory */
3822 if (selected == 0 && view->focussed)
3823 wstandend(view->window);
3830 while (te != *first_displayed_entry)
3831 te = SIMPLEQ_NEXT(te, entry);
3835 char *line = NULL, *id_str = NULL;
3838 err = got_object_id_str(&id_str, te->id);
3840 return got_error_from_errno(
3841 "got_object_id_str");
3843 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3844 te->name, S_ISDIR(te->mode) ? "/" :
3845 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3847 return got_error_from_errno("asprintf");
3850 err = format_line(&wline, &width, line, view->ncols);
3855 if (n == selected) {
3857 wstandout(view->window);
3858 *selected_entry = te;
3860 waddwstr(view->window, wline);
3861 if (width < view->ncols - 1)
3862 waddch(view->window, '\n');
3863 if (n == selected && view->focussed)
3864 wstandend(view->window);
3870 *last_displayed_entry = te;
3873 te = SIMPLEQ_NEXT(te, entry);
3880 tree_scroll_up(struct tog_view *view,
3881 struct got_tree_entry **first_displayed_entry, int maxscroll,
3882 const struct got_tree_entries *entries, int isroot)
3884 struct got_tree_entry *te, *prev;
3887 if (*first_displayed_entry == NULL)
3890 te = SIMPLEQ_FIRST(&entries->head);
3891 if (*first_displayed_entry == te) {
3893 *first_displayed_entry = NULL;
3897 /* XXX this is stupid... switch to TAILQ? */
3898 for (i = 0; i < maxscroll; i++) {
3899 while (te != *first_displayed_entry) {
3901 te = SIMPLEQ_NEXT(te, entry);
3903 *first_displayed_entry = prev;
3904 te = SIMPLEQ_FIRST(&entries->head);
3906 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3907 *first_displayed_entry = NULL;
3911 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3912 struct got_tree_entry *last_displayed_entry,
3913 const struct got_tree_entries *entries)
3915 struct got_tree_entry *next, *last;
3918 if (*first_displayed_entry)
3919 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3921 next = SIMPLEQ_FIRST(&entries->head);
3922 last = last_displayed_entry;
3923 while (next && last && n++ < maxscroll) {
3924 last = SIMPLEQ_NEXT(last, entry);
3926 *first_displayed_entry = next;
3927 next = SIMPLEQ_NEXT(next, entry);
3933 static const struct got_error *
3934 tree_entry_path(char **path, struct tog_parent_trees *parents,
3935 struct got_tree_entry *te)
3937 const struct got_error *err = NULL;
3938 struct tog_parent_tree *pt;
3939 size_t len = 2; /* for leading slash and NUL */
3941 TAILQ_FOREACH(pt, parents, entry)
3942 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3944 len += strlen(te->name);
3946 *path = calloc(1, len);
3948 return got_error_from_errno("calloc");
3951 pt = TAILQ_LAST(parents, tog_parent_trees);
3953 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3954 err = got_error(GOT_ERR_NO_SPACE);
3957 if (strlcat(*path, "/", len) >= len) {
3958 err = got_error(GOT_ERR_NO_SPACE);
3961 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3964 if (strlcat(*path, te->name, len) >= len) {
3965 err = got_error(GOT_ERR_NO_SPACE);
3977 static const struct got_error *
3978 blame_tree_entry(struct tog_view **new_view, int begin_x,
3979 struct got_tree_entry *te, struct tog_parent_trees *parents,
3980 struct got_object_id *commit_id, struct got_reflist_head *refs,
3981 struct got_repository *repo)
3983 const struct got_error *err = NULL;
3985 struct tog_view *blame_view;
3989 err = tree_entry_path(&path, parents, te);
3993 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3994 if (blame_view == NULL)
3995 return got_error_from_errno("view_open");
3997 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3999 view_close(blame_view);
4002 *new_view = blame_view;
4006 static const struct got_error *
4007 log_tree_entry(struct tog_view **new_view, int begin_x,
4008 struct got_tree_entry *te, struct tog_parent_trees *parents,
4009 struct got_object_id *commit_id, struct got_reflist_head *refs,
4010 struct got_repository *repo)
4012 struct tog_view *log_view;
4013 const struct got_error *err = NULL;
4018 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4019 if (log_view == NULL)
4020 return got_error_from_errno("view_open");
4022 err = tree_entry_path(&path, parents, te);
4026 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4028 view_close(log_view);
4030 *new_view = log_view;
4035 static const struct got_error *
4036 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4037 struct got_object_id *commit_id, struct got_reflist_head *refs,
4038 struct got_repository *repo)
4040 const struct got_error *err = NULL;
4041 char *commit_id_str = NULL;
4042 struct tog_tree_view_state *s = &view->state.tree;
4044 TAILQ_INIT(&s->parents);
4046 err = got_object_id_str(&commit_id_str, commit_id);
4050 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4051 err = got_error_from_errno("asprintf");
4055 s->root = s->tree = root;
4056 s->entries = got_object_tree_get_entries(root);
4057 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4058 s->commit_id = got_object_id_dup(commit_id);
4059 if (s->commit_id == NULL) {
4060 err = got_error_from_errno("got_object_id_dup");
4066 view->show = show_tree_view;
4067 view->input = input_tree_view;
4068 view->close = close_tree_view;
4069 view->search_start = search_start_tree_view;
4070 view->search_next = search_next_tree_view;
4072 free(commit_id_str);
4074 free(s->tree_label);
4075 s->tree_label = NULL;
4080 static const struct got_error *
4081 close_tree_view(struct tog_view *view)
4083 struct tog_tree_view_state *s = &view->state.tree;
4085 free(s->tree_label);
4086 s->tree_label = NULL;
4088 s->commit_id = NULL;
4089 while (!TAILQ_EMPTY(&s->parents)) {
4090 struct tog_parent_tree *parent;
4091 parent = TAILQ_FIRST(&s->parents);
4092 TAILQ_REMOVE(&s->parents, parent, entry);
4096 if (s->tree != s->root)
4097 got_object_tree_close(s->tree);
4098 got_object_tree_close(s->root);
4103 static const struct got_error *
4104 search_start_tree_view(struct tog_view *view)
4106 struct tog_tree_view_state *s = &view->state.tree;
4108 s->matched_entry = NULL;
4113 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4115 regmatch_t regmatch;
4117 return regexec(regex, te->name, 1, ®match, 0) == 0;
4120 static const struct got_error *
4121 search_next_tree_view(struct tog_view *view)
4123 struct tog_tree_view_state *s = &view->state.tree;
4124 struct got_tree_entry *entry = NULL, *te;
4126 if (!view->searching) {
4127 view->search_next_done = 1;
4131 if (s->matched_entry) {
4132 if (view->searching == TOG_SEARCH_FORWARD) {
4133 if (s->selected_entry)
4134 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4136 entry = SIMPLEQ_FIRST(&s->entries->head);
4139 if (s->selected_entry == NULL) {
4140 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4143 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4145 if (SIMPLEQ_NEXT(te, entry) ==
4152 if (view->searching == TOG_SEARCH_FORWARD)
4153 entry = SIMPLEQ_FIRST(&s->entries->head);
4155 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4161 if (entry == NULL) {
4162 if (s->matched_entry == NULL) {
4163 view->search_next_done = 1;
4166 if (view->searching == TOG_SEARCH_FORWARD)
4167 entry = SIMPLEQ_FIRST(&s->entries->head);
4169 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4174 if (match_tree_entry(entry, &view->regex)) {
4175 view->search_next_done = 1;
4176 s->matched_entry = entry;
4180 if (view->searching == TOG_SEARCH_FORWARD)
4181 entry = SIMPLEQ_NEXT(entry, entry);
4183 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4186 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4187 if (SIMPLEQ_NEXT(te, entry) == entry) {
4196 if (s->matched_entry) {
4197 s->first_displayed_entry = s->matched_entry;
4204 static const struct got_error *
4205 show_tree_view(struct tog_view *view)
4207 const struct got_error *err = NULL;
4208 struct tog_tree_view_state *s = &view->state.tree;
4211 err = tree_entry_path(&parent_path, &s->parents, NULL);
4215 err = draw_tree_entries(view, &s->first_displayed_entry,
4216 &s->last_displayed_entry, &s->selected_entry,
4217 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4218 s->entries, s->selected, view->nlines, s->tree == s->root);
4225 static const struct got_error *
4226 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4227 struct tog_view **focus_view, struct tog_view *view, int ch)
4229 const struct got_error *err = NULL;
4230 struct tog_tree_view_state *s = &view->state.tree;
4231 struct tog_view *log_view;
4232 int begin_x = 0, nscrolled;
4236 s->show_ids = !s->show_ids;
4239 if (!s->selected_entry)
4241 if (view_is_parent_view(view))
4242 begin_x = view_split_begin_x(view->begin_x);
4243 err = log_tree_entry(&log_view, begin_x,
4244 s->selected_entry, &s->parents,
4245 s->commit_id, s->refs, s->repo);
4246 if (view_is_parent_view(view)) {
4247 err = view_close_child(view);
4250 err = view_set_child(view, log_view);
4252 view_close(log_view);
4255 *focus_view = log_view;
4256 view->child_focussed = 1;
4258 *new_view = log_view;
4262 if (s->selected > 0) {
4264 if (s->selected == 0)
4267 if (s->selected > 0)
4269 tree_scroll_up(view, &s->first_displayed_entry, 1,
4270 s->entries, s->tree == s->root);
4273 tree_scroll_up(view, &s->first_displayed_entry,
4274 MAX(0, view->nlines - 4 - s->selected), s->entries,
4275 s->tree == s->root);
4277 if (SIMPLEQ_FIRST(&s->entries->head) ==
4278 s->first_displayed_entry && s->tree != s->root)
4279 s->first_displayed_entry = NULL;
4283 if (s->selected < s->ndisplayed - 1) {
4287 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4288 /* can't scroll any further */
4290 tree_scroll_down(&s->first_displayed_entry, 1,
4291 s->last_displayed_entry, s->entries);
4294 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4296 /* can't scroll any further; move cursor down */
4297 if (s->selected < s->ndisplayed - 1)
4298 s->selected = s->ndisplayed - 1;
4301 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4302 view->nlines, s->last_displayed_entry, s->entries);
4303 if (nscrolled < view->nlines) {
4305 struct got_tree_entry *te;
4306 te = s->first_displayed_entry;
4309 te = SIMPLEQ_NEXT(te, entry);
4311 s->selected = ndisplayed - 1;
4317 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4318 struct tog_parent_tree *parent;
4319 /* user selected '..' */
4320 if (s->tree == s->root)
4322 parent = TAILQ_FIRST(&s->parents);
4323 TAILQ_REMOVE(&s->parents, parent,
4325 got_object_tree_close(s->tree);
4326 s->tree = parent->tree;
4328 got_object_tree_get_entries(s->tree);
4329 s->first_displayed_entry =
4330 parent->first_displayed_entry;
4332 parent->selected_entry;
4333 s->selected = parent->selected;
4335 } else if (S_ISDIR(s->selected_entry->mode)) {
4336 struct got_tree_object *subtree;
4337 err = got_object_open_as_tree(&subtree,
4338 s->repo, s->selected_entry->id);
4341 err = tree_view_visit_subtree(subtree, s);
4343 got_object_tree_close(subtree);
4346 } else if (S_ISREG(s->selected_entry->mode)) {
4347 struct tog_view *blame_view;
4348 int begin_x = view_is_parent_view(view) ?
4349 view_split_begin_x(view->begin_x) : 0;
4351 err = blame_tree_entry(&blame_view, begin_x,
4352 s->selected_entry, &s->parents,
4353 s->commit_id, s->refs, s->repo);
4356 if (view_is_parent_view(view)) {
4357 err = view_close_child(view);
4360 err = view_set_child(view, blame_view);
4362 view_close(blame_view);
4365 *focus_view = blame_view;
4366 view->child_focussed = 1;
4368 *new_view = blame_view;
4372 if (s->selected > view->nlines)
4373 s->selected = s->ndisplayed - 1;
4386 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4391 static const struct got_error *
4392 cmd_tree(int argc, char *argv[])
4394 const struct got_error *error;
4395 struct got_repository *repo = NULL;
4396 struct got_reflist_head refs;
4397 char *repo_path = NULL;
4398 struct got_object_id *commit_id = NULL;
4399 char *commit_id_arg = NULL;
4400 struct got_commit_object *commit = NULL;
4401 struct got_tree_object *tree = NULL;
4403 struct tog_view *view;
4405 SIMPLEQ_INIT(&refs);
4408 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4413 while ((ch = getopt(argc, argv, "c:")) != -1) {
4416 commit_id_arg = optarg;
4428 struct got_worktree *worktree;
4429 char *cwd = getcwd(NULL, 0);
4431 return got_error_from_errno("getcwd");
4432 error = got_worktree_open(&worktree, cwd);
4433 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4438 strdup(got_worktree_get_repo_path(worktree));
4439 got_worktree_close(worktree);
4442 if (repo_path == NULL) {
4443 error = got_error_from_errno("strdup");
4446 } else if (argc == 1) {
4447 repo_path = realpath(argv[0], NULL);
4448 if (repo_path == NULL)
4449 return got_error_from_errno2("realpath", argv[0]);
4455 error = got_repo_open(&repo, repo_path);
4459 error = apply_unveil(got_repo_get_path(repo), NULL);
4463 if (commit_id_arg == NULL)
4464 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4466 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4468 if (error->code != GOT_ERR_NOT_REF)
4470 error = got_repo_match_object_id_prefix(&commit_id,
4471 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4477 error = got_object_open_as_commit(&commit, repo, commit_id);
4481 error = got_object_open_as_tree(&tree, repo,
4482 got_object_commit_get_tree_id(commit));
4486 error = got_ref_list(&refs, repo);
4490 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4492 error = got_error_from_errno("view_open");
4495 error = open_tree_view(view, tree, commit_id, &refs, repo);
4498 error = view_loop(view);
4503 got_object_commit_close(commit);
4505 got_object_tree_close(tree);
4507 got_repo_close(repo);
4508 got_ref_list_free(&refs);
4517 fprintf(stderr, "commands:");
4518 for (i = 0; i < nitems(tog_commands); i++) {
4519 struct tog_cmd *cmd = &tog_commands[i];
4520 fprintf(stderr, " %s", cmd->name);
4522 fputc('\n', stderr);
4528 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4536 make_argv(const char *arg0, const char *arg1)
4539 int argc = (arg1 == NULL ? 1 : 2);
4541 argv = calloc(argc, sizeof(char *));
4544 argv[0] = strdup(arg0);
4545 if (argv[0] == NULL)
4548 argv[1] = strdup(arg1);
4549 if (argv[1] == NULL)
4557 main(int argc, char *argv[])
4559 const struct got_error *error = NULL;
4560 struct tog_cmd *cmd = NULL;
4561 int ch, hflag = 0, Vflag = 0;
4562 char **cmd_argv = NULL;
4564 setlocale(LC_CTYPE, "");
4566 while ((ch = getopt(argc, argv, "hV")) != -1) {
4586 got_version_print_str();
4593 /* Build an argument vector which runs a default command. */
4594 cmd = &tog_commands[0];
4595 cmd_argv = make_argv(cmd->name, NULL);
4600 /* Did the user specific a command? */
4601 for (i = 0; i < nitems(tog_commands); i++) {
4602 if (strncmp(tog_commands[i].name, argv[0],
4603 strlen(argv[0])) == 0) {
4604 cmd = &tog_commands[i];
4610 fprintf(stderr, "%s: unknown command '%s'\n",
4611 getprogname(), argv[0]);
4620 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4625 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);