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)
932 const struct got_error *err = NULL;
934 wchar_t *wline = NULL;
941 err = mbs2ws(&wline, &wlen, line);
946 while (i < wlen && cols < wlimit) {
947 int width = wcwidth(wline[i]);
954 if (cols + width <= wlimit)
959 if (wline[i] == L'\t')
960 cols += TABSIZE - ((cols + 1) % TABSIZE);
964 err = got_error_from_errno("wcwidth");
979 static const struct got_error*
980 build_refs_str(char **refs_str, struct got_reflist_head *refs,
981 struct got_object_id *id, struct got_repository *repo)
983 static const struct got_error *err = NULL;
984 struct got_reflist_entry *re;
990 SIMPLEQ_FOREACH(re, refs, entry) {
991 struct got_tag_object *tag = NULL;
994 name = got_ref_get_name(re->ref);
995 if (strcmp(name, GOT_REF_HEAD) == 0)
997 if (strncmp(name, "refs/", 5) == 0)
999 if (strncmp(name, "got/", 4) == 0)
1001 if (strncmp(name, "heads/", 6) == 0)
1003 if (strncmp(name, "remotes/", 8) == 0)
1005 if (strncmp(name, "tags/", 5) == 0) {
1006 err = got_object_open_as_tag(&tag, repo, re->id);
1008 if (err->code != GOT_ERR_OBJ_TYPE)
1010 /* Ref points at something other than a tag. */
1015 cmp = got_object_id_cmp(tag ?
1016 got_object_tag_get_object_id(tag) : re->id, id);
1018 got_object_tag_close(tag);
1022 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1023 s ? ", " : "", name) == -1) {
1024 err = got_error_from_errno("asprintf");
1035 static const struct got_error *
1036 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1038 char *smallerthan, *at;
1040 smallerthan = strchr(author, '<');
1041 if (smallerthan && smallerthan[1] != '\0')
1042 author = smallerthan + 1;
1043 at = strchr(author, '@');
1046 return format_line(wauthor, author_width, author, limit);
1049 static const struct got_error *
1050 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1051 struct got_object_id *id, struct got_reflist_head *refs,
1052 int author_display_cols)
1054 const struct got_error *err = NULL;
1055 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1056 char *logmsg0 = NULL, *logmsg = NULL;
1057 char *author = NULL;
1058 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1059 int author_width, logmsg_width;
1060 char *newline, *line = NULL;
1062 static const size_t date_display_cols = 9;
1063 const int avail = view->ncols;
1065 time_t committer_time;
1067 committer_time = got_object_commit_get_committer_time(commit);
1068 if (localtime_r(&committer_time, &tm) == NULL)
1069 return got_error_from_errno("localtime_r");
1070 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1072 return got_error(GOT_ERR_NO_SPACE);
1074 if (avail < date_display_cols)
1075 limit = MIN(sizeof(datebuf) - 1, avail);
1077 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1078 waddnstr(view->window, datebuf, limit);
1083 author = strdup(got_object_commit_get_author(commit));
1084 if (author == NULL) {
1085 err = got_error_from_errno("strdup");
1088 err = format_author(&wauthor, &author_width, author, avail - col);
1091 waddwstr(view->window, wauthor);
1092 col += author_width;
1093 while (col <= avail && author_width < author_display_cols + 2) {
1094 waddch(view->window, ' ');
1101 err = got_object_commit_get_logmsg(&logmsg0, commit);
1105 while (*logmsg == '\n')
1107 newline = strchr(logmsg, '\n');
1110 limit = avail - col;
1111 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1114 waddwstr(view->window, wlogmsg);
1115 col += logmsg_width;
1116 while (col <= avail) {
1117 waddch(view->window, ' ');
1129 static struct commit_queue_entry *
1130 alloc_commit_queue_entry(struct got_commit_object *commit,
1131 struct got_object_id *id)
1133 struct commit_queue_entry *entry;
1135 entry = calloc(1, sizeof(*entry));
1140 entry->commit = commit;
1145 pop_commit(struct commit_queue *commits)
1147 struct commit_queue_entry *entry;
1149 entry = TAILQ_FIRST(&commits->head);
1150 TAILQ_REMOVE(&commits->head, entry, entry);
1151 got_object_commit_close(entry->commit);
1152 commits->ncommits--;
1153 /* Don't free entry->id! It is owned by the commit graph. */
1158 free_commits(struct commit_queue *commits)
1160 while (!TAILQ_EMPTY(&commits->head))
1161 pop_commit(commits);
1164 static const struct got_error *
1165 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1166 int minqueue, struct got_repository *repo, const char *path)
1168 const struct got_error *err = NULL;
1172 * We keep all commits open throughout the lifetime of the log
1173 * view in order to avoid having to re-fetch commits from disk
1174 * while updating the display.
1176 while (nqueued < minqueue) {
1177 struct got_object_id *id;
1178 struct got_commit_object *commit;
1179 struct commit_queue_entry *entry;
1182 err = got_commit_graph_iter_next(&id, graph);
1184 if (err->code != GOT_ERR_ITER_NEED_MORE)
1186 err = got_commit_graph_fetch_commits(graph,
1187 minqueue, repo, NULL, NULL);
1196 err = got_object_open_as_commit(&commit, repo, id);
1199 entry = alloc_commit_queue_entry(commit, id);
1200 if (entry == NULL) {
1201 err = got_error_from_errno("alloc_commit_queue_entry");
1205 errcode = pthread_mutex_lock(&tog_mutex);
1207 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1211 entry->idx = commits->ncommits;
1212 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1214 commits->ncommits++;
1216 errcode = pthread_mutex_unlock(&tog_mutex);
1217 if (errcode && err == NULL)
1218 err = got_error_set_errno(errcode,
1219 "pthread_mutex_unlock");
1225 static const struct got_error *
1226 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1227 struct got_repository *repo)
1229 const struct got_error *err = NULL;
1230 struct got_reference *head_ref;
1234 err = got_ref_open(&head_ref, repo, branch_name, 0);
1238 err = got_ref_resolve(head_id, repo, head_ref);
1239 got_ref_close(head_ref);
1248 static const struct got_error *
1249 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1250 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1251 struct commit_queue *commits, int selected_idx, int limit,
1252 struct got_reflist_head *refs, const char *path, int commits_needed)
1254 const struct got_error *err = NULL;
1255 struct tog_log_view_state *s = &view->state.log;
1256 struct commit_queue_entry *entry;
1258 int ncommits, author_cols = 10;
1259 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1260 char *refs_str = NULL;
1266 if (ncommits == selected_idx) {
1270 entry = TAILQ_NEXT(entry, entry);
1274 if (*selected && !(view->searching && view->search_next_done == 0)) {
1275 err = got_object_id_str(&id_str, (*selected)->id);
1279 err = build_refs_str(&refs_str, refs, (*selected)->id,
1286 if (commits_needed == 0)
1287 halfdelay(10); /* disable fast refresh */
1289 if (asprintf(&ncommits_str, " [%d/%d] %s",
1290 entry ? entry->idx + 1 : 0, commits->ncommits,
1291 commits_needed > 0 ?
1292 (view->searching && view->search_next_done == 0
1293 ? "searching..." : "loading... ") :
1294 (refs_str ? refs_str : "")) == -1) {
1295 err = got_error_from_errno("asprintf");
1299 if (path && strcmp(path, "/") != 0) {
1300 if (asprintf(&header, "commit %s %s%s",
1301 id_str ? id_str : "........................................",
1302 path, ncommits_str) == -1) {
1303 err = got_error_from_errno("asprintf");
1307 } else if (asprintf(&header, "commit %s%s",
1308 id_str ? id_str : "........................................",
1309 ncommits_str) == -1) {
1310 err = got_error_from_errno("asprintf");
1314 err = format_line(&wline, &width, header, view->ncols);
1318 werase(view->window);
1320 if (view_needs_focus_indication(view))
1321 wstandout(view->window);
1322 waddwstr(view->window, wline);
1323 while (width < view->ncols) {
1324 waddch(view->window, ' ');
1327 if (view_needs_focus_indication(view))
1328 wstandend(view->window);
1333 /* Grow author column size if necessary. */
1340 if (ncommits >= limit - 1)
1342 author = strdup(got_object_commit_get_author(entry->commit));
1343 if (author == NULL) {
1344 err = got_error_from_errno("strdup");
1347 err = format_author(&wauthor, &width, author, COLS);
1348 if (author_cols < width)
1349 author_cols = width;
1352 entry = TAILQ_NEXT(entry, entry);
1359 if (ncommits >= limit - 1)
1361 if (ncommits == selected_idx)
1362 wstandout(view->window);
1363 err = draw_commit(view, entry->commit, entry->id, refs,
1365 if (ncommits == selected_idx)
1366 wstandend(view->window);
1371 entry = TAILQ_NEXT(entry, entry);
1384 scroll_up(struct tog_view *view,
1385 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1386 struct commit_queue *commits)
1388 struct commit_queue_entry *entry;
1391 entry = TAILQ_FIRST(&commits->head);
1392 if (*first_displayed_entry == entry)
1395 entry = *first_displayed_entry;
1396 while (entry && nscrolled < maxscroll) {
1397 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1399 *first_displayed_entry = entry;
1405 static const struct got_error *
1406 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1407 pthread_cond_t *need_commits)
1412 halfdelay(1); /* fast refresh while loading commits */
1414 while (*commits_needed > 0) {
1418 /* Wake the log thread. */
1419 errcode = pthread_cond_signal(need_commits);
1421 return got_error_set_errno(errcode,
1422 "pthread_cond_signal");
1423 errcode = pthread_mutex_unlock(&tog_mutex);
1425 return got_error_set_errno(errcode,
1426 "pthread_mutex_unlock");
1428 errcode = pthread_mutex_lock(&tog_mutex);
1430 return got_error_set_errno(errcode,
1431 "pthread_mutex_lock");
1433 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1435 * Thread is not done yet; lose a key press
1436 * and let the user retry... this way the GUI
1437 * remains interactive while logging deep paths
1438 * with few commits in history.
1447 static const struct got_error *
1448 scroll_down(struct tog_view *view,
1449 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1450 struct commit_queue_entry **last_displayed_entry,
1451 struct commit_queue *commits, int *log_complete, int *commits_needed,
1452 pthread_cond_t *need_commits)
1454 const struct got_error *err = NULL;
1455 struct commit_queue_entry *pentry;
1458 if (*last_displayed_entry == NULL)
1461 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1462 if (pentry == NULL && !*log_complete) {
1464 * Ask the log thread for required amount of commits
1465 * plus some amount of pre-fetching.
1467 (*commits_needed) += maxscroll + 20;
1468 err = trigger_log_thread(0, commits_needed, log_complete,
1475 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1479 *last_displayed_entry = pentry;
1481 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1484 *first_displayed_entry = pentry;
1485 } while (++nscrolled < maxscroll);
1490 static const struct got_error *
1491 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1492 struct got_commit_object *commit, struct got_object_id *commit_id,
1493 struct tog_view *log_view, struct got_reflist_head *refs,
1494 struct got_repository *repo)
1496 const struct got_error *err;
1497 struct got_object_qid *parent_id;
1498 struct tog_view *diff_view;
1500 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1501 if (diff_view == NULL)
1502 return got_error_from_errno("view_open");
1504 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1505 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1506 commit_id, log_view, refs, repo);
1508 *new_view = diff_view;
1512 static const struct got_error *
1513 tree_view_visit_subtree(struct got_tree_object *subtree,
1514 struct tog_tree_view_state *s)
1516 struct tog_parent_tree *parent;
1518 parent = calloc(1, sizeof(*parent));
1520 return got_error_from_errno("calloc");
1522 parent->tree = s->tree;
1523 parent->first_displayed_entry = s->first_displayed_entry;
1524 parent->selected_entry = s->selected_entry;
1525 parent->selected = s->selected;
1526 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1528 s->entries = got_object_tree_get_entries(s->tree);
1530 s->first_displayed_entry = NULL;
1535 static const struct got_error *
1536 browse_commit_tree(struct tog_view **new_view, int begin_x,
1537 struct commit_queue_entry *entry, const char *path,
1538 struct got_reflist_head *refs, struct got_repository *repo)
1540 const struct got_error *err = NULL;
1541 struct got_tree_object *tree;
1542 struct got_tree_entry *te;
1543 struct tog_tree_view_state *s;
1544 struct tog_view *tree_view;
1545 char *slash, *subpath = NULL;
1548 err = got_object_open_as_tree(&tree, repo,
1549 got_object_commit_get_tree_id(entry->commit));
1553 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1554 if (tree_view == NULL)
1555 return got_error_from_errno("view_open");
1557 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1559 got_object_tree_close(tree);
1562 s = &tree_view->state.tree;
1564 *new_view = tree_view;
1566 /* Walk the path and open corresponding tree objects. */
1571 struct got_object_id *tree_id;
1573 /* Ensure the correct subtree entry is selected. */
1574 slash = strchr(p, '/');
1576 slash = strchr(p, '\0');
1577 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1578 if (strncmp(p, te->name, slash - p) == 0) {
1579 s->selected_entry = te;
1584 if (s->selected_entry == NULL) {
1585 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1588 if (s->tree != s->root)
1589 s->selected++; /* skip '..' */
1591 if (!S_ISDIR(s->selected_entry->mode)) {
1592 /* Jump to this file's entry. */
1593 s->first_displayed_entry = s->selected_entry;
1598 slash = strchr(p, '/');
1600 subpath = strndup(path, slash - path);
1602 subpath = strdup(path);
1603 if (subpath == NULL) {
1604 err = got_error_from_errno("strdup");
1608 err = got_object_id_by_path(&tree_id, repo, entry->id,
1613 err = got_object_open_as_tree(&tree, repo, tree_id);
1618 err = tree_view_visit_subtree(tree, s);
1620 got_object_tree_close(tree);
1635 log_thread(void *arg)
1637 const struct got_error *err = NULL;
1639 struct tog_log_thread_args *a = arg;
1642 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1647 while (!done && !err && !tog_sigpipe_received) {
1648 err = queue_commits(a->graph, a->commits, 1, a->repo,
1651 if (err->code != GOT_ERR_ITER_COMPLETED)
1655 } else if (a->commits_needed > 0)
1656 a->commits_needed--;
1658 errcode = pthread_mutex_lock(&tog_mutex);
1660 err = got_error_set_errno(errcode,
1661 "pthread_mutex_lock");
1663 } else if (*a->quit)
1665 else if (*a->first_displayed_entry == NULL) {
1666 *a->first_displayed_entry =
1667 TAILQ_FIRST(&a->commits->head);
1668 *a->selected_entry = *a->first_displayed_entry;
1672 a->commits_needed = 0;
1673 else if (a->commits_needed == 0) {
1674 errcode = pthread_cond_wait(&a->need_commits,
1677 err = got_error_set_errno(errcode,
1678 "pthread_cond_wait");
1681 errcode = pthread_mutex_unlock(&tog_mutex);
1682 if (errcode && err == NULL)
1683 err = got_error_set_errno(errcode,
1684 "pthread_mutex_unlock");
1686 a->log_complete = 1;
1690 static const struct got_error *
1691 stop_log_thread(struct tog_log_view_state *s)
1693 const struct got_error *err = NULL;
1698 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1700 return got_error_set_errno(errcode,
1701 "pthread_cond_signal");
1702 errcode = pthread_mutex_unlock(&tog_mutex);
1704 return got_error_set_errno(errcode,
1705 "pthread_mutex_unlock");
1706 errcode = pthread_join(s->thread, (void **)&err);
1708 return got_error_set_errno(errcode, "pthread_join");
1709 errcode = pthread_mutex_lock(&tog_mutex);
1711 return got_error_set_errno(errcode,
1712 "pthread_mutex_lock");
1716 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1717 if (errcode && err == NULL)
1718 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1720 if (s->thread_args.repo) {
1721 got_repo_close(s->thread_args.repo);
1722 s->thread_args.repo = NULL;
1725 if (s->thread_args.graph) {
1726 got_commit_graph_close(s->thread_args.graph);
1727 s->thread_args.graph = NULL;
1733 static const struct got_error *
1734 close_log_view(struct tog_view *view)
1736 const struct got_error *err = NULL;
1737 struct tog_log_view_state *s = &view->state.log;
1739 err = stop_log_thread(s);
1740 free_commits(&s->commits);
1741 free(s->in_repo_path);
1742 s->in_repo_path = NULL;
1748 static const struct got_error *
1749 search_start_log_view(struct tog_view *view)
1751 struct tog_log_view_state *s = &view->state.log;
1753 s->matched_entry = NULL;
1754 s->search_entry = NULL;
1759 match_commit(struct got_commit_object *commit, const char *id_str,
1760 const char *logmsg, regex_t *regex)
1762 regmatch_t regmatch;
1764 if (regexec(regex, got_object_commit_get_author(commit), 1,
1765 ®match, 0) == 0 ||
1766 regexec(regex, got_object_commit_get_committer(commit), 1,
1767 ®match, 0) == 0 ||
1768 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1769 regexec(regex, logmsg, 1, ®match, 0) == 0)
1775 static const struct got_error *
1776 search_next_log_view(struct tog_view *view)
1778 const struct got_error *err = NULL;
1779 struct tog_log_view_state *s = &view->state.log;
1780 struct commit_queue_entry *entry;
1782 if (!view->searching) {
1783 view->search_next_done = 1;
1787 if (s->search_entry) {
1788 if (wgetch(view->window) == KEY_BACKSPACE) {
1789 view->search_next_done = 1;
1792 if (view->searching == TOG_SEARCH_FORWARD)
1793 entry = TAILQ_NEXT(s->search_entry, entry);
1795 entry = TAILQ_PREV(s->search_entry,
1796 commit_queue_head, entry);
1797 } else if (s->matched_entry) {
1798 if (view->searching == TOG_SEARCH_FORWARD)
1799 entry = TAILQ_NEXT(s->selected_entry, entry);
1801 entry = TAILQ_PREV(s->selected_entry,
1802 commit_queue_head, entry);
1804 if (view->searching == TOG_SEARCH_FORWARD)
1805 entry = TAILQ_FIRST(&s->commits.head);
1807 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1811 char *id_str, *logmsg;
1812 if (entry == NULL) {
1813 if (s->thread_args.log_complete ||
1814 view->searching == TOG_SEARCH_BACKWARD) {
1815 view->search_next_done = 1;
1819 * Poke the log thread for more commits and return,
1820 * allowing the main loop to make progress. Search
1821 * will resume at s->search_entry once we come back.
1823 s->thread_args.commits_needed++;
1824 return trigger_log_thread(1,
1825 &s->thread_args.commits_needed,
1826 &s->thread_args.log_complete,
1827 &s->thread_args.need_commits);
1830 err = got_object_id_str(&id_str, entry->id);
1834 err = got_object_commit_get_logmsg(&logmsg, entry->commit);
1837 if (match_commit(entry->commit, id_str, logmsg, &view->regex)) {
1839 view->search_next_done = 1;
1840 s->matched_entry = entry;
1846 s->search_entry = entry;
1847 if (view->searching == TOG_SEARCH_FORWARD)
1848 entry = TAILQ_NEXT(entry, entry);
1850 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1853 if (s->matched_entry) {
1854 int cur = s->selected_entry->idx;
1855 while (cur < s->matched_entry->idx) {
1856 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1861 while (cur > s->matched_entry->idx) {
1862 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1869 s->search_entry = NULL;
1874 static const struct got_error *
1875 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1876 struct got_reflist_head *refs, struct got_repository *repo,
1877 const char *head_ref_name, const char *path, int check_disk)
1879 const struct got_error *err = NULL;
1880 struct tog_log_view_state *s = &view->state.log;
1881 struct got_repository *thread_repo = NULL;
1882 struct got_commit_graph *thread_graph = NULL;
1885 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1889 /* The commit queue only contains commits being displayed. */
1890 TAILQ_INIT(&s->commits.head);
1891 s->commits.ncommits = 0;
1895 s->head_ref_name = head_ref_name;
1896 s->start_id = got_object_id_dup(start_id);
1897 if (s->start_id == NULL) {
1898 err = got_error_from_errno("got_object_id_dup");
1902 view->show = show_log_view;
1903 view->input = input_log_view;
1904 view->close = close_log_view;
1905 view->search_start = search_start_log_view;
1906 view->search_next = search_next_log_view;
1908 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1911 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1916 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1918 err = got_error_set_errno(errcode, "pthread_cond_init");
1922 s->thread_args.commits_needed = view->nlines;
1923 s->thread_args.graph = thread_graph;
1924 s->thread_args.commits = &s->commits;
1925 s->thread_args.in_repo_path = s->in_repo_path;
1926 s->thread_args.start_id = s->start_id;
1927 s->thread_args.repo = thread_repo;
1928 s->thread_args.log_complete = 0;
1929 s->thread_args.quit = &s->quit;
1930 s->thread_args.view = view;
1931 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1932 s->thread_args.selected_entry = &s->selected_entry;
1935 close_log_view(view);
1939 static const struct got_error *
1940 show_log_view(struct tog_view *view)
1942 struct tog_log_view_state *s = &view->state.log;
1944 if (s->thread == NULL) {
1945 int errcode = pthread_create(&s->thread, NULL, log_thread,
1948 return got_error_set_errno(errcode, "pthread_create");
1951 return draw_commits(view, &s->last_displayed_entry,
1952 &s->selected_entry, s->first_displayed_entry,
1953 &s->commits, s->selected, view->nlines, s->refs,
1954 s->in_repo_path, s->thread_args.commits_needed);
1957 static const struct got_error *
1958 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1959 struct tog_view **focus_view, struct tog_view *view, int ch)
1961 const struct got_error *err = NULL;
1962 struct tog_log_view_state *s = &view->state.log;
1963 char *parent_path, *in_repo_path = NULL;
1964 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1966 struct got_object_id *start_id;
1976 if (s->first_displayed_entry == NULL)
1978 if (s->selected > 0)
1981 scroll_up(view, &s->first_displayed_entry, 1,
1986 if (s->first_displayed_entry == NULL)
1988 if (TAILQ_FIRST(&s->commits.head) ==
1989 s->first_displayed_entry) {
1993 scroll_up(view, &s->first_displayed_entry,
1994 view->nlines, &s->commits);
2000 if (s->first_displayed_entry == NULL)
2002 if (s->selected < MIN(view->nlines - 2,
2003 s->commits.ncommits - 1)) {
2007 err = scroll_down(view, &s->first_displayed_entry, 1,
2008 &s->last_displayed_entry, &s->commits,
2009 &s->thread_args.log_complete,
2010 &s->thread_args.commits_needed,
2011 &s->thread_args.need_commits);
2015 struct commit_queue_entry *first;
2016 first = s->first_displayed_entry;
2019 err = scroll_down(view, &s->first_displayed_entry,
2020 view->nlines, &s->last_displayed_entry,
2021 &s->commits, &s->thread_args.log_complete,
2022 &s->thread_args.commits_needed,
2023 &s->thread_args.need_commits);
2024 if (first == s->first_displayed_entry &&
2025 s->selected < MIN(view->nlines - 2,
2026 s->commits.ncommits - 1)) {
2027 /* can't scroll further down */
2028 s->selected = MIN(view->nlines - 2,
2029 s->commits.ncommits - 1);
2035 if (s->selected > view->nlines - 2)
2036 s->selected = view->nlines - 2;
2037 if (s->selected > s->commits.ncommits - 1)
2038 s->selected = s->commits.ncommits - 1;
2043 if (s->selected_entry == NULL)
2045 if (view_is_parent_view(view))
2046 begin_x = view_split_begin_x(view->begin_x);
2047 err = open_diff_view_for_commit(&diff_view, begin_x,
2048 s->selected_entry->commit, s->selected_entry->id,
2049 view, s->refs, s->repo);
2052 if (view_is_parent_view(view)) {
2053 err = view_close_child(view);
2056 err = view_set_child(view, diff_view);
2058 view_close(diff_view);
2061 *focus_view = diff_view;
2062 view->child_focussed = 1;
2064 *new_view = diff_view;
2067 if (s->selected_entry == NULL)
2069 if (view_is_parent_view(view))
2070 begin_x = view_split_begin_x(view->begin_x);
2071 err = browse_commit_tree(&tree_view, begin_x,
2072 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2075 if (view_is_parent_view(view)) {
2076 err = view_close_child(view);
2079 err = view_set_child(view, tree_view);
2081 view_close(tree_view);
2084 *focus_view = tree_view;
2085 view->child_focussed = 1;
2087 *new_view = tree_view;
2090 if (strcmp(s->in_repo_path, "/") == 0)
2092 parent_path = dirname(s->in_repo_path);
2093 if (parent_path && strcmp(parent_path, ".") != 0) {
2094 err = stop_log_thread(s);
2097 lv = view_open(view->nlines, view->ncols,
2098 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2100 return got_error_from_errno(
2102 err = open_log_view(lv, s->start_id, s->refs,
2103 s->repo, s->head_ref_name, parent_path, 0);
2106 if (view_is_parent_view(view))
2109 view_set_child(view->parent, lv);
2116 err = stop_log_thread(s);
2119 lv = view_open(view->nlines, view->ncols,
2120 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2122 return got_error_from_errno("view_open");
2123 err = get_head_commit_id(&start_id, s->head_ref_name ?
2124 s->head_ref_name : GOT_REF_HEAD, s->repo);
2129 in_repo_path = strdup(s->in_repo_path);
2130 if (in_repo_path == NULL) {
2133 return got_error_from_errno("strdup");
2135 err = open_log_view(lv, start_id, s->refs, s->repo,
2136 s->head_ref_name, in_repo_path, 0);
2152 static const struct got_error *
2153 apply_unveil(const char *repo_path, const char *worktree_path)
2155 const struct got_error *error;
2158 if (unveil("gmon.out", "rwc") != 0)
2159 return got_error_from_errno2("unveil", "gmon.out");
2161 if (repo_path && unveil(repo_path, "r") != 0)
2162 return got_error_from_errno2("unveil", repo_path);
2164 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2165 return got_error_from_errno2("unveil", worktree_path);
2167 if (unveil("/tmp", "rwc") != 0)
2168 return got_error_from_errno2("unveil", "/tmp");
2170 error = got_privsep_unveil_exec_helpers();
2174 if (unveil(NULL, NULL) != 0)
2175 return got_error_from_errno("unveil");
2185 halfdelay(1); /* Do fast refresh while initial view is loading. */
2188 intrflush(stdscr, FALSE);
2189 keypad(stdscr, TRUE);
2191 signal(SIGWINCH, tog_sigwinch);
2192 signal(SIGPIPE, tog_sigpipe);
2195 static const struct got_error *
2196 cmd_log(int argc, char *argv[])
2198 const struct got_error *error;
2199 struct got_repository *repo = NULL;
2200 struct got_worktree *worktree = NULL;
2201 struct got_reflist_head refs;
2202 struct got_object_id *start_id = NULL;
2203 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2204 char *start_commit = NULL;
2206 struct tog_view *view;
2208 SIMPLEQ_INIT(&refs);
2211 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2216 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2219 start_commit = optarg;
2222 repo_path = realpath(optarg, NULL);
2223 if (repo_path == NULL)
2224 err(1, "-r option");
2235 cwd = getcwd(NULL, 0);
2237 error = got_error_from_errno("getcwd");
2240 error = got_worktree_open(&worktree, cwd);
2241 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2248 error = got_error_from_errno("strdup");
2251 } else if (argc == 1) {
2253 error = got_worktree_resolve_path(&path, worktree,
2258 path = strdup(argv[0]);
2260 error = got_error_from_errno("strdup");
2267 if (repo_path == NULL) {
2270 got_worktree_get_repo_path(worktree));
2272 repo_path = strdup(cwd);
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2281 error = got_repo_open(&repo, repo_path);
2285 error = apply_unveil(got_repo_get_path(repo),
2286 worktree ? got_worktree_get_root_path(worktree) : NULL);
2290 if (start_commit == NULL)
2291 error = get_head_commit_id(&start_id, worktree ?
2292 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2295 error = get_head_commit_id(&start_id, start_commit, repo);
2297 if (error->code != GOT_ERR_NOT_REF)
2299 error = got_repo_match_object_id_prefix(&start_id,
2300 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2306 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2310 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2312 error = got_error_from_errno("view_open");
2315 error = open_log_view(view, start_id, &refs, repo, worktree ?
2316 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2319 error = view_loop(view);
2326 got_repo_close(repo);
2328 got_worktree_close(worktree);
2329 got_ref_list_free(&refs);
2337 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2343 parse_next_line(FILE *f, size_t *len)
2348 const char delim[3] = { '\0', '\0', '\0'};
2350 line = fparseln(f, &linelen, &lineno, delim, 0);
2356 static const struct got_error *
2357 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2358 int *last_displayed_line, int *eof, int max_lines,
2361 const struct got_error *err;
2362 int nlines = 0, nprinted = 0;
2369 werase(view->window);
2372 err = format_line(&wline, &width, header, view->ncols);
2377 if (view_needs_focus_indication(view))
2378 wstandout(view->window);
2379 waddwstr(view->window, wline);
2380 if (view_needs_focus_indication(view))
2381 wstandend(view->window);
2382 if (width < view->ncols - 1)
2383 waddch(view->window, '\n');
2391 while (nprinted < max_lines) {
2392 line = parse_next_line(f, &len);
2397 if (++nlines < *first_displayed_line) {
2402 err = format_line(&wline, &width, line, view->ncols);
2407 waddwstr(view->window, wline);
2408 if (width < view->ncols - 1)
2409 waddch(view->window, '\n');
2410 if (++nprinted == 1)
2411 *first_displayed_line = nlines;
2416 *last_displayed_line = nlines;
2421 while (nprinted < view->nlines) {
2422 waddch(view->window, '\n');
2426 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2431 wstandout(view->window);
2432 waddwstr(view->window, wline);
2433 wstandend(view->window);
2440 get_datestr(time_t *time, char *datebuf)
2442 struct tm mytm, *tm;
2445 tm = gmtime_r(time, &mytm);
2448 s = asctime_r(tm, datebuf);
2451 p = strchr(s, '\n');
2457 static const struct got_error *
2458 write_commit_info(struct got_object_id *commit_id,
2459 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2461 const struct got_error *err = NULL;
2462 char datebuf[26], *datestr;
2463 struct got_commit_object *commit;
2464 char *id_str = NULL, *logmsg = NULL;
2465 time_t committer_time;
2466 const char *author, *committer;
2467 char *refs_str = NULL;
2470 err = build_refs_str(&refs_str, refs, commit_id, repo);
2475 err = got_object_open_as_commit(&commit, repo, commit_id);
2479 err = got_object_id_str(&id_str, commit_id);
2481 err = got_error_from_errno("got_object_id_str");
2485 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2486 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2487 err = got_error_from_errno("fprintf");
2490 if (fprintf(outfile, "from: %s\n",
2491 got_object_commit_get_author(commit)) < 0) {
2492 err = got_error_from_errno("fprintf");
2495 committer_time = got_object_commit_get_committer_time(commit);
2496 datestr = get_datestr(&committer_time, datebuf);
2497 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2498 err = got_error_from_errno("fprintf");
2501 author = got_object_commit_get_author(commit);
2502 committer = got_object_commit_get_committer(commit);
2503 if (strcmp(author, committer) != 0 &&
2504 fprintf(outfile, "via: %s\n", committer) < 0) {
2505 err = got_error_from_errno("fprintf");
2508 err = got_object_commit_get_logmsg(&logmsg, commit);
2511 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2512 err = got_error_from_errno("fprintf");
2519 got_object_commit_close(commit);
2523 static const struct got_error *
2524 create_diff(struct tog_diff_view_state *s)
2526 const struct got_error *err = NULL;
2532 err = got_error_from_errno("got_opentemp");
2535 if (s->f && fclose(s->f) != 0) {
2536 err = got_error_from_errno("fclose");
2542 err = got_object_get_type(&obj_type, s->repo, s->id1);
2544 err = got_object_get_type(&obj_type, s->repo, s->id2);
2549 case GOT_OBJ_TYPE_BLOB:
2550 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2551 s->diff_context, s->repo, f);
2553 case GOT_OBJ_TYPE_TREE:
2554 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2555 s->diff_context, s->repo, f);
2557 case GOT_OBJ_TYPE_COMMIT: {
2558 const struct got_object_id_queue *parent_ids;
2559 struct got_object_qid *pid;
2560 struct got_commit_object *commit2;
2562 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2565 /* Show commit info if we're diffing to a parent/root commit. */
2567 write_commit_info(s->id2, s->refs, s->repo, f);
2569 parent_ids = got_object_commit_get_parent_ids(commit2);
2570 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2571 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2572 write_commit_info(s->id2, s->refs,
2578 got_object_commit_close(commit2);
2580 err = got_diff_objects_as_commits(s->id1, s->id2,
2581 s->diff_context, s->repo, f);
2585 err = got_error(GOT_ERR_OBJ_TYPE);
2589 if (f && fflush(f) != 0 && err == NULL)
2590 err = got_error_from_errno("fflush");
2595 diff_view_indicate_progress(struct tog_view *view)
2597 mvwaddstr(view->window, 0, 0, "diffing...");
2602 static const struct got_error *
2603 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2604 struct got_object_id *id2, struct tog_view *log_view,
2605 struct got_reflist_head *refs, struct got_repository *repo)
2607 const struct got_error *err;
2609 if (id1 != NULL && id2 != NULL) {
2611 err = got_object_get_type(&type1, repo, id1);
2614 err = got_object_get_type(&type2, repo, id2);
2619 return got_error(GOT_ERR_OBJ_TYPE);
2623 view->state.diff.id1 = got_object_id_dup(id1);
2624 if (view->state.diff.id1 == NULL)
2625 return got_error_from_errno("got_object_id_dup");
2627 view->state.diff.id1 = NULL;
2629 view->state.diff.id2 = got_object_id_dup(id2);
2630 if (view->state.diff.id2 == NULL) {
2631 free(view->state.diff.id1);
2632 view->state.diff.id1 = NULL;
2633 return got_error_from_errno("got_object_id_dup");
2635 view->state.diff.f = NULL;
2636 view->state.diff.first_displayed_line = 1;
2637 view->state.diff.last_displayed_line = view->nlines;
2638 view->state.diff.diff_context = 3;
2639 view->state.diff.log_view = log_view;
2640 view->state.diff.repo = repo;
2641 view->state.diff.refs = refs;
2643 if (log_view && view_is_splitscreen(view))
2644 show_log_view(log_view); /* draw vborder */
2645 diff_view_indicate_progress(view);
2647 err = create_diff(&view->state.diff);
2649 free(view->state.diff.id1);
2650 view->state.diff.id1 = NULL;
2651 free(view->state.diff.id2);
2652 view->state.diff.id2 = NULL;
2656 view->show = show_diff_view;
2657 view->input = input_diff_view;
2658 view->close = close_diff_view;
2663 static const struct got_error *
2664 close_diff_view(struct tog_view *view)
2666 const struct got_error *err = NULL;
2668 free(view->state.diff.id1);
2669 view->state.diff.id1 = NULL;
2670 free(view->state.diff.id2);
2671 view->state.diff.id2 = NULL;
2672 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2673 err = got_error_from_errno("fclose");
2677 static const struct got_error *
2678 show_diff_view(struct tog_view *view)
2680 const struct got_error *err;
2681 struct tog_diff_view_state *s = &view->state.diff;
2682 char *id_str1 = NULL, *id_str2, *header;
2685 err = got_object_id_str(&id_str1, s->id1);
2689 err = got_object_id_str(&id_str2, s->id2);
2693 if (asprintf(&header, "diff %s %s",
2694 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2695 err = got_error_from_errno("asprintf");
2703 return draw_file(view, s->f, &s->first_displayed_line,
2704 &s->last_displayed_line, &s->eof, view->nlines,
2708 static const struct got_error *
2709 set_selected_commit(struct tog_diff_view_state *s,
2710 struct commit_queue_entry *entry)
2712 const struct got_error *err;
2713 const struct got_object_id_queue *parent_ids;
2714 struct got_commit_object *selected_commit;
2715 struct got_object_qid *pid;
2718 s->id2 = got_object_id_dup(entry->id);
2720 return got_error_from_errno("got_object_id_dup");
2722 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2725 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2727 pid = SIMPLEQ_FIRST(parent_ids);
2728 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2729 got_object_commit_close(selected_commit);
2733 static const struct got_error *
2734 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2735 struct tog_view **focus_view, struct tog_view *view, int ch)
2737 const struct got_error *err = NULL;
2738 struct tog_diff_view_state *s = &view->state.diff;
2739 struct tog_log_view_state *ls;
2740 struct commit_queue_entry *entry;
2746 if (s->first_displayed_line > 1)
2747 s->first_displayed_line--;
2751 if (s->first_displayed_line == 1)
2754 while (i++ < view->nlines - 1 &&
2755 s->first_displayed_line > 1)
2756 s->first_displayed_line--;
2761 s->first_displayed_line++;
2769 while (!s->eof && i++ < view->nlines - 1) {
2771 line = parse_next_line(s->f, NULL);
2772 s->first_displayed_line++;
2778 if (s->diff_context > 0) {
2780 diff_view_indicate_progress(view);
2781 err = create_diff(s);
2785 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2787 diff_view_indicate_progress(view);
2788 err = create_diff(s);
2793 if (s->log_view == NULL)
2795 ls = &s->log_view->state.log;
2796 entry = TAILQ_PREV(ls->selected_entry,
2797 commit_queue_head, entry);
2801 err = input_log_view(NULL, NULL, NULL, s->log_view,
2806 err = set_selected_commit(s, entry);
2810 s->first_displayed_line = 1;
2811 s->last_displayed_line = view->nlines;
2813 diff_view_indicate_progress(view);
2814 err = create_diff(s);
2818 if (s->log_view == NULL)
2820 ls = &s->log_view->state.log;
2822 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2823 ls->thread_args.commits_needed++;
2825 /* Display "loading..." in log view. */
2826 show_log_view(s->log_view);
2830 err = trigger_log_thread(1 /* load_all */,
2831 &ls->thread_args.commits_needed,
2832 &ls->thread_args.log_complete,
2833 &ls->thread_args.need_commits);
2837 err = input_log_view(NULL, NULL, NULL, s->log_view,
2842 entry = TAILQ_NEXT(ls->selected_entry, entry);
2846 err = set_selected_commit(s, entry);
2850 s->first_displayed_line = 1;
2851 s->last_displayed_line = view->nlines;
2853 diff_view_indicate_progress(view);
2854 err = create_diff(s);
2863 static const struct got_error *
2864 cmd_diff(int argc, char *argv[])
2866 const struct got_error *error = NULL;
2867 struct got_repository *repo = NULL;
2868 struct got_reflist_head refs;
2869 struct got_object_id *id1 = NULL, *id2 = NULL;
2870 char *repo_path = NULL;
2871 char *id_str1 = NULL, *id_str2 = NULL;
2873 struct tog_view *view;
2875 SIMPLEQ_INIT(&refs);
2878 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2883 while ((ch = getopt(argc, argv, "")) != -1) {
2895 usage_diff(); /* TODO show local worktree changes */
2896 } else if (argc == 2) {
2897 repo_path = getcwd(NULL, 0);
2898 if (repo_path == NULL)
2899 return got_error_from_errno("getcwd");
2902 } else if (argc == 3) {
2903 repo_path = realpath(argv[0], NULL);
2904 if (repo_path == NULL)
2905 return got_error_from_errno2("realpath", argv[0]);
2913 error = got_repo_open(&repo, repo_path);
2917 error = apply_unveil(got_repo_get_path(repo), NULL);
2921 error = got_repo_match_object_id_prefix(&id1, id_str1,
2922 GOT_OBJ_TYPE_ANY, repo);
2926 error = got_repo_match_object_id_prefix(&id2, id_str2,
2927 GOT_OBJ_TYPE_ANY, repo);
2931 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2935 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2937 error = got_error_from_errno("view_open");
2940 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2943 error = view_loop(view);
2947 got_repo_close(repo);
2948 got_ref_list_free(&refs);
2956 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2961 struct tog_blame_line {
2963 struct got_object_id *id;
2966 static const struct got_error *
2967 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2968 const char *path, struct tog_blame_line *lines, int nlines,
2969 int blame_complete, int selected_line, int *first_displayed_line,
2970 int *last_displayed_line, int *eof, int max_lines)
2972 const struct got_error *err;
2973 int lineno = 0, nprinted = 0;
2978 struct tog_blame_line *blame_line;
2979 struct got_object_id *prev_id = NULL;
2982 err = got_object_id_str(&id_str, id);
2987 werase(view->window);
2989 if (asprintf(&line, "commit %s", id_str) == -1) {
2990 err = got_error_from_errno("asprintf");
2995 err = format_line(&wline, &width, line, view->ncols);
2998 if (view_needs_focus_indication(view))
2999 wstandout(view->window);
3000 waddwstr(view->window, wline);
3001 if (view_needs_focus_indication(view))
3002 wstandend(view->window);
3005 if (width < view->ncols - 1)
3006 waddch(view->window, '\n');
3008 if (asprintf(&line, "[%d/%d] %s%s",
3009 *first_displayed_line - 1 + selected_line, nlines,
3010 blame_complete ? "" : "annotating... ", path) == -1) {
3012 return got_error_from_errno("asprintf");
3015 err = format_line(&wline, &width, line, view->ncols);
3020 waddwstr(view->window, wline);
3023 if (width < view->ncols - 1)
3024 waddch(view->window, '\n');
3027 while (nprinted < max_lines - 2) {
3028 line = parse_next_line(f, &len);
3033 if (++lineno < *first_displayed_line) {
3038 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3039 err = format_line(&wline, &width, line, wlimit);
3045 if (view->focussed && nprinted == selected_line - 1)
3046 wstandout(view->window);
3049 blame_line = &lines[lineno - 1];
3050 if (blame_line->annotated && prev_id &&
3051 got_object_id_cmp(prev_id, blame_line->id) == 0)
3052 waddstr(view->window, " ");
3053 else if (blame_line->annotated) {
3055 err = got_object_id_str(&id_str, blame_line->id);
3061 wprintw(view->window, "%.8s ", id_str);
3063 prev_id = blame_line->id;
3065 waddstr(view->window, "........ ");
3069 waddstr(view->window, "........ ");
3073 waddwstr(view->window, wline);
3074 while (width < wlimit) {
3075 waddch(view->window, ' ');
3078 if (view->focussed && nprinted == selected_line - 1)
3079 wstandend(view->window);
3080 if (++nprinted == 1)
3081 *first_displayed_line = lineno;
3086 *last_displayed_line = lineno;
3093 static const struct got_error *
3094 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3096 const struct got_error *err = NULL;
3097 struct tog_blame_cb_args *a = arg;
3098 struct tog_blame_line *line;
3101 if (nlines != a->nlines ||
3102 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3103 return got_error(GOT_ERR_RANGE);
3105 errcode = pthread_mutex_lock(&tog_mutex);
3107 return got_error_set_errno(errcode, "pthread_mutex_lock");
3109 if (*a->quit) { /* user has quit the blame view */
3110 err = got_error(GOT_ERR_ITER_COMPLETED);
3115 goto done; /* no change in this commit */
3117 line = &a->lines[lineno - 1];
3118 if (line->annotated)
3121 line->id = got_object_id_dup(id);
3122 if (line->id == NULL) {
3123 err = got_error_from_errno("got_object_id_dup");
3126 line->annotated = 1;
3128 errcode = pthread_mutex_unlock(&tog_mutex);
3130 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3135 blame_thread(void *arg)
3137 const struct got_error *err;
3138 struct tog_blame_thread_args *ta = arg;
3139 struct tog_blame_cb_args *a = ta->cb_args;
3142 err = got_blame(ta->path, a->commit_id, ta->repo,
3143 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3144 if (err && err->code == GOT_ERR_CANCELLED)
3147 errcode = pthread_mutex_lock(&tog_mutex);
3149 return (void *)got_error_set_errno(errcode,
3150 "pthread_mutex_lock");
3152 got_repo_close(ta->repo);
3156 errcode = pthread_mutex_unlock(&tog_mutex);
3157 if (errcode && err == NULL)
3158 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3163 static struct got_object_id *
3164 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3165 int first_displayed_line, int selected_line)
3167 struct tog_blame_line *line;
3172 line = &lines[first_displayed_line - 1 + selected_line - 1];
3173 if (!line->annotated)
3179 static const struct got_error *
3180 stop_blame(struct tog_blame *blame)
3182 const struct got_error *err = NULL;
3185 if (blame->thread) {
3187 errcode = pthread_mutex_unlock(&tog_mutex);
3189 return got_error_set_errno(errcode,
3190 "pthread_mutex_unlock");
3191 errcode = pthread_join(blame->thread, (void **)&err);
3193 return got_error_set_errno(errcode, "pthread_join");
3194 errcode = pthread_mutex_lock(&tog_mutex);
3196 return got_error_set_errno(errcode,
3197 "pthread_mutex_lock");
3198 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3200 blame->thread = NULL;
3202 if (blame->thread_args.repo) {
3203 got_repo_close(blame->thread_args.repo);
3204 blame->thread_args.repo = NULL;
3207 if (fclose(blame->f) != 0 && err == NULL)
3208 err = got_error_from_errno("fclose");
3212 for (i = 0; i < blame->nlines; i++)
3213 free(blame->lines[i].id);
3215 blame->lines = NULL;
3217 free(blame->cb_args.commit_id);
3218 blame->cb_args.commit_id = NULL;
3223 static const struct got_error *
3224 cancel_blame_view(void *arg)
3226 const struct got_error *err = NULL;
3230 errcode = pthread_mutex_lock(&tog_mutex);
3232 return got_error_set_errno(errcode,
3233 "pthread_mutex_unlock");
3236 err = got_error(GOT_ERR_CANCELLED);
3238 errcode = pthread_mutex_unlock(&tog_mutex);
3240 return got_error_set_errno(errcode,
3241 "pthread_mutex_lock");
3246 static const struct got_error *
3247 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3248 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3249 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3250 struct got_repository *repo)
3252 const struct got_error *err = NULL;
3253 struct got_blob_object *blob = NULL;
3254 struct got_repository *thread_repo = NULL;
3255 struct got_object_id *obj_id = NULL;
3258 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3262 return got_error(GOT_ERR_NO_OBJ);
3264 err = got_object_get_type(&obj_type, repo, obj_id);
3268 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3269 err = got_error(GOT_ERR_OBJ_TYPE);
3273 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3276 blame->f = got_opentemp();
3277 if (blame->f == NULL) {
3278 err = got_error_from_errno("got_opentemp");
3281 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3282 &blame->line_offsets, blame->f, blob);
3283 if (err || blame->nlines == 0)
3286 /* Don't include \n at EOF in the blame line count. */
3287 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3290 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3291 if (blame->lines == NULL) {
3292 err = got_error_from_errno("calloc");
3296 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3300 blame->cb_args.view = view;
3301 blame->cb_args.lines = blame->lines;
3302 blame->cb_args.nlines = blame->nlines;
3303 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3304 if (blame->cb_args.commit_id == NULL) {
3305 err = got_error_from_errno("got_object_id_dup");
3308 blame->cb_args.quit = done;
3310 blame->thread_args.path = path;
3311 blame->thread_args.repo = thread_repo;
3312 blame->thread_args.cb_args = &blame->cb_args;
3313 blame->thread_args.complete = blame_complete;
3314 blame->thread_args.cancel_cb = cancel_blame_view;
3315 blame->thread_args.cancel_arg = done;
3316 *blame_complete = 0;
3320 got_object_blob_close(blob);
3327 static const struct got_error *
3328 open_blame_view(struct tog_view *view, char *path,
3329 struct got_object_id *commit_id, struct got_reflist_head *refs,
3330 struct got_repository *repo)
3332 const struct got_error *err = NULL;
3333 struct tog_blame_view_state *s = &view->state.blame;
3335 SIMPLEQ_INIT(&s->blamed_commits);
3337 s->path = strdup(path);
3338 if (s->path == NULL)
3339 return got_error_from_errno("strdup");
3341 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3347 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3348 s->first_displayed_line = 1;
3349 s->last_displayed_line = view->nlines;
3350 s->selected_line = 1;
3351 s->blame_complete = 0;
3354 s->commit_id = commit_id;
3355 memset(&s->blame, 0, sizeof(s->blame));
3357 view->show = show_blame_view;
3358 view->input = input_blame_view;
3359 view->close = close_blame_view;
3360 view->search_start = search_start_blame_view;
3361 view->search_next = search_next_blame_view;
3363 return run_blame(&s->blame, view, &s->blame_complete,
3364 &s->first_displayed_line, &s->last_displayed_line,
3365 &s->selected_line, &s->done, &s->eof, s->path,
3366 s->blamed_commit->id, s->repo);
3369 static const struct got_error *
3370 close_blame_view(struct tog_view *view)
3372 const struct got_error *err = NULL;
3373 struct tog_blame_view_state *s = &view->state.blame;
3375 if (s->blame.thread)
3376 err = stop_blame(&s->blame);
3378 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3379 struct got_object_qid *blamed_commit;
3380 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3381 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3382 got_object_qid_free(blamed_commit);
3390 static const struct got_error *
3391 search_start_blame_view(struct tog_view *view)
3393 struct tog_blame_view_state *s = &view->state.blame;
3395 s->matched_line = 0;
3400 match_line(const char *line, regex_t *regex)
3402 regmatch_t regmatch;
3404 return regexec(regex, line, 1, ®match, 0) == 0;
3408 static const struct got_error *
3409 search_next_blame_view(struct tog_view *view)
3411 struct tog_blame_view_state *s = &view->state.blame;
3414 if (!view->searching) {
3415 view->search_next_done = 1;
3419 if (s->matched_line) {
3420 if (view->searching == TOG_SEARCH_FORWARD)
3421 lineno = s->matched_line + 1;
3423 lineno = s->matched_line - 1;
3425 if (view->searching == TOG_SEARCH_FORWARD)
3428 lineno = s->blame.nlines;
3436 if (lineno <= 0 || lineno > s->blame.nlines) {
3437 if (s->matched_line == 0) {
3438 view->search_next_done = 1;
3443 if (view->searching == TOG_SEARCH_FORWARD)
3446 lineno = s->blame.nlines;
3449 offset = s->blame.line_offsets[lineno - 1];
3450 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3452 return got_error_from_errno("fseeko");
3455 line = parse_next_line(s->blame.f, &len);
3456 if (line && match_line(line, &view->regex)) {
3457 view->search_next_done = 1;
3458 s->matched_line = lineno;
3463 if (view->searching == TOG_SEARCH_FORWARD)
3469 if (s->matched_line) {
3470 s->first_displayed_line = s->matched_line;
3471 s->selected_line = 1;
3477 static const struct got_error *
3478 show_blame_view(struct tog_view *view)
3480 const struct got_error *err = NULL;
3481 struct tog_blame_view_state *s = &view->state.blame;
3484 if (s->blame.thread == NULL) {
3485 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3486 &s->blame.thread_args);
3488 return got_error_set_errno(errcode, "pthread_create");
3490 halfdelay(1); /* fast refresh while annotating */
3493 if (s->blame_complete)
3494 halfdelay(10); /* disable fast refresh */
3496 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3497 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3498 s->selected_line, &s->first_displayed_line,
3499 &s->last_displayed_line, &s->eof, view->nlines);
3505 static const struct got_error *
3506 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3507 struct tog_view **focus_view, struct tog_view *view, int ch)
3509 const struct got_error *err = NULL, *thread_err = NULL;
3510 struct tog_view *diff_view;
3511 struct tog_blame_view_state *s = &view->state.blame;
3520 if (s->selected_line > 1)
3522 else if (s->selected_line == 1 &&
3523 s->first_displayed_line > 1)
3524 s->first_displayed_line--;
3527 if (s->first_displayed_line == 1) {
3528 s->selected_line = 1;
3531 if (s->first_displayed_line > view->nlines - 2)
3532 s->first_displayed_line -=
3535 s->first_displayed_line = 1;
3539 if (s->selected_line < view->nlines - 2 &&
3540 s->first_displayed_line +
3541 s->selected_line <= s->blame.nlines)
3543 else if (s->last_displayed_line <
3545 s->first_displayed_line++;
3549 struct got_object_id *id = NULL;
3550 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3551 s->first_displayed_line, s->selected_line);
3555 struct got_commit_object *commit;
3556 struct got_object_qid *pid;
3557 struct got_object_id *blob_id = NULL;
3559 err = got_object_open_as_commit(&commit,
3563 pid = SIMPLEQ_FIRST(
3564 got_object_commit_get_parent_ids(commit));
3566 got_object_commit_close(commit);
3569 /* Check if path history ends here. */
3570 err = got_object_id_by_path(&blob_id, s->repo,
3573 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3575 got_object_commit_close(commit);
3578 err = got_object_get_type(&obj_type, s->repo,
3581 /* Can't blame non-blob type objects. */
3582 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3583 got_object_commit_close(commit);
3586 err = got_object_qid_alloc(&s->blamed_commit,
3588 got_object_commit_close(commit);
3590 if (got_object_id_cmp(id,
3591 s->blamed_commit->id) == 0)
3593 err = got_object_qid_alloc(&s->blamed_commit,
3599 thread_err = stop_blame(&s->blame);
3603 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3604 s->blamed_commit, entry);
3605 err = run_blame(&s->blame, view, &s->blame_complete,
3606 &s->first_displayed_line, &s->last_displayed_line,
3607 &s->selected_line, &s->done, &s->eof,
3608 s->path, s->blamed_commit->id, s->repo);
3614 struct got_object_qid *first;
3615 first = SIMPLEQ_FIRST(&s->blamed_commits);
3616 if (!got_object_id_cmp(first->id, s->commit_id))
3619 thread_err = stop_blame(&s->blame);
3623 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3624 got_object_qid_free(s->blamed_commit);
3626 SIMPLEQ_FIRST(&s->blamed_commits);
3627 err = run_blame(&s->blame, view, &s->blame_complete,
3628 &s->first_displayed_line, &s->last_displayed_line,
3629 &s->selected_line, &s->done, &s->eof, s->path,
3630 s->blamed_commit->id, s->repo);
3637 struct got_object_id *id = NULL;
3638 struct got_object_qid *pid;
3639 struct got_commit_object *commit = NULL;
3640 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3641 s->first_displayed_line, s->selected_line);
3644 err = got_object_open_as_commit(&commit, s->repo, id);
3647 pid = SIMPLEQ_FIRST(
3648 got_object_commit_get_parent_ids(commit));
3649 if (view_is_parent_view(view))
3650 begin_x = view_split_begin_x(view->begin_x);
3651 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3652 if (diff_view == NULL) {
3653 got_object_commit_close(commit);
3654 err = got_error_from_errno("view_open");
3657 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3658 id, NULL, s->refs, s->repo);
3659 got_object_commit_close(commit);
3661 view_close(diff_view);
3664 if (view_is_parent_view(view)) {
3665 err = view_close_child(view);
3668 err = view_set_child(view, diff_view);
3670 view_close(diff_view);
3673 *focus_view = diff_view;
3674 view->child_focussed = 1;
3676 *new_view = diff_view;
3683 if (s->last_displayed_line >= s->blame.nlines &&
3684 s->selected_line >= MIN(s->blame.nlines,
3685 view->nlines - 2)) {
3688 if (s->last_displayed_line >= s->blame.nlines &&
3689 s->selected_line < view->nlines - 2) {
3690 s->selected_line = MIN(s->blame.nlines,
3694 if (s->last_displayed_line + view->nlines - 2
3696 s->first_displayed_line +=
3699 s->first_displayed_line =
3704 if (s->selected_line > view->nlines - 2) {
3705 s->selected_line = MIN(s->blame.nlines,
3712 return thread_err ? thread_err : err;
3715 static const struct got_error *
3716 cmd_blame(int argc, char *argv[])
3718 const struct got_error *error;
3719 struct got_repository *repo = NULL;
3720 struct got_reflist_head refs;
3721 struct got_worktree *worktree = NULL;
3722 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3723 struct got_object_id *commit_id = NULL;
3724 char *commit_id_str = NULL;
3726 struct tog_view *view;
3728 SIMPLEQ_INIT(&refs);
3731 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3736 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3739 commit_id_str = optarg;
3742 repo_path = realpath(optarg, NULL);
3743 if (repo_path == NULL)
3744 err(1, "-r option");
3760 cwd = getcwd(NULL, 0);
3762 error = got_error_from_errno("getcwd");
3765 if (repo_path == NULL) {
3766 error = got_worktree_open(&worktree, cwd);
3767 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3773 strdup(got_worktree_get_repo_path(worktree));
3774 if (repo_path == NULL)
3775 error = got_error_from_errno("strdup");
3779 repo_path = strdup(cwd);
3780 if (repo_path == NULL) {
3781 error = got_error_from_errno("strdup");
3789 error = got_repo_open(&repo, repo_path);
3793 error = apply_unveil(got_repo_get_path(repo), NULL);
3798 const char *prefix = got_worktree_get_path_prefix(worktree);
3799 char *p, *worktree_subdir = cwd +
3800 strlen(got_worktree_get_root_path(worktree));
3801 if (asprintf(&p, "%s%s%s%s%s",
3802 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3803 worktree_subdir, worktree_subdir[0] ? "/" : "",
3805 error = got_error_from_errno("asprintf");
3808 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3811 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3816 if (commit_id_str == NULL) {
3817 struct got_reference *head_ref;
3818 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3821 error = got_ref_resolve(&commit_id, repo, head_ref);
3822 got_ref_close(head_ref);
3824 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3826 if (error->code != GOT_ERR_NOT_REF)
3828 error = got_repo_match_object_id_prefix(&commit_id,
3829 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3835 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3839 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3841 error = got_error_from_errno("view_open");
3844 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3847 error = view_loop(view);
3853 got_worktree_close(worktree);
3855 got_repo_close(repo);
3856 got_ref_list_free(&refs);
3860 static const struct got_error *
3861 draw_tree_entries(struct tog_view *view,
3862 struct got_tree_entry **first_displayed_entry,
3863 struct got_tree_entry **last_displayed_entry,
3864 struct got_tree_entry **selected_entry, int *ndisplayed,
3865 const char *label, int show_ids, const char *parent_path,
3866 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3868 const struct got_error *err = NULL;
3869 struct got_tree_entry *te;
3875 werase(view->window);
3880 err = format_line(&wline, &width, label, view->ncols);
3883 if (view_needs_focus_indication(view))
3884 wstandout(view->window);
3885 waddwstr(view->window, wline);
3886 if (view_needs_focus_indication(view))
3887 wstandend(view->window);
3890 if (width < view->ncols - 1)
3891 waddch(view->window, '\n');
3894 err = format_line(&wline, &width, parent_path, view->ncols);
3897 waddwstr(view->window, wline);
3900 if (width < view->ncols - 1)
3901 waddch(view->window, '\n');
3904 waddch(view->window, '\n');
3908 te = SIMPLEQ_FIRST(&entries->head);
3909 if (*first_displayed_entry == NULL) {
3910 if (selected == 0) {
3912 wstandout(view->window);
3913 *selected_entry = NULL;
3915 waddstr(view->window, " ..\n"); /* parent directory */
3916 if (selected == 0 && view->focussed)
3917 wstandend(view->window);
3924 while (te != *first_displayed_entry)
3925 te = SIMPLEQ_NEXT(te, entry);
3929 char *line = NULL, *id_str = NULL;
3930 const char *modestr = "";
3933 err = got_object_id_str(&id_str, te->id);
3935 return got_error_from_errno(
3936 "got_object_id_str");
3938 if (got_object_tree_entry_is_submodule(te))
3940 else if (S_ISLNK(te->mode))
3942 else if (S_ISDIR(te->mode))
3944 else if (te->mode & S_IXUSR)
3946 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3947 te->name, modestr) == -1) {
3949 return got_error_from_errno("asprintf");
3952 err = format_line(&wline, &width, line, view->ncols);
3957 if (n == selected) {
3959 wstandout(view->window);
3960 *selected_entry = te;
3962 waddwstr(view->window, wline);
3963 if (width < view->ncols - 1)
3964 waddch(view->window, '\n');
3965 if (n == selected && view->focussed)
3966 wstandend(view->window);
3972 *last_displayed_entry = te;
3975 te = SIMPLEQ_NEXT(te, entry);
3982 tree_scroll_up(struct tog_view *view,
3983 struct got_tree_entry **first_displayed_entry, int maxscroll,
3984 const struct got_tree_entries *entries, int isroot)
3986 struct got_tree_entry *te, *prev;
3989 if (*first_displayed_entry == NULL)
3992 te = SIMPLEQ_FIRST(&entries->head);
3993 if (*first_displayed_entry == te) {
3995 *first_displayed_entry = NULL;
3999 /* XXX this is stupid... switch to TAILQ? */
4000 for (i = 0; i < maxscroll; i++) {
4001 while (te != *first_displayed_entry) {
4003 te = SIMPLEQ_NEXT(te, entry);
4005 *first_displayed_entry = prev;
4006 te = SIMPLEQ_FIRST(&entries->head);
4008 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4009 *first_displayed_entry = NULL;
4013 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4014 struct got_tree_entry *last_displayed_entry,
4015 const struct got_tree_entries *entries)
4017 struct got_tree_entry *next, *last;
4020 if (*first_displayed_entry)
4021 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4023 next = SIMPLEQ_FIRST(&entries->head);
4024 last = last_displayed_entry;
4025 while (next && last && n++ < maxscroll) {
4026 last = SIMPLEQ_NEXT(last, entry);
4028 *first_displayed_entry = next;
4029 next = SIMPLEQ_NEXT(next, entry);
4035 static const struct got_error *
4036 tree_entry_path(char **path, struct tog_parent_trees *parents,
4037 struct got_tree_entry *te)
4039 const struct got_error *err = NULL;
4040 struct tog_parent_tree *pt;
4041 size_t len = 2; /* for leading slash and NUL */
4043 TAILQ_FOREACH(pt, parents, entry)
4044 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4046 len += strlen(te->name);
4048 *path = calloc(1, len);
4050 return got_error_from_errno("calloc");
4053 pt = TAILQ_LAST(parents, tog_parent_trees);
4055 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4056 err = got_error(GOT_ERR_NO_SPACE);
4059 if (strlcat(*path, "/", len) >= len) {
4060 err = got_error(GOT_ERR_NO_SPACE);
4063 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4066 if (strlcat(*path, te->name, len) >= len) {
4067 err = got_error(GOT_ERR_NO_SPACE);
4079 static const struct got_error *
4080 blame_tree_entry(struct tog_view **new_view, int begin_x,
4081 struct got_tree_entry *te, struct tog_parent_trees *parents,
4082 struct got_object_id *commit_id, struct got_reflist_head *refs,
4083 struct got_repository *repo)
4085 const struct got_error *err = NULL;
4087 struct tog_view *blame_view;
4091 err = tree_entry_path(&path, parents, te);
4095 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4096 if (blame_view == NULL) {
4097 err = got_error_from_errno("view_open");
4101 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4103 if (err->code == GOT_ERR_CANCELLED)
4105 view_close(blame_view);
4107 *new_view = blame_view;
4113 static const struct got_error *
4114 log_tree_entry(struct tog_view **new_view, int begin_x,
4115 struct got_tree_entry *te, struct tog_parent_trees *parents,
4116 struct got_object_id *commit_id, struct got_reflist_head *refs,
4117 struct got_repository *repo)
4119 struct tog_view *log_view;
4120 const struct got_error *err = NULL;
4125 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4126 if (log_view == NULL)
4127 return got_error_from_errno("view_open");
4129 err = tree_entry_path(&path, parents, te);
4133 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4135 view_close(log_view);
4137 *new_view = log_view;
4142 static const struct got_error *
4143 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4144 struct got_object_id *commit_id, struct got_reflist_head *refs,
4145 struct got_repository *repo)
4147 const struct got_error *err = NULL;
4148 char *commit_id_str = NULL;
4149 struct tog_tree_view_state *s = &view->state.tree;
4151 TAILQ_INIT(&s->parents);
4153 err = got_object_id_str(&commit_id_str, commit_id);
4157 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4158 err = got_error_from_errno("asprintf");
4162 s->root = s->tree = root;
4163 s->entries = got_object_tree_get_entries(root);
4164 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4165 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4166 s->commit_id = got_object_id_dup(commit_id);
4167 if (s->commit_id == NULL) {
4168 err = got_error_from_errno("got_object_id_dup");
4174 view->show = show_tree_view;
4175 view->input = input_tree_view;
4176 view->close = close_tree_view;
4177 view->search_start = search_start_tree_view;
4178 view->search_next = search_next_tree_view;
4180 free(commit_id_str);
4182 free(s->tree_label);
4183 s->tree_label = NULL;
4188 static const struct got_error *
4189 close_tree_view(struct tog_view *view)
4191 struct tog_tree_view_state *s = &view->state.tree;
4193 free(s->tree_label);
4194 s->tree_label = NULL;
4196 s->commit_id = NULL;
4197 while (!TAILQ_EMPTY(&s->parents)) {
4198 struct tog_parent_tree *parent;
4199 parent = TAILQ_FIRST(&s->parents);
4200 TAILQ_REMOVE(&s->parents, parent, entry);
4204 if (s->tree != s->root)
4205 got_object_tree_close(s->tree);
4206 got_object_tree_close(s->root);
4211 static const struct got_error *
4212 search_start_tree_view(struct tog_view *view)
4214 struct tog_tree_view_state *s = &view->state.tree;
4216 s->matched_entry = NULL;
4221 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4223 regmatch_t regmatch;
4225 return regexec(regex, te->name, 1, ®match, 0) == 0;
4228 static const struct got_error *
4229 search_next_tree_view(struct tog_view *view)
4231 struct tog_tree_view_state *s = &view->state.tree;
4232 struct got_tree_entry *entry = NULL, *te;
4234 if (!view->searching) {
4235 view->search_next_done = 1;
4239 if (s->matched_entry) {
4240 if (view->searching == TOG_SEARCH_FORWARD) {
4241 if (s->selected_entry)
4242 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4244 entry = SIMPLEQ_FIRST(&s->entries->head);
4247 if (s->selected_entry == NULL) {
4248 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4251 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4253 if (SIMPLEQ_NEXT(te, entry) ==
4260 if (view->searching == TOG_SEARCH_FORWARD)
4261 entry = SIMPLEQ_FIRST(&s->entries->head);
4263 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4269 if (entry == NULL) {
4270 if (s->matched_entry == NULL) {
4271 view->search_next_done = 1;
4274 if (view->searching == TOG_SEARCH_FORWARD)
4275 entry = SIMPLEQ_FIRST(&s->entries->head);
4277 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4282 if (match_tree_entry(entry, &view->regex)) {
4283 view->search_next_done = 1;
4284 s->matched_entry = entry;
4288 if (view->searching == TOG_SEARCH_FORWARD)
4289 entry = SIMPLEQ_NEXT(entry, entry);
4291 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4294 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4295 if (SIMPLEQ_NEXT(te, entry) == entry) {
4304 if (s->matched_entry) {
4305 s->first_displayed_entry = s->matched_entry;
4312 static const struct got_error *
4313 show_tree_view(struct tog_view *view)
4315 const struct got_error *err = NULL;
4316 struct tog_tree_view_state *s = &view->state.tree;
4319 err = tree_entry_path(&parent_path, &s->parents, NULL);
4323 err = draw_tree_entries(view, &s->first_displayed_entry,
4324 &s->last_displayed_entry, &s->selected_entry,
4325 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4326 s->entries, s->selected, view->nlines, s->tree == s->root);
4333 static const struct got_error *
4334 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4335 struct tog_view **focus_view, struct tog_view *view, int ch)
4337 const struct got_error *err = NULL;
4338 struct tog_tree_view_state *s = &view->state.tree;
4339 struct tog_view *log_view;
4340 int begin_x = 0, nscrolled;
4344 s->show_ids = !s->show_ids;
4347 if (!s->selected_entry)
4349 if (view_is_parent_view(view))
4350 begin_x = view_split_begin_x(view->begin_x);
4351 err = log_tree_entry(&log_view, begin_x,
4352 s->selected_entry, &s->parents,
4353 s->commit_id, s->refs, s->repo);
4354 if (view_is_parent_view(view)) {
4355 err = view_close_child(view);
4358 err = view_set_child(view, log_view);
4360 view_close(log_view);
4363 *focus_view = log_view;
4364 view->child_focussed = 1;
4366 *new_view = log_view;
4370 if (s->selected > 0) {
4372 if (s->selected == 0)
4375 if (s->selected > 0)
4377 tree_scroll_up(view, &s->first_displayed_entry, 1,
4378 s->entries, s->tree == s->root);
4381 tree_scroll_up(view, &s->first_displayed_entry,
4382 MAX(0, view->nlines - 4 - s->selected), s->entries,
4383 s->tree == s->root);
4385 if (SIMPLEQ_FIRST(&s->entries->head) ==
4386 s->first_displayed_entry && s->tree != s->root)
4387 s->first_displayed_entry = NULL;
4391 if (s->selected < s->ndisplayed - 1) {
4395 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4396 /* can't scroll any further */
4398 tree_scroll_down(&s->first_displayed_entry, 1,
4399 s->last_displayed_entry, s->entries);
4402 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4404 /* can't scroll any further; move cursor down */
4405 if (s->selected < s->ndisplayed - 1)
4406 s->selected = s->ndisplayed - 1;
4409 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4410 view->nlines, s->last_displayed_entry, s->entries);
4411 if (nscrolled < view->nlines) {
4413 struct got_tree_entry *te;
4414 te = s->first_displayed_entry;
4417 te = SIMPLEQ_NEXT(te, entry);
4419 s->selected = ndisplayed - 1;
4425 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4426 struct tog_parent_tree *parent;
4427 /* user selected '..' */
4428 if (s->tree == s->root)
4430 parent = TAILQ_FIRST(&s->parents);
4431 TAILQ_REMOVE(&s->parents, parent,
4433 got_object_tree_close(s->tree);
4434 s->tree = parent->tree;
4436 got_object_tree_get_entries(s->tree);
4437 s->first_displayed_entry =
4438 parent->first_displayed_entry;
4440 parent->selected_entry;
4441 s->selected = parent->selected;
4443 } else if (S_ISDIR(s->selected_entry->mode)) {
4444 struct got_tree_object *subtree;
4445 err = got_object_open_as_tree(&subtree,
4446 s->repo, s->selected_entry->id);
4449 err = tree_view_visit_subtree(subtree, s);
4451 got_object_tree_close(subtree);
4454 } else if (S_ISREG(s->selected_entry->mode)) {
4455 struct tog_view *blame_view;
4456 int begin_x = view_is_parent_view(view) ?
4457 view_split_begin_x(view->begin_x) : 0;
4459 err = blame_tree_entry(&blame_view, begin_x,
4460 s->selected_entry, &s->parents,
4461 s->commit_id, s->refs, s->repo);
4464 if (view_is_parent_view(view)) {
4465 err = view_close_child(view);
4468 err = view_set_child(view, blame_view);
4470 view_close(blame_view);
4473 *focus_view = blame_view;
4474 view->child_focussed = 1;
4476 *new_view = blame_view;
4480 if (s->selected > view->nlines)
4481 s->selected = s->ndisplayed - 1;
4494 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4499 static const struct got_error *
4500 cmd_tree(int argc, char *argv[])
4502 const struct got_error *error;
4503 struct got_repository *repo = NULL;
4504 struct got_reflist_head refs;
4505 char *repo_path = NULL;
4506 struct got_object_id *commit_id = NULL;
4507 char *commit_id_arg = NULL;
4508 struct got_commit_object *commit = NULL;
4509 struct got_tree_object *tree = NULL;
4511 struct tog_view *view;
4513 SIMPLEQ_INIT(&refs);
4516 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4521 while ((ch = getopt(argc, argv, "c:")) != -1) {
4524 commit_id_arg = optarg;
4536 struct got_worktree *worktree;
4537 char *cwd = getcwd(NULL, 0);
4539 return got_error_from_errno("getcwd");
4540 error = got_worktree_open(&worktree, cwd);
4541 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4546 strdup(got_worktree_get_repo_path(worktree));
4547 got_worktree_close(worktree);
4550 if (repo_path == NULL) {
4551 error = got_error_from_errno("strdup");
4554 } else if (argc == 1) {
4555 repo_path = realpath(argv[0], NULL);
4556 if (repo_path == NULL)
4557 return got_error_from_errno2("realpath", argv[0]);
4563 error = got_repo_open(&repo, repo_path);
4567 error = apply_unveil(got_repo_get_path(repo), NULL);
4571 if (commit_id_arg == NULL)
4572 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4574 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4576 if (error->code != GOT_ERR_NOT_REF)
4578 error = got_repo_match_object_id_prefix(&commit_id,
4579 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4585 error = got_object_open_as_commit(&commit, repo, commit_id);
4589 error = got_object_open_as_tree(&tree, repo,
4590 got_object_commit_get_tree_id(commit));
4594 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4598 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4600 error = got_error_from_errno("view_open");
4603 error = open_tree_view(view, tree, commit_id, &refs, repo);
4606 error = view_loop(view);
4611 got_object_commit_close(commit);
4613 got_object_tree_close(tree);
4615 got_repo_close(repo);
4616 got_ref_list_free(&refs);
4625 fprintf(stderr, "commands:");
4626 for (i = 0; i < nitems(tog_commands); i++) {
4627 struct tog_cmd *cmd = &tog_commands[i];
4628 fprintf(stderr, " %s", cmd->name);
4630 fputc('\n', stderr);
4636 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4644 make_argv(const char *arg0, const char *arg1)
4647 int argc = (arg1 == NULL ? 1 : 2);
4649 argv = calloc(argc, sizeof(char *));
4652 argv[0] = strdup(arg0);
4653 if (argv[0] == NULL)
4656 argv[1] = strdup(arg1);
4657 if (argv[1] == NULL)
4665 main(int argc, char *argv[])
4667 const struct got_error *error = NULL;
4668 struct tog_cmd *cmd = NULL;
4669 int ch, hflag = 0, Vflag = 0;
4670 char **cmd_argv = NULL;
4672 setlocale(LC_CTYPE, "");
4674 while ((ch = getopt(argc, argv, "hV")) != -1) {
4694 got_version_print_str();
4701 /* Build an argument vector which runs a default command. */
4702 cmd = &tog_commands[0];
4703 cmd_argv = make_argv(cmd->name, NULL);
4708 /* Did the user specific a command? */
4709 for (i = 0; i < nitems(tog_commands); i++) {
4710 if (strncmp(tog_commands[i].name, argv[0],
4711 strlen(argv[0])) == 0) {
4712 cmd = &tog_commands[i];
4718 fprintf(stderr, "%s: unknown command '%s'\n",
4719 getprogname(), argv[0]);
4728 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4732 if (error && error->code != GOT_ERR_CANCELLED)
4733 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);