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_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_diff.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
48 #include "got_utf8.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_path.h"
52 #include "got_worktree.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 #define CTRL(x) ((x) & 0x1f)
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct tog_cmd {
69 const char *name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 };
74 __dead static void usage(int);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "diff", cmd_diff, usage_diff },
88 { "blame", cmd_blame, usage_blame },
89 { "tree", cmd_tree, usage_tree },
90 };
92 enum tog_view_type {
93 TOG_VIEW_DIFF,
94 TOG_VIEW_LOG,
95 TOG_VIEW_BLAME,
96 TOG_VIEW_TREE
97 };
99 #define TOG_EOF_STRING "(END)"
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 const char *head_ref_name;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
155 sig_atomic_t quit;
156 pthread_t thread;
157 struct tog_log_thread_args thread_args;
158 struct commit_queue_entry *matched_entry;
159 struct commit_queue_entry *search_entry;
160 };
162 struct tog_blame_cb_args {
163 struct tog_blame_line *lines; /* one per line */
164 int nlines;
166 struct tog_view *view;
167 struct got_object_id *commit_id;
168 int *quit;
169 };
171 struct tog_blame_thread_args {
172 const char *path;
173 struct got_repository *repo;
174 struct tog_blame_cb_args *cb_args;
175 int *complete;
176 };
178 struct tog_blame {
179 FILE *f;
180 size_t filesize;
181 struct tog_blame_line *lines;
182 int nlines;
183 off_t *line_offsets;
184 pthread_t thread;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
187 const char *path;
188 };
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
193 int selected_line;
194 int blame_complete;
195 int eof;
196 int done;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
199 char *path;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
204 int matched_line;
205 };
207 struct tog_parent_tree {
208 TAILQ_ENTRY(tog_parent_tree) entry;
209 struct got_tree_object *tree;
210 struct got_tree_entry *first_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int selected;
213 };
215 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
217 struct tog_tree_view_state {
218 char *tree_label;
219 struct got_tree_object *root;
220 struct got_tree_object *tree;
221 const struct got_tree_entries *entries;
222 struct got_tree_entry *first_displayed_entry;
223 struct got_tree_entry *last_displayed_entry;
224 struct got_tree_entry *selected_entry;
225 int ndisplayed, selected, show_ids;
226 struct tog_parent_trees parents;
227 struct got_object_id *commit_id;
228 struct got_repository *repo;
229 struct got_reflist_head *refs;
230 struct got_tree_entry *matched_entry;
231 };
233 /*
234 * We implement two types of views: parent views and child views.
236 * The 'Tab' key switches between a parent view and its child view.
237 * Child views are shown side-by-side to their parent view, provided
238 * there is enough screen estate.
240 * When a new view is opened from within a parent view, this new view
241 * becomes a child view of the parent view, replacing any existing child.
243 * When a new view is opened from within a child view, this new view
244 * becomes a parent view which will obscure the views below until the
245 * user quits the new parent view by typing 'q'.
247 * This list of views contains parent views only.
248 * Child views are only pointed to by their parent view.
249 */
250 TAILQ_HEAD(tog_view_list_head, tog_view);
252 struct tog_view {
253 TAILQ_ENTRY(tog_view) entry;
254 WINDOW *window;
255 PANEL *panel;
256 int nlines, ncols, begin_y, begin_x;
257 int lines, cols; /* copies of LINES and COLS */
258 int focussed;
259 struct tog_view *parent;
260 struct tog_view *child;
261 int child_focussed;
263 /* type-specific state */
264 enum tog_view_type type;
265 union {
266 struct tog_diff_view_state diff;
267 struct tog_log_view_state log;
268 struct tog_blame_view_state blame;
269 struct tog_tree_view_state tree;
270 } state;
272 const struct got_error *(*show)(struct tog_view *);
273 const struct got_error *(*input)(struct tog_view **,
274 struct tog_view **, struct tog_view**, struct tog_view *, int);
275 const struct got_error *(*close)(struct tog_view *);
277 const struct got_error *(*search_start)(struct tog_view *);
278 const struct got_error *(*search_next)(struct tog_view *);
279 int searching;
280 #define TOG_SEARCH_FORWARD 1
281 #define TOG_SEARCH_BACKWARD 2
282 int search_next_done;
283 regex_t regex;
284 };
286 static const struct got_error *open_diff_view(struct tog_view *,
287 struct got_object_id *, struct got_object_id *, struct tog_view *,
288 struct got_reflist_head *, struct got_repository *);
289 static const struct got_error *show_diff_view(struct tog_view *);
290 static const struct got_error *input_diff_view(struct tog_view **,
291 struct tog_view **, struct tog_view **, struct tog_view *, int);
292 static const struct got_error* close_diff_view(struct tog_view *);
294 static const struct got_error *open_log_view(struct tog_view *,
295 struct got_object_id *, struct got_reflist_head *,
296 struct got_repository *, const char *, const char *, int);
297 static const struct got_error * show_log_view(struct tog_view *);
298 static const struct got_error *input_log_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_log_view(struct tog_view *);
301 static const struct got_error *search_start_log_view(struct tog_view *);
302 static const struct got_error *search_next_log_view(struct tog_view *);
304 static const struct got_error *open_blame_view(struct tog_view *, char *,
305 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 static const struct got_error *show_blame_view(struct tog_view *);
307 static const struct got_error *input_blame_view(struct tog_view **,
308 struct tog_view **, struct tog_view **, struct tog_view *, int);
309 static const struct got_error *close_blame_view(struct tog_view *);
310 static const struct got_error *search_start_blame_view(struct tog_view *);
311 static const struct got_error *search_next_blame_view(struct tog_view *);
313 static const struct got_error *open_tree_view(struct tog_view *,
314 struct got_tree_object *, struct got_object_id *,
315 struct got_reflist_head *, struct got_repository *);
316 static const struct got_error *show_tree_view(struct tog_view *);
317 static const struct got_error *input_tree_view(struct tog_view **,
318 struct tog_view **, struct tog_view **, struct tog_view *, int);
319 static const struct got_error *close_tree_view(struct tog_view *);
320 static const struct got_error *search_start_tree_view(struct tog_view *);
321 static const struct got_error *search_next_tree_view(struct tog_view *);
323 static volatile sig_atomic_t tog_sigwinch_received;
325 static void
326 tog_sigwinch(int signo)
328 tog_sigwinch_received = 1;
331 static const struct got_error *
332 view_close(struct tog_view *view)
334 const struct got_error *err = NULL;
336 if (view->child) {
337 view_close(view->child);
338 view->child = NULL;
340 if (view->close)
341 err = view->close(view);
342 if (view->panel)
343 del_panel(view->panel);
344 if (view->window)
345 delwin(view->window);
346 free(view);
347 return err;
350 static struct tog_view *
351 view_open(int nlines, int ncols, int begin_y, int begin_x,
352 enum tog_view_type type)
354 struct tog_view *view = calloc(1, sizeof(*view));
356 if (view == NULL)
357 return NULL;
359 view->type = type;
360 view->lines = LINES;
361 view->cols = COLS;
362 view->nlines = nlines ? nlines : LINES - begin_y;
363 view->ncols = ncols ? ncols : COLS - begin_x;
364 view->begin_y = begin_y;
365 view->begin_x = begin_x;
366 view->window = newwin(nlines, ncols, begin_y, begin_x);
367 if (view->window == NULL) {
368 view_close(view);
369 return NULL;
371 view->panel = new_panel(view->window);
372 if (view->panel == NULL ||
373 set_panel_userptr(view->panel, view) != OK) {
374 view_close(view);
375 return NULL;
378 keypad(view->window, TRUE);
379 return view;
382 static int
383 view_split_begin_x(int begin_x)
385 if (begin_x > 0 || COLS < 120)
386 return 0;
387 return (COLS - MAX(COLS / 2, 80));
390 static const struct got_error *view_resize(struct tog_view *);
392 static const struct got_error *
393 view_splitscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
397 view->begin_y = 0;
398 view->begin_x = view_split_begin_x(0);
399 view->nlines = LINES;
400 view->ncols = COLS - view->begin_x;
401 view->lines = LINES;
402 view->cols = COLS;
403 err = view_resize(view);
404 if (err)
405 return err;
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno("mvwin");
410 return NULL;
413 static const struct got_error *
414 view_fullscreen(struct tog_view *view)
416 const struct got_error *err = NULL;
418 view->begin_x = 0;
419 view->begin_y = 0;
420 view->nlines = LINES;
421 view->ncols = COLS;
422 view->lines = LINES;
423 view->cols = COLS;
424 err = view_resize(view);
425 if (err)
426 return err;
428 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 return got_error_from_errno("mvwin");
431 return NULL;
434 static int
435 view_is_parent_view(struct tog_view *view)
437 return view->parent == NULL;
440 static const struct got_error *
441 view_resize(struct tog_view *view)
443 int nlines, ncols;
445 if (view->lines > LINES)
446 nlines = view->nlines - (view->lines - LINES);
447 else
448 nlines = view->nlines + (LINES - view->lines);
450 if (view->cols > COLS)
451 ncols = view->ncols - (view->cols - COLS);
452 else
453 ncols = view->ncols + (COLS - view->cols);
455 if (wresize(view->window, nlines, ncols) == ERR)
456 return got_error_from_errno("wresize");
457 if (replace_panel(view->panel, view->window) == ERR)
458 return got_error_from_errno("replace_panel");
459 wclear(view->window);
461 view->nlines = nlines;
462 view->ncols = ncols;
463 view->lines = LINES;
464 view->cols = COLS;
466 if (view->child) {
467 view->child->begin_x = view_split_begin_x(view->begin_x);
468 if (view->child->begin_x == 0) {
469 view_fullscreen(view->child);
470 if (view->child->focussed)
471 show_panel(view->child->panel);
472 else
473 show_panel(view->panel);
474 } else {
475 view_splitscreen(view->child);
476 show_panel(view->child->panel);
480 return NULL;
483 static const struct got_error *
484 view_close_child(struct tog_view *view)
486 const struct got_error *err = NULL;
488 if (view->child == NULL)
489 return NULL;
491 err = view_close(view->child);
492 view->child = NULL;
493 return err;
496 static const struct got_error *
497 view_set_child(struct tog_view *view, struct tog_view *child)
499 const struct got_error *err = NULL;
501 view->child = child;
502 child->parent = view;
503 return err;
506 static int
507 view_is_splitscreen(struct tog_view *view)
509 return view->begin_x > 0;
512 static void
513 tog_resizeterm(void)
515 int cols, lines;
516 struct winsize size;
518 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
519 cols = 80; /* Default */
520 lines = 24;
521 } else {
522 cols = size.ws_col;
523 lines = size.ws_row;
525 resize_term(lines, cols);
528 static const struct got_error *
529 view_search_start(struct tog_view *view)
531 const struct got_error *err = NULL;
532 char pattern[1024];
533 int ret;
535 if (view->nlines < 1)
536 return NULL;
538 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
539 view->begin_x, "/");
540 wclrtoeol(view->window);
542 nocbreak();
543 echo();
544 ret = wgetnstr(view->window, pattern, sizeof(pattern));
545 cbreak();
546 noecho();
547 if (ret == ERR)
548 return NULL;
550 if (view->searching) {
551 regfree(&view->regex);
552 view->searching = 0;
555 if (regcomp(&view->regex, pattern,
556 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
557 err = view->search_start(view);
558 if (err) {
559 regfree(&view->regex);
560 return err;
562 view->searching = TOG_SEARCH_FORWARD;
563 view->search_next_done = 0;
564 view->search_next(view);
567 return NULL;
570 static const struct got_error *
571 view_input(struct tog_view **new, struct tog_view **dead,
572 struct tog_view **focus, int *done, struct tog_view *view,
573 struct tog_view_list_head *views)
575 const struct got_error *err = NULL;
576 struct tog_view *v;
577 int ch, errcode;
579 *new = NULL;
580 *dead = NULL;
581 *focus = NULL;
583 if (view->searching && !view->search_next_done) {
584 errcode = pthread_mutex_unlock(&tog_mutex);
585 if (errcode)
586 return got_error_set_errno(errcode,
587 "pthread_mutex_unlock");
588 pthread_yield();
589 errcode = pthread_mutex_lock(&tog_mutex);
590 if (errcode)
591 return got_error_set_errno(errcode,
592 "pthread_mutex_lock");
593 view->search_next(view);
594 return NULL;
597 nodelay(stdscr, FALSE);
598 /* Allow threads to make progress while we are waiting for input. */
599 errcode = pthread_mutex_unlock(&tog_mutex);
600 if (errcode)
601 return got_error_set_errno(errcode, "pthread_mutex_unlock");
602 ch = wgetch(view->window);
603 errcode = pthread_mutex_lock(&tog_mutex);
604 if (errcode)
605 return got_error_set_errno(errcode, "pthread_mutex_lock");
606 nodelay(stdscr, TRUE);
608 if (tog_sigwinch_received) {
609 tog_resizeterm();
610 tog_sigwinch_received = 0;
611 TAILQ_FOREACH(v, views, entry) {
612 err = view_resize(v);
613 if (err)
614 return err;
615 err = v->input(new, dead, focus, v, KEY_RESIZE);
616 if (err)
617 return err;
621 switch (ch) {
622 case ERR:
623 break;
624 case '\t':
625 if (view->child) {
626 *focus = view->child;
627 view->child_focussed = 1;
628 } else if (view->parent) {
629 *focus = view->parent;
630 view->parent->child_focussed = 0;
632 break;
633 case 'q':
634 err = view->input(new, dead, focus, view, ch);
635 *dead = view;
636 break;
637 case 'Q':
638 *done = 1;
639 break;
640 case 'f':
641 if (view_is_parent_view(view)) {
642 if (view->child == NULL)
643 break;
644 if (view_is_splitscreen(view->child)) {
645 *focus = view->child;
646 view->child_focussed = 1;
647 err = view_fullscreen(view->child);
648 } else
649 err = view_splitscreen(view->child);
650 if (err)
651 break;
652 err = view->child->input(new, dead, focus,
653 view->child, KEY_RESIZE);
654 } else {
655 if (view_is_splitscreen(view)) {
656 *focus = view;
657 view->parent->child_focussed = 1;
658 err = view_fullscreen(view);
659 } else {
660 err = view_splitscreen(view);
662 if (err)
663 break;
664 err = view->input(new, dead, focus, view,
665 KEY_RESIZE);
667 break;
668 case KEY_RESIZE:
669 break;
670 case '/':
671 if (view->search_start)
672 view_search_start(view);
673 else
674 err = view->input(new, dead, focus, view, ch);
675 break;
676 case 'N':
677 case 'n':
678 if (view->search_next && view->searching) {
679 view->searching = (ch == 'n' ?
680 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
681 view->search_next_done = 0;
682 view->search_next(view);
683 } else
684 err = view->input(new, dead, focus, view, ch);
685 break;
686 default:
687 err = view->input(new, dead, focus, view, ch);
688 break;
691 return err;
694 void
695 view_vborder(struct tog_view *view)
697 PANEL *panel;
698 struct tog_view *view_above;
700 if (view->parent)
701 return view_vborder(view->parent);
703 panel = panel_above(view->panel);
704 if (panel == NULL)
705 return;
707 view_above = panel_userptr(panel);
708 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
709 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
712 int
713 view_needs_focus_indication(struct tog_view *view)
715 if (view_is_parent_view(view)) {
716 if (view->child == NULL || view->child_focussed)
717 return 0;
718 if (!view_is_splitscreen(view->child))
719 return 0;
720 } else if (!view_is_splitscreen(view))
721 return 0;
723 return view->focussed;
726 static const struct got_error *
727 view_loop(struct tog_view *view)
729 const struct got_error *err = NULL;
730 struct tog_view_list_head views;
731 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
732 int fast_refresh = 10;
733 int done = 0, errcode;
735 errcode = pthread_mutex_lock(&tog_mutex);
736 if (errcode)
737 return got_error_set_errno(errcode, "pthread_mutex_lock");
739 TAILQ_INIT(&views);
740 TAILQ_INSERT_HEAD(&views, view, entry);
742 main_view = view;
743 view->focussed = 1;
744 err = view->show(view);
745 if (err)
746 return err;
747 update_panels();
748 doupdate();
749 while (!TAILQ_EMPTY(&views) && !done) {
750 /* Refresh fast during initialization, then become slower. */
751 if (fast_refresh && fast_refresh-- == 0)
752 halfdelay(10); /* switch to once per second */
754 err = view_input(&new_view, &dead_view, &focus_view, &done,
755 view, &views);
756 if (err)
757 break;
758 if (dead_view) {
759 struct tog_view *prev = NULL;
761 if (view_is_parent_view(dead_view))
762 prev = TAILQ_PREV(dead_view,
763 tog_view_list_head, entry);
764 else if (view->parent != dead_view)
765 prev = view->parent;
767 if (dead_view->parent)
768 dead_view->parent->child = NULL;
769 else
770 TAILQ_REMOVE(&views, dead_view, entry);
772 err = view_close(dead_view);
773 if (err || (dead_view == main_view && new_view == NULL))
774 goto done;
776 if (view == dead_view) {
777 if (focus_view)
778 view = focus_view;
779 else if (prev)
780 view = prev;
781 else if (!TAILQ_EMPTY(&views))
782 view = TAILQ_LAST(&views,
783 tog_view_list_head);
784 else
785 view = NULL;
786 if (view) {
787 if (view->child && view->child_focussed)
788 focus_view = view->child;
789 else
790 focus_view = view;
794 if (new_view) {
795 struct tog_view *v, *t;
796 /* Only allow one parent view per type. */
797 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
798 if (v->type != new_view->type)
799 continue;
800 TAILQ_REMOVE(&views, v, entry);
801 err = view_close(v);
802 if (err)
803 goto done;
804 break;
806 TAILQ_INSERT_TAIL(&views, new_view, entry);
807 view = new_view;
808 if (focus_view == NULL)
809 focus_view = new_view;
811 if (focus_view) {
812 show_panel(focus_view->panel);
813 if (view)
814 view->focussed = 0;
815 focus_view->focussed = 1;
816 view = focus_view;
817 if (new_view)
818 show_panel(new_view->panel);
819 if (view->child && view_is_splitscreen(view->child))
820 show_panel(view->child->panel);
822 if (view) {
823 if (focus_view == NULL) {
824 view->focussed = 1;
825 show_panel(view->panel);
826 if (view->child && view_is_splitscreen(view->child))
827 show_panel(view->child->panel);
828 focus_view = view;
830 if (view->parent) {
831 err = view->parent->show(view->parent);
832 if (err)
833 goto done;
835 err = view->show(view);
836 if (err)
837 goto done;
838 if (view->child) {
839 err = view->child->show(view->child);
840 if (err)
841 goto done;
843 update_panels();
844 doupdate();
847 done:
848 while (!TAILQ_EMPTY(&views)) {
849 view = TAILQ_FIRST(&views);
850 TAILQ_REMOVE(&views, view, entry);
851 view_close(view);
854 errcode = pthread_mutex_unlock(&tog_mutex);
855 if (errcode)
856 return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 return err;
861 __dead static void
862 usage_log(void)
864 endwin();
865 fprintf(stderr,
866 "usage: %s log [-c commit] [-r repository-path] [path]\n",
867 getprogname());
868 exit(1);
871 /* Create newly allocated wide-character string equivalent to a byte string. */
872 static const struct got_error *
873 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
875 char *vis = NULL;
876 const struct got_error *err = NULL;
878 *ws = NULL;
879 *wlen = mbstowcs(NULL, s, 0);
880 if (*wlen == (size_t)-1) {
881 int vislen;
882 if (errno != EILSEQ)
883 return got_error_from_errno("mbstowcs");
885 /* byte string invalid in current encoding; try to "fix" it */
886 err = got_mbsavis(&vis, &vislen, s);
887 if (err)
888 return err;
889 *wlen = mbstowcs(NULL, vis, 0);
890 if (*wlen == (size_t)-1) {
891 err = got_error_from_errno("mbstowcs"); /* give up */
892 goto done;
896 *ws = calloc(*wlen + 1, sizeof(*ws));
897 if (*ws == NULL) {
898 err = got_error_from_errno("calloc");
899 goto done;
902 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
903 err = got_error_from_errno("mbstowcs");
904 done:
905 free(vis);
906 if (err) {
907 free(*ws);
908 *ws = NULL;
909 *wlen = 0;
911 return err;
914 /* Format a line for display, ensuring that it won't overflow a width limit. */
915 static const struct got_error *
916 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
918 const struct got_error *err = NULL;
919 int cols = 0;
920 wchar_t *wline = NULL;
921 size_t wlen;
922 int i;
924 *wlinep = NULL;
925 *widthp = 0;
927 err = mbs2ws(&wline, &wlen, line);
928 if (err)
929 return err;
931 i = 0;
932 while (i < wlen && cols < wlimit) {
933 int width = wcwidth(wline[i]);
934 switch (width) {
935 case 0:
936 i++;
937 break;
938 case 1:
939 case 2:
940 if (cols + width <= wlimit)
941 cols += width;
942 i++;
943 break;
944 case -1:
945 if (wline[i] == L'\t')
946 cols += TABSIZE - ((cols + 1) % TABSIZE);
947 i++;
948 break;
949 default:
950 err = got_error_from_errno("wcwidth");
951 goto done;
954 wline[i] = L'\0';
955 if (widthp)
956 *widthp = cols;
957 done:
958 if (err)
959 free(wline);
960 else
961 *wlinep = wline;
962 return err;
965 static const struct got_error*
966 build_refs_str(char **refs_str, struct got_reflist_head *refs,
967 struct got_object_id *id)
969 static const struct got_error *err = NULL;
970 struct got_reflist_entry *re;
971 char *s;
972 const char *name;
974 *refs_str = NULL;
976 SIMPLEQ_FOREACH(re, refs, entry) {
977 if (got_object_id_cmp(re->id, id) != 0)
978 continue;
979 name = got_ref_get_name(re->ref);
980 if (strcmp(name, GOT_REF_HEAD) == 0)
981 continue;
982 if (strncmp(name, "refs/", 5) == 0)
983 name += 5;
984 if (strncmp(name, "got/", 4) == 0)
985 continue;
986 if (strncmp(name, "heads/", 6) == 0)
987 name += 6;
988 if (strncmp(name, "remotes/", 8) == 0)
989 name += 8;
990 s = *refs_str;
991 if (asprintf(refs_str, "%s%s%s", s ? s : "",
992 s ? ", " : "", name) == -1) {
993 err = got_error_from_errno("asprintf");
994 free(s);
995 *refs_str = NULL;
996 break;
998 free(s);
1001 return err;
1004 static const struct got_error *
1005 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1007 char *smallerthan, *at;
1009 smallerthan = strchr(author, '<');
1010 if (smallerthan && smallerthan[1] != '\0')
1011 author = smallerthan + 1;
1012 at = strchr(author, '@');
1013 if (at)
1014 *at = '\0';
1015 return format_line(wauthor, author_width, author, limit);
1018 static const struct got_error *
1019 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1020 struct got_object_id *id, struct got_reflist_head *refs,
1021 int author_display_cols)
1023 const struct got_error *err = NULL;
1024 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1025 char *logmsg0 = NULL, *logmsg = NULL;
1026 char *author = NULL;
1027 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1028 int author_width, logmsg_width;
1029 char *newline, *line = NULL;
1030 int col, limit;
1031 static const size_t date_display_cols = 9;
1032 const int avail = view->ncols;
1033 struct tm tm;
1034 time_t committer_time;
1036 committer_time = got_object_commit_get_committer_time(commit);
1037 if (localtime_r(&committer_time, &tm) == NULL)
1038 return got_error_from_errno("localtime_r");
1039 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1040 >= sizeof(datebuf))
1041 return got_error(GOT_ERR_NO_SPACE);
1043 if (avail < date_display_cols)
1044 limit = MIN(sizeof(datebuf) - 1, avail);
1045 else
1046 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1047 waddnstr(view->window, datebuf, limit);
1048 col = limit + 1;
1049 if (col > avail)
1050 goto done;
1052 author = strdup(got_object_commit_get_author(commit));
1053 if (author == NULL) {
1054 err = got_error_from_errno("strdup");
1055 goto done;
1057 err = format_author(&wauthor, &author_width, author, avail - col);
1058 if (err)
1059 goto done;
1060 waddwstr(view->window, wauthor);
1061 col += author_width;
1062 while (col <= avail && author_width < author_display_cols + 2) {
1063 waddch(view->window, ' ');
1064 col++;
1065 author_width++;
1067 if (col > avail)
1068 goto done;
1070 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1071 if (logmsg0 == NULL) {
1072 err = got_error_from_errno("strdup");
1073 goto done;
1075 logmsg = logmsg0;
1076 while (*logmsg == '\n')
1077 logmsg++;
1078 newline = strchr(logmsg, '\n');
1079 if (newline)
1080 *newline = '\0';
1081 limit = avail - col;
1082 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1083 if (err)
1084 goto done;
1085 waddwstr(view->window, wlogmsg);
1086 col += logmsg_width;
1087 while (col <= avail) {
1088 waddch(view->window, ' ');
1089 col++;
1091 done:
1092 free(logmsg0);
1093 free(wlogmsg);
1094 free(author);
1095 free(wauthor);
1096 free(line);
1097 return err;
1100 static struct commit_queue_entry *
1101 alloc_commit_queue_entry(struct got_commit_object *commit,
1102 struct got_object_id *id)
1104 struct commit_queue_entry *entry;
1106 entry = calloc(1, sizeof(*entry));
1107 if (entry == NULL)
1108 return NULL;
1110 entry->id = id;
1111 entry->commit = commit;
1112 return entry;
1115 static void
1116 pop_commit(struct commit_queue *commits)
1118 struct commit_queue_entry *entry;
1120 entry = TAILQ_FIRST(&commits->head);
1121 TAILQ_REMOVE(&commits->head, entry, entry);
1122 got_object_commit_close(entry->commit);
1123 commits->ncommits--;
1124 /* Don't free entry->id! It is owned by the commit graph. */
1125 free(entry);
1128 static void
1129 free_commits(struct commit_queue *commits)
1131 while (!TAILQ_EMPTY(&commits->head))
1132 pop_commit(commits);
1135 static const struct got_error *
1136 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1137 int minqueue, struct got_repository *repo, const char *path)
1139 const struct got_error *err = NULL;
1140 int nqueued = 0;
1143 * We keep all commits open throughout the lifetime of the log
1144 * view in order to avoid having to re-fetch commits from disk
1145 * while updating the display.
1147 while (nqueued < minqueue) {
1148 struct got_object_id *id;
1149 struct got_commit_object *commit;
1150 struct commit_queue_entry *entry;
1151 int errcode;
1153 err = got_commit_graph_iter_next(&id, graph);
1154 if (err) {
1155 if (err->code != GOT_ERR_ITER_NEED_MORE)
1156 break;
1157 err = got_commit_graph_fetch_commits(graph,
1158 minqueue, repo);
1159 if (err)
1160 return err;
1161 continue;
1164 if (id == NULL)
1165 break;
1167 err = got_object_open_as_commit(&commit, repo, id);
1168 if (err)
1169 break;
1170 entry = alloc_commit_queue_entry(commit, id);
1171 if (entry == NULL) {
1172 err = got_error_from_errno("alloc_commit_queue_entry");
1173 break;
1176 errcode = pthread_mutex_lock(&tog_mutex);
1177 if (errcode) {
1178 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1179 break;
1182 entry->idx = commits->ncommits;
1183 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1184 nqueued++;
1185 commits->ncommits++;
1187 errcode = pthread_mutex_unlock(&tog_mutex);
1188 if (errcode && err == NULL)
1189 err = got_error_set_errno(errcode,
1190 "pthread_mutex_unlock");
1193 return err;
1196 static const struct got_error *
1197 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1198 struct got_repository *repo)
1200 const struct got_error *err = NULL;
1201 struct got_reference *head_ref;
1203 *head_id = NULL;
1205 err = got_ref_open(&head_ref, repo, branch_name, 0);
1206 if (err)
1207 return err;
1209 err = got_ref_resolve(head_id, repo, head_ref);
1210 got_ref_close(head_ref);
1211 if (err) {
1212 *head_id = NULL;
1213 return err;
1216 return NULL;
1219 static const struct got_error *
1220 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1221 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1222 struct commit_queue *commits, int selected_idx, int limit,
1223 struct got_reflist_head *refs, const char *path, int commits_needed)
1225 const struct got_error *err = NULL;
1226 struct commit_queue_entry *entry;
1227 int width;
1228 int ncommits, author_cols = 10;
1229 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1230 char *refs_str = NULL;
1231 wchar_t *wline;
1233 entry = first;
1234 ncommits = 0;
1235 while (entry) {
1236 if (ncommits == selected_idx) {
1237 *selected = entry;
1238 break;
1240 entry = TAILQ_NEXT(entry, entry);
1241 ncommits++;
1244 if (*selected && !(view->searching && view->search_next_done == 0)) {
1245 err = got_object_id_str(&id_str, (*selected)->id);
1246 if (err)
1247 return err;
1248 if (refs) {
1249 err = build_refs_str(&refs_str, refs, (*selected)->id);
1250 if (err)
1251 goto done;
1255 if (commits_needed == 0)
1256 halfdelay(10); /* disable fast refresh */
1258 if (asprintf(&ncommits_str, " [%d/%d] %s",
1259 entry ? entry->idx + 1 : 0, commits->ncommits,
1260 commits_needed > 0 ?
1261 (view->searching && view->search_next_done == 0
1262 ? "searching..." : "loading... ") :
1263 (refs_str ? refs_str : "")) == -1) {
1264 err = got_error_from_errno("asprintf");
1265 goto done;
1268 if (path && strcmp(path, "/") != 0) {
1269 if (asprintf(&header, "commit %s %s%s",
1270 id_str ? id_str : "........................................",
1271 path, ncommits_str) == -1) {
1272 err = got_error_from_errno("asprintf");
1273 header = NULL;
1274 goto done;
1276 } else if (asprintf(&header, "commit %s%s",
1277 id_str ? id_str : "........................................",
1278 ncommits_str) == -1) {
1279 err = got_error_from_errno("asprintf");
1280 header = NULL;
1281 goto done;
1283 err = format_line(&wline, &width, header, view->ncols);
1284 if (err)
1285 goto done;
1287 werase(view->window);
1289 if (view_needs_focus_indication(view))
1290 wstandout(view->window);
1291 waddwstr(view->window, wline);
1292 while (width < view->ncols) {
1293 waddch(view->window, ' ');
1294 width++;
1296 if (view_needs_focus_indication(view))
1297 wstandend(view->window);
1298 free(wline);
1299 if (limit <= 1)
1300 goto done;
1302 /* Grow author column size if necessary. */
1303 entry = first;
1304 ncommits = 0;
1305 while (entry) {
1306 char *author;
1307 wchar_t *wauthor;
1308 int width;
1309 if (ncommits >= limit - 1)
1310 break;
1311 author = strdup(got_object_commit_get_author(entry->commit));
1312 if (author == NULL) {
1313 err = got_error_from_errno("strdup");
1314 goto done;
1316 err = format_author(&wauthor, &width, author, COLS);
1317 if (author_cols < width)
1318 author_cols = width;
1319 free(wauthor);
1320 free(author);
1321 entry = TAILQ_NEXT(entry, entry);
1324 entry = first;
1325 *last = first;
1326 ncommits = 0;
1327 while (entry) {
1328 if (ncommits >= limit - 1)
1329 break;
1330 if (ncommits == selected_idx)
1331 wstandout(view->window);
1332 err = draw_commit(view, entry->commit, entry->id, refs,
1333 author_cols);
1334 if (ncommits == selected_idx)
1335 wstandend(view->window);
1336 if (err)
1337 goto done;
1338 ncommits++;
1339 *last = entry;
1340 entry = TAILQ_NEXT(entry, entry);
1343 view_vborder(view);
1344 done:
1345 free(id_str);
1346 free(refs_str);
1347 free(ncommits_str);
1348 free(header);
1349 return err;
1352 static void
1353 scroll_up(struct tog_view *view,
1354 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1355 struct commit_queue *commits)
1357 struct commit_queue_entry *entry;
1358 int nscrolled = 0;
1360 entry = TAILQ_FIRST(&commits->head);
1361 if (*first_displayed_entry == entry)
1362 return;
1364 entry = *first_displayed_entry;
1365 while (entry && nscrolled < maxscroll) {
1366 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1367 if (entry) {
1368 *first_displayed_entry = entry;
1369 nscrolled++;
1374 static const struct got_error *
1375 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1376 pthread_cond_t *need_commits)
1378 int errcode;
1379 int max_wait = 20;
1381 halfdelay(1); /* fast refresh while loading commits */
1383 while (*commits_needed > 0) {
1384 if (*log_complete)
1385 break;
1387 /* Wake the log thread. */
1388 errcode = pthread_cond_signal(need_commits);
1389 if (errcode)
1390 return got_error_set_errno(errcode,
1391 "pthread_cond_signal");
1392 errcode = pthread_mutex_unlock(&tog_mutex);
1393 if (errcode)
1394 return got_error_set_errno(errcode,
1395 "pthread_mutex_unlock");
1396 pthread_yield();
1397 errcode = pthread_mutex_lock(&tog_mutex);
1398 if (errcode)
1399 return got_error_set_errno(errcode,
1400 "pthread_mutex_lock");
1402 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1404 * Thread is not done yet; lose a key press
1405 * and let the user retry... this way the GUI
1406 * remains interactive while logging deep paths
1407 * with few commits in history.
1409 return NULL;
1413 return NULL;
1416 static const struct got_error *
1417 scroll_down(struct tog_view *view,
1418 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1419 struct commit_queue_entry **last_displayed_entry,
1420 struct commit_queue *commits, int *log_complete, int *commits_needed,
1421 pthread_cond_t *need_commits)
1423 const struct got_error *err = NULL;
1424 struct commit_queue_entry *pentry;
1425 int nscrolled = 0;
1427 if (*last_displayed_entry == NULL)
1428 return NULL;
1430 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1431 if (pentry == NULL && !*log_complete) {
1433 * Ask the log thread for required amount of commits
1434 * plus some amount of pre-fetching.
1436 (*commits_needed) += maxscroll + 20;
1437 err = trigger_log_thread(0, commits_needed, log_complete,
1438 need_commits);
1439 if (err)
1440 return err;
1443 do {
1444 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1445 if (pentry == NULL)
1446 break;
1448 *last_displayed_entry = pentry;
1450 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1451 if (pentry == NULL)
1452 break;
1453 *first_displayed_entry = pentry;
1454 } while (++nscrolled < maxscroll);
1456 return err;
1459 static const struct got_error *
1460 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1461 struct got_commit_object *commit, struct got_object_id *commit_id,
1462 struct tog_view *log_view, struct got_reflist_head *refs,
1463 struct got_repository *repo)
1465 const struct got_error *err;
1466 struct got_object_qid *parent_id;
1467 struct tog_view *diff_view;
1469 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1470 if (diff_view == NULL)
1471 return got_error_from_errno("view_open");
1473 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1474 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1475 commit_id, log_view, refs, repo);
1476 if (err == NULL)
1477 *new_view = diff_view;
1478 return err;
1481 static const struct got_error *
1482 tree_view_visit_subtree(struct got_tree_object *subtree,
1483 struct tog_tree_view_state *s)
1485 struct tog_parent_tree *parent;
1487 parent = calloc(1, sizeof(*parent));
1488 if (parent == NULL)
1489 return got_error_from_errno("calloc");
1491 parent->tree = s->tree;
1492 parent->first_displayed_entry = s->first_displayed_entry;
1493 parent->selected_entry = s->selected_entry;
1494 parent->selected = s->selected;
1495 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1496 s->tree = subtree;
1497 s->entries = got_object_tree_get_entries(s->tree);
1498 s->selected = 0;
1499 s->first_displayed_entry = NULL;
1500 return NULL;
1504 static const struct got_error *
1505 browse_commit_tree(struct tog_view **new_view, int begin_x,
1506 struct commit_queue_entry *entry, const char *path,
1507 struct got_reflist_head *refs, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 struct got_tree_object *tree;
1511 struct got_tree_entry *te;
1512 struct tog_tree_view_state *s;
1513 struct tog_view *tree_view;
1514 char *slash, *subpath = NULL;
1515 const char *p;
1517 err = got_object_open_as_tree(&tree, repo,
1518 got_object_commit_get_tree_id(entry->commit));
1519 if (err)
1520 return err;
1522 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1523 if (tree_view == NULL)
1524 return got_error_from_errno("view_open");
1526 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1527 if (err) {
1528 got_object_tree_close(tree);
1529 return err;
1531 s = &tree_view->state.tree;
1533 *new_view = tree_view;
1535 /* Walk the path and open corresponding tree objects. */
1536 p = path;
1537 while (*p) {
1538 struct got_object_id *tree_id;
1540 while (p[0] == '/')
1541 p++;
1543 /* Ensure the correct subtree entry is selected. */
1544 slash = strchr(p, '/');
1545 if (slash == NULL)
1546 slash = strchr(p, '\0');
1547 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1548 if (strncmp(p, te->name, slash - p) == 0) {
1549 s->selected_entry = te;
1550 break;
1552 s->selected++;
1554 if (s->selected_entry == NULL) {
1555 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1556 break;
1558 if (s->tree != s->root)
1559 s->selected++; /* skip '..' */
1561 if (!S_ISDIR(s->selected_entry->mode)) {
1562 /* Jump to this file's entry. */
1563 s->first_displayed_entry = s->selected_entry;
1564 s->selected = 0;
1565 break;
1568 slash = strchr(p, '/');
1569 if (slash)
1570 subpath = strndup(path, slash - path);
1571 else
1572 subpath = strdup(path);
1573 if (subpath == NULL) {
1574 err = got_error_from_errno("strdup");
1575 break;
1578 err = got_object_id_by_path(&tree_id, repo, entry->id,
1579 subpath);
1580 if (err)
1581 break;
1583 err = got_object_open_as_tree(&tree, repo, tree_id);
1584 free(tree_id);
1585 if (err)
1586 break;
1588 err = tree_view_visit_subtree(tree, s);
1589 if (err) {
1590 got_object_tree_close(tree);
1591 break;
1593 if (slash == NULL)
1594 break;
1595 free(subpath);
1596 subpath = NULL;
1597 p = slash;
1600 free(subpath);
1601 return err;
1604 static void *
1605 log_thread(void *arg)
1607 const struct got_error *err = NULL;
1608 int errcode = 0;
1609 struct tog_log_thread_args *a = arg;
1610 int done = 0;
1612 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1613 if (err)
1614 return (void *)err;
1616 while (!done && !err) {
1617 err = queue_commits(a->graph, a->commits, 1, a->repo,
1618 a->in_repo_path);
1619 if (err) {
1620 if (err->code != GOT_ERR_ITER_COMPLETED)
1621 return (void *)err;
1622 err = NULL;
1623 done = 1;
1624 } else if (a->commits_needed > 0)
1625 a->commits_needed--;
1627 errcode = pthread_mutex_lock(&tog_mutex);
1628 if (errcode) {
1629 err = got_error_set_errno(errcode,
1630 "pthread_mutex_lock");
1631 break;
1632 } else if (*a->quit)
1633 done = 1;
1634 else if (*a->first_displayed_entry == NULL) {
1635 *a->first_displayed_entry =
1636 TAILQ_FIRST(&a->commits->head);
1637 *a->selected_entry = *a->first_displayed_entry;
1640 if (done)
1641 a->commits_needed = 0;
1642 else if (a->commits_needed == 0) {
1643 errcode = pthread_cond_wait(&a->need_commits,
1644 &tog_mutex);
1645 if (errcode)
1646 err = got_error_set_errno(errcode,
1647 "pthread_cond_wait");
1650 errcode = pthread_mutex_unlock(&tog_mutex);
1651 if (errcode && err == NULL)
1652 err = got_error_set_errno(errcode,
1653 "pthread_mutex_unlock");
1655 a->log_complete = 1;
1656 return (void *)err;
1659 static const struct got_error *
1660 stop_log_thread(struct tog_log_view_state *s)
1662 const struct got_error *err = NULL;
1663 int errcode;
1665 if (s->thread) {
1666 s->quit = 1;
1667 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1668 if (errcode)
1669 return got_error_set_errno(errcode,
1670 "pthread_cond_signal");
1671 errcode = pthread_mutex_unlock(&tog_mutex);
1672 if (errcode)
1673 return got_error_set_errno(errcode,
1674 "pthread_mutex_unlock");
1675 errcode = pthread_join(s->thread, (void **)&err);
1676 if (errcode)
1677 return got_error_set_errno(errcode, "pthread_join");
1678 errcode = pthread_mutex_lock(&tog_mutex);
1679 if (errcode)
1680 return got_error_set_errno(errcode,
1681 "pthread_mutex_lock");
1682 s->thread = NULL;
1685 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1686 if (errcode && err == NULL)
1687 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1689 if (s->thread_args.repo) {
1690 got_repo_close(s->thread_args.repo);
1691 s->thread_args.repo = NULL;
1694 if (s->thread_args.graph) {
1695 got_commit_graph_close(s->thread_args.graph);
1696 s->thread_args.graph = NULL;
1699 return err;
1702 static const struct got_error *
1703 close_log_view(struct tog_view *view)
1705 const struct got_error *err = NULL;
1706 struct tog_log_view_state *s = &view->state.log;
1708 err = stop_log_thread(s);
1709 free_commits(&s->commits);
1710 free(s->in_repo_path);
1711 s->in_repo_path = NULL;
1712 free(s->start_id);
1713 s->start_id = NULL;
1714 return err;
1717 static const struct got_error *
1718 search_start_log_view(struct tog_view *view)
1720 struct tog_log_view_state *s = &view->state.log;
1722 s->matched_entry = NULL;
1723 s->search_entry = NULL;
1724 return NULL;
1727 static int
1728 match_commit(struct got_commit_object *commit, const char *id_str,
1729 regex_t *regex)
1731 regmatch_t regmatch;
1733 if (regexec(regex, got_object_commit_get_author(commit), 1,
1734 &regmatch, 0) == 0 ||
1735 regexec(regex, got_object_commit_get_committer(commit), 1,
1736 &regmatch, 0) == 0 ||
1737 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1738 &regmatch, 0) == 0 ||
1739 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1740 return 1;
1742 return 0;
1745 static const struct got_error *
1746 search_next_log_view(struct tog_view *view)
1748 const struct got_error *err = NULL;
1749 struct tog_log_view_state *s = &view->state.log;
1750 struct commit_queue_entry *entry;
1752 if (!view->searching) {
1753 view->search_next_done = 1;
1754 return NULL;
1757 if (s->search_entry) {
1758 if (view->searching == TOG_SEARCH_FORWARD)
1759 entry = TAILQ_NEXT(s->search_entry, entry);
1760 else
1761 entry = TAILQ_PREV(s->search_entry,
1762 commit_queue_head, entry);
1763 } else if (s->matched_entry) {
1764 if (view->searching == TOG_SEARCH_FORWARD)
1765 entry = TAILQ_NEXT(s->selected_entry, entry);
1766 else
1767 entry = TAILQ_PREV(s->selected_entry,
1768 commit_queue_head, entry);
1769 } else {
1770 if (view->searching == TOG_SEARCH_FORWARD)
1771 entry = TAILQ_FIRST(&s->commits.head);
1772 else
1773 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1776 while (1) {
1777 char *id_str;
1778 if (entry == NULL) {
1779 if (s->thread_args.log_complete ||
1780 view->searching == TOG_SEARCH_BACKWARD) {
1781 view->search_next_done = 1;
1782 return NULL;
1785 * Poke the log thread for more commits and return,
1786 * allowing the main loop to make progress. Search
1787 * will resume at s->search_entry once we come back.
1789 s->thread_args.commits_needed++;
1790 return trigger_log_thread(1,
1791 &s->thread_args.commits_needed,
1792 &s->thread_args.log_complete,
1793 &s->thread_args.need_commits);
1796 err = got_object_id_str(&id_str, entry->id);
1797 if (err)
1798 return err;
1800 if (match_commit(entry->commit, id_str, &view->regex)) {
1801 view->search_next_done = 1;
1802 s->matched_entry = entry;
1803 free(id_str);
1804 break;
1806 free(id_str);
1807 s->search_entry = entry;
1808 if (view->searching == TOG_SEARCH_FORWARD)
1809 entry = TAILQ_NEXT(entry, entry);
1810 else
1811 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1814 if (s->matched_entry) {
1815 int cur = s->selected_entry->idx;
1816 while (cur < s->matched_entry->idx) {
1817 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1818 if (err)
1819 return err;
1820 cur++;
1822 while (cur > s->matched_entry->idx) {
1823 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1824 if (err)
1825 return err;
1826 cur--;
1830 s->search_entry = NULL;
1832 return NULL;
1835 static const struct got_error *
1836 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1837 struct got_reflist_head *refs, struct got_repository *repo,
1838 const char *head_ref_name, const char *path, int check_disk)
1840 const struct got_error *err = NULL;
1841 struct tog_log_view_state *s = &view->state.log;
1842 struct got_repository *thread_repo = NULL;
1843 struct got_commit_graph *thread_graph = NULL;
1844 int errcode;
1846 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1847 if (err != NULL)
1848 goto done;
1850 /* The commit queue only contains commits being displayed. */
1851 TAILQ_INIT(&s->commits.head);
1852 s->commits.ncommits = 0;
1854 s->refs = refs;
1855 s->repo = repo;
1856 s->head_ref_name = head_ref_name;
1857 s->start_id = got_object_id_dup(start_id);
1858 if (s->start_id == NULL) {
1859 err = got_error_from_errno("got_object_id_dup");
1860 goto done;
1863 view->show = show_log_view;
1864 view->input = input_log_view;
1865 view->close = close_log_view;
1866 view->search_start = search_start_log_view;
1867 view->search_next = search_next_log_view;
1869 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1870 if (err)
1871 goto done;
1872 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1873 0, thread_repo);
1874 if (err)
1875 goto done;
1877 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1878 if (errcode) {
1879 err = got_error_set_errno(errcode, "pthread_cond_init");
1880 goto done;
1883 s->thread_args.commits_needed = view->nlines;
1884 s->thread_args.graph = thread_graph;
1885 s->thread_args.commits = &s->commits;
1886 s->thread_args.in_repo_path = s->in_repo_path;
1887 s->thread_args.start_id = s->start_id;
1888 s->thread_args.repo = thread_repo;
1889 s->thread_args.log_complete = 0;
1890 s->thread_args.quit = &s->quit;
1891 s->thread_args.view = view;
1892 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1893 s->thread_args.selected_entry = &s->selected_entry;
1894 done:
1895 if (err)
1896 close_log_view(view);
1897 return err;
1900 static const struct got_error *
1901 show_log_view(struct tog_view *view)
1903 struct tog_log_view_state *s = &view->state.log;
1905 if (s->thread == NULL) {
1906 int errcode = pthread_create(&s->thread, NULL, log_thread,
1907 &s->thread_args);
1908 if (errcode)
1909 return got_error_set_errno(errcode, "pthread_create");
1912 return draw_commits(view, &s->last_displayed_entry,
1913 &s->selected_entry, s->first_displayed_entry,
1914 &s->commits, s->selected, view->nlines, s->refs,
1915 s->in_repo_path, s->thread_args.commits_needed);
1918 static const struct got_error *
1919 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1920 struct tog_view **focus_view, struct tog_view *view, int ch)
1922 const struct got_error *err = NULL;
1923 struct tog_log_view_state *s = &view->state.log;
1924 char *parent_path, *in_repo_path = NULL;
1925 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1926 int begin_x = 0;
1927 struct got_object_id *start_id;
1929 switch (ch) {
1930 case 'q':
1931 s->quit = 1;
1932 break;
1933 case 'k':
1934 case KEY_UP:
1935 case '<':
1936 case ',':
1937 if (s->first_displayed_entry == NULL)
1938 break;
1939 if (s->selected > 0)
1940 s->selected--;
1941 else
1942 scroll_up(view, &s->first_displayed_entry, 1,
1943 &s->commits);
1944 break;
1945 case KEY_PPAGE:
1946 case CTRL('b'):
1947 if (s->first_displayed_entry == NULL)
1948 break;
1949 if (TAILQ_FIRST(&s->commits.head) ==
1950 s->first_displayed_entry) {
1951 s->selected = 0;
1952 break;
1954 scroll_up(view, &s->first_displayed_entry,
1955 view->nlines, &s->commits);
1956 break;
1957 case 'j':
1958 case KEY_DOWN:
1959 case '>':
1960 case '.':
1961 if (s->first_displayed_entry == NULL)
1962 break;
1963 if (s->selected < MIN(view->nlines - 2,
1964 s->commits.ncommits - 1)) {
1965 s->selected++;
1966 break;
1968 err = scroll_down(view, &s->first_displayed_entry, 1,
1969 &s->last_displayed_entry, &s->commits,
1970 &s->thread_args.log_complete,
1971 &s->thread_args.commits_needed,
1972 &s->thread_args.need_commits);
1973 break;
1974 case KEY_NPAGE:
1975 case CTRL('f'): {
1976 struct commit_queue_entry *first;
1977 first = s->first_displayed_entry;
1978 if (first == NULL)
1979 break;
1980 err = scroll_down(view, &s->first_displayed_entry,
1981 view->nlines, &s->last_displayed_entry,
1982 &s->commits, &s->thread_args.log_complete,
1983 &s->thread_args.commits_needed,
1984 &s->thread_args.need_commits);
1985 if (first == s->first_displayed_entry &&
1986 s->selected < MIN(view->nlines - 2,
1987 s->commits.ncommits - 1)) {
1988 /* can't scroll further down */
1989 s->selected = MIN(view->nlines - 2,
1990 s->commits.ncommits - 1);
1992 err = NULL;
1993 break;
1995 case KEY_RESIZE:
1996 if (s->selected > view->nlines - 2)
1997 s->selected = view->nlines - 2;
1998 if (s->selected > s->commits.ncommits - 1)
1999 s->selected = s->commits.ncommits - 1;
2000 break;
2001 case KEY_ENTER:
2002 case ' ':
2003 case '\r':
2004 if (s->selected_entry == NULL)
2005 break;
2006 if (view_is_parent_view(view))
2007 begin_x = view_split_begin_x(view->begin_x);
2008 err = open_diff_view_for_commit(&diff_view, begin_x,
2009 s->selected_entry->commit, s->selected_entry->id,
2010 view, s->refs, s->repo);
2011 if (err)
2012 break;
2013 if (view_is_parent_view(view)) {
2014 err = view_close_child(view);
2015 if (err)
2016 return err;
2017 err = view_set_child(view, diff_view);
2018 if (err) {
2019 view_close(diff_view);
2020 break;
2022 *focus_view = diff_view;
2023 view->child_focussed = 1;
2024 } else
2025 *new_view = diff_view;
2026 break;
2027 case 't':
2028 if (s->selected_entry == NULL)
2029 break;
2030 if (view_is_parent_view(view))
2031 begin_x = view_split_begin_x(view->begin_x);
2032 err = browse_commit_tree(&tree_view, begin_x,
2033 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2034 if (err)
2035 break;
2036 if (view_is_parent_view(view)) {
2037 err = view_close_child(view);
2038 if (err)
2039 return err;
2040 err = view_set_child(view, tree_view);
2041 if (err) {
2042 view_close(tree_view);
2043 break;
2045 *focus_view = tree_view;
2046 view->child_focussed = 1;
2047 } else
2048 *new_view = tree_view;
2049 break;
2050 case KEY_BACKSPACE:
2051 if (strcmp(s->in_repo_path, "/") == 0)
2052 break;
2053 parent_path = dirname(s->in_repo_path);
2054 if (parent_path && strcmp(parent_path, ".") != 0) {
2055 err = stop_log_thread(s);
2056 if (err)
2057 return err;
2058 lv = view_open(view->nlines, view->ncols,
2059 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2060 if (lv == NULL)
2061 return got_error_from_errno(
2062 "view_open");
2063 err = open_log_view(lv, s->start_id, s->refs,
2064 s->repo, s->head_ref_name, parent_path, 0);
2065 if (err)
2066 return err;;
2067 if (view_is_parent_view(view))
2068 *new_view = lv;
2069 else {
2070 view_set_child(view->parent, lv);
2071 *focus_view = lv;
2073 return NULL;
2075 break;
2076 case CTRL('l'):
2077 err = stop_log_thread(s);
2078 if (err)
2079 return err;
2080 lv = view_open(view->nlines, view->ncols,
2081 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2082 if (lv == NULL)
2083 return got_error_from_errno("view_open");
2084 err = get_head_commit_id(&start_id, s->head_ref_name ?
2085 s->head_ref_name : GOT_REF_HEAD, s->repo);
2086 if (err)
2087 return err;
2088 in_repo_path = strdup(s->in_repo_path);
2089 if (in_repo_path == NULL) {
2090 free(start_id);
2091 return got_error_from_errno("strdup");
2093 err = open_log_view(lv, start_id, s->refs, s->repo,
2094 s->head_ref_name, in_repo_path, 0);
2095 if (err)
2096 return err;;
2097 *dead_view = view;
2098 *new_view = lv;
2099 break;
2100 default:
2101 break;
2104 return err;
2107 static const struct got_error *
2108 apply_unveil(const char *repo_path, const char *worktree_path)
2110 const struct got_error *error;
2112 if (repo_path && unveil(repo_path, "r") != 0)
2113 return got_error_from_errno2("unveil", repo_path);
2115 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2116 return got_error_from_errno2("unveil", worktree_path);
2118 if (unveil("/tmp", "rwc") != 0)
2119 return got_error_from_errno2("unveil", "/tmp");
2121 error = got_privsep_unveil_exec_helpers();
2122 if (error != NULL)
2123 return error;
2125 if (unveil(NULL, NULL) != 0)
2126 return got_error_from_errno("unveil");
2128 return NULL;
2131 static void
2132 init_curses(void)
2134 initscr();
2135 cbreak();
2136 halfdelay(1); /* Do fast refresh while initial view is loading. */
2137 noecho();
2138 nonl();
2139 intrflush(stdscr, FALSE);
2140 keypad(stdscr, TRUE);
2141 curs_set(0);
2142 signal(SIGWINCH, tog_sigwinch);
2145 static const struct got_error *
2146 cmd_log(int argc, char *argv[])
2148 const struct got_error *error;
2149 struct got_repository *repo = NULL;
2150 struct got_worktree *worktree = NULL;
2151 struct got_reflist_head refs;
2152 struct got_object_id *start_id = NULL;
2153 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2154 char *start_commit = NULL;
2155 int ch;
2156 struct tog_view *view;
2158 SIMPLEQ_INIT(&refs);
2160 #ifndef PROFILE
2161 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2162 NULL) == -1)
2163 err(1, "pledge");
2164 #endif
2166 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2167 switch (ch) {
2168 case 'c':
2169 start_commit = optarg;
2170 break;
2171 case 'r':
2172 repo_path = realpath(optarg, NULL);
2173 if (repo_path == NULL)
2174 err(1, "-r option");
2175 break;
2176 default:
2177 usage_log();
2178 /* NOTREACHED */
2182 argc -= optind;
2183 argv += optind;
2185 cwd = getcwd(NULL, 0);
2186 if (cwd == NULL) {
2187 error = got_error_from_errno("getcwd");
2188 goto done;
2190 error = got_worktree_open(&worktree, cwd);
2191 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2192 goto done;
2193 error = NULL;
2195 if (argc == 0) {
2196 path = strdup("");
2197 if (path == NULL) {
2198 error = got_error_from_errno("strdup");
2199 goto done;
2201 } else if (argc == 1) {
2202 if (worktree) {
2203 error = got_worktree_resolve_path(&path, worktree,
2204 argv[0]);
2205 if (error)
2206 goto done;
2207 } else {
2208 path = strdup(argv[0]);
2209 if (path == NULL) {
2210 error = got_error_from_errno("strdup");
2211 goto done;
2214 } else
2215 usage_log();
2217 repo_path = worktree ?
2218 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2219 if (repo_path == NULL) {
2220 error = got_error_from_errno("strdup");
2221 goto done;
2224 init_curses();
2226 error = got_repo_open(&repo, repo_path);
2227 if (error != NULL)
2228 goto done;
2230 error = apply_unveil(got_repo_get_path(repo),
2231 worktree ? got_worktree_get_root_path(worktree) : NULL);
2232 if (error)
2233 goto done;
2235 if (start_commit == NULL)
2236 error = get_head_commit_id(&start_id, worktree ?
2237 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2238 repo);
2239 else
2240 error = got_repo_match_object_id_prefix(&start_id,
2241 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2242 if (error != NULL)
2243 goto done;
2245 error = got_ref_list(&refs, repo);
2246 if (error)
2247 goto done;
2249 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2250 if (view == NULL) {
2251 error = got_error_from_errno("view_open");
2252 goto done;
2254 error = open_log_view(view, start_id, &refs, repo, worktree ?
2255 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2256 if (error)
2257 goto done;
2258 error = view_loop(view);
2259 done:
2260 free(repo_path);
2261 free(cwd);
2262 free(path);
2263 free(start_id);
2264 if (repo)
2265 got_repo_close(repo);
2266 if (worktree)
2267 got_worktree_close(worktree);
2268 got_ref_list_free(&refs);
2269 return error;
2272 __dead static void
2273 usage_diff(void)
2275 endwin();
2276 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2277 getprogname());
2278 exit(1);
2281 static char *
2282 parse_next_line(FILE *f, size_t *len)
2284 char *line;
2285 size_t linelen;
2286 size_t lineno;
2287 const char delim[3] = { '\0', '\0', '\0'};
2289 line = fparseln(f, &linelen, &lineno, delim, 0);
2290 if (len)
2291 *len = linelen;
2292 return line;
2295 static const struct got_error *
2296 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2297 int *last_displayed_line, int *eof, int max_lines,
2298 char *header)
2300 const struct got_error *err;
2301 int nlines = 0, nprinted = 0;
2302 char *line;
2303 size_t len;
2304 wchar_t *wline;
2305 int width;
2307 rewind(f);
2308 werase(view->window);
2310 if (header) {
2311 err = format_line(&wline, &width, header, view->ncols);
2312 if (err) {
2313 return err;
2316 if (view_needs_focus_indication(view))
2317 wstandout(view->window);
2318 waddwstr(view->window, wline);
2319 if (view_needs_focus_indication(view))
2320 wstandend(view->window);
2321 if (width < view->ncols - 1)
2322 waddch(view->window, '\n');
2324 if (max_lines <= 1)
2325 return NULL;
2326 max_lines--;
2329 *eof = 0;
2330 while (nprinted < max_lines) {
2331 line = parse_next_line(f, &len);
2332 if (line == NULL) {
2333 *eof = 1;
2334 break;
2336 if (++nlines < *first_displayed_line) {
2337 free(line);
2338 continue;
2341 err = format_line(&wline, &width, line, view->ncols);
2342 if (err) {
2343 free(line);
2344 return err;
2346 waddwstr(view->window, wline);
2347 if (width < view->ncols - 1)
2348 waddch(view->window, '\n');
2349 if (++nprinted == 1)
2350 *first_displayed_line = nlines;
2351 free(line);
2352 free(wline);
2353 wline = NULL;
2355 *last_displayed_line = nlines;
2357 view_vborder(view);
2359 if (*eof) {
2360 while (nprinted < view->nlines) {
2361 waddch(view->window, '\n');
2362 nprinted++;
2365 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2366 if (err) {
2367 return err;
2370 wstandout(view->window);
2371 waddwstr(view->window, wline);
2372 wstandend(view->window);
2375 return NULL;
2378 static char *
2379 get_datestr(time_t *time, char *datebuf)
2381 char *p, *s = ctime_r(time, datebuf);
2382 p = strchr(s, '\n');
2383 if (p)
2384 *p = '\0';
2385 return s;
2388 static const struct got_error *
2389 write_commit_info(struct got_object_id *commit_id,
2390 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2392 const struct got_error *err = NULL;
2393 char datebuf[26];
2394 struct got_commit_object *commit;
2395 char *id_str = NULL;
2396 time_t committer_time;
2397 const char *author, *committer;
2398 char *refs_str = NULL;
2400 if (refs) {
2401 err = build_refs_str(&refs_str, refs, commit_id);
2402 if (err)
2403 return err;
2406 err = got_object_open_as_commit(&commit, repo, commit_id);
2407 if (err)
2408 return err;
2410 err = got_object_id_str(&id_str, commit_id);
2411 if (err) {
2412 err = got_error_from_errno("got_object_id_str");
2413 goto done;
2416 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2417 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2418 err = got_error_from_errno("fprintf");
2419 goto done;
2421 if (fprintf(outfile, "from: %s\n",
2422 got_object_commit_get_author(commit)) < 0) {
2423 err = got_error_from_errno("fprintf");
2424 goto done;
2426 committer_time = got_object_commit_get_committer_time(commit);
2427 if (fprintf(outfile, "date: %s UTC\n",
2428 get_datestr(&committer_time, datebuf)) < 0) {
2429 err = got_error_from_errno("fprintf");
2430 goto done;
2432 author = got_object_commit_get_author(commit);
2433 committer = got_object_commit_get_committer(commit);
2434 if (strcmp(author, committer) != 0 &&
2435 fprintf(outfile, "via: %s\n", committer) < 0) {
2436 err = got_error_from_errno("fprintf");
2437 goto done;
2439 if (fprintf(outfile, "%s\n",
2440 got_object_commit_get_logmsg(commit)) < 0) {
2441 err = got_error_from_errno("fprintf");
2442 goto done;
2444 done:
2445 free(id_str);
2446 free(refs_str);
2447 got_object_commit_close(commit);
2448 return err;
2451 static const struct got_error *
2452 create_diff(struct tog_diff_view_state *s)
2454 const struct got_error *err = NULL;
2455 FILE *f = NULL;
2456 int obj_type;
2458 f = got_opentemp();
2459 if (f == NULL) {
2460 err = got_error_from_errno("got_opentemp");
2461 goto done;
2463 if (s->f && fclose(s->f) != 0) {
2464 err = got_error_from_errno("fclose");
2465 goto done;
2467 s->f = f;
2469 if (s->id1)
2470 err = got_object_get_type(&obj_type, s->repo, s->id1);
2471 else
2472 err = got_object_get_type(&obj_type, s->repo, s->id2);
2473 if (err)
2474 goto done;
2476 switch (obj_type) {
2477 case GOT_OBJ_TYPE_BLOB:
2478 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2479 s->diff_context, s->repo, f);
2480 break;
2481 case GOT_OBJ_TYPE_TREE:
2482 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2483 s->diff_context, s->repo, f);
2484 break;
2485 case GOT_OBJ_TYPE_COMMIT: {
2486 const struct got_object_id_queue *parent_ids;
2487 struct got_object_qid *pid;
2488 struct got_commit_object *commit2;
2490 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2491 if (err)
2492 break;
2493 /* Show commit info if we're diffing to a parent/root commit. */
2494 if (s->id1 == NULL)
2495 write_commit_info(s->id2, s->refs, s->repo, f);
2496 else {
2497 parent_ids = got_object_commit_get_parent_ids(commit2);
2498 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2499 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2500 write_commit_info(s->id2, s->refs,
2501 s->repo, f);
2502 break;
2506 got_object_commit_close(commit2);
2508 err = got_diff_objects_as_commits(s->id1, s->id2,
2509 s->diff_context, s->repo, f);
2510 break;
2512 default:
2513 err = got_error(GOT_ERR_OBJ_TYPE);
2514 break;
2516 done:
2517 if (f && fflush(f) != 0 && err == NULL)
2518 err = got_error_from_errno("fflush");
2519 return err;
2522 static void
2523 diff_view_indicate_progress(struct tog_view *view)
2525 mvwaddstr(view->window, 0, 0, "diffing...");
2526 update_panels();
2527 doupdate();
2530 static const struct got_error *
2531 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2532 struct got_object_id *id2, struct tog_view *log_view,
2533 struct got_reflist_head *refs, struct got_repository *repo)
2535 const struct got_error *err;
2537 if (id1 != NULL && id2 != NULL) {
2538 int type1, type2;
2539 err = got_object_get_type(&type1, repo, id1);
2540 if (err)
2541 return err;
2542 err = got_object_get_type(&type2, repo, id2);
2543 if (err)
2544 return err;
2546 if (type1 != type2)
2547 return got_error(GOT_ERR_OBJ_TYPE);
2550 if (id1) {
2551 view->state.diff.id1 = got_object_id_dup(id1);
2552 if (view->state.diff.id1 == NULL)
2553 return got_error_from_errno("got_object_id_dup");
2554 } else
2555 view->state.diff.id1 = NULL;
2557 view->state.diff.id2 = got_object_id_dup(id2);
2558 if (view->state.diff.id2 == NULL) {
2559 free(view->state.diff.id1);
2560 view->state.diff.id1 = NULL;
2561 return got_error_from_errno("got_object_id_dup");
2563 view->state.diff.f = NULL;
2564 view->state.diff.first_displayed_line = 1;
2565 view->state.diff.last_displayed_line = view->nlines;
2566 view->state.diff.diff_context = 3;
2567 view->state.diff.log_view = log_view;
2568 view->state.diff.repo = repo;
2569 view->state.diff.refs = refs;
2571 if (log_view && view_is_splitscreen(view))
2572 show_log_view(log_view); /* draw vborder */
2573 diff_view_indicate_progress(view);
2575 err = create_diff(&view->state.diff);
2576 if (err) {
2577 free(view->state.diff.id1);
2578 view->state.diff.id1 = NULL;
2579 free(view->state.diff.id2);
2580 view->state.diff.id2 = NULL;
2581 return err;
2584 view->show = show_diff_view;
2585 view->input = input_diff_view;
2586 view->close = close_diff_view;
2588 return NULL;
2591 static const struct got_error *
2592 close_diff_view(struct tog_view *view)
2594 const struct got_error *err = NULL;
2596 free(view->state.diff.id1);
2597 view->state.diff.id1 = NULL;
2598 free(view->state.diff.id2);
2599 view->state.diff.id2 = NULL;
2600 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2601 err = got_error_from_errno("fclose");
2602 return err;
2605 static const struct got_error *
2606 show_diff_view(struct tog_view *view)
2608 const struct got_error *err;
2609 struct tog_diff_view_state *s = &view->state.diff;
2610 char *id_str1 = NULL, *id_str2, *header;
2612 if (s->id1) {
2613 err = got_object_id_str(&id_str1, s->id1);
2614 if (err)
2615 return err;
2617 err = got_object_id_str(&id_str2, s->id2);
2618 if (err)
2619 return err;
2621 if (asprintf(&header, "diff %s %s",
2622 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2623 err = got_error_from_errno("asprintf");
2624 free(id_str1);
2625 free(id_str2);
2626 return err;
2628 free(id_str1);
2629 free(id_str2);
2631 return draw_file(view, s->f, &s->first_displayed_line,
2632 &s->last_displayed_line, &s->eof, view->nlines,
2633 header);
2636 static const struct got_error *
2637 set_selected_commit(struct tog_diff_view_state *s,
2638 struct commit_queue_entry *entry)
2640 const struct got_error *err;
2641 const struct got_object_id_queue *parent_ids;
2642 struct got_commit_object *selected_commit;
2643 struct got_object_qid *pid;
2645 free(s->id2);
2646 s->id2 = got_object_id_dup(entry->id);
2647 if (s->id2 == NULL)
2648 return got_error_from_errno("got_object_id_dup");
2650 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2651 if (err)
2652 return err;
2653 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2654 free(s->id1);
2655 pid = SIMPLEQ_FIRST(parent_ids);
2656 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2657 got_object_commit_close(selected_commit);
2658 return NULL;
2661 static const struct got_error *
2662 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2663 struct tog_view **focus_view, struct tog_view *view, int ch)
2665 const struct got_error *err = NULL;
2666 struct tog_diff_view_state *s = &view->state.diff;
2667 struct tog_log_view_state *ls;
2668 struct commit_queue_entry *entry;
2669 int i;
2671 switch (ch) {
2672 case 'k':
2673 case KEY_UP:
2674 if (s->first_displayed_line > 1)
2675 s->first_displayed_line--;
2676 break;
2677 case KEY_PPAGE:
2678 case CTRL('b'):
2679 if (s->first_displayed_line == 1)
2680 break;
2681 i = 0;
2682 while (i++ < view->nlines - 1 &&
2683 s->first_displayed_line > 1)
2684 s->first_displayed_line--;
2685 break;
2686 case 'j':
2687 case KEY_DOWN:
2688 if (!s->eof)
2689 s->first_displayed_line++;
2690 break;
2691 case KEY_NPAGE:
2692 case CTRL('f'):
2693 case ' ':
2694 if (s->eof)
2695 break;
2696 i = 0;
2697 while (!s->eof && i++ < view->nlines - 1) {
2698 char *line;
2699 line = parse_next_line(s->f, NULL);
2700 s->first_displayed_line++;
2701 if (line == NULL)
2702 break;
2704 break;
2705 case '[':
2706 if (s->diff_context > 0) {
2707 s->diff_context--;
2708 diff_view_indicate_progress(view);
2709 err = create_diff(s);
2711 break;
2712 case ']':
2713 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2714 s->diff_context++;
2715 diff_view_indicate_progress(view);
2716 err = create_diff(s);
2718 break;
2719 case '<':
2720 case ',':
2721 if (s->log_view == NULL)
2722 break;
2723 ls = &s->log_view->state.log;
2724 entry = TAILQ_PREV(ls->selected_entry,
2725 commit_queue_head, entry);
2726 if (entry == NULL)
2727 break;
2729 err = input_log_view(NULL, NULL, NULL, s->log_view,
2730 KEY_UP);
2731 if (err)
2732 break;
2734 err = set_selected_commit(s, entry);
2735 if (err)
2736 break;
2738 s->first_displayed_line = 1;
2739 s->last_displayed_line = view->nlines;
2741 diff_view_indicate_progress(view);
2742 err = create_diff(s);
2743 break;
2744 case '>':
2745 case '.':
2746 if (s->log_view == NULL)
2747 break;
2748 ls = &s->log_view->state.log;
2750 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2751 ls->thread_args.commits_needed++;
2753 /* Display "loading..." in log view. */
2754 show_log_view(s->log_view);
2755 update_panels();
2756 doupdate();
2758 err = trigger_log_thread(1 /* load_all */,
2759 &ls->thread_args.commits_needed,
2760 &ls->thread_args.log_complete,
2761 &ls->thread_args.need_commits);
2762 if (err)
2763 break;
2765 err = input_log_view(NULL, NULL, NULL, s->log_view,
2766 KEY_DOWN);
2767 if (err)
2768 break;
2770 entry = TAILQ_NEXT(ls->selected_entry, entry);
2771 if (entry == NULL)
2772 break;
2774 err = set_selected_commit(s, entry);
2775 if (err)
2776 break;
2778 s->first_displayed_line = 1;
2779 s->last_displayed_line = view->nlines;
2781 diff_view_indicate_progress(view);
2782 err = create_diff(s);
2783 break;
2784 default:
2785 break;
2788 return err;
2791 static const struct got_error *
2792 cmd_diff(int argc, char *argv[])
2794 const struct got_error *error = NULL;
2795 struct got_repository *repo = NULL;
2796 struct got_reflist_head refs;
2797 struct got_object_id *id1 = NULL, *id2 = NULL;
2798 char *repo_path = NULL;
2799 char *id_str1 = NULL, *id_str2 = NULL;
2800 int ch;
2801 struct tog_view *view;
2803 SIMPLEQ_INIT(&refs);
2805 #ifndef PROFILE
2806 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2807 NULL) == -1)
2808 err(1, "pledge");
2809 #endif
2811 while ((ch = getopt(argc, argv, "")) != -1) {
2812 switch (ch) {
2813 default:
2814 usage_diff();
2815 /* NOTREACHED */
2819 argc -= optind;
2820 argv += optind;
2822 if (argc == 0) {
2823 usage_diff(); /* TODO show local worktree changes */
2824 } else if (argc == 2) {
2825 repo_path = getcwd(NULL, 0);
2826 if (repo_path == NULL)
2827 return got_error_from_errno("getcwd");
2828 id_str1 = argv[0];
2829 id_str2 = argv[1];
2830 } else if (argc == 3) {
2831 repo_path = realpath(argv[0], NULL);
2832 if (repo_path == NULL)
2833 return got_error_from_errno2("realpath", argv[0]);
2834 id_str1 = argv[1];
2835 id_str2 = argv[2];
2836 } else
2837 usage_diff();
2839 init_curses();
2841 error = got_repo_open(&repo, repo_path);
2842 if (error)
2843 goto done;
2845 error = apply_unveil(got_repo_get_path(repo), NULL);
2846 if (error)
2847 goto done;
2849 error = got_repo_match_object_id_prefix(&id1, id_str1,
2850 GOT_OBJ_TYPE_ANY, repo);
2851 if (error)
2852 goto done;
2854 error = got_repo_match_object_id_prefix(&id2, id_str2,
2855 GOT_OBJ_TYPE_ANY, repo);
2856 if (error)
2857 goto done;
2859 error = got_ref_list(&refs, repo);
2860 if (error)
2861 goto done;
2863 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2864 if (view == NULL) {
2865 error = got_error_from_errno("view_open");
2866 goto done;
2868 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2869 if (error)
2870 goto done;
2871 error = view_loop(view);
2872 done:
2873 free(repo_path);
2874 if (repo)
2875 got_repo_close(repo);
2876 got_ref_list_free(&refs);
2877 return error;
2880 __dead static void
2881 usage_blame(void)
2883 endwin();
2884 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2885 getprogname());
2886 exit(1);
2889 struct tog_blame_line {
2890 int annotated;
2891 struct got_object_id *id;
2894 static const struct got_error *
2895 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2896 const char *path, struct tog_blame_line *lines, int nlines,
2897 int blame_complete, int selected_line, int *first_displayed_line,
2898 int *last_displayed_line, int *eof, int max_lines)
2900 const struct got_error *err;
2901 int lineno = 0, nprinted = 0;
2902 char *line;
2903 size_t len;
2904 wchar_t *wline;
2905 int width, wlimit;
2906 struct tog_blame_line *blame_line;
2907 struct got_object_id *prev_id = NULL;
2908 char *id_str;
2910 err = got_object_id_str(&id_str, id);
2911 if (err)
2912 return err;
2914 rewind(f);
2915 werase(view->window);
2917 if (asprintf(&line, "commit %s", id_str) == -1) {
2918 err = got_error_from_errno("asprintf");
2919 free(id_str);
2920 return err;
2923 err = format_line(&wline, &width, line, view->ncols);
2924 free(line);
2925 line = NULL;
2926 if (view_needs_focus_indication(view))
2927 wstandout(view->window);
2928 waddwstr(view->window, wline);
2929 if (view_needs_focus_indication(view))
2930 wstandend(view->window);
2931 free(wline);
2932 wline = NULL;
2933 if (width < view->ncols - 1)
2934 waddch(view->window, '\n');
2936 if (asprintf(&line, "[%d/%d] %s%s",
2937 *first_displayed_line - 1 + selected_line, nlines,
2938 blame_complete ? "" : "annotating... ", path) == -1) {
2939 free(id_str);
2940 return got_error_from_errno("asprintf");
2942 free(id_str);
2943 err = format_line(&wline, &width, line, view->ncols);
2944 free(line);
2945 line = NULL;
2946 if (err)
2947 return err;
2948 waddwstr(view->window, wline);
2949 free(wline);
2950 wline = NULL;
2951 if (width < view->ncols - 1)
2952 waddch(view->window, '\n');
2954 *eof = 0;
2955 while (nprinted < max_lines - 2) {
2956 line = parse_next_line(f, &len);
2957 if (line == NULL) {
2958 *eof = 1;
2959 break;
2961 if (++lineno < *first_displayed_line) {
2962 free(line);
2963 continue;
2966 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2967 err = format_line(&wline, &width, line, wlimit);
2968 if (err) {
2969 free(line);
2970 return err;
2973 if (view->focussed && nprinted == selected_line - 1)
2974 wstandout(view->window);
2976 blame_line = &lines[lineno - 1];
2977 if (blame_line->annotated && prev_id &&
2978 got_object_id_cmp(prev_id, blame_line->id) == 0)
2979 waddstr(view->window, " ");
2980 else if (blame_line->annotated) {
2981 char *id_str;
2982 err = got_object_id_str(&id_str, blame_line->id);
2983 if (err) {
2984 free(line);
2985 free(wline);
2986 return err;
2988 wprintw(view->window, "%.8s ", id_str);
2989 free(id_str);
2990 prev_id = blame_line->id;
2991 } else {
2992 waddstr(view->window, "........ ");
2993 prev_id = NULL;
2996 waddwstr(view->window, wline);
2997 while (width < wlimit) {
2998 waddch(view->window, ' ');
2999 width++;
3001 if (view->focussed && nprinted == selected_line - 1)
3002 wstandend(view->window);
3003 if (++nprinted == 1)
3004 *first_displayed_line = lineno;
3005 free(line);
3006 free(wline);
3007 wline = NULL;
3009 *last_displayed_line = lineno;
3011 view_vborder(view);
3013 return NULL;
3016 static const struct got_error *
3017 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3019 const struct got_error *err = NULL;
3020 struct tog_blame_cb_args *a = arg;
3021 struct tog_blame_line *line;
3022 int errcode;
3024 if (nlines != a->nlines ||
3025 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3026 return got_error(GOT_ERR_RANGE);
3028 errcode = pthread_mutex_lock(&tog_mutex);
3029 if (errcode)
3030 return got_error_set_errno(errcode, "pthread_mutex_lock");
3032 if (*a->quit) { /* user has quit the blame view */
3033 err = got_error(GOT_ERR_ITER_COMPLETED);
3034 goto done;
3037 if (lineno == -1)
3038 goto done; /* no change in this commit */
3040 line = &a->lines[lineno - 1];
3041 if (line->annotated)
3042 goto done;
3044 line->id = got_object_id_dup(id);
3045 if (line->id == NULL) {
3046 err = got_error_from_errno("got_object_id_dup");
3047 goto done;
3049 line->annotated = 1;
3050 done:
3051 errcode = pthread_mutex_unlock(&tog_mutex);
3052 if (errcode)
3053 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3054 return err;
3057 static void *
3058 blame_thread(void *arg)
3060 const struct got_error *err;
3061 struct tog_blame_thread_args *ta = arg;
3062 struct tog_blame_cb_args *a = ta->cb_args;
3063 int errcode;
3065 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3066 blame_cb, ta->cb_args);
3068 errcode = pthread_mutex_lock(&tog_mutex);
3069 if (errcode)
3070 return (void *)got_error_set_errno(errcode,
3071 "pthread_mutex_lock");
3073 got_repo_close(ta->repo);
3074 ta->repo = NULL;
3075 *ta->complete = 1;
3077 errcode = pthread_mutex_unlock(&tog_mutex);
3078 if (errcode && err == NULL)
3079 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3081 return (void *)err;
3084 static struct got_object_id *
3085 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3086 int selected_line)
3088 struct tog_blame_line *line;
3090 line = &lines[first_displayed_line - 1 + selected_line - 1];
3091 if (!line->annotated)
3092 return NULL;
3094 return line->id;
3097 static const struct got_error *
3098 stop_blame(struct tog_blame *blame)
3100 const struct got_error *err = NULL;
3101 int i;
3103 if (blame->thread) {
3104 int errcode;
3105 errcode = pthread_mutex_unlock(&tog_mutex);
3106 if (errcode)
3107 return got_error_set_errno(errcode,
3108 "pthread_mutex_unlock");
3109 errcode = pthread_join(blame->thread, (void **)&err);
3110 if (errcode)
3111 return got_error_set_errno(errcode, "pthread_join");
3112 errcode = pthread_mutex_lock(&tog_mutex);
3113 if (errcode)
3114 return got_error_set_errno(errcode,
3115 "pthread_mutex_lock");
3116 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3117 err = NULL;
3118 blame->thread = NULL;
3120 if (blame->thread_args.repo) {
3121 got_repo_close(blame->thread_args.repo);
3122 blame->thread_args.repo = NULL;
3124 if (blame->f) {
3125 if (fclose(blame->f) != 0 && err == NULL)
3126 err = got_error_from_errno("fclose");
3127 blame->f = NULL;
3129 if (blame->lines) {
3130 for (i = 0; i < blame->nlines; i++)
3131 free(blame->lines[i].id);
3132 free(blame->lines);
3133 blame->lines = NULL;
3135 free(blame->cb_args.commit_id);
3136 blame->cb_args.commit_id = NULL;
3138 return err;
3141 static const struct got_error *
3142 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3143 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3144 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3145 struct got_repository *repo)
3147 const struct got_error *err = NULL;
3148 struct got_blob_object *blob = NULL;
3149 struct got_repository *thread_repo = NULL;
3150 struct got_object_id *obj_id = NULL;
3151 int obj_type;
3153 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3154 if (err)
3155 return err;
3156 if (obj_id == NULL)
3157 return got_error(GOT_ERR_NO_OBJ);
3159 err = got_object_get_type(&obj_type, repo, obj_id);
3160 if (err)
3161 goto done;
3163 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3164 err = got_error(GOT_ERR_OBJ_TYPE);
3165 goto done;
3168 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3169 if (err)
3170 goto done;
3171 blame->f = got_opentemp();
3172 if (blame->f == NULL) {
3173 err = got_error_from_errno("got_opentemp");
3174 goto done;
3176 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3177 &blame->line_offsets, blame->f, blob);
3178 if (err)
3179 goto done;
3181 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3182 if (blame->lines == NULL) {
3183 err = got_error_from_errno("calloc");
3184 goto done;
3187 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3188 if (err)
3189 goto done;
3191 blame->cb_args.view = view;
3192 blame->cb_args.lines = blame->lines;
3193 blame->cb_args.nlines = blame->nlines;
3194 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3195 if (blame->cb_args.commit_id == NULL) {
3196 err = got_error_from_errno("got_object_id_dup");
3197 goto done;
3199 blame->cb_args.quit = done;
3201 blame->thread_args.path = path;
3202 blame->thread_args.repo = thread_repo;
3203 blame->thread_args.cb_args = &blame->cb_args;
3204 blame->thread_args.complete = blame_complete;
3205 *blame_complete = 0;
3207 done:
3208 if (blob)
3209 got_object_blob_close(blob);
3210 free(obj_id);
3211 if (err)
3212 stop_blame(blame);
3213 return err;
3216 static const struct got_error *
3217 open_blame_view(struct tog_view *view, char *path,
3218 struct got_object_id *commit_id, struct got_reflist_head *refs,
3219 struct got_repository *repo)
3221 const struct got_error *err = NULL;
3222 struct tog_blame_view_state *s = &view->state.blame;
3224 SIMPLEQ_INIT(&s->blamed_commits);
3226 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3227 if (err)
3228 return err;
3230 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3231 s->first_displayed_line = 1;
3232 s->last_displayed_line = view->nlines;
3233 s->selected_line = 1;
3234 s->blame_complete = 0;
3235 s->path = path;
3236 if (s->path == NULL)
3237 return got_error_from_errno("open_blame_view");
3238 s->repo = repo;
3239 s->refs = refs;
3240 s->commit_id = commit_id;
3241 memset(&s->blame, 0, sizeof(s->blame));
3243 view->show = show_blame_view;
3244 view->input = input_blame_view;
3245 view->close = close_blame_view;
3246 view->search_start = search_start_blame_view;
3247 view->search_next = search_next_blame_view;
3249 return run_blame(&s->blame, view, &s->blame_complete,
3250 &s->first_displayed_line, &s->last_displayed_line,
3251 &s->selected_line, &s->done, &s->eof, s->path,
3252 s->blamed_commit->id, s->repo);
3255 static const struct got_error *
3256 close_blame_view(struct tog_view *view)
3258 const struct got_error *err = NULL;
3259 struct tog_blame_view_state *s = &view->state.blame;
3261 if (s->blame.thread)
3262 err = stop_blame(&s->blame);
3264 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3265 struct got_object_qid *blamed_commit;
3266 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3267 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3268 got_object_qid_free(blamed_commit);
3271 free(s->path);
3273 return err;
3276 static const struct got_error *
3277 search_start_blame_view(struct tog_view *view)
3279 struct tog_blame_view_state *s = &view->state.blame;
3281 s->matched_line = 0;
3282 return NULL;
3285 static int
3286 match_line(const char *line, regex_t *regex)
3288 regmatch_t regmatch;
3290 return regexec(regex, line, 1, &regmatch, 0) == 0;
3294 static const struct got_error *
3295 search_next_blame_view(struct tog_view *view)
3297 struct tog_blame_view_state *s = &view->state.blame;
3298 int lineno;
3300 if (!view->searching) {
3301 view->search_next_done = 1;
3302 return NULL;
3305 if (s->matched_line) {
3306 if (view->searching == TOG_SEARCH_FORWARD)
3307 lineno = s->matched_line + 1;
3308 else
3309 lineno = s->matched_line - 1;
3310 } else {
3311 if (view->searching == TOG_SEARCH_FORWARD)
3312 lineno = 1;
3313 else
3314 lineno = s->blame.nlines;
3317 while (1) {
3318 char *line = NULL;
3319 off_t offset;
3320 size_t len;
3322 if (lineno <= 0 || lineno > s->blame.nlines) {
3323 if (s->matched_line == 0) {
3324 view->search_next_done = 1;
3325 free(line);
3326 break;
3329 if (view->searching == TOG_SEARCH_FORWARD)
3330 lineno = 1;
3331 else
3332 lineno = s->blame.nlines;
3335 offset = s->blame.line_offsets[lineno - 1];
3336 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3337 free(line);
3338 return got_error_from_errno("fseeko");
3340 free(line);
3341 line = parse_next_line(s->blame.f, &len);
3342 if (line && match_line(line, &view->regex)) {
3343 view->search_next_done = 1;
3344 s->matched_line = lineno;
3345 free(line);
3346 break;
3348 free(line);
3349 if (view->searching == TOG_SEARCH_FORWARD)
3350 lineno++;
3351 else
3352 lineno--;
3355 if (s->matched_line) {
3356 s->first_displayed_line = s->matched_line;
3357 s->selected_line = 1;
3360 return NULL;
3363 static const struct got_error *
3364 show_blame_view(struct tog_view *view)
3366 const struct got_error *err = NULL;
3367 struct tog_blame_view_state *s = &view->state.blame;
3368 int errcode;
3370 if (s->blame.thread == NULL) {
3371 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3372 &s->blame.thread_args);
3373 if (errcode)
3374 return got_error_set_errno(errcode, "pthread_create");
3377 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3378 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3379 s->selected_line, &s->first_displayed_line,
3380 &s->last_displayed_line, &s->eof, view->nlines);
3382 view_vborder(view);
3383 return err;
3386 static const struct got_error *
3387 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3388 struct tog_view **focus_view, struct tog_view *view, int ch)
3390 const struct got_error *err = NULL, *thread_err = NULL;
3391 struct tog_view *diff_view;
3392 struct tog_blame_view_state *s = &view->state.blame;
3393 int begin_x = 0;
3395 switch (ch) {
3396 case 'q':
3397 s->done = 1;
3398 break;
3399 case 'k':
3400 case KEY_UP:
3401 if (s->selected_line > 1)
3402 s->selected_line--;
3403 else if (s->selected_line == 1 &&
3404 s->first_displayed_line > 1)
3405 s->first_displayed_line--;
3406 break;
3407 case KEY_PPAGE:
3408 if (s->first_displayed_line == 1) {
3409 s->selected_line = 1;
3410 break;
3412 if (s->first_displayed_line > view->nlines - 2)
3413 s->first_displayed_line -=
3414 (view->nlines - 2);
3415 else
3416 s->first_displayed_line = 1;
3417 break;
3418 case 'j':
3419 case KEY_DOWN:
3420 if (s->selected_line < view->nlines - 2 &&
3421 s->first_displayed_line +
3422 s->selected_line <= s->blame.nlines)
3423 s->selected_line++;
3424 else if (s->last_displayed_line <
3425 s->blame.nlines)
3426 s->first_displayed_line++;
3427 break;
3428 case 'b':
3429 case 'p': {
3430 struct got_object_id *id = NULL;
3431 id = get_selected_commit_id(s->blame.lines,
3432 s->first_displayed_line, s->selected_line);
3433 if (id == NULL)
3434 break;
3435 if (ch == 'p') {
3436 struct got_commit_object *commit;
3437 struct got_object_qid *pid;
3438 struct got_object_id *blob_id = NULL;
3439 int obj_type;
3440 err = got_object_open_as_commit(&commit,
3441 s->repo, id);
3442 if (err)
3443 break;
3444 pid = SIMPLEQ_FIRST(
3445 got_object_commit_get_parent_ids(commit));
3446 if (pid == NULL) {
3447 got_object_commit_close(commit);
3448 break;
3450 /* Check if path history ends here. */
3451 err = got_object_id_by_path(&blob_id, s->repo,
3452 pid->id, s->path);
3453 if (err) {
3454 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3455 err = NULL;
3456 got_object_commit_close(commit);
3457 break;
3459 err = got_object_get_type(&obj_type, s->repo,
3460 blob_id);
3461 free(blob_id);
3462 /* Can't blame non-blob type objects. */
3463 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3464 got_object_commit_close(commit);
3465 break;
3467 err = got_object_qid_alloc(&s->blamed_commit,
3468 pid->id);
3469 got_object_commit_close(commit);
3470 } else {
3471 if (got_object_id_cmp(id,
3472 s->blamed_commit->id) == 0)
3473 break;
3474 err = got_object_qid_alloc(&s->blamed_commit,
3475 id);
3477 if (err)
3478 break;
3479 s->done = 1;
3480 thread_err = stop_blame(&s->blame);
3481 s->done = 0;
3482 if (thread_err)
3483 break;
3484 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3485 s->blamed_commit, entry);
3486 err = run_blame(&s->blame, view, &s->blame_complete,
3487 &s->first_displayed_line, &s->last_displayed_line,
3488 &s->selected_line, &s->done, &s->eof,
3489 s->path, s->blamed_commit->id, s->repo);
3490 if (err)
3491 break;
3492 break;
3494 case 'B': {
3495 struct got_object_qid *first;
3496 first = SIMPLEQ_FIRST(&s->blamed_commits);
3497 if (!got_object_id_cmp(first->id, s->commit_id))
3498 break;
3499 s->done = 1;
3500 thread_err = stop_blame(&s->blame);
3501 s->done = 0;
3502 if (thread_err)
3503 break;
3504 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3505 got_object_qid_free(s->blamed_commit);
3506 s->blamed_commit =
3507 SIMPLEQ_FIRST(&s->blamed_commits);
3508 err = run_blame(&s->blame, view, &s->blame_complete,
3509 &s->first_displayed_line, &s->last_displayed_line,
3510 &s->selected_line, &s->done, &s->eof, s->path,
3511 s->blamed_commit->id, s->repo);
3512 if (err)
3513 break;
3514 break;
3516 case KEY_ENTER:
3517 case '\r': {
3518 struct got_object_id *id = NULL;
3519 struct got_object_qid *pid;
3520 struct got_commit_object *commit = NULL;
3521 id = get_selected_commit_id(s->blame.lines,
3522 s->first_displayed_line, s->selected_line);
3523 if (id == NULL)
3524 break;
3525 err = got_object_open_as_commit(&commit, s->repo, id);
3526 if (err)
3527 break;
3528 pid = SIMPLEQ_FIRST(
3529 got_object_commit_get_parent_ids(commit));
3530 if (view_is_parent_view(view))
3531 begin_x = view_split_begin_x(view->begin_x);
3532 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3533 if (diff_view == NULL) {
3534 got_object_commit_close(commit);
3535 err = got_error_from_errno("view_open");
3536 break;
3538 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3539 id, NULL, s->refs, s->repo);
3540 got_object_commit_close(commit);
3541 if (err) {
3542 view_close(diff_view);
3543 break;
3545 if (view_is_parent_view(view)) {
3546 err = view_close_child(view);
3547 if (err)
3548 break;
3549 err = view_set_child(view, diff_view);
3550 if (err) {
3551 view_close(diff_view);
3552 break;
3554 *focus_view = diff_view;
3555 view->child_focussed = 1;
3556 } else
3557 *new_view = diff_view;
3558 if (err)
3559 break;
3560 break;
3562 case KEY_NPAGE:
3563 case ' ':
3564 if (s->last_displayed_line >= s->blame.nlines &&
3565 s->selected_line >= MIN(s->blame.nlines,
3566 view->nlines - 2)) {
3567 break;
3569 if (s->last_displayed_line >= s->blame.nlines &&
3570 s->selected_line < view->nlines - 2) {
3571 s->selected_line = MIN(s->blame.nlines,
3572 view->nlines - 2);
3573 break;
3575 if (s->last_displayed_line + view->nlines - 2
3576 <= s->blame.nlines)
3577 s->first_displayed_line +=
3578 view->nlines - 2;
3579 else
3580 s->first_displayed_line =
3581 s->blame.nlines -
3582 (view->nlines - 3);
3583 break;
3584 case KEY_RESIZE:
3585 if (s->selected_line > view->nlines - 2) {
3586 s->selected_line = MIN(s->blame.nlines,
3587 view->nlines - 2);
3589 break;
3590 default:
3591 break;
3593 return thread_err ? thread_err : err;
3596 static const struct got_error *
3597 cmd_blame(int argc, char *argv[])
3599 const struct got_error *error;
3600 struct got_repository *repo = NULL;
3601 struct got_reflist_head refs;
3602 struct got_worktree *worktree = NULL;
3603 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3604 struct got_object_id *commit_id = NULL;
3605 char *commit_id_str = NULL;
3606 int ch;
3607 struct tog_view *view;
3609 SIMPLEQ_INIT(&refs);
3611 #ifndef PROFILE
3612 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3613 NULL) == -1)
3614 err(1, "pledge");
3615 #endif
3617 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3618 switch (ch) {
3619 case 'c':
3620 commit_id_str = optarg;
3621 break;
3622 case 'r':
3623 repo_path = realpath(optarg, NULL);
3624 if (repo_path == NULL)
3625 err(1, "-r option");
3626 break;
3627 default:
3628 usage_blame();
3629 /* NOTREACHED */
3633 argc -= optind;
3634 argv += optind;
3636 if (argc == 1)
3637 path = argv[0];
3638 else
3639 usage_blame();
3641 cwd = getcwd(NULL, 0);
3642 if (cwd == NULL) {
3643 error = got_error_from_errno("getcwd");
3644 goto done;
3646 if (repo_path == NULL) {
3647 error = got_worktree_open(&worktree, cwd);
3648 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3649 goto done;
3650 else
3651 error = NULL;
3652 if (worktree) {
3653 repo_path =
3654 strdup(got_worktree_get_repo_path(worktree));
3655 if (repo_path == NULL)
3656 error = got_error_from_errno("strdup");
3657 if (error)
3658 goto done;
3659 } else {
3660 repo_path = strdup(cwd);
3661 if (repo_path == NULL) {
3662 error = got_error_from_errno("strdup");
3663 goto done;
3668 init_curses();
3670 error = got_repo_open(&repo, repo_path);
3671 if (error != NULL)
3672 goto done;
3674 error = apply_unveil(got_repo_get_path(repo), NULL);
3675 if (error)
3676 goto done;
3678 if (worktree) {
3679 const char *prefix = got_worktree_get_path_prefix(worktree);
3680 char *p, *worktree_subdir = cwd +
3681 strlen(got_worktree_get_root_path(worktree));
3682 if (asprintf(&p, "%s%s%s%s%s",
3683 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3684 worktree_subdir, worktree_subdir[0] ? "/" : "",
3685 path) == -1) {
3686 error = got_error_from_errno("asprintf");
3687 goto done;
3689 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3690 free(p);
3691 } else {
3692 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3694 if (error)
3695 goto done;
3697 if (commit_id_str == NULL) {
3698 struct got_reference *head_ref;
3699 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3700 if (error != NULL)
3701 goto done;
3702 error = got_ref_resolve(&commit_id, repo, head_ref);
3703 got_ref_close(head_ref);
3704 } else {
3705 error = got_repo_match_object_id_prefix(&commit_id,
3706 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3708 if (error != NULL)
3709 goto done;
3711 error = got_ref_list(&refs, repo);
3712 if (error)
3713 goto done;
3715 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3716 if (view == NULL) {
3717 error = got_error_from_errno("view_open");
3718 goto done;
3720 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3721 if (error)
3722 goto done;
3723 error = view_loop(view);
3724 done:
3725 free(repo_path);
3726 free(cwd);
3727 free(commit_id);
3728 if (worktree)
3729 got_worktree_close(worktree);
3730 if (repo)
3731 got_repo_close(repo);
3732 got_ref_list_free(&refs);
3733 return error;
3736 static const struct got_error *
3737 draw_tree_entries(struct tog_view *view,
3738 struct got_tree_entry **first_displayed_entry,
3739 struct got_tree_entry **last_displayed_entry,
3740 struct got_tree_entry **selected_entry, int *ndisplayed,
3741 const char *label, int show_ids, const char *parent_path,
3742 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3744 const struct got_error *err = NULL;
3745 struct got_tree_entry *te;
3746 wchar_t *wline;
3747 int width, n;
3749 *ndisplayed = 0;
3751 werase(view->window);
3753 if (limit == 0)
3754 return NULL;
3756 err = format_line(&wline, &width, label, view->ncols);
3757 if (err)
3758 return err;
3759 if (view_needs_focus_indication(view))
3760 wstandout(view->window);
3761 waddwstr(view->window, wline);
3762 if (view_needs_focus_indication(view))
3763 wstandend(view->window);
3764 free(wline);
3765 wline = NULL;
3766 if (width < view->ncols - 1)
3767 waddch(view->window, '\n');
3768 if (--limit <= 0)
3769 return NULL;
3770 err = format_line(&wline, &width, parent_path, view->ncols);
3771 if (err)
3772 return err;
3773 waddwstr(view->window, wline);
3774 free(wline);
3775 wline = NULL;
3776 if (width < view->ncols - 1)
3777 waddch(view->window, '\n');
3778 if (--limit <= 0)
3779 return NULL;
3780 waddch(view->window, '\n');
3781 if (--limit <= 0)
3782 return NULL;
3784 te = SIMPLEQ_FIRST(&entries->head);
3785 if (*first_displayed_entry == NULL) {
3786 if (selected == 0) {
3787 if (view->focussed)
3788 wstandout(view->window);
3789 *selected_entry = NULL;
3791 waddstr(view->window, " ..\n"); /* parent directory */
3792 if (selected == 0 && view->focussed)
3793 wstandend(view->window);
3794 (*ndisplayed)++;
3795 if (--limit <= 0)
3796 return NULL;
3797 n = 1;
3798 } else {
3799 n = 0;
3800 while (te != *first_displayed_entry)
3801 te = SIMPLEQ_NEXT(te, entry);
3804 while (te) {
3805 char *line = NULL, *id_str = NULL;
3807 if (show_ids) {
3808 err = got_object_id_str(&id_str, te->id);
3809 if (err)
3810 return got_error_from_errno(
3811 "got_object_id_str");
3813 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3814 te->name, S_ISDIR(te->mode) ? "/" :
3815 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3816 free(id_str);
3817 return got_error_from_errno("asprintf");
3819 free(id_str);
3820 err = format_line(&wline, &width, line, view->ncols);
3821 if (err) {
3822 free(line);
3823 break;
3825 if (n == selected) {
3826 if (view->focussed)
3827 wstandout(view->window);
3828 *selected_entry = te;
3830 waddwstr(view->window, wline);
3831 if (width < view->ncols - 1)
3832 waddch(view->window, '\n');
3833 if (n == selected && view->focussed)
3834 wstandend(view->window);
3835 free(line);
3836 free(wline);
3837 wline = NULL;
3838 n++;
3839 (*ndisplayed)++;
3840 *last_displayed_entry = te;
3841 if (--limit <= 0)
3842 break;
3843 te = SIMPLEQ_NEXT(te, entry);
3846 return err;
3849 static void
3850 tree_scroll_up(struct tog_view *view,
3851 struct got_tree_entry **first_displayed_entry, int maxscroll,
3852 const struct got_tree_entries *entries, int isroot)
3854 struct got_tree_entry *te, *prev;
3855 int i;
3857 if (*first_displayed_entry == NULL)
3858 return;
3860 te = SIMPLEQ_FIRST(&entries->head);
3861 if (*first_displayed_entry == te) {
3862 if (!isroot)
3863 *first_displayed_entry = NULL;
3864 return;
3867 /* XXX this is stupid... switch to TAILQ? */
3868 for (i = 0; i < maxscroll; i++) {
3869 while (te != *first_displayed_entry) {
3870 prev = te;
3871 te = SIMPLEQ_NEXT(te, entry);
3873 *first_displayed_entry = prev;
3874 te = SIMPLEQ_FIRST(&entries->head);
3876 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3877 *first_displayed_entry = NULL;
3880 static int
3881 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3882 struct got_tree_entry *last_displayed_entry,
3883 const struct got_tree_entries *entries)
3885 struct got_tree_entry *next, *last;
3886 int n = 0;
3888 if (*first_displayed_entry)
3889 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3890 else
3891 next = SIMPLEQ_FIRST(&entries->head);
3892 last = last_displayed_entry;
3893 while (next && last && n++ < maxscroll) {
3894 last = SIMPLEQ_NEXT(last, entry);
3895 if (last) {
3896 *first_displayed_entry = next;
3897 next = SIMPLEQ_NEXT(next, entry);
3900 return n;
3903 static const struct got_error *
3904 tree_entry_path(char **path, struct tog_parent_trees *parents,
3905 struct got_tree_entry *te)
3907 const struct got_error *err = NULL;
3908 struct tog_parent_tree *pt;
3909 size_t len = 2; /* for leading slash and NUL */
3911 TAILQ_FOREACH(pt, parents, entry)
3912 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3913 if (te)
3914 len += strlen(te->name);
3916 *path = calloc(1, len);
3917 if (path == NULL)
3918 return got_error_from_errno("calloc");
3920 (*path)[0] = '/';
3921 pt = TAILQ_LAST(parents, tog_parent_trees);
3922 while (pt) {
3923 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3924 err = got_error(GOT_ERR_NO_SPACE);
3925 goto done;
3927 if (strlcat(*path, "/", len) >= len) {
3928 err = got_error(GOT_ERR_NO_SPACE);
3929 goto done;
3931 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3933 if (te) {
3934 if (strlcat(*path, te->name, len) >= len) {
3935 err = got_error(GOT_ERR_NO_SPACE);
3936 goto done;
3939 done:
3940 if (err) {
3941 free(*path);
3942 *path = NULL;
3944 return err;
3947 static const struct got_error *
3948 blame_tree_entry(struct tog_view **new_view, int begin_x,
3949 struct got_tree_entry *te, struct tog_parent_trees *parents,
3950 struct got_object_id *commit_id, struct got_reflist_head *refs,
3951 struct got_repository *repo)
3953 const struct got_error *err = NULL;
3954 char *path;
3955 struct tog_view *blame_view;
3957 err = tree_entry_path(&path, parents, te);
3958 if (err)
3959 return err;
3961 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3962 if (blame_view == NULL)
3963 return got_error_from_errno("view_open");
3965 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3966 if (err) {
3967 view_close(blame_view);
3968 free(path);
3969 } else
3970 *new_view = blame_view;
3971 return err;
3974 static const struct got_error *
3975 log_tree_entry(struct tog_view **new_view, int begin_x,
3976 struct got_tree_entry *te, struct tog_parent_trees *parents,
3977 struct got_object_id *commit_id, struct got_reflist_head *refs,
3978 struct got_repository *repo)
3980 struct tog_view *log_view;
3981 const struct got_error *err = NULL;
3982 char *path;
3984 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3985 if (log_view == NULL)
3986 return got_error_from_errno("view_open");
3988 err = tree_entry_path(&path, parents, te);
3989 if (err)
3990 return err;
3992 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3993 if (err)
3994 view_close(log_view);
3995 else
3996 *new_view = log_view;
3997 free(path);
3998 return err;
4001 static const struct got_error *
4002 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4003 struct got_object_id *commit_id, struct got_reflist_head *refs,
4004 struct got_repository *repo)
4006 const struct got_error *err = NULL;
4007 char *commit_id_str = NULL;
4008 struct tog_tree_view_state *s = &view->state.tree;
4010 TAILQ_INIT(&s->parents);
4012 err = got_object_id_str(&commit_id_str, commit_id);
4013 if (err != NULL)
4014 goto done;
4016 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4017 err = got_error_from_errno("asprintf");
4018 goto done;
4021 s->root = s->tree = root;
4022 s->entries = got_object_tree_get_entries(root);
4023 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4024 s->commit_id = got_object_id_dup(commit_id);
4025 if (s->commit_id == NULL) {
4026 err = got_error_from_errno("got_object_id_dup");
4027 goto done;
4029 s->refs = refs;
4030 s->repo = repo;
4032 view->show = show_tree_view;
4033 view->input = input_tree_view;
4034 view->close = close_tree_view;
4035 view->search_start = search_start_tree_view;
4036 view->search_next = search_next_tree_view;
4037 done:
4038 free(commit_id_str);
4039 if (err) {
4040 free(s->tree_label);
4041 s->tree_label = NULL;
4043 return err;
4046 static const struct got_error *
4047 close_tree_view(struct tog_view *view)
4049 struct tog_tree_view_state *s = &view->state.tree;
4051 free(s->tree_label);
4052 s->tree_label = NULL;
4053 free(s->commit_id);
4054 s->commit_id = NULL;
4055 while (!TAILQ_EMPTY(&s->parents)) {
4056 struct tog_parent_tree *parent;
4057 parent = TAILQ_FIRST(&s->parents);
4058 TAILQ_REMOVE(&s->parents, parent, entry);
4059 free(parent);
4062 if (s->tree != s->root)
4063 got_object_tree_close(s->tree);
4064 got_object_tree_close(s->root);
4066 return NULL;
4069 static const struct got_error *
4070 search_start_tree_view(struct tog_view *view)
4072 struct tog_tree_view_state *s = &view->state.tree;
4074 s->matched_entry = NULL;
4075 return NULL;
4078 static int
4079 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4081 regmatch_t regmatch;
4083 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4086 static const struct got_error *
4087 search_next_tree_view(struct tog_view *view)
4089 struct tog_tree_view_state *s = &view->state.tree;
4090 struct got_tree_entry *entry, *te;
4092 if (!view->searching) {
4093 view->search_next_done = 1;
4094 return NULL;
4097 if (s->matched_entry) {
4098 if (view->searching == TOG_SEARCH_FORWARD) {
4099 if (s->selected_entry)
4100 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4101 else
4102 entry = SIMPLEQ_FIRST(&s->entries->head);
4104 else {
4105 if (s->selected_entry == NULL) {
4106 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4107 entry = te;
4108 } else {
4109 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4110 entry = te;
4111 if (SIMPLEQ_NEXT(te, entry) ==
4112 s->selected_entry)
4113 break;
4117 } else {
4118 if (view->searching == TOG_SEARCH_FORWARD)
4119 entry = SIMPLEQ_FIRST(&s->entries->head);
4120 else {
4121 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4122 entry = te;
4126 while (1) {
4127 if (entry == NULL) {
4128 if (s->matched_entry == NULL) {
4129 view->search_next_done = 1;
4130 return NULL;
4132 if (view->searching == TOG_SEARCH_FORWARD)
4133 entry = SIMPLEQ_FIRST(&s->entries->head);
4134 else {
4135 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4136 entry = te;
4140 if (match_tree_entry(entry, &view->regex)) {
4141 view->search_next_done = 1;
4142 s->matched_entry = entry;
4143 break;
4146 if (view->searching == TOG_SEARCH_FORWARD)
4147 entry = SIMPLEQ_NEXT(entry, entry);
4148 else {
4149 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4150 entry = NULL;
4151 else {
4152 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4153 if (SIMPLEQ_NEXT(te, entry) == entry) {
4154 entry = te;
4155 break;
4162 if (s->matched_entry) {
4163 s->first_displayed_entry = s->matched_entry;
4164 s->selected = 0;
4167 return NULL;
4170 static const struct got_error *
4171 show_tree_view(struct tog_view *view)
4173 const struct got_error *err = NULL;
4174 struct tog_tree_view_state *s = &view->state.tree;
4175 char *parent_path;
4177 err = tree_entry_path(&parent_path, &s->parents, NULL);
4178 if (err)
4179 return err;
4181 err = draw_tree_entries(view, &s->first_displayed_entry,
4182 &s->last_displayed_entry, &s->selected_entry,
4183 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4184 s->entries, s->selected, view->nlines, s->tree == s->root);
4185 free(parent_path);
4187 view_vborder(view);
4188 return err;
4191 static const struct got_error *
4192 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4193 struct tog_view **focus_view, struct tog_view *view, int ch)
4195 const struct got_error *err = NULL;
4196 struct tog_tree_view_state *s = &view->state.tree;
4197 struct tog_view *log_view;
4198 int begin_x = 0, nscrolled;
4200 switch (ch) {
4201 case 'i':
4202 s->show_ids = !s->show_ids;
4203 break;
4204 case 'l':
4205 if (!s->selected_entry)
4206 break;
4207 if (view_is_parent_view(view))
4208 begin_x = view_split_begin_x(view->begin_x);
4209 err = log_tree_entry(&log_view, begin_x,
4210 s->selected_entry, &s->parents,
4211 s->commit_id, s->refs, s->repo);
4212 if (view_is_parent_view(view)) {
4213 err = view_close_child(view);
4214 if (err)
4215 return err;
4216 err = view_set_child(view, log_view);
4217 if (err) {
4218 view_close(log_view);
4219 break;
4221 *focus_view = log_view;
4222 view->child_focussed = 1;
4223 } else
4224 *new_view = log_view;
4225 break;
4226 case 'k':
4227 case KEY_UP:
4228 if (s->selected > 0) {
4229 s->selected--;
4230 if (s->selected == 0)
4231 break;
4233 if (s->selected > 0)
4234 break;
4235 tree_scroll_up(view, &s->first_displayed_entry, 1,
4236 s->entries, s->tree == s->root);
4237 break;
4238 case KEY_PPAGE:
4239 tree_scroll_up(view, &s->first_displayed_entry,
4240 MAX(0, view->nlines - 4 - s->selected), s->entries,
4241 s->tree == s->root);
4242 s->selected = 0;
4243 if (SIMPLEQ_FIRST(&s->entries->head) ==
4244 s->first_displayed_entry && s->tree != s->root)
4245 s->first_displayed_entry = NULL;
4246 break;
4247 case 'j':
4248 case KEY_DOWN:
4249 if (s->selected < s->ndisplayed - 1) {
4250 s->selected++;
4251 break;
4253 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4254 /* can't scroll any further */
4255 break;
4256 tree_scroll_down(&s->first_displayed_entry, 1,
4257 s->last_displayed_entry, s->entries);
4258 break;
4259 case KEY_NPAGE:
4260 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4261 == NULL) {
4262 /* can't scroll any further; move cursor down */
4263 if (s->selected < s->ndisplayed - 1)
4264 s->selected = s->ndisplayed - 1;
4265 break;
4267 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4268 view->nlines, s->last_displayed_entry, s->entries);
4269 if (nscrolled < view->nlines) {
4270 int ndisplayed = 0;
4271 struct got_tree_entry *te;
4272 te = s->first_displayed_entry;
4273 do {
4274 ndisplayed++;
4275 te = SIMPLEQ_NEXT(te, entry);
4276 } while (te);
4277 s->selected = ndisplayed - 1;
4279 break;
4280 case KEY_ENTER:
4281 case '\r':
4282 case KEY_BACKSPACE:
4283 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4284 struct tog_parent_tree *parent;
4285 /* user selected '..' */
4286 if (s->tree == s->root)
4287 break;
4288 parent = TAILQ_FIRST(&s->parents);
4289 TAILQ_REMOVE(&s->parents, parent,
4290 entry);
4291 got_object_tree_close(s->tree);
4292 s->tree = parent->tree;
4293 s->entries =
4294 got_object_tree_get_entries(s->tree);
4295 s->first_displayed_entry =
4296 parent->first_displayed_entry;
4297 s->selected_entry =
4298 parent->selected_entry;
4299 s->selected = parent->selected;
4300 free(parent);
4301 } else if (S_ISDIR(s->selected_entry->mode)) {
4302 struct got_tree_object *subtree;
4303 err = got_object_open_as_tree(&subtree,
4304 s->repo, s->selected_entry->id);
4305 if (err)
4306 break;
4307 err = tree_view_visit_subtree(subtree, s);
4308 if (err) {
4309 got_object_tree_close(subtree);
4310 break;
4312 } else if (S_ISREG(s->selected_entry->mode)) {
4313 struct tog_view *blame_view;
4314 int begin_x = view_is_parent_view(view) ?
4315 view_split_begin_x(view->begin_x) : 0;
4317 err = blame_tree_entry(&blame_view, begin_x,
4318 s->selected_entry, &s->parents,
4319 s->commit_id, s->refs, s->repo);
4320 if (err)
4321 break;
4322 if (view_is_parent_view(view)) {
4323 err = view_close_child(view);
4324 if (err)
4325 return err;
4326 err = view_set_child(view, blame_view);
4327 if (err) {
4328 view_close(blame_view);
4329 break;
4331 *focus_view = blame_view;
4332 view->child_focussed = 1;
4333 } else
4334 *new_view = blame_view;
4336 break;
4337 case KEY_RESIZE:
4338 if (s->selected > view->nlines)
4339 s->selected = s->ndisplayed - 1;
4340 break;
4341 default:
4342 break;
4345 return err;
4348 __dead static void
4349 usage_tree(void)
4351 endwin();
4352 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4353 getprogname());
4354 exit(1);
4357 static const struct got_error *
4358 cmd_tree(int argc, char *argv[])
4360 const struct got_error *error;
4361 struct got_repository *repo = NULL;
4362 struct got_reflist_head refs;
4363 char *repo_path = NULL;
4364 struct got_object_id *commit_id = NULL;
4365 char *commit_id_arg = NULL;
4366 struct got_commit_object *commit = NULL;
4367 struct got_tree_object *tree = NULL;
4368 int ch;
4369 struct tog_view *view;
4371 SIMPLEQ_INIT(&refs);
4373 #ifndef PROFILE
4374 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4375 NULL) == -1)
4376 err(1, "pledge");
4377 #endif
4379 while ((ch = getopt(argc, argv, "c:")) != -1) {
4380 switch (ch) {
4381 case 'c':
4382 commit_id_arg = optarg;
4383 break;
4384 default:
4385 usage_tree();
4386 /* NOTREACHED */
4390 argc -= optind;
4391 argv += optind;
4393 if (argc == 0) {
4394 struct got_worktree *worktree;
4395 char *cwd = getcwd(NULL, 0);
4396 if (cwd == NULL)
4397 return got_error_from_errno("getcwd");
4398 error = got_worktree_open(&worktree, cwd);
4399 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4400 goto done;
4401 if (worktree) {
4402 free(cwd);
4403 repo_path =
4404 strdup(got_worktree_get_repo_path(worktree));
4405 got_worktree_close(worktree);
4406 } else
4407 repo_path = cwd;
4408 if (repo_path == NULL) {
4409 error = got_error_from_errno("strdup");
4410 goto done;
4412 } else if (argc == 1) {
4413 repo_path = realpath(argv[0], NULL);
4414 if (repo_path == NULL)
4415 return got_error_from_errno2("realpath", argv[0]);
4416 } else
4417 usage_log();
4419 init_curses();
4421 error = got_repo_open(&repo, repo_path);
4422 if (error != NULL)
4423 goto done;
4425 error = apply_unveil(got_repo_get_path(repo), NULL);
4426 if (error)
4427 goto done;
4429 if (commit_id_arg == NULL)
4430 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4431 else
4432 error = got_repo_match_object_id_prefix(&commit_id,
4433 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4434 if (error != NULL)
4435 goto done;
4437 error = got_object_open_as_commit(&commit, repo, commit_id);
4438 if (error != NULL)
4439 goto done;
4441 error = got_object_open_as_tree(&tree, repo,
4442 got_object_commit_get_tree_id(commit));
4443 if (error != NULL)
4444 goto done;
4446 error = got_ref_list(&refs, repo);
4447 if (error)
4448 goto done;
4450 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4451 if (view == NULL) {
4452 error = got_error_from_errno("view_open");
4453 goto done;
4455 error = open_tree_view(view, tree, commit_id, &refs, repo);
4456 if (error)
4457 goto done;
4458 error = view_loop(view);
4459 done:
4460 free(repo_path);
4461 free(commit_id);
4462 if (commit)
4463 got_object_commit_close(commit);
4464 if (tree)
4465 got_object_tree_close(tree);
4466 if (repo)
4467 got_repo_close(repo);
4468 got_ref_list_free(&refs);
4469 return error;
4472 static void
4473 list_commands(void)
4475 int i;
4477 fprintf(stderr, "commands:");
4478 for (i = 0; i < nitems(tog_commands); i++) {
4479 struct tog_cmd *cmd = &tog_commands[i];
4480 fprintf(stderr, " %s", cmd->name);
4482 fputc('\n', stderr);
4485 __dead static void
4486 usage(int hflag)
4488 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4489 if (hflag)
4490 list_commands();
4491 exit(1);
4494 static char **
4495 make_argv(const char *arg0, const char *arg1)
4497 char **argv;
4498 int argc = (arg1 == NULL ? 1 : 2);
4500 argv = calloc(argc, sizeof(char *));
4501 if (argv == NULL)
4502 err(1, "calloc");
4503 argv[0] = strdup(arg0);
4504 if (argv[0] == NULL)
4505 err(1, "calloc");
4506 if (arg1) {
4507 argv[1] = strdup(arg1);
4508 if (argv[1] == NULL)
4509 err(1, "calloc");
4512 return argv;
4515 int
4516 main(int argc, char *argv[])
4518 const struct got_error *error = NULL;
4519 struct tog_cmd *cmd = NULL;
4520 int ch, hflag = 0;
4521 char **cmd_argv = NULL;
4523 setlocale(LC_CTYPE, "");
4525 while ((ch = getopt(argc, argv, "h")) != -1) {
4526 switch (ch) {
4527 case 'h':
4528 hflag = 1;
4529 break;
4530 default:
4531 usage(hflag);
4532 /* NOTREACHED */
4536 argc -= optind;
4537 argv += optind;
4538 optind = 0;
4539 optreset = 1;
4541 if (argc == 0) {
4542 if (hflag)
4543 usage(hflag);
4544 /* Build an argument vector which runs a default command. */
4545 cmd = &tog_commands[0];
4546 cmd_argv = make_argv(cmd->name, NULL);
4547 argc = 1;
4548 } else {
4549 int i;
4551 /* Did the user specific a command? */
4552 for (i = 0; i < nitems(tog_commands); i++) {
4553 if (strncmp(tog_commands[i].name, argv[0],
4554 strlen(argv[0])) == 0) {
4555 cmd = &tog_commands[i];
4556 break;
4560 if (cmd == NULL) {
4561 fprintf(stderr, "%s: unknown command '%s'\n",
4562 getprogname(), argv[0]);
4563 list_commands();
4564 return 1;
4568 if (hflag)
4569 cmd->cmd_usage();
4570 else
4571 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4573 endwin();
4574 free(cmd_argv);
4575 if (error)
4576 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4577 return 0;