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)) {
1472 /* Jump to this file's entry. */
1473 s->first_displayed_entry = s->selected_entry;
1478 slash = strchr(p, '/');
1480 subpath = strndup(path, slash - path);
1482 subpath = strdup(path);
1483 if (subpath == NULL) {
1484 err = got_error_from_errno("strdup");
1488 err = got_object_id_by_path(&tree_id, repo, entry->id,
1493 err = got_object_open_as_tree(&tree, repo, tree_id);
1498 err = tree_view_visit_subtree(tree, s);
1500 got_object_tree_close(tree);
1515 log_thread(void *arg)
1517 const struct got_error *err = NULL;
1519 struct tog_log_thread_args *a = arg;
1522 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1526 while (!done && !err) {
1527 err = queue_commits(a->graph, a->commits, 1, a->repo,
1530 if (err->code != GOT_ERR_ITER_COMPLETED)
1534 } else if (a->commits_needed > 0)
1535 a->commits_needed--;
1537 errcode = pthread_mutex_lock(&tog_mutex);
1539 err = got_error_set_errno(errcode,
1540 "pthread_mutex_lock");
1542 } else if (*a->quit)
1544 else if (*a->first_displayed_entry == NULL) {
1545 *a->first_displayed_entry =
1546 TAILQ_FIRST(&a->commits->head);
1547 *a->selected_entry = *a->first_displayed_entry;
1551 a->commits_needed = 0;
1552 else if (a->commits_needed == 0) {
1553 errcode = pthread_cond_wait(&a->need_commits,
1556 err = got_error_set_errno(errcode,
1557 "pthread_cond_wait");
1560 errcode = pthread_mutex_unlock(&tog_mutex);
1561 if (errcode && err == NULL)
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_unlock");
1565 a->log_complete = 1;
1569 static const struct got_error *
1570 stop_log_thread(struct tog_log_view_state *s)
1572 const struct got_error *err = NULL;
1577 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1579 return got_error_set_errno(errcode,
1580 "pthread_cond_signal");
1581 errcode = pthread_mutex_unlock(&tog_mutex);
1583 return got_error_set_errno(errcode,
1584 "pthread_mutex_unlock");
1585 errcode = pthread_join(s->thread, (void **)&err);
1587 return got_error_set_errno(errcode, "pthread_join");
1588 errcode = pthread_mutex_lock(&tog_mutex);
1590 return got_error_set_errno(errcode,
1591 "pthread_mutex_lock");
1595 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1596 if (errcode && err == NULL)
1597 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1599 if (s->thread_args.repo) {
1600 got_repo_close(s->thread_args.repo);
1601 s->thread_args.repo = NULL;
1604 if (s->thread_args.graph) {
1605 got_commit_graph_close(s->thread_args.graph);
1606 s->thread_args.graph = NULL;
1612 static const struct got_error *
1613 close_log_view(struct tog_view *view)
1615 const struct got_error *err = NULL;
1616 struct tog_log_view_state *s = &view->state.log;
1618 err = stop_log_thread(s);
1619 free_commits(&s->commits);
1620 free(s->in_repo_path);
1621 s->in_repo_path = NULL;
1627 static const struct got_error *
1628 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1629 struct got_reflist_head *refs, struct got_repository *repo,
1630 const char *path, int check_disk)
1632 const struct got_error *err = NULL;
1633 struct tog_log_view_state *s = &view->state.log;
1634 struct got_repository *thread_repo = NULL;
1635 struct got_commit_graph *thread_graph = NULL;
1638 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1642 /* The commit queue only contains commits being displayed. */
1643 TAILQ_INIT(&s->commits.head);
1644 s->commits.ncommits = 0;
1648 s->start_id = got_object_id_dup(start_id);
1649 if (s->start_id == NULL) {
1650 err = got_error_from_errno("got_object_id_dup");
1654 view->show = show_log_view;
1655 view->input = input_log_view;
1656 view->close = close_log_view;
1658 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1661 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1666 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1668 err = got_error_set_errno(errcode, "pthread_cond_init");
1672 s->thread_args.commits_needed = view->nlines;
1673 s->thread_args.graph = thread_graph;
1674 s->thread_args.commits = &s->commits;
1675 s->thread_args.in_repo_path = s->in_repo_path;
1676 s->thread_args.start_id = s->start_id;
1677 s->thread_args.repo = thread_repo;
1678 s->thread_args.log_complete = 0;
1679 s->thread_args.quit = &s->quit;
1680 s->thread_args.view = view;
1681 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1682 s->thread_args.selected_entry = &s->selected_entry;
1685 close_log_view(view);
1689 static const struct got_error *
1690 show_log_view(struct tog_view *view)
1692 struct tog_log_view_state *s = &view->state.log;
1694 if (s->thread == NULL) {
1695 int errcode = pthread_create(&s->thread, NULL, log_thread,
1698 return got_error_set_errno(errcode, "pthread_create");
1701 return draw_commits(view, &s->last_displayed_entry,
1702 &s->selected_entry, s->first_displayed_entry,
1703 &s->commits, s->selected, view->nlines, s->refs,
1704 s->in_repo_path, s->thread_args.commits_needed);
1707 static const struct got_error *
1708 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1709 struct tog_view **focus_view, struct tog_view *view, int ch)
1711 const struct got_error *err = NULL;
1712 struct tog_log_view_state *s = &view->state.log;
1714 struct tog_view *diff_view = NULL, *tree_view = NULL;
1725 if (s->first_displayed_entry == NULL)
1727 if (s->selected > 0)
1729 if (s->selected > 0)
1731 scroll_up(view, &s->first_displayed_entry, 1,
1736 if (s->first_displayed_entry == NULL)
1738 if (TAILQ_FIRST(&s->commits.head) ==
1739 s->first_displayed_entry) {
1743 scroll_up(view, &s->first_displayed_entry,
1744 view->nlines, &s->commits);
1750 if (s->first_displayed_entry == NULL)
1752 if (s->selected < MIN(view->nlines - 2,
1753 s->commits.ncommits - 1)) {
1757 err = scroll_down(view, &s->first_displayed_entry, 1,
1758 &s->last_displayed_entry, &s->commits,
1759 &s->thread_args.log_complete,
1760 &s->thread_args.commits_needed,
1761 &s->thread_args.need_commits);
1765 struct commit_queue_entry *first;
1766 first = s->first_displayed_entry;
1769 err = scroll_down(view, &s->first_displayed_entry,
1770 view->nlines, &s->last_displayed_entry,
1771 &s->commits, &s->thread_args.log_complete,
1772 &s->thread_args.commits_needed,
1773 &s->thread_args.need_commits);
1774 if (first == s->first_displayed_entry &&
1775 s->selected < MIN(view->nlines - 2,
1776 s->commits.ncommits - 1)) {
1777 /* can't scroll further down */
1778 s->selected = MIN(view->nlines - 2,
1779 s->commits.ncommits - 1);
1785 if (s->selected > view->nlines - 2)
1786 s->selected = view->nlines - 2;
1787 if (s->selected > s->commits.ncommits - 1)
1788 s->selected = s->commits.ncommits - 1;
1793 if (s->selected_entry == NULL)
1795 if (view_is_parent_view(view))
1796 begin_x = view_split_begin_x(view->begin_x);
1797 err = open_diff_view_for_commit(&diff_view, begin_x,
1798 s->selected_entry->commit, s->selected_entry->id,
1799 view, s->refs, s->repo);
1802 if (view_is_parent_view(view)) {
1803 err = view_close_child(view);
1806 err = view_set_child(view, diff_view);
1808 view_close(diff_view);
1811 *focus_view = diff_view;
1812 view->child_focussed = 1;
1814 *new_view = diff_view;
1817 if (s->selected_entry == NULL)
1819 if (view_is_parent_view(view))
1820 begin_x = view_split_begin_x(view->begin_x);
1821 err = browse_commit_tree(&tree_view, begin_x,
1822 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1825 if (view_is_parent_view(view)) {
1826 err = view_close_child(view);
1829 err = view_set_child(view, tree_view);
1831 view_close(tree_view);
1834 *focus_view = tree_view;
1835 view->child_focussed = 1;
1837 *new_view = tree_view;
1840 if (strcmp(s->in_repo_path, "/") == 0)
1842 parent_path = dirname(s->in_repo_path);
1843 if (parent_path && strcmp(parent_path, ".") != 0) {
1844 struct tog_view *lv;
1845 err = stop_log_thread(s);
1848 lv = view_open(view->nlines, view->ncols,
1849 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1851 return got_error_from_errno(
1853 err = open_log_view(lv, s->start_id, s->refs,
1854 s->repo, parent_path, 0);
1857 if (view_is_parent_view(view))
1860 view_set_child(view->parent, lv);
1873 static const struct got_error *
1874 apply_unveil(const char *repo_path, const char *worktree_path)
1876 const struct got_error *error;
1878 if (repo_path && unveil(repo_path, "r") != 0)
1879 return got_error_from_errno2("unveil", repo_path);
1881 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1882 return got_error_from_errno2("unveil", worktree_path);
1884 if (unveil("/tmp", "rwc") != 0)
1885 return got_error_from_errno2("unveil", "/tmp");
1887 error = got_privsep_unveil_exec_helpers();
1891 if (unveil(NULL, NULL) != 0)
1892 return got_error_from_errno("unveil");
1902 halfdelay(1); /* Do fast refresh while initial view is loading. */
1905 intrflush(stdscr, FALSE);
1906 keypad(stdscr, TRUE);
1908 signal(SIGWINCH, tog_sigwinch);
1911 static const struct got_error *
1912 cmd_log(int argc, char *argv[])
1914 const struct got_error *error;
1915 struct got_repository *repo = NULL;
1916 struct got_worktree *worktree = NULL;
1917 struct got_reflist_head refs;
1918 struct got_object_id *start_id = NULL;
1919 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1920 char *start_commit = NULL;
1922 struct tog_view *view;
1924 SIMPLEQ_INIT(&refs);
1927 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1932 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1935 start_commit = optarg;
1938 repo_path = realpath(optarg, NULL);
1939 if (repo_path == NULL)
1940 err(1, "-r option");
1951 cwd = getcwd(NULL, 0);
1953 error = got_error_from_errno("getcwd");
1956 error = got_worktree_open(&worktree, cwd);
1957 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1964 error = got_error_from_errno("strdup");
1967 } else if (argc == 1) {
1969 error = got_worktree_resolve_path(&path, worktree,
1974 path = strdup(argv[0]);
1976 error = got_error_from_errno("strdup");
1983 repo_path = worktree ?
1984 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1985 if (repo_path == NULL) {
1986 error = got_error_from_errno("strdup");
1992 error = got_repo_open(&repo, repo_path);
1996 error = apply_unveil(got_repo_get_path(repo),
1997 worktree ? got_worktree_get_root_path(worktree) : NULL);
2001 if (start_commit == NULL)
2002 error = get_head_commit_id(&start_id, worktree ?
2003 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2006 error = got_object_resolve_id_str(&start_id, repo,
2011 error = got_ref_list(&refs, repo);
2015 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2017 error = got_error_from_errno("view_open");
2020 error = open_log_view(view, start_id, &refs, repo, path, 1);
2023 error = view_loop(view);
2030 got_repo_close(repo);
2032 got_worktree_close(worktree);
2033 got_ref_list_free(&refs);
2041 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2047 parse_next_line(FILE *f, size_t *len)
2052 const char delim[3] = { '\0', '\0', '\0'};
2054 line = fparseln(f, &linelen, &lineno, delim, 0);
2060 static const struct got_error *
2061 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2062 int *last_displayed_line, int *eof, int max_lines,
2065 const struct got_error *err;
2066 int nlines = 0, nprinted = 0;
2073 werase(view->window);
2076 err = format_line(&wline, &width, header, view->ncols);
2081 if (view_needs_focus_indication(view))
2082 wstandout(view->window);
2083 waddwstr(view->window, wline);
2084 if (view_needs_focus_indication(view))
2085 wstandend(view->window);
2086 if (width < view->ncols)
2087 waddch(view->window, '\n');
2095 while (nprinted < max_lines) {
2096 line = parse_next_line(f, &len);
2101 if (++nlines < *first_displayed_line) {
2106 err = format_line(&wline, &width, line, view->ncols);
2111 waddwstr(view->window, wline);
2112 if (width < view->ncols)
2113 waddch(view->window, '\n');
2114 if (++nprinted == 1)
2115 *first_displayed_line = nlines;
2120 *last_displayed_line = nlines;
2125 while (nprinted < view->nlines) {
2126 waddch(view->window, '\n');
2130 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2135 wstandout(view->window);
2136 waddwstr(view->window, wline);
2137 wstandend(view->window);
2144 get_datestr(time_t *time, char *datebuf)
2146 char *p, *s = ctime_r(time, datebuf);
2147 p = strchr(s, '\n');
2153 static const struct got_error *
2154 write_commit_info(struct got_object_id *commit_id,
2155 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2157 const struct got_error *err = NULL;
2159 struct got_commit_object *commit;
2160 char *id_str = NULL;
2161 time_t committer_time;
2162 const char *author, *committer;
2163 char *refs_str = NULL;
2166 err = build_refs_str(&refs_str, refs, commit_id);
2171 err = got_object_open_as_commit(&commit, repo, commit_id);
2175 err = got_object_id_str(&id_str, commit_id);
2177 err = got_error_from_errno("got_object_id_str");
2181 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2182 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2183 err = got_error_from_errno("fprintf");
2186 if (fprintf(outfile, "from: %s\n",
2187 got_object_commit_get_author(commit)) < 0) {
2188 err = got_error_from_errno("fprintf");
2191 committer_time = got_object_commit_get_committer_time(commit);
2192 if (fprintf(outfile, "date: %s UTC\n",
2193 get_datestr(&committer_time, datebuf)) < 0) {
2194 err = got_error_from_errno("fprintf");
2197 author = got_object_commit_get_author(commit);
2198 committer = got_object_commit_get_committer(commit);
2199 if (strcmp(author, committer) != 0 &&
2200 fprintf(outfile, "via: %s\n", committer) < 0) {
2201 err = got_error_from_errno("fprintf");
2204 if (fprintf(outfile, "%s\n",
2205 got_object_commit_get_logmsg(commit)) < 0) {
2206 err = got_error_from_errno("fprintf");
2212 got_object_commit_close(commit);
2216 static const struct got_error *
2217 create_diff(struct tog_diff_view_state *s)
2219 const struct got_error *err = NULL;
2225 err = got_error_from_errno("got_opentemp");
2228 if (s->f && fclose(s->f) != 0) {
2229 err = got_error_from_errno("fclose");
2235 err = got_object_get_type(&obj_type, s->repo, s->id1);
2237 err = got_object_get_type(&obj_type, s->repo, s->id2);
2242 case GOT_OBJ_TYPE_BLOB:
2243 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2244 s->diff_context, s->repo, f);
2246 case GOT_OBJ_TYPE_TREE:
2247 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2248 s->diff_context, s->repo, f);
2250 case GOT_OBJ_TYPE_COMMIT: {
2251 const struct got_object_id_queue *parent_ids;
2252 struct got_object_qid *pid;
2253 struct got_commit_object *commit2;
2255 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2258 /* Show commit info if we're diffing to a parent/root commit. */
2260 write_commit_info(s->id2, s->refs, s->repo, f);
2262 parent_ids = got_object_commit_get_parent_ids(commit2);
2263 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2264 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2265 write_commit_info(s->id2, s->refs,
2271 got_object_commit_close(commit2);
2273 err = got_diff_objects_as_commits(s->id1, s->id2,
2274 s->diff_context, s->repo, f);
2278 err = got_error(GOT_ERR_OBJ_TYPE);
2282 if (f && fflush(f) != 0 && err == NULL)
2283 err = got_error_from_errno("fflush");
2288 diff_view_indicate_progress(struct tog_view *view)
2290 mvwaddstr(view->window, 0, 0, "diffing...");
2295 static const struct got_error *
2296 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2297 struct got_object_id *id2, struct tog_view *log_view,
2298 struct got_reflist_head *refs, struct got_repository *repo)
2300 const struct got_error *err;
2302 if (id1 != NULL && id2 != NULL) {
2304 err = got_object_get_type(&type1, repo, id1);
2307 err = got_object_get_type(&type2, repo, id2);
2312 return got_error(GOT_ERR_OBJ_TYPE);
2316 view->state.diff.id1 = got_object_id_dup(id1);
2317 if (view->state.diff.id1 == NULL)
2318 return got_error_from_errno("got_object_id_dup");
2320 view->state.diff.id1 = NULL;
2322 view->state.diff.id2 = got_object_id_dup(id2);
2323 if (view->state.diff.id2 == NULL) {
2324 free(view->state.diff.id1);
2325 view->state.diff.id1 = NULL;
2326 return got_error_from_errno("got_object_id_dup");
2328 view->state.diff.f = NULL;
2329 view->state.diff.first_displayed_line = 1;
2330 view->state.diff.last_displayed_line = view->nlines;
2331 view->state.diff.diff_context = 3;
2332 view->state.diff.log_view = log_view;
2333 view->state.diff.repo = repo;
2334 view->state.diff.refs = refs;
2336 if (log_view && view_is_splitscreen(view))
2337 show_log_view(log_view); /* draw vborder */
2338 diff_view_indicate_progress(view);
2340 err = create_diff(&view->state.diff);
2342 free(view->state.diff.id1);
2343 view->state.diff.id1 = NULL;
2344 free(view->state.diff.id2);
2345 view->state.diff.id2 = NULL;
2349 view->show = show_diff_view;
2350 view->input = input_diff_view;
2351 view->close = close_diff_view;
2356 static const struct got_error *
2357 close_diff_view(struct tog_view *view)
2359 const struct got_error *err = NULL;
2361 free(view->state.diff.id1);
2362 view->state.diff.id1 = NULL;
2363 free(view->state.diff.id2);
2364 view->state.diff.id2 = NULL;
2365 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2366 err = got_error_from_errno("fclose");
2370 static const struct got_error *
2371 show_diff_view(struct tog_view *view)
2373 const struct got_error *err;
2374 struct tog_diff_view_state *s = &view->state.diff;
2375 char *id_str1 = NULL, *id_str2, *header;
2378 err = got_object_id_str(&id_str1, s->id1);
2382 err = got_object_id_str(&id_str2, s->id2);
2386 if (asprintf(&header, "diff %s %s",
2387 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2388 err = got_error_from_errno("asprintf");
2396 return draw_file(view, s->f, &s->first_displayed_line,
2397 &s->last_displayed_line, &s->eof, view->nlines,
2401 static const struct got_error *
2402 set_selected_commit(struct tog_diff_view_state *s,
2403 struct commit_queue_entry *entry)
2405 const struct got_error *err;
2406 const struct got_object_id_queue *parent_ids;
2407 struct got_commit_object *selected_commit;
2408 struct got_object_qid *pid;
2411 s->id2 = got_object_id_dup(entry->id);
2413 return got_error_from_errno("got_object_id_dup");
2415 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2418 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2420 pid = SIMPLEQ_FIRST(parent_ids);
2421 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2422 got_object_commit_close(selected_commit);
2426 static const struct got_error *
2427 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2428 struct tog_view **focus_view, struct tog_view *view, int ch)
2430 const struct got_error *err = NULL;
2431 struct tog_diff_view_state *s = &view->state.diff;
2432 struct tog_log_view_state *ls;
2433 struct commit_queue_entry *entry;
2439 if (s->first_displayed_line > 1)
2440 s->first_displayed_line--;
2444 if (s->first_displayed_line == 1)
2447 while (i++ < view->nlines - 1 &&
2448 s->first_displayed_line > 1)
2449 s->first_displayed_line--;
2454 s->first_displayed_line++;
2462 while (!s->eof && i++ < view->nlines - 1) {
2464 line = parse_next_line(s->f, NULL);
2465 s->first_displayed_line++;
2471 if (s->diff_context > 0) {
2473 diff_view_indicate_progress(view);
2474 err = create_diff(s);
2478 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2480 diff_view_indicate_progress(view);
2481 err = create_diff(s);
2486 if (s->log_view == NULL)
2488 ls = &s->log_view->state.log;
2489 entry = TAILQ_PREV(ls->selected_entry,
2490 commit_queue_head, entry);
2494 err = input_log_view(NULL, NULL, NULL, s->log_view,
2499 err = set_selected_commit(s, entry);
2503 s->first_displayed_line = 1;
2504 s->last_displayed_line = view->nlines;
2506 diff_view_indicate_progress(view);
2507 err = create_diff(s);
2511 if (s->log_view == NULL)
2513 ls = &s->log_view->state.log;
2515 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2516 ls->thread_args.commits_needed++;
2518 /* Display "loading..." in log view. */
2519 show_log_view(s->log_view);
2523 err = trigger_log_thread(1 /* load_all */,
2524 &ls->thread_args.commits_needed,
2525 &ls->thread_args.log_complete,
2526 &ls->thread_args.need_commits);
2530 err = input_log_view(NULL, NULL, NULL, s->log_view,
2535 entry = TAILQ_NEXT(ls->selected_entry, entry);
2539 err = set_selected_commit(s, entry);
2543 s->first_displayed_line = 1;
2544 s->last_displayed_line = view->nlines;
2546 diff_view_indicate_progress(view);
2547 err = create_diff(s);
2556 static const struct got_error *
2557 cmd_diff(int argc, char *argv[])
2559 const struct got_error *error = NULL;
2560 struct got_repository *repo = NULL;
2561 struct got_reflist_head refs;
2562 struct got_object_id *id1 = NULL, *id2 = NULL;
2563 char *repo_path = NULL;
2564 char *id_str1 = NULL, *id_str2 = NULL;
2566 struct tog_view *view;
2568 SIMPLEQ_INIT(&refs);
2571 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2576 while ((ch = getopt(argc, argv, "")) != -1) {
2588 usage_diff(); /* TODO show local worktree changes */
2589 } else if (argc == 2) {
2590 repo_path = getcwd(NULL, 0);
2591 if (repo_path == NULL)
2592 return got_error_from_errno("getcwd");
2595 } else if (argc == 3) {
2596 repo_path = realpath(argv[0], NULL);
2597 if (repo_path == NULL)
2598 return got_error_from_errno2("realpath", argv[0]);
2606 error = got_repo_open(&repo, repo_path);
2610 error = apply_unveil(got_repo_get_path(repo), NULL);
2614 error = got_object_resolve_id_str(&id1, repo, id_str1);
2618 error = got_object_resolve_id_str(&id2, repo, id_str2);
2622 error = got_ref_list(&refs, repo);
2626 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2628 error = got_error_from_errno("view_open");
2631 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2634 error = view_loop(view);
2637 got_repo_close(repo);
2638 got_ref_list_free(&refs);
2646 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2651 struct tog_blame_line {
2653 struct got_object_id *id;
2656 static const struct got_error *
2657 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2658 const char *path, struct tog_blame_line *lines, int nlines,
2659 int blame_complete, int selected_line, int *first_displayed_line,
2660 int *last_displayed_line, int *eof, int max_lines)
2662 const struct got_error *err;
2663 int lineno = 0, nprinted = 0;
2668 struct tog_blame_line *blame_line;
2669 struct got_object_id *prev_id = NULL;
2672 err = got_object_id_str(&id_str, id);
2677 werase(view->window);
2679 if (asprintf(&line, "commit %s", id_str) == -1) {
2680 err = got_error_from_errno("asprintf");
2685 err = format_line(&wline, &width, line, view->ncols);
2688 if (view_needs_focus_indication(view))
2689 wstandout(view->window);
2690 waddwstr(view->window, wline);
2691 if (view_needs_focus_indication(view))
2692 wstandend(view->window);
2695 if (width < view->ncols)
2696 waddch(view->window, '\n');
2698 if (asprintf(&line, "[%d/%d] %s%s",
2699 *first_displayed_line - 1 + selected_line, nlines,
2700 blame_complete ? "" : "annotating... ", path) == -1) {
2702 return got_error_from_errno("asprintf");
2705 err = format_line(&wline, &width, line, view->ncols);
2710 waddwstr(view->window, wline);
2713 if (width < view->ncols)
2714 waddch(view->window, '\n');
2717 while (nprinted < max_lines - 2) {
2718 line = parse_next_line(f, &len);
2723 if (++lineno < *first_displayed_line) {
2728 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2729 err = format_line(&wline, &width, line, wlimit);
2735 if (view->focussed && nprinted == selected_line - 1)
2736 wstandout(view->window);
2738 blame_line = &lines[lineno - 1];
2739 if (blame_line->annotated && prev_id &&
2740 got_object_id_cmp(prev_id, blame_line->id) == 0)
2741 waddstr(view->window, " ");
2742 else if (blame_line->annotated) {
2744 err = got_object_id_str(&id_str, blame_line->id);
2750 wprintw(view->window, "%.8s ", id_str);
2752 prev_id = blame_line->id;
2754 waddstr(view->window, "........ ");
2758 waddwstr(view->window, wline);
2759 while (width < wlimit) {
2760 waddch(view->window, ' ');
2763 if (view->focussed && nprinted == selected_line - 1)
2764 wstandend(view->window);
2765 if (++nprinted == 1)
2766 *first_displayed_line = lineno;
2771 *last_displayed_line = lineno;
2778 static const struct got_error *
2779 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2781 const struct got_error *err = NULL;
2782 struct tog_blame_cb_args *a = arg;
2783 struct tog_blame_line *line;
2786 if (nlines != a->nlines ||
2787 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2788 return got_error(GOT_ERR_RANGE);
2790 errcode = pthread_mutex_lock(&tog_mutex);
2792 return got_error_set_errno(errcode, "pthread_mutex_lock");
2794 if (*a->quit) { /* user has quit the blame view */
2795 err = got_error(GOT_ERR_ITER_COMPLETED);
2800 goto done; /* no change in this commit */
2802 line = &a->lines[lineno - 1];
2803 if (line->annotated)
2806 line->id = got_object_id_dup(id);
2807 if (line->id == NULL) {
2808 err = got_error_from_errno("got_object_id_dup");
2811 line->annotated = 1;
2813 errcode = pthread_mutex_unlock(&tog_mutex);
2815 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2820 blame_thread(void *arg)
2822 const struct got_error *err;
2823 struct tog_blame_thread_args *ta = arg;
2824 struct tog_blame_cb_args *a = ta->cb_args;
2827 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2828 blame_cb, ta->cb_args);
2830 errcode = pthread_mutex_lock(&tog_mutex);
2832 return (void *)got_error_set_errno(errcode,
2833 "pthread_mutex_lock");
2835 got_repo_close(ta->repo);
2839 errcode = pthread_mutex_unlock(&tog_mutex);
2840 if (errcode && err == NULL)
2841 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2846 static struct got_object_id *
2847 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2850 struct tog_blame_line *line;
2852 line = &lines[first_displayed_line - 1 + selected_line - 1];
2853 if (!line->annotated)
2859 static const struct got_error *
2860 stop_blame(struct tog_blame *blame)
2862 const struct got_error *err = NULL;
2865 if (blame->thread) {
2867 errcode = pthread_mutex_unlock(&tog_mutex);
2869 return got_error_set_errno(errcode,
2870 "pthread_mutex_unlock");
2871 errcode = pthread_join(blame->thread, (void **)&err);
2873 return got_error_set_errno(errcode, "pthread_join");
2874 errcode = pthread_mutex_lock(&tog_mutex);
2876 return got_error_set_errno(errcode,
2877 "pthread_mutex_lock");
2878 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2880 blame->thread = NULL;
2882 if (blame->thread_args.repo) {
2883 got_repo_close(blame->thread_args.repo);
2884 blame->thread_args.repo = NULL;
2887 if (fclose(blame->f) != 0 && err == NULL)
2888 err = got_error_from_errno("fclose");
2892 for (i = 0; i < blame->nlines; i++)
2893 free(blame->lines[i].id);
2895 blame->lines = NULL;
2897 free(blame->cb_args.commit_id);
2898 blame->cb_args.commit_id = NULL;
2903 static const struct got_error *
2904 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2905 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2906 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL;
2910 struct got_blob_object *blob = NULL;
2911 struct got_repository *thread_repo = NULL;
2912 struct got_object_id *obj_id = NULL;
2915 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2919 return got_error(GOT_ERR_NO_OBJ);
2921 err = got_object_get_type(&obj_type, repo, obj_id);
2925 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2926 err = got_error(GOT_ERR_OBJ_TYPE);
2930 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2933 blame->f = got_opentemp();
2934 if (blame->f == NULL) {
2935 err = got_error_from_errno("got_opentemp");
2938 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2943 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2944 if (blame->lines == NULL) {
2945 err = got_error_from_errno("calloc");
2949 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2953 blame->cb_args.view = view;
2954 blame->cb_args.lines = blame->lines;
2955 blame->cb_args.nlines = blame->nlines;
2956 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2957 if (blame->cb_args.commit_id == NULL) {
2958 err = got_error_from_errno("got_object_id_dup");
2961 blame->cb_args.quit = done;
2963 blame->thread_args.path = path;
2964 blame->thread_args.repo = thread_repo;
2965 blame->thread_args.cb_args = &blame->cb_args;
2966 blame->thread_args.complete = blame_complete;
2967 *blame_complete = 0;
2971 got_object_blob_close(blob);
2978 static const struct got_error *
2979 open_blame_view(struct tog_view *view, char *path,
2980 struct got_object_id *commit_id, struct got_reflist_head *refs,
2981 struct got_repository *repo)
2983 const struct got_error *err = NULL;
2984 struct tog_blame_view_state *s = &view->state.blame;
2986 SIMPLEQ_INIT(&s->blamed_commits);
2988 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2992 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2993 s->first_displayed_line = 1;
2994 s->last_displayed_line = view->nlines;
2995 s->selected_line = 1;
2996 s->blame_complete = 0;
2998 if (s->path == NULL)
2999 return got_error_from_errno("open_blame_view");
3002 s->commit_id = commit_id;
3003 memset(&s->blame, 0, sizeof(s->blame));
3005 view->show = show_blame_view;
3006 view->input = input_blame_view;
3007 view->close = close_blame_view;
3009 return run_blame(&s->blame, view, &s->blame_complete,
3010 &s->first_displayed_line, &s->last_displayed_line,
3011 &s->selected_line, &s->done, &s->eof, s->path,
3012 s->blamed_commit->id, s->repo);
3015 static const struct got_error *
3016 close_blame_view(struct tog_view *view)
3018 const struct got_error *err = NULL;
3019 struct tog_blame_view_state *s = &view->state.blame;
3021 if (s->blame.thread)
3022 err = stop_blame(&s->blame);
3024 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3025 struct got_object_qid *blamed_commit;
3026 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3027 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3028 got_object_qid_free(blamed_commit);
3036 static const struct got_error *
3037 show_blame_view(struct tog_view *view)
3039 const struct got_error *err = NULL;
3040 struct tog_blame_view_state *s = &view->state.blame;
3043 if (s->blame.thread == NULL) {
3044 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3045 &s->blame.thread_args);
3047 return got_error_set_errno(errcode, "pthread_create");
3050 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3051 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3052 s->selected_line, &s->first_displayed_line,
3053 &s->last_displayed_line, &s->eof, view->nlines);
3059 static const struct got_error *
3060 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3061 struct tog_view **focus_view, struct tog_view *view, int ch)
3063 const struct got_error *err = NULL, *thread_err = NULL;
3064 struct tog_view *diff_view;
3065 struct tog_blame_view_state *s = &view->state.blame;
3074 if (s->selected_line > 1)
3076 else if (s->selected_line == 1 &&
3077 s->first_displayed_line > 1)
3078 s->first_displayed_line--;
3081 if (s->first_displayed_line == 1) {
3082 s->selected_line = 1;
3085 if (s->first_displayed_line > view->nlines - 2)
3086 s->first_displayed_line -=
3089 s->first_displayed_line = 1;
3093 if (s->selected_line < view->nlines - 2 &&
3094 s->first_displayed_line +
3095 s->selected_line <= s->blame.nlines)
3097 else if (s->last_displayed_line <
3099 s->first_displayed_line++;
3103 struct got_object_id *id = NULL;
3104 id = get_selected_commit_id(s->blame.lines,
3105 s->first_displayed_line, s->selected_line);
3109 struct got_commit_object *commit;
3110 struct got_object_qid *pid;
3111 struct got_object_id *blob_id = NULL;
3113 err = got_object_open_as_commit(&commit,
3117 pid = SIMPLEQ_FIRST(
3118 got_object_commit_get_parent_ids(commit));
3120 got_object_commit_close(commit);
3123 /* Check if path history ends here. */
3124 err = got_object_id_by_path(&blob_id, s->repo,
3127 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3129 got_object_commit_close(commit);
3132 err = got_object_get_type(&obj_type, s->repo,
3135 /* Can't blame non-blob type objects. */
3136 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3137 got_object_commit_close(commit);
3140 err = got_object_qid_alloc(&s->blamed_commit,
3142 got_object_commit_close(commit);
3144 if (got_object_id_cmp(id,
3145 s->blamed_commit->id) == 0)
3147 err = got_object_qid_alloc(&s->blamed_commit,
3153 thread_err = stop_blame(&s->blame);
3157 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3158 s->blamed_commit, entry);
3159 err = run_blame(&s->blame, view, &s->blame_complete,
3160 &s->first_displayed_line, &s->last_displayed_line,
3161 &s->selected_line, &s->done, &s->eof,
3162 s->path, s->blamed_commit->id, s->repo);
3168 struct got_object_qid *first;
3169 first = SIMPLEQ_FIRST(&s->blamed_commits);
3170 if (!got_object_id_cmp(first->id, s->commit_id))
3173 thread_err = stop_blame(&s->blame);
3177 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3178 got_object_qid_free(s->blamed_commit);
3180 SIMPLEQ_FIRST(&s->blamed_commits);
3181 err = run_blame(&s->blame, view, &s->blame_complete,
3182 &s->first_displayed_line, &s->last_displayed_line,
3183 &s->selected_line, &s->done, &s->eof, s->path,
3184 s->blamed_commit->id, s->repo);
3191 struct got_object_id *id = NULL;
3192 struct got_object_qid *pid;
3193 struct got_commit_object *commit = NULL;
3194 id = get_selected_commit_id(s->blame.lines,
3195 s->first_displayed_line, s->selected_line);
3198 err = got_object_open_as_commit(&commit, s->repo, id);
3201 pid = SIMPLEQ_FIRST(
3202 got_object_commit_get_parent_ids(commit));
3203 if (view_is_parent_view(view))
3204 begin_x = view_split_begin_x(view->begin_x);
3205 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3206 if (diff_view == NULL) {
3207 got_object_commit_close(commit);
3208 err = got_error_from_errno("view_open");
3211 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3212 id, NULL, s->refs, s->repo);
3213 got_object_commit_close(commit);
3215 view_close(diff_view);
3218 if (view_is_parent_view(view)) {
3219 err = view_close_child(view);
3222 err = view_set_child(view, diff_view);
3224 view_close(diff_view);
3227 *focus_view = diff_view;
3228 view->child_focussed = 1;
3230 *new_view = diff_view;
3237 if (s->last_displayed_line >= s->blame.nlines &&
3238 s->selected_line >= MIN(s->blame.nlines,
3239 view->nlines - 2)) {
3242 if (s->last_displayed_line >= s->blame.nlines &&
3243 s->selected_line < view->nlines - 2) {
3244 s->selected_line = MIN(s->blame.nlines,
3248 if (s->last_displayed_line + view->nlines - 2
3250 s->first_displayed_line +=
3253 s->first_displayed_line =
3258 if (s->selected_line > view->nlines - 2) {
3259 s->selected_line = MIN(s->blame.nlines,
3266 return thread_err ? thread_err : err;
3269 static const struct got_error *
3270 cmd_blame(int argc, char *argv[])
3272 const struct got_error *error;
3273 struct got_repository *repo = NULL;
3274 struct got_reflist_head refs;
3275 struct got_worktree *worktree = NULL;
3276 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3277 struct got_object_id *commit_id = NULL;
3278 char *commit_id_str = NULL;
3280 struct tog_view *view;
3282 SIMPLEQ_INIT(&refs);
3285 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3290 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3293 commit_id_str = optarg;
3296 repo_path = realpath(optarg, NULL);
3297 if (repo_path == NULL)
3298 err(1, "-r option");
3314 cwd = getcwd(NULL, 0);
3316 error = got_error_from_errno("getcwd");
3319 if (repo_path == NULL) {
3320 error = got_worktree_open(&worktree, cwd);
3321 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3327 strdup(got_worktree_get_repo_path(worktree));
3328 if (repo_path == NULL)
3329 error = got_error_from_errno("strdup");
3333 repo_path = strdup(cwd);
3334 if (repo_path == NULL) {
3335 error = got_error_from_errno("strdup");
3343 error = got_repo_open(&repo, repo_path);
3347 error = apply_unveil(got_repo_get_path(repo), NULL);
3352 const char *prefix = got_worktree_get_path_prefix(worktree);
3353 char *p, *worktree_subdir = cwd +
3354 strlen(got_worktree_get_root_path(worktree));
3355 if (asprintf(&p, "%s%s%s%s%s",
3356 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3357 worktree_subdir, worktree_subdir[0] ? "/" : "",
3359 error = got_error_from_errno("asprintf");
3362 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3365 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3370 if (commit_id_str == NULL) {
3371 struct got_reference *head_ref;
3372 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3375 error = got_ref_resolve(&commit_id, repo, head_ref);
3376 got_ref_close(head_ref);
3378 error = got_object_resolve_id_str(&commit_id, repo,
3384 error = got_ref_list(&refs, repo);
3388 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3390 error = got_error_from_errno("view_open");
3393 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3396 error = view_loop(view);
3402 got_worktree_close(worktree);
3404 got_repo_close(repo);
3405 got_ref_list_free(&refs);
3409 static const struct got_error *
3410 draw_tree_entries(struct tog_view *view,
3411 struct got_tree_entry **first_displayed_entry,
3412 struct got_tree_entry **last_displayed_entry,
3413 struct got_tree_entry **selected_entry, int *ndisplayed,
3414 const char *label, int show_ids, const char *parent_path,
3415 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3417 const struct got_error *err = NULL;
3418 struct got_tree_entry *te;
3424 werase(view->window);
3429 err = format_line(&wline, &width, label, view->ncols);
3432 if (view_needs_focus_indication(view))
3433 wstandout(view->window);
3434 waddwstr(view->window, wline);
3435 if (view_needs_focus_indication(view))
3436 wstandend(view->window);
3439 if (width < view->ncols)
3440 waddch(view->window, '\n');
3443 err = format_line(&wline, &width, parent_path, view->ncols);
3446 waddwstr(view->window, wline);
3449 if (width < view->ncols)
3450 waddch(view->window, '\n');
3453 waddch(view->window, '\n');
3457 te = SIMPLEQ_FIRST(&entries->head);
3458 if (*first_displayed_entry == NULL) {
3459 if (selected == 0) {
3461 wstandout(view->window);
3462 *selected_entry = NULL;
3464 waddstr(view->window, " ..\n"); /* parent directory */
3465 if (selected == 0 && view->focussed)
3466 wstandend(view->window);
3473 while (te != *first_displayed_entry)
3474 te = SIMPLEQ_NEXT(te, entry);
3478 char *line = NULL, *id_str = NULL;
3481 err = got_object_id_str(&id_str, te->id);
3483 return got_error_from_errno(
3484 "got_object_id_str");
3486 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3487 te->name, S_ISDIR(te->mode) ? "/" :
3488 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3490 return got_error_from_errno("asprintf");
3493 err = format_line(&wline, &width, line, view->ncols);
3498 if (n == selected) {
3500 wstandout(view->window);
3501 *selected_entry = te;
3503 waddwstr(view->window, wline);
3504 if (width < view->ncols)
3505 waddch(view->window, '\n');
3506 if (n == selected && view->focussed)
3507 wstandend(view->window);
3513 *last_displayed_entry = te;
3516 te = SIMPLEQ_NEXT(te, entry);
3523 tree_scroll_up(struct tog_view *view,
3524 struct got_tree_entry **first_displayed_entry, int maxscroll,
3525 const struct got_tree_entries *entries, int isroot)
3527 struct got_tree_entry *te, *prev;
3530 if (*first_displayed_entry == NULL)
3533 te = SIMPLEQ_FIRST(&entries->head);
3534 if (*first_displayed_entry == te) {
3536 *first_displayed_entry = NULL;
3540 /* XXX this is stupid... switch to TAILQ? */
3541 for (i = 0; i < maxscroll; i++) {
3542 while (te != *first_displayed_entry) {
3544 te = SIMPLEQ_NEXT(te, entry);
3546 *first_displayed_entry = prev;
3547 te = SIMPLEQ_FIRST(&entries->head);
3549 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3550 *first_displayed_entry = NULL;
3554 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3555 struct got_tree_entry *last_displayed_entry,
3556 const struct got_tree_entries *entries)
3558 struct got_tree_entry *next, *last;
3561 if (*first_displayed_entry)
3562 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3564 next = SIMPLEQ_FIRST(&entries->head);
3565 last = last_displayed_entry;
3566 while (next && last && n++ < maxscroll) {
3567 last = SIMPLEQ_NEXT(last, entry);
3569 *first_displayed_entry = next;
3570 next = SIMPLEQ_NEXT(next, entry);
3576 static const struct got_error *
3577 tree_entry_path(char **path, struct tog_parent_trees *parents,
3578 struct got_tree_entry *te)
3580 const struct got_error *err = NULL;
3581 struct tog_parent_tree *pt;
3582 size_t len = 2; /* for leading slash and NUL */
3584 TAILQ_FOREACH(pt, parents, entry)
3585 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3587 len += strlen(te->name);
3589 *path = calloc(1, len);
3591 return got_error_from_errno("calloc");
3594 pt = TAILQ_LAST(parents, tog_parent_trees);
3596 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3597 err = got_error(GOT_ERR_NO_SPACE);
3600 if (strlcat(*path, "/", len) >= len) {
3601 err = got_error(GOT_ERR_NO_SPACE);
3604 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3607 if (strlcat(*path, te->name, len) >= len) {
3608 err = got_error(GOT_ERR_NO_SPACE);
3620 static const struct got_error *
3621 blame_tree_entry(struct tog_view **new_view, int begin_x,
3622 struct got_tree_entry *te, struct tog_parent_trees *parents,
3623 struct got_object_id *commit_id, struct got_reflist_head *refs,
3624 struct got_repository *repo)
3626 const struct got_error *err = NULL;
3628 struct tog_view *blame_view;
3630 err = tree_entry_path(&path, parents, te);
3634 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3635 if (blame_view == NULL)
3636 return got_error_from_errno("view_open");
3638 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3640 view_close(blame_view);
3643 *new_view = blame_view;
3647 static const struct got_error *
3648 log_tree_entry(struct tog_view **new_view, int begin_x,
3649 struct got_tree_entry *te, struct tog_parent_trees *parents,
3650 struct got_object_id *commit_id, struct got_reflist_head *refs,
3651 struct got_repository *repo)
3653 struct tog_view *log_view;
3654 const struct got_error *err = NULL;
3657 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3658 if (log_view == NULL)
3659 return got_error_from_errno("view_open");
3661 err = tree_entry_path(&path, parents, te);
3665 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3667 view_close(log_view);
3669 *new_view = log_view;
3674 static const struct got_error *
3675 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3676 struct got_object_id *commit_id, struct got_reflist_head *refs,
3677 struct got_repository *repo)
3679 const struct got_error *err = NULL;
3680 char *commit_id_str = NULL;
3681 struct tog_tree_view_state *s = &view->state.tree;
3683 TAILQ_INIT(&s->parents);
3685 err = got_object_id_str(&commit_id_str, commit_id);
3689 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3690 err = got_error_from_errno("asprintf");
3694 s->root = s->tree = root;
3695 s->entries = got_object_tree_get_entries(root);
3696 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3697 s->commit_id = got_object_id_dup(commit_id);
3698 if (s->commit_id == NULL) {
3699 err = got_error_from_errno("got_object_id_dup");
3705 view->show = show_tree_view;
3706 view->input = input_tree_view;
3707 view->close = close_tree_view;
3709 free(commit_id_str);
3711 free(s->tree_label);
3712 s->tree_label = NULL;
3717 static const struct got_error *
3718 close_tree_view(struct tog_view *view)
3720 struct tog_tree_view_state *s = &view->state.tree;
3722 free(s->tree_label);
3723 s->tree_label = NULL;
3725 s->commit_id = NULL;
3726 while (!TAILQ_EMPTY(&s->parents)) {
3727 struct tog_parent_tree *parent;
3728 parent = TAILQ_FIRST(&s->parents);
3729 TAILQ_REMOVE(&s->parents, parent, entry);
3733 if (s->tree != s->root)
3734 got_object_tree_close(s->tree);
3735 got_object_tree_close(s->root);
3740 static const struct got_error *
3741 show_tree_view(struct tog_view *view)
3743 const struct got_error *err = NULL;
3744 struct tog_tree_view_state *s = &view->state.tree;
3747 err = tree_entry_path(&parent_path, &s->parents, NULL);
3751 err = draw_tree_entries(view, &s->first_displayed_entry,
3752 &s->last_displayed_entry, &s->selected_entry,
3753 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3754 s->entries, s->selected, view->nlines, s->tree == s->root);
3761 static const struct got_error *
3762 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3763 struct tog_view **focus_view, struct tog_view *view, int ch)
3765 const struct got_error *err = NULL;
3766 struct tog_tree_view_state *s = &view->state.tree;
3767 struct tog_view *log_view;
3768 int begin_x = 0, nscrolled;
3772 s->show_ids = !s->show_ids;
3775 if (!s->selected_entry)
3777 if (view_is_parent_view(view))
3778 begin_x = view_split_begin_x(view->begin_x);
3779 err = log_tree_entry(&log_view, begin_x,
3780 s->selected_entry, &s->parents,
3781 s->commit_id, s->refs, s->repo);
3782 if (view_is_parent_view(view)) {
3783 err = view_close_child(view);
3786 err = view_set_child(view, log_view);
3788 view_close(log_view);
3791 *focus_view = log_view;
3792 view->child_focussed = 1;
3794 *new_view = log_view;
3798 if (s->selected > 0) {
3800 if (s->selected == 0)
3803 if (s->selected > 0)
3805 tree_scroll_up(view, &s->first_displayed_entry, 1,
3806 s->entries, s->tree == s->root);
3809 tree_scroll_up(view, &s->first_displayed_entry,
3810 MAX(0, view->nlines - 4 - s->selected), s->entries,
3811 s->tree == s->root);
3813 if (SIMPLEQ_FIRST(&s->entries->head) ==
3814 s->first_displayed_entry && s->tree != s->root)
3815 s->first_displayed_entry = NULL;
3819 if (s->selected < s->ndisplayed - 1) {
3823 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3824 /* can't scroll any further */
3826 tree_scroll_down(&s->first_displayed_entry, 1,
3827 s->last_displayed_entry, s->entries);
3830 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3832 /* can't scroll any further; move cursor down */
3833 if (s->selected < s->ndisplayed - 1)
3834 s->selected = s->ndisplayed - 1;
3837 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3838 view->nlines, s->last_displayed_entry, s->entries);
3839 if (nscrolled < view->nlines) {
3841 struct got_tree_entry *te;
3842 te = s->first_displayed_entry;
3845 te = SIMPLEQ_NEXT(te, entry);
3847 s->selected = ndisplayed - 1;
3853 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3854 struct tog_parent_tree *parent;
3855 /* user selected '..' */
3856 if (s->tree == s->root)
3858 parent = TAILQ_FIRST(&s->parents);
3859 TAILQ_REMOVE(&s->parents, parent,
3861 got_object_tree_close(s->tree);
3862 s->tree = parent->tree;
3864 got_object_tree_get_entries(s->tree);
3865 s->first_displayed_entry =
3866 parent->first_displayed_entry;
3868 parent->selected_entry;
3869 s->selected = parent->selected;
3871 } else if (S_ISDIR(s->selected_entry->mode)) {
3872 struct got_tree_object *subtree;
3873 err = got_object_open_as_tree(&subtree,
3874 s->repo, s->selected_entry->id);
3877 err = tree_view_visit_subtree(subtree, s);
3879 got_object_tree_close(subtree);
3882 } else if (S_ISREG(s->selected_entry->mode)) {
3883 struct tog_view *blame_view;
3884 int begin_x = view_is_parent_view(view) ?
3885 view_split_begin_x(view->begin_x) : 0;
3887 err = blame_tree_entry(&blame_view, begin_x,
3888 s->selected_entry, &s->parents,
3889 s->commit_id, s->refs, s->repo);
3892 if (view_is_parent_view(view)) {
3893 err = view_close_child(view);
3896 err = view_set_child(view, blame_view);
3898 view_close(blame_view);
3901 *focus_view = blame_view;
3902 view->child_focussed = 1;
3904 *new_view = blame_view;
3908 if (s->selected > view->nlines)
3909 s->selected = s->ndisplayed - 1;
3922 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3927 static const struct got_error *
3928 cmd_tree(int argc, char *argv[])
3930 const struct got_error *error;
3931 struct got_repository *repo = NULL;
3932 struct got_reflist_head refs;
3933 char *repo_path = NULL;
3934 struct got_object_id *commit_id = NULL;
3935 char *commit_id_arg = NULL;
3936 struct got_commit_object *commit = NULL;
3937 struct got_tree_object *tree = NULL;
3939 struct tog_view *view;
3941 SIMPLEQ_INIT(&refs);
3944 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3949 while ((ch = getopt(argc, argv, "c:")) != -1) {
3952 commit_id_arg = optarg;
3964 struct got_worktree *worktree;
3965 char *cwd = getcwd(NULL, 0);
3967 return got_error_from_errno("getcwd");
3968 error = got_worktree_open(&worktree, cwd);
3969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3974 strdup(got_worktree_get_repo_path(worktree));
3975 got_worktree_close(worktree);
3978 if (repo_path == NULL) {
3979 error = got_error_from_errno("strdup");
3982 } else if (argc == 1) {
3983 repo_path = realpath(argv[0], NULL);
3984 if (repo_path == NULL)
3985 return got_error_from_errno2("realpath", argv[0]);
3991 error = got_repo_open(&repo, repo_path);
3995 error = apply_unveil(got_repo_get_path(repo), NULL);
3999 if (commit_id_arg == NULL)
4000 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4002 error = got_object_resolve_id_str(&commit_id, repo,
4007 error = got_object_open_as_commit(&commit, repo, commit_id);
4011 error = got_object_open_as_tree(&tree, repo,
4012 got_object_commit_get_tree_id(commit));
4016 error = got_ref_list(&refs, repo);
4020 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4022 error = got_error_from_errno("view_open");
4025 error = open_tree_view(view, tree, commit_id, &refs, repo);
4028 error = view_loop(view);
4033 got_object_commit_close(commit);
4035 got_object_tree_close(tree);
4037 got_repo_close(repo);
4038 got_ref_list_free(&refs);
4047 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4048 "Available commands:\n", getprogname());
4049 for (i = 0; i < nitems(tog_commands); i++) {
4050 struct tog_cmd *cmd = &tog_commands[i];
4051 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4057 make_argv(const char *arg0, const char *arg1)
4060 int argc = (arg1 == NULL ? 1 : 2);
4062 argv = calloc(argc, sizeof(char *));
4065 argv[0] = strdup(arg0);
4066 if (argv[0] == NULL)
4069 argv[1] = strdup(arg1);
4070 if (argv[1] == NULL)
4078 main(int argc, char *argv[])
4080 const struct got_error *error = NULL;
4081 struct tog_cmd *cmd = NULL;
4083 char **cmd_argv = NULL;
4085 setlocale(LC_CTYPE, "");
4087 while ((ch = getopt(argc, argv, "h")) != -1) {
4106 /* Build an argument vector which runs a default command. */
4107 cmd = &tog_commands[0];
4108 cmd_argv = make_argv(cmd->name, NULL);
4113 /* Did the user specific a command? */
4114 for (i = 0; i < nitems(tog_commands); i++) {
4115 if (strncmp(tog_commands[i].name, argv[0],
4116 strlen(argv[0])) == 0) {
4117 cmd = &tog_commands[i];
4119 tog_commands[i].cmd_usage();
4124 /* Did the user specify a repository? */
4125 char *repo_path = realpath(argv[0], NULL);
4127 struct got_repository *repo;
4128 error = got_repo_open(&repo, repo_path);
4130 got_repo_close(repo);
4132 error = got_error_from_errno2("realpath",
4136 fprintf(stderr, "%s: '%s' is not a "
4137 "known command\n", getprogname(),
4141 fprintf(stderr, "%s: '%s' is neither a known "
4142 "command nor a path to a repository\n",
4143 getprogname(), argv[0]);
4147 cmd = &tog_commands[0];
4148 cmd_argv = make_argv(cmd->name, repo_path);
4154 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4161 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);