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))
61 #define CTRL(x) ((x) & 0x1f)
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 const struct got_error *(*cmd_main)(int, char *[]);
70 void (*cmd_usage)(void);
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log,
87 "show repository history" },
88 { "diff", cmd_diff, usage_diff,
89 "compare files and directories" },
90 { "blame", cmd_blame, usage_blame,
91 "show line-by-line file history" },
92 { "tree", cmd_tree, usage_tree,
93 "browse trees in repository" },
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 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
158 struct tog_log_thread_args thread_args;
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
165 struct tog_view *view;
166 struct got_object_id *commit_id;
170 struct tog_blame_thread_args {
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
180 struct tog_blame_line *lines;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
198 struct got_repository *repo;
199 struct got_reflist_head *refs;
200 struct got_object_id *commit_id;
201 struct tog_blame blame;
204 struct tog_parent_tree {
205 TAILQ_ENTRY(tog_parent_tree) entry;
206 struct got_tree_object *tree;
207 struct got_tree_entry *first_displayed_entry;
208 struct got_tree_entry *selected_entry;
212 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
214 struct tog_tree_view_state {
216 struct got_tree_object *root;
217 struct got_tree_object *tree;
218 const struct got_tree_entries *entries;
219 struct got_tree_entry *first_displayed_entry;
220 struct got_tree_entry *last_displayed_entry;
221 struct got_tree_entry *selected_entry;
222 int ndisplayed, selected, show_ids;
223 struct tog_parent_trees parents;
224 struct got_object_id *commit_id;
225 struct got_repository *repo;
226 struct got_reflist_head *refs;
230 * We implement two types of views: parent views and child views.
232 * The 'Tab' key switches between a parent view and its child view.
233 * Child views are shown side-by-side to their parent view, provided
234 * there is enough screen estate.
236 * When a new view is opened from within a parent view, this new view
237 * becomes a child view of the parent view, replacing any existing child.
239 * When a new view is opened from within a child view, this new view
240 * becomes a parent view which will obscure the views below until the
241 * user quits the new parent view by typing 'q'.
243 * This list of views contains parent views only.
244 * Child views are only pointed to by their parent view.
246 TAILQ_HEAD(tog_view_list_head, tog_view);
249 TAILQ_ENTRY(tog_view) entry;
252 int nlines, ncols, begin_y, begin_x;
253 int lines, cols; /* copies of LINES and COLS */
255 struct tog_view *parent;
256 struct tog_view *child;
259 /* type-specific state */
260 enum tog_view_type type;
262 struct tog_diff_view_state diff;
263 struct tog_log_view_state log;
264 struct tog_blame_view_state blame;
265 struct tog_tree_view_state tree;
268 const struct got_error *(*show)(struct tog_view *);
269 const struct got_error *(*input)(struct tog_view **,
270 struct tog_view **, struct tog_view**, struct tog_view *, int);
271 const struct got_error *(*close)(struct tog_view *);
274 static const struct got_error *open_diff_view(struct tog_view *,
275 struct got_object_id *, struct got_object_id *, struct tog_view *,
276 struct got_reflist_head *, struct got_repository *);
277 static const struct got_error *show_diff_view(struct tog_view *);
278 static const struct got_error *input_diff_view(struct tog_view **,
279 struct tog_view **, struct tog_view **, struct tog_view *, int);
280 static const struct got_error* close_diff_view(struct tog_view *);
282 static const struct got_error *open_log_view(struct tog_view *,
283 struct got_object_id *, struct got_reflist_head *,
284 struct got_repository *, const char *, int);
285 static const struct got_error * show_log_view(struct tog_view *);
286 static const struct got_error *input_log_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_log_view(struct tog_view *);
290 static const struct got_error *open_blame_view(struct tog_view *, char *,
291 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
292 static const struct got_error *show_blame_view(struct tog_view *);
293 static const struct got_error *input_blame_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_blame_view(struct tog_view *);
297 static const struct got_error *open_tree_view(struct tog_view *,
298 struct got_tree_object *, struct got_object_id *,
299 struct got_reflist_head *, struct got_repository *);
300 static const struct got_error *show_tree_view(struct tog_view *);
301 static const struct got_error *input_tree_view(struct tog_view **,
302 struct tog_view **, struct tog_view **, struct tog_view *, int);
303 static const struct got_error *close_tree_view(struct tog_view *);
305 static volatile sig_atomic_t tog_sigwinch_received;
308 tog_sigwinch(int signo)
310 tog_sigwinch_received = 1;
313 static const struct got_error *
314 view_close(struct tog_view *view)
316 const struct got_error *err = NULL;
319 view_close(view->child);
323 err = view->close(view);
325 del_panel(view->panel);
327 delwin(view->window);
332 static struct tog_view *
333 view_open(int nlines, int ncols, int begin_y, int begin_x,
334 enum tog_view_type type)
336 struct tog_view *view = calloc(1, sizeof(*view));
344 view->nlines = nlines ? nlines : LINES - begin_y;
345 view->ncols = ncols ? ncols : COLS - begin_x;
346 view->begin_y = begin_y;
347 view->begin_x = begin_x;
348 view->window = newwin(nlines, ncols, begin_y, begin_x);
349 if (view->window == NULL) {
353 view->panel = new_panel(view->window);
354 if (view->panel == NULL ||
355 set_panel_userptr(view->panel, view) != OK) {
360 keypad(view->window, TRUE);
365 view_split_begin_x(int begin_x)
367 if (begin_x > 0 || COLS < 120)
369 return (COLS - MAX(COLS / 2, 80));
372 static const struct got_error *view_resize(struct tog_view *);
374 static const struct got_error *
375 view_splitscreen(struct tog_view *view)
377 const struct got_error *err = NULL;
380 view->begin_x = view_split_begin_x(0);
381 view->nlines = LINES;
382 view->ncols = COLS - view->begin_x;
385 err = view_resize(view);
389 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
390 return got_error_prefix_errno("mvwin");
395 static const struct got_error *
396 view_fullscreen(struct tog_view *view)
398 const struct got_error *err = NULL;
402 view->nlines = LINES;
406 err = view_resize(view);
410 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
411 return got_error_prefix_errno("mvwin");
417 view_is_parent_view(struct tog_view *view)
419 return view->parent == NULL;
422 static const struct got_error *
423 view_resize(struct tog_view *view)
427 if (view->lines > LINES)
428 nlines = view->nlines - (view->lines - LINES);
430 nlines = view->nlines + (LINES - view->lines);
432 if (view->cols > COLS)
433 ncols = view->ncols - (view->cols - COLS);
435 ncols = view->ncols + (COLS - view->cols);
437 if (wresize(view->window, nlines, ncols) == ERR)
438 return got_error_prefix_errno("wresize");
439 if (replace_panel(view->panel, view->window) == ERR)
440 return got_error_prefix_errno("replace_panel");
441 wclear(view->window);
443 view->nlines = nlines;
449 view->child->begin_x = view_split_begin_x(view->begin_x);
450 if (view->child->begin_x == 0) {
451 view_fullscreen(view->child);
452 if (view->child->focussed)
453 show_panel(view->child->panel);
455 show_panel(view->panel);
457 view_splitscreen(view->child);
458 show_panel(view->child->panel);
465 static const struct got_error *
466 view_close_child(struct tog_view *view)
468 const struct got_error *err = NULL;
470 if (view->child == NULL)
473 err = view_close(view->child);
478 static const struct got_error *
479 view_set_child(struct tog_view *view, struct tog_view *child)
481 const struct got_error *err = NULL;
484 child->parent = view;
489 view_is_splitscreen(struct tog_view *view)
491 return view->begin_x > 0;
500 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
501 cols = 80; /* Default */
507 resize_term(lines, cols);
510 static const struct got_error *
511 view_input(struct tog_view **new, struct tog_view **dead,
512 struct tog_view **focus, int *done, struct tog_view *view,
513 struct tog_view_list_head *views)
515 const struct got_error *err = NULL;
523 nodelay(stdscr, FALSE);
524 /* Allow threads to make progress while we are waiting for input. */
525 errcode = pthread_mutex_unlock(&tog_mutex);
527 return got_error_set_errno(errcode, "pthread_mutex_unlock");
528 ch = wgetch(view->window);
529 errcode = pthread_mutex_lock(&tog_mutex);
531 return got_error_set_errno(errcode, "pthread_mutex_lock");
532 nodelay(stdscr, TRUE);
534 if (tog_sigwinch_received) {
536 tog_sigwinch_received = 0;
537 TAILQ_FOREACH(v, views, entry) {
538 err = view_resize(v);
541 err = v->input(new, dead, focus, v, KEY_RESIZE);
552 *focus = view->child;
553 view->child_focussed = 1;
554 } else if (view->parent) {
555 *focus = view->parent;
556 view->parent->child_focussed = 0;
560 err = view->input(new, dead, focus, view, ch);
567 if (view_is_parent_view(view)) {
568 if (view->child == NULL)
570 if (view_is_splitscreen(view->child)) {
571 *focus = view->child;
572 view->child_focussed = 1;
573 err = view_fullscreen(view->child);
575 err = view_splitscreen(view->child);
578 err = view->child->input(new, dead, focus,
579 view->child, KEY_RESIZE);
581 if (view_is_splitscreen(view)) {
583 view->parent->child_focussed = 1;
584 err = view_fullscreen(view);
586 err = view_splitscreen(view);
590 err = view->input(new, dead, focus, view,
597 err = view->input(new, dead, focus, view, ch);
605 view_vborder(struct tog_view *view)
608 struct tog_view *view_above;
611 return view_vborder(view->parent);
613 panel = panel_above(view->panel);
617 view_above = panel_userptr(panel);
618 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
619 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
623 view_needs_focus_indication(struct tog_view *view)
625 if (view_is_parent_view(view)) {
626 if (view->child == NULL || view->child_focussed)
628 if (!view_is_splitscreen(view->child))
630 } else if (!view_is_splitscreen(view))
633 return view->focussed;
636 static const struct got_error *
637 view_loop(struct tog_view *view)
639 const struct got_error *err = NULL;
640 struct tog_view_list_head views;
641 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
642 int fast_refresh = 10;
643 int done = 0, errcode;
645 errcode = pthread_mutex_lock(&tog_mutex);
647 return got_error_set_errno(errcode, "pthread_mutex_lock");
650 TAILQ_INSERT_HEAD(&views, view, entry);
654 err = view->show(view);
659 while (!TAILQ_EMPTY(&views) && !done) {
660 /* Refresh fast during initialization, then become slower. */
661 if (fast_refresh && fast_refresh-- == 0)
662 halfdelay(10); /* switch to once per second */
664 err = view_input(&new_view, &dead_view, &focus_view, &done,
669 struct tog_view *prev = NULL;
671 if (view_is_parent_view(dead_view))
672 prev = TAILQ_PREV(dead_view,
673 tog_view_list_head, entry);
674 else if (view->parent != dead_view)
677 if (dead_view->parent)
678 dead_view->parent->child = NULL;
680 TAILQ_REMOVE(&views, dead_view, entry);
682 err = view_close(dead_view);
683 if (err || dead_view == main_view)
686 if (view == dead_view) {
691 else if (!TAILQ_EMPTY(&views))
692 view = TAILQ_LAST(&views,
697 if (view->child && view->child_focussed)
698 focus_view = view->child;
705 struct tog_view *v, *t;
706 /* Only allow one parent view per type. */
707 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
708 if (v->type != new_view->type)
710 TAILQ_REMOVE(&views, v, entry);
716 TAILQ_INSERT_TAIL(&views, new_view, entry);
718 if (focus_view == NULL)
719 focus_view = new_view;
722 show_panel(focus_view->panel);
725 focus_view->focussed = 1;
728 show_panel(new_view->panel);
729 if (view->child && view_is_splitscreen(view->child))
730 show_panel(view->child->panel);
733 if (focus_view == NULL) {
735 show_panel(view->panel);
736 if (view->child && view_is_splitscreen(view->child))
737 show_panel(view->child->panel);
741 err = view->parent->show(view->parent);
745 err = view->show(view);
749 err = view->child->show(view->child);
758 while (!TAILQ_EMPTY(&views)) {
759 view = TAILQ_FIRST(&views);
760 TAILQ_REMOVE(&views, view, entry);
764 errcode = pthread_mutex_unlock(&tog_mutex);
766 return got_error_set_errno(errcode, "pthread_mutex_unlock");
776 "usage: %s log [-c commit] [-r repository-path] [path]\n",
781 /* Create newly allocated wide-character string equivalent to a byte string. */
782 static const struct got_error *
783 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
786 const struct got_error *err = NULL;
789 *wlen = mbstowcs(NULL, s, 0);
790 if (*wlen == (size_t)-1) {
793 return got_error_prefix_errno("mbstowcs");
795 /* byte string invalid in current encoding; try to "fix" it */
796 err = got_mbsavis(&vis, &vislen, s);
799 *wlen = mbstowcs(NULL, vis, 0);
800 if (*wlen == (size_t)-1) {
801 err = got_error_prefix_errno("mbstowcs"); /* give up */
806 *ws = calloc(*wlen + 1, sizeof(*ws));
808 err = got_error_prefix_errno("calloc");
812 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
813 err = got_error_prefix_errno("mbstowcs");
824 /* Format a line for display, ensuring that it won't overflow a width limit. */
825 static const struct got_error *
826 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
828 const struct got_error *err = NULL;
830 wchar_t *wline = NULL;
837 err = mbs2ws(&wline, &wlen, line);
842 while (i < wlen && cols < wlimit) {
843 int width = wcwidth(wline[i]);
850 if (cols + width <= wlimit)
855 if (wline[i] == L'\t')
856 cols += TABSIZE - ((cols + 1) % TABSIZE);
860 err = got_error_prefix_errno("wcwidth");
875 static const struct got_error*
876 build_refs_str(char **refs_str, struct got_reflist_head *refs,
877 struct got_object_id *id)
879 static const struct got_error *err = NULL;
880 struct got_reflist_entry *re;
886 SIMPLEQ_FOREACH(re, refs, entry) {
887 if (got_object_id_cmp(re->id, id) != 0)
889 name = got_ref_get_name(re->ref);
890 if (strcmp(name, GOT_REF_HEAD) == 0)
892 if (strncmp(name, "refs/", 5) == 0)
894 if (strncmp(name, "got/", 4) == 0)
896 if (strncmp(name, "heads/", 6) == 0)
898 if (strncmp(name, "remotes/", 8) == 0)
901 if (asprintf(refs_str, "%s%s%s", s ? s : "",
902 s ? ", " : "", name) == -1) {
903 err = got_error_prefix_errno("asprintf");
914 static const struct got_error *
915 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
917 char *smallerthan, *at;
919 smallerthan = strchr(author, '<');
920 if (smallerthan && smallerthan[1] != '\0')
921 author = smallerthan + 1;
922 at = strchr(author, '@');
925 return format_line(wauthor, author_width, author, limit);
928 static const struct got_error *
929 draw_commit(struct tog_view *view, struct got_commit_object *commit,
930 struct got_object_id *id, struct got_reflist_head *refs,
931 int author_display_cols)
933 const struct got_error *err = NULL;
934 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
935 char *logmsg0 = NULL, *logmsg = NULL;
937 wchar_t *wlogmsg = NULL, *wauthor = NULL;
938 int author_width, logmsg_width;
939 char *newline, *line = NULL;
941 static const size_t date_display_cols = 9;
942 const int avail = view->ncols;
944 time_t committer_time;
946 committer_time = got_object_commit_get_committer_time(commit);
947 if (localtime_r(&committer_time, &tm) == NULL)
948 return got_error_prefix_errno("localtime_r");
949 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
951 return got_error(GOT_ERR_NO_SPACE);
953 if (avail < date_display_cols)
954 limit = MIN(sizeof(datebuf) - 1, avail);
956 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
957 waddnstr(view->window, datebuf, limit);
962 author = strdup(got_object_commit_get_author(commit));
963 if (author == NULL) {
964 err = got_error_prefix_errno("strdup");
967 err = format_author(&wauthor, &author_width, author, avail - col);
970 waddwstr(view->window, wauthor);
972 while (col <= avail && author_width < author_display_cols + 2) {
973 waddch(view->window, ' ');
980 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
981 if (logmsg0 == NULL) {
982 err = got_error_prefix_errno("strdup");
986 while (*logmsg == '\n')
988 newline = strchr(logmsg, '\n');
992 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
995 waddwstr(view->window, wlogmsg);
997 while (col <= avail) {
998 waddch(view->window, ' ');
1010 static struct commit_queue_entry *
1011 alloc_commit_queue_entry(struct got_commit_object *commit,
1012 struct got_object_id *id)
1014 struct commit_queue_entry *entry;
1016 entry = calloc(1, sizeof(*entry));
1021 entry->commit = commit;
1026 pop_commit(struct commit_queue *commits)
1028 struct commit_queue_entry *entry;
1030 entry = TAILQ_FIRST(&commits->head);
1031 TAILQ_REMOVE(&commits->head, entry, entry);
1032 got_object_commit_close(entry->commit);
1033 commits->ncommits--;
1034 /* Don't free entry->id! It is owned by the commit graph. */
1039 free_commits(struct commit_queue *commits)
1041 while (!TAILQ_EMPTY(&commits->head))
1042 pop_commit(commits);
1045 static const struct got_error *
1046 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1047 int minqueue, struct got_repository *repo, const char *path)
1049 const struct got_error *err = NULL;
1053 * We keep all commits open throughout the lifetime of the log
1054 * view in order to avoid having to re-fetch commits from disk
1055 * while updating the display.
1057 while (nqueued < minqueue) {
1058 struct got_object_id *id;
1059 struct got_commit_object *commit;
1060 struct commit_queue_entry *entry;
1063 err = got_commit_graph_iter_next(&id, graph);
1065 if (err->code != GOT_ERR_ITER_NEED_MORE)
1067 err = got_commit_graph_fetch_commits(graph,
1077 err = got_object_open_as_commit(&commit, repo, id);
1080 entry = alloc_commit_queue_entry(commit, id);
1081 if (entry == NULL) {
1082 err = got_error_prefix_errno("alloc_commit_queue_entry");
1086 errcode = pthread_mutex_lock(&tog_mutex);
1088 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1092 entry->idx = commits->ncommits;
1093 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1095 commits->ncommits++;
1097 errcode = pthread_mutex_unlock(&tog_mutex);
1098 if (errcode && err == NULL)
1099 err = got_error_set_errno(errcode,
1100 "pthread_mutex_unlock");
1106 static const struct got_error *
1107 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1109 const struct got_error *err = NULL;
1110 struct got_reference *head_ref;
1114 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1118 err = got_ref_resolve(head_id, repo, head_ref);
1119 got_ref_close(head_ref);
1128 static const struct got_error *
1129 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1130 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1131 struct commit_queue *commits, int selected_idx, int limit,
1132 struct got_reflist_head *refs, const char *path, int commits_needed)
1134 const struct got_error *err = NULL;
1135 struct commit_queue_entry *entry;
1136 int ncommits, width;
1137 int author_cols = 10;
1138 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1139 char *refs_str = NULL;
1145 if (ncommits == selected_idx) {
1149 entry = TAILQ_NEXT(entry, entry);
1154 err = got_object_id_str(&id_str, (*selected)->id);
1158 err = build_refs_str(&refs_str, refs, (*selected)->id);
1164 if (commits_needed == 0)
1165 halfdelay(10); /* disable fast refresh */
1167 if (asprintf(&ncommits_str, " [%d/%d] %s",
1168 entry ? entry->idx + 1 : 0, commits->ncommits,
1169 commits_needed > 0 ? "loading... " :
1170 (refs_str ? refs_str : "")) == -1) {
1171 err = got_error_prefix_errno("asprintf");
1175 if (path && strcmp(path, "/") != 0) {
1176 if (asprintf(&header, "commit %s %s%s",
1177 id_str ? id_str : "........................................",
1178 path, ncommits_str) == -1) {
1179 err = got_error_prefix_errno("asprintf");
1183 } else if (asprintf(&header, "commit %s%s",
1184 id_str ? id_str : "........................................",
1185 ncommits_str) == -1) {
1186 err = got_error_prefix_errno("asprintf");
1190 err = format_line(&wline, &width, header, view->ncols);
1194 werase(view->window);
1196 if (view_needs_focus_indication(view))
1197 wstandout(view->window);
1198 waddwstr(view->window, wline);
1199 while (width < view->ncols) {
1200 waddch(view->window, ' ');
1203 if (view_needs_focus_indication(view))
1204 wstandend(view->window);
1209 /* Grow author column size if necessary. */
1216 if (ncommits >= limit - 1)
1218 author = strdup(got_object_commit_get_author(entry->commit));
1219 if (author == NULL) {
1220 err = got_error_prefix_errno("strdup");
1223 err = format_author(&wauthor, &width, author, COLS);
1224 if (author_cols < width)
1225 author_cols = width;
1228 entry = TAILQ_NEXT(entry, entry);
1235 if (ncommits >= limit - 1)
1237 if (ncommits == selected_idx)
1238 wstandout(view->window);
1239 err = draw_commit(view, entry->commit, entry->id, refs,
1241 if (ncommits == selected_idx)
1242 wstandend(view->window);
1247 entry = TAILQ_NEXT(entry, entry);
1260 scroll_up(struct tog_view *view,
1261 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1262 struct commit_queue *commits)
1264 struct commit_queue_entry *entry;
1267 entry = TAILQ_FIRST(&commits->head);
1268 if (*first_displayed_entry == entry)
1271 entry = *first_displayed_entry;
1272 while (entry && nscrolled < maxscroll) {
1273 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1275 *first_displayed_entry = entry;
1281 static const struct got_error *
1282 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1283 pthread_cond_t *need_commits)
1288 halfdelay(1); /* fast refresh while loading commits */
1290 while (*commits_needed > 0) {
1294 /* Wake the log thread. */
1295 errcode = pthread_cond_signal(need_commits);
1297 return got_error_set_errno(errcode,
1298 "pthread_cond_signal");
1299 errcode = pthread_mutex_unlock(&tog_mutex);
1301 return got_error_set_errno(errcode,
1302 "pthread_mutex_unlock");
1304 errcode = pthread_mutex_lock(&tog_mutex);
1306 return got_error_set_errno(errcode,
1307 "pthread_mutex_lock");
1309 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1311 * Thread is not done yet; lose a key press
1312 * and let the user retry... this way the GUI
1313 * remains interactive while logging deep paths
1314 * with few commits in history.
1323 static const struct got_error *
1324 scroll_down(struct tog_view *view,
1325 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1326 struct commit_queue_entry **last_displayed_entry,
1327 struct commit_queue *commits, int *log_complete, int *commits_needed,
1328 pthread_cond_t *need_commits)
1330 const struct got_error *err = NULL;
1331 struct commit_queue_entry *pentry;
1334 if (*last_displayed_entry == NULL)
1337 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1338 if (pentry == NULL && !*log_complete) {
1340 * Ask the log thread for required amount of commits
1341 * plus some amount of pre-fetching.
1343 (*commits_needed) += maxscroll + 20;
1344 err = trigger_log_thread(0, commits_needed, log_complete,
1351 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1355 *last_displayed_entry = pentry;
1357 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1360 *first_displayed_entry = pentry;
1361 } while (++nscrolled < maxscroll);
1366 static const struct got_error *
1367 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1368 struct got_commit_object *commit, struct got_object_id *commit_id,
1369 struct tog_view *log_view, struct got_reflist_head *refs,
1370 struct got_repository *repo)
1372 const struct got_error *err;
1373 struct got_object_qid *parent_id;
1374 struct tog_view *diff_view;
1376 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1377 if (diff_view == NULL)
1378 return got_error_prefix_errno("view_open");
1380 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1381 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1382 commit_id, log_view, refs, repo);
1384 *new_view = diff_view;
1388 static const struct got_error *
1389 browse_commit(struct tog_view **new_view, int begin_x,
1390 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1391 struct got_repository *repo)
1393 const struct got_error *err = NULL;
1394 struct got_tree_object *tree;
1395 struct tog_view *tree_view;
1397 err = got_object_open_as_tree(&tree, repo,
1398 got_object_commit_get_tree_id(entry->commit));
1402 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1403 if (tree_view == NULL)
1404 return got_error_prefix_errno("view_open");
1406 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1408 got_object_tree_close(tree);
1410 *new_view = tree_view;
1415 log_thread(void *arg)
1417 const struct got_error *err = NULL;
1419 struct tog_log_thread_args *a = arg;
1422 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1426 while (!done && !err) {
1427 err = queue_commits(a->graph, a->commits, 1, a->repo,
1430 if (err->code != GOT_ERR_ITER_COMPLETED)
1434 } else if (a->commits_needed > 0)
1435 a->commits_needed--;
1437 errcode = pthread_mutex_lock(&tog_mutex);
1439 err = got_error_set_errno(errcode,
1440 "pthread_mutex_lock");
1442 } else if (*a->quit)
1444 else if (*a->first_displayed_entry == NULL) {
1445 *a->first_displayed_entry =
1446 TAILQ_FIRST(&a->commits->head);
1447 *a->selected_entry = *a->first_displayed_entry;
1451 a->commits_needed = 0;
1452 else if (a->commits_needed == 0) {
1453 errcode = pthread_cond_wait(&a->need_commits,
1456 err = got_error_set_errno(errcode,
1457 "pthread_cond_wait");
1460 errcode = pthread_mutex_unlock(&tog_mutex);
1461 if (errcode && err == NULL)
1462 err = got_error_set_errno(errcode,
1463 "pthread_mutex_unlock");
1465 a->log_complete = 1;
1469 static const struct got_error *
1470 stop_log_thread(struct tog_log_view_state *s)
1472 const struct got_error *err = NULL;
1477 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1479 return got_error_set_errno(errcode,
1480 "pthread_cond_signal");
1481 errcode = pthread_mutex_unlock(&tog_mutex);
1483 return got_error_set_errno(errcode,
1484 "pthread_mutex_unlock");
1485 errcode = pthread_join(s->thread, (void **)&err);
1487 return got_error_set_errno(errcode, "pthread_join");
1488 errcode = pthread_mutex_lock(&tog_mutex);
1490 return got_error_set_errno(errcode,
1491 "pthread_mutex_lock");
1495 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1496 if (errcode && err == NULL)
1497 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1499 if (s->thread_args.repo) {
1500 got_repo_close(s->thread_args.repo);
1501 s->thread_args.repo = NULL;
1504 if (s->thread_args.graph) {
1505 got_commit_graph_close(s->thread_args.graph);
1506 s->thread_args.graph = NULL;
1512 static const struct got_error *
1513 close_log_view(struct tog_view *view)
1515 const struct got_error *err = NULL;
1516 struct tog_log_view_state *s = &view->state.log;
1518 err = stop_log_thread(s);
1519 free_commits(&s->commits);
1520 free(s->in_repo_path);
1521 s->in_repo_path = NULL;
1527 static const struct got_error *
1528 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1529 struct got_reflist_head *refs, struct got_repository *repo,
1530 const char *path, int check_disk)
1532 const struct got_error *err = NULL;
1533 struct tog_log_view_state *s = &view->state.log;
1534 struct got_repository *thread_repo = NULL;
1535 struct got_commit_graph *thread_graph = NULL;
1538 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1542 /* The commit queue only contains commits being displayed. */
1543 TAILQ_INIT(&s->commits.head);
1544 s->commits.ncommits = 0;
1548 s->start_id = got_object_id_dup(start_id);
1549 if (s->start_id == NULL) {
1550 err = got_error_prefix_errno("got_object_id_dup");
1554 view->show = show_log_view;
1555 view->input = input_log_view;
1556 view->close = close_log_view;
1558 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1561 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1566 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1568 err = got_error_set_errno(errcode, "pthread_cond_init");
1572 s->thread_args.commits_needed = view->nlines;
1573 s->thread_args.graph = thread_graph;
1574 s->thread_args.commits = &s->commits;
1575 s->thread_args.in_repo_path = s->in_repo_path;
1576 s->thread_args.start_id = s->start_id;
1577 s->thread_args.repo = thread_repo;
1578 s->thread_args.log_complete = 0;
1579 s->thread_args.quit = &s->quit;
1580 s->thread_args.view = view;
1581 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1582 s->thread_args.selected_entry = &s->selected_entry;
1585 close_log_view(view);
1589 static const struct got_error *
1590 show_log_view(struct tog_view *view)
1592 struct tog_log_view_state *s = &view->state.log;
1594 if (s->thread == NULL) {
1595 int errcode = pthread_create(&s->thread, NULL, log_thread,
1598 return got_error_set_errno(errcode, "pthread_create");
1601 return draw_commits(view, &s->last_displayed_entry,
1602 &s->selected_entry, s->first_displayed_entry,
1603 &s->commits, s->selected, view->nlines, s->refs,
1604 s->in_repo_path, s->thread_args.commits_needed);
1607 static const struct got_error *
1608 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1609 struct tog_view **focus_view, struct tog_view *view, int ch)
1611 const struct got_error *err = NULL;
1612 struct tog_log_view_state *s = &view->state.log;
1614 struct tog_view *diff_view = NULL, *tree_view = NULL;
1625 if (s->first_displayed_entry == NULL)
1627 if (s->selected > 0)
1629 if (s->selected > 0)
1631 scroll_up(view, &s->first_displayed_entry, 1,
1636 if (s->first_displayed_entry == NULL)
1638 if (TAILQ_FIRST(&s->commits.head) ==
1639 s->first_displayed_entry) {
1643 scroll_up(view, &s->first_displayed_entry,
1644 view->nlines, &s->commits);
1650 if (s->first_displayed_entry == NULL)
1652 if (s->selected < MIN(view->nlines - 2,
1653 s->commits.ncommits - 1)) {
1657 err = scroll_down(view, &s->first_displayed_entry, 1,
1658 &s->last_displayed_entry, &s->commits,
1659 &s->thread_args.log_complete,
1660 &s->thread_args.commits_needed,
1661 &s->thread_args.need_commits);
1665 struct commit_queue_entry *first;
1666 first = s->first_displayed_entry;
1669 err = scroll_down(view, &s->first_displayed_entry,
1670 view->nlines, &s->last_displayed_entry,
1671 &s->commits, &s->thread_args.log_complete,
1672 &s->thread_args.commits_needed,
1673 &s->thread_args.need_commits);
1674 if (first == s->first_displayed_entry &&
1675 s->selected < MIN(view->nlines - 2,
1676 s->commits.ncommits - 1)) {
1677 /* can't scroll further down */
1678 s->selected = MIN(view->nlines - 2,
1679 s->commits.ncommits - 1);
1685 if (s->selected > view->nlines - 2)
1686 s->selected = view->nlines - 2;
1687 if (s->selected > s->commits.ncommits - 1)
1688 s->selected = s->commits.ncommits - 1;
1693 if (s->selected_entry == NULL)
1695 if (view_is_parent_view(view))
1696 begin_x = view_split_begin_x(view->begin_x);
1697 err = open_diff_view_for_commit(&diff_view, begin_x,
1698 s->selected_entry->commit, s->selected_entry->id,
1699 view, s->refs, s->repo);
1702 if (view_is_parent_view(view)) {
1703 err = view_close_child(view);
1706 err = view_set_child(view, diff_view);
1708 view_close(diff_view);
1711 *focus_view = diff_view;
1712 view->child_focussed = 1;
1714 *new_view = diff_view;
1717 if (s->selected_entry == NULL)
1719 if (view_is_parent_view(view))
1720 begin_x = view_split_begin_x(view->begin_x);
1721 err = browse_commit(&tree_view, begin_x,
1722 s->selected_entry, s->refs, s->repo);
1725 if (view_is_parent_view(view)) {
1726 err = view_close_child(view);
1729 err = view_set_child(view, tree_view);
1731 view_close(tree_view);
1734 *focus_view = tree_view;
1735 view->child_focussed = 1;
1737 *new_view = tree_view;
1740 if (strcmp(s->in_repo_path, "/") == 0)
1742 parent_path = dirname(s->in_repo_path);
1743 if (parent_path && strcmp(parent_path, ".") != 0) {
1744 struct tog_view *lv;
1745 err = stop_log_thread(s);
1748 lv = view_open(view->nlines, view->ncols,
1749 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1751 return got_error_prefix_errno(
1753 err = open_log_view(lv, s->start_id, s->refs,
1754 s->repo, parent_path, 0);
1757 if (view_is_parent_view(view))
1760 view_set_child(view->parent, lv);
1773 static const struct got_error *
1774 apply_unveil(const char *repo_path, const char *worktree_path)
1776 const struct got_error *error;
1778 if (repo_path && unveil(repo_path, "r") != 0)
1779 return got_error_prefix_errno2("unveil", repo_path);
1781 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1782 return got_error_prefix_errno2("unveil", worktree_path);
1784 if (unveil("/tmp", "rwc") != 0)
1785 return got_error_prefix_errno2("unveil", "/tmp");
1787 error = got_privsep_unveil_exec_helpers();
1791 if (unveil(NULL, NULL) != 0)
1792 return got_error_prefix_errno("unveil");
1802 halfdelay(1); /* Do fast refresh while initial view is loading. */
1805 intrflush(stdscr, FALSE);
1806 keypad(stdscr, TRUE);
1808 signal(SIGWINCH, tog_sigwinch);
1811 static const struct got_error *
1812 cmd_log(int argc, char *argv[])
1814 const struct got_error *error;
1815 struct got_repository *repo = NULL;
1816 struct got_worktree *worktree = NULL;
1817 struct got_reflist_head refs;
1818 struct got_object_id *start_id = NULL;
1819 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1820 char *start_commit = NULL;
1822 struct tog_view *view;
1824 SIMPLEQ_INIT(&refs);
1827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1832 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1835 start_commit = optarg;
1838 repo_path = realpath(optarg, NULL);
1839 if (repo_path == NULL)
1840 err(1, "-r option");
1851 cwd = getcwd(NULL, 0);
1853 error = got_error_prefix_errno("getcwd");
1856 error = got_worktree_open(&worktree, cwd);
1857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1864 error = got_error_prefix_errno("strdup");
1867 } else if (argc == 1) {
1869 error = got_worktree_resolve_path(&path, worktree,
1874 path = strdup(argv[0]);
1876 error = got_error_prefix_errno("strdup");
1883 repo_path = worktree ?
1884 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1885 if (repo_path == NULL) {
1886 error = got_error_prefix_errno("strdup");
1892 error = got_repo_open(&repo, repo_path);
1896 error = apply_unveil(got_repo_get_path(repo),
1897 worktree ? got_worktree_get_root_path(worktree) : NULL);
1901 if (start_commit == NULL)
1902 error = get_head_commit_id(&start_id, repo);
1904 error = got_object_resolve_id_str(&start_id, repo,
1909 error = got_ref_list(&refs, repo);
1913 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1915 error = got_error_prefix_errno("view_open");
1918 error = open_log_view(view, start_id, &refs, repo, path, 1);
1921 error = view_loop(view);
1928 got_repo_close(repo);
1930 got_worktree_close(worktree);
1931 got_ref_list_free(&refs);
1939 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1945 parse_next_line(FILE *f, size_t *len)
1950 const char delim[3] = { '\0', '\0', '\0'};
1952 line = fparseln(f, &linelen, &lineno, delim, 0);
1958 static const struct got_error *
1959 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1960 int *last_displayed_line, int *eof, int max_lines,
1963 const struct got_error *err;
1964 int nlines = 0, nprinted = 0;
1971 werase(view->window);
1974 err = format_line(&wline, &width, header, view->ncols);
1979 if (view_needs_focus_indication(view))
1980 wstandout(view->window);
1981 waddwstr(view->window, wline);
1982 if (view_needs_focus_indication(view))
1983 wstandend(view->window);
1984 if (width < view->ncols)
1985 waddch(view->window, '\n');
1993 while (nprinted < max_lines) {
1994 line = parse_next_line(f, &len);
1999 if (++nlines < *first_displayed_line) {
2004 err = format_line(&wline, &width, line, view->ncols);
2009 waddwstr(view->window, wline);
2010 if (width < view->ncols)
2011 waddch(view->window, '\n');
2012 if (++nprinted == 1)
2013 *first_displayed_line = nlines;
2018 *last_displayed_line = nlines;
2026 get_datestr(time_t *time, char *datebuf)
2028 char *p, *s = ctime_r(time, datebuf);
2029 p = strchr(s, '\n');
2035 static const struct got_error *
2036 write_commit_info(struct got_object_id *commit_id,
2037 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2039 const struct got_error *err = NULL;
2041 struct got_commit_object *commit;
2042 char *id_str = NULL;
2043 time_t committer_time;
2044 const char *author, *committer;
2045 char *refs_str = NULL;
2048 err = build_refs_str(&refs_str, refs, commit_id);
2053 err = got_object_open_as_commit(&commit, repo, commit_id);
2057 err = got_object_id_str(&id_str, commit_id);
2059 err = got_error_prefix_errno("got_object_id_str");
2063 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2064 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2065 err = got_error_prefix_errno("fprintf");
2068 if (fprintf(outfile, "from: %s\n",
2069 got_object_commit_get_author(commit)) < 0) {
2070 err = got_error_prefix_errno("fprintf");
2073 committer_time = got_object_commit_get_committer_time(commit);
2074 if (fprintf(outfile, "date: %s UTC\n",
2075 get_datestr(&committer_time, datebuf)) < 0) {
2076 err = got_error_prefix_errno("fprintf");
2079 author = got_object_commit_get_author(commit);
2080 committer = got_object_commit_get_committer(commit);
2081 if (strcmp(author, committer) != 0 &&
2082 fprintf(outfile, "via: %s\n", committer) < 0) {
2083 err = got_error_prefix_errno("fprintf");
2086 if (fprintf(outfile, "%s\n",
2087 got_object_commit_get_logmsg(commit)) < 0) {
2088 err = got_error_prefix_errno("fprintf");
2094 got_object_commit_close(commit);
2098 static const struct got_error *
2099 create_diff(struct tog_diff_view_state *s)
2101 const struct got_error *err = NULL;
2107 err = got_error_prefix_errno("got_opentemp");
2110 if (s->f && fclose(s->f) != 0) {
2111 err = got_error_prefix_errno("fclose");
2117 err = got_object_get_type(&obj_type, s->repo, s->id1);
2119 err = got_object_get_type(&obj_type, s->repo, s->id2);
2124 case GOT_OBJ_TYPE_BLOB:
2125 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2126 s->diff_context, s->repo, f);
2128 case GOT_OBJ_TYPE_TREE:
2129 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2130 s->diff_context, s->repo, f);
2132 case GOT_OBJ_TYPE_COMMIT: {
2133 const struct got_object_id_queue *parent_ids;
2134 struct got_object_qid *pid;
2135 struct got_commit_object *commit2;
2137 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2140 /* Show commit info if we're diffing to a parent/root commit. */
2142 write_commit_info(s->id2, s->refs, s->repo, f);
2144 parent_ids = got_object_commit_get_parent_ids(commit2);
2145 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2146 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2147 write_commit_info(s->id2, s->refs,
2153 got_object_commit_close(commit2);
2155 err = got_diff_objects_as_commits(s->id1, s->id2,
2156 s->diff_context, s->repo, f);
2160 err = got_error(GOT_ERR_OBJ_TYPE);
2164 if (f && fflush(f) != 0 && err == NULL)
2165 err = got_error_prefix_errno("fflush");
2170 diff_view_indicate_progress(struct tog_view *view)
2172 werase(view->window);
2173 waddstr(view->window, "diffing...");
2178 static const struct got_error *
2179 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2180 struct got_object_id *id2, struct tog_view *log_view,
2181 struct got_reflist_head *refs, struct got_repository *repo)
2183 const struct got_error *err;
2185 if (id1 != NULL && id2 != NULL) {
2187 err = got_object_get_type(&type1, repo, id1);
2190 err = got_object_get_type(&type2, repo, id2);
2195 return got_error(GOT_ERR_OBJ_TYPE);
2199 view->state.diff.id1 = got_object_id_dup(id1);
2200 if (view->state.diff.id1 == NULL)
2201 return got_error_prefix_errno("got_object_id_dup");
2203 view->state.diff.id1 = NULL;
2205 view->state.diff.id2 = got_object_id_dup(id2);
2206 if (view->state.diff.id2 == NULL) {
2207 free(view->state.diff.id1);
2208 view->state.diff.id1 = NULL;
2209 return got_error_prefix_errno("got_object_id_dup");
2211 view->state.diff.f = NULL;
2212 view->state.diff.first_displayed_line = 1;
2213 view->state.diff.last_displayed_line = view->nlines;
2214 view->state.diff.diff_context = 3;
2215 view->state.diff.log_view = log_view;
2216 view->state.diff.repo = repo;
2217 view->state.diff.refs = refs;
2219 if (log_view && view_is_splitscreen(view))
2220 show_log_view(log_view); /* draw vborder */
2221 diff_view_indicate_progress(view);
2223 err = create_diff(&view->state.diff);
2225 free(view->state.diff.id1);
2226 view->state.diff.id1 = NULL;
2227 free(view->state.diff.id2);
2228 view->state.diff.id2 = NULL;
2232 view->show = show_diff_view;
2233 view->input = input_diff_view;
2234 view->close = close_diff_view;
2239 static const struct got_error *
2240 close_diff_view(struct tog_view *view)
2242 const struct got_error *err = NULL;
2244 free(view->state.diff.id1);
2245 view->state.diff.id1 = NULL;
2246 free(view->state.diff.id2);
2247 view->state.diff.id2 = NULL;
2248 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2249 err = got_error_prefix_errno("fclose");
2253 static const struct got_error *
2254 show_diff_view(struct tog_view *view)
2256 const struct got_error *err;
2257 struct tog_diff_view_state *s = &view->state.diff;
2258 char *id_str1 = NULL, *id_str2, *header;
2261 err = got_object_id_str(&id_str1, s->id1);
2265 err = got_object_id_str(&id_str2, s->id2);
2269 if (asprintf(&header, "diff %s %s",
2270 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2271 err = got_error_prefix_errno("asprintf");
2279 return draw_file(view, s->f, &s->first_displayed_line,
2280 &s->last_displayed_line, &s->eof, view->nlines,
2284 static const struct got_error *
2285 set_selected_commit(struct tog_diff_view_state *s,
2286 struct commit_queue_entry *entry)
2288 const struct got_error *err;
2289 const struct got_object_id_queue *parent_ids;
2290 struct got_commit_object *selected_commit;
2291 struct got_object_qid *pid;
2294 s->id2 = got_object_id_dup(entry->id);
2296 return got_error_prefix_errno("got_object_id_dup");
2298 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2301 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2303 pid = SIMPLEQ_FIRST(parent_ids);
2304 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2305 got_object_commit_close(selected_commit);
2309 static const struct got_error *
2310 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2311 struct tog_view **focus_view, struct tog_view *view, int ch)
2313 const struct got_error *err = NULL;
2314 struct tog_diff_view_state *s = &view->state.diff;
2315 struct tog_log_view_state *ls;
2316 struct commit_queue_entry *entry;
2322 if (s->first_displayed_line > 1)
2323 s->first_displayed_line--;
2326 if (s->first_displayed_line == 1)
2329 while (i++ < view->nlines - 1 &&
2330 s->first_displayed_line > 1)
2331 s->first_displayed_line--;
2336 s->first_displayed_line++;
2343 while (!s->eof && i++ < view->nlines - 1) {
2345 line = parse_next_line(s->f, NULL);
2346 s->first_displayed_line++;
2352 if (s->diff_context > 0) {
2354 diff_view_indicate_progress(view);
2355 err = create_diff(s);
2359 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2361 diff_view_indicate_progress(view);
2362 err = create_diff(s);
2367 if (s->log_view == NULL)
2369 ls = &s->log_view->state.log;
2370 entry = TAILQ_PREV(ls->selected_entry,
2371 commit_queue_head, entry);
2375 err = input_log_view(NULL, NULL, NULL, s->log_view,
2380 err = set_selected_commit(s, entry);
2384 s->first_displayed_line = 1;
2385 s->last_displayed_line = view->nlines;
2387 diff_view_indicate_progress(view);
2388 err = create_diff(s);
2392 if (s->log_view == NULL)
2394 ls = &s->log_view->state.log;
2396 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2397 ls->thread_args.commits_needed++;
2399 /* Display "loading..." in log view. */
2400 show_log_view(s->log_view);
2404 err = trigger_log_thread(1 /* load_all */,
2405 &ls->thread_args.commits_needed,
2406 &ls->thread_args.log_complete,
2407 &ls->thread_args.need_commits);
2411 err = input_log_view(NULL, NULL, NULL, s->log_view,
2416 entry = TAILQ_NEXT(ls->selected_entry, entry);
2420 err = set_selected_commit(s, entry);
2424 s->first_displayed_line = 1;
2425 s->last_displayed_line = view->nlines;
2427 diff_view_indicate_progress(view);
2428 err = create_diff(s);
2437 static const struct got_error *
2438 cmd_diff(int argc, char *argv[])
2440 const struct got_error *error = NULL;
2441 struct got_repository *repo = NULL;
2442 struct got_reflist_head refs;
2443 struct got_object_id *id1 = NULL, *id2 = NULL;
2444 char *repo_path = NULL;
2445 char *id_str1 = NULL, *id_str2 = NULL;
2447 struct tog_view *view;
2449 SIMPLEQ_INIT(&refs);
2452 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2457 while ((ch = getopt(argc, argv, "")) != -1) {
2469 usage_diff(); /* TODO show local worktree changes */
2470 } else if (argc == 2) {
2471 repo_path = getcwd(NULL, 0);
2472 if (repo_path == NULL)
2473 return got_error_prefix_errno("getcwd");
2476 } else if (argc == 3) {
2477 repo_path = realpath(argv[0], NULL);
2478 if (repo_path == NULL)
2479 return got_error_prefix_errno2("realpath", argv[0]);
2487 error = got_repo_open(&repo, repo_path);
2491 error = apply_unveil(got_repo_get_path(repo), NULL);
2495 error = got_object_resolve_id_str(&id1, repo, id_str1);
2499 error = got_object_resolve_id_str(&id2, repo, id_str2);
2503 error = got_ref_list(&refs, repo);
2507 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2509 error = got_error_prefix_errno("view_open");
2512 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2515 error = view_loop(view);
2518 got_repo_close(repo);
2519 got_ref_list_free(&refs);
2527 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2532 struct tog_blame_line {
2534 struct got_object_id *id;
2537 static const struct got_error *
2538 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2539 const char *path, struct tog_blame_line *lines, int nlines,
2540 int blame_complete, int selected_line, int *first_displayed_line,
2541 int *last_displayed_line, int *eof, int max_lines)
2543 const struct got_error *err;
2544 int lineno = 0, nprinted = 0;
2549 struct tog_blame_line *blame_line;
2550 struct got_object_id *prev_id = NULL;
2553 err = got_object_id_str(&id_str, id);
2558 werase(view->window);
2560 if (asprintf(&line, "commit %s", id_str) == -1) {
2561 err = got_error_prefix_errno("asprintf");
2566 err = format_line(&wline, &width, line, view->ncols);
2569 if (view_needs_focus_indication(view))
2570 wstandout(view->window);
2571 waddwstr(view->window, wline);
2572 if (view_needs_focus_indication(view))
2573 wstandend(view->window);
2576 if (width < view->ncols)
2577 waddch(view->window, '\n');
2579 if (asprintf(&line, "[%d/%d] %s%s",
2580 *first_displayed_line - 1 + selected_line, nlines,
2581 blame_complete ? "" : "annotating... ", path) == -1) {
2583 return got_error_prefix_errno("asprintf");
2586 err = format_line(&wline, &width, line, view->ncols);
2591 waddwstr(view->window, wline);
2594 if (width < view->ncols)
2595 waddch(view->window, '\n');
2598 while (nprinted < max_lines - 2) {
2599 line = parse_next_line(f, &len);
2604 if (++lineno < *first_displayed_line) {
2609 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2610 err = format_line(&wline, &width, line, wlimit);
2616 if (view->focussed && nprinted == selected_line - 1)
2617 wstandout(view->window);
2619 blame_line = &lines[lineno - 1];
2620 if (blame_line->annotated && prev_id &&
2621 got_object_id_cmp(prev_id, blame_line->id) == 0)
2622 waddstr(view->window, " ");
2623 else if (blame_line->annotated) {
2625 err = got_object_id_str(&id_str, blame_line->id);
2631 wprintw(view->window, "%.8s ", id_str);
2633 prev_id = blame_line->id;
2635 waddstr(view->window, "........ ");
2639 waddwstr(view->window, wline);
2640 while (width < wlimit) {
2641 waddch(view->window, ' ');
2644 if (view->focussed && nprinted == selected_line - 1)
2645 wstandend(view->window);
2646 if (++nprinted == 1)
2647 *first_displayed_line = lineno;
2652 *last_displayed_line = lineno;
2659 static const struct got_error *
2660 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2662 const struct got_error *err = NULL;
2663 struct tog_blame_cb_args *a = arg;
2664 struct tog_blame_line *line;
2667 if (nlines != a->nlines ||
2668 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2669 return got_error(GOT_ERR_RANGE);
2671 errcode = pthread_mutex_lock(&tog_mutex);
2673 return got_error_set_errno(errcode, "pthread_mutex_lock");
2675 if (*a->quit) { /* user has quit the blame view */
2676 err = got_error(GOT_ERR_ITER_COMPLETED);
2681 goto done; /* no change in this commit */
2683 line = &a->lines[lineno - 1];
2684 if (line->annotated)
2687 line->id = got_object_id_dup(id);
2688 if (line->id == NULL) {
2689 err = got_error_prefix_errno("got_object_id_dup");
2692 line->annotated = 1;
2694 errcode = pthread_mutex_unlock(&tog_mutex);
2696 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2701 blame_thread(void *arg)
2703 const struct got_error *err;
2704 struct tog_blame_thread_args *ta = arg;
2705 struct tog_blame_cb_args *a = ta->cb_args;
2708 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2709 blame_cb, ta->cb_args);
2711 errcode = pthread_mutex_lock(&tog_mutex);
2713 return (void *)got_error_set_errno(errcode,
2714 "pthread_mutex_lock");
2716 got_repo_close(ta->repo);
2720 errcode = pthread_mutex_unlock(&tog_mutex);
2721 if (errcode && err == NULL)
2722 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2727 static struct got_object_id *
2728 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2731 struct tog_blame_line *line;
2733 line = &lines[first_displayed_line - 1 + selected_line - 1];
2734 if (!line->annotated)
2740 static const struct got_error *
2741 stop_blame(struct tog_blame *blame)
2743 const struct got_error *err = NULL;
2746 if (blame->thread) {
2748 errcode = pthread_mutex_unlock(&tog_mutex);
2750 return got_error_set_errno(errcode,
2751 "pthread_mutex_unlock");
2752 errcode = pthread_join(blame->thread, (void **)&err);
2754 return got_error_set_errno(errcode, "pthread_join");
2755 errcode = pthread_mutex_lock(&tog_mutex);
2757 return got_error_set_errno(errcode,
2758 "pthread_mutex_lock");
2759 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2761 blame->thread = NULL;
2763 if (blame->thread_args.repo) {
2764 got_repo_close(blame->thread_args.repo);
2765 blame->thread_args.repo = NULL;
2768 if (fclose(blame->f) != 0 && err == NULL)
2769 err = got_error_prefix_errno("fclose");
2773 for (i = 0; i < blame->nlines; i++)
2774 free(blame->lines[i].id);
2776 blame->lines = NULL;
2778 free(blame->cb_args.commit_id);
2779 blame->cb_args.commit_id = NULL;
2784 static const struct got_error *
2785 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2786 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2787 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2788 struct got_repository *repo)
2790 const struct got_error *err = NULL;
2791 struct got_blob_object *blob = NULL;
2792 struct got_repository *thread_repo = NULL;
2793 struct got_object_id *obj_id = NULL;
2796 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2800 return got_error(GOT_ERR_NO_OBJ);
2802 err = got_object_get_type(&obj_type, repo, obj_id);
2806 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2807 err = got_error(GOT_ERR_OBJ_TYPE);
2811 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2814 blame->f = got_opentemp();
2815 if (blame->f == NULL) {
2816 err = got_error_prefix_errno("got_opentemp");
2819 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2824 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2825 if (blame->lines == NULL) {
2826 err = got_error_prefix_errno("calloc");
2830 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2834 blame->cb_args.view = view;
2835 blame->cb_args.lines = blame->lines;
2836 blame->cb_args.nlines = blame->nlines;
2837 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2838 if (blame->cb_args.commit_id == NULL) {
2839 err = got_error_prefix_errno("got_object_id_dup");
2842 blame->cb_args.quit = done;
2844 blame->thread_args.path = path;
2845 blame->thread_args.repo = thread_repo;
2846 blame->thread_args.cb_args = &blame->cb_args;
2847 blame->thread_args.complete = blame_complete;
2848 *blame_complete = 0;
2852 got_object_blob_close(blob);
2859 static const struct got_error *
2860 open_blame_view(struct tog_view *view, char *path,
2861 struct got_object_id *commit_id, struct got_reflist_head *refs,
2862 struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct tog_blame_view_state *s = &view->state.blame;
2867 SIMPLEQ_INIT(&s->blamed_commits);
2869 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2873 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2874 s->first_displayed_line = 1;
2875 s->last_displayed_line = view->nlines;
2876 s->selected_line = 1;
2877 s->blame_complete = 0;
2879 if (s->path == NULL)
2880 return got_error_prefix_errno("open_blame_view");
2883 s->commit_id = commit_id;
2884 memset(&s->blame, 0, sizeof(s->blame));
2886 view->show = show_blame_view;
2887 view->input = input_blame_view;
2888 view->close = close_blame_view;
2890 return run_blame(&s->blame, view, &s->blame_complete,
2891 &s->first_displayed_line, &s->last_displayed_line,
2892 &s->selected_line, &s->done, &s->eof, s->path,
2893 s->blamed_commit->id, s->repo);
2896 static const struct got_error *
2897 close_blame_view(struct tog_view *view)
2899 const struct got_error *err = NULL;
2900 struct tog_blame_view_state *s = &view->state.blame;
2902 if (s->blame.thread)
2903 err = stop_blame(&s->blame);
2905 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2906 struct got_object_qid *blamed_commit;
2907 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2908 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2909 got_object_qid_free(blamed_commit);
2917 static const struct got_error *
2918 show_blame_view(struct tog_view *view)
2920 const struct got_error *err = NULL;
2921 struct tog_blame_view_state *s = &view->state.blame;
2924 if (s->blame.thread == NULL) {
2925 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2926 &s->blame.thread_args);
2928 return got_error_set_errno(errcode, "pthread_create");
2931 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2932 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2933 s->selected_line, &s->first_displayed_line,
2934 &s->last_displayed_line, &s->eof, view->nlines);
2940 static const struct got_error *
2941 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2942 struct tog_view **focus_view, struct tog_view *view, int ch)
2944 const struct got_error *err = NULL, *thread_err = NULL;
2945 struct tog_view *diff_view;
2946 struct tog_blame_view_state *s = &view->state.blame;
2955 if (s->selected_line > 1)
2957 else if (s->selected_line == 1 &&
2958 s->first_displayed_line > 1)
2959 s->first_displayed_line--;
2962 if (s->first_displayed_line == 1) {
2963 s->selected_line = 1;
2966 if (s->first_displayed_line > view->nlines - 2)
2967 s->first_displayed_line -=
2970 s->first_displayed_line = 1;
2974 if (s->selected_line < view->nlines - 2 &&
2975 s->first_displayed_line +
2976 s->selected_line <= s->blame.nlines)
2978 else if (s->last_displayed_line <
2980 s->first_displayed_line++;
2984 struct got_object_id *id = NULL;
2985 id = get_selected_commit_id(s->blame.lines,
2986 s->first_displayed_line, s->selected_line);
2990 struct got_commit_object *commit;
2991 struct got_object_qid *pid;
2992 struct got_object_id *blob_id = NULL;
2994 err = got_object_open_as_commit(&commit,
2998 pid = SIMPLEQ_FIRST(
2999 got_object_commit_get_parent_ids(commit));
3001 got_object_commit_close(commit);
3004 /* Check if path history ends here. */
3005 err = got_object_id_by_path(&blob_id, s->repo,
3008 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3010 got_object_commit_close(commit);
3013 err = got_object_get_type(&obj_type, s->repo,
3016 /* Can't blame non-blob type objects. */
3017 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3018 got_object_commit_close(commit);
3021 err = got_object_qid_alloc(&s->blamed_commit,
3023 got_object_commit_close(commit);
3025 if (got_object_id_cmp(id,
3026 s->blamed_commit->id) == 0)
3028 err = got_object_qid_alloc(&s->blamed_commit,
3034 thread_err = stop_blame(&s->blame);
3038 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3039 s->blamed_commit, entry);
3040 err = run_blame(&s->blame, view, &s->blame_complete,
3041 &s->first_displayed_line, &s->last_displayed_line,
3042 &s->selected_line, &s->done, &s->eof,
3043 s->path, s->blamed_commit->id, s->repo);
3049 struct got_object_qid *first;
3050 first = SIMPLEQ_FIRST(&s->blamed_commits);
3051 if (!got_object_id_cmp(first->id, s->commit_id))
3054 thread_err = stop_blame(&s->blame);
3058 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3059 got_object_qid_free(s->blamed_commit);
3061 SIMPLEQ_FIRST(&s->blamed_commits);
3062 err = run_blame(&s->blame, view, &s->blame_complete,
3063 &s->first_displayed_line, &s->last_displayed_line,
3064 &s->selected_line, &s->done, &s->eof, s->path,
3065 s->blamed_commit->id, s->repo);
3072 struct got_object_id *id = NULL;
3073 struct got_object_qid *pid;
3074 struct got_commit_object *commit = NULL;
3075 id = get_selected_commit_id(s->blame.lines,
3076 s->first_displayed_line, s->selected_line);
3079 err = got_object_open_as_commit(&commit, s->repo, id);
3082 pid = SIMPLEQ_FIRST(
3083 got_object_commit_get_parent_ids(commit));
3084 if (view_is_parent_view(view))
3085 begin_x = view_split_begin_x(view->begin_x);
3086 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3087 if (diff_view == NULL) {
3088 got_object_commit_close(commit);
3089 err = got_error_prefix_errno("view_open");
3092 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3093 id, NULL, s->refs, s->repo);
3094 got_object_commit_close(commit);
3096 view_close(diff_view);
3099 if (view_is_parent_view(view)) {
3100 err = view_close_child(view);
3103 err = view_set_child(view, diff_view);
3105 view_close(diff_view);
3108 *focus_view = diff_view;
3109 view->child_focussed = 1;
3111 *new_view = diff_view;
3118 if (s->last_displayed_line >= s->blame.nlines &&
3119 s->selected_line >= MIN(s->blame.nlines,
3120 view->nlines - 2)) {
3123 if (s->last_displayed_line >= s->blame.nlines &&
3124 s->selected_line < view->nlines - 2) {
3125 s->selected_line = MIN(s->blame.nlines,
3129 if (s->last_displayed_line + view->nlines - 2
3131 s->first_displayed_line +=
3134 s->first_displayed_line =
3139 if (s->selected_line > view->nlines - 2) {
3140 s->selected_line = MIN(s->blame.nlines,
3147 return thread_err ? thread_err : err;
3150 static const struct got_error *
3151 cmd_blame(int argc, char *argv[])
3153 const struct got_error *error;
3154 struct got_repository *repo = NULL;
3155 struct got_reflist_head refs;
3156 struct got_worktree *worktree = NULL;
3157 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3158 struct got_object_id *commit_id = NULL;
3159 char *commit_id_str = NULL;
3161 struct tog_view *view;
3163 SIMPLEQ_INIT(&refs);
3166 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3171 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3174 commit_id_str = optarg;
3177 repo_path = realpath(optarg, NULL);
3178 if (repo_path == NULL)
3179 err(1, "-r option");
3195 cwd = getcwd(NULL, 0);
3197 error = got_error_prefix_errno("getcwd");
3200 if (repo_path == NULL) {
3201 error = got_worktree_open(&worktree, cwd);
3202 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3208 strdup(got_worktree_get_repo_path(worktree));
3209 if (repo_path == NULL)
3210 error = got_error_prefix_errno("strdup");
3214 repo_path = strdup(cwd);
3215 if (repo_path == NULL) {
3216 error = got_error_prefix_errno("strdup");
3224 error = got_repo_open(&repo, repo_path);
3228 error = apply_unveil(got_repo_get_path(repo), NULL);
3233 const char *prefix = got_worktree_get_path_prefix(worktree);
3234 char *p, *worktree_subdir = cwd +
3235 strlen(got_worktree_get_root_path(worktree));
3236 if (asprintf(&p, "%s%s%s%s%s",
3237 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3238 worktree_subdir, worktree_subdir[0] ? "/" : "",
3240 error = got_error_prefix_errno("asprintf");
3243 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3246 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3251 if (commit_id_str == NULL) {
3252 struct got_reference *head_ref;
3253 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3256 error = got_ref_resolve(&commit_id, repo, head_ref);
3257 got_ref_close(head_ref);
3259 error = got_object_resolve_id_str(&commit_id, repo,
3265 error = got_ref_list(&refs, repo);
3269 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3271 error = got_error_prefix_errno("view_open");
3274 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3277 error = view_loop(view);
3283 got_worktree_close(worktree);
3285 got_repo_close(repo);
3286 got_ref_list_free(&refs);
3290 static const struct got_error *
3291 draw_tree_entries(struct tog_view *view,
3292 struct got_tree_entry **first_displayed_entry,
3293 struct got_tree_entry **last_displayed_entry,
3294 struct got_tree_entry **selected_entry, int *ndisplayed,
3295 const char *label, int show_ids, const char *parent_path,
3296 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3298 const struct got_error *err = NULL;
3299 struct got_tree_entry *te;
3305 werase(view->window);
3310 err = format_line(&wline, &width, label, view->ncols);
3313 if (view_needs_focus_indication(view))
3314 wstandout(view->window);
3315 waddwstr(view->window, wline);
3316 if (view_needs_focus_indication(view))
3317 wstandend(view->window);
3320 if (width < view->ncols)
3321 waddch(view->window, '\n');
3324 err = format_line(&wline, &width, parent_path, view->ncols);
3327 waddwstr(view->window, wline);
3330 if (width < view->ncols)
3331 waddch(view->window, '\n');
3334 waddch(view->window, '\n');
3338 te = SIMPLEQ_FIRST(&entries->head);
3339 if (*first_displayed_entry == NULL) {
3340 if (selected == 0) {
3342 wstandout(view->window);
3343 *selected_entry = NULL;
3345 waddstr(view->window, " ..\n"); /* parent directory */
3346 if (selected == 0 && view->focussed)
3347 wstandend(view->window);
3354 while (te != *first_displayed_entry)
3355 te = SIMPLEQ_NEXT(te, entry);
3359 char *line = NULL, *id_str = NULL;
3362 err = got_object_id_str(&id_str, te->id);
3364 return got_error_prefix_errno(
3365 "got_object_id_str");
3367 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3368 te->name, S_ISDIR(te->mode) ? "/" :
3369 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3371 return got_error_prefix_errno("asprintf");
3374 err = format_line(&wline, &width, line, view->ncols);
3379 if (n == selected) {
3381 wstandout(view->window);
3382 *selected_entry = te;
3384 waddwstr(view->window, wline);
3385 if (width < view->ncols)
3386 waddch(view->window, '\n');
3387 if (n == selected && view->focussed)
3388 wstandend(view->window);
3394 *last_displayed_entry = te;
3397 te = SIMPLEQ_NEXT(te, entry);
3404 tree_scroll_up(struct tog_view *view,
3405 struct got_tree_entry **first_displayed_entry, int maxscroll,
3406 const struct got_tree_entries *entries, int isroot)
3408 struct got_tree_entry *te, *prev;
3411 if (*first_displayed_entry == NULL)
3414 te = SIMPLEQ_FIRST(&entries->head);
3415 if (*first_displayed_entry == te) {
3417 *first_displayed_entry = NULL;
3421 /* XXX this is stupid... switch to TAILQ? */
3422 for (i = 0; i < maxscroll; i++) {
3423 while (te != *first_displayed_entry) {
3425 te = SIMPLEQ_NEXT(te, entry);
3427 *first_displayed_entry = prev;
3428 te = SIMPLEQ_FIRST(&entries->head);
3430 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3431 *first_displayed_entry = NULL;
3435 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3436 struct got_tree_entry *last_displayed_entry,
3437 const struct got_tree_entries *entries)
3439 struct got_tree_entry *next, *last;
3442 if (*first_displayed_entry)
3443 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3445 next = SIMPLEQ_FIRST(&entries->head);
3446 last = last_displayed_entry;
3447 while (next && last && n++ < maxscroll) {
3448 last = SIMPLEQ_NEXT(last, entry);
3450 *first_displayed_entry = next;
3451 next = SIMPLEQ_NEXT(next, entry);
3457 static const struct got_error *
3458 tree_entry_path(char **path, struct tog_parent_trees *parents,
3459 struct got_tree_entry *te)
3461 const struct got_error *err = NULL;
3462 struct tog_parent_tree *pt;
3463 size_t len = 2; /* for leading slash and NUL */
3465 TAILQ_FOREACH(pt, parents, entry)
3466 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3468 len += strlen(te->name);
3470 *path = calloc(1, len);
3472 return got_error_prefix_errno("calloc");
3475 pt = TAILQ_LAST(parents, tog_parent_trees);
3477 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3478 err = got_error(GOT_ERR_NO_SPACE);
3481 if (strlcat(*path, "/", len) >= len) {
3482 err = got_error(GOT_ERR_NO_SPACE);
3485 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3488 if (strlcat(*path, te->name, len) >= len) {
3489 err = got_error(GOT_ERR_NO_SPACE);
3501 static const struct got_error *
3502 blame_tree_entry(struct tog_view **new_view, int begin_x,
3503 struct got_tree_entry *te, struct tog_parent_trees *parents,
3504 struct got_object_id *commit_id, struct got_reflist_head *refs,
3505 struct got_repository *repo)
3507 const struct got_error *err = NULL;
3509 struct tog_view *blame_view;
3511 err = tree_entry_path(&path, parents, te);
3515 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3516 if (blame_view == NULL)
3517 return got_error_prefix_errno("view_open");
3519 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3521 view_close(blame_view);
3524 *new_view = blame_view;
3528 static const struct got_error *
3529 log_tree_entry(struct tog_view **new_view, int begin_x,
3530 struct got_tree_entry *te, struct tog_parent_trees *parents,
3531 struct got_object_id *commit_id, struct got_reflist_head *refs,
3532 struct got_repository *repo)
3534 struct tog_view *log_view;
3535 const struct got_error *err = NULL;
3538 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3539 if (log_view == NULL)
3540 return got_error_prefix_errno("view_open");
3542 err = tree_entry_path(&path, parents, te);
3546 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3548 view_close(log_view);
3550 *new_view = log_view;
3555 static const struct got_error *
3556 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3557 struct got_object_id *commit_id, struct got_reflist_head *refs,
3558 struct got_repository *repo)
3560 const struct got_error *err = NULL;
3561 char *commit_id_str = NULL;
3562 struct tog_tree_view_state *s = &view->state.tree;
3564 TAILQ_INIT(&s->parents);
3566 err = got_object_id_str(&commit_id_str, commit_id);
3570 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3571 err = got_error_prefix_errno("asprintf");
3575 s->root = s->tree = root;
3576 s->entries = got_object_tree_get_entries(root);
3577 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3578 s->commit_id = got_object_id_dup(commit_id);
3579 if (s->commit_id == NULL) {
3580 err = got_error_prefix_errno("got_object_id_dup");
3586 view->show = show_tree_view;
3587 view->input = input_tree_view;
3588 view->close = close_tree_view;
3590 free(commit_id_str);
3592 free(s->tree_label);
3593 s->tree_label = NULL;
3598 static const struct got_error *
3599 close_tree_view(struct tog_view *view)
3601 struct tog_tree_view_state *s = &view->state.tree;
3603 free(s->tree_label);
3604 s->tree_label = NULL;
3606 s->commit_id = NULL;
3607 while (!TAILQ_EMPTY(&s->parents)) {
3608 struct tog_parent_tree *parent;
3609 parent = TAILQ_FIRST(&s->parents);
3610 TAILQ_REMOVE(&s->parents, parent, entry);
3614 if (s->tree != s->root)
3615 got_object_tree_close(s->tree);
3616 got_object_tree_close(s->root);
3621 static const struct got_error *
3622 show_tree_view(struct tog_view *view)
3624 const struct got_error *err = NULL;
3625 struct tog_tree_view_state *s = &view->state.tree;
3628 err = tree_entry_path(&parent_path, &s->parents, NULL);
3632 err = draw_tree_entries(view, &s->first_displayed_entry,
3633 &s->last_displayed_entry, &s->selected_entry,
3634 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3635 s->entries, s->selected, view->nlines, s->tree == s->root);
3642 static const struct got_error *
3643 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3644 struct tog_view **focus_view, struct tog_view *view, int ch)
3646 const struct got_error *err = NULL;
3647 struct tog_tree_view_state *s = &view->state.tree;
3648 struct tog_view *log_view;
3649 int begin_x = 0, nscrolled;
3653 s->show_ids = !s->show_ids;
3656 if (!s->selected_entry)
3658 if (view_is_parent_view(view))
3659 begin_x = view_split_begin_x(view->begin_x);
3660 err = log_tree_entry(&log_view, begin_x,
3661 s->selected_entry, &s->parents,
3662 s->commit_id, s->refs, s->repo);
3663 if (view_is_parent_view(view)) {
3664 err = view_close_child(view);
3667 err = view_set_child(view, log_view);
3669 view_close(log_view);
3672 *focus_view = log_view;
3673 view->child_focussed = 1;
3675 *new_view = log_view;
3679 if (s->selected > 0) {
3681 if (s->selected == 0)
3684 if (s->selected > 0)
3686 tree_scroll_up(view, &s->first_displayed_entry, 1,
3687 s->entries, s->tree == s->root);
3690 tree_scroll_up(view, &s->first_displayed_entry,
3691 MAX(0, view->nlines - 4 - s->selected), s->entries,
3692 s->tree == s->root);
3694 if (SIMPLEQ_FIRST(&s->entries->head) ==
3695 s->first_displayed_entry && s->tree != s->root)
3696 s->first_displayed_entry = NULL;
3700 if (s->selected < s->ndisplayed - 1) {
3704 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3705 /* can't scroll any further */
3707 tree_scroll_down(&s->first_displayed_entry, 1,
3708 s->last_displayed_entry, s->entries);
3711 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3713 /* can't scroll any further; move cursor down */
3714 if (s->selected < s->ndisplayed - 1)
3715 s->selected = s->ndisplayed - 1;
3718 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3719 view->nlines, s->last_displayed_entry, s->entries);
3720 if (nscrolled < view->nlines) {
3722 struct got_tree_entry *te;
3723 te = s->first_displayed_entry;
3726 te = SIMPLEQ_NEXT(te, entry);
3728 s->selected = ndisplayed - 1;
3734 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3735 struct tog_parent_tree *parent;
3736 /* user selected '..' */
3737 if (s->tree == s->root)
3739 parent = TAILQ_FIRST(&s->parents);
3740 TAILQ_REMOVE(&s->parents, parent,
3742 got_object_tree_close(s->tree);
3743 s->tree = parent->tree;
3745 got_object_tree_get_entries(s->tree);
3746 s->first_displayed_entry =
3747 parent->first_displayed_entry;
3749 parent->selected_entry;
3750 s->selected = parent->selected;
3752 } else if (S_ISDIR(s->selected_entry->mode)) {
3753 struct tog_parent_tree *parent;
3754 struct got_tree_object *child;
3755 err = got_object_open_as_tree(&child,
3756 s->repo, s->selected_entry->id);
3759 parent = calloc(1, sizeof(*parent));
3760 if (parent == NULL) {
3761 err = got_error_prefix_errno("calloc");
3764 parent->tree = s->tree;
3765 parent->first_displayed_entry =
3766 s->first_displayed_entry;
3767 parent->selected_entry = s->selected_entry;
3768 parent->selected = s->selected;
3769 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3772 got_object_tree_get_entries(s->tree);
3774 s->first_displayed_entry = NULL;
3775 } else if (S_ISREG(s->selected_entry->mode)) {
3776 struct tog_view *blame_view;
3777 int begin_x = view_is_parent_view(view) ?
3778 view_split_begin_x(view->begin_x) : 0;
3780 err = blame_tree_entry(&blame_view, begin_x,
3781 s->selected_entry, &s->parents,
3782 s->commit_id, s->refs, s->repo);
3785 if (view_is_parent_view(view)) {
3786 err = view_close_child(view);
3789 err = view_set_child(view, blame_view);
3791 view_close(blame_view);
3794 *focus_view = blame_view;
3795 view->child_focussed = 1;
3797 *new_view = blame_view;
3801 if (s->selected > view->nlines)
3802 s->selected = s->ndisplayed - 1;
3815 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3820 static const struct got_error *
3821 cmd_tree(int argc, char *argv[])
3823 const struct got_error *error;
3824 struct got_repository *repo = NULL;
3825 struct got_reflist_head refs;
3826 char *repo_path = NULL;
3827 struct got_object_id *commit_id = NULL;
3828 char *commit_id_arg = NULL;
3829 struct got_commit_object *commit = NULL;
3830 struct got_tree_object *tree = NULL;
3832 struct tog_view *view;
3834 SIMPLEQ_INIT(&refs);
3837 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3842 while ((ch = getopt(argc, argv, "c:")) != -1) {
3845 commit_id_arg = optarg;
3857 struct got_worktree *worktree;
3858 char *cwd = getcwd(NULL, 0);
3860 return got_error_prefix_errno("getcwd");
3861 error = got_worktree_open(&worktree, cwd);
3862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3867 strdup(got_worktree_get_repo_path(worktree));
3868 got_worktree_close(worktree);
3871 if (repo_path == NULL) {
3872 error = got_error_prefix_errno("strdup");
3875 } else if (argc == 1) {
3876 repo_path = realpath(argv[0], NULL);
3877 if (repo_path == NULL)
3878 return got_error_prefix_errno2("realpath", argv[0]);
3884 error = got_repo_open(&repo, repo_path);
3888 error = apply_unveil(got_repo_get_path(repo), NULL);
3892 if (commit_id_arg == NULL)
3893 error = get_head_commit_id(&commit_id, repo);
3895 error = got_object_resolve_id_str(&commit_id, repo,
3900 error = got_object_open_as_commit(&commit, repo, commit_id);
3904 error = got_object_open_as_tree(&tree, repo,
3905 got_object_commit_get_tree_id(commit));
3909 error = got_ref_list(&refs, repo);
3913 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3915 error = got_error_prefix_errno("view_open");
3918 error = open_tree_view(view, tree, commit_id, &refs, repo);
3921 error = view_loop(view);
3926 got_object_commit_close(commit);
3928 got_object_tree_close(tree);
3930 got_repo_close(repo);
3931 got_ref_list_free(&refs);
3940 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3941 "Available commands:\n", getprogname());
3942 for (i = 0; i < nitems(tog_commands); i++) {
3943 struct tog_cmd *cmd = &tog_commands[i];
3944 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3950 make_argv(const char *arg0, const char *arg1)
3953 int argc = (arg1 == NULL ? 1 : 2);
3955 argv = calloc(argc, sizeof(char *));
3958 argv[0] = strdup(arg0);
3959 if (argv[0] == NULL)
3962 argv[1] = strdup(arg1);
3963 if (argv[1] == NULL)
3971 main(int argc, char *argv[])
3973 const struct got_error *error = NULL;
3974 struct tog_cmd *cmd = NULL;
3976 char **cmd_argv = NULL;
3978 setlocale(LC_CTYPE, "");
3980 while ((ch = getopt(argc, argv, "h")) != -1) {
3999 /* Build an argument vector which runs a default command. */
4000 cmd = &tog_commands[0];
4001 cmd_argv = make_argv(cmd->name, NULL);
4006 /* Did the user specific a command? */
4007 for (i = 0; i < nitems(tog_commands); i++) {
4008 if (strncmp(tog_commands[i].name, argv[0],
4009 strlen(argv[0])) == 0) {
4010 cmd = &tog_commands[i];
4012 tog_commands[i].cmd_usage();
4017 /* Did the user specify a repository? */
4018 char *repo_path = realpath(argv[0], NULL);
4020 struct got_repository *repo;
4021 error = got_repo_open(&repo, repo_path);
4023 got_repo_close(repo);
4025 error = got_error_prefix_errno2("realpath",
4029 fprintf(stderr, "%s: '%s' is not a "
4030 "known command\n", getprogname(),
4034 fprintf(stderr, "%s: '%s' is neither a known "
4035 "command nor a path to a repository\n",
4036 getprogname(), argv[0]);
4040 cmd = &tog_commands[0];
4041 cmd_argv = make_argv(cmd->name, repo_path);
4047 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4054 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);