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_worktree.h"
52 #ifndef MIN
53 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 #endif
56 #ifndef MAX
57 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
58 #endif
61 #ifndef nitems
62 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 #endif
65 struct tog_cmd {
66 const char *name;
67 const struct got_error *(*cmd_main)(int, char *[]);
68 void (*cmd_usage)(void);
69 const char *descr;
70 };
72 __dead static void usage(void);
73 __dead static void usage_log(void);
74 __dead static void usage_diff(void);
75 __dead static void usage_blame(void);
76 __dead static void usage_tree(void);
78 static const struct got_error* cmd_log(int, char *[]);
79 static const struct got_error* cmd_diff(int, char *[]);
80 static const struct got_error* cmd_blame(int, char *[]);
81 static const struct got_error* cmd_tree(int, char *[]);
83 static struct tog_cmd tog_commands[] = {
84 { "log", cmd_log, usage_log,
85 "show repository history" },
86 { "diff", cmd_diff, usage_diff,
87 "compare files and directories" },
88 { "blame", cmd_blame, usage_blame,
89 "show line-by-line file history" },
90 { "tree", cmd_tree, usage_tree,
91 "browse trees in repository" },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
154 sig_atomic_t quit;
155 pthread_t thread;
156 struct tog_log_thread_args thread_args;
157 };
159 struct tog_blame_cb_args {
160 struct tog_blame_line *lines; /* one per line */
161 int nlines;
163 struct tog_view *view;
164 struct got_object_id *commit_id;
165 int *quit;
166 };
168 struct tog_blame_thread_args {
169 const char *path;
170 struct got_repository *repo;
171 struct tog_blame_cb_args *cb_args;
172 int *complete;
173 };
175 struct tog_blame {
176 FILE *f;
177 size_t filesize;
178 struct tog_blame_line *lines;
179 int nlines;
180 pthread_t thread;
181 struct tog_blame_thread_args thread_args;
182 struct tog_blame_cb_args cb_args;
183 const char *path;
184 };
186 struct tog_blame_view_state {
187 int first_displayed_line;
188 int last_displayed_line;
189 int selected_line;
190 int blame_complete;
191 int eof;
192 int done;
193 struct got_object_id_queue blamed_commits;
194 struct got_object_qid *blamed_commit;
195 char *path;
196 struct got_repository *repo;
197 struct got_reflist_head *refs;
198 struct got_object_id *commit_id;
199 struct tog_blame blame;
200 };
202 struct tog_parent_tree {
203 TAILQ_ENTRY(tog_parent_tree) entry;
204 struct got_tree_object *tree;
205 struct got_tree_entry *first_displayed_entry;
206 struct got_tree_entry *selected_entry;
207 int selected;
208 };
210 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
212 struct tog_tree_view_state {
213 char *tree_label;
214 struct got_tree_object *root;
215 struct got_tree_object *tree;
216 const struct got_tree_entries *entries;
217 struct got_tree_entry *first_displayed_entry;
218 struct got_tree_entry *last_displayed_entry;
219 struct got_tree_entry *selected_entry;
220 int ndisplayed, selected, show_ids;
221 struct tog_parent_trees parents;
222 struct got_object_id *commit_id;
223 struct got_repository *repo;
224 struct got_reflist_head *refs;
225 };
227 /*
228 * We implement two types of views: parent views and child views.
230 * The 'Tab' key switches between a parent view and its child view.
231 * Child views are shown side-by-side to their parent view, provided
232 * there is enough screen estate.
234 * When a new view is opened from within a parent view, this new view
235 * becomes a child view of the parent view, replacing any existing child.
237 * When a new view is opened from within a child view, this new view
238 * becomes a parent view which will obscure the views below until the
239 * user quits the new parent view by typing 'q'.
241 * This list of views contains parent views only.
242 * Child views are only pointed to by their parent view.
243 */
244 TAILQ_HEAD(tog_view_list_head, tog_view);
246 struct tog_view {
247 TAILQ_ENTRY(tog_view) entry;
248 WINDOW *window;
249 PANEL *panel;
250 int nlines, ncols, begin_y, begin_x;
251 int lines, cols; /* copies of LINES and COLS */
252 int focussed;
253 struct tog_view *parent;
254 struct tog_view *child;
255 int child_focussed;
257 /* type-specific state */
258 enum tog_view_type type;
259 union {
260 struct tog_diff_view_state diff;
261 struct tog_log_view_state log;
262 struct tog_blame_view_state blame;
263 struct tog_tree_view_state tree;
264 } state;
266 const struct got_error *(*show)(struct tog_view *);
267 const struct got_error *(*input)(struct tog_view **,
268 struct tog_view **, struct tog_view**, struct tog_view *, int);
269 const struct got_error *(*close)(struct tog_view *);
270 };
272 static const struct got_error *open_diff_view(struct tog_view *,
273 struct got_object_id *, struct got_object_id *, struct tog_view *,
274 struct got_reflist_head *, struct got_repository *);
275 static const struct got_error *show_diff_view(struct tog_view *);
276 static const struct got_error *input_diff_view(struct tog_view **,
277 struct tog_view **, struct tog_view **, struct tog_view *, int);
278 static const struct got_error* close_diff_view(struct tog_view *);
280 static const struct got_error *open_log_view(struct tog_view *,
281 struct got_object_id *, struct got_reflist_head *,
282 struct got_repository *, const char *, int);
283 static const struct got_error * show_log_view(struct tog_view *);
284 static const struct got_error *input_log_view(struct tog_view **,
285 struct tog_view **, struct tog_view **, struct tog_view *, int);
286 static const struct got_error *close_log_view(struct tog_view *);
288 static const struct got_error *open_blame_view(struct tog_view *, char *,
289 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_blame_view(struct tog_view *);
291 static const struct got_error *input_blame_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error *close_blame_view(struct tog_view *);
295 static const struct got_error *open_tree_view(struct tog_view *,
296 struct got_tree_object *, struct got_object_id *,
297 struct got_reflist_head *, struct got_repository *);
298 static const struct got_error *show_tree_view(struct tog_view *);
299 static const struct got_error *input_tree_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_tree_view(struct tog_view *);
303 static volatile sig_atomic_t tog_sigwinch_received;
305 static void
306 tog_sigwinch(int signo)
308 tog_sigwinch_received = 1;
311 static const struct got_error *
312 view_close(struct tog_view *view)
314 const struct got_error *err = NULL;
316 if (view->child) {
317 view_close(view->child);
318 view->child = NULL;
320 if (view->close)
321 err = view->close(view);
322 if (view->panel)
323 del_panel(view->panel);
324 if (view->window)
325 delwin(view->window);
326 free(view);
327 return err;
330 static struct tog_view *
331 view_open(int nlines, int ncols, int begin_y, int begin_x,
332 enum tog_view_type type)
334 struct tog_view *view = calloc(1, sizeof(*view));
336 if (view == NULL)
337 return NULL;
339 view->type = type;
340 view->lines = LINES;
341 view->cols = COLS;
342 view->nlines = nlines ? nlines : LINES - begin_y;
343 view->ncols = ncols ? ncols : COLS - begin_x;
344 view->begin_y = begin_y;
345 view->begin_x = begin_x;
346 view->window = newwin(nlines, ncols, begin_y, begin_x);
347 if (view->window == NULL) {
348 view_close(view);
349 return NULL;
351 view->panel = new_panel(view->window);
352 if (view->panel == NULL ||
353 set_panel_userptr(view->panel, view) != OK) {
354 view_close(view);
355 return NULL;
358 keypad(view->window, TRUE);
359 return view;
362 static int
363 view_split_begin_x(int begin_x)
365 if (begin_x > 0 || COLS < 120)
366 return 0;
367 return (COLS - MAX(COLS / 2, 80));
370 static const struct got_error *view_resize(struct tog_view *);
372 static const struct got_error *
373 view_splitscreen(struct tog_view *view)
375 const struct got_error *err = NULL;
377 view->begin_y = 0;
378 view->begin_x = view_split_begin_x(0);
379 view->nlines = LINES;
380 view->ncols = COLS - view->begin_x;
381 view->lines = LINES;
382 view->cols = COLS;
383 err = view_resize(view);
384 if (err)
385 return err;
387 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
388 return got_error_prefix_errno("mvwin");
390 return NULL;
393 static const struct got_error *
394 view_fullscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
398 view->begin_x = 0;
399 view->begin_y = 0;
400 view->nlines = LINES;
401 view->ncols = COLS;
402 view->lines = LINES;
403 view->cols = COLS;
404 err = view_resize(view);
405 if (err)
406 return err;
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_prefix_errno("mvwin");
411 return NULL;
414 static int
415 view_is_parent_view(struct tog_view *view)
417 return view->parent == NULL;
420 static const struct got_error *
421 view_resize(struct tog_view *view)
423 int nlines, ncols;
425 if (view->lines > LINES)
426 nlines = view->nlines - (view->lines - LINES);
427 else
428 nlines = view->nlines + (LINES - view->lines);
430 if (view->cols > COLS)
431 ncols = view->ncols - (view->cols - COLS);
432 else
433 ncols = view->ncols + (COLS - view->cols);
435 if (wresize(view->window, nlines, ncols) == ERR)
436 return got_error_prefix_errno("wresize");
437 if (replace_panel(view->panel, view->window) == ERR)
438 return got_error_prefix_errno("replace_panel");
439 wclear(view->window);
441 view->nlines = nlines;
442 view->ncols = ncols;
443 view->lines = LINES;
444 view->cols = COLS;
446 if (view->child) {
447 view->child->begin_x = view_split_begin_x(view->begin_x);
448 if (view->child->begin_x == 0) {
449 view_fullscreen(view->child);
450 if (view->child->focussed)
451 show_panel(view->child->panel);
452 else
453 show_panel(view->panel);
454 } else {
455 view_splitscreen(view->child);
456 show_panel(view->child->panel);
460 return NULL;
463 static const struct got_error *
464 view_close_child(struct tog_view *view)
466 const struct got_error *err = NULL;
468 if (view->child == NULL)
469 return NULL;
471 err = view_close(view->child);
472 view->child = NULL;
473 return err;
476 static const struct got_error *
477 view_set_child(struct tog_view *view, struct tog_view *child)
479 const struct got_error *err = NULL;
481 view->child = child;
482 child->parent = view;
483 return err;
486 static int
487 view_is_splitscreen(struct tog_view *view)
489 return view->begin_x > 0;
492 /*
493 * Erase all content of the view. Can be used to "flash" the view because
494 * the view loop will redraw it quickly, providing a more subtle visual
495 * effect than curs_flash(3) would provide.
496 */
497 static void
498 view_flash(struct tog_view *view)
500 werase(view->window);
501 update_panels();
502 doupdate();
505 static void
506 tog_resizeterm(void)
508 int cols, lines;
509 struct winsize size;
511 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
512 cols = 80; /* Default */
513 lines = 24;
514 } else {
515 cols = size.ws_col;
516 lines = size.ws_row;
518 resize_term(lines, cols);
521 static const struct got_error *
522 view_input(struct tog_view **new, struct tog_view **dead,
523 struct tog_view **focus, int *done, struct tog_view *view,
524 struct tog_view_list_head *views)
526 const struct got_error *err = NULL;
527 struct tog_view *v;
528 int ch, errcode;
530 *new = NULL;
531 *dead = NULL;
532 *focus = NULL;
534 nodelay(stdscr, FALSE);
535 /* Allow threads to make progress while we are waiting for input. */
536 errcode = pthread_mutex_unlock(&tog_mutex);
537 if (errcode)
538 return got_error_set_errno(errcode, "pthread_mutex_unlock");
539 ch = wgetch(view->window);
540 errcode = pthread_mutex_lock(&tog_mutex);
541 if (errcode)
542 return got_error_set_errno(errcode, "pthread_mutex_lock");
543 nodelay(stdscr, TRUE);
545 if (tog_sigwinch_received) {
546 tog_resizeterm();
547 tog_sigwinch_received = 0;
548 TAILQ_FOREACH(v, views, entry) {
549 err = view_resize(v);
550 if (err)
551 return err;
552 err = v->input(new, dead, focus, v, KEY_RESIZE);
553 if (err)
554 return err;
558 switch (ch) {
559 case ERR:
560 break;
561 case '\t':
562 if (view->child) {
563 *focus = view->child;
564 view->child_focussed = 1;
565 } else if (view->parent) {
566 *focus = view->parent;
567 view->parent->child_focussed = 0;
569 break;
570 case 'q':
571 err = view->input(new, dead, focus, view, ch);
572 *dead = view;
573 break;
574 case 'Q':
575 *done = 1;
576 break;
577 case 'f':
578 if (view_is_parent_view(view)) {
579 if (view->child == NULL)
580 break;
581 if (view_is_splitscreen(view->child)) {
582 *focus = view->child;
583 view->child_focussed = 1;
584 err = view_fullscreen(view->child);
585 } else
586 err = view_splitscreen(view->child);
587 if (err)
588 break;
589 err = view->child->input(new, dead, focus,
590 view->child, KEY_RESIZE);
591 } else {
592 if (view_is_splitscreen(view)) {
593 *focus = view;
594 view->parent->child_focussed = 1;
595 err = view_fullscreen(view);
596 } else {
597 err = view_splitscreen(view);
599 if (err)
600 break;
601 err = view->input(new, dead, focus, view,
602 KEY_RESIZE);
604 break;
605 case KEY_RESIZE:
606 break;
607 default:
608 err = view->input(new, dead, focus, view, ch);
609 break;
612 return err;
615 void
616 view_vborder(struct tog_view *view)
618 PANEL *panel;
619 struct tog_view *view_above;
621 if (view->parent)
622 return view_vborder(view->parent);
624 panel = panel_above(view->panel);
625 if (panel == NULL)
626 return;
628 view_above = panel_userptr(panel);
629 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
630 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
633 int
634 view_needs_focus_indication(struct tog_view *view)
636 if (view_is_parent_view(view)) {
637 if (view->child == NULL || view->child_focussed)
638 return 0;
639 if (!view_is_splitscreen(view->child))
640 return 0;
641 } else if (!view_is_splitscreen(view))
642 return 0;
644 return view->focussed;
647 static const struct got_error *
648 view_loop(struct tog_view *view)
650 const struct got_error *err = NULL;
651 struct tog_view_list_head views;
652 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
653 int fast_refresh = 10;
654 int done = 0, errcode;
656 errcode = pthread_mutex_lock(&tog_mutex);
657 if (errcode)
658 return got_error_set_errno(errcode, "pthread_mutex_lock");
660 TAILQ_INIT(&views);
661 TAILQ_INSERT_HEAD(&views, view, entry);
663 main_view = view;
664 view->focussed = 1;
665 err = view->show(view);
666 if (err)
667 return err;
668 update_panels();
669 doupdate();
670 while (!TAILQ_EMPTY(&views) && !done) {
671 /* Refresh fast during initialization, then become slower. */
672 if (fast_refresh && fast_refresh-- == 0)
673 halfdelay(10); /* switch to once per second */
675 err = view_input(&new_view, &dead_view, &focus_view, &done,
676 view, &views);
677 if (err)
678 break;
679 if (dead_view) {
680 struct tog_view *prev = NULL;
682 if (view_is_parent_view(dead_view))
683 prev = TAILQ_PREV(dead_view,
684 tog_view_list_head, entry);
685 else if (view->parent != dead_view)
686 prev = view->parent;
688 if (dead_view->parent)
689 dead_view->parent->child = NULL;
690 else
691 TAILQ_REMOVE(&views, dead_view, entry);
693 err = view_close(dead_view);
694 if (err || dead_view == main_view)
695 goto done;
697 if (view == dead_view) {
698 if (focus_view)
699 view = focus_view;
700 else if (prev)
701 view = prev;
702 else if (!TAILQ_EMPTY(&views))
703 view = TAILQ_LAST(&views,
704 tog_view_list_head);
705 else
706 view = NULL;
707 if (view) {
708 if (view->child && view->child_focussed)
709 focus_view = view->child;
710 else
711 focus_view = view;
715 if (new_view) {
716 struct tog_view *v, *t;
717 /* Only allow one parent view per type. */
718 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
719 if (v->type != new_view->type)
720 continue;
721 TAILQ_REMOVE(&views, v, entry);
722 err = view_close(v);
723 if (err)
724 goto done;
725 break;
727 TAILQ_INSERT_TAIL(&views, new_view, entry);
728 view = new_view;
729 if (focus_view == NULL)
730 focus_view = new_view;
732 if (focus_view) {
733 show_panel(focus_view->panel);
734 if (view)
735 view->focussed = 0;
736 focus_view->focussed = 1;
737 view = focus_view;
738 if (new_view)
739 show_panel(new_view->panel);
740 if (view->child && view_is_splitscreen(view->child))
741 show_panel(view->child->panel);
743 if (view) {
744 if (focus_view == NULL) {
745 view->focussed = 1;
746 show_panel(view->panel);
747 if (view->child && view_is_splitscreen(view->child))
748 show_panel(view->child->panel);
749 focus_view = view;
751 if (view->parent) {
752 err = view->parent->show(view->parent);
753 if (err)
754 goto done;
756 err = view->show(view);
757 if (err)
758 goto done;
759 if (view->child) {
760 err = view->child->show(view->child);
761 if (err)
762 goto done;
764 update_panels();
765 doupdate();
768 done:
769 while (!TAILQ_EMPTY(&views)) {
770 view = TAILQ_FIRST(&views);
771 TAILQ_REMOVE(&views, view, entry);
772 view_close(view);
775 errcode = pthread_mutex_unlock(&tog_mutex);
776 if (errcode)
777 return got_error_set_errno(errcode, "pthread_mutex_unlock");
779 return err;
782 __dead static void
783 usage_log(void)
785 endwin();
786 fprintf(stderr,
787 "usage: %s log [-c commit] [-r repository-path] [path]\n",
788 getprogname());
789 exit(1);
792 /* Create newly allocated wide-character string equivalent to a byte string. */
793 static const struct got_error *
794 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
796 char *vis = NULL;
797 const struct got_error *err = NULL;
799 *ws = NULL;
800 *wlen = mbstowcs(NULL, s, 0);
801 if (*wlen == (size_t)-1) {
802 int vislen;
803 if (errno != EILSEQ)
804 return got_error_prefix_errno("mbstowcs");
806 /* byte string invalid in current encoding; try to "fix" it */
807 err = got_mbsavis(&vis, &vislen, s);
808 if (err)
809 return err;
810 *wlen = mbstowcs(NULL, vis, 0);
811 if (*wlen == (size_t)-1) {
812 err = got_error_prefix_errno("mbstowcs"); /* give up */
813 goto done;
817 *ws = calloc(*wlen + 1, sizeof(*ws));
818 if (*ws == NULL) {
819 err = got_error_prefix_errno("calloc");
820 goto done;
823 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
824 err = got_error_prefix_errno("mbstowcs");
825 done:
826 free(vis);
827 if (err) {
828 free(*ws);
829 *ws = NULL;
830 *wlen = 0;
832 return err;
835 /* Format a line for display, ensuring that it won't overflow a width limit. */
836 static const struct got_error *
837 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
839 const struct got_error *err = NULL;
840 int cols = 0;
841 wchar_t *wline = NULL;
842 size_t wlen;
843 int i;
845 *wlinep = NULL;
846 *widthp = 0;
848 err = mbs2ws(&wline, &wlen, line);
849 if (err)
850 return err;
852 i = 0;
853 while (i < wlen && cols < wlimit) {
854 int width = wcwidth(wline[i]);
855 switch (width) {
856 case 0:
857 i++;
858 break;
859 case 1:
860 case 2:
861 if (cols + width <= wlimit)
862 cols += width;
863 i++;
864 break;
865 case -1:
866 if (wline[i] == L'\t')
867 cols += TABSIZE - ((cols + 1) % TABSIZE);
868 i++;
869 break;
870 default:
871 err = got_error_prefix_errno("wcwidth");
872 goto done;
875 wline[i] = L'\0';
876 if (widthp)
877 *widthp = cols;
878 done:
879 if (err)
880 free(wline);
881 else
882 *wlinep = wline;
883 return err;
886 static const struct got_error*
887 build_refs_str(char **refs_str, struct got_reflist_head *refs,
888 struct got_object_id *id)
890 static const struct got_error *err = NULL;
891 struct got_reflist_entry *re;
892 char *s;
893 const char *name;
895 *refs_str = NULL;
897 SIMPLEQ_FOREACH(re, refs, entry) {
898 if (got_object_id_cmp(re->id, id) != 0)
899 continue;
900 name = got_ref_get_name(re->ref);
901 if (strcmp(name, GOT_REF_HEAD) == 0)
902 continue;
903 if (strncmp(name, "refs/", 5) == 0)
904 name += 5;
905 if (strncmp(name, "got/", 4) == 0)
906 continue;
907 if (strncmp(name, "heads/", 6) == 0)
908 name += 6;
909 if (strncmp(name, "remotes/", 8) == 0)
910 name += 8;
911 s = *refs_str;
912 if (asprintf(refs_str, "%s%s%s", s ? s : "",
913 s ? ", " : "", name) == -1) {
914 err = got_error_prefix_errno("asprintf");
915 free(s);
916 *refs_str = NULL;
917 break;
919 free(s);
922 return err;
925 static const struct got_error *
926 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
928 char *smallerthan, *at;
930 smallerthan = strchr(author, '<');
931 if (smallerthan && smallerthan[1] != '\0')
932 author = smallerthan + 1;
933 at = strchr(author, '@');
934 if (at)
935 *at = '\0';
936 return format_line(wauthor, author_width, author, limit);
939 static const struct got_error *
940 draw_commit(struct tog_view *view, struct got_commit_object *commit,
941 struct got_object_id *id, struct got_reflist_head *refs,
942 int author_display_cols)
944 const struct got_error *err = NULL;
945 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
946 char *logmsg0 = NULL, *logmsg = NULL;
947 char *author = NULL;
948 wchar_t *wlogmsg = NULL, *wauthor = NULL;
949 int author_width, logmsg_width;
950 char *newline, *line = NULL;
951 int col, limit;
952 static const size_t date_display_cols = 9;
953 const int avail = view->ncols;
954 struct tm tm;
955 time_t committer_time;
957 committer_time = got_object_commit_get_committer_time(commit);
958 if (localtime_r(&committer_time, &tm) == NULL)
959 return got_error_prefix_errno("localtime_r");
960 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
961 >= sizeof(datebuf))
962 return got_error(GOT_ERR_NO_SPACE);
964 if (avail < date_display_cols)
965 limit = MIN(sizeof(datebuf) - 1, avail);
966 else
967 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
968 waddnstr(view->window, datebuf, limit);
969 col = limit + 1;
970 if (col > avail)
971 goto done;
973 author = strdup(got_object_commit_get_author(commit));
974 if (author == NULL) {
975 err = got_error_prefix_errno("strdup");
976 goto done;
978 err = format_author(&wauthor, &author_width, author, avail - col);
979 if (err)
980 goto done;
981 waddwstr(view->window, wauthor);
982 col += author_width;
983 while (col <= avail && author_width < author_display_cols + 2) {
984 waddch(view->window, ' ');
985 col++;
986 author_width++;
988 if (col > avail)
989 goto done;
991 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
992 if (logmsg0 == NULL) {
993 err = got_error_prefix_errno("strdup");
994 goto done;
996 logmsg = logmsg0;
997 while (*logmsg == '\n')
998 logmsg++;
999 newline = strchr(logmsg, '\n');
1000 if (newline)
1001 *newline = '\0';
1002 limit = avail - col;
1003 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1004 if (err)
1005 goto done;
1006 waddwstr(view->window, wlogmsg);
1007 col += logmsg_width;
1008 while (col <= avail) {
1009 waddch(view->window, ' ');
1010 col++;
1012 done:
1013 free(logmsg0);
1014 free(wlogmsg);
1015 free(author);
1016 free(wauthor);
1017 free(line);
1018 return err;
1021 static struct commit_queue_entry *
1022 alloc_commit_queue_entry(struct got_commit_object *commit,
1023 struct got_object_id *id)
1025 struct commit_queue_entry *entry;
1027 entry = calloc(1, sizeof(*entry));
1028 if (entry == NULL)
1029 return NULL;
1031 entry->id = id;
1032 entry->commit = commit;
1033 return entry;
1036 static void
1037 pop_commit(struct commit_queue *commits)
1039 struct commit_queue_entry *entry;
1041 entry = TAILQ_FIRST(&commits->head);
1042 TAILQ_REMOVE(&commits->head, entry, entry);
1043 got_object_commit_close(entry->commit);
1044 commits->ncommits--;
1045 /* Don't free entry->id! It is owned by the commit graph. */
1046 free(entry);
1049 static void
1050 free_commits(struct commit_queue *commits)
1052 while (!TAILQ_EMPTY(&commits->head))
1053 pop_commit(commits);
1056 static const struct got_error *
1057 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1058 int minqueue, struct got_repository *repo, const char *path)
1060 const struct got_error *err = NULL;
1061 int nqueued = 0;
1064 * We keep all commits open throughout the lifetime of the log
1065 * view in order to avoid having to re-fetch commits from disk
1066 * while updating the display.
1068 while (nqueued < minqueue) {
1069 struct got_object_id *id;
1070 struct got_commit_object *commit;
1071 struct commit_queue_entry *entry;
1072 int errcode;
1074 err = got_commit_graph_iter_next(&id, graph);
1075 if (err) {
1076 if (err->code != GOT_ERR_ITER_NEED_MORE)
1077 break;
1078 err = got_commit_graph_fetch_commits(graph,
1079 minqueue, repo);
1080 if (err)
1081 return err;
1082 continue;
1085 if (id == NULL)
1086 break;
1088 err = got_object_open_as_commit(&commit, repo, id);
1089 if (err)
1090 break;
1091 entry = alloc_commit_queue_entry(commit, id);
1092 if (entry == NULL) {
1093 err = got_error_prefix_errno("alloc_commit_queue_entry");
1094 break;
1097 errcode = pthread_mutex_lock(&tog_mutex);
1098 if (errcode) {
1099 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1100 break;
1103 entry->idx = commits->ncommits;
1104 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1105 nqueued++;
1106 commits->ncommits++;
1108 errcode = pthread_mutex_unlock(&tog_mutex);
1109 if (errcode && err == NULL)
1110 err = got_error_set_errno(errcode,
1111 "pthread_mutex_unlock");
1114 return err;
1117 static const struct got_error *
1118 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1120 const struct got_error *err = NULL;
1121 struct got_reference *head_ref;
1123 *head_id = NULL;
1125 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1126 if (err)
1127 return err;
1129 err = got_ref_resolve(head_id, repo, head_ref);
1130 got_ref_close(head_ref);
1131 if (err) {
1132 *head_id = NULL;
1133 return err;
1136 return NULL;
1139 static const struct got_error *
1140 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1141 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1142 struct commit_queue *commits, int selected_idx, int limit,
1143 struct got_reflist_head *refs, const char *path, int commits_needed)
1145 const struct got_error *err = NULL;
1146 struct commit_queue_entry *entry;
1147 int ncommits, width;
1148 int author_cols = 10;
1149 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1150 char *refs_str = NULL;
1151 wchar_t *wline;
1153 entry = first;
1154 ncommits = 0;
1155 while (entry) {
1156 if (ncommits == selected_idx) {
1157 *selected = entry;
1158 break;
1160 entry = TAILQ_NEXT(entry, entry);
1161 ncommits++;
1164 if (*selected) {
1165 err = got_object_id_str(&id_str, (*selected)->id);
1166 if (err)
1167 return err;
1168 if (refs) {
1169 err = build_refs_str(&refs_str, refs, (*selected)->id);
1170 if (err)
1171 goto done;
1175 if (commits_needed == 0)
1176 halfdelay(10); /* disable fast refresh */
1178 if (asprintf(&ncommits_str, " [%d/%d] %s",
1179 entry ? entry->idx + 1 : 0, commits->ncommits,
1180 commits_needed > 0 ? "loading... " :
1181 (refs_str ? refs_str : "")) == -1) {
1182 err = got_error_prefix_errno("asprintf");
1183 goto done;
1186 if (path && strcmp(path, "/") != 0) {
1187 if (asprintf(&header, "commit %s %s%s",
1188 id_str ? id_str : "........................................",
1189 path, ncommits_str) == -1) {
1190 err = got_error_prefix_errno("asprintf");
1191 header = NULL;
1192 goto done;
1194 } else if (asprintf(&header, "commit %s%s",
1195 id_str ? id_str : "........................................",
1196 ncommits_str) == -1) {
1197 err = got_error_prefix_errno("asprintf");
1198 header = NULL;
1199 goto done;
1201 err = format_line(&wline, &width, header, view->ncols);
1202 if (err)
1203 goto done;
1205 werase(view->window);
1207 if (view_needs_focus_indication(view))
1208 wstandout(view->window);
1209 waddwstr(view->window, wline);
1210 while (width < view->ncols) {
1211 waddch(view->window, ' ');
1212 width++;
1214 if (view_needs_focus_indication(view))
1215 wstandend(view->window);
1216 free(wline);
1217 if (limit <= 1)
1218 goto done;
1220 /* Grow author column size if necessary. */
1221 entry = first;
1222 ncommits = 0;
1223 while (entry) {
1224 char *author;
1225 wchar_t *wauthor;
1226 int width;
1227 if (ncommits >= limit - 1)
1228 break;
1229 author = strdup(got_object_commit_get_author(entry->commit));
1230 if (author == NULL) {
1231 err = got_error_prefix_errno("strdup");
1232 goto done;
1234 err = format_author(&wauthor, &width, author, COLS);
1235 if (author_cols < width)
1236 author_cols = width;
1237 free(wauthor);
1238 free(author);
1239 entry = TAILQ_NEXT(entry, entry);
1242 entry = first;
1243 *last = first;
1244 ncommits = 0;
1245 while (entry) {
1246 if (ncommits >= limit - 1)
1247 break;
1248 if (ncommits == selected_idx)
1249 wstandout(view->window);
1250 err = draw_commit(view, entry->commit, entry->id, refs,
1251 author_cols);
1252 if (ncommits == selected_idx)
1253 wstandend(view->window);
1254 if (err)
1255 break;
1256 ncommits++;
1257 *last = entry;
1258 entry = TAILQ_NEXT(entry, entry);
1261 view_vborder(view);
1262 done:
1263 free(id_str);
1264 free(refs_str);
1265 free(ncommits_str);
1266 free(header);
1267 return err;
1270 static void
1271 scroll_up(struct tog_view *view,
1272 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1273 struct commit_queue *commits)
1275 struct commit_queue_entry *entry;
1276 int nscrolled = 0;
1278 entry = TAILQ_FIRST(&commits->head);
1279 if (*first_displayed_entry == entry) {
1280 view_flash(view);
1281 return;
1284 entry = *first_displayed_entry;
1285 while (entry && nscrolled < maxscroll) {
1286 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1287 if (entry) {
1288 *first_displayed_entry = entry;
1289 nscrolled++;
1294 static const struct got_error *
1295 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1296 pthread_cond_t *need_commits)
1298 int errcode;
1299 int max_wait = 20;
1301 halfdelay(1); /* fast refresh while loading commits */
1303 while (*commits_needed > 0) {
1304 if (*log_complete)
1305 break;
1307 /* Wake the log thread. */
1308 errcode = pthread_cond_signal(need_commits);
1309 if (errcode)
1310 return got_error_set_errno(errcode,
1311 "pthread_cond_signal");
1312 errcode = pthread_mutex_unlock(&tog_mutex);
1313 if (errcode)
1314 return got_error_set_errno(errcode,
1315 "pthread_mutex_unlock");
1316 pthread_yield();
1317 errcode = pthread_mutex_lock(&tog_mutex);
1318 if (errcode)
1319 return got_error_set_errno(errcode,
1320 "pthread_mutex_lock");
1322 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1324 * Thread is not done yet; lose a key press
1325 * and let the user retry... this way the GUI
1326 * remains interactive while logging deep paths
1327 * with few commits in history.
1329 return NULL;
1333 return NULL;
1336 static const struct got_error *
1337 scroll_down(struct tog_view *view,
1338 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1339 struct commit_queue_entry **last_displayed_entry,
1340 struct commit_queue *commits, int *log_complete, int *commits_needed,
1341 pthread_cond_t *need_commits)
1343 const struct got_error *err = NULL;
1344 struct commit_queue_entry *pentry;
1345 int nscrolled = 0;
1347 if (*last_displayed_entry == NULL)
1348 return NULL;
1350 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1351 if (pentry == NULL && !*log_complete) {
1353 * Ask the log thread for required amount of commits
1354 * plus some amount of pre-fetching.
1356 (*commits_needed) += maxscroll + 20;
1357 err = trigger_log_thread(0, commits_needed, log_complete,
1358 need_commits);
1359 if (err)
1360 return err;
1363 do {
1364 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1365 if (pentry == NULL) {
1366 if (*log_complete)
1367 view_flash(view);
1368 break;
1371 *last_displayed_entry = pentry;
1373 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1374 if (pentry == NULL)
1375 break;
1376 *first_displayed_entry = pentry;
1377 } while (++nscrolled < maxscroll);
1379 return err;
1382 static const struct got_error *
1383 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1384 struct got_commit_object *commit, struct got_object_id *commit_id,
1385 struct tog_view *log_view, struct got_reflist_head *refs,
1386 struct got_repository *repo)
1388 const struct got_error *err;
1389 struct got_object_qid *parent_id;
1390 struct tog_view *diff_view;
1392 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1393 if (diff_view == NULL)
1394 return got_error_prefix_errno("view_open");
1396 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1397 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1398 commit_id, log_view, refs, repo);
1399 if (err == NULL)
1400 *new_view = diff_view;
1401 return err;
1404 static const struct got_error *
1405 browse_commit(struct tog_view **new_view, int begin_x,
1406 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1407 struct got_repository *repo)
1409 const struct got_error *err = NULL;
1410 struct got_tree_object *tree;
1411 struct tog_view *tree_view;
1413 err = got_object_open_as_tree(&tree, repo,
1414 got_object_commit_get_tree_id(entry->commit));
1415 if (err)
1416 return err;
1418 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1419 if (tree_view == NULL)
1420 return got_error_prefix_errno("view_open");
1422 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1423 if (err)
1424 got_object_tree_close(tree);
1425 else
1426 *new_view = tree_view;
1427 return err;
1430 static void *
1431 log_thread(void *arg)
1433 const struct got_error *err = NULL;
1434 int errcode = 0;
1435 struct tog_log_thread_args *a = arg;
1436 int done = 0;
1438 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1439 if (err)
1440 return (void *)err;
1442 while (!done && !err) {
1443 err = queue_commits(a->graph, a->commits, 1, a->repo,
1444 a->in_repo_path);
1445 if (err) {
1446 if (err->code != GOT_ERR_ITER_COMPLETED)
1447 return (void *)err;
1448 err = NULL;
1449 done = 1;
1450 } else if (a->commits_needed > 0)
1451 a->commits_needed--;
1453 errcode = pthread_mutex_lock(&tog_mutex);
1454 if (errcode) {
1455 err = got_error_set_errno(errcode,
1456 "pthread_mutex_lock");
1457 break;
1458 } else if (*a->quit)
1459 done = 1;
1460 else if (*a->first_displayed_entry == NULL) {
1461 *a->first_displayed_entry =
1462 TAILQ_FIRST(&a->commits->head);
1463 *a->selected_entry = *a->first_displayed_entry;
1466 if (done)
1467 a->commits_needed = 0;
1468 else if (a->commits_needed == 0) {
1469 errcode = pthread_cond_wait(&a->need_commits,
1470 &tog_mutex);
1471 if (errcode)
1472 err = got_error_set_errno(errcode,
1473 "pthread_cond_wait");
1476 errcode = pthread_mutex_unlock(&tog_mutex);
1477 if (errcode && err == NULL)
1478 err = got_error_set_errno(errcode,
1479 "pthread_mutex_unlock");
1481 a->log_complete = 1;
1482 return (void *)err;
1485 static const struct got_error *
1486 stop_log_thread(struct tog_log_view_state *s)
1488 const struct got_error *err = NULL;
1489 int errcode;
1491 if (s->thread) {
1492 s->quit = 1;
1493 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1494 if (errcode)
1495 return got_error_set_errno(errcode,
1496 "pthread_cond_signal");
1497 errcode = pthread_mutex_unlock(&tog_mutex);
1498 if (errcode)
1499 return got_error_set_errno(errcode,
1500 "pthread_mutex_unlock");
1501 errcode = pthread_join(s->thread, (void **)&err);
1502 if (errcode)
1503 return got_error_set_errno(errcode, "pthread_join");
1504 errcode = pthread_mutex_lock(&tog_mutex);
1505 if (errcode)
1506 return got_error_set_errno(errcode,
1507 "pthread_mutex_lock");
1508 s->thread = NULL;
1511 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1512 if (errcode && err == NULL)
1513 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1515 if (s->thread_args.repo) {
1516 got_repo_close(s->thread_args.repo);
1517 s->thread_args.repo = NULL;
1520 if (s->thread_args.graph) {
1521 got_commit_graph_close(s->thread_args.graph);
1522 s->thread_args.graph = NULL;
1525 return err;
1528 static const struct got_error *
1529 close_log_view(struct tog_view *view)
1531 const struct got_error *err = NULL;
1532 struct tog_log_view_state *s = &view->state.log;
1534 err = stop_log_thread(s);
1535 free_commits(&s->commits);
1536 free(s->in_repo_path);
1537 s->in_repo_path = NULL;
1538 free(s->start_id);
1539 s->start_id = NULL;
1540 return err;
1543 static const struct got_error *
1544 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1545 struct got_reflist_head *refs, struct got_repository *repo,
1546 const char *path, int check_disk)
1548 const struct got_error *err = NULL;
1549 struct tog_log_view_state *s = &view->state.log;
1550 struct got_repository *thread_repo = NULL;
1551 struct got_commit_graph *thread_graph = NULL;
1552 int errcode;
1554 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1555 if (err != NULL)
1556 goto done;
1558 /* The commit queue only contains commits being displayed. */
1559 TAILQ_INIT(&s->commits.head);
1560 s->commits.ncommits = 0;
1562 s->refs = refs;
1563 s->repo = repo;
1564 s->start_id = got_object_id_dup(start_id);
1565 if (s->start_id == NULL) {
1566 err = got_error_prefix_errno("got_object_id_dup");
1567 goto done;
1570 view->show = show_log_view;
1571 view->input = input_log_view;
1572 view->close = close_log_view;
1574 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1575 if (err)
1576 goto done;
1577 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1578 0, thread_repo);
1579 if (err)
1580 goto done;
1582 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1583 if (errcode) {
1584 err = got_error_set_errno(errcode, "pthread_cond_init");
1585 goto done;
1588 s->thread_args.commits_needed = view->nlines;
1589 s->thread_args.graph = thread_graph;
1590 s->thread_args.commits = &s->commits;
1591 s->thread_args.in_repo_path = s->in_repo_path;
1592 s->thread_args.start_id = s->start_id;
1593 s->thread_args.repo = thread_repo;
1594 s->thread_args.log_complete = 0;
1595 s->thread_args.quit = &s->quit;
1596 s->thread_args.view = view;
1597 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1598 s->thread_args.selected_entry = &s->selected_entry;
1599 done:
1600 if (err)
1601 close_log_view(view);
1602 return err;
1605 static const struct got_error *
1606 show_log_view(struct tog_view *view)
1608 struct tog_log_view_state *s = &view->state.log;
1610 if (s->thread == NULL) {
1611 int errcode = pthread_create(&s->thread, NULL, log_thread,
1612 &s->thread_args);
1613 if (errcode)
1614 return got_error_set_errno(errcode, "pthread_create");
1617 return draw_commits(view, &s->last_displayed_entry,
1618 &s->selected_entry, s->first_displayed_entry,
1619 &s->commits, s->selected, view->nlines, s->refs,
1620 s->in_repo_path, s->thread_args.commits_needed);
1623 static const struct got_error *
1624 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1625 struct tog_view **focus_view, struct tog_view *view, int ch)
1627 const struct got_error *err = NULL;
1628 struct tog_log_view_state *s = &view->state.log;
1629 char *parent_path;
1630 struct tog_view *diff_view = NULL, *tree_view = NULL;
1631 int begin_x = 0;
1633 switch (ch) {
1634 case 'q':
1635 s->quit = 1;
1636 break;
1637 case 'k':
1638 case KEY_UP:
1639 case '<':
1640 case ',':
1641 if (s->first_displayed_entry == NULL)
1642 break;
1643 if (s->selected > 0)
1644 s->selected--;
1645 if (s->selected > 0)
1646 break;
1647 scroll_up(view, &s->first_displayed_entry, 1,
1648 &s->commits);
1649 break;
1650 case KEY_PPAGE:
1651 if (s->first_displayed_entry == NULL)
1652 break;
1653 if (TAILQ_FIRST(&s->commits.head) ==
1654 s->first_displayed_entry) {
1655 if (s->selected == 0) {
1656 view_flash(view);
1657 break;
1659 s->selected = 0;
1660 break;
1662 scroll_up(view, &s->first_displayed_entry,
1663 view->nlines, &s->commits);
1664 break;
1665 case 'j':
1666 case KEY_DOWN:
1667 case '>':
1668 case '.':
1669 if (s->first_displayed_entry == NULL)
1670 break;
1671 if (s->selected < MIN(view->nlines - 2,
1672 s->commits.ncommits - 1)) {
1673 s->selected++;
1674 break;
1676 err = scroll_down(view, &s->first_displayed_entry, 1,
1677 &s->last_displayed_entry, &s->commits,
1678 &s->thread_args.log_complete,
1679 &s->thread_args.commits_needed,
1680 &s->thread_args.need_commits);
1681 break;
1682 case KEY_NPAGE: {
1683 struct commit_queue_entry *first;
1684 first = s->first_displayed_entry;
1685 if (first == NULL)
1686 break;
1687 err = scroll_down(view, &s->first_displayed_entry,
1688 view->nlines, &s->last_displayed_entry,
1689 &s->commits, &s->thread_args.log_complete,
1690 &s->thread_args.commits_needed,
1691 &s->thread_args.need_commits);
1692 if (first == s->first_displayed_entry &&
1693 s->selected < MIN(view->nlines - 2,
1694 s->commits.ncommits - 1)) {
1695 /* can't scroll further down */
1696 s->selected = MIN(view->nlines - 2,
1697 s->commits.ncommits - 1);
1699 err = NULL;
1700 break;
1702 case KEY_RESIZE:
1703 if (s->selected > view->nlines - 2)
1704 s->selected = view->nlines - 2;
1705 if (s->selected > s->commits.ncommits - 1)
1706 s->selected = s->commits.ncommits - 1;
1707 break;
1708 case KEY_ENTER:
1709 case '\r':
1710 if (s->selected_entry == NULL)
1711 break;
1712 if (view_is_parent_view(view))
1713 begin_x = view_split_begin_x(view->begin_x);
1714 err = open_diff_view_for_commit(&diff_view, begin_x,
1715 s->selected_entry->commit, s->selected_entry->id,
1716 view, s->refs, s->repo);
1717 if (err)
1718 break;
1719 if (view_is_parent_view(view)) {
1720 err = view_close_child(view);
1721 if (err)
1722 return err;
1723 err = view_set_child(view, diff_view);
1724 if (err) {
1725 view_close(diff_view);
1726 break;
1728 *focus_view = diff_view;
1729 view->child_focussed = 1;
1730 } else
1731 *new_view = diff_view;
1732 break;
1733 case 't':
1734 if (s->selected_entry == NULL)
1735 break;
1736 if (view_is_parent_view(view))
1737 begin_x = view_split_begin_x(view->begin_x);
1738 err = browse_commit(&tree_view, begin_x,
1739 s->selected_entry, s->refs, s->repo);
1740 if (err)
1741 break;
1742 if (view_is_parent_view(view)) {
1743 err = view_close_child(view);
1744 if (err)
1745 return err;
1746 err = view_set_child(view, tree_view);
1747 if (err) {
1748 view_close(tree_view);
1749 break;
1751 *focus_view = tree_view;
1752 view->child_focussed = 1;
1753 } else
1754 *new_view = tree_view;
1755 break;
1756 case KEY_BACKSPACE:
1757 if (strcmp(s->in_repo_path, "/") == 0)
1758 break;
1759 parent_path = dirname(s->in_repo_path);
1760 if (parent_path && strcmp(parent_path, ".") != 0) {
1761 struct tog_view *lv;
1762 err = stop_log_thread(s);
1763 if (err)
1764 return err;
1765 lv = view_open(view->nlines, view->ncols,
1766 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1767 if (lv == NULL)
1768 return got_error_prefix_errno(
1769 "view_open");
1770 err = open_log_view(lv, s->start_id, s->refs,
1771 s->repo, parent_path, 0);
1772 if (err)
1773 return err;;
1774 if (view_is_parent_view(view))
1775 *new_view = lv;
1776 else {
1777 view_set_child(view->parent, lv);
1778 *focus_view = lv;
1780 return NULL;
1782 break;
1783 default:
1784 break;
1787 return err;
1790 static const struct got_error *
1791 apply_unveil(const char *repo_path, const char *worktree_path)
1793 const struct got_error *error;
1795 if (repo_path && unveil(repo_path, "r") != 0)
1796 return got_error_prefix_errno2("unveil", repo_path);
1798 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1799 return got_error_prefix_errno2("unveil", worktree_path);
1801 if (unveil("/tmp", "rwc") != 0)
1802 return got_error_prefix_errno2("unveil", "/tmp");
1804 error = got_privsep_unveil_exec_helpers();
1805 if (error != NULL)
1806 return error;
1808 if (unveil(NULL, NULL) != 0)
1809 return got_error_prefix_errno("unveil");
1811 return NULL;
1814 static void
1815 init_curses(void)
1817 initscr();
1818 cbreak();
1819 halfdelay(1); /* Do fast refresh while initial view is loading. */
1820 noecho();
1821 nonl();
1822 intrflush(stdscr, FALSE);
1823 keypad(stdscr, TRUE);
1824 curs_set(0);
1825 signal(SIGWINCH, tog_sigwinch);
1828 static const struct got_error *
1829 cmd_log(int argc, char *argv[])
1831 const struct got_error *error;
1832 struct got_repository *repo = NULL;
1833 struct got_worktree *worktree = NULL;
1834 struct got_reflist_head refs;
1835 struct got_object_id *start_id = NULL;
1836 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1837 char *start_commit = NULL;
1838 int ch;
1839 struct tog_view *view;
1841 SIMPLEQ_INIT(&refs);
1843 #ifndef PROFILE
1844 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1845 NULL) == -1)
1846 err(1, "pledge");
1847 #endif
1849 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1850 switch (ch) {
1851 case 'c':
1852 start_commit = optarg;
1853 break;
1854 case 'r':
1855 repo_path = realpath(optarg, NULL);
1856 if (repo_path == NULL)
1857 err(1, "-r option");
1858 break;
1859 default:
1860 usage_log();
1861 /* NOTREACHED */
1865 argc -= optind;
1866 argv += optind;
1868 cwd = getcwd(NULL, 0);
1869 if (cwd == NULL) {
1870 error = got_error_prefix_errno("getcwd");
1871 goto done;
1873 error = got_worktree_open(&worktree, cwd);
1874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1875 goto done;
1876 error = NULL;
1878 if (argc == 0) {
1879 path = strdup("");
1880 if (path == NULL) {
1881 error = got_error_prefix_errno("strdup");
1882 goto done;
1884 } else if (argc == 1) {
1885 if (worktree) {
1886 error = got_worktree_resolve_path(&path, worktree,
1887 argv[0]);
1888 if (error)
1889 goto done;
1890 } else {
1891 path = strdup(argv[0]);
1892 if (path == NULL) {
1893 error = got_error_prefix_errno("strdup");
1894 goto done;
1897 } else
1898 usage_log();
1900 repo_path = worktree ?
1901 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1902 if (repo_path == NULL) {
1903 error = got_error_prefix_errno("strdup");
1904 goto done;
1907 init_curses();
1909 error = got_repo_open(&repo, repo_path);
1910 if (error != NULL)
1911 goto done;
1913 error = apply_unveil(got_repo_get_path(repo),
1914 worktree ? got_worktree_get_root_path(worktree) : NULL);
1915 if (error)
1916 goto done;
1918 if (start_commit == NULL)
1919 error = get_head_commit_id(&start_id, repo);
1920 else
1921 error = got_object_resolve_id_str(&start_id, repo,
1922 start_commit);
1923 if (error != NULL)
1924 goto done;
1926 error = got_ref_list(&refs, repo);
1927 if (error)
1928 goto done;
1930 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1931 if (view == NULL) {
1932 error = got_error_prefix_errno("view_open");
1933 goto done;
1935 error = open_log_view(view, start_id, &refs, repo, path, 1);
1936 if (error)
1937 goto done;
1938 error = view_loop(view);
1939 done:
1940 free(repo_path);
1941 free(cwd);
1942 free(path);
1943 free(start_id);
1944 if (repo)
1945 got_repo_close(repo);
1946 if (worktree)
1947 got_worktree_close(worktree);
1948 got_ref_list_free(&refs);
1949 return error;
1952 __dead static void
1953 usage_diff(void)
1955 endwin();
1956 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1957 getprogname());
1958 exit(1);
1961 static char *
1962 parse_next_line(FILE *f, size_t *len)
1964 char *line;
1965 size_t linelen;
1966 size_t lineno;
1967 const char delim[3] = { '\0', '\0', '\0'};
1969 line = fparseln(f, &linelen, &lineno, delim, 0);
1970 if (len)
1971 *len = linelen;
1972 return line;
1975 static const struct got_error *
1976 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1977 int *last_displayed_line, int *eof, int max_lines,
1978 char * header)
1980 const struct got_error *err;
1981 int nlines = 0, nprinted = 0;
1982 char *line;
1983 size_t len;
1984 wchar_t *wline;
1985 int width;
1987 rewind(f);
1988 werase(view->window);
1990 if (header) {
1991 err = format_line(&wline, &width, header, view->ncols);
1992 if (err) {
1993 return err;
1996 if (view_needs_focus_indication(view))
1997 wstandout(view->window);
1998 waddwstr(view->window, wline);
1999 if (view_needs_focus_indication(view))
2000 wstandend(view->window);
2001 if (width < view->ncols)
2002 waddch(view->window, '\n');
2004 if (max_lines <= 1)
2005 return NULL;
2006 max_lines--;
2009 *eof = 0;
2010 while (nprinted < max_lines) {
2011 line = parse_next_line(f, &len);
2012 if (line == NULL) {
2013 *eof = 1;
2014 break;
2016 if (++nlines < *first_displayed_line) {
2017 free(line);
2018 continue;
2021 err = format_line(&wline, &width, line, view->ncols);
2022 if (err) {
2023 free(line);
2024 return err;
2026 waddwstr(view->window, wline);
2027 if (width < view->ncols)
2028 waddch(view->window, '\n');
2029 if (++nprinted == 1)
2030 *first_displayed_line = nlines;
2031 free(line);
2032 free(wline);
2033 wline = NULL;
2035 *last_displayed_line = nlines;
2037 view_vborder(view);
2039 return NULL;
2042 static char *
2043 get_datestr(time_t *time, char *datebuf)
2045 char *p, *s = ctime_r(time, datebuf);
2046 p = strchr(s, '\n');
2047 if (p)
2048 *p = '\0';
2049 return s;
2052 static const struct got_error *
2053 write_commit_info(struct got_object_id *commit_id,
2054 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2056 const struct got_error *err = NULL;
2057 char datebuf[26];
2058 struct got_commit_object *commit;
2059 char *id_str = NULL;
2060 time_t committer_time;
2061 const char *author, *committer;
2062 char *refs_str = NULL;
2064 if (refs) {
2065 err = build_refs_str(&refs_str, refs, commit_id);
2066 if (err)
2067 return err;
2070 err = got_object_open_as_commit(&commit, repo, commit_id);
2071 if (err)
2072 return err;
2074 err = got_object_id_str(&id_str, commit_id);
2075 if (err) {
2076 err = got_error_prefix_errno("got_object_id_str");
2077 goto done;
2080 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2081 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2082 err = got_error_prefix_errno("fprintf");
2083 goto done;
2085 if (fprintf(outfile, "from: %s\n",
2086 got_object_commit_get_author(commit)) < 0) {
2087 err = got_error_prefix_errno("fprintf");
2088 goto done;
2090 committer_time = got_object_commit_get_committer_time(commit);
2091 if (fprintf(outfile, "date: %s UTC\n",
2092 get_datestr(&committer_time, datebuf)) < 0) {
2093 err = got_error_prefix_errno("fprintf");
2094 goto done;
2096 author = got_object_commit_get_author(commit);
2097 committer = got_object_commit_get_committer(commit);
2098 if (strcmp(author, committer) != 0 &&
2099 fprintf(outfile, "via: %s\n", committer) < 0) {
2100 err = got_error_prefix_errno("fprintf");
2101 goto done;
2103 if (fprintf(outfile, "%s\n",
2104 got_object_commit_get_logmsg(commit)) < 0) {
2105 err = got_error_prefix_errno("fprintf");
2106 goto done;
2108 done:
2109 free(id_str);
2110 free(refs_str);
2111 got_object_commit_close(commit);
2112 return err;
2115 static const struct got_error *
2116 create_diff(struct tog_diff_view_state *s)
2118 const struct got_error *err = NULL;
2119 FILE *f = NULL;
2120 int obj_type;
2122 f = got_opentemp();
2123 if (f == NULL) {
2124 err = got_error_prefix_errno("got_opentemp");
2125 goto done;
2127 if (s->f && fclose(s->f) != 0) {
2128 err = got_error_prefix_errno("fclose");
2129 goto done;
2131 s->f = f;
2133 if (s->id1)
2134 err = got_object_get_type(&obj_type, s->repo, s->id1);
2135 else
2136 err = got_object_get_type(&obj_type, s->repo, s->id2);
2137 if (err)
2138 goto done;
2140 switch (obj_type) {
2141 case GOT_OBJ_TYPE_BLOB:
2142 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2143 s->diff_context, s->repo, f);
2144 break;
2145 case GOT_OBJ_TYPE_TREE:
2146 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2147 s->diff_context, s->repo, f);
2148 break;
2149 case GOT_OBJ_TYPE_COMMIT: {
2150 const struct got_object_id_queue *parent_ids;
2151 struct got_object_qid *pid;
2152 struct got_commit_object *commit2;
2154 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2155 if (err)
2156 break;
2157 /* Show commit info if we're diffing to a parent/root commit. */
2158 if (s->id1 == NULL)
2159 write_commit_info(s->id2, s->refs, s->repo, f);
2160 else {
2161 parent_ids = got_object_commit_get_parent_ids(commit2);
2162 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2163 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2164 write_commit_info(s->id2, s->refs,
2165 s->repo, f);
2166 break;
2170 got_object_commit_close(commit2);
2172 err = got_diff_objects_as_commits(s->id1, s->id2,
2173 s->diff_context, s->repo, f);
2174 break;
2176 default:
2177 err = got_error(GOT_ERR_OBJ_TYPE);
2178 break;
2180 done:
2181 if (f && fflush(f) != 0 && err == NULL)
2182 err = got_error_prefix_errno("fflush");
2183 return err;
2186 static void
2187 diff_view_indicate_progress(struct tog_view *view)
2189 werase(view->window);
2190 waddstr(view->window, "diffing...");
2191 update_panels();
2192 doupdate();
2195 static const struct got_error *
2196 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2197 struct got_object_id *id2, struct tog_view *log_view,
2198 struct got_reflist_head *refs, struct got_repository *repo)
2200 const struct got_error *err;
2202 if (id1 != NULL && id2 != NULL) {
2203 int type1, type2;
2204 err = got_object_get_type(&type1, repo, id1);
2205 if (err)
2206 return err;
2207 err = got_object_get_type(&type2, repo, id2);
2208 if (err)
2209 return err;
2211 if (type1 != type2)
2212 return got_error(GOT_ERR_OBJ_TYPE);
2215 if (id1) {
2216 view->state.diff.id1 = got_object_id_dup(id1);
2217 if (view->state.diff.id1 == NULL)
2218 return got_error_prefix_errno("got_object_id_dup");
2219 } else
2220 view->state.diff.id1 = NULL;
2222 view->state.diff.id2 = got_object_id_dup(id2);
2223 if (view->state.diff.id2 == NULL) {
2224 free(view->state.diff.id1);
2225 view->state.diff.id1 = NULL;
2226 return got_error_prefix_errno("got_object_id_dup");
2228 view->state.diff.f = NULL;
2229 view->state.diff.first_displayed_line = 1;
2230 view->state.diff.last_displayed_line = view->nlines;
2231 view->state.diff.diff_context = 3;
2232 view->state.diff.log_view = log_view;
2233 view->state.diff.repo = repo;
2234 view->state.diff.refs = refs;
2236 if (log_view && view_is_splitscreen(view))
2237 show_log_view(log_view); /* draw vborder */
2238 diff_view_indicate_progress(view);
2240 err = create_diff(&view->state.diff);
2241 if (err) {
2242 free(view->state.diff.id1);
2243 view->state.diff.id1 = NULL;
2244 free(view->state.diff.id2);
2245 view->state.diff.id2 = NULL;
2246 return err;
2249 view->show = show_diff_view;
2250 view->input = input_diff_view;
2251 view->close = close_diff_view;
2253 return NULL;
2256 static const struct got_error *
2257 close_diff_view(struct tog_view *view)
2259 const struct got_error *err = NULL;
2261 free(view->state.diff.id1);
2262 view->state.diff.id1 = NULL;
2263 free(view->state.diff.id2);
2264 view->state.diff.id2 = NULL;
2265 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2266 err = got_error_prefix_errno("fclose");
2267 return err;
2270 static const struct got_error *
2271 show_diff_view(struct tog_view *view)
2273 const struct got_error *err;
2274 struct tog_diff_view_state *s = &view->state.diff;
2275 char *id_str1 = NULL, *id_str2, *header;
2277 if (s->id1) {
2278 err = got_object_id_str(&id_str1, s->id1);
2279 if (err)
2280 return err;
2282 err = got_object_id_str(&id_str2, s->id2);
2283 if (err)
2284 return err;
2286 if (asprintf(&header, "diff %s %s",
2287 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2288 err = got_error_prefix_errno("asprintf");
2289 free(id_str1);
2290 free(id_str2);
2291 return err;
2293 free(id_str1);
2294 free(id_str2);
2296 return draw_file(view, s->f, &s->first_displayed_line,
2297 &s->last_displayed_line, &s->eof, view->nlines,
2298 header);
2301 static const struct got_error *
2302 set_selected_commit(struct tog_diff_view_state *s,
2303 struct commit_queue_entry *entry)
2305 const struct got_error *err;
2306 const struct got_object_id_queue *parent_ids;
2307 struct got_commit_object *selected_commit;
2308 struct got_object_qid *pid;
2310 free(s->id2);
2311 s->id2 = got_object_id_dup(entry->id);
2312 if (s->id2 == NULL)
2313 return got_error_prefix_errno("got_object_id_dup");
2315 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2316 if (err)
2317 return err;
2318 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2319 free(s->id1);
2320 pid = SIMPLEQ_FIRST(parent_ids);
2321 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2322 got_object_commit_close(selected_commit);
2323 return NULL;
2326 static const struct got_error *
2327 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2328 struct tog_view **focus_view, struct tog_view *view, int ch)
2330 const struct got_error *err = NULL;
2331 struct tog_diff_view_state *s = &view->state.diff;
2332 struct tog_log_view_state *ls;
2333 struct commit_queue_entry *entry;
2334 int i;
2336 switch (ch) {
2337 case 'k':
2338 case KEY_UP:
2339 if (s->first_displayed_line > 1)
2340 s->first_displayed_line--;
2341 else
2342 view_flash(view);
2343 break;
2344 case KEY_PPAGE:
2345 if (s->first_displayed_line == 1) {
2346 view_flash(view);
2347 break;
2349 i = 0;
2350 while (i++ < view->nlines - 1 &&
2351 s->first_displayed_line > 1)
2352 s->first_displayed_line--;
2353 break;
2354 case 'j':
2355 case KEY_DOWN:
2356 if (!s->eof)
2357 s->first_displayed_line++;
2358 else
2359 view_flash(view);
2360 break;
2361 case KEY_NPAGE:
2362 case ' ':
2363 if (s->eof) {
2364 view_flash(view);
2365 break;
2367 i = 0;
2368 while (!s->eof && i++ < view->nlines - 1) {
2369 char *line;
2370 line = parse_next_line(s->f, NULL);
2371 s->first_displayed_line++;
2372 if (line == NULL)
2373 break;
2375 break;
2376 case '[':
2377 if (s->diff_context > 0) {
2378 s->diff_context--;
2379 diff_view_indicate_progress(view);
2380 err = create_diff(s);
2382 break;
2383 case ']':
2384 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2385 s->diff_context++;
2386 diff_view_indicate_progress(view);
2387 err = create_diff(s);
2389 break;
2390 case '<':
2391 case ',':
2392 if (s->log_view == NULL)
2393 break;
2394 ls = &s->log_view->state.log;
2395 entry = TAILQ_PREV(ls->selected_entry,
2396 commit_queue_head, entry);
2397 if (entry == NULL)
2398 break;
2400 err = input_log_view(NULL, NULL, NULL, s->log_view,
2401 KEY_UP);
2402 if (err)
2403 break;
2405 err = set_selected_commit(s, entry);
2406 if (err)
2407 break;
2409 s->first_displayed_line = 1;
2410 s->last_displayed_line = view->nlines;
2412 diff_view_indicate_progress(view);
2413 err = create_diff(s);
2414 break;
2415 case '>':
2416 case '.':
2417 if (s->log_view == NULL)
2418 break;
2419 ls = &s->log_view->state.log;
2421 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2422 ls->thread_args.commits_needed++;
2424 /* Display "loading..." in log view. */
2425 show_log_view(s->log_view);
2426 update_panels();
2427 doupdate();
2429 err = trigger_log_thread(1 /* load_all */,
2430 &ls->thread_args.commits_needed,
2431 &ls->thread_args.log_complete,
2432 &ls->thread_args.need_commits);
2433 if (err)
2434 break;
2436 err = input_log_view(NULL, NULL, NULL, s->log_view,
2437 KEY_DOWN);
2438 if (err)
2439 break;
2441 entry = TAILQ_NEXT(ls->selected_entry, entry);
2442 if (entry == NULL)
2443 break;
2445 err = set_selected_commit(s, entry);
2446 if (err)
2447 break;
2449 s->first_displayed_line = 1;
2450 s->last_displayed_line = view->nlines;
2452 diff_view_indicate_progress(view);
2453 err = create_diff(s);
2454 break;
2455 default:
2456 break;
2459 return err;
2462 static const struct got_error *
2463 cmd_diff(int argc, char *argv[])
2465 const struct got_error *error = NULL;
2466 struct got_repository *repo = NULL;
2467 struct got_reflist_head refs;
2468 struct got_object_id *id1 = NULL, *id2 = NULL;
2469 char *repo_path = NULL;
2470 char *id_str1 = NULL, *id_str2 = NULL;
2471 int ch;
2472 struct tog_view *view;
2474 SIMPLEQ_INIT(&refs);
2476 #ifndef PROFILE
2477 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2478 NULL) == -1)
2479 err(1, "pledge");
2480 #endif
2482 while ((ch = getopt(argc, argv, "")) != -1) {
2483 switch (ch) {
2484 default:
2485 usage_diff();
2486 /* NOTREACHED */
2490 argc -= optind;
2491 argv += optind;
2493 if (argc == 0) {
2494 usage_diff(); /* TODO show local worktree changes */
2495 } else if (argc == 2) {
2496 repo_path = getcwd(NULL, 0);
2497 if (repo_path == NULL)
2498 return got_error_prefix_errno("getcwd");
2499 id_str1 = argv[0];
2500 id_str2 = argv[1];
2501 } else if (argc == 3) {
2502 repo_path = realpath(argv[0], NULL);
2503 if (repo_path == NULL)
2504 return got_error_prefix_errno2("realpath", argv[0]);
2505 id_str1 = argv[1];
2506 id_str2 = argv[2];
2507 } else
2508 usage_diff();
2510 init_curses();
2512 error = got_repo_open(&repo, repo_path);
2513 if (error)
2514 goto done;
2516 error = apply_unveil(got_repo_get_path(repo), NULL);
2517 if (error)
2518 goto done;
2520 error = got_object_resolve_id_str(&id1, repo, id_str1);
2521 if (error)
2522 goto done;
2524 error = got_object_resolve_id_str(&id2, repo, id_str2);
2525 if (error)
2526 goto done;
2528 error = got_ref_list(&refs, repo);
2529 if (error)
2530 goto done;
2532 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2533 if (view == NULL) {
2534 error = got_error_prefix_errno("view_open");
2535 goto done;
2537 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2538 if (error)
2539 goto done;
2540 error = view_loop(view);
2541 done:
2542 free(repo_path);
2543 got_repo_close(repo);
2544 got_ref_list_free(&refs);
2545 return error;
2548 __dead static void
2549 usage_blame(void)
2551 endwin();
2552 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2553 getprogname());
2554 exit(1);
2557 struct tog_blame_line {
2558 int annotated;
2559 struct got_object_id *id;
2562 static const struct got_error *
2563 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2564 const char *path, struct tog_blame_line *lines, int nlines,
2565 int blame_complete, int selected_line, int *first_displayed_line,
2566 int *last_displayed_line, int *eof, int max_lines)
2568 const struct got_error *err;
2569 int lineno = 0, nprinted = 0;
2570 char *line;
2571 size_t len;
2572 wchar_t *wline;
2573 int width, wlimit;
2574 struct tog_blame_line *blame_line;
2575 struct got_object_id *prev_id = NULL;
2576 char *id_str;
2578 err = got_object_id_str(&id_str, id);
2579 if (err)
2580 return err;
2582 rewind(f);
2583 werase(view->window);
2585 if (asprintf(&line, "commit %s", id_str) == -1) {
2586 err = got_error_prefix_errno("asprintf");
2587 free(id_str);
2588 return err;
2591 err = format_line(&wline, &width, line, view->ncols);
2592 free(line);
2593 line = NULL;
2594 if (view_needs_focus_indication(view))
2595 wstandout(view->window);
2596 waddwstr(view->window, wline);
2597 if (view_needs_focus_indication(view))
2598 wstandend(view->window);
2599 free(wline);
2600 wline = NULL;
2601 if (width < view->ncols)
2602 waddch(view->window, '\n');
2604 if (asprintf(&line, "[%d/%d] %s%s",
2605 *first_displayed_line - 1 + selected_line, nlines,
2606 blame_complete ? "" : "annotating... ", path) == -1) {
2607 free(id_str);
2608 return got_error_prefix_errno("asprintf");
2610 free(id_str);
2611 err = format_line(&wline, &width, line, view->ncols);
2612 free(line);
2613 line = NULL;
2614 if (err)
2615 return err;
2616 waddwstr(view->window, wline);
2617 free(wline);
2618 wline = NULL;
2619 if (width < view->ncols)
2620 waddch(view->window, '\n');
2622 *eof = 0;
2623 while (nprinted < max_lines - 2) {
2624 line = parse_next_line(f, &len);
2625 if (line == NULL) {
2626 *eof = 1;
2627 break;
2629 if (++lineno < *first_displayed_line) {
2630 free(line);
2631 continue;
2634 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2635 err = format_line(&wline, &width, line, wlimit);
2636 if (err) {
2637 free(line);
2638 return err;
2641 if (view->focussed && nprinted == selected_line - 1)
2642 wstandout(view->window);
2644 blame_line = &lines[lineno - 1];
2645 if (blame_line->annotated && prev_id &&
2646 got_object_id_cmp(prev_id, blame_line->id) == 0)
2647 waddstr(view->window, " ");
2648 else if (blame_line->annotated) {
2649 char *id_str;
2650 err = got_object_id_str(&id_str, blame_line->id);
2651 if (err) {
2652 free(line);
2653 free(wline);
2654 return err;
2656 wprintw(view->window, "%.8s ", id_str);
2657 free(id_str);
2658 prev_id = blame_line->id;
2659 } else {
2660 waddstr(view->window, "........ ");
2661 prev_id = NULL;
2664 waddwstr(view->window, wline);
2665 while (width < wlimit) {
2666 waddch(view->window, ' ');
2667 width++;
2669 if (view->focussed && nprinted == selected_line - 1)
2670 wstandend(view->window);
2671 if (++nprinted == 1)
2672 *first_displayed_line = lineno;
2673 free(line);
2674 free(wline);
2675 wline = NULL;
2677 *last_displayed_line = lineno;
2679 view_vborder(view);
2681 return NULL;
2684 static const struct got_error *
2685 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2687 const struct got_error *err = NULL;
2688 struct tog_blame_cb_args *a = arg;
2689 struct tog_blame_line *line;
2690 int errcode;
2692 if (nlines != a->nlines ||
2693 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2694 return got_error(GOT_ERR_RANGE);
2696 errcode = pthread_mutex_lock(&tog_mutex);
2697 if (errcode)
2698 return got_error_set_errno(errcode, "pthread_mutex_lock");
2700 if (*a->quit) { /* user has quit the blame view */
2701 err = got_error(GOT_ERR_ITER_COMPLETED);
2702 goto done;
2705 if (lineno == -1)
2706 goto done; /* no change in this commit */
2708 line = &a->lines[lineno - 1];
2709 if (line->annotated)
2710 goto done;
2712 line->id = got_object_id_dup(id);
2713 if (line->id == NULL) {
2714 err = got_error_prefix_errno("got_object_id_dup");
2715 goto done;
2717 line->annotated = 1;
2718 done:
2719 errcode = pthread_mutex_unlock(&tog_mutex);
2720 if (errcode)
2721 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2722 return err;
2725 static void *
2726 blame_thread(void *arg)
2728 const struct got_error *err;
2729 struct tog_blame_thread_args *ta = arg;
2730 struct tog_blame_cb_args *a = ta->cb_args;
2731 int errcode;
2733 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2734 blame_cb, ta->cb_args);
2736 errcode = pthread_mutex_lock(&tog_mutex);
2737 if (errcode)
2738 return (void *)got_error_set_errno(errcode,
2739 "pthread_mutex_lock");
2741 got_repo_close(ta->repo);
2742 ta->repo = NULL;
2743 *ta->complete = 1;
2745 errcode = pthread_mutex_unlock(&tog_mutex);
2746 if (errcode && err == NULL)
2747 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2749 return (void *)err;
2752 static struct got_object_id *
2753 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2754 int selected_line)
2756 struct tog_blame_line *line;
2758 line = &lines[first_displayed_line - 1 + selected_line - 1];
2759 if (!line->annotated)
2760 return NULL;
2762 return line->id;
2765 static const struct got_error *
2766 stop_blame(struct tog_blame *blame)
2768 const struct got_error *err = NULL;
2769 int i;
2771 if (blame->thread) {
2772 int errcode;
2773 errcode = pthread_mutex_unlock(&tog_mutex);
2774 if (errcode)
2775 return got_error_set_errno(errcode,
2776 "pthread_mutex_unlock");
2777 errcode = pthread_join(blame->thread, (void **)&err);
2778 if (errcode)
2779 return got_error_set_errno(errcode, "pthread_join");
2780 errcode = pthread_mutex_lock(&tog_mutex);
2781 if (errcode)
2782 return got_error_set_errno(errcode,
2783 "pthread_mutex_lock");
2784 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2785 err = NULL;
2786 blame->thread = NULL;
2788 if (blame->thread_args.repo) {
2789 got_repo_close(blame->thread_args.repo);
2790 blame->thread_args.repo = NULL;
2792 if (blame->f) {
2793 if (fclose(blame->f) != 0 && err == NULL)
2794 err = got_error_prefix_errno("fclose");
2795 blame->f = NULL;
2797 if (blame->lines) {
2798 for (i = 0; i < blame->nlines; i++)
2799 free(blame->lines[i].id);
2800 free(blame->lines);
2801 blame->lines = NULL;
2803 free(blame->cb_args.commit_id);
2804 blame->cb_args.commit_id = NULL;
2806 return err;
2809 static const struct got_error *
2810 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2811 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2812 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2813 struct got_repository *repo)
2815 const struct got_error *err = NULL;
2816 struct got_blob_object *blob = NULL;
2817 struct got_repository *thread_repo = NULL;
2818 struct got_object_id *obj_id = NULL;
2819 int obj_type;
2821 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2822 if (err)
2823 return err;
2824 if (obj_id == NULL)
2825 return got_error(GOT_ERR_NO_OBJ);
2827 err = got_object_get_type(&obj_type, repo, obj_id);
2828 if (err)
2829 goto done;
2831 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2832 err = got_error(GOT_ERR_OBJ_TYPE);
2833 goto done;
2836 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2837 if (err)
2838 goto done;
2839 blame->f = got_opentemp();
2840 if (blame->f == NULL) {
2841 err = got_error_prefix_errno("got_opentemp");
2842 goto done;
2844 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2845 blame->f, blob);
2846 if (err)
2847 goto done;
2849 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2850 if (blame->lines == NULL) {
2851 err = got_error_prefix_errno("calloc");
2852 goto done;
2855 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2856 if (err)
2857 goto done;
2859 blame->cb_args.view = view;
2860 blame->cb_args.lines = blame->lines;
2861 blame->cb_args.nlines = blame->nlines;
2862 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2863 if (blame->cb_args.commit_id == NULL) {
2864 err = got_error_prefix_errno("got_object_id_dup");
2865 goto done;
2867 blame->cb_args.quit = done;
2869 blame->thread_args.path = path;
2870 blame->thread_args.repo = thread_repo;
2871 blame->thread_args.cb_args = &blame->cb_args;
2872 blame->thread_args.complete = blame_complete;
2873 *blame_complete = 0;
2875 done:
2876 if (blob)
2877 got_object_blob_close(blob);
2878 free(obj_id);
2879 if (err)
2880 stop_blame(blame);
2881 return err;
2884 static const struct got_error *
2885 open_blame_view(struct tog_view *view, char *path,
2886 struct got_object_id *commit_id, struct got_reflist_head *refs,
2887 struct got_repository *repo)
2889 const struct got_error *err = NULL;
2890 struct tog_blame_view_state *s = &view->state.blame;
2892 SIMPLEQ_INIT(&s->blamed_commits);
2894 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2895 if (err)
2896 return err;
2898 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2899 s->first_displayed_line = 1;
2900 s->last_displayed_line = view->nlines;
2901 s->selected_line = 1;
2902 s->blame_complete = 0;
2903 s->path = path;
2904 if (s->path == NULL)
2905 return got_error_prefix_errno("open_blame_view");
2906 s->repo = repo;
2907 s->refs = refs;
2908 s->commit_id = commit_id;
2909 memset(&s->blame, 0, sizeof(s->blame));
2911 view->show = show_blame_view;
2912 view->input = input_blame_view;
2913 view->close = close_blame_view;
2915 return run_blame(&s->blame, view, &s->blame_complete,
2916 &s->first_displayed_line, &s->last_displayed_line,
2917 &s->selected_line, &s->done, &s->eof, s->path,
2918 s->blamed_commit->id, s->repo);
2921 static const struct got_error *
2922 close_blame_view(struct tog_view *view)
2924 const struct got_error *err = NULL;
2925 struct tog_blame_view_state *s = &view->state.blame;
2927 if (s->blame.thread)
2928 err = stop_blame(&s->blame);
2930 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2931 struct got_object_qid *blamed_commit;
2932 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2933 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2934 got_object_qid_free(blamed_commit);
2937 free(s->path);
2939 return err;
2942 static const struct got_error *
2943 show_blame_view(struct tog_view *view)
2945 const struct got_error *err = NULL;
2946 struct tog_blame_view_state *s = &view->state.blame;
2947 int errcode;
2949 if (s->blame.thread == NULL) {
2950 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2951 &s->blame.thread_args);
2952 if (errcode)
2953 return got_error_set_errno(errcode, "pthread_create");
2956 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2957 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2958 s->selected_line, &s->first_displayed_line,
2959 &s->last_displayed_line, &s->eof, view->nlines);
2961 view_vborder(view);
2962 return err;
2965 static const struct got_error *
2966 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2967 struct tog_view **focus_view, struct tog_view *view, int ch)
2969 const struct got_error *err = NULL, *thread_err = NULL;
2970 struct tog_view *diff_view;
2971 struct tog_blame_view_state *s = &view->state.blame;
2972 int begin_x = 0;
2974 switch (ch) {
2975 case 'q':
2976 s->done = 1;
2977 break;
2978 case 'k':
2979 case KEY_UP:
2980 if (s->selected_line > 1)
2981 s->selected_line--;
2982 else if (s->selected_line == 1 &&
2983 s->first_displayed_line > 1)
2984 s->first_displayed_line--;
2985 else
2986 view_flash(view);
2987 break;
2988 case KEY_PPAGE:
2989 if (s->first_displayed_line == 1) {
2990 if (s->selected_line == 1) {
2991 view_flash(view);
2992 break;
2994 s->selected_line = 1;
2995 break;
2997 if (s->first_displayed_line > view->nlines - 2)
2998 s->first_displayed_line -=
2999 (view->nlines - 2);
3000 else
3001 s->first_displayed_line = 1;
3002 break;
3003 case 'j':
3004 case KEY_DOWN:
3005 if (s->selected_line < view->nlines - 2 &&
3006 s->first_displayed_line +
3007 s->selected_line <= s->blame.nlines)
3008 s->selected_line++;
3009 else if (s->last_displayed_line <
3010 s->blame.nlines)
3011 s->first_displayed_line++;
3012 else
3013 view_flash(view);
3014 break;
3015 case 'b':
3016 case 'p': {
3017 struct got_object_id *id = NULL;
3018 id = get_selected_commit_id(s->blame.lines,
3019 s->first_displayed_line, s->selected_line);
3020 if (id == NULL)
3021 break;
3022 if (ch == 'p') {
3023 struct got_commit_object *commit;
3024 struct got_object_qid *pid;
3025 struct got_object_id *blob_id = NULL;
3026 int obj_type;
3027 err = got_object_open_as_commit(&commit,
3028 s->repo, id);
3029 if (err)
3030 break;
3031 pid = SIMPLEQ_FIRST(
3032 got_object_commit_get_parent_ids(commit));
3033 if (pid == NULL) {
3034 got_object_commit_close(commit);
3035 break;
3037 /* Check if path history ends here. */
3038 err = got_object_id_by_path(&blob_id, s->repo,
3039 pid->id, s->path);
3040 if (err) {
3041 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3042 err = NULL;
3043 got_object_commit_close(commit);
3044 break;
3046 err = got_object_get_type(&obj_type, s->repo,
3047 blob_id);
3048 free(blob_id);
3049 /* Can't blame non-blob type objects. */
3050 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3051 got_object_commit_close(commit);
3052 break;
3054 err = got_object_qid_alloc(&s->blamed_commit,
3055 pid->id);
3056 got_object_commit_close(commit);
3057 } else {
3058 if (got_object_id_cmp(id,
3059 s->blamed_commit->id) == 0)
3060 break;
3061 err = got_object_qid_alloc(&s->blamed_commit,
3062 id);
3064 if (err)
3065 break;
3066 s->done = 1;
3067 thread_err = stop_blame(&s->blame);
3068 s->done = 0;
3069 if (thread_err)
3070 break;
3071 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3072 s->blamed_commit, entry);
3073 err = run_blame(&s->blame, view, &s->blame_complete,
3074 &s->first_displayed_line, &s->last_displayed_line,
3075 &s->selected_line, &s->done, &s->eof,
3076 s->path, s->blamed_commit->id, s->repo);
3077 if (err)
3078 break;
3079 break;
3081 case 'B': {
3082 struct got_object_qid *first;
3083 first = SIMPLEQ_FIRST(&s->blamed_commits);
3084 if (!got_object_id_cmp(first->id, s->commit_id))
3085 break;
3086 s->done = 1;
3087 thread_err = stop_blame(&s->blame);
3088 s->done = 0;
3089 if (thread_err)
3090 break;
3091 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3092 got_object_qid_free(s->blamed_commit);
3093 s->blamed_commit =
3094 SIMPLEQ_FIRST(&s->blamed_commits);
3095 err = run_blame(&s->blame, view, &s->blame_complete,
3096 &s->first_displayed_line, &s->last_displayed_line,
3097 &s->selected_line, &s->done, &s->eof, s->path,
3098 s->blamed_commit->id, s->repo);
3099 if (err)
3100 break;
3101 break;
3103 case KEY_ENTER:
3104 case '\r': {
3105 struct got_object_id *id = NULL;
3106 struct got_object_qid *pid;
3107 struct got_commit_object *commit = NULL;
3108 id = get_selected_commit_id(s->blame.lines,
3109 s->first_displayed_line, s->selected_line);
3110 if (id == NULL)
3111 break;
3112 err = got_object_open_as_commit(&commit, s->repo, id);
3113 if (err)
3114 break;
3115 pid = SIMPLEQ_FIRST(
3116 got_object_commit_get_parent_ids(commit));
3117 if (view_is_parent_view(view))
3118 begin_x = view_split_begin_x(view->begin_x);
3119 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3120 if (diff_view == NULL) {
3121 got_object_commit_close(commit);
3122 err = got_error_prefix_errno("view_open");
3123 break;
3125 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3126 id, NULL, s->refs, s->repo);
3127 got_object_commit_close(commit);
3128 if (err) {
3129 view_close(diff_view);
3130 break;
3132 if (view_is_parent_view(view)) {
3133 err = view_close_child(view);
3134 if (err)
3135 break;
3136 err = view_set_child(view, diff_view);
3137 if (err) {
3138 view_close(diff_view);
3139 break;
3141 *focus_view = diff_view;
3142 view->child_focussed = 1;
3143 } else
3144 *new_view = diff_view;
3145 if (err)
3146 break;
3147 break;
3149 case KEY_NPAGE:
3150 case ' ':
3151 if (s->last_displayed_line >= s->blame.nlines &&
3152 s->selected_line >= MIN(s->blame.nlines,
3153 view->nlines - 2)) {
3154 view_flash(view);
3155 break;
3157 if (s->last_displayed_line >= s->blame.nlines &&
3158 s->selected_line < view->nlines - 2) {
3159 s->selected_line = MIN(s->blame.nlines,
3160 view->nlines - 2);
3161 break;
3163 if (s->last_displayed_line + view->nlines - 2
3164 <= s->blame.nlines)
3165 s->first_displayed_line +=
3166 view->nlines - 2;
3167 else
3168 s->first_displayed_line =
3169 s->blame.nlines -
3170 (view->nlines - 3);
3171 break;
3172 case KEY_RESIZE:
3173 if (s->selected_line > view->nlines - 2) {
3174 s->selected_line = MIN(s->blame.nlines,
3175 view->nlines - 2);
3177 break;
3178 default:
3179 break;
3181 return thread_err ? thread_err : err;
3184 static const struct got_error *
3185 cmd_blame(int argc, char *argv[])
3187 const struct got_error *error;
3188 struct got_repository *repo = NULL;
3189 struct got_reflist_head refs;
3190 struct got_worktree *worktree = NULL;
3191 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3192 struct got_object_id *commit_id = NULL;
3193 char *commit_id_str = NULL;
3194 int ch;
3195 struct tog_view *view;
3197 SIMPLEQ_INIT(&refs);
3199 #ifndef PROFILE
3200 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3201 NULL) == -1)
3202 err(1, "pledge");
3203 #endif
3205 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3206 switch (ch) {
3207 case 'c':
3208 commit_id_str = optarg;
3209 break;
3210 case 'r':
3211 repo_path = realpath(optarg, NULL);
3212 if (repo_path == NULL)
3213 err(1, "-r option");
3214 break;
3215 default:
3216 usage_blame();
3217 /* NOTREACHED */
3221 argc -= optind;
3222 argv += optind;
3224 if (argc == 1)
3225 path = argv[0];
3226 else
3227 usage_blame();
3229 cwd = getcwd(NULL, 0);
3230 if (cwd == NULL) {
3231 error = got_error_prefix_errno("getcwd");
3232 goto done;
3234 if (repo_path == NULL) {
3235 error = got_worktree_open(&worktree, cwd);
3236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3237 goto done;
3238 else
3239 error = NULL;
3240 if (worktree) {
3241 repo_path =
3242 strdup(got_worktree_get_repo_path(worktree));
3243 if (repo_path == NULL)
3244 error = got_error_prefix_errno("strdup");
3245 if (error)
3246 goto done;
3247 } else {
3248 repo_path = strdup(cwd);
3249 if (repo_path == NULL) {
3250 error = got_error_prefix_errno("strdup");
3251 goto done;
3256 init_curses();
3258 error = got_repo_open(&repo, repo_path);
3259 if (error != NULL)
3260 goto done;
3262 error = apply_unveil(got_repo_get_path(repo), NULL);
3263 if (error)
3264 goto done;
3266 if (worktree) {
3267 const char *prefix = got_worktree_get_path_prefix(worktree);
3268 char *p, *worktree_subdir = cwd +
3269 strlen(got_worktree_get_root_path(worktree));
3270 if (asprintf(&p, "%s%s%s%s%s",
3271 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3272 worktree_subdir, worktree_subdir[0] ? "/" : "",
3273 path) == -1) {
3274 error = got_error_prefix_errno("asprintf");
3275 goto done;
3277 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3278 free(p);
3279 } else {
3280 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3282 if (error)
3283 goto done;
3285 if (commit_id_str == NULL) {
3286 struct got_reference *head_ref;
3287 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3288 if (error != NULL)
3289 goto done;
3290 error = got_ref_resolve(&commit_id, repo, head_ref);
3291 got_ref_close(head_ref);
3292 } else {
3293 error = got_object_resolve_id_str(&commit_id, repo,
3294 commit_id_str);
3296 if (error != NULL)
3297 goto done;
3299 error = got_ref_list(&refs, repo);
3300 if (error)
3301 goto done;
3303 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3304 if (view == NULL) {
3305 error = got_error_prefix_errno("view_open");
3306 goto done;
3308 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3309 if (error)
3310 goto done;
3311 error = view_loop(view);
3312 done:
3313 free(repo_path);
3314 free(cwd);
3315 free(commit_id);
3316 if (worktree)
3317 got_worktree_close(worktree);
3318 if (repo)
3319 got_repo_close(repo);
3320 got_ref_list_free(&refs);
3321 return error;
3324 static const struct got_error *
3325 draw_tree_entries(struct tog_view *view,
3326 struct got_tree_entry **first_displayed_entry,
3327 struct got_tree_entry **last_displayed_entry,
3328 struct got_tree_entry **selected_entry, int *ndisplayed,
3329 const char *label, int show_ids, const char *parent_path,
3330 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3332 const struct got_error *err = NULL;
3333 struct got_tree_entry *te;
3334 wchar_t *wline;
3335 int width, n;
3337 *ndisplayed = 0;
3339 werase(view->window);
3341 if (limit == 0)
3342 return NULL;
3344 err = format_line(&wline, &width, label, view->ncols);
3345 if (err)
3346 return err;
3347 if (view_needs_focus_indication(view))
3348 wstandout(view->window);
3349 waddwstr(view->window, wline);
3350 if (view_needs_focus_indication(view))
3351 wstandend(view->window);
3352 free(wline);
3353 wline = NULL;
3354 if (width < view->ncols)
3355 waddch(view->window, '\n');
3356 if (--limit <= 0)
3357 return NULL;
3358 err = format_line(&wline, &width, parent_path, view->ncols);
3359 if (err)
3360 return err;
3361 waddwstr(view->window, wline);
3362 free(wline);
3363 wline = NULL;
3364 if (width < view->ncols)
3365 waddch(view->window, '\n');
3366 if (--limit <= 0)
3367 return NULL;
3368 waddch(view->window, '\n');
3369 if (--limit <= 0)
3370 return NULL;
3372 te = SIMPLEQ_FIRST(&entries->head);
3373 if (*first_displayed_entry == NULL) {
3374 if (selected == 0) {
3375 if (view->focussed)
3376 wstandout(view->window);
3377 *selected_entry = NULL;
3379 waddstr(view->window, " ..\n"); /* parent directory */
3380 if (selected == 0 && view->focussed)
3381 wstandend(view->window);
3382 (*ndisplayed)++;
3383 if (--limit <= 0)
3384 return NULL;
3385 n = 1;
3386 } else {
3387 n = 0;
3388 while (te != *first_displayed_entry)
3389 te = SIMPLEQ_NEXT(te, entry);
3392 while (te) {
3393 char *line = NULL, *id_str = NULL;
3395 if (show_ids) {
3396 err = got_object_id_str(&id_str, te->id);
3397 if (err)
3398 return got_error_prefix_errno(
3399 "got_object_id_str");
3401 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3402 te->name, S_ISDIR(te->mode) ? "/" :
3403 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3404 free(id_str);
3405 return got_error_prefix_errno("asprintf");
3407 free(id_str);
3408 err = format_line(&wline, &width, line, view->ncols);
3409 if (err) {
3410 free(line);
3411 break;
3413 if (n == selected) {
3414 if (view->focussed)
3415 wstandout(view->window);
3416 *selected_entry = te;
3418 waddwstr(view->window, wline);
3419 if (width < view->ncols)
3420 waddch(view->window, '\n');
3421 if (n == selected && view->focussed)
3422 wstandend(view->window);
3423 free(line);
3424 free(wline);
3425 wline = NULL;
3426 n++;
3427 (*ndisplayed)++;
3428 *last_displayed_entry = te;
3429 if (--limit <= 0)
3430 break;
3431 te = SIMPLEQ_NEXT(te, entry);
3434 return err;
3437 static void
3438 tree_scroll_up(struct tog_view *view,
3439 struct got_tree_entry **first_displayed_entry, int maxscroll,
3440 const struct got_tree_entries *entries, int isroot)
3442 struct got_tree_entry *te, *prev;
3443 int i;
3445 if (*first_displayed_entry == NULL) {
3446 view_flash(view);
3447 return;
3450 te = SIMPLEQ_FIRST(&entries->head);
3451 if (*first_displayed_entry == te) {
3452 view_flash(view);
3453 if (!isroot)
3454 *first_displayed_entry = NULL;
3455 return;
3458 /* XXX this is stupid... switch to TAILQ? */
3459 for (i = 0; i < maxscroll; i++) {
3460 while (te != *first_displayed_entry) {
3461 prev = te;
3462 te = SIMPLEQ_NEXT(te, entry);
3464 *first_displayed_entry = prev;
3465 te = SIMPLEQ_FIRST(&entries->head);
3467 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3468 *first_displayed_entry = NULL;
3471 static int
3472 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3473 struct got_tree_entry *last_displayed_entry,
3474 const struct got_tree_entries *entries)
3476 struct got_tree_entry *next, *last;
3477 int n = 0;
3479 if (*first_displayed_entry)
3480 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3481 else
3482 next = SIMPLEQ_FIRST(&entries->head);
3483 last = last_displayed_entry;
3484 while (next && last && n++ < maxscroll) {
3485 last = SIMPLEQ_NEXT(last, entry);
3486 if (last) {
3487 *first_displayed_entry = next;
3488 next = SIMPLEQ_NEXT(next, entry);
3491 return n;
3494 static const struct got_error *
3495 tree_entry_path(char **path, struct tog_parent_trees *parents,
3496 struct got_tree_entry *te)
3498 const struct got_error *err = NULL;
3499 struct tog_parent_tree *pt;
3500 size_t len = 2; /* for leading slash and NUL */
3502 TAILQ_FOREACH(pt, parents, entry)
3503 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3504 if (te)
3505 len += strlen(te->name);
3507 *path = calloc(1, len);
3508 if (path == NULL)
3509 return got_error_prefix_errno("calloc");
3511 (*path)[0] = '/';
3512 pt = TAILQ_LAST(parents, tog_parent_trees);
3513 while (pt) {
3514 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3515 err = got_error(GOT_ERR_NO_SPACE);
3516 goto done;
3518 if (strlcat(*path, "/", len) >= len) {
3519 err = got_error(GOT_ERR_NO_SPACE);
3520 goto done;
3522 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3524 if (te) {
3525 if (strlcat(*path, te->name, len) >= len) {
3526 err = got_error(GOT_ERR_NO_SPACE);
3527 goto done;
3530 done:
3531 if (err) {
3532 free(*path);
3533 *path = NULL;
3535 return err;
3538 static const struct got_error *
3539 blame_tree_entry(struct tog_view **new_view, int begin_x,
3540 struct got_tree_entry *te, struct tog_parent_trees *parents,
3541 struct got_object_id *commit_id, struct got_reflist_head *refs,
3542 struct got_repository *repo)
3544 const struct got_error *err = NULL;
3545 char *path;
3546 struct tog_view *blame_view;
3548 err = tree_entry_path(&path, parents, te);
3549 if (err)
3550 return err;
3552 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3553 if (blame_view == NULL)
3554 return got_error_prefix_errno("view_open");
3556 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3557 if (err) {
3558 view_close(blame_view);
3559 free(path);
3560 } else
3561 *new_view = blame_view;
3562 return err;
3565 static const struct got_error *
3566 log_tree_entry(struct tog_view **new_view, int begin_x,
3567 struct got_tree_entry *te, struct tog_parent_trees *parents,
3568 struct got_object_id *commit_id, struct got_reflist_head *refs,
3569 struct got_repository *repo)
3571 struct tog_view *log_view;
3572 const struct got_error *err = NULL;
3573 char *path;
3575 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3576 if (log_view == NULL)
3577 return got_error_prefix_errno("view_open");
3579 err = tree_entry_path(&path, parents, te);
3580 if (err)
3581 return err;
3583 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3584 if (err)
3585 view_close(log_view);
3586 else
3587 *new_view = log_view;
3588 free(path);
3589 return err;
3592 static const struct got_error *
3593 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3594 struct got_object_id *commit_id, struct got_reflist_head *refs,
3595 struct got_repository *repo)
3597 const struct got_error *err = NULL;
3598 char *commit_id_str = NULL;
3599 struct tog_tree_view_state *s = &view->state.tree;
3601 TAILQ_INIT(&s->parents);
3603 err = got_object_id_str(&commit_id_str, commit_id);
3604 if (err != NULL)
3605 goto done;
3607 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3608 err = got_error_prefix_errno("asprintf");
3609 goto done;
3612 s->root = s->tree = root;
3613 s->entries = got_object_tree_get_entries(root);
3614 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3615 s->commit_id = got_object_id_dup(commit_id);
3616 if (s->commit_id == NULL) {
3617 err = got_error_prefix_errno("got_object_id_dup");
3618 goto done;
3620 s->refs = refs;
3621 s->repo = repo;
3623 view->show = show_tree_view;
3624 view->input = input_tree_view;
3625 view->close = close_tree_view;
3626 done:
3627 free(commit_id_str);
3628 if (err) {
3629 free(s->tree_label);
3630 s->tree_label = NULL;
3632 return err;
3635 static const struct got_error *
3636 close_tree_view(struct tog_view *view)
3638 struct tog_tree_view_state *s = &view->state.tree;
3640 free(s->tree_label);
3641 s->tree_label = NULL;
3642 free(s->commit_id);
3643 s->commit_id = NULL;
3644 while (!TAILQ_EMPTY(&s->parents)) {
3645 struct tog_parent_tree *parent;
3646 parent = TAILQ_FIRST(&s->parents);
3647 TAILQ_REMOVE(&s->parents, parent, entry);
3648 free(parent);
3651 if (s->tree != s->root)
3652 got_object_tree_close(s->tree);
3653 got_object_tree_close(s->root);
3655 return NULL;
3658 static const struct got_error *
3659 show_tree_view(struct tog_view *view)
3661 const struct got_error *err = NULL;
3662 struct tog_tree_view_state *s = &view->state.tree;
3663 char *parent_path;
3665 err = tree_entry_path(&parent_path, &s->parents, NULL);
3666 if (err)
3667 return err;
3669 err = draw_tree_entries(view, &s->first_displayed_entry,
3670 &s->last_displayed_entry, &s->selected_entry,
3671 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3672 s->entries, s->selected, view->nlines, s->tree == s->root);
3673 free(parent_path);
3675 view_vborder(view);
3676 return err;
3679 static const struct got_error *
3680 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3681 struct tog_view **focus_view, struct tog_view *view, int ch)
3683 const struct got_error *err = NULL;
3684 struct tog_tree_view_state *s = &view->state.tree;
3685 struct tog_view *log_view;
3686 int begin_x = 0, nscrolled;
3688 switch (ch) {
3689 case 'i':
3690 s->show_ids = !s->show_ids;
3691 break;
3692 case 'l':
3693 if (!s->selected_entry)
3694 break;
3695 if (view_is_parent_view(view))
3696 begin_x = view_split_begin_x(view->begin_x);
3697 err = log_tree_entry(&log_view, begin_x,
3698 s->selected_entry, &s->parents,
3699 s->commit_id, s->refs, s->repo);
3700 if (view_is_parent_view(view)) {
3701 err = view_close_child(view);
3702 if (err)
3703 return err;
3704 err = view_set_child(view, log_view);
3705 if (err) {
3706 view_close(log_view);
3707 break;
3709 *focus_view = log_view;
3710 view->child_focussed = 1;
3711 } else
3712 *new_view = log_view;
3713 break;
3714 case 'k':
3715 case KEY_UP:
3716 if (s->selected > 0) {
3717 s->selected--;
3718 if (s->selected == 0)
3719 break;
3721 if (s->selected > 0)
3722 break;
3723 tree_scroll_up(view, &s->first_displayed_entry, 1,
3724 s->entries, s->tree == s->root);
3725 break;
3726 case KEY_PPAGE:
3727 tree_scroll_up(view, &s->first_displayed_entry,
3728 MAX(0, view->nlines - 4 - s->selected), s->entries,
3729 s->tree == s->root);
3730 s->selected = 0;
3731 if (SIMPLEQ_FIRST(&s->entries->head) ==
3732 s->first_displayed_entry && s->tree != s->root)
3733 s->first_displayed_entry = NULL;
3734 break;
3735 case 'j':
3736 case KEY_DOWN:
3737 if (s->selected < s->ndisplayed - 1) {
3738 s->selected++;
3739 break;
3741 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3742 == NULL) {
3743 /* can't scroll any further */
3744 view_flash(view);
3745 break;
3747 tree_scroll_down(&s->first_displayed_entry, 1,
3748 s->last_displayed_entry, s->entries);
3749 break;
3750 case KEY_NPAGE:
3751 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3752 == NULL) {
3753 /* can't scroll any further; move cursor down */
3754 if (s->selected < s->ndisplayed - 1)
3755 s->selected = s->ndisplayed - 1;
3756 else
3757 view_flash(view);
3758 break;
3760 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3761 view->nlines, s->last_displayed_entry, s->entries);
3762 if (nscrolled < view->nlines) {
3763 int ndisplayed = 0;
3764 struct got_tree_entry *te;
3765 te = s->first_displayed_entry;
3766 do {
3767 ndisplayed++;
3768 te = SIMPLEQ_NEXT(te, entry);
3769 } while (te);
3770 s->selected = ndisplayed - 1;
3772 break;
3773 case KEY_ENTER:
3774 case '\r':
3775 if (s->selected_entry == NULL) {
3776 struct tog_parent_tree *parent;
3777 case KEY_BACKSPACE:
3778 /* user selected '..' */
3779 if (s->tree == s->root)
3780 break;
3781 parent = TAILQ_FIRST(&s->parents);
3782 TAILQ_REMOVE(&s->parents, parent,
3783 entry);
3784 got_object_tree_close(s->tree);
3785 s->tree = parent->tree;
3786 s->entries =
3787 got_object_tree_get_entries(s->tree);
3788 s->first_displayed_entry =
3789 parent->first_displayed_entry;
3790 s->selected_entry =
3791 parent->selected_entry;
3792 s->selected = parent->selected;
3793 free(parent);
3794 } else if (S_ISDIR(s->selected_entry->mode)) {
3795 struct tog_parent_tree *parent;
3796 struct got_tree_object *child;
3797 err = got_object_open_as_tree(&child,
3798 s->repo, s->selected_entry->id);
3799 if (err)
3800 break;
3801 parent = calloc(1, sizeof(*parent));
3802 if (parent == NULL) {
3803 err = got_error_prefix_errno("calloc");
3804 break;
3806 parent->tree = s->tree;
3807 parent->first_displayed_entry =
3808 s->first_displayed_entry;
3809 parent->selected_entry = s->selected_entry;
3810 parent->selected = s->selected;
3811 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3812 s->tree = child;
3813 s->entries =
3814 got_object_tree_get_entries(s->tree);
3815 s->selected = 0;
3816 s->first_displayed_entry = NULL;
3817 } else if (S_ISREG(s->selected_entry->mode)) {
3818 struct tog_view *blame_view;
3819 int begin_x = view_is_parent_view(view) ?
3820 view_split_begin_x(view->begin_x) : 0;
3822 err = blame_tree_entry(&blame_view, begin_x,
3823 s->selected_entry, &s->parents,
3824 s->commit_id, s->refs, s->repo);
3825 if (err)
3826 break;
3827 if (view_is_parent_view(view)) {
3828 err = view_close_child(view);
3829 if (err)
3830 return err;
3831 err = view_set_child(view, blame_view);
3832 if (err) {
3833 view_close(blame_view);
3834 break;
3836 *focus_view = blame_view;
3837 view->child_focussed = 1;
3838 } else
3839 *new_view = blame_view;
3841 break;
3842 case KEY_RESIZE:
3843 if (s->selected > view->nlines)
3844 s->selected = s->ndisplayed - 1;
3845 break;
3846 default:
3847 break;
3850 return err;
3853 __dead static void
3854 usage_tree(void)
3856 endwin();
3857 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3858 getprogname());
3859 exit(1);
3862 static const struct got_error *
3863 cmd_tree(int argc, char *argv[])
3865 const struct got_error *error;
3866 struct got_repository *repo = NULL;
3867 struct got_reflist_head refs;
3868 char *repo_path = NULL;
3869 struct got_object_id *commit_id = NULL;
3870 char *commit_id_arg = NULL;
3871 struct got_commit_object *commit = NULL;
3872 struct got_tree_object *tree = NULL;
3873 int ch;
3874 struct tog_view *view;
3876 SIMPLEQ_INIT(&refs);
3878 #ifndef PROFILE
3879 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3880 NULL) == -1)
3881 err(1, "pledge");
3882 #endif
3884 while ((ch = getopt(argc, argv, "c:")) != -1) {
3885 switch (ch) {
3886 case 'c':
3887 commit_id_arg = optarg;
3888 break;
3889 default:
3890 usage_tree();
3891 /* NOTREACHED */
3895 argc -= optind;
3896 argv += optind;
3898 if (argc == 0) {
3899 struct got_worktree *worktree;
3900 char *cwd = getcwd(NULL, 0);
3901 if (cwd == NULL)
3902 return got_error_prefix_errno("getcwd");
3903 error = got_worktree_open(&worktree, cwd);
3904 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3905 goto done;
3906 if (worktree) {
3907 free(cwd);
3908 repo_path =
3909 strdup(got_worktree_get_repo_path(worktree));
3910 got_worktree_close(worktree);
3911 } else
3912 repo_path = cwd;
3913 if (repo_path == NULL) {
3914 error = got_error_prefix_errno("strdup");
3915 goto done;
3917 } else if (argc == 1) {
3918 repo_path = realpath(argv[0], NULL);
3919 if (repo_path == NULL)
3920 return got_error_prefix_errno2("realpath", argv[0]);
3921 } else
3922 usage_log();
3924 init_curses();
3926 error = got_repo_open(&repo, repo_path);
3927 if (error != NULL)
3928 goto done;
3930 error = apply_unveil(got_repo_get_path(repo), NULL);
3931 if (error)
3932 goto done;
3934 if (commit_id_arg == NULL)
3935 error = get_head_commit_id(&commit_id, repo);
3936 else
3937 error = got_object_resolve_id_str(&commit_id, repo,
3938 commit_id_arg);
3939 if (error != NULL)
3940 goto done;
3942 error = got_object_open_as_commit(&commit, repo, commit_id);
3943 if (error != NULL)
3944 goto done;
3946 error = got_object_open_as_tree(&tree, repo,
3947 got_object_commit_get_tree_id(commit));
3948 if (error != NULL)
3949 goto done;
3951 error = got_ref_list(&refs, repo);
3952 if (error)
3953 goto done;
3955 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3956 if (view == NULL) {
3957 error = got_error_prefix_errno("view_open");
3958 goto done;
3960 error = open_tree_view(view, tree, commit_id, &refs, repo);
3961 if (error)
3962 goto done;
3963 error = view_loop(view);
3964 done:
3965 free(repo_path);
3966 free(commit_id);
3967 if (commit)
3968 got_object_commit_close(commit);
3969 if (tree)
3970 got_object_tree_close(tree);
3971 if (repo)
3972 got_repo_close(repo);
3973 got_ref_list_free(&refs);
3974 return error;
3977 __dead static void
3978 usage(void)
3980 int i;
3982 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3983 "Available commands:\n", getprogname());
3984 for (i = 0; i < nitems(tog_commands); i++) {
3985 struct tog_cmd *cmd = &tog_commands[i];
3986 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3988 exit(1);
3991 static char **
3992 make_argv(const char *arg0, const char *arg1)
3994 char **argv;
3995 int argc = (arg1 == NULL ? 1 : 2);
3997 argv = calloc(argc, sizeof(char *));
3998 if (argv == NULL)
3999 err(1, "calloc");
4000 argv[0] = strdup(arg0);
4001 if (argv[0] == NULL)
4002 err(1, "calloc");
4003 if (arg1) {
4004 argv[1] = strdup(arg1);
4005 if (argv[1] == NULL)
4006 err(1, "calloc");
4009 return argv;
4012 int
4013 main(int argc, char *argv[])
4015 const struct got_error *error = NULL;
4016 struct tog_cmd *cmd = NULL;
4017 int ch, hflag = 0;
4018 char **cmd_argv = NULL;
4020 setlocale(LC_CTYPE, "");
4022 while ((ch = getopt(argc, argv, "h")) != -1) {
4023 switch (ch) {
4024 case 'h':
4025 hflag = 1;
4026 break;
4027 default:
4028 usage();
4029 /* NOTREACHED */
4033 argc -= optind;
4034 argv += optind;
4035 optind = 0;
4036 optreset = 1;
4038 if (argc == 0) {
4039 if (hflag)
4040 usage();
4041 /* Build an argument vector which runs a default command. */
4042 cmd = &tog_commands[0];
4043 cmd_argv = make_argv(cmd->name, NULL);
4044 argc = 1;
4045 } else {
4046 int i;
4048 /* Did the user specific a command? */
4049 for (i = 0; i < nitems(tog_commands); i++) {
4050 if (strncmp(tog_commands[i].name, argv[0],
4051 strlen(argv[0])) == 0) {
4052 cmd = &tog_commands[i];
4053 if (hflag)
4054 tog_commands[i].cmd_usage();
4055 break;
4058 if (cmd == NULL) {
4059 /* Did the user specify a repository? */
4060 char *repo_path = realpath(argv[0], NULL);
4061 if (repo_path) {
4062 struct got_repository *repo;
4063 error = got_repo_open(&repo, repo_path);
4064 if (error == NULL)
4065 got_repo_close(repo);
4066 } else
4067 error = got_error_prefix_errno2("realpath",
4068 argv[0]);
4069 if (error) {
4070 if (hflag) {
4071 fprintf(stderr, "%s: '%s' is not a "
4072 "known command\n", getprogname(),
4073 argv[0]);
4074 usage();
4076 fprintf(stderr, "%s: '%s' is neither a known "
4077 "command nor a path to a repository\n",
4078 getprogname(), argv[0]);
4079 free(repo_path);
4080 return 1;
4082 cmd = &tog_commands[0];
4083 cmd_argv = make_argv(cmd->name, repo_path);
4084 argc = 2;
4085 free(repo_path);
4089 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4090 if (error)
4091 goto done;
4092 done:
4093 endwin();
4094 free(cmd_argv);
4095 if (error)
4096 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4097 return 0;