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_from_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_from_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_from_errno("wresize");
439 if (replace_panel(view->panel, view->window) == ERR)
440 return got_error_from_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_from_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_from_errno("mbstowcs"); /* give up */
806 *ws = calloc(*wlen + 1, sizeof(*ws));
808 err = got_error_from_errno("calloc");
812 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
813 err = got_error_from_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_from_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_from_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_from_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_from_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_from_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_from_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_from_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_from_errno("asprintf");
1183 } else if (asprintf(&header, "commit %s%s",
1184 id_str ? id_str : "........................................",
1185 ncommits_str) == -1) {
1186 err = got_error_from_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_from_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_from_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_from_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_from_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_from_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_from_errno2("unveil", repo_path);
1781 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1782 return got_error_from_errno2("unveil", worktree_path);
1784 if (unveil("/tmp", "rwc") != 0)
1785 return got_error_from_errno2("unveil", "/tmp");
1787 error = got_privsep_unveil_exec_helpers();
1791 if (unveil(NULL, NULL) != 0)
1792 return got_error_from_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_from_errno("getcwd");
1856 error = got_worktree_open(&worktree, cwd);
1857 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1864 error = got_error_from_errno("strdup");
1867 } else if (argc == 1) {
1869 error = got_worktree_resolve_path(&path, worktree,
1874 path = strdup(argv[0]);
1876 error = got_error_from_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_from_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_from_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_from_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_from_errno("fprintf");
2068 if (fprintf(outfile, "from: %s\n",
2069 got_object_commit_get_author(commit)) < 0) {
2070 err = got_error_from_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_from_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_from_errno("fprintf");
2086 if (fprintf(outfile, "%s\n",
2087 got_object_commit_get_logmsg(commit)) < 0) {
2088 err = got_error_from_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_from_errno("got_opentemp");
2110 if (s->f && fclose(s->f) != 0) {
2111 err = got_error_from_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_from_errno("fflush");
2170 diff_view_indicate_progress(struct tog_view *view)
2172 mvwaddstr(view->window, 0, 0, "diffing...");
2177 static const struct got_error *
2178 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2179 struct got_object_id *id2, struct tog_view *log_view,
2180 struct got_reflist_head *refs, struct got_repository *repo)
2182 const struct got_error *err;
2184 if (id1 != NULL && id2 != NULL) {
2186 err = got_object_get_type(&type1, repo, id1);
2189 err = got_object_get_type(&type2, repo, id2);
2194 return got_error(GOT_ERR_OBJ_TYPE);
2198 view->state.diff.id1 = got_object_id_dup(id1);
2199 if (view->state.diff.id1 == NULL)
2200 return got_error_from_errno("got_object_id_dup");
2202 view->state.diff.id1 = NULL;
2204 view->state.diff.id2 = got_object_id_dup(id2);
2205 if (view->state.diff.id2 == NULL) {
2206 free(view->state.diff.id1);
2207 view->state.diff.id1 = NULL;
2208 return got_error_from_errno("got_object_id_dup");
2210 view->state.diff.f = NULL;
2211 view->state.diff.first_displayed_line = 1;
2212 view->state.diff.last_displayed_line = view->nlines;
2213 view->state.diff.diff_context = 3;
2214 view->state.diff.log_view = log_view;
2215 view->state.diff.repo = repo;
2216 view->state.diff.refs = refs;
2218 if (log_view && view_is_splitscreen(view))
2219 show_log_view(log_view); /* draw vborder */
2220 diff_view_indicate_progress(view);
2222 err = create_diff(&view->state.diff);
2224 free(view->state.diff.id1);
2225 view->state.diff.id1 = NULL;
2226 free(view->state.diff.id2);
2227 view->state.diff.id2 = NULL;
2231 view->show = show_diff_view;
2232 view->input = input_diff_view;
2233 view->close = close_diff_view;
2238 static const struct got_error *
2239 close_diff_view(struct tog_view *view)
2241 const struct got_error *err = NULL;
2243 free(view->state.diff.id1);
2244 view->state.diff.id1 = NULL;
2245 free(view->state.diff.id2);
2246 view->state.diff.id2 = NULL;
2247 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2248 err = got_error_from_errno("fclose");
2252 static const struct got_error *
2253 show_diff_view(struct tog_view *view)
2255 const struct got_error *err;
2256 struct tog_diff_view_state *s = &view->state.diff;
2257 char *id_str1 = NULL, *id_str2, *header;
2260 err = got_object_id_str(&id_str1, s->id1);
2264 err = got_object_id_str(&id_str2, s->id2);
2268 if (asprintf(&header, "diff %s %s",
2269 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2270 err = got_error_from_errno("asprintf");
2278 return draw_file(view, s->f, &s->first_displayed_line,
2279 &s->last_displayed_line, &s->eof, view->nlines,
2283 static const struct got_error *
2284 set_selected_commit(struct tog_diff_view_state *s,
2285 struct commit_queue_entry *entry)
2287 const struct got_error *err;
2288 const struct got_object_id_queue *parent_ids;
2289 struct got_commit_object *selected_commit;
2290 struct got_object_qid *pid;
2293 s->id2 = got_object_id_dup(entry->id);
2295 return got_error_from_errno("got_object_id_dup");
2297 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2300 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2302 pid = SIMPLEQ_FIRST(parent_ids);
2303 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2304 got_object_commit_close(selected_commit);
2308 static const struct got_error *
2309 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2310 struct tog_view **focus_view, struct tog_view *view, int ch)
2312 const struct got_error *err = NULL;
2313 struct tog_diff_view_state *s = &view->state.diff;
2314 struct tog_log_view_state *ls;
2315 struct commit_queue_entry *entry;
2321 if (s->first_displayed_line > 1)
2322 s->first_displayed_line--;
2325 if (s->first_displayed_line == 1)
2328 while (i++ < view->nlines - 1 &&
2329 s->first_displayed_line > 1)
2330 s->first_displayed_line--;
2335 s->first_displayed_line++;
2342 while (!s->eof && i++ < view->nlines - 1) {
2344 line = parse_next_line(s->f, NULL);
2345 s->first_displayed_line++;
2351 if (s->diff_context > 0) {
2353 diff_view_indicate_progress(view);
2354 err = create_diff(s);
2358 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2360 diff_view_indicate_progress(view);
2361 err = create_diff(s);
2366 if (s->log_view == NULL)
2368 ls = &s->log_view->state.log;
2369 entry = TAILQ_PREV(ls->selected_entry,
2370 commit_queue_head, entry);
2374 err = input_log_view(NULL, NULL, NULL, s->log_view,
2379 err = set_selected_commit(s, entry);
2383 s->first_displayed_line = 1;
2384 s->last_displayed_line = view->nlines;
2386 diff_view_indicate_progress(view);
2387 err = create_diff(s);
2391 if (s->log_view == NULL)
2393 ls = &s->log_view->state.log;
2395 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2396 ls->thread_args.commits_needed++;
2398 /* Display "loading..." in log view. */
2399 show_log_view(s->log_view);
2403 err = trigger_log_thread(1 /* load_all */,
2404 &ls->thread_args.commits_needed,
2405 &ls->thread_args.log_complete,
2406 &ls->thread_args.need_commits);
2410 err = input_log_view(NULL, NULL, NULL, s->log_view,
2415 entry = TAILQ_NEXT(ls->selected_entry, entry);
2419 err = set_selected_commit(s, entry);
2423 s->first_displayed_line = 1;
2424 s->last_displayed_line = view->nlines;
2426 diff_view_indicate_progress(view);
2427 err = create_diff(s);
2436 static const struct got_error *
2437 cmd_diff(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct got_repository *repo = NULL;
2441 struct got_reflist_head refs;
2442 struct got_object_id *id1 = NULL, *id2 = NULL;
2443 char *repo_path = NULL;
2444 char *id_str1 = NULL, *id_str2 = NULL;
2446 struct tog_view *view;
2448 SIMPLEQ_INIT(&refs);
2451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2456 while ((ch = getopt(argc, argv, "")) != -1) {
2468 usage_diff(); /* TODO show local worktree changes */
2469 } else if (argc == 2) {
2470 repo_path = getcwd(NULL, 0);
2471 if (repo_path == NULL)
2472 return got_error_from_errno("getcwd");
2475 } else if (argc == 3) {
2476 repo_path = realpath(argv[0], NULL);
2477 if (repo_path == NULL)
2478 return got_error_from_errno2("realpath", argv[0]);
2486 error = got_repo_open(&repo, repo_path);
2490 error = apply_unveil(got_repo_get_path(repo), NULL);
2494 error = got_object_resolve_id_str(&id1, repo, id_str1);
2498 error = got_object_resolve_id_str(&id2, repo, id_str2);
2502 error = got_ref_list(&refs, repo);
2506 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2508 error = got_error_from_errno("view_open");
2511 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2514 error = view_loop(view);
2517 got_repo_close(repo);
2518 got_ref_list_free(&refs);
2526 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2531 struct tog_blame_line {
2533 struct got_object_id *id;
2536 static const struct got_error *
2537 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2538 const char *path, struct tog_blame_line *lines, int nlines,
2539 int blame_complete, int selected_line, int *first_displayed_line,
2540 int *last_displayed_line, int *eof, int max_lines)
2542 const struct got_error *err;
2543 int lineno = 0, nprinted = 0;
2548 struct tog_blame_line *blame_line;
2549 struct got_object_id *prev_id = NULL;
2552 err = got_object_id_str(&id_str, id);
2557 werase(view->window);
2559 if (asprintf(&line, "commit %s", id_str) == -1) {
2560 err = got_error_from_errno("asprintf");
2565 err = format_line(&wline, &width, line, view->ncols);
2568 if (view_needs_focus_indication(view))
2569 wstandout(view->window);
2570 waddwstr(view->window, wline);
2571 if (view_needs_focus_indication(view))
2572 wstandend(view->window);
2575 if (width < view->ncols)
2576 waddch(view->window, '\n');
2578 if (asprintf(&line, "[%d/%d] %s%s",
2579 *first_displayed_line - 1 + selected_line, nlines,
2580 blame_complete ? "" : "annotating... ", path) == -1) {
2582 return got_error_from_errno("asprintf");
2585 err = format_line(&wline, &width, line, view->ncols);
2590 waddwstr(view->window, wline);
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2597 while (nprinted < max_lines - 2) {
2598 line = parse_next_line(f, &len);
2603 if (++lineno < *first_displayed_line) {
2608 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2609 err = format_line(&wline, &width, line, wlimit);
2615 if (view->focussed && nprinted == selected_line - 1)
2616 wstandout(view->window);
2618 blame_line = &lines[lineno - 1];
2619 if (blame_line->annotated && prev_id &&
2620 got_object_id_cmp(prev_id, blame_line->id) == 0)
2621 waddstr(view->window, " ");
2622 else if (blame_line->annotated) {
2624 err = got_object_id_str(&id_str, blame_line->id);
2630 wprintw(view->window, "%.8s ", id_str);
2632 prev_id = blame_line->id;
2634 waddstr(view->window, "........ ");
2638 waddwstr(view->window, wline);
2639 while (width < wlimit) {
2640 waddch(view->window, ' ');
2643 if (view->focussed && nprinted == selected_line - 1)
2644 wstandend(view->window);
2645 if (++nprinted == 1)
2646 *first_displayed_line = lineno;
2651 *last_displayed_line = lineno;
2658 static const struct got_error *
2659 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2661 const struct got_error *err = NULL;
2662 struct tog_blame_cb_args *a = arg;
2663 struct tog_blame_line *line;
2666 if (nlines != a->nlines ||
2667 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2668 return got_error(GOT_ERR_RANGE);
2670 errcode = pthread_mutex_lock(&tog_mutex);
2672 return got_error_set_errno(errcode, "pthread_mutex_lock");
2674 if (*a->quit) { /* user has quit the blame view */
2675 err = got_error(GOT_ERR_ITER_COMPLETED);
2680 goto done; /* no change in this commit */
2682 line = &a->lines[lineno - 1];
2683 if (line->annotated)
2686 line->id = got_object_id_dup(id);
2687 if (line->id == NULL) {
2688 err = got_error_from_errno("got_object_id_dup");
2691 line->annotated = 1;
2693 errcode = pthread_mutex_unlock(&tog_mutex);
2695 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2700 blame_thread(void *arg)
2702 const struct got_error *err;
2703 struct tog_blame_thread_args *ta = arg;
2704 struct tog_blame_cb_args *a = ta->cb_args;
2707 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2708 blame_cb, ta->cb_args);
2710 errcode = pthread_mutex_lock(&tog_mutex);
2712 return (void *)got_error_set_errno(errcode,
2713 "pthread_mutex_lock");
2715 got_repo_close(ta->repo);
2719 errcode = pthread_mutex_unlock(&tog_mutex);
2720 if (errcode && err == NULL)
2721 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2726 static struct got_object_id *
2727 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2730 struct tog_blame_line *line;
2732 line = &lines[first_displayed_line - 1 + selected_line - 1];
2733 if (!line->annotated)
2739 static const struct got_error *
2740 stop_blame(struct tog_blame *blame)
2742 const struct got_error *err = NULL;
2745 if (blame->thread) {
2747 errcode = pthread_mutex_unlock(&tog_mutex);
2749 return got_error_set_errno(errcode,
2750 "pthread_mutex_unlock");
2751 errcode = pthread_join(blame->thread, (void **)&err);
2753 return got_error_set_errno(errcode, "pthread_join");
2754 errcode = pthread_mutex_lock(&tog_mutex);
2756 return got_error_set_errno(errcode,
2757 "pthread_mutex_lock");
2758 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2760 blame->thread = NULL;
2762 if (blame->thread_args.repo) {
2763 got_repo_close(blame->thread_args.repo);
2764 blame->thread_args.repo = NULL;
2767 if (fclose(blame->f) != 0 && err == NULL)
2768 err = got_error_from_errno("fclose");
2772 for (i = 0; i < blame->nlines; i++)
2773 free(blame->lines[i].id);
2775 blame->lines = NULL;
2777 free(blame->cb_args.commit_id);
2778 blame->cb_args.commit_id = NULL;
2783 static const struct got_error *
2784 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2785 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2786 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2787 struct got_repository *repo)
2789 const struct got_error *err = NULL;
2790 struct got_blob_object *blob = NULL;
2791 struct got_repository *thread_repo = NULL;
2792 struct got_object_id *obj_id = NULL;
2795 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2799 return got_error(GOT_ERR_NO_OBJ);
2801 err = got_object_get_type(&obj_type, repo, obj_id);
2805 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2806 err = got_error(GOT_ERR_OBJ_TYPE);
2810 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2813 blame->f = got_opentemp();
2814 if (blame->f == NULL) {
2815 err = got_error_from_errno("got_opentemp");
2818 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2823 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2824 if (blame->lines == NULL) {
2825 err = got_error_from_errno("calloc");
2829 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2833 blame->cb_args.view = view;
2834 blame->cb_args.lines = blame->lines;
2835 blame->cb_args.nlines = blame->nlines;
2836 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2837 if (blame->cb_args.commit_id == NULL) {
2838 err = got_error_from_errno("got_object_id_dup");
2841 blame->cb_args.quit = done;
2843 blame->thread_args.path = path;
2844 blame->thread_args.repo = thread_repo;
2845 blame->thread_args.cb_args = &blame->cb_args;
2846 blame->thread_args.complete = blame_complete;
2847 *blame_complete = 0;
2851 got_object_blob_close(blob);
2858 static const struct got_error *
2859 open_blame_view(struct tog_view *view, char *path,
2860 struct got_object_id *commit_id, struct got_reflist_head *refs,
2861 struct got_repository *repo)
2863 const struct got_error *err = NULL;
2864 struct tog_blame_view_state *s = &view->state.blame;
2866 SIMPLEQ_INIT(&s->blamed_commits);
2868 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2872 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2873 s->first_displayed_line = 1;
2874 s->last_displayed_line = view->nlines;
2875 s->selected_line = 1;
2876 s->blame_complete = 0;
2878 if (s->path == NULL)
2879 return got_error_from_errno("open_blame_view");
2882 s->commit_id = commit_id;
2883 memset(&s->blame, 0, sizeof(s->blame));
2885 view->show = show_blame_view;
2886 view->input = input_blame_view;
2887 view->close = close_blame_view;
2889 return run_blame(&s->blame, view, &s->blame_complete,
2890 &s->first_displayed_line, &s->last_displayed_line,
2891 &s->selected_line, &s->done, &s->eof, s->path,
2892 s->blamed_commit->id, s->repo);
2895 static const struct got_error *
2896 close_blame_view(struct tog_view *view)
2898 const struct got_error *err = NULL;
2899 struct tog_blame_view_state *s = &view->state.blame;
2901 if (s->blame.thread)
2902 err = stop_blame(&s->blame);
2904 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2905 struct got_object_qid *blamed_commit;
2906 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2907 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2908 got_object_qid_free(blamed_commit);
2916 static const struct got_error *
2917 show_blame_view(struct tog_view *view)
2919 const struct got_error *err = NULL;
2920 struct tog_blame_view_state *s = &view->state.blame;
2923 if (s->blame.thread == NULL) {
2924 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2925 &s->blame.thread_args);
2927 return got_error_set_errno(errcode, "pthread_create");
2930 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2931 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2932 s->selected_line, &s->first_displayed_line,
2933 &s->last_displayed_line, &s->eof, view->nlines);
2939 static const struct got_error *
2940 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2941 struct tog_view **focus_view, struct tog_view *view, int ch)
2943 const struct got_error *err = NULL, *thread_err = NULL;
2944 struct tog_view *diff_view;
2945 struct tog_blame_view_state *s = &view->state.blame;
2954 if (s->selected_line > 1)
2956 else if (s->selected_line == 1 &&
2957 s->first_displayed_line > 1)
2958 s->first_displayed_line--;
2961 if (s->first_displayed_line == 1) {
2962 s->selected_line = 1;
2965 if (s->first_displayed_line > view->nlines - 2)
2966 s->first_displayed_line -=
2969 s->first_displayed_line = 1;
2973 if (s->selected_line < view->nlines - 2 &&
2974 s->first_displayed_line +
2975 s->selected_line <= s->blame.nlines)
2977 else if (s->last_displayed_line <
2979 s->first_displayed_line++;
2983 struct got_object_id *id = NULL;
2984 id = get_selected_commit_id(s->blame.lines,
2985 s->first_displayed_line, s->selected_line);
2989 struct got_commit_object *commit;
2990 struct got_object_qid *pid;
2991 struct got_object_id *blob_id = NULL;
2993 err = got_object_open_as_commit(&commit,
2997 pid = SIMPLEQ_FIRST(
2998 got_object_commit_get_parent_ids(commit));
3000 got_object_commit_close(commit);
3003 /* Check if path history ends here. */
3004 err = got_object_id_by_path(&blob_id, s->repo,
3007 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3009 got_object_commit_close(commit);
3012 err = got_object_get_type(&obj_type, s->repo,
3015 /* Can't blame non-blob type objects. */
3016 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3017 got_object_commit_close(commit);
3020 err = got_object_qid_alloc(&s->blamed_commit,
3022 got_object_commit_close(commit);
3024 if (got_object_id_cmp(id,
3025 s->blamed_commit->id) == 0)
3027 err = got_object_qid_alloc(&s->blamed_commit,
3033 thread_err = stop_blame(&s->blame);
3037 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3038 s->blamed_commit, entry);
3039 err = run_blame(&s->blame, view, &s->blame_complete,
3040 &s->first_displayed_line, &s->last_displayed_line,
3041 &s->selected_line, &s->done, &s->eof,
3042 s->path, s->blamed_commit->id, s->repo);
3048 struct got_object_qid *first;
3049 first = SIMPLEQ_FIRST(&s->blamed_commits);
3050 if (!got_object_id_cmp(first->id, s->commit_id))
3053 thread_err = stop_blame(&s->blame);
3057 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3058 got_object_qid_free(s->blamed_commit);
3060 SIMPLEQ_FIRST(&s->blamed_commits);
3061 err = run_blame(&s->blame, view, &s->blame_complete,
3062 &s->first_displayed_line, &s->last_displayed_line,
3063 &s->selected_line, &s->done, &s->eof, s->path,
3064 s->blamed_commit->id, s->repo);
3071 struct got_object_id *id = NULL;
3072 struct got_object_qid *pid;
3073 struct got_commit_object *commit = NULL;
3074 id = get_selected_commit_id(s->blame.lines,
3075 s->first_displayed_line, s->selected_line);
3078 err = got_object_open_as_commit(&commit, s->repo, id);
3081 pid = SIMPLEQ_FIRST(
3082 got_object_commit_get_parent_ids(commit));
3083 if (view_is_parent_view(view))
3084 begin_x = view_split_begin_x(view->begin_x);
3085 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3086 if (diff_view == NULL) {
3087 got_object_commit_close(commit);
3088 err = got_error_from_errno("view_open");
3091 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3092 id, NULL, s->refs, s->repo);
3093 got_object_commit_close(commit);
3095 view_close(diff_view);
3098 if (view_is_parent_view(view)) {
3099 err = view_close_child(view);
3102 err = view_set_child(view, diff_view);
3104 view_close(diff_view);
3107 *focus_view = diff_view;
3108 view->child_focussed = 1;
3110 *new_view = diff_view;
3117 if (s->last_displayed_line >= s->blame.nlines &&
3118 s->selected_line >= MIN(s->blame.nlines,
3119 view->nlines - 2)) {
3122 if (s->last_displayed_line >= s->blame.nlines &&
3123 s->selected_line < view->nlines - 2) {
3124 s->selected_line = MIN(s->blame.nlines,
3128 if (s->last_displayed_line + view->nlines - 2
3130 s->first_displayed_line +=
3133 s->first_displayed_line =
3138 if (s->selected_line > view->nlines - 2) {
3139 s->selected_line = MIN(s->blame.nlines,
3146 return thread_err ? thread_err : err;
3149 static const struct got_error *
3150 cmd_blame(int argc, char *argv[])
3152 const struct got_error *error;
3153 struct got_repository *repo = NULL;
3154 struct got_reflist_head refs;
3155 struct got_worktree *worktree = NULL;
3156 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3157 struct got_object_id *commit_id = NULL;
3158 char *commit_id_str = NULL;
3160 struct tog_view *view;
3162 SIMPLEQ_INIT(&refs);
3165 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3170 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3173 commit_id_str = optarg;
3176 repo_path = realpath(optarg, NULL);
3177 if (repo_path == NULL)
3178 err(1, "-r option");
3194 cwd = getcwd(NULL, 0);
3196 error = got_error_from_errno("getcwd");
3199 if (repo_path == NULL) {
3200 error = got_worktree_open(&worktree, cwd);
3201 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3207 strdup(got_worktree_get_repo_path(worktree));
3208 if (repo_path == NULL)
3209 error = got_error_from_errno("strdup");
3213 repo_path = strdup(cwd);
3214 if (repo_path == NULL) {
3215 error = got_error_from_errno("strdup");
3223 error = got_repo_open(&repo, repo_path);
3227 error = apply_unveil(got_repo_get_path(repo), NULL);
3232 const char *prefix = got_worktree_get_path_prefix(worktree);
3233 char *p, *worktree_subdir = cwd +
3234 strlen(got_worktree_get_root_path(worktree));
3235 if (asprintf(&p, "%s%s%s%s%s",
3236 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3237 worktree_subdir, worktree_subdir[0] ? "/" : "",
3239 error = got_error_from_errno("asprintf");
3242 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3245 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3250 if (commit_id_str == NULL) {
3251 struct got_reference *head_ref;
3252 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3255 error = got_ref_resolve(&commit_id, repo, head_ref);
3256 got_ref_close(head_ref);
3258 error = got_object_resolve_id_str(&commit_id, repo,
3264 error = got_ref_list(&refs, repo);
3268 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3270 error = got_error_from_errno("view_open");
3273 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3276 error = view_loop(view);
3282 got_worktree_close(worktree);
3284 got_repo_close(repo);
3285 got_ref_list_free(&refs);
3289 static const struct got_error *
3290 draw_tree_entries(struct tog_view *view,
3291 struct got_tree_entry **first_displayed_entry,
3292 struct got_tree_entry **last_displayed_entry,
3293 struct got_tree_entry **selected_entry, int *ndisplayed,
3294 const char *label, int show_ids, const char *parent_path,
3295 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3297 const struct got_error *err = NULL;
3298 struct got_tree_entry *te;
3304 werase(view->window);
3309 err = format_line(&wline, &width, label, view->ncols);
3312 if (view_needs_focus_indication(view))
3313 wstandout(view->window);
3314 waddwstr(view->window, wline);
3315 if (view_needs_focus_indication(view))
3316 wstandend(view->window);
3319 if (width < view->ncols)
3320 waddch(view->window, '\n');
3323 err = format_line(&wline, &width, parent_path, view->ncols);
3326 waddwstr(view->window, wline);
3329 if (width < view->ncols)
3330 waddch(view->window, '\n');
3333 waddch(view->window, '\n');
3337 te = SIMPLEQ_FIRST(&entries->head);
3338 if (*first_displayed_entry == NULL) {
3339 if (selected == 0) {
3341 wstandout(view->window);
3342 *selected_entry = NULL;
3344 waddstr(view->window, " ..\n"); /* parent directory */
3345 if (selected == 0 && view->focussed)
3346 wstandend(view->window);
3353 while (te != *first_displayed_entry)
3354 te = SIMPLEQ_NEXT(te, entry);
3358 char *line = NULL, *id_str = NULL;
3361 err = got_object_id_str(&id_str, te->id);
3363 return got_error_from_errno(
3364 "got_object_id_str");
3366 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3367 te->name, S_ISDIR(te->mode) ? "/" :
3368 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3370 return got_error_from_errno("asprintf");
3373 err = format_line(&wline, &width, line, view->ncols);
3378 if (n == selected) {
3380 wstandout(view->window);
3381 *selected_entry = te;
3383 waddwstr(view->window, wline);
3384 if (width < view->ncols)
3385 waddch(view->window, '\n');
3386 if (n == selected && view->focussed)
3387 wstandend(view->window);
3393 *last_displayed_entry = te;
3396 te = SIMPLEQ_NEXT(te, entry);
3403 tree_scroll_up(struct tog_view *view,
3404 struct got_tree_entry **first_displayed_entry, int maxscroll,
3405 const struct got_tree_entries *entries, int isroot)
3407 struct got_tree_entry *te, *prev;
3410 if (*first_displayed_entry == NULL)
3413 te = SIMPLEQ_FIRST(&entries->head);
3414 if (*first_displayed_entry == te) {
3416 *first_displayed_entry = NULL;
3420 /* XXX this is stupid... switch to TAILQ? */
3421 for (i = 0; i < maxscroll; i++) {
3422 while (te != *first_displayed_entry) {
3424 te = SIMPLEQ_NEXT(te, entry);
3426 *first_displayed_entry = prev;
3427 te = SIMPLEQ_FIRST(&entries->head);
3429 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3430 *first_displayed_entry = NULL;
3434 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3435 struct got_tree_entry *last_displayed_entry,
3436 const struct got_tree_entries *entries)
3438 struct got_tree_entry *next, *last;
3441 if (*first_displayed_entry)
3442 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3444 next = SIMPLEQ_FIRST(&entries->head);
3445 last = last_displayed_entry;
3446 while (next && last && n++ < maxscroll) {
3447 last = SIMPLEQ_NEXT(last, entry);
3449 *first_displayed_entry = next;
3450 next = SIMPLEQ_NEXT(next, entry);
3456 static const struct got_error *
3457 tree_entry_path(char **path, struct tog_parent_trees *parents,
3458 struct got_tree_entry *te)
3460 const struct got_error *err = NULL;
3461 struct tog_parent_tree *pt;
3462 size_t len = 2; /* for leading slash and NUL */
3464 TAILQ_FOREACH(pt, parents, entry)
3465 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3467 len += strlen(te->name);
3469 *path = calloc(1, len);
3471 return got_error_from_errno("calloc");
3474 pt = TAILQ_LAST(parents, tog_parent_trees);
3476 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3477 err = got_error(GOT_ERR_NO_SPACE);
3480 if (strlcat(*path, "/", len) >= len) {
3481 err = got_error(GOT_ERR_NO_SPACE);
3484 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3487 if (strlcat(*path, te->name, len) >= len) {
3488 err = got_error(GOT_ERR_NO_SPACE);
3500 static const struct got_error *
3501 blame_tree_entry(struct tog_view **new_view, int begin_x,
3502 struct got_tree_entry *te, struct tog_parent_trees *parents,
3503 struct got_object_id *commit_id, struct got_reflist_head *refs,
3504 struct got_repository *repo)
3506 const struct got_error *err = NULL;
3508 struct tog_view *blame_view;
3510 err = tree_entry_path(&path, parents, te);
3514 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3515 if (blame_view == NULL)
3516 return got_error_from_errno("view_open");
3518 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3520 view_close(blame_view);
3523 *new_view = blame_view;
3527 static const struct got_error *
3528 log_tree_entry(struct tog_view **new_view, int begin_x,
3529 struct got_tree_entry *te, struct tog_parent_trees *parents,
3530 struct got_object_id *commit_id, struct got_reflist_head *refs,
3531 struct got_repository *repo)
3533 struct tog_view *log_view;
3534 const struct got_error *err = NULL;
3537 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3538 if (log_view == NULL)
3539 return got_error_from_errno("view_open");
3541 err = tree_entry_path(&path, parents, te);
3545 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3547 view_close(log_view);
3549 *new_view = log_view;
3554 static const struct got_error *
3555 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3556 struct got_object_id *commit_id, struct got_reflist_head *refs,
3557 struct got_repository *repo)
3559 const struct got_error *err = NULL;
3560 char *commit_id_str = NULL;
3561 struct tog_tree_view_state *s = &view->state.tree;
3563 TAILQ_INIT(&s->parents);
3565 err = got_object_id_str(&commit_id_str, commit_id);
3569 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3570 err = got_error_from_errno("asprintf");
3574 s->root = s->tree = root;
3575 s->entries = got_object_tree_get_entries(root);
3576 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3577 s->commit_id = got_object_id_dup(commit_id);
3578 if (s->commit_id == NULL) {
3579 err = got_error_from_errno("got_object_id_dup");
3585 view->show = show_tree_view;
3586 view->input = input_tree_view;
3587 view->close = close_tree_view;
3589 free(commit_id_str);
3591 free(s->tree_label);
3592 s->tree_label = NULL;
3597 static const struct got_error *
3598 close_tree_view(struct tog_view *view)
3600 struct tog_tree_view_state *s = &view->state.tree;
3602 free(s->tree_label);
3603 s->tree_label = NULL;
3605 s->commit_id = NULL;
3606 while (!TAILQ_EMPTY(&s->parents)) {
3607 struct tog_parent_tree *parent;
3608 parent = TAILQ_FIRST(&s->parents);
3609 TAILQ_REMOVE(&s->parents, parent, entry);
3613 if (s->tree != s->root)
3614 got_object_tree_close(s->tree);
3615 got_object_tree_close(s->root);
3620 static const struct got_error *
3621 show_tree_view(struct tog_view *view)
3623 const struct got_error *err = NULL;
3624 struct tog_tree_view_state *s = &view->state.tree;
3627 err = tree_entry_path(&parent_path, &s->parents, NULL);
3631 err = draw_tree_entries(view, &s->first_displayed_entry,
3632 &s->last_displayed_entry, &s->selected_entry,
3633 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3634 s->entries, s->selected, view->nlines, s->tree == s->root);
3641 static const struct got_error *
3642 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3643 struct tog_view **focus_view, struct tog_view *view, int ch)
3645 const struct got_error *err = NULL;
3646 struct tog_tree_view_state *s = &view->state.tree;
3647 struct tog_view *log_view;
3648 int begin_x = 0, nscrolled;
3652 s->show_ids = !s->show_ids;
3655 if (!s->selected_entry)
3657 if (view_is_parent_view(view))
3658 begin_x = view_split_begin_x(view->begin_x);
3659 err = log_tree_entry(&log_view, begin_x,
3660 s->selected_entry, &s->parents,
3661 s->commit_id, s->refs, s->repo);
3662 if (view_is_parent_view(view)) {
3663 err = view_close_child(view);
3666 err = view_set_child(view, log_view);
3668 view_close(log_view);
3671 *focus_view = log_view;
3672 view->child_focussed = 1;
3674 *new_view = log_view;
3678 if (s->selected > 0) {
3680 if (s->selected == 0)
3683 if (s->selected > 0)
3685 tree_scroll_up(view, &s->first_displayed_entry, 1,
3686 s->entries, s->tree == s->root);
3689 tree_scroll_up(view, &s->first_displayed_entry,
3690 MAX(0, view->nlines - 4 - s->selected), s->entries,
3691 s->tree == s->root);
3693 if (SIMPLEQ_FIRST(&s->entries->head) ==
3694 s->first_displayed_entry && s->tree != s->root)
3695 s->first_displayed_entry = NULL;
3699 if (s->selected < s->ndisplayed - 1) {
3703 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3704 /* can't scroll any further */
3706 tree_scroll_down(&s->first_displayed_entry, 1,
3707 s->last_displayed_entry, s->entries);
3710 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3712 /* can't scroll any further; move cursor down */
3713 if (s->selected < s->ndisplayed - 1)
3714 s->selected = s->ndisplayed - 1;
3717 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3718 view->nlines, s->last_displayed_entry, s->entries);
3719 if (nscrolled < view->nlines) {
3721 struct got_tree_entry *te;
3722 te = s->first_displayed_entry;
3725 te = SIMPLEQ_NEXT(te, entry);
3727 s->selected = ndisplayed - 1;
3733 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3734 struct tog_parent_tree *parent;
3735 /* user selected '..' */
3736 if (s->tree == s->root)
3738 parent = TAILQ_FIRST(&s->parents);
3739 TAILQ_REMOVE(&s->parents, parent,
3741 got_object_tree_close(s->tree);
3742 s->tree = parent->tree;
3744 got_object_tree_get_entries(s->tree);
3745 s->first_displayed_entry =
3746 parent->first_displayed_entry;
3748 parent->selected_entry;
3749 s->selected = parent->selected;
3751 } else if (S_ISDIR(s->selected_entry->mode)) {
3752 struct tog_parent_tree *parent;
3753 struct got_tree_object *child;
3754 err = got_object_open_as_tree(&child,
3755 s->repo, s->selected_entry->id);
3758 parent = calloc(1, sizeof(*parent));
3759 if (parent == NULL) {
3760 err = got_error_from_errno("calloc");
3763 parent->tree = s->tree;
3764 parent->first_displayed_entry =
3765 s->first_displayed_entry;
3766 parent->selected_entry = s->selected_entry;
3767 parent->selected = s->selected;
3768 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3771 got_object_tree_get_entries(s->tree);
3773 s->first_displayed_entry = NULL;
3774 } else if (S_ISREG(s->selected_entry->mode)) {
3775 struct tog_view *blame_view;
3776 int begin_x = view_is_parent_view(view) ?
3777 view_split_begin_x(view->begin_x) : 0;
3779 err = blame_tree_entry(&blame_view, begin_x,
3780 s->selected_entry, &s->parents,
3781 s->commit_id, s->refs, s->repo);
3784 if (view_is_parent_view(view)) {
3785 err = view_close_child(view);
3788 err = view_set_child(view, blame_view);
3790 view_close(blame_view);
3793 *focus_view = blame_view;
3794 view->child_focussed = 1;
3796 *new_view = blame_view;
3800 if (s->selected > view->nlines)
3801 s->selected = s->ndisplayed - 1;
3814 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3819 static const struct got_error *
3820 cmd_tree(int argc, char *argv[])
3822 const struct got_error *error;
3823 struct got_repository *repo = NULL;
3824 struct got_reflist_head refs;
3825 char *repo_path = NULL;
3826 struct got_object_id *commit_id = NULL;
3827 char *commit_id_arg = NULL;
3828 struct got_commit_object *commit = NULL;
3829 struct got_tree_object *tree = NULL;
3831 struct tog_view *view;
3833 SIMPLEQ_INIT(&refs);
3836 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3841 while ((ch = getopt(argc, argv, "c:")) != -1) {
3844 commit_id_arg = optarg;
3856 struct got_worktree *worktree;
3857 char *cwd = getcwd(NULL, 0);
3859 return got_error_from_errno("getcwd");
3860 error = got_worktree_open(&worktree, cwd);
3861 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3866 strdup(got_worktree_get_repo_path(worktree));
3867 got_worktree_close(worktree);
3870 if (repo_path == NULL) {
3871 error = got_error_from_errno("strdup");
3874 } else if (argc == 1) {
3875 repo_path = realpath(argv[0], NULL);
3876 if (repo_path == NULL)
3877 return got_error_from_errno2("realpath", argv[0]);
3883 error = got_repo_open(&repo, repo_path);
3887 error = apply_unveil(got_repo_get_path(repo), NULL);
3891 if (commit_id_arg == NULL)
3892 error = get_head_commit_id(&commit_id, repo);
3894 error = got_object_resolve_id_str(&commit_id, repo,
3899 error = got_object_open_as_commit(&commit, repo, commit_id);
3903 error = got_object_open_as_tree(&tree, repo,
3904 got_object_commit_get_tree_id(commit));
3908 error = got_ref_list(&refs, repo);
3912 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3914 error = got_error_from_errno("view_open");
3917 error = open_tree_view(view, tree, commit_id, &refs, repo);
3920 error = view_loop(view);
3925 got_object_commit_close(commit);
3927 got_object_tree_close(tree);
3929 got_repo_close(repo);
3930 got_ref_list_free(&refs);
3939 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3940 "Available commands:\n", getprogname());
3941 for (i = 0; i < nitems(tog_commands); i++) {
3942 struct tog_cmd *cmd = &tog_commands[i];
3943 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3949 make_argv(const char *arg0, const char *arg1)
3952 int argc = (arg1 == NULL ? 1 : 2);
3954 argv = calloc(argc, sizeof(char *));
3957 argv[0] = strdup(arg0);
3958 if (argv[0] == NULL)
3961 argv[1] = strdup(arg1);
3962 if (argv[1] == NULL)
3970 main(int argc, char *argv[])
3972 const struct got_error *error = NULL;
3973 struct tog_cmd *cmd = NULL;
3975 char **cmd_argv = NULL;
3977 setlocale(LC_CTYPE, "");
3979 while ((ch = getopt(argc, argv, "h")) != -1) {
3998 /* Build an argument vector which runs a default command. */
3999 cmd = &tog_commands[0];
4000 cmd_argv = make_argv(cmd->name, NULL);
4005 /* Did the user specific a command? */
4006 for (i = 0; i < nitems(tog_commands); i++) {
4007 if (strncmp(tog_commands[i].name, argv[0],
4008 strlen(argv[0])) == 0) {
4009 cmd = &tog_commands[i];
4011 tog_commands[i].cmd_usage();
4016 /* Did the user specify a repository? */
4017 char *repo_path = realpath(argv[0], NULL);
4019 struct got_repository *repo;
4020 error = got_repo_open(&repo, repo_path);
4022 got_repo_close(repo);
4024 error = got_error_from_errno2("realpath",
4028 fprintf(stderr, "%s: '%s' is not a "
4029 "known command\n", getprogname(),
4033 fprintf(stderr, "%s: '%s' is neither a known "
4034 "command nor a path to a repository\n",
4035 getprogname(), argv[0]);
4039 cmd = &tog_commands[0];
4040 cmd_argv = make_argv(cmd->name, repo_path);
4046 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4053 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);