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 got_ref_list_free(s->refs);
2146 err = got_ref_list(s->refs, s->repo, NULL,
2147 got_ref_cmp_by_name, NULL);
2153 err = open_log_view(lv, start_id, s->refs, s->repo,
2154 s->head_ref_name, in_repo_path, 0);
2170 static const struct got_error *
2171 apply_unveil(const char *repo_path, const char *worktree_path)
2173 const struct got_error *error;
2176 if (unveil("gmon.out", "rwc") != 0)
2177 return got_error_from_errno2("unveil", "gmon.out");
2179 if (repo_path && unveil(repo_path, "r") != 0)
2180 return got_error_from_errno2("unveil", repo_path);
2182 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2183 return got_error_from_errno2("unveil", worktree_path);
2185 if (unveil("/tmp", "rwc") != 0)
2186 return got_error_from_errno2("unveil", "/tmp");
2188 error = got_privsep_unveil_exec_helpers();
2192 if (unveil(NULL, NULL) != 0)
2193 return got_error_from_errno("unveil");
2203 halfdelay(1); /* Do fast refresh while initial view is loading. */
2206 intrflush(stdscr, FALSE);
2207 keypad(stdscr, TRUE);
2209 signal(SIGWINCH, tog_sigwinch);
2210 signal(SIGPIPE, tog_sigpipe);
2213 static const struct got_error *
2214 cmd_log(int argc, char *argv[])
2216 const struct got_error *error;
2217 struct got_repository *repo = NULL;
2218 struct got_worktree *worktree = NULL;
2219 struct got_reflist_head refs;
2220 struct got_object_id *start_id = NULL;
2221 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2222 char *start_commit = NULL, *head_ref_name = NULL;
2224 struct tog_view *view;
2226 SIMPLEQ_INIT(&refs);
2229 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2234 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2237 start_commit = optarg;
2240 repo_path = realpath(optarg, NULL);
2241 if (repo_path == NULL)
2242 err(1, "-r option");
2253 cwd = getcwd(NULL, 0);
2255 error = got_error_from_errno("getcwd");
2258 error = got_worktree_open(&worktree, cwd);
2259 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2266 error = got_error_from_errno("strdup");
2269 } else if (argc == 1) {
2271 error = got_worktree_resolve_path(&path, worktree,
2276 path = strdup(argv[0]);
2278 error = got_error_from_errno("strdup");
2285 if (repo_path == NULL) {
2288 got_worktree_get_repo_path(worktree));
2290 repo_path = strdup(cwd);
2292 if (repo_path == NULL) {
2293 error = got_error_from_errno("strdup");
2299 error = got_repo_open(&repo, repo_path, NULL);
2303 error = apply_unveil(got_repo_get_path(repo),
2304 worktree ? got_worktree_get_root_path(worktree) : NULL);
2308 if (start_commit == NULL)
2309 error = get_head_commit_id(&start_id, worktree ?
2310 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2313 error = get_head_commit_id(&start_id, start_commit, repo);
2315 if (error->code != GOT_ERR_NOT_REF)
2317 error = got_repo_match_object_id_prefix(&start_id,
2318 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2324 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2328 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2330 error = got_error_from_errno("view_open");
2334 head_ref_name = strdup(
2335 got_worktree_get_head_ref_name(worktree));
2336 if (head_ref_name == NULL) {
2337 error = got_error_from_errno("strdup");
2341 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2346 /* Release work tree lock. */
2347 got_worktree_close(worktree);
2350 error = view_loop(view);
2356 free(head_ref_name);
2358 got_repo_close(repo);
2360 got_worktree_close(worktree);
2361 got_ref_list_free(&refs);
2369 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2375 parse_next_line(FILE *f, size_t *len)
2380 const char delim[3] = { '\0', '\0', '\0'};
2382 line = fparseln(f, &linelen, &lineno, delim, 0);
2388 static const struct got_error *
2389 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2390 int *last_displayed_line, int *eof, int max_lines,
2393 const struct got_error *err;
2394 int nlines = 0, nprinted = 0;
2401 werase(view->window);
2404 err = format_line(&wline, &width, header, view->ncols, 0);
2409 if (view_needs_focus_indication(view))
2410 wstandout(view->window);
2411 waddwstr(view->window, wline);
2412 if (view_needs_focus_indication(view))
2413 wstandend(view->window);
2414 if (width <= view->ncols - 1)
2415 waddch(view->window, '\n');
2423 while (nprinted < max_lines) {
2424 line = parse_next_line(f, &len);
2429 if (++nlines < *first_displayed_line) {
2434 err = format_line(&wline, &width, line, view->ncols, 0);
2439 waddwstr(view->window, wline);
2440 if (width <= view->ncols - 1)
2441 waddch(view->window, '\n');
2442 if (++nprinted == 1)
2443 *first_displayed_line = nlines;
2448 *last_displayed_line = nlines;
2453 while (nprinted < view->nlines) {
2454 waddch(view->window, '\n');
2458 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2463 wstandout(view->window);
2464 waddwstr(view->window, wline);
2465 wstandend(view->window);
2472 get_datestr(time_t *time, char *datebuf)
2474 struct tm mytm, *tm;
2477 tm = gmtime_r(time, &mytm);
2480 s = asctime_r(tm, datebuf);
2483 p = strchr(s, '\n');
2489 static const struct got_error *
2490 write_commit_info(struct got_object_id *commit_id,
2491 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2493 const struct got_error *err = NULL;
2494 char datebuf[26], *datestr;
2495 struct got_commit_object *commit;
2496 char *id_str = NULL, *logmsg = NULL;
2497 time_t committer_time;
2498 const char *author, *committer;
2499 char *refs_str = NULL;
2502 err = build_refs_str(&refs_str, refs, commit_id, repo);
2507 err = got_object_open_as_commit(&commit, repo, commit_id);
2511 err = got_object_id_str(&id_str, commit_id);
2513 err = got_error_from_errno("got_object_id_str");
2517 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2518 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2519 err = got_error_from_errno("fprintf");
2522 if (fprintf(outfile, "from: %s\n",
2523 got_object_commit_get_author(commit)) < 0) {
2524 err = got_error_from_errno("fprintf");
2527 committer_time = got_object_commit_get_committer_time(commit);
2528 datestr = get_datestr(&committer_time, datebuf);
2529 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2530 err = got_error_from_errno("fprintf");
2533 author = got_object_commit_get_author(commit);
2534 committer = got_object_commit_get_committer(commit);
2535 if (strcmp(author, committer) != 0 &&
2536 fprintf(outfile, "via: %s\n", committer) < 0) {
2537 err = got_error_from_errno("fprintf");
2540 err = got_object_commit_get_logmsg(&logmsg, commit);
2543 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2544 err = got_error_from_errno("fprintf");
2551 got_object_commit_close(commit);
2555 static const struct got_error *
2556 create_diff(struct tog_diff_view_state *s)
2558 const struct got_error *err = NULL;
2564 err = got_error_from_errno("got_opentemp");
2567 if (s->f && fclose(s->f) != 0) {
2568 err = got_error_from_errno("fclose");
2574 err = got_object_get_type(&obj_type, s->repo, s->id1);
2576 err = got_object_get_type(&obj_type, s->repo, s->id2);
2581 case GOT_OBJ_TYPE_BLOB:
2582 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2583 s->diff_context, s->repo, f);
2585 case GOT_OBJ_TYPE_TREE:
2586 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2587 s->diff_context, s->repo, f);
2589 case GOT_OBJ_TYPE_COMMIT: {
2590 const struct got_object_id_queue *parent_ids;
2591 struct got_object_qid *pid;
2592 struct got_commit_object *commit2;
2594 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2597 /* Show commit info if we're diffing to a parent/root commit. */
2599 write_commit_info(s->id2, s->refs, s->repo, f);
2601 parent_ids = got_object_commit_get_parent_ids(commit2);
2602 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2603 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2604 write_commit_info(s->id2, s->refs,
2610 got_object_commit_close(commit2);
2612 err = got_diff_objects_as_commits(s->id1, s->id2,
2613 s->diff_context, s->repo, f);
2617 err = got_error(GOT_ERR_OBJ_TYPE);
2621 if (f && fflush(f) != 0 && err == NULL)
2622 err = got_error_from_errno("fflush");
2627 diff_view_indicate_progress(struct tog_view *view)
2629 mvwaddstr(view->window, 0, 0, "diffing...");
2634 static const struct got_error *
2635 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2636 struct got_object_id *id2, struct tog_view *log_view,
2637 struct got_reflist_head *refs, struct got_repository *repo)
2639 const struct got_error *err;
2641 if (id1 != NULL && id2 != NULL) {
2643 err = got_object_get_type(&type1, repo, id1);
2646 err = got_object_get_type(&type2, repo, id2);
2651 return got_error(GOT_ERR_OBJ_TYPE);
2655 view->state.diff.id1 = got_object_id_dup(id1);
2656 if (view->state.diff.id1 == NULL)
2657 return got_error_from_errno("got_object_id_dup");
2659 view->state.diff.id1 = NULL;
2661 view->state.diff.id2 = got_object_id_dup(id2);
2662 if (view->state.diff.id2 == NULL) {
2663 free(view->state.diff.id1);
2664 view->state.diff.id1 = NULL;
2665 return got_error_from_errno("got_object_id_dup");
2667 view->state.diff.f = NULL;
2668 view->state.diff.first_displayed_line = 1;
2669 view->state.diff.last_displayed_line = view->nlines;
2670 view->state.diff.diff_context = 3;
2671 view->state.diff.log_view = log_view;
2672 view->state.diff.repo = repo;
2673 view->state.diff.refs = refs;
2675 if (log_view && view_is_splitscreen(view))
2676 show_log_view(log_view); /* draw vborder */
2677 diff_view_indicate_progress(view);
2679 err = create_diff(&view->state.diff);
2681 free(view->state.diff.id1);
2682 view->state.diff.id1 = NULL;
2683 free(view->state.diff.id2);
2684 view->state.diff.id2 = NULL;
2688 view->show = show_diff_view;
2689 view->input = input_diff_view;
2690 view->close = close_diff_view;
2695 static const struct got_error *
2696 close_diff_view(struct tog_view *view)
2698 const struct got_error *err = NULL;
2700 free(view->state.diff.id1);
2701 view->state.diff.id1 = NULL;
2702 free(view->state.diff.id2);
2703 view->state.diff.id2 = NULL;
2704 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2705 err = got_error_from_errno("fclose");
2709 static const struct got_error *
2710 show_diff_view(struct tog_view *view)
2712 const struct got_error *err;
2713 struct tog_diff_view_state *s = &view->state.diff;
2714 char *id_str1 = NULL, *id_str2, *header;
2717 err = got_object_id_str(&id_str1, s->id1);
2721 err = got_object_id_str(&id_str2, s->id2);
2725 if (asprintf(&header, "diff %s %s",
2726 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2727 err = got_error_from_errno("asprintf");
2735 return draw_file(view, s->f, &s->first_displayed_line,
2736 &s->last_displayed_line, &s->eof, view->nlines,
2740 static const struct got_error *
2741 set_selected_commit(struct tog_diff_view_state *s,
2742 struct commit_queue_entry *entry)
2744 const struct got_error *err;
2745 const struct got_object_id_queue *parent_ids;
2746 struct got_commit_object *selected_commit;
2747 struct got_object_qid *pid;
2750 s->id2 = got_object_id_dup(entry->id);
2752 return got_error_from_errno("got_object_id_dup");
2754 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2757 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2759 pid = SIMPLEQ_FIRST(parent_ids);
2760 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2761 got_object_commit_close(selected_commit);
2765 static const struct got_error *
2766 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2767 struct tog_view **focus_view, struct tog_view *view, int ch)
2769 const struct got_error *err = NULL;
2770 struct tog_diff_view_state *s = &view->state.diff;
2771 struct tog_log_view_state *ls;
2772 struct commit_queue_entry *entry;
2778 if (s->first_displayed_line > 1)
2779 s->first_displayed_line--;
2783 if (s->first_displayed_line == 1)
2786 while (i++ < view->nlines - 1 &&
2787 s->first_displayed_line > 1)
2788 s->first_displayed_line--;
2793 s->first_displayed_line++;
2801 while (!s->eof && i++ < view->nlines - 1) {
2803 line = parse_next_line(s->f, NULL);
2804 s->first_displayed_line++;
2810 if (s->diff_context > 0) {
2812 diff_view_indicate_progress(view);
2813 err = create_diff(s);
2817 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2819 diff_view_indicate_progress(view);
2820 err = create_diff(s);
2825 if (s->log_view == NULL)
2827 ls = &s->log_view->state.log;
2828 entry = TAILQ_PREV(ls->selected_entry,
2829 commit_queue_head, entry);
2833 err = input_log_view(NULL, NULL, NULL, s->log_view,
2838 err = set_selected_commit(s, entry);
2842 s->first_displayed_line = 1;
2843 s->last_displayed_line = view->nlines;
2845 diff_view_indicate_progress(view);
2846 err = create_diff(s);
2850 if (s->log_view == NULL)
2852 ls = &s->log_view->state.log;
2854 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2855 ls->thread_args.commits_needed++;
2857 /* Display "loading..." in log view. */
2858 show_log_view(s->log_view);
2862 err = trigger_log_thread(1 /* load_all */,
2863 &ls->thread_args.commits_needed,
2864 &ls->thread_args.log_complete,
2865 &ls->thread_args.need_commits);
2869 err = input_log_view(NULL, NULL, NULL, s->log_view,
2874 entry = TAILQ_NEXT(ls->selected_entry, entry);
2878 err = set_selected_commit(s, entry);
2882 s->first_displayed_line = 1;
2883 s->last_displayed_line = view->nlines;
2885 diff_view_indicate_progress(view);
2886 err = create_diff(s);
2895 static const struct got_error *
2896 cmd_diff(int argc, char *argv[])
2898 const struct got_error *error = NULL;
2899 struct got_repository *repo = NULL;
2900 struct got_reflist_head refs;
2901 struct got_object_id *id1 = NULL, *id2 = NULL;
2902 char *repo_path = NULL;
2903 char *id_str1 = NULL, *id_str2 = NULL;
2905 struct tog_view *view;
2907 SIMPLEQ_INIT(&refs);
2910 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2915 while ((ch = getopt(argc, argv, "")) != -1) {
2927 usage_diff(); /* TODO show local worktree changes */
2928 } else if (argc == 2) {
2929 repo_path = getcwd(NULL, 0);
2930 if (repo_path == NULL)
2931 return got_error_from_errno("getcwd");
2934 } else if (argc == 3) {
2935 repo_path = realpath(argv[0], NULL);
2936 if (repo_path == NULL)
2937 return got_error_from_errno2("realpath", argv[0]);
2945 error = got_repo_open(&repo, repo_path, NULL);
2949 error = apply_unveil(got_repo_get_path(repo), NULL);
2953 error = got_repo_match_object_id_prefix(&id1, id_str1,
2954 GOT_OBJ_TYPE_ANY, repo);
2958 error = got_repo_match_object_id_prefix(&id2, id_str2,
2959 GOT_OBJ_TYPE_ANY, repo);
2963 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2967 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2969 error = got_error_from_errno("view_open");
2972 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2975 error = view_loop(view);
2979 got_repo_close(repo);
2980 got_ref_list_free(&refs);
2988 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2993 struct tog_blame_line {
2995 struct got_object_id *id;
2998 static const struct got_error *
2999 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3000 const char *path, struct tog_blame_line *lines, int nlines,
3001 int blame_complete, int selected_line, int *first_displayed_line,
3002 int *last_displayed_line, int *eof, int max_lines)
3004 const struct got_error *err;
3005 int lineno = 0, nprinted = 0;
3010 struct tog_blame_line *blame_line;
3011 struct got_object_id *prev_id = NULL;
3014 err = got_object_id_str(&id_str, id);
3019 werase(view->window);
3021 if (asprintf(&line, "commit %s", id_str) == -1) {
3022 err = got_error_from_errno("asprintf");
3027 err = format_line(&wline, &width, line, view->ncols, 0);
3032 if (view_needs_focus_indication(view))
3033 wstandout(view->window);
3034 waddwstr(view->window, wline);
3035 if (view_needs_focus_indication(view))
3036 wstandend(view->window);
3039 if (width < view->ncols - 1)
3040 waddch(view->window, '\n');
3042 if (asprintf(&line, "[%d/%d] %s%s",
3043 *first_displayed_line - 1 + selected_line, nlines,
3044 blame_complete ? "" : "annotating... ", path) == -1) {
3046 return got_error_from_errno("asprintf");
3049 err = format_line(&wline, &width, line, view->ncols, 0);
3054 waddwstr(view->window, wline);
3057 if (width < view->ncols - 1)
3058 waddch(view->window, '\n');
3061 while (nprinted < max_lines - 2) {
3062 line = parse_next_line(f, &len);
3067 if (++lineno < *first_displayed_line) {
3072 if (view->ncols <= 9) {
3074 wline = wcsdup(L"");
3076 err = got_error_from_errno("wcsdup");
3078 err = format_line(&wline, &width, line,
3079 view->ncols - 9, 9);
3087 if (view->focussed && nprinted == selected_line - 1)
3088 wstandout(view->window);
3091 blame_line = &lines[lineno - 1];
3092 if (blame_line->annotated && prev_id &&
3093 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3095 nprinted == selected_line - 1)) {
3096 waddstr(view->window, " ");
3097 } else if (blame_line->annotated) {
3099 err = got_object_id_str(&id_str, blame_line->id);
3105 wprintw(view->window, "%.8s", id_str);
3107 prev_id = blame_line->id;
3109 waddstr(view->window, "........");
3113 waddstr(view->window, "........");
3117 if (view->focussed && nprinted == selected_line - 1)
3118 wstandend(view->window);
3119 waddstr(view->window, " ");
3121 waddwstr(view->window, wline);
3122 if (width <= view->ncols - 1)
3123 waddch(view->window, '\n');
3124 if (++nprinted == 1)
3125 *first_displayed_line = lineno;
3130 *last_displayed_line = lineno;
3137 static const struct got_error *
3138 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3140 const struct got_error *err = NULL;
3141 struct tog_blame_cb_args *a = arg;
3142 struct tog_blame_line *line;
3145 if (nlines != a->nlines ||
3146 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3147 return got_error(GOT_ERR_RANGE);
3149 errcode = pthread_mutex_lock(&tog_mutex);
3151 return got_error_set_errno(errcode, "pthread_mutex_lock");
3153 if (*a->quit) { /* user has quit the blame view */
3154 err = got_error(GOT_ERR_ITER_COMPLETED);
3159 goto done; /* no change in this commit */
3161 line = &a->lines[lineno - 1];
3162 if (line->annotated)
3165 line->id = got_object_id_dup(id);
3166 if (line->id == NULL) {
3167 err = got_error_from_errno("got_object_id_dup");
3170 line->annotated = 1;
3172 errcode = pthread_mutex_unlock(&tog_mutex);
3174 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3179 blame_thread(void *arg)
3181 const struct got_error *err;
3182 struct tog_blame_thread_args *ta = arg;
3183 struct tog_blame_cb_args *a = ta->cb_args;
3186 err = got_blame(ta->path, a->commit_id, ta->repo,
3187 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3188 if (err && err->code == GOT_ERR_CANCELLED)
3191 errcode = pthread_mutex_lock(&tog_mutex);
3193 return (void *)got_error_set_errno(errcode,
3194 "pthread_mutex_lock");
3196 got_repo_close(ta->repo);
3200 errcode = pthread_mutex_unlock(&tog_mutex);
3201 if (errcode && err == NULL)
3202 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3207 static struct got_object_id *
3208 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3209 int first_displayed_line, int selected_line)
3211 struct tog_blame_line *line;
3216 line = &lines[first_displayed_line - 1 + selected_line - 1];
3217 if (!line->annotated)
3223 static const struct got_error *
3224 stop_blame(struct tog_blame *blame)
3226 const struct got_error *err = NULL;
3229 if (blame->thread) {
3231 errcode = pthread_mutex_unlock(&tog_mutex);
3233 return got_error_set_errno(errcode,
3234 "pthread_mutex_unlock");
3235 errcode = pthread_join(blame->thread, (void **)&err);
3237 return got_error_set_errno(errcode, "pthread_join");
3238 errcode = pthread_mutex_lock(&tog_mutex);
3240 return got_error_set_errno(errcode,
3241 "pthread_mutex_lock");
3242 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3244 blame->thread = NULL;
3246 if (blame->thread_args.repo) {
3247 got_repo_close(blame->thread_args.repo);
3248 blame->thread_args.repo = NULL;
3251 if (fclose(blame->f) != 0 && err == NULL)
3252 err = got_error_from_errno("fclose");
3256 for (i = 0; i < blame->nlines; i++)
3257 free(blame->lines[i].id);
3259 blame->lines = NULL;
3261 free(blame->cb_args.commit_id);
3262 blame->cb_args.commit_id = NULL;
3267 static const struct got_error *
3268 cancel_blame_view(void *arg)
3270 const struct got_error *err = NULL;
3274 errcode = pthread_mutex_lock(&tog_mutex);
3276 return got_error_set_errno(errcode,
3277 "pthread_mutex_unlock");
3280 err = got_error(GOT_ERR_CANCELLED);
3282 errcode = pthread_mutex_unlock(&tog_mutex);
3284 return got_error_set_errno(errcode,
3285 "pthread_mutex_lock");
3290 static const struct got_error *
3291 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3292 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3293 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3294 struct got_repository *repo)
3296 const struct got_error *err = NULL;
3297 struct got_blob_object *blob = NULL;
3298 struct got_repository *thread_repo = NULL;
3299 struct got_object_id *obj_id = NULL;
3302 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3306 return got_error(GOT_ERR_NO_OBJ);
3308 err = got_object_get_type(&obj_type, repo, obj_id);
3312 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3313 err = got_error(GOT_ERR_OBJ_TYPE);
3317 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3320 blame->f = got_opentemp();
3321 if (blame->f == NULL) {
3322 err = got_error_from_errno("got_opentemp");
3325 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3326 &blame->line_offsets, blame->f, blob);
3327 if (err || blame->nlines == 0)
3330 /* Don't include \n at EOF in the blame line count. */
3331 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3334 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3335 if (blame->lines == NULL) {
3336 err = got_error_from_errno("calloc");
3340 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3344 blame->cb_args.view = view;
3345 blame->cb_args.lines = blame->lines;
3346 blame->cb_args.nlines = blame->nlines;
3347 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3348 if (blame->cb_args.commit_id == NULL) {
3349 err = got_error_from_errno("got_object_id_dup");
3352 blame->cb_args.quit = done;
3354 blame->thread_args.path = path;
3355 blame->thread_args.repo = thread_repo;
3356 blame->thread_args.cb_args = &blame->cb_args;
3357 blame->thread_args.complete = blame_complete;
3358 blame->thread_args.cancel_cb = cancel_blame_view;
3359 blame->thread_args.cancel_arg = done;
3360 *blame_complete = 0;
3364 got_object_blob_close(blob);
3371 static const struct got_error *
3372 open_blame_view(struct tog_view *view, char *path,
3373 struct got_object_id *commit_id, struct got_reflist_head *refs,
3374 struct got_repository *repo)
3376 const struct got_error *err = NULL;
3377 struct tog_blame_view_state *s = &view->state.blame;
3379 SIMPLEQ_INIT(&s->blamed_commits);
3381 s->path = strdup(path);
3382 if (s->path == NULL)
3383 return got_error_from_errno("strdup");
3385 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3391 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3392 s->first_displayed_line = 1;
3393 s->last_displayed_line = view->nlines;
3394 s->selected_line = 1;
3395 s->blame_complete = 0;
3398 s->commit_id = commit_id;
3399 memset(&s->blame, 0, sizeof(s->blame));
3401 view->show = show_blame_view;
3402 view->input = input_blame_view;
3403 view->close = close_blame_view;
3404 view->search_start = search_start_blame_view;
3405 view->search_next = search_next_blame_view;
3407 return run_blame(&s->blame, view, &s->blame_complete,
3408 &s->first_displayed_line, &s->last_displayed_line,
3409 &s->selected_line, &s->done, &s->eof, s->path,
3410 s->blamed_commit->id, s->repo);
3413 static const struct got_error *
3414 close_blame_view(struct tog_view *view)
3416 const struct got_error *err = NULL;
3417 struct tog_blame_view_state *s = &view->state.blame;
3419 if (s->blame.thread)
3420 err = stop_blame(&s->blame);
3422 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3423 struct got_object_qid *blamed_commit;
3424 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3425 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3426 got_object_qid_free(blamed_commit);
3434 static const struct got_error *
3435 search_start_blame_view(struct tog_view *view)
3437 struct tog_blame_view_state *s = &view->state.blame;
3439 s->matched_line = 0;
3444 match_line(const char *line, regex_t *regex)
3446 regmatch_t regmatch;
3448 return regexec(regex, line, 1, ®match, 0) == 0;
3452 static const struct got_error *
3453 search_next_blame_view(struct tog_view *view)
3455 struct tog_blame_view_state *s = &view->state.blame;
3458 if (!view->searching) {
3459 view->search_next_done = 1;
3463 if (s->matched_line) {
3464 if (view->searching == TOG_SEARCH_FORWARD)
3465 lineno = s->matched_line + 1;
3467 lineno = s->matched_line - 1;
3469 if (view->searching == TOG_SEARCH_FORWARD)
3472 lineno = s->blame.nlines;
3480 if (lineno <= 0 || lineno > s->blame.nlines) {
3481 if (s->matched_line == 0) {
3482 view->search_next_done = 1;
3487 if (view->searching == TOG_SEARCH_FORWARD)
3490 lineno = s->blame.nlines;
3493 offset = s->blame.line_offsets[lineno - 1];
3494 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3496 return got_error_from_errno("fseeko");
3499 line = parse_next_line(s->blame.f, &len);
3500 if (line && match_line(line, &view->regex)) {
3501 view->search_next_done = 1;
3502 s->matched_line = lineno;
3507 if (view->searching == TOG_SEARCH_FORWARD)
3513 if (s->matched_line) {
3514 s->first_displayed_line = s->matched_line;
3515 s->selected_line = 1;
3521 static const struct got_error *
3522 show_blame_view(struct tog_view *view)
3524 const struct got_error *err = NULL;
3525 struct tog_blame_view_state *s = &view->state.blame;
3528 if (s->blame.thread == NULL) {
3529 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3530 &s->blame.thread_args);
3532 return got_error_set_errno(errcode, "pthread_create");
3534 halfdelay(1); /* fast refresh while annotating */
3537 if (s->blame_complete)
3538 halfdelay(10); /* disable fast refresh */
3540 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3541 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3542 s->selected_line, &s->first_displayed_line,
3543 &s->last_displayed_line, &s->eof, view->nlines);
3549 static const struct got_error *
3550 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3551 struct tog_view **focus_view, struct tog_view *view, int ch)
3553 const struct got_error *err = NULL, *thread_err = NULL;
3554 struct tog_view *diff_view;
3555 struct tog_blame_view_state *s = &view->state.blame;
3564 if (s->selected_line > 1)
3566 else if (s->selected_line == 1 &&
3567 s->first_displayed_line > 1)
3568 s->first_displayed_line--;
3571 if (s->first_displayed_line == 1) {
3572 s->selected_line = 1;
3575 if (s->first_displayed_line > view->nlines - 2)
3576 s->first_displayed_line -=
3579 s->first_displayed_line = 1;
3583 if (s->selected_line < view->nlines - 2 &&
3584 s->first_displayed_line +
3585 s->selected_line <= s->blame.nlines)
3587 else if (s->last_displayed_line <
3589 s->first_displayed_line++;
3593 struct got_object_id *id = NULL;
3594 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3595 s->first_displayed_line, s->selected_line);
3599 struct got_commit_object *commit;
3600 struct got_object_qid *pid;
3601 struct got_object_id *blob_id = NULL;
3603 err = got_object_open_as_commit(&commit,
3607 pid = SIMPLEQ_FIRST(
3608 got_object_commit_get_parent_ids(commit));
3610 got_object_commit_close(commit);
3613 /* Check if path history ends here. */
3614 err = got_object_id_by_path(&blob_id, s->repo,
3617 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3619 got_object_commit_close(commit);
3622 err = got_object_get_type(&obj_type, s->repo,
3625 /* Can't blame non-blob type objects. */
3626 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3627 got_object_commit_close(commit);
3630 err = got_object_qid_alloc(&s->blamed_commit,
3632 got_object_commit_close(commit);
3634 if (got_object_id_cmp(id,
3635 s->blamed_commit->id) == 0)
3637 err = got_object_qid_alloc(&s->blamed_commit,
3643 thread_err = stop_blame(&s->blame);
3647 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3648 s->blamed_commit, entry);
3649 err = run_blame(&s->blame, view, &s->blame_complete,
3650 &s->first_displayed_line, &s->last_displayed_line,
3651 &s->selected_line, &s->done, &s->eof,
3652 s->path, s->blamed_commit->id, s->repo);
3658 struct got_object_qid *first;
3659 first = SIMPLEQ_FIRST(&s->blamed_commits);
3660 if (!got_object_id_cmp(first->id, s->commit_id))
3663 thread_err = stop_blame(&s->blame);
3667 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3668 got_object_qid_free(s->blamed_commit);
3670 SIMPLEQ_FIRST(&s->blamed_commits);
3671 err = run_blame(&s->blame, view, &s->blame_complete,
3672 &s->first_displayed_line, &s->last_displayed_line,
3673 &s->selected_line, &s->done, &s->eof, s->path,
3674 s->blamed_commit->id, s->repo);
3681 struct got_object_id *id = NULL;
3682 struct got_object_qid *pid;
3683 struct got_commit_object *commit = NULL;
3684 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3685 s->first_displayed_line, s->selected_line);
3688 err = got_object_open_as_commit(&commit, s->repo, id);
3691 pid = SIMPLEQ_FIRST(
3692 got_object_commit_get_parent_ids(commit));
3693 if (view_is_parent_view(view))
3694 begin_x = view_split_begin_x(view->begin_x);
3695 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3696 if (diff_view == NULL) {
3697 got_object_commit_close(commit);
3698 err = got_error_from_errno("view_open");
3701 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3702 id, NULL, s->refs, s->repo);
3703 got_object_commit_close(commit);
3705 view_close(diff_view);
3708 if (view_is_parent_view(view)) {
3709 err = view_close_child(view);
3712 err = view_set_child(view, diff_view);
3714 view_close(diff_view);
3717 *focus_view = diff_view;
3718 view->child_focussed = 1;
3720 *new_view = diff_view;
3727 if (s->last_displayed_line >= s->blame.nlines &&
3728 s->selected_line >= MIN(s->blame.nlines,
3729 view->nlines - 2)) {
3732 if (s->last_displayed_line >= s->blame.nlines &&
3733 s->selected_line < view->nlines - 2) {
3734 s->selected_line = MIN(s->blame.nlines,
3738 if (s->last_displayed_line + view->nlines - 2
3740 s->first_displayed_line +=
3743 s->first_displayed_line =
3748 if (s->selected_line > view->nlines - 2) {
3749 s->selected_line = MIN(s->blame.nlines,
3756 return thread_err ? thread_err : err;
3759 static const struct got_error *
3760 cmd_blame(int argc, char *argv[])
3762 const struct got_error *error;
3763 struct got_repository *repo = NULL;
3764 struct got_reflist_head refs;
3765 struct got_worktree *worktree = NULL;
3766 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3767 struct got_object_id *commit_id = NULL;
3768 char *commit_id_str = NULL;
3770 struct tog_view *view;
3772 SIMPLEQ_INIT(&refs);
3775 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3783 commit_id_str = optarg;
3786 repo_path = realpath(optarg, NULL);
3787 if (repo_path == NULL)
3788 err(1, "-r option");
3804 cwd = getcwd(NULL, 0);
3806 error = got_error_from_errno("getcwd");
3809 if (repo_path == NULL) {
3810 error = got_worktree_open(&worktree, cwd);
3811 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3817 strdup(got_worktree_get_repo_path(worktree));
3818 if (repo_path == NULL)
3819 error = got_error_from_errno("strdup");
3823 repo_path = strdup(cwd);
3824 if (repo_path == NULL) {
3825 error = got_error_from_errno("strdup");
3833 error = got_repo_open(&repo, repo_path, NULL);
3837 error = apply_unveil(got_repo_get_path(repo), NULL);
3842 const char *prefix = got_worktree_get_path_prefix(worktree);
3843 char *p, *worktree_subdir = cwd +
3844 strlen(got_worktree_get_root_path(worktree));
3845 if (asprintf(&p, "%s%s%s%s%s",
3846 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3847 worktree_subdir, worktree_subdir[0] ? "/" : "",
3849 error = got_error_from_errno("asprintf");
3852 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3855 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3860 if (commit_id_str == NULL) {
3861 struct got_reference *head_ref;
3862 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3865 error = got_ref_resolve(&commit_id, repo, head_ref);
3866 got_ref_close(head_ref);
3868 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3870 if (error->code != GOT_ERR_NOT_REF)
3872 error = got_repo_match_object_id_prefix(&commit_id,
3873 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3879 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3883 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3885 error = got_error_from_errno("view_open");
3888 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3892 /* Release work tree lock. */
3893 got_worktree_close(worktree);
3896 error = view_loop(view);
3902 got_worktree_close(worktree);
3904 got_repo_close(repo);
3905 got_ref_list_free(&refs);
3909 static const struct got_error *
3910 draw_tree_entries(struct tog_view *view,
3911 struct got_tree_entry **first_displayed_entry,
3912 struct got_tree_entry **last_displayed_entry,
3913 struct got_tree_entry **selected_entry, int *ndisplayed,
3914 const char *label, int show_ids, const char *parent_path,
3915 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3917 const struct got_error *err = NULL;
3918 struct got_tree_entry *te;
3924 werase(view->window);
3929 err = format_line(&wline, &width, label, view->ncols, 0);
3932 if (view_needs_focus_indication(view))
3933 wstandout(view->window);
3934 waddwstr(view->window, wline);
3935 if (view_needs_focus_indication(view))
3936 wstandend(view->window);
3939 if (width < view->ncols - 1)
3940 waddch(view->window, '\n');
3943 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3946 waddwstr(view->window, wline);
3949 if (width < view->ncols - 1)
3950 waddch(view->window, '\n');
3953 waddch(view->window, '\n');
3957 te = SIMPLEQ_FIRST(&entries->head);
3958 if (*first_displayed_entry == NULL) {
3959 if (selected == 0) {
3961 wstandout(view->window);
3962 *selected_entry = NULL;
3964 waddstr(view->window, " ..\n"); /* parent directory */
3965 if (selected == 0 && view->focussed)
3966 wstandend(view->window);
3973 while (te != *first_displayed_entry)
3974 te = SIMPLEQ_NEXT(te, entry);
3978 char *line = NULL, *id_str = NULL;
3979 const char *modestr = "";
3982 err = got_object_id_str(&id_str, te->id);
3984 return got_error_from_errno(
3985 "got_object_id_str");
3987 if (got_object_tree_entry_is_submodule(te))
3989 else if (S_ISLNK(te->mode))
3991 else if (S_ISDIR(te->mode))
3993 else if (te->mode & S_IXUSR)
3995 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3996 te->name, modestr) == -1) {
3998 return got_error_from_errno("asprintf");
4001 err = format_line(&wline, &width, line, view->ncols, 0);
4006 if (n == selected) {
4008 wstandout(view->window);
4009 *selected_entry = te;
4011 waddwstr(view->window, wline);
4012 if (width < view->ncols - 1)
4013 waddch(view->window, '\n');
4014 if (n == selected && view->focussed)
4015 wstandend(view->window);
4021 *last_displayed_entry = te;
4024 te = SIMPLEQ_NEXT(te, entry);
4031 tree_scroll_up(struct tog_view *view,
4032 struct got_tree_entry **first_displayed_entry, int maxscroll,
4033 const struct got_tree_entries *entries, int isroot)
4035 struct got_tree_entry *te, *prev;
4038 if (*first_displayed_entry == NULL)
4041 te = SIMPLEQ_FIRST(&entries->head);
4042 if (*first_displayed_entry == te) {
4044 *first_displayed_entry = NULL;
4048 /* XXX this is stupid... switch to TAILQ? */
4049 for (i = 0; i < maxscroll; i++) {
4050 while (te != *first_displayed_entry) {
4052 te = SIMPLEQ_NEXT(te, entry);
4054 *first_displayed_entry = prev;
4055 te = SIMPLEQ_FIRST(&entries->head);
4057 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4058 *first_displayed_entry = NULL;
4062 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4063 struct got_tree_entry *last_displayed_entry,
4064 const struct got_tree_entries *entries)
4066 struct got_tree_entry *next, *last;
4069 if (*first_displayed_entry)
4070 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4072 next = SIMPLEQ_FIRST(&entries->head);
4073 last = last_displayed_entry;
4074 while (next && last && n++ < maxscroll) {
4075 last = SIMPLEQ_NEXT(last, entry);
4077 *first_displayed_entry = next;
4078 next = SIMPLEQ_NEXT(next, entry);
4084 static const struct got_error *
4085 tree_entry_path(char **path, struct tog_parent_trees *parents,
4086 struct got_tree_entry *te)
4088 const struct got_error *err = NULL;
4089 struct tog_parent_tree *pt;
4090 size_t len = 2; /* for leading slash and NUL */
4092 TAILQ_FOREACH(pt, parents, entry)
4093 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4095 len += strlen(te->name);
4097 *path = calloc(1, len);
4099 return got_error_from_errno("calloc");
4102 pt = TAILQ_LAST(parents, tog_parent_trees);
4104 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4105 err = got_error(GOT_ERR_NO_SPACE);
4108 if (strlcat(*path, "/", len) >= len) {
4109 err = got_error(GOT_ERR_NO_SPACE);
4112 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4115 if (strlcat(*path, te->name, len) >= len) {
4116 err = got_error(GOT_ERR_NO_SPACE);
4128 static const struct got_error *
4129 blame_tree_entry(struct tog_view **new_view, int begin_x,
4130 struct got_tree_entry *te, struct tog_parent_trees *parents,
4131 struct got_object_id *commit_id, struct got_reflist_head *refs,
4132 struct got_repository *repo)
4134 const struct got_error *err = NULL;
4136 struct tog_view *blame_view;
4140 err = tree_entry_path(&path, parents, te);
4144 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4145 if (blame_view == NULL) {
4146 err = got_error_from_errno("view_open");
4150 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4152 if (err->code == GOT_ERR_CANCELLED)
4154 view_close(blame_view);
4156 *new_view = blame_view;
4162 static const struct got_error *
4163 log_tree_entry(struct tog_view **new_view, int begin_x,
4164 struct got_tree_entry *te, struct tog_parent_trees *parents,
4165 struct got_object_id *commit_id, struct got_reflist_head *refs,
4166 struct got_repository *repo)
4168 struct tog_view *log_view;
4169 const struct got_error *err = NULL;
4174 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4175 if (log_view == NULL)
4176 return got_error_from_errno("view_open");
4178 err = tree_entry_path(&path, parents, te);
4182 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4184 view_close(log_view);
4186 *new_view = log_view;
4191 static const struct got_error *
4192 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4193 struct got_object_id *commit_id, struct got_reflist_head *refs,
4194 struct got_repository *repo)
4196 const struct got_error *err = NULL;
4197 char *commit_id_str = NULL;
4198 struct tog_tree_view_state *s = &view->state.tree;
4200 TAILQ_INIT(&s->parents);
4202 err = got_object_id_str(&commit_id_str, commit_id);
4206 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4207 err = got_error_from_errno("asprintf");
4211 s->root = s->tree = root;
4212 s->entries = got_object_tree_get_entries(root);
4213 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4214 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4215 s->commit_id = got_object_id_dup(commit_id);
4216 if (s->commit_id == NULL) {
4217 err = got_error_from_errno("got_object_id_dup");
4223 view->show = show_tree_view;
4224 view->input = input_tree_view;
4225 view->close = close_tree_view;
4226 view->search_start = search_start_tree_view;
4227 view->search_next = search_next_tree_view;
4229 free(commit_id_str);
4231 free(s->tree_label);
4232 s->tree_label = NULL;
4237 static const struct got_error *
4238 close_tree_view(struct tog_view *view)
4240 struct tog_tree_view_state *s = &view->state.tree;
4242 free(s->tree_label);
4243 s->tree_label = NULL;
4245 s->commit_id = NULL;
4246 while (!TAILQ_EMPTY(&s->parents)) {
4247 struct tog_parent_tree *parent;
4248 parent = TAILQ_FIRST(&s->parents);
4249 TAILQ_REMOVE(&s->parents, parent, entry);
4253 if (s->tree != s->root)
4254 got_object_tree_close(s->tree);
4255 got_object_tree_close(s->root);
4260 static const struct got_error *
4261 search_start_tree_view(struct tog_view *view)
4263 struct tog_tree_view_state *s = &view->state.tree;
4265 s->matched_entry = NULL;
4270 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4272 regmatch_t regmatch;
4274 return regexec(regex, te->name, 1, ®match, 0) == 0;
4277 static const struct got_error *
4278 search_next_tree_view(struct tog_view *view)
4280 struct tog_tree_view_state *s = &view->state.tree;
4281 struct got_tree_entry *entry = NULL, *te;
4283 if (!view->searching) {
4284 view->search_next_done = 1;
4288 if (s->matched_entry) {
4289 if (view->searching == TOG_SEARCH_FORWARD) {
4290 if (s->selected_entry)
4291 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4293 entry = SIMPLEQ_FIRST(&s->entries->head);
4296 if (s->selected_entry == NULL) {
4297 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4300 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4302 if (SIMPLEQ_NEXT(te, entry) ==
4309 if (view->searching == TOG_SEARCH_FORWARD)
4310 entry = SIMPLEQ_FIRST(&s->entries->head);
4312 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4318 if (entry == NULL) {
4319 if (s->matched_entry == NULL) {
4320 view->search_next_done = 1;
4323 if (view->searching == TOG_SEARCH_FORWARD)
4324 entry = SIMPLEQ_FIRST(&s->entries->head);
4326 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4331 if (match_tree_entry(entry, &view->regex)) {
4332 view->search_next_done = 1;
4333 s->matched_entry = entry;
4337 if (view->searching == TOG_SEARCH_FORWARD)
4338 entry = SIMPLEQ_NEXT(entry, entry);
4340 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4343 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4344 if (SIMPLEQ_NEXT(te, entry) == entry) {
4353 if (s->matched_entry) {
4354 s->first_displayed_entry = s->matched_entry;
4361 static const struct got_error *
4362 show_tree_view(struct tog_view *view)
4364 const struct got_error *err = NULL;
4365 struct tog_tree_view_state *s = &view->state.tree;
4368 err = tree_entry_path(&parent_path, &s->parents, NULL);
4372 err = draw_tree_entries(view, &s->first_displayed_entry,
4373 &s->last_displayed_entry, &s->selected_entry,
4374 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4375 s->entries, s->selected, view->nlines, s->tree == s->root);
4382 static const struct got_error *
4383 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4384 struct tog_view **focus_view, struct tog_view *view, int ch)
4386 const struct got_error *err = NULL;
4387 struct tog_tree_view_state *s = &view->state.tree;
4388 struct tog_view *log_view;
4389 int begin_x = 0, nscrolled;
4393 s->show_ids = !s->show_ids;
4396 if (!s->selected_entry)
4398 if (view_is_parent_view(view))
4399 begin_x = view_split_begin_x(view->begin_x);
4400 err = log_tree_entry(&log_view, begin_x,
4401 s->selected_entry, &s->parents,
4402 s->commit_id, s->refs, s->repo);
4403 if (view_is_parent_view(view)) {
4404 err = view_close_child(view);
4407 err = view_set_child(view, log_view);
4409 view_close(log_view);
4412 *focus_view = log_view;
4413 view->child_focussed = 1;
4415 *new_view = log_view;
4419 if (s->selected > 0) {
4421 if (s->selected == 0)
4424 if (s->selected > 0)
4426 tree_scroll_up(view, &s->first_displayed_entry, 1,
4427 s->entries, s->tree == s->root);
4430 tree_scroll_up(view, &s->first_displayed_entry,
4431 MAX(0, view->nlines - 4 - s->selected), s->entries,
4432 s->tree == s->root);
4434 if (SIMPLEQ_FIRST(&s->entries->head) ==
4435 s->first_displayed_entry && s->tree != s->root)
4436 s->first_displayed_entry = NULL;
4440 if (s->selected < s->ndisplayed - 1) {
4444 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4445 /* can't scroll any further */
4447 tree_scroll_down(&s->first_displayed_entry, 1,
4448 s->last_displayed_entry, s->entries);
4451 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4453 /* can't scroll any further; move cursor down */
4454 if (s->selected < s->ndisplayed - 1)
4455 s->selected = s->ndisplayed - 1;
4458 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4459 view->nlines, s->last_displayed_entry, s->entries);
4460 if (nscrolled < view->nlines) {
4462 struct got_tree_entry *te;
4463 te = s->first_displayed_entry;
4466 te = SIMPLEQ_NEXT(te, entry);
4468 s->selected = ndisplayed - 1;
4474 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4475 struct tog_parent_tree *parent;
4476 /* user selected '..' */
4477 if (s->tree == s->root)
4479 parent = TAILQ_FIRST(&s->parents);
4480 TAILQ_REMOVE(&s->parents, parent,
4482 got_object_tree_close(s->tree);
4483 s->tree = parent->tree;
4485 got_object_tree_get_entries(s->tree);
4486 s->first_displayed_entry =
4487 parent->first_displayed_entry;
4489 parent->selected_entry;
4490 s->selected = parent->selected;
4492 } else if (S_ISDIR(s->selected_entry->mode)) {
4493 struct got_tree_object *subtree;
4494 err = got_object_open_as_tree(&subtree,
4495 s->repo, s->selected_entry->id);
4498 err = tree_view_visit_subtree(subtree, s);
4500 got_object_tree_close(subtree);
4503 } else if (S_ISREG(s->selected_entry->mode)) {
4504 struct tog_view *blame_view;
4505 int begin_x = view_is_parent_view(view) ?
4506 view_split_begin_x(view->begin_x) : 0;
4508 err = blame_tree_entry(&blame_view, begin_x,
4509 s->selected_entry, &s->parents,
4510 s->commit_id, s->refs, s->repo);
4513 if (view_is_parent_view(view)) {
4514 err = view_close_child(view);
4517 err = view_set_child(view, blame_view);
4519 view_close(blame_view);
4522 *focus_view = blame_view;
4523 view->child_focussed = 1;
4525 *new_view = blame_view;
4529 if (s->selected > view->nlines)
4530 s->selected = s->ndisplayed - 1;
4543 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4548 static const struct got_error *
4549 cmd_tree(int argc, char *argv[])
4551 const struct got_error *error;
4552 struct got_repository *repo = NULL;
4553 struct got_reflist_head refs;
4554 char *repo_path = NULL;
4555 struct got_object_id *commit_id = NULL;
4556 char *commit_id_arg = NULL;
4557 struct got_commit_object *commit = NULL;
4558 struct got_tree_object *tree = NULL;
4560 struct tog_view *view;
4562 SIMPLEQ_INIT(&refs);
4565 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4570 while ((ch = getopt(argc, argv, "c:")) != -1) {
4573 commit_id_arg = optarg;
4585 struct got_worktree *worktree;
4586 char *cwd = getcwd(NULL, 0);
4588 return got_error_from_errno("getcwd");
4589 error = got_worktree_open(&worktree, cwd);
4590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4595 strdup(got_worktree_get_repo_path(worktree));
4596 got_worktree_close(worktree);
4599 if (repo_path == NULL) {
4600 error = got_error_from_errno("strdup");
4603 } else if (argc == 1) {
4604 repo_path = realpath(argv[0], NULL);
4605 if (repo_path == NULL)
4606 return got_error_from_errno2("realpath", argv[0]);
4612 error = got_repo_open(&repo, repo_path, NULL);
4616 error = apply_unveil(got_repo_get_path(repo), NULL);
4620 if (commit_id_arg == NULL)
4621 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4623 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4625 if (error->code != GOT_ERR_NOT_REF)
4627 error = got_repo_match_object_id_prefix(&commit_id,
4628 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4634 error = got_object_open_as_commit(&commit, repo, commit_id);
4638 error = got_object_open_as_tree(&tree, repo,
4639 got_object_commit_get_tree_id(commit));
4643 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4647 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4649 error = got_error_from_errno("view_open");
4652 error = open_tree_view(view, tree, commit_id, &refs, repo);
4655 error = view_loop(view);
4660 got_object_commit_close(commit);
4662 got_object_tree_close(tree);
4664 got_repo_close(repo);
4665 got_ref_list_free(&refs);
4674 fprintf(stderr, "commands:");
4675 for (i = 0; i < nitems(tog_commands); i++) {
4676 struct tog_cmd *cmd = &tog_commands[i];
4677 fprintf(stderr, " %s", cmd->name);
4679 fputc('\n', stderr);
4685 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4693 make_argv(const char *arg0, const char *arg1)
4696 int argc = (arg1 == NULL ? 1 : 2);
4698 argv = calloc(argc, sizeof(char *));
4701 argv[0] = strdup(arg0);
4702 if (argv[0] == NULL)
4705 argv[1] = strdup(arg1);
4706 if (argv[1] == NULL)
4714 main(int argc, char *argv[])
4716 const struct got_error *error = NULL;
4717 struct tog_cmd *cmd = NULL;
4718 int ch, hflag = 0, Vflag = 0;
4719 char **cmd_argv = NULL;
4721 setlocale(LC_CTYPE, "");
4723 while ((ch = getopt(argc, argv, "hV")) != -1) {
4743 got_version_print_str();
4750 /* Build an argument vector which runs a default command. */
4751 cmd = &tog_commands[0];
4752 cmd_argv = make_argv(cmd->name, NULL);
4757 /* Did the user specific a command? */
4758 for (i = 0; i < nitems(tog_commands); i++) {
4759 if (strncmp(tog_commands[i].name, argv[0],
4760 strlen(argv[0])) == 0) {
4761 cmd = &tog_commands[i];
4767 fprintf(stderr, "%s: unknown command '%s'\n",
4768 getprogname(), argv[0]);
4777 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4781 if (error && error->code != GOT_ERR_CANCELLED)
4782 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);