Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
40 #include "got_error.h"
41 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository.h"
44 #include "got_diff.h"
45 #include "got_opentemp.h"
46 #include "got_commit_graph.h"
47 #include "got_utf8.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_path.h"
51 #include "got_worktree.h"
53 #ifndef MIN
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 #endif
57 #ifndef MAX
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 #endif
61 #define CTRL(x) ((x) & 0x1f)
63 #ifndef nitems
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
65 #endif
67 struct tog_cmd {
68 const char *name;
69 const struct got_error *(*cmd_main)(int, char *[]);
70 void (*cmd_usage)(void);
71 const char *descr;
72 };
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log,
87 "show repository history" },
88 { "diff", cmd_diff, usage_diff,
89 "compare files and directories" },
90 { "blame", cmd_blame, usage_blame,
91 "show line-by-line file history" },
92 { "tree", cmd_tree, usage_tree,
93 "browse trees in repository" },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
109 int idx;
110 };
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
113 int ncommits;
114 struct commit_queue_head head;
115 };
117 struct tog_diff_view_state {
118 struct got_object_id *id1, *id2;
119 FILE *f;
120 int first_displayed_line;
121 int last_displayed_line;
122 int eof;
123 int diff_context;
124 struct got_repository *repo;
125 struct got_reflist_head *refs;
127 /* passed from log view; may be NULL */
128 struct tog_view *log_view;
129 };
131 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
133 struct tog_log_thread_args {
134 pthread_cond_t need_commits;
135 int commits_needed;
136 struct got_commit_graph *graph;
137 struct commit_queue *commits;
138 const char *in_repo_path;
139 struct got_object_id *start_id;
140 struct got_repository *repo;
141 int log_complete;
142 sig_atomic_t *quit;
143 struct tog_view *view;
144 struct commit_queue_entry **first_displayed_entry;
145 struct commit_queue_entry **selected_entry;
146 };
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
153 int selected;
154 char *in_repo_path;
155 struct got_repository *repo;
156 struct got_reflist_head *refs;
157 struct got_object_id *start_id;
158 sig_atomic_t quit;
159 pthread_t thread;
160 struct tog_log_thread_args thread_args;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 pthread_t thread;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
187 const char *path;
188 };
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
193 int selected_line;
194 int blame_complete;
195 int eof;
196 int done;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
199 char *path;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
204 };
206 struct tog_parent_tree {
207 TAILQ_ENTRY(tog_parent_tree) entry;
208 struct got_tree_object *tree;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *selected_entry;
211 int selected;
212 };
214 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 struct tog_tree_view_state {
217 char *tree_label;
218 struct got_tree_object *root;
219 struct got_tree_object *tree;
220 const struct got_tree_entries *entries;
221 struct got_tree_entry *first_displayed_entry;
222 struct got_tree_entry *last_displayed_entry;
223 struct got_tree_entry *selected_entry;
224 int ndisplayed, selected, show_ids;
225 struct tog_parent_trees parents;
226 struct got_object_id *commit_id;
227 struct got_repository *repo;
228 struct got_reflist_head *refs;
229 };
231 /*
232 * We implement two types of views: parent views and child views.
234 * The 'Tab' key switches between a parent view and its child view.
235 * Child views are shown side-by-side to their parent view, provided
236 * there is enough screen estate.
238 * When a new view is opened from within a parent view, this new view
239 * becomes a child view of the parent view, replacing any existing child.
241 * When a new view is opened from within a child view, this new view
242 * becomes a parent view which will obscure the views below until the
243 * user quits the new parent view by typing 'q'.
245 * This list of views contains parent views only.
246 * Child views are only pointed to by their parent view.
247 */
248 TAILQ_HEAD(tog_view_list_head, tog_view);
250 struct tog_view {
251 TAILQ_ENTRY(tog_view) entry;
252 WINDOW *window;
253 PANEL *panel;
254 int nlines, ncols, begin_y, begin_x;
255 int lines, cols; /* copies of LINES and COLS */
256 int focussed;
257 struct tog_view *parent;
258 struct tog_view *child;
259 int child_focussed;
261 /* type-specific state */
262 enum tog_view_type type;
263 union {
264 struct tog_diff_view_state diff;
265 struct tog_log_view_state log;
266 struct tog_blame_view_state blame;
267 struct tog_tree_view_state tree;
268 } state;
270 const struct got_error *(*show)(struct tog_view *);
271 const struct got_error *(*input)(struct tog_view **,
272 struct tog_view **, struct tog_view**, struct tog_view *, int);
273 const struct got_error *(*close)(struct tog_view *);
274 };
276 static const struct got_error *open_diff_view(struct tog_view *,
277 struct got_object_id *, struct got_object_id *, struct tog_view *,
278 struct got_reflist_head *, struct got_repository *);
279 static const struct got_error *show_diff_view(struct tog_view *);
280 static const struct got_error *input_diff_view(struct tog_view **,
281 struct tog_view **, struct tog_view **, struct tog_view *, int);
282 static const struct got_error* close_diff_view(struct tog_view *);
284 static const struct got_error *open_log_view(struct tog_view *,
285 struct got_object_id *, struct got_reflist_head *,
286 struct got_repository *, const char *, int);
287 static const struct got_error * show_log_view(struct tog_view *);
288 static const struct got_error *input_log_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error *close_log_view(struct tog_view *);
292 static const struct got_error *open_blame_view(struct tog_view *, char *,
293 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
294 static const struct got_error *show_blame_view(struct tog_view *);
295 static const struct got_error *input_blame_view(struct tog_view **,
296 struct tog_view **, struct tog_view **, struct tog_view *, int);
297 static const struct got_error *close_blame_view(struct tog_view *);
299 static const struct got_error *open_tree_view(struct tog_view *,
300 struct got_tree_object *, struct got_object_id *,
301 struct got_reflist_head *, struct got_repository *);
302 static const struct got_error *show_tree_view(struct tog_view *);
303 static const struct got_error *input_tree_view(struct tog_view **,
304 struct tog_view **, struct tog_view **, struct tog_view *, int);
305 static const struct got_error *close_tree_view(struct tog_view *);
307 static volatile sig_atomic_t tog_sigwinch_received;
309 static void
310 tog_sigwinch(int signo)
312 tog_sigwinch_received = 1;
315 static const struct got_error *
316 view_close(struct tog_view *view)
318 const struct got_error *err = NULL;
320 if (view->child) {
321 view_close(view->child);
322 view->child = NULL;
324 if (view->close)
325 err = view->close(view);
326 if (view->panel)
327 del_panel(view->panel);
328 if (view->window)
329 delwin(view->window);
330 free(view);
331 return err;
334 static struct tog_view *
335 view_open(int nlines, int ncols, int begin_y, int begin_x,
336 enum tog_view_type type)
338 struct tog_view *view = calloc(1, sizeof(*view));
340 if (view == NULL)
341 return NULL;
343 view->type = type;
344 view->lines = LINES;
345 view->cols = COLS;
346 view->nlines = nlines ? nlines : LINES - begin_y;
347 view->ncols = ncols ? ncols : COLS - begin_x;
348 view->begin_y = begin_y;
349 view->begin_x = begin_x;
350 view->window = newwin(nlines, ncols, begin_y, begin_x);
351 if (view->window == NULL) {
352 view_close(view);
353 return NULL;
355 view->panel = new_panel(view->window);
356 if (view->panel == NULL ||
357 set_panel_userptr(view->panel, view) != OK) {
358 view_close(view);
359 return NULL;
362 keypad(view->window, TRUE);
363 return view;
366 static int
367 view_split_begin_x(int begin_x)
369 if (begin_x > 0 || COLS < 120)
370 return 0;
371 return (COLS - MAX(COLS / 2, 80));
374 static const struct got_error *view_resize(struct tog_view *);
376 static const struct got_error *
377 view_splitscreen(struct tog_view *view)
379 const struct got_error *err = NULL;
381 view->begin_y = 0;
382 view->begin_x = view_split_begin_x(0);
383 view->nlines = LINES;
384 view->ncols = COLS - view->begin_x;
385 view->lines = LINES;
386 view->cols = COLS;
387 err = view_resize(view);
388 if (err)
389 return err;
391 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
392 return got_error_from_errno("mvwin");
394 return NULL;
397 static const struct got_error *
398 view_fullscreen(struct tog_view *view)
400 const struct got_error *err = NULL;
402 view->begin_x = 0;
403 view->begin_y = 0;
404 view->nlines = LINES;
405 view->ncols = COLS;
406 view->lines = LINES;
407 view->cols = COLS;
408 err = view_resize(view);
409 if (err)
410 return err;
412 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
413 return got_error_from_errno("mvwin");
415 return NULL;
418 static int
419 view_is_parent_view(struct tog_view *view)
421 return view->parent == NULL;
424 static const struct got_error *
425 view_resize(struct tog_view *view)
427 int nlines, ncols;
429 if (view->lines > LINES)
430 nlines = view->nlines - (view->lines - LINES);
431 else
432 nlines = view->nlines + (LINES - view->lines);
434 if (view->cols > COLS)
435 ncols = view->ncols - (view->cols - COLS);
436 else
437 ncols = view->ncols + (COLS - view->cols);
439 if (wresize(view->window, nlines, ncols) == ERR)
440 return got_error_from_errno("wresize");
441 if (replace_panel(view->panel, view->window) == ERR)
442 return got_error_from_errno("replace_panel");
443 wclear(view->window);
445 view->nlines = nlines;
446 view->ncols = ncols;
447 view->lines = LINES;
448 view->cols = COLS;
450 if (view->child) {
451 view->child->begin_x = view_split_begin_x(view->begin_x);
452 if (view->child->begin_x == 0) {
453 view_fullscreen(view->child);
454 if (view->child->focussed)
455 show_panel(view->child->panel);
456 else
457 show_panel(view->panel);
458 } else {
459 view_splitscreen(view->child);
460 show_panel(view->child->panel);
464 return NULL;
467 static const struct got_error *
468 view_close_child(struct tog_view *view)
470 const struct got_error *err = NULL;
472 if (view->child == NULL)
473 return NULL;
475 err = view_close(view->child);
476 view->child = NULL;
477 return err;
480 static const struct got_error *
481 view_set_child(struct tog_view *view, struct tog_view *child)
483 const struct got_error *err = NULL;
485 view->child = child;
486 child->parent = view;
487 return err;
490 static int
491 view_is_splitscreen(struct tog_view *view)
493 return view->begin_x > 0;
496 static void
497 tog_resizeterm(void)
499 int cols, lines;
500 struct winsize size;
502 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
503 cols = 80; /* Default */
504 lines = 24;
505 } else {
506 cols = size.ws_col;
507 lines = size.ws_row;
509 resize_term(lines, cols);
512 static const struct got_error *
513 view_input(struct tog_view **new, struct tog_view **dead,
514 struct tog_view **focus, int *done, struct tog_view *view,
515 struct tog_view_list_head *views)
517 const struct got_error *err = NULL;
518 struct tog_view *v;
519 int ch, errcode;
521 *new = NULL;
522 *dead = NULL;
523 *focus = NULL;
525 nodelay(stdscr, FALSE);
526 /* Allow threads to make progress while we are waiting for input. */
527 errcode = pthread_mutex_unlock(&tog_mutex);
528 if (errcode)
529 return got_error_set_errno(errcode, "pthread_mutex_unlock");
530 ch = wgetch(view->window);
531 errcode = pthread_mutex_lock(&tog_mutex);
532 if (errcode)
533 return got_error_set_errno(errcode, "pthread_mutex_lock");
534 nodelay(stdscr, TRUE);
536 if (tog_sigwinch_received) {
537 tog_resizeterm();
538 tog_sigwinch_received = 0;
539 TAILQ_FOREACH(v, views, entry) {
540 err = view_resize(v);
541 if (err)
542 return err;
543 err = v->input(new, dead, focus, v, KEY_RESIZE);
544 if (err)
545 return err;
549 switch (ch) {
550 case ERR:
551 break;
552 case '\t':
553 if (view->child) {
554 *focus = view->child;
555 view->child_focussed = 1;
556 } else if (view->parent) {
557 *focus = view->parent;
558 view->parent->child_focussed = 0;
560 break;
561 case 'q':
562 err = view->input(new, dead, focus, view, ch);
563 *dead = view;
564 break;
565 case 'Q':
566 *done = 1;
567 break;
568 case 'f':
569 if (view_is_parent_view(view)) {
570 if (view->child == NULL)
571 break;
572 if (view_is_splitscreen(view->child)) {
573 *focus = view->child;
574 view->child_focussed = 1;
575 err = view_fullscreen(view->child);
576 } else
577 err = view_splitscreen(view->child);
578 if (err)
579 break;
580 err = view->child->input(new, dead, focus,
581 view->child, KEY_RESIZE);
582 } else {
583 if (view_is_splitscreen(view)) {
584 *focus = view;
585 view->parent->child_focussed = 1;
586 err = view_fullscreen(view);
587 } else {
588 err = view_splitscreen(view);
590 if (err)
591 break;
592 err = view->input(new, dead, focus, view,
593 KEY_RESIZE);
595 break;
596 case KEY_RESIZE:
597 break;
598 default:
599 err = view->input(new, dead, focus, view, ch);
600 break;
603 return err;
606 void
607 view_vborder(struct tog_view *view)
609 PANEL *panel;
610 struct tog_view *view_above;
612 if (view->parent)
613 return view_vborder(view->parent);
615 panel = panel_above(view->panel);
616 if (panel == NULL)
617 return;
619 view_above = panel_userptr(panel);
620 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
621 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
624 int
625 view_needs_focus_indication(struct tog_view *view)
627 if (view_is_parent_view(view)) {
628 if (view->child == NULL || view->child_focussed)
629 return 0;
630 if (!view_is_splitscreen(view->child))
631 return 0;
632 } else if (!view_is_splitscreen(view))
633 return 0;
635 return view->focussed;
638 static const struct got_error *
639 view_loop(struct tog_view *view)
641 const struct got_error *err = NULL;
642 struct tog_view_list_head views;
643 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
644 int fast_refresh = 10;
645 int done = 0, errcode;
647 errcode = pthread_mutex_lock(&tog_mutex);
648 if (errcode)
649 return got_error_set_errno(errcode, "pthread_mutex_lock");
651 TAILQ_INIT(&views);
652 TAILQ_INSERT_HEAD(&views, view, entry);
654 main_view = view;
655 view->focussed = 1;
656 err = view->show(view);
657 if (err)
658 return err;
659 update_panels();
660 doupdate();
661 while (!TAILQ_EMPTY(&views) && !done) {
662 /* Refresh fast during initialization, then become slower. */
663 if (fast_refresh && fast_refresh-- == 0)
664 halfdelay(10); /* switch to once per second */
666 err = view_input(&new_view, &dead_view, &focus_view, &done,
667 view, &views);
668 if (err)
669 break;
670 if (dead_view) {
671 struct tog_view *prev = NULL;
673 if (view_is_parent_view(dead_view))
674 prev = TAILQ_PREV(dead_view,
675 tog_view_list_head, entry);
676 else if (view->parent != dead_view)
677 prev = view->parent;
679 if (dead_view->parent)
680 dead_view->parent->child = NULL;
681 else
682 TAILQ_REMOVE(&views, dead_view, entry);
684 err = view_close(dead_view);
685 if (err || dead_view == main_view)
686 goto done;
688 if (view == dead_view) {
689 if (focus_view)
690 view = focus_view;
691 else if (prev)
692 view = prev;
693 else if (!TAILQ_EMPTY(&views))
694 view = TAILQ_LAST(&views,
695 tog_view_list_head);
696 else
697 view = NULL;
698 if (view) {
699 if (view->child && view->child_focussed)
700 focus_view = view->child;
701 else
702 focus_view = view;
706 if (new_view) {
707 struct tog_view *v, *t;
708 /* Only allow one parent view per type. */
709 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
710 if (v->type != new_view->type)
711 continue;
712 TAILQ_REMOVE(&views, v, entry);
713 err = view_close(v);
714 if (err)
715 goto done;
716 break;
718 TAILQ_INSERT_TAIL(&views, new_view, entry);
719 view = new_view;
720 if (focus_view == NULL)
721 focus_view = new_view;
723 if (focus_view) {
724 show_panel(focus_view->panel);
725 if (view)
726 view->focussed = 0;
727 focus_view->focussed = 1;
728 view = focus_view;
729 if (new_view)
730 show_panel(new_view->panel);
731 if (view->child && view_is_splitscreen(view->child))
732 show_panel(view->child->panel);
734 if (view) {
735 if (focus_view == NULL) {
736 view->focussed = 1;
737 show_panel(view->panel);
738 if (view->child && view_is_splitscreen(view->child))
739 show_panel(view->child->panel);
740 focus_view = view;
742 if (view->parent) {
743 err = view->parent->show(view->parent);
744 if (err)
745 goto done;
747 err = view->show(view);
748 if (err)
749 goto done;
750 if (view->child) {
751 err = view->child->show(view->child);
752 if (err)
753 goto done;
755 update_panels();
756 doupdate();
759 done:
760 while (!TAILQ_EMPTY(&views)) {
761 view = TAILQ_FIRST(&views);
762 TAILQ_REMOVE(&views, view, entry);
763 view_close(view);
766 errcode = pthread_mutex_unlock(&tog_mutex);
767 if (errcode)
768 return got_error_set_errno(errcode, "pthread_mutex_unlock");
770 return err;
773 __dead static void
774 usage_log(void)
776 endwin();
777 fprintf(stderr,
778 "usage: %s log [-c commit] [-r repository-path] [path]\n",
779 getprogname());
780 exit(1);
783 /* Create newly allocated wide-character string equivalent to a byte string. */
784 static const struct got_error *
785 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
787 char *vis = NULL;
788 const struct got_error *err = NULL;
790 *ws = NULL;
791 *wlen = mbstowcs(NULL, s, 0);
792 if (*wlen == (size_t)-1) {
793 int vislen;
794 if (errno != EILSEQ)
795 return got_error_from_errno("mbstowcs");
797 /* byte string invalid in current encoding; try to "fix" it */
798 err = got_mbsavis(&vis, &vislen, s);
799 if (err)
800 return err;
801 *wlen = mbstowcs(NULL, vis, 0);
802 if (*wlen == (size_t)-1) {
803 err = got_error_from_errno("mbstowcs"); /* give up */
804 goto done;
808 *ws = calloc(*wlen + 1, sizeof(*ws));
809 if (*ws == NULL) {
810 err = got_error_from_errno("calloc");
811 goto done;
814 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
815 err = got_error_from_errno("mbstowcs");
816 done:
817 free(vis);
818 if (err) {
819 free(*ws);
820 *ws = NULL;
821 *wlen = 0;
823 return err;
826 /* Format a line for display, ensuring that it won't overflow a width limit. */
827 static const struct got_error *
828 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
830 const struct got_error *err = NULL;
831 int cols = 0;
832 wchar_t *wline = NULL;
833 size_t wlen;
834 int i;
836 *wlinep = NULL;
837 *widthp = 0;
839 err = mbs2ws(&wline, &wlen, line);
840 if (err)
841 return err;
843 i = 0;
844 while (i < wlen && cols < wlimit) {
845 int width = wcwidth(wline[i]);
846 switch (width) {
847 case 0:
848 i++;
849 break;
850 case 1:
851 case 2:
852 if (cols + width <= wlimit)
853 cols += width;
854 i++;
855 break;
856 case -1:
857 if (wline[i] == L'\t')
858 cols += TABSIZE - ((cols + 1) % TABSIZE);
859 i++;
860 break;
861 default:
862 err = got_error_from_errno("wcwidth");
863 goto done;
866 wline[i] = L'\0';
867 if (widthp)
868 *widthp = cols;
869 done:
870 if (err)
871 free(wline);
872 else
873 *wlinep = wline;
874 return err;
877 static const struct got_error*
878 build_refs_str(char **refs_str, struct got_reflist_head *refs,
879 struct got_object_id *id)
881 static const struct got_error *err = NULL;
882 struct got_reflist_entry *re;
883 char *s;
884 const char *name;
886 *refs_str = NULL;
888 SIMPLEQ_FOREACH(re, refs, entry) {
889 if (got_object_id_cmp(re->id, id) != 0)
890 continue;
891 name = got_ref_get_name(re->ref);
892 if (strcmp(name, GOT_REF_HEAD) == 0)
893 continue;
894 if (strncmp(name, "refs/", 5) == 0)
895 name += 5;
896 if (strncmp(name, "got/", 4) == 0)
897 continue;
898 if (strncmp(name, "heads/", 6) == 0)
899 name += 6;
900 if (strncmp(name, "remotes/", 8) == 0)
901 name += 8;
902 s = *refs_str;
903 if (asprintf(refs_str, "%s%s%s", s ? s : "",
904 s ? ", " : "", name) == -1) {
905 err = got_error_from_errno("asprintf");
906 free(s);
907 *refs_str = NULL;
908 break;
910 free(s);
913 return err;
916 static const struct got_error *
917 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
919 char *smallerthan, *at;
921 smallerthan = strchr(author, '<');
922 if (smallerthan && smallerthan[1] != '\0')
923 author = smallerthan + 1;
924 at = strchr(author, '@');
925 if (at)
926 *at = '\0';
927 return format_line(wauthor, author_width, author, limit);
930 static const struct got_error *
931 draw_commit(struct tog_view *view, struct got_commit_object *commit,
932 struct got_object_id *id, struct got_reflist_head *refs,
933 int author_display_cols)
935 const struct got_error *err = NULL;
936 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
937 char *logmsg0 = NULL, *logmsg = NULL;
938 char *author = NULL;
939 wchar_t *wlogmsg = NULL, *wauthor = NULL;
940 int author_width, logmsg_width;
941 char *newline, *line = NULL;
942 int col, limit;
943 static const size_t date_display_cols = 9;
944 const int avail = view->ncols;
945 struct tm tm;
946 time_t committer_time;
948 committer_time = got_object_commit_get_committer_time(commit);
949 if (localtime_r(&committer_time, &tm) == NULL)
950 return got_error_from_errno("localtime_r");
951 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
952 >= sizeof(datebuf))
953 return got_error(GOT_ERR_NO_SPACE);
955 if (avail < date_display_cols)
956 limit = MIN(sizeof(datebuf) - 1, avail);
957 else
958 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
959 waddnstr(view->window, datebuf, limit);
960 col = limit + 1;
961 if (col > avail)
962 goto done;
964 author = strdup(got_object_commit_get_author(commit));
965 if (author == NULL) {
966 err = got_error_from_errno("strdup");
967 goto done;
969 err = format_author(&wauthor, &author_width, author, avail - col);
970 if (err)
971 goto done;
972 waddwstr(view->window, wauthor);
973 col += author_width;
974 while (col <= avail && author_width < author_display_cols + 2) {
975 waddch(view->window, ' ');
976 col++;
977 author_width++;
979 if (col > avail)
980 goto done;
982 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
983 if (logmsg0 == NULL) {
984 err = got_error_from_errno("strdup");
985 goto done;
987 logmsg = logmsg0;
988 while (*logmsg == '\n')
989 logmsg++;
990 newline = strchr(logmsg, '\n');
991 if (newline)
992 *newline = '\0';
993 limit = avail - col;
994 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
995 if (err)
996 goto done;
997 waddwstr(view->window, wlogmsg);
998 col += logmsg_width;
999 while (col <= avail) {
1000 waddch(view->window, ' ');
1001 col++;
1003 done:
1004 free(logmsg0);
1005 free(wlogmsg);
1006 free(author);
1007 free(wauthor);
1008 free(line);
1009 return err;
1012 static struct commit_queue_entry *
1013 alloc_commit_queue_entry(struct got_commit_object *commit,
1014 struct got_object_id *id)
1016 struct commit_queue_entry *entry;
1018 entry = calloc(1, sizeof(*entry));
1019 if (entry == NULL)
1020 return NULL;
1022 entry->id = id;
1023 entry->commit = commit;
1024 return entry;
1027 static void
1028 pop_commit(struct commit_queue *commits)
1030 struct commit_queue_entry *entry;
1032 entry = TAILQ_FIRST(&commits->head);
1033 TAILQ_REMOVE(&commits->head, entry, entry);
1034 got_object_commit_close(entry->commit);
1035 commits->ncommits--;
1036 /* Don't free entry->id! It is owned by the commit graph. */
1037 free(entry);
1040 static void
1041 free_commits(struct commit_queue *commits)
1043 while (!TAILQ_EMPTY(&commits->head))
1044 pop_commit(commits);
1047 static const struct got_error *
1048 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1049 int minqueue, struct got_repository *repo, const char *path)
1051 const struct got_error *err = NULL;
1052 int nqueued = 0;
1055 * We keep all commits open throughout the lifetime of the log
1056 * view in order to avoid having to re-fetch commits from disk
1057 * while updating the display.
1059 while (nqueued < minqueue) {
1060 struct got_object_id *id;
1061 struct got_commit_object *commit;
1062 struct commit_queue_entry *entry;
1063 int errcode;
1065 err = got_commit_graph_iter_next(&id, graph);
1066 if (err) {
1067 if (err->code != GOT_ERR_ITER_NEED_MORE)
1068 break;
1069 err = got_commit_graph_fetch_commits(graph,
1070 minqueue, repo);
1071 if (err)
1072 return err;
1073 continue;
1076 if (id == NULL)
1077 break;
1079 err = got_object_open_as_commit(&commit, repo, id);
1080 if (err)
1081 break;
1082 entry = alloc_commit_queue_entry(commit, id);
1083 if (entry == NULL) {
1084 err = got_error_from_errno("alloc_commit_queue_entry");
1085 break;
1088 errcode = pthread_mutex_lock(&tog_mutex);
1089 if (errcode) {
1090 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1091 break;
1094 entry->idx = commits->ncommits;
1095 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1096 nqueued++;
1097 commits->ncommits++;
1099 errcode = pthread_mutex_unlock(&tog_mutex);
1100 if (errcode && err == NULL)
1101 err = got_error_set_errno(errcode,
1102 "pthread_mutex_unlock");
1105 return err;
1108 static const struct got_error *
1109 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1110 struct got_repository *repo)
1112 const struct got_error *err = NULL;
1113 struct got_reference *head_ref;
1115 *head_id = NULL;
1117 err = got_ref_open(&head_ref, repo, branch_name, 0);
1118 if (err)
1119 return err;
1121 err = got_ref_resolve(head_id, repo, head_ref);
1122 got_ref_close(head_ref);
1123 if (err) {
1124 *head_id = NULL;
1125 return err;
1128 return NULL;
1131 static const struct got_error *
1132 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1133 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1134 struct commit_queue *commits, int selected_idx, int limit,
1135 struct got_reflist_head *refs, const char *path, int commits_needed)
1137 const struct got_error *err = NULL;
1138 struct commit_queue_entry *entry;
1139 int ncommits, width;
1140 int author_cols = 10;
1141 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1142 char *refs_str = NULL;
1143 wchar_t *wline;
1145 entry = first;
1146 ncommits = 0;
1147 while (entry) {
1148 if (ncommits == selected_idx) {
1149 *selected = entry;
1150 break;
1152 entry = TAILQ_NEXT(entry, entry);
1153 ncommits++;
1156 if (*selected) {
1157 err = got_object_id_str(&id_str, (*selected)->id);
1158 if (err)
1159 return err;
1160 if (refs) {
1161 err = build_refs_str(&refs_str, refs, (*selected)->id);
1162 if (err)
1163 goto done;
1167 if (commits_needed == 0)
1168 halfdelay(10); /* disable fast refresh */
1170 if (asprintf(&ncommits_str, " [%d/%d] %s",
1171 entry ? entry->idx + 1 : 0, commits->ncommits,
1172 commits_needed > 0 ? "loading... " :
1173 (refs_str ? refs_str : "")) == -1) {
1174 err = got_error_from_errno("asprintf");
1175 goto done;
1178 if (path && strcmp(path, "/") != 0) {
1179 if (asprintf(&header, "commit %s %s%s",
1180 id_str ? id_str : "........................................",
1181 path, ncommits_str) == -1) {
1182 err = got_error_from_errno("asprintf");
1183 header = NULL;
1184 goto done;
1186 } else if (asprintf(&header, "commit %s%s",
1187 id_str ? id_str : "........................................",
1188 ncommits_str) == -1) {
1189 err = got_error_from_errno("asprintf");
1190 header = NULL;
1191 goto done;
1193 err = format_line(&wline, &width, header, view->ncols);
1194 if (err)
1195 goto done;
1197 werase(view->window);
1199 if (view_needs_focus_indication(view))
1200 wstandout(view->window);
1201 waddwstr(view->window, wline);
1202 while (width < view->ncols) {
1203 waddch(view->window, ' ');
1204 width++;
1206 if (view_needs_focus_indication(view))
1207 wstandend(view->window);
1208 free(wline);
1209 if (limit <= 1)
1210 goto done;
1212 /* Grow author column size if necessary. */
1213 entry = first;
1214 ncommits = 0;
1215 while (entry) {
1216 char *author;
1217 wchar_t *wauthor;
1218 int width;
1219 if (ncommits >= limit - 1)
1220 break;
1221 author = strdup(got_object_commit_get_author(entry->commit));
1222 if (author == NULL) {
1223 err = got_error_from_errno("strdup");
1224 goto done;
1226 err = format_author(&wauthor, &width, author, COLS);
1227 if (author_cols < width)
1228 author_cols = width;
1229 free(wauthor);
1230 free(author);
1231 entry = TAILQ_NEXT(entry, entry);
1234 entry = first;
1235 *last = first;
1236 ncommits = 0;
1237 while (entry) {
1238 if (ncommits >= limit - 1)
1239 break;
1240 if (ncommits == selected_idx)
1241 wstandout(view->window);
1242 err = draw_commit(view, entry->commit, entry->id, refs,
1243 author_cols);
1244 if (ncommits == selected_idx)
1245 wstandend(view->window);
1246 if (err)
1247 break;
1248 ncommits++;
1249 *last = entry;
1250 entry = TAILQ_NEXT(entry, entry);
1253 view_vborder(view);
1254 done:
1255 free(id_str);
1256 free(refs_str);
1257 free(ncommits_str);
1258 free(header);
1259 return err;
1262 static void
1263 scroll_up(struct tog_view *view,
1264 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1265 struct commit_queue *commits)
1267 struct commit_queue_entry *entry;
1268 int nscrolled = 0;
1270 entry = TAILQ_FIRST(&commits->head);
1271 if (*first_displayed_entry == entry)
1272 return;
1274 entry = *first_displayed_entry;
1275 while (entry && nscrolled < maxscroll) {
1276 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1277 if (entry) {
1278 *first_displayed_entry = entry;
1279 nscrolled++;
1284 static const struct got_error *
1285 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1286 pthread_cond_t *need_commits)
1288 int errcode;
1289 int max_wait = 20;
1291 halfdelay(1); /* fast refresh while loading commits */
1293 while (*commits_needed > 0) {
1294 if (*log_complete)
1295 break;
1297 /* Wake the log thread. */
1298 errcode = pthread_cond_signal(need_commits);
1299 if (errcode)
1300 return got_error_set_errno(errcode,
1301 "pthread_cond_signal");
1302 errcode = pthread_mutex_unlock(&tog_mutex);
1303 if (errcode)
1304 return got_error_set_errno(errcode,
1305 "pthread_mutex_unlock");
1306 pthread_yield();
1307 errcode = pthread_mutex_lock(&tog_mutex);
1308 if (errcode)
1309 return got_error_set_errno(errcode,
1310 "pthread_mutex_lock");
1312 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1314 * Thread is not done yet; lose a key press
1315 * and let the user retry... this way the GUI
1316 * remains interactive while logging deep paths
1317 * with few commits in history.
1319 return NULL;
1323 return NULL;
1326 static const struct got_error *
1327 scroll_down(struct tog_view *view,
1328 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1329 struct commit_queue_entry **last_displayed_entry,
1330 struct commit_queue *commits, int *log_complete, int *commits_needed,
1331 pthread_cond_t *need_commits)
1333 const struct got_error *err = NULL;
1334 struct commit_queue_entry *pentry;
1335 int nscrolled = 0;
1337 if (*last_displayed_entry == NULL)
1338 return NULL;
1340 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1341 if (pentry == NULL && !*log_complete) {
1343 * Ask the log thread for required amount of commits
1344 * plus some amount of pre-fetching.
1346 (*commits_needed) += maxscroll + 20;
1347 err = trigger_log_thread(0, commits_needed, log_complete,
1348 need_commits);
1349 if (err)
1350 return err;
1353 do {
1354 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1355 if (pentry == NULL)
1356 break;
1358 *last_displayed_entry = pentry;
1360 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1361 if (pentry == NULL)
1362 break;
1363 *first_displayed_entry = pentry;
1364 } while (++nscrolled < maxscroll);
1366 return err;
1369 static const struct got_error *
1370 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1371 struct got_commit_object *commit, struct got_object_id *commit_id,
1372 struct tog_view *log_view, struct got_reflist_head *refs,
1373 struct got_repository *repo)
1375 const struct got_error *err;
1376 struct got_object_qid *parent_id;
1377 struct tog_view *diff_view;
1379 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1380 if (diff_view == NULL)
1381 return got_error_from_errno("view_open");
1383 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1384 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1385 commit_id, log_view, refs, repo);
1386 if (err == NULL)
1387 *new_view = diff_view;
1388 return err;
1391 static const struct got_error *
1392 tree_view_visit_subtree(struct got_tree_object *subtree,
1393 struct tog_tree_view_state *s)
1395 struct tog_parent_tree *parent;
1397 parent = calloc(1, sizeof(*parent));
1398 if (parent == NULL)
1399 return got_error_from_errno("calloc");
1401 parent->tree = s->tree;
1402 parent->first_displayed_entry = s->first_displayed_entry;
1403 parent->selected_entry = s->selected_entry;
1404 parent->selected = s->selected;
1405 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1406 s->tree = subtree;
1407 s->entries = got_object_tree_get_entries(s->tree);
1408 s->selected = 0;
1409 s->first_displayed_entry = NULL;
1410 return NULL;
1414 static const struct got_error *
1415 browse_commit_tree(struct tog_view **new_view, int begin_x,
1416 struct commit_queue_entry *entry, const char *path,
1417 struct got_reflist_head *refs, struct got_repository *repo)
1419 const struct got_error *err = NULL;
1420 struct got_tree_object *tree;
1421 struct got_tree_entry *te;
1422 struct tog_tree_view_state *s;
1423 struct tog_view *tree_view;
1424 char *slash, *subpath = NULL;
1425 const char *p;
1427 err = got_object_open_as_tree(&tree, repo,
1428 got_object_commit_get_tree_id(entry->commit));
1429 if (err)
1430 return err;
1432 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1433 if (tree_view == NULL)
1434 return got_error_from_errno("view_open");
1436 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1437 if (err) {
1438 got_object_tree_close(tree);
1439 return err;
1441 s = &tree_view->state.tree;
1443 *new_view = tree_view;
1445 /* Walk the path and open corresponding tree objects. */
1446 p = path;
1447 while (*p) {
1448 struct got_object_id *tree_id;
1450 while (p[0] == '/')
1451 p++;
1453 /* Ensure the correct subtree entry is selected. */
1454 slash = strchr(p, '/');
1455 if (slash == NULL)
1456 slash = strchr(p, '\0');
1457 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1458 if (strncmp(p, te->name, slash - p) == 0) {
1459 s->selected_entry = te;
1460 break;
1462 s->selected++;
1464 if (s->selected_entry == NULL) {
1465 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1466 break;
1468 if (s->tree != s->root)
1469 s->selected++; /* skip '..' */
1471 if (!S_ISDIR(s->selected_entry->mode))
1472 break;
1474 slash = strchr(p, '/');
1475 if (slash)
1476 subpath = strndup(path, slash - path);
1477 else
1478 subpath = strdup(path);
1479 if (subpath == NULL) {
1480 err = got_error_from_errno("strdup");
1481 break;
1484 err = got_object_id_by_path(&tree_id, repo, entry->id,
1485 subpath);
1486 if (err)
1487 break;
1489 err = got_object_open_as_tree(&tree, repo, tree_id);
1490 free(tree_id);
1491 if (err)
1492 break;
1494 err = tree_view_visit_subtree(tree, s);
1495 if (err) {
1496 got_object_tree_close(tree);
1497 break;
1499 if (slash == NULL)
1500 break;
1501 free(subpath);
1502 subpath = NULL;
1503 p = slash;
1506 free(subpath);
1507 return err;
1510 static void *
1511 log_thread(void *arg)
1513 const struct got_error *err = NULL;
1514 int errcode = 0;
1515 struct tog_log_thread_args *a = arg;
1516 int done = 0;
1518 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1519 if (err)
1520 return (void *)err;
1522 while (!done && !err) {
1523 err = queue_commits(a->graph, a->commits, 1, a->repo,
1524 a->in_repo_path);
1525 if (err) {
1526 if (err->code != GOT_ERR_ITER_COMPLETED)
1527 return (void *)err;
1528 err = NULL;
1529 done = 1;
1530 } else if (a->commits_needed > 0)
1531 a->commits_needed--;
1533 errcode = pthread_mutex_lock(&tog_mutex);
1534 if (errcode) {
1535 err = got_error_set_errno(errcode,
1536 "pthread_mutex_lock");
1537 break;
1538 } else if (*a->quit)
1539 done = 1;
1540 else if (*a->first_displayed_entry == NULL) {
1541 *a->first_displayed_entry =
1542 TAILQ_FIRST(&a->commits->head);
1543 *a->selected_entry = *a->first_displayed_entry;
1546 if (done)
1547 a->commits_needed = 0;
1548 else if (a->commits_needed == 0) {
1549 errcode = pthread_cond_wait(&a->need_commits,
1550 &tog_mutex);
1551 if (errcode)
1552 err = got_error_set_errno(errcode,
1553 "pthread_cond_wait");
1556 errcode = pthread_mutex_unlock(&tog_mutex);
1557 if (errcode && err == NULL)
1558 err = got_error_set_errno(errcode,
1559 "pthread_mutex_unlock");
1561 a->log_complete = 1;
1562 return (void *)err;
1565 static const struct got_error *
1566 stop_log_thread(struct tog_log_view_state *s)
1568 const struct got_error *err = NULL;
1569 int errcode;
1571 if (s->thread) {
1572 s->quit = 1;
1573 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1574 if (errcode)
1575 return got_error_set_errno(errcode,
1576 "pthread_cond_signal");
1577 errcode = pthread_mutex_unlock(&tog_mutex);
1578 if (errcode)
1579 return got_error_set_errno(errcode,
1580 "pthread_mutex_unlock");
1581 errcode = pthread_join(s->thread, (void **)&err);
1582 if (errcode)
1583 return got_error_set_errno(errcode, "pthread_join");
1584 errcode = pthread_mutex_lock(&tog_mutex);
1585 if (errcode)
1586 return got_error_set_errno(errcode,
1587 "pthread_mutex_lock");
1588 s->thread = NULL;
1591 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1592 if (errcode && err == NULL)
1593 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1595 if (s->thread_args.repo) {
1596 got_repo_close(s->thread_args.repo);
1597 s->thread_args.repo = NULL;
1600 if (s->thread_args.graph) {
1601 got_commit_graph_close(s->thread_args.graph);
1602 s->thread_args.graph = NULL;
1605 return err;
1608 static const struct got_error *
1609 close_log_view(struct tog_view *view)
1611 const struct got_error *err = NULL;
1612 struct tog_log_view_state *s = &view->state.log;
1614 err = stop_log_thread(s);
1615 free_commits(&s->commits);
1616 free(s->in_repo_path);
1617 s->in_repo_path = NULL;
1618 free(s->start_id);
1619 s->start_id = NULL;
1620 return err;
1623 static const struct got_error *
1624 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1625 struct got_reflist_head *refs, struct got_repository *repo,
1626 const char *path, int check_disk)
1628 const struct got_error *err = NULL;
1629 struct tog_log_view_state *s = &view->state.log;
1630 struct got_repository *thread_repo = NULL;
1631 struct got_commit_graph *thread_graph = NULL;
1632 int errcode;
1634 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1635 if (err != NULL)
1636 goto done;
1638 /* The commit queue only contains commits being displayed. */
1639 TAILQ_INIT(&s->commits.head);
1640 s->commits.ncommits = 0;
1642 s->refs = refs;
1643 s->repo = repo;
1644 s->start_id = got_object_id_dup(start_id);
1645 if (s->start_id == NULL) {
1646 err = got_error_from_errno("got_object_id_dup");
1647 goto done;
1650 view->show = show_log_view;
1651 view->input = input_log_view;
1652 view->close = close_log_view;
1654 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1655 if (err)
1656 goto done;
1657 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1658 0, thread_repo);
1659 if (err)
1660 goto done;
1662 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1663 if (errcode) {
1664 err = got_error_set_errno(errcode, "pthread_cond_init");
1665 goto done;
1668 s->thread_args.commits_needed = view->nlines;
1669 s->thread_args.graph = thread_graph;
1670 s->thread_args.commits = &s->commits;
1671 s->thread_args.in_repo_path = s->in_repo_path;
1672 s->thread_args.start_id = s->start_id;
1673 s->thread_args.repo = thread_repo;
1674 s->thread_args.log_complete = 0;
1675 s->thread_args.quit = &s->quit;
1676 s->thread_args.view = view;
1677 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1678 s->thread_args.selected_entry = &s->selected_entry;
1679 done:
1680 if (err)
1681 close_log_view(view);
1682 return err;
1685 static const struct got_error *
1686 show_log_view(struct tog_view *view)
1688 struct tog_log_view_state *s = &view->state.log;
1690 if (s->thread == NULL) {
1691 int errcode = pthread_create(&s->thread, NULL, log_thread,
1692 &s->thread_args);
1693 if (errcode)
1694 return got_error_set_errno(errcode, "pthread_create");
1697 return draw_commits(view, &s->last_displayed_entry,
1698 &s->selected_entry, s->first_displayed_entry,
1699 &s->commits, s->selected, view->nlines, s->refs,
1700 s->in_repo_path, s->thread_args.commits_needed);
1703 static const struct got_error *
1704 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1705 struct tog_view **focus_view, struct tog_view *view, int ch)
1707 const struct got_error *err = NULL;
1708 struct tog_log_view_state *s = &view->state.log;
1709 char *parent_path;
1710 struct tog_view *diff_view = NULL, *tree_view = NULL;
1711 int begin_x = 0;
1713 switch (ch) {
1714 case 'q':
1715 s->quit = 1;
1716 break;
1717 case 'k':
1718 case KEY_UP:
1719 case '<':
1720 case ',':
1721 if (s->first_displayed_entry == NULL)
1722 break;
1723 if (s->selected > 0)
1724 s->selected--;
1725 if (s->selected > 0)
1726 break;
1727 scroll_up(view, &s->first_displayed_entry, 1,
1728 &s->commits);
1729 break;
1730 case KEY_PPAGE:
1731 case CTRL('b'):
1732 if (s->first_displayed_entry == NULL)
1733 break;
1734 if (TAILQ_FIRST(&s->commits.head) ==
1735 s->first_displayed_entry) {
1736 s->selected = 0;
1737 break;
1739 scroll_up(view, &s->first_displayed_entry,
1740 view->nlines, &s->commits);
1741 break;
1742 case 'j':
1743 case KEY_DOWN:
1744 case '>':
1745 case '.':
1746 if (s->first_displayed_entry == NULL)
1747 break;
1748 if (s->selected < MIN(view->nlines - 2,
1749 s->commits.ncommits - 1)) {
1750 s->selected++;
1751 break;
1753 err = scroll_down(view, &s->first_displayed_entry, 1,
1754 &s->last_displayed_entry, &s->commits,
1755 &s->thread_args.log_complete,
1756 &s->thread_args.commits_needed,
1757 &s->thread_args.need_commits);
1758 break;
1759 case KEY_NPAGE:
1760 case CTRL('f'): {
1761 struct commit_queue_entry *first;
1762 first = s->first_displayed_entry;
1763 if (first == NULL)
1764 break;
1765 err = scroll_down(view, &s->first_displayed_entry,
1766 view->nlines, &s->last_displayed_entry,
1767 &s->commits, &s->thread_args.log_complete,
1768 &s->thread_args.commits_needed,
1769 &s->thread_args.need_commits);
1770 if (first == s->first_displayed_entry &&
1771 s->selected < MIN(view->nlines - 2,
1772 s->commits.ncommits - 1)) {
1773 /* can't scroll further down */
1774 s->selected = MIN(view->nlines - 2,
1775 s->commits.ncommits - 1);
1777 err = NULL;
1778 break;
1780 case KEY_RESIZE:
1781 if (s->selected > view->nlines - 2)
1782 s->selected = view->nlines - 2;
1783 if (s->selected > s->commits.ncommits - 1)
1784 s->selected = s->commits.ncommits - 1;
1785 break;
1786 case KEY_ENTER:
1787 case ' ':
1788 case '\r':
1789 if (s->selected_entry == NULL)
1790 break;
1791 if (view_is_parent_view(view))
1792 begin_x = view_split_begin_x(view->begin_x);
1793 err = open_diff_view_for_commit(&diff_view, begin_x,
1794 s->selected_entry->commit, s->selected_entry->id,
1795 view, s->refs, s->repo);
1796 if (err)
1797 break;
1798 if (view_is_parent_view(view)) {
1799 err = view_close_child(view);
1800 if (err)
1801 return err;
1802 err = view_set_child(view, diff_view);
1803 if (err) {
1804 view_close(diff_view);
1805 break;
1807 *focus_view = diff_view;
1808 view->child_focussed = 1;
1809 } else
1810 *new_view = diff_view;
1811 break;
1812 case 't':
1813 if (s->selected_entry == NULL)
1814 break;
1815 if (view_is_parent_view(view))
1816 begin_x = view_split_begin_x(view->begin_x);
1817 err = browse_commit_tree(&tree_view, begin_x,
1818 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1819 if (err)
1820 break;
1821 if (view_is_parent_view(view)) {
1822 err = view_close_child(view);
1823 if (err)
1824 return err;
1825 err = view_set_child(view, tree_view);
1826 if (err) {
1827 view_close(tree_view);
1828 break;
1830 *focus_view = tree_view;
1831 view->child_focussed = 1;
1832 } else
1833 *new_view = tree_view;
1834 break;
1835 case KEY_BACKSPACE:
1836 if (strcmp(s->in_repo_path, "/") == 0)
1837 break;
1838 parent_path = dirname(s->in_repo_path);
1839 if (parent_path && strcmp(parent_path, ".") != 0) {
1840 struct tog_view *lv;
1841 err = stop_log_thread(s);
1842 if (err)
1843 return err;
1844 lv = view_open(view->nlines, view->ncols,
1845 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1846 if (lv == NULL)
1847 return got_error_from_errno(
1848 "view_open");
1849 err = open_log_view(lv, s->start_id, s->refs,
1850 s->repo, parent_path, 0);
1851 if (err)
1852 return err;;
1853 if (view_is_parent_view(view))
1854 *new_view = lv;
1855 else {
1856 view_set_child(view->parent, lv);
1857 *focus_view = lv;
1859 return NULL;
1861 break;
1862 default:
1863 break;
1866 return err;
1869 static const struct got_error *
1870 apply_unveil(const char *repo_path, const char *worktree_path)
1872 const struct got_error *error;
1874 if (repo_path && unveil(repo_path, "r") != 0)
1875 return got_error_from_errno2("unveil", repo_path);
1877 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1878 return got_error_from_errno2("unveil", worktree_path);
1880 if (unveil("/tmp", "rwc") != 0)
1881 return got_error_from_errno2("unveil", "/tmp");
1883 error = got_privsep_unveil_exec_helpers();
1884 if (error != NULL)
1885 return error;
1887 if (unveil(NULL, NULL) != 0)
1888 return got_error_from_errno("unveil");
1890 return NULL;
1893 static void
1894 init_curses(void)
1896 initscr();
1897 cbreak();
1898 halfdelay(1); /* Do fast refresh while initial view is loading. */
1899 noecho();
1900 nonl();
1901 intrflush(stdscr, FALSE);
1902 keypad(stdscr, TRUE);
1903 curs_set(0);
1904 signal(SIGWINCH, tog_sigwinch);
1907 static const struct got_error *
1908 cmd_log(int argc, char *argv[])
1910 const struct got_error *error;
1911 struct got_repository *repo = NULL;
1912 struct got_worktree *worktree = NULL;
1913 struct got_reflist_head refs;
1914 struct got_object_id *start_id = NULL;
1915 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1916 char *start_commit = NULL;
1917 int ch;
1918 struct tog_view *view;
1920 SIMPLEQ_INIT(&refs);
1922 #ifndef PROFILE
1923 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1924 NULL) == -1)
1925 err(1, "pledge");
1926 #endif
1928 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1929 switch (ch) {
1930 case 'c':
1931 start_commit = optarg;
1932 break;
1933 case 'r':
1934 repo_path = realpath(optarg, NULL);
1935 if (repo_path == NULL)
1936 err(1, "-r option");
1937 break;
1938 default:
1939 usage_log();
1940 /* NOTREACHED */
1944 argc -= optind;
1945 argv += optind;
1947 cwd = getcwd(NULL, 0);
1948 if (cwd == NULL) {
1949 error = got_error_from_errno("getcwd");
1950 goto done;
1952 error = got_worktree_open(&worktree, cwd);
1953 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1954 goto done;
1955 error = NULL;
1957 if (argc == 0) {
1958 path = strdup("");
1959 if (path == NULL) {
1960 error = got_error_from_errno("strdup");
1961 goto done;
1963 } else if (argc == 1) {
1964 if (worktree) {
1965 error = got_worktree_resolve_path(&path, worktree,
1966 argv[0]);
1967 if (error)
1968 goto done;
1969 } else {
1970 path = strdup(argv[0]);
1971 if (path == NULL) {
1972 error = got_error_from_errno("strdup");
1973 goto done;
1976 } else
1977 usage_log();
1979 repo_path = worktree ?
1980 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1981 if (repo_path == NULL) {
1982 error = got_error_from_errno("strdup");
1983 goto done;
1986 init_curses();
1988 error = got_repo_open(&repo, repo_path);
1989 if (error != NULL)
1990 goto done;
1992 error = apply_unveil(got_repo_get_path(repo),
1993 worktree ? got_worktree_get_root_path(worktree) : NULL);
1994 if (error)
1995 goto done;
1997 if (start_commit == NULL)
1998 error = get_head_commit_id(&start_id, worktree ?
1999 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2000 repo);
2001 else
2002 error = got_object_resolve_id_str(&start_id, repo,
2003 start_commit);
2004 if (error != NULL)
2005 goto done;
2007 error = got_ref_list(&refs, repo);
2008 if (error)
2009 goto done;
2011 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2012 if (view == NULL) {
2013 error = got_error_from_errno("view_open");
2014 goto done;
2016 error = open_log_view(view, start_id, &refs, repo, path, 1);
2017 if (error)
2018 goto done;
2019 error = view_loop(view);
2020 done:
2021 free(repo_path);
2022 free(cwd);
2023 free(path);
2024 free(start_id);
2025 if (repo)
2026 got_repo_close(repo);
2027 if (worktree)
2028 got_worktree_close(worktree);
2029 got_ref_list_free(&refs);
2030 return error;
2033 __dead static void
2034 usage_diff(void)
2036 endwin();
2037 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2038 getprogname());
2039 exit(1);
2042 static char *
2043 parse_next_line(FILE *f, size_t *len)
2045 char *line;
2046 size_t linelen;
2047 size_t lineno;
2048 const char delim[3] = { '\0', '\0', '\0'};
2050 line = fparseln(f, &linelen, &lineno, delim, 0);
2051 if (len)
2052 *len = linelen;
2053 return line;
2056 static const struct got_error *
2057 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2058 int *last_displayed_line, int *eof, int max_lines,
2059 char *header)
2061 const struct got_error *err;
2062 int nlines = 0, nprinted = 0;
2063 char *line;
2064 size_t len;
2065 wchar_t *wline;
2066 int width;
2068 rewind(f);
2069 werase(view->window);
2071 if (header) {
2072 err = format_line(&wline, &width, header, view->ncols);
2073 if (err) {
2074 return err;
2077 if (view_needs_focus_indication(view))
2078 wstandout(view->window);
2079 waddwstr(view->window, wline);
2080 if (view_needs_focus_indication(view))
2081 wstandend(view->window);
2082 if (width < view->ncols)
2083 waddch(view->window, '\n');
2085 if (max_lines <= 1)
2086 return NULL;
2087 max_lines--;
2090 *eof = 0;
2091 while (nprinted < max_lines) {
2092 line = parse_next_line(f, &len);
2093 if (line == NULL) {
2094 *eof = 1;
2095 break;
2097 if (++nlines < *first_displayed_line) {
2098 free(line);
2099 continue;
2102 err = format_line(&wline, &width, line, view->ncols);
2103 if (err) {
2104 free(line);
2105 return err;
2107 waddwstr(view->window, wline);
2108 if (width < view->ncols)
2109 waddch(view->window, '\n');
2110 if (++nprinted == 1)
2111 *first_displayed_line = nlines;
2112 free(line);
2113 free(wline);
2114 wline = NULL;
2116 *last_displayed_line = nlines;
2118 view_vborder(view);
2120 if (*eof) {
2121 while (nprinted < view->nlines) {
2122 waddch(view->window, '\n');
2123 nprinted++;
2126 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2127 if (err) {
2128 return err;
2131 wstandout(view->window);
2132 waddwstr(view->window, wline);
2133 wstandend(view->window);
2136 return NULL;
2139 static char *
2140 get_datestr(time_t *time, char *datebuf)
2142 char *p, *s = ctime_r(time, datebuf);
2143 p = strchr(s, '\n');
2144 if (p)
2145 *p = '\0';
2146 return s;
2149 static const struct got_error *
2150 write_commit_info(struct got_object_id *commit_id,
2151 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2153 const struct got_error *err = NULL;
2154 char datebuf[26];
2155 struct got_commit_object *commit;
2156 char *id_str = NULL;
2157 time_t committer_time;
2158 const char *author, *committer;
2159 char *refs_str = NULL;
2161 if (refs) {
2162 err = build_refs_str(&refs_str, refs, commit_id);
2163 if (err)
2164 return err;
2167 err = got_object_open_as_commit(&commit, repo, commit_id);
2168 if (err)
2169 return err;
2171 err = got_object_id_str(&id_str, commit_id);
2172 if (err) {
2173 err = got_error_from_errno("got_object_id_str");
2174 goto done;
2177 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2178 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2179 err = got_error_from_errno("fprintf");
2180 goto done;
2182 if (fprintf(outfile, "from: %s\n",
2183 got_object_commit_get_author(commit)) < 0) {
2184 err = got_error_from_errno("fprintf");
2185 goto done;
2187 committer_time = got_object_commit_get_committer_time(commit);
2188 if (fprintf(outfile, "date: %s UTC\n",
2189 get_datestr(&committer_time, datebuf)) < 0) {
2190 err = got_error_from_errno("fprintf");
2191 goto done;
2193 author = got_object_commit_get_author(commit);
2194 committer = got_object_commit_get_committer(commit);
2195 if (strcmp(author, committer) != 0 &&
2196 fprintf(outfile, "via: %s\n", committer) < 0) {
2197 err = got_error_from_errno("fprintf");
2198 goto done;
2200 if (fprintf(outfile, "%s\n",
2201 got_object_commit_get_logmsg(commit)) < 0) {
2202 err = got_error_from_errno("fprintf");
2203 goto done;
2205 done:
2206 free(id_str);
2207 free(refs_str);
2208 got_object_commit_close(commit);
2209 return err;
2212 static const struct got_error *
2213 create_diff(struct tog_diff_view_state *s)
2215 const struct got_error *err = NULL;
2216 FILE *f = NULL;
2217 int obj_type;
2219 f = got_opentemp();
2220 if (f == NULL) {
2221 err = got_error_from_errno("got_opentemp");
2222 goto done;
2224 if (s->f && fclose(s->f) != 0) {
2225 err = got_error_from_errno("fclose");
2226 goto done;
2228 s->f = f;
2230 if (s->id1)
2231 err = got_object_get_type(&obj_type, s->repo, s->id1);
2232 else
2233 err = got_object_get_type(&obj_type, s->repo, s->id2);
2234 if (err)
2235 goto done;
2237 switch (obj_type) {
2238 case GOT_OBJ_TYPE_BLOB:
2239 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2240 s->diff_context, s->repo, f);
2241 break;
2242 case GOT_OBJ_TYPE_TREE:
2243 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2244 s->diff_context, s->repo, f);
2245 break;
2246 case GOT_OBJ_TYPE_COMMIT: {
2247 const struct got_object_id_queue *parent_ids;
2248 struct got_object_qid *pid;
2249 struct got_commit_object *commit2;
2251 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2252 if (err)
2253 break;
2254 /* Show commit info if we're diffing to a parent/root commit. */
2255 if (s->id1 == NULL)
2256 write_commit_info(s->id2, s->refs, s->repo, f);
2257 else {
2258 parent_ids = got_object_commit_get_parent_ids(commit2);
2259 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2260 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2261 write_commit_info(s->id2, s->refs,
2262 s->repo, f);
2263 break;
2267 got_object_commit_close(commit2);
2269 err = got_diff_objects_as_commits(s->id1, s->id2,
2270 s->diff_context, s->repo, f);
2271 break;
2273 default:
2274 err = got_error(GOT_ERR_OBJ_TYPE);
2275 break;
2277 done:
2278 if (f && fflush(f) != 0 && err == NULL)
2279 err = got_error_from_errno("fflush");
2280 return err;
2283 static void
2284 diff_view_indicate_progress(struct tog_view *view)
2286 mvwaddstr(view->window, 0, 0, "diffing...");
2287 update_panels();
2288 doupdate();
2291 static const struct got_error *
2292 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2293 struct got_object_id *id2, struct tog_view *log_view,
2294 struct got_reflist_head *refs, struct got_repository *repo)
2296 const struct got_error *err;
2298 if (id1 != NULL && id2 != NULL) {
2299 int type1, type2;
2300 err = got_object_get_type(&type1, repo, id1);
2301 if (err)
2302 return err;
2303 err = got_object_get_type(&type2, repo, id2);
2304 if (err)
2305 return err;
2307 if (type1 != type2)
2308 return got_error(GOT_ERR_OBJ_TYPE);
2311 if (id1) {
2312 view->state.diff.id1 = got_object_id_dup(id1);
2313 if (view->state.diff.id1 == NULL)
2314 return got_error_from_errno("got_object_id_dup");
2315 } else
2316 view->state.diff.id1 = NULL;
2318 view->state.diff.id2 = got_object_id_dup(id2);
2319 if (view->state.diff.id2 == NULL) {
2320 free(view->state.diff.id1);
2321 view->state.diff.id1 = NULL;
2322 return got_error_from_errno("got_object_id_dup");
2324 view->state.diff.f = NULL;
2325 view->state.diff.first_displayed_line = 1;
2326 view->state.diff.last_displayed_line = view->nlines;
2327 view->state.diff.diff_context = 3;
2328 view->state.diff.log_view = log_view;
2329 view->state.diff.repo = repo;
2330 view->state.diff.refs = refs;
2332 if (log_view && view_is_splitscreen(view))
2333 show_log_view(log_view); /* draw vborder */
2334 diff_view_indicate_progress(view);
2336 err = create_diff(&view->state.diff);
2337 if (err) {
2338 free(view->state.diff.id1);
2339 view->state.diff.id1 = NULL;
2340 free(view->state.diff.id2);
2341 view->state.diff.id2 = NULL;
2342 return err;
2345 view->show = show_diff_view;
2346 view->input = input_diff_view;
2347 view->close = close_diff_view;
2349 return NULL;
2352 static const struct got_error *
2353 close_diff_view(struct tog_view *view)
2355 const struct got_error *err = NULL;
2357 free(view->state.diff.id1);
2358 view->state.diff.id1 = NULL;
2359 free(view->state.diff.id2);
2360 view->state.diff.id2 = NULL;
2361 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2362 err = got_error_from_errno("fclose");
2363 return err;
2366 static const struct got_error *
2367 show_diff_view(struct tog_view *view)
2369 const struct got_error *err;
2370 struct tog_diff_view_state *s = &view->state.diff;
2371 char *id_str1 = NULL, *id_str2, *header;
2373 if (s->id1) {
2374 err = got_object_id_str(&id_str1, s->id1);
2375 if (err)
2376 return err;
2378 err = got_object_id_str(&id_str2, s->id2);
2379 if (err)
2380 return err;
2382 if (asprintf(&header, "diff %s %s",
2383 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2384 err = got_error_from_errno("asprintf");
2385 free(id_str1);
2386 free(id_str2);
2387 return err;
2389 free(id_str1);
2390 free(id_str2);
2392 return draw_file(view, s->f, &s->first_displayed_line,
2393 &s->last_displayed_line, &s->eof, view->nlines,
2394 header);
2397 static const struct got_error *
2398 set_selected_commit(struct tog_diff_view_state *s,
2399 struct commit_queue_entry *entry)
2401 const struct got_error *err;
2402 const struct got_object_id_queue *parent_ids;
2403 struct got_commit_object *selected_commit;
2404 struct got_object_qid *pid;
2406 free(s->id2);
2407 s->id2 = got_object_id_dup(entry->id);
2408 if (s->id2 == NULL)
2409 return got_error_from_errno("got_object_id_dup");
2411 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2412 if (err)
2413 return err;
2414 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2415 free(s->id1);
2416 pid = SIMPLEQ_FIRST(parent_ids);
2417 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2418 got_object_commit_close(selected_commit);
2419 return NULL;
2422 static const struct got_error *
2423 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2424 struct tog_view **focus_view, struct tog_view *view, int ch)
2426 const struct got_error *err = NULL;
2427 struct tog_diff_view_state *s = &view->state.diff;
2428 struct tog_log_view_state *ls;
2429 struct commit_queue_entry *entry;
2430 int i;
2432 switch (ch) {
2433 case 'k':
2434 case KEY_UP:
2435 if (s->first_displayed_line > 1)
2436 s->first_displayed_line--;
2437 break;
2438 case KEY_PPAGE:
2439 case CTRL('b'):
2440 if (s->first_displayed_line == 1)
2441 break;
2442 i = 0;
2443 while (i++ < view->nlines - 1 &&
2444 s->first_displayed_line > 1)
2445 s->first_displayed_line--;
2446 break;
2447 case 'j':
2448 case KEY_DOWN:
2449 if (!s->eof)
2450 s->first_displayed_line++;
2451 break;
2452 case KEY_NPAGE:
2453 case CTRL('f'):
2454 case ' ':
2455 if (s->eof)
2456 break;
2457 i = 0;
2458 while (!s->eof && i++ < view->nlines - 1) {
2459 char *line;
2460 line = parse_next_line(s->f, NULL);
2461 s->first_displayed_line++;
2462 if (line == NULL)
2463 break;
2465 break;
2466 case '[':
2467 if (s->diff_context > 0) {
2468 s->diff_context--;
2469 diff_view_indicate_progress(view);
2470 err = create_diff(s);
2472 break;
2473 case ']':
2474 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2475 s->diff_context++;
2476 diff_view_indicate_progress(view);
2477 err = create_diff(s);
2479 break;
2480 case '<':
2481 case ',':
2482 if (s->log_view == NULL)
2483 break;
2484 ls = &s->log_view->state.log;
2485 entry = TAILQ_PREV(ls->selected_entry,
2486 commit_queue_head, entry);
2487 if (entry == NULL)
2488 break;
2490 err = input_log_view(NULL, NULL, NULL, s->log_view,
2491 KEY_UP);
2492 if (err)
2493 break;
2495 err = set_selected_commit(s, entry);
2496 if (err)
2497 break;
2499 s->first_displayed_line = 1;
2500 s->last_displayed_line = view->nlines;
2502 diff_view_indicate_progress(view);
2503 err = create_diff(s);
2504 break;
2505 case '>':
2506 case '.':
2507 if (s->log_view == NULL)
2508 break;
2509 ls = &s->log_view->state.log;
2511 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2512 ls->thread_args.commits_needed++;
2514 /* Display "loading..." in log view. */
2515 show_log_view(s->log_view);
2516 update_panels();
2517 doupdate();
2519 err = trigger_log_thread(1 /* load_all */,
2520 &ls->thread_args.commits_needed,
2521 &ls->thread_args.log_complete,
2522 &ls->thread_args.need_commits);
2523 if (err)
2524 break;
2526 err = input_log_view(NULL, NULL, NULL, s->log_view,
2527 KEY_DOWN);
2528 if (err)
2529 break;
2531 entry = TAILQ_NEXT(ls->selected_entry, entry);
2532 if (entry == NULL)
2533 break;
2535 err = set_selected_commit(s, entry);
2536 if (err)
2537 break;
2539 s->first_displayed_line = 1;
2540 s->last_displayed_line = view->nlines;
2542 diff_view_indicate_progress(view);
2543 err = create_diff(s);
2544 break;
2545 default:
2546 break;
2549 return err;
2552 static const struct got_error *
2553 cmd_diff(int argc, char *argv[])
2555 const struct got_error *error = NULL;
2556 struct got_repository *repo = NULL;
2557 struct got_reflist_head refs;
2558 struct got_object_id *id1 = NULL, *id2 = NULL;
2559 char *repo_path = NULL;
2560 char *id_str1 = NULL, *id_str2 = NULL;
2561 int ch;
2562 struct tog_view *view;
2564 SIMPLEQ_INIT(&refs);
2566 #ifndef PROFILE
2567 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2568 NULL) == -1)
2569 err(1, "pledge");
2570 #endif
2572 while ((ch = getopt(argc, argv, "")) != -1) {
2573 switch (ch) {
2574 default:
2575 usage_diff();
2576 /* NOTREACHED */
2580 argc -= optind;
2581 argv += optind;
2583 if (argc == 0) {
2584 usage_diff(); /* TODO show local worktree changes */
2585 } else if (argc == 2) {
2586 repo_path = getcwd(NULL, 0);
2587 if (repo_path == NULL)
2588 return got_error_from_errno("getcwd");
2589 id_str1 = argv[0];
2590 id_str2 = argv[1];
2591 } else if (argc == 3) {
2592 repo_path = realpath(argv[0], NULL);
2593 if (repo_path == NULL)
2594 return got_error_from_errno2("realpath", argv[0]);
2595 id_str1 = argv[1];
2596 id_str2 = argv[2];
2597 } else
2598 usage_diff();
2600 init_curses();
2602 error = got_repo_open(&repo, repo_path);
2603 if (error)
2604 goto done;
2606 error = apply_unveil(got_repo_get_path(repo), NULL);
2607 if (error)
2608 goto done;
2610 error = got_object_resolve_id_str(&id1, repo, id_str1);
2611 if (error)
2612 goto done;
2614 error = got_object_resolve_id_str(&id2, repo, id_str2);
2615 if (error)
2616 goto done;
2618 error = got_ref_list(&refs, repo);
2619 if (error)
2620 goto done;
2622 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2623 if (view == NULL) {
2624 error = got_error_from_errno("view_open");
2625 goto done;
2627 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2628 if (error)
2629 goto done;
2630 error = view_loop(view);
2631 done:
2632 free(repo_path);
2633 got_repo_close(repo);
2634 got_ref_list_free(&refs);
2635 return error;
2638 __dead static void
2639 usage_blame(void)
2641 endwin();
2642 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2643 getprogname());
2644 exit(1);
2647 struct tog_blame_line {
2648 int annotated;
2649 struct got_object_id *id;
2652 static const struct got_error *
2653 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2654 const char *path, struct tog_blame_line *lines, int nlines,
2655 int blame_complete, int selected_line, int *first_displayed_line,
2656 int *last_displayed_line, int *eof, int max_lines)
2658 const struct got_error *err;
2659 int lineno = 0, nprinted = 0;
2660 char *line;
2661 size_t len;
2662 wchar_t *wline;
2663 int width, wlimit;
2664 struct tog_blame_line *blame_line;
2665 struct got_object_id *prev_id = NULL;
2666 char *id_str;
2668 err = got_object_id_str(&id_str, id);
2669 if (err)
2670 return err;
2672 rewind(f);
2673 werase(view->window);
2675 if (asprintf(&line, "commit %s", id_str) == -1) {
2676 err = got_error_from_errno("asprintf");
2677 free(id_str);
2678 return err;
2681 err = format_line(&wline, &width, line, view->ncols);
2682 free(line);
2683 line = NULL;
2684 if (view_needs_focus_indication(view))
2685 wstandout(view->window);
2686 waddwstr(view->window, wline);
2687 if (view_needs_focus_indication(view))
2688 wstandend(view->window);
2689 free(wline);
2690 wline = NULL;
2691 if (width < view->ncols)
2692 waddch(view->window, '\n');
2694 if (asprintf(&line, "[%d/%d] %s%s",
2695 *first_displayed_line - 1 + selected_line, nlines,
2696 blame_complete ? "" : "annotating... ", path) == -1) {
2697 free(id_str);
2698 return got_error_from_errno("asprintf");
2700 free(id_str);
2701 err = format_line(&wline, &width, line, view->ncols);
2702 free(line);
2703 line = NULL;
2704 if (err)
2705 return err;
2706 waddwstr(view->window, wline);
2707 free(wline);
2708 wline = NULL;
2709 if (width < view->ncols)
2710 waddch(view->window, '\n');
2712 *eof = 0;
2713 while (nprinted < max_lines - 2) {
2714 line = parse_next_line(f, &len);
2715 if (line == NULL) {
2716 *eof = 1;
2717 break;
2719 if (++lineno < *first_displayed_line) {
2720 free(line);
2721 continue;
2724 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2725 err = format_line(&wline, &width, line, wlimit);
2726 if (err) {
2727 free(line);
2728 return err;
2731 if (view->focussed && nprinted == selected_line - 1)
2732 wstandout(view->window);
2734 blame_line = &lines[lineno - 1];
2735 if (blame_line->annotated && prev_id &&
2736 got_object_id_cmp(prev_id, blame_line->id) == 0)
2737 waddstr(view->window, " ");
2738 else if (blame_line->annotated) {
2739 char *id_str;
2740 err = got_object_id_str(&id_str, blame_line->id);
2741 if (err) {
2742 free(line);
2743 free(wline);
2744 return err;
2746 wprintw(view->window, "%.8s ", id_str);
2747 free(id_str);
2748 prev_id = blame_line->id;
2749 } else {
2750 waddstr(view->window, "........ ");
2751 prev_id = NULL;
2754 waddwstr(view->window, wline);
2755 while (width < wlimit) {
2756 waddch(view->window, ' ');
2757 width++;
2759 if (view->focussed && nprinted == selected_line - 1)
2760 wstandend(view->window);
2761 if (++nprinted == 1)
2762 *first_displayed_line = lineno;
2763 free(line);
2764 free(wline);
2765 wline = NULL;
2767 *last_displayed_line = lineno;
2769 view_vborder(view);
2771 return NULL;
2774 static const struct got_error *
2775 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2777 const struct got_error *err = NULL;
2778 struct tog_blame_cb_args *a = arg;
2779 struct tog_blame_line *line;
2780 int errcode;
2782 if (nlines != a->nlines ||
2783 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2784 return got_error(GOT_ERR_RANGE);
2786 errcode = pthread_mutex_lock(&tog_mutex);
2787 if (errcode)
2788 return got_error_set_errno(errcode, "pthread_mutex_lock");
2790 if (*a->quit) { /* user has quit the blame view */
2791 err = got_error(GOT_ERR_ITER_COMPLETED);
2792 goto done;
2795 if (lineno == -1)
2796 goto done; /* no change in this commit */
2798 line = &a->lines[lineno - 1];
2799 if (line->annotated)
2800 goto done;
2802 line->id = got_object_id_dup(id);
2803 if (line->id == NULL) {
2804 err = got_error_from_errno("got_object_id_dup");
2805 goto done;
2807 line->annotated = 1;
2808 done:
2809 errcode = pthread_mutex_unlock(&tog_mutex);
2810 if (errcode)
2811 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2812 return err;
2815 static void *
2816 blame_thread(void *arg)
2818 const struct got_error *err;
2819 struct tog_blame_thread_args *ta = arg;
2820 struct tog_blame_cb_args *a = ta->cb_args;
2821 int errcode;
2823 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2824 blame_cb, ta->cb_args);
2826 errcode = pthread_mutex_lock(&tog_mutex);
2827 if (errcode)
2828 return (void *)got_error_set_errno(errcode,
2829 "pthread_mutex_lock");
2831 got_repo_close(ta->repo);
2832 ta->repo = NULL;
2833 *ta->complete = 1;
2835 errcode = pthread_mutex_unlock(&tog_mutex);
2836 if (errcode && err == NULL)
2837 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2839 return (void *)err;
2842 static struct got_object_id *
2843 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2844 int selected_line)
2846 struct tog_blame_line *line;
2848 line = &lines[first_displayed_line - 1 + selected_line - 1];
2849 if (!line->annotated)
2850 return NULL;
2852 return line->id;
2855 static const struct got_error *
2856 stop_blame(struct tog_blame *blame)
2858 const struct got_error *err = NULL;
2859 int i;
2861 if (blame->thread) {
2862 int errcode;
2863 errcode = pthread_mutex_unlock(&tog_mutex);
2864 if (errcode)
2865 return got_error_set_errno(errcode,
2866 "pthread_mutex_unlock");
2867 errcode = pthread_join(blame->thread, (void **)&err);
2868 if (errcode)
2869 return got_error_set_errno(errcode, "pthread_join");
2870 errcode = pthread_mutex_lock(&tog_mutex);
2871 if (errcode)
2872 return got_error_set_errno(errcode,
2873 "pthread_mutex_lock");
2874 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2875 err = NULL;
2876 blame->thread = NULL;
2878 if (blame->thread_args.repo) {
2879 got_repo_close(blame->thread_args.repo);
2880 blame->thread_args.repo = NULL;
2882 if (blame->f) {
2883 if (fclose(blame->f) != 0 && err == NULL)
2884 err = got_error_from_errno("fclose");
2885 blame->f = NULL;
2887 if (blame->lines) {
2888 for (i = 0; i < blame->nlines; i++)
2889 free(blame->lines[i].id);
2890 free(blame->lines);
2891 blame->lines = NULL;
2893 free(blame->cb_args.commit_id);
2894 blame->cb_args.commit_id = NULL;
2896 return err;
2899 static const struct got_error *
2900 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2901 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2902 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2903 struct got_repository *repo)
2905 const struct got_error *err = NULL;
2906 struct got_blob_object *blob = NULL;
2907 struct got_repository *thread_repo = NULL;
2908 struct got_object_id *obj_id = NULL;
2909 int obj_type;
2911 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2912 if (err)
2913 return err;
2914 if (obj_id == NULL)
2915 return got_error(GOT_ERR_NO_OBJ);
2917 err = got_object_get_type(&obj_type, repo, obj_id);
2918 if (err)
2919 goto done;
2921 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2922 err = got_error(GOT_ERR_OBJ_TYPE);
2923 goto done;
2926 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2927 if (err)
2928 goto done;
2929 blame->f = got_opentemp();
2930 if (blame->f == NULL) {
2931 err = got_error_from_errno("got_opentemp");
2932 goto done;
2934 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2935 blame->f, blob);
2936 if (err)
2937 goto done;
2939 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2940 if (blame->lines == NULL) {
2941 err = got_error_from_errno("calloc");
2942 goto done;
2945 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2946 if (err)
2947 goto done;
2949 blame->cb_args.view = view;
2950 blame->cb_args.lines = blame->lines;
2951 blame->cb_args.nlines = blame->nlines;
2952 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2953 if (blame->cb_args.commit_id == NULL) {
2954 err = got_error_from_errno("got_object_id_dup");
2955 goto done;
2957 blame->cb_args.quit = done;
2959 blame->thread_args.path = path;
2960 blame->thread_args.repo = thread_repo;
2961 blame->thread_args.cb_args = &blame->cb_args;
2962 blame->thread_args.complete = blame_complete;
2963 *blame_complete = 0;
2965 done:
2966 if (blob)
2967 got_object_blob_close(blob);
2968 free(obj_id);
2969 if (err)
2970 stop_blame(blame);
2971 return err;
2974 static const struct got_error *
2975 open_blame_view(struct tog_view *view, char *path,
2976 struct got_object_id *commit_id, struct got_reflist_head *refs,
2977 struct got_repository *repo)
2979 const struct got_error *err = NULL;
2980 struct tog_blame_view_state *s = &view->state.blame;
2982 SIMPLEQ_INIT(&s->blamed_commits);
2984 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2985 if (err)
2986 return err;
2988 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2989 s->first_displayed_line = 1;
2990 s->last_displayed_line = view->nlines;
2991 s->selected_line = 1;
2992 s->blame_complete = 0;
2993 s->path = path;
2994 if (s->path == NULL)
2995 return got_error_from_errno("open_blame_view");
2996 s->repo = repo;
2997 s->refs = refs;
2998 s->commit_id = commit_id;
2999 memset(&s->blame, 0, sizeof(s->blame));
3001 view->show = show_blame_view;
3002 view->input = input_blame_view;
3003 view->close = close_blame_view;
3005 return run_blame(&s->blame, view, &s->blame_complete,
3006 &s->first_displayed_line, &s->last_displayed_line,
3007 &s->selected_line, &s->done, &s->eof, s->path,
3008 s->blamed_commit->id, s->repo);
3011 static const struct got_error *
3012 close_blame_view(struct tog_view *view)
3014 const struct got_error *err = NULL;
3015 struct tog_blame_view_state *s = &view->state.blame;
3017 if (s->blame.thread)
3018 err = stop_blame(&s->blame);
3020 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3021 struct got_object_qid *blamed_commit;
3022 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3023 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3024 got_object_qid_free(blamed_commit);
3027 free(s->path);
3029 return err;
3032 static const struct got_error *
3033 show_blame_view(struct tog_view *view)
3035 const struct got_error *err = NULL;
3036 struct tog_blame_view_state *s = &view->state.blame;
3037 int errcode;
3039 if (s->blame.thread == NULL) {
3040 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3041 &s->blame.thread_args);
3042 if (errcode)
3043 return got_error_set_errno(errcode, "pthread_create");
3046 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3047 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3048 s->selected_line, &s->first_displayed_line,
3049 &s->last_displayed_line, &s->eof, view->nlines);
3051 view_vborder(view);
3052 return err;
3055 static const struct got_error *
3056 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3057 struct tog_view **focus_view, struct tog_view *view, int ch)
3059 const struct got_error *err = NULL, *thread_err = NULL;
3060 struct tog_view *diff_view;
3061 struct tog_blame_view_state *s = &view->state.blame;
3062 int begin_x = 0;
3064 switch (ch) {
3065 case 'q':
3066 s->done = 1;
3067 break;
3068 case 'k':
3069 case KEY_UP:
3070 if (s->selected_line > 1)
3071 s->selected_line--;
3072 else if (s->selected_line == 1 &&
3073 s->first_displayed_line > 1)
3074 s->first_displayed_line--;
3075 break;
3076 case KEY_PPAGE:
3077 if (s->first_displayed_line == 1) {
3078 s->selected_line = 1;
3079 break;
3081 if (s->first_displayed_line > view->nlines - 2)
3082 s->first_displayed_line -=
3083 (view->nlines - 2);
3084 else
3085 s->first_displayed_line = 1;
3086 break;
3087 case 'j':
3088 case KEY_DOWN:
3089 if (s->selected_line < view->nlines - 2 &&
3090 s->first_displayed_line +
3091 s->selected_line <= s->blame.nlines)
3092 s->selected_line++;
3093 else if (s->last_displayed_line <
3094 s->blame.nlines)
3095 s->first_displayed_line++;
3096 break;
3097 case 'b':
3098 case 'p': {
3099 struct got_object_id *id = NULL;
3100 id = get_selected_commit_id(s->blame.lines,
3101 s->first_displayed_line, s->selected_line);
3102 if (id == NULL)
3103 break;
3104 if (ch == 'p') {
3105 struct got_commit_object *commit;
3106 struct got_object_qid *pid;
3107 struct got_object_id *blob_id = NULL;
3108 int obj_type;
3109 err = got_object_open_as_commit(&commit,
3110 s->repo, id);
3111 if (err)
3112 break;
3113 pid = SIMPLEQ_FIRST(
3114 got_object_commit_get_parent_ids(commit));
3115 if (pid == NULL) {
3116 got_object_commit_close(commit);
3117 break;
3119 /* Check if path history ends here. */
3120 err = got_object_id_by_path(&blob_id, s->repo,
3121 pid->id, s->path);
3122 if (err) {
3123 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3124 err = NULL;
3125 got_object_commit_close(commit);
3126 break;
3128 err = got_object_get_type(&obj_type, s->repo,
3129 blob_id);
3130 free(blob_id);
3131 /* Can't blame non-blob type objects. */
3132 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3133 got_object_commit_close(commit);
3134 break;
3136 err = got_object_qid_alloc(&s->blamed_commit,
3137 pid->id);
3138 got_object_commit_close(commit);
3139 } else {
3140 if (got_object_id_cmp(id,
3141 s->blamed_commit->id) == 0)
3142 break;
3143 err = got_object_qid_alloc(&s->blamed_commit,
3144 id);
3146 if (err)
3147 break;
3148 s->done = 1;
3149 thread_err = stop_blame(&s->blame);
3150 s->done = 0;
3151 if (thread_err)
3152 break;
3153 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3154 s->blamed_commit, entry);
3155 err = run_blame(&s->blame, view, &s->blame_complete,
3156 &s->first_displayed_line, &s->last_displayed_line,
3157 &s->selected_line, &s->done, &s->eof,
3158 s->path, s->blamed_commit->id, s->repo);
3159 if (err)
3160 break;
3161 break;
3163 case 'B': {
3164 struct got_object_qid *first;
3165 first = SIMPLEQ_FIRST(&s->blamed_commits);
3166 if (!got_object_id_cmp(first->id, s->commit_id))
3167 break;
3168 s->done = 1;
3169 thread_err = stop_blame(&s->blame);
3170 s->done = 0;
3171 if (thread_err)
3172 break;
3173 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3174 got_object_qid_free(s->blamed_commit);
3175 s->blamed_commit =
3176 SIMPLEQ_FIRST(&s->blamed_commits);
3177 err = run_blame(&s->blame, view, &s->blame_complete,
3178 &s->first_displayed_line, &s->last_displayed_line,
3179 &s->selected_line, &s->done, &s->eof, s->path,
3180 s->blamed_commit->id, s->repo);
3181 if (err)
3182 break;
3183 break;
3185 case KEY_ENTER:
3186 case '\r': {
3187 struct got_object_id *id = NULL;
3188 struct got_object_qid *pid;
3189 struct got_commit_object *commit = NULL;
3190 id = get_selected_commit_id(s->blame.lines,
3191 s->first_displayed_line, s->selected_line);
3192 if (id == NULL)
3193 break;
3194 err = got_object_open_as_commit(&commit, s->repo, id);
3195 if (err)
3196 break;
3197 pid = SIMPLEQ_FIRST(
3198 got_object_commit_get_parent_ids(commit));
3199 if (view_is_parent_view(view))
3200 begin_x = view_split_begin_x(view->begin_x);
3201 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3202 if (diff_view == NULL) {
3203 got_object_commit_close(commit);
3204 err = got_error_from_errno("view_open");
3205 break;
3207 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3208 id, NULL, s->refs, s->repo);
3209 got_object_commit_close(commit);
3210 if (err) {
3211 view_close(diff_view);
3212 break;
3214 if (view_is_parent_view(view)) {
3215 err = view_close_child(view);
3216 if (err)
3217 break;
3218 err = view_set_child(view, diff_view);
3219 if (err) {
3220 view_close(diff_view);
3221 break;
3223 *focus_view = diff_view;
3224 view->child_focussed = 1;
3225 } else
3226 *new_view = diff_view;
3227 if (err)
3228 break;
3229 break;
3231 case KEY_NPAGE:
3232 case ' ':
3233 if (s->last_displayed_line >= s->blame.nlines &&
3234 s->selected_line >= MIN(s->blame.nlines,
3235 view->nlines - 2)) {
3236 break;
3238 if (s->last_displayed_line >= s->blame.nlines &&
3239 s->selected_line < view->nlines - 2) {
3240 s->selected_line = MIN(s->blame.nlines,
3241 view->nlines - 2);
3242 break;
3244 if (s->last_displayed_line + view->nlines - 2
3245 <= s->blame.nlines)
3246 s->first_displayed_line +=
3247 view->nlines - 2;
3248 else
3249 s->first_displayed_line =
3250 s->blame.nlines -
3251 (view->nlines - 3);
3252 break;
3253 case KEY_RESIZE:
3254 if (s->selected_line > view->nlines - 2) {
3255 s->selected_line = MIN(s->blame.nlines,
3256 view->nlines - 2);
3258 break;
3259 default:
3260 break;
3262 return thread_err ? thread_err : err;
3265 static const struct got_error *
3266 cmd_blame(int argc, char *argv[])
3268 const struct got_error *error;
3269 struct got_repository *repo = NULL;
3270 struct got_reflist_head refs;
3271 struct got_worktree *worktree = NULL;
3272 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3273 struct got_object_id *commit_id = NULL;
3274 char *commit_id_str = NULL;
3275 int ch;
3276 struct tog_view *view;
3278 SIMPLEQ_INIT(&refs);
3280 #ifndef PROFILE
3281 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3282 NULL) == -1)
3283 err(1, "pledge");
3284 #endif
3286 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3287 switch (ch) {
3288 case 'c':
3289 commit_id_str = optarg;
3290 break;
3291 case 'r':
3292 repo_path = realpath(optarg, NULL);
3293 if (repo_path == NULL)
3294 err(1, "-r option");
3295 break;
3296 default:
3297 usage_blame();
3298 /* NOTREACHED */
3302 argc -= optind;
3303 argv += optind;
3305 if (argc == 1)
3306 path = argv[0];
3307 else
3308 usage_blame();
3310 cwd = getcwd(NULL, 0);
3311 if (cwd == NULL) {
3312 error = got_error_from_errno("getcwd");
3313 goto done;
3315 if (repo_path == NULL) {
3316 error = got_worktree_open(&worktree, cwd);
3317 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3318 goto done;
3319 else
3320 error = NULL;
3321 if (worktree) {
3322 repo_path =
3323 strdup(got_worktree_get_repo_path(worktree));
3324 if (repo_path == NULL)
3325 error = got_error_from_errno("strdup");
3326 if (error)
3327 goto done;
3328 } else {
3329 repo_path = strdup(cwd);
3330 if (repo_path == NULL) {
3331 error = got_error_from_errno("strdup");
3332 goto done;
3337 init_curses();
3339 error = got_repo_open(&repo, repo_path);
3340 if (error != NULL)
3341 goto done;
3343 error = apply_unveil(got_repo_get_path(repo), NULL);
3344 if (error)
3345 goto done;
3347 if (worktree) {
3348 const char *prefix = got_worktree_get_path_prefix(worktree);
3349 char *p, *worktree_subdir = cwd +
3350 strlen(got_worktree_get_root_path(worktree));
3351 if (asprintf(&p, "%s%s%s%s%s",
3352 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3353 worktree_subdir, worktree_subdir[0] ? "/" : "",
3354 path) == -1) {
3355 error = got_error_from_errno("asprintf");
3356 goto done;
3358 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3359 free(p);
3360 } else {
3361 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3363 if (error)
3364 goto done;
3366 if (commit_id_str == NULL) {
3367 struct got_reference *head_ref;
3368 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3369 if (error != NULL)
3370 goto done;
3371 error = got_ref_resolve(&commit_id, repo, head_ref);
3372 got_ref_close(head_ref);
3373 } else {
3374 error = got_object_resolve_id_str(&commit_id, repo,
3375 commit_id_str);
3377 if (error != NULL)
3378 goto done;
3380 error = got_ref_list(&refs, repo);
3381 if (error)
3382 goto done;
3384 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3385 if (view == NULL) {
3386 error = got_error_from_errno("view_open");
3387 goto done;
3389 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3390 if (error)
3391 goto done;
3392 error = view_loop(view);
3393 done:
3394 free(repo_path);
3395 free(cwd);
3396 free(commit_id);
3397 if (worktree)
3398 got_worktree_close(worktree);
3399 if (repo)
3400 got_repo_close(repo);
3401 got_ref_list_free(&refs);
3402 return error;
3405 static const struct got_error *
3406 draw_tree_entries(struct tog_view *view,
3407 struct got_tree_entry **first_displayed_entry,
3408 struct got_tree_entry **last_displayed_entry,
3409 struct got_tree_entry **selected_entry, int *ndisplayed,
3410 const char *label, int show_ids, const char *parent_path,
3411 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3413 const struct got_error *err = NULL;
3414 struct got_tree_entry *te;
3415 wchar_t *wline;
3416 int width, n;
3418 *ndisplayed = 0;
3420 werase(view->window);
3422 if (limit == 0)
3423 return NULL;
3425 err = format_line(&wline, &width, label, view->ncols);
3426 if (err)
3427 return err;
3428 if (view_needs_focus_indication(view))
3429 wstandout(view->window);
3430 waddwstr(view->window, wline);
3431 if (view_needs_focus_indication(view))
3432 wstandend(view->window);
3433 free(wline);
3434 wline = NULL;
3435 if (width < view->ncols)
3436 waddch(view->window, '\n');
3437 if (--limit <= 0)
3438 return NULL;
3439 err = format_line(&wline, &width, parent_path, view->ncols);
3440 if (err)
3441 return err;
3442 waddwstr(view->window, wline);
3443 free(wline);
3444 wline = NULL;
3445 if (width < view->ncols)
3446 waddch(view->window, '\n');
3447 if (--limit <= 0)
3448 return NULL;
3449 waddch(view->window, '\n');
3450 if (--limit <= 0)
3451 return NULL;
3453 te = SIMPLEQ_FIRST(&entries->head);
3454 if (*first_displayed_entry == NULL) {
3455 if (selected == 0) {
3456 if (view->focussed)
3457 wstandout(view->window);
3458 *selected_entry = NULL;
3460 waddstr(view->window, " ..\n"); /* parent directory */
3461 if (selected == 0 && view->focussed)
3462 wstandend(view->window);
3463 (*ndisplayed)++;
3464 if (--limit <= 0)
3465 return NULL;
3466 n = 1;
3467 } else {
3468 n = 0;
3469 while (te != *first_displayed_entry)
3470 te = SIMPLEQ_NEXT(te, entry);
3473 while (te) {
3474 char *line = NULL, *id_str = NULL;
3476 if (show_ids) {
3477 err = got_object_id_str(&id_str, te->id);
3478 if (err)
3479 return got_error_from_errno(
3480 "got_object_id_str");
3482 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3483 te->name, S_ISDIR(te->mode) ? "/" :
3484 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3485 free(id_str);
3486 return got_error_from_errno("asprintf");
3488 free(id_str);
3489 err = format_line(&wline, &width, line, view->ncols);
3490 if (err) {
3491 free(line);
3492 break;
3494 if (n == selected) {
3495 if (view->focussed)
3496 wstandout(view->window);
3497 *selected_entry = te;
3499 waddwstr(view->window, wline);
3500 if (width < view->ncols)
3501 waddch(view->window, '\n');
3502 if (n == selected && view->focussed)
3503 wstandend(view->window);
3504 free(line);
3505 free(wline);
3506 wline = NULL;
3507 n++;
3508 (*ndisplayed)++;
3509 *last_displayed_entry = te;
3510 if (--limit <= 0)
3511 break;
3512 te = SIMPLEQ_NEXT(te, entry);
3515 return err;
3518 static void
3519 tree_scroll_up(struct tog_view *view,
3520 struct got_tree_entry **first_displayed_entry, int maxscroll,
3521 const struct got_tree_entries *entries, int isroot)
3523 struct got_tree_entry *te, *prev;
3524 int i;
3526 if (*first_displayed_entry == NULL)
3527 return;
3529 te = SIMPLEQ_FIRST(&entries->head);
3530 if (*first_displayed_entry == te) {
3531 if (!isroot)
3532 *first_displayed_entry = NULL;
3533 return;
3536 /* XXX this is stupid... switch to TAILQ? */
3537 for (i = 0; i < maxscroll; i++) {
3538 while (te != *first_displayed_entry) {
3539 prev = te;
3540 te = SIMPLEQ_NEXT(te, entry);
3542 *first_displayed_entry = prev;
3543 te = SIMPLEQ_FIRST(&entries->head);
3545 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3546 *first_displayed_entry = NULL;
3549 static int
3550 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3551 struct got_tree_entry *last_displayed_entry,
3552 const struct got_tree_entries *entries)
3554 struct got_tree_entry *next, *last;
3555 int n = 0;
3557 if (*first_displayed_entry)
3558 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3559 else
3560 next = SIMPLEQ_FIRST(&entries->head);
3561 last = last_displayed_entry;
3562 while (next && last && n++ < maxscroll) {
3563 last = SIMPLEQ_NEXT(last, entry);
3564 if (last) {
3565 *first_displayed_entry = next;
3566 next = SIMPLEQ_NEXT(next, entry);
3569 return n;
3572 static const struct got_error *
3573 tree_entry_path(char **path, struct tog_parent_trees *parents,
3574 struct got_tree_entry *te)
3576 const struct got_error *err = NULL;
3577 struct tog_parent_tree *pt;
3578 size_t len = 2; /* for leading slash and NUL */
3580 TAILQ_FOREACH(pt, parents, entry)
3581 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3582 if (te)
3583 len += strlen(te->name);
3585 *path = calloc(1, len);
3586 if (path == NULL)
3587 return got_error_from_errno("calloc");
3589 (*path)[0] = '/';
3590 pt = TAILQ_LAST(parents, tog_parent_trees);
3591 while (pt) {
3592 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3593 err = got_error(GOT_ERR_NO_SPACE);
3594 goto done;
3596 if (strlcat(*path, "/", len) >= len) {
3597 err = got_error(GOT_ERR_NO_SPACE);
3598 goto done;
3600 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3602 if (te) {
3603 if (strlcat(*path, te->name, len) >= len) {
3604 err = got_error(GOT_ERR_NO_SPACE);
3605 goto done;
3608 done:
3609 if (err) {
3610 free(*path);
3611 *path = NULL;
3613 return err;
3616 static const struct got_error *
3617 blame_tree_entry(struct tog_view **new_view, int begin_x,
3618 struct got_tree_entry *te, struct tog_parent_trees *parents,
3619 struct got_object_id *commit_id, struct got_reflist_head *refs,
3620 struct got_repository *repo)
3622 const struct got_error *err = NULL;
3623 char *path;
3624 struct tog_view *blame_view;
3626 err = tree_entry_path(&path, parents, te);
3627 if (err)
3628 return err;
3630 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3631 if (blame_view == NULL)
3632 return got_error_from_errno("view_open");
3634 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3635 if (err) {
3636 view_close(blame_view);
3637 free(path);
3638 } else
3639 *new_view = blame_view;
3640 return err;
3643 static const struct got_error *
3644 log_tree_entry(struct tog_view **new_view, int begin_x,
3645 struct got_tree_entry *te, struct tog_parent_trees *parents,
3646 struct got_object_id *commit_id, struct got_reflist_head *refs,
3647 struct got_repository *repo)
3649 struct tog_view *log_view;
3650 const struct got_error *err = NULL;
3651 char *path;
3653 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3654 if (log_view == NULL)
3655 return got_error_from_errno("view_open");
3657 err = tree_entry_path(&path, parents, te);
3658 if (err)
3659 return err;
3661 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3662 if (err)
3663 view_close(log_view);
3664 else
3665 *new_view = log_view;
3666 free(path);
3667 return err;
3670 static const struct got_error *
3671 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3672 struct got_object_id *commit_id, struct got_reflist_head *refs,
3673 struct got_repository *repo)
3675 const struct got_error *err = NULL;
3676 char *commit_id_str = NULL;
3677 struct tog_tree_view_state *s = &view->state.tree;
3679 TAILQ_INIT(&s->parents);
3681 err = got_object_id_str(&commit_id_str, commit_id);
3682 if (err != NULL)
3683 goto done;
3685 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3686 err = got_error_from_errno("asprintf");
3687 goto done;
3690 s->root = s->tree = root;
3691 s->entries = got_object_tree_get_entries(root);
3692 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3693 s->commit_id = got_object_id_dup(commit_id);
3694 if (s->commit_id == NULL) {
3695 err = got_error_from_errno("got_object_id_dup");
3696 goto done;
3698 s->refs = refs;
3699 s->repo = repo;
3701 view->show = show_tree_view;
3702 view->input = input_tree_view;
3703 view->close = close_tree_view;
3704 done:
3705 free(commit_id_str);
3706 if (err) {
3707 free(s->tree_label);
3708 s->tree_label = NULL;
3710 return err;
3713 static const struct got_error *
3714 close_tree_view(struct tog_view *view)
3716 struct tog_tree_view_state *s = &view->state.tree;
3718 free(s->tree_label);
3719 s->tree_label = NULL;
3720 free(s->commit_id);
3721 s->commit_id = NULL;
3722 while (!TAILQ_EMPTY(&s->parents)) {
3723 struct tog_parent_tree *parent;
3724 parent = TAILQ_FIRST(&s->parents);
3725 TAILQ_REMOVE(&s->parents, parent, entry);
3726 free(parent);
3729 if (s->tree != s->root)
3730 got_object_tree_close(s->tree);
3731 got_object_tree_close(s->root);
3733 return NULL;
3736 static const struct got_error *
3737 show_tree_view(struct tog_view *view)
3739 const struct got_error *err = NULL;
3740 struct tog_tree_view_state *s = &view->state.tree;
3741 char *parent_path;
3743 err = tree_entry_path(&parent_path, &s->parents, NULL);
3744 if (err)
3745 return err;
3747 err = draw_tree_entries(view, &s->first_displayed_entry,
3748 &s->last_displayed_entry, &s->selected_entry,
3749 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3750 s->entries, s->selected, view->nlines, s->tree == s->root);
3751 free(parent_path);
3753 view_vborder(view);
3754 return err;
3757 static const struct got_error *
3758 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3759 struct tog_view **focus_view, struct tog_view *view, int ch)
3761 const struct got_error *err = NULL;
3762 struct tog_tree_view_state *s = &view->state.tree;
3763 struct tog_view *log_view;
3764 int begin_x = 0, nscrolled;
3766 switch (ch) {
3767 case 'i':
3768 s->show_ids = !s->show_ids;
3769 break;
3770 case 'l':
3771 if (!s->selected_entry)
3772 break;
3773 if (view_is_parent_view(view))
3774 begin_x = view_split_begin_x(view->begin_x);
3775 err = log_tree_entry(&log_view, begin_x,
3776 s->selected_entry, &s->parents,
3777 s->commit_id, s->refs, s->repo);
3778 if (view_is_parent_view(view)) {
3779 err = view_close_child(view);
3780 if (err)
3781 return err;
3782 err = view_set_child(view, log_view);
3783 if (err) {
3784 view_close(log_view);
3785 break;
3787 *focus_view = log_view;
3788 view->child_focussed = 1;
3789 } else
3790 *new_view = log_view;
3791 break;
3792 case 'k':
3793 case KEY_UP:
3794 if (s->selected > 0) {
3795 s->selected--;
3796 if (s->selected == 0)
3797 break;
3799 if (s->selected > 0)
3800 break;
3801 tree_scroll_up(view, &s->first_displayed_entry, 1,
3802 s->entries, s->tree == s->root);
3803 break;
3804 case KEY_PPAGE:
3805 tree_scroll_up(view, &s->first_displayed_entry,
3806 MAX(0, view->nlines - 4 - s->selected), s->entries,
3807 s->tree == s->root);
3808 s->selected = 0;
3809 if (SIMPLEQ_FIRST(&s->entries->head) ==
3810 s->first_displayed_entry && s->tree != s->root)
3811 s->first_displayed_entry = NULL;
3812 break;
3813 case 'j':
3814 case KEY_DOWN:
3815 if (s->selected < s->ndisplayed - 1) {
3816 s->selected++;
3817 break;
3819 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3820 /* can't scroll any further */
3821 break;
3822 tree_scroll_down(&s->first_displayed_entry, 1,
3823 s->last_displayed_entry, s->entries);
3824 break;
3825 case KEY_NPAGE:
3826 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3827 == NULL) {
3828 /* can't scroll any further; move cursor down */
3829 if (s->selected < s->ndisplayed - 1)
3830 s->selected = s->ndisplayed - 1;
3831 break;
3833 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3834 view->nlines, s->last_displayed_entry, s->entries);
3835 if (nscrolled < view->nlines) {
3836 int ndisplayed = 0;
3837 struct got_tree_entry *te;
3838 te = s->first_displayed_entry;
3839 do {
3840 ndisplayed++;
3841 te = SIMPLEQ_NEXT(te, entry);
3842 } while (te);
3843 s->selected = ndisplayed - 1;
3845 break;
3846 case KEY_ENTER:
3847 case '\r':
3848 case KEY_BACKSPACE:
3849 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3850 struct tog_parent_tree *parent;
3851 /* user selected '..' */
3852 if (s->tree == s->root)
3853 break;
3854 parent = TAILQ_FIRST(&s->parents);
3855 TAILQ_REMOVE(&s->parents, parent,
3856 entry);
3857 got_object_tree_close(s->tree);
3858 s->tree = parent->tree;
3859 s->entries =
3860 got_object_tree_get_entries(s->tree);
3861 s->first_displayed_entry =
3862 parent->first_displayed_entry;
3863 s->selected_entry =
3864 parent->selected_entry;
3865 s->selected = parent->selected;
3866 free(parent);
3867 } else if (S_ISDIR(s->selected_entry->mode)) {
3868 struct got_tree_object *subtree;
3869 err = got_object_open_as_tree(&subtree,
3870 s->repo, s->selected_entry->id);
3871 if (err)
3872 break;
3873 err = tree_view_visit_subtree(subtree, s);
3874 if (err) {
3875 got_object_tree_close(subtree);
3876 break;
3878 } else if (S_ISREG(s->selected_entry->mode)) {
3879 struct tog_view *blame_view;
3880 int begin_x = view_is_parent_view(view) ?
3881 view_split_begin_x(view->begin_x) : 0;
3883 err = blame_tree_entry(&blame_view, begin_x,
3884 s->selected_entry, &s->parents,
3885 s->commit_id, s->refs, s->repo);
3886 if (err)
3887 break;
3888 if (view_is_parent_view(view)) {
3889 err = view_close_child(view);
3890 if (err)
3891 return err;
3892 err = view_set_child(view, blame_view);
3893 if (err) {
3894 view_close(blame_view);
3895 break;
3897 *focus_view = blame_view;
3898 view->child_focussed = 1;
3899 } else
3900 *new_view = blame_view;
3902 break;
3903 case KEY_RESIZE:
3904 if (s->selected > view->nlines)
3905 s->selected = s->ndisplayed - 1;
3906 break;
3907 default:
3908 break;
3911 return err;
3914 __dead static void
3915 usage_tree(void)
3917 endwin();
3918 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3919 getprogname());
3920 exit(1);
3923 static const struct got_error *
3924 cmd_tree(int argc, char *argv[])
3926 const struct got_error *error;
3927 struct got_repository *repo = NULL;
3928 struct got_reflist_head refs;
3929 char *repo_path = NULL;
3930 struct got_object_id *commit_id = NULL;
3931 char *commit_id_arg = NULL;
3932 struct got_commit_object *commit = NULL;
3933 struct got_tree_object *tree = NULL;
3934 int ch;
3935 struct tog_view *view;
3937 SIMPLEQ_INIT(&refs);
3939 #ifndef PROFILE
3940 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3941 NULL) == -1)
3942 err(1, "pledge");
3943 #endif
3945 while ((ch = getopt(argc, argv, "c:")) != -1) {
3946 switch (ch) {
3947 case 'c':
3948 commit_id_arg = optarg;
3949 break;
3950 default:
3951 usage_tree();
3952 /* NOTREACHED */
3956 argc -= optind;
3957 argv += optind;
3959 if (argc == 0) {
3960 struct got_worktree *worktree;
3961 char *cwd = getcwd(NULL, 0);
3962 if (cwd == NULL)
3963 return got_error_from_errno("getcwd");
3964 error = got_worktree_open(&worktree, cwd);
3965 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3966 goto done;
3967 if (worktree) {
3968 free(cwd);
3969 repo_path =
3970 strdup(got_worktree_get_repo_path(worktree));
3971 got_worktree_close(worktree);
3972 } else
3973 repo_path = cwd;
3974 if (repo_path == NULL) {
3975 error = got_error_from_errno("strdup");
3976 goto done;
3978 } else if (argc == 1) {
3979 repo_path = realpath(argv[0], NULL);
3980 if (repo_path == NULL)
3981 return got_error_from_errno2("realpath", argv[0]);
3982 } else
3983 usage_log();
3985 init_curses();
3987 error = got_repo_open(&repo, repo_path);
3988 if (error != NULL)
3989 goto done;
3991 error = apply_unveil(got_repo_get_path(repo), NULL);
3992 if (error)
3993 goto done;
3995 if (commit_id_arg == NULL)
3996 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
3997 else
3998 error = got_object_resolve_id_str(&commit_id, repo,
3999 commit_id_arg);
4000 if (error != NULL)
4001 goto done;
4003 error = got_object_open_as_commit(&commit, repo, commit_id);
4004 if (error != NULL)
4005 goto done;
4007 error = got_object_open_as_tree(&tree, repo,
4008 got_object_commit_get_tree_id(commit));
4009 if (error != NULL)
4010 goto done;
4012 error = got_ref_list(&refs, repo);
4013 if (error)
4014 goto done;
4016 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4017 if (view == NULL) {
4018 error = got_error_from_errno("view_open");
4019 goto done;
4021 error = open_tree_view(view, tree, commit_id, &refs, repo);
4022 if (error)
4023 goto done;
4024 error = view_loop(view);
4025 done:
4026 free(repo_path);
4027 free(commit_id);
4028 if (commit)
4029 got_object_commit_close(commit);
4030 if (tree)
4031 got_object_tree_close(tree);
4032 if (repo)
4033 got_repo_close(repo);
4034 got_ref_list_free(&refs);
4035 return error;
4038 __dead static void
4039 usage(void)
4041 int i;
4043 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4044 "Available commands:\n", getprogname());
4045 for (i = 0; i < nitems(tog_commands); i++) {
4046 struct tog_cmd *cmd = &tog_commands[i];
4047 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4049 exit(1);
4052 static char **
4053 make_argv(const char *arg0, const char *arg1)
4055 char **argv;
4056 int argc = (arg1 == NULL ? 1 : 2);
4058 argv = calloc(argc, sizeof(char *));
4059 if (argv == NULL)
4060 err(1, "calloc");
4061 argv[0] = strdup(arg0);
4062 if (argv[0] == NULL)
4063 err(1, "calloc");
4064 if (arg1) {
4065 argv[1] = strdup(arg1);
4066 if (argv[1] == NULL)
4067 err(1, "calloc");
4070 return argv;
4073 int
4074 main(int argc, char *argv[])
4076 const struct got_error *error = NULL;
4077 struct tog_cmd *cmd = NULL;
4078 int ch, hflag = 0;
4079 char **cmd_argv = NULL;
4081 setlocale(LC_CTYPE, "");
4083 while ((ch = getopt(argc, argv, "h")) != -1) {
4084 switch (ch) {
4085 case 'h':
4086 hflag = 1;
4087 break;
4088 default:
4089 usage();
4090 /* NOTREACHED */
4094 argc -= optind;
4095 argv += optind;
4096 optind = 0;
4097 optreset = 1;
4099 if (argc == 0) {
4100 if (hflag)
4101 usage();
4102 /* Build an argument vector which runs a default command. */
4103 cmd = &tog_commands[0];
4104 cmd_argv = make_argv(cmd->name, NULL);
4105 argc = 1;
4106 } else {
4107 int i;
4109 /* Did the user specific a command? */
4110 for (i = 0; i < nitems(tog_commands); i++) {
4111 if (strncmp(tog_commands[i].name, argv[0],
4112 strlen(argv[0])) == 0) {
4113 cmd = &tog_commands[i];
4114 if (hflag)
4115 tog_commands[i].cmd_usage();
4116 break;
4119 if (cmd == NULL) {
4120 /* Did the user specify a repository? */
4121 char *repo_path = realpath(argv[0], NULL);
4122 if (repo_path) {
4123 struct got_repository *repo;
4124 error = got_repo_open(&repo, repo_path);
4125 if (error == NULL)
4126 got_repo_close(repo);
4127 } else
4128 error = got_error_from_errno2("realpath",
4129 argv[0]);
4130 if (error) {
4131 if (hflag) {
4132 fprintf(stderr, "%s: '%s' is not a "
4133 "known command\n", getprogname(),
4134 argv[0]);
4135 usage();
4137 fprintf(stderr, "%s: '%s' is neither a known "
4138 "command nor a path to a repository\n",
4139 getprogname(), argv[0]);
4140 free(repo_path);
4141 return 1;
4143 cmd = &tog_commands[0];
4144 cmd_argv = make_argv(cmd->name, repo_path);
4145 argc = 2;
4146 free(repo_path);
4150 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4151 if (error)
4152 goto done;
4153 done:
4154 endwin();
4155 free(cmd_argv);
4156 if (error)
4157 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4158 return 0;