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;
199 * We implement two types of views: parent views and child views.
201 * The 'Tab' key switches between a parent view and its child view.
202 * Child views are shown side-by-side to their parent view, provided
203 * there is enough screen estate.
205 * When a new view is opened from within a parent view, this new view
206 * becomes a child view of the parent view, replacing any existing child.
208 * When a new view is opened from within a child view, this new view
209 * becomes a parent view which will obscure the views below until the
210 * user quits the new parent view by typing 'q'.
212 * This list of views contains parent views only.
213 * Child views are only pointed to by their parent view.
215 TAILQ_HEAD(tog_view_list_head, tog_view);
218 TAILQ_ENTRY(tog_view) entry;
221 int nlines, ncols, begin_y, begin_x;
222 int lines, cols; /* copies of LINES and COLS */
224 struct tog_view *parent;
225 struct tog_view *child;
228 /* type-specific state */
229 enum tog_view_type type;
231 struct tog_diff_view_state diff;
232 struct tog_log_view_state log;
233 struct tog_blame_view_state blame;
234 struct tog_tree_view_state tree;
237 const struct got_error *(*show)(struct tog_view *);
238 const struct got_error *(*input)(struct tog_view **,
239 struct tog_view **, struct tog_view**, struct tog_view *, int);
240 const struct got_error *(*close)(struct tog_view *);
243 static const struct got_error *open_diff_view(struct tog_view *,
244 struct got_object *, struct got_object *, struct got_repository *);
245 static const struct got_error *show_diff_view(struct tog_view *);
246 static const struct got_error *input_diff_view(struct tog_view **,
247 struct tog_view **, struct tog_view **, struct tog_view *, int);
248 static const struct got_error* close_diff_view(struct tog_view *);
250 static const struct got_error *open_log_view(struct tog_view *,
251 struct got_object_id *, struct got_repository *, const char *);
252 static const struct got_error * show_log_view(struct tog_view *);
253 static const struct got_error *input_log_view(struct tog_view **,
254 struct tog_view **, struct tog_view **, struct tog_view *, int);
255 static const struct got_error *close_log_view(struct tog_view *);
257 static const struct got_error *open_blame_view(struct tog_view *, char *,
258 struct got_object_id *, struct got_repository *);
259 static const struct got_error *show_blame_view(struct tog_view *);
260 static const struct got_error *input_blame_view(struct tog_view **,
261 struct tog_view **, struct tog_view **, struct tog_view *, int);
262 static const struct got_error *close_blame_view(struct tog_view *);
264 static const struct got_error *open_tree_view(struct tog_view *,
265 struct got_tree_object *, struct got_object_id *, struct got_repository *);
266 static const struct got_error *show_tree_view(struct tog_view *);
267 static const struct got_error *input_tree_view(struct tog_view **,
268 struct tog_view **, struct tog_view **, struct tog_view *, int);
269 static const struct got_error *close_tree_view(struct tog_view *);
271 static const struct got_error *
272 view_close(struct tog_view *view)
274 const struct got_error *err = NULL;
277 view_close(view->child);
281 err = view->close(view);
283 del_panel(view->panel);
285 delwin(view->window);
290 static struct tog_view *
291 view_open(int nlines, int ncols, int begin_y, int begin_x,
292 enum tog_view_type type)
294 struct tog_view *view = calloc(1, sizeof(*view));
302 view->nlines = nlines ? nlines : LINES - begin_y;
303 view->ncols = ncols ? ncols : COLS - begin_x;
304 view->begin_y = begin_y;
305 view->begin_x = begin_x;
306 view->window = newwin(nlines, ncols, begin_y, begin_x);
307 if (view->window == NULL) {
311 view->panel = new_panel(view->window);
312 if (view->panel == NULL ||
313 set_panel_userptr(view->panel, view) != OK) {
318 keypad(view->window, TRUE);
323 view_split_begin_x(int begin_x)
327 return (COLS >= 120 ? COLS/2 : 0);
330 static const struct got_error *
331 view_resize(struct tog_view *view)
335 if (view->lines > LINES)
336 nlines = view->nlines - (view->lines - LINES);
338 nlines = view->nlines + (LINES - view->lines);
340 if (view->cols > COLS)
341 ncols = view->ncols - (view->cols - COLS);
343 ncols = view->ncols + (COLS - view->cols);
345 if (wresize(view->window, nlines, ncols) == ERR)
346 return got_error_from_errno();
347 replace_panel(view->panel, view->window);
349 view->nlines = nlines;
358 view_is_parent_view(struct tog_view *view)
360 return view->parent == NULL;
363 static const struct got_error *
364 view_close_child(struct tog_view *view)
366 const struct got_error *err;
368 if (view->child == NULL)
371 err = view_close(view->child);
376 static const struct got_error *
377 view_set_child(struct tog_view *view, struct tog_view *child)
379 const struct got_error *err = NULL;
382 child->parent = view;
387 view_is_splitscreen(struct tog_view *view)
389 return !view_is_parent_view(view) && view->begin_x > 0;
392 static const struct got_error *
393 view_splitscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
398 view->begin_x = view_split_begin_x(0);
399 view->nlines = LINES;
400 view->ncols = COLS - view->begin_x;
403 err = view_resize(view);
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno();
413 static const struct got_error *
414 view_fullscreen(struct tog_view *view)
416 const struct got_error *err = NULL;
420 view->nlines = LINES;
424 err = view_resize(view);
428 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 return got_error_from_errno();
434 static const struct got_error *
435 view_input(struct tog_view **new, struct tog_view **dead,
436 struct tog_view **focus, int *done, struct tog_view *view,
437 struct tog_view_list_head *views)
439 const struct got_error *err = NULL;
447 nodelay(stdscr, FALSE);
448 ch = wgetch(view->window);
449 nodelay(stdscr, TRUE);
455 *focus = view->child;
456 view->child_focussed = 1;
457 } else if (view->parent) {
458 *focus = view->parent;
459 view->parent->child_focussed = 0;
463 err = view->input(new, dead, focus, view, ch);
470 if (view_is_parent_view(view)) {
471 if (view->child == NULL)
473 if (view_is_splitscreen(view->child)) {
474 *focus = view->child;
475 view->child_focussed = 1;
476 err = view_fullscreen(view->child);
478 err = view_splitscreen(view->child);
481 err = view->child->input(new, dead, focus,
482 view->child, KEY_RESIZE);
484 if (view_is_splitscreen(view)) {
486 view->parent->child_focussed = 1;
487 err = view_fullscreen(view);
489 err = view_splitscreen(view);
493 err = view->input(new, dead, focus, view,
498 TAILQ_FOREACH(v, views, entry) {
499 err = view_resize(v);
502 err = v->input(new, dead, focus, v, ch);
506 err = view->input(new, dead, focus, view, ch);
514 view_vborder(struct tog_view *view)
517 struct tog_view *view_above;
520 return view_vborder(view->parent);
522 panel = panel_above(view->panel);
526 view_above = panel_userptr(panel);
527 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
528 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
532 view_needs_focus_indication(struct tog_view *view)
534 if (view_is_parent_view(view)) {
535 if (view->child == NULL || view->child_focussed)
537 if (!view_is_splitscreen(view->child))
539 } else if (!view_is_splitscreen(view))
542 return view->focussed;
545 static const struct got_error *
546 view_loop(struct tog_view *view)
548 const struct got_error *err = NULL;
549 struct tog_view_list_head views;
550 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
554 TAILQ_INSERT_HEAD(&views, view, entry);
558 err = view->show(view);
563 while (!TAILQ_EMPTY(&views) && !done) {
564 err = view_input(&new_view, &dead_view, &focus_view, &done,
569 struct tog_view *prev = NULL;
571 if (view_is_parent_view(dead_view))
572 prev = TAILQ_PREV(dead_view,
573 tog_view_list_head, entry);
577 if (dead_view->parent)
578 dead_view->parent->child = NULL;
580 TAILQ_REMOVE(&views, dead_view, entry);
582 err = view_close(dead_view);
583 if (err || dead_view == main_view)
586 if (view == dead_view) {
591 else if (!TAILQ_EMPTY(&views))
592 view = TAILQ_LAST(&views,
597 if (view->child && view->child_focussed)
598 focus_view = view->child;
605 TAILQ_INSERT_TAIL(&views, new_view, entry);
606 if (focus_view == NULL)
607 focus_view = new_view;
610 show_panel(focus_view->panel);
613 focus_view->focussed = 1;
616 show_panel(new_view->panel);
617 if (view->child && view_is_splitscreen(view->child))
618 show_panel(view->child->panel);
622 err = view->parent->show(view->parent);
626 err = view->show(view);
630 err = view->child->show(view->child);
639 while (!TAILQ_EMPTY(&views)) {
640 view = TAILQ_FIRST(&views);
641 TAILQ_REMOVE(&views, view, entry);
652 "usage: %s log [-c commit] [-r repository-path] [path]\n",
657 /* Create newly allocated wide-character string equivalent to a byte string. */
658 static const struct got_error *
659 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
662 const struct got_error *err = NULL;
665 *wlen = mbstowcs(NULL, s, 0);
666 if (*wlen == (size_t)-1) {
669 return got_error_from_errno();
671 /* byte string invalid in current encoding; try to "fix" it */
672 err = got_mbsavis(&vis, &vislen, s);
675 *wlen = mbstowcs(NULL, vis, 0);
676 if (*wlen == (size_t)-1) {
677 err = got_error_from_errno(); /* give up */
682 *ws = calloc(*wlen + 1, sizeof(*ws));
684 err = got_error_from_errno();
688 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
689 err = got_error_from_errno();
700 /* Format a line for display, ensuring that it won't overflow a width limit. */
701 static const struct got_error *
702 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
704 const struct got_error *err = NULL;
706 wchar_t *wline = NULL;
713 err = mbs2ws(&wline, &wlen, line);
718 while (i < wlen && cols < wlimit) {
719 int width = wcwidth(wline[i]);
726 if (cols + width <= wlimit)
731 if (wline[i] == L'\t')
732 cols += TABSIZE - ((cols + 1) % TABSIZE);
736 err = got_error_from_errno();
751 static const struct got_error *
752 draw_commit(struct tog_view *view, struct got_commit_object *commit,
753 struct got_object_id *id)
755 const struct got_error *err = NULL;
756 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
757 char *logmsg0 = NULL, *logmsg = NULL;
758 char *author0 = NULL, *author = NULL;
759 wchar_t *wlogmsg = NULL, *wauthor = NULL;
760 int author_width, logmsg_width;
761 char *newline, *smallerthan;
764 static const size_t date_display_cols = 9;
765 static const size_t author_display_cols = 16;
766 const int avail = view->ncols;
768 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
769 &commit->tm_committer) >= sizeof(datebuf))
770 return got_error(GOT_ERR_NO_SPACE);
772 if (avail < date_display_cols)
773 limit = MIN(sizeof(datebuf) - 1, avail);
775 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
776 waddnstr(view->window, datebuf, limit);
781 author0 = strdup(commit->author);
782 if (author0 == NULL) {
783 err = got_error_from_errno();
787 smallerthan = strchr(author, '<');
791 char *at = strchr(author, '@');
796 err = format_line(&wauthor, &author_width, author, limit);
799 waddwstr(view->window, wauthor);
801 while (col <= avail && author_width < author_display_cols + 1) {
802 waddch(view->window, ' ');
809 logmsg0 = strdup(commit->logmsg);
810 if (logmsg0 == NULL) {
811 err = got_error_from_errno();
815 while (*logmsg == '\n')
817 newline = strchr(logmsg, '\n');
821 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
824 waddwstr(view->window, wlogmsg);
826 while (col <= avail) {
827 waddch(view->window, ' ');
839 static struct commit_queue_entry *
840 alloc_commit_queue_entry(struct got_commit_object *commit,
841 struct got_object_id *id)
843 struct commit_queue_entry *entry;
845 entry = calloc(1, sizeof(*entry));
850 entry->commit = commit;
855 pop_commit(struct commit_queue *commits)
857 struct commit_queue_entry *entry;
859 entry = TAILQ_FIRST(&commits->head);
860 TAILQ_REMOVE(&commits->head, entry, entry);
861 got_object_commit_close(entry->commit);
863 /* Don't free entry->id! It is owned by the commit graph. */
868 free_commits(struct commit_queue *commits)
870 while (!TAILQ_EMPTY(&commits->head))
874 static const struct got_error *
875 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
876 struct got_object_id *start_id, int minqueue,
877 struct got_repository *repo, const char *path)
879 const struct got_error *err = NULL;
883 err = got_commit_graph_iter_start(graph, start_id, repo);
888 while (nqueued < minqueue) {
889 struct got_object_id *id;
890 struct got_commit_object *commit;
891 struct commit_queue_entry *entry;
893 err = got_commit_graph_iter_next(&id, graph);
895 if (err->code == GOT_ERR_ITER_COMPLETED) {
899 if (err->code != GOT_ERR_ITER_NEED_MORE)
901 err = got_commit_graph_fetch_commits(graph,
911 err = got_object_open_as_commit(&commit, repo, id);
914 entry = alloc_commit_queue_entry(commit, id);
916 err = got_error_from_errno();
920 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
928 static const struct got_error *
929 fetch_next_commit(struct commit_queue_entry **pentry,
930 struct commit_queue_entry *entry, struct commit_queue *commits,
931 struct got_commit_graph *graph, struct got_repository *repo,
934 const struct got_error *err = NULL;
938 err = queue_commits(graph, commits, NULL, 1, repo, path);
942 /* Next entry to display should now be available. */
943 *pentry = TAILQ_NEXT(entry, entry);
947 static const struct got_error *
948 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
950 const struct got_error *err = NULL;
951 struct got_reference *head_ref;
955 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
959 err = got_ref_resolve(head_id, repo, head_ref);
960 got_ref_close(head_ref);
969 static const struct got_error *
970 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
971 struct commit_queue_entry **selected, struct commit_queue_entry *first,
972 struct commit_queue *commits, int selected_idx, int limit,
973 struct got_commit_graph *graph, struct got_repository *repo,
976 const struct got_error *err = NULL;
977 struct commit_queue_entry *entry;
979 char *id_str, *header;
985 if (ncommits == selected_idx) {
989 entry = TAILQ_NEXT(entry, entry);
993 err = got_object_id_str(&id_str, (*selected)->id);
997 if (path && strcmp(path, "/") != 0) {
998 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
999 err = got_error_from_errno();
1003 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
1004 err = got_error_from_errno();
1009 err = format_line(&wline, &width, header, view->ncols);
1016 werase(view->window);
1018 if (view_needs_focus_indication(view))
1019 wstandout(view->window);
1020 waddwstr(view->window, wline);
1021 if (view_needs_focus_indication(view))
1022 wstandend(view->window);
1023 if (width < view->ncols)
1024 waddch(view->window, '\n');
1033 if (ncommits >= limit - 1)
1035 if (view->focussed && ncommits == selected_idx)
1036 wstandout(view->window);
1037 err = draw_commit(view, entry->commit, entry->id);
1038 if (view->focussed && ncommits == selected_idx)
1039 wstandend(view->window);
1044 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
1045 err = queue_commits(graph, commits, NULL, 1,
1048 if (err->code != GOT_ERR_ITER_COMPLETED)
1053 entry = TAILQ_NEXT(entry, entry);
1062 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1063 struct commit_queue *commits)
1065 struct commit_queue_entry *entry;
1068 entry = TAILQ_FIRST(&commits->head);
1069 if (*first_displayed_entry == entry)
1072 entry = *first_displayed_entry;
1073 while (entry && nscrolled < maxscroll) {
1074 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1076 *first_displayed_entry = entry;
1082 static const struct got_error *
1083 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1084 struct commit_queue_entry **last_displayed_entry,
1085 struct commit_queue *commits, struct got_commit_graph *graph,
1086 struct got_repository *repo, const char *path)
1088 const struct got_error *err = NULL;
1089 struct commit_queue_entry *pentry;
1093 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1094 if (pentry == NULL) {
1095 err = fetch_next_commit(&pentry, *last_displayed_entry,
1096 commits, graph, repo, path);
1097 if (err || pentry == NULL)
1100 *last_displayed_entry = pentry;
1102 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1105 *first_displayed_entry = pentry;
1106 } while (++nscrolled < maxscroll);
1111 static const struct got_error *
1112 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1113 struct got_object_id *commit_id, struct got_commit_object *commit,
1114 struct got_repository *repo)
1116 const struct got_error *err;
1117 struct got_object *obj1 = NULL, *obj2 = NULL;
1118 struct got_object_qid *parent_id;
1119 struct tog_view *diff_view;
1121 err = got_object_open(&obj2, repo, commit_id);
1125 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1127 err = got_object_open(&obj1, repo, parent_id->id);
1132 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1133 if (diff_view == NULL) {
1134 err = got_error_from_errno();
1138 err = open_diff_view(diff_view, obj1, obj2, repo);
1140 *new_view = diff_view;
1143 got_object_close(obj1);
1145 got_object_close(obj2);
1149 static const struct got_error *
1150 browse_commit(struct tog_view **new_view, int begin_x,
1151 struct commit_queue_entry *entry, struct got_repository *repo)
1153 const struct got_error *err = NULL;
1154 struct got_tree_object *tree;
1155 struct tog_view *tree_view;
1157 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1161 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1162 if (tree_view == NULL)
1163 return got_error_from_errno();
1165 err = open_tree_view(tree_view, tree, entry->id, repo);
1167 got_object_tree_close(tree);
1169 *new_view = tree_view;
1173 static const struct got_error *
1174 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1175 struct got_repository *repo, const char *path)
1177 const struct got_error *err = NULL;
1178 struct tog_log_view_state *s = &view->state.log;
1180 err = got_repo_map_path(&s->in_repo_path, repo, path);
1184 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1188 /* The commit queue only contains commits being displayed. */
1189 TAILQ_INIT(&s->commits.head);
1190 s->commits.ncommits = 0;
1193 * Open the initial batch of commits, sorted in commit graph order.
1194 * We keep all commits open throughout the lifetime of the log view
1195 * in order to avoid having to re-fetch commits from disk while
1196 * updating the display.
1198 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1199 repo, s->in_repo_path);
1201 if (err->code != GOT_ERR_ITER_COMPLETED)
1206 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1207 s->selected_entry = s->first_displayed_entry;
1209 s->start_id = got_object_id_dup(start_id);
1210 if (s->start_id == NULL) {
1211 err = got_error_from_errno();
1215 view->show = show_log_view;
1216 view->input = input_log_view;
1217 view->close = close_log_view;
1222 static const struct got_error *
1223 close_log_view(struct tog_view *view)
1225 struct tog_log_view_state *s = &view->state.log;
1228 got_commit_graph_close(s->graph);
1229 free_commits(&s->commits);
1230 free(s->in_repo_path);
1235 static const struct got_error *
1236 show_log_view(struct tog_view *view)
1238 const struct got_error *err = NULL;
1239 struct tog_log_view_state *s = &view->state.log;
1241 return draw_commits(view, &s->last_displayed_entry,
1242 &s->selected_entry, s->first_displayed_entry,
1243 &s->commits, s->selected, view->nlines, s->graph,
1244 s->repo, s->in_repo_path);
1249 static const struct got_error *
1250 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1251 struct tog_view **focus_view, struct tog_view *view, int ch)
1253 const struct got_error *err = NULL;
1254 struct tog_log_view_state *s = &view->state.log;
1256 struct tog_view *diff_view = NULL, *tree_view = NULL;
1263 if (s->selected > 0)
1265 if (s->selected > 0)
1267 scroll_up(&s->first_displayed_entry, 1,
1271 if (TAILQ_FIRST(&s->commits.head) ==
1272 s->first_displayed_entry) {
1276 scroll_up(&s->first_displayed_entry,
1277 view->nlines, &s->commits);
1282 if (s->selected < MIN(view->nlines - 2,
1283 s->commits.ncommits - 1)) {
1287 err = scroll_down(&s->first_displayed_entry, 1,
1288 &s->last_displayed_entry, &s->commits,
1289 s->graph, s->repo, s->in_repo_path);
1291 if (err->code != GOT_ERR_ITER_COMPLETED)
1297 struct commit_queue_entry *first;
1298 first = s->first_displayed_entry;
1299 err = scroll_down(&s->first_displayed_entry,
1300 view->nlines, &s->last_displayed_entry,
1301 &s->commits, s->graph, s->repo,
1303 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1305 if (first == s->first_displayed_entry &&
1306 s->selected < MIN(view->nlines - 2,
1307 s->commits.ncommits - 1)) {
1308 /* can't scroll further down */
1309 s->selected = MIN(view->nlines - 2,
1310 s->commits.ncommits - 1);
1316 if (s->selected > view->nlines - 2)
1317 s->selected = view->nlines - 2;
1318 if (s->selected > s->commits.ncommits - 1)
1319 s->selected = s->commits.ncommits - 1;
1323 if (view_is_parent_view(view))
1324 begin_x = view_split_begin_x(view->begin_x);
1325 err = open_diff_view_for_commit(&diff_view, begin_x,
1326 s->selected_entry->id, s->selected_entry->commit,
1330 if (view_is_parent_view(view)) {
1331 err = view_close_child(view);
1334 err = view_set_child(view, diff_view);
1336 view_close(diff_view);
1339 if (!view_is_splitscreen(diff_view)) {
1340 *focus_view = diff_view;
1341 view->child_focussed = 1;
1344 *new_view = diff_view;
1347 if (view_is_parent_view(view))
1348 begin_x = view_split_begin_x(view->begin_x);
1349 err = browse_commit(&tree_view, begin_x,
1350 s->selected_entry, s->repo);
1351 if (view_is_parent_view(view)) {
1352 err = view_close_child(view);
1355 err = view_set_child(view, tree_view);
1357 view_close(tree_view);
1360 *focus_view = tree_view;
1361 view->child_focussed = 1;
1363 *new_view = tree_view;
1366 if (strcmp(s->in_repo_path, "/") == 0)
1368 parent_path = dirname(s->in_repo_path);
1369 if (parent_path && strcmp(parent_path, ".") != 0) {
1370 struct tog_view *lv;
1371 lv = view_open(view->nlines, view->ncols,
1372 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1374 return got_error_from_errno();
1375 err = open_log_view(lv, s->start_id, s->repo,
1379 if (view_is_parent_view(view))
1382 view_set_child(view->parent, lv);
1394 static const struct got_error *
1395 cmd_log(int argc, char *argv[])
1397 const struct got_error *error;
1398 struct got_repository *repo = NULL;
1399 struct got_object_id *start_id = NULL;
1400 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1401 char *start_commit = NULL;
1403 struct tog_view *view;
1406 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1411 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1414 start_commit = optarg;
1417 repo_path = realpath(optarg, NULL);
1418 if (repo_path == NULL)
1419 err(1, "-r option");
1433 path = strdup(argv[0]);
1437 return got_error_from_errno();
1439 cwd = getcwd(NULL, 0);
1441 error = got_error_from_errno();
1444 if (repo_path == NULL) {
1445 repo_path = strdup(cwd);
1446 if (repo_path == NULL) {
1447 error = got_error_from_errno();
1452 error = got_repo_open(&repo, repo_path);
1456 if (start_commit == NULL) {
1457 error = get_head_commit_id(&start_id, repo);
1461 struct got_object *obj;
1462 error = got_object_open_by_id_str(&obj, repo, start_commit);
1463 if (error == NULL) {
1464 start_id = got_object_id_dup(got_object_get_id(obj));
1465 if (start_id == NULL)
1466 error = got_error_from_errno();
1473 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1475 error = got_error_from_errno();
1478 error = open_log_view(view, start_id, repo, path);
1481 error = view_loop(view);
1488 got_repo_close(repo);
1496 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1502 parse_next_line(FILE *f, size_t *len)
1507 const char delim[3] = { '\0', '\0', '\0'};
1509 line = fparseln(f, &linelen, &lineno, delim, 0);
1515 static const struct got_error *
1516 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1517 int *last_displayed_line, int *eof, int max_lines,
1520 const struct got_error *err;
1521 int nlines = 0, nprinted = 0;
1528 werase(view->window);
1531 err = format_line(&wline, &width, header, view->ncols);
1536 if (view_needs_focus_indication(view))
1537 wstandout(view->window);
1538 waddwstr(view->window, wline);
1539 if (view_needs_focus_indication(view))
1540 wstandend(view->window);
1541 if (width < view->ncols)
1542 waddch(view->window, '\n');
1550 while (nprinted < max_lines) {
1551 line = parse_next_line(f, &len);
1556 if (++nlines < *first_displayed_line) {
1561 err = format_line(&wline, &width, line, view->ncols);
1566 waddwstr(view->window, wline);
1567 if (width < view->ncols)
1568 waddch(view->window, '\n');
1569 if (++nprinted == 1)
1570 *first_displayed_line = nlines;
1575 *last_displayed_line = nlines;
1582 static const struct got_error *
1583 open_diff_view(struct tog_view *view, struct got_object *obj1,
1584 struct got_object *obj2, struct got_repository *repo)
1586 const struct got_error *err;
1589 if (obj1 != NULL && obj2 != NULL &&
1590 got_object_get_type(obj1) != got_object_get_type(obj2))
1591 return got_error(GOT_ERR_OBJ_TYPE);
1595 return got_error_from_errno();
1597 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1598 case GOT_OBJ_TYPE_BLOB:
1599 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1602 case GOT_OBJ_TYPE_TREE:
1603 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1605 case GOT_OBJ_TYPE_COMMIT:
1606 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1609 return got_error(GOT_ERR_OBJ_TYPE);
1614 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1615 view->state.diff.id2 = got_object_get_id(obj2);
1616 view->state.diff.f = f;
1617 view->state.diff.first_displayed_line = 1;
1618 view->state.diff.last_displayed_line = view->nlines;
1620 view->show = show_diff_view;
1621 view->input = input_diff_view;
1622 view->close = close_diff_view;
1627 static const struct got_error *
1628 close_diff_view(struct tog_view *view)
1630 const struct got_error *err = NULL;
1632 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1633 err = got_error_from_errno();
1637 static const struct got_error *
1638 show_diff_view(struct tog_view *view)
1640 const struct got_error *err;
1641 struct tog_diff_view_state *s = &view->state.diff;
1642 char *id_str1 = NULL, *id_str2, *header;
1645 err = got_object_id_str(&id_str1, s->id1);
1649 err = got_object_id_str(&id_str2, s->id2);
1653 if (asprintf(&header, "diff: %s %s",
1654 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1655 err = got_error_from_errno();
1663 return draw_file(view, s->f, &s->first_displayed_line,
1664 &s->last_displayed_line, &s->eof, view->nlines,
1668 static const struct got_error *
1669 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1670 struct tog_view **focus_view, struct tog_view *view, int ch)
1672 const struct got_error *err = NULL;
1673 struct tog_diff_view_state *s = &view->state.diff;
1679 if (s->first_displayed_line > 1)
1680 s->first_displayed_line--;
1684 while (i++ < view->nlines - 1 &&
1685 s->first_displayed_line > 1)
1686 s->first_displayed_line--;
1691 s->first_displayed_line++;
1696 while (!s->eof && i++ < view->nlines - 1) {
1698 line = parse_next_line(s->f, NULL);
1699 s->first_displayed_line++;
1711 static const struct got_error *
1712 cmd_diff(int argc, char *argv[])
1714 const struct got_error *error = NULL;
1715 struct got_repository *repo = NULL;
1716 struct got_object *obj1 = NULL, *obj2 = NULL;
1717 char *repo_path = NULL;
1718 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1720 struct tog_view *view;
1723 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1728 while ((ch = getopt(argc, argv, "")) != -1) {
1740 usage_diff(); /* TODO show local worktree changes */
1741 } else if (argc == 2) {
1742 repo_path = getcwd(NULL, 0);
1743 if (repo_path == NULL)
1744 return got_error_from_errno();
1745 obj_id_str1 = argv[0];
1746 obj_id_str2 = argv[1];
1747 } else if (argc == 3) {
1748 repo_path = realpath(argv[0], NULL);
1749 if (repo_path == NULL)
1750 return got_error_from_errno();
1751 obj_id_str1 = argv[1];
1752 obj_id_str2 = argv[2];
1756 error = got_repo_open(&repo, repo_path);
1761 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1765 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1769 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
1771 error = got_error_from_errno();
1774 error = open_diff_view(view, obj1, obj2, repo);
1777 error = view_loop(view);
1779 got_repo_close(repo);
1781 got_object_close(obj1);
1783 got_object_close(obj2);
1791 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1796 struct tog_blame_line {
1798 struct got_object_id *id;
1801 static const struct got_error *
1802 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1803 const char *path, struct tog_blame_line *lines, int nlines,
1804 int blame_complete, int selected_line, int *first_displayed_line,
1805 int *last_displayed_line, int *eof, int max_lines)
1807 const struct got_error *err;
1808 int lineno = 0, nprinted = 0;
1813 struct tog_blame_line *blame_line;
1814 struct got_object_id *prev_id = NULL;
1817 err = got_object_id_str(&id_str, id);
1822 werase(view->window);
1824 if (asprintf(&line, "commit: %s", id_str) == -1) {
1825 err = got_error_from_errno();
1830 err = format_line(&wline, &width, line, view->ncols);
1833 if (view_needs_focus_indication(view))
1834 wstandout(view->window);
1835 waddwstr(view->window, wline);
1836 if (view_needs_focus_indication(view))
1837 wstandend(view->window);
1840 if (width < view->ncols)
1841 waddch(view->window, '\n');
1843 if (asprintf(&line, "[%d/%d] %s%s",
1844 *first_displayed_line - 1 + selected_line, nlines,
1845 blame_complete ? "" : "annotating ", path) == -1) {
1847 return got_error_from_errno();
1850 err = format_line(&wline, &width, line, view->ncols);
1855 waddwstr(view->window, wline);
1858 if (width < view->ncols)
1859 waddch(view->window, '\n');
1862 while (nprinted < max_lines - 2) {
1863 line = parse_next_line(f, &len);
1868 if (++lineno < *first_displayed_line) {
1873 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1874 err = format_line(&wline, &width, line, wlimit);
1880 if (view->focussed && nprinted == selected_line - 1)
1881 wstandout(view->window);
1883 blame_line = &lines[lineno - 1];
1884 if (blame_line->annotated && prev_id &&
1885 got_object_id_cmp(prev_id, blame_line->id) == 0)
1886 waddstr(view->window, " ");
1887 else if (blame_line->annotated) {
1889 err = got_object_id_str(&id_str, blame_line->id);
1895 wprintw(view->window, "%.8s ", id_str);
1897 prev_id = blame_line->id;
1899 waddstr(view->window, "........ ");
1903 waddwstr(view->window, wline);
1904 while (width < wlimit) {
1905 waddch(view->window, ' ');
1908 if (view->focussed && nprinted == selected_line - 1)
1909 wstandend(view->window);
1910 if (++nprinted == 1)
1911 *first_displayed_line = lineno;
1916 *last_displayed_line = lineno;
1923 static const struct got_error *
1924 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1926 const struct got_error *err = NULL;
1927 struct tog_blame_cb_args *a = arg;
1928 struct tog_blame_line *line;
1930 if (nlines != a->nlines ||
1931 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1932 return got_error(GOT_ERR_RANGE);
1934 if (pthread_mutex_lock(a->mutex) != 0)
1935 return got_error_from_errno();
1937 if (*a->quit) { /* user has quit the blame view */
1938 err = got_error(GOT_ERR_ITER_COMPLETED);
1943 goto done; /* no change in this commit */
1945 line = &a->lines[lineno - 1];
1946 if (line->annotated)
1949 line->id = got_object_id_dup(id);
1950 if (line->id == NULL) {
1951 err = got_error_from_errno();
1954 line->annotated = 1;
1956 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1957 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1958 a->last_displayed_line, a->eof, a->view->nlines);
1960 if (pthread_mutex_unlock(a->mutex) != 0)
1961 return got_error_from_errno();
1966 blame_thread(void *arg)
1968 const struct got_error *err;
1969 struct tog_blame_thread_args *ta = arg;
1970 struct tog_blame_cb_args *a = ta->cb_args;
1972 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1973 blame_cb, ta->cb_args);
1975 if (pthread_mutex_lock(a->mutex) != 0)
1976 return (void *)got_error_from_errno();
1978 got_repo_close(ta->repo);
1982 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1983 a->lines, a->nlines, 1, *a->selected_line,
1984 a->first_displayed_line, a->last_displayed_line, a->eof,
1987 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1988 err = got_error_from_errno();
1993 static struct got_object_id *
1994 get_selected_commit_id(struct tog_blame_line *lines,
1995 int first_displayed_line, int selected_line)
1997 struct tog_blame_line *line;
1999 line = &lines[first_displayed_line - 1 + selected_line - 1];
2000 if (!line->annotated)
2006 static const struct got_error *
2007 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2008 struct tog_blame_line *lines, int first_displayed_line,
2009 int selected_line, struct got_repository *repo)
2011 const struct got_error *err = NULL;
2012 struct got_commit_object *commit = NULL;
2013 struct got_object_id *selected_id;
2014 struct got_object_qid *pid;
2019 selected_id = get_selected_commit_id(lines,
2020 first_displayed_line, selected_line);
2021 if (selected_id == NULL)
2024 err = got_object_open(obj, repo, selected_id);
2028 err = got_object_commit_open(&commit, repo, *obj);
2032 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2034 err = got_object_open(pobj, repo, pid->id);
2040 got_object_commit_close(commit);
2044 static const struct got_error *
2045 stop_blame(struct tog_blame *blame)
2047 const struct got_error *err = NULL;
2050 if (blame->thread) {
2051 if (pthread_join(blame->thread, (void **)&err) != 0)
2052 err = got_error_from_errno();
2053 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2055 blame->thread = NULL;
2057 if (blame->thread_args.repo) {
2058 got_repo_close(blame->thread_args.repo);
2059 blame->thread_args.repo = NULL;
2065 for (i = 0; i < blame->nlines; i++)
2066 free(blame->lines[i].id);
2068 blame->lines = NULL;
2069 free(blame->cb_args.commit_id);
2070 blame->cb_args.commit_id = NULL;
2075 static const struct got_error *
2076 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2077 struct tog_view *view, int *blame_complete,
2078 int *first_displayed_line, int *last_displayed_line,
2079 int *selected_line, int *done, int *eof, const char *path,
2080 struct got_object_id *commit_id,
2081 struct got_repository *repo)
2083 const struct got_error *err = NULL;
2084 struct got_blob_object *blob = NULL;
2085 struct got_repository *thread_repo = NULL;
2086 struct got_object_id *obj_id = NULL;
2087 struct got_object *obj = NULL;
2089 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2093 err = got_object_open(&obj, repo, obj_id);
2097 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2098 err = got_error(GOT_ERR_OBJ_TYPE);
2102 err = got_object_blob_open(&blob, repo, obj, 8192);
2105 blame->f = got_opentemp();
2106 if (blame->f == NULL) {
2107 err = got_error_from_errno();
2110 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2115 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2116 if (blame->lines == NULL) {
2117 err = got_error_from_errno();
2121 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2125 blame->cb_args.view = view;
2126 blame->cb_args.lines = blame->lines;
2127 blame->cb_args.nlines = blame->nlines;
2128 blame->cb_args.mutex = mutex;
2129 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2130 if (blame->cb_args.commit_id == NULL) {
2131 err = got_error_from_errno();
2134 blame->cb_args.f = blame->f;
2135 blame->cb_args.path = path;
2136 blame->cb_args.first_displayed_line = first_displayed_line;
2137 blame->cb_args.selected_line = selected_line;
2138 blame->cb_args.last_displayed_line = last_displayed_line;
2139 blame->cb_args.quit = done;
2140 blame->cb_args.eof = eof;
2142 blame->thread_args.path = path;
2143 blame->thread_args.repo = thread_repo;
2144 blame->thread_args.cb_args = &blame->cb_args;
2145 blame->thread_args.complete = blame_complete;
2146 *blame_complete = 0;
2148 if (pthread_create(&blame->thread, NULL, blame_thread,
2149 &blame->thread_args) != 0) {
2150 err = got_error_from_errno();
2156 got_object_blob_close(blob);
2159 got_object_close(obj);
2165 static const struct got_error *
2166 open_blame_view(struct tog_view *view, char *path,
2167 struct got_object_id *commit_id, struct got_repository *repo)
2169 const struct got_error *err = NULL;
2170 struct tog_blame_view_state *s = &view->state.blame;
2172 SIMPLEQ_INIT(&s->blamed_commits);
2174 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2175 return got_error_from_errno();
2177 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2181 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2182 s->first_displayed_line = 1;
2183 s->last_displayed_line = view->nlines;
2184 s->selected_line = 1;
2185 s->blame_complete = 0;
2187 if (s->path == NULL)
2188 return got_error_from_errno();
2190 s->commit_id = commit_id;
2191 memset(&s->blame, 0, sizeof(s->blame));
2193 view->show = show_blame_view;
2194 view->input = input_blame_view;
2195 view->close = close_blame_view;
2197 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2198 &s->first_displayed_line, &s->last_displayed_line,
2199 &s->selected_line, &s->done, &s->eof, s->path,
2200 s->blamed_commit->id, s->repo);
2203 static const struct got_error *
2204 close_blame_view(struct tog_view *view)
2206 const struct got_error *err = NULL;
2207 struct tog_blame_view_state *s = &view->state.blame;
2209 if (s->blame.thread)
2210 err = stop_blame(&s->blame);
2212 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2213 struct got_object_qid *blamed_commit;
2214 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2215 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2216 got_object_qid_free(blamed_commit);
2224 static const struct got_error *
2225 show_blame_view(struct tog_view *view)
2227 const struct got_error *err = NULL;
2228 struct tog_blame_view_state *s = &view->state.blame;
2230 if (pthread_mutex_lock(&s->mutex) != 0)
2231 return got_error_from_errno();
2233 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2234 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2235 s->selected_line, &s->first_displayed_line,
2236 &s->last_displayed_line, &s->eof, view->nlines);
2238 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2239 err = got_error_from_errno();
2245 static const struct got_error *
2246 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2247 struct tog_view **focus_view, struct tog_view *view, int ch)
2249 const struct got_error *err = NULL, *thread_err = NULL;
2250 struct got_object *obj = NULL, *pobj = NULL;
2251 struct tog_view *diff_view;
2252 struct tog_blame_view_state *s = &view->state.blame;
2255 if (pthread_mutex_lock(&s->mutex) != 0) {
2256 err = got_error_from_errno();
2263 if (pthread_mutex_unlock(&s->mutex) != 0) {
2264 err = got_error_from_errno();
2267 return stop_blame(&s->blame);
2270 if (s->selected_line > 1)
2272 else if (s->selected_line == 1 &&
2273 s->first_displayed_line > 1)
2274 s->first_displayed_line--;
2277 if (s->first_displayed_line == 1) {
2278 s->selected_line = 1;
2281 if (s->first_displayed_line > view->nlines - 2)
2282 s->first_displayed_line -=
2285 s->first_displayed_line = 1;
2289 if (s->selected_line < view->nlines - 2 &&
2290 s->first_displayed_line +
2291 s->selected_line <= s->blame.nlines)
2293 else if (s->last_displayed_line <
2295 s->first_displayed_line++;
2299 struct got_object_id *id;
2300 id = get_selected_commit_id(s->blame.lines,
2301 s->first_displayed_line, s->selected_line);
2302 if (id == NULL || got_object_id_cmp(id,
2303 s->blamed_commit->id) == 0)
2305 err = open_selected_commit(&pobj, &obj,
2306 s->blame.lines, s->first_displayed_line,
2307 s->selected_line, s->repo);
2310 if (pobj == NULL && obj == NULL)
2312 if (ch == 'p' && pobj == NULL)
2315 if (pthread_mutex_unlock(&s->mutex) != 0) {
2316 err = got_error_from_errno();
2319 thread_err = stop_blame(&s->blame);
2321 if (pthread_mutex_lock(&s->mutex) != 0) {
2322 err = got_error_from_errno();
2327 id = got_object_get_id(ch == 'b' ? obj : pobj);
2328 got_object_close(obj);
2331 got_object_close(pobj);
2334 err = got_object_qid_alloc(&s->blamed_commit, id);
2337 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2338 s->blamed_commit, entry);
2339 err = run_blame(&s->blame, &s->mutex, view,
2341 &s->first_displayed_line,
2342 &s->last_displayed_line,
2343 &s->selected_line, &s->done, &s->eof,
2344 s->path, s->blamed_commit->id, s->repo);
2350 struct got_object_qid *first;
2351 first = SIMPLEQ_FIRST(&s->blamed_commits);
2352 if (!got_object_id_cmp(first->id, s->commit_id))
2355 if (pthread_mutex_unlock(&s->mutex) != 0) {
2356 err = got_error_from_errno();
2359 thread_err = stop_blame(&s->blame);
2361 if (pthread_mutex_lock(&s->mutex) != 0) {
2362 err = got_error_from_errno();
2367 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2368 got_object_qid_free(s->blamed_commit);
2370 SIMPLEQ_FIRST(&s->blamed_commits);
2371 err = run_blame(&s->blame, &s->mutex, view,
2373 &s->first_displayed_line,
2374 &s->last_displayed_line,
2375 &s->selected_line, &s->done, &s->eof, s->path,
2376 s->blamed_commit->id, s->repo);
2383 err = open_selected_commit(&pobj, &obj,
2384 s->blame.lines, s->first_displayed_line,
2385 s->selected_line, s->repo);
2388 if (pobj == NULL && obj == NULL)
2391 if (view_is_parent_view(view))
2392 begin_x = view_split_begin_x(view->begin_x);
2393 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2394 if (diff_view == NULL) {
2395 err = got_error_from_errno();
2398 err = open_diff_view(diff_view, pobj, obj, s->repo);
2400 view_close(diff_view);
2403 if (view_is_parent_view(view)) {
2404 err = view_close_child(view);
2407 err = view_set_child(view, diff_view);
2409 view_close(diff_view);
2412 if (!view_is_splitscreen(diff_view)) {
2413 *focus_view = diff_view;
2414 view->child_focussed = 1;
2417 *new_view = diff_view;
2419 got_object_close(pobj);
2422 got_object_close(obj);
2429 if (s->last_displayed_line >= s->blame.nlines &&
2430 s->selected_line < view->nlines - 2) {
2431 s->selected_line = MIN(s->blame.nlines,
2435 if (s->last_displayed_line + view->nlines - 2
2437 s->first_displayed_line +=
2440 s->first_displayed_line =
2445 if (s->selected_line > view->nlines - 2) {
2446 s->selected_line = MIN(s->blame.nlines,
2454 if (pthread_mutex_unlock(&s->mutex) != 0)
2455 err = got_error_from_errno();
2458 got_object_close(pobj);
2459 return thread_err ? thread_err : err;
2462 static const struct got_error *
2463 cmd_blame(int argc, char *argv[])
2465 const struct got_error *error;
2466 struct got_repository *repo = NULL;
2467 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2468 struct got_object_id *commit_id = NULL;
2469 char *commit_id_str = NULL;
2471 struct tog_view *view;
2474 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2479 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2482 commit_id_str = optarg;
2485 repo_path = realpath(optarg, NULL);
2486 if (repo_path == NULL)
2487 err(1, "-r option");
2503 cwd = getcwd(NULL, 0);
2505 error = got_error_from_errno();
2508 if (repo_path == NULL) {
2509 repo_path = strdup(cwd);
2510 if (repo_path == NULL) {
2511 error = got_error_from_errno();
2517 error = got_repo_open(&repo, repo_path);
2521 error = got_repo_map_path(&in_repo_path, repo, path);
2525 if (commit_id_str == NULL) {
2526 struct got_reference *head_ref;
2527 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2530 error = got_ref_resolve(&commit_id, repo, head_ref);
2531 got_ref_close(head_ref);
2533 struct got_object *obj;
2534 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2537 commit_id = got_object_id_dup(got_object_get_id(obj));
2538 if (commit_id == NULL)
2539 error = got_error_from_errno();
2540 got_object_close(obj);
2545 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2547 error = got_error_from_errno();
2550 error = open_blame_view(view, in_repo_path, commit_id, repo);
2553 error = view_loop(view);
2559 got_repo_close(repo);
2563 static const struct got_error *
2564 draw_tree_entries(struct tog_view *view,
2565 struct got_tree_entry **first_displayed_entry,
2566 struct got_tree_entry **last_displayed_entry,
2567 struct got_tree_entry **selected_entry, int *ndisplayed,
2568 const char *label, int show_ids, const char *parent_path,
2569 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2571 const struct got_error *err = NULL;
2572 struct got_tree_entry *te;
2578 werase(view->window);
2583 err = format_line(&wline, &width, label, view->ncols);
2586 if (view_needs_focus_indication(view))
2587 wstandout(view->window);
2588 waddwstr(view->window, wline);
2589 if (view_needs_focus_indication(view))
2590 wstandend(view->window);
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2597 err = format_line(&wline, &width, parent_path, view->ncols);
2600 waddwstr(view->window, wline);
2603 if (width < view->ncols)
2604 waddch(view->window, '\n');
2607 waddch(view->window, '\n');
2611 te = SIMPLEQ_FIRST(&entries->head);
2612 if (*first_displayed_entry == NULL) {
2613 if (selected == 0) {
2615 wstandout(view->window);
2616 *selected_entry = NULL;
2618 waddstr(view->window, " ..\n"); /* parent directory */
2619 if (selected == 0 && view->focussed)
2620 wstandend(view->window);
2627 while (te != *first_displayed_entry)
2628 te = SIMPLEQ_NEXT(te, entry);
2632 char *line = NULL, *id_str = NULL;
2635 err = got_object_id_str(&id_str, te->id);
2637 return got_error_from_errno();
2639 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2640 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2642 return got_error_from_errno();
2645 err = format_line(&wline, &width, line, view->ncols);
2650 if (n == selected) {
2652 wstandout(view->window);
2653 *selected_entry = te;
2655 waddwstr(view->window, wline);
2656 if (width < view->ncols)
2657 waddch(view->window, '\n');
2658 if (n == selected && view->focussed)
2659 wstandend(view->window);
2665 *last_displayed_entry = te;
2668 te = SIMPLEQ_NEXT(te, entry);
2675 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2676 const struct got_tree_entries *entries, int isroot)
2678 struct got_tree_entry *te, *prev;
2681 if (*first_displayed_entry == NULL)
2684 te = SIMPLEQ_FIRST(&entries->head);
2685 if (*first_displayed_entry == te) {
2687 *first_displayed_entry = NULL;
2691 /* XXX this is stupid... switch to TAILQ? */
2692 for (i = 0; i < maxscroll; i++) {
2693 while (te != *first_displayed_entry) {
2695 te = SIMPLEQ_NEXT(te, entry);
2697 *first_displayed_entry = prev;
2698 te = SIMPLEQ_FIRST(&entries->head);
2700 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2701 *first_displayed_entry = NULL;
2705 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2706 struct got_tree_entry *last_displayed_entry,
2707 const struct got_tree_entries *entries)
2709 struct got_tree_entry *next;
2712 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2715 if (*first_displayed_entry)
2716 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2718 next = SIMPLEQ_FIRST(&entries->head);
2720 *first_displayed_entry = next;
2721 if (++n >= maxscroll)
2723 next = SIMPLEQ_NEXT(next, entry);
2727 static const struct got_error *
2728 tree_entry_path(char **path, struct tog_parent_trees *parents,
2729 struct got_tree_entry *te)
2731 const struct got_error *err = NULL;
2732 struct tog_parent_tree *pt;
2733 size_t len = 2; /* for leading slash and NUL */
2735 TAILQ_FOREACH(pt, parents, entry)
2736 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2738 len += strlen(te->name);
2740 *path = calloc(1, len);
2742 return got_error_from_errno();
2745 pt = TAILQ_LAST(parents, tog_parent_trees);
2747 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2748 err = got_error(GOT_ERR_NO_SPACE);
2751 if (strlcat(*path, "/", len) >= len) {
2752 err = got_error(GOT_ERR_NO_SPACE);
2755 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2758 if (strlcat(*path, te->name, len) >= len) {
2759 err = got_error(GOT_ERR_NO_SPACE);
2771 static const struct got_error *
2772 blame_tree_entry(struct tog_view **new_view, int begin_x,
2773 struct got_tree_entry *te, struct tog_parent_trees *parents,
2774 struct got_object_id *commit_id, struct got_repository *repo)
2776 const struct got_error *err = NULL;
2778 struct tog_view *blame_view;
2780 err = tree_entry_path(&path, parents, te);
2784 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
2785 if (blame_view == NULL)
2786 return got_error_from_errno();
2788 err = open_blame_view(blame_view, path, commit_id, repo);
2790 view_close(blame_view);
2793 *new_view = blame_view;
2797 static const struct got_error *
2798 log_tree_entry(struct tog_view **new_view, int begin_x,
2799 struct got_tree_entry *te, struct tog_parent_trees *parents,
2800 struct got_object_id *commit_id, struct got_repository *repo)
2802 struct tog_view *log_view;
2803 const struct got_error *err = NULL;
2806 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
2807 if (log_view == NULL)
2808 return got_error_from_errno();
2810 err = tree_entry_path(&path, parents, te);
2814 err = open_log_view(log_view, commit_id, repo, path);
2816 view_close(log_view);
2818 *new_view = log_view;
2823 static const struct got_error *
2824 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2825 struct got_object_id *commit_id, struct got_repository *repo)
2827 const struct got_error *err = NULL;
2828 char *commit_id_str = NULL;
2829 struct tog_tree_view_state *s = &view->state.tree;
2831 TAILQ_INIT(&s->parents);
2833 err = got_object_id_str(&commit_id_str, commit_id);
2837 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2838 err = got_error_from_errno();
2842 s->root = s->tree = root;
2843 s->entries = got_object_tree_get_entries(root);
2844 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2845 s->commit_id = got_object_id_dup(commit_id);
2846 if (s->commit_id == NULL) {
2847 err = got_error_from_errno();
2852 view->show = show_tree_view;
2853 view->input = input_tree_view;
2854 view->close = close_tree_view;
2856 free(commit_id_str);
2858 free(s->tree_label);
2859 s->tree_label = NULL;
2864 static const struct got_error *
2865 close_tree_view(struct tog_view *view)
2867 struct tog_tree_view_state *s = &view->state.tree;
2869 free(s->tree_label);
2870 s->tree_label = NULL;
2872 s->commit_id = NULL;
2873 while (!TAILQ_EMPTY(&s->parents)) {
2874 struct tog_parent_tree *parent;
2875 parent = TAILQ_FIRST(&s->parents);
2876 TAILQ_REMOVE(&s->parents, parent, entry);
2880 if (s->tree != s->root)
2881 got_object_tree_close(s->tree);
2882 got_object_tree_close(s->root);
2887 static const struct got_error *
2888 show_tree_view(struct tog_view *view)
2890 const struct got_error *err = NULL;
2891 struct tog_tree_view_state *s = &view->state.tree;
2894 err = tree_entry_path(&parent_path, &s->parents, NULL);
2898 err = draw_tree_entries(view, &s->first_displayed_entry,
2899 &s->last_displayed_entry, &s->selected_entry,
2900 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2901 s->entries, s->selected, view->nlines, s->tree == s->root);
2908 static const struct got_error *
2909 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2910 struct tog_view **focus_view, struct tog_view *view, int ch)
2912 const struct got_error *err = NULL;
2913 struct tog_tree_view_state *s = &view->state.tree;
2914 struct tog_view *log_view;
2919 s->show_ids = !s->show_ids;
2922 if (!s->selected_entry)
2924 if (view_is_parent_view(view))
2925 begin_x = view_split_begin_x(view->begin_x);
2926 err = log_tree_entry(&log_view, begin_x,
2927 s->selected_entry, &s->parents,
2928 s->commit_id, s->repo);
2929 if (view_is_parent_view(view)) {
2930 err = view_close_child(view);
2933 err = view_set_child(view, log_view);
2935 view_close(log_view);
2938 *focus_view = log_view;
2939 view->child_focussed = 1;
2941 *new_view = log_view;
2945 if (s->selected > 0)
2947 if (s->selected > 0)
2949 tree_scroll_up(&s->first_displayed_entry, 1,
2950 s->entries, s->tree == s->root);
2953 if (SIMPLEQ_FIRST(&s->entries->head) ==
2954 s->first_displayed_entry) {
2955 if (s->tree != s->root)
2956 s->first_displayed_entry = NULL;
2960 tree_scroll_up(&s->first_displayed_entry,
2961 view->nlines, s->entries,
2962 s->tree == s->root);
2966 if (s->selected < s->ndisplayed - 1) {
2970 tree_scroll_down(&s->first_displayed_entry, 1,
2971 s->last_displayed_entry, s->entries);
2974 tree_scroll_down(&s->first_displayed_entry,
2975 view->nlines, s->last_displayed_entry,
2977 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2980 /* can't scroll any further; move cursor down */
2981 if (s->selected < s->ndisplayed - 1)
2982 s->selected = s->ndisplayed - 1;
2986 if (s->selected_entry == NULL) {
2987 struct tog_parent_tree *parent;
2989 /* user selected '..' */
2990 if (s->tree == s->root)
2992 parent = TAILQ_FIRST(&s->parents);
2993 TAILQ_REMOVE(&s->parents, parent,
2995 got_object_tree_close(s->tree);
2996 s->tree = parent->tree;
2998 got_object_tree_get_entries(s->tree);
2999 s->first_displayed_entry =
3000 parent->first_displayed_entry;
3002 parent->selected_entry;
3003 s->selected = parent->selected;
3005 } else if (S_ISDIR(s->selected_entry->mode)) {
3006 struct tog_parent_tree *parent;
3007 struct got_tree_object *child;
3008 err = got_object_open_as_tree(&child,
3009 s->repo, s->selected_entry->id);
3012 parent = calloc(1, sizeof(*parent));
3013 if (parent == NULL) {
3014 err = got_error_from_errno();
3017 parent->tree = s->tree;
3018 parent->first_displayed_entry =
3019 s->first_displayed_entry;
3020 parent->selected_entry = s->selected_entry;
3021 parent->selected = s->selected;
3022 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3025 got_object_tree_get_entries(s->tree);
3027 s->first_displayed_entry = NULL;
3028 } else if (S_ISREG(s->selected_entry->mode)) {
3029 struct tog_view *blame_view;
3030 int begin_x = view_is_parent_view(view) ?
3031 view_split_begin_x(view->begin_x) : 0;
3033 err = blame_tree_entry(&blame_view, begin_x,
3034 s->selected_entry, &s->parents, s->commit_id,
3038 if (view_is_parent_view(view)) {
3039 err = view_close_child(view);
3042 err = view_set_child(view, blame_view);
3044 view_close(blame_view);
3047 *focus_view = blame_view;
3048 view->child_focussed = 1;
3050 *new_view = blame_view;
3054 if (s->selected > view->nlines)
3055 s->selected = s->ndisplayed - 1;
3068 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3073 static const struct got_error *
3074 cmd_tree(int argc, char *argv[])
3076 const struct got_error *error;
3077 struct got_repository *repo = NULL;
3078 char *repo_path = NULL;
3079 struct got_object_id *commit_id = NULL;
3080 char *commit_id_arg = NULL;
3081 struct got_commit_object *commit = NULL;
3082 struct got_tree_object *tree = NULL;
3084 struct tog_view *view;
3087 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3092 while ((ch = getopt(argc, argv, "c:")) != -1) {
3095 commit_id_arg = optarg;
3107 repo_path = getcwd(NULL, 0);
3108 if (repo_path == NULL)
3109 return got_error_from_errno();
3110 } else if (argc == 1) {
3111 repo_path = realpath(argv[0], NULL);
3112 if (repo_path == NULL)
3113 return got_error_from_errno();
3117 error = got_repo_open(&repo, repo_path);
3122 if (commit_id_arg == NULL) {
3123 error = get_head_commit_id(&commit_id, repo);
3127 struct got_object *obj;
3128 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3129 if (error == NULL) {
3130 commit_id = got_object_id_dup(got_object_get_id(obj));
3131 if (commit_id == NULL)
3132 error = got_error_from_errno();
3138 error = got_object_open_as_commit(&commit, repo, commit_id);
3142 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3146 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3148 error = got_error_from_errno();
3151 error = open_tree_view(view, tree, commit_id, repo);
3154 error = view_loop(view);
3158 got_object_commit_close(commit);
3160 got_object_tree_close(tree);
3162 got_repo_close(repo);
3172 intrflush(stdscr, FALSE);
3173 keypad(stdscr, TRUE);
3182 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3183 "Available commands:\n", getprogname());
3184 for (i = 0; i < nitems(tog_commands); i++) {
3185 struct tog_cmd *cmd = &tog_commands[i];
3186 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3192 make_argv(const char *arg0, const char *arg1)
3195 int argc = (arg1 == NULL ? 1 : 2);
3197 argv = calloc(argc, sizeof(char *));
3200 argv[0] = strdup(arg0);
3201 if (argv[0] == NULL)
3204 argv[1] = strdup(arg1);
3205 if (argv[1] == NULL)
3213 main(int argc, char *argv[])
3215 const struct got_error *error = NULL;
3216 struct tog_cmd *cmd = NULL;
3218 char **cmd_argv = NULL;
3220 setlocale(LC_ALL, "");
3222 while ((ch = getopt(argc, argv, "h")) != -1) {
3241 /* Build an argument vector which runs a default command. */
3242 cmd = &tog_commands[0];
3243 cmd_argv = make_argv(cmd->name, NULL);
3248 /* Did the user specific a command? */
3249 for (i = 0; i < nitems(tog_commands); i++) {
3250 if (strncmp(tog_commands[i].name, argv[0],
3251 strlen(argv[0])) == 0) {
3252 cmd = &tog_commands[i];
3254 tog_commands[i].cmd_usage();
3259 /* Did the user specify a repository? */
3260 char *repo_path = realpath(argv[0], NULL);
3262 struct got_repository *repo;
3263 error = got_repo_open(&repo, repo_path);
3265 got_repo_close(repo);
3267 error = got_error_from_errno();
3270 fprintf(stderr, "%s: '%s' is not a "
3271 "known command\n", getprogname(),
3275 fprintf(stderr, "%s: '%s' is neither a known "
3276 "command nor a path to a repository\n",
3277 getprogname(), argv[0]);
3281 cmd = &tog_commands[0];
3282 cmd_argv = make_argv(cmd->name, repo_path);
3290 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3297 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);