2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
47 #include "got_blame.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 const struct got_error *(*cmd_main)(int, char *[]);
60 void (*cmd_usage)(void);
64 __dead static void usage(void);
65 __dead static void usage_log(void);
66 __dead static void usage_diff(void);
67 __dead static void usage_blame(void);
68 __dead static void usage_tree(void);
70 static const struct got_error* cmd_log(int, char *[]);
71 static const struct got_error* cmd_diff(int, char *[]);
72 static const struct got_error* cmd_blame(int, char *[]);
73 static const struct got_error* cmd_tree(int, char *[]);
75 static struct tog_cmd tog_commands[] = {
76 { "log", cmd_log, usage_log,
77 "show repository history" },
78 { "diff", cmd_diff, usage_diff,
79 "compare files and directories" },
80 { "blame", cmd_blame, usage_blame,
81 "show line-by-line file history" },
82 { "tree", cmd_tree, usage_tree,
83 "browse trees in repository" },
93 struct tog_diff_view_state {
94 struct got_object_id *id1, *id2;
96 int first_displayed_line;
97 int last_displayed_line;
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
106 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
107 struct commit_queue {
109 struct commit_queue_head head;
112 struct tog_log_view_state {
113 struct got_commit_graph *graph;
114 struct commit_queue commits;
115 struct commit_queue_entry *first_displayed_entry;
116 struct commit_queue_entry *last_displayed_entry;
117 struct commit_queue_entry *selected_entry;
120 struct got_repository *repo;
121 struct got_object_id *start_id;
124 struct tog_blame_cb_args {
125 pthread_mutex_t *mutex;
126 struct tog_blame_line *lines; /* one per line */
129 struct tog_view *view;
130 struct got_object_id *commit_id;
133 int *first_displayed_line;
134 int *last_displayed_line;
140 struct tog_blame_thread_args {
142 struct got_repository *repo;
143 struct tog_blame_cb_args *cb_args;
150 struct tog_blame_line *lines;
153 struct tog_blame_thread_args thread_args;
154 struct tog_blame_cb_args cb_args;
158 struct tog_blame_view_state {
159 int first_displayed_line;
160 int last_displayed_line;
165 pthread_mutex_t mutex;
166 struct got_object_id_queue blamed_commits;
167 struct got_object_qid *blamed_commit;
169 struct got_repository *repo;
170 struct got_object_id *commit_id;
171 struct tog_blame blame;
174 struct tog_parent_tree {
175 TAILQ_ENTRY(tog_parent_tree) entry;
176 struct got_tree_object *tree;
177 struct got_tree_entry *first_displayed_entry;
178 struct got_tree_entry *selected_entry;
182 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
184 struct tog_tree_view_state {
186 struct got_tree_object *root;
187 struct got_tree_object *tree;
188 const struct got_tree_entries *entries;
189 struct got_tree_entry *first_displayed_entry;
190 struct got_tree_entry *last_displayed_entry;
191 struct got_tree_entry *selected_entry;
192 int nentries, ndisplayed, selected, show_ids;
193 struct tog_parent_trees parents;
194 struct got_object_id *commit_id;
195 struct got_repository *repo;
198 TAILQ_HEAD(tog_view_list_head, tog_view);
200 TAILQ_ENTRY(tog_view) entry;
203 int nlines, ncols, begin_y, begin_x;
204 int lines, cols; /* copies of LINES and COLS */
206 struct tog_view *parent;
207 struct tog_view *child;
209 /* type-specific state */
210 enum tog_view_type type;
212 struct tog_diff_view_state diff;
213 struct tog_log_view_state log;
214 struct tog_blame_view_state blame;
215 struct tog_tree_view_state tree;
218 const struct got_error *(*show)(struct tog_view *);
219 const struct got_error *(*input)(struct tog_view **,
220 struct tog_view **, struct tog_view *, int);
221 const struct got_error *(*set_child)(struct tog_view *,
223 const struct got_error *(*close)(struct tog_view *);
226 static const struct got_error *open_diff_view(struct tog_view *,
227 struct got_object *, struct got_object *, struct got_repository *);
228 static const struct got_error *show_diff_view(struct tog_view *);
229 static const struct got_error *input_diff_view(struct tog_view **,
230 struct tog_view **, struct tog_view *, int);
231 static const struct got_error* close_diff_view(struct tog_view *);
233 static const struct got_error *open_log_view(struct tog_view *,
234 struct got_object_id *, struct got_repository *, const char *);
235 static const struct got_error * show_log_view(struct tog_view *);
236 static const struct got_error *input_log_view(struct tog_view **,
237 struct tog_view **, struct tog_view *, int);
238 static const struct got_error *close_log_view(struct tog_view *);
239 static const struct got_error* set_child_log_view(struct tog_view *,
242 static const struct got_error *open_blame_view(struct tog_view *, char *,
243 struct got_object_id *, struct got_repository *);
244 static const struct got_error *show_blame_view(struct tog_view *);
245 static const struct got_error *input_blame_view(struct tog_view **,
246 struct tog_view **, struct tog_view *, int);
247 static const struct got_error *close_blame_view(struct tog_view *);
249 static const struct got_error *open_tree_view(struct tog_view *,
250 struct got_tree_object *, struct got_object_id *, struct got_repository *);
251 static const struct got_error *show_tree_view(struct tog_view *);
252 static const struct got_error *input_tree_view(struct tog_view **,
253 struct tog_view **, struct tog_view *, int);
254 static const struct got_error *close_tree_view(struct tog_view *);
256 static const struct got_error *
257 view_close(struct tog_view *view)
259 const struct got_error *err = NULL;
262 view->child->parent = NULL;
264 view->parent->child = NULL;
266 err = view->close(view);
268 del_panel(view->panel);
270 delwin(view->window);
275 static struct tog_view *
276 view_open(int nlines, int ncols, int begin_y, int begin_x,
277 struct tog_view *parent, enum tog_view_type type)
279 struct tog_view *view = calloc(1, sizeof(*view));
284 if (begin_x == 0 && parent && parent->ncols - 80 > 10)
285 begin_x = parent->ncols - 80;
287 view->parent = parent;
289 parent->child = view;
293 view->nlines = nlines ? nlines : LINES - begin_y;
294 view->ncols = ncols ? ncols : COLS - begin_x;
295 view->begin_y = begin_y;
296 view->begin_x = begin_x;
297 view->window = newwin(nlines, ncols, begin_y, begin_x);
298 if (view->window == NULL) {
302 view->panel = new_panel(view->window);
303 if (view->panel == NULL) {
308 keypad(view->window, TRUE);
312 static const struct got_error *
313 view_show(struct tog_view *view)
315 const struct got_error *err;
318 err = view->parent->show(view->parent);
321 show_panel(view->parent->panel);
324 err = view->show(view);
327 show_panel(view->panel);
329 if (view->child && view->child->begin_x > view->begin_x) {
330 err = view->child->show(view->child);
333 show_panel(view->child->panel);
342 static const struct got_error *
343 view_resize(struct tog_view *view)
348 if (view->lines > LINES)
349 nlines = view->nlines - (view->lines - LINES);
351 nlines = view->nlines + (LINES - view->lines);
353 if (view->cols > COLS)
354 ncols = view->ncols - (view->cols - COLS);
356 ncols = view->ncols + (COLS - view->cols);
358 if (wresize(view->window, nlines, ncols) == ERR)
359 return got_error_from_errno();
360 replace_panel(view->panel, view->window);
362 view->nlines = nlines;
373 static const struct got_error *
374 view_input(struct tog_view **new, struct tog_view **dead,
375 struct tog_view **focus, int *done, struct tog_view *view,
376 struct tog_view_list_head *views)
378 const struct got_error *err = NULL;
379 struct tog_view *next, *prev;
385 nodelay(stdscr, FALSE);
386 ch = wgetch(view->window);
387 nodelay(stdscr, TRUE);
392 next = TAILQ_NEXT(view, entry);
396 *focus = TAILQ_FIRST(views);
398 (*focus)->focussed = 1;
401 prev = TAILQ_PREV(view, tog_view_list_head, entry);
405 *focus = TAILQ_LAST(views, tog_view_list_head);
407 (*focus)->focussed = 1;
410 err = view->input(new, dead, view, ch);
417 err = view_resize(view);
420 err = view->input(new, dead, view, ch);
423 err = view->input(new, dead, view, ch);
430 static const struct got_error *
431 view_set_child(struct tog_view *view, struct tog_view *child)
433 const struct got_error *err;
435 if (view->set_child) {
436 err = view->set_child(view, child);
446 view_vborder(struct tog_view *view)
448 if (view->child == NULL)
451 mvwvline(view->window, view->begin_y, view->child->begin_x - 1,
452 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
456 view_needs_focus_indication(struct tog_view *view)
461 if (view->child && view->child->begin_x > view->begin_x)
464 if (view->parent && view->begin_x > view->parent->begin_x)
470 static const struct got_error *
471 view_loop(struct tog_view *view)
473 const struct got_error *err = NULL;
474 struct tog_view_list_head views;
475 struct tog_view *new_view, *dead_view;
479 TAILQ_INSERT_HEAD(&views, view, entry);
482 while (!TAILQ_EMPTY(&views) && !done) {
483 err = view_show(view);
486 err = view_input(&new_view, &dead_view, &view, &done,
491 struct tog_view *v, *t;
492 TAILQ_REMOVE(&views, dead_view, entry);
493 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
494 if (v->parent == dead_view) {
495 TAILQ_REMOVE(&views, v, entry);
501 if (dead_view->parent)
502 view = dead_view->parent;
504 view = TAILQ_LAST(&views, tog_view_list_head);
507 err = view_close(dead_view);
512 struct tog_view *v, *t;
514 /* Only allow one view per type. */
515 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
516 if (v->type != new_view->type)
518 TAILQ_REMOVE(&views, v, entry);
523 TAILQ_INSERT_TAIL(&views, new_view, entry);
524 if (new_view->parent) {
525 err = view_set_child(new_view->parent, new_view);
528 new_view->parent->focussed = 0;
535 while (!TAILQ_EMPTY(&views)) {
536 view = TAILQ_FIRST(&views);
537 TAILQ_REMOVE(&views, view, entry);
548 "usage: %s log [-c commit] [-r repository-path] [path]\n",
553 /* Create newly allocated wide-character string equivalent to a byte string. */
554 static const struct got_error *
555 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
558 const struct got_error *err = NULL;
561 *wlen = mbstowcs(NULL, s, 0);
562 if (*wlen == (size_t)-1) {
565 return got_error_from_errno();
567 /* byte string invalid in current encoding; try to "fix" it */
568 err = got_mbsavis(&vis, &vislen, s);
571 *wlen = mbstowcs(NULL, vis, 0);
572 if (*wlen == (size_t)-1) {
573 err = got_error_from_errno(); /* give up */
578 *ws = calloc(*wlen + 1, sizeof(*ws));
580 err = got_error_from_errno();
584 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
585 err = got_error_from_errno();
596 /* Format a line for display, ensuring that it won't overflow a width limit. */
597 static const struct got_error *
598 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
600 const struct got_error *err = NULL;
602 wchar_t *wline = NULL;
609 err = mbs2ws(&wline, &wlen, line);
614 while (i < wlen && cols < wlimit) {
615 int width = wcwidth(wline[i]);
622 if (cols + width <= wlimit)
627 if (wline[i] == L'\t')
628 cols += TABSIZE - ((cols + 1) % TABSIZE);
632 err = got_error_from_errno();
647 static const struct got_error *
648 draw_commit(struct tog_view *view, struct got_commit_object *commit,
649 struct got_object_id *id)
651 const struct got_error *err = NULL;
652 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
653 char *logmsg0 = NULL, *logmsg = NULL;
654 char *author0 = NULL, *author = NULL;
655 wchar_t *wlogmsg = NULL, *wauthor = NULL;
656 int author_width, logmsg_width;
657 char *newline, *smallerthan;
660 static const size_t date_display_cols = 9;
661 static const size_t author_display_cols = 16;
662 const int avail = view->ncols;
664 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
665 &commit->tm_committer) >= sizeof(datebuf))
666 return got_error(GOT_ERR_NO_SPACE);
668 if (avail < date_display_cols)
669 limit = MIN(sizeof(datebuf) - 1, avail);
671 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
672 waddnstr(view->window, datebuf, limit);
677 author0 = strdup(commit->author);
678 if (author0 == NULL) {
679 err = got_error_from_errno();
683 smallerthan = strchr(author, '<');
687 char *at = strchr(author, '@');
692 err = format_line(&wauthor, &author_width, author, limit);
695 waddwstr(view->window, wauthor);
697 while (col <= avail && author_width < author_display_cols + 1) {
698 waddch(view->window, ' ');
705 logmsg0 = strdup(commit->logmsg);
706 if (logmsg0 == NULL) {
707 err = got_error_from_errno();
711 while (*logmsg == '\n')
713 newline = strchr(logmsg, '\n');
717 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
720 waddwstr(view->window, wlogmsg);
722 while (col <= avail) {
723 waddch(view->window, ' ');
735 static struct commit_queue_entry *
736 alloc_commit_queue_entry(struct got_commit_object *commit,
737 struct got_object_id *id)
739 struct commit_queue_entry *entry;
741 entry = calloc(1, sizeof(*entry));
746 entry->commit = commit;
751 pop_commit(struct commit_queue *commits)
753 struct commit_queue_entry *entry;
755 entry = TAILQ_FIRST(&commits->head);
756 TAILQ_REMOVE(&commits->head, entry, entry);
757 got_object_commit_close(entry->commit);
759 /* Don't free entry->id! It is owned by the commit graph. */
764 free_commits(struct commit_queue *commits)
766 while (!TAILQ_EMPTY(&commits->head))
770 static const struct got_error *
771 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
772 struct got_object_id *start_id, int minqueue,
773 struct got_repository *repo, const char *path)
775 const struct got_error *err = NULL;
779 err = got_commit_graph_iter_start(graph, start_id, repo);
784 while (nqueued < minqueue) {
785 struct got_object_id *id;
786 struct got_commit_object *commit;
787 struct commit_queue_entry *entry;
789 err = got_commit_graph_iter_next(&id, graph);
791 if (err->code == GOT_ERR_ITER_COMPLETED) {
795 if (err->code != GOT_ERR_ITER_NEED_MORE)
797 err = got_commit_graph_fetch_commits(graph,
807 err = got_object_open_as_commit(&commit, repo, id);
810 entry = alloc_commit_queue_entry(commit, id);
812 err = got_error_from_errno();
816 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
824 static const struct got_error *
825 fetch_next_commit(struct commit_queue_entry **pentry,
826 struct commit_queue_entry *entry, struct commit_queue *commits,
827 struct got_commit_graph *graph, struct got_repository *repo,
830 const struct got_error *err = NULL;
834 err = queue_commits(graph, commits, NULL, 1, repo, path);
838 /* Next entry to display should now be available. */
839 *pentry = TAILQ_NEXT(entry, entry);
843 static const struct got_error *
844 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
846 const struct got_error *err = NULL;
847 struct got_reference *head_ref;
851 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
855 err = got_ref_resolve(head_id, repo, head_ref);
856 got_ref_close(head_ref);
865 static const struct got_error *
866 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
867 struct commit_queue_entry **selected, struct commit_queue_entry *first,
868 struct commit_queue *commits, int selected_idx, int limit,
869 struct got_commit_graph *graph, struct got_repository *repo,
872 const struct got_error *err = NULL;
873 struct commit_queue_entry *entry;
875 char *id_str, *header;
881 if (ncommits == selected_idx) {
885 entry = TAILQ_NEXT(entry, entry);
889 err = got_object_id_str(&id_str, (*selected)->id);
893 if (path && strcmp(path, "/") != 0) {
894 if (asprintf(&header, "commit: %s [%s]", id_str, path) == -1) {
895 err = got_error_from_errno();
899 } else if (asprintf(&header, "commit: %s", id_str) == -1) {
900 err = got_error_from_errno();
905 err = format_line(&wline, &width, header, view->ncols);
912 werase(view->window);
914 if (view_needs_focus_indication(view))
915 wstandout(view->window);
916 waddwstr(view->window, wline);
917 if (view_needs_focus_indication(view))
918 wstandend(view->window);
919 if (width < view->ncols)
920 waddch(view->window, '\n');
929 if (ncommits >= limit - 1)
931 if (ncommits == selected_idx)
932 wstandout(view->window);
933 err = draw_commit(view, entry->commit, entry->id);
934 if (ncommits == selected_idx)
935 wstandend(view->window);
940 if (entry == TAILQ_LAST(&commits->head, commit_queue_head)) {
941 err = queue_commits(graph, commits, NULL, 1,
944 if (err->code != GOT_ERR_ITER_COMPLETED)
949 entry = TAILQ_NEXT(entry, entry);
958 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
959 struct commit_queue *commits)
961 struct commit_queue_entry *entry;
964 entry = TAILQ_FIRST(&commits->head);
965 if (*first_displayed_entry == entry)
968 entry = *first_displayed_entry;
969 while (entry && nscrolled < maxscroll) {
970 entry = TAILQ_PREV(entry, commit_queue_head, entry);
972 *first_displayed_entry = entry;
978 static const struct got_error *
979 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
980 struct commit_queue_entry **last_displayed_entry,
981 struct commit_queue *commits, struct got_commit_graph *graph,
982 struct got_repository *repo, const char *path)
984 const struct got_error *err = NULL;
985 struct commit_queue_entry *pentry;
989 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
990 if (pentry == NULL) {
991 err = fetch_next_commit(&pentry, *last_displayed_entry,
992 commits, graph, repo, path);
993 if (err || pentry == NULL)
996 *last_displayed_entry = pentry;
998 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1001 *first_displayed_entry = pentry;
1002 } while (++nscrolled < maxscroll);
1007 static const struct got_error *
1008 show_commit(struct tog_view **new_view, struct tog_view *parent_view,
1009 struct commit_queue_entry *entry, struct got_repository *repo)
1011 const struct got_error *err;
1012 struct got_object *obj1 = NULL, *obj2 = NULL;
1013 struct got_object_qid *parent_id;
1014 struct tog_view *diff_view;
1016 err = got_object_open(&obj2, repo, entry->id);
1020 parent_id = SIMPLEQ_FIRST(&entry->commit->parent_ids);
1022 err = got_object_open(&obj1, repo, parent_id->id);
1027 diff_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_DIFF);
1028 if (diff_view == NULL) {
1029 err = got_error_from_errno();
1033 err = open_diff_view(diff_view, obj1, obj2, repo);
1035 *new_view = diff_view;
1038 got_object_close(obj1);
1040 got_object_close(obj2);
1044 static const struct got_error *
1045 browse_commit(struct tog_view **new_view, struct tog_view *parent_view,
1046 struct commit_queue_entry *entry, struct got_repository *repo)
1048 const struct got_error *err = NULL;
1049 struct got_tree_object *tree;
1050 struct tog_view *tree_view;
1052 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1056 tree_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_TREE);
1057 if (tree_view == NULL)
1058 return got_error_from_errno();
1060 err = open_tree_view(tree_view, tree, entry->id, repo);
1062 got_object_tree_close(tree);
1064 *new_view = tree_view;
1068 static const struct got_error *
1069 set_child_log_view(struct tog_view *view, struct tog_view *child)
1071 struct tog_log_view_state *s = &view->state.log;
1072 struct tog_diff_view_state *ds;
1073 struct commit_queue_entry *commit, *child_entry = NULL;
1074 int selected_idx = 0;
1076 if (child->type != TOG_VIEW_DIFF)
1078 ds = &child->state.diff;
1080 TAILQ_FOREACH(commit, &s->commits.head, entry) {
1081 if (got_object_id_cmp(commit->id, ds->id2) == 0) {
1082 child_entry = commit;
1086 if (child_entry == NULL)
1089 commit = s->first_displayed_entry;
1091 if (got_object_id_cmp(commit->id, child_entry->id) == 0) {
1092 s->selected_entry = child_entry;
1093 s->selected = selected_idx;
1096 if (commit == s->last_displayed_entry)
1099 commit = TAILQ_NEXT(commit, entry);
1102 return show_log_view(view);
1105 static const struct got_error *
1106 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1107 struct got_repository *repo, const char *path)
1109 const struct got_error *err = NULL;
1110 struct tog_log_view_state *s = &view->state.log;
1112 err = got_repo_map_path(&s->in_repo_path, repo, path);
1116 err = got_commit_graph_open(&s->graph, start_id, s->in_repo_path,
1120 /* The commit queue only contains commits being displayed. */
1121 TAILQ_INIT(&s->commits.head);
1122 s->commits.ncommits = 0;
1125 * Open the initial batch of commits, sorted in commit graph order.
1126 * We keep all commits open throughout the lifetime of the log view
1127 * in order to avoid having to re-fetch commits from disk while
1128 * updating the display.
1130 err = queue_commits(s->graph, &s->commits, start_id, view->nlines,
1131 repo, s->in_repo_path);
1133 if (err->code != GOT_ERR_ITER_COMPLETED)
1138 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
1139 s->selected_entry = s->first_displayed_entry;
1141 s->start_id = got_object_id_dup(start_id);
1142 if (s->start_id == NULL) {
1143 err = got_error_from_errno();
1147 view->show = show_log_view;
1148 view->input = input_log_view;
1149 view->close = close_log_view;
1150 view->set_child = set_child_log_view;
1155 static const struct got_error *
1156 close_log_view(struct tog_view *view)
1158 struct tog_log_view_state *s = &view->state.log;
1161 got_commit_graph_close(s->graph);
1162 free_commits(&s->commits);
1163 free(s->in_repo_path);
1168 static const struct got_error *
1169 update_diff_view(struct tog_view *diff_view,
1170 struct got_object_id *commit_id, struct got_commit_object *commit,
1171 struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 struct tog_diff_view_state *ds;
1175 struct got_object *obj1 = NULL, *obj2 = NULL;
1176 struct got_object_qid *parent_id;
1178 ds = &diff_view->state.diff;
1179 if (got_object_id_cmp(ds->id2, commit_id) == 0)
1182 err = got_object_open(&obj2, repo, commit_id);
1186 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1188 err = got_object_open(&obj1, repo, parent_id->id);
1193 err = close_diff_view(diff_view);
1197 err = open_diff_view(diff_view, obj1, obj2, repo);
1202 got_object_close(obj1);
1204 got_object_close(obj2);
1208 static const struct got_error *
1209 show_log_view(struct tog_view *view)
1211 const struct got_error *err = NULL;
1212 struct tog_log_view_state *s = &view->state.log;
1214 err = draw_commits(view, &s->last_displayed_entry,
1215 &s->selected_entry, s->first_displayed_entry,
1216 &s->commits, s->selected, view->nlines, s->graph,
1217 s->repo, s->in_repo_path);
1221 if (view->child && view->child->type == TOG_VIEW_DIFF)
1222 err = update_diff_view(view->child, s->selected_entry->id,
1223 s->selected_entry->commit, s->repo);
1228 static const struct got_error *
1229 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1230 struct tog_view *view, int ch)
1232 const struct got_error *err = NULL;
1233 struct tog_log_view_state *s = &view->state.log;
1240 if (s->selected > 0)
1242 if (s->selected > 0)
1244 scroll_up(&s->first_displayed_entry, 1,
1248 if (TAILQ_FIRST(&s->commits.head) ==
1249 s->first_displayed_entry) {
1253 scroll_up(&s->first_displayed_entry,
1254 view->nlines, &s->commits);
1259 if (s->selected < MIN(view->nlines - 2,
1260 s->commits.ncommits - 1)) {
1264 err = scroll_down(&s->first_displayed_entry, 1,
1265 &s->last_displayed_entry, &s->commits,
1266 s->graph, s->repo, s->in_repo_path);
1268 if (err->code != GOT_ERR_ITER_COMPLETED)
1274 struct commit_queue_entry *first;
1275 first = s->first_displayed_entry;
1276 err = scroll_down(&s->first_displayed_entry,
1277 view->nlines, &s->last_displayed_entry,
1278 &s->commits, s->graph, s->repo,
1280 if (err && err->code != GOT_ERR_ITER_COMPLETED)
1282 if (first == s->first_displayed_entry &&
1283 s->selected < MIN(view->nlines - 2,
1284 s->commits.ncommits - 1)) {
1285 /* can't scroll further down */
1286 s->selected = MIN(view->nlines - 2,
1287 s->commits.ncommits - 1);
1293 if (s->selected > view->nlines - 2)
1294 s->selected = view->nlines - 2;
1295 if (s->selected > s->commits.ncommits - 1)
1296 s->selected = s->commits.ncommits - 1;
1300 err = show_commit(new_view, view, s->selected_entry,
1304 err = browse_commit(new_view, view, s->selected_entry,
1308 if (strcmp(s->in_repo_path, "/") == 0)
1310 parent_path = dirname(s->in_repo_path);
1311 if (parent_path && strcmp(parent_path, ".") != 0) {
1312 struct tog_view *lv;
1313 lv = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1315 return got_error_from_errno();
1316 err = open_log_view(lv, s->start_id, s->repo,
1330 static const struct got_error *
1331 cmd_log(int argc, char *argv[])
1333 const struct got_error *error;
1334 struct got_repository *repo = NULL;
1335 struct got_object_id *start_id = NULL;
1336 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1337 char *start_commit = NULL;
1339 struct tog_view *view;
1342 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1347 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1350 start_commit = optarg;
1353 repo_path = realpath(optarg, NULL);
1354 if (repo_path == NULL)
1355 err(1, "-r option");
1369 path = strdup(argv[0]);
1373 return got_error_from_errno();
1375 cwd = getcwd(NULL, 0);
1377 error = got_error_from_errno();
1380 if (repo_path == NULL) {
1381 repo_path = strdup(cwd);
1382 if (repo_path == NULL) {
1383 error = got_error_from_errno();
1388 error = got_repo_open(&repo, repo_path);
1392 if (start_commit == NULL) {
1393 error = get_head_commit_id(&start_id, repo);
1397 struct got_object *obj;
1398 error = got_object_open_by_id_str(&obj, repo, start_commit);
1399 if (error == NULL) {
1400 start_id = got_object_id_dup(got_object_get_id(obj));
1401 if (start_id == NULL)
1402 error = got_error_from_errno();
1409 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_LOG);
1411 error = got_error_from_errno();
1414 error = open_log_view(view, start_id, repo, path);
1417 error = view_loop(view);
1424 got_repo_close(repo);
1432 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1438 parse_next_line(FILE *f, size_t *len)
1443 const char delim[3] = { '\0', '\0', '\0'};
1445 line = fparseln(f, &linelen, &lineno, delim, 0);
1451 static const struct got_error *
1452 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1453 int *last_displayed_line, int *eof, int max_lines,
1456 const struct got_error *err;
1457 int nlines = 0, nprinted = 0;
1464 werase(view->window);
1467 err = format_line(&wline, &width, header, view->ncols);
1472 if (view_needs_focus_indication(view))
1473 wstandout(view->window);
1474 waddwstr(view->window, wline);
1475 if (view_needs_focus_indication(view))
1476 wstandend(view->window);
1477 if (width < view->ncols)
1478 waddch(view->window, '\n');
1486 while (nprinted < max_lines) {
1487 line = parse_next_line(f, &len);
1492 if (++nlines < *first_displayed_line) {
1497 err = format_line(&wline, &width, line, view->ncols);
1502 waddwstr(view->window, wline);
1503 if (width < view->ncols)
1504 waddch(view->window, '\n');
1505 if (++nprinted == 1)
1506 *first_displayed_line = nlines;
1511 *last_displayed_line = nlines;
1518 static const struct got_error *
1519 open_diff_view(struct tog_view *view, struct got_object *obj1,
1520 struct got_object *obj2, struct got_repository *repo)
1522 const struct got_error *err;
1525 if (obj1 != NULL && obj2 != NULL &&
1526 got_object_get_type(obj1) != got_object_get_type(obj2))
1527 return got_error(GOT_ERR_OBJ_TYPE);
1531 return got_error_from_errno();
1533 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1534 case GOT_OBJ_TYPE_BLOB:
1535 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1538 case GOT_OBJ_TYPE_TREE:
1539 err = got_diff_objects_as_trees(obj1, obj2, "", "", repo, f);
1541 case GOT_OBJ_TYPE_COMMIT:
1542 err = got_diff_objects_as_commits(obj1, obj2, repo, f);
1545 return got_error(GOT_ERR_OBJ_TYPE);
1550 view->state.diff.id1 = obj1 ? got_object_get_id(obj1) : NULL;
1551 view->state.diff.id2 = got_object_get_id(obj2);
1552 view->state.diff.f = f;
1553 view->state.diff.first_displayed_line = 1;
1554 view->state.diff.last_displayed_line = view->nlines;
1556 view->show = show_diff_view;
1557 view->input = input_diff_view;
1558 view->close = close_diff_view;
1563 static const struct got_error *
1564 close_diff_view(struct tog_view *view)
1566 const struct got_error *err = NULL;
1568 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1569 err = got_error_from_errno();
1573 static const struct got_error *
1574 show_diff_view(struct tog_view *view)
1576 const struct got_error *err;
1577 struct tog_diff_view_state *s = &view->state.diff;
1578 char *id_str1 = NULL, *id_str2, *header;
1581 err = got_object_id_str(&id_str1, s->id1);
1585 err = got_object_id_str(&id_str2, s->id2);
1589 if (asprintf(&header, "diff: %s %s",
1590 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1591 err = got_error_from_errno();
1599 return draw_file(view, s->f, &s->first_displayed_line,
1600 &s->last_displayed_line, &s->eof, view->nlines,
1604 static const struct got_error *
1605 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1606 struct tog_view *view, int ch)
1608 const struct got_error *err = NULL;
1609 struct tog_diff_view_state *s = &view->state.diff;
1615 if (s->first_displayed_line > 1)
1616 s->first_displayed_line--;
1620 while (i++ < view->nlines - 1 &&
1621 s->first_displayed_line > 1)
1622 s->first_displayed_line--;
1627 s->first_displayed_line++;
1632 while (!s->eof && i++ < view->nlines - 1) {
1634 line = parse_next_line(s->f, NULL);
1635 s->first_displayed_line++;
1642 struct tog_log_view_state *ls;
1643 struct commit_queue_entry *entry;
1644 struct tog_view *diff_view;
1646 if (view->parent == NULL)
1648 if (view->parent->type != TOG_VIEW_LOG)
1650 ls = &view->parent->state.log;
1653 entry = TAILQ_PREV(ls->selected_entry,
1654 commit_queue_head, entry);
1656 entry = TAILQ_NEXT(ls->selected_entry, entry);
1657 if (entry == NULL) {
1658 err = fetch_next_commit(&entry,
1660 &ls->commits, ls->graph,
1661 ls->repo, ls->in_repo_path);
1668 err = show_commit(&diff_view, view->parent,
1672 *new_view = diff_view;
1683 static const struct got_error *
1684 cmd_diff(int argc, char *argv[])
1686 const struct got_error *error = NULL;
1687 struct got_repository *repo = NULL;
1688 struct got_object *obj1 = NULL, *obj2 = NULL;
1689 char *repo_path = NULL;
1690 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1692 struct tog_view *view;
1695 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1700 while ((ch = getopt(argc, argv, "")) != -1) {
1712 usage_diff(); /* TODO show local worktree changes */
1713 } else if (argc == 2) {
1714 repo_path = getcwd(NULL, 0);
1715 if (repo_path == NULL)
1716 return got_error_from_errno();
1717 obj_id_str1 = argv[0];
1718 obj_id_str2 = argv[1];
1719 } else if (argc == 3) {
1720 repo_path = realpath(argv[0], NULL);
1721 if (repo_path == NULL)
1722 return got_error_from_errno();
1723 obj_id_str1 = argv[1];
1724 obj_id_str2 = argv[2];
1728 error = got_repo_open(&repo, repo_path);
1733 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
1737 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
1741 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_DIFF);
1743 error = got_error_from_errno();
1746 error = open_diff_view(view, obj1, obj2, repo);
1749 error = view_loop(view);
1751 got_repo_close(repo);
1753 got_object_close(obj1);
1755 got_object_close(obj2);
1763 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
1768 struct tog_blame_line {
1770 struct got_object_id *id;
1773 static const struct got_error *
1774 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
1775 const char *path, struct tog_blame_line *lines, int nlines,
1776 int blame_complete, int selected_line, int *first_displayed_line,
1777 int *last_displayed_line, int *eof, int max_lines)
1779 const struct got_error *err;
1780 int lineno = 0, nprinted = 0;
1785 struct tog_blame_line *blame_line;
1786 struct got_object_id *prev_id = NULL;
1789 err = got_object_id_str(&id_str, id);
1794 werase(view->window);
1796 if (asprintf(&line, "commit: %s", id_str) == -1) {
1797 err = got_error_from_errno();
1802 err = format_line(&wline, &width, line, view->ncols);
1805 if (view_needs_focus_indication(view))
1806 wstandout(view->window);
1807 waddwstr(view->window, wline);
1808 if (view_needs_focus_indication(view))
1809 wstandend(view->window);
1812 if (width < view->ncols)
1813 waddch(view->window, '\n');
1815 if (asprintf(&line, "[%d/%d] %s%s",
1816 *first_displayed_line - 1 + selected_line, nlines,
1817 blame_complete ? "" : "annotating ", path) == -1) {
1819 return got_error_from_errno();
1822 err = format_line(&wline, &width, line, view->ncols);
1827 waddwstr(view->window, wline);
1830 if (width < view->ncols)
1831 waddch(view->window, '\n');
1834 while (nprinted < max_lines - 2) {
1835 line = parse_next_line(f, &len);
1840 if (++lineno < *first_displayed_line) {
1845 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
1846 err = format_line(&wline, &width, line, wlimit);
1852 if (nprinted == selected_line - 1)
1853 wstandout(view->window);
1855 blame_line = &lines[lineno - 1];
1856 if (blame_line->annotated && prev_id &&
1857 got_object_id_cmp(prev_id, blame_line->id) == 0)
1858 waddstr(view->window, " ");
1859 else if (blame_line->annotated) {
1861 err = got_object_id_str(&id_str, blame_line->id);
1867 wprintw(view->window, "%.8s ", id_str);
1869 prev_id = blame_line->id;
1871 waddstr(view->window, "........ ");
1875 waddwstr(view->window, wline);
1876 while (width < wlimit) {
1877 waddch(view->window, ' ');
1880 if (nprinted == selected_line - 1)
1881 wstandend(view->window);
1882 if (++nprinted == 1)
1883 *first_displayed_line = lineno;
1888 *last_displayed_line = lineno;
1895 static const struct got_error *
1896 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
1898 const struct got_error *err = NULL;
1899 struct tog_blame_cb_args *a = arg;
1900 struct tog_blame_line *line;
1902 if (nlines != a->nlines ||
1903 (lineno != -1 && lineno < 1) || lineno > a->nlines)
1904 return got_error(GOT_ERR_RANGE);
1906 if (pthread_mutex_lock(a->mutex) != 0)
1907 return got_error_from_errno();
1909 if (*a->quit) { /* user has quit the blame view */
1910 err = got_error(GOT_ERR_ITER_COMPLETED);
1915 goto done; /* no change in this commit */
1917 line = &a->lines[lineno - 1];
1918 if (line->annotated)
1921 line->id = got_object_id_dup(id);
1922 if (line->id == NULL) {
1923 err = got_error_from_errno();
1926 line->annotated = 1;
1928 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1929 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
1930 a->last_displayed_line, a->eof, a->view->nlines);
1932 if (pthread_mutex_unlock(a->mutex) != 0)
1933 return got_error_from_errno();
1938 blame_thread(void *arg)
1940 const struct got_error *err;
1941 struct tog_blame_thread_args *ta = arg;
1942 struct tog_blame_cb_args *a = ta->cb_args;
1944 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
1945 blame_cb, ta->cb_args);
1947 if (pthread_mutex_lock(a->mutex) != 0)
1948 return (void *)got_error_from_errno();
1950 got_repo_close(ta->repo);
1954 err = draw_blame(a->view, a->commit_id, a->f, a->path,
1955 a->lines, a->nlines, 1, *a->selected_line,
1956 a->first_displayed_line, a->last_displayed_line, a->eof,
1959 if (pthread_mutex_unlock(a->mutex) != 0 && err == NULL)
1960 err = got_error_from_errno();
1965 static struct got_object_id *
1966 get_selected_commit_id(struct tog_blame_line *lines,
1967 int first_displayed_line, int selected_line)
1969 struct tog_blame_line *line;
1971 line = &lines[first_displayed_line - 1 + selected_line - 1];
1972 if (!line->annotated)
1978 static const struct got_error *
1979 open_selected_commit(struct got_object **pobj, struct got_object **obj,
1980 struct tog_blame_line *lines, int first_displayed_line,
1981 int selected_line, struct got_repository *repo)
1983 const struct got_error *err = NULL;
1984 struct got_commit_object *commit = NULL;
1985 struct got_object_id *selected_id;
1986 struct got_object_qid *pid;
1991 selected_id = get_selected_commit_id(lines,
1992 first_displayed_line, selected_line);
1993 if (selected_id == NULL)
1996 err = got_object_open(obj, repo, selected_id);
2000 err = got_object_commit_open(&commit, repo, *obj);
2004 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2006 err = got_object_open(pobj, repo, pid->id);
2012 got_object_commit_close(commit);
2016 static const struct got_error *
2017 stop_blame(struct tog_blame *blame)
2019 const struct got_error *err = NULL;
2022 if (blame->thread) {
2023 if (pthread_join(blame->thread, (void **)&err) != 0)
2024 err = got_error_from_errno();
2025 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2027 blame->thread = NULL;
2029 if (blame->thread_args.repo) {
2030 got_repo_close(blame->thread_args.repo);
2031 blame->thread_args.repo = NULL;
2037 for (i = 0; i < blame->nlines; i++)
2038 free(blame->lines[i].id);
2040 blame->lines = NULL;
2041 free(blame->cb_args.commit_id);
2042 blame->cb_args.commit_id = NULL;
2047 static const struct got_error *
2048 run_blame(struct tog_blame *blame, pthread_mutex_t *mutex,
2049 struct tog_view *view, int *blame_complete,
2050 int *first_displayed_line, int *last_displayed_line,
2051 int *selected_line, int *done, int *eof, const char *path,
2052 struct got_object_id *commit_id,
2053 struct got_repository *repo)
2055 const struct got_error *err = NULL;
2056 struct got_blob_object *blob = NULL;
2057 struct got_repository *thread_repo = NULL;
2058 struct got_object_id *obj_id = NULL;
2059 struct got_object *obj = NULL;
2061 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2065 err = got_object_open(&obj, repo, obj_id);
2069 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2070 err = got_error(GOT_ERR_OBJ_TYPE);
2074 err = got_object_blob_open(&blob, repo, obj, 8192);
2077 blame->f = got_opentemp();
2078 if (blame->f == NULL) {
2079 err = got_error_from_errno();
2082 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2087 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2088 if (blame->lines == NULL) {
2089 err = got_error_from_errno();
2093 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2097 blame->cb_args.view = view;
2098 blame->cb_args.lines = blame->lines;
2099 blame->cb_args.nlines = blame->nlines;
2100 blame->cb_args.mutex = mutex;
2101 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2102 if (blame->cb_args.commit_id == NULL) {
2103 err = got_error_from_errno();
2106 blame->cb_args.f = blame->f;
2107 blame->cb_args.path = path;
2108 blame->cb_args.first_displayed_line = first_displayed_line;
2109 blame->cb_args.selected_line = selected_line;
2110 blame->cb_args.last_displayed_line = last_displayed_line;
2111 blame->cb_args.quit = done;
2112 blame->cb_args.eof = eof;
2114 blame->thread_args.path = path;
2115 blame->thread_args.repo = thread_repo;
2116 blame->thread_args.cb_args = &blame->cb_args;
2117 blame->thread_args.complete = blame_complete;
2118 *blame_complete = 0;
2120 if (pthread_create(&blame->thread, NULL, blame_thread,
2121 &blame->thread_args) != 0) {
2122 err = got_error_from_errno();
2128 got_object_blob_close(blob);
2131 got_object_close(obj);
2137 static const struct got_error *
2138 open_blame_view(struct tog_view *view, char *path,
2139 struct got_object_id *commit_id, struct got_repository *repo)
2141 const struct got_error *err = NULL;
2142 struct tog_blame_view_state *s = &view->state.blame;
2144 SIMPLEQ_INIT(&s->blamed_commits);
2146 if (pthread_mutex_init(&s->mutex, NULL) != 0)
2147 return got_error_from_errno();
2149 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2153 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2154 s->first_displayed_line = 1;
2155 s->last_displayed_line = view->nlines;
2156 s->selected_line = 1;
2157 s->blame_complete = 0;
2159 if (s->path == NULL)
2160 return got_error_from_errno();
2162 s->commit_id = commit_id;
2163 memset(&s->blame, 0, sizeof(s->blame));
2165 view->show = show_blame_view;
2166 view->input = input_blame_view;
2167 view->close = close_blame_view;
2169 return run_blame(&s->blame, &s->mutex, view, &s->blame_complete,
2170 &s->first_displayed_line, &s->last_displayed_line,
2171 &s->selected_line, &s->done, &s->eof, s->path,
2172 s->blamed_commit->id, s->repo);
2175 static const struct got_error *
2176 close_blame_view(struct tog_view *view)
2178 const struct got_error *err = NULL;
2179 struct tog_blame_view_state *s = &view->state.blame;
2181 if (s->blame.thread)
2182 err = stop_blame(&s->blame);
2184 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2185 struct got_object_qid *blamed_commit;
2186 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2187 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2188 got_object_qid_free(blamed_commit);
2196 static const struct got_error *
2197 show_blame_view(struct tog_view *view)
2199 const struct got_error *err = NULL;
2200 struct tog_blame_view_state *s = &view->state.blame;
2202 if (pthread_mutex_lock(&s->mutex) != 0)
2203 return got_error_from_errno();
2205 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2206 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2207 s->selected_line, &s->first_displayed_line,
2208 &s->last_displayed_line, &s->eof, view->nlines);
2210 if (pthread_mutex_unlock(&s->mutex) != 0 && err == NULL)
2211 err = got_error_from_errno();
2216 static const struct got_error *
2217 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2218 struct tog_view *view, int ch)
2220 const struct got_error *err = NULL, *thread_err = NULL;
2221 struct got_object *obj = NULL, *pobj = NULL;
2222 struct tog_view *diff_view;
2223 struct tog_blame_view_state *s = &view->state.blame;
2225 if (pthread_mutex_lock(&s->mutex) != 0) {
2226 err = got_error_from_errno();
2233 if (pthread_mutex_unlock(&s->mutex) != 0) {
2234 err = got_error_from_errno();
2237 return stop_blame(&s->blame);
2240 if (s->selected_line > 1)
2242 else if (s->selected_line == 1 &&
2243 s->first_displayed_line > 1)
2244 s->first_displayed_line--;
2247 if (s->first_displayed_line == 1) {
2248 s->selected_line = 1;
2251 if (s->first_displayed_line > view->nlines - 2)
2252 s->first_displayed_line -=
2255 s->first_displayed_line = 1;
2259 if (s->selected_line < view->nlines - 2 &&
2260 s->first_displayed_line +
2261 s->selected_line <= s->blame.nlines)
2263 else if (s->last_displayed_line <
2265 s->first_displayed_line++;
2269 struct got_object_id *id;
2270 id = get_selected_commit_id(s->blame.lines,
2271 s->first_displayed_line, s->selected_line);
2272 if (id == NULL || got_object_id_cmp(id,
2273 s->blamed_commit->id) == 0)
2275 err = open_selected_commit(&pobj, &obj,
2276 s->blame.lines, s->first_displayed_line,
2277 s->selected_line, s->repo);
2280 if (pobj == NULL && obj == NULL)
2282 if (ch == 'p' && pobj == NULL)
2285 if (pthread_mutex_unlock(&s->mutex) != 0) {
2286 err = got_error_from_errno();
2289 thread_err = stop_blame(&s->blame);
2291 if (pthread_mutex_lock(&s->mutex) != 0) {
2292 err = got_error_from_errno();
2297 id = got_object_get_id(ch == 'b' ? obj : pobj);
2298 got_object_close(obj);
2301 got_object_close(pobj);
2304 err = got_object_qid_alloc(&s->blamed_commit, id);
2307 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2308 s->blamed_commit, entry);
2309 err = run_blame(&s->blame, &s->mutex, view,
2311 &s->first_displayed_line,
2312 &s->last_displayed_line,
2313 &s->selected_line, &s->done, &s->eof,
2314 s->path, s->blamed_commit->id, s->repo);
2320 struct got_object_qid *first;
2321 first = SIMPLEQ_FIRST(&s->blamed_commits);
2322 if (!got_object_id_cmp(first->id, s->commit_id))
2325 if (pthread_mutex_unlock(&s->mutex) != 0) {
2326 err = got_error_from_errno();
2329 thread_err = stop_blame(&s->blame);
2331 if (pthread_mutex_lock(&s->mutex) != 0) {
2332 err = got_error_from_errno();
2337 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2338 got_object_qid_free(s->blamed_commit);
2340 SIMPLEQ_FIRST(&s->blamed_commits);
2341 err = run_blame(&s->blame, &s->mutex, view,
2343 &s->first_displayed_line,
2344 &s->last_displayed_line,
2345 &s->selected_line, &s->done, &s->eof, s->path,
2346 s->blamed_commit->id, s->repo);
2353 err = open_selected_commit(&pobj, &obj,
2354 s->blame.lines, s->first_displayed_line,
2355 s->selected_line, s->repo);
2358 if (pobj == NULL && obj == NULL)
2360 diff_view = view_open(0, 0, 0, 0, view,
2362 if (diff_view == NULL) {
2363 err = got_error_from_errno();
2366 err = open_diff_view(diff_view, pobj, obj,
2369 view_close(diff_view);
2372 *new_view = diff_view;
2374 got_object_close(pobj);
2377 got_object_close(obj);
2384 if (s->last_displayed_line >= s->blame.nlines &&
2385 s->selected_line < view->nlines - 2) {
2386 s->selected_line = MIN(s->blame.nlines,
2390 if (s->last_displayed_line + view->nlines - 2
2392 s->first_displayed_line +=
2395 s->first_displayed_line =
2400 if (s->selected_line > view->nlines - 2) {
2401 s->selected_line = MIN(s->blame.nlines,
2409 if (pthread_mutex_unlock(&s->mutex) != 0)
2410 err = got_error_from_errno();
2413 got_object_close(pobj);
2414 return thread_err ? thread_err : err;
2417 static const struct got_error *
2418 cmd_blame(int argc, char *argv[])
2420 const struct got_error *error;
2421 struct got_repository *repo = NULL;
2422 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2423 struct got_object_id *commit_id = NULL;
2424 char *commit_id_str = NULL;
2426 struct tog_view *view;
2429 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2434 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2437 commit_id_str = optarg;
2440 repo_path = realpath(optarg, NULL);
2441 if (repo_path == NULL)
2442 err(1, "-r option");
2458 cwd = getcwd(NULL, 0);
2460 error = got_error_from_errno();
2463 if (repo_path == NULL) {
2464 repo_path = strdup(cwd);
2465 if (repo_path == NULL) {
2466 error = got_error_from_errno();
2472 error = got_repo_open(&repo, repo_path);
2476 error = got_repo_map_path(&in_repo_path, repo, path);
2480 if (commit_id_str == NULL) {
2481 struct got_reference *head_ref;
2482 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2485 error = got_ref_resolve(&commit_id, repo, head_ref);
2486 got_ref_close(head_ref);
2488 struct got_object *obj;
2489 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2492 commit_id = got_object_id_dup(got_object_get_id(obj));
2493 if (commit_id == NULL)
2494 error = got_error_from_errno();
2495 got_object_close(obj);
2500 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_BLAME);
2502 error = got_error_from_errno();
2505 error = open_blame_view(view, in_repo_path, commit_id, repo);
2508 error = view_loop(view);
2514 got_repo_close(repo);
2518 static const struct got_error *
2519 draw_tree_entries(struct tog_view *view,
2520 struct got_tree_entry **first_displayed_entry,
2521 struct got_tree_entry **last_displayed_entry,
2522 struct got_tree_entry **selected_entry, int *ndisplayed,
2523 const char *label, int show_ids, const char *parent_path,
2524 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2526 const struct got_error *err = NULL;
2527 struct got_tree_entry *te;
2533 werase(view->window);
2538 err = format_line(&wline, &width, label, view->ncols);
2541 if (view_needs_focus_indication(view))
2542 wstandout(view->window);
2543 waddwstr(view->window, wline);
2544 if (view_needs_focus_indication(view))
2545 wstandend(view->window);
2548 if (width < view->ncols)
2549 waddch(view->window, '\n');
2552 err = format_line(&wline, &width, parent_path, view->ncols);
2555 waddwstr(view->window, wline);
2558 if (width < view->ncols)
2559 waddch(view->window, '\n');
2562 waddch(view->window, '\n');
2566 te = SIMPLEQ_FIRST(&entries->head);
2567 if (*first_displayed_entry == NULL) {
2568 if (selected == 0) {
2569 wstandout(view->window);
2570 *selected_entry = NULL;
2572 waddstr(view->window, " ..\n"); /* parent directory */
2574 wstandend(view->window);
2581 while (te != *first_displayed_entry)
2582 te = SIMPLEQ_NEXT(te, entry);
2586 char *line = NULL, *id_str = NULL;
2589 err = got_object_id_str(&id_str, te->id);
2591 return got_error_from_errno();
2593 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2594 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2596 return got_error_from_errno();
2599 err = format_line(&wline, &width, line, view->ncols);
2604 if (n == selected) {
2605 wstandout(view->window);
2606 *selected_entry = te;
2608 waddwstr(view->window, wline);
2609 if (width < view->ncols)
2610 waddch(view->window, '\n');
2612 wstandend(view->window);
2618 *last_displayed_entry = te;
2621 te = SIMPLEQ_NEXT(te, entry);
2629 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2630 const struct got_tree_entries *entries, int isroot)
2632 struct got_tree_entry *te, *prev;
2635 if (*first_displayed_entry == NULL)
2638 te = SIMPLEQ_FIRST(&entries->head);
2639 if (*first_displayed_entry == te) {
2641 *first_displayed_entry = NULL;
2645 /* XXX this is stupid... switch to TAILQ? */
2646 for (i = 0; i < maxscroll; i++) {
2647 while (te != *first_displayed_entry) {
2649 te = SIMPLEQ_NEXT(te, entry);
2651 *first_displayed_entry = prev;
2652 te = SIMPLEQ_FIRST(&entries->head);
2654 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2655 *first_displayed_entry = NULL;
2659 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2660 struct got_tree_entry *last_displayed_entry,
2661 const struct got_tree_entries *entries)
2663 struct got_tree_entry *next;
2666 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2669 if (*first_displayed_entry)
2670 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2672 next = SIMPLEQ_FIRST(&entries->head);
2674 *first_displayed_entry = next;
2675 if (++n >= maxscroll)
2677 next = SIMPLEQ_NEXT(next, entry);
2681 static const struct got_error *
2682 tree_entry_path(char **path, struct tog_parent_trees *parents,
2683 struct got_tree_entry *te)
2685 const struct got_error *err = NULL;
2686 struct tog_parent_tree *pt;
2687 size_t len = 2; /* for leading slash and NUL */
2689 TAILQ_FOREACH(pt, parents, entry)
2690 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2692 len += strlen(te->name);
2694 *path = calloc(1, len);
2696 return got_error_from_errno();
2699 pt = TAILQ_LAST(parents, tog_parent_trees);
2701 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2702 err = got_error(GOT_ERR_NO_SPACE);
2705 if (strlcat(*path, "/", len) >= len) {
2706 err = got_error(GOT_ERR_NO_SPACE);
2709 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2712 if (strlcat(*path, te->name, len) >= len) {
2713 err = got_error(GOT_ERR_NO_SPACE);
2725 static const struct got_error *
2726 blame_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2727 struct got_tree_entry *te, struct tog_parent_trees *parents,
2728 struct got_object_id *commit_id, struct got_repository *repo)
2730 const struct got_error *err = NULL;
2732 struct tog_view *blame_view;
2734 err = tree_entry_path(&path, parents, te);
2738 blame_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_BLAME);
2739 if (blame_view == NULL)
2740 return got_error_from_errno();
2742 err = open_blame_view(blame_view, path, commit_id, repo);
2744 view_close(blame_view);
2747 *new_view = blame_view;
2751 static const struct got_error *
2752 log_tree_entry(struct tog_view **new_view, struct tog_view *parent_view,
2753 struct got_tree_entry *te, struct tog_parent_trees *parents,
2754 struct got_object_id *commit_id, struct got_repository *repo)
2756 struct tog_view *log_view;
2757 const struct got_error *err = NULL;
2760 log_view = view_open(0, 0, 0, 0, parent_view, TOG_VIEW_LOG);
2761 if (log_view == NULL)
2762 return got_error_from_errno();
2764 err = tree_entry_path(&path, parents, te);
2768 err = open_log_view(log_view, commit_id, repo, path);
2770 view_close(log_view);
2772 *new_view = log_view;
2777 static const struct got_error *
2778 open_tree_view(struct tog_view *view, struct got_tree_object *root,
2779 struct got_object_id *commit_id, struct got_repository *repo)
2781 const struct got_error *err = NULL;
2782 char *commit_id_str = NULL;
2783 struct tog_tree_view_state *s = &view->state.tree;
2785 TAILQ_INIT(&s->parents);
2787 err = got_object_id_str(&commit_id_str, commit_id);
2791 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
2792 err = got_error_from_errno();
2796 s->root = s->tree = root;
2797 s->entries = got_object_tree_get_entries(root);
2798 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
2799 s->commit_id = got_object_id_dup(commit_id);
2800 if (s->commit_id == NULL) {
2801 err = got_error_from_errno();
2806 view->show = show_tree_view;
2807 view->input = input_tree_view;
2808 view->close = close_tree_view;
2810 free(commit_id_str);
2812 free(s->tree_label);
2813 s->tree_label = NULL;
2818 static const struct got_error *
2819 close_tree_view(struct tog_view *view)
2821 struct tog_tree_view_state *s = &view->state.tree;
2823 free(s->tree_label);
2824 s->tree_label = NULL;
2826 s->commit_id = NULL;
2827 while (!TAILQ_EMPTY(&s->parents)) {
2828 struct tog_parent_tree *parent;
2829 parent = TAILQ_FIRST(&s->parents);
2830 TAILQ_REMOVE(&s->parents, parent, entry);
2834 if (s->tree != s->root)
2835 got_object_tree_close(s->tree);
2836 got_object_tree_close(s->root);
2841 static const struct got_error *
2842 show_tree_view(struct tog_view *view)
2844 const struct got_error *err = NULL;
2845 struct tog_tree_view_state *s = &view->state.tree;
2848 err = tree_entry_path(&parent_path, &s->parents, NULL);
2852 err = draw_tree_entries(view, &s->first_displayed_entry,
2853 &s->last_displayed_entry, &s->selected_entry,
2854 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
2855 s->entries, s->selected, view->nlines, s->tree == s->root);
2860 static const struct got_error *
2861 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
2862 struct tog_view *view, int ch)
2864 const struct got_error *err = NULL;
2865 struct tog_tree_view_state *s = &view->state.tree;
2869 s->show_ids = !s->show_ids;
2872 if (s->selected_entry) {
2873 err = log_tree_entry(new_view, view,
2874 s->selected_entry, &s->parents,
2875 s->commit_id, s->repo);
2880 if (s->selected > 0)
2882 if (s->selected > 0)
2884 tree_scroll_up(&s->first_displayed_entry, 1,
2885 s->entries, s->tree == s->root);
2888 if (SIMPLEQ_FIRST(&s->entries->head) ==
2889 s->first_displayed_entry) {
2890 if (s->tree != s->root)
2891 s->first_displayed_entry = NULL;
2895 tree_scroll_up(&s->first_displayed_entry,
2896 view->nlines, s->entries,
2897 s->tree == s->root);
2901 if (s->selected < s->ndisplayed - 1) {
2905 tree_scroll_down(&s->first_displayed_entry, 1,
2906 s->last_displayed_entry, s->entries);
2909 tree_scroll_down(&s->first_displayed_entry,
2910 view->nlines, s->last_displayed_entry,
2912 if (SIMPLEQ_NEXT(s->last_displayed_entry,
2915 /* can't scroll any further; move cursor down */
2916 if (s->selected < s->ndisplayed - 1)
2917 s->selected = s->ndisplayed - 1;
2921 if (s->selected_entry == NULL) {
2922 struct tog_parent_tree *parent;
2924 /* user selected '..' */
2925 if (s->tree == s->root)
2927 parent = TAILQ_FIRST(&s->parents);
2928 TAILQ_REMOVE(&s->parents, parent,
2930 got_object_tree_close(s->tree);
2931 s->tree = parent->tree;
2933 got_object_tree_get_entries(s->tree);
2934 s->first_displayed_entry =
2935 parent->first_displayed_entry;
2937 parent->selected_entry;
2938 s->selected = parent->selected;
2940 } else if (S_ISDIR(s->selected_entry->mode)) {
2941 struct tog_parent_tree *parent;
2942 struct got_tree_object *child;
2943 err = got_object_open_as_tree(&child,
2944 s->repo, s->selected_entry->id);
2947 parent = calloc(1, sizeof(*parent));
2948 if (parent == NULL) {
2949 err = got_error_from_errno();
2952 parent->tree = s->tree;
2953 parent->first_displayed_entry =
2954 s->first_displayed_entry;
2955 parent->selected_entry = s->selected_entry;
2956 parent->selected = s->selected;
2957 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2960 got_object_tree_get_entries(s->tree);
2962 s->first_displayed_entry = NULL;
2963 } else if (S_ISREG(s->selected_entry->mode)) {
2964 err = blame_tree_entry(new_view, view,
2965 s->selected_entry, &s->parents,
2966 s->commit_id, s->repo);
2972 if (s->selected > view->nlines)
2973 s->selected = s->ndisplayed - 1;
2986 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
2991 static const struct got_error *
2992 cmd_tree(int argc, char *argv[])
2994 const struct got_error *error;
2995 struct got_repository *repo = NULL;
2996 char *repo_path = NULL;
2997 struct got_object_id *commit_id = NULL;
2998 char *commit_id_arg = NULL;
2999 struct got_commit_object *commit = NULL;
3000 struct got_tree_object *tree = NULL;
3002 struct tog_view *view;
3005 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3010 while ((ch = getopt(argc, argv, "c:")) != -1) {
3013 commit_id_arg = optarg;
3025 repo_path = getcwd(NULL, 0);
3026 if (repo_path == NULL)
3027 return got_error_from_errno();
3028 } else if (argc == 1) {
3029 repo_path = realpath(argv[0], NULL);
3030 if (repo_path == NULL)
3031 return got_error_from_errno();
3035 error = got_repo_open(&repo, repo_path);
3040 if (commit_id_arg == NULL) {
3041 error = get_head_commit_id(&commit_id, repo);
3045 struct got_object *obj;
3046 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3047 if (error == NULL) {
3048 commit_id = got_object_id_dup(got_object_get_id(obj));
3049 if (commit_id == NULL)
3050 error = got_error_from_errno();
3056 error = got_object_open_as_commit(&commit, repo, commit_id);
3060 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3064 view = view_open(0, 0, 0, 0, NULL, TOG_VIEW_TREE);
3066 error = got_error_from_errno();
3069 error = open_tree_view(view, tree, commit_id, repo);
3072 error = view_loop(view);
3076 got_object_commit_close(commit);
3078 got_object_tree_close(tree);
3080 got_repo_close(repo);
3090 intrflush(stdscr, FALSE);
3091 keypad(stdscr, TRUE);
3100 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3101 "Available commands:\n", getprogname());
3102 for (i = 0; i < nitems(tog_commands); i++) {
3103 struct tog_cmd *cmd = &tog_commands[i];
3104 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3110 make_argv(const char *arg0, const char *arg1)
3113 int argc = (arg1 == NULL ? 1 : 2);
3115 argv = calloc(argc, sizeof(char *));
3118 argv[0] = strdup(arg0);
3119 if (argv[0] == NULL)
3122 argv[1] = strdup(arg1);
3123 if (argv[1] == NULL)
3131 main(int argc, char *argv[])
3133 const struct got_error *error = NULL;
3134 struct tog_cmd *cmd = NULL;
3136 char **cmd_argv = NULL;
3138 setlocale(LC_ALL, "");
3140 while ((ch = getopt(argc, argv, "h")) != -1) {
3159 /* Build an argument vector which runs a default command. */
3160 cmd = &tog_commands[0];
3161 cmd_argv = make_argv(cmd->name, NULL);
3166 /* Did the user specific a command? */
3167 for (i = 0; i < nitems(tog_commands); i++) {
3168 if (strncmp(tog_commands[i].name, argv[0],
3169 strlen(argv[0])) == 0) {
3170 cmd = &tog_commands[i];
3172 tog_commands[i].cmd_usage();
3177 /* Did the user specify a repository? */
3178 char *repo_path = realpath(argv[0], NULL);
3180 struct got_repository *repo;
3181 error = got_repo_open(&repo, repo_path);
3183 got_repo_close(repo);
3185 error = got_error_from_errno();
3188 fprintf(stderr, "%s: '%s' is not a "
3189 "known command\n", getprogname(),
3193 fprintf(stderr, "%s: '%s' is neither a known "
3194 "command nor a path to a repository\n",
3195 getprogname(), argv[0]);
3199 cmd = &tog_commands[0];
3200 cmd_argv = make_argv(cmd->name, repo_path);
3208 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3215 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);