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>
19 #include <sys/ioctl.h>
22 #define _XOPEN_SOURCE_EXTENDED
24 #undef _XOPEN_SOURCE_EXTENDED
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
48 #include "got_blame.h"
51 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 const struct got_error *(*cmd_main)(int, char *[]);
66 void (*cmd_usage)(void);
70 __dead static void usage(void);
71 __dead static void usage_log(void);
72 __dead static void usage_diff(void);
73 __dead static void usage_blame(void);
74 __dead static void usage_tree(void);
76 static const struct got_error* cmd_log(int, char *[]);
77 static const struct got_error* cmd_diff(int, char *[]);
78 static const struct got_error* cmd_blame(int, char *[]);
79 static const struct got_error* cmd_tree(int, char *[]);
81 static struct tog_cmd tog_commands[] = {
82 { "log", cmd_log, usage_log,
83 "show repository history" },
84 { "diff", cmd_diff, usage_diff,
85 "compare files and directories" },
86 { "blame", cmd_blame, usage_blame,
87 "show line-by-line file history" },
88 { "tree", cmd_tree, usage_tree,
89 "browse trees in repository" },
99 struct tog_diff_view_state {
100 struct got_object_id *id1, *id2;
102 int first_displayed_line;
103 int last_displayed_line;
106 struct got_repository *repo;
109 struct commit_queue_entry {
110 TAILQ_ENTRY(commit_queue_entry) entry;
111 struct got_object_id *id;
112 struct got_commit_object *commit;
115 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
116 struct commit_queue {
118 struct commit_queue_head head;
121 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
123 struct tog_log_thread_args {
124 pthread_cond_t need_commits;
126 struct got_commit_graph *graph;
127 struct commit_queue *commits;
128 const char *in_repo_path;
129 struct got_object_id *start_id;
130 struct got_repository *repo;
133 struct tog_view *view;
134 struct commit_queue_entry **first_displayed_entry;
135 struct commit_queue_entry **selected_entry;
138 struct tog_log_view_state {
139 struct commit_queue commits;
140 struct commit_queue_entry *first_displayed_entry;
141 struct commit_queue_entry *last_displayed_entry;
142 struct commit_queue_entry *selected_entry;
145 struct got_repository *repo;
146 struct got_object_id *start_id;
149 struct tog_log_thread_args thread_args;
152 struct tog_blame_cb_args {
153 struct tog_blame_line *lines; /* one per line */
156 struct tog_view *view;
157 struct got_object_id *commit_id;
161 struct tog_blame_thread_args {
163 struct got_repository *repo;
164 struct tog_blame_cb_args *cb_args;
171 struct tog_blame_line *lines;
174 struct tog_blame_thread_args thread_args;
175 struct tog_blame_cb_args cb_args;
179 struct tog_blame_view_state {
180 int first_displayed_line;
181 int last_displayed_line;
186 struct got_object_id_queue blamed_commits;
187 struct got_object_qid *blamed_commit;
189 struct got_repository *repo;
190 struct got_object_id *commit_id;
191 struct tog_blame blame;
194 struct tog_parent_tree {
195 TAILQ_ENTRY(tog_parent_tree) entry;
196 struct got_tree_object *tree;
197 struct got_tree_entry *first_displayed_entry;
198 struct got_tree_entry *selected_entry;
202 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
204 struct tog_tree_view_state {
206 struct got_tree_object *root;
207 struct got_tree_object *tree;
208 const struct got_tree_entries *entries;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *last_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int nentries, ndisplayed, selected, show_ids;
213 struct tog_parent_trees parents;
214 struct got_object_id *commit_id;
215 struct got_repository *repo;
219 * We implement two types of views: parent views and child views.
221 * The 'Tab' key switches between a parent view and its child view.
222 * Child views are shown side-by-side to their parent view, provided
223 * there is enough screen estate.
225 * When a new view is opened from within a parent view, this new view
226 * becomes a child view of the parent view, replacing any existing child.
228 * When a new view is opened from within a child view, this new view
229 * becomes a parent view which will obscure the views below until the
230 * user quits the new parent view by typing 'q'.
232 * This list of views contains parent views only.
233 * Child views are only pointed to by their parent view.
235 TAILQ_HEAD(tog_view_list_head, tog_view);
238 TAILQ_ENTRY(tog_view) entry;
241 int nlines, ncols, begin_y, begin_x;
242 int lines, cols; /* copies of LINES and COLS */
244 struct tog_view *parent;
245 struct tog_view *child;
248 /* type-specific state */
249 enum tog_view_type type;
251 struct tog_diff_view_state diff;
252 struct tog_log_view_state log;
253 struct tog_blame_view_state blame;
254 struct tog_tree_view_state tree;
257 const struct got_error *(*show)(struct tog_view *);
258 const struct got_error *(*input)(struct tog_view **,
259 struct tog_view **, struct tog_view**, struct tog_view *, int);
260 const struct got_error *(*close)(struct tog_view *);
263 static const struct got_error *open_diff_view(struct tog_view *,
264 struct got_object *, struct got_object *, struct got_repository *);
265 static const struct got_error *show_diff_view(struct tog_view *);
266 static const struct got_error *input_diff_view(struct tog_view **,
267 struct tog_view **, struct tog_view **, struct tog_view *, int);
268 static const struct got_error* close_diff_view(struct tog_view *);
270 static const struct got_error *open_log_view(struct tog_view *,
271 struct got_object_id *, struct got_repository *, const char *, int);
272 static const struct got_error * show_log_view(struct tog_view *);
273 static const struct got_error *input_log_view(struct tog_view **,
274 struct tog_view **, struct tog_view **, struct tog_view *, int);
275 static const struct got_error *close_log_view(struct tog_view *);
277 static const struct got_error *open_blame_view(struct tog_view *, char *,
278 struct got_object_id *, struct got_repository *);
279 static const struct got_error *show_blame_view(struct tog_view *);
280 static const struct got_error *input_blame_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error *close_blame_view(struct tog_view *);
284 static const struct got_error *open_tree_view(struct tog_view *,
285 struct got_tree_object *, struct got_object_id *, struct got_repository *);
286 static const struct got_error *show_tree_view(struct tog_view *);
287 static const struct got_error *input_tree_view(struct tog_view **,
288 struct tog_view **, struct tog_view **, struct tog_view *, int);
289 static const struct got_error *close_tree_view(struct tog_view *);
291 static volatile sig_atomic_t tog_sigwinch_received;
294 tog_sigwinch(int signo)
296 tog_sigwinch_received = 1;
299 static const struct got_error *
300 view_close(struct tog_view *view)
302 const struct got_error *err = NULL;
305 view_close(view->child);
309 err = view->close(view);
311 del_panel(view->panel);
313 delwin(view->window);
318 static struct tog_view *
319 view_open(int nlines, int ncols, int begin_y, int begin_x,
320 enum tog_view_type type)
322 struct tog_view *view = calloc(1, sizeof(*view));
330 view->nlines = nlines ? nlines : LINES - begin_y;
331 view->ncols = ncols ? ncols : COLS - begin_x;
332 view->begin_y = begin_y;
333 view->begin_x = begin_x;
334 view->window = newwin(nlines, ncols, begin_y, begin_x);
335 if (view->window == NULL) {
339 view->panel = new_panel(view->window);
340 if (view->panel == NULL ||
341 set_panel_userptr(view->panel, view) != OK) {
346 keypad(view->window, TRUE);
351 view_split_begin_x(int begin_x)
353 if (begin_x > 0 || COLS < 120)
355 return (COLS - MAX(COLS / 2, 80));
358 static const struct got_error *view_resize(struct tog_view *);
360 static const struct got_error *
361 view_splitscreen(struct tog_view *view)
363 const struct got_error *err = NULL;
366 view->begin_x = view_split_begin_x(0);
367 view->nlines = LINES;
368 view->ncols = COLS - view->begin_x;
371 err = view_resize(view);
375 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
376 return got_error_from_errno();
381 static const struct got_error *
382 view_fullscreen(struct tog_view *view)
384 const struct got_error *err = NULL;
388 view->nlines = LINES;
392 err = view_resize(view);
396 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
397 return got_error_from_errno();
403 view_is_parent_view(struct tog_view *view)
405 return view->parent == NULL;
408 static const struct got_error *
409 view_resize(struct tog_view *view)
413 if (view->lines > LINES)
414 nlines = view->nlines - (view->lines - LINES);
416 nlines = view->nlines + (LINES - view->lines);
418 if (view->cols > COLS)
419 ncols = view->ncols - (view->cols - COLS);
421 ncols = view->ncols + (COLS - view->cols);
423 if (wresize(view->window, nlines, ncols) == ERR)
424 return got_error_from_errno();
425 if (replace_panel(view->panel, view->window) == ERR)
426 return got_error_from_errno();
427 wclear(view->window);
429 view->nlines = nlines;
435 view->child->begin_x = view_split_begin_x(view->begin_x);
436 if (view->child->begin_x == 0) {
437 view_fullscreen(view->child);
438 if (view->child->focussed)
439 show_panel(view->child->panel);
441 show_panel(view->panel);
443 view_splitscreen(view->child);
444 show_panel(view->child->panel);
451 static const struct got_error *
452 view_close_child(struct tog_view *view)
454 const struct got_error *err = NULL;
456 if (view->child == NULL)
459 err = view_close(view->child);
464 static const struct got_error *
465 view_set_child(struct tog_view *view, struct tog_view *child)
467 const struct got_error *err = NULL;
470 child->parent = view;
475 view_is_splitscreen(struct tog_view *view)
477 return !view_is_parent_view(view) && view->begin_x > 0;
486 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
487 cols = 80; /* Default */
493 resize_term(lines, cols);
496 static const struct got_error *
497 view_input(struct tog_view **new, struct tog_view **dead,
498 struct tog_view **focus, int *done, struct tog_view *view,
499 struct tog_view_list_head *views)
501 const struct got_error *err = NULL;
509 nodelay(stdscr, FALSE);
510 /* Allow threads to make progress while we are waiting for input. */
511 errcode = pthread_mutex_unlock(&tog_mutex);
513 return got_error_set_errno(errcode);
514 ch = wgetch(view->window);
515 errcode = pthread_mutex_lock(&tog_mutex);
517 return got_error_set_errno(errcode);
518 nodelay(stdscr, TRUE);
520 if (tog_sigwinch_received) {
522 tog_sigwinch_received = 0;
523 TAILQ_FOREACH(v, views, entry) {
524 err = view_resize(v);
527 err = v->input(new, dead, focus, v, KEY_RESIZE);
538 *focus = view->child;
539 view->child_focussed = 1;
540 } else if (view->parent) {
541 *focus = view->parent;
542 view->parent->child_focussed = 0;
546 err = view->input(new, dead, focus, view, ch);
553 if (view_is_parent_view(view)) {
554 if (view->child == NULL)
556 if (view_is_splitscreen(view->child)) {
557 *focus = view->child;
558 view->child_focussed = 1;
559 err = view_fullscreen(view->child);
561 err = view_splitscreen(view->child);
564 err = view->child->input(new, dead, focus,
565 view->child, KEY_RESIZE);
567 if (view_is_splitscreen(view)) {
569 view->parent->child_focussed = 1;
570 err = view_fullscreen(view);
572 err = view_splitscreen(view);
576 err = view->input(new, dead, focus, view,
583 err = view->input(new, dead, focus, view, ch);
591 view_vborder(struct tog_view *view)
594 struct tog_view *view_above;
597 return view_vborder(view->parent);
599 panel = panel_above(view->panel);
603 view_above = panel_userptr(panel);
604 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
605 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
609 view_needs_focus_indication(struct tog_view *view)
611 if (view_is_parent_view(view)) {
612 if (view->child == NULL || view->child_focussed)
614 if (!view_is_splitscreen(view->child))
616 } else if (!view_is_splitscreen(view))
619 return view->focussed;
622 static const struct got_error *
623 view_loop(struct tog_view *view)
625 const struct got_error *err = NULL;
626 struct tog_view_list_head views;
627 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
628 int fast_refresh = 10;
629 int done = 0, errcode;
631 errcode = pthread_mutex_lock(&tog_mutex);
633 return got_error_set_errno(errcode);
636 TAILQ_INSERT_HEAD(&views, view, entry);
640 err = view->show(view);
645 while (!TAILQ_EMPTY(&views) && !done) {
646 /* Refresh fast during initialization, then become slower. */
647 if (fast_refresh && fast_refresh-- == 0)
648 halfdelay(10); /* switch to once per second */
650 err = view_input(&new_view, &dead_view, &focus_view, &done,
655 struct tog_view *prev = NULL;
657 if (view_is_parent_view(dead_view))
658 prev = TAILQ_PREV(dead_view,
659 tog_view_list_head, entry);
660 else if (view->parent != dead_view)
663 if (dead_view->parent)
664 dead_view->parent->child = NULL;
666 TAILQ_REMOVE(&views, dead_view, entry);
668 err = view_close(dead_view);
669 if (err || dead_view == main_view)
672 if (view == dead_view) {
677 else if (!TAILQ_EMPTY(&views))
678 view = TAILQ_LAST(&views,
683 if (view->child && view->child_focussed)
684 focus_view = view->child;
691 struct tog_view *v, *t;
692 /* Only allow one parent view per type. */
693 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
694 if (v->type != new_view->type)
696 TAILQ_REMOVE(&views, v, entry);
702 TAILQ_INSERT_TAIL(&views, new_view, entry);
704 if (focus_view == NULL)
705 focus_view = new_view;
708 show_panel(focus_view->panel);
711 focus_view->focussed = 1;
714 show_panel(new_view->panel);
715 if (view->child && view_is_splitscreen(view->child))
716 show_panel(view->child->panel);
719 if (focus_view == NULL) {
721 show_panel(view->panel);
722 if (view->child && view_is_splitscreen(view->child))
723 show_panel(view->child->panel);
727 err = view->parent->show(view->parent);
731 err = view->show(view);
735 err = view->child->show(view->child);
744 while (!TAILQ_EMPTY(&views)) {
745 view = TAILQ_FIRST(&views);
746 TAILQ_REMOVE(&views, view, entry);
750 errcode = pthread_mutex_unlock(&tog_mutex);
752 return got_error_set_errno(errcode);
762 "usage: %s log [-c commit] [-r repository-path] [path]\n",
767 /* Create newly allocated wide-character string equivalent to a byte string. */
768 static const struct got_error *
769 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
772 const struct got_error *err = NULL;
775 *wlen = mbstowcs(NULL, s, 0);
776 if (*wlen == (size_t)-1) {
779 return got_error_from_errno();
781 /* byte string invalid in current encoding; try to "fix" it */
782 err = got_mbsavis(&vis, &vislen, s);
785 *wlen = mbstowcs(NULL, vis, 0);
786 if (*wlen == (size_t)-1) {
787 err = got_error_from_errno(); /* give up */
792 *ws = calloc(*wlen + 1, sizeof(*ws));
794 err = got_error_from_errno();
798 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
799 err = got_error_from_errno();
810 /* Format a line for display, ensuring that it won't overflow a width limit. */
811 static const struct got_error *
812 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
814 const struct got_error *err = NULL;
816 wchar_t *wline = NULL;
823 err = mbs2ws(&wline, &wlen, line);
828 while (i < wlen && cols < wlimit) {
829 int width = wcwidth(wline[i]);
836 if (cols + width <= wlimit)
841 if (wline[i] == L'\t')
842 cols += TABSIZE - ((cols + 1) % TABSIZE);
846 err = got_error_from_errno();
861 static const struct got_error *
862 draw_commit(struct tog_view *view, struct got_commit_object *commit,
863 struct got_object_id *id)
865 const struct got_error *err = NULL;
866 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
867 char *logmsg0 = NULL, *logmsg = NULL;
868 char *author0 = NULL, *author = NULL;
869 wchar_t *wlogmsg = NULL, *wauthor = NULL;
870 int author_width, logmsg_width;
871 char *newline, *smallerthan;
874 static const size_t date_display_cols = 9;
875 static const size_t author_display_cols = 16;
876 const int avail = view->ncols;
879 if (localtime_r(&commit->committer_time, &tm) == NULL)
880 return got_error_from_errno();
881 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
883 return got_error(GOT_ERR_NO_SPACE);
885 if (avail < date_display_cols)
886 limit = MIN(sizeof(datebuf) - 1, avail);
888 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
889 waddnstr(view->window, datebuf, limit);
894 author0 = strdup(commit->author);
895 if (author0 == NULL) {
896 err = got_error_from_errno();
900 smallerthan = strchr(author, '<');
904 char *at = strchr(author, '@');
909 err = format_line(&wauthor, &author_width, author, limit);
912 waddwstr(view->window, wauthor);
914 while (col <= avail && author_width < author_display_cols + 1) {
915 waddch(view->window, ' ');
922 logmsg0 = strdup(commit->logmsg);
923 if (logmsg0 == NULL) {
924 err = got_error_from_errno();
928 while (*logmsg == '\n')
930 newline = strchr(logmsg, '\n');
934 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
937 waddwstr(view->window, wlogmsg);
939 while (col <= avail) {
940 waddch(view->window, ' ');
952 static struct commit_queue_entry *
953 alloc_commit_queue_entry(struct got_commit_object *commit,
954 struct got_object_id *id)
956 struct commit_queue_entry *entry;
958 entry = calloc(1, sizeof(*entry));
963 entry->commit = commit;
968 pop_commit(struct commit_queue *commits)
970 struct commit_queue_entry *entry;
972 entry = TAILQ_FIRST(&commits->head);
973 TAILQ_REMOVE(&commits->head, entry, entry);
974 got_object_commit_close(entry->commit);
976 /* Don't free entry->id! It is owned by the commit graph. */
981 free_commits(struct commit_queue *commits)
983 while (!TAILQ_EMPTY(&commits->head))
987 static const struct got_error *
988 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
989 int minqueue, struct got_repository *repo, const char *path)
991 const struct got_error *err = NULL;
995 * We keep all commits open throughout the lifetime of the log
996 * view in order to avoid having to re-fetch commits from disk
997 * while updating the display.
999 while (nqueued < minqueue) {
1000 struct got_object_id *id;
1001 struct got_commit_object *commit;
1002 struct commit_queue_entry *entry;
1005 err = got_commit_graph_iter_next(&id, graph);
1007 if (err->code != GOT_ERR_ITER_NEED_MORE)
1009 err = got_commit_graph_fetch_commits(graph,
1019 err = got_object_open_as_commit(&commit, repo, id);
1022 entry = alloc_commit_queue_entry(commit, id);
1023 if (entry == NULL) {
1024 err = got_error_from_errno();
1028 errcode = pthread_mutex_lock(&tog_mutex);
1030 err = got_error_set_errno(errcode);
1034 entry->idx = commits->ncommits;
1035 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1037 commits->ncommits++;
1039 errcode = pthread_mutex_unlock(&tog_mutex);
1040 if (errcode && err == NULL)
1041 err = got_error_set_errno(errcode);
1047 static const struct got_error *
1048 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1050 const struct got_error *err = NULL;
1051 struct got_reference *head_ref;
1055 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1059 err = got_ref_resolve(head_id, repo, head_ref);
1060 got_ref_close(head_ref);
1069 static const struct got_error *
1070 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1071 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1072 struct commit_queue *commits, int selected_idx, int limit,
1073 const char *path, int commits_needed)
1075 const struct got_error *err = NULL;
1076 struct commit_queue_entry *entry;
1077 int ncommits, width;
1078 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1084 if (ncommits == selected_idx) {
1088 entry = TAILQ_NEXT(entry, entry);
1093 err = got_object_id_str(&id_str, (*selected)->id);
1098 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1099 entry ? entry->idx + 1 : 0, commits->ncommits,
1100 commits_needed == 0 ? "" : " loading...") == -1)
1101 return got_error_from_errno();
1103 if (path && strcmp(path, "/") != 0) {
1104 if (asprintf(&header, "commit: %s %s%s",
1105 id_str ? id_str : "........................................",
1106 path, ncommits_str) == -1) {
1107 err = got_error_from_errno();
1111 } else if (asprintf(&header, "commit: %s%s",
1112 id_str ? id_str : "........................................",
1113 ncommits_str) == -1) {
1114 err = got_error_from_errno();
1118 err = format_line(&wline, &width, header, view->ncols);
1122 werase(view->window);
1124 if (view_needs_focus_indication(view))
1125 wstandout(view->window);
1126 waddwstr(view->window, wline);
1127 while (width < view->ncols) {
1128 waddch(view->window, ' ');
1131 if (view_needs_focus_indication(view))
1132 wstandend(view->window);
1141 if (ncommits >= limit - 1)
1143 if (view->focussed && ncommits == selected_idx)
1144 wstandout(view->window);
1145 err = draw_commit(view, entry->commit, entry->id);
1146 if (view->focussed && ncommits == selected_idx)
1147 wstandend(view->window);
1152 entry = TAILQ_NEXT(entry, entry);
1164 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1165 struct commit_queue *commits)
1167 struct commit_queue_entry *entry;
1170 entry = TAILQ_FIRST(&commits->head);
1171 if (*first_displayed_entry == entry)
1174 entry = *first_displayed_entry;
1175 while (entry && nscrolled < maxscroll) {
1176 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1178 *first_displayed_entry = entry;
1184 static const struct got_error *
1185 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1186 struct commit_queue_entry **last_displayed_entry,
1187 struct commit_queue *commits, int *log_complete, int *commits_needed,
1188 pthread_cond_t *need_commits)
1190 const struct got_error *err = NULL;
1191 struct commit_queue_entry *pentry;
1194 if (*last_displayed_entry == NULL)
1198 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1199 if (pentry == NULL) {
1203 *commits_needed = maxscroll + 20;
1204 errcode = pthread_cond_signal(need_commits);
1206 return got_error_set_errno(errcode);
1209 *last_displayed_entry = pentry;
1211 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1214 *first_displayed_entry = pentry;
1215 } while (++nscrolled < maxscroll);
1220 static const struct got_error *
1221 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1222 struct got_object_id *commit_id, struct got_commit_object *commit,
1223 struct got_repository *repo)
1225 const struct got_error *err;
1226 struct got_object *obj1 = NULL, *obj2 = NULL;
1227 struct got_object_qid *parent_id;
1228 struct tog_view *diff_view;
1230 err = got_object_open(&obj2, repo, commit_id);
1234 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1236 err = got_object_open(&obj1, repo, parent_id->id);
1241 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1242 if (diff_view == NULL) {
1243 err = got_error_from_errno();
1247 err = open_diff_view(diff_view, obj1, obj2, repo);
1249 *new_view = diff_view;
1252 got_object_close(obj1);
1254 got_object_close(obj2);
1258 static const struct got_error *
1259 browse_commit(struct tog_view **new_view, int begin_x,
1260 struct commit_queue_entry *entry, struct got_repository *repo)
1262 const struct got_error *err = NULL;
1263 struct got_tree_object *tree;
1264 struct tog_view *tree_view;
1266 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1270 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1271 if (tree_view == NULL)
1272 return got_error_from_errno();
1274 err = open_tree_view(tree_view, tree, entry->id, repo);
1276 got_object_tree_close(tree);
1278 *new_view = tree_view;
1283 log_thread(void *arg)
1285 const struct got_error *err = NULL;
1287 struct tog_log_thread_args *a = arg;
1290 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1294 while (!done && !err) {
1295 err = queue_commits(a->graph, a->commits, 1, a->repo,
1298 if (err->code != GOT_ERR_ITER_COMPLETED)
1302 } else if (a->commits_needed > 0)
1303 a->commits_needed--;
1305 errcode = pthread_mutex_lock(&tog_mutex);
1307 return (void *)got_error_set_errno(errcode);
1310 a->log_complete = 1;
1311 else if (*a->quit) {
1313 a->log_complete = 1;
1314 } else if (*a->first_displayed_entry == NULL) {
1315 *a->first_displayed_entry =
1316 TAILQ_FIRST(&a->commits->head);
1317 *a->selected_entry = *a->first_displayed_entry;
1321 a->commits_needed = 0;
1322 else if (a->commits_needed == 0) {
1323 errcode = pthread_cond_wait(&a->need_commits,
1326 err = got_error_set_errno(errcode);
1329 errcode = pthread_mutex_unlock(&tog_mutex);
1330 if (errcode && err == NULL)
1331 err = got_error_set_errno(errcode);
1336 static const struct got_error *
1337 stop_log_thread(struct tog_log_view_state *s)
1339 const struct got_error *err = NULL;
1344 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1346 return got_error_set_errno(errcode);
1347 errcode = pthread_mutex_unlock(&tog_mutex);
1349 return got_error_set_errno(errcode);
1350 errcode = pthread_join(s->thread, (void **)&err);
1352 return got_error_set_errno(errcode);
1353 errcode = pthread_mutex_lock(&tog_mutex);
1355 return got_error_set_errno(errcode);
1359 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1360 if (errcode && err == NULL)
1361 err = got_error_set_errno(errcode);
1363 if (s->thread_args.repo) {
1364 got_repo_close(s->thread_args.repo);
1365 s->thread_args.repo = NULL;
1368 if (s->thread_args.graph) {
1369 got_commit_graph_close(s->thread_args.graph);
1370 s->thread_args.graph = NULL;
1376 static const struct got_error *
1377 close_log_view(struct tog_view *view)
1379 const struct got_error *err = NULL;
1380 struct tog_log_view_state *s = &view->state.log;
1382 err = stop_log_thread(s);
1383 free_commits(&s->commits);
1384 free(s->in_repo_path);
1385 s->in_repo_path = NULL;
1391 static const struct got_error *
1392 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1393 struct got_repository *repo, const char *path, int check_disk)
1395 const struct got_error *err = NULL;
1396 struct tog_log_view_state *s = &view->state.log;
1397 struct got_repository *thread_repo = NULL;
1398 struct got_commit_graph *thread_graph = NULL;
1401 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1405 /* The commit queue only contains commits being displayed. */
1406 TAILQ_INIT(&s->commits.head);
1407 s->commits.ncommits = 0;
1410 s->start_id = got_object_id_dup(start_id);
1411 if (s->start_id == NULL) {
1412 err = got_error_from_errno();
1416 view->show = show_log_view;
1417 view->input = input_log_view;
1418 view->close = close_log_view;
1420 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1423 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1428 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1430 err = got_error_set_errno(errcode);
1434 s->thread_args.commits_needed = view->nlines;
1435 s->thread_args.graph = thread_graph;
1436 s->thread_args.commits = &s->commits;
1437 s->thread_args.in_repo_path = s->in_repo_path;
1438 s->thread_args.start_id = s->start_id;
1439 s->thread_args.repo = thread_repo;
1440 s->thread_args.log_complete = 0;
1441 s->thread_args.quit = &s->quit;
1442 s->thread_args.view = view;
1443 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1444 s->thread_args.selected_entry = &s->selected_entry;
1447 close_log_view(view);
1451 static const struct got_error *
1452 show_log_view(struct tog_view *view)
1454 struct tog_log_view_state *s = &view->state.log;
1456 if (s->thread == NULL) {
1457 int errcode = pthread_create(&s->thread, NULL, log_thread,
1460 return got_error_set_errno(errcode);
1463 return draw_commits(view, &s->last_displayed_entry,
1464 &s->selected_entry, s->first_displayed_entry,
1465 &s->commits, s->selected, view->nlines,
1466 s->in_repo_path, s->thread_args.commits_needed);
1469 static const struct got_error *
1470 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1471 struct tog_view **focus_view, struct tog_view *view, int ch)
1473 const struct got_error *err = NULL;
1474 struct tog_log_view_state *s = &view->state.log;
1476 struct tog_view *diff_view = NULL, *tree_view = NULL;
1485 if (s->first_displayed_entry == NULL)
1487 if (s->selected > 0)
1489 if (s->selected > 0)
1491 scroll_up(&s->first_displayed_entry, 1,
1495 if (s->first_displayed_entry == NULL)
1497 if (TAILQ_FIRST(&s->commits.head) ==
1498 s->first_displayed_entry) {
1502 scroll_up(&s->first_displayed_entry,
1503 view->nlines, &s->commits);
1507 if (s->first_displayed_entry == NULL)
1509 if (s->selected < MIN(view->nlines - 2,
1510 s->commits.ncommits - 1)) {
1514 err = scroll_down(&s->first_displayed_entry, 1,
1515 &s->last_displayed_entry, &s->commits,
1516 &s->thread_args.log_complete,
1517 &s->thread_args.commits_needed,
1518 &s->thread_args.need_commits);
1521 struct commit_queue_entry *first;
1522 first = s->first_displayed_entry;
1525 err = scroll_down(&s->first_displayed_entry,
1526 view->nlines, &s->last_displayed_entry,
1527 &s->commits, &s->thread_args.log_complete,
1528 &s->thread_args.commits_needed,
1529 &s->thread_args.need_commits);
1530 if (first == s->first_displayed_entry &&
1531 s->selected < MIN(view->nlines - 2,
1532 s->commits.ncommits - 1)) {
1533 /* can't scroll further down */
1534 s->selected = MIN(view->nlines - 2,
1535 s->commits.ncommits - 1);
1541 if (s->selected > view->nlines - 2)
1542 s->selected = view->nlines - 2;
1543 if (s->selected > s->commits.ncommits - 1)
1544 s->selected = s->commits.ncommits - 1;
1548 if (s->selected_entry == NULL)
1550 if (view_is_parent_view(view))
1551 begin_x = view_split_begin_x(view->begin_x);
1552 err = open_diff_view_for_commit(&diff_view, begin_x,
1553 s->selected_entry->id, s->selected_entry->commit,
1557 if (view_is_parent_view(view)) {
1558 err = view_close_child(view);
1561 err = view_set_child(view, diff_view);
1563 view_close(diff_view);
1566 *focus_view = diff_view;
1567 view->child_focussed = 1;
1569 *new_view = diff_view;
1572 if (s->selected_entry == NULL)
1574 if (view_is_parent_view(view))
1575 begin_x = view_split_begin_x(view->begin_x);
1576 err = browse_commit(&tree_view, begin_x,
1577 s->selected_entry, s->repo);
1580 if (view_is_parent_view(view)) {
1581 err = view_close_child(view);
1584 err = view_set_child(view, tree_view);
1586 view_close(tree_view);
1589 *focus_view = tree_view;
1590 view->child_focussed = 1;
1592 *new_view = tree_view;
1595 if (strcmp(s->in_repo_path, "/") == 0)
1597 parent_path = dirname(s->in_repo_path);
1598 if (parent_path && strcmp(parent_path, ".") != 0) {
1599 struct tog_view *lv;
1600 err = stop_log_thread(s);
1603 lv = view_open(view->nlines, view->ncols,
1604 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1606 return got_error_from_errno();
1607 err = open_log_view(lv, s->start_id, s->repo,
1611 if (view_is_parent_view(view))
1614 view_set_child(view->parent, lv);
1627 static const struct got_error *
1628 cmd_log(int argc, char *argv[])
1630 const struct got_error *error;
1631 struct got_repository *repo = NULL;
1632 struct got_object_id *start_id = NULL;
1633 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1634 char *start_commit = NULL;
1636 struct tog_view *view;
1639 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
1644 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1647 start_commit = optarg;
1650 repo_path = realpath(optarg, NULL);
1651 if (repo_path == NULL)
1652 err(1, "-r option");
1666 path = strdup(argv[0]);
1670 return got_error_from_errno();
1672 cwd = getcwd(NULL, 0);
1674 error = got_error_from_errno();
1677 if (repo_path == NULL) {
1678 repo_path = strdup(cwd);
1679 if (repo_path == NULL) {
1680 error = got_error_from_errno();
1685 error = got_repo_open(&repo, repo_path);
1689 if (start_commit == NULL) {
1690 error = get_head_commit_id(&start_id, repo);
1694 struct got_object *obj;
1695 error = got_object_open_by_id_str(&obj, repo, start_commit);
1696 if (error == NULL) {
1697 start_id = got_object_id_dup(got_object_get_id(obj));
1698 if (start_id == NULL)
1699 error = got_error_from_errno();
1706 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1708 error = got_error_from_errno();
1711 error = open_log_view(view, start_id, repo, path, 1);
1714 error = view_loop(view);
1721 got_repo_close(repo);
1729 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1735 parse_next_line(FILE *f, size_t *len)
1740 const char delim[3] = { '\0', '\0', '\0'};
1742 line = fparseln(f, &linelen, &lineno, delim, 0);
1748 static const struct got_error *
1749 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1750 int *last_displayed_line, int *eof, int max_lines,
1753 const struct got_error *err;
1754 int nlines = 0, nprinted = 0;
1761 werase(view->window);
1764 err = format_line(&wline, &width, header, view->ncols);
1769 if (view_needs_focus_indication(view))
1770 wstandout(view->window);
1771 waddwstr(view->window, wline);
1772 if (view_needs_focus_indication(view))
1773 wstandend(view->window);
1774 if (width < view->ncols)
1775 waddch(view->window, '\n');
1783 while (nprinted < max_lines) {
1784 line = parse_next_line(f, &len);
1789 if (++nlines < *first_displayed_line) {
1794 err = format_line(&wline, &width, line, view->ncols);
1799 waddwstr(view->window, wline);
1800 if (width < view->ncols)
1801 waddch(view->window, '\n');
1802 if (++nprinted == 1)
1803 *first_displayed_line = nlines;
1808 *last_displayed_line = nlines;
1815 static const struct got_error *
1816 create_diff(struct tog_diff_view_state *s)
1818 const struct got_error *err = NULL;
1819 struct got_object *obj1 = NULL, *obj2 = NULL;
1823 err = got_object_open(&obj1, s->repo, s->id1);
1828 err = got_object_open(&obj2, s->repo, s->id2);
1834 err = got_error_from_errno();
1841 switch (got_object_get_type(obj1 ? obj1 : obj2)) {
1842 case GOT_OBJ_TYPE_BLOB:
1843 err = got_diff_objects_as_blobs(obj1, obj2, NULL, NULL,
1844 s->diff_context, s->repo, f);
1846 case GOT_OBJ_TYPE_TREE:
1847 err = got_diff_objects_as_trees(obj1, obj2, "", "",
1848 s->diff_context, s->repo, f);
1850 case GOT_OBJ_TYPE_COMMIT:
1851 err = got_diff_objects_as_commits(obj1, obj2, s->diff_context,
1855 err = got_error(GOT_ERR_OBJ_TYPE);
1860 got_object_close(obj1);
1861 got_object_close(obj2);
1867 static const struct got_error *
1868 open_diff_view(struct tog_view *view, struct got_object *obj1,
1869 struct got_object *obj2, struct got_repository *repo)
1871 const struct got_error *err;
1873 if (obj1 != NULL && obj2 != NULL &&
1874 got_object_get_type(obj1) != got_object_get_type(obj2))
1875 return got_error(GOT_ERR_OBJ_TYPE);
1878 struct got_object_id *id1;
1879 id1 = got_object_id_dup(got_object_get_id(obj1));
1881 return got_error_from_errno();
1882 view->state.diff.id1 = id1;
1884 view->state.diff.id1 = NULL;
1886 view->state.diff.id2 = got_object_id_dup(got_object_get_id(obj2));
1887 if (view->state.diff.id2 == NULL) {
1888 free(view->state.diff.id1);
1889 view->state.diff.id1 = NULL;
1890 return got_error_from_errno();
1892 view->state.diff.f = NULL;
1893 view->state.diff.first_displayed_line = 1;
1894 view->state.diff.last_displayed_line = view->nlines;
1895 view->state.diff.diff_context = 3;
1896 view->state.diff.repo = repo;
1898 err = create_diff(&view->state.diff);
1900 free(view->state.diff.id1);
1901 view->state.diff.id1 = NULL;
1902 free(view->state.diff.id2);
1903 view->state.diff.id2 = NULL;
1907 view->show = show_diff_view;
1908 view->input = input_diff_view;
1909 view->close = close_diff_view;
1914 static const struct got_error *
1915 close_diff_view(struct tog_view *view)
1917 const struct got_error *err = NULL;
1919 free(view->state.diff.id1);
1920 view->state.diff.id1 = NULL;
1921 free(view->state.diff.id2);
1922 view->state.diff.id2 = NULL;
1923 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
1924 err = got_error_from_errno();
1928 static const struct got_error *
1929 show_diff_view(struct tog_view *view)
1931 const struct got_error *err;
1932 struct tog_diff_view_state *s = &view->state.diff;
1933 char *id_str1 = NULL, *id_str2, *header;
1936 err = got_object_id_str(&id_str1, s->id1);
1940 err = got_object_id_str(&id_str2, s->id2);
1944 if (asprintf(&header, "diff: %s %s",
1945 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
1946 err = got_error_from_errno();
1954 return draw_file(view, s->f, &s->first_displayed_line,
1955 &s->last_displayed_line, &s->eof, view->nlines,
1959 static const struct got_error *
1960 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
1961 struct tog_view **focus_view, struct tog_view *view, int ch)
1963 const struct got_error *err = NULL;
1964 struct tog_diff_view_state *s = &view->state.diff;
1970 if (s->first_displayed_line > 1)
1971 s->first_displayed_line--;
1975 while (i++ < view->nlines - 1 &&
1976 s->first_displayed_line > 1)
1977 s->first_displayed_line--;
1982 s->first_displayed_line++;
1987 while (!s->eof && i++ < view->nlines - 1) {
1989 line = parse_next_line(s->f, NULL);
1990 s->first_displayed_line++;
1996 if (s->diff_context > 0) {
1998 err = create_diff(s);
2002 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2004 err = create_diff(s);
2014 static const struct got_error *
2015 cmd_diff(int argc, char *argv[])
2017 const struct got_error *error = NULL;
2018 struct got_repository *repo = NULL;
2019 struct got_object *obj1 = NULL, *obj2 = NULL;
2020 char *repo_path = NULL;
2021 char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
2023 struct tog_view *view;
2026 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2031 while ((ch = getopt(argc, argv, "")) != -1) {
2043 usage_diff(); /* TODO show local worktree changes */
2044 } else if (argc == 2) {
2045 repo_path = getcwd(NULL, 0);
2046 if (repo_path == NULL)
2047 return got_error_from_errno();
2048 obj_id_str1 = argv[0];
2049 obj_id_str2 = argv[1];
2050 } else if (argc == 3) {
2051 repo_path = realpath(argv[0], NULL);
2052 if (repo_path == NULL)
2053 return got_error_from_errno();
2054 obj_id_str1 = argv[1];
2055 obj_id_str2 = argv[2];
2059 error = got_repo_open(&repo, repo_path);
2064 error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
2068 error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
2072 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2074 error = got_error_from_errno();
2077 error = open_diff_view(view, obj1, obj2, repo);
2080 error = view_loop(view);
2082 got_repo_close(repo);
2084 got_object_close(obj1);
2086 got_object_close(obj2);
2094 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2099 struct tog_blame_line {
2101 struct got_object_id *id;
2104 static const struct got_error *
2105 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2106 const char *path, struct tog_blame_line *lines, int nlines,
2107 int blame_complete, int selected_line, int *first_displayed_line,
2108 int *last_displayed_line, int *eof, int max_lines)
2110 const struct got_error *err;
2111 int lineno = 0, nprinted = 0;
2116 struct tog_blame_line *blame_line;
2117 struct got_object_id *prev_id = NULL;
2120 err = got_object_id_str(&id_str, id);
2125 werase(view->window);
2127 if (asprintf(&line, "commit: %s", id_str) == -1) {
2128 err = got_error_from_errno();
2133 err = format_line(&wline, &width, line, view->ncols);
2136 if (view_needs_focus_indication(view))
2137 wstandout(view->window);
2138 waddwstr(view->window, wline);
2139 if (view_needs_focus_indication(view))
2140 wstandend(view->window);
2143 if (width < view->ncols)
2144 waddch(view->window, '\n');
2146 if (asprintf(&line, "[%d/%d] %s%s",
2147 *first_displayed_line - 1 + selected_line, nlines,
2148 blame_complete ? "" : "annotating ", path) == -1) {
2150 return got_error_from_errno();
2153 err = format_line(&wline, &width, line, view->ncols);
2158 waddwstr(view->window, wline);
2161 if (width < view->ncols)
2162 waddch(view->window, '\n');
2165 while (nprinted < max_lines - 2) {
2166 line = parse_next_line(f, &len);
2171 if (++lineno < *first_displayed_line) {
2176 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2177 err = format_line(&wline, &width, line, wlimit);
2183 if (view->focussed && nprinted == selected_line - 1)
2184 wstandout(view->window);
2186 blame_line = &lines[lineno - 1];
2187 if (blame_line->annotated && prev_id &&
2188 got_object_id_cmp(prev_id, blame_line->id) == 0)
2189 waddstr(view->window, " ");
2190 else if (blame_line->annotated) {
2192 err = got_object_id_str(&id_str, blame_line->id);
2198 wprintw(view->window, "%.8s ", id_str);
2200 prev_id = blame_line->id;
2202 waddstr(view->window, "........ ");
2206 waddwstr(view->window, wline);
2207 while (width < wlimit) {
2208 waddch(view->window, ' ');
2211 if (view->focussed && nprinted == selected_line - 1)
2212 wstandend(view->window);
2213 if (++nprinted == 1)
2214 *first_displayed_line = lineno;
2219 *last_displayed_line = lineno;
2226 static const struct got_error *
2227 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2229 const struct got_error *err = NULL;
2230 struct tog_blame_cb_args *a = arg;
2231 struct tog_blame_line *line;
2234 if (nlines != a->nlines ||
2235 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2236 return got_error(GOT_ERR_RANGE);
2238 errcode = pthread_mutex_lock(&tog_mutex);
2240 return got_error_set_errno(errcode);
2242 if (*a->quit) { /* user has quit the blame view */
2243 err = got_error(GOT_ERR_ITER_COMPLETED);
2248 goto done; /* no change in this commit */
2250 line = &a->lines[lineno - 1];
2251 if (line->annotated)
2254 line->id = got_object_id_dup(id);
2255 if (line->id == NULL) {
2256 err = got_error_from_errno();
2259 line->annotated = 1;
2261 errcode = pthread_mutex_unlock(&tog_mutex);
2263 err = got_error_set_errno(errcode);
2268 blame_thread(void *arg)
2270 const struct got_error *err;
2271 struct tog_blame_thread_args *ta = arg;
2272 struct tog_blame_cb_args *a = ta->cb_args;
2275 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2276 blame_cb, ta->cb_args);
2278 errcode = pthread_mutex_lock(&tog_mutex);
2280 return (void *)got_error_set_errno(errcode);
2282 got_repo_close(ta->repo);
2286 errcode = pthread_mutex_unlock(&tog_mutex);
2287 if (errcode && err == NULL)
2288 err = got_error_set_errno(errcode);
2293 static struct got_object_id *
2294 get_selected_commit_id(struct tog_blame_line *lines,
2295 int first_displayed_line, int selected_line)
2297 struct tog_blame_line *line;
2299 line = &lines[first_displayed_line - 1 + selected_line - 1];
2300 if (!line->annotated)
2306 static const struct got_error *
2307 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2308 struct tog_blame_line *lines, int first_displayed_line,
2309 int selected_line, struct got_repository *repo)
2311 const struct got_error *err = NULL;
2312 struct got_commit_object *commit = NULL;
2313 struct got_object_id *selected_id;
2314 struct got_object_qid *pid;
2319 selected_id = get_selected_commit_id(lines,
2320 first_displayed_line, selected_line);
2321 if (selected_id == NULL)
2324 err = got_object_open(obj, repo, selected_id);
2328 err = got_object_commit_open(&commit, repo, *obj);
2332 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2334 err = got_object_open(pobj, repo, pid->id);
2340 got_object_commit_close(commit);
2344 static const struct got_error *
2345 stop_blame(struct tog_blame *blame)
2347 const struct got_error *err = NULL;
2350 if (blame->thread) {
2352 errcode = pthread_mutex_unlock(&tog_mutex);
2354 return got_error_set_errno(errcode);
2355 errcode = pthread_join(blame->thread, (void **)&err);
2357 return got_error_set_errno(errcode);
2358 errcode = pthread_mutex_lock(&tog_mutex);
2360 return got_error_set_errno(errcode);
2361 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2363 blame->thread = NULL;
2365 if (blame->thread_args.repo) {
2366 got_repo_close(blame->thread_args.repo);
2367 blame->thread_args.repo = NULL;
2373 for (i = 0; i < blame->nlines; i++)
2374 free(blame->lines[i].id);
2376 blame->lines = NULL;
2377 free(blame->cb_args.commit_id);
2378 blame->cb_args.commit_id = NULL;
2383 static const struct got_error *
2384 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2385 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2386 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2387 struct got_repository *repo)
2389 const struct got_error *err = NULL;
2390 struct got_blob_object *blob = NULL;
2391 struct got_repository *thread_repo = NULL;
2392 struct got_object_id *obj_id = NULL;
2393 struct got_object *obj = NULL;
2395 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2399 err = got_object_open(&obj, repo, obj_id);
2403 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2404 err = got_error(GOT_ERR_OBJ_TYPE);
2408 err = got_object_blob_open(&blob, repo, obj, 8192);
2411 blame->f = got_opentemp();
2412 if (blame->f == NULL) {
2413 err = got_error_from_errno();
2416 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2421 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2422 if (blame->lines == NULL) {
2423 err = got_error_from_errno();
2427 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2431 blame->cb_args.view = view;
2432 blame->cb_args.lines = blame->lines;
2433 blame->cb_args.nlines = blame->nlines;
2434 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2435 if (blame->cb_args.commit_id == NULL) {
2436 err = got_error_from_errno();
2439 blame->cb_args.quit = done;
2441 blame->thread_args.path = path;
2442 blame->thread_args.repo = thread_repo;
2443 blame->thread_args.cb_args = &blame->cb_args;
2444 blame->thread_args.complete = blame_complete;
2445 *blame_complete = 0;
2449 got_object_blob_close(blob);
2452 got_object_close(obj);
2458 static const struct got_error *
2459 open_blame_view(struct tog_view *view, char *path,
2460 struct got_object_id *commit_id, struct got_repository *repo)
2462 const struct got_error *err = NULL;
2463 struct tog_blame_view_state *s = &view->state.blame;
2465 SIMPLEQ_INIT(&s->blamed_commits);
2467 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2471 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2472 s->first_displayed_line = 1;
2473 s->last_displayed_line = view->nlines;
2474 s->selected_line = 1;
2475 s->blame_complete = 0;
2477 if (s->path == NULL)
2478 return got_error_from_errno();
2480 s->commit_id = commit_id;
2481 memset(&s->blame, 0, sizeof(s->blame));
2483 view->show = show_blame_view;
2484 view->input = input_blame_view;
2485 view->close = close_blame_view;
2487 return run_blame(&s->blame, view, &s->blame_complete,
2488 &s->first_displayed_line, &s->last_displayed_line,
2489 &s->selected_line, &s->done, &s->eof, s->path,
2490 s->blamed_commit->id, s->repo);
2493 static const struct got_error *
2494 close_blame_view(struct tog_view *view)
2496 const struct got_error *err = NULL;
2497 struct tog_blame_view_state *s = &view->state.blame;
2499 if (s->blame.thread)
2500 err = stop_blame(&s->blame);
2502 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2503 struct got_object_qid *blamed_commit;
2504 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2505 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2506 got_object_qid_free(blamed_commit);
2514 static const struct got_error *
2515 show_blame_view(struct tog_view *view)
2517 const struct got_error *err = NULL;
2518 struct tog_blame_view_state *s = &view->state.blame;
2521 if (s->blame.thread == NULL) {
2522 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2523 &s->blame.thread_args);
2525 return got_error_set_errno(errcode);
2528 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2529 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2530 s->selected_line, &s->first_displayed_line,
2531 &s->last_displayed_line, &s->eof, view->nlines);
2537 static const struct got_error *
2538 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2539 struct tog_view **focus_view, struct tog_view *view, int ch)
2541 const struct got_error *err = NULL, *thread_err = NULL;
2542 struct got_object *obj = NULL, *pobj = NULL;
2543 struct tog_view *diff_view;
2544 struct tog_blame_view_state *s = &view->state.blame;
2553 if (s->selected_line > 1)
2555 else if (s->selected_line == 1 &&
2556 s->first_displayed_line > 1)
2557 s->first_displayed_line--;
2560 if (s->first_displayed_line == 1) {
2561 s->selected_line = 1;
2564 if (s->first_displayed_line > view->nlines - 2)
2565 s->first_displayed_line -=
2568 s->first_displayed_line = 1;
2572 if (s->selected_line < view->nlines - 2 &&
2573 s->first_displayed_line +
2574 s->selected_line <= s->blame.nlines)
2576 else if (s->last_displayed_line <
2578 s->first_displayed_line++;
2582 struct got_object_id *id;
2583 id = get_selected_commit_id(s->blame.lines,
2584 s->first_displayed_line, s->selected_line);
2585 if (id == NULL || got_object_id_cmp(id,
2586 s->blamed_commit->id) == 0)
2588 err = open_selected_commit(&pobj, &obj,
2589 s->blame.lines, s->first_displayed_line,
2590 s->selected_line, s->repo);
2593 if (pobj == NULL && obj == NULL)
2595 if (ch == 'p' && pobj == NULL)
2598 thread_err = stop_blame(&s->blame);
2602 id = got_object_get_id(ch == 'b' ? obj : pobj);
2603 got_object_close(obj);
2606 got_object_close(pobj);
2609 err = got_object_qid_alloc(&s->blamed_commit, id);
2612 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2613 s->blamed_commit, entry);
2614 err = run_blame(&s->blame, view, &s->blame_complete,
2615 &s->first_displayed_line, &s->last_displayed_line,
2616 &s->selected_line, &s->done, &s->eof,
2617 s->path, s->blamed_commit->id, s->repo);
2623 struct got_object_qid *first;
2624 first = SIMPLEQ_FIRST(&s->blamed_commits);
2625 if (!got_object_id_cmp(first->id, s->commit_id))
2628 thread_err = stop_blame(&s->blame);
2632 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2633 got_object_qid_free(s->blamed_commit);
2635 SIMPLEQ_FIRST(&s->blamed_commits);
2636 err = run_blame(&s->blame, view, &s->blame_complete,
2637 &s->first_displayed_line, &s->last_displayed_line,
2638 &s->selected_line, &s->done, &s->eof, s->path,
2639 s->blamed_commit->id, s->repo);
2646 err = open_selected_commit(&pobj, &obj,
2647 s->blame.lines, s->first_displayed_line,
2648 s->selected_line, s->repo);
2651 if (pobj == NULL && obj == NULL)
2654 if (view_is_parent_view(view))
2655 begin_x = view_split_begin_x(view->begin_x);
2656 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2657 if (diff_view == NULL) {
2658 err = got_error_from_errno();
2661 err = open_diff_view(diff_view, pobj, obj, s->repo);
2663 view_close(diff_view);
2666 if (view_is_parent_view(view)) {
2667 err = view_close_child(view);
2670 err = view_set_child(view, diff_view);
2672 view_close(diff_view);
2675 *focus_view = diff_view;
2676 view->child_focussed = 1;
2678 *new_view = diff_view;
2680 got_object_close(pobj);
2683 got_object_close(obj);
2690 if (s->last_displayed_line >= s->blame.nlines &&
2691 s->selected_line < view->nlines - 2) {
2692 s->selected_line = MIN(s->blame.nlines,
2696 if (s->last_displayed_line + view->nlines - 2
2698 s->first_displayed_line +=
2701 s->first_displayed_line =
2706 if (s->selected_line > view->nlines - 2) {
2707 s->selected_line = MIN(s->blame.nlines,
2716 got_object_close(pobj);
2717 return thread_err ? thread_err : err;
2720 static const struct got_error *
2721 cmd_blame(int argc, char *argv[])
2723 const struct got_error *error;
2724 struct got_repository *repo = NULL;
2725 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2726 struct got_object_id *commit_id = NULL;
2727 char *commit_id_str = NULL;
2729 struct tog_view *view;
2732 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2737 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2740 commit_id_str = optarg;
2743 repo_path = realpath(optarg, NULL);
2744 if (repo_path == NULL)
2745 err(1, "-r option");
2761 cwd = getcwd(NULL, 0);
2763 error = got_error_from_errno();
2766 if (repo_path == NULL) {
2767 repo_path = strdup(cwd);
2768 if (repo_path == NULL) {
2769 error = got_error_from_errno();
2775 error = got_repo_open(&repo, repo_path);
2779 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2783 if (commit_id_str == NULL) {
2784 struct got_reference *head_ref;
2785 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2788 error = got_ref_resolve(&commit_id, repo, head_ref);
2789 got_ref_close(head_ref);
2791 struct got_object *obj;
2792 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2795 commit_id = got_object_id_dup(got_object_get_id(obj));
2796 if (commit_id == NULL)
2797 error = got_error_from_errno();
2798 got_object_close(obj);
2803 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2805 error = got_error_from_errno();
2808 error = open_blame_view(view, in_repo_path, commit_id, repo);
2811 error = view_loop(view);
2817 got_repo_close(repo);
2821 static const struct got_error *
2822 draw_tree_entries(struct tog_view *view,
2823 struct got_tree_entry **first_displayed_entry,
2824 struct got_tree_entry **last_displayed_entry,
2825 struct got_tree_entry **selected_entry, int *ndisplayed,
2826 const char *label, int show_ids, const char *parent_path,
2827 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2829 const struct got_error *err = NULL;
2830 struct got_tree_entry *te;
2836 werase(view->window);
2841 err = format_line(&wline, &width, label, view->ncols);
2844 if (view_needs_focus_indication(view))
2845 wstandout(view->window);
2846 waddwstr(view->window, wline);
2847 if (view_needs_focus_indication(view))
2848 wstandend(view->window);
2851 if (width < view->ncols)
2852 waddch(view->window, '\n');
2855 err = format_line(&wline, &width, parent_path, view->ncols);
2858 waddwstr(view->window, wline);
2861 if (width < view->ncols)
2862 waddch(view->window, '\n');
2865 waddch(view->window, '\n');
2869 te = SIMPLEQ_FIRST(&entries->head);
2870 if (*first_displayed_entry == NULL) {
2871 if (selected == 0) {
2873 wstandout(view->window);
2874 *selected_entry = NULL;
2876 waddstr(view->window, " ..\n"); /* parent directory */
2877 if (selected == 0 && view->focussed)
2878 wstandend(view->window);
2885 while (te != *first_displayed_entry)
2886 te = SIMPLEQ_NEXT(te, entry);
2890 char *line = NULL, *id_str = NULL;
2893 err = got_object_id_str(&id_str, te->id);
2895 return got_error_from_errno();
2897 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2898 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2900 return got_error_from_errno();
2903 err = format_line(&wline, &width, line, view->ncols);
2908 if (n == selected) {
2910 wstandout(view->window);
2911 *selected_entry = te;
2913 waddwstr(view->window, wline);
2914 if (width < view->ncols)
2915 waddch(view->window, '\n');
2916 if (n == selected && view->focussed)
2917 wstandend(view->window);
2923 *last_displayed_entry = te;
2926 te = SIMPLEQ_NEXT(te, entry);
2933 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2934 const struct got_tree_entries *entries, int isroot)
2936 struct got_tree_entry *te, *prev;
2939 if (*first_displayed_entry == NULL)
2942 te = SIMPLEQ_FIRST(&entries->head);
2943 if (*first_displayed_entry == te) {
2945 *first_displayed_entry = NULL;
2949 /* XXX this is stupid... switch to TAILQ? */
2950 for (i = 0; i < maxscroll; i++) {
2951 while (te != *first_displayed_entry) {
2953 te = SIMPLEQ_NEXT(te, entry);
2955 *first_displayed_entry = prev;
2956 te = SIMPLEQ_FIRST(&entries->head);
2958 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2959 *first_displayed_entry = NULL;
2963 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2964 struct got_tree_entry *last_displayed_entry,
2965 const struct got_tree_entries *entries)
2967 struct got_tree_entry *next;
2970 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2973 if (*first_displayed_entry)
2974 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2976 next = SIMPLEQ_FIRST(&entries->head);
2978 *first_displayed_entry = next;
2979 if (++n >= maxscroll)
2981 next = SIMPLEQ_NEXT(next, entry);
2985 static const struct got_error *
2986 tree_entry_path(char **path, struct tog_parent_trees *parents,
2987 struct got_tree_entry *te)
2989 const struct got_error *err = NULL;
2990 struct tog_parent_tree *pt;
2991 size_t len = 2; /* for leading slash and NUL */
2993 TAILQ_FOREACH(pt, parents, entry)
2994 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2996 len += strlen(te->name);
2998 *path = calloc(1, len);
3000 return got_error_from_errno();
3003 pt = TAILQ_LAST(parents, tog_parent_trees);
3005 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3006 err = got_error(GOT_ERR_NO_SPACE);
3009 if (strlcat(*path, "/", len) >= len) {
3010 err = got_error(GOT_ERR_NO_SPACE);
3013 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3016 if (strlcat(*path, te->name, len) >= len) {
3017 err = got_error(GOT_ERR_NO_SPACE);
3029 static const struct got_error *
3030 blame_tree_entry(struct tog_view **new_view, int begin_x,
3031 struct got_tree_entry *te, struct tog_parent_trees *parents,
3032 struct got_object_id *commit_id, struct got_repository *repo)
3034 const struct got_error *err = NULL;
3036 struct tog_view *blame_view;
3038 err = tree_entry_path(&path, parents, te);
3042 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3043 if (blame_view == NULL)
3044 return got_error_from_errno();
3046 err = open_blame_view(blame_view, path, commit_id, repo);
3048 view_close(blame_view);
3051 *new_view = blame_view;
3055 static const struct got_error *
3056 log_tree_entry(struct tog_view **new_view, int begin_x,
3057 struct got_tree_entry *te, struct tog_parent_trees *parents,
3058 struct got_object_id *commit_id, struct got_repository *repo)
3060 struct tog_view *log_view;
3061 const struct got_error *err = NULL;
3064 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3065 if (log_view == NULL)
3066 return got_error_from_errno();
3068 err = tree_entry_path(&path, parents, te);
3072 err = open_log_view(log_view, commit_id, repo, path, 0);
3074 view_close(log_view);
3076 *new_view = log_view;
3081 static const struct got_error *
3082 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3083 struct got_object_id *commit_id, struct got_repository *repo)
3085 const struct got_error *err = NULL;
3086 char *commit_id_str = NULL;
3087 struct tog_tree_view_state *s = &view->state.tree;
3089 TAILQ_INIT(&s->parents);
3091 err = got_object_id_str(&commit_id_str, commit_id);
3095 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3096 err = got_error_from_errno();
3100 s->root = s->tree = root;
3101 s->entries = got_object_tree_get_entries(root);
3102 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3103 s->commit_id = got_object_id_dup(commit_id);
3104 if (s->commit_id == NULL) {
3105 err = got_error_from_errno();
3110 view->show = show_tree_view;
3111 view->input = input_tree_view;
3112 view->close = close_tree_view;
3114 free(commit_id_str);
3116 free(s->tree_label);
3117 s->tree_label = NULL;
3122 static const struct got_error *
3123 close_tree_view(struct tog_view *view)
3125 struct tog_tree_view_state *s = &view->state.tree;
3127 free(s->tree_label);
3128 s->tree_label = NULL;
3130 s->commit_id = NULL;
3131 while (!TAILQ_EMPTY(&s->parents)) {
3132 struct tog_parent_tree *parent;
3133 parent = TAILQ_FIRST(&s->parents);
3134 TAILQ_REMOVE(&s->parents, parent, entry);
3138 if (s->tree != s->root)
3139 got_object_tree_close(s->tree);
3140 got_object_tree_close(s->root);
3145 static const struct got_error *
3146 show_tree_view(struct tog_view *view)
3148 const struct got_error *err = NULL;
3149 struct tog_tree_view_state *s = &view->state.tree;
3152 err = tree_entry_path(&parent_path, &s->parents, NULL);
3156 err = draw_tree_entries(view, &s->first_displayed_entry,
3157 &s->last_displayed_entry, &s->selected_entry,
3158 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3159 s->entries, s->selected, view->nlines, s->tree == s->root);
3166 static const struct got_error *
3167 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3168 struct tog_view **focus_view, struct tog_view *view, int ch)
3170 const struct got_error *err = NULL;
3171 struct tog_tree_view_state *s = &view->state.tree;
3172 struct tog_view *log_view;
3177 s->show_ids = !s->show_ids;
3180 if (!s->selected_entry)
3182 if (view_is_parent_view(view))
3183 begin_x = view_split_begin_x(view->begin_x);
3184 err = log_tree_entry(&log_view, begin_x,
3185 s->selected_entry, &s->parents,
3186 s->commit_id, s->repo);
3187 if (view_is_parent_view(view)) {
3188 err = view_close_child(view);
3191 err = view_set_child(view, log_view);
3193 view_close(log_view);
3196 *focus_view = log_view;
3197 view->child_focussed = 1;
3199 *new_view = log_view;
3203 if (s->selected > 0)
3205 if (s->selected > 0)
3207 tree_scroll_up(&s->first_displayed_entry, 1,
3208 s->entries, s->tree == s->root);
3212 if (SIMPLEQ_FIRST(&s->entries->head) ==
3213 s->first_displayed_entry) {
3214 if (s->tree != s->root)
3215 s->first_displayed_entry = NULL;
3218 tree_scroll_up(&s->first_displayed_entry,
3219 view->nlines, s->entries,
3220 s->tree == s->root);
3224 if (s->selected < s->ndisplayed - 1) {
3228 tree_scroll_down(&s->first_displayed_entry, 1,
3229 s->last_displayed_entry, s->entries);
3232 tree_scroll_down(&s->first_displayed_entry,
3233 view->nlines, s->last_displayed_entry,
3235 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3238 /* can't scroll any further; move cursor down */
3239 if (s->selected < s->ndisplayed - 1)
3240 s->selected = s->ndisplayed - 1;
3244 if (s->selected_entry == NULL) {
3245 struct tog_parent_tree *parent;
3247 /* user selected '..' */
3248 if (s->tree == s->root)
3250 parent = TAILQ_FIRST(&s->parents);
3251 TAILQ_REMOVE(&s->parents, parent,
3253 got_object_tree_close(s->tree);
3254 s->tree = parent->tree;
3256 got_object_tree_get_entries(s->tree);
3257 s->first_displayed_entry =
3258 parent->first_displayed_entry;
3260 parent->selected_entry;
3261 s->selected = parent->selected;
3263 } else if (S_ISDIR(s->selected_entry->mode)) {
3264 struct tog_parent_tree *parent;
3265 struct got_tree_object *child;
3266 err = got_object_open_as_tree(&child,
3267 s->repo, s->selected_entry->id);
3270 parent = calloc(1, sizeof(*parent));
3271 if (parent == NULL) {
3272 err = got_error_from_errno();
3275 parent->tree = s->tree;
3276 parent->first_displayed_entry =
3277 s->first_displayed_entry;
3278 parent->selected_entry = s->selected_entry;
3279 parent->selected = s->selected;
3280 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3283 got_object_tree_get_entries(s->tree);
3285 s->first_displayed_entry = NULL;
3286 } else if (S_ISREG(s->selected_entry->mode)) {
3287 struct tog_view *blame_view;
3288 int begin_x = view_is_parent_view(view) ?
3289 view_split_begin_x(view->begin_x) : 0;
3291 err = blame_tree_entry(&blame_view, begin_x,
3292 s->selected_entry, &s->parents, s->commit_id,
3296 if (view_is_parent_view(view)) {
3297 err = view_close_child(view);
3300 err = view_set_child(view, blame_view);
3302 view_close(blame_view);
3305 *focus_view = blame_view;
3306 view->child_focussed = 1;
3308 *new_view = blame_view;
3312 if (s->selected > view->nlines)
3313 s->selected = s->ndisplayed - 1;
3326 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3331 static const struct got_error *
3332 cmd_tree(int argc, char *argv[])
3334 const struct got_error *error;
3335 struct got_repository *repo = NULL;
3336 char *repo_path = NULL;
3337 struct got_object_id *commit_id = NULL;
3338 char *commit_id_arg = NULL;
3339 struct got_commit_object *commit = NULL;
3340 struct got_tree_object *tree = NULL;
3342 struct tog_view *view;
3345 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3350 while ((ch = getopt(argc, argv, "c:")) != -1) {
3353 commit_id_arg = optarg;
3365 repo_path = getcwd(NULL, 0);
3366 if (repo_path == NULL)
3367 return got_error_from_errno();
3368 } else if (argc == 1) {
3369 repo_path = realpath(argv[0], NULL);
3370 if (repo_path == NULL)
3371 return got_error_from_errno();
3375 error = got_repo_open(&repo, repo_path);
3380 if (commit_id_arg == NULL) {
3381 error = get_head_commit_id(&commit_id, repo);
3385 struct got_object *obj;
3386 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3387 if (error == NULL) {
3388 commit_id = got_object_id_dup(got_object_get_id(obj));
3389 if (commit_id == NULL)
3390 error = got_error_from_errno();
3396 error = got_object_open_as_commit(&commit, repo, commit_id);
3400 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3404 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3406 error = got_error_from_errno();
3409 error = open_tree_view(view, tree, commit_id, repo);
3412 error = view_loop(view);
3416 got_object_commit_close(commit);
3418 got_object_tree_close(tree);
3420 got_repo_close(repo);
3429 halfdelay(1); /* Do fast refresh while initial view is loading. */
3432 intrflush(stdscr, FALSE);
3433 keypad(stdscr, TRUE);
3435 signal(SIGWINCH, tog_sigwinch);
3443 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3444 "Available commands:\n", getprogname());
3445 for (i = 0; i < nitems(tog_commands); i++) {
3446 struct tog_cmd *cmd = &tog_commands[i];
3447 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3453 make_argv(const char *arg0, const char *arg1)
3456 int argc = (arg1 == NULL ? 1 : 2);
3458 argv = calloc(argc, sizeof(char *));
3461 argv[0] = strdup(arg0);
3462 if (argv[0] == NULL)
3465 argv[1] = strdup(arg1);
3466 if (argv[1] == NULL)
3474 main(int argc, char *argv[])
3476 const struct got_error *error = NULL;
3477 struct tog_cmd *cmd = NULL;
3479 char **cmd_argv = NULL;
3481 setlocale(LC_ALL, "");
3483 while ((ch = getopt(argc, argv, "h")) != -1) {
3502 /* Build an argument vector which runs a default command. */
3503 cmd = &tog_commands[0];
3504 cmd_argv = make_argv(cmd->name, NULL);
3509 /* Did the user specific a command? */
3510 for (i = 0; i < nitems(tog_commands); i++) {
3511 if (strncmp(tog_commands[i].name, argv[0],
3512 strlen(argv[0])) == 0) {
3513 cmd = &tog_commands[i];
3515 tog_commands[i].cmd_usage();
3520 /* Did the user specify a repository? */
3521 char *repo_path = realpath(argv[0], NULL);
3523 struct got_repository *repo;
3524 error = got_repo_open(&repo, repo_path);
3526 got_repo_close(repo);
3528 error = got_error_from_errno();
3531 fprintf(stderr, "%s: '%s' is not a "
3532 "known command\n", getprogname(),
3536 fprintf(stderr, "%s: '%s' is neither a known "
3537 "command nor a path to a repository\n",
3538 getprogname(), argv[0]);
3542 cmd = &tog_commands[0];
3543 cmd_argv = make_argv(cmd->name, repo_path);
3551 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3558 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);