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"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
54 #include "got_worktree.h"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #define CTRL(x) ((x) & 0x1f)
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
112 struct commit_queue_head head;
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
118 int first_displayed_line;
119 int last_displayed_line;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
141 struct tog_view *view;
142 struct commit_queue_entry **first_displayed_entry;
143 struct commit_queue_entry **selected_entry;
146 struct tog_log_view_state {
147 struct commit_queue commits;
148 struct commit_queue_entry *first_displayed_entry;
149 struct commit_queue_entry *last_displayed_entry;
150 struct commit_queue_entry *selected_entry;
153 const char *head_ref_name;
154 struct got_repository *repo;
155 struct got_reflist_head *refs;
156 struct got_object_id *start_id;
159 struct tog_log_thread_args thread_args;
160 struct commit_queue_entry *matched_entry;
161 struct commit_queue_entry *search_entry;
164 struct tog_blame_cb_args {
165 struct tog_blame_line *lines; /* one per line */
168 struct tog_view *view;
169 struct got_object_id *commit_id;
173 struct tog_blame_thread_args {
175 struct got_repository *repo;
176 struct tog_blame_cb_args *cb_args;
183 struct tog_blame_line *lines;
187 struct tog_blame_thread_args thread_args;
188 struct tog_blame_cb_args cb_args;
192 struct tog_blame_view_state {
193 int first_displayed_line;
194 int last_displayed_line;
199 struct got_object_id_queue blamed_commits;
200 struct got_object_qid *blamed_commit;
202 struct got_repository *repo;
203 struct got_reflist_head *refs;
204 struct got_object_id *commit_id;
205 struct tog_blame blame;
209 struct tog_parent_tree {
210 TAILQ_ENTRY(tog_parent_tree) entry;
211 struct got_tree_object *tree;
212 struct got_tree_entry *first_displayed_entry;
213 struct got_tree_entry *selected_entry;
217 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
219 struct tog_tree_view_state {
221 struct got_tree_object *root;
222 struct got_tree_object *tree;
223 const struct got_tree_entries *entries;
224 struct got_tree_entry *first_displayed_entry;
225 struct got_tree_entry *last_displayed_entry;
226 struct got_tree_entry *selected_entry;
227 int ndisplayed, selected, show_ids;
228 struct tog_parent_trees parents;
229 struct got_object_id *commit_id;
230 struct got_repository *repo;
231 struct got_reflist_head *refs;
232 struct got_tree_entry *matched_entry;
236 * We implement two types of views: parent views and child views.
238 * The 'Tab' key switches between a parent view and its child view.
239 * Child views are shown side-by-side to their parent view, provided
240 * there is enough screen estate.
242 * When a new view is opened from within a parent view, this new view
243 * becomes a child view of the parent view, replacing any existing child.
245 * When a new view is opened from within a child view, this new view
246 * becomes a parent view which will obscure the views below until the
247 * user quits the new parent view by typing 'q'.
249 * This list of views contains parent views only.
250 * Child views are only pointed to by their parent view.
252 TAILQ_HEAD(tog_view_list_head, tog_view);
255 TAILQ_ENTRY(tog_view) entry;
258 int nlines, ncols, begin_y, begin_x;
259 int lines, cols; /* copies of LINES and COLS */
261 struct tog_view *parent;
262 struct tog_view *child;
265 /* type-specific state */
266 enum tog_view_type type;
268 struct tog_diff_view_state diff;
269 struct tog_log_view_state log;
270 struct tog_blame_view_state blame;
271 struct tog_tree_view_state tree;
274 const struct got_error *(*show)(struct tog_view *);
275 const struct got_error *(*input)(struct tog_view **,
276 struct tog_view **, struct tog_view**, struct tog_view *, int);
277 const struct got_error *(*close)(struct tog_view *);
279 const struct got_error *(*search_start)(struct tog_view *);
280 const struct got_error *(*search_next)(struct tog_view *);
282 #define TOG_SEARCH_FORWARD 1
283 #define TOG_SEARCH_BACKWARD 2
284 int search_next_done;
288 static const struct got_error *open_diff_view(struct tog_view *,
289 struct got_object_id *, struct got_object_id *, struct tog_view *,
290 struct got_reflist_head *, struct got_repository *);
291 static const struct got_error *show_diff_view(struct tog_view *);
292 static const struct got_error *input_diff_view(struct tog_view **,
293 struct tog_view **, struct tog_view **, struct tog_view *, int);
294 static const struct got_error* close_diff_view(struct tog_view *);
296 static const struct got_error *open_log_view(struct tog_view *,
297 struct got_object_id *, struct got_reflist_head *,
298 struct got_repository *, const char *, const char *, int);
299 static const struct got_error * show_log_view(struct tog_view *);
300 static const struct got_error *input_log_view(struct tog_view **,
301 struct tog_view **, struct tog_view **, struct tog_view *, int);
302 static const struct got_error *close_log_view(struct tog_view *);
303 static const struct got_error *search_start_log_view(struct tog_view *);
304 static const struct got_error *search_next_log_view(struct tog_view *);
306 static const struct got_error *open_blame_view(struct tog_view *, char *,
307 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
308 static const struct got_error *show_blame_view(struct tog_view *);
309 static const struct got_error *input_blame_view(struct tog_view **,
310 struct tog_view **, struct tog_view **, struct tog_view *, int);
311 static const struct got_error *close_blame_view(struct tog_view *);
312 static const struct got_error *search_start_blame_view(struct tog_view *);
313 static const struct got_error *search_next_blame_view(struct tog_view *);
315 static const struct got_error *open_tree_view(struct tog_view *,
316 struct got_tree_object *, struct got_object_id *,
317 struct got_reflist_head *, struct got_repository *);
318 static const struct got_error *show_tree_view(struct tog_view *);
319 static const struct got_error *input_tree_view(struct tog_view **,
320 struct tog_view **, struct tog_view **, struct tog_view *, int);
321 static const struct got_error *close_tree_view(struct tog_view *);
322 static const struct got_error *search_start_tree_view(struct tog_view *);
323 static const struct got_error *search_next_tree_view(struct tog_view *);
325 static volatile sig_atomic_t tog_sigwinch_received;
326 static volatile sig_atomic_t tog_sigpipe_received;
329 tog_sigwinch(int signo)
331 tog_sigwinch_received = 1;
335 tog_sigpipe(int signo)
337 tog_sigpipe_received = 1;
340 static const struct got_error *
341 view_close(struct tog_view *view)
343 const struct got_error *err = NULL;
346 view_close(view->child);
350 err = view->close(view);
352 del_panel(view->panel);
354 delwin(view->window);
359 static struct tog_view *
360 view_open(int nlines, int ncols, int begin_y, int begin_x,
361 enum tog_view_type type)
363 struct tog_view *view = calloc(1, sizeof(*view));
371 view->nlines = nlines ? nlines : LINES - begin_y;
372 view->ncols = ncols ? ncols : COLS - begin_x;
373 view->begin_y = begin_y;
374 view->begin_x = begin_x;
375 view->window = newwin(nlines, ncols, begin_y, begin_x);
376 if (view->window == NULL) {
380 view->panel = new_panel(view->window);
381 if (view->panel == NULL ||
382 set_panel_userptr(view->panel, view) != OK) {
387 keypad(view->window, TRUE);
392 view_split_begin_x(int begin_x)
394 if (begin_x > 0 || COLS < 120)
396 return (COLS - MAX(COLS / 2, 80));
399 static const struct got_error *view_resize(struct tog_view *);
401 static const struct got_error *
402 view_splitscreen(struct tog_view *view)
404 const struct got_error *err = NULL;
407 view->begin_x = view_split_begin_x(0);
408 view->nlines = LINES;
409 view->ncols = COLS - view->begin_x;
412 err = view_resize(view);
416 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
417 return got_error_from_errno("mvwin");
422 static const struct got_error *
423 view_fullscreen(struct tog_view *view)
425 const struct got_error *err = NULL;
429 view->nlines = LINES;
433 err = view_resize(view);
437 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
438 return got_error_from_errno("mvwin");
444 view_is_parent_view(struct tog_view *view)
446 return view->parent == NULL;
449 static const struct got_error *
450 view_resize(struct tog_view *view)
454 if (view->lines > LINES)
455 nlines = view->nlines - (view->lines - LINES);
457 nlines = view->nlines + (LINES - view->lines);
459 if (view->cols > COLS)
460 ncols = view->ncols - (view->cols - COLS);
462 ncols = view->ncols + (COLS - view->cols);
464 if (wresize(view->window, nlines, ncols) == ERR)
465 return got_error_from_errno("wresize");
466 if (replace_panel(view->panel, view->window) == ERR)
467 return got_error_from_errno("replace_panel");
468 wclear(view->window);
470 view->nlines = nlines;
476 view->child->begin_x = view_split_begin_x(view->begin_x);
477 if (view->child->begin_x == 0) {
478 view_fullscreen(view->child);
479 if (view->child->focussed)
480 show_panel(view->child->panel);
482 show_panel(view->panel);
484 view_splitscreen(view->child);
485 show_panel(view->child->panel);
492 static const struct got_error *
493 view_close_child(struct tog_view *view)
495 const struct got_error *err = NULL;
497 if (view->child == NULL)
500 err = view_close(view->child);
505 static const struct got_error *
506 view_set_child(struct tog_view *view, struct tog_view *child)
508 const struct got_error *err = NULL;
511 child->parent = view;
516 view_is_splitscreen(struct tog_view *view)
518 return view->begin_x > 0;
527 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
528 cols = 80; /* Default */
534 resize_term(lines, cols);
537 static const struct got_error *
538 view_search_start(struct tog_view *view)
540 const struct got_error *err = NULL;
545 if (view->nlines < 1)
548 if (!view_is_parent_view(view))
549 begin_x = view_split_begin_x(view->begin_x);
550 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
552 wclrtoeol(view->window);
556 ret = wgetnstr(view->window, pattern, sizeof(pattern));
562 if (view->searching) {
563 regfree(&view->regex);
567 if (regcomp(&view->regex, pattern,
568 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
569 err = view->search_start(view);
571 regfree(&view->regex);
574 view->searching = TOG_SEARCH_FORWARD;
575 view->search_next_done = 0;
576 view->search_next(view);
582 static const struct got_error *
583 view_input(struct tog_view **new, struct tog_view **dead,
584 struct tog_view **focus, int *done, struct tog_view *view,
585 struct tog_view_list_head *views)
587 const struct got_error *err = NULL;
595 if (view->searching && !view->search_next_done) {
596 errcode = pthread_mutex_unlock(&tog_mutex);
598 return got_error_set_errno(errcode,
599 "pthread_mutex_unlock");
601 errcode = pthread_mutex_lock(&tog_mutex);
603 return got_error_set_errno(errcode,
604 "pthread_mutex_lock");
605 view->search_next(view);
609 nodelay(stdscr, FALSE);
610 /* Allow threads to make progress while we are waiting for input. */
611 errcode = pthread_mutex_unlock(&tog_mutex);
613 return got_error_set_errno(errcode, "pthread_mutex_unlock");
614 ch = wgetch(view->window);
615 errcode = pthread_mutex_lock(&tog_mutex);
617 return got_error_set_errno(errcode, "pthread_mutex_lock");
618 nodelay(stdscr, TRUE);
620 if (tog_sigwinch_received) {
622 tog_sigwinch_received = 0;
623 TAILQ_FOREACH(v, views, entry) {
624 err = view_resize(v);
627 err = v->input(new, dead, focus, v, KEY_RESIZE);
638 *focus = view->child;
639 view->child_focussed = 1;
640 } else if (view->parent) {
641 *focus = view->parent;
642 view->parent->child_focussed = 0;
646 err = view->input(new, dead, focus, view, ch);
653 if (view_is_parent_view(view)) {
654 if (view->child == NULL)
656 if (view_is_splitscreen(view->child)) {
657 *focus = view->child;
658 view->child_focussed = 1;
659 err = view_fullscreen(view->child);
661 err = view_splitscreen(view->child);
664 err = view->child->input(new, dead, focus,
665 view->child, KEY_RESIZE);
667 if (view_is_splitscreen(view)) {
669 view->parent->child_focussed = 1;
670 err = view_fullscreen(view);
672 err = view_splitscreen(view);
676 err = view->input(new, dead, focus, view,
683 if (view->search_start)
684 view_search_start(view);
686 err = view->input(new, dead, focus, view, ch);
690 if (view->search_next && view->searching) {
691 view->searching = (ch == 'n' ?
692 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
693 view->search_next_done = 0;
694 view->search_next(view);
696 err = view->input(new, dead, focus, view, ch);
699 err = view->input(new, dead, focus, view, ch);
707 view_vborder(struct tog_view *view)
710 struct tog_view *view_above;
713 return view_vborder(view->parent);
715 panel = panel_above(view->panel);
719 view_above = panel_userptr(panel);
720 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
721 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
725 view_needs_focus_indication(struct tog_view *view)
727 if (view_is_parent_view(view)) {
728 if (view->child == NULL || view->child_focussed)
730 if (!view_is_splitscreen(view->child))
732 } else if (!view_is_splitscreen(view))
735 return view->focussed;
738 static const struct got_error *
739 view_loop(struct tog_view *view)
741 const struct got_error *err = NULL;
742 struct tog_view_list_head views;
743 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
744 int fast_refresh = 10;
745 int done = 0, errcode;
747 errcode = pthread_mutex_lock(&tog_mutex);
749 return got_error_set_errno(errcode, "pthread_mutex_lock");
752 TAILQ_INSERT_HEAD(&views, view, entry);
756 err = view->show(view);
761 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
762 /* Refresh fast during initialization, then become slower. */
763 if (fast_refresh && fast_refresh-- == 0)
764 halfdelay(10); /* switch to once per second */
766 err = view_input(&new_view, &dead_view, &focus_view, &done,
771 struct tog_view *prev = NULL;
773 if (view_is_parent_view(dead_view))
774 prev = TAILQ_PREV(dead_view,
775 tog_view_list_head, entry);
776 else if (view->parent != dead_view)
779 if (dead_view->parent)
780 dead_view->parent->child = NULL;
782 TAILQ_REMOVE(&views, dead_view, entry);
784 err = view_close(dead_view);
785 if (err || (dead_view == main_view && new_view == NULL))
788 if (view == dead_view) {
793 else if (!TAILQ_EMPTY(&views))
794 view = TAILQ_LAST(&views,
799 if (view->child && view->child_focussed)
800 focus_view = view->child;
807 struct tog_view *v, *t;
808 /* Only allow one parent view per type. */
809 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
810 if (v->type != new_view->type)
812 TAILQ_REMOVE(&views, v, entry);
818 TAILQ_INSERT_TAIL(&views, new_view, entry);
820 if (focus_view == NULL)
821 focus_view = new_view;
824 show_panel(focus_view->panel);
827 focus_view->focussed = 1;
830 show_panel(new_view->panel);
831 if (view->child && view_is_splitscreen(view->child))
832 show_panel(view->child->panel);
835 if (focus_view == NULL) {
837 show_panel(view->panel);
838 if (view->child && view_is_splitscreen(view->child))
839 show_panel(view->child->panel);
843 err = view->parent->show(view->parent);
847 err = view->show(view);
851 err = view->child->show(view->child);
860 while (!TAILQ_EMPTY(&views)) {
861 view = TAILQ_FIRST(&views);
862 TAILQ_REMOVE(&views, view, entry);
866 errcode = pthread_mutex_unlock(&tog_mutex);
867 if (errcode && err == NULL)
868 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
878 "usage: %s log [-c commit] [-r repository-path] [path]\n",
883 /* Create newly allocated wide-character string equivalent to a byte string. */
884 static const struct got_error *
885 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
888 const struct got_error *err = NULL;
891 *wlen = mbstowcs(NULL, s, 0);
892 if (*wlen == (size_t)-1) {
895 return got_error_from_errno("mbstowcs");
897 /* byte string invalid in current encoding; try to "fix" it */
898 err = got_mbsavis(&vis, &vislen, s);
901 *wlen = mbstowcs(NULL, vis, 0);
902 if (*wlen == (size_t)-1) {
903 err = got_error_from_errno("mbstowcs"); /* give up */
908 *ws = calloc(*wlen + 1, sizeof(*ws));
910 err = got_error_from_errno("calloc");
914 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
915 err = got_error_from_errno("mbstowcs");
926 /* Format a line for display, ensuring that it won't overflow a width limit. */
927 static const struct got_error *
928 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
930 const struct got_error *err = NULL;
932 wchar_t *wline = NULL;
939 err = mbs2ws(&wline, &wlen, line);
944 while (i < wlen && cols < wlimit) {
945 int width = wcwidth(wline[i]);
952 if (cols + width <= wlimit)
957 if (wline[i] == L'\t')
958 cols += TABSIZE - ((cols + 1) % TABSIZE);
962 err = got_error_from_errno("wcwidth");
977 static const struct got_error*
978 build_refs_str(char **refs_str, struct got_reflist_head *refs,
979 struct got_object_id *id, struct got_repository *repo)
981 static const struct got_error *err = NULL;
982 struct got_reflist_entry *re;
988 SIMPLEQ_FOREACH(re, refs, entry) {
989 struct got_tag_object *tag = NULL;
992 name = got_ref_get_name(re->ref);
993 if (strcmp(name, GOT_REF_HEAD) == 0)
995 if (strncmp(name, "refs/", 5) == 0)
997 if (strncmp(name, "got/", 4) == 0)
999 if (strncmp(name, "heads/", 6) == 0)
1001 if (strncmp(name, "remotes/", 8) == 0)
1003 if (strncmp(name, "tags/", 5) == 0) {
1004 err = got_object_open_as_tag(&tag, repo, re->id);
1006 if (err->code != GOT_ERR_OBJ_TYPE)
1008 /* Ref points at something other than a tag. */
1013 cmp = got_object_id_cmp(tag ?
1014 got_object_tag_get_object_id(tag) : re->id, id);
1016 got_object_tag_close(tag);
1020 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1021 s ? ", " : "", name) == -1) {
1022 err = got_error_from_errno("asprintf");
1033 static const struct got_error *
1034 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1036 char *smallerthan, *at;
1038 smallerthan = strchr(author, '<');
1039 if (smallerthan && smallerthan[1] != '\0')
1040 author = smallerthan + 1;
1041 at = strchr(author, '@');
1044 return format_line(wauthor, author_width, author, limit);
1047 static const struct got_error *
1048 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1049 struct got_object_id *id, struct got_reflist_head *refs,
1050 int author_display_cols)
1052 const struct got_error *err = NULL;
1053 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1054 char *logmsg0 = NULL, *logmsg = NULL;
1055 char *author = NULL;
1056 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1057 int author_width, logmsg_width;
1058 char *newline, *line = NULL;
1060 static const size_t date_display_cols = 9;
1061 const int avail = view->ncols;
1063 time_t committer_time;
1065 committer_time = got_object_commit_get_committer_time(commit);
1066 if (localtime_r(&committer_time, &tm) == NULL)
1067 return got_error_from_errno("localtime_r");
1068 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1070 return got_error(GOT_ERR_NO_SPACE);
1072 if (avail < date_display_cols)
1073 limit = MIN(sizeof(datebuf) - 1, avail);
1075 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1076 waddnstr(view->window, datebuf, limit);
1081 author = strdup(got_object_commit_get_author(commit));
1082 if (author == NULL) {
1083 err = got_error_from_errno("strdup");
1086 err = format_author(&wauthor, &author_width, author, avail - col);
1089 waddwstr(view->window, wauthor);
1090 col += author_width;
1091 while (col <= avail && author_width < author_display_cols + 2) {
1092 waddch(view->window, ' ');
1099 err = got_object_commit_get_logmsg(&logmsg0, commit);
1103 while (*logmsg == '\n')
1105 newline = strchr(logmsg, '\n');
1108 limit = avail - col;
1109 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1112 waddwstr(view->window, wlogmsg);
1113 col += logmsg_width;
1114 while (col <= avail) {
1115 waddch(view->window, ' ');
1127 static struct commit_queue_entry *
1128 alloc_commit_queue_entry(struct got_commit_object *commit,
1129 struct got_object_id *id)
1131 struct commit_queue_entry *entry;
1133 entry = calloc(1, sizeof(*entry));
1138 entry->commit = commit;
1143 pop_commit(struct commit_queue *commits)
1145 struct commit_queue_entry *entry;
1147 entry = TAILQ_FIRST(&commits->head);
1148 TAILQ_REMOVE(&commits->head, entry, entry);
1149 got_object_commit_close(entry->commit);
1150 commits->ncommits--;
1151 /* Don't free entry->id! It is owned by the commit graph. */
1156 free_commits(struct commit_queue *commits)
1158 while (!TAILQ_EMPTY(&commits->head))
1159 pop_commit(commits);
1162 static const struct got_error *
1163 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1164 int minqueue, struct got_repository *repo, const char *path)
1166 const struct got_error *err = NULL;
1170 * We keep all commits open throughout the lifetime of the log
1171 * view in order to avoid having to re-fetch commits from disk
1172 * while updating the display.
1174 while (nqueued < minqueue) {
1175 struct got_object_id *id;
1176 struct got_commit_object *commit;
1177 struct commit_queue_entry *entry;
1180 err = got_commit_graph_iter_next(&id, graph);
1182 if (err->code != GOT_ERR_ITER_NEED_MORE)
1184 err = got_commit_graph_fetch_commits(graph,
1185 minqueue, repo, NULL, NULL);
1194 err = got_object_open_as_commit(&commit, repo, id);
1197 entry = alloc_commit_queue_entry(commit, id);
1198 if (entry == NULL) {
1199 err = got_error_from_errno("alloc_commit_queue_entry");
1203 errcode = pthread_mutex_lock(&tog_mutex);
1205 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1209 entry->idx = commits->ncommits;
1210 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1212 commits->ncommits++;
1214 errcode = pthread_mutex_unlock(&tog_mutex);
1215 if (errcode && err == NULL)
1216 err = got_error_set_errno(errcode,
1217 "pthread_mutex_unlock");
1223 static const struct got_error *
1224 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1225 struct got_repository *repo)
1227 const struct got_error *err = NULL;
1228 struct got_reference *head_ref;
1232 err = got_ref_open(&head_ref, repo, branch_name, 0);
1236 err = got_ref_resolve(head_id, repo, head_ref);
1237 got_ref_close(head_ref);
1246 static const struct got_error *
1247 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1248 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1249 struct commit_queue *commits, int selected_idx, int limit,
1250 struct got_reflist_head *refs, const char *path, int commits_needed)
1252 const struct got_error *err = NULL;
1253 struct tog_log_view_state *s = &view->state.log;
1254 struct commit_queue_entry *entry;
1256 int ncommits, author_cols = 10;
1257 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1258 char *refs_str = NULL;
1264 if (ncommits == selected_idx) {
1268 entry = TAILQ_NEXT(entry, entry);
1272 if (*selected && !(view->searching && view->search_next_done == 0)) {
1273 err = got_object_id_str(&id_str, (*selected)->id);
1277 err = build_refs_str(&refs_str, refs, (*selected)->id,
1284 if (commits_needed == 0)
1285 halfdelay(10); /* disable fast refresh */
1287 if (asprintf(&ncommits_str, " [%d/%d] %s",
1288 entry ? entry->idx + 1 : 0, commits->ncommits,
1289 commits_needed > 0 ?
1290 (view->searching && view->search_next_done == 0
1291 ? "searching..." : "loading... ") :
1292 (refs_str ? refs_str : "")) == -1) {
1293 err = got_error_from_errno("asprintf");
1297 if (path && strcmp(path, "/") != 0) {
1298 if (asprintf(&header, "commit %s %s%s",
1299 id_str ? id_str : "........................................",
1300 path, ncommits_str) == -1) {
1301 err = got_error_from_errno("asprintf");
1305 } else if (asprintf(&header, "commit %s%s",
1306 id_str ? id_str : "........................................",
1307 ncommits_str) == -1) {
1308 err = got_error_from_errno("asprintf");
1312 err = format_line(&wline, &width, header, view->ncols);
1316 werase(view->window);
1318 if (view_needs_focus_indication(view))
1319 wstandout(view->window);
1320 waddwstr(view->window, wline);
1321 while (width < view->ncols) {
1322 waddch(view->window, ' ');
1325 if (view_needs_focus_indication(view))
1326 wstandend(view->window);
1331 /* Grow author column size if necessary. */
1338 if (ncommits >= limit - 1)
1340 author = strdup(got_object_commit_get_author(entry->commit));
1341 if (author == NULL) {
1342 err = got_error_from_errno("strdup");
1345 err = format_author(&wauthor, &width, author, COLS);
1346 if (author_cols < width)
1347 author_cols = width;
1350 entry = TAILQ_NEXT(entry, entry);
1357 if (ncommits >= limit - 1)
1359 if (ncommits == selected_idx)
1360 wstandout(view->window);
1361 err = draw_commit(view, entry->commit, entry->id, refs,
1363 if (ncommits == selected_idx)
1364 wstandend(view->window);
1369 entry = TAILQ_NEXT(entry, entry);
1382 scroll_up(struct tog_view *view,
1383 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1384 struct commit_queue *commits)
1386 struct commit_queue_entry *entry;
1389 entry = TAILQ_FIRST(&commits->head);
1390 if (*first_displayed_entry == entry)
1393 entry = *first_displayed_entry;
1394 while (entry && nscrolled < maxscroll) {
1395 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1397 *first_displayed_entry = entry;
1403 static const struct got_error *
1404 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1405 pthread_cond_t *need_commits)
1410 halfdelay(1); /* fast refresh while loading commits */
1412 while (*commits_needed > 0) {
1416 /* Wake the log thread. */
1417 errcode = pthread_cond_signal(need_commits);
1419 return got_error_set_errno(errcode,
1420 "pthread_cond_signal");
1421 errcode = pthread_mutex_unlock(&tog_mutex);
1423 return got_error_set_errno(errcode,
1424 "pthread_mutex_unlock");
1426 errcode = pthread_mutex_lock(&tog_mutex);
1428 return got_error_set_errno(errcode,
1429 "pthread_mutex_lock");
1431 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1433 * Thread is not done yet; lose a key press
1434 * and let the user retry... this way the GUI
1435 * remains interactive while logging deep paths
1436 * with few commits in history.
1445 static const struct got_error *
1446 scroll_down(struct tog_view *view,
1447 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1448 struct commit_queue_entry **last_displayed_entry,
1449 struct commit_queue *commits, int *log_complete, int *commits_needed,
1450 pthread_cond_t *need_commits)
1452 const struct got_error *err = NULL;
1453 struct commit_queue_entry *pentry;
1456 if (*last_displayed_entry == NULL)
1459 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1460 if (pentry == NULL && !*log_complete) {
1462 * Ask the log thread for required amount of commits
1463 * plus some amount of pre-fetching.
1465 (*commits_needed) += maxscroll + 20;
1466 err = trigger_log_thread(0, commits_needed, log_complete,
1473 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1477 *last_displayed_entry = pentry;
1479 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1482 *first_displayed_entry = pentry;
1483 } while (++nscrolled < maxscroll);
1488 static const struct got_error *
1489 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1490 struct got_commit_object *commit, struct got_object_id *commit_id,
1491 struct tog_view *log_view, struct got_reflist_head *refs,
1492 struct got_repository *repo)
1494 const struct got_error *err;
1495 struct got_object_qid *parent_id;
1496 struct tog_view *diff_view;
1498 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1499 if (diff_view == NULL)
1500 return got_error_from_errno("view_open");
1502 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1504 commit_id, log_view, refs, repo);
1506 *new_view = diff_view;
1510 static const struct got_error *
1511 tree_view_visit_subtree(struct got_tree_object *subtree,
1512 struct tog_tree_view_state *s)
1514 struct tog_parent_tree *parent;
1516 parent = calloc(1, sizeof(*parent));
1518 return got_error_from_errno("calloc");
1520 parent->tree = s->tree;
1521 parent->first_displayed_entry = s->first_displayed_entry;
1522 parent->selected_entry = s->selected_entry;
1523 parent->selected = s->selected;
1524 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1526 s->entries = got_object_tree_get_entries(s->tree);
1528 s->first_displayed_entry = NULL;
1533 static const struct got_error *
1534 browse_commit_tree(struct tog_view **new_view, int begin_x,
1535 struct commit_queue_entry *entry, const char *path,
1536 struct got_reflist_head *refs, struct got_repository *repo)
1538 const struct got_error *err = NULL;
1539 struct got_tree_object *tree;
1540 struct got_tree_entry *te;
1541 struct tog_tree_view_state *s;
1542 struct tog_view *tree_view;
1543 char *slash, *subpath = NULL;
1546 err = got_object_open_as_tree(&tree, repo,
1547 got_object_commit_get_tree_id(entry->commit));
1551 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1552 if (tree_view == NULL)
1553 return got_error_from_errno("view_open");
1555 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1557 got_object_tree_close(tree);
1560 s = &tree_view->state.tree;
1562 *new_view = tree_view;
1564 /* Walk the path and open corresponding tree objects. */
1569 struct got_object_id *tree_id;
1571 /* Ensure the correct subtree entry is selected. */
1572 slash = strchr(p, '/');
1574 slash = strchr(p, '\0');
1575 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1576 if (strncmp(p, te->name, slash - p) == 0) {
1577 s->selected_entry = te;
1582 if (s->selected_entry == NULL) {
1583 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1586 if (s->tree != s->root)
1587 s->selected++; /* skip '..' */
1589 if (!S_ISDIR(s->selected_entry->mode)) {
1590 /* Jump to this file's entry. */
1591 s->first_displayed_entry = s->selected_entry;
1596 slash = strchr(p, '/');
1598 subpath = strndup(path, slash - path);
1600 subpath = strdup(path);
1601 if (subpath == NULL) {
1602 err = got_error_from_errno("strdup");
1606 err = got_object_id_by_path(&tree_id, repo, entry->id,
1611 err = got_object_open_as_tree(&tree, repo, tree_id);
1616 err = tree_view_visit_subtree(tree, s);
1618 got_object_tree_close(tree);
1633 log_thread(void *arg)
1635 const struct got_error *err = NULL;
1637 struct tog_log_thread_args *a = arg;
1640 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1645 while (!done && !err && !tog_sigpipe_received) {
1646 err = queue_commits(a->graph, a->commits, 1, a->repo,
1649 if (err->code != GOT_ERR_ITER_COMPLETED)
1653 } else if (a->commits_needed > 0)
1654 a->commits_needed--;
1656 errcode = pthread_mutex_lock(&tog_mutex);
1658 err = got_error_set_errno(errcode,
1659 "pthread_mutex_lock");
1661 } else if (*a->quit)
1663 else if (*a->first_displayed_entry == NULL) {
1664 *a->first_displayed_entry =
1665 TAILQ_FIRST(&a->commits->head);
1666 *a->selected_entry = *a->first_displayed_entry;
1670 a->commits_needed = 0;
1671 else if (a->commits_needed == 0) {
1672 errcode = pthread_cond_wait(&a->need_commits,
1675 err = got_error_set_errno(errcode,
1676 "pthread_cond_wait");
1679 errcode = pthread_mutex_unlock(&tog_mutex);
1680 if (errcode && err == NULL)
1681 err = got_error_set_errno(errcode,
1682 "pthread_mutex_unlock");
1684 a->log_complete = 1;
1688 static const struct got_error *
1689 stop_log_thread(struct tog_log_view_state *s)
1691 const struct got_error *err = NULL;
1696 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1698 return got_error_set_errno(errcode,
1699 "pthread_cond_signal");
1700 errcode = pthread_mutex_unlock(&tog_mutex);
1702 return got_error_set_errno(errcode,
1703 "pthread_mutex_unlock");
1704 errcode = pthread_join(s->thread, (void **)&err);
1706 return got_error_set_errno(errcode, "pthread_join");
1707 errcode = pthread_mutex_lock(&tog_mutex);
1709 return got_error_set_errno(errcode,
1710 "pthread_mutex_lock");
1714 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1715 if (errcode && err == NULL)
1716 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1718 if (s->thread_args.repo) {
1719 got_repo_close(s->thread_args.repo);
1720 s->thread_args.repo = NULL;
1723 if (s->thread_args.graph) {
1724 got_commit_graph_close(s->thread_args.graph);
1725 s->thread_args.graph = NULL;
1731 static const struct got_error *
1732 close_log_view(struct tog_view *view)
1734 const struct got_error *err = NULL;
1735 struct tog_log_view_state *s = &view->state.log;
1737 err = stop_log_thread(s);
1738 free_commits(&s->commits);
1739 free(s->in_repo_path);
1740 s->in_repo_path = NULL;
1746 static const struct got_error *
1747 search_start_log_view(struct tog_view *view)
1749 struct tog_log_view_state *s = &view->state.log;
1751 s->matched_entry = NULL;
1752 s->search_entry = NULL;
1757 match_commit(struct got_commit_object *commit, const char *id_str,
1758 const char *logmsg, regex_t *regex)
1760 regmatch_t regmatch;
1762 if (regexec(regex, got_object_commit_get_author(commit), 1,
1763 ®match, 0) == 0 ||
1764 regexec(regex, got_object_commit_get_committer(commit), 1,
1765 ®match, 0) == 0 ||
1766 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1767 regexec(regex, logmsg, 1, ®match, 0) == 0)
1773 static const struct got_error *
1774 search_next_log_view(struct tog_view *view)
1776 const struct got_error *err = NULL;
1777 struct tog_log_view_state *s = &view->state.log;
1778 struct commit_queue_entry *entry;
1780 if (!view->searching) {
1781 view->search_next_done = 1;
1785 if (s->search_entry) {
1786 if (wgetch(view->window) == KEY_BACKSPACE) {
1787 view->search_next_done = 1;
1790 if (view->searching == TOG_SEARCH_FORWARD)
1791 entry = TAILQ_NEXT(s->search_entry, entry);
1793 entry = TAILQ_PREV(s->search_entry,
1794 commit_queue_head, entry);
1795 } else if (s->matched_entry) {
1796 if (view->searching == TOG_SEARCH_FORWARD)
1797 entry = TAILQ_NEXT(s->selected_entry, entry);
1799 entry = TAILQ_PREV(s->selected_entry,
1800 commit_queue_head, entry);
1802 if (view->searching == TOG_SEARCH_FORWARD)
1803 entry = TAILQ_FIRST(&s->commits.head);
1805 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1809 char *id_str, *logmsg;
1810 if (entry == NULL) {
1811 if (s->thread_args.log_complete ||
1812 view->searching == TOG_SEARCH_BACKWARD) {
1813 view->search_next_done = 1;
1817 * Poke the log thread for more commits and return,
1818 * allowing the main loop to make progress. Search
1819 * will resume at s->search_entry once we come back.
1821 s->thread_args.commits_needed++;
1822 return trigger_log_thread(1,
1823 &s->thread_args.commits_needed,
1824 &s->thread_args.log_complete,
1825 &s->thread_args.need_commits);
1828 err = got_object_id_str(&id_str, entry->id);
1832 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1835 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1837 view->search_next_done = 1;
1838 s->matched_entry = entry;
1844 s->search_entry = entry;
1845 if (view->searching == TOG_SEARCH_FORWARD)
1846 entry = TAILQ_NEXT(entry, entry);
1848 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1851 if (s->matched_entry) {
1852 int cur = s->selected_entry->idx;
1853 while (cur < s->matched_entry->idx) {
1854 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1859 while (cur > s->matched_entry->idx) {
1860 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1867 s->search_entry = NULL;
1872 static const struct got_error *
1873 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1874 struct got_reflist_head *refs, struct got_repository *repo,
1875 const char *head_ref_name, const char *path, int check_disk)
1877 const struct got_error *err = NULL;
1878 struct tog_log_view_state *s = &view->state.log;
1879 struct got_repository *thread_repo = NULL;
1880 struct got_commit_graph *thread_graph = NULL;
1883 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1887 /* The commit queue only contains commits being displayed. */
1888 TAILQ_INIT(&s->commits.head);
1889 s->commits.ncommits = 0;
1893 s->head_ref_name = head_ref_name;
1894 s->start_id = got_object_id_dup(start_id);
1895 if (s->start_id == NULL) {
1896 err = got_error_from_errno("got_object_id_dup");
1900 view->show = show_log_view;
1901 view->input = input_log_view;
1902 view->close = close_log_view;
1903 view->search_start = search_start_log_view;
1904 view->search_next = search_next_log_view;
1906 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1909 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1914 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1916 err = got_error_set_errno(errcode, "pthread_cond_init");
1920 s->thread_args.commits_needed = view->nlines;
1921 s->thread_args.graph = thread_graph;
1922 s->thread_args.commits = &s->commits;
1923 s->thread_args.in_repo_path = s->in_repo_path;
1924 s->thread_args.start_id = s->start_id;
1925 s->thread_args.repo = thread_repo;
1926 s->thread_args.log_complete = 0;
1927 s->thread_args.quit = &s->quit;
1928 s->thread_args.view = view;
1929 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1930 s->thread_args.selected_entry = &s->selected_entry;
1933 close_log_view(view);
1937 static const struct got_error *
1938 show_log_view(struct tog_view *view)
1940 struct tog_log_view_state *s = &view->state.log;
1942 if (s->thread == NULL) {
1943 int errcode = pthread_create(&s->thread, NULL, log_thread,
1946 return got_error_set_errno(errcode, "pthread_create");
1949 return draw_commits(view, &s->last_displayed_entry,
1950 &s->selected_entry, s->first_displayed_entry,
1951 &s->commits, s->selected, view->nlines, s->refs,
1952 s->in_repo_path, s->thread_args.commits_needed);
1955 static const struct got_error *
1956 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1957 struct tog_view **focus_view, struct tog_view *view, int ch)
1959 const struct got_error *err = NULL;
1960 struct tog_log_view_state *s = &view->state.log;
1961 char *parent_path, *in_repo_path = NULL;
1962 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1964 struct got_object_id *start_id;
1974 if (s->first_displayed_entry == NULL)
1976 if (s->selected > 0)
1979 scroll_up(view, &s->first_displayed_entry, 1,
1984 if (s->first_displayed_entry == NULL)
1986 if (TAILQ_FIRST(&s->commits.head) ==
1987 s->first_displayed_entry) {
1991 scroll_up(view, &s->first_displayed_entry,
1992 view->nlines, &s->commits);
1998 if (s->first_displayed_entry == NULL)
2000 if (s->selected < MIN(view->nlines - 2,
2001 s->commits.ncommits - 1)) {
2005 err = scroll_down(view, &s->first_displayed_entry, 1,
2006 &s->last_displayed_entry, &s->commits,
2007 &s->thread_args.log_complete,
2008 &s->thread_args.commits_needed,
2009 &s->thread_args.need_commits);
2013 struct commit_queue_entry *first;
2014 first = s->first_displayed_entry;
2017 err = scroll_down(view, &s->first_displayed_entry,
2018 view->nlines, &s->last_displayed_entry,
2019 &s->commits, &s->thread_args.log_complete,
2020 &s->thread_args.commits_needed,
2021 &s->thread_args.need_commits);
2022 if (first == s->first_displayed_entry &&
2023 s->selected < MIN(view->nlines - 2,
2024 s->commits.ncommits - 1)) {
2025 /* can't scroll further down */
2026 s->selected = MIN(view->nlines - 2,
2027 s->commits.ncommits - 1);
2033 if (s->selected > view->nlines - 2)
2034 s->selected = view->nlines - 2;
2035 if (s->selected > s->commits.ncommits - 1)
2036 s->selected = s->commits.ncommits - 1;
2041 if (s->selected_entry == NULL)
2043 if (view_is_parent_view(view))
2044 begin_x = view_split_begin_x(view->begin_x);
2045 err = open_diff_view_for_commit(&diff_view, begin_x,
2046 s->selected_entry->commit, s->selected_entry->id,
2047 view, s->refs, s->repo);
2050 if (view_is_parent_view(view)) {
2051 err = view_close_child(view);
2054 err = view_set_child(view, diff_view);
2056 view_close(diff_view);
2059 *focus_view = diff_view;
2060 view->child_focussed = 1;
2062 *new_view = diff_view;
2065 if (s->selected_entry == NULL)
2067 if (view_is_parent_view(view))
2068 begin_x = view_split_begin_x(view->begin_x);
2069 err = browse_commit_tree(&tree_view, begin_x,
2070 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2073 if (view_is_parent_view(view)) {
2074 err = view_close_child(view);
2077 err = view_set_child(view, tree_view);
2079 view_close(tree_view);
2082 *focus_view = tree_view;
2083 view->child_focussed = 1;
2085 *new_view = tree_view;
2088 if (strcmp(s->in_repo_path, "/") == 0)
2090 parent_path = dirname(s->in_repo_path);
2091 if (parent_path && strcmp(parent_path, ".") != 0) {
2092 err = stop_log_thread(s);
2095 lv = view_open(view->nlines, view->ncols,
2096 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2098 return got_error_from_errno(
2100 err = open_log_view(lv, s->start_id, s->refs,
2101 s->repo, s->head_ref_name, parent_path, 0);
2104 if (view_is_parent_view(view))
2107 view_set_child(view->parent, lv);
2114 err = stop_log_thread(s);
2117 lv = view_open(view->nlines, view->ncols,
2118 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2120 return got_error_from_errno("view_open");
2121 err = get_head_commit_id(&start_id, s->head_ref_name ?
2122 s->head_ref_name : GOT_REF_HEAD, s->repo);
2127 in_repo_path = strdup(s->in_repo_path);
2128 if (in_repo_path == NULL) {
2131 return got_error_from_errno("strdup");
2133 err = open_log_view(lv, start_id, s->refs, s->repo,
2134 s->head_ref_name, in_repo_path, 0);
2150 static const struct got_error *
2151 apply_unveil(const char *repo_path, const char *worktree_path)
2153 const struct got_error *error;
2156 if (unveil("gmon.out", "rwc") != 0)
2157 return got_error_from_errno2("unveil", "gmon.out");
2159 if (repo_path && unveil(repo_path, "r") != 0)
2160 return got_error_from_errno2("unveil", repo_path);
2162 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2163 return got_error_from_errno2("unveil", worktree_path);
2165 if (unveil("/tmp", "rwc") != 0)
2166 return got_error_from_errno2("unveil", "/tmp");
2168 error = got_privsep_unveil_exec_helpers();
2172 if (unveil(NULL, NULL) != 0)
2173 return got_error_from_errno("unveil");
2183 halfdelay(1); /* Do fast refresh while initial view is loading. */
2186 intrflush(stdscr, FALSE);
2187 keypad(stdscr, TRUE);
2189 signal(SIGWINCH, tog_sigwinch);
2190 signal(SIGPIPE, tog_sigpipe);
2193 static const struct got_error *
2194 cmd_log(int argc, char *argv[])
2196 const struct got_error *error;
2197 struct got_repository *repo = NULL;
2198 struct got_worktree *worktree = NULL;
2199 struct got_reflist_head refs;
2200 struct got_object_id *start_id = NULL;
2201 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2202 char *start_commit = NULL;
2204 struct tog_view *view;
2206 SIMPLEQ_INIT(&refs);
2209 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2214 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2217 start_commit = optarg;
2220 repo_path = realpath(optarg, NULL);
2221 if (repo_path == NULL)
2222 err(1, "-r option");
2233 cwd = getcwd(NULL, 0);
2235 error = got_error_from_errno("getcwd");
2238 error = got_worktree_open(&worktree, cwd);
2239 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2246 error = got_error_from_errno("strdup");
2249 } else if (argc == 1) {
2251 error = got_worktree_resolve_path(&path, worktree,
2256 path = strdup(argv[0]);
2258 error = got_error_from_errno("strdup");
2265 if (repo_path == NULL) {
2268 got_worktree_get_repo_path(worktree));
2270 repo_path = strdup(cwd);
2272 if (repo_path == NULL) {
2273 error = got_error_from_errno("strdup");
2279 error = got_repo_open(&repo, repo_path);
2283 error = apply_unveil(got_repo_get_path(repo),
2284 worktree ? got_worktree_get_root_path(worktree) : NULL);
2288 if (start_commit == NULL)
2289 error = get_head_commit_id(&start_id, worktree ?
2290 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2293 error = get_head_commit_id(&start_id, start_commit, repo);
2295 if (error->code != GOT_ERR_NOT_REF)
2297 error = got_repo_match_object_id_prefix(&start_id,
2298 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2304 error = got_ref_list(&refs, repo);
2308 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2310 error = got_error_from_errno("view_open");
2313 error = open_log_view(view, start_id, &refs, repo, worktree ?
2314 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2317 error = view_loop(view);
2324 got_repo_close(repo);
2326 got_worktree_close(worktree);
2327 got_ref_list_free(&refs);
2335 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2341 parse_next_line(FILE *f, size_t *len)
2346 const char delim[3] = { '\0', '\0', '\0'};
2348 line = fparseln(f, &linelen, &lineno, delim, 0);
2354 static const struct got_error *
2355 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2356 int *last_displayed_line, int *eof, int max_lines,
2359 const struct got_error *err;
2360 int nlines = 0, nprinted = 0;
2367 werase(view->window);
2370 err = format_line(&wline, &width, header, view->ncols);
2375 if (view_needs_focus_indication(view))
2376 wstandout(view->window);
2377 waddwstr(view->window, wline);
2378 if (view_needs_focus_indication(view))
2379 wstandend(view->window);
2380 if (width < view->ncols - 1)
2381 waddch(view->window, '\n');
2389 while (nprinted < max_lines) {
2390 line = parse_next_line(f, &len);
2395 if (++nlines < *first_displayed_line) {
2400 err = format_line(&wline, &width, line, view->ncols);
2405 waddwstr(view->window, wline);
2406 if (width < view->ncols - 1)
2407 waddch(view->window, '\n');
2408 if (++nprinted == 1)
2409 *first_displayed_line = nlines;
2414 *last_displayed_line = nlines;
2419 while (nprinted < view->nlines) {
2420 waddch(view->window, '\n');
2424 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2429 wstandout(view->window);
2430 waddwstr(view->window, wline);
2431 wstandend(view->window);
2438 get_datestr(time_t *time, char *datebuf)
2440 struct tm mytm, *tm;
2443 tm = gmtime_r(time, &mytm);
2446 s = asctime_r(tm, datebuf);
2449 p = strchr(s, '\n');
2455 static const struct got_error *
2456 write_commit_info(struct got_object_id *commit_id,
2457 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2459 const struct got_error *err = NULL;
2460 char datebuf[26], *datestr;
2461 struct got_commit_object *commit;
2462 char *id_str = NULL, *logmsg = NULL;
2463 time_t committer_time;
2464 const char *author, *committer;
2465 char *refs_str = NULL;
2468 err = build_refs_str(&refs_str, refs, commit_id, repo);
2473 err = got_object_open_as_commit(&commit, repo, commit_id);
2477 err = got_object_id_str(&id_str, commit_id);
2479 err = got_error_from_errno("got_object_id_str");
2483 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2484 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2485 err = got_error_from_errno("fprintf");
2488 if (fprintf(outfile, "from: %s\n",
2489 got_object_commit_get_author(commit)) < 0) {
2490 err = got_error_from_errno("fprintf");
2493 committer_time = got_object_commit_get_committer_time(commit);
2494 datestr = get_datestr(&committer_time, datebuf);
2495 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2496 err = got_error_from_errno("fprintf");
2499 author = got_object_commit_get_author(commit);
2500 committer = got_object_commit_get_committer(commit);
2501 if (strcmp(author, committer) != 0 &&
2502 fprintf(outfile, "via: %s\n", committer) < 0) {
2503 err = got_error_from_errno("fprintf");
2506 err = got_object_commit_get_logmsg(&logmsg, commit);
2509 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2510 err = got_error_from_errno("fprintf");
2517 got_object_commit_close(commit);
2521 static const struct got_error *
2522 create_diff(struct tog_diff_view_state *s)
2524 const struct got_error *err = NULL;
2530 err = got_error_from_errno("got_opentemp");
2533 if (s->f && fclose(s->f) != 0) {
2534 err = got_error_from_errno("fclose");
2540 err = got_object_get_type(&obj_type, s->repo, s->id1);
2542 err = got_object_get_type(&obj_type, s->repo, s->id2);
2547 case GOT_OBJ_TYPE_BLOB:
2548 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2549 s->diff_context, s->repo, f);
2551 case GOT_OBJ_TYPE_TREE:
2552 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2553 s->diff_context, s->repo, f);
2555 case GOT_OBJ_TYPE_COMMIT: {
2556 const struct got_object_id_queue *parent_ids;
2557 struct got_object_qid *pid;
2558 struct got_commit_object *commit2;
2560 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2563 /* Show commit info if we're diffing to a parent/root commit. */
2565 write_commit_info(s->id2, s->refs, s->repo, f);
2567 parent_ids = got_object_commit_get_parent_ids(commit2);
2568 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2569 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2570 write_commit_info(s->id2, s->refs,
2576 got_object_commit_close(commit2);
2578 err = got_diff_objects_as_commits(s->id1, s->id2,
2579 s->diff_context, s->repo, f);
2583 err = got_error(GOT_ERR_OBJ_TYPE);
2587 if (f && fflush(f) != 0 && err == NULL)
2588 err = got_error_from_errno("fflush");
2593 diff_view_indicate_progress(struct tog_view *view)
2595 mvwaddstr(view->window, 0, 0, "diffing...");
2600 static const struct got_error *
2601 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2602 struct got_object_id *id2, struct tog_view *log_view,
2603 struct got_reflist_head *refs, struct got_repository *repo)
2605 const struct got_error *err;
2607 if (id1 != NULL && id2 != NULL) {
2609 err = got_object_get_type(&type1, repo, id1);
2612 err = got_object_get_type(&type2, repo, id2);
2617 return got_error(GOT_ERR_OBJ_TYPE);
2621 view->state.diff.id1 = got_object_id_dup(id1);
2622 if (view->state.diff.id1 == NULL)
2623 return got_error_from_errno("got_object_id_dup");
2625 view->state.diff.id1 = NULL;
2627 view->state.diff.id2 = got_object_id_dup(id2);
2628 if (view->state.diff.id2 == NULL) {
2629 free(view->state.diff.id1);
2630 view->state.diff.id1 = NULL;
2631 return got_error_from_errno("got_object_id_dup");
2633 view->state.diff.f = NULL;
2634 view->state.diff.first_displayed_line = 1;
2635 view->state.diff.last_displayed_line = view->nlines;
2636 view->state.diff.diff_context = 3;
2637 view->state.diff.log_view = log_view;
2638 view->state.diff.repo = repo;
2639 view->state.diff.refs = refs;
2641 if (log_view && view_is_splitscreen(view))
2642 show_log_view(log_view); /* draw vborder */
2643 diff_view_indicate_progress(view);
2645 err = create_diff(&view->state.diff);
2647 free(view->state.diff.id1);
2648 view->state.diff.id1 = NULL;
2649 free(view->state.diff.id2);
2650 view->state.diff.id2 = NULL;
2654 view->show = show_diff_view;
2655 view->input = input_diff_view;
2656 view->close = close_diff_view;
2661 static const struct got_error *
2662 close_diff_view(struct tog_view *view)
2664 const struct got_error *err = NULL;
2666 free(view->state.diff.id1);
2667 view->state.diff.id1 = NULL;
2668 free(view->state.diff.id2);
2669 view->state.diff.id2 = NULL;
2670 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2671 err = got_error_from_errno("fclose");
2675 static const struct got_error *
2676 show_diff_view(struct tog_view *view)
2678 const struct got_error *err;
2679 struct tog_diff_view_state *s = &view->state.diff;
2680 char *id_str1 = NULL, *id_str2, *header;
2683 err = got_object_id_str(&id_str1, s->id1);
2687 err = got_object_id_str(&id_str2, s->id2);
2691 if (asprintf(&header, "diff %s %s",
2692 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2693 err = got_error_from_errno("asprintf");
2701 return draw_file(view, s->f, &s->first_displayed_line,
2702 &s->last_displayed_line, &s->eof, view->nlines,
2706 static const struct got_error *
2707 set_selected_commit(struct tog_diff_view_state *s,
2708 struct commit_queue_entry *entry)
2710 const struct got_error *err;
2711 const struct got_object_id_queue *parent_ids;
2712 struct got_commit_object *selected_commit;
2713 struct got_object_qid *pid;
2716 s->id2 = got_object_id_dup(entry->id);
2718 return got_error_from_errno("got_object_id_dup");
2720 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2723 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2725 pid = SIMPLEQ_FIRST(parent_ids);
2726 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2727 got_object_commit_close(selected_commit);
2731 static const struct got_error *
2732 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2733 struct tog_view **focus_view, struct tog_view *view, int ch)
2735 const struct got_error *err = NULL;
2736 struct tog_diff_view_state *s = &view->state.diff;
2737 struct tog_log_view_state *ls;
2738 struct commit_queue_entry *entry;
2744 if (s->first_displayed_line > 1)
2745 s->first_displayed_line--;
2749 if (s->first_displayed_line == 1)
2752 while (i++ < view->nlines - 1 &&
2753 s->first_displayed_line > 1)
2754 s->first_displayed_line--;
2759 s->first_displayed_line++;
2767 while (!s->eof && i++ < view->nlines - 1) {
2769 line = parse_next_line(s->f, NULL);
2770 s->first_displayed_line++;
2776 if (s->diff_context > 0) {
2778 diff_view_indicate_progress(view);
2779 err = create_diff(s);
2783 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2785 diff_view_indicate_progress(view);
2786 err = create_diff(s);
2791 if (s->log_view == NULL)
2793 ls = &s->log_view->state.log;
2794 entry = TAILQ_PREV(ls->selected_entry,
2795 commit_queue_head, entry);
2799 err = input_log_view(NULL, NULL, NULL, s->log_view,
2804 err = set_selected_commit(s, entry);
2808 s->first_displayed_line = 1;
2809 s->last_displayed_line = view->nlines;
2811 diff_view_indicate_progress(view);
2812 err = create_diff(s);
2816 if (s->log_view == NULL)
2818 ls = &s->log_view->state.log;
2820 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2821 ls->thread_args.commits_needed++;
2823 /* Display "loading..." in log view. */
2824 show_log_view(s->log_view);
2828 err = trigger_log_thread(1 /* load_all */,
2829 &ls->thread_args.commits_needed,
2830 &ls->thread_args.log_complete,
2831 &ls->thread_args.need_commits);
2835 err = input_log_view(NULL, NULL, NULL, s->log_view,
2840 entry = TAILQ_NEXT(ls->selected_entry, entry);
2844 err = set_selected_commit(s, entry);
2848 s->first_displayed_line = 1;
2849 s->last_displayed_line = view->nlines;
2851 diff_view_indicate_progress(view);
2852 err = create_diff(s);
2861 static const struct got_error *
2862 cmd_diff(int argc, char *argv[])
2864 const struct got_error *error = NULL;
2865 struct got_repository *repo = NULL;
2866 struct got_reflist_head refs;
2867 struct got_object_id *id1 = NULL, *id2 = NULL;
2868 char *repo_path = NULL;
2869 char *id_str1 = NULL, *id_str2 = NULL;
2871 struct tog_view *view;
2873 SIMPLEQ_INIT(&refs);
2876 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2881 while ((ch = getopt(argc, argv, "")) != -1) {
2893 usage_diff(); /* TODO show local worktree changes */
2894 } else if (argc == 2) {
2895 repo_path = getcwd(NULL, 0);
2896 if (repo_path == NULL)
2897 return got_error_from_errno("getcwd");
2900 } else if (argc == 3) {
2901 repo_path = realpath(argv[0], NULL);
2902 if (repo_path == NULL)
2903 return got_error_from_errno2("realpath", argv[0]);
2911 error = got_repo_open(&repo, repo_path);
2915 error = apply_unveil(got_repo_get_path(repo), NULL);
2919 error = got_repo_match_object_id_prefix(&id1, id_str1,
2920 GOT_OBJ_TYPE_ANY, repo);
2924 error = got_repo_match_object_id_prefix(&id2, id_str2,
2925 GOT_OBJ_TYPE_ANY, repo);
2929 error = got_ref_list(&refs, repo);
2933 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2935 error = got_error_from_errno("view_open");
2938 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2941 error = view_loop(view);
2945 got_repo_close(repo);
2946 got_ref_list_free(&refs);
2954 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2959 struct tog_blame_line {
2961 struct got_object_id *id;
2964 static const struct got_error *
2965 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2966 const char *path, struct tog_blame_line *lines, int nlines,
2967 int blame_complete, int selected_line, int *first_displayed_line,
2968 int *last_displayed_line, int *eof, int max_lines)
2970 const struct got_error *err;
2971 int lineno = 0, nprinted = 0;
2976 struct tog_blame_line *blame_line;
2977 struct got_object_id *prev_id = NULL;
2980 err = got_object_id_str(&id_str, id);
2985 werase(view->window);
2987 if (asprintf(&line, "commit %s", id_str) == -1) {
2988 err = got_error_from_errno("asprintf");
2993 err = format_line(&wline, &width, line, view->ncols);
2996 if (view_needs_focus_indication(view))
2997 wstandout(view->window);
2998 waddwstr(view->window, wline);
2999 if (view_needs_focus_indication(view))
3000 wstandend(view->window);
3003 if (width < view->ncols - 1)
3004 waddch(view->window, '\n');
3006 if (asprintf(&line, "[%d/%d] %s%s",
3007 *first_displayed_line - 1 + selected_line, nlines,
3008 blame_complete ? "" : "annotating... ", path) == -1) {
3010 return got_error_from_errno("asprintf");
3013 err = format_line(&wline, &width, line, view->ncols);
3018 waddwstr(view->window, wline);
3021 if (width < view->ncols - 1)
3022 waddch(view->window, '\n');
3025 while (nprinted < max_lines - 2) {
3026 line = parse_next_line(f, &len);
3031 if (++lineno < *first_displayed_line) {
3036 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3037 err = format_line(&wline, &width, line, wlimit);
3043 if (view->focussed && nprinted == selected_line - 1)
3044 wstandout(view->window);
3047 blame_line = &lines[lineno - 1];
3048 if (blame_line->annotated && prev_id &&
3049 got_object_id_cmp(prev_id, blame_line->id) == 0)
3050 waddstr(view->window, " ");
3051 else if (blame_line->annotated) {
3053 err = got_object_id_str(&id_str, blame_line->id);
3059 wprintw(view->window, "%.8s ", id_str);
3061 prev_id = blame_line->id;
3063 waddstr(view->window, "........ ");
3067 waddstr(view->window, "........ ");
3071 waddwstr(view->window, wline);
3072 while (width < wlimit) {
3073 waddch(view->window, ' ');
3076 if (view->focussed && nprinted == selected_line - 1)
3077 wstandend(view->window);
3078 if (++nprinted == 1)
3079 *first_displayed_line = lineno;
3084 *last_displayed_line = lineno;
3091 static const struct got_error *
3092 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3094 const struct got_error *err = NULL;
3095 struct tog_blame_cb_args *a = arg;
3096 struct tog_blame_line *line;
3099 if (nlines != a->nlines ||
3100 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3101 return got_error(GOT_ERR_RANGE);
3103 errcode = pthread_mutex_lock(&tog_mutex);
3105 return got_error_set_errno(errcode, "pthread_mutex_lock");
3107 if (*a->quit) { /* user has quit the blame view */
3108 err = got_error(GOT_ERR_ITER_COMPLETED);
3113 goto done; /* no change in this commit */
3115 line = &a->lines[lineno - 1];
3116 if (line->annotated)
3119 line->id = got_object_id_dup(id);
3120 if (line->id == NULL) {
3121 err = got_error_from_errno("got_object_id_dup");
3124 line->annotated = 1;
3126 errcode = pthread_mutex_unlock(&tog_mutex);
3128 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3133 blame_thread(void *arg)
3135 const struct got_error *err;
3136 struct tog_blame_thread_args *ta = arg;
3137 struct tog_blame_cb_args *a = ta->cb_args;
3140 err = got_blame(ta->path, a->commit_id, ta->repo,
3141 blame_cb, ta->cb_args, NULL, NULL);
3143 errcode = pthread_mutex_lock(&tog_mutex);
3145 return (void *)got_error_set_errno(errcode,
3146 "pthread_mutex_lock");
3148 got_repo_close(ta->repo);
3152 errcode = pthread_mutex_unlock(&tog_mutex);
3153 if (errcode && err == NULL)
3154 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3159 static struct got_object_id *
3160 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3161 int first_displayed_line, int selected_line)
3163 struct tog_blame_line *line;
3168 line = &lines[first_displayed_line - 1 + selected_line - 1];
3169 if (!line->annotated)
3175 static const struct got_error *
3176 stop_blame(struct tog_blame *blame)
3178 const struct got_error *err = NULL;
3181 if (blame->thread) {
3183 errcode = pthread_mutex_unlock(&tog_mutex);
3185 return got_error_set_errno(errcode,
3186 "pthread_mutex_unlock");
3187 errcode = pthread_join(blame->thread, (void **)&err);
3189 return got_error_set_errno(errcode, "pthread_join");
3190 errcode = pthread_mutex_lock(&tog_mutex);
3192 return got_error_set_errno(errcode,
3193 "pthread_mutex_lock");
3194 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3196 blame->thread = NULL;
3198 if (blame->thread_args.repo) {
3199 got_repo_close(blame->thread_args.repo);
3200 blame->thread_args.repo = NULL;
3203 if (fclose(blame->f) != 0 && err == NULL)
3204 err = got_error_from_errno("fclose");
3208 for (i = 0; i < blame->nlines; i++)
3209 free(blame->lines[i].id);
3211 blame->lines = NULL;
3213 free(blame->cb_args.commit_id);
3214 blame->cb_args.commit_id = NULL;
3219 static const struct got_error *
3220 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3221 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3222 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3223 struct got_repository *repo)
3225 const struct got_error *err = NULL;
3226 struct got_blob_object *blob = NULL;
3227 struct got_repository *thread_repo = NULL;
3228 struct got_object_id *obj_id = NULL;
3231 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3235 return got_error(GOT_ERR_NO_OBJ);
3237 err = got_object_get_type(&obj_type, repo, obj_id);
3241 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3242 err = got_error(GOT_ERR_OBJ_TYPE);
3246 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3249 blame->f = got_opentemp();
3250 if (blame->f == NULL) {
3251 err = got_error_from_errno("got_opentemp");
3254 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3255 &blame->line_offsets, blame->f, blob);
3256 if (err || blame->nlines == 0)
3259 /* Don't include \n at EOF in the blame line count. */
3260 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3263 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3264 if (blame->lines == NULL) {
3265 err = got_error_from_errno("calloc");
3269 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3273 blame->cb_args.view = view;
3274 blame->cb_args.lines = blame->lines;
3275 blame->cb_args.nlines = blame->nlines;
3276 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3277 if (blame->cb_args.commit_id == NULL) {
3278 err = got_error_from_errno("got_object_id_dup");
3281 blame->cb_args.quit = done;
3283 blame->thread_args.path = path;
3284 blame->thread_args.repo = thread_repo;
3285 blame->thread_args.cb_args = &blame->cb_args;
3286 blame->thread_args.complete = blame_complete;
3287 *blame_complete = 0;
3291 got_object_blob_close(blob);
3298 static const struct got_error *
3299 open_blame_view(struct tog_view *view, char *path,
3300 struct got_object_id *commit_id, struct got_reflist_head *refs,
3301 struct got_repository *repo)
3303 const struct got_error *err = NULL;
3304 struct tog_blame_view_state *s = &view->state.blame;
3306 SIMPLEQ_INIT(&s->blamed_commits);
3308 s->path = strdup(path);
3309 if (s->path == NULL)
3310 return got_error_from_errno("strdup");
3312 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3318 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3319 s->first_displayed_line = 1;
3320 s->last_displayed_line = view->nlines;
3321 s->selected_line = 1;
3322 s->blame_complete = 0;
3325 s->commit_id = commit_id;
3326 memset(&s->blame, 0, sizeof(s->blame));
3328 view->show = show_blame_view;
3329 view->input = input_blame_view;
3330 view->close = close_blame_view;
3331 view->search_start = search_start_blame_view;
3332 view->search_next = search_next_blame_view;
3334 return run_blame(&s->blame, view, &s->blame_complete,
3335 &s->first_displayed_line, &s->last_displayed_line,
3336 &s->selected_line, &s->done, &s->eof, s->path,
3337 s->blamed_commit->id, s->repo);
3340 static const struct got_error *
3341 close_blame_view(struct tog_view *view)
3343 const struct got_error *err = NULL;
3344 struct tog_blame_view_state *s = &view->state.blame;
3346 if (s->blame.thread)
3347 err = stop_blame(&s->blame);
3349 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3350 struct got_object_qid *blamed_commit;
3351 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3352 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3353 got_object_qid_free(blamed_commit);
3361 static const struct got_error *
3362 search_start_blame_view(struct tog_view *view)
3364 struct tog_blame_view_state *s = &view->state.blame;
3366 s->matched_line = 0;
3371 match_line(const char *line, regex_t *regex)
3373 regmatch_t regmatch;
3375 return regexec(regex, line, 1, ®match, 0) == 0;
3379 static const struct got_error *
3380 search_next_blame_view(struct tog_view *view)
3382 struct tog_blame_view_state *s = &view->state.blame;
3385 if (!view->searching) {
3386 view->search_next_done = 1;
3390 if (s->matched_line) {
3391 if (view->searching == TOG_SEARCH_FORWARD)
3392 lineno = s->matched_line + 1;
3394 lineno = s->matched_line - 1;
3396 if (view->searching == TOG_SEARCH_FORWARD)
3399 lineno = s->blame.nlines;
3407 if (lineno <= 0 || lineno > s->blame.nlines) {
3408 if (s->matched_line == 0) {
3409 view->search_next_done = 1;
3414 if (view->searching == TOG_SEARCH_FORWARD)
3417 lineno = s->blame.nlines;
3420 offset = s->blame.line_offsets[lineno - 1];
3421 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3423 return got_error_from_errno("fseeko");
3426 line = parse_next_line(s->blame.f, &len);
3427 if (line && match_line(line, &view->regex)) {
3428 view->search_next_done = 1;
3429 s->matched_line = lineno;
3434 if (view->searching == TOG_SEARCH_FORWARD)
3440 if (s->matched_line) {
3441 s->first_displayed_line = s->matched_line;
3442 s->selected_line = 1;
3448 static const struct got_error *
3449 show_blame_view(struct tog_view *view)
3451 const struct got_error *err = NULL;
3452 struct tog_blame_view_state *s = &view->state.blame;
3455 if (s->blame.thread == NULL) {
3456 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3457 &s->blame.thread_args);
3459 return got_error_set_errno(errcode, "pthread_create");
3461 halfdelay(1); /* fast refresh while annotating */
3464 if (s->blame_complete)
3465 halfdelay(10); /* disable fast refresh */
3467 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3468 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3469 s->selected_line, &s->first_displayed_line,
3470 &s->last_displayed_line, &s->eof, view->nlines);
3476 static const struct got_error *
3477 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3478 struct tog_view **focus_view, struct tog_view *view, int ch)
3480 const struct got_error *err = NULL, *thread_err = NULL;
3481 struct tog_view *diff_view;
3482 struct tog_blame_view_state *s = &view->state.blame;
3491 if (s->selected_line > 1)
3493 else if (s->selected_line == 1 &&
3494 s->first_displayed_line > 1)
3495 s->first_displayed_line--;
3498 if (s->first_displayed_line == 1) {
3499 s->selected_line = 1;
3502 if (s->first_displayed_line > view->nlines - 2)
3503 s->first_displayed_line -=
3506 s->first_displayed_line = 1;
3510 if (s->selected_line < view->nlines - 2 &&
3511 s->first_displayed_line +
3512 s->selected_line <= s->blame.nlines)
3514 else if (s->last_displayed_line <
3516 s->first_displayed_line++;
3520 struct got_object_id *id = NULL;
3521 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3522 s->first_displayed_line, s->selected_line);
3526 struct got_commit_object *commit;
3527 struct got_object_qid *pid;
3528 struct got_object_id *blob_id = NULL;
3530 err = got_object_open_as_commit(&commit,
3534 pid = SIMPLEQ_FIRST(
3535 got_object_commit_get_parent_ids(commit));
3537 got_object_commit_close(commit);
3540 /* Check if path history ends here. */
3541 err = got_object_id_by_path(&blob_id, s->repo,
3544 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3546 got_object_commit_close(commit);
3549 err = got_object_get_type(&obj_type, s->repo,
3552 /* Can't blame non-blob type objects. */
3553 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3554 got_object_commit_close(commit);
3557 err = got_object_qid_alloc(&s->blamed_commit,
3559 got_object_commit_close(commit);
3561 if (got_object_id_cmp(id,
3562 s->blamed_commit->id) == 0)
3564 err = got_object_qid_alloc(&s->blamed_commit,
3570 thread_err = stop_blame(&s->blame);
3574 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3575 s->blamed_commit, entry);
3576 err = run_blame(&s->blame, view, &s->blame_complete,
3577 &s->first_displayed_line, &s->last_displayed_line,
3578 &s->selected_line, &s->done, &s->eof,
3579 s->path, s->blamed_commit->id, s->repo);
3585 struct got_object_qid *first;
3586 first = SIMPLEQ_FIRST(&s->blamed_commits);
3587 if (!got_object_id_cmp(first->id, s->commit_id))
3590 thread_err = stop_blame(&s->blame);
3594 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3595 got_object_qid_free(s->blamed_commit);
3597 SIMPLEQ_FIRST(&s->blamed_commits);
3598 err = run_blame(&s->blame, view, &s->blame_complete,
3599 &s->first_displayed_line, &s->last_displayed_line,
3600 &s->selected_line, &s->done, &s->eof, s->path,
3601 s->blamed_commit->id, s->repo);
3608 struct got_object_id *id = NULL;
3609 struct got_object_qid *pid;
3610 struct got_commit_object *commit = NULL;
3611 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3612 s->first_displayed_line, s->selected_line);
3615 err = got_object_open_as_commit(&commit, s->repo, id);
3618 pid = SIMPLEQ_FIRST(
3619 got_object_commit_get_parent_ids(commit));
3620 if (view_is_parent_view(view))
3621 begin_x = view_split_begin_x(view->begin_x);
3622 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3623 if (diff_view == NULL) {
3624 got_object_commit_close(commit);
3625 err = got_error_from_errno("view_open");
3628 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3629 id, NULL, s->refs, s->repo);
3630 got_object_commit_close(commit);
3632 view_close(diff_view);
3635 if (view_is_parent_view(view)) {
3636 err = view_close_child(view);
3639 err = view_set_child(view, diff_view);
3641 view_close(diff_view);
3644 *focus_view = diff_view;
3645 view->child_focussed = 1;
3647 *new_view = diff_view;
3654 if (s->last_displayed_line >= s->blame.nlines &&
3655 s->selected_line >= MIN(s->blame.nlines,
3656 view->nlines - 2)) {
3659 if (s->last_displayed_line >= s->blame.nlines &&
3660 s->selected_line < view->nlines - 2) {
3661 s->selected_line = MIN(s->blame.nlines,
3665 if (s->last_displayed_line + view->nlines - 2
3667 s->first_displayed_line +=
3670 s->first_displayed_line =
3675 if (s->selected_line > view->nlines - 2) {
3676 s->selected_line = MIN(s->blame.nlines,
3683 return thread_err ? thread_err : err;
3686 static const struct got_error *
3687 cmd_blame(int argc, char *argv[])
3689 const struct got_error *error;
3690 struct got_repository *repo = NULL;
3691 struct got_reflist_head refs;
3692 struct got_worktree *worktree = NULL;
3693 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3694 struct got_object_id *commit_id = NULL;
3695 char *commit_id_str = NULL;
3697 struct tog_view *view;
3699 SIMPLEQ_INIT(&refs);
3702 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3707 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3710 commit_id_str = optarg;
3713 repo_path = realpath(optarg, NULL);
3714 if (repo_path == NULL)
3715 err(1, "-r option");
3731 cwd = getcwd(NULL, 0);
3733 error = got_error_from_errno("getcwd");
3736 if (repo_path == NULL) {
3737 error = got_worktree_open(&worktree, cwd);
3738 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3744 strdup(got_worktree_get_repo_path(worktree));
3745 if (repo_path == NULL)
3746 error = got_error_from_errno("strdup");
3750 repo_path = strdup(cwd);
3751 if (repo_path == NULL) {
3752 error = got_error_from_errno("strdup");
3760 error = got_repo_open(&repo, repo_path);
3764 error = apply_unveil(got_repo_get_path(repo), NULL);
3769 const char *prefix = got_worktree_get_path_prefix(worktree);
3770 char *p, *worktree_subdir = cwd +
3771 strlen(got_worktree_get_root_path(worktree));
3772 if (asprintf(&p, "%s%s%s%s%s",
3773 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3774 worktree_subdir, worktree_subdir[0] ? "/" : "",
3776 error = got_error_from_errno("asprintf");
3779 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3782 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3787 if (commit_id_str == NULL) {
3788 struct got_reference *head_ref;
3789 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3792 error = got_ref_resolve(&commit_id, repo, head_ref);
3793 got_ref_close(head_ref);
3795 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3797 if (error->code != GOT_ERR_NOT_REF)
3799 error = got_repo_match_object_id_prefix(&commit_id,
3800 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3806 error = got_ref_list(&refs, repo);
3810 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3812 error = got_error_from_errno("view_open");
3815 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3818 error = view_loop(view);
3824 got_worktree_close(worktree);
3826 got_repo_close(repo);
3827 got_ref_list_free(&refs);
3831 static const struct got_error *
3832 draw_tree_entries(struct tog_view *view,
3833 struct got_tree_entry **first_displayed_entry,
3834 struct got_tree_entry **last_displayed_entry,
3835 struct got_tree_entry **selected_entry, int *ndisplayed,
3836 const char *label, int show_ids, const char *parent_path,
3837 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3839 const struct got_error *err = NULL;
3840 struct got_tree_entry *te;
3846 werase(view->window);
3851 err = format_line(&wline, &width, label, view->ncols);
3854 if (view_needs_focus_indication(view))
3855 wstandout(view->window);
3856 waddwstr(view->window, wline);
3857 if (view_needs_focus_indication(view))
3858 wstandend(view->window);
3861 if (width < view->ncols - 1)
3862 waddch(view->window, '\n');
3865 err = format_line(&wline, &width, parent_path, view->ncols);
3868 waddwstr(view->window, wline);
3871 if (width < view->ncols - 1)
3872 waddch(view->window, '\n');
3875 waddch(view->window, '\n');
3879 te = SIMPLEQ_FIRST(&entries->head);
3880 if (*first_displayed_entry == NULL) {
3881 if (selected == 0) {
3883 wstandout(view->window);
3884 *selected_entry = NULL;
3886 waddstr(view->window, " ..\n"); /* parent directory */
3887 if (selected == 0 && view->focussed)
3888 wstandend(view->window);
3895 while (te != *first_displayed_entry)
3896 te = SIMPLEQ_NEXT(te, entry);
3900 char *line = NULL, *id_str = NULL;
3901 const char *modestr = "";
3904 err = got_object_id_str(&id_str, te->id);
3906 return got_error_from_errno(
3907 "got_object_id_str");
3909 if (S_ISLNK(te->mode))
3911 else if (S_ISDIR(te->mode))
3913 else if (te->mode & S_IXUSR)
3915 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3916 te->name, modestr) == -1) {
3918 return got_error_from_errno("asprintf");
3921 err = format_line(&wline, &width, line, view->ncols);
3926 if (n == selected) {
3928 wstandout(view->window);
3929 *selected_entry = te;
3931 waddwstr(view->window, wline);
3932 if (width < view->ncols - 1)
3933 waddch(view->window, '\n');
3934 if (n == selected && view->focussed)
3935 wstandend(view->window);
3941 *last_displayed_entry = te;
3944 te = SIMPLEQ_NEXT(te, entry);
3951 tree_scroll_up(struct tog_view *view,
3952 struct got_tree_entry **first_displayed_entry, int maxscroll,
3953 const struct got_tree_entries *entries, int isroot)
3955 struct got_tree_entry *te, *prev;
3958 if (*first_displayed_entry == NULL)
3961 te = SIMPLEQ_FIRST(&entries->head);
3962 if (*first_displayed_entry == te) {
3964 *first_displayed_entry = NULL;
3968 /* XXX this is stupid... switch to TAILQ? */
3969 for (i = 0; i < maxscroll; i++) {
3970 while (te != *first_displayed_entry) {
3972 te = SIMPLEQ_NEXT(te, entry);
3974 *first_displayed_entry = prev;
3975 te = SIMPLEQ_FIRST(&entries->head);
3977 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3978 *first_displayed_entry = NULL;
3982 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3983 struct got_tree_entry *last_displayed_entry,
3984 const struct got_tree_entries *entries)
3986 struct got_tree_entry *next, *last;
3989 if (*first_displayed_entry)
3990 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3992 next = SIMPLEQ_FIRST(&entries->head);
3993 last = last_displayed_entry;
3994 while (next && last && n++ < maxscroll) {
3995 last = SIMPLEQ_NEXT(last, entry);
3997 *first_displayed_entry = next;
3998 next = SIMPLEQ_NEXT(next, entry);
4004 static const struct got_error *
4005 tree_entry_path(char **path, struct tog_parent_trees *parents,
4006 struct got_tree_entry *te)
4008 const struct got_error *err = NULL;
4009 struct tog_parent_tree *pt;
4010 size_t len = 2; /* for leading slash and NUL */
4012 TAILQ_FOREACH(pt, parents, entry)
4013 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4015 len += strlen(te->name);
4017 *path = calloc(1, len);
4019 return got_error_from_errno("calloc");
4022 pt = TAILQ_LAST(parents, tog_parent_trees);
4024 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4025 err = got_error(GOT_ERR_NO_SPACE);
4028 if (strlcat(*path, "/", len) >= len) {
4029 err = got_error(GOT_ERR_NO_SPACE);
4032 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4035 if (strlcat(*path, te->name, len) >= len) {
4036 err = got_error(GOT_ERR_NO_SPACE);
4048 static const struct got_error *
4049 blame_tree_entry(struct tog_view **new_view, int begin_x,
4050 struct got_tree_entry *te, struct tog_parent_trees *parents,
4051 struct got_object_id *commit_id, struct got_reflist_head *refs,
4052 struct got_repository *repo)
4054 const struct got_error *err = NULL;
4056 struct tog_view *blame_view;
4060 err = tree_entry_path(&path, parents, te);
4064 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4065 if (blame_view == NULL) {
4066 err = got_error_from_errno("view_open");
4070 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4072 view_close(blame_view);
4074 *new_view = blame_view;
4080 static const struct got_error *
4081 log_tree_entry(struct tog_view **new_view, int begin_x,
4082 struct got_tree_entry *te, struct tog_parent_trees *parents,
4083 struct got_object_id *commit_id, struct got_reflist_head *refs,
4084 struct got_repository *repo)
4086 struct tog_view *log_view;
4087 const struct got_error *err = NULL;
4092 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4093 if (log_view == NULL)
4094 return got_error_from_errno("view_open");
4096 err = tree_entry_path(&path, parents, te);
4100 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4102 view_close(log_view);
4104 *new_view = log_view;
4109 static const struct got_error *
4110 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4111 struct got_object_id *commit_id, struct got_reflist_head *refs,
4112 struct got_repository *repo)
4114 const struct got_error *err = NULL;
4115 char *commit_id_str = NULL;
4116 struct tog_tree_view_state *s = &view->state.tree;
4118 TAILQ_INIT(&s->parents);
4120 err = got_object_id_str(&commit_id_str, commit_id);
4124 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4125 err = got_error_from_errno("asprintf");
4129 s->root = s->tree = root;
4130 s->entries = got_object_tree_get_entries(root);
4131 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4132 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4133 s->commit_id = got_object_id_dup(commit_id);
4134 if (s->commit_id == NULL) {
4135 err = got_error_from_errno("got_object_id_dup");
4141 view->show = show_tree_view;
4142 view->input = input_tree_view;
4143 view->close = close_tree_view;
4144 view->search_start = search_start_tree_view;
4145 view->search_next = search_next_tree_view;
4147 free(commit_id_str);
4149 free(s->tree_label);
4150 s->tree_label = NULL;
4155 static const struct got_error *
4156 close_tree_view(struct tog_view *view)
4158 struct tog_tree_view_state *s = &view->state.tree;
4160 free(s->tree_label);
4161 s->tree_label = NULL;
4163 s->commit_id = NULL;
4164 while (!TAILQ_EMPTY(&s->parents)) {
4165 struct tog_parent_tree *parent;
4166 parent = TAILQ_FIRST(&s->parents);
4167 TAILQ_REMOVE(&s->parents, parent, entry);
4171 if (s->tree != s->root)
4172 got_object_tree_close(s->tree);
4173 got_object_tree_close(s->root);
4178 static const struct got_error *
4179 search_start_tree_view(struct tog_view *view)
4181 struct tog_tree_view_state *s = &view->state.tree;
4183 s->matched_entry = NULL;
4188 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4190 regmatch_t regmatch;
4192 return regexec(regex, te->name, 1, ®match, 0) == 0;
4195 static const struct got_error *
4196 search_next_tree_view(struct tog_view *view)
4198 struct tog_tree_view_state *s = &view->state.tree;
4199 struct got_tree_entry *entry = NULL, *te;
4201 if (!view->searching) {
4202 view->search_next_done = 1;
4206 if (s->matched_entry) {
4207 if (view->searching == TOG_SEARCH_FORWARD) {
4208 if (s->selected_entry)
4209 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4211 entry = SIMPLEQ_FIRST(&s->entries->head);
4214 if (s->selected_entry == NULL) {
4215 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4218 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4220 if (SIMPLEQ_NEXT(te, entry) ==
4227 if (view->searching == TOG_SEARCH_FORWARD)
4228 entry = SIMPLEQ_FIRST(&s->entries->head);
4230 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4236 if (entry == NULL) {
4237 if (s->matched_entry == NULL) {
4238 view->search_next_done = 1;
4241 if (view->searching == TOG_SEARCH_FORWARD)
4242 entry = SIMPLEQ_FIRST(&s->entries->head);
4244 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4249 if (match_tree_entry(entry, &view->regex)) {
4250 view->search_next_done = 1;
4251 s->matched_entry = entry;
4255 if (view->searching == TOG_SEARCH_FORWARD)
4256 entry = SIMPLEQ_NEXT(entry, entry);
4258 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4261 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4262 if (SIMPLEQ_NEXT(te, entry) == entry) {
4271 if (s->matched_entry) {
4272 s->first_displayed_entry = s->matched_entry;
4279 static const struct got_error *
4280 show_tree_view(struct tog_view *view)
4282 const struct got_error *err = NULL;
4283 struct tog_tree_view_state *s = &view->state.tree;
4286 err = tree_entry_path(&parent_path, &s->parents, NULL);
4290 err = draw_tree_entries(view, &s->first_displayed_entry,
4291 &s->last_displayed_entry, &s->selected_entry,
4292 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4293 s->entries, s->selected, view->nlines, s->tree == s->root);
4300 static const struct got_error *
4301 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4302 struct tog_view **focus_view, struct tog_view *view, int ch)
4304 const struct got_error *err = NULL;
4305 struct tog_tree_view_state *s = &view->state.tree;
4306 struct tog_view *log_view;
4307 int begin_x = 0, nscrolled;
4311 s->show_ids = !s->show_ids;
4314 if (!s->selected_entry)
4316 if (view_is_parent_view(view))
4317 begin_x = view_split_begin_x(view->begin_x);
4318 err = log_tree_entry(&log_view, begin_x,
4319 s->selected_entry, &s->parents,
4320 s->commit_id, s->refs, s->repo);
4321 if (view_is_parent_view(view)) {
4322 err = view_close_child(view);
4325 err = view_set_child(view, log_view);
4327 view_close(log_view);
4330 *focus_view = log_view;
4331 view->child_focussed = 1;
4333 *new_view = log_view;
4337 if (s->selected > 0) {
4339 if (s->selected == 0)
4342 if (s->selected > 0)
4344 tree_scroll_up(view, &s->first_displayed_entry, 1,
4345 s->entries, s->tree == s->root);
4348 tree_scroll_up(view, &s->first_displayed_entry,
4349 MAX(0, view->nlines - 4 - s->selected), s->entries,
4350 s->tree == s->root);
4352 if (SIMPLEQ_FIRST(&s->entries->head) ==
4353 s->first_displayed_entry && s->tree != s->root)
4354 s->first_displayed_entry = NULL;
4358 if (s->selected < s->ndisplayed - 1) {
4362 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4363 /* can't scroll any further */
4365 tree_scroll_down(&s->first_displayed_entry, 1,
4366 s->last_displayed_entry, s->entries);
4369 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4371 /* can't scroll any further; move cursor down */
4372 if (s->selected < s->ndisplayed - 1)
4373 s->selected = s->ndisplayed - 1;
4376 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4377 view->nlines, s->last_displayed_entry, s->entries);
4378 if (nscrolled < view->nlines) {
4380 struct got_tree_entry *te;
4381 te = s->first_displayed_entry;
4384 te = SIMPLEQ_NEXT(te, entry);
4386 s->selected = ndisplayed - 1;
4392 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4393 struct tog_parent_tree *parent;
4394 /* user selected '..' */
4395 if (s->tree == s->root)
4397 parent = TAILQ_FIRST(&s->parents);
4398 TAILQ_REMOVE(&s->parents, parent,
4400 got_object_tree_close(s->tree);
4401 s->tree = parent->tree;
4403 got_object_tree_get_entries(s->tree);
4404 s->first_displayed_entry =
4405 parent->first_displayed_entry;
4407 parent->selected_entry;
4408 s->selected = parent->selected;
4410 } else if (S_ISDIR(s->selected_entry->mode)) {
4411 struct got_tree_object *subtree;
4412 err = got_object_open_as_tree(&subtree,
4413 s->repo, s->selected_entry->id);
4416 err = tree_view_visit_subtree(subtree, s);
4418 got_object_tree_close(subtree);
4421 } else if (S_ISREG(s->selected_entry->mode)) {
4422 struct tog_view *blame_view;
4423 int begin_x = view_is_parent_view(view) ?
4424 view_split_begin_x(view->begin_x) : 0;
4426 err = blame_tree_entry(&blame_view, begin_x,
4427 s->selected_entry, &s->parents,
4428 s->commit_id, s->refs, s->repo);
4431 if (view_is_parent_view(view)) {
4432 err = view_close_child(view);
4435 err = view_set_child(view, blame_view);
4437 view_close(blame_view);
4440 *focus_view = blame_view;
4441 view->child_focussed = 1;
4443 *new_view = blame_view;
4447 if (s->selected > view->nlines)
4448 s->selected = s->ndisplayed - 1;
4461 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4466 static const struct got_error *
4467 cmd_tree(int argc, char *argv[])
4469 const struct got_error *error;
4470 struct got_repository *repo = NULL;
4471 struct got_reflist_head refs;
4472 char *repo_path = NULL;
4473 struct got_object_id *commit_id = NULL;
4474 char *commit_id_arg = NULL;
4475 struct got_commit_object *commit = NULL;
4476 struct got_tree_object *tree = NULL;
4478 struct tog_view *view;
4480 SIMPLEQ_INIT(&refs);
4483 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4488 while ((ch = getopt(argc, argv, "c:")) != -1) {
4491 commit_id_arg = optarg;
4503 struct got_worktree *worktree;
4504 char *cwd = getcwd(NULL, 0);
4506 return got_error_from_errno("getcwd");
4507 error = got_worktree_open(&worktree, cwd);
4508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4513 strdup(got_worktree_get_repo_path(worktree));
4514 got_worktree_close(worktree);
4517 if (repo_path == NULL) {
4518 error = got_error_from_errno("strdup");
4521 } else if (argc == 1) {
4522 repo_path = realpath(argv[0], NULL);
4523 if (repo_path == NULL)
4524 return got_error_from_errno2("realpath", argv[0]);
4530 error = got_repo_open(&repo, repo_path);
4534 error = apply_unveil(got_repo_get_path(repo), NULL);
4538 if (commit_id_arg == NULL)
4539 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4541 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4543 if (error->code != GOT_ERR_NOT_REF)
4545 error = got_repo_match_object_id_prefix(&commit_id,
4546 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4552 error = got_object_open_as_commit(&commit, repo, commit_id);
4556 error = got_object_open_as_tree(&tree, repo,
4557 got_object_commit_get_tree_id(commit));
4561 error = got_ref_list(&refs, repo);
4565 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4567 error = got_error_from_errno("view_open");
4570 error = open_tree_view(view, tree, commit_id, &refs, repo);
4573 error = view_loop(view);
4578 got_object_commit_close(commit);
4580 got_object_tree_close(tree);
4582 got_repo_close(repo);
4583 got_ref_list_free(&refs);
4592 fprintf(stderr, "commands:");
4593 for (i = 0; i < nitems(tog_commands); i++) {
4594 struct tog_cmd *cmd = &tog_commands[i];
4595 fprintf(stderr, " %s", cmd->name);
4597 fputc('\n', stderr);
4603 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4611 make_argv(const char *arg0, const char *arg1)
4614 int argc = (arg1 == NULL ? 1 : 2);
4616 argv = calloc(argc, sizeof(char *));
4619 argv[0] = strdup(arg0);
4620 if (argv[0] == NULL)
4623 argv[1] = strdup(arg1);
4624 if (argv[1] == NULL)
4632 main(int argc, char *argv[])
4634 const struct got_error *error = NULL;
4635 struct tog_cmd *cmd = NULL;
4636 int ch, hflag = 0, Vflag = 0;
4637 char **cmd_argv = NULL;
4639 setlocale(LC_CTYPE, "");
4641 while ((ch = getopt(argc, argv, "hV")) != -1) {
4661 got_version_print_str();
4668 /* Build an argument vector which runs a default command. */
4669 cmd = &tog_commands[0];
4670 cmd_argv = make_argv(cmd->name, NULL);
4675 /* Did the user specific a command? */
4676 for (i = 0; i < nitems(tog_commands); i++) {
4677 if (strncmp(tog_commands[i].name, argv[0],
4678 strlen(argv[0])) == 0) {
4679 cmd = &tog_commands[i];
4685 fprintf(stderr, "%s: unknown command '%s'\n",
4686 getprogname(), argv[0]);
4695 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4700 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);