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>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_line_color {
116 SIMPLEQ_ENTRY(tog_line_color) entry;
117 regex_t regex;
118 short colorpair;
119 };
120 SIMPLEQ_HEAD(tog_line_colors, tog_line_color);
122 struct tog_diff_view_state {
123 struct got_object_id *id1, *id2;
124 FILE *f;
125 int first_displayed_line;
126 int last_displayed_line;
127 int eof;
128 int diff_context;
129 struct got_repository *repo;
130 struct got_reflist_head *refs;
131 struct tog_line_colors line_colors;
133 /* passed from log view; may be NULL */
134 struct tog_view *log_view;
135 };
137 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
139 struct tog_log_thread_args {
140 pthread_cond_t need_commits;
141 int commits_needed;
142 struct got_commit_graph *graph;
143 struct commit_queue *commits;
144 const char *in_repo_path;
145 struct got_object_id *start_id;
146 struct got_repository *repo;
147 int log_complete;
148 sig_atomic_t *quit;
149 struct commit_queue_entry **first_displayed_entry;
150 struct commit_queue_entry **selected_entry;
151 int *searching;
152 int *search_next_done;
153 regex_t *regex;
154 };
156 struct tog_log_view_state {
157 struct commit_queue commits;
158 struct commit_queue_entry *first_displayed_entry;
159 struct commit_queue_entry *last_displayed_entry;
160 struct commit_queue_entry *selected_entry;
161 int selected;
162 char *in_repo_path;
163 const char *head_ref_name;
164 struct got_repository *repo;
165 struct got_reflist_head *refs;
166 struct got_object_id *start_id;
167 sig_atomic_t quit;
168 pthread_t thread;
169 struct tog_log_thread_args thread_args;
170 struct commit_queue_entry *matched_entry;
171 struct commit_queue_entry *search_entry;
172 };
174 struct tog_blame_cb_args {
175 struct tog_blame_line *lines; /* one per line */
176 int nlines;
178 struct tog_view *view;
179 struct got_object_id *commit_id;
180 int *quit;
181 };
183 struct tog_blame_thread_args {
184 const char *path;
185 struct got_repository *repo;
186 struct tog_blame_cb_args *cb_args;
187 int *complete;
188 got_cancel_cb cancel_cb;
189 void *cancel_arg;
190 };
192 struct tog_blame {
193 FILE *f;
194 size_t filesize;
195 struct tog_blame_line *lines;
196 int nlines;
197 off_t *line_offsets;
198 pthread_t thread;
199 struct tog_blame_thread_args thread_args;
200 struct tog_blame_cb_args cb_args;
201 const char *path;
202 };
204 struct tog_blame_view_state {
205 int first_displayed_line;
206 int last_displayed_line;
207 int selected_line;
208 int blame_complete;
209 int eof;
210 int done;
211 struct got_object_id_queue blamed_commits;
212 struct got_object_qid *blamed_commit;
213 char *path;
214 struct got_repository *repo;
215 struct got_reflist_head *refs;
216 struct got_object_id *commit_id;
217 struct tog_blame blame;
218 int matched_line;
219 };
221 struct tog_parent_tree {
222 TAILQ_ENTRY(tog_parent_tree) entry;
223 struct got_tree_object *tree;
224 struct got_tree_entry *first_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int selected;
227 };
229 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
231 struct tog_tree_view_state {
232 char *tree_label;
233 struct got_tree_object *root;
234 struct got_tree_object *tree;
235 const struct got_tree_entries *entries;
236 struct got_tree_entry *first_displayed_entry;
237 struct got_tree_entry *last_displayed_entry;
238 struct got_tree_entry *selected_entry;
239 int ndisplayed, selected, show_ids;
240 struct tog_parent_trees parents;
241 struct got_object_id *commit_id;
242 struct got_repository *repo;
243 struct got_reflist_head *refs;
244 struct got_tree_entry *matched_entry;
245 struct tog_line_colors line_colors;
246 };
248 /*
249 * We implement two types of views: parent views and child views.
251 * The 'Tab' key switches between a parent view and its child view.
252 * Child views are shown side-by-side to their parent view, provided
253 * there is enough screen estate.
255 * When a new view is opened from within a parent view, this new view
256 * becomes a child view of the parent view, replacing any existing child.
258 * When a new view is opened from within a child view, this new view
259 * becomes a parent view which will obscure the views below until the
260 * user quits the new parent view by typing 'q'.
262 * This list of views contains parent views only.
263 * Child views are only pointed to by their parent view.
264 */
265 TAILQ_HEAD(tog_view_list_head, tog_view);
267 struct tog_view {
268 TAILQ_ENTRY(tog_view) entry;
269 WINDOW *window;
270 PANEL *panel;
271 int nlines, ncols, begin_y, begin_x;
272 int lines, cols; /* copies of LINES and COLS */
273 int focussed;
274 struct tog_view *parent;
275 struct tog_view *child;
276 int child_focussed;
278 /* type-specific state */
279 enum tog_view_type type;
280 union {
281 struct tog_diff_view_state diff;
282 struct tog_log_view_state log;
283 struct tog_blame_view_state blame;
284 struct tog_tree_view_state tree;
285 } state;
287 const struct got_error *(*show)(struct tog_view *);
288 const struct got_error *(*input)(struct tog_view **,
289 struct tog_view **, struct tog_view**, struct tog_view *, int);
290 const struct got_error *(*close)(struct tog_view *);
292 const struct got_error *(*search_start)(struct tog_view *);
293 const struct got_error *(*search_next)(struct tog_view *);
294 int searching;
295 #define TOG_SEARCH_FORWARD 1
296 #define TOG_SEARCH_BACKWARD 2
297 int search_next_done;
298 regex_t regex;
299 };
301 static const struct got_error *open_diff_view(struct tog_view *,
302 struct got_object_id *, struct got_object_id *, struct tog_view *,
303 struct got_reflist_head *, struct got_repository *);
304 static const struct got_error *show_diff_view(struct tog_view *);
305 static const struct got_error *input_diff_view(struct tog_view **,
306 struct tog_view **, struct tog_view **, struct tog_view *, int);
307 static const struct got_error* close_diff_view(struct tog_view *);
309 static const struct got_error *open_log_view(struct tog_view *,
310 struct got_object_id *, struct got_reflist_head *,
311 struct got_repository *, const char *, const char *, int);
312 static const struct got_error * show_log_view(struct tog_view *);
313 static const struct got_error *input_log_view(struct tog_view **,
314 struct tog_view **, struct tog_view **, struct tog_view *, int);
315 static const struct got_error *close_log_view(struct tog_view *);
316 static const struct got_error *search_start_log_view(struct tog_view *);
317 static const struct got_error *search_next_log_view(struct tog_view *);
319 static const struct got_error *open_blame_view(struct tog_view *, char *,
320 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
321 static const struct got_error *show_blame_view(struct tog_view *);
322 static const struct got_error *input_blame_view(struct tog_view **,
323 struct tog_view **, struct tog_view **, struct tog_view *, int);
324 static const struct got_error *close_blame_view(struct tog_view *);
325 static const struct got_error *search_start_blame_view(struct tog_view *);
326 static const struct got_error *search_next_blame_view(struct tog_view *);
328 static const struct got_error *open_tree_view(struct tog_view *,
329 struct got_tree_object *, struct got_object_id *,
330 struct got_reflist_head *, struct got_repository *);
331 static const struct got_error *show_tree_view(struct tog_view *);
332 static const struct got_error *input_tree_view(struct tog_view **,
333 struct tog_view **, struct tog_view **, struct tog_view *, int);
334 static const struct got_error *close_tree_view(struct tog_view *);
335 static const struct got_error *search_start_tree_view(struct tog_view *);
336 static const struct got_error *search_next_tree_view(struct tog_view *);
338 static volatile sig_atomic_t tog_sigwinch_received;
339 static volatile sig_atomic_t tog_sigpipe_received;
341 static void
342 tog_sigwinch(int signo)
344 tog_sigwinch_received = 1;
347 static void
348 tog_sigpipe(int signo)
350 tog_sigpipe_received = 1;
353 static const struct got_error *
354 view_close(struct tog_view *view)
356 const struct got_error *err = NULL;
358 if (view->child) {
359 view_close(view->child);
360 view->child = NULL;
362 if (view->close)
363 err = view->close(view);
364 if (view->panel)
365 del_panel(view->panel);
366 if (view->window)
367 delwin(view->window);
368 free(view);
369 return err;
372 static struct tog_view *
373 view_open(int nlines, int ncols, int begin_y, int begin_x,
374 enum tog_view_type type)
376 struct tog_view *view = calloc(1, sizeof(*view));
378 if (view == NULL)
379 return NULL;
381 view->type = type;
382 view->lines = LINES;
383 view->cols = COLS;
384 view->nlines = nlines ? nlines : LINES - begin_y;
385 view->ncols = ncols ? ncols : COLS - begin_x;
386 view->begin_y = begin_y;
387 view->begin_x = begin_x;
388 view->window = newwin(nlines, ncols, begin_y, begin_x);
389 if (view->window == NULL) {
390 view_close(view);
391 return NULL;
393 view->panel = new_panel(view->window);
394 if (view->panel == NULL ||
395 set_panel_userptr(view->panel, view) != OK) {
396 view_close(view);
397 return NULL;
400 keypad(view->window, TRUE);
401 return view;
404 static int
405 view_split_begin_x(int begin_x)
407 if (begin_x > 0 || COLS < 120)
408 return 0;
409 return (COLS - MAX(COLS / 2, 80));
412 static const struct got_error *view_resize(struct tog_view *);
414 static const struct got_error *
415 view_splitscreen(struct tog_view *view)
417 const struct got_error *err = NULL;
419 view->begin_y = 0;
420 view->begin_x = view_split_begin_x(0);
421 view->nlines = LINES;
422 view->ncols = COLS - view->begin_x;
423 view->lines = LINES;
424 view->cols = COLS;
425 err = view_resize(view);
426 if (err)
427 return err;
429 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
430 return got_error_from_errno("mvwin");
432 return NULL;
435 static const struct got_error *
436 view_fullscreen(struct tog_view *view)
438 const struct got_error *err = NULL;
440 view->begin_x = 0;
441 view->begin_y = 0;
442 view->nlines = LINES;
443 view->ncols = COLS;
444 view->lines = LINES;
445 view->cols = COLS;
446 err = view_resize(view);
447 if (err)
448 return err;
450 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
451 return got_error_from_errno("mvwin");
453 return NULL;
456 static int
457 view_is_parent_view(struct tog_view *view)
459 return view->parent == NULL;
462 static const struct got_error *
463 view_resize(struct tog_view *view)
465 int nlines, ncols;
467 if (view->lines > LINES)
468 nlines = view->nlines - (view->lines - LINES);
469 else
470 nlines = view->nlines + (LINES - view->lines);
472 if (view->cols > COLS)
473 ncols = view->ncols - (view->cols - COLS);
474 else
475 ncols = view->ncols + (COLS - view->cols);
477 if (wresize(view->window, nlines, ncols) == ERR)
478 return got_error_from_errno("wresize");
479 if (replace_panel(view->panel, view->window) == ERR)
480 return got_error_from_errno("replace_panel");
481 wclear(view->window);
483 view->nlines = nlines;
484 view->ncols = ncols;
485 view->lines = LINES;
486 view->cols = COLS;
488 if (view->child) {
489 view->child->begin_x = view_split_begin_x(view->begin_x);
490 if (view->child->begin_x == 0) {
491 view_fullscreen(view->child);
492 if (view->child->focussed)
493 show_panel(view->child->panel);
494 else
495 show_panel(view->panel);
496 } else {
497 view_splitscreen(view->child);
498 show_panel(view->child->panel);
502 return NULL;
505 static const struct got_error *
506 view_close_child(struct tog_view *view)
508 const struct got_error *err = NULL;
510 if (view->child == NULL)
511 return NULL;
513 err = view_close(view->child);
514 view->child = NULL;
515 return err;
518 static const struct got_error *
519 view_set_child(struct tog_view *view, struct tog_view *child)
521 const struct got_error *err = NULL;
523 view->child = child;
524 child->parent = view;
525 return err;
528 static int
529 view_is_splitscreen(struct tog_view *view)
531 return view->begin_x > 0;
534 static void
535 tog_resizeterm(void)
537 int cols, lines;
538 struct winsize size;
540 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
541 cols = 80; /* Default */
542 lines = 24;
543 } else {
544 cols = size.ws_col;
545 lines = size.ws_row;
547 resize_term(lines, cols);
550 static const struct got_error *
551 view_search_start(struct tog_view *view)
553 const struct got_error *err = NULL;
554 char pattern[1024];
555 int ret;
556 int begin_x = 0;
558 if (view->nlines < 1)
559 return NULL;
561 if (!view_is_parent_view(view))
562 begin_x = view_split_begin_x(view->begin_x);
563 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
564 begin_x, "/");
565 wclrtoeol(view->window);
567 nocbreak();
568 echo();
569 ret = wgetnstr(view->window, pattern, sizeof(pattern));
570 cbreak();
571 noecho();
572 if (ret == ERR)
573 return NULL;
575 if (view->searching) {
576 regfree(&view->regex);
577 view->searching = 0;
580 if (regcomp(&view->regex, pattern,
581 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
582 err = view->search_start(view);
583 if (err) {
584 regfree(&view->regex);
585 return err;
587 view->searching = TOG_SEARCH_FORWARD;
588 view->search_next_done = 0;
589 view->search_next(view);
592 return NULL;
595 static const struct got_error *
596 view_input(struct tog_view **new, struct tog_view **dead,
597 struct tog_view **focus, int *done, struct tog_view *view,
598 struct tog_view_list_head *views)
600 const struct got_error *err = NULL;
601 struct tog_view *v;
602 int ch, errcode;
604 *new = NULL;
605 *dead = NULL;
606 *focus = NULL;
608 if (view->searching && !view->search_next_done) {
609 errcode = pthread_mutex_unlock(&tog_mutex);
610 if (errcode)
611 return got_error_set_errno(errcode,
612 "pthread_mutex_unlock");
613 pthread_yield();
614 errcode = pthread_mutex_lock(&tog_mutex);
615 if (errcode)
616 return got_error_set_errno(errcode,
617 "pthread_mutex_lock");
618 view->search_next(view);
619 return NULL;
622 nodelay(stdscr, FALSE);
623 /* Allow threads to make progress while we are waiting for input. */
624 errcode = pthread_mutex_unlock(&tog_mutex);
625 if (errcode)
626 return got_error_set_errno(errcode, "pthread_mutex_unlock");
627 ch = wgetch(view->window);
628 errcode = pthread_mutex_lock(&tog_mutex);
629 if (errcode)
630 return got_error_set_errno(errcode, "pthread_mutex_lock");
631 nodelay(stdscr, TRUE);
633 if (tog_sigwinch_received) {
634 tog_resizeterm();
635 tog_sigwinch_received = 0;
636 TAILQ_FOREACH(v, views, entry) {
637 err = view_resize(v);
638 if (err)
639 return err;
640 err = v->input(new, dead, focus, v, KEY_RESIZE);
641 if (err)
642 return err;
646 switch (ch) {
647 case ERR:
648 break;
649 case '\t':
650 if (view->child) {
651 *focus = view->child;
652 view->child_focussed = 1;
653 } else if (view->parent) {
654 *focus = view->parent;
655 view->parent->child_focussed = 0;
657 break;
658 case 'q':
659 err = view->input(new, dead, focus, view, ch);
660 *dead = view;
661 break;
662 case 'Q':
663 *done = 1;
664 break;
665 case 'f':
666 if (view_is_parent_view(view)) {
667 if (view->child == NULL)
668 break;
669 if (view_is_splitscreen(view->child)) {
670 *focus = view->child;
671 view->child_focussed = 1;
672 err = view_fullscreen(view->child);
673 } else
674 err = view_splitscreen(view->child);
675 if (err)
676 break;
677 err = view->child->input(new, dead, focus,
678 view->child, KEY_RESIZE);
679 } else {
680 if (view_is_splitscreen(view)) {
681 *focus = view;
682 view->parent->child_focussed = 1;
683 err = view_fullscreen(view);
684 } else {
685 err = view_splitscreen(view);
687 if (err)
688 break;
689 err = view->input(new, dead, focus, view,
690 KEY_RESIZE);
692 break;
693 case KEY_RESIZE:
694 break;
695 case '/':
696 if (view->search_start)
697 view_search_start(view);
698 else
699 err = view->input(new, dead, focus, view, ch);
700 break;
701 case 'N':
702 case 'n':
703 if (view->search_next && view->searching) {
704 view->searching = (ch == 'n' ?
705 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
706 view->search_next_done = 0;
707 view->search_next(view);
708 } else
709 err = view->input(new, dead, focus, view, ch);
710 break;
711 default:
712 err = view->input(new, dead, focus, view, ch);
713 break;
716 return err;
719 void
720 view_vborder(struct tog_view *view)
722 PANEL *panel;
723 struct tog_view *view_above;
725 if (view->parent)
726 return view_vborder(view->parent);
728 panel = panel_above(view->panel);
729 if (panel == NULL)
730 return;
732 view_above = panel_userptr(panel);
733 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
734 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
737 int
738 view_needs_focus_indication(struct tog_view *view)
740 if (view_is_parent_view(view)) {
741 if (view->child == NULL || view->child_focussed)
742 return 0;
743 if (!view_is_splitscreen(view->child))
744 return 0;
745 } else if (!view_is_splitscreen(view))
746 return 0;
748 return view->focussed;
751 static const struct got_error *
752 view_loop(struct tog_view *view)
754 const struct got_error *err = NULL;
755 struct tog_view_list_head views;
756 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
757 int fast_refresh = 10;
758 int done = 0, errcode;
760 errcode = pthread_mutex_lock(&tog_mutex);
761 if (errcode)
762 return got_error_set_errno(errcode, "pthread_mutex_lock");
764 TAILQ_INIT(&views);
765 TAILQ_INSERT_HEAD(&views, view, entry);
767 main_view = view;
768 view->focussed = 1;
769 err = view->show(view);
770 if (err)
771 return err;
772 update_panels();
773 doupdate();
774 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
775 /* Refresh fast during initialization, then become slower. */
776 if (fast_refresh && fast_refresh-- == 0)
777 halfdelay(10); /* switch to once per second */
779 err = view_input(&new_view, &dead_view, &focus_view, &done,
780 view, &views);
781 if (err)
782 break;
783 if (dead_view) {
784 struct tog_view *prev = NULL;
786 if (view_is_parent_view(dead_view))
787 prev = TAILQ_PREV(dead_view,
788 tog_view_list_head, entry);
789 else if (view->parent != dead_view)
790 prev = view->parent;
792 if (dead_view->parent)
793 dead_view->parent->child = NULL;
794 else
795 TAILQ_REMOVE(&views, dead_view, entry);
797 err = view_close(dead_view);
798 if (err || (dead_view == main_view && new_view == NULL))
799 goto done;
801 if (view == dead_view) {
802 if (focus_view)
803 view = focus_view;
804 else if (prev)
805 view = prev;
806 else if (!TAILQ_EMPTY(&views))
807 view = TAILQ_LAST(&views,
808 tog_view_list_head);
809 else
810 view = NULL;
811 if (view) {
812 if (view->child && view->child_focussed)
813 focus_view = view->child;
814 else
815 focus_view = view;
819 if (new_view) {
820 struct tog_view *v, *t;
821 /* Only allow one parent view per type. */
822 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
823 if (v->type != new_view->type)
824 continue;
825 TAILQ_REMOVE(&views, v, entry);
826 err = view_close(v);
827 if (err)
828 goto done;
829 break;
831 TAILQ_INSERT_TAIL(&views, new_view, entry);
832 view = new_view;
833 if (focus_view == NULL)
834 focus_view = new_view;
836 if (focus_view) {
837 show_panel(focus_view->panel);
838 if (view)
839 view->focussed = 0;
840 focus_view->focussed = 1;
841 view = focus_view;
842 if (new_view)
843 show_panel(new_view->panel);
844 if (view->child && view_is_splitscreen(view->child))
845 show_panel(view->child->panel);
847 if (view) {
848 if (focus_view == NULL) {
849 view->focussed = 1;
850 show_panel(view->panel);
851 if (view->child && view_is_splitscreen(view->child))
852 show_panel(view->child->panel);
853 focus_view = view;
855 if (view->parent) {
856 err = view->parent->show(view->parent);
857 if (err)
858 goto done;
860 err = view->show(view);
861 if (err)
862 goto done;
863 if (view->child) {
864 err = view->child->show(view->child);
865 if (err)
866 goto done;
868 update_panels();
869 doupdate();
872 done:
873 while (!TAILQ_EMPTY(&views)) {
874 view = TAILQ_FIRST(&views);
875 TAILQ_REMOVE(&views, view, entry);
876 view_close(view);
879 errcode = pthread_mutex_unlock(&tog_mutex);
880 if (errcode && err == NULL)
881 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
883 return err;
886 __dead static void
887 usage_log(void)
889 endwin();
890 fprintf(stderr,
891 "usage: %s log [-c commit] [-r repository-path] [path]\n",
892 getprogname());
893 exit(1);
896 /* Create newly allocated wide-character string equivalent to a byte string. */
897 static const struct got_error *
898 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
900 char *vis = NULL;
901 const struct got_error *err = NULL;
903 *ws = NULL;
904 *wlen = mbstowcs(NULL, s, 0);
905 if (*wlen == (size_t)-1) {
906 int vislen;
907 if (errno != EILSEQ)
908 return got_error_from_errno("mbstowcs");
910 /* byte string invalid in current encoding; try to "fix" it */
911 err = got_mbsavis(&vis, &vislen, s);
912 if (err)
913 return err;
914 *wlen = mbstowcs(NULL, vis, 0);
915 if (*wlen == (size_t)-1) {
916 err = got_error_from_errno("mbstowcs"); /* give up */
917 goto done;
921 *ws = calloc(*wlen + 1, sizeof(**ws));
922 if (*ws == NULL) {
923 err = got_error_from_errno("calloc");
924 goto done;
927 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
928 err = got_error_from_errno("mbstowcs");
929 done:
930 free(vis);
931 if (err) {
932 free(*ws);
933 *ws = NULL;
934 *wlen = 0;
936 return err;
939 /* Format a line for display, ensuring that it won't overflow a width limit. */
940 static const struct got_error *
941 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
942 int col_tab_align)
944 const struct got_error *err = NULL;
945 int cols = 0;
946 wchar_t *wline = NULL;
947 size_t wlen;
948 int i;
950 *wlinep = NULL;
951 *widthp = 0;
953 err = mbs2ws(&wline, &wlen, line);
954 if (err)
955 return err;
957 i = 0;
958 while (i < wlen) {
959 int width = wcwidth(wline[i]);
961 if (width == 0) {
962 i++;
963 continue;
966 if (width == 1 || width == 2) {
967 if (cols + width > wlimit)
968 break;
969 cols += width;
970 i++;
971 } else if (width == -1) {
972 if (wline[i] == L'\t') {
973 width = TABSIZE -
974 ((cols + col_tab_align) % TABSIZE);
975 if (cols + width > wlimit)
976 break;
977 cols += width;
979 i++;
980 } else {
981 err = got_error_from_errno("wcwidth");
982 goto done;
985 wline[i] = L'\0';
986 if (widthp)
987 *widthp = cols;
988 done:
989 if (err)
990 free(wline);
991 else
992 *wlinep = wline;
993 return err;
996 static const struct got_error*
997 build_refs_str(char **refs_str, struct got_reflist_head *refs,
998 struct got_object_id *id, struct got_repository *repo)
1000 static const struct got_error *err = NULL;
1001 struct got_reflist_entry *re;
1002 char *s;
1003 const char *name;
1005 *refs_str = NULL;
1007 SIMPLEQ_FOREACH(re, refs, entry) {
1008 struct got_tag_object *tag = NULL;
1009 int cmp;
1011 name = got_ref_get_name(re->ref);
1012 if (strcmp(name, GOT_REF_HEAD) == 0)
1013 continue;
1014 if (strncmp(name, "refs/", 5) == 0)
1015 name += 5;
1016 if (strncmp(name, "got/", 4) == 0)
1017 continue;
1018 if (strncmp(name, "heads/", 6) == 0)
1019 name += 6;
1020 if (strncmp(name, "remotes/", 8) == 0)
1021 name += 8;
1022 if (strncmp(name, "tags/", 5) == 0) {
1023 err = got_object_open_as_tag(&tag, repo, re->id);
1024 if (err) {
1025 if (err->code != GOT_ERR_OBJ_TYPE)
1026 break;
1027 /* Ref points at something other than a tag. */
1028 err = NULL;
1029 tag = NULL;
1032 cmp = got_object_id_cmp(tag ?
1033 got_object_tag_get_object_id(tag) : re->id, id);
1034 if (tag)
1035 got_object_tag_close(tag);
1036 if (cmp != 0)
1037 continue;
1038 s = *refs_str;
1039 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1040 s ? ", " : "", name) == -1) {
1041 err = got_error_from_errno("asprintf");
1042 free(s);
1043 *refs_str = NULL;
1044 break;
1046 free(s);
1049 return err;
1052 static const struct got_error *
1053 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1054 int col_tab_align)
1056 char *smallerthan, *at;
1058 smallerthan = strchr(author, '<');
1059 if (smallerthan && smallerthan[1] != '\0')
1060 author = smallerthan + 1;
1061 at = strchr(author, '@');
1062 if (at)
1063 *at = '\0';
1064 return format_line(wauthor, author_width, author, limit, col_tab_align);
1067 static const struct got_error *
1068 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1069 struct got_object_id *id, struct got_reflist_head *refs,
1070 const size_t date_display_cols, int author_display_cols)
1072 const struct got_error *err = NULL;
1073 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1074 char *logmsg0 = NULL, *logmsg = NULL;
1075 char *author = NULL;
1076 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1077 int author_width, logmsg_width;
1078 char *newline, *line = NULL;
1079 int col, limit;
1080 const int avail = view->ncols;
1081 struct tm tm;
1082 time_t committer_time;
1084 committer_time = got_object_commit_get_committer_time(commit);
1085 if (localtime_r(&committer_time, &tm) == NULL)
1086 return got_error_from_errno("localtime_r");
1087 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1088 >= sizeof(datebuf))
1089 return got_error(GOT_ERR_NO_SPACE);
1091 if (avail <= date_display_cols)
1092 limit = MIN(sizeof(datebuf) - 1, avail);
1093 else
1094 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1095 waddnstr(view->window, datebuf, limit);
1096 col = limit;
1097 if (col > avail)
1098 goto done;
1100 if (avail >= 120) {
1101 char *id_str;
1102 err = got_object_id_str(&id_str, id);
1103 if (err)
1104 goto done;
1105 wprintw(view->window, "%.8s ", id_str);
1106 free(id_str);
1107 col += 9;
1108 if (col > avail)
1109 goto done;
1112 author = strdup(got_object_commit_get_author(commit));
1113 if (author == NULL) {
1114 err = got_error_from_errno("strdup");
1115 goto done;
1117 err = format_author(&wauthor, &author_width, author, avail - col, col);
1118 if (err)
1119 goto done;
1120 waddwstr(view->window, wauthor);
1121 col += author_width;
1122 while (col < avail && author_width < author_display_cols + 2) {
1123 waddch(view->window, ' ');
1124 col++;
1125 author_width++;
1127 if (col > avail)
1128 goto done;
1130 err = got_object_commit_get_logmsg(&logmsg0, commit);
1131 if (err)
1132 goto done;
1133 logmsg = logmsg0;
1134 while (*logmsg == '\n')
1135 logmsg++;
1136 newline = strchr(logmsg, '\n');
1137 if (newline)
1138 *newline = '\0';
1139 limit = avail - col;
1140 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1141 if (err)
1142 goto done;
1143 waddwstr(view->window, wlogmsg);
1144 col += logmsg_width;
1145 while (col < avail) {
1146 waddch(view->window, ' ');
1147 col++;
1149 done:
1150 free(logmsg0);
1151 free(wlogmsg);
1152 free(author);
1153 free(wauthor);
1154 free(line);
1155 return err;
1158 static struct commit_queue_entry *
1159 alloc_commit_queue_entry(struct got_commit_object *commit,
1160 struct got_object_id *id)
1162 struct commit_queue_entry *entry;
1164 entry = calloc(1, sizeof(*entry));
1165 if (entry == NULL)
1166 return NULL;
1168 entry->id = id;
1169 entry->commit = commit;
1170 return entry;
1173 static void
1174 pop_commit(struct commit_queue *commits)
1176 struct commit_queue_entry *entry;
1178 entry = TAILQ_FIRST(&commits->head);
1179 TAILQ_REMOVE(&commits->head, entry, entry);
1180 got_object_commit_close(entry->commit);
1181 commits->ncommits--;
1182 /* Don't free entry->id! It is owned by the commit graph. */
1183 free(entry);
1186 static void
1187 free_commits(struct commit_queue *commits)
1189 while (!TAILQ_EMPTY(&commits->head))
1190 pop_commit(commits);
1193 static const struct got_error *
1194 match_commit(int *have_match, struct got_object_id *id,
1195 struct got_commit_object *commit, regex_t *regex)
1197 const struct got_error *err = NULL;
1198 regmatch_t regmatch;
1199 char *id_str = NULL, *logmsg = NULL;
1201 *have_match = 0;
1203 err = got_object_id_str(&id_str, id);
1204 if (err)
1205 return err;
1207 err = got_object_commit_get_logmsg(&logmsg, commit);
1208 if (err)
1209 goto done;
1211 if (regexec(regex, got_object_commit_get_author(commit), 1,
1212 &regmatch, 0) == 0 ||
1213 regexec(regex, got_object_commit_get_committer(commit), 1,
1214 &regmatch, 0) == 0 ||
1215 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1216 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1217 *have_match = 1;
1218 done:
1219 free(id_str);
1220 free(logmsg);
1221 return err;
1224 static const struct got_error *
1225 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1226 int minqueue, struct got_repository *repo, const char *path,
1227 int *searching, int *search_next_done, regex_t *regex)
1229 const struct got_error *err = NULL;
1230 int nqueued = 0, have_match = 0;
1233 * We keep all commits open throughout the lifetime of the log
1234 * view in order to avoid having to re-fetch commits from disk
1235 * while updating the display.
1237 while (nqueued < minqueue ||
1238 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1239 struct got_object_id *id;
1240 struct got_commit_object *commit;
1241 struct commit_queue_entry *entry;
1242 int errcode;
1244 err = got_commit_graph_iter_next(&id, graph);
1245 if (err) {
1246 if (err->code != GOT_ERR_ITER_NEED_MORE)
1247 break;
1248 err = got_commit_graph_fetch_commits(graph,
1249 minqueue, repo, NULL, NULL);
1250 if (err)
1251 return err;
1252 continue;
1255 if (id == NULL)
1256 break;
1258 err = got_object_open_as_commit(&commit, repo, id);
1259 if (err)
1260 break;
1261 entry = alloc_commit_queue_entry(commit, id);
1262 if (entry == NULL) {
1263 err = got_error_from_errno("alloc_commit_queue_entry");
1264 break;
1267 errcode = pthread_mutex_lock(&tog_mutex);
1268 if (errcode) {
1269 err = got_error_set_errno(errcode,
1270 "pthread_mutex_lock");
1271 break;
1274 entry->idx = commits->ncommits;
1275 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1276 nqueued++;
1277 commits->ncommits++;
1279 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1280 err = match_commit(&have_match, id, commit, regex);
1281 if (err) {
1282 pthread_mutex_lock(&tog_mutex);
1283 break;
1287 errcode = pthread_mutex_unlock(&tog_mutex);
1288 if (errcode && err == NULL)
1289 err = got_error_set_errno(errcode,
1290 "pthread_mutex_unlock");
1292 if (have_match)
1293 break;
1296 return err;
1299 static const struct got_error *
1300 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1301 struct got_repository *repo)
1303 const struct got_error *err = NULL;
1304 struct got_reference *head_ref;
1306 *head_id = NULL;
1308 err = got_ref_open(&head_ref, repo, branch_name, 0);
1309 if (err)
1310 return err;
1312 err = got_ref_resolve(head_id, repo, head_ref);
1313 got_ref_close(head_ref);
1314 if (err) {
1315 *head_id = NULL;
1316 return err;
1319 return NULL;
1322 static const struct got_error *
1323 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1324 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1325 struct commit_queue *commits, int selected_idx, int limit,
1326 struct got_reflist_head *refs, const char *path, int commits_needed)
1328 const struct got_error *err = NULL;
1329 struct tog_log_view_state *s = &view->state.log;
1330 struct commit_queue_entry *entry;
1331 int width;
1332 int ncommits, author_cols = 10;
1333 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1334 char *refs_str = NULL;
1335 wchar_t *wline;
1336 static const size_t date_display_cols = 9;
1338 entry = first;
1339 ncommits = 0;
1340 while (entry) {
1341 if (ncommits == selected_idx) {
1342 *selected = entry;
1343 break;
1345 entry = TAILQ_NEXT(entry, entry);
1346 ncommits++;
1349 if (*selected && !(view->searching && view->search_next_done == 0)) {
1350 err = got_object_id_str(&id_str, (*selected)->id);
1351 if (err)
1352 return err;
1353 if (refs) {
1354 err = build_refs_str(&refs_str, refs, (*selected)->id,
1355 s->repo);
1356 if (err)
1357 goto done;
1361 if (commits_needed == 0)
1362 halfdelay(10); /* disable fast refresh */
1364 if (asprintf(&ncommits_str, " [%d/%d] %s",
1365 entry ? entry->idx + 1 : 0, commits->ncommits,
1366 commits_needed > 0 ?
1367 (view->searching && view->search_next_done == 0
1368 ? "searching..." : "loading... ") :
1369 (refs_str ? refs_str : "")) == -1) {
1370 err = got_error_from_errno("asprintf");
1371 goto done;
1374 if (path && strcmp(path, "/") != 0) {
1375 if (asprintf(&header, "commit %s %s%s",
1376 id_str ? id_str : "........................................",
1377 path, ncommits_str) == -1) {
1378 err = got_error_from_errno("asprintf");
1379 header = NULL;
1380 goto done;
1382 } else if (asprintf(&header, "commit %s%s",
1383 id_str ? id_str : "........................................",
1384 ncommits_str) == -1) {
1385 err = got_error_from_errno("asprintf");
1386 header = NULL;
1387 goto done;
1389 err = format_line(&wline, &width, header, view->ncols, 0);
1390 if (err)
1391 goto done;
1393 werase(view->window);
1395 if (view_needs_focus_indication(view))
1396 wstandout(view->window);
1397 waddwstr(view->window, wline);
1398 while (width < view->ncols) {
1399 waddch(view->window, ' ');
1400 width++;
1402 if (view_needs_focus_indication(view))
1403 wstandend(view->window);
1404 free(wline);
1405 if (limit <= 1)
1406 goto done;
1408 /* Grow author column size if necessary. */
1409 entry = first;
1410 ncommits = 0;
1411 while (entry) {
1412 char *author;
1413 wchar_t *wauthor;
1414 int width;
1415 if (ncommits >= limit - 1)
1416 break;
1417 author = strdup(got_object_commit_get_author(entry->commit));
1418 if (author == NULL) {
1419 err = got_error_from_errno("strdup");
1420 goto done;
1422 err = format_author(&wauthor, &width, author, COLS,
1423 date_display_cols);
1424 if (author_cols < width)
1425 author_cols = width;
1426 free(wauthor);
1427 free(author);
1428 ncommits++;
1429 entry = TAILQ_NEXT(entry, entry);
1432 entry = first;
1433 *last = first;
1434 ncommits = 0;
1435 while (entry) {
1436 if (ncommits >= limit - 1)
1437 break;
1438 if (ncommits == selected_idx)
1439 wstandout(view->window);
1440 err = draw_commit(view, entry->commit, entry->id, refs,
1441 date_display_cols, author_cols);
1442 if (ncommits == selected_idx)
1443 wstandend(view->window);
1444 if (err)
1445 goto done;
1446 ncommits++;
1447 *last = entry;
1448 entry = TAILQ_NEXT(entry, entry);
1451 view_vborder(view);
1452 done:
1453 free(id_str);
1454 free(refs_str);
1455 free(ncommits_str);
1456 free(header);
1457 return err;
1460 static void
1461 scroll_up(struct tog_view *view,
1462 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1463 struct commit_queue *commits)
1465 struct commit_queue_entry *entry;
1466 int nscrolled = 0;
1468 entry = TAILQ_FIRST(&commits->head);
1469 if (*first_displayed_entry == entry)
1470 return;
1472 entry = *first_displayed_entry;
1473 while (entry && nscrolled < maxscroll) {
1474 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1475 if (entry) {
1476 *first_displayed_entry = entry;
1477 nscrolled++;
1482 static const struct got_error *
1483 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1484 pthread_cond_t *need_commits)
1486 int errcode;
1487 int max_wait = 20;
1489 halfdelay(1); /* fast refresh while loading commits */
1491 while (*commits_needed > 0) {
1492 if (*log_complete)
1493 break;
1495 /* Wake the log thread. */
1496 errcode = pthread_cond_signal(need_commits);
1497 if (errcode)
1498 return got_error_set_errno(errcode,
1499 "pthread_cond_signal");
1500 errcode = pthread_mutex_unlock(&tog_mutex);
1501 if (errcode)
1502 return got_error_set_errno(errcode,
1503 "pthread_mutex_unlock");
1504 pthread_yield();
1505 errcode = pthread_mutex_lock(&tog_mutex);
1506 if (errcode)
1507 return got_error_set_errno(errcode,
1508 "pthread_mutex_lock");
1510 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1512 * Thread is not done yet; lose a key press
1513 * and let the user retry... this way the GUI
1514 * remains interactive while logging deep paths
1515 * with few commits in history.
1517 return NULL;
1521 return NULL;
1524 static const struct got_error *
1525 scroll_down(struct tog_view *view,
1526 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1527 struct commit_queue_entry **last_displayed_entry,
1528 struct commit_queue *commits, int *log_complete, int *commits_needed,
1529 pthread_cond_t *need_commits)
1531 const struct got_error *err = NULL;
1532 struct commit_queue_entry *pentry;
1533 int nscrolled = 0;
1535 if (*last_displayed_entry == NULL)
1536 return NULL;
1538 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1539 if (pentry == NULL && !*log_complete) {
1541 * Ask the log thread for required amount of commits
1542 * plus some amount of pre-fetching.
1544 (*commits_needed) += maxscroll + 20;
1545 err = trigger_log_thread(0, commits_needed, log_complete,
1546 need_commits);
1547 if (err)
1548 return err;
1551 do {
1552 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1553 if (pentry == NULL)
1554 break;
1556 *last_displayed_entry = pentry;
1558 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1559 if (pentry == NULL)
1560 break;
1561 *first_displayed_entry = pentry;
1562 } while (++nscrolled < maxscroll);
1564 return err;
1567 static const struct got_error *
1568 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1569 struct got_commit_object *commit, struct got_object_id *commit_id,
1570 struct tog_view *log_view, struct got_reflist_head *refs,
1571 struct got_repository *repo)
1573 const struct got_error *err;
1574 struct got_object_qid *parent_id;
1575 struct tog_view *diff_view;
1577 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1578 if (diff_view == NULL)
1579 return got_error_from_errno("view_open");
1581 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1582 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1583 commit_id, log_view, refs, repo);
1584 if (err == NULL)
1585 *new_view = diff_view;
1586 return err;
1589 static const struct got_error *
1590 tree_view_visit_subtree(struct got_tree_object *subtree,
1591 struct tog_tree_view_state *s)
1593 struct tog_parent_tree *parent;
1595 parent = calloc(1, sizeof(*parent));
1596 if (parent == NULL)
1597 return got_error_from_errno("calloc");
1599 parent->tree = s->tree;
1600 parent->first_displayed_entry = s->first_displayed_entry;
1601 parent->selected_entry = s->selected_entry;
1602 parent->selected = s->selected;
1603 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1604 s->tree = subtree;
1605 s->entries = got_object_tree_get_entries(s->tree);
1606 s->selected = 0;
1607 s->first_displayed_entry = NULL;
1608 return NULL;
1612 static const struct got_error *
1613 browse_commit_tree(struct tog_view **new_view, int begin_x,
1614 struct commit_queue_entry *entry, const char *path,
1615 struct got_reflist_head *refs, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_tree_object *tree;
1619 struct got_tree_entry *te;
1620 struct tog_tree_view_state *s;
1621 struct tog_view *tree_view;
1622 char *slash, *subpath = NULL;
1623 const char *p;
1625 err = got_object_open_as_tree(&tree, repo,
1626 got_object_commit_get_tree_id(entry->commit));
1627 if (err)
1628 return err;
1630 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1631 if (tree_view == NULL)
1632 return got_error_from_errno("view_open");
1634 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1635 if (err) {
1636 got_object_tree_close(tree);
1637 return err;
1639 s = &tree_view->state.tree;
1641 *new_view = tree_view;
1643 /* Walk the path and open corresponding tree objects. */
1644 p = path;
1645 while (p[0] == '/')
1646 p++;
1647 while (*p) {
1648 struct got_object_id *tree_id;
1650 /* Ensure the correct subtree entry is selected. */
1651 slash = strchr(p, '/');
1652 if (slash == NULL)
1653 slash = strchr(p, '\0');
1654 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1655 if (strncmp(p, te->name, slash - p) == 0) {
1656 s->selected_entry = te;
1657 break;
1659 s->selected++;
1661 if (s->selected_entry == NULL) {
1662 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1663 break;
1665 if (s->tree != s->root)
1666 s->selected++; /* skip '..' */
1668 if (!S_ISDIR(s->selected_entry->mode)) {
1669 /* Jump to this file's entry. */
1670 s->first_displayed_entry = s->selected_entry;
1671 s->selected = 0;
1672 break;
1675 slash = strchr(p, '/');
1676 if (slash)
1677 subpath = strndup(path, slash - path);
1678 else
1679 subpath = strdup(path);
1680 if (subpath == NULL) {
1681 err = got_error_from_errno("strdup");
1682 break;
1685 err = got_object_id_by_path(&tree_id, repo, entry->id,
1686 subpath);
1687 if (err)
1688 break;
1690 err = got_object_open_as_tree(&tree, repo, tree_id);
1691 free(tree_id);
1692 if (err)
1693 break;
1695 err = tree_view_visit_subtree(tree, s);
1696 if (err) {
1697 got_object_tree_close(tree);
1698 break;
1700 if (slash == NULL)
1701 break;
1702 free(subpath);
1703 subpath = NULL;
1704 p = slash;
1707 free(subpath);
1708 return err;
1711 static void *
1712 log_thread(void *arg)
1714 const struct got_error *err = NULL;
1715 int errcode = 0;
1716 struct tog_log_thread_args *a = arg;
1717 int done = 0;
1719 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1720 NULL, NULL);
1721 if (err)
1722 return (void *)err;
1724 while (!done && !err && !tog_sigpipe_received) {
1725 err = queue_commits(a->graph, a->commits, 1, a->repo,
1726 a->in_repo_path, a->searching, a->search_next_done,
1727 a->regex);
1728 if (err) {
1729 if (err->code != GOT_ERR_ITER_COMPLETED)
1730 return (void *)err;
1731 err = NULL;
1732 done = 1;
1733 } else if (a->commits_needed > 0)
1734 a->commits_needed--;
1736 errcode = pthread_mutex_lock(&tog_mutex);
1737 if (errcode) {
1738 err = got_error_set_errno(errcode,
1739 "pthread_mutex_lock");
1740 break;
1741 } else if (*a->quit)
1742 done = 1;
1743 else if (*a->first_displayed_entry == NULL) {
1744 *a->first_displayed_entry =
1745 TAILQ_FIRST(&a->commits->head);
1746 *a->selected_entry = *a->first_displayed_entry;
1749 if (done)
1750 a->commits_needed = 0;
1751 else if (a->commits_needed == 0) {
1752 errcode = pthread_cond_wait(&a->need_commits,
1753 &tog_mutex);
1754 if (errcode)
1755 err = got_error_set_errno(errcode,
1756 "pthread_cond_wait");
1759 errcode = pthread_mutex_unlock(&tog_mutex);
1760 if (errcode && err == NULL)
1761 err = got_error_set_errno(errcode,
1762 "pthread_mutex_unlock");
1764 a->log_complete = 1;
1765 return (void *)err;
1768 static const struct got_error *
1769 stop_log_thread(struct tog_log_view_state *s)
1771 const struct got_error *err = NULL;
1772 int errcode;
1774 if (s->thread) {
1775 s->quit = 1;
1776 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1777 if (errcode)
1778 return got_error_set_errno(errcode,
1779 "pthread_cond_signal");
1780 errcode = pthread_mutex_unlock(&tog_mutex);
1781 if (errcode)
1782 return got_error_set_errno(errcode,
1783 "pthread_mutex_unlock");
1784 errcode = pthread_join(s->thread, (void **)&err);
1785 if (errcode)
1786 return got_error_set_errno(errcode, "pthread_join");
1787 errcode = pthread_mutex_lock(&tog_mutex);
1788 if (errcode)
1789 return got_error_set_errno(errcode,
1790 "pthread_mutex_lock");
1791 s->thread = NULL;
1794 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1795 if (errcode && err == NULL)
1796 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1798 if (s->thread_args.repo) {
1799 got_repo_close(s->thread_args.repo);
1800 s->thread_args.repo = NULL;
1803 if (s->thread_args.graph) {
1804 got_commit_graph_close(s->thread_args.graph);
1805 s->thread_args.graph = NULL;
1808 return err;
1811 static const struct got_error *
1812 close_log_view(struct tog_view *view)
1814 const struct got_error *err = NULL;
1815 struct tog_log_view_state *s = &view->state.log;
1817 err = stop_log_thread(s);
1818 free_commits(&s->commits);
1819 free(s->in_repo_path);
1820 s->in_repo_path = NULL;
1821 free(s->start_id);
1822 s->start_id = NULL;
1823 return err;
1826 static const struct got_error *
1827 search_start_log_view(struct tog_view *view)
1829 struct tog_log_view_state *s = &view->state.log;
1831 s->matched_entry = NULL;
1832 s->search_entry = NULL;
1833 return NULL;
1836 static const struct got_error *
1837 search_next_log_view(struct tog_view *view)
1839 const struct got_error *err = NULL;
1840 struct tog_log_view_state *s = &view->state.log;
1841 struct commit_queue_entry *entry;
1843 if (!view->searching) {
1844 view->search_next_done = 1;
1845 return NULL;
1848 if (s->search_entry) {
1849 int errcode, ch;
1850 errcode = pthread_mutex_unlock(&tog_mutex);
1851 if (errcode)
1852 return got_error_set_errno(errcode,
1853 "pthread_mutex_unlock");
1854 ch = wgetch(view->window);
1855 errcode = pthread_mutex_lock(&tog_mutex);
1856 if (errcode)
1857 return got_error_set_errno(errcode,
1858 "pthread_mutex_lock");
1859 if (ch == KEY_BACKSPACE) {
1860 view->search_next_done = 1;
1861 return NULL;
1863 if (view->searching == TOG_SEARCH_FORWARD)
1864 entry = TAILQ_NEXT(s->search_entry, entry);
1865 else
1866 entry = TAILQ_PREV(s->search_entry,
1867 commit_queue_head, entry);
1868 } else if (s->matched_entry) {
1869 if (view->searching == TOG_SEARCH_FORWARD)
1870 entry = TAILQ_NEXT(s->selected_entry, entry);
1871 else
1872 entry = TAILQ_PREV(s->selected_entry,
1873 commit_queue_head, entry);
1874 } else {
1875 if (view->searching == TOG_SEARCH_FORWARD)
1876 entry = TAILQ_FIRST(&s->commits.head);
1877 else
1878 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1881 while (1) {
1882 int have_match = 0;
1884 if (entry == NULL) {
1885 if (s->thread_args.log_complete ||
1886 view->searching == TOG_SEARCH_BACKWARD) {
1887 view->search_next_done = 1;
1888 return NULL;
1891 * Poke the log thread for more commits and return,
1892 * allowing the main loop to make progress. Search
1893 * will resume at s->search_entry once we come back.
1895 s->thread_args.commits_needed++;
1896 return trigger_log_thread(1,
1897 &s->thread_args.commits_needed,
1898 &s->thread_args.log_complete,
1899 &s->thread_args.need_commits);
1902 err = match_commit(&have_match, entry->id, entry->commit,
1903 &view->regex);
1904 if (err)
1905 break;
1906 if (have_match) {
1907 view->search_next_done = 1;
1908 s->matched_entry = entry;
1909 break;
1912 s->search_entry = entry;
1913 if (view->searching == TOG_SEARCH_FORWARD)
1914 entry = TAILQ_NEXT(entry, entry);
1915 else
1916 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1919 if (s->matched_entry) {
1920 int cur = s->selected_entry->idx;
1921 while (cur < s->matched_entry->idx) {
1922 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1923 if (err)
1924 return err;
1925 cur++;
1927 while (cur > s->matched_entry->idx) {
1928 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1929 if (err)
1930 return err;
1931 cur--;
1935 s->search_entry = NULL;
1937 return NULL;
1940 static const struct got_error *
1941 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1942 struct got_reflist_head *refs, struct got_repository *repo,
1943 const char *head_ref_name, const char *path, int check_disk)
1945 const struct got_error *err = NULL;
1946 struct tog_log_view_state *s = &view->state.log;
1947 struct got_repository *thread_repo = NULL;
1948 struct got_commit_graph *thread_graph = NULL;
1949 int errcode;
1951 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1952 if (err != NULL)
1953 goto done;
1955 /* The commit queue only contains commits being displayed. */
1956 TAILQ_INIT(&s->commits.head);
1957 s->commits.ncommits = 0;
1959 s->refs = refs;
1960 s->repo = repo;
1961 s->head_ref_name = head_ref_name;
1962 s->start_id = got_object_id_dup(start_id);
1963 if (s->start_id == NULL) {
1964 err = got_error_from_errno("got_object_id_dup");
1965 goto done;
1968 view->show = show_log_view;
1969 view->input = input_log_view;
1970 view->close = close_log_view;
1971 view->search_start = search_start_log_view;
1972 view->search_next = search_next_log_view;
1974 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1975 if (err)
1976 goto done;
1977 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1978 0, thread_repo);
1979 if (err)
1980 goto done;
1982 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1983 if (errcode) {
1984 err = got_error_set_errno(errcode, "pthread_cond_init");
1985 goto done;
1988 s->thread_args.commits_needed = view->nlines;
1989 s->thread_args.graph = thread_graph;
1990 s->thread_args.commits = &s->commits;
1991 s->thread_args.in_repo_path = s->in_repo_path;
1992 s->thread_args.start_id = s->start_id;
1993 s->thread_args.repo = thread_repo;
1994 s->thread_args.log_complete = 0;
1995 s->thread_args.quit = &s->quit;
1996 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1997 s->thread_args.selected_entry = &s->selected_entry;
1998 s->thread_args.searching = &view->searching;
1999 s->thread_args.search_next_done = &view->search_next_done;
2000 s->thread_args.regex = &view->regex;
2001 done:
2002 if (err)
2003 close_log_view(view);
2004 return err;
2007 static const struct got_error *
2008 show_log_view(struct tog_view *view)
2010 struct tog_log_view_state *s = &view->state.log;
2012 if (s->thread == NULL) {
2013 int errcode = pthread_create(&s->thread, NULL, log_thread,
2014 &s->thread_args);
2015 if (errcode)
2016 return got_error_set_errno(errcode, "pthread_create");
2019 return draw_commits(view, &s->last_displayed_entry,
2020 &s->selected_entry, s->first_displayed_entry,
2021 &s->commits, s->selected, view->nlines, s->refs,
2022 s->in_repo_path, s->thread_args.commits_needed);
2025 static const struct got_error *
2026 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2027 struct tog_view **focus_view, struct tog_view *view, int ch)
2029 const struct got_error *err = NULL;
2030 struct tog_log_view_state *s = &view->state.log;
2031 char *parent_path, *in_repo_path = NULL;
2032 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2033 int begin_x = 0;
2034 struct got_object_id *start_id;
2036 switch (ch) {
2037 case 'q':
2038 s->quit = 1;
2039 break;
2040 case 'k':
2041 case KEY_UP:
2042 case '<':
2043 case ',':
2044 if (s->first_displayed_entry == NULL)
2045 break;
2046 if (s->selected > 0)
2047 s->selected--;
2048 else
2049 scroll_up(view, &s->first_displayed_entry, 1,
2050 &s->commits);
2051 break;
2052 case KEY_PPAGE:
2053 case CTRL('b'):
2054 if (s->first_displayed_entry == NULL)
2055 break;
2056 if (TAILQ_FIRST(&s->commits.head) ==
2057 s->first_displayed_entry) {
2058 s->selected = 0;
2059 break;
2061 scroll_up(view, &s->first_displayed_entry,
2062 view->nlines, &s->commits);
2063 break;
2064 case 'j':
2065 case KEY_DOWN:
2066 case '>':
2067 case '.':
2068 if (s->first_displayed_entry == NULL)
2069 break;
2070 if (s->selected < MIN(view->nlines - 2,
2071 s->commits.ncommits - 1)) {
2072 s->selected++;
2073 break;
2075 err = scroll_down(view, &s->first_displayed_entry, 1,
2076 &s->last_displayed_entry, &s->commits,
2077 &s->thread_args.log_complete,
2078 &s->thread_args.commits_needed,
2079 &s->thread_args.need_commits);
2080 break;
2081 case KEY_NPAGE:
2082 case CTRL('f'): {
2083 struct commit_queue_entry *first;
2084 first = s->first_displayed_entry;
2085 if (first == NULL)
2086 break;
2087 err = scroll_down(view, &s->first_displayed_entry,
2088 view->nlines, &s->last_displayed_entry,
2089 &s->commits, &s->thread_args.log_complete,
2090 &s->thread_args.commits_needed,
2091 &s->thread_args.need_commits);
2092 if (err)
2093 break;
2094 if (first == s->first_displayed_entry &&
2095 s->selected < MIN(view->nlines - 2,
2096 s->commits.ncommits - 1)) {
2097 /* can't scroll further down */
2098 s->selected = MIN(view->nlines - 2,
2099 s->commits.ncommits - 1);
2101 err = NULL;
2102 break;
2104 case KEY_RESIZE:
2105 if (s->selected > view->nlines - 2)
2106 s->selected = view->nlines - 2;
2107 if (s->selected > s->commits.ncommits - 1)
2108 s->selected = s->commits.ncommits - 1;
2109 break;
2110 case KEY_ENTER:
2111 case ' ':
2112 case '\r':
2113 if (s->selected_entry == NULL)
2114 break;
2115 if (view_is_parent_view(view))
2116 begin_x = view_split_begin_x(view->begin_x);
2117 err = open_diff_view_for_commit(&diff_view, begin_x,
2118 s->selected_entry->commit, s->selected_entry->id,
2119 view, s->refs, s->repo);
2120 if (err)
2121 break;
2122 if (view_is_parent_view(view)) {
2123 err = view_close_child(view);
2124 if (err)
2125 return err;
2126 err = view_set_child(view, diff_view);
2127 if (err) {
2128 view_close(diff_view);
2129 break;
2131 *focus_view = diff_view;
2132 view->child_focussed = 1;
2133 } else
2134 *new_view = diff_view;
2135 break;
2136 case 't':
2137 if (s->selected_entry == NULL)
2138 break;
2139 if (view_is_parent_view(view))
2140 begin_x = view_split_begin_x(view->begin_x);
2141 err = browse_commit_tree(&tree_view, begin_x,
2142 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2143 if (err)
2144 break;
2145 if (view_is_parent_view(view)) {
2146 err = view_close_child(view);
2147 if (err)
2148 return err;
2149 err = view_set_child(view, tree_view);
2150 if (err) {
2151 view_close(tree_view);
2152 break;
2154 *focus_view = tree_view;
2155 view->child_focussed = 1;
2156 } else
2157 *new_view = tree_view;
2158 break;
2159 case KEY_BACKSPACE:
2160 if (strcmp(s->in_repo_path, "/") == 0)
2161 break;
2162 parent_path = dirname(s->in_repo_path);
2163 if (parent_path && strcmp(parent_path, ".") != 0) {
2164 err = stop_log_thread(s);
2165 if (err)
2166 return err;
2167 lv = view_open(view->nlines, view->ncols,
2168 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2169 if (lv == NULL)
2170 return got_error_from_errno(
2171 "view_open");
2172 err = open_log_view(lv, s->start_id, s->refs,
2173 s->repo, s->head_ref_name, parent_path, 0);
2174 if (err)
2175 return err;;
2176 if (view_is_parent_view(view))
2177 *new_view = lv;
2178 else {
2179 view_set_child(view->parent, lv);
2180 *focus_view = lv;
2182 return NULL;
2184 break;
2185 case CTRL('l'):
2186 err = stop_log_thread(s);
2187 if (err)
2188 return err;
2189 lv = view_open(view->nlines, view->ncols,
2190 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2191 if (lv == NULL)
2192 return got_error_from_errno("view_open");
2193 err = get_head_commit_id(&start_id, s->head_ref_name ?
2194 s->head_ref_name : GOT_REF_HEAD, s->repo);
2195 if (err) {
2196 view_close(lv);
2197 return err;
2199 in_repo_path = strdup(s->in_repo_path);
2200 if (in_repo_path == NULL) {
2201 free(start_id);
2202 view_close(lv);
2203 return got_error_from_errno("strdup");
2205 got_ref_list_free(s->refs);
2206 err = got_ref_list(s->refs, s->repo, NULL,
2207 got_ref_cmp_by_name, NULL);
2208 if (err) {
2209 free(start_id);
2210 view_close(lv);
2211 return err;
2213 err = open_log_view(lv, start_id, s->refs, s->repo,
2214 s->head_ref_name, in_repo_path, 0);
2215 if (err) {
2216 free(start_id);
2217 view_close(lv);
2218 return err;;
2220 *dead_view = view;
2221 *new_view = lv;
2222 break;
2223 default:
2224 break;
2227 return err;
2230 static const struct got_error *
2231 apply_unveil(const char *repo_path, const char *worktree_path)
2233 const struct got_error *error;
2235 #ifdef PROFILE
2236 if (unveil("gmon.out", "rwc") != 0)
2237 return got_error_from_errno2("unveil", "gmon.out");
2238 #endif
2239 if (repo_path && unveil(repo_path, "r") != 0)
2240 return got_error_from_errno2("unveil", repo_path);
2242 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2243 return got_error_from_errno2("unveil", worktree_path);
2245 if (unveil("/tmp", "rwc") != 0)
2246 return got_error_from_errno2("unveil", "/tmp");
2248 error = got_privsep_unveil_exec_helpers();
2249 if (error != NULL)
2250 return error;
2252 if (unveil(NULL, NULL) != 0)
2253 return got_error_from_errno("unveil");
2255 return NULL;
2258 static void
2259 init_curses(void)
2261 initscr();
2262 cbreak();
2263 halfdelay(1); /* Do fast refresh while initial view is loading. */
2264 noecho();
2265 nonl();
2266 intrflush(stdscr, FALSE);
2267 keypad(stdscr, TRUE);
2268 curs_set(0);
2269 if (getenv("TOG_COLORS") != NULL) {
2270 start_color();
2271 use_default_colors();
2273 signal(SIGWINCH, tog_sigwinch);
2274 signal(SIGPIPE, tog_sigpipe);
2277 static const struct got_error *
2278 cmd_log(int argc, char *argv[])
2280 const struct got_error *error;
2281 struct got_repository *repo = NULL;
2282 struct got_worktree *worktree = NULL;
2283 struct got_reflist_head refs;
2284 struct got_object_id *start_id = NULL;
2285 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2286 char *start_commit = NULL, *head_ref_name = NULL;
2287 int ch;
2288 struct tog_view *view;
2290 SIMPLEQ_INIT(&refs);
2292 #ifndef PROFILE
2293 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2294 NULL) == -1)
2295 err(1, "pledge");
2296 #endif
2298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2299 switch (ch) {
2300 case 'c':
2301 start_commit = optarg;
2302 break;
2303 case 'r':
2304 repo_path = realpath(optarg, NULL);
2305 if (repo_path == NULL)
2306 return got_error_from_errno2("realpath",
2307 optarg);
2308 break;
2309 default:
2310 usage_log();
2311 /* NOTREACHED */
2315 argc -= optind;
2316 argv += optind;
2318 cwd = getcwd(NULL, 0);
2319 if (cwd == NULL) {
2320 error = got_error_from_errno("getcwd");
2321 goto done;
2323 error = got_worktree_open(&worktree, cwd);
2324 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2325 goto done;
2326 error = NULL;
2328 if (argc == 0) {
2329 path = strdup("");
2330 if (path == NULL) {
2331 error = got_error_from_errno("strdup");
2332 goto done;
2334 } else if (argc == 1) {
2335 if (worktree) {
2336 error = got_worktree_resolve_path(&path, worktree,
2337 argv[0]);
2338 if (error)
2339 goto done;
2340 } else {
2341 path = strdup(argv[0]);
2342 if (path == NULL) {
2343 error = got_error_from_errno("strdup");
2344 goto done;
2347 } else
2348 usage_log();
2350 if (repo_path == NULL) {
2351 if (worktree)
2352 repo_path = strdup(
2353 got_worktree_get_repo_path(worktree));
2354 else
2355 repo_path = strdup(cwd);
2357 if (repo_path == NULL) {
2358 error = got_error_from_errno("strdup");
2359 goto done;
2362 init_curses();
2364 error = got_repo_open(&repo, repo_path, NULL);
2365 if (error != NULL)
2366 goto done;
2368 error = apply_unveil(got_repo_get_path(repo),
2369 worktree ? got_worktree_get_root_path(worktree) : NULL);
2370 if (error)
2371 goto done;
2373 if (start_commit == NULL)
2374 error = get_head_commit_id(&start_id, worktree ?
2375 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2376 repo);
2377 else {
2378 error = get_head_commit_id(&start_id, start_commit, repo);
2379 if (error) {
2380 if (error->code != GOT_ERR_NOT_REF)
2381 goto done;
2382 error = got_repo_match_object_id_prefix(&start_id,
2383 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2386 if (error != NULL)
2387 goto done;
2389 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2390 if (error)
2391 goto done;
2393 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2394 if (view == NULL) {
2395 error = got_error_from_errno("view_open");
2396 goto done;
2398 if (worktree) {
2399 head_ref_name = strdup(
2400 got_worktree_get_head_ref_name(worktree));
2401 if (head_ref_name == NULL) {
2402 error = got_error_from_errno("strdup");
2403 goto done;
2406 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2407 path, 1);
2408 if (error)
2409 goto done;
2410 if (worktree) {
2411 /* Release work tree lock. */
2412 got_worktree_close(worktree);
2413 worktree = NULL;
2415 error = view_loop(view);
2416 done:
2417 free(repo_path);
2418 free(cwd);
2419 free(path);
2420 free(start_id);
2421 free(head_ref_name);
2422 if (repo)
2423 got_repo_close(repo);
2424 if (worktree)
2425 got_worktree_close(worktree);
2426 got_ref_list_free(&refs);
2427 return error;
2430 __dead static void
2431 usage_diff(void)
2433 endwin();
2434 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2435 getprogname());
2436 exit(1);
2439 static char *
2440 parse_next_line(FILE *f, size_t *len)
2442 char *line;
2443 size_t linelen;
2444 size_t lineno;
2445 const char delim[3] = { '\0', '\0', '\0'};
2447 line = fparseln(f, &linelen, &lineno, delim, 0);
2448 if (len)
2449 *len = linelen;
2450 return line;
2453 static int
2454 match_line(const char *line, regex_t *regex)
2456 regmatch_t regmatch;
2458 return regexec(regex, line, 1, &regmatch, 0) == 0;
2461 struct tog_line_color *
2462 match_line_color(struct tog_line_colors *colors, const char *line)
2464 struct tog_line_color *tc = NULL;
2466 SIMPLEQ_FOREACH(tc, colors, entry) {
2467 if (match_line(line, &tc->regex))
2468 return tc;
2471 return NULL;
2474 static const struct got_error *
2475 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2476 int *last_displayed_line, int *eof, int max_lines, char *header,
2477 struct tog_line_colors *colors)
2479 const struct got_error *err;
2480 int nlines = 0, nprinted = 0;
2481 char *line;
2482 struct tog_line_color *lc;
2483 size_t len;
2484 wchar_t *wline;
2485 int width;
2487 rewind(f);
2488 werase(view->window);
2490 if (header) {
2491 err = format_line(&wline, &width, header, view->ncols, 0);
2492 if (err) {
2493 return err;
2496 if (view_needs_focus_indication(view))
2497 wstandout(view->window);
2498 waddwstr(view->window, wline);
2499 if (view_needs_focus_indication(view))
2500 wstandend(view->window);
2501 if (width <= view->ncols - 1)
2502 waddch(view->window, '\n');
2504 if (max_lines <= 1)
2505 return NULL;
2506 max_lines--;
2509 *eof = 0;
2510 while (nprinted < max_lines) {
2511 line = parse_next_line(f, &len);
2512 if (line == NULL) {
2513 *eof = 1;
2514 break;
2516 if (++nlines < *first_displayed_line) {
2517 free(line);
2518 continue;
2521 err = format_line(&wline, &width, line, view->ncols, 0);
2522 if (err) {
2523 free(line);
2524 return err;
2527 lc = match_line_color(colors, line);
2528 if (lc)
2529 wattr_on(view->window,
2530 COLOR_PAIR(lc->colorpair), NULL);
2531 waddwstr(view->window, wline);
2532 if (lc)
2533 wattr_off(view->window,
2534 COLOR_PAIR(lc->colorpair), NULL);
2535 if (width <= view->ncols - 1)
2536 waddch(view->window, '\n');
2537 if (++nprinted == 1)
2538 *first_displayed_line = nlines;
2539 free(line);
2540 free(wline);
2541 wline = NULL;
2543 *last_displayed_line = nlines;
2545 view_vborder(view);
2547 if (*eof) {
2548 while (nprinted < view->nlines) {
2549 waddch(view->window, '\n');
2550 nprinted++;
2553 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2554 if (err) {
2555 return err;
2558 wstandout(view->window);
2559 waddwstr(view->window, wline);
2560 wstandend(view->window);
2563 return NULL;
2566 static char *
2567 get_datestr(time_t *time, char *datebuf)
2569 struct tm mytm, *tm;
2570 char *p, *s;
2572 tm = gmtime_r(time, &mytm);
2573 if (tm == NULL)
2574 return NULL;
2575 s = asctime_r(tm, datebuf);
2576 if (s == NULL)
2577 return NULL;
2578 p = strchr(s, '\n');
2579 if (p)
2580 *p = '\0';
2581 return s;
2584 static const struct got_error *
2585 write_commit_info(struct got_object_id *commit_id,
2586 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2588 const struct got_error *err = NULL;
2589 char datebuf[26], *datestr;
2590 struct got_commit_object *commit;
2591 char *id_str = NULL, *logmsg = NULL;
2592 time_t committer_time;
2593 const char *author, *committer;
2594 char *refs_str = NULL;
2596 if (refs) {
2597 err = build_refs_str(&refs_str, refs, commit_id, repo);
2598 if (err)
2599 return err;
2602 err = got_object_open_as_commit(&commit, repo, commit_id);
2603 if (err)
2604 return err;
2606 err = got_object_id_str(&id_str, commit_id);
2607 if (err) {
2608 err = got_error_from_errno("got_object_id_str");
2609 goto done;
2612 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2613 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2614 err = got_error_from_errno("fprintf");
2615 goto done;
2617 if (fprintf(outfile, "from: %s\n",
2618 got_object_commit_get_author(commit)) < 0) {
2619 err = got_error_from_errno("fprintf");
2620 goto done;
2622 committer_time = got_object_commit_get_committer_time(commit);
2623 datestr = get_datestr(&committer_time, datebuf);
2624 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2625 err = got_error_from_errno("fprintf");
2626 goto done;
2628 author = got_object_commit_get_author(commit);
2629 committer = got_object_commit_get_committer(commit);
2630 if (strcmp(author, committer) != 0 &&
2631 fprintf(outfile, "via: %s\n", committer) < 0) {
2632 err = got_error_from_errno("fprintf");
2633 goto done;
2635 err = got_object_commit_get_logmsg(&logmsg, commit);
2636 if (err)
2637 goto done;
2638 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2639 err = got_error_from_errno("fprintf");
2640 goto done;
2642 done:
2643 free(id_str);
2644 free(logmsg);
2645 free(refs_str);
2646 got_object_commit_close(commit);
2647 return err;
2650 static const struct got_error *
2651 create_diff(struct tog_diff_view_state *s)
2653 const struct got_error *err = NULL;
2654 FILE *f = NULL;
2655 int obj_type;
2657 f = got_opentemp();
2658 if (f == NULL) {
2659 err = got_error_from_errno("got_opentemp");
2660 goto done;
2662 if (s->f && fclose(s->f) != 0) {
2663 err = got_error_from_errno("fclose");
2664 goto done;
2666 s->f = f;
2668 if (s->id1)
2669 err = got_object_get_type(&obj_type, s->repo, s->id1);
2670 else
2671 err = got_object_get_type(&obj_type, s->repo, s->id2);
2672 if (err)
2673 goto done;
2675 switch (obj_type) {
2676 case GOT_OBJ_TYPE_BLOB:
2677 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2678 s->diff_context, 0, s->repo, f);
2679 break;
2680 case GOT_OBJ_TYPE_TREE:
2681 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2682 s->diff_context, 0, s->repo, f);
2683 break;
2684 case GOT_OBJ_TYPE_COMMIT: {
2685 const struct got_object_id_queue *parent_ids;
2686 struct got_object_qid *pid;
2687 struct got_commit_object *commit2;
2689 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2690 if (err)
2691 break;
2692 /* Show commit info if we're diffing to a parent/root commit. */
2693 if (s->id1 == NULL)
2694 write_commit_info(s->id2, s->refs, s->repo, f);
2695 else {
2696 parent_ids = got_object_commit_get_parent_ids(commit2);
2697 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2698 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2699 write_commit_info(s->id2, s->refs,
2700 s->repo, f);
2701 break;
2705 got_object_commit_close(commit2);
2707 err = got_diff_objects_as_commits(s->id1, s->id2,
2708 s->diff_context, 0, s->repo, f);
2709 break;
2711 default:
2712 err = got_error(GOT_ERR_OBJ_TYPE);
2713 break;
2715 done:
2716 if (f && fflush(f) != 0 && err == NULL)
2717 err = got_error_from_errno("fflush");
2718 return err;
2721 static void
2722 diff_view_indicate_progress(struct tog_view *view)
2724 mvwaddstr(view->window, 0, 0, "diffing...");
2725 update_panels();
2726 doupdate();
2729 static const struct got_error *
2730 add_line_color(struct tog_line_colors *colors, const char *pattern,
2731 int idx, short color)
2733 const struct got_error *err = NULL;
2734 struct tog_line_color *lc;
2735 int regerr = 0;
2737 init_pair(idx, color, -1);
2739 lc = calloc(1, sizeof(*lc));
2740 if (lc == NULL)
2741 return got_error_from_errno("calloc");
2742 regerr = regcomp(&lc->regex, pattern,
2743 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
2744 if (regerr) {
2745 static char regerr_msg[512];
2746 static char err_msg[512];
2747 regerror(regerr, &lc->regex, regerr_msg,
2748 sizeof(regerr_msg));
2749 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
2750 regerr_msg);
2751 err = got_error_msg(GOT_ERR_REGEX, err_msg);
2752 free(lc);
2753 return err;
2755 lc->colorpair = idx;
2756 SIMPLEQ_INSERT_HEAD(colors, lc, entry);
2757 return NULL;
2760 static void
2761 free_line_colors(struct tog_line_colors *colors)
2763 struct tog_line_color *lc;
2765 while (!SIMPLEQ_EMPTY(colors)) {
2766 lc = SIMPLEQ_FIRST(colors);
2767 SIMPLEQ_REMOVE_HEAD(colors, entry);
2768 regfree(&lc->regex);
2769 free(lc);
2773 static int
2774 default_color_value(const char *envvar)
2776 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
2777 return COLOR_MAGENTA;
2778 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
2779 return COLOR_CYAN;
2780 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
2781 return COLOR_YELLOW;
2782 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
2783 return COLOR_GREEN;
2784 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
2785 return COLOR_MAGENTA;
2786 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
2787 return COLOR_CYAN;
2788 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
2789 return COLOR_BLUE;
2790 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
2791 return COLOR_GREEN;
2793 return -1;
2796 static int
2797 get_color_value(const char *envvar)
2799 const char *val = getenv(envvar);
2801 if (val == NULL)
2802 return default_color_value(envvar);
2804 if (strcasecmp(val, "black") == 0)
2805 return COLOR_BLACK;
2806 if (strcasecmp(val, "red") == 0)
2807 return COLOR_RED;
2808 if (strcasecmp(val, "green") == 0)
2809 return COLOR_GREEN;
2810 if (strcasecmp(val, "yellow") == 0)
2811 return COLOR_YELLOW;
2812 if (strcasecmp(val, "blue") == 0)
2813 return COLOR_BLUE;
2814 if (strcasecmp(val, "magenta") == 0)
2815 return COLOR_MAGENTA;
2816 if (strcasecmp(val, "cyan") == 0)
2817 return COLOR_CYAN;
2818 if (strcasecmp(val, "white") == 0)
2819 return COLOR_WHITE;
2821 return default_color_value(envvar);
2824 static const struct got_error *
2825 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2826 struct got_object_id *id2, struct tog_view *log_view,
2827 struct got_reflist_head *refs, struct got_repository *repo)
2829 const struct got_error *err;
2831 if (id1 != NULL && id2 != NULL) {
2832 int type1, type2;
2833 err = got_object_get_type(&type1, repo, id1);
2834 if (err)
2835 return err;
2836 err = got_object_get_type(&type2, repo, id2);
2837 if (err)
2838 return err;
2840 if (type1 != type2)
2841 return got_error(GOT_ERR_OBJ_TYPE);
2844 if (id1) {
2845 view->state.diff.id1 = got_object_id_dup(id1);
2846 if (view->state.diff.id1 == NULL)
2847 return got_error_from_errno("got_object_id_dup");
2848 } else
2849 view->state.diff.id1 = NULL;
2851 view->state.diff.id2 = got_object_id_dup(id2);
2852 if (view->state.diff.id2 == NULL) {
2853 free(view->state.diff.id1);
2854 view->state.diff.id1 = NULL;
2855 return got_error_from_errno("got_object_id_dup");
2857 view->state.diff.f = NULL;
2858 view->state.diff.first_displayed_line = 1;
2859 view->state.diff.last_displayed_line = view->nlines;
2860 view->state.diff.diff_context = 3;
2861 view->state.diff.log_view = log_view;
2862 view->state.diff.repo = repo;
2863 view->state.diff.refs = refs;
2864 SIMPLEQ_INIT(&view->state.diff.line_colors);
2866 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2867 err = add_line_color(&view->state.diff.line_colors,
2868 "^-", 1, get_color_value("TOG_COLOR_DIFF_MINUS"));
2869 if (err)
2870 return err;
2871 err = add_line_color(&view->state.diff.line_colors,
2872 "^\\+", 2, get_color_value("TOG_COLOR_DIFF_PLUS"));
2873 if (err) {
2874 free_line_colors(&view->state.diff.line_colors);
2875 return err;
2877 err = add_line_color(&view->state.diff.line_colors,
2878 "^@@", 3,
2879 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2880 if (err) {
2881 free_line_colors(&view->state.diff.line_colors);
2882 return err;
2885 err = add_line_color(&view->state.diff.line_colors,
2886 "^(commit|(blob|file) [-+] )", 4,
2887 get_color_value("TOG_COLOR_DIFF_META"));
2888 if (err) {
2889 free_line_colors(&view->state.diff.line_colors);
2890 return err;
2894 if (log_view && view_is_splitscreen(view))
2895 show_log_view(log_view); /* draw vborder */
2896 diff_view_indicate_progress(view);
2898 err = create_diff(&view->state.diff);
2899 if (err) {
2900 free(view->state.diff.id1);
2901 view->state.diff.id1 = NULL;
2902 free(view->state.diff.id2);
2903 view->state.diff.id2 = NULL;
2904 return err;
2907 view->show = show_diff_view;
2908 view->input = input_diff_view;
2909 view->close = close_diff_view;
2911 return NULL;
2914 static const struct got_error *
2915 close_diff_view(struct tog_view *view)
2917 const struct got_error *err = NULL;
2919 free(view->state.diff.id1);
2920 view->state.diff.id1 = NULL;
2921 free(view->state.diff.id2);
2922 view->state.diff.id2 = NULL;
2923 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2924 err = got_error_from_errno("fclose");
2925 free_line_colors(&view->state.diff.line_colors);
2926 return err;
2929 static const struct got_error *
2930 show_diff_view(struct tog_view *view)
2932 const struct got_error *err;
2933 struct tog_diff_view_state *s = &view->state.diff;
2934 char *id_str1 = NULL, *id_str2, *header;
2936 if (s->id1) {
2937 err = got_object_id_str(&id_str1, s->id1);
2938 if (err)
2939 return err;
2941 err = got_object_id_str(&id_str2, s->id2);
2942 if (err)
2943 return err;
2945 if (asprintf(&header, "diff %s %s",
2946 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2947 err = got_error_from_errno("asprintf");
2948 free(id_str1);
2949 free(id_str2);
2950 return err;
2952 free(id_str1);
2953 free(id_str2);
2955 return draw_file(view, s->f, &s->first_displayed_line,
2956 &s->last_displayed_line, &s->eof, view->nlines,
2957 header, &s->line_colors);
2960 static const struct got_error *
2961 set_selected_commit(struct tog_diff_view_state *s,
2962 struct commit_queue_entry *entry)
2964 const struct got_error *err;
2965 const struct got_object_id_queue *parent_ids;
2966 struct got_commit_object *selected_commit;
2967 struct got_object_qid *pid;
2969 free(s->id2);
2970 s->id2 = got_object_id_dup(entry->id);
2971 if (s->id2 == NULL)
2972 return got_error_from_errno("got_object_id_dup");
2974 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2975 if (err)
2976 return err;
2977 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2978 free(s->id1);
2979 pid = SIMPLEQ_FIRST(parent_ids);
2980 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2981 got_object_commit_close(selected_commit);
2982 return NULL;
2985 static const struct got_error *
2986 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2987 struct tog_view **focus_view, struct tog_view *view, int ch)
2989 const struct got_error *err = NULL;
2990 struct tog_diff_view_state *s = &view->state.diff;
2991 struct tog_log_view_state *ls;
2992 struct commit_queue_entry *entry;
2993 int i;
2995 switch (ch) {
2996 case 'k':
2997 case KEY_UP:
2998 if (s->first_displayed_line > 1)
2999 s->first_displayed_line--;
3000 break;
3001 case KEY_PPAGE:
3002 case CTRL('b'):
3003 if (s->first_displayed_line == 1)
3004 break;
3005 i = 0;
3006 while (i++ < view->nlines - 1 &&
3007 s->first_displayed_line > 1)
3008 s->first_displayed_line--;
3009 break;
3010 case 'j':
3011 case KEY_DOWN:
3012 if (!s->eof)
3013 s->first_displayed_line++;
3014 break;
3015 case KEY_NPAGE:
3016 case CTRL('f'):
3017 case ' ':
3018 if (s->eof)
3019 break;
3020 i = 0;
3021 while (!s->eof && i++ < view->nlines - 1) {
3022 char *line;
3023 line = parse_next_line(s->f, NULL);
3024 s->first_displayed_line++;
3025 if (line == NULL)
3026 break;
3028 break;
3029 case '[':
3030 if (s->diff_context > 0) {
3031 s->diff_context--;
3032 diff_view_indicate_progress(view);
3033 err = create_diff(s);
3035 break;
3036 case ']':
3037 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3038 s->diff_context++;
3039 diff_view_indicate_progress(view);
3040 err = create_diff(s);
3042 break;
3043 case '<':
3044 case ',':
3045 if (s->log_view == NULL)
3046 break;
3047 ls = &s->log_view->state.log;
3048 entry = TAILQ_PREV(ls->selected_entry,
3049 commit_queue_head, entry);
3050 if (entry == NULL)
3051 break;
3053 err = input_log_view(NULL, NULL, NULL, s->log_view,
3054 KEY_UP);
3055 if (err)
3056 break;
3058 err = set_selected_commit(s, entry);
3059 if (err)
3060 break;
3062 s->first_displayed_line = 1;
3063 s->last_displayed_line = view->nlines;
3065 diff_view_indicate_progress(view);
3066 err = create_diff(s);
3067 break;
3068 case '>':
3069 case '.':
3070 if (s->log_view == NULL)
3071 break;
3072 ls = &s->log_view->state.log;
3074 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3075 ls->thread_args.commits_needed++;
3077 /* Display "loading..." in log view. */
3078 show_log_view(s->log_view);
3079 update_panels();
3080 doupdate();
3082 err = trigger_log_thread(1 /* load_all */,
3083 &ls->thread_args.commits_needed,
3084 &ls->thread_args.log_complete,
3085 &ls->thread_args.need_commits);
3086 if (err)
3087 break;
3089 err = input_log_view(NULL, NULL, NULL, s->log_view,
3090 KEY_DOWN);
3091 if (err)
3092 break;
3094 entry = TAILQ_NEXT(ls->selected_entry, entry);
3095 if (entry == NULL)
3096 break;
3098 err = set_selected_commit(s, entry);
3099 if (err)
3100 break;
3102 s->first_displayed_line = 1;
3103 s->last_displayed_line = view->nlines;
3105 diff_view_indicate_progress(view);
3106 err = create_diff(s);
3107 break;
3108 default:
3109 break;
3112 return err;
3115 static const struct got_error *
3116 cmd_diff(int argc, char *argv[])
3118 const struct got_error *error = NULL;
3119 struct got_repository *repo = NULL;
3120 struct got_reflist_head refs;
3121 struct got_object_id *id1 = NULL, *id2 = NULL;
3122 char *repo_path = NULL;
3123 char *id_str1 = NULL, *id_str2 = NULL;
3124 int ch;
3125 struct tog_view *view;
3127 SIMPLEQ_INIT(&refs);
3129 #ifndef PROFILE
3130 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3131 NULL) == -1)
3132 err(1, "pledge");
3133 #endif
3135 while ((ch = getopt(argc, argv, "")) != -1) {
3136 switch (ch) {
3137 default:
3138 usage_diff();
3139 /* NOTREACHED */
3143 argc -= optind;
3144 argv += optind;
3146 if (argc == 0) {
3147 usage_diff(); /* TODO show local worktree changes */
3148 } else if (argc == 2) {
3149 repo_path = getcwd(NULL, 0);
3150 if (repo_path == NULL)
3151 return got_error_from_errno("getcwd");
3152 id_str1 = argv[0];
3153 id_str2 = argv[1];
3154 } else if (argc == 3) {
3155 repo_path = realpath(argv[0], NULL);
3156 if (repo_path == NULL)
3157 return got_error_from_errno2("realpath", argv[0]);
3158 id_str1 = argv[1];
3159 id_str2 = argv[2];
3160 } else
3161 usage_diff();
3163 init_curses();
3165 error = got_repo_open(&repo, repo_path, NULL);
3166 if (error)
3167 goto done;
3169 error = apply_unveil(got_repo_get_path(repo), NULL);
3170 if (error)
3171 goto done;
3173 error = got_repo_match_object_id_prefix(&id1, id_str1,
3174 GOT_OBJ_TYPE_ANY, repo);
3175 if (error)
3176 goto done;
3178 error = got_repo_match_object_id_prefix(&id2, id_str2,
3179 GOT_OBJ_TYPE_ANY, repo);
3180 if (error)
3181 goto done;
3183 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3184 if (error)
3185 goto done;
3187 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3188 if (view == NULL) {
3189 error = got_error_from_errno("view_open");
3190 goto done;
3192 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3193 if (error)
3194 goto done;
3195 error = view_loop(view);
3196 done:
3197 free(repo_path);
3198 if (repo)
3199 got_repo_close(repo);
3200 got_ref_list_free(&refs);
3201 return error;
3204 __dead static void
3205 usage_blame(void)
3207 endwin();
3208 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3209 getprogname());
3210 exit(1);
3213 struct tog_blame_line {
3214 int annotated;
3215 struct got_object_id *id;
3218 static const struct got_error *
3219 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3220 const char *path, struct tog_blame_line *lines, int nlines,
3221 int blame_complete, int selected_line, int *first_displayed_line,
3222 int *last_displayed_line, int *eof, int max_lines)
3224 const struct got_error *err;
3225 int lineno = 0, nprinted = 0;
3226 char *line;
3227 size_t len;
3228 wchar_t *wline;
3229 int width;
3230 struct tog_blame_line *blame_line;
3231 struct got_object_id *prev_id = NULL;
3232 char *id_str;
3234 err = got_object_id_str(&id_str, id);
3235 if (err)
3236 return err;
3238 rewind(f);
3239 werase(view->window);
3241 if (asprintf(&line, "commit %s", id_str) == -1) {
3242 err = got_error_from_errno("asprintf");
3243 free(id_str);
3244 return err;
3247 err = format_line(&wline, &width, line, view->ncols, 0);
3248 free(line);
3249 line = NULL;
3250 if (err)
3251 return err;
3252 if (view_needs_focus_indication(view))
3253 wstandout(view->window);
3254 waddwstr(view->window, wline);
3255 if (view_needs_focus_indication(view))
3256 wstandend(view->window);
3257 free(wline);
3258 wline = NULL;
3259 if (width < view->ncols - 1)
3260 waddch(view->window, '\n');
3262 if (asprintf(&line, "[%d/%d] %s%s",
3263 *first_displayed_line - 1 + selected_line, nlines,
3264 blame_complete ? "" : "annotating... ", path) == -1) {
3265 free(id_str);
3266 return got_error_from_errno("asprintf");
3268 free(id_str);
3269 err = format_line(&wline, &width, line, view->ncols, 0);
3270 free(line);
3271 line = NULL;
3272 if (err)
3273 return err;
3274 waddwstr(view->window, wline);
3275 free(wline);
3276 wline = NULL;
3277 if (width < view->ncols - 1)
3278 waddch(view->window, '\n');
3280 *eof = 0;
3281 while (nprinted < max_lines - 2) {
3282 line = parse_next_line(f, &len);
3283 if (line == NULL) {
3284 *eof = 1;
3285 break;
3287 if (++lineno < *first_displayed_line) {
3288 free(line);
3289 continue;
3292 if (view->ncols <= 9) {
3293 width = 9;
3294 wline = wcsdup(L"");
3295 if (wline == NULL)
3296 err = got_error_from_errno("wcsdup");
3297 } else {
3298 err = format_line(&wline, &width, line,
3299 view->ncols - 9, 9);
3300 width += 9;
3302 if (err) {
3303 free(line);
3304 return err;
3307 if (view->focussed && nprinted == selected_line - 1)
3308 wstandout(view->window);
3310 if (nlines > 0) {
3311 blame_line = &lines[lineno - 1];
3312 if (blame_line->annotated && prev_id &&
3313 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3314 !(view->focussed &&
3315 nprinted == selected_line - 1)) {
3316 waddstr(view->window, " ");
3317 } else if (blame_line->annotated) {
3318 char *id_str;
3319 err = got_object_id_str(&id_str, blame_line->id);
3320 if (err) {
3321 free(line);
3322 free(wline);
3323 return err;
3325 wprintw(view->window, "%.8s", id_str);
3326 free(id_str);
3327 prev_id = blame_line->id;
3328 } else {
3329 waddstr(view->window, "........");
3330 prev_id = NULL;
3332 } else {
3333 waddstr(view->window, "........");
3334 prev_id = NULL;
3337 if (view->focussed && nprinted == selected_line - 1)
3338 wstandend(view->window);
3339 waddstr(view->window, " ");
3341 waddwstr(view->window, wline);
3342 if (width <= view->ncols - 1)
3343 waddch(view->window, '\n');
3344 if (++nprinted == 1)
3345 *first_displayed_line = lineno;
3346 free(line);
3347 free(wline);
3348 wline = NULL;
3350 *last_displayed_line = lineno;
3352 view_vborder(view);
3354 return NULL;
3357 static const struct got_error *
3358 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3360 const struct got_error *err = NULL;
3361 struct tog_blame_cb_args *a = arg;
3362 struct tog_blame_line *line;
3363 int errcode;
3365 if (nlines != a->nlines ||
3366 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3367 return got_error(GOT_ERR_RANGE);
3369 errcode = pthread_mutex_lock(&tog_mutex);
3370 if (errcode)
3371 return got_error_set_errno(errcode, "pthread_mutex_lock");
3373 if (*a->quit) { /* user has quit the blame view */
3374 err = got_error(GOT_ERR_ITER_COMPLETED);
3375 goto done;
3378 if (lineno == -1)
3379 goto done; /* no change in this commit */
3381 line = &a->lines[lineno - 1];
3382 if (line->annotated)
3383 goto done;
3385 line->id = got_object_id_dup(id);
3386 if (line->id == NULL) {
3387 err = got_error_from_errno("got_object_id_dup");
3388 goto done;
3390 line->annotated = 1;
3391 done:
3392 errcode = pthread_mutex_unlock(&tog_mutex);
3393 if (errcode)
3394 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3395 return err;
3398 static void *
3399 blame_thread(void *arg)
3401 const struct got_error *err;
3402 struct tog_blame_thread_args *ta = arg;
3403 struct tog_blame_cb_args *a = ta->cb_args;
3404 int errcode;
3406 err = got_blame(ta->path, a->commit_id, ta->repo,
3407 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3408 if (err && err->code == GOT_ERR_CANCELLED)
3409 err = NULL;
3411 errcode = pthread_mutex_lock(&tog_mutex);
3412 if (errcode)
3413 return (void *)got_error_set_errno(errcode,
3414 "pthread_mutex_lock");
3416 got_repo_close(ta->repo);
3417 ta->repo = NULL;
3418 *ta->complete = 1;
3420 errcode = pthread_mutex_unlock(&tog_mutex);
3421 if (errcode && err == NULL)
3422 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3424 return (void *)err;
3427 static struct got_object_id *
3428 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3429 int first_displayed_line, int selected_line)
3431 struct tog_blame_line *line;
3433 if (nlines <= 0)
3434 return NULL;
3436 line = &lines[first_displayed_line - 1 + selected_line - 1];
3437 if (!line->annotated)
3438 return NULL;
3440 return line->id;
3443 static const struct got_error *
3444 stop_blame(struct tog_blame *blame)
3446 const struct got_error *err = NULL;
3447 int i;
3449 if (blame->thread) {
3450 int errcode;
3451 errcode = pthread_mutex_unlock(&tog_mutex);
3452 if (errcode)
3453 return got_error_set_errno(errcode,
3454 "pthread_mutex_unlock");
3455 errcode = pthread_join(blame->thread, (void **)&err);
3456 if (errcode)
3457 return got_error_set_errno(errcode, "pthread_join");
3458 errcode = pthread_mutex_lock(&tog_mutex);
3459 if (errcode)
3460 return got_error_set_errno(errcode,
3461 "pthread_mutex_lock");
3462 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3463 err = NULL;
3464 blame->thread = NULL;
3466 if (blame->thread_args.repo) {
3467 got_repo_close(blame->thread_args.repo);
3468 blame->thread_args.repo = NULL;
3470 if (blame->f) {
3471 if (fclose(blame->f) != 0 && err == NULL)
3472 err = got_error_from_errno("fclose");
3473 blame->f = NULL;
3475 if (blame->lines) {
3476 for (i = 0; i < blame->nlines; i++)
3477 free(blame->lines[i].id);
3478 free(blame->lines);
3479 blame->lines = NULL;
3481 free(blame->cb_args.commit_id);
3482 blame->cb_args.commit_id = NULL;
3484 return err;
3487 static const struct got_error *
3488 cancel_blame_view(void *arg)
3490 const struct got_error *err = NULL;
3491 int *done = arg;
3492 int errcode;
3494 errcode = pthread_mutex_lock(&tog_mutex);
3495 if (errcode)
3496 return got_error_set_errno(errcode,
3497 "pthread_mutex_unlock");
3499 if (*done)
3500 err = got_error(GOT_ERR_CANCELLED);
3502 errcode = pthread_mutex_unlock(&tog_mutex);
3503 if (errcode)
3504 return got_error_set_errno(errcode,
3505 "pthread_mutex_lock");
3507 return err;
3510 static const struct got_error *
3511 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3512 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3513 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3514 struct got_repository *repo)
3516 const struct got_error *err = NULL;
3517 struct got_blob_object *blob = NULL;
3518 struct got_repository *thread_repo = NULL;
3519 struct got_object_id *obj_id = NULL;
3520 int obj_type;
3522 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3523 if (err)
3524 return err;
3525 if (obj_id == NULL)
3526 return got_error(GOT_ERR_NO_OBJ);
3528 err = got_object_get_type(&obj_type, repo, obj_id);
3529 if (err)
3530 goto done;
3532 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3533 err = got_error(GOT_ERR_OBJ_TYPE);
3534 goto done;
3537 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3538 if (err)
3539 goto done;
3540 blame->f = got_opentemp();
3541 if (blame->f == NULL) {
3542 err = got_error_from_errno("got_opentemp");
3543 goto done;
3545 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3546 &blame->line_offsets, blame->f, blob);
3547 if (err || blame->nlines == 0)
3548 goto done;
3550 /* Don't include \n at EOF in the blame line count. */
3551 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3552 blame->nlines--;
3554 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3555 if (blame->lines == NULL) {
3556 err = got_error_from_errno("calloc");
3557 goto done;
3560 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3561 if (err)
3562 goto done;
3564 blame->cb_args.view = view;
3565 blame->cb_args.lines = blame->lines;
3566 blame->cb_args.nlines = blame->nlines;
3567 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3568 if (blame->cb_args.commit_id == NULL) {
3569 err = got_error_from_errno("got_object_id_dup");
3570 goto done;
3572 blame->cb_args.quit = done;
3574 blame->thread_args.path = path;
3575 blame->thread_args.repo = thread_repo;
3576 blame->thread_args.cb_args = &blame->cb_args;
3577 blame->thread_args.complete = blame_complete;
3578 blame->thread_args.cancel_cb = cancel_blame_view;
3579 blame->thread_args.cancel_arg = done;
3580 *blame_complete = 0;
3582 done:
3583 if (blob)
3584 got_object_blob_close(blob);
3585 free(obj_id);
3586 if (err)
3587 stop_blame(blame);
3588 return err;
3591 static const struct got_error *
3592 open_blame_view(struct tog_view *view, char *path,
3593 struct got_object_id *commit_id, struct got_reflist_head *refs,
3594 struct got_repository *repo)
3596 const struct got_error *err = NULL;
3597 struct tog_blame_view_state *s = &view->state.blame;
3599 SIMPLEQ_INIT(&s->blamed_commits);
3601 s->path = strdup(path);
3602 if (s->path == NULL)
3603 return got_error_from_errno("strdup");
3605 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3606 if (err) {
3607 free(s->path);
3608 return err;
3611 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3612 s->first_displayed_line = 1;
3613 s->last_displayed_line = view->nlines;
3614 s->selected_line = 1;
3615 s->blame_complete = 0;
3616 s->repo = repo;
3617 s->refs = refs;
3618 s->commit_id = commit_id;
3619 memset(&s->blame, 0, sizeof(s->blame));
3621 view->show = show_blame_view;
3622 view->input = input_blame_view;
3623 view->close = close_blame_view;
3624 view->search_start = search_start_blame_view;
3625 view->search_next = search_next_blame_view;
3627 return run_blame(&s->blame, view, &s->blame_complete,
3628 &s->first_displayed_line, &s->last_displayed_line,
3629 &s->selected_line, &s->done, &s->eof, s->path,
3630 s->blamed_commit->id, s->repo);
3633 static const struct got_error *
3634 close_blame_view(struct tog_view *view)
3636 const struct got_error *err = NULL;
3637 struct tog_blame_view_state *s = &view->state.blame;
3639 if (s->blame.thread)
3640 err = stop_blame(&s->blame);
3642 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3643 struct got_object_qid *blamed_commit;
3644 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3645 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3646 got_object_qid_free(blamed_commit);
3649 free(s->path);
3651 return err;
3654 static const struct got_error *
3655 search_start_blame_view(struct tog_view *view)
3657 struct tog_blame_view_state *s = &view->state.blame;
3659 s->matched_line = 0;
3660 return NULL;
3663 static const struct got_error *
3664 search_next_blame_view(struct tog_view *view)
3666 struct tog_blame_view_state *s = &view->state.blame;
3667 int lineno;
3669 if (!view->searching) {
3670 view->search_next_done = 1;
3671 return NULL;
3674 if (s->matched_line) {
3675 if (view->searching == TOG_SEARCH_FORWARD)
3676 lineno = s->matched_line + 1;
3677 else
3678 lineno = s->matched_line - 1;
3679 } else {
3680 if (view->searching == TOG_SEARCH_FORWARD)
3681 lineno = 1;
3682 else
3683 lineno = s->blame.nlines;
3686 while (1) {
3687 char *line = NULL;
3688 off_t offset;
3689 size_t len;
3691 if (lineno <= 0 || lineno > s->blame.nlines) {
3692 if (s->matched_line == 0) {
3693 view->search_next_done = 1;
3694 free(line);
3695 break;
3698 if (view->searching == TOG_SEARCH_FORWARD)
3699 lineno = 1;
3700 else
3701 lineno = s->blame.nlines;
3704 offset = s->blame.line_offsets[lineno - 1];
3705 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3706 free(line);
3707 return got_error_from_errno("fseeko");
3709 free(line);
3710 line = parse_next_line(s->blame.f, &len);
3711 if (line && match_line(line, &view->regex)) {
3712 view->search_next_done = 1;
3713 s->matched_line = lineno;
3714 free(line);
3715 break;
3717 free(line);
3718 if (view->searching == TOG_SEARCH_FORWARD)
3719 lineno++;
3720 else
3721 lineno--;
3724 if (s->matched_line) {
3725 s->first_displayed_line = s->matched_line;
3726 s->selected_line = 1;
3729 return NULL;
3732 static const struct got_error *
3733 show_blame_view(struct tog_view *view)
3735 const struct got_error *err = NULL;
3736 struct tog_blame_view_state *s = &view->state.blame;
3737 int errcode;
3739 if (s->blame.thread == NULL) {
3740 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3741 &s->blame.thread_args);
3742 if (errcode)
3743 return got_error_set_errno(errcode, "pthread_create");
3745 halfdelay(1); /* fast refresh while annotating */
3748 if (s->blame_complete)
3749 halfdelay(10); /* disable fast refresh */
3751 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3752 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3753 s->selected_line, &s->first_displayed_line,
3754 &s->last_displayed_line, &s->eof, view->nlines);
3756 view_vborder(view);
3757 return err;
3760 static const struct got_error *
3761 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3762 struct tog_view **focus_view, struct tog_view *view, int ch)
3764 const struct got_error *err = NULL, *thread_err = NULL;
3765 struct tog_view *diff_view;
3766 struct tog_blame_view_state *s = &view->state.blame;
3767 int begin_x = 0;
3769 switch (ch) {
3770 case 'q':
3771 s->done = 1;
3772 break;
3773 case 'k':
3774 case KEY_UP:
3775 if (s->selected_line > 1)
3776 s->selected_line--;
3777 else if (s->selected_line == 1 &&
3778 s->first_displayed_line > 1)
3779 s->first_displayed_line--;
3780 break;
3781 case KEY_PPAGE:
3782 if (s->first_displayed_line == 1) {
3783 s->selected_line = 1;
3784 break;
3786 if (s->first_displayed_line > view->nlines - 2)
3787 s->first_displayed_line -=
3788 (view->nlines - 2);
3789 else
3790 s->first_displayed_line = 1;
3791 break;
3792 case 'j':
3793 case KEY_DOWN:
3794 if (s->selected_line < view->nlines - 2 &&
3795 s->first_displayed_line +
3796 s->selected_line <= s->blame.nlines)
3797 s->selected_line++;
3798 else if (s->last_displayed_line <
3799 s->blame.nlines)
3800 s->first_displayed_line++;
3801 break;
3802 case 'b':
3803 case 'p': {
3804 struct got_object_id *id = NULL;
3805 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3806 s->first_displayed_line, s->selected_line);
3807 if (id == NULL)
3808 break;
3809 if (ch == 'p') {
3810 struct got_commit_object *commit;
3811 struct got_object_qid *pid;
3812 struct got_object_id *blob_id = NULL;
3813 int obj_type;
3814 err = got_object_open_as_commit(&commit,
3815 s->repo, id);
3816 if (err)
3817 break;
3818 pid = SIMPLEQ_FIRST(
3819 got_object_commit_get_parent_ids(commit));
3820 if (pid == NULL) {
3821 got_object_commit_close(commit);
3822 break;
3824 /* Check if path history ends here. */
3825 err = got_object_id_by_path(&blob_id, s->repo,
3826 pid->id, s->path);
3827 if (err) {
3828 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3829 err = NULL;
3830 got_object_commit_close(commit);
3831 break;
3833 err = got_object_get_type(&obj_type, s->repo,
3834 blob_id);
3835 free(blob_id);
3836 /* Can't blame non-blob type objects. */
3837 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3838 got_object_commit_close(commit);
3839 break;
3841 err = got_object_qid_alloc(&s->blamed_commit,
3842 pid->id);
3843 got_object_commit_close(commit);
3844 } else {
3845 if (got_object_id_cmp(id,
3846 s->blamed_commit->id) == 0)
3847 break;
3848 err = got_object_qid_alloc(&s->blamed_commit,
3849 id);
3851 if (err)
3852 break;
3853 s->done = 1;
3854 thread_err = stop_blame(&s->blame);
3855 s->done = 0;
3856 if (thread_err)
3857 break;
3858 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3859 s->blamed_commit, entry);
3860 err = run_blame(&s->blame, view, &s->blame_complete,
3861 &s->first_displayed_line, &s->last_displayed_line,
3862 &s->selected_line, &s->done, &s->eof,
3863 s->path, s->blamed_commit->id, s->repo);
3864 if (err)
3865 break;
3866 break;
3868 case 'B': {
3869 struct got_object_qid *first;
3870 first = SIMPLEQ_FIRST(&s->blamed_commits);
3871 if (!got_object_id_cmp(first->id, s->commit_id))
3872 break;
3873 s->done = 1;
3874 thread_err = stop_blame(&s->blame);
3875 s->done = 0;
3876 if (thread_err)
3877 break;
3878 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3879 got_object_qid_free(s->blamed_commit);
3880 s->blamed_commit =
3881 SIMPLEQ_FIRST(&s->blamed_commits);
3882 err = run_blame(&s->blame, view, &s->blame_complete,
3883 &s->first_displayed_line, &s->last_displayed_line,
3884 &s->selected_line, &s->done, &s->eof, s->path,
3885 s->blamed_commit->id, s->repo);
3886 if (err)
3887 break;
3888 break;
3890 case KEY_ENTER:
3891 case '\r': {
3892 struct got_object_id *id = NULL;
3893 struct got_object_qid *pid;
3894 struct got_commit_object *commit = NULL;
3895 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3896 s->first_displayed_line, s->selected_line);
3897 if (id == NULL)
3898 break;
3899 err = got_object_open_as_commit(&commit, s->repo, id);
3900 if (err)
3901 break;
3902 pid = SIMPLEQ_FIRST(
3903 got_object_commit_get_parent_ids(commit));
3904 if (view_is_parent_view(view))
3905 begin_x = view_split_begin_x(view->begin_x);
3906 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3907 if (diff_view == NULL) {
3908 got_object_commit_close(commit);
3909 err = got_error_from_errno("view_open");
3910 break;
3912 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3913 id, NULL, s->refs, s->repo);
3914 got_object_commit_close(commit);
3915 if (err) {
3916 view_close(diff_view);
3917 break;
3919 if (view_is_parent_view(view)) {
3920 err = view_close_child(view);
3921 if (err)
3922 break;
3923 err = view_set_child(view, diff_view);
3924 if (err) {
3925 view_close(diff_view);
3926 break;
3928 *focus_view = diff_view;
3929 view->child_focussed = 1;
3930 } else
3931 *new_view = diff_view;
3932 if (err)
3933 break;
3934 break;
3936 case KEY_NPAGE:
3937 case ' ':
3938 if (s->last_displayed_line >= s->blame.nlines &&
3939 s->selected_line >= MIN(s->blame.nlines,
3940 view->nlines - 2)) {
3941 break;
3943 if (s->last_displayed_line >= s->blame.nlines &&
3944 s->selected_line < view->nlines - 2) {
3945 s->selected_line = MIN(s->blame.nlines,
3946 view->nlines - 2);
3947 break;
3949 if (s->last_displayed_line + view->nlines - 2
3950 <= s->blame.nlines)
3951 s->first_displayed_line +=
3952 view->nlines - 2;
3953 else
3954 s->first_displayed_line =
3955 s->blame.nlines -
3956 (view->nlines - 3);
3957 break;
3958 case KEY_RESIZE:
3959 if (s->selected_line > view->nlines - 2) {
3960 s->selected_line = MIN(s->blame.nlines,
3961 view->nlines - 2);
3963 break;
3964 default:
3965 break;
3967 return thread_err ? thread_err : err;
3970 static const struct got_error *
3971 cmd_blame(int argc, char *argv[])
3973 const struct got_error *error;
3974 struct got_repository *repo = NULL;
3975 struct got_reflist_head refs;
3976 struct got_worktree *worktree = NULL;
3977 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3978 struct got_object_id *commit_id = NULL;
3979 char *commit_id_str = NULL;
3980 int ch;
3981 struct tog_view *view;
3983 SIMPLEQ_INIT(&refs);
3985 #ifndef PROFILE
3986 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3987 NULL) == -1)
3988 err(1, "pledge");
3989 #endif
3991 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3992 switch (ch) {
3993 case 'c':
3994 commit_id_str = optarg;
3995 break;
3996 case 'r':
3997 repo_path = realpath(optarg, NULL);
3998 if (repo_path == NULL)
3999 return got_error_from_errno2("realpath",
4000 optarg);
4001 break;
4002 default:
4003 usage_blame();
4004 /* NOTREACHED */
4008 argc -= optind;
4009 argv += optind;
4011 if (argc == 1)
4012 path = argv[0];
4013 else
4014 usage_blame();
4016 cwd = getcwd(NULL, 0);
4017 if (cwd == NULL) {
4018 error = got_error_from_errno("getcwd");
4019 goto done;
4021 if (repo_path == NULL) {
4022 error = got_worktree_open(&worktree, cwd);
4023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4024 goto done;
4025 else
4026 error = NULL;
4027 if (worktree) {
4028 repo_path =
4029 strdup(got_worktree_get_repo_path(worktree));
4030 if (repo_path == NULL)
4031 error = got_error_from_errno("strdup");
4032 if (error)
4033 goto done;
4034 } else {
4035 repo_path = strdup(cwd);
4036 if (repo_path == NULL) {
4037 error = got_error_from_errno("strdup");
4038 goto done;
4043 init_curses();
4045 error = got_repo_open(&repo, repo_path, NULL);
4046 if (error != NULL)
4047 goto done;
4049 error = apply_unveil(got_repo_get_path(repo), NULL);
4050 if (error)
4051 goto done;
4053 if (worktree) {
4054 const char *prefix = got_worktree_get_path_prefix(worktree);
4055 char *p, *worktree_subdir = cwd +
4056 strlen(got_worktree_get_root_path(worktree));
4057 if (asprintf(&p, "%s%s%s%s%s",
4058 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4059 worktree_subdir, worktree_subdir[0] ? "/" : "",
4060 path) == -1) {
4061 error = got_error_from_errno("asprintf");
4062 goto done;
4064 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4065 free(p);
4066 } else {
4067 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4069 if (error)
4070 goto done;
4072 if (commit_id_str == NULL) {
4073 struct got_reference *head_ref;
4074 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4075 if (error != NULL)
4076 goto done;
4077 error = got_ref_resolve(&commit_id, repo, head_ref);
4078 got_ref_close(head_ref);
4079 } else {
4080 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4081 if (error) {
4082 if (error->code != GOT_ERR_NOT_REF)
4083 goto done;
4084 error = got_repo_match_object_id_prefix(&commit_id,
4085 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4088 if (error != NULL)
4089 goto done;
4091 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4092 if (error)
4093 goto done;
4095 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4096 if (view == NULL) {
4097 error = got_error_from_errno("view_open");
4098 goto done;
4100 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4101 if (error)
4102 goto done;
4103 if (worktree) {
4104 /* Release work tree lock. */
4105 got_worktree_close(worktree);
4106 worktree = NULL;
4108 error = view_loop(view);
4109 done:
4110 free(repo_path);
4111 free(cwd);
4112 free(commit_id);
4113 if (worktree)
4114 got_worktree_close(worktree);
4115 if (repo)
4116 got_repo_close(repo);
4117 got_ref_list_free(&refs);
4118 return error;
4121 static const struct got_error *
4122 draw_tree_entries(struct tog_view *view,
4123 struct got_tree_entry **first_displayed_entry,
4124 struct got_tree_entry **last_displayed_entry,
4125 struct got_tree_entry **selected_entry, int *ndisplayed,
4126 const char *label, int show_ids, const char *parent_path,
4127 const struct got_tree_entries *entries, int selected, int limit,
4128 int isroot, struct tog_line_colors *colors)
4130 const struct got_error *err = NULL;
4131 struct got_tree_entry *te;
4132 wchar_t *wline;
4133 struct tog_line_color *lc;
4134 int width, n;
4136 *ndisplayed = 0;
4138 werase(view->window);
4140 if (limit == 0)
4141 return NULL;
4143 err = format_line(&wline, &width, label, view->ncols, 0);
4144 if (err)
4145 return err;
4146 if (view_needs_focus_indication(view))
4147 wstandout(view->window);
4148 waddwstr(view->window, wline);
4149 if (view_needs_focus_indication(view))
4150 wstandend(view->window);
4151 free(wline);
4152 wline = NULL;
4153 if (width < view->ncols - 1)
4154 waddch(view->window, '\n');
4155 if (--limit <= 0)
4156 return NULL;
4157 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4158 if (err)
4159 return err;
4160 waddwstr(view->window, wline);
4161 free(wline);
4162 wline = NULL;
4163 if (width < view->ncols - 1)
4164 waddch(view->window, '\n');
4165 if (--limit <= 0)
4166 return NULL;
4167 waddch(view->window, '\n');
4168 if (--limit <= 0)
4169 return NULL;
4171 te = SIMPLEQ_FIRST(&entries->head);
4172 if (*first_displayed_entry == NULL) {
4173 if (selected == 0) {
4174 if (view->focussed)
4175 wstandout(view->window);
4176 *selected_entry = NULL;
4178 waddstr(view->window, " ..\n"); /* parent directory */
4179 if (selected == 0 && view->focussed)
4180 wstandend(view->window);
4181 (*ndisplayed)++;
4182 if (--limit <= 0)
4183 return NULL;
4184 n = 1;
4185 } else {
4186 n = 0;
4187 while (te != *first_displayed_entry)
4188 te = SIMPLEQ_NEXT(te, entry);
4191 while (te) {
4192 char *line = NULL, *id_str = NULL;
4193 const char *modestr = "";
4195 if (show_ids) {
4196 err = got_object_id_str(&id_str, te->id);
4197 if (err)
4198 return got_error_from_errno(
4199 "got_object_id_str");
4201 if (got_object_tree_entry_is_submodule(te))
4202 modestr = "$";
4203 else if (S_ISLNK(te->mode))
4204 modestr = "@";
4205 else if (S_ISDIR(te->mode))
4206 modestr = "/";
4207 else if (te->mode & S_IXUSR)
4208 modestr = "*";
4209 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4210 te->name, modestr) == -1) {
4211 free(id_str);
4212 return got_error_from_errno("asprintf");
4214 free(id_str);
4215 err = format_line(&wline, &width, line, view->ncols, 0);
4216 if (err) {
4217 free(line);
4218 break;
4220 if (n == selected) {
4221 if (view->focussed)
4222 wstandout(view->window);
4223 *selected_entry = te;
4225 lc = match_line_color(colors, line);
4226 if (lc)
4227 wattr_on(view->window,
4228 COLOR_PAIR(lc->colorpair), NULL);
4229 waddwstr(view->window, wline);
4230 if (lc)
4231 wattr_off(view->window,
4232 COLOR_PAIR(lc->colorpair), NULL);
4233 if (width < view->ncols - 1)
4234 waddch(view->window, '\n');
4235 if (n == selected && view->focussed)
4236 wstandend(view->window);
4237 free(line);
4238 free(wline);
4239 wline = NULL;
4240 n++;
4241 (*ndisplayed)++;
4242 *last_displayed_entry = te;
4243 if (--limit <= 0)
4244 break;
4245 te = SIMPLEQ_NEXT(te, entry);
4248 return err;
4251 static void
4252 tree_scroll_up(struct tog_view *view,
4253 struct got_tree_entry **first_displayed_entry, int maxscroll,
4254 const struct got_tree_entries *entries, int isroot)
4256 struct got_tree_entry *te, *prev;
4257 int i;
4259 if (*first_displayed_entry == NULL)
4260 return;
4262 te = SIMPLEQ_FIRST(&entries->head);
4263 if (*first_displayed_entry == te) {
4264 if (!isroot)
4265 *first_displayed_entry = NULL;
4266 return;
4269 /* XXX this is stupid... switch to TAILQ? */
4270 for (i = 0; i < maxscroll; i++) {
4271 while (te != *first_displayed_entry) {
4272 prev = te;
4273 te = SIMPLEQ_NEXT(te, entry);
4275 *first_displayed_entry = prev;
4276 te = SIMPLEQ_FIRST(&entries->head);
4278 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4279 *first_displayed_entry = NULL;
4282 static int
4283 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4284 struct got_tree_entry *last_displayed_entry,
4285 const struct got_tree_entries *entries)
4287 struct got_tree_entry *next, *last;
4288 int n = 0;
4290 if (*first_displayed_entry)
4291 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4292 else
4293 next = SIMPLEQ_FIRST(&entries->head);
4294 last = last_displayed_entry;
4295 while (next && last && n++ < maxscroll) {
4296 last = SIMPLEQ_NEXT(last, entry);
4297 if (last) {
4298 *first_displayed_entry = next;
4299 next = SIMPLEQ_NEXT(next, entry);
4302 return n;
4305 static const struct got_error *
4306 tree_entry_path(char **path, struct tog_parent_trees *parents,
4307 struct got_tree_entry *te)
4309 const struct got_error *err = NULL;
4310 struct tog_parent_tree *pt;
4311 size_t len = 2; /* for leading slash and NUL */
4313 TAILQ_FOREACH(pt, parents, entry)
4314 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4315 if (te)
4316 len += strlen(te->name);
4318 *path = calloc(1, len);
4319 if (path == NULL)
4320 return got_error_from_errno("calloc");
4322 (*path)[0] = '/';
4323 pt = TAILQ_LAST(parents, tog_parent_trees);
4324 while (pt) {
4325 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4326 err = got_error(GOT_ERR_NO_SPACE);
4327 goto done;
4329 if (strlcat(*path, "/", len) >= len) {
4330 err = got_error(GOT_ERR_NO_SPACE);
4331 goto done;
4333 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4335 if (te) {
4336 if (strlcat(*path, te->name, len) >= len) {
4337 err = got_error(GOT_ERR_NO_SPACE);
4338 goto done;
4341 done:
4342 if (err) {
4343 free(*path);
4344 *path = NULL;
4346 return err;
4349 static const struct got_error *
4350 blame_tree_entry(struct tog_view **new_view, int begin_x,
4351 struct got_tree_entry *te, struct tog_parent_trees *parents,
4352 struct got_object_id *commit_id, struct got_reflist_head *refs,
4353 struct got_repository *repo)
4355 const struct got_error *err = NULL;
4356 char *path;
4357 struct tog_view *blame_view;
4359 *new_view = NULL;
4361 err = tree_entry_path(&path, parents, te);
4362 if (err)
4363 return err;
4365 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4366 if (blame_view == NULL) {
4367 err = got_error_from_errno("view_open");
4368 goto done;
4371 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4372 if (err) {
4373 if (err->code == GOT_ERR_CANCELLED)
4374 err = NULL;
4375 view_close(blame_view);
4376 } else
4377 *new_view = blame_view;
4378 done:
4379 free(path);
4380 return err;
4383 static const struct got_error *
4384 log_tree_entry(struct tog_view **new_view, int begin_x,
4385 struct got_tree_entry *te, struct tog_parent_trees *parents,
4386 struct got_object_id *commit_id, struct got_reflist_head *refs,
4387 struct got_repository *repo)
4389 struct tog_view *log_view;
4390 const struct got_error *err = NULL;
4391 char *path;
4393 *new_view = NULL;
4395 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4396 if (log_view == NULL)
4397 return got_error_from_errno("view_open");
4399 err = tree_entry_path(&path, parents, te);
4400 if (err)
4401 return err;
4403 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4404 if (err)
4405 view_close(log_view);
4406 else
4407 *new_view = log_view;
4408 free(path);
4409 return err;
4412 static const struct got_error *
4413 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4414 struct got_object_id *commit_id, struct got_reflist_head *refs,
4415 struct got_repository *repo)
4417 const struct got_error *err = NULL;
4418 char *commit_id_str = NULL;
4419 struct tog_tree_view_state *s = &view->state.tree;
4421 TAILQ_INIT(&s->parents);
4423 err = got_object_id_str(&commit_id_str, commit_id);
4424 if (err != NULL)
4425 goto done;
4427 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4428 err = got_error_from_errno("asprintf");
4429 goto done;
4432 s->root = s->tree = root;
4433 s->entries = got_object_tree_get_entries(root);
4434 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4435 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4436 s->commit_id = got_object_id_dup(commit_id);
4437 if (s->commit_id == NULL) {
4438 err = got_error_from_errno("got_object_id_dup");
4439 goto done;
4441 s->refs = refs;
4442 s->repo = repo;
4444 SIMPLEQ_INIT(&s->line_colors);
4446 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4447 err = add_line_color(&s->line_colors,
4448 "\\$$", 1, get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4449 if (err)
4450 goto done;
4451 err = add_line_color(&s->line_colors,
4452 "@$", 2, get_color_value("TOG_COLOR_TREE_SYMLINK"));
4453 if (err) {
4454 free_line_colors(&s->line_colors);
4455 goto done;
4457 err = add_line_color(&s->line_colors,
4458 "/$", 3, get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4459 if (err) {
4460 free_line_colors(&s->line_colors);
4461 goto done;
4464 err = add_line_color(&s->line_colors,
4465 "\\*$", 4, get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4466 if (err) {
4467 free_line_colors(&s->line_colors);
4468 goto done;
4472 view->show = show_tree_view;
4473 view->input = input_tree_view;
4474 view->close = close_tree_view;
4475 view->search_start = search_start_tree_view;
4476 view->search_next = search_next_tree_view;
4477 done:
4478 free(commit_id_str);
4479 if (err) {
4480 free(s->tree_label);
4481 s->tree_label = NULL;
4483 return err;
4486 static const struct got_error *
4487 close_tree_view(struct tog_view *view)
4489 struct tog_tree_view_state *s = &view->state.tree;
4491 free_line_colors(&s->line_colors);
4492 free(s->tree_label);
4493 s->tree_label = NULL;
4494 free(s->commit_id);
4495 s->commit_id = NULL;
4496 while (!TAILQ_EMPTY(&s->parents)) {
4497 struct tog_parent_tree *parent;
4498 parent = TAILQ_FIRST(&s->parents);
4499 TAILQ_REMOVE(&s->parents, parent, entry);
4500 free(parent);
4503 if (s->tree != s->root)
4504 got_object_tree_close(s->tree);
4505 got_object_tree_close(s->root);
4507 return NULL;
4510 static const struct got_error *
4511 search_start_tree_view(struct tog_view *view)
4513 struct tog_tree_view_state *s = &view->state.tree;
4515 s->matched_entry = NULL;
4516 return NULL;
4519 static int
4520 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4522 regmatch_t regmatch;
4524 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4527 static const struct got_error *
4528 search_next_tree_view(struct tog_view *view)
4530 struct tog_tree_view_state *s = &view->state.tree;
4531 struct got_tree_entry *entry = NULL, *te;
4533 if (!view->searching) {
4534 view->search_next_done = 1;
4535 return NULL;
4538 if (s->matched_entry) {
4539 if (view->searching == TOG_SEARCH_FORWARD) {
4540 if (s->selected_entry)
4541 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4542 else
4543 entry = SIMPLEQ_FIRST(&s->entries->head);
4545 else {
4546 if (s->selected_entry == NULL) {
4547 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4548 entry = te;
4549 } else {
4550 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4551 entry = te;
4552 if (SIMPLEQ_NEXT(te, entry) ==
4553 s->selected_entry)
4554 break;
4558 } else {
4559 if (view->searching == TOG_SEARCH_FORWARD)
4560 entry = SIMPLEQ_FIRST(&s->entries->head);
4561 else {
4562 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4563 entry = te;
4567 while (1) {
4568 if (entry == NULL) {
4569 if (s->matched_entry == NULL) {
4570 view->search_next_done = 1;
4571 return NULL;
4573 if (view->searching == TOG_SEARCH_FORWARD)
4574 entry = SIMPLEQ_FIRST(&s->entries->head);
4575 else {
4576 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4577 entry = te;
4581 if (match_tree_entry(entry, &view->regex)) {
4582 view->search_next_done = 1;
4583 s->matched_entry = entry;
4584 break;
4587 if (view->searching == TOG_SEARCH_FORWARD)
4588 entry = SIMPLEQ_NEXT(entry, entry);
4589 else {
4590 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4591 entry = NULL;
4592 else {
4593 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4594 if (SIMPLEQ_NEXT(te, entry) == entry) {
4595 entry = te;
4596 break;
4603 if (s->matched_entry) {
4604 s->first_displayed_entry = s->matched_entry;
4605 s->selected = 0;
4608 return NULL;
4611 static const struct got_error *
4612 show_tree_view(struct tog_view *view)
4614 const struct got_error *err = NULL;
4615 struct tog_tree_view_state *s = &view->state.tree;
4616 char *parent_path;
4618 err = tree_entry_path(&parent_path, &s->parents, NULL);
4619 if (err)
4620 return err;
4622 err = draw_tree_entries(view, &s->first_displayed_entry,
4623 &s->last_displayed_entry, &s->selected_entry,
4624 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4625 s->entries, s->selected, view->nlines, s->tree == s->root,
4626 &s->line_colors);
4627 free(parent_path);
4629 view_vborder(view);
4630 return err;
4633 static const struct got_error *
4634 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4635 struct tog_view **focus_view, struct tog_view *view, int ch)
4637 const struct got_error *err = NULL;
4638 struct tog_tree_view_state *s = &view->state.tree;
4639 struct tog_view *log_view;
4640 int begin_x = 0, nscrolled;
4642 switch (ch) {
4643 case 'i':
4644 s->show_ids = !s->show_ids;
4645 break;
4646 case 'l':
4647 if (!s->selected_entry)
4648 break;
4649 if (view_is_parent_view(view))
4650 begin_x = view_split_begin_x(view->begin_x);
4651 err = log_tree_entry(&log_view, begin_x,
4652 s->selected_entry, &s->parents,
4653 s->commit_id, s->refs, s->repo);
4654 if (view_is_parent_view(view)) {
4655 err = view_close_child(view);
4656 if (err)
4657 return err;
4658 err = view_set_child(view, log_view);
4659 if (err) {
4660 view_close(log_view);
4661 break;
4663 *focus_view = log_view;
4664 view->child_focussed = 1;
4665 } else
4666 *new_view = log_view;
4667 break;
4668 case 'k':
4669 case KEY_UP:
4670 if (s->selected > 0) {
4671 s->selected--;
4672 if (s->selected == 0)
4673 break;
4675 if (s->selected > 0)
4676 break;
4677 tree_scroll_up(view, &s->first_displayed_entry, 1,
4678 s->entries, s->tree == s->root);
4679 break;
4680 case KEY_PPAGE:
4681 tree_scroll_up(view, &s->first_displayed_entry,
4682 MAX(0, view->nlines - 4 - s->selected), s->entries,
4683 s->tree == s->root);
4684 s->selected = 0;
4685 if (SIMPLEQ_FIRST(&s->entries->head) ==
4686 s->first_displayed_entry && s->tree != s->root)
4687 s->first_displayed_entry = NULL;
4688 break;
4689 case 'j':
4690 case KEY_DOWN:
4691 if (s->selected < s->ndisplayed - 1) {
4692 s->selected++;
4693 break;
4695 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4696 /* can't scroll any further */
4697 break;
4698 tree_scroll_down(&s->first_displayed_entry, 1,
4699 s->last_displayed_entry, s->entries);
4700 break;
4701 case KEY_NPAGE:
4702 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4703 == NULL) {
4704 /* can't scroll any further; move cursor down */
4705 if (s->selected < s->ndisplayed - 1)
4706 s->selected = s->ndisplayed - 1;
4707 break;
4709 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4710 view->nlines, s->last_displayed_entry, s->entries);
4711 if (nscrolled < view->nlines) {
4712 int ndisplayed = 0;
4713 struct got_tree_entry *te;
4714 te = s->first_displayed_entry;
4715 do {
4716 ndisplayed++;
4717 te = SIMPLEQ_NEXT(te, entry);
4718 } while (te);
4719 s->selected = ndisplayed - 1;
4721 break;
4722 case KEY_ENTER:
4723 case '\r':
4724 case KEY_BACKSPACE:
4725 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4726 struct tog_parent_tree *parent;
4727 /* user selected '..' */
4728 if (s->tree == s->root)
4729 break;
4730 parent = TAILQ_FIRST(&s->parents);
4731 TAILQ_REMOVE(&s->parents, parent,
4732 entry);
4733 got_object_tree_close(s->tree);
4734 s->tree = parent->tree;
4735 s->entries =
4736 got_object_tree_get_entries(s->tree);
4737 s->first_displayed_entry =
4738 parent->first_displayed_entry;
4739 s->selected_entry =
4740 parent->selected_entry;
4741 s->selected = parent->selected;
4742 free(parent);
4743 } else if (S_ISDIR(s->selected_entry->mode)) {
4744 struct got_tree_object *subtree;
4745 err = got_object_open_as_tree(&subtree,
4746 s->repo, s->selected_entry->id);
4747 if (err)
4748 break;
4749 err = tree_view_visit_subtree(subtree, s);
4750 if (err) {
4751 got_object_tree_close(subtree);
4752 break;
4754 } else if (S_ISREG(s->selected_entry->mode)) {
4755 struct tog_view *blame_view;
4756 int begin_x = view_is_parent_view(view) ?
4757 view_split_begin_x(view->begin_x) : 0;
4759 err = blame_tree_entry(&blame_view, begin_x,
4760 s->selected_entry, &s->parents,
4761 s->commit_id, s->refs, s->repo);
4762 if (err)
4763 break;
4764 if (view_is_parent_view(view)) {
4765 err = view_close_child(view);
4766 if (err)
4767 return err;
4768 err = view_set_child(view, blame_view);
4769 if (err) {
4770 view_close(blame_view);
4771 break;
4773 *focus_view = blame_view;
4774 view->child_focussed = 1;
4775 } else
4776 *new_view = blame_view;
4778 break;
4779 case KEY_RESIZE:
4780 if (s->selected > view->nlines)
4781 s->selected = s->ndisplayed - 1;
4782 break;
4783 default:
4784 break;
4787 return err;
4790 __dead static void
4791 usage_tree(void)
4793 endwin();
4794 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4795 getprogname());
4796 exit(1);
4799 static const struct got_error *
4800 cmd_tree(int argc, char *argv[])
4802 const struct got_error *error;
4803 struct got_repository *repo = NULL;
4804 struct got_reflist_head refs;
4805 char *repo_path = NULL;
4806 struct got_object_id *commit_id = NULL;
4807 char *commit_id_arg = NULL;
4808 struct got_commit_object *commit = NULL;
4809 struct got_tree_object *tree = NULL;
4810 int ch;
4811 struct tog_view *view;
4813 SIMPLEQ_INIT(&refs);
4815 #ifndef PROFILE
4816 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4817 NULL) == -1)
4818 err(1, "pledge");
4819 #endif
4821 while ((ch = getopt(argc, argv, "c:")) != -1) {
4822 switch (ch) {
4823 case 'c':
4824 commit_id_arg = optarg;
4825 break;
4826 default:
4827 usage_tree();
4828 /* NOTREACHED */
4832 argc -= optind;
4833 argv += optind;
4835 if (argc == 0) {
4836 struct got_worktree *worktree;
4837 char *cwd = getcwd(NULL, 0);
4838 if (cwd == NULL)
4839 return got_error_from_errno("getcwd");
4840 error = got_worktree_open(&worktree, cwd);
4841 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4842 goto done;
4843 if (worktree) {
4844 free(cwd);
4845 repo_path =
4846 strdup(got_worktree_get_repo_path(worktree));
4847 got_worktree_close(worktree);
4848 } else
4849 repo_path = cwd;
4850 if (repo_path == NULL) {
4851 error = got_error_from_errno("strdup");
4852 goto done;
4854 } else if (argc == 1) {
4855 repo_path = realpath(argv[0], NULL);
4856 if (repo_path == NULL)
4857 return got_error_from_errno2("realpath", argv[0]);
4858 } else
4859 usage_log();
4861 init_curses();
4863 error = got_repo_open(&repo, repo_path, NULL);
4864 if (error != NULL)
4865 goto done;
4867 error = apply_unveil(got_repo_get_path(repo), NULL);
4868 if (error)
4869 goto done;
4871 if (commit_id_arg == NULL)
4872 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4873 else {
4874 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4875 if (error) {
4876 if (error->code != GOT_ERR_NOT_REF)
4877 goto done;
4878 error = got_repo_match_object_id_prefix(&commit_id,
4879 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4882 if (error != NULL)
4883 goto done;
4885 error = got_object_open_as_commit(&commit, repo, commit_id);
4886 if (error != NULL)
4887 goto done;
4889 error = got_object_open_as_tree(&tree, repo,
4890 got_object_commit_get_tree_id(commit));
4891 if (error != NULL)
4892 goto done;
4894 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4895 if (error)
4896 goto done;
4898 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4899 if (view == NULL) {
4900 error = got_error_from_errno("view_open");
4901 goto done;
4903 error = open_tree_view(view, tree, commit_id, &refs, repo);
4904 if (error)
4905 goto done;
4906 error = view_loop(view);
4907 done:
4908 free(repo_path);
4909 free(commit_id);
4910 if (commit)
4911 got_object_commit_close(commit);
4912 if (tree)
4913 got_object_tree_close(tree);
4914 if (repo)
4915 got_repo_close(repo);
4916 got_ref_list_free(&refs);
4917 return error;
4920 static void
4921 list_commands(void)
4923 int i;
4925 fprintf(stderr, "commands:");
4926 for (i = 0; i < nitems(tog_commands); i++) {
4927 struct tog_cmd *cmd = &tog_commands[i];
4928 fprintf(stderr, " %s", cmd->name);
4930 fputc('\n', stderr);
4933 __dead static void
4934 usage(int hflag)
4936 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4937 getprogname());
4938 if (hflag)
4939 list_commands();
4940 exit(1);
4943 static char **
4944 make_argv(const char *arg0, const char *arg1)
4946 char **argv;
4947 int argc = (arg1 == NULL ? 1 : 2);
4949 argv = calloc(argc, sizeof(char *));
4950 if (argv == NULL)
4951 err(1, "calloc");
4952 argv[0] = strdup(arg0);
4953 if (argv[0] == NULL)
4954 err(1, "strdup");
4955 if (arg1) {
4956 argv[1] = strdup(arg1);
4957 if (argv[1] == NULL)
4958 err(1, "strdup");
4961 return argv;
4964 int
4965 main(int argc, char *argv[])
4967 const struct got_error *error = NULL;
4968 struct tog_cmd *cmd = NULL;
4969 int ch, hflag = 0, Vflag = 0;
4970 char **cmd_argv = NULL;
4972 setlocale(LC_CTYPE, "");
4974 while ((ch = getopt(argc, argv, "hV")) != -1) {
4975 switch (ch) {
4976 case 'h':
4977 hflag = 1;
4978 break;
4979 case 'V':
4980 Vflag = 1;
4981 break;
4982 default:
4983 usage(hflag);
4984 /* NOTREACHED */
4988 argc -= optind;
4989 argv += optind;
4990 optind = 0;
4991 optreset = 1;
4993 if (Vflag) {
4994 got_version_print_str();
4995 return 1;
4998 if (argc == 0) {
4999 if (hflag)
5000 usage(hflag);
5001 /* Build an argument vector which runs a default command. */
5002 cmd = &tog_commands[0];
5003 cmd_argv = make_argv(cmd->name, NULL);
5004 argc = 1;
5005 } else {
5006 int i;
5008 /* Did the user specific a command? */
5009 for (i = 0; i < nitems(tog_commands); i++) {
5010 if (strncmp(tog_commands[i].name, argv[0],
5011 strlen(argv[0])) == 0) {
5012 cmd = &tog_commands[i];
5013 break;
5017 if (cmd == NULL) {
5018 fprintf(stderr, "%s: unknown command '%s'\n",
5019 getprogname(), argv[0]);
5020 list_commands();
5021 return 1;
5025 if (hflag)
5026 cmd->cmd_usage();
5027 else
5028 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5030 endwin();
5031 free(cmd_argv);
5032 if (error && error->code != GOT_ERR_CANCELLED)
5033 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5034 return 0;