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(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "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 };
161 struct tog_blame_cb_args {
162 struct tog_blame_line *lines; /* one per line */
163 int nlines;
165 struct tog_view *view;
166 struct got_object_id *commit_id;
167 int *quit;
168 };
170 struct tog_blame_thread_args {
171 const char *path;
172 struct got_repository *repo;
173 struct tog_blame_cb_args *cb_args;
174 int *complete;
175 };
177 struct tog_blame {
178 FILE *f;
179 size_t filesize;
180 struct tog_blame_line *lines;
181 int nlines;
182 off_t *line_offsets;
183 pthread_t thread;
184 struct tog_blame_thread_args thread_args;
185 struct tog_blame_cb_args cb_args;
186 const char *path;
187 };
189 struct tog_blame_view_state {
190 int first_displayed_line;
191 int last_displayed_line;
192 int selected_line;
193 int blame_complete;
194 int eof;
195 int done;
196 struct got_object_id_queue blamed_commits;
197 struct got_object_qid *blamed_commit;
198 char *path;
199 struct got_repository *repo;
200 struct got_reflist_head *refs;
201 struct got_object_id *commit_id;
202 struct tog_blame blame;
203 int matched_line;
204 };
206 struct tog_parent_tree {
207 TAILQ_ENTRY(tog_parent_tree) entry;
208 struct got_tree_object *tree;
209 struct got_tree_entry *first_displayed_entry;
210 struct got_tree_entry *selected_entry;
211 int selected;
212 };
214 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
216 struct tog_tree_view_state {
217 char *tree_label;
218 struct got_tree_object *root;
219 struct got_tree_object *tree;
220 const struct got_tree_entries *entries;
221 struct got_tree_entry *first_displayed_entry;
222 struct got_tree_entry *last_displayed_entry;
223 struct got_tree_entry *selected_entry;
224 int ndisplayed, selected, show_ids;
225 struct tog_parent_trees parents;
226 struct got_object_id *commit_id;
227 struct got_repository *repo;
228 struct got_reflist_head *refs;
229 struct got_tree_entry *matched_entry;
230 };
232 /*
233 * We implement two types of views: parent views and child views.
235 * The 'Tab' key switches between a parent view and its child view.
236 * Child views are shown side-by-side to their parent view, provided
237 * there is enough screen estate.
239 * When a new view is opened from within a parent view, this new view
240 * becomes a child view of the parent view, replacing any existing child.
242 * When a new view is opened from within a child view, this new view
243 * becomes a parent view which will obscure the views below until the
244 * user quits the new parent view by typing 'q'.
246 * This list of views contains parent views only.
247 * Child views are only pointed to by their parent view.
248 */
249 TAILQ_HEAD(tog_view_list_head, tog_view);
251 struct tog_view {
252 TAILQ_ENTRY(tog_view) entry;
253 WINDOW *window;
254 PANEL *panel;
255 int nlines, ncols, begin_y, begin_x;
256 int lines, cols; /* copies of LINES and COLS */
257 int focussed;
258 struct tog_view *parent;
259 struct tog_view *child;
260 int child_focussed;
262 /* type-specific state */
263 enum tog_view_type type;
264 union {
265 struct tog_diff_view_state diff;
266 struct tog_log_view_state log;
267 struct tog_blame_view_state blame;
268 struct tog_tree_view_state tree;
269 } state;
271 const struct got_error *(*show)(struct tog_view *);
272 const struct got_error *(*input)(struct tog_view **,
273 struct tog_view **, struct tog_view**, struct tog_view *, int);
274 const struct got_error *(*close)(struct tog_view *);
276 const struct got_error *(*search_start)(struct tog_view *);
277 const struct got_error *(*search_next)(struct tog_view *);
278 int searching;
279 #define TOG_SEARCH_FORWARD 1
280 #define TOG_SEARCH_BACKWARD 2
281 int search_next_done;
282 regex_t regex;
283 };
285 static const struct got_error *open_diff_view(struct tog_view *,
286 struct got_object_id *, struct got_object_id *, struct tog_view *,
287 struct got_reflist_head *, struct got_repository *);
288 static const struct got_error *show_diff_view(struct tog_view *);
289 static const struct got_error *input_diff_view(struct tog_view **,
290 struct tog_view **, struct tog_view **, struct tog_view *, int);
291 static const struct got_error* close_diff_view(struct tog_view *);
293 static const struct got_error *open_log_view(struct tog_view *,
294 struct got_object_id *, struct got_reflist_head *,
295 struct got_repository *, const char *, const char *, int);
296 static const struct got_error * show_log_view(struct tog_view *);
297 static const struct got_error *input_log_view(struct tog_view **,
298 struct tog_view **, struct tog_view **, struct tog_view *, int);
299 static const struct got_error *close_log_view(struct tog_view *);
300 static const struct got_error *search_start_log_view(struct tog_view *);
301 static const struct got_error *search_next_log_view(struct tog_view *);
303 static const struct got_error *open_blame_view(struct tog_view *, char *,
304 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
305 static const struct got_error *show_blame_view(struct tog_view *);
306 static const struct got_error *input_blame_view(struct tog_view **,
307 struct tog_view **, struct tog_view **, struct tog_view *, int);
308 static const struct got_error *close_blame_view(struct tog_view *);
309 static const struct got_error *search_start_blame_view(struct tog_view *);
310 static const struct got_error *search_next_blame_view(struct tog_view *);
312 static const struct got_error *open_tree_view(struct tog_view *,
313 struct got_tree_object *, struct got_object_id *,
314 struct got_reflist_head *, struct got_repository *);
315 static const struct got_error *show_tree_view(struct tog_view *);
316 static const struct got_error *input_tree_view(struct tog_view **,
317 struct tog_view **, struct tog_view **, struct tog_view *, int);
318 static const struct got_error *close_tree_view(struct tog_view *);
319 static const struct got_error *search_start_tree_view(struct tog_view *);
320 static const struct got_error *search_next_tree_view(struct tog_view *);
322 static volatile sig_atomic_t tog_sigwinch_received;
324 static void
325 tog_sigwinch(int signo)
327 tog_sigwinch_received = 1;
330 static const struct got_error *
331 view_close(struct tog_view *view)
333 const struct got_error *err = NULL;
335 if (view->child) {
336 view_close(view->child);
337 view->child = NULL;
339 if (view->close)
340 err = view->close(view);
341 if (view->panel)
342 del_panel(view->panel);
343 if (view->window)
344 delwin(view->window);
345 free(view);
346 return err;
349 static struct tog_view *
350 view_open(int nlines, int ncols, int begin_y, int begin_x,
351 enum tog_view_type type)
353 struct tog_view *view = calloc(1, sizeof(*view));
355 if (view == NULL)
356 return NULL;
358 view->type = type;
359 view->lines = LINES;
360 view->cols = COLS;
361 view->nlines = nlines ? nlines : LINES - begin_y;
362 view->ncols = ncols ? ncols : COLS - begin_x;
363 view->begin_y = begin_y;
364 view->begin_x = begin_x;
365 view->window = newwin(nlines, ncols, begin_y, begin_x);
366 if (view->window == NULL) {
367 view_close(view);
368 return NULL;
370 view->panel = new_panel(view->window);
371 if (view->panel == NULL ||
372 set_panel_userptr(view->panel, view) != OK) {
373 view_close(view);
374 return NULL;
377 keypad(view->window, TRUE);
378 return view;
381 static int
382 view_split_begin_x(int begin_x)
384 if (begin_x > 0 || COLS < 120)
385 return 0;
386 return (COLS - MAX(COLS / 2, 80));
389 static const struct got_error *view_resize(struct tog_view *);
391 static const struct got_error *
392 view_splitscreen(struct tog_view *view)
394 const struct got_error *err = NULL;
396 view->begin_y = 0;
397 view->begin_x = view_split_begin_x(0);
398 view->nlines = LINES;
399 view->ncols = COLS - view->begin_x;
400 view->lines = LINES;
401 view->cols = COLS;
402 err = view_resize(view);
403 if (err)
404 return err;
406 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
407 return got_error_from_errno("mvwin");
409 return NULL;
412 static const struct got_error *
413 view_fullscreen(struct tog_view *view)
415 const struct got_error *err = NULL;
417 view->begin_x = 0;
418 view->begin_y = 0;
419 view->nlines = LINES;
420 view->ncols = COLS;
421 view->lines = LINES;
422 view->cols = COLS;
423 err = view_resize(view);
424 if (err)
425 return err;
427 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
428 return got_error_from_errno("mvwin");
430 return NULL;
433 static int
434 view_is_parent_view(struct tog_view *view)
436 return view->parent == NULL;
439 static const struct got_error *
440 view_resize(struct tog_view *view)
442 int nlines, ncols;
444 if (view->lines > LINES)
445 nlines = view->nlines - (view->lines - LINES);
446 else
447 nlines = view->nlines + (LINES - view->lines);
449 if (view->cols > COLS)
450 ncols = view->ncols - (view->cols - COLS);
451 else
452 ncols = view->ncols + (COLS - view->cols);
454 if (wresize(view->window, nlines, ncols) == ERR)
455 return got_error_from_errno("wresize");
456 if (replace_panel(view->panel, view->window) == ERR)
457 return got_error_from_errno("replace_panel");
458 wclear(view->window);
460 view->nlines = nlines;
461 view->ncols = ncols;
462 view->lines = LINES;
463 view->cols = COLS;
465 if (view->child) {
466 view->child->begin_x = view_split_begin_x(view->begin_x);
467 if (view->child->begin_x == 0) {
468 view_fullscreen(view->child);
469 if (view->child->focussed)
470 show_panel(view->child->panel);
471 else
472 show_panel(view->panel);
473 } else {
474 view_splitscreen(view->child);
475 show_panel(view->child->panel);
479 return NULL;
482 static const struct got_error *
483 view_close_child(struct tog_view *view)
485 const struct got_error *err = NULL;
487 if (view->child == NULL)
488 return NULL;
490 err = view_close(view->child);
491 view->child = NULL;
492 return err;
495 static const struct got_error *
496 view_set_child(struct tog_view *view, struct tog_view *child)
498 const struct got_error *err = NULL;
500 view->child = child;
501 child->parent = view;
502 return err;
505 static int
506 view_is_splitscreen(struct tog_view *view)
508 return view->begin_x > 0;
511 static void
512 tog_resizeterm(void)
514 int cols, lines;
515 struct winsize size;
517 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
518 cols = 80; /* Default */
519 lines = 24;
520 } else {
521 cols = size.ws_col;
522 lines = size.ws_row;
524 resize_term(lines, cols);
527 static const struct got_error *
528 view_search_start(struct tog_view *view)
530 const struct got_error *err = NULL;
531 char pattern[1024];
532 int ret;
534 if (view->nlines < 1)
535 return NULL;
537 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
538 view->begin_x, "/");
539 wclrtoeol(view->window);
541 nocbreak();
542 echo();
543 ret = wgetnstr(view->window, pattern, sizeof(pattern));
544 cbreak();
545 noecho();
546 if (ret == ERR)
547 return NULL;
549 if (view->searching) {
550 regfree(&view->regex);
551 view->searching = 0;
554 if (regcomp(&view->regex, pattern,
555 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
556 err = view->search_start(view);
557 if (err) {
558 regfree(&view->regex);
559 return err;
561 view->searching = TOG_SEARCH_FORWARD;
562 view->search_next_done = 0;
563 view->search_next(view);
566 return NULL;
569 static const struct got_error *
570 view_input(struct tog_view **new, struct tog_view **dead,
571 struct tog_view **focus, int *done, struct tog_view *view,
572 struct tog_view_list_head *views)
574 const struct got_error *err = NULL;
575 struct tog_view *v;
576 int ch, errcode;
578 *new = NULL;
579 *dead = NULL;
580 *focus = NULL;
582 if (view->searching && !view->search_next_done) {
583 errcode = pthread_mutex_unlock(&tog_mutex);
584 if (errcode)
585 return got_error_set_errno(errcode,
586 "pthread_mutex_unlock");
587 pthread_yield();
588 errcode = pthread_mutex_lock(&tog_mutex);
589 if (errcode)
590 return got_error_set_errno(errcode,
591 "pthread_mutex_lock");
592 view->search_next(view);
593 return NULL;
596 nodelay(stdscr, FALSE);
597 /* Allow threads to make progress while we are waiting for input. */
598 errcode = pthread_mutex_unlock(&tog_mutex);
599 if (errcode)
600 return got_error_set_errno(errcode, "pthread_mutex_unlock");
601 ch = wgetch(view->window);
602 errcode = pthread_mutex_lock(&tog_mutex);
603 if (errcode)
604 return got_error_set_errno(errcode, "pthread_mutex_lock");
605 nodelay(stdscr, TRUE);
607 if (tog_sigwinch_received) {
608 tog_resizeterm();
609 tog_sigwinch_received = 0;
610 TAILQ_FOREACH(v, views, entry) {
611 err = view_resize(v);
612 if (err)
613 return err;
614 err = v->input(new, dead, focus, v, KEY_RESIZE);
615 if (err)
616 return err;
620 switch (ch) {
621 case ERR:
622 break;
623 case '\t':
624 if (view->child) {
625 *focus = view->child;
626 view->child_focussed = 1;
627 } else if (view->parent) {
628 *focus = view->parent;
629 view->parent->child_focussed = 0;
631 break;
632 case 'q':
633 err = view->input(new, dead, focus, view, ch);
634 *dead = view;
635 break;
636 case 'Q':
637 *done = 1;
638 break;
639 case 'f':
640 if (view_is_parent_view(view)) {
641 if (view->child == NULL)
642 break;
643 if (view_is_splitscreen(view->child)) {
644 *focus = view->child;
645 view->child_focussed = 1;
646 err = view_fullscreen(view->child);
647 } else
648 err = view_splitscreen(view->child);
649 if (err)
650 break;
651 err = view->child->input(new, dead, focus,
652 view->child, KEY_RESIZE);
653 } else {
654 if (view_is_splitscreen(view)) {
655 *focus = view;
656 view->parent->child_focussed = 1;
657 err = view_fullscreen(view);
658 } else {
659 err = view_splitscreen(view);
661 if (err)
662 break;
663 err = view->input(new, dead, focus, view,
664 KEY_RESIZE);
666 break;
667 case KEY_RESIZE:
668 break;
669 case '/':
670 if (view->search_start)
671 view_search_start(view);
672 else
673 err = view->input(new, dead, focus, view, ch);
674 break;
675 case 'N':
676 case 'n':
677 if (view->search_next && view->searching) {
678 view->searching = (ch == 'n' ?
679 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
680 view->search_next_done = 0;
681 view->search_next(view);
682 } else
683 err = view->input(new, dead, focus, view, ch);
684 break;
685 default:
686 err = view->input(new, dead, focus, view, ch);
687 break;
690 return err;
693 void
694 view_vborder(struct tog_view *view)
696 PANEL *panel;
697 struct tog_view *view_above;
699 if (view->parent)
700 return view_vborder(view->parent);
702 panel = panel_above(view->panel);
703 if (panel == NULL)
704 return;
706 view_above = panel_userptr(panel);
707 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
708 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
711 int
712 view_needs_focus_indication(struct tog_view *view)
714 if (view_is_parent_view(view)) {
715 if (view->child == NULL || view->child_focussed)
716 return 0;
717 if (!view_is_splitscreen(view->child))
718 return 0;
719 } else if (!view_is_splitscreen(view))
720 return 0;
722 return view->focussed;
725 static const struct got_error *
726 view_loop(struct tog_view *view)
728 const struct got_error *err = NULL;
729 struct tog_view_list_head views;
730 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
731 int fast_refresh = 10;
732 int done = 0, errcode;
734 errcode = pthread_mutex_lock(&tog_mutex);
735 if (errcode)
736 return got_error_set_errno(errcode, "pthread_mutex_lock");
738 TAILQ_INIT(&views);
739 TAILQ_INSERT_HEAD(&views, view, entry);
741 main_view = view;
742 view->focussed = 1;
743 err = view->show(view);
744 if (err)
745 return err;
746 update_panels();
747 doupdate();
748 while (!TAILQ_EMPTY(&views) && !done) {
749 /* Refresh fast during initialization, then become slower. */
750 if (fast_refresh && fast_refresh-- == 0)
751 halfdelay(10); /* switch to once per second */
753 err = view_input(&new_view, &dead_view, &focus_view, &done,
754 view, &views);
755 if (err)
756 break;
757 if (dead_view) {
758 struct tog_view *prev = NULL;
760 if (view_is_parent_view(dead_view))
761 prev = TAILQ_PREV(dead_view,
762 tog_view_list_head, entry);
763 else if (view->parent != dead_view)
764 prev = view->parent;
766 if (dead_view->parent)
767 dead_view->parent->child = NULL;
768 else
769 TAILQ_REMOVE(&views, dead_view, entry);
771 err = view_close(dead_view);
772 if (err || (dead_view == main_view && new_view == NULL))
773 goto done;
775 if (view == dead_view) {
776 if (focus_view)
777 view = focus_view;
778 else if (prev)
779 view = prev;
780 else if (!TAILQ_EMPTY(&views))
781 view = TAILQ_LAST(&views,
782 tog_view_list_head);
783 else
784 view = NULL;
785 if (view) {
786 if (view->child && view->child_focussed)
787 focus_view = view->child;
788 else
789 focus_view = view;
793 if (new_view) {
794 struct tog_view *v, *t;
795 /* Only allow one parent view per type. */
796 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
797 if (v->type != new_view->type)
798 continue;
799 TAILQ_REMOVE(&views, v, entry);
800 err = view_close(v);
801 if (err)
802 goto done;
803 break;
805 TAILQ_INSERT_TAIL(&views, new_view, entry);
806 view = new_view;
807 if (focus_view == NULL)
808 focus_view = new_view;
810 if (focus_view) {
811 show_panel(focus_view->panel);
812 if (view)
813 view->focussed = 0;
814 focus_view->focussed = 1;
815 view = focus_view;
816 if (new_view)
817 show_panel(new_view->panel);
818 if (view->child && view_is_splitscreen(view->child))
819 show_panel(view->child->panel);
821 if (view) {
822 if (focus_view == NULL) {
823 view->focussed = 1;
824 show_panel(view->panel);
825 if (view->child && view_is_splitscreen(view->child))
826 show_panel(view->child->panel);
827 focus_view = view;
829 if (view->parent) {
830 err = view->parent->show(view->parent);
831 if (err)
832 goto done;
834 err = view->show(view);
835 if (err)
836 goto done;
837 if (view->child) {
838 err = view->child->show(view->child);
839 if (err)
840 goto done;
842 update_panels();
843 doupdate();
846 done:
847 while (!TAILQ_EMPTY(&views)) {
848 view = TAILQ_FIRST(&views);
849 TAILQ_REMOVE(&views, view, entry);
850 view_close(view);
853 errcode = pthread_mutex_unlock(&tog_mutex);
854 if (errcode)
855 return got_error_set_errno(errcode, "pthread_mutex_unlock");
857 return err;
860 __dead static void
861 usage_log(void)
863 endwin();
864 fprintf(stderr,
865 "usage: %s log [-c commit] [-r repository-path] [path]\n",
866 getprogname());
867 exit(1);
870 /* Create newly allocated wide-character string equivalent to a byte string. */
871 static const struct got_error *
872 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
874 char *vis = NULL;
875 const struct got_error *err = NULL;
877 *ws = NULL;
878 *wlen = mbstowcs(NULL, s, 0);
879 if (*wlen == (size_t)-1) {
880 int vislen;
881 if (errno != EILSEQ)
882 return got_error_from_errno("mbstowcs");
884 /* byte string invalid in current encoding; try to "fix" it */
885 err = got_mbsavis(&vis, &vislen, s);
886 if (err)
887 return err;
888 *wlen = mbstowcs(NULL, vis, 0);
889 if (*wlen == (size_t)-1) {
890 err = got_error_from_errno("mbstowcs"); /* give up */
891 goto done;
895 *ws = calloc(*wlen + 1, sizeof(*ws));
896 if (*ws == NULL) {
897 err = got_error_from_errno("calloc");
898 goto done;
901 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
902 err = got_error_from_errno("mbstowcs");
903 done:
904 free(vis);
905 if (err) {
906 free(*ws);
907 *ws = NULL;
908 *wlen = 0;
910 return err;
913 /* Format a line for display, ensuring that it won't overflow a width limit. */
914 static const struct got_error *
915 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
917 const struct got_error *err = NULL;
918 int cols = 0;
919 wchar_t *wline = NULL;
920 size_t wlen;
921 int i;
923 *wlinep = NULL;
924 *widthp = 0;
926 err = mbs2ws(&wline, &wlen, line);
927 if (err)
928 return err;
930 i = 0;
931 while (i < wlen && cols < wlimit) {
932 int width = wcwidth(wline[i]);
933 switch (width) {
934 case 0:
935 i++;
936 break;
937 case 1:
938 case 2:
939 if (cols + width <= wlimit)
940 cols += width;
941 i++;
942 break;
943 case -1:
944 if (wline[i] == L'\t')
945 cols += TABSIZE - ((cols + 1) % TABSIZE);
946 i++;
947 break;
948 default:
949 err = got_error_from_errno("wcwidth");
950 goto done;
953 wline[i] = L'\0';
954 if (widthp)
955 *widthp = cols;
956 done:
957 if (err)
958 free(wline);
959 else
960 *wlinep = wline;
961 return err;
964 static const struct got_error*
965 build_refs_str(char **refs_str, struct got_reflist_head *refs,
966 struct got_object_id *id)
968 static const struct got_error *err = NULL;
969 struct got_reflist_entry *re;
970 char *s;
971 const char *name;
973 *refs_str = NULL;
975 SIMPLEQ_FOREACH(re, refs, entry) {
976 if (got_object_id_cmp(re->id, id) != 0)
977 continue;
978 name = got_ref_get_name(re->ref);
979 if (strcmp(name, GOT_REF_HEAD) == 0)
980 continue;
981 if (strncmp(name, "refs/", 5) == 0)
982 name += 5;
983 if (strncmp(name, "got/", 4) == 0)
984 continue;
985 if (strncmp(name, "heads/", 6) == 0)
986 name += 6;
987 if (strncmp(name, "remotes/", 8) == 0)
988 name += 8;
989 s = *refs_str;
990 if (asprintf(refs_str, "%s%s%s", s ? s : "",
991 s ? ", " : "", name) == -1) {
992 err = got_error_from_errno("asprintf");
993 free(s);
994 *refs_str = NULL;
995 break;
997 free(s);
1000 return err;
1003 static const struct got_error *
1004 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1006 char *smallerthan, *at;
1008 smallerthan = strchr(author, '<');
1009 if (smallerthan && smallerthan[1] != '\0')
1010 author = smallerthan + 1;
1011 at = strchr(author, '@');
1012 if (at)
1013 *at = '\0';
1014 return format_line(wauthor, author_width, author, limit);
1017 static const struct got_error *
1018 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1019 struct got_object_id *id, struct got_reflist_head *refs,
1020 int author_display_cols)
1022 const struct got_error *err = NULL;
1023 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1024 char *logmsg0 = NULL, *logmsg = NULL;
1025 char *author = NULL;
1026 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1027 int author_width, logmsg_width;
1028 char *newline, *line = NULL;
1029 int col, limit;
1030 static const size_t date_display_cols = 9;
1031 const int avail = view->ncols;
1032 struct tm tm;
1033 time_t committer_time;
1035 committer_time = got_object_commit_get_committer_time(commit);
1036 if (localtime_r(&committer_time, &tm) == NULL)
1037 return got_error_from_errno("localtime_r");
1038 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1039 >= sizeof(datebuf))
1040 return got_error(GOT_ERR_NO_SPACE);
1042 if (avail < date_display_cols)
1043 limit = MIN(sizeof(datebuf) - 1, avail);
1044 else
1045 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1046 waddnstr(view->window, datebuf, limit);
1047 col = limit + 1;
1048 if (col > avail)
1049 goto done;
1051 author = strdup(got_object_commit_get_author(commit));
1052 if (author == NULL) {
1053 err = got_error_from_errno("strdup");
1054 goto done;
1056 err = format_author(&wauthor, &author_width, author, avail - col);
1057 if (err)
1058 goto done;
1059 waddwstr(view->window, wauthor);
1060 col += author_width;
1061 while (col <= avail && author_width < author_display_cols + 2) {
1062 waddch(view->window, ' ');
1063 col++;
1064 author_width++;
1066 if (col > avail)
1067 goto done;
1069 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1070 if (logmsg0 == NULL) {
1071 err = got_error_from_errno("strdup");
1072 goto done;
1074 logmsg = logmsg0;
1075 while (*logmsg == '\n')
1076 logmsg++;
1077 newline = strchr(logmsg, '\n');
1078 if (newline)
1079 *newline = '\0';
1080 limit = avail - col;
1081 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1082 if (err)
1083 goto done;
1084 waddwstr(view->window, wlogmsg);
1085 col += logmsg_width;
1086 while (col <= avail) {
1087 waddch(view->window, ' ');
1088 col++;
1090 done:
1091 free(logmsg0);
1092 free(wlogmsg);
1093 free(author);
1094 free(wauthor);
1095 free(line);
1096 return err;
1099 static struct commit_queue_entry *
1100 alloc_commit_queue_entry(struct got_commit_object *commit,
1101 struct got_object_id *id)
1103 struct commit_queue_entry *entry;
1105 entry = calloc(1, sizeof(*entry));
1106 if (entry == NULL)
1107 return NULL;
1109 entry->id = id;
1110 entry->commit = commit;
1111 return entry;
1114 static void
1115 pop_commit(struct commit_queue *commits)
1117 struct commit_queue_entry *entry;
1119 entry = TAILQ_FIRST(&commits->head);
1120 TAILQ_REMOVE(&commits->head, entry, entry);
1121 got_object_commit_close(entry->commit);
1122 commits->ncommits--;
1123 /* Don't free entry->id! It is owned by the commit graph. */
1124 free(entry);
1127 static void
1128 free_commits(struct commit_queue *commits)
1130 while (!TAILQ_EMPTY(&commits->head))
1131 pop_commit(commits);
1134 static const struct got_error *
1135 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1136 int minqueue, struct got_repository *repo, const char *path)
1138 const struct got_error *err = NULL;
1139 int nqueued = 0;
1142 * We keep all commits open throughout the lifetime of the log
1143 * view in order to avoid having to re-fetch commits from disk
1144 * while updating the display.
1146 while (nqueued < minqueue) {
1147 struct got_object_id *id;
1148 struct got_commit_object *commit;
1149 struct commit_queue_entry *entry;
1150 int errcode;
1152 err = got_commit_graph_iter_next(&id, graph);
1153 if (err) {
1154 if (err->code != GOT_ERR_ITER_NEED_MORE)
1155 break;
1156 err = got_commit_graph_fetch_commits(graph,
1157 minqueue, repo);
1158 if (err)
1159 return err;
1160 continue;
1163 if (id == NULL)
1164 break;
1166 err = got_object_open_as_commit(&commit, repo, id);
1167 if (err)
1168 break;
1169 entry = alloc_commit_queue_entry(commit, id);
1170 if (entry == NULL) {
1171 err = got_error_from_errno("alloc_commit_queue_entry");
1172 break;
1175 errcode = pthread_mutex_lock(&tog_mutex);
1176 if (errcode) {
1177 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1178 break;
1181 entry->idx = commits->ncommits;
1182 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1183 nqueued++;
1184 commits->ncommits++;
1186 errcode = pthread_mutex_unlock(&tog_mutex);
1187 if (errcode && err == NULL)
1188 err = got_error_set_errno(errcode,
1189 "pthread_mutex_unlock");
1192 return err;
1195 static const struct got_error *
1196 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1197 struct got_repository *repo)
1199 const struct got_error *err = NULL;
1200 struct got_reference *head_ref;
1202 *head_id = NULL;
1204 err = got_ref_open(&head_ref, repo, branch_name, 0);
1205 if (err)
1206 return err;
1208 err = got_ref_resolve(head_id, repo, head_ref);
1209 got_ref_close(head_ref);
1210 if (err) {
1211 *head_id = NULL;
1212 return err;
1215 return NULL;
1218 static const struct got_error *
1219 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1220 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1221 struct commit_queue *commits, int selected_idx, int limit,
1222 struct got_reflist_head *refs, const char *path, int commits_needed)
1224 const struct got_error *err = NULL;
1225 struct commit_queue_entry *entry;
1226 int width;
1227 int ncommits, author_cols = 10;
1228 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1229 char *refs_str = NULL;
1230 wchar_t *wline;
1232 entry = first;
1233 ncommits = 0;
1234 while (entry) {
1235 if (ncommits == selected_idx) {
1236 *selected = entry;
1237 break;
1239 entry = TAILQ_NEXT(entry, entry);
1240 ncommits++;
1243 if (*selected && !(view->searching && view->search_next_done == 0)) {
1244 err = got_object_id_str(&id_str, (*selected)->id);
1245 if (err)
1246 return err;
1247 if (refs) {
1248 err = build_refs_str(&refs_str, refs, (*selected)->id);
1249 if (err)
1250 goto done;
1254 if (commits_needed == 0)
1255 halfdelay(10); /* disable fast refresh */
1257 if (asprintf(&ncommits_str, " [%d/%d] %s",
1258 entry ? entry->idx + 1 : 0, commits->ncommits,
1259 commits_needed > 0 ?
1260 (view->searching && view->search_next_done == 0
1261 ? "searching..." : "loading... ") :
1262 (refs_str ? refs_str : "")) == -1) {
1263 err = got_error_from_errno("asprintf");
1264 goto done;
1267 if (path && strcmp(path, "/") != 0) {
1268 if (asprintf(&header, "commit %s %s%s",
1269 id_str ? id_str : "........................................",
1270 path, ncommits_str) == -1) {
1271 err = got_error_from_errno("asprintf");
1272 header = NULL;
1273 goto done;
1275 } else if (asprintf(&header, "commit %s%s",
1276 id_str ? id_str : "........................................",
1277 ncommits_str) == -1) {
1278 err = got_error_from_errno("asprintf");
1279 header = NULL;
1280 goto done;
1282 err = format_line(&wline, &width, header, view->ncols);
1283 if (err)
1284 goto done;
1286 werase(view->window);
1288 if (view_needs_focus_indication(view))
1289 wstandout(view->window);
1290 waddwstr(view->window, wline);
1291 while (width < view->ncols) {
1292 waddch(view->window, ' ');
1293 width++;
1295 if (view_needs_focus_indication(view))
1296 wstandend(view->window);
1297 free(wline);
1298 if (limit <= 1)
1299 goto done;
1301 /* Grow author column size if necessary. */
1302 entry = first;
1303 ncommits = 0;
1304 while (entry) {
1305 char *author;
1306 wchar_t *wauthor;
1307 int width;
1308 if (ncommits >= limit - 1)
1309 break;
1310 author = strdup(got_object_commit_get_author(entry->commit));
1311 if (author == NULL) {
1312 err = got_error_from_errno("strdup");
1313 goto done;
1315 err = format_author(&wauthor, &width, author, COLS);
1316 if (author_cols < width)
1317 author_cols = width;
1318 free(wauthor);
1319 free(author);
1320 entry = TAILQ_NEXT(entry, entry);
1323 entry = first;
1324 *last = first;
1325 ncommits = 0;
1326 while (entry) {
1327 if (ncommits >= limit - 1)
1328 break;
1329 if (ncommits == selected_idx)
1330 wstandout(view->window);
1331 err = draw_commit(view, entry->commit, entry->id, refs,
1332 author_cols);
1333 if (ncommits == selected_idx)
1334 wstandend(view->window);
1335 if (err)
1336 goto done;
1337 ncommits++;
1338 *last = entry;
1339 entry = TAILQ_NEXT(entry, entry);
1342 view_vborder(view);
1343 done:
1344 free(id_str);
1345 free(refs_str);
1346 free(ncommits_str);
1347 free(header);
1348 return err;
1351 static void
1352 scroll_up(struct tog_view *view,
1353 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1354 struct commit_queue *commits)
1356 struct commit_queue_entry *entry;
1357 int nscrolled = 0;
1359 entry = TAILQ_FIRST(&commits->head);
1360 if (*first_displayed_entry == entry)
1361 return;
1363 entry = *first_displayed_entry;
1364 while (entry && nscrolled < maxscroll) {
1365 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1366 if (entry) {
1367 *first_displayed_entry = entry;
1368 nscrolled++;
1373 static const struct got_error *
1374 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1375 pthread_cond_t *need_commits)
1377 int errcode;
1378 int max_wait = 20;
1380 halfdelay(1); /* fast refresh while loading commits */
1382 while (*commits_needed > 0) {
1383 if (*log_complete)
1384 break;
1386 /* Wake the log thread. */
1387 errcode = pthread_cond_signal(need_commits);
1388 if (errcode)
1389 return got_error_set_errno(errcode,
1390 "pthread_cond_signal");
1391 errcode = pthread_mutex_unlock(&tog_mutex);
1392 if (errcode)
1393 return got_error_set_errno(errcode,
1394 "pthread_mutex_unlock");
1395 pthread_yield();
1396 errcode = pthread_mutex_lock(&tog_mutex);
1397 if (errcode)
1398 return got_error_set_errno(errcode,
1399 "pthread_mutex_lock");
1401 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1403 * Thread is not done yet; lose a key press
1404 * and let the user retry... this way the GUI
1405 * remains interactive while logging deep paths
1406 * with few commits in history.
1408 return NULL;
1412 return NULL;
1415 static const struct got_error *
1416 scroll_down(struct tog_view *view,
1417 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1418 struct commit_queue_entry **last_displayed_entry,
1419 struct commit_queue *commits, int *log_complete, int *commits_needed,
1420 pthread_cond_t *need_commits)
1422 const struct got_error *err = NULL;
1423 struct commit_queue_entry *pentry;
1424 int nscrolled = 0;
1426 if (*last_displayed_entry == NULL)
1427 return NULL;
1429 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1430 if (pentry == NULL && !*log_complete) {
1432 * Ask the log thread for required amount of commits
1433 * plus some amount of pre-fetching.
1435 (*commits_needed) += maxscroll + 20;
1436 err = trigger_log_thread(0, commits_needed, log_complete,
1437 need_commits);
1438 if (err)
1439 return err;
1442 do {
1443 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1444 if (pentry == NULL)
1445 break;
1447 *last_displayed_entry = pentry;
1449 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1450 if (pentry == NULL)
1451 break;
1452 *first_displayed_entry = pentry;
1453 } while (++nscrolled < maxscroll);
1455 return err;
1458 static const struct got_error *
1459 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1460 struct got_commit_object *commit, struct got_object_id *commit_id,
1461 struct tog_view *log_view, struct got_reflist_head *refs,
1462 struct got_repository *repo)
1464 const struct got_error *err;
1465 struct got_object_qid *parent_id;
1466 struct tog_view *diff_view;
1468 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1469 if (diff_view == NULL)
1470 return got_error_from_errno("view_open");
1472 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1473 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1474 commit_id, log_view, refs, repo);
1475 if (err == NULL)
1476 *new_view = diff_view;
1477 return err;
1480 static const struct got_error *
1481 tree_view_visit_subtree(struct got_tree_object *subtree,
1482 struct tog_tree_view_state *s)
1484 struct tog_parent_tree *parent;
1486 parent = calloc(1, sizeof(*parent));
1487 if (parent == NULL)
1488 return got_error_from_errno("calloc");
1490 parent->tree = s->tree;
1491 parent->first_displayed_entry = s->first_displayed_entry;
1492 parent->selected_entry = s->selected_entry;
1493 parent->selected = s->selected;
1494 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1495 s->tree = subtree;
1496 s->entries = got_object_tree_get_entries(s->tree);
1497 s->selected = 0;
1498 s->first_displayed_entry = NULL;
1499 return NULL;
1503 static const struct got_error *
1504 browse_commit_tree(struct tog_view **new_view, int begin_x,
1505 struct commit_queue_entry *entry, const char *path,
1506 struct got_reflist_head *refs, struct got_repository *repo)
1508 const struct got_error *err = NULL;
1509 struct got_tree_object *tree;
1510 struct got_tree_entry *te;
1511 struct tog_tree_view_state *s;
1512 struct tog_view *tree_view;
1513 char *slash, *subpath = NULL;
1514 const char *p;
1516 err = got_object_open_as_tree(&tree, repo,
1517 got_object_commit_get_tree_id(entry->commit));
1518 if (err)
1519 return err;
1521 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1522 if (tree_view == NULL)
1523 return got_error_from_errno("view_open");
1525 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1526 if (err) {
1527 got_object_tree_close(tree);
1528 return err;
1530 s = &tree_view->state.tree;
1532 *new_view = tree_view;
1534 /* Walk the path and open corresponding tree objects. */
1535 p = path;
1536 while (*p) {
1537 struct got_object_id *tree_id;
1539 while (p[0] == '/')
1540 p++;
1542 /* Ensure the correct subtree entry is selected. */
1543 slash = strchr(p, '/');
1544 if (slash == NULL)
1545 slash = strchr(p, '\0');
1546 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1547 if (strncmp(p, te->name, slash - p) == 0) {
1548 s->selected_entry = te;
1549 break;
1551 s->selected++;
1553 if (s->selected_entry == NULL) {
1554 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1555 break;
1557 if (s->tree != s->root)
1558 s->selected++; /* skip '..' */
1560 if (!S_ISDIR(s->selected_entry->mode)) {
1561 /* Jump to this file's entry. */
1562 s->first_displayed_entry = s->selected_entry;
1563 s->selected = 0;
1564 break;
1567 slash = strchr(p, '/');
1568 if (slash)
1569 subpath = strndup(path, slash - path);
1570 else
1571 subpath = strdup(path);
1572 if (subpath == NULL) {
1573 err = got_error_from_errno("strdup");
1574 break;
1577 err = got_object_id_by_path(&tree_id, repo, entry->id,
1578 subpath);
1579 if (err)
1580 break;
1582 err = got_object_open_as_tree(&tree, repo, tree_id);
1583 free(tree_id);
1584 if (err)
1585 break;
1587 err = tree_view_visit_subtree(tree, s);
1588 if (err) {
1589 got_object_tree_close(tree);
1590 break;
1592 if (slash == NULL)
1593 break;
1594 free(subpath);
1595 subpath = NULL;
1596 p = slash;
1599 free(subpath);
1600 return err;
1603 static void *
1604 log_thread(void *arg)
1606 const struct got_error *err = NULL;
1607 int errcode = 0;
1608 struct tog_log_thread_args *a = arg;
1609 int done = 0;
1611 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1612 if (err)
1613 return (void *)err;
1615 while (!done && !err) {
1616 err = queue_commits(a->graph, a->commits, 1, a->repo,
1617 a->in_repo_path);
1618 if (err) {
1619 if (err->code != GOT_ERR_ITER_COMPLETED)
1620 return (void *)err;
1621 err = NULL;
1622 done = 1;
1623 } else if (a->commits_needed > 0)
1624 a->commits_needed--;
1626 errcode = pthread_mutex_lock(&tog_mutex);
1627 if (errcode) {
1628 err = got_error_set_errno(errcode,
1629 "pthread_mutex_lock");
1630 break;
1631 } else if (*a->quit)
1632 done = 1;
1633 else if (*a->first_displayed_entry == NULL) {
1634 *a->first_displayed_entry =
1635 TAILQ_FIRST(&a->commits->head);
1636 *a->selected_entry = *a->first_displayed_entry;
1639 if (done)
1640 a->commits_needed = 0;
1641 else if (a->commits_needed == 0) {
1642 errcode = pthread_cond_wait(&a->need_commits,
1643 &tog_mutex);
1644 if (errcode)
1645 err = got_error_set_errno(errcode,
1646 "pthread_cond_wait");
1649 errcode = pthread_mutex_unlock(&tog_mutex);
1650 if (errcode && err == NULL)
1651 err = got_error_set_errno(errcode,
1652 "pthread_mutex_unlock");
1654 a->log_complete = 1;
1655 return (void *)err;
1658 static const struct got_error *
1659 stop_log_thread(struct tog_log_view_state *s)
1661 const struct got_error *err = NULL;
1662 int errcode;
1664 if (s->thread) {
1665 s->quit = 1;
1666 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1667 if (errcode)
1668 return got_error_set_errno(errcode,
1669 "pthread_cond_signal");
1670 errcode = pthread_mutex_unlock(&tog_mutex);
1671 if (errcode)
1672 return got_error_set_errno(errcode,
1673 "pthread_mutex_unlock");
1674 errcode = pthread_join(s->thread, (void **)&err);
1675 if (errcode)
1676 return got_error_set_errno(errcode, "pthread_join");
1677 errcode = pthread_mutex_lock(&tog_mutex);
1678 if (errcode)
1679 return got_error_set_errno(errcode,
1680 "pthread_mutex_lock");
1681 s->thread = NULL;
1684 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1685 if (errcode && err == NULL)
1686 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1688 if (s->thread_args.repo) {
1689 got_repo_close(s->thread_args.repo);
1690 s->thread_args.repo = NULL;
1693 if (s->thread_args.graph) {
1694 got_commit_graph_close(s->thread_args.graph);
1695 s->thread_args.graph = NULL;
1698 return err;
1701 static const struct got_error *
1702 close_log_view(struct tog_view *view)
1704 const struct got_error *err = NULL;
1705 struct tog_log_view_state *s = &view->state.log;
1707 err = stop_log_thread(s);
1708 free_commits(&s->commits);
1709 free(s->in_repo_path);
1710 s->in_repo_path = NULL;
1711 free(s->start_id);
1712 s->start_id = NULL;
1713 return err;
1716 static const struct got_error *
1717 search_start_log_view(struct tog_view *view)
1719 struct tog_log_view_state *s = &view->state.log;
1721 s->matched_entry = NULL;
1722 return NULL;
1725 static int
1726 match_commit(struct got_commit_object *commit, regex_t *regex)
1728 regmatch_t regmatch;
1730 if (regexec(regex, got_object_commit_get_author(commit), 1,
1731 &regmatch, 0) == 0 ||
1732 regexec(regex, got_object_commit_get_committer(commit), 1,
1733 &regmatch, 0) == 0 ||
1734 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1735 &regmatch, 0) == 0)
1736 return 1;
1738 return 0;
1741 static const struct got_error *
1742 search_next_log_view(struct tog_view *view)
1744 const struct got_error *err = NULL;
1745 struct tog_log_view_state *s = &view->state.log;
1746 struct commit_queue_entry *entry;
1748 if (!view->searching) {
1749 view->search_next_done = 1;
1750 return NULL;
1753 if (s->matched_entry) {
1754 if (view->searching == TOG_SEARCH_FORWARD)
1755 entry = TAILQ_NEXT(s->selected_entry, entry);
1756 else
1757 entry = TAILQ_PREV(s->selected_entry,
1758 commit_queue_head, entry);
1759 } else {
1760 if (view->searching == TOG_SEARCH_FORWARD)
1761 entry = TAILQ_FIRST(&s->commits.head);
1762 else
1763 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1766 while (1) {
1767 if (entry == NULL) {
1768 if (s->thread_args.log_complete ||
1769 view->searching == TOG_SEARCH_BACKWARD) {
1770 view->search_next_done = 1;
1771 return NULL;
1773 s->thread_args.commits_needed = 1;
1774 return trigger_log_thread(0,
1775 &s->thread_args.commits_needed,
1776 &s->thread_args.log_complete,
1777 &s->thread_args.need_commits);
1780 if (match_commit(entry->commit, &view->regex)) {
1781 view->search_next_done = 1;
1782 s->matched_entry = entry;
1783 break;
1785 if (view->searching == TOG_SEARCH_FORWARD)
1786 entry = TAILQ_NEXT(entry, entry);
1787 else
1788 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1791 if (s->matched_entry) {
1792 int cur = s->selected_entry->idx;
1793 while (cur < s->matched_entry->idx) {
1794 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1795 if (err)
1796 return err;
1797 cur++;
1799 while (cur > s->matched_entry->idx) {
1800 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1801 if (err)
1802 return err;
1803 cur--;
1807 return NULL;
1810 static const struct got_error *
1811 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1812 struct got_reflist_head *refs, struct got_repository *repo,
1813 const char *head_ref_name, const char *path, int check_disk)
1815 const struct got_error *err = NULL;
1816 struct tog_log_view_state *s = &view->state.log;
1817 struct got_repository *thread_repo = NULL;
1818 struct got_commit_graph *thread_graph = NULL;
1819 int errcode;
1821 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1822 if (err != NULL)
1823 goto done;
1825 /* The commit queue only contains commits being displayed. */
1826 TAILQ_INIT(&s->commits.head);
1827 s->commits.ncommits = 0;
1829 s->refs = refs;
1830 s->repo = repo;
1831 s->head_ref_name = head_ref_name;
1832 s->start_id = got_object_id_dup(start_id);
1833 if (s->start_id == NULL) {
1834 err = got_error_from_errno("got_object_id_dup");
1835 goto done;
1838 view->show = show_log_view;
1839 view->input = input_log_view;
1840 view->close = close_log_view;
1841 view->search_start = search_start_log_view;
1842 view->search_next = search_next_log_view;
1844 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1845 if (err)
1846 goto done;
1847 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1848 0, thread_repo);
1849 if (err)
1850 goto done;
1852 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1853 if (errcode) {
1854 err = got_error_set_errno(errcode, "pthread_cond_init");
1855 goto done;
1858 s->thread_args.commits_needed = view->nlines;
1859 s->thread_args.graph = thread_graph;
1860 s->thread_args.commits = &s->commits;
1861 s->thread_args.in_repo_path = s->in_repo_path;
1862 s->thread_args.start_id = s->start_id;
1863 s->thread_args.repo = thread_repo;
1864 s->thread_args.log_complete = 0;
1865 s->thread_args.quit = &s->quit;
1866 s->thread_args.view = view;
1867 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1868 s->thread_args.selected_entry = &s->selected_entry;
1869 done:
1870 if (err)
1871 close_log_view(view);
1872 return err;
1875 static const struct got_error *
1876 show_log_view(struct tog_view *view)
1878 struct tog_log_view_state *s = &view->state.log;
1880 if (s->thread == NULL) {
1881 int errcode = pthread_create(&s->thread, NULL, log_thread,
1882 &s->thread_args);
1883 if (errcode)
1884 return got_error_set_errno(errcode, "pthread_create");
1887 return draw_commits(view, &s->last_displayed_entry,
1888 &s->selected_entry, s->first_displayed_entry,
1889 &s->commits, s->selected, view->nlines, s->refs,
1890 s->in_repo_path, s->thread_args.commits_needed);
1893 static const struct got_error *
1894 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1895 struct tog_view **focus_view, struct tog_view *view, int ch)
1897 const struct got_error *err = NULL;
1898 struct tog_log_view_state *s = &view->state.log;
1899 char *parent_path, *in_repo_path = NULL;
1900 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1901 int begin_x = 0;
1902 struct got_object_id *start_id;
1904 switch (ch) {
1905 case 'q':
1906 s->quit = 1;
1907 break;
1908 case 'k':
1909 case KEY_UP:
1910 case '<':
1911 case ',':
1912 if (s->first_displayed_entry == NULL)
1913 break;
1914 if (s->selected > 0)
1915 s->selected--;
1916 else
1917 scroll_up(view, &s->first_displayed_entry, 1,
1918 &s->commits);
1919 break;
1920 case KEY_PPAGE:
1921 case CTRL('b'):
1922 if (s->first_displayed_entry == NULL)
1923 break;
1924 if (TAILQ_FIRST(&s->commits.head) ==
1925 s->first_displayed_entry) {
1926 s->selected = 0;
1927 break;
1929 scroll_up(view, &s->first_displayed_entry,
1930 view->nlines, &s->commits);
1931 break;
1932 case 'j':
1933 case KEY_DOWN:
1934 case '>':
1935 case '.':
1936 if (s->first_displayed_entry == NULL)
1937 break;
1938 if (s->selected < MIN(view->nlines - 2,
1939 s->commits.ncommits - 1)) {
1940 s->selected++;
1941 break;
1943 err = scroll_down(view, &s->first_displayed_entry, 1,
1944 &s->last_displayed_entry, &s->commits,
1945 &s->thread_args.log_complete,
1946 &s->thread_args.commits_needed,
1947 &s->thread_args.need_commits);
1948 break;
1949 case KEY_NPAGE:
1950 case CTRL('f'): {
1951 struct commit_queue_entry *first;
1952 first = s->first_displayed_entry;
1953 if (first == NULL)
1954 break;
1955 err = scroll_down(view, &s->first_displayed_entry,
1956 view->nlines, &s->last_displayed_entry,
1957 &s->commits, &s->thread_args.log_complete,
1958 &s->thread_args.commits_needed,
1959 &s->thread_args.need_commits);
1960 if (first == s->first_displayed_entry &&
1961 s->selected < MIN(view->nlines - 2,
1962 s->commits.ncommits - 1)) {
1963 /* can't scroll further down */
1964 s->selected = MIN(view->nlines - 2,
1965 s->commits.ncommits - 1);
1967 err = NULL;
1968 break;
1970 case KEY_RESIZE:
1971 if (s->selected > view->nlines - 2)
1972 s->selected = view->nlines - 2;
1973 if (s->selected > s->commits.ncommits - 1)
1974 s->selected = s->commits.ncommits - 1;
1975 break;
1976 case KEY_ENTER:
1977 case ' ':
1978 case '\r':
1979 if (s->selected_entry == NULL)
1980 break;
1981 if (view_is_parent_view(view))
1982 begin_x = view_split_begin_x(view->begin_x);
1983 err = open_diff_view_for_commit(&diff_view, begin_x,
1984 s->selected_entry->commit, s->selected_entry->id,
1985 view, s->refs, s->repo);
1986 if (err)
1987 break;
1988 if (view_is_parent_view(view)) {
1989 err = view_close_child(view);
1990 if (err)
1991 return err;
1992 err = view_set_child(view, diff_view);
1993 if (err) {
1994 view_close(diff_view);
1995 break;
1997 *focus_view = diff_view;
1998 view->child_focussed = 1;
1999 } else
2000 *new_view = diff_view;
2001 break;
2002 case 't':
2003 if (s->selected_entry == NULL)
2004 break;
2005 if (view_is_parent_view(view))
2006 begin_x = view_split_begin_x(view->begin_x);
2007 err = browse_commit_tree(&tree_view, begin_x,
2008 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2009 if (err)
2010 break;
2011 if (view_is_parent_view(view)) {
2012 err = view_close_child(view);
2013 if (err)
2014 return err;
2015 err = view_set_child(view, tree_view);
2016 if (err) {
2017 view_close(tree_view);
2018 break;
2020 *focus_view = tree_view;
2021 view->child_focussed = 1;
2022 } else
2023 *new_view = tree_view;
2024 break;
2025 case KEY_BACKSPACE:
2026 if (strcmp(s->in_repo_path, "/") == 0)
2027 break;
2028 parent_path = dirname(s->in_repo_path);
2029 if (parent_path && strcmp(parent_path, ".") != 0) {
2030 err = stop_log_thread(s);
2031 if (err)
2032 return err;
2033 lv = view_open(view->nlines, view->ncols,
2034 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2035 if (lv == NULL)
2036 return got_error_from_errno(
2037 "view_open");
2038 err = open_log_view(lv, s->start_id, s->refs,
2039 s->repo, s->head_ref_name, parent_path, 0);
2040 if (err)
2041 return err;;
2042 if (view_is_parent_view(view))
2043 *new_view = lv;
2044 else {
2045 view_set_child(view->parent, lv);
2046 *focus_view = lv;
2048 return NULL;
2050 break;
2051 case 'r':
2052 case CTRL('l'):
2053 err = stop_log_thread(s);
2054 if (err)
2055 return err;
2056 lv = view_open(view->nlines, view->ncols,
2057 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2058 if (lv == NULL)
2059 return got_error_from_errno("view_open");
2060 err = get_head_commit_id(&start_id, s->head_ref_name ?
2061 s->head_ref_name : GOT_REF_HEAD, s->repo);
2062 if (err)
2063 return err;
2064 in_repo_path = strdup(s->in_repo_path);
2065 if (in_repo_path == NULL) {
2066 free(start_id);
2067 return got_error_from_errno("strdup");
2069 err = open_log_view(lv, start_id, s->refs, s->repo,
2070 s->head_ref_name, in_repo_path, 0);
2071 if (err)
2072 return err;;
2073 *dead_view = view;
2074 *new_view = lv;
2075 break;
2076 default:
2077 break;
2080 return err;
2083 static const struct got_error *
2084 apply_unveil(const char *repo_path, const char *worktree_path)
2086 const struct got_error *error;
2088 if (repo_path && unveil(repo_path, "r") != 0)
2089 return got_error_from_errno2("unveil", repo_path);
2091 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2092 return got_error_from_errno2("unveil", worktree_path);
2094 if (unveil("/tmp", "rwc") != 0)
2095 return got_error_from_errno2("unveil", "/tmp");
2097 error = got_privsep_unveil_exec_helpers();
2098 if (error != NULL)
2099 return error;
2101 if (unveil(NULL, NULL) != 0)
2102 return got_error_from_errno("unveil");
2104 return NULL;
2107 static void
2108 init_curses(void)
2110 initscr();
2111 cbreak();
2112 halfdelay(1); /* Do fast refresh while initial view is loading. */
2113 noecho();
2114 nonl();
2115 intrflush(stdscr, FALSE);
2116 keypad(stdscr, TRUE);
2117 curs_set(0);
2118 signal(SIGWINCH, tog_sigwinch);
2121 static const struct got_error *
2122 cmd_log(int argc, char *argv[])
2124 const struct got_error *error;
2125 struct got_repository *repo = NULL;
2126 struct got_worktree *worktree = NULL;
2127 struct got_reflist_head refs;
2128 struct got_object_id *start_id = NULL;
2129 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2130 char *start_commit = NULL;
2131 int ch;
2132 struct tog_view *view;
2134 SIMPLEQ_INIT(&refs);
2136 #ifndef PROFILE
2137 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2138 NULL) == -1)
2139 err(1, "pledge");
2140 #endif
2142 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2143 switch (ch) {
2144 case 'c':
2145 start_commit = optarg;
2146 break;
2147 case 'r':
2148 repo_path = realpath(optarg, NULL);
2149 if (repo_path == NULL)
2150 err(1, "-r option");
2151 break;
2152 default:
2153 usage_log();
2154 /* NOTREACHED */
2158 argc -= optind;
2159 argv += optind;
2161 cwd = getcwd(NULL, 0);
2162 if (cwd == NULL) {
2163 error = got_error_from_errno("getcwd");
2164 goto done;
2166 error = got_worktree_open(&worktree, cwd);
2167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2168 goto done;
2169 error = NULL;
2171 if (argc == 0) {
2172 path = strdup("");
2173 if (path == NULL) {
2174 error = got_error_from_errno("strdup");
2175 goto done;
2177 } else if (argc == 1) {
2178 if (worktree) {
2179 error = got_worktree_resolve_path(&path, worktree,
2180 argv[0]);
2181 if (error)
2182 goto done;
2183 } else {
2184 path = strdup(argv[0]);
2185 if (path == NULL) {
2186 error = got_error_from_errno("strdup");
2187 goto done;
2190 } else
2191 usage_log();
2193 repo_path = worktree ?
2194 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2195 if (repo_path == NULL) {
2196 error = got_error_from_errno("strdup");
2197 goto done;
2200 init_curses();
2202 error = got_repo_open(&repo, repo_path);
2203 if (error != NULL)
2204 goto done;
2206 error = apply_unveil(got_repo_get_path(repo),
2207 worktree ? got_worktree_get_root_path(worktree) : NULL);
2208 if (error)
2209 goto done;
2211 if (start_commit == NULL)
2212 error = get_head_commit_id(&start_id, worktree ?
2213 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2214 repo);
2215 else
2216 error = got_object_resolve_id_str(&start_id, repo,
2217 start_commit);
2218 if (error != NULL)
2219 goto done;
2221 error = got_ref_list(&refs, repo);
2222 if (error)
2223 goto done;
2225 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2226 if (view == NULL) {
2227 error = got_error_from_errno("view_open");
2228 goto done;
2230 error = open_log_view(view, start_id, &refs, repo, worktree ?
2231 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2232 if (error)
2233 goto done;
2234 error = view_loop(view);
2235 done:
2236 free(repo_path);
2237 free(cwd);
2238 free(path);
2239 free(start_id);
2240 if (repo)
2241 got_repo_close(repo);
2242 if (worktree)
2243 got_worktree_close(worktree);
2244 got_ref_list_free(&refs);
2245 return error;
2248 __dead static void
2249 usage_diff(void)
2251 endwin();
2252 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2253 getprogname());
2254 exit(1);
2257 static char *
2258 parse_next_line(FILE *f, size_t *len)
2260 char *line;
2261 size_t linelen;
2262 size_t lineno;
2263 const char delim[3] = { '\0', '\0', '\0'};
2265 line = fparseln(f, &linelen, &lineno, delim, 0);
2266 if (len)
2267 *len = linelen;
2268 return line;
2271 static const struct got_error *
2272 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2273 int *last_displayed_line, int *eof, int max_lines,
2274 char *header)
2276 const struct got_error *err;
2277 int nlines = 0, nprinted = 0;
2278 char *line;
2279 size_t len;
2280 wchar_t *wline;
2281 int width;
2283 rewind(f);
2284 werase(view->window);
2286 if (header) {
2287 err = format_line(&wline, &width, header, view->ncols);
2288 if (err) {
2289 return err;
2292 if (view_needs_focus_indication(view))
2293 wstandout(view->window);
2294 waddwstr(view->window, wline);
2295 if (view_needs_focus_indication(view))
2296 wstandend(view->window);
2297 if (width < view->ncols - 1)
2298 waddch(view->window, '\n');
2300 if (max_lines <= 1)
2301 return NULL;
2302 max_lines--;
2305 *eof = 0;
2306 while (nprinted < max_lines) {
2307 line = parse_next_line(f, &len);
2308 if (line == NULL) {
2309 *eof = 1;
2310 break;
2312 if (++nlines < *first_displayed_line) {
2313 free(line);
2314 continue;
2317 err = format_line(&wline, &width, line, view->ncols);
2318 if (err) {
2319 free(line);
2320 return err;
2322 waddwstr(view->window, wline);
2323 if (width < view->ncols - 1)
2324 waddch(view->window, '\n');
2325 if (++nprinted == 1)
2326 *first_displayed_line = nlines;
2327 free(line);
2328 free(wline);
2329 wline = NULL;
2331 *last_displayed_line = nlines;
2333 view_vborder(view);
2335 if (*eof) {
2336 while (nprinted < view->nlines) {
2337 waddch(view->window, '\n');
2338 nprinted++;
2341 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2342 if (err) {
2343 return err;
2346 wstandout(view->window);
2347 waddwstr(view->window, wline);
2348 wstandend(view->window);
2351 return NULL;
2354 static char *
2355 get_datestr(time_t *time, char *datebuf)
2357 char *p, *s = ctime_r(time, datebuf);
2358 p = strchr(s, '\n');
2359 if (p)
2360 *p = '\0';
2361 return s;
2364 static const struct got_error *
2365 write_commit_info(struct got_object_id *commit_id,
2366 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2368 const struct got_error *err = NULL;
2369 char datebuf[26];
2370 struct got_commit_object *commit;
2371 char *id_str = NULL;
2372 time_t committer_time;
2373 const char *author, *committer;
2374 char *refs_str = NULL;
2376 if (refs) {
2377 err = build_refs_str(&refs_str, refs, commit_id);
2378 if (err)
2379 return err;
2382 err = got_object_open_as_commit(&commit, repo, commit_id);
2383 if (err)
2384 return err;
2386 err = got_object_id_str(&id_str, commit_id);
2387 if (err) {
2388 err = got_error_from_errno("got_object_id_str");
2389 goto done;
2392 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2393 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2394 err = got_error_from_errno("fprintf");
2395 goto done;
2397 if (fprintf(outfile, "from: %s\n",
2398 got_object_commit_get_author(commit)) < 0) {
2399 err = got_error_from_errno("fprintf");
2400 goto done;
2402 committer_time = got_object_commit_get_committer_time(commit);
2403 if (fprintf(outfile, "date: %s UTC\n",
2404 get_datestr(&committer_time, datebuf)) < 0) {
2405 err = got_error_from_errno("fprintf");
2406 goto done;
2408 author = got_object_commit_get_author(commit);
2409 committer = got_object_commit_get_committer(commit);
2410 if (strcmp(author, committer) != 0 &&
2411 fprintf(outfile, "via: %s\n", committer) < 0) {
2412 err = got_error_from_errno("fprintf");
2413 goto done;
2415 if (fprintf(outfile, "%s\n",
2416 got_object_commit_get_logmsg(commit)) < 0) {
2417 err = got_error_from_errno("fprintf");
2418 goto done;
2420 done:
2421 free(id_str);
2422 free(refs_str);
2423 got_object_commit_close(commit);
2424 return err;
2427 static const struct got_error *
2428 create_diff(struct tog_diff_view_state *s)
2430 const struct got_error *err = NULL;
2431 FILE *f = NULL;
2432 int obj_type;
2434 f = got_opentemp();
2435 if (f == NULL) {
2436 err = got_error_from_errno("got_opentemp");
2437 goto done;
2439 if (s->f && fclose(s->f) != 0) {
2440 err = got_error_from_errno("fclose");
2441 goto done;
2443 s->f = f;
2445 if (s->id1)
2446 err = got_object_get_type(&obj_type, s->repo, s->id1);
2447 else
2448 err = got_object_get_type(&obj_type, s->repo, s->id2);
2449 if (err)
2450 goto done;
2452 switch (obj_type) {
2453 case GOT_OBJ_TYPE_BLOB:
2454 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2455 s->diff_context, s->repo, f);
2456 break;
2457 case GOT_OBJ_TYPE_TREE:
2458 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2459 s->diff_context, s->repo, f);
2460 break;
2461 case GOT_OBJ_TYPE_COMMIT: {
2462 const struct got_object_id_queue *parent_ids;
2463 struct got_object_qid *pid;
2464 struct got_commit_object *commit2;
2466 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2467 if (err)
2468 break;
2469 /* Show commit info if we're diffing to a parent/root commit. */
2470 if (s->id1 == NULL)
2471 write_commit_info(s->id2, s->refs, s->repo, f);
2472 else {
2473 parent_ids = got_object_commit_get_parent_ids(commit2);
2474 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2475 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2476 write_commit_info(s->id2, s->refs,
2477 s->repo, f);
2478 break;
2482 got_object_commit_close(commit2);
2484 err = got_diff_objects_as_commits(s->id1, s->id2,
2485 s->diff_context, s->repo, f);
2486 break;
2488 default:
2489 err = got_error(GOT_ERR_OBJ_TYPE);
2490 break;
2492 done:
2493 if (f && fflush(f) != 0 && err == NULL)
2494 err = got_error_from_errno("fflush");
2495 return err;
2498 static void
2499 diff_view_indicate_progress(struct tog_view *view)
2501 mvwaddstr(view->window, 0, 0, "diffing...");
2502 update_panels();
2503 doupdate();
2506 static const struct got_error *
2507 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2508 struct got_object_id *id2, struct tog_view *log_view,
2509 struct got_reflist_head *refs, struct got_repository *repo)
2511 const struct got_error *err;
2513 if (id1 != NULL && id2 != NULL) {
2514 int type1, type2;
2515 err = got_object_get_type(&type1, repo, id1);
2516 if (err)
2517 return err;
2518 err = got_object_get_type(&type2, repo, id2);
2519 if (err)
2520 return err;
2522 if (type1 != type2)
2523 return got_error(GOT_ERR_OBJ_TYPE);
2526 if (id1) {
2527 view->state.diff.id1 = got_object_id_dup(id1);
2528 if (view->state.diff.id1 == NULL)
2529 return got_error_from_errno("got_object_id_dup");
2530 } else
2531 view->state.diff.id1 = NULL;
2533 view->state.diff.id2 = got_object_id_dup(id2);
2534 if (view->state.diff.id2 == NULL) {
2535 free(view->state.diff.id1);
2536 view->state.diff.id1 = NULL;
2537 return got_error_from_errno("got_object_id_dup");
2539 view->state.diff.f = NULL;
2540 view->state.diff.first_displayed_line = 1;
2541 view->state.diff.last_displayed_line = view->nlines;
2542 view->state.diff.diff_context = 3;
2543 view->state.diff.log_view = log_view;
2544 view->state.diff.repo = repo;
2545 view->state.diff.refs = refs;
2547 if (log_view && view_is_splitscreen(view))
2548 show_log_view(log_view); /* draw vborder */
2549 diff_view_indicate_progress(view);
2551 err = create_diff(&view->state.diff);
2552 if (err) {
2553 free(view->state.diff.id1);
2554 view->state.diff.id1 = NULL;
2555 free(view->state.diff.id2);
2556 view->state.diff.id2 = NULL;
2557 return err;
2560 view->show = show_diff_view;
2561 view->input = input_diff_view;
2562 view->close = close_diff_view;
2564 return NULL;
2567 static const struct got_error *
2568 close_diff_view(struct tog_view *view)
2570 const struct got_error *err = NULL;
2572 free(view->state.diff.id1);
2573 view->state.diff.id1 = NULL;
2574 free(view->state.diff.id2);
2575 view->state.diff.id2 = NULL;
2576 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2577 err = got_error_from_errno("fclose");
2578 return err;
2581 static const struct got_error *
2582 show_diff_view(struct tog_view *view)
2584 const struct got_error *err;
2585 struct tog_diff_view_state *s = &view->state.diff;
2586 char *id_str1 = NULL, *id_str2, *header;
2588 if (s->id1) {
2589 err = got_object_id_str(&id_str1, s->id1);
2590 if (err)
2591 return err;
2593 err = got_object_id_str(&id_str2, s->id2);
2594 if (err)
2595 return err;
2597 if (asprintf(&header, "diff %s %s",
2598 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2599 err = got_error_from_errno("asprintf");
2600 free(id_str1);
2601 free(id_str2);
2602 return err;
2604 free(id_str1);
2605 free(id_str2);
2607 return draw_file(view, s->f, &s->first_displayed_line,
2608 &s->last_displayed_line, &s->eof, view->nlines,
2609 header);
2612 static const struct got_error *
2613 set_selected_commit(struct tog_diff_view_state *s,
2614 struct commit_queue_entry *entry)
2616 const struct got_error *err;
2617 const struct got_object_id_queue *parent_ids;
2618 struct got_commit_object *selected_commit;
2619 struct got_object_qid *pid;
2621 free(s->id2);
2622 s->id2 = got_object_id_dup(entry->id);
2623 if (s->id2 == NULL)
2624 return got_error_from_errno("got_object_id_dup");
2626 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2627 if (err)
2628 return err;
2629 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2630 free(s->id1);
2631 pid = SIMPLEQ_FIRST(parent_ids);
2632 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2633 got_object_commit_close(selected_commit);
2634 return NULL;
2637 static const struct got_error *
2638 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2639 struct tog_view **focus_view, struct tog_view *view, int ch)
2641 const struct got_error *err = NULL;
2642 struct tog_diff_view_state *s = &view->state.diff;
2643 struct tog_log_view_state *ls;
2644 struct commit_queue_entry *entry;
2645 int i;
2647 switch (ch) {
2648 case 'k':
2649 case KEY_UP:
2650 if (s->first_displayed_line > 1)
2651 s->first_displayed_line--;
2652 break;
2653 case KEY_PPAGE:
2654 case CTRL('b'):
2655 if (s->first_displayed_line == 1)
2656 break;
2657 i = 0;
2658 while (i++ < view->nlines - 1 &&
2659 s->first_displayed_line > 1)
2660 s->first_displayed_line--;
2661 break;
2662 case 'j':
2663 case KEY_DOWN:
2664 if (!s->eof)
2665 s->first_displayed_line++;
2666 break;
2667 case KEY_NPAGE:
2668 case CTRL('f'):
2669 case ' ':
2670 if (s->eof)
2671 break;
2672 i = 0;
2673 while (!s->eof && i++ < view->nlines - 1) {
2674 char *line;
2675 line = parse_next_line(s->f, NULL);
2676 s->first_displayed_line++;
2677 if (line == NULL)
2678 break;
2680 break;
2681 case '[':
2682 if (s->diff_context > 0) {
2683 s->diff_context--;
2684 diff_view_indicate_progress(view);
2685 err = create_diff(s);
2687 break;
2688 case ']':
2689 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2690 s->diff_context++;
2691 diff_view_indicate_progress(view);
2692 err = create_diff(s);
2694 break;
2695 case '<':
2696 case ',':
2697 if (s->log_view == NULL)
2698 break;
2699 ls = &s->log_view->state.log;
2700 entry = TAILQ_PREV(ls->selected_entry,
2701 commit_queue_head, entry);
2702 if (entry == NULL)
2703 break;
2705 err = input_log_view(NULL, NULL, NULL, s->log_view,
2706 KEY_UP);
2707 if (err)
2708 break;
2710 err = set_selected_commit(s, entry);
2711 if (err)
2712 break;
2714 s->first_displayed_line = 1;
2715 s->last_displayed_line = view->nlines;
2717 diff_view_indicate_progress(view);
2718 err = create_diff(s);
2719 break;
2720 case '>':
2721 case '.':
2722 if (s->log_view == NULL)
2723 break;
2724 ls = &s->log_view->state.log;
2726 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2727 ls->thread_args.commits_needed++;
2729 /* Display "loading..." in log view. */
2730 show_log_view(s->log_view);
2731 update_panels();
2732 doupdate();
2734 err = trigger_log_thread(1 /* load_all */,
2735 &ls->thread_args.commits_needed,
2736 &ls->thread_args.log_complete,
2737 &ls->thread_args.need_commits);
2738 if (err)
2739 break;
2741 err = input_log_view(NULL, NULL, NULL, s->log_view,
2742 KEY_DOWN);
2743 if (err)
2744 break;
2746 entry = TAILQ_NEXT(ls->selected_entry, entry);
2747 if (entry == NULL)
2748 break;
2750 err = set_selected_commit(s, entry);
2751 if (err)
2752 break;
2754 s->first_displayed_line = 1;
2755 s->last_displayed_line = view->nlines;
2757 diff_view_indicate_progress(view);
2758 err = create_diff(s);
2759 break;
2760 default:
2761 break;
2764 return err;
2767 static const struct got_error *
2768 cmd_diff(int argc, char *argv[])
2770 const struct got_error *error = NULL;
2771 struct got_repository *repo = NULL;
2772 struct got_reflist_head refs;
2773 struct got_object_id *id1 = NULL, *id2 = NULL;
2774 char *repo_path = NULL;
2775 char *id_str1 = NULL, *id_str2 = NULL;
2776 int ch;
2777 struct tog_view *view;
2779 SIMPLEQ_INIT(&refs);
2781 #ifndef PROFILE
2782 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2783 NULL) == -1)
2784 err(1, "pledge");
2785 #endif
2787 while ((ch = getopt(argc, argv, "")) != -1) {
2788 switch (ch) {
2789 default:
2790 usage_diff();
2791 /* NOTREACHED */
2795 argc -= optind;
2796 argv += optind;
2798 if (argc == 0) {
2799 usage_diff(); /* TODO show local worktree changes */
2800 } else if (argc == 2) {
2801 repo_path = getcwd(NULL, 0);
2802 if (repo_path == NULL)
2803 return got_error_from_errno("getcwd");
2804 id_str1 = argv[0];
2805 id_str2 = argv[1];
2806 } else if (argc == 3) {
2807 repo_path = realpath(argv[0], NULL);
2808 if (repo_path == NULL)
2809 return got_error_from_errno2("realpath", argv[0]);
2810 id_str1 = argv[1];
2811 id_str2 = argv[2];
2812 } else
2813 usage_diff();
2815 init_curses();
2817 error = got_repo_open(&repo, repo_path);
2818 if (error)
2819 goto done;
2821 error = apply_unveil(got_repo_get_path(repo), NULL);
2822 if (error)
2823 goto done;
2825 error = got_object_resolve_id_str(&id1, repo, id_str1);
2826 if (error)
2827 goto done;
2829 error = got_object_resolve_id_str(&id2, repo, id_str2);
2830 if (error)
2831 goto done;
2833 error = got_ref_list(&refs, repo);
2834 if (error)
2835 goto done;
2837 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2838 if (view == NULL) {
2839 error = got_error_from_errno("view_open");
2840 goto done;
2842 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2843 if (error)
2844 goto done;
2845 error = view_loop(view);
2846 done:
2847 free(repo_path);
2848 got_repo_close(repo);
2849 got_ref_list_free(&refs);
2850 return error;
2853 __dead static void
2854 usage_blame(void)
2856 endwin();
2857 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2858 getprogname());
2859 exit(1);
2862 struct tog_blame_line {
2863 int annotated;
2864 struct got_object_id *id;
2867 static const struct got_error *
2868 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2869 const char *path, struct tog_blame_line *lines, int nlines,
2870 int blame_complete, int selected_line, int *first_displayed_line,
2871 int *last_displayed_line, int *eof, int max_lines)
2873 const struct got_error *err;
2874 int lineno = 0, nprinted = 0;
2875 char *line;
2876 size_t len;
2877 wchar_t *wline;
2878 int width, wlimit;
2879 struct tog_blame_line *blame_line;
2880 struct got_object_id *prev_id = NULL;
2881 char *id_str;
2883 err = got_object_id_str(&id_str, id);
2884 if (err)
2885 return err;
2887 rewind(f);
2888 werase(view->window);
2890 if (asprintf(&line, "commit %s", id_str) == -1) {
2891 err = got_error_from_errno("asprintf");
2892 free(id_str);
2893 return err;
2896 err = format_line(&wline, &width, line, view->ncols);
2897 free(line);
2898 line = NULL;
2899 if (view_needs_focus_indication(view))
2900 wstandout(view->window);
2901 waddwstr(view->window, wline);
2902 if (view_needs_focus_indication(view))
2903 wstandend(view->window);
2904 free(wline);
2905 wline = NULL;
2906 if (width < view->ncols - 1)
2907 waddch(view->window, '\n');
2909 if (asprintf(&line, "[%d/%d] %s%s",
2910 *first_displayed_line - 1 + selected_line, nlines,
2911 blame_complete ? "" : "annotating... ", path) == -1) {
2912 free(id_str);
2913 return got_error_from_errno("asprintf");
2915 free(id_str);
2916 err = format_line(&wline, &width, line, view->ncols);
2917 free(line);
2918 line = NULL;
2919 if (err)
2920 return err;
2921 waddwstr(view->window, wline);
2922 free(wline);
2923 wline = NULL;
2924 if (width < view->ncols - 1)
2925 waddch(view->window, '\n');
2927 *eof = 0;
2928 while (nprinted < max_lines - 2) {
2929 line = parse_next_line(f, &len);
2930 if (line == NULL) {
2931 *eof = 1;
2932 break;
2934 if (++lineno < *first_displayed_line) {
2935 free(line);
2936 continue;
2939 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2940 err = format_line(&wline, &width, line, wlimit);
2941 if (err) {
2942 free(line);
2943 return err;
2946 if (view->focussed && nprinted == selected_line - 1)
2947 wstandout(view->window);
2949 blame_line = &lines[lineno - 1];
2950 if (blame_line->annotated && prev_id &&
2951 got_object_id_cmp(prev_id, blame_line->id) == 0)
2952 waddstr(view->window, " ");
2953 else if (blame_line->annotated) {
2954 char *id_str;
2955 err = got_object_id_str(&id_str, blame_line->id);
2956 if (err) {
2957 free(line);
2958 free(wline);
2959 return err;
2961 wprintw(view->window, "%.8s ", id_str);
2962 free(id_str);
2963 prev_id = blame_line->id;
2964 } else {
2965 waddstr(view->window, "........ ");
2966 prev_id = NULL;
2969 waddwstr(view->window, wline);
2970 while (width < wlimit) {
2971 waddch(view->window, ' ');
2972 width++;
2974 if (view->focussed && nprinted == selected_line - 1)
2975 wstandend(view->window);
2976 if (++nprinted == 1)
2977 *first_displayed_line = lineno;
2978 free(line);
2979 free(wline);
2980 wline = NULL;
2982 *last_displayed_line = lineno;
2984 view_vborder(view);
2986 return NULL;
2989 static const struct got_error *
2990 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2992 const struct got_error *err = NULL;
2993 struct tog_blame_cb_args *a = arg;
2994 struct tog_blame_line *line;
2995 int errcode;
2997 if (nlines != a->nlines ||
2998 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2999 return got_error(GOT_ERR_RANGE);
3001 errcode = pthread_mutex_lock(&tog_mutex);
3002 if (errcode)
3003 return got_error_set_errno(errcode, "pthread_mutex_lock");
3005 if (*a->quit) { /* user has quit the blame view */
3006 err = got_error(GOT_ERR_ITER_COMPLETED);
3007 goto done;
3010 if (lineno == -1)
3011 goto done; /* no change in this commit */
3013 line = &a->lines[lineno - 1];
3014 if (line->annotated)
3015 goto done;
3017 line->id = got_object_id_dup(id);
3018 if (line->id == NULL) {
3019 err = got_error_from_errno("got_object_id_dup");
3020 goto done;
3022 line->annotated = 1;
3023 done:
3024 errcode = pthread_mutex_unlock(&tog_mutex);
3025 if (errcode)
3026 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3027 return err;
3030 static void *
3031 blame_thread(void *arg)
3033 const struct got_error *err;
3034 struct tog_blame_thread_args *ta = arg;
3035 struct tog_blame_cb_args *a = ta->cb_args;
3036 int errcode;
3038 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3039 blame_cb, ta->cb_args);
3041 errcode = pthread_mutex_lock(&tog_mutex);
3042 if (errcode)
3043 return (void *)got_error_set_errno(errcode,
3044 "pthread_mutex_lock");
3046 got_repo_close(ta->repo);
3047 ta->repo = NULL;
3048 *ta->complete = 1;
3050 errcode = pthread_mutex_unlock(&tog_mutex);
3051 if (errcode && err == NULL)
3052 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3054 return (void *)err;
3057 static struct got_object_id *
3058 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3059 int selected_line)
3061 struct tog_blame_line *line;
3063 line = &lines[first_displayed_line - 1 + selected_line - 1];
3064 if (!line->annotated)
3065 return NULL;
3067 return line->id;
3070 static const struct got_error *
3071 stop_blame(struct tog_blame *blame)
3073 const struct got_error *err = NULL;
3074 int i;
3076 if (blame->thread) {
3077 int errcode;
3078 errcode = pthread_mutex_unlock(&tog_mutex);
3079 if (errcode)
3080 return got_error_set_errno(errcode,
3081 "pthread_mutex_unlock");
3082 errcode = pthread_join(blame->thread, (void **)&err);
3083 if (errcode)
3084 return got_error_set_errno(errcode, "pthread_join");
3085 errcode = pthread_mutex_lock(&tog_mutex);
3086 if (errcode)
3087 return got_error_set_errno(errcode,
3088 "pthread_mutex_lock");
3089 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3090 err = NULL;
3091 blame->thread = NULL;
3093 if (blame->thread_args.repo) {
3094 got_repo_close(blame->thread_args.repo);
3095 blame->thread_args.repo = NULL;
3097 if (blame->f) {
3098 if (fclose(blame->f) != 0 && err == NULL)
3099 err = got_error_from_errno("fclose");
3100 blame->f = NULL;
3102 if (blame->lines) {
3103 for (i = 0; i < blame->nlines; i++)
3104 free(blame->lines[i].id);
3105 free(blame->lines);
3106 blame->lines = NULL;
3108 free(blame->cb_args.commit_id);
3109 blame->cb_args.commit_id = NULL;
3111 return err;
3114 static const struct got_error *
3115 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3116 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3117 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3118 struct got_repository *repo)
3120 const struct got_error *err = NULL;
3121 struct got_blob_object *blob = NULL;
3122 struct got_repository *thread_repo = NULL;
3123 struct got_object_id *obj_id = NULL;
3124 int obj_type;
3126 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3127 if (err)
3128 return err;
3129 if (obj_id == NULL)
3130 return got_error(GOT_ERR_NO_OBJ);
3132 err = got_object_get_type(&obj_type, repo, obj_id);
3133 if (err)
3134 goto done;
3136 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3137 err = got_error(GOT_ERR_OBJ_TYPE);
3138 goto done;
3141 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3142 if (err)
3143 goto done;
3144 blame->f = got_opentemp();
3145 if (blame->f == NULL) {
3146 err = got_error_from_errno("got_opentemp");
3147 goto done;
3149 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3150 &blame->line_offsets, blame->f, blob);
3151 if (err)
3152 goto done;
3154 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3155 if (blame->lines == NULL) {
3156 err = got_error_from_errno("calloc");
3157 goto done;
3160 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3161 if (err)
3162 goto done;
3164 blame->cb_args.view = view;
3165 blame->cb_args.lines = blame->lines;
3166 blame->cb_args.nlines = blame->nlines;
3167 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3168 if (blame->cb_args.commit_id == NULL) {
3169 err = got_error_from_errno("got_object_id_dup");
3170 goto done;
3172 blame->cb_args.quit = done;
3174 blame->thread_args.path = path;
3175 blame->thread_args.repo = thread_repo;
3176 blame->thread_args.cb_args = &blame->cb_args;
3177 blame->thread_args.complete = blame_complete;
3178 *blame_complete = 0;
3180 done:
3181 if (blob)
3182 got_object_blob_close(blob);
3183 free(obj_id);
3184 if (err)
3185 stop_blame(blame);
3186 return err;
3189 static const struct got_error *
3190 open_blame_view(struct tog_view *view, char *path,
3191 struct got_object_id *commit_id, struct got_reflist_head *refs,
3192 struct got_repository *repo)
3194 const struct got_error *err = NULL;
3195 struct tog_blame_view_state *s = &view->state.blame;
3197 SIMPLEQ_INIT(&s->blamed_commits);
3199 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3200 if (err)
3201 return err;
3203 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3204 s->first_displayed_line = 1;
3205 s->last_displayed_line = view->nlines;
3206 s->selected_line = 1;
3207 s->blame_complete = 0;
3208 s->path = path;
3209 if (s->path == NULL)
3210 return got_error_from_errno("open_blame_view");
3211 s->repo = repo;
3212 s->refs = refs;
3213 s->commit_id = commit_id;
3214 memset(&s->blame, 0, sizeof(s->blame));
3216 view->show = show_blame_view;
3217 view->input = input_blame_view;
3218 view->close = close_blame_view;
3219 view->search_start = search_start_blame_view;
3220 view->search_next = search_next_blame_view;
3222 return run_blame(&s->blame, view, &s->blame_complete,
3223 &s->first_displayed_line, &s->last_displayed_line,
3224 &s->selected_line, &s->done, &s->eof, s->path,
3225 s->blamed_commit->id, s->repo);
3228 static const struct got_error *
3229 close_blame_view(struct tog_view *view)
3231 const struct got_error *err = NULL;
3232 struct tog_blame_view_state *s = &view->state.blame;
3234 if (s->blame.thread)
3235 err = stop_blame(&s->blame);
3237 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3238 struct got_object_qid *blamed_commit;
3239 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3240 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3241 got_object_qid_free(blamed_commit);
3244 free(s->path);
3246 return err;
3249 static const struct got_error *
3250 search_start_blame_view(struct tog_view *view)
3252 struct tog_blame_view_state *s = &view->state.blame;
3254 s->matched_line = 0;
3255 return NULL;
3258 static int
3259 match_line(const char *line, regex_t *regex)
3261 regmatch_t regmatch;
3263 return regexec(regex, line, 1, &regmatch, 0) == 0;
3267 static const struct got_error *
3268 search_next_blame_view(struct tog_view *view)
3270 struct tog_blame_view_state *s = &view->state.blame;
3271 int lineno;
3273 if (!view->searching) {
3274 view->search_next_done = 1;
3275 return NULL;
3278 if (s->matched_line) {
3279 if (view->searching == TOG_SEARCH_FORWARD)
3280 lineno = s->matched_line + 1;
3281 else
3282 lineno = s->matched_line - 1;
3283 } else {
3284 if (view->searching == TOG_SEARCH_FORWARD)
3285 lineno = 1;
3286 else
3287 lineno = s->blame.nlines;
3290 while (1) {
3291 char *line = NULL;
3292 off_t offset;
3293 size_t len;
3295 if (lineno <= 0 || lineno > s->blame.nlines) {
3296 if (s->matched_line == 0) {
3297 view->search_next_done = 1;
3298 free(line);
3299 break;
3302 if (view->searching == TOG_SEARCH_FORWARD)
3303 lineno = 1;
3304 else
3305 lineno = s->blame.nlines;
3308 offset = s->blame.line_offsets[lineno - 1];
3309 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3310 free(line);
3311 return got_error_from_errno("fseeko");
3313 free(line);
3314 line = parse_next_line(s->blame.f, &len);
3315 if (line && match_line(line, &view->regex)) {
3316 view->search_next_done = 1;
3317 s->matched_line = lineno;
3318 free(line);
3319 break;
3321 free(line);
3322 if (view->searching == TOG_SEARCH_FORWARD)
3323 lineno++;
3324 else
3325 lineno--;
3328 if (s->matched_line) {
3329 s->first_displayed_line = s->matched_line;
3330 s->selected_line = 1;
3333 return NULL;
3336 static const struct got_error *
3337 show_blame_view(struct tog_view *view)
3339 const struct got_error *err = NULL;
3340 struct tog_blame_view_state *s = &view->state.blame;
3341 int errcode;
3343 if (s->blame.thread == NULL) {
3344 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3345 &s->blame.thread_args);
3346 if (errcode)
3347 return got_error_set_errno(errcode, "pthread_create");
3350 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3351 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3352 s->selected_line, &s->first_displayed_line,
3353 &s->last_displayed_line, &s->eof, view->nlines);
3355 view_vborder(view);
3356 return err;
3359 static const struct got_error *
3360 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3361 struct tog_view **focus_view, struct tog_view *view, int ch)
3363 const struct got_error *err = NULL, *thread_err = NULL;
3364 struct tog_view *diff_view;
3365 struct tog_blame_view_state *s = &view->state.blame;
3366 int begin_x = 0;
3368 switch (ch) {
3369 case 'q':
3370 s->done = 1;
3371 break;
3372 case 'k':
3373 case KEY_UP:
3374 if (s->selected_line > 1)
3375 s->selected_line--;
3376 else if (s->selected_line == 1 &&
3377 s->first_displayed_line > 1)
3378 s->first_displayed_line--;
3379 break;
3380 case KEY_PPAGE:
3381 if (s->first_displayed_line == 1) {
3382 s->selected_line = 1;
3383 break;
3385 if (s->first_displayed_line > view->nlines - 2)
3386 s->first_displayed_line -=
3387 (view->nlines - 2);
3388 else
3389 s->first_displayed_line = 1;
3390 break;
3391 case 'j':
3392 case KEY_DOWN:
3393 if (s->selected_line < view->nlines - 2 &&
3394 s->first_displayed_line +
3395 s->selected_line <= s->blame.nlines)
3396 s->selected_line++;
3397 else if (s->last_displayed_line <
3398 s->blame.nlines)
3399 s->first_displayed_line++;
3400 break;
3401 case 'b':
3402 case 'p': {
3403 struct got_object_id *id = NULL;
3404 id = get_selected_commit_id(s->blame.lines,
3405 s->first_displayed_line, s->selected_line);
3406 if (id == NULL)
3407 break;
3408 if (ch == 'p') {
3409 struct got_commit_object *commit;
3410 struct got_object_qid *pid;
3411 struct got_object_id *blob_id = NULL;
3412 int obj_type;
3413 err = got_object_open_as_commit(&commit,
3414 s->repo, id);
3415 if (err)
3416 break;
3417 pid = SIMPLEQ_FIRST(
3418 got_object_commit_get_parent_ids(commit));
3419 if (pid == NULL) {
3420 got_object_commit_close(commit);
3421 break;
3423 /* Check if path history ends here. */
3424 err = got_object_id_by_path(&blob_id, s->repo,
3425 pid->id, s->path);
3426 if (err) {
3427 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3428 err = NULL;
3429 got_object_commit_close(commit);
3430 break;
3432 err = got_object_get_type(&obj_type, s->repo,
3433 blob_id);
3434 free(blob_id);
3435 /* Can't blame non-blob type objects. */
3436 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3437 got_object_commit_close(commit);
3438 break;
3440 err = got_object_qid_alloc(&s->blamed_commit,
3441 pid->id);
3442 got_object_commit_close(commit);
3443 } else {
3444 if (got_object_id_cmp(id,
3445 s->blamed_commit->id) == 0)
3446 break;
3447 err = got_object_qid_alloc(&s->blamed_commit,
3448 id);
3450 if (err)
3451 break;
3452 s->done = 1;
3453 thread_err = stop_blame(&s->blame);
3454 s->done = 0;
3455 if (thread_err)
3456 break;
3457 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3458 s->blamed_commit, entry);
3459 err = run_blame(&s->blame, view, &s->blame_complete,
3460 &s->first_displayed_line, &s->last_displayed_line,
3461 &s->selected_line, &s->done, &s->eof,
3462 s->path, s->blamed_commit->id, s->repo);
3463 if (err)
3464 break;
3465 break;
3467 case 'B': {
3468 struct got_object_qid *first;
3469 first = SIMPLEQ_FIRST(&s->blamed_commits);
3470 if (!got_object_id_cmp(first->id, s->commit_id))
3471 break;
3472 s->done = 1;
3473 thread_err = stop_blame(&s->blame);
3474 s->done = 0;
3475 if (thread_err)
3476 break;
3477 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3478 got_object_qid_free(s->blamed_commit);
3479 s->blamed_commit =
3480 SIMPLEQ_FIRST(&s->blamed_commits);
3481 err = run_blame(&s->blame, view, &s->blame_complete,
3482 &s->first_displayed_line, &s->last_displayed_line,
3483 &s->selected_line, &s->done, &s->eof, s->path,
3484 s->blamed_commit->id, s->repo);
3485 if (err)
3486 break;
3487 break;
3489 case KEY_ENTER:
3490 case '\r': {
3491 struct got_object_id *id = NULL;
3492 struct got_object_qid *pid;
3493 struct got_commit_object *commit = NULL;
3494 id = get_selected_commit_id(s->blame.lines,
3495 s->first_displayed_line, s->selected_line);
3496 if (id == NULL)
3497 break;
3498 err = got_object_open_as_commit(&commit, s->repo, id);
3499 if (err)
3500 break;
3501 pid = SIMPLEQ_FIRST(
3502 got_object_commit_get_parent_ids(commit));
3503 if (view_is_parent_view(view))
3504 begin_x = view_split_begin_x(view->begin_x);
3505 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3506 if (diff_view == NULL) {
3507 got_object_commit_close(commit);
3508 err = got_error_from_errno("view_open");
3509 break;
3511 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3512 id, NULL, s->refs, s->repo);
3513 got_object_commit_close(commit);
3514 if (err) {
3515 view_close(diff_view);
3516 break;
3518 if (view_is_parent_view(view)) {
3519 err = view_close_child(view);
3520 if (err)
3521 break;
3522 err = view_set_child(view, diff_view);
3523 if (err) {
3524 view_close(diff_view);
3525 break;
3527 *focus_view = diff_view;
3528 view->child_focussed = 1;
3529 } else
3530 *new_view = diff_view;
3531 if (err)
3532 break;
3533 break;
3535 case KEY_NPAGE:
3536 case ' ':
3537 if (s->last_displayed_line >= s->blame.nlines &&
3538 s->selected_line >= MIN(s->blame.nlines,
3539 view->nlines - 2)) {
3540 break;
3542 if (s->last_displayed_line >= s->blame.nlines &&
3543 s->selected_line < view->nlines - 2) {
3544 s->selected_line = MIN(s->blame.nlines,
3545 view->nlines - 2);
3546 break;
3548 if (s->last_displayed_line + view->nlines - 2
3549 <= s->blame.nlines)
3550 s->first_displayed_line +=
3551 view->nlines - 2;
3552 else
3553 s->first_displayed_line =
3554 s->blame.nlines -
3555 (view->nlines - 3);
3556 break;
3557 case KEY_RESIZE:
3558 if (s->selected_line > view->nlines - 2) {
3559 s->selected_line = MIN(s->blame.nlines,
3560 view->nlines - 2);
3562 break;
3563 default:
3564 break;
3566 return thread_err ? thread_err : err;
3569 static const struct got_error *
3570 cmd_blame(int argc, char *argv[])
3572 const struct got_error *error;
3573 struct got_repository *repo = NULL;
3574 struct got_reflist_head refs;
3575 struct got_worktree *worktree = NULL;
3576 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3577 struct got_object_id *commit_id = NULL;
3578 char *commit_id_str = NULL;
3579 int ch;
3580 struct tog_view *view;
3582 SIMPLEQ_INIT(&refs);
3584 #ifndef PROFILE
3585 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3586 NULL) == -1)
3587 err(1, "pledge");
3588 #endif
3590 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3591 switch (ch) {
3592 case 'c':
3593 commit_id_str = optarg;
3594 break;
3595 case 'r':
3596 repo_path = realpath(optarg, NULL);
3597 if (repo_path == NULL)
3598 err(1, "-r option");
3599 break;
3600 default:
3601 usage_blame();
3602 /* NOTREACHED */
3606 argc -= optind;
3607 argv += optind;
3609 if (argc == 1)
3610 path = argv[0];
3611 else
3612 usage_blame();
3614 cwd = getcwd(NULL, 0);
3615 if (cwd == NULL) {
3616 error = got_error_from_errno("getcwd");
3617 goto done;
3619 if (repo_path == NULL) {
3620 error = got_worktree_open(&worktree, cwd);
3621 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3622 goto done;
3623 else
3624 error = NULL;
3625 if (worktree) {
3626 repo_path =
3627 strdup(got_worktree_get_repo_path(worktree));
3628 if (repo_path == NULL)
3629 error = got_error_from_errno("strdup");
3630 if (error)
3631 goto done;
3632 } else {
3633 repo_path = strdup(cwd);
3634 if (repo_path == NULL) {
3635 error = got_error_from_errno("strdup");
3636 goto done;
3641 init_curses();
3643 error = got_repo_open(&repo, repo_path);
3644 if (error != NULL)
3645 goto done;
3647 error = apply_unveil(got_repo_get_path(repo), NULL);
3648 if (error)
3649 goto done;
3651 if (worktree) {
3652 const char *prefix = got_worktree_get_path_prefix(worktree);
3653 char *p, *worktree_subdir = cwd +
3654 strlen(got_worktree_get_root_path(worktree));
3655 if (asprintf(&p, "%s%s%s%s%s",
3656 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3657 worktree_subdir, worktree_subdir[0] ? "/" : "",
3658 path) == -1) {
3659 error = got_error_from_errno("asprintf");
3660 goto done;
3662 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3663 free(p);
3664 } else {
3665 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3667 if (error)
3668 goto done;
3670 if (commit_id_str == NULL) {
3671 struct got_reference *head_ref;
3672 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3673 if (error != NULL)
3674 goto done;
3675 error = got_ref_resolve(&commit_id, repo, head_ref);
3676 got_ref_close(head_ref);
3677 } else {
3678 error = got_object_resolve_id_str(&commit_id, repo,
3679 commit_id_str);
3681 if (error != NULL)
3682 goto done;
3684 error = got_ref_list(&refs, repo);
3685 if (error)
3686 goto done;
3688 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3689 if (view == NULL) {
3690 error = got_error_from_errno("view_open");
3691 goto done;
3693 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3694 if (error)
3695 goto done;
3696 error = view_loop(view);
3697 done:
3698 free(repo_path);
3699 free(cwd);
3700 free(commit_id);
3701 if (worktree)
3702 got_worktree_close(worktree);
3703 if (repo)
3704 got_repo_close(repo);
3705 got_ref_list_free(&refs);
3706 return error;
3709 static const struct got_error *
3710 draw_tree_entries(struct tog_view *view,
3711 struct got_tree_entry **first_displayed_entry,
3712 struct got_tree_entry **last_displayed_entry,
3713 struct got_tree_entry **selected_entry, int *ndisplayed,
3714 const char *label, int show_ids, const char *parent_path,
3715 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3717 const struct got_error *err = NULL;
3718 struct got_tree_entry *te;
3719 wchar_t *wline;
3720 int width, n;
3722 *ndisplayed = 0;
3724 werase(view->window);
3726 if (limit == 0)
3727 return NULL;
3729 err = format_line(&wline, &width, label, view->ncols);
3730 if (err)
3731 return err;
3732 if (view_needs_focus_indication(view))
3733 wstandout(view->window);
3734 waddwstr(view->window, wline);
3735 if (view_needs_focus_indication(view))
3736 wstandend(view->window);
3737 free(wline);
3738 wline = NULL;
3739 if (width < view->ncols - 1)
3740 waddch(view->window, '\n');
3741 if (--limit <= 0)
3742 return NULL;
3743 err = format_line(&wline, &width, parent_path, view->ncols);
3744 if (err)
3745 return err;
3746 waddwstr(view->window, wline);
3747 free(wline);
3748 wline = NULL;
3749 if (width < view->ncols - 1)
3750 waddch(view->window, '\n');
3751 if (--limit <= 0)
3752 return NULL;
3753 waddch(view->window, '\n');
3754 if (--limit <= 0)
3755 return NULL;
3757 te = SIMPLEQ_FIRST(&entries->head);
3758 if (*first_displayed_entry == NULL) {
3759 if (selected == 0) {
3760 if (view->focussed)
3761 wstandout(view->window);
3762 *selected_entry = NULL;
3764 waddstr(view->window, " ..\n"); /* parent directory */
3765 if (selected == 0 && view->focussed)
3766 wstandend(view->window);
3767 (*ndisplayed)++;
3768 if (--limit <= 0)
3769 return NULL;
3770 n = 1;
3771 } else {
3772 n = 0;
3773 while (te != *first_displayed_entry)
3774 te = SIMPLEQ_NEXT(te, entry);
3777 while (te) {
3778 char *line = NULL, *id_str = NULL;
3780 if (show_ids) {
3781 err = got_object_id_str(&id_str, te->id);
3782 if (err)
3783 return got_error_from_errno(
3784 "got_object_id_str");
3786 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3787 te->name, S_ISDIR(te->mode) ? "/" :
3788 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3789 free(id_str);
3790 return got_error_from_errno("asprintf");
3792 free(id_str);
3793 err = format_line(&wline, &width, line, view->ncols);
3794 if (err) {
3795 free(line);
3796 break;
3798 if (n == selected) {
3799 if (view->focussed)
3800 wstandout(view->window);
3801 *selected_entry = te;
3803 waddwstr(view->window, wline);
3804 if (width < view->ncols - 1)
3805 waddch(view->window, '\n');
3806 if (n == selected && view->focussed)
3807 wstandend(view->window);
3808 free(line);
3809 free(wline);
3810 wline = NULL;
3811 n++;
3812 (*ndisplayed)++;
3813 *last_displayed_entry = te;
3814 if (--limit <= 0)
3815 break;
3816 te = SIMPLEQ_NEXT(te, entry);
3819 return err;
3822 static void
3823 tree_scroll_up(struct tog_view *view,
3824 struct got_tree_entry **first_displayed_entry, int maxscroll,
3825 const struct got_tree_entries *entries, int isroot)
3827 struct got_tree_entry *te, *prev;
3828 int i;
3830 if (*first_displayed_entry == NULL)
3831 return;
3833 te = SIMPLEQ_FIRST(&entries->head);
3834 if (*first_displayed_entry == te) {
3835 if (!isroot)
3836 *first_displayed_entry = NULL;
3837 return;
3840 /* XXX this is stupid... switch to TAILQ? */
3841 for (i = 0; i < maxscroll; i++) {
3842 while (te != *first_displayed_entry) {
3843 prev = te;
3844 te = SIMPLEQ_NEXT(te, entry);
3846 *first_displayed_entry = prev;
3847 te = SIMPLEQ_FIRST(&entries->head);
3849 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3850 *first_displayed_entry = NULL;
3853 static int
3854 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3855 struct got_tree_entry *last_displayed_entry,
3856 const struct got_tree_entries *entries)
3858 struct got_tree_entry *next, *last;
3859 int n = 0;
3861 if (*first_displayed_entry)
3862 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3863 else
3864 next = SIMPLEQ_FIRST(&entries->head);
3865 last = last_displayed_entry;
3866 while (next && last && n++ < maxscroll) {
3867 last = SIMPLEQ_NEXT(last, entry);
3868 if (last) {
3869 *first_displayed_entry = next;
3870 next = SIMPLEQ_NEXT(next, entry);
3873 return n;
3876 static const struct got_error *
3877 tree_entry_path(char **path, struct tog_parent_trees *parents,
3878 struct got_tree_entry *te)
3880 const struct got_error *err = NULL;
3881 struct tog_parent_tree *pt;
3882 size_t len = 2; /* for leading slash and NUL */
3884 TAILQ_FOREACH(pt, parents, entry)
3885 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3886 if (te)
3887 len += strlen(te->name);
3889 *path = calloc(1, len);
3890 if (path == NULL)
3891 return got_error_from_errno("calloc");
3893 (*path)[0] = '/';
3894 pt = TAILQ_LAST(parents, tog_parent_trees);
3895 while (pt) {
3896 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3897 err = got_error(GOT_ERR_NO_SPACE);
3898 goto done;
3900 if (strlcat(*path, "/", len) >= len) {
3901 err = got_error(GOT_ERR_NO_SPACE);
3902 goto done;
3904 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3906 if (te) {
3907 if (strlcat(*path, te->name, len) >= len) {
3908 err = got_error(GOT_ERR_NO_SPACE);
3909 goto done;
3912 done:
3913 if (err) {
3914 free(*path);
3915 *path = NULL;
3917 return err;
3920 static const struct got_error *
3921 blame_tree_entry(struct tog_view **new_view, int begin_x,
3922 struct got_tree_entry *te, struct tog_parent_trees *parents,
3923 struct got_object_id *commit_id, struct got_reflist_head *refs,
3924 struct got_repository *repo)
3926 const struct got_error *err = NULL;
3927 char *path;
3928 struct tog_view *blame_view;
3930 err = tree_entry_path(&path, parents, te);
3931 if (err)
3932 return err;
3934 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3935 if (blame_view == NULL)
3936 return got_error_from_errno("view_open");
3938 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3939 if (err) {
3940 view_close(blame_view);
3941 free(path);
3942 } else
3943 *new_view = blame_view;
3944 return err;
3947 static const struct got_error *
3948 log_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 struct tog_view *log_view;
3954 const struct got_error *err = NULL;
3955 char *path;
3957 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3958 if (log_view == NULL)
3959 return got_error_from_errno("view_open");
3961 err = tree_entry_path(&path, parents, te);
3962 if (err)
3963 return err;
3965 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
3966 if (err)
3967 view_close(log_view);
3968 else
3969 *new_view = log_view;
3970 free(path);
3971 return err;
3974 static const struct got_error *
3975 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3976 struct got_object_id *commit_id, struct got_reflist_head *refs,
3977 struct got_repository *repo)
3979 const struct got_error *err = NULL;
3980 char *commit_id_str = NULL;
3981 struct tog_tree_view_state *s = &view->state.tree;
3983 TAILQ_INIT(&s->parents);
3985 err = got_object_id_str(&commit_id_str, commit_id);
3986 if (err != NULL)
3987 goto done;
3989 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3990 err = got_error_from_errno("asprintf");
3991 goto done;
3994 s->root = s->tree = root;
3995 s->entries = got_object_tree_get_entries(root);
3996 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3997 s->commit_id = got_object_id_dup(commit_id);
3998 if (s->commit_id == NULL) {
3999 err = got_error_from_errno("got_object_id_dup");
4000 goto done;
4002 s->refs = refs;
4003 s->repo = repo;
4005 view->show = show_tree_view;
4006 view->input = input_tree_view;
4007 view->close = close_tree_view;
4008 view->search_start = search_start_tree_view;
4009 view->search_next = search_next_tree_view;
4010 done:
4011 free(commit_id_str);
4012 if (err) {
4013 free(s->tree_label);
4014 s->tree_label = NULL;
4016 return err;
4019 static const struct got_error *
4020 close_tree_view(struct tog_view *view)
4022 struct tog_tree_view_state *s = &view->state.tree;
4024 free(s->tree_label);
4025 s->tree_label = NULL;
4026 free(s->commit_id);
4027 s->commit_id = NULL;
4028 while (!TAILQ_EMPTY(&s->parents)) {
4029 struct tog_parent_tree *parent;
4030 parent = TAILQ_FIRST(&s->parents);
4031 TAILQ_REMOVE(&s->parents, parent, entry);
4032 free(parent);
4035 if (s->tree != s->root)
4036 got_object_tree_close(s->tree);
4037 got_object_tree_close(s->root);
4039 return NULL;
4042 static const struct got_error *
4043 search_start_tree_view(struct tog_view *view)
4045 struct tog_tree_view_state *s = &view->state.tree;
4047 s->matched_entry = NULL;
4048 return NULL;
4051 static int
4052 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4054 regmatch_t regmatch;
4056 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4059 static const struct got_error *
4060 search_next_tree_view(struct tog_view *view)
4062 struct tog_tree_view_state *s = &view->state.tree;
4063 struct got_tree_entry *entry, *te;
4065 if (!view->searching) {
4066 view->search_next_done = 1;
4067 return NULL;
4070 if (s->matched_entry) {
4071 if (view->searching == TOG_SEARCH_FORWARD) {
4072 if (s->selected_entry)
4073 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4074 else
4075 entry = SIMPLEQ_FIRST(&s->entries->head);
4077 else {
4078 if (s->selected_entry == NULL) {
4079 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4080 entry = te;
4081 } else {
4082 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4083 entry = te;
4084 if (SIMPLEQ_NEXT(te, entry) ==
4085 s->selected_entry)
4086 break;
4090 } else {
4091 if (view->searching == TOG_SEARCH_FORWARD)
4092 entry = SIMPLEQ_FIRST(&s->entries->head);
4093 else {
4094 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4095 entry = te;
4099 while (1) {
4100 if (entry == NULL) {
4101 if (s->matched_entry == NULL) {
4102 view->search_next_done = 1;
4103 return NULL;
4105 if (view->searching == TOG_SEARCH_FORWARD)
4106 entry = SIMPLEQ_FIRST(&s->entries->head);
4107 else {
4108 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4109 entry = te;
4113 if (match_tree_entry(entry, &view->regex)) {
4114 view->search_next_done = 1;
4115 s->matched_entry = entry;
4116 break;
4119 if (view->searching == TOG_SEARCH_FORWARD)
4120 entry = SIMPLEQ_NEXT(entry, entry);
4121 else {
4122 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4123 entry = NULL;
4124 else {
4125 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4126 if (SIMPLEQ_NEXT(te, entry) == entry) {
4127 entry = te;
4128 break;
4135 if (s->matched_entry) {
4136 s->first_displayed_entry = s->matched_entry;
4137 s->selected = 0;
4140 return NULL;
4143 static const struct got_error *
4144 show_tree_view(struct tog_view *view)
4146 const struct got_error *err = NULL;
4147 struct tog_tree_view_state *s = &view->state.tree;
4148 char *parent_path;
4150 err = tree_entry_path(&parent_path, &s->parents, NULL);
4151 if (err)
4152 return err;
4154 err = draw_tree_entries(view, &s->first_displayed_entry,
4155 &s->last_displayed_entry, &s->selected_entry,
4156 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4157 s->entries, s->selected, view->nlines, s->tree == s->root);
4158 free(parent_path);
4160 view_vborder(view);
4161 return err;
4164 static const struct got_error *
4165 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4166 struct tog_view **focus_view, struct tog_view *view, int ch)
4168 const struct got_error *err = NULL;
4169 struct tog_tree_view_state *s = &view->state.tree;
4170 struct tog_view *log_view;
4171 int begin_x = 0, nscrolled;
4173 switch (ch) {
4174 case 'i':
4175 s->show_ids = !s->show_ids;
4176 break;
4177 case 'l':
4178 if (!s->selected_entry)
4179 break;
4180 if (view_is_parent_view(view))
4181 begin_x = view_split_begin_x(view->begin_x);
4182 err = log_tree_entry(&log_view, begin_x,
4183 s->selected_entry, &s->parents,
4184 s->commit_id, s->refs, s->repo);
4185 if (view_is_parent_view(view)) {
4186 err = view_close_child(view);
4187 if (err)
4188 return err;
4189 err = view_set_child(view, log_view);
4190 if (err) {
4191 view_close(log_view);
4192 break;
4194 *focus_view = log_view;
4195 view->child_focussed = 1;
4196 } else
4197 *new_view = log_view;
4198 break;
4199 case 'k':
4200 case KEY_UP:
4201 if (s->selected > 0) {
4202 s->selected--;
4203 if (s->selected == 0)
4204 break;
4206 if (s->selected > 0)
4207 break;
4208 tree_scroll_up(view, &s->first_displayed_entry, 1,
4209 s->entries, s->tree == s->root);
4210 break;
4211 case KEY_PPAGE:
4212 tree_scroll_up(view, &s->first_displayed_entry,
4213 MAX(0, view->nlines - 4 - s->selected), s->entries,
4214 s->tree == s->root);
4215 s->selected = 0;
4216 if (SIMPLEQ_FIRST(&s->entries->head) ==
4217 s->first_displayed_entry && s->tree != s->root)
4218 s->first_displayed_entry = NULL;
4219 break;
4220 case 'j':
4221 case KEY_DOWN:
4222 if (s->selected < s->ndisplayed - 1) {
4223 s->selected++;
4224 break;
4226 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4227 /* can't scroll any further */
4228 break;
4229 tree_scroll_down(&s->first_displayed_entry, 1,
4230 s->last_displayed_entry, s->entries);
4231 break;
4232 case KEY_NPAGE:
4233 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4234 == NULL) {
4235 /* can't scroll any further; move cursor down */
4236 if (s->selected < s->ndisplayed - 1)
4237 s->selected = s->ndisplayed - 1;
4238 break;
4240 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4241 view->nlines, s->last_displayed_entry, s->entries);
4242 if (nscrolled < view->nlines) {
4243 int ndisplayed = 0;
4244 struct got_tree_entry *te;
4245 te = s->first_displayed_entry;
4246 do {
4247 ndisplayed++;
4248 te = SIMPLEQ_NEXT(te, entry);
4249 } while (te);
4250 s->selected = ndisplayed - 1;
4252 break;
4253 case KEY_ENTER:
4254 case '\r':
4255 case KEY_BACKSPACE:
4256 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4257 struct tog_parent_tree *parent;
4258 /* user selected '..' */
4259 if (s->tree == s->root)
4260 break;
4261 parent = TAILQ_FIRST(&s->parents);
4262 TAILQ_REMOVE(&s->parents, parent,
4263 entry);
4264 got_object_tree_close(s->tree);
4265 s->tree = parent->tree;
4266 s->entries =
4267 got_object_tree_get_entries(s->tree);
4268 s->first_displayed_entry =
4269 parent->first_displayed_entry;
4270 s->selected_entry =
4271 parent->selected_entry;
4272 s->selected = parent->selected;
4273 free(parent);
4274 } else if (S_ISDIR(s->selected_entry->mode)) {
4275 struct got_tree_object *subtree;
4276 err = got_object_open_as_tree(&subtree,
4277 s->repo, s->selected_entry->id);
4278 if (err)
4279 break;
4280 err = tree_view_visit_subtree(subtree, s);
4281 if (err) {
4282 got_object_tree_close(subtree);
4283 break;
4285 } else if (S_ISREG(s->selected_entry->mode)) {
4286 struct tog_view *blame_view;
4287 int begin_x = view_is_parent_view(view) ?
4288 view_split_begin_x(view->begin_x) : 0;
4290 err = blame_tree_entry(&blame_view, begin_x,
4291 s->selected_entry, &s->parents,
4292 s->commit_id, s->refs, s->repo);
4293 if (err)
4294 break;
4295 if (view_is_parent_view(view)) {
4296 err = view_close_child(view);
4297 if (err)
4298 return err;
4299 err = view_set_child(view, blame_view);
4300 if (err) {
4301 view_close(blame_view);
4302 break;
4304 *focus_view = blame_view;
4305 view->child_focussed = 1;
4306 } else
4307 *new_view = blame_view;
4309 break;
4310 case KEY_RESIZE:
4311 if (s->selected > view->nlines)
4312 s->selected = s->ndisplayed - 1;
4313 break;
4314 default:
4315 break;
4318 return err;
4321 __dead static void
4322 usage_tree(void)
4324 endwin();
4325 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4326 getprogname());
4327 exit(1);
4330 static const struct got_error *
4331 cmd_tree(int argc, char *argv[])
4333 const struct got_error *error;
4334 struct got_repository *repo = NULL;
4335 struct got_reflist_head refs;
4336 char *repo_path = NULL;
4337 struct got_object_id *commit_id = NULL;
4338 char *commit_id_arg = NULL;
4339 struct got_commit_object *commit = NULL;
4340 struct got_tree_object *tree = NULL;
4341 int ch;
4342 struct tog_view *view;
4344 SIMPLEQ_INIT(&refs);
4346 #ifndef PROFILE
4347 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4348 NULL) == -1)
4349 err(1, "pledge");
4350 #endif
4352 while ((ch = getopt(argc, argv, "c:")) != -1) {
4353 switch (ch) {
4354 case 'c':
4355 commit_id_arg = optarg;
4356 break;
4357 default:
4358 usage_tree();
4359 /* NOTREACHED */
4363 argc -= optind;
4364 argv += optind;
4366 if (argc == 0) {
4367 struct got_worktree *worktree;
4368 char *cwd = getcwd(NULL, 0);
4369 if (cwd == NULL)
4370 return got_error_from_errno("getcwd");
4371 error = got_worktree_open(&worktree, cwd);
4372 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4373 goto done;
4374 if (worktree) {
4375 free(cwd);
4376 repo_path =
4377 strdup(got_worktree_get_repo_path(worktree));
4378 got_worktree_close(worktree);
4379 } else
4380 repo_path = cwd;
4381 if (repo_path == NULL) {
4382 error = got_error_from_errno("strdup");
4383 goto done;
4385 } else if (argc == 1) {
4386 repo_path = realpath(argv[0], NULL);
4387 if (repo_path == NULL)
4388 return got_error_from_errno2("realpath", argv[0]);
4389 } else
4390 usage_log();
4392 init_curses();
4394 error = got_repo_open(&repo, repo_path);
4395 if (error != NULL)
4396 goto done;
4398 error = apply_unveil(got_repo_get_path(repo), NULL);
4399 if (error)
4400 goto done;
4402 if (commit_id_arg == NULL)
4403 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4404 else
4405 error = got_object_resolve_id_str(&commit_id, repo,
4406 commit_id_arg);
4407 if (error != NULL)
4408 goto done;
4410 error = got_object_open_as_commit(&commit, repo, commit_id);
4411 if (error != NULL)
4412 goto done;
4414 error = got_object_open_as_tree(&tree, repo,
4415 got_object_commit_get_tree_id(commit));
4416 if (error != NULL)
4417 goto done;
4419 error = got_ref_list(&refs, repo);
4420 if (error)
4421 goto done;
4423 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4424 if (view == NULL) {
4425 error = got_error_from_errno("view_open");
4426 goto done;
4428 error = open_tree_view(view, tree, commit_id, &refs, repo);
4429 if (error)
4430 goto done;
4431 error = view_loop(view);
4432 done:
4433 free(repo_path);
4434 free(commit_id);
4435 if (commit)
4436 got_object_commit_close(commit);
4437 if (tree)
4438 got_object_tree_close(tree);
4439 if (repo)
4440 got_repo_close(repo);
4441 got_ref_list_free(&refs);
4442 return error;
4445 __dead static void
4446 usage(void)
4448 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4449 exit(1);
4452 static char **
4453 make_argv(const char *arg0, const char *arg1)
4455 char **argv;
4456 int argc = (arg1 == NULL ? 1 : 2);
4458 argv = calloc(argc, sizeof(char *));
4459 if (argv == NULL)
4460 err(1, "calloc");
4461 argv[0] = strdup(arg0);
4462 if (argv[0] == NULL)
4463 err(1, "calloc");
4464 if (arg1) {
4465 argv[1] = strdup(arg1);
4466 if (argv[1] == NULL)
4467 err(1, "calloc");
4470 return argv;
4473 int
4474 main(int argc, char *argv[])
4476 const struct got_error *error = NULL;
4477 struct tog_cmd *cmd = NULL;
4478 int ch, hflag = 0;
4479 char **cmd_argv = NULL;
4481 setlocale(LC_CTYPE, "");
4483 while ((ch = getopt(argc, argv, "h")) != -1) {
4484 switch (ch) {
4485 case 'h':
4486 hflag = 1;
4487 break;
4488 default:
4489 usage();
4490 /* NOTREACHED */
4494 argc -= optind;
4495 argv += optind;
4496 optind = 0;
4497 optreset = 1;
4499 if (argc == 0) {
4500 if (hflag)
4501 usage();
4502 /* Build an argument vector which runs a default command. */
4503 cmd = &tog_commands[0];
4504 cmd_argv = make_argv(cmd->name, NULL);
4505 argc = 1;
4506 } else {
4507 int i;
4509 /* Did the user specific a command? */
4510 for (i = 0; i < nitems(tog_commands); i++) {
4511 if (strncmp(tog_commands[i].name, argv[0],
4512 strlen(argv[0])) == 0) {
4513 cmd = &tog_commands[i];
4514 if (hflag)
4515 tog_commands[i].cmd_usage();
4516 break;
4519 if (cmd == NULL) {
4520 /* Did the user specify a repository? */
4521 char *repo_path = realpath(argv[0], NULL);
4522 if (repo_path) {
4523 struct got_repository *repo;
4524 error = got_repo_open(&repo, repo_path);
4525 if (error == NULL)
4526 got_repo_close(repo);
4527 } else
4528 error = got_error_from_errno2("realpath",
4529 argv[0]);
4530 if (error) {
4531 if (hflag) {
4532 fprintf(stderr, "%s: '%s' is not a "
4533 "known command\n", getprogname(),
4534 argv[0]);
4535 usage();
4537 fprintf(stderr, "%s: '%s' is neither a known "
4538 "command nor a path to a repository\n",
4539 getprogname(), argv[0]);
4540 free(repo_path);
4541 return 1;
4543 cmd = &tog_commands[0];
4544 cmd_argv = make_argv(cmd->name, repo_path);
4545 argc = 2;
4546 free(repo_path);
4550 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4551 if (error)
4552 goto done;
4553 done:
4554 endwin();
4555 free(cmd_argv);
4556 if (error)
4557 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4558 return 0;