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);
2093 in_repo_path = strdup(s->in_repo_path);
2094 if (in_repo_path == NULL) {
2096 return got_error_from_errno("strdup");
2098 err = open_log_view(lv, start_id, s->refs, s->repo,
2099 s->head_ref_name, in_repo_path, 0);
2112 static const struct got_error *
2113 apply_unveil(const char *repo_path, const char *worktree_path)
2115 const struct got_error *error;
2118 if (unveil("gmon.out", "rwc") != 0)
2119 return got_error_from_errno2("unveil", "gmon.out");
2121 if (repo_path && unveil(repo_path, "r") != 0)
2122 return got_error_from_errno2("unveil", repo_path);
2124 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2125 return got_error_from_errno2("unveil", worktree_path);
2127 if (unveil("/tmp", "rwc") != 0)
2128 return got_error_from_errno2("unveil", "/tmp");
2130 error = got_privsep_unveil_exec_helpers();
2134 if (unveil(NULL, NULL) != 0)
2135 return got_error_from_errno("unveil");
2145 halfdelay(1); /* Do fast refresh while initial view is loading. */
2148 intrflush(stdscr, FALSE);
2149 keypad(stdscr, TRUE);
2151 signal(SIGWINCH, tog_sigwinch);
2154 static const struct got_error *
2155 cmd_log(int argc, char *argv[])
2157 const struct got_error *error;
2158 struct got_repository *repo = NULL;
2159 struct got_worktree *worktree = NULL;
2160 struct got_reflist_head refs;
2161 struct got_object_id *start_id = NULL;
2162 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2163 char *start_commit = NULL;
2165 struct tog_view *view;
2167 SIMPLEQ_INIT(&refs);
2170 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2175 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2178 start_commit = optarg;
2181 repo_path = realpath(optarg, NULL);
2182 if (repo_path == NULL)
2183 err(1, "-r option");
2194 cwd = getcwd(NULL, 0);
2196 error = got_error_from_errno("getcwd");
2199 error = got_worktree_open(&worktree, cwd);
2200 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2207 error = got_error_from_errno("strdup");
2210 } else if (argc == 1) {
2212 error = got_worktree_resolve_path(&path, worktree,
2217 path = strdup(argv[0]);
2219 error = got_error_from_errno("strdup");
2226 repo_path = worktree ?
2227 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2228 if (repo_path == NULL) {
2229 error = got_error_from_errno("strdup");
2235 error = got_repo_open(&repo, repo_path);
2239 error = apply_unveil(got_repo_get_path(repo),
2240 worktree ? got_worktree_get_root_path(worktree) : NULL);
2244 if (start_commit == NULL)
2245 error = get_head_commit_id(&start_id, worktree ?
2246 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2249 error = get_head_commit_id(&start_id, start_commit, repo);
2251 if (error->code != GOT_ERR_NOT_REF)
2253 error = got_repo_match_object_id_prefix(&start_id,
2254 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2260 error = got_ref_list(&refs, repo);
2264 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2266 error = got_error_from_errno("view_open");
2269 error = open_log_view(view, start_id, &refs, repo, worktree ?
2270 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2273 error = view_loop(view);
2280 got_repo_close(repo);
2282 got_worktree_close(worktree);
2283 got_ref_list_free(&refs);
2291 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2297 parse_next_line(FILE *f, size_t *len)
2302 const char delim[3] = { '\0', '\0', '\0'};
2304 line = fparseln(f, &linelen, &lineno, delim, 0);
2310 static const struct got_error *
2311 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2312 int *last_displayed_line, int *eof, int max_lines,
2315 const struct got_error *err;
2316 int nlines = 0, nprinted = 0;
2323 werase(view->window);
2326 err = format_line(&wline, &width, header, view->ncols);
2331 if (view_needs_focus_indication(view))
2332 wstandout(view->window);
2333 waddwstr(view->window, wline);
2334 if (view_needs_focus_indication(view))
2335 wstandend(view->window);
2336 if (width < view->ncols - 1)
2337 waddch(view->window, '\n');
2345 while (nprinted < max_lines) {
2346 line = parse_next_line(f, &len);
2351 if (++nlines < *first_displayed_line) {
2356 err = format_line(&wline, &width, line, view->ncols);
2361 waddwstr(view->window, wline);
2362 if (width < view->ncols - 1)
2363 waddch(view->window, '\n');
2364 if (++nprinted == 1)
2365 *first_displayed_line = nlines;
2370 *last_displayed_line = nlines;
2375 while (nprinted < view->nlines) {
2376 waddch(view->window, '\n');
2380 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2385 wstandout(view->window);
2386 waddwstr(view->window, wline);
2387 wstandend(view->window);
2394 get_datestr(time_t *time, char *datebuf)
2396 char *p, *s = ctime_r(time, datebuf);
2397 p = strchr(s, '\n');
2403 static const struct got_error *
2404 write_commit_info(struct got_object_id *commit_id,
2405 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2407 const struct got_error *err = NULL;
2409 struct got_commit_object *commit;
2410 char *id_str = NULL;
2411 time_t committer_time;
2412 const char *author, *committer;
2413 char *refs_str = NULL;
2416 err = build_refs_str(&refs_str, refs, commit_id);
2421 err = got_object_open_as_commit(&commit, repo, commit_id);
2425 err = got_object_id_str(&id_str, commit_id);
2427 err = got_error_from_errno("got_object_id_str");
2431 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2432 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2433 err = got_error_from_errno("fprintf");
2436 if (fprintf(outfile, "from: %s\n",
2437 got_object_commit_get_author(commit)) < 0) {
2438 err = got_error_from_errno("fprintf");
2441 committer_time = got_object_commit_get_committer_time(commit);
2442 if (fprintf(outfile, "date: %s UTC\n",
2443 get_datestr(&committer_time, datebuf)) < 0) {
2444 err = got_error_from_errno("fprintf");
2447 author = got_object_commit_get_author(commit);
2448 committer = got_object_commit_get_committer(commit);
2449 if (strcmp(author, committer) != 0 &&
2450 fprintf(outfile, "via: %s\n", committer) < 0) {
2451 err = got_error_from_errno("fprintf");
2454 if (fprintf(outfile, "%s\n",
2455 got_object_commit_get_logmsg(commit)) < 0) {
2456 err = got_error_from_errno("fprintf");
2462 got_object_commit_close(commit);
2466 static const struct got_error *
2467 create_diff(struct tog_diff_view_state *s)
2469 const struct got_error *err = NULL;
2475 err = got_error_from_errno("got_opentemp");
2478 if (s->f && fclose(s->f) != 0) {
2479 err = got_error_from_errno("fclose");
2485 err = got_object_get_type(&obj_type, s->repo, s->id1);
2487 err = got_object_get_type(&obj_type, s->repo, s->id2);
2492 case GOT_OBJ_TYPE_BLOB:
2493 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2494 s->diff_context, s->repo, f);
2496 case GOT_OBJ_TYPE_TREE:
2497 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2498 s->diff_context, s->repo, f);
2500 case GOT_OBJ_TYPE_COMMIT: {
2501 const struct got_object_id_queue *parent_ids;
2502 struct got_object_qid *pid;
2503 struct got_commit_object *commit2;
2505 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2508 /* Show commit info if we're diffing to a parent/root commit. */
2510 write_commit_info(s->id2, s->refs, s->repo, f);
2512 parent_ids = got_object_commit_get_parent_ids(commit2);
2513 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2514 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2515 write_commit_info(s->id2, s->refs,
2521 got_object_commit_close(commit2);
2523 err = got_diff_objects_as_commits(s->id1, s->id2,
2524 s->diff_context, s->repo, f);
2528 err = got_error(GOT_ERR_OBJ_TYPE);
2532 if (f && fflush(f) != 0 && err == NULL)
2533 err = got_error_from_errno("fflush");
2538 diff_view_indicate_progress(struct tog_view *view)
2540 mvwaddstr(view->window, 0, 0, "diffing...");
2545 static const struct got_error *
2546 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2547 struct got_object_id *id2, struct tog_view *log_view,
2548 struct got_reflist_head *refs, struct got_repository *repo)
2550 const struct got_error *err;
2552 if (id1 != NULL && id2 != NULL) {
2554 err = got_object_get_type(&type1, repo, id1);
2557 err = got_object_get_type(&type2, repo, id2);
2562 return got_error(GOT_ERR_OBJ_TYPE);
2566 view->state.diff.id1 = got_object_id_dup(id1);
2567 if (view->state.diff.id1 == NULL)
2568 return got_error_from_errno("got_object_id_dup");
2570 view->state.diff.id1 = NULL;
2572 view->state.diff.id2 = got_object_id_dup(id2);
2573 if (view->state.diff.id2 == NULL) {
2574 free(view->state.diff.id1);
2575 view->state.diff.id1 = NULL;
2576 return got_error_from_errno("got_object_id_dup");
2578 view->state.diff.f = NULL;
2579 view->state.diff.first_displayed_line = 1;
2580 view->state.diff.last_displayed_line = view->nlines;
2581 view->state.diff.diff_context = 3;
2582 view->state.diff.log_view = log_view;
2583 view->state.diff.repo = repo;
2584 view->state.diff.refs = refs;
2586 if (log_view && view_is_splitscreen(view))
2587 show_log_view(log_view); /* draw vborder */
2588 diff_view_indicate_progress(view);
2590 err = create_diff(&view->state.diff);
2592 free(view->state.diff.id1);
2593 view->state.diff.id1 = NULL;
2594 free(view->state.diff.id2);
2595 view->state.diff.id2 = NULL;
2599 view->show = show_diff_view;
2600 view->input = input_diff_view;
2601 view->close = close_diff_view;
2606 static const struct got_error *
2607 close_diff_view(struct tog_view *view)
2609 const struct got_error *err = NULL;
2611 free(view->state.diff.id1);
2612 view->state.diff.id1 = NULL;
2613 free(view->state.diff.id2);
2614 view->state.diff.id2 = NULL;
2615 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2616 err = got_error_from_errno("fclose");
2620 static const struct got_error *
2621 show_diff_view(struct tog_view *view)
2623 const struct got_error *err;
2624 struct tog_diff_view_state *s = &view->state.diff;
2625 char *id_str1 = NULL, *id_str2, *header;
2628 err = got_object_id_str(&id_str1, s->id1);
2632 err = got_object_id_str(&id_str2, s->id2);
2636 if (asprintf(&header, "diff %s %s",
2637 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2638 err = got_error_from_errno("asprintf");
2646 return draw_file(view, s->f, &s->first_displayed_line,
2647 &s->last_displayed_line, &s->eof, view->nlines,
2651 static const struct got_error *
2652 set_selected_commit(struct tog_diff_view_state *s,
2653 struct commit_queue_entry *entry)
2655 const struct got_error *err;
2656 const struct got_object_id_queue *parent_ids;
2657 struct got_commit_object *selected_commit;
2658 struct got_object_qid *pid;
2661 s->id2 = got_object_id_dup(entry->id);
2663 return got_error_from_errno("got_object_id_dup");
2665 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2668 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2670 pid = SIMPLEQ_FIRST(parent_ids);
2671 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2672 got_object_commit_close(selected_commit);
2676 static const struct got_error *
2677 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2678 struct tog_view **focus_view, struct tog_view *view, int ch)
2680 const struct got_error *err = NULL;
2681 struct tog_diff_view_state *s = &view->state.diff;
2682 struct tog_log_view_state *ls;
2683 struct commit_queue_entry *entry;
2689 if (s->first_displayed_line > 1)
2690 s->first_displayed_line--;
2694 if (s->first_displayed_line == 1)
2697 while (i++ < view->nlines - 1 &&
2698 s->first_displayed_line > 1)
2699 s->first_displayed_line--;
2704 s->first_displayed_line++;
2712 while (!s->eof && i++ < view->nlines - 1) {
2714 line = parse_next_line(s->f, NULL);
2715 s->first_displayed_line++;
2721 if (s->diff_context > 0) {
2723 diff_view_indicate_progress(view);
2724 err = create_diff(s);
2728 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2730 diff_view_indicate_progress(view);
2731 err = create_diff(s);
2736 if (s->log_view == NULL)
2738 ls = &s->log_view->state.log;
2739 entry = TAILQ_PREV(ls->selected_entry,
2740 commit_queue_head, entry);
2744 err = input_log_view(NULL, NULL, NULL, s->log_view,
2749 err = set_selected_commit(s, entry);
2753 s->first_displayed_line = 1;
2754 s->last_displayed_line = view->nlines;
2756 diff_view_indicate_progress(view);
2757 err = create_diff(s);
2761 if (s->log_view == NULL)
2763 ls = &s->log_view->state.log;
2765 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2766 ls->thread_args.commits_needed++;
2768 /* Display "loading..." in log view. */
2769 show_log_view(s->log_view);
2773 err = trigger_log_thread(1 /* load_all */,
2774 &ls->thread_args.commits_needed,
2775 &ls->thread_args.log_complete,
2776 &ls->thread_args.need_commits);
2780 err = input_log_view(NULL, NULL, NULL, s->log_view,
2785 entry = TAILQ_NEXT(ls->selected_entry, entry);
2789 err = set_selected_commit(s, entry);
2793 s->first_displayed_line = 1;
2794 s->last_displayed_line = view->nlines;
2796 diff_view_indicate_progress(view);
2797 err = create_diff(s);
2806 static const struct got_error *
2807 cmd_diff(int argc, char *argv[])
2809 const struct got_error *error = NULL;
2810 struct got_repository *repo = NULL;
2811 struct got_reflist_head refs;
2812 struct got_object_id *id1 = NULL, *id2 = NULL;
2813 char *repo_path = NULL;
2814 char *id_str1 = NULL, *id_str2 = NULL;
2816 struct tog_view *view;
2818 SIMPLEQ_INIT(&refs);
2821 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2826 while ((ch = getopt(argc, argv, "")) != -1) {
2838 usage_diff(); /* TODO show local worktree changes */
2839 } else if (argc == 2) {
2840 repo_path = getcwd(NULL, 0);
2841 if (repo_path == NULL)
2842 return got_error_from_errno("getcwd");
2845 } else if (argc == 3) {
2846 repo_path = realpath(argv[0], NULL);
2847 if (repo_path == NULL)
2848 return got_error_from_errno2("realpath", argv[0]);
2856 error = got_repo_open(&repo, repo_path);
2860 error = apply_unveil(got_repo_get_path(repo), NULL);
2864 error = got_repo_match_object_id_prefix(&id1, id_str1,
2865 GOT_OBJ_TYPE_ANY, repo);
2869 error = got_repo_match_object_id_prefix(&id2, id_str2,
2870 GOT_OBJ_TYPE_ANY, repo);
2874 error = got_ref_list(&refs, repo);
2878 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2880 error = got_error_from_errno("view_open");
2883 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2886 error = view_loop(view);
2890 got_repo_close(repo);
2891 got_ref_list_free(&refs);
2899 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2904 struct tog_blame_line {
2906 struct got_object_id *id;
2909 static const struct got_error *
2910 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2911 const char *path, struct tog_blame_line *lines, int nlines,
2912 int blame_complete, int selected_line, int *first_displayed_line,
2913 int *last_displayed_line, int *eof, int max_lines)
2915 const struct got_error *err;
2916 int lineno = 0, nprinted = 0;
2921 struct tog_blame_line *blame_line;
2922 struct got_object_id *prev_id = NULL;
2925 err = got_object_id_str(&id_str, id);
2930 werase(view->window);
2932 if (asprintf(&line, "commit %s", id_str) == -1) {
2933 err = got_error_from_errno("asprintf");
2938 err = format_line(&wline, &width, line, view->ncols);
2941 if (view_needs_focus_indication(view))
2942 wstandout(view->window);
2943 waddwstr(view->window, wline);
2944 if (view_needs_focus_indication(view))
2945 wstandend(view->window);
2948 if (width < view->ncols - 1)
2949 waddch(view->window, '\n');
2951 if (asprintf(&line, "[%d/%d] %s%s",
2952 *first_displayed_line - 1 + selected_line, nlines,
2953 blame_complete ? "" : "annotating... ", path) == -1) {
2955 return got_error_from_errno("asprintf");
2958 err = format_line(&wline, &width, line, view->ncols);
2963 waddwstr(view->window, wline);
2966 if (width < view->ncols - 1)
2967 waddch(view->window, '\n');
2970 while (nprinted < max_lines - 2) {
2971 line = parse_next_line(f, &len);
2976 if (++lineno < *first_displayed_line) {
2981 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2982 err = format_line(&wline, &width, line, wlimit);
2988 if (view->focussed && nprinted == selected_line - 1)
2989 wstandout(view->window);
2991 blame_line = &lines[lineno - 1];
2992 if (blame_line->annotated && prev_id &&
2993 got_object_id_cmp(prev_id, blame_line->id) == 0)
2994 waddstr(view->window, " ");
2995 else if (blame_line->annotated) {
2997 err = got_object_id_str(&id_str, blame_line->id);
3003 wprintw(view->window, "%.8s ", id_str);
3005 prev_id = blame_line->id;
3007 waddstr(view->window, "........ ");
3011 waddwstr(view->window, wline);
3012 while (width < wlimit) {
3013 waddch(view->window, ' ');
3016 if (view->focussed && nprinted == selected_line - 1)
3017 wstandend(view->window);
3018 if (++nprinted == 1)
3019 *first_displayed_line = lineno;
3024 *last_displayed_line = lineno;
3031 static const struct got_error *
3032 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3034 const struct got_error *err = NULL;
3035 struct tog_blame_cb_args *a = arg;
3036 struct tog_blame_line *line;
3039 if (nlines != a->nlines ||
3040 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3041 return got_error(GOT_ERR_RANGE);
3043 errcode = pthread_mutex_lock(&tog_mutex);
3045 return got_error_set_errno(errcode, "pthread_mutex_lock");
3047 if (*a->quit) { /* user has quit the blame view */
3048 err = got_error(GOT_ERR_ITER_COMPLETED);
3053 goto done; /* no change in this commit */
3055 line = &a->lines[lineno - 1];
3056 if (line->annotated)
3059 line->id = got_object_id_dup(id);
3060 if (line->id == NULL) {
3061 err = got_error_from_errno("got_object_id_dup");
3064 line->annotated = 1;
3066 errcode = pthread_mutex_unlock(&tog_mutex);
3068 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3073 blame_thread(void *arg)
3075 const struct got_error *err;
3076 struct tog_blame_thread_args *ta = arg;
3077 struct tog_blame_cb_args *a = ta->cb_args;
3080 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3081 blame_cb, ta->cb_args);
3083 errcode = pthread_mutex_lock(&tog_mutex);
3085 return (void *)got_error_set_errno(errcode,
3086 "pthread_mutex_lock");
3088 got_repo_close(ta->repo);
3092 errcode = pthread_mutex_unlock(&tog_mutex);
3093 if (errcode && err == NULL)
3094 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3099 static struct got_object_id *
3100 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3103 struct tog_blame_line *line;
3105 line = &lines[first_displayed_line - 1 + selected_line - 1];
3106 if (!line->annotated)
3112 static const struct got_error *
3113 stop_blame(struct tog_blame *blame)
3115 const struct got_error *err = NULL;
3118 if (blame->thread) {
3120 errcode = pthread_mutex_unlock(&tog_mutex);
3122 return got_error_set_errno(errcode,
3123 "pthread_mutex_unlock");
3124 errcode = pthread_join(blame->thread, (void **)&err);
3126 return got_error_set_errno(errcode, "pthread_join");
3127 errcode = pthread_mutex_lock(&tog_mutex);
3129 return got_error_set_errno(errcode,
3130 "pthread_mutex_lock");
3131 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3133 blame->thread = NULL;
3135 if (blame->thread_args.repo) {
3136 got_repo_close(blame->thread_args.repo);
3137 blame->thread_args.repo = NULL;
3140 if (fclose(blame->f) != 0 && err == NULL)
3141 err = got_error_from_errno("fclose");
3145 for (i = 0; i < blame->nlines; i++)
3146 free(blame->lines[i].id);
3148 blame->lines = NULL;
3150 free(blame->cb_args.commit_id);
3151 blame->cb_args.commit_id = NULL;
3156 static const struct got_error *
3157 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3158 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3159 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3160 struct got_repository *repo)
3162 const struct got_error *err = NULL;
3163 struct got_blob_object *blob = NULL;
3164 struct got_repository *thread_repo = NULL;
3165 struct got_object_id *obj_id = NULL;
3168 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3172 return got_error(GOT_ERR_NO_OBJ);
3174 err = got_object_get_type(&obj_type, repo, obj_id);
3178 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3179 err = got_error(GOT_ERR_OBJ_TYPE);
3183 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3186 blame->f = got_opentemp();
3187 if (blame->f == NULL) {
3188 err = got_error_from_errno("got_opentemp");
3191 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3192 &blame->line_offsets, blame->f, blob);
3196 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3197 if (blame->lines == NULL) {
3198 err = got_error_from_errno("calloc");
3202 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3206 blame->cb_args.view = view;
3207 blame->cb_args.lines = blame->lines;
3208 blame->cb_args.nlines = blame->nlines;
3209 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3210 if (blame->cb_args.commit_id == NULL) {
3211 err = got_error_from_errno("got_object_id_dup");
3214 blame->cb_args.quit = done;
3216 blame->thread_args.path = path;
3217 blame->thread_args.repo = thread_repo;
3218 blame->thread_args.cb_args = &blame->cb_args;
3219 blame->thread_args.complete = blame_complete;
3220 *blame_complete = 0;
3224 got_object_blob_close(blob);
3231 static const struct got_error *
3232 open_blame_view(struct tog_view *view, char *path,
3233 struct got_object_id *commit_id, struct got_reflist_head *refs,
3234 struct got_repository *repo)
3236 const struct got_error *err = NULL;
3237 struct tog_blame_view_state *s = &view->state.blame;
3239 SIMPLEQ_INIT(&s->blamed_commits);
3241 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3245 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3246 s->first_displayed_line = 1;
3247 s->last_displayed_line = view->nlines;
3248 s->selected_line = 1;
3249 s->blame_complete = 0;
3251 if (s->path == NULL)
3252 return got_error_from_errno("open_blame_view");
3255 s->commit_id = commit_id;
3256 memset(&s->blame, 0, sizeof(s->blame));
3258 view->show = show_blame_view;
3259 view->input = input_blame_view;
3260 view->close = close_blame_view;
3261 view->search_start = search_start_blame_view;
3262 view->search_next = search_next_blame_view;
3264 return run_blame(&s->blame, view, &s->blame_complete,
3265 &s->first_displayed_line, &s->last_displayed_line,
3266 &s->selected_line, &s->done, &s->eof, s->path,
3267 s->blamed_commit->id, s->repo);
3270 static const struct got_error *
3271 close_blame_view(struct tog_view *view)
3273 const struct got_error *err = NULL;
3274 struct tog_blame_view_state *s = &view->state.blame;
3276 if (s->blame.thread)
3277 err = stop_blame(&s->blame);
3279 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3280 struct got_object_qid *blamed_commit;
3281 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3282 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3283 got_object_qid_free(blamed_commit);
3291 static const struct got_error *
3292 search_start_blame_view(struct tog_view *view)
3294 struct tog_blame_view_state *s = &view->state.blame;
3296 s->matched_line = 0;
3301 match_line(const char *line, regex_t *regex)
3303 regmatch_t regmatch;
3305 return regexec(regex, line, 1, ®match, 0) == 0;
3309 static const struct got_error *
3310 search_next_blame_view(struct tog_view *view)
3312 struct tog_blame_view_state *s = &view->state.blame;
3315 if (!view->searching) {
3316 view->search_next_done = 1;
3320 if (s->matched_line) {
3321 if (view->searching == TOG_SEARCH_FORWARD)
3322 lineno = s->matched_line + 1;
3324 lineno = s->matched_line - 1;
3326 if (view->searching == TOG_SEARCH_FORWARD)
3329 lineno = s->blame.nlines;
3337 if (lineno <= 0 || lineno > s->blame.nlines) {
3338 if (s->matched_line == 0) {
3339 view->search_next_done = 1;
3344 if (view->searching == TOG_SEARCH_FORWARD)
3347 lineno = s->blame.nlines;
3350 offset = s->blame.line_offsets[lineno - 1];
3351 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3353 return got_error_from_errno("fseeko");
3356 line = parse_next_line(s->blame.f, &len);
3357 if (line && match_line(line, &view->regex)) {
3358 view->search_next_done = 1;
3359 s->matched_line = lineno;
3364 if (view->searching == TOG_SEARCH_FORWARD)
3370 if (s->matched_line) {
3371 s->first_displayed_line = s->matched_line;
3372 s->selected_line = 1;
3378 static const struct got_error *
3379 show_blame_view(struct tog_view *view)
3381 const struct got_error *err = NULL;
3382 struct tog_blame_view_state *s = &view->state.blame;
3385 if (s->blame.thread == NULL) {
3386 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3387 &s->blame.thread_args);
3389 return got_error_set_errno(errcode, "pthread_create");
3392 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3393 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3394 s->selected_line, &s->first_displayed_line,
3395 &s->last_displayed_line, &s->eof, view->nlines);
3401 static const struct got_error *
3402 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3403 struct tog_view **focus_view, struct tog_view *view, int ch)
3405 const struct got_error *err = NULL, *thread_err = NULL;
3406 struct tog_view *diff_view;
3407 struct tog_blame_view_state *s = &view->state.blame;
3416 if (s->selected_line > 1)
3418 else if (s->selected_line == 1 &&
3419 s->first_displayed_line > 1)
3420 s->first_displayed_line--;
3423 if (s->first_displayed_line == 1) {
3424 s->selected_line = 1;
3427 if (s->first_displayed_line > view->nlines - 2)
3428 s->first_displayed_line -=
3431 s->first_displayed_line = 1;
3435 if (s->selected_line < view->nlines - 2 &&
3436 s->first_displayed_line +
3437 s->selected_line <= s->blame.nlines)
3439 else if (s->last_displayed_line <
3441 s->first_displayed_line++;
3445 struct got_object_id *id = NULL;
3446 id = get_selected_commit_id(s->blame.lines,
3447 s->first_displayed_line, s->selected_line);
3451 struct got_commit_object *commit;
3452 struct got_object_qid *pid;
3453 struct got_object_id *blob_id = NULL;
3455 err = got_object_open_as_commit(&commit,
3459 pid = SIMPLEQ_FIRST(
3460 got_object_commit_get_parent_ids(commit));
3462 got_object_commit_close(commit);
3465 /* Check if path history ends here. */
3466 err = got_object_id_by_path(&blob_id, s->repo,
3469 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3471 got_object_commit_close(commit);
3474 err = got_object_get_type(&obj_type, s->repo,
3477 /* Can't blame non-blob type objects. */
3478 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3479 got_object_commit_close(commit);
3482 err = got_object_qid_alloc(&s->blamed_commit,
3484 got_object_commit_close(commit);
3486 if (got_object_id_cmp(id,
3487 s->blamed_commit->id) == 0)
3489 err = got_object_qid_alloc(&s->blamed_commit,
3495 thread_err = stop_blame(&s->blame);
3499 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3500 s->blamed_commit, entry);
3501 err = run_blame(&s->blame, view, &s->blame_complete,
3502 &s->first_displayed_line, &s->last_displayed_line,
3503 &s->selected_line, &s->done, &s->eof,
3504 s->path, s->blamed_commit->id, s->repo);
3510 struct got_object_qid *first;
3511 first = SIMPLEQ_FIRST(&s->blamed_commits);
3512 if (!got_object_id_cmp(first->id, s->commit_id))
3515 thread_err = stop_blame(&s->blame);
3519 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3520 got_object_qid_free(s->blamed_commit);
3522 SIMPLEQ_FIRST(&s->blamed_commits);
3523 err = run_blame(&s->blame, view, &s->blame_complete,
3524 &s->first_displayed_line, &s->last_displayed_line,
3525 &s->selected_line, &s->done, &s->eof, s->path,
3526 s->blamed_commit->id, s->repo);
3533 struct got_object_id *id = NULL;
3534 struct got_object_qid *pid;
3535 struct got_commit_object *commit = NULL;
3536 id = get_selected_commit_id(s->blame.lines,
3537 s->first_displayed_line, s->selected_line);
3540 err = got_object_open_as_commit(&commit, s->repo, id);
3543 pid = SIMPLEQ_FIRST(
3544 got_object_commit_get_parent_ids(commit));
3545 if (view_is_parent_view(view))
3546 begin_x = view_split_begin_x(view->begin_x);
3547 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3548 if (diff_view == NULL) {
3549 got_object_commit_close(commit);
3550 err = got_error_from_errno("view_open");
3553 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3554 id, NULL, s->refs, s->repo);
3555 got_object_commit_close(commit);
3557 view_close(diff_view);
3560 if (view_is_parent_view(view)) {
3561 err = view_close_child(view);
3564 err = view_set_child(view, diff_view);
3566 view_close(diff_view);
3569 *focus_view = diff_view;
3570 view->child_focussed = 1;
3572 *new_view = diff_view;
3579 if (s->last_displayed_line >= s->blame.nlines &&
3580 s->selected_line >= MIN(s->blame.nlines,
3581 view->nlines - 2)) {
3584 if (s->last_displayed_line >= s->blame.nlines &&
3585 s->selected_line < view->nlines - 2) {
3586 s->selected_line = MIN(s->blame.nlines,
3590 if (s->last_displayed_line + view->nlines - 2
3592 s->first_displayed_line +=
3595 s->first_displayed_line =
3600 if (s->selected_line > view->nlines - 2) {
3601 s->selected_line = MIN(s->blame.nlines,
3608 return thread_err ? thread_err : err;
3611 static const struct got_error *
3612 cmd_blame(int argc, char *argv[])
3614 const struct got_error *error;
3615 struct got_repository *repo = NULL;
3616 struct got_reflist_head refs;
3617 struct got_worktree *worktree = NULL;
3618 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3619 struct got_object_id *commit_id = NULL;
3620 char *commit_id_str = NULL;
3622 struct tog_view *view;
3624 SIMPLEQ_INIT(&refs);
3627 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3632 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3635 commit_id_str = optarg;
3638 repo_path = realpath(optarg, NULL);
3639 if (repo_path == NULL)
3640 err(1, "-r option");
3656 cwd = getcwd(NULL, 0);
3658 error = got_error_from_errno("getcwd");
3661 if (repo_path == NULL) {
3662 error = got_worktree_open(&worktree, cwd);
3663 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3669 strdup(got_worktree_get_repo_path(worktree));
3670 if (repo_path == NULL)
3671 error = got_error_from_errno("strdup");
3675 repo_path = strdup(cwd);
3676 if (repo_path == NULL) {
3677 error = got_error_from_errno("strdup");
3685 error = got_repo_open(&repo, repo_path);
3689 error = apply_unveil(got_repo_get_path(repo), NULL);
3694 const char *prefix = got_worktree_get_path_prefix(worktree);
3695 char *p, *worktree_subdir = cwd +
3696 strlen(got_worktree_get_root_path(worktree));
3697 if (asprintf(&p, "%s%s%s%s%s",
3698 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3699 worktree_subdir, worktree_subdir[0] ? "/" : "",
3701 error = got_error_from_errno("asprintf");
3704 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3707 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3712 if (commit_id_str == NULL) {
3713 struct got_reference *head_ref;
3714 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3717 error = got_ref_resolve(&commit_id, repo, head_ref);
3718 got_ref_close(head_ref);
3720 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3722 if (error->code != GOT_ERR_NOT_REF)
3724 error = got_repo_match_object_id_prefix(&commit_id,
3725 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3731 error = got_ref_list(&refs, repo);
3735 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3737 error = got_error_from_errno("view_open");
3740 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3743 error = view_loop(view);
3749 got_worktree_close(worktree);
3751 got_repo_close(repo);
3752 got_ref_list_free(&refs);
3756 static const struct got_error *
3757 draw_tree_entries(struct tog_view *view,
3758 struct got_tree_entry **first_displayed_entry,
3759 struct got_tree_entry **last_displayed_entry,
3760 struct got_tree_entry **selected_entry, int *ndisplayed,
3761 const char *label, int show_ids, const char *parent_path,
3762 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3764 const struct got_error *err = NULL;
3765 struct got_tree_entry *te;
3771 werase(view->window);
3776 err = format_line(&wline, &width, label, view->ncols);
3779 if (view_needs_focus_indication(view))
3780 wstandout(view->window);
3781 waddwstr(view->window, wline);
3782 if (view_needs_focus_indication(view))
3783 wstandend(view->window);
3786 if (width < view->ncols - 1)
3787 waddch(view->window, '\n');
3790 err = format_line(&wline, &width, parent_path, view->ncols);
3793 waddwstr(view->window, wline);
3796 if (width < view->ncols - 1)
3797 waddch(view->window, '\n');
3800 waddch(view->window, '\n');
3804 te = SIMPLEQ_FIRST(&entries->head);
3805 if (*first_displayed_entry == NULL) {
3806 if (selected == 0) {
3808 wstandout(view->window);
3809 *selected_entry = NULL;
3811 waddstr(view->window, " ..\n"); /* parent directory */
3812 if (selected == 0 && view->focussed)
3813 wstandend(view->window);
3820 while (te != *first_displayed_entry)
3821 te = SIMPLEQ_NEXT(te, entry);
3825 char *line = NULL, *id_str = NULL;
3828 err = got_object_id_str(&id_str, te->id);
3830 return got_error_from_errno(
3831 "got_object_id_str");
3833 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3834 te->name, S_ISDIR(te->mode) ? "/" :
3835 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3837 return got_error_from_errno("asprintf");
3840 err = format_line(&wline, &width, line, view->ncols);
3845 if (n == selected) {
3847 wstandout(view->window);
3848 *selected_entry = te;
3850 waddwstr(view->window, wline);
3851 if (width < view->ncols - 1)
3852 waddch(view->window, '\n');
3853 if (n == selected && view->focussed)
3854 wstandend(view->window);
3860 *last_displayed_entry = te;
3863 te = SIMPLEQ_NEXT(te, entry);
3870 tree_scroll_up(struct tog_view *view,
3871 struct got_tree_entry **first_displayed_entry, int maxscroll,
3872 const struct got_tree_entries *entries, int isroot)
3874 struct got_tree_entry *te, *prev;
3877 if (*first_displayed_entry == NULL)
3880 te = SIMPLEQ_FIRST(&entries->head);
3881 if (*first_displayed_entry == te) {
3883 *first_displayed_entry = NULL;
3887 /* XXX this is stupid... switch to TAILQ? */
3888 for (i = 0; i < maxscroll; i++) {
3889 while (te != *first_displayed_entry) {
3891 te = SIMPLEQ_NEXT(te, entry);
3893 *first_displayed_entry = prev;
3894 te = SIMPLEQ_FIRST(&entries->head);
3896 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3897 *first_displayed_entry = NULL;
3901 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3902 struct got_tree_entry *last_displayed_entry,
3903 const struct got_tree_entries *entries)
3905 struct got_tree_entry *next, *last;
3908 if (*first_displayed_entry)
3909 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3911 next = SIMPLEQ_FIRST(&entries->head);
3912 last = last_displayed_entry;
3913 while (next && last && n++ < maxscroll) {
3914 last = SIMPLEQ_NEXT(last, entry);
3916 *first_displayed_entry = next;
3917 next = SIMPLEQ_NEXT(next, entry);
3923 static const struct got_error *
3924 tree_entry_path(char **path, struct tog_parent_trees *parents,
3925 struct got_tree_entry *te)
3927 const struct got_error *err = NULL;
3928 struct tog_parent_tree *pt;
3929 size_t len = 2; /* for leading slash and NUL */
3931 TAILQ_FOREACH(pt, parents, entry)
3932 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3934 len += strlen(te->name);
3936 *path = calloc(1, len);
3938 return got_error_from_errno("calloc");
3941 pt = TAILQ_LAST(parents, tog_parent_trees);
3943 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3944 err = got_error(GOT_ERR_NO_SPACE);
3947 if (strlcat(*path, "/", len) >= len) {
3948 err = got_error(GOT_ERR_NO_SPACE);
3951 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3954 if (strlcat(*path, te->name, len) >= len) {
3955 err = got_error(GOT_ERR_NO_SPACE);
3967 static const struct got_error *
3968 blame_tree_entry(struct tog_view **new_view, int begin_x,
3969 struct got_tree_entry *te, struct tog_parent_trees *parents,
3970 struct got_object_id *commit_id, struct got_reflist_head *refs,
3971 struct got_repository *repo)
3973 const struct got_error *err = NULL;
3975 struct tog_view *blame_view;
3977 err = tree_entry_path(&path, parents, te);
3981 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3982 if (blame_view == NULL)
3983 return got_error_from_errno("view_open");
3985 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3987 view_close(blame_view);
3990 *new_view = blame_view;
3994 static const struct got_error *
3995 log_tree_entry(struct tog_view **new_view, int begin_x,
3996 struct got_tree_entry *te, struct tog_parent_trees *parents,
3997 struct got_object_id *commit_id, struct got_reflist_head *refs,
3998 struct got_repository *repo)
4000 struct tog_view *log_view;
4001 const struct got_error *err = NULL;
4004 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4005 if (log_view == NULL)
4006 return got_error_from_errno("view_open");
4008 err = tree_entry_path(&path, parents, te);
4012 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4014 view_close(log_view);
4016 *new_view = log_view;
4021 static const struct got_error *
4022 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4023 struct got_object_id *commit_id, struct got_reflist_head *refs,
4024 struct got_repository *repo)
4026 const struct got_error *err = NULL;
4027 char *commit_id_str = NULL;
4028 struct tog_tree_view_state *s = &view->state.tree;
4030 TAILQ_INIT(&s->parents);
4032 err = got_object_id_str(&commit_id_str, commit_id);
4036 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4037 err = got_error_from_errno("asprintf");
4041 s->root = s->tree = root;
4042 s->entries = got_object_tree_get_entries(root);
4043 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4044 s->commit_id = got_object_id_dup(commit_id);
4045 if (s->commit_id == NULL) {
4046 err = got_error_from_errno("got_object_id_dup");
4052 view->show = show_tree_view;
4053 view->input = input_tree_view;
4054 view->close = close_tree_view;
4055 view->search_start = search_start_tree_view;
4056 view->search_next = search_next_tree_view;
4058 free(commit_id_str);
4060 free(s->tree_label);
4061 s->tree_label = NULL;
4066 static const struct got_error *
4067 close_tree_view(struct tog_view *view)
4069 struct tog_tree_view_state *s = &view->state.tree;
4071 free(s->tree_label);
4072 s->tree_label = NULL;
4074 s->commit_id = NULL;
4075 while (!TAILQ_EMPTY(&s->parents)) {
4076 struct tog_parent_tree *parent;
4077 parent = TAILQ_FIRST(&s->parents);
4078 TAILQ_REMOVE(&s->parents, parent, entry);
4082 if (s->tree != s->root)
4083 got_object_tree_close(s->tree);
4084 got_object_tree_close(s->root);
4089 static const struct got_error *
4090 search_start_tree_view(struct tog_view *view)
4092 struct tog_tree_view_state *s = &view->state.tree;
4094 s->matched_entry = NULL;
4099 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4101 regmatch_t regmatch;
4103 return regexec(regex, te->name, 1, ®match, 0) == 0;
4106 static const struct got_error *
4107 search_next_tree_view(struct tog_view *view)
4109 struct tog_tree_view_state *s = &view->state.tree;
4110 struct got_tree_entry *entry, *te;
4112 if (!view->searching) {
4113 view->search_next_done = 1;
4117 if (s->matched_entry) {
4118 if (view->searching == TOG_SEARCH_FORWARD) {
4119 if (s->selected_entry)
4120 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4122 entry = SIMPLEQ_FIRST(&s->entries->head);
4125 if (s->selected_entry == NULL) {
4126 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4129 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4131 if (SIMPLEQ_NEXT(te, entry) ==
4138 if (view->searching == TOG_SEARCH_FORWARD)
4139 entry = SIMPLEQ_FIRST(&s->entries->head);
4141 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4147 if (entry == NULL) {
4148 if (s->matched_entry == NULL) {
4149 view->search_next_done = 1;
4152 if (view->searching == TOG_SEARCH_FORWARD)
4153 entry = SIMPLEQ_FIRST(&s->entries->head);
4155 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4160 if (match_tree_entry(entry, &view->regex)) {
4161 view->search_next_done = 1;
4162 s->matched_entry = entry;
4166 if (view->searching == TOG_SEARCH_FORWARD)
4167 entry = SIMPLEQ_NEXT(entry, entry);
4169 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4172 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4173 if (SIMPLEQ_NEXT(te, entry) == entry) {
4182 if (s->matched_entry) {
4183 s->first_displayed_entry = s->matched_entry;
4190 static const struct got_error *
4191 show_tree_view(struct tog_view *view)
4193 const struct got_error *err = NULL;
4194 struct tog_tree_view_state *s = &view->state.tree;
4197 err = tree_entry_path(&parent_path, &s->parents, NULL);
4201 err = draw_tree_entries(view, &s->first_displayed_entry,
4202 &s->last_displayed_entry, &s->selected_entry,
4203 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4204 s->entries, s->selected, view->nlines, s->tree == s->root);
4211 static const struct got_error *
4212 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4213 struct tog_view **focus_view, struct tog_view *view, int ch)
4215 const struct got_error *err = NULL;
4216 struct tog_tree_view_state *s = &view->state.tree;
4217 struct tog_view *log_view;
4218 int begin_x = 0, nscrolled;
4222 s->show_ids = !s->show_ids;
4225 if (!s->selected_entry)
4227 if (view_is_parent_view(view))
4228 begin_x = view_split_begin_x(view->begin_x);
4229 err = log_tree_entry(&log_view, begin_x,
4230 s->selected_entry, &s->parents,
4231 s->commit_id, s->refs, s->repo);
4232 if (view_is_parent_view(view)) {
4233 err = view_close_child(view);
4236 err = view_set_child(view, log_view);
4238 view_close(log_view);
4241 *focus_view = log_view;
4242 view->child_focussed = 1;
4244 *new_view = log_view;
4248 if (s->selected > 0) {
4250 if (s->selected == 0)
4253 if (s->selected > 0)
4255 tree_scroll_up(view, &s->first_displayed_entry, 1,
4256 s->entries, s->tree == s->root);
4259 tree_scroll_up(view, &s->first_displayed_entry,
4260 MAX(0, view->nlines - 4 - s->selected), s->entries,
4261 s->tree == s->root);
4263 if (SIMPLEQ_FIRST(&s->entries->head) ==
4264 s->first_displayed_entry && s->tree != s->root)
4265 s->first_displayed_entry = NULL;
4269 if (s->selected < s->ndisplayed - 1) {
4273 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4274 /* can't scroll any further */
4276 tree_scroll_down(&s->first_displayed_entry, 1,
4277 s->last_displayed_entry, s->entries);
4280 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4282 /* can't scroll any further; move cursor down */
4283 if (s->selected < s->ndisplayed - 1)
4284 s->selected = s->ndisplayed - 1;
4287 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4288 view->nlines, s->last_displayed_entry, s->entries);
4289 if (nscrolled < view->nlines) {
4291 struct got_tree_entry *te;
4292 te = s->first_displayed_entry;
4295 te = SIMPLEQ_NEXT(te, entry);
4297 s->selected = ndisplayed - 1;
4303 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4304 struct tog_parent_tree *parent;
4305 /* user selected '..' */
4306 if (s->tree == s->root)
4308 parent = TAILQ_FIRST(&s->parents);
4309 TAILQ_REMOVE(&s->parents, parent,
4311 got_object_tree_close(s->tree);
4312 s->tree = parent->tree;
4314 got_object_tree_get_entries(s->tree);
4315 s->first_displayed_entry =
4316 parent->first_displayed_entry;
4318 parent->selected_entry;
4319 s->selected = parent->selected;
4321 } else if (S_ISDIR(s->selected_entry->mode)) {
4322 struct got_tree_object *subtree;
4323 err = got_object_open_as_tree(&subtree,
4324 s->repo, s->selected_entry->id);
4327 err = tree_view_visit_subtree(subtree, s);
4329 got_object_tree_close(subtree);
4332 } else if (S_ISREG(s->selected_entry->mode)) {
4333 struct tog_view *blame_view;
4334 int begin_x = view_is_parent_view(view) ?
4335 view_split_begin_x(view->begin_x) : 0;
4337 err = blame_tree_entry(&blame_view, begin_x,
4338 s->selected_entry, &s->parents,
4339 s->commit_id, s->refs, s->repo);
4342 if (view_is_parent_view(view)) {
4343 err = view_close_child(view);
4346 err = view_set_child(view, blame_view);
4348 view_close(blame_view);
4351 *focus_view = blame_view;
4352 view->child_focussed = 1;
4354 *new_view = blame_view;
4358 if (s->selected > view->nlines)
4359 s->selected = s->ndisplayed - 1;
4372 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4377 static const struct got_error *
4378 cmd_tree(int argc, char *argv[])
4380 const struct got_error *error;
4381 struct got_repository *repo = NULL;
4382 struct got_reflist_head refs;
4383 char *repo_path = NULL;
4384 struct got_object_id *commit_id = NULL;
4385 char *commit_id_arg = NULL;
4386 struct got_commit_object *commit = NULL;
4387 struct got_tree_object *tree = NULL;
4389 struct tog_view *view;
4391 SIMPLEQ_INIT(&refs);
4394 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4399 while ((ch = getopt(argc, argv, "c:")) != -1) {
4402 commit_id_arg = optarg;
4414 struct got_worktree *worktree;
4415 char *cwd = getcwd(NULL, 0);
4417 return got_error_from_errno("getcwd");
4418 error = got_worktree_open(&worktree, cwd);
4419 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4424 strdup(got_worktree_get_repo_path(worktree));
4425 got_worktree_close(worktree);
4428 if (repo_path == NULL) {
4429 error = got_error_from_errno("strdup");
4432 } else if (argc == 1) {
4433 repo_path = realpath(argv[0], NULL);
4434 if (repo_path == NULL)
4435 return got_error_from_errno2("realpath", argv[0]);
4441 error = got_repo_open(&repo, repo_path);
4445 error = apply_unveil(got_repo_get_path(repo), NULL);
4449 if (commit_id_arg == NULL)
4450 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4452 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4454 if (error->code != GOT_ERR_NOT_REF)
4456 error = got_repo_match_object_id_prefix(&commit_id,
4457 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4463 error = got_object_open_as_commit(&commit, repo, commit_id);
4467 error = got_object_open_as_tree(&tree, repo,
4468 got_object_commit_get_tree_id(commit));
4472 error = got_ref_list(&refs, repo);
4476 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4478 error = got_error_from_errno("view_open");
4481 error = open_tree_view(view, tree, commit_id, &refs, repo);
4484 error = view_loop(view);
4489 got_object_commit_close(commit);
4491 got_object_tree_close(tree);
4493 got_repo_close(repo);
4494 got_ref_list_free(&refs);
4503 fprintf(stderr, "commands:");
4504 for (i = 0; i < nitems(tog_commands); i++) {
4505 struct tog_cmd *cmd = &tog_commands[i];
4506 fprintf(stderr, " %s", cmd->name);
4508 fputc('\n', stderr);
4514 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4522 make_argv(const char *arg0, const char *arg1)
4525 int argc = (arg1 == NULL ? 1 : 2);
4527 argv = calloc(argc, sizeof(char *));
4530 argv[0] = strdup(arg0);
4531 if (argv[0] == NULL)
4534 argv[1] = strdup(arg1);
4535 if (argv[1] == NULL)
4543 main(int argc, char *argv[])
4545 const struct got_error *error = NULL;
4546 struct tog_cmd *cmd = NULL;
4547 int ch, hflag = 0, Vflag = 0;
4548 char **cmd_argv = NULL;
4550 setlocale(LC_CTYPE, "");
4552 while ((ch = getopt(argc, argv, "hV")) != -1) {
4572 got_version_print_str();
4579 /* Build an argument vector which runs a default command. */
4580 cmd = &tog_commands[0];
4581 cmd_argv = make_argv(cmd->name, NULL);
4586 /* Did the user specific a command? */
4587 for (i = 0; i < nitems(tog_commands); i++) {
4588 if (strncmp(tog_commands[i].name, argv[0],
4589 strlen(argv[0])) == 0) {
4590 cmd = &tog_commands[i];
4596 fprintf(stderr, "%s: unknown command '%s'\n",
4597 getprogname(), argv[0]);
4606 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4611 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);