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;
495 * Erase all content of the view. Can be used to "flash" the view because
496 * the view loop will redraw it quickly, providing a more subtle visual
497 * effect than curs_flash(3) would provide.
500 view_flash(struct tog_view *view)
502 werase(view->window);
513 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
514 cols = 80; /* Default */
520 resize_term(lines, cols);
523 static const struct got_error *
524 view_input(struct tog_view **new, struct tog_view **dead,
525 struct tog_view **focus, int *done, struct tog_view *view,
526 struct tog_view_list_head *views)
528 const struct got_error *err = NULL;
536 nodelay(stdscr, FALSE);
537 /* Allow threads to make progress while we are waiting for input. */
538 errcode = pthread_mutex_unlock(&tog_mutex);
540 return got_error_set_errno(errcode, "pthread_mutex_unlock");
541 ch = wgetch(view->window);
542 errcode = pthread_mutex_lock(&tog_mutex);
544 return got_error_set_errno(errcode, "pthread_mutex_lock");
545 nodelay(stdscr, TRUE);
547 if (tog_sigwinch_received) {
549 tog_sigwinch_received = 0;
550 TAILQ_FOREACH(v, views, entry) {
551 err = view_resize(v);
554 err = v->input(new, dead, focus, v, KEY_RESIZE);
565 *focus = view->child;
566 view->child_focussed = 1;
567 } else if (view->parent) {
568 *focus = view->parent;
569 view->parent->child_focussed = 0;
573 err = view->input(new, dead, focus, view, ch);
580 if (view_is_parent_view(view)) {
581 if (view->child == NULL)
583 if (view_is_splitscreen(view->child)) {
584 *focus = view->child;
585 view->child_focussed = 1;
586 err = view_fullscreen(view->child);
588 err = view_splitscreen(view->child);
591 err = view->child->input(new, dead, focus,
592 view->child, KEY_RESIZE);
594 if (view_is_splitscreen(view)) {
596 view->parent->child_focussed = 1;
597 err = view_fullscreen(view);
599 err = view_splitscreen(view);
603 err = view->input(new, dead, focus, view,
610 err = view->input(new, dead, focus, view, ch);
618 view_vborder(struct tog_view *view)
621 struct tog_view *view_above;
624 return view_vborder(view->parent);
626 panel = panel_above(view->panel);
630 view_above = panel_userptr(panel);
631 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
632 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
636 view_needs_focus_indication(struct tog_view *view)
638 if (view_is_parent_view(view)) {
639 if (view->child == NULL || view->child_focussed)
641 if (!view_is_splitscreen(view->child))
643 } else if (!view_is_splitscreen(view))
646 return view->focussed;
649 static const struct got_error *
650 view_loop(struct tog_view *view)
652 const struct got_error *err = NULL;
653 struct tog_view_list_head views;
654 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
655 int fast_refresh = 10;
656 int done = 0, errcode;
658 errcode = pthread_mutex_lock(&tog_mutex);
660 return got_error_set_errno(errcode, "pthread_mutex_lock");
663 TAILQ_INSERT_HEAD(&views, view, entry);
667 err = view->show(view);
672 while (!TAILQ_EMPTY(&views) && !done) {
673 /* Refresh fast during initialization, then become slower. */
674 if (fast_refresh && fast_refresh-- == 0)
675 halfdelay(10); /* switch to once per second */
677 err = view_input(&new_view, &dead_view, &focus_view, &done,
682 struct tog_view *prev = NULL;
684 if (view_is_parent_view(dead_view))
685 prev = TAILQ_PREV(dead_view,
686 tog_view_list_head, entry);
687 else if (view->parent != dead_view)
690 if (dead_view->parent)
691 dead_view->parent->child = NULL;
693 TAILQ_REMOVE(&views, dead_view, entry);
695 err = view_close(dead_view);
696 if (err || dead_view == main_view)
699 if (view == dead_view) {
704 else if (!TAILQ_EMPTY(&views))
705 view = TAILQ_LAST(&views,
710 if (view->child && view->child_focussed)
711 focus_view = view->child;
718 struct tog_view *v, *t;
719 /* Only allow one parent view per type. */
720 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
721 if (v->type != new_view->type)
723 TAILQ_REMOVE(&views, v, entry);
729 TAILQ_INSERT_TAIL(&views, new_view, entry);
731 if (focus_view == NULL)
732 focus_view = new_view;
735 show_panel(focus_view->panel);
738 focus_view->focussed = 1;
741 show_panel(new_view->panel);
742 if (view->child && view_is_splitscreen(view->child))
743 show_panel(view->child->panel);
746 if (focus_view == NULL) {
748 show_panel(view->panel);
749 if (view->child && view_is_splitscreen(view->child))
750 show_panel(view->child->panel);
754 err = view->parent->show(view->parent);
758 err = view->show(view);
762 err = view->child->show(view->child);
771 while (!TAILQ_EMPTY(&views)) {
772 view = TAILQ_FIRST(&views);
773 TAILQ_REMOVE(&views, view, entry);
777 errcode = pthread_mutex_unlock(&tog_mutex);
779 return got_error_set_errno(errcode, "pthread_mutex_unlock");
789 "usage: %s log [-c commit] [-r repository-path] [path]\n",
794 /* Create newly allocated wide-character string equivalent to a byte string. */
795 static const struct got_error *
796 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
799 const struct got_error *err = NULL;
802 *wlen = mbstowcs(NULL, s, 0);
803 if (*wlen == (size_t)-1) {
806 return got_error_prefix_errno("mbstowcs");
808 /* byte string invalid in current encoding; try to "fix" it */
809 err = got_mbsavis(&vis, &vislen, s);
812 *wlen = mbstowcs(NULL, vis, 0);
813 if (*wlen == (size_t)-1) {
814 err = got_error_prefix_errno("mbstowcs"); /* give up */
819 *ws = calloc(*wlen + 1, sizeof(*ws));
821 err = got_error_prefix_errno("calloc");
825 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
826 err = got_error_prefix_errno("mbstowcs");
837 /* Format a line for display, ensuring that it won't overflow a width limit. */
838 static const struct got_error *
839 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
841 const struct got_error *err = NULL;
843 wchar_t *wline = NULL;
850 err = mbs2ws(&wline, &wlen, line);
855 while (i < wlen && cols < wlimit) {
856 int width = wcwidth(wline[i]);
863 if (cols + width <= wlimit)
868 if (wline[i] == L'\t')
869 cols += TABSIZE - ((cols + 1) % TABSIZE);
873 err = got_error_prefix_errno("wcwidth");
888 static const struct got_error*
889 build_refs_str(char **refs_str, struct got_reflist_head *refs,
890 struct got_object_id *id)
892 static const struct got_error *err = NULL;
893 struct got_reflist_entry *re;
899 SIMPLEQ_FOREACH(re, refs, entry) {
900 if (got_object_id_cmp(re->id, id) != 0)
902 name = got_ref_get_name(re->ref);
903 if (strcmp(name, GOT_REF_HEAD) == 0)
905 if (strncmp(name, "refs/", 5) == 0)
907 if (strncmp(name, "got/", 4) == 0)
909 if (strncmp(name, "heads/", 6) == 0)
911 if (strncmp(name, "remotes/", 8) == 0)
914 if (asprintf(refs_str, "%s%s%s", s ? s : "",
915 s ? ", " : "", name) == -1) {
916 err = got_error_prefix_errno("asprintf");
927 static const struct got_error *
928 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
930 char *smallerthan, *at;
932 smallerthan = strchr(author, '<');
933 if (smallerthan && smallerthan[1] != '\0')
934 author = smallerthan + 1;
935 at = strchr(author, '@');
938 return format_line(wauthor, author_width, author, limit);
941 static const struct got_error *
942 draw_commit(struct tog_view *view, struct got_commit_object *commit,
943 struct got_object_id *id, struct got_reflist_head *refs,
944 int author_display_cols)
946 const struct got_error *err = NULL;
947 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
948 char *logmsg0 = NULL, *logmsg = NULL;
950 wchar_t *wlogmsg = NULL, *wauthor = NULL;
951 int author_width, logmsg_width;
952 char *newline, *line = NULL;
954 static const size_t date_display_cols = 9;
955 const int avail = view->ncols;
957 time_t committer_time;
959 committer_time = got_object_commit_get_committer_time(commit);
960 if (localtime_r(&committer_time, &tm) == NULL)
961 return got_error_prefix_errno("localtime_r");
962 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
964 return got_error(GOT_ERR_NO_SPACE);
966 if (avail < date_display_cols)
967 limit = MIN(sizeof(datebuf) - 1, avail);
969 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
970 waddnstr(view->window, datebuf, limit);
975 author = strdup(got_object_commit_get_author(commit));
976 if (author == NULL) {
977 err = got_error_prefix_errno("strdup");
980 err = format_author(&wauthor, &author_width, author, avail - col);
983 waddwstr(view->window, wauthor);
985 while (col <= avail && author_width < author_display_cols + 2) {
986 waddch(view->window, ' ');
993 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
994 if (logmsg0 == NULL) {
995 err = got_error_prefix_errno("strdup");
999 while (*logmsg == '\n')
1001 newline = strchr(logmsg, '\n');
1004 limit = avail - col;
1005 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1008 waddwstr(view->window, wlogmsg);
1009 col += logmsg_width;
1010 while (col <= avail) {
1011 waddch(view->window, ' ');
1023 static struct commit_queue_entry *
1024 alloc_commit_queue_entry(struct got_commit_object *commit,
1025 struct got_object_id *id)
1027 struct commit_queue_entry *entry;
1029 entry = calloc(1, sizeof(*entry));
1034 entry->commit = commit;
1039 pop_commit(struct commit_queue *commits)
1041 struct commit_queue_entry *entry;
1043 entry = TAILQ_FIRST(&commits->head);
1044 TAILQ_REMOVE(&commits->head, entry, entry);
1045 got_object_commit_close(entry->commit);
1046 commits->ncommits--;
1047 /* Don't free entry->id! It is owned by the commit graph. */
1052 free_commits(struct commit_queue *commits)
1054 while (!TAILQ_EMPTY(&commits->head))
1055 pop_commit(commits);
1058 static const struct got_error *
1059 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1060 int minqueue, struct got_repository *repo, const char *path)
1062 const struct got_error *err = NULL;
1066 * We keep all commits open throughout the lifetime of the log
1067 * view in order to avoid having to re-fetch commits from disk
1068 * while updating the display.
1070 while (nqueued < minqueue) {
1071 struct got_object_id *id;
1072 struct got_commit_object *commit;
1073 struct commit_queue_entry *entry;
1076 err = got_commit_graph_iter_next(&id, graph);
1078 if (err->code != GOT_ERR_ITER_NEED_MORE)
1080 err = got_commit_graph_fetch_commits(graph,
1090 err = got_object_open_as_commit(&commit, repo, id);
1093 entry = alloc_commit_queue_entry(commit, id);
1094 if (entry == NULL) {
1095 err = got_error_prefix_errno("alloc_commit_queue_entry");
1099 errcode = pthread_mutex_lock(&tog_mutex);
1101 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1105 entry->idx = commits->ncommits;
1106 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1108 commits->ncommits++;
1110 errcode = pthread_mutex_unlock(&tog_mutex);
1111 if (errcode && err == NULL)
1112 err = got_error_set_errno(errcode,
1113 "pthread_mutex_unlock");
1119 static const struct got_error *
1120 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1122 const struct got_error *err = NULL;
1123 struct got_reference *head_ref;
1127 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1131 err = got_ref_resolve(head_id, repo, head_ref);
1132 got_ref_close(head_ref);
1141 static const struct got_error *
1142 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1143 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1144 struct commit_queue *commits, int selected_idx, int limit,
1145 struct got_reflist_head *refs, const char *path, int commits_needed)
1147 const struct got_error *err = NULL;
1148 struct commit_queue_entry *entry;
1149 int ncommits, width;
1150 int author_cols = 10;
1151 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1152 char *refs_str = NULL;
1158 if (ncommits == selected_idx) {
1162 entry = TAILQ_NEXT(entry, entry);
1167 err = got_object_id_str(&id_str, (*selected)->id);
1171 err = build_refs_str(&refs_str, refs, (*selected)->id);
1177 if (commits_needed == 0)
1178 halfdelay(10); /* disable fast refresh */
1180 if (asprintf(&ncommits_str, " [%d/%d] %s",
1181 entry ? entry->idx + 1 : 0, commits->ncommits,
1182 commits_needed > 0 ? "loading... " :
1183 (refs_str ? refs_str : "")) == -1) {
1184 err = got_error_prefix_errno("asprintf");
1188 if (path && strcmp(path, "/") != 0) {
1189 if (asprintf(&header, "commit %s %s%s",
1190 id_str ? id_str : "........................................",
1191 path, ncommits_str) == -1) {
1192 err = got_error_prefix_errno("asprintf");
1196 } else if (asprintf(&header, "commit %s%s",
1197 id_str ? id_str : "........................................",
1198 ncommits_str) == -1) {
1199 err = got_error_prefix_errno("asprintf");
1203 err = format_line(&wline, &width, header, view->ncols);
1207 werase(view->window);
1209 if (view_needs_focus_indication(view))
1210 wstandout(view->window);
1211 waddwstr(view->window, wline);
1212 while (width < view->ncols) {
1213 waddch(view->window, ' ');
1216 if (view_needs_focus_indication(view))
1217 wstandend(view->window);
1222 /* Grow author column size if necessary. */
1229 if (ncommits >= limit - 1)
1231 author = strdup(got_object_commit_get_author(entry->commit));
1232 if (author == NULL) {
1233 err = got_error_prefix_errno("strdup");
1236 err = format_author(&wauthor, &width, author, COLS);
1237 if (author_cols < width)
1238 author_cols = width;
1241 entry = TAILQ_NEXT(entry, entry);
1248 if (ncommits >= limit - 1)
1250 if (ncommits == selected_idx)
1251 wstandout(view->window);
1252 err = draw_commit(view, entry->commit, entry->id, refs,
1254 if (ncommits == selected_idx)
1255 wstandend(view->window);
1260 entry = TAILQ_NEXT(entry, entry);
1273 scroll_up(struct tog_view *view,
1274 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1275 struct commit_queue *commits)
1277 struct commit_queue_entry *entry;
1280 entry = TAILQ_FIRST(&commits->head);
1281 if (*first_displayed_entry == entry) {
1286 entry = *first_displayed_entry;
1287 while (entry && nscrolled < maxscroll) {
1288 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1290 *first_displayed_entry = entry;
1296 static const struct got_error *
1297 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1298 pthread_cond_t *need_commits)
1303 halfdelay(1); /* fast refresh while loading commits */
1305 while (*commits_needed > 0) {
1309 /* Wake the log thread. */
1310 errcode = pthread_cond_signal(need_commits);
1312 return got_error_set_errno(errcode,
1313 "pthread_cond_signal");
1314 errcode = pthread_mutex_unlock(&tog_mutex);
1316 return got_error_set_errno(errcode,
1317 "pthread_mutex_unlock");
1319 errcode = pthread_mutex_lock(&tog_mutex);
1321 return got_error_set_errno(errcode,
1322 "pthread_mutex_lock");
1324 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1326 * Thread is not done yet; lose a key press
1327 * and let the user retry... this way the GUI
1328 * remains interactive while logging deep paths
1329 * with few commits in history.
1338 static const struct got_error *
1339 scroll_down(struct tog_view *view,
1340 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1341 struct commit_queue_entry **last_displayed_entry,
1342 struct commit_queue *commits, int *log_complete, int *commits_needed,
1343 pthread_cond_t *need_commits)
1345 const struct got_error *err = NULL;
1346 struct commit_queue_entry *pentry;
1349 if (*last_displayed_entry == NULL)
1352 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1353 if (pentry == NULL && !*log_complete) {
1355 * Ask the log thread for required amount of commits
1356 * plus some amount of pre-fetching.
1358 (*commits_needed) += maxscroll + 20;
1359 err = trigger_log_thread(0, commits_needed, log_complete,
1366 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1367 if (pentry == NULL) {
1373 *last_displayed_entry = pentry;
1375 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1378 *first_displayed_entry = pentry;
1379 } while (++nscrolled < maxscroll);
1384 static const struct got_error *
1385 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1386 struct got_commit_object *commit, struct got_object_id *commit_id,
1387 struct tog_view *log_view, struct got_reflist_head *refs,
1388 struct got_repository *repo)
1390 const struct got_error *err;
1391 struct got_object_qid *parent_id;
1392 struct tog_view *diff_view;
1394 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1395 if (diff_view == NULL)
1396 return got_error_prefix_errno("view_open");
1398 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1399 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1400 commit_id, log_view, refs, repo);
1402 *new_view = diff_view;
1406 static const struct got_error *
1407 browse_commit(struct tog_view **new_view, int begin_x,
1408 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1409 struct got_repository *repo)
1411 const struct got_error *err = NULL;
1412 struct got_tree_object *tree;
1413 struct tog_view *tree_view;
1415 err = got_object_open_as_tree(&tree, repo,
1416 got_object_commit_get_tree_id(entry->commit));
1420 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1421 if (tree_view == NULL)
1422 return got_error_prefix_errno("view_open");
1424 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1426 got_object_tree_close(tree);
1428 *new_view = tree_view;
1433 log_thread(void *arg)
1435 const struct got_error *err = NULL;
1437 struct tog_log_thread_args *a = arg;
1440 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1444 while (!done && !err) {
1445 err = queue_commits(a->graph, a->commits, 1, a->repo,
1448 if (err->code != GOT_ERR_ITER_COMPLETED)
1452 } else if (a->commits_needed > 0)
1453 a->commits_needed--;
1455 errcode = pthread_mutex_lock(&tog_mutex);
1457 err = got_error_set_errno(errcode,
1458 "pthread_mutex_lock");
1460 } else if (*a->quit)
1462 else if (*a->first_displayed_entry == NULL) {
1463 *a->first_displayed_entry =
1464 TAILQ_FIRST(&a->commits->head);
1465 *a->selected_entry = *a->first_displayed_entry;
1469 a->commits_needed = 0;
1470 else if (a->commits_needed == 0) {
1471 errcode = pthread_cond_wait(&a->need_commits,
1474 err = got_error_set_errno(errcode,
1475 "pthread_cond_wait");
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1479 if (errcode && err == NULL)
1480 err = got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1483 a->log_complete = 1;
1487 static const struct got_error *
1488 stop_log_thread(struct tog_log_view_state *s)
1490 const struct got_error *err = NULL;
1495 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1497 return got_error_set_errno(errcode,
1498 "pthread_cond_signal");
1499 errcode = pthread_mutex_unlock(&tog_mutex);
1501 return got_error_set_errno(errcode,
1502 "pthread_mutex_unlock");
1503 errcode = pthread_join(s->thread, (void **)&err);
1505 return got_error_set_errno(errcode, "pthread_join");
1506 errcode = pthread_mutex_lock(&tog_mutex);
1508 return got_error_set_errno(errcode,
1509 "pthread_mutex_lock");
1513 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1514 if (errcode && err == NULL)
1515 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1517 if (s->thread_args.repo) {
1518 got_repo_close(s->thread_args.repo);
1519 s->thread_args.repo = NULL;
1522 if (s->thread_args.graph) {
1523 got_commit_graph_close(s->thread_args.graph);
1524 s->thread_args.graph = NULL;
1530 static const struct got_error *
1531 close_log_view(struct tog_view *view)
1533 const struct got_error *err = NULL;
1534 struct tog_log_view_state *s = &view->state.log;
1536 err = stop_log_thread(s);
1537 free_commits(&s->commits);
1538 free(s->in_repo_path);
1539 s->in_repo_path = NULL;
1545 static const struct got_error *
1546 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1547 struct got_reflist_head *refs, struct got_repository *repo,
1548 const char *path, int check_disk)
1550 const struct got_error *err = NULL;
1551 struct tog_log_view_state *s = &view->state.log;
1552 struct got_repository *thread_repo = NULL;
1553 struct got_commit_graph *thread_graph = NULL;
1556 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1560 /* The commit queue only contains commits being displayed. */
1561 TAILQ_INIT(&s->commits.head);
1562 s->commits.ncommits = 0;
1566 s->start_id = got_object_id_dup(start_id);
1567 if (s->start_id == NULL) {
1568 err = got_error_prefix_errno("got_object_id_dup");
1572 view->show = show_log_view;
1573 view->input = input_log_view;
1574 view->close = close_log_view;
1576 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1579 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1584 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1586 err = got_error_set_errno(errcode, "pthread_cond_init");
1590 s->thread_args.commits_needed = view->nlines;
1591 s->thread_args.graph = thread_graph;
1592 s->thread_args.commits = &s->commits;
1593 s->thread_args.in_repo_path = s->in_repo_path;
1594 s->thread_args.start_id = s->start_id;
1595 s->thread_args.repo = thread_repo;
1596 s->thread_args.log_complete = 0;
1597 s->thread_args.quit = &s->quit;
1598 s->thread_args.view = view;
1599 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1600 s->thread_args.selected_entry = &s->selected_entry;
1603 close_log_view(view);
1607 static const struct got_error *
1608 show_log_view(struct tog_view *view)
1610 struct tog_log_view_state *s = &view->state.log;
1612 if (s->thread == NULL) {
1613 int errcode = pthread_create(&s->thread, NULL, log_thread,
1616 return got_error_set_errno(errcode, "pthread_create");
1619 return draw_commits(view, &s->last_displayed_entry,
1620 &s->selected_entry, s->first_displayed_entry,
1621 &s->commits, s->selected, view->nlines, s->refs,
1622 s->in_repo_path, s->thread_args.commits_needed);
1625 static const struct got_error *
1626 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1627 struct tog_view **focus_view, struct tog_view *view, int ch)
1629 const struct got_error *err = NULL;
1630 struct tog_log_view_state *s = &view->state.log;
1632 struct tog_view *diff_view = NULL, *tree_view = NULL;
1643 if (s->first_displayed_entry == NULL)
1645 if (s->selected > 0)
1647 if (s->selected > 0)
1649 scroll_up(view, &s->first_displayed_entry, 1,
1654 if (s->first_displayed_entry == NULL)
1656 if (TAILQ_FIRST(&s->commits.head) ==
1657 s->first_displayed_entry) {
1658 if (s->selected == 0) {
1665 scroll_up(view, &s->first_displayed_entry,
1666 view->nlines, &s->commits);
1672 if (s->first_displayed_entry == NULL)
1674 if (s->selected < MIN(view->nlines - 2,
1675 s->commits.ncommits - 1)) {
1679 err = scroll_down(view, &s->first_displayed_entry, 1,
1680 &s->last_displayed_entry, &s->commits,
1681 &s->thread_args.log_complete,
1682 &s->thread_args.commits_needed,
1683 &s->thread_args.need_commits);
1687 struct commit_queue_entry *first;
1688 first = s->first_displayed_entry;
1691 err = scroll_down(view, &s->first_displayed_entry,
1692 view->nlines, &s->last_displayed_entry,
1693 &s->commits, &s->thread_args.log_complete,
1694 &s->thread_args.commits_needed,
1695 &s->thread_args.need_commits);
1696 if (first == s->first_displayed_entry &&
1697 s->selected < MIN(view->nlines - 2,
1698 s->commits.ncommits - 1)) {
1699 /* can't scroll further down */
1700 s->selected = MIN(view->nlines - 2,
1701 s->commits.ncommits - 1);
1707 if (s->selected > view->nlines - 2)
1708 s->selected = view->nlines - 2;
1709 if (s->selected > s->commits.ncommits - 1)
1710 s->selected = s->commits.ncommits - 1;
1714 if (s->selected_entry == NULL)
1716 if (view_is_parent_view(view))
1717 begin_x = view_split_begin_x(view->begin_x);
1718 err = open_diff_view_for_commit(&diff_view, begin_x,
1719 s->selected_entry->commit, s->selected_entry->id,
1720 view, s->refs, s->repo);
1723 if (view_is_parent_view(view)) {
1724 err = view_close_child(view);
1727 err = view_set_child(view, diff_view);
1729 view_close(diff_view);
1732 *focus_view = diff_view;
1733 view->child_focussed = 1;
1735 *new_view = diff_view;
1738 if (s->selected_entry == NULL)
1740 if (view_is_parent_view(view))
1741 begin_x = view_split_begin_x(view->begin_x);
1742 err = browse_commit(&tree_view, begin_x,
1743 s->selected_entry, s->refs, s->repo);
1746 if (view_is_parent_view(view)) {
1747 err = view_close_child(view);
1750 err = view_set_child(view, tree_view);
1752 view_close(tree_view);
1755 *focus_view = tree_view;
1756 view->child_focussed = 1;
1758 *new_view = tree_view;
1761 if (strcmp(s->in_repo_path, "/") == 0)
1763 parent_path = dirname(s->in_repo_path);
1764 if (parent_path && strcmp(parent_path, ".") != 0) {
1765 struct tog_view *lv;
1766 err = stop_log_thread(s);
1769 lv = view_open(view->nlines, view->ncols,
1770 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1772 return got_error_prefix_errno(
1774 err = open_log_view(lv, s->start_id, s->refs,
1775 s->repo, parent_path, 0);
1778 if (view_is_parent_view(view))
1781 view_set_child(view->parent, lv);
1794 static const struct got_error *
1795 apply_unveil(const char *repo_path, const char *worktree_path)
1797 const struct got_error *error;
1799 if (repo_path && unveil(repo_path, "r") != 0)
1800 return got_error_prefix_errno2("unveil", repo_path);
1802 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1803 return got_error_prefix_errno2("unveil", worktree_path);
1805 if (unveil("/tmp", "rwc") != 0)
1806 return got_error_prefix_errno2("unveil", "/tmp");
1808 error = got_privsep_unveil_exec_helpers();
1812 if (unveil(NULL, NULL) != 0)
1813 return got_error_prefix_errno("unveil");
1823 halfdelay(1); /* Do fast refresh while initial view is loading. */
1826 intrflush(stdscr, FALSE);
1827 keypad(stdscr, TRUE);
1829 signal(SIGWINCH, tog_sigwinch);
1832 static const struct got_error *
1833 cmd_log(int argc, char *argv[])
1835 const struct got_error *error;
1836 struct got_repository *repo = NULL;
1837 struct got_worktree *worktree = NULL;
1838 struct got_reflist_head refs;
1839 struct got_object_id *start_id = NULL;
1840 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1841 char *start_commit = NULL;
1843 struct tog_view *view;
1845 SIMPLEQ_INIT(&refs);
1848 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1853 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1856 start_commit = optarg;
1859 repo_path = realpath(optarg, NULL);
1860 if (repo_path == NULL)
1861 err(1, "-r option");
1872 cwd = getcwd(NULL, 0);
1874 error = got_error_prefix_errno("getcwd");
1877 error = got_worktree_open(&worktree, cwd);
1878 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1885 error = got_error_prefix_errno("strdup");
1888 } else if (argc == 1) {
1890 error = got_worktree_resolve_path(&path, worktree,
1895 path = strdup(argv[0]);
1897 error = got_error_prefix_errno("strdup");
1904 repo_path = worktree ?
1905 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1906 if (repo_path == NULL) {
1907 error = got_error_prefix_errno("strdup");
1913 error = got_repo_open(&repo, repo_path);
1917 error = apply_unveil(got_repo_get_path(repo),
1918 worktree ? got_worktree_get_root_path(worktree) : NULL);
1922 if (start_commit == NULL)
1923 error = get_head_commit_id(&start_id, repo);
1925 error = got_object_resolve_id_str(&start_id, repo,
1930 error = got_ref_list(&refs, repo);
1934 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1936 error = got_error_prefix_errno("view_open");
1939 error = open_log_view(view, start_id, &refs, repo, path, 1);
1942 error = view_loop(view);
1949 got_repo_close(repo);
1951 got_worktree_close(worktree);
1952 got_ref_list_free(&refs);
1960 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1966 parse_next_line(FILE *f, size_t *len)
1971 const char delim[3] = { '\0', '\0', '\0'};
1973 line = fparseln(f, &linelen, &lineno, delim, 0);
1979 static const struct got_error *
1980 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1981 int *last_displayed_line, int *eof, int max_lines,
1984 const struct got_error *err;
1985 int nlines = 0, nprinted = 0;
1992 werase(view->window);
1995 err = format_line(&wline, &width, header, view->ncols);
2000 if (view_needs_focus_indication(view))
2001 wstandout(view->window);
2002 waddwstr(view->window, wline);
2003 if (view_needs_focus_indication(view))
2004 wstandend(view->window);
2005 if (width < view->ncols)
2006 waddch(view->window, '\n');
2014 while (nprinted < max_lines) {
2015 line = parse_next_line(f, &len);
2020 if (++nlines < *first_displayed_line) {
2025 err = format_line(&wline, &width, line, view->ncols);
2030 waddwstr(view->window, wline);
2031 if (width < view->ncols)
2032 waddch(view->window, '\n');
2033 if (++nprinted == 1)
2034 *first_displayed_line = nlines;
2039 *last_displayed_line = nlines;
2047 get_datestr(time_t *time, char *datebuf)
2049 char *p, *s = ctime_r(time, datebuf);
2050 p = strchr(s, '\n');
2056 static const struct got_error *
2057 write_commit_info(struct got_object_id *commit_id,
2058 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2060 const struct got_error *err = NULL;
2062 struct got_commit_object *commit;
2063 char *id_str = NULL;
2064 time_t committer_time;
2065 const char *author, *committer;
2066 char *refs_str = NULL;
2069 err = build_refs_str(&refs_str, refs, commit_id);
2074 err = got_object_open_as_commit(&commit, repo, commit_id);
2078 err = got_object_id_str(&id_str, commit_id);
2080 err = got_error_prefix_errno("got_object_id_str");
2084 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2085 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2086 err = got_error_prefix_errno("fprintf");
2089 if (fprintf(outfile, "from: %s\n",
2090 got_object_commit_get_author(commit)) < 0) {
2091 err = got_error_prefix_errno("fprintf");
2094 committer_time = got_object_commit_get_committer_time(commit);
2095 if (fprintf(outfile, "date: %s UTC\n",
2096 get_datestr(&committer_time, datebuf)) < 0) {
2097 err = got_error_prefix_errno("fprintf");
2100 author = got_object_commit_get_author(commit);
2101 committer = got_object_commit_get_committer(commit);
2102 if (strcmp(author, committer) != 0 &&
2103 fprintf(outfile, "via: %s\n", committer) < 0) {
2104 err = got_error_prefix_errno("fprintf");
2107 if (fprintf(outfile, "%s\n",
2108 got_object_commit_get_logmsg(commit)) < 0) {
2109 err = got_error_prefix_errno("fprintf");
2115 got_object_commit_close(commit);
2119 static const struct got_error *
2120 create_diff(struct tog_diff_view_state *s)
2122 const struct got_error *err = NULL;
2128 err = got_error_prefix_errno("got_opentemp");
2131 if (s->f && fclose(s->f) != 0) {
2132 err = got_error_prefix_errno("fclose");
2138 err = got_object_get_type(&obj_type, s->repo, s->id1);
2140 err = got_object_get_type(&obj_type, s->repo, s->id2);
2145 case GOT_OBJ_TYPE_BLOB:
2146 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2147 s->diff_context, s->repo, f);
2149 case GOT_OBJ_TYPE_TREE:
2150 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2151 s->diff_context, s->repo, f);
2153 case GOT_OBJ_TYPE_COMMIT: {
2154 const struct got_object_id_queue *parent_ids;
2155 struct got_object_qid *pid;
2156 struct got_commit_object *commit2;
2158 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2161 /* Show commit info if we're diffing to a parent/root commit. */
2163 write_commit_info(s->id2, s->refs, s->repo, f);
2165 parent_ids = got_object_commit_get_parent_ids(commit2);
2166 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2167 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2168 write_commit_info(s->id2, s->refs,
2174 got_object_commit_close(commit2);
2176 err = got_diff_objects_as_commits(s->id1, s->id2,
2177 s->diff_context, s->repo, f);
2181 err = got_error(GOT_ERR_OBJ_TYPE);
2185 if (f && fflush(f) != 0 && err == NULL)
2186 err = got_error_prefix_errno("fflush");
2191 diff_view_indicate_progress(struct tog_view *view)
2193 werase(view->window);
2194 waddstr(view->window, "diffing...");
2199 static const struct got_error *
2200 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2201 struct got_object_id *id2, struct tog_view *log_view,
2202 struct got_reflist_head *refs, struct got_repository *repo)
2204 const struct got_error *err;
2206 if (id1 != NULL && id2 != NULL) {
2208 err = got_object_get_type(&type1, repo, id1);
2211 err = got_object_get_type(&type2, repo, id2);
2216 return got_error(GOT_ERR_OBJ_TYPE);
2220 view->state.diff.id1 = got_object_id_dup(id1);
2221 if (view->state.diff.id1 == NULL)
2222 return got_error_prefix_errno("got_object_id_dup");
2224 view->state.diff.id1 = NULL;
2226 view->state.diff.id2 = got_object_id_dup(id2);
2227 if (view->state.diff.id2 == NULL) {
2228 free(view->state.diff.id1);
2229 view->state.diff.id1 = NULL;
2230 return got_error_prefix_errno("got_object_id_dup");
2232 view->state.diff.f = NULL;
2233 view->state.diff.first_displayed_line = 1;
2234 view->state.diff.last_displayed_line = view->nlines;
2235 view->state.diff.diff_context = 3;
2236 view->state.diff.log_view = log_view;
2237 view->state.diff.repo = repo;
2238 view->state.diff.refs = refs;
2240 if (log_view && view_is_splitscreen(view))
2241 show_log_view(log_view); /* draw vborder */
2242 diff_view_indicate_progress(view);
2244 err = create_diff(&view->state.diff);
2246 free(view->state.diff.id1);
2247 view->state.diff.id1 = NULL;
2248 free(view->state.diff.id2);
2249 view->state.diff.id2 = NULL;
2253 view->show = show_diff_view;
2254 view->input = input_diff_view;
2255 view->close = close_diff_view;
2260 static const struct got_error *
2261 close_diff_view(struct tog_view *view)
2263 const struct got_error *err = NULL;
2265 free(view->state.diff.id1);
2266 view->state.diff.id1 = NULL;
2267 free(view->state.diff.id2);
2268 view->state.diff.id2 = NULL;
2269 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2270 err = got_error_prefix_errno("fclose");
2274 static const struct got_error *
2275 show_diff_view(struct tog_view *view)
2277 const struct got_error *err;
2278 struct tog_diff_view_state *s = &view->state.diff;
2279 char *id_str1 = NULL, *id_str2, *header;
2282 err = got_object_id_str(&id_str1, s->id1);
2286 err = got_object_id_str(&id_str2, s->id2);
2290 if (asprintf(&header, "diff %s %s",
2291 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2292 err = got_error_prefix_errno("asprintf");
2300 return draw_file(view, s->f, &s->first_displayed_line,
2301 &s->last_displayed_line, &s->eof, view->nlines,
2305 static const struct got_error *
2306 set_selected_commit(struct tog_diff_view_state *s,
2307 struct commit_queue_entry *entry)
2309 const struct got_error *err;
2310 const struct got_object_id_queue *parent_ids;
2311 struct got_commit_object *selected_commit;
2312 struct got_object_qid *pid;
2315 s->id2 = got_object_id_dup(entry->id);
2317 return got_error_prefix_errno("got_object_id_dup");
2319 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2322 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2324 pid = SIMPLEQ_FIRST(parent_ids);
2325 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2326 got_object_commit_close(selected_commit);
2330 static const struct got_error *
2331 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2332 struct tog_view **focus_view, struct tog_view *view, int ch)
2334 const struct got_error *err = NULL;
2335 struct tog_diff_view_state *s = &view->state.diff;
2336 struct tog_log_view_state *ls;
2337 struct commit_queue_entry *entry;
2343 if (s->first_displayed_line > 1)
2344 s->first_displayed_line--;
2349 if (s->first_displayed_line == 1) {
2354 while (i++ < view->nlines - 1 &&
2355 s->first_displayed_line > 1)
2356 s->first_displayed_line--;
2361 s->first_displayed_line++;
2372 while (!s->eof && i++ < view->nlines - 1) {
2374 line = parse_next_line(s->f, NULL);
2375 s->first_displayed_line++;
2381 if (s->diff_context > 0) {
2383 diff_view_indicate_progress(view);
2384 err = create_diff(s);
2388 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2390 diff_view_indicate_progress(view);
2391 err = create_diff(s);
2396 if (s->log_view == NULL)
2398 ls = &s->log_view->state.log;
2399 entry = TAILQ_PREV(ls->selected_entry,
2400 commit_queue_head, entry);
2404 err = input_log_view(NULL, NULL, NULL, s->log_view,
2409 err = set_selected_commit(s, entry);
2413 s->first_displayed_line = 1;
2414 s->last_displayed_line = view->nlines;
2416 diff_view_indicate_progress(view);
2417 err = create_diff(s);
2421 if (s->log_view == NULL)
2423 ls = &s->log_view->state.log;
2425 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2426 ls->thread_args.commits_needed++;
2428 /* Display "loading..." in log view. */
2429 show_log_view(s->log_view);
2433 err = trigger_log_thread(1 /* load_all */,
2434 &ls->thread_args.commits_needed,
2435 &ls->thread_args.log_complete,
2436 &ls->thread_args.need_commits);
2440 err = input_log_view(NULL, NULL, NULL, s->log_view,
2445 entry = TAILQ_NEXT(ls->selected_entry, entry);
2449 err = set_selected_commit(s, entry);
2453 s->first_displayed_line = 1;
2454 s->last_displayed_line = view->nlines;
2456 diff_view_indicate_progress(view);
2457 err = create_diff(s);
2466 static const struct got_error *
2467 cmd_diff(int argc, char *argv[])
2469 const struct got_error *error = NULL;
2470 struct got_repository *repo = NULL;
2471 struct got_reflist_head refs;
2472 struct got_object_id *id1 = NULL, *id2 = NULL;
2473 char *repo_path = NULL;
2474 char *id_str1 = NULL, *id_str2 = NULL;
2476 struct tog_view *view;
2478 SIMPLEQ_INIT(&refs);
2481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2486 while ((ch = getopt(argc, argv, "")) != -1) {
2498 usage_diff(); /* TODO show local worktree changes */
2499 } else if (argc == 2) {
2500 repo_path = getcwd(NULL, 0);
2501 if (repo_path == NULL)
2502 return got_error_prefix_errno("getcwd");
2505 } else if (argc == 3) {
2506 repo_path = realpath(argv[0], NULL);
2507 if (repo_path == NULL)
2508 return got_error_prefix_errno2("realpath", argv[0]);
2516 error = got_repo_open(&repo, repo_path);
2520 error = apply_unveil(got_repo_get_path(repo), NULL);
2524 error = got_object_resolve_id_str(&id1, repo, id_str1);
2528 error = got_object_resolve_id_str(&id2, repo, id_str2);
2532 error = got_ref_list(&refs, repo);
2536 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2538 error = got_error_prefix_errno("view_open");
2541 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2544 error = view_loop(view);
2547 got_repo_close(repo);
2548 got_ref_list_free(&refs);
2556 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2561 struct tog_blame_line {
2563 struct got_object_id *id;
2566 static const struct got_error *
2567 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2568 const char *path, struct tog_blame_line *lines, int nlines,
2569 int blame_complete, int selected_line, int *first_displayed_line,
2570 int *last_displayed_line, int *eof, int max_lines)
2572 const struct got_error *err;
2573 int lineno = 0, nprinted = 0;
2578 struct tog_blame_line *blame_line;
2579 struct got_object_id *prev_id = NULL;
2582 err = got_object_id_str(&id_str, id);
2587 werase(view->window);
2589 if (asprintf(&line, "commit %s", id_str) == -1) {
2590 err = got_error_prefix_errno("asprintf");
2595 err = format_line(&wline, &width, line, view->ncols);
2598 if (view_needs_focus_indication(view))
2599 wstandout(view->window);
2600 waddwstr(view->window, wline);
2601 if (view_needs_focus_indication(view))
2602 wstandend(view->window);
2605 if (width < view->ncols)
2606 waddch(view->window, '\n');
2608 if (asprintf(&line, "[%d/%d] %s%s",
2609 *first_displayed_line - 1 + selected_line, nlines,
2610 blame_complete ? "" : "annotating... ", path) == -1) {
2612 return got_error_prefix_errno("asprintf");
2615 err = format_line(&wline, &width, line, view->ncols);
2620 waddwstr(view->window, wline);
2623 if (width < view->ncols)
2624 waddch(view->window, '\n');
2627 while (nprinted < max_lines - 2) {
2628 line = parse_next_line(f, &len);
2633 if (++lineno < *first_displayed_line) {
2638 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2639 err = format_line(&wline, &width, line, wlimit);
2645 if (view->focussed && nprinted == selected_line - 1)
2646 wstandout(view->window);
2648 blame_line = &lines[lineno - 1];
2649 if (blame_line->annotated && prev_id &&
2650 got_object_id_cmp(prev_id, blame_line->id) == 0)
2651 waddstr(view->window, " ");
2652 else if (blame_line->annotated) {
2654 err = got_object_id_str(&id_str, blame_line->id);
2660 wprintw(view->window, "%.8s ", id_str);
2662 prev_id = blame_line->id;
2664 waddstr(view->window, "........ ");
2668 waddwstr(view->window, wline);
2669 while (width < wlimit) {
2670 waddch(view->window, ' ');
2673 if (view->focussed && nprinted == selected_line - 1)
2674 wstandend(view->window);
2675 if (++nprinted == 1)
2676 *first_displayed_line = lineno;
2681 *last_displayed_line = lineno;
2688 static const struct got_error *
2689 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2691 const struct got_error *err = NULL;
2692 struct tog_blame_cb_args *a = arg;
2693 struct tog_blame_line *line;
2696 if (nlines != a->nlines ||
2697 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2698 return got_error(GOT_ERR_RANGE);
2700 errcode = pthread_mutex_lock(&tog_mutex);
2702 return got_error_set_errno(errcode, "pthread_mutex_lock");
2704 if (*a->quit) { /* user has quit the blame view */
2705 err = got_error(GOT_ERR_ITER_COMPLETED);
2710 goto done; /* no change in this commit */
2712 line = &a->lines[lineno - 1];
2713 if (line->annotated)
2716 line->id = got_object_id_dup(id);
2717 if (line->id == NULL) {
2718 err = got_error_prefix_errno("got_object_id_dup");
2721 line->annotated = 1;
2723 errcode = pthread_mutex_unlock(&tog_mutex);
2725 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2730 blame_thread(void *arg)
2732 const struct got_error *err;
2733 struct tog_blame_thread_args *ta = arg;
2734 struct tog_blame_cb_args *a = ta->cb_args;
2737 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2738 blame_cb, ta->cb_args);
2740 errcode = pthread_mutex_lock(&tog_mutex);
2742 return (void *)got_error_set_errno(errcode,
2743 "pthread_mutex_lock");
2745 got_repo_close(ta->repo);
2749 errcode = pthread_mutex_unlock(&tog_mutex);
2750 if (errcode && err == NULL)
2751 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2756 static struct got_object_id *
2757 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2760 struct tog_blame_line *line;
2762 line = &lines[first_displayed_line - 1 + selected_line - 1];
2763 if (!line->annotated)
2769 static const struct got_error *
2770 stop_blame(struct tog_blame *blame)
2772 const struct got_error *err = NULL;
2775 if (blame->thread) {
2777 errcode = pthread_mutex_unlock(&tog_mutex);
2779 return got_error_set_errno(errcode,
2780 "pthread_mutex_unlock");
2781 errcode = pthread_join(blame->thread, (void **)&err);
2783 return got_error_set_errno(errcode, "pthread_join");
2784 errcode = pthread_mutex_lock(&tog_mutex);
2786 return got_error_set_errno(errcode,
2787 "pthread_mutex_lock");
2788 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2790 blame->thread = NULL;
2792 if (blame->thread_args.repo) {
2793 got_repo_close(blame->thread_args.repo);
2794 blame->thread_args.repo = NULL;
2797 if (fclose(blame->f) != 0 && err == NULL)
2798 err = got_error_prefix_errno("fclose");
2802 for (i = 0; i < blame->nlines; i++)
2803 free(blame->lines[i].id);
2805 blame->lines = NULL;
2807 free(blame->cb_args.commit_id);
2808 blame->cb_args.commit_id = NULL;
2813 static const struct got_error *
2814 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2815 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2816 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2817 struct got_repository *repo)
2819 const struct got_error *err = NULL;
2820 struct got_blob_object *blob = NULL;
2821 struct got_repository *thread_repo = NULL;
2822 struct got_object_id *obj_id = NULL;
2825 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2829 return got_error(GOT_ERR_NO_OBJ);
2831 err = got_object_get_type(&obj_type, repo, obj_id);
2835 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2836 err = got_error(GOT_ERR_OBJ_TYPE);
2840 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2843 blame->f = got_opentemp();
2844 if (blame->f == NULL) {
2845 err = got_error_prefix_errno("got_opentemp");
2848 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2853 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2854 if (blame->lines == NULL) {
2855 err = got_error_prefix_errno("calloc");
2859 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2863 blame->cb_args.view = view;
2864 blame->cb_args.lines = blame->lines;
2865 blame->cb_args.nlines = blame->nlines;
2866 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2867 if (blame->cb_args.commit_id == NULL) {
2868 err = got_error_prefix_errno("got_object_id_dup");
2871 blame->cb_args.quit = done;
2873 blame->thread_args.path = path;
2874 blame->thread_args.repo = thread_repo;
2875 blame->thread_args.cb_args = &blame->cb_args;
2876 blame->thread_args.complete = blame_complete;
2877 *blame_complete = 0;
2881 got_object_blob_close(blob);
2888 static const struct got_error *
2889 open_blame_view(struct tog_view *view, char *path,
2890 struct got_object_id *commit_id, struct got_reflist_head *refs,
2891 struct got_repository *repo)
2893 const struct got_error *err = NULL;
2894 struct tog_blame_view_state *s = &view->state.blame;
2896 SIMPLEQ_INIT(&s->blamed_commits);
2898 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2902 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2903 s->first_displayed_line = 1;
2904 s->last_displayed_line = view->nlines;
2905 s->selected_line = 1;
2906 s->blame_complete = 0;
2908 if (s->path == NULL)
2909 return got_error_prefix_errno("open_blame_view");
2912 s->commit_id = commit_id;
2913 memset(&s->blame, 0, sizeof(s->blame));
2915 view->show = show_blame_view;
2916 view->input = input_blame_view;
2917 view->close = close_blame_view;
2919 return run_blame(&s->blame, view, &s->blame_complete,
2920 &s->first_displayed_line, &s->last_displayed_line,
2921 &s->selected_line, &s->done, &s->eof, s->path,
2922 s->blamed_commit->id, s->repo);
2925 static const struct got_error *
2926 close_blame_view(struct tog_view *view)
2928 const struct got_error *err = NULL;
2929 struct tog_blame_view_state *s = &view->state.blame;
2931 if (s->blame.thread)
2932 err = stop_blame(&s->blame);
2934 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2935 struct got_object_qid *blamed_commit;
2936 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2937 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2938 got_object_qid_free(blamed_commit);
2946 static const struct got_error *
2947 show_blame_view(struct tog_view *view)
2949 const struct got_error *err = NULL;
2950 struct tog_blame_view_state *s = &view->state.blame;
2953 if (s->blame.thread == NULL) {
2954 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2955 &s->blame.thread_args);
2957 return got_error_set_errno(errcode, "pthread_create");
2960 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2961 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2962 s->selected_line, &s->first_displayed_line,
2963 &s->last_displayed_line, &s->eof, view->nlines);
2969 static const struct got_error *
2970 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2971 struct tog_view **focus_view, struct tog_view *view, int ch)
2973 const struct got_error *err = NULL, *thread_err = NULL;
2974 struct tog_view *diff_view;
2975 struct tog_blame_view_state *s = &view->state.blame;
2984 if (s->selected_line > 1)
2986 else if (s->selected_line == 1 &&
2987 s->first_displayed_line > 1)
2988 s->first_displayed_line--;
2993 if (s->first_displayed_line == 1) {
2994 if (s->selected_line == 1) {
2998 s->selected_line = 1;
3001 if (s->first_displayed_line > view->nlines - 2)
3002 s->first_displayed_line -=
3005 s->first_displayed_line = 1;
3009 if (s->selected_line < view->nlines - 2 &&
3010 s->first_displayed_line +
3011 s->selected_line <= s->blame.nlines)
3013 else if (s->last_displayed_line <
3015 s->first_displayed_line++;
3021 struct got_object_id *id = NULL;
3022 id = get_selected_commit_id(s->blame.lines,
3023 s->first_displayed_line, s->selected_line);
3027 struct got_commit_object *commit;
3028 struct got_object_qid *pid;
3029 struct got_object_id *blob_id = NULL;
3031 err = got_object_open_as_commit(&commit,
3035 pid = SIMPLEQ_FIRST(
3036 got_object_commit_get_parent_ids(commit));
3038 got_object_commit_close(commit);
3041 /* Check if path history ends here. */
3042 err = got_object_id_by_path(&blob_id, s->repo,
3045 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3047 got_object_commit_close(commit);
3050 err = got_object_get_type(&obj_type, s->repo,
3053 /* Can't blame non-blob type objects. */
3054 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3055 got_object_commit_close(commit);
3058 err = got_object_qid_alloc(&s->blamed_commit,
3060 got_object_commit_close(commit);
3062 if (got_object_id_cmp(id,
3063 s->blamed_commit->id) == 0)
3065 err = got_object_qid_alloc(&s->blamed_commit,
3071 thread_err = stop_blame(&s->blame);
3075 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3076 s->blamed_commit, entry);
3077 err = run_blame(&s->blame, view, &s->blame_complete,
3078 &s->first_displayed_line, &s->last_displayed_line,
3079 &s->selected_line, &s->done, &s->eof,
3080 s->path, s->blamed_commit->id, s->repo);
3086 struct got_object_qid *first;
3087 first = SIMPLEQ_FIRST(&s->blamed_commits);
3088 if (!got_object_id_cmp(first->id, s->commit_id))
3091 thread_err = stop_blame(&s->blame);
3095 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3096 got_object_qid_free(s->blamed_commit);
3098 SIMPLEQ_FIRST(&s->blamed_commits);
3099 err = run_blame(&s->blame, view, &s->blame_complete,
3100 &s->first_displayed_line, &s->last_displayed_line,
3101 &s->selected_line, &s->done, &s->eof, s->path,
3102 s->blamed_commit->id, s->repo);
3109 struct got_object_id *id = NULL;
3110 struct got_object_qid *pid;
3111 struct got_commit_object *commit = NULL;
3112 id = get_selected_commit_id(s->blame.lines,
3113 s->first_displayed_line, s->selected_line);
3116 err = got_object_open_as_commit(&commit, s->repo, id);
3119 pid = SIMPLEQ_FIRST(
3120 got_object_commit_get_parent_ids(commit));
3121 if (view_is_parent_view(view))
3122 begin_x = view_split_begin_x(view->begin_x);
3123 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3124 if (diff_view == NULL) {
3125 got_object_commit_close(commit);
3126 err = got_error_prefix_errno("view_open");
3129 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3130 id, NULL, s->refs, s->repo);
3131 got_object_commit_close(commit);
3133 view_close(diff_view);
3136 if (view_is_parent_view(view)) {
3137 err = view_close_child(view);
3140 err = view_set_child(view, diff_view);
3142 view_close(diff_view);
3145 *focus_view = diff_view;
3146 view->child_focussed = 1;
3148 *new_view = diff_view;
3155 if (s->last_displayed_line >= s->blame.nlines &&
3156 s->selected_line >= MIN(s->blame.nlines,
3157 view->nlines - 2)) {
3161 if (s->last_displayed_line >= s->blame.nlines &&
3162 s->selected_line < view->nlines - 2) {
3163 s->selected_line = MIN(s->blame.nlines,
3167 if (s->last_displayed_line + view->nlines - 2
3169 s->first_displayed_line +=
3172 s->first_displayed_line =
3177 if (s->selected_line > view->nlines - 2) {
3178 s->selected_line = MIN(s->blame.nlines,
3185 return thread_err ? thread_err : err;
3188 static const struct got_error *
3189 cmd_blame(int argc, char *argv[])
3191 const struct got_error *error;
3192 struct got_repository *repo = NULL;
3193 struct got_reflist_head refs;
3194 struct got_worktree *worktree = NULL;
3195 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3196 struct got_object_id *commit_id = NULL;
3197 char *commit_id_str = NULL;
3199 struct tog_view *view;
3201 SIMPLEQ_INIT(&refs);
3204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3212 commit_id_str = optarg;
3215 repo_path = realpath(optarg, NULL);
3216 if (repo_path == NULL)
3217 err(1, "-r option");
3233 cwd = getcwd(NULL, 0);
3235 error = got_error_prefix_errno("getcwd");
3238 if (repo_path == NULL) {
3239 error = got_worktree_open(&worktree, cwd);
3240 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3246 strdup(got_worktree_get_repo_path(worktree));
3247 if (repo_path == NULL)
3248 error = got_error_prefix_errno("strdup");
3252 repo_path = strdup(cwd);
3253 if (repo_path == NULL) {
3254 error = got_error_prefix_errno("strdup");
3262 error = got_repo_open(&repo, repo_path);
3266 error = apply_unveil(got_repo_get_path(repo), NULL);
3271 const char *prefix = got_worktree_get_path_prefix(worktree);
3272 char *p, *worktree_subdir = cwd +
3273 strlen(got_worktree_get_root_path(worktree));
3274 if (asprintf(&p, "%s%s%s%s%s",
3275 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3276 worktree_subdir, worktree_subdir[0] ? "/" : "",
3278 error = got_error_prefix_errno("asprintf");
3281 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3284 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3289 if (commit_id_str == NULL) {
3290 struct got_reference *head_ref;
3291 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3294 error = got_ref_resolve(&commit_id, repo, head_ref);
3295 got_ref_close(head_ref);
3297 error = got_object_resolve_id_str(&commit_id, repo,
3303 error = got_ref_list(&refs, repo);
3307 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3309 error = got_error_prefix_errno("view_open");
3312 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3315 error = view_loop(view);
3321 got_worktree_close(worktree);
3323 got_repo_close(repo);
3324 got_ref_list_free(&refs);
3328 static const struct got_error *
3329 draw_tree_entries(struct tog_view *view,
3330 struct got_tree_entry **first_displayed_entry,
3331 struct got_tree_entry **last_displayed_entry,
3332 struct got_tree_entry **selected_entry, int *ndisplayed,
3333 const char *label, int show_ids, const char *parent_path,
3334 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3336 const struct got_error *err = NULL;
3337 struct got_tree_entry *te;
3343 werase(view->window);
3348 err = format_line(&wline, &width, label, view->ncols);
3351 if (view_needs_focus_indication(view))
3352 wstandout(view->window);
3353 waddwstr(view->window, wline);
3354 if (view_needs_focus_indication(view))
3355 wstandend(view->window);
3358 if (width < view->ncols)
3359 waddch(view->window, '\n');
3362 err = format_line(&wline, &width, parent_path, view->ncols);
3365 waddwstr(view->window, wline);
3368 if (width < view->ncols)
3369 waddch(view->window, '\n');
3372 waddch(view->window, '\n');
3376 te = SIMPLEQ_FIRST(&entries->head);
3377 if (*first_displayed_entry == NULL) {
3378 if (selected == 0) {
3380 wstandout(view->window);
3381 *selected_entry = NULL;
3383 waddstr(view->window, " ..\n"); /* parent directory */
3384 if (selected == 0 && view->focussed)
3385 wstandend(view->window);
3392 while (te != *first_displayed_entry)
3393 te = SIMPLEQ_NEXT(te, entry);
3397 char *line = NULL, *id_str = NULL;
3400 err = got_object_id_str(&id_str, te->id);
3402 return got_error_prefix_errno(
3403 "got_object_id_str");
3405 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3406 te->name, S_ISDIR(te->mode) ? "/" :
3407 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3409 return got_error_prefix_errno("asprintf");
3412 err = format_line(&wline, &width, line, view->ncols);
3417 if (n == selected) {
3419 wstandout(view->window);
3420 *selected_entry = te;
3422 waddwstr(view->window, wline);
3423 if (width < view->ncols)
3424 waddch(view->window, '\n');
3425 if (n == selected && view->focussed)
3426 wstandend(view->window);
3432 *last_displayed_entry = te;
3435 te = SIMPLEQ_NEXT(te, entry);
3442 tree_scroll_up(struct tog_view *view,
3443 struct got_tree_entry **first_displayed_entry, int maxscroll,
3444 const struct got_tree_entries *entries, int isroot)
3446 struct got_tree_entry *te, *prev;
3449 if (*first_displayed_entry == NULL) {
3454 te = SIMPLEQ_FIRST(&entries->head);
3455 if (*first_displayed_entry == te) {
3458 *first_displayed_entry = NULL;
3462 /* XXX this is stupid... switch to TAILQ? */
3463 for (i = 0; i < maxscroll; i++) {
3464 while (te != *first_displayed_entry) {
3466 te = SIMPLEQ_NEXT(te, entry);
3468 *first_displayed_entry = prev;
3469 te = SIMPLEQ_FIRST(&entries->head);
3471 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3472 *first_displayed_entry = NULL;
3476 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3477 struct got_tree_entry *last_displayed_entry,
3478 const struct got_tree_entries *entries)
3480 struct got_tree_entry *next, *last;
3483 if (*first_displayed_entry)
3484 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3486 next = SIMPLEQ_FIRST(&entries->head);
3487 last = last_displayed_entry;
3488 while (next && last && n++ < maxscroll) {
3489 last = SIMPLEQ_NEXT(last, entry);
3491 *first_displayed_entry = next;
3492 next = SIMPLEQ_NEXT(next, entry);
3498 static const struct got_error *
3499 tree_entry_path(char **path, struct tog_parent_trees *parents,
3500 struct got_tree_entry *te)
3502 const struct got_error *err = NULL;
3503 struct tog_parent_tree *pt;
3504 size_t len = 2; /* for leading slash and NUL */
3506 TAILQ_FOREACH(pt, parents, entry)
3507 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3509 len += strlen(te->name);
3511 *path = calloc(1, len);
3513 return got_error_prefix_errno("calloc");
3516 pt = TAILQ_LAST(parents, tog_parent_trees);
3518 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3519 err = got_error(GOT_ERR_NO_SPACE);
3522 if (strlcat(*path, "/", len) >= len) {
3523 err = got_error(GOT_ERR_NO_SPACE);
3526 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3529 if (strlcat(*path, te->name, len) >= len) {
3530 err = got_error(GOT_ERR_NO_SPACE);
3542 static const struct got_error *
3543 blame_tree_entry(struct tog_view **new_view, int begin_x,
3544 struct got_tree_entry *te, struct tog_parent_trees *parents,
3545 struct got_object_id *commit_id, struct got_reflist_head *refs,
3546 struct got_repository *repo)
3548 const struct got_error *err = NULL;
3550 struct tog_view *blame_view;
3552 err = tree_entry_path(&path, parents, te);
3556 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3557 if (blame_view == NULL)
3558 return got_error_prefix_errno("view_open");
3560 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3562 view_close(blame_view);
3565 *new_view = blame_view;
3569 static const struct got_error *
3570 log_tree_entry(struct tog_view **new_view, int begin_x,
3571 struct got_tree_entry *te, struct tog_parent_trees *parents,
3572 struct got_object_id *commit_id, struct got_reflist_head *refs,
3573 struct got_repository *repo)
3575 struct tog_view *log_view;
3576 const struct got_error *err = NULL;
3579 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3580 if (log_view == NULL)
3581 return got_error_prefix_errno("view_open");
3583 err = tree_entry_path(&path, parents, te);
3587 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3589 view_close(log_view);
3591 *new_view = log_view;
3596 static const struct got_error *
3597 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3598 struct got_object_id *commit_id, struct got_reflist_head *refs,
3599 struct got_repository *repo)
3601 const struct got_error *err = NULL;
3602 char *commit_id_str = NULL;
3603 struct tog_tree_view_state *s = &view->state.tree;
3605 TAILQ_INIT(&s->parents);
3607 err = got_object_id_str(&commit_id_str, commit_id);
3611 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3612 err = got_error_prefix_errno("asprintf");
3616 s->root = s->tree = root;
3617 s->entries = got_object_tree_get_entries(root);
3618 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3619 s->commit_id = got_object_id_dup(commit_id);
3620 if (s->commit_id == NULL) {
3621 err = got_error_prefix_errno("got_object_id_dup");
3627 view->show = show_tree_view;
3628 view->input = input_tree_view;
3629 view->close = close_tree_view;
3631 free(commit_id_str);
3633 free(s->tree_label);
3634 s->tree_label = NULL;
3639 static const struct got_error *
3640 close_tree_view(struct tog_view *view)
3642 struct tog_tree_view_state *s = &view->state.tree;
3644 free(s->tree_label);
3645 s->tree_label = NULL;
3647 s->commit_id = NULL;
3648 while (!TAILQ_EMPTY(&s->parents)) {
3649 struct tog_parent_tree *parent;
3650 parent = TAILQ_FIRST(&s->parents);
3651 TAILQ_REMOVE(&s->parents, parent, entry);
3655 if (s->tree != s->root)
3656 got_object_tree_close(s->tree);
3657 got_object_tree_close(s->root);
3662 static const struct got_error *
3663 show_tree_view(struct tog_view *view)
3665 const struct got_error *err = NULL;
3666 struct tog_tree_view_state *s = &view->state.tree;
3669 err = tree_entry_path(&parent_path, &s->parents, NULL);
3673 err = draw_tree_entries(view, &s->first_displayed_entry,
3674 &s->last_displayed_entry, &s->selected_entry,
3675 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3676 s->entries, s->selected, view->nlines, s->tree == s->root);
3683 static const struct got_error *
3684 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3685 struct tog_view **focus_view, struct tog_view *view, int ch)
3687 const struct got_error *err = NULL;
3688 struct tog_tree_view_state *s = &view->state.tree;
3689 struct tog_view *log_view;
3690 int begin_x = 0, nscrolled;
3694 s->show_ids = !s->show_ids;
3697 if (!s->selected_entry)
3699 if (view_is_parent_view(view))
3700 begin_x = view_split_begin_x(view->begin_x);
3701 err = log_tree_entry(&log_view, begin_x,
3702 s->selected_entry, &s->parents,
3703 s->commit_id, s->refs, s->repo);
3704 if (view_is_parent_view(view)) {
3705 err = view_close_child(view);
3708 err = view_set_child(view, log_view);
3710 view_close(log_view);
3713 *focus_view = log_view;
3714 view->child_focussed = 1;
3716 *new_view = log_view;
3720 if (s->selected > 0) {
3722 if (s->selected == 0)
3725 if (s->selected > 0)
3727 tree_scroll_up(view, &s->first_displayed_entry, 1,
3728 s->entries, s->tree == s->root);
3731 tree_scroll_up(view, &s->first_displayed_entry,
3732 MAX(0, view->nlines - 4 - s->selected), s->entries,
3733 s->tree == s->root);
3735 if (SIMPLEQ_FIRST(&s->entries->head) ==
3736 s->first_displayed_entry && s->tree != s->root)
3737 s->first_displayed_entry = NULL;
3741 if (s->selected < s->ndisplayed - 1) {
3745 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3747 /* can't scroll any further */
3751 tree_scroll_down(&s->first_displayed_entry, 1,
3752 s->last_displayed_entry, s->entries);
3755 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3757 /* can't scroll any further; move cursor down */
3758 if (s->selected < s->ndisplayed - 1)
3759 s->selected = s->ndisplayed - 1;
3764 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3765 view->nlines, s->last_displayed_entry, s->entries);
3766 if (nscrolled < view->nlines) {
3768 struct got_tree_entry *te;
3769 te = s->first_displayed_entry;
3772 te = SIMPLEQ_NEXT(te, entry);
3774 s->selected = ndisplayed - 1;
3779 if (s->selected_entry == NULL) {
3780 struct tog_parent_tree *parent;
3782 /* user selected '..' */
3783 if (s->tree == s->root)
3785 parent = TAILQ_FIRST(&s->parents);
3786 TAILQ_REMOVE(&s->parents, parent,
3788 got_object_tree_close(s->tree);
3789 s->tree = parent->tree;
3791 got_object_tree_get_entries(s->tree);
3792 s->first_displayed_entry =
3793 parent->first_displayed_entry;
3795 parent->selected_entry;
3796 s->selected = parent->selected;
3798 } else if (S_ISDIR(s->selected_entry->mode)) {
3799 struct tog_parent_tree *parent;
3800 struct got_tree_object *child;
3801 err = got_object_open_as_tree(&child,
3802 s->repo, s->selected_entry->id);
3805 parent = calloc(1, sizeof(*parent));
3806 if (parent == NULL) {
3807 err = got_error_prefix_errno("calloc");
3810 parent->tree = s->tree;
3811 parent->first_displayed_entry =
3812 s->first_displayed_entry;
3813 parent->selected_entry = s->selected_entry;
3814 parent->selected = s->selected;
3815 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3818 got_object_tree_get_entries(s->tree);
3820 s->first_displayed_entry = NULL;
3821 } else if (S_ISREG(s->selected_entry->mode)) {
3822 struct tog_view *blame_view;
3823 int begin_x = view_is_parent_view(view) ?
3824 view_split_begin_x(view->begin_x) : 0;
3826 err = blame_tree_entry(&blame_view, begin_x,
3827 s->selected_entry, &s->parents,
3828 s->commit_id, s->refs, s->repo);
3831 if (view_is_parent_view(view)) {
3832 err = view_close_child(view);
3835 err = view_set_child(view, blame_view);
3837 view_close(blame_view);
3840 *focus_view = blame_view;
3841 view->child_focussed = 1;
3843 *new_view = blame_view;
3847 if (s->selected > view->nlines)
3848 s->selected = s->ndisplayed - 1;
3861 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3866 static const struct got_error *
3867 cmd_tree(int argc, char *argv[])
3869 const struct got_error *error;
3870 struct got_repository *repo = NULL;
3871 struct got_reflist_head refs;
3872 char *repo_path = NULL;
3873 struct got_object_id *commit_id = NULL;
3874 char *commit_id_arg = NULL;
3875 struct got_commit_object *commit = NULL;
3876 struct got_tree_object *tree = NULL;
3878 struct tog_view *view;
3880 SIMPLEQ_INIT(&refs);
3883 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3888 while ((ch = getopt(argc, argv, "c:")) != -1) {
3891 commit_id_arg = optarg;
3903 struct got_worktree *worktree;
3904 char *cwd = getcwd(NULL, 0);
3906 return got_error_prefix_errno("getcwd");
3907 error = got_worktree_open(&worktree, cwd);
3908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3913 strdup(got_worktree_get_repo_path(worktree));
3914 got_worktree_close(worktree);
3917 if (repo_path == NULL) {
3918 error = got_error_prefix_errno("strdup");
3921 } else if (argc == 1) {
3922 repo_path = realpath(argv[0], NULL);
3923 if (repo_path == NULL)
3924 return got_error_prefix_errno2("realpath", argv[0]);
3930 error = got_repo_open(&repo, repo_path);
3934 error = apply_unveil(got_repo_get_path(repo), NULL);
3938 if (commit_id_arg == NULL)
3939 error = get_head_commit_id(&commit_id, repo);
3941 error = got_object_resolve_id_str(&commit_id, repo,
3946 error = got_object_open_as_commit(&commit, repo, commit_id);
3950 error = got_object_open_as_tree(&tree, repo,
3951 got_object_commit_get_tree_id(commit));
3955 error = got_ref_list(&refs, repo);
3959 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3961 error = got_error_prefix_errno("view_open");
3964 error = open_tree_view(view, tree, commit_id, &refs, repo);
3967 error = view_loop(view);
3972 got_object_commit_close(commit);
3974 got_object_tree_close(tree);
3976 got_repo_close(repo);
3977 got_ref_list_free(&refs);
3986 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3987 "Available commands:\n", getprogname());
3988 for (i = 0; i < nitems(tog_commands); i++) {
3989 struct tog_cmd *cmd = &tog_commands[i];
3990 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3996 make_argv(const char *arg0, const char *arg1)
3999 int argc = (arg1 == NULL ? 1 : 2);
4001 argv = calloc(argc, sizeof(char *));
4004 argv[0] = strdup(arg0);
4005 if (argv[0] == NULL)
4008 argv[1] = strdup(arg1);
4009 if (argv[1] == NULL)
4017 main(int argc, char *argv[])
4019 const struct got_error *error = NULL;
4020 struct tog_cmd *cmd = NULL;
4022 char **cmd_argv = NULL;
4024 setlocale(LC_CTYPE, "");
4026 while ((ch = getopt(argc, argv, "h")) != -1) {
4045 /* Build an argument vector which runs a default command. */
4046 cmd = &tog_commands[0];
4047 cmd_argv = make_argv(cmd->name, NULL);
4052 /* Did the user specific a command? */
4053 for (i = 0; i < nitems(tog_commands); i++) {
4054 if (strncmp(tog_commands[i].name, argv[0],
4055 strlen(argv[0])) == 0) {
4056 cmd = &tog_commands[i];
4058 tog_commands[i].cmd_usage();
4063 /* Did the user specify a repository? */
4064 char *repo_path = realpath(argv[0], NULL);
4066 struct got_repository *repo;
4067 error = got_repo_open(&repo, repo_path);
4069 got_repo_close(repo);
4071 error = got_error_prefix_errno2("realpath",
4075 fprintf(stderr, "%s: '%s' is not a "
4076 "known command\n", getprogname(),
4080 fprintf(stderr, "%s: '%s' is neither a known "
4081 "command nor a path to a repository\n",
4082 getprogname(), argv[0]);
4086 cmd = &tog_commands[0];
4087 cmd_argv = make_argv(cmd->name, repo_path);
4093 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4100 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);