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;
178 got_cancel_cb cancel_cb;
185 struct tog_blame_line *lines;
189 struct tog_blame_thread_args thread_args;
190 struct tog_blame_cb_args cb_args;
194 struct tog_blame_view_state {
195 int first_displayed_line;
196 int last_displayed_line;
201 struct got_object_id_queue blamed_commits;
202 struct got_object_qid *blamed_commit;
204 struct got_repository *repo;
205 struct got_reflist_head *refs;
206 struct got_object_id *commit_id;
207 struct tog_blame blame;
211 struct tog_parent_tree {
212 TAILQ_ENTRY(tog_parent_tree) entry;
213 struct got_tree_object *tree;
214 struct got_tree_entry *first_displayed_entry;
215 struct got_tree_entry *selected_entry;
219 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
221 struct tog_tree_view_state {
223 struct got_tree_object *root;
224 struct got_tree_object *tree;
225 const struct got_tree_entries *entries;
226 struct got_tree_entry *first_displayed_entry;
227 struct got_tree_entry *last_displayed_entry;
228 struct got_tree_entry *selected_entry;
229 int ndisplayed, selected, show_ids;
230 struct tog_parent_trees parents;
231 struct got_object_id *commit_id;
232 struct got_repository *repo;
233 struct got_reflist_head *refs;
234 struct got_tree_entry *matched_entry;
238 * We implement two types of views: parent views and child views.
240 * The 'Tab' key switches between a parent view and its child view.
241 * Child views are shown side-by-side to their parent view, provided
242 * there is enough screen estate.
244 * When a new view is opened from within a parent view, this new view
245 * becomes a child view of the parent view, replacing any existing child.
247 * When a new view is opened from within a child view, this new view
248 * becomes a parent view which will obscure the views below until the
249 * user quits the new parent view by typing 'q'.
251 * This list of views contains parent views only.
252 * Child views are only pointed to by their parent view.
254 TAILQ_HEAD(tog_view_list_head, tog_view);
257 TAILQ_ENTRY(tog_view) entry;
260 int nlines, ncols, begin_y, begin_x;
261 int lines, cols; /* copies of LINES and COLS */
263 struct tog_view *parent;
264 struct tog_view *child;
267 /* type-specific state */
268 enum tog_view_type type;
270 struct tog_diff_view_state diff;
271 struct tog_log_view_state log;
272 struct tog_blame_view_state blame;
273 struct tog_tree_view_state tree;
276 const struct got_error *(*show)(struct tog_view *);
277 const struct got_error *(*input)(struct tog_view **,
278 struct tog_view **, struct tog_view**, struct tog_view *, int);
279 const struct got_error *(*close)(struct tog_view *);
281 const struct got_error *(*search_start)(struct tog_view *);
282 const struct got_error *(*search_next)(struct tog_view *);
284 #define TOG_SEARCH_FORWARD 1
285 #define TOG_SEARCH_BACKWARD 2
286 int search_next_done;
290 static const struct got_error *open_diff_view(struct tog_view *,
291 struct got_object_id *, struct got_object_id *, struct tog_view *,
292 struct got_reflist_head *, struct got_repository *);
293 static const struct got_error *show_diff_view(struct tog_view *);
294 static const struct got_error *input_diff_view(struct tog_view **,
295 struct tog_view **, struct tog_view **, struct tog_view *, int);
296 static const struct got_error* close_diff_view(struct tog_view *);
298 static const struct got_error *open_log_view(struct tog_view *,
299 struct got_object_id *, struct got_reflist_head *,
300 struct got_repository *, const char *, const char *, int);
301 static const struct got_error * show_log_view(struct tog_view *);
302 static const struct got_error *input_log_view(struct tog_view **,
303 struct tog_view **, struct tog_view **, struct tog_view *, int);
304 static const struct got_error *close_log_view(struct tog_view *);
305 static const struct got_error *search_start_log_view(struct tog_view *);
306 static const struct got_error *search_next_log_view(struct tog_view *);
308 static const struct got_error *open_blame_view(struct tog_view *, char *,
309 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
310 static const struct got_error *show_blame_view(struct tog_view *);
311 static const struct got_error *input_blame_view(struct tog_view **,
312 struct tog_view **, struct tog_view **, struct tog_view *, int);
313 static const struct got_error *close_blame_view(struct tog_view *);
314 static const struct got_error *search_start_blame_view(struct tog_view *);
315 static const struct got_error *search_next_blame_view(struct tog_view *);
317 static const struct got_error *open_tree_view(struct tog_view *,
318 struct got_tree_object *, struct got_object_id *,
319 struct got_reflist_head *, struct got_repository *);
320 static const struct got_error *show_tree_view(struct tog_view *);
321 static const struct got_error *input_tree_view(struct tog_view **,
322 struct tog_view **, struct tog_view **, struct tog_view *, int);
323 static const struct got_error *close_tree_view(struct tog_view *);
324 static const struct got_error *search_start_tree_view(struct tog_view *);
325 static const struct got_error *search_next_tree_view(struct tog_view *);
327 static volatile sig_atomic_t tog_sigwinch_received;
328 static volatile sig_atomic_t tog_sigpipe_received;
331 tog_sigwinch(int signo)
333 tog_sigwinch_received = 1;
337 tog_sigpipe(int signo)
339 tog_sigpipe_received = 1;
342 static const struct got_error *
343 view_close(struct tog_view *view)
345 const struct got_error *err = NULL;
348 view_close(view->child);
352 err = view->close(view);
354 del_panel(view->panel);
356 delwin(view->window);
361 static struct tog_view *
362 view_open(int nlines, int ncols, int begin_y, int begin_x,
363 enum tog_view_type type)
365 struct tog_view *view = calloc(1, sizeof(*view));
373 view->nlines = nlines ? nlines : LINES - begin_y;
374 view->ncols = ncols ? ncols : COLS - begin_x;
375 view->begin_y = begin_y;
376 view->begin_x = begin_x;
377 view->window = newwin(nlines, ncols, begin_y, begin_x);
378 if (view->window == NULL) {
382 view->panel = new_panel(view->window);
383 if (view->panel == NULL ||
384 set_panel_userptr(view->panel, view) != OK) {
389 keypad(view->window, TRUE);
394 view_split_begin_x(int begin_x)
396 if (begin_x > 0 || COLS < 120)
398 return (COLS - MAX(COLS / 2, 80));
401 static const struct got_error *view_resize(struct tog_view *);
403 static const struct got_error *
404 view_splitscreen(struct tog_view *view)
406 const struct got_error *err = NULL;
409 view->begin_x = view_split_begin_x(0);
410 view->nlines = LINES;
411 view->ncols = COLS - view->begin_x;
414 err = view_resize(view);
418 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
419 return got_error_from_errno("mvwin");
424 static const struct got_error *
425 view_fullscreen(struct tog_view *view)
427 const struct got_error *err = NULL;
431 view->nlines = LINES;
435 err = view_resize(view);
439 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
440 return got_error_from_errno("mvwin");
446 view_is_parent_view(struct tog_view *view)
448 return view->parent == NULL;
451 static const struct got_error *
452 view_resize(struct tog_view *view)
456 if (view->lines > LINES)
457 nlines = view->nlines - (view->lines - LINES);
459 nlines = view->nlines + (LINES - view->lines);
461 if (view->cols > COLS)
462 ncols = view->ncols - (view->cols - COLS);
464 ncols = view->ncols + (COLS - view->cols);
466 if (wresize(view->window, nlines, ncols) == ERR)
467 return got_error_from_errno("wresize");
468 if (replace_panel(view->panel, view->window) == ERR)
469 return got_error_from_errno("replace_panel");
470 wclear(view->window);
472 view->nlines = nlines;
478 view->child->begin_x = view_split_begin_x(view->begin_x);
479 if (view->child->begin_x == 0) {
480 view_fullscreen(view->child);
481 if (view->child->focussed)
482 show_panel(view->child->panel);
484 show_panel(view->panel);
486 view_splitscreen(view->child);
487 show_panel(view->child->panel);
494 static const struct got_error *
495 view_close_child(struct tog_view *view)
497 const struct got_error *err = NULL;
499 if (view->child == NULL)
502 err = view_close(view->child);
507 static const struct got_error *
508 view_set_child(struct tog_view *view, struct tog_view *child)
510 const struct got_error *err = NULL;
513 child->parent = view;
518 view_is_splitscreen(struct tog_view *view)
520 return view->begin_x > 0;
529 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
530 cols = 80; /* Default */
536 resize_term(lines, cols);
539 static const struct got_error *
540 view_search_start(struct tog_view *view)
542 const struct got_error *err = NULL;
547 if (view->nlines < 1)
550 if (!view_is_parent_view(view))
551 begin_x = view_split_begin_x(view->begin_x);
552 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
554 wclrtoeol(view->window);
558 ret = wgetnstr(view->window, pattern, sizeof(pattern));
564 if (view->searching) {
565 regfree(&view->regex);
569 if (regcomp(&view->regex, pattern,
570 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
571 err = view->search_start(view);
573 regfree(&view->regex);
576 view->searching = TOG_SEARCH_FORWARD;
577 view->search_next_done = 0;
578 view->search_next(view);
584 static const struct got_error *
585 view_input(struct tog_view **new, struct tog_view **dead,
586 struct tog_view **focus, int *done, struct tog_view *view,
587 struct tog_view_list_head *views)
589 const struct got_error *err = NULL;
597 if (view->searching && !view->search_next_done) {
598 errcode = pthread_mutex_unlock(&tog_mutex);
600 return got_error_set_errno(errcode,
601 "pthread_mutex_unlock");
603 errcode = pthread_mutex_lock(&tog_mutex);
605 return got_error_set_errno(errcode,
606 "pthread_mutex_lock");
607 view->search_next(view);
611 nodelay(stdscr, FALSE);
612 /* Allow threads to make progress while we are waiting for input. */
613 errcode = pthread_mutex_unlock(&tog_mutex);
615 return got_error_set_errno(errcode, "pthread_mutex_unlock");
616 ch = wgetch(view->window);
617 errcode = pthread_mutex_lock(&tog_mutex);
619 return got_error_set_errno(errcode, "pthread_mutex_lock");
620 nodelay(stdscr, TRUE);
622 if (tog_sigwinch_received) {
624 tog_sigwinch_received = 0;
625 TAILQ_FOREACH(v, views, entry) {
626 err = view_resize(v);
629 err = v->input(new, dead, focus, v, KEY_RESIZE);
640 *focus = view->child;
641 view->child_focussed = 1;
642 } else if (view->parent) {
643 *focus = view->parent;
644 view->parent->child_focussed = 0;
648 err = view->input(new, dead, focus, view, ch);
655 if (view_is_parent_view(view)) {
656 if (view->child == NULL)
658 if (view_is_splitscreen(view->child)) {
659 *focus = view->child;
660 view->child_focussed = 1;
661 err = view_fullscreen(view->child);
663 err = view_splitscreen(view->child);
666 err = view->child->input(new, dead, focus,
667 view->child, KEY_RESIZE);
669 if (view_is_splitscreen(view)) {
671 view->parent->child_focussed = 1;
672 err = view_fullscreen(view);
674 err = view_splitscreen(view);
678 err = view->input(new, dead, focus, view,
685 if (view->search_start)
686 view_search_start(view);
688 err = view->input(new, dead, focus, view, ch);
692 if (view->search_next && view->searching) {
693 view->searching = (ch == 'n' ?
694 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
695 view->search_next_done = 0;
696 view->search_next(view);
698 err = view->input(new, dead, focus, view, ch);
701 err = view->input(new, dead, focus, view, ch);
709 view_vborder(struct tog_view *view)
712 struct tog_view *view_above;
715 return view_vborder(view->parent);
717 panel = panel_above(view->panel);
721 view_above = panel_userptr(panel);
722 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
723 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
727 view_needs_focus_indication(struct tog_view *view)
729 if (view_is_parent_view(view)) {
730 if (view->child == NULL || view->child_focussed)
732 if (!view_is_splitscreen(view->child))
734 } else if (!view_is_splitscreen(view))
737 return view->focussed;
740 static const struct got_error *
741 view_loop(struct tog_view *view)
743 const struct got_error *err = NULL;
744 struct tog_view_list_head views;
745 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
746 int fast_refresh = 10;
747 int done = 0, errcode;
749 errcode = pthread_mutex_lock(&tog_mutex);
751 return got_error_set_errno(errcode, "pthread_mutex_lock");
754 TAILQ_INSERT_HEAD(&views, view, entry);
758 err = view->show(view);
763 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
764 /* Refresh fast during initialization, then become slower. */
765 if (fast_refresh && fast_refresh-- == 0)
766 halfdelay(10); /* switch to once per second */
768 err = view_input(&new_view, &dead_view, &focus_view, &done,
773 struct tog_view *prev = NULL;
775 if (view_is_parent_view(dead_view))
776 prev = TAILQ_PREV(dead_view,
777 tog_view_list_head, entry);
778 else if (view->parent != dead_view)
781 if (dead_view->parent)
782 dead_view->parent->child = NULL;
784 TAILQ_REMOVE(&views, dead_view, entry);
786 err = view_close(dead_view);
787 if (err || (dead_view == main_view && new_view == NULL))
790 if (view == dead_view) {
795 else if (!TAILQ_EMPTY(&views))
796 view = TAILQ_LAST(&views,
801 if (view->child && view->child_focussed)
802 focus_view = view->child;
809 struct tog_view *v, *t;
810 /* Only allow one parent view per type. */
811 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
812 if (v->type != new_view->type)
814 TAILQ_REMOVE(&views, v, entry);
820 TAILQ_INSERT_TAIL(&views, new_view, entry);
822 if (focus_view == NULL)
823 focus_view = new_view;
826 show_panel(focus_view->panel);
829 focus_view->focussed = 1;
832 show_panel(new_view->panel);
833 if (view->child && view_is_splitscreen(view->child))
834 show_panel(view->child->panel);
837 if (focus_view == NULL) {
839 show_panel(view->panel);
840 if (view->child && view_is_splitscreen(view->child))
841 show_panel(view->child->panel);
845 err = view->parent->show(view->parent);
849 err = view->show(view);
853 err = view->child->show(view->child);
862 while (!TAILQ_EMPTY(&views)) {
863 view = TAILQ_FIRST(&views);
864 TAILQ_REMOVE(&views, view, entry);
868 errcode = pthread_mutex_unlock(&tog_mutex);
869 if (errcode && err == NULL)
870 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
880 "usage: %s log [-c commit] [-r repository-path] [path]\n",
885 /* Create newly allocated wide-character string equivalent to a byte string. */
886 static const struct got_error *
887 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
890 const struct got_error *err = NULL;
893 *wlen = mbstowcs(NULL, s, 0);
894 if (*wlen == (size_t)-1) {
897 return got_error_from_errno("mbstowcs");
899 /* byte string invalid in current encoding; try to "fix" it */
900 err = got_mbsavis(&vis, &vislen, s);
903 *wlen = mbstowcs(NULL, vis, 0);
904 if (*wlen == (size_t)-1) {
905 err = got_error_from_errno("mbstowcs"); /* give up */
910 *ws = calloc(*wlen + 1, sizeof(**ws));
912 err = got_error_from_errno("calloc");
916 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
917 err = got_error_from_errno("mbstowcs");
928 /* Format a line for display, ensuring that it won't overflow a width limit. */
929 static const struct got_error *
930 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
933 const struct got_error *err = NULL;
935 wchar_t *wline = NULL;
942 err = mbs2ws(&wline, &wlen, line);
948 int width = wcwidth(wline[i]);
955 if (width == 1 || width == 2) {
956 if (cols + width > wlimit)
960 } else if (width == -1) {
961 if (wline[i] == L'\t') {
963 ((cols + col_tab_align) % TABSIZE);
964 if (cols + width > wlimit)
970 err = got_error_from_errno("wcwidth");
985 static const struct got_error*
986 build_refs_str(char **refs_str, struct got_reflist_head *refs,
987 struct got_object_id *id, struct got_repository *repo)
989 static const struct got_error *err = NULL;
990 struct got_reflist_entry *re;
996 SIMPLEQ_FOREACH(re, refs, entry) {
997 struct got_tag_object *tag = NULL;
1000 name = got_ref_get_name(re->ref);
1001 if (strcmp(name, GOT_REF_HEAD) == 0)
1003 if (strncmp(name, "refs/", 5) == 0)
1005 if (strncmp(name, "got/", 4) == 0)
1007 if (strncmp(name, "heads/", 6) == 0)
1009 if (strncmp(name, "remotes/", 8) == 0)
1011 if (strncmp(name, "tags/", 5) == 0) {
1012 err = got_object_open_as_tag(&tag, repo, re->id);
1014 if (err->code != GOT_ERR_OBJ_TYPE)
1016 /* Ref points at something other than a tag. */
1021 cmp = got_object_id_cmp(tag ?
1022 got_object_tag_get_object_id(tag) : re->id, id);
1024 got_object_tag_close(tag);
1028 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1029 s ? ", " : "", name) == -1) {
1030 err = got_error_from_errno("asprintf");
1041 static const struct got_error *
1042 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1045 char *smallerthan, *at;
1047 smallerthan = strchr(author, '<');
1048 if (smallerthan && smallerthan[1] != '\0')
1049 author = smallerthan + 1;
1050 at = strchr(author, '@');
1053 return format_line(wauthor, author_width, author, limit, col_tab_align);
1056 static const struct got_error *
1057 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1058 struct got_object_id *id, struct got_reflist_head *refs,
1059 const size_t date_display_cols, int author_display_cols)
1061 const struct got_error *err = NULL;
1062 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1063 char *logmsg0 = NULL, *logmsg = NULL;
1064 char *author = NULL;
1065 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1066 int author_width, logmsg_width;
1067 char *newline, *line = NULL;
1069 const int avail = view->ncols;
1071 time_t committer_time;
1073 committer_time = got_object_commit_get_committer_time(commit);
1074 if (localtime_r(&committer_time, &tm) == NULL)
1075 return got_error_from_errno("localtime_r");
1076 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1078 return got_error(GOT_ERR_NO_SPACE);
1080 if (avail <= date_display_cols)
1081 limit = MIN(sizeof(datebuf) - 1, avail);
1083 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1084 waddnstr(view->window, datebuf, limit);
1089 author = strdup(got_object_commit_get_author(commit));
1090 if (author == NULL) {
1091 err = got_error_from_errno("strdup");
1094 err = format_author(&wauthor, &author_width, author, avail - col, col);
1097 waddwstr(view->window, wauthor);
1098 col += author_width;
1099 while (col < avail && author_width < author_display_cols + 2) {
1100 waddch(view->window, ' ');
1107 err = got_object_commit_get_logmsg(&logmsg0, commit);
1111 while (*logmsg == '\n')
1113 newline = strchr(logmsg, '\n');
1116 limit = avail - col;
1117 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1120 waddwstr(view->window, wlogmsg);
1121 col += logmsg_width;
1122 while (col < avail) {
1123 waddch(view->window, ' ');
1135 static struct commit_queue_entry *
1136 alloc_commit_queue_entry(struct got_commit_object *commit,
1137 struct got_object_id *id)
1139 struct commit_queue_entry *entry;
1141 entry = calloc(1, sizeof(*entry));
1146 entry->commit = commit;
1151 pop_commit(struct commit_queue *commits)
1153 struct commit_queue_entry *entry;
1155 entry = TAILQ_FIRST(&commits->head);
1156 TAILQ_REMOVE(&commits->head, entry, entry);
1157 got_object_commit_close(entry->commit);
1158 commits->ncommits--;
1159 /* Don't free entry->id! It is owned by the commit graph. */
1164 free_commits(struct commit_queue *commits)
1166 while (!TAILQ_EMPTY(&commits->head))
1167 pop_commit(commits);
1170 static const struct got_error *
1171 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1172 int minqueue, struct got_repository *repo, const char *path)
1174 const struct got_error *err = NULL;
1178 * We keep all commits open throughout the lifetime of the log
1179 * view in order to avoid having to re-fetch commits from disk
1180 * while updating the display.
1182 while (nqueued < minqueue) {
1183 struct got_object_id *id;
1184 struct got_commit_object *commit;
1185 struct commit_queue_entry *entry;
1188 err = got_commit_graph_iter_next(&id, graph);
1190 if (err->code != GOT_ERR_ITER_NEED_MORE)
1192 err = got_commit_graph_fetch_commits(graph,
1193 minqueue, repo, NULL, NULL);
1202 err = got_object_open_as_commit(&commit, repo, id);
1205 entry = alloc_commit_queue_entry(commit, id);
1206 if (entry == NULL) {
1207 err = got_error_from_errno("alloc_commit_queue_entry");
1211 errcode = pthread_mutex_lock(&tog_mutex);
1213 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1217 entry->idx = commits->ncommits;
1218 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1220 commits->ncommits++;
1222 errcode = pthread_mutex_unlock(&tog_mutex);
1223 if (errcode && err == NULL)
1224 err = got_error_set_errno(errcode,
1225 "pthread_mutex_unlock");
1231 static const struct got_error *
1232 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1233 struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 struct got_reference *head_ref;
1240 err = got_ref_open(&head_ref, repo, branch_name, 0);
1244 err = got_ref_resolve(head_id, repo, head_ref);
1245 got_ref_close(head_ref);
1254 static const struct got_error *
1255 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1256 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1257 struct commit_queue *commits, int selected_idx, int limit,
1258 struct got_reflist_head *refs, const char *path, int commits_needed)
1260 const struct got_error *err = NULL;
1261 struct tog_log_view_state *s = &view->state.log;
1262 struct commit_queue_entry *entry;
1264 int ncommits, author_cols = 10;
1265 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1266 char *refs_str = NULL;
1268 static const size_t date_display_cols = 9;
1273 if (ncommits == selected_idx) {
1277 entry = TAILQ_NEXT(entry, entry);
1281 if (*selected && !(view->searching && view->search_next_done == 0)) {
1282 err = got_object_id_str(&id_str, (*selected)->id);
1286 err = build_refs_str(&refs_str, refs, (*selected)->id,
1293 if (commits_needed == 0)
1294 halfdelay(10); /* disable fast refresh */
1296 if (asprintf(&ncommits_str, " [%d/%d] %s",
1297 entry ? entry->idx + 1 : 0, commits->ncommits,
1298 commits_needed > 0 ?
1299 (view->searching && view->search_next_done == 0
1300 ? "searching..." : "loading... ") :
1301 (refs_str ? refs_str : "")) == -1) {
1302 err = got_error_from_errno("asprintf");
1306 if (path && strcmp(path, "/") != 0) {
1307 if (asprintf(&header, "commit %s %s%s",
1308 id_str ? id_str : "........................................",
1309 path, ncommits_str) == -1) {
1310 err = got_error_from_errno("asprintf");
1314 } else if (asprintf(&header, "commit %s%s",
1315 id_str ? id_str : "........................................",
1316 ncommits_str) == -1) {
1317 err = got_error_from_errno("asprintf");
1321 err = format_line(&wline, &width, header, view->ncols, 0);
1325 werase(view->window);
1327 if (view_needs_focus_indication(view))
1328 wstandout(view->window);
1329 waddwstr(view->window, wline);
1330 while (width < view->ncols) {
1331 waddch(view->window, ' ');
1334 if (view_needs_focus_indication(view))
1335 wstandend(view->window);
1340 /* Grow author column size if necessary. */
1347 if (ncommits >= limit - 1)
1349 author = strdup(got_object_commit_get_author(entry->commit));
1350 if (author == NULL) {
1351 err = got_error_from_errno("strdup");
1354 err = format_author(&wauthor, &width, author, COLS,
1356 if (author_cols < width)
1357 author_cols = width;
1360 entry = TAILQ_NEXT(entry, entry);
1367 if (ncommits >= limit - 1)
1369 if (ncommits == selected_idx)
1370 wstandout(view->window);
1371 err = draw_commit(view, entry->commit, entry->id, refs,
1372 date_display_cols, author_cols);
1373 if (ncommits == selected_idx)
1374 wstandend(view->window);
1379 entry = TAILQ_NEXT(entry, entry);
1392 scroll_up(struct tog_view *view,
1393 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1394 struct commit_queue *commits)
1396 struct commit_queue_entry *entry;
1399 entry = TAILQ_FIRST(&commits->head);
1400 if (*first_displayed_entry == entry)
1403 entry = *first_displayed_entry;
1404 while (entry && nscrolled < maxscroll) {
1405 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1407 *first_displayed_entry = entry;
1413 static const struct got_error *
1414 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1415 pthread_cond_t *need_commits)
1420 halfdelay(1); /* fast refresh while loading commits */
1422 while (*commits_needed > 0) {
1426 /* Wake the log thread. */
1427 errcode = pthread_cond_signal(need_commits);
1429 return got_error_set_errno(errcode,
1430 "pthread_cond_signal");
1431 errcode = pthread_mutex_unlock(&tog_mutex);
1433 return got_error_set_errno(errcode,
1434 "pthread_mutex_unlock");
1436 errcode = pthread_mutex_lock(&tog_mutex);
1438 return got_error_set_errno(errcode,
1439 "pthread_mutex_lock");
1441 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1443 * Thread is not done yet; lose a key press
1444 * and let the user retry... this way the GUI
1445 * remains interactive while logging deep paths
1446 * with few commits in history.
1455 static const struct got_error *
1456 scroll_down(struct tog_view *view,
1457 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1458 struct commit_queue_entry **last_displayed_entry,
1459 struct commit_queue *commits, int *log_complete, int *commits_needed,
1460 pthread_cond_t *need_commits)
1462 const struct got_error *err = NULL;
1463 struct commit_queue_entry *pentry;
1466 if (*last_displayed_entry == NULL)
1469 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1470 if (pentry == NULL && !*log_complete) {
1472 * Ask the log thread for required amount of commits
1473 * plus some amount of pre-fetching.
1475 (*commits_needed) += maxscroll + 20;
1476 err = trigger_log_thread(0, commits_needed, log_complete,
1483 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1487 *last_displayed_entry = pentry;
1489 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1492 *first_displayed_entry = pentry;
1493 } while (++nscrolled < maxscroll);
1498 static const struct got_error *
1499 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1500 struct got_commit_object *commit, struct got_object_id *commit_id,
1501 struct tog_view *log_view, struct got_reflist_head *refs,
1502 struct got_repository *repo)
1504 const struct got_error *err;
1505 struct got_object_qid *parent_id;
1506 struct tog_view *diff_view;
1508 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1509 if (diff_view == NULL)
1510 return got_error_from_errno("view_open");
1512 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1513 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1514 commit_id, log_view, refs, repo);
1516 *new_view = diff_view;
1520 static const struct got_error *
1521 tree_view_visit_subtree(struct got_tree_object *subtree,
1522 struct tog_tree_view_state *s)
1524 struct tog_parent_tree *parent;
1526 parent = calloc(1, sizeof(*parent));
1528 return got_error_from_errno("calloc");
1530 parent->tree = s->tree;
1531 parent->first_displayed_entry = s->first_displayed_entry;
1532 parent->selected_entry = s->selected_entry;
1533 parent->selected = s->selected;
1534 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1536 s->entries = got_object_tree_get_entries(s->tree);
1538 s->first_displayed_entry = NULL;
1543 static const struct got_error *
1544 browse_commit_tree(struct tog_view **new_view, int begin_x,
1545 struct commit_queue_entry *entry, const char *path,
1546 struct got_reflist_head *refs, struct got_repository *repo)
1548 const struct got_error *err = NULL;
1549 struct got_tree_object *tree;
1550 struct got_tree_entry *te;
1551 struct tog_tree_view_state *s;
1552 struct tog_view *tree_view;
1553 char *slash, *subpath = NULL;
1556 err = got_object_open_as_tree(&tree, repo,
1557 got_object_commit_get_tree_id(entry->commit));
1561 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1562 if (tree_view == NULL)
1563 return got_error_from_errno("view_open");
1565 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1567 got_object_tree_close(tree);
1570 s = &tree_view->state.tree;
1572 *new_view = tree_view;
1574 /* Walk the path and open corresponding tree objects. */
1579 struct got_object_id *tree_id;
1581 /* Ensure the correct subtree entry is selected. */
1582 slash = strchr(p, '/');
1584 slash = strchr(p, '\0');
1585 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1586 if (strncmp(p, te->name, slash - p) == 0) {
1587 s->selected_entry = te;
1592 if (s->selected_entry == NULL) {
1593 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1596 if (s->tree != s->root)
1597 s->selected++; /* skip '..' */
1599 if (!S_ISDIR(s->selected_entry->mode)) {
1600 /* Jump to this file's entry. */
1601 s->first_displayed_entry = s->selected_entry;
1606 slash = strchr(p, '/');
1608 subpath = strndup(path, slash - path);
1610 subpath = strdup(path);
1611 if (subpath == NULL) {
1612 err = got_error_from_errno("strdup");
1616 err = got_object_id_by_path(&tree_id, repo, entry->id,
1621 err = got_object_open_as_tree(&tree, repo, tree_id);
1626 err = tree_view_visit_subtree(tree, s);
1628 got_object_tree_close(tree);
1643 log_thread(void *arg)
1645 const struct got_error *err = NULL;
1647 struct tog_log_thread_args *a = arg;
1650 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1655 while (!done && !err && !tog_sigpipe_received) {
1656 err = queue_commits(a->graph, a->commits, 1, a->repo,
1659 if (err->code != GOT_ERR_ITER_COMPLETED)
1663 } else if (a->commits_needed > 0)
1664 a->commits_needed--;
1666 errcode = pthread_mutex_lock(&tog_mutex);
1668 err = got_error_set_errno(errcode,
1669 "pthread_mutex_lock");
1671 } else if (*a->quit)
1673 else if (*a->first_displayed_entry == NULL) {
1674 *a->first_displayed_entry =
1675 TAILQ_FIRST(&a->commits->head);
1676 *a->selected_entry = *a->first_displayed_entry;
1680 a->commits_needed = 0;
1681 else if (a->commits_needed == 0) {
1682 errcode = pthread_cond_wait(&a->need_commits,
1685 err = got_error_set_errno(errcode,
1686 "pthread_cond_wait");
1689 errcode = pthread_mutex_unlock(&tog_mutex);
1690 if (errcode && err == NULL)
1691 err = got_error_set_errno(errcode,
1692 "pthread_mutex_unlock");
1694 a->log_complete = 1;
1698 static const struct got_error *
1699 stop_log_thread(struct tog_log_view_state *s)
1701 const struct got_error *err = NULL;
1706 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1708 return got_error_set_errno(errcode,
1709 "pthread_cond_signal");
1710 errcode = pthread_mutex_unlock(&tog_mutex);
1712 return got_error_set_errno(errcode,
1713 "pthread_mutex_unlock");
1714 errcode = pthread_join(s->thread, (void **)&err);
1716 return got_error_set_errno(errcode, "pthread_join");
1717 errcode = pthread_mutex_lock(&tog_mutex);
1719 return got_error_set_errno(errcode,
1720 "pthread_mutex_lock");
1724 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1725 if (errcode && err == NULL)
1726 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1728 if (s->thread_args.repo) {
1729 got_repo_close(s->thread_args.repo);
1730 s->thread_args.repo = NULL;
1733 if (s->thread_args.graph) {
1734 got_commit_graph_close(s->thread_args.graph);
1735 s->thread_args.graph = NULL;
1741 static const struct got_error *
1742 close_log_view(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1747 err = stop_log_thread(s);
1748 free_commits(&s->commits);
1749 free(s->in_repo_path);
1750 s->in_repo_path = NULL;
1756 static const struct got_error *
1757 search_start_log_view(struct tog_view *view)
1759 struct tog_log_view_state *s = &view->state.log;
1761 s->matched_entry = NULL;
1762 s->search_entry = NULL;
1767 match_commit(struct got_commit_object *commit, const char *id_str,
1768 const char *logmsg, regex_t *regex)
1770 regmatch_t regmatch;
1772 if (regexec(regex, got_object_commit_get_author(commit), 1,
1773 ®match, 0) == 0 ||
1774 regexec(regex, got_object_commit_get_committer(commit), 1,
1775 ®match, 0) == 0 ||
1776 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1777 regexec(regex, logmsg, 1, ®match, 0) == 0)
1783 static const struct got_error *
1784 search_next_log_view(struct tog_view *view)
1786 const struct got_error *err = NULL;
1787 struct tog_log_view_state *s = &view->state.log;
1788 struct commit_queue_entry *entry;
1790 if (!view->searching) {
1791 view->search_next_done = 1;
1795 if (s->search_entry) {
1796 if (wgetch(view->window) == KEY_BACKSPACE) {
1797 view->search_next_done = 1;
1800 if (view->searching == TOG_SEARCH_FORWARD)
1801 entry = TAILQ_NEXT(s->search_entry, entry);
1803 entry = TAILQ_PREV(s->search_entry,
1804 commit_queue_head, entry);
1805 } else if (s->matched_entry) {
1806 if (view->searching == TOG_SEARCH_FORWARD)
1807 entry = TAILQ_NEXT(s->selected_entry, entry);
1809 entry = TAILQ_PREV(s->selected_entry,
1810 commit_queue_head, entry);
1812 if (view->searching == TOG_SEARCH_FORWARD)
1813 entry = TAILQ_FIRST(&s->commits.head);
1815 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1819 char *id_str, *logmsg;
1820 if (entry == NULL) {
1821 if (s->thread_args.log_complete ||
1822 view->searching == TOG_SEARCH_BACKWARD) {
1823 view->search_next_done = 1;
1827 * Poke the log thread for more commits and return,
1828 * allowing the main loop to make progress. Search
1829 * will resume at s->search_entry once we come back.
1831 s->thread_args.commits_needed++;
1832 return trigger_log_thread(1,
1833 &s->thread_args.commits_needed,
1834 &s->thread_args.log_complete,
1835 &s->thread_args.need_commits);
1838 err = got_object_id_str(&id_str, entry->id);
1842 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1845 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1847 view->search_next_done = 1;
1848 s->matched_entry = entry;
1854 s->search_entry = entry;
1855 if (view->searching == TOG_SEARCH_FORWARD)
1856 entry = TAILQ_NEXT(entry, entry);
1858 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1861 if (s->matched_entry) {
1862 int cur = s->selected_entry->idx;
1863 while (cur < s->matched_entry->idx) {
1864 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1869 while (cur > s->matched_entry->idx) {
1870 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1877 s->search_entry = NULL;
1882 static const struct got_error *
1883 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1884 struct got_reflist_head *refs, struct got_repository *repo,
1885 const char *head_ref_name, const char *path, int check_disk)
1887 const struct got_error *err = NULL;
1888 struct tog_log_view_state *s = &view->state.log;
1889 struct got_repository *thread_repo = NULL;
1890 struct got_commit_graph *thread_graph = NULL;
1893 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1897 /* The commit queue only contains commits being displayed. */
1898 TAILQ_INIT(&s->commits.head);
1899 s->commits.ncommits = 0;
1903 s->head_ref_name = head_ref_name;
1904 s->start_id = got_object_id_dup(start_id);
1905 if (s->start_id == NULL) {
1906 err = got_error_from_errno("got_object_id_dup");
1910 view->show = show_log_view;
1911 view->input = input_log_view;
1912 view->close = close_log_view;
1913 view->search_start = search_start_log_view;
1914 view->search_next = search_next_log_view;
1916 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1919 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1924 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1926 err = got_error_set_errno(errcode, "pthread_cond_init");
1930 s->thread_args.commits_needed = view->nlines;
1931 s->thread_args.graph = thread_graph;
1932 s->thread_args.commits = &s->commits;
1933 s->thread_args.in_repo_path = s->in_repo_path;
1934 s->thread_args.start_id = s->start_id;
1935 s->thread_args.repo = thread_repo;
1936 s->thread_args.log_complete = 0;
1937 s->thread_args.quit = &s->quit;
1938 s->thread_args.view = view;
1939 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1940 s->thread_args.selected_entry = &s->selected_entry;
1943 close_log_view(view);
1947 static const struct got_error *
1948 show_log_view(struct tog_view *view)
1950 struct tog_log_view_state *s = &view->state.log;
1952 if (s->thread == NULL) {
1953 int errcode = pthread_create(&s->thread, NULL, log_thread,
1956 return got_error_set_errno(errcode, "pthread_create");
1959 return draw_commits(view, &s->last_displayed_entry,
1960 &s->selected_entry, s->first_displayed_entry,
1961 &s->commits, s->selected, view->nlines, s->refs,
1962 s->in_repo_path, s->thread_args.commits_needed);
1965 static const struct got_error *
1966 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1967 struct tog_view **focus_view, struct tog_view *view, int ch)
1969 const struct got_error *err = NULL;
1970 struct tog_log_view_state *s = &view->state.log;
1971 char *parent_path, *in_repo_path = NULL;
1972 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1974 struct got_object_id *start_id;
1984 if (s->first_displayed_entry == NULL)
1986 if (s->selected > 0)
1989 scroll_up(view, &s->first_displayed_entry, 1,
1994 if (s->first_displayed_entry == NULL)
1996 if (TAILQ_FIRST(&s->commits.head) ==
1997 s->first_displayed_entry) {
2001 scroll_up(view, &s->first_displayed_entry,
2002 view->nlines, &s->commits);
2008 if (s->first_displayed_entry == NULL)
2010 if (s->selected < MIN(view->nlines - 2,
2011 s->commits.ncommits - 1)) {
2015 err = scroll_down(view, &s->first_displayed_entry, 1,
2016 &s->last_displayed_entry, &s->commits,
2017 &s->thread_args.log_complete,
2018 &s->thread_args.commits_needed,
2019 &s->thread_args.need_commits);
2023 struct commit_queue_entry *first;
2024 first = s->first_displayed_entry;
2027 err = scroll_down(view, &s->first_displayed_entry,
2028 view->nlines, &s->last_displayed_entry,
2029 &s->commits, &s->thread_args.log_complete,
2030 &s->thread_args.commits_needed,
2031 &s->thread_args.need_commits);
2032 if (first == s->first_displayed_entry &&
2033 s->selected < MIN(view->nlines - 2,
2034 s->commits.ncommits - 1)) {
2035 /* can't scroll further down */
2036 s->selected = MIN(view->nlines - 2,
2037 s->commits.ncommits - 1);
2043 if (s->selected > view->nlines - 2)
2044 s->selected = view->nlines - 2;
2045 if (s->selected > s->commits.ncommits - 1)
2046 s->selected = s->commits.ncommits - 1;
2051 if (s->selected_entry == NULL)
2053 if (view_is_parent_view(view))
2054 begin_x = view_split_begin_x(view->begin_x);
2055 err = open_diff_view_for_commit(&diff_view, begin_x,
2056 s->selected_entry->commit, s->selected_entry->id,
2057 view, s->refs, s->repo);
2060 if (view_is_parent_view(view)) {
2061 err = view_close_child(view);
2064 err = view_set_child(view, diff_view);
2066 view_close(diff_view);
2069 *focus_view = diff_view;
2070 view->child_focussed = 1;
2072 *new_view = diff_view;
2075 if (s->selected_entry == NULL)
2077 if (view_is_parent_view(view))
2078 begin_x = view_split_begin_x(view->begin_x);
2079 err = browse_commit_tree(&tree_view, begin_x,
2080 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2083 if (view_is_parent_view(view)) {
2084 err = view_close_child(view);
2087 err = view_set_child(view, tree_view);
2089 view_close(tree_view);
2092 *focus_view = tree_view;
2093 view->child_focussed = 1;
2095 *new_view = tree_view;
2098 if (strcmp(s->in_repo_path, "/") == 0)
2100 parent_path = dirname(s->in_repo_path);
2101 if (parent_path && strcmp(parent_path, ".") != 0) {
2102 err = stop_log_thread(s);
2105 lv = view_open(view->nlines, view->ncols,
2106 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2108 return got_error_from_errno(
2110 err = open_log_view(lv, s->start_id, s->refs,
2111 s->repo, s->head_ref_name, parent_path, 0);
2114 if (view_is_parent_view(view))
2117 view_set_child(view->parent, lv);
2124 err = stop_log_thread(s);
2127 lv = view_open(view->nlines, view->ncols,
2128 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2130 return got_error_from_errno("view_open");
2131 err = get_head_commit_id(&start_id, s->head_ref_name ?
2132 s->head_ref_name : GOT_REF_HEAD, s->repo);
2137 in_repo_path = strdup(s->in_repo_path);
2138 if (in_repo_path == NULL) {
2141 return got_error_from_errno("strdup");
2143 err = open_log_view(lv, start_id, s->refs, s->repo,
2144 s->head_ref_name, in_repo_path, 0);
2160 static const struct got_error *
2161 apply_unveil(const char *repo_path, const char *worktree_path)
2163 const struct got_error *error;
2166 if (unveil("gmon.out", "rwc") != 0)
2167 return got_error_from_errno2("unveil", "gmon.out");
2169 if (repo_path && unveil(repo_path, "r") != 0)
2170 return got_error_from_errno2("unveil", repo_path);
2172 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2173 return got_error_from_errno2("unveil", worktree_path);
2175 if (unveil("/tmp", "rwc") != 0)
2176 return got_error_from_errno2("unveil", "/tmp");
2178 error = got_privsep_unveil_exec_helpers();
2182 if (unveil(NULL, NULL) != 0)
2183 return got_error_from_errno("unveil");
2193 halfdelay(1); /* Do fast refresh while initial view is loading. */
2196 intrflush(stdscr, FALSE);
2197 keypad(stdscr, TRUE);
2199 signal(SIGWINCH, tog_sigwinch);
2200 signal(SIGPIPE, tog_sigpipe);
2203 static const struct got_error *
2204 cmd_log(int argc, char *argv[])
2206 const struct got_error *error;
2207 struct got_repository *repo = NULL;
2208 struct got_worktree *worktree = NULL;
2209 struct got_reflist_head refs;
2210 struct got_object_id *start_id = NULL;
2211 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2212 char *start_commit = NULL, *head_ref_name = NULL;
2214 struct tog_view *view;
2216 SIMPLEQ_INIT(&refs);
2219 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2224 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2227 start_commit = optarg;
2230 repo_path = realpath(optarg, NULL);
2231 if (repo_path == NULL)
2232 err(1, "-r option");
2243 cwd = getcwd(NULL, 0);
2245 error = got_error_from_errno("getcwd");
2248 error = got_worktree_open(&worktree, cwd);
2249 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2256 error = got_error_from_errno("strdup");
2259 } else if (argc == 1) {
2261 error = got_worktree_resolve_path(&path, worktree,
2266 path = strdup(argv[0]);
2268 error = got_error_from_errno("strdup");
2275 if (repo_path == NULL) {
2278 got_worktree_get_repo_path(worktree));
2280 repo_path = strdup(cwd);
2282 if (repo_path == NULL) {
2283 error = got_error_from_errno("strdup");
2289 error = got_repo_open(&repo, repo_path, NULL);
2293 error = apply_unveil(got_repo_get_path(repo),
2294 worktree ? got_worktree_get_root_path(worktree) : NULL);
2298 if (start_commit == NULL)
2299 error = get_head_commit_id(&start_id, worktree ?
2300 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2303 error = get_head_commit_id(&start_id, start_commit, repo);
2305 if (error->code != GOT_ERR_NOT_REF)
2307 error = got_repo_match_object_id_prefix(&start_id,
2308 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2314 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2318 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2320 error = got_error_from_errno("view_open");
2324 head_ref_name = strdup(
2325 got_worktree_get_head_ref_name(worktree));
2326 if (head_ref_name == NULL) {
2327 error = got_error_from_errno("strdup");
2331 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2336 /* Release work tree lock. */
2337 got_worktree_close(worktree);
2340 error = view_loop(view);
2346 free(head_ref_name);
2348 got_repo_close(repo);
2350 got_worktree_close(worktree);
2351 got_ref_list_free(&refs);
2359 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2365 parse_next_line(FILE *f, size_t *len)
2370 const char delim[3] = { '\0', '\0', '\0'};
2372 line = fparseln(f, &linelen, &lineno, delim, 0);
2378 static const struct got_error *
2379 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2380 int *last_displayed_line, int *eof, int max_lines,
2383 const struct got_error *err;
2384 int nlines = 0, nprinted = 0;
2391 werase(view->window);
2394 err = format_line(&wline, &width, header, view->ncols, 0);
2399 if (view_needs_focus_indication(view))
2400 wstandout(view->window);
2401 waddwstr(view->window, wline);
2402 if (view_needs_focus_indication(view))
2403 wstandend(view->window);
2404 if (width <= view->ncols - 1)
2405 waddch(view->window, '\n');
2413 while (nprinted < max_lines) {
2414 line = parse_next_line(f, &len);
2419 if (++nlines < *first_displayed_line) {
2424 err = format_line(&wline, &width, line, view->ncols, 0);
2429 waddwstr(view->window, wline);
2430 if (width <= view->ncols - 1)
2431 waddch(view->window, '\n');
2432 if (++nprinted == 1)
2433 *first_displayed_line = nlines;
2438 *last_displayed_line = nlines;
2443 while (nprinted < view->nlines) {
2444 waddch(view->window, '\n');
2448 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2453 wstandout(view->window);
2454 waddwstr(view->window, wline);
2455 wstandend(view->window);
2462 get_datestr(time_t *time, char *datebuf)
2464 struct tm mytm, *tm;
2467 tm = gmtime_r(time, &mytm);
2470 s = asctime_r(tm, datebuf);
2473 p = strchr(s, '\n');
2479 static const struct got_error *
2480 write_commit_info(struct got_object_id *commit_id,
2481 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2483 const struct got_error *err = NULL;
2484 char datebuf[26], *datestr;
2485 struct got_commit_object *commit;
2486 char *id_str = NULL, *logmsg = NULL;
2487 time_t committer_time;
2488 const char *author, *committer;
2489 char *refs_str = NULL;
2492 err = build_refs_str(&refs_str, refs, commit_id, repo);
2497 err = got_object_open_as_commit(&commit, repo, commit_id);
2501 err = got_object_id_str(&id_str, commit_id);
2503 err = got_error_from_errno("got_object_id_str");
2507 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2508 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2509 err = got_error_from_errno("fprintf");
2512 if (fprintf(outfile, "from: %s\n",
2513 got_object_commit_get_author(commit)) < 0) {
2514 err = got_error_from_errno("fprintf");
2517 committer_time = got_object_commit_get_committer_time(commit);
2518 datestr = get_datestr(&committer_time, datebuf);
2519 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2520 err = got_error_from_errno("fprintf");
2523 author = got_object_commit_get_author(commit);
2524 committer = got_object_commit_get_committer(commit);
2525 if (strcmp(author, committer) != 0 &&
2526 fprintf(outfile, "via: %s\n", committer) < 0) {
2527 err = got_error_from_errno("fprintf");
2530 err = got_object_commit_get_logmsg(&logmsg, commit);
2533 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2534 err = got_error_from_errno("fprintf");
2541 got_object_commit_close(commit);
2545 static const struct got_error *
2546 create_diff(struct tog_diff_view_state *s)
2548 const struct got_error *err = NULL;
2554 err = got_error_from_errno("got_opentemp");
2557 if (s->f && fclose(s->f) != 0) {
2558 err = got_error_from_errno("fclose");
2564 err = got_object_get_type(&obj_type, s->repo, s->id1);
2566 err = got_object_get_type(&obj_type, s->repo, s->id2);
2571 case GOT_OBJ_TYPE_BLOB:
2572 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2573 s->diff_context, s->repo, f);
2575 case GOT_OBJ_TYPE_TREE:
2576 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2577 s->diff_context, s->repo, f);
2579 case GOT_OBJ_TYPE_COMMIT: {
2580 const struct got_object_id_queue *parent_ids;
2581 struct got_object_qid *pid;
2582 struct got_commit_object *commit2;
2584 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2587 /* Show commit info if we're diffing to a parent/root commit. */
2589 write_commit_info(s->id2, s->refs, s->repo, f);
2591 parent_ids = got_object_commit_get_parent_ids(commit2);
2592 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2593 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2594 write_commit_info(s->id2, s->refs,
2600 got_object_commit_close(commit2);
2602 err = got_diff_objects_as_commits(s->id1, s->id2,
2603 s->diff_context, s->repo, f);
2607 err = got_error(GOT_ERR_OBJ_TYPE);
2611 if (f && fflush(f) != 0 && err == NULL)
2612 err = got_error_from_errno("fflush");
2617 diff_view_indicate_progress(struct tog_view *view)
2619 mvwaddstr(view->window, 0, 0, "diffing...");
2624 static const struct got_error *
2625 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2626 struct got_object_id *id2, struct tog_view *log_view,
2627 struct got_reflist_head *refs, struct got_repository *repo)
2629 const struct got_error *err;
2631 if (id1 != NULL && id2 != NULL) {
2633 err = got_object_get_type(&type1, repo, id1);
2636 err = got_object_get_type(&type2, repo, id2);
2641 return got_error(GOT_ERR_OBJ_TYPE);
2645 view->state.diff.id1 = got_object_id_dup(id1);
2646 if (view->state.diff.id1 == NULL)
2647 return got_error_from_errno("got_object_id_dup");
2649 view->state.diff.id1 = NULL;
2651 view->state.diff.id2 = got_object_id_dup(id2);
2652 if (view->state.diff.id2 == NULL) {
2653 free(view->state.diff.id1);
2654 view->state.diff.id1 = NULL;
2655 return got_error_from_errno("got_object_id_dup");
2657 view->state.diff.f = NULL;
2658 view->state.diff.first_displayed_line = 1;
2659 view->state.diff.last_displayed_line = view->nlines;
2660 view->state.diff.diff_context = 3;
2661 view->state.diff.log_view = log_view;
2662 view->state.diff.repo = repo;
2663 view->state.diff.refs = refs;
2665 if (log_view && view_is_splitscreen(view))
2666 show_log_view(log_view); /* draw vborder */
2667 diff_view_indicate_progress(view);
2669 err = create_diff(&view->state.diff);
2671 free(view->state.diff.id1);
2672 view->state.diff.id1 = NULL;
2673 free(view->state.diff.id2);
2674 view->state.diff.id2 = NULL;
2678 view->show = show_diff_view;
2679 view->input = input_diff_view;
2680 view->close = close_diff_view;
2685 static const struct got_error *
2686 close_diff_view(struct tog_view *view)
2688 const struct got_error *err = NULL;
2690 free(view->state.diff.id1);
2691 view->state.diff.id1 = NULL;
2692 free(view->state.diff.id2);
2693 view->state.diff.id2 = NULL;
2694 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2695 err = got_error_from_errno("fclose");
2699 static const struct got_error *
2700 show_diff_view(struct tog_view *view)
2702 const struct got_error *err;
2703 struct tog_diff_view_state *s = &view->state.diff;
2704 char *id_str1 = NULL, *id_str2, *header;
2707 err = got_object_id_str(&id_str1, s->id1);
2711 err = got_object_id_str(&id_str2, s->id2);
2715 if (asprintf(&header, "diff %s %s",
2716 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2717 err = got_error_from_errno("asprintf");
2725 return draw_file(view, s->f, &s->first_displayed_line,
2726 &s->last_displayed_line, &s->eof, view->nlines,
2730 static const struct got_error *
2731 set_selected_commit(struct tog_diff_view_state *s,
2732 struct commit_queue_entry *entry)
2734 const struct got_error *err;
2735 const struct got_object_id_queue *parent_ids;
2736 struct got_commit_object *selected_commit;
2737 struct got_object_qid *pid;
2740 s->id2 = got_object_id_dup(entry->id);
2742 return got_error_from_errno("got_object_id_dup");
2744 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2747 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2749 pid = SIMPLEQ_FIRST(parent_ids);
2750 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2751 got_object_commit_close(selected_commit);
2755 static const struct got_error *
2756 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2757 struct tog_view **focus_view, struct tog_view *view, int ch)
2759 const struct got_error *err = NULL;
2760 struct tog_diff_view_state *s = &view->state.diff;
2761 struct tog_log_view_state *ls;
2762 struct commit_queue_entry *entry;
2768 if (s->first_displayed_line > 1)
2769 s->first_displayed_line--;
2773 if (s->first_displayed_line == 1)
2776 while (i++ < view->nlines - 1 &&
2777 s->first_displayed_line > 1)
2778 s->first_displayed_line--;
2783 s->first_displayed_line++;
2791 while (!s->eof && i++ < view->nlines - 1) {
2793 line = parse_next_line(s->f, NULL);
2794 s->first_displayed_line++;
2800 if (s->diff_context > 0) {
2802 diff_view_indicate_progress(view);
2803 err = create_diff(s);
2807 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2809 diff_view_indicate_progress(view);
2810 err = create_diff(s);
2815 if (s->log_view == NULL)
2817 ls = &s->log_view->state.log;
2818 entry = TAILQ_PREV(ls->selected_entry,
2819 commit_queue_head, entry);
2823 err = input_log_view(NULL, NULL, NULL, s->log_view,
2828 err = set_selected_commit(s, entry);
2832 s->first_displayed_line = 1;
2833 s->last_displayed_line = view->nlines;
2835 diff_view_indicate_progress(view);
2836 err = create_diff(s);
2840 if (s->log_view == NULL)
2842 ls = &s->log_view->state.log;
2844 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2845 ls->thread_args.commits_needed++;
2847 /* Display "loading..." in log view. */
2848 show_log_view(s->log_view);
2852 err = trigger_log_thread(1 /* load_all */,
2853 &ls->thread_args.commits_needed,
2854 &ls->thread_args.log_complete,
2855 &ls->thread_args.need_commits);
2859 err = input_log_view(NULL, NULL, NULL, s->log_view,
2864 entry = TAILQ_NEXT(ls->selected_entry, entry);
2868 err = set_selected_commit(s, entry);
2872 s->first_displayed_line = 1;
2873 s->last_displayed_line = view->nlines;
2875 diff_view_indicate_progress(view);
2876 err = create_diff(s);
2885 static const struct got_error *
2886 cmd_diff(int argc, char *argv[])
2888 const struct got_error *error = NULL;
2889 struct got_repository *repo = NULL;
2890 struct got_reflist_head refs;
2891 struct got_object_id *id1 = NULL, *id2 = NULL;
2892 char *repo_path = NULL;
2893 char *id_str1 = NULL, *id_str2 = NULL;
2895 struct tog_view *view;
2897 SIMPLEQ_INIT(&refs);
2900 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2905 while ((ch = getopt(argc, argv, "")) != -1) {
2917 usage_diff(); /* TODO show local worktree changes */
2918 } else if (argc == 2) {
2919 repo_path = getcwd(NULL, 0);
2920 if (repo_path == NULL)
2921 return got_error_from_errno("getcwd");
2924 } else if (argc == 3) {
2925 repo_path = realpath(argv[0], NULL);
2926 if (repo_path == NULL)
2927 return got_error_from_errno2("realpath", argv[0]);
2935 error = got_repo_open(&repo, repo_path, NULL);
2939 error = apply_unveil(got_repo_get_path(repo), NULL);
2943 error = got_repo_match_object_id_prefix(&id1, id_str1,
2944 GOT_OBJ_TYPE_ANY, repo);
2948 error = got_repo_match_object_id_prefix(&id2, id_str2,
2949 GOT_OBJ_TYPE_ANY, repo);
2953 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2957 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2959 error = got_error_from_errno("view_open");
2962 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2965 error = view_loop(view);
2969 got_repo_close(repo);
2970 got_ref_list_free(&refs);
2978 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2983 struct tog_blame_line {
2985 struct got_object_id *id;
2988 static const struct got_error *
2989 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2990 const char *path, struct tog_blame_line *lines, int nlines,
2991 int blame_complete, int selected_line, int *first_displayed_line,
2992 int *last_displayed_line, int *eof, int max_lines)
2994 const struct got_error *err;
2995 int lineno = 0, nprinted = 0;
3000 struct tog_blame_line *blame_line;
3001 struct got_object_id *prev_id = NULL;
3004 err = got_object_id_str(&id_str, id);
3009 werase(view->window);
3011 if (asprintf(&line, "commit %s", id_str) == -1) {
3012 err = got_error_from_errno("asprintf");
3017 err = format_line(&wline, &width, line, view->ncols, 0);
3020 if (view_needs_focus_indication(view))
3021 wstandout(view->window);
3022 waddwstr(view->window, wline);
3023 if (view_needs_focus_indication(view))
3024 wstandend(view->window);
3027 if (width < view->ncols - 1)
3028 waddch(view->window, '\n');
3030 if (asprintf(&line, "[%d/%d] %s%s",
3031 *first_displayed_line - 1 + selected_line, nlines,
3032 blame_complete ? "" : "annotating... ", path) == -1) {
3034 return got_error_from_errno("asprintf");
3037 err = format_line(&wline, &width, line, view->ncols, 0);
3042 waddwstr(view->window, wline);
3045 if (width < view->ncols - 1)
3046 waddch(view->window, '\n');
3049 while (nprinted < max_lines - 2) {
3050 line = parse_next_line(f, &len);
3055 if (++lineno < *first_displayed_line) {
3060 if (view->ncols <= 9) {
3062 wline = wcsdup(L"");
3064 err = got_error_from_errno("wcsdup");
3066 err = format_line(&wline, &width, line,
3067 view->ncols - 9, 9);
3075 if (view->focussed && nprinted == selected_line - 1)
3076 wstandout(view->window);
3079 blame_line = &lines[lineno - 1];
3080 if (blame_line->annotated && prev_id &&
3081 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3083 nprinted == selected_line - 1)) {
3084 waddstr(view->window, " ");
3085 } else if (blame_line->annotated) {
3087 err = got_object_id_str(&id_str, blame_line->id);
3093 wprintw(view->window, "%.8s", id_str);
3095 prev_id = blame_line->id;
3097 waddstr(view->window, "........");
3101 waddstr(view->window, "........");
3105 if (view->focussed && nprinted == selected_line - 1)
3106 wstandend(view->window);
3107 waddstr(view->window, " ");
3109 waddwstr(view->window, wline);
3110 if (width <= view->ncols - 1)
3111 waddch(view->window, '\n');
3112 if (++nprinted == 1)
3113 *first_displayed_line = lineno;
3118 *last_displayed_line = lineno;
3125 static const struct got_error *
3126 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3128 const struct got_error *err = NULL;
3129 struct tog_blame_cb_args *a = arg;
3130 struct tog_blame_line *line;
3133 if (nlines != a->nlines ||
3134 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3135 return got_error(GOT_ERR_RANGE);
3137 errcode = pthread_mutex_lock(&tog_mutex);
3139 return got_error_set_errno(errcode, "pthread_mutex_lock");
3141 if (*a->quit) { /* user has quit the blame view */
3142 err = got_error(GOT_ERR_ITER_COMPLETED);
3147 goto done; /* no change in this commit */
3149 line = &a->lines[lineno - 1];
3150 if (line->annotated)
3153 line->id = got_object_id_dup(id);
3154 if (line->id == NULL) {
3155 err = got_error_from_errno("got_object_id_dup");
3158 line->annotated = 1;
3160 errcode = pthread_mutex_unlock(&tog_mutex);
3162 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3167 blame_thread(void *arg)
3169 const struct got_error *err;
3170 struct tog_blame_thread_args *ta = arg;
3171 struct tog_blame_cb_args *a = ta->cb_args;
3174 err = got_blame(ta->path, a->commit_id, ta->repo,
3175 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3176 if (err && err->code == GOT_ERR_CANCELLED)
3179 errcode = pthread_mutex_lock(&tog_mutex);
3181 return (void *)got_error_set_errno(errcode,
3182 "pthread_mutex_lock");
3184 got_repo_close(ta->repo);
3188 errcode = pthread_mutex_unlock(&tog_mutex);
3189 if (errcode && err == NULL)
3190 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3195 static struct got_object_id *
3196 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3197 int first_displayed_line, int selected_line)
3199 struct tog_blame_line *line;
3204 line = &lines[first_displayed_line - 1 + selected_line - 1];
3205 if (!line->annotated)
3211 static const struct got_error *
3212 stop_blame(struct tog_blame *blame)
3214 const struct got_error *err = NULL;
3217 if (blame->thread) {
3219 errcode = pthread_mutex_unlock(&tog_mutex);
3221 return got_error_set_errno(errcode,
3222 "pthread_mutex_unlock");
3223 errcode = pthread_join(blame->thread, (void **)&err);
3225 return got_error_set_errno(errcode, "pthread_join");
3226 errcode = pthread_mutex_lock(&tog_mutex);
3228 return got_error_set_errno(errcode,
3229 "pthread_mutex_lock");
3230 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3232 blame->thread = NULL;
3234 if (blame->thread_args.repo) {
3235 got_repo_close(blame->thread_args.repo);
3236 blame->thread_args.repo = NULL;
3239 if (fclose(blame->f) != 0 && err == NULL)
3240 err = got_error_from_errno("fclose");
3244 for (i = 0; i < blame->nlines; i++)
3245 free(blame->lines[i].id);
3247 blame->lines = NULL;
3249 free(blame->cb_args.commit_id);
3250 blame->cb_args.commit_id = NULL;
3255 static const struct got_error *
3256 cancel_blame_view(void *arg)
3258 const struct got_error *err = NULL;
3262 errcode = pthread_mutex_lock(&tog_mutex);
3264 return got_error_set_errno(errcode,
3265 "pthread_mutex_unlock");
3268 err = got_error(GOT_ERR_CANCELLED);
3270 errcode = pthread_mutex_unlock(&tog_mutex);
3272 return got_error_set_errno(errcode,
3273 "pthread_mutex_lock");
3278 static const struct got_error *
3279 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3280 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3281 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3282 struct got_repository *repo)
3284 const struct got_error *err = NULL;
3285 struct got_blob_object *blob = NULL;
3286 struct got_repository *thread_repo = NULL;
3287 struct got_object_id *obj_id = NULL;
3290 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3294 return got_error(GOT_ERR_NO_OBJ);
3296 err = got_object_get_type(&obj_type, repo, obj_id);
3300 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3301 err = got_error(GOT_ERR_OBJ_TYPE);
3305 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3308 blame->f = got_opentemp();
3309 if (blame->f == NULL) {
3310 err = got_error_from_errno("got_opentemp");
3313 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3314 &blame->line_offsets, blame->f, blob);
3315 if (err || blame->nlines == 0)
3318 /* Don't include \n at EOF in the blame line count. */
3319 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3322 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3323 if (blame->lines == NULL) {
3324 err = got_error_from_errno("calloc");
3328 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3332 blame->cb_args.view = view;
3333 blame->cb_args.lines = blame->lines;
3334 blame->cb_args.nlines = blame->nlines;
3335 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3336 if (blame->cb_args.commit_id == NULL) {
3337 err = got_error_from_errno("got_object_id_dup");
3340 blame->cb_args.quit = done;
3342 blame->thread_args.path = path;
3343 blame->thread_args.repo = thread_repo;
3344 blame->thread_args.cb_args = &blame->cb_args;
3345 blame->thread_args.complete = blame_complete;
3346 blame->thread_args.cancel_cb = cancel_blame_view;
3347 blame->thread_args.cancel_arg = done;
3348 *blame_complete = 0;
3352 got_object_blob_close(blob);
3359 static const struct got_error *
3360 open_blame_view(struct tog_view *view, char *path,
3361 struct got_object_id *commit_id, struct got_reflist_head *refs,
3362 struct got_repository *repo)
3364 const struct got_error *err = NULL;
3365 struct tog_blame_view_state *s = &view->state.blame;
3367 SIMPLEQ_INIT(&s->blamed_commits);
3369 s->path = strdup(path);
3370 if (s->path == NULL)
3371 return got_error_from_errno("strdup");
3373 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3379 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3380 s->first_displayed_line = 1;
3381 s->last_displayed_line = view->nlines;
3382 s->selected_line = 1;
3383 s->blame_complete = 0;
3386 s->commit_id = commit_id;
3387 memset(&s->blame, 0, sizeof(s->blame));
3389 view->show = show_blame_view;
3390 view->input = input_blame_view;
3391 view->close = close_blame_view;
3392 view->search_start = search_start_blame_view;
3393 view->search_next = search_next_blame_view;
3395 return run_blame(&s->blame, view, &s->blame_complete,
3396 &s->first_displayed_line, &s->last_displayed_line,
3397 &s->selected_line, &s->done, &s->eof, s->path,
3398 s->blamed_commit->id, s->repo);
3401 static const struct got_error *
3402 close_blame_view(struct tog_view *view)
3404 const struct got_error *err = NULL;
3405 struct tog_blame_view_state *s = &view->state.blame;
3407 if (s->blame.thread)
3408 err = stop_blame(&s->blame);
3410 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3411 struct got_object_qid *blamed_commit;
3412 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3413 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3414 got_object_qid_free(blamed_commit);
3422 static const struct got_error *
3423 search_start_blame_view(struct tog_view *view)
3425 struct tog_blame_view_state *s = &view->state.blame;
3427 s->matched_line = 0;
3432 match_line(const char *line, regex_t *regex)
3434 regmatch_t regmatch;
3436 return regexec(regex, line, 1, ®match, 0) == 0;
3440 static const struct got_error *
3441 search_next_blame_view(struct tog_view *view)
3443 struct tog_blame_view_state *s = &view->state.blame;
3446 if (!view->searching) {
3447 view->search_next_done = 1;
3451 if (s->matched_line) {
3452 if (view->searching == TOG_SEARCH_FORWARD)
3453 lineno = s->matched_line + 1;
3455 lineno = s->matched_line - 1;
3457 if (view->searching == TOG_SEARCH_FORWARD)
3460 lineno = s->blame.nlines;
3468 if (lineno <= 0 || lineno > s->blame.nlines) {
3469 if (s->matched_line == 0) {
3470 view->search_next_done = 1;
3475 if (view->searching == TOG_SEARCH_FORWARD)
3478 lineno = s->blame.nlines;
3481 offset = s->blame.line_offsets[lineno - 1];
3482 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3484 return got_error_from_errno("fseeko");
3487 line = parse_next_line(s->blame.f, &len);
3488 if (line && match_line(line, &view->regex)) {
3489 view->search_next_done = 1;
3490 s->matched_line = lineno;
3495 if (view->searching == TOG_SEARCH_FORWARD)
3501 if (s->matched_line) {
3502 s->first_displayed_line = s->matched_line;
3503 s->selected_line = 1;
3509 static const struct got_error *
3510 show_blame_view(struct tog_view *view)
3512 const struct got_error *err = NULL;
3513 struct tog_blame_view_state *s = &view->state.blame;
3516 if (s->blame.thread == NULL) {
3517 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3518 &s->blame.thread_args);
3520 return got_error_set_errno(errcode, "pthread_create");
3522 halfdelay(1); /* fast refresh while annotating */
3525 if (s->blame_complete)
3526 halfdelay(10); /* disable fast refresh */
3528 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3529 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3530 s->selected_line, &s->first_displayed_line,
3531 &s->last_displayed_line, &s->eof, view->nlines);
3537 static const struct got_error *
3538 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3539 struct tog_view **focus_view, struct tog_view *view, int ch)
3541 const struct got_error *err = NULL, *thread_err = NULL;
3542 struct tog_view *diff_view;
3543 struct tog_blame_view_state *s = &view->state.blame;
3552 if (s->selected_line > 1)
3554 else if (s->selected_line == 1 &&
3555 s->first_displayed_line > 1)
3556 s->first_displayed_line--;
3559 if (s->first_displayed_line == 1) {
3560 s->selected_line = 1;
3563 if (s->first_displayed_line > view->nlines - 2)
3564 s->first_displayed_line -=
3567 s->first_displayed_line = 1;
3571 if (s->selected_line < view->nlines - 2 &&
3572 s->first_displayed_line +
3573 s->selected_line <= s->blame.nlines)
3575 else if (s->last_displayed_line <
3577 s->first_displayed_line++;
3581 struct got_object_id *id = NULL;
3582 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3583 s->first_displayed_line, s->selected_line);
3587 struct got_commit_object *commit;
3588 struct got_object_qid *pid;
3589 struct got_object_id *blob_id = NULL;
3591 err = got_object_open_as_commit(&commit,
3595 pid = SIMPLEQ_FIRST(
3596 got_object_commit_get_parent_ids(commit));
3598 got_object_commit_close(commit);
3601 /* Check if path history ends here. */
3602 err = got_object_id_by_path(&blob_id, s->repo,
3605 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3607 got_object_commit_close(commit);
3610 err = got_object_get_type(&obj_type, s->repo,
3613 /* Can't blame non-blob type objects. */
3614 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3615 got_object_commit_close(commit);
3618 err = got_object_qid_alloc(&s->blamed_commit,
3620 got_object_commit_close(commit);
3622 if (got_object_id_cmp(id,
3623 s->blamed_commit->id) == 0)
3625 err = got_object_qid_alloc(&s->blamed_commit,
3631 thread_err = stop_blame(&s->blame);
3635 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3636 s->blamed_commit, entry);
3637 err = run_blame(&s->blame, view, &s->blame_complete,
3638 &s->first_displayed_line, &s->last_displayed_line,
3639 &s->selected_line, &s->done, &s->eof,
3640 s->path, s->blamed_commit->id, s->repo);
3646 struct got_object_qid *first;
3647 first = SIMPLEQ_FIRST(&s->blamed_commits);
3648 if (!got_object_id_cmp(first->id, s->commit_id))
3651 thread_err = stop_blame(&s->blame);
3655 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3656 got_object_qid_free(s->blamed_commit);
3658 SIMPLEQ_FIRST(&s->blamed_commits);
3659 err = run_blame(&s->blame, view, &s->blame_complete,
3660 &s->first_displayed_line, &s->last_displayed_line,
3661 &s->selected_line, &s->done, &s->eof, s->path,
3662 s->blamed_commit->id, s->repo);
3669 struct got_object_id *id = NULL;
3670 struct got_object_qid *pid;
3671 struct got_commit_object *commit = NULL;
3672 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3673 s->first_displayed_line, s->selected_line);
3676 err = got_object_open_as_commit(&commit, s->repo, id);
3679 pid = SIMPLEQ_FIRST(
3680 got_object_commit_get_parent_ids(commit));
3681 if (view_is_parent_view(view))
3682 begin_x = view_split_begin_x(view->begin_x);
3683 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3684 if (diff_view == NULL) {
3685 got_object_commit_close(commit);
3686 err = got_error_from_errno("view_open");
3689 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3690 id, NULL, s->refs, s->repo);
3691 got_object_commit_close(commit);
3693 view_close(diff_view);
3696 if (view_is_parent_view(view)) {
3697 err = view_close_child(view);
3700 err = view_set_child(view, diff_view);
3702 view_close(diff_view);
3705 *focus_view = diff_view;
3706 view->child_focussed = 1;
3708 *new_view = diff_view;
3715 if (s->last_displayed_line >= s->blame.nlines &&
3716 s->selected_line >= MIN(s->blame.nlines,
3717 view->nlines - 2)) {
3720 if (s->last_displayed_line >= s->blame.nlines &&
3721 s->selected_line < view->nlines - 2) {
3722 s->selected_line = MIN(s->blame.nlines,
3726 if (s->last_displayed_line + view->nlines - 2
3728 s->first_displayed_line +=
3731 s->first_displayed_line =
3736 if (s->selected_line > view->nlines - 2) {
3737 s->selected_line = MIN(s->blame.nlines,
3744 return thread_err ? thread_err : err;
3747 static const struct got_error *
3748 cmd_blame(int argc, char *argv[])
3750 const struct got_error *error;
3751 struct got_repository *repo = NULL;
3752 struct got_reflist_head refs;
3753 struct got_worktree *worktree = NULL;
3754 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3755 struct got_object_id *commit_id = NULL;
3756 char *commit_id_str = NULL;
3758 struct tog_view *view;
3760 SIMPLEQ_INIT(&refs);
3763 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3768 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3771 commit_id_str = optarg;
3774 repo_path = realpath(optarg, NULL);
3775 if (repo_path == NULL)
3776 err(1, "-r option");
3792 cwd = getcwd(NULL, 0);
3794 error = got_error_from_errno("getcwd");
3797 if (repo_path == NULL) {
3798 error = got_worktree_open(&worktree, cwd);
3799 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3805 strdup(got_worktree_get_repo_path(worktree));
3806 if (repo_path == NULL)
3807 error = got_error_from_errno("strdup");
3811 repo_path = strdup(cwd);
3812 if (repo_path == NULL) {
3813 error = got_error_from_errno("strdup");
3821 error = got_repo_open(&repo, repo_path, NULL);
3825 error = apply_unveil(got_repo_get_path(repo), NULL);
3830 const char *prefix = got_worktree_get_path_prefix(worktree);
3831 char *p, *worktree_subdir = cwd +
3832 strlen(got_worktree_get_root_path(worktree));
3833 if (asprintf(&p, "%s%s%s%s%s",
3834 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3835 worktree_subdir, worktree_subdir[0] ? "/" : "",
3837 error = got_error_from_errno("asprintf");
3840 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3843 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3848 if (commit_id_str == NULL) {
3849 struct got_reference *head_ref;
3850 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3853 error = got_ref_resolve(&commit_id, repo, head_ref);
3854 got_ref_close(head_ref);
3856 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3858 if (error->code != GOT_ERR_NOT_REF)
3860 error = got_repo_match_object_id_prefix(&commit_id,
3861 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3867 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3871 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3873 error = got_error_from_errno("view_open");
3876 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3880 /* Release work tree lock. */
3881 got_worktree_close(worktree);
3884 error = view_loop(view);
3890 got_worktree_close(worktree);
3892 got_repo_close(repo);
3893 got_ref_list_free(&refs);
3897 static const struct got_error *
3898 draw_tree_entries(struct tog_view *view,
3899 struct got_tree_entry **first_displayed_entry,
3900 struct got_tree_entry **last_displayed_entry,
3901 struct got_tree_entry **selected_entry, int *ndisplayed,
3902 const char *label, int show_ids, const char *parent_path,
3903 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3905 const struct got_error *err = NULL;
3906 struct got_tree_entry *te;
3912 werase(view->window);
3917 err = format_line(&wline, &width, label, view->ncols, 0);
3920 if (view_needs_focus_indication(view))
3921 wstandout(view->window);
3922 waddwstr(view->window, wline);
3923 if (view_needs_focus_indication(view))
3924 wstandend(view->window);
3927 if (width < view->ncols - 1)
3928 waddch(view->window, '\n');
3931 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3934 waddwstr(view->window, wline);
3937 if (width < view->ncols - 1)
3938 waddch(view->window, '\n');
3941 waddch(view->window, '\n');
3945 te = SIMPLEQ_FIRST(&entries->head);
3946 if (*first_displayed_entry == NULL) {
3947 if (selected == 0) {
3949 wstandout(view->window);
3950 *selected_entry = NULL;
3952 waddstr(view->window, " ..\n"); /* parent directory */
3953 if (selected == 0 && view->focussed)
3954 wstandend(view->window);
3961 while (te != *first_displayed_entry)
3962 te = SIMPLEQ_NEXT(te, entry);
3966 char *line = NULL, *id_str = NULL;
3967 const char *modestr = "";
3970 err = got_object_id_str(&id_str, te->id);
3972 return got_error_from_errno(
3973 "got_object_id_str");
3975 if (got_object_tree_entry_is_submodule(te))
3977 else if (S_ISLNK(te->mode))
3979 else if (S_ISDIR(te->mode))
3981 else if (te->mode & S_IXUSR)
3983 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3984 te->name, modestr) == -1) {
3986 return got_error_from_errno("asprintf");
3989 err = format_line(&wline, &width, line, view->ncols, 0);
3994 if (n == selected) {
3996 wstandout(view->window);
3997 *selected_entry = te;
3999 waddwstr(view->window, wline);
4000 if (width < view->ncols - 1)
4001 waddch(view->window, '\n');
4002 if (n == selected && view->focussed)
4003 wstandend(view->window);
4009 *last_displayed_entry = te;
4012 te = SIMPLEQ_NEXT(te, entry);
4019 tree_scroll_up(struct tog_view *view,
4020 struct got_tree_entry **first_displayed_entry, int maxscroll,
4021 const struct got_tree_entries *entries, int isroot)
4023 struct got_tree_entry *te, *prev;
4026 if (*first_displayed_entry == NULL)
4029 te = SIMPLEQ_FIRST(&entries->head);
4030 if (*first_displayed_entry == te) {
4032 *first_displayed_entry = NULL;
4036 /* XXX this is stupid... switch to TAILQ? */
4037 for (i = 0; i < maxscroll; i++) {
4038 while (te != *first_displayed_entry) {
4040 te = SIMPLEQ_NEXT(te, entry);
4042 *first_displayed_entry = prev;
4043 te = SIMPLEQ_FIRST(&entries->head);
4045 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4046 *first_displayed_entry = NULL;
4050 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4051 struct got_tree_entry *last_displayed_entry,
4052 const struct got_tree_entries *entries)
4054 struct got_tree_entry *next, *last;
4057 if (*first_displayed_entry)
4058 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4060 next = SIMPLEQ_FIRST(&entries->head);
4061 last = last_displayed_entry;
4062 while (next && last && n++ < maxscroll) {
4063 last = SIMPLEQ_NEXT(last, entry);
4065 *first_displayed_entry = next;
4066 next = SIMPLEQ_NEXT(next, entry);
4072 static const struct got_error *
4073 tree_entry_path(char **path, struct tog_parent_trees *parents,
4074 struct got_tree_entry *te)
4076 const struct got_error *err = NULL;
4077 struct tog_parent_tree *pt;
4078 size_t len = 2; /* for leading slash and NUL */
4080 TAILQ_FOREACH(pt, parents, entry)
4081 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4083 len += strlen(te->name);
4085 *path = calloc(1, len);
4087 return got_error_from_errno("calloc");
4090 pt = TAILQ_LAST(parents, tog_parent_trees);
4092 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4093 err = got_error(GOT_ERR_NO_SPACE);
4096 if (strlcat(*path, "/", len) >= len) {
4097 err = got_error(GOT_ERR_NO_SPACE);
4100 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4103 if (strlcat(*path, te->name, len) >= len) {
4104 err = got_error(GOT_ERR_NO_SPACE);
4116 static const struct got_error *
4117 blame_tree_entry(struct tog_view **new_view, int begin_x,
4118 struct got_tree_entry *te, struct tog_parent_trees *parents,
4119 struct got_object_id *commit_id, struct got_reflist_head *refs,
4120 struct got_repository *repo)
4122 const struct got_error *err = NULL;
4124 struct tog_view *blame_view;
4128 err = tree_entry_path(&path, parents, te);
4132 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4133 if (blame_view == NULL) {
4134 err = got_error_from_errno("view_open");
4138 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4140 if (err->code == GOT_ERR_CANCELLED)
4142 view_close(blame_view);
4144 *new_view = blame_view;
4150 static const struct got_error *
4151 log_tree_entry(struct tog_view **new_view, int begin_x,
4152 struct got_tree_entry *te, struct tog_parent_trees *parents,
4153 struct got_object_id *commit_id, struct got_reflist_head *refs,
4154 struct got_repository *repo)
4156 struct tog_view *log_view;
4157 const struct got_error *err = NULL;
4162 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4163 if (log_view == NULL)
4164 return got_error_from_errno("view_open");
4166 err = tree_entry_path(&path, parents, te);
4170 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4172 view_close(log_view);
4174 *new_view = log_view;
4179 static const struct got_error *
4180 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4181 struct got_object_id *commit_id, struct got_reflist_head *refs,
4182 struct got_repository *repo)
4184 const struct got_error *err = NULL;
4185 char *commit_id_str = NULL;
4186 struct tog_tree_view_state *s = &view->state.tree;
4188 TAILQ_INIT(&s->parents);
4190 err = got_object_id_str(&commit_id_str, commit_id);
4194 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4195 err = got_error_from_errno("asprintf");
4199 s->root = s->tree = root;
4200 s->entries = got_object_tree_get_entries(root);
4201 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4202 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4203 s->commit_id = got_object_id_dup(commit_id);
4204 if (s->commit_id == NULL) {
4205 err = got_error_from_errno("got_object_id_dup");
4211 view->show = show_tree_view;
4212 view->input = input_tree_view;
4213 view->close = close_tree_view;
4214 view->search_start = search_start_tree_view;
4215 view->search_next = search_next_tree_view;
4217 free(commit_id_str);
4219 free(s->tree_label);
4220 s->tree_label = NULL;
4225 static const struct got_error *
4226 close_tree_view(struct tog_view *view)
4228 struct tog_tree_view_state *s = &view->state.tree;
4230 free(s->tree_label);
4231 s->tree_label = NULL;
4233 s->commit_id = NULL;
4234 while (!TAILQ_EMPTY(&s->parents)) {
4235 struct tog_parent_tree *parent;
4236 parent = TAILQ_FIRST(&s->parents);
4237 TAILQ_REMOVE(&s->parents, parent, entry);
4241 if (s->tree != s->root)
4242 got_object_tree_close(s->tree);
4243 got_object_tree_close(s->root);
4248 static const struct got_error *
4249 search_start_tree_view(struct tog_view *view)
4251 struct tog_tree_view_state *s = &view->state.tree;
4253 s->matched_entry = NULL;
4258 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4260 regmatch_t regmatch;
4262 return regexec(regex, te->name, 1, ®match, 0) == 0;
4265 static const struct got_error *
4266 search_next_tree_view(struct tog_view *view)
4268 struct tog_tree_view_state *s = &view->state.tree;
4269 struct got_tree_entry *entry = NULL, *te;
4271 if (!view->searching) {
4272 view->search_next_done = 1;
4276 if (s->matched_entry) {
4277 if (view->searching == TOG_SEARCH_FORWARD) {
4278 if (s->selected_entry)
4279 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4281 entry = SIMPLEQ_FIRST(&s->entries->head);
4284 if (s->selected_entry == NULL) {
4285 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4288 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4290 if (SIMPLEQ_NEXT(te, entry) ==
4297 if (view->searching == TOG_SEARCH_FORWARD)
4298 entry = SIMPLEQ_FIRST(&s->entries->head);
4300 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4306 if (entry == NULL) {
4307 if (s->matched_entry == NULL) {
4308 view->search_next_done = 1;
4311 if (view->searching == TOG_SEARCH_FORWARD)
4312 entry = SIMPLEQ_FIRST(&s->entries->head);
4314 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4319 if (match_tree_entry(entry, &view->regex)) {
4320 view->search_next_done = 1;
4321 s->matched_entry = entry;
4325 if (view->searching == TOG_SEARCH_FORWARD)
4326 entry = SIMPLEQ_NEXT(entry, entry);
4328 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4331 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4332 if (SIMPLEQ_NEXT(te, entry) == entry) {
4341 if (s->matched_entry) {
4342 s->first_displayed_entry = s->matched_entry;
4349 static const struct got_error *
4350 show_tree_view(struct tog_view *view)
4352 const struct got_error *err = NULL;
4353 struct tog_tree_view_state *s = &view->state.tree;
4356 err = tree_entry_path(&parent_path, &s->parents, NULL);
4360 err = draw_tree_entries(view, &s->first_displayed_entry,
4361 &s->last_displayed_entry, &s->selected_entry,
4362 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4363 s->entries, s->selected, view->nlines, s->tree == s->root);
4370 static const struct got_error *
4371 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4372 struct tog_view **focus_view, struct tog_view *view, int ch)
4374 const struct got_error *err = NULL;
4375 struct tog_tree_view_state *s = &view->state.tree;
4376 struct tog_view *log_view;
4377 int begin_x = 0, nscrolled;
4381 s->show_ids = !s->show_ids;
4384 if (!s->selected_entry)
4386 if (view_is_parent_view(view))
4387 begin_x = view_split_begin_x(view->begin_x);
4388 err = log_tree_entry(&log_view, begin_x,
4389 s->selected_entry, &s->parents,
4390 s->commit_id, s->refs, s->repo);
4391 if (view_is_parent_view(view)) {
4392 err = view_close_child(view);
4395 err = view_set_child(view, log_view);
4397 view_close(log_view);
4400 *focus_view = log_view;
4401 view->child_focussed = 1;
4403 *new_view = log_view;
4407 if (s->selected > 0) {
4409 if (s->selected == 0)
4412 if (s->selected > 0)
4414 tree_scroll_up(view, &s->first_displayed_entry, 1,
4415 s->entries, s->tree == s->root);
4418 tree_scroll_up(view, &s->first_displayed_entry,
4419 MAX(0, view->nlines - 4 - s->selected), s->entries,
4420 s->tree == s->root);
4422 if (SIMPLEQ_FIRST(&s->entries->head) ==
4423 s->first_displayed_entry && s->tree != s->root)
4424 s->first_displayed_entry = NULL;
4428 if (s->selected < s->ndisplayed - 1) {
4432 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4433 /* can't scroll any further */
4435 tree_scroll_down(&s->first_displayed_entry, 1,
4436 s->last_displayed_entry, s->entries);
4439 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4441 /* can't scroll any further; move cursor down */
4442 if (s->selected < s->ndisplayed - 1)
4443 s->selected = s->ndisplayed - 1;
4446 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4447 view->nlines, s->last_displayed_entry, s->entries);
4448 if (nscrolled < view->nlines) {
4450 struct got_tree_entry *te;
4451 te = s->first_displayed_entry;
4454 te = SIMPLEQ_NEXT(te, entry);
4456 s->selected = ndisplayed - 1;
4462 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4463 struct tog_parent_tree *parent;
4464 /* user selected '..' */
4465 if (s->tree == s->root)
4467 parent = TAILQ_FIRST(&s->parents);
4468 TAILQ_REMOVE(&s->parents, parent,
4470 got_object_tree_close(s->tree);
4471 s->tree = parent->tree;
4473 got_object_tree_get_entries(s->tree);
4474 s->first_displayed_entry =
4475 parent->first_displayed_entry;
4477 parent->selected_entry;
4478 s->selected = parent->selected;
4480 } else if (S_ISDIR(s->selected_entry->mode)) {
4481 struct got_tree_object *subtree;
4482 err = got_object_open_as_tree(&subtree,
4483 s->repo, s->selected_entry->id);
4486 err = tree_view_visit_subtree(subtree, s);
4488 got_object_tree_close(subtree);
4491 } else if (S_ISREG(s->selected_entry->mode)) {
4492 struct tog_view *blame_view;
4493 int begin_x = view_is_parent_view(view) ?
4494 view_split_begin_x(view->begin_x) : 0;
4496 err = blame_tree_entry(&blame_view, begin_x,
4497 s->selected_entry, &s->parents,
4498 s->commit_id, s->refs, s->repo);
4501 if (view_is_parent_view(view)) {
4502 err = view_close_child(view);
4505 err = view_set_child(view, blame_view);
4507 view_close(blame_view);
4510 *focus_view = blame_view;
4511 view->child_focussed = 1;
4513 *new_view = blame_view;
4517 if (s->selected > view->nlines)
4518 s->selected = s->ndisplayed - 1;
4531 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4536 static const struct got_error *
4537 cmd_tree(int argc, char *argv[])
4539 const struct got_error *error;
4540 struct got_repository *repo = NULL;
4541 struct got_reflist_head refs;
4542 char *repo_path = NULL;
4543 struct got_object_id *commit_id = NULL;
4544 char *commit_id_arg = NULL;
4545 struct got_commit_object *commit = NULL;
4546 struct got_tree_object *tree = NULL;
4548 struct tog_view *view;
4550 SIMPLEQ_INIT(&refs);
4553 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4558 while ((ch = getopt(argc, argv, "c:")) != -1) {
4561 commit_id_arg = optarg;
4573 struct got_worktree *worktree;
4574 char *cwd = getcwd(NULL, 0);
4576 return got_error_from_errno("getcwd");
4577 error = got_worktree_open(&worktree, cwd);
4578 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4583 strdup(got_worktree_get_repo_path(worktree));
4584 got_worktree_close(worktree);
4587 if (repo_path == NULL) {
4588 error = got_error_from_errno("strdup");
4591 } else if (argc == 1) {
4592 repo_path = realpath(argv[0], NULL);
4593 if (repo_path == NULL)
4594 return got_error_from_errno2("realpath", argv[0]);
4600 error = got_repo_open(&repo, repo_path, NULL);
4604 error = apply_unveil(got_repo_get_path(repo), NULL);
4608 if (commit_id_arg == NULL)
4609 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4611 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4613 if (error->code != GOT_ERR_NOT_REF)
4615 error = got_repo_match_object_id_prefix(&commit_id,
4616 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4622 error = got_object_open_as_commit(&commit, repo, commit_id);
4626 error = got_object_open_as_tree(&tree, repo,
4627 got_object_commit_get_tree_id(commit));
4631 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4635 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4637 error = got_error_from_errno("view_open");
4640 error = open_tree_view(view, tree, commit_id, &refs, repo);
4643 error = view_loop(view);
4648 got_object_commit_close(commit);
4650 got_object_tree_close(tree);
4652 got_repo_close(repo);
4653 got_ref_list_free(&refs);
4662 fprintf(stderr, "commands:");
4663 for (i = 0; i < nitems(tog_commands); i++) {
4664 struct tog_cmd *cmd = &tog_commands[i];
4665 fprintf(stderr, " %s", cmd->name);
4667 fputc('\n', stderr);
4673 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4681 make_argv(const char *arg0, const char *arg1)
4684 int argc = (arg1 == NULL ? 1 : 2);
4686 argv = calloc(argc, sizeof(char *));
4689 argv[0] = strdup(arg0);
4690 if (argv[0] == NULL)
4693 argv[1] = strdup(arg1);
4694 if (argv[1] == NULL)
4702 main(int argc, char *argv[])
4704 const struct got_error *error = NULL;
4705 struct tog_cmd *cmd = NULL;
4706 int ch, hflag = 0, Vflag = 0;
4707 char **cmd_argv = NULL;
4709 setlocale(LC_CTYPE, "");
4711 while ((ch = getopt(argc, argv, "hV")) != -1) {
4731 got_version_print_str();
4738 /* Build an argument vector which runs a default command. */
4739 cmd = &tog_commands[0];
4740 cmd_argv = make_argv(cmd->name, NULL);
4745 /* Did the user specific a command? */
4746 for (i = 0; i < nitems(tog_commands); i++) {
4747 if (strncmp(tog_commands[i].name, argv[0],
4748 strlen(argv[0])) == 0) {
4749 cmd = &tog_commands[i];
4755 fprintf(stderr, "%s: unknown command '%s'\n",
4756 getprogname(), argv[0]);
4765 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4769 if (error && error->code != GOT_ERR_CANCELLED)
4770 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);