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
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
51 #include "got_worktree.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 const struct got_error *(*cmd_main)(int, char *[]);
69 void (*cmd_usage)(void);
73 __dead static void usage(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
79 static const struct got_error* cmd_log(int, char *[]);
80 static const struct got_error* cmd_diff(int, char *[]);
81 static const struct got_error* cmd_blame(int, char *[]);
82 static const struct got_error* cmd_tree(int, char *[]);
84 static struct tog_cmd tog_commands[] = {
85 { "log", cmd_log, usage_log,
86 "show repository history" },
87 { "diff", cmd_diff, usage_diff,
88 "compare files and directories" },
89 { "blame", cmd_blame, usage_blame,
90 "show line-by-line file history" },
91 { "tree", cmd_tree, usage_tree,
92 "browse trees in repository" },
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
111 struct commit_queue_head head;
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
117 int first_displayed_line;
118 int last_displayed_line;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
157 struct tog_log_thread_args thread_args;
160 struct tog_blame_cb_args {
161 struct tog_blame_line *lines; /* one per line */
164 struct tog_view *view;
165 struct got_object_id *commit_id;
169 struct tog_blame_thread_args {
171 struct got_repository *repo;
172 struct tog_blame_cb_args *cb_args;
179 struct tog_blame_line *lines;
182 struct tog_blame_thread_args thread_args;
183 struct tog_blame_cb_args cb_args;
187 struct tog_blame_view_state {
188 int first_displayed_line;
189 int last_displayed_line;
194 struct got_object_id_queue blamed_commits;
195 struct got_object_qid *blamed_commit;
197 struct got_repository *repo;
198 struct got_reflist_head *refs;
199 struct got_object_id *commit_id;
200 struct tog_blame blame;
203 struct tog_parent_tree {
204 TAILQ_ENTRY(tog_parent_tree) entry;
205 struct got_tree_object *tree;
206 struct got_tree_entry *first_displayed_entry;
207 struct got_tree_entry *selected_entry;
211 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 struct tog_tree_view_state {
215 struct got_tree_object *root;
216 struct got_tree_object *tree;
217 const struct got_tree_entries *entries;
218 struct got_tree_entry *first_displayed_entry;
219 struct got_tree_entry *last_displayed_entry;
220 struct got_tree_entry *selected_entry;
221 int ndisplayed, selected, show_ids;
222 struct tog_parent_trees parents;
223 struct got_object_id *commit_id;
224 struct got_repository *repo;
225 struct got_reflist_head *refs;
229 * We implement two types of views: parent views and child views.
231 * The 'Tab' key switches between a parent view and its child view.
232 * Child views are shown side-by-side to their parent view, provided
233 * there is enough screen estate.
235 * When a new view is opened from within a parent view, this new view
236 * becomes a child view of the parent view, replacing any existing child.
238 * When a new view is opened from within a child view, this new view
239 * becomes a parent view which will obscure the views below until the
240 * user quits the new parent view by typing 'q'.
242 * This list of views contains parent views only.
243 * Child views are only pointed to by their parent view.
245 TAILQ_HEAD(tog_view_list_head, tog_view);
248 TAILQ_ENTRY(tog_view) entry;
251 int nlines, ncols, begin_y, begin_x;
252 int lines, cols; /* copies of LINES and COLS */
254 struct tog_view *parent;
255 struct tog_view *child;
258 /* type-specific state */
259 enum tog_view_type type;
261 struct tog_diff_view_state diff;
262 struct tog_log_view_state log;
263 struct tog_blame_view_state blame;
264 struct tog_tree_view_state tree;
267 const struct got_error *(*show)(struct tog_view *);
268 const struct got_error *(*input)(struct tog_view **,
269 struct tog_view **, struct tog_view**, struct tog_view *, int);
270 const struct got_error *(*close)(struct tog_view *);
273 static const struct got_error *open_diff_view(struct tog_view *,
274 struct got_object_id *, struct got_object_id *, struct tog_view *,
275 struct got_reflist_head *, struct got_repository *);
276 static const struct got_error *show_diff_view(struct tog_view *);
277 static const struct got_error *input_diff_view(struct tog_view **,
278 struct tog_view **, struct tog_view **, struct tog_view *, int);
279 static const struct got_error* close_diff_view(struct tog_view *);
281 static const struct got_error *open_log_view(struct tog_view *,
282 struct got_object_id *, struct got_reflist_head *,
283 struct got_repository *, const char *, int);
284 static const struct got_error * show_log_view(struct tog_view *);
285 static const struct got_error *input_log_view(struct tog_view **,
286 struct tog_view **, struct tog_view **, struct tog_view *, int);
287 static const struct got_error *close_log_view(struct tog_view *);
289 static const struct got_error *open_blame_view(struct tog_view *, char *,
290 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
291 static const struct got_error *show_blame_view(struct tog_view *);
292 static const struct got_error *input_blame_view(struct tog_view **,
293 struct tog_view **, struct tog_view **, struct tog_view *, int);
294 static const struct got_error *close_blame_view(struct tog_view *);
296 static const struct got_error *open_tree_view(struct tog_view *,
297 struct got_tree_object *, struct got_object_id *,
298 struct got_reflist_head *, struct got_repository *);
299 static const struct got_error *show_tree_view(struct tog_view *);
300 static const struct got_error *input_tree_view(struct tog_view **,
301 struct tog_view **, struct tog_view **, struct tog_view *, int);
302 static const struct got_error *close_tree_view(struct tog_view *);
304 static volatile sig_atomic_t tog_sigwinch_received;
307 tog_sigwinch(int signo)
309 tog_sigwinch_received = 1;
312 static const struct got_error *
313 view_close(struct tog_view *view)
315 const struct got_error *err = NULL;
318 view_close(view->child);
322 err = view->close(view);
324 del_panel(view->panel);
326 delwin(view->window);
331 static struct tog_view *
332 view_open(int nlines, int ncols, int begin_y, int begin_x,
333 enum tog_view_type type)
335 struct tog_view *view = calloc(1, sizeof(*view));
343 view->nlines = nlines ? nlines : LINES - begin_y;
344 view->ncols = ncols ? ncols : COLS - begin_x;
345 view->begin_y = begin_y;
346 view->begin_x = begin_x;
347 view->window = newwin(nlines, ncols, begin_y, begin_x);
348 if (view->window == NULL) {
352 view->panel = new_panel(view->window);
353 if (view->panel == NULL ||
354 set_panel_userptr(view->panel, view) != OK) {
359 keypad(view->window, TRUE);
364 view_split_begin_x(int begin_x)
366 if (begin_x > 0 || COLS < 120)
368 return (COLS - MAX(COLS / 2, 80));
371 static const struct got_error *view_resize(struct tog_view *);
373 static const struct got_error *
374 view_splitscreen(struct tog_view *view)
376 const struct got_error *err = NULL;
379 view->begin_x = view_split_begin_x(0);
380 view->nlines = LINES;
381 view->ncols = COLS - view->begin_x;
384 err = view_resize(view);
388 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
389 return got_error_prefix_errno("mvwin");
394 static const struct got_error *
395 view_fullscreen(struct tog_view *view)
397 const struct got_error *err = NULL;
401 view->nlines = LINES;
405 err = view_resize(view);
409 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
410 return got_error_prefix_errno("mvwin");
416 view_is_parent_view(struct tog_view *view)
418 return view->parent == NULL;
421 static const struct got_error *
422 view_resize(struct tog_view *view)
426 if (view->lines > LINES)
427 nlines = view->nlines - (view->lines - LINES);
429 nlines = view->nlines + (LINES - view->lines);
431 if (view->cols > COLS)
432 ncols = view->ncols - (view->cols - COLS);
434 ncols = view->ncols + (COLS - view->cols);
436 if (wresize(view->window, nlines, ncols) == ERR)
437 return got_error_prefix_errno("wresize");
438 if (replace_panel(view->panel, view->window) == ERR)
439 return got_error_prefix_errno("replace_panel");
440 wclear(view->window);
442 view->nlines = nlines;
448 view->child->begin_x = view_split_begin_x(view->begin_x);
449 if (view->child->begin_x == 0) {
450 view_fullscreen(view->child);
451 if (view->child->focussed)
452 show_panel(view->child->panel);
454 show_panel(view->panel);
456 view_splitscreen(view->child);
457 show_panel(view->child->panel);
464 static const struct got_error *
465 view_close_child(struct tog_view *view)
467 const struct got_error *err = NULL;
469 if (view->child == NULL)
472 err = view_close(view->child);
477 static const struct got_error *
478 view_set_child(struct tog_view *view, struct tog_view *child)
480 const struct got_error *err = NULL;
483 child->parent = view;
488 view_is_splitscreen(struct tog_view *view)
490 return view->begin_x > 0;
494 * Erase all content of the view. Can be used to "flash" the view because
495 * the view loop will redraw it quickly, providing a more subtle visual
496 * effect than curs_flash(3) would provide.
499 view_flash(struct tog_view *view)
501 werase(view->window);
512 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
513 cols = 80; /* Default */
519 resize_term(lines, cols);
522 static const struct got_error *
523 view_input(struct tog_view **new, struct tog_view **dead,
524 struct tog_view **focus, int *done, struct tog_view *view,
525 struct tog_view_list_head *views)
527 const struct got_error *err = NULL;
535 nodelay(stdscr, FALSE);
536 /* Allow threads to make progress while we are waiting for input. */
537 errcode = pthread_mutex_unlock(&tog_mutex);
539 return got_error_set_errno(errcode, "pthread_mutex_unlock");
540 ch = wgetch(view->window);
541 errcode = pthread_mutex_lock(&tog_mutex);
543 return got_error_set_errno(errcode, "pthread_mutex_lock");
544 nodelay(stdscr, TRUE);
546 if (tog_sigwinch_received) {
548 tog_sigwinch_received = 0;
549 TAILQ_FOREACH(v, views, entry) {
550 err = view_resize(v);
553 err = v->input(new, dead, focus, v, KEY_RESIZE);
564 *focus = view->child;
565 view->child_focussed = 1;
566 } else if (view->parent) {
567 *focus = view->parent;
568 view->parent->child_focussed = 0;
572 err = view->input(new, dead, focus, view, ch);
579 if (view_is_parent_view(view)) {
580 if (view->child == NULL)
582 if (view_is_splitscreen(view->child)) {
583 *focus = view->child;
584 view->child_focussed = 1;
585 err = view_fullscreen(view->child);
587 err = view_splitscreen(view->child);
590 err = view->child->input(new, dead, focus,
591 view->child, KEY_RESIZE);
593 if (view_is_splitscreen(view)) {
595 view->parent->child_focussed = 1;
596 err = view_fullscreen(view);
598 err = view_splitscreen(view);
602 err = view->input(new, dead, focus, view,
609 err = view->input(new, dead, focus, view, ch);
617 view_vborder(struct tog_view *view)
620 struct tog_view *view_above;
623 return view_vborder(view->parent);
625 panel = panel_above(view->panel);
629 view_above = panel_userptr(panel);
630 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
631 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
635 view_needs_focus_indication(struct tog_view *view)
637 if (view_is_parent_view(view)) {
638 if (view->child == NULL || view->child_focussed)
640 if (!view_is_splitscreen(view->child))
642 } else if (!view_is_splitscreen(view))
645 return view->focussed;
648 static const struct got_error *
649 view_loop(struct tog_view *view)
651 const struct got_error *err = NULL;
652 struct tog_view_list_head views;
653 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
654 int fast_refresh = 10;
655 int done = 0, errcode;
657 errcode = pthread_mutex_lock(&tog_mutex);
659 return got_error_set_errno(errcode, "pthread_mutex_lock");
662 TAILQ_INSERT_HEAD(&views, view, entry);
666 err = view->show(view);
671 while (!TAILQ_EMPTY(&views) && !done) {
672 /* Refresh fast during initialization, then become slower. */
673 if (fast_refresh && fast_refresh-- == 0)
674 halfdelay(10); /* switch to once per second */
676 err = view_input(&new_view, &dead_view, &focus_view, &done,
681 struct tog_view *prev = NULL;
683 if (view_is_parent_view(dead_view))
684 prev = TAILQ_PREV(dead_view,
685 tog_view_list_head, entry);
686 else if (view->parent != dead_view)
689 if (dead_view->parent)
690 dead_view->parent->child = NULL;
692 TAILQ_REMOVE(&views, dead_view, entry);
694 err = view_close(dead_view);
695 if (err || dead_view == main_view)
698 if (view == dead_view) {
703 else if (!TAILQ_EMPTY(&views))
704 view = TAILQ_LAST(&views,
709 if (view->child && view->child_focussed)
710 focus_view = view->child;
717 struct tog_view *v, *t;
718 /* Only allow one parent view per type. */
719 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
720 if (v->type != new_view->type)
722 TAILQ_REMOVE(&views, v, entry);
728 TAILQ_INSERT_TAIL(&views, new_view, entry);
730 if (focus_view == NULL)
731 focus_view = new_view;
734 show_panel(focus_view->panel);
737 focus_view->focussed = 1;
740 show_panel(new_view->panel);
741 if (view->child && view_is_splitscreen(view->child))
742 show_panel(view->child->panel);
745 if (focus_view == NULL) {
747 show_panel(view->panel);
748 if (view->child && view_is_splitscreen(view->child))
749 show_panel(view->child->panel);
753 err = view->parent->show(view->parent);
757 err = view->show(view);
761 err = view->child->show(view->child);
770 while (!TAILQ_EMPTY(&views)) {
771 view = TAILQ_FIRST(&views);
772 TAILQ_REMOVE(&views, view, entry);
776 errcode = pthread_mutex_unlock(&tog_mutex);
778 return got_error_set_errno(errcode, "pthread_mutex_unlock");
788 "usage: %s log [-c commit] [-r repository-path] [path]\n",
793 /* Create newly allocated wide-character string equivalent to a byte string. */
794 static const struct got_error *
795 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
798 const struct got_error *err = NULL;
801 *wlen = mbstowcs(NULL, s, 0);
802 if (*wlen == (size_t)-1) {
805 return got_error_prefix_errno("mbstowcs");
807 /* byte string invalid in current encoding; try to "fix" it */
808 err = got_mbsavis(&vis, &vislen, s);
811 *wlen = mbstowcs(NULL, vis, 0);
812 if (*wlen == (size_t)-1) {
813 err = got_error_prefix_errno("mbstowcs"); /* give up */
818 *ws = calloc(*wlen + 1, sizeof(*ws));
820 err = got_error_prefix_errno("calloc");
824 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
825 err = got_error_prefix_errno("mbstowcs");
836 /* Format a line for display, ensuring that it won't overflow a width limit. */
837 static const struct got_error *
838 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
840 const struct got_error *err = NULL;
842 wchar_t *wline = NULL;
849 err = mbs2ws(&wline, &wlen, line);
854 while (i < wlen && cols < wlimit) {
855 int width = wcwidth(wline[i]);
862 if (cols + width <= wlimit)
867 if (wline[i] == L'\t')
868 cols += TABSIZE - ((cols + 1) % TABSIZE);
872 err = got_error_prefix_errno("wcwidth");
887 static const struct got_error*
888 build_refs_str(char **refs_str, struct got_reflist_head *refs,
889 struct got_object_id *id)
891 static const struct got_error *err = NULL;
892 struct got_reflist_entry *re;
898 SIMPLEQ_FOREACH(re, refs, entry) {
899 if (got_object_id_cmp(re->id, id) != 0)
901 name = got_ref_get_name(re->ref);
902 if (strcmp(name, GOT_REF_HEAD) == 0)
904 if (strncmp(name, "refs/", 5) == 0)
906 if (strncmp(name, "got/", 4) == 0)
908 if (strncmp(name, "heads/", 6) == 0)
910 if (strncmp(name, "remotes/", 8) == 0)
913 if (asprintf(refs_str, "%s%s%s", s ? s : "",
914 s ? ", " : "", name) == -1) {
915 err = got_error_prefix_errno("asprintf");
926 static const struct got_error *
927 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
929 char *smallerthan, *at;
931 smallerthan = strchr(author, '<');
932 if (smallerthan && smallerthan[1] != '\0')
933 author = smallerthan + 1;
934 at = strchr(author, '@');
937 return format_line(wauthor, author_width, author, limit);
940 static const struct got_error *
941 draw_commit(struct tog_view *view, struct got_commit_object *commit,
942 struct got_object_id *id, struct got_reflist_head *refs,
943 int author_display_cols)
945 const struct got_error *err = NULL;
946 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
947 char *logmsg0 = NULL, *logmsg = NULL;
949 wchar_t *wlogmsg = NULL, *wauthor = NULL;
950 int author_width, logmsg_width;
951 char *newline, *line = NULL;
953 static const size_t date_display_cols = 9;
954 const int avail = view->ncols;
956 time_t committer_time;
958 committer_time = got_object_commit_get_committer_time(commit);
959 if (localtime_r(&committer_time, &tm) == NULL)
960 return got_error_prefix_errno("localtime_r");
961 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
963 return got_error(GOT_ERR_NO_SPACE);
965 if (avail < date_display_cols)
966 limit = MIN(sizeof(datebuf) - 1, avail);
968 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
969 waddnstr(view->window, datebuf, limit);
974 author = strdup(got_object_commit_get_author(commit));
975 if (author == NULL) {
976 err = got_error_prefix_errno("strdup");
979 err = format_author(&wauthor, &author_width, author, avail - col);
982 waddwstr(view->window, wauthor);
984 while (col <= avail && author_width < author_display_cols + 2) {
985 waddch(view->window, ' ');
992 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
993 if (logmsg0 == NULL) {
994 err = got_error_prefix_errno("strdup");
998 while (*logmsg == '\n')
1000 newline = strchr(logmsg, '\n');
1003 limit = avail - col;
1004 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1007 waddwstr(view->window, wlogmsg);
1008 col += logmsg_width;
1009 while (col <= avail) {
1010 waddch(view->window, ' ');
1022 static struct commit_queue_entry *
1023 alloc_commit_queue_entry(struct got_commit_object *commit,
1024 struct got_object_id *id)
1026 struct commit_queue_entry *entry;
1028 entry = calloc(1, sizeof(*entry));
1033 entry->commit = commit;
1038 pop_commit(struct commit_queue *commits)
1040 struct commit_queue_entry *entry;
1042 entry = TAILQ_FIRST(&commits->head);
1043 TAILQ_REMOVE(&commits->head, entry, entry);
1044 got_object_commit_close(entry->commit);
1045 commits->ncommits--;
1046 /* Don't free entry->id! It is owned by the commit graph. */
1051 free_commits(struct commit_queue *commits)
1053 while (!TAILQ_EMPTY(&commits->head))
1054 pop_commit(commits);
1057 static const struct got_error *
1058 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1059 int minqueue, struct got_repository *repo, const char *path)
1061 const struct got_error *err = NULL;
1065 * We keep all commits open throughout the lifetime of the log
1066 * view in order to avoid having to re-fetch commits from disk
1067 * while updating the display.
1069 while (nqueued < minqueue) {
1070 struct got_object_id *id;
1071 struct got_commit_object *commit;
1072 struct commit_queue_entry *entry;
1075 err = got_commit_graph_iter_next(&id, graph);
1077 if (err->code != GOT_ERR_ITER_NEED_MORE)
1079 err = got_commit_graph_fetch_commits(graph,
1089 err = got_object_open_as_commit(&commit, repo, id);
1092 entry = alloc_commit_queue_entry(commit, id);
1093 if (entry == NULL) {
1094 err = got_error_prefix_errno("alloc_commit_queue_entry");
1098 errcode = pthread_mutex_lock(&tog_mutex);
1100 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1104 entry->idx = commits->ncommits;
1105 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1107 commits->ncommits++;
1109 errcode = pthread_mutex_unlock(&tog_mutex);
1110 if (errcode && err == NULL)
1111 err = got_error_set_errno(errcode,
1112 "pthread_mutex_unlock");
1118 static const struct got_error *
1119 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1121 const struct got_error *err = NULL;
1122 struct got_reference *head_ref;
1126 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1130 err = got_ref_resolve(head_id, repo, head_ref);
1131 got_ref_close(head_ref);
1140 static const struct got_error *
1141 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1142 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1143 struct commit_queue *commits, int selected_idx, int limit,
1144 struct got_reflist_head *refs, const char *path, int commits_needed)
1146 const struct got_error *err = NULL;
1147 struct commit_queue_entry *entry;
1148 int ncommits, width;
1149 int author_cols = 10;
1150 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1151 char *refs_str = NULL;
1157 if (ncommits == selected_idx) {
1161 entry = TAILQ_NEXT(entry, entry);
1166 err = got_object_id_str(&id_str, (*selected)->id);
1170 err = build_refs_str(&refs_str, refs, (*selected)->id);
1176 if (commits_needed == 0)
1177 halfdelay(10); /* disable fast refresh */
1179 if (asprintf(&ncommits_str, " [%d/%d] %s",
1180 entry ? entry->idx + 1 : 0, commits->ncommits,
1181 commits_needed > 0 ? "loading... " :
1182 (refs_str ? refs_str : "")) == -1) {
1183 err = got_error_prefix_errno("asprintf");
1187 if (path && strcmp(path, "/") != 0) {
1188 if (asprintf(&header, "commit %s %s%s",
1189 id_str ? id_str : "........................................",
1190 path, ncommits_str) == -1) {
1191 err = got_error_prefix_errno("asprintf");
1195 } else if (asprintf(&header, "commit %s%s",
1196 id_str ? id_str : "........................................",
1197 ncommits_str) == -1) {
1198 err = got_error_prefix_errno("asprintf");
1202 err = format_line(&wline, &width, header, view->ncols);
1206 werase(view->window);
1208 if (view_needs_focus_indication(view))
1209 wstandout(view->window);
1210 waddwstr(view->window, wline);
1211 while (width < view->ncols) {
1212 waddch(view->window, ' ');
1215 if (view_needs_focus_indication(view))
1216 wstandend(view->window);
1221 /* Grow author column size if necessary. */
1228 if (ncommits >= limit - 1)
1230 author = strdup(got_object_commit_get_author(entry->commit));
1231 if (author == NULL) {
1232 err = got_error_prefix_errno("strdup");
1235 err = format_author(&wauthor, &width, author, COLS);
1236 if (author_cols < width)
1237 author_cols = width;
1240 entry = TAILQ_NEXT(entry, entry);
1247 if (ncommits >= limit - 1)
1249 if (ncommits == selected_idx)
1250 wstandout(view->window);
1251 err = draw_commit(view, entry->commit, entry->id, refs,
1253 if (ncommits == selected_idx)
1254 wstandend(view->window);
1259 entry = TAILQ_NEXT(entry, entry);
1272 scroll_up(struct tog_view *view,
1273 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1274 struct commit_queue *commits)
1276 struct commit_queue_entry *entry;
1279 entry = TAILQ_FIRST(&commits->head);
1280 if (*first_displayed_entry == entry) {
1285 entry = *first_displayed_entry;
1286 while (entry && nscrolled < maxscroll) {
1287 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1289 *first_displayed_entry = entry;
1295 static const struct got_error *
1296 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1297 pthread_cond_t *need_commits)
1302 halfdelay(1); /* fast refresh while loading commits */
1304 while (*commits_needed > 0) {
1308 /* Wake the log thread. */
1309 errcode = pthread_cond_signal(need_commits);
1311 return got_error_set_errno(errcode,
1312 "pthread_cond_signal");
1313 errcode = pthread_mutex_unlock(&tog_mutex);
1315 return got_error_set_errno(errcode,
1316 "pthread_mutex_unlock");
1318 errcode = pthread_mutex_lock(&tog_mutex);
1320 return got_error_set_errno(errcode,
1321 "pthread_mutex_lock");
1323 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1325 * Thread is not done yet; lose a key press
1326 * and let the user retry... this way the GUI
1327 * remains interactive while logging deep paths
1328 * with few commits in history.
1337 static const struct got_error *
1338 scroll_down(struct tog_view *view,
1339 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1340 struct commit_queue_entry **last_displayed_entry,
1341 struct commit_queue *commits, int *log_complete, int *commits_needed,
1342 pthread_cond_t *need_commits)
1344 const struct got_error *err = NULL;
1345 struct commit_queue_entry *pentry;
1348 if (*last_displayed_entry == NULL)
1351 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1352 if (pentry == NULL && !*log_complete) {
1354 * Ask the log thread for required amount of commits
1355 * plus some amount of pre-fetching.
1357 (*commits_needed) += maxscroll + 20;
1358 err = trigger_log_thread(0, commits_needed, log_complete,
1365 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1366 if (pentry == NULL) {
1372 *last_displayed_entry = pentry;
1374 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1377 *first_displayed_entry = pentry;
1378 } while (++nscrolled < maxscroll);
1383 static const struct got_error *
1384 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1385 struct got_commit_object *commit, struct got_object_id *commit_id,
1386 struct tog_view *log_view, struct got_reflist_head *refs,
1387 struct got_repository *repo)
1389 const struct got_error *err;
1390 struct got_object_qid *parent_id;
1391 struct tog_view *diff_view;
1393 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1394 if (diff_view == NULL)
1395 return got_error_prefix_errno("view_open");
1397 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1398 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1399 commit_id, log_view, refs, repo);
1401 *new_view = diff_view;
1405 static const struct got_error *
1406 browse_commit(struct tog_view **new_view, int begin_x,
1407 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1408 struct got_repository *repo)
1410 const struct got_error *err = NULL;
1411 struct got_tree_object *tree;
1412 struct tog_view *tree_view;
1414 err = got_object_open_as_tree(&tree, repo,
1415 got_object_commit_get_tree_id(entry->commit));
1419 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1420 if (tree_view == NULL)
1421 return got_error_prefix_errno("view_open");
1423 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1425 got_object_tree_close(tree);
1427 *new_view = tree_view;
1432 log_thread(void *arg)
1434 const struct got_error *err = NULL;
1436 struct tog_log_thread_args *a = arg;
1439 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1443 while (!done && !err) {
1444 err = queue_commits(a->graph, a->commits, 1, a->repo,
1447 if (err->code != GOT_ERR_ITER_COMPLETED)
1451 } else if (a->commits_needed > 0)
1452 a->commits_needed--;
1454 errcode = pthread_mutex_lock(&tog_mutex);
1456 err = got_error_set_errno(errcode,
1457 "pthread_mutex_lock");
1459 } else if (*a->quit)
1461 else if (*a->first_displayed_entry == NULL) {
1462 *a->first_displayed_entry =
1463 TAILQ_FIRST(&a->commits->head);
1464 *a->selected_entry = *a->first_displayed_entry;
1468 a->commits_needed = 0;
1469 else if (a->commits_needed == 0) {
1470 errcode = pthread_cond_wait(&a->need_commits,
1473 err = got_error_set_errno(errcode,
1474 "pthread_cond_wait");
1477 errcode = pthread_mutex_unlock(&tog_mutex);
1478 if (errcode && err == NULL)
1479 err = got_error_set_errno(errcode,
1480 "pthread_mutex_unlock");
1482 a->log_complete = 1;
1486 static const struct got_error *
1487 stop_log_thread(struct tog_log_view_state *s)
1489 const struct got_error *err = NULL;
1494 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1496 return got_error_set_errno(errcode,
1497 "pthread_cond_signal");
1498 errcode = pthread_mutex_unlock(&tog_mutex);
1500 return got_error_set_errno(errcode,
1501 "pthread_mutex_unlock");
1502 errcode = pthread_join(s->thread, (void **)&err);
1504 return got_error_set_errno(errcode, "pthread_join");
1505 errcode = pthread_mutex_lock(&tog_mutex);
1507 return got_error_set_errno(errcode,
1508 "pthread_mutex_lock");
1512 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1516 if (s->thread_args.repo) {
1517 got_repo_close(s->thread_args.repo);
1518 s->thread_args.repo = NULL;
1521 if (s->thread_args.graph) {
1522 got_commit_graph_close(s->thread_args.graph);
1523 s->thread_args.graph = NULL;
1529 static const struct got_error *
1530 close_log_view(struct tog_view *view)
1532 const struct got_error *err = NULL;
1533 struct tog_log_view_state *s = &view->state.log;
1535 err = stop_log_thread(s);
1536 free_commits(&s->commits);
1537 free(s->in_repo_path);
1538 s->in_repo_path = NULL;
1544 static const struct got_error *
1545 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1546 struct got_reflist_head *refs, struct got_repository *repo,
1547 const char *path, int check_disk)
1549 const struct got_error *err = NULL;
1550 struct tog_log_view_state *s = &view->state.log;
1551 struct got_repository *thread_repo = NULL;
1552 struct got_commit_graph *thread_graph = NULL;
1555 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1559 /* The commit queue only contains commits being displayed. */
1560 TAILQ_INIT(&s->commits.head);
1561 s->commits.ncommits = 0;
1565 s->start_id = got_object_id_dup(start_id);
1566 if (s->start_id == NULL) {
1567 err = got_error_prefix_errno("got_object_id_dup");
1571 view->show = show_log_view;
1572 view->input = input_log_view;
1573 view->close = close_log_view;
1575 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1578 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1583 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1585 err = got_error_set_errno(errcode, "pthread_cond_init");
1589 s->thread_args.commits_needed = view->nlines;
1590 s->thread_args.graph = thread_graph;
1591 s->thread_args.commits = &s->commits;
1592 s->thread_args.in_repo_path = s->in_repo_path;
1593 s->thread_args.start_id = s->start_id;
1594 s->thread_args.repo = thread_repo;
1595 s->thread_args.log_complete = 0;
1596 s->thread_args.quit = &s->quit;
1597 s->thread_args.view = view;
1598 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1599 s->thread_args.selected_entry = &s->selected_entry;
1602 close_log_view(view);
1606 static const struct got_error *
1607 show_log_view(struct tog_view *view)
1609 struct tog_log_view_state *s = &view->state.log;
1611 if (s->thread == NULL) {
1612 int errcode = pthread_create(&s->thread, NULL, log_thread,
1615 return got_error_set_errno(errcode, "pthread_create");
1618 return draw_commits(view, &s->last_displayed_entry,
1619 &s->selected_entry, s->first_displayed_entry,
1620 &s->commits, s->selected, view->nlines, s->refs,
1621 s->in_repo_path, s->thread_args.commits_needed);
1624 static const struct got_error *
1625 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1626 struct tog_view **focus_view, struct tog_view *view, int ch)
1628 const struct got_error *err = NULL;
1629 struct tog_log_view_state *s = &view->state.log;
1631 struct tog_view *diff_view = NULL, *tree_view = NULL;
1642 if (s->first_displayed_entry == NULL)
1644 if (s->selected > 0)
1646 if (s->selected > 0)
1648 scroll_up(view, &s->first_displayed_entry, 1,
1652 if (s->first_displayed_entry == NULL)
1654 if (TAILQ_FIRST(&s->commits.head) ==
1655 s->first_displayed_entry) {
1656 if (s->selected == 0) {
1663 scroll_up(view, &s->first_displayed_entry,
1664 view->nlines, &s->commits);
1670 if (s->first_displayed_entry == NULL)
1672 if (s->selected < MIN(view->nlines - 2,
1673 s->commits.ncommits - 1)) {
1677 err = scroll_down(view, &s->first_displayed_entry, 1,
1678 &s->last_displayed_entry, &s->commits,
1679 &s->thread_args.log_complete,
1680 &s->thread_args.commits_needed,
1681 &s->thread_args.need_commits);
1684 struct commit_queue_entry *first;
1685 first = s->first_displayed_entry;
1688 err = scroll_down(view, &s->first_displayed_entry,
1689 view->nlines, &s->last_displayed_entry,
1690 &s->commits, &s->thread_args.log_complete,
1691 &s->thread_args.commits_needed,
1692 &s->thread_args.need_commits);
1693 if (first == s->first_displayed_entry &&
1694 s->selected < MIN(view->nlines - 2,
1695 s->commits.ncommits - 1)) {
1696 /* can't scroll further down */
1697 s->selected = MIN(view->nlines - 2,
1698 s->commits.ncommits - 1);
1704 if (s->selected > view->nlines - 2)
1705 s->selected = view->nlines - 2;
1706 if (s->selected > s->commits.ncommits - 1)
1707 s->selected = s->commits.ncommits - 1;
1711 if (s->selected_entry == NULL)
1713 if (view_is_parent_view(view))
1714 begin_x = view_split_begin_x(view->begin_x);
1715 err = open_diff_view_for_commit(&diff_view, begin_x,
1716 s->selected_entry->commit, s->selected_entry->id,
1717 view, s->refs, s->repo);
1720 if (view_is_parent_view(view)) {
1721 err = view_close_child(view);
1724 err = view_set_child(view, diff_view);
1726 view_close(diff_view);
1729 *focus_view = diff_view;
1730 view->child_focussed = 1;
1732 *new_view = diff_view;
1735 if (s->selected_entry == NULL)
1737 if (view_is_parent_view(view))
1738 begin_x = view_split_begin_x(view->begin_x);
1739 err = browse_commit(&tree_view, begin_x,
1740 s->selected_entry, s->refs, s->repo);
1743 if (view_is_parent_view(view)) {
1744 err = view_close_child(view);
1747 err = view_set_child(view, tree_view);
1749 view_close(tree_view);
1752 *focus_view = tree_view;
1753 view->child_focussed = 1;
1755 *new_view = tree_view;
1758 if (strcmp(s->in_repo_path, "/") == 0)
1760 parent_path = dirname(s->in_repo_path);
1761 if (parent_path && strcmp(parent_path, ".") != 0) {
1762 struct tog_view *lv;
1763 err = stop_log_thread(s);
1766 lv = view_open(view->nlines, view->ncols,
1767 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1769 return got_error_prefix_errno(
1771 err = open_log_view(lv, s->start_id, s->refs,
1772 s->repo, parent_path, 0);
1775 if (view_is_parent_view(view))
1778 view_set_child(view->parent, lv);
1791 static const struct got_error *
1792 apply_unveil(const char *repo_path, const char *worktree_path)
1794 const struct got_error *error;
1796 if (repo_path && unveil(repo_path, "r") != 0)
1797 return got_error_prefix_errno2("unveil", repo_path);
1799 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1800 return got_error_prefix_errno2("unveil", worktree_path);
1802 if (unveil("/tmp", "rwc") != 0)
1803 return got_error_prefix_errno2("unveil", "/tmp");
1805 error = got_privsep_unveil_exec_helpers();
1809 if (unveil(NULL, NULL) != 0)
1810 return got_error_prefix_errno("unveil");
1820 halfdelay(1); /* Do fast refresh while initial view is loading. */
1823 intrflush(stdscr, FALSE);
1824 keypad(stdscr, TRUE);
1826 signal(SIGWINCH, tog_sigwinch);
1829 static const struct got_error *
1830 cmd_log(int argc, char *argv[])
1832 const struct got_error *error;
1833 struct got_repository *repo = NULL;
1834 struct got_worktree *worktree = NULL;
1835 struct got_reflist_head refs;
1836 struct got_object_id *start_id = NULL;
1837 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1838 char *start_commit = NULL;
1840 struct tog_view *view;
1842 SIMPLEQ_INIT(&refs);
1845 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1850 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1853 start_commit = optarg;
1856 repo_path = realpath(optarg, NULL);
1857 if (repo_path == NULL)
1858 err(1, "-r option");
1869 cwd = getcwd(NULL, 0);
1871 error = got_error_prefix_errno("getcwd");
1874 error = got_worktree_open(&worktree, cwd);
1875 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1882 error = got_error_prefix_errno("strdup");
1885 } else if (argc == 1) {
1887 error = got_worktree_resolve_path(&path, worktree,
1892 path = strdup(argv[0]);
1894 error = got_error_prefix_errno("strdup");
1901 repo_path = worktree ?
1902 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1903 if (repo_path == NULL) {
1904 error = got_error_prefix_errno("strdup");
1910 error = got_repo_open(&repo, repo_path);
1914 error = apply_unveil(got_repo_get_path(repo),
1915 worktree ? got_worktree_get_root_path(worktree) : NULL);
1919 if (start_commit == NULL)
1920 error = get_head_commit_id(&start_id, repo);
1922 error = got_object_resolve_id_str(&start_id, repo,
1927 error = got_ref_list(&refs, repo);
1931 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1933 error = got_error_prefix_errno("view_open");
1936 error = open_log_view(view, start_id, &refs, repo, path, 1);
1939 error = view_loop(view);
1946 got_repo_close(repo);
1948 got_worktree_close(worktree);
1949 got_ref_list_free(&refs);
1957 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1963 parse_next_line(FILE *f, size_t *len)
1968 const char delim[3] = { '\0', '\0', '\0'};
1970 line = fparseln(f, &linelen, &lineno, delim, 0);
1976 static const struct got_error *
1977 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1978 int *last_displayed_line, int *eof, int max_lines,
1981 const struct got_error *err;
1982 int nlines = 0, nprinted = 0;
1989 werase(view->window);
1992 err = format_line(&wline, &width, header, view->ncols);
1997 if (view_needs_focus_indication(view))
1998 wstandout(view->window);
1999 waddwstr(view->window, wline);
2000 if (view_needs_focus_indication(view))
2001 wstandend(view->window);
2002 if (width < view->ncols)
2003 waddch(view->window, '\n');
2011 while (nprinted < max_lines) {
2012 line = parse_next_line(f, &len);
2017 if (++nlines < *first_displayed_line) {
2022 err = format_line(&wline, &width, line, view->ncols);
2027 waddwstr(view->window, wline);
2028 if (width < view->ncols)
2029 waddch(view->window, '\n');
2030 if (++nprinted == 1)
2031 *first_displayed_line = nlines;
2036 *last_displayed_line = nlines;
2044 get_datestr(time_t *time, char *datebuf)
2046 char *p, *s = ctime_r(time, datebuf);
2047 p = strchr(s, '\n');
2053 static const struct got_error *
2054 write_commit_info(struct got_object_id *commit_id,
2055 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2057 const struct got_error *err = NULL;
2059 struct got_commit_object *commit;
2060 char *id_str = NULL;
2061 time_t committer_time;
2062 const char *author, *committer;
2063 char *refs_str = NULL;
2066 err = build_refs_str(&refs_str, refs, commit_id);
2071 err = got_object_open_as_commit(&commit, repo, commit_id);
2075 err = got_object_id_str(&id_str, commit_id);
2077 err = got_error_prefix_errno("got_object_id_str");
2081 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2082 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2083 err = got_error_prefix_errno("fprintf");
2086 if (fprintf(outfile, "from: %s\n",
2087 got_object_commit_get_author(commit)) < 0) {
2088 err = got_error_prefix_errno("fprintf");
2091 committer_time = got_object_commit_get_committer_time(commit);
2092 if (fprintf(outfile, "date: %s UTC\n",
2093 get_datestr(&committer_time, datebuf)) < 0) {
2094 err = got_error_prefix_errno("fprintf");
2097 author = got_object_commit_get_author(commit);
2098 committer = got_object_commit_get_committer(commit);
2099 if (strcmp(author, committer) != 0 &&
2100 fprintf(outfile, "via: %s\n", committer) < 0) {
2101 err = got_error_prefix_errno("fprintf");
2104 if (fprintf(outfile, "%s\n",
2105 got_object_commit_get_logmsg(commit)) < 0) {
2106 err = got_error_prefix_errno("fprintf");
2112 got_object_commit_close(commit);
2116 static const struct got_error *
2117 create_diff(struct tog_diff_view_state *s)
2119 const struct got_error *err = NULL;
2125 err = got_error_prefix_errno("got_opentemp");
2128 if (s->f && fclose(s->f) != 0) {
2129 err = got_error_prefix_errno("fclose");
2135 err = got_object_get_type(&obj_type, s->repo, s->id1);
2137 err = got_object_get_type(&obj_type, s->repo, s->id2);
2142 case GOT_OBJ_TYPE_BLOB:
2143 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2144 s->diff_context, s->repo, f);
2146 case GOT_OBJ_TYPE_TREE:
2147 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2148 s->diff_context, s->repo, f);
2150 case GOT_OBJ_TYPE_COMMIT: {
2151 const struct got_object_id_queue *parent_ids;
2152 struct got_object_qid *pid;
2153 struct got_commit_object *commit2;
2155 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2158 /* Show commit info if we're diffing to a parent/root commit. */
2160 write_commit_info(s->id2, s->refs, s->repo, f);
2162 parent_ids = got_object_commit_get_parent_ids(commit2);
2163 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2164 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2165 write_commit_info(s->id2, s->refs,
2171 got_object_commit_close(commit2);
2173 err = got_diff_objects_as_commits(s->id1, s->id2,
2174 s->diff_context, s->repo, f);
2178 err = got_error(GOT_ERR_OBJ_TYPE);
2182 if (f && fflush(f) != 0 && err == NULL)
2183 err = got_error_prefix_errno("fflush");
2188 diff_view_indicate_progress(struct tog_view *view)
2190 werase(view->window);
2191 waddstr(view->window, "diffing...");
2196 static const struct got_error *
2197 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2198 struct got_object_id *id2, struct tog_view *log_view,
2199 struct got_reflist_head *refs, struct got_repository *repo)
2201 const struct got_error *err;
2203 if (id1 != NULL && id2 != NULL) {
2205 err = got_object_get_type(&type1, repo, id1);
2208 err = got_object_get_type(&type2, repo, id2);
2213 return got_error(GOT_ERR_OBJ_TYPE);
2217 view->state.diff.id1 = got_object_id_dup(id1);
2218 if (view->state.diff.id1 == NULL)
2219 return got_error_prefix_errno("got_object_id_dup");
2221 view->state.diff.id1 = NULL;
2223 view->state.diff.id2 = got_object_id_dup(id2);
2224 if (view->state.diff.id2 == NULL) {
2225 free(view->state.diff.id1);
2226 view->state.diff.id1 = NULL;
2227 return got_error_prefix_errno("got_object_id_dup");
2229 view->state.diff.f = NULL;
2230 view->state.diff.first_displayed_line = 1;
2231 view->state.diff.last_displayed_line = view->nlines;
2232 view->state.diff.diff_context = 3;
2233 view->state.diff.log_view = log_view;
2234 view->state.diff.repo = repo;
2235 view->state.diff.refs = refs;
2237 if (log_view && view_is_splitscreen(view))
2238 show_log_view(log_view); /* draw vborder */
2239 diff_view_indicate_progress(view);
2241 err = create_diff(&view->state.diff);
2243 free(view->state.diff.id1);
2244 view->state.diff.id1 = NULL;
2245 free(view->state.diff.id2);
2246 view->state.diff.id2 = NULL;
2250 view->show = show_diff_view;
2251 view->input = input_diff_view;
2252 view->close = close_diff_view;
2257 static const struct got_error *
2258 close_diff_view(struct tog_view *view)
2260 const struct got_error *err = NULL;
2262 free(view->state.diff.id1);
2263 view->state.diff.id1 = NULL;
2264 free(view->state.diff.id2);
2265 view->state.diff.id2 = NULL;
2266 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2267 err = got_error_prefix_errno("fclose");
2271 static const struct got_error *
2272 show_diff_view(struct tog_view *view)
2274 const struct got_error *err;
2275 struct tog_diff_view_state *s = &view->state.diff;
2276 char *id_str1 = NULL, *id_str2, *header;
2279 err = got_object_id_str(&id_str1, s->id1);
2283 err = got_object_id_str(&id_str2, s->id2);
2287 if (asprintf(&header, "diff %s %s",
2288 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2289 err = got_error_prefix_errno("asprintf");
2297 return draw_file(view, s->f, &s->first_displayed_line,
2298 &s->last_displayed_line, &s->eof, view->nlines,
2302 static const struct got_error *
2303 set_selected_commit(struct tog_diff_view_state *s,
2304 struct commit_queue_entry *entry)
2306 const struct got_error *err;
2307 const struct got_object_id_queue *parent_ids;
2308 struct got_commit_object *selected_commit;
2309 struct got_object_qid *pid;
2312 s->id2 = got_object_id_dup(entry->id);
2314 return got_error_prefix_errno("got_object_id_dup");
2316 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2319 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2321 pid = SIMPLEQ_FIRST(parent_ids);
2322 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2323 got_object_commit_close(selected_commit);
2327 static const struct got_error *
2328 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2329 struct tog_view **focus_view, struct tog_view *view, int ch)
2331 const struct got_error *err = NULL;
2332 struct tog_diff_view_state *s = &view->state.diff;
2333 struct tog_log_view_state *ls;
2334 struct commit_queue_entry *entry;
2340 if (s->first_displayed_line > 1)
2341 s->first_displayed_line--;
2346 if (s->first_displayed_line == 1) {
2351 while (i++ < view->nlines - 1 &&
2352 s->first_displayed_line > 1)
2353 s->first_displayed_line--;
2358 s->first_displayed_line++;
2369 while (!s->eof && i++ < view->nlines - 1) {
2371 line = parse_next_line(s->f, NULL);
2372 s->first_displayed_line++;
2378 if (s->diff_context > 0) {
2380 diff_view_indicate_progress(view);
2381 err = create_diff(s);
2385 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2387 diff_view_indicate_progress(view);
2388 err = create_diff(s);
2393 if (s->log_view == NULL)
2395 ls = &s->log_view->state.log;
2396 entry = TAILQ_PREV(ls->selected_entry,
2397 commit_queue_head, entry);
2401 err = input_log_view(NULL, NULL, NULL, s->log_view,
2406 err = set_selected_commit(s, entry);
2410 s->first_displayed_line = 1;
2411 s->last_displayed_line = view->nlines;
2413 diff_view_indicate_progress(view);
2414 err = create_diff(s);
2418 if (s->log_view == NULL)
2420 ls = &s->log_view->state.log;
2422 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2423 ls->thread_args.commits_needed++;
2425 /* Display "loading..." in log view. */
2426 show_log_view(s->log_view);
2430 err = trigger_log_thread(1 /* load_all */,
2431 &ls->thread_args.commits_needed,
2432 &ls->thread_args.log_complete,
2433 &ls->thread_args.need_commits);
2437 err = input_log_view(NULL, NULL, NULL, s->log_view,
2442 entry = TAILQ_NEXT(ls->selected_entry, entry);
2446 err = set_selected_commit(s, entry);
2450 s->first_displayed_line = 1;
2451 s->last_displayed_line = view->nlines;
2453 diff_view_indicate_progress(view);
2454 err = create_diff(s);
2463 static const struct got_error *
2464 cmd_diff(int argc, char *argv[])
2466 const struct got_error *error = NULL;
2467 struct got_repository *repo = NULL;
2468 struct got_reflist_head refs;
2469 struct got_object_id *id1 = NULL, *id2 = NULL;
2470 char *repo_path = NULL;
2471 char *id_str1 = NULL, *id_str2 = NULL;
2473 struct tog_view *view;
2475 SIMPLEQ_INIT(&refs);
2478 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2483 while ((ch = getopt(argc, argv, "")) != -1) {
2495 usage_diff(); /* TODO show local worktree changes */
2496 } else if (argc == 2) {
2497 repo_path = getcwd(NULL, 0);
2498 if (repo_path == NULL)
2499 return got_error_prefix_errno("getcwd");
2502 } else if (argc == 3) {
2503 repo_path = realpath(argv[0], NULL);
2504 if (repo_path == NULL)
2505 return got_error_prefix_errno2("realpath", argv[0]);
2513 error = got_repo_open(&repo, repo_path);
2517 error = apply_unveil(got_repo_get_path(repo), NULL);
2521 error = got_object_resolve_id_str(&id1, repo, id_str1);
2525 error = got_object_resolve_id_str(&id2, repo, id_str2);
2529 error = got_ref_list(&refs, repo);
2533 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2535 error = got_error_prefix_errno("view_open");
2538 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2541 error = view_loop(view);
2544 got_repo_close(repo);
2545 got_ref_list_free(&refs);
2553 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2558 struct tog_blame_line {
2560 struct got_object_id *id;
2563 static const struct got_error *
2564 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2565 const char *path, struct tog_blame_line *lines, int nlines,
2566 int blame_complete, int selected_line, int *first_displayed_line,
2567 int *last_displayed_line, int *eof, int max_lines)
2569 const struct got_error *err;
2570 int lineno = 0, nprinted = 0;
2575 struct tog_blame_line *blame_line;
2576 struct got_object_id *prev_id = NULL;
2579 err = got_object_id_str(&id_str, id);
2584 werase(view->window);
2586 if (asprintf(&line, "commit %s", id_str) == -1) {
2587 err = got_error_prefix_errno("asprintf");
2592 err = format_line(&wline, &width, line, view->ncols);
2595 if (view_needs_focus_indication(view))
2596 wstandout(view->window);
2597 waddwstr(view->window, wline);
2598 if (view_needs_focus_indication(view))
2599 wstandend(view->window);
2602 if (width < view->ncols)
2603 waddch(view->window, '\n');
2605 if (asprintf(&line, "[%d/%d] %s%s",
2606 *first_displayed_line - 1 + selected_line, nlines,
2607 blame_complete ? "" : "annotating... ", path) == -1) {
2609 return got_error_prefix_errno("asprintf");
2612 err = format_line(&wline, &width, line, view->ncols);
2617 waddwstr(view->window, wline);
2620 if (width < view->ncols)
2621 waddch(view->window, '\n');
2624 while (nprinted < max_lines - 2) {
2625 line = parse_next_line(f, &len);
2630 if (++lineno < *first_displayed_line) {
2635 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2636 err = format_line(&wline, &width, line, wlimit);
2642 if (view->focussed && nprinted == selected_line - 1)
2643 wstandout(view->window);
2645 blame_line = &lines[lineno - 1];
2646 if (blame_line->annotated && prev_id &&
2647 got_object_id_cmp(prev_id, blame_line->id) == 0)
2648 waddstr(view->window, " ");
2649 else if (blame_line->annotated) {
2651 err = got_object_id_str(&id_str, blame_line->id);
2657 wprintw(view->window, "%.8s ", id_str);
2659 prev_id = blame_line->id;
2661 waddstr(view->window, "........ ");
2665 waddwstr(view->window, wline);
2666 while (width < wlimit) {
2667 waddch(view->window, ' ');
2670 if (view->focussed && nprinted == selected_line - 1)
2671 wstandend(view->window);
2672 if (++nprinted == 1)
2673 *first_displayed_line = lineno;
2678 *last_displayed_line = lineno;
2685 static const struct got_error *
2686 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2688 const struct got_error *err = NULL;
2689 struct tog_blame_cb_args *a = arg;
2690 struct tog_blame_line *line;
2693 if (nlines != a->nlines ||
2694 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2695 return got_error(GOT_ERR_RANGE);
2697 errcode = pthread_mutex_lock(&tog_mutex);
2699 return got_error_set_errno(errcode, "pthread_mutex_lock");
2701 if (*a->quit) { /* user has quit the blame view */
2702 err = got_error(GOT_ERR_ITER_COMPLETED);
2707 goto done; /* no change in this commit */
2709 line = &a->lines[lineno - 1];
2710 if (line->annotated)
2713 line->id = got_object_id_dup(id);
2714 if (line->id == NULL) {
2715 err = got_error_prefix_errno("got_object_id_dup");
2718 line->annotated = 1;
2720 errcode = pthread_mutex_unlock(&tog_mutex);
2722 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2727 blame_thread(void *arg)
2729 const struct got_error *err;
2730 struct tog_blame_thread_args *ta = arg;
2731 struct tog_blame_cb_args *a = ta->cb_args;
2734 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2735 blame_cb, ta->cb_args);
2737 errcode = pthread_mutex_lock(&tog_mutex);
2739 return (void *)got_error_set_errno(errcode,
2740 "pthread_mutex_lock");
2742 got_repo_close(ta->repo);
2746 errcode = pthread_mutex_unlock(&tog_mutex);
2747 if (errcode && err == NULL)
2748 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2753 static struct got_object_id *
2754 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2757 struct tog_blame_line *line;
2759 line = &lines[first_displayed_line - 1 + selected_line - 1];
2760 if (!line->annotated)
2766 static const struct got_error *
2767 stop_blame(struct tog_blame *blame)
2769 const struct got_error *err = NULL;
2772 if (blame->thread) {
2774 errcode = pthread_mutex_unlock(&tog_mutex);
2776 return got_error_set_errno(errcode,
2777 "pthread_mutex_unlock");
2778 errcode = pthread_join(blame->thread, (void **)&err);
2780 return got_error_set_errno(errcode, "pthread_join");
2781 errcode = pthread_mutex_lock(&tog_mutex);
2783 return got_error_set_errno(errcode,
2784 "pthread_mutex_lock");
2785 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2787 blame->thread = NULL;
2789 if (blame->thread_args.repo) {
2790 got_repo_close(blame->thread_args.repo);
2791 blame->thread_args.repo = NULL;
2794 if (fclose(blame->f) != 0 && err == NULL)
2795 err = got_error_prefix_errno("fclose");
2799 for (i = 0; i < blame->nlines; i++)
2800 free(blame->lines[i].id);
2802 blame->lines = NULL;
2804 free(blame->cb_args.commit_id);
2805 blame->cb_args.commit_id = NULL;
2810 static const struct got_error *
2811 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2812 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2813 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL;
2817 struct got_blob_object *blob = NULL;
2818 struct got_repository *thread_repo = NULL;
2819 struct got_object_id *obj_id = NULL;
2822 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2826 return got_error(GOT_ERR_NO_OBJ);
2828 err = got_object_get_type(&obj_type, repo, obj_id);
2832 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2833 err = got_error(GOT_ERR_OBJ_TYPE);
2837 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2840 blame->f = got_opentemp();
2841 if (blame->f == NULL) {
2842 err = got_error_prefix_errno("got_opentemp");
2845 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2850 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2851 if (blame->lines == NULL) {
2852 err = got_error_prefix_errno("calloc");
2856 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2860 blame->cb_args.view = view;
2861 blame->cb_args.lines = blame->lines;
2862 blame->cb_args.nlines = blame->nlines;
2863 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2864 if (blame->cb_args.commit_id == NULL) {
2865 err = got_error_prefix_errno("got_object_id_dup");
2868 blame->cb_args.quit = done;
2870 blame->thread_args.path = path;
2871 blame->thread_args.repo = thread_repo;
2872 blame->thread_args.cb_args = &blame->cb_args;
2873 blame->thread_args.complete = blame_complete;
2874 *blame_complete = 0;
2878 got_object_blob_close(blob);
2885 static const struct got_error *
2886 open_blame_view(struct tog_view *view, char *path,
2887 struct got_object_id *commit_id, struct got_reflist_head *refs,
2888 struct got_repository *repo)
2890 const struct got_error *err = NULL;
2891 struct tog_blame_view_state *s = &view->state.blame;
2893 SIMPLEQ_INIT(&s->blamed_commits);
2895 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2899 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2900 s->first_displayed_line = 1;
2901 s->last_displayed_line = view->nlines;
2902 s->selected_line = 1;
2903 s->blame_complete = 0;
2905 if (s->path == NULL)
2906 return got_error_prefix_errno("open_blame_view");
2909 s->commit_id = commit_id;
2910 memset(&s->blame, 0, sizeof(s->blame));
2912 view->show = show_blame_view;
2913 view->input = input_blame_view;
2914 view->close = close_blame_view;
2916 return run_blame(&s->blame, view, &s->blame_complete,
2917 &s->first_displayed_line, &s->last_displayed_line,
2918 &s->selected_line, &s->done, &s->eof, s->path,
2919 s->blamed_commit->id, s->repo);
2922 static const struct got_error *
2923 close_blame_view(struct tog_view *view)
2925 const struct got_error *err = NULL;
2926 struct tog_blame_view_state *s = &view->state.blame;
2928 if (s->blame.thread)
2929 err = stop_blame(&s->blame);
2931 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2932 struct got_object_qid *blamed_commit;
2933 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2934 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2935 got_object_qid_free(blamed_commit);
2943 static const struct got_error *
2944 show_blame_view(struct tog_view *view)
2946 const struct got_error *err = NULL;
2947 struct tog_blame_view_state *s = &view->state.blame;
2950 if (s->blame.thread == NULL) {
2951 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2952 &s->blame.thread_args);
2954 return got_error_set_errno(errcode, "pthread_create");
2957 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2958 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2959 s->selected_line, &s->first_displayed_line,
2960 &s->last_displayed_line, &s->eof, view->nlines);
2966 static const struct got_error *
2967 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2968 struct tog_view **focus_view, struct tog_view *view, int ch)
2970 const struct got_error *err = NULL, *thread_err = NULL;
2971 struct tog_view *diff_view;
2972 struct tog_blame_view_state *s = &view->state.blame;
2981 if (s->selected_line > 1)
2983 else if (s->selected_line == 1 &&
2984 s->first_displayed_line > 1)
2985 s->first_displayed_line--;
2990 if (s->first_displayed_line == 1) {
2991 if (s->selected_line == 1) {
2995 s->selected_line = 1;
2998 if (s->first_displayed_line > view->nlines - 2)
2999 s->first_displayed_line -=
3002 s->first_displayed_line = 1;
3006 if (s->selected_line < view->nlines - 2 &&
3007 s->first_displayed_line +
3008 s->selected_line <= s->blame.nlines)
3010 else if (s->last_displayed_line <
3012 s->first_displayed_line++;
3018 struct got_object_id *id = NULL;
3019 id = get_selected_commit_id(s->blame.lines,
3020 s->first_displayed_line, s->selected_line);
3024 struct got_commit_object *commit;
3025 struct got_object_qid *pid;
3026 struct got_object_id *blob_id = NULL;
3028 err = got_object_open_as_commit(&commit,
3032 pid = SIMPLEQ_FIRST(
3033 got_object_commit_get_parent_ids(commit));
3035 got_object_commit_close(commit);
3038 /* Check if path history ends here. */
3039 err = got_object_id_by_path(&blob_id, s->repo,
3042 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3044 got_object_commit_close(commit);
3047 err = got_object_get_type(&obj_type, s->repo,
3050 /* Can't blame non-blob type objects. */
3051 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3052 got_object_commit_close(commit);
3055 err = got_object_qid_alloc(&s->blamed_commit,
3057 got_object_commit_close(commit);
3059 if (got_object_id_cmp(id,
3060 s->blamed_commit->id) == 0)
3062 err = got_object_qid_alloc(&s->blamed_commit,
3068 thread_err = stop_blame(&s->blame);
3072 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3073 s->blamed_commit, entry);
3074 err = run_blame(&s->blame, view, &s->blame_complete,
3075 &s->first_displayed_line, &s->last_displayed_line,
3076 &s->selected_line, &s->done, &s->eof,
3077 s->path, s->blamed_commit->id, s->repo);
3083 struct got_object_qid *first;
3084 first = SIMPLEQ_FIRST(&s->blamed_commits);
3085 if (!got_object_id_cmp(first->id, s->commit_id))
3088 thread_err = stop_blame(&s->blame);
3092 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3093 got_object_qid_free(s->blamed_commit);
3095 SIMPLEQ_FIRST(&s->blamed_commits);
3096 err = run_blame(&s->blame, view, &s->blame_complete,
3097 &s->first_displayed_line, &s->last_displayed_line,
3098 &s->selected_line, &s->done, &s->eof, s->path,
3099 s->blamed_commit->id, s->repo);
3106 struct got_object_id *id = NULL;
3107 struct got_object_qid *pid;
3108 struct got_commit_object *commit = NULL;
3109 id = get_selected_commit_id(s->blame.lines,
3110 s->first_displayed_line, s->selected_line);
3113 err = got_object_open_as_commit(&commit, s->repo, id);
3116 pid = SIMPLEQ_FIRST(
3117 got_object_commit_get_parent_ids(commit));
3118 if (view_is_parent_view(view))
3119 begin_x = view_split_begin_x(view->begin_x);
3120 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3121 if (diff_view == NULL) {
3122 got_object_commit_close(commit);
3123 err = got_error_prefix_errno("view_open");
3126 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3127 id, NULL, s->refs, s->repo);
3128 got_object_commit_close(commit);
3130 view_close(diff_view);
3133 if (view_is_parent_view(view)) {
3134 err = view_close_child(view);
3137 err = view_set_child(view, diff_view);
3139 view_close(diff_view);
3142 *focus_view = diff_view;
3143 view->child_focussed = 1;
3145 *new_view = diff_view;
3152 if (s->last_displayed_line >= s->blame.nlines &&
3153 s->selected_line >= MIN(s->blame.nlines,
3154 view->nlines - 2)) {
3158 if (s->last_displayed_line >= s->blame.nlines &&
3159 s->selected_line < view->nlines - 2) {
3160 s->selected_line = MIN(s->blame.nlines,
3164 if (s->last_displayed_line + view->nlines - 2
3166 s->first_displayed_line +=
3169 s->first_displayed_line =
3174 if (s->selected_line > view->nlines - 2) {
3175 s->selected_line = MIN(s->blame.nlines,
3182 return thread_err ? thread_err : err;
3185 static const struct got_error *
3186 cmd_blame(int argc, char *argv[])
3188 const struct got_error *error;
3189 struct got_repository *repo = NULL;
3190 struct got_reflist_head refs;
3191 struct got_worktree *worktree = NULL;
3192 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3193 struct got_object_id *commit_id = NULL;
3194 char *commit_id_str = NULL;
3196 struct tog_view *view;
3198 SIMPLEQ_INIT(&refs);
3201 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3206 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3209 commit_id_str = optarg;
3212 repo_path = realpath(optarg, NULL);
3213 if (repo_path == NULL)
3214 err(1, "-r option");
3230 cwd = getcwd(NULL, 0);
3232 error = got_error_prefix_errno("getcwd");
3235 if (repo_path == NULL) {
3236 error = got_worktree_open(&worktree, cwd);
3237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3243 strdup(got_worktree_get_repo_path(worktree));
3244 if (repo_path == NULL)
3245 error = got_error_prefix_errno("strdup");
3249 repo_path = strdup(cwd);
3250 if (repo_path == NULL) {
3251 error = got_error_prefix_errno("strdup");
3259 error = got_repo_open(&repo, repo_path);
3263 error = apply_unveil(got_repo_get_path(repo), NULL);
3268 const char *prefix = got_worktree_get_path_prefix(worktree);
3269 char *p, *worktree_subdir = cwd +
3270 strlen(got_worktree_get_root_path(worktree));
3271 if (asprintf(&p, "%s%s%s%s%s",
3272 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3273 worktree_subdir, worktree_subdir[0] ? "/" : "",
3275 error = got_error_prefix_errno("asprintf");
3278 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3281 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3286 if (commit_id_str == NULL) {
3287 struct got_reference *head_ref;
3288 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3291 error = got_ref_resolve(&commit_id, repo, head_ref);
3292 got_ref_close(head_ref);
3294 error = got_object_resolve_id_str(&commit_id, repo,
3300 error = got_ref_list(&refs, repo);
3304 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3306 error = got_error_prefix_errno("view_open");
3309 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3312 error = view_loop(view);
3318 got_worktree_close(worktree);
3320 got_repo_close(repo);
3321 got_ref_list_free(&refs);
3325 static const struct got_error *
3326 draw_tree_entries(struct tog_view *view,
3327 struct got_tree_entry **first_displayed_entry,
3328 struct got_tree_entry **last_displayed_entry,
3329 struct got_tree_entry **selected_entry, int *ndisplayed,
3330 const char *label, int show_ids, const char *parent_path,
3331 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3333 const struct got_error *err = NULL;
3334 struct got_tree_entry *te;
3340 werase(view->window);
3345 err = format_line(&wline, &width, label, view->ncols);
3348 if (view_needs_focus_indication(view))
3349 wstandout(view->window);
3350 waddwstr(view->window, wline);
3351 if (view_needs_focus_indication(view))
3352 wstandend(view->window);
3355 if (width < view->ncols)
3356 waddch(view->window, '\n');
3359 err = format_line(&wline, &width, parent_path, view->ncols);
3362 waddwstr(view->window, wline);
3365 if (width < view->ncols)
3366 waddch(view->window, '\n');
3369 waddch(view->window, '\n');
3373 te = SIMPLEQ_FIRST(&entries->head);
3374 if (*first_displayed_entry == NULL) {
3375 if (selected == 0) {
3377 wstandout(view->window);
3378 *selected_entry = NULL;
3380 waddstr(view->window, " ..\n"); /* parent directory */
3381 if (selected == 0 && view->focussed)
3382 wstandend(view->window);
3389 while (te != *first_displayed_entry)
3390 te = SIMPLEQ_NEXT(te, entry);
3394 char *line = NULL, *id_str = NULL;
3397 err = got_object_id_str(&id_str, te->id);
3399 return got_error_prefix_errno(
3400 "got_object_id_str");
3402 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3403 te->name, S_ISDIR(te->mode) ? "/" :
3404 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3406 return got_error_prefix_errno("asprintf");
3409 err = format_line(&wline, &width, line, view->ncols);
3414 if (n == selected) {
3416 wstandout(view->window);
3417 *selected_entry = te;
3419 waddwstr(view->window, wline);
3420 if (width < view->ncols)
3421 waddch(view->window, '\n');
3422 if (n == selected && view->focussed)
3423 wstandend(view->window);
3429 *last_displayed_entry = te;
3432 te = SIMPLEQ_NEXT(te, entry);
3439 tree_scroll_up(struct tog_view *view,
3440 struct got_tree_entry **first_displayed_entry, int maxscroll,
3441 const struct got_tree_entries *entries, int isroot)
3443 struct got_tree_entry *te, *prev;
3446 if (*first_displayed_entry == NULL) {
3451 te = SIMPLEQ_FIRST(&entries->head);
3452 if (*first_displayed_entry == te) {
3455 *first_displayed_entry = NULL;
3459 /* XXX this is stupid... switch to TAILQ? */
3460 for (i = 0; i < maxscroll; i++) {
3461 while (te != *first_displayed_entry) {
3463 te = SIMPLEQ_NEXT(te, entry);
3465 *first_displayed_entry = prev;
3466 te = SIMPLEQ_FIRST(&entries->head);
3468 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3469 *first_displayed_entry = NULL;
3473 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3474 struct got_tree_entry *last_displayed_entry,
3475 const struct got_tree_entries *entries)
3477 struct got_tree_entry *next, *last;
3480 if (*first_displayed_entry)
3481 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3483 next = SIMPLEQ_FIRST(&entries->head);
3484 last = last_displayed_entry;
3485 while (next && last && n++ < maxscroll) {
3486 last = SIMPLEQ_NEXT(last, entry);
3488 *first_displayed_entry = next;
3489 next = SIMPLEQ_NEXT(next, entry);
3495 static const struct got_error *
3496 tree_entry_path(char **path, struct tog_parent_trees *parents,
3497 struct got_tree_entry *te)
3499 const struct got_error *err = NULL;
3500 struct tog_parent_tree *pt;
3501 size_t len = 2; /* for leading slash and NUL */
3503 TAILQ_FOREACH(pt, parents, entry)
3504 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3506 len += strlen(te->name);
3508 *path = calloc(1, len);
3510 return got_error_prefix_errno("calloc");
3513 pt = TAILQ_LAST(parents, tog_parent_trees);
3515 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3516 err = got_error(GOT_ERR_NO_SPACE);
3519 if (strlcat(*path, "/", len) >= len) {
3520 err = got_error(GOT_ERR_NO_SPACE);
3523 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3526 if (strlcat(*path, te->name, len) >= len) {
3527 err = got_error(GOT_ERR_NO_SPACE);
3539 static const struct got_error *
3540 blame_tree_entry(struct tog_view **new_view, int begin_x,
3541 struct got_tree_entry *te, struct tog_parent_trees *parents,
3542 struct got_object_id *commit_id, struct got_reflist_head *refs,
3543 struct got_repository *repo)
3545 const struct got_error *err = NULL;
3547 struct tog_view *blame_view;
3549 err = tree_entry_path(&path, parents, te);
3553 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3554 if (blame_view == NULL)
3555 return got_error_prefix_errno("view_open");
3557 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3559 view_close(blame_view);
3562 *new_view = blame_view;
3566 static const struct got_error *
3567 log_tree_entry(struct tog_view **new_view, int begin_x,
3568 struct got_tree_entry *te, struct tog_parent_trees *parents,
3569 struct got_object_id *commit_id, struct got_reflist_head *refs,
3570 struct got_repository *repo)
3572 struct tog_view *log_view;
3573 const struct got_error *err = NULL;
3576 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3577 if (log_view == NULL)
3578 return got_error_prefix_errno("view_open");
3580 err = tree_entry_path(&path, parents, te);
3584 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3586 view_close(log_view);
3588 *new_view = log_view;
3593 static const struct got_error *
3594 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3595 struct got_object_id *commit_id, struct got_reflist_head *refs,
3596 struct got_repository *repo)
3598 const struct got_error *err = NULL;
3599 char *commit_id_str = NULL;
3600 struct tog_tree_view_state *s = &view->state.tree;
3602 TAILQ_INIT(&s->parents);
3604 err = got_object_id_str(&commit_id_str, commit_id);
3608 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3609 err = got_error_prefix_errno("asprintf");
3613 s->root = s->tree = root;
3614 s->entries = got_object_tree_get_entries(root);
3615 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3616 s->commit_id = got_object_id_dup(commit_id);
3617 if (s->commit_id == NULL) {
3618 err = got_error_prefix_errno("got_object_id_dup");
3624 view->show = show_tree_view;
3625 view->input = input_tree_view;
3626 view->close = close_tree_view;
3628 free(commit_id_str);
3630 free(s->tree_label);
3631 s->tree_label = NULL;
3636 static const struct got_error *
3637 close_tree_view(struct tog_view *view)
3639 struct tog_tree_view_state *s = &view->state.tree;
3641 free(s->tree_label);
3642 s->tree_label = NULL;
3644 s->commit_id = NULL;
3645 while (!TAILQ_EMPTY(&s->parents)) {
3646 struct tog_parent_tree *parent;
3647 parent = TAILQ_FIRST(&s->parents);
3648 TAILQ_REMOVE(&s->parents, parent, entry);
3652 if (s->tree != s->root)
3653 got_object_tree_close(s->tree);
3654 got_object_tree_close(s->root);
3659 static const struct got_error *
3660 show_tree_view(struct tog_view *view)
3662 const struct got_error *err = NULL;
3663 struct tog_tree_view_state *s = &view->state.tree;
3666 err = tree_entry_path(&parent_path, &s->parents, NULL);
3670 err = draw_tree_entries(view, &s->first_displayed_entry,
3671 &s->last_displayed_entry, &s->selected_entry,
3672 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3673 s->entries, s->selected, view->nlines, s->tree == s->root);
3680 static const struct got_error *
3681 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3682 struct tog_view **focus_view, struct tog_view *view, int ch)
3684 const struct got_error *err = NULL;
3685 struct tog_tree_view_state *s = &view->state.tree;
3686 struct tog_view *log_view;
3687 int begin_x = 0, nscrolled;
3691 s->show_ids = !s->show_ids;
3694 if (!s->selected_entry)
3696 if (view_is_parent_view(view))
3697 begin_x = view_split_begin_x(view->begin_x);
3698 err = log_tree_entry(&log_view, begin_x,
3699 s->selected_entry, &s->parents,
3700 s->commit_id, s->refs, s->repo);
3701 if (view_is_parent_view(view)) {
3702 err = view_close_child(view);
3705 err = view_set_child(view, log_view);
3707 view_close(log_view);
3710 *focus_view = log_view;
3711 view->child_focussed = 1;
3713 *new_view = log_view;
3717 if (s->selected > 0) {
3719 if (s->selected == 0)
3722 if (s->selected > 0)
3724 tree_scroll_up(view, &s->first_displayed_entry, 1,
3725 s->entries, s->tree == s->root);
3728 tree_scroll_up(view, &s->first_displayed_entry,
3729 MAX(0, view->nlines - 4 - s->selected), s->entries,
3730 s->tree == s->root);
3732 if (SIMPLEQ_FIRST(&s->entries->head) ==
3733 s->first_displayed_entry && s->tree != s->root)
3734 s->first_displayed_entry = NULL;
3738 if (s->selected < s->ndisplayed - 1) {
3742 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3744 /* can't scroll any further */
3748 tree_scroll_down(&s->first_displayed_entry, 1,
3749 s->last_displayed_entry, s->entries);
3752 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3754 /* can't scroll any further; move cursor down */
3755 if (s->selected < s->ndisplayed - 1)
3756 s->selected = s->ndisplayed - 1;
3761 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3762 view->nlines, s->last_displayed_entry, s->entries);
3763 if (nscrolled < view->nlines) {
3765 struct got_tree_entry *te;
3766 te = s->first_displayed_entry;
3769 te = SIMPLEQ_NEXT(te, entry);
3771 s->selected = ndisplayed - 1;
3776 if (s->selected_entry == NULL) {
3777 struct tog_parent_tree *parent;
3779 /* user selected '..' */
3780 if (s->tree == s->root)
3782 parent = TAILQ_FIRST(&s->parents);
3783 TAILQ_REMOVE(&s->parents, parent,
3785 got_object_tree_close(s->tree);
3786 s->tree = parent->tree;
3788 got_object_tree_get_entries(s->tree);
3789 s->first_displayed_entry =
3790 parent->first_displayed_entry;
3792 parent->selected_entry;
3793 s->selected = parent->selected;
3795 } else if (S_ISDIR(s->selected_entry->mode)) {
3796 struct tog_parent_tree *parent;
3797 struct got_tree_object *child;
3798 err = got_object_open_as_tree(&child,
3799 s->repo, s->selected_entry->id);
3802 parent = calloc(1, sizeof(*parent));
3803 if (parent == NULL) {
3804 err = got_error_prefix_errno("calloc");
3807 parent->tree = s->tree;
3808 parent->first_displayed_entry =
3809 s->first_displayed_entry;
3810 parent->selected_entry = s->selected_entry;
3811 parent->selected = s->selected;
3812 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3815 got_object_tree_get_entries(s->tree);
3817 s->first_displayed_entry = NULL;
3818 } else if (S_ISREG(s->selected_entry->mode)) {
3819 struct tog_view *blame_view;
3820 int begin_x = view_is_parent_view(view) ?
3821 view_split_begin_x(view->begin_x) : 0;
3823 err = blame_tree_entry(&blame_view, begin_x,
3824 s->selected_entry, &s->parents,
3825 s->commit_id, s->refs, s->repo);
3828 if (view_is_parent_view(view)) {
3829 err = view_close_child(view);
3832 err = view_set_child(view, blame_view);
3834 view_close(blame_view);
3837 *focus_view = blame_view;
3838 view->child_focussed = 1;
3840 *new_view = blame_view;
3844 if (s->selected > view->nlines)
3845 s->selected = s->ndisplayed - 1;
3858 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3863 static const struct got_error *
3864 cmd_tree(int argc, char *argv[])
3866 const struct got_error *error;
3867 struct got_repository *repo = NULL;
3868 struct got_reflist_head refs;
3869 char *repo_path = NULL;
3870 struct got_object_id *commit_id = NULL;
3871 char *commit_id_arg = NULL;
3872 struct got_commit_object *commit = NULL;
3873 struct got_tree_object *tree = NULL;
3875 struct tog_view *view;
3877 SIMPLEQ_INIT(&refs);
3880 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3885 while ((ch = getopt(argc, argv, "c:")) != -1) {
3888 commit_id_arg = optarg;
3900 struct got_worktree *worktree;
3901 char *cwd = getcwd(NULL, 0);
3903 return got_error_prefix_errno("getcwd");
3904 error = got_worktree_open(&worktree, cwd);
3905 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3910 strdup(got_worktree_get_repo_path(worktree));
3911 got_worktree_close(worktree);
3914 if (repo_path == NULL) {
3915 error = got_error_prefix_errno("strdup");
3918 } else if (argc == 1) {
3919 repo_path = realpath(argv[0], NULL);
3920 if (repo_path == NULL)
3921 return got_error_prefix_errno2("realpath", argv[0]);
3927 error = got_repo_open(&repo, repo_path);
3931 error = apply_unveil(got_repo_get_path(repo), NULL);
3935 if (commit_id_arg == NULL)
3936 error = get_head_commit_id(&commit_id, repo);
3938 error = got_object_resolve_id_str(&commit_id, repo,
3943 error = got_object_open_as_commit(&commit, repo, commit_id);
3947 error = got_object_open_as_tree(&tree, repo,
3948 got_object_commit_get_tree_id(commit));
3952 error = got_ref_list(&refs, repo);
3956 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3958 error = got_error_prefix_errno("view_open");
3961 error = open_tree_view(view, tree, commit_id, &refs, repo);
3964 error = view_loop(view);
3969 got_object_commit_close(commit);
3971 got_object_tree_close(tree);
3973 got_repo_close(repo);
3974 got_ref_list_free(&refs);
3983 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3984 "Available commands:\n", getprogname());
3985 for (i = 0; i < nitems(tog_commands); i++) {
3986 struct tog_cmd *cmd = &tog_commands[i];
3987 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3993 make_argv(const char *arg0, const char *arg1)
3996 int argc = (arg1 == NULL ? 1 : 2);
3998 argv = calloc(argc, sizeof(char *));
4001 argv[0] = strdup(arg0);
4002 if (argv[0] == NULL)
4005 argv[1] = strdup(arg1);
4006 if (argv[1] == NULL)
4014 main(int argc, char *argv[])
4016 const struct got_error *error = NULL;
4017 struct tog_cmd *cmd = NULL;
4019 char **cmd_argv = NULL;
4021 setlocale(LC_CTYPE, "");
4023 while ((ch = getopt(argc, argv, "h")) != -1) {
4042 /* Build an argument vector which runs a default command. */
4043 cmd = &tog_commands[0];
4044 cmd_argv = make_argv(cmd->name, NULL);
4049 /* Did the user specific a command? */
4050 for (i = 0; i < nitems(tog_commands); i++) {
4051 if (strncmp(tog_commands[i].name, argv[0],
4052 strlen(argv[0])) == 0) {
4053 cmd = &tog_commands[i];
4055 tog_commands[i].cmd_usage();
4060 /* Did the user specify a repository? */
4061 char *repo_path = realpath(argv[0], NULL);
4063 struct got_repository *repo;
4064 error = got_repo_open(&repo, repo_path);
4066 got_repo_close(repo);
4068 error = got_error_prefix_errno2("realpath",
4072 fprintf(stderr, "%s: '%s' is not a "
4073 "known command\n", getprogname(),
4077 fprintf(stderr, "%s: '%s' is neither a known "
4078 "command nor a path to a repository\n",
4079 getprogname(), argv[0]);
4083 cmd = &tog_commands[0];
4084 cmd_argv = make_argv(cmd->name, repo_path);
4090 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4097 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);