Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_path.h"
51 #include "got_worktree.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef MAX
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #endif
62 #ifndef nitems
63 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
64 #endif
66 struct tog_cmd {
67 const char *name;
68 const struct got_error *(*cmd_main)(int, char *[]);
69 void (*cmd_usage)(void);
70 const char *descr;
71 };
73 __dead static void usage(void);
74 __dead static void usage_log(void);
75 __dead static void usage_diff(void);
76 __dead static void usage_blame(void);
77 __dead static void usage_tree(void);
79 static const struct got_error* cmd_log(int, char *[]);
80 static const struct got_error* cmd_diff(int, char *[]);
81 static const struct got_error* cmd_blame(int, char *[]);
82 static const struct got_error* cmd_tree(int, char *[]);
84 static struct tog_cmd tog_commands[] = {
85 { "log", cmd_log, usage_log,
86 "show repository history" },
87 { "diff", cmd_diff, usage_diff,
88 "compare files and directories" },
89 { "blame", cmd_blame, usage_blame,
90 "show line-by-line file history" },
91 { "tree", cmd_tree, usage_tree,
92 "browse trees in repository" },
93 };
95 enum tog_view_type {
96 TOG_VIEW_DIFF,
97 TOG_VIEW_LOG,
98 TOG_VIEW_BLAME,
99 TOG_VIEW_TREE
100 };
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
106 int idx;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
116 FILE *f;
117 int first_displayed_line;
118 int last_displayed_line;
119 int eof;
120 int diff_context;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
126 };
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
132 int commits_needed;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
138 int log_complete;
139 sig_atomic_t *quit;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 };
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
150 int selected;
151 char *in_repo_path;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
155 sig_atomic_t quit;
156 pthread_t thread;
157 struct tog_log_thread_args thread_args;
158 };
160 struct tog_blame_cb_args {
161 struct tog_blame_line *lines; /* one per line */
162 int nlines;
164 struct tog_view *view;
165 struct got_object_id *commit_id;
166 int *quit;
167 };
169 struct tog_blame_thread_args {
170 const char *path;
171 struct got_repository *repo;
172 struct tog_blame_cb_args *cb_args;
173 int *complete;
174 };
176 struct tog_blame {
177 FILE *f;
178 size_t filesize;
179 struct tog_blame_line *lines;
180 int nlines;
181 pthread_t thread;
182 struct tog_blame_thread_args thread_args;
183 struct tog_blame_cb_args cb_args;
184 const char *path;
185 };
187 struct tog_blame_view_state {
188 int first_displayed_line;
189 int last_displayed_line;
190 int selected_line;
191 int blame_complete;
192 int eof;
193 int done;
194 struct got_object_id_queue blamed_commits;
195 struct got_object_qid *blamed_commit;
196 char *path;
197 struct got_repository *repo;
198 struct got_reflist_head *refs;
199 struct got_object_id *commit_id;
200 struct tog_blame blame;
201 };
203 struct tog_parent_tree {
204 TAILQ_ENTRY(tog_parent_tree) entry;
205 struct got_tree_object *tree;
206 struct got_tree_entry *first_displayed_entry;
207 struct got_tree_entry *selected_entry;
208 int selected;
209 };
211 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
213 struct tog_tree_view_state {
214 char *tree_label;
215 struct got_tree_object *root;
216 struct got_tree_object *tree;
217 const struct got_tree_entries *entries;
218 struct got_tree_entry *first_displayed_entry;
219 struct got_tree_entry *last_displayed_entry;
220 struct got_tree_entry *selected_entry;
221 int ndisplayed, selected, show_ids;
222 struct tog_parent_trees parents;
223 struct got_object_id *commit_id;
224 struct got_repository *repo;
225 struct got_reflist_head *refs;
226 };
228 /*
229 * We implement two types of views: parent views and child views.
231 * The 'Tab' key switches between a parent view and its child view.
232 * Child views are shown side-by-side to their parent view, provided
233 * there is enough screen estate.
235 * When a new view is opened from within a parent view, this new view
236 * becomes a child view of the parent view, replacing any existing child.
238 * When a new view is opened from within a child view, this new view
239 * becomes a parent view which will obscure the views below until the
240 * user quits the new parent view by typing 'q'.
242 * This list of views contains parent views only.
243 * Child views are only pointed to by their parent view.
244 */
245 TAILQ_HEAD(tog_view_list_head, tog_view);
247 struct tog_view {
248 TAILQ_ENTRY(tog_view) entry;
249 WINDOW *window;
250 PANEL *panel;
251 int nlines, ncols, begin_y, begin_x;
252 int lines, cols; /* copies of LINES and COLS */
253 int focussed;
254 struct tog_view *parent;
255 struct tog_view *child;
256 int child_focussed;
258 /* type-specific state */
259 enum tog_view_type type;
260 union {
261 struct tog_diff_view_state diff;
262 struct tog_log_view_state log;
263 struct tog_blame_view_state blame;
264 struct tog_tree_view_state tree;
265 } state;
267 const struct got_error *(*show)(struct tog_view *);
268 const struct got_error *(*input)(struct tog_view **,
269 struct tog_view **, struct tog_view**, struct tog_view *, int);
270 const struct got_error *(*close)(struct tog_view *);
271 };
273 static const struct got_error *open_diff_view(struct tog_view *,
274 struct got_object_id *, struct got_object_id *, struct tog_view *,
275 struct got_reflist_head *, struct got_repository *);
276 static const struct got_error *show_diff_view(struct tog_view *);
277 static const struct got_error *input_diff_view(struct tog_view **,
278 struct tog_view **, struct tog_view **, struct tog_view *, int);
279 static const struct got_error* close_diff_view(struct tog_view *);
281 static const struct got_error *open_log_view(struct tog_view *,
282 struct got_object_id *, struct got_reflist_head *,
283 struct got_repository *, const char *, int);
284 static const struct got_error * show_log_view(struct tog_view *);
285 static const struct got_error *input_log_view(struct tog_view **,
286 struct tog_view **, struct tog_view **, struct tog_view *, int);
287 static const struct got_error *close_log_view(struct tog_view *);
289 static const struct got_error *open_blame_view(struct tog_view *, char *,
290 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
291 static const struct got_error *show_blame_view(struct tog_view *);
292 static const struct got_error *input_blame_view(struct tog_view **,
293 struct tog_view **, struct tog_view **, struct tog_view *, int);
294 static const struct got_error *close_blame_view(struct tog_view *);
296 static const struct got_error *open_tree_view(struct tog_view *,
297 struct got_tree_object *, struct got_object_id *,
298 struct got_reflist_head *, struct got_repository *);
299 static const struct got_error *show_tree_view(struct tog_view *);
300 static const struct got_error *input_tree_view(struct tog_view **,
301 struct tog_view **, struct tog_view **, struct tog_view *, int);
302 static const struct got_error *close_tree_view(struct tog_view *);
304 static volatile sig_atomic_t tog_sigwinch_received;
306 static void
307 tog_sigwinch(int signo)
309 tog_sigwinch_received = 1;
312 static const struct got_error *
313 view_close(struct tog_view *view)
315 const struct got_error *err = NULL;
317 if (view->child) {
318 view_close(view->child);
319 view->child = NULL;
321 if (view->close)
322 err = view->close(view);
323 if (view->panel)
324 del_panel(view->panel);
325 if (view->window)
326 delwin(view->window);
327 free(view);
328 return err;
331 static struct tog_view *
332 view_open(int nlines, int ncols, int begin_y, int begin_x,
333 enum tog_view_type type)
335 struct tog_view *view = calloc(1, sizeof(*view));
337 if (view == NULL)
338 return NULL;
340 view->type = type;
341 view->lines = LINES;
342 view->cols = COLS;
343 view->nlines = nlines ? nlines : LINES - begin_y;
344 view->ncols = ncols ? ncols : COLS - begin_x;
345 view->begin_y = begin_y;
346 view->begin_x = begin_x;
347 view->window = newwin(nlines, ncols, begin_y, begin_x);
348 if (view->window == NULL) {
349 view_close(view);
350 return NULL;
352 view->panel = new_panel(view->window);
353 if (view->panel == NULL ||
354 set_panel_userptr(view->panel, view) != OK) {
355 view_close(view);
356 return NULL;
359 keypad(view->window, TRUE);
360 return view;
363 static int
364 view_split_begin_x(int begin_x)
366 if (begin_x > 0 || COLS < 120)
367 return 0;
368 return (COLS - MAX(COLS / 2, 80));
371 static const struct got_error *view_resize(struct tog_view *);
373 static const struct got_error *
374 view_splitscreen(struct tog_view *view)
376 const struct got_error *err = NULL;
378 view->begin_y = 0;
379 view->begin_x = view_split_begin_x(0);
380 view->nlines = LINES;
381 view->ncols = COLS - view->begin_x;
382 view->lines = LINES;
383 view->cols = COLS;
384 err = view_resize(view);
385 if (err)
386 return err;
388 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
389 return got_error_prefix_errno("mvwin");
391 return NULL;
394 static const struct got_error *
395 view_fullscreen(struct tog_view *view)
397 const struct got_error *err = NULL;
399 view->begin_x = 0;
400 view->begin_y = 0;
401 view->nlines = LINES;
402 view->ncols = COLS;
403 view->lines = LINES;
404 view->cols = COLS;
405 err = view_resize(view);
406 if (err)
407 return err;
409 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
410 return got_error_prefix_errno("mvwin");
412 return NULL;
415 static int
416 view_is_parent_view(struct tog_view *view)
418 return view->parent == NULL;
421 static const struct got_error *
422 view_resize(struct tog_view *view)
424 int nlines, ncols;
426 if (view->lines > LINES)
427 nlines = view->nlines - (view->lines - LINES);
428 else
429 nlines = view->nlines + (LINES - view->lines);
431 if (view->cols > COLS)
432 ncols = view->ncols - (view->cols - COLS);
433 else
434 ncols = view->ncols + (COLS - view->cols);
436 if (wresize(view->window, nlines, ncols) == ERR)
437 return got_error_prefix_errno("wresize");
438 if (replace_panel(view->panel, view->window) == ERR)
439 return got_error_prefix_errno("replace_panel");
440 wclear(view->window);
442 view->nlines = nlines;
443 view->ncols = ncols;
444 view->lines = LINES;
445 view->cols = COLS;
447 if (view->child) {
448 view->child->begin_x = view_split_begin_x(view->begin_x);
449 if (view->child->begin_x == 0) {
450 view_fullscreen(view->child);
451 if (view->child->focussed)
452 show_panel(view->child->panel);
453 else
454 show_panel(view->panel);
455 } else {
456 view_splitscreen(view->child);
457 show_panel(view->child->panel);
461 return NULL;
464 static const struct got_error *
465 view_close_child(struct tog_view *view)
467 const struct got_error *err = NULL;
469 if (view->child == NULL)
470 return NULL;
472 err = view_close(view->child);
473 view->child = NULL;
474 return err;
477 static const struct got_error *
478 view_set_child(struct tog_view *view, struct tog_view *child)
480 const struct got_error *err = NULL;
482 view->child = child;
483 child->parent = view;
484 return err;
487 static int
488 view_is_splitscreen(struct tog_view *view)
490 return view->begin_x > 0;
493 /*
494 * Erase all content of the view. Can be used to "flash" the view because
495 * the view loop will redraw it quickly, providing a more subtle visual
496 * effect than curs_flash(3) would provide.
497 */
498 static void
499 view_flash(struct tog_view *view)
501 werase(view->window);
502 update_panels();
503 doupdate();
506 static void
507 tog_resizeterm(void)
509 int cols, lines;
510 struct winsize size;
512 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
513 cols = 80; /* Default */
514 lines = 24;
515 } else {
516 cols = size.ws_col;
517 lines = size.ws_row;
519 resize_term(lines, cols);
522 static const struct got_error *
523 view_input(struct tog_view **new, struct tog_view **dead,
524 struct tog_view **focus, int *done, struct tog_view *view,
525 struct tog_view_list_head *views)
527 const struct got_error *err = NULL;
528 struct tog_view *v;
529 int ch, errcode;
531 *new = NULL;
532 *dead = NULL;
533 *focus = NULL;
535 nodelay(stdscr, FALSE);
536 /* Allow threads to make progress while we are waiting for input. */
537 errcode = pthread_mutex_unlock(&tog_mutex);
538 if (errcode)
539 return got_error_set_errno(errcode, "pthread_mutex_unlock");
540 ch = wgetch(view->window);
541 errcode = pthread_mutex_lock(&tog_mutex);
542 if (errcode)
543 return got_error_set_errno(errcode, "pthread_mutex_lock");
544 nodelay(stdscr, TRUE);
546 if (tog_sigwinch_received) {
547 tog_resizeterm();
548 tog_sigwinch_received = 0;
549 TAILQ_FOREACH(v, views, entry) {
550 err = view_resize(v);
551 if (err)
552 return err;
553 err = v->input(new, dead, focus, v, KEY_RESIZE);
554 if (err)
555 return err;
559 switch (ch) {
560 case ERR:
561 break;
562 case '\t':
563 if (view->child) {
564 *focus = view->child;
565 view->child_focussed = 1;
566 } else if (view->parent) {
567 *focus = view->parent;
568 view->parent->child_focussed = 0;
570 break;
571 case 'q':
572 err = view->input(new, dead, focus, view, ch);
573 *dead = view;
574 break;
575 case 'Q':
576 *done = 1;
577 break;
578 case 'f':
579 if (view_is_parent_view(view)) {
580 if (view->child == NULL)
581 break;
582 if (view_is_splitscreen(view->child)) {
583 *focus = view->child;
584 view->child_focussed = 1;
585 err = view_fullscreen(view->child);
586 } else
587 err = view_splitscreen(view->child);
588 if (err)
589 break;
590 err = view->child->input(new, dead, focus,
591 view->child, KEY_RESIZE);
592 } else {
593 if (view_is_splitscreen(view)) {
594 *focus = view;
595 view->parent->child_focussed = 1;
596 err = view_fullscreen(view);
597 } else {
598 err = view_splitscreen(view);
600 if (err)
601 break;
602 err = view->input(new, dead, focus, view,
603 KEY_RESIZE);
605 break;
606 case KEY_RESIZE:
607 break;
608 default:
609 err = view->input(new, dead, focus, view, ch);
610 break;
613 return err;
616 void
617 view_vborder(struct tog_view *view)
619 PANEL *panel;
620 struct tog_view *view_above;
622 if (view->parent)
623 return view_vborder(view->parent);
625 panel = panel_above(view->panel);
626 if (panel == NULL)
627 return;
629 view_above = panel_userptr(panel);
630 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
631 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
634 int
635 view_needs_focus_indication(struct tog_view *view)
637 if (view_is_parent_view(view)) {
638 if (view->child == NULL || view->child_focussed)
639 return 0;
640 if (!view_is_splitscreen(view->child))
641 return 0;
642 } else if (!view_is_splitscreen(view))
643 return 0;
645 return view->focussed;
648 static const struct got_error *
649 view_loop(struct tog_view *view)
651 const struct got_error *err = NULL;
652 struct tog_view_list_head views;
653 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
654 int fast_refresh = 10;
655 int done = 0, errcode;
657 errcode = pthread_mutex_lock(&tog_mutex);
658 if (errcode)
659 return got_error_set_errno(errcode, "pthread_mutex_lock");
661 TAILQ_INIT(&views);
662 TAILQ_INSERT_HEAD(&views, view, entry);
664 main_view = view;
665 view->focussed = 1;
666 err = view->show(view);
667 if (err)
668 return err;
669 update_panels();
670 doupdate();
671 while (!TAILQ_EMPTY(&views) && !done) {
672 /* Refresh fast during initialization, then become slower. */
673 if (fast_refresh && fast_refresh-- == 0)
674 halfdelay(10); /* switch to once per second */
676 err = view_input(&new_view, &dead_view, &focus_view, &done,
677 view, &views);
678 if (err)
679 break;
680 if (dead_view) {
681 struct tog_view *prev = NULL;
683 if (view_is_parent_view(dead_view))
684 prev = TAILQ_PREV(dead_view,
685 tog_view_list_head, entry);
686 else if (view->parent != dead_view)
687 prev = view->parent;
689 if (dead_view->parent)
690 dead_view->parent->child = NULL;
691 else
692 TAILQ_REMOVE(&views, dead_view, entry);
694 err = view_close(dead_view);
695 if (err || dead_view == main_view)
696 goto done;
698 if (view == dead_view) {
699 if (focus_view)
700 view = focus_view;
701 else if (prev)
702 view = prev;
703 else if (!TAILQ_EMPTY(&views))
704 view = TAILQ_LAST(&views,
705 tog_view_list_head);
706 else
707 view = NULL;
708 if (view) {
709 if (view->child && view->child_focussed)
710 focus_view = view->child;
711 else
712 focus_view = view;
716 if (new_view) {
717 struct tog_view *v, *t;
718 /* Only allow one parent view per type. */
719 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
720 if (v->type != new_view->type)
721 continue;
722 TAILQ_REMOVE(&views, v, entry);
723 err = view_close(v);
724 if (err)
725 goto done;
726 break;
728 TAILQ_INSERT_TAIL(&views, new_view, entry);
729 view = new_view;
730 if (focus_view == NULL)
731 focus_view = new_view;
733 if (focus_view) {
734 show_panel(focus_view->panel);
735 if (view)
736 view->focussed = 0;
737 focus_view->focussed = 1;
738 view = focus_view;
739 if (new_view)
740 show_panel(new_view->panel);
741 if (view->child && view_is_splitscreen(view->child))
742 show_panel(view->child->panel);
744 if (view) {
745 if (focus_view == NULL) {
746 view->focussed = 1;
747 show_panel(view->panel);
748 if (view->child && view_is_splitscreen(view->child))
749 show_panel(view->child->panel);
750 focus_view = view;
752 if (view->parent) {
753 err = view->parent->show(view->parent);
754 if (err)
755 goto done;
757 err = view->show(view);
758 if (err)
759 goto done;
760 if (view->child) {
761 err = view->child->show(view->child);
762 if (err)
763 goto done;
765 update_panels();
766 doupdate();
769 done:
770 while (!TAILQ_EMPTY(&views)) {
771 view = TAILQ_FIRST(&views);
772 TAILQ_REMOVE(&views, view, entry);
773 view_close(view);
776 errcode = pthread_mutex_unlock(&tog_mutex);
777 if (errcode)
778 return got_error_set_errno(errcode, "pthread_mutex_unlock");
780 return err;
783 __dead static void
784 usage_log(void)
786 endwin();
787 fprintf(stderr,
788 "usage: %s log [-c commit] [-r repository-path] [path]\n",
789 getprogname());
790 exit(1);
793 /* Create newly allocated wide-character string equivalent to a byte string. */
794 static const struct got_error *
795 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
797 char *vis = NULL;
798 const struct got_error *err = NULL;
800 *ws = NULL;
801 *wlen = mbstowcs(NULL, s, 0);
802 if (*wlen == (size_t)-1) {
803 int vislen;
804 if (errno != EILSEQ)
805 return got_error_prefix_errno("mbstowcs");
807 /* byte string invalid in current encoding; try to "fix" it */
808 err = got_mbsavis(&vis, &vislen, s);
809 if (err)
810 return err;
811 *wlen = mbstowcs(NULL, vis, 0);
812 if (*wlen == (size_t)-1) {
813 err = got_error_prefix_errno("mbstowcs"); /* give up */
814 goto done;
818 *ws = calloc(*wlen + 1, sizeof(*ws));
819 if (*ws == NULL) {
820 err = got_error_prefix_errno("calloc");
821 goto done;
824 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
825 err = got_error_prefix_errno("mbstowcs");
826 done:
827 free(vis);
828 if (err) {
829 free(*ws);
830 *ws = NULL;
831 *wlen = 0;
833 return err;
836 /* Format a line for display, ensuring that it won't overflow a width limit. */
837 static const struct got_error *
838 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
840 const struct got_error *err = NULL;
841 int cols = 0;
842 wchar_t *wline = NULL;
843 size_t wlen;
844 int i;
846 *wlinep = NULL;
847 *widthp = 0;
849 err = mbs2ws(&wline, &wlen, line);
850 if (err)
851 return err;
853 i = 0;
854 while (i < wlen && cols < wlimit) {
855 int width = wcwidth(wline[i]);
856 switch (width) {
857 case 0:
858 i++;
859 break;
860 case 1:
861 case 2:
862 if (cols + width <= wlimit)
863 cols += width;
864 i++;
865 break;
866 case -1:
867 if (wline[i] == L'\t')
868 cols += TABSIZE - ((cols + 1) % TABSIZE);
869 i++;
870 break;
871 default:
872 err = got_error_prefix_errno("wcwidth");
873 goto done;
876 wline[i] = L'\0';
877 if (widthp)
878 *widthp = cols;
879 done:
880 if (err)
881 free(wline);
882 else
883 *wlinep = wline;
884 return err;
887 static const struct got_error*
888 build_refs_str(char **refs_str, struct got_reflist_head *refs,
889 struct got_object_id *id)
891 static const struct got_error *err = NULL;
892 struct got_reflist_entry *re;
893 char *s;
894 const char *name;
896 *refs_str = NULL;
898 SIMPLEQ_FOREACH(re, refs, entry) {
899 if (got_object_id_cmp(re->id, id) != 0)
900 continue;
901 name = got_ref_get_name(re->ref);
902 if (strcmp(name, GOT_REF_HEAD) == 0)
903 continue;
904 if (strncmp(name, "refs/", 5) == 0)
905 name += 5;
906 if (strncmp(name, "got/", 4) == 0)
907 continue;
908 if (strncmp(name, "heads/", 6) == 0)
909 name += 6;
910 if (strncmp(name, "remotes/", 8) == 0)
911 name += 8;
912 s = *refs_str;
913 if (asprintf(refs_str, "%s%s%s", s ? s : "",
914 s ? ", " : "", name) == -1) {
915 err = got_error_prefix_errno("asprintf");
916 free(s);
917 *refs_str = NULL;
918 break;
920 free(s);
923 return err;
926 static const struct got_error *
927 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
929 char *smallerthan, *at;
931 smallerthan = strchr(author, '<');
932 if (smallerthan && smallerthan[1] != '\0')
933 author = smallerthan + 1;
934 at = strchr(author, '@');
935 if (at)
936 *at = '\0';
937 return format_line(wauthor, author_width, author, limit);
940 static const struct got_error *
941 draw_commit(struct tog_view *view, struct got_commit_object *commit,
942 struct got_object_id *id, struct got_reflist_head *refs,
943 int author_display_cols)
945 const struct got_error *err = NULL;
946 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
947 char *logmsg0 = NULL, *logmsg = NULL;
948 char *author = NULL;
949 wchar_t *wlogmsg = NULL, *wauthor = NULL;
950 int author_width, logmsg_width;
951 char *newline, *line = NULL;
952 int col, limit;
953 static const size_t date_display_cols = 9;
954 const int avail = view->ncols;
955 struct tm tm;
956 time_t committer_time;
958 committer_time = got_object_commit_get_committer_time(commit);
959 if (localtime_r(&committer_time, &tm) == NULL)
960 return got_error_prefix_errno("localtime_r");
961 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
962 >= sizeof(datebuf))
963 return got_error(GOT_ERR_NO_SPACE);
965 if (avail < date_display_cols)
966 limit = MIN(sizeof(datebuf) - 1, avail);
967 else
968 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
969 waddnstr(view->window, datebuf, limit);
970 col = limit + 1;
971 if (col > avail)
972 goto done;
974 author = strdup(got_object_commit_get_author(commit));
975 if (author == NULL) {
976 err = got_error_prefix_errno("strdup");
977 goto done;
979 err = format_author(&wauthor, &author_width, author, avail - col);
980 if (err)
981 goto done;
982 waddwstr(view->window, wauthor);
983 col += author_width;
984 while (col <= avail && author_width < author_display_cols + 2) {
985 waddch(view->window, ' ');
986 col++;
987 author_width++;
989 if (col > avail)
990 goto done;
992 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
993 if (logmsg0 == NULL) {
994 err = got_error_prefix_errno("strdup");
995 goto done;
997 logmsg = logmsg0;
998 while (*logmsg == '\n')
999 logmsg++;
1000 newline = strchr(logmsg, '\n');
1001 if (newline)
1002 *newline = '\0';
1003 limit = avail - col;
1004 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1005 if (err)
1006 goto done;
1007 waddwstr(view->window, wlogmsg);
1008 col += logmsg_width;
1009 while (col <= avail) {
1010 waddch(view->window, ' ');
1011 col++;
1013 done:
1014 free(logmsg0);
1015 free(wlogmsg);
1016 free(author);
1017 free(wauthor);
1018 free(line);
1019 return err;
1022 static struct commit_queue_entry *
1023 alloc_commit_queue_entry(struct got_commit_object *commit,
1024 struct got_object_id *id)
1026 struct commit_queue_entry *entry;
1028 entry = calloc(1, sizeof(*entry));
1029 if (entry == NULL)
1030 return NULL;
1032 entry->id = id;
1033 entry->commit = commit;
1034 return entry;
1037 static void
1038 pop_commit(struct commit_queue *commits)
1040 struct commit_queue_entry *entry;
1042 entry = TAILQ_FIRST(&commits->head);
1043 TAILQ_REMOVE(&commits->head, entry, entry);
1044 got_object_commit_close(entry->commit);
1045 commits->ncommits--;
1046 /* Don't free entry->id! It is owned by the commit graph. */
1047 free(entry);
1050 static void
1051 free_commits(struct commit_queue *commits)
1053 while (!TAILQ_EMPTY(&commits->head))
1054 pop_commit(commits);
1057 static const struct got_error *
1058 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1059 int minqueue, struct got_repository *repo, const char *path)
1061 const struct got_error *err = NULL;
1062 int nqueued = 0;
1065 * We keep all commits open throughout the lifetime of the log
1066 * view in order to avoid having to re-fetch commits from disk
1067 * while updating the display.
1069 while (nqueued < minqueue) {
1070 struct got_object_id *id;
1071 struct got_commit_object *commit;
1072 struct commit_queue_entry *entry;
1073 int errcode;
1075 err = got_commit_graph_iter_next(&id, graph);
1076 if (err) {
1077 if (err->code != GOT_ERR_ITER_NEED_MORE)
1078 break;
1079 err = got_commit_graph_fetch_commits(graph,
1080 minqueue, repo);
1081 if (err)
1082 return err;
1083 continue;
1086 if (id == NULL)
1087 break;
1089 err = got_object_open_as_commit(&commit, repo, id);
1090 if (err)
1091 break;
1092 entry = alloc_commit_queue_entry(commit, id);
1093 if (entry == NULL) {
1094 err = got_error_prefix_errno("alloc_commit_queue_entry");
1095 break;
1098 errcode = pthread_mutex_lock(&tog_mutex);
1099 if (errcode) {
1100 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1101 break;
1104 entry->idx = commits->ncommits;
1105 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1106 nqueued++;
1107 commits->ncommits++;
1109 errcode = pthread_mutex_unlock(&tog_mutex);
1110 if (errcode && err == NULL)
1111 err = got_error_set_errno(errcode,
1112 "pthread_mutex_unlock");
1115 return err;
1118 static const struct got_error *
1119 get_head_commit_id(struct got_object_id **head_id, struct got_repository *repo)
1121 const struct got_error *err = NULL;
1122 struct got_reference *head_ref;
1124 *head_id = NULL;
1126 err = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
1127 if (err)
1128 return err;
1130 err = got_ref_resolve(head_id, repo, head_ref);
1131 got_ref_close(head_ref);
1132 if (err) {
1133 *head_id = NULL;
1134 return err;
1137 return NULL;
1140 static const struct got_error *
1141 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1142 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1143 struct commit_queue *commits, int selected_idx, int limit,
1144 struct got_reflist_head *refs, const char *path, int commits_needed)
1146 const struct got_error *err = NULL;
1147 struct commit_queue_entry *entry;
1148 int ncommits, width;
1149 int author_cols = 10;
1150 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1151 char *refs_str = NULL;
1152 wchar_t *wline;
1154 entry = first;
1155 ncommits = 0;
1156 while (entry) {
1157 if (ncommits == selected_idx) {
1158 *selected = entry;
1159 break;
1161 entry = TAILQ_NEXT(entry, entry);
1162 ncommits++;
1165 if (*selected) {
1166 err = got_object_id_str(&id_str, (*selected)->id);
1167 if (err)
1168 return err;
1169 if (refs) {
1170 err = build_refs_str(&refs_str, refs, (*selected)->id);
1171 if (err)
1172 goto done;
1176 if (commits_needed == 0)
1177 halfdelay(10); /* disable fast refresh */
1179 if (asprintf(&ncommits_str, " [%d/%d] %s",
1180 entry ? entry->idx + 1 : 0, commits->ncommits,
1181 commits_needed > 0 ? "loading... " :
1182 (refs_str ? refs_str : "")) == -1) {
1183 err = got_error_prefix_errno("asprintf");
1184 goto done;
1187 if (path && strcmp(path, "/") != 0) {
1188 if (asprintf(&header, "commit %s %s%s",
1189 id_str ? id_str : "........................................",
1190 path, ncommits_str) == -1) {
1191 err = got_error_prefix_errno("asprintf");
1192 header = NULL;
1193 goto done;
1195 } else if (asprintf(&header, "commit %s%s",
1196 id_str ? id_str : "........................................",
1197 ncommits_str) == -1) {
1198 err = got_error_prefix_errno("asprintf");
1199 header = NULL;
1200 goto done;
1202 err = format_line(&wline, &width, header, view->ncols);
1203 if (err)
1204 goto done;
1206 werase(view->window);
1208 if (view_needs_focus_indication(view))
1209 wstandout(view->window);
1210 waddwstr(view->window, wline);
1211 while (width < view->ncols) {
1212 waddch(view->window, ' ');
1213 width++;
1215 if (view_needs_focus_indication(view))
1216 wstandend(view->window);
1217 free(wline);
1218 if (limit <= 1)
1219 goto done;
1221 /* Grow author column size if necessary. */
1222 entry = first;
1223 ncommits = 0;
1224 while (entry) {
1225 char *author;
1226 wchar_t *wauthor;
1227 int width;
1228 if (ncommits >= limit - 1)
1229 break;
1230 author = strdup(got_object_commit_get_author(entry->commit));
1231 if (author == NULL) {
1232 err = got_error_prefix_errno("strdup");
1233 goto done;
1235 err = format_author(&wauthor, &width, author, COLS);
1236 if (author_cols < width)
1237 author_cols = width;
1238 free(wauthor);
1239 free(author);
1240 entry = TAILQ_NEXT(entry, entry);
1243 entry = first;
1244 *last = first;
1245 ncommits = 0;
1246 while (entry) {
1247 if (ncommits >= limit - 1)
1248 break;
1249 if (ncommits == selected_idx)
1250 wstandout(view->window);
1251 err = draw_commit(view, entry->commit, entry->id, refs,
1252 author_cols);
1253 if (ncommits == selected_idx)
1254 wstandend(view->window);
1255 if (err)
1256 break;
1257 ncommits++;
1258 *last = entry;
1259 entry = TAILQ_NEXT(entry, entry);
1262 view_vborder(view);
1263 done:
1264 free(id_str);
1265 free(refs_str);
1266 free(ncommits_str);
1267 free(header);
1268 return err;
1271 static void
1272 scroll_up(struct tog_view *view,
1273 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1274 struct commit_queue *commits)
1276 struct commit_queue_entry *entry;
1277 int nscrolled = 0;
1279 entry = TAILQ_FIRST(&commits->head);
1280 if (*first_displayed_entry == entry) {
1281 view_flash(view);
1282 return;
1285 entry = *first_displayed_entry;
1286 while (entry && nscrolled < maxscroll) {
1287 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1288 if (entry) {
1289 *first_displayed_entry = entry;
1290 nscrolled++;
1295 static const struct got_error *
1296 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1297 pthread_cond_t *need_commits)
1299 int errcode;
1300 int max_wait = 20;
1302 halfdelay(1); /* fast refresh while loading commits */
1304 while (*commits_needed > 0) {
1305 if (*log_complete)
1306 break;
1308 /* Wake the log thread. */
1309 errcode = pthread_cond_signal(need_commits);
1310 if (errcode)
1311 return got_error_set_errno(errcode,
1312 "pthread_cond_signal");
1313 errcode = pthread_mutex_unlock(&tog_mutex);
1314 if (errcode)
1315 return got_error_set_errno(errcode,
1316 "pthread_mutex_unlock");
1317 pthread_yield();
1318 errcode = pthread_mutex_lock(&tog_mutex);
1319 if (errcode)
1320 return got_error_set_errno(errcode,
1321 "pthread_mutex_lock");
1323 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1325 * Thread is not done yet; lose a key press
1326 * and let the user retry... this way the GUI
1327 * remains interactive while logging deep paths
1328 * with few commits in history.
1330 return NULL;
1334 return NULL;
1337 static const struct got_error *
1338 scroll_down(struct tog_view *view,
1339 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1340 struct commit_queue_entry **last_displayed_entry,
1341 struct commit_queue *commits, int *log_complete, int *commits_needed,
1342 pthread_cond_t *need_commits)
1344 const struct got_error *err = NULL;
1345 struct commit_queue_entry *pentry;
1346 int nscrolled = 0;
1348 if (*last_displayed_entry == NULL)
1349 return NULL;
1351 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1352 if (pentry == NULL && !*log_complete) {
1354 * Ask the log thread for required amount of commits
1355 * plus some amount of pre-fetching.
1357 (*commits_needed) += maxscroll + 20;
1358 err = trigger_log_thread(0, commits_needed, log_complete,
1359 need_commits);
1360 if (err)
1361 return err;
1364 do {
1365 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1366 if (pentry == NULL) {
1367 if (*log_complete)
1368 view_flash(view);
1369 break;
1372 *last_displayed_entry = pentry;
1374 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1375 if (pentry == NULL)
1376 break;
1377 *first_displayed_entry = pentry;
1378 } while (++nscrolled < maxscroll);
1380 return err;
1383 static const struct got_error *
1384 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1385 struct got_commit_object *commit, struct got_object_id *commit_id,
1386 struct tog_view *log_view, struct got_reflist_head *refs,
1387 struct got_repository *repo)
1389 const struct got_error *err;
1390 struct got_object_qid *parent_id;
1391 struct tog_view *diff_view;
1393 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1394 if (diff_view == NULL)
1395 return got_error_prefix_errno("view_open");
1397 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1398 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1399 commit_id, log_view, refs, repo);
1400 if (err == NULL)
1401 *new_view = diff_view;
1402 return err;
1405 static const struct got_error *
1406 browse_commit(struct tog_view **new_view, int begin_x,
1407 struct commit_queue_entry *entry, struct got_reflist_head *refs,
1408 struct got_repository *repo)
1410 const struct got_error *err = NULL;
1411 struct got_tree_object *tree;
1412 struct tog_view *tree_view;
1414 err = got_object_open_as_tree(&tree, repo,
1415 got_object_commit_get_tree_id(entry->commit));
1416 if (err)
1417 return err;
1419 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1420 if (tree_view == NULL)
1421 return got_error_prefix_errno("view_open");
1423 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1424 if (err)
1425 got_object_tree_close(tree);
1426 else
1427 *new_view = tree_view;
1428 return err;
1431 static void *
1432 log_thread(void *arg)
1434 const struct got_error *err = NULL;
1435 int errcode = 0;
1436 struct tog_log_thread_args *a = arg;
1437 int done = 0;
1439 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1440 if (err)
1441 return (void *)err;
1443 while (!done && !err) {
1444 err = queue_commits(a->graph, a->commits, 1, a->repo,
1445 a->in_repo_path);
1446 if (err) {
1447 if (err->code != GOT_ERR_ITER_COMPLETED)
1448 return (void *)err;
1449 err = NULL;
1450 done = 1;
1451 } else if (a->commits_needed > 0)
1452 a->commits_needed--;
1454 errcode = pthread_mutex_lock(&tog_mutex);
1455 if (errcode) {
1456 err = got_error_set_errno(errcode,
1457 "pthread_mutex_lock");
1458 break;
1459 } else if (*a->quit)
1460 done = 1;
1461 else if (*a->first_displayed_entry == NULL) {
1462 *a->first_displayed_entry =
1463 TAILQ_FIRST(&a->commits->head);
1464 *a->selected_entry = *a->first_displayed_entry;
1467 if (done)
1468 a->commits_needed = 0;
1469 else if (a->commits_needed == 0) {
1470 errcode = pthread_cond_wait(&a->need_commits,
1471 &tog_mutex);
1472 if (errcode)
1473 err = got_error_set_errno(errcode,
1474 "pthread_cond_wait");
1477 errcode = pthread_mutex_unlock(&tog_mutex);
1478 if (errcode && err == NULL)
1479 err = got_error_set_errno(errcode,
1480 "pthread_mutex_unlock");
1482 a->log_complete = 1;
1483 return (void *)err;
1486 static const struct got_error *
1487 stop_log_thread(struct tog_log_view_state *s)
1489 const struct got_error *err = NULL;
1490 int errcode;
1492 if (s->thread) {
1493 s->quit = 1;
1494 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1495 if (errcode)
1496 return got_error_set_errno(errcode,
1497 "pthread_cond_signal");
1498 errcode = pthread_mutex_unlock(&tog_mutex);
1499 if (errcode)
1500 return got_error_set_errno(errcode,
1501 "pthread_mutex_unlock");
1502 errcode = pthread_join(s->thread, (void **)&err);
1503 if (errcode)
1504 return got_error_set_errno(errcode, "pthread_join");
1505 errcode = pthread_mutex_lock(&tog_mutex);
1506 if (errcode)
1507 return got_error_set_errno(errcode,
1508 "pthread_mutex_lock");
1509 s->thread = NULL;
1512 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1513 if (errcode && err == NULL)
1514 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1516 if (s->thread_args.repo) {
1517 got_repo_close(s->thread_args.repo);
1518 s->thread_args.repo = NULL;
1521 if (s->thread_args.graph) {
1522 got_commit_graph_close(s->thread_args.graph);
1523 s->thread_args.graph = NULL;
1526 return err;
1529 static const struct got_error *
1530 close_log_view(struct tog_view *view)
1532 const struct got_error *err = NULL;
1533 struct tog_log_view_state *s = &view->state.log;
1535 err = stop_log_thread(s);
1536 free_commits(&s->commits);
1537 free(s->in_repo_path);
1538 s->in_repo_path = NULL;
1539 free(s->start_id);
1540 s->start_id = NULL;
1541 return err;
1544 static const struct got_error *
1545 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1546 struct got_reflist_head *refs, struct got_repository *repo,
1547 const char *path, int check_disk)
1549 const struct got_error *err = NULL;
1550 struct tog_log_view_state *s = &view->state.log;
1551 struct got_repository *thread_repo = NULL;
1552 struct got_commit_graph *thread_graph = NULL;
1553 int errcode;
1555 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1556 if (err != NULL)
1557 goto done;
1559 /* The commit queue only contains commits being displayed. */
1560 TAILQ_INIT(&s->commits.head);
1561 s->commits.ncommits = 0;
1563 s->refs = refs;
1564 s->repo = repo;
1565 s->start_id = got_object_id_dup(start_id);
1566 if (s->start_id == NULL) {
1567 err = got_error_prefix_errno("got_object_id_dup");
1568 goto done;
1571 view->show = show_log_view;
1572 view->input = input_log_view;
1573 view->close = close_log_view;
1575 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1576 if (err)
1577 goto done;
1578 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1579 0, thread_repo);
1580 if (err)
1581 goto done;
1583 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1584 if (errcode) {
1585 err = got_error_set_errno(errcode, "pthread_cond_init");
1586 goto done;
1589 s->thread_args.commits_needed = view->nlines;
1590 s->thread_args.graph = thread_graph;
1591 s->thread_args.commits = &s->commits;
1592 s->thread_args.in_repo_path = s->in_repo_path;
1593 s->thread_args.start_id = s->start_id;
1594 s->thread_args.repo = thread_repo;
1595 s->thread_args.log_complete = 0;
1596 s->thread_args.quit = &s->quit;
1597 s->thread_args.view = view;
1598 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1599 s->thread_args.selected_entry = &s->selected_entry;
1600 done:
1601 if (err)
1602 close_log_view(view);
1603 return err;
1606 static const struct got_error *
1607 show_log_view(struct tog_view *view)
1609 struct tog_log_view_state *s = &view->state.log;
1611 if (s->thread == NULL) {
1612 int errcode = pthread_create(&s->thread, NULL, log_thread,
1613 &s->thread_args);
1614 if (errcode)
1615 return got_error_set_errno(errcode, "pthread_create");
1618 return draw_commits(view, &s->last_displayed_entry,
1619 &s->selected_entry, s->first_displayed_entry,
1620 &s->commits, s->selected, view->nlines, s->refs,
1621 s->in_repo_path, s->thread_args.commits_needed);
1624 static const struct got_error *
1625 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1626 struct tog_view **focus_view, struct tog_view *view, int ch)
1628 const struct got_error *err = NULL;
1629 struct tog_log_view_state *s = &view->state.log;
1630 char *parent_path;
1631 struct tog_view *diff_view = NULL, *tree_view = NULL;
1632 int begin_x = 0;
1634 switch (ch) {
1635 case 'q':
1636 s->quit = 1;
1637 break;
1638 case 'k':
1639 case KEY_UP:
1640 case '<':
1641 case ',':
1642 if (s->first_displayed_entry == NULL)
1643 break;
1644 if (s->selected > 0)
1645 s->selected--;
1646 if (s->selected > 0)
1647 break;
1648 scroll_up(view, &s->first_displayed_entry, 1,
1649 &s->commits);
1650 break;
1651 case KEY_PPAGE:
1652 if (s->first_displayed_entry == NULL)
1653 break;
1654 if (TAILQ_FIRST(&s->commits.head) ==
1655 s->first_displayed_entry) {
1656 if (s->selected == 0) {
1657 view_flash(view);
1658 break;
1660 s->selected = 0;
1661 break;
1663 scroll_up(view, &s->first_displayed_entry,
1664 view->nlines, &s->commits);
1665 break;
1666 case 'j':
1667 case KEY_DOWN:
1668 case '>':
1669 case '.':
1670 if (s->first_displayed_entry == NULL)
1671 break;
1672 if (s->selected < MIN(view->nlines - 2,
1673 s->commits.ncommits - 1)) {
1674 s->selected++;
1675 break;
1677 err = scroll_down(view, &s->first_displayed_entry, 1,
1678 &s->last_displayed_entry, &s->commits,
1679 &s->thread_args.log_complete,
1680 &s->thread_args.commits_needed,
1681 &s->thread_args.need_commits);
1682 break;
1683 case KEY_NPAGE: {
1684 struct commit_queue_entry *first;
1685 first = s->first_displayed_entry;
1686 if (first == NULL)
1687 break;
1688 err = scroll_down(view, &s->first_displayed_entry,
1689 view->nlines, &s->last_displayed_entry,
1690 &s->commits, &s->thread_args.log_complete,
1691 &s->thread_args.commits_needed,
1692 &s->thread_args.need_commits);
1693 if (first == s->first_displayed_entry &&
1694 s->selected < MIN(view->nlines - 2,
1695 s->commits.ncommits - 1)) {
1696 /* can't scroll further down */
1697 s->selected = MIN(view->nlines - 2,
1698 s->commits.ncommits - 1);
1700 err = NULL;
1701 break;
1703 case KEY_RESIZE:
1704 if (s->selected > view->nlines - 2)
1705 s->selected = view->nlines - 2;
1706 if (s->selected > s->commits.ncommits - 1)
1707 s->selected = s->commits.ncommits - 1;
1708 break;
1709 case KEY_ENTER:
1710 case '\r':
1711 if (s->selected_entry == NULL)
1712 break;
1713 if (view_is_parent_view(view))
1714 begin_x = view_split_begin_x(view->begin_x);
1715 err = open_diff_view_for_commit(&diff_view, begin_x,
1716 s->selected_entry->commit, s->selected_entry->id,
1717 view, s->refs, s->repo);
1718 if (err)
1719 break;
1720 if (view_is_parent_view(view)) {
1721 err = view_close_child(view);
1722 if (err)
1723 return err;
1724 err = view_set_child(view, diff_view);
1725 if (err) {
1726 view_close(diff_view);
1727 break;
1729 *focus_view = diff_view;
1730 view->child_focussed = 1;
1731 } else
1732 *new_view = diff_view;
1733 break;
1734 case 't':
1735 if (s->selected_entry == NULL)
1736 break;
1737 if (view_is_parent_view(view))
1738 begin_x = view_split_begin_x(view->begin_x);
1739 err = browse_commit(&tree_view, begin_x,
1740 s->selected_entry, s->refs, s->repo);
1741 if (err)
1742 break;
1743 if (view_is_parent_view(view)) {
1744 err = view_close_child(view);
1745 if (err)
1746 return err;
1747 err = view_set_child(view, tree_view);
1748 if (err) {
1749 view_close(tree_view);
1750 break;
1752 *focus_view = tree_view;
1753 view->child_focussed = 1;
1754 } else
1755 *new_view = tree_view;
1756 break;
1757 case KEY_BACKSPACE:
1758 if (strcmp(s->in_repo_path, "/") == 0)
1759 break;
1760 parent_path = dirname(s->in_repo_path);
1761 if (parent_path && strcmp(parent_path, ".") != 0) {
1762 struct tog_view *lv;
1763 err = stop_log_thread(s);
1764 if (err)
1765 return err;
1766 lv = view_open(view->nlines, view->ncols,
1767 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1768 if (lv == NULL)
1769 return got_error_prefix_errno(
1770 "view_open");
1771 err = open_log_view(lv, s->start_id, s->refs,
1772 s->repo, parent_path, 0);
1773 if (err)
1774 return err;;
1775 if (view_is_parent_view(view))
1776 *new_view = lv;
1777 else {
1778 view_set_child(view->parent, lv);
1779 *focus_view = lv;
1781 return NULL;
1783 break;
1784 default:
1785 break;
1788 return err;
1791 static const struct got_error *
1792 apply_unveil(const char *repo_path, const char *worktree_path)
1794 const struct got_error *error;
1796 if (repo_path && unveil(repo_path, "r") != 0)
1797 return got_error_prefix_errno2("unveil", repo_path);
1799 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1800 return got_error_prefix_errno2("unveil", worktree_path);
1802 if (unveil("/tmp", "rwc") != 0)
1803 return got_error_prefix_errno2("unveil", "/tmp");
1805 error = got_privsep_unveil_exec_helpers();
1806 if (error != NULL)
1807 return error;
1809 if (unveil(NULL, NULL) != 0)
1810 return got_error_prefix_errno("unveil");
1812 return NULL;
1815 static void
1816 init_curses(void)
1818 initscr();
1819 cbreak();
1820 halfdelay(1); /* Do fast refresh while initial view is loading. */
1821 noecho();
1822 nonl();
1823 intrflush(stdscr, FALSE);
1824 keypad(stdscr, TRUE);
1825 curs_set(0);
1826 signal(SIGWINCH, tog_sigwinch);
1829 static const struct got_error *
1830 cmd_log(int argc, char *argv[])
1832 const struct got_error *error;
1833 struct got_repository *repo = NULL;
1834 struct got_worktree *worktree = NULL;
1835 struct got_reflist_head refs;
1836 struct got_object_id *start_id = NULL;
1837 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1838 char *start_commit = NULL;
1839 int ch;
1840 struct tog_view *view;
1842 SIMPLEQ_INIT(&refs);
1844 #ifndef PROFILE
1845 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1846 NULL) == -1)
1847 err(1, "pledge");
1848 #endif
1850 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1851 switch (ch) {
1852 case 'c':
1853 start_commit = optarg;
1854 break;
1855 case 'r':
1856 repo_path = realpath(optarg, NULL);
1857 if (repo_path == NULL)
1858 err(1, "-r option");
1859 break;
1860 default:
1861 usage_log();
1862 /* NOTREACHED */
1866 argc -= optind;
1867 argv += optind;
1869 cwd = getcwd(NULL, 0);
1870 if (cwd == NULL) {
1871 error = got_error_prefix_errno("getcwd");
1872 goto done;
1874 error = got_worktree_open(&worktree, cwd);
1875 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1876 goto done;
1877 error = NULL;
1879 if (argc == 0) {
1880 path = strdup("");
1881 if (path == NULL) {
1882 error = got_error_prefix_errno("strdup");
1883 goto done;
1885 } else if (argc == 1) {
1886 if (worktree) {
1887 error = got_worktree_resolve_path(&path, worktree,
1888 argv[0]);
1889 if (error)
1890 goto done;
1891 } else {
1892 path = strdup(argv[0]);
1893 if (path == NULL) {
1894 error = got_error_prefix_errno("strdup");
1895 goto done;
1898 } else
1899 usage_log();
1901 repo_path = worktree ?
1902 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1903 if (repo_path == NULL) {
1904 error = got_error_prefix_errno("strdup");
1905 goto done;
1908 init_curses();
1910 error = got_repo_open(&repo, repo_path);
1911 if (error != NULL)
1912 goto done;
1914 error = apply_unveil(got_repo_get_path(repo),
1915 worktree ? got_worktree_get_root_path(worktree) : NULL);
1916 if (error)
1917 goto done;
1919 if (start_commit == NULL)
1920 error = get_head_commit_id(&start_id, repo);
1921 else
1922 error = got_object_resolve_id_str(&start_id, repo,
1923 start_commit);
1924 if (error != NULL)
1925 goto done;
1927 error = got_ref_list(&refs, repo);
1928 if (error)
1929 goto done;
1931 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
1932 if (view == NULL) {
1933 error = got_error_prefix_errno("view_open");
1934 goto done;
1936 error = open_log_view(view, start_id, &refs, repo, path, 1);
1937 if (error)
1938 goto done;
1939 error = view_loop(view);
1940 done:
1941 free(repo_path);
1942 free(cwd);
1943 free(path);
1944 free(start_id);
1945 if (repo)
1946 got_repo_close(repo);
1947 if (worktree)
1948 got_worktree_close(worktree);
1949 got_ref_list_free(&refs);
1950 return error;
1953 __dead static void
1954 usage_diff(void)
1956 endwin();
1957 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
1958 getprogname());
1959 exit(1);
1962 static char *
1963 parse_next_line(FILE *f, size_t *len)
1965 char *line;
1966 size_t linelen;
1967 size_t lineno;
1968 const char delim[3] = { '\0', '\0', '\0'};
1970 line = fparseln(f, &linelen, &lineno, delim, 0);
1971 if (len)
1972 *len = linelen;
1973 return line;
1976 static const struct got_error *
1977 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
1978 int *last_displayed_line, int *eof, int max_lines,
1979 char * header)
1981 const struct got_error *err;
1982 int nlines = 0, nprinted = 0;
1983 char *line;
1984 size_t len;
1985 wchar_t *wline;
1986 int width;
1988 rewind(f);
1989 werase(view->window);
1991 if (header) {
1992 err = format_line(&wline, &width, header, view->ncols);
1993 if (err) {
1994 return err;
1997 if (view_needs_focus_indication(view))
1998 wstandout(view->window);
1999 waddwstr(view->window, wline);
2000 if (view_needs_focus_indication(view))
2001 wstandend(view->window);
2002 if (width < view->ncols)
2003 waddch(view->window, '\n');
2005 if (max_lines <= 1)
2006 return NULL;
2007 max_lines--;
2010 *eof = 0;
2011 while (nprinted < max_lines) {
2012 line = parse_next_line(f, &len);
2013 if (line == NULL) {
2014 *eof = 1;
2015 break;
2017 if (++nlines < *first_displayed_line) {
2018 free(line);
2019 continue;
2022 err = format_line(&wline, &width, line, view->ncols);
2023 if (err) {
2024 free(line);
2025 return err;
2027 waddwstr(view->window, wline);
2028 if (width < view->ncols)
2029 waddch(view->window, '\n');
2030 if (++nprinted == 1)
2031 *first_displayed_line = nlines;
2032 free(line);
2033 free(wline);
2034 wline = NULL;
2036 *last_displayed_line = nlines;
2038 view_vborder(view);
2040 return NULL;
2043 static char *
2044 get_datestr(time_t *time, char *datebuf)
2046 char *p, *s = ctime_r(time, datebuf);
2047 p = strchr(s, '\n');
2048 if (p)
2049 *p = '\0';
2050 return s;
2053 static const struct got_error *
2054 write_commit_info(struct got_object_id *commit_id,
2055 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2057 const struct got_error *err = NULL;
2058 char datebuf[26];
2059 struct got_commit_object *commit;
2060 char *id_str = NULL;
2061 time_t committer_time;
2062 const char *author, *committer;
2063 char *refs_str = NULL;
2065 if (refs) {
2066 err = build_refs_str(&refs_str, refs, commit_id);
2067 if (err)
2068 return err;
2071 err = got_object_open_as_commit(&commit, repo, commit_id);
2072 if (err)
2073 return err;
2075 err = got_object_id_str(&id_str, commit_id);
2076 if (err) {
2077 err = got_error_prefix_errno("got_object_id_str");
2078 goto done;
2081 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2082 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2083 err = got_error_prefix_errno("fprintf");
2084 goto done;
2086 if (fprintf(outfile, "from: %s\n",
2087 got_object_commit_get_author(commit)) < 0) {
2088 err = got_error_prefix_errno("fprintf");
2089 goto done;
2091 committer_time = got_object_commit_get_committer_time(commit);
2092 if (fprintf(outfile, "date: %s UTC\n",
2093 get_datestr(&committer_time, datebuf)) < 0) {
2094 err = got_error_prefix_errno("fprintf");
2095 goto done;
2097 author = got_object_commit_get_author(commit);
2098 committer = got_object_commit_get_committer(commit);
2099 if (strcmp(author, committer) != 0 &&
2100 fprintf(outfile, "via: %s\n", committer) < 0) {
2101 err = got_error_prefix_errno("fprintf");
2102 goto done;
2104 if (fprintf(outfile, "%s\n",
2105 got_object_commit_get_logmsg(commit)) < 0) {
2106 err = got_error_prefix_errno("fprintf");
2107 goto done;
2109 done:
2110 free(id_str);
2111 free(refs_str);
2112 got_object_commit_close(commit);
2113 return err;
2116 static const struct got_error *
2117 create_diff(struct tog_diff_view_state *s)
2119 const struct got_error *err = NULL;
2120 FILE *f = NULL;
2121 int obj_type;
2123 f = got_opentemp();
2124 if (f == NULL) {
2125 err = got_error_prefix_errno("got_opentemp");
2126 goto done;
2128 if (s->f && fclose(s->f) != 0) {
2129 err = got_error_prefix_errno("fclose");
2130 goto done;
2132 s->f = f;
2134 if (s->id1)
2135 err = got_object_get_type(&obj_type, s->repo, s->id1);
2136 else
2137 err = got_object_get_type(&obj_type, s->repo, s->id2);
2138 if (err)
2139 goto done;
2141 switch (obj_type) {
2142 case GOT_OBJ_TYPE_BLOB:
2143 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2144 s->diff_context, s->repo, f);
2145 break;
2146 case GOT_OBJ_TYPE_TREE:
2147 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2148 s->diff_context, s->repo, f);
2149 break;
2150 case GOT_OBJ_TYPE_COMMIT: {
2151 const struct got_object_id_queue *parent_ids;
2152 struct got_object_qid *pid;
2153 struct got_commit_object *commit2;
2155 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2156 if (err)
2157 break;
2158 /* Show commit info if we're diffing to a parent/root commit. */
2159 if (s->id1 == NULL)
2160 write_commit_info(s->id2, s->refs, s->repo, f);
2161 else {
2162 parent_ids = got_object_commit_get_parent_ids(commit2);
2163 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2164 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2165 write_commit_info(s->id2, s->refs,
2166 s->repo, f);
2167 break;
2171 got_object_commit_close(commit2);
2173 err = got_diff_objects_as_commits(s->id1, s->id2,
2174 s->diff_context, s->repo, f);
2175 break;
2177 default:
2178 err = got_error(GOT_ERR_OBJ_TYPE);
2179 break;
2181 done:
2182 if (f && fflush(f) != 0 && err == NULL)
2183 err = got_error_prefix_errno("fflush");
2184 return err;
2187 static void
2188 diff_view_indicate_progress(struct tog_view *view)
2190 werase(view->window);
2191 waddstr(view->window, "diffing...");
2192 update_panels();
2193 doupdate();
2196 static const struct got_error *
2197 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2198 struct got_object_id *id2, struct tog_view *log_view,
2199 struct got_reflist_head *refs, struct got_repository *repo)
2201 const struct got_error *err;
2203 if (id1 != NULL && id2 != NULL) {
2204 int type1, type2;
2205 err = got_object_get_type(&type1, repo, id1);
2206 if (err)
2207 return err;
2208 err = got_object_get_type(&type2, repo, id2);
2209 if (err)
2210 return err;
2212 if (type1 != type2)
2213 return got_error(GOT_ERR_OBJ_TYPE);
2216 if (id1) {
2217 view->state.diff.id1 = got_object_id_dup(id1);
2218 if (view->state.diff.id1 == NULL)
2219 return got_error_prefix_errno("got_object_id_dup");
2220 } else
2221 view->state.diff.id1 = NULL;
2223 view->state.diff.id2 = got_object_id_dup(id2);
2224 if (view->state.diff.id2 == NULL) {
2225 free(view->state.diff.id1);
2226 view->state.diff.id1 = NULL;
2227 return got_error_prefix_errno("got_object_id_dup");
2229 view->state.diff.f = NULL;
2230 view->state.diff.first_displayed_line = 1;
2231 view->state.diff.last_displayed_line = view->nlines;
2232 view->state.diff.diff_context = 3;
2233 view->state.diff.log_view = log_view;
2234 view->state.diff.repo = repo;
2235 view->state.diff.refs = refs;
2237 if (log_view && view_is_splitscreen(view))
2238 show_log_view(log_view); /* draw vborder */
2239 diff_view_indicate_progress(view);
2241 err = create_diff(&view->state.diff);
2242 if (err) {
2243 free(view->state.diff.id1);
2244 view->state.diff.id1 = NULL;
2245 free(view->state.diff.id2);
2246 view->state.diff.id2 = NULL;
2247 return err;
2250 view->show = show_diff_view;
2251 view->input = input_diff_view;
2252 view->close = close_diff_view;
2254 return NULL;
2257 static const struct got_error *
2258 close_diff_view(struct tog_view *view)
2260 const struct got_error *err = NULL;
2262 free(view->state.diff.id1);
2263 view->state.diff.id1 = NULL;
2264 free(view->state.diff.id2);
2265 view->state.diff.id2 = NULL;
2266 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2267 err = got_error_prefix_errno("fclose");
2268 return err;
2271 static const struct got_error *
2272 show_diff_view(struct tog_view *view)
2274 const struct got_error *err;
2275 struct tog_diff_view_state *s = &view->state.diff;
2276 char *id_str1 = NULL, *id_str2, *header;
2278 if (s->id1) {
2279 err = got_object_id_str(&id_str1, s->id1);
2280 if (err)
2281 return err;
2283 err = got_object_id_str(&id_str2, s->id2);
2284 if (err)
2285 return err;
2287 if (asprintf(&header, "diff %s %s",
2288 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2289 err = got_error_prefix_errno("asprintf");
2290 free(id_str1);
2291 free(id_str2);
2292 return err;
2294 free(id_str1);
2295 free(id_str2);
2297 return draw_file(view, s->f, &s->first_displayed_line,
2298 &s->last_displayed_line, &s->eof, view->nlines,
2299 header);
2302 static const struct got_error *
2303 set_selected_commit(struct tog_diff_view_state *s,
2304 struct commit_queue_entry *entry)
2306 const struct got_error *err;
2307 const struct got_object_id_queue *parent_ids;
2308 struct got_commit_object *selected_commit;
2309 struct got_object_qid *pid;
2311 free(s->id2);
2312 s->id2 = got_object_id_dup(entry->id);
2313 if (s->id2 == NULL)
2314 return got_error_prefix_errno("got_object_id_dup");
2316 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2317 if (err)
2318 return err;
2319 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2320 free(s->id1);
2321 pid = SIMPLEQ_FIRST(parent_ids);
2322 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2323 got_object_commit_close(selected_commit);
2324 return NULL;
2327 static const struct got_error *
2328 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2329 struct tog_view **focus_view, struct tog_view *view, int ch)
2331 const struct got_error *err = NULL;
2332 struct tog_diff_view_state *s = &view->state.diff;
2333 struct tog_log_view_state *ls;
2334 struct commit_queue_entry *entry;
2335 int i;
2337 switch (ch) {
2338 case 'k':
2339 case KEY_UP:
2340 if (s->first_displayed_line > 1)
2341 s->first_displayed_line--;
2342 else
2343 view_flash(view);
2344 break;
2345 case KEY_PPAGE:
2346 if (s->first_displayed_line == 1) {
2347 view_flash(view);
2348 break;
2350 i = 0;
2351 while (i++ < view->nlines - 1 &&
2352 s->first_displayed_line > 1)
2353 s->first_displayed_line--;
2354 break;
2355 case 'j':
2356 case KEY_DOWN:
2357 if (!s->eof)
2358 s->first_displayed_line++;
2359 else
2360 view_flash(view);
2361 break;
2362 case KEY_NPAGE:
2363 case ' ':
2364 if (s->eof) {
2365 view_flash(view);
2366 break;
2368 i = 0;
2369 while (!s->eof && i++ < view->nlines - 1) {
2370 char *line;
2371 line = parse_next_line(s->f, NULL);
2372 s->first_displayed_line++;
2373 if (line == NULL)
2374 break;
2376 break;
2377 case '[':
2378 if (s->diff_context > 0) {
2379 s->diff_context--;
2380 diff_view_indicate_progress(view);
2381 err = create_diff(s);
2383 break;
2384 case ']':
2385 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2386 s->diff_context++;
2387 diff_view_indicate_progress(view);
2388 err = create_diff(s);
2390 break;
2391 case '<':
2392 case ',':
2393 if (s->log_view == NULL)
2394 break;
2395 ls = &s->log_view->state.log;
2396 entry = TAILQ_PREV(ls->selected_entry,
2397 commit_queue_head, entry);
2398 if (entry == NULL)
2399 break;
2401 err = input_log_view(NULL, NULL, NULL, s->log_view,
2402 KEY_UP);
2403 if (err)
2404 break;
2406 err = set_selected_commit(s, entry);
2407 if (err)
2408 break;
2410 s->first_displayed_line = 1;
2411 s->last_displayed_line = view->nlines;
2413 diff_view_indicate_progress(view);
2414 err = create_diff(s);
2415 break;
2416 case '>':
2417 case '.':
2418 if (s->log_view == NULL)
2419 break;
2420 ls = &s->log_view->state.log;
2422 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2423 ls->thread_args.commits_needed++;
2425 /* Display "loading..." in log view. */
2426 show_log_view(s->log_view);
2427 update_panels();
2428 doupdate();
2430 err = trigger_log_thread(1 /* load_all */,
2431 &ls->thread_args.commits_needed,
2432 &ls->thread_args.log_complete,
2433 &ls->thread_args.need_commits);
2434 if (err)
2435 break;
2437 err = input_log_view(NULL, NULL, NULL, s->log_view,
2438 KEY_DOWN);
2439 if (err)
2440 break;
2442 entry = TAILQ_NEXT(ls->selected_entry, entry);
2443 if (entry == NULL)
2444 break;
2446 err = set_selected_commit(s, entry);
2447 if (err)
2448 break;
2450 s->first_displayed_line = 1;
2451 s->last_displayed_line = view->nlines;
2453 diff_view_indicate_progress(view);
2454 err = create_diff(s);
2455 break;
2456 default:
2457 break;
2460 return err;
2463 static const struct got_error *
2464 cmd_diff(int argc, char *argv[])
2466 const struct got_error *error = NULL;
2467 struct got_repository *repo = NULL;
2468 struct got_reflist_head refs;
2469 struct got_object_id *id1 = NULL, *id2 = NULL;
2470 char *repo_path = NULL;
2471 char *id_str1 = NULL, *id_str2 = NULL;
2472 int ch;
2473 struct tog_view *view;
2475 SIMPLEQ_INIT(&refs);
2477 #ifndef PROFILE
2478 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2479 NULL) == -1)
2480 err(1, "pledge");
2481 #endif
2483 while ((ch = getopt(argc, argv, "")) != -1) {
2484 switch (ch) {
2485 default:
2486 usage_diff();
2487 /* NOTREACHED */
2491 argc -= optind;
2492 argv += optind;
2494 if (argc == 0) {
2495 usage_diff(); /* TODO show local worktree changes */
2496 } else if (argc == 2) {
2497 repo_path = getcwd(NULL, 0);
2498 if (repo_path == NULL)
2499 return got_error_prefix_errno("getcwd");
2500 id_str1 = argv[0];
2501 id_str2 = argv[1];
2502 } else if (argc == 3) {
2503 repo_path = realpath(argv[0], NULL);
2504 if (repo_path == NULL)
2505 return got_error_prefix_errno2("realpath", argv[0]);
2506 id_str1 = argv[1];
2507 id_str2 = argv[2];
2508 } else
2509 usage_diff();
2511 init_curses();
2513 error = got_repo_open(&repo, repo_path);
2514 if (error)
2515 goto done;
2517 error = apply_unveil(got_repo_get_path(repo), NULL);
2518 if (error)
2519 goto done;
2521 error = got_object_resolve_id_str(&id1, repo, id_str1);
2522 if (error)
2523 goto done;
2525 error = got_object_resolve_id_str(&id2, repo, id_str2);
2526 if (error)
2527 goto done;
2529 error = got_ref_list(&refs, repo);
2530 if (error)
2531 goto done;
2533 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2534 if (view == NULL) {
2535 error = got_error_prefix_errno("view_open");
2536 goto done;
2538 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2539 if (error)
2540 goto done;
2541 error = view_loop(view);
2542 done:
2543 free(repo_path);
2544 got_repo_close(repo);
2545 got_ref_list_free(&refs);
2546 return error;
2549 __dead static void
2550 usage_blame(void)
2552 endwin();
2553 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2554 getprogname());
2555 exit(1);
2558 struct tog_blame_line {
2559 int annotated;
2560 struct got_object_id *id;
2563 static const struct got_error *
2564 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2565 const char *path, struct tog_blame_line *lines, int nlines,
2566 int blame_complete, int selected_line, int *first_displayed_line,
2567 int *last_displayed_line, int *eof, int max_lines)
2569 const struct got_error *err;
2570 int lineno = 0, nprinted = 0;
2571 char *line;
2572 size_t len;
2573 wchar_t *wline;
2574 int width, wlimit;
2575 struct tog_blame_line *blame_line;
2576 struct got_object_id *prev_id = NULL;
2577 char *id_str;
2579 err = got_object_id_str(&id_str, id);
2580 if (err)
2581 return err;
2583 rewind(f);
2584 werase(view->window);
2586 if (asprintf(&line, "commit %s", id_str) == -1) {
2587 err = got_error_prefix_errno("asprintf");
2588 free(id_str);
2589 return err;
2592 err = format_line(&wline, &width, line, view->ncols);
2593 free(line);
2594 line = NULL;
2595 if (view_needs_focus_indication(view))
2596 wstandout(view->window);
2597 waddwstr(view->window, wline);
2598 if (view_needs_focus_indication(view))
2599 wstandend(view->window);
2600 free(wline);
2601 wline = NULL;
2602 if (width < view->ncols)
2603 waddch(view->window, '\n');
2605 if (asprintf(&line, "[%d/%d] %s%s",
2606 *first_displayed_line - 1 + selected_line, nlines,
2607 blame_complete ? "" : "annotating... ", path) == -1) {
2608 free(id_str);
2609 return got_error_prefix_errno("asprintf");
2611 free(id_str);
2612 err = format_line(&wline, &width, line, view->ncols);
2613 free(line);
2614 line = NULL;
2615 if (err)
2616 return err;
2617 waddwstr(view->window, wline);
2618 free(wline);
2619 wline = NULL;
2620 if (width < view->ncols)
2621 waddch(view->window, '\n');
2623 *eof = 0;
2624 while (nprinted < max_lines - 2) {
2625 line = parse_next_line(f, &len);
2626 if (line == NULL) {
2627 *eof = 1;
2628 break;
2630 if (++lineno < *first_displayed_line) {
2631 free(line);
2632 continue;
2635 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2636 err = format_line(&wline, &width, line, wlimit);
2637 if (err) {
2638 free(line);
2639 return err;
2642 if (view->focussed && nprinted == selected_line - 1)
2643 wstandout(view->window);
2645 blame_line = &lines[lineno - 1];
2646 if (blame_line->annotated && prev_id &&
2647 got_object_id_cmp(prev_id, blame_line->id) == 0)
2648 waddstr(view->window, " ");
2649 else if (blame_line->annotated) {
2650 char *id_str;
2651 err = got_object_id_str(&id_str, blame_line->id);
2652 if (err) {
2653 free(line);
2654 free(wline);
2655 return err;
2657 wprintw(view->window, "%.8s ", id_str);
2658 free(id_str);
2659 prev_id = blame_line->id;
2660 } else {
2661 waddstr(view->window, "........ ");
2662 prev_id = NULL;
2665 waddwstr(view->window, wline);
2666 while (width < wlimit) {
2667 waddch(view->window, ' ');
2668 width++;
2670 if (view->focussed && nprinted == selected_line - 1)
2671 wstandend(view->window);
2672 if (++nprinted == 1)
2673 *first_displayed_line = lineno;
2674 free(line);
2675 free(wline);
2676 wline = NULL;
2678 *last_displayed_line = lineno;
2680 view_vborder(view);
2682 return NULL;
2685 static const struct got_error *
2686 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2688 const struct got_error *err = NULL;
2689 struct tog_blame_cb_args *a = arg;
2690 struct tog_blame_line *line;
2691 int errcode;
2693 if (nlines != a->nlines ||
2694 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2695 return got_error(GOT_ERR_RANGE);
2697 errcode = pthread_mutex_lock(&tog_mutex);
2698 if (errcode)
2699 return got_error_set_errno(errcode, "pthread_mutex_lock");
2701 if (*a->quit) { /* user has quit the blame view */
2702 err = got_error(GOT_ERR_ITER_COMPLETED);
2703 goto done;
2706 if (lineno == -1)
2707 goto done; /* no change in this commit */
2709 line = &a->lines[lineno - 1];
2710 if (line->annotated)
2711 goto done;
2713 line->id = got_object_id_dup(id);
2714 if (line->id == NULL) {
2715 err = got_error_prefix_errno("got_object_id_dup");
2716 goto done;
2718 line->annotated = 1;
2719 done:
2720 errcode = pthread_mutex_unlock(&tog_mutex);
2721 if (errcode)
2722 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2723 return err;
2726 static void *
2727 blame_thread(void *arg)
2729 const struct got_error *err;
2730 struct tog_blame_thread_args *ta = arg;
2731 struct tog_blame_cb_args *a = ta->cb_args;
2732 int errcode;
2734 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2735 blame_cb, ta->cb_args);
2737 errcode = pthread_mutex_lock(&tog_mutex);
2738 if (errcode)
2739 return (void *)got_error_set_errno(errcode,
2740 "pthread_mutex_lock");
2742 got_repo_close(ta->repo);
2743 ta->repo = NULL;
2744 *ta->complete = 1;
2746 errcode = pthread_mutex_unlock(&tog_mutex);
2747 if (errcode && err == NULL)
2748 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2750 return (void *)err;
2753 static struct got_object_id *
2754 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2755 int selected_line)
2757 struct tog_blame_line *line;
2759 line = &lines[first_displayed_line - 1 + selected_line - 1];
2760 if (!line->annotated)
2761 return NULL;
2763 return line->id;
2766 static const struct got_error *
2767 stop_blame(struct tog_blame *blame)
2769 const struct got_error *err = NULL;
2770 int i;
2772 if (blame->thread) {
2773 int errcode;
2774 errcode = pthread_mutex_unlock(&tog_mutex);
2775 if (errcode)
2776 return got_error_set_errno(errcode,
2777 "pthread_mutex_unlock");
2778 errcode = pthread_join(blame->thread, (void **)&err);
2779 if (errcode)
2780 return got_error_set_errno(errcode, "pthread_join");
2781 errcode = pthread_mutex_lock(&tog_mutex);
2782 if (errcode)
2783 return got_error_set_errno(errcode,
2784 "pthread_mutex_lock");
2785 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2786 err = NULL;
2787 blame->thread = NULL;
2789 if (blame->thread_args.repo) {
2790 got_repo_close(blame->thread_args.repo);
2791 blame->thread_args.repo = NULL;
2793 if (blame->f) {
2794 if (fclose(blame->f) != 0 && err == NULL)
2795 err = got_error_prefix_errno("fclose");
2796 blame->f = NULL;
2798 if (blame->lines) {
2799 for (i = 0; i < blame->nlines; i++)
2800 free(blame->lines[i].id);
2801 free(blame->lines);
2802 blame->lines = NULL;
2804 free(blame->cb_args.commit_id);
2805 blame->cb_args.commit_id = NULL;
2807 return err;
2810 static const struct got_error *
2811 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2812 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2813 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2814 struct got_repository *repo)
2816 const struct got_error *err = NULL;
2817 struct got_blob_object *blob = NULL;
2818 struct got_repository *thread_repo = NULL;
2819 struct got_object_id *obj_id = NULL;
2820 int obj_type;
2822 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2823 if (err)
2824 return err;
2825 if (obj_id == NULL)
2826 return got_error(GOT_ERR_NO_OBJ);
2828 err = got_object_get_type(&obj_type, repo, obj_id);
2829 if (err)
2830 goto done;
2832 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2833 err = got_error(GOT_ERR_OBJ_TYPE);
2834 goto done;
2837 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2838 if (err)
2839 goto done;
2840 blame->f = got_opentemp();
2841 if (blame->f == NULL) {
2842 err = got_error_prefix_errno("got_opentemp");
2843 goto done;
2845 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2846 blame->f, blob);
2847 if (err)
2848 goto done;
2850 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2851 if (blame->lines == NULL) {
2852 err = got_error_prefix_errno("calloc");
2853 goto done;
2856 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2857 if (err)
2858 goto done;
2860 blame->cb_args.view = view;
2861 blame->cb_args.lines = blame->lines;
2862 blame->cb_args.nlines = blame->nlines;
2863 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2864 if (blame->cb_args.commit_id == NULL) {
2865 err = got_error_prefix_errno("got_object_id_dup");
2866 goto done;
2868 blame->cb_args.quit = done;
2870 blame->thread_args.path = path;
2871 blame->thread_args.repo = thread_repo;
2872 blame->thread_args.cb_args = &blame->cb_args;
2873 blame->thread_args.complete = blame_complete;
2874 *blame_complete = 0;
2876 done:
2877 if (blob)
2878 got_object_blob_close(blob);
2879 free(obj_id);
2880 if (err)
2881 stop_blame(blame);
2882 return err;
2885 static const struct got_error *
2886 open_blame_view(struct tog_view *view, char *path,
2887 struct got_object_id *commit_id, struct got_reflist_head *refs,
2888 struct got_repository *repo)
2890 const struct got_error *err = NULL;
2891 struct tog_blame_view_state *s = &view->state.blame;
2893 SIMPLEQ_INIT(&s->blamed_commits);
2895 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2896 if (err)
2897 return err;
2899 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2900 s->first_displayed_line = 1;
2901 s->last_displayed_line = view->nlines;
2902 s->selected_line = 1;
2903 s->blame_complete = 0;
2904 s->path = path;
2905 if (s->path == NULL)
2906 return got_error_prefix_errno("open_blame_view");
2907 s->repo = repo;
2908 s->refs = refs;
2909 s->commit_id = commit_id;
2910 memset(&s->blame, 0, sizeof(s->blame));
2912 view->show = show_blame_view;
2913 view->input = input_blame_view;
2914 view->close = close_blame_view;
2916 return run_blame(&s->blame, view, &s->blame_complete,
2917 &s->first_displayed_line, &s->last_displayed_line,
2918 &s->selected_line, &s->done, &s->eof, s->path,
2919 s->blamed_commit->id, s->repo);
2922 static const struct got_error *
2923 close_blame_view(struct tog_view *view)
2925 const struct got_error *err = NULL;
2926 struct tog_blame_view_state *s = &view->state.blame;
2928 if (s->blame.thread)
2929 err = stop_blame(&s->blame);
2931 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
2932 struct got_object_qid *blamed_commit;
2933 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
2934 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
2935 got_object_qid_free(blamed_commit);
2938 free(s->path);
2940 return err;
2943 static const struct got_error *
2944 show_blame_view(struct tog_view *view)
2946 const struct got_error *err = NULL;
2947 struct tog_blame_view_state *s = &view->state.blame;
2948 int errcode;
2950 if (s->blame.thread == NULL) {
2951 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
2952 &s->blame.thread_args);
2953 if (errcode)
2954 return got_error_set_errno(errcode, "pthread_create");
2957 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
2958 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
2959 s->selected_line, &s->first_displayed_line,
2960 &s->last_displayed_line, &s->eof, view->nlines);
2962 view_vborder(view);
2963 return err;
2966 static const struct got_error *
2967 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
2968 struct tog_view **focus_view, struct tog_view *view, int ch)
2970 const struct got_error *err = NULL, *thread_err = NULL;
2971 struct tog_view *diff_view;
2972 struct tog_blame_view_state *s = &view->state.blame;
2973 int begin_x = 0;
2975 switch (ch) {
2976 case 'q':
2977 s->done = 1;
2978 break;
2979 case 'k':
2980 case KEY_UP:
2981 if (s->selected_line > 1)
2982 s->selected_line--;
2983 else if (s->selected_line == 1 &&
2984 s->first_displayed_line > 1)
2985 s->first_displayed_line--;
2986 else
2987 view_flash(view);
2988 break;
2989 case KEY_PPAGE:
2990 if (s->first_displayed_line == 1) {
2991 if (s->selected_line == 1) {
2992 view_flash(view);
2993 break;
2995 s->selected_line = 1;
2996 break;
2998 if (s->first_displayed_line > view->nlines - 2)
2999 s->first_displayed_line -=
3000 (view->nlines - 2);
3001 else
3002 s->first_displayed_line = 1;
3003 break;
3004 case 'j':
3005 case KEY_DOWN:
3006 if (s->selected_line < view->nlines - 2 &&
3007 s->first_displayed_line +
3008 s->selected_line <= s->blame.nlines)
3009 s->selected_line++;
3010 else if (s->last_displayed_line <
3011 s->blame.nlines)
3012 s->first_displayed_line++;
3013 else
3014 view_flash(view);
3015 break;
3016 case 'b':
3017 case 'p': {
3018 struct got_object_id *id = NULL;
3019 id = get_selected_commit_id(s->blame.lines,
3020 s->first_displayed_line, s->selected_line);
3021 if (id == NULL)
3022 break;
3023 if (ch == 'p') {
3024 struct got_commit_object *commit;
3025 struct got_object_qid *pid;
3026 struct got_object_id *blob_id = NULL;
3027 int obj_type;
3028 err = got_object_open_as_commit(&commit,
3029 s->repo, id);
3030 if (err)
3031 break;
3032 pid = SIMPLEQ_FIRST(
3033 got_object_commit_get_parent_ids(commit));
3034 if (pid == NULL) {
3035 got_object_commit_close(commit);
3036 break;
3038 /* Check if path history ends here. */
3039 err = got_object_id_by_path(&blob_id, s->repo,
3040 pid->id, s->path);
3041 if (err) {
3042 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3043 err = NULL;
3044 got_object_commit_close(commit);
3045 break;
3047 err = got_object_get_type(&obj_type, s->repo,
3048 blob_id);
3049 free(blob_id);
3050 /* Can't blame non-blob type objects. */
3051 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3052 got_object_commit_close(commit);
3053 break;
3055 err = got_object_qid_alloc(&s->blamed_commit,
3056 pid->id);
3057 got_object_commit_close(commit);
3058 } else {
3059 if (got_object_id_cmp(id,
3060 s->blamed_commit->id) == 0)
3061 break;
3062 err = got_object_qid_alloc(&s->blamed_commit,
3063 id);
3065 if (err)
3066 break;
3067 s->done = 1;
3068 thread_err = stop_blame(&s->blame);
3069 s->done = 0;
3070 if (thread_err)
3071 break;
3072 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3073 s->blamed_commit, entry);
3074 err = run_blame(&s->blame, view, &s->blame_complete,
3075 &s->first_displayed_line, &s->last_displayed_line,
3076 &s->selected_line, &s->done, &s->eof,
3077 s->path, s->blamed_commit->id, s->repo);
3078 if (err)
3079 break;
3080 break;
3082 case 'B': {
3083 struct got_object_qid *first;
3084 first = SIMPLEQ_FIRST(&s->blamed_commits);
3085 if (!got_object_id_cmp(first->id, s->commit_id))
3086 break;
3087 s->done = 1;
3088 thread_err = stop_blame(&s->blame);
3089 s->done = 0;
3090 if (thread_err)
3091 break;
3092 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3093 got_object_qid_free(s->blamed_commit);
3094 s->blamed_commit =
3095 SIMPLEQ_FIRST(&s->blamed_commits);
3096 err = run_blame(&s->blame, view, &s->blame_complete,
3097 &s->first_displayed_line, &s->last_displayed_line,
3098 &s->selected_line, &s->done, &s->eof, s->path,
3099 s->blamed_commit->id, s->repo);
3100 if (err)
3101 break;
3102 break;
3104 case KEY_ENTER:
3105 case '\r': {
3106 struct got_object_id *id = NULL;
3107 struct got_object_qid *pid;
3108 struct got_commit_object *commit = NULL;
3109 id = get_selected_commit_id(s->blame.lines,
3110 s->first_displayed_line, s->selected_line);
3111 if (id == NULL)
3112 break;
3113 err = got_object_open_as_commit(&commit, s->repo, id);
3114 if (err)
3115 break;
3116 pid = SIMPLEQ_FIRST(
3117 got_object_commit_get_parent_ids(commit));
3118 if (view_is_parent_view(view))
3119 begin_x = view_split_begin_x(view->begin_x);
3120 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3121 if (diff_view == NULL) {
3122 got_object_commit_close(commit);
3123 err = got_error_prefix_errno("view_open");
3124 break;
3126 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3127 id, NULL, s->refs, s->repo);
3128 got_object_commit_close(commit);
3129 if (err) {
3130 view_close(diff_view);
3131 break;
3133 if (view_is_parent_view(view)) {
3134 err = view_close_child(view);
3135 if (err)
3136 break;
3137 err = view_set_child(view, diff_view);
3138 if (err) {
3139 view_close(diff_view);
3140 break;
3142 *focus_view = diff_view;
3143 view->child_focussed = 1;
3144 } else
3145 *new_view = diff_view;
3146 if (err)
3147 break;
3148 break;
3150 case KEY_NPAGE:
3151 case ' ':
3152 if (s->last_displayed_line >= s->blame.nlines &&
3153 s->selected_line >= MIN(s->blame.nlines,
3154 view->nlines - 2)) {
3155 view_flash(view);
3156 break;
3158 if (s->last_displayed_line >= s->blame.nlines &&
3159 s->selected_line < view->nlines - 2) {
3160 s->selected_line = MIN(s->blame.nlines,
3161 view->nlines - 2);
3162 break;
3164 if (s->last_displayed_line + view->nlines - 2
3165 <= s->blame.nlines)
3166 s->first_displayed_line +=
3167 view->nlines - 2;
3168 else
3169 s->first_displayed_line =
3170 s->blame.nlines -
3171 (view->nlines - 3);
3172 break;
3173 case KEY_RESIZE:
3174 if (s->selected_line > view->nlines - 2) {
3175 s->selected_line = MIN(s->blame.nlines,
3176 view->nlines - 2);
3178 break;
3179 default:
3180 break;
3182 return thread_err ? thread_err : err;
3185 static const struct got_error *
3186 cmd_blame(int argc, char *argv[])
3188 const struct got_error *error;
3189 struct got_repository *repo = NULL;
3190 struct got_reflist_head refs;
3191 struct got_worktree *worktree = NULL;
3192 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3193 struct got_object_id *commit_id = NULL;
3194 char *commit_id_str = NULL;
3195 int ch;
3196 struct tog_view *view;
3198 SIMPLEQ_INIT(&refs);
3200 #ifndef PROFILE
3201 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3202 NULL) == -1)
3203 err(1, "pledge");
3204 #endif
3206 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3207 switch (ch) {
3208 case 'c':
3209 commit_id_str = optarg;
3210 break;
3211 case 'r':
3212 repo_path = realpath(optarg, NULL);
3213 if (repo_path == NULL)
3214 err(1, "-r option");
3215 break;
3216 default:
3217 usage_blame();
3218 /* NOTREACHED */
3222 argc -= optind;
3223 argv += optind;
3225 if (argc == 1)
3226 path = argv[0];
3227 else
3228 usage_blame();
3230 cwd = getcwd(NULL, 0);
3231 if (cwd == NULL) {
3232 error = got_error_prefix_errno("getcwd");
3233 goto done;
3235 if (repo_path == NULL) {
3236 error = got_worktree_open(&worktree, cwd);
3237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3238 goto done;
3239 else
3240 error = NULL;
3241 if (worktree) {
3242 repo_path =
3243 strdup(got_worktree_get_repo_path(worktree));
3244 if (repo_path == NULL)
3245 error = got_error_prefix_errno("strdup");
3246 if (error)
3247 goto done;
3248 } else {
3249 repo_path = strdup(cwd);
3250 if (repo_path == NULL) {
3251 error = got_error_prefix_errno("strdup");
3252 goto done;
3257 init_curses();
3259 error = got_repo_open(&repo, repo_path);
3260 if (error != NULL)
3261 goto done;
3263 error = apply_unveil(got_repo_get_path(repo), NULL);
3264 if (error)
3265 goto done;
3267 if (worktree) {
3268 const char *prefix = got_worktree_get_path_prefix(worktree);
3269 char *p, *worktree_subdir = cwd +
3270 strlen(got_worktree_get_root_path(worktree));
3271 if (asprintf(&p, "%s%s%s%s%s",
3272 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3273 worktree_subdir, worktree_subdir[0] ? "/" : "",
3274 path) == -1) {
3275 error = got_error_prefix_errno("asprintf");
3276 goto done;
3278 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3279 free(p);
3280 } else {
3281 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3283 if (error)
3284 goto done;
3286 if (commit_id_str == NULL) {
3287 struct got_reference *head_ref;
3288 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
3289 if (error != NULL)
3290 goto done;
3291 error = got_ref_resolve(&commit_id, repo, head_ref);
3292 got_ref_close(head_ref);
3293 } else {
3294 error = got_object_resolve_id_str(&commit_id, repo,
3295 commit_id_str);
3297 if (error != NULL)
3298 goto done;
3300 error = got_ref_list(&refs, repo);
3301 if (error)
3302 goto done;
3304 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3305 if (view == NULL) {
3306 error = got_error_prefix_errno("view_open");
3307 goto done;
3309 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3310 if (error)
3311 goto done;
3312 error = view_loop(view);
3313 done:
3314 free(repo_path);
3315 free(cwd);
3316 free(commit_id);
3317 if (worktree)
3318 got_worktree_close(worktree);
3319 if (repo)
3320 got_repo_close(repo);
3321 got_ref_list_free(&refs);
3322 return error;
3325 static const struct got_error *
3326 draw_tree_entries(struct tog_view *view,
3327 struct got_tree_entry **first_displayed_entry,
3328 struct got_tree_entry **last_displayed_entry,
3329 struct got_tree_entry **selected_entry, int *ndisplayed,
3330 const char *label, int show_ids, const char *parent_path,
3331 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3333 const struct got_error *err = NULL;
3334 struct got_tree_entry *te;
3335 wchar_t *wline;
3336 int width, n;
3338 *ndisplayed = 0;
3340 werase(view->window);
3342 if (limit == 0)
3343 return NULL;
3345 err = format_line(&wline, &width, label, view->ncols);
3346 if (err)
3347 return err;
3348 if (view_needs_focus_indication(view))
3349 wstandout(view->window);
3350 waddwstr(view->window, wline);
3351 if (view_needs_focus_indication(view))
3352 wstandend(view->window);
3353 free(wline);
3354 wline = NULL;
3355 if (width < view->ncols)
3356 waddch(view->window, '\n');
3357 if (--limit <= 0)
3358 return NULL;
3359 err = format_line(&wline, &width, parent_path, view->ncols);
3360 if (err)
3361 return err;
3362 waddwstr(view->window, wline);
3363 free(wline);
3364 wline = NULL;
3365 if (width < view->ncols)
3366 waddch(view->window, '\n');
3367 if (--limit <= 0)
3368 return NULL;
3369 waddch(view->window, '\n');
3370 if (--limit <= 0)
3371 return NULL;
3373 te = SIMPLEQ_FIRST(&entries->head);
3374 if (*first_displayed_entry == NULL) {
3375 if (selected == 0) {
3376 if (view->focussed)
3377 wstandout(view->window);
3378 *selected_entry = NULL;
3380 waddstr(view->window, " ..\n"); /* parent directory */
3381 if (selected == 0 && view->focussed)
3382 wstandend(view->window);
3383 (*ndisplayed)++;
3384 if (--limit <= 0)
3385 return NULL;
3386 n = 1;
3387 } else {
3388 n = 0;
3389 while (te != *first_displayed_entry)
3390 te = SIMPLEQ_NEXT(te, entry);
3393 while (te) {
3394 char *line = NULL, *id_str = NULL;
3396 if (show_ids) {
3397 err = got_object_id_str(&id_str, te->id);
3398 if (err)
3399 return got_error_prefix_errno(
3400 "got_object_id_str");
3402 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3403 te->name, S_ISDIR(te->mode) ? "/" :
3404 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3405 free(id_str);
3406 return got_error_prefix_errno("asprintf");
3408 free(id_str);
3409 err = format_line(&wline, &width, line, view->ncols);
3410 if (err) {
3411 free(line);
3412 break;
3414 if (n == selected) {
3415 if (view->focussed)
3416 wstandout(view->window);
3417 *selected_entry = te;
3419 waddwstr(view->window, wline);
3420 if (width < view->ncols)
3421 waddch(view->window, '\n');
3422 if (n == selected && view->focussed)
3423 wstandend(view->window);
3424 free(line);
3425 free(wline);
3426 wline = NULL;
3427 n++;
3428 (*ndisplayed)++;
3429 *last_displayed_entry = te;
3430 if (--limit <= 0)
3431 break;
3432 te = SIMPLEQ_NEXT(te, entry);
3435 return err;
3438 static void
3439 tree_scroll_up(struct tog_view *view,
3440 struct got_tree_entry **first_displayed_entry, int maxscroll,
3441 const struct got_tree_entries *entries, int isroot)
3443 struct got_tree_entry *te, *prev;
3444 int i;
3446 if (*first_displayed_entry == NULL) {
3447 view_flash(view);
3448 return;
3451 te = SIMPLEQ_FIRST(&entries->head);
3452 if (*first_displayed_entry == te) {
3453 view_flash(view);
3454 if (!isroot)
3455 *first_displayed_entry = NULL;
3456 return;
3459 /* XXX this is stupid... switch to TAILQ? */
3460 for (i = 0; i < maxscroll; i++) {
3461 while (te != *first_displayed_entry) {
3462 prev = te;
3463 te = SIMPLEQ_NEXT(te, entry);
3465 *first_displayed_entry = prev;
3466 te = SIMPLEQ_FIRST(&entries->head);
3468 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3469 *first_displayed_entry = NULL;
3472 static int
3473 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3474 struct got_tree_entry *last_displayed_entry,
3475 const struct got_tree_entries *entries)
3477 struct got_tree_entry *next, *last;
3478 int n = 0;
3480 if (*first_displayed_entry)
3481 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3482 else
3483 next = SIMPLEQ_FIRST(&entries->head);
3484 last = last_displayed_entry;
3485 while (next && last && n++ < maxscroll) {
3486 last = SIMPLEQ_NEXT(last, entry);
3487 if (last) {
3488 *first_displayed_entry = next;
3489 next = SIMPLEQ_NEXT(next, entry);
3492 return n;
3495 static const struct got_error *
3496 tree_entry_path(char **path, struct tog_parent_trees *parents,
3497 struct got_tree_entry *te)
3499 const struct got_error *err = NULL;
3500 struct tog_parent_tree *pt;
3501 size_t len = 2; /* for leading slash and NUL */
3503 TAILQ_FOREACH(pt, parents, entry)
3504 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3505 if (te)
3506 len += strlen(te->name);
3508 *path = calloc(1, len);
3509 if (path == NULL)
3510 return got_error_prefix_errno("calloc");
3512 (*path)[0] = '/';
3513 pt = TAILQ_LAST(parents, tog_parent_trees);
3514 while (pt) {
3515 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3516 err = got_error(GOT_ERR_NO_SPACE);
3517 goto done;
3519 if (strlcat(*path, "/", len) >= len) {
3520 err = got_error(GOT_ERR_NO_SPACE);
3521 goto done;
3523 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3525 if (te) {
3526 if (strlcat(*path, te->name, len) >= len) {
3527 err = got_error(GOT_ERR_NO_SPACE);
3528 goto done;
3531 done:
3532 if (err) {
3533 free(*path);
3534 *path = NULL;
3536 return err;
3539 static const struct got_error *
3540 blame_tree_entry(struct tog_view **new_view, int begin_x,
3541 struct got_tree_entry *te, struct tog_parent_trees *parents,
3542 struct got_object_id *commit_id, struct got_reflist_head *refs,
3543 struct got_repository *repo)
3545 const struct got_error *err = NULL;
3546 char *path;
3547 struct tog_view *blame_view;
3549 err = tree_entry_path(&path, parents, te);
3550 if (err)
3551 return err;
3553 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3554 if (blame_view == NULL)
3555 return got_error_prefix_errno("view_open");
3557 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3558 if (err) {
3559 view_close(blame_view);
3560 free(path);
3561 } else
3562 *new_view = blame_view;
3563 return err;
3566 static const struct got_error *
3567 log_tree_entry(struct tog_view **new_view, int begin_x,
3568 struct got_tree_entry *te, struct tog_parent_trees *parents,
3569 struct got_object_id *commit_id, struct got_reflist_head *refs,
3570 struct got_repository *repo)
3572 struct tog_view *log_view;
3573 const struct got_error *err = NULL;
3574 char *path;
3576 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3577 if (log_view == NULL)
3578 return got_error_prefix_errno("view_open");
3580 err = tree_entry_path(&path, parents, te);
3581 if (err)
3582 return err;
3584 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3585 if (err)
3586 view_close(log_view);
3587 else
3588 *new_view = log_view;
3589 free(path);
3590 return err;
3593 static const struct got_error *
3594 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3595 struct got_object_id *commit_id, struct got_reflist_head *refs,
3596 struct got_repository *repo)
3598 const struct got_error *err = NULL;
3599 char *commit_id_str = NULL;
3600 struct tog_tree_view_state *s = &view->state.tree;
3602 TAILQ_INIT(&s->parents);
3604 err = got_object_id_str(&commit_id_str, commit_id);
3605 if (err != NULL)
3606 goto done;
3608 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3609 err = got_error_prefix_errno("asprintf");
3610 goto done;
3613 s->root = s->tree = root;
3614 s->entries = got_object_tree_get_entries(root);
3615 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3616 s->commit_id = got_object_id_dup(commit_id);
3617 if (s->commit_id == NULL) {
3618 err = got_error_prefix_errno("got_object_id_dup");
3619 goto done;
3621 s->refs = refs;
3622 s->repo = repo;
3624 view->show = show_tree_view;
3625 view->input = input_tree_view;
3626 view->close = close_tree_view;
3627 done:
3628 free(commit_id_str);
3629 if (err) {
3630 free(s->tree_label);
3631 s->tree_label = NULL;
3633 return err;
3636 static const struct got_error *
3637 close_tree_view(struct tog_view *view)
3639 struct tog_tree_view_state *s = &view->state.tree;
3641 free(s->tree_label);
3642 s->tree_label = NULL;
3643 free(s->commit_id);
3644 s->commit_id = NULL;
3645 while (!TAILQ_EMPTY(&s->parents)) {
3646 struct tog_parent_tree *parent;
3647 parent = TAILQ_FIRST(&s->parents);
3648 TAILQ_REMOVE(&s->parents, parent, entry);
3649 free(parent);
3652 if (s->tree != s->root)
3653 got_object_tree_close(s->tree);
3654 got_object_tree_close(s->root);
3656 return NULL;
3659 static const struct got_error *
3660 show_tree_view(struct tog_view *view)
3662 const struct got_error *err = NULL;
3663 struct tog_tree_view_state *s = &view->state.tree;
3664 char *parent_path;
3666 err = tree_entry_path(&parent_path, &s->parents, NULL);
3667 if (err)
3668 return err;
3670 err = draw_tree_entries(view, &s->first_displayed_entry,
3671 &s->last_displayed_entry, &s->selected_entry,
3672 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3673 s->entries, s->selected, view->nlines, s->tree == s->root);
3674 free(parent_path);
3676 view_vborder(view);
3677 return err;
3680 static const struct got_error *
3681 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3682 struct tog_view **focus_view, struct tog_view *view, int ch)
3684 const struct got_error *err = NULL;
3685 struct tog_tree_view_state *s = &view->state.tree;
3686 struct tog_view *log_view;
3687 int begin_x = 0, nscrolled;
3689 switch (ch) {
3690 case 'i':
3691 s->show_ids = !s->show_ids;
3692 break;
3693 case 'l':
3694 if (!s->selected_entry)
3695 break;
3696 if (view_is_parent_view(view))
3697 begin_x = view_split_begin_x(view->begin_x);
3698 err = log_tree_entry(&log_view, begin_x,
3699 s->selected_entry, &s->parents,
3700 s->commit_id, s->refs, s->repo);
3701 if (view_is_parent_view(view)) {
3702 err = view_close_child(view);
3703 if (err)
3704 return err;
3705 err = view_set_child(view, log_view);
3706 if (err) {
3707 view_close(log_view);
3708 break;
3710 *focus_view = log_view;
3711 view->child_focussed = 1;
3712 } else
3713 *new_view = log_view;
3714 break;
3715 case 'k':
3716 case KEY_UP:
3717 if (s->selected > 0) {
3718 s->selected--;
3719 if (s->selected == 0)
3720 break;
3722 if (s->selected > 0)
3723 break;
3724 tree_scroll_up(view, &s->first_displayed_entry, 1,
3725 s->entries, s->tree == s->root);
3726 break;
3727 case KEY_PPAGE:
3728 tree_scroll_up(view, &s->first_displayed_entry,
3729 MAX(0, view->nlines - 4 - s->selected), s->entries,
3730 s->tree == s->root);
3731 s->selected = 0;
3732 if (SIMPLEQ_FIRST(&s->entries->head) ==
3733 s->first_displayed_entry && s->tree != s->root)
3734 s->first_displayed_entry = NULL;
3735 break;
3736 case 'j':
3737 case KEY_DOWN:
3738 if (s->selected < s->ndisplayed - 1) {
3739 s->selected++;
3740 break;
3742 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3743 == NULL) {
3744 /* can't scroll any further */
3745 view_flash(view);
3746 break;
3748 tree_scroll_down(&s->first_displayed_entry, 1,
3749 s->last_displayed_entry, s->entries);
3750 break;
3751 case KEY_NPAGE:
3752 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3753 == NULL) {
3754 /* can't scroll any further; move cursor down */
3755 if (s->selected < s->ndisplayed - 1)
3756 s->selected = s->ndisplayed - 1;
3757 else
3758 view_flash(view);
3759 break;
3761 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3762 view->nlines, s->last_displayed_entry, s->entries);
3763 if (nscrolled < view->nlines) {
3764 int ndisplayed = 0;
3765 struct got_tree_entry *te;
3766 te = s->first_displayed_entry;
3767 do {
3768 ndisplayed++;
3769 te = SIMPLEQ_NEXT(te, entry);
3770 } while (te);
3771 s->selected = ndisplayed - 1;
3773 break;
3774 case KEY_ENTER:
3775 case '\r':
3776 if (s->selected_entry == NULL) {
3777 struct tog_parent_tree *parent;
3778 case KEY_BACKSPACE:
3779 /* user selected '..' */
3780 if (s->tree == s->root)
3781 break;
3782 parent = TAILQ_FIRST(&s->parents);
3783 TAILQ_REMOVE(&s->parents, parent,
3784 entry);
3785 got_object_tree_close(s->tree);
3786 s->tree = parent->tree;
3787 s->entries =
3788 got_object_tree_get_entries(s->tree);
3789 s->first_displayed_entry =
3790 parent->first_displayed_entry;
3791 s->selected_entry =
3792 parent->selected_entry;
3793 s->selected = parent->selected;
3794 free(parent);
3795 } else if (S_ISDIR(s->selected_entry->mode)) {
3796 struct tog_parent_tree *parent;
3797 struct got_tree_object *child;
3798 err = got_object_open_as_tree(&child,
3799 s->repo, s->selected_entry->id);
3800 if (err)
3801 break;
3802 parent = calloc(1, sizeof(*parent));
3803 if (parent == NULL) {
3804 err = got_error_prefix_errno("calloc");
3805 break;
3807 parent->tree = s->tree;
3808 parent->first_displayed_entry =
3809 s->first_displayed_entry;
3810 parent->selected_entry = s->selected_entry;
3811 parent->selected = s->selected;
3812 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
3813 s->tree = child;
3814 s->entries =
3815 got_object_tree_get_entries(s->tree);
3816 s->selected = 0;
3817 s->first_displayed_entry = NULL;
3818 } else if (S_ISREG(s->selected_entry->mode)) {
3819 struct tog_view *blame_view;
3820 int begin_x = view_is_parent_view(view) ?
3821 view_split_begin_x(view->begin_x) : 0;
3823 err = blame_tree_entry(&blame_view, begin_x,
3824 s->selected_entry, &s->parents,
3825 s->commit_id, s->refs, s->repo);
3826 if (err)
3827 break;
3828 if (view_is_parent_view(view)) {
3829 err = view_close_child(view);
3830 if (err)
3831 return err;
3832 err = view_set_child(view, blame_view);
3833 if (err) {
3834 view_close(blame_view);
3835 break;
3837 *focus_view = blame_view;
3838 view->child_focussed = 1;
3839 } else
3840 *new_view = blame_view;
3842 break;
3843 case KEY_RESIZE:
3844 if (s->selected > view->nlines)
3845 s->selected = s->ndisplayed - 1;
3846 break;
3847 default:
3848 break;
3851 return err;
3854 __dead static void
3855 usage_tree(void)
3857 endwin();
3858 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3859 getprogname());
3860 exit(1);
3863 static const struct got_error *
3864 cmd_tree(int argc, char *argv[])
3866 const struct got_error *error;
3867 struct got_repository *repo = NULL;
3868 struct got_reflist_head refs;
3869 char *repo_path = NULL;
3870 struct got_object_id *commit_id = NULL;
3871 char *commit_id_arg = NULL;
3872 struct got_commit_object *commit = NULL;
3873 struct got_tree_object *tree = NULL;
3874 int ch;
3875 struct tog_view *view;
3877 SIMPLEQ_INIT(&refs);
3879 #ifndef PROFILE
3880 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3881 NULL) == -1)
3882 err(1, "pledge");
3883 #endif
3885 while ((ch = getopt(argc, argv, "c:")) != -1) {
3886 switch (ch) {
3887 case 'c':
3888 commit_id_arg = optarg;
3889 break;
3890 default:
3891 usage_tree();
3892 /* NOTREACHED */
3896 argc -= optind;
3897 argv += optind;
3899 if (argc == 0) {
3900 struct got_worktree *worktree;
3901 char *cwd = getcwd(NULL, 0);
3902 if (cwd == NULL)
3903 return got_error_prefix_errno("getcwd");
3904 error = got_worktree_open(&worktree, cwd);
3905 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3906 goto done;
3907 if (worktree) {
3908 free(cwd);
3909 repo_path =
3910 strdup(got_worktree_get_repo_path(worktree));
3911 got_worktree_close(worktree);
3912 } else
3913 repo_path = cwd;
3914 if (repo_path == NULL) {
3915 error = got_error_prefix_errno("strdup");
3916 goto done;
3918 } else if (argc == 1) {
3919 repo_path = realpath(argv[0], NULL);
3920 if (repo_path == NULL)
3921 return got_error_prefix_errno2("realpath", argv[0]);
3922 } else
3923 usage_log();
3925 init_curses();
3927 error = got_repo_open(&repo, repo_path);
3928 if (error != NULL)
3929 goto done;
3931 error = apply_unveil(got_repo_get_path(repo), NULL);
3932 if (error)
3933 goto done;
3935 if (commit_id_arg == NULL)
3936 error = get_head_commit_id(&commit_id, repo);
3937 else
3938 error = got_object_resolve_id_str(&commit_id, repo,
3939 commit_id_arg);
3940 if (error != NULL)
3941 goto done;
3943 error = got_object_open_as_commit(&commit, repo, commit_id);
3944 if (error != NULL)
3945 goto done;
3947 error = got_object_open_as_tree(&tree, repo,
3948 got_object_commit_get_tree_id(commit));
3949 if (error != NULL)
3950 goto done;
3952 error = got_ref_list(&refs, repo);
3953 if (error)
3954 goto done;
3956 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
3957 if (view == NULL) {
3958 error = got_error_prefix_errno("view_open");
3959 goto done;
3961 error = open_tree_view(view, tree, commit_id, &refs, repo);
3962 if (error)
3963 goto done;
3964 error = view_loop(view);
3965 done:
3966 free(repo_path);
3967 free(commit_id);
3968 if (commit)
3969 got_object_commit_close(commit);
3970 if (tree)
3971 got_object_tree_close(tree);
3972 if (repo)
3973 got_repo_close(repo);
3974 got_ref_list_free(&refs);
3975 return error;
3978 __dead static void
3979 usage(void)
3981 int i;
3983 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
3984 "Available commands:\n", getprogname());
3985 for (i = 0; i < nitems(tog_commands); i++) {
3986 struct tog_cmd *cmd = &tog_commands[i];
3987 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
3989 exit(1);
3992 static char **
3993 make_argv(const char *arg0, const char *arg1)
3995 char **argv;
3996 int argc = (arg1 == NULL ? 1 : 2);
3998 argv = calloc(argc, sizeof(char *));
3999 if (argv == NULL)
4000 err(1, "calloc");
4001 argv[0] = strdup(arg0);
4002 if (argv[0] == NULL)
4003 err(1, "calloc");
4004 if (arg1) {
4005 argv[1] = strdup(arg1);
4006 if (argv[1] == NULL)
4007 err(1, "calloc");
4010 return argv;
4013 int
4014 main(int argc, char *argv[])
4016 const struct got_error *error = NULL;
4017 struct tog_cmd *cmd = NULL;
4018 int ch, hflag = 0;
4019 char **cmd_argv = NULL;
4021 setlocale(LC_CTYPE, "");
4023 while ((ch = getopt(argc, argv, "h")) != -1) {
4024 switch (ch) {
4025 case 'h':
4026 hflag = 1;
4027 break;
4028 default:
4029 usage();
4030 /* NOTREACHED */
4034 argc -= optind;
4035 argv += optind;
4036 optind = 0;
4037 optreset = 1;
4039 if (argc == 0) {
4040 if (hflag)
4041 usage();
4042 /* Build an argument vector which runs a default command. */
4043 cmd = &tog_commands[0];
4044 cmd_argv = make_argv(cmd->name, NULL);
4045 argc = 1;
4046 } else {
4047 int i;
4049 /* Did the user specific a command? */
4050 for (i = 0; i < nitems(tog_commands); i++) {
4051 if (strncmp(tog_commands[i].name, argv[0],
4052 strlen(argv[0])) == 0) {
4053 cmd = &tog_commands[i];
4054 if (hflag)
4055 tog_commands[i].cmd_usage();
4056 break;
4059 if (cmd == NULL) {
4060 /* Did the user specify a repository? */
4061 char *repo_path = realpath(argv[0], NULL);
4062 if (repo_path) {
4063 struct got_repository *repo;
4064 error = got_repo_open(&repo, repo_path);
4065 if (error == NULL)
4066 got_repo_close(repo);
4067 } else
4068 error = got_error_prefix_errno2("realpath",
4069 argv[0]);
4070 if (error) {
4071 if (hflag) {
4072 fprintf(stderr, "%s: '%s' is not a "
4073 "known command\n", getprogname(),
4074 argv[0]);
4075 usage();
4077 fprintf(stderr, "%s: '%s' is neither a known "
4078 "command nor a path to a repository\n",
4079 getprogname(), argv[0]);
4080 free(repo_path);
4081 return 1;
4083 cmd = &tog_commands[0];
4084 cmd_argv = make_argv(cmd->name, repo_path);
4085 argc = 2;
4086 free(repo_path);
4090 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4091 if (error)
4092 goto done;
4093 done:
4094 endwin();
4095 free(cmd_argv);
4096 if (error)
4097 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4098 return 0;