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
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
47 #include "got_blame.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
64 __dead static void usage(void);
65 __dead static void usage_log(void);
66 __dead static void usage_diff(void);
67 __dead static void usage_blame(void);
68 __dead static void usage_tree(void);
70 static const struct got_error* cmd_log(int, char *[]);
71 static const struct got_error* cmd_diff(int, char *[]);
72 static const struct got_error* cmd_blame(int, char *[]);
73 static const struct got_error* cmd_tree(int, char *[]);
75 static struct tog_cmd tog_commands[] = {
76 { "log", cmd_log, usage_log,
77 "show repository history" },
78 { "diff", cmd_diff, usage_diff,
79 "compare files and directories" },
80 { "blame", cmd_blame, usage_blame,
81 "show line-by-line file history" },
82 { "tree", cmd_tree, usage_tree,
83 "browse trees in repository" },
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
96 int first_displayed_line;
97 int last_displayed_line;
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
106 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 struct commit_queue {
109 struct commit_queue_head head;
112 struct tog_log_view_state {
113 struct got_commit_graph *graph;
114 struct commit_queue commits;
115 struct commit_queue_entry *first_displayed_entry;
116 struct commit_queue_entry *last_displayed_entry;
117 struct commit_queue_entry *selected_entry;
120 struct got_repository *repo;
121 struct got_object_id *start_id;
124 struct tog_blame_cb_args {
125 pthread_mutex_t *mutex;
126 struct tog_blame_line *lines; /* one per line */
129 struct tog_view *view;
130 struct got_object_id *commit_id;
133 int *first_displayed_line;
134 int *last_displayed_line;
140 struct tog_blame_thread_args {
142 struct got_repository *repo;
143 struct tog_blame_cb_args *cb_args;
150 struct tog_blame_line *lines;
153 struct tog_blame_thread_args thread_args;
154 struct tog_blame_cb_args cb_args;
158 struct tog_blame_view_state {
159 int first_displayed_line;
160 int last_displayed_line;
165 pthread_mutex_t mutex;
166 struct got_object_id_queue blamed_commits;
167 struct got_object_qid *blamed_commit;
169 struct got_repository *repo;
170 struct got_object_id *commit_id;
171 struct tog_blame blame;
174 struct tog_parent_tree {
175 TAILQ_ENTRY(tog_parent_tree) entry;
176 struct got_tree_object *tree;
177 struct got_tree_entry *first_displayed_entry;
178 struct got_tree_entry *selected_entry;
182 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 struct tog_tree_view_state {
186 struct got_tree_object *root;
187 struct got_tree_object *tree;
188 const struct got_tree_entries *entries;
189 struct got_tree_entry *first_displayed_entry;
190 struct got_tree_entry *last_displayed_entry;
191 struct got_tree_entry *selected_entry;
192 int nentries, ndisplayed, selected, show_ids;
193 struct tog_parent_trees parents;
194 struct got_object_id *commit_id;
195 struct got_repository *repo;
198 TAILQ_HEAD(tog_view_list_head, tog_view);
200 TAILQ_ENTRY(tog_view) entry;
203 int nlines, ncols, begin_y, begin_x;
204 int lines, cols; /* copies of LINES and COLS */
206 struct tog_view *parent;
207 struct tog_view *child;
209 /* type-specific state */
210 enum tog_view_type type;
212 struct tog_diff_view_state diff;
213 struct tog_log_view_state log;
214 struct tog_blame_view_state blame;
215 struct tog_tree_view_state tree;
218 const struct got_error *(*show)(struct tog_view *);
219 const struct got_error *(*update_siblings)(int *, struct tog_view *,
221 const struct got_error *(*input)(struct tog_view **,
222 struct tog_view **, struct tog_view *, int);
223 const struct got_error *(*set_child)(struct tog_view *,
225 const struct got_error *(*close)(struct tog_view *);
228 static const struct got_error *open_diff_view(struct tog_view *,
229 struct got_object *, struct got_object *, struct got_repository *);
230 static const struct got_error *show_diff_view(struct tog_view *);
231 static const struct got_error *update_siblings_diff_view(int *,
232 struct tog_view *, struct tog_view *);
233 static const struct got_error *input_diff_view(struct tog_view **,
234 struct tog_view **, struct tog_view *, int);
235 static const struct got_error* close_diff_view(struct tog_view *);
237 static const struct got_error *open_log_view(struct tog_view *,
238 struct got_object_id *, struct got_repository *, const char *);
239 static const struct got_error * show_log_view(struct tog_view *);
240 static const struct got_error *update_siblings_log_view(int *,
241 struct tog_view *, struct tog_view *);
242 static const struct got_error *input_log_view(struct tog_view **,
243 struct tog_view **, struct tog_view *, int);
244 static const struct got_error *close_log_view(struct tog_view *);
245 static const struct got_error* set_child_log_view(struct tog_view *,
248 static const struct got_error *open_blame_view(struct tog_view *, char *,
249 struct got_object_id *, struct got_repository *);
250 static const struct got_error *show_blame_view(struct tog_view *);
251 static const struct got_error *update_siblings_blame_view(int *,
252 struct tog_view *, struct tog_view *);
253 static const struct got_error *input_blame_view(struct tog_view **,
254 struct tog_view **, struct tog_view *, int);
255 static const struct got_error *close_blame_view(struct tog_view *);
257 static const struct got_error *open_tree_view(struct tog_view *,
258 struct got_tree_object *, struct got_object_id *, struct got_repository *);
259 static const struct got_error *show_tree_view(struct tog_view *);
260 static const struct got_error *update_siblings_tree_view(int *,
261 struct tog_view *, struct tog_view *);
262 static const struct got_error *input_tree_view(struct tog_view **,
263 struct tog_view **, struct tog_view *, int);
264 static const struct got_error *close_tree_view(struct tog_view *);
266 static const struct got_error *
267 view_close(struct tog_view *view)
269 const struct got_error *err = NULL;
272 err = view->close(view);
274 del_panel(view->panel);
276 delwin(view->window);
281 static struct tog_view *
282 view_open(int nlines, int ncols, int begin_y, int begin_x,
283 enum tog_view_type type)
285 struct tog_view *view = calloc(1, sizeof(*view));
293 view->nlines = nlines ? nlines : LINES - begin_y;
294 view->ncols = ncols ? ncols : COLS - begin_x;
295 view->begin_y = begin_y;
296 view->begin_x = begin_x;
297 view->window = newwin(nlines, ncols, begin_y, begin_x);
298 if (view->window == NULL) {
302 view->panel = new_panel(view->window);
303 if (view->panel == NULL ||
304 set_panel_userptr(view->panel, view) != OK) {
309 keypad(view->window, TRUE);
313 static const struct got_error *
314 view_show(struct tog_view *view, struct tog_view_list_head *views)
316 const struct got_error *err;
319 err = view->show(view);
326 TAILQ_FOREACH(v, views, entry) {
328 err = view->update_siblings(&updated, view, v);
342 view_split_begin_x(int begin_x)
346 return (COLS >= 120 ? COLS/2 : 0);
349 static const struct got_error *
350 view_resize(struct tog_view *view)
354 if (view->lines > LINES)
355 nlines = view->nlines - (view->lines - LINES);
357 nlines = view->nlines + (LINES - view->lines);
359 if (view->cols > COLS)
360 ncols = view->ncols - (view->cols - COLS);
362 ncols = view->ncols + (COLS - view->cols);
364 if (wresize(view->window, nlines, ncols) == ERR)
365 return got_error_from_errno();
366 replace_panel(view->panel, view->window);
368 view->nlines = nlines;
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();
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();
418 static const struct got_error *
419 view_input(struct tog_view **new, struct tog_view **dead,
420 struct tog_view **focus, int *done, struct tog_view *view,
421 struct tog_view_list_head *views)
423 const struct got_error *err = NULL;
424 struct tog_view *next, *prev, *v;
431 nodelay(stdscr, FALSE);
432 ch = wgetch(view->window);
433 nodelay(stdscr, TRUE);
438 next = TAILQ_NEXT(view, entry);
442 *focus = TAILQ_FIRST(views);
445 prev = TAILQ_PREV(view, tog_view_list_head, entry);
449 *focus = TAILQ_LAST(views, tog_view_list_head);
452 err = view->input(new, dead, view, ch);
459 if (view->begin_x == 0)
460 err = view_splitscreen(view);
462 err = view_fullscreen(view);
465 err = view->input(new, dead, view, KEY_RESIZE);
471 TAILQ_FOREACH(v, views, entry) {
472 err = view_resize(v);
475 err = v->input(new, dead, v, ch);
479 err = view->input(new, dead, view, ch);
487 view_vborder(struct tog_view *view)
490 struct tog_view *view_above;
492 panel = panel_above(view->panel);
496 view_above = panel_userptr(panel);
497 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
498 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
502 view_needs_focus_indication(struct tog_view *view)
509 panel = panel_above(view->panel);
511 struct tog_view *view_above = panel_userptr(panel);
512 if (view_above->begin_x > view->begin_x)
516 panel = panel_below(view->panel);
518 struct tog_view *view_below = panel_userptr(panel);
519 if (view->begin_x > view_below->begin_x)
526 static const struct got_error *
527 view_loop(struct tog_view *view)
529 const struct got_error *err = NULL;
530 struct tog_view_list_head views;
531 struct tog_view *new_view, *dead_view, *focus_view, *v;
535 TAILQ_INSERT_HEAD(&views, view, entry);
538 err = view_show(view, &views);
543 while (!TAILQ_EMPTY(&views) && !done) {
544 err = view_input(&new_view, &dead_view, &focus_view, &done,
549 TAILQ_REMOVE(&views, dead_view, entry);
550 err = view_close(dead_view);
553 if (view == dead_view) {
556 else if (!TAILQ_EMPTY(&views)) {
557 view = TAILQ_LAST(&views,
566 /* Only allow one view per type. */
567 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
568 if (v->type != new_view->type)
570 TAILQ_REMOVE(&views, v, entry);
578 TAILQ_INSERT_TAIL(&views, new_view, entry);
579 focus_view = new_view;
582 show_panel(focus_view->panel);
584 if (focus_view->begin_x == 0 &&
586 focus_view != new_view)
587 show_panel(view->panel);
590 focus_view->focussed = 1;
593 TAILQ_FOREACH(v, &views, entry) {
594 err = view_show(v, &views);
602 while (!TAILQ_EMPTY(&views)) {
603 view = TAILQ_FIRST(&views);
604 TAILQ_REMOVE(&views, view, entry);
615 "usage: %s log [-c commit] [-r repository-path] [path]\n",
620 /* Create newly allocated wide-character string equivalent to a byte string. */
621 static const struct got_error *
622 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
625 const struct got_error *err = NULL;
628 *wlen = mbstowcs(NULL, s, 0);
629 if (*wlen == (size_t)-1) {
632 return got_error_from_errno();
634 /* byte string invalid in current encoding; try to "fix" it */
635 err = got_mbsavis(&vis, &vislen, s);
638 *wlen = mbstowcs(NULL, vis, 0);
639 if (*wlen == (size_t)-1) {
640 err = got_error_from_errno(); /* give up */
645 *ws = calloc(*wlen + 1, sizeof(*ws));
647 err = got_error_from_errno();
651 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
652 err = got_error_from_errno();
663 /* Format a line for display, ensuring that it won't overflow a width limit. */
664 static const struct got_error *
665 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
667 const struct got_error *err = NULL;
669 wchar_t *wline = NULL;
676 err = mbs2ws(&wline, &wlen, line);
681 while (i < wlen && cols < wlimit) {
682 int width = wcwidth(wline[i]);
689 if (cols + width <= wlimit)
694 if (wline[i] == L'\t')
695 cols += TABSIZE - ((cols + 1) % TABSIZE);
699 err = got_error_from_errno();
714 static const struct got_error *
715 draw_commit(struct tog_view *view, struct got_commit_object *commit,
716 struct got_object_id *id)
718 const struct got_error *err = NULL;
719 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
720 char *logmsg0 = NULL, *logmsg = NULL;
721 char *author0 = NULL, *author = NULL;
722 wchar_t *wlogmsg = NULL, *wauthor = NULL;
723 int author_width, logmsg_width;
724 char *newline, *smallerthan;
727 static const size_t date_display_cols = 9;
728 static const size_t author_display_cols = 16;
729 const int avail = view->ncols;
731 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
732 &commit->tm_committer) >= sizeof(datebuf))
733 return got_error(GOT_ERR_NO_SPACE);
735 if (avail < date_display_cols)
736 limit = MIN(sizeof(datebuf) - 1, avail);
738 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
739 waddnstr(view->window, datebuf, limit);
744 author0 = strdup(commit->author);
745 if (author0 == NULL) {
746 err = got_error_from_errno();
750 smallerthan = strchr(author, '<');
754 char *at = strchr(author, '@');
759 err = format_line(&wauthor, &author_width, author, limit);
762 waddwstr(view->window, wauthor);
764 while (col <= avail && author_width < author_display_cols + 1) {
765 waddch(view->window, ' ');
772 logmsg0 = strdup(commit->logmsg);
773 if (logmsg0 == NULL) {
774 err = got_error_from_errno();
778 while (*logmsg == '\n')
780 newline = strchr(logmsg, '\n');
784 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
787 waddwstr(view->window, wlogmsg);
789 while (col <= avail) {
790 waddch(view->window, ' ');
802 static struct commit_queue_entry *
803 alloc_commit_queue_entry(struct got_commit_object *commit,
804 struct got_object_id *id)
806 struct commit_queue_entry *entry;
808 entry = calloc(1, sizeof(*entry));
813 entry->commit = commit;
818 pop_commit(struct commit_queue *commits)
820 struct commit_queue_entry *entry;
822 entry = TAILQ_FIRST(&commits->head);
823 TAILQ_REMOVE(&commits->head, entry, entry);
824 got_object_commit_close(entry->commit);
826 /* Don't free entry->id! It is owned by the commit graph. */
831 free_commits(struct commit_queue *commits)
833 while (!TAILQ_EMPTY(&commits->head))
837 static const struct got_error *
838 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
839 struct got_object_id *start_id, int minqueue,
840 struct got_repository *repo, const char *path)
842 const struct got_error *err = NULL;
846 err = got_commit_graph_iter_start(graph, start_id, repo);
851 while (nqueued < minqueue) {
852 struct got_object_id *id;
853 struct got_commit_object *commit;
854 struct commit_queue_entry *entry;
856 err = got_commit_graph_iter_next(&id, graph);
858 if (err->code == GOT_ERR_ITER_COMPLETED) {
862 if (err->code != GOT_ERR_ITER_NEED_MORE)
864 err = got_commit_graph_fetch_commits(graph,
874 err = got_object_open_as_commit(&commit, repo, id);
877 entry = alloc_commit_queue_entry(commit, id);
879 err = got_error_from_errno();
883 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
891 static const struct got_error *
892 fetch_next_commit(struct commit_queue_entry **pentry,
893 struct commit_queue_entry *entry, struct commit_queue *commits,
894 struct got_commit_graph *graph, struct got_repository *repo,
897 const struct got_error *err = NULL;
901 err = queue_commits(graph, commits, NULL, 1, repo, path);
905 /* Next entry to display should now be available. */
906 *pentry = TAILQ_NEXT(entry, entry);
910 static const struct got_error *
911 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
913 const struct got_error *err = NULL;
914 struct got_reference *head_ref;
918 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
922 err = got_ref_resolve(head_id, repo, head_ref);
923 got_ref_close(head_ref);
932 static const struct got_error *
933 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
934 struct commit_queue_entry **selected, struct commit_queue_entry *first,
935 struct commit_queue *commits, int selected_idx, int limit,
936 struct got_commit_graph *graph, struct got_repository *repo,
939 const struct got_error *err = NULL;
940 struct commit_queue_entry *entry;
942 char *id_str, *header;
948 if (ncommits == selected_idx) {
952 entry = TAILQ_NEXT(entry, entry);
956 err = got_object_id_str(&id_str, (*selected)->id);
960 if (path && strcmp(path, "/") != 0) {
961 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
962 err = got_error_from_errno();
966 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
967 err = got_error_from_errno();
972 err = format_line(&wline, &width, header, view->ncols);
979 werase(view->window);
981 if (view_needs_focus_indication(view))
982 wstandout(view->window);
983 waddwstr(view->window, wline);
984 if (view_needs_focus_indication(view))
985 wstandend(view->window);
986 if (width < view->ncols)
987 waddch(view->window, '\n');
996 if (ncommits >= limit - 1)
998 if (view->focussed && ncommits == selected_idx)
999 wstandout(view->window);
1000 err = draw_commit(view, entry->commit, entry->id);
1001 if (view->focussed && ncommits == selected_idx)
1002 wstandend(view->window);
1007 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1008 err = queue_commits(graph, commits, NULL, 1,
1011 if (err->code != GOT_ERR_ITER_COMPLETED)
1016 entry = TAILQ_NEXT(entry, entry);
1025 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1026 struct commit_queue *commits)
1028 struct commit_queue_entry *entry;
1031 entry = TAILQ_FIRST(&commits->head);
1032 if (*first_displayed_entry == entry)
1035 entry = *first_displayed_entry;
1036 while (entry && nscrolled < maxscroll) {
1037 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1039 *first_displayed_entry = entry;
1045 static const struct got_error *
1046 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1047 struct commit_queue_entry **last_displayed_entry,
1048 struct commit_queue *commits, struct got_commit_graph *graph,
1049 struct got_repository *repo, const char *path)
1051 const struct got_error *err = NULL;
1052 struct commit_queue_entry *pentry;
1056 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1057 if (pentry == NULL) {
1058 err = fetch_next_commit(&pentry, *last_displayed_entry,
1059 commits, graph, repo, path);
1060 if (err || pentry == NULL)
1063 *last_displayed_entry = pentry;
1065 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1068 *first_displayed_entry = pentry;
1069 } while (++nscrolled < maxscroll);
1074 static const struct got_error *
1075 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1076 struct got_object_id *commit_id, struct got_commit_object *commit,
1077 struct got_repository *repo)
1079 const struct got_error *err;
1080 struct got_object *obj1 = NULL, *obj2 = NULL;
1081 struct got_object_qid *parent_id;
1082 struct tog_view *diff_view;
1084 err = got_object_open(&obj2, repo, commit_id);
1088 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1090 err = got_object_open(&obj1, repo, parent_id->id);
1095 diff_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1097 if (diff_view == NULL) {
1098 err = got_error_from_errno();
1102 err = open_diff_view(diff_view, obj1, obj2, repo);
1104 *new_view = diff_view;
1107 got_object_close(obj1);
1109 got_object_close(obj2);
1113 static const struct got_error *
1114 browse_commit(struct tog_view **new_view, int begin_x,
1115 struct commit_queue_entry *entry, struct got_repository *repo)
1117 const struct got_error *err = NULL;
1118 struct got_tree_object *tree;
1119 struct tog_view *tree_view;
1121 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1125 tree_view = view_open(0, 0, 0, view_split_begin_x(begin_x),
1127 if (tree_view == NULL)
1128 return got_error_from_errno();
1130 err = open_tree_view(tree_view, tree, entry->id, repo);
1132 got_object_tree_close(tree);
1134 *new_view = tree_view;
1138 static const struct got_error *
1139 set_child_log_view(struct tog_view *view, struct tog_view *child)
1141 struct tog_log_view_state *s = &view->state.log;
1142 struct tog_diff_view_state *ds;
1143 struct commit_queue_entry *commit, *child_entry = NULL;
1144 int selected_idx = 0;
1146 if (child->type != TOG_VIEW_DIFF)
1148 ds = &child->state.diff;
1150 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1151 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1152 child_entry = commit;
1156 if (child_entry == NULL)
1159 commit = s->first_displayed_entry;
1161 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1162 s->selected_entry = child_entry;
1163 s->selected = selected_idx;
1166 if (commit == s->last_displayed_entry)
1169 commit = TAILQ_NEXT(commit, entry);
1172 return show_log_view(view);
1175 static const struct got_error *
1176 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1177 struct got_repository *repo, const char *path)
1179 const struct got_error *err = NULL;
1180 struct tog_log_view_state *s = &view->state.log;
1182 err = got_repo_map_path(&s->in_repo_path, repo, path);
1186 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1190 /* The commit queue only contains commits being displayed. */
1191 TAILQ_INIT(&s->commits.head);
1192 s->commits.ncommits = 0;
1195 * Open the initial batch of commits, sorted in commit graph order.
1196 * We keep all commits open throughout the lifetime of the log view
1197 * in order to avoid having to re-fetch commits from disk while
1198 * updating the display.
1200 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1201 repo, s->in_repo_path);
1203 if (err->code != GOT_ERR_ITER_COMPLETED)
1208 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1209 s->selected_entry = s->first_displayed_entry;
1211 s->start_id = got_object_id_dup(start_id);
1212 if (s->start_id == NULL) {
1213 err = got_error_from_errno();
1217 view->show = show_log_view;
1218 view->update_siblings = update_siblings_log_view;
1219 view->input = input_log_view;
1220 view->close = close_log_view;
1221 view->set_child = set_child_log_view;
1226 static const struct got_error *
1227 close_log_view(struct tog_view *view)
1229 struct tog_log_view_state *s = &view->state.log;
1232 got_commit_graph_close(s->graph);
1233 free_commits(&s->commits);
1234 free(s->in_repo_path);
1239 static const struct got_error *
1240 update_diff_view(struct tog_view *diff_view,
1241 struct got_object_id *commit_id, struct got_commit_object *commit,
1242 struct got_repository *repo)
1244 const struct got_error *err = NULL;
1245 struct tog_diff_view_state *ds;
1246 struct got_object *obj1 = NULL, *obj2 = NULL;
1247 struct got_object_qid *parent_id;
1249 ds = &diff_view->state.diff;
1250 if (got_object_id_cmp(ds->id2, commit_id) == 0)
1253 err = got_object_open(&obj2, repo, commit_id);
1257 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1259 err = got_object_open(&obj1, repo, parent_id->id);
1264 err = close_diff_view(diff_view);
1268 err = open_diff_view(diff_view, obj1, obj2, repo);
1273 got_object_close(obj1);
1275 got_object_close(obj2);
1279 static const struct got_error *
1280 show_log_view(struct tog_view *view)
1282 const struct got_error *err = NULL;
1283 struct tog_log_view_state *s = &view->state.log;
1285 return draw_commits(view, &s->last_displayed_entry,
1286 &s->selected_entry, s->first_displayed_entry,
1287 &s->commits, s->selected, view->nlines, s->graph,
1288 s->repo, s->in_repo_path);
1293 static const struct got_error *
1294 update_siblings_log_view(int *updated, struct tog_view *view,
1295 struct tog_view *sibling_view)
1297 const struct got_error *err = NULL;
1298 struct tog_log_view_state *s = &view->state.log;
1300 switch (sibling_view->type) {
1302 err = update_diff_view(sibling_view, s->selected_entry->id,
1303 s->selected_entry->commit, s->repo);
1313 static const struct got_error *
1314 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1315 struct tog_view *view, int ch)
1317 const struct got_error *err = NULL;
1318 struct tog_log_view_state *s = &view->state.log;
1325 if (s->selected > 0)
1327 if (s->selected > 0)
1329 scroll_up(&s->first_displayed_entry, 1,
1333 if (TAILQ_FIRST(&s->commits.head) ==
1334 s->first_displayed_entry) {
1338 scroll_up(&s->first_displayed_entry,
1339 view->nlines, &s->commits);
1344 if (s->selected < MIN(view->nlines - 2,
1345 s->commits.ncommits - 1)) {
1349 err = scroll_down(&s->first_displayed_entry, 1,
1350 &s->last_displayed_entry, &s->commits,
1351 s->graph, s->repo, s->in_repo_path);
1353 if (err->code != GOT_ERR_ITER_COMPLETED)
1359 struct commit_queue_entry *first;
1360 first = s->first_displayed_entry;
1361 err = scroll_down(&s->first_displayed_entry,
1362 view->nlines, &s->last_displayed_entry,
1363 &s->commits, s->graph, s->repo,
1365 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1367 if (first == s->first_displayed_entry &&
1368 s->selected < MIN(view->nlines - 2,
1369 s->commits.ncommits - 1)) {
1370 /* can't scroll further down */
1371 s->selected = MIN(view->nlines - 2,
1372 s->commits.ncommits - 1);
1378 if (s->selected > view->nlines - 2)
1379 s->selected = view->nlines - 2;
1380 if (s->selected > s->commits.ncommits - 1)
1381 s->selected = s->commits.ncommits - 1;
1385 err = open_diff_view_for_commit(new_view, view->begin_x,
1386 s->selected_entry->id, s->selected_entry->commit,
1390 err = browse_commit(new_view, view->begin_x,
1391 s->selected_entry, s->repo);
1394 if (strcmp(s->in_repo_path, "/") == 0)
1396 parent_path = dirname(s->in_repo_path);
1397 if (parent_path && strcmp(parent_path, ".") != 0) {
1398 struct tog_view *lv;
1399 lv = view_open(view->nlines, view->ncols,
1400 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1402 return got_error_from_errno();
1403 err = open_log_view(lv, s->start_id, s->repo,
1417 static const struct got_error *
1418 cmd_log(int argc, char *argv[])
1420 const struct got_error *error;
1421 struct got_repository *repo = NULL;
1422 struct got_object_id *start_id = NULL;
1423 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1424 char *start_commit = NULL;
1426 struct tog_view *view;
1429 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1437 start_commit = optarg;
1440 repo_path = realpath(optarg, NULL);
1441 if (repo_path == NULL)
1442 err(1, "-r option");
1456 path = strdup(argv[0]);
1460 return got_error_from_errno();
1462 cwd = getcwd(NULL, 0);
1464 error = got_error_from_errno();
1467 if (repo_path == NULL) {
1468 repo_path = strdup(cwd);
1469 if (repo_path == NULL) {
1470 error = got_error_from_errno();
1475 error = got_repo_open(&repo, repo_path);
1479 if (start_commit == NULL) {
1480 error = get_head_commit_id(&start_id, repo);
1484 struct got_object *obj;
1485 error = got_object_open_by_id_str(&obj, repo, start_commit);
1486 if (error == NULL) {
1487 start_id = got_object_id_dup(got_object_get_id(obj));
1488 if (start_id == NULL)
1489 error = got_error_from_errno();
1496 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1498 error = got_error_from_errno();
1501 error = open_log_view(view, start_id, repo, path);
1504 error = view_loop(view);
1511 got_repo_close(repo);
1519 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1525 parse_next_line(FILE *f, size_t *len)
1530 const char delim[3] = { '\0', '\0', '\0'};
1532 line = fparseln(f, &linelen, &lineno, delim, 0);
1538 static const struct got_error *
1539 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1540 int *last_displayed_line, int *eof, int max_lines,
1543 const struct got_error *err;
1544 int nlines = 0, nprinted = 0;
1551 werase(view->window);
1554 err = format_line(&wline, &width, header, view->ncols);
1559 if (view_needs_focus_indication(view))
1560 wstandout(view->window);
1561 waddwstr(view->window, wline);
1562 if (view_needs_focus_indication(view))
1563 wstandend(view->window);
1564 if (width < view->ncols)
1565 waddch(view->window, '\n');
1573 while (nprinted < max_lines) {
1574 line = parse_next_line(f, &len);
1579 if (++nlines < *first_displayed_line) {
1584 err = format_line(&wline, &width, line, view->ncols);
1589 waddwstr(view->window, wline);
1590 if (width < view->ncols)
1591 waddch(view->window, '\n');
1592 if (++nprinted == 1)
1593 *first_displayed_line = nlines;
1598 *last_displayed_line = nlines;
1605 static const struct got_error *
1606 open_diff_view(struct tog_view *view, struct got_object *obj1,
1607 struct got_object *obj2, struct got_repository *repo)
1609 const struct got_error *err;
1612 if (obj1 != NULL && obj2 != NULL &&
1613 got_object_get_type(obj1) != got_object_get_type(obj2))
1614 return got_error(GOT_ERR_OBJ_TYPE);
1618 return got_error_from_errno();
1620 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1621 case GOT_OBJ_TYPE_BLOB:
1622 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1625 case GOT_OBJ_TYPE_TREE:
1626 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1628 case GOT_OBJ_TYPE_COMMIT:
1629 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1632 return got_error(GOT_ERR_OBJ_TYPE);
1637 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1638 view->state.diff.id2 = got_object_get_id(obj2);
1639 view->state.diff.f = f;
1640 view->state.diff.first_displayed_line = 1;
1641 view->state.diff.last_displayed_line = view->nlines;
1643 view->show = show_diff_view;
1644 view->update_siblings = update_siblings_diff_view;
1645 view->input = input_diff_view;
1646 view->close = close_diff_view;
1651 static const struct got_error *
1652 close_diff_view(struct tog_view *view)
1654 const struct got_error *err = NULL;
1656 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1657 err = got_error_from_errno();
1661 static const struct got_error *
1662 show_diff_view(struct tog_view *view)
1664 const struct got_error *err;
1665 struct tog_diff_view_state *s = &view->state.diff;
1666 char *id_str1 = NULL, *id_str2, *header;
1669 err = got_object_id_str(&id_str1, s->id1);
1673 err = got_object_id_str(&id_str2, s->id2);
1677 if (asprintf(&header, "diff: %s %s",
1678 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1679 err = got_error_from_errno();
1687 return draw_file(view, s->f, &s->first_displayed_line,
1688 &s->last_displayed_line, &s->eof, view->nlines,
1692 static const struct got_error *
1693 update_siblings_diff_view(int *updated, struct tog_view *view,
1694 struct tog_view *sibling_view)
1699 static const struct got_error *
1700 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1701 struct tog_view *view, int ch)
1703 const struct got_error *err = NULL;
1704 struct tog_diff_view_state *s = &view->state.diff;
1710 if (s->first_displayed_line > 1)
1711 s->first_displayed_line--;
1715 while (i++ < view->nlines - 1 &&
1716 s->first_displayed_line > 1)
1717 s->first_displayed_line--;
1722 s->first_displayed_line++;
1727 while (!s->eof && i++ < view->nlines - 1) {
1729 line = parse_next_line(s->f, NULL);
1730 s->first_displayed_line++;
1742 static const struct got_error *
1743 cmd_diff(int argc, char *argv[])
1745 const struct got_error *error = NULL;
1746 struct got_repository *repo = NULL;
1747 struct got_object *obj1 = NULL, *obj2 = NULL;
1748 char *repo_path = NULL;
1749 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1751 struct tog_view *view;
1754 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1759 while ((ch = getopt(argc, argv, "")) != -1) {
1771 usage_diff(); /* TODO show local worktree changes */
1772 } else if (argc == 2) {
1773 repo_path = getcwd(NULL, 0);
1774 if (repo_path == NULL)
1775 return got_error_from_errno();
1776 obj_id_str1 = argv[0];
1777 obj_id_str2 = argv[1];
1778 } else if (argc == 3) {
1779 repo_path = realpath(argv[0], NULL);
1780 if (repo_path == NULL)
1781 return got_error_from_errno();
1782 obj_id_str1 = argv[1];
1783 obj_id_str2 = argv[2];
1787 error = got_repo_open(&repo, repo_path);
1792 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1796 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1800 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1802 error = got_error_from_errno();
1805 error = open_diff_view(view, obj1, obj2, repo);
1808 error = view_loop(view);
1810 got_repo_close(repo);
1812 got_object_close(obj1);
1814 got_object_close(obj2);
1822 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1827 struct tog_blame_line {
1829 struct got_object_id *id;
1832 static const struct got_error *
1833 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1834 const char *path, struct tog_blame_line *lines, int nlines,
1835 int blame_complete, int selected_line, int *first_displayed_line,
1836 int *last_displayed_line, int *eof, int max_lines)
1838 const struct got_error *err;
1839 int lineno = 0, nprinted = 0;
1844 struct tog_blame_line *blame_line;
1845 struct got_object_id *prev_id = NULL;
1848 err = got_object_id_str(&id_str, id);
1853 werase(view->window);
1855 if (asprintf(&line, "commit: %s", id_str) == -1) {
1856 err = got_error_from_errno();
1861 err = format_line(&wline, &width, line, view->ncols);
1864 if (view_needs_focus_indication(view))
1865 wstandout(view->window);
1866 waddwstr(view->window, wline);
1867 if (view_needs_focus_indication(view))
1868 wstandend(view->window);
1871 if (width < view->ncols)
1872 waddch(view->window, '\n');
1874 if (asprintf(&line, "[%d/%d] %s%s",
1875 *first_displayed_line - 1 + selected_line, nlines,
1876 blame_complete ? "" : "annotating ", path) == -1) {
1878 return got_error_from_errno();
1881 err = format_line(&wline, &width, line, view->ncols);
1886 waddwstr(view->window, wline);
1889 if (width < view->ncols)
1890 waddch(view->window, '\n');
1893 while (nprinted < max_lines - 2) {
1894 line = parse_next_line(f, &len);
1899 if (++lineno < *first_displayed_line) {
1904 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1905 err = format_line(&wline, &width, line, wlimit);
1911 if (view->focussed && nprinted == selected_line - 1)
1912 wstandout(view->window);
1914 blame_line = &lines[lineno - 1];
1915 if (blame_line->annotated && prev_id &&
1916 got_object_id_cmp(prev_id, blame_line->id) == 0)
1917 waddstr(view->window, " ");
1918 else if (blame_line->annotated) {
1920 err = got_object_id_str(&id_str, blame_line->id);
1926 wprintw(view->window, "%.8s ", id_str);
1928 prev_id = blame_line->id;
1930 waddstr(view->window, "........ ");
1934 waddwstr(view->window, wline);
1935 while (width < wlimit) {
1936 waddch(view->window, ' ');
1939 if (view->focussed && nprinted == selected_line - 1)
1940 wstandend(view->window);
1941 if (++nprinted == 1)
1942 *first_displayed_line = lineno;
1947 *last_displayed_line = lineno;
1954 static const struct got_error *
1955 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1957 const struct got_error *err = NULL;
1958 struct tog_blame_cb_args *a = arg;
1959 struct tog_blame_line *line;
1961 if (nlines != a->nlines ||
1962 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1963 return got_error(GOT_ERR_RANGE);
1965 if (pthread_mutex_lock(a->mutex) != 0)
1966 return got_error_from_errno();
1968 if (*a->quit) { /* user has quit the blame view */
1969 err = got_error(GOT_ERR_ITER_COMPLETED);
1974 goto done; /* no change in this commit */
1976 line = &a->lines[lineno - 1];
1977 if (line->annotated)
1980 line->id = got_object_id_dup(id);
1981 if (line->id == NULL) {
1982 err = got_error_from_errno();
1985 line->annotated = 1;
1987 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1988 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1989 a->last_displayed_line, a->eof, a->view->nlines);
1991 if (pthread_mutex_unlock(a->mutex) != 0)
1992 return got_error_from_errno();
1997 blame_thread(void *arg)
1999 const struct got_error *err;
2000 struct tog_blame_thread_args *ta = arg;
2001 struct tog_blame_cb_args *a = ta->cb_args;
2003 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2004 blame_cb, ta->cb_args);
2006 if (pthread_mutex_lock(a->mutex) != 0)
2007 return (void *)got_error_from_errno();
2009 got_repo_close(ta->repo);
2013 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2014 a->lines, a->nlines, 1, *a->selected_line,
2015 a->first_displayed_line, a->last_displayed_line, a->eof,
2018 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
2019 err = got_error_from_errno();
2024 static struct got_object_id *
2025 get_selected_commit_id(struct tog_blame_line *lines,
2026 int first_displayed_line, int selected_line)
2028 struct tog_blame_line *line;
2030 line = &lines[first_displayed_line - 1 + selected_line - 1];
2031 if (!line->annotated)
2037 static const struct got_error *
2038 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2039 struct tog_blame_line *lines, int first_displayed_line,
2040 int selected_line, struct got_repository *repo)
2042 const struct got_error *err = NULL;
2043 struct got_commit_object *commit = NULL;
2044 struct got_object_id *selected_id;
2045 struct got_object_qid *pid;
2050 selected_id = get_selected_commit_id(lines,
2051 first_displayed_line, selected_line);
2052 if (selected_id == NULL)
2055 err = got_object_open(obj, repo, selected_id);
2059 err = got_object_commit_open(&commit, repo, *obj);
2063 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2065 err = got_object_open(pobj, repo, pid->id);
2071 got_object_commit_close(commit);
2075 static const struct got_error *
2076 stop_blame(struct tog_blame *blame)
2078 const struct got_error *err = NULL;
2081 if (blame->thread) {
2082 if (pthread_join(blame->thread, (void **)&err) != 0)
2083 err = got_error_from_errno();
2084 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2086 blame->thread = NULL;
2088 if (blame->thread_args.repo) {
2089 got_repo_close(blame->thread_args.repo);
2090 blame->thread_args.repo = NULL;
2096 for (i = 0; i < blame->nlines; i++)
2097 free(blame->lines[i].id);
2099 blame->lines = NULL;
2100 free(blame->cb_args.commit_id);
2101 blame->cb_args.commit_id = NULL;
2106 static const struct got_error *
2107 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2108 struct tog_view *view, int *blame_complete,
2109 int *first_displayed_line, int *last_displayed_line,
2110 int *selected_line, int *done, int *eof, const char *path,
2111 struct got_object_id *commit_id,
2112 struct got_repository *repo)
2114 const struct got_error *err = NULL;
2115 struct got_blob_object *blob = NULL;
2116 struct got_repository *thread_repo = NULL;
2117 struct got_object_id *obj_id = NULL;
2118 struct got_object *obj = NULL;
2120 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2124 err = got_object_open(&obj, repo, obj_id);
2128 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2129 err = got_error(GOT_ERR_OBJ_TYPE);
2133 err = got_object_blob_open(&blob, repo, obj, 8192);
2136 blame->f = got_opentemp();
2137 if (blame->f == NULL) {
2138 err = got_error_from_errno();
2141 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2146 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2147 if (blame->lines == NULL) {
2148 err = got_error_from_errno();
2152 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2156 blame->cb_args.view = view;
2157 blame->cb_args.lines = blame->lines;
2158 blame->cb_args.nlines = blame->nlines;
2159 blame->cb_args.mutex = mutex;
2160 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2161 if (blame->cb_args.commit_id == NULL) {
2162 err = got_error_from_errno();
2165 blame->cb_args.f = blame->f;
2166 blame->cb_args.path = path;
2167 blame->cb_args.first_displayed_line = first_displayed_line;
2168 blame->cb_args.selected_line = selected_line;
2169 blame->cb_args.last_displayed_line = last_displayed_line;
2170 blame->cb_args.quit = done;
2171 blame->cb_args.eof = eof;
2173 blame->thread_args.path = path;
2174 blame->thread_args.repo = thread_repo;
2175 blame->thread_args.cb_args = &blame->cb_args;
2176 blame->thread_args.complete = blame_complete;
2177 *blame_complete = 0;
2179 if (pthread_create(&blame->thread, NULL, blame_thread,
2180 &blame->thread_args) != 0) {
2181 err = got_error_from_errno();
2187 got_object_blob_close(blob);
2190 got_object_close(obj);
2196 static const struct got_error *
2197 open_blame_view(struct tog_view *view, char *path,
2198 struct got_object_id *commit_id, struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 struct tog_blame_view_state *s = &view->state.blame;
2203 SIMPLEQ_INIT(&s->blamed_commits);
2205 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2206 return got_error_from_errno();
2208 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2212 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2213 s->first_displayed_line = 1;
2214 s->last_displayed_line = view->nlines;
2215 s->selected_line = 1;
2216 s->blame_complete = 0;
2218 if (s->path == NULL)
2219 return got_error_from_errno();
2221 s->commit_id = commit_id;
2222 memset(&s->blame, 0, sizeof(s->blame));
2224 view->show = show_blame_view;
2225 view->update_siblings = update_siblings_blame_view;
2226 view->input = input_blame_view;
2227 view->close = close_blame_view;
2229 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2230 &s->first_displayed_line, &s->last_displayed_line,
2231 &s->selected_line, &s->done, &s->eof, s->path,
2232 s->blamed_commit->id, s->repo);
2235 static const struct got_error *
2236 close_blame_view(struct tog_view *view)
2238 const struct got_error *err = NULL;
2239 struct tog_blame_view_state *s = &view->state.blame;
2241 if (s->blame.thread)
2242 err = stop_blame(&s->blame);
2244 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2245 struct got_object_qid *blamed_commit;
2246 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2247 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2248 got_object_qid_free(blamed_commit);
2256 static const struct got_error *
2257 show_blame_view(struct tog_view *view)
2259 const struct got_error *err = NULL;
2260 struct tog_blame_view_state *s = &view->state.blame;
2262 if (pthread_mutex_lock(&s->mutex) != 0)
2263 return got_error_from_errno();
2265 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2266 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2267 s->selected_line, &s->first_displayed_line,
2268 &s->last_displayed_line, &s->eof, view->nlines);
2270 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2271 err = got_error_from_errno();
2276 static const struct got_error *
2277 update_siblings_blame_view(int *updated, struct tog_view *view,
2278 struct tog_view *sibling_view)
2283 static const struct got_error *
2284 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2285 struct tog_view *view, int ch)
2287 const struct got_error *err = NULL, *thread_err = NULL;
2288 struct got_object *obj = NULL, *pobj = NULL;
2289 struct tog_view *diff_view;
2290 struct tog_blame_view_state *s = &view->state.blame;
2292 if (pthread_mutex_lock(&s->mutex) != 0) {
2293 err = got_error_from_errno();
2300 if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2304 return stop_blame(&s->blame);
2307 if (s->selected_line > 1)
2309 else if (s->selected_line == 1 &&
2310 s->first_displayed_line > 1)
2311 s->first_displayed_line--;
2314 if (s->first_displayed_line == 1) {
2315 s->selected_line = 1;
2318 if (s->first_displayed_line > view->nlines - 2)
2319 s->first_displayed_line -=
2322 s->first_displayed_line = 1;
2326 if (s->selected_line < view->nlines - 2 &&
2327 s->first_displayed_line +
2328 s->selected_line <= s->blame.nlines)
2330 else if (s->last_displayed_line <
2332 s->first_displayed_line++;
2336 struct got_object_id *id;
2337 id = get_selected_commit_id(s->blame.lines,
2338 s->first_displayed_line, s->selected_line);
2339 if (id == NULL || got_object_id_cmp(id,
2340 s->blamed_commit->id) == 0)
2342 err = open_selected_commit(&pobj, &obj,
2343 s->blame.lines, s->first_displayed_line,
2344 s->selected_line, s->repo);
2347 if (pobj == NULL && obj == NULL)
2349 if (ch == 'p' && pobj == NULL)
2352 if (pthread_mutex_unlock(&s->mutex) != 0) {
2353 err = got_error_from_errno();
2356 thread_err = stop_blame(&s->blame);
2358 if (pthread_mutex_lock(&s->mutex) != 0) {
2359 err = got_error_from_errno();
2364 id = got_object_get_id(ch == 'b' ? obj : pobj);
2365 got_object_close(obj);
2368 got_object_close(pobj);
2371 err = got_object_qid_alloc(&s->blamed_commit, id);
2374 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2375 s->blamed_commit, entry);
2376 err = run_blame(&s->blame, &s->mutex, view,
2378 &s->first_displayed_line,
2379 &s->last_displayed_line,
2380 &s->selected_line, &s->done, &s->eof,
2381 s->path, s->blamed_commit->id, s->repo);
2387 struct got_object_qid *first;
2388 first = SIMPLEQ_FIRST(&s->blamed_commits);
2389 if (!got_object_id_cmp(first->id, s->commit_id))
2392 if (pthread_mutex_unlock(&s->mutex) != 0) {
2393 err = got_error_from_errno();
2396 thread_err = stop_blame(&s->blame);
2398 if (pthread_mutex_lock(&s->mutex) != 0) {
2399 err = got_error_from_errno();
2404 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2405 got_object_qid_free(s->blamed_commit);
2407 SIMPLEQ_FIRST(&s->blamed_commits);
2408 err = run_blame(&s->blame, &s->mutex, view,
2410 &s->first_displayed_line,
2411 &s->last_displayed_line,
2412 &s->selected_line, &s->done, &s->eof, s->path,
2413 s->blamed_commit->id, s->repo);
2420 err = open_selected_commit(&pobj, &obj,
2421 s->blame.lines, s->first_displayed_line,
2422 s->selected_line, s->repo);
2425 if (pobj == NULL && obj == NULL)
2427 diff_view = view_open(0, 0, 0,
2428 view_split_begin_x(view->begin_x), TOG_VIEW_DIFF);
2429 if (diff_view == NULL) {
2430 err = got_error_from_errno();
2433 err = open_diff_view(diff_view, pobj, obj,
2436 view_close(diff_view);
2439 *new_view = diff_view;
2441 got_object_close(pobj);
2444 got_object_close(obj);
2451 if (s->last_displayed_line >= s->blame.nlines &&
2452 s->selected_line < view->nlines - 2) {
2453 s->selected_line = MIN(s->blame.nlines,
2457 if (s->last_displayed_line + view->nlines - 2
2459 s->first_displayed_line +=
2462 s->first_displayed_line =
2467 if (s->selected_line > view->nlines - 2) {
2468 s->selected_line = MIN(s->blame.nlines,
2476 if (pthread_mutex_unlock(&s->mutex) != 0)
2477 err = got_error_from_errno();
2480 got_object_close(pobj);
2481 return thread_err ? thread_err : err;
2484 static const struct got_error *
2485 cmd_blame(int argc, char *argv[])
2487 const struct got_error *error;
2488 struct got_repository *repo = NULL;
2489 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2490 struct got_object_id *commit_id = NULL;
2491 char *commit_id_str = NULL;
2493 struct tog_view *view;
2496 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2501 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2504 commit_id_str = optarg;
2507 repo_path = realpath(optarg, NULL);
2508 if (repo_path == NULL)
2509 err(1, "-r option");
2525 cwd = getcwd(NULL, 0);
2527 error = got_error_from_errno();
2530 if (repo_path == NULL) {
2531 repo_path = strdup(cwd);
2532 if (repo_path == NULL) {
2533 error = got_error_from_errno();
2539 error = got_repo_open(&repo, repo_path);
2543 error = got_repo_map_path(&in_repo_path, repo, path);
2547 if (commit_id_str == NULL) {
2548 struct got_reference *head_ref;
2549 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2552 error = got_ref_resolve(&commit_id, repo, head_ref);
2553 got_ref_close(head_ref);
2555 struct got_object *obj;
2556 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2559 commit_id = got_object_id_dup(got_object_get_id(obj));
2560 if (commit_id == NULL)
2561 error = got_error_from_errno();
2562 got_object_close(obj);
2567 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2569 error = got_error_from_errno();
2572 error = open_blame_view(view, in_repo_path, commit_id, repo);
2575 error = view_loop(view);
2581 got_repo_close(repo);
2585 static const struct got_error *
2586 draw_tree_entries(struct tog_view *view,
2587 struct got_tree_entry **first_displayed_entry,
2588 struct got_tree_entry **last_displayed_entry,
2589 struct got_tree_entry **selected_entry, int *ndisplayed,
2590 const char *label, int show_ids, const char *parent_path,
2591 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2593 const struct got_error *err = NULL;
2594 struct got_tree_entry *te;
2600 werase(view->window);
2605 err = format_line(&wline, &width, label, view->ncols);
2608 if (view_needs_focus_indication(view))
2609 wstandout(view->window);
2610 waddwstr(view->window, wline);
2611 if (view_needs_focus_indication(view))
2612 wstandend(view->window);
2615 if (width < view->ncols)
2616 waddch(view->window, '\n');
2619 err = format_line(&wline, &width, parent_path, view->ncols);
2622 waddwstr(view->window, wline);
2625 if (width < view->ncols)
2626 waddch(view->window, '\n');
2629 waddch(view->window, '\n');
2633 te = SIMPLEQ_FIRST(&entries->head);
2634 if (*first_displayed_entry == NULL) {
2635 if (selected == 0) {
2637 wstandout(view->window);
2638 *selected_entry = NULL;
2640 waddstr(view->window, " ..\n"); /* parent directory */
2641 if (selected == 0 && view->focussed)
2642 wstandend(view->window);
2649 while (te != *first_displayed_entry)
2650 te = SIMPLEQ_NEXT(te, entry);
2654 char *line = NULL, *id_str = NULL;
2657 err = got_object_id_str(&id_str, te->id);
2659 return got_error_from_errno();
2661 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2662 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2664 return got_error_from_errno();
2667 err = format_line(&wline, &width, line, view->ncols);
2672 if (n == selected) {
2674 wstandout(view->window);
2675 *selected_entry = te;
2677 waddwstr(view->window, wline);
2678 if (width < view->ncols)
2679 waddch(view->window, '\n');
2680 if (n == selected && view->focussed)
2681 wstandend(view->window);
2687 *last_displayed_entry = te;
2690 te = SIMPLEQ_NEXT(te, entry);
2698 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2699 const struct got_tree_entries *entries, int isroot)
2701 struct got_tree_entry *te, *prev;
2704 if (*first_displayed_entry == NULL)
2707 te = SIMPLEQ_FIRST(&entries->head);
2708 if (*first_displayed_entry == te) {
2710 *first_displayed_entry = NULL;
2714 /* XXX this is stupid... switch to TAILQ? */
2715 for (i = 0; i < maxscroll; i++) {
2716 while (te != *first_displayed_entry) {
2718 te = SIMPLEQ_NEXT(te, entry);
2720 *first_displayed_entry = prev;
2721 te = SIMPLEQ_FIRST(&entries->head);
2723 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2724 *first_displayed_entry = NULL;
2728 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2729 struct got_tree_entry *last_displayed_entry,
2730 const struct got_tree_entries *entries)
2732 struct got_tree_entry *next;
2735 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2738 if (*first_displayed_entry)
2739 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2741 next = SIMPLEQ_FIRST(&entries->head);
2743 *first_displayed_entry = next;
2744 if (++n >= maxscroll)
2746 next = SIMPLEQ_NEXT(next, entry);
2750 static const struct got_error *
2751 tree_entry_path(char **path, struct tog_parent_trees *parents,
2752 struct got_tree_entry *te)
2754 const struct got_error *err = NULL;
2755 struct tog_parent_tree *pt;
2756 size_t len = 2; /* for leading slash and NUL */
2758 TAILQ_FOREACH(pt, parents, entry)
2759 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2761 len += strlen(te->name);
2763 *path = calloc(1, len);
2765 return got_error_from_errno();
2768 pt = TAILQ_LAST(parents, tog_parent_trees);
2770 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2771 err = got_error(GOT_ERR_NO_SPACE);
2774 if (strlcat(*path, "/", len) >= len) {
2775 err = got_error(GOT_ERR_NO_SPACE);
2778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2781 if (strlcat(*path, te->name, len) >= len) {
2782 err = got_error(GOT_ERR_NO_SPACE);
2794 static const struct got_error *
2795 blame_tree_entry(struct tog_view **new_view, int begin_x,
2796 struct got_tree_entry *te, struct tog_parent_trees *parents,
2797 struct got_object_id *commit_id, struct got_repository *repo)
2799 const struct got_error *err = NULL;
2801 struct tog_view *blame_view;
2803 err = tree_entry_path(&path, parents, te);
2807 blame_view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2808 if (blame_view == NULL)
2809 return got_error_from_errno();
2811 err = open_blame_view(blame_view, path, commit_id, repo);
2813 view_close(blame_view);
2816 *new_view = blame_view;
2820 static const struct got_error *
2821 log_tree_entry(struct tog_view **new_view,
2822 struct got_tree_entry *te, struct tog_parent_trees *parents,
2823 struct got_object_id *commit_id, struct got_repository *repo)
2825 struct tog_view *log_view;
2826 const struct got_error *err = NULL;
2829 log_view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2830 if (log_view == NULL)
2831 return got_error_from_errno();
2833 err = tree_entry_path(&path, parents, te);
2837 err = open_log_view(log_view, commit_id, repo, path);
2839 view_close(log_view);
2841 *new_view = log_view;
2846 static const struct got_error *
2847 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2848 struct got_object_id *commit_id, struct got_repository *repo)
2850 const struct got_error *err = NULL;
2851 char *commit_id_str = NULL;
2852 struct tog_tree_view_state *s = &view->state.tree;
2854 TAILQ_INIT(&s->parents);
2856 err = got_object_id_str(&commit_id_str, commit_id);
2860 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2861 err = got_error_from_errno();
2865 s->root = s->tree = root;
2866 s->entries = got_object_tree_get_entries(root);
2867 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2868 s->commit_id = got_object_id_dup(commit_id);
2869 if (s->commit_id == NULL) {
2870 err = got_error_from_errno();
2875 view->show = show_tree_view;
2876 view->update_siblings = update_siblings_tree_view;
2877 view->input = input_tree_view;
2878 view->close = close_tree_view;
2880 free(commit_id_str);
2882 free(s->tree_label);
2883 s->tree_label = NULL;
2888 static const struct got_error *
2889 close_tree_view(struct tog_view *view)
2891 struct tog_tree_view_state *s = &view->state.tree;
2893 free(s->tree_label);
2894 s->tree_label = NULL;
2896 s->commit_id = NULL;
2897 while (!TAILQ_EMPTY(&s->parents)) {
2898 struct tog_parent_tree *parent;
2899 parent = TAILQ_FIRST(&s->parents);
2900 TAILQ_REMOVE(&s->parents, parent, entry);
2904 if (s->tree != s->root)
2905 got_object_tree_close(s->tree);
2906 got_object_tree_close(s->root);
2911 static const struct got_error *
2912 show_tree_view(struct tog_view *view)
2914 const struct got_error *err = NULL;
2915 struct tog_tree_view_state *s = &view->state.tree;
2918 err = tree_entry_path(&parent_path, &s->parents, NULL);
2922 err = draw_tree_entries(view, &s->first_displayed_entry,
2923 &s->last_displayed_entry, &s->selected_entry,
2924 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2925 s->entries, s->selected, view->nlines, s->tree == s->root);
2930 static const struct got_error *
2931 update_siblings_tree_view(int *updated, struct tog_view *view,
2932 struct tog_view *sibling_view)
2937 static const struct got_error *
2938 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2939 struct tog_view *view, int ch)
2941 const struct got_error *err = NULL;
2942 struct tog_tree_view_state *s = &view->state.tree;
2946 s->show_ids = !s->show_ids;
2949 if (s->selected_entry) {
2950 err = log_tree_entry(new_view,
2951 s->selected_entry, &s->parents,
2952 s->commit_id, s->repo);
2957 if (s->selected > 0)
2959 if (s->selected > 0)
2961 tree_scroll_up(&s->first_displayed_entry, 1,
2962 s->entries, s->tree == s->root);
2965 if (SIMPLEQ_FIRST(&s->entries->head) ==
2966 s->first_displayed_entry) {
2967 if (s->tree != s->root)
2968 s->first_displayed_entry = NULL;
2972 tree_scroll_up(&s->first_displayed_entry,
2973 view->nlines, s->entries,
2974 s->tree == s->root);
2978 if (s->selected < s->ndisplayed - 1) {
2982 tree_scroll_down(&s->first_displayed_entry, 1,
2983 s->last_displayed_entry, s->entries);
2986 tree_scroll_down(&s->first_displayed_entry,
2987 view->nlines, s->last_displayed_entry,
2989 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2992 /* can't scroll any further; move cursor down */
2993 if (s->selected < s->ndisplayed - 1)
2994 s->selected = s->ndisplayed - 1;
2998 if (s->selected_entry == NULL) {
2999 struct tog_parent_tree *parent;
3001 /* user selected '..' */
3002 if (s->tree == s->root)
3004 parent = TAILQ_FIRST(&s->parents);
3005 TAILQ_REMOVE(&s->parents, parent,
3007 got_object_tree_close(s->tree);
3008 s->tree = parent->tree;
3010 got_object_tree_get_entries(s->tree);
3011 s->first_displayed_entry =
3012 parent->first_displayed_entry;
3014 parent->selected_entry;
3015 s->selected = parent->selected;
3017 } else if (S_ISDIR(s->selected_entry->mode)) {
3018 struct tog_parent_tree *parent;
3019 struct got_tree_object *child;
3020 err = got_object_open_as_tree(&child,
3021 s->repo, s->selected_entry->id);
3024 parent = calloc(1, sizeof(*parent));
3025 if (parent == NULL) {
3026 err = got_error_from_errno();
3029 parent->tree = s->tree;
3030 parent->first_displayed_entry =
3031 s->first_displayed_entry;
3032 parent->selected_entry = s->selected_entry;
3033 parent->selected = s->selected;
3034 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3037 got_object_tree_get_entries(s->tree);
3039 s->first_displayed_entry = NULL;
3040 } else if (S_ISREG(s->selected_entry->mode)) {
3041 err = blame_tree_entry(new_view, view->begin_x,
3042 s->selected_entry, &s->parents,
3043 s->commit_id, s->repo);
3049 if (s->selected > view->nlines)
3050 s->selected = s->ndisplayed - 1;
3063 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3068 static const struct got_error *
3069 cmd_tree(int argc, char *argv[])
3071 const struct got_error *error;
3072 struct got_repository *repo = NULL;
3073 char *repo_path = NULL;
3074 struct got_object_id *commit_id = NULL;
3075 char *commit_id_arg = NULL;
3076 struct got_commit_object *commit = NULL;
3077 struct got_tree_object *tree = NULL;
3079 struct tog_view *view;
3082 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3087 while ((ch = getopt(argc, argv, "c:")) != -1) {
3090 commit_id_arg = optarg;
3102 repo_path = getcwd(NULL, 0);
3103 if (repo_path == NULL)
3104 return got_error_from_errno();
3105 } else if (argc == 1) {
3106 repo_path = realpath(argv[0], NULL);
3107 if (repo_path == NULL)
3108 return got_error_from_errno();
3112 error = got_repo_open(&repo, repo_path);
3117 if (commit_id_arg == NULL) {
3118 error = get_head_commit_id(&commit_id, repo);
3122 struct got_object *obj;
3123 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3124 if (error == NULL) {
3125 commit_id = got_object_id_dup(got_object_get_id(obj));
3126 if (commit_id == NULL)
3127 error = got_error_from_errno();
3133 error = got_object_open_as_commit(&commit, repo, commit_id);
3137 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3141 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3143 error = got_error_from_errno();
3146 error = open_tree_view(view, tree, commit_id, repo);
3149 error = view_loop(view);
3153 got_object_commit_close(commit);
3155 got_object_tree_close(tree);
3157 got_repo_close(repo);
3167 intrflush(stdscr, FALSE);
3168 keypad(stdscr, TRUE);
3177 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3178 "Available commands:\n", getprogname());
3179 for (i = 0; i < nitems(tog_commands); i++) {
3180 struct tog_cmd *cmd = &tog_commands[i];
3181 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3187 make_argv(const char *arg0, const char *arg1)
3190 int argc = (arg1 == NULL ? 1 : 2);
3192 argv = calloc(argc, sizeof(char *));
3195 argv[0] = strdup(arg0);
3196 if (argv[0] == NULL)
3199 argv[1] = strdup(arg1);
3200 if (argv[1] == NULL)
3208 main(int argc, char *argv[])
3210 const struct got_error *error = NULL;
3211 struct tog_cmd *cmd = NULL;
3213 char **cmd_argv = NULL;
3215 setlocale(LC_ALL, "");
3217 while ((ch = getopt(argc, argv, "h")) != -1) {
3236 /* Build an argument vector which runs a default command. */
3237 cmd = &tog_commands[0];
3238 cmd_argv = make_argv(cmd->name, NULL);
3243 /* Did the user specific a command? */
3244 for (i = 0; i < nitems(tog_commands); i++) {
3245 if (strncmp(tog_commands[i].name, argv[0],
3246 strlen(argv[0])) == 0) {
3247 cmd = &tog_commands[i];
3249 tog_commands[i].cmd_usage();
3254 /* Did the user specify a repository? */
3255 char *repo_path = realpath(argv[0], NULL);
3257 struct got_repository *repo;
3258 error = got_repo_open(&repo, repo_path);
3260 got_repo_close(repo);
3262 error = got_error_from_errno();
3265 fprintf(stderr, "%s: '%s' is not a "
3266 "known command\n", getprogname(),
3270 fprintf(stderr, "%s: '%s' is neither a known "
3271 "command nor a path to a repository\n",
3272 getprogname(), argv[0]);
3276 cmd = &tog_commands[0];
3277 cmd_argv = make_argv(cmd->name, repo_path);
3285 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3292 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);