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 browse_commit(struct tog_view **new_view, int begin_x,
1393 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1394 struct got_repository *repo)
1396 const struct got_error *err = NULL;
1397 struct got_tree_object *tree;
1398 struct tog_view *tree_view;
1400 err = got_object_open_as_tree(&tree, repo,
1401 got_object_commit_get_tree_id(entry->commit));
1405 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1406 if (tree_view == NULL)
1407 return got_error_from_errno("view_open");
1409 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1411 got_object_tree_close(tree);
1413 *new_view = tree_view;
1418 log_thread(void *arg)
1420 const struct got_error *err = NULL;
1422 struct tog_log_thread_args *a = arg;
1425 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1429 while (!done && !err) {
1430 err = queue_commits(a->graph, a->commits, 1, a->repo,
1433 if (err->code != GOT_ERR_ITER_COMPLETED)
1437 } else if (a->commits_needed > 0)
1438 a->commits_needed--;
1440 errcode = pthread_mutex_lock(&tog_mutex);
1442 err = got_error_set_errno(errcode,
1443 "pthread_mutex_lock");
1445 } else if (*a->quit)
1447 else if (*a->first_displayed_entry == NULL) {
1448 *a->first_displayed_entry =
1449 TAILQ_FIRST(&a->commits->head);
1450 *a->selected_entry = *a->first_displayed_entry;
1454 a->commits_needed = 0;
1455 else if (a->commits_needed == 0) {
1456 errcode = pthread_cond_wait(&a->need_commits,
1459 err = got_error_set_errno(errcode,
1460 "pthread_cond_wait");
1463 errcode = pthread_mutex_unlock(&tog_mutex);
1464 if (errcode && err == NULL)
1465 err = got_error_set_errno(errcode,
1466 "pthread_mutex_unlock");
1468 a->log_complete = 1;
1472 static const struct got_error *
1473 stop_log_thread(struct tog_log_view_state *s)
1475 const struct got_error *err = NULL;
1480 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1482 return got_error_set_errno(errcode,
1483 "pthread_cond_signal");
1484 errcode = pthread_mutex_unlock(&tog_mutex);
1486 return got_error_set_errno(errcode,
1487 "pthread_mutex_unlock");
1488 errcode = pthread_join(s->thread, (void **)&err);
1490 return got_error_set_errno(errcode, "pthread_join");
1491 errcode = pthread_mutex_lock(&tog_mutex);
1493 return got_error_set_errno(errcode,
1494 "pthread_mutex_lock");
1498 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1499 if (errcode && err == NULL)
1500 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1502 if (s->thread_args.repo) {
1503 got_repo_close(s->thread_args.repo);
1504 s->thread_args.repo = NULL;
1507 if (s->thread_args.graph) {
1508 got_commit_graph_close(s->thread_args.graph);
1509 s->thread_args.graph = NULL;
1515 static const struct got_error *
1516 close_log_view(struct tog_view *view)
1518 const struct got_error *err = NULL;
1519 struct tog_log_view_state *s = &view->state.log;
1521 err = stop_log_thread(s);
1522 free_commits(&s->commits);
1523 free(s->in_repo_path);
1524 s->in_repo_path = NULL;
1530 static const struct got_error *
1531 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1532 struct got_reflist_head *refs, struct got_repository *repo,
1533 const char *path, int check_disk)
1535 const struct got_error *err = NULL;
1536 struct tog_log_view_state *s = &view->state.log;
1537 struct got_repository *thread_repo = NULL;
1538 struct got_commit_graph *thread_graph = NULL;
1541 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1545 /* The commit queue only contains commits being displayed. */
1546 TAILQ_INIT(&s->commits.head);
1547 s->commits.ncommits = 0;
1551 s->start_id = got_object_id_dup(start_id);
1552 if (s->start_id == NULL) {
1553 err = got_error_from_errno("got_object_id_dup");
1557 view->show = show_log_view;
1558 view->input = input_log_view;
1559 view->close = close_log_view;
1561 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1564 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1569 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1571 err = got_error_set_errno(errcode, "pthread_cond_init");
1575 s->thread_args.commits_needed = view->nlines;
1576 s->thread_args.graph = thread_graph;
1577 s->thread_args.commits = &s->commits;
1578 s->thread_args.in_repo_path = s->in_repo_path;
1579 s->thread_args.start_id = s->start_id;
1580 s->thread_args.repo = thread_repo;
1581 s->thread_args.log_complete = 0;
1582 s->thread_args.quit = &s->quit;
1583 s->thread_args.view = view;
1584 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1585 s->thread_args.selected_entry = &s->selected_entry;
1588 close_log_view(view);
1592 static const struct got_error *
1593 show_log_view(struct tog_view *view)
1595 struct tog_log_view_state *s = &view->state.log;
1597 if (s->thread == NULL) {
1598 int errcode = pthread_create(&s->thread, NULL, log_thread,
1601 return got_error_set_errno(errcode, "pthread_create");
1604 return draw_commits(view, &s->last_displayed_entry,
1605 &s->selected_entry, s->first_displayed_entry,
1606 &s->commits, s->selected, view->nlines, s->refs,
1607 s->in_repo_path, s->thread_args.commits_needed);
1610 static const struct got_error *
1611 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1612 struct tog_view **focus_view, struct tog_view *view, int ch)
1614 const struct got_error *err = NULL;
1615 struct tog_log_view_state *s = &view->state.log;
1617 struct tog_view *diff_view = NULL, *tree_view = NULL;
1628 if (s->first_displayed_entry == NULL)
1630 if (s->selected > 0)
1632 if (s->selected > 0)
1634 scroll_up(view, &s->first_displayed_entry, 1,
1639 if (s->first_displayed_entry == NULL)
1641 if (TAILQ_FIRST(&s->commits.head) ==
1642 s->first_displayed_entry) {
1646 scroll_up(view, &s->first_displayed_entry,
1647 view->nlines, &s->commits);
1653 if (s->first_displayed_entry == NULL)
1655 if (s->selected < MIN(view->nlines - 2,
1656 s->commits.ncommits - 1)) {
1660 err = scroll_down(view, &s->first_displayed_entry, 1,
1661 &s->last_displayed_entry, &s->commits,
1662 &s->thread_args.log_complete,
1663 &s->thread_args.commits_needed,
1664 &s->thread_args.need_commits);
1668 struct commit_queue_entry *first;
1669 first = s->first_displayed_entry;
1672 err = scroll_down(view, &s->first_displayed_entry,
1673 view->nlines, &s->last_displayed_entry,
1674 &s->commits, &s->thread_args.log_complete,
1675 &s->thread_args.commits_needed,
1676 &s->thread_args.need_commits);
1677 if (first == s->first_displayed_entry &&
1678 s->selected < MIN(view->nlines - 2,
1679 s->commits.ncommits - 1)) {
1680 /* can't scroll further down */
1681 s->selected = MIN(view->nlines - 2,
1682 s->commits.ncommits - 1);
1688 if (s->selected > view->nlines - 2)
1689 s->selected = view->nlines - 2;
1690 if (s->selected > s->commits.ncommits - 1)
1691 s->selected = s->commits.ncommits - 1;
1696 if (s->selected_entry == NULL)
1698 if (view_is_parent_view(view))
1699 begin_x = view_split_begin_x(view->begin_x);
1700 err = open_diff_view_for_commit(&diff_view, begin_x,
1701 s->selected_entry->commit, s->selected_entry->id,
1702 view, s->refs, s->repo);
1705 if (view_is_parent_view(view)) {
1706 err = view_close_child(view);
1709 err = view_set_child(view, diff_view);
1711 view_close(diff_view);
1714 *focus_view = diff_view;
1715 view->child_focussed = 1;
1717 *new_view = diff_view;
1720 if (s->selected_entry == NULL)
1722 if (view_is_parent_view(view))
1723 begin_x = view_split_begin_x(view->begin_x);
1724 err = browse_commit(&tree_view, begin_x,
1725 s->selected_entry, s->refs, s->repo);
1728 if (view_is_parent_view(view)) {
1729 err = view_close_child(view);
1732 err = view_set_child(view, tree_view);
1734 view_close(tree_view);
1737 *focus_view = tree_view;
1738 view->child_focussed = 1;
1740 *new_view = tree_view;
1743 if (strcmp(s->in_repo_path, "/") == 0)
1745 parent_path = dirname(s->in_repo_path);
1746 if (parent_path && strcmp(parent_path, ".") != 0) {
1747 struct tog_view *lv;
1748 err = stop_log_thread(s);
1751 lv = view_open(view->nlines, view->ncols,
1752 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1754 return got_error_from_errno(
1756 err = open_log_view(lv, s->start_id, s->refs,
1757 s->repo, parent_path, 0);
1760 if (view_is_parent_view(view))
1763 view_set_child(view->parent, lv);
1776 static const struct got_error *
1777 apply_unveil(const char *repo_path, const char *worktree_path)
1779 const struct got_error *error;
1781 if (repo_path && unveil(repo_path, "r") != 0)
1782 return got_error_from_errno2("unveil", repo_path);
1784 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1785 return got_error_from_errno2("unveil", worktree_path);
1787 if (unveil("/tmp", "rwc") != 0)
1788 return got_error_from_errno2("unveil", "/tmp");
1790 error = got_privsep_unveil_exec_helpers();
1794 if (unveil(NULL, NULL) != 0)
1795 return got_error_from_errno("unveil");
1805 halfdelay(1); /* Do fast refresh while initial view is loading. */
1808 intrflush(stdscr, FALSE);
1809 keypad(stdscr, TRUE);
1811 signal(SIGWINCH, tog_sigwinch);
1814 static const struct got_error *
1815 cmd_log(int argc, char *argv[])
1817 const struct got_error *error;
1818 struct got_repository *repo = NULL;
1819 struct got_worktree *worktree = NULL;
1820 struct got_reflist_head refs;
1821 struct got_object_id *start_id = NULL;
1822 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1823 char *start_commit = NULL;
1825 struct tog_view *view;
1827 SIMPLEQ_INIT(&refs);
1830 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1835 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1838 start_commit = optarg;
1841 repo_path = realpath(optarg, NULL);
1842 if (repo_path == NULL)
1843 err(1, "-r option");
1854 cwd = getcwd(NULL, 0);
1856 error = got_error_from_errno("getcwd");
1859 error = got_worktree_open(&worktree, cwd);
1860 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1867 error = got_error_from_errno("strdup");
1870 } else if (argc == 1) {
1872 error = got_worktree_resolve_path(&path, worktree,
1877 path = strdup(argv[0]);
1879 error = got_error_from_errno("strdup");
1886 repo_path = worktree ?
1887 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1888 if (repo_path == NULL) {
1889 error = got_error_from_errno("strdup");
1895 error = got_repo_open(&repo, repo_path);
1899 error = apply_unveil(got_repo_get_path(repo),
1900 worktree ? got_worktree_get_root_path(worktree) : NULL);
1904 if (start_commit == NULL)
1905 error = get_head_commit_id(&start_id, worktree ?
1906 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
1909 error = got_object_resolve_id_str(&start_id, repo,
1914 error = got_ref_list(&refs, repo);
1918 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1920 error = got_error_from_errno("view_open");
1923 error = open_log_view(view, start_id, &refs, repo, path, 1);
1926 error = view_loop(view);
1933 got_repo_close(repo);
1935 got_worktree_close(worktree);
1936 got_ref_list_free(&refs);
1944 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1950 parse_next_line(FILE *f, size_t *len)
1955 const char delim[3] = { '\0', '\0', '\0'};
1957 line = fparseln(f, &linelen, &lineno, delim, 0);
1963 static const struct got_error *
1964 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1965 int *last_displayed_line, int *eof, int max_lines,
1968 const struct got_error *err;
1969 int nlines = 0, nprinted = 0;
1976 werase(view->window);
1979 err = format_line(&wline, &width, header, view->ncols);
1984 if (view_needs_focus_indication(view))
1985 wstandout(view->window);
1986 waddwstr(view->window, wline);
1987 if (view_needs_focus_indication(view))
1988 wstandend(view->window);
1989 if (width < view->ncols)
1990 waddch(view->window, '\n');
1998 while (nprinted < max_lines) {
1999 line = parse_next_line(f, &len);
2004 if (++nlines < *first_displayed_line) {
2009 err = format_line(&wline, &width, line, view->ncols);
2014 waddwstr(view->window, wline);
2015 if (width < view->ncols)
2016 waddch(view->window, '\n');
2017 if (++nprinted == 1)
2018 *first_displayed_line = nlines;
2023 *last_displayed_line = nlines;
2028 while (nprinted < view->nlines) {
2029 waddch(view->window, '\n');
2033 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2038 wstandout(view->window);
2039 waddwstr(view->window, wline);
2040 wstandend(view->window);
2047 get_datestr(time_t *time, char *datebuf)
2049 char *p, *s = ctime_r(time, datebuf);
2050 p = strchr(s, '\n');
2056 static const struct got_error *
2057 write_commit_info(struct got_object_id *commit_id,
2058 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2060 const struct got_error *err = NULL;
2062 struct got_commit_object *commit;
2063 char *id_str = NULL;
2064 time_t committer_time;
2065 const char *author, *committer;
2066 char *refs_str = NULL;
2069 err = build_refs_str(&refs_str, refs, commit_id);
2074 err = got_object_open_as_commit(&commit, repo, commit_id);
2078 err = got_object_id_str(&id_str, commit_id);
2080 err = got_error_from_errno("got_object_id_str");
2084 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2085 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2086 err = got_error_from_errno("fprintf");
2089 if (fprintf(outfile, "from: %s\n",
2090 got_object_commit_get_author(commit)) < 0) {
2091 err = got_error_from_errno("fprintf");
2094 committer_time = got_object_commit_get_committer_time(commit);
2095 if (fprintf(outfile, "date: %s UTC\n",
2096 get_datestr(&committer_time, datebuf)) < 0) {
2097 err = got_error_from_errno("fprintf");
2100 author = got_object_commit_get_author(commit);
2101 committer = got_object_commit_get_committer(commit);
2102 if (strcmp(author, committer) != 0 &&
2103 fprintf(outfile, "via: %s\n", committer) < 0) {
2104 err = got_error_from_errno("fprintf");
2107 if (fprintf(outfile, "%s\n",
2108 got_object_commit_get_logmsg(commit)) < 0) {
2109 err = got_error_from_errno("fprintf");
2115 got_object_commit_close(commit);
2119 static const struct got_error *
2120 create_diff(struct tog_diff_view_state *s)
2122 const struct got_error *err = NULL;
2128 err = got_error_from_errno("got_opentemp");
2131 if (s->f && fclose(s->f) != 0) {
2132 err = got_error_from_errno("fclose");
2138 err = got_object_get_type(&obj_type, s->repo, s->id1);
2140 err = got_object_get_type(&obj_type, s->repo, s->id2);
2145 case GOT_OBJ_TYPE_BLOB:
2146 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2147 s->diff_context, s->repo, f);
2149 case GOT_OBJ_TYPE_TREE:
2150 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2151 s->diff_context, s->repo, f);
2153 case GOT_OBJ_TYPE_COMMIT: {
2154 const struct got_object_id_queue *parent_ids;
2155 struct got_object_qid *pid;
2156 struct got_commit_object *commit2;
2158 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2161 /* Show commit info if we're diffing to a parent/root commit. */
2163 write_commit_info(s->id2, s->refs, s->repo, f);
2165 parent_ids = got_object_commit_get_parent_ids(commit2);
2166 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2167 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2168 write_commit_info(s->id2, s->refs,
2174 got_object_commit_close(commit2);
2176 err = got_diff_objects_as_commits(s->id1, s->id2,
2177 s->diff_context, s->repo, f);
2181 err = got_error(GOT_ERR_OBJ_TYPE);
2185 if (f && fflush(f) != 0 && err == NULL)
2186 err = got_error_from_errno("fflush");
2191 diff_view_indicate_progress(struct tog_view *view)
2193 mvwaddstr(view->window, 0, 0, "diffing...");
2198 static const struct got_error *
2199 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2200 struct got_object_id *id2, struct tog_view *log_view,
2201 struct got_reflist_head *refs, struct got_repository *repo)
2203 const struct got_error *err;
2205 if (id1 != NULL && id2 != NULL) {
2207 err = got_object_get_type(&type1, repo, id1);
2210 err = got_object_get_type(&type2, repo, id2);
2215 return got_error(GOT_ERR_OBJ_TYPE);
2219 view->state.diff.id1 = got_object_id_dup(id1);
2220 if (view->state.diff.id1 == NULL)
2221 return got_error_from_errno("got_object_id_dup");
2223 view->state.diff.id1 = NULL;
2225 view->state.diff.id2 = got_object_id_dup(id2);
2226 if (view->state.diff.id2 == NULL) {
2227 free(view->state.diff.id1);
2228 view->state.diff.id1 = NULL;
2229 return got_error_from_errno("got_object_id_dup");
2231 view->state.diff.f = NULL;
2232 view->state.diff.first_displayed_line = 1;
2233 view->state.diff.last_displayed_line = view->nlines;
2234 view->state.diff.diff_context = 3;
2235 view->state.diff.log_view = log_view;
2236 view->state.diff.repo = repo;
2237 view->state.diff.refs = refs;
2239 if (log_view && view_is_splitscreen(view))
2240 show_log_view(log_view); /* draw vborder */
2241 diff_view_indicate_progress(view);
2243 err = create_diff(&view->state.diff);
2245 free(view->state.diff.id1);
2246 view->state.diff.id1 = NULL;
2247 free(view->state.diff.id2);
2248 view->state.diff.id2 = NULL;
2252 view->show = show_diff_view;
2253 view->input = input_diff_view;
2254 view->close = close_diff_view;
2259 static const struct got_error *
2260 close_diff_view(struct tog_view *view)
2262 const struct got_error *err = NULL;
2264 free(view->state.diff.id1);
2265 view->state.diff.id1 = NULL;
2266 free(view->state.diff.id2);
2267 view->state.diff.id2 = NULL;
2268 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2269 err = got_error_from_errno("fclose");
2273 static const struct got_error *
2274 show_diff_view(struct tog_view *view)
2276 const struct got_error *err;
2277 struct tog_diff_view_state *s = &view->state.diff;
2278 char *id_str1 = NULL, *id_str2, *header;
2281 err = got_object_id_str(&id_str1, s->id1);
2285 err = got_object_id_str(&id_str2, s->id2);
2289 if (asprintf(&header, "diff %s %s",
2290 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2291 err = got_error_from_errno("asprintf");
2299 return draw_file(view, s->f, &s->first_displayed_line,
2300 &s->last_displayed_line, &s->eof, view->nlines,
2304 static const struct got_error *
2305 set_selected_commit(struct tog_diff_view_state *s,
2306 struct commit_queue_entry *entry)
2308 const struct got_error *err;
2309 const struct got_object_id_queue *parent_ids;
2310 struct got_commit_object *selected_commit;
2311 struct got_object_qid *pid;
2314 s->id2 = got_object_id_dup(entry->id);
2316 return got_error_from_errno("got_object_id_dup");
2318 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2321 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2323 pid = SIMPLEQ_FIRST(parent_ids);
2324 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2325 got_object_commit_close(selected_commit);
2329 static const struct got_error *
2330 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2331 struct tog_view **focus_view, struct tog_view *view, int ch)
2333 const struct got_error *err = NULL;
2334 struct tog_diff_view_state *s = &view->state.diff;
2335 struct tog_log_view_state *ls;
2336 struct commit_queue_entry *entry;
2342 if (s->first_displayed_line > 1)
2343 s->first_displayed_line--;
2347 if (s->first_displayed_line == 1)
2350 while (i++ < view->nlines - 1 &&
2351 s->first_displayed_line > 1)
2352 s->first_displayed_line--;
2357 s->first_displayed_line++;
2365 while (!s->eof && i++ < view->nlines - 1) {
2367 line = parse_next_line(s->f, NULL);
2368 s->first_displayed_line++;
2374 if (s->diff_context > 0) {
2376 diff_view_indicate_progress(view);
2377 err = create_diff(s);
2381 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2383 diff_view_indicate_progress(view);
2384 err = create_diff(s);
2389 if (s->log_view == NULL)
2391 ls = &s->log_view->state.log;
2392 entry = TAILQ_PREV(ls->selected_entry,
2393 commit_queue_head, entry);
2397 err = input_log_view(NULL, NULL, NULL, s->log_view,
2402 err = set_selected_commit(s, entry);
2406 s->first_displayed_line = 1;
2407 s->last_displayed_line = view->nlines;
2409 diff_view_indicate_progress(view);
2410 err = create_diff(s);
2414 if (s->log_view == NULL)
2416 ls = &s->log_view->state.log;
2418 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2419 ls->thread_args.commits_needed++;
2421 /* Display "loading..." in log view. */
2422 show_log_view(s->log_view);
2426 err = trigger_log_thread(1 /* load_all */,
2427 &ls->thread_args.commits_needed,
2428 &ls->thread_args.log_complete,
2429 &ls->thread_args.need_commits);
2433 err = input_log_view(NULL, NULL, NULL, s->log_view,
2438 entry = TAILQ_NEXT(ls->selected_entry, entry);
2442 err = set_selected_commit(s, entry);
2446 s->first_displayed_line = 1;
2447 s->last_displayed_line = view->nlines;
2449 diff_view_indicate_progress(view);
2450 err = create_diff(s);
2459 static const struct got_error *
2460 cmd_diff(int argc, char *argv[])
2462 const struct got_error *error = NULL;
2463 struct got_repository *repo = NULL;
2464 struct got_reflist_head refs;
2465 struct got_object_id *id1 = NULL, *id2 = NULL;
2466 char *repo_path = NULL;
2467 char *id_str1 = NULL, *id_str2 = NULL;
2469 struct tog_view *view;
2471 SIMPLEQ_INIT(&refs);
2474 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2479 while ((ch = getopt(argc, argv, "")) != -1) {
2491 usage_diff(); /* TODO show local worktree changes */
2492 } else if (argc == 2) {
2493 repo_path = getcwd(NULL, 0);
2494 if (repo_path == NULL)
2495 return got_error_from_errno("getcwd");
2498 } else if (argc == 3) {
2499 repo_path = realpath(argv[0], NULL);
2500 if (repo_path == NULL)
2501 return got_error_from_errno2("realpath", argv[0]);
2509 error = got_repo_open(&repo, repo_path);
2513 error = apply_unveil(got_repo_get_path(repo), NULL);
2517 error = got_object_resolve_id_str(&id1, repo, id_str1);
2521 error = got_object_resolve_id_str(&id2, repo, id_str2);
2525 error = got_ref_list(&refs, repo);
2529 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2531 error = got_error_from_errno("view_open");
2534 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2537 error = view_loop(view);
2540 got_repo_close(repo);
2541 got_ref_list_free(&refs);
2549 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2554 struct tog_blame_line {
2556 struct got_object_id *id;
2559 static const struct got_error *
2560 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2561 const char *path, struct tog_blame_line *lines, int nlines,
2562 int blame_complete, int selected_line, int *first_displayed_line,
2563 int *last_displayed_line, int *eof, int max_lines)
2565 const struct got_error *err;
2566 int lineno = 0, nprinted = 0;
2571 struct tog_blame_line *blame_line;
2572 struct got_object_id *prev_id = NULL;
2575 err = got_object_id_str(&id_str, id);
2580 werase(view->window);
2582 if (asprintf(&line, "commit %s", id_str) == -1) {
2583 err = got_error_from_errno("asprintf");
2588 err = format_line(&wline, &width, line, view->ncols);
2591 if (view_needs_focus_indication(view))
2592 wstandout(view->window);
2593 waddwstr(view->window, wline);
2594 if (view_needs_focus_indication(view))
2595 wstandend(view->window);
2598 if (width < view->ncols)
2599 waddch(view->window, '\n');
2601 if (asprintf(&line, "[%d/%d] %s%s",
2602 *first_displayed_line - 1 + selected_line, nlines,
2603 blame_complete ? "" : "annotating... ", path) == -1) {
2605 return got_error_from_errno("asprintf");
2608 err = format_line(&wline, &width, line, view->ncols);
2613 waddwstr(view->window, wline);
2616 if (width < view->ncols)
2617 waddch(view->window, '\n');
2620 while (nprinted < max_lines - 2) {
2621 line = parse_next_line(f, &len);
2626 if (++lineno < *first_displayed_line) {
2631 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2632 err = format_line(&wline, &width, line, wlimit);
2638 if (view->focussed && nprinted == selected_line - 1)
2639 wstandout(view->window);
2641 blame_line = &lines[lineno - 1];
2642 if (blame_line->annotated && prev_id &&
2643 got_object_id_cmp(prev_id, blame_line->id) == 0)
2644 waddstr(view->window, " ");
2645 else if (blame_line->annotated) {
2647 err = got_object_id_str(&id_str, blame_line->id);
2653 wprintw(view->window, "%.8s ", id_str);
2655 prev_id = blame_line->id;
2657 waddstr(view->window, "........ ");
2661 waddwstr(view->window, wline);
2662 while (width < wlimit) {
2663 waddch(view->window, ' ');
2666 if (view->focussed && nprinted == selected_line - 1)
2667 wstandend(view->window);
2668 if (++nprinted == 1)
2669 *first_displayed_line = lineno;
2674 *last_displayed_line = lineno;
2681 static const struct got_error *
2682 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2684 const struct got_error *err = NULL;
2685 struct tog_blame_cb_args *a = arg;
2686 struct tog_blame_line *line;
2689 if (nlines != a->nlines ||
2690 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2691 return got_error(GOT_ERR_RANGE);
2693 errcode = pthread_mutex_lock(&tog_mutex);
2695 return got_error_set_errno(errcode, "pthread_mutex_lock");
2697 if (*a->quit) { /* user has quit the blame view */
2698 err = got_error(GOT_ERR_ITER_COMPLETED);
2703 goto done; /* no change in this commit */
2705 line = &a->lines[lineno - 1];
2706 if (line->annotated)
2709 line->id = got_object_id_dup(id);
2710 if (line->id == NULL) {
2711 err = got_error_from_errno("got_object_id_dup");
2714 line->annotated = 1;
2716 errcode = pthread_mutex_unlock(&tog_mutex);
2718 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2723 blame_thread(void *arg)
2725 const struct got_error *err;
2726 struct tog_blame_thread_args *ta = arg;
2727 struct tog_blame_cb_args *a = ta->cb_args;
2730 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2731 blame_cb, ta->cb_args);
2733 errcode = pthread_mutex_lock(&tog_mutex);
2735 return (void *)got_error_set_errno(errcode,
2736 "pthread_mutex_lock");
2738 got_repo_close(ta->repo);
2742 errcode = pthread_mutex_unlock(&tog_mutex);
2743 if (errcode && err == NULL)
2744 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2749 static struct got_object_id *
2750 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2753 struct tog_blame_line *line;
2755 line = &lines[first_displayed_line - 1 + selected_line - 1];
2756 if (!line->annotated)
2762 static const struct got_error *
2763 stop_blame(struct tog_blame *blame)
2765 const struct got_error *err = NULL;
2768 if (blame->thread) {
2770 errcode = pthread_mutex_unlock(&tog_mutex);
2772 return got_error_set_errno(errcode,
2773 "pthread_mutex_unlock");
2774 errcode = pthread_join(blame->thread, (void **)&err);
2776 return got_error_set_errno(errcode, "pthread_join");
2777 errcode = pthread_mutex_lock(&tog_mutex);
2779 return got_error_set_errno(errcode,
2780 "pthread_mutex_lock");
2781 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2783 blame->thread = NULL;
2785 if (blame->thread_args.repo) {
2786 got_repo_close(blame->thread_args.repo);
2787 blame->thread_args.repo = NULL;
2790 if (fclose(blame->f) != 0 && err == NULL)
2791 err = got_error_from_errno("fclose");
2795 for (i = 0; i < blame->nlines; i++)
2796 free(blame->lines[i].id);
2798 blame->lines = NULL;
2800 free(blame->cb_args.commit_id);
2801 blame->cb_args.commit_id = NULL;
2806 static const struct got_error *
2807 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2808 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2809 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2810 struct got_repository *repo)
2812 const struct got_error *err = NULL;
2813 struct got_blob_object *blob = NULL;
2814 struct got_repository *thread_repo = NULL;
2815 struct got_object_id *obj_id = NULL;
2818 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2822 return got_error(GOT_ERR_NO_OBJ);
2824 err = got_object_get_type(&obj_type, repo, obj_id);
2828 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2829 err = got_error(GOT_ERR_OBJ_TYPE);
2833 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2836 blame->f = got_opentemp();
2837 if (blame->f == NULL) {
2838 err = got_error_from_errno("got_opentemp");
2841 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2846 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2847 if (blame->lines == NULL) {
2848 err = got_error_from_errno("calloc");
2852 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2856 blame->cb_args.view = view;
2857 blame->cb_args.lines = blame->lines;
2858 blame->cb_args.nlines = blame->nlines;
2859 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2860 if (blame->cb_args.commit_id == NULL) {
2861 err = got_error_from_errno("got_object_id_dup");
2864 blame->cb_args.quit = done;
2866 blame->thread_args.path = path;
2867 blame->thread_args.repo = thread_repo;
2868 blame->thread_args.cb_args = &blame->cb_args;
2869 blame->thread_args.complete = blame_complete;
2870 *blame_complete = 0;
2874 got_object_blob_close(blob);
2881 static const struct got_error *
2882 open_blame_view(struct tog_view *view, char *path,
2883 struct got_object_id *commit_id, struct got_reflist_head *refs,
2884 struct got_repository *repo)
2886 const struct got_error *err = NULL;
2887 struct tog_blame_view_state *s = &view->state.blame;
2889 SIMPLEQ_INIT(&s->blamed_commits);
2891 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2895 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2896 s->first_displayed_line = 1;
2897 s->last_displayed_line = view->nlines;
2898 s->selected_line = 1;
2899 s->blame_complete = 0;
2901 if (s->path == NULL)
2902 return got_error_from_errno("open_blame_view");
2905 s->commit_id = commit_id;
2906 memset(&s->blame, 0, sizeof(s->blame));
2908 view->show = show_blame_view;
2909 view->input = input_blame_view;
2910 view->close = close_blame_view;
2912 return run_blame(&s->blame, view, &s->blame_complete,
2913 &s->first_displayed_line, &s->last_displayed_line,
2914 &s->selected_line, &s->done, &s->eof, s->path,
2915 s->blamed_commit->id, s->repo);
2918 static const struct got_error *
2919 close_blame_view(struct tog_view *view)
2921 const struct got_error *err = NULL;
2922 struct tog_blame_view_state *s = &view->state.blame;
2924 if (s->blame.thread)
2925 err = stop_blame(&s->blame);
2927 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2928 struct got_object_qid *blamed_commit;
2929 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2930 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2931 got_object_qid_free(blamed_commit);
2939 static const struct got_error *
2940 show_blame_view(struct tog_view *view)
2942 const struct got_error *err = NULL;
2943 struct tog_blame_view_state *s = &view->state.blame;
2946 if (s->blame.thread == NULL) {
2947 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2948 &s->blame.thread_args);
2950 return got_error_set_errno(errcode, "pthread_create");
2953 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2954 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2955 s->selected_line, &s->first_displayed_line,
2956 &s->last_displayed_line, &s->eof, view->nlines);
2962 static const struct got_error *
2963 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2964 struct tog_view **focus_view, struct tog_view *view, int ch)
2966 const struct got_error *err = NULL, *thread_err = NULL;
2967 struct tog_view *diff_view;
2968 struct tog_blame_view_state *s = &view->state.blame;
2977 if (s->selected_line > 1)
2979 else if (s->selected_line == 1 &&
2980 s->first_displayed_line > 1)
2981 s->first_displayed_line--;
2984 if (s->first_displayed_line == 1) {
2985 s->selected_line = 1;
2988 if (s->first_displayed_line > view->nlines - 2)
2989 s->first_displayed_line -=
2992 s->first_displayed_line = 1;
2996 if (s->selected_line < view->nlines - 2 &&
2997 s->first_displayed_line +
2998 s->selected_line <= s->blame.nlines)
3000 else if (s->last_displayed_line <
3002 s->first_displayed_line++;
3006 struct got_object_id *id = NULL;
3007 id = get_selected_commit_id(s->blame.lines,
3008 s->first_displayed_line, s->selected_line);
3012 struct got_commit_object *commit;
3013 struct got_object_qid *pid;
3014 struct got_object_id *blob_id = NULL;
3016 err = got_object_open_as_commit(&commit,
3020 pid = SIMPLEQ_FIRST(
3021 got_object_commit_get_parent_ids(commit));
3023 got_object_commit_close(commit);
3026 /* Check if path history ends here. */
3027 err = got_object_id_by_path(&blob_id, s->repo,
3030 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3032 got_object_commit_close(commit);
3035 err = got_object_get_type(&obj_type, s->repo,
3038 /* Can't blame non-blob type objects. */
3039 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 got_object_commit_close(commit);
3043 err = got_object_qid_alloc(&s->blamed_commit,
3045 got_object_commit_close(commit);
3047 if (got_object_id_cmp(id,
3048 s->blamed_commit->id) == 0)
3050 err = got_object_qid_alloc(&s->blamed_commit,
3056 thread_err = stop_blame(&s->blame);
3060 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3061 s->blamed_commit, entry);
3062 err = run_blame(&s->blame, view, &s->blame_complete,
3063 &s->first_displayed_line, &s->last_displayed_line,
3064 &s->selected_line, &s->done, &s->eof,
3065 s->path, s->blamed_commit->id, s->repo);
3071 struct got_object_qid *first;
3072 first = SIMPLEQ_FIRST(&s->blamed_commits);
3073 if (!got_object_id_cmp(first->id, s->commit_id))
3076 thread_err = stop_blame(&s->blame);
3080 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3081 got_object_qid_free(s->blamed_commit);
3083 SIMPLEQ_FIRST(&s->blamed_commits);
3084 err = run_blame(&s->blame, view, &s->blame_complete,
3085 &s->first_displayed_line, &s->last_displayed_line,
3086 &s->selected_line, &s->done, &s->eof, s->path,
3087 s->blamed_commit->id, s->repo);
3094 struct got_object_id *id = NULL;
3095 struct got_object_qid *pid;
3096 struct got_commit_object *commit = NULL;
3097 id = get_selected_commit_id(s->blame.lines,
3098 s->first_displayed_line, s->selected_line);
3101 err = got_object_open_as_commit(&commit, s->repo, id);
3104 pid = SIMPLEQ_FIRST(
3105 got_object_commit_get_parent_ids(commit));
3106 if (view_is_parent_view(view))
3107 begin_x = view_split_begin_x(view->begin_x);
3108 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3109 if (diff_view == NULL) {
3110 got_object_commit_close(commit);
3111 err = got_error_from_errno("view_open");
3114 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3115 id, NULL, s->refs, s->repo);
3116 got_object_commit_close(commit);
3118 view_close(diff_view);
3121 if (view_is_parent_view(view)) {
3122 err = view_close_child(view);
3125 err = view_set_child(view, diff_view);
3127 view_close(diff_view);
3130 *focus_view = diff_view;
3131 view->child_focussed = 1;
3133 *new_view = diff_view;
3140 if (s->last_displayed_line >= s->blame.nlines &&
3141 s->selected_line >= MIN(s->blame.nlines,
3142 view->nlines - 2)) {
3145 if (s->last_displayed_line >= s->blame.nlines &&
3146 s->selected_line < view->nlines - 2) {
3147 s->selected_line = MIN(s->blame.nlines,
3151 if (s->last_displayed_line + view->nlines - 2
3153 s->first_displayed_line +=
3156 s->first_displayed_line =
3161 if (s->selected_line > view->nlines - 2) {
3162 s->selected_line = MIN(s->blame.nlines,
3169 return thread_err ? thread_err : err;
3172 static const struct got_error *
3173 cmd_blame(int argc, char *argv[])
3175 const struct got_error *error;
3176 struct got_repository *repo = NULL;
3177 struct got_reflist_head refs;
3178 struct got_worktree *worktree = NULL;
3179 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3180 struct got_object_id *commit_id = NULL;
3181 char *commit_id_str = NULL;
3183 struct tog_view *view;
3185 SIMPLEQ_INIT(&refs);
3188 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3193 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3196 commit_id_str = optarg;
3199 repo_path = realpath(optarg, NULL);
3200 if (repo_path == NULL)
3201 err(1, "-r option");
3217 cwd = getcwd(NULL, 0);
3219 error = got_error_from_errno("getcwd");
3222 if (repo_path == NULL) {
3223 error = got_worktree_open(&worktree, cwd);
3224 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3230 strdup(got_worktree_get_repo_path(worktree));
3231 if (repo_path == NULL)
3232 error = got_error_from_errno("strdup");
3236 repo_path = strdup(cwd);
3237 if (repo_path == NULL) {
3238 error = got_error_from_errno("strdup");
3246 error = got_repo_open(&repo, repo_path);
3250 error = apply_unveil(got_repo_get_path(repo), NULL);
3255 const char *prefix = got_worktree_get_path_prefix(worktree);
3256 char *p, *worktree_subdir = cwd +
3257 strlen(got_worktree_get_root_path(worktree));
3258 if (asprintf(&p, "%s%s%s%s%s",
3259 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3260 worktree_subdir, worktree_subdir[0] ? "/" : "",
3262 error = got_error_from_errno("asprintf");
3265 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3268 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3273 if (commit_id_str == NULL) {
3274 struct got_reference *head_ref;
3275 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3278 error = got_ref_resolve(&commit_id, repo, head_ref);
3279 got_ref_close(head_ref);
3281 error = got_object_resolve_id_str(&commit_id, repo,
3287 error = got_ref_list(&refs, repo);
3291 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3293 error = got_error_from_errno("view_open");
3296 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3299 error = view_loop(view);
3305 got_worktree_close(worktree);
3307 got_repo_close(repo);
3308 got_ref_list_free(&refs);
3312 static const struct got_error *
3313 draw_tree_entries(struct tog_view *view,
3314 struct got_tree_entry **first_displayed_entry,
3315 struct got_tree_entry **last_displayed_entry,
3316 struct got_tree_entry **selected_entry, int *ndisplayed,
3317 const char *label, int show_ids, const char *parent_path,
3318 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3320 const struct got_error *err = NULL;
3321 struct got_tree_entry *te;
3327 werase(view->window);
3332 err = format_line(&wline, &width, label, view->ncols);
3335 if (view_needs_focus_indication(view))
3336 wstandout(view->window);
3337 waddwstr(view->window, wline);
3338 if (view_needs_focus_indication(view))
3339 wstandend(view->window);
3342 if (width < view->ncols)
3343 waddch(view->window, '\n');
3346 err = format_line(&wline, &width, parent_path, view->ncols);
3349 waddwstr(view->window, wline);
3352 if (width < view->ncols)
3353 waddch(view->window, '\n');
3356 waddch(view->window, '\n');
3360 te = SIMPLEQ_FIRST(&entries->head);
3361 if (*first_displayed_entry == NULL) {
3362 if (selected == 0) {
3364 wstandout(view->window);
3365 *selected_entry = NULL;
3367 waddstr(view->window, " ..\n"); /* parent directory */
3368 if (selected == 0 && view->focussed)
3369 wstandend(view->window);
3376 while (te != *first_displayed_entry)
3377 te = SIMPLEQ_NEXT(te, entry);
3381 char *line = NULL, *id_str = NULL;
3384 err = got_object_id_str(&id_str, te->id);
3386 return got_error_from_errno(
3387 "got_object_id_str");
3389 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3390 te->name, S_ISDIR(te->mode) ? "/" :
3391 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3393 return got_error_from_errno("asprintf");
3396 err = format_line(&wline, &width, line, view->ncols);
3401 if (n == selected) {
3403 wstandout(view->window);
3404 *selected_entry = te;
3406 waddwstr(view->window, wline);
3407 if (width < view->ncols)
3408 waddch(view->window, '\n');
3409 if (n == selected && view->focussed)
3410 wstandend(view->window);
3416 *last_displayed_entry = te;
3419 te = SIMPLEQ_NEXT(te, entry);
3426 tree_scroll_up(struct tog_view *view,
3427 struct got_tree_entry **first_displayed_entry, int maxscroll,
3428 const struct got_tree_entries *entries, int isroot)
3430 struct got_tree_entry *te, *prev;
3433 if (*first_displayed_entry == NULL)
3436 te = SIMPLEQ_FIRST(&entries->head);
3437 if (*first_displayed_entry == te) {
3439 *first_displayed_entry = NULL;
3443 /* XXX this is stupid... switch to TAILQ? */
3444 for (i = 0; i < maxscroll; i++) {
3445 while (te != *first_displayed_entry) {
3447 te = SIMPLEQ_NEXT(te, entry);
3449 *first_displayed_entry = prev;
3450 te = SIMPLEQ_FIRST(&entries->head);
3452 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3453 *first_displayed_entry = NULL;
3457 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3458 struct got_tree_entry *last_displayed_entry,
3459 const struct got_tree_entries *entries)
3461 struct got_tree_entry *next, *last;
3464 if (*first_displayed_entry)
3465 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3467 next = SIMPLEQ_FIRST(&entries->head);
3468 last = last_displayed_entry;
3469 while (next && last && n++ < maxscroll) {
3470 last = SIMPLEQ_NEXT(last, entry);
3472 *first_displayed_entry = next;
3473 next = SIMPLEQ_NEXT(next, entry);
3479 static const struct got_error *
3480 tree_entry_path(char **path, struct tog_parent_trees *parents,
3481 struct got_tree_entry *te)
3483 const struct got_error *err = NULL;
3484 struct tog_parent_tree *pt;
3485 size_t len = 2; /* for leading slash and NUL */
3487 TAILQ_FOREACH(pt, parents, entry)
3488 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3490 len += strlen(te->name);
3492 *path = calloc(1, len);
3494 return got_error_from_errno("calloc");
3497 pt = TAILQ_LAST(parents, tog_parent_trees);
3499 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3500 err = got_error(GOT_ERR_NO_SPACE);
3503 if (strlcat(*path, "/", len) >= len) {
3504 err = got_error(GOT_ERR_NO_SPACE);
3507 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3510 if (strlcat(*path, te->name, len) >= len) {
3511 err = got_error(GOT_ERR_NO_SPACE);
3523 static const struct got_error *
3524 blame_tree_entry(struct tog_view **new_view, int begin_x,
3525 struct got_tree_entry *te, struct tog_parent_trees *parents,
3526 struct got_object_id *commit_id, struct got_reflist_head *refs,
3527 struct got_repository *repo)
3529 const struct got_error *err = NULL;
3531 struct tog_view *blame_view;
3533 err = tree_entry_path(&path, parents, te);
3537 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3538 if (blame_view == NULL)
3539 return got_error_from_errno("view_open");
3541 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3543 view_close(blame_view);
3546 *new_view = blame_view;
3550 static const struct got_error *
3551 log_tree_entry(struct tog_view **new_view, int begin_x,
3552 struct got_tree_entry *te, struct tog_parent_trees *parents,
3553 struct got_object_id *commit_id, struct got_reflist_head *refs,
3554 struct got_repository *repo)
3556 struct tog_view *log_view;
3557 const struct got_error *err = NULL;
3560 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3561 if (log_view == NULL)
3562 return got_error_from_errno("view_open");
3564 err = tree_entry_path(&path, parents, te);
3568 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3570 view_close(log_view);
3572 *new_view = log_view;
3577 static const struct got_error *
3578 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3579 struct got_object_id *commit_id, struct got_reflist_head *refs,
3580 struct got_repository *repo)
3582 const struct got_error *err = NULL;
3583 char *commit_id_str = NULL;
3584 struct tog_tree_view_state *s = &view->state.tree;
3586 TAILQ_INIT(&s->parents);
3588 err = got_object_id_str(&commit_id_str, commit_id);
3592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3593 err = got_error_from_errno("asprintf");
3597 s->root = s->tree = root;
3598 s->entries = got_object_tree_get_entries(root);
3599 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3600 s->commit_id = got_object_id_dup(commit_id);
3601 if (s->commit_id == NULL) {
3602 err = got_error_from_errno("got_object_id_dup");
3608 view->show = show_tree_view;
3609 view->input = input_tree_view;
3610 view->close = close_tree_view;
3612 free(commit_id_str);
3614 free(s->tree_label);
3615 s->tree_label = NULL;
3620 static const struct got_error *
3621 close_tree_view(struct tog_view *view)
3623 struct tog_tree_view_state *s = &view->state.tree;
3625 free(s->tree_label);
3626 s->tree_label = NULL;
3628 s->commit_id = NULL;
3629 while (!TAILQ_EMPTY(&s->parents)) {
3630 struct tog_parent_tree *parent;
3631 parent = TAILQ_FIRST(&s->parents);
3632 TAILQ_REMOVE(&s->parents, parent, entry);
3636 if (s->tree != s->root)
3637 got_object_tree_close(s->tree);
3638 got_object_tree_close(s->root);
3643 static const struct got_error *
3644 show_tree_view(struct tog_view *view)
3646 const struct got_error *err = NULL;
3647 struct tog_tree_view_state *s = &view->state.tree;
3650 err = tree_entry_path(&parent_path, &s->parents, NULL);
3654 err = draw_tree_entries(view, &s->first_displayed_entry,
3655 &s->last_displayed_entry, &s->selected_entry,
3656 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3657 s->entries, s->selected, view->nlines, s->tree == s->root);
3664 static const struct got_error *
3665 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3666 struct tog_view **focus_view, struct tog_view *view, int ch)
3668 const struct got_error *err = NULL;
3669 struct tog_tree_view_state *s = &view->state.tree;
3670 struct tog_view *log_view;
3671 int begin_x = 0, nscrolled;
3675 s->show_ids = !s->show_ids;
3678 if (!s->selected_entry)
3680 if (view_is_parent_view(view))
3681 begin_x = view_split_begin_x(view->begin_x);
3682 err = log_tree_entry(&log_view, begin_x,
3683 s->selected_entry, &s->parents,
3684 s->commit_id, s->refs, s->repo);
3685 if (view_is_parent_view(view)) {
3686 err = view_close_child(view);
3689 err = view_set_child(view, log_view);
3691 view_close(log_view);
3694 *focus_view = log_view;
3695 view->child_focussed = 1;
3697 *new_view = log_view;
3701 if (s->selected > 0) {
3703 if (s->selected == 0)
3706 if (s->selected > 0)
3708 tree_scroll_up(view, &s->first_displayed_entry, 1,
3709 s->entries, s->tree == s->root);
3712 tree_scroll_up(view, &s->first_displayed_entry,
3713 MAX(0, view->nlines - 4 - s->selected), s->entries,
3714 s->tree == s->root);
3716 if (SIMPLEQ_FIRST(&s->entries->head) ==
3717 s->first_displayed_entry && s->tree != s->root)
3718 s->first_displayed_entry = NULL;
3722 if (s->selected < s->ndisplayed - 1) {
3726 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3727 /* can't scroll any further */
3729 tree_scroll_down(&s->first_displayed_entry, 1,
3730 s->last_displayed_entry, s->entries);
3733 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3735 /* can't scroll any further; move cursor down */
3736 if (s->selected < s->ndisplayed - 1)
3737 s->selected = s->ndisplayed - 1;
3740 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3741 view->nlines, s->last_displayed_entry, s->entries);
3742 if (nscrolled < view->nlines) {
3744 struct got_tree_entry *te;
3745 te = s->first_displayed_entry;
3748 te = SIMPLEQ_NEXT(te, entry);
3750 s->selected = ndisplayed - 1;
3756 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3757 struct tog_parent_tree *parent;
3758 /* user selected '..' */
3759 if (s->tree == s->root)
3761 parent = TAILQ_FIRST(&s->parents);
3762 TAILQ_REMOVE(&s->parents, parent,
3764 got_object_tree_close(s->tree);
3765 s->tree = parent->tree;
3767 got_object_tree_get_entries(s->tree);
3768 s->first_displayed_entry =
3769 parent->first_displayed_entry;
3771 parent->selected_entry;
3772 s->selected = parent->selected;
3774 } else if (S_ISDIR(s->selected_entry->mode)) {
3775 struct tog_parent_tree *parent;
3776 struct got_tree_object *child;
3777 err = got_object_open_as_tree(&child,
3778 s->repo, s->selected_entry->id);
3781 parent = calloc(1, sizeof(*parent));
3782 if (parent == NULL) {
3783 err = got_error_from_errno("calloc");
3786 parent->tree = s->tree;
3787 parent->first_displayed_entry =
3788 s->first_displayed_entry;
3789 parent->selected_entry = s->selected_entry;
3790 parent->selected = s->selected;
3791 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3794 got_object_tree_get_entries(s->tree);
3796 s->first_displayed_entry = NULL;
3797 } else if (S_ISREG(s->selected_entry->mode)) {
3798 struct tog_view *blame_view;
3799 int begin_x = view_is_parent_view(view) ?
3800 view_split_begin_x(view->begin_x) : 0;
3802 err = blame_tree_entry(&blame_view, begin_x,
3803 s->selected_entry, &s->parents,
3804 s->commit_id, s->refs, s->repo);
3807 if (view_is_parent_view(view)) {
3808 err = view_close_child(view);
3811 err = view_set_child(view, blame_view);
3813 view_close(blame_view);
3816 *focus_view = blame_view;
3817 view->child_focussed = 1;
3819 *new_view = blame_view;
3823 if (s->selected > view->nlines)
3824 s->selected = s->ndisplayed - 1;
3837 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3842 static const struct got_error *
3843 cmd_tree(int argc, char *argv[])
3845 const struct got_error *error;
3846 struct got_repository *repo = NULL;
3847 struct got_reflist_head refs;
3848 char *repo_path = NULL;
3849 struct got_object_id *commit_id = NULL;
3850 char *commit_id_arg = NULL;
3851 struct got_commit_object *commit = NULL;
3852 struct got_tree_object *tree = NULL;
3854 struct tog_view *view;
3856 SIMPLEQ_INIT(&refs);
3859 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3864 while ((ch = getopt(argc, argv, "c:")) != -1) {
3867 commit_id_arg = optarg;
3879 struct got_worktree *worktree;
3880 char *cwd = getcwd(NULL, 0);
3882 return got_error_from_errno("getcwd");
3883 error = got_worktree_open(&worktree, cwd);
3884 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3889 strdup(got_worktree_get_repo_path(worktree));
3890 got_worktree_close(worktree);
3893 if (repo_path == NULL) {
3894 error = got_error_from_errno("strdup");
3897 } else if (argc == 1) {
3898 repo_path = realpath(argv[0], NULL);
3899 if (repo_path == NULL)
3900 return got_error_from_errno2("realpath", argv[0]);
3906 error = got_repo_open(&repo, repo_path);
3910 error = apply_unveil(got_repo_get_path(repo), NULL);
3914 if (commit_id_arg == NULL)
3915 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
3917 error = got_object_resolve_id_str(&commit_id, repo,
3922 error = got_object_open_as_commit(&commit, repo, commit_id);
3926 error = got_object_open_as_tree(&tree, repo,
3927 got_object_commit_get_tree_id(commit));
3931 error = got_ref_list(&refs, repo);
3935 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3937 error = got_error_from_errno("view_open");
3940 error = open_tree_view(view, tree, commit_id, &refs, repo);
3943 error = view_loop(view);
3948 got_object_commit_close(commit);
3950 got_object_tree_close(tree);
3952 got_repo_close(repo);
3953 got_ref_list_free(&refs);
3962 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3963 "Available commands:\n", getprogname());
3964 for (i = 0; i < nitems(tog_commands); i++) {
3965 struct tog_cmd *cmd = &tog_commands[i];
3966 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3972 make_argv(const char *arg0, const char *arg1)
3975 int argc = (arg1 == NULL ? 1 : 2);
3977 argv = calloc(argc, sizeof(char *));
3980 argv[0] = strdup(arg0);
3981 if (argv[0] == NULL)
3984 argv[1] = strdup(arg1);
3985 if (argv[1] == NULL)
3993 main(int argc, char *argv[])
3995 const struct got_error *error = NULL;
3996 struct tog_cmd *cmd = NULL;
3998 char **cmd_argv = NULL;
4000 setlocale(LC_CTYPE, "");
4002 while ((ch = getopt(argc, argv, "h")) != -1) {
4021 /* Build an argument vector which runs a default command. */
4022 cmd = &tog_commands[0];
4023 cmd_argv = make_argv(cmd->name, NULL);
4028 /* Did the user specific a command? */
4029 for (i = 0; i < nitems(tog_commands); i++) {
4030 if (strncmp(tog_commands[i].name, argv[0],
4031 strlen(argv[0])) == 0) {
4032 cmd = &tog_commands[i];
4034 tog_commands[i].cmd_usage();
4039 /* Did the user specify a repository? */
4040 char *repo_path = realpath(argv[0], NULL);
4042 struct got_repository *repo;
4043 error = got_repo_open(&repo, repo_path);
4045 got_repo_close(repo);
4047 error = got_error_from_errno2("realpath",
4051 fprintf(stderr, "%s: '%s' is not a "
4052 "known command\n", getprogname(),
4056 fprintf(stderr, "%s: '%s' is neither a known "
4057 "command nor a path to a repository\n",
4058 getprogname(), argv[0]);
4062 cmd = &tog_commands[0];
4063 cmd_argv = make_argv(cmd->name, repo_path);
4069 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4076 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);