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 /* Jump to this file's entry. */
1473 s->first_displayed_entry = s->selected_entry;
1474 s->selected = 0;
1475 break;
1478 slash = strchr(p, '/');
1479 if (slash)
1480 subpath = strndup(path, slash - path);
1481 else
1482 subpath = strdup(path);
1483 if (subpath == NULL) {
1484 err = got_error_from_errno("strdup");
1485 break;
1488 err = got_object_id_by_path(&tree_id, repo, entry->id,
1489 subpath);
1490 if (err)
1491 break;
1493 err = got_object_open_as_tree(&tree, repo, tree_id);
1494 free(tree_id);
1495 if (err)
1496 break;
1498 err = tree_view_visit_subtree(tree, s);
1499 if (err) {
1500 got_object_tree_close(tree);
1501 break;
1503 if (slash == NULL)
1504 break;
1505 free(subpath);
1506 subpath = NULL;
1507 p = slash;
1510 free(subpath);
1511 return err;
1514 static void *
1515 log_thread(void *arg)
1517 const struct got_error *err = NULL;
1518 int errcode = 0;
1519 struct tog_log_thread_args *a = arg;
1520 int done = 0;
1522 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1523 if (err)
1524 return (void *)err;
1526 while (!done && !err) {
1527 err = queue_commits(a->graph, a->commits, 1, a->repo,
1528 a->in_repo_path);
1529 if (err) {
1530 if (err->code != GOT_ERR_ITER_COMPLETED)
1531 return (void *)err;
1532 err = NULL;
1533 done = 1;
1534 } else if (a->commits_needed > 0)
1535 a->commits_needed--;
1537 errcode = pthread_mutex_lock(&tog_mutex);
1538 if (errcode) {
1539 err = got_error_set_errno(errcode,
1540 "pthread_mutex_lock");
1541 break;
1542 } else if (*a->quit)
1543 done = 1;
1544 else if (*a->first_displayed_entry == NULL) {
1545 *a->first_displayed_entry =
1546 TAILQ_FIRST(&a->commits->head);
1547 *a->selected_entry = *a->first_displayed_entry;
1550 if (done)
1551 a->commits_needed = 0;
1552 else if (a->commits_needed == 0) {
1553 errcode = pthread_cond_wait(&a->need_commits,
1554 &tog_mutex);
1555 if (errcode)
1556 err = got_error_set_errno(errcode,
1557 "pthread_cond_wait");
1560 errcode = pthread_mutex_unlock(&tog_mutex);
1561 if (errcode && err == NULL)
1562 err = got_error_set_errno(errcode,
1563 "pthread_mutex_unlock");
1565 a->log_complete = 1;
1566 return (void *)err;
1569 static const struct got_error *
1570 stop_log_thread(struct tog_log_view_state *s)
1572 const struct got_error *err = NULL;
1573 int errcode;
1575 if (s->thread) {
1576 s->quit = 1;
1577 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1578 if (errcode)
1579 return got_error_set_errno(errcode,
1580 "pthread_cond_signal");
1581 errcode = pthread_mutex_unlock(&tog_mutex);
1582 if (errcode)
1583 return got_error_set_errno(errcode,
1584 "pthread_mutex_unlock");
1585 errcode = pthread_join(s->thread, (void **)&err);
1586 if (errcode)
1587 return got_error_set_errno(errcode, "pthread_join");
1588 errcode = pthread_mutex_lock(&tog_mutex);
1589 if (errcode)
1590 return got_error_set_errno(errcode,
1591 "pthread_mutex_lock");
1592 s->thread = NULL;
1595 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1596 if (errcode && err == NULL)
1597 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1599 if (s->thread_args.repo) {
1600 got_repo_close(s->thread_args.repo);
1601 s->thread_args.repo = NULL;
1604 if (s->thread_args.graph) {
1605 got_commit_graph_close(s->thread_args.graph);
1606 s->thread_args.graph = NULL;
1609 return err;
1612 static const struct got_error *
1613 close_log_view(struct tog_view *view)
1615 const struct got_error *err = NULL;
1616 struct tog_log_view_state *s = &view->state.log;
1618 err = stop_log_thread(s);
1619 free_commits(&s->commits);
1620 free(s->in_repo_path);
1621 s->in_repo_path = NULL;
1622 free(s->start_id);
1623 s->start_id = NULL;
1624 return err;
1627 static const struct got_error *
1628 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1629 struct got_reflist_head *refs, struct got_repository *repo,
1630 const char *path, int check_disk)
1632 const struct got_error *err = NULL;
1633 struct tog_log_view_state *s = &view->state.log;
1634 struct got_repository *thread_repo = NULL;
1635 struct got_commit_graph *thread_graph = NULL;
1636 int errcode;
1638 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1639 if (err != NULL)
1640 goto done;
1642 /* The commit queue only contains commits being displayed. */
1643 TAILQ_INIT(&s->commits.head);
1644 s->commits.ncommits = 0;
1646 s->refs = refs;
1647 s->repo = repo;
1648 s->start_id = got_object_id_dup(start_id);
1649 if (s->start_id == NULL) {
1650 err = got_error_from_errno("got_object_id_dup");
1651 goto done;
1654 view->show = show_log_view;
1655 view->input = input_log_view;
1656 view->close = close_log_view;
1658 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1659 if (err)
1660 goto done;
1661 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1662 0, thread_repo);
1663 if (err)
1664 goto done;
1666 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1667 if (errcode) {
1668 err = got_error_set_errno(errcode, "pthread_cond_init");
1669 goto done;
1672 s->thread_args.commits_needed = view->nlines;
1673 s->thread_args.graph = thread_graph;
1674 s->thread_args.commits = &s->commits;
1675 s->thread_args.in_repo_path = s->in_repo_path;
1676 s->thread_args.start_id = s->start_id;
1677 s->thread_args.repo = thread_repo;
1678 s->thread_args.log_complete = 0;
1679 s->thread_args.quit = &s->quit;
1680 s->thread_args.view = view;
1681 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1682 s->thread_args.selected_entry = &s->selected_entry;
1683 done:
1684 if (err)
1685 close_log_view(view);
1686 return err;
1689 static const struct got_error *
1690 show_log_view(struct tog_view *view)
1692 struct tog_log_view_state *s = &view->state.log;
1694 if (s->thread == NULL) {
1695 int errcode = pthread_create(&s->thread, NULL, log_thread,
1696 &s->thread_args);
1697 if (errcode)
1698 return got_error_set_errno(errcode, "pthread_create");
1701 return draw_commits(view, &s->last_displayed_entry,
1702 &s->selected_entry, s->first_displayed_entry,
1703 &s->commits, s->selected, view->nlines, s->refs,
1704 s->in_repo_path, s->thread_args.commits_needed);
1707 static const struct got_error *
1708 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1709 struct tog_view **focus_view, struct tog_view *view, int ch)
1711 const struct got_error *err = NULL;
1712 struct tog_log_view_state *s = &view->state.log;
1713 char *parent_path;
1714 struct tog_view *diff_view = NULL, *tree_view = NULL;
1715 int begin_x = 0;
1717 switch (ch) {
1718 case 'q':
1719 s->quit = 1;
1720 break;
1721 case 'k':
1722 case KEY_UP:
1723 case '<':
1724 case ',':
1725 if (s->first_displayed_entry == NULL)
1726 break;
1727 if (s->selected > 0)
1728 s->selected--;
1729 if (s->selected > 0)
1730 break;
1731 scroll_up(view, &s->first_displayed_entry, 1,
1732 &s->commits);
1733 break;
1734 case KEY_PPAGE:
1735 case CTRL('b'):
1736 if (s->first_displayed_entry == NULL)
1737 break;
1738 if (TAILQ_FIRST(&s->commits.head) ==
1739 s->first_displayed_entry) {
1740 s->selected = 0;
1741 break;
1743 scroll_up(view, &s->first_displayed_entry,
1744 view->nlines, &s->commits);
1745 break;
1746 case 'j':
1747 case KEY_DOWN:
1748 case '>':
1749 case '.':
1750 if (s->first_displayed_entry == NULL)
1751 break;
1752 if (s->selected < MIN(view->nlines - 2,
1753 s->commits.ncommits - 1)) {
1754 s->selected++;
1755 break;
1757 err = scroll_down(view, &s->first_displayed_entry, 1,
1758 &s->last_displayed_entry, &s->commits,
1759 &s->thread_args.log_complete,
1760 &s->thread_args.commits_needed,
1761 &s->thread_args.need_commits);
1762 break;
1763 case KEY_NPAGE:
1764 case CTRL('f'): {
1765 struct commit_queue_entry *first;
1766 first = s->first_displayed_entry;
1767 if (first == NULL)
1768 break;
1769 err = scroll_down(view, &s->first_displayed_entry,
1770 view->nlines, &s->last_displayed_entry,
1771 &s->commits, &s->thread_args.log_complete,
1772 &s->thread_args.commits_needed,
1773 &s->thread_args.need_commits);
1774 if (first == s->first_displayed_entry &&
1775 s->selected < MIN(view->nlines - 2,
1776 s->commits.ncommits - 1)) {
1777 /* can't scroll further down */
1778 s->selected = MIN(view->nlines - 2,
1779 s->commits.ncommits - 1);
1781 err = NULL;
1782 break;
1784 case KEY_RESIZE:
1785 if (s->selected > view->nlines - 2)
1786 s->selected = view->nlines - 2;
1787 if (s->selected > s->commits.ncommits - 1)
1788 s->selected = s->commits.ncommits - 1;
1789 break;
1790 case KEY_ENTER:
1791 case ' ':
1792 case '\r':
1793 if (s->selected_entry == NULL)
1794 break;
1795 if (view_is_parent_view(view))
1796 begin_x = view_split_begin_x(view->begin_x);
1797 err = open_diff_view_for_commit(&diff_view, begin_x,
1798 s->selected_entry->commit, s->selected_entry->id,
1799 view, s->refs, s->repo);
1800 if (err)
1801 break;
1802 if (view_is_parent_view(view)) {
1803 err = view_close_child(view);
1804 if (err)
1805 return err;
1806 err = view_set_child(view, diff_view);
1807 if (err) {
1808 view_close(diff_view);
1809 break;
1811 *focus_view = diff_view;
1812 view->child_focussed = 1;
1813 } else
1814 *new_view = diff_view;
1815 break;
1816 case 't':
1817 if (s->selected_entry == NULL)
1818 break;
1819 if (view_is_parent_view(view))
1820 begin_x = view_split_begin_x(view->begin_x);
1821 err = browse_commit_tree(&tree_view, begin_x,
1822 s->selected_entry, s->in_repo_path, s->refs, s->repo);
1823 if (err)
1824 break;
1825 if (view_is_parent_view(view)) {
1826 err = view_close_child(view);
1827 if (err)
1828 return err;
1829 err = view_set_child(view, tree_view);
1830 if (err) {
1831 view_close(tree_view);
1832 break;
1834 *focus_view = tree_view;
1835 view->child_focussed = 1;
1836 } else
1837 *new_view = tree_view;
1838 break;
1839 case KEY_BACKSPACE:
1840 if (strcmp(s->in_repo_path, "/") == 0)
1841 break;
1842 parent_path = dirname(s->in_repo_path);
1843 if (parent_path && strcmp(parent_path, ".") != 0) {
1844 struct tog_view *lv;
1845 err = stop_log_thread(s);
1846 if (err)
1847 return err;
1848 lv = view_open(view->nlines, view->ncols,
1849 view->begin_y, view->begin_x, TOG_VIEW_LOG);
1850 if (lv == NULL)
1851 return got_error_from_errno(
1852 "view_open");
1853 err = open_log_view(lv, s->start_id, s->refs,
1854 s->repo, parent_path, 0);
1855 if (err)
1856 return err;;
1857 if (view_is_parent_view(view))
1858 *new_view = lv;
1859 else {
1860 view_set_child(view->parent, lv);
1861 *focus_view = lv;
1863 return NULL;
1865 break;
1866 default:
1867 break;
1870 return err;
1873 static const struct got_error *
1874 apply_unveil(const char *repo_path, const char *worktree_path)
1876 const struct got_error *error;
1878 if (repo_path && unveil(repo_path, "r") != 0)
1879 return got_error_from_errno2("unveil", repo_path);
1881 if (worktree_path && unveil(worktree_path, "rwc") != 0)
1882 return got_error_from_errno2("unveil", worktree_path);
1884 if (unveil("/tmp", "rwc") != 0)
1885 return got_error_from_errno2("unveil", "/tmp");
1887 error = got_privsep_unveil_exec_helpers();
1888 if (error != NULL)
1889 return error;
1891 if (unveil(NULL, NULL) != 0)
1892 return got_error_from_errno("unveil");
1894 return NULL;
1897 static void
1898 init_curses(void)
1900 initscr();
1901 cbreak();
1902 halfdelay(1); /* Do fast refresh while initial view is loading. */
1903 noecho();
1904 nonl();
1905 intrflush(stdscr, FALSE);
1906 keypad(stdscr, TRUE);
1907 curs_set(0);
1908 signal(SIGWINCH, tog_sigwinch);
1911 static const struct got_error *
1912 cmd_log(int argc, char *argv[])
1914 const struct got_error *error;
1915 struct got_repository *repo = NULL;
1916 struct got_worktree *worktree = NULL;
1917 struct got_reflist_head refs;
1918 struct got_object_id *start_id = NULL;
1919 char *path = NULL, *repo_path = NULL, *cwd = NULL;
1920 char *start_commit = NULL;
1921 int ch;
1922 struct tog_view *view;
1924 SIMPLEQ_INIT(&refs);
1926 #ifndef PROFILE
1927 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
1928 NULL) == -1)
1929 err(1, "pledge");
1930 #endif
1932 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1933 switch (ch) {
1934 case 'c':
1935 start_commit = optarg;
1936 break;
1937 case 'r':
1938 repo_path = realpath(optarg, NULL);
1939 if (repo_path == NULL)
1940 err(1, "-r option");
1941 break;
1942 default:
1943 usage_log();
1944 /* NOTREACHED */
1948 argc -= optind;
1949 argv += optind;
1951 cwd = getcwd(NULL, 0);
1952 if (cwd == NULL) {
1953 error = got_error_from_errno("getcwd");
1954 goto done;
1956 error = got_worktree_open(&worktree, cwd);
1957 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1958 goto done;
1959 error = NULL;
1961 if (argc == 0) {
1962 path = strdup("");
1963 if (path == NULL) {
1964 error = got_error_from_errno("strdup");
1965 goto done;
1967 } else if (argc == 1) {
1968 if (worktree) {
1969 error = got_worktree_resolve_path(&path, worktree,
1970 argv[0]);
1971 if (error)
1972 goto done;
1973 } else {
1974 path = strdup(argv[0]);
1975 if (path == NULL) {
1976 error = got_error_from_errno("strdup");
1977 goto done;
1980 } else
1981 usage_log();
1983 repo_path = worktree ?
1984 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1985 if (repo_path == NULL) {
1986 error = got_error_from_errno("strdup");
1987 goto done;
1990 init_curses();
1992 error = got_repo_open(&repo, repo_path);
1993 if (error != NULL)
1994 goto done;
1996 error = apply_unveil(got_repo_get_path(repo),
1997 worktree ? got_worktree_get_root_path(worktree) : NULL);
1998 if (error)
1999 goto done;
2001 if (start_commit == NULL)
2002 error = get_head_commit_id(&start_id, worktree ?
2003 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2004 repo);
2005 else
2006 error = got_object_resolve_id_str(&start_id, repo,
2007 start_commit);
2008 if (error != NULL)
2009 goto done;
2011 error = got_ref_list(&refs, repo);
2012 if (error)
2013 goto done;
2015 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2016 if (view == NULL) {
2017 error = got_error_from_errno("view_open");
2018 goto done;
2020 error = open_log_view(view, start_id, &refs, repo, path, 1);
2021 if (error)
2022 goto done;
2023 error = view_loop(view);
2024 done:
2025 free(repo_path);
2026 free(cwd);
2027 free(path);
2028 free(start_id);
2029 if (repo)
2030 got_repo_close(repo);
2031 if (worktree)
2032 got_worktree_close(worktree);
2033 got_ref_list_free(&refs);
2034 return error;
2037 __dead static void
2038 usage_diff(void)
2040 endwin();
2041 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2042 getprogname());
2043 exit(1);
2046 static char *
2047 parse_next_line(FILE *f, size_t *len)
2049 char *line;
2050 size_t linelen;
2051 size_t lineno;
2052 const char delim[3] = { '\0', '\0', '\0'};
2054 line = fparseln(f, &linelen, &lineno, delim, 0);
2055 if (len)
2056 *len = linelen;
2057 return line;
2060 static const struct got_error *
2061 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2062 int *last_displayed_line, int *eof, int max_lines,
2063 char *header)
2065 const struct got_error *err;
2066 int nlines = 0, nprinted = 0;
2067 char *line;
2068 size_t len;
2069 wchar_t *wline;
2070 int width;
2072 rewind(f);
2073 werase(view->window);
2075 if (header) {
2076 err = format_line(&wline, &width, header, view->ncols);
2077 if (err) {
2078 return err;
2081 if (view_needs_focus_indication(view))
2082 wstandout(view->window);
2083 waddwstr(view->window, wline);
2084 if (view_needs_focus_indication(view))
2085 wstandend(view->window);
2086 if (width < view->ncols - 1)
2087 waddch(view->window, '\n');
2089 if (max_lines <= 1)
2090 return NULL;
2091 max_lines--;
2094 *eof = 0;
2095 while (nprinted < max_lines) {
2096 line = parse_next_line(f, &len);
2097 if (line == NULL) {
2098 *eof = 1;
2099 break;
2101 if (++nlines < *first_displayed_line) {
2102 free(line);
2103 continue;
2106 err = format_line(&wline, &width, line, view->ncols);
2107 if (err) {
2108 free(line);
2109 return err;
2111 waddwstr(view->window, wline);
2112 if (width < view->ncols - 1)
2113 waddch(view->window, '\n');
2114 if (++nprinted == 1)
2115 *first_displayed_line = nlines;
2116 free(line);
2117 free(wline);
2118 wline = NULL;
2120 *last_displayed_line = nlines;
2122 view_vborder(view);
2124 if (*eof) {
2125 while (nprinted < view->nlines) {
2126 waddch(view->window, '\n');
2127 nprinted++;
2130 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2131 if (err) {
2132 return err;
2135 wstandout(view->window);
2136 waddwstr(view->window, wline);
2137 wstandend(view->window);
2140 return NULL;
2143 static char *
2144 get_datestr(time_t *time, char *datebuf)
2146 char *p, *s = ctime_r(time, datebuf);
2147 p = strchr(s, '\n');
2148 if (p)
2149 *p = '\0';
2150 return s;
2153 static const struct got_error *
2154 write_commit_info(struct got_object_id *commit_id,
2155 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2157 const struct got_error *err = NULL;
2158 char datebuf[26];
2159 struct got_commit_object *commit;
2160 char *id_str = NULL;
2161 time_t committer_time;
2162 const char *author, *committer;
2163 char *refs_str = NULL;
2165 if (refs) {
2166 err = build_refs_str(&refs_str, refs, commit_id);
2167 if (err)
2168 return err;
2171 err = got_object_open_as_commit(&commit, repo, commit_id);
2172 if (err)
2173 return err;
2175 err = got_object_id_str(&id_str, commit_id);
2176 if (err) {
2177 err = got_error_from_errno("got_object_id_str");
2178 goto done;
2181 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2182 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2183 err = got_error_from_errno("fprintf");
2184 goto done;
2186 if (fprintf(outfile, "from: %s\n",
2187 got_object_commit_get_author(commit)) < 0) {
2188 err = got_error_from_errno("fprintf");
2189 goto done;
2191 committer_time = got_object_commit_get_committer_time(commit);
2192 if (fprintf(outfile, "date: %s UTC\n",
2193 get_datestr(&committer_time, datebuf)) < 0) {
2194 err = got_error_from_errno("fprintf");
2195 goto done;
2197 author = got_object_commit_get_author(commit);
2198 committer = got_object_commit_get_committer(commit);
2199 if (strcmp(author, committer) != 0 &&
2200 fprintf(outfile, "via: %s\n", committer) < 0) {
2201 err = got_error_from_errno("fprintf");
2202 goto done;
2204 if (fprintf(outfile, "%s\n",
2205 got_object_commit_get_logmsg(commit)) < 0) {
2206 err = got_error_from_errno("fprintf");
2207 goto done;
2209 done:
2210 free(id_str);
2211 free(refs_str);
2212 got_object_commit_close(commit);
2213 return err;
2216 static const struct got_error *
2217 create_diff(struct tog_diff_view_state *s)
2219 const struct got_error *err = NULL;
2220 FILE *f = NULL;
2221 int obj_type;
2223 f = got_opentemp();
2224 if (f == NULL) {
2225 err = got_error_from_errno("got_opentemp");
2226 goto done;
2228 if (s->f && fclose(s->f) != 0) {
2229 err = got_error_from_errno("fclose");
2230 goto done;
2232 s->f = f;
2234 if (s->id1)
2235 err = got_object_get_type(&obj_type, s->repo, s->id1);
2236 else
2237 err = got_object_get_type(&obj_type, s->repo, s->id2);
2238 if (err)
2239 goto done;
2241 switch (obj_type) {
2242 case GOT_OBJ_TYPE_BLOB:
2243 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2244 s->diff_context, s->repo, f);
2245 break;
2246 case GOT_OBJ_TYPE_TREE:
2247 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2248 s->diff_context, s->repo, f);
2249 break;
2250 case GOT_OBJ_TYPE_COMMIT: {
2251 const struct got_object_id_queue *parent_ids;
2252 struct got_object_qid *pid;
2253 struct got_commit_object *commit2;
2255 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2256 if (err)
2257 break;
2258 /* Show commit info if we're diffing to a parent/root commit. */
2259 if (s->id1 == NULL)
2260 write_commit_info(s->id2, s->refs, s->repo, f);
2261 else {
2262 parent_ids = got_object_commit_get_parent_ids(commit2);
2263 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2264 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2265 write_commit_info(s->id2, s->refs,
2266 s->repo, f);
2267 break;
2271 got_object_commit_close(commit2);
2273 err = got_diff_objects_as_commits(s->id1, s->id2,
2274 s->diff_context, s->repo, f);
2275 break;
2277 default:
2278 err = got_error(GOT_ERR_OBJ_TYPE);
2279 break;
2281 done:
2282 if (f && fflush(f) != 0 && err == NULL)
2283 err = got_error_from_errno("fflush");
2284 return err;
2287 static void
2288 diff_view_indicate_progress(struct tog_view *view)
2290 mvwaddstr(view->window, 0, 0, "diffing...");
2291 update_panels();
2292 doupdate();
2295 static const struct got_error *
2296 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2297 struct got_object_id *id2, struct tog_view *log_view,
2298 struct got_reflist_head *refs, struct got_repository *repo)
2300 const struct got_error *err;
2302 if (id1 != NULL && id2 != NULL) {
2303 int type1, type2;
2304 err = got_object_get_type(&type1, repo, id1);
2305 if (err)
2306 return err;
2307 err = got_object_get_type(&type2, repo, id2);
2308 if (err)
2309 return err;
2311 if (type1 != type2)
2312 return got_error(GOT_ERR_OBJ_TYPE);
2315 if (id1) {
2316 view->state.diff.id1 = got_object_id_dup(id1);
2317 if (view->state.diff.id1 == NULL)
2318 return got_error_from_errno("got_object_id_dup");
2319 } else
2320 view->state.diff.id1 = NULL;
2322 view->state.diff.id2 = got_object_id_dup(id2);
2323 if (view->state.diff.id2 == NULL) {
2324 free(view->state.diff.id1);
2325 view->state.diff.id1 = NULL;
2326 return got_error_from_errno("got_object_id_dup");
2328 view->state.diff.f = NULL;
2329 view->state.diff.first_displayed_line = 1;
2330 view->state.diff.last_displayed_line = view->nlines;
2331 view->state.diff.diff_context = 3;
2332 view->state.diff.log_view = log_view;
2333 view->state.diff.repo = repo;
2334 view->state.diff.refs = refs;
2336 if (log_view && view_is_splitscreen(view))
2337 show_log_view(log_view); /* draw vborder */
2338 diff_view_indicate_progress(view);
2340 err = create_diff(&view->state.diff);
2341 if (err) {
2342 free(view->state.diff.id1);
2343 view->state.diff.id1 = NULL;
2344 free(view->state.diff.id2);
2345 view->state.diff.id2 = NULL;
2346 return err;
2349 view->show = show_diff_view;
2350 view->input = input_diff_view;
2351 view->close = close_diff_view;
2353 return NULL;
2356 static const struct got_error *
2357 close_diff_view(struct tog_view *view)
2359 const struct got_error *err = NULL;
2361 free(view->state.diff.id1);
2362 view->state.diff.id1 = NULL;
2363 free(view->state.diff.id2);
2364 view->state.diff.id2 = NULL;
2365 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2366 err = got_error_from_errno("fclose");
2367 return err;
2370 static const struct got_error *
2371 show_diff_view(struct tog_view *view)
2373 const struct got_error *err;
2374 struct tog_diff_view_state *s = &view->state.diff;
2375 char *id_str1 = NULL, *id_str2, *header;
2377 if (s->id1) {
2378 err = got_object_id_str(&id_str1, s->id1);
2379 if (err)
2380 return err;
2382 err = got_object_id_str(&id_str2, s->id2);
2383 if (err)
2384 return err;
2386 if (asprintf(&header, "diff %s %s",
2387 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2388 err = got_error_from_errno("asprintf");
2389 free(id_str1);
2390 free(id_str2);
2391 return err;
2393 free(id_str1);
2394 free(id_str2);
2396 return draw_file(view, s->f, &s->first_displayed_line,
2397 &s->last_displayed_line, &s->eof, view->nlines,
2398 header);
2401 static const struct got_error *
2402 set_selected_commit(struct tog_diff_view_state *s,
2403 struct commit_queue_entry *entry)
2405 const struct got_error *err;
2406 const struct got_object_id_queue *parent_ids;
2407 struct got_commit_object *selected_commit;
2408 struct got_object_qid *pid;
2410 free(s->id2);
2411 s->id2 = got_object_id_dup(entry->id);
2412 if (s->id2 == NULL)
2413 return got_error_from_errno("got_object_id_dup");
2415 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2416 if (err)
2417 return err;
2418 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2419 free(s->id1);
2420 pid = SIMPLEQ_FIRST(parent_ids);
2421 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2422 got_object_commit_close(selected_commit);
2423 return NULL;
2426 static const struct got_error *
2427 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2428 struct tog_view **focus_view, struct tog_view *view, int ch)
2430 const struct got_error *err = NULL;
2431 struct tog_diff_view_state *s = &view->state.diff;
2432 struct tog_log_view_state *ls;
2433 struct commit_queue_entry *entry;
2434 int i;
2436 switch (ch) {
2437 case 'k':
2438 case KEY_UP:
2439 if (s->first_displayed_line > 1)
2440 s->first_displayed_line--;
2441 break;
2442 case KEY_PPAGE:
2443 case CTRL('b'):
2444 if (s->first_displayed_line == 1)
2445 break;
2446 i = 0;
2447 while (i++ < view->nlines - 1 &&
2448 s->first_displayed_line > 1)
2449 s->first_displayed_line--;
2450 break;
2451 case 'j':
2452 case KEY_DOWN:
2453 if (!s->eof)
2454 s->first_displayed_line++;
2455 break;
2456 case KEY_NPAGE:
2457 case CTRL('f'):
2458 case ' ':
2459 if (s->eof)
2460 break;
2461 i = 0;
2462 while (!s->eof && i++ < view->nlines - 1) {
2463 char *line;
2464 line = parse_next_line(s->f, NULL);
2465 s->first_displayed_line++;
2466 if (line == NULL)
2467 break;
2469 break;
2470 case '[':
2471 if (s->diff_context > 0) {
2472 s->diff_context--;
2473 diff_view_indicate_progress(view);
2474 err = create_diff(s);
2476 break;
2477 case ']':
2478 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2479 s->diff_context++;
2480 diff_view_indicate_progress(view);
2481 err = create_diff(s);
2483 break;
2484 case '<':
2485 case ',':
2486 if (s->log_view == NULL)
2487 break;
2488 ls = &s->log_view->state.log;
2489 entry = TAILQ_PREV(ls->selected_entry,
2490 commit_queue_head, entry);
2491 if (entry == NULL)
2492 break;
2494 err = input_log_view(NULL, NULL, NULL, s->log_view,
2495 KEY_UP);
2496 if (err)
2497 break;
2499 err = set_selected_commit(s, entry);
2500 if (err)
2501 break;
2503 s->first_displayed_line = 1;
2504 s->last_displayed_line = view->nlines;
2506 diff_view_indicate_progress(view);
2507 err = create_diff(s);
2508 break;
2509 case '>':
2510 case '.':
2511 if (s->log_view == NULL)
2512 break;
2513 ls = &s->log_view->state.log;
2515 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2516 ls->thread_args.commits_needed++;
2518 /* Display "loading..." in log view. */
2519 show_log_view(s->log_view);
2520 update_panels();
2521 doupdate();
2523 err = trigger_log_thread(1 /* load_all */,
2524 &ls->thread_args.commits_needed,
2525 &ls->thread_args.log_complete,
2526 &ls->thread_args.need_commits);
2527 if (err)
2528 break;
2530 err = input_log_view(NULL, NULL, NULL, s->log_view,
2531 KEY_DOWN);
2532 if (err)
2533 break;
2535 entry = TAILQ_NEXT(ls->selected_entry, entry);
2536 if (entry == NULL)
2537 break;
2539 err = set_selected_commit(s, entry);
2540 if (err)
2541 break;
2543 s->first_displayed_line = 1;
2544 s->last_displayed_line = view->nlines;
2546 diff_view_indicate_progress(view);
2547 err = create_diff(s);
2548 break;
2549 default:
2550 break;
2553 return err;
2556 static const struct got_error *
2557 cmd_diff(int argc, char *argv[])
2559 const struct got_error *error = NULL;
2560 struct got_repository *repo = NULL;
2561 struct got_reflist_head refs;
2562 struct got_object_id *id1 = NULL, *id2 = NULL;
2563 char *repo_path = NULL;
2564 char *id_str1 = NULL, *id_str2 = NULL;
2565 int ch;
2566 struct tog_view *view;
2568 SIMPLEQ_INIT(&refs);
2570 #ifndef PROFILE
2571 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2572 NULL) == -1)
2573 err(1, "pledge");
2574 #endif
2576 while ((ch = getopt(argc, argv, "")) != -1) {
2577 switch (ch) {
2578 default:
2579 usage_diff();
2580 /* NOTREACHED */
2584 argc -= optind;
2585 argv += optind;
2587 if (argc == 0) {
2588 usage_diff(); /* TODO show local worktree changes */
2589 } else if (argc == 2) {
2590 repo_path = getcwd(NULL, 0);
2591 if (repo_path == NULL)
2592 return got_error_from_errno("getcwd");
2593 id_str1 = argv[0];
2594 id_str2 = argv[1];
2595 } else if (argc == 3) {
2596 repo_path = realpath(argv[0], NULL);
2597 if (repo_path == NULL)
2598 return got_error_from_errno2("realpath", argv[0]);
2599 id_str1 = argv[1];
2600 id_str2 = argv[2];
2601 } else
2602 usage_diff();
2604 init_curses();
2606 error = got_repo_open(&repo, repo_path);
2607 if (error)
2608 goto done;
2610 error = apply_unveil(got_repo_get_path(repo), NULL);
2611 if (error)
2612 goto done;
2614 error = got_object_resolve_id_str(&id1, repo, id_str1);
2615 if (error)
2616 goto done;
2618 error = got_object_resolve_id_str(&id2, repo, id_str2);
2619 if (error)
2620 goto done;
2622 error = got_ref_list(&refs, repo);
2623 if (error)
2624 goto done;
2626 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2627 if (view == NULL) {
2628 error = got_error_from_errno("view_open");
2629 goto done;
2631 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2632 if (error)
2633 goto done;
2634 error = view_loop(view);
2635 done:
2636 free(repo_path);
2637 got_repo_close(repo);
2638 got_ref_list_free(&refs);
2639 return error;
2642 __dead static void
2643 usage_blame(void)
2645 endwin();
2646 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2647 getprogname());
2648 exit(1);
2651 struct tog_blame_line {
2652 int annotated;
2653 struct got_object_id *id;
2656 static const struct got_error *
2657 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2658 const char *path, struct tog_blame_line *lines, int nlines,
2659 int blame_complete, int selected_line, int *first_displayed_line,
2660 int *last_displayed_line, int *eof, int max_lines)
2662 const struct got_error *err;
2663 int lineno = 0, nprinted = 0;
2664 char *line;
2665 size_t len;
2666 wchar_t *wline;
2667 int width, wlimit;
2668 struct tog_blame_line *blame_line;
2669 struct got_object_id *prev_id = NULL;
2670 char *id_str;
2672 err = got_object_id_str(&id_str, id);
2673 if (err)
2674 return err;
2676 rewind(f);
2677 werase(view->window);
2679 if (asprintf(&line, "commit %s", id_str) == -1) {
2680 err = got_error_from_errno("asprintf");
2681 free(id_str);
2682 return err;
2685 err = format_line(&wline, &width, line, view->ncols);
2686 free(line);
2687 line = NULL;
2688 if (view_needs_focus_indication(view))
2689 wstandout(view->window);
2690 waddwstr(view->window, wline);
2691 if (view_needs_focus_indication(view))
2692 wstandend(view->window);
2693 free(wline);
2694 wline = NULL;
2695 if (width < view->ncols - 1)
2696 waddch(view->window, '\n');
2698 if (asprintf(&line, "[%d/%d] %s%s",
2699 *first_displayed_line - 1 + selected_line, nlines,
2700 blame_complete ? "" : "annotating... ", path) == -1) {
2701 free(id_str);
2702 return got_error_from_errno("asprintf");
2704 free(id_str);
2705 err = format_line(&wline, &width, line, view->ncols);
2706 free(line);
2707 line = NULL;
2708 if (err)
2709 return err;
2710 waddwstr(view->window, wline);
2711 free(wline);
2712 wline = NULL;
2713 if (width < view->ncols - 1)
2714 waddch(view->window, '\n');
2716 *eof = 0;
2717 while (nprinted < max_lines - 2) {
2718 line = parse_next_line(f, &len);
2719 if (line == NULL) {
2720 *eof = 1;
2721 break;
2723 if (++lineno < *first_displayed_line) {
2724 free(line);
2725 continue;
2728 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2729 err = format_line(&wline, &width, line, wlimit);
2730 if (err) {
2731 free(line);
2732 return err;
2735 if (view->focussed && nprinted == selected_line - 1)
2736 wstandout(view->window);
2738 blame_line = &lines[lineno - 1];
2739 if (blame_line->annotated && prev_id &&
2740 got_object_id_cmp(prev_id, blame_line->id) == 0)
2741 waddstr(view->window, " ");
2742 else if (blame_line->annotated) {
2743 char *id_str;
2744 err = got_object_id_str(&id_str, blame_line->id);
2745 if (err) {
2746 free(line);
2747 free(wline);
2748 return err;
2750 wprintw(view->window, "%.8s ", id_str);
2751 free(id_str);
2752 prev_id = blame_line->id;
2753 } else {
2754 waddstr(view->window, "........ ");
2755 prev_id = NULL;
2758 waddwstr(view->window, wline);
2759 while (width < wlimit) {
2760 waddch(view->window, ' ');
2761 width++;
2763 if (view->focussed && nprinted == selected_line - 1)
2764 wstandend(view->window);
2765 if (++nprinted == 1)
2766 *first_displayed_line = lineno;
2767 free(line);
2768 free(wline);
2769 wline = NULL;
2771 *last_displayed_line = lineno;
2773 view_vborder(view);
2775 return NULL;
2778 static const struct got_error *
2779 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2781 const struct got_error *err = NULL;
2782 struct tog_blame_cb_args *a = arg;
2783 struct tog_blame_line *line;
2784 int errcode;
2786 if (nlines != a->nlines ||
2787 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2788 return got_error(GOT_ERR_RANGE);
2790 errcode = pthread_mutex_lock(&tog_mutex);
2791 if (errcode)
2792 return got_error_set_errno(errcode, "pthread_mutex_lock");
2794 if (*a->quit) { /* user has quit the blame view */
2795 err = got_error(GOT_ERR_ITER_COMPLETED);
2796 goto done;
2799 if (lineno == -1)
2800 goto done; /* no change in this commit */
2802 line = &a->lines[lineno - 1];
2803 if (line->annotated)
2804 goto done;
2806 line->id = got_object_id_dup(id);
2807 if (line->id == NULL) {
2808 err = got_error_from_errno("got_object_id_dup");
2809 goto done;
2811 line->annotated = 1;
2812 done:
2813 errcode = pthread_mutex_unlock(&tog_mutex);
2814 if (errcode)
2815 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2816 return err;
2819 static void *
2820 blame_thread(void *arg)
2822 const struct got_error *err;
2823 struct tog_blame_thread_args *ta = arg;
2824 struct tog_blame_cb_args *a = ta->cb_args;
2825 int errcode;
2827 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
2828 blame_cb, ta->cb_args);
2830 errcode = pthread_mutex_lock(&tog_mutex);
2831 if (errcode)
2832 return (void *)got_error_set_errno(errcode,
2833 "pthread_mutex_lock");
2835 got_repo_close(ta->repo);
2836 ta->repo = NULL;
2837 *ta->complete = 1;
2839 errcode = pthread_mutex_unlock(&tog_mutex);
2840 if (errcode && err == NULL)
2841 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2843 return (void *)err;
2846 static struct got_object_id *
2847 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
2848 int selected_line)
2850 struct tog_blame_line *line;
2852 line = &lines[first_displayed_line - 1 + selected_line - 1];
2853 if (!line->annotated)
2854 return NULL;
2856 return line->id;
2859 static const struct got_error *
2860 stop_blame(struct tog_blame *blame)
2862 const struct got_error *err = NULL;
2863 int i;
2865 if (blame->thread) {
2866 int errcode;
2867 errcode = pthread_mutex_unlock(&tog_mutex);
2868 if (errcode)
2869 return got_error_set_errno(errcode,
2870 "pthread_mutex_unlock");
2871 errcode = pthread_join(blame->thread, (void **)&err);
2872 if (errcode)
2873 return got_error_set_errno(errcode, "pthread_join");
2874 errcode = pthread_mutex_lock(&tog_mutex);
2875 if (errcode)
2876 return got_error_set_errno(errcode,
2877 "pthread_mutex_lock");
2878 if (err && err->code == GOT_ERR_ITER_COMPLETED)
2879 err = NULL;
2880 blame->thread = NULL;
2882 if (blame->thread_args.repo) {
2883 got_repo_close(blame->thread_args.repo);
2884 blame->thread_args.repo = NULL;
2886 if (blame->f) {
2887 if (fclose(blame->f) != 0 && err == NULL)
2888 err = got_error_from_errno("fclose");
2889 blame->f = NULL;
2891 if (blame->lines) {
2892 for (i = 0; i < blame->nlines; i++)
2893 free(blame->lines[i].id);
2894 free(blame->lines);
2895 blame->lines = NULL;
2897 free(blame->cb_args.commit_id);
2898 blame->cb_args.commit_id = NULL;
2900 return err;
2903 static const struct got_error *
2904 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
2905 int *first_displayed_line, int *last_displayed_line, int *selected_line,
2906 int *done, int *eof, const char *path, struct got_object_id *commit_id,
2907 struct got_repository *repo)
2909 const struct got_error *err = NULL;
2910 struct got_blob_object *blob = NULL;
2911 struct got_repository *thread_repo = NULL;
2912 struct got_object_id *obj_id = NULL;
2913 int obj_type;
2915 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
2916 if (err)
2917 return err;
2918 if (obj_id == NULL)
2919 return got_error(GOT_ERR_NO_OBJ);
2921 err = got_object_get_type(&obj_type, repo, obj_id);
2922 if (err)
2923 goto done;
2925 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2926 err = got_error(GOT_ERR_OBJ_TYPE);
2927 goto done;
2930 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2931 if (err)
2932 goto done;
2933 blame->f = got_opentemp();
2934 if (blame->f == NULL) {
2935 err = got_error_from_errno("got_opentemp");
2936 goto done;
2938 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
2939 blame->f, blob);
2940 if (err)
2941 goto done;
2943 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
2944 if (blame->lines == NULL) {
2945 err = got_error_from_errno("calloc");
2946 goto done;
2949 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
2950 if (err)
2951 goto done;
2953 blame->cb_args.view = view;
2954 blame->cb_args.lines = blame->lines;
2955 blame->cb_args.nlines = blame->nlines;
2956 blame->cb_args.commit_id = got_object_id_dup(commit_id);
2957 if (blame->cb_args.commit_id == NULL) {
2958 err = got_error_from_errno("got_object_id_dup");
2959 goto done;
2961 blame->cb_args.quit = done;
2963 blame->thread_args.path = path;
2964 blame->thread_args.repo = thread_repo;
2965 blame->thread_args.cb_args = &blame->cb_args;
2966 blame->thread_args.complete = blame_complete;
2967 *blame_complete = 0;
2969 done:
2970 if (blob)
2971 got_object_blob_close(blob);
2972 free(obj_id);
2973 if (err)
2974 stop_blame(blame);
2975 return err;
2978 static const struct got_error *
2979 open_blame_view(struct tog_view *view, char *path,
2980 struct got_object_id *commit_id, struct got_reflist_head *refs,
2981 struct got_repository *repo)
2983 const struct got_error *err = NULL;
2984 struct tog_blame_view_state *s = &view->state.blame;
2986 SIMPLEQ_INIT(&s->blamed_commits);
2988 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
2989 if (err)
2990 return err;
2992 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
2993 s->first_displayed_line = 1;
2994 s->last_displayed_line = view->nlines;
2995 s->selected_line = 1;
2996 s->blame_complete = 0;
2997 s->path = path;
2998 if (s->path == NULL)
2999 return got_error_from_errno("open_blame_view");
3000 s->repo = repo;
3001 s->refs = refs;
3002 s->commit_id = commit_id;
3003 memset(&s->blame, 0, sizeof(s->blame));
3005 view->show = show_blame_view;
3006 view->input = input_blame_view;
3007 view->close = close_blame_view;
3009 return run_blame(&s->blame, view, &s->blame_complete,
3010 &s->first_displayed_line, &s->last_displayed_line,
3011 &s->selected_line, &s->done, &s->eof, s->path,
3012 s->blamed_commit->id, s->repo);
3015 static const struct got_error *
3016 close_blame_view(struct tog_view *view)
3018 const struct got_error *err = NULL;
3019 struct tog_blame_view_state *s = &view->state.blame;
3021 if (s->blame.thread)
3022 err = stop_blame(&s->blame);
3024 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3025 struct got_object_qid *blamed_commit;
3026 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3027 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3028 got_object_qid_free(blamed_commit);
3031 free(s->path);
3033 return err;
3036 static const struct got_error *
3037 show_blame_view(struct tog_view *view)
3039 const struct got_error *err = NULL;
3040 struct tog_blame_view_state *s = &view->state.blame;
3041 int errcode;
3043 if (s->blame.thread == NULL) {
3044 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3045 &s->blame.thread_args);
3046 if (errcode)
3047 return got_error_set_errno(errcode, "pthread_create");
3050 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3051 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3052 s->selected_line, &s->first_displayed_line,
3053 &s->last_displayed_line, &s->eof, view->nlines);
3055 view_vborder(view);
3056 return err;
3059 static const struct got_error *
3060 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3061 struct tog_view **focus_view, struct tog_view *view, int ch)
3063 const struct got_error *err = NULL, *thread_err = NULL;
3064 struct tog_view *diff_view;
3065 struct tog_blame_view_state *s = &view->state.blame;
3066 int begin_x = 0;
3068 switch (ch) {
3069 case 'q':
3070 s->done = 1;
3071 break;
3072 case 'k':
3073 case KEY_UP:
3074 if (s->selected_line > 1)
3075 s->selected_line--;
3076 else if (s->selected_line == 1 &&
3077 s->first_displayed_line > 1)
3078 s->first_displayed_line--;
3079 break;
3080 case KEY_PPAGE:
3081 if (s->first_displayed_line == 1) {
3082 s->selected_line = 1;
3083 break;
3085 if (s->first_displayed_line > view->nlines - 2)
3086 s->first_displayed_line -=
3087 (view->nlines - 2);
3088 else
3089 s->first_displayed_line = 1;
3090 break;
3091 case 'j':
3092 case KEY_DOWN:
3093 if (s->selected_line < view->nlines - 2 &&
3094 s->first_displayed_line +
3095 s->selected_line <= s->blame.nlines)
3096 s->selected_line++;
3097 else if (s->last_displayed_line <
3098 s->blame.nlines)
3099 s->first_displayed_line++;
3100 break;
3101 case 'b':
3102 case 'p': {
3103 struct got_object_id *id = NULL;
3104 id = get_selected_commit_id(s->blame.lines,
3105 s->first_displayed_line, s->selected_line);
3106 if (id == NULL)
3107 break;
3108 if (ch == 'p') {
3109 struct got_commit_object *commit;
3110 struct got_object_qid *pid;
3111 struct got_object_id *blob_id = NULL;
3112 int obj_type;
3113 err = got_object_open_as_commit(&commit,
3114 s->repo, id);
3115 if (err)
3116 break;
3117 pid = SIMPLEQ_FIRST(
3118 got_object_commit_get_parent_ids(commit));
3119 if (pid == NULL) {
3120 got_object_commit_close(commit);
3121 break;
3123 /* Check if path history ends here. */
3124 err = got_object_id_by_path(&blob_id, s->repo,
3125 pid->id, s->path);
3126 if (err) {
3127 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3128 err = NULL;
3129 got_object_commit_close(commit);
3130 break;
3132 err = got_object_get_type(&obj_type, s->repo,
3133 blob_id);
3134 free(blob_id);
3135 /* Can't blame non-blob type objects. */
3136 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3137 got_object_commit_close(commit);
3138 break;
3140 err = got_object_qid_alloc(&s->blamed_commit,
3141 pid->id);
3142 got_object_commit_close(commit);
3143 } else {
3144 if (got_object_id_cmp(id,
3145 s->blamed_commit->id) == 0)
3146 break;
3147 err = got_object_qid_alloc(&s->blamed_commit,
3148 id);
3150 if (err)
3151 break;
3152 s->done = 1;
3153 thread_err = stop_blame(&s->blame);
3154 s->done = 0;
3155 if (thread_err)
3156 break;
3157 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3158 s->blamed_commit, entry);
3159 err = run_blame(&s->blame, view, &s->blame_complete,
3160 &s->first_displayed_line, &s->last_displayed_line,
3161 &s->selected_line, &s->done, &s->eof,
3162 s->path, s->blamed_commit->id, s->repo);
3163 if (err)
3164 break;
3165 break;
3167 case 'B': {
3168 struct got_object_qid *first;
3169 first = SIMPLEQ_FIRST(&s->blamed_commits);
3170 if (!got_object_id_cmp(first->id, s->commit_id))
3171 break;
3172 s->done = 1;
3173 thread_err = stop_blame(&s->blame);
3174 s->done = 0;
3175 if (thread_err)
3176 break;
3177 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3178 got_object_qid_free(s->blamed_commit);
3179 s->blamed_commit =
3180 SIMPLEQ_FIRST(&s->blamed_commits);
3181 err = run_blame(&s->blame, view, &s->blame_complete,
3182 &s->first_displayed_line, &s->last_displayed_line,
3183 &s->selected_line, &s->done, &s->eof, s->path,
3184 s->blamed_commit->id, s->repo);
3185 if (err)
3186 break;
3187 break;
3189 case KEY_ENTER:
3190 case '\r': {
3191 struct got_object_id *id = NULL;
3192 struct got_object_qid *pid;
3193 struct got_commit_object *commit = NULL;
3194 id = get_selected_commit_id(s->blame.lines,
3195 s->first_displayed_line, s->selected_line);
3196 if (id == NULL)
3197 break;
3198 err = got_object_open_as_commit(&commit, s->repo, id);
3199 if (err)
3200 break;
3201 pid = SIMPLEQ_FIRST(
3202 got_object_commit_get_parent_ids(commit));
3203 if (view_is_parent_view(view))
3204 begin_x = view_split_begin_x(view->begin_x);
3205 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3206 if (diff_view == NULL) {
3207 got_object_commit_close(commit);
3208 err = got_error_from_errno("view_open");
3209 break;
3211 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3212 id, NULL, s->refs, s->repo);
3213 got_object_commit_close(commit);
3214 if (err) {
3215 view_close(diff_view);
3216 break;
3218 if (view_is_parent_view(view)) {
3219 err = view_close_child(view);
3220 if (err)
3221 break;
3222 err = view_set_child(view, diff_view);
3223 if (err) {
3224 view_close(diff_view);
3225 break;
3227 *focus_view = diff_view;
3228 view->child_focussed = 1;
3229 } else
3230 *new_view = diff_view;
3231 if (err)
3232 break;
3233 break;
3235 case KEY_NPAGE:
3236 case ' ':
3237 if (s->last_displayed_line >= s->blame.nlines &&
3238 s->selected_line >= MIN(s->blame.nlines,
3239 view->nlines - 2)) {
3240 break;
3242 if (s->last_displayed_line >= s->blame.nlines &&
3243 s->selected_line < view->nlines - 2) {
3244 s->selected_line = MIN(s->blame.nlines,
3245 view->nlines - 2);
3246 break;
3248 if (s->last_displayed_line + view->nlines - 2
3249 <= s->blame.nlines)
3250 s->first_displayed_line +=
3251 view->nlines - 2;
3252 else
3253 s->first_displayed_line =
3254 s->blame.nlines -
3255 (view->nlines - 3);
3256 break;
3257 case KEY_RESIZE:
3258 if (s->selected_line > view->nlines - 2) {
3259 s->selected_line = MIN(s->blame.nlines,
3260 view->nlines - 2);
3262 break;
3263 default:
3264 break;
3266 return thread_err ? thread_err : err;
3269 static const struct got_error *
3270 cmd_blame(int argc, char *argv[])
3272 const struct got_error *error;
3273 struct got_repository *repo = NULL;
3274 struct got_reflist_head refs;
3275 struct got_worktree *worktree = NULL;
3276 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3277 struct got_object_id *commit_id = NULL;
3278 char *commit_id_str = NULL;
3279 int ch;
3280 struct tog_view *view;
3282 SIMPLEQ_INIT(&refs);
3284 #ifndef PROFILE
3285 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3286 NULL) == -1)
3287 err(1, "pledge");
3288 #endif
3290 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3291 switch (ch) {
3292 case 'c':
3293 commit_id_str = optarg;
3294 break;
3295 case 'r':
3296 repo_path = realpath(optarg, NULL);
3297 if (repo_path == NULL)
3298 err(1, "-r option");
3299 break;
3300 default:
3301 usage_blame();
3302 /* NOTREACHED */
3306 argc -= optind;
3307 argv += optind;
3309 if (argc == 1)
3310 path = argv[0];
3311 else
3312 usage_blame();
3314 cwd = getcwd(NULL, 0);
3315 if (cwd == NULL) {
3316 error = got_error_from_errno("getcwd");
3317 goto done;
3319 if (repo_path == NULL) {
3320 error = got_worktree_open(&worktree, cwd);
3321 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3322 goto done;
3323 else
3324 error = NULL;
3325 if (worktree) {
3326 repo_path =
3327 strdup(got_worktree_get_repo_path(worktree));
3328 if (repo_path == NULL)
3329 error = got_error_from_errno("strdup");
3330 if (error)
3331 goto done;
3332 } else {
3333 repo_path = strdup(cwd);
3334 if (repo_path == NULL) {
3335 error = got_error_from_errno("strdup");
3336 goto done;
3341 init_curses();
3343 error = got_repo_open(&repo, repo_path);
3344 if (error != NULL)
3345 goto done;
3347 error = apply_unveil(got_repo_get_path(repo), NULL);
3348 if (error)
3349 goto done;
3351 if (worktree) {
3352 const char *prefix = got_worktree_get_path_prefix(worktree);
3353 char *p, *worktree_subdir = cwd +
3354 strlen(got_worktree_get_root_path(worktree));
3355 if (asprintf(&p, "%s%s%s%s%s",
3356 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3357 worktree_subdir, worktree_subdir[0] ? "/" : "",
3358 path) == -1) {
3359 error = got_error_from_errno("asprintf");
3360 goto done;
3362 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3363 free(p);
3364 } else {
3365 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3367 if (error)
3368 goto done;
3370 if (commit_id_str == NULL) {
3371 struct got_reference *head_ref;
3372 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3373 if (error != NULL)
3374 goto done;
3375 error = got_ref_resolve(&commit_id, repo, head_ref);
3376 got_ref_close(head_ref);
3377 } else {
3378 error = got_object_resolve_id_str(&commit_id, repo,
3379 commit_id_str);
3381 if (error != NULL)
3382 goto done;
3384 error = got_ref_list(&refs, repo);
3385 if (error)
3386 goto done;
3388 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3389 if (view == NULL) {
3390 error = got_error_from_errno("view_open");
3391 goto done;
3393 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3394 if (error)
3395 goto done;
3396 error = view_loop(view);
3397 done:
3398 free(repo_path);
3399 free(cwd);
3400 free(commit_id);
3401 if (worktree)
3402 got_worktree_close(worktree);
3403 if (repo)
3404 got_repo_close(repo);
3405 got_ref_list_free(&refs);
3406 return error;
3409 static const struct got_error *
3410 draw_tree_entries(struct tog_view *view,
3411 struct got_tree_entry **first_displayed_entry,
3412 struct got_tree_entry **last_displayed_entry,
3413 struct got_tree_entry **selected_entry, int *ndisplayed,
3414 const char *label, int show_ids, const char *parent_path,
3415 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3417 const struct got_error *err = NULL;
3418 struct got_tree_entry *te;
3419 wchar_t *wline;
3420 int width, n;
3422 *ndisplayed = 0;
3424 werase(view->window);
3426 if (limit == 0)
3427 return NULL;
3429 err = format_line(&wline, &width, label, view->ncols);
3430 if (err)
3431 return err;
3432 if (view_needs_focus_indication(view))
3433 wstandout(view->window);
3434 waddwstr(view->window, wline);
3435 if (view_needs_focus_indication(view))
3436 wstandend(view->window);
3437 free(wline);
3438 wline = NULL;
3439 if (width < view->ncols - 1)
3440 waddch(view->window, '\n');
3441 if (--limit <= 0)
3442 return NULL;
3443 err = format_line(&wline, &width, parent_path, view->ncols);
3444 if (err)
3445 return err;
3446 waddwstr(view->window, wline);
3447 free(wline);
3448 wline = NULL;
3449 if (width < view->ncols - 1)
3450 waddch(view->window, '\n');
3451 if (--limit <= 0)
3452 return NULL;
3453 waddch(view->window, '\n');
3454 if (--limit <= 0)
3455 return NULL;
3457 te = SIMPLEQ_FIRST(&entries->head);
3458 if (*first_displayed_entry == NULL) {
3459 if (selected == 0) {
3460 if (view->focussed)
3461 wstandout(view->window);
3462 *selected_entry = NULL;
3464 waddstr(view->window, " ..\n"); /* parent directory */
3465 if (selected == 0 && view->focussed)
3466 wstandend(view->window);
3467 (*ndisplayed)++;
3468 if (--limit <= 0)
3469 return NULL;
3470 n = 1;
3471 } else {
3472 n = 0;
3473 while (te != *first_displayed_entry)
3474 te = SIMPLEQ_NEXT(te, entry);
3477 while (te) {
3478 char *line = NULL, *id_str = NULL;
3480 if (show_ids) {
3481 err = got_object_id_str(&id_str, te->id);
3482 if (err)
3483 return got_error_from_errno(
3484 "got_object_id_str");
3486 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3487 te->name, S_ISDIR(te->mode) ? "/" :
3488 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3489 free(id_str);
3490 return got_error_from_errno("asprintf");
3492 free(id_str);
3493 err = format_line(&wline, &width, line, view->ncols);
3494 if (err) {
3495 free(line);
3496 break;
3498 if (n == selected) {
3499 if (view->focussed)
3500 wstandout(view->window);
3501 *selected_entry = te;
3503 waddwstr(view->window, wline);
3504 if (width < view->ncols - 1)
3505 waddch(view->window, '\n');
3506 if (n == selected && view->focussed)
3507 wstandend(view->window);
3508 free(line);
3509 free(wline);
3510 wline = NULL;
3511 n++;
3512 (*ndisplayed)++;
3513 *last_displayed_entry = te;
3514 if (--limit <= 0)
3515 break;
3516 te = SIMPLEQ_NEXT(te, entry);
3519 return err;
3522 static void
3523 tree_scroll_up(struct tog_view *view,
3524 struct got_tree_entry **first_displayed_entry, int maxscroll,
3525 const struct got_tree_entries *entries, int isroot)
3527 struct got_tree_entry *te, *prev;
3528 int i;
3530 if (*first_displayed_entry == NULL)
3531 return;
3533 te = SIMPLEQ_FIRST(&entries->head);
3534 if (*first_displayed_entry == te) {
3535 if (!isroot)
3536 *first_displayed_entry = NULL;
3537 return;
3540 /* XXX this is stupid... switch to TAILQ? */
3541 for (i = 0; i < maxscroll; i++) {
3542 while (te != *first_displayed_entry) {
3543 prev = te;
3544 te = SIMPLEQ_NEXT(te, entry);
3546 *first_displayed_entry = prev;
3547 te = SIMPLEQ_FIRST(&entries->head);
3549 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3550 *first_displayed_entry = NULL;
3553 static int
3554 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3555 struct got_tree_entry *last_displayed_entry,
3556 const struct got_tree_entries *entries)
3558 struct got_tree_entry *next, *last;
3559 int n = 0;
3561 if (*first_displayed_entry)
3562 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3563 else
3564 next = SIMPLEQ_FIRST(&entries->head);
3565 last = last_displayed_entry;
3566 while (next && last && n++ < maxscroll) {
3567 last = SIMPLEQ_NEXT(last, entry);
3568 if (last) {
3569 *first_displayed_entry = next;
3570 next = SIMPLEQ_NEXT(next, entry);
3573 return n;
3576 static const struct got_error *
3577 tree_entry_path(char **path, struct tog_parent_trees *parents,
3578 struct got_tree_entry *te)
3580 const struct got_error *err = NULL;
3581 struct tog_parent_tree *pt;
3582 size_t len = 2; /* for leading slash and NUL */
3584 TAILQ_FOREACH(pt, parents, entry)
3585 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3586 if (te)
3587 len += strlen(te->name);
3589 *path = calloc(1, len);
3590 if (path == NULL)
3591 return got_error_from_errno("calloc");
3593 (*path)[0] = '/';
3594 pt = TAILQ_LAST(parents, tog_parent_trees);
3595 while (pt) {
3596 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3597 err = got_error(GOT_ERR_NO_SPACE);
3598 goto done;
3600 if (strlcat(*path, "/", len) >= len) {
3601 err = got_error(GOT_ERR_NO_SPACE);
3602 goto done;
3604 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3606 if (te) {
3607 if (strlcat(*path, te->name, len) >= len) {
3608 err = got_error(GOT_ERR_NO_SPACE);
3609 goto done;
3612 done:
3613 if (err) {
3614 free(*path);
3615 *path = NULL;
3617 return err;
3620 static const struct got_error *
3621 blame_tree_entry(struct tog_view **new_view, int begin_x,
3622 struct got_tree_entry *te, struct tog_parent_trees *parents,
3623 struct got_object_id *commit_id, struct got_reflist_head *refs,
3624 struct got_repository *repo)
3626 const struct got_error *err = NULL;
3627 char *path;
3628 struct tog_view *blame_view;
3630 err = tree_entry_path(&path, parents, te);
3631 if (err)
3632 return err;
3634 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3635 if (blame_view == NULL)
3636 return got_error_from_errno("view_open");
3638 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3639 if (err) {
3640 view_close(blame_view);
3641 free(path);
3642 } else
3643 *new_view = blame_view;
3644 return err;
3647 static const struct got_error *
3648 log_tree_entry(struct tog_view **new_view, int begin_x,
3649 struct got_tree_entry *te, struct tog_parent_trees *parents,
3650 struct got_object_id *commit_id, struct got_reflist_head *refs,
3651 struct got_repository *repo)
3653 struct tog_view *log_view;
3654 const struct got_error *err = NULL;
3655 char *path;
3657 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3658 if (log_view == NULL)
3659 return got_error_from_errno("view_open");
3661 err = tree_entry_path(&path, parents, te);
3662 if (err)
3663 return err;
3665 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3666 if (err)
3667 view_close(log_view);
3668 else
3669 *new_view = log_view;
3670 free(path);
3671 return err;
3674 static const struct got_error *
3675 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3676 struct got_object_id *commit_id, struct got_reflist_head *refs,
3677 struct got_repository *repo)
3679 const struct got_error *err = NULL;
3680 char *commit_id_str = NULL;
3681 struct tog_tree_view_state *s = &view->state.tree;
3683 TAILQ_INIT(&s->parents);
3685 err = got_object_id_str(&commit_id_str, commit_id);
3686 if (err != NULL)
3687 goto done;
3689 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3690 err = got_error_from_errno("asprintf");
3691 goto done;
3694 s->root = s->tree = root;
3695 s->entries = got_object_tree_get_entries(root);
3696 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3697 s->commit_id = got_object_id_dup(commit_id);
3698 if (s->commit_id == NULL) {
3699 err = got_error_from_errno("got_object_id_dup");
3700 goto done;
3702 s->refs = refs;
3703 s->repo = repo;
3705 view->show = show_tree_view;
3706 view->input = input_tree_view;
3707 view->close = close_tree_view;
3708 done:
3709 free(commit_id_str);
3710 if (err) {
3711 free(s->tree_label);
3712 s->tree_label = NULL;
3714 return err;
3717 static const struct got_error *
3718 close_tree_view(struct tog_view *view)
3720 struct tog_tree_view_state *s = &view->state.tree;
3722 free(s->tree_label);
3723 s->tree_label = NULL;
3724 free(s->commit_id);
3725 s->commit_id = NULL;
3726 while (!TAILQ_EMPTY(&s->parents)) {
3727 struct tog_parent_tree *parent;
3728 parent = TAILQ_FIRST(&s->parents);
3729 TAILQ_REMOVE(&s->parents, parent, entry);
3730 free(parent);
3733 if (s->tree != s->root)
3734 got_object_tree_close(s->tree);
3735 got_object_tree_close(s->root);
3737 return NULL;
3740 static const struct got_error *
3741 show_tree_view(struct tog_view *view)
3743 const struct got_error *err = NULL;
3744 struct tog_tree_view_state *s = &view->state.tree;
3745 char *parent_path;
3747 err = tree_entry_path(&parent_path, &s->parents, NULL);
3748 if (err)
3749 return err;
3751 err = draw_tree_entries(view, &s->first_displayed_entry,
3752 &s->last_displayed_entry, &s->selected_entry,
3753 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
3754 s->entries, s->selected, view->nlines, s->tree == s->root);
3755 free(parent_path);
3757 view_vborder(view);
3758 return err;
3761 static const struct got_error *
3762 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
3763 struct tog_view **focus_view, struct tog_view *view, int ch)
3765 const struct got_error *err = NULL;
3766 struct tog_tree_view_state *s = &view->state.tree;
3767 struct tog_view *log_view;
3768 int begin_x = 0, nscrolled;
3770 switch (ch) {
3771 case 'i':
3772 s->show_ids = !s->show_ids;
3773 break;
3774 case 'l':
3775 if (!s->selected_entry)
3776 break;
3777 if (view_is_parent_view(view))
3778 begin_x = view_split_begin_x(view->begin_x);
3779 err = log_tree_entry(&log_view, begin_x,
3780 s->selected_entry, &s->parents,
3781 s->commit_id, s->refs, s->repo);
3782 if (view_is_parent_view(view)) {
3783 err = view_close_child(view);
3784 if (err)
3785 return err;
3786 err = view_set_child(view, log_view);
3787 if (err) {
3788 view_close(log_view);
3789 break;
3791 *focus_view = log_view;
3792 view->child_focussed = 1;
3793 } else
3794 *new_view = log_view;
3795 break;
3796 case 'k':
3797 case KEY_UP:
3798 if (s->selected > 0) {
3799 s->selected--;
3800 if (s->selected == 0)
3801 break;
3803 if (s->selected > 0)
3804 break;
3805 tree_scroll_up(view, &s->first_displayed_entry, 1,
3806 s->entries, s->tree == s->root);
3807 break;
3808 case KEY_PPAGE:
3809 tree_scroll_up(view, &s->first_displayed_entry,
3810 MAX(0, view->nlines - 4 - s->selected), s->entries,
3811 s->tree == s->root);
3812 s->selected = 0;
3813 if (SIMPLEQ_FIRST(&s->entries->head) ==
3814 s->first_displayed_entry && s->tree != s->root)
3815 s->first_displayed_entry = NULL;
3816 break;
3817 case 'j':
3818 case KEY_DOWN:
3819 if (s->selected < s->ndisplayed - 1) {
3820 s->selected++;
3821 break;
3823 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
3824 /* can't scroll any further */
3825 break;
3826 tree_scroll_down(&s->first_displayed_entry, 1,
3827 s->last_displayed_entry, s->entries);
3828 break;
3829 case KEY_NPAGE:
3830 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
3831 == NULL) {
3832 /* can't scroll any further; move cursor down */
3833 if (s->selected < s->ndisplayed - 1)
3834 s->selected = s->ndisplayed - 1;
3835 break;
3837 nscrolled = tree_scroll_down(&s->first_displayed_entry,
3838 view->nlines, s->last_displayed_entry, s->entries);
3839 if (nscrolled < view->nlines) {
3840 int ndisplayed = 0;
3841 struct got_tree_entry *te;
3842 te = s->first_displayed_entry;
3843 do {
3844 ndisplayed++;
3845 te = SIMPLEQ_NEXT(te, entry);
3846 } while (te);
3847 s->selected = ndisplayed - 1;
3849 break;
3850 case KEY_ENTER:
3851 case '\r':
3852 case KEY_BACKSPACE:
3853 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
3854 struct tog_parent_tree *parent;
3855 /* user selected '..' */
3856 if (s->tree == s->root)
3857 break;
3858 parent = TAILQ_FIRST(&s->parents);
3859 TAILQ_REMOVE(&s->parents, parent,
3860 entry);
3861 got_object_tree_close(s->tree);
3862 s->tree = parent->tree;
3863 s->entries =
3864 got_object_tree_get_entries(s->tree);
3865 s->first_displayed_entry =
3866 parent->first_displayed_entry;
3867 s->selected_entry =
3868 parent->selected_entry;
3869 s->selected = parent->selected;
3870 free(parent);
3871 } else if (S_ISDIR(s->selected_entry->mode)) {
3872 struct got_tree_object *subtree;
3873 err = got_object_open_as_tree(&subtree,
3874 s->repo, s->selected_entry->id);
3875 if (err)
3876 break;
3877 err = tree_view_visit_subtree(subtree, s);
3878 if (err) {
3879 got_object_tree_close(subtree);
3880 break;
3882 } else if (S_ISREG(s->selected_entry->mode)) {
3883 struct tog_view *blame_view;
3884 int begin_x = view_is_parent_view(view) ?
3885 view_split_begin_x(view->begin_x) : 0;
3887 err = blame_tree_entry(&blame_view, begin_x,
3888 s->selected_entry, &s->parents,
3889 s->commit_id, s->refs, s->repo);
3890 if (err)
3891 break;
3892 if (view_is_parent_view(view)) {
3893 err = view_close_child(view);
3894 if (err)
3895 return err;
3896 err = view_set_child(view, blame_view);
3897 if (err) {
3898 view_close(blame_view);
3899 break;
3901 *focus_view = blame_view;
3902 view->child_focussed = 1;
3903 } else
3904 *new_view = blame_view;
3906 break;
3907 case KEY_RESIZE:
3908 if (s->selected > view->nlines)
3909 s->selected = s->ndisplayed - 1;
3910 break;
3911 default:
3912 break;
3915 return err;
3918 __dead static void
3919 usage_tree(void)
3921 endwin();
3922 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
3923 getprogname());
3924 exit(1);
3927 static const struct got_error *
3928 cmd_tree(int argc, char *argv[])
3930 const struct got_error *error;
3931 struct got_repository *repo = NULL;
3932 struct got_reflist_head refs;
3933 char *repo_path = NULL;
3934 struct got_object_id *commit_id = NULL;
3935 char *commit_id_arg = NULL;
3936 struct got_commit_object *commit = NULL;
3937 struct got_tree_object *tree = NULL;
3938 int ch;
3939 struct tog_view *view;
3941 SIMPLEQ_INIT(&refs);
3943 #ifndef PROFILE
3944 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3945 NULL) == -1)
3946 err(1, "pledge");
3947 #endif
3949 while ((ch = getopt(argc, argv, "c:")) != -1) {
3950 switch (ch) {
3951 case 'c':
3952 commit_id_arg = optarg;
3953 break;
3954 default:
3955 usage_tree();
3956 /* NOTREACHED */
3960 argc -= optind;
3961 argv += optind;
3963 if (argc == 0) {
3964 struct got_worktree *worktree;
3965 char *cwd = getcwd(NULL, 0);
3966 if (cwd == NULL)
3967 return got_error_from_errno("getcwd");
3968 error = got_worktree_open(&worktree, cwd);
3969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3970 goto done;
3971 if (worktree) {
3972 free(cwd);
3973 repo_path =
3974 strdup(got_worktree_get_repo_path(worktree));
3975 got_worktree_close(worktree);
3976 } else
3977 repo_path = cwd;
3978 if (repo_path == NULL) {
3979 error = got_error_from_errno("strdup");
3980 goto done;
3982 } else if (argc == 1) {
3983 repo_path = realpath(argv[0], NULL);
3984 if (repo_path == NULL)
3985 return got_error_from_errno2("realpath", argv[0]);
3986 } else
3987 usage_log();
3989 init_curses();
3991 error = got_repo_open(&repo, repo_path);
3992 if (error != NULL)
3993 goto done;
3995 error = apply_unveil(got_repo_get_path(repo), NULL);
3996 if (error)
3997 goto done;
3999 if (commit_id_arg == NULL)
4000 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4001 else
4002 error = got_object_resolve_id_str(&commit_id, repo,
4003 commit_id_arg);
4004 if (error != NULL)
4005 goto done;
4007 error = got_object_open_as_commit(&commit, repo, commit_id);
4008 if (error != NULL)
4009 goto done;
4011 error = got_object_open_as_tree(&tree, repo,
4012 got_object_commit_get_tree_id(commit));
4013 if (error != NULL)
4014 goto done;
4016 error = got_ref_list(&refs, repo);
4017 if (error)
4018 goto done;
4020 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4021 if (view == NULL) {
4022 error = got_error_from_errno("view_open");
4023 goto done;
4025 error = open_tree_view(view, tree, commit_id, &refs, repo);
4026 if (error)
4027 goto done;
4028 error = view_loop(view);
4029 done:
4030 free(repo_path);
4031 free(commit_id);
4032 if (commit)
4033 got_object_commit_close(commit);
4034 if (tree)
4035 got_object_tree_close(tree);
4036 if (repo)
4037 got_repo_close(repo);
4038 got_ref_list_free(&refs);
4039 return error;
4042 __dead static void
4043 usage(void)
4045 int i;
4047 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n\n"
4048 "Available commands:\n", getprogname());
4049 for (i = 0; i < nitems(tog_commands); i++) {
4050 struct tog_cmd *cmd = &tog_commands[i];
4051 fprintf(stderr, " %s: %s\n", cmd->name, cmd->descr);
4053 exit(1);
4056 static char **
4057 make_argv(const char *arg0, const char *arg1)
4059 char **argv;
4060 int argc = (arg1 == NULL ? 1 : 2);
4062 argv = calloc(argc, sizeof(char *));
4063 if (argv == NULL)
4064 err(1, "calloc");
4065 argv[0] = strdup(arg0);
4066 if (argv[0] == NULL)
4067 err(1, "calloc");
4068 if (arg1) {
4069 argv[1] = strdup(arg1);
4070 if (argv[1] == NULL)
4071 err(1, "calloc");
4074 return argv;
4077 int
4078 main(int argc, char *argv[])
4080 const struct got_error *error = NULL;
4081 struct tog_cmd *cmd = NULL;
4082 int ch, hflag = 0;
4083 char **cmd_argv = NULL;
4085 setlocale(LC_CTYPE, "");
4087 while ((ch = getopt(argc, argv, "h")) != -1) {
4088 switch (ch) {
4089 case 'h':
4090 hflag = 1;
4091 break;
4092 default:
4093 usage();
4094 /* NOTREACHED */
4098 argc -= optind;
4099 argv += optind;
4100 optind = 0;
4101 optreset = 1;
4103 if (argc == 0) {
4104 if (hflag)
4105 usage();
4106 /* Build an argument vector which runs a default command. */
4107 cmd = &tog_commands[0];
4108 cmd_argv = make_argv(cmd->name, NULL);
4109 argc = 1;
4110 } else {
4111 int i;
4113 /* Did the user specific a command? */
4114 for (i = 0; i < nitems(tog_commands); i++) {
4115 if (strncmp(tog_commands[i].name, argv[0],
4116 strlen(argv[0])) == 0) {
4117 cmd = &tog_commands[i];
4118 if (hflag)
4119 tog_commands[i].cmd_usage();
4120 break;
4123 if (cmd == NULL) {
4124 /* Did the user specify a repository? */
4125 char *repo_path = realpath(argv[0], NULL);
4126 if (repo_path) {
4127 struct got_repository *repo;
4128 error = got_repo_open(&repo, repo_path);
4129 if (error == NULL)
4130 got_repo_close(repo);
4131 } else
4132 error = got_error_from_errno2("realpath",
4133 argv[0]);
4134 if (error) {
4135 if (hflag) {
4136 fprintf(stderr, "%s: '%s' is not a "
4137 "known command\n", getprogname(),
4138 argv[0]);
4139 usage();
4141 fprintf(stderr, "%s: '%s' is neither a known "
4142 "command nor a path to a repository\n",
4143 getprogname(), argv[0]);
4144 free(repo_path);
4145 return 1;
4147 cmd = &tog_commands[0];
4148 cmd_argv = make_argv(cmd->name, repo_path);
4149 argc = 2;
4150 free(repo_path);
4154 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4155 if (error)
4156 goto done;
4157 done:
4158 endwin();
4159 free(cmd_argv);
4160 if (error)
4161 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4162 return 0;