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 repo_path = worktree ?
2233 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2234 if (repo_path == NULL) {
2235 error = got_error_from_errno("strdup");
2241 error = got_repo_open(&repo, repo_path);
2245 error = apply_unveil(got_repo_get_path(repo),
2246 worktree ? got_worktree_get_root_path(worktree) : NULL);
2250 if (start_commit == NULL)
2251 error = get_head_commit_id(&start_id, worktree ?
2252 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2255 error = get_head_commit_id(&start_id, start_commit, repo);
2257 if (error->code != GOT_ERR_NOT_REF)
2259 error = got_repo_match_object_id_prefix(&start_id,
2260 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2266 error = got_ref_list(&refs, repo);
2270 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2272 error = got_error_from_errno("view_open");
2275 error = open_log_view(view, start_id, &refs, repo, worktree ?
2276 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2279 error = view_loop(view);
2286 got_repo_close(repo);
2288 got_worktree_close(worktree);
2289 got_ref_list_free(&refs);
2297 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2303 parse_next_line(FILE *f, size_t *len)
2308 const char delim[3] = { '\0', '\0', '\0'};
2310 line = fparseln(f, &linelen, &lineno, delim, 0);
2316 static const struct got_error *
2317 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2318 int *last_displayed_line, int *eof, int max_lines,
2321 const struct got_error *err;
2322 int nlines = 0, nprinted = 0;
2329 werase(view->window);
2332 err = format_line(&wline, &width, header, view->ncols);
2337 if (view_needs_focus_indication(view))
2338 wstandout(view->window);
2339 waddwstr(view->window, wline);
2340 if (view_needs_focus_indication(view))
2341 wstandend(view->window);
2342 if (width < view->ncols - 1)
2343 waddch(view->window, '\n');
2351 while (nprinted < max_lines) {
2352 line = parse_next_line(f, &len);
2357 if (++nlines < *first_displayed_line) {
2362 err = format_line(&wline, &width, line, view->ncols);
2367 waddwstr(view->window, wline);
2368 if (width < view->ncols - 1)
2369 waddch(view->window, '\n');
2370 if (++nprinted == 1)
2371 *first_displayed_line = nlines;
2376 *last_displayed_line = nlines;
2381 while (nprinted < view->nlines) {
2382 waddch(view->window, '\n');
2386 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2391 wstandout(view->window);
2392 waddwstr(view->window, wline);
2393 wstandend(view->window);
2400 get_datestr(time_t *time, char *datebuf)
2402 char *p, *s = ctime_r(time, datebuf);
2403 p = strchr(s, '\n');
2409 static const struct got_error *
2410 write_commit_info(struct got_object_id *commit_id,
2411 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2413 const struct got_error *err = NULL;
2415 struct got_commit_object *commit;
2416 char *id_str = NULL;
2417 time_t committer_time;
2418 const char *author, *committer;
2419 char *refs_str = NULL;
2422 err = build_refs_str(&refs_str, refs, commit_id);
2427 err = got_object_open_as_commit(&commit, repo, commit_id);
2431 err = got_object_id_str(&id_str, commit_id);
2433 err = got_error_from_errno("got_object_id_str");
2437 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2438 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2439 err = got_error_from_errno("fprintf");
2442 if (fprintf(outfile, "from: %s\n",
2443 got_object_commit_get_author(commit)) < 0) {
2444 err = got_error_from_errno("fprintf");
2447 committer_time = got_object_commit_get_committer_time(commit);
2448 if (fprintf(outfile, "date: %s UTC\n",
2449 get_datestr(&committer_time, datebuf)) < 0) {
2450 err = got_error_from_errno("fprintf");
2453 author = got_object_commit_get_author(commit);
2454 committer = got_object_commit_get_committer(commit);
2455 if (strcmp(author, committer) != 0 &&
2456 fprintf(outfile, "via: %s\n", committer) < 0) {
2457 err = got_error_from_errno("fprintf");
2460 if (fprintf(outfile, "%s\n",
2461 got_object_commit_get_logmsg(commit)) < 0) {
2462 err = got_error_from_errno("fprintf");
2468 got_object_commit_close(commit);
2472 static const struct got_error *
2473 create_diff(struct tog_diff_view_state *s)
2475 const struct got_error *err = NULL;
2481 err = got_error_from_errno("got_opentemp");
2484 if (s->f && fclose(s->f) != 0) {
2485 err = got_error_from_errno("fclose");
2491 err = got_object_get_type(&obj_type, s->repo, s->id1);
2493 err = got_object_get_type(&obj_type, s->repo, s->id2);
2498 case GOT_OBJ_TYPE_BLOB:
2499 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2500 s->diff_context, s->repo, f);
2502 case GOT_OBJ_TYPE_TREE:
2503 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2504 s->diff_context, s->repo, f);
2506 case GOT_OBJ_TYPE_COMMIT: {
2507 const struct got_object_id_queue *parent_ids;
2508 struct got_object_qid *pid;
2509 struct got_commit_object *commit2;
2511 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2514 /* Show commit info if we're diffing to a parent/root commit. */
2516 write_commit_info(s->id2, s->refs, s->repo, f);
2518 parent_ids = got_object_commit_get_parent_ids(commit2);
2519 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2520 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2521 write_commit_info(s->id2, s->refs,
2527 got_object_commit_close(commit2);
2529 err = got_diff_objects_as_commits(s->id1, s->id2,
2530 s->diff_context, s->repo, f);
2534 err = got_error(GOT_ERR_OBJ_TYPE);
2538 if (f && fflush(f) != 0 && err == NULL)
2539 err = got_error_from_errno("fflush");
2544 diff_view_indicate_progress(struct tog_view *view)
2546 mvwaddstr(view->window, 0, 0, "diffing...");
2551 static const struct got_error *
2552 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2553 struct got_object_id *id2, struct tog_view *log_view,
2554 struct got_reflist_head *refs, struct got_repository *repo)
2556 const struct got_error *err;
2558 if (id1 != NULL && id2 != NULL) {
2560 err = got_object_get_type(&type1, repo, id1);
2563 err = got_object_get_type(&type2, repo, id2);
2568 return got_error(GOT_ERR_OBJ_TYPE);
2572 view->state.diff.id1 = got_object_id_dup(id1);
2573 if (view->state.diff.id1 == NULL)
2574 return got_error_from_errno("got_object_id_dup");
2576 view->state.diff.id1 = NULL;
2578 view->state.diff.id2 = got_object_id_dup(id2);
2579 if (view->state.diff.id2 == NULL) {
2580 free(view->state.diff.id1);
2581 view->state.diff.id1 = NULL;
2582 return got_error_from_errno("got_object_id_dup");
2584 view->state.diff.f = NULL;
2585 view->state.diff.first_displayed_line = 1;
2586 view->state.diff.last_displayed_line = view->nlines;
2587 view->state.diff.diff_context = 3;
2588 view->state.diff.log_view = log_view;
2589 view->state.diff.repo = repo;
2590 view->state.diff.refs = refs;
2592 if (log_view && view_is_splitscreen(view))
2593 show_log_view(log_view); /* draw vborder */
2594 diff_view_indicate_progress(view);
2596 err = create_diff(&view->state.diff);
2598 free(view->state.diff.id1);
2599 view->state.diff.id1 = NULL;
2600 free(view->state.diff.id2);
2601 view->state.diff.id2 = NULL;
2605 view->show = show_diff_view;
2606 view->input = input_diff_view;
2607 view->close = close_diff_view;
2612 static const struct got_error *
2613 close_diff_view(struct tog_view *view)
2615 const struct got_error *err = NULL;
2617 free(view->state.diff.id1);
2618 view->state.diff.id1 = NULL;
2619 free(view->state.diff.id2);
2620 view->state.diff.id2 = NULL;
2621 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2622 err = got_error_from_errno("fclose");
2626 static const struct got_error *
2627 show_diff_view(struct tog_view *view)
2629 const struct got_error *err;
2630 struct tog_diff_view_state *s = &view->state.diff;
2631 char *id_str1 = NULL, *id_str2, *header;
2634 err = got_object_id_str(&id_str1, s->id1);
2638 err = got_object_id_str(&id_str2, s->id2);
2642 if (asprintf(&header, "diff %s %s",
2643 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2644 err = got_error_from_errno("asprintf");
2652 return draw_file(view, s->f, &s->first_displayed_line,
2653 &s->last_displayed_line, &s->eof, view->nlines,
2657 static const struct got_error *
2658 set_selected_commit(struct tog_diff_view_state *s,
2659 struct commit_queue_entry *entry)
2661 const struct got_error *err;
2662 const struct got_object_id_queue *parent_ids;
2663 struct got_commit_object *selected_commit;
2664 struct got_object_qid *pid;
2667 s->id2 = got_object_id_dup(entry->id);
2669 return got_error_from_errno("got_object_id_dup");
2671 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2674 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2676 pid = SIMPLEQ_FIRST(parent_ids);
2677 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2678 got_object_commit_close(selected_commit);
2682 static const struct got_error *
2683 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2684 struct tog_view **focus_view, struct tog_view *view, int ch)
2686 const struct got_error *err = NULL;
2687 struct tog_diff_view_state *s = &view->state.diff;
2688 struct tog_log_view_state *ls;
2689 struct commit_queue_entry *entry;
2695 if (s->first_displayed_line > 1)
2696 s->first_displayed_line--;
2700 if (s->first_displayed_line == 1)
2703 while (i++ < view->nlines - 1 &&
2704 s->first_displayed_line > 1)
2705 s->first_displayed_line--;
2710 s->first_displayed_line++;
2718 while (!s->eof && i++ < view->nlines - 1) {
2720 line = parse_next_line(s->f, NULL);
2721 s->first_displayed_line++;
2727 if (s->diff_context > 0) {
2729 diff_view_indicate_progress(view);
2730 err = create_diff(s);
2734 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2736 diff_view_indicate_progress(view);
2737 err = create_diff(s);
2742 if (s->log_view == NULL)
2744 ls = &s->log_view->state.log;
2745 entry = TAILQ_PREV(ls->selected_entry,
2746 commit_queue_head, entry);
2750 err = input_log_view(NULL, NULL, NULL, s->log_view,
2755 err = set_selected_commit(s, entry);
2759 s->first_displayed_line = 1;
2760 s->last_displayed_line = view->nlines;
2762 diff_view_indicate_progress(view);
2763 err = create_diff(s);
2767 if (s->log_view == NULL)
2769 ls = &s->log_view->state.log;
2771 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2772 ls->thread_args.commits_needed++;
2774 /* Display "loading..." in log view. */
2775 show_log_view(s->log_view);
2779 err = trigger_log_thread(1 /* load_all */,
2780 &ls->thread_args.commits_needed,
2781 &ls->thread_args.log_complete,
2782 &ls->thread_args.need_commits);
2786 err = input_log_view(NULL, NULL, NULL, s->log_view,
2791 entry = TAILQ_NEXT(ls->selected_entry, entry);
2795 err = set_selected_commit(s, entry);
2799 s->first_displayed_line = 1;
2800 s->last_displayed_line = view->nlines;
2802 diff_view_indicate_progress(view);
2803 err = create_diff(s);
2812 static const struct got_error *
2813 cmd_diff(int argc, char *argv[])
2815 const struct got_error *error = NULL;
2816 struct got_repository *repo = NULL;
2817 struct got_reflist_head refs;
2818 struct got_object_id *id1 = NULL, *id2 = NULL;
2819 char *repo_path = NULL;
2820 char *id_str1 = NULL, *id_str2 = NULL;
2822 struct tog_view *view;
2824 SIMPLEQ_INIT(&refs);
2827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2832 while ((ch = getopt(argc, argv, "")) != -1) {
2844 usage_diff(); /* TODO show local worktree changes */
2845 } else if (argc == 2) {
2846 repo_path = getcwd(NULL, 0);
2847 if (repo_path == NULL)
2848 return got_error_from_errno("getcwd");
2851 } else if (argc == 3) {
2852 repo_path = realpath(argv[0], NULL);
2853 if (repo_path == NULL)
2854 return got_error_from_errno2("realpath", argv[0]);
2862 error = got_repo_open(&repo, repo_path);
2866 error = apply_unveil(got_repo_get_path(repo), NULL);
2870 error = got_repo_match_object_id_prefix(&id1, id_str1,
2871 GOT_OBJ_TYPE_ANY, repo);
2875 error = got_repo_match_object_id_prefix(&id2, id_str2,
2876 GOT_OBJ_TYPE_ANY, repo);
2880 error = got_ref_list(&refs, repo);
2884 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2886 error = got_error_from_errno("view_open");
2889 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2892 error = view_loop(view);
2896 got_repo_close(repo);
2897 got_ref_list_free(&refs);
2905 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2910 struct tog_blame_line {
2912 struct got_object_id *id;
2915 static const struct got_error *
2916 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2917 const char *path, struct tog_blame_line *lines, int nlines,
2918 int blame_complete, int selected_line, int *first_displayed_line,
2919 int *last_displayed_line, int *eof, int max_lines)
2921 const struct got_error *err;
2922 int lineno = 0, nprinted = 0;
2927 struct tog_blame_line *blame_line;
2928 struct got_object_id *prev_id = NULL;
2931 err = got_object_id_str(&id_str, id);
2936 werase(view->window);
2938 if (asprintf(&line, "commit %s", id_str) == -1) {
2939 err = got_error_from_errno("asprintf");
2944 err = format_line(&wline, &width, line, view->ncols);
2947 if (view_needs_focus_indication(view))
2948 wstandout(view->window);
2949 waddwstr(view->window, wline);
2950 if (view_needs_focus_indication(view))
2951 wstandend(view->window);
2954 if (width < view->ncols - 1)
2955 waddch(view->window, '\n');
2957 if (asprintf(&line, "[%d/%d] %s%s",
2958 *first_displayed_line - 1 + selected_line, nlines,
2959 blame_complete ? "" : "annotating... ", path) == -1) {
2961 return got_error_from_errno("asprintf");
2964 err = format_line(&wline, &width, line, view->ncols);
2969 waddwstr(view->window, wline);
2972 if (width < view->ncols - 1)
2973 waddch(view->window, '\n');
2976 while (nprinted < max_lines - 2) {
2977 line = parse_next_line(f, &len);
2982 if (++lineno < *first_displayed_line) {
2987 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2988 err = format_line(&wline, &width, line, wlimit);
2994 if (view->focussed && nprinted == selected_line - 1)
2995 wstandout(view->window);
2997 blame_line = &lines[lineno - 1];
2998 if (blame_line->annotated && prev_id &&
2999 got_object_id_cmp(prev_id, blame_line->id) == 0)
3000 waddstr(view->window, " ");
3001 else if (blame_line->annotated) {
3003 err = got_object_id_str(&id_str, blame_line->id);
3009 wprintw(view->window, "%.8s ", id_str);
3011 prev_id = blame_line->id;
3013 waddstr(view->window, "........ ");
3017 waddwstr(view->window, wline);
3018 while (width < wlimit) {
3019 waddch(view->window, ' ');
3022 if (view->focussed && nprinted == selected_line - 1)
3023 wstandend(view->window);
3024 if (++nprinted == 1)
3025 *first_displayed_line = lineno;
3030 *last_displayed_line = lineno;
3037 static const struct got_error *
3038 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3040 const struct got_error *err = NULL;
3041 struct tog_blame_cb_args *a = arg;
3042 struct tog_blame_line *line;
3045 if (nlines != a->nlines ||
3046 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3047 return got_error(GOT_ERR_RANGE);
3049 errcode = pthread_mutex_lock(&tog_mutex);
3051 return got_error_set_errno(errcode, "pthread_mutex_lock");
3053 if (*a->quit) { /* user has quit the blame view */
3054 err = got_error(GOT_ERR_ITER_COMPLETED);
3059 goto done; /* no change in this commit */
3061 line = &a->lines[lineno - 1];
3062 if (line->annotated)
3065 line->id = got_object_id_dup(id);
3066 if (line->id == NULL) {
3067 err = got_error_from_errno("got_object_id_dup");
3070 line->annotated = 1;
3072 errcode = pthread_mutex_unlock(&tog_mutex);
3074 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3079 blame_thread(void *arg)
3081 const struct got_error *err;
3082 struct tog_blame_thread_args *ta = arg;
3083 struct tog_blame_cb_args *a = ta->cb_args;
3086 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3087 blame_cb, ta->cb_args);
3089 errcode = pthread_mutex_lock(&tog_mutex);
3091 return (void *)got_error_set_errno(errcode,
3092 "pthread_mutex_lock");
3094 got_repo_close(ta->repo);
3098 errcode = pthread_mutex_unlock(&tog_mutex);
3099 if (errcode && err == NULL)
3100 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3105 static struct got_object_id *
3106 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3109 struct tog_blame_line *line;
3111 line = &lines[first_displayed_line - 1 + selected_line - 1];
3112 if (!line->annotated)
3118 static const struct got_error *
3119 stop_blame(struct tog_blame *blame)
3121 const struct got_error *err = NULL;
3124 if (blame->thread) {
3126 errcode = pthread_mutex_unlock(&tog_mutex);
3128 return got_error_set_errno(errcode,
3129 "pthread_mutex_unlock");
3130 errcode = pthread_join(blame->thread, (void **)&err);
3132 return got_error_set_errno(errcode, "pthread_join");
3133 errcode = pthread_mutex_lock(&tog_mutex);
3135 return got_error_set_errno(errcode,
3136 "pthread_mutex_lock");
3137 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3139 blame->thread = NULL;
3141 if (blame->thread_args.repo) {
3142 got_repo_close(blame->thread_args.repo);
3143 blame->thread_args.repo = NULL;
3146 if (fclose(blame->f) != 0 && err == NULL)
3147 err = got_error_from_errno("fclose");
3151 for (i = 0; i < blame->nlines; i++)
3152 free(blame->lines[i].id);
3154 blame->lines = NULL;
3156 free(blame->cb_args.commit_id);
3157 blame->cb_args.commit_id = NULL;
3162 static const struct got_error *
3163 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3164 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3165 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3166 struct got_repository *repo)
3168 const struct got_error *err = NULL;
3169 struct got_blob_object *blob = NULL;
3170 struct got_repository *thread_repo = NULL;
3171 struct got_object_id *obj_id = NULL;
3174 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3178 return got_error(GOT_ERR_NO_OBJ);
3180 err = got_object_get_type(&obj_type, repo, obj_id);
3184 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3185 err = got_error(GOT_ERR_OBJ_TYPE);
3189 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3192 blame->f = got_opentemp();
3193 if (blame->f == NULL) {
3194 err = got_error_from_errno("got_opentemp");
3197 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3198 &blame->line_offsets, blame->f, blob);
3202 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3203 if (blame->lines == NULL) {
3204 err = got_error_from_errno("calloc");
3208 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3212 blame->cb_args.view = view;
3213 blame->cb_args.lines = blame->lines;
3214 blame->cb_args.nlines = blame->nlines;
3215 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3216 if (blame->cb_args.commit_id == NULL) {
3217 err = got_error_from_errno("got_object_id_dup");
3220 blame->cb_args.quit = done;
3222 blame->thread_args.path = path;
3223 blame->thread_args.repo = thread_repo;
3224 blame->thread_args.cb_args = &blame->cb_args;
3225 blame->thread_args.complete = blame_complete;
3226 *blame_complete = 0;
3230 got_object_blob_close(blob);
3237 static const struct got_error *
3238 open_blame_view(struct tog_view *view, char *path,
3239 struct got_object_id *commit_id, struct got_reflist_head *refs,
3240 struct got_repository *repo)
3242 const struct got_error *err = NULL;
3243 struct tog_blame_view_state *s = &view->state.blame;
3245 SIMPLEQ_INIT(&s->blamed_commits);
3247 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3251 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3252 s->first_displayed_line = 1;
3253 s->last_displayed_line = view->nlines;
3254 s->selected_line = 1;
3255 s->blame_complete = 0;
3257 if (s->path == NULL)
3258 return got_error_from_errno("open_blame_view");
3261 s->commit_id = commit_id;
3262 memset(&s->blame, 0, sizeof(s->blame));
3264 view->show = show_blame_view;
3265 view->input = input_blame_view;
3266 view->close = close_blame_view;
3267 view->search_start = search_start_blame_view;
3268 view->search_next = search_next_blame_view;
3270 return run_blame(&s->blame, view, &s->blame_complete,
3271 &s->first_displayed_line, &s->last_displayed_line,
3272 &s->selected_line, &s->done, &s->eof, s->path,
3273 s->blamed_commit->id, s->repo);
3276 static const struct got_error *
3277 close_blame_view(struct tog_view *view)
3279 const struct got_error *err = NULL;
3280 struct tog_blame_view_state *s = &view->state.blame;
3282 if (s->blame.thread)
3283 err = stop_blame(&s->blame);
3285 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3286 struct got_object_qid *blamed_commit;
3287 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3288 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3289 got_object_qid_free(blamed_commit);
3297 static const struct got_error *
3298 search_start_blame_view(struct tog_view *view)
3300 struct tog_blame_view_state *s = &view->state.blame;
3302 s->matched_line = 0;
3307 match_line(const char *line, regex_t *regex)
3309 regmatch_t regmatch;
3311 return regexec(regex, line, 1, ®match, 0) == 0;
3315 static const struct got_error *
3316 search_next_blame_view(struct tog_view *view)
3318 struct tog_blame_view_state *s = &view->state.blame;
3321 if (!view->searching) {
3322 view->search_next_done = 1;
3326 if (s->matched_line) {
3327 if (view->searching == TOG_SEARCH_FORWARD)
3328 lineno = s->matched_line + 1;
3330 lineno = s->matched_line - 1;
3332 if (view->searching == TOG_SEARCH_FORWARD)
3335 lineno = s->blame.nlines;
3343 if (lineno <= 0 || lineno > s->blame.nlines) {
3344 if (s->matched_line == 0) {
3345 view->search_next_done = 1;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3353 lineno = s->blame.nlines;
3356 offset = s->blame.line_offsets[lineno - 1];
3357 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3359 return got_error_from_errno("fseeko");
3362 line = parse_next_line(s->blame.f, &len);
3363 if (line && match_line(line, &view->regex)) {
3364 view->search_next_done = 1;
3365 s->matched_line = lineno;
3370 if (view->searching == TOG_SEARCH_FORWARD)
3376 if (s->matched_line) {
3377 s->first_displayed_line = s->matched_line;
3378 s->selected_line = 1;
3384 static const struct got_error *
3385 show_blame_view(struct tog_view *view)
3387 const struct got_error *err = NULL;
3388 struct tog_blame_view_state *s = &view->state.blame;
3391 if (s->blame.thread == NULL) {
3392 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3393 &s->blame.thread_args);
3395 return got_error_set_errno(errcode, "pthread_create");
3398 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3399 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3400 s->selected_line, &s->first_displayed_line,
3401 &s->last_displayed_line, &s->eof, view->nlines);
3407 static const struct got_error *
3408 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3409 struct tog_view **focus_view, struct tog_view *view, int ch)
3411 const struct got_error *err = NULL, *thread_err = NULL;
3412 struct tog_view *diff_view;
3413 struct tog_blame_view_state *s = &view->state.blame;
3422 if (s->selected_line > 1)
3424 else if (s->selected_line == 1 &&
3425 s->first_displayed_line > 1)
3426 s->first_displayed_line--;
3429 if (s->first_displayed_line == 1) {
3430 s->selected_line = 1;
3433 if (s->first_displayed_line > view->nlines - 2)
3434 s->first_displayed_line -=
3437 s->first_displayed_line = 1;
3441 if (s->selected_line < view->nlines - 2 &&
3442 s->first_displayed_line +
3443 s->selected_line <= s->blame.nlines)
3445 else if (s->last_displayed_line <
3447 s->first_displayed_line++;
3451 struct got_object_id *id = NULL;
3452 id = get_selected_commit_id(s->blame.lines,
3453 s->first_displayed_line, s->selected_line);
3457 struct got_commit_object *commit;
3458 struct got_object_qid *pid;
3459 struct got_object_id *blob_id = NULL;
3461 err = got_object_open_as_commit(&commit,
3465 pid = SIMPLEQ_FIRST(
3466 got_object_commit_get_parent_ids(commit));
3468 got_object_commit_close(commit);
3471 /* Check if path history ends here. */
3472 err = got_object_id_by_path(&blob_id, s->repo,
3475 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3477 got_object_commit_close(commit);
3480 err = got_object_get_type(&obj_type, s->repo,
3483 /* Can't blame non-blob type objects. */
3484 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3485 got_object_commit_close(commit);
3488 err = got_object_qid_alloc(&s->blamed_commit,
3490 got_object_commit_close(commit);
3492 if (got_object_id_cmp(id,
3493 s->blamed_commit->id) == 0)
3495 err = got_object_qid_alloc(&s->blamed_commit,
3501 thread_err = stop_blame(&s->blame);
3505 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3506 s->blamed_commit, entry);
3507 err = run_blame(&s->blame, view, &s->blame_complete,
3508 &s->first_displayed_line, &s->last_displayed_line,
3509 &s->selected_line, &s->done, &s->eof,
3510 s->path, s->blamed_commit->id, s->repo);
3516 struct got_object_qid *first;
3517 first = SIMPLEQ_FIRST(&s->blamed_commits);
3518 if (!got_object_id_cmp(first->id, s->commit_id))
3521 thread_err = stop_blame(&s->blame);
3525 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3526 got_object_qid_free(s->blamed_commit);
3528 SIMPLEQ_FIRST(&s->blamed_commits);
3529 err = run_blame(&s->blame, view, &s->blame_complete,
3530 &s->first_displayed_line, &s->last_displayed_line,
3531 &s->selected_line, &s->done, &s->eof, s->path,
3532 s->blamed_commit->id, s->repo);
3539 struct got_object_id *id = NULL;
3540 struct got_object_qid *pid;
3541 struct got_commit_object *commit = NULL;
3542 id = get_selected_commit_id(s->blame.lines,
3543 s->first_displayed_line, s->selected_line);
3546 err = got_object_open_as_commit(&commit, s->repo, id);
3549 pid = SIMPLEQ_FIRST(
3550 got_object_commit_get_parent_ids(commit));
3551 if (view_is_parent_view(view))
3552 begin_x = view_split_begin_x(view->begin_x);
3553 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3554 if (diff_view == NULL) {
3555 got_object_commit_close(commit);
3556 err = got_error_from_errno("view_open");
3559 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3560 id, NULL, s->refs, s->repo);
3561 got_object_commit_close(commit);
3563 view_close(diff_view);
3566 if (view_is_parent_view(view)) {
3567 err = view_close_child(view);
3570 err = view_set_child(view, diff_view);
3572 view_close(diff_view);
3575 *focus_view = diff_view;
3576 view->child_focussed = 1;
3578 *new_view = diff_view;
3585 if (s->last_displayed_line >= s->blame.nlines &&
3586 s->selected_line >= MIN(s->blame.nlines,
3587 view->nlines - 2)) {
3590 if (s->last_displayed_line >= s->blame.nlines &&
3591 s->selected_line < view->nlines - 2) {
3592 s->selected_line = MIN(s->blame.nlines,
3596 if (s->last_displayed_line + view->nlines - 2
3598 s->first_displayed_line +=
3601 s->first_displayed_line =
3606 if (s->selected_line > view->nlines - 2) {
3607 s->selected_line = MIN(s->blame.nlines,
3614 return thread_err ? thread_err : err;
3617 static const struct got_error *
3618 cmd_blame(int argc, char *argv[])
3620 const struct got_error *error;
3621 struct got_repository *repo = NULL;
3622 struct got_reflist_head refs;
3623 struct got_worktree *worktree = NULL;
3624 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3625 struct got_object_id *commit_id = NULL;
3626 char *commit_id_str = NULL;
3628 struct tog_view *view;
3630 SIMPLEQ_INIT(&refs);
3633 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3638 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3641 commit_id_str = optarg;
3644 repo_path = realpath(optarg, NULL);
3645 if (repo_path == NULL)
3646 err(1, "-r option");
3662 cwd = getcwd(NULL, 0);
3664 error = got_error_from_errno("getcwd");
3667 if (repo_path == NULL) {
3668 error = got_worktree_open(&worktree, cwd);
3669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3675 strdup(got_worktree_get_repo_path(worktree));
3676 if (repo_path == NULL)
3677 error = got_error_from_errno("strdup");
3681 repo_path = strdup(cwd);
3682 if (repo_path == NULL) {
3683 error = got_error_from_errno("strdup");
3691 error = got_repo_open(&repo, repo_path);
3695 error = apply_unveil(got_repo_get_path(repo), NULL);
3700 const char *prefix = got_worktree_get_path_prefix(worktree);
3701 char *p, *worktree_subdir = cwd +
3702 strlen(got_worktree_get_root_path(worktree));
3703 if (asprintf(&p, "%s%s%s%s%s",
3704 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3705 worktree_subdir, worktree_subdir[0] ? "/" : "",
3707 error = got_error_from_errno("asprintf");
3710 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3713 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3718 if (commit_id_str == NULL) {
3719 struct got_reference *head_ref;
3720 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3723 error = got_ref_resolve(&commit_id, repo, head_ref);
3724 got_ref_close(head_ref);
3726 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3728 if (error->code != GOT_ERR_NOT_REF)
3730 error = got_repo_match_object_id_prefix(&commit_id,
3731 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3737 error = got_ref_list(&refs, repo);
3741 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3743 error = got_error_from_errno("view_open");
3746 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3749 error = view_loop(view);
3755 got_worktree_close(worktree);
3757 got_repo_close(repo);
3758 got_ref_list_free(&refs);
3762 static const struct got_error *
3763 draw_tree_entries(struct tog_view *view,
3764 struct got_tree_entry **first_displayed_entry,
3765 struct got_tree_entry **last_displayed_entry,
3766 struct got_tree_entry **selected_entry, int *ndisplayed,
3767 const char *label, int show_ids, const char *parent_path,
3768 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3770 const struct got_error *err = NULL;
3771 struct got_tree_entry *te;
3777 werase(view->window);
3782 err = format_line(&wline, &width, label, view->ncols);
3785 if (view_needs_focus_indication(view))
3786 wstandout(view->window);
3787 waddwstr(view->window, wline);
3788 if (view_needs_focus_indication(view))
3789 wstandend(view->window);
3792 if (width < view->ncols - 1)
3793 waddch(view->window, '\n');
3796 err = format_line(&wline, &width, parent_path, view->ncols);
3799 waddwstr(view->window, wline);
3802 if (width < view->ncols - 1)
3803 waddch(view->window, '\n');
3806 waddch(view->window, '\n');
3810 te = SIMPLEQ_FIRST(&entries->head);
3811 if (*first_displayed_entry == NULL) {
3812 if (selected == 0) {
3814 wstandout(view->window);
3815 *selected_entry = NULL;
3817 waddstr(view->window, " ..\n"); /* parent directory */
3818 if (selected == 0 && view->focussed)
3819 wstandend(view->window);
3826 while (te != *first_displayed_entry)
3827 te = SIMPLEQ_NEXT(te, entry);
3831 char *line = NULL, *id_str = NULL;
3834 err = got_object_id_str(&id_str, te->id);
3836 return got_error_from_errno(
3837 "got_object_id_str");
3839 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3840 te->name, S_ISDIR(te->mode) ? "/" :
3841 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3843 return got_error_from_errno("asprintf");
3846 err = format_line(&wline, &width, line, view->ncols);
3851 if (n == selected) {
3853 wstandout(view->window);
3854 *selected_entry = te;
3856 waddwstr(view->window, wline);
3857 if (width < view->ncols - 1)
3858 waddch(view->window, '\n');
3859 if (n == selected && view->focussed)
3860 wstandend(view->window);
3866 *last_displayed_entry = te;
3869 te = SIMPLEQ_NEXT(te, entry);
3876 tree_scroll_up(struct tog_view *view,
3877 struct got_tree_entry **first_displayed_entry, int maxscroll,
3878 const struct got_tree_entries *entries, int isroot)
3880 struct got_tree_entry *te, *prev;
3883 if (*first_displayed_entry == NULL)
3886 te = SIMPLEQ_FIRST(&entries->head);
3887 if (*first_displayed_entry == te) {
3889 *first_displayed_entry = NULL;
3893 /* XXX this is stupid... switch to TAILQ? */
3894 for (i = 0; i < maxscroll; i++) {
3895 while (te != *first_displayed_entry) {
3897 te = SIMPLEQ_NEXT(te, entry);
3899 *first_displayed_entry = prev;
3900 te = SIMPLEQ_FIRST(&entries->head);
3902 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3903 *first_displayed_entry = NULL;
3907 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3908 struct got_tree_entry *last_displayed_entry,
3909 const struct got_tree_entries *entries)
3911 struct got_tree_entry *next, *last;
3914 if (*first_displayed_entry)
3915 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3917 next = SIMPLEQ_FIRST(&entries->head);
3918 last = last_displayed_entry;
3919 while (next && last && n++ < maxscroll) {
3920 last = SIMPLEQ_NEXT(last, entry);
3922 *first_displayed_entry = next;
3923 next = SIMPLEQ_NEXT(next, entry);
3929 static const struct got_error *
3930 tree_entry_path(char **path, struct tog_parent_trees *parents,
3931 struct got_tree_entry *te)
3933 const struct got_error *err = NULL;
3934 struct tog_parent_tree *pt;
3935 size_t len = 2; /* for leading slash and NUL */
3937 TAILQ_FOREACH(pt, parents, entry)
3938 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3940 len += strlen(te->name);
3942 *path = calloc(1, len);
3944 return got_error_from_errno("calloc");
3947 pt = TAILQ_LAST(parents, tog_parent_trees);
3949 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3950 err = got_error(GOT_ERR_NO_SPACE);
3953 if (strlcat(*path, "/", len) >= len) {
3954 err = got_error(GOT_ERR_NO_SPACE);
3957 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3960 if (strlcat(*path, te->name, len) >= len) {
3961 err = got_error(GOT_ERR_NO_SPACE);
3973 static const struct got_error *
3974 blame_tree_entry(struct tog_view **new_view, int begin_x,
3975 struct got_tree_entry *te, struct tog_parent_trees *parents,
3976 struct got_object_id *commit_id, struct got_reflist_head *refs,
3977 struct got_repository *repo)
3979 const struct got_error *err = NULL;
3981 struct tog_view *blame_view;
3983 err = tree_entry_path(&path, parents, te);
3987 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3988 if (blame_view == NULL)
3989 return got_error_from_errno("view_open");
3991 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3993 view_close(blame_view);
3996 *new_view = blame_view;
4000 static const struct got_error *
4001 log_tree_entry(struct tog_view **new_view, int begin_x,
4002 struct got_tree_entry *te, struct tog_parent_trees *parents,
4003 struct got_object_id *commit_id, struct got_reflist_head *refs,
4004 struct got_repository *repo)
4006 struct tog_view *log_view;
4007 const struct got_error *err = NULL;
4010 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4011 if (log_view == NULL)
4012 return got_error_from_errno("view_open");
4014 err = tree_entry_path(&path, parents, te);
4018 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4020 view_close(log_view);
4022 *new_view = log_view;
4027 static const struct got_error *
4028 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4029 struct got_object_id *commit_id, struct got_reflist_head *refs,
4030 struct got_repository *repo)
4032 const struct got_error *err = NULL;
4033 char *commit_id_str = NULL;
4034 struct tog_tree_view_state *s = &view->state.tree;
4036 TAILQ_INIT(&s->parents);
4038 err = got_object_id_str(&commit_id_str, commit_id);
4042 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4043 err = got_error_from_errno("asprintf");
4047 s->root = s->tree = root;
4048 s->entries = got_object_tree_get_entries(root);
4049 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4050 s->commit_id = got_object_id_dup(commit_id);
4051 if (s->commit_id == NULL) {
4052 err = got_error_from_errno("got_object_id_dup");
4058 view->show = show_tree_view;
4059 view->input = input_tree_view;
4060 view->close = close_tree_view;
4061 view->search_start = search_start_tree_view;
4062 view->search_next = search_next_tree_view;
4064 free(commit_id_str);
4066 free(s->tree_label);
4067 s->tree_label = NULL;
4072 static const struct got_error *
4073 close_tree_view(struct tog_view *view)
4075 struct tog_tree_view_state *s = &view->state.tree;
4077 free(s->tree_label);
4078 s->tree_label = NULL;
4080 s->commit_id = NULL;
4081 while (!TAILQ_EMPTY(&s->parents)) {
4082 struct tog_parent_tree *parent;
4083 parent = TAILQ_FIRST(&s->parents);
4084 TAILQ_REMOVE(&s->parents, parent, entry);
4088 if (s->tree != s->root)
4089 got_object_tree_close(s->tree);
4090 got_object_tree_close(s->root);
4095 static const struct got_error *
4096 search_start_tree_view(struct tog_view *view)
4098 struct tog_tree_view_state *s = &view->state.tree;
4100 s->matched_entry = NULL;
4105 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4107 regmatch_t regmatch;
4109 return regexec(regex, te->name, 1, ®match, 0) == 0;
4112 static const struct got_error *
4113 search_next_tree_view(struct tog_view *view)
4115 struct tog_tree_view_state *s = &view->state.tree;
4116 struct got_tree_entry *entry, *te;
4118 if (!view->searching) {
4119 view->search_next_done = 1;
4123 if (s->matched_entry) {
4124 if (view->searching == TOG_SEARCH_FORWARD) {
4125 if (s->selected_entry)
4126 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4128 entry = SIMPLEQ_FIRST(&s->entries->head);
4131 if (s->selected_entry == NULL) {
4132 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4135 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4137 if (SIMPLEQ_NEXT(te, entry) ==
4144 if (view->searching == TOG_SEARCH_FORWARD)
4145 entry = SIMPLEQ_FIRST(&s->entries->head);
4147 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4153 if (entry == NULL) {
4154 if (s->matched_entry == NULL) {
4155 view->search_next_done = 1;
4158 if (view->searching == TOG_SEARCH_FORWARD)
4159 entry = SIMPLEQ_FIRST(&s->entries->head);
4161 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4166 if (match_tree_entry(entry, &view->regex)) {
4167 view->search_next_done = 1;
4168 s->matched_entry = entry;
4172 if (view->searching == TOG_SEARCH_FORWARD)
4173 entry = SIMPLEQ_NEXT(entry, entry);
4175 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4178 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4179 if (SIMPLEQ_NEXT(te, entry) == entry) {
4188 if (s->matched_entry) {
4189 s->first_displayed_entry = s->matched_entry;
4196 static const struct got_error *
4197 show_tree_view(struct tog_view *view)
4199 const struct got_error *err = NULL;
4200 struct tog_tree_view_state *s = &view->state.tree;
4203 err = tree_entry_path(&parent_path, &s->parents, NULL);
4207 err = draw_tree_entries(view, &s->first_displayed_entry,
4208 &s->last_displayed_entry, &s->selected_entry,
4209 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4210 s->entries, s->selected, view->nlines, s->tree == s->root);
4217 static const struct got_error *
4218 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4219 struct tog_view **focus_view, struct tog_view *view, int ch)
4221 const struct got_error *err = NULL;
4222 struct tog_tree_view_state *s = &view->state.tree;
4223 struct tog_view *log_view;
4224 int begin_x = 0, nscrolled;
4228 s->show_ids = !s->show_ids;
4231 if (!s->selected_entry)
4233 if (view_is_parent_view(view))
4234 begin_x = view_split_begin_x(view->begin_x);
4235 err = log_tree_entry(&log_view, begin_x,
4236 s->selected_entry, &s->parents,
4237 s->commit_id, s->refs, s->repo);
4238 if (view_is_parent_view(view)) {
4239 err = view_close_child(view);
4242 err = view_set_child(view, log_view);
4244 view_close(log_view);
4247 *focus_view = log_view;
4248 view->child_focussed = 1;
4250 *new_view = log_view;
4254 if (s->selected > 0) {
4256 if (s->selected == 0)
4259 if (s->selected > 0)
4261 tree_scroll_up(view, &s->first_displayed_entry, 1,
4262 s->entries, s->tree == s->root);
4265 tree_scroll_up(view, &s->first_displayed_entry,
4266 MAX(0, view->nlines - 4 - s->selected), s->entries,
4267 s->tree == s->root);
4269 if (SIMPLEQ_FIRST(&s->entries->head) ==
4270 s->first_displayed_entry && s->tree != s->root)
4271 s->first_displayed_entry = NULL;
4275 if (s->selected < s->ndisplayed - 1) {
4279 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4280 /* can't scroll any further */
4282 tree_scroll_down(&s->first_displayed_entry, 1,
4283 s->last_displayed_entry, s->entries);
4286 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4288 /* can't scroll any further; move cursor down */
4289 if (s->selected < s->ndisplayed - 1)
4290 s->selected = s->ndisplayed - 1;
4293 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4294 view->nlines, s->last_displayed_entry, s->entries);
4295 if (nscrolled < view->nlines) {
4297 struct got_tree_entry *te;
4298 te = s->first_displayed_entry;
4301 te = SIMPLEQ_NEXT(te, entry);
4303 s->selected = ndisplayed - 1;
4309 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4310 struct tog_parent_tree *parent;
4311 /* user selected '..' */
4312 if (s->tree == s->root)
4314 parent = TAILQ_FIRST(&s->parents);
4315 TAILQ_REMOVE(&s->parents, parent,
4317 got_object_tree_close(s->tree);
4318 s->tree = parent->tree;
4320 got_object_tree_get_entries(s->tree);
4321 s->first_displayed_entry =
4322 parent->first_displayed_entry;
4324 parent->selected_entry;
4325 s->selected = parent->selected;
4327 } else if (S_ISDIR(s->selected_entry->mode)) {
4328 struct got_tree_object *subtree;
4329 err = got_object_open_as_tree(&subtree,
4330 s->repo, s->selected_entry->id);
4333 err = tree_view_visit_subtree(subtree, s);
4335 got_object_tree_close(subtree);
4338 } else if (S_ISREG(s->selected_entry->mode)) {
4339 struct tog_view *blame_view;
4340 int begin_x = view_is_parent_view(view) ?
4341 view_split_begin_x(view->begin_x) : 0;
4343 err = blame_tree_entry(&blame_view, begin_x,
4344 s->selected_entry, &s->parents,
4345 s->commit_id, s->refs, s->repo);
4348 if (view_is_parent_view(view)) {
4349 err = view_close_child(view);
4352 err = view_set_child(view, blame_view);
4354 view_close(blame_view);
4357 *focus_view = blame_view;
4358 view->child_focussed = 1;
4360 *new_view = blame_view;
4364 if (s->selected > view->nlines)
4365 s->selected = s->ndisplayed - 1;
4378 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4383 static const struct got_error *
4384 cmd_tree(int argc, char *argv[])
4386 const struct got_error *error;
4387 struct got_repository *repo = NULL;
4388 struct got_reflist_head refs;
4389 char *repo_path = NULL;
4390 struct got_object_id *commit_id = NULL;
4391 char *commit_id_arg = NULL;
4392 struct got_commit_object *commit = NULL;
4393 struct got_tree_object *tree = NULL;
4395 struct tog_view *view;
4397 SIMPLEQ_INIT(&refs);
4400 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4405 while ((ch = getopt(argc, argv, "c:")) != -1) {
4408 commit_id_arg = optarg;
4420 struct got_worktree *worktree;
4421 char *cwd = getcwd(NULL, 0);
4423 return got_error_from_errno("getcwd");
4424 error = got_worktree_open(&worktree, cwd);
4425 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4430 strdup(got_worktree_get_repo_path(worktree));
4431 got_worktree_close(worktree);
4434 if (repo_path == NULL) {
4435 error = got_error_from_errno("strdup");
4438 } else if (argc == 1) {
4439 repo_path = realpath(argv[0], NULL);
4440 if (repo_path == NULL)
4441 return got_error_from_errno2("realpath", argv[0]);
4447 error = got_repo_open(&repo, repo_path);
4451 error = apply_unveil(got_repo_get_path(repo), NULL);
4455 if (commit_id_arg == NULL)
4456 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4458 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4460 if (error->code != GOT_ERR_NOT_REF)
4462 error = got_repo_match_object_id_prefix(&commit_id,
4463 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4469 error = got_object_open_as_commit(&commit, repo, commit_id);
4473 error = got_object_open_as_tree(&tree, repo,
4474 got_object_commit_get_tree_id(commit));
4478 error = got_ref_list(&refs, repo);
4482 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4484 error = got_error_from_errno("view_open");
4487 error = open_tree_view(view, tree, commit_id, &refs, repo);
4490 error = view_loop(view);
4495 got_object_commit_close(commit);
4497 got_object_tree_close(tree);
4499 got_repo_close(repo);
4500 got_ref_list_free(&refs);
4509 fprintf(stderr, "commands:");
4510 for (i = 0; i < nitems(tog_commands); i++) {
4511 struct tog_cmd *cmd = &tog_commands[i];
4512 fprintf(stderr, " %s", cmd->name);
4514 fputc('\n', stderr);
4520 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4528 make_argv(const char *arg0, const char *arg1)
4531 int argc = (arg1 == NULL ? 1 : 2);
4533 argv = calloc(argc, sizeof(char *));
4536 argv[0] = strdup(arg0);
4537 if (argv[0] == NULL)
4540 argv[1] = strdup(arg1);
4541 if (argv[1] == NULL)
4549 main(int argc, char *argv[])
4551 const struct got_error *error = NULL;
4552 struct tog_cmd *cmd = NULL;
4553 int ch, hflag = 0, Vflag = 0;
4554 char **cmd_argv = NULL;
4556 setlocale(LC_CTYPE, "");
4558 while ((ch = getopt(argc, argv, "hV")) != -1) {
4578 got_version_print_str();
4585 /* Build an argument vector which runs a default command. */
4586 cmd = &tog_commands[0];
4587 cmd_argv = make_argv(cmd->name, NULL);
4592 /* Did the user specific a command? */
4593 for (i = 0; i < nitems(tog_commands); i++) {
4594 if (strncmp(tog_commands[i].name, argv[0],
4595 strlen(argv[0])) == 0) {
4596 cmd = &tog_commands[i];
4602 fprintf(stderr, "%s: unknown command '%s'\n",
4603 getprogname(), argv[0]);
4612 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4617 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);