Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
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.
7 *
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.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_path.h"
51 #include "got_worktree.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef MAX
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #endif
61 #define CTRL(x) ((x) & 0x1f)
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 struct tog_cmd {
68 const char *name;
69 const struct got_error *(*cmd_main)(int, char *[]);
70 void (*cmd_usage)(void);
71 const char *descr;
72 };
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log,
87 "show repository history" },
88 { "diff", cmd_diff, usage_diff,
89 "compare files and directories" },
90 { "blame", cmd_blame, usage_blame,
91 "show line-by-line file history" },
92 { "tree", cmd_tree, usage_tree,
93 "browse trees in repository" },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct tog_view *view;
142 struct commit_queue_entry **first_displayed_entry;
143 struct commit_queue_entry **selected_entry;
144 };
146 struct tog_log_view_state {
147 struct commit_queue commits;
148 struct commit_queue_entry *first_displayed_entry;
149 struct commit_queue_entry *last_displayed_entry;
150 struct commit_queue_entry *selected_entry;
151 int selected;
152 char *in_repo_path;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 };
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
163 int nlines;
165 struct tog_view *view;
166 struct got_object_id *commit_id;
167 int *quit;
168 };
170 struct tog_blame_thread_args {
171 const char *path;
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
174 int *complete;
175 };
177 struct tog_blame {
178 FILE *f;
179 size_t filesize;
180 struct tog_blame_line *lines;
181 int nlines;
182 pthread_t thread;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
185 const char *path;
186 };
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
191 int selected_line;
192 int blame_complete;
193 int eof;
194 int done;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
197 char *path;
198 struct got_repository *repo;
199 struct got_reflist_head *refs;
200 struct got_object_id *commit_id;
201 struct tog_blame blame;
202 };
204 struct tog_parent_tree {
205 TAILQ_ENTRY(tog_parent_tree) entry;
206 struct got_tree_object *tree;
207 struct got_tree_entry *first_displayed_entry;
208 struct got_tree_entry *selected_entry;
209 int selected;
210 };
212 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
214 struct tog_tree_view_state {
215 char *tree_label;
216 struct got_tree_object *root;
217 struct got_tree_object *tree;
218 const struct got_tree_entries *entries;
219 struct got_tree_entry *first_displayed_entry;
220 struct got_tree_entry *last_displayed_entry;
221 struct got_tree_entry *selected_entry;
222 int ndisplayed, selected, show_ids;
223 struct tog_parent_trees parents;
224 struct got_object_id *commit_id;
225 struct got_repository *repo;
226 struct got_reflist_head *refs;
227 };
229 /*
230 * We implement two types of views: parent views and child views.
232 * The 'Tab' key switches between a parent view and its child view.
233 * Child views are shown side-by-side to their parent view, provided
234 * there is enough screen estate.
236 * When a new view is opened from within a parent view, this new view
237 * becomes a child view of the parent view, replacing any existing child.
239 * When a new view is opened from within a child view, this new view
240 * becomes a parent view which will obscure the views below until the
241 * user quits the new parent view by typing 'q'.
243 * This list of views contains parent views only.
244 * Child views are only pointed to by their parent view.
245 */
246 TAILQ_HEAD(tog_view_list_head, tog_view);
248 struct tog_view {
249 TAILQ_ENTRY(tog_view) entry;
250 WINDOW *window;
251 PANEL *panel;
252 int nlines, ncols, begin_y, begin_x;
253 int lines, cols; /* copies of LINES and COLS */
254 int focussed;
255 struct tog_view *parent;
256 struct tog_view *child;
257 int child_focussed;
259 /* type-specific state */
260 enum tog_view_type type;
261 union {
262 struct tog_diff_view_state diff;
263 struct tog_log_view_state log;
264 struct tog_blame_view_state blame;
265 struct tog_tree_view_state tree;
266 } state;
268 const struct got_error *(*show)(struct tog_view *);
269 const struct got_error *(*input)(struct tog_view **,
270 struct tog_view **, struct tog_view**, struct tog_view *, int);
271 const struct got_error *(*close)(struct tog_view *);
272 };
274 static const struct got_error *open_diff_view(struct tog_view *,
275 struct got_object_id *, struct got_object_id *, struct tog_view *,
276 struct got_reflist_head *, struct got_repository *);
277 static const struct got_error *show_diff_view(struct tog_view *);
278 static const struct got_error *input_diff_view(struct tog_view **,
279 struct tog_view **, struct tog_view **, struct tog_view *, int);
280 static const struct got_error* close_diff_view(struct tog_view *);
282 static const struct got_error *open_log_view(struct tog_view *,
283 struct got_object_id *, struct got_reflist_head *,
284 struct got_repository *, const char *, int);
285 static const struct got_error * show_log_view(struct tog_view *);
286 static const struct got_error *input_log_view(struct tog_view **,
287 struct tog_view **, struct tog_view **, struct tog_view *, int);
288 static const struct got_error *close_log_view(struct tog_view *);
290 static const struct got_error *open_blame_view(struct tog_view *, char *,
291 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
292 static const struct got_error *show_blame_view(struct tog_view *);
293 static const struct got_error *input_blame_view(struct tog_view **,
294 struct tog_view **, struct tog_view **, struct tog_view *, int);
295 static const struct got_error *close_blame_view(struct tog_view *);
297 static const struct got_error *open_tree_view(struct tog_view *,
298 struct got_tree_object *, struct got_object_id *,
299 struct got_reflist_head *, struct got_repository *);
300 static const struct got_error *show_tree_view(struct tog_view *);
301 static const struct got_error *input_tree_view(struct tog_view **,
302 struct tog_view **, struct tog_view **, struct tog_view *, int);
303 static const struct got_error *close_tree_view(struct tog_view *);
305 static volatile sig_atomic_t tog_sigwinch_received;
307 static void
308 tog_sigwinch(int signo)
310 tog_sigwinch_received = 1;
313 static const struct got_error *
314 view_close(struct tog_view *view)
316 const struct got_error *err = NULL;
318 if (view->child) {
319 view_close(view->child);
320 view->child = NULL;
322 if (view->close)
323 err = view->close(view);
324 if (view->panel)
325 del_panel(view->panel);
326 if (view->window)
327 delwin(view->window);
328 free(view);
329 return err;
332 static struct tog_view *
333 view_open(int nlines, int ncols, int begin_y, int begin_x,
334 enum tog_view_type type)
336 struct tog_view *view = calloc(1, sizeof(*view));
338 if (view == NULL)
339 return NULL;
341 view->type = type;
342 view->lines = LINES;
343 view->cols = COLS;
344 view->nlines = nlines ? nlines : LINES - begin_y;
345 view->ncols = ncols ? ncols : COLS - begin_x;
346 view->begin_y = begin_y;
347 view->begin_x = begin_x;
348 view->window = newwin(nlines, ncols, begin_y, begin_x);
349 if (view->window == NULL) {
350 view_close(view);
351 return NULL;
353 view->panel = new_panel(view->window);
354 if (view->panel == NULL ||
355 set_panel_userptr(view->panel, view) != OK) {
356 view_close(view);
357 return NULL;
360 keypad(view->window, TRUE);
361 return view;
364 static int
365 view_split_begin_x(int begin_x)
367 if (begin_x > 0 || COLS < 120)
368 return 0;
369 return (COLS - MAX(COLS / 2, 80));
372 static const struct got_error *view_resize(struct tog_view *);
374 static const struct got_error *
375 view_splitscreen(struct tog_view *view)
377 const struct got_error *err = NULL;
379 view->begin_y = 0;
380 view->begin_x = view_split_begin_x(0);
381 view->nlines = LINES;
382 view->ncols = COLS - view->begin_x;
383 view->lines = LINES;
384 view->cols = COLS;
385 err = view_resize(view);
386 if (err)
387 return err;
389 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
390 return got_error_prefix_errno("mvwin");
392 return NULL;
395 static const struct got_error *
396 view_fullscreen(struct tog_view *view)
398 const struct got_error *err = NULL;
400 view->begin_x = 0;
401 view->begin_y = 0;
402 view->nlines = LINES;
403 view->ncols = COLS;
404 view->lines = LINES;
405 view->cols = COLS;
406 err = view_resize(view);
407 if (err)
408 return err;
410 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
411 return got_error_prefix_errno("mvwin");
413 return NULL;
416 static int
417 view_is_parent_view(struct tog_view *view)
419 return view->parent == NULL;
422 static const struct got_error *
423 view_resize(struct tog_view *view)
425 int nlines, ncols;
427 if (view->lines > LINES)
428 nlines = view->nlines - (view->lines - LINES);
429 else
430 nlines = view->nlines + (LINES - view->lines);
432 if (view->cols > COLS)
433 ncols = view->ncols - (view->cols - COLS);
434 else
435 ncols = view->ncols + (COLS - view->cols);
437 if (wresize(view->window, nlines, ncols) == ERR)
438 return got_error_prefix_errno("wresize");
439 if (replace_panel(view->panel, view->window) == ERR)
440 return got_error_prefix_errno("replace_panel");
441 wclear(view->window);
443 view->nlines = nlines;
444 view->ncols = ncols;
445 view->lines = LINES;
446 view->cols = COLS;
448 if (view->child) {
449 view->child->begin_x = view_split_begin_x(view->begin_x);
450 if (view->child->begin_x == 0) {
451 view_fullscreen(view->child);
452 if (view->child->focussed)
453 show_panel(view->child->panel);
454 else
455 show_panel(view->panel);
456 } else {
457 view_splitscreen(view->child);
458 show_panel(view->child->panel);
462 return NULL;
465 static const struct got_error *
466 view_close_child(struct tog_view *view)
468 const struct got_error *err = NULL;
470 if (view->child == NULL)
471 return NULL;
473 err = view_close(view->child);
474 view->child = NULL;
475 return err;
478 static const struct got_error *
479 view_set_child(struct tog_view *view, struct tog_view *child)
481 const struct got_error *err = NULL;
483 view->child = child;
484 child->parent = view;
485 return err;
488 static int
489 view_is_splitscreen(struct tog_view *view)
491 return view->begin_x > 0;
494 /*
495 * Erase all content of the view. Can be used to "flash" the view because
496 * the view loop will redraw it quickly, providing a more subtle visual
497 * effect than curs_flash(3) would provide.
498 */
499 static void
500 view_flash(struct tog_view *view)
502 werase(view->window);
503 update_panels();
504 doupdate();
507 static void
508 tog_resizeterm(void)
510 int cols, lines;
511 struct winsize size;
513 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
514 cols = 80; /* Default */
515 lines = 24;
516 } else {
517 cols = size.ws_col;
518 lines = size.ws_row;
520 resize_term(lines, cols);
523 static const struct got_error *
524 view_input(struct tog_view **new, struct tog_view **dead,
525 struct tog_view **focus, int *done, struct tog_view *view,
526 struct tog_view_list_head *views)
528 const struct got_error *err = NULL;
529 struct tog_view *v;
530 int ch, errcode;
532 *new = NULL;
533 *dead = NULL;
534 *focus = NULL;
536 nodelay(stdscr, FALSE);
537 /* Allow threads to make progress while we are waiting for input. */
538 errcode = pthread_mutex_unlock(&tog_mutex);
539 if (errcode)
540 return got_error_set_errno(errcode, "pthread_mutex_unlock");
541 ch = wgetch(view->window);
542 errcode = pthread_mutex_lock(&tog_mutex);
543 if (errcode)
544 return got_error_set_errno(errcode, "pthread_mutex_lock");
545 nodelay(stdscr, TRUE);
547 if (tog_sigwinch_received) {
548 tog_resizeterm();
549 tog_sigwinch_received = 0;
550 TAILQ_FOREACH(v, views, entry) {
551 err = view_resize(v);
552 if (err)
553 return err;
554 err = v->input(new, dead, focus, v, KEY_RESIZE);
555 if (err)
556 return err;
560 switch (ch) {
561 case ERR:
562 break;
563 case '\t':
564 if (view->child) {
565 *focus = view->child;
566 view->child_focussed = 1;
567 } else if (view->parent) {
568 *focus = view->parent;
569 view->parent->child_focussed = 0;
571 break;
572 case 'q':
573 err = view->input(new, dead, focus, view, ch);
574 *dead = view;
575 break;
576 case 'Q':
577 *done = 1;
578 break;
579 case 'f':
580 if (view_is_parent_view(view)) {
581 if (view->child == NULL)
582 break;
583 if (view_is_splitscreen(view->child)) {
584 *focus = view->child;
585 view->child_focussed = 1;
586 err = view_fullscreen(view->child);
587 } else
588 err = view_splitscreen(view->child);
589 if (err)
590 break;
591 err = view->child->input(new, dead, focus,
592 view->child, KEY_RESIZE);
593 } else {
594 if (view_is_splitscreen(view)) {
595 *focus = view;
596 view->parent->child_focussed = 1;
597 err = view_fullscreen(view);
598 } else {
599 err = view_splitscreen(view);
601 if (err)
602 break;
603 err = view->input(new, dead, focus, view,
604 KEY_RESIZE);
606 break;
607 case KEY_RESIZE:
608 break;
609 default:
610 err = view->input(new, dead, focus, view, ch);
611 break;
614 return err;
617 void
618 view_vborder(struct tog_view *view)
620 PANEL *panel;
621 struct tog_view *view_above;
623 if (view->parent)
624 return view_vborder(view->parent);
626 panel = panel_above(view->panel);
627 if (panel == NULL)
628 return;
630 view_above = panel_userptr(panel);
631 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
632 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
635 int
636 view_needs_focus_indication(struct tog_view *view)
638 if (view_is_parent_view(view)) {
639 if (view->child == NULL || view->child_focussed)
640 return 0;
641 if (!view_is_splitscreen(view->child))
642 return 0;
643 } else if (!view_is_splitscreen(view))
644 return 0;
646 return view->focussed;
649 static const struct got_error *
650 view_loop(struct tog_view *view)
652 const struct got_error *err = NULL;
653 struct tog_view_list_head views;
654 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
655 int fast_refresh = 10;
656 int done = 0, errcode;
658 errcode = pthread_mutex_lock(&tog_mutex);
659 if (errcode)
660 return got_error_set_errno(errcode, "pthread_mutex_lock");
662 TAILQ_INIT(&views);
663 TAILQ_INSERT_HEAD(&views, view, entry);
665 main_view = view;
666 view->focussed = 1;
667 err = view->show(view);
668 if (err)
669 return err;
670 update_panels();
671 doupdate();
672 while (!TAILQ_EMPTY(&views) && !done) {
673 /* Refresh fast during initialization, then become slower. */
674 if (fast_refresh && fast_refresh-- == 0)
675 halfdelay(10); /* switch to once per second */
677 err = view_input(&new_view, &dead_view, &focus_view, &done,
678 view, &views);
679 if (err)
680 break;
681 if (dead_view) {
682 struct tog_view *prev = NULL;
684 if (view_is_parent_view(dead_view))
685 prev = TAILQ_PREV(dead_view,
686 tog_view_list_head, entry);
687 else if (view->parent != dead_view)
688 prev = view->parent;
690 if (dead_view->parent)
691 dead_view->parent->child = NULL;
692 else
693 TAILQ_REMOVE(&views, dead_view, entry);
695 err = view_close(dead_view);
696 if (err || dead_view == main_view)
697 goto done;
699 if (view == dead_view) {
700 if (focus_view)
701 view = focus_view;
702 else if (prev)
703 view = prev;
704 else if (!TAILQ_EMPTY(&views))
705 view = TAILQ_LAST(&views,
706 tog_view_list_head);
707 else
708 view = NULL;
709 if (view) {
710 if (view->child && view->child_focussed)
711 focus_view = view->child;
712 else
713 focus_view = view;
717 if (new_view) {
718 struct tog_view *v, *t;
719 /* Only allow one parent view per type. */
720 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
721 if (v->type != new_view->type)
722 continue;
723 TAILQ_REMOVE(&views, v, entry);
724 err = view_close(v);
725 if (err)
726 goto done;
727 break;
729 TAILQ_INSERT_TAIL(&views, new_view, entry);
730 view = new_view;
731 if (focus_view == NULL)
732 focus_view = new_view;
734 if (focus_view) {
735 show_panel(focus_view->panel);
736 if (view)
737 view->focussed = 0;
738 focus_view->focussed = 1;
739 view = focus_view;
740 if (new_view)
741 show_panel(new_view->panel);
742 if (view->child && view_is_splitscreen(view->child))
743 show_panel(view->child->panel);
745 if (view) {
746 if (focus_view == NULL) {
747 view->focussed = 1;
748 show_panel(view->panel);
749 if (view->child && view_is_splitscreen(view->child))
750 show_panel(view->child->panel);
751 focus_view = view;
753 if (view->parent) {
754 err = view->parent->show(view->parent);
755 if (err)
756 goto done;
758 err = view->show(view);
759 if (err)
760 goto done;
761 if (view->child) {
762 err = view->child->show(view->child);
763 if (err)
764 goto done;
766 update_panels();
767 doupdate();
770 done:
771 while (!TAILQ_EMPTY(&views)) {
772 view = TAILQ_FIRST(&views);
773 TAILQ_REMOVE(&views, view, entry);
774 view_close(view);
777 errcode = pthread_mutex_unlock(&tog_mutex);
778 if (errcode)
779 return got_error_set_errno(errcode, "pthread_mutex_unlock");
781 return err;
784 __dead static void
785 usage_log(void)
787 endwin();
788 fprintf(stderr,
789 "usage: %s log [-c commit] [-r repository-path] [path]\n",
790 getprogname());
791 exit(1);
794 /* Create newly allocated wide-character string equivalent to a byte string. */
795 static const struct got_error *
796 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
798 char *vis = NULL;
799 const struct got_error *err = NULL;
801 *ws = NULL;
802 *wlen = mbstowcs(NULL, s, 0);
803 if (*wlen == (size_t)-1) {
804 int vislen;
805 if (errno != EILSEQ)
806 return got_error_prefix_errno("mbstowcs");
808 /* byte string invalid in current encoding; try to "fix" it */
809 err = got_mbsavis(&vis, &vislen, s);
810 if (err)
811 return err;
812 *wlen = mbstowcs(NULL, vis, 0);
813 if (*wlen == (size_t)-1) {
814 err = got_error_prefix_errno("mbstowcs"); /* give up */
815 goto done;
819 *ws = calloc(*wlen + 1, sizeof(*ws));
820 if (*ws == NULL) {
821 err = got_error_prefix_errno("calloc");
822 goto done;
825 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
826 err = got_error_prefix_errno("mbstowcs");
827 done:
828 free(vis);
829 if (err) {
830 free(*ws);
831 *ws = NULL;
832 *wlen = 0;
834 return err;
837 /* Format a line for display, ensuring that it won't overflow a width limit. */
838 static const struct got_error *
839 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
841 const struct got_error *err = NULL;
842 int cols = 0;
843 wchar_t *wline = NULL;
844 size_t wlen;
845 int i;
847 *wlinep = NULL;
848 *widthp = 0;
850 err = mbs2ws(&wline, &wlen, line);
851 if (err)
852 return err;
854 i = 0;
855 while (i < wlen && cols < wlimit) {
856 int width = wcwidth(wline[i]);
857 switch (width) {
858 case 0:
859 i++;
860 break;
861 case 1:
862 case 2:
863 if (cols + width <= wlimit)
864 cols += width;
865 i++;
866 break;
867 case -1:
868 if (wline[i] == L'\t')
869 cols += TABSIZE - ((cols + 1) % TABSIZE);
870 i++;
871 break;
872 default:
873 err = got_error_prefix_errno("wcwidth");
874 goto done;
877 wline[i] = L'\0';
878 if (widthp)
879 *widthp = cols;
880 done:
881 if (err)
882 free(wline);
883 else
884 *wlinep = wline;
885 return err;
888 static const struct got_error*
889 build_refs_str(char **refs_str, struct got_reflist_head *refs,
890 struct got_object_id *id)
892 static const struct got_error *err = NULL;
893 struct got_reflist_entry *re;
894 char *s;
895 const char *name;
897 *refs_str = NULL;
899 SIMPLEQ_FOREACH(re, refs, entry) {
900 if (got_object_id_cmp(re->id, id) != 0)
901 continue;
902 name = got_ref_get_name(re->ref);
903 if (strcmp(name, GOT_REF_HEAD) == 0)
904 continue;
905 if (strncmp(name, "refs/", 5) == 0)
906 name += 5;
907 if (strncmp(name, "got/", 4) == 0)
908 continue;
909 if (strncmp(name, "heads/", 6) == 0)
910 name += 6;
911 if (strncmp(name, "remotes/", 8) == 0)
912 name += 8;
913 s = *refs_str;
914 if (asprintf(refs_str, "%s%s%s", s ? s : "",
915 s ? ", " : "", name) == -1) {
916 err = got_error_prefix_errno("asprintf");
917 free(s);
918 *refs_str = NULL;
919 break;
921 free(s);
924 return err;
927 static const struct got_error *
928 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
930 char *smallerthan, *at;
932 smallerthan = strchr(author, '<');
933 if (smallerthan && smallerthan[1] != '\0')
934 author = smallerthan + 1;
935 at = strchr(author, '@');
936 if (at)
937 *at = '\0';
938 return format_line(wauthor, author_width, author, limit);
941 static const struct got_error *
942 draw_commit(struct tog_view *view, struct got_commit_object *commit,
943 struct got_object_id *id, struct got_reflist_head *refs,
944 int author_display_cols)
946 const struct got_error *err = NULL;
947 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
948 char *logmsg0 = NULL, *logmsg = NULL;
949 char *author = NULL;
950 wchar_t *wlogmsg = NULL, *wauthor = NULL;
951 int author_width, logmsg_width;
952 char *newline, *line = NULL;
953 int col, limit;
954 static const size_t date_display_cols = 9;
955 const int avail = view->ncols;
956 struct tm tm;
957 time_t committer_time;
959 committer_time = got_object_commit_get_committer_time(commit);
960 if (localtime_r(&committer_time, &tm) == NULL)
961 return got_error_prefix_errno("localtime_r");
962 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
963 >= sizeof(datebuf))
964 return got_error(GOT_ERR_NO_SPACE);
966 if (avail < date_display_cols)
967 limit = MIN(sizeof(datebuf) - 1, avail);
968 else
969 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
970 waddnstr(view->window, datebuf, limit);
971 col = limit + 1;
972 if (col > avail)
973 goto done;
975 author = strdup(got_object_commit_get_author(commit));
976 if (author == NULL) {
977 err = got_error_prefix_errno("strdup");
978 goto done;
980 err = format_author(&wauthor, &author_width, author, avail - col);
981 if (err)
982 goto done;
983 waddwstr(view->window, wauthor);
984 col += author_width;
985 while (col <= avail && author_width < author_display_cols + 2) {
986 waddch(view->window, ' ');
987 col++;
988 author_width++;
990 if (col > avail)
991 goto done;
993 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
994 if (logmsg0 == NULL) {
995 err = got_error_prefix_errno("strdup");
996 goto done;
998 logmsg = logmsg0;
999 while (*logmsg == '\n')
1000 logmsg++;
1001 newline = strchr(logmsg, '\n');
1002 if (newline)
1003 *newline = '\0';
1004 limit = avail - col;
1005 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1006 if (err)
1007 goto done;
1008 waddwstr(view->window, wlogmsg);
1009 col += logmsg_width;
1010 while (col <= avail) {
1011 waddch(view->window, ' ');
1012 col++;
1014 done:
1015 free(logmsg0);
1016 free(wlogmsg);
1017 free(author);
1018 free(wauthor);
1019 free(line);
1020 return err;
1023 static struct commit_queue_entry *
1024 alloc_commit_queue_entry(struct got_commit_object *commit,
1025 struct got_object_id *id)
1027 struct commit_queue_entry *entry;
1029 entry = calloc(1, sizeof(*entry));
1030 if (entry == NULL)
1031 return NULL;
1033 entry->id = id;
1034 entry->commit = commit;
1035 return entry;
1038 static void
1039 pop_commit(struct commit_queue *commits)
1041 struct commit_queue_entry *entry;
1043 entry = TAILQ_FIRST(&commits->head);
1044 TAILQ_REMOVE(&commits->head, entry, entry);
1045 got_object_commit_close(entry->commit);
1046 commits->ncommits--;
1047 /* Don't free entry->id! It is owned by the commit graph. */
1048 free(entry);
1051 static void
1052 free_commits(struct commit_queue *commits)
1054 while (!TAILQ_EMPTY(&commits->head))
1055 pop_commit(commits);
1058 static const struct got_error *
1059 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1060 int minqueue, struct got_repository *repo, const char *path)
1062 const struct got_error *err = NULL;
1063 int nqueued = 0;
1066 * We keep all commits open throughout the lifetime of the log
1067 * view in order to avoid having to re-fetch commits from disk
1068 * while updating the display.
1070 while (nqueued < minqueue) {
1071 struct got_object_id *id;
1072 struct got_commit_object *commit;
1073 struct commit_queue_entry *entry;
1074 int errcode;
1076 err = got_commit_graph_iter_next(&id, graph);
1077 if (err) {
1078 if (err->code != GOT_ERR_ITER_NEED_MORE)
1079 break;
1080 err = got_commit_graph_fetch_commits(graph,
1081 minqueue, repo);
1082 if (err)
1083 return err;
1084 continue;
1087 if (id == NULL)
1088 break;
1090 err = got_object_open_as_commit(&commit, repo, id);
1091 if (err)
1092 break;
1093 entry = alloc_commit_queue_entry(commit, id);
1094 if (entry == NULL) {
1095 err = got_error_prefix_errno("alloc_commit_queue_entry");
1096 break;
1099 errcode = pthread_mutex_lock(&tog_mutex);
1100 if (errcode) {
1101 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1102 break;
1105 entry->idx = commits->ncommits;
1106 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1107 nqueued++;
1108 commits->ncommits++;
1110 errcode = pthread_mutex_unlock(&tog_mutex);
1111 if (errcode && err == NULL)
1112 err = got_error_set_errno(errcode,
1113 "pthread_mutex_unlock");
1116 return err;
1119 static const struct got_error *
1120 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1122 const struct got_error *err = NULL;
1123 struct got_reference *head_ref;
1125 *head_id = NULL;
1127 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1128 if (err)
1129 return err;
1131 err = got_ref_resolve(head_id, repo, head_ref);
1132 got_ref_close(head_ref);
1133 if (err) {
1134 *head_id = NULL;
1135 return err;
1138 return NULL;
1141 static const struct got_error *
1142 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1143 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1144 struct commit_queue *commits, int selected_idx, int limit,
1145 struct got_reflist_head *refs, const char *path, int commits_needed)
1147 const struct got_error *err = NULL;
1148 struct commit_queue_entry *entry;
1149 int ncommits, width;
1150 int author_cols = 10;
1151 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1152 char *refs_str = NULL;
1153 wchar_t *wline;
1155 entry = first;
1156 ncommits = 0;
1157 while (entry) {
1158 if (ncommits == selected_idx) {
1159 *selected = entry;
1160 break;
1162 entry = TAILQ_NEXT(entry, entry);
1163 ncommits++;
1166 if (*selected) {
1167 err = got_object_id_str(&id_str, (*selected)->id);
1168 if (err)
1169 return err;
1170 if (refs) {
1171 err = build_refs_str(&refs_str, refs, (*selected)->id);
1172 if (err)
1173 goto done;
1177 if (commits_needed == 0)
1178 halfdelay(10); /* disable fast refresh */
1180 if (asprintf(&ncommits_str, " [%d/%d] %s",
1181 entry ? entry->idx + 1 : 0, commits->ncommits,
1182 commits_needed > 0 ? "loading... " :
1183 (refs_str ? refs_str : "")) == -1) {
1184 err = got_error_prefix_errno("asprintf");
1185 goto done;
1188 if (path && strcmp(path, "/") != 0) {
1189 if (asprintf(&header, "commit %s %s%s",
1190 id_str ? id_str : "........................................",
1191 path, ncommits_str) == -1) {
1192 err = got_error_prefix_errno("asprintf");
1193 header = NULL;
1194 goto done;
1196 } else if (asprintf(&header, "commit %s%s",
1197 id_str ? id_str : "........................................",
1198 ncommits_str) == -1) {
1199 err = got_error_prefix_errno("asprintf");
1200 header = NULL;
1201 goto done;
1203 err = format_line(&wline, &width, header, view->ncols);
1204 if (err)
1205 goto done;
1207 werase(view->window);
1209 if (view_needs_focus_indication(view))
1210 wstandout(view->window);
1211 waddwstr(view->window, wline);
1212 while (width < view->ncols) {
1213 waddch(view->window, ' ');
1214 width++;
1216 if (view_needs_focus_indication(view))
1217 wstandend(view->window);
1218 free(wline);
1219 if (limit <= 1)
1220 goto done;
1222 /* Grow author column size if necessary. */
1223 entry = first;
1224 ncommits = 0;
1225 while (entry) {
1226 char *author;
1227 wchar_t *wauthor;
1228 int width;
1229 if (ncommits >= limit - 1)
1230 break;
1231 author = strdup(got_object_commit_get_author(entry->commit));
1232 if (author == NULL) {
1233 err = got_error_prefix_errno("strdup");
1234 goto done;
1236 err = format_author(&wauthor, &width, author, COLS);
1237 if (author_cols < width)
1238 author_cols = width;
1239 free(wauthor);
1240 free(author);
1241 entry = TAILQ_NEXT(entry, entry);
1244 entry = first;
1245 *last = first;
1246 ncommits = 0;
1247 while (entry) {
1248 if (ncommits >= limit - 1)
1249 break;
1250 if (ncommits == selected_idx)
1251 wstandout(view->window);
1252 err = draw_commit(view, entry->commit, entry->id, refs,
1253 author_cols);
1254 if (ncommits == selected_idx)
1255 wstandend(view->window);
1256 if (err)
1257 break;
1258 ncommits++;
1259 *last = entry;
1260 entry = TAILQ_NEXT(entry, entry);
1263 view_vborder(view);
1264 done:
1265 free(id_str);
1266 free(refs_str);
1267 free(ncommits_str);
1268 free(header);
1269 return err;
1272 static void
1273 scroll_up(struct tog_view *view,
1274 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1275 struct commit_queue *commits)
1277 struct commit_queue_entry *entry;
1278 int nscrolled = 0;
1280 entry = TAILQ_FIRST(&commits->head);
1281 if (*first_displayed_entry == entry) {
1282 view_flash(view);
1283 return;
1286 entry = *first_displayed_entry;
1287 while (entry && nscrolled < maxscroll) {
1288 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1289 if (entry) {
1290 *first_displayed_entry = entry;
1291 nscrolled++;
1296 static const struct got_error *
1297 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1298 pthread_cond_t *need_commits)
1300 int errcode;
1301 int max_wait = 20;
1303 halfdelay(1); /* fast refresh while loading commits */
1305 while (*commits_needed > 0) {
1306 if (*log_complete)
1307 break;
1309 /* Wake the log thread. */
1310 errcode = pthread_cond_signal(need_commits);
1311 if (errcode)
1312 return got_error_set_errno(errcode,
1313 "pthread_cond_signal");
1314 errcode = pthread_mutex_unlock(&tog_mutex);
1315 if (errcode)
1316 return got_error_set_errno(errcode,
1317 "pthread_mutex_unlock");
1318 pthread_yield();
1319 errcode = pthread_mutex_lock(&tog_mutex);
1320 if (errcode)
1321 return got_error_set_errno(errcode,
1322 "pthread_mutex_lock");
1324 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1326 * Thread is not done yet; lose a key press
1327 * and let the user retry... this way the GUI
1328 * remains interactive while logging deep paths
1329 * with few commits in history.
1331 return NULL;
1335 return NULL;
1338 static const struct got_error *
1339 scroll_down(struct tog_view *view,
1340 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1341 struct commit_queue_entry **last_displayed_entry,
1342 struct commit_queue *commits, int *log_complete, int *commits_needed,
1343 pthread_cond_t *need_commits)
1345 const struct got_error *err = NULL;
1346 struct commit_queue_entry *pentry;
1347 int nscrolled = 0;
1349 if (*last_displayed_entry == NULL)
1350 return NULL;
1352 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1353 if (pentry == NULL && !*log_complete) {
1355 * Ask the log thread for required amount of commits
1356 * plus some amount of pre-fetching.
1358 (*commits_needed) += maxscroll + 20;
1359 err = trigger_log_thread(0, commits_needed, log_complete,
1360 need_commits);
1361 if (err)
1362 return err;
1365 do {
1366 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1367 if (pentry == NULL) {
1368 if (*log_complete)
1369 view_flash(view);
1370 break;
1373 *last_displayed_entry = pentry;
1375 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1376 if (pentry == NULL)
1377 break;
1378 *first_displayed_entry = pentry;
1379 } while (++nscrolled < maxscroll);
1381 return err;
1384 static const struct got_error *
1385 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1386 struct got_commit_object *commit, struct got_object_id *commit_id,
1387 struct tog_view *log_view, struct got_reflist_head *refs,
1388 struct got_repository *repo)
1390 const struct got_error *err;
1391 struct got_object_qid *parent_id;
1392 struct tog_view *diff_view;
1394 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1395 if (diff_view == NULL)
1396 return got_error_prefix_errno("view_open");
1398 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1399 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1400 commit_id, log_view, refs, repo);
1401 if (err == NULL)
1402 *new_view = diff_view;
1403 return err;
1406 static const struct got_error *
1407 browse_commit(struct tog_view **new_view, int begin_x,
1408 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1409 struct got_repository *repo)
1411 const struct got_error *err = NULL;
1412 struct got_tree_object *tree;
1413 struct tog_view *tree_view;
1415 err = got_object_open_as_tree(&tree, repo,
1416 got_object_commit_get_tree_id(entry->commit));
1417 if (err)
1418 return err;
1420 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1421 if (tree_view == NULL)
1422 return got_error_prefix_errno("view_open");
1424 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1425 if (err)
1426 got_object_tree_close(tree);
1427 else
1428 *new_view = tree_view;
1429 return err;
1432 static void *
1433 log_thread(void *arg)
1435 const struct got_error *err = NULL;
1436 int errcode = 0;
1437 struct tog_log_thread_args *a = arg;
1438 int done = 0;
1440 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1441 if (err)
1442 return (void *)err;
1444 while (!done && !err) {
1445 err = queue_commits(a->graph, a->commits, 1, a->repo,
1446 a->in_repo_path);
1447 if (err) {
1448 if (err->code != GOT_ERR_ITER_COMPLETED)
1449 return (void *)err;
1450 err = NULL;
1451 done = 1;
1452 } else if (a->commits_needed > 0)
1453 a->commits_needed--;
1455 errcode = pthread_mutex_lock(&tog_mutex);
1456 if (errcode) {
1457 err = got_error_set_errno(errcode,
1458 "pthread_mutex_lock");
1459 break;
1460 } else if (*a->quit)
1461 done = 1;
1462 else if (*a->first_displayed_entry == NULL) {
1463 *a->first_displayed_entry =
1464 TAILQ_FIRST(&a->commits->head);
1465 *a->selected_entry = *a->first_displayed_entry;
1468 if (done)
1469 a->commits_needed = 0;
1470 else if (a->commits_needed == 0) {
1471 errcode = pthread_cond_wait(&a->need_commits,
1472 &tog_mutex);
1473 if (errcode)
1474 err = got_error_set_errno(errcode,
1475 "pthread_cond_wait");
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1479 if (errcode && err == NULL)
1480 err = got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1483 a->log_complete = 1;
1484 return (void *)err;
1487 static const struct got_error *
1488 stop_log_thread(struct tog_log_view_state *s)
1490 const struct got_error *err = NULL;
1491 int errcode;
1493 if (s->thread) {
1494 s->quit = 1;
1495 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1496 if (errcode)
1497 return got_error_set_errno(errcode,
1498 "pthread_cond_signal");
1499 errcode = pthread_mutex_unlock(&tog_mutex);
1500 if (errcode)
1501 return got_error_set_errno(errcode,
1502 "pthread_mutex_unlock");
1503 errcode = pthread_join(s->thread, (void **)&err);
1504 if (errcode)
1505 return got_error_set_errno(errcode, "pthread_join");
1506 errcode = pthread_mutex_lock(&tog_mutex);
1507 if (errcode)
1508 return got_error_set_errno(errcode,
1509 "pthread_mutex_lock");
1510 s->thread = NULL;
1513 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1514 if (errcode && err == NULL)
1515 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1517 if (s->thread_args.repo) {
1518 got_repo_close(s->thread_args.repo);
1519 s->thread_args.repo = NULL;
1522 if (s->thread_args.graph) {
1523 got_commit_graph_close(s->thread_args.graph);
1524 s->thread_args.graph = NULL;
1527 return err;
1530 static const struct got_error *
1531 close_log_view(struct tog_view *view)
1533 const struct got_error *err = NULL;
1534 struct tog_log_view_state *s = &view->state.log;
1536 err = stop_log_thread(s);
1537 free_commits(&s->commits);
1538 free(s->in_repo_path);
1539 s->in_repo_path = NULL;
1540 free(s->start_id);
1541 s->start_id = NULL;
1542 return err;
1545 static const struct got_error *
1546 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1547 struct got_reflist_head *refs, struct got_repository *repo,
1548 const char *path, int check_disk)
1550 const struct got_error *err = NULL;
1551 struct tog_log_view_state *s = &view->state.log;
1552 struct got_repository *thread_repo = NULL;
1553 struct got_commit_graph *thread_graph = NULL;
1554 int errcode;
1556 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1557 if (err != NULL)
1558 goto done;
1560 /* The commit queue only contains commits being displayed. */
1561 TAILQ_INIT(&s->commits.head);
1562 s->commits.ncommits = 0;
1564 s->refs = refs;
1565 s->repo = repo;
1566 s->start_id = got_object_id_dup(start_id);
1567 if (s->start_id == NULL) {
1568 err = got_error_prefix_errno("got_object_id_dup");
1569 goto done;
1572 view->show = show_log_view;
1573 view->input = input_log_view;
1574 view->close = close_log_view;
1576 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1577 if (err)
1578 goto done;
1579 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1580 0, thread_repo);
1581 if (err)
1582 goto done;
1584 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1585 if (errcode) {
1586 err = got_error_set_errno(errcode, "pthread_cond_init");
1587 goto done;
1590 s->thread_args.commits_needed = view->nlines;
1591 s->thread_args.graph = thread_graph;
1592 s->thread_args.commits = &s->commits;
1593 s->thread_args.in_repo_path = s->in_repo_path;
1594 s->thread_args.start_id = s->start_id;
1595 s->thread_args.repo = thread_repo;
1596 s->thread_args.log_complete = 0;
1597 s->thread_args.quit = &s->quit;
1598 s->thread_args.view = view;
1599 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1600 s->thread_args.selected_entry = &s->selected_entry;
1601 done:
1602 if (err)
1603 close_log_view(view);
1604 return err;
1607 static const struct got_error *
1608 show_log_view(struct tog_view *view)
1610 struct tog_log_view_state *s = &view->state.log;
1612 if (s->thread == NULL) {
1613 int errcode = pthread_create(&s->thread, NULL, log_thread,
1614 &s->thread_args);
1615 if (errcode)
1616 return got_error_set_errno(errcode, "pthread_create");
1619 return draw_commits(view, &s->last_displayed_entry,
1620 &s->selected_entry, s->first_displayed_entry,
1621 &s->commits, s->selected, view->nlines, s->refs,
1622 s->in_repo_path, s->thread_args.commits_needed);
1625 static const struct got_error *
1626 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1627 struct tog_view **focus_view, struct tog_view *view, int ch)
1629 const struct got_error *err = NULL;
1630 struct tog_log_view_state *s = &view->state.log;
1631 char *parent_path;
1632 struct tog_view *diff_view = NULL, *tree_view = NULL;
1633 int begin_x = 0;
1635 switch (ch) {
1636 case 'q':
1637 s->quit = 1;
1638 break;
1639 case 'k':
1640 case KEY_UP:
1641 case '<':
1642 case ',':
1643 if (s->first_displayed_entry == NULL)
1644 break;
1645 if (s->selected > 0)
1646 s->selected--;
1647 if (s->selected > 0)
1648 break;
1649 scroll_up(view, &s->first_displayed_entry, 1,
1650 &s->commits);
1651 break;
1652 case KEY_PPAGE:
1653 case CTRL('b'):
1654 if (s->first_displayed_entry == NULL)
1655 break;
1656 if (TAILQ_FIRST(&s->commits.head) ==
1657 s->first_displayed_entry) {
1658 if (s->selected == 0) {
1659 view_flash(view);
1660 break;
1662 s->selected = 0;
1663 break;
1665 scroll_up(view, &s->first_displayed_entry,
1666 view->nlines, &s->commits);
1667 break;
1668 case 'j':
1669 case KEY_DOWN:
1670 case '>':
1671 case '.':
1672 if (s->first_displayed_entry == NULL)
1673 break;
1674 if (s->selected < MIN(view->nlines - 2,
1675 s->commits.ncommits - 1)) {
1676 s->selected++;
1677 break;
1679 err = scroll_down(view, &s->first_displayed_entry, 1,
1680 &s->last_displayed_entry, &s->commits,
1681 &s->thread_args.log_complete,
1682 &s->thread_args.commits_needed,
1683 &s->thread_args.need_commits);
1684 break;
1685 case KEY_NPAGE:
1686 case CTRL('f'): {
1687 struct commit_queue_entry *first;
1688 first = s->first_displayed_entry;
1689 if (first == NULL)
1690 break;
1691 err = scroll_down(view, &s->first_displayed_entry,
1692 view->nlines, &s->last_displayed_entry,
1693 &s->commits, &s->thread_args.log_complete,
1694 &s->thread_args.commits_needed,
1695 &s->thread_args.need_commits);
1696 if (first == s->first_displayed_entry &&
1697 s->selected < MIN(view->nlines - 2,
1698 s->commits.ncommits - 1)) {
1699 /* can't scroll further down */
1700 s->selected = MIN(view->nlines - 2,
1701 s->commits.ncommits - 1);
1703 err = NULL;
1704 break;
1706 case KEY_RESIZE:
1707 if (s->selected > view->nlines - 2)
1708 s->selected = view->nlines - 2;
1709 if (s->selected > s->commits.ncommits - 1)
1710 s->selected = s->commits.ncommits - 1;
1711 break;
1712 case KEY_ENTER:
1713 case '\r':
1714 if (s->selected_entry == NULL)
1715 break;
1716 if (view_is_parent_view(view))
1717 begin_x = view_split_begin_x(view->begin_x);
1718 err = open_diff_view_for_commit(&diff_view, begin_x,
1719 s->selected_entry->commit, s->selected_entry->id,
1720 view, s->refs, s->repo);
1721 if (err)
1722 break;
1723 if (view_is_parent_view(view)) {
1724 err = view_close_child(view);
1725 if (err)
1726 return err;
1727 err = view_set_child(view, diff_view);
1728 if (err) {
1729 view_close(diff_view);
1730 break;
1732 *focus_view = diff_view;
1733 view->child_focussed = 1;
1734 } else
1735 *new_view = diff_view;
1736 break;
1737 case 't':
1738 if (s->selected_entry == NULL)
1739 break;
1740 if (view_is_parent_view(view))
1741 begin_x = view_split_begin_x(view->begin_x);
1742 err = browse_commit(&tree_view, begin_x,
1743 s->selected_entry, s->refs, s->repo);
1744 if (err)
1745 break;
1746 if (view_is_parent_view(view)) {
1747 err = view_close_child(view);
1748 if (err)
1749 return err;
1750 err = view_set_child(view, tree_view);
1751 if (err) {
1752 view_close(tree_view);
1753 break;
1755 *focus_view = tree_view;
1756 view->child_focussed = 1;
1757 } else
1758 *new_view = tree_view;
1759 break;
1760 case KEY_BACKSPACE:
1761 if (strcmp(s->in_repo_path, "/") == 0)
1762 break;
1763 parent_path = dirname(s->in_repo_path);
1764 if (parent_path && strcmp(parent_path, ".") != 0) {
1765 struct tog_view *lv;
1766 err = stop_log_thread(s);
1767 if (err)
1768 return err;
1769 lv = view_open(view->nlines, view->ncols,
1770 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1771 if (lv == NULL)
1772 return got_error_prefix_errno(
1773 "view_open");
1774 err = open_log_view(lv, s->start_id, s->refs,
1775 s->repo, parent_path, 0);
1776 if (err)
1777 return err;;
1778 if (view_is_parent_view(view))
1779 *new_view = lv;
1780 else {
1781 view_set_child(view->parent, lv);
1782 *focus_view = lv;
1784 return NULL;
1786 break;
1787 default:
1788 break;
1791 return err;
1794 static const struct got_error *
1795 apply_unveil(const char *repo_path, const char *worktree_path)
1797 const struct got_error *error;
1799 if (repo_path && unveil(repo_path, "r") != 0)
1800 return got_error_prefix_errno2("unveil", repo_path);
1802 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1803 return got_error_prefix_errno2("unveil", worktree_path);
1805 if (unveil("/tmp", "rwc") != 0)
1806 return got_error_prefix_errno2("unveil", "/tmp");
1808 error = got_privsep_unveil_exec_helpers();
1809 if (error != NULL)
1810 return error;
1812 if (unveil(NULL, NULL) != 0)
1813 return got_error_prefix_errno("unveil");
1815 return NULL;
1818 static void
1819 init_curses(void)
1821 initscr();
1822 cbreak();
1823 halfdelay(1); /* Do fast refresh while initial view is loading. */
1824 noecho();
1825 nonl();
1826 intrflush(stdscr, FALSE);
1827 keypad(stdscr, TRUE);
1828 curs_set(0);
1829 signal(SIGWINCH, tog_sigwinch);
1832 static const struct got_error *
1833 cmd_log(int argc, char *argv[])
1835 const struct got_error *error;
1836 struct got_repository *repo = NULL;
1837 struct got_worktree *worktree = NULL;
1838 struct got_reflist_head refs;
1839 struct got_object_id *start_id = NULL;
1840 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1841 char *start_commit = NULL;
1842 int ch;
1843 struct tog_view *view;
1845 SIMPLEQ_INIT(&refs);
1847 #ifndef PROFILE
1848 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1849 NULL) == -1)
1850 err(1, "pledge");
1851 #endif
1853 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1854 switch (ch) {
1855 case 'c':
1856 start_commit = optarg;
1857 break;
1858 case 'r':
1859 repo_path = realpath(optarg, NULL);
1860 if (repo_path == NULL)
1861 err(1, "-r option");
1862 break;
1863 default:
1864 usage_log();
1865 /* NOTREACHED */
1869 argc -= optind;
1870 argv += optind;
1872 cwd = getcwd(NULL, 0);
1873 if (cwd == NULL) {
1874 error = got_error_prefix_errno("getcwd");
1875 goto done;
1877 error = got_worktree_open(&worktree, cwd);
1878 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1879 goto done;
1880 error = NULL;
1882 if (argc == 0) {
1883 path = strdup("");
1884 if (path == NULL) {
1885 error = got_error_prefix_errno("strdup");
1886 goto done;
1888 } else if (argc == 1) {
1889 if (worktree) {
1890 error = got_worktree_resolve_path(&path, worktree,
1891 argv[0]);
1892 if (error)
1893 goto done;
1894 } else {
1895 path = strdup(argv[0]);
1896 if (path == NULL) {
1897 error = got_error_prefix_errno("strdup");
1898 goto done;
1901 } else
1902 usage_log();
1904 repo_path = worktree ?
1905 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1906 if (repo_path == NULL) {
1907 error = got_error_prefix_errno("strdup");
1908 goto done;
1911 init_curses();
1913 error = got_repo_open(&repo, repo_path);
1914 if (error != NULL)
1915 goto done;
1917 error = apply_unveil(got_repo_get_path(repo),
1918 worktree ? got_worktree_get_root_path(worktree) : NULL);
1919 if (error)
1920 goto done;
1922 if (start_commit == NULL)
1923 error = get_head_commit_id(&start_id, repo);
1924 else
1925 error = got_object_resolve_id_str(&start_id, repo,
1926 start_commit);
1927 if (error != NULL)
1928 goto done;
1930 error = got_ref_list(&refs, repo);
1931 if (error)
1932 goto done;
1934 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1935 if (view == NULL) {
1936 error = got_error_prefix_errno("view_open");
1937 goto done;
1939 error = open_log_view(view, start_id, &refs, repo, path, 1);
1940 if (error)
1941 goto done;
1942 error = view_loop(view);
1943 done:
1944 free(repo_path);
1945 free(cwd);
1946 free(path);
1947 free(start_id);
1948 if (repo)
1949 got_repo_close(repo);
1950 if (worktree)
1951 got_worktree_close(worktree);
1952 got_ref_list_free(&refs);
1953 return error;
1956 __dead static void
1957 usage_diff(void)
1959 endwin();
1960 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1961 getprogname());
1962 exit(1);
1965 static char *
1966 parse_next_line(FILE *f, size_t *len)
1968 char *line;
1969 size_t linelen;
1970 size_t lineno;
1971 const char delim[3] = { '\0', '\0', '\0'};
1973 line = fparseln(f, &linelen, &lineno, delim, 0);
1974 if (len)
1975 *len = linelen;
1976 return line;
1979 static const struct got_error *
1980 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1981 int *last_displayed_line, int *eof, int max_lines,
1982 char * header)
1984 const struct got_error *err;
1985 int nlines = 0, nprinted = 0;
1986 char *line;
1987 size_t len;
1988 wchar_t *wline;
1989 int width;
1991 rewind(f);
1992 werase(view->window);
1994 if (header) {
1995 err = format_line(&wline, &width, header, view->ncols);
1996 if (err) {
1997 return err;
2000 if (view_needs_focus_indication(view))
2001 wstandout(view->window);
2002 waddwstr(view->window, wline);
2003 if (view_needs_focus_indication(view))
2004 wstandend(view->window);
2005 if (width < view->ncols)
2006 waddch(view->window, '\n');
2008 if (max_lines <= 1)
2009 return NULL;
2010 max_lines--;
2013 *eof = 0;
2014 while (nprinted < max_lines) {
2015 line = parse_next_line(f, &len);
2016 if (line == NULL) {
2017 *eof = 1;
2018 break;
2020 if (++nlines < *first_displayed_line) {
2021 free(line);
2022 continue;
2025 err = format_line(&wline, &width, line, view->ncols);
2026 if (err) {
2027 free(line);
2028 return err;
2030 waddwstr(view->window, wline);
2031 if (width < view->ncols)
2032 waddch(view->window, '\n');
2033 if (++nprinted == 1)
2034 *first_displayed_line = nlines;
2035 free(line);
2036 free(wline);
2037 wline = NULL;
2039 *last_displayed_line = nlines;
2041 view_vborder(view);
2043 return NULL;
2046 static char *
2047 get_datestr(time_t *time, char *datebuf)
2049 char *p, *s = ctime_r(time, datebuf);
2050 p = strchr(s, '\n');
2051 if (p)
2052 *p = '\0';
2053 return s;
2056 static const struct got_error *
2057 write_commit_info(struct got_object_id *commit_id,
2058 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2060 const struct got_error *err = NULL;
2061 char datebuf[26];
2062 struct got_commit_object *commit;
2063 char *id_str = NULL;
2064 time_t committer_time;
2065 const char *author, *committer;
2066 char *refs_str = NULL;
2068 if (refs) {
2069 err = build_refs_str(&refs_str, refs, commit_id);
2070 if (err)
2071 return err;
2074 err = got_object_open_as_commit(&commit, repo, commit_id);
2075 if (err)
2076 return err;
2078 err = got_object_id_str(&id_str, commit_id);
2079 if (err) {
2080 err = got_error_prefix_errno("got_object_id_str");
2081 goto done;
2084 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2085 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2086 err = got_error_prefix_errno("fprintf");
2087 goto done;
2089 if (fprintf(outfile, "from: %s\n",
2090 got_object_commit_get_author(commit)) < 0) {
2091 err = got_error_prefix_errno("fprintf");
2092 goto done;
2094 committer_time = got_object_commit_get_committer_time(commit);
2095 if (fprintf(outfile, "date: %s UTC\n",
2096 get_datestr(&committer_time, datebuf)) < 0) {
2097 err = got_error_prefix_errno("fprintf");
2098 goto done;
2100 author = got_object_commit_get_author(commit);
2101 committer = got_object_commit_get_committer(commit);
2102 if (strcmp(author, committer) != 0 &&
2103 fprintf(outfile, "via: %s\n", committer) < 0) {
2104 err = got_error_prefix_errno("fprintf");
2105 goto done;
2107 if (fprintf(outfile, "%s\n",
2108 got_object_commit_get_logmsg(commit)) < 0) {
2109 err = got_error_prefix_errno("fprintf");
2110 goto done;
2112 done:
2113 free(id_str);
2114 free(refs_str);
2115 got_object_commit_close(commit);
2116 return err;
2119 static const struct got_error *
2120 create_diff(struct tog_diff_view_state *s)
2122 const struct got_error *err = NULL;
2123 FILE *f = NULL;
2124 int obj_type;
2126 f = got_opentemp();
2127 if (f == NULL) {
2128 err = got_error_prefix_errno("got_opentemp");
2129 goto done;
2131 if (s->f && fclose(s->f) != 0) {
2132 err = got_error_prefix_errno("fclose");
2133 goto done;
2135 s->f = f;
2137 if (s->id1)
2138 err = got_object_get_type(&obj_type, s->repo, s->id1);
2139 else
2140 err = got_object_get_type(&obj_type, s->repo, s->id2);
2141 if (err)
2142 goto done;
2144 switch (obj_type) {
2145 case GOT_OBJ_TYPE_BLOB:
2146 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2147 s->diff_context, s->repo, f);
2148 break;
2149 case GOT_OBJ_TYPE_TREE:
2150 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2151 s->diff_context, s->repo, f);
2152 break;
2153 case GOT_OBJ_TYPE_COMMIT: {
2154 const struct got_object_id_queue *parent_ids;
2155 struct got_object_qid *pid;
2156 struct got_commit_object *commit2;
2158 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2159 if (err)
2160 break;
2161 /* Show commit info if we're diffing to a parent/root commit. */
2162 if (s->id1 == NULL)
2163 write_commit_info(s->id2, s->refs, s->repo, f);
2164 else {
2165 parent_ids = got_object_commit_get_parent_ids(commit2);
2166 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2167 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2168 write_commit_info(s->id2, s->refs,
2169 s->repo, f);
2170 break;
2174 got_object_commit_close(commit2);
2176 err = got_diff_objects_as_commits(s->id1, s->id2,
2177 s->diff_context, s->repo, f);
2178 break;
2180 default:
2181 err = got_error(GOT_ERR_OBJ_TYPE);
2182 break;
2184 done:
2185 if (f && fflush(f) != 0 && err == NULL)
2186 err = got_error_prefix_errno("fflush");
2187 return err;
2190 static void
2191 diff_view_indicate_progress(struct tog_view *view)
2193 werase(view->window);
2194 waddstr(view->window, "diffing...");
2195 update_panels();
2196 doupdate();
2199 static const struct got_error *
2200 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2201 struct got_object_id *id2, struct tog_view *log_view,
2202 struct got_reflist_head *refs, struct got_repository *repo)
2204 const struct got_error *err;
2206 if (id1 != NULL && id2 != NULL) {
2207 int type1, type2;
2208 err = got_object_get_type(&type1, repo, id1);
2209 if (err)
2210 return err;
2211 err = got_object_get_type(&type2, repo, id2);
2212 if (err)
2213 return err;
2215 if (type1 != type2)
2216 return got_error(GOT_ERR_OBJ_TYPE);
2219 if (id1) {
2220 view->state.diff.id1 = got_object_id_dup(id1);
2221 if (view->state.diff.id1 == NULL)
2222 return got_error_prefix_errno("got_object_id_dup");
2223 } else
2224 view->state.diff.id1 = NULL;
2226 view->state.diff.id2 = got_object_id_dup(id2);
2227 if (view->state.diff.id2 == NULL) {
2228 free(view->state.diff.id1);
2229 view->state.diff.id1 = NULL;
2230 return got_error_prefix_errno("got_object_id_dup");
2232 view->state.diff.f = NULL;
2233 view->state.diff.first_displayed_line = 1;
2234 view->state.diff.last_displayed_line = view->nlines;
2235 view->state.diff.diff_context = 3;
2236 view->state.diff.log_view = log_view;
2237 view->state.diff.repo = repo;
2238 view->state.diff.refs = refs;
2240 if (log_view && view_is_splitscreen(view))
2241 show_log_view(log_view); /* draw vborder */
2242 diff_view_indicate_progress(view);
2244 err = create_diff(&view->state.diff);
2245 if (err) {
2246 free(view->state.diff.id1);
2247 view->state.diff.id1 = NULL;
2248 free(view->state.diff.id2);
2249 view->state.diff.id2 = NULL;
2250 return err;
2253 view->show = show_diff_view;
2254 view->input = input_diff_view;
2255 view->close = close_diff_view;
2257 return NULL;
2260 static const struct got_error *
2261 close_diff_view(struct tog_view *view)
2263 const struct got_error *err = NULL;
2265 free(view->state.diff.id1);
2266 view->state.diff.id1 = NULL;
2267 free(view->state.diff.id2);
2268 view->state.diff.id2 = NULL;
2269 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2270 err = got_error_prefix_errno("fclose");
2271 return err;
2274 static const struct got_error *
2275 show_diff_view(struct tog_view *view)
2277 const struct got_error *err;
2278 struct tog_diff_view_state *s = &view->state.diff;
2279 char *id_str1 = NULL, *id_str2, *header;
2281 if (s->id1) {
2282 err = got_object_id_str(&id_str1, s->id1);
2283 if (err)
2284 return err;
2286 err = got_object_id_str(&id_str2, s->id2);
2287 if (err)
2288 return err;
2290 if (asprintf(&header, "diff %s %s",
2291 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2292 err = got_error_prefix_errno("asprintf");
2293 free(id_str1);
2294 free(id_str2);
2295 return err;
2297 free(id_str1);
2298 free(id_str2);
2300 return draw_file(view, s->f, &s->first_displayed_line,
2301 &s->last_displayed_line, &s->eof, view->nlines,
2302 header);
2305 static const struct got_error *
2306 set_selected_commit(struct tog_diff_view_state *s,
2307 struct commit_queue_entry *entry)
2309 const struct got_error *err;
2310 const struct got_object_id_queue *parent_ids;
2311 struct got_commit_object *selected_commit;
2312 struct got_object_qid *pid;
2314 free(s->id2);
2315 s->id2 = got_object_id_dup(entry->id);
2316 if (s->id2 == NULL)
2317 return got_error_prefix_errno("got_object_id_dup");
2319 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2320 if (err)
2321 return err;
2322 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2323 free(s->id1);
2324 pid = SIMPLEQ_FIRST(parent_ids);
2325 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2326 got_object_commit_close(selected_commit);
2327 return NULL;
2330 static const struct got_error *
2331 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2332 struct tog_view **focus_view, struct tog_view *view, int ch)
2334 const struct got_error *err = NULL;
2335 struct tog_diff_view_state *s = &view->state.diff;
2336 struct tog_log_view_state *ls;
2337 struct commit_queue_entry *entry;
2338 int i;
2340 switch (ch) {
2341 case 'k':
2342 case KEY_UP:
2343 if (s->first_displayed_line > 1)
2344 s->first_displayed_line--;
2345 else
2346 view_flash(view);
2347 break;
2348 case KEY_PPAGE:
2349 if (s->first_displayed_line == 1) {
2350 view_flash(view);
2351 break;
2353 i = 0;
2354 while (i++ < view->nlines - 1 &&
2355 s->first_displayed_line > 1)
2356 s->first_displayed_line--;
2357 break;
2358 case 'j':
2359 case KEY_DOWN:
2360 if (!s->eof)
2361 s->first_displayed_line++;
2362 else
2363 view_flash(view);
2364 break;
2365 case KEY_NPAGE:
2366 case ' ':
2367 if (s->eof) {
2368 view_flash(view);
2369 break;
2371 i = 0;
2372 while (!s->eof && i++ < view->nlines - 1) {
2373 char *line;
2374 line = parse_next_line(s->f, NULL);
2375 s->first_displayed_line++;
2376 if (line == NULL)
2377 break;
2379 break;
2380 case '[':
2381 if (s->diff_context > 0) {
2382 s->diff_context--;
2383 diff_view_indicate_progress(view);
2384 err = create_diff(s);
2386 break;
2387 case ']':
2388 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2389 s->diff_context++;
2390 diff_view_indicate_progress(view);
2391 err = create_diff(s);
2393 break;
2394 case '<':
2395 case ',':
2396 if (s->log_view == NULL)
2397 break;
2398 ls = &s->log_view->state.log;
2399 entry = TAILQ_PREV(ls->selected_entry,
2400 commit_queue_head, entry);
2401 if (entry == NULL)
2402 break;
2404 err = input_log_view(NULL, NULL, NULL, s->log_view,
2405 KEY_UP);
2406 if (err)
2407 break;
2409 err = set_selected_commit(s, entry);
2410 if (err)
2411 break;
2413 s->first_displayed_line = 1;
2414 s->last_displayed_line = view->nlines;
2416 diff_view_indicate_progress(view);
2417 err = create_diff(s);
2418 break;
2419 case '>':
2420 case '.':
2421 if (s->log_view == NULL)
2422 break;
2423 ls = &s->log_view->state.log;
2425 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2426 ls->thread_args.commits_needed++;
2428 /* Display "loading..." in log view. */
2429 show_log_view(s->log_view);
2430 update_panels();
2431 doupdate();
2433 err = trigger_log_thread(1 /* load_all */,
2434 &ls->thread_args.commits_needed,
2435 &ls->thread_args.log_complete,
2436 &ls->thread_args.need_commits);
2437 if (err)
2438 break;
2440 err = input_log_view(NULL, NULL, NULL, s->log_view,
2441 KEY_DOWN);
2442 if (err)
2443 break;
2445 entry = TAILQ_NEXT(ls->selected_entry, entry);
2446 if (entry == NULL)
2447 break;
2449 err = set_selected_commit(s, entry);
2450 if (err)
2451 break;
2453 s->first_displayed_line = 1;
2454 s->last_displayed_line = view->nlines;
2456 diff_view_indicate_progress(view);
2457 err = create_diff(s);
2458 break;
2459 default:
2460 break;
2463 return err;
2466 static const struct got_error *
2467 cmd_diff(int argc, char *argv[])
2469 const struct got_error *error = NULL;
2470 struct got_repository *repo = NULL;
2471 struct got_reflist_head refs;
2472 struct got_object_id *id1 = NULL, *id2 = NULL;
2473 char *repo_path = NULL;
2474 char *id_str1 = NULL, *id_str2 = NULL;
2475 int ch;
2476 struct tog_view *view;
2478 SIMPLEQ_INIT(&refs);
2480 #ifndef PROFILE
2481 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2482 NULL) == -1)
2483 err(1, "pledge");
2484 #endif
2486 while ((ch = getopt(argc, argv, "")) != -1) {
2487 switch (ch) {
2488 default:
2489 usage_diff();
2490 /* NOTREACHED */
2494 argc -= optind;
2495 argv += optind;
2497 if (argc == 0) {
2498 usage_diff(); /* TODO show local worktree changes */
2499 } else if (argc == 2) {
2500 repo_path = getcwd(NULL, 0);
2501 if (repo_path == NULL)
2502 return got_error_prefix_errno("getcwd");
2503 id_str1 = argv[0];
2504 id_str2 = argv[1];
2505 } else if (argc == 3) {
2506 repo_path = realpath(argv[0], NULL);
2507 if (repo_path == NULL)
2508 return got_error_prefix_errno2("realpath", argv[0]);
2509 id_str1 = argv[1];
2510 id_str2 = argv[2];
2511 } else
2512 usage_diff();
2514 init_curses();
2516 error = got_repo_open(&repo, repo_path);
2517 if (error)
2518 goto done;
2520 error = apply_unveil(got_repo_get_path(repo), NULL);
2521 if (error)
2522 goto done;
2524 error = got_object_resolve_id_str(&id1, repo, id_str1);
2525 if (error)
2526 goto done;
2528 error = got_object_resolve_id_str(&id2, repo, id_str2);
2529 if (error)
2530 goto done;
2532 error = got_ref_list(&refs, repo);
2533 if (error)
2534 goto done;
2536 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2537 if (view == NULL) {
2538 error = got_error_prefix_errno("view_open");
2539 goto done;
2541 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2542 if (error)
2543 goto done;
2544 error = view_loop(view);
2545 done:
2546 free(repo_path);
2547 got_repo_close(repo);
2548 got_ref_list_free(&refs);
2549 return error;
2552 __dead static void
2553 usage_blame(void)
2555 endwin();
2556 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2557 getprogname());
2558 exit(1);
2561 struct tog_blame_line {
2562 int annotated;
2563 struct got_object_id *id;
2566 static const struct got_error *
2567 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2568 const char *path, struct tog_blame_line *lines, int nlines,
2569 int blame_complete, int selected_line, int *first_displayed_line,
2570 int *last_displayed_line, int *eof, int max_lines)
2572 const struct got_error *err;
2573 int lineno = 0, nprinted = 0;
2574 char *line;
2575 size_t len;
2576 wchar_t *wline;
2577 int width, wlimit;
2578 struct tog_blame_line *blame_line;
2579 struct got_object_id *prev_id = NULL;
2580 char *id_str;
2582 err = got_object_id_str(&id_str, id);
2583 if (err)
2584 return err;
2586 rewind(f);
2587 werase(view->window);
2589 if (asprintf(&line, "commit %s", id_str) == -1) {
2590 err = got_error_prefix_errno("asprintf");
2591 free(id_str);
2592 return err;
2595 err = format_line(&wline, &width, line, view->ncols);
2596 free(line);
2597 line = NULL;
2598 if (view_needs_focus_indication(view))
2599 wstandout(view->window);
2600 waddwstr(view->window, wline);
2601 if (view_needs_focus_indication(view))
2602 wstandend(view->window);
2603 free(wline);
2604 wline = NULL;
2605 if (width < view->ncols)
2606 waddch(view->window, '\n');
2608 if (asprintf(&line, "[%d/%d] %s%s",
2609 *first_displayed_line - 1 + selected_line, nlines,
2610 blame_complete ? "" : "annotating... ", path) == -1) {
2611 free(id_str);
2612 return got_error_prefix_errno("asprintf");
2614 free(id_str);
2615 err = format_line(&wline, &width, line, view->ncols);
2616 free(line);
2617 line = NULL;
2618 if (err)
2619 return err;
2620 waddwstr(view->window, wline);
2621 free(wline);
2622 wline = NULL;
2623 if (width < view->ncols)
2624 waddch(view->window, '\n');
2626 *eof = 0;
2627 while (nprinted < max_lines - 2) {
2628 line = parse_next_line(f, &len);
2629 if (line == NULL) {
2630 *eof = 1;
2631 break;
2633 if (++lineno < *first_displayed_line) {
2634 free(line);
2635 continue;
2638 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2639 err = format_line(&wline, &width, line, wlimit);
2640 if (err) {
2641 free(line);
2642 return err;
2645 if (view->focussed && nprinted == selected_line - 1)
2646 wstandout(view->window);
2648 blame_line = &lines[lineno - 1];
2649 if (blame_line->annotated && prev_id &&
2650 got_object_id_cmp(prev_id, blame_line->id) == 0)
2651 waddstr(view->window, " ");
2652 else if (blame_line->annotated) {
2653 char *id_str;
2654 err = got_object_id_str(&id_str, blame_line->id);
2655 if (err) {
2656 free(line);
2657 free(wline);
2658 return err;
2660 wprintw(view->window, "%.8s ", id_str);
2661 free(id_str);
2662 prev_id = blame_line->id;
2663 } else {
2664 waddstr(view->window, "........ ");
2665 prev_id = NULL;
2668 waddwstr(view->window, wline);
2669 while (width < wlimit) {
2670 waddch(view->window, ' ');
2671 width++;
2673 if (view->focussed && nprinted == selected_line - 1)
2674 wstandend(view->window);
2675 if (++nprinted == 1)
2676 *first_displayed_line = lineno;
2677 free(line);
2678 free(wline);
2679 wline = NULL;
2681 *last_displayed_line = lineno;
2683 view_vborder(view);
2685 return NULL;
2688 static const struct got_error *
2689 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2691 const struct got_error *err = NULL;
2692 struct tog_blame_cb_args *a = arg;
2693 struct tog_blame_line *line;
2694 int errcode;
2696 if (nlines != a->nlines ||
2697 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2698 return got_error(GOT_ERR_RANGE);
2700 errcode = pthread_mutex_lock(&tog_mutex);
2701 if (errcode)
2702 return got_error_set_errno(errcode, "pthread_mutex_lock");
2704 if (*a->quit) { /* user has quit the blame view */
2705 err = got_error(GOT_ERR_ITER_COMPLETED);
2706 goto done;
2709 if (lineno == -1)
2710 goto done; /* no change in this commit */
2712 line = &a->lines[lineno - 1];
2713 if (line->annotated)
2714 goto done;
2716 line->id = got_object_id_dup(id);
2717 if (line->id == NULL) {
2718 err = got_error_prefix_errno("got_object_id_dup");
2719 goto done;
2721 line->annotated = 1;
2722 done:
2723 errcode = pthread_mutex_unlock(&tog_mutex);
2724 if (errcode)
2725 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2726 return err;
2729 static void *
2730 blame_thread(void *arg)
2732 const struct got_error *err;
2733 struct tog_blame_thread_args *ta = arg;
2734 struct tog_blame_cb_args *a = ta->cb_args;
2735 int errcode;
2737 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2738 blame_cb, ta->cb_args);
2740 errcode = pthread_mutex_lock(&tog_mutex);
2741 if (errcode)
2742 return (void *)got_error_set_errno(errcode,
2743 "pthread_mutex_lock");
2745 got_repo_close(ta->repo);
2746 ta->repo = NULL;
2747 *ta->complete = 1;
2749 errcode = pthread_mutex_unlock(&tog_mutex);
2750 if (errcode && err == NULL)
2751 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2753 return (void *)err;
2756 static struct got_object_id *
2757 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2758 int selected_line)
2760 struct tog_blame_line *line;
2762 line = &lines[first_displayed_line - 1 + selected_line - 1];
2763 if (!line->annotated)
2764 return NULL;
2766 return line->id;
2769 static const struct got_error *
2770 stop_blame(struct tog_blame *blame)
2772 const struct got_error *err = NULL;
2773 int i;
2775 if (blame->thread) {
2776 int errcode;
2777 errcode = pthread_mutex_unlock(&tog_mutex);
2778 if (errcode)
2779 return got_error_set_errno(errcode,
2780 "pthread_mutex_unlock");
2781 errcode = pthread_join(blame->thread, (void **)&err);
2782 if (errcode)
2783 return got_error_set_errno(errcode, "pthread_join");
2784 errcode = pthread_mutex_lock(&tog_mutex);
2785 if (errcode)
2786 return got_error_set_errno(errcode,
2787 "pthread_mutex_lock");
2788 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2789 err = NULL;
2790 blame->thread = NULL;
2792 if (blame->thread_args.repo) {
2793 got_repo_close(blame->thread_args.repo);
2794 blame->thread_args.repo = NULL;
2796 if (blame->f) {
2797 if (fclose(blame->f) != 0 && err == NULL)
2798 err = got_error_prefix_errno("fclose");
2799 blame->f = NULL;
2801 if (blame->lines) {
2802 for (i = 0; i < blame->nlines; i++)
2803 free(blame->lines[i].id);
2804 free(blame->lines);
2805 blame->lines = NULL;
2807 free(blame->cb_args.commit_id);
2808 blame->cb_args.commit_id = NULL;
2810 return err;
2813 static const struct got_error *
2814 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2815 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2816 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2817 struct got_repository *repo)
2819 const struct got_error *err = NULL;
2820 struct got_blob_object *blob = NULL;
2821 struct got_repository *thread_repo = NULL;
2822 struct got_object_id *obj_id = NULL;
2823 int obj_type;
2825 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2826 if (err)
2827 return err;
2828 if (obj_id == NULL)
2829 return got_error(GOT_ERR_NO_OBJ);
2831 err = got_object_get_type(&obj_type, repo, obj_id);
2832 if (err)
2833 goto done;
2835 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2836 err = got_error(GOT_ERR_OBJ_TYPE);
2837 goto done;
2840 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2841 if (err)
2842 goto done;
2843 blame->f = got_opentemp();
2844 if (blame->f == NULL) {
2845 err = got_error_prefix_errno("got_opentemp");
2846 goto done;
2848 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2849 blame->f, blob);
2850 if (err)
2851 goto done;
2853 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2854 if (blame->lines == NULL) {
2855 err = got_error_prefix_errno("calloc");
2856 goto done;
2859 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2860 if (err)
2861 goto done;
2863 blame->cb_args.view = view;
2864 blame->cb_args.lines = blame->lines;
2865 blame->cb_args.nlines = blame->nlines;
2866 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2867 if (blame->cb_args.commit_id == NULL) {
2868 err = got_error_prefix_errno("got_object_id_dup");
2869 goto done;
2871 blame->cb_args.quit = done;
2873 blame->thread_args.path = path;
2874 blame->thread_args.repo = thread_repo;
2875 blame->thread_args.cb_args = &blame->cb_args;
2876 blame->thread_args.complete = blame_complete;
2877 *blame_complete = 0;
2879 done:
2880 if (blob)
2881 got_object_blob_close(blob);
2882 free(obj_id);
2883 if (err)
2884 stop_blame(blame);
2885 return err;
2888 static const struct got_error *
2889 open_blame_view(struct tog_view *view, char *path,
2890 struct got_object_id *commit_id, struct got_reflist_head *refs,
2891 struct got_repository *repo)
2893 const struct got_error *err = NULL;
2894 struct tog_blame_view_state *s = &view->state.blame;
2896 SIMPLEQ_INIT(&s->blamed_commits);
2898 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2899 if (err)
2900 return err;
2902 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2903 s->first_displayed_line = 1;
2904 s->last_displayed_line = view->nlines;
2905 s->selected_line = 1;
2906 s->blame_complete = 0;
2907 s->path = path;
2908 if (s->path == NULL)
2909 return got_error_prefix_errno("open_blame_view");
2910 s->repo = repo;
2911 s->refs = refs;
2912 s->commit_id = commit_id;
2913 memset(&s->blame, 0, sizeof(s->blame));
2915 view->show = show_blame_view;
2916 view->input = input_blame_view;
2917 view->close = close_blame_view;
2919 return run_blame(&s->blame, view, &s->blame_complete,
2920 &s->first_displayed_line, &s->last_displayed_line,
2921 &s->selected_line, &s->done, &s->eof, s->path,
2922 s->blamed_commit->id, s->repo);
2925 static const struct got_error *
2926 close_blame_view(struct tog_view *view)
2928 const struct got_error *err = NULL;
2929 struct tog_blame_view_state *s = &view->state.blame;
2931 if (s->blame.thread)
2932 err = stop_blame(&s->blame);
2934 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2935 struct got_object_qid *blamed_commit;
2936 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2937 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2938 got_object_qid_free(blamed_commit);
2941 free(s->path);
2943 return err;
2946 static const struct got_error *
2947 show_blame_view(struct tog_view *view)
2949 const struct got_error *err = NULL;
2950 struct tog_blame_view_state *s = &view->state.blame;
2951 int errcode;
2953 if (s->blame.thread == NULL) {
2954 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2955 &s->blame.thread_args);
2956 if (errcode)
2957 return got_error_set_errno(errcode, "pthread_create");
2960 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2961 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2962 s->selected_line, &s->first_displayed_line,
2963 &s->last_displayed_line, &s->eof, view->nlines);
2965 view_vborder(view);
2966 return err;
2969 static const struct got_error *
2970 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2971 struct tog_view **focus_view, struct tog_view *view, int ch)
2973 const struct got_error *err = NULL, *thread_err = NULL;
2974 struct tog_view *diff_view;
2975 struct tog_blame_view_state *s = &view->state.blame;
2976 int begin_x = 0;
2978 switch (ch) {
2979 case 'q':
2980 s->done = 1;
2981 break;
2982 case 'k':
2983 case KEY_UP:
2984 if (s->selected_line > 1)
2985 s->selected_line--;
2986 else if (s->selected_line == 1 &&
2987 s->first_displayed_line > 1)
2988 s->first_displayed_line--;
2989 else
2990 view_flash(view);
2991 break;
2992 case KEY_PPAGE:
2993 if (s->first_displayed_line == 1) {
2994 if (s->selected_line == 1) {
2995 view_flash(view);
2996 break;
2998 s->selected_line = 1;
2999 break;
3001 if (s->first_displayed_line > view->nlines - 2)
3002 s->first_displayed_line -=
3003 (view->nlines - 2);
3004 else
3005 s->first_displayed_line = 1;
3006 break;
3007 case 'j':
3008 case KEY_DOWN:
3009 if (s->selected_line < view->nlines - 2 &&
3010 s->first_displayed_line +
3011 s->selected_line <= s->blame.nlines)
3012 s->selected_line++;
3013 else if (s->last_displayed_line <
3014 s->blame.nlines)
3015 s->first_displayed_line++;
3016 else
3017 view_flash(view);
3018 break;
3019 case 'b':
3020 case 'p': {
3021 struct got_object_id *id = NULL;
3022 id = get_selected_commit_id(s->blame.lines,
3023 s->first_displayed_line, s->selected_line);
3024 if (id == NULL)
3025 break;
3026 if (ch == 'p') {
3027 struct got_commit_object *commit;
3028 struct got_object_qid *pid;
3029 struct got_object_id *blob_id = NULL;
3030 int obj_type;
3031 err = got_object_open_as_commit(&commit,
3032 s->repo, id);
3033 if (err)
3034 break;
3035 pid = SIMPLEQ_FIRST(
3036 got_object_commit_get_parent_ids(commit));
3037 if (pid == NULL) {
3038 got_object_commit_close(commit);
3039 break;
3041 /* Check if path history ends here. */
3042 err = got_object_id_by_path(&blob_id, s->repo,
3043 pid->id, s->path);
3044 if (err) {
3045 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3046 err = NULL;
3047 got_object_commit_close(commit);
3048 break;
3050 err = got_object_get_type(&obj_type, s->repo,
3051 blob_id);
3052 free(blob_id);
3053 /* Can't blame non-blob type objects. */
3054 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3055 got_object_commit_close(commit);
3056 break;
3058 err = got_object_qid_alloc(&s->blamed_commit,
3059 pid->id);
3060 got_object_commit_close(commit);
3061 } else {
3062 if (got_object_id_cmp(id,
3063 s->blamed_commit->id) == 0)
3064 break;
3065 err = got_object_qid_alloc(&s->blamed_commit,
3066 id);
3068 if (err)
3069 break;
3070 s->done = 1;
3071 thread_err = stop_blame(&s->blame);
3072 s->done = 0;
3073 if (thread_err)
3074 break;
3075 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3076 s->blamed_commit, entry);
3077 err = run_blame(&s->blame, view, &s->blame_complete,
3078 &s->first_displayed_line, &s->last_displayed_line,
3079 &s->selected_line, &s->done, &s->eof,
3080 s->path, s->blamed_commit->id, s->repo);
3081 if (err)
3082 break;
3083 break;
3085 case 'B': {
3086 struct got_object_qid *first;
3087 first = SIMPLEQ_FIRST(&s->blamed_commits);
3088 if (!got_object_id_cmp(first->id, s->commit_id))
3089 break;
3090 s->done = 1;
3091 thread_err = stop_blame(&s->blame);
3092 s->done = 0;
3093 if (thread_err)
3094 break;
3095 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3096 got_object_qid_free(s->blamed_commit);
3097 s->blamed_commit =
3098 SIMPLEQ_FIRST(&s->blamed_commits);
3099 err = run_blame(&s->blame, view, &s->blame_complete,
3100 &s->first_displayed_line, &s->last_displayed_line,
3101 &s->selected_line, &s->done, &s->eof, s->path,
3102 s->blamed_commit->id, s->repo);
3103 if (err)
3104 break;
3105 break;
3107 case KEY_ENTER:
3108 case '\r': {
3109 struct got_object_id *id = NULL;
3110 struct got_object_qid *pid;
3111 struct got_commit_object *commit = NULL;
3112 id = get_selected_commit_id(s->blame.lines,
3113 s->first_displayed_line, s->selected_line);
3114 if (id == NULL)
3115 break;
3116 err = got_object_open_as_commit(&commit, s->repo, id);
3117 if (err)
3118 break;
3119 pid = SIMPLEQ_FIRST(
3120 got_object_commit_get_parent_ids(commit));
3121 if (view_is_parent_view(view))
3122 begin_x = view_split_begin_x(view->begin_x);
3123 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3124 if (diff_view == NULL) {
3125 got_object_commit_close(commit);
3126 err = got_error_prefix_errno("view_open");
3127 break;
3129 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3130 id, NULL, s->refs, s->repo);
3131 got_object_commit_close(commit);
3132 if (err) {
3133 view_close(diff_view);
3134 break;
3136 if (view_is_parent_view(view)) {
3137 err = view_close_child(view);
3138 if (err)
3139 break;
3140 err = view_set_child(view, diff_view);
3141 if (err) {
3142 view_close(diff_view);
3143 break;
3145 *focus_view = diff_view;
3146 view->child_focussed = 1;
3147 } else
3148 *new_view = diff_view;
3149 if (err)
3150 break;
3151 break;
3153 case KEY_NPAGE:
3154 case ' ':
3155 if (s->last_displayed_line >= s->blame.nlines &&
3156 s->selected_line >= MIN(s->blame.nlines,
3157 view->nlines - 2)) {
3158 view_flash(view);
3159 break;
3161 if (s->last_displayed_line >= s->blame.nlines &&
3162 s->selected_line < view->nlines - 2) {
3163 s->selected_line = MIN(s->blame.nlines,
3164 view->nlines - 2);
3165 break;
3167 if (s->last_displayed_line + view->nlines - 2
3168 <= s->blame.nlines)
3169 s->first_displayed_line +=
3170 view->nlines - 2;
3171 else
3172 s->first_displayed_line =
3173 s->blame.nlines -
3174 (view->nlines - 3);
3175 break;
3176 case KEY_RESIZE:
3177 if (s->selected_line > view->nlines - 2) {
3178 s->selected_line = MIN(s->blame.nlines,
3179 view->nlines - 2);
3181 break;
3182 default:
3183 break;
3185 return thread_err ? thread_err : err;
3188 static const struct got_error *
3189 cmd_blame(int argc, char *argv[])
3191 const struct got_error *error;
3192 struct got_repository *repo = NULL;
3193 struct got_reflist_head refs;
3194 struct got_worktree *worktree = NULL;
3195 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3196 struct got_object_id *commit_id = NULL;
3197 char *commit_id_str = NULL;
3198 int ch;
3199 struct tog_view *view;
3201 SIMPLEQ_INIT(&refs);
3203 #ifndef PROFILE
3204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3205 NULL) == -1)
3206 err(1, "pledge");
3207 #endif
3209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3210 switch (ch) {
3211 case 'c':
3212 commit_id_str = optarg;
3213 break;
3214 case 'r':
3215 repo_path = realpath(optarg, NULL);
3216 if (repo_path == NULL)
3217 err(1, "-r option");
3218 break;
3219 default:
3220 usage_blame();
3221 /* NOTREACHED */
3225 argc -= optind;
3226 argv += optind;
3228 if (argc == 1)
3229 path = argv[0];
3230 else
3231 usage_blame();
3233 cwd = getcwd(NULL, 0);
3234 if (cwd == NULL) {
3235 error = got_error_prefix_errno("getcwd");
3236 goto done;
3238 if (repo_path == NULL) {
3239 error = got_worktree_open(&worktree, cwd);
3240 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3241 goto done;
3242 else
3243 error = NULL;
3244 if (worktree) {
3245 repo_path =
3246 strdup(got_worktree_get_repo_path(worktree));
3247 if (repo_path == NULL)
3248 error = got_error_prefix_errno("strdup");
3249 if (error)
3250 goto done;
3251 } else {
3252 repo_path = strdup(cwd);
3253 if (repo_path == NULL) {
3254 error = got_error_prefix_errno("strdup");
3255 goto done;
3260 init_curses();
3262 error = got_repo_open(&repo, repo_path);
3263 if (error != NULL)
3264 goto done;
3266 error = apply_unveil(got_repo_get_path(repo), NULL);
3267 if (error)
3268 goto done;
3270 if (worktree) {
3271 const char *prefix = got_worktree_get_path_prefix(worktree);
3272 char *p, *worktree_subdir = cwd +
3273 strlen(got_worktree_get_root_path(worktree));
3274 if (asprintf(&p, "%s%s%s%s%s",
3275 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3276 worktree_subdir, worktree_subdir[0] ? "/" : "",
3277 path) == -1) {
3278 error = got_error_prefix_errno("asprintf");
3279 goto done;
3281 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3282 free(p);
3283 } else {
3284 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3286 if (error)
3287 goto done;
3289 if (commit_id_str == NULL) {
3290 struct got_reference *head_ref;
3291 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3292 if (error != NULL)
3293 goto done;
3294 error = got_ref_resolve(&commit_id, repo, head_ref);
3295 got_ref_close(head_ref);
3296 } else {
3297 error = got_object_resolve_id_str(&commit_id, repo,
3298 commit_id_str);
3300 if (error != NULL)
3301 goto done;
3303 error = got_ref_list(&refs, repo);
3304 if (error)
3305 goto done;
3307 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3308 if (view == NULL) {
3309 error = got_error_prefix_errno("view_open");
3310 goto done;
3312 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3313 if (error)
3314 goto done;
3315 error = view_loop(view);
3316 done:
3317 free(repo_path);
3318 free(cwd);
3319 free(commit_id);
3320 if (worktree)
3321 got_worktree_close(worktree);
3322 if (repo)
3323 got_repo_close(repo);
3324 got_ref_list_free(&refs);
3325 return error;
3328 static const struct got_error *
3329 draw_tree_entries(struct tog_view *view,
3330 struct got_tree_entry **first_displayed_entry,
3331 struct got_tree_entry **last_displayed_entry,
3332 struct got_tree_entry **selected_entry, int *ndisplayed,
3333 const char *label, int show_ids, const char *parent_path,
3334 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3336 const struct got_error *err = NULL;
3337 struct got_tree_entry *te;
3338 wchar_t *wline;
3339 int width, n;
3341 *ndisplayed = 0;
3343 werase(view->window);
3345 if (limit == 0)
3346 return NULL;
3348 err = format_line(&wline, &width, label, view->ncols);
3349 if (err)
3350 return err;
3351 if (view_needs_focus_indication(view))
3352 wstandout(view->window);
3353 waddwstr(view->window, wline);
3354 if (view_needs_focus_indication(view))
3355 wstandend(view->window);
3356 free(wline);
3357 wline = NULL;
3358 if (width < view->ncols)
3359 waddch(view->window, '\n');
3360 if (--limit <= 0)
3361 return NULL;
3362 err = format_line(&wline, &width, parent_path, view->ncols);
3363 if (err)
3364 return err;
3365 waddwstr(view->window, wline);
3366 free(wline);
3367 wline = NULL;
3368 if (width < view->ncols)
3369 waddch(view->window, '\n');
3370 if (--limit <= 0)
3371 return NULL;
3372 waddch(view->window, '\n');
3373 if (--limit <= 0)
3374 return NULL;
3376 te = SIMPLEQ_FIRST(&entries->head);
3377 if (*first_displayed_entry == NULL) {
3378 if (selected == 0) {
3379 if (view->focussed)
3380 wstandout(view->window);
3381 *selected_entry = NULL;
3383 waddstr(view->window, " ..\n"); /* parent directory */
3384 if (selected == 0 && view->focussed)
3385 wstandend(view->window);
3386 (*ndisplayed)++;
3387 if (--limit <= 0)
3388 return NULL;
3389 n = 1;
3390 } else {
3391 n = 0;
3392 while (te != *first_displayed_entry)
3393 te = SIMPLEQ_NEXT(te, entry);
3396 while (te) {
3397 char *line = NULL, *id_str = NULL;
3399 if (show_ids) {
3400 err = got_object_id_str(&id_str, te->id);
3401 if (err)
3402 return got_error_prefix_errno(
3403 "got_object_id_str");
3405 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3406 te->name, S_ISDIR(te->mode) ? "/" :
3407 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3408 free(id_str);
3409 return got_error_prefix_errno("asprintf");
3411 free(id_str);
3412 err = format_line(&wline, &width, line, view->ncols);
3413 if (err) {
3414 free(line);
3415 break;
3417 if (n == selected) {
3418 if (view->focussed)
3419 wstandout(view->window);
3420 *selected_entry = te;
3422 waddwstr(view->window, wline);
3423 if (width < view->ncols)
3424 waddch(view->window, '\n');
3425 if (n == selected && view->focussed)
3426 wstandend(view->window);
3427 free(line);
3428 free(wline);
3429 wline = NULL;
3430 n++;
3431 (*ndisplayed)++;
3432 *last_displayed_entry = te;
3433 if (--limit <= 0)
3434 break;
3435 te = SIMPLEQ_NEXT(te, entry);
3438 return err;
3441 static void
3442 tree_scroll_up(struct tog_view *view,
3443 struct got_tree_entry **first_displayed_entry, int maxscroll,
3444 const struct got_tree_entries *entries, int isroot)
3446 struct got_tree_entry *te, *prev;
3447 int i;
3449 if (*first_displayed_entry == NULL) {
3450 view_flash(view);
3451 return;
3454 te = SIMPLEQ_FIRST(&entries->head);
3455 if (*first_displayed_entry == te) {
3456 view_flash(view);
3457 if (!isroot)
3458 *first_displayed_entry = NULL;
3459 return;
3462 /* XXX this is stupid... switch to TAILQ? */
3463 for (i = 0; i < maxscroll; i++) {
3464 while (te != *first_displayed_entry) {
3465 prev = te;
3466 te = SIMPLEQ_NEXT(te, entry);
3468 *first_displayed_entry = prev;
3469 te = SIMPLEQ_FIRST(&entries->head);
3471 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3472 *first_displayed_entry = NULL;
3475 static int
3476 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3477 struct got_tree_entry *last_displayed_entry,
3478 const struct got_tree_entries *entries)
3480 struct got_tree_entry *next, *last;
3481 int n = 0;
3483 if (*first_displayed_entry)
3484 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3485 else
3486 next = SIMPLEQ_FIRST(&entries->head);
3487 last = last_displayed_entry;
3488 while (next && last && n++ < maxscroll) {
3489 last = SIMPLEQ_NEXT(last, entry);
3490 if (last) {
3491 *first_displayed_entry = next;
3492 next = SIMPLEQ_NEXT(next, entry);
3495 return n;
3498 static const struct got_error *
3499 tree_entry_path(char **path, struct tog_parent_trees *parents,
3500 struct got_tree_entry *te)
3502 const struct got_error *err = NULL;
3503 struct tog_parent_tree *pt;
3504 size_t len = 2; /* for leading slash and NUL */
3506 TAILQ_FOREACH(pt, parents, entry)
3507 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3508 if (te)
3509 len += strlen(te->name);
3511 *path = calloc(1, len);
3512 if (path == NULL)
3513 return got_error_prefix_errno("calloc");
3515 (*path)[0] = '/';
3516 pt = TAILQ_LAST(parents, tog_parent_trees);
3517 while (pt) {
3518 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3519 err = got_error(GOT_ERR_NO_SPACE);
3520 goto done;
3522 if (strlcat(*path, "/", len) >= len) {
3523 err = got_error(GOT_ERR_NO_SPACE);
3524 goto done;
3526 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3528 if (te) {
3529 if (strlcat(*path, te->name, len) >= len) {
3530 err = got_error(GOT_ERR_NO_SPACE);
3531 goto done;
3534 done:
3535 if (err) {
3536 free(*path);
3537 *path = NULL;
3539 return err;
3542 static const struct got_error *
3543 blame_tree_entry(struct tog_view **new_view, int begin_x,
3544 struct got_tree_entry *te, struct tog_parent_trees *parents,
3545 struct got_object_id *commit_id, struct got_reflist_head *refs,
3546 struct got_repository *repo)
3548 const struct got_error *err = NULL;
3549 char *path;
3550 struct tog_view *blame_view;
3552 err = tree_entry_path(&path, parents, te);
3553 if (err)
3554 return err;
3556 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3557 if (blame_view == NULL)
3558 return got_error_prefix_errno("view_open");
3560 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3561 if (err) {
3562 view_close(blame_view);
3563 free(path);
3564 } else
3565 *new_view = blame_view;
3566 return err;
3569 static const struct got_error *
3570 log_tree_entry(struct tog_view **new_view, int begin_x,
3571 struct got_tree_entry *te, struct tog_parent_trees *parents,
3572 struct got_object_id *commit_id, struct got_reflist_head *refs,
3573 struct got_repository *repo)
3575 struct tog_view *log_view;
3576 const struct got_error *err = NULL;
3577 char *path;
3579 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3580 if (log_view == NULL)
3581 return got_error_prefix_errno("view_open");
3583 err = tree_entry_path(&path, parents, te);
3584 if (err)
3585 return err;
3587 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3588 if (err)
3589 view_close(log_view);
3590 else
3591 *new_view = log_view;
3592 free(path);
3593 return err;
3596 static const struct got_error *
3597 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3598 struct got_object_id *commit_id, struct got_reflist_head *refs,
3599 struct got_repository *repo)
3601 const struct got_error *err = NULL;
3602 char *commit_id_str = NULL;
3603 struct tog_tree_view_state *s = &view->state.tree;
3605 TAILQ_INIT(&s->parents);
3607 err = got_object_id_str(&commit_id_str, commit_id);
3608 if (err != NULL)
3609 goto done;
3611 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3612 err = got_error_prefix_errno("asprintf");
3613 goto done;
3616 s->root = s->tree = root;
3617 s->entries = got_object_tree_get_entries(root);
3618 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3619 s->commit_id = got_object_id_dup(commit_id);
3620 if (s->commit_id == NULL) {
3621 err = got_error_prefix_errno("got_object_id_dup");
3622 goto done;
3624 s->refs = refs;
3625 s->repo = repo;
3627 view->show = show_tree_view;
3628 view->input = input_tree_view;
3629 view->close = close_tree_view;
3630 done:
3631 free(commit_id_str);
3632 if (err) {
3633 free(s->tree_label);
3634 s->tree_label = NULL;
3636 return err;
3639 static const struct got_error *
3640 close_tree_view(struct tog_view *view)
3642 struct tog_tree_view_state *s = &view->state.tree;
3644 free(s->tree_label);
3645 s->tree_label = NULL;
3646 free(s->commit_id);
3647 s->commit_id = NULL;
3648 while (!TAILQ_EMPTY(&s->parents)) {
3649 struct tog_parent_tree *parent;
3650 parent = TAILQ_FIRST(&s->parents);
3651 TAILQ_REMOVE(&s->parents, parent, entry);
3652 free(parent);
3655 if (s->tree != s->root)
3656 got_object_tree_close(s->tree);
3657 got_object_tree_close(s->root);
3659 return NULL;
3662 static const struct got_error *
3663 show_tree_view(struct tog_view *view)
3665 const struct got_error *err = NULL;
3666 struct tog_tree_view_state *s = &view->state.tree;
3667 char *parent_path;
3669 err = tree_entry_path(&parent_path, &s->parents, NULL);
3670 if (err)
3671 return err;
3673 err = draw_tree_entries(view, &s->first_displayed_entry,
3674 &s->last_displayed_entry, &s->selected_entry,
3675 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3676 s->entries, s->selected, view->nlines, s->tree == s->root);
3677 free(parent_path);
3679 view_vborder(view);
3680 return err;
3683 static const struct got_error *
3684 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3685 struct tog_view **focus_view, struct tog_view *view, int ch)
3687 const struct got_error *err = NULL;
3688 struct tog_tree_view_state *s = &view->state.tree;
3689 struct tog_view *log_view;
3690 int begin_x = 0, nscrolled;
3692 switch (ch) {
3693 case 'i':
3694 s->show_ids = !s->show_ids;
3695 break;
3696 case 'l':
3697 if (!s->selected_entry)
3698 break;
3699 if (view_is_parent_view(view))
3700 begin_x = view_split_begin_x(view->begin_x);
3701 err = log_tree_entry(&log_view, begin_x,
3702 s->selected_entry, &s->parents,
3703 s->commit_id, s->refs, s->repo);
3704 if (view_is_parent_view(view)) {
3705 err = view_close_child(view);
3706 if (err)
3707 return err;
3708 err = view_set_child(view, log_view);
3709 if (err) {
3710 view_close(log_view);
3711 break;
3713 *focus_view = log_view;
3714 view->child_focussed = 1;
3715 } else
3716 *new_view = log_view;
3717 break;
3718 case 'k':
3719 case KEY_UP:
3720 if (s->selected > 0) {
3721 s->selected--;
3722 if (s->selected == 0)
3723 break;
3725 if (s->selected > 0)
3726 break;
3727 tree_scroll_up(view, &s->first_displayed_entry, 1,
3728 s->entries, s->tree == s->root);
3729 break;
3730 case KEY_PPAGE:
3731 tree_scroll_up(view, &s->first_displayed_entry,
3732 MAX(0, view->nlines - 4 - s->selected), s->entries,
3733 s->tree == s->root);
3734 s->selected = 0;
3735 if (SIMPLEQ_FIRST(&s->entries->head) ==
3736 s->first_displayed_entry && s->tree != s->root)
3737 s->first_displayed_entry = NULL;
3738 break;
3739 case 'j':
3740 case KEY_DOWN:
3741 if (s->selected < s->ndisplayed - 1) {
3742 s->selected++;
3743 break;
3745 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3746 == NULL) {
3747 /* can't scroll any further */
3748 view_flash(view);
3749 break;
3751 tree_scroll_down(&s->first_displayed_entry, 1,
3752 s->last_displayed_entry, s->entries);
3753 break;
3754 case KEY_NPAGE:
3755 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3756 == NULL) {
3757 /* can't scroll any further; move cursor down */
3758 if (s->selected < s->ndisplayed - 1)
3759 s->selected = s->ndisplayed - 1;
3760 else
3761 view_flash(view);
3762 break;
3764 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3765 view->nlines, s->last_displayed_entry, s->entries);
3766 if (nscrolled < view->nlines) {
3767 int ndisplayed = 0;
3768 struct got_tree_entry *te;
3769 te = s->first_displayed_entry;
3770 do {
3771 ndisplayed++;
3772 te = SIMPLEQ_NEXT(te, entry);
3773 } while (te);
3774 s->selected = ndisplayed - 1;
3776 break;
3777 case KEY_ENTER:
3778 case '\r':
3779 if (s->selected_entry == NULL) {
3780 struct tog_parent_tree *parent;
3781 case KEY_BACKSPACE:
3782 /* user selected '..' */
3783 if (s->tree == s->root)
3784 break;
3785 parent = TAILQ_FIRST(&s->parents);
3786 TAILQ_REMOVE(&s->parents, parent,
3787 entry);
3788 got_object_tree_close(s->tree);
3789 s->tree = parent->tree;
3790 s->entries =
3791 got_object_tree_get_entries(s->tree);
3792 s->first_displayed_entry =
3793 parent->first_displayed_entry;
3794 s->selected_entry =
3795 parent->selected_entry;
3796 s->selected = parent->selected;
3797 free(parent);
3798 } else if (S_ISDIR(s->selected_entry->mode)) {
3799 struct tog_parent_tree *parent;
3800 struct got_tree_object *child;
3801 err = got_object_open_as_tree(&child,
3802 s->repo, s->selected_entry->id);
3803 if (err)
3804 break;
3805 parent = calloc(1, sizeof(*parent));
3806 if (parent == NULL) {
3807 err = got_error_prefix_errno("calloc");
3808 break;
3810 parent->tree = s->tree;
3811 parent->first_displayed_entry =
3812 s->first_displayed_entry;
3813 parent->selected_entry = s->selected_entry;
3814 parent->selected = s->selected;
3815 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3816 s->tree = child;
3817 s->entries =
3818 got_object_tree_get_entries(s->tree);
3819 s->selected = 0;
3820 s->first_displayed_entry = NULL;
3821 } else if (S_ISREG(s->selected_entry->mode)) {
3822 struct tog_view *blame_view;
3823 int begin_x = view_is_parent_view(view) ?
3824 view_split_begin_x(view->begin_x) : 0;
3826 err = blame_tree_entry(&blame_view, begin_x,
3827 s->selected_entry, &s->parents,
3828 s->commit_id, s->refs, s->repo);
3829 if (err)
3830 break;
3831 if (view_is_parent_view(view)) {
3832 err = view_close_child(view);
3833 if (err)
3834 return err;
3835 err = view_set_child(view, blame_view);
3836 if (err) {
3837 view_close(blame_view);
3838 break;
3840 *focus_view = blame_view;
3841 view->child_focussed = 1;
3842 } else
3843 *new_view = blame_view;
3845 break;
3846 case KEY_RESIZE:
3847 if (s->selected > view->nlines)
3848 s->selected = s->ndisplayed - 1;
3849 break;
3850 default:
3851 break;
3854 return err;
3857 __dead static void
3858 usage_tree(void)
3860 endwin();
3861 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3862 getprogname());
3863 exit(1);
3866 static const struct got_error *
3867 cmd_tree(int argc, char *argv[])
3869 const struct got_error *error;
3870 struct got_repository *repo = NULL;
3871 struct got_reflist_head refs;
3872 char *repo_path = NULL;
3873 struct got_object_id *commit_id = NULL;
3874 char *commit_id_arg = NULL;
3875 struct got_commit_object *commit = NULL;
3876 struct got_tree_object *tree = NULL;
3877 int ch;
3878 struct tog_view *view;
3880 SIMPLEQ_INIT(&refs);
3882 #ifndef PROFILE
3883 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3884 NULL) == -1)
3885 err(1, "pledge");
3886 #endif
3888 while ((ch = getopt(argc, argv, "c:")) != -1) {
3889 switch (ch) {
3890 case 'c':
3891 commit_id_arg = optarg;
3892 break;
3893 default:
3894 usage_tree();
3895 /* NOTREACHED */
3899 argc -= optind;
3900 argv += optind;
3902 if (argc == 0) {
3903 struct got_worktree *worktree;
3904 char *cwd = getcwd(NULL, 0);
3905 if (cwd == NULL)
3906 return got_error_prefix_errno("getcwd");
3907 error = got_worktree_open(&worktree, cwd);
3908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3909 goto done;
3910 if (worktree) {
3911 free(cwd);
3912 repo_path =
3913 strdup(got_worktree_get_repo_path(worktree));
3914 got_worktree_close(worktree);
3915 } else
3916 repo_path = cwd;
3917 if (repo_path == NULL) {
3918 error = got_error_prefix_errno("strdup");
3919 goto done;
3921 } else if (argc == 1) {
3922 repo_path = realpath(argv[0], NULL);
3923 if (repo_path == NULL)
3924 return got_error_prefix_errno2("realpath", argv[0]);
3925 } else
3926 usage_log();
3928 init_curses();
3930 error = got_repo_open(&repo, repo_path);
3931 if (error != NULL)
3932 goto done;
3934 error = apply_unveil(got_repo_get_path(repo), NULL);
3935 if (error)
3936 goto done;
3938 if (commit_id_arg == NULL)
3939 error = get_head_commit_id(&commit_id, repo);
3940 else
3941 error = got_object_resolve_id_str(&commit_id, repo,
3942 commit_id_arg);
3943 if (error != NULL)
3944 goto done;
3946 error = got_object_open_as_commit(&commit, repo, commit_id);
3947 if (error != NULL)
3948 goto done;
3950 error = got_object_open_as_tree(&tree, repo,
3951 got_object_commit_get_tree_id(commit));
3952 if (error != NULL)
3953 goto done;
3955 error = got_ref_list(&refs, repo);
3956 if (error)
3957 goto done;
3959 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3960 if (view == NULL) {
3961 error = got_error_prefix_errno("view_open");
3962 goto done;
3964 error = open_tree_view(view, tree, commit_id, &refs, repo);
3965 if (error)
3966 goto done;
3967 error = view_loop(view);
3968 done:
3969 free(repo_path);
3970 free(commit_id);
3971 if (commit)
3972 got_object_commit_close(commit);
3973 if (tree)
3974 got_object_tree_close(tree);
3975 if (repo)
3976 got_repo_close(repo);
3977 got_ref_list_free(&refs);
3978 return error;
3981 __dead static void
3982 usage(void)
3984 int i;
3986 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3987 "Available commands:\n", getprogname());
3988 for (i = 0; i < nitems(tog_commands); i++) {
3989 struct tog_cmd *cmd = &tog_commands[i];
3990 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3992 exit(1);
3995 static char **
3996 make_argv(const char *arg0, const char *arg1)
3998 char **argv;
3999 int argc = (arg1 == NULL ? 1 : 2);
4001 argv = calloc(argc, sizeof(char *));
4002 if (argv == NULL)
4003 err(1, "calloc");
4004 argv[0] = strdup(arg0);
4005 if (argv[0] == NULL)
4006 err(1, "calloc");
4007 if (arg1) {
4008 argv[1] = strdup(arg1);
4009 if (argv[1] == NULL)
4010 err(1, "calloc");
4013 return argv;
4016 int
4017 main(int argc, char *argv[])
4019 const struct got_error *error = NULL;
4020 struct tog_cmd *cmd = NULL;
4021 int ch, hflag = 0;
4022 char **cmd_argv = NULL;
4024 setlocale(LC_CTYPE, "");
4026 while ((ch = getopt(argc, argv, "h")) != -1) {
4027 switch (ch) {
4028 case 'h':
4029 hflag = 1;
4030 break;
4031 default:
4032 usage();
4033 /* NOTREACHED */
4037 argc -= optind;
4038 argv += optind;
4039 optind = 0;
4040 optreset = 1;
4042 if (argc == 0) {
4043 if (hflag)
4044 usage();
4045 /* Build an argument vector which runs a default command. */
4046 cmd = &tog_commands[0];
4047 cmd_argv = make_argv(cmd->name, NULL);
4048 argc = 1;
4049 } else {
4050 int i;
4052 /* Did the user specific a command? */
4053 for (i = 0; i < nitems(tog_commands); i++) {
4054 if (strncmp(tog_commands[i].name, argv[0],
4055 strlen(argv[0])) == 0) {
4056 cmd = &tog_commands[i];
4057 if (hflag)
4058 tog_commands[i].cmd_usage();
4059 break;
4062 if (cmd == NULL) {
4063 /* Did the user specify a repository? */
4064 char *repo_path = realpath(argv[0], NULL);
4065 if (repo_path) {
4066 struct got_repository *repo;
4067 error = got_repo_open(&repo, repo_path);
4068 if (error == NULL)
4069 got_repo_close(repo);
4070 } else
4071 error = got_error_prefix_errno2("realpath",
4072 argv[0]);
4073 if (error) {
4074 if (hflag) {
4075 fprintf(stderr, "%s: '%s' is not a "
4076 "known command\n", getprogname(),
4077 argv[0]);
4078 usage();
4080 fprintf(stderr, "%s: '%s' is neither a known "
4081 "command nor a path to a repository\n",
4082 getprogname(), argv[0]);
4083 free(repo_path);
4084 return 1;
4086 cmd = &tog_commands[0];
4087 cmd_argv = make_argv(cmd->name, repo_path);
4088 argc = 2;
4089 free(repo_path);
4093 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4094 if (error)
4095 goto done;
4096 done:
4097 endwin();
4098 free(cmd_argv);
4099 if (error)
4100 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4101 return 0;