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 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
114 struct commit_queue_head head;
117 struct tog_diff_view_state {
118 struct got_object_id *id1, *id2;
120 int first_displayed_line;
121 int last_displayed_line;
124 struct got_repository *repo;
125 struct got_reflist_head *refs;
127 /* passed from log view; may be NULL */
128 struct tog_view *log_view;
131 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
133 struct tog_log_thread_args {
134 pthread_cond_t need_commits;
136 struct got_commit_graph *graph;
137 struct commit_queue *commits;
138 const char *in_repo_path;
139 struct got_object_id *start_id;
140 struct got_repository *repo;
143 struct tog_view *view;
144 struct commit_queue_entry **first_displayed_entry;
145 struct commit_queue_entry **selected_entry;
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
155 struct got_repository *repo;
156 struct got_reflist_head *refs;
157 struct got_object_id *start_id;
160 struct tog_log_thread_args thread_args;
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
167 struct tog_view *view;
168 struct got_object_id *commit_id;
172 struct tog_blame_thread_args {
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
182 struct tog_blame_line *lines;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
206 struct tog_parent_tree {
207 TAILQ_ENTRY(tog_parent_tree) entry;
208 struct got_tree_object *tree;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *selected_entry;
214 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 struct tog_tree_view_state {
218 struct got_tree_object *root;
219 struct got_tree_object *tree;
220 const struct got_tree_entries *entries;
221 struct got_tree_entry *first_displayed_entry;
222 struct got_tree_entry *last_displayed_entry;
223 struct got_tree_entry *selected_entry;
224 int ndisplayed, selected, show_ids;
225 struct tog_parent_trees parents;
226 struct got_object_id *commit_id;
227 struct got_repository *repo;
228 struct got_reflist_head *refs;
232 * We implement two types of views: parent views and child views.
234 * The 'Tab' key switches between a parent view and its child view.
235 * Child views are shown side-by-side to their parent view, provided
236 * there is enough screen estate.
238 * When a new view is opened from within a parent view, this new view
239 * becomes a child view of the parent view, replacing any existing child.
241 * When a new view is opened from within a child view, this new view
242 * becomes a parent view which will obscure the views below until the
243 * user quits the new parent view by typing 'q'.
245 * This list of views contains parent views only.
246 * Child views are only pointed to by their parent view.
248 TAILQ_HEAD(tog_view_list_head, tog_view);
251 TAILQ_ENTRY(tog_view) entry;
254 int nlines, ncols, begin_y, begin_x;
255 int lines, cols; /* copies of LINES and COLS */
257 struct tog_view *parent;
258 struct tog_view *child;
261 /* type-specific state */
262 enum tog_view_type type;
264 struct tog_diff_view_state diff;
265 struct tog_log_view_state log;
266 struct tog_blame_view_state blame;
267 struct tog_tree_view_state tree;
270 const struct got_error *(*show)(struct tog_view *);
271 const struct got_error *(*input)(struct tog_view **,
272 struct tog_view **, struct tog_view**, struct tog_view *, int);
273 const struct got_error *(*close)(struct tog_view *);
276 static const struct got_error *open_diff_view(struct tog_view *,
277 struct got_object_id *, struct got_object_id *, struct tog_view *,
278 struct got_reflist_head *, struct got_repository *);
279 static const struct got_error *show_diff_view(struct tog_view *);
280 static const struct got_error *input_diff_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error* close_diff_view(struct tog_view *);
284 static const struct got_error *open_log_view(struct tog_view *,
285 struct got_object_id *, struct got_reflist_head *,
286 struct got_repository *, const char *, int);
287 static const struct got_error * show_log_view(struct tog_view *);
288 static const struct got_error *input_log_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error *close_log_view(struct tog_view *);
292 static const struct got_error *open_blame_view(struct tog_view *, char *,
293 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
294 static const struct got_error *show_blame_view(struct tog_view *);
295 static const struct got_error *input_blame_view(struct tog_view **,
296 struct tog_view **, struct tog_view **, struct tog_view *, int);
297 static const struct got_error *close_blame_view(struct tog_view *);
299 static const struct got_error *open_tree_view(struct tog_view *,
300 struct got_tree_object *, struct got_object_id *,
301 struct got_reflist_head *, struct got_repository *);
302 static const struct got_error *show_tree_view(struct tog_view *);
303 static const struct got_error *input_tree_view(struct tog_view **,
304 struct tog_view **, struct tog_view **, struct tog_view *, int);
305 static const struct got_error *close_tree_view(struct tog_view *);
307 static volatile sig_atomic_t tog_sigwinch_received;
310 tog_sigwinch(int signo)
312 tog_sigwinch_received = 1;
315 static const struct got_error *
316 view_close(struct tog_view *view)
318 const struct got_error *err = NULL;
321 view_close(view->child);
325 err = view->close(view);
327 del_panel(view->panel);
329 delwin(view->window);
334 static struct tog_view *
335 view_open(int nlines, int ncols, int begin_y, int begin_x,
336 enum tog_view_type type)
338 struct tog_view *view = calloc(1, sizeof(*view));
346 view->nlines = nlines ? nlines : LINES - begin_y;
347 view->ncols = ncols ? ncols : COLS - begin_x;
348 view->begin_y = begin_y;
349 view->begin_x = begin_x;
350 view->window = newwin(nlines, ncols, begin_y, begin_x);
351 if (view->window == NULL) {
355 view->panel = new_panel(view->window);
356 if (view->panel == NULL ||
357 set_panel_userptr(view->panel, view) != OK) {
362 keypad(view->window, TRUE);
367 view_split_begin_x(int begin_x)
369 if (begin_x > 0 || COLS < 120)
371 return (COLS - MAX(COLS / 2, 80));
374 static const struct got_error *view_resize(struct tog_view *);
376 static const struct got_error *
377 view_splitscreen(struct tog_view *view)
379 const struct got_error *err = NULL;
382 view->begin_x = view_split_begin_x(0);
383 view->nlines = LINES;
384 view->ncols = COLS - view->begin_x;
387 err = view_resize(view);
391 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
392 return got_error_from_errno("mvwin");
397 static const struct got_error *
398 view_fullscreen(struct tog_view *view)
400 const struct got_error *err = NULL;
404 view->nlines = LINES;
408 err = view_resize(view);
412 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
413 return got_error_from_errno("mvwin");
419 view_is_parent_view(struct tog_view *view)
421 return view->parent == NULL;
424 static const struct got_error *
425 view_resize(struct tog_view *view)
429 if (view->lines > LINES)
430 nlines = view->nlines - (view->lines - LINES);
432 nlines = view->nlines + (LINES - view->lines);
434 if (view->cols > COLS)
435 ncols = view->ncols - (view->cols - COLS);
437 ncols = view->ncols + (COLS - view->cols);
439 if (wresize(view->window, nlines, ncols) == ERR)
440 return got_error_from_errno("wresize");
441 if (replace_panel(view->panel, view->window) == ERR)
442 return got_error_from_errno("replace_panel");
443 wclear(view->window);
445 view->nlines = nlines;
451 view->child->begin_x = view_split_begin_x(view->begin_x);
452 if (view->child->begin_x == 0) {
453 view_fullscreen(view->child);
454 if (view->child->focussed)
455 show_panel(view->child->panel);
457 show_panel(view->panel);
459 view_splitscreen(view->child);
460 show_panel(view->child->panel);
467 static const struct got_error *
468 view_close_child(struct tog_view *view)
470 const struct got_error *err = NULL;
472 if (view->child == NULL)
475 err = view_close(view->child);
480 static const struct got_error *
481 view_set_child(struct tog_view *view, struct tog_view *child)
483 const struct got_error *err = NULL;
486 child->parent = view;
491 view_is_splitscreen(struct tog_view *view)
493 return view->begin_x > 0;
502 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
503 cols = 80; /* Default */
509 resize_term(lines, cols);
512 static const struct got_error *
513 view_input(struct tog_view **new, struct tog_view **dead,
514 struct tog_view **focus, int *done, struct tog_view *view,
515 struct tog_view_list_head *views)
517 const struct got_error *err = NULL;
525 nodelay(stdscr, FALSE);
526 /* Allow threads to make progress while we are waiting for input. */
527 errcode = pthread_mutex_unlock(&tog_mutex);
529 return got_error_set_errno(errcode, "pthread_mutex_unlock");
530 ch = wgetch(view->window);
531 errcode = pthread_mutex_lock(&tog_mutex);
533 return got_error_set_errno(errcode, "pthread_mutex_lock");
534 nodelay(stdscr, TRUE);
536 if (tog_sigwinch_received) {
538 tog_sigwinch_received = 0;
539 TAILQ_FOREACH(v, views, entry) {
540 err = view_resize(v);
543 err = v->input(new, dead, focus, v, KEY_RESIZE);
554 *focus = view->child;
555 view->child_focussed = 1;
556 } else if (view->parent) {
557 *focus = view->parent;
558 view->parent->child_focussed = 0;
562 err = view->input(new, dead, focus, view, ch);
569 if (view_is_parent_view(view)) {
570 if (view->child == NULL)
572 if (view_is_splitscreen(view->child)) {
573 *focus = view->child;
574 view->child_focussed = 1;
575 err = view_fullscreen(view->child);
577 err = view_splitscreen(view->child);
580 err = view->child->input(new, dead, focus,
581 view->child, KEY_RESIZE);
583 if (view_is_splitscreen(view)) {
585 view->parent->child_focussed = 1;
586 err = view_fullscreen(view);
588 err = view_splitscreen(view);
592 err = view->input(new, dead, focus, view,
599 err = view->input(new, dead, focus, view, ch);
607 view_vborder(struct tog_view *view)
610 struct tog_view *view_above;
613 return view_vborder(view->parent);
615 panel = panel_above(view->panel);
619 view_above = panel_userptr(panel);
620 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
621 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
625 view_needs_focus_indication(struct tog_view *view)
627 if (view_is_parent_view(view)) {
628 if (view->child == NULL || view->child_focussed)
630 if (!view_is_splitscreen(view->child))
632 } else if (!view_is_splitscreen(view))
635 return view->focussed;
638 static const struct got_error *
639 view_loop(struct tog_view *view)
641 const struct got_error *err = NULL;
642 struct tog_view_list_head views;
643 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
644 int fast_refresh = 10;
645 int done = 0, errcode;
647 errcode = pthread_mutex_lock(&tog_mutex);
649 return got_error_set_errno(errcode, "pthread_mutex_lock");
652 TAILQ_INSERT_HEAD(&views, view, entry);
656 err = view->show(view);
661 while (!TAILQ_EMPTY(&views) && !done) {
662 /* Refresh fast during initialization, then become slower. */
663 if (fast_refresh && fast_refresh-- == 0)
664 halfdelay(10); /* switch to once per second */
666 err = view_input(&new_view, &dead_view, &focus_view, &done,
671 struct tog_view *prev = NULL;
673 if (view_is_parent_view(dead_view))
674 prev = TAILQ_PREV(dead_view,
675 tog_view_list_head, entry);
676 else if (view->parent != dead_view)
679 if (dead_view->parent)
680 dead_view->parent->child = NULL;
682 TAILQ_REMOVE(&views, dead_view, entry);
684 err = view_close(dead_view);
685 if (err || dead_view == main_view)
688 if (view == dead_view) {
693 else if (!TAILQ_EMPTY(&views))
694 view = TAILQ_LAST(&views,
699 if (view->child && view->child_focussed)
700 focus_view = view->child;
707 struct tog_view *v, *t;
708 /* Only allow one parent view per type. */
709 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
710 if (v->type != new_view->type)
712 TAILQ_REMOVE(&views, v, entry);
718 TAILQ_INSERT_TAIL(&views, new_view, entry);
720 if (focus_view == NULL)
721 focus_view = new_view;
724 show_panel(focus_view->panel);
727 focus_view->focussed = 1;
730 show_panel(new_view->panel);
731 if (view->child && view_is_splitscreen(view->child))
732 show_panel(view->child->panel);
735 if (focus_view == NULL) {
737 show_panel(view->panel);
738 if (view->child && view_is_splitscreen(view->child))
739 show_panel(view->child->panel);
743 err = view->parent->show(view->parent);
747 err = view->show(view);
751 err = view->child->show(view->child);
760 while (!TAILQ_EMPTY(&views)) {
761 view = TAILQ_FIRST(&views);
762 TAILQ_REMOVE(&views, view, entry);
766 errcode = pthread_mutex_unlock(&tog_mutex);
768 return got_error_set_errno(errcode, "pthread_mutex_unlock");
778 "usage: %s log [-c commit] [-r repository-path] [path]\n",
783 /* Create newly allocated wide-character string equivalent to a byte string. */
784 static const struct got_error *
785 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
788 const struct got_error *err = NULL;
791 *wlen = mbstowcs(NULL, s, 0);
792 if (*wlen == (size_t)-1) {
795 return got_error_from_errno("mbstowcs");
797 /* byte string invalid in current encoding; try to "fix" it */
798 err = got_mbsavis(&vis, &vislen, s);
801 *wlen = mbstowcs(NULL, vis, 0);
802 if (*wlen == (size_t)-1) {
803 err = got_error_from_errno("mbstowcs"); /* give up */
808 *ws = calloc(*wlen + 1, sizeof(*ws));
810 err = got_error_from_errno("calloc");
814 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
815 err = got_error_from_errno("mbstowcs");
826 /* Format a line for display, ensuring that it won't overflow a width limit. */
827 static const struct got_error *
828 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
830 const struct got_error *err = NULL;
832 wchar_t *wline = NULL;
839 err = mbs2ws(&wline, &wlen, line);
844 while (i < wlen && cols < wlimit) {
845 int width = wcwidth(wline[i]);
852 if (cols + width <= wlimit)
857 if (wline[i] == L'\t')
858 cols += TABSIZE - ((cols + 1) % TABSIZE);
862 err = got_error_from_errno("wcwidth");
877 static const struct got_error*
878 build_refs_str(char **refs_str, struct got_reflist_head *refs,
879 struct got_object_id *id)
881 static const struct got_error *err = NULL;
882 struct got_reflist_entry *re;
888 SIMPLEQ_FOREACH(re, refs, entry) {
889 if (got_object_id_cmp(re->id, id) != 0)
891 name = got_ref_get_name(re->ref);
892 if (strcmp(name, GOT_REF_HEAD) == 0)
894 if (strncmp(name, "refs/", 5) == 0)
896 if (strncmp(name, "got/", 4) == 0)
898 if (strncmp(name, "heads/", 6) == 0)
900 if (strncmp(name, "remotes/", 8) == 0)
903 if (asprintf(refs_str, "%s%s%s", s ? s : "",
904 s ? ", " : "", name) == -1) {
905 err = got_error_from_errno("asprintf");
916 static const struct got_error *
917 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
919 char *smallerthan, *at;
921 smallerthan = strchr(author, '<');
922 if (smallerthan && smallerthan[1] != '\0')
923 author = smallerthan + 1;
924 at = strchr(author, '@');
927 return format_line(wauthor, author_width, author, limit);
930 static const struct got_error *
931 draw_commit(struct tog_view *view, struct got_commit_object *commit,
932 struct got_object_id *id, struct got_reflist_head *refs,
933 int author_display_cols)
935 const struct got_error *err = NULL;
936 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
937 char *logmsg0 = NULL, *logmsg = NULL;
939 wchar_t *wlogmsg = NULL, *wauthor = NULL;
940 int author_width, logmsg_width;
941 char *newline, *line = NULL;
943 static const size_t date_display_cols = 9;
944 const int avail = view->ncols;
946 time_t committer_time;
948 committer_time = got_object_commit_get_committer_time(commit);
949 if (localtime_r(&committer_time, &tm) == NULL)
950 return got_error_from_errno("localtime_r");
951 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
953 return got_error(GOT_ERR_NO_SPACE);
955 if (avail < date_display_cols)
956 limit = MIN(sizeof(datebuf) - 1, avail);
958 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
959 waddnstr(view->window, datebuf, limit);
964 author = strdup(got_object_commit_get_author(commit));
965 if (author == NULL) {
966 err = got_error_from_errno("strdup");
969 err = format_author(&wauthor, &author_width, author, avail - col);
972 waddwstr(view->window, wauthor);
974 while (col <= avail && author_width < author_display_cols + 2) {
975 waddch(view->window, ' ');
982 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
983 if (logmsg0 == NULL) {
984 err = got_error_from_errno("strdup");
988 while (*logmsg == '\n')
990 newline = strchr(logmsg, '\n');
994 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
997 waddwstr(view->window, wlogmsg);
999 while (col <= avail) {
1000 waddch(view->window, ' ');
1012 static struct commit_queue_entry *
1013 alloc_commit_queue_entry(struct got_commit_object *commit,
1014 struct got_object_id *id)
1016 struct commit_queue_entry *entry;
1018 entry = calloc(1, sizeof(*entry));
1023 entry->commit = commit;
1028 pop_commit(struct commit_queue *commits)
1030 struct commit_queue_entry *entry;
1032 entry = TAILQ_FIRST(&commits->head);
1033 TAILQ_REMOVE(&commits->head, entry, entry);
1034 got_object_commit_close(entry->commit);
1035 commits->ncommits--;
1036 /* Don't free entry->id! It is owned by the commit graph. */
1041 free_commits(struct commit_queue *commits)
1043 while (!TAILQ_EMPTY(&commits->head))
1044 pop_commit(commits);
1047 static const struct got_error *
1048 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1049 int minqueue, struct got_repository *repo, const char *path)
1051 const struct got_error *err = NULL;
1055 * We keep all commits open throughout the lifetime of the log
1056 * view in order to avoid having to re-fetch commits from disk
1057 * while updating the display.
1059 while (nqueued < minqueue) {
1060 struct got_object_id *id;
1061 struct got_commit_object *commit;
1062 struct commit_queue_entry *entry;
1065 err = got_commit_graph_iter_next(&id, graph);
1067 if (err->code != GOT_ERR_ITER_NEED_MORE)
1069 err = got_commit_graph_fetch_commits(graph,
1079 err = got_object_open_as_commit(&commit, repo, id);
1082 entry = alloc_commit_queue_entry(commit, id);
1083 if (entry == NULL) {
1084 err = got_error_from_errno("alloc_commit_queue_entry");
1088 errcode = pthread_mutex_lock(&tog_mutex);
1090 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1094 entry->idx = commits->ncommits;
1095 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1097 commits->ncommits++;
1099 errcode = pthread_mutex_unlock(&tog_mutex);
1100 if (errcode && err == NULL)
1101 err = got_error_set_errno(errcode,
1102 "pthread_mutex_unlock");
1108 static const struct got_error *
1109 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_reference *head_ref;
1117 err = got_ref_open(&head_ref, repo, branch_name, 0);
1121 err = got_ref_resolve(head_id, repo, head_ref);
1122 got_ref_close(head_ref);
1131 static const struct got_error *
1132 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1133 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1134 struct commit_queue *commits, int selected_idx, int limit,
1135 struct got_reflist_head *refs, const char *path, int commits_needed)
1137 const struct got_error *err = NULL;
1138 struct commit_queue_entry *entry;
1139 int ncommits, width;
1140 int author_cols = 10;
1141 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1142 char *refs_str = NULL;
1148 if (ncommits == selected_idx) {
1152 entry = TAILQ_NEXT(entry, entry);
1157 err = got_object_id_str(&id_str, (*selected)->id);
1161 err = build_refs_str(&refs_str, refs, (*selected)->id);
1167 if (commits_needed == 0)
1168 halfdelay(10); /* disable fast refresh */
1170 if (asprintf(&ncommits_str, " [%d/%d] %s",
1171 entry ? entry->idx + 1 : 0, commits->ncommits,
1172 commits_needed > 0 ? "loading... " :
1173 (refs_str ? refs_str : "")) == -1) {
1174 err = got_error_from_errno("asprintf");
1178 if (path && strcmp(path, "/") != 0) {
1179 if (asprintf(&header, "commit %s %s%s",
1180 id_str ? id_str : "........................................",
1181 path, ncommits_str) == -1) {
1182 err = got_error_from_errno("asprintf");
1186 } else if (asprintf(&header, "commit %s%s",
1187 id_str ? id_str : "........................................",
1188 ncommits_str) == -1) {
1189 err = got_error_from_errno("asprintf");
1193 err = format_line(&wline, &width, header, view->ncols);
1197 werase(view->window);
1199 if (view_needs_focus_indication(view))
1200 wstandout(view->window);
1201 waddwstr(view->window, wline);
1202 while (width < view->ncols) {
1203 waddch(view->window, ' ');
1206 if (view_needs_focus_indication(view))
1207 wstandend(view->window);
1212 /* Grow author column size if necessary. */
1219 if (ncommits >= limit - 1)
1221 author = strdup(got_object_commit_get_author(entry->commit));
1222 if (author == NULL) {
1223 err = got_error_from_errno("strdup");
1226 err = format_author(&wauthor, &width, author, COLS);
1227 if (author_cols < width)
1228 author_cols = width;
1231 entry = TAILQ_NEXT(entry, entry);
1238 if (ncommits >= limit - 1)
1240 if (ncommits == selected_idx)
1241 wstandout(view->window);
1242 err = draw_commit(view, entry->commit, entry->id, refs,
1244 if (ncommits == selected_idx)
1245 wstandend(view->window);
1250 entry = TAILQ_NEXT(entry, entry);
1263 scroll_up(struct tog_view *view,
1264 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1265 struct commit_queue *commits)
1267 struct commit_queue_entry *entry;
1270 entry = TAILQ_FIRST(&commits->head);
1271 if (*first_displayed_entry == entry)
1274 entry = *first_displayed_entry;
1275 while (entry && nscrolled < maxscroll) {
1276 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1278 *first_displayed_entry = entry;
1284 static const struct got_error *
1285 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1286 pthread_cond_t *need_commits)
1291 halfdelay(1); /* fast refresh while loading commits */
1293 while (*commits_needed > 0) {
1297 /* Wake the log thread. */
1298 errcode = pthread_cond_signal(need_commits);
1300 return got_error_set_errno(errcode,
1301 "pthread_cond_signal");
1302 errcode = pthread_mutex_unlock(&tog_mutex);
1304 return got_error_set_errno(errcode,
1305 "pthread_mutex_unlock");
1307 errcode = pthread_mutex_lock(&tog_mutex);
1309 return got_error_set_errno(errcode,
1310 "pthread_mutex_lock");
1312 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1314 * Thread is not done yet; lose a key press
1315 * and let the user retry... this way the GUI
1316 * remains interactive while logging deep paths
1317 * with few commits in history.
1326 static const struct got_error *
1327 scroll_down(struct tog_view *view,
1328 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1329 struct commit_queue_entry **last_displayed_entry,
1330 struct commit_queue *commits, int *log_complete, int *commits_needed,
1331 pthread_cond_t *need_commits)
1333 const struct got_error *err = NULL;
1334 struct commit_queue_entry *pentry;
1337 if (*last_displayed_entry == NULL)
1340 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1341 if (pentry == NULL && !*log_complete) {
1343 * Ask the log thread for required amount of commits
1344 * plus some amount of pre-fetching.
1346 (*commits_needed) += maxscroll + 20;
1347 err = trigger_log_thread(0, commits_needed, log_complete,
1354 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1358 *last_displayed_entry = pentry;
1360 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1363 *first_displayed_entry = pentry;
1364 } while (++nscrolled < maxscroll);
1369 static const struct got_error *
1370 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1371 struct got_commit_object *commit, struct got_object_id *commit_id,
1372 struct tog_view *log_view, struct got_reflist_head *refs,
1373 struct got_repository *repo)
1375 const struct got_error *err;
1376 struct got_object_qid *parent_id;
1377 struct tog_view *diff_view;
1379 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1380 if (diff_view == NULL)
1381 return got_error_from_errno("view_open");
1383 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1384 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1385 commit_id, log_view, refs, repo);
1387 *new_view = diff_view;
1391 static const struct got_error *
1392 tree_view_visit_subtree(struct got_tree_object *subtree,
1393 struct tog_tree_view_state *s)
1395 struct tog_parent_tree *parent;
1397 parent = calloc(1, sizeof(*parent));
1399 return got_error_from_errno("calloc");
1401 parent->tree = s->tree;
1402 parent->first_displayed_entry = s->first_displayed_entry;
1403 parent->selected_entry = s->selected_entry;
1404 parent->selected = s->selected;
1405 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1407 s->entries = got_object_tree_get_entries(s->tree);
1409 s->first_displayed_entry = NULL;
1414 static const struct got_error *
1415 browse_commit_tree(struct tog_view **new_view, int begin_x,
1416 struct commit_queue_entry *entry, const char *path,
1417 struct got_reflist_head *refs, struct got_repository *repo)
1419 const struct got_error *err = NULL;
1420 struct got_tree_object *tree;
1421 struct got_tree_entry *te;
1422 struct tog_tree_view_state *s;
1423 struct tog_view *tree_view;
1424 char *slash, *subpath = NULL;
1427 err = got_object_open_as_tree(&tree, repo,
1428 got_object_commit_get_tree_id(entry->commit));
1432 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1433 if (tree_view == NULL)
1434 return got_error_from_errno("view_open");
1436 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1438 got_object_tree_close(tree);
1441 s = &tree_view->state.tree;
1443 *new_view = tree_view;
1445 /* Walk the path and open corresponding tree objects. */
1448 struct got_object_id *tree_id;
1453 /* Ensure the correct subtree entry is selected. */
1454 slash = strchr(p, '/');
1456 slash = strchr(p, '\0');
1457 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1458 if (strncmp(p, te->name, slash - p) == 0) {
1459 s->selected_entry = te;
1464 if (s->selected_entry == NULL) {
1465 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1468 if (s->tree != s->root)
1469 s->selected++; /* skip '..' */
1471 if (!S_ISDIR(s->selected_entry->mode))
1474 slash = strchr(p, '/');
1476 subpath = strndup(path, slash - path);
1478 subpath = strdup(path);
1479 if (subpath == NULL) {
1480 err = got_error_from_errno("strdup");
1484 err = got_object_id_by_path(&tree_id, repo, entry->id,
1489 err = got_object_open_as_tree(&tree, repo, tree_id);
1494 err = tree_view_visit_subtree(tree, s);
1496 got_object_tree_close(tree);
1511 log_thread(void *arg)
1513 const struct got_error *err = NULL;
1515 struct tog_log_thread_args *a = arg;
1518 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1522 while (!done && !err) {
1523 err = queue_commits(a->graph, a->commits, 1, a->repo,
1526 if (err->code != GOT_ERR_ITER_COMPLETED)
1530 } else if (a->commits_needed > 0)
1531 a->commits_needed--;
1533 errcode = pthread_mutex_lock(&tog_mutex);
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1538 } else if (*a->quit)
1540 else if (*a->first_displayed_entry == NULL) {
1541 *a->first_displayed_entry =
1542 TAILQ_FIRST(&a->commits->head);
1543 *a->selected_entry = *a->first_displayed_entry;
1547 a->commits_needed = 0;
1548 else if (a->commits_needed == 0) {
1549 errcode = pthread_cond_wait(&a->need_commits,
1552 err = got_error_set_errno(errcode,
1553 "pthread_cond_wait");
1556 errcode = pthread_mutex_unlock(&tog_mutex);
1557 if (errcode && err == NULL)
1558 err = got_error_set_errno(errcode,
1559 "pthread_mutex_unlock");
1561 a->log_complete = 1;
1565 static const struct got_error *
1566 stop_log_thread(struct tog_log_view_state *s)
1568 const struct got_error *err = NULL;
1573 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1575 return got_error_set_errno(errcode,
1576 "pthread_cond_signal");
1577 errcode = pthread_mutex_unlock(&tog_mutex);
1579 return got_error_set_errno(errcode,
1580 "pthread_mutex_unlock");
1581 errcode = pthread_join(s->thread, (void **)&err);
1583 return got_error_set_errno(errcode, "pthread_join");
1584 errcode = pthread_mutex_lock(&tog_mutex);
1586 return got_error_set_errno(errcode,
1587 "pthread_mutex_lock");
1591 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1592 if (errcode && err == NULL)
1593 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1595 if (s->thread_args.repo) {
1596 got_repo_close(s->thread_args.repo);
1597 s->thread_args.repo = NULL;
1600 if (s->thread_args.graph) {
1601 got_commit_graph_close(s->thread_args.graph);
1602 s->thread_args.graph = NULL;
1608 static const struct got_error *
1609 close_log_view(struct tog_view *view)
1611 const struct got_error *err = NULL;
1612 struct tog_log_view_state *s = &view->state.log;
1614 err = stop_log_thread(s);
1615 free_commits(&s->commits);
1616 free(s->in_repo_path);
1617 s->in_repo_path = NULL;
1623 static const struct got_error *
1624 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1625 struct got_reflist_head *refs, struct got_repository *repo,
1626 const char *path, int check_disk)
1628 const struct got_error *err = NULL;
1629 struct tog_log_view_state *s = &view->state.log;
1630 struct got_repository *thread_repo = NULL;
1631 struct got_commit_graph *thread_graph = NULL;
1634 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1638 /* The commit queue only contains commits being displayed. */
1639 TAILQ_INIT(&s->commits.head);
1640 s->commits.ncommits = 0;
1644 s->start_id = got_object_id_dup(start_id);
1645 if (s->start_id == NULL) {
1646 err = got_error_from_errno("got_object_id_dup");
1650 view->show = show_log_view;
1651 view->input = input_log_view;
1652 view->close = close_log_view;
1654 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1657 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1662 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1664 err = got_error_set_errno(errcode, "pthread_cond_init");
1668 s->thread_args.commits_needed = view->nlines;
1669 s->thread_args.graph = thread_graph;
1670 s->thread_args.commits = &s->commits;
1671 s->thread_args.in_repo_path = s->in_repo_path;
1672 s->thread_args.start_id = s->start_id;
1673 s->thread_args.repo = thread_repo;
1674 s->thread_args.log_complete = 0;
1675 s->thread_args.quit = &s->quit;
1676 s->thread_args.view = view;
1677 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1678 s->thread_args.selected_entry = &s->selected_entry;
1681 close_log_view(view);
1685 static const struct got_error *
1686 show_log_view(struct tog_view *view)
1688 struct tog_log_view_state *s = &view->state.log;
1690 if (s->thread == NULL) {
1691 int errcode = pthread_create(&s->thread, NULL, log_thread,
1694 return got_error_set_errno(errcode, "pthread_create");
1697 return draw_commits(view, &s->last_displayed_entry,
1698 &s->selected_entry, s->first_displayed_entry,
1699 &s->commits, s->selected, view->nlines, s->refs,
1700 s->in_repo_path, s->thread_args.commits_needed);
1703 static const struct got_error *
1704 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1705 struct tog_view **focus_view, struct tog_view *view, int ch)
1707 const struct got_error *err = NULL;
1708 struct tog_log_view_state *s = &view->state.log;
1710 struct tog_view *diff_view = NULL, *tree_view = NULL;
1721 if (s->first_displayed_entry == NULL)
1723 if (s->selected > 0)
1725 if (s->selected > 0)
1727 scroll_up(view, &s->first_displayed_entry, 1,
1732 if (s->first_displayed_entry == NULL)
1734 if (TAILQ_FIRST(&s->commits.head) ==
1735 s->first_displayed_entry) {
1739 scroll_up(view, &s->first_displayed_entry,
1740 view->nlines, &s->commits);
1746 if (s->first_displayed_entry == NULL)
1748 if (s->selected < MIN(view->nlines - 2,
1749 s->commits.ncommits - 1)) {
1753 err = scroll_down(view, &s->first_displayed_entry, 1,
1754 &s->last_displayed_entry, &s->commits,
1755 &s->thread_args.log_complete,
1756 &s->thread_args.commits_needed,
1757 &s->thread_args.need_commits);
1761 struct commit_queue_entry *first;
1762 first = s->first_displayed_entry;
1765 err = scroll_down(view, &s->first_displayed_entry,
1766 view->nlines, &s->last_displayed_entry,
1767 &s->commits, &s->thread_args.log_complete,
1768 &s->thread_args.commits_needed,
1769 &s->thread_args.need_commits);
1770 if (first == s->first_displayed_entry &&
1771 s->selected < MIN(view->nlines - 2,
1772 s->commits.ncommits - 1)) {
1773 /* can't scroll further down */
1774 s->selected = MIN(view->nlines - 2,
1775 s->commits.ncommits - 1);
1781 if (s->selected > view->nlines - 2)
1782 s->selected = view->nlines - 2;
1783 if (s->selected > s->commits.ncommits - 1)
1784 s->selected = s->commits.ncommits - 1;
1789 if (s->selected_entry == NULL)
1791 if (view_is_parent_view(view))
1792 begin_x = view_split_begin_x(view->begin_x);
1793 err = open_diff_view_for_commit(&diff_view, begin_x,
1794 s->selected_entry->commit, s->selected_entry->id,
1795 view, s->refs, s->repo);
1798 if (view_is_parent_view(view)) {
1799 err = view_close_child(view);
1802 err = view_set_child(view, diff_view);
1804 view_close(diff_view);
1807 *focus_view = diff_view;
1808 view->child_focussed = 1;
1810 *new_view = diff_view;
1813 if (s->selected_entry == NULL)
1815 if (view_is_parent_view(view))
1816 begin_x = view_split_begin_x(view->begin_x);
1817 err = browse_commit_tree(&tree_view, begin_x,
1818 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1821 if (view_is_parent_view(view)) {
1822 err = view_close_child(view);
1825 err = view_set_child(view, tree_view);
1827 view_close(tree_view);
1830 *focus_view = tree_view;
1831 view->child_focussed = 1;
1833 *new_view = tree_view;
1836 if (strcmp(s->in_repo_path, "/") == 0)
1838 parent_path = dirname(s->in_repo_path);
1839 if (parent_path && strcmp(parent_path, ".") != 0) {
1840 struct tog_view *lv;
1841 err = stop_log_thread(s);
1844 lv = view_open(view->nlines, view->ncols,
1845 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1847 return got_error_from_errno(
1849 err = open_log_view(lv, s->start_id, s->refs,
1850 s->repo, parent_path, 0);
1853 if (view_is_parent_view(view))
1856 view_set_child(view->parent, lv);
1869 static const struct got_error *
1870 apply_unveil(const char *repo_path, const char *worktree_path)
1872 const struct got_error *error;
1874 if (repo_path && unveil(repo_path, "r") != 0)
1875 return got_error_from_errno2("unveil", repo_path);
1877 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1878 return got_error_from_errno2("unveil", worktree_path);
1880 if (unveil("/tmp", "rwc") != 0)
1881 return got_error_from_errno2("unveil", "/tmp");
1883 error = got_privsep_unveil_exec_helpers();
1887 if (unveil(NULL, NULL) != 0)
1888 return got_error_from_errno("unveil");
1898 halfdelay(1); /* Do fast refresh while initial view is loading. */
1901 intrflush(stdscr, FALSE);
1902 keypad(stdscr, TRUE);
1904 signal(SIGWINCH, tog_sigwinch);
1907 static const struct got_error *
1908 cmd_log(int argc, char *argv[])
1910 const struct got_error *error;
1911 struct got_repository *repo = NULL;
1912 struct got_worktree *worktree = NULL;
1913 struct got_reflist_head refs;
1914 struct got_object_id *start_id = NULL;
1915 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1916 char *start_commit = NULL;
1918 struct tog_view *view;
1920 SIMPLEQ_INIT(&refs);
1923 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1928 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1931 start_commit = optarg;
1934 repo_path = realpath(optarg, NULL);
1935 if (repo_path == NULL)
1936 err(1, "-r option");
1947 cwd = getcwd(NULL, 0);
1949 error = got_error_from_errno("getcwd");
1952 error = got_worktree_open(&worktree, cwd);
1953 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1960 error = got_error_from_errno("strdup");
1963 } else if (argc == 1) {
1965 error = got_worktree_resolve_path(&path, worktree,
1970 path = strdup(argv[0]);
1972 error = got_error_from_errno("strdup");
1979 repo_path = worktree ?
1980 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1981 if (repo_path == NULL) {
1982 error = got_error_from_errno("strdup");
1988 error = got_repo_open(&repo, repo_path);
1992 error = apply_unveil(got_repo_get_path(repo),
1993 worktree ? got_worktree_get_root_path(worktree) : NULL);
1997 if (start_commit == NULL)
1998 error = get_head_commit_id(&start_id, worktree ?
1999 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2002 error = got_object_resolve_id_str(&start_id, repo,
2007 error = got_ref_list(&refs, repo);
2011 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2013 error = got_error_from_errno("view_open");
2016 error = open_log_view(view, start_id, &refs, repo, path, 1);
2019 error = view_loop(view);
2026 got_repo_close(repo);
2028 got_worktree_close(worktree);
2029 got_ref_list_free(&refs);
2037 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2043 parse_next_line(FILE *f, size_t *len)
2048 const char delim[3] = { '\0', '\0', '\0'};
2050 line = fparseln(f, &linelen, &lineno, delim, 0);
2056 static const struct got_error *
2057 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2058 int *last_displayed_line, int *eof, int max_lines,
2061 const struct got_error *err;
2062 int nlines = 0, nprinted = 0;
2069 werase(view->window);
2072 err = format_line(&wline, &width, header, view->ncols);
2077 if (view_needs_focus_indication(view))
2078 wstandout(view->window);
2079 waddwstr(view->window, wline);
2080 if (view_needs_focus_indication(view))
2081 wstandend(view->window);
2082 if (width < view->ncols)
2083 waddch(view->window, '\n');
2091 while (nprinted < max_lines) {
2092 line = parse_next_line(f, &len);
2097 if (++nlines < *first_displayed_line) {
2102 err = format_line(&wline, &width, line, view->ncols);
2107 waddwstr(view->window, wline);
2108 if (width < view->ncols)
2109 waddch(view->window, '\n');
2110 if (++nprinted == 1)
2111 *first_displayed_line = nlines;
2116 *last_displayed_line = nlines;
2121 while (nprinted < view->nlines) {
2122 waddch(view->window, '\n');
2126 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2131 wstandout(view->window);
2132 waddwstr(view->window, wline);
2133 wstandend(view->window);
2140 get_datestr(time_t *time, char *datebuf)
2142 char *p, *s = ctime_r(time, datebuf);
2143 p = strchr(s, '\n');
2149 static const struct got_error *
2150 write_commit_info(struct got_object_id *commit_id,
2151 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2153 const struct got_error *err = NULL;
2155 struct got_commit_object *commit;
2156 char *id_str = NULL;
2157 time_t committer_time;
2158 const char *author, *committer;
2159 char *refs_str = NULL;
2162 err = build_refs_str(&refs_str, refs, commit_id);
2167 err = got_object_open_as_commit(&commit, repo, commit_id);
2171 err = got_object_id_str(&id_str, commit_id);
2173 err = got_error_from_errno("got_object_id_str");
2177 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2178 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2179 err = got_error_from_errno("fprintf");
2182 if (fprintf(outfile, "from: %s\n",
2183 got_object_commit_get_author(commit)) < 0) {
2184 err = got_error_from_errno("fprintf");
2187 committer_time = got_object_commit_get_committer_time(commit);
2188 if (fprintf(outfile, "date: %s UTC\n",
2189 get_datestr(&committer_time, datebuf)) < 0) {
2190 err = got_error_from_errno("fprintf");
2193 author = got_object_commit_get_author(commit);
2194 committer = got_object_commit_get_committer(commit);
2195 if (strcmp(author, committer) != 0 &&
2196 fprintf(outfile, "via: %s\n", committer) < 0) {
2197 err = got_error_from_errno("fprintf");
2200 if (fprintf(outfile, "%s\n",
2201 got_object_commit_get_logmsg(commit)) < 0) {
2202 err = got_error_from_errno("fprintf");
2208 got_object_commit_close(commit);
2212 static const struct got_error *
2213 create_diff(struct tog_diff_view_state *s)
2215 const struct got_error *err = NULL;
2221 err = got_error_from_errno("got_opentemp");
2224 if (s->f && fclose(s->f) != 0) {
2225 err = got_error_from_errno("fclose");
2231 err = got_object_get_type(&obj_type, s->repo, s->id1);
2233 err = got_object_get_type(&obj_type, s->repo, s->id2);
2238 case GOT_OBJ_TYPE_BLOB:
2239 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2240 s->diff_context, s->repo, f);
2242 case GOT_OBJ_TYPE_TREE:
2243 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2244 s->diff_context, s->repo, f);
2246 case GOT_OBJ_TYPE_COMMIT: {
2247 const struct got_object_id_queue *parent_ids;
2248 struct got_object_qid *pid;
2249 struct got_commit_object *commit2;
2251 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2254 /* Show commit info if we're diffing to a parent/root commit. */
2256 write_commit_info(s->id2, s->refs, s->repo, f);
2258 parent_ids = got_object_commit_get_parent_ids(commit2);
2259 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2260 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2261 write_commit_info(s->id2, s->refs,
2267 got_object_commit_close(commit2);
2269 err = got_diff_objects_as_commits(s->id1, s->id2,
2270 s->diff_context, s->repo, f);
2274 err = got_error(GOT_ERR_OBJ_TYPE);
2278 if (f && fflush(f) != 0 && err == NULL)
2279 err = got_error_from_errno("fflush");
2284 diff_view_indicate_progress(struct tog_view *view)
2286 mvwaddstr(view->window, 0, 0, "diffing...");
2291 static const struct got_error *
2292 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2293 struct got_object_id *id2, struct tog_view *log_view,
2294 struct got_reflist_head *refs, struct got_repository *repo)
2296 const struct got_error *err;
2298 if (id1 != NULL && id2 != NULL) {
2300 err = got_object_get_type(&type1, repo, id1);
2303 err = got_object_get_type(&type2, repo, id2);
2308 return got_error(GOT_ERR_OBJ_TYPE);
2312 view->state.diff.id1 = got_object_id_dup(id1);
2313 if (view->state.diff.id1 == NULL)
2314 return got_error_from_errno("got_object_id_dup");
2316 view->state.diff.id1 = NULL;
2318 view->state.diff.id2 = got_object_id_dup(id2);
2319 if (view->state.diff.id2 == NULL) {
2320 free(view->state.diff.id1);
2321 view->state.diff.id1 = NULL;
2322 return got_error_from_errno("got_object_id_dup");
2324 view->state.diff.f = NULL;
2325 view->state.diff.first_displayed_line = 1;
2326 view->state.diff.last_displayed_line = view->nlines;
2327 view->state.diff.diff_context = 3;
2328 view->state.diff.log_view = log_view;
2329 view->state.diff.repo = repo;
2330 view->state.diff.refs = refs;
2332 if (log_view && view_is_splitscreen(view))
2333 show_log_view(log_view); /* draw vborder */
2334 diff_view_indicate_progress(view);
2336 err = create_diff(&view->state.diff);
2338 free(view->state.diff.id1);
2339 view->state.diff.id1 = NULL;
2340 free(view->state.diff.id2);
2341 view->state.diff.id2 = NULL;
2345 view->show = show_diff_view;
2346 view->input = input_diff_view;
2347 view->close = close_diff_view;
2352 static const struct got_error *
2353 close_diff_view(struct tog_view *view)
2355 const struct got_error *err = NULL;
2357 free(view->state.diff.id1);
2358 view->state.diff.id1 = NULL;
2359 free(view->state.diff.id2);
2360 view->state.diff.id2 = NULL;
2361 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2362 err = got_error_from_errno("fclose");
2366 static const struct got_error *
2367 show_diff_view(struct tog_view *view)
2369 const struct got_error *err;
2370 struct tog_diff_view_state *s = &view->state.diff;
2371 char *id_str1 = NULL, *id_str2, *header;
2374 err = got_object_id_str(&id_str1, s->id1);
2378 err = got_object_id_str(&id_str2, s->id2);
2382 if (asprintf(&header, "diff %s %s",
2383 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2384 err = got_error_from_errno("asprintf");
2392 return draw_file(view, s->f, &s->first_displayed_line,
2393 &s->last_displayed_line, &s->eof, view->nlines,
2397 static const struct got_error *
2398 set_selected_commit(struct tog_diff_view_state *s,
2399 struct commit_queue_entry *entry)
2401 const struct got_error *err;
2402 const struct got_object_id_queue *parent_ids;
2403 struct got_commit_object *selected_commit;
2404 struct got_object_qid *pid;
2407 s->id2 = got_object_id_dup(entry->id);
2409 return got_error_from_errno("got_object_id_dup");
2411 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2414 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2416 pid = SIMPLEQ_FIRST(parent_ids);
2417 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2418 got_object_commit_close(selected_commit);
2422 static const struct got_error *
2423 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2424 struct tog_view **focus_view, struct tog_view *view, int ch)
2426 const struct got_error *err = NULL;
2427 struct tog_diff_view_state *s = &view->state.diff;
2428 struct tog_log_view_state *ls;
2429 struct commit_queue_entry *entry;
2435 if (s->first_displayed_line > 1)
2436 s->first_displayed_line--;
2440 if (s->first_displayed_line == 1)
2443 while (i++ < view->nlines - 1 &&
2444 s->first_displayed_line > 1)
2445 s->first_displayed_line--;
2450 s->first_displayed_line++;
2458 while (!s->eof && i++ < view->nlines - 1) {
2460 line = parse_next_line(s->f, NULL);
2461 s->first_displayed_line++;
2467 if (s->diff_context > 0) {
2469 diff_view_indicate_progress(view);
2470 err = create_diff(s);
2474 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2476 diff_view_indicate_progress(view);
2477 err = create_diff(s);
2482 if (s->log_view == NULL)
2484 ls = &s->log_view->state.log;
2485 entry = TAILQ_PREV(ls->selected_entry,
2486 commit_queue_head, entry);
2490 err = input_log_view(NULL, NULL, NULL, s->log_view,
2495 err = set_selected_commit(s, entry);
2499 s->first_displayed_line = 1;
2500 s->last_displayed_line = view->nlines;
2502 diff_view_indicate_progress(view);
2503 err = create_diff(s);
2507 if (s->log_view == NULL)
2509 ls = &s->log_view->state.log;
2511 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2512 ls->thread_args.commits_needed++;
2514 /* Display "loading..." in log view. */
2515 show_log_view(s->log_view);
2519 err = trigger_log_thread(1 /* load_all */,
2520 &ls->thread_args.commits_needed,
2521 &ls->thread_args.log_complete,
2522 &ls->thread_args.need_commits);
2526 err = input_log_view(NULL, NULL, NULL, s->log_view,
2531 entry = TAILQ_NEXT(ls->selected_entry, entry);
2535 err = set_selected_commit(s, entry);
2539 s->first_displayed_line = 1;
2540 s->last_displayed_line = view->nlines;
2542 diff_view_indicate_progress(view);
2543 err = create_diff(s);
2552 static const struct got_error *
2553 cmd_diff(int argc, char *argv[])
2555 const struct got_error *error = NULL;
2556 struct got_repository *repo = NULL;
2557 struct got_reflist_head refs;
2558 struct got_object_id *id1 = NULL, *id2 = NULL;
2559 char *repo_path = NULL;
2560 char *id_str1 = NULL, *id_str2 = NULL;
2562 struct tog_view *view;
2564 SIMPLEQ_INIT(&refs);
2567 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2572 while ((ch = getopt(argc, argv, "")) != -1) {
2584 usage_diff(); /* TODO show local worktree changes */
2585 } else if (argc == 2) {
2586 repo_path = getcwd(NULL, 0);
2587 if (repo_path == NULL)
2588 return got_error_from_errno("getcwd");
2591 } else if (argc == 3) {
2592 repo_path = realpath(argv[0], NULL);
2593 if (repo_path == NULL)
2594 return got_error_from_errno2("realpath", argv[0]);
2602 error = got_repo_open(&repo, repo_path);
2606 error = apply_unveil(got_repo_get_path(repo), NULL);
2610 error = got_object_resolve_id_str(&id1, repo, id_str1);
2614 error = got_object_resolve_id_str(&id2, repo, id_str2);
2618 error = got_ref_list(&refs, repo);
2622 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2624 error = got_error_from_errno("view_open");
2627 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2630 error = view_loop(view);
2633 got_repo_close(repo);
2634 got_ref_list_free(&refs);
2642 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2647 struct tog_blame_line {
2649 struct got_object_id *id;
2652 static const struct got_error *
2653 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2654 const char *path, struct tog_blame_line *lines, int nlines,
2655 int blame_complete, int selected_line, int *first_displayed_line,
2656 int *last_displayed_line, int *eof, int max_lines)
2658 const struct got_error *err;
2659 int lineno = 0, nprinted = 0;
2664 struct tog_blame_line *blame_line;
2665 struct got_object_id *prev_id = NULL;
2668 err = got_object_id_str(&id_str, id);
2673 werase(view->window);
2675 if (asprintf(&line, "commit %s", id_str) == -1) {
2676 err = got_error_from_errno("asprintf");
2681 err = format_line(&wline, &width, line, view->ncols);
2684 if (view_needs_focus_indication(view))
2685 wstandout(view->window);
2686 waddwstr(view->window, wline);
2687 if (view_needs_focus_indication(view))
2688 wstandend(view->window);
2691 if (width < view->ncols)
2692 waddch(view->window, '\n');
2694 if (asprintf(&line, "[%d/%d] %s%s",
2695 *first_displayed_line - 1 + selected_line, nlines,
2696 blame_complete ? "" : "annotating... ", path) == -1) {
2698 return got_error_from_errno("asprintf");
2701 err = format_line(&wline, &width, line, view->ncols);
2706 waddwstr(view->window, wline);
2709 if (width < view->ncols)
2710 waddch(view->window, '\n');
2713 while (nprinted < max_lines - 2) {
2714 line = parse_next_line(f, &len);
2719 if (++lineno < *first_displayed_line) {
2724 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2725 err = format_line(&wline, &width, line, wlimit);
2731 if (view->focussed && nprinted == selected_line - 1)
2732 wstandout(view->window);
2734 blame_line = &lines[lineno - 1];
2735 if (blame_line->annotated && prev_id &&
2736 got_object_id_cmp(prev_id, blame_line->id) == 0)
2737 waddstr(view->window, " ");
2738 else if (blame_line->annotated) {
2740 err = got_object_id_str(&id_str, blame_line->id);
2746 wprintw(view->window, "%.8s ", id_str);
2748 prev_id = blame_line->id;
2750 waddstr(view->window, "........ ");
2754 waddwstr(view->window, wline);
2755 while (width < wlimit) {
2756 waddch(view->window, ' ');
2759 if (view->focussed && nprinted == selected_line - 1)
2760 wstandend(view->window);
2761 if (++nprinted == 1)
2762 *first_displayed_line = lineno;
2767 *last_displayed_line = lineno;
2774 static const struct got_error *
2775 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2777 const struct got_error *err = NULL;
2778 struct tog_blame_cb_args *a = arg;
2779 struct tog_blame_line *line;
2782 if (nlines != a->nlines ||
2783 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2784 return got_error(GOT_ERR_RANGE);
2786 errcode = pthread_mutex_lock(&tog_mutex);
2788 return got_error_set_errno(errcode, "pthread_mutex_lock");
2790 if (*a->quit) { /* user has quit the blame view */
2791 err = got_error(GOT_ERR_ITER_COMPLETED);
2796 goto done; /* no change in this commit */
2798 line = &a->lines[lineno - 1];
2799 if (line->annotated)
2802 line->id = got_object_id_dup(id);
2803 if (line->id == NULL) {
2804 err = got_error_from_errno("got_object_id_dup");
2807 line->annotated = 1;
2809 errcode = pthread_mutex_unlock(&tog_mutex);
2811 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2816 blame_thread(void *arg)
2818 const struct got_error *err;
2819 struct tog_blame_thread_args *ta = arg;
2820 struct tog_blame_cb_args *a = ta->cb_args;
2823 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2824 blame_cb, ta->cb_args);
2826 errcode = pthread_mutex_lock(&tog_mutex);
2828 return (void *)got_error_set_errno(errcode,
2829 "pthread_mutex_lock");
2831 got_repo_close(ta->repo);
2835 errcode = pthread_mutex_unlock(&tog_mutex);
2836 if (errcode && err == NULL)
2837 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2842 static struct got_object_id *
2843 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2846 struct tog_blame_line *line;
2848 line = &lines[first_displayed_line - 1 + selected_line - 1];
2849 if (!line->annotated)
2855 static const struct got_error *
2856 stop_blame(struct tog_blame *blame)
2858 const struct got_error *err = NULL;
2861 if (blame->thread) {
2863 errcode = pthread_mutex_unlock(&tog_mutex);
2865 return got_error_set_errno(errcode,
2866 "pthread_mutex_unlock");
2867 errcode = pthread_join(blame->thread, (void **)&err);
2869 return got_error_set_errno(errcode, "pthread_join");
2870 errcode = pthread_mutex_lock(&tog_mutex);
2872 return got_error_set_errno(errcode,
2873 "pthread_mutex_lock");
2874 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2876 blame->thread = NULL;
2878 if (blame->thread_args.repo) {
2879 got_repo_close(blame->thread_args.repo);
2880 blame->thread_args.repo = NULL;
2883 if (fclose(blame->f) != 0 && err == NULL)
2884 err = got_error_from_errno("fclose");
2888 for (i = 0; i < blame->nlines; i++)
2889 free(blame->lines[i].id);
2891 blame->lines = NULL;
2893 free(blame->cb_args.commit_id);
2894 blame->cb_args.commit_id = NULL;
2899 static const struct got_error *
2900 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2901 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2902 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2903 struct got_repository *repo)
2905 const struct got_error *err = NULL;
2906 struct got_blob_object *blob = NULL;
2907 struct got_repository *thread_repo = NULL;
2908 struct got_object_id *obj_id = NULL;
2911 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2915 return got_error(GOT_ERR_NO_OBJ);
2917 err = got_object_get_type(&obj_type, repo, obj_id);
2921 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2922 err = got_error(GOT_ERR_OBJ_TYPE);
2926 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2929 blame->f = got_opentemp();
2930 if (blame->f == NULL) {
2931 err = got_error_from_errno("got_opentemp");
2934 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2939 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2940 if (blame->lines == NULL) {
2941 err = got_error_from_errno("calloc");
2945 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2949 blame->cb_args.view = view;
2950 blame->cb_args.lines = blame->lines;
2951 blame->cb_args.nlines = blame->nlines;
2952 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2953 if (blame->cb_args.commit_id == NULL) {
2954 err = got_error_from_errno("got_object_id_dup");
2957 blame->cb_args.quit = done;
2959 blame->thread_args.path = path;
2960 blame->thread_args.repo = thread_repo;
2961 blame->thread_args.cb_args = &blame->cb_args;
2962 blame->thread_args.complete = blame_complete;
2963 *blame_complete = 0;
2967 got_object_blob_close(blob);
2974 static const struct got_error *
2975 open_blame_view(struct tog_view *view, char *path,
2976 struct got_object_id *commit_id, struct got_reflist_head *refs,
2977 struct got_repository *repo)
2979 const struct got_error *err = NULL;
2980 struct tog_blame_view_state *s = &view->state.blame;
2982 SIMPLEQ_INIT(&s->blamed_commits);
2984 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2988 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2989 s->first_displayed_line = 1;
2990 s->last_displayed_line = view->nlines;
2991 s->selected_line = 1;
2992 s->blame_complete = 0;
2994 if (s->path == NULL)
2995 return got_error_from_errno("open_blame_view");
2998 s->commit_id = commit_id;
2999 memset(&s->blame, 0, sizeof(s->blame));
3001 view->show = show_blame_view;
3002 view->input = input_blame_view;
3003 view->close = close_blame_view;
3005 return run_blame(&s->blame, view, &s->blame_complete,
3006 &s->first_displayed_line, &s->last_displayed_line,
3007 &s->selected_line, &s->done, &s->eof, s->path,
3008 s->blamed_commit->id, s->repo);
3011 static const struct got_error *
3012 close_blame_view(struct tog_view *view)
3014 const struct got_error *err = NULL;
3015 struct tog_blame_view_state *s = &view->state.blame;
3017 if (s->blame.thread)
3018 err = stop_blame(&s->blame);
3020 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3021 struct got_object_qid *blamed_commit;
3022 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3023 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3024 got_object_qid_free(blamed_commit);
3032 static const struct got_error *
3033 show_blame_view(struct tog_view *view)
3035 const struct got_error *err = NULL;
3036 struct tog_blame_view_state *s = &view->state.blame;
3039 if (s->blame.thread == NULL) {
3040 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3041 &s->blame.thread_args);
3043 return got_error_set_errno(errcode, "pthread_create");
3046 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3047 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3048 s->selected_line, &s->first_displayed_line,
3049 &s->last_displayed_line, &s->eof, view->nlines);
3055 static const struct got_error *
3056 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3057 struct tog_view **focus_view, struct tog_view *view, int ch)
3059 const struct got_error *err = NULL, *thread_err = NULL;
3060 struct tog_view *diff_view;
3061 struct tog_blame_view_state *s = &view->state.blame;
3070 if (s->selected_line > 1)
3072 else if (s->selected_line == 1 &&
3073 s->first_displayed_line > 1)
3074 s->first_displayed_line--;
3077 if (s->first_displayed_line == 1) {
3078 s->selected_line = 1;
3081 if (s->first_displayed_line > view->nlines - 2)
3082 s->first_displayed_line -=
3085 s->first_displayed_line = 1;
3089 if (s->selected_line < view->nlines - 2 &&
3090 s->first_displayed_line +
3091 s->selected_line <= s->blame.nlines)
3093 else if (s->last_displayed_line <
3095 s->first_displayed_line++;
3099 struct got_object_id *id = NULL;
3100 id = get_selected_commit_id(s->blame.lines,
3101 s->first_displayed_line, s->selected_line);
3105 struct got_commit_object *commit;
3106 struct got_object_qid *pid;
3107 struct got_object_id *blob_id = NULL;
3109 err = got_object_open_as_commit(&commit,
3113 pid = SIMPLEQ_FIRST(
3114 got_object_commit_get_parent_ids(commit));
3116 got_object_commit_close(commit);
3119 /* Check if path history ends here. */
3120 err = got_object_id_by_path(&blob_id, s->repo,
3123 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3125 got_object_commit_close(commit);
3128 err = got_object_get_type(&obj_type, s->repo,
3131 /* Can't blame non-blob type objects. */
3132 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3133 got_object_commit_close(commit);
3136 err = got_object_qid_alloc(&s->blamed_commit,
3138 got_object_commit_close(commit);
3140 if (got_object_id_cmp(id,
3141 s->blamed_commit->id) == 0)
3143 err = got_object_qid_alloc(&s->blamed_commit,
3149 thread_err = stop_blame(&s->blame);
3153 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3154 s->blamed_commit, entry);
3155 err = run_blame(&s->blame, view, &s->blame_complete,
3156 &s->first_displayed_line, &s->last_displayed_line,
3157 &s->selected_line, &s->done, &s->eof,
3158 s->path, s->blamed_commit->id, s->repo);
3164 struct got_object_qid *first;
3165 first = SIMPLEQ_FIRST(&s->blamed_commits);
3166 if (!got_object_id_cmp(first->id, s->commit_id))
3169 thread_err = stop_blame(&s->blame);
3173 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3174 got_object_qid_free(s->blamed_commit);
3176 SIMPLEQ_FIRST(&s->blamed_commits);
3177 err = run_blame(&s->blame, view, &s->blame_complete,
3178 &s->first_displayed_line, &s->last_displayed_line,
3179 &s->selected_line, &s->done, &s->eof, s->path,
3180 s->blamed_commit->id, s->repo);
3187 struct got_object_id *id = NULL;
3188 struct got_object_qid *pid;
3189 struct got_commit_object *commit = NULL;
3190 id = get_selected_commit_id(s->blame.lines,
3191 s->first_displayed_line, s->selected_line);
3194 err = got_object_open_as_commit(&commit, s->repo, id);
3197 pid = SIMPLEQ_FIRST(
3198 got_object_commit_get_parent_ids(commit));
3199 if (view_is_parent_view(view))
3200 begin_x = view_split_begin_x(view->begin_x);
3201 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3202 if (diff_view == NULL) {
3203 got_object_commit_close(commit);
3204 err = got_error_from_errno("view_open");
3207 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3208 id, NULL, s->refs, s->repo);
3209 got_object_commit_close(commit);
3211 view_close(diff_view);
3214 if (view_is_parent_view(view)) {
3215 err = view_close_child(view);
3218 err = view_set_child(view, diff_view);
3220 view_close(diff_view);
3223 *focus_view = diff_view;
3224 view->child_focussed = 1;
3226 *new_view = diff_view;
3233 if (s->last_displayed_line >= s->blame.nlines &&
3234 s->selected_line >= MIN(s->blame.nlines,
3235 view->nlines - 2)) {
3238 if (s->last_displayed_line >= s->blame.nlines &&
3239 s->selected_line < view->nlines - 2) {
3240 s->selected_line = MIN(s->blame.nlines,
3244 if (s->last_displayed_line + view->nlines - 2
3246 s->first_displayed_line +=
3249 s->first_displayed_line =
3254 if (s->selected_line > view->nlines - 2) {
3255 s->selected_line = MIN(s->blame.nlines,
3262 return thread_err ? thread_err : err;
3265 static const struct got_error *
3266 cmd_blame(int argc, char *argv[])
3268 const struct got_error *error;
3269 struct got_repository *repo = NULL;
3270 struct got_reflist_head refs;
3271 struct got_worktree *worktree = NULL;
3272 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3273 struct got_object_id *commit_id = NULL;
3274 char *commit_id_str = NULL;
3276 struct tog_view *view;
3278 SIMPLEQ_INIT(&refs);
3281 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3286 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3289 commit_id_str = optarg;
3292 repo_path = realpath(optarg, NULL);
3293 if (repo_path == NULL)
3294 err(1, "-r option");
3310 cwd = getcwd(NULL, 0);
3312 error = got_error_from_errno("getcwd");
3315 if (repo_path == NULL) {
3316 error = got_worktree_open(&worktree, cwd);
3317 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3323 strdup(got_worktree_get_repo_path(worktree));
3324 if (repo_path == NULL)
3325 error = got_error_from_errno("strdup");
3329 repo_path = strdup(cwd);
3330 if (repo_path == NULL) {
3331 error = got_error_from_errno("strdup");
3339 error = got_repo_open(&repo, repo_path);
3343 error = apply_unveil(got_repo_get_path(repo), NULL);
3348 const char *prefix = got_worktree_get_path_prefix(worktree);
3349 char *p, *worktree_subdir = cwd +
3350 strlen(got_worktree_get_root_path(worktree));
3351 if (asprintf(&p, "%s%s%s%s%s",
3352 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3353 worktree_subdir, worktree_subdir[0] ? "/" : "",
3355 error = got_error_from_errno("asprintf");
3358 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3361 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3366 if (commit_id_str == NULL) {
3367 struct got_reference *head_ref;
3368 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3371 error = got_ref_resolve(&commit_id, repo, head_ref);
3372 got_ref_close(head_ref);
3374 error = got_object_resolve_id_str(&commit_id, repo,
3380 error = got_ref_list(&refs, repo);
3384 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3386 error = got_error_from_errno("view_open");
3389 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3392 error = view_loop(view);
3398 got_worktree_close(worktree);
3400 got_repo_close(repo);
3401 got_ref_list_free(&refs);
3405 static const struct got_error *
3406 draw_tree_entries(struct tog_view *view,
3407 struct got_tree_entry **first_displayed_entry,
3408 struct got_tree_entry **last_displayed_entry,
3409 struct got_tree_entry **selected_entry, int *ndisplayed,
3410 const char *label, int show_ids, const char *parent_path,
3411 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3413 const struct got_error *err = NULL;
3414 struct got_tree_entry *te;
3420 werase(view->window);
3425 err = format_line(&wline, &width, label, view->ncols);
3428 if (view_needs_focus_indication(view))
3429 wstandout(view->window);
3430 waddwstr(view->window, wline);
3431 if (view_needs_focus_indication(view))
3432 wstandend(view->window);
3435 if (width < view->ncols)
3436 waddch(view->window, '\n');
3439 err = format_line(&wline, &width, parent_path, view->ncols);
3442 waddwstr(view->window, wline);
3445 if (width < view->ncols)
3446 waddch(view->window, '\n');
3449 waddch(view->window, '\n');
3453 te = SIMPLEQ_FIRST(&entries->head);
3454 if (*first_displayed_entry == NULL) {
3455 if (selected == 0) {
3457 wstandout(view->window);
3458 *selected_entry = NULL;
3460 waddstr(view->window, " ..\n"); /* parent directory */
3461 if (selected == 0 && view->focussed)
3462 wstandend(view->window);
3469 while (te != *first_displayed_entry)
3470 te = SIMPLEQ_NEXT(te, entry);
3474 char *line = NULL, *id_str = NULL;
3477 err = got_object_id_str(&id_str, te->id);
3479 return got_error_from_errno(
3480 "got_object_id_str");
3482 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3483 te->name, S_ISDIR(te->mode) ? "/" :
3484 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3486 return got_error_from_errno("asprintf");
3489 err = format_line(&wline, &width, line, view->ncols);
3494 if (n == selected) {
3496 wstandout(view->window);
3497 *selected_entry = te;
3499 waddwstr(view->window, wline);
3500 if (width < view->ncols)
3501 waddch(view->window, '\n');
3502 if (n == selected && view->focussed)
3503 wstandend(view->window);
3509 *last_displayed_entry = te;
3512 te = SIMPLEQ_NEXT(te, entry);
3519 tree_scroll_up(struct tog_view *view,
3520 struct got_tree_entry **first_displayed_entry, int maxscroll,
3521 const struct got_tree_entries *entries, int isroot)
3523 struct got_tree_entry *te, *prev;
3526 if (*first_displayed_entry == NULL)
3529 te = SIMPLEQ_FIRST(&entries->head);
3530 if (*first_displayed_entry == te) {
3532 *first_displayed_entry = NULL;
3536 /* XXX this is stupid... switch to TAILQ? */
3537 for (i = 0; i < maxscroll; i++) {
3538 while (te != *first_displayed_entry) {
3540 te = SIMPLEQ_NEXT(te, entry);
3542 *first_displayed_entry = prev;
3543 te = SIMPLEQ_FIRST(&entries->head);
3545 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3546 *first_displayed_entry = NULL;
3550 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3551 struct got_tree_entry *last_displayed_entry,
3552 const struct got_tree_entries *entries)
3554 struct got_tree_entry *next, *last;
3557 if (*first_displayed_entry)
3558 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3560 next = SIMPLEQ_FIRST(&entries->head);
3561 last = last_displayed_entry;
3562 while (next && last && n++ < maxscroll) {
3563 last = SIMPLEQ_NEXT(last, entry);
3565 *first_displayed_entry = next;
3566 next = SIMPLEQ_NEXT(next, entry);
3572 static const struct got_error *
3573 tree_entry_path(char **path, struct tog_parent_trees *parents,
3574 struct got_tree_entry *te)
3576 const struct got_error *err = NULL;
3577 struct tog_parent_tree *pt;
3578 size_t len = 2; /* for leading slash and NUL */
3580 TAILQ_FOREACH(pt, parents, entry)
3581 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3583 len += strlen(te->name);
3585 *path = calloc(1, len);
3587 return got_error_from_errno("calloc");
3590 pt = TAILQ_LAST(parents, tog_parent_trees);
3592 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3593 err = got_error(GOT_ERR_NO_SPACE);
3596 if (strlcat(*path, "/", len) >= len) {
3597 err = got_error(GOT_ERR_NO_SPACE);
3600 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3603 if (strlcat(*path, te->name, len) >= len) {
3604 err = got_error(GOT_ERR_NO_SPACE);
3616 static const struct got_error *
3617 blame_tree_entry(struct tog_view **new_view, int begin_x,
3618 struct got_tree_entry *te, struct tog_parent_trees *parents,
3619 struct got_object_id *commit_id, struct got_reflist_head *refs,
3620 struct got_repository *repo)
3622 const struct got_error *err = NULL;
3624 struct tog_view *blame_view;
3626 err = tree_entry_path(&path, parents, te);
3630 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3631 if (blame_view == NULL)
3632 return got_error_from_errno("view_open");
3634 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3636 view_close(blame_view);
3639 *new_view = blame_view;
3643 static const struct got_error *
3644 log_tree_entry(struct tog_view **new_view, int begin_x,
3645 struct got_tree_entry *te, struct tog_parent_trees *parents,
3646 struct got_object_id *commit_id, struct got_reflist_head *refs,
3647 struct got_repository *repo)
3649 struct tog_view *log_view;
3650 const struct got_error *err = NULL;
3653 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3654 if (log_view == NULL)
3655 return got_error_from_errno("view_open");
3657 err = tree_entry_path(&path, parents, te);
3661 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3663 view_close(log_view);
3665 *new_view = log_view;
3670 static const struct got_error *
3671 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3672 struct got_object_id *commit_id, struct got_reflist_head *refs,
3673 struct got_repository *repo)
3675 const struct got_error *err = NULL;
3676 char *commit_id_str = NULL;
3677 struct tog_tree_view_state *s = &view->state.tree;
3679 TAILQ_INIT(&s->parents);
3681 err = got_object_id_str(&commit_id_str, commit_id);
3685 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3686 err = got_error_from_errno("asprintf");
3690 s->root = s->tree = root;
3691 s->entries = got_object_tree_get_entries(root);
3692 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3693 s->commit_id = got_object_id_dup(commit_id);
3694 if (s->commit_id == NULL) {
3695 err = got_error_from_errno("got_object_id_dup");
3701 view->show = show_tree_view;
3702 view->input = input_tree_view;
3703 view->close = close_tree_view;
3705 free(commit_id_str);
3707 free(s->tree_label);
3708 s->tree_label = NULL;
3713 static const struct got_error *
3714 close_tree_view(struct tog_view *view)
3716 struct tog_tree_view_state *s = &view->state.tree;
3718 free(s->tree_label);
3719 s->tree_label = NULL;
3721 s->commit_id = NULL;
3722 while (!TAILQ_EMPTY(&s->parents)) {
3723 struct tog_parent_tree *parent;
3724 parent = TAILQ_FIRST(&s->parents);
3725 TAILQ_REMOVE(&s->parents, parent, entry);
3729 if (s->tree != s->root)
3730 got_object_tree_close(s->tree);
3731 got_object_tree_close(s->root);
3736 static const struct got_error *
3737 show_tree_view(struct tog_view *view)
3739 const struct got_error *err = NULL;
3740 struct tog_tree_view_state *s = &view->state.tree;
3743 err = tree_entry_path(&parent_path, &s->parents, NULL);
3747 err = draw_tree_entries(view, &s->first_displayed_entry,
3748 &s->last_displayed_entry, &s->selected_entry,
3749 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3750 s->entries, s->selected, view->nlines, s->tree == s->root);
3757 static const struct got_error *
3758 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3759 struct tog_view **focus_view, struct tog_view *view, int ch)
3761 const struct got_error *err = NULL;
3762 struct tog_tree_view_state *s = &view->state.tree;
3763 struct tog_view *log_view;
3764 int begin_x = 0, nscrolled;
3768 s->show_ids = !s->show_ids;
3771 if (!s->selected_entry)
3773 if (view_is_parent_view(view))
3774 begin_x = view_split_begin_x(view->begin_x);
3775 err = log_tree_entry(&log_view, begin_x,
3776 s->selected_entry, &s->parents,
3777 s->commit_id, s->refs, s->repo);
3778 if (view_is_parent_view(view)) {
3779 err = view_close_child(view);
3782 err = view_set_child(view, log_view);
3784 view_close(log_view);
3787 *focus_view = log_view;
3788 view->child_focussed = 1;
3790 *new_view = log_view;
3794 if (s->selected > 0) {
3796 if (s->selected == 0)
3799 if (s->selected > 0)
3801 tree_scroll_up(view, &s->first_displayed_entry, 1,
3802 s->entries, s->tree == s->root);
3805 tree_scroll_up(view, &s->first_displayed_entry,
3806 MAX(0, view->nlines - 4 - s->selected), s->entries,
3807 s->tree == s->root);
3809 if (SIMPLEQ_FIRST(&s->entries->head) ==
3810 s->first_displayed_entry && s->tree != s->root)
3811 s->first_displayed_entry = NULL;
3815 if (s->selected < s->ndisplayed - 1) {
3819 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3820 /* can't scroll any further */
3822 tree_scroll_down(&s->first_displayed_entry, 1,
3823 s->last_displayed_entry, s->entries);
3826 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3828 /* can't scroll any further; move cursor down */
3829 if (s->selected < s->ndisplayed - 1)
3830 s->selected = s->ndisplayed - 1;
3833 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3834 view->nlines, s->last_displayed_entry, s->entries);
3835 if (nscrolled < view->nlines) {
3837 struct got_tree_entry *te;
3838 te = s->first_displayed_entry;
3841 te = SIMPLEQ_NEXT(te, entry);
3843 s->selected = ndisplayed - 1;
3849 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3850 struct tog_parent_tree *parent;
3851 /* user selected '..' */
3852 if (s->tree == s->root)
3854 parent = TAILQ_FIRST(&s->parents);
3855 TAILQ_REMOVE(&s->parents, parent,
3857 got_object_tree_close(s->tree);
3858 s->tree = parent->tree;
3860 got_object_tree_get_entries(s->tree);
3861 s->first_displayed_entry =
3862 parent->first_displayed_entry;
3864 parent->selected_entry;
3865 s->selected = parent->selected;
3867 } else if (S_ISDIR(s->selected_entry->mode)) {
3868 struct got_tree_object *subtree;
3869 err = got_object_open_as_tree(&subtree,
3870 s->repo, s->selected_entry->id);
3873 err = tree_view_visit_subtree(subtree, s);
3875 got_object_tree_close(subtree);
3878 } else if (S_ISREG(s->selected_entry->mode)) {
3879 struct tog_view *blame_view;
3880 int begin_x = view_is_parent_view(view) ?
3881 view_split_begin_x(view->begin_x) : 0;
3883 err = blame_tree_entry(&blame_view, begin_x,
3884 s->selected_entry, &s->parents,
3885 s->commit_id, s->refs, s->repo);
3888 if (view_is_parent_view(view)) {
3889 err = view_close_child(view);
3892 err = view_set_child(view, blame_view);
3894 view_close(blame_view);
3897 *focus_view = blame_view;
3898 view->child_focussed = 1;
3900 *new_view = blame_view;
3904 if (s->selected > view->nlines)
3905 s->selected = s->ndisplayed - 1;
3918 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3923 static const struct got_error *
3924 cmd_tree(int argc, char *argv[])
3926 const struct got_error *error;
3927 struct got_repository *repo = NULL;
3928 struct got_reflist_head refs;
3929 char *repo_path = NULL;
3930 struct got_object_id *commit_id = NULL;
3931 char *commit_id_arg = NULL;
3932 struct got_commit_object *commit = NULL;
3933 struct got_tree_object *tree = NULL;
3935 struct tog_view *view;
3937 SIMPLEQ_INIT(&refs);
3940 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3945 while ((ch = getopt(argc, argv, "c:")) != -1) {
3948 commit_id_arg = optarg;
3960 struct got_worktree *worktree;
3961 char *cwd = getcwd(NULL, 0);
3963 return got_error_from_errno("getcwd");
3964 error = got_worktree_open(&worktree, cwd);
3965 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3970 strdup(got_worktree_get_repo_path(worktree));
3971 got_worktree_close(worktree);
3974 if (repo_path == NULL) {
3975 error = got_error_from_errno("strdup");
3978 } else if (argc == 1) {
3979 repo_path = realpath(argv[0], NULL);
3980 if (repo_path == NULL)
3981 return got_error_from_errno2("realpath", argv[0]);
3987 error = got_repo_open(&repo, repo_path);
3991 error = apply_unveil(got_repo_get_path(repo), NULL);
3995 if (commit_id_arg == NULL)
3996 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
3998 error = got_object_resolve_id_str(&commit_id, repo,
4003 error = got_object_open_as_commit(&commit, repo, commit_id);
4007 error = got_object_open_as_tree(&tree, repo,
4008 got_object_commit_get_tree_id(commit));
4012 error = got_ref_list(&refs, repo);
4016 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4018 error = got_error_from_errno("view_open");
4021 error = open_tree_view(view, tree, commit_id, &refs, repo);
4024 error = view_loop(view);
4029 got_object_commit_close(commit);
4031 got_object_tree_close(tree);
4033 got_repo_close(repo);
4034 got_ref_list_free(&refs);
4043 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4044 "Available commands:\n", getprogname());
4045 for (i = 0; i < nitems(tog_commands); i++) {
4046 struct tog_cmd *cmd = &tog_commands[i];
4047 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4053 make_argv(const char *arg0, const char *arg1)
4056 int argc = (arg1 == NULL ? 1 : 2);
4058 argv = calloc(argc, sizeof(char *));
4061 argv[0] = strdup(arg0);
4062 if (argv[0] == NULL)
4065 argv[1] = strdup(arg1);
4066 if (argv[1] == NULL)
4074 main(int argc, char *argv[])
4076 const struct got_error *error = NULL;
4077 struct tog_cmd *cmd = NULL;
4079 char **cmd_argv = NULL;
4081 setlocale(LC_CTYPE, "");
4083 while ((ch = getopt(argc, argv, "h")) != -1) {
4102 /* Build an argument vector which runs a default command. */
4103 cmd = &tog_commands[0];
4104 cmd_argv = make_argv(cmd->name, NULL);
4109 /* Did the user specific a command? */
4110 for (i = 0; i < nitems(tog_commands); i++) {
4111 if (strncmp(tog_commands[i].name, argv[0],
4112 strlen(argv[0])) == 0) {
4113 cmd = &tog_commands[i];
4115 tog_commands[i].cmd_usage();
4120 /* Did the user specify a repository? */
4121 char *repo_path = realpath(argv[0], NULL);
4123 struct got_repository *repo;
4124 error = got_repo_open(&repo, repo_path);
4126 got_repo_close(repo);
4128 error = got_error_from_errno2("realpath",
4132 fprintf(stderr, "%s: '%s' is not a "
4133 "known command\n", getprogname(),
4137 fprintf(stderr, "%s: '%s' is neither a known "
4138 "command nor a path to a repository\n",
4139 getprogname(), argv[0]);
4143 cmd = &tog_commands[0];
4144 cmd_argv = make_argv(cmd->name, repo_path);
4150 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4157 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);