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);
2034 if (first == s->first_displayed_entry &&
2035 s->selected < MIN(view->nlines - 2,
2036 s->commits.ncommits - 1)) {
2037 /* can't scroll further down */
2038 s->selected = MIN(view->nlines - 2,
2039 s->commits.ncommits - 1);
2045 if (s->selected > view->nlines - 2)
2046 s->selected = view->nlines - 2;
2047 if (s->selected > s->commits.ncommits - 1)
2048 s->selected = s->commits.ncommits - 1;
2053 if (s->selected_entry == NULL)
2055 if (view_is_parent_view(view))
2056 begin_x = view_split_begin_x(view->begin_x);
2057 err = open_diff_view_for_commit(&diff_view, begin_x,
2058 s->selected_entry->commit, s->selected_entry->id,
2059 view, s->refs, s->repo);
2062 if (view_is_parent_view(view)) {
2063 err = view_close_child(view);
2066 err = view_set_child(view, diff_view);
2068 view_close(diff_view);
2071 *focus_view = diff_view;
2072 view->child_focussed = 1;
2074 *new_view = diff_view;
2077 if (s->selected_entry == NULL)
2079 if (view_is_parent_view(view))
2080 begin_x = view_split_begin_x(view->begin_x);
2081 err = browse_commit_tree(&tree_view, begin_x,
2082 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2085 if (view_is_parent_view(view)) {
2086 err = view_close_child(view);
2089 err = view_set_child(view, tree_view);
2091 view_close(tree_view);
2094 *focus_view = tree_view;
2095 view->child_focussed = 1;
2097 *new_view = tree_view;
2100 if (strcmp(s->in_repo_path, "/") == 0)
2102 parent_path = dirname(s->in_repo_path);
2103 if (parent_path && strcmp(parent_path, ".") != 0) {
2104 err = stop_log_thread(s);
2107 lv = view_open(view->nlines, view->ncols,
2108 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2110 return got_error_from_errno(
2112 err = open_log_view(lv, s->start_id, s->refs,
2113 s->repo, s->head_ref_name, parent_path, 0);
2116 if (view_is_parent_view(view))
2119 view_set_child(view->parent, lv);
2126 err = stop_log_thread(s);
2129 lv = view_open(view->nlines, view->ncols,
2130 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2132 return got_error_from_errno("view_open");
2133 err = get_head_commit_id(&start_id, s->head_ref_name ?
2134 s->head_ref_name : GOT_REF_HEAD, s->repo);
2139 in_repo_path = strdup(s->in_repo_path);
2140 if (in_repo_path == NULL) {
2143 return got_error_from_errno("strdup");
2145 err = open_log_view(lv, start_id, s->refs, s->repo,
2146 s->head_ref_name, in_repo_path, 0);
2162 static const struct got_error *
2163 apply_unveil(const char *repo_path, const char *worktree_path)
2165 const struct got_error *error;
2168 if (unveil("gmon.out", "rwc") != 0)
2169 return got_error_from_errno2("unveil", "gmon.out");
2171 if (repo_path && unveil(repo_path, "r") != 0)
2172 return got_error_from_errno2("unveil", repo_path);
2174 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2175 return got_error_from_errno2("unveil", worktree_path);
2177 if (unveil("/tmp", "rwc") != 0)
2178 return got_error_from_errno2("unveil", "/tmp");
2180 error = got_privsep_unveil_exec_helpers();
2184 if (unveil(NULL, NULL) != 0)
2185 return got_error_from_errno("unveil");
2195 halfdelay(1); /* Do fast refresh while initial view is loading. */
2198 intrflush(stdscr, FALSE);
2199 keypad(stdscr, TRUE);
2201 signal(SIGWINCH, tog_sigwinch);
2202 signal(SIGPIPE, tog_sigpipe);
2205 static const struct got_error *
2206 cmd_log(int argc, char *argv[])
2208 const struct got_error *error;
2209 struct got_repository *repo = NULL;
2210 struct got_worktree *worktree = NULL;
2211 struct got_reflist_head refs;
2212 struct got_object_id *start_id = NULL;
2213 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2214 char *start_commit = NULL, *head_ref_name = NULL;
2216 struct tog_view *view;
2218 SIMPLEQ_INIT(&refs);
2221 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2226 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2229 start_commit = optarg;
2232 repo_path = realpath(optarg, NULL);
2233 if (repo_path == NULL)
2234 err(1, "-r option");
2245 cwd = getcwd(NULL, 0);
2247 error = got_error_from_errno("getcwd");
2250 error = got_worktree_open(&worktree, cwd);
2251 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2258 error = got_error_from_errno("strdup");
2261 } else if (argc == 1) {
2263 error = got_worktree_resolve_path(&path, worktree,
2268 path = strdup(argv[0]);
2270 error = got_error_from_errno("strdup");
2277 if (repo_path == NULL) {
2280 got_worktree_get_repo_path(worktree));
2282 repo_path = strdup(cwd);
2284 if (repo_path == NULL) {
2285 error = got_error_from_errno("strdup");
2291 error = got_repo_open(&repo, repo_path, NULL);
2295 error = apply_unveil(got_repo_get_path(repo),
2296 worktree ? got_worktree_get_root_path(worktree) : NULL);
2300 if (start_commit == NULL)
2301 error = get_head_commit_id(&start_id, worktree ?
2302 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2305 error = get_head_commit_id(&start_id, start_commit, repo);
2307 if (error->code != GOT_ERR_NOT_REF)
2309 error = got_repo_match_object_id_prefix(&start_id,
2310 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2316 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2320 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2322 error = got_error_from_errno("view_open");
2326 head_ref_name = strdup(
2327 got_worktree_get_head_ref_name(worktree));
2328 if (head_ref_name == NULL) {
2329 error = got_error_from_errno("strdup");
2333 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2338 /* Release work tree lock. */
2339 got_worktree_close(worktree);
2342 error = view_loop(view);
2348 free(head_ref_name);
2350 got_repo_close(repo);
2352 got_worktree_close(worktree);
2353 got_ref_list_free(&refs);
2361 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2367 parse_next_line(FILE *f, size_t *len)
2372 const char delim[3] = { '\0', '\0', '\0'};
2374 line = fparseln(f, &linelen, &lineno, delim, 0);
2380 static const struct got_error *
2381 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2382 int *last_displayed_line, int *eof, int max_lines,
2385 const struct got_error *err;
2386 int nlines = 0, nprinted = 0;
2393 werase(view->window);
2396 err = format_line(&wline, &width, header, view->ncols, 0);
2401 if (view_needs_focus_indication(view))
2402 wstandout(view->window);
2403 waddwstr(view->window, wline);
2404 if (view_needs_focus_indication(view))
2405 wstandend(view->window);
2406 if (width <= view->ncols - 1)
2407 waddch(view->window, '\n');
2415 while (nprinted < max_lines) {
2416 line = parse_next_line(f, &len);
2421 if (++nlines < *first_displayed_line) {
2426 err = format_line(&wline, &width, line, view->ncols, 0);
2431 waddwstr(view->window, wline);
2432 if (width <= view->ncols - 1)
2433 waddch(view->window, '\n');
2434 if (++nprinted == 1)
2435 *first_displayed_line = nlines;
2440 *last_displayed_line = nlines;
2445 while (nprinted < view->nlines) {
2446 waddch(view->window, '\n');
2450 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2455 wstandout(view->window);
2456 waddwstr(view->window, wline);
2457 wstandend(view->window);
2464 get_datestr(time_t *time, char *datebuf)
2466 struct tm mytm, *tm;
2469 tm = gmtime_r(time, &mytm);
2472 s = asctime_r(tm, datebuf);
2475 p = strchr(s, '\n');
2481 static const struct got_error *
2482 write_commit_info(struct got_object_id *commit_id,
2483 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2485 const struct got_error *err = NULL;
2486 char datebuf[26], *datestr;
2487 struct got_commit_object *commit;
2488 char *id_str = NULL, *logmsg = NULL;
2489 time_t committer_time;
2490 const char *author, *committer;
2491 char *refs_str = NULL;
2494 err = build_refs_str(&refs_str, refs, commit_id, repo);
2499 err = got_object_open_as_commit(&commit, repo, commit_id);
2503 err = got_object_id_str(&id_str, commit_id);
2505 err = got_error_from_errno("got_object_id_str");
2509 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2510 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2511 err = got_error_from_errno("fprintf");
2514 if (fprintf(outfile, "from: %s\n",
2515 got_object_commit_get_author(commit)) < 0) {
2516 err = got_error_from_errno("fprintf");
2519 committer_time = got_object_commit_get_committer_time(commit);
2520 datestr = get_datestr(&committer_time, datebuf);
2521 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2522 err = got_error_from_errno("fprintf");
2525 author = got_object_commit_get_author(commit);
2526 committer = got_object_commit_get_committer(commit);
2527 if (strcmp(author, committer) != 0 &&
2528 fprintf(outfile, "via: %s\n", committer) < 0) {
2529 err = got_error_from_errno("fprintf");
2532 err = got_object_commit_get_logmsg(&logmsg, commit);
2535 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2536 err = got_error_from_errno("fprintf");
2543 got_object_commit_close(commit);
2547 static const struct got_error *
2548 create_diff(struct tog_diff_view_state *s)
2550 const struct got_error *err = NULL;
2556 err = got_error_from_errno("got_opentemp");
2559 if (s->f && fclose(s->f) != 0) {
2560 err = got_error_from_errno("fclose");
2566 err = got_object_get_type(&obj_type, s->repo, s->id1);
2568 err = got_object_get_type(&obj_type, s->repo, s->id2);
2573 case GOT_OBJ_TYPE_BLOB:
2574 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2575 s->diff_context, s->repo, f);
2577 case GOT_OBJ_TYPE_TREE:
2578 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2579 s->diff_context, s->repo, f);
2581 case GOT_OBJ_TYPE_COMMIT: {
2582 const struct got_object_id_queue *parent_ids;
2583 struct got_object_qid *pid;
2584 struct got_commit_object *commit2;
2586 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2589 /* Show commit info if we're diffing to a parent/root commit. */
2591 write_commit_info(s->id2, s->refs, s->repo, f);
2593 parent_ids = got_object_commit_get_parent_ids(commit2);
2594 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2595 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2596 write_commit_info(s->id2, s->refs,
2602 got_object_commit_close(commit2);
2604 err = got_diff_objects_as_commits(s->id1, s->id2,
2605 s->diff_context, s->repo, f);
2609 err = got_error(GOT_ERR_OBJ_TYPE);
2613 if (f && fflush(f) != 0 && err == NULL)
2614 err = got_error_from_errno("fflush");
2619 diff_view_indicate_progress(struct tog_view *view)
2621 mvwaddstr(view->window, 0, 0, "diffing...");
2626 static const struct got_error *
2627 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2628 struct got_object_id *id2, struct tog_view *log_view,
2629 struct got_reflist_head *refs, struct got_repository *repo)
2631 const struct got_error *err;
2633 if (id1 != NULL && id2 != NULL) {
2635 err = got_object_get_type(&type1, repo, id1);
2638 err = got_object_get_type(&type2, repo, id2);
2643 return got_error(GOT_ERR_OBJ_TYPE);
2647 view->state.diff.id1 = got_object_id_dup(id1);
2648 if (view->state.diff.id1 == NULL)
2649 return got_error_from_errno("got_object_id_dup");
2651 view->state.diff.id1 = NULL;
2653 view->state.diff.id2 = got_object_id_dup(id2);
2654 if (view->state.diff.id2 == NULL) {
2655 free(view->state.diff.id1);
2656 view->state.diff.id1 = NULL;
2657 return got_error_from_errno("got_object_id_dup");
2659 view->state.diff.f = NULL;
2660 view->state.diff.first_displayed_line = 1;
2661 view->state.diff.last_displayed_line = view->nlines;
2662 view->state.diff.diff_context = 3;
2663 view->state.diff.log_view = log_view;
2664 view->state.diff.repo = repo;
2665 view->state.diff.refs = refs;
2667 if (log_view && view_is_splitscreen(view))
2668 show_log_view(log_view); /* draw vborder */
2669 diff_view_indicate_progress(view);
2671 err = create_diff(&view->state.diff);
2673 free(view->state.diff.id1);
2674 view->state.diff.id1 = NULL;
2675 free(view->state.diff.id2);
2676 view->state.diff.id2 = NULL;
2680 view->show = show_diff_view;
2681 view->input = input_diff_view;
2682 view->close = close_diff_view;
2687 static const struct got_error *
2688 close_diff_view(struct tog_view *view)
2690 const struct got_error *err = NULL;
2692 free(view->state.diff.id1);
2693 view->state.diff.id1 = NULL;
2694 free(view->state.diff.id2);
2695 view->state.diff.id2 = NULL;
2696 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2697 err = got_error_from_errno("fclose");
2701 static const struct got_error *
2702 show_diff_view(struct tog_view *view)
2704 const struct got_error *err;
2705 struct tog_diff_view_state *s = &view->state.diff;
2706 char *id_str1 = NULL, *id_str2, *header;
2709 err = got_object_id_str(&id_str1, s->id1);
2713 err = got_object_id_str(&id_str2, s->id2);
2717 if (asprintf(&header, "diff %s %s",
2718 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2719 err = got_error_from_errno("asprintf");
2727 return draw_file(view, s->f, &s->first_displayed_line,
2728 &s->last_displayed_line, &s->eof, view->nlines,
2732 static const struct got_error *
2733 set_selected_commit(struct tog_diff_view_state *s,
2734 struct commit_queue_entry *entry)
2736 const struct got_error *err;
2737 const struct got_object_id_queue *parent_ids;
2738 struct got_commit_object *selected_commit;
2739 struct got_object_qid *pid;
2742 s->id2 = got_object_id_dup(entry->id);
2744 return got_error_from_errno("got_object_id_dup");
2746 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2749 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2751 pid = SIMPLEQ_FIRST(parent_ids);
2752 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2753 got_object_commit_close(selected_commit);
2757 static const struct got_error *
2758 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2759 struct tog_view **focus_view, struct tog_view *view, int ch)
2761 const struct got_error *err = NULL;
2762 struct tog_diff_view_state *s = &view->state.diff;
2763 struct tog_log_view_state *ls;
2764 struct commit_queue_entry *entry;
2770 if (s->first_displayed_line > 1)
2771 s->first_displayed_line--;
2775 if (s->first_displayed_line == 1)
2778 while (i++ < view->nlines - 1 &&
2779 s->first_displayed_line > 1)
2780 s->first_displayed_line--;
2785 s->first_displayed_line++;
2793 while (!s->eof && i++ < view->nlines - 1) {
2795 line = parse_next_line(s->f, NULL);
2796 s->first_displayed_line++;
2802 if (s->diff_context > 0) {
2804 diff_view_indicate_progress(view);
2805 err = create_diff(s);
2809 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2811 diff_view_indicate_progress(view);
2812 err = create_diff(s);
2817 if (s->log_view == NULL)
2819 ls = &s->log_view->state.log;
2820 entry = TAILQ_PREV(ls->selected_entry,
2821 commit_queue_head, entry);
2825 err = input_log_view(NULL, NULL, NULL, s->log_view,
2830 err = set_selected_commit(s, entry);
2834 s->first_displayed_line = 1;
2835 s->last_displayed_line = view->nlines;
2837 diff_view_indicate_progress(view);
2838 err = create_diff(s);
2842 if (s->log_view == NULL)
2844 ls = &s->log_view->state.log;
2846 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2847 ls->thread_args.commits_needed++;
2849 /* Display "loading..." in log view. */
2850 show_log_view(s->log_view);
2854 err = trigger_log_thread(1 /* load_all */,
2855 &ls->thread_args.commits_needed,
2856 &ls->thread_args.log_complete,
2857 &ls->thread_args.need_commits);
2861 err = input_log_view(NULL, NULL, NULL, s->log_view,
2866 entry = TAILQ_NEXT(ls->selected_entry, entry);
2870 err = set_selected_commit(s, entry);
2874 s->first_displayed_line = 1;
2875 s->last_displayed_line = view->nlines;
2877 diff_view_indicate_progress(view);
2878 err = create_diff(s);
2887 static const struct got_error *
2888 cmd_diff(int argc, char *argv[])
2890 const struct got_error *error = NULL;
2891 struct got_repository *repo = NULL;
2892 struct got_reflist_head refs;
2893 struct got_object_id *id1 = NULL, *id2 = NULL;
2894 char *repo_path = NULL;
2895 char *id_str1 = NULL, *id_str2 = NULL;
2897 struct tog_view *view;
2899 SIMPLEQ_INIT(&refs);
2902 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2907 while ((ch = getopt(argc, argv, "")) != -1) {
2919 usage_diff(); /* TODO show local worktree changes */
2920 } else if (argc == 2) {
2921 repo_path = getcwd(NULL, 0);
2922 if (repo_path == NULL)
2923 return got_error_from_errno("getcwd");
2926 } else if (argc == 3) {
2927 repo_path = realpath(argv[0], NULL);
2928 if (repo_path == NULL)
2929 return got_error_from_errno2("realpath", argv[0]);
2937 error = got_repo_open(&repo, repo_path, NULL);
2941 error = apply_unveil(got_repo_get_path(repo), NULL);
2945 error = got_repo_match_object_id_prefix(&id1, id_str1,
2946 GOT_OBJ_TYPE_ANY, repo);
2950 error = got_repo_match_object_id_prefix(&id2, id_str2,
2951 GOT_OBJ_TYPE_ANY, repo);
2955 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2959 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2961 error = got_error_from_errno("view_open");
2964 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2967 error = view_loop(view);
2971 got_repo_close(repo);
2972 got_ref_list_free(&refs);
2980 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2985 struct tog_blame_line {
2987 struct got_object_id *id;
2990 static const struct got_error *
2991 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2992 const char *path, struct tog_blame_line *lines, int nlines,
2993 int blame_complete, int selected_line, int *first_displayed_line,
2994 int *last_displayed_line, int *eof, int max_lines)
2996 const struct got_error *err;
2997 int lineno = 0, nprinted = 0;
3002 struct tog_blame_line *blame_line;
3003 struct got_object_id *prev_id = NULL;
3006 err = got_object_id_str(&id_str, id);
3011 werase(view->window);
3013 if (asprintf(&line, "commit %s", id_str) == -1) {
3014 err = got_error_from_errno("asprintf");
3019 err = format_line(&wline, &width, line, view->ncols, 0);
3024 if (view_needs_focus_indication(view))
3025 wstandout(view->window);
3026 waddwstr(view->window, wline);
3027 if (view_needs_focus_indication(view))
3028 wstandend(view->window);
3031 if (width < view->ncols - 1)
3032 waddch(view->window, '\n');
3034 if (asprintf(&line, "[%d/%d] %s%s",
3035 *first_displayed_line - 1 + selected_line, nlines,
3036 blame_complete ? "" : "annotating... ", path) == -1) {
3038 return got_error_from_errno("asprintf");
3041 err = format_line(&wline, &width, line, view->ncols, 0);
3046 waddwstr(view->window, wline);
3049 if (width < view->ncols - 1)
3050 waddch(view->window, '\n');
3053 while (nprinted < max_lines - 2) {
3054 line = parse_next_line(f, &len);
3059 if (++lineno < *first_displayed_line) {
3064 if (view->ncols <= 9) {
3066 wline = wcsdup(L"");
3068 err = got_error_from_errno("wcsdup");
3070 err = format_line(&wline, &width, line,
3071 view->ncols - 9, 9);
3079 if (view->focussed && nprinted == selected_line - 1)
3080 wstandout(view->window);
3083 blame_line = &lines[lineno - 1];
3084 if (blame_line->annotated && prev_id &&
3085 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3087 nprinted == selected_line - 1)) {
3088 waddstr(view->window, " ");
3089 } else if (blame_line->annotated) {
3091 err = got_object_id_str(&id_str, blame_line->id);
3097 wprintw(view->window, "%.8s", id_str);
3099 prev_id = blame_line->id;
3101 waddstr(view->window, "........");
3105 waddstr(view->window, "........");
3109 if (view->focussed && nprinted == selected_line - 1)
3110 wstandend(view->window);
3111 waddstr(view->window, " ");
3113 waddwstr(view->window, wline);
3114 if (width <= view->ncols - 1)
3115 waddch(view->window, '\n');
3116 if (++nprinted == 1)
3117 *first_displayed_line = lineno;
3122 *last_displayed_line = lineno;
3129 static const struct got_error *
3130 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3132 const struct got_error *err = NULL;
3133 struct tog_blame_cb_args *a = arg;
3134 struct tog_blame_line *line;
3137 if (nlines != a->nlines ||
3138 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3139 return got_error(GOT_ERR_RANGE);
3141 errcode = pthread_mutex_lock(&tog_mutex);
3143 return got_error_set_errno(errcode, "pthread_mutex_lock");
3145 if (*a->quit) { /* user has quit the blame view */
3146 err = got_error(GOT_ERR_ITER_COMPLETED);
3151 goto done; /* no change in this commit */
3153 line = &a->lines[lineno - 1];
3154 if (line->annotated)
3157 line->id = got_object_id_dup(id);
3158 if (line->id == NULL) {
3159 err = got_error_from_errno("got_object_id_dup");
3162 line->annotated = 1;
3164 errcode = pthread_mutex_unlock(&tog_mutex);
3166 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3171 blame_thread(void *arg)
3173 const struct got_error *err;
3174 struct tog_blame_thread_args *ta = arg;
3175 struct tog_blame_cb_args *a = ta->cb_args;
3178 err = got_blame(ta->path, a->commit_id, ta->repo,
3179 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3180 if (err && err->code == GOT_ERR_CANCELLED)
3183 errcode = pthread_mutex_lock(&tog_mutex);
3185 return (void *)got_error_set_errno(errcode,
3186 "pthread_mutex_lock");
3188 got_repo_close(ta->repo);
3192 errcode = pthread_mutex_unlock(&tog_mutex);
3193 if (errcode && err == NULL)
3194 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3199 static struct got_object_id *
3200 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3201 int first_displayed_line, int selected_line)
3203 struct tog_blame_line *line;
3208 line = &lines[first_displayed_line - 1 + selected_line - 1];
3209 if (!line->annotated)
3215 static const struct got_error *
3216 stop_blame(struct tog_blame *blame)
3218 const struct got_error *err = NULL;
3221 if (blame->thread) {
3223 errcode = pthread_mutex_unlock(&tog_mutex);
3225 return got_error_set_errno(errcode,
3226 "pthread_mutex_unlock");
3227 errcode = pthread_join(blame->thread, (void **)&err);
3229 return got_error_set_errno(errcode, "pthread_join");
3230 errcode = pthread_mutex_lock(&tog_mutex);
3232 return got_error_set_errno(errcode,
3233 "pthread_mutex_lock");
3234 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3236 blame->thread = NULL;
3238 if (blame->thread_args.repo) {
3239 got_repo_close(blame->thread_args.repo);
3240 blame->thread_args.repo = NULL;
3243 if (fclose(blame->f) != 0 && err == NULL)
3244 err = got_error_from_errno("fclose");
3248 for (i = 0; i < blame->nlines; i++)
3249 free(blame->lines[i].id);
3251 blame->lines = NULL;
3253 free(blame->cb_args.commit_id);
3254 blame->cb_args.commit_id = NULL;
3259 static const struct got_error *
3260 cancel_blame_view(void *arg)
3262 const struct got_error *err = NULL;
3266 errcode = pthread_mutex_lock(&tog_mutex);
3268 return got_error_set_errno(errcode,
3269 "pthread_mutex_unlock");
3272 err = got_error(GOT_ERR_CANCELLED);
3274 errcode = pthread_mutex_unlock(&tog_mutex);
3276 return got_error_set_errno(errcode,
3277 "pthread_mutex_lock");
3282 static const struct got_error *
3283 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3284 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3285 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3286 struct got_repository *repo)
3288 const struct got_error *err = NULL;
3289 struct got_blob_object *blob = NULL;
3290 struct got_repository *thread_repo = NULL;
3291 struct got_object_id *obj_id = NULL;
3294 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3298 return got_error(GOT_ERR_NO_OBJ);
3300 err = got_object_get_type(&obj_type, repo, obj_id);
3304 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3305 err = got_error(GOT_ERR_OBJ_TYPE);
3309 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3312 blame->f = got_opentemp();
3313 if (blame->f == NULL) {
3314 err = got_error_from_errno("got_opentemp");
3317 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3318 &blame->line_offsets, blame->f, blob);
3319 if (err || blame->nlines == 0)
3322 /* Don't include \n at EOF in the blame line count. */
3323 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3326 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3327 if (blame->lines == NULL) {
3328 err = got_error_from_errno("calloc");
3332 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3336 blame->cb_args.view = view;
3337 blame->cb_args.lines = blame->lines;
3338 blame->cb_args.nlines = blame->nlines;
3339 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3340 if (blame->cb_args.commit_id == NULL) {
3341 err = got_error_from_errno("got_object_id_dup");
3344 blame->cb_args.quit = done;
3346 blame->thread_args.path = path;
3347 blame->thread_args.repo = thread_repo;
3348 blame->thread_args.cb_args = &blame->cb_args;
3349 blame->thread_args.complete = blame_complete;
3350 blame->thread_args.cancel_cb = cancel_blame_view;
3351 blame->thread_args.cancel_arg = done;
3352 *blame_complete = 0;
3356 got_object_blob_close(blob);
3363 static const struct got_error *
3364 open_blame_view(struct tog_view *view, char *path,
3365 struct got_object_id *commit_id, struct got_reflist_head *refs,
3366 struct got_repository *repo)
3368 const struct got_error *err = NULL;
3369 struct tog_blame_view_state *s = &view->state.blame;
3371 SIMPLEQ_INIT(&s->blamed_commits);
3373 s->path = strdup(path);
3374 if (s->path == NULL)
3375 return got_error_from_errno("strdup");
3377 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3383 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3384 s->first_displayed_line = 1;
3385 s->last_displayed_line = view->nlines;
3386 s->selected_line = 1;
3387 s->blame_complete = 0;
3390 s->commit_id = commit_id;
3391 memset(&s->blame, 0, sizeof(s->blame));
3393 view->show = show_blame_view;
3394 view->input = input_blame_view;
3395 view->close = close_blame_view;
3396 view->search_start = search_start_blame_view;
3397 view->search_next = search_next_blame_view;
3399 return run_blame(&s->blame, view, &s->blame_complete,
3400 &s->first_displayed_line, &s->last_displayed_line,
3401 &s->selected_line, &s->done, &s->eof, s->path,
3402 s->blamed_commit->id, s->repo);
3405 static const struct got_error *
3406 close_blame_view(struct tog_view *view)
3408 const struct got_error *err = NULL;
3409 struct tog_blame_view_state *s = &view->state.blame;
3411 if (s->blame.thread)
3412 err = stop_blame(&s->blame);
3414 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3415 struct got_object_qid *blamed_commit;
3416 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3417 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3418 got_object_qid_free(blamed_commit);
3426 static const struct got_error *
3427 search_start_blame_view(struct tog_view *view)
3429 struct tog_blame_view_state *s = &view->state.blame;
3431 s->matched_line = 0;
3436 match_line(const char *line, regex_t *regex)
3438 regmatch_t regmatch;
3440 return regexec(regex, line, 1, ®match, 0) == 0;
3444 static const struct got_error *
3445 search_next_blame_view(struct tog_view *view)
3447 struct tog_blame_view_state *s = &view->state.blame;
3450 if (!view->searching) {
3451 view->search_next_done = 1;
3455 if (s->matched_line) {
3456 if (view->searching == TOG_SEARCH_FORWARD)
3457 lineno = s->matched_line + 1;
3459 lineno = s->matched_line - 1;
3461 if (view->searching == TOG_SEARCH_FORWARD)
3464 lineno = s->blame.nlines;
3472 if (lineno <= 0 || lineno > s->blame.nlines) {
3473 if (s->matched_line == 0) {
3474 view->search_next_done = 1;
3479 if (view->searching == TOG_SEARCH_FORWARD)
3482 lineno = s->blame.nlines;
3485 offset = s->blame.line_offsets[lineno - 1];
3486 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3488 return got_error_from_errno("fseeko");
3491 line = parse_next_line(s->blame.f, &len);
3492 if (line && match_line(line, &view->regex)) {
3493 view->search_next_done = 1;
3494 s->matched_line = lineno;
3499 if (view->searching == TOG_SEARCH_FORWARD)
3505 if (s->matched_line) {
3506 s->first_displayed_line = s->matched_line;
3507 s->selected_line = 1;
3513 static const struct got_error *
3514 show_blame_view(struct tog_view *view)
3516 const struct got_error *err = NULL;
3517 struct tog_blame_view_state *s = &view->state.blame;
3520 if (s->blame.thread == NULL) {
3521 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3522 &s->blame.thread_args);
3524 return got_error_set_errno(errcode, "pthread_create");
3526 halfdelay(1); /* fast refresh while annotating */
3529 if (s->blame_complete)
3530 halfdelay(10); /* disable fast refresh */
3532 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3533 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3534 s->selected_line, &s->first_displayed_line,
3535 &s->last_displayed_line, &s->eof, view->nlines);
3541 static const struct got_error *
3542 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3543 struct tog_view **focus_view, struct tog_view *view, int ch)
3545 const struct got_error *err = NULL, *thread_err = NULL;
3546 struct tog_view *diff_view;
3547 struct tog_blame_view_state *s = &view->state.blame;
3556 if (s->selected_line > 1)
3558 else if (s->selected_line == 1 &&
3559 s->first_displayed_line > 1)
3560 s->first_displayed_line--;
3563 if (s->first_displayed_line == 1) {
3564 s->selected_line = 1;
3567 if (s->first_displayed_line > view->nlines - 2)
3568 s->first_displayed_line -=
3571 s->first_displayed_line = 1;
3575 if (s->selected_line < view->nlines - 2 &&
3576 s->first_displayed_line +
3577 s->selected_line <= s->blame.nlines)
3579 else if (s->last_displayed_line <
3581 s->first_displayed_line++;
3585 struct got_object_id *id = NULL;
3586 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3587 s->first_displayed_line, s->selected_line);
3591 struct got_commit_object *commit;
3592 struct got_object_qid *pid;
3593 struct got_object_id *blob_id = NULL;
3595 err = got_object_open_as_commit(&commit,
3599 pid = SIMPLEQ_FIRST(
3600 got_object_commit_get_parent_ids(commit));
3602 got_object_commit_close(commit);
3605 /* Check if path history ends here. */
3606 err = got_object_id_by_path(&blob_id, s->repo,
3609 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3611 got_object_commit_close(commit);
3614 err = got_object_get_type(&obj_type, s->repo,
3617 /* Can't blame non-blob type objects. */
3618 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3619 got_object_commit_close(commit);
3622 err = got_object_qid_alloc(&s->blamed_commit,
3624 got_object_commit_close(commit);
3626 if (got_object_id_cmp(id,
3627 s->blamed_commit->id) == 0)
3629 err = got_object_qid_alloc(&s->blamed_commit,
3635 thread_err = stop_blame(&s->blame);
3639 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3640 s->blamed_commit, entry);
3641 err = run_blame(&s->blame, view, &s->blame_complete,
3642 &s->first_displayed_line, &s->last_displayed_line,
3643 &s->selected_line, &s->done, &s->eof,
3644 s->path, s->blamed_commit->id, s->repo);
3650 struct got_object_qid *first;
3651 first = SIMPLEQ_FIRST(&s->blamed_commits);
3652 if (!got_object_id_cmp(first->id, s->commit_id))
3655 thread_err = stop_blame(&s->blame);
3659 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3660 got_object_qid_free(s->blamed_commit);
3662 SIMPLEQ_FIRST(&s->blamed_commits);
3663 err = run_blame(&s->blame, view, &s->blame_complete,
3664 &s->first_displayed_line, &s->last_displayed_line,
3665 &s->selected_line, &s->done, &s->eof, s->path,
3666 s->blamed_commit->id, s->repo);
3673 struct got_object_id *id = NULL;
3674 struct got_object_qid *pid;
3675 struct got_commit_object *commit = NULL;
3676 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3677 s->first_displayed_line, s->selected_line);
3680 err = got_object_open_as_commit(&commit, s->repo, id);
3683 pid = SIMPLEQ_FIRST(
3684 got_object_commit_get_parent_ids(commit));
3685 if (view_is_parent_view(view))
3686 begin_x = view_split_begin_x(view->begin_x);
3687 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3688 if (diff_view == NULL) {
3689 got_object_commit_close(commit);
3690 err = got_error_from_errno("view_open");
3693 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3694 id, NULL, s->refs, s->repo);
3695 got_object_commit_close(commit);
3697 view_close(diff_view);
3700 if (view_is_parent_view(view)) {
3701 err = view_close_child(view);
3704 err = view_set_child(view, diff_view);
3706 view_close(diff_view);
3709 *focus_view = diff_view;
3710 view->child_focussed = 1;
3712 *new_view = diff_view;
3719 if (s->last_displayed_line >= s->blame.nlines &&
3720 s->selected_line >= MIN(s->blame.nlines,
3721 view->nlines - 2)) {
3724 if (s->last_displayed_line >= s->blame.nlines &&
3725 s->selected_line < view->nlines - 2) {
3726 s->selected_line = MIN(s->blame.nlines,
3730 if (s->last_displayed_line + view->nlines - 2
3732 s->first_displayed_line +=
3735 s->first_displayed_line =
3740 if (s->selected_line > view->nlines - 2) {
3741 s->selected_line = MIN(s->blame.nlines,
3748 return thread_err ? thread_err : err;
3751 static const struct got_error *
3752 cmd_blame(int argc, char *argv[])
3754 const struct got_error *error;
3755 struct got_repository *repo = NULL;
3756 struct got_reflist_head refs;
3757 struct got_worktree *worktree = NULL;
3758 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3759 struct got_object_id *commit_id = NULL;
3760 char *commit_id_str = NULL;
3762 struct tog_view *view;
3764 SIMPLEQ_INIT(&refs);
3767 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3772 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3775 commit_id_str = optarg;
3778 repo_path = realpath(optarg, NULL);
3779 if (repo_path == NULL)
3780 err(1, "-r option");
3796 cwd = getcwd(NULL, 0);
3798 error = got_error_from_errno("getcwd");
3801 if (repo_path == NULL) {
3802 error = got_worktree_open(&worktree, cwd);
3803 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3809 strdup(got_worktree_get_repo_path(worktree));
3810 if (repo_path == NULL)
3811 error = got_error_from_errno("strdup");
3815 repo_path = strdup(cwd);
3816 if (repo_path == NULL) {
3817 error = got_error_from_errno("strdup");
3825 error = got_repo_open(&repo, repo_path, NULL);
3829 error = apply_unveil(got_repo_get_path(repo), NULL);
3834 const char *prefix = got_worktree_get_path_prefix(worktree);
3835 char *p, *worktree_subdir = cwd +
3836 strlen(got_worktree_get_root_path(worktree));
3837 if (asprintf(&p, "%s%s%s%s%s",
3838 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3839 worktree_subdir, worktree_subdir[0] ? "/" : "",
3841 error = got_error_from_errno("asprintf");
3844 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3847 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3852 if (commit_id_str == NULL) {
3853 struct got_reference *head_ref;
3854 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3857 error = got_ref_resolve(&commit_id, repo, head_ref);
3858 got_ref_close(head_ref);
3860 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3862 if (error->code != GOT_ERR_NOT_REF)
3864 error = got_repo_match_object_id_prefix(&commit_id,
3865 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3871 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3875 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3877 error = got_error_from_errno("view_open");
3880 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3884 /* Release work tree lock. */
3885 got_worktree_close(worktree);
3888 error = view_loop(view);
3894 got_worktree_close(worktree);
3896 got_repo_close(repo);
3897 got_ref_list_free(&refs);
3901 static const struct got_error *
3902 draw_tree_entries(struct tog_view *view,
3903 struct got_tree_entry **first_displayed_entry,
3904 struct got_tree_entry **last_displayed_entry,
3905 struct got_tree_entry **selected_entry, int *ndisplayed,
3906 const char *label, int show_ids, const char *parent_path,
3907 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3909 const struct got_error *err = NULL;
3910 struct got_tree_entry *te;
3916 werase(view->window);
3921 err = format_line(&wline, &width, label, view->ncols, 0);
3924 if (view_needs_focus_indication(view))
3925 wstandout(view->window);
3926 waddwstr(view->window, wline);
3927 if (view_needs_focus_indication(view))
3928 wstandend(view->window);
3931 if (width < view->ncols - 1)
3932 waddch(view->window, '\n');
3935 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3938 waddwstr(view->window, wline);
3941 if (width < view->ncols - 1)
3942 waddch(view->window, '\n');
3945 waddch(view->window, '\n');
3949 te = SIMPLEQ_FIRST(&entries->head);
3950 if (*first_displayed_entry == NULL) {
3951 if (selected == 0) {
3953 wstandout(view->window);
3954 *selected_entry = NULL;
3956 waddstr(view->window, " ..\n"); /* parent directory */
3957 if (selected == 0 && view->focussed)
3958 wstandend(view->window);
3965 while (te != *first_displayed_entry)
3966 te = SIMPLEQ_NEXT(te, entry);
3970 char *line = NULL, *id_str = NULL;
3971 const char *modestr = "";
3974 err = got_object_id_str(&id_str, te->id);
3976 return got_error_from_errno(
3977 "got_object_id_str");
3979 if (got_object_tree_entry_is_submodule(te))
3981 else if (S_ISLNK(te->mode))
3983 else if (S_ISDIR(te->mode))
3985 else if (te->mode & S_IXUSR)
3987 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3988 te->name, modestr) == -1) {
3990 return got_error_from_errno("asprintf");
3993 err = format_line(&wline, &width, line, view->ncols, 0);
3998 if (n == selected) {
4000 wstandout(view->window);
4001 *selected_entry = te;
4003 waddwstr(view->window, wline);
4004 if (width < view->ncols - 1)
4005 waddch(view->window, '\n');
4006 if (n == selected && view->focussed)
4007 wstandend(view->window);
4013 *last_displayed_entry = te;
4016 te = SIMPLEQ_NEXT(te, entry);
4023 tree_scroll_up(struct tog_view *view,
4024 struct got_tree_entry **first_displayed_entry, int maxscroll,
4025 const struct got_tree_entries *entries, int isroot)
4027 struct got_tree_entry *te, *prev;
4030 if (*first_displayed_entry == NULL)
4033 te = SIMPLEQ_FIRST(&entries->head);
4034 if (*first_displayed_entry == te) {
4036 *first_displayed_entry = NULL;
4040 /* XXX this is stupid... switch to TAILQ? */
4041 for (i = 0; i < maxscroll; i++) {
4042 while (te != *first_displayed_entry) {
4044 te = SIMPLEQ_NEXT(te, entry);
4046 *first_displayed_entry = prev;
4047 te = SIMPLEQ_FIRST(&entries->head);
4049 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4050 *first_displayed_entry = NULL;
4054 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4055 struct got_tree_entry *last_displayed_entry,
4056 const struct got_tree_entries *entries)
4058 struct got_tree_entry *next, *last;
4061 if (*first_displayed_entry)
4062 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4064 next = SIMPLEQ_FIRST(&entries->head);
4065 last = last_displayed_entry;
4066 while (next && last && n++ < maxscroll) {
4067 last = SIMPLEQ_NEXT(last, entry);
4069 *first_displayed_entry = next;
4070 next = SIMPLEQ_NEXT(next, entry);
4076 static const struct got_error *
4077 tree_entry_path(char **path, struct tog_parent_trees *parents,
4078 struct got_tree_entry *te)
4080 const struct got_error *err = NULL;
4081 struct tog_parent_tree *pt;
4082 size_t len = 2; /* for leading slash and NUL */
4084 TAILQ_FOREACH(pt, parents, entry)
4085 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4087 len += strlen(te->name);
4089 *path = calloc(1, len);
4091 return got_error_from_errno("calloc");
4094 pt = TAILQ_LAST(parents, tog_parent_trees);
4096 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4097 err = got_error(GOT_ERR_NO_SPACE);
4100 if (strlcat(*path, "/", len) >= len) {
4101 err = got_error(GOT_ERR_NO_SPACE);
4104 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4107 if (strlcat(*path, te->name, len) >= len) {
4108 err = got_error(GOT_ERR_NO_SPACE);
4120 static const struct got_error *
4121 blame_tree_entry(struct tog_view **new_view, int begin_x,
4122 struct got_tree_entry *te, struct tog_parent_trees *parents,
4123 struct got_object_id *commit_id, struct got_reflist_head *refs,
4124 struct got_repository *repo)
4126 const struct got_error *err = NULL;
4128 struct tog_view *blame_view;
4132 err = tree_entry_path(&path, parents, te);
4136 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4137 if (blame_view == NULL) {
4138 err = got_error_from_errno("view_open");
4142 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4144 if (err->code == GOT_ERR_CANCELLED)
4146 view_close(blame_view);
4148 *new_view = blame_view;
4154 static const struct got_error *
4155 log_tree_entry(struct tog_view **new_view, int begin_x,
4156 struct got_tree_entry *te, struct tog_parent_trees *parents,
4157 struct got_object_id *commit_id, struct got_reflist_head *refs,
4158 struct got_repository *repo)
4160 struct tog_view *log_view;
4161 const struct got_error *err = NULL;
4166 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4167 if (log_view == NULL)
4168 return got_error_from_errno("view_open");
4170 err = tree_entry_path(&path, parents, te);
4174 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4176 view_close(log_view);
4178 *new_view = log_view;
4183 static const struct got_error *
4184 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4185 struct got_object_id *commit_id, struct got_reflist_head *refs,
4186 struct got_repository *repo)
4188 const struct got_error *err = NULL;
4189 char *commit_id_str = NULL;
4190 struct tog_tree_view_state *s = &view->state.tree;
4192 TAILQ_INIT(&s->parents);
4194 err = got_object_id_str(&commit_id_str, commit_id);
4198 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4199 err = got_error_from_errno("asprintf");
4203 s->root = s->tree = root;
4204 s->entries = got_object_tree_get_entries(root);
4205 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4206 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4207 s->commit_id = got_object_id_dup(commit_id);
4208 if (s->commit_id == NULL) {
4209 err = got_error_from_errno("got_object_id_dup");
4215 view->show = show_tree_view;
4216 view->input = input_tree_view;
4217 view->close = close_tree_view;
4218 view->search_start = search_start_tree_view;
4219 view->search_next = search_next_tree_view;
4221 free(commit_id_str);
4223 free(s->tree_label);
4224 s->tree_label = NULL;
4229 static const struct got_error *
4230 close_tree_view(struct tog_view *view)
4232 struct tog_tree_view_state *s = &view->state.tree;
4234 free(s->tree_label);
4235 s->tree_label = NULL;
4237 s->commit_id = NULL;
4238 while (!TAILQ_EMPTY(&s->parents)) {
4239 struct tog_parent_tree *parent;
4240 parent = TAILQ_FIRST(&s->parents);
4241 TAILQ_REMOVE(&s->parents, parent, entry);
4245 if (s->tree != s->root)
4246 got_object_tree_close(s->tree);
4247 got_object_tree_close(s->root);
4252 static const struct got_error *
4253 search_start_tree_view(struct tog_view *view)
4255 struct tog_tree_view_state *s = &view->state.tree;
4257 s->matched_entry = NULL;
4262 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4264 regmatch_t regmatch;
4266 return regexec(regex, te->name, 1, ®match, 0) == 0;
4269 static const struct got_error *
4270 search_next_tree_view(struct tog_view *view)
4272 struct tog_tree_view_state *s = &view->state.tree;
4273 struct got_tree_entry *entry = NULL, *te;
4275 if (!view->searching) {
4276 view->search_next_done = 1;
4280 if (s->matched_entry) {
4281 if (view->searching == TOG_SEARCH_FORWARD) {
4282 if (s->selected_entry)
4283 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4285 entry = SIMPLEQ_FIRST(&s->entries->head);
4288 if (s->selected_entry == NULL) {
4289 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4292 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4294 if (SIMPLEQ_NEXT(te, entry) ==
4301 if (view->searching == TOG_SEARCH_FORWARD)
4302 entry = SIMPLEQ_FIRST(&s->entries->head);
4304 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4310 if (entry == NULL) {
4311 if (s->matched_entry == NULL) {
4312 view->search_next_done = 1;
4315 if (view->searching == TOG_SEARCH_FORWARD)
4316 entry = SIMPLEQ_FIRST(&s->entries->head);
4318 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4323 if (match_tree_entry(entry, &view->regex)) {
4324 view->search_next_done = 1;
4325 s->matched_entry = entry;
4329 if (view->searching == TOG_SEARCH_FORWARD)
4330 entry = SIMPLEQ_NEXT(entry, entry);
4332 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4335 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4336 if (SIMPLEQ_NEXT(te, entry) == entry) {
4345 if (s->matched_entry) {
4346 s->first_displayed_entry = s->matched_entry;
4353 static const struct got_error *
4354 show_tree_view(struct tog_view *view)
4356 const struct got_error *err = NULL;
4357 struct tog_tree_view_state *s = &view->state.tree;
4360 err = tree_entry_path(&parent_path, &s->parents, NULL);
4364 err = draw_tree_entries(view, &s->first_displayed_entry,
4365 &s->last_displayed_entry, &s->selected_entry,
4366 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4367 s->entries, s->selected, view->nlines, s->tree == s->root);
4374 static const struct got_error *
4375 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4376 struct tog_view **focus_view, struct tog_view *view, int ch)
4378 const struct got_error *err = NULL;
4379 struct tog_tree_view_state *s = &view->state.tree;
4380 struct tog_view *log_view;
4381 int begin_x = 0, nscrolled;
4385 s->show_ids = !s->show_ids;
4388 if (!s->selected_entry)
4390 if (view_is_parent_view(view))
4391 begin_x = view_split_begin_x(view->begin_x);
4392 err = log_tree_entry(&log_view, begin_x,
4393 s->selected_entry, &s->parents,
4394 s->commit_id, s->refs, s->repo);
4395 if (view_is_parent_view(view)) {
4396 err = view_close_child(view);
4399 err = view_set_child(view, log_view);
4401 view_close(log_view);
4404 *focus_view = log_view;
4405 view->child_focussed = 1;
4407 *new_view = log_view;
4411 if (s->selected > 0) {
4413 if (s->selected == 0)
4416 if (s->selected > 0)
4418 tree_scroll_up(view, &s->first_displayed_entry, 1,
4419 s->entries, s->tree == s->root);
4422 tree_scroll_up(view, &s->first_displayed_entry,
4423 MAX(0, view->nlines - 4 - s->selected), s->entries,
4424 s->tree == s->root);
4426 if (SIMPLEQ_FIRST(&s->entries->head) ==
4427 s->first_displayed_entry && s->tree != s->root)
4428 s->first_displayed_entry = NULL;
4432 if (s->selected < s->ndisplayed - 1) {
4436 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4437 /* can't scroll any further */
4439 tree_scroll_down(&s->first_displayed_entry, 1,
4440 s->last_displayed_entry, s->entries);
4443 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4445 /* can't scroll any further; move cursor down */
4446 if (s->selected < s->ndisplayed - 1)
4447 s->selected = s->ndisplayed - 1;
4450 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4451 view->nlines, s->last_displayed_entry, s->entries);
4452 if (nscrolled < view->nlines) {
4454 struct got_tree_entry *te;
4455 te = s->first_displayed_entry;
4458 te = SIMPLEQ_NEXT(te, entry);
4460 s->selected = ndisplayed - 1;
4466 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4467 struct tog_parent_tree *parent;
4468 /* user selected '..' */
4469 if (s->tree == s->root)
4471 parent = TAILQ_FIRST(&s->parents);
4472 TAILQ_REMOVE(&s->parents, parent,
4474 got_object_tree_close(s->tree);
4475 s->tree = parent->tree;
4477 got_object_tree_get_entries(s->tree);
4478 s->first_displayed_entry =
4479 parent->first_displayed_entry;
4481 parent->selected_entry;
4482 s->selected = parent->selected;
4484 } else if (S_ISDIR(s->selected_entry->mode)) {
4485 struct got_tree_object *subtree;
4486 err = got_object_open_as_tree(&subtree,
4487 s->repo, s->selected_entry->id);
4490 err = tree_view_visit_subtree(subtree, s);
4492 got_object_tree_close(subtree);
4495 } else if (S_ISREG(s->selected_entry->mode)) {
4496 struct tog_view *blame_view;
4497 int begin_x = view_is_parent_view(view) ?
4498 view_split_begin_x(view->begin_x) : 0;
4500 err = blame_tree_entry(&blame_view, begin_x,
4501 s->selected_entry, &s->parents,
4502 s->commit_id, s->refs, s->repo);
4505 if (view_is_parent_view(view)) {
4506 err = view_close_child(view);
4509 err = view_set_child(view, blame_view);
4511 view_close(blame_view);
4514 *focus_view = blame_view;
4515 view->child_focussed = 1;
4517 *new_view = blame_view;
4521 if (s->selected > view->nlines)
4522 s->selected = s->ndisplayed - 1;
4535 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4540 static const struct got_error *
4541 cmd_tree(int argc, char *argv[])
4543 const struct got_error *error;
4544 struct got_repository *repo = NULL;
4545 struct got_reflist_head refs;
4546 char *repo_path = NULL;
4547 struct got_object_id *commit_id = NULL;
4548 char *commit_id_arg = NULL;
4549 struct got_commit_object *commit = NULL;
4550 struct got_tree_object *tree = NULL;
4552 struct tog_view *view;
4554 SIMPLEQ_INIT(&refs);
4557 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4562 while ((ch = getopt(argc, argv, "c:")) != -1) {
4565 commit_id_arg = optarg;
4577 struct got_worktree *worktree;
4578 char *cwd = getcwd(NULL, 0);
4580 return got_error_from_errno("getcwd");
4581 error = got_worktree_open(&worktree, cwd);
4582 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4587 strdup(got_worktree_get_repo_path(worktree));
4588 got_worktree_close(worktree);
4591 if (repo_path == NULL) {
4592 error = got_error_from_errno("strdup");
4595 } else if (argc == 1) {
4596 repo_path = realpath(argv[0], NULL);
4597 if (repo_path == NULL)
4598 return got_error_from_errno2("realpath", argv[0]);
4604 error = got_repo_open(&repo, repo_path, NULL);
4608 error = apply_unveil(got_repo_get_path(repo), NULL);
4612 if (commit_id_arg == NULL)
4613 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4615 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4617 if (error->code != GOT_ERR_NOT_REF)
4619 error = got_repo_match_object_id_prefix(&commit_id,
4620 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4626 error = got_object_open_as_commit(&commit, repo, commit_id);
4630 error = got_object_open_as_tree(&tree, repo,
4631 got_object_commit_get_tree_id(commit));
4635 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4639 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4641 error = got_error_from_errno("view_open");
4644 error = open_tree_view(view, tree, commit_id, &refs, repo);
4647 error = view_loop(view);
4652 got_object_commit_close(commit);
4654 got_object_tree_close(tree);
4656 got_repo_close(repo);
4657 got_ref_list_free(&refs);
4666 fprintf(stderr, "commands:");
4667 for (i = 0; i < nitems(tog_commands); i++) {
4668 struct tog_cmd *cmd = &tog_commands[i];
4669 fprintf(stderr, " %s", cmd->name);
4671 fputc('\n', stderr);
4677 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4685 make_argv(const char *arg0, const char *arg1)
4688 int argc = (arg1 == NULL ? 1 : 2);
4690 argv = calloc(argc, sizeof(char *));
4693 argv[0] = strdup(arg0);
4694 if (argv[0] == NULL)
4697 argv[1] = strdup(arg1);
4698 if (argv[1] == NULL)
4706 main(int argc, char *argv[])
4708 const struct got_error *error = NULL;
4709 struct tog_cmd *cmd = NULL;
4710 int ch, hflag = 0, Vflag = 0;
4711 char **cmd_argv = NULL;
4713 setlocale(LC_CTYPE, "");
4715 while ((ch = getopt(argc, argv, "hV")) != -1) {
4735 got_version_print_str();
4742 /* Build an argument vector which runs a default command. */
4743 cmd = &tog_commands[0];
4744 cmd_argv = make_argv(cmd->name, NULL);
4749 /* Did the user specific a command? */
4750 for (i = 0; i < nitems(tog_commands); i++) {
4751 if (strncmp(tog_commands[i].name, argv[0],
4752 strlen(argv[0])) == 0) {
4753 cmd = &tog_commands[i];
4759 fprintf(stderr, "%s: unknown command '%s'\n",
4760 getprogname(), argv[0]);
4769 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4773 if (error && error->code != GOT_ERR_CANCELLED)
4774 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);