2 * Copyright (c) 2018 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>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
46 #include "got_blame.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
92 struct tog_diff_view_state {
93 struct got_object_id *id1, *id2;
95 int first_displayed_line;
96 int last_displayed_line;
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
108 struct commit_queue_head head;
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
119 struct got_repository *repo;
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
127 struct tog_view *view;
128 struct got_object_id *commit_id;
131 int *first_displayed_line;
132 int *last_displayed_line;
138 struct tog_blame_thread_args {
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
148 struct tog_blame_line *lines;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
196 TAILQ_HEAD(tog_view_list_head, tog_view);
198 TAILQ_ENTRY(tog_view) entry;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
221 const struct got_error *(*close)(struct tog_view *);
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
260 view->child->parent = NULL;
262 view->parent->child = NULL;
264 err = view->close(view);
266 del_panel(view->panel);
268 delwin(view->window);
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
287 parent->child = view;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
306 keypad(view->window, TRUE);
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
316 err = view->parent->show(view->parent);
319 show_panel(view->parent->panel);
322 err = view->show(view);
325 show_panel(view->panel);
327 if (view->child && view->child->begin_x > view->begin_x) {
328 err = view->child->show(view->child);
331 show_panel(view->child->panel);
340 static const struct got_error *
341 view_resize(struct tog_view *view)
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
390 next = TAILQ_NEXT(view, entry);
394 *focus = TAILQ_FIRST(views);
396 (*focus)->focussed = 1;
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
403 *focus = TAILQ_LAST(views, tog_view_list_head);
405 (*focus)->focussed = 1;
408 err = view->input(new, dead, view, ch);
415 err = view_resize(view);
418 err = view->input(new, dead, view, ch);
421 err = view->input(new, dead, view, ch);
428 static const struct got_error *
429 view_set_child(struct tog_view *view, struct tog_view *child)
431 const struct got_error *err;
433 if (view->set_child) {
434 err = view->set_child(view, child);
444 view_vborder(struct tog_view *view)
446 if (view->child == NULL)
449 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
454 view_needs_focus_indication(struct tog_view *view)
459 if (view->child && view->child->begin_x > view->begin_x)
462 if (view->parent && view->begin_x > view->parent->begin_x)
468 static const struct got_error *
469 view_loop(struct tog_view *view)
471 const struct got_error *err = NULL;
472 struct tog_view_list_head views;
473 struct tog_view *new_view, *dead_view;
477 TAILQ_INSERT_HEAD(&views, view, entry);
480 while (!TAILQ_EMPTY(&views) && !done) {
481 err = view_show(view);
484 err = view_input(&new_view, &dead_view, &view, &done,
489 struct tog_view *v, *t;
490 TAILQ_REMOVE(&views, dead_view, entry);
491 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 if (v->parent == dead_view) {
493 TAILQ_REMOVE(&views, v, entry);
499 if (dead_view->parent)
500 view = dead_view->parent;
502 view = TAILQ_LAST(&views, tog_view_list_head);
505 err = view_close(dead_view);
511 /* TODO: de-duplicate! */
512 TAILQ_INSERT_TAIL(&views, new_view, entry);
513 if (new_view->parent) {
514 err = view_set_child(new_view->parent, new_view);
517 new_view->parent->focussed = 0;
524 while (!TAILQ_EMPTY(&views)) {
525 view = TAILQ_FIRST(&views);
526 TAILQ_REMOVE(&views, view, entry);
537 "usage: %s log [-c commit] [-r repository-path] [path]\n",
542 /* Create newly allocated wide-character string equivalent to a byte string. */
543 static const struct got_error *
544 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
547 const struct got_error *err = NULL;
550 *wlen = mbstowcs(NULL, s, 0);
551 if (*wlen == (size_t)-1) {
554 return got_error_from_errno();
556 /* byte string invalid in current encoding; try to "fix" it */
557 err = got_mbsavis(&vis, &vislen, s);
560 *wlen = mbstowcs(NULL, vis, 0);
561 if (*wlen == (size_t)-1) {
562 err = got_error_from_errno(); /* give up */
567 *ws = calloc(*wlen + 1, sizeof(*ws));
569 err = got_error_from_errno();
573 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 err = got_error_from_errno();
585 /* Format a line for display, ensuring that it won't overflow a width limit. */
586 static const struct got_error *
587 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 const struct got_error *err = NULL;
591 wchar_t *wline = NULL;
598 err = mbs2ws(&wline, &wlen, line);
603 while (i < wlen && cols < wlimit) {
604 int width = wcwidth(wline[i]);
611 if (cols + width <= wlimit)
616 if (wline[i] == L'\t')
617 cols += TABSIZE - ((cols + 1) % TABSIZE);
621 err = got_error_from_errno();
636 static const struct got_error *
637 draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 struct got_object_id *id)
640 const struct got_error *err = NULL;
641 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 char *logmsg0 = NULL, *logmsg = NULL;
643 char *author0 = NULL, *author = NULL;
644 wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 int author_width, logmsg_width;
646 char *newline, *smallerthan;
649 static const size_t date_display_cols = 9;
650 static const size_t author_display_cols = 16;
651 const int avail = view->ncols;
653 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 &commit->tm_committer) >= sizeof(datebuf))
655 return got_error(GOT_ERR_NO_SPACE);
657 if (avail < date_display_cols)
658 limit = MIN(sizeof(datebuf) - 1, avail);
660 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 waddnstr(view->window, datebuf, limit);
666 author0 = strdup(commit->author);
667 if (author0 == NULL) {
668 err = got_error_from_errno();
672 smallerthan = strchr(author, '<');
676 char *at = strchr(author, '@');
681 err = format_line(&wauthor, &author_width, author, limit);
684 waddwstr(view->window, wauthor);
686 while (col <= avail && author_width < author_display_cols + 1) {
687 waddch(view->window, ' ');
694 logmsg0 = strdup(commit->logmsg);
695 if (logmsg0 == NULL) {
696 err = got_error_from_errno();
700 while (*logmsg == '\n')
702 newline = strchr(logmsg, '\n');
706 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
709 waddwstr(view->window, wlogmsg);
711 while (col <= avail) {
712 waddch(view->window, ' ');
724 static struct commit_queue_entry *
725 alloc_commit_queue_entry(struct got_commit_object *commit,
726 struct got_object_id *id)
728 struct commit_queue_entry *entry;
730 entry = calloc(1, sizeof(*entry));
735 entry->commit = commit;
740 pop_commit(struct commit_queue *commits)
742 struct commit_queue_entry *entry;
744 entry = TAILQ_FIRST(&commits->head);
745 TAILQ_REMOVE(&commits->head, entry, entry);
746 got_object_commit_close(entry->commit);
748 /* Don't free entry->id! It is owned by the commit graph. */
753 free_commits(struct commit_queue *commits)
755 while (!TAILQ_EMPTY(&commits->head))
759 static const struct got_error *
760 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 struct got_object_id *start_id, int minqueue, int init,
762 struct got_repository *repo, const char *path)
764 const struct got_error *err = NULL;
765 struct got_object_id *id;
766 struct commit_queue_entry *entry;
767 int nfetched, nqueued = 0, found_obj = 0;
768 int is_root_path = strcmp(path, "/") == 0;
770 err = got_commit_graph_iter_start(graph, start_id);
774 entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
778 /* Start ID's commit is already on the queue; skip over it. */
779 err = got_commit_graph_iter_next(&id, graph);
780 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
783 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
789 struct got_commit_object *commit;
791 err = got_commit_graph_iter_next(&id, graph);
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
795 if (nqueued >= minqueue) {
799 err = got_commit_graph_fetch_commits(&nfetched,
808 err = got_object_open_as_commit(&commit, repo, id);
813 struct got_object *obj;
814 struct got_object_qid *pid;
817 err = got_object_open_by_path(&obj, repo, id, path);
819 got_object_commit_close(commit);
820 if (err->code == GOT_ERR_NO_OBJ &&
821 (found_obj || !init)) {
822 /* History stops here. */
823 err = got_error(GOT_ERR_ITER_COMPLETED);
829 pid = SIMPLEQ_FIRST(&commit->parent_ids);
831 struct got_object *pobj;
832 err = got_object_open_by_path(&pobj, repo,
835 if (err->code != GOT_ERR_NO_OBJ) {
836 got_object_close(obj);
837 got_object_commit_close(commit);
843 struct got_object_id *id, *pid;
844 id = got_object_get_id(obj);
845 pid = got_object_get_id(pobj);
847 (got_object_id_cmp(id, pid) != 0);
848 got_object_close(pobj);
851 got_object_close(obj);
853 got_object_commit_close(commit);
858 entry = alloc_commit_queue_entry(commit, id);
860 err = got_error_from_errno();
863 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
871 static const struct got_error *
872 fetch_next_commit(struct commit_queue_entry **pentry,
873 struct commit_queue_entry *entry, struct commit_queue *commits,
874 struct got_commit_graph *graph, struct got_repository *repo,
877 const struct got_error *err = NULL;
881 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
885 /* Next entry to display should now be available. */
886 *pentry = TAILQ_NEXT(entry, entry);
888 return got_error(GOT_ERR_NO_OBJ);
893 static const struct got_error *
894 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
896 const struct got_error *err = NULL;
897 struct got_reference *head_ref;
901 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
905 err = got_ref_resolve(head_id, repo, head_ref);
906 got_ref_close(head_ref);
915 static const struct got_error *
916 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
917 struct commit_queue_entry **selected, struct commit_queue_entry *first,
918 struct commit_queue *commits, int selected_idx, int limit,
919 struct got_commit_graph *graph, struct got_repository *repo,
922 const struct got_error *err = NULL;
923 struct commit_queue_entry *entry;
925 char *id_str, *header;
931 if (ncommits == selected_idx) {
935 entry = TAILQ_NEXT(entry, entry);
939 err = got_object_id_str(&id_str, (*selected)->id);
943 if (path && strcmp(path, "/") != 0) {
944 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
945 err = got_error_from_errno();
949 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
950 err = got_error_from_errno();
955 err = format_line(&wline, &width, header, view->ncols);
962 werase(view->window);
964 if (view_needs_focus_indication(view))
965 wstandout(view->window);
966 waddwstr(view->window, wline);
967 if (view_needs_focus_indication(view))
968 wstandend(view->window);
969 if (width < view->ncols)
970 waddch(view->window, '\n');
979 if (ncommits >= limit - 1)
981 if (ncommits == selected_idx)
982 wstandout(view->window);
983 err = draw_commit(view, entry->commit, entry->id);
984 if (ncommits == selected_idx)
985 wstandend(view->window);
990 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
991 err = queue_commits(graph, commits, entry->id, 1,
994 if (err->code != GOT_ERR_ITER_COMPLETED)
999 entry = TAILQ_NEXT(entry, entry);
1008 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1009 struct commit_queue *commits)
1011 struct commit_queue_entry *entry;
1014 entry = TAILQ_FIRST(&commits->head);
1015 if (*first_displayed_entry == entry)
1018 entry = *first_displayed_entry;
1019 while (entry && nscrolled < maxscroll) {
1020 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1022 *first_displayed_entry = entry;
1028 static const struct got_error *
1029 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1030 struct commit_queue_entry *last_displayed_entry,
1031 struct commit_queue *commits, struct got_commit_graph *graph,
1032 struct got_repository *repo, const char *path)
1034 const struct got_error *err = NULL;
1035 struct commit_queue_entry *pentry;
1039 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1040 if (pentry == NULL) {
1041 err = fetch_next_commit(&pentry, last_displayed_entry,
1042 commits, graph, repo, path);
1043 if (err || pentry == NULL)
1046 last_displayed_entry = pentry;
1048 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1051 *first_displayed_entry = pentry;
1052 } while (++nscrolled < maxscroll);
1057 static const struct got_error *
1058 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1059 struct commit_queue_entry *entry, struct got_repository *repo)
1061 const struct got_error *err;
1062 struct got_object *obj1 = NULL, *obj2 = NULL;
1063 struct got_object_qid *parent_id;
1064 struct tog_view *diff_view;
1066 err = got_object_open(&obj2, repo, entry->id);
1070 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1072 err = got_object_open(&obj1, repo, parent_id->id);
1077 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1078 if (diff_view == NULL) {
1079 err = got_error_from_errno();
1083 err = open_diff_view(diff_view, obj1, obj2, repo);
1085 *new_view = diff_view;
1088 got_object_close(obj1);
1090 got_object_close(obj2);
1094 static const struct got_error *
1095 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1096 struct commit_queue_entry *entry, struct got_repository *repo)
1098 const struct got_error *err = NULL;
1099 struct got_tree_object *tree;
1100 struct tog_view *tree_view;
1102 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1106 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1107 if (tree_view == NULL)
1108 return got_error_from_errno();
1110 err = open_tree_view(tree_view, tree, entry->id, repo);
1112 got_object_tree_close(tree);
1114 *new_view = tree_view;
1118 static const struct got_error *
1119 set_child_log_view(struct tog_view *view, struct tog_view *child)
1121 struct tog_log_view_state *s = &view->state.log;
1122 struct tog_diff_view_state *ds;
1123 struct commit_queue_entry *commit, *child_entry = NULL;
1124 int selected_idx = 0;
1126 if (child->type != TOG_VIEW_DIFF)
1128 ds = &child->state.diff;
1130 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1131 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1132 child_entry = commit;
1136 if (child_entry == NULL)
1139 commit = s->first_displayed_entry;
1141 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1142 s->selected_entry = child_entry;
1143 s->selected = selected_idx;
1146 if (commit == s->last_displayed_entry)
1149 commit = TAILQ_NEXT(commit, entry);
1152 return show_log_view(view);
1155 static const struct got_error *
1156 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1157 struct got_repository *repo, const char *path)
1159 const struct got_error *err = NULL;
1160 struct got_object_id *head_id = NULL;
1162 struct tog_log_view_state *s = &view->state.log;
1164 err = got_repo_map_path(&s->in_repo_path, repo, path);
1168 err = get_head_commit_id(&head_id, repo);
1172 /* The graph contains all commits. */
1173 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1176 /* The commit queue contains a subset of commits filtered by path. */
1177 TAILQ_INIT(&s->commits.head);
1178 s->commits.ncommits = 0;
1180 /* Populate commit graph with a sufficient number of commits. */
1181 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1187 * Open the initial batch of commits, sorted in commit graph order.
1188 * We keep all commits open throughout the lifetime of the log view
1189 * in order to avoid having to re-fetch commits from disk while
1190 * updating the display.
1192 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1193 repo, s->in_repo_path);
1195 if (err->code != GOT_ERR_ITER_COMPLETED)
1200 s->first_displayed_entry =
1201 TAILQ_FIRST(&s->commits.head);
1202 s->selected_entry = s->first_displayed_entry;
1205 view->show = show_log_view;
1206 view->input = input_log_view;
1207 view->close = close_log_view;
1208 view->set_child = set_child_log_view;
1214 static const struct got_error *
1215 close_log_view(struct tog_view *view)
1217 struct tog_log_view_state *s = &view->state.log;
1220 got_commit_graph_close(s->graph);
1221 free_commits(&s->commits);
1222 free(s->in_repo_path);
1226 static const struct got_error *
1227 update_diff_child_view(struct tog_view *parent,
1228 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1230 const struct got_error *err = NULL;
1231 struct tog_diff_view_state *ds;
1232 struct got_object *obj1 = NULL, *obj2 = NULL;
1233 struct got_object_qid *parent_id;
1234 struct tog_view *child_view = parent->child;
1236 if (child_view == NULL)
1238 if (child_view->type != TOG_VIEW_DIFF)
1240 ds = &child_view->state.diff;
1241 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1244 err = got_object_open(&obj2, repo, selected_entry->id);
1248 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1250 err = got_object_open(&obj1, repo, parent_id->id);
1255 err = close_diff_view(child_view);
1259 err = open_diff_view(child_view, obj1, obj2, repo);
1264 got_object_close(obj1);
1266 got_object_close(obj2);
1270 static const struct got_error *
1271 show_log_view(struct tog_view *view)
1273 const struct got_error *err = NULL;
1274 struct tog_log_view_state *s = &view->state.log;
1276 err = draw_commits(view, &s->last_displayed_entry,
1277 &s->selected_entry, s->first_displayed_entry,
1278 &s->commits, s->selected, view->nlines, s->graph,
1279 s->repo, s->in_repo_path);
1283 return update_diff_child_view(view, s->selected_entry, s->repo);
1286 static const struct got_error *
1287 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1288 struct tog_view *view, int ch)
1290 const struct got_error *err = NULL;
1291 struct tog_log_view_state *s = &view->state.log;
1297 if (s->selected > 0)
1299 if (s->selected > 0)
1301 scroll_up(&s->first_displayed_entry, 1,
1305 if (TAILQ_FIRST(&s->commits.head) ==
1306 s->first_displayed_entry) {
1310 scroll_up(&s->first_displayed_entry,
1311 view->nlines, &s->commits);
1316 if (s->selected < MIN(view->nlines - 2,
1317 s->commits.ncommits - 1)) {
1321 err = scroll_down(&s->first_displayed_entry, 1,
1322 s->last_displayed_entry, &s->commits,
1323 s->graph, s->repo, s->in_repo_path);
1325 if (err->code != GOT_ERR_ITER_COMPLETED)
1331 struct commit_queue_entry *first;
1332 first = s->first_displayed_entry;
1333 err = scroll_down(&s->first_displayed_entry,
1334 view->nlines, s->last_displayed_entry,
1335 &s->commits, s->graph, s->repo,
1339 if (err->code != GOT_ERR_ITER_COMPLETED)
1341 if (first == s->first_displayed_entry &&
1342 s->selected < MIN(view->nlines - 2,
1343 s->commits.ncommits - 1)) {
1344 /* can't scroll further down */
1345 s->selected = MIN(view->nlines - 2,
1346 s->commits.ncommits - 1);
1352 if (s->selected > view->nlines - 2)
1353 s->selected = view->nlines - 2;
1354 if (s->selected > s->commits.ncommits - 1)
1355 s->selected = s->commits.ncommits - 1;
1359 err = show_commit(new_view, view, s->selected_entry,
1363 err = browse_commit(new_view, view, s->selected_entry,
1373 static const struct got_error *
1374 cmd_log(int argc, char *argv[])
1376 const struct got_error *error;
1377 struct got_repository *repo = NULL;
1378 struct got_object_id *start_id = NULL;
1379 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1380 char *start_commit = NULL;
1382 struct tog_view *view;
1385 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1390 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1393 start_commit = optarg;
1396 repo_path = realpath(optarg, NULL);
1397 if (repo_path == NULL)
1398 err(1, "-r option");
1412 path = strdup(argv[0]);
1416 return got_error_from_errno();
1418 cwd = getcwd(NULL, 0);
1420 error = got_error_from_errno();
1423 if (repo_path == NULL) {
1424 repo_path = strdup(cwd);
1425 if (repo_path == NULL) {
1426 error = got_error_from_errno();
1431 error = got_repo_open(&repo, repo_path);
1435 if (start_commit == NULL) {
1436 error = get_head_commit_id(&start_id, repo);
1440 struct got_object *obj;
1441 error = got_object_open_by_id_str(&obj, repo, start_commit);
1442 if (error == NULL) {
1443 start_id = got_object_id_dup(got_object_get_id(obj));
1444 if (start_id == NULL)
1445 error = got_error_from_errno();
1452 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1454 error = got_error_from_errno();
1457 error = open_log_view(view, start_id, repo, path);
1460 error = view_loop(view);
1467 got_repo_close(repo);
1475 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1481 parse_next_line(FILE *f, size_t *len)
1486 const char delim[3] = { '\0', '\0', '\0'};
1488 line = fparseln(f, &linelen, &lineno, delim, 0);
1494 static const struct got_error *
1495 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1496 int *last_displayed_line, int *eof, int max_lines,
1499 const struct got_error *err;
1500 int nlines = 0, nprinted = 0;
1507 werase(view->window);
1510 err = format_line(&wline, &width, header, view->ncols);
1515 if (view_needs_focus_indication(view))
1516 wstandout(view->window);
1517 waddwstr(view->window, wline);
1518 if (view_needs_focus_indication(view))
1519 wstandend(view->window);
1520 if (width < view->ncols)
1521 waddch(view->window, '\n');
1529 while (nprinted < max_lines) {
1530 line = parse_next_line(f, &len);
1535 if (++nlines < *first_displayed_line) {
1540 err = format_line(&wline, &width, line, view->ncols);
1545 waddwstr(view->window, wline);
1546 if (width < view->ncols)
1547 waddch(view->window, '\n');
1548 if (++nprinted == 1)
1549 *first_displayed_line = nlines;
1554 *last_displayed_line = nlines;
1561 static const struct got_error *
1562 open_diff_view(struct tog_view *view, struct got_object *obj1,
1563 struct got_object *obj2, struct got_repository *repo)
1565 const struct got_error *err;
1568 if (obj1 != NULL && obj2 != NULL &&
1569 got_object_get_type(obj1) != got_object_get_type(obj2))
1570 return got_error(GOT_ERR_OBJ_TYPE);
1574 return got_error_from_errno();
1576 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1577 case GOT_OBJ_TYPE_BLOB:
1578 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1581 case GOT_OBJ_TYPE_TREE:
1582 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1584 case GOT_OBJ_TYPE_COMMIT:
1585 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1588 return got_error(GOT_ERR_OBJ_TYPE);
1593 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1594 view->state.diff.id2 = got_object_get_id(obj2);
1595 view->state.diff.f = f;
1596 view->state.diff.first_displayed_line = 1;
1597 view->state.diff.last_displayed_line = view->nlines;
1599 view->show = show_diff_view;
1600 view->input = input_diff_view;
1601 view->close = close_diff_view;
1606 static const struct got_error *
1607 close_diff_view(struct tog_view *view)
1609 const struct got_error *err = NULL;
1611 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1612 err = got_error_from_errno();
1616 static const struct got_error *
1617 show_diff_view(struct tog_view *view)
1619 const struct got_error *err;
1620 struct tog_diff_view_state *s = &view->state.diff;
1621 char *id_str1 = NULL, *id_str2, *header;
1624 err = got_object_id_str(&id_str1, s->id1);
1628 err = got_object_id_str(&id_str2, s->id2);
1632 if (asprintf(&header, "diff: %s %s",
1633 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1634 err = got_error_from_errno();
1642 return draw_file(view, s->f, &s->first_displayed_line,
1643 &s->last_displayed_line, &s->eof, view->nlines,
1647 static const struct got_error *
1648 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1649 struct tog_view *view, int ch)
1651 const struct got_error *err = NULL;
1652 struct tog_diff_view_state *s = &view->state.diff;
1658 if (s->first_displayed_line > 1)
1659 s->first_displayed_line--;
1663 while (i++ < view->nlines - 1 &&
1664 s->first_displayed_line > 1)
1665 s->first_displayed_line--;
1670 s->first_displayed_line++;
1675 while (!s->eof && i++ < view->nlines - 1) {
1677 line = parse_next_line(s->f, NULL);
1678 s->first_displayed_line++;
1685 struct tog_log_view_state *ls;
1686 struct commit_queue_entry *entry;
1687 struct tog_view *diff_view;
1689 if (view->parent == NULL)
1691 if (view->parent->type != TOG_VIEW_LOG)
1693 ls = &view->parent->state.log;
1696 entry = TAILQ_PREV(ls->selected_entry,
1697 commit_queue_head, entry);
1699 entry = TAILQ_NEXT(ls->selected_entry, entry);
1700 if (entry == NULL) {
1701 err = fetch_next_commit(&entry,
1703 &ls->commits, ls->graph,
1704 ls->repo, ls->in_repo_path);
1711 err = show_commit(&diff_view, view->parent,
1715 *new_view = diff_view;
1726 static const struct got_error *
1727 cmd_diff(int argc, char *argv[])
1729 const struct got_error *error = NULL;
1730 struct got_repository *repo = NULL;
1731 struct got_object *obj1 = NULL, *obj2 = NULL;
1732 char *repo_path = NULL;
1733 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1735 struct tog_view *view;
1738 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1743 while ((ch = getopt(argc, argv, "")) != -1) {
1755 usage_diff(); /* TODO show local worktree changes */
1756 } else if (argc == 2) {
1757 repo_path = getcwd(NULL, 0);
1758 if (repo_path == NULL)
1759 return got_error_from_errno();
1760 obj_id_str1 = argv[0];
1761 obj_id_str2 = argv[1];
1762 } else if (argc == 3) {
1763 repo_path = realpath(argv[0], NULL);
1764 if (repo_path == NULL)
1765 return got_error_from_errno();
1766 obj_id_str1 = argv[1];
1767 obj_id_str2 = argv[2];
1771 error = got_repo_open(&repo, repo_path);
1776 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1780 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1784 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1786 error = got_error_from_errno();
1789 error = open_diff_view(view, obj1, obj2, repo);
1792 error = view_loop(view);
1794 got_repo_close(repo);
1796 got_object_close(obj1);
1798 got_object_close(obj2);
1806 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1811 struct tog_blame_line {
1813 struct got_object_id *id;
1816 static const struct got_error *
1817 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1818 const char *path, struct tog_blame_line *lines, int nlines,
1819 int blame_complete, int selected_line, int *first_displayed_line,
1820 int *last_displayed_line, int *eof, int max_lines)
1822 const struct got_error *err;
1823 int lineno = 0, nprinted = 0;
1828 struct tog_blame_line *blame_line;
1829 struct got_object_id *prev_id = NULL;
1832 err = got_object_id_str(&id_str, id);
1837 werase(view->window);
1839 if (asprintf(&line, "commit: %s", id_str) == -1) {
1840 err = got_error_from_errno();
1845 err = format_line(&wline, &width, line, view->ncols);
1848 if (view_needs_focus_indication(view))
1849 wstandout(view->window);
1850 waddwstr(view->window, wline);
1851 if (view_needs_focus_indication(view))
1852 wstandend(view->window);
1855 if (width < view->ncols)
1856 waddch(view->window, '\n');
1858 if (asprintf(&line, "[%d/%d] %s%s",
1859 *first_displayed_line - 1 + selected_line, nlines,
1860 blame_complete ? "" : "annotating ", path) == -1) {
1862 return got_error_from_errno();
1865 err = format_line(&wline, &width, line, view->ncols);
1870 waddwstr(view->window, wline);
1873 if (width < view->ncols)
1874 waddch(view->window, '\n');
1877 while (nprinted < max_lines - 2) {
1878 line = parse_next_line(f, &len);
1883 if (++lineno < *first_displayed_line) {
1888 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1889 err = format_line(&wline, &width, line, wlimit);
1895 if (nprinted == selected_line - 1)
1896 wstandout(view->window);
1898 blame_line = &lines[lineno - 1];
1899 if (blame_line->annotated && prev_id &&
1900 got_object_id_cmp(prev_id, blame_line->id) == 0)
1901 waddstr(view->window, " ");
1902 else if (blame_line->annotated) {
1904 err = got_object_id_str(&id_str, blame_line->id);
1910 wprintw(view->window, "%.8s ", id_str);
1912 prev_id = blame_line->id;
1914 waddstr(view->window, "........ ");
1918 waddwstr(view->window, wline);
1919 while (width < wlimit) {
1920 waddch(view->window, ' ');
1923 if (nprinted == selected_line - 1)
1924 wstandend(view->window);
1925 if (++nprinted == 1)
1926 *first_displayed_line = lineno;
1931 *last_displayed_line = lineno;
1938 static const struct got_error *
1939 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1941 const struct got_error *err = NULL;
1942 struct tog_blame_cb_args *a = arg;
1943 struct tog_blame_line *line;
1945 if (nlines != a->nlines ||
1946 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1947 return got_error(GOT_ERR_RANGE);
1949 if (pthread_mutex_lock(a->mutex) != 0)
1950 return got_error_from_errno();
1952 if (*a->quit) { /* user has quit the blame view */
1953 err = got_error(GOT_ERR_ITER_COMPLETED);
1958 goto done; /* no change in this commit */
1960 line = &a->lines[lineno - 1];
1961 if (line->annotated)
1964 line->id = got_object_id_dup(id);
1965 if (line->id == NULL) {
1966 err = got_error_from_errno();
1969 line->annotated = 1;
1971 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1972 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1973 a->last_displayed_line, a->eof, a->view->nlines);
1975 if (pthread_mutex_unlock(a->mutex) != 0)
1976 return got_error_from_errno();
1981 blame_thread(void *arg)
1983 const struct got_error *err;
1984 struct tog_blame_thread_args *ta = arg;
1985 struct tog_blame_cb_args *a = ta->cb_args;
1987 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1988 blame_cb, ta->cb_args);
1990 if (pthread_mutex_lock(a->mutex) != 0)
1991 return (void *)got_error_from_errno();
1993 got_repo_close(ta->repo);
1997 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1998 a->lines, a->nlines, 1, *a->selected_line,
1999 a->first_displayed_line, a->last_displayed_line, a->eof,
2002 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2003 err = got_error_from_errno();
2008 static struct got_object_id *
2009 get_selected_commit_id(struct tog_blame_line *lines,
2010 int first_displayed_line, int selected_line)
2012 struct tog_blame_line *line;
2014 line = &lines[first_displayed_line - 1 + selected_line - 1];
2015 if (!line->annotated)
2021 static const struct got_error *
2022 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2023 struct tog_blame_line *lines, int first_displayed_line,
2024 int selected_line, struct got_repository *repo)
2026 const struct got_error *err = NULL;
2027 struct got_commit_object *commit = NULL;
2028 struct got_object_id *selected_id;
2029 struct got_object_qid *pid;
2034 selected_id = get_selected_commit_id(lines,
2035 first_displayed_line, selected_line);
2036 if (selected_id == NULL)
2039 err = got_object_open(obj, repo, selected_id);
2043 err = got_object_commit_open(&commit, repo, *obj);
2047 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2049 err = got_object_open(pobj, repo, pid->id);
2055 got_object_commit_close(commit);
2059 static const struct got_error *
2060 stop_blame(struct tog_blame *blame)
2062 const struct got_error *err = NULL;
2065 if (blame->thread) {
2066 if (pthread_join(blame->thread, (void **)&err) != 0)
2067 err = got_error_from_errno();
2068 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2070 blame->thread = NULL;
2072 if (blame->thread_args.repo) {
2073 got_repo_close(blame->thread_args.repo);
2074 blame->thread_args.repo = NULL;
2080 for (i = 0; i < blame->nlines; i++)
2081 free(blame->lines[i].id);
2083 blame->lines = NULL;
2084 free(blame->cb_args.commit_id);
2085 blame->cb_args.commit_id = NULL;
2090 static const struct got_error *
2091 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2092 struct tog_view *view, int *blame_complete,
2093 int *first_displayed_line, int *last_displayed_line,
2094 int *selected_line, int *done, int *eof, const char *path,
2095 struct got_object_id *commit_id,
2096 struct got_repository *repo)
2098 const struct got_error *err = NULL;
2099 struct got_blob_object *blob = NULL;
2100 struct got_repository *thread_repo = NULL;
2101 struct got_object *obj;
2103 err = got_object_open_by_path(&obj, repo, commit_id, path);
2106 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2107 err = got_error(GOT_ERR_OBJ_TYPE);
2111 err = got_object_blob_open(&blob, repo, obj, 8192);
2114 blame->f = got_opentemp();
2115 if (blame->f == NULL) {
2116 err = got_error_from_errno();
2119 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2124 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2125 if (blame->lines == NULL) {
2126 err = got_error_from_errno();
2130 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2134 blame->cb_args.view = view;
2135 blame->cb_args.lines = blame->lines;
2136 blame->cb_args.nlines = blame->nlines;
2137 blame->cb_args.mutex = mutex;
2138 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2139 if (blame->cb_args.commit_id == NULL) {
2140 err = got_error_from_errno();
2143 blame->cb_args.f = blame->f;
2144 blame->cb_args.path = path;
2145 blame->cb_args.first_displayed_line = first_displayed_line;
2146 blame->cb_args.selected_line = selected_line;
2147 blame->cb_args.last_displayed_line = last_displayed_line;
2148 blame->cb_args.quit = done;
2149 blame->cb_args.eof = eof;
2151 blame->thread_args.path = path;
2152 blame->thread_args.repo = thread_repo;
2153 blame->thread_args.cb_args = &blame->cb_args;
2154 blame->thread_args.complete = blame_complete;
2155 *blame_complete = 0;
2157 if (pthread_create(&blame->thread, NULL, blame_thread,
2158 &blame->thread_args) != 0) {
2159 err = got_error_from_errno();
2165 got_object_blob_close(blob);
2167 got_object_close(obj);
2173 static const struct got_error *
2174 open_blame_view(struct tog_view *view, char *path,
2175 struct got_object_id *commit_id, struct got_repository *repo)
2177 const struct got_error *err = NULL;
2178 struct tog_blame_view_state *s = &view->state.blame;
2180 SIMPLEQ_INIT(&s->blamed_commits);
2182 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2183 return got_error_from_errno();
2185 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2189 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2190 s->first_displayed_line = 1;
2191 s->last_displayed_line = view->nlines;
2192 s->selected_line = 1;
2193 s->blame_complete = 0;
2195 if (s->path == NULL)
2196 return got_error_from_errno();
2198 s->commit_id = commit_id;
2199 memset(&s->blame, 0, sizeof(s->blame));
2201 view->show = show_blame_view;
2202 view->input = input_blame_view;
2203 view->close = close_blame_view;
2205 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2206 &s->first_displayed_line, &s->last_displayed_line,
2207 &s->selected_line, &s->done, &s->eof, s->path,
2208 s->blamed_commit->id, s->repo);
2211 static const struct got_error *
2212 close_blame_view(struct tog_view *view)
2214 const struct got_error *err = NULL;
2215 struct tog_blame_view_state *s = &view->state.blame;
2217 if (s->blame.thread)
2218 err = stop_blame(&s->blame);
2220 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2221 struct got_object_qid *blamed_commit;
2222 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2223 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2224 got_object_qid_free(blamed_commit);
2232 static const struct got_error *
2233 show_blame_view(struct tog_view *view)
2235 const struct got_error *err = NULL;
2236 struct tog_blame_view_state *s = &view->state.blame;
2238 if (pthread_mutex_lock(&s->mutex) != 0)
2239 return got_error_from_errno();
2241 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2242 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2243 s->selected_line, &s->first_displayed_line,
2244 &s->last_displayed_line, &s->eof, view->nlines);
2246 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2247 err = got_error_from_errno();
2252 static const struct got_error *
2253 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2254 struct tog_view *view, int ch)
2256 const struct got_error *err = NULL, *thread_err = NULL;
2257 struct got_object *obj = NULL, *pobj = NULL;
2258 struct tog_view *diff_view;
2259 struct tog_blame_view_state *s = &view->state.blame;
2261 if (pthread_mutex_lock(&s->mutex) != 0) {
2262 err = got_error_from_errno();
2269 if (pthread_mutex_unlock(&s->mutex) != 0) {
2270 err = got_error_from_errno();
2273 return stop_blame(&s->blame);
2276 if (s->selected_line > 1)
2278 else if (s->selected_line == 1 &&
2279 s->first_displayed_line > 1)
2280 s->first_displayed_line--;
2283 if (s->first_displayed_line == 1) {
2284 s->selected_line = 1;
2287 if (s->first_displayed_line > view->nlines - 2)
2288 s->first_displayed_line -=
2291 s->first_displayed_line = 1;
2295 if (s->selected_line < view->nlines - 2 &&
2296 s->first_displayed_line +
2297 s->selected_line <= s->blame.nlines)
2299 else if (s->last_displayed_line <
2301 s->first_displayed_line++;
2305 struct got_object_id *id;
2306 id = get_selected_commit_id(s->blame.lines,
2307 s->first_displayed_line, s->selected_line);
2308 if (id == NULL || got_object_id_cmp(id,
2309 s->blamed_commit->id) == 0)
2311 err = open_selected_commit(&pobj, &obj,
2312 s->blame.lines, s->first_displayed_line,
2313 s->selected_line, s->repo);
2316 if (pobj == NULL && obj == NULL)
2318 if (ch == 'p' && pobj == NULL)
2321 if (pthread_mutex_unlock(&s->mutex) != 0) {
2322 err = got_error_from_errno();
2325 thread_err = stop_blame(&s->blame);
2327 if (pthread_mutex_lock(&s->mutex) != 0) {
2328 err = got_error_from_errno();
2333 id = got_object_get_id(ch == 'b' ? obj : pobj);
2334 got_object_close(obj);
2337 got_object_close(pobj);
2340 err = got_object_qid_alloc(&s->blamed_commit, id);
2343 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2344 s->blamed_commit, entry);
2345 err = run_blame(&s->blame, &s->mutex, view,
2347 &s->first_displayed_line,
2348 &s->last_displayed_line,
2349 &s->selected_line, &s->done, &s->eof,
2350 s->path, s->blamed_commit->id, s->repo);
2356 struct got_object_qid *first;
2357 first = SIMPLEQ_FIRST(&s->blamed_commits);
2358 if (!got_object_id_cmp(first->id, s->commit_id))
2361 if (pthread_mutex_unlock(&s->mutex) != 0) {
2362 err = got_error_from_errno();
2365 thread_err = stop_blame(&s->blame);
2367 if (pthread_mutex_lock(&s->mutex) != 0) {
2368 err = got_error_from_errno();
2373 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2374 got_object_qid_free(s->blamed_commit);
2376 SIMPLEQ_FIRST(&s->blamed_commits);
2377 err = run_blame(&s->blame, &s->mutex, view,
2379 &s->first_displayed_line,
2380 &s->last_displayed_line,
2381 &s->selected_line, &s->done, &s->eof, s->path,
2382 s->blamed_commit->id, s->repo);
2389 err = open_selected_commit(&pobj, &obj,
2390 s->blame.lines, s->first_displayed_line,
2391 s->selected_line, s->repo);
2394 if (pobj == NULL && obj == NULL)
2396 diff_view = view_open(0, 0, 0, 0, view,
2398 if (diff_view == NULL) {
2399 err = got_error_from_errno();
2402 err = open_diff_view(diff_view, pobj, obj,
2405 view_close(diff_view);
2408 *new_view = diff_view;
2410 got_object_close(pobj);
2413 got_object_close(obj);
2420 if (s->last_displayed_line >= s->blame.nlines &&
2421 s->selected_line < view->nlines - 2) {
2422 s->selected_line = MIN(s->blame.nlines,
2426 if (s->last_displayed_line + view->nlines - 2
2428 s->first_displayed_line +=
2431 s->first_displayed_line =
2436 if (s->selected_line > view->nlines - 2) {
2437 s->selected_line = MIN(s->blame.nlines,
2445 if (pthread_mutex_unlock(&s->mutex) != 0)
2446 err = got_error_from_errno();
2449 got_object_close(pobj);
2450 return thread_err ? thread_err : err;
2453 static const struct got_error *
2454 cmd_blame(int argc, char *argv[])
2456 const struct got_error *error;
2457 struct got_repository *repo = NULL;
2458 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2459 struct got_object_id *commit_id = NULL;
2460 char *commit_id_str = NULL;
2462 struct tog_view *view;
2465 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2470 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2473 commit_id_str = optarg;
2476 repo_path = realpath(optarg, NULL);
2477 if (repo_path == NULL)
2478 err(1, "-r option");
2494 cwd = getcwd(NULL, 0);
2496 error = got_error_from_errno();
2499 if (repo_path == NULL) {
2500 repo_path = strdup(cwd);
2501 if (repo_path == NULL) {
2502 error = got_error_from_errno();
2508 error = got_repo_open(&repo, repo_path);
2512 error = got_repo_map_path(&in_repo_path, repo, path);
2516 if (commit_id_str == NULL) {
2517 struct got_reference *head_ref;
2518 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2521 error = got_ref_resolve(&commit_id, repo, head_ref);
2522 got_ref_close(head_ref);
2524 struct got_object *obj;
2525 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2528 commit_id = got_object_id_dup(got_object_get_id(obj));
2529 if (commit_id == NULL)
2530 error = got_error_from_errno();
2531 got_object_close(obj);
2536 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2538 error = got_error_from_errno();
2541 error = open_blame_view(view, in_repo_path, commit_id, repo);
2544 error = view_loop(view);
2550 got_repo_close(repo);
2554 static const struct got_error *
2555 draw_tree_entries(struct tog_view *view,
2556 struct got_tree_entry **first_displayed_entry,
2557 struct got_tree_entry **last_displayed_entry,
2558 struct got_tree_entry **selected_entry, int *ndisplayed,
2559 const char *label, int show_ids, const char *parent_path,
2560 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2562 const struct got_error *err = NULL;
2563 struct got_tree_entry *te;
2569 werase(view->window);
2574 err = format_line(&wline, &width, label, view->ncols);
2577 if (view_needs_focus_indication(view))
2578 wstandout(view->window);
2579 waddwstr(view->window, wline);
2580 if (view_needs_focus_indication(view))
2581 wstandend(view->window);
2584 if (width < view->ncols)
2585 waddch(view->window, '\n');
2588 err = format_line(&wline, &width, parent_path, view->ncols);
2591 waddwstr(view->window, wline);
2594 if (width < view->ncols)
2595 waddch(view->window, '\n');
2598 waddch(view->window, '\n');
2602 te = SIMPLEQ_FIRST(&entries->head);
2603 if (*first_displayed_entry == NULL) {
2604 if (selected == 0) {
2605 wstandout(view->window);
2606 *selected_entry = NULL;
2608 waddstr(view->window, " ..\n"); /* parent directory */
2610 wstandend(view->window);
2617 while (te != *first_displayed_entry)
2618 te = SIMPLEQ_NEXT(te, entry);
2622 char *line = NULL, *id_str = NULL;
2625 err = got_object_id_str(&id_str, te->id);
2627 return got_error_from_errno();
2629 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2630 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2632 return got_error_from_errno();
2635 err = format_line(&wline, &width, line, view->ncols);
2640 if (n == selected) {
2641 wstandout(view->window);
2642 *selected_entry = te;
2644 waddwstr(view->window, wline);
2645 if (width < view->ncols)
2646 waddch(view->window, '\n');
2648 wstandend(view->window);
2654 *last_displayed_entry = te;
2657 te = SIMPLEQ_NEXT(te, entry);
2665 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2666 const struct got_tree_entries *entries, int isroot)
2668 struct got_tree_entry *te, *prev;
2671 if (*first_displayed_entry == NULL)
2674 te = SIMPLEQ_FIRST(&entries->head);
2675 if (*first_displayed_entry == te) {
2677 *first_displayed_entry = NULL;
2681 /* XXX this is stupid... switch to TAILQ? */
2682 for (i = 0; i < maxscroll; i++) {
2683 while (te != *first_displayed_entry) {
2685 te = SIMPLEQ_NEXT(te, entry);
2687 *first_displayed_entry = prev;
2688 te = SIMPLEQ_FIRST(&entries->head);
2690 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2691 *first_displayed_entry = NULL;
2695 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2696 struct got_tree_entry *last_displayed_entry,
2697 const struct got_tree_entries *entries)
2699 struct got_tree_entry *next;
2702 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2705 if (*first_displayed_entry)
2706 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2708 next = SIMPLEQ_FIRST(&entries->head);
2710 *first_displayed_entry = next;
2711 if (++n >= maxscroll)
2713 next = SIMPLEQ_NEXT(next, entry);
2717 static const struct got_error *
2718 tree_entry_path(char **path, struct tog_parent_trees *parents,
2719 struct got_tree_entry *te)
2721 const struct got_error *err = NULL;
2722 struct tog_parent_tree *pt;
2723 size_t len = 2; /* for leading slash and NUL */
2725 TAILQ_FOREACH(pt, parents, entry)
2726 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2728 len += strlen(te->name);
2730 *path = calloc(1, len);
2732 return got_error_from_errno();
2735 pt = TAILQ_LAST(parents, tog_parent_trees);
2737 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2738 err = got_error(GOT_ERR_NO_SPACE);
2741 if (strlcat(*path, "/", len) >= len) {
2742 err = got_error(GOT_ERR_NO_SPACE);
2745 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2748 if (strlcat(*path, te->name, len) >= len) {
2749 err = got_error(GOT_ERR_NO_SPACE);
2761 static const struct got_error *
2762 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2763 struct got_tree_entry *te, struct tog_parent_trees *parents,
2764 struct got_object_id *commit_id, struct got_repository *repo)
2766 const struct got_error *err = NULL;
2768 struct tog_view *blame_view;
2770 err = tree_entry_path(&path, parents, te);
2774 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2775 if (blame_view == NULL)
2776 return got_error_from_errno();
2778 err = open_blame_view(blame_view, path, commit_id, repo);
2780 view_close(blame_view);
2783 *new_view = blame_view;
2787 static const struct got_error *
2788 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2789 struct got_tree_entry *te, struct tog_parent_trees *parents,
2790 struct got_object_id *commit_id, struct got_repository *repo)
2792 struct tog_view *log_view;
2793 const struct got_error *err = NULL;
2796 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2797 if (log_view == NULL)
2798 return got_error_from_errno();
2800 err = tree_entry_path(&path, parents, te);
2804 err = open_log_view(log_view, commit_id, repo, path);
2806 view_close(log_view);
2808 *new_view = log_view;
2813 static const struct got_error *
2814 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2815 struct got_object_id *commit_id, struct got_repository *repo)
2817 const struct got_error *err = NULL;
2818 char *commit_id_str = NULL;
2819 struct tog_tree_view_state *s = &view->state.tree;
2821 TAILQ_INIT(&s->parents);
2823 err = got_object_id_str(&commit_id_str, commit_id);
2827 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2828 err = got_error_from_errno();
2832 s->root = s->tree = root;
2833 s->entries = got_object_tree_get_entries(root);
2834 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2835 s->commit_id = commit_id;
2838 view->show = show_tree_view;
2839 view->input = input_tree_view;
2840 view->close = close_tree_view;
2842 free(commit_id_str);
2844 free(s->tree_label);
2848 static const struct got_error *
2849 close_tree_view(struct tog_view *view)
2851 struct tog_tree_view_state *s = &view->state.tree;
2853 free(s->tree_label);
2854 while (!TAILQ_EMPTY(&s->parents)) {
2855 struct tog_parent_tree *parent;
2856 parent = TAILQ_FIRST(&s->parents);
2857 TAILQ_REMOVE(&s->parents, parent, entry);
2861 if (s->tree != s->root)
2862 got_object_tree_close(s->tree);
2863 got_object_tree_close(s->root);
2868 static const struct got_error *
2869 show_tree_view(struct tog_view *view)
2871 const struct got_error *err = NULL;
2872 struct tog_tree_view_state *s = &view->state.tree;
2875 err = tree_entry_path(&parent_path, &s->parents, NULL);
2879 err = draw_tree_entries(view, &s->first_displayed_entry,
2880 &s->last_displayed_entry, &s->selected_entry,
2881 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2882 s->entries, s->selected, view->nlines, s->tree == s->root);
2887 static const struct got_error *
2888 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2889 struct tog_view *view, int ch)
2891 const struct got_error *err = NULL;
2892 struct tog_tree_view_state *s = &view->state.tree;
2896 s->show_ids = !s->show_ids;
2899 if (s->selected_entry) {
2900 err = log_tree_entry(new_view, view,
2901 s->selected_entry, &s->parents,
2902 s->commit_id, s->repo);
2907 if (s->selected > 0)
2909 if (s->selected > 0)
2911 tree_scroll_up(&s->first_displayed_entry, 1,
2912 s->entries, s->tree == s->root);
2915 if (SIMPLEQ_FIRST(&s->entries->head) ==
2916 s->first_displayed_entry) {
2917 if (s->tree != s->root)
2918 s->first_displayed_entry = NULL;
2922 tree_scroll_up(&s->first_displayed_entry,
2923 view->nlines, s->entries,
2924 s->tree == s->root);
2928 if (s->selected < s->ndisplayed - 1) {
2932 tree_scroll_down(&s->first_displayed_entry, 1,
2933 s->last_displayed_entry, s->entries);
2936 tree_scroll_down(&s->first_displayed_entry,
2937 view->nlines, s->last_displayed_entry,
2939 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2942 /* can't scroll any further; move cursor down */
2943 if (s->selected < s->ndisplayed - 1)
2944 s->selected = s->ndisplayed - 1;
2948 if (s->selected_entry == NULL) {
2949 struct tog_parent_tree *parent;
2951 /* user selected '..' */
2952 if (s->tree == s->root)
2954 parent = TAILQ_FIRST(&s->parents);
2955 TAILQ_REMOVE(&s->parents, parent,
2957 got_object_tree_close(s->tree);
2958 s->tree = parent->tree;
2960 got_object_tree_get_entries(s->tree);
2961 s->first_displayed_entry =
2962 parent->first_displayed_entry;
2964 parent->selected_entry;
2965 s->selected = parent->selected;
2967 } else if (S_ISDIR(s->selected_entry->mode)) {
2968 struct tog_parent_tree *parent;
2969 struct got_tree_object *child;
2970 err = got_object_open_as_tree(&child,
2971 s->repo, s->selected_entry->id);
2974 parent = calloc(1, sizeof(*parent));
2975 if (parent == NULL) {
2976 err = got_error_from_errno();
2979 parent->tree = s->tree;
2980 parent->first_displayed_entry =
2981 s->first_displayed_entry;
2982 parent->selected_entry = s->selected_entry;
2983 parent->selected = s->selected;
2984 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2987 got_object_tree_get_entries(s->tree);
2989 s->first_displayed_entry = NULL;
2990 } else if (S_ISREG(s->selected_entry->mode)) {
2991 err = blame_tree_entry(new_view, view,
2992 s->selected_entry, &s->parents,
2993 s->commit_id, s->repo);
2999 if (s->selected > view->nlines)
3000 s->selected = s->ndisplayed - 1;
3013 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3018 static const struct got_error *
3019 cmd_tree(int argc, char *argv[])
3021 const struct got_error *error;
3022 struct got_repository *repo = NULL;
3023 char *repo_path = NULL;
3024 struct got_object_id *commit_id = NULL;
3025 char *commit_id_arg = NULL;
3026 struct got_commit_object *commit = NULL;
3027 struct got_tree_object *tree = NULL;
3029 struct tog_view *view;
3032 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3037 while ((ch = getopt(argc, argv, "c:")) != -1) {
3040 commit_id_arg = optarg;
3052 repo_path = getcwd(NULL, 0);
3053 if (repo_path == NULL)
3054 return got_error_from_errno();
3055 } else if (argc == 1) {
3056 repo_path = realpath(argv[0], NULL);
3057 if (repo_path == NULL)
3058 return got_error_from_errno();
3062 error = got_repo_open(&repo, repo_path);
3067 if (commit_id_arg == NULL) {
3068 error = get_head_commit_id(&commit_id, repo);
3072 struct got_object *obj;
3073 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3074 if (error == NULL) {
3075 commit_id = got_object_id_dup(got_object_get_id(obj));
3076 if (commit_id == NULL)
3077 error = got_error_from_errno();
3083 error = got_object_open_as_commit(&commit, repo, commit_id);
3087 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3091 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3093 error = got_error_from_errno();
3096 error = open_tree_view(view, tree, commit_id, repo);
3099 error = view_loop(view);
3103 got_object_commit_close(commit);
3105 got_object_tree_close(tree);
3107 got_repo_close(repo);
3117 intrflush(stdscr, FALSE);
3118 keypad(stdscr, TRUE);
3127 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3128 "Available commands:\n", getprogname());
3129 for (i = 0; i < nitems(tog_commands); i++) {
3130 struct tog_cmd *cmd = &tog_commands[i];
3131 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3137 make_argv(const char *arg0, const char *arg1)
3140 int argc = (arg1 == NULL ? 1 : 2);
3142 argv = calloc(argc, sizeof(char *));
3145 argv[0] = strdup(arg0);
3146 if (argv[0] == NULL)
3149 argv[1] = strdup(arg1);
3150 if (argv[1] == NULL)
3158 main(int argc, char *argv[])
3160 const struct got_error *error = NULL;
3161 struct tog_cmd *cmd = NULL;
3163 char **cmd_argv = NULL;
3165 setlocale(LC_ALL, "");
3167 while ((ch = getopt(argc, argv, "h")) != -1) {
3186 /* Build an argument vector which runs a default command. */
3187 cmd = &tog_commands[0];
3188 cmd_argv = make_argv(cmd->name, NULL);
3193 /* Did the user specific a command? */
3194 for (i = 0; i < nitems(tog_commands); i++) {
3195 if (strncmp(tog_commands[i].name, argv[0],
3196 strlen(argv[0])) == 0) {
3197 cmd = &tog_commands[i];
3199 tog_commands[i].cmd_usage();
3204 /* Did the user specify a repository? */
3205 char *repo_path = realpath(argv[0], NULL);
3207 struct got_repository *repo;
3208 error = got_repo_open(&repo, repo_path);
3210 got_repo_close(repo);
3212 error = got_error_from_errno();
3215 fprintf(stderr, "%s: '%s' is not a "
3216 "known command\n", getprogname(),
3220 fprintf(stderr, "%s: '%s' is neither a known "
3221 "command nor a path to a repository\n",
3222 getprogname(), argv[0]);
3226 cmd = &tog_commands[0];
3227 cmd_argv = make_argv(cmd->name, repo_path);
3235 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3242 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);