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 MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 const struct got_error *(*cmd_main)(int, char *[]);
65 void (*cmd_usage)(void);
69 __dead static void usage(void);
70 __dead static void usage_log(void);
71 __dead static void usage_diff(void);
72 __dead static void usage_blame(void);
73 __dead static void usage_tree(void);
75 static const struct got_error* cmd_log(int, char *[]);
76 static const struct got_error* cmd_diff(int, char *[]);
77 static const struct got_error* cmd_blame(int, char *[]);
78 static const struct got_error* cmd_tree(int, char *[]);
80 static struct tog_cmd tog_commands[] = {
81 { "log", cmd_log, usage_log,
82 "show repository history" },
83 { "diff", cmd_diff, usage_diff,
84 "compare files and directories" },
85 { "blame", cmd_blame, usage_blame,
86 "show line-by-line file history" },
87 { "tree", cmd_tree, usage_tree,
88 "browse trees in repository" },
98 struct tog_diff_view_state {
99 struct got_object_id *id1, *id2;
101 int first_displayed_line;
102 int last_displayed_line;
105 struct got_repository *repo;
108 struct commit_queue_entry {
109 TAILQ_ENTRY(commit_queue_entry) entry;
110 struct got_object_id *id;
111 struct got_commit_object *commit;
114 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
115 struct commit_queue {
117 struct commit_queue_head head;
120 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
122 struct tog_log_thread_args {
123 pthread_cond_t need_commits;
125 struct got_commit_graph *graph;
126 struct commit_queue *commits;
127 const char *in_repo_path;
128 struct got_object_id *start_id;
129 struct got_repository *repo;
132 struct tog_view *view;
133 struct commit_queue_entry **first_displayed_entry;
134 struct commit_queue_entry **last_displayed_entry;
135 struct commit_queue_entry **selected_entry;
139 struct tog_log_view_state {
140 struct commit_queue commits;
141 struct commit_queue_entry *first_displayed_entry;
142 struct commit_queue_entry *last_displayed_entry;
143 struct commit_queue_entry *selected_entry;
146 struct got_repository *repo;
147 struct got_object_id *start_id;
150 struct tog_log_thread_args thread_args;
153 struct tog_blame_cb_args {
154 struct tog_blame_line *lines; /* one per line */
157 struct tog_view *view;
158 struct got_object_id *commit_id;
161 int *first_displayed_line;
162 int *last_displayed_line;
168 struct tog_blame_thread_args {
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
178 struct tog_blame_line *lines;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
196 struct got_repository *repo;
197 struct got_object_id *commit_id;
198 struct tog_blame blame;
201 struct tog_parent_tree {
202 TAILQ_ENTRY(tog_parent_tree) entry;
203 struct got_tree_object *tree;
204 struct got_tree_entry *first_displayed_entry;
205 struct got_tree_entry *selected_entry;
209 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
211 struct tog_tree_view_state {
213 struct got_tree_object *root;
214 struct got_tree_object *tree;
215 const struct got_tree_entries *entries;
216 struct got_tree_entry *first_displayed_entry;
217 struct got_tree_entry *last_displayed_entry;
218 struct got_tree_entry *selected_entry;
219 int nentries, ndisplayed, selected, show_ids;
220 struct tog_parent_trees parents;
221 struct got_object_id *commit_id;
222 struct got_repository *repo;
226 * We implement two types of views: parent views and child views.
228 * The 'Tab' key switches between a parent view and its child view.
229 * Child views are shown side-by-side to their parent view, provided
230 * there is enough screen estate.
232 * When a new view is opened from within a parent view, this new view
233 * becomes a child view of the parent view, replacing any existing child.
235 * When a new view is opened from within a child view, this new view
236 * becomes a parent view which will obscure the views below until the
237 * user quits the new parent view by typing 'q'.
239 * This list of views contains parent views only.
240 * Child views are only pointed to by their parent view.
242 TAILQ_HEAD(tog_view_list_head, tog_view);
245 TAILQ_ENTRY(tog_view) entry;
248 int nlines, ncols, begin_y, begin_x;
249 int lines, cols; /* copies of LINES and COLS */
251 struct tog_view *parent;
252 struct tog_view *child;
255 /* type-specific state */
256 enum tog_view_type type;
258 struct tog_diff_view_state diff;
259 struct tog_log_view_state log;
260 struct tog_blame_view_state blame;
261 struct tog_tree_view_state tree;
264 const struct got_error *(*show)(struct tog_view *);
265 const struct got_error *(*input)(struct tog_view **,
266 struct tog_view **, struct tog_view**, struct tog_view *, int);
267 const struct got_error *(*close)(struct tog_view *);
270 static const struct got_error *open_diff_view(struct tog_view *,
271 struct got_object *, struct got_object *, struct got_repository *);
272 static const struct got_error *show_diff_view(struct tog_view *);
273 static const struct got_error *input_diff_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error* close_diff_view(struct tog_view *);
277 static const struct got_error *open_log_view(struct tog_view *,
278 struct got_object_id *, struct got_repository *, const char *, int);
279 static const struct got_error * show_log_view(struct tog_view *);
280 static const struct got_error *input_log_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_log_view(struct tog_view *);
284 static const struct got_error *open_blame_view(struct tog_view *, char *,
285 struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_blame_view(struct tog_view *);
287 static const struct got_error *input_blame_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_blame_view(struct tog_view *);
291 static const struct got_error *open_tree_view(struct tog_view *,
292 struct got_tree_object *, struct got_object_id *, struct got_repository *);
293 static const struct got_error *show_tree_view(struct tog_view *);
294 static const struct got_error *input_tree_view(struct tog_view **,
295 struct tog_view **, struct tog_view **, struct tog_view *, int);
296 static const struct got_error *close_tree_view(struct tog_view *);
298 static const struct got_error *
299 view_close(struct tog_view *view)
301 const struct got_error *err = NULL;
304 view_close(view->child);
308 err = view->close(view);
310 del_panel(view->panel);
312 delwin(view->window);
317 static struct tog_view *
318 view_open(int nlines, int ncols, int begin_y, int begin_x,
319 enum tog_view_type type)
321 struct tog_view *view = calloc(1, sizeof(*view));
329 view->nlines = nlines ? nlines : LINES - begin_y;
330 view->ncols = ncols ? ncols : COLS - begin_x;
331 view->begin_y = begin_y;
332 view->begin_x = begin_x;
333 view->window = newwin(nlines, ncols, begin_y, begin_x);
334 if (view->window == NULL) {
338 view->panel = new_panel(view->window);
339 if (view->panel == NULL ||
340 set_panel_userptr(view->panel, view) != OK) {
345 keypad(view->window, TRUE);
350 view_split_begin_x(int begin_x)
352 if (begin_x > 0 || COLS < 120)
354 return (COLS - MAX(COLS / 2, 80));
357 static const struct got_error *view_resize(struct tog_view *);
359 static const struct got_error *
360 view_splitscreen(struct tog_view *view)
362 const struct got_error *err = NULL;
365 view->begin_x = view_split_begin_x(0);
366 view->nlines = LINES;
367 view->ncols = COLS - view->begin_x;
370 err = view_resize(view);
374 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
375 return got_error_from_errno();
380 static const struct got_error *
381 view_fullscreen(struct tog_view *view)
383 const struct got_error *err = NULL;
387 view->nlines = LINES;
391 err = view_resize(view);
395 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
396 return got_error_from_errno();
402 view_is_parent_view(struct tog_view *view)
404 return view->parent == NULL;
407 static const struct got_error *
408 view_resize(struct tog_view *view)
412 if (view->lines > LINES)
413 nlines = view->nlines - (view->lines - LINES);
415 nlines = view->nlines + (LINES - view->lines);
417 if (view->cols > COLS)
418 ncols = view->ncols - (view->cols - COLS);
420 ncols = view->ncols + (COLS - view->cols);
422 if (wresize(view->window, nlines, ncols) == ERR)
423 return got_error_from_errno();
424 replace_panel(view->panel, view->window);
426 view->nlines = nlines;
432 view->child->begin_x = view_split_begin_x(view->begin_x);
433 if (view->child->begin_x == 0) {
434 view_fullscreen(view->child);
435 if (view->child->focussed)
436 show_panel(view->child->panel);
438 show_panel(view->panel);
440 view_splitscreen(view->child);
441 show_panel(view->child->panel);
448 static const struct got_error *
449 view_close_child(struct tog_view *view)
451 const struct got_error *err = NULL;
453 if (view->child == NULL)
456 err = view_close(view->child);
461 static const struct got_error *
462 view_set_child(struct tog_view *view, struct tog_view *child)
464 const struct got_error *err = NULL;
467 child->parent = view;
472 view_is_splitscreen(struct tog_view *view)
474 return !view_is_parent_view(view) && view->begin_x > 0;
477 static const struct got_error *
478 view_input(struct tog_view **new, struct tog_view **dead,
479 struct tog_view **focus, int *done, struct tog_view *view,
480 struct tog_view_list_head *views)
482 const struct got_error *err = NULL;
490 nodelay(stdscr, FALSE);
491 /* Allow threads to make progress while we are waiting for input. */
492 errcode = pthread_mutex_unlock(&tog_mutex);
494 return got_error_set_errno(errcode);
495 ch = wgetch(view->window);
496 errcode = pthread_mutex_lock(&tog_mutex);
498 return got_error_set_errno(errcode);
499 nodelay(stdscr, TRUE);
505 *focus = view->child;
506 view->child_focussed = 1;
507 } else if (view->parent) {
508 *focus = view->parent;
509 view->parent->child_focussed = 0;
513 err = view->input(new, dead, focus, view, ch);
520 if (view_is_parent_view(view)) {
521 if (view->child == NULL)
523 if (view_is_splitscreen(view->child)) {
524 *focus = view->child;
525 view->child_focussed = 1;
526 err = view_fullscreen(view->child);
528 err = view_splitscreen(view->child);
531 err = view->child->input(new, dead, focus,
532 view->child, KEY_RESIZE);
534 if (view_is_splitscreen(view)) {
536 view->parent->child_focussed = 1;
537 err = view_fullscreen(view);
539 err = view_splitscreen(view);
543 err = view->input(new, dead, focus, view,
548 TAILQ_FOREACH(v, views, entry) {
549 err = view_resize(v);
552 err = v->input(new, dead, focus, v, ch);
558 err = view->input(new, dead, focus, view, ch);
566 view_vborder(struct tog_view *view)
569 struct tog_view *view_above;
572 return view_vborder(view->parent);
574 panel = panel_above(view->panel);
578 view_above = panel_userptr(panel);
579 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
580 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
584 view_needs_focus_indication(struct tog_view *view)
586 if (view_is_parent_view(view)) {
587 if (view->child == NULL || view->child_focussed)
589 if (!view_is_splitscreen(view->child))
591 } else if (!view_is_splitscreen(view))
594 return view->focussed;
597 static const struct got_error *
598 view_loop(struct tog_view *view)
600 const struct got_error *err = NULL;
601 struct tog_view_list_head views;
602 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
603 int fast_refresh = 10;
604 int done = 0, errcode;
606 errcode = pthread_mutex_lock(&tog_mutex);
608 return got_error_set_errno(errcode);
611 TAILQ_INSERT_HEAD(&views, view, entry);
615 err = view->show(view);
620 while (!TAILQ_EMPTY(&views) && !done) {
621 /* Refresh fast during initialization, then become slower. */
622 if (fast_refresh && fast_refresh-- == 0)
623 halfdelay(10); /* switch to once per second */
625 err = view_input(&new_view, &dead_view, &focus_view, &done,
630 struct tog_view *prev = NULL;
632 if (view_is_parent_view(dead_view))
633 prev = TAILQ_PREV(dead_view,
634 tog_view_list_head, entry);
638 if (dead_view->parent)
639 dead_view->parent->child = NULL;
641 TAILQ_REMOVE(&views, dead_view, entry);
643 err = view_close(dead_view);
644 if (err || dead_view == main_view)
647 if (view == dead_view) {
652 else if (!TAILQ_EMPTY(&views))
653 view = TAILQ_LAST(&views,
658 if (view->child && view->child_focussed)
659 focus_view = view->child;
666 struct tog_view *v, *t;
667 /* Only allow one parent view per type. */
668 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
669 if (v->type != new_view->type)
671 TAILQ_REMOVE(&views, v, entry);
679 TAILQ_INSERT_TAIL(&views, new_view, entry);
680 if (focus_view == NULL)
681 focus_view = new_view;
684 show_panel(focus_view->panel);
687 focus_view->focussed = 1;
690 show_panel(new_view->panel);
691 if (view->child && view_is_splitscreen(view->child))
692 show_panel(view->child->panel);
695 if (focus_view == NULL) {
697 show_panel(view->panel);
698 if (view->child && view_is_splitscreen(view->child))
699 show_panel(view->child->panel);
703 err = view->parent->show(view->parent);
707 err = view->show(view);
711 err = view->child->show(view->child);
720 while (!TAILQ_EMPTY(&views)) {
721 view = TAILQ_FIRST(&views);
722 TAILQ_REMOVE(&views, view, entry);
726 errcode = pthread_mutex_unlock(&tog_mutex);
728 return got_error_set_errno(errcode);
738 "usage: %s log [-c commit] [-r repository-path] [path]\n",
743 /* Create newly allocated wide-character string equivalent to a byte string. */
744 static const struct got_error *
745 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
748 const struct got_error *err = NULL;
751 *wlen = mbstowcs(NULL, s, 0);
752 if (*wlen == (size_t)-1) {
755 return got_error_from_errno();
757 /* byte string invalid in current encoding; try to "fix" it */
758 err = got_mbsavis(&vis, &vislen, s);
761 *wlen = mbstowcs(NULL, vis, 0);
762 if (*wlen == (size_t)-1) {
763 err = got_error_from_errno(); /* give up */
768 *ws = calloc(*wlen + 1, sizeof(*ws));
770 err = got_error_from_errno();
774 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
775 err = got_error_from_errno();
786 /* Format a line for display, ensuring that it won't overflow a width limit. */
787 static const struct got_error *
788 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
790 const struct got_error *err = NULL;
792 wchar_t *wline = NULL;
799 err = mbs2ws(&wline, &wlen, line);
804 while (i < wlen && cols < wlimit) {
805 int width = wcwidth(wline[i]);
812 if (cols + width <= wlimit)
817 if (wline[i] == L'\t')
818 cols += TABSIZE - ((cols + 1) % TABSIZE);
822 err = got_error_from_errno();
837 static const struct got_error *
838 draw_commit(struct tog_view *view, struct got_commit_object *commit,
839 struct got_object_id *id)
841 const struct got_error *err = NULL;
842 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
843 char *logmsg0 = NULL, *logmsg = NULL;
844 char *author0 = NULL, *author = NULL;
845 wchar_t *wlogmsg = NULL, *wauthor = NULL;
846 int author_width, logmsg_width;
847 char *newline, *smallerthan;
850 static const size_t date_display_cols = 9;
851 static const size_t author_display_cols = 16;
852 const int avail = view->ncols;
854 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
855 &commit->tm_committer) >= sizeof(datebuf))
856 return got_error(GOT_ERR_NO_SPACE);
858 if (avail < date_display_cols)
859 limit = MIN(sizeof(datebuf) - 1, avail);
861 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
862 waddnstr(view->window, datebuf, limit);
867 author0 = strdup(commit->author);
868 if (author0 == NULL) {
869 err = got_error_from_errno();
873 smallerthan = strchr(author, '<');
877 char *at = strchr(author, '@');
882 err = format_line(&wauthor, &author_width, author, limit);
885 waddwstr(view->window, wauthor);
887 while (col <= avail && author_width < author_display_cols + 1) {
888 waddch(view->window, ' ');
895 logmsg0 = strdup(commit->logmsg);
896 if (logmsg0 == NULL) {
897 err = got_error_from_errno();
901 while (*logmsg == '\n')
903 newline = strchr(logmsg, '\n');
907 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
910 waddwstr(view->window, wlogmsg);
912 while (col <= avail) {
913 waddch(view->window, ' ');
925 static struct commit_queue_entry *
926 alloc_commit_queue_entry(struct got_commit_object *commit,
927 struct got_object_id *id)
929 struct commit_queue_entry *entry;
931 entry = calloc(1, sizeof(*entry));
936 entry->commit = commit;
941 pop_commit(struct commit_queue *commits)
943 struct commit_queue_entry *entry;
945 entry = TAILQ_FIRST(&commits->head);
946 TAILQ_REMOVE(&commits->head, entry, entry);
947 got_object_commit_close(entry->commit);
949 /* Don't free entry->id! It is owned by the commit graph. */
954 free_commits(struct commit_queue *commits)
956 while (!TAILQ_EMPTY(&commits->head))
960 static const struct got_error *
961 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
962 int minqueue, struct got_repository *repo, const char *path)
964 const struct got_error *err = NULL;
968 * We keep all commits open throughout the lifetime of the log
969 * view in order to avoid having to re-fetch commits from disk
970 * while updating the display.
972 while (nqueued < minqueue) {
973 struct got_object_id *id;
974 struct got_commit_object *commit;
975 struct commit_queue_entry *entry;
978 err = got_commit_graph_iter_next(&id, graph);
980 if (err->code != GOT_ERR_ITER_NEED_MORE)
982 err = got_commit_graph_fetch_commits(graph,
992 err = got_object_open_as_commit(&commit, repo, id);
995 entry = alloc_commit_queue_entry(commit, id);
997 err = got_error_from_errno();
1001 errcode = pthread_mutex_lock(&tog_mutex);
1003 err = got_error_set_errno(errcode);
1007 entry->idx = commits->ncommits;
1008 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1010 commits->ncommits++;
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode);
1020 static const struct got_error *
1021 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1023 const struct got_error *err = NULL;
1024 struct got_reference *head_ref;
1028 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1032 err = got_ref_resolve(head_id, repo, head_ref);
1033 got_ref_close(head_ref);
1042 static const struct got_error *
1043 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1044 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1045 struct commit_queue *commits, int selected_idx, int limit,
1046 const char *path, int commits_needed)
1048 const struct got_error *err = NULL;
1049 struct commit_queue_entry *entry;
1050 int ncommits, width;
1051 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1057 if (ncommits == selected_idx) {
1061 entry = TAILQ_NEXT(entry, entry);
1066 err = got_object_id_str(&id_str, (*selected)->id);
1071 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1072 entry ? entry->idx + 1 : 0, commits->ncommits,
1073 commits_needed == 0 ? "" : " loading...") == -1)
1074 return got_error_from_errno();
1076 if (path && strcmp(path, "/") != 0) {
1077 if (asprintf(&header, "commit: %s %s%s",
1078 id_str ? id_str : "........................................",
1079 path, ncommits_str) == -1) {
1080 err = got_error_from_errno();
1084 } else if (asprintf(&header, "commit: %s%s",
1085 id_str ? id_str : "........................................",
1086 ncommits_str) == -1) {
1087 err = got_error_from_errno();
1091 err = format_line(&wline, &width, header, view->ncols);
1095 werase(view->window);
1097 if (view_needs_focus_indication(view))
1098 wstandout(view->window);
1099 waddwstr(view->window, wline);
1100 while (width < view->ncols) {
1101 waddch(view->window, ' ');
1104 if (view_needs_focus_indication(view))
1105 wstandend(view->window);
1114 if (ncommits >= limit - 1)
1116 if (view->focussed && ncommits == selected_idx)
1117 wstandout(view->window);
1118 err = draw_commit(view, entry->commit, entry->id);
1119 if (view->focussed && ncommits == selected_idx)
1120 wstandend(view->window);
1125 entry = TAILQ_NEXT(entry, entry);
1137 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1138 struct commit_queue *commits)
1140 struct commit_queue_entry *entry;
1143 entry = TAILQ_FIRST(&commits->head);
1144 if (*first_displayed_entry == entry)
1147 entry = *first_displayed_entry;
1148 while (entry && nscrolled < maxscroll) {
1149 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1151 *first_displayed_entry = entry;
1157 static const struct got_error *
1158 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1159 struct commit_queue_entry **last_displayed_entry,
1160 struct commit_queue *commits, int *log_complete, int *commits_needed,
1161 pthread_cond_t *need_commits)
1163 const struct got_error *err = NULL;
1164 struct commit_queue_entry *pentry;
1167 if (*last_displayed_entry == NULL)
1171 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1172 if (pentry == NULL) {
1176 *commits_needed = maxscroll + 20;
1177 errcode = pthread_cond_signal(need_commits);
1179 return got_error_set_errno(errcode);
1182 *last_displayed_entry = pentry;
1184 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1187 *first_displayed_entry = pentry;
1188 } while (++nscrolled < maxscroll);
1193 static const struct got_error *
1194 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1195 struct got_object_id *commit_id, struct got_commit_object *commit,
1196 struct got_repository *repo)
1198 const struct got_error *err;
1199 struct got_object *obj1 = NULL, *obj2 = NULL;
1200 struct got_object_qid *parent_id;
1201 struct tog_view *diff_view;
1203 err = got_object_open(&obj2, repo, commit_id);
1207 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1209 err = got_object_open(&obj1, repo, parent_id->id);
1214 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1215 if (diff_view == NULL) {
1216 err = got_error_from_errno();
1220 err = open_diff_view(diff_view, obj1, obj2, repo);
1222 *new_view = diff_view;
1225 got_object_close(obj1);
1227 got_object_close(obj2);
1231 static const struct got_error *
1232 browse_commit(struct tog_view **new_view, int begin_x,
1233 struct commit_queue_entry *entry, struct got_repository *repo)
1235 const struct got_error *err = NULL;
1236 struct got_tree_object *tree;
1237 struct tog_view *tree_view;
1239 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1243 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1244 if (tree_view == NULL)
1245 return got_error_from_errno();
1247 err = open_tree_view(tree_view, tree, entry->id, repo);
1249 got_object_tree_close(tree);
1251 *new_view = tree_view;
1256 log_thread(void *arg)
1258 const struct got_error *err = NULL;
1260 struct tog_log_thread_args *a = arg;
1263 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1267 while (!done && !err) {
1268 err = queue_commits(a->graph, a->commits, 1, a->repo,
1271 if (err->code != GOT_ERR_ITER_COMPLETED)
1275 } else if (a->commits_needed > 0)
1276 a->commits_needed--;
1278 errcode = pthread_mutex_lock(&tog_mutex);
1280 return (void *)got_error_set_errno(errcode);
1283 a->log_complete = 1;
1284 else if (*a->quit) {
1286 a->log_complete = 1;
1287 } else if (*a->first_displayed_entry == NULL) {
1288 *a->first_displayed_entry =
1289 TAILQ_FIRST(&a->commits->head);
1290 *a->selected_entry = *a->first_displayed_entry;
1293 err = draw_commits(a->view, a->last_displayed_entry,
1294 a->selected_entry, *a->first_displayed_entry,
1295 a->commits, *a->selected, a->view->nlines,
1296 a->in_repo_path, a->commits_needed);
1299 a->commits_needed = 0;
1300 else if (a->commits_needed == 0) {
1301 errcode = pthread_cond_wait(&a->need_commits,
1304 err = got_error_set_errno(errcode);
1307 errcode = pthread_mutex_unlock(&tog_mutex);
1308 if (errcode && err == NULL)
1309 err = got_error_set_errno(errcode);
1314 static const struct got_error *
1315 stop_log_thread(struct tog_log_view_state *s)
1317 const struct got_error *err = NULL;
1322 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1324 return got_error_set_errno(errcode);
1325 errcode = pthread_mutex_unlock(&tog_mutex);
1327 return got_error_set_errno(errcode);
1328 errcode = pthread_join(s->thread, (void **)&err);
1330 return got_error_set_errno(errcode);
1331 errcode = pthread_mutex_lock(&tog_mutex);
1333 return got_error_set_errno(errcode);
1337 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1338 if (errcode && err == NULL)
1339 err = got_error_set_errno(errcode);
1341 if (s->thread_args.repo) {
1342 got_repo_close(s->thread_args.repo);
1343 s->thread_args.repo = NULL;
1346 if (s->thread_args.graph) {
1347 got_commit_graph_close(s->thread_args.graph);
1348 s->thread_args.graph = NULL;
1354 static const struct got_error *
1355 close_log_view(struct tog_view *view)
1357 const struct got_error *err = NULL;
1358 struct tog_log_view_state *s = &view->state.log;
1360 err = stop_log_thread(s);
1361 free_commits(&s->commits);
1362 free(s->in_repo_path);
1363 s->in_repo_path = NULL;
1369 static const struct got_error *
1370 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1371 struct got_repository *repo, const char *path, int check_disk)
1373 const struct got_error *err = NULL;
1374 struct tog_log_view_state *s = &view->state.log;
1375 struct got_repository *thread_repo = NULL;
1376 struct got_commit_graph *thread_graph = NULL;
1379 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1383 /* The commit queue only contains commits being displayed. */
1384 TAILQ_INIT(&s->commits.head);
1385 s->commits.ncommits = 0;
1388 s->start_id = got_object_id_dup(start_id);
1389 if (s->start_id == NULL) {
1390 err = got_error_from_errno();
1394 view->show = show_log_view;
1395 view->input = input_log_view;
1396 view->close = close_log_view;
1398 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1401 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1406 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1408 err = got_error_set_errno(errcode);
1412 s->thread_args.commits_needed = view->nlines;
1413 s->thread_args.graph = thread_graph;
1414 s->thread_args.commits = &s->commits;
1415 s->thread_args.in_repo_path = s->in_repo_path;
1416 s->thread_args.start_id = s->start_id;
1417 s->thread_args.repo = thread_repo;
1418 s->thread_args.log_complete = 0;
1419 s->thread_args.quit = &s->quit;
1420 s->thread_args.view = view;
1421 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1422 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1423 s->thread_args.selected_entry = &s->selected_entry;
1424 s->thread_args.selected = &s->selected;
1426 errcode = pthread_create(&s->thread, NULL, log_thread,
1429 err = got_error_set_errno(errcode);
1435 close_log_view(view);
1439 static const struct got_error *
1440 show_log_view(struct tog_view *view)
1442 struct tog_log_view_state *s = &view->state.log;
1444 return draw_commits(view, &s->last_displayed_entry,
1445 &s->selected_entry, s->first_displayed_entry,
1446 &s->commits, s->selected, view->nlines,
1447 s->in_repo_path, s->thread_args.commits_needed);
1450 static const struct got_error *
1451 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1452 struct tog_view **focus_view, struct tog_view *view, int ch)
1454 const struct got_error *err = NULL;
1455 struct tog_log_view_state *s = &view->state.log;
1457 struct tog_view *diff_view = NULL, *tree_view = NULL;
1466 if (s->selected > 0)
1468 if (s->selected > 0)
1470 scroll_up(&s->first_displayed_entry, 1,
1474 if (TAILQ_FIRST(&s->commits.head) ==
1475 s->first_displayed_entry) {
1479 scroll_up(&s->first_displayed_entry,
1480 view->nlines, &s->commits);
1484 if (s->selected < MIN(view->nlines - 2,
1485 s->commits.ncommits - 1)) {
1489 err = scroll_down(&s->first_displayed_entry, 1,
1490 &s->last_displayed_entry, &s->commits,
1491 &s->thread_args.log_complete,
1492 &s->thread_args.commits_needed,
1493 &s->thread_args.need_commits);
1496 struct commit_queue_entry *first;
1497 first = s->first_displayed_entry;
1498 err = scroll_down(&s->first_displayed_entry,
1499 view->nlines, &s->last_displayed_entry,
1500 &s->commits, &s->thread_args.log_complete,
1501 &s->thread_args.commits_needed,
1502 &s->thread_args.need_commits);
1503 if (first == s->first_displayed_entry &&
1504 s->selected < MIN(view->nlines - 2,
1505 s->commits.ncommits - 1)) {
1506 /* can't scroll further down */
1507 s->selected = MIN(view->nlines - 2,
1508 s->commits.ncommits - 1);
1514 if (s->selected > view->nlines - 2)
1515 s->selected = view->nlines - 2;
1516 if (s->selected > s->commits.ncommits - 1)
1517 s->selected = s->commits.ncommits - 1;
1521 if (view_is_parent_view(view))
1522 begin_x = view_split_begin_x(view->begin_x);
1523 err = open_diff_view_for_commit(&diff_view, begin_x,
1524 s->selected_entry->id, s->selected_entry->commit,
1528 if (view_is_parent_view(view)) {
1529 err = view_close_child(view);
1532 err = view_set_child(view, diff_view);
1534 view_close(diff_view);
1537 if (!view_is_splitscreen(diff_view)) {
1538 *focus_view = diff_view;
1539 view->child_focussed = 1;
1542 *new_view = diff_view;
1545 if (view_is_parent_view(view))
1546 begin_x = view_split_begin_x(view->begin_x);
1547 err = browse_commit(&tree_view, begin_x,
1548 s->selected_entry, s->repo);
1549 if (view_is_parent_view(view)) {
1550 err = view_close_child(view);
1553 err = view_set_child(view, tree_view);
1555 view_close(tree_view);
1558 *focus_view = tree_view;
1559 view->child_focussed = 1;
1561 *new_view = tree_view;
1564 if (strcmp(s->in_repo_path, "/") == 0)
1566 parent_path = dirname(s->in_repo_path);
1567 if (parent_path && strcmp(parent_path, ".") != 0) {
1568 struct tog_view *lv;
1569 err = stop_log_thread(s);
1572 lv = view_open(view->nlines, view->ncols,
1573 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1575 return got_error_from_errno();
1576 err = open_log_view(lv, s->start_id, s->repo,
1580 if (view_is_parent_view(view))
1583 view_set_child(view->parent, lv);
1596 static const struct got_error *
1597 cmd_log(int argc, char *argv[])
1599 const struct got_error *error;
1600 struct got_repository *repo = NULL;
1601 struct got_object_id *start_id = NULL;
1602 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1603 char *start_commit = NULL;
1605 struct tog_view *view;
1608 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1613 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1616 start_commit = optarg;
1619 repo_path = realpath(optarg, NULL);
1620 if (repo_path == NULL)
1621 err(1, "-r option");
1635 path = strdup(argv[0]);
1639 return got_error_from_errno();
1641 cwd = getcwd(NULL, 0);
1643 error = got_error_from_errno();
1646 if (repo_path == NULL) {
1647 repo_path = strdup(cwd);
1648 if (repo_path == NULL) {
1649 error = got_error_from_errno();
1654 error = got_repo_open(&repo, repo_path);
1658 if (start_commit == NULL) {
1659 error = get_head_commit_id(&start_id, repo);
1663 struct got_object *obj;
1664 error = got_object_open_by_id_str(&obj, repo, start_commit);
1665 if (error == NULL) {
1666 start_id = got_object_id_dup(got_object_get_id(obj));
1667 if (start_id == NULL)
1668 error = got_error_from_errno();
1675 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1677 error = got_error_from_errno();
1680 error = open_log_view(view, start_id, repo, path, 1);
1683 error = view_loop(view);
1690 got_repo_close(repo);
1698 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1704 parse_next_line(FILE *f, size_t *len)
1709 const char delim[3] = { '\0', '\0', '\0'};
1711 line = fparseln(f, &linelen, &lineno, delim, 0);
1717 static const struct got_error *
1718 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1719 int *last_displayed_line, int *eof, int max_lines,
1722 const struct got_error *err;
1723 int nlines = 0, nprinted = 0;
1730 werase(view->window);
1733 err = format_line(&wline, &width, header, view->ncols);
1738 if (view_needs_focus_indication(view))
1739 wstandout(view->window);
1740 waddwstr(view->window, wline);
1741 if (view_needs_focus_indication(view))
1742 wstandend(view->window);
1743 if (width < view->ncols)
1744 waddch(view->window, '\n');
1752 while (nprinted < max_lines) {
1753 line = parse_next_line(f, &len);
1758 if (++nlines < *first_displayed_line) {
1763 err = format_line(&wline, &width, line, view->ncols);
1768 waddwstr(view->window, wline);
1769 if (width < view->ncols)
1770 waddch(view->window, '\n');
1771 if (++nprinted == 1)
1772 *first_displayed_line = nlines;
1777 *last_displayed_line = nlines;
1784 static const struct got_error *
1785 create_diff(struct tog_diff_view_state *s)
1787 const struct got_error *err = NULL;
1788 struct got_object *obj1 = NULL, *obj2 = NULL;
1792 err = got_object_open(&obj1, s->repo, s->id1);
1797 err = got_object_open(&obj2, s->repo, s->id2);
1803 err = got_error_from_errno();
1810 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1811 case GOT_OBJ_TYPE_BLOB:
1812 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1813 s->diff_context, s->repo, f);
1815 case GOT_OBJ_TYPE_TREE:
1816 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1817 s->diff_context, s->repo, f);
1819 case GOT_OBJ_TYPE_COMMIT:
1820 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1824 err = got_error(GOT_ERR_OBJ_TYPE);
1829 got_object_close(obj1);
1830 got_object_close(obj2);
1836 static const struct got_error *
1837 open_diff_view(struct tog_view *view, struct got_object *obj1,
1838 struct got_object *obj2, struct got_repository *repo)
1840 const struct got_error *err;
1842 if (obj1 != NULL && obj2 != NULL &&
1843 got_object_get_type(obj1) != got_object_get_type(obj2))
1844 return got_error(GOT_ERR_OBJ_TYPE);
1847 struct got_object_id *id1;
1848 id1 = got_object_id_dup(got_object_get_id(obj1));
1850 return got_error_from_errno();
1851 view->state.diff.id1 = id1;
1853 view->state.diff.id1 = NULL;
1855 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1856 if (view->state.diff.id2 == NULL) {
1857 free(view->state.diff.id1);
1858 view->state.diff.id1 = NULL;
1859 return got_error_from_errno();
1861 view->state.diff.f = NULL;
1862 view->state.diff.first_displayed_line = 1;
1863 view->state.diff.last_displayed_line = view->nlines;
1864 view->state.diff.diff_context = 3;
1865 view->state.diff.repo = repo;
1867 err = create_diff(&view->state.diff);
1869 free(view->state.diff.id1);
1870 view->state.diff.id1 = NULL;
1871 free(view->state.diff.id2);
1872 view->state.diff.id2 = NULL;
1876 view->show = show_diff_view;
1877 view->input = input_diff_view;
1878 view->close = close_diff_view;
1883 static const struct got_error *
1884 close_diff_view(struct tog_view *view)
1886 const struct got_error *err = NULL;
1888 free(view->state.diff.id1);
1889 view->state.diff.id1 = NULL;
1890 free(view->state.diff.id2);
1891 view->state.diff.id2 = NULL;
1892 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1893 err = got_error_from_errno();
1897 static const struct got_error *
1898 show_diff_view(struct tog_view *view)
1900 const struct got_error *err;
1901 struct tog_diff_view_state *s = &view->state.diff;
1902 char *id_str1 = NULL, *id_str2, *header;
1905 err = got_object_id_str(&id_str1, s->id1);
1909 err = got_object_id_str(&id_str2, s->id2);
1913 if (asprintf(&header, "diff: %s %s",
1914 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1915 err = got_error_from_errno();
1923 return draw_file(view, s->f, &s->first_displayed_line,
1924 &s->last_displayed_line, &s->eof, view->nlines,
1928 static const struct got_error *
1929 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1930 struct tog_view **focus_view, struct tog_view *view, int ch)
1932 const struct got_error *err = NULL;
1933 struct tog_diff_view_state *s = &view->state.diff;
1939 if (s->first_displayed_line > 1)
1940 s->first_displayed_line--;
1944 while (i++ < view->nlines - 1 &&
1945 s->first_displayed_line > 1)
1946 s->first_displayed_line--;
1951 s->first_displayed_line++;
1956 while (!s->eof && i++ < view->nlines - 1) {
1958 line = parse_next_line(s->f, NULL);
1959 s->first_displayed_line++;
1965 if (s->diff_context > 0) {
1967 err = create_diff(s);
1971 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1973 err = create_diff(s);
1983 static const struct got_error *
1984 cmd_diff(int argc, char *argv[])
1986 const struct got_error *error = NULL;
1987 struct got_repository *repo = NULL;
1988 struct got_object *obj1 = NULL, *obj2 = NULL;
1989 char *repo_path = NULL;
1990 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1992 struct tog_view *view;
1995 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2000 while ((ch = getopt(argc, argv, "")) != -1) {
2012 usage_diff(); /* TODO show local worktree changes */
2013 } else if (argc == 2) {
2014 repo_path = getcwd(NULL, 0);
2015 if (repo_path == NULL)
2016 return got_error_from_errno();
2017 obj_id_str1 = argv[0];
2018 obj_id_str2 = argv[1];
2019 } else if (argc == 3) {
2020 repo_path = realpath(argv[0], NULL);
2021 if (repo_path == NULL)
2022 return got_error_from_errno();
2023 obj_id_str1 = argv[1];
2024 obj_id_str2 = argv[2];
2028 error = got_repo_open(&repo, repo_path);
2033 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2037 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2041 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2043 error = got_error_from_errno();
2046 error = open_diff_view(view, obj1, obj2, repo);
2049 error = view_loop(view);
2051 got_repo_close(repo);
2053 got_object_close(obj1);
2055 got_object_close(obj2);
2063 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2068 struct tog_blame_line {
2070 struct got_object_id *id;
2073 static const struct got_error *
2074 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2075 const char *path, struct tog_blame_line *lines, int nlines,
2076 int blame_complete, int selected_line, int *first_displayed_line,
2077 int *last_displayed_line, int *eof, int max_lines)
2079 const struct got_error *err;
2080 int lineno = 0, nprinted = 0;
2085 struct tog_blame_line *blame_line;
2086 struct got_object_id *prev_id = NULL;
2089 err = got_object_id_str(&id_str, id);
2094 werase(view->window);
2096 if (asprintf(&line, "commit: %s", id_str) == -1) {
2097 err = got_error_from_errno();
2102 err = format_line(&wline, &width, line, view->ncols);
2105 if (view_needs_focus_indication(view))
2106 wstandout(view->window);
2107 waddwstr(view->window, wline);
2108 if (view_needs_focus_indication(view))
2109 wstandend(view->window);
2112 if (width < view->ncols)
2113 waddch(view->window, '\n');
2115 if (asprintf(&line, "[%d/%d] %s%s",
2116 *first_displayed_line - 1 + selected_line, nlines,
2117 blame_complete ? "" : "annotating ", path) == -1) {
2119 return got_error_from_errno();
2122 err = format_line(&wline, &width, line, view->ncols);
2127 waddwstr(view->window, wline);
2130 if (width < view->ncols)
2131 waddch(view->window, '\n');
2134 while (nprinted < max_lines - 2) {
2135 line = parse_next_line(f, &len);
2140 if (++lineno < *first_displayed_line) {
2145 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2146 err = format_line(&wline, &width, line, wlimit);
2152 if (view->focussed && nprinted == selected_line - 1)
2153 wstandout(view->window);
2155 blame_line = &lines[lineno - 1];
2156 if (blame_line->annotated && prev_id &&
2157 got_object_id_cmp(prev_id, blame_line->id) == 0)
2158 waddstr(view->window, " ");
2159 else if (blame_line->annotated) {
2161 err = got_object_id_str(&id_str, blame_line->id);
2167 wprintw(view->window, "%.8s ", id_str);
2169 prev_id = blame_line->id;
2171 waddstr(view->window, "........ ");
2175 waddwstr(view->window, wline);
2176 while (width < wlimit) {
2177 waddch(view->window, ' ');
2180 if (view->focussed && nprinted == selected_line - 1)
2181 wstandend(view->window);
2182 if (++nprinted == 1)
2183 *first_displayed_line = lineno;
2188 *last_displayed_line = lineno;
2195 static const struct got_error *
2196 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2198 const struct got_error *err = NULL;
2199 struct tog_blame_cb_args *a = arg;
2200 struct tog_blame_line *line;
2203 if (nlines != a->nlines ||
2204 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2205 return got_error(GOT_ERR_RANGE);
2207 errcode = pthread_mutex_lock(&tog_mutex);
2209 return got_error_set_errno(errcode);
2211 if (*a->quit) { /* user has quit the blame view */
2212 err = got_error(GOT_ERR_ITER_COMPLETED);
2217 goto done; /* no change in this commit */
2219 line = &a->lines[lineno - 1];
2220 if (line->annotated)
2223 line->id = got_object_id_dup(id);
2224 if (line->id == NULL) {
2225 err = got_error_from_errno();
2228 line->annotated = 1;
2230 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2231 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2232 a->last_displayed_line, a->eof, a->view->nlines);
2234 errcode = pthread_mutex_unlock(&tog_mutex);
2236 err = got_error_set_errno(errcode);
2241 blame_thread(void *arg)
2243 const struct got_error *err;
2244 struct tog_blame_thread_args *ta = arg;
2245 struct tog_blame_cb_args *a = ta->cb_args;
2248 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2249 blame_cb, ta->cb_args);
2251 errcode = pthread_mutex_lock(&tog_mutex);
2253 return (void *)got_error_set_errno(errcode);
2255 got_repo_close(ta->repo);
2259 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2260 a->lines, a->nlines, 1, *a->selected_line,
2261 a->first_displayed_line, a->last_displayed_line, a->eof,
2265 errcode = pthread_mutex_unlock(&tog_mutex);
2266 if (errcode && err == NULL)
2267 err = got_error_set_errno(errcode);
2272 static struct got_object_id *
2273 get_selected_commit_id(struct tog_blame_line *lines,
2274 int first_displayed_line, int selected_line)
2276 struct tog_blame_line *line;
2278 line = &lines[first_displayed_line - 1 + selected_line - 1];
2279 if (!line->annotated)
2285 static const struct got_error *
2286 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2287 struct tog_blame_line *lines, int first_displayed_line,
2288 int selected_line, struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 struct got_commit_object *commit = NULL;
2292 struct got_object_id *selected_id;
2293 struct got_object_qid *pid;
2298 selected_id = get_selected_commit_id(lines,
2299 first_displayed_line, selected_line);
2300 if (selected_id == NULL)
2303 err = got_object_open(obj, repo, selected_id);
2307 err = got_object_commit_open(&commit, repo, *obj);
2311 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2313 err = got_object_open(pobj, repo, pid->id);
2319 got_object_commit_close(commit);
2323 static const struct got_error *
2324 stop_blame(struct tog_blame *blame)
2326 const struct got_error *err = NULL;
2329 if (blame->thread) {
2331 errcode = pthread_mutex_unlock(&tog_mutex);
2333 return got_error_set_errno(errcode);
2334 errcode = pthread_join(blame->thread, (void **)&err);
2336 return got_error_set_errno(errcode);
2337 errcode = pthread_mutex_lock(&tog_mutex);
2339 return got_error_set_errno(errcode);
2340 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2342 blame->thread = NULL;
2344 if (blame->thread_args.repo) {
2345 got_repo_close(blame->thread_args.repo);
2346 blame->thread_args.repo = NULL;
2352 for (i = 0; i < blame->nlines; i++)
2353 free(blame->lines[i].id);
2355 blame->lines = NULL;
2356 free(blame->cb_args.commit_id);
2357 blame->cb_args.commit_id = NULL;
2362 static const struct got_error *
2363 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2364 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2365 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2366 struct got_repository *repo)
2368 const struct got_error *err = NULL;
2369 struct got_blob_object *blob = NULL;
2370 struct got_repository *thread_repo = NULL;
2371 struct got_object_id *obj_id = NULL;
2372 struct got_object *obj = NULL;
2375 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2379 err = got_object_open(&obj, repo, obj_id);
2383 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2384 err = got_error(GOT_ERR_OBJ_TYPE);
2388 err = got_object_blob_open(&blob, repo, obj, 8192);
2391 blame->f = got_opentemp();
2392 if (blame->f == NULL) {
2393 err = got_error_from_errno();
2396 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2401 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2402 if (blame->lines == NULL) {
2403 err = got_error_from_errno();
2407 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2411 blame->cb_args.view = view;
2412 blame->cb_args.lines = blame->lines;
2413 blame->cb_args.nlines = blame->nlines;
2414 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2415 if (blame->cb_args.commit_id == NULL) {
2416 err = got_error_from_errno();
2419 blame->cb_args.f = blame->f;
2420 blame->cb_args.path = path;
2421 blame->cb_args.first_displayed_line = first_displayed_line;
2422 blame->cb_args.selected_line = selected_line;
2423 blame->cb_args.last_displayed_line = last_displayed_line;
2424 blame->cb_args.quit = done;
2425 blame->cb_args.eof = eof;
2427 blame->thread_args.path = path;
2428 blame->thread_args.repo = thread_repo;
2429 blame->thread_args.cb_args = &blame->cb_args;
2430 blame->thread_args.complete = blame_complete;
2431 *blame_complete = 0;
2433 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2434 &blame->thread_args);
2436 err = got_error_set_errno(errcode);
2442 got_object_blob_close(blob);
2445 got_object_close(obj);
2451 static const struct got_error *
2452 open_blame_view(struct tog_view *view, char *path,
2453 struct got_object_id *commit_id, struct got_repository *repo)
2455 const struct got_error *err = NULL;
2456 struct tog_blame_view_state *s = &view->state.blame;
2458 SIMPLEQ_INIT(&s->blamed_commits);
2460 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2464 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2465 s->first_displayed_line = 1;
2466 s->last_displayed_line = view->nlines;
2467 s->selected_line = 1;
2468 s->blame_complete = 0;
2470 if (s->path == NULL)
2471 return got_error_from_errno();
2473 s->commit_id = commit_id;
2474 memset(&s->blame, 0, sizeof(s->blame));
2476 view->show = show_blame_view;
2477 view->input = input_blame_view;
2478 view->close = close_blame_view;
2480 return run_blame(&s->blame, view, &s->blame_complete,
2481 &s->first_displayed_line, &s->last_displayed_line,
2482 &s->selected_line, &s->done, &s->eof, s->path,
2483 s->blamed_commit->id, s->repo);
2486 static const struct got_error *
2487 close_blame_view(struct tog_view *view)
2489 const struct got_error *err = NULL;
2490 struct tog_blame_view_state *s = &view->state.blame;
2492 if (s->blame.thread)
2493 err = stop_blame(&s->blame);
2495 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2496 struct got_object_qid *blamed_commit;
2497 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2498 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2499 got_object_qid_free(blamed_commit);
2507 static const struct got_error *
2508 show_blame_view(struct tog_view *view)
2510 const struct got_error *err = NULL;
2511 struct tog_blame_view_state *s = &view->state.blame;
2513 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2514 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2515 s->selected_line, &s->first_displayed_line,
2516 &s->last_displayed_line, &s->eof, view->nlines);
2522 static const struct got_error *
2523 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2524 struct tog_view **focus_view, struct tog_view *view, int ch)
2526 const struct got_error *err = NULL, *thread_err = NULL;
2527 struct got_object *obj = NULL, *pobj = NULL;
2528 struct tog_view *diff_view;
2529 struct tog_blame_view_state *s = &view->state.blame;
2538 if (s->selected_line > 1)
2540 else if (s->selected_line == 1 &&
2541 s->first_displayed_line > 1)
2542 s->first_displayed_line--;
2545 if (s->first_displayed_line == 1) {
2546 s->selected_line = 1;
2549 if (s->first_displayed_line > view->nlines - 2)
2550 s->first_displayed_line -=
2553 s->first_displayed_line = 1;
2557 if (s->selected_line < view->nlines - 2 &&
2558 s->first_displayed_line +
2559 s->selected_line <= s->blame.nlines)
2561 else if (s->last_displayed_line <
2563 s->first_displayed_line++;
2567 struct got_object_id *id;
2568 id = get_selected_commit_id(s->blame.lines,
2569 s->first_displayed_line, s->selected_line);
2570 if (id == NULL || got_object_id_cmp(id,
2571 s->blamed_commit->id) == 0)
2573 err = open_selected_commit(&pobj, &obj,
2574 s->blame.lines, s->first_displayed_line,
2575 s->selected_line, s->repo);
2578 if (pobj == NULL && obj == NULL)
2580 if (ch == 'p' && pobj == NULL)
2583 thread_err = stop_blame(&s->blame);
2587 id = got_object_get_id(ch == 'b' ? obj : pobj);
2588 got_object_close(obj);
2591 got_object_close(pobj);
2594 err = got_object_qid_alloc(&s->blamed_commit, id);
2597 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2598 s->blamed_commit, entry);
2599 err = run_blame(&s->blame, view, &s->blame_complete,
2600 &s->first_displayed_line, &s->last_displayed_line,
2601 &s->selected_line, &s->done, &s->eof,
2602 s->path, s->blamed_commit->id, s->repo);
2608 struct got_object_qid *first;
2609 first = SIMPLEQ_FIRST(&s->blamed_commits);
2610 if (!got_object_id_cmp(first->id, s->commit_id))
2613 thread_err = stop_blame(&s->blame);
2617 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2618 got_object_qid_free(s->blamed_commit);
2620 SIMPLEQ_FIRST(&s->blamed_commits);
2621 err = run_blame(&s->blame, view, &s->blame_complete,
2622 &s->first_displayed_line, &s->last_displayed_line,
2623 &s->selected_line, &s->done, &s->eof, s->path,
2624 s->blamed_commit->id, s->repo);
2631 err = open_selected_commit(&pobj, &obj,
2632 s->blame.lines, s->first_displayed_line,
2633 s->selected_line, s->repo);
2636 if (pobj == NULL && obj == NULL)
2639 if (view_is_parent_view(view))
2640 begin_x = view_split_begin_x(view->begin_x);
2641 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2642 if (diff_view == NULL) {
2643 err = got_error_from_errno();
2646 err = open_diff_view(diff_view, pobj, obj, s->repo);
2648 view_close(diff_view);
2651 if (view_is_parent_view(view)) {
2652 err = view_close_child(view);
2655 err = view_set_child(view, diff_view);
2657 view_close(diff_view);
2660 if (!view_is_splitscreen(diff_view)) {
2661 *focus_view = diff_view;
2662 view->child_focussed = 1;
2665 *new_view = diff_view;
2667 got_object_close(pobj);
2670 got_object_close(obj);
2677 if (s->last_displayed_line >= s->blame.nlines &&
2678 s->selected_line < view->nlines - 2) {
2679 s->selected_line = MIN(s->blame.nlines,
2683 if (s->last_displayed_line + view->nlines - 2
2685 s->first_displayed_line +=
2688 s->first_displayed_line =
2693 if (s->selected_line > view->nlines - 2) {
2694 s->selected_line = MIN(s->blame.nlines,
2703 got_object_close(pobj);
2704 return thread_err ? thread_err : err;
2707 static const struct got_error *
2708 cmd_blame(int argc, char *argv[])
2710 const struct got_error *error;
2711 struct got_repository *repo = NULL;
2712 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2713 struct got_object_id *commit_id = NULL;
2714 char *commit_id_str = NULL;
2716 struct tog_view *view;
2719 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2724 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2727 commit_id_str = optarg;
2730 repo_path = realpath(optarg, NULL);
2731 if (repo_path == NULL)
2732 err(1, "-r option");
2748 cwd = getcwd(NULL, 0);
2750 error = got_error_from_errno();
2753 if (repo_path == NULL) {
2754 repo_path = strdup(cwd);
2755 if (repo_path == NULL) {
2756 error = got_error_from_errno();
2762 error = got_repo_open(&repo, repo_path);
2766 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2770 if (commit_id_str == NULL) {
2771 struct got_reference *head_ref;
2772 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2775 error = got_ref_resolve(&commit_id, repo, head_ref);
2776 got_ref_close(head_ref);
2778 struct got_object *obj;
2779 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2782 commit_id = got_object_id_dup(got_object_get_id(obj));
2783 if (commit_id == NULL)
2784 error = got_error_from_errno();
2785 got_object_close(obj);
2790 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2792 error = got_error_from_errno();
2795 error = open_blame_view(view, in_repo_path, commit_id, repo);
2798 error = view_loop(view);
2804 got_repo_close(repo);
2808 static const struct got_error *
2809 draw_tree_entries(struct tog_view *view,
2810 struct got_tree_entry **first_displayed_entry,
2811 struct got_tree_entry **last_displayed_entry,
2812 struct got_tree_entry **selected_entry, int *ndisplayed,
2813 const char *label, int show_ids, const char *parent_path,
2814 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2816 const struct got_error *err = NULL;
2817 struct got_tree_entry *te;
2823 werase(view->window);
2828 err = format_line(&wline, &width, label, view->ncols);
2831 if (view_needs_focus_indication(view))
2832 wstandout(view->window);
2833 waddwstr(view->window, wline);
2834 if (view_needs_focus_indication(view))
2835 wstandend(view->window);
2838 if (width < view->ncols)
2839 waddch(view->window, '\n');
2842 err = format_line(&wline, &width, parent_path, view->ncols);
2845 waddwstr(view->window, wline);
2848 if (width < view->ncols)
2849 waddch(view->window, '\n');
2852 waddch(view->window, '\n');
2856 te = SIMPLEQ_FIRST(&entries->head);
2857 if (*first_displayed_entry == NULL) {
2858 if (selected == 0) {
2860 wstandout(view->window);
2861 *selected_entry = NULL;
2863 waddstr(view->window, " ..\n"); /* parent directory */
2864 if (selected == 0 && view->focussed)
2865 wstandend(view->window);
2872 while (te != *first_displayed_entry)
2873 te = SIMPLEQ_NEXT(te, entry);
2877 char *line = NULL, *id_str = NULL;
2880 err = got_object_id_str(&id_str, te->id);
2882 return got_error_from_errno();
2884 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2885 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2887 return got_error_from_errno();
2890 err = format_line(&wline, &width, line, view->ncols);
2895 if (n == selected) {
2897 wstandout(view->window);
2898 *selected_entry = te;
2900 waddwstr(view->window, wline);
2901 if (width < view->ncols)
2902 waddch(view->window, '\n');
2903 if (n == selected && view->focussed)
2904 wstandend(view->window);
2910 *last_displayed_entry = te;
2913 te = SIMPLEQ_NEXT(te, entry);
2920 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 const struct got_tree_entries *entries, int isroot)
2923 struct got_tree_entry *te, *prev;
2926 if (*first_displayed_entry == NULL)
2929 te = SIMPLEQ_FIRST(&entries->head);
2930 if (*first_displayed_entry == te) {
2932 *first_displayed_entry = NULL;
2936 /* XXX this is stupid... switch to TAILQ? */
2937 for (i = 0; i < maxscroll; i++) {
2938 while (te != *first_displayed_entry) {
2940 te = SIMPLEQ_NEXT(te, entry);
2942 *first_displayed_entry = prev;
2943 te = SIMPLEQ_FIRST(&entries->head);
2945 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2946 *first_displayed_entry = NULL;
2950 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2951 struct got_tree_entry *last_displayed_entry,
2952 const struct got_tree_entries *entries)
2954 struct got_tree_entry *next;
2957 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2960 if (*first_displayed_entry)
2961 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2963 next = SIMPLEQ_FIRST(&entries->head);
2965 *first_displayed_entry = next;
2966 if (++n >= maxscroll)
2968 next = SIMPLEQ_NEXT(next, entry);
2972 static const struct got_error *
2973 tree_entry_path(char **path, struct tog_parent_trees *parents,
2974 struct got_tree_entry *te)
2976 const struct got_error *err = NULL;
2977 struct tog_parent_tree *pt;
2978 size_t len = 2; /* for leading slash and NUL */
2980 TAILQ_FOREACH(pt, parents, entry)
2981 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2983 len += strlen(te->name);
2985 *path = calloc(1, len);
2987 return got_error_from_errno();
2990 pt = TAILQ_LAST(parents, tog_parent_trees);
2992 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2993 err = got_error(GOT_ERR_NO_SPACE);
2996 if (strlcat(*path, "/", len) >= len) {
2997 err = got_error(GOT_ERR_NO_SPACE);
3000 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3003 if (strlcat(*path, te->name, len) >= len) {
3004 err = got_error(GOT_ERR_NO_SPACE);
3016 static const struct got_error *
3017 blame_tree_entry(struct tog_view **new_view, int begin_x,
3018 struct got_tree_entry *te, struct tog_parent_trees *parents,
3019 struct got_object_id *commit_id, struct got_repository *repo)
3021 const struct got_error *err = NULL;
3023 struct tog_view *blame_view;
3025 err = tree_entry_path(&path, parents, te);
3029 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3030 if (blame_view == NULL)
3031 return got_error_from_errno();
3033 err = open_blame_view(blame_view, path, commit_id, repo);
3035 view_close(blame_view);
3038 *new_view = blame_view;
3042 static const struct got_error *
3043 log_tree_entry(struct tog_view **new_view, int begin_x,
3044 struct got_tree_entry *te, struct tog_parent_trees *parents,
3045 struct got_object_id *commit_id, struct got_repository *repo)
3047 struct tog_view *log_view;
3048 const struct got_error *err = NULL;
3051 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3052 if (log_view == NULL)
3053 return got_error_from_errno();
3055 err = tree_entry_path(&path, parents, te);
3059 err = open_log_view(log_view, commit_id, repo, path, 0);
3061 view_close(log_view);
3063 *new_view = log_view;
3068 static const struct got_error *
3069 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3070 struct got_object_id *commit_id, struct got_repository *repo)
3072 const struct got_error *err = NULL;
3073 char *commit_id_str = NULL;
3074 struct tog_tree_view_state *s = &view->state.tree;
3076 TAILQ_INIT(&s->parents);
3078 err = got_object_id_str(&commit_id_str, commit_id);
3082 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3083 err = got_error_from_errno();
3087 s->root = s->tree = root;
3088 s->entries = got_object_tree_get_entries(root);
3089 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3090 s->commit_id = got_object_id_dup(commit_id);
3091 if (s->commit_id == NULL) {
3092 err = got_error_from_errno();
3097 view->show = show_tree_view;
3098 view->input = input_tree_view;
3099 view->close = close_tree_view;
3101 free(commit_id_str);
3103 free(s->tree_label);
3104 s->tree_label = NULL;
3109 static const struct got_error *
3110 close_tree_view(struct tog_view *view)
3112 struct tog_tree_view_state *s = &view->state.tree;
3114 free(s->tree_label);
3115 s->tree_label = NULL;
3117 s->commit_id = NULL;
3118 while (!TAILQ_EMPTY(&s->parents)) {
3119 struct tog_parent_tree *parent;
3120 parent = TAILQ_FIRST(&s->parents);
3121 TAILQ_REMOVE(&s->parents, parent, entry);
3125 if (s->tree != s->root)
3126 got_object_tree_close(s->tree);
3127 got_object_tree_close(s->root);
3132 static const struct got_error *
3133 show_tree_view(struct tog_view *view)
3135 const struct got_error *err = NULL;
3136 struct tog_tree_view_state *s = &view->state.tree;
3139 err = tree_entry_path(&parent_path, &s->parents, NULL);
3143 err = draw_tree_entries(view, &s->first_displayed_entry,
3144 &s->last_displayed_entry, &s->selected_entry,
3145 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3146 s->entries, s->selected, view->nlines, s->tree == s->root);
3153 static const struct got_error *
3154 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3155 struct tog_view **focus_view, struct tog_view *view, int ch)
3157 const struct got_error *err = NULL;
3158 struct tog_tree_view_state *s = &view->state.tree;
3159 struct tog_view *log_view;
3164 s->show_ids = !s->show_ids;
3167 if (!s->selected_entry)
3169 if (view_is_parent_view(view))
3170 begin_x = view_split_begin_x(view->begin_x);
3171 err = log_tree_entry(&log_view, begin_x,
3172 s->selected_entry, &s->parents,
3173 s->commit_id, s->repo);
3174 if (view_is_parent_view(view)) {
3175 err = view_close_child(view);
3178 err = view_set_child(view, log_view);
3180 view_close(log_view);
3183 *focus_view = log_view;
3184 view->child_focussed = 1;
3186 *new_view = log_view;
3190 if (s->selected > 0)
3192 if (s->selected > 0)
3194 tree_scroll_up(&s->first_displayed_entry, 1,
3195 s->entries, s->tree == s->root);
3198 if (SIMPLEQ_FIRST(&s->entries->head) ==
3199 s->first_displayed_entry) {
3200 if (s->tree != s->root)
3201 s->first_displayed_entry = NULL;
3205 tree_scroll_up(&s->first_displayed_entry,
3206 view->nlines, s->entries,
3207 s->tree == s->root);
3211 if (s->selected < s->ndisplayed - 1) {
3215 tree_scroll_down(&s->first_displayed_entry, 1,
3216 s->last_displayed_entry, s->entries);
3219 tree_scroll_down(&s->first_displayed_entry,
3220 view->nlines, s->last_displayed_entry,
3222 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3225 /* can't scroll any further; move cursor down */
3226 if (s->selected < s->ndisplayed - 1)
3227 s->selected = s->ndisplayed - 1;
3231 if (s->selected_entry == NULL) {
3232 struct tog_parent_tree *parent;
3234 /* user selected '..' */
3235 if (s->tree == s->root)
3237 parent = TAILQ_FIRST(&s->parents);
3238 TAILQ_REMOVE(&s->parents, parent,
3240 got_object_tree_close(s->tree);
3241 s->tree = parent->tree;
3243 got_object_tree_get_entries(s->tree);
3244 s->first_displayed_entry =
3245 parent->first_displayed_entry;
3247 parent->selected_entry;
3248 s->selected = parent->selected;
3250 } else if (S_ISDIR(s->selected_entry->mode)) {
3251 struct tog_parent_tree *parent;
3252 struct got_tree_object *child;
3253 err = got_object_open_as_tree(&child,
3254 s->repo, s->selected_entry->id);
3257 parent = calloc(1, sizeof(*parent));
3258 if (parent == NULL) {
3259 err = got_error_from_errno();
3262 parent->tree = s->tree;
3263 parent->first_displayed_entry =
3264 s->first_displayed_entry;
3265 parent->selected_entry = s->selected_entry;
3266 parent->selected = s->selected;
3267 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3270 got_object_tree_get_entries(s->tree);
3272 s->first_displayed_entry = NULL;
3273 } else if (S_ISREG(s->selected_entry->mode)) {
3274 struct tog_view *blame_view;
3275 int begin_x = view_is_parent_view(view) ?
3276 view_split_begin_x(view->begin_x) : 0;
3278 err = blame_tree_entry(&blame_view, begin_x,
3279 s->selected_entry, &s->parents, s->commit_id,
3283 if (view_is_parent_view(view)) {
3284 err = view_close_child(view);
3287 err = view_set_child(view, blame_view);
3289 view_close(blame_view);
3292 *focus_view = blame_view;
3293 view->child_focussed = 1;
3295 *new_view = blame_view;
3299 if (s->selected > view->nlines)
3300 s->selected = s->ndisplayed - 1;
3313 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3318 static const struct got_error *
3319 cmd_tree(int argc, char *argv[])
3321 const struct got_error *error;
3322 struct got_repository *repo = NULL;
3323 char *repo_path = NULL;
3324 struct got_object_id *commit_id = NULL;
3325 char *commit_id_arg = NULL;
3326 struct got_commit_object *commit = NULL;
3327 struct got_tree_object *tree = NULL;
3329 struct tog_view *view;
3332 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3337 while ((ch = getopt(argc, argv, "c:")) != -1) {
3340 commit_id_arg = optarg;
3352 repo_path = getcwd(NULL, 0);
3353 if (repo_path == NULL)
3354 return got_error_from_errno();
3355 } else if (argc == 1) {
3356 repo_path = realpath(argv[0], NULL);
3357 if (repo_path == NULL)
3358 return got_error_from_errno();
3362 error = got_repo_open(&repo, repo_path);
3367 if (commit_id_arg == NULL) {
3368 error = get_head_commit_id(&commit_id, repo);
3372 struct got_object *obj;
3373 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3374 if (error == NULL) {
3375 commit_id = got_object_id_dup(got_object_get_id(obj));
3376 if (commit_id == NULL)
3377 error = got_error_from_errno();
3383 error = got_object_open_as_commit(&commit, repo, commit_id);
3387 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3391 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3393 error = got_error_from_errno();
3396 error = open_tree_view(view, tree, commit_id, repo);
3399 error = view_loop(view);
3403 got_object_commit_close(commit);
3405 got_object_tree_close(tree);
3407 got_repo_close(repo);
3415 halfdelay(1); /* Do fast refresh while initial view is loading. */
3418 intrflush(stdscr, FALSE);
3419 keypad(stdscr, TRUE);
3428 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3429 "Available commands:\n", getprogname());
3430 for (i = 0; i < nitems(tog_commands); i++) {
3431 struct tog_cmd *cmd = &tog_commands[i];
3432 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3438 make_argv(const char *arg0, const char *arg1)
3441 int argc = (arg1 == NULL ? 1 : 2);
3443 argv = calloc(argc, sizeof(char *));
3446 argv[0] = strdup(arg0);
3447 if (argv[0] == NULL)
3450 argv[1] = strdup(arg1);
3451 if (argv[1] == NULL)
3459 main(int argc, char *argv[])
3461 const struct got_error *error = NULL;
3462 struct tog_cmd *cmd = NULL;
3464 char **cmd_argv = NULL;
3466 setlocale(LC_ALL, "");
3468 while ((ch = getopt(argc, argv, "h")) != -1) {
3487 /* Build an argument vector which runs a default command. */
3488 cmd = &tog_commands[0];
3489 cmd_argv = make_argv(cmd->name, NULL);
3494 /* Did the user specific a command? */
3495 for (i = 0; i < nitems(tog_commands); i++) {
3496 if (strncmp(tog_commands[i].name, argv[0],
3497 strlen(argv[0])) == 0) {
3498 cmd = &tog_commands[i];
3500 tog_commands[i].cmd_usage();
3505 /* Did the user specify a repository? */
3506 char *repo_path = realpath(argv[0], NULL);
3508 struct got_repository *repo;
3509 error = got_repo_open(&repo, repo_path);
3511 got_repo_close(repo);
3513 error = got_error_from_errno();
3516 fprintf(stderr, "%s: '%s' is not a "
3517 "known command\n", getprogname(),
3521 fprintf(stderr, "%s: '%s' is neither a known "
3522 "command nor a path to a repository\n",
3523 getprogname(), argv[0]);
3527 cmd = &tog_commands[0];
3528 cmd_argv = make_argv(cmd->name, repo_path);
3536 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3543 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);