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;
100 struct got_repository *repo;
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
112 struct commit_queue_head head;
115 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
117 struct tog_log_thread_args {
118 pthread_cond_t need_commits;
120 struct got_commit_graph *graph;
121 struct commit_queue *commits;
122 const char *in_repo_path;
123 struct got_object_id *start_id;
124 struct got_repository *repo;
127 struct tog_view *view;
128 struct commit_queue_entry **first_displayed_entry;
129 struct commit_queue_entry **last_displayed_entry;
130 struct commit_queue_entry **selected_entry;
134 struct tog_log_view_state {
135 struct commit_queue commits;
136 struct commit_queue_entry *first_displayed_entry;
137 struct commit_queue_entry *last_displayed_entry;
138 struct commit_queue_entry *selected_entry;
141 struct got_repository *repo;
142 struct got_object_id *start_id;
145 struct tog_log_thread_args thread_args;
148 struct tog_blame_cb_args {
149 struct tog_blame_line *lines; /* one per line */
152 struct tog_view *view;
153 struct got_object_id *commit_id;
156 int *first_displayed_line;
157 int *last_displayed_line;
163 struct tog_blame_thread_args {
165 struct got_repository *repo;
166 struct tog_blame_cb_args *cb_args;
173 struct tog_blame_line *lines;
176 struct tog_blame_thread_args thread_args;
177 struct tog_blame_cb_args cb_args;
181 struct tog_blame_view_state {
182 int first_displayed_line;
183 int last_displayed_line;
188 struct got_object_id_queue blamed_commits;
189 struct got_object_qid *blamed_commit;
191 struct got_repository *repo;
192 struct got_object_id *commit_id;
193 struct tog_blame blame;
196 struct tog_parent_tree {
197 TAILQ_ENTRY(tog_parent_tree) entry;
198 struct got_tree_object *tree;
199 struct got_tree_entry *first_displayed_entry;
200 struct got_tree_entry *selected_entry;
204 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
206 struct tog_tree_view_state {
208 struct got_tree_object *root;
209 struct got_tree_object *tree;
210 const struct got_tree_entries *entries;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *last_displayed_entry;
213 struct got_tree_entry *selected_entry;
214 int nentries, ndisplayed, selected, show_ids;
215 struct tog_parent_trees parents;
216 struct got_object_id *commit_id;
217 struct got_repository *repo;
221 * We implement two types of views: parent views and child views.
223 * The 'Tab' key switches between a parent view and its child view.
224 * Child views are shown side-by-side to their parent view, provided
225 * there is enough screen estate.
227 * When a new view is opened from within a parent view, this new view
228 * becomes a child view of the parent view, replacing any existing child.
230 * When a new view is opened from within a child view, this new view
231 * becomes a parent view which will obscure the views below until the
232 * user quits the new parent view by typing 'q'.
234 * This list of views contains parent views only.
235 * Child views are only pointed to by their parent view.
237 TAILQ_HEAD(tog_view_list_head, tog_view);
240 TAILQ_ENTRY(tog_view) entry;
243 int nlines, ncols, begin_y, begin_x;
244 int lines, cols; /* copies of LINES and COLS */
246 struct tog_view *parent;
247 struct tog_view *child;
250 /* type-specific state */
251 enum tog_view_type type;
253 struct tog_diff_view_state diff;
254 struct tog_log_view_state log;
255 struct tog_blame_view_state blame;
256 struct tog_tree_view_state tree;
259 const struct got_error *(*show)(struct tog_view *);
260 const struct got_error *(*input)(struct tog_view **,
261 struct tog_view **, struct tog_view**, struct tog_view *, int);
262 const struct got_error *(*close)(struct tog_view *);
265 static const struct got_error *open_diff_view(struct tog_view *,
266 struct got_object *, struct got_object *, struct got_repository *);
267 static const struct got_error *show_diff_view(struct tog_view *);
268 static const struct got_error *input_diff_view(struct tog_view **,
269 struct tog_view **, struct tog_view **, struct tog_view *, int);
270 static const struct got_error* close_diff_view(struct tog_view *);
272 static const struct got_error *open_log_view(struct tog_view *,
273 struct got_object_id *, struct got_repository *, const char *);
274 static const struct got_error * show_log_view(struct tog_view *);
275 static const struct got_error *input_log_view(struct tog_view **,
276 struct tog_view **, struct tog_view **, struct tog_view *, int);
277 static const struct got_error *close_log_view(struct tog_view *);
279 static const struct got_error *open_blame_view(struct tog_view *, char *,
280 struct got_object_id *, struct got_repository *);
281 static const struct got_error *show_blame_view(struct tog_view *);
282 static const struct got_error *input_blame_view(struct tog_view **,
283 struct tog_view **, struct tog_view **, struct tog_view *, int);
284 static const struct got_error *close_blame_view(struct tog_view *);
286 static const struct got_error *open_tree_view(struct tog_view *,
287 struct got_tree_object *, struct got_object_id *, struct got_repository *);
288 static const struct got_error *show_tree_view(struct tog_view *);
289 static const struct got_error *input_tree_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error *close_tree_view(struct tog_view *);
293 static const struct got_error *
294 view_close(struct tog_view *view)
296 const struct got_error *err = NULL;
299 view_close(view->child);
303 err = view->close(view);
305 del_panel(view->panel);
307 delwin(view->window);
312 static struct tog_view *
313 view_open(int nlines, int ncols, int begin_y, int begin_x,
314 enum tog_view_type type)
316 struct tog_view *view = calloc(1, sizeof(*view));
324 view->nlines = nlines ? nlines : LINES - begin_y;
325 view->ncols = ncols ? ncols : COLS - begin_x;
326 view->begin_y = begin_y;
327 view->begin_x = begin_x;
328 view->window = newwin(nlines, ncols, begin_y, begin_x);
329 if (view->window == NULL) {
333 view->panel = new_panel(view->window);
334 if (view->panel == NULL ||
335 set_panel_userptr(view->panel, view) != OK) {
340 keypad(view->window, TRUE);
345 view_split_begin_x(int begin_x)
349 return (COLS >= 120 ? COLS/2 : 0);
352 static const struct got_error *view_resize(struct tog_view *);
354 static const struct got_error *
355 view_splitscreen(struct tog_view *view)
357 const struct got_error *err = NULL;
360 view->begin_x = view_split_begin_x(0);
361 view->nlines = LINES;
362 view->ncols = COLS - view->begin_x;
365 err = view_resize(view);
369 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
370 return got_error_from_errno();
375 static const struct got_error *
376 view_fullscreen(struct tog_view *view)
378 const struct got_error *err = NULL;
382 view->nlines = LINES;
386 err = view_resize(view);
390 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
391 return got_error_from_errno();
397 view_is_parent_view(struct tog_view *view)
399 return view->parent == NULL;
402 static const struct got_error *
403 view_resize(struct tog_view *view)
407 if (view->lines > LINES)
408 nlines = view->nlines - (view->lines - LINES);
410 nlines = view->nlines + (LINES - view->lines);
412 if (view->cols > COLS)
413 ncols = view->ncols - (view->cols - COLS);
415 ncols = view->ncols + (COLS - view->cols);
417 if (wresize(view->window, nlines, ncols) == ERR)
418 return got_error_from_errno();
419 replace_panel(view->panel, view->window);
421 view->nlines = nlines;
427 view->child->begin_x = view_split_begin_x(view->begin_x);
428 if (view->child->begin_x == 0) {
429 view_fullscreen(view->child);
430 if (view->child->focussed)
431 show_panel(view->child->panel);
433 show_panel(view->panel);
435 view_splitscreen(view->child);
436 show_panel(view->child->panel);
443 static const struct got_error *
444 view_close_child(struct tog_view *view)
446 const struct got_error *err = NULL;
448 if (view->child == NULL)
451 err = view_close(view->child);
456 static const struct got_error *
457 view_set_child(struct tog_view *view, struct tog_view *child)
459 const struct got_error *err = NULL;
462 child->parent = view;
467 view_is_splitscreen(struct tog_view *view)
469 return !view_is_parent_view(view) && view->begin_x > 0;
472 static const struct got_error *
473 view_input(struct tog_view **new, struct tog_view **dead,
474 struct tog_view **focus, int *done, struct tog_view *view,
475 struct tog_view_list_head *views)
477 const struct got_error *err = NULL;
485 nodelay(stdscr, FALSE);
486 /* Allow threads to make progress while we are waiting for input. */
487 errcode = pthread_mutex_unlock(&tog_mutex);
489 return got_error_set_errno(errcode);
490 ch = wgetch(view->window);
491 errcode = pthread_mutex_lock(&tog_mutex);
493 return got_error_set_errno(errcode);
494 nodelay(stdscr, TRUE);
500 *focus = view->child;
501 view->child_focussed = 1;
502 } else if (view->parent) {
503 *focus = view->parent;
504 view->parent->child_focussed = 0;
508 err = view->input(new, dead, focus, view, ch);
515 if (view_is_parent_view(view)) {
516 if (view->child == NULL)
518 if (view_is_splitscreen(view->child)) {
519 *focus = view->child;
520 view->child_focussed = 1;
521 err = view_fullscreen(view->child);
523 err = view_splitscreen(view->child);
526 err = view->child->input(new, dead, focus,
527 view->child, KEY_RESIZE);
529 if (view_is_splitscreen(view)) {
531 view->parent->child_focussed = 1;
532 err = view_fullscreen(view);
534 err = view_splitscreen(view);
538 err = view->input(new, dead, focus, view,
543 TAILQ_FOREACH(v, views, entry) {
544 err = view_resize(v);
547 err = v->input(new, dead, focus, v, ch);
553 err = view->input(new, dead, focus, view, ch);
561 view_vborder(struct tog_view *view)
564 struct tog_view *view_above;
567 return view_vborder(view->parent);
569 panel = panel_above(view->panel);
573 view_above = panel_userptr(panel);
574 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
575 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
579 view_needs_focus_indication(struct tog_view *view)
581 if (view_is_parent_view(view)) {
582 if (view->child == NULL || view->child_focussed)
584 if (!view_is_splitscreen(view->child))
586 } else if (!view_is_splitscreen(view))
589 return view->focussed;
592 static const struct got_error *
593 view_loop(struct tog_view *view)
595 const struct got_error *err = NULL;
596 struct tog_view_list_head views;
597 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
598 int fast_refresh = 10;
599 int done = 0, errcode;
601 errcode = pthread_mutex_lock(&tog_mutex);
603 return got_error_set_errno(errcode);
606 TAILQ_INSERT_HEAD(&views, view, entry);
610 err = view->show(view);
615 while (!TAILQ_EMPTY(&views) && !done) {
616 /* Refresh fast during initialization, then become slower. */
617 if (fast_refresh && fast_refresh-- == 0)
618 halfdelay(10); /* switch to once per second */
620 err = view_input(&new_view, &dead_view, &focus_view, &done,
625 struct tog_view *prev = NULL;
627 if (view_is_parent_view(dead_view))
628 prev = TAILQ_PREV(dead_view,
629 tog_view_list_head, entry);
633 if (dead_view->parent)
634 dead_view->parent->child = NULL;
636 TAILQ_REMOVE(&views, dead_view, entry);
638 err = view_close(dead_view);
639 if (err || dead_view == main_view)
642 if (view == dead_view) {
647 else if (!TAILQ_EMPTY(&views))
648 view = TAILQ_LAST(&views,
653 if (view->child && view->child_focussed)
654 focus_view = view->child;
661 struct tog_view *v, *t;
662 /* Only allow one parent view per type. */
663 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
664 if (v->type != new_view->type)
666 TAILQ_REMOVE(&views, v, entry);
674 TAILQ_INSERT_TAIL(&views, new_view, entry);
675 if (focus_view == NULL)
676 focus_view = new_view;
679 show_panel(focus_view->panel);
682 focus_view->focussed = 1;
685 show_panel(new_view->panel);
686 if (view->child && view_is_splitscreen(view->child))
687 show_panel(view->child->panel);
690 if (focus_view == NULL) {
692 focus_view->focussed = 1;
695 err = view->parent->show(view->parent);
699 err = view->show(view);
703 err = view->child->show(view->child);
712 while (!TAILQ_EMPTY(&views)) {
713 view = TAILQ_FIRST(&views);
714 TAILQ_REMOVE(&views, view, entry);
718 errcode = pthread_mutex_unlock(&tog_mutex);
720 return got_error_set_errno(errcode);
730 "usage: %s log [-c commit] [-r repository-path] [path]\n",
735 /* Create newly allocated wide-character string equivalent to a byte string. */
736 static const struct got_error *
737 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
740 const struct got_error *err = NULL;
743 *wlen = mbstowcs(NULL, s, 0);
744 if (*wlen == (size_t)-1) {
747 return got_error_from_errno();
749 /* byte string invalid in current encoding; try to "fix" it */
750 err = got_mbsavis(&vis, &vislen, s);
753 *wlen = mbstowcs(NULL, vis, 0);
754 if (*wlen == (size_t)-1) {
755 err = got_error_from_errno(); /* give up */
760 *ws = calloc(*wlen + 1, sizeof(*ws));
762 err = got_error_from_errno();
766 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
767 err = got_error_from_errno();
778 /* Format a line for display, ensuring that it won't overflow a width limit. */
779 static const struct got_error *
780 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
782 const struct got_error *err = NULL;
784 wchar_t *wline = NULL;
791 err = mbs2ws(&wline, &wlen, line);
796 while (i < wlen && cols < wlimit) {
797 int width = wcwidth(wline[i]);
804 if (cols + width <= wlimit)
809 if (wline[i] == L'\t')
810 cols += TABSIZE - ((cols + 1) % TABSIZE);
814 err = got_error_from_errno();
829 static const struct got_error *
830 draw_commit(struct tog_view *view, struct got_commit_object *commit,
831 struct got_object_id *id)
833 const struct got_error *err = NULL;
834 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
835 char *logmsg0 = NULL, *logmsg = NULL;
836 char *author0 = NULL, *author = NULL;
837 wchar_t *wlogmsg = NULL, *wauthor = NULL;
838 int author_width, logmsg_width;
839 char *newline, *smallerthan;
842 static const size_t date_display_cols = 9;
843 static const size_t author_display_cols = 16;
844 const int avail = view->ncols;
846 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
847 &commit->tm_committer) >= sizeof(datebuf))
848 return got_error(GOT_ERR_NO_SPACE);
850 if (avail < date_display_cols)
851 limit = MIN(sizeof(datebuf) - 1, avail);
853 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
854 waddnstr(view->window, datebuf, limit);
859 author0 = strdup(commit->author);
860 if (author0 == NULL) {
861 err = got_error_from_errno();
865 smallerthan = strchr(author, '<');
869 char *at = strchr(author, '@');
874 err = format_line(&wauthor, &author_width, author, limit);
877 waddwstr(view->window, wauthor);
879 while (col <= avail && author_width < author_display_cols + 1) {
880 waddch(view->window, ' ');
887 logmsg0 = strdup(commit->logmsg);
888 if (logmsg0 == NULL) {
889 err = got_error_from_errno();
893 while (*logmsg == '\n')
895 newline = strchr(logmsg, '\n');
899 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
902 waddwstr(view->window, wlogmsg);
904 while (col <= avail) {
905 waddch(view->window, ' ');
917 static struct commit_queue_entry *
918 alloc_commit_queue_entry(struct got_commit_object *commit,
919 struct got_object_id *id)
921 struct commit_queue_entry *entry;
923 entry = calloc(1, sizeof(*entry));
928 entry->commit = commit;
933 pop_commit(struct commit_queue *commits)
935 struct commit_queue_entry *entry;
937 entry = TAILQ_FIRST(&commits->head);
938 TAILQ_REMOVE(&commits->head, entry, entry);
939 got_object_commit_close(entry->commit);
941 /* Don't free entry->id! It is owned by the commit graph. */
946 free_commits(struct commit_queue *commits)
948 while (!TAILQ_EMPTY(&commits->head))
952 static const struct got_error *
953 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
954 int minqueue, struct got_repository *repo, const char *path)
956 const struct got_error *err = NULL;
960 * We keep all commits open throughout the lifetime of the log
961 * view in order to avoid having to re-fetch commits from disk
962 * while updating the display.
964 while (nqueued < minqueue) {
965 struct got_object_id *id;
966 struct got_commit_object *commit;
967 struct commit_queue_entry *entry;
970 err = got_commit_graph_iter_next(&id, graph);
972 if (err->code != GOT_ERR_ITER_NEED_MORE)
974 err = got_commit_graph_fetch_commits(graph,
984 err = got_object_open_as_commit(&commit, repo, id);
987 entry = alloc_commit_queue_entry(commit, id);
989 err = got_error_from_errno();
993 errcode = pthread_mutex_lock(&tog_mutex);
995 err = got_error_set_errno(errcode);
999 entry->idx = commits->ncommits;
1000 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1002 commits->ncommits++;
1004 errcode = pthread_mutex_unlock(&tog_mutex);
1005 if (errcode && err == NULL)
1006 err = got_error_set_errno(errcode);
1012 static const struct got_error *
1013 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1015 const struct got_error *err = NULL;
1016 struct got_reference *head_ref;
1020 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1024 err = got_ref_resolve(head_id, repo, head_ref);
1025 got_ref_close(head_ref);
1034 static const struct got_error *
1035 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1036 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1037 struct commit_queue *commits, int selected_idx, int limit,
1038 const char *path, int commits_needed)
1040 const struct got_error *err = NULL;
1041 struct commit_queue_entry *entry;
1042 int ncommits, width;
1043 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1049 if (ncommits == selected_idx) {
1053 entry = TAILQ_NEXT(entry, entry);
1058 err = got_object_id_str(&id_str, (*selected)->id);
1063 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1064 entry ? entry->idx + 1 : 0, commits->ncommits,
1065 commits_needed == 0 ? "" : " loading...") == -1)
1066 return got_error_from_errno();
1068 if (path && strcmp(path, "/") != 0) {
1069 if (asprintf(&header, "commit: %s %s%s",
1070 id_str ? id_str : "........................................",
1071 path, ncommits_str) == -1) {
1072 err = got_error_from_errno();
1076 } else if (asprintf(&header, "commit: %s%s",
1077 id_str ? id_str : "........................................",
1078 ncommits_str) == -1) {
1079 err = got_error_from_errno();
1083 err = format_line(&wline, &width, header, view->ncols);
1087 werase(view->window);
1089 if (view_needs_focus_indication(view))
1090 wstandout(view->window);
1091 waddwstr(view->window, wline);
1092 while (width < view->ncols) {
1093 waddch(view->window, ' ');
1096 if (view_needs_focus_indication(view))
1097 wstandend(view->window);
1106 if (ncommits >= limit - 1)
1108 if (view->focussed && ncommits == selected_idx)
1109 wstandout(view->window);
1110 err = draw_commit(view, entry->commit, entry->id);
1111 if (view->focussed && ncommits == selected_idx)
1112 wstandend(view->window);
1117 entry = TAILQ_NEXT(entry, entry);
1129 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1130 struct commit_queue *commits)
1132 struct commit_queue_entry *entry;
1135 entry = TAILQ_FIRST(&commits->head);
1136 if (*first_displayed_entry == entry)
1139 entry = *first_displayed_entry;
1140 while (entry && nscrolled < maxscroll) {
1141 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1143 *first_displayed_entry = entry;
1149 static const struct got_error *
1150 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1151 struct commit_queue_entry **last_displayed_entry,
1152 struct commit_queue *commits, int *log_complete, int *commits_needed,
1153 pthread_cond_t *need_commits)
1155 const struct got_error *err = NULL;
1156 struct commit_queue_entry *pentry;
1159 if (*last_displayed_entry == NULL)
1163 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1164 if (pentry == NULL) {
1168 *commits_needed = maxscroll + 20;
1169 errcode = pthread_cond_signal(need_commits);
1171 return got_error_set_errno(errcode);
1174 *last_displayed_entry = pentry;
1176 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1179 *first_displayed_entry = pentry;
1180 } while (++nscrolled < maxscroll);
1185 static const struct got_error *
1186 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1187 struct got_object_id *commit_id, struct got_commit_object *commit,
1188 struct got_repository *repo)
1190 const struct got_error *err;
1191 struct got_object *obj1 = NULL, *obj2 = NULL;
1192 struct got_object_qid *parent_id;
1193 struct tog_view *diff_view;
1195 err = got_object_open(&obj2, repo, commit_id);
1199 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1201 err = got_object_open(&obj1, repo, parent_id->id);
1206 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1207 if (diff_view == NULL) {
1208 err = got_error_from_errno();
1212 err = open_diff_view(diff_view, obj1, obj2, repo);
1214 *new_view = diff_view;
1217 got_object_close(obj1);
1219 got_object_close(obj2);
1223 static const struct got_error *
1224 browse_commit(struct tog_view **new_view, int begin_x,
1225 struct commit_queue_entry *entry, struct got_repository *repo)
1227 const struct got_error *err = NULL;
1228 struct got_tree_object *tree;
1229 struct tog_view *tree_view;
1231 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1235 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1236 if (tree_view == NULL)
1237 return got_error_from_errno();
1239 err = open_tree_view(tree_view, tree, entry->id, repo);
1241 got_object_tree_close(tree);
1243 *new_view = tree_view;
1248 log_thread(void *arg)
1250 const struct got_error *err = NULL;
1252 struct tog_log_thread_args *a = arg;
1255 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1259 while (!done && !err) {
1260 err = queue_commits(a->graph, a->commits, 1, a->repo,
1263 if (err->code != GOT_ERR_ITER_COMPLETED)
1267 } else if (a->commits_needed > 0)
1268 a->commits_needed--;
1270 errcode = pthread_mutex_lock(&tog_mutex);
1272 return (void *)got_error_set_errno(errcode);
1275 a->log_complete = 1;
1276 else if (*a->quit) {
1278 a->log_complete = 1;
1279 } else if (*a->first_displayed_entry == NULL) {
1280 *a->first_displayed_entry =
1281 TAILQ_FIRST(&a->commits->head);
1282 *a->selected_entry = *a->first_displayed_entry;
1285 err = draw_commits(a->view, a->last_displayed_entry,
1286 a->selected_entry, *a->first_displayed_entry,
1287 a->commits, *a->selected, a->view->nlines,
1288 a->in_repo_path, a->commits_needed);
1291 a->commits_needed = 0;
1292 else if (a->commits_needed == 0) {
1293 errcode = pthread_cond_wait(&a->need_commits,
1296 err = got_error_set_errno(errcode);
1299 errcode = pthread_mutex_unlock(&tog_mutex);
1300 if (errcode && err == NULL)
1301 err = got_error_set_errno(errcode);
1306 static const struct got_error *
1307 stop_log_thread(struct tog_log_view_state *s)
1309 const struct got_error *err = NULL;
1314 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1316 return got_error_set_errno(errcode);
1317 errcode = pthread_mutex_unlock(&tog_mutex);
1319 return got_error_set_errno(errcode);
1320 errcode = pthread_join(s->thread, (void **)&err);
1322 return got_error_set_errno(errcode);
1323 errcode = pthread_mutex_lock(&tog_mutex);
1325 return got_error_set_errno(errcode);
1329 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1330 if (errcode && err == NULL)
1331 err = got_error_set_errno(errcode);
1333 if (s->thread_args.repo) {
1334 got_repo_close(s->thread_args.repo);
1335 s->thread_args.repo = NULL;
1338 if (s->thread_args.graph) {
1339 got_commit_graph_close(s->thread_args.graph);
1340 s->thread_args.graph = NULL;
1346 static const struct got_error *
1347 close_log_view(struct tog_view *view)
1349 const struct got_error *err = NULL;
1350 struct tog_log_view_state *s = &view->state.log;
1352 err = stop_log_thread(s);
1353 free_commits(&s->commits);
1354 free(s->in_repo_path);
1355 s->in_repo_path = NULL;
1361 static const struct got_error *
1362 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1363 struct got_repository *repo, const char *path)
1365 const struct got_error *err = NULL;
1366 struct tog_log_view_state *s = &view->state.log;
1367 struct got_repository *thread_repo = NULL;
1368 struct got_commit_graph *thread_graph = NULL;
1371 err = got_repo_map_path(&s->in_repo_path, repo, path);
1375 /* The commit queue only contains commits being displayed. */
1376 TAILQ_INIT(&s->commits.head);
1377 s->commits.ncommits = 0;
1380 s->start_id = got_object_id_dup(start_id);
1381 if (s->start_id == NULL) {
1382 err = got_error_from_errno();
1386 view->show = show_log_view;
1387 view->input = input_log_view;
1388 view->close = close_log_view;
1390 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1393 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1398 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1400 err = got_error_set_errno(errcode);
1404 s->thread_args.commits_needed = view->nlines;
1405 s->thread_args.graph = thread_graph;
1406 s->thread_args.commits = &s->commits;
1407 s->thread_args.in_repo_path = s->in_repo_path;
1408 s->thread_args.start_id = s->start_id;
1409 s->thread_args.repo = thread_repo;
1410 s->thread_args.log_complete = 0;
1411 s->thread_args.quit = &s->quit;
1412 s->thread_args.view = view;
1413 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1414 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1415 s->thread_args.selected_entry = &s->selected_entry;
1416 s->thread_args.selected = &s->selected;
1418 errcode = pthread_create(&s->thread, NULL, log_thread,
1421 err = got_error_set_errno(errcode);
1427 close_log_view(view);
1431 static const struct got_error *
1432 show_log_view(struct tog_view *view)
1434 struct tog_log_view_state *s = &view->state.log;
1436 return draw_commits(view, &s->last_displayed_entry,
1437 &s->selected_entry, s->first_displayed_entry,
1438 &s->commits, s->selected, view->nlines,
1439 s->in_repo_path, s->thread_args.commits_needed);
1442 static const struct got_error *
1443 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1444 struct tog_view **focus_view, struct tog_view *view, int ch)
1446 const struct got_error *err = NULL;
1447 struct tog_log_view_state *s = &view->state.log;
1449 struct tog_view *diff_view = NULL, *tree_view = NULL;
1458 if (s->selected > 0)
1460 if (s->selected > 0)
1462 scroll_up(&s->first_displayed_entry, 1,
1466 if (TAILQ_FIRST(&s->commits.head) ==
1467 s->first_displayed_entry) {
1471 scroll_up(&s->first_displayed_entry,
1472 view->nlines, &s->commits);
1476 if (s->selected < MIN(view->nlines - 2,
1477 s->commits.ncommits - 1)) {
1481 err = scroll_down(&s->first_displayed_entry, 1,
1482 &s->last_displayed_entry, &s->commits,
1483 &s->thread_args.log_complete,
1484 &s->thread_args.commits_needed,
1485 &s->thread_args.need_commits);
1488 struct commit_queue_entry *first;
1489 first = s->first_displayed_entry;
1490 err = scroll_down(&s->first_displayed_entry,
1491 view->nlines, &s->last_displayed_entry,
1492 &s->commits, &s->thread_args.log_complete,
1493 &s->thread_args.commits_needed,
1494 &s->thread_args.need_commits);
1495 if (first == s->first_displayed_entry &&
1496 s->selected < MIN(view->nlines - 2,
1497 s->commits.ncommits - 1)) {
1498 /* can't scroll further down */
1499 s->selected = MIN(view->nlines - 2,
1500 s->commits.ncommits - 1);
1506 if (s->selected > view->nlines - 2)
1507 s->selected = view->nlines - 2;
1508 if (s->selected > s->commits.ncommits - 1)
1509 s->selected = s->commits.ncommits - 1;
1513 if (view_is_parent_view(view))
1514 begin_x = view_split_begin_x(view->begin_x);
1515 err = open_diff_view_for_commit(&diff_view, begin_x,
1516 s->selected_entry->id, s->selected_entry->commit,
1520 if (view_is_parent_view(view)) {
1521 err = view_close_child(view);
1524 err = view_set_child(view, diff_view);
1526 view_close(diff_view);
1529 if (!view_is_splitscreen(diff_view)) {
1530 *focus_view = diff_view;
1531 view->child_focussed = 1;
1534 *new_view = diff_view;
1537 if (view_is_parent_view(view))
1538 begin_x = view_split_begin_x(view->begin_x);
1539 err = browse_commit(&tree_view, begin_x,
1540 s->selected_entry, s->repo);
1541 if (view_is_parent_view(view)) {
1542 err = view_close_child(view);
1545 err = view_set_child(view, tree_view);
1547 view_close(tree_view);
1550 *focus_view = tree_view;
1551 view->child_focussed = 1;
1553 *new_view = tree_view;
1556 if (strcmp(s->in_repo_path, "/") == 0)
1558 parent_path = dirname(s->in_repo_path);
1559 if (parent_path && strcmp(parent_path, ".") != 0) {
1560 struct tog_view *lv;
1561 err = stop_log_thread(s);
1564 lv = view_open(view->nlines, view->ncols,
1565 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1567 return got_error_from_errno();
1568 err = open_log_view(lv, s->start_id, s->repo,
1572 if (view_is_parent_view(view))
1575 view_set_child(view->parent, lv);
1588 static const struct got_error *
1589 cmd_log(int argc, char *argv[])
1591 const struct got_error *error;
1592 struct got_repository *repo = NULL;
1593 struct got_object_id *start_id = NULL;
1594 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1595 char *start_commit = NULL;
1597 struct tog_view *view;
1600 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1605 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1608 start_commit = optarg;
1611 repo_path = realpath(optarg, NULL);
1612 if (repo_path == NULL)
1613 err(1, "-r option");
1627 path = strdup(argv[0]);
1631 return got_error_from_errno();
1633 cwd = getcwd(NULL, 0);
1635 error = got_error_from_errno();
1638 if (repo_path == NULL) {
1639 repo_path = strdup(cwd);
1640 if (repo_path == NULL) {
1641 error = got_error_from_errno();
1646 error = got_repo_open(&repo, repo_path);
1650 if (start_commit == NULL) {
1651 error = get_head_commit_id(&start_id, repo);
1655 struct got_object *obj;
1656 error = got_object_open_by_id_str(&obj, repo, start_commit);
1657 if (error == NULL) {
1658 start_id = got_object_id_dup(got_object_get_id(obj));
1659 if (start_id == NULL)
1660 error = got_error_from_errno();
1667 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1669 error = got_error_from_errno();
1672 error = open_log_view(view, start_id, repo, path);
1675 error = view_loop(view);
1682 got_repo_close(repo);
1690 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1696 parse_next_line(FILE *f, size_t *len)
1701 const char delim[3] = { '\0', '\0', '\0'};
1703 line = fparseln(f, &linelen, &lineno, delim, 0);
1709 static const struct got_error *
1710 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1711 int *last_displayed_line, int *eof, int max_lines,
1714 const struct got_error *err;
1715 int nlines = 0, nprinted = 0;
1722 werase(view->window);
1725 err = format_line(&wline, &width, header, view->ncols);
1730 if (view_needs_focus_indication(view))
1731 wstandout(view->window);
1732 waddwstr(view->window, wline);
1733 if (view_needs_focus_indication(view))
1734 wstandend(view->window);
1735 if (width < view->ncols)
1736 waddch(view->window, '\n');
1744 while (nprinted < max_lines) {
1745 line = parse_next_line(f, &len);
1750 if (++nlines < *first_displayed_line) {
1755 err = format_line(&wline, &width, line, view->ncols);
1760 waddwstr(view->window, wline);
1761 if (width < view->ncols)
1762 waddch(view->window, '\n');
1763 if (++nprinted == 1)
1764 *first_displayed_line = nlines;
1769 *last_displayed_line = nlines;
1776 static const struct got_error *
1777 create_diff(struct tog_diff_view_state *s)
1779 const struct got_error *err = NULL;
1780 struct got_object *obj1 = NULL, *obj2 = NULL;
1784 err = got_object_open(&obj1, s->repo, s->id1);
1789 err = got_object_open(&obj2, s->repo, s->id2);
1795 err = got_error_from_errno();
1802 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1803 case GOT_OBJ_TYPE_BLOB:
1804 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1805 s->diff_context, s->repo, f);
1807 case GOT_OBJ_TYPE_TREE:
1808 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1809 s->diff_context, s->repo, f);
1811 case GOT_OBJ_TYPE_COMMIT:
1812 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1816 err = got_error(GOT_ERR_OBJ_TYPE);
1821 got_object_close(obj1);
1822 got_object_close(obj2);
1828 static const struct got_error *
1829 open_diff_view(struct tog_view *view, struct got_object *obj1,
1830 struct got_object *obj2, struct got_repository *repo)
1832 const struct got_error *err;
1834 if (obj1 != NULL && obj2 != NULL &&
1835 got_object_get_type(obj1) != got_object_get_type(obj2))
1836 return got_error(GOT_ERR_OBJ_TYPE);
1839 struct got_object_id *id1;
1840 id1 = got_object_id_dup(got_object_get_id(obj1));
1842 return got_error_from_errno();
1843 view->state.diff.id1 = id1;
1845 view->state.diff.id1 = NULL;
1847 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1848 if (view->state.diff.id2 == NULL) {
1849 free(view->state.diff.id1);
1850 view->state.diff.id1 = NULL;
1851 return got_error_from_errno();
1853 view->state.diff.f = NULL;
1854 view->state.diff.first_displayed_line = 1;
1855 view->state.diff.last_displayed_line = view->nlines;
1856 view->state.diff.diff_context = 3;
1857 view->state.diff.repo = repo;
1859 err = create_diff(&view->state.diff);
1861 free(view->state.diff.id1);
1862 view->state.diff.id1 = NULL;
1863 free(view->state.diff.id2);
1864 view->state.diff.id2 = NULL;
1868 view->show = show_diff_view;
1869 view->input = input_diff_view;
1870 view->close = close_diff_view;
1875 static const struct got_error *
1876 close_diff_view(struct tog_view *view)
1878 const struct got_error *err = NULL;
1880 free(view->state.diff.id1);
1881 view->state.diff.id1 = NULL;
1882 free(view->state.diff.id2);
1883 view->state.diff.id2 = NULL;
1884 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1885 err = got_error_from_errno();
1889 static const struct got_error *
1890 show_diff_view(struct tog_view *view)
1892 const struct got_error *err;
1893 struct tog_diff_view_state *s = &view->state.diff;
1894 char *id_str1 = NULL, *id_str2, *header;
1897 err = got_object_id_str(&id_str1, s->id1);
1901 err = got_object_id_str(&id_str2, s->id2);
1905 if (asprintf(&header, "diff: %s %s",
1906 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1907 err = got_error_from_errno();
1915 return draw_file(view, s->f, &s->first_displayed_line,
1916 &s->last_displayed_line, &s->eof, view->nlines,
1920 static const struct got_error *
1921 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1922 struct tog_view **focus_view, struct tog_view *view, int ch)
1924 const struct got_error *err = NULL;
1925 struct tog_diff_view_state *s = &view->state.diff;
1931 if (s->first_displayed_line > 1)
1932 s->first_displayed_line--;
1936 while (i++ < view->nlines - 1 &&
1937 s->first_displayed_line > 1)
1938 s->first_displayed_line--;
1943 s->first_displayed_line++;
1948 while (!s->eof && i++ < view->nlines - 1) {
1950 line = parse_next_line(s->f, NULL);
1951 s->first_displayed_line++;
1957 if (s->diff_context > 0) {
1959 err = create_diff(s);
1963 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1965 err = create_diff(s);
1975 static const struct got_error *
1976 cmd_diff(int argc, char *argv[])
1978 const struct got_error *error = NULL;
1979 struct got_repository *repo = NULL;
1980 struct got_object *obj1 = NULL, *obj2 = NULL;
1981 char *repo_path = NULL;
1982 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1984 struct tog_view *view;
1987 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1992 while ((ch = getopt(argc, argv, "")) != -1) {
2004 usage_diff(); /* TODO show local worktree changes */
2005 } else if (argc == 2) {
2006 repo_path = getcwd(NULL, 0);
2007 if (repo_path == NULL)
2008 return got_error_from_errno();
2009 obj_id_str1 = argv[0];
2010 obj_id_str2 = argv[1];
2011 } else if (argc == 3) {
2012 repo_path = realpath(argv[0], NULL);
2013 if (repo_path == NULL)
2014 return got_error_from_errno();
2015 obj_id_str1 = argv[1];
2016 obj_id_str2 = argv[2];
2020 error = got_repo_open(&repo, repo_path);
2025 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2029 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2033 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2035 error = got_error_from_errno();
2038 error = open_diff_view(view, obj1, obj2, repo);
2041 error = view_loop(view);
2043 got_repo_close(repo);
2045 got_object_close(obj1);
2047 got_object_close(obj2);
2055 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2060 struct tog_blame_line {
2062 struct got_object_id *id;
2065 static const struct got_error *
2066 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2067 const char *path, struct tog_blame_line *lines, int nlines,
2068 int blame_complete, int selected_line, int *first_displayed_line,
2069 int *last_displayed_line, int *eof, int max_lines)
2071 const struct got_error *err;
2072 int lineno = 0, nprinted = 0;
2077 struct tog_blame_line *blame_line;
2078 struct got_object_id *prev_id = NULL;
2081 err = got_object_id_str(&id_str, id);
2086 werase(view->window);
2088 if (asprintf(&line, "commit: %s", id_str) == -1) {
2089 err = got_error_from_errno();
2094 err = format_line(&wline, &width, line, view->ncols);
2097 if (view_needs_focus_indication(view))
2098 wstandout(view->window);
2099 waddwstr(view->window, wline);
2100 if (view_needs_focus_indication(view))
2101 wstandend(view->window);
2104 if (width < view->ncols)
2105 waddch(view->window, '\n');
2107 if (asprintf(&line, "[%d/%d] %s%s",
2108 *first_displayed_line - 1 + selected_line, nlines,
2109 blame_complete ? "" : "annotating ", path) == -1) {
2111 return got_error_from_errno();
2114 err = format_line(&wline, &width, line, view->ncols);
2119 waddwstr(view->window, wline);
2122 if (width < view->ncols)
2123 waddch(view->window, '\n');
2126 while (nprinted < max_lines - 2) {
2127 line = parse_next_line(f, &len);
2132 if (++lineno < *first_displayed_line) {
2137 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2138 err = format_line(&wline, &width, line, wlimit);
2144 if (view->focussed && nprinted == selected_line - 1)
2145 wstandout(view->window);
2147 blame_line = &lines[lineno - 1];
2148 if (blame_line->annotated && prev_id &&
2149 got_object_id_cmp(prev_id, blame_line->id) == 0)
2150 waddstr(view->window, " ");
2151 else if (blame_line->annotated) {
2153 err = got_object_id_str(&id_str, blame_line->id);
2159 wprintw(view->window, "%.8s ", id_str);
2161 prev_id = blame_line->id;
2163 waddstr(view->window, "........ ");
2167 waddwstr(view->window, wline);
2168 while (width < wlimit) {
2169 waddch(view->window, ' ');
2172 if (view->focussed && nprinted == selected_line - 1)
2173 wstandend(view->window);
2174 if (++nprinted == 1)
2175 *first_displayed_line = lineno;
2180 *last_displayed_line = lineno;
2187 static const struct got_error *
2188 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2190 const struct got_error *err = NULL;
2191 struct tog_blame_cb_args *a = arg;
2192 struct tog_blame_line *line;
2195 if (nlines != a->nlines ||
2196 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2197 return got_error(GOT_ERR_RANGE);
2199 errcode = pthread_mutex_lock(&tog_mutex);
2201 return got_error_set_errno(errcode);
2203 if (*a->quit) { /* user has quit the blame view */
2204 err = got_error(GOT_ERR_ITER_COMPLETED);
2209 goto done; /* no change in this commit */
2211 line = &a->lines[lineno - 1];
2212 if (line->annotated)
2215 line->id = got_object_id_dup(id);
2216 if (line->id == NULL) {
2217 err = got_error_from_errno();
2220 line->annotated = 1;
2222 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2223 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2224 a->last_displayed_line, a->eof, a->view->nlines);
2226 errcode = pthread_mutex_unlock(&tog_mutex);
2228 err = got_error_set_errno(errcode);
2233 blame_thread(void *arg)
2235 const struct got_error *err;
2236 struct tog_blame_thread_args *ta = arg;
2237 struct tog_blame_cb_args *a = ta->cb_args;
2240 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2241 blame_cb, ta->cb_args);
2243 errcode = pthread_mutex_lock(&tog_mutex);
2245 return (void *)got_error_set_errno(errcode);
2247 got_repo_close(ta->repo);
2251 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2252 a->lines, a->nlines, 1, *a->selected_line,
2253 a->first_displayed_line, a->last_displayed_line, a->eof,
2257 errcode = pthread_mutex_unlock(&tog_mutex);
2258 if (errcode && err == NULL)
2259 err = got_error_set_errno(errcode);
2264 static struct got_object_id *
2265 get_selected_commit_id(struct tog_blame_line *lines,
2266 int first_displayed_line, int selected_line)
2268 struct tog_blame_line *line;
2270 line = &lines[first_displayed_line - 1 + selected_line - 1];
2271 if (!line->annotated)
2277 static const struct got_error *
2278 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2279 struct tog_blame_line *lines, int first_displayed_line,
2280 int selected_line, struct got_repository *repo)
2282 const struct got_error *err = NULL;
2283 struct got_commit_object *commit = NULL;
2284 struct got_object_id *selected_id;
2285 struct got_object_qid *pid;
2290 selected_id = get_selected_commit_id(lines,
2291 first_displayed_line, selected_line);
2292 if (selected_id == NULL)
2295 err = got_object_open(obj, repo, selected_id);
2299 err = got_object_commit_open(&commit, repo, *obj);
2303 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2305 err = got_object_open(pobj, repo, pid->id);
2311 got_object_commit_close(commit);
2315 static const struct got_error *
2316 stop_blame(struct tog_blame *blame)
2318 const struct got_error *err = NULL;
2321 if (blame->thread) {
2323 errcode = pthread_mutex_unlock(&tog_mutex);
2325 return got_error_set_errno(errcode);
2326 errcode = pthread_join(blame->thread, (void **)&err);
2328 return got_error_set_errno(errcode);
2329 errcode = pthread_mutex_lock(&tog_mutex);
2331 return got_error_set_errno(errcode);
2332 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2334 blame->thread = NULL;
2336 if (blame->thread_args.repo) {
2337 got_repo_close(blame->thread_args.repo);
2338 blame->thread_args.repo = NULL;
2344 for (i = 0; i < blame->nlines; i++)
2345 free(blame->lines[i].id);
2347 blame->lines = NULL;
2348 free(blame->cb_args.commit_id);
2349 blame->cb_args.commit_id = NULL;
2354 static const struct got_error *
2355 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2356 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2357 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2358 struct got_repository *repo)
2360 const struct got_error *err = NULL;
2361 struct got_blob_object *blob = NULL;
2362 struct got_repository *thread_repo = NULL;
2363 struct got_object_id *obj_id = NULL;
2364 struct got_object *obj = NULL;
2367 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2371 err = got_object_open(&obj, repo, obj_id);
2375 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2376 err = got_error(GOT_ERR_OBJ_TYPE);
2380 err = got_object_blob_open(&blob, repo, obj, 8192);
2383 blame->f = got_opentemp();
2384 if (blame->f == NULL) {
2385 err = got_error_from_errno();
2388 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2393 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2394 if (blame->lines == NULL) {
2395 err = got_error_from_errno();
2399 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2403 blame->cb_args.view = view;
2404 blame->cb_args.lines = blame->lines;
2405 blame->cb_args.nlines = blame->nlines;
2406 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2407 if (blame->cb_args.commit_id == NULL) {
2408 err = got_error_from_errno();
2411 blame->cb_args.f = blame->f;
2412 blame->cb_args.path = path;
2413 blame->cb_args.first_displayed_line = first_displayed_line;
2414 blame->cb_args.selected_line = selected_line;
2415 blame->cb_args.last_displayed_line = last_displayed_line;
2416 blame->cb_args.quit = done;
2417 blame->cb_args.eof = eof;
2419 blame->thread_args.path = path;
2420 blame->thread_args.repo = thread_repo;
2421 blame->thread_args.cb_args = &blame->cb_args;
2422 blame->thread_args.complete = blame_complete;
2423 *blame_complete = 0;
2425 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2426 &blame->thread_args);
2428 err = got_error_set_errno(errcode);
2434 got_object_blob_close(blob);
2437 got_object_close(obj);
2443 static const struct got_error *
2444 open_blame_view(struct tog_view *view, char *path,
2445 struct got_object_id *commit_id, struct got_repository *repo)
2447 const struct got_error *err = NULL;
2448 struct tog_blame_view_state *s = &view->state.blame;
2450 SIMPLEQ_INIT(&s->blamed_commits);
2452 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2456 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2457 s->first_displayed_line = 1;
2458 s->last_displayed_line = view->nlines;
2459 s->selected_line = 1;
2460 s->blame_complete = 0;
2462 if (s->path == NULL)
2463 return got_error_from_errno();
2465 s->commit_id = commit_id;
2466 memset(&s->blame, 0, sizeof(s->blame));
2468 view->show = show_blame_view;
2469 view->input = input_blame_view;
2470 view->close = close_blame_view;
2472 return run_blame(&s->blame, view, &s->blame_complete,
2473 &s->first_displayed_line, &s->last_displayed_line,
2474 &s->selected_line, &s->done, &s->eof, s->path,
2475 s->blamed_commit->id, s->repo);
2478 static const struct got_error *
2479 close_blame_view(struct tog_view *view)
2481 const struct got_error *err = NULL;
2482 struct tog_blame_view_state *s = &view->state.blame;
2484 if (s->blame.thread)
2485 err = stop_blame(&s->blame);
2487 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2488 struct got_object_qid *blamed_commit;
2489 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2490 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2491 got_object_qid_free(blamed_commit);
2499 static const struct got_error *
2500 show_blame_view(struct tog_view *view)
2502 const struct got_error *err = NULL;
2503 struct tog_blame_view_state *s = &view->state.blame;
2505 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2506 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2507 s->selected_line, &s->first_displayed_line,
2508 &s->last_displayed_line, &s->eof, view->nlines);
2514 static const struct got_error *
2515 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2516 struct tog_view **focus_view, struct tog_view *view, int ch)
2518 const struct got_error *err = NULL, *thread_err = NULL;
2519 struct got_object *obj = NULL, *pobj = NULL;
2520 struct tog_view *diff_view;
2521 struct tog_blame_view_state *s = &view->state.blame;
2530 if (s->selected_line > 1)
2532 else if (s->selected_line == 1 &&
2533 s->first_displayed_line > 1)
2534 s->first_displayed_line--;
2537 if (s->first_displayed_line == 1) {
2538 s->selected_line = 1;
2541 if (s->first_displayed_line > view->nlines - 2)
2542 s->first_displayed_line -=
2545 s->first_displayed_line = 1;
2549 if (s->selected_line < view->nlines - 2 &&
2550 s->first_displayed_line +
2551 s->selected_line <= s->blame.nlines)
2553 else if (s->last_displayed_line <
2555 s->first_displayed_line++;
2559 struct got_object_id *id;
2560 id = get_selected_commit_id(s->blame.lines,
2561 s->first_displayed_line, s->selected_line);
2562 if (id == NULL || got_object_id_cmp(id,
2563 s->blamed_commit->id) == 0)
2565 err = open_selected_commit(&pobj, &obj,
2566 s->blame.lines, s->first_displayed_line,
2567 s->selected_line, s->repo);
2570 if (pobj == NULL && obj == NULL)
2572 if (ch == 'p' && pobj == NULL)
2575 thread_err = stop_blame(&s->blame);
2579 id = got_object_get_id(ch == 'b' ? obj : pobj);
2580 got_object_close(obj);
2583 got_object_close(pobj);
2586 err = got_object_qid_alloc(&s->blamed_commit, id);
2589 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2590 s->blamed_commit, entry);
2591 err = run_blame(&s->blame, view, &s->blame_complete,
2592 &s->first_displayed_line, &s->last_displayed_line,
2593 &s->selected_line, &s->done, &s->eof,
2594 s->path, s->blamed_commit->id, s->repo);
2600 struct got_object_qid *first;
2601 first = SIMPLEQ_FIRST(&s->blamed_commits);
2602 if (!got_object_id_cmp(first->id, s->commit_id))
2605 thread_err = stop_blame(&s->blame);
2609 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2610 got_object_qid_free(s->blamed_commit);
2612 SIMPLEQ_FIRST(&s->blamed_commits);
2613 err = run_blame(&s->blame, view, &s->blame_complete,
2614 &s->first_displayed_line, &s->last_displayed_line,
2615 &s->selected_line, &s->done, &s->eof, s->path,
2616 s->blamed_commit->id, s->repo);
2623 err = open_selected_commit(&pobj, &obj,
2624 s->blame.lines, s->first_displayed_line,
2625 s->selected_line, s->repo);
2628 if (pobj == NULL && obj == NULL)
2631 if (view_is_parent_view(view))
2632 begin_x = view_split_begin_x(view->begin_x);
2633 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2634 if (diff_view == NULL) {
2635 err = got_error_from_errno();
2638 err = open_diff_view(diff_view, pobj, obj, s->repo);
2640 view_close(diff_view);
2643 if (view_is_parent_view(view)) {
2644 err = view_close_child(view);
2647 err = view_set_child(view, diff_view);
2649 view_close(diff_view);
2652 if (!view_is_splitscreen(diff_view)) {
2653 *focus_view = diff_view;
2654 view->child_focussed = 1;
2657 *new_view = diff_view;
2659 got_object_close(pobj);
2662 got_object_close(obj);
2669 if (s->last_displayed_line >= s->blame.nlines &&
2670 s->selected_line < view->nlines - 2) {
2671 s->selected_line = MIN(s->blame.nlines,
2675 if (s->last_displayed_line + view->nlines - 2
2677 s->first_displayed_line +=
2680 s->first_displayed_line =
2685 if (s->selected_line > view->nlines - 2) {
2686 s->selected_line = MIN(s->blame.nlines,
2695 got_object_close(pobj);
2696 return thread_err ? thread_err : err;
2699 static const struct got_error *
2700 cmd_blame(int argc, char *argv[])
2702 const struct got_error *error;
2703 struct got_repository *repo = NULL;
2704 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2705 struct got_object_id *commit_id = NULL;
2706 char *commit_id_str = NULL;
2708 struct tog_view *view;
2711 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2716 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2719 commit_id_str = optarg;
2722 repo_path = realpath(optarg, NULL);
2723 if (repo_path == NULL)
2724 err(1, "-r option");
2740 cwd = getcwd(NULL, 0);
2742 error = got_error_from_errno();
2745 if (repo_path == NULL) {
2746 repo_path = strdup(cwd);
2747 if (repo_path == NULL) {
2748 error = got_error_from_errno();
2754 error = got_repo_open(&repo, repo_path);
2758 error = got_repo_map_path(&in_repo_path, repo, path);
2762 if (commit_id_str == NULL) {
2763 struct got_reference *head_ref;
2764 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2767 error = got_ref_resolve(&commit_id, repo, head_ref);
2768 got_ref_close(head_ref);
2770 struct got_object *obj;
2771 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2774 commit_id = got_object_id_dup(got_object_get_id(obj));
2775 if (commit_id == NULL)
2776 error = got_error_from_errno();
2777 got_object_close(obj);
2782 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2784 error = got_error_from_errno();
2787 error = open_blame_view(view, in_repo_path, commit_id, repo);
2790 error = view_loop(view);
2796 got_repo_close(repo);
2800 static const struct got_error *
2801 draw_tree_entries(struct tog_view *view,
2802 struct got_tree_entry **first_displayed_entry,
2803 struct got_tree_entry **last_displayed_entry,
2804 struct got_tree_entry **selected_entry, int *ndisplayed,
2805 const char *label, int show_ids, const char *parent_path,
2806 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2808 const struct got_error *err = NULL;
2809 struct got_tree_entry *te;
2815 werase(view->window);
2820 err = format_line(&wline, &width, label, view->ncols);
2823 if (view_needs_focus_indication(view))
2824 wstandout(view->window);
2825 waddwstr(view->window, wline);
2826 if (view_needs_focus_indication(view))
2827 wstandend(view->window);
2830 if (width < view->ncols)
2831 waddch(view->window, '\n');
2834 err = format_line(&wline, &width, parent_path, view->ncols);
2837 waddwstr(view->window, wline);
2840 if (width < view->ncols)
2841 waddch(view->window, '\n');
2844 waddch(view->window, '\n');
2848 te = SIMPLEQ_FIRST(&entries->head);
2849 if (*first_displayed_entry == NULL) {
2850 if (selected == 0) {
2852 wstandout(view->window);
2853 *selected_entry = NULL;
2855 waddstr(view->window, " ..\n"); /* parent directory */
2856 if (selected == 0 && view->focussed)
2857 wstandend(view->window);
2864 while (te != *first_displayed_entry)
2865 te = SIMPLEQ_NEXT(te, entry);
2869 char *line = NULL, *id_str = NULL;
2872 err = got_object_id_str(&id_str, te->id);
2874 return got_error_from_errno();
2876 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2877 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2879 return got_error_from_errno();
2882 err = format_line(&wline, &width, line, view->ncols);
2887 if (n == selected) {
2889 wstandout(view->window);
2890 *selected_entry = te;
2892 waddwstr(view->window, wline);
2893 if (width < view->ncols)
2894 waddch(view->window, '\n');
2895 if (n == selected && view->focussed)
2896 wstandend(view->window);
2902 *last_displayed_entry = te;
2905 te = SIMPLEQ_NEXT(te, entry);
2912 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2913 const struct got_tree_entries *entries, int isroot)
2915 struct got_tree_entry *te, *prev;
2918 if (*first_displayed_entry == NULL)
2921 te = SIMPLEQ_FIRST(&entries->head);
2922 if (*first_displayed_entry == te) {
2924 *first_displayed_entry = NULL;
2928 /* XXX this is stupid... switch to TAILQ? */
2929 for (i = 0; i < maxscroll; i++) {
2930 while (te != *first_displayed_entry) {
2932 te = SIMPLEQ_NEXT(te, entry);
2934 *first_displayed_entry = prev;
2935 te = SIMPLEQ_FIRST(&entries->head);
2937 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2938 *first_displayed_entry = NULL;
2942 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2943 struct got_tree_entry *last_displayed_entry,
2944 const struct got_tree_entries *entries)
2946 struct got_tree_entry *next;
2949 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2952 if (*first_displayed_entry)
2953 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2955 next = SIMPLEQ_FIRST(&entries->head);
2957 *first_displayed_entry = next;
2958 if (++n >= maxscroll)
2960 next = SIMPLEQ_NEXT(next, entry);
2964 static const struct got_error *
2965 tree_entry_path(char **path, struct tog_parent_trees *parents,
2966 struct got_tree_entry *te)
2968 const struct got_error *err = NULL;
2969 struct tog_parent_tree *pt;
2970 size_t len = 2; /* for leading slash and NUL */
2972 TAILQ_FOREACH(pt, parents, entry)
2973 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2975 len += strlen(te->name);
2977 *path = calloc(1, len);
2979 return got_error_from_errno();
2982 pt = TAILQ_LAST(parents, tog_parent_trees);
2984 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2985 err = got_error(GOT_ERR_NO_SPACE);
2988 if (strlcat(*path, "/", len) >= len) {
2989 err = got_error(GOT_ERR_NO_SPACE);
2992 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2995 if (strlcat(*path, te->name, len) >= len) {
2996 err = got_error(GOT_ERR_NO_SPACE);
3008 static const struct got_error *
3009 blame_tree_entry(struct tog_view **new_view, int begin_x,
3010 struct got_tree_entry *te, struct tog_parent_trees *parents,
3011 struct got_object_id *commit_id, struct got_repository *repo)
3013 const struct got_error *err = NULL;
3015 struct tog_view *blame_view;
3017 err = tree_entry_path(&path, parents, te);
3021 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3022 if (blame_view == NULL)
3023 return got_error_from_errno();
3025 err = open_blame_view(blame_view, path, commit_id, repo);
3027 view_close(blame_view);
3030 *new_view = blame_view;
3034 static const struct got_error *
3035 log_tree_entry(struct tog_view **new_view, int begin_x,
3036 struct got_tree_entry *te, struct tog_parent_trees *parents,
3037 struct got_object_id *commit_id, struct got_repository *repo)
3039 struct tog_view *log_view;
3040 const struct got_error *err = NULL;
3043 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3044 if (log_view == NULL)
3045 return got_error_from_errno();
3047 err = tree_entry_path(&path, parents, te);
3051 err = open_log_view(log_view, commit_id, repo, path);
3053 view_close(log_view);
3055 *new_view = log_view;
3060 static const struct got_error *
3061 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3062 struct got_object_id *commit_id, struct got_repository *repo)
3064 const struct got_error *err = NULL;
3065 char *commit_id_str = NULL;
3066 struct tog_tree_view_state *s = &view->state.tree;
3068 TAILQ_INIT(&s->parents);
3070 err = got_object_id_str(&commit_id_str, commit_id);
3074 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3075 err = got_error_from_errno();
3079 s->root = s->tree = root;
3080 s->entries = got_object_tree_get_entries(root);
3081 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3082 s->commit_id = got_object_id_dup(commit_id);
3083 if (s->commit_id == NULL) {
3084 err = got_error_from_errno();
3089 view->show = show_tree_view;
3090 view->input = input_tree_view;
3091 view->close = close_tree_view;
3093 free(commit_id_str);
3095 free(s->tree_label);
3096 s->tree_label = NULL;
3101 static const struct got_error *
3102 close_tree_view(struct tog_view *view)
3104 struct tog_tree_view_state *s = &view->state.tree;
3106 free(s->tree_label);
3107 s->tree_label = NULL;
3109 s->commit_id = NULL;
3110 while (!TAILQ_EMPTY(&s->parents)) {
3111 struct tog_parent_tree *parent;
3112 parent = TAILQ_FIRST(&s->parents);
3113 TAILQ_REMOVE(&s->parents, parent, entry);
3117 if (s->tree != s->root)
3118 got_object_tree_close(s->tree);
3119 got_object_tree_close(s->root);
3124 static const struct got_error *
3125 show_tree_view(struct tog_view *view)
3127 const struct got_error *err = NULL;
3128 struct tog_tree_view_state *s = &view->state.tree;
3131 err = tree_entry_path(&parent_path, &s->parents, NULL);
3135 err = draw_tree_entries(view, &s->first_displayed_entry,
3136 &s->last_displayed_entry, &s->selected_entry,
3137 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3138 s->entries, s->selected, view->nlines, s->tree == s->root);
3145 static const struct got_error *
3146 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3147 struct tog_view **focus_view, struct tog_view *view, int ch)
3149 const struct got_error *err = NULL;
3150 struct tog_tree_view_state *s = &view->state.tree;
3151 struct tog_view *log_view;
3156 s->show_ids = !s->show_ids;
3159 if (!s->selected_entry)
3161 if (view_is_parent_view(view))
3162 begin_x = view_split_begin_x(view->begin_x);
3163 err = log_tree_entry(&log_view, begin_x,
3164 s->selected_entry, &s->parents,
3165 s->commit_id, s->repo);
3166 if (view_is_parent_view(view)) {
3167 err = view_close_child(view);
3170 err = view_set_child(view, log_view);
3172 view_close(log_view);
3175 *focus_view = log_view;
3176 view->child_focussed = 1;
3178 *new_view = log_view;
3182 if (s->selected > 0)
3184 if (s->selected > 0)
3186 tree_scroll_up(&s->first_displayed_entry, 1,
3187 s->entries, s->tree == s->root);
3190 if (SIMPLEQ_FIRST(&s->entries->head) ==
3191 s->first_displayed_entry) {
3192 if (s->tree != s->root)
3193 s->first_displayed_entry = NULL;
3197 tree_scroll_up(&s->first_displayed_entry,
3198 view->nlines, s->entries,
3199 s->tree == s->root);
3203 if (s->selected < s->ndisplayed - 1) {
3207 tree_scroll_down(&s->first_displayed_entry, 1,
3208 s->last_displayed_entry, s->entries);
3211 tree_scroll_down(&s->first_displayed_entry,
3212 view->nlines, s->last_displayed_entry,
3214 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3217 /* can't scroll any further; move cursor down */
3218 if (s->selected < s->ndisplayed - 1)
3219 s->selected = s->ndisplayed - 1;
3223 if (s->selected_entry == NULL) {
3224 struct tog_parent_tree *parent;
3226 /* user selected '..' */
3227 if (s->tree == s->root)
3229 parent = TAILQ_FIRST(&s->parents);
3230 TAILQ_REMOVE(&s->parents, parent,
3232 got_object_tree_close(s->tree);
3233 s->tree = parent->tree;
3235 got_object_tree_get_entries(s->tree);
3236 s->first_displayed_entry =
3237 parent->first_displayed_entry;
3239 parent->selected_entry;
3240 s->selected = parent->selected;
3242 } else if (S_ISDIR(s->selected_entry->mode)) {
3243 struct tog_parent_tree *parent;
3244 struct got_tree_object *child;
3245 err = got_object_open_as_tree(&child,
3246 s->repo, s->selected_entry->id);
3249 parent = calloc(1, sizeof(*parent));
3250 if (parent == NULL) {
3251 err = got_error_from_errno();
3254 parent->tree = s->tree;
3255 parent->first_displayed_entry =
3256 s->first_displayed_entry;
3257 parent->selected_entry = s->selected_entry;
3258 parent->selected = s->selected;
3259 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3262 got_object_tree_get_entries(s->tree);
3264 s->first_displayed_entry = NULL;
3265 } else if (S_ISREG(s->selected_entry->mode)) {
3266 struct tog_view *blame_view;
3267 int begin_x = view_is_parent_view(view) ?
3268 view_split_begin_x(view->begin_x) : 0;
3270 err = blame_tree_entry(&blame_view, begin_x,
3271 s->selected_entry, &s->parents, s->commit_id,
3275 if (view_is_parent_view(view)) {
3276 err = view_close_child(view);
3279 err = view_set_child(view, blame_view);
3281 view_close(blame_view);
3284 *focus_view = blame_view;
3285 view->child_focussed = 1;
3287 *new_view = blame_view;
3291 if (s->selected > view->nlines)
3292 s->selected = s->ndisplayed - 1;
3305 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3310 static const struct got_error *
3311 cmd_tree(int argc, char *argv[])
3313 const struct got_error *error;
3314 struct got_repository *repo = NULL;
3315 char *repo_path = NULL;
3316 struct got_object_id *commit_id = NULL;
3317 char *commit_id_arg = NULL;
3318 struct got_commit_object *commit = NULL;
3319 struct got_tree_object *tree = NULL;
3321 struct tog_view *view;
3324 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3329 while ((ch = getopt(argc, argv, "c:")) != -1) {
3332 commit_id_arg = optarg;
3344 repo_path = getcwd(NULL, 0);
3345 if (repo_path == NULL)
3346 return got_error_from_errno();
3347 } else if (argc == 1) {
3348 repo_path = realpath(argv[0], NULL);
3349 if (repo_path == NULL)
3350 return got_error_from_errno();
3354 error = got_repo_open(&repo, repo_path);
3359 if (commit_id_arg == NULL) {
3360 error = get_head_commit_id(&commit_id, repo);
3364 struct got_object *obj;
3365 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3366 if (error == NULL) {
3367 commit_id = got_object_id_dup(got_object_get_id(obj));
3368 if (commit_id == NULL)
3369 error = got_error_from_errno();
3375 error = got_object_open_as_commit(&commit, repo, commit_id);
3379 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3383 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3385 error = got_error_from_errno();
3388 error = open_tree_view(view, tree, commit_id, repo);
3391 error = view_loop(view);
3395 got_object_commit_close(commit);
3397 got_object_tree_close(tree);
3399 got_repo_close(repo);
3407 halfdelay(1); /* Do fast refresh while initial view is loading. */
3410 intrflush(stdscr, FALSE);
3411 keypad(stdscr, TRUE);
3420 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3421 "Available commands:\n", getprogname());
3422 for (i = 0; i < nitems(tog_commands); i++) {
3423 struct tog_cmd *cmd = &tog_commands[i];
3424 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3430 make_argv(const char *arg0, const char *arg1)
3433 int argc = (arg1 == NULL ? 1 : 2);
3435 argv = calloc(argc, sizeof(char *));
3438 argv[0] = strdup(arg0);
3439 if (argv[0] == NULL)
3442 argv[1] = strdup(arg1);
3443 if (argv[1] == NULL)
3451 main(int argc, char *argv[])
3453 const struct got_error *error = NULL;
3454 struct tog_cmd *cmd = NULL;
3456 char **cmd_argv = NULL;
3458 setlocale(LC_ALL, "");
3460 while ((ch = getopt(argc, argv, "h")) != -1) {
3479 /* Build an argument vector which runs a default command. */
3480 cmd = &tog_commands[0];
3481 cmd_argv = make_argv(cmd->name, NULL);
3486 /* Did the user specific a command? */
3487 for (i = 0; i < nitems(tog_commands); i++) {
3488 if (strncmp(tog_commands[i].name, argv[0],
3489 strlen(argv[0])) == 0) {
3490 cmd = &tog_commands[i];
3492 tog_commands[i].cmd_usage();
3497 /* Did the user specify a repository? */
3498 char *repo_path = realpath(argv[0], NULL);
3500 struct got_repository *repo;
3501 error = got_repo_open(&repo, repo_path);
3503 got_repo_close(repo);
3505 error = got_error_from_errno();
3508 fprintf(stderr, "%s: '%s' is not a "
3509 "known command\n", getprogname(),
3513 fprintf(stderr, "%s: '%s' is neither a known "
3514 "command nor a path to a repository\n",
3515 getprogname(), argv[0]);
3519 cmd = &tog_commands[0];
3520 cmd_argv = make_argv(cmd->name, repo_path);
3528 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3535 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);