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);
551 err = view->input(new, dead, focus, view, ch);
559 view_vborder(struct tog_view *view)
562 struct tog_view *view_above;
565 return view_vborder(view->parent);
567 panel = panel_above(view->panel);
571 view_above = panel_userptr(panel);
572 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
573 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
577 view_needs_focus_indication(struct tog_view *view)
579 if (view_is_parent_view(view)) {
580 if (view->child == NULL || view->child_focussed)
582 if (!view_is_splitscreen(view->child))
584 } else if (!view_is_splitscreen(view))
587 return view->focussed;
590 static const struct got_error *
591 view_loop(struct tog_view *view)
593 const struct got_error *err = NULL;
594 struct tog_view_list_head views;
595 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
596 int done = 0, errcode;
598 errcode = pthread_mutex_lock(&tog_mutex);
600 return got_error_set_errno(errcode);
603 TAILQ_INSERT_HEAD(&views, view, entry);
607 err = view->show(view);
612 while (!TAILQ_EMPTY(&views) && !done) {
613 err = view_input(&new_view, &dead_view, &focus_view, &done,
618 struct tog_view *prev = NULL;
620 if (view_is_parent_view(dead_view))
621 prev = TAILQ_PREV(dead_view,
622 tog_view_list_head, entry);
626 if (dead_view->parent)
627 dead_view->parent->child = NULL;
629 TAILQ_REMOVE(&views, dead_view, entry);
631 err = view_close(dead_view);
632 if (err || dead_view == main_view)
635 if (view == dead_view) {
640 else if (!TAILQ_EMPTY(&views))
641 view = TAILQ_LAST(&views,
646 if (view->child && view->child_focussed)
647 focus_view = view->child;
654 struct tog_view *v, *t;
655 /* Only allow one parent view per type. */
656 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
657 if (v->type != new_view->type)
659 TAILQ_REMOVE(&views, v, entry);
667 TAILQ_INSERT_TAIL(&views, new_view, entry);
668 if (focus_view == NULL)
669 focus_view = new_view;
672 show_panel(focus_view->panel);
675 focus_view->focussed = 1;
678 show_panel(new_view->panel);
679 if (view->child && view_is_splitscreen(view->child))
680 show_panel(view->child->panel);
683 if (focus_view == NULL) {
685 focus_view->focussed = 1;
688 err = view->parent->show(view->parent);
692 err = view->show(view);
696 err = view->child->show(view->child);
705 while (!TAILQ_EMPTY(&views)) {
706 view = TAILQ_FIRST(&views);
707 TAILQ_REMOVE(&views, view, entry);
711 errcode = pthread_mutex_unlock(&tog_mutex);
713 return got_error_set_errno(errcode);
723 "usage: %s log [-c commit] [-r repository-path] [path]\n",
728 /* Create newly allocated wide-character string equivalent to a byte string. */
729 static const struct got_error *
730 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
733 const struct got_error *err = NULL;
736 *wlen = mbstowcs(NULL, s, 0);
737 if (*wlen == (size_t)-1) {
740 return got_error_from_errno();
742 /* byte string invalid in current encoding; try to "fix" it */
743 err = got_mbsavis(&vis, &vislen, s);
746 *wlen = mbstowcs(NULL, vis, 0);
747 if (*wlen == (size_t)-1) {
748 err = got_error_from_errno(); /* give up */
753 *ws = calloc(*wlen + 1, sizeof(*ws));
755 err = got_error_from_errno();
759 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
760 err = got_error_from_errno();
771 /* Format a line for display, ensuring that it won't overflow a width limit. */
772 static const struct got_error *
773 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
775 const struct got_error *err = NULL;
777 wchar_t *wline = NULL;
784 err = mbs2ws(&wline, &wlen, line);
789 while (i < wlen && cols < wlimit) {
790 int width = wcwidth(wline[i]);
797 if (cols + width <= wlimit)
802 if (wline[i] == L'\t')
803 cols += TABSIZE - ((cols + 1) % TABSIZE);
807 err = got_error_from_errno();
822 static const struct got_error *
823 draw_commit(struct tog_view *view, struct got_commit_object *commit,
824 struct got_object_id *id)
826 const struct got_error *err = NULL;
827 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
828 char *logmsg0 = NULL, *logmsg = NULL;
829 char *author0 = NULL, *author = NULL;
830 wchar_t *wlogmsg = NULL, *wauthor = NULL;
831 int author_width, logmsg_width;
832 char *newline, *smallerthan;
835 static const size_t date_display_cols = 9;
836 static const size_t author_display_cols = 16;
837 const int avail = view->ncols;
839 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
840 &commit->tm_committer) >= sizeof(datebuf))
841 return got_error(GOT_ERR_NO_SPACE);
843 if (avail < date_display_cols)
844 limit = MIN(sizeof(datebuf) - 1, avail);
846 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
847 waddnstr(view->window, datebuf, limit);
852 author0 = strdup(commit->author);
853 if (author0 == NULL) {
854 err = got_error_from_errno();
858 smallerthan = strchr(author, '<');
862 char *at = strchr(author, '@');
867 err = format_line(&wauthor, &author_width, author, limit);
870 waddwstr(view->window, wauthor);
872 while (col <= avail && author_width < author_display_cols + 1) {
873 waddch(view->window, ' ');
880 logmsg0 = strdup(commit->logmsg);
881 if (logmsg0 == NULL) {
882 err = got_error_from_errno();
886 while (*logmsg == '\n')
888 newline = strchr(logmsg, '\n');
892 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
895 waddwstr(view->window, wlogmsg);
897 while (col <= avail) {
898 waddch(view->window, ' ');
910 static struct commit_queue_entry *
911 alloc_commit_queue_entry(struct got_commit_object *commit,
912 struct got_object_id *id)
914 struct commit_queue_entry *entry;
916 entry = calloc(1, sizeof(*entry));
921 entry->commit = commit;
926 pop_commit(struct commit_queue *commits)
928 struct commit_queue_entry *entry;
930 entry = TAILQ_FIRST(&commits->head);
931 TAILQ_REMOVE(&commits->head, entry, entry);
932 got_object_commit_close(entry->commit);
934 /* Don't free entry->id! It is owned by the commit graph. */
939 free_commits(struct commit_queue *commits)
941 while (!TAILQ_EMPTY(&commits->head))
945 static const struct got_error *
946 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
947 int minqueue, struct got_repository *repo, const char *path)
949 const struct got_error *err = NULL;
953 * We keep all commits open throughout the lifetime of the log
954 * view in order to avoid having to re-fetch commits from disk
955 * while updating the display.
957 while (nqueued < minqueue) {
958 struct got_object_id *id;
959 struct got_commit_object *commit;
960 struct commit_queue_entry *entry;
963 err = got_commit_graph_iter_next(&id, graph);
965 if (err->code != GOT_ERR_ITER_NEED_MORE)
967 err = got_commit_graph_fetch_commits(graph,
977 err = got_object_open_as_commit(&commit, repo, id);
980 entry = alloc_commit_queue_entry(commit, id);
982 err = got_error_from_errno();
986 errcode = pthread_mutex_lock(&tog_mutex);
988 err = got_error_set_errno(errcode);
992 entry->idx = commits->ncommits;
993 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
997 errcode = pthread_mutex_unlock(&tog_mutex);
998 if (errcode && err == NULL)
999 err = got_error_set_errno(errcode);
1005 static const struct got_error *
1006 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1008 const struct got_error *err = NULL;
1009 struct got_reference *head_ref;
1013 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1017 err = got_ref_resolve(head_id, repo, head_ref);
1018 got_ref_close(head_ref);
1027 static const struct got_error *
1028 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1029 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1030 struct commit_queue *commits, int selected_idx, int limit,
1031 const char *path, int commits_needed)
1033 const struct got_error *err = NULL;
1034 struct commit_queue_entry *entry;
1035 int ncommits, width;
1036 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1042 if (ncommits == selected_idx) {
1046 entry = TAILQ_NEXT(entry, entry);
1051 err = got_object_id_str(&id_str, (*selected)->id);
1056 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1057 entry ? entry->idx + 1 : 0, commits->ncommits,
1058 commits_needed == 0 ? "" : " loading...") == -1)
1059 return got_error_from_errno();
1061 if (path && strcmp(path, "/") != 0) {
1062 if (asprintf(&header, "commit: %s %s%s",
1063 id_str ? id_str : "........................................",
1064 path, ncommits_str) == -1) {
1065 err = got_error_from_errno();
1069 } else if (asprintf(&header, "commit: %s%s",
1070 id_str ? id_str : "........................................",
1071 ncommits_str) == -1) {
1072 err = got_error_from_errno();
1076 err = format_line(&wline, &width, header, view->ncols);
1080 werase(view->window);
1082 if (view_needs_focus_indication(view))
1083 wstandout(view->window);
1084 waddwstr(view->window, wline);
1085 while (width < view->ncols) {
1086 waddch(view->window, ' ');
1089 if (view_needs_focus_indication(view))
1090 wstandend(view->window);
1099 if (ncommits >= limit - 1)
1101 if (view->focussed && ncommits == selected_idx)
1102 wstandout(view->window);
1103 err = draw_commit(view, entry->commit, entry->id);
1104 if (view->focussed && ncommits == selected_idx)
1105 wstandend(view->window);
1110 entry = TAILQ_NEXT(entry, entry);
1122 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1123 struct commit_queue *commits)
1125 struct commit_queue_entry *entry;
1128 entry = TAILQ_FIRST(&commits->head);
1129 if (*first_displayed_entry == entry)
1132 entry = *first_displayed_entry;
1133 while (entry && nscrolled < maxscroll) {
1134 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1136 *first_displayed_entry = entry;
1142 static const struct got_error *
1143 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1144 struct commit_queue_entry **last_displayed_entry,
1145 struct commit_queue *commits, int *log_complete, int *commits_needed,
1146 pthread_cond_t *need_commits)
1148 const struct got_error *err = NULL;
1149 struct commit_queue_entry *pentry;
1152 if (*last_displayed_entry == NULL)
1156 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1157 if (pentry == NULL) {
1161 *commits_needed = maxscroll + 20;
1162 errcode = pthread_cond_signal(need_commits);
1164 return got_error_set_errno(errcode);
1167 *last_displayed_entry = pentry;
1169 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1172 *first_displayed_entry = pentry;
1173 } while (++nscrolled < maxscroll);
1178 static const struct got_error *
1179 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1180 struct got_object_id *commit_id, struct got_commit_object *commit,
1181 struct got_repository *repo)
1183 const struct got_error *err;
1184 struct got_object *obj1 = NULL, *obj2 = NULL;
1185 struct got_object_qid *parent_id;
1186 struct tog_view *diff_view;
1188 err = got_object_open(&obj2, repo, commit_id);
1192 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1194 err = got_object_open(&obj1, repo, parent_id->id);
1199 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1200 if (diff_view == NULL) {
1201 err = got_error_from_errno();
1205 err = open_diff_view(diff_view, obj1, obj2, repo);
1207 *new_view = diff_view;
1210 got_object_close(obj1);
1212 got_object_close(obj2);
1216 static const struct got_error *
1217 browse_commit(struct tog_view **new_view, int begin_x,
1218 struct commit_queue_entry *entry, struct got_repository *repo)
1220 const struct got_error *err = NULL;
1221 struct got_tree_object *tree;
1222 struct tog_view *tree_view;
1224 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1228 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1229 if (tree_view == NULL)
1230 return got_error_from_errno();
1232 err = open_tree_view(tree_view, tree, entry->id, repo);
1234 got_object_tree_close(tree);
1236 *new_view = tree_view;
1241 log_thread(void *arg)
1243 const struct got_error *err = NULL;
1245 struct tog_log_thread_args *a = arg;
1248 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1252 while (!done && !err) {
1253 err = queue_commits(a->graph, a->commits, 1, a->repo,
1256 if (err->code != GOT_ERR_ITER_COMPLETED)
1260 } else if (a->commits_needed > 0)
1261 a->commits_needed--;
1263 errcode = pthread_mutex_lock(&tog_mutex);
1265 return (void *)got_error_set_errno(errcode);
1268 a->log_complete = 1;
1269 else if (*a->quit) {
1271 a->log_complete = 1;
1272 } else if (*a->first_displayed_entry == NULL) {
1273 *a->first_displayed_entry =
1274 TAILQ_FIRST(&a->commits->head);
1275 *a->selected_entry = *a->first_displayed_entry;
1278 err = draw_commits(a->view, a->last_displayed_entry,
1279 a->selected_entry, *a->first_displayed_entry,
1280 a->commits, *a->selected, a->view->nlines,
1281 a->in_repo_path, a->commits_needed);
1287 a->commits_needed = 0;
1288 else if (a->commits_needed == 0) {
1289 errcode = pthread_cond_wait(&a->need_commits,
1292 err = got_error_set_errno(errcode);
1295 errcode = pthread_mutex_unlock(&tog_mutex);
1296 if (errcode && err == NULL)
1297 err = got_error_set_errno(errcode);
1302 static const struct got_error *
1303 stop_log_thread(struct tog_log_view_state *s)
1305 const struct got_error *err = NULL;
1310 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1312 return got_error_set_errno(errcode);
1313 errcode = pthread_mutex_unlock(&tog_mutex);
1315 return got_error_set_errno(errcode);
1316 errcode = pthread_join(s->thread, (void **)&err);
1318 return got_error_set_errno(errcode);
1319 errcode = pthread_mutex_lock(&tog_mutex);
1321 return got_error_set_errno(errcode);
1325 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1326 if (errcode && err == NULL)
1327 err = got_error_set_errno(errcode);
1329 if (s->thread_args.repo) {
1330 got_repo_close(s->thread_args.repo);
1331 s->thread_args.repo = NULL;
1334 if (s->thread_args.graph) {
1335 got_commit_graph_close(s->thread_args.graph);
1336 s->thread_args.graph = NULL;
1342 static const struct got_error *
1343 close_log_view(struct tog_view *view)
1345 const struct got_error *err = NULL;
1346 struct tog_log_view_state *s = &view->state.log;
1348 err = stop_log_thread(s);
1349 free_commits(&s->commits);
1350 free(s->in_repo_path);
1355 static const struct got_error *
1356 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1357 struct got_repository *repo, const char *path)
1359 const struct got_error *err = NULL;
1360 struct tog_log_view_state *s = &view->state.log;
1361 struct got_repository *thread_repo = NULL;
1362 struct got_commit_graph *thread_graph = NULL;
1365 err = got_repo_map_path(&s->in_repo_path, repo, path);
1369 /* The commit queue only contains commits being displayed. */
1370 TAILQ_INIT(&s->commits.head);
1371 s->commits.ncommits = 0;
1374 s->start_id = got_object_id_dup(start_id);
1375 if (s->start_id == NULL) {
1376 err = got_error_from_errno();
1380 view->show = show_log_view;
1381 view->input = input_log_view;
1382 view->close = close_log_view;
1384 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1387 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1392 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1394 err = got_error_set_errno(errcode);
1398 s->thread_args.commits_needed = view->nlines;
1399 s->thread_args.graph = thread_graph;
1400 s->thread_args.commits = &s->commits;
1401 s->thread_args.in_repo_path = s->in_repo_path;
1402 s->thread_args.start_id = s->start_id;
1403 s->thread_args.repo = thread_repo;
1404 s->thread_args.log_complete = 0;
1405 s->thread_args.quit = &s->quit;
1406 s->thread_args.view = view;
1407 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1408 s->thread_args.last_displayed_entry = &s->last_displayed_entry;
1409 s->thread_args.selected_entry = &s->selected_entry;
1410 s->thread_args.selected = &s->selected;
1412 errcode = pthread_create(&s->thread, NULL, log_thread,
1415 err = got_error_set_errno(errcode);
1421 close_log_view(view);
1425 static const struct got_error *
1426 show_log_view(struct tog_view *view)
1428 struct tog_log_view_state *s = &view->state.log;
1430 return draw_commits(view, &s->last_displayed_entry,
1431 &s->selected_entry, s->first_displayed_entry,
1432 &s->commits, s->selected, view->nlines,
1433 s->in_repo_path, s->thread_args.commits_needed);
1436 static const struct got_error *
1437 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1438 struct tog_view **focus_view, struct tog_view *view, int ch)
1440 const struct got_error *err = NULL;
1441 struct tog_log_view_state *s = &view->state.log;
1443 struct tog_view *diff_view = NULL, *tree_view = NULL;
1452 if (s->selected > 0)
1454 if (s->selected > 0)
1456 scroll_up(&s->first_displayed_entry, 1,
1460 if (TAILQ_FIRST(&s->commits.head) ==
1461 s->first_displayed_entry) {
1465 scroll_up(&s->first_displayed_entry,
1466 view->nlines, &s->commits);
1470 if (s->selected < MIN(view->nlines - 2,
1471 s->commits.ncommits - 1)) {
1475 err = scroll_down(&s->first_displayed_entry, 1,
1476 &s->last_displayed_entry, &s->commits,
1477 &s->thread_args.log_complete,
1478 &s->thread_args.commits_needed,
1479 &s->thread_args.need_commits);
1482 struct commit_queue_entry *first;
1483 first = s->first_displayed_entry;
1484 err = scroll_down(&s->first_displayed_entry,
1485 view->nlines, &s->last_displayed_entry,
1486 &s->commits, &s->thread_args.log_complete,
1487 &s->thread_args.commits_needed,
1488 &s->thread_args.need_commits);
1489 if (first == s->first_displayed_entry &&
1490 s->selected < MIN(view->nlines - 2,
1491 s->commits.ncommits - 1)) {
1492 /* can't scroll further down */
1493 s->selected = MIN(view->nlines - 2,
1494 s->commits.ncommits - 1);
1500 if (s->selected > view->nlines - 2)
1501 s->selected = view->nlines - 2;
1502 if (s->selected > s->commits.ncommits - 1)
1503 s->selected = s->commits.ncommits - 1;
1507 if (view_is_parent_view(view))
1508 begin_x = view_split_begin_x(view->begin_x);
1509 err = open_diff_view_for_commit(&diff_view, begin_x,
1510 s->selected_entry->id, s->selected_entry->commit,
1514 if (view_is_parent_view(view)) {
1515 err = view_close_child(view);
1518 err = view_set_child(view, diff_view);
1520 view_close(diff_view);
1523 if (!view_is_splitscreen(diff_view)) {
1524 *focus_view = diff_view;
1525 view->child_focussed = 1;
1528 *new_view = diff_view;
1531 if (view_is_parent_view(view))
1532 begin_x = view_split_begin_x(view->begin_x);
1533 err = browse_commit(&tree_view, begin_x,
1534 s->selected_entry, s->repo);
1535 if (view_is_parent_view(view)) {
1536 err = view_close_child(view);
1539 err = view_set_child(view, tree_view);
1541 view_close(tree_view);
1544 *focus_view = tree_view;
1545 view->child_focussed = 1;
1547 *new_view = tree_view;
1550 if (strcmp(s->in_repo_path, "/") == 0)
1552 parent_path = dirname(s->in_repo_path);
1553 if (parent_path && strcmp(parent_path, ".") != 0) {
1554 struct tog_view *lv;
1555 err = stop_log_thread(s);
1558 lv = view_open(view->nlines, view->ncols,
1559 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1561 return got_error_from_errno();
1562 err = open_log_view(lv, s->start_id, s->repo,
1566 if (view_is_parent_view(view))
1569 view_set_child(view->parent, lv);
1582 static const struct got_error *
1583 cmd_log(int argc, char *argv[])
1585 const struct got_error *error;
1586 struct got_repository *repo = NULL;
1587 struct got_object_id *start_id = NULL;
1588 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1589 char *start_commit = NULL;
1591 struct tog_view *view;
1594 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1599 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1602 start_commit = optarg;
1605 repo_path = realpath(optarg, NULL);
1606 if (repo_path == NULL)
1607 err(1, "-r option");
1621 path = strdup(argv[0]);
1625 return got_error_from_errno();
1627 cwd = getcwd(NULL, 0);
1629 error = got_error_from_errno();
1632 if (repo_path == NULL) {
1633 repo_path = strdup(cwd);
1634 if (repo_path == NULL) {
1635 error = got_error_from_errno();
1640 error = got_repo_open(&repo, repo_path);
1644 if (start_commit == NULL) {
1645 error = get_head_commit_id(&start_id, repo);
1649 struct got_object *obj;
1650 error = got_object_open_by_id_str(&obj, repo, start_commit);
1651 if (error == NULL) {
1652 start_id = got_object_id_dup(got_object_get_id(obj));
1653 if (start_id == NULL)
1654 error = got_error_from_errno();
1661 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1663 error = got_error_from_errno();
1666 error = open_log_view(view, start_id, repo, path);
1669 error = view_loop(view);
1676 got_repo_close(repo);
1684 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1690 parse_next_line(FILE *f, size_t *len)
1695 const char delim[3] = { '\0', '\0', '\0'};
1697 line = fparseln(f, &linelen, &lineno, delim, 0);
1703 static const struct got_error *
1704 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1705 int *last_displayed_line, int *eof, int max_lines,
1708 const struct got_error *err;
1709 int nlines = 0, nprinted = 0;
1716 werase(view->window);
1719 err = format_line(&wline, &width, header, view->ncols);
1724 if (view_needs_focus_indication(view))
1725 wstandout(view->window);
1726 waddwstr(view->window, wline);
1727 if (view_needs_focus_indication(view))
1728 wstandend(view->window);
1729 if (width < view->ncols)
1730 waddch(view->window, '\n');
1738 while (nprinted < max_lines) {
1739 line = parse_next_line(f, &len);
1744 if (++nlines < *first_displayed_line) {
1749 err = format_line(&wline, &width, line, view->ncols);
1754 waddwstr(view->window, wline);
1755 if (width < view->ncols)
1756 waddch(view->window, '\n');
1757 if (++nprinted == 1)
1758 *first_displayed_line = nlines;
1763 *last_displayed_line = nlines;
1770 static const struct got_error *
1771 create_diff(struct tog_diff_view_state *s)
1773 const struct got_error *err = NULL;
1774 struct got_object *obj1 = NULL, *obj2 = NULL;
1778 err = got_object_open(&obj1, s->repo, s->id1);
1783 err = got_object_open(&obj2, s->repo, s->id2);
1789 err = got_error_from_errno();
1796 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1797 case GOT_OBJ_TYPE_BLOB:
1798 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1799 s->diff_context, s->repo, f);
1801 case GOT_OBJ_TYPE_TREE:
1802 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1803 s->diff_context, s->repo, f);
1805 case GOT_OBJ_TYPE_COMMIT:
1806 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1810 err = got_error(GOT_ERR_OBJ_TYPE);
1815 got_object_close(obj1);
1816 got_object_close(obj2);
1822 static const struct got_error *
1823 open_diff_view(struct tog_view *view, struct got_object *obj1,
1824 struct got_object *obj2, struct got_repository *repo)
1826 const struct got_error *err;
1828 if (obj1 != NULL && obj2 != NULL &&
1829 got_object_get_type(obj1) != got_object_get_type(obj2))
1830 return got_error(GOT_ERR_OBJ_TYPE);
1833 struct got_object_id *id1;
1834 id1 = got_object_id_dup(got_object_get_id(obj1));
1836 return got_error_from_errno();
1837 view->state.diff.id1 = id1;
1839 view->state.diff.id1 = NULL;
1841 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1842 if (view->state.diff.id2 == NULL) {
1843 free(view->state.diff.id1);
1844 view->state.diff.id1 = NULL;
1845 return got_error_from_errno();
1847 view->state.diff.f = NULL;
1848 view->state.diff.first_displayed_line = 1;
1849 view->state.diff.last_displayed_line = view->nlines;
1850 view->state.diff.diff_context = 3;
1851 view->state.diff.repo = repo;
1853 err = create_diff(&view->state.diff);
1855 free(view->state.diff.id1);
1856 view->state.diff.id1 = NULL;
1857 free(view->state.diff.id2);
1858 view->state.diff.id2 = NULL;
1862 view->show = show_diff_view;
1863 view->input = input_diff_view;
1864 view->close = close_diff_view;
1869 static const struct got_error *
1870 close_diff_view(struct tog_view *view)
1872 const struct got_error *err = NULL;
1874 free(view->state.diff.id1);
1875 view->state.diff.id1 = NULL;
1876 free(view->state.diff.id2);
1877 view->state.diff.id2 = NULL;
1878 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1879 err = got_error_from_errno();
1883 static const struct got_error *
1884 show_diff_view(struct tog_view *view)
1886 const struct got_error *err;
1887 struct tog_diff_view_state *s = &view->state.diff;
1888 char *id_str1 = NULL, *id_str2, *header;
1891 err = got_object_id_str(&id_str1, s->id1);
1895 err = got_object_id_str(&id_str2, s->id2);
1899 if (asprintf(&header, "diff: %s %s",
1900 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1901 err = got_error_from_errno();
1909 return draw_file(view, s->f, &s->first_displayed_line,
1910 &s->last_displayed_line, &s->eof, view->nlines,
1914 static const struct got_error *
1915 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1916 struct tog_view **focus_view, struct tog_view *view, int ch)
1918 const struct got_error *err = NULL;
1919 struct tog_diff_view_state *s = &view->state.diff;
1925 if (s->first_displayed_line > 1)
1926 s->first_displayed_line--;
1930 while (i++ < view->nlines - 1 &&
1931 s->first_displayed_line > 1)
1932 s->first_displayed_line--;
1937 s->first_displayed_line++;
1942 while (!s->eof && i++ < view->nlines - 1) {
1944 line = parse_next_line(s->f, NULL);
1945 s->first_displayed_line++;
1951 if (s->diff_context > 0) {
1953 err = create_diff(s);
1957 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
1959 err = create_diff(s);
1969 static const struct got_error *
1970 cmd_diff(int argc, char *argv[])
1972 const struct got_error *error = NULL;
1973 struct got_repository *repo = NULL;
1974 struct got_object *obj1 = NULL, *obj2 = NULL;
1975 char *repo_path = NULL;
1976 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
1978 struct tog_view *view;
1981 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1986 while ((ch = getopt(argc, argv, "")) != -1) {
1998 usage_diff(); /* TODO show local worktree changes */
1999 } else if (argc == 2) {
2000 repo_path = getcwd(NULL, 0);
2001 if (repo_path == NULL)
2002 return got_error_from_errno();
2003 obj_id_str1 = argv[0];
2004 obj_id_str2 = argv[1];
2005 } else if (argc == 3) {
2006 repo_path = realpath(argv[0], NULL);
2007 if (repo_path == NULL)
2008 return got_error_from_errno();
2009 obj_id_str1 = argv[1];
2010 obj_id_str2 = argv[2];
2014 error = got_repo_open(&repo, repo_path);
2019 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2023 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2027 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2029 error = got_error_from_errno();
2032 error = open_diff_view(view, obj1, obj2, repo);
2035 error = view_loop(view);
2037 got_repo_close(repo);
2039 got_object_close(obj1);
2041 got_object_close(obj2);
2049 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2054 struct tog_blame_line {
2056 struct got_object_id *id;
2059 static const struct got_error *
2060 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2061 const char *path, struct tog_blame_line *lines, int nlines,
2062 int blame_complete, int selected_line, int *first_displayed_line,
2063 int *last_displayed_line, int *eof, int max_lines)
2065 const struct got_error *err;
2066 int lineno = 0, nprinted = 0;
2071 struct tog_blame_line *blame_line;
2072 struct got_object_id *prev_id = NULL;
2075 err = got_object_id_str(&id_str, id);
2080 werase(view->window);
2082 if (asprintf(&line, "commit: %s", id_str) == -1) {
2083 err = got_error_from_errno();
2088 err = format_line(&wline, &width, line, view->ncols);
2091 if (view_needs_focus_indication(view))
2092 wstandout(view->window);
2093 waddwstr(view->window, wline);
2094 if (view_needs_focus_indication(view))
2095 wstandend(view->window);
2098 if (width < view->ncols)
2099 waddch(view->window, '\n');
2101 if (asprintf(&line, "[%d/%d] %s%s",
2102 *first_displayed_line - 1 + selected_line, nlines,
2103 blame_complete ? "" : "annotating ", path) == -1) {
2105 return got_error_from_errno();
2108 err = format_line(&wline, &width, line, view->ncols);
2113 waddwstr(view->window, wline);
2116 if (width < view->ncols)
2117 waddch(view->window, '\n');
2120 while (nprinted < max_lines - 2) {
2121 line = parse_next_line(f, &len);
2126 if (++lineno < *first_displayed_line) {
2131 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2132 err = format_line(&wline, &width, line, wlimit);
2138 if (view->focussed && nprinted == selected_line - 1)
2139 wstandout(view->window);
2141 blame_line = &lines[lineno - 1];
2142 if (blame_line->annotated && prev_id &&
2143 got_object_id_cmp(prev_id, blame_line->id) == 0)
2144 waddstr(view->window, " ");
2145 else if (blame_line->annotated) {
2147 err = got_object_id_str(&id_str, blame_line->id);
2153 wprintw(view->window, "%.8s ", id_str);
2155 prev_id = blame_line->id;
2157 waddstr(view->window, "........ ");
2161 waddwstr(view->window, wline);
2162 while (width < wlimit) {
2163 waddch(view->window, ' ');
2166 if (view->focussed && nprinted == selected_line - 1)
2167 wstandend(view->window);
2168 if (++nprinted == 1)
2169 *first_displayed_line = lineno;
2174 *last_displayed_line = lineno;
2181 static const struct got_error *
2182 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2184 const struct got_error *err = NULL;
2185 struct tog_blame_cb_args *a = arg;
2186 struct tog_blame_line *line;
2189 if (nlines != a->nlines ||
2190 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 return got_error(GOT_ERR_RANGE);
2193 errcode = pthread_mutex_lock(&tog_mutex);
2195 return got_error_set_errno(errcode);
2197 if (*a->quit) { /* user has quit the blame view */
2198 err = got_error(GOT_ERR_ITER_COMPLETED);
2203 goto done; /* no change in this commit */
2205 line = &a->lines[lineno - 1];
2206 if (line->annotated)
2209 line->id = got_object_id_dup(id);
2210 if (line->id == NULL) {
2211 err = got_error_from_errno();
2214 line->annotated = 1;
2216 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2217 a->lines, a->nlines, 0, *a->selected_line, a->first_displayed_line,
2218 a->last_displayed_line, a->eof, a->view->nlines);
2220 errcode = pthread_mutex_unlock(&tog_mutex);
2222 err = got_error_set_errno(errcode);
2227 blame_thread(void *arg)
2229 const struct got_error *err;
2230 struct tog_blame_thread_args *ta = arg;
2231 struct tog_blame_cb_args *a = ta->cb_args;
2234 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2235 blame_cb, ta->cb_args);
2237 errcode = pthread_mutex_lock(&tog_mutex);
2239 return (void *)got_error_set_errno(errcode);
2241 got_repo_close(ta->repo);
2245 err = draw_blame(a->view, a->commit_id, a->f, a->path,
2246 a->lines, a->nlines, 1, *a->selected_line,
2247 a->first_displayed_line, a->last_displayed_line, a->eof,
2255 errcode = pthread_mutex_unlock(&tog_mutex);
2256 if (errcode && err == NULL)
2257 err = got_error_set_errno(errcode);
2262 static struct got_object_id *
2263 get_selected_commit_id(struct tog_blame_line *lines,
2264 int first_displayed_line, int selected_line)
2266 struct tog_blame_line *line;
2268 line = &lines[first_displayed_line - 1 + selected_line - 1];
2269 if (!line->annotated)
2275 static const struct got_error *
2276 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2277 struct tog_blame_line *lines, int first_displayed_line,
2278 int selected_line, struct got_repository *repo)
2280 const struct got_error *err = NULL;
2281 struct got_commit_object *commit = NULL;
2282 struct got_object_id *selected_id;
2283 struct got_object_qid *pid;
2288 selected_id = get_selected_commit_id(lines,
2289 first_displayed_line, selected_line);
2290 if (selected_id == NULL)
2293 err = got_object_open(obj, repo, selected_id);
2297 err = got_object_commit_open(&commit, repo, *obj);
2301 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2303 err = got_object_open(pobj, repo, pid->id);
2309 got_object_commit_close(commit);
2313 static const struct got_error *
2314 stop_blame(struct tog_blame *blame)
2316 const struct got_error *err = NULL;
2319 if (blame->thread) {
2321 errcode = pthread_mutex_unlock(&tog_mutex);
2323 return got_error_set_errno(errcode);
2324 errcode = pthread_join(blame->thread, (void **)&err);
2326 return got_error_set_errno(errcode);
2327 errcode = pthread_mutex_lock(&tog_mutex);
2329 return got_error_set_errno(errcode);
2330 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2332 blame->thread = NULL;
2334 if (blame->thread_args.repo) {
2335 got_repo_close(blame->thread_args.repo);
2336 blame->thread_args.repo = NULL;
2342 for (i = 0; i < blame->nlines; i++)
2343 free(blame->lines[i].id);
2345 blame->lines = NULL;
2346 free(blame->cb_args.commit_id);
2347 blame->cb_args.commit_id = NULL;
2352 static const struct got_error *
2353 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2354 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2355 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2356 struct got_repository *repo)
2358 const struct got_error *err = NULL;
2359 struct got_blob_object *blob = NULL;
2360 struct got_repository *thread_repo = NULL;
2361 struct got_object_id *obj_id = NULL;
2362 struct got_object *obj = NULL;
2365 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2369 err = got_object_open(&obj, repo, obj_id);
2373 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2374 err = got_error(GOT_ERR_OBJ_TYPE);
2378 err = got_object_blob_open(&blob, repo, obj, 8192);
2381 blame->f = got_opentemp();
2382 if (blame->f == NULL) {
2383 err = got_error_from_errno();
2386 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2391 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2392 if (blame->lines == NULL) {
2393 err = got_error_from_errno();
2397 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2401 blame->cb_args.view = view;
2402 blame->cb_args.lines = blame->lines;
2403 blame->cb_args.nlines = blame->nlines;
2404 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2405 if (blame->cb_args.commit_id == NULL) {
2406 err = got_error_from_errno();
2409 blame->cb_args.f = blame->f;
2410 blame->cb_args.path = path;
2411 blame->cb_args.first_displayed_line = first_displayed_line;
2412 blame->cb_args.selected_line = selected_line;
2413 blame->cb_args.last_displayed_line = last_displayed_line;
2414 blame->cb_args.quit = done;
2415 blame->cb_args.eof = eof;
2417 blame->thread_args.path = path;
2418 blame->thread_args.repo = thread_repo;
2419 blame->thread_args.cb_args = &blame->cb_args;
2420 blame->thread_args.complete = blame_complete;
2421 *blame_complete = 0;
2423 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2424 &blame->thread_args);
2426 err = got_error_set_errno(errcode);
2432 got_object_blob_close(blob);
2435 got_object_close(obj);
2441 static const struct got_error *
2442 open_blame_view(struct tog_view *view, char *path,
2443 struct got_object_id *commit_id, struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct tog_blame_view_state *s = &view->state.blame;
2448 SIMPLEQ_INIT(&s->blamed_commits);
2450 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2454 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2455 s->first_displayed_line = 1;
2456 s->last_displayed_line = view->nlines;
2457 s->selected_line = 1;
2458 s->blame_complete = 0;
2460 if (s->path == NULL)
2461 return got_error_from_errno();
2463 s->commit_id = commit_id;
2464 memset(&s->blame, 0, sizeof(s->blame));
2466 view->show = show_blame_view;
2467 view->input = input_blame_view;
2468 view->close = close_blame_view;
2470 return run_blame(&s->blame, view, &s->blame_complete,
2471 &s->first_displayed_line, &s->last_displayed_line,
2472 &s->selected_line, &s->done, &s->eof, s->path,
2473 s->blamed_commit->id, s->repo);
2476 static const struct got_error *
2477 close_blame_view(struct tog_view *view)
2479 const struct got_error *err = NULL;
2480 struct tog_blame_view_state *s = &view->state.blame;
2482 if (s->blame.thread)
2483 err = stop_blame(&s->blame);
2485 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2486 struct got_object_qid *blamed_commit;
2487 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2488 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2489 got_object_qid_free(blamed_commit);
2497 static const struct got_error *
2498 show_blame_view(struct tog_view *view)
2500 const struct got_error *err = NULL;
2501 struct tog_blame_view_state *s = &view->state.blame;
2503 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2504 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2505 s->selected_line, &s->first_displayed_line,
2506 &s->last_displayed_line, &s->eof, view->nlines);
2512 static const struct got_error *
2513 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2514 struct tog_view **focus_view, struct tog_view *view, int ch)
2516 const struct got_error *err = NULL, *thread_err = NULL;
2517 struct got_object *obj = NULL, *pobj = NULL;
2518 struct tog_view *diff_view;
2519 struct tog_blame_view_state *s = &view->state.blame;
2528 if (s->selected_line > 1)
2530 else if (s->selected_line == 1 &&
2531 s->first_displayed_line > 1)
2532 s->first_displayed_line--;
2535 if (s->first_displayed_line == 1) {
2536 s->selected_line = 1;
2539 if (s->first_displayed_line > view->nlines - 2)
2540 s->first_displayed_line -=
2543 s->first_displayed_line = 1;
2547 if (s->selected_line < view->nlines - 2 &&
2548 s->first_displayed_line +
2549 s->selected_line <= s->blame.nlines)
2551 else if (s->last_displayed_line <
2553 s->first_displayed_line++;
2557 struct got_object_id *id;
2558 id = get_selected_commit_id(s->blame.lines,
2559 s->first_displayed_line, s->selected_line);
2560 if (id == NULL || got_object_id_cmp(id,
2561 s->blamed_commit->id) == 0)
2563 err = open_selected_commit(&pobj, &obj,
2564 s->blame.lines, s->first_displayed_line,
2565 s->selected_line, s->repo);
2568 if (pobj == NULL && obj == NULL)
2570 if (ch == 'p' && pobj == NULL)
2573 thread_err = stop_blame(&s->blame);
2577 id = got_object_get_id(ch == 'b' ? obj : pobj);
2578 got_object_close(obj);
2581 got_object_close(pobj);
2584 err = got_object_qid_alloc(&s->blamed_commit, id);
2587 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2588 s->blamed_commit, entry);
2589 err = run_blame(&s->blame, view, &s->blame_complete,
2590 &s->first_displayed_line, &s->last_displayed_line,
2591 &s->selected_line, &s->done, &s->eof,
2592 s->path, s->blamed_commit->id, s->repo);
2598 struct got_object_qid *first;
2599 first = SIMPLEQ_FIRST(&s->blamed_commits);
2600 if (!got_object_id_cmp(first->id, s->commit_id))
2603 thread_err = stop_blame(&s->blame);
2607 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2608 got_object_qid_free(s->blamed_commit);
2610 SIMPLEQ_FIRST(&s->blamed_commits);
2611 err = run_blame(&s->blame, view, &s->blame_complete,
2612 &s->first_displayed_line, &s->last_displayed_line,
2613 &s->selected_line, &s->done, &s->eof, s->path,
2614 s->blamed_commit->id, s->repo);
2621 err = open_selected_commit(&pobj, &obj,
2622 s->blame.lines, s->first_displayed_line,
2623 s->selected_line, s->repo);
2626 if (pobj == NULL && obj == NULL)
2629 if (view_is_parent_view(view))
2630 begin_x = view_split_begin_x(view->begin_x);
2631 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2632 if (diff_view == NULL) {
2633 err = got_error_from_errno();
2636 err = open_diff_view(diff_view, pobj, obj, s->repo);
2638 view_close(diff_view);
2641 if (view_is_parent_view(view)) {
2642 err = view_close_child(view);
2645 err = view_set_child(view, diff_view);
2647 view_close(diff_view);
2650 if (!view_is_splitscreen(diff_view)) {
2651 *focus_view = diff_view;
2652 view->child_focussed = 1;
2655 *new_view = diff_view;
2657 got_object_close(pobj);
2660 got_object_close(obj);
2667 if (s->last_displayed_line >= s->blame.nlines &&
2668 s->selected_line < view->nlines - 2) {
2669 s->selected_line = MIN(s->blame.nlines,
2673 if (s->last_displayed_line + view->nlines - 2
2675 s->first_displayed_line +=
2678 s->first_displayed_line =
2683 if (s->selected_line > view->nlines - 2) {
2684 s->selected_line = MIN(s->blame.nlines,
2693 got_object_close(pobj);
2694 return thread_err ? thread_err : err;
2697 static const struct got_error *
2698 cmd_blame(int argc, char *argv[])
2700 const struct got_error *error;
2701 struct got_repository *repo = NULL;
2702 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2703 struct got_object_id *commit_id = NULL;
2704 char *commit_id_str = NULL;
2706 struct tog_view *view;
2709 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2714 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2717 commit_id_str = optarg;
2720 repo_path = realpath(optarg, NULL);
2721 if (repo_path == NULL)
2722 err(1, "-r option");
2738 cwd = getcwd(NULL, 0);
2740 error = got_error_from_errno();
2743 if (repo_path == NULL) {
2744 repo_path = strdup(cwd);
2745 if (repo_path == NULL) {
2746 error = got_error_from_errno();
2752 error = got_repo_open(&repo, repo_path);
2756 error = got_repo_map_path(&in_repo_path, repo, path);
2760 if (commit_id_str == NULL) {
2761 struct got_reference *head_ref;
2762 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2765 error = got_ref_resolve(&commit_id, repo, head_ref);
2766 got_ref_close(head_ref);
2768 struct got_object *obj;
2769 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2772 commit_id = got_object_id_dup(got_object_get_id(obj));
2773 if (commit_id == NULL)
2774 error = got_error_from_errno();
2775 got_object_close(obj);
2780 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2782 error = got_error_from_errno();
2785 error = open_blame_view(view, in_repo_path, commit_id, repo);
2788 error = view_loop(view);
2794 got_repo_close(repo);
2798 static const struct got_error *
2799 draw_tree_entries(struct tog_view *view,
2800 struct got_tree_entry **first_displayed_entry,
2801 struct got_tree_entry **last_displayed_entry,
2802 struct got_tree_entry **selected_entry, int *ndisplayed,
2803 const char *label, int show_ids, const char *parent_path,
2804 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2806 const struct got_error *err = NULL;
2807 struct got_tree_entry *te;
2813 werase(view->window);
2818 err = format_line(&wline, &width, label, view->ncols);
2821 if (view_needs_focus_indication(view))
2822 wstandout(view->window);
2823 waddwstr(view->window, wline);
2824 if (view_needs_focus_indication(view))
2825 wstandend(view->window);
2828 if (width < view->ncols)
2829 waddch(view->window, '\n');
2832 err = format_line(&wline, &width, parent_path, view->ncols);
2835 waddwstr(view->window, wline);
2838 if (width < view->ncols)
2839 waddch(view->window, '\n');
2842 waddch(view->window, '\n');
2846 te = SIMPLEQ_FIRST(&entries->head);
2847 if (*first_displayed_entry == NULL) {
2848 if (selected == 0) {
2850 wstandout(view->window);
2851 *selected_entry = NULL;
2853 waddstr(view->window, " ..\n"); /* parent directory */
2854 if (selected == 0 && view->focussed)
2855 wstandend(view->window);
2862 while (te != *first_displayed_entry)
2863 te = SIMPLEQ_NEXT(te, entry);
2867 char *line = NULL, *id_str = NULL;
2870 err = got_object_id_str(&id_str, te->id);
2872 return got_error_from_errno();
2874 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2875 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2877 return got_error_from_errno();
2880 err = format_line(&wline, &width, line, view->ncols);
2885 if (n == selected) {
2887 wstandout(view->window);
2888 *selected_entry = te;
2890 waddwstr(view->window, wline);
2891 if (width < view->ncols)
2892 waddch(view->window, '\n');
2893 if (n == selected && view->focussed)
2894 wstandend(view->window);
2900 *last_displayed_entry = te;
2903 te = SIMPLEQ_NEXT(te, entry);
2910 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2911 const struct got_tree_entries *entries, int isroot)
2913 struct got_tree_entry *te, *prev;
2916 if (*first_displayed_entry == NULL)
2919 te = SIMPLEQ_FIRST(&entries->head);
2920 if (*first_displayed_entry == te) {
2922 *first_displayed_entry = NULL;
2926 /* XXX this is stupid... switch to TAILQ? */
2927 for (i = 0; i < maxscroll; i++) {
2928 while (te != *first_displayed_entry) {
2930 te = SIMPLEQ_NEXT(te, entry);
2932 *first_displayed_entry = prev;
2933 te = SIMPLEQ_FIRST(&entries->head);
2935 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2936 *first_displayed_entry = NULL;
2940 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2941 struct got_tree_entry *last_displayed_entry,
2942 const struct got_tree_entries *entries)
2944 struct got_tree_entry *next;
2947 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2950 if (*first_displayed_entry)
2951 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2953 next = SIMPLEQ_FIRST(&entries->head);
2955 *first_displayed_entry = next;
2956 if (++n >= maxscroll)
2958 next = SIMPLEQ_NEXT(next, entry);
2962 static const struct got_error *
2963 tree_entry_path(char **path, struct tog_parent_trees *parents,
2964 struct got_tree_entry *te)
2966 const struct got_error *err = NULL;
2967 struct tog_parent_tree *pt;
2968 size_t len = 2; /* for leading slash and NUL */
2970 TAILQ_FOREACH(pt, parents, entry)
2971 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2973 len += strlen(te->name);
2975 *path = calloc(1, len);
2977 return got_error_from_errno();
2980 pt = TAILQ_LAST(parents, tog_parent_trees);
2982 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2983 err = got_error(GOT_ERR_NO_SPACE);
2986 if (strlcat(*path, "/", len) >= len) {
2987 err = got_error(GOT_ERR_NO_SPACE);
2990 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2993 if (strlcat(*path, te->name, len) >= len) {
2994 err = got_error(GOT_ERR_NO_SPACE);
3006 static const struct got_error *
3007 blame_tree_entry(struct tog_view **new_view, int begin_x,
3008 struct got_tree_entry *te, struct tog_parent_trees *parents,
3009 struct got_object_id *commit_id, struct got_repository *repo)
3011 const struct got_error *err = NULL;
3013 struct tog_view *blame_view;
3015 err = tree_entry_path(&path, parents, te);
3019 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3020 if (blame_view == NULL)
3021 return got_error_from_errno();
3023 err = open_blame_view(blame_view, path, commit_id, repo);
3025 view_close(blame_view);
3028 *new_view = blame_view;
3032 static const struct got_error *
3033 log_tree_entry(struct tog_view **new_view, int begin_x,
3034 struct got_tree_entry *te, struct tog_parent_trees *parents,
3035 struct got_object_id *commit_id, struct got_repository *repo)
3037 struct tog_view *log_view;
3038 const struct got_error *err = NULL;
3041 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3042 if (log_view == NULL)
3043 return got_error_from_errno();
3045 err = tree_entry_path(&path, parents, te);
3049 err = open_log_view(log_view, commit_id, repo, path);
3051 view_close(log_view);
3053 *new_view = log_view;
3058 static const struct got_error *
3059 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3060 struct got_object_id *commit_id, struct got_repository *repo)
3062 const struct got_error *err = NULL;
3063 char *commit_id_str = NULL;
3064 struct tog_tree_view_state *s = &view->state.tree;
3066 TAILQ_INIT(&s->parents);
3068 err = got_object_id_str(&commit_id_str, commit_id);
3072 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3073 err = got_error_from_errno();
3077 s->root = s->tree = root;
3078 s->entries = got_object_tree_get_entries(root);
3079 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3080 s->commit_id = got_object_id_dup(commit_id);
3081 if (s->commit_id == NULL) {
3082 err = got_error_from_errno();
3087 view->show = show_tree_view;
3088 view->input = input_tree_view;
3089 view->close = close_tree_view;
3091 free(commit_id_str);
3093 free(s->tree_label);
3094 s->tree_label = NULL;
3099 static const struct got_error *
3100 close_tree_view(struct tog_view *view)
3102 struct tog_tree_view_state *s = &view->state.tree;
3104 free(s->tree_label);
3105 s->tree_label = NULL;
3107 s->commit_id = NULL;
3108 while (!TAILQ_EMPTY(&s->parents)) {
3109 struct tog_parent_tree *parent;
3110 parent = TAILQ_FIRST(&s->parents);
3111 TAILQ_REMOVE(&s->parents, parent, entry);
3115 if (s->tree != s->root)
3116 got_object_tree_close(s->tree);
3117 got_object_tree_close(s->root);
3122 static const struct got_error *
3123 show_tree_view(struct tog_view *view)
3125 const struct got_error *err = NULL;
3126 struct tog_tree_view_state *s = &view->state.tree;
3129 err = tree_entry_path(&parent_path, &s->parents, NULL);
3133 err = draw_tree_entries(view, &s->first_displayed_entry,
3134 &s->last_displayed_entry, &s->selected_entry,
3135 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3136 s->entries, s->selected, view->nlines, s->tree == s->root);
3143 static const struct got_error *
3144 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3145 struct tog_view **focus_view, struct tog_view *view, int ch)
3147 const struct got_error *err = NULL;
3148 struct tog_tree_view_state *s = &view->state.tree;
3149 struct tog_view *log_view;
3154 s->show_ids = !s->show_ids;
3157 if (!s->selected_entry)
3159 if (view_is_parent_view(view))
3160 begin_x = view_split_begin_x(view->begin_x);
3161 err = log_tree_entry(&log_view, begin_x,
3162 s->selected_entry, &s->parents,
3163 s->commit_id, s->repo);
3164 if (view_is_parent_view(view)) {
3165 err = view_close_child(view);
3168 err = view_set_child(view, log_view);
3170 view_close(log_view);
3173 *focus_view = log_view;
3174 view->child_focussed = 1;
3176 *new_view = log_view;
3180 if (s->selected > 0)
3182 if (s->selected > 0)
3184 tree_scroll_up(&s->first_displayed_entry, 1,
3185 s->entries, s->tree == s->root);
3188 if (SIMPLEQ_FIRST(&s->entries->head) ==
3189 s->first_displayed_entry) {
3190 if (s->tree != s->root)
3191 s->first_displayed_entry = NULL;
3195 tree_scroll_up(&s->first_displayed_entry,
3196 view->nlines, s->entries,
3197 s->tree == s->root);
3201 if (s->selected < s->ndisplayed - 1) {
3205 tree_scroll_down(&s->first_displayed_entry, 1,
3206 s->last_displayed_entry, s->entries);
3209 tree_scroll_down(&s->first_displayed_entry,
3210 view->nlines, s->last_displayed_entry,
3212 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3215 /* can't scroll any further; move cursor down */
3216 if (s->selected < s->ndisplayed - 1)
3217 s->selected = s->ndisplayed - 1;
3221 if (s->selected_entry == NULL) {
3222 struct tog_parent_tree *parent;
3224 /* user selected '..' */
3225 if (s->tree == s->root)
3227 parent = TAILQ_FIRST(&s->parents);
3228 TAILQ_REMOVE(&s->parents, parent,
3230 got_object_tree_close(s->tree);
3231 s->tree = parent->tree;
3233 got_object_tree_get_entries(s->tree);
3234 s->first_displayed_entry =
3235 parent->first_displayed_entry;
3237 parent->selected_entry;
3238 s->selected = parent->selected;
3240 } else if (S_ISDIR(s->selected_entry->mode)) {
3241 struct tog_parent_tree *parent;
3242 struct got_tree_object *child;
3243 err = got_object_open_as_tree(&child,
3244 s->repo, s->selected_entry->id);
3247 parent = calloc(1, sizeof(*parent));
3248 if (parent == NULL) {
3249 err = got_error_from_errno();
3252 parent->tree = s->tree;
3253 parent->first_displayed_entry =
3254 s->first_displayed_entry;
3255 parent->selected_entry = s->selected_entry;
3256 parent->selected = s->selected;
3257 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3260 got_object_tree_get_entries(s->tree);
3262 s->first_displayed_entry = NULL;
3263 } else if (S_ISREG(s->selected_entry->mode)) {
3264 struct tog_view *blame_view;
3265 int begin_x = view_is_parent_view(view) ?
3266 view_split_begin_x(view->begin_x) : 0;
3268 err = blame_tree_entry(&blame_view, begin_x,
3269 s->selected_entry, &s->parents, s->commit_id,
3273 if (view_is_parent_view(view)) {
3274 err = view_close_child(view);
3277 err = view_set_child(view, blame_view);
3279 view_close(blame_view);
3282 *focus_view = blame_view;
3283 view->child_focussed = 1;
3285 *new_view = blame_view;
3289 if (s->selected > view->nlines)
3290 s->selected = s->ndisplayed - 1;
3303 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3308 static const struct got_error *
3309 cmd_tree(int argc, char *argv[])
3311 const struct got_error *error;
3312 struct got_repository *repo = NULL;
3313 char *repo_path = NULL;
3314 struct got_object_id *commit_id = NULL;
3315 char *commit_id_arg = NULL;
3316 struct got_commit_object *commit = NULL;
3317 struct got_tree_object *tree = NULL;
3319 struct tog_view *view;
3322 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3327 while ((ch = getopt(argc, argv, "c:")) != -1) {
3330 commit_id_arg = optarg;
3342 repo_path = getcwd(NULL, 0);
3343 if (repo_path == NULL)
3344 return got_error_from_errno();
3345 } else if (argc == 1) {
3346 repo_path = realpath(argv[0], NULL);
3347 if (repo_path == NULL)
3348 return got_error_from_errno();
3352 error = got_repo_open(&repo, repo_path);
3357 if (commit_id_arg == NULL) {
3358 error = get_head_commit_id(&commit_id, repo);
3362 struct got_object *obj;
3363 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3364 if (error == NULL) {
3365 commit_id = got_object_id_dup(got_object_get_id(obj));
3366 if (commit_id == NULL)
3367 error = got_error_from_errno();
3373 error = got_object_open_as_commit(&commit, repo, commit_id);
3377 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3381 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3383 error = got_error_from_errno();
3386 error = open_tree_view(view, tree, commit_id, repo);
3389 error = view_loop(view);
3393 got_object_commit_close(commit);
3395 got_object_tree_close(tree);
3397 got_repo_close(repo);
3407 intrflush(stdscr, FALSE);
3408 keypad(stdscr, TRUE);
3417 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3418 "Available commands:\n", getprogname());
3419 for (i = 0; i < nitems(tog_commands); i++) {
3420 struct tog_cmd *cmd = &tog_commands[i];
3421 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3427 make_argv(const char *arg0, const char *arg1)
3430 int argc = (arg1 == NULL ? 1 : 2);
3432 argv = calloc(argc, sizeof(char *));
3435 argv[0] = strdup(arg0);
3436 if (argv[0] == NULL)
3439 argv[1] = strdup(arg1);
3440 if (argv[1] == NULL)
3448 main(int argc, char *argv[])
3450 const struct got_error *error = NULL;
3451 struct tog_cmd *cmd = NULL;
3453 char **cmd_argv = NULL;
3455 setlocale(LC_ALL, "");
3457 while ((ch = getopt(argc, argv, "h")) != -1) {
3476 /* Build an argument vector which runs a default command. */
3477 cmd = &tog_commands[0];
3478 cmd_argv = make_argv(cmd->name, NULL);
3483 /* Did the user specific a command? */
3484 for (i = 0; i < nitems(tog_commands); i++) {
3485 if (strncmp(tog_commands[i].name, argv[0],
3486 strlen(argv[0])) == 0) {
3487 cmd = &tog_commands[i];
3489 tog_commands[i].cmd_usage();
3494 /* Did the user specify a repository? */
3495 char *repo_path = realpath(argv[0], NULL);
3497 struct got_repository *repo;
3498 error = got_repo_open(&repo, repo_path);
3500 got_repo_close(repo);
3502 error = got_error_from_errno();
3505 fprintf(stderr, "%s: '%s' is not a "
3506 "known command\n", getprogname(),
3510 fprintf(stderr, "%s: '%s' is neither a known "
3511 "command nor a path to a repository\n",
3512 getprogname(), argv[0]);
3516 cmd = &tog_commands[0];
3517 cmd_argv = make_argv(cmd->name, repo_path);
3525 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3532 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);