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 *id;
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);
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);
406 err = view->input(new, dead, view, ch);
413 err = view_resize(view);
416 err = view->input(new, dead, view, ch);
419 err = view->input(new, dead, view, ch);
426 static const struct got_error *
427 view_set_child(struct tog_view *view, struct tog_view *child)
429 const struct got_error *err;
431 if (view->set_child) {
432 err = view->set_child(view, child);
442 view_vborder(struct tog_view *view)
444 if (view->child == NULL)
447 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
448 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
451 static const struct got_error *
452 view_loop(struct tog_view *view)
454 const struct got_error *err = NULL;
455 struct tog_view_list_head views;
456 struct tog_view *new_view, *dead_view;
460 TAILQ_INSERT_HEAD(&views, view, entry);
463 while (!TAILQ_EMPTY(&views) && !done) {
464 err = view_show(view);
467 err = view_input(&new_view, &dead_view, &view, &done,
472 TAILQ_REMOVE(&views, dead_view, entry);
473 TAILQ_FOREACH(view, &views, entry) {
474 if (view->parent == dead_view)
477 if (dead_view->parent)
478 view = dead_view->parent;
480 view = TAILQ_LAST(&views, tog_view_list_head);
481 if (dead_view->child) {
482 TAILQ_REMOVE(&views, dead_view->child, entry);
483 err = view_close(dead_view->child);
487 err = view_close(dead_view);
492 /* TODO: de-duplicate! */
493 TAILQ_INSERT_TAIL(&views, new_view, entry);
494 if (new_view->parent) {
495 err = view_set_child(new_view->parent, new_view);
498 new_view->parent->focussed = 0;
505 while (!TAILQ_EMPTY(&views)) {
506 view = TAILQ_FIRST(&views);
507 TAILQ_REMOVE(&views, view, entry);
518 "usage: %s log [-c commit] [-r repository-path] [path]\n",
523 /* Create newly allocated wide-character string equivalent to a byte string. */
524 static const struct got_error *
525 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
528 const struct got_error *err = NULL;
531 *wlen = mbstowcs(NULL, s, 0);
532 if (*wlen == (size_t)-1) {
535 return got_error_from_errno();
537 /* byte string invalid in current encoding; try to "fix" it */
538 err = got_mbsavis(&vis, &vislen, s);
541 *wlen = mbstowcs(NULL, vis, 0);
542 if (*wlen == (size_t)-1) {
543 err = got_error_from_errno(); /* give up */
548 *ws = calloc(*wlen + 1, sizeof(*ws));
550 err = got_error_from_errno();
554 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
555 err = got_error_from_errno();
566 /* Format a line for display, ensuring that it won't overflow a width limit. */
567 static const struct got_error *
568 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
570 const struct got_error *err = NULL;
572 wchar_t *wline = NULL;
579 err = mbs2ws(&wline, &wlen, line);
584 while (i < wlen && cols < wlimit) {
585 int width = wcwidth(wline[i]);
592 if (cols + width <= wlimit) {
598 if (wline[i] == L'\t')
599 cols += TABSIZE - ((cols + 1) % TABSIZE);
603 err = got_error_from_errno();
618 static const struct got_error *
619 draw_commit(struct tog_view *view, struct got_commit_object *commit,
620 struct got_object_id *id)
622 const struct got_error *err = NULL;
623 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
624 char *logmsg0 = NULL, *logmsg = NULL;
625 char *author0 = NULL, *author = NULL;
626 wchar_t *wlogmsg = NULL, *wauthor = NULL;
627 int author_width, logmsg_width;
628 char *newline, *smallerthan;
631 static const size_t date_display_cols = 9;
632 static const size_t author_display_cols = 16;
633 const int avail = view->ncols;
635 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
636 &commit->tm_committer) >= sizeof(datebuf))
637 return got_error(GOT_ERR_NO_SPACE);
639 if (avail < date_display_cols)
640 limit = MIN(sizeof(datebuf) - 1, avail);
642 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
643 waddnstr(view->window, datebuf, limit);
648 author0 = strdup(commit->author);
649 if (author0 == NULL) {
650 err = got_error_from_errno();
654 smallerthan = strchr(author, '<');
658 char *at = strchr(author, '@');
663 err = format_line(&wauthor, &author_width, author, limit);
666 waddwstr(view->window, wauthor);
668 while (col <= avail && author_width < author_display_cols + 1) {
669 waddch(view->window, ' ');
676 logmsg0 = strdup(commit->logmsg);
677 if (logmsg0 == NULL) {
678 err = got_error_from_errno();
682 while (*logmsg == '\n')
684 newline = strchr(logmsg, '\n');
688 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
691 waddwstr(view->window, wlogmsg);
693 while (col <= avail) {
694 waddch(view->window, ' ');
706 static struct commit_queue_entry *
707 alloc_commit_queue_entry(struct got_commit_object *commit,
708 struct got_object_id *id)
710 struct commit_queue_entry *entry;
712 entry = calloc(1, sizeof(*entry));
717 entry->commit = commit;
722 pop_commit(struct commit_queue *commits)
724 struct commit_queue_entry *entry;
726 entry = TAILQ_FIRST(&commits->head);
727 TAILQ_REMOVE(&commits->head, entry, entry);
728 got_object_commit_close(entry->commit);
730 /* Don't free entry->id! It is owned by the commit graph. */
735 free_commits(struct commit_queue *commits)
737 while (!TAILQ_EMPTY(&commits->head))
741 static const struct got_error *
742 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
743 struct got_object_id *start_id, int minqueue, int init,
744 struct got_repository *repo, const char *path)
746 const struct got_error *err = NULL;
747 struct got_object_id *id;
748 struct commit_queue_entry *entry;
749 int nfetched, nqueued = 0, found_obj = 0;
750 int is_root_path = strcmp(path, "/") == 0;
752 err = got_commit_graph_iter_start(graph, start_id);
756 entry = TAILQ_LAST(&commits->head, commit_queue_head);
757 if (entry && got_object_id_cmp(entry->id, start_id) == 0) {
760 /* Start ID's commit is already on the queue; skip over it. */
761 err = got_commit_graph_iter_next(&id, graph);
762 if (err && err->code != GOT_ERR_ITER_NEED_MORE)
765 err = got_commit_graph_fetch_commits(&nfetched, graph, 1, repo);
771 struct got_commit_object *commit;
773 err = got_commit_graph_iter_next(&id, graph);
775 if (err->code != GOT_ERR_ITER_NEED_MORE)
777 if (nqueued >= minqueue) {
781 err = got_commit_graph_fetch_commits(&nfetched,
790 err = got_object_open_as_commit(&commit, repo, id);
795 struct got_object *obj;
796 struct got_object_qid *pid;
799 err = got_object_open_by_path(&obj, repo, id, path);
801 got_object_commit_close(commit);
802 if (err->code == GOT_ERR_NO_OBJ &&
803 (found_obj || !init)) {
804 /* History stops here. */
805 err = got_error(GOT_ERR_ITER_COMPLETED);
811 pid = SIMPLEQ_FIRST(&commit->parent_ids);
813 struct got_object *pobj;
814 err = got_object_open_by_path(&pobj, repo,
817 if (err->code != GOT_ERR_NO_OBJ) {
818 got_object_close(obj);
819 got_object_commit_close(commit);
825 struct got_object_id *id, *pid;
826 id = got_object_get_id(obj);
828 err = got_error_from_errno();
829 got_object_close(obj);
830 got_object_close(pobj);
833 pid = got_object_get_id(pobj);
835 err = got_error_from_errno();
837 got_object_close(obj);
838 got_object_close(pobj);
842 (got_object_id_cmp(id, pid) != 0);
843 got_object_close(pobj);
848 got_object_close(obj);
850 got_object_commit_close(commit);
855 entry = alloc_commit_queue_entry(commit, id);
857 err = got_error_from_errno();
860 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
868 static const struct got_error *
869 fetch_next_commit(struct commit_queue_entry **pentry,
870 struct commit_queue_entry *entry, struct commit_queue *commits,
871 struct got_commit_graph *graph, struct got_repository *repo,
874 const struct got_error *err = NULL;
878 err = queue_commits(graph, commits, entry->id, 1, 0, repo, path);
882 /* Next entry to display should now be available. */
883 *pentry = TAILQ_NEXT(entry, entry);
885 return got_error(GOT_ERR_NO_OBJ);
890 static const struct got_error *
891 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
893 const struct got_error *err = NULL;
894 struct got_reference *head_ref;
898 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
902 err = got_ref_resolve(head_id, repo, head_ref);
903 got_ref_close(head_ref);
912 static const struct got_error *
913 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
914 struct commit_queue_entry **selected, struct commit_queue_entry *first,
915 struct commit_queue *commits, int selected_idx, int limit,
916 struct got_commit_graph *graph, struct got_repository *repo,
919 const struct got_error *err = NULL;
920 struct commit_queue_entry *entry;
922 char *id_str, *header;
928 if (ncommits == selected_idx) {
932 entry = TAILQ_NEXT(entry, entry);
936 err = got_object_id_str(&id_str, (*selected)->id);
940 if (path && strcmp(path, "/") != 0) {
941 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
942 err = got_error_from_errno();
946 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
947 err = got_error_from_errno();
952 err = format_line(&wline, &width, header, view->ncols);
959 werase(view->window);
961 waddwstr(view->window, wline);
962 if (width < view->ncols)
963 waddch(view->window, '\n');
972 if (ncommits >= limit - 1)
974 if (ncommits == selected_idx)
975 wstandout(view->window);
976 err = draw_commit(view, entry->commit, entry->id);
977 if (ncommits == selected_idx)
978 wstandend(view->window);
983 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
984 err = queue_commits(graph, commits, entry->id, 1,
987 if (err->code != GOT_ERR_ITER_COMPLETED)
992 entry = TAILQ_NEXT(entry, entry);
1001 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1002 struct commit_queue *commits)
1004 struct commit_queue_entry *entry;
1007 entry = TAILQ_FIRST(&commits->head);
1008 if (*first_displayed_entry == entry)
1011 entry = *first_displayed_entry;
1012 while (entry && nscrolled < maxscroll) {
1013 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1015 *first_displayed_entry = entry;
1021 static const struct got_error *
1022 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1023 struct commit_queue_entry *last_displayed_entry,
1024 struct commit_queue *commits, struct got_commit_graph *graph,
1025 struct got_repository *repo, const char *path)
1027 const struct got_error *err = NULL;
1028 struct commit_queue_entry *pentry;
1032 pentry = TAILQ_NEXT(last_displayed_entry, entry);
1033 if (pentry == NULL) {
1034 err = fetch_next_commit(&pentry, last_displayed_entry,
1035 commits, graph, repo, path);
1036 if (err || pentry == NULL)
1039 last_displayed_entry = pentry;
1041 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1044 *first_displayed_entry = pentry;
1045 } while (++nscrolled < maxscroll);
1050 static const struct got_error *
1051 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1052 struct commit_queue_entry *entry, struct got_repository *repo)
1054 const struct got_error *err;
1055 struct got_object *obj1 = NULL, *obj2 = NULL;
1056 struct got_object_qid *parent_id;
1057 struct tog_view *diff_view;
1059 err = got_object_open(&obj2, repo, entry->id);
1063 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1065 err = got_object_open(&obj1, repo, parent_id->id);
1070 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1071 if (diff_view == NULL) {
1072 err = got_error_from_errno();
1076 err = open_diff_view(diff_view, obj1, obj2, repo);
1078 *new_view = diff_view;
1081 got_object_close(obj1);
1083 got_object_close(obj2);
1087 static const struct got_error *
1088 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1089 struct commit_queue_entry *entry, struct got_repository *repo)
1091 const struct got_error *err = NULL;
1092 struct got_tree_object *tree;
1093 struct tog_view *tree_view;
1095 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1099 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1100 if (tree_view == NULL)
1101 return got_error_from_errno();
1103 err = open_tree_view(tree_view, tree, entry->id, repo);
1105 got_object_tree_close(tree);
1107 *new_view = tree_view;
1111 static const struct got_error *
1112 set_child_log_view(struct tog_view *view, struct tog_view *child)
1114 struct tog_log_view_state *s = &view->state.log;
1115 struct tog_diff_view_state *ds;
1116 struct commit_queue_entry *commit, *child_entry = NULL;
1117 int selected_idx = 0;
1119 if (child->type != TOG_VIEW_DIFF)
1121 ds = &child->state.diff;
1123 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1124 if (got_object_id_cmp(commit->id, ds->id) == 0) {
1125 child_entry = commit;
1129 if (child_entry == NULL)
1132 commit = s->first_displayed_entry;
1134 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1135 s->selected_entry = child_entry;
1136 s->selected = selected_idx;
1139 if (commit == s->last_displayed_entry)
1142 commit = TAILQ_NEXT(commit, entry);
1145 return show_log_view(view);
1148 static const struct got_error *
1149 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1150 struct got_repository *repo, const char *path)
1152 const struct got_error *err = NULL;
1153 struct got_object_id *head_id = NULL;
1155 struct tog_log_view_state *s = &view->state.log;
1157 err = got_repo_map_path(&s->in_repo_path, repo, path);
1161 err = get_head_commit_id(&head_id, repo);
1165 /* The graph contains all commits. */
1166 err = got_commit_graph_open(&s->graph, head_id, 0, repo);
1169 /* The commit queue contains a subset of commits filtered by path. */
1170 TAILQ_INIT(&s->commits.head);
1171 s->commits.ncommits = 0;
1173 /* Populate commit graph with a sufficient number of commits. */
1174 err = got_commit_graph_fetch_commits_up_to(&nfetched, s->graph,
1180 * Open the initial batch of commits, sorted in commit graph order.
1181 * We keep all commits open throughout the lifetime of the log view
1182 * in order to avoid having to re-fetch commits from disk while
1183 * updating the display.
1185 err = queue_commits(s->graph, &s->commits, start_id, view->nlines, 1,
1186 repo, s->in_repo_path);
1188 if (err->code != GOT_ERR_ITER_COMPLETED)
1193 s->first_displayed_entry =
1194 TAILQ_FIRST(&s->commits.head);
1195 s->selected_entry = s->first_displayed_entry;
1198 view->show = show_log_view;
1199 view->input = input_log_view;
1200 view->close = close_log_view;
1201 view->set_child = set_child_log_view;
1207 static const struct got_error *
1208 close_log_view(struct tog_view *view)
1210 struct tog_log_view_state *s = &view->state.log;
1213 got_commit_graph_close(s->graph);
1214 free_commits(&s->commits);
1215 free(s->in_repo_path);
1219 static const struct got_error *
1220 update_diff_child_view(struct tog_view *parent,
1221 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1223 const struct got_error *err = NULL;
1224 struct tog_diff_view_state *ds;
1225 struct got_object *obj1 = NULL, *obj2 = NULL;
1226 struct got_object_qid *parent_id;
1227 struct tog_view *child_view = parent->child;
1229 if (child_view == NULL)
1231 if (child_view->type != TOG_VIEW_DIFF)
1233 ds = &child_view->state.diff;
1234 if (got_object_id_cmp(ds->id, selected_entry->id) == 0)
1237 err = got_object_open(&obj2, repo, selected_entry->id);
1241 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1243 err = got_object_open(&obj1, repo, parent_id->id);
1248 err = close_diff_view(child_view);
1252 err = open_diff_view(child_view, obj1, obj2, repo);
1257 got_object_close(obj1);
1259 got_object_close(obj2);
1263 static const struct got_error *
1264 show_log_view(struct tog_view *view)
1266 const struct got_error *err = NULL;
1267 struct tog_log_view_state *s = &view->state.log;
1269 err = draw_commits(view, &s->last_displayed_entry,
1270 &s->selected_entry, s->first_displayed_entry,
1271 &s->commits, s->selected, view->nlines, s->graph,
1272 s->repo, s->in_repo_path);
1276 return update_diff_child_view(view, s->selected_entry, s->repo);
1279 static const struct got_error *
1280 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1281 struct tog_view *view, int ch)
1283 const struct got_error *err = NULL;
1284 struct tog_log_view_state *s = &view->state.log;
1290 if (s->selected > 0)
1292 if (s->selected > 0)
1294 scroll_up(&s->first_displayed_entry, 1,
1298 if (TAILQ_FIRST(&s->commits.head) ==
1299 s->first_displayed_entry) {
1303 scroll_up(&s->first_displayed_entry,
1304 view->nlines, &s->commits);
1309 if (s->selected < MIN(view->nlines - 2,
1310 s->commits.ncommits - 1)) {
1314 err = scroll_down(&s->first_displayed_entry, 1,
1315 s->last_displayed_entry, &s->commits,
1316 s->graph, s->repo, s->in_repo_path);
1318 if (err->code != GOT_ERR_ITER_COMPLETED)
1324 struct commit_queue_entry *first;
1325 first = s->first_displayed_entry;
1326 err = scroll_down(&s->first_displayed_entry,
1327 view->nlines, s->last_displayed_entry,
1328 &s->commits, s->graph, s->repo,
1332 if (err->code != GOT_ERR_ITER_COMPLETED)
1334 if (first == s->first_displayed_entry &&
1335 s->selected < MIN(view->nlines - 2,
1336 s->commits.ncommits - 1)) {
1337 /* can't scroll further down */
1338 s->selected = MIN(view->nlines - 2,
1339 s->commits.ncommits - 1);
1345 if (s->selected > view->nlines - 2)
1346 s->selected = view->nlines - 2;
1347 if (s->selected > s->commits.ncommits - 1)
1348 s->selected = s->commits.ncommits - 1;
1352 err = show_commit(new_view, view, s->selected_entry,
1356 err = browse_commit(new_view, view, s->selected_entry,
1366 static const struct got_error *
1367 cmd_log(int argc, char *argv[])
1369 const struct got_error *error;
1370 struct got_repository *repo = NULL;
1371 struct got_object_id *start_id = NULL;
1372 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1373 char *start_commit = NULL;
1375 struct tog_view *view;
1378 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1382 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1385 start_commit = optarg;
1388 repo_path = realpath(optarg, NULL);
1389 if (repo_path == NULL)
1390 err(1, "-r option");
1404 path = strdup(argv[0]);
1408 return got_error_from_errno();
1410 cwd = getcwd(NULL, 0);
1412 error = got_error_from_errno();
1415 if (repo_path == NULL) {
1416 repo_path = strdup(cwd);
1417 if (repo_path == NULL) {
1418 error = got_error_from_errno();
1423 error = got_repo_open(&repo, repo_path);
1427 if (start_commit == NULL) {
1428 error = get_head_commit_id(&start_id, repo);
1432 struct got_object *obj;
1433 error = got_object_open_by_id_str(&obj, repo, start_commit);
1434 if (error == NULL) {
1435 start_id = got_object_get_id(obj);
1436 if (start_id == NULL)
1437 error = got_error_from_errno();
1444 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1446 error = got_error_from_errno();
1449 error = open_log_view(view, start_id, repo, path);
1452 error = view_loop(view);
1459 got_repo_close(repo);
1467 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1473 parse_next_line(FILE *f, size_t *len)
1478 const char delim[3] = { '\0', '\0', '\0'};
1480 line = fparseln(f, &linelen, &lineno, delim, 0);
1486 static const struct got_error *
1487 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1488 int *last_displayed_line, int *eof, int max_lines)
1490 const struct got_error *err;
1491 int nlines = 0, nprinted = 0;
1498 werase(view->window);
1501 while (nprinted < max_lines) {
1502 line = parse_next_line(f, &len);
1507 if (++nlines < *first_displayed_line) {
1512 err = format_line(&wline, &width, line, view->ncols);
1518 waddwstr(view->window, wline);
1519 if (width < view->ncols)
1520 waddch(view->window, '\n');
1521 if (++nprinted == 1)
1522 *first_displayed_line = nlines;
1527 *last_displayed_line = nlines;
1534 static const struct got_error *
1535 open_diff_view(struct tog_view *view, struct got_object *obj1,
1536 struct got_object *obj2, struct got_repository *repo)
1538 const struct got_error *err;
1541 if (obj1 != NULL && obj2 != NULL &&
1542 got_object_get_type(obj1) != got_object_get_type(obj2))
1543 return got_error(GOT_ERR_OBJ_TYPE);
1547 return got_error_from_errno();
1549 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1550 case GOT_OBJ_TYPE_BLOB:
1551 err = got_diff_objects_as_blobs(obj1, obj2, repo, f);
1553 case GOT_OBJ_TYPE_TREE:
1554 err = got_diff_objects_as_trees(obj1, obj2, repo, f);
1556 case GOT_OBJ_TYPE_COMMIT:
1557 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1560 return got_error(GOT_ERR_OBJ_TYPE);
1565 view->state.diff.id = got_object_get_id(obj2);
1566 if (view->state.diff.id == NULL)
1567 return got_error_from_errno();
1568 view->state.diff.f = f;
1569 view->state.diff.first_displayed_line = 1;
1570 view->state.diff.last_displayed_line = view->nlines;
1572 view->show = show_diff_view;
1573 view->input = input_diff_view;
1574 view->close = close_diff_view;
1579 static const struct got_error *
1580 close_diff_view(struct tog_view *view)
1582 const struct got_error *err = NULL;
1584 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1585 err = got_error_from_errno();
1586 free(view->state.diff.id);
1590 static const struct got_error *
1591 show_diff_view(struct tog_view *view)
1593 struct tog_diff_view_state *s = &view->state.diff;
1595 return draw_file(view, s->f, &s->first_displayed_line,
1596 &s->last_displayed_line, &s->eof, view->nlines);
1599 static const struct got_error *
1600 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1601 struct tog_view *view, int ch)
1603 const struct got_error *err = NULL;
1604 struct tog_diff_view_state *s = &view->state.diff;
1610 if (s->first_displayed_line > 1)
1611 s->first_displayed_line--;
1615 while (i++ < view->nlines - 1 &&
1616 s->first_displayed_line > 1)
1617 s->first_displayed_line--;
1622 s->first_displayed_line++;
1627 while (!s->eof && i++ < view->nlines - 1) {
1629 line = parse_next_line(s->f, NULL);
1630 s->first_displayed_line++;
1637 struct tog_log_view_state *ls;
1638 struct commit_queue_entry *entry;
1639 struct tog_view *diff_view;
1641 if (view->parent == NULL)
1643 if (view->parent->type != TOG_VIEW_LOG)
1645 ls = &view->parent->state.log;
1648 entry = TAILQ_PREV(ls->selected_entry,
1649 commit_queue_head, entry);
1651 entry = TAILQ_NEXT(ls->selected_entry, entry);
1652 if (entry == NULL) {
1653 err = fetch_next_commit(&entry,
1655 &ls->commits, ls->graph,
1656 ls->repo, ls->in_repo_path);
1663 err = show_commit(&diff_view, view->parent,
1667 *new_view = diff_view;
1678 static const struct got_error *
1679 cmd_diff(int argc, char *argv[])
1681 const struct got_error *error = NULL;
1682 struct got_repository *repo = NULL;
1683 struct got_object *obj1 = NULL, *obj2 = NULL;
1684 char *repo_path = NULL;
1685 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1687 struct tog_view *view;
1690 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
1694 while ((ch = getopt(argc, argv, "")) != -1) {
1706 usage_diff(); /* TODO show local worktree changes */
1707 } else if (argc == 2) {
1708 repo_path = getcwd(NULL, 0);
1709 if (repo_path == NULL)
1710 return got_error_from_errno();
1711 obj_id_str1 = argv[0];
1712 obj_id_str2 = argv[1];
1713 } else if (argc == 3) {
1714 repo_path = realpath(argv[0], NULL);
1715 if (repo_path == NULL)
1716 return got_error_from_errno();
1717 obj_id_str1 = argv[1];
1718 obj_id_str2 = argv[2];
1722 error = got_repo_open(&repo, repo_path);
1727 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1731 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1735 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1737 error = got_error_from_errno();
1740 error = open_diff_view(view, obj1, obj2, repo);
1743 error = view_loop(view);
1745 got_repo_close(repo);
1747 got_object_close(obj1);
1749 got_object_close(obj2);
1757 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1762 struct tog_blame_line {
1764 struct got_object_id *id;
1767 static const struct got_error *
1768 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1769 const char *path, struct tog_blame_line *lines, int nlines,
1770 int blame_complete, int selected_line, int *first_displayed_line,
1771 int *last_displayed_line, int *eof, int max_lines)
1773 const struct got_error *err;
1774 int lineno = 0, nprinted = 0;
1779 struct tog_blame_line *blame_line;
1780 struct got_object_id *prev_id = NULL;
1783 err = got_object_id_str(&id_str, id);
1788 werase(view->window);
1790 if (asprintf(&line, "commit: %s", id_str) == -1) {
1791 err = got_error_from_errno();
1796 err = format_line(&wline, &width, line, view->ncols);
1799 waddwstr(view->window, wline);
1802 if (width < view->ncols)
1803 waddch(view->window, '\n');
1805 if (asprintf(&line, "[%d/%d] %s%s",
1806 *first_displayed_line - 1 + selected_line, nlines,
1807 blame_complete ? "" : "annotating ", path) == -1) {
1809 return got_error_from_errno();
1812 err = format_line(&wline, &width, line, view->ncols);
1817 waddwstr(view->window, wline);
1820 if (width < view->ncols)
1821 waddch(view->window, '\n');
1824 while (nprinted < max_lines - 2) {
1825 line = parse_next_line(f, &len);
1830 if (++lineno < *first_displayed_line) {
1835 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1836 err = format_line(&wline, &width, line, wlimit);
1842 if (nprinted == selected_line - 1)
1843 wstandout(view->window);
1845 blame_line = &lines[lineno - 1];
1846 if (blame_line->annotated && prev_id &&
1847 got_object_id_cmp(prev_id, blame_line->id) == 0)
1848 waddstr(view->window, " ");
1849 else if (blame_line->annotated) {
1851 err = got_object_id_str(&id_str, blame_line->id);
1857 wprintw(view->window, "%.8s ", id_str);
1859 prev_id = blame_line->id;
1861 waddstr(view->window, "........ ");
1865 waddwstr(view->window, wline);
1866 while (width < wlimit) {
1867 waddch(view->window, ' ');
1870 if (nprinted == selected_line - 1)
1871 wstandend(view->window);
1872 if (++nprinted == 1)
1873 *first_displayed_line = lineno;
1878 *last_displayed_line = lineno;
1885 static const struct got_error *
1886 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1888 const struct got_error *err = NULL;
1889 struct tog_blame_cb_args *a = arg;
1890 struct tog_blame_line *line;
1892 if (nlines != a->nlines ||
1893 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1894 return got_error(GOT_ERR_RANGE);
1896 if (pthread_mutex_lock(a->mutex) != 0)
1897 return got_error_from_errno();
1899 if (*a->quit) { /* user has quit the blame view */
1900 err = got_error(GOT_ERR_ITER_COMPLETED);
1905 goto done; /* no change in this commit */
1907 line = &a->lines[lineno - 1];
1908 if (line->annotated)
1911 line->id = got_object_id_dup(id);
1912 if (line->id == NULL) {
1913 err = got_error_from_errno();
1916 line->annotated = 1;
1918 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1919 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1920 a->last_displayed_line, a->eof, a->view->nlines);
1922 if (pthread_mutex_unlock(a->mutex) != 0)
1923 return got_error_from_errno();
1928 blame_thread(void *arg)
1930 const struct got_error *err;
1931 struct tog_blame_thread_args *ta = arg;
1932 struct tog_blame_cb_args *a = ta->cb_args;
1934 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1935 blame_cb, ta->cb_args);
1937 if (pthread_mutex_lock(a->mutex) != 0)
1938 return (void *)got_error_from_errno();
1940 got_repo_close(ta->repo);
1944 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1945 a->lines, a->nlines, 1, *a->selected_line,
1946 a->first_displayed_line, a->last_displayed_line, a->eof,
1949 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1950 err = got_error_from_errno();
1955 static struct got_object_id *
1956 get_selected_commit_id(struct tog_blame_line *lines,
1957 int first_displayed_line, int selected_line)
1959 struct tog_blame_line *line;
1961 line = &lines[first_displayed_line - 1 + selected_line - 1];
1962 if (!line->annotated)
1968 static const struct got_error *
1969 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1970 struct tog_blame_line *lines, int first_displayed_line,
1971 int selected_line, struct got_repository *repo)
1973 const struct got_error *err = NULL;
1974 struct got_commit_object *commit = NULL;
1975 struct got_object_id *selected_id;
1976 struct got_object_qid *pid;
1981 selected_id = get_selected_commit_id(lines,
1982 first_displayed_line, selected_line);
1983 if (selected_id == NULL)
1986 err = got_object_open(obj, repo, selected_id);
1990 err = got_object_commit_open(&commit, repo, *obj);
1994 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1996 err = got_object_open(pobj, repo, pid->id);
2002 got_object_commit_close(commit);
2006 static const struct got_error *
2007 stop_blame(struct tog_blame *blame)
2009 const struct got_error *err = NULL;
2012 if (blame->thread) {
2013 if (pthread_join(blame->thread, (void **)&err) != 0)
2014 err = got_error_from_errno();
2015 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2017 blame->thread = NULL;
2019 if (blame->thread_args.repo) {
2020 got_repo_close(blame->thread_args.repo);
2021 blame->thread_args.repo = NULL;
2027 for (i = 0; i < blame->nlines; i++)
2028 free(blame->lines[i].id);
2030 blame->lines = NULL;
2031 free(blame->cb_args.commit_id);
2032 blame->cb_args.commit_id = NULL;
2037 static const struct got_error *
2038 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2039 struct tog_view *view, int *blame_complete,
2040 int *first_displayed_line, int *last_displayed_line,
2041 int *selected_line, int *done, int *eof, const char *path,
2042 struct got_object_id *commit_id,
2043 struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_blob_object *blob = NULL;
2047 struct got_repository *thread_repo = NULL;
2048 struct got_object *obj;
2050 err = got_object_open_by_path(&obj, repo, commit_id, path);
2053 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2054 err = got_error(GOT_ERR_OBJ_TYPE);
2058 err = got_object_blob_open(&blob, repo, obj, 8192);
2061 blame->f = got_opentemp();
2062 if (blame->f == NULL) {
2063 err = got_error_from_errno();
2066 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2071 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2072 if (blame->lines == NULL) {
2073 err = got_error_from_errno();
2077 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2081 blame->cb_args.view = view;
2082 blame->cb_args.lines = blame->lines;
2083 blame->cb_args.nlines = blame->nlines;
2084 blame->cb_args.mutex = mutex;
2085 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2086 if (blame->cb_args.commit_id == NULL) {
2087 err = got_error_from_errno();
2090 blame->cb_args.f = blame->f;
2091 blame->cb_args.path = path;
2092 blame->cb_args.first_displayed_line = first_displayed_line;
2093 blame->cb_args.selected_line = selected_line;
2094 blame->cb_args.last_displayed_line = last_displayed_line;
2095 blame->cb_args.quit = done;
2096 blame->cb_args.eof = eof;
2098 blame->thread_args.path = path;
2099 blame->thread_args.repo = thread_repo;
2100 blame->thread_args.cb_args = &blame->cb_args;
2101 blame->thread_args.complete = blame_complete;
2102 *blame_complete = 0;
2104 if (pthread_create(&blame->thread, NULL, blame_thread,
2105 &blame->thread_args) != 0) {
2106 err = got_error_from_errno();
2112 got_object_blob_close(blob);
2114 got_object_close(obj);
2120 static const struct got_error *
2121 open_blame_view(struct tog_view *view, char *path,
2122 struct got_object_id *commit_id, struct got_repository *repo)
2124 const struct got_error *err = NULL;
2125 struct tog_blame_view_state *s = &view->state.blame;
2127 SIMPLEQ_INIT(&s->blamed_commits);
2129 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2130 return got_error_from_errno();
2132 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2136 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2137 s->first_displayed_line = 1;
2138 s->last_displayed_line = view->nlines;
2139 s->selected_line = 1;
2140 s->blame_complete = 0;
2142 if (s->path == NULL)
2143 return got_error_from_errno();
2145 s->commit_id = commit_id;
2146 memset(&s->blame, 0, sizeof(s->blame));
2148 view->show = show_blame_view;
2149 view->input = input_blame_view;
2150 view->close = close_blame_view;
2152 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2153 &s->first_displayed_line, &s->last_displayed_line,
2154 &s->selected_line, &s->done, &s->eof, s->path,
2155 s->blamed_commit->id, s->repo);
2158 static const struct got_error *
2159 close_blame_view(struct tog_view *view)
2161 const struct got_error *err = NULL;
2162 struct tog_blame_view_state *s = &view->state.blame;
2164 if (s->blame.thread)
2165 err = stop_blame(&s->blame);
2167 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2168 struct got_object_qid *blamed_commit;
2169 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2170 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2171 got_object_qid_free(blamed_commit);
2179 static const struct got_error *
2180 show_blame_view(struct tog_view *view)
2182 const struct got_error *err = NULL;
2183 struct tog_blame_view_state *s = &view->state.blame;
2185 if (pthread_mutex_lock(&s->mutex) != 0)
2186 return got_error_from_errno();
2188 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2189 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2190 s->selected_line, &s->first_displayed_line,
2191 &s->last_displayed_line, &s->eof, view->nlines);
2193 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2194 err = got_error_from_errno();
2199 static const struct got_error *
2200 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 struct tog_view *view, int ch)
2203 const struct got_error *err = NULL, *thread_err = NULL;
2204 struct got_object *obj = NULL, *pobj = NULL;
2205 struct tog_view *diff_view;
2206 struct tog_blame_view_state *s = &view->state.blame;
2208 if (pthread_mutex_lock(&s->mutex) != 0) {
2209 err = got_error_from_errno();
2216 if (pthread_mutex_unlock(&s->mutex) != 0) {
2217 err = got_error_from_errno();
2220 return stop_blame(&s->blame);
2223 if (s->selected_line > 1)
2225 else if (s->selected_line == 1 &&
2226 s->first_displayed_line > 1)
2227 s->first_displayed_line--;
2230 if (s->first_displayed_line == 1) {
2231 s->selected_line = 1;
2234 if (s->first_displayed_line > view->nlines - 2)
2235 s->first_displayed_line -=
2238 s->first_displayed_line = 1;
2242 if (s->selected_line < view->nlines - 2 &&
2243 s->first_displayed_line +
2244 s->selected_line <= s->blame.nlines)
2246 else if (s->last_displayed_line <
2248 s->first_displayed_line++;
2252 struct got_object_id *id;
2253 id = get_selected_commit_id(s->blame.lines,
2254 s->first_displayed_line, s->selected_line);
2255 if (id == NULL || got_object_id_cmp(id,
2256 s->blamed_commit->id) == 0)
2258 err = open_selected_commit(&pobj, &obj,
2259 s->blame.lines, s->first_displayed_line,
2260 s->selected_line, s->repo);
2263 if (pobj == NULL && obj == NULL)
2265 if (ch == 'p' && pobj == NULL)
2268 if (pthread_mutex_unlock(&s->mutex) != 0) {
2269 err = got_error_from_errno();
2272 thread_err = stop_blame(&s->blame);
2274 if (pthread_mutex_lock(&s->mutex) != 0) {
2275 err = got_error_from_errno();
2280 id = got_object_get_id(ch == 'b' ? obj : pobj);
2281 got_object_close(obj);
2284 got_object_close(pobj);
2288 err = got_error_from_errno();
2291 err = got_object_qid_alloc(
2292 &s->blamed_commit, id);
2296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2297 s->blamed_commit, entry);
2298 err = run_blame(&s->blame, &s->mutex, view,
2300 &s->first_displayed_line,
2301 &s->last_displayed_line,
2302 &s->selected_line, &s->done, &s->eof,
2303 s->path, s->blamed_commit->id, s->repo);
2309 struct got_object_qid *first;
2310 first = SIMPLEQ_FIRST(&s->blamed_commits);
2311 if (!got_object_id_cmp(first->id, s->commit_id))
2314 if (pthread_mutex_unlock(&s->mutex) != 0) {
2315 err = got_error_from_errno();
2318 thread_err = stop_blame(&s->blame);
2320 if (pthread_mutex_lock(&s->mutex) != 0) {
2321 err = got_error_from_errno();
2326 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2327 got_object_qid_free(s->blamed_commit);
2329 SIMPLEQ_FIRST(&s->blamed_commits);
2330 err = run_blame(&s->blame, &s->mutex, view,
2332 &s->first_displayed_line,
2333 &s->last_displayed_line,
2334 &s->selected_line, &s->done, &s->eof, s->path,
2335 s->blamed_commit->id, s->repo);
2342 err = open_selected_commit(&pobj, &obj,
2343 s->blame.lines, s->first_displayed_line,
2344 s->selected_line, s->repo);
2347 if (pobj == NULL && obj == NULL)
2349 diff_view = view_open(0, 0, 0, 0, view,
2351 if (diff_view == NULL) {
2352 err = got_error_from_errno();
2355 err = open_diff_view(diff_view, pobj, obj,
2358 view_close(diff_view);
2361 *new_view = diff_view;
2363 got_object_close(pobj);
2366 got_object_close(obj);
2373 if (s->last_displayed_line >= s->blame.nlines &&
2374 s->selected_line < view->nlines - 2) {
2375 s->selected_line = MIN(s->blame.nlines,
2379 if (s->last_displayed_line + view->nlines - 2
2381 s->first_displayed_line +=
2384 s->first_displayed_line =
2389 if (s->selected_line > view->nlines - 2) {
2390 s->selected_line = MIN(s->blame.nlines,
2398 if (pthread_mutex_unlock(&s->mutex) != 0)
2399 err = got_error_from_errno();
2402 got_object_close(pobj);
2403 return thread_err ? thread_err : err;
2406 static const struct got_error *
2407 cmd_blame(int argc, char *argv[])
2409 const struct got_error *error;
2410 struct got_repository *repo = NULL;
2411 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2412 struct got_object_id *commit_id = NULL;
2413 char *commit_id_str = NULL;
2415 struct tog_view *view;
2418 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2422 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2425 commit_id_str = optarg;
2428 repo_path = realpath(optarg, NULL);
2429 if (repo_path == NULL)
2430 err(1, "-r option");
2446 cwd = getcwd(NULL, 0);
2448 error = got_error_from_errno();
2451 if (repo_path == NULL) {
2452 repo_path = strdup(cwd);
2453 if (repo_path == NULL) {
2454 error = got_error_from_errno();
2460 error = got_repo_open(&repo, repo_path);
2464 error = got_repo_map_path(&in_repo_path, repo, path);
2468 if (commit_id_str == NULL) {
2469 struct got_reference *head_ref;
2470 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2473 error = got_ref_resolve(&commit_id, repo, head_ref);
2474 got_ref_close(head_ref);
2476 struct got_object *obj;
2477 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2480 commit_id = got_object_get_id(obj);
2481 if (commit_id == NULL)
2482 error = got_error_from_errno();
2483 got_object_close(obj);
2488 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2490 error = got_error_from_errno();
2493 error = open_blame_view(view, in_repo_path, commit_id, repo);
2496 error = view_loop(view);
2502 got_repo_close(repo);
2506 static const struct got_error *
2507 draw_tree_entries(struct tog_view *view,
2508 struct got_tree_entry **first_displayed_entry,
2509 struct got_tree_entry **last_displayed_entry,
2510 struct got_tree_entry **selected_entry, int *ndisplayed,
2511 const char *label, int show_ids, const char *parent_path,
2512 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2514 const struct got_error *err = NULL;
2515 struct got_tree_entry *te;
2521 werase(view->window);
2526 err = format_line(&wline, &width, label, view->ncols);
2529 waddwstr(view->window, wline);
2532 if (width < view->ncols)
2533 waddch(view->window, '\n');
2536 err = format_line(&wline, &width, parent_path, view->ncols);
2539 waddwstr(view->window, wline);
2542 if (width < view->ncols)
2543 waddch(view->window, '\n');
2546 waddch(view->window, '\n');
2550 te = SIMPLEQ_FIRST(&entries->head);
2551 if (*first_displayed_entry == NULL) {
2552 if (selected == 0) {
2553 wstandout(view->window);
2554 *selected_entry = NULL;
2556 waddstr(view->window, " ..\n"); /* parent directory */
2558 wstandend(view->window);
2565 while (te != *first_displayed_entry)
2566 te = SIMPLEQ_NEXT(te, entry);
2570 char *line = NULL, *id_str = NULL;
2573 err = got_object_id_str(&id_str, te->id);
2575 return got_error_from_errno();
2577 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2578 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2580 return got_error_from_errno();
2583 err = format_line(&wline, &width, line, view->ncols);
2588 if (n == selected) {
2589 wstandout(view->window);
2590 *selected_entry = te;
2592 waddwstr(view->window, wline);
2593 if (width < view->ncols)
2594 waddch(view->window, '\n');
2596 wstandend(view->window);
2602 *last_displayed_entry = te;
2605 te = SIMPLEQ_NEXT(te, entry);
2613 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2614 const struct got_tree_entries *entries, int isroot)
2616 struct got_tree_entry *te, *prev;
2619 if (*first_displayed_entry == NULL)
2622 te = SIMPLEQ_FIRST(&entries->head);
2623 if (*first_displayed_entry == te) {
2625 *first_displayed_entry = NULL;
2629 /* XXX this is stupid... switch to TAILQ? */
2630 for (i = 0; i < maxscroll; i++) {
2631 while (te != *first_displayed_entry) {
2633 te = SIMPLEQ_NEXT(te, entry);
2635 *first_displayed_entry = prev;
2636 te = SIMPLEQ_FIRST(&entries->head);
2638 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2639 *first_displayed_entry = NULL;
2643 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2644 struct got_tree_entry *last_displayed_entry,
2645 const struct got_tree_entries *entries)
2647 struct got_tree_entry *next;
2650 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2653 if (*first_displayed_entry)
2654 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2656 next = SIMPLEQ_FIRST(&entries->head);
2658 *first_displayed_entry = next;
2659 if (++n >= maxscroll)
2661 next = SIMPLEQ_NEXT(next, entry);
2665 static const struct got_error *
2666 tree_entry_path(char **path, struct tog_parent_trees *parents,
2667 struct got_tree_entry *te)
2669 const struct got_error *err = NULL;
2670 struct tog_parent_tree *pt;
2671 size_t len = 2; /* for leading slash and NUL */
2673 TAILQ_FOREACH(pt, parents, entry)
2674 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2676 len += strlen(te->name);
2678 *path = calloc(1, len);
2680 return got_error_from_errno();
2683 pt = TAILQ_LAST(parents, tog_parent_trees);
2685 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2686 err = got_error(GOT_ERR_NO_SPACE);
2689 if (strlcat(*path, "/", len) >= len) {
2690 err = got_error(GOT_ERR_NO_SPACE);
2693 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2696 if (strlcat(*path, te->name, len) >= len) {
2697 err = got_error(GOT_ERR_NO_SPACE);
2709 static const struct got_error *
2710 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2711 struct got_tree_entry *te, struct tog_parent_trees *parents,
2712 struct got_object_id *commit_id, struct got_repository *repo)
2714 const struct got_error *err = NULL;
2716 struct tog_view *blame_view;
2718 err = tree_entry_path(&path, parents, te);
2722 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2723 if (blame_view == NULL)
2724 return got_error_from_errno();
2726 err = open_blame_view(blame_view, path, commit_id, repo);
2728 view_close(blame_view);
2731 *new_view = blame_view;
2735 static const struct got_error *
2736 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2737 struct got_tree_entry *te, struct tog_parent_trees *parents,
2738 struct got_object_id *commit_id, struct got_repository *repo)
2740 struct tog_view *log_view;
2741 const struct got_error *err = NULL;
2744 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2745 if (log_view == NULL)
2746 return got_error_from_errno();
2748 err = tree_entry_path(&path, parents, te);
2752 err = open_log_view(log_view, commit_id, repo, path);
2754 view_close(log_view);
2756 *new_view = log_view;
2761 static const struct got_error *
2762 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2763 struct got_object_id *commit_id, struct got_repository *repo)
2765 const struct got_error *err = NULL;
2766 char *commit_id_str = NULL;
2767 struct tog_tree_view_state *s = &view->state.tree;
2769 TAILQ_INIT(&s->parents);
2771 err = got_object_id_str(&commit_id_str, commit_id);
2775 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2776 err = got_error_from_errno();
2780 s->root = s->tree = root;
2781 s->entries = got_object_tree_get_entries(root);
2782 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2783 s->commit_id = commit_id;
2786 view->show = show_tree_view;
2787 view->input = input_tree_view;
2788 view->close = close_tree_view;
2790 free(commit_id_str);
2792 free(s->tree_label);
2796 static const struct got_error *
2797 close_tree_view(struct tog_view *view)
2799 struct tog_tree_view_state *s = &view->state.tree;
2801 free(s->tree_label);
2802 while (!TAILQ_EMPTY(&s->parents)) {
2803 struct tog_parent_tree *parent;
2804 parent = TAILQ_FIRST(&s->parents);
2805 TAILQ_REMOVE(&s->parents, parent, entry);
2809 if (s->tree != s->root)
2810 got_object_tree_close(s->tree);
2811 got_object_tree_close(s->root);
2816 static const struct got_error *
2817 show_tree_view(struct tog_view *view)
2819 const struct got_error *err = NULL;
2820 struct tog_tree_view_state *s = &view->state.tree;
2823 err = tree_entry_path(&parent_path, &s->parents, NULL);
2827 err = draw_tree_entries(view, &s->first_displayed_entry,
2828 &s->last_displayed_entry, &s->selected_entry,
2829 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2830 s->entries, s->selected, view->nlines, s->tree == s->root);
2835 static const struct got_error *
2836 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2837 struct tog_view *view, int ch)
2839 const struct got_error *err = NULL;
2840 struct tog_tree_view_state *s = &view->state.tree;
2844 s->show_ids = !s->show_ids;
2847 if (s->selected_entry) {
2848 err = log_tree_entry(new_view, view,
2849 s->selected_entry, &s->parents,
2850 s->commit_id, s->repo);
2855 if (s->selected > 0)
2857 if (s->selected > 0)
2859 tree_scroll_up(&s->first_displayed_entry, 1,
2860 s->entries, s->tree == s->root);
2863 if (SIMPLEQ_FIRST(&s->entries->head) ==
2864 s->first_displayed_entry) {
2865 if (s->tree != s->root)
2866 s->first_displayed_entry = NULL;
2870 tree_scroll_up(&s->first_displayed_entry,
2871 view->nlines, s->entries,
2872 s->tree == s->root);
2876 if (s->selected < s->ndisplayed - 1) {
2880 tree_scroll_down(&s->first_displayed_entry, 1,
2881 s->last_displayed_entry, s->entries);
2884 tree_scroll_down(&s->first_displayed_entry,
2885 view->nlines, s->last_displayed_entry,
2887 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2890 /* can't scroll any further; move cursor down */
2891 if (s->selected < s->ndisplayed - 1)
2892 s->selected = s->ndisplayed - 1;
2896 if (s->selected_entry == NULL) {
2897 struct tog_parent_tree *parent;
2899 /* user selected '..' */
2900 if (s->tree == s->root)
2902 parent = TAILQ_FIRST(&s->parents);
2903 TAILQ_REMOVE(&s->parents, parent,
2905 got_object_tree_close(s->tree);
2906 s->tree = parent->tree;
2908 got_object_tree_get_entries(s->tree);
2909 s->first_displayed_entry =
2910 parent->first_displayed_entry;
2912 parent->selected_entry;
2913 s->selected = parent->selected;
2915 } else if (S_ISDIR(s->selected_entry->mode)) {
2916 struct tog_parent_tree *parent;
2917 struct got_tree_object *child;
2918 err = got_object_open_as_tree(&child,
2919 s->repo, s->selected_entry->id);
2922 parent = calloc(1, sizeof(*parent));
2923 if (parent == NULL) {
2924 err = got_error_from_errno();
2927 parent->tree = s->tree;
2928 parent->first_displayed_entry =
2929 s->first_displayed_entry;
2930 parent->selected_entry = s->selected_entry;
2931 parent->selected = s->selected;
2932 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2935 got_object_tree_get_entries(s->tree);
2937 s->first_displayed_entry = NULL;
2938 } else if (S_ISREG(s->selected_entry->mode)) {
2939 err = blame_tree_entry(new_view, view,
2940 s->selected_entry, &s->parents,
2941 s->commit_id, s->repo);
2947 if (s->selected > view->nlines)
2948 s->selected = s->ndisplayed - 1;
2961 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2966 static const struct got_error *
2967 cmd_tree(int argc, char *argv[])
2969 const struct got_error *error;
2970 struct got_repository *repo = NULL;
2971 char *repo_path = NULL;
2972 struct got_object_id *commit_id = NULL;
2973 char *commit_id_arg = NULL;
2974 struct got_commit_object *commit = NULL;
2975 struct got_tree_object *tree = NULL;
2977 struct tog_view *view;
2980 if (pledge("stdio rpath wpath cpath flock proc tty", NULL) == -1)
2984 while ((ch = getopt(argc, argv, "c:")) != -1) {
2987 commit_id_arg = optarg;
2999 repo_path = getcwd(NULL, 0);
3000 if (repo_path == NULL)
3001 return got_error_from_errno();
3002 } else if (argc == 1) {
3003 repo_path = realpath(argv[0], NULL);
3004 if (repo_path == NULL)
3005 return got_error_from_errno();
3009 error = got_repo_open(&repo, repo_path);
3014 if (commit_id_arg == NULL) {
3015 error = get_head_commit_id(&commit_id, repo);
3019 struct got_object *obj;
3020 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3021 if (error == NULL) {
3022 commit_id = got_object_get_id(obj);
3023 if (commit_id == NULL)
3024 error = got_error_from_errno();
3030 error = got_object_open_as_commit(&commit, repo, commit_id);
3034 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3038 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3040 error = got_error_from_errno();
3043 error = open_tree_view(view, tree, commit_id, repo);
3046 error = view_loop(view);
3050 got_object_commit_close(commit);
3052 got_object_tree_close(tree);
3054 got_repo_close(repo);
3064 intrflush(stdscr, FALSE);
3065 keypad(stdscr, TRUE);
3074 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3075 "Available commands:\n", getprogname());
3076 for (i = 0; i < nitems(tog_commands); i++) {
3077 struct tog_cmd *cmd = &tog_commands[i];
3078 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3084 make_argv(const char *arg0, const char *arg1)
3087 int argc = (arg1 == NULL ? 1 : 2);
3089 argv = calloc(argc, sizeof(char *));
3092 argv[0] = strdup(arg0);
3093 if (argv[0] == NULL)
3096 argv[1] = strdup(arg1);
3097 if (argv[1] == NULL)
3105 main(int argc, char *argv[])
3107 const struct got_error *error = NULL;
3108 struct tog_cmd *cmd = NULL;
3110 char **cmd_argv = NULL;
3112 setlocale(LC_ALL, "");
3114 while ((ch = getopt(argc, argv, "h")) != -1) {
3133 /* Build an argument vector which runs a default command. */
3134 cmd = &tog_commands[0];
3135 cmd_argv = make_argv(cmd->name, NULL);
3140 /* Did the user specific a command? */
3141 for (i = 0; i < nitems(tog_commands); i++) {
3142 if (strncmp(tog_commands[i].name, argv[0],
3143 strlen(argv[0])) == 0) {
3144 cmd = &tog_commands[i];
3146 tog_commands[i].cmd_usage();
3151 /* Did the user specify a repository? */
3152 char *repo_path = realpath(argv[0], NULL);
3154 struct got_repository *repo;
3155 error = got_repo_open(&repo, repo_path);
3157 got_repo_close(repo);
3159 error = got_error_from_errno();
3162 fprintf(stderr, "%s: '%s' is not a "
3163 "known command\n", getprogname(),
3167 fprintf(stderr, "%s: '%s' is neither a known "
3168 "command nor a path to a repository\n",
3169 getprogname(), argv[0]);
3173 cmd = &tog_commands[0];
3174 cmd_argv = make_argv(cmd->name, repo_path);
3182 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3189 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);