2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/queue.h>
21 #define _XOPEN_SOURCE_EXTENDED
23 #undef _XOPEN_SOURCE_EXTENDED
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_opentemp.h"
45 #include "got_commit_graph.h"
47 #include "got_blame.h"
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 const struct got_error *(*cmd_main)(int, char *[]);
65 void (*cmd_usage)(void);
69 __dead static void usage(void);
70 __dead static void usage_log(void);
71 __dead static void usage_diff(void);
72 __dead static void usage_blame(void);
73 __dead static void usage_tree(void);
75 static const struct got_error* cmd_log(int, char *[]);
76 static const struct got_error* cmd_diff(int, char *[]);
77 static const struct got_error* cmd_blame(int, char *[]);
78 static const struct got_error* cmd_tree(int, char *[]);
80 static struct tog_cmd tog_commands[] = {
81 { "log", cmd_log, usage_log,
82 "show repository history" },
83 { "diff", cmd_diff, usage_diff,
84 "compare files and directories" },
85 { "blame", cmd_blame, usage_blame,
86 "show line-by-line file history" },
87 { "tree", cmd_tree, usage_tree,
88 "browse trees in repository" },
98 struct tog_diff_view_state {
99 struct got_object_id *id1, *id2;
101 int first_displayed_line;
102 int last_displayed_line;
105 struct got_repository *repo;
108 struct commit_queue_entry {
109 TAILQ_ENTRY(commit_queue_entry) entry;
110 struct got_object_id *id;
111 struct got_commit_object *commit;
114 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
115 struct commit_queue {
117 struct commit_queue_head head;
120 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
122 struct tog_log_thread_args {
123 pthread_cond_t need_commits;
125 struct got_commit_graph *graph;
126 struct commit_queue *commits;
127 const char *in_repo_path;
128 struct got_object_id *start_id;
129 struct got_repository *repo;
132 struct tog_view *view;
133 struct commit_queue_entry **first_displayed_entry;
134 struct commit_queue_entry **selected_entry;
137 struct tog_log_view_state {
138 struct commit_queue commits;
139 struct commit_queue_entry *first_displayed_entry;
140 struct commit_queue_entry *last_displayed_entry;
141 struct commit_queue_entry *selected_entry;
144 struct got_repository *repo;
145 struct got_object_id *start_id;
148 struct tog_log_thread_args thread_args;
151 struct tog_blame_cb_args {
152 struct tog_blame_line *lines; /* one per line */
155 struct tog_view *view;
156 struct got_object_id *commit_id;
160 struct tog_blame_thread_args {
162 struct got_repository *repo;
163 struct tog_blame_cb_args *cb_args;
170 struct tog_blame_line *lines;
173 struct tog_blame_thread_args thread_args;
174 struct tog_blame_cb_args cb_args;
178 struct tog_blame_view_state {
179 int first_displayed_line;
180 int last_displayed_line;
185 struct got_object_id_queue blamed_commits;
186 struct got_object_qid *blamed_commit;
188 struct got_repository *repo;
189 struct got_object_id *commit_id;
190 struct tog_blame blame;
193 struct tog_parent_tree {
194 TAILQ_ENTRY(tog_parent_tree) entry;
195 struct got_tree_object *tree;
196 struct got_tree_entry *first_displayed_entry;
197 struct got_tree_entry *selected_entry;
201 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
203 struct tog_tree_view_state {
205 struct got_tree_object *root;
206 struct got_tree_object *tree;
207 const struct got_tree_entries *entries;
208 struct got_tree_entry *first_displayed_entry;
209 struct got_tree_entry *last_displayed_entry;
210 struct got_tree_entry *selected_entry;
211 int nentries, ndisplayed, selected, show_ids;
212 struct tog_parent_trees parents;
213 struct got_object_id *commit_id;
214 struct got_repository *repo;
218 * We implement two types of views: parent views and child views.
220 * The 'Tab' key switches between a parent view and its child view.
221 * Child views are shown side-by-side to their parent view, provided
222 * there is enough screen estate.
224 * When a new view is opened from within a parent view, this new view
225 * becomes a child view of the parent view, replacing any existing child.
227 * When a new view is opened from within a child view, this new view
228 * becomes a parent view which will obscure the views below until the
229 * user quits the new parent view by typing 'q'.
231 * This list of views contains parent views only.
232 * Child views are only pointed to by their parent view.
234 TAILQ_HEAD(tog_view_list_head, tog_view);
237 TAILQ_ENTRY(tog_view) entry;
240 int nlines, ncols, begin_y, begin_x;
241 int lines, cols; /* copies of LINES and COLS */
243 struct tog_view *parent;
244 struct tog_view *child;
247 /* type-specific state */
248 enum tog_view_type type;
250 struct tog_diff_view_state diff;
251 struct tog_log_view_state log;
252 struct tog_blame_view_state blame;
253 struct tog_tree_view_state tree;
256 const struct got_error *(*show)(struct tog_view *);
257 const struct got_error *(*input)(struct tog_view **,
258 struct tog_view **, struct tog_view**, struct tog_view *, int);
259 const struct got_error *(*close)(struct tog_view *);
262 static const struct got_error *open_diff_view(struct tog_view *,
263 struct got_object *, struct got_object *, struct got_repository *);
264 static const struct got_error *show_diff_view(struct tog_view *);
265 static const struct got_error *input_diff_view(struct tog_view **,
266 struct tog_view **, struct tog_view **, struct tog_view *, int);
267 static const struct got_error* close_diff_view(struct tog_view *);
269 static const struct got_error *open_log_view(struct tog_view *,
270 struct got_object_id *, struct got_repository *, const char *, int);
271 static const struct got_error * show_log_view(struct tog_view *);
272 static const struct got_error *input_log_view(struct tog_view **,
273 struct tog_view **, struct tog_view **, struct tog_view *, int);
274 static const struct got_error *close_log_view(struct tog_view *);
276 static const struct got_error *open_blame_view(struct tog_view *, char *,
277 struct got_object_id *, struct got_repository *);
278 static const struct got_error *show_blame_view(struct tog_view *);
279 static const struct got_error *input_blame_view(struct tog_view **,
280 struct tog_view **, struct tog_view **, struct tog_view *, int);
281 static const struct got_error *close_blame_view(struct tog_view *);
283 static const struct got_error *open_tree_view(struct tog_view *,
284 struct got_tree_object *, struct got_object_id *, struct got_repository *);
285 static const struct got_error *show_tree_view(struct tog_view *);
286 static const struct got_error *input_tree_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_tree_view(struct tog_view *);
290 static const struct got_error *
291 view_close(struct tog_view *view)
293 const struct got_error *err = NULL;
296 view_close(view->child);
300 err = view->close(view);
302 del_panel(view->panel);
304 delwin(view->window);
309 static struct tog_view *
310 view_open(int nlines, int ncols, int begin_y, int begin_x,
311 enum tog_view_type type)
313 struct tog_view *view = calloc(1, sizeof(*view));
321 view->nlines = nlines ? nlines : LINES - begin_y;
322 view->ncols = ncols ? ncols : COLS - begin_x;
323 view->begin_y = begin_y;
324 view->begin_x = begin_x;
325 view->window = newwin(nlines, ncols, begin_y, begin_x);
326 if (view->window == NULL) {
330 view->panel = new_panel(view->window);
331 if (view->panel == NULL ||
332 set_panel_userptr(view->panel, view) != OK) {
337 keypad(view->window, TRUE);
342 view_split_begin_x(int begin_x)
344 if (begin_x > 0 || COLS < 120)
346 return (COLS - MAX(COLS / 2, 80));
349 static const struct got_error *view_resize(struct tog_view *);
351 static const struct got_error *
352 view_splitscreen(struct tog_view *view)
354 const struct got_error *err = NULL;
357 view->begin_x = view_split_begin_x(0);
358 view->nlines = LINES;
359 view->ncols = COLS - view->begin_x;
362 err = view_resize(view);
366 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
367 return got_error_from_errno();
372 static const struct got_error *
373 view_fullscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
379 view->nlines = LINES;
383 err = view_resize(view);
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_from_errno();
394 view_is_parent_view(struct tog_view *view)
396 return view->parent == NULL;
399 static const struct got_error *
400 view_resize(struct tog_view *view)
404 if (view->lines > LINES)
405 nlines = view->nlines - (view->lines - LINES);
407 nlines = view->nlines + (LINES - view->lines);
409 if (view->cols > COLS)
410 ncols = view->ncols - (view->cols - COLS);
412 ncols = view->ncols + (COLS - view->cols);
414 if (wresize(view->window, nlines, ncols) == ERR)
415 return got_error_from_errno();
416 if (replace_panel(view->panel, view->window) == ERR)
417 return got_error_from_errno();
419 view->nlines = nlines;
425 view->child->begin_x = view_split_begin_x(view->begin_x);
426 if (view->child->begin_x == 0) {
427 view_fullscreen(view->child);
428 if (view->child->focussed)
429 show_panel(view->child->panel);
431 show_panel(view->panel);
433 view_splitscreen(view->child);
434 show_panel(view->child->panel);
441 static const struct got_error *
442 view_close_child(struct tog_view *view)
444 const struct got_error *err = NULL;
446 if (view->child == NULL)
449 err = view_close(view->child);
454 static const struct got_error *
455 view_set_child(struct tog_view *view, struct tog_view *child)
457 const struct got_error *err = NULL;
460 child->parent = view;
465 view_is_splitscreen(struct tog_view *view)
467 return !view_is_parent_view(view) && view->begin_x > 0;
470 static const struct got_error *
471 view_input(struct tog_view **new, struct tog_view **dead,
472 struct tog_view **focus, int *done, struct tog_view *view,
473 struct tog_view_list_head *views)
475 const struct got_error *err = NULL;
483 nodelay(stdscr, FALSE);
484 /* Allow threads to make progress while we are waiting for input. */
485 errcode = pthread_mutex_unlock(&tog_mutex);
487 return got_error_set_errno(errcode);
488 ch = wgetch(view->window);
489 errcode = pthread_mutex_lock(&tog_mutex);
491 return got_error_set_errno(errcode);
492 nodelay(stdscr, TRUE);
498 *focus = view->child;
499 view->child_focussed = 1;
500 } else if (view->parent) {
501 *focus = view->parent;
502 view->parent->child_focussed = 0;
506 err = view->input(new, dead, focus, view, ch);
513 if (view_is_parent_view(view)) {
514 if (view->child == NULL)
516 if (view_is_splitscreen(view->child)) {
517 *focus = view->child;
518 view->child_focussed = 1;
519 err = view_fullscreen(view->child);
521 err = view_splitscreen(view->child);
524 err = view->child->input(new, dead, focus,
525 view->child, KEY_RESIZE);
527 if (view_is_splitscreen(view)) {
529 view->parent->child_focussed = 1;
530 err = view_fullscreen(view);
532 err = view_splitscreen(view);
536 err = view->input(new, dead, focus, view,
541 TAILQ_FOREACH(v, views, entry) {
542 err = view_resize(v);
545 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 fast_refresh = 10;
597 int done = 0, errcode;
599 errcode = pthread_mutex_lock(&tog_mutex);
601 return got_error_set_errno(errcode);
604 TAILQ_INSERT_HEAD(&views, view, entry);
608 err = view->show(view);
613 while (!TAILQ_EMPTY(&views) && !done) {
614 /* Refresh fast during initialization, then become slower. */
615 if (fast_refresh && fast_refresh-- == 0)
616 halfdelay(10); /* switch to once per second */
618 err = view_input(&new_view, &dead_view, &focus_view, &done,
623 struct tog_view *prev = NULL;
625 if (view_is_parent_view(dead_view))
626 prev = TAILQ_PREV(dead_view,
627 tog_view_list_head, entry);
631 if (dead_view->parent)
632 dead_view->parent->child = NULL;
634 TAILQ_REMOVE(&views, dead_view, entry);
636 err = view_close(dead_view);
637 if (err || dead_view == main_view)
640 if (view == dead_view) {
645 else if (!TAILQ_EMPTY(&views))
646 view = TAILQ_LAST(&views,
651 if (view->child && view->child_focussed)
652 focus_view = view->child;
659 struct tog_view *v, *t;
660 /* Only allow one parent view per type. */
661 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
662 if (v->type != new_view->type)
664 TAILQ_REMOVE(&views, v, entry);
672 TAILQ_INSERT_TAIL(&views, new_view, entry);
673 if (focus_view == NULL)
674 focus_view = new_view;
677 show_panel(focus_view->panel);
680 focus_view->focussed = 1;
683 show_panel(new_view->panel);
684 if (view->child && view_is_splitscreen(view->child))
685 show_panel(view->child->panel);
688 if (focus_view == NULL) {
690 show_panel(view->panel);
691 if (view->child && view_is_splitscreen(view->child))
692 show_panel(view->child->panel);
696 err = view->parent->show(view->parent);
700 err = view->show(view);
704 err = view->child->show(view->child);
713 while (!TAILQ_EMPTY(&views)) {
714 view = TAILQ_FIRST(&views);
715 TAILQ_REMOVE(&views, view, entry);
719 errcode = pthread_mutex_unlock(&tog_mutex);
721 return got_error_set_errno(errcode);
731 "usage: %s log [-c commit] [-r repository-path] [path]\n",
736 /* Create newly allocated wide-character string equivalent to a byte string. */
737 static const struct got_error *
738 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
741 const struct got_error *err = NULL;
744 *wlen = mbstowcs(NULL, s, 0);
745 if (*wlen == (size_t)-1) {
748 return got_error_from_errno();
750 /* byte string invalid in current encoding; try to "fix" it */
751 err = got_mbsavis(&vis, &vislen, s);
754 *wlen = mbstowcs(NULL, vis, 0);
755 if (*wlen == (size_t)-1) {
756 err = got_error_from_errno(); /* give up */
761 *ws = calloc(*wlen + 1, sizeof(*ws));
763 err = got_error_from_errno();
767 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
768 err = got_error_from_errno();
779 /* Format a line for display, ensuring that it won't overflow a width limit. */
780 static const struct got_error *
781 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
783 const struct got_error *err = NULL;
785 wchar_t *wline = NULL;
792 err = mbs2ws(&wline, &wlen, line);
797 while (i < wlen && cols < wlimit) {
798 int width = wcwidth(wline[i]);
805 if (cols + width <= wlimit)
810 if (wline[i] == L'\t')
811 cols += TABSIZE - ((cols + 1) % TABSIZE);
815 err = got_error_from_errno();
830 static const struct got_error *
831 draw_commit(struct tog_view *view, struct got_commit_object *commit,
832 struct got_object_id *id)
834 const struct got_error *err = NULL;
835 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
836 char *logmsg0 = NULL, *logmsg = NULL;
837 char *author0 = NULL, *author = NULL;
838 wchar_t *wlogmsg = NULL, *wauthor = NULL;
839 int author_width, logmsg_width;
840 char *newline, *smallerthan;
843 static const size_t date_display_cols = 9;
844 static const size_t author_display_cols = 16;
845 const int avail = view->ncols;
847 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ",
848 &commit->tm_committer) >= sizeof(datebuf))
849 return got_error(GOT_ERR_NO_SPACE);
851 if (avail < date_display_cols)
852 limit = MIN(sizeof(datebuf) - 1, avail);
854 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
855 waddnstr(view->window, datebuf, limit);
860 author0 = strdup(commit->author);
861 if (author0 == NULL) {
862 err = got_error_from_errno();
866 smallerthan = strchr(author, '<');
870 char *at = strchr(author, '@');
875 err = format_line(&wauthor, &author_width, author, limit);
878 waddwstr(view->window, wauthor);
880 while (col <= avail && author_width < author_display_cols + 1) {
881 waddch(view->window, ' ');
888 logmsg0 = strdup(commit->logmsg);
889 if (logmsg0 == NULL) {
890 err = got_error_from_errno();
894 while (*logmsg == '\n')
896 newline = strchr(logmsg, '\n');
900 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
903 waddwstr(view->window, wlogmsg);
905 while (col <= avail) {
906 waddch(view->window, ' ');
918 static struct commit_queue_entry *
919 alloc_commit_queue_entry(struct got_commit_object *commit,
920 struct got_object_id *id)
922 struct commit_queue_entry *entry;
924 entry = calloc(1, sizeof(*entry));
929 entry->commit = commit;
934 pop_commit(struct commit_queue *commits)
936 struct commit_queue_entry *entry;
938 entry = TAILQ_FIRST(&commits->head);
939 TAILQ_REMOVE(&commits->head, entry, entry);
940 got_object_commit_close(entry->commit);
942 /* Don't free entry->id! It is owned by the commit graph. */
947 free_commits(struct commit_queue *commits)
949 while (!TAILQ_EMPTY(&commits->head))
953 static const struct got_error *
954 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
955 int minqueue, struct got_repository *repo, const char *path)
957 const struct got_error *err = NULL;
961 * We keep all commits open throughout the lifetime of the log
962 * view in order to avoid having to re-fetch commits from disk
963 * while updating the display.
965 while (nqueued < minqueue) {
966 struct got_object_id *id;
967 struct got_commit_object *commit;
968 struct commit_queue_entry *entry;
971 err = got_commit_graph_iter_next(&id, graph);
973 if (err->code != GOT_ERR_ITER_NEED_MORE)
975 err = got_commit_graph_fetch_commits(graph,
985 err = got_object_open_as_commit(&commit, repo, id);
988 entry = alloc_commit_queue_entry(commit, id);
990 err = got_error_from_errno();
994 errcode = pthread_mutex_lock(&tog_mutex);
996 err = got_error_set_errno(errcode);
1000 entry->idx = commits->ncommits;
1001 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1003 commits->ncommits++;
1005 errcode = pthread_mutex_unlock(&tog_mutex);
1006 if (errcode && err == NULL)
1007 err = got_error_set_errno(errcode);
1013 static const struct got_error *
1014 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1016 const struct got_error *err = NULL;
1017 struct got_reference *head_ref;
1021 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1025 err = got_ref_resolve(head_id, repo, head_ref);
1026 got_ref_close(head_ref);
1035 static const struct got_error *
1036 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1037 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1038 struct commit_queue *commits, int selected_idx, int limit,
1039 const char *path, int commits_needed)
1041 const struct got_error *err = NULL;
1042 struct commit_queue_entry *entry;
1043 int ncommits, width;
1044 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1050 if (ncommits == selected_idx) {
1054 entry = TAILQ_NEXT(entry, entry);
1059 err = got_object_id_str(&id_str, (*selected)->id);
1064 if (asprintf(&ncommits_str, " [%d/%d]%s ",
1065 entry ? entry->idx + 1 : 0, commits->ncommits,
1066 commits_needed == 0 ? "" : " loading...") == -1)
1067 return got_error_from_errno();
1069 if (path && strcmp(path, "/") != 0) {
1070 if (asprintf(&header, "commit: %s %s%s",
1071 id_str ? id_str : "........................................",
1072 path, ncommits_str) == -1) {
1073 err = got_error_from_errno();
1077 } else if (asprintf(&header, "commit: %s%s",
1078 id_str ? id_str : "........................................",
1079 ncommits_str) == -1) {
1080 err = got_error_from_errno();
1084 err = format_line(&wline, &width, header, view->ncols);
1088 werase(view->window);
1090 if (view_needs_focus_indication(view))
1091 wstandout(view->window);
1092 waddwstr(view->window, wline);
1093 while (width < view->ncols) {
1094 waddch(view->window, ' ');
1097 if (view_needs_focus_indication(view))
1098 wstandend(view->window);
1107 if (ncommits >= limit - 1)
1109 if (view->focussed && ncommits == selected_idx)
1110 wstandout(view->window);
1111 err = draw_commit(view, entry->commit, entry->id);
1112 if (view->focussed && ncommits == selected_idx)
1113 wstandend(view->window);
1118 entry = TAILQ_NEXT(entry, entry);
1130 scroll_up(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1131 struct commit_queue *commits)
1133 struct commit_queue_entry *entry;
1136 entry = TAILQ_FIRST(&commits->head);
1137 if (*first_displayed_entry == entry)
1140 entry = *first_displayed_entry;
1141 while (entry && nscrolled < maxscroll) {
1142 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1144 *first_displayed_entry = entry;
1150 static const struct got_error *
1151 scroll_down(struct commit_queue_entry **first_displayed_entry, int maxscroll,
1152 struct commit_queue_entry **last_displayed_entry,
1153 struct commit_queue *commits, int *log_complete, int *commits_needed,
1154 pthread_cond_t *need_commits)
1156 const struct got_error *err = NULL;
1157 struct commit_queue_entry *pentry;
1160 if (*last_displayed_entry == NULL)
1164 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1165 if (pentry == NULL) {
1169 *commits_needed = maxscroll + 20;
1170 errcode = pthread_cond_signal(need_commits);
1172 return got_error_set_errno(errcode);
1175 *last_displayed_entry = pentry;
1177 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1180 *first_displayed_entry = pentry;
1181 } while (++nscrolled < maxscroll);
1186 static const struct got_error *
1187 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1188 struct got_object_id *commit_id, struct got_commit_object *commit,
1189 struct got_repository *repo)
1191 const struct got_error *err;
1192 struct got_object *obj1 = NULL, *obj2 = NULL;
1193 struct got_object_qid *parent_id;
1194 struct tog_view *diff_view;
1196 err = got_object_open(&obj2, repo, commit_id);
1200 parent_id = SIMPLEQ_FIRST(&commit->parent_ids);
1202 err = got_object_open(&obj1, repo, parent_id->id);
1207 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1208 if (diff_view == NULL) {
1209 err = got_error_from_errno();
1213 err = open_diff_view(diff_view, obj1, obj2, repo);
1215 *new_view = diff_view;
1218 got_object_close(obj1);
1220 got_object_close(obj2);
1224 static const struct got_error *
1225 browse_commit(struct tog_view **new_view, int begin_x,
1226 struct commit_queue_entry *entry, struct got_repository *repo)
1228 const struct got_error *err = NULL;
1229 struct got_tree_object *tree;
1230 struct tog_view *tree_view;
1232 err = got_object_open_as_tree(&tree, repo, entry->commit->tree_id);
1236 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1237 if (tree_view == NULL)
1238 return got_error_from_errno();
1240 err = open_tree_view(tree_view, tree, entry->id, repo);
1242 got_object_tree_close(tree);
1244 *new_view = tree_view;
1249 log_thread(void *arg)
1251 const struct got_error *err = NULL;
1253 struct tog_log_thread_args *a = arg;
1256 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1260 while (!done && !err) {
1261 err = queue_commits(a->graph, a->commits, 1, a->repo,
1264 if (err->code != GOT_ERR_ITER_COMPLETED)
1268 } else if (a->commits_needed > 0)
1269 a->commits_needed--;
1271 errcode = pthread_mutex_lock(&tog_mutex);
1273 return (void *)got_error_set_errno(errcode);
1276 a->log_complete = 1;
1277 else if (*a->quit) {
1279 a->log_complete = 1;
1280 } else if (*a->first_displayed_entry == NULL) {
1281 *a->first_displayed_entry =
1282 TAILQ_FIRST(&a->commits->head);
1283 *a->selected_entry = *a->first_displayed_entry;
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);
1351 s->in_repo_path = NULL;
1357 static const struct got_error *
1358 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1359 struct got_repository *repo, const char *path, int check_disk)
1361 const struct got_error *err = NULL;
1362 struct tog_log_view_state *s = &view->state.log;
1363 struct got_repository *thread_repo = NULL;
1364 struct got_commit_graph *thread_graph = NULL;
1367 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1371 /* The commit queue only contains commits being displayed. */
1372 TAILQ_INIT(&s->commits.head);
1373 s->commits.ncommits = 0;
1376 s->start_id = got_object_id_dup(start_id);
1377 if (s->start_id == NULL) {
1378 err = got_error_from_errno();
1382 view->show = show_log_view;
1383 view->input = input_log_view;
1384 view->close = close_log_view;
1386 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1389 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1394 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1396 err = got_error_set_errno(errcode);
1400 s->thread_args.commits_needed = view->nlines;
1401 s->thread_args.graph = thread_graph;
1402 s->thread_args.commits = &s->commits;
1403 s->thread_args.in_repo_path = s->in_repo_path;
1404 s->thread_args.start_id = s->start_id;
1405 s->thread_args.repo = thread_repo;
1406 s->thread_args.log_complete = 0;
1407 s->thread_args.quit = &s->quit;
1408 s->thread_args.view = view;
1409 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1410 s->thread_args.selected_entry = &s->selected_entry;
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, 1);
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 errcode = pthread_mutex_unlock(&tog_mutex);
2218 err = got_error_set_errno(errcode);
2223 blame_thread(void *arg)
2225 const struct got_error *err;
2226 struct tog_blame_thread_args *ta = arg;
2227 struct tog_blame_cb_args *a = ta->cb_args;
2230 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2231 blame_cb, ta->cb_args);
2233 errcode = pthread_mutex_lock(&tog_mutex);
2235 return (void *)got_error_set_errno(errcode);
2237 got_repo_close(ta->repo);
2241 errcode = pthread_mutex_unlock(&tog_mutex);
2242 if (errcode && err == NULL)
2243 err = got_error_set_errno(errcode);
2248 static struct got_object_id *
2249 get_selected_commit_id(struct tog_blame_line *lines,
2250 int first_displayed_line, int selected_line)
2252 struct tog_blame_line *line;
2254 line = &lines[first_displayed_line - 1 + selected_line - 1];
2255 if (!line->annotated)
2261 static const struct got_error *
2262 open_selected_commit(struct got_object **pobj, struct got_object **obj,
2263 struct tog_blame_line *lines, int first_displayed_line,
2264 int selected_line, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_commit_object *commit = NULL;
2268 struct got_object_id *selected_id;
2269 struct got_object_qid *pid;
2274 selected_id = get_selected_commit_id(lines,
2275 first_displayed_line, selected_line);
2276 if (selected_id == NULL)
2279 err = got_object_open(obj, repo, selected_id);
2283 err = got_object_commit_open(&commit, repo, *obj);
2287 pid = SIMPLEQ_FIRST(&commit->parent_ids);
2289 err = got_object_open(pobj, repo, pid->id);
2295 got_object_commit_close(commit);
2299 static const struct got_error *
2300 stop_blame(struct tog_blame *blame)
2302 const struct got_error *err = NULL;
2305 if (blame->thread) {
2307 errcode = pthread_mutex_unlock(&tog_mutex);
2309 return got_error_set_errno(errcode);
2310 errcode = pthread_join(blame->thread, (void **)&err);
2312 return got_error_set_errno(errcode);
2313 errcode = pthread_mutex_lock(&tog_mutex);
2315 return got_error_set_errno(errcode);
2316 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2318 blame->thread = NULL;
2320 if (blame->thread_args.repo) {
2321 got_repo_close(blame->thread_args.repo);
2322 blame->thread_args.repo = NULL;
2328 for (i = 0; i < blame->nlines; i++)
2329 free(blame->lines[i].id);
2331 blame->lines = NULL;
2332 free(blame->cb_args.commit_id);
2333 blame->cb_args.commit_id = NULL;
2338 static const struct got_error *
2339 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2340 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2341 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2342 struct got_repository *repo)
2344 const struct got_error *err = NULL;
2345 struct got_blob_object *blob = NULL;
2346 struct got_repository *thread_repo = NULL;
2347 struct got_object_id *obj_id = NULL;
2348 struct got_object *obj = NULL;
2351 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2355 err = got_object_open(&obj, repo, obj_id);
2359 if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
2360 err = got_error(GOT_ERR_OBJ_TYPE);
2364 err = got_object_blob_open(&blob, repo, obj, 8192);
2367 blame->f = got_opentemp();
2368 if (blame->f == NULL) {
2369 err = got_error_from_errno();
2372 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2377 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2378 if (blame->lines == NULL) {
2379 err = got_error_from_errno();
2383 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2387 blame->cb_args.view = view;
2388 blame->cb_args.lines = blame->lines;
2389 blame->cb_args.nlines = blame->nlines;
2390 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2391 if (blame->cb_args.commit_id == NULL) {
2392 err = got_error_from_errno();
2395 blame->cb_args.quit = done;
2397 blame->thread_args.path = path;
2398 blame->thread_args.repo = thread_repo;
2399 blame->thread_args.cb_args = &blame->cb_args;
2400 blame->thread_args.complete = blame_complete;
2401 *blame_complete = 0;
2403 errcode = pthread_create(&blame->thread, NULL, blame_thread,
2404 &blame->thread_args);
2406 err = got_error_set_errno(errcode);
2412 got_object_blob_close(blob);
2415 got_object_close(obj);
2421 static const struct got_error *
2422 open_blame_view(struct tog_view *view, char *path,
2423 struct got_object_id *commit_id, struct got_repository *repo)
2425 const struct got_error *err = NULL;
2426 struct tog_blame_view_state *s = &view->state.blame;
2428 SIMPLEQ_INIT(&s->blamed_commits);
2430 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2434 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2435 s->first_displayed_line = 1;
2436 s->last_displayed_line = view->nlines;
2437 s->selected_line = 1;
2438 s->blame_complete = 0;
2440 if (s->path == NULL)
2441 return got_error_from_errno();
2443 s->commit_id = commit_id;
2444 memset(&s->blame, 0, sizeof(s->blame));
2446 view->show = show_blame_view;
2447 view->input = input_blame_view;
2448 view->close = close_blame_view;
2450 return run_blame(&s->blame, view, &s->blame_complete,
2451 &s->first_displayed_line, &s->last_displayed_line,
2452 &s->selected_line, &s->done, &s->eof, s->path,
2453 s->blamed_commit->id, s->repo);
2456 static const struct got_error *
2457 close_blame_view(struct tog_view *view)
2459 const struct got_error *err = NULL;
2460 struct tog_blame_view_state *s = &view->state.blame;
2462 if (s->blame.thread)
2463 err = stop_blame(&s->blame);
2465 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2466 struct got_object_qid *blamed_commit;
2467 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2468 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2469 got_object_qid_free(blamed_commit);
2477 static const struct got_error *
2478 show_blame_view(struct tog_view *view)
2480 const struct got_error *err = NULL;
2481 struct tog_blame_view_state *s = &view->state.blame;
2483 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2484 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2485 s->selected_line, &s->first_displayed_line,
2486 &s->last_displayed_line, &s->eof, view->nlines);
2492 static const struct got_error *
2493 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2494 struct tog_view **focus_view, struct tog_view *view, int ch)
2496 const struct got_error *err = NULL, *thread_err = NULL;
2497 struct got_object *obj = NULL, *pobj = NULL;
2498 struct tog_view *diff_view;
2499 struct tog_blame_view_state *s = &view->state.blame;
2508 if (s->selected_line > 1)
2510 else if (s->selected_line == 1 &&
2511 s->first_displayed_line > 1)
2512 s->first_displayed_line--;
2515 if (s->first_displayed_line == 1) {
2516 s->selected_line = 1;
2519 if (s->first_displayed_line > view->nlines - 2)
2520 s->first_displayed_line -=
2523 s->first_displayed_line = 1;
2527 if (s->selected_line < view->nlines - 2 &&
2528 s->first_displayed_line +
2529 s->selected_line <= s->blame.nlines)
2531 else if (s->last_displayed_line <
2533 s->first_displayed_line++;
2537 struct got_object_id *id;
2538 id = get_selected_commit_id(s->blame.lines,
2539 s->first_displayed_line, s->selected_line);
2540 if (id == NULL || got_object_id_cmp(id,
2541 s->blamed_commit->id) == 0)
2543 err = open_selected_commit(&pobj, &obj,
2544 s->blame.lines, s->first_displayed_line,
2545 s->selected_line, s->repo);
2548 if (pobj == NULL && obj == NULL)
2550 if (ch == 'p' && pobj == NULL)
2553 thread_err = stop_blame(&s->blame);
2557 id = got_object_get_id(ch == 'b' ? obj : pobj);
2558 got_object_close(obj);
2561 got_object_close(pobj);
2564 err = got_object_qid_alloc(&s->blamed_commit, id);
2567 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
2568 s->blamed_commit, entry);
2569 err = run_blame(&s->blame, view, &s->blame_complete,
2570 &s->first_displayed_line, &s->last_displayed_line,
2571 &s->selected_line, &s->done, &s->eof,
2572 s->path, s->blamed_commit->id, s->repo);
2578 struct got_object_qid *first;
2579 first = SIMPLEQ_FIRST(&s->blamed_commits);
2580 if (!got_object_id_cmp(first->id, s->commit_id))
2583 thread_err = stop_blame(&s->blame);
2587 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2588 got_object_qid_free(s->blamed_commit);
2590 SIMPLEQ_FIRST(&s->blamed_commits);
2591 err = run_blame(&s->blame, view, &s->blame_complete,
2592 &s->first_displayed_line, &s->last_displayed_line,
2593 &s->selected_line, &s->done, &s->eof, s->path,
2594 s->blamed_commit->id, s->repo);
2601 err = open_selected_commit(&pobj, &obj,
2602 s->blame.lines, s->first_displayed_line,
2603 s->selected_line, s->repo);
2606 if (pobj == NULL && obj == NULL)
2609 if (view_is_parent_view(view))
2610 begin_x = view_split_begin_x(view->begin_x);
2611 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2612 if (diff_view == NULL) {
2613 err = got_error_from_errno();
2616 err = open_diff_view(diff_view, pobj, obj, s->repo);
2618 view_close(diff_view);
2621 if (view_is_parent_view(view)) {
2622 err = view_close_child(view);
2625 err = view_set_child(view, diff_view);
2627 view_close(diff_view);
2630 if (!view_is_splitscreen(diff_view)) {
2631 *focus_view = diff_view;
2632 view->child_focussed = 1;
2635 *new_view = diff_view;
2637 got_object_close(pobj);
2640 got_object_close(obj);
2647 if (s->last_displayed_line >= s->blame.nlines &&
2648 s->selected_line < view->nlines - 2) {
2649 s->selected_line = MIN(s->blame.nlines,
2653 if (s->last_displayed_line + view->nlines - 2
2655 s->first_displayed_line +=
2658 s->first_displayed_line =
2663 if (s->selected_line > view->nlines - 2) {
2664 s->selected_line = MIN(s->blame.nlines,
2673 got_object_close(pobj);
2674 return thread_err ? thread_err : err;
2677 static const struct got_error *
2678 cmd_blame(int argc, char *argv[])
2680 const struct got_error *error;
2681 struct got_repository *repo = NULL;
2682 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2683 struct got_object_id *commit_id = NULL;
2684 char *commit_id_str = NULL;
2686 struct tog_view *view;
2689 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
2694 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2697 commit_id_str = optarg;
2700 repo_path = realpath(optarg, NULL);
2701 if (repo_path == NULL)
2702 err(1, "-r option");
2718 cwd = getcwd(NULL, 0);
2720 error = got_error_from_errno();
2723 if (repo_path == NULL) {
2724 repo_path = strdup(cwd);
2725 if (repo_path == NULL) {
2726 error = got_error_from_errno();
2732 error = got_repo_open(&repo, repo_path);
2736 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2740 if (commit_id_str == NULL) {
2741 struct got_reference *head_ref;
2742 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
2745 error = got_ref_resolve(&commit_id, repo, head_ref);
2746 got_ref_close(head_ref);
2748 struct got_object *obj;
2749 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
2752 commit_id = got_object_id_dup(got_object_get_id(obj));
2753 if (commit_id == NULL)
2754 error = got_error_from_errno();
2755 got_object_close(obj);
2760 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
2762 error = got_error_from_errno();
2765 error = open_blame_view(view, in_repo_path, commit_id, repo);
2768 error = view_loop(view);
2774 got_repo_close(repo);
2778 static const struct got_error *
2779 draw_tree_entries(struct tog_view *view,
2780 struct got_tree_entry **first_displayed_entry,
2781 struct got_tree_entry **last_displayed_entry,
2782 struct got_tree_entry **selected_entry, int *ndisplayed,
2783 const char *label, int show_ids, const char *parent_path,
2784 const struct got_tree_entries *entries, int selected, int limit, int isroot)
2786 const struct got_error *err = NULL;
2787 struct got_tree_entry *te;
2793 werase(view->window);
2798 err = format_line(&wline, &width, label, view->ncols);
2801 if (view_needs_focus_indication(view))
2802 wstandout(view->window);
2803 waddwstr(view->window, wline);
2804 if (view_needs_focus_indication(view))
2805 wstandend(view->window);
2808 if (width < view->ncols)
2809 waddch(view->window, '\n');
2812 err = format_line(&wline, &width, parent_path, view->ncols);
2815 waddwstr(view->window, wline);
2818 if (width < view->ncols)
2819 waddch(view->window, '\n');
2822 waddch(view->window, '\n');
2826 te = SIMPLEQ_FIRST(&entries->head);
2827 if (*first_displayed_entry == NULL) {
2828 if (selected == 0) {
2830 wstandout(view->window);
2831 *selected_entry = NULL;
2833 waddstr(view->window, " ..\n"); /* parent directory */
2834 if (selected == 0 && view->focussed)
2835 wstandend(view->window);
2842 while (te != *first_displayed_entry)
2843 te = SIMPLEQ_NEXT(te, entry);
2847 char *line = NULL, *id_str = NULL;
2850 err = got_object_id_str(&id_str, te->id);
2852 return got_error_from_errno();
2854 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
2855 te->name, S_ISDIR(te->mode) ? "/" : "") == -1) {
2857 return got_error_from_errno();
2860 err = format_line(&wline, &width, line, view->ncols);
2865 if (n == selected) {
2867 wstandout(view->window);
2868 *selected_entry = te;
2870 waddwstr(view->window, wline);
2871 if (width < view->ncols)
2872 waddch(view->window, '\n');
2873 if (n == selected && view->focussed)
2874 wstandend(view->window);
2880 *last_displayed_entry = te;
2883 te = SIMPLEQ_NEXT(te, entry);
2890 tree_scroll_up(struct got_tree_entry **first_displayed_entry, int maxscroll,
2891 const struct got_tree_entries *entries, int isroot)
2893 struct got_tree_entry *te, *prev;
2896 if (*first_displayed_entry == NULL)
2899 te = SIMPLEQ_FIRST(&entries->head);
2900 if (*first_displayed_entry == te) {
2902 *first_displayed_entry = NULL;
2906 /* XXX this is stupid... switch to TAILQ? */
2907 for (i = 0; i < maxscroll; i++) {
2908 while (te != *first_displayed_entry) {
2910 te = SIMPLEQ_NEXT(te, entry);
2912 *first_displayed_entry = prev;
2913 te = SIMPLEQ_FIRST(&entries->head);
2915 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
2916 *first_displayed_entry = NULL;
2920 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
2921 struct got_tree_entry *last_displayed_entry,
2922 const struct got_tree_entries *entries)
2924 struct got_tree_entry *next;
2927 if (SIMPLEQ_NEXT(last_displayed_entry, entry) == NULL)
2930 if (*first_displayed_entry)
2931 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
2933 next = SIMPLEQ_FIRST(&entries->head);
2935 *first_displayed_entry = next;
2936 if (++n >= maxscroll)
2938 next = SIMPLEQ_NEXT(next, entry);
2942 static const struct got_error *
2943 tree_entry_path(char **path, struct tog_parent_trees *parents,
2944 struct got_tree_entry *te)
2946 const struct got_error *err = NULL;
2947 struct tog_parent_tree *pt;
2948 size_t len = 2; /* for leading slash and NUL */
2950 TAILQ_FOREACH(pt, parents, entry)
2951 len += strlen(pt->selected_entry->name) + 1 /* slash */;
2953 len += strlen(te->name);
2955 *path = calloc(1, len);
2957 return got_error_from_errno();
2960 pt = TAILQ_LAST(parents, tog_parent_trees);
2962 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
2963 err = got_error(GOT_ERR_NO_SPACE);
2966 if (strlcat(*path, "/", len) >= len) {
2967 err = got_error(GOT_ERR_NO_SPACE);
2970 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
2973 if (strlcat(*path, te->name, len) >= len) {
2974 err = got_error(GOT_ERR_NO_SPACE);
2986 static const struct got_error *
2987 blame_tree_entry(struct tog_view **new_view, int begin_x,
2988 struct got_tree_entry *te, struct tog_parent_trees *parents,
2989 struct got_object_id *commit_id, struct got_repository *repo)
2991 const struct got_error *err = NULL;
2993 struct tog_view *blame_view;
2995 err = tree_entry_path(&path, parents, te);
2999 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3000 if (blame_view == NULL)
3001 return got_error_from_errno();
3003 err = open_blame_view(blame_view, path, commit_id, repo);
3005 view_close(blame_view);
3008 *new_view = blame_view;
3012 static const struct got_error *
3013 log_tree_entry(struct tog_view **new_view, int begin_x,
3014 struct got_tree_entry *te, struct tog_parent_trees *parents,
3015 struct got_object_id *commit_id, struct got_repository *repo)
3017 struct tog_view *log_view;
3018 const struct got_error *err = NULL;
3021 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3022 if (log_view == NULL)
3023 return got_error_from_errno();
3025 err = tree_entry_path(&path, parents, te);
3029 err = open_log_view(log_view, commit_id, repo, path, 0);
3031 view_close(log_view);
3033 *new_view = log_view;
3038 static const struct got_error *
3039 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3040 struct got_object_id *commit_id, struct got_repository *repo)
3042 const struct got_error *err = NULL;
3043 char *commit_id_str = NULL;
3044 struct tog_tree_view_state *s = &view->state.tree;
3046 TAILQ_INIT(&s->parents);
3048 err = got_object_id_str(&commit_id_str, commit_id);
3052 if (asprintf(&s->tree_label, "commit: %s", commit_id_str) == -1) {
3053 err = got_error_from_errno();
3057 s->root = s->tree = root;
3058 s->entries = got_object_tree_get_entries(root);
3059 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3060 s->commit_id = got_object_id_dup(commit_id);
3061 if (s->commit_id == NULL) {
3062 err = got_error_from_errno();
3067 view->show = show_tree_view;
3068 view->input = input_tree_view;
3069 view->close = close_tree_view;
3071 free(commit_id_str);
3073 free(s->tree_label);
3074 s->tree_label = NULL;
3079 static const struct got_error *
3080 close_tree_view(struct tog_view *view)
3082 struct tog_tree_view_state *s = &view->state.tree;
3084 free(s->tree_label);
3085 s->tree_label = NULL;
3087 s->commit_id = NULL;
3088 while (!TAILQ_EMPTY(&s->parents)) {
3089 struct tog_parent_tree *parent;
3090 parent = TAILQ_FIRST(&s->parents);
3091 TAILQ_REMOVE(&s->parents, parent, entry);
3095 if (s->tree != s->root)
3096 got_object_tree_close(s->tree);
3097 got_object_tree_close(s->root);
3102 static const struct got_error *
3103 show_tree_view(struct tog_view *view)
3105 const struct got_error *err = NULL;
3106 struct tog_tree_view_state *s = &view->state.tree;
3109 err = tree_entry_path(&parent_path, &s->parents, NULL);
3113 err = draw_tree_entries(view, &s->first_displayed_entry,
3114 &s->last_displayed_entry, &s->selected_entry,
3115 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3116 s->entries, s->selected, view->nlines, s->tree == s->root);
3123 static const struct got_error *
3124 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3125 struct tog_view **focus_view, struct tog_view *view, int ch)
3127 const struct got_error *err = NULL;
3128 struct tog_tree_view_state *s = &view->state.tree;
3129 struct tog_view *log_view;
3134 s->show_ids = !s->show_ids;
3137 if (!s->selected_entry)
3139 if (view_is_parent_view(view))
3140 begin_x = view_split_begin_x(view->begin_x);
3141 err = log_tree_entry(&log_view, begin_x,
3142 s->selected_entry, &s->parents,
3143 s->commit_id, s->repo);
3144 if (view_is_parent_view(view)) {
3145 err = view_close_child(view);
3148 err = view_set_child(view, log_view);
3150 view_close(log_view);
3153 *focus_view = log_view;
3154 view->child_focussed = 1;
3156 *new_view = log_view;
3160 if (s->selected > 0)
3162 if (s->selected > 0)
3164 tree_scroll_up(&s->first_displayed_entry, 1,
3165 s->entries, s->tree == s->root);
3168 if (SIMPLEQ_FIRST(&s->entries->head) ==
3169 s->first_displayed_entry) {
3170 if (s->tree != s->root)
3171 s->first_displayed_entry = NULL;
3175 tree_scroll_up(&s->first_displayed_entry,
3176 view->nlines, s->entries,
3177 s->tree == s->root);
3181 if (s->selected < s->ndisplayed - 1) {
3185 tree_scroll_down(&s->first_displayed_entry, 1,
3186 s->last_displayed_entry, s->entries);
3189 tree_scroll_down(&s->first_displayed_entry,
3190 view->nlines, s->last_displayed_entry,
3192 if (SIMPLEQ_NEXT(s->last_displayed_entry,
3195 /* can't scroll any further; move cursor down */
3196 if (s->selected < s->ndisplayed - 1)
3197 s->selected = s->ndisplayed - 1;
3201 if (s->selected_entry == NULL) {
3202 struct tog_parent_tree *parent;
3204 /* user selected '..' */
3205 if (s->tree == s->root)
3207 parent = TAILQ_FIRST(&s->parents);
3208 TAILQ_REMOVE(&s->parents, parent,
3210 got_object_tree_close(s->tree);
3211 s->tree = parent->tree;
3213 got_object_tree_get_entries(s->tree);
3214 s->first_displayed_entry =
3215 parent->first_displayed_entry;
3217 parent->selected_entry;
3218 s->selected = parent->selected;
3220 } else if (S_ISDIR(s->selected_entry->mode)) {
3221 struct tog_parent_tree *parent;
3222 struct got_tree_object *child;
3223 err = got_object_open_as_tree(&child,
3224 s->repo, s->selected_entry->id);
3227 parent = calloc(1, sizeof(*parent));
3228 if (parent == NULL) {
3229 err = got_error_from_errno();
3232 parent->tree = s->tree;
3233 parent->first_displayed_entry =
3234 s->first_displayed_entry;
3235 parent->selected_entry = s->selected_entry;
3236 parent->selected = s->selected;
3237 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3240 got_object_tree_get_entries(s->tree);
3242 s->first_displayed_entry = NULL;
3243 } else if (S_ISREG(s->selected_entry->mode)) {
3244 struct tog_view *blame_view;
3245 int begin_x = view_is_parent_view(view) ?
3246 view_split_begin_x(view->begin_x) : 0;
3248 err = blame_tree_entry(&blame_view, begin_x,
3249 s->selected_entry, &s->parents, s->commit_id,
3253 if (view_is_parent_view(view)) {
3254 err = view_close_child(view);
3257 err = view_set_child(view, blame_view);
3259 view_close(blame_view);
3262 *focus_view = blame_view;
3263 view->child_focussed = 1;
3265 *new_view = blame_view;
3269 if (s->selected > view->nlines)
3270 s->selected = s->ndisplayed - 1;
3283 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3288 static const struct got_error *
3289 cmd_tree(int argc, char *argv[])
3291 const struct got_error *error;
3292 struct got_repository *repo = NULL;
3293 char *repo_path = NULL;
3294 struct got_object_id *commit_id = NULL;
3295 char *commit_id_arg = NULL;
3296 struct got_commit_object *commit = NULL;
3297 struct got_tree_object *tree = NULL;
3299 struct tog_view *view;
3302 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd", NULL)
3307 while ((ch = getopt(argc, argv, "c:")) != -1) {
3310 commit_id_arg = optarg;
3322 repo_path = getcwd(NULL, 0);
3323 if (repo_path == NULL)
3324 return got_error_from_errno();
3325 } else if (argc == 1) {
3326 repo_path = realpath(argv[0], NULL);
3327 if (repo_path == NULL)
3328 return got_error_from_errno();
3332 error = got_repo_open(&repo, repo_path);
3337 if (commit_id_arg == NULL) {
3338 error = get_head_commit_id(&commit_id, repo);
3342 struct got_object *obj;
3343 error = got_object_open_by_id_str(&obj, repo, commit_id_arg);
3344 if (error == NULL) {
3345 commit_id = got_object_id_dup(got_object_get_id(obj));
3346 if (commit_id == NULL)
3347 error = got_error_from_errno();
3353 error = got_object_open_as_commit(&commit, repo, commit_id);
3357 error = got_object_open_as_tree(&tree, repo, commit->tree_id);
3361 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3363 error = got_error_from_errno();
3366 error = open_tree_view(view, tree, commit_id, repo);
3369 error = view_loop(view);
3373 got_object_commit_close(commit);
3375 got_object_tree_close(tree);
3377 got_repo_close(repo);
3385 halfdelay(1); /* Do fast refresh while initial view is loading. */
3388 intrflush(stdscr, FALSE);
3389 keypad(stdscr, TRUE);
3398 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3399 "Available commands:\n", getprogname());
3400 for (i = 0; i < nitems(tog_commands); i++) {
3401 struct tog_cmd *cmd = &tog_commands[i];
3402 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3408 make_argv(const char *arg0, const char *arg1)
3411 int argc = (arg1 == NULL ? 1 : 2);
3413 argv = calloc(argc, sizeof(char *));
3416 argv[0] = strdup(arg0);
3417 if (argv[0] == NULL)
3420 argv[1] = strdup(arg1);
3421 if (argv[1] == NULL)
3429 main(int argc, char *argv[])
3431 const struct got_error *error = NULL;
3432 struct tog_cmd *cmd = NULL;
3434 char **cmd_argv = NULL;
3436 setlocale(LC_ALL, "");
3438 while ((ch = getopt(argc, argv, "h")) != -1) {
3457 /* Build an argument vector which runs a default command. */
3458 cmd = &tog_commands[0];
3459 cmd_argv = make_argv(cmd->name, NULL);
3464 /* Did the user specific a command? */
3465 for (i = 0; i < nitems(tog_commands); i++) {
3466 if (strncmp(tog_commands[i].name, argv[0],
3467 strlen(argv[0])) == 0) {
3468 cmd = &tog_commands[i];
3470 tog_commands[i].cmd_usage();
3475 /* Did the user specify a repository? */
3476 char *repo_path = realpath(argv[0], NULL);
3478 struct got_repository *repo;
3479 error = got_repo_open(&repo, repo_path);
3481 got_repo_close(repo);
3483 error = got_error_from_errno();
3486 fprintf(stderr, "%s: '%s' is not a "
3487 "known command\n", getprogname(),
3491 fprintf(stderr, "%s: '%s' is neither a known "
3492 "command nor a path to a repository\n",
3493 getprogname(), argv[0]);
3497 cmd = &tog_commands[0];
3498 cmd_argv = make_argv(cmd->name, repo_path);
3506 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
3513 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);