2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_opentemp.h"
44 #include "got_commit_graph.h"
46 #include "got_blame.h"
49 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 const struct got_error *(*cmd_main)(int, char *[]);
59 void (*cmd_usage)(void);
63 __dead static void usage(void);
64 __dead static void usage_log(void);
65 __dead static void usage_diff(void);
66 __dead static void usage_blame(void);
67 __dead static void usage_tree(void);
69 static const struct got_error* cmd_log(int, char *[]);
70 static const struct got_error* cmd_diff(int, char *[]);
71 static const struct got_error* cmd_blame(int, char *[]);
72 static const struct got_error* cmd_tree(int, char *[]);
74 static struct tog_cmd tog_commands[] = {
75 { "log", cmd_log, usage_log,
76 "show repository history" },
77 { "diff", cmd_diff, usage_diff,
78 "compare files and directories" },
79 { "blame", cmd_blame, usage_blame,
80 "show line-by-line file history" },
81 { "tree", cmd_tree, usage_tree,
82 "browse trees in repository" },
92 struct tog_diff_view_state {
93 struct got_object_id *id1, *id2;
95 int first_displayed_line;
96 int last_displayed_line;
100 struct commit_queue_entry {
101 TAILQ_ENTRY(commit_queue_entry) entry;
102 struct got_object_id *id;
103 struct got_commit_object *commit;
105 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
106 struct commit_queue {
108 struct commit_queue_head head;
111 struct tog_log_view_state {
112 struct got_commit_graph *graph;
113 struct commit_queue commits;
114 struct commit_queue_entry *first_displayed_entry;
115 struct commit_queue_entry *last_displayed_entry;
116 struct commit_queue_entry *selected_entry;
119 struct got_repository *repo;
122 struct tog_blame_cb_args {
123 pthread_mutex_t *mutex;
124 struct tog_blame_line *lines; /* one per line */
127 struct tog_view *view;
128 struct got_object_id *commit_id;
131 int *first_displayed_line;
132 int *last_displayed_line;
138 struct tog_blame_thread_args {
140 struct got_repository *repo;
141 struct tog_blame_cb_args *cb_args;
148 struct tog_blame_line *lines;
151 struct tog_blame_thread_args thread_args;
152 struct tog_blame_cb_args cb_args;
156 struct tog_blame_view_state {
157 int first_displayed_line;
158 int last_displayed_line;
163 pthread_mutex_t mutex;
164 struct got_object_id_queue blamed_commits;
165 struct got_object_qid *blamed_commit;
167 struct got_repository *repo;
168 struct got_object_id *commit_id;
169 struct tog_blame blame;
172 struct tog_parent_tree {
173 TAILQ_ENTRY(tog_parent_tree) entry;
174 struct got_tree_object *tree;
175 struct got_tree_entry *first_displayed_entry;
176 struct got_tree_entry *selected_entry;
180 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
182 struct tog_tree_view_state {
184 struct got_tree_object *root;
185 struct got_tree_object *tree;
186 const struct got_tree_entries *entries;
187 struct got_tree_entry *first_displayed_entry;
188 struct got_tree_entry *last_displayed_entry;
189 struct got_tree_entry *selected_entry;
190 int nentries, ndisplayed, selected, show_ids;
191 struct tog_parent_trees parents;
192 struct got_object_id *commit_id;
193 struct got_repository *repo;
196 TAILQ_HEAD(tog_view_list_head, tog_view);
198 TAILQ_ENTRY(tog_view) entry;
201 int nlines, ncols, begin_y, begin_x;
202 int lines, cols; /* copies of LINES and COLS */
204 struct tog_view *parent;
205 struct tog_view *child;
207 /* type-specific state */
208 enum tog_view_type type;
210 struct tog_diff_view_state diff;
211 struct tog_log_view_state log;
212 struct tog_blame_view_state blame;
213 struct tog_tree_view_state tree;
216 const struct got_error *(*show)(struct tog_view *);
217 const struct got_error *(*input)(struct tog_view **,
218 struct tog_view **, struct tog_view *, int);
219 const struct got_error *(*set_child)(struct tog_view *,
221 const struct got_error *(*close)(struct tog_view *);
224 static const struct got_error *open_diff_view(struct tog_view *,
225 struct got_object *, struct got_object *, struct got_repository *);
226 static const struct got_error *show_diff_view(struct tog_view *);
227 static const struct got_error *input_diff_view(struct tog_view **,
228 struct tog_view **, struct tog_view *, int);
229 static const struct got_error* close_diff_view(struct tog_view *);
231 static const struct got_error *open_log_view(struct tog_view *,
232 struct got_object_id *, struct got_repository *, const char *);
233 static const struct got_error * show_log_view(struct tog_view *);
234 static const struct got_error *input_log_view(struct tog_view **,
235 struct tog_view **, struct tog_view *, int);
236 static const struct got_error *close_log_view(struct tog_view *);
237 static const struct got_error* set_child_log_view(struct tog_view *,
240 static const struct got_error *open_blame_view(struct tog_view *, char *,
241 struct got_object_id *, struct got_repository *);
242 static const struct got_error *show_blame_view(struct tog_view *);
243 static const struct got_error *input_blame_view(struct tog_view **,
244 struct tog_view **, struct tog_view *, int);
245 static const struct got_error *close_blame_view(struct tog_view *);
247 static const struct got_error *open_tree_view(struct tog_view *,
248 struct got_tree_object *, struct got_object_id *, struct got_repository *);
249 static const struct got_error *show_tree_view(struct tog_view *);
250 static const struct got_error *input_tree_view(struct tog_view **,
251 struct tog_view **, struct tog_view *, int);
252 static const struct got_error *close_tree_view(struct tog_view *);
254 static const struct got_error *
255 view_close(struct tog_view *view)
257 const struct got_error *err = NULL;
260 view->child->parent = NULL;
262 view->parent->child = NULL;
264 err = view->close(view);
266 del_panel(view->panel);
268 delwin(view->window);
273 static struct tog_view *
274 view_open(int nlines, int ncols, int begin_y, int begin_x,
275 struct tog_view *parent, enum tog_view_type type)
277 struct tog_view *view = calloc(1, sizeof(*view));
282 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
283 begin_x = parent->ncols - 80;
285 view->parent = parent;
287 parent->child = view;
291 view->nlines = nlines ? nlines : LINES - begin_y;
292 view->ncols = ncols ? ncols : COLS - begin_x;
293 view->begin_y = begin_y;
294 view->begin_x = begin_x;
295 view->window = newwin(nlines, ncols, begin_y, begin_x);
296 if (view->window == NULL) {
300 view->panel = new_panel(view->window);
301 if (view->panel == NULL) {
306 keypad(view->window, TRUE);
310 static const struct got_error *
311 view_show(struct tog_view *view)
313 const struct got_error *err;
316 err = view->parent->show(view->parent);
319 show_panel(view->parent->panel);
322 err = view->show(view);
325 show_panel(view->panel);
327 if (view->child && view->child->begin_x > view->begin_x) {
328 err = view->child->show(view->child);
331 show_panel(view->child->panel);
340 static const struct got_error *
341 view_resize(struct tog_view *view)
346 if (view->lines > LINES)
347 nlines = view->nlines - (view->lines - LINES);
349 nlines = view->nlines + (LINES - view->lines);
351 if (view->cols > COLS)
352 ncols = view->ncols - (view->cols - COLS);
354 ncols = view->ncols + (COLS - view->cols);
356 if (wresize(view->window, nlines, ncols) == ERR)
357 return got_error_from_errno();
358 replace_panel(view->panel, view->window);
360 view->nlines = nlines;
371 static const struct got_error *
372 view_input(struct tog_view **new, struct tog_view **dead,
373 struct tog_view **focus, int *done, struct tog_view *view,
374 struct tog_view_list_head *views)
376 const struct got_error *err = NULL;
377 struct tog_view *next, *prev;
383 nodelay(stdscr, FALSE);
384 ch = wgetch(view->window);
385 nodelay(stdscr, TRUE);
390 next = TAILQ_NEXT(view, entry);
394 *focus = TAILQ_FIRST(views);
396 (*focus)->focussed = 1;
399 prev = TAILQ_PREV(view, tog_view_list_head, entry);
403 *focus = TAILQ_LAST(views, tog_view_list_head);
405 (*focus)->focussed = 1;
408 err = view->input(new, dead, view, ch);
415 err = view_resize(view);
418 err = view->input(new, dead, view, ch);
421 err = view->input(new, dead, view, ch);
428 static const struct got_error *
429 view_set_child(struct tog_view *view, struct tog_view *child)
431 const struct got_error *err;
433 if (view->set_child) {
434 err = view->set_child(view, child);
444 view_vborder(struct tog_view *view)
446 if (view->child == NULL)
449 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
450 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
454 view_needs_focus_indication(struct tog_view *view)
459 if (view->child && view->child->begin_x > view->begin_x)
462 if (view->parent && view->begin_x > view->parent->begin_x)
468 static const struct got_error *
469 view_loop(struct tog_view *view)
471 const struct got_error *err = NULL;
472 struct tog_view_list_head views;
473 struct tog_view *new_view, *dead_view;
477 TAILQ_INSERT_HEAD(&views, view, entry);
480 while (!TAILQ_EMPTY(&views) && !done) {
481 err = view_show(view);
484 err = view_input(&new_view, &dead_view, &view, &done,
489 struct tog_view *v, *t;
490 TAILQ_REMOVE(&views, dead_view, entry);
491 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
492 if (v->parent == dead_view) {
493 TAILQ_REMOVE(&views, v, entry);
499 if (dead_view->parent)
500 view = dead_view->parent;
502 view = TAILQ_LAST(&views, tog_view_list_head);
505 err = view_close(dead_view);
511 /* TODO: de-duplicate! */
512 TAILQ_INSERT_TAIL(&views, new_view, entry);
513 if (new_view->parent) {
514 err = view_set_child(new_view->parent, new_view);
517 new_view->parent->focussed = 0;
524 while (!TAILQ_EMPTY(&views)) {
525 view = TAILQ_FIRST(&views);
526 TAILQ_REMOVE(&views, view, entry);
537 "usage: %s log [-c commit] [-r repository-path] [path]\n",
542 /* Create newly allocated wide-character string equivalent to a byte string. */
543 static const struct got_error *
544 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
547 const struct got_error *err = NULL;
550 *wlen = mbstowcs(NULL, s, 0);
551 if (*wlen == (size_t)-1) {
554 return got_error_from_errno();
556 /* byte string invalid in current encoding; try to "fix" it */
557 err = got_mbsavis(&vis, &vislen, s);
560 *wlen = mbstowcs(NULL, vis, 0);
561 if (*wlen == (size_t)-1) {
562 err = got_error_from_errno(); /* give up */
567 *ws = calloc(*wlen + 1, sizeof(*ws));
569 err = got_error_from_errno();
573 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
574 err = got_error_from_errno();
585 /* Format a line for display, ensuring that it won't overflow a width limit. */
586 static const struct got_error *
587 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
589 const struct got_error *err = NULL;
591 wchar_t *wline = NULL;
598 err = mbs2ws(&wline, &wlen, line);
603 while (i < wlen && cols < wlimit) {
604 int width = wcwidth(wline[i]);
611 if (cols + width <= wlimit)
616 if (wline[i] == L'\t')
617 cols += TABSIZE - ((cols + 1) % TABSIZE);
621 err = got_error_from_errno();
636 static const struct got_error *
637 draw_commit(struct tog_view *view, struct got_commit_object *commit,
638 struct got_object_id *id)
640 const struct got_error *err = NULL;
641 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
642 char *logmsg0 = NULL, *logmsg = NULL;
643 char *author0 = NULL, *author = NULL;
644 wchar_t *wlogmsg = NULL, *wauthor = NULL;
645 int author_width, logmsg_width;
646 char *newline, *smallerthan;
649 static const size_t date_display_cols = 9;
650 static const size_t author_display_cols = 16;
651 const int avail = view->ncols;
653 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
654 &commit->tm_committer) >= sizeof(datebuf))
655 return got_error(GOT_ERR_NO_SPACE);
657 if (avail < date_display_cols)
658 limit = MIN(sizeof(datebuf) - 1, avail);
660 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
661 waddnstr(view->window, datebuf, limit);
666 author0 = strdup(commit->author);
667 if (author0 == NULL) {
668 err = got_error_from_errno();
672 smallerthan = strchr(author, '<');
676 char *at = strchr(author, '@');
681 err = format_line(&wauthor, &author_width, author, limit);
684 waddwstr(view->window, wauthor);
686 while (col <= avail && author_width < author_display_cols + 1) {
687 waddch(view->window, ' ');
694 logmsg0 = strdup(commit->logmsg);
695 if (logmsg0 == NULL) {
696 err = got_error_from_errno();
700 while (*logmsg == '\n')
702 newline = strchr(logmsg, '\n');
706 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
709 waddwstr(view->window, wlogmsg);
711 while (col <= avail) {
712 waddch(view->window, ' ');
724 static struct commit_queue_entry *
725 alloc_commit_queue_entry(struct got_commit_object *commit,
726 struct got_object_id *id)
728 struct commit_queue_entry *entry;
730 entry = calloc(1, sizeof(*entry));
735 entry->commit = commit;
740 pop_commit(struct commit_queue *commits)
742 struct commit_queue_entry *entry;
744 entry = TAILQ_FIRST(&commits->head);
745 TAILQ_REMOVE(&commits->head, entry, entry);
746 got_object_commit_close(entry->commit);
748 /* Don't free entry->id! It is owned by the commit graph. */
753 free_commits(struct commit_queue *commits)
755 while (!TAILQ_EMPTY(&commits->head))
759 static const struct got_error *
760 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
761 struct got_object_id *start_id, int minqueue, int init,
762 struct got_repository *repo, const char *path)
764 const struct got_error *err = NULL;
765 struct got_object_id *id;
766 struct commit_queue_entry *entry;
767 int nqueued = 0, found_obj = 0;
768 int is_root_path = strcmp(path, "/") == 0;
770 err = got_commit_graph_iter_start(graph, start_id, repo);
774 entry = TAILQ_LAST(&commits->head, commit_queue_head);
775 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
776 /* Start ID's commit is already on the queue; skip over it. */
777 err = got_commit_graph_iter_next(&id, graph);
778 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
781 err = got_commit_graph_fetch_commits(graph, 1, repo);
787 struct got_commit_object *commit;
789 err = got_commit_graph_iter_next(&id, graph);
791 if (err->code != GOT_ERR_ITER_NEED_MORE)
793 if (nqueued >= minqueue) {
797 err = got_commit_graph_fetch_commits(graph, 1, repo);
805 err = got_object_open_as_commit(&commit, repo, id);
810 struct got_object_id *obj_id = NULL;
811 struct got_object_qid *pid;
814 err = got_object_id_by_path(&obj_id, repo, id, path);
816 got_object_commit_close(commit);
817 if (err->code == GOT_ERR_NO_OBJ &&
818 (found_obj || !init)) {
819 /* History stops here. */
820 err = got_error(GOT_ERR_ITER_COMPLETED);
826 pid = SIMPLEQ_FIRST(&commit->parent_ids);
828 struct got_object_id *pobj_id;
829 err = got_object_id_by_path(&pobj_id, repo,
832 if (err->code != GOT_ERR_NO_OBJ) {
833 got_object_commit_close(commit);
840 changed = (got_object_id_cmp(obj_id,
847 got_object_commit_close(commit);
852 entry = alloc_commit_queue_entry(commit, id);
854 err = got_error_from_errno();
857 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
865 static const struct got_error *
866 fetch_next_commit(struct commit_queue_entry **pentry,
867 struct commit_queue_entry *entry, struct commit_queue *commits,
868 struct got_commit_graph *graph, struct got_repository *repo,
871 const struct got_error *err = NULL;
875 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
879 /* Next entry to display should now be available. */
880 *pentry = TAILQ_NEXT(entry, entry);
882 return got_error(GOT_ERR_NO_OBJ);
887 static const struct got_error *
888 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
890 const struct got_error *err = NULL;
891 struct got_reference *head_ref;
895 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
899 err = got_ref_resolve(head_id, repo, head_ref);
900 got_ref_close(head_ref);
909 static const struct got_error *
910 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
911 struct commit_queue_entry **selected, struct commit_queue_entry *first,
912 struct commit_queue *commits, int selected_idx, int limit,
913 struct got_commit_graph *graph, struct got_repository *repo,
916 const struct got_error *err = NULL;
917 struct commit_queue_entry *entry;
919 char *id_str, *header;
925 if (ncommits == selected_idx) {
929 entry = TAILQ_NEXT(entry, entry);
933 err = got_object_id_str(&id_str, (*selected)->id);
937 if (path && strcmp(path, "/") != 0) {
938 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
939 err = got_error_from_errno();
943 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
944 err = got_error_from_errno();
949 err = format_line(&wline, &width, header, view->ncols);
956 werase(view->window);
958 if (view_needs_focus_indication(view))
959 wstandout(view->window);
960 waddwstr(view->window, wline);
961 if (view_needs_focus_indication(view))
962 wstandend(view->window);
963 if (width < view->ncols)
964 waddch(view->window, '\n');
973 if (ncommits >= limit - 1)
975 if (ncommits == selected_idx)
976 wstandout(view->window);
977 err = draw_commit(view, entry->commit, entry->id);
978 if (ncommits == selected_idx)
979 wstandend(view->window);
984 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
985 err = queue_commits(graph, commits, entry->id, 1,
988 if (err->code != GOT_ERR_ITER_COMPLETED)
993 entry = TAILQ_NEXT(entry, entry);
1002 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1003 struct commit_queue *commits)
1005 struct commit_queue_entry *entry;
1008 entry = TAILQ_FIRST(&commits->head);
1009 if (*first_displayed_entry == entry)
1012 entry = *first_displayed_entry;
1013 while (entry && nscrolled < maxscroll) {
1014 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1016 *first_displayed_entry = entry;
1022 static const struct got_error *
1023 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1024 struct commit_queue_entry *last_displayed_entry,
1025 struct commit_queue *commits, struct got_commit_graph *graph,
1026 struct got_repository *repo, const char *path)
1028 const struct got_error *err = NULL;
1029 struct commit_queue_entry *pentry;
1033 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1034 if (pentry == NULL) {
1035 err = fetch_next_commit(&pentry, last_displayed_entry,
1036 commits, graph, repo, path);
1037 if (err || pentry == NULL)
1040 last_displayed_entry = pentry;
1042 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1045 *first_displayed_entry = pentry;
1046 } while (++nscrolled < maxscroll);
1051 static const struct got_error *
1052 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1053 struct commit_queue_entry *entry, struct got_repository *repo)
1055 const struct got_error *err;
1056 struct got_object *obj1 = NULL, *obj2 = NULL;
1057 struct got_object_qid *parent_id;
1058 struct tog_view *diff_view;
1060 err = got_object_open(&obj2, repo, entry->id);
1064 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1066 err = got_object_open(&obj1, repo, parent_id->id);
1071 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1072 if (diff_view == NULL) {
1073 err = got_error_from_errno();
1077 err = open_diff_view(diff_view, obj1, obj2, repo);
1079 *new_view = diff_view;
1082 got_object_close(obj1);
1084 got_object_close(obj2);
1088 static const struct got_error *
1089 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1090 struct commit_queue_entry *entry, struct got_repository *repo)
1092 const struct got_error *err = NULL;
1093 struct got_tree_object *tree;
1094 struct tog_view *tree_view;
1096 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1100 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1101 if (tree_view == NULL)
1102 return got_error_from_errno();
1104 err = open_tree_view(tree_view, tree, entry->id, repo);
1106 got_object_tree_close(tree);
1108 *new_view = tree_view;
1112 static const struct got_error *
1113 set_child_log_view(struct tog_view *view, struct tog_view *child)
1115 struct tog_log_view_state *s = &view->state.log;
1116 struct tog_diff_view_state *ds;
1117 struct commit_queue_entry *commit, *child_entry = NULL;
1118 int selected_idx = 0;
1120 if (child->type != TOG_VIEW_DIFF)
1122 ds = &child->state.diff;
1124 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1125 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1126 child_entry = commit;
1130 if (child_entry == NULL)
1133 commit = s->first_displayed_entry;
1135 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1136 s->selected_entry = child_entry;
1137 s->selected = selected_idx;
1140 if (commit == s->last_displayed_entry)
1143 commit = TAILQ_NEXT(commit, entry);
1146 return show_log_view(view);
1149 static const struct got_error *
1150 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1151 struct got_repository *repo, const char *path)
1153 const struct got_error *err = NULL;
1154 struct got_object_id *head_id = NULL;
1156 struct tog_log_view_state *s = &view->state.log;
1158 err = got_repo_map_path(&s->in_repo_path, repo, path);
1162 err = get_head_commit_id(&head_id, repo);
1166 /* The graph contains all commits. */
1167 err = got_commit_graph_open(&s->graph, head_id, "/", 0, repo);
1170 /* The commit queue contains a subset of commits filtered by path. */
1171 TAILQ_INIT(&s->commits.head);
1172 s->commits.ncommits = 0;
1174 /* Populate commit graph with a sufficient number of commits. */
1175 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1181 * Open the initial batch of commits, sorted in commit graph order.
1182 * We keep all commits open throughout the lifetime of the log view
1183 * in order to avoid having to re-fetch commits from disk while
1184 * updating the display.
1186 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1187 repo, s->in_repo_path);
1189 if (err->code != GOT_ERR_ITER_COMPLETED)
1194 s->first_displayed_entry =
1195 TAILQ_FIRST(&s->commits.head);
1196 s->selected_entry = s->first_displayed_entry;
1199 view->show = show_log_view;
1200 view->input = input_log_view;
1201 view->close = close_log_view;
1202 view->set_child = set_child_log_view;
1208 static const struct got_error *
1209 close_log_view(struct tog_view *view)
1211 struct tog_log_view_state *s = &view->state.log;
1214 got_commit_graph_close(s->graph);
1215 free_commits(&s->commits);
1216 free(s->in_repo_path);
1220 static const struct got_error *
1221 update_diff_child_view(struct tog_view *parent,
1222 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 struct tog_diff_view_state *ds;
1226 struct got_object *obj1 = NULL, *obj2 = NULL;
1227 struct got_object_qid *parent_id;
1228 struct tog_view *child_view = parent->child;
1230 if (child_view == NULL)
1232 if (child_view->type != TOG_VIEW_DIFF)
1234 ds = &child_view->state.diff;
1235 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1238 err = got_object_open(&obj2, repo, selected_entry->id);
1242 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1244 err = got_object_open(&obj1, repo, parent_id->id);
1249 err = close_diff_view(child_view);
1253 err = open_diff_view(child_view, obj1, obj2, repo);
1258 got_object_close(obj1);
1260 got_object_close(obj2);
1264 static const struct got_error *
1265 show_log_view(struct tog_view *view)
1267 const struct got_error *err = NULL;
1268 struct tog_log_view_state *s = &view->state.log;
1270 err = draw_commits(view, &s->last_displayed_entry,
1271 &s->selected_entry, s->first_displayed_entry,
1272 &s->commits, s->selected, view->nlines, s->graph,
1273 s->repo, s->in_repo_path);
1277 return update_diff_child_view(view, s->selected_entry, s->repo);
1280 static const struct got_error *
1281 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1282 struct tog_view *view, int ch)
1284 const struct got_error *err = NULL;
1285 struct tog_log_view_state *s = &view->state.log;
1291 if (s->selected > 0)
1293 if (s->selected > 0)
1295 scroll_up(&s->first_displayed_entry, 1,
1299 if (TAILQ_FIRST(&s->commits.head) ==
1300 s->first_displayed_entry) {
1304 scroll_up(&s->first_displayed_entry,
1305 view->nlines, &s->commits);
1310 if (s->selected < MIN(view->nlines - 2,
1311 s->commits.ncommits - 1)) {
1315 err = scroll_down(&s->first_displayed_entry, 1,
1316 s->last_displayed_entry, &s->commits,
1317 s->graph, s->repo, s->in_repo_path);
1319 if (err->code != GOT_ERR_ITER_COMPLETED)
1325 struct commit_queue_entry *first;
1326 first = s->first_displayed_entry;
1327 err = scroll_down(&s->first_displayed_entry,
1328 view->nlines, s->last_displayed_entry,
1329 &s->commits, s->graph, s->repo,
1333 if (err->code != GOT_ERR_ITER_COMPLETED)
1335 if (first == s->first_displayed_entry &&
1336 s->selected < MIN(view->nlines - 2,
1337 s->commits.ncommits - 1)) {
1338 /* can't scroll further down */
1339 s->selected = MIN(view->nlines - 2,
1340 s->commits.ncommits - 1);
1346 if (s->selected > view->nlines - 2)
1347 s->selected = view->nlines - 2;
1348 if (s->selected > s->commits.ncommits - 1)
1349 s->selected = s->commits.ncommits - 1;
1353 err = show_commit(new_view, view, s->selected_entry,
1357 err = browse_commit(new_view, view, s->selected_entry,
1367 static const struct got_error *
1368 cmd_log(int argc, char *argv[])
1370 const struct got_error *error;
1371 struct got_repository *repo = NULL;
1372 struct got_object_id *start_id = NULL;
1373 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1374 char *start_commit = NULL;
1376 struct tog_view *view;
1379 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1384 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1387 start_commit = optarg;
1390 repo_path = realpath(optarg, NULL);
1391 if (repo_path == NULL)
1392 err(1, "-r option");
1406 path = strdup(argv[0]);
1410 return got_error_from_errno();
1412 cwd = getcwd(NULL, 0);
1414 error = got_error_from_errno();
1417 if (repo_path == NULL) {
1418 repo_path = strdup(cwd);
1419 if (repo_path == NULL) {
1420 error = got_error_from_errno();
1425 error = got_repo_open(&repo, repo_path);
1429 if (start_commit == NULL) {
1430 error = get_head_commit_id(&start_id, repo);
1434 struct got_object *obj;
1435 error = got_object_open_by_id_str(&obj, repo, start_commit);
1436 if (error == NULL) {
1437 start_id = got_object_id_dup(got_object_get_id(obj));
1438 if (start_id == NULL)
1439 error = got_error_from_errno();
1446 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1448 error = got_error_from_errno();
1451 error = open_log_view(view, start_id, repo, path);
1454 error = view_loop(view);
1461 got_repo_close(repo);
1469 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1475 parse_next_line(FILE *f, size_t *len)
1480 const char delim[3] = { '\0', '\0', '\0'};
1482 line = fparseln(f, &linelen, &lineno, delim, 0);
1488 static const struct got_error *
1489 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1490 int *last_displayed_line, int *eof, int max_lines,
1493 const struct got_error *err;
1494 int nlines = 0, nprinted = 0;
1501 werase(view->window);
1504 err = format_line(&wline, &width, header, view->ncols);
1509 if (view_needs_focus_indication(view))
1510 wstandout(view->window);
1511 waddwstr(view->window, wline);
1512 if (view_needs_focus_indication(view))
1513 wstandend(view->window);
1514 if (width < view->ncols)
1515 waddch(view->window, '\n');
1523 while (nprinted < max_lines) {
1524 line = parse_next_line(f, &len);
1529 if (++nlines < *first_displayed_line) {
1534 err = format_line(&wline, &width, line, view->ncols);
1539 waddwstr(view->window, wline);
1540 if (width < view->ncols)
1541 waddch(view->window, '\n');
1542 if (++nprinted == 1)
1543 *first_displayed_line = nlines;
1548 *last_displayed_line = nlines;
1555 static const struct got_error *
1556 open_diff_view(struct tog_view *view, struct got_object *obj1,
1557 struct got_object *obj2, struct got_repository *repo)
1559 const struct got_error *err;
1562 if (obj1 != NULL && obj2 != NULL &&
1563 got_object_get_type(obj1) != got_object_get_type(obj2))
1564 return got_error(GOT_ERR_OBJ_TYPE);
1568 return got_error_from_errno();
1570 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1575 case GOT_OBJ_TYPE_TREE:
1576 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1578 case GOT_OBJ_TYPE_COMMIT:
1579 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1582 return got_error(GOT_ERR_OBJ_TYPE);
1587 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1588 view->state.diff.id2 = got_object_get_id(obj2);
1589 view->state.diff.f = f;
1590 view->state.diff.first_displayed_line = 1;
1591 view->state.diff.last_displayed_line = view->nlines;
1593 view->show = show_diff_view;
1594 view->input = input_diff_view;
1595 view->close = close_diff_view;
1600 static const struct got_error *
1601 close_diff_view(struct tog_view *view)
1603 const struct got_error *err = NULL;
1605 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1606 err = got_error_from_errno();
1610 static const struct got_error *
1611 show_diff_view(struct tog_view *view)
1613 const struct got_error *err;
1614 struct tog_diff_view_state *s = &view->state.diff;
1615 char *id_str1 = NULL, *id_str2, *header;
1618 err = got_object_id_str(&id_str1, s->id1);
1622 err = got_object_id_str(&id_str2, s->id2);
1626 if (asprintf(&header, "diff: %s %s",
1627 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1628 err = got_error_from_errno();
1636 return draw_file(view, s->f, &s->first_displayed_line,
1637 &s->last_displayed_line, &s->eof, view->nlines,
1641 static const struct got_error *
1642 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1643 struct tog_view *view, int ch)
1645 const struct got_error *err = NULL;
1646 struct tog_diff_view_state *s = &view->state.diff;
1652 if (s->first_displayed_line > 1)
1653 s->first_displayed_line--;
1657 while (i++ < view->nlines - 1 &&
1658 s->first_displayed_line > 1)
1659 s->first_displayed_line--;
1664 s->first_displayed_line++;
1669 while (!s->eof && i++ < view->nlines - 1) {
1671 line = parse_next_line(s->f, NULL);
1672 s->first_displayed_line++;
1679 struct tog_log_view_state *ls;
1680 struct commit_queue_entry *entry;
1681 struct tog_view *diff_view;
1683 if (view->parent == NULL)
1685 if (view->parent->type != TOG_VIEW_LOG)
1687 ls = &view->parent->state.log;
1690 entry = TAILQ_PREV(ls->selected_entry,
1691 commit_queue_head, entry);
1693 entry = TAILQ_NEXT(ls->selected_entry, entry);
1694 if (entry == NULL) {
1695 err = fetch_next_commit(&entry,
1697 &ls->commits, ls->graph,
1698 ls->repo, ls->in_repo_path);
1705 err = show_commit(&diff_view, view->parent,
1709 *new_view = diff_view;
1720 static const struct got_error *
1721 cmd_diff(int argc, char *argv[])
1723 const struct got_error *error = NULL;
1724 struct got_repository *repo = NULL;
1725 struct got_object *obj1 = NULL, *obj2 = NULL;
1726 char *repo_path = NULL;
1727 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1729 struct tog_view *view;
1732 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1737 while ((ch = getopt(argc, argv, "")) != -1) {
1749 usage_diff(); /* TODO show local worktree changes */
1750 } else if (argc == 2) {
1751 repo_path = getcwd(NULL, 0);
1752 if (repo_path == NULL)
1753 return got_error_from_errno();
1754 obj_id_str1 = argv[0];
1755 obj_id_str2 = argv[1];
1756 } else if (argc == 3) {
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL)
1759 return got_error_from_errno();
1760 obj_id_str1 = argv[1];
1761 obj_id_str2 = argv[2];
1765 error = got_repo_open(&repo, repo_path);
1770 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1774 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1778 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1780 error = got_error_from_errno();
1783 error = open_diff_view(view, obj1, obj2, repo);
1786 error = view_loop(view);
1788 got_repo_close(repo);
1790 got_object_close(obj1);
1792 got_object_close(obj2);
1800 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1805 struct tog_blame_line {
1807 struct got_object_id *id;
1810 static const struct got_error *
1811 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1812 const char *path, struct tog_blame_line *lines, int nlines,
1813 int blame_complete, int selected_line, int *first_displayed_line,
1814 int *last_displayed_line, int *eof, int max_lines)
1816 const struct got_error *err;
1817 int lineno = 0, nprinted = 0;
1822 struct tog_blame_line *blame_line;
1823 struct got_object_id *prev_id = NULL;
1826 err = got_object_id_str(&id_str, id);
1831 werase(view->window);
1833 if (asprintf(&line, "commit: %s", id_str) == -1) {
1834 err = got_error_from_errno();
1839 err = format_line(&wline, &width, line, view->ncols);
1842 if (view_needs_focus_indication(view))
1843 wstandout(view->window);
1844 waddwstr(view->window, wline);
1845 if (view_needs_focus_indication(view))
1846 wstandend(view->window);
1849 if (width < view->ncols)
1850 waddch(view->window, '\n');
1852 if (asprintf(&line, "[%d/%d] %s%s",
1853 *first_displayed_line - 1 + selected_line, nlines,
1854 blame_complete ? "" : "annotating ", path) == -1) {
1856 return got_error_from_errno();
1859 err = format_line(&wline, &width, line, view->ncols);
1864 waddwstr(view->window, wline);
1867 if (width < view->ncols)
1868 waddch(view->window, '\n');
1871 while (nprinted < max_lines - 2) {
1872 line = parse_next_line(f, &len);
1877 if (++lineno < *first_displayed_line) {
1882 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1883 err = format_line(&wline, &width, line, wlimit);
1889 if (nprinted == selected_line - 1)
1890 wstandout(view->window);
1892 blame_line = &lines[lineno - 1];
1893 if (blame_line->annotated && prev_id &&
1894 got_object_id_cmp(prev_id, blame_line->id) == 0)
1895 waddstr(view->window, " ");
1896 else if (blame_line->annotated) {
1898 err = got_object_id_str(&id_str, blame_line->id);
1904 wprintw(view->window, "%.8s ", id_str);
1906 prev_id = blame_line->id;
1908 waddstr(view->window, "........ ");
1912 waddwstr(view->window, wline);
1913 while (width < wlimit) {
1914 waddch(view->window, ' ');
1917 if (nprinted == selected_line - 1)
1918 wstandend(view->window);
1919 if (++nprinted == 1)
1920 *first_displayed_line = lineno;
1925 *last_displayed_line = lineno;
1932 static const struct got_error *
1933 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1935 const struct got_error *err = NULL;
1936 struct tog_blame_cb_args *a = arg;
1937 struct tog_blame_line *line;
1939 if (nlines != a->nlines ||
1940 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1941 return got_error(GOT_ERR_RANGE);
1943 if (pthread_mutex_lock(a->mutex) != 0)
1944 return got_error_from_errno();
1946 if (*a->quit) { /* user has quit the blame view */
1947 err = got_error(GOT_ERR_ITER_COMPLETED);
1952 goto done; /* no change in this commit */
1954 line = &a->lines[lineno - 1];
1955 if (line->annotated)
1958 line->id = got_object_id_dup(id);
1959 if (line->id == NULL) {
1960 err = got_error_from_errno();
1963 line->annotated = 1;
1965 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1966 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1967 a->last_displayed_line, a->eof, a->view->nlines);
1969 if (pthread_mutex_unlock(a->mutex) != 0)
1970 return got_error_from_errno();
1975 blame_thread(void *arg)
1977 const struct got_error *err;
1978 struct tog_blame_thread_args *ta = arg;
1979 struct tog_blame_cb_args *a = ta->cb_args;
1981 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1982 blame_cb, ta->cb_args);
1984 if (pthread_mutex_lock(a->mutex) != 0)
1985 return (void *)got_error_from_errno();
1987 got_repo_close(ta->repo);
1991 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1992 a->lines, a->nlines, 1, *a->selected_line,
1993 a->first_displayed_line, a->last_displayed_line, a->eof,
1996 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1997 err = got_error_from_errno();
2002 static struct got_object_id *
2003 get_selected_commit_id(struct tog_blame_line *lines,
2004 int first_displayed_line, int selected_line)
2006 struct tog_blame_line *line;
2008 line = &lines[first_displayed_line - 1 + selected_line - 1];
2009 if (!line->annotated)
2015 static const struct got_error *
2016 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2017 struct tog_blame_line *lines, int first_displayed_line,
2018 int selected_line, struct got_repository *repo)
2020 const struct got_error *err = NULL;
2021 struct got_commit_object *commit = NULL;
2022 struct got_object_id *selected_id;
2023 struct got_object_qid *pid;
2028 selected_id = get_selected_commit_id(lines,
2029 first_displayed_line, selected_line);
2030 if (selected_id == NULL)
2033 err = got_object_open(obj, repo, selected_id);
2037 err = got_object_commit_open(&commit, repo, *obj);
2041 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2043 err = got_object_open(pobj, repo, pid->id);
2049 got_object_commit_close(commit);
2053 static const struct got_error *
2054 stop_blame(struct tog_blame *blame)
2056 const struct got_error *err = NULL;
2059 if (blame->thread) {
2060 if (pthread_join(blame->thread, (void **)&err) != 0)
2061 err = got_error_from_errno();
2062 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2064 blame->thread = NULL;
2066 if (blame->thread_args.repo) {
2067 got_repo_close(blame->thread_args.repo);
2068 blame->thread_args.repo = NULL;
2074 for (i = 0; i < blame->nlines; i++)
2075 free(blame->lines[i].id);
2077 blame->lines = NULL;
2078 free(blame->cb_args.commit_id);
2079 blame->cb_args.commit_id = NULL;
2084 static const struct got_error *
2085 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2086 struct tog_view *view, int *blame_complete,
2087 int *first_displayed_line, int *last_displayed_line,
2088 int *selected_line, int *done, int *eof, const char *path,
2089 struct got_object_id *commit_id,
2090 struct got_repository *repo)
2092 const struct got_error *err = NULL;
2093 struct got_blob_object *blob = NULL;
2094 struct got_repository *thread_repo = NULL;
2095 struct got_object_id *obj_id = NULL;
2096 struct got_object *obj = NULL;
2098 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2102 err = got_object_open(&obj, repo, obj_id);
2106 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2107 err = got_error(GOT_ERR_OBJ_TYPE);
2111 err = got_object_blob_open(&blob, repo, obj, 8192);
2114 blame->f = got_opentemp();
2115 if (blame->f == NULL) {
2116 err = got_error_from_errno();
2119 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2124 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2125 if (blame->lines == NULL) {
2126 err = got_error_from_errno();
2130 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2134 blame->cb_args.view = view;
2135 blame->cb_args.lines = blame->lines;
2136 blame->cb_args.nlines = blame->nlines;
2137 blame->cb_args.mutex = mutex;
2138 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2139 if (blame->cb_args.commit_id == NULL) {
2140 err = got_error_from_errno();
2143 blame->cb_args.f = blame->f;
2144 blame->cb_args.path = path;
2145 blame->cb_args.first_displayed_line = first_displayed_line;
2146 blame->cb_args.selected_line = selected_line;
2147 blame->cb_args.last_displayed_line = last_displayed_line;
2148 blame->cb_args.quit = done;
2149 blame->cb_args.eof = eof;
2151 blame->thread_args.path = path;
2152 blame->thread_args.repo = thread_repo;
2153 blame->thread_args.cb_args = &blame->cb_args;
2154 blame->thread_args.complete = blame_complete;
2155 *blame_complete = 0;
2157 if (pthread_create(&blame->thread, NULL, blame_thread,
2158 &blame->thread_args) != 0) {
2159 err = got_error_from_errno();
2165 got_object_blob_close(blob);
2168 got_object_close(obj);
2174 static const struct got_error *
2175 open_blame_view(struct tog_view *view, char *path,
2176 struct got_object_id *commit_id, struct got_repository *repo)
2178 const struct got_error *err = NULL;
2179 struct tog_blame_view_state *s = &view->state.blame;
2181 SIMPLEQ_INIT(&s->blamed_commits);
2183 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2184 return got_error_from_errno();
2186 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2190 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2191 s->first_displayed_line = 1;
2192 s->last_displayed_line = view->nlines;
2193 s->selected_line = 1;
2194 s->blame_complete = 0;
2196 if (s->path == NULL)
2197 return got_error_from_errno();
2199 s->commit_id = commit_id;
2200 memset(&s->blame, 0, sizeof(s->blame));
2202 view->show = show_blame_view;
2203 view->input = input_blame_view;
2204 view->close = close_blame_view;
2206 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2207 &s->first_displayed_line, &s->last_displayed_line,
2208 &s->selected_line, &s->done, &s->eof, s->path,
2209 s->blamed_commit->id, s->repo);
2212 static const struct got_error *
2213 close_blame_view(struct tog_view *view)
2215 const struct got_error *err = NULL;
2216 struct tog_blame_view_state *s = &view->state.blame;
2218 if (s->blame.thread)
2219 err = stop_blame(&s->blame);
2221 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2222 struct got_object_qid *blamed_commit;
2223 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2224 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2225 got_object_qid_free(blamed_commit);
2233 static const struct got_error *
2234 show_blame_view(struct tog_view *view)
2236 const struct got_error *err = NULL;
2237 struct tog_blame_view_state *s = &view->state.blame;
2239 if (pthread_mutex_lock(&s->mutex) != 0)
2240 return got_error_from_errno();
2242 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2243 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2244 s->selected_line, &s->first_displayed_line,
2245 &s->last_displayed_line, &s->eof, view->nlines);
2247 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2248 err = got_error_from_errno();
2253 static const struct got_error *
2254 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2255 struct tog_view *view, int ch)
2257 const struct got_error *err = NULL, *thread_err = NULL;
2258 struct got_object *obj = NULL, *pobj = NULL;
2259 struct tog_view *diff_view;
2260 struct tog_blame_view_state *s = &view->state.blame;
2262 if (pthread_mutex_lock(&s->mutex) != 0) {
2263 err = got_error_from_errno();
2270 if (pthread_mutex_unlock(&s->mutex) != 0) {
2271 err = got_error_from_errno();
2274 return stop_blame(&s->blame);
2277 if (s->selected_line > 1)
2279 else if (s->selected_line == 1 &&
2280 s->first_displayed_line > 1)
2281 s->first_displayed_line--;
2284 if (s->first_displayed_line == 1) {
2285 s->selected_line = 1;
2288 if (s->first_displayed_line > view->nlines - 2)
2289 s->first_displayed_line -=
2292 s->first_displayed_line = 1;
2296 if (s->selected_line < view->nlines - 2 &&
2297 s->first_displayed_line +
2298 s->selected_line <= s->blame.nlines)
2300 else if (s->last_displayed_line <
2302 s->first_displayed_line++;
2306 struct got_object_id *id;
2307 id = get_selected_commit_id(s->blame.lines,
2308 s->first_displayed_line, s->selected_line);
2309 if (id == NULL || got_object_id_cmp(id,
2310 s->blamed_commit->id) == 0)
2312 err = open_selected_commit(&pobj, &obj,
2313 s->blame.lines, s->first_displayed_line,
2314 s->selected_line, s->repo);
2317 if (pobj == NULL && obj == NULL)
2319 if (ch == 'p' && pobj == NULL)
2322 if (pthread_mutex_unlock(&s->mutex) != 0) {
2323 err = got_error_from_errno();
2326 thread_err = stop_blame(&s->blame);
2328 if (pthread_mutex_lock(&s->mutex) != 0) {
2329 err = got_error_from_errno();
2334 id = got_object_get_id(ch == 'b' ? obj : pobj);
2335 got_object_close(obj);
2338 got_object_close(pobj);
2341 err = got_object_qid_alloc(&s->blamed_commit, id);
2344 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2345 s->blamed_commit, entry);
2346 err = run_blame(&s->blame, &s->mutex, view,
2348 &s->first_displayed_line,
2349 &s->last_displayed_line,
2350 &s->selected_line, &s->done, &s->eof,
2351 s->path, s->blamed_commit->id, s->repo);
2357 struct got_object_qid *first;
2358 first = SIMPLEQ_FIRST(&s->blamed_commits);
2359 if (!got_object_id_cmp(first->id, s->commit_id))
2362 if (pthread_mutex_unlock(&s->mutex) != 0) {
2363 err = got_error_from_errno();
2366 thread_err = stop_blame(&s->blame);
2368 if (pthread_mutex_lock(&s->mutex) != 0) {
2369 err = got_error_from_errno();
2374 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2375 got_object_qid_free(s->blamed_commit);
2377 SIMPLEQ_FIRST(&s->blamed_commits);
2378 err = run_blame(&s->blame, &s->mutex, view,
2380 &s->first_displayed_line,
2381 &s->last_displayed_line,
2382 &s->selected_line, &s->done, &s->eof, s->path,
2383 s->blamed_commit->id, s->repo);
2390 err = open_selected_commit(&pobj, &obj,
2391 s->blame.lines, s->first_displayed_line,
2392 s->selected_line, s->repo);
2395 if (pobj == NULL && obj == NULL)
2397 diff_view = view_open(0, 0, 0, 0, view,
2399 if (diff_view == NULL) {
2400 err = got_error_from_errno();
2403 err = open_diff_view(diff_view, pobj, obj,
2406 view_close(diff_view);
2409 *new_view = diff_view;
2411 got_object_close(pobj);
2414 got_object_close(obj);
2421 if (s->last_displayed_line >= s->blame.nlines &&
2422 s->selected_line < view->nlines - 2) {
2423 s->selected_line = MIN(s->blame.nlines,
2427 if (s->last_displayed_line + view->nlines - 2
2429 s->first_displayed_line +=
2432 s->first_displayed_line =
2437 if (s->selected_line > view->nlines - 2) {
2438 s->selected_line = MIN(s->blame.nlines,
2446 if (pthread_mutex_unlock(&s->mutex) != 0)
2447 err = got_error_from_errno();
2450 got_object_close(pobj);
2451 return thread_err ? thread_err : err;
2454 static const struct got_error *
2455 cmd_blame(int argc, char *argv[])
2457 const struct got_error *error;
2458 struct got_repository *repo = NULL;
2459 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2460 struct got_object_id *commit_id = NULL;
2461 char *commit_id_str = NULL;
2463 struct tog_view *view;
2466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2471 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2474 commit_id_str = optarg;
2477 repo_path = realpath(optarg, NULL);
2478 if (repo_path == NULL)
2479 err(1, "-r option");
2495 cwd = getcwd(NULL, 0);
2497 error = got_error_from_errno();
2500 if (repo_path == NULL) {
2501 repo_path = strdup(cwd);
2502 if (repo_path == NULL) {
2503 error = got_error_from_errno();
2509 error = got_repo_open(&repo, repo_path);
2513 error = got_repo_map_path(&in_repo_path, repo, path);
2517 if (commit_id_str == NULL) {
2518 struct got_reference *head_ref;
2519 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2522 error = got_ref_resolve(&commit_id, repo, head_ref);
2523 got_ref_close(head_ref);
2525 struct got_object *obj;
2526 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2529 commit_id = got_object_id_dup(got_object_get_id(obj));
2530 if (commit_id == NULL)
2531 error = got_error_from_errno();
2532 got_object_close(obj);
2537 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2539 error = got_error_from_errno();
2542 error = open_blame_view(view, in_repo_path, commit_id, repo);
2545 error = view_loop(view);
2551 got_repo_close(repo);
2555 static const struct got_error *
2556 draw_tree_entries(struct tog_view *view,
2557 struct got_tree_entry **first_displayed_entry,
2558 struct got_tree_entry **last_displayed_entry,
2559 struct got_tree_entry **selected_entry, int *ndisplayed,
2560 const char *label, int show_ids, const char *parent_path,
2561 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2563 const struct got_error *err = NULL;
2564 struct got_tree_entry *te;
2570 werase(view->window);
2575 err = format_line(&wline, &width, label, view->ncols);
2578 if (view_needs_focus_indication(view))
2579 wstandout(view->window);
2580 waddwstr(view->window, wline);
2581 if (view_needs_focus_indication(view))
2582 wstandend(view->window);
2585 if (width < view->ncols)
2586 waddch(view->window, '\n');
2589 err = format_line(&wline, &width, parent_path, view->ncols);
2592 waddwstr(view->window, wline);
2595 if (width < view->ncols)
2596 waddch(view->window, '\n');
2599 waddch(view->window, '\n');
2603 te = SIMPLEQ_FIRST(&entries->head);
2604 if (*first_displayed_entry == NULL) {
2605 if (selected == 0) {
2606 wstandout(view->window);
2607 *selected_entry = NULL;
2609 waddstr(view->window, " ..\n"); /* parent directory */
2611 wstandend(view->window);
2618 while (te != *first_displayed_entry)
2619 te = SIMPLEQ_NEXT(te, entry);
2623 char *line = NULL, *id_str = NULL;
2626 err = got_object_id_str(&id_str, te->id);
2628 return got_error_from_errno();
2630 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2631 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2633 return got_error_from_errno();
2636 err = format_line(&wline, &width, line, view->ncols);
2641 if (n == selected) {
2642 wstandout(view->window);
2643 *selected_entry = te;
2645 waddwstr(view->window, wline);
2646 if (width < view->ncols)
2647 waddch(view->window, '\n');
2649 wstandend(view->window);
2655 *last_displayed_entry = te;
2658 te = SIMPLEQ_NEXT(te, entry);
2666 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2667 const struct got_tree_entries *entries, int isroot)
2669 struct got_tree_entry *te, *prev;
2672 if (*first_displayed_entry == NULL)
2675 te = SIMPLEQ_FIRST(&entries->head);
2676 if (*first_displayed_entry == te) {
2678 *first_displayed_entry = NULL;
2682 /* XXX this is stupid... switch to TAILQ? */
2683 for (i = 0; i < maxscroll; i++) {
2684 while (te != *first_displayed_entry) {
2686 te = SIMPLEQ_NEXT(te, entry);
2688 *first_displayed_entry = prev;
2689 te = SIMPLEQ_FIRST(&entries->head);
2691 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2692 *first_displayed_entry = NULL;
2696 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2697 struct got_tree_entry *last_displayed_entry,
2698 const struct got_tree_entries *entries)
2700 struct got_tree_entry *next;
2703 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2706 if (*first_displayed_entry)
2707 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2709 next = SIMPLEQ_FIRST(&entries->head);
2711 *first_displayed_entry = next;
2712 if (++n >= maxscroll)
2714 next = SIMPLEQ_NEXT(next, entry);
2718 static const struct got_error *
2719 tree_entry_path(char **path, struct tog_parent_trees *parents,
2720 struct got_tree_entry *te)
2722 const struct got_error *err = NULL;
2723 struct tog_parent_tree *pt;
2724 size_t len = 2; /* for leading slash and NUL */
2726 TAILQ_FOREACH(pt, parents, entry)
2727 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2729 len += strlen(te->name);
2731 *path = calloc(1, len);
2733 return got_error_from_errno();
2736 pt = TAILQ_LAST(parents, tog_parent_trees);
2738 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2739 err = got_error(GOT_ERR_NO_SPACE);
2742 if (strlcat(*path, "/", len) >= len) {
2743 err = got_error(GOT_ERR_NO_SPACE);
2746 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2749 if (strlcat(*path, te->name, len) >= len) {
2750 err = got_error(GOT_ERR_NO_SPACE);
2762 static const struct got_error *
2763 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2764 struct got_tree_entry *te, struct tog_parent_trees *parents,
2765 struct got_object_id *commit_id, struct got_repository *repo)
2767 const struct got_error *err = NULL;
2769 struct tog_view *blame_view;
2771 err = tree_entry_path(&path, parents, te);
2775 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2776 if (blame_view == NULL)
2777 return got_error_from_errno();
2779 err = open_blame_view(blame_view, path, commit_id, repo);
2781 view_close(blame_view);
2784 *new_view = blame_view;
2788 static const struct got_error *
2789 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2790 struct got_tree_entry *te, struct tog_parent_trees *parents,
2791 struct got_object_id *commit_id, struct got_repository *repo)
2793 struct tog_view *log_view;
2794 const struct got_error *err = NULL;
2797 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2798 if (log_view == NULL)
2799 return got_error_from_errno();
2801 err = tree_entry_path(&path, parents, te);
2805 err = open_log_view(log_view, commit_id, repo, path);
2807 view_close(log_view);
2809 *new_view = log_view;
2814 static const struct got_error *
2815 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2816 struct got_object_id *commit_id, struct got_repository *repo)
2818 const struct got_error *err = NULL;
2819 char *commit_id_str = NULL;
2820 struct tog_tree_view_state *s = &view->state.tree;
2822 TAILQ_INIT(&s->parents);
2824 err = got_object_id_str(&commit_id_str, commit_id);
2828 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2829 err = got_error_from_errno();
2833 s->root = s->tree = root;
2834 s->entries = got_object_tree_get_entries(root);
2835 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2836 s->commit_id = commit_id;
2839 view->show = show_tree_view;
2840 view->input = input_tree_view;
2841 view->close = close_tree_view;
2843 free(commit_id_str);
2845 free(s->tree_label);
2849 static const struct got_error *
2850 close_tree_view(struct tog_view *view)
2852 struct tog_tree_view_state *s = &view->state.tree;
2854 free(s->tree_label);
2855 while (!TAILQ_EMPTY(&s->parents)) {
2856 struct tog_parent_tree *parent;
2857 parent = TAILQ_FIRST(&s->parents);
2858 TAILQ_REMOVE(&s->parents, parent, entry);
2862 if (s->tree != s->root)
2863 got_object_tree_close(s->tree);
2864 got_object_tree_close(s->root);
2869 static const struct got_error *
2870 show_tree_view(struct tog_view *view)
2872 const struct got_error *err = NULL;
2873 struct tog_tree_view_state *s = &view->state.tree;
2876 err = tree_entry_path(&parent_path, &s->parents, NULL);
2880 err = draw_tree_entries(view, &s->first_displayed_entry,
2881 &s->last_displayed_entry, &s->selected_entry,
2882 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2883 s->entries, s->selected, view->nlines, s->tree == s->root);
2888 static const struct got_error *
2889 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2890 struct tog_view *view, int ch)
2892 const struct got_error *err = NULL;
2893 struct tog_tree_view_state *s = &view->state.tree;
2897 s->show_ids = !s->show_ids;
2900 if (s->selected_entry) {
2901 err = log_tree_entry(new_view, view,
2902 s->selected_entry, &s->parents,
2903 s->commit_id, s->repo);
2908 if (s->selected > 0)
2910 if (s->selected > 0)
2912 tree_scroll_up(&s->first_displayed_entry, 1,
2913 s->entries, s->tree == s->root);
2916 if (SIMPLEQ_FIRST(&s->entries->head) ==
2917 s->first_displayed_entry) {
2918 if (s->tree != s->root)
2919 s->first_displayed_entry = NULL;
2923 tree_scroll_up(&s->first_displayed_entry,
2924 view->nlines, s->entries,
2925 s->tree == s->root);
2929 if (s->selected < s->ndisplayed - 1) {
2933 tree_scroll_down(&s->first_displayed_entry, 1,
2934 s->last_displayed_entry, s->entries);
2937 tree_scroll_down(&s->first_displayed_entry,
2938 view->nlines, s->last_displayed_entry,
2940 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2943 /* can't scroll any further; move cursor down */
2944 if (s->selected < s->ndisplayed - 1)
2945 s->selected = s->ndisplayed - 1;
2949 if (s->selected_entry == NULL) {
2950 struct tog_parent_tree *parent;
2952 /* user selected '..' */
2953 if (s->tree == s->root)
2955 parent = TAILQ_FIRST(&s->parents);
2956 TAILQ_REMOVE(&s->parents, parent,
2958 got_object_tree_close(s->tree);
2959 s->tree = parent->tree;
2961 got_object_tree_get_entries(s->tree);
2962 s->first_displayed_entry =
2963 parent->first_displayed_entry;
2965 parent->selected_entry;
2966 s->selected = parent->selected;
2968 } else if (S_ISDIR(s->selected_entry->mode)) {
2969 struct tog_parent_tree *parent;
2970 struct got_tree_object *child;
2971 err = got_object_open_as_tree(&child,
2972 s->repo, s->selected_entry->id);
2975 parent = calloc(1, sizeof(*parent));
2976 if (parent == NULL) {
2977 err = got_error_from_errno();
2980 parent->tree = s->tree;
2981 parent->first_displayed_entry =
2982 s->first_displayed_entry;
2983 parent->selected_entry = s->selected_entry;
2984 parent->selected = s->selected;
2985 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2988 got_object_tree_get_entries(s->tree);
2990 s->first_displayed_entry = NULL;
2991 } else if (S_ISREG(s->selected_entry->mode)) {
2992 err = blame_tree_entry(new_view, view,
2993 s->selected_entry, &s->parents,
2994 s->commit_id, s->repo);
3000 if (s->selected > view->nlines)
3001 s->selected = s->ndisplayed - 1;
3014 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3019 static const struct got_error *
3020 cmd_tree(int argc, char *argv[])
3022 const struct got_error *error;
3023 struct got_repository *repo = NULL;
3024 char *repo_path = NULL;
3025 struct got_object_id *commit_id = NULL;
3026 char *commit_id_arg = NULL;
3027 struct got_commit_object *commit = NULL;
3028 struct got_tree_object *tree = NULL;
3030 struct tog_view *view;
3033 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3038 while ((ch = getopt(argc, argv, "c:")) != -1) {
3041 commit_id_arg = optarg;
3053 repo_path = getcwd(NULL, 0);
3054 if (repo_path == NULL)
3055 return got_error_from_errno();
3056 } else if (argc == 1) {
3057 repo_path = realpath(argv[0], NULL);
3058 if (repo_path == NULL)
3059 return got_error_from_errno();
3063 error = got_repo_open(&repo, repo_path);
3068 if (commit_id_arg == NULL) {
3069 error = get_head_commit_id(&commit_id, repo);
3073 struct got_object *obj;
3074 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3075 if (error == NULL) {
3076 commit_id = got_object_id_dup(got_object_get_id(obj));
3077 if (commit_id == NULL)
3078 error = got_error_from_errno();
3084 error = got_object_open_as_commit(&commit, repo, commit_id);
3088 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3092 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3094 error = got_error_from_errno();
3097 error = open_tree_view(view, tree, commit_id, repo);
3100 error = view_loop(view);
3104 got_object_commit_close(commit);
3106 got_object_tree_close(tree);
3108 got_repo_close(repo);
3118 intrflush(stdscr, FALSE);
3119 keypad(stdscr, TRUE);
3128 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3129 "Available commands:\n", getprogname());
3130 for (i = 0; i < nitems(tog_commands); i++) {
3131 struct tog_cmd *cmd = &tog_commands[i];
3132 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3138 make_argv(const char *arg0, const char *arg1)
3141 int argc = (arg1 == NULL ? 1 : 2);
3143 argv = calloc(argc, sizeof(char *));
3146 argv[0] = strdup(arg0);
3147 if (argv[0] == NULL)
3150 argv[1] = strdup(arg1);
3151 if (argv[1] == NULL)
3159 main(int argc, char *argv[])
3161 const struct got_error *error = NULL;
3162 struct tog_cmd *cmd = NULL;
3164 char **cmd_argv = NULL;
3166 setlocale(LC_ALL, "");
3168 while ((ch = getopt(argc, argv, "h")) != -1) {
3187 /* Build an argument vector which runs a default command. */
3188 cmd = &tog_commands[0];
3189 cmd_argv = make_argv(cmd->name, NULL);
3194 /* Did the user specific a command? */
3195 for (i = 0; i < nitems(tog_commands); i++) {
3196 if (strncmp(tog_commands[i].name, argv[0],
3197 strlen(argv[0])) == 0) {
3198 cmd = &tog_commands[i];
3200 tog_commands[i].cmd_usage();
3205 /* Did the user specify a repository? */
3206 char *repo_path = realpath(argv[0], NULL);
3208 struct got_repository *repo;
3209 error = got_repo_open(&repo, repo_path);
3211 got_repo_close(repo);
3213 error = got_error_from_errno();
3216 fprintf(stderr, "%s: '%s' is not a "
3217 "known command\n", getprogname(),
3221 fprintf(stderr, "%s: '%s' is neither a known "
3222 "command nor a path to a repository\n",
3223 getprogname(), argv[0]);
3227 cmd = &tog_commands[0];
3228 cmd_argv = make_argv(cmd->name, repo_path);
3236 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3243 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);