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);
510 struct tog_view *v, *t;
512 /* Only allow one view per type. */
513 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
514 if (v->type != new_view->type)
516 TAILQ_REMOVE(&views, v, entry);
521 TAILQ_INSERT_TAIL(&views, new_view, entry);
522 if (new_view->parent) {
523 err = view_set_child(new_view->parent, new_view);
526 new_view->parent->focussed = 0;
533 while (!TAILQ_EMPTY(&views)) {
534 view = TAILQ_FIRST(&views);
535 TAILQ_REMOVE(&views, view, entry);
546 "usage: %s log [-c commit] [-r repository-path] [path]\n",
551 /* Create newly allocated wide-character string equivalent to a byte string. */
552 static const struct got_error *
553 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
556 const struct got_error *err = NULL;
559 *wlen = mbstowcs(NULL, s, 0);
560 if (*wlen == (size_t)-1) {
563 return got_error_from_errno();
565 /* byte string invalid in current encoding; try to "fix" it */
566 err = got_mbsavis(&vis, &vislen, s);
569 *wlen = mbstowcs(NULL, vis, 0);
570 if (*wlen == (size_t)-1) {
571 err = got_error_from_errno(); /* give up */
576 *ws = calloc(*wlen + 1, sizeof(*ws));
578 err = got_error_from_errno();
582 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
583 err = got_error_from_errno();
594 /* Format a line for display, ensuring that it won't overflow a width limit. */
595 static const struct got_error *
596 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
598 const struct got_error *err = NULL;
600 wchar_t *wline = NULL;
607 err = mbs2ws(&wline, &wlen, line);
612 while (i < wlen && cols < wlimit) {
613 int width = wcwidth(wline[i]);
620 if (cols + width <= wlimit)
625 if (wline[i] == L'\t')
626 cols += TABSIZE - ((cols + 1) % TABSIZE);
630 err = got_error_from_errno();
645 static const struct got_error *
646 draw_commit(struct tog_view *view, struct got_commit_object *commit,
647 struct got_object_id *id)
649 const struct got_error *err = NULL;
650 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
651 char *logmsg0 = NULL, *logmsg = NULL;
652 char *author0 = NULL, *author = NULL;
653 wchar_t *wlogmsg = NULL, *wauthor = NULL;
654 int author_width, logmsg_width;
655 char *newline, *smallerthan;
658 static const size_t date_display_cols = 9;
659 static const size_t author_display_cols = 16;
660 const int avail = view->ncols;
662 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
663 &commit->tm_committer) >= sizeof(datebuf))
664 return got_error(GOT_ERR_NO_SPACE);
666 if (avail < date_display_cols)
667 limit = MIN(sizeof(datebuf) - 1, avail);
669 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
670 waddnstr(view->window, datebuf, limit);
675 author0 = strdup(commit->author);
676 if (author0 == NULL) {
677 err = got_error_from_errno();
681 smallerthan = strchr(author, '<');
685 char *at = strchr(author, '@');
690 err = format_line(&wauthor, &author_width, author, limit);
693 waddwstr(view->window, wauthor);
695 while (col <= avail && author_width < author_display_cols + 1) {
696 waddch(view->window, ' ');
703 logmsg0 = strdup(commit->logmsg);
704 if (logmsg0 == NULL) {
705 err = got_error_from_errno();
709 while (*logmsg == '\n')
711 newline = strchr(logmsg, '\n');
715 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
718 waddwstr(view->window, wlogmsg);
720 while (col <= avail) {
721 waddch(view->window, ' ');
733 static struct commit_queue_entry *
734 alloc_commit_queue_entry(struct got_commit_object *commit,
735 struct got_object_id *id)
737 struct commit_queue_entry *entry;
739 entry = calloc(1, sizeof(*entry));
744 entry->commit = commit;
749 pop_commit(struct commit_queue *commits)
751 struct commit_queue_entry *entry;
753 entry = TAILQ_FIRST(&commits->head);
754 TAILQ_REMOVE(&commits->head, entry, entry);
755 got_object_commit_close(entry->commit);
757 /* Don't free entry->id! It is owned by the commit graph. */
762 free_commits(struct commit_queue *commits)
764 while (!TAILQ_EMPTY(&commits->head))
768 static const struct got_error *
769 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
770 struct got_object_id *start_id, int minqueue,
771 struct got_repository *repo, const char *path)
773 const struct got_error *err = NULL;
777 err = got_commit_graph_iter_start(graph, start_id, repo);
782 while (nqueued < minqueue) {
783 struct got_object_id *id;
784 struct got_commit_object *commit;
785 struct commit_queue_entry *entry;
787 err = got_commit_graph_iter_next(&id, graph);
789 if (err->code == GOT_ERR_ITER_COMPLETED) {
793 if (err->code != GOT_ERR_ITER_NEED_MORE)
795 err = got_commit_graph_fetch_commits(graph,
805 err = got_object_open_as_commit(&commit, repo, id);
808 entry = alloc_commit_queue_entry(commit, id);
810 err = got_error_from_errno();
814 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
822 static const struct got_error *
823 fetch_next_commit(struct commit_queue_entry **pentry,
824 struct commit_queue_entry *entry, struct commit_queue *commits,
825 struct got_commit_graph *graph, struct got_repository *repo,
828 const struct got_error *err = NULL;
832 err = queue_commits(graph, commits, NULL, 1, repo, path);
836 /* Next entry to display should now be available. */
837 *pentry = TAILQ_NEXT(entry, entry);
841 static const struct got_error *
842 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
844 const struct got_error *err = NULL;
845 struct got_reference *head_ref;
849 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
853 err = got_ref_resolve(head_id, repo, head_ref);
854 got_ref_close(head_ref);
863 static const struct got_error *
864 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
865 struct commit_queue_entry **selected, struct commit_queue_entry *first,
866 struct commit_queue *commits, int selected_idx, int limit,
867 struct got_commit_graph *graph, struct got_repository *repo,
870 const struct got_error *err = NULL;
871 struct commit_queue_entry *entry;
873 char *id_str, *header;
879 if (ncommits == selected_idx) {
883 entry = TAILQ_NEXT(entry, entry);
887 err = got_object_id_str(&id_str, (*selected)->id);
891 if (path && strcmp(path, "/") != 0) {
892 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
893 err = got_error_from_errno();
897 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
898 err = got_error_from_errno();
903 err = format_line(&wline, &width, header, view->ncols);
910 werase(view->window);
912 if (view_needs_focus_indication(view))
913 wstandout(view->window);
914 waddwstr(view->window, wline);
915 if (view_needs_focus_indication(view))
916 wstandend(view->window);
917 if (width < view->ncols)
918 waddch(view->window, '\n');
927 if (ncommits >= limit - 1)
929 if (ncommits == selected_idx)
930 wstandout(view->window);
931 err = draw_commit(view, entry->commit, entry->id);
932 if (ncommits == selected_idx)
933 wstandend(view->window);
938 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
939 err = queue_commits(graph, commits, NULL, 1,
942 if (err->code != GOT_ERR_ITER_COMPLETED)
947 entry = TAILQ_NEXT(entry, entry);
956 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
957 struct commit_queue *commits)
959 struct commit_queue_entry *entry;
962 entry = TAILQ_FIRST(&commits->head);
963 if (*first_displayed_entry == entry)
966 entry = *first_displayed_entry;
967 while (entry && nscrolled < maxscroll) {
968 entry = TAILQ_PREV(entry, commit_queue_head, entry);
970 *first_displayed_entry = entry;
976 static const struct got_error *
977 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
978 struct commit_queue_entry **last_displayed_entry,
979 struct commit_queue *commits, struct got_commit_graph *graph,
980 struct got_repository *repo, const char *path)
982 const struct got_error *err = NULL;
983 struct commit_queue_entry *pentry;
987 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
988 if (pentry == NULL) {
989 err = fetch_next_commit(&pentry, *last_displayed_entry,
990 commits, graph, repo, path);
991 if (err || pentry == NULL)
994 *last_displayed_entry = pentry;
996 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
999 *first_displayed_entry = pentry;
1000 } while (++nscrolled < maxscroll);
1005 static const struct got_error *
1006 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1007 struct commit_queue_entry *entry, struct got_repository *repo)
1009 const struct got_error *err;
1010 struct got_object *obj1 = NULL, *obj2 = NULL;
1011 struct got_object_qid *parent_id;
1012 struct tog_view *diff_view;
1014 err = got_object_open(&obj2, repo, entry->id);
1018 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1020 err = got_object_open(&obj1, repo, parent_id->id);
1025 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1026 if (diff_view == NULL) {
1027 err = got_error_from_errno();
1031 err = open_diff_view(diff_view, obj1, obj2, repo);
1033 *new_view = diff_view;
1036 got_object_close(obj1);
1038 got_object_close(obj2);
1042 static const struct got_error *
1043 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1044 struct commit_queue_entry *entry, struct got_repository *repo)
1046 const struct got_error *err = NULL;
1047 struct got_tree_object *tree;
1048 struct tog_view *tree_view;
1050 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1054 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1055 if (tree_view == NULL)
1056 return got_error_from_errno();
1058 err = open_tree_view(tree_view, tree, entry->id, repo);
1060 got_object_tree_close(tree);
1062 *new_view = tree_view;
1066 static const struct got_error *
1067 set_child_log_view(struct tog_view *view, struct tog_view *child)
1069 struct tog_log_view_state *s = &view->state.log;
1070 struct tog_diff_view_state *ds;
1071 struct commit_queue_entry *commit, *child_entry = NULL;
1072 int selected_idx = 0;
1074 if (child->type != TOG_VIEW_DIFF)
1076 ds = &child->state.diff;
1078 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1079 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1080 child_entry = commit;
1084 if (child_entry == NULL)
1087 commit = s->first_displayed_entry;
1089 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1090 s->selected_entry = child_entry;
1091 s->selected = selected_idx;
1094 if (commit == s->last_displayed_entry)
1097 commit = TAILQ_NEXT(commit, entry);
1100 return show_log_view(view);
1103 static const struct got_error *
1104 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1105 struct got_repository *repo, const char *path)
1107 const struct got_error *err = NULL;
1108 struct tog_log_view_state *s = &view->state.log;
1110 err = got_repo_map_path(&s->in_repo_path, repo, path);
1114 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1118 /* The commit queue only contains commits being displayed. */
1119 TAILQ_INIT(&s->commits.head);
1120 s->commits.ncommits = 0;
1123 * Open the initial batch of commits, sorted in commit graph order.
1124 * We keep all commits open throughout the lifetime of the log view
1125 * in order to avoid having to re-fetch commits from disk while
1126 * updating the display.
1128 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1129 repo, s->in_repo_path);
1131 if (err->code != GOT_ERR_ITER_COMPLETED)
1136 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1137 s->selected_entry = s->first_displayed_entry;
1140 view->show = show_log_view;
1141 view->input = input_log_view;
1142 view->close = close_log_view;
1143 view->set_child = set_child_log_view;
1148 static const struct got_error *
1149 close_log_view(struct tog_view *view)
1151 struct tog_log_view_state *s = &view->state.log;
1154 got_commit_graph_close(s->graph);
1155 free_commits(&s->commits);
1156 free(s->in_repo_path);
1160 static const struct got_error *
1161 update_diff_child_view(struct tog_view *parent,
1162 struct commit_queue_entry *selected_entry, struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 struct tog_diff_view_state *ds;
1166 struct got_object *obj1 = NULL, *obj2 = NULL;
1167 struct got_object_qid *parent_id;
1168 struct tog_view *child_view = parent->child;
1170 if (child_view == NULL)
1172 if (child_view->type != TOG_VIEW_DIFF)
1174 ds = &child_view->state.diff;
1175 if (got_object_id_cmp(ds->id2, selected_entry->id) == 0)
1178 err = got_object_open(&obj2, repo, selected_entry->id);
1182 parent_id = SIMPLEQ_FIRST(&selected_entry->commit->parent_ids);
1184 err = got_object_open(&obj1, repo, parent_id->id);
1189 err = close_diff_view(child_view);
1193 err = open_diff_view(child_view, obj1, obj2, repo);
1198 got_object_close(obj1);
1200 got_object_close(obj2);
1204 static const struct got_error *
1205 show_log_view(struct tog_view *view)
1207 const struct got_error *err = NULL;
1208 struct tog_log_view_state *s = &view->state.log;
1210 err = draw_commits(view, &s->last_displayed_entry,
1211 &s->selected_entry, s->first_displayed_entry,
1212 &s->commits, s->selected, view->nlines, s->graph,
1213 s->repo, s->in_repo_path);
1217 return update_diff_child_view(view, s->selected_entry, s->repo);
1220 static const struct got_error *
1221 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1222 struct tog_view *view, int ch)
1224 const struct got_error *err = NULL;
1225 struct tog_log_view_state *s = &view->state.log;
1231 if (s->selected > 0)
1233 if (s->selected > 0)
1235 scroll_up(&s->first_displayed_entry, 1,
1239 if (TAILQ_FIRST(&s->commits.head) ==
1240 s->first_displayed_entry) {
1244 scroll_up(&s->first_displayed_entry,
1245 view->nlines, &s->commits);
1250 if (s->selected < MIN(view->nlines - 2,
1251 s->commits.ncommits - 1)) {
1255 err = scroll_down(&s->first_displayed_entry, 1,
1256 &s->last_displayed_entry, &s->commits,
1257 s->graph, s->repo, s->in_repo_path);
1259 if (err->code != GOT_ERR_ITER_COMPLETED)
1265 struct commit_queue_entry *first;
1266 first = s->first_displayed_entry;
1267 err = scroll_down(&s->first_displayed_entry,
1268 view->nlines, &s->last_displayed_entry,
1269 &s->commits, s->graph, s->repo,
1271 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1273 if (first == s->first_displayed_entry &&
1274 s->selected < MIN(view->nlines - 2,
1275 s->commits.ncommits - 1)) {
1276 /* can't scroll further down */
1277 s->selected = MIN(view->nlines - 2,
1278 s->commits.ncommits - 1);
1284 if (s->selected > view->nlines - 2)
1285 s->selected = view->nlines - 2;
1286 if (s->selected > s->commits.ncommits - 1)
1287 s->selected = s->commits.ncommits - 1;
1291 err = show_commit(new_view, view, s->selected_entry,
1295 err = browse_commit(new_view, view, s->selected_entry,
1305 static const struct got_error *
1306 cmd_log(int argc, char *argv[])
1308 const struct got_error *error;
1309 struct got_repository *repo = NULL;
1310 struct got_object_id *start_id = NULL;
1311 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1312 char *start_commit = NULL;
1314 struct tog_view *view;
1317 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1322 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1325 start_commit = optarg;
1328 repo_path = realpath(optarg, NULL);
1329 if (repo_path == NULL)
1330 err(1, "-r option");
1344 path = strdup(argv[0]);
1348 return got_error_from_errno();
1350 cwd = getcwd(NULL, 0);
1352 error = got_error_from_errno();
1355 if (repo_path == NULL) {
1356 repo_path = strdup(cwd);
1357 if (repo_path == NULL) {
1358 error = got_error_from_errno();
1363 error = got_repo_open(&repo, repo_path);
1367 if (start_commit == NULL) {
1368 error = get_head_commit_id(&start_id, repo);
1372 struct got_object *obj;
1373 error = got_object_open_by_id_str(&obj, repo, start_commit);
1374 if (error == NULL) {
1375 start_id = got_object_id_dup(got_object_get_id(obj));
1376 if (start_id == NULL)
1377 error = got_error_from_errno();
1384 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1386 error = got_error_from_errno();
1389 error = open_log_view(view, start_id, repo, path);
1392 error = view_loop(view);
1399 got_repo_close(repo);
1407 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1413 parse_next_line(FILE *f, size_t *len)
1418 const char delim[3] = { '\0', '\0', '\0'};
1420 line = fparseln(f, &linelen, &lineno, delim, 0);
1426 static const struct got_error *
1427 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1428 int *last_displayed_line, int *eof, int max_lines,
1431 const struct got_error *err;
1432 int nlines = 0, nprinted = 0;
1439 werase(view->window);
1442 err = format_line(&wline, &width, header, view->ncols);
1447 if (view_needs_focus_indication(view))
1448 wstandout(view->window);
1449 waddwstr(view->window, wline);
1450 if (view_needs_focus_indication(view))
1451 wstandend(view->window);
1452 if (width < view->ncols)
1453 waddch(view->window, '\n');
1461 while (nprinted < max_lines) {
1462 line = parse_next_line(f, &len);
1467 if (++nlines < *first_displayed_line) {
1472 err = format_line(&wline, &width, line, view->ncols);
1477 waddwstr(view->window, wline);
1478 if (width < view->ncols)
1479 waddch(view->window, '\n');
1480 if (++nprinted == 1)
1481 *first_displayed_line = nlines;
1486 *last_displayed_line = nlines;
1493 static const struct got_error *
1494 open_diff_view(struct tog_view *view, struct got_object *obj1,
1495 struct got_object *obj2, struct got_repository *repo)
1497 const struct got_error *err;
1500 if (obj1 != NULL && obj2 != NULL &&
1501 got_object_get_type(obj1) != got_object_get_type(obj2))
1502 return got_error(GOT_ERR_OBJ_TYPE);
1506 return got_error_from_errno();
1508 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1509 case GOT_OBJ_TYPE_BLOB:
1510 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1513 case GOT_OBJ_TYPE_TREE:
1514 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1516 case GOT_OBJ_TYPE_COMMIT:
1517 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1520 return got_error(GOT_ERR_OBJ_TYPE);
1525 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1526 view->state.diff.id2 = got_object_get_id(obj2);
1527 view->state.diff.f = f;
1528 view->state.diff.first_displayed_line = 1;
1529 view->state.diff.last_displayed_line = view->nlines;
1531 view->show = show_diff_view;
1532 view->input = input_diff_view;
1533 view->close = close_diff_view;
1538 static const struct got_error *
1539 close_diff_view(struct tog_view *view)
1541 const struct got_error *err = NULL;
1543 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1544 err = got_error_from_errno();
1548 static const struct got_error *
1549 show_diff_view(struct tog_view *view)
1551 const struct got_error *err;
1552 struct tog_diff_view_state *s = &view->state.diff;
1553 char *id_str1 = NULL, *id_str2, *header;
1556 err = got_object_id_str(&id_str1, s->id1);
1560 err = got_object_id_str(&id_str2, s->id2);
1564 if (asprintf(&header, "diff: %s %s",
1565 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1566 err = got_error_from_errno();
1574 return draw_file(view, s->f, &s->first_displayed_line,
1575 &s->last_displayed_line, &s->eof, view->nlines,
1579 static const struct got_error *
1580 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1581 struct tog_view *view, int ch)
1583 const struct got_error *err = NULL;
1584 struct tog_diff_view_state *s = &view->state.diff;
1590 if (s->first_displayed_line > 1)
1591 s->first_displayed_line--;
1595 while (i++ < view->nlines - 1 &&
1596 s->first_displayed_line > 1)
1597 s->first_displayed_line--;
1602 s->first_displayed_line++;
1607 while (!s->eof && i++ < view->nlines - 1) {
1609 line = parse_next_line(s->f, NULL);
1610 s->first_displayed_line++;
1617 struct tog_log_view_state *ls;
1618 struct commit_queue_entry *entry;
1619 struct tog_view *diff_view;
1621 if (view->parent == NULL)
1623 if (view->parent->type != TOG_VIEW_LOG)
1625 ls = &view->parent->state.log;
1628 entry = TAILQ_PREV(ls->selected_entry,
1629 commit_queue_head, entry);
1631 entry = TAILQ_NEXT(ls->selected_entry, entry);
1632 if (entry == NULL) {
1633 err = fetch_next_commit(&entry,
1635 &ls->commits, ls->graph,
1636 ls->repo, ls->in_repo_path);
1643 err = show_commit(&diff_view, view->parent,
1647 *new_view = diff_view;
1658 static const struct got_error *
1659 cmd_diff(int argc, char *argv[])
1661 const struct got_error *error = NULL;
1662 struct got_repository *repo = NULL;
1663 struct got_object *obj1 = NULL, *obj2 = NULL;
1664 char *repo_path = NULL;
1665 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1667 struct tog_view *view;
1670 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1675 while ((ch = getopt(argc, argv, "")) != -1) {
1687 usage_diff(); /* TODO show local worktree changes */
1688 } else if (argc == 2) {
1689 repo_path = getcwd(NULL, 0);
1690 if (repo_path == NULL)
1691 return got_error_from_errno();
1692 obj_id_str1 = argv[0];
1693 obj_id_str2 = argv[1];
1694 } else if (argc == 3) {
1695 repo_path = realpath(argv[0], NULL);
1696 if (repo_path == NULL)
1697 return got_error_from_errno();
1698 obj_id_str1 = argv[1];
1699 obj_id_str2 = argv[2];
1703 error = got_repo_open(&repo, repo_path);
1708 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1712 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1716 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1718 error = got_error_from_errno();
1721 error = open_diff_view(view, obj1, obj2, repo);
1724 error = view_loop(view);
1726 got_repo_close(repo);
1728 got_object_close(obj1);
1730 got_object_close(obj2);
1738 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1743 struct tog_blame_line {
1745 struct got_object_id *id;
1748 static const struct got_error *
1749 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1750 const char *path, struct tog_blame_line *lines, int nlines,
1751 int blame_complete, int selected_line, int *first_displayed_line,
1752 int *last_displayed_line, int *eof, int max_lines)
1754 const struct got_error *err;
1755 int lineno = 0, nprinted = 0;
1760 struct tog_blame_line *blame_line;
1761 struct got_object_id *prev_id = NULL;
1764 err = got_object_id_str(&id_str, id);
1769 werase(view->window);
1771 if (asprintf(&line, "commit: %s", id_str) == -1) {
1772 err = got_error_from_errno();
1777 err = format_line(&wline, &width, line, view->ncols);
1780 if (view_needs_focus_indication(view))
1781 wstandout(view->window);
1782 waddwstr(view->window, wline);
1783 if (view_needs_focus_indication(view))
1784 wstandend(view->window);
1787 if (width < view->ncols)
1788 waddch(view->window, '\n');
1790 if (asprintf(&line, "[%d/%d] %s%s",
1791 *first_displayed_line - 1 + selected_line, nlines,
1792 blame_complete ? "" : "annotating ", path) == -1) {
1794 return got_error_from_errno();
1797 err = format_line(&wline, &width, line, view->ncols);
1802 waddwstr(view->window, wline);
1805 if (width < view->ncols)
1806 waddch(view->window, '\n');
1809 while (nprinted < max_lines - 2) {
1810 line = parse_next_line(f, &len);
1815 if (++lineno < *first_displayed_line) {
1820 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1821 err = format_line(&wline, &width, line, wlimit);
1827 if (nprinted == selected_line - 1)
1828 wstandout(view->window);
1830 blame_line = &lines[lineno - 1];
1831 if (blame_line->annotated && prev_id &&
1832 got_object_id_cmp(prev_id, blame_line->id) == 0)
1833 waddstr(view->window, " ");
1834 else if (blame_line->annotated) {
1836 err = got_object_id_str(&id_str, blame_line->id);
1842 wprintw(view->window, "%.8s ", id_str);
1844 prev_id = blame_line->id;
1846 waddstr(view->window, "........ ");
1850 waddwstr(view->window, wline);
1851 while (width < wlimit) {
1852 waddch(view->window, ' ');
1855 if (nprinted == selected_line - 1)
1856 wstandend(view->window);
1857 if (++nprinted == 1)
1858 *first_displayed_line = lineno;
1863 *last_displayed_line = lineno;
1870 static const struct got_error *
1871 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1873 const struct got_error *err = NULL;
1874 struct tog_blame_cb_args *a = arg;
1875 struct tog_blame_line *line;
1877 if (nlines != a->nlines ||
1878 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1879 return got_error(GOT_ERR_RANGE);
1881 if (pthread_mutex_lock(a->mutex) != 0)
1882 return got_error_from_errno();
1884 if (*a->quit) { /* user has quit the blame view */
1885 err = got_error(GOT_ERR_ITER_COMPLETED);
1890 goto done; /* no change in this commit */
1892 line = &a->lines[lineno - 1];
1893 if (line->annotated)
1896 line->id = got_object_id_dup(id);
1897 if (line->id == NULL) {
1898 err = got_error_from_errno();
1901 line->annotated = 1;
1903 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1904 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1905 a->last_displayed_line, a->eof, a->view->nlines);
1907 if (pthread_mutex_unlock(a->mutex) != 0)
1908 return got_error_from_errno();
1913 blame_thread(void *arg)
1915 const struct got_error *err;
1916 struct tog_blame_thread_args *ta = arg;
1917 struct tog_blame_cb_args *a = ta->cb_args;
1919 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1920 blame_cb, ta->cb_args);
1922 if (pthread_mutex_lock(a->mutex) != 0)
1923 return (void *)got_error_from_errno();
1925 got_repo_close(ta->repo);
1929 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1930 a->lines, a->nlines, 1, *a->selected_line,
1931 a->first_displayed_line, a->last_displayed_line, a->eof,
1934 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1935 err = got_error_from_errno();
1940 static struct got_object_id *
1941 get_selected_commit_id(struct tog_blame_line *lines,
1942 int first_displayed_line, int selected_line)
1944 struct tog_blame_line *line;
1946 line = &lines[first_displayed_line - 1 + selected_line - 1];
1947 if (!line->annotated)
1953 static const struct got_error *
1954 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1955 struct tog_blame_line *lines, int first_displayed_line,
1956 int selected_line, struct got_repository *repo)
1958 const struct got_error *err = NULL;
1959 struct got_commit_object *commit = NULL;
1960 struct got_object_id *selected_id;
1961 struct got_object_qid *pid;
1966 selected_id = get_selected_commit_id(lines,
1967 first_displayed_line, selected_line);
1968 if (selected_id == NULL)
1971 err = got_object_open(obj, repo, selected_id);
1975 err = got_object_commit_open(&commit, repo, *obj);
1979 pid = SIMPLEQ_FIRST(&commit->parent_ids);
1981 err = got_object_open(pobj, repo, pid->id);
1987 got_object_commit_close(commit);
1991 static const struct got_error *
1992 stop_blame(struct tog_blame *blame)
1994 const struct got_error *err = NULL;
1997 if (blame->thread) {
1998 if (pthread_join(blame->thread, (void **)&err) != 0)
1999 err = got_error_from_errno();
2000 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2002 blame->thread = NULL;
2004 if (blame->thread_args.repo) {
2005 got_repo_close(blame->thread_args.repo);
2006 blame->thread_args.repo = NULL;
2012 for (i = 0; i < blame->nlines; i++)
2013 free(blame->lines[i].id);
2015 blame->lines = NULL;
2016 free(blame->cb_args.commit_id);
2017 blame->cb_args.commit_id = NULL;
2022 static const struct got_error *
2023 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2024 struct tog_view *view, int *blame_complete,
2025 int *first_displayed_line, int *last_displayed_line,
2026 int *selected_line, int *done, int *eof, const char *path,
2027 struct got_object_id *commit_id,
2028 struct got_repository *repo)
2030 const struct got_error *err = NULL;
2031 struct got_blob_object *blob = NULL;
2032 struct got_repository *thread_repo = NULL;
2033 struct got_object_id *obj_id = NULL;
2034 struct got_object *obj = NULL;
2036 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2040 err = got_object_open(&obj, repo, obj_id);
2044 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2045 err = got_error(GOT_ERR_OBJ_TYPE);
2049 err = got_object_blob_open(&blob, repo, obj, 8192);
2052 blame->f = got_opentemp();
2053 if (blame->f == NULL) {
2054 err = got_error_from_errno();
2057 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2062 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2063 if (blame->lines == NULL) {
2064 err = got_error_from_errno();
2068 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2072 blame->cb_args.view = view;
2073 blame->cb_args.lines = blame->lines;
2074 blame->cb_args.nlines = blame->nlines;
2075 blame->cb_args.mutex = mutex;
2076 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2077 if (blame->cb_args.commit_id == NULL) {
2078 err = got_error_from_errno();
2081 blame->cb_args.f = blame->f;
2082 blame->cb_args.path = path;
2083 blame->cb_args.first_displayed_line = first_displayed_line;
2084 blame->cb_args.selected_line = selected_line;
2085 blame->cb_args.last_displayed_line = last_displayed_line;
2086 blame->cb_args.quit = done;
2087 blame->cb_args.eof = eof;
2089 blame->thread_args.path = path;
2090 blame->thread_args.repo = thread_repo;
2091 blame->thread_args.cb_args = &blame->cb_args;
2092 blame->thread_args.complete = blame_complete;
2093 *blame_complete = 0;
2095 if (pthread_create(&blame->thread, NULL, blame_thread,
2096 &blame->thread_args) != 0) {
2097 err = got_error_from_errno();
2103 got_object_blob_close(blob);
2106 got_object_close(obj);
2112 static const struct got_error *
2113 open_blame_view(struct tog_view *view, char *path,
2114 struct got_object_id *commit_id, struct got_repository *repo)
2116 const struct got_error *err = NULL;
2117 struct tog_blame_view_state *s = &view->state.blame;
2119 SIMPLEQ_INIT(&s->blamed_commits);
2121 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2122 return got_error_from_errno();
2124 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2128 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2129 s->first_displayed_line = 1;
2130 s->last_displayed_line = view->nlines;
2131 s->selected_line = 1;
2132 s->blame_complete = 0;
2134 if (s->path == NULL)
2135 return got_error_from_errno();
2137 s->commit_id = commit_id;
2138 memset(&s->blame, 0, sizeof(s->blame));
2140 view->show = show_blame_view;
2141 view->input = input_blame_view;
2142 view->close = close_blame_view;
2144 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2145 &s->first_displayed_line, &s->last_displayed_line,
2146 &s->selected_line, &s->done, &s->eof, s->path,
2147 s->blamed_commit->id, s->repo);
2150 static const struct got_error *
2151 close_blame_view(struct tog_view *view)
2153 const struct got_error *err = NULL;
2154 struct tog_blame_view_state *s = &view->state.blame;
2156 if (s->blame.thread)
2157 err = stop_blame(&s->blame);
2159 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2160 struct got_object_qid *blamed_commit;
2161 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2162 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2163 got_object_qid_free(blamed_commit);
2171 static const struct got_error *
2172 show_blame_view(struct tog_view *view)
2174 const struct got_error *err = NULL;
2175 struct tog_blame_view_state *s = &view->state.blame;
2177 if (pthread_mutex_lock(&s->mutex) != 0)
2178 return got_error_from_errno();
2180 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2181 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2182 s->selected_line, &s->first_displayed_line,
2183 &s->last_displayed_line, &s->eof, view->nlines);
2185 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2186 err = got_error_from_errno();
2191 static const struct got_error *
2192 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2193 struct tog_view *view, int ch)
2195 const struct got_error *err = NULL, *thread_err = NULL;
2196 struct got_object *obj = NULL, *pobj = NULL;
2197 struct tog_view *diff_view;
2198 struct tog_blame_view_state *s = &view->state.blame;
2200 if (pthread_mutex_lock(&s->mutex) != 0) {
2201 err = got_error_from_errno();
2208 if (pthread_mutex_unlock(&s->mutex) != 0) {
2209 err = got_error_from_errno();
2212 return stop_blame(&s->blame);
2215 if (s->selected_line > 1)
2217 else if (s->selected_line == 1 &&
2218 s->first_displayed_line > 1)
2219 s->first_displayed_line--;
2222 if (s->first_displayed_line == 1) {
2223 s->selected_line = 1;
2226 if (s->first_displayed_line > view->nlines - 2)
2227 s->first_displayed_line -=
2230 s->first_displayed_line = 1;
2234 if (s->selected_line < view->nlines - 2 &&
2235 s->first_displayed_line +
2236 s->selected_line <= s->blame.nlines)
2238 else if (s->last_displayed_line <
2240 s->first_displayed_line++;
2244 struct got_object_id *id;
2245 id = get_selected_commit_id(s->blame.lines,
2246 s->first_displayed_line, s->selected_line);
2247 if (id == NULL || got_object_id_cmp(id,
2248 s->blamed_commit->id) == 0)
2250 err = open_selected_commit(&pobj, &obj,
2251 s->blame.lines, s->first_displayed_line,
2252 s->selected_line, s->repo);
2255 if (pobj == NULL && obj == NULL)
2257 if (ch == 'p' && pobj == NULL)
2260 if (pthread_mutex_unlock(&s->mutex) != 0) {
2261 err = got_error_from_errno();
2264 thread_err = stop_blame(&s->blame);
2266 if (pthread_mutex_lock(&s->mutex) != 0) {
2267 err = got_error_from_errno();
2272 id = got_object_get_id(ch == 'b' ? obj : pobj);
2273 got_object_close(obj);
2276 got_object_close(pobj);
2279 err = got_object_qid_alloc(&s->blamed_commit, id);
2282 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2283 s->blamed_commit, entry);
2284 err = run_blame(&s->blame, &s->mutex, view,
2286 &s->first_displayed_line,
2287 &s->last_displayed_line,
2288 &s->selected_line, &s->done, &s->eof,
2289 s->path, s->blamed_commit->id, s->repo);
2295 struct got_object_qid *first;
2296 first = SIMPLEQ_FIRST(&s->blamed_commits);
2297 if (!got_object_id_cmp(first->id, s->commit_id))
2300 if (pthread_mutex_unlock(&s->mutex) != 0) {
2301 err = got_error_from_errno();
2304 thread_err = stop_blame(&s->blame);
2306 if (pthread_mutex_lock(&s->mutex) != 0) {
2307 err = got_error_from_errno();
2312 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2313 got_object_qid_free(s->blamed_commit);
2315 SIMPLEQ_FIRST(&s->blamed_commits);
2316 err = run_blame(&s->blame, &s->mutex, view,
2318 &s->first_displayed_line,
2319 &s->last_displayed_line,
2320 &s->selected_line, &s->done, &s->eof, s->path,
2321 s->blamed_commit->id, s->repo);
2328 err = open_selected_commit(&pobj, &obj,
2329 s->blame.lines, s->first_displayed_line,
2330 s->selected_line, s->repo);
2333 if (pobj == NULL && obj == NULL)
2335 diff_view = view_open(0, 0, 0, 0, view,
2337 if (diff_view == NULL) {
2338 err = got_error_from_errno();
2341 err = open_diff_view(diff_view, pobj, obj,
2344 view_close(diff_view);
2347 *new_view = diff_view;
2349 got_object_close(pobj);
2352 got_object_close(obj);
2359 if (s->last_displayed_line >= s->blame.nlines &&
2360 s->selected_line < view->nlines - 2) {
2361 s->selected_line = MIN(s->blame.nlines,
2365 if (s->last_displayed_line + view->nlines - 2
2367 s->first_displayed_line +=
2370 s->first_displayed_line =
2375 if (s->selected_line > view->nlines - 2) {
2376 s->selected_line = MIN(s->blame.nlines,
2384 if (pthread_mutex_unlock(&s->mutex) != 0)
2385 err = got_error_from_errno();
2388 got_object_close(pobj);
2389 return thread_err ? thread_err : err;
2392 static const struct got_error *
2393 cmd_blame(int argc, char *argv[])
2395 const struct got_error *error;
2396 struct got_repository *repo = NULL;
2397 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2398 struct got_object_id *commit_id = NULL;
2399 char *commit_id_str = NULL;
2401 struct tog_view *view;
2404 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2409 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2412 commit_id_str = optarg;
2415 repo_path = realpath(optarg, NULL);
2416 if (repo_path == NULL)
2417 err(1, "-r option");
2433 cwd = getcwd(NULL, 0);
2435 error = got_error_from_errno();
2438 if (repo_path == NULL) {
2439 repo_path = strdup(cwd);
2440 if (repo_path == NULL) {
2441 error = got_error_from_errno();
2447 error = got_repo_open(&repo, repo_path);
2451 error = got_repo_map_path(&in_repo_path, repo, path);
2455 if (commit_id_str == NULL) {
2456 struct got_reference *head_ref;
2457 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2460 error = got_ref_resolve(&commit_id, repo, head_ref);
2461 got_ref_close(head_ref);
2463 struct got_object *obj;
2464 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2467 commit_id = got_object_id_dup(got_object_get_id(obj));
2468 if (commit_id == NULL)
2469 error = got_error_from_errno();
2470 got_object_close(obj);
2475 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2477 error = got_error_from_errno();
2480 error = open_blame_view(view, in_repo_path, commit_id, repo);
2483 error = view_loop(view);
2489 got_repo_close(repo);
2493 static const struct got_error *
2494 draw_tree_entries(struct tog_view *view,
2495 struct got_tree_entry **first_displayed_entry,
2496 struct got_tree_entry **last_displayed_entry,
2497 struct got_tree_entry **selected_entry, int *ndisplayed,
2498 const char *label, int show_ids, const char *parent_path,
2499 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2501 const struct got_error *err = NULL;
2502 struct got_tree_entry *te;
2508 werase(view->window);
2513 err = format_line(&wline, &width, label, view->ncols);
2516 if (view_needs_focus_indication(view))
2517 wstandout(view->window);
2518 waddwstr(view->window, wline);
2519 if (view_needs_focus_indication(view))
2520 wstandend(view->window);
2523 if (width < view->ncols)
2524 waddch(view->window, '\n');
2527 err = format_line(&wline, &width, parent_path, view->ncols);
2530 waddwstr(view->window, wline);
2533 if (width < view->ncols)
2534 waddch(view->window, '\n');
2537 waddch(view->window, '\n');
2541 te = SIMPLEQ_FIRST(&entries->head);
2542 if (*first_displayed_entry == NULL) {
2543 if (selected == 0) {
2544 wstandout(view->window);
2545 *selected_entry = NULL;
2547 waddstr(view->window, " ..\n"); /* parent directory */
2549 wstandend(view->window);
2556 while (te != *first_displayed_entry)
2557 te = SIMPLEQ_NEXT(te, entry);
2561 char *line = NULL, *id_str = NULL;
2564 err = got_object_id_str(&id_str, te->id);
2566 return got_error_from_errno();
2568 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2569 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2571 return got_error_from_errno();
2574 err = format_line(&wline, &width, line, view->ncols);
2579 if (n == selected) {
2580 wstandout(view->window);
2581 *selected_entry = te;
2583 waddwstr(view->window, wline);
2584 if (width < view->ncols)
2585 waddch(view->window, '\n');
2587 wstandend(view->window);
2593 *last_displayed_entry = te;
2596 te = SIMPLEQ_NEXT(te, entry);
2604 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2605 const struct got_tree_entries *entries, int isroot)
2607 struct got_tree_entry *te, *prev;
2610 if (*first_displayed_entry == NULL)
2613 te = SIMPLEQ_FIRST(&entries->head);
2614 if (*first_displayed_entry == te) {
2616 *first_displayed_entry = NULL;
2620 /* XXX this is stupid... switch to TAILQ? */
2621 for (i = 0; i < maxscroll; i++) {
2622 while (te != *first_displayed_entry) {
2624 te = SIMPLEQ_NEXT(te, entry);
2626 *first_displayed_entry = prev;
2627 te = SIMPLEQ_FIRST(&entries->head);
2629 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2630 *first_displayed_entry = NULL;
2634 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2635 struct got_tree_entry *last_displayed_entry,
2636 const struct got_tree_entries *entries)
2638 struct got_tree_entry *next;
2641 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2644 if (*first_displayed_entry)
2645 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2647 next = SIMPLEQ_FIRST(&entries->head);
2649 *first_displayed_entry = next;
2650 if (++n >= maxscroll)
2652 next = SIMPLEQ_NEXT(next, entry);
2656 static const struct got_error *
2657 tree_entry_path(char **path, struct tog_parent_trees *parents,
2658 struct got_tree_entry *te)
2660 const struct got_error *err = NULL;
2661 struct tog_parent_tree *pt;
2662 size_t len = 2; /* for leading slash and NUL */
2664 TAILQ_FOREACH(pt, parents, entry)
2665 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2667 len += strlen(te->name);
2669 *path = calloc(1, len);
2671 return got_error_from_errno();
2674 pt = TAILQ_LAST(parents, tog_parent_trees);
2676 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2677 err = got_error(GOT_ERR_NO_SPACE);
2680 if (strlcat(*path, "/", len) >= len) {
2681 err = got_error(GOT_ERR_NO_SPACE);
2684 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2687 if (strlcat(*path, te->name, len) >= len) {
2688 err = got_error(GOT_ERR_NO_SPACE);
2700 static const struct got_error *
2701 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2702 struct got_tree_entry *te, struct tog_parent_trees *parents,
2703 struct got_object_id *commit_id, struct got_repository *repo)
2705 const struct got_error *err = NULL;
2707 struct tog_view *blame_view;
2709 err = tree_entry_path(&path, parents, te);
2713 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2714 if (blame_view == NULL)
2715 return got_error_from_errno();
2717 err = open_blame_view(blame_view, path, commit_id, repo);
2719 view_close(blame_view);
2722 *new_view = blame_view;
2726 static const struct got_error *
2727 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2728 struct got_tree_entry *te, struct tog_parent_trees *parents,
2729 struct got_object_id *commit_id, struct got_repository *repo)
2731 struct tog_view *log_view;
2732 const struct got_error *err = NULL;
2735 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2736 if (log_view == NULL)
2737 return got_error_from_errno();
2739 err = tree_entry_path(&path, parents, te);
2743 err = open_log_view(log_view, commit_id, repo, path);
2745 view_close(log_view);
2747 *new_view = log_view;
2752 static const struct got_error *
2753 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2754 struct got_object_id *commit_id, struct got_repository *repo)
2756 const struct got_error *err = NULL;
2757 char *commit_id_str = NULL;
2758 struct tog_tree_view_state *s = &view->state.tree;
2760 TAILQ_INIT(&s->parents);
2762 err = got_object_id_str(&commit_id_str, commit_id);
2766 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2767 err = got_error_from_errno();
2771 s->root = s->tree = root;
2772 s->entries = got_object_tree_get_entries(root);
2773 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2774 s->commit_id = commit_id;
2777 view->show = show_tree_view;
2778 view->input = input_tree_view;
2779 view->close = close_tree_view;
2781 free(commit_id_str);
2783 free(s->tree_label);
2787 static const struct got_error *
2788 close_tree_view(struct tog_view *view)
2790 struct tog_tree_view_state *s = &view->state.tree;
2792 free(s->tree_label);
2793 while (!TAILQ_EMPTY(&s->parents)) {
2794 struct tog_parent_tree *parent;
2795 parent = TAILQ_FIRST(&s->parents);
2796 TAILQ_REMOVE(&s->parents, parent, entry);
2800 if (s->tree != s->root)
2801 got_object_tree_close(s->tree);
2802 got_object_tree_close(s->root);
2807 static const struct got_error *
2808 show_tree_view(struct tog_view *view)
2810 const struct got_error *err = NULL;
2811 struct tog_tree_view_state *s = &view->state.tree;
2814 err = tree_entry_path(&parent_path, &s->parents, NULL);
2818 err = draw_tree_entries(view, &s->first_displayed_entry,
2819 &s->last_displayed_entry, &s->selected_entry,
2820 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2821 s->entries, s->selected, view->nlines, s->tree == s->root);
2826 static const struct got_error *
2827 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2828 struct tog_view *view, int ch)
2830 const struct got_error *err = NULL;
2831 struct tog_tree_view_state *s = &view->state.tree;
2835 s->show_ids = !s->show_ids;
2838 if (s->selected_entry) {
2839 err = log_tree_entry(new_view, view,
2840 s->selected_entry, &s->parents,
2841 s->commit_id, s->repo);
2846 if (s->selected > 0)
2848 if (s->selected > 0)
2850 tree_scroll_up(&s->first_displayed_entry, 1,
2851 s->entries, s->tree == s->root);
2854 if (SIMPLEQ_FIRST(&s->entries->head) ==
2855 s->first_displayed_entry) {
2856 if (s->tree != s->root)
2857 s->first_displayed_entry = NULL;
2861 tree_scroll_up(&s->first_displayed_entry,
2862 view->nlines, s->entries,
2863 s->tree == s->root);
2867 if (s->selected < s->ndisplayed - 1) {
2871 tree_scroll_down(&s->first_displayed_entry, 1,
2872 s->last_displayed_entry, s->entries);
2875 tree_scroll_down(&s->first_displayed_entry,
2876 view->nlines, s->last_displayed_entry,
2878 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2881 /* can't scroll any further; move cursor down */
2882 if (s->selected < s->ndisplayed - 1)
2883 s->selected = s->ndisplayed - 1;
2887 if (s->selected_entry == NULL) {
2888 struct tog_parent_tree *parent;
2890 /* user selected '..' */
2891 if (s->tree == s->root)
2893 parent = TAILQ_FIRST(&s->parents);
2894 TAILQ_REMOVE(&s->parents, parent,
2896 got_object_tree_close(s->tree);
2897 s->tree = parent->tree;
2899 got_object_tree_get_entries(s->tree);
2900 s->first_displayed_entry =
2901 parent->first_displayed_entry;
2903 parent->selected_entry;
2904 s->selected = parent->selected;
2906 } else if (S_ISDIR(s->selected_entry->mode)) {
2907 struct tog_parent_tree *parent;
2908 struct got_tree_object *child;
2909 err = got_object_open_as_tree(&child,
2910 s->repo, s->selected_entry->id);
2913 parent = calloc(1, sizeof(*parent));
2914 if (parent == NULL) {
2915 err = got_error_from_errno();
2918 parent->tree = s->tree;
2919 parent->first_displayed_entry =
2920 s->first_displayed_entry;
2921 parent->selected_entry = s->selected_entry;
2922 parent->selected = s->selected;
2923 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2926 got_object_tree_get_entries(s->tree);
2928 s->first_displayed_entry = NULL;
2929 } else if (S_ISREG(s->selected_entry->mode)) {
2930 err = blame_tree_entry(new_view, view,
2931 s->selected_entry, &s->parents,
2932 s->commit_id, s->repo);
2938 if (s->selected > view->nlines)
2939 s->selected = s->ndisplayed - 1;
2952 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2957 static const struct got_error *
2958 cmd_tree(int argc, char *argv[])
2960 const struct got_error *error;
2961 struct got_repository *repo = NULL;
2962 char *repo_path = NULL;
2963 struct got_object_id *commit_id = NULL;
2964 char *commit_id_arg = NULL;
2965 struct got_commit_object *commit = NULL;
2966 struct got_tree_object *tree = NULL;
2968 struct tog_view *view;
2971 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2976 while ((ch = getopt(argc, argv, "c:")) != -1) {
2979 commit_id_arg = optarg;
2991 repo_path = getcwd(NULL, 0);
2992 if (repo_path == NULL)
2993 return got_error_from_errno();
2994 } else if (argc == 1) {
2995 repo_path = realpath(argv[0], NULL);
2996 if (repo_path == NULL)
2997 return got_error_from_errno();
3001 error = got_repo_open(&repo, repo_path);
3006 if (commit_id_arg == NULL) {
3007 error = get_head_commit_id(&commit_id, repo);
3011 struct got_object *obj;
3012 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3013 if (error == NULL) {
3014 commit_id = got_object_id_dup(got_object_get_id(obj));
3015 if (commit_id == NULL)
3016 error = got_error_from_errno();
3022 error = got_object_open_as_commit(&commit, repo, commit_id);
3026 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3030 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3032 error = got_error_from_errno();
3035 error = open_tree_view(view, tree, commit_id, repo);
3038 error = view_loop(view);
3042 got_object_commit_close(commit);
3044 got_object_tree_close(tree);
3046 got_repo_close(repo);
3056 intrflush(stdscr, FALSE);
3057 keypad(stdscr, TRUE);
3066 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3067 "Available commands:\n", getprogname());
3068 for (i = 0; i < nitems(tog_commands); i++) {
3069 struct tog_cmd *cmd = &tog_commands[i];
3070 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3076 make_argv(const char *arg0, const char *arg1)
3079 int argc = (arg1 == NULL ? 1 : 2);
3081 argv = calloc(argc, sizeof(char *));
3084 argv[0] = strdup(arg0);
3085 if (argv[0] == NULL)
3088 argv[1] = strdup(arg1);
3089 if (argv[1] == NULL)
3097 main(int argc, char *argv[])
3099 const struct got_error *error = NULL;
3100 struct tog_cmd *cmd = NULL;
3102 char **cmd_argv = NULL;
3104 setlocale(LC_ALL, "");
3106 while ((ch = getopt(argc, argv, "h")) != -1) {
3125 /* Build an argument vector which runs a default command. */
3126 cmd = &tog_commands[0];
3127 cmd_argv = make_argv(cmd->name, NULL);
3132 /* Did the user specific a command? */
3133 for (i = 0; i < nitems(tog_commands); i++) {
3134 if (strncmp(tog_commands[i].name, argv[0],
3135 strlen(argv[0])) == 0) {
3136 cmd = &tog_commands[i];
3138 tog_commands[i].cmd_usage();
3143 /* Did the user specify a repository? */
3144 char *repo_path = realpath(argv[0], NULL);
3146 struct got_repository *repo;
3147 error = got_repo_open(&repo, repo_path);
3149 got_repo_close(repo);
3151 error = got_error_from_errno();
3154 fprintf(stderr, "%s: '%s' is not a "
3155 "known command\n", getprogname(),
3159 fprintf(stderr, "%s: '%s' is neither a known "
3160 "command nor a path to a repository\n",
3161 getprogname(), argv[0]);
3165 cmd = &tog_commands[0];
3166 cmd_argv = make_argv(cmd->name, repo_path);
3174 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3181 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);