Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_diff.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
48 #include "got_utf8.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_path.h"
52 #include "got_worktree.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 #define CTRL(x) ((x) & 0x1f)
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct tog_cmd {
69 const char *name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 };
74 __dead static void usage(int);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "diff", cmd_diff, usage_diff },
88 { "blame", cmd_blame, usage_blame },
89 { "tree", cmd_tree, usage_tree },
90 };
92 enum tog_view_type {
93 TOG_VIEW_DIFF,
94 TOG_VIEW_LOG,
95 TOG_VIEW_BLAME,
96 TOG_VIEW_TREE
97 };
99 #define TOG_EOF_STRING "(END)"
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 const char *head_ref_name;
152 struct got_repository *repo;
153 struct got_reflist_head *refs;
154 struct got_object_id *start_id;
155 sig_atomic_t quit;
156 pthread_t thread;
157 struct tog_log_thread_args thread_args;
158 struct commit_queue_entry *matched_entry;
159 struct commit_queue_entry *search_entry;
160 };
162 struct tog_blame_cb_args {
163 struct tog_blame_line *lines; /* one per line */
164 int nlines;
166 struct tog_view *view;
167 struct got_object_id *commit_id;
168 int *quit;
169 };
171 struct tog_blame_thread_args {
172 const char *path;
173 struct got_repository *repo;
174 struct tog_blame_cb_args *cb_args;
175 int *complete;
176 };
178 struct tog_blame {
179 FILE *f;
180 size_t filesize;
181 struct tog_blame_line *lines;
182 int nlines;
183 off_t *line_offsets;
184 pthread_t thread;
185 struct tog_blame_thread_args thread_args;
186 struct tog_blame_cb_args cb_args;
187 const char *path;
188 };
190 struct tog_blame_view_state {
191 int first_displayed_line;
192 int last_displayed_line;
193 int selected_line;
194 int blame_complete;
195 int eof;
196 int done;
197 struct got_object_id_queue blamed_commits;
198 struct got_object_qid *blamed_commit;
199 char *path;
200 struct got_repository *repo;
201 struct got_reflist_head *refs;
202 struct got_object_id *commit_id;
203 struct tog_blame blame;
204 int matched_line;
205 };
207 struct tog_parent_tree {
208 TAILQ_ENTRY(tog_parent_tree) entry;
209 struct got_tree_object *tree;
210 struct got_tree_entry *first_displayed_entry;
211 struct got_tree_entry *selected_entry;
212 int selected;
213 };
215 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
217 struct tog_tree_view_state {
218 char *tree_label;
219 struct got_tree_object *root;
220 struct got_tree_object *tree;
221 const struct got_tree_entries *entries;
222 struct got_tree_entry *first_displayed_entry;
223 struct got_tree_entry *last_displayed_entry;
224 struct got_tree_entry *selected_entry;
225 int ndisplayed, selected, show_ids;
226 struct tog_parent_trees parents;
227 struct got_object_id *commit_id;
228 struct got_repository *repo;
229 struct got_reflist_head *refs;
230 struct got_tree_entry *matched_entry;
231 };
233 /*
234 * We implement two types of views: parent views and child views.
236 * The 'Tab' key switches between a parent view and its child view.
237 * Child views are shown side-by-side to their parent view, provided
238 * there is enough screen estate.
240 * When a new view is opened from within a parent view, this new view
241 * becomes a child view of the parent view, replacing any existing child.
243 * When a new view is opened from within a child view, this new view
244 * becomes a parent view which will obscure the views below until the
245 * user quits the new parent view by typing 'q'.
247 * This list of views contains parent views only.
248 * Child views are only pointed to by their parent view.
249 */
250 TAILQ_HEAD(tog_view_list_head, tog_view);
252 struct tog_view {
253 TAILQ_ENTRY(tog_view) entry;
254 WINDOW *window;
255 PANEL *panel;
256 int nlines, ncols, begin_y, begin_x;
257 int lines, cols; /* copies of LINES and COLS */
258 int focussed;
259 struct tog_view *parent;
260 struct tog_view *child;
261 int child_focussed;
263 /* type-specific state */
264 enum tog_view_type type;
265 union {
266 struct tog_diff_view_state diff;
267 struct tog_log_view_state log;
268 struct tog_blame_view_state blame;
269 struct tog_tree_view_state tree;
270 } state;
272 const struct got_error *(*show)(struct tog_view *);
273 const struct got_error *(*input)(struct tog_view **,
274 struct tog_view **, struct tog_view**, struct tog_view *, int);
275 const struct got_error *(*close)(struct tog_view *);
277 const struct got_error *(*search_start)(struct tog_view *);
278 const struct got_error *(*search_next)(struct tog_view *);
279 int searching;
280 #define TOG_SEARCH_FORWARD 1
281 #define TOG_SEARCH_BACKWARD 2
282 int search_next_done;
283 regex_t regex;
284 };
286 static const struct got_error *open_diff_view(struct tog_view *,
287 struct got_object_id *, struct got_object_id *, struct tog_view *,
288 struct got_reflist_head *, struct got_repository *);
289 static const struct got_error *show_diff_view(struct tog_view *);
290 static const struct got_error *input_diff_view(struct tog_view **,
291 struct tog_view **, struct tog_view **, struct tog_view *, int);
292 static const struct got_error* close_diff_view(struct tog_view *);
294 static const struct got_error *open_log_view(struct tog_view *,
295 struct got_object_id *, struct got_reflist_head *,
296 struct got_repository *, const char *, const char *, int);
297 static const struct got_error * show_log_view(struct tog_view *);
298 static const struct got_error *input_log_view(struct tog_view **,
299 struct tog_view **, struct tog_view **, struct tog_view *, int);
300 static const struct got_error *close_log_view(struct tog_view *);
301 static const struct got_error *search_start_log_view(struct tog_view *);
302 static const struct got_error *search_next_log_view(struct tog_view *);
304 static const struct got_error *open_blame_view(struct tog_view *, char *,
305 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
306 static const struct got_error *show_blame_view(struct tog_view *);
307 static const struct got_error *input_blame_view(struct tog_view **,
308 struct tog_view **, struct tog_view **, struct tog_view *, int);
309 static const struct got_error *close_blame_view(struct tog_view *);
310 static const struct got_error *search_start_blame_view(struct tog_view *);
311 static const struct got_error *search_next_blame_view(struct tog_view *);
313 static const struct got_error *open_tree_view(struct tog_view *,
314 struct got_tree_object *, struct got_object_id *,
315 struct got_reflist_head *, struct got_repository *);
316 static const struct got_error *show_tree_view(struct tog_view *);
317 static const struct got_error *input_tree_view(struct tog_view **,
318 struct tog_view **, struct tog_view **, struct tog_view *, int);
319 static const struct got_error *close_tree_view(struct tog_view *);
320 static const struct got_error *search_start_tree_view(struct tog_view *);
321 static const struct got_error *search_next_tree_view(struct tog_view *);
323 static volatile sig_atomic_t tog_sigwinch_received;
325 static void
326 tog_sigwinch(int signo)
328 tog_sigwinch_received = 1;
331 static const struct got_error *
332 view_close(struct tog_view *view)
334 const struct got_error *err = NULL;
336 if (view->child) {
337 view_close(view->child);
338 view->child = NULL;
340 if (view->close)
341 err = view->close(view);
342 if (view->panel)
343 del_panel(view->panel);
344 if (view->window)
345 delwin(view->window);
346 free(view);
347 return err;
350 static struct tog_view *
351 view_open(int nlines, int ncols, int begin_y, int begin_x,
352 enum tog_view_type type)
354 struct tog_view *view = calloc(1, sizeof(*view));
356 if (view == NULL)
357 return NULL;
359 view->type = type;
360 view->lines = LINES;
361 view->cols = COLS;
362 view->nlines = nlines ? nlines : LINES - begin_y;
363 view->ncols = ncols ? ncols : COLS - begin_x;
364 view->begin_y = begin_y;
365 view->begin_x = begin_x;
366 view->window = newwin(nlines, ncols, begin_y, begin_x);
367 if (view->window == NULL) {
368 view_close(view);
369 return NULL;
371 view->panel = new_panel(view->window);
372 if (view->panel == NULL ||
373 set_panel_userptr(view->panel, view) != OK) {
374 view_close(view);
375 return NULL;
378 keypad(view->window, TRUE);
379 return view;
382 static int
383 view_split_begin_x(int begin_x)
385 if (begin_x > 0 || COLS < 120)
386 return 0;
387 return (COLS - MAX(COLS / 2, 80));
390 static const struct got_error *view_resize(struct tog_view *);
392 static const struct got_error *
393 view_splitscreen(struct tog_view *view)
395 const struct got_error *err = NULL;
397 view->begin_y = 0;
398 view->begin_x = view_split_begin_x(0);
399 view->nlines = LINES;
400 view->ncols = COLS - view->begin_x;
401 view->lines = LINES;
402 view->cols = COLS;
403 err = view_resize(view);
404 if (err)
405 return err;
407 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
408 return got_error_from_errno("mvwin");
410 return NULL;
413 static const struct got_error *
414 view_fullscreen(struct tog_view *view)
416 const struct got_error *err = NULL;
418 view->begin_x = 0;
419 view->begin_y = 0;
420 view->nlines = LINES;
421 view->ncols = COLS;
422 view->lines = LINES;
423 view->cols = COLS;
424 err = view_resize(view);
425 if (err)
426 return err;
428 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
429 return got_error_from_errno("mvwin");
431 return NULL;
434 static int
435 view_is_parent_view(struct tog_view *view)
437 return view->parent == NULL;
440 static const struct got_error *
441 view_resize(struct tog_view *view)
443 int nlines, ncols;
445 if (view->lines > LINES)
446 nlines = view->nlines - (view->lines - LINES);
447 else
448 nlines = view->nlines + (LINES - view->lines);
450 if (view->cols > COLS)
451 ncols = view->ncols - (view->cols - COLS);
452 else
453 ncols = view->ncols + (COLS - view->cols);
455 if (wresize(view->window, nlines, ncols) == ERR)
456 return got_error_from_errno("wresize");
457 if (replace_panel(view->panel, view->window) == ERR)
458 return got_error_from_errno("replace_panel");
459 wclear(view->window);
461 view->nlines = nlines;
462 view->ncols = ncols;
463 view->lines = LINES;
464 view->cols = COLS;
466 if (view->child) {
467 view->child->begin_x = view_split_begin_x(view->begin_x);
468 if (view->child->begin_x == 0) {
469 view_fullscreen(view->child);
470 if (view->child->focussed)
471 show_panel(view->child->panel);
472 else
473 show_panel(view->panel);
474 } else {
475 view_splitscreen(view->child);
476 show_panel(view->child->panel);
480 return NULL;
483 static const struct got_error *
484 view_close_child(struct tog_view *view)
486 const struct got_error *err = NULL;
488 if (view->child == NULL)
489 return NULL;
491 err = view_close(view->child);
492 view->child = NULL;
493 return err;
496 static const struct got_error *
497 view_set_child(struct tog_view *view, struct tog_view *child)
499 const struct got_error *err = NULL;
501 view->child = child;
502 child->parent = view;
503 return err;
506 static int
507 view_is_splitscreen(struct tog_view *view)
509 return view->begin_x > 0;
512 static void
513 tog_resizeterm(void)
515 int cols, lines;
516 struct winsize size;
518 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
519 cols = 80; /* Default */
520 lines = 24;
521 } else {
522 cols = size.ws_col;
523 lines = size.ws_row;
525 resize_term(lines, cols);
528 static const struct got_error *
529 view_search_start(struct tog_view *view)
531 const struct got_error *err = NULL;
532 char pattern[1024];
533 int ret;
535 if (view->nlines < 1)
536 return NULL;
538 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
539 view->begin_x, "/");
540 wclrtoeol(view->window);
542 nocbreak();
543 echo();
544 ret = wgetnstr(view->window, pattern, sizeof(pattern));
545 cbreak();
546 noecho();
547 if (ret == ERR)
548 return NULL;
550 if (view->searching) {
551 regfree(&view->regex);
552 view->searching = 0;
555 if (regcomp(&view->regex, pattern,
556 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
557 err = view->search_start(view);
558 if (err) {
559 regfree(&view->regex);
560 return err;
562 view->searching = TOG_SEARCH_FORWARD;
563 view->search_next_done = 0;
564 view->search_next(view);
567 return NULL;
570 static const struct got_error *
571 view_input(struct tog_view **new, struct tog_view **dead,
572 struct tog_view **focus, int *done, struct tog_view *view,
573 struct tog_view_list_head *views)
575 const struct got_error *err = NULL;
576 struct tog_view *v;
577 int ch, errcode;
579 *new = NULL;
580 *dead = NULL;
581 *focus = NULL;
583 if (view->searching && !view->search_next_done) {
584 errcode = pthread_mutex_unlock(&tog_mutex);
585 if (errcode)
586 return got_error_set_errno(errcode,
587 "pthread_mutex_unlock");
588 pthread_yield();
589 errcode = pthread_mutex_lock(&tog_mutex);
590 if (errcode)
591 return got_error_set_errno(errcode,
592 "pthread_mutex_lock");
593 view->search_next(view);
594 return NULL;
597 nodelay(stdscr, FALSE);
598 /* Allow threads to make progress while we are waiting for input. */
599 errcode = pthread_mutex_unlock(&tog_mutex);
600 if (errcode)
601 return got_error_set_errno(errcode, "pthread_mutex_unlock");
602 ch = wgetch(view->window);
603 errcode = pthread_mutex_lock(&tog_mutex);
604 if (errcode)
605 return got_error_set_errno(errcode, "pthread_mutex_lock");
606 nodelay(stdscr, TRUE);
608 if (tog_sigwinch_received) {
609 tog_resizeterm();
610 tog_sigwinch_received = 0;
611 TAILQ_FOREACH(v, views, entry) {
612 err = view_resize(v);
613 if (err)
614 return err;
615 err = v->input(new, dead, focus, v, KEY_RESIZE);
616 if (err)
617 return err;
621 switch (ch) {
622 case ERR:
623 break;
624 case '\t':
625 if (view->child) {
626 *focus = view->child;
627 view->child_focussed = 1;
628 } else if (view->parent) {
629 *focus = view->parent;
630 view->parent->child_focussed = 0;
632 break;
633 case 'q':
634 err = view->input(new, dead, focus, view, ch);
635 *dead = view;
636 break;
637 case 'Q':
638 *done = 1;
639 break;
640 case 'f':
641 if (view_is_parent_view(view)) {
642 if (view->child == NULL)
643 break;
644 if (view_is_splitscreen(view->child)) {
645 *focus = view->child;
646 view->child_focussed = 1;
647 err = view_fullscreen(view->child);
648 } else
649 err = view_splitscreen(view->child);
650 if (err)
651 break;
652 err = view->child->input(new, dead, focus,
653 view->child, KEY_RESIZE);
654 } else {
655 if (view_is_splitscreen(view)) {
656 *focus = view;
657 view->parent->child_focussed = 1;
658 err = view_fullscreen(view);
659 } else {
660 err = view_splitscreen(view);
662 if (err)
663 break;
664 err = view->input(new, dead, focus, view,
665 KEY_RESIZE);
667 break;
668 case KEY_RESIZE:
669 break;
670 case '/':
671 if (view->search_start)
672 view_search_start(view);
673 else
674 err = view->input(new, dead, focus, view, ch);
675 break;
676 case 'N':
677 case 'n':
678 if (view->search_next && view->searching) {
679 view->searching = (ch == 'n' ?
680 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
681 view->search_next_done = 0;
682 view->search_next(view);
683 } else
684 err = view->input(new, dead, focus, view, ch);
685 break;
686 default:
687 err = view->input(new, dead, focus, view, ch);
688 break;
691 return err;
694 void
695 view_vborder(struct tog_view *view)
697 PANEL *panel;
698 struct tog_view *view_above;
700 if (view->parent)
701 return view_vborder(view->parent);
703 panel = panel_above(view->panel);
704 if (panel == NULL)
705 return;
707 view_above = panel_userptr(panel);
708 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
709 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
712 int
713 view_needs_focus_indication(struct tog_view *view)
715 if (view_is_parent_view(view)) {
716 if (view->child == NULL || view->child_focussed)
717 return 0;
718 if (!view_is_splitscreen(view->child))
719 return 0;
720 } else if (!view_is_splitscreen(view))
721 return 0;
723 return view->focussed;
726 static const struct got_error *
727 view_loop(struct tog_view *view)
729 const struct got_error *err = NULL;
730 struct tog_view_list_head views;
731 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
732 int fast_refresh = 10;
733 int done = 0, errcode;
735 errcode = pthread_mutex_lock(&tog_mutex);
736 if (errcode)
737 return got_error_set_errno(errcode, "pthread_mutex_lock");
739 TAILQ_INIT(&views);
740 TAILQ_INSERT_HEAD(&views, view, entry);
742 main_view = view;
743 view->focussed = 1;
744 err = view->show(view);
745 if (err)
746 return err;
747 update_panels();
748 doupdate();
749 while (!TAILQ_EMPTY(&views) && !done) {
750 /* Refresh fast during initialization, then become slower. */
751 if (fast_refresh && fast_refresh-- == 0)
752 halfdelay(10); /* switch to once per second */
754 err = view_input(&new_view, &dead_view, &focus_view, &done,
755 view, &views);
756 if (err)
757 break;
758 if (dead_view) {
759 struct tog_view *prev = NULL;
761 if (view_is_parent_view(dead_view))
762 prev = TAILQ_PREV(dead_view,
763 tog_view_list_head, entry);
764 else if (view->parent != dead_view)
765 prev = view->parent;
767 if (dead_view->parent)
768 dead_view->parent->child = NULL;
769 else
770 TAILQ_REMOVE(&views, dead_view, entry);
772 err = view_close(dead_view);
773 if (err || (dead_view == main_view && new_view == NULL))
774 goto done;
776 if (view == dead_view) {
777 if (focus_view)
778 view = focus_view;
779 else if (prev)
780 view = prev;
781 else if (!TAILQ_EMPTY(&views))
782 view = TAILQ_LAST(&views,
783 tog_view_list_head);
784 else
785 view = NULL;
786 if (view) {
787 if (view->child && view->child_focussed)
788 focus_view = view->child;
789 else
790 focus_view = view;
794 if (new_view) {
795 struct tog_view *v, *t;
796 /* Only allow one parent view per type. */
797 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
798 if (v->type != new_view->type)
799 continue;
800 TAILQ_REMOVE(&views, v, entry);
801 err = view_close(v);
802 if (err)
803 goto done;
804 break;
806 TAILQ_INSERT_TAIL(&views, new_view, entry);
807 view = new_view;
808 if (focus_view == NULL)
809 focus_view = new_view;
811 if (focus_view) {
812 show_panel(focus_view->panel);
813 if (view)
814 view->focussed = 0;
815 focus_view->focussed = 1;
816 view = focus_view;
817 if (new_view)
818 show_panel(new_view->panel);
819 if (view->child && view_is_splitscreen(view->child))
820 show_panel(view->child->panel);
822 if (view) {
823 if (focus_view == NULL) {
824 view->focussed = 1;
825 show_panel(view->panel);
826 if (view->child && view_is_splitscreen(view->child))
827 show_panel(view->child->panel);
828 focus_view = view;
830 if (view->parent) {
831 err = view->parent->show(view->parent);
832 if (err)
833 goto done;
835 err = view->show(view);
836 if (err)
837 goto done;
838 if (view->child) {
839 err = view->child->show(view->child);
840 if (err)
841 goto done;
843 update_panels();
844 doupdate();
847 done:
848 while (!TAILQ_EMPTY(&views)) {
849 view = TAILQ_FIRST(&views);
850 TAILQ_REMOVE(&views, view, entry);
851 view_close(view);
854 errcode = pthread_mutex_unlock(&tog_mutex);
855 if (errcode)
856 return got_error_set_errno(errcode, "pthread_mutex_unlock");
858 return err;
861 __dead static void
862 usage_log(void)
864 endwin();
865 fprintf(stderr,
866 "usage: %s log [-c commit] [-r repository-path] [path]\n",
867 getprogname());
868 exit(1);
871 /* Create newly allocated wide-character string equivalent to a byte string. */
872 static const struct got_error *
873 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
875 char *vis = NULL;
876 const struct got_error *err = NULL;
878 *ws = NULL;
879 *wlen = mbstowcs(NULL, s, 0);
880 if (*wlen == (size_t)-1) {
881 int vislen;
882 if (errno != EILSEQ)
883 return got_error_from_errno("mbstowcs");
885 /* byte string invalid in current encoding; try to "fix" it */
886 err = got_mbsavis(&vis, &vislen, s);
887 if (err)
888 return err;
889 *wlen = mbstowcs(NULL, vis, 0);
890 if (*wlen == (size_t)-1) {
891 err = got_error_from_errno("mbstowcs"); /* give up */
892 goto done;
896 *ws = calloc(*wlen + 1, sizeof(*ws));
897 if (*ws == NULL) {
898 err = got_error_from_errno("calloc");
899 goto done;
902 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
903 err = got_error_from_errno("mbstowcs");
904 done:
905 free(vis);
906 if (err) {
907 free(*ws);
908 *ws = NULL;
909 *wlen = 0;
911 return err;
914 /* Format a line for display, ensuring that it won't overflow a width limit. */
915 static const struct got_error *
916 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
918 const struct got_error *err = NULL;
919 int cols = 0;
920 wchar_t *wline = NULL;
921 size_t wlen;
922 int i;
924 *wlinep = NULL;
925 *widthp = 0;
927 err = mbs2ws(&wline, &wlen, line);
928 if (err)
929 return err;
931 i = 0;
932 while (i < wlen && cols < wlimit) {
933 int width = wcwidth(wline[i]);
934 switch (width) {
935 case 0:
936 i++;
937 break;
938 case 1:
939 case 2:
940 if (cols + width <= wlimit)
941 cols += width;
942 i++;
943 break;
944 case -1:
945 if (wline[i] == L'\t')
946 cols += TABSIZE - ((cols + 1) % TABSIZE);
947 i++;
948 break;
949 default:
950 err = got_error_from_errno("wcwidth");
951 goto done;
954 wline[i] = L'\0';
955 if (widthp)
956 *widthp = cols;
957 done:
958 if (err)
959 free(wline);
960 else
961 *wlinep = wline;
962 return err;
965 static const struct got_error*
966 build_refs_str(char **refs_str, struct got_reflist_head *refs,
967 struct got_object_id *id)
969 static const struct got_error *err = NULL;
970 struct got_reflist_entry *re;
971 char *s;
972 const char *name;
974 *refs_str = NULL;
976 SIMPLEQ_FOREACH(re, refs, entry) {
977 if (got_object_id_cmp(re->id, id) != 0)
978 continue;
979 name = got_ref_get_name(re->ref);
980 if (strcmp(name, GOT_REF_HEAD) == 0)
981 continue;
982 if (strncmp(name, "refs/", 5) == 0)
983 name += 5;
984 if (strncmp(name, "got/", 4) == 0)
985 continue;
986 if (strncmp(name, "heads/", 6) == 0)
987 name += 6;
988 if (strncmp(name, "remotes/", 8) == 0)
989 name += 8;
990 s = *refs_str;
991 if (asprintf(refs_str, "%s%s%s", s ? s : "",
992 s ? ", " : "", name) == -1) {
993 err = got_error_from_errno("asprintf");
994 free(s);
995 *refs_str = NULL;
996 break;
998 free(s);
1001 return err;
1004 static const struct got_error *
1005 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1007 char *smallerthan, *at;
1009 smallerthan = strchr(author, '<');
1010 if (smallerthan && smallerthan[1] != '\0')
1011 author = smallerthan + 1;
1012 at = strchr(author, '@');
1013 if (at)
1014 *at = '\0';
1015 return format_line(wauthor, author_width, author, limit);
1018 static const struct got_error *
1019 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1020 struct got_object_id *id, struct got_reflist_head *refs,
1021 int author_display_cols)
1023 const struct got_error *err = NULL;
1024 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1025 char *logmsg0 = NULL, *logmsg = NULL;
1026 char *author = NULL;
1027 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1028 int author_width, logmsg_width;
1029 char *newline, *line = NULL;
1030 int col, limit;
1031 static const size_t date_display_cols = 9;
1032 const int avail = view->ncols;
1033 struct tm tm;
1034 time_t committer_time;
1036 committer_time = got_object_commit_get_committer_time(commit);
1037 if (localtime_r(&committer_time, &tm) == NULL)
1038 return got_error_from_errno("localtime_r");
1039 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1040 >= sizeof(datebuf))
1041 return got_error(GOT_ERR_NO_SPACE);
1043 if (avail < date_display_cols)
1044 limit = MIN(sizeof(datebuf) - 1, avail);
1045 else
1046 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1047 waddnstr(view->window, datebuf, limit);
1048 col = limit + 1;
1049 if (col > avail)
1050 goto done;
1052 author = strdup(got_object_commit_get_author(commit));
1053 if (author == NULL) {
1054 err = got_error_from_errno("strdup");
1055 goto done;
1057 err = format_author(&wauthor, &author_width, author, avail - col);
1058 if (err)
1059 goto done;
1060 waddwstr(view->window, wauthor);
1061 col += author_width;
1062 while (col <= avail && author_width < author_display_cols + 2) {
1063 waddch(view->window, ' ');
1064 col++;
1065 author_width++;
1067 if (col > avail)
1068 goto done;
1070 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1071 if (logmsg0 == NULL) {
1072 err = got_error_from_errno("strdup");
1073 goto done;
1075 logmsg = logmsg0;
1076 while (*logmsg == '\n')
1077 logmsg++;
1078 newline = strchr(logmsg, '\n');
1079 if (newline)
1080 *newline = '\0';
1081 limit = avail - col;
1082 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1083 if (err)
1084 goto done;
1085 waddwstr(view->window, wlogmsg);
1086 col += logmsg_width;
1087 while (col <= avail) {
1088 waddch(view->window, ' ');
1089 col++;
1091 done:
1092 free(logmsg0);
1093 free(wlogmsg);
1094 free(author);
1095 free(wauthor);
1096 free(line);
1097 return err;
1100 static struct commit_queue_entry *
1101 alloc_commit_queue_entry(struct got_commit_object *commit,
1102 struct got_object_id *id)
1104 struct commit_queue_entry *entry;
1106 entry = calloc(1, sizeof(*entry));
1107 if (entry == NULL)
1108 return NULL;
1110 entry->id = id;
1111 entry->commit = commit;
1112 return entry;
1115 static void
1116 pop_commit(struct commit_queue *commits)
1118 struct commit_queue_entry *entry;
1120 entry = TAILQ_FIRST(&commits->head);
1121 TAILQ_REMOVE(&commits->head, entry, entry);
1122 got_object_commit_close(entry->commit);
1123 commits->ncommits--;
1124 /* Don't free entry->id! It is owned by the commit graph. */
1125 free(entry);
1128 static void
1129 free_commits(struct commit_queue *commits)
1131 while (!TAILQ_EMPTY(&commits->head))
1132 pop_commit(commits);
1135 static const struct got_error *
1136 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1137 int minqueue, struct got_repository *repo, const char *path)
1139 const struct got_error *err = NULL;
1140 int nqueued = 0;
1143 * We keep all commits open throughout the lifetime of the log
1144 * view in order to avoid having to re-fetch commits from disk
1145 * while updating the display.
1147 while (nqueued < minqueue) {
1148 struct got_object_id *id;
1149 struct got_commit_object *commit;
1150 struct commit_queue_entry *entry;
1151 int errcode;
1153 err = got_commit_graph_iter_next(&id, graph);
1154 if (err) {
1155 if (err->code != GOT_ERR_ITER_NEED_MORE)
1156 break;
1157 err = got_commit_graph_fetch_commits(graph,
1158 minqueue, repo);
1159 if (err)
1160 return err;
1161 continue;
1164 if (id == NULL)
1165 break;
1167 err = got_object_open_as_commit(&commit, repo, id);
1168 if (err)
1169 break;
1170 entry = alloc_commit_queue_entry(commit, id);
1171 if (entry == NULL) {
1172 err = got_error_from_errno("alloc_commit_queue_entry");
1173 break;
1176 errcode = pthread_mutex_lock(&tog_mutex);
1177 if (errcode) {
1178 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1179 break;
1182 entry->idx = commits->ncommits;
1183 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1184 nqueued++;
1185 commits->ncommits++;
1187 errcode = pthread_mutex_unlock(&tog_mutex);
1188 if (errcode && err == NULL)
1189 err = got_error_set_errno(errcode,
1190 "pthread_mutex_unlock");
1193 return err;
1196 static const struct got_error *
1197 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1198 struct got_repository *repo)
1200 const struct got_error *err = NULL;
1201 struct got_reference *head_ref;
1203 *head_id = NULL;
1205 err = got_ref_open(&head_ref, repo, branch_name, 0);
1206 if (err)
1207 return err;
1209 err = got_ref_resolve(head_id, repo, head_ref);
1210 got_ref_close(head_ref);
1211 if (err) {
1212 *head_id = NULL;
1213 return err;
1216 return NULL;
1219 static const struct got_error *
1220 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1221 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1222 struct commit_queue *commits, int selected_idx, int limit,
1223 struct got_reflist_head *refs, const char *path, int commits_needed)
1225 const struct got_error *err = NULL;
1226 struct commit_queue_entry *entry;
1227 int width;
1228 int ncommits, author_cols = 10;
1229 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1230 char *refs_str = NULL;
1231 wchar_t *wline;
1233 entry = first;
1234 ncommits = 0;
1235 while (entry) {
1236 if (ncommits == selected_idx) {
1237 *selected = entry;
1238 break;
1240 entry = TAILQ_NEXT(entry, entry);
1241 ncommits++;
1244 if (*selected && !(view->searching && view->search_next_done == 0)) {
1245 err = got_object_id_str(&id_str, (*selected)->id);
1246 if (err)
1247 return err;
1248 if (refs) {
1249 err = build_refs_str(&refs_str, refs, (*selected)->id);
1250 if (err)
1251 goto done;
1255 if (commits_needed == 0)
1256 halfdelay(10); /* disable fast refresh */
1258 if (asprintf(&ncommits_str, " [%d/%d] %s",
1259 entry ? entry->idx + 1 : 0, commits->ncommits,
1260 commits_needed > 0 ?
1261 (view->searching && view->search_next_done == 0
1262 ? "searching..." : "loading... ") :
1263 (refs_str ? refs_str : "")) == -1) {
1264 err = got_error_from_errno("asprintf");
1265 goto done;
1268 if (path && strcmp(path, "/") != 0) {
1269 if (asprintf(&header, "commit %s %s%s",
1270 id_str ? id_str : "........................................",
1271 path, ncommits_str) == -1) {
1272 err = got_error_from_errno("asprintf");
1273 header = NULL;
1274 goto done;
1276 } else if (asprintf(&header, "commit %s%s",
1277 id_str ? id_str : "........................................",
1278 ncommits_str) == -1) {
1279 err = got_error_from_errno("asprintf");
1280 header = NULL;
1281 goto done;
1283 err = format_line(&wline, &width, header, view->ncols);
1284 if (err)
1285 goto done;
1287 werase(view->window);
1289 if (view_needs_focus_indication(view))
1290 wstandout(view->window);
1291 waddwstr(view->window, wline);
1292 while (width < view->ncols) {
1293 waddch(view->window, ' ');
1294 width++;
1296 if (view_needs_focus_indication(view))
1297 wstandend(view->window);
1298 free(wline);
1299 if (limit <= 1)
1300 goto done;
1302 /* Grow author column size if necessary. */
1303 entry = first;
1304 ncommits = 0;
1305 while (entry) {
1306 char *author;
1307 wchar_t *wauthor;
1308 int width;
1309 if (ncommits >= limit - 1)
1310 break;
1311 author = strdup(got_object_commit_get_author(entry->commit));
1312 if (author == NULL) {
1313 err = got_error_from_errno("strdup");
1314 goto done;
1316 err = format_author(&wauthor, &width, author, COLS);
1317 if (author_cols < width)
1318 author_cols = width;
1319 free(wauthor);
1320 free(author);
1321 entry = TAILQ_NEXT(entry, entry);
1324 entry = first;
1325 *last = first;
1326 ncommits = 0;
1327 while (entry) {
1328 if (ncommits >= limit - 1)
1329 break;
1330 if (ncommits == selected_idx)
1331 wstandout(view->window);
1332 err = draw_commit(view, entry->commit, entry->id, refs,
1333 author_cols);
1334 if (ncommits == selected_idx)
1335 wstandend(view->window);
1336 if (err)
1337 goto done;
1338 ncommits++;
1339 *last = entry;
1340 entry = TAILQ_NEXT(entry, entry);
1343 view_vborder(view);
1344 done:
1345 free(id_str);
1346 free(refs_str);
1347 free(ncommits_str);
1348 free(header);
1349 return err;
1352 static void
1353 scroll_up(struct tog_view *view,
1354 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1355 struct commit_queue *commits)
1357 struct commit_queue_entry *entry;
1358 int nscrolled = 0;
1360 entry = TAILQ_FIRST(&commits->head);
1361 if (*first_displayed_entry == entry)
1362 return;
1364 entry = *first_displayed_entry;
1365 while (entry && nscrolled < maxscroll) {
1366 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1367 if (entry) {
1368 *first_displayed_entry = entry;
1369 nscrolled++;
1374 static const struct got_error *
1375 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1376 pthread_cond_t *need_commits)
1378 int errcode;
1379 int max_wait = 20;
1381 halfdelay(1); /* fast refresh while loading commits */
1383 while (*commits_needed > 0) {
1384 if (*log_complete)
1385 break;
1387 /* Wake the log thread. */
1388 errcode = pthread_cond_signal(need_commits);
1389 if (errcode)
1390 return got_error_set_errno(errcode,
1391 "pthread_cond_signal");
1392 errcode = pthread_mutex_unlock(&tog_mutex);
1393 if (errcode)
1394 return got_error_set_errno(errcode,
1395 "pthread_mutex_unlock");
1396 pthread_yield();
1397 errcode = pthread_mutex_lock(&tog_mutex);
1398 if (errcode)
1399 return got_error_set_errno(errcode,
1400 "pthread_mutex_lock");
1402 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1404 * Thread is not done yet; lose a key press
1405 * and let the user retry... this way the GUI
1406 * remains interactive while logging deep paths
1407 * with few commits in history.
1409 return NULL;
1413 return NULL;
1416 static const struct got_error *
1417 scroll_down(struct tog_view *view,
1418 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1419 struct commit_queue_entry **last_displayed_entry,
1420 struct commit_queue *commits, int *log_complete, int *commits_needed,
1421 pthread_cond_t *need_commits)
1423 const struct got_error *err = NULL;
1424 struct commit_queue_entry *pentry;
1425 int nscrolled = 0;
1427 if (*last_displayed_entry == NULL)
1428 return NULL;
1430 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1431 if (pentry == NULL && !*log_complete) {
1433 * Ask the log thread for required amount of commits
1434 * plus some amount of pre-fetching.
1436 (*commits_needed) += maxscroll + 20;
1437 err = trigger_log_thread(0, commits_needed, log_complete,
1438 need_commits);
1439 if (err)
1440 return err;
1443 do {
1444 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1445 if (pentry == NULL)
1446 break;
1448 *last_displayed_entry = pentry;
1450 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1451 if (pentry == NULL)
1452 break;
1453 *first_displayed_entry = pentry;
1454 } while (++nscrolled < maxscroll);
1456 return err;
1459 static const struct got_error *
1460 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1461 struct got_commit_object *commit, struct got_object_id *commit_id,
1462 struct tog_view *log_view, struct got_reflist_head *refs,
1463 struct got_repository *repo)
1465 const struct got_error *err;
1466 struct got_object_qid *parent_id;
1467 struct tog_view *diff_view;
1469 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1470 if (diff_view == NULL)
1471 return got_error_from_errno("view_open");
1473 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1474 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1475 commit_id, log_view, refs, repo);
1476 if (err == NULL)
1477 *new_view = diff_view;
1478 return err;
1481 static const struct got_error *
1482 tree_view_visit_subtree(struct got_tree_object *subtree,
1483 struct tog_tree_view_state *s)
1485 struct tog_parent_tree *parent;
1487 parent = calloc(1, sizeof(*parent));
1488 if (parent == NULL)
1489 return got_error_from_errno("calloc");
1491 parent->tree = s->tree;
1492 parent->first_displayed_entry = s->first_displayed_entry;
1493 parent->selected_entry = s->selected_entry;
1494 parent->selected = s->selected;
1495 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1496 s->tree = subtree;
1497 s->entries = got_object_tree_get_entries(s->tree);
1498 s->selected = 0;
1499 s->first_displayed_entry = NULL;
1500 return NULL;
1504 static const struct got_error *
1505 browse_commit_tree(struct tog_view **new_view, int begin_x,
1506 struct commit_queue_entry *entry, const char *path,
1507 struct got_reflist_head *refs, struct got_repository *repo)
1509 const struct got_error *err = NULL;
1510 struct got_tree_object *tree;
1511 struct got_tree_entry *te;
1512 struct tog_tree_view_state *s;
1513 struct tog_view *tree_view;
1514 char *slash, *subpath = NULL;
1515 const char *p;
1517 err = got_object_open_as_tree(&tree, repo,
1518 got_object_commit_get_tree_id(entry->commit));
1519 if (err)
1520 return err;
1522 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1523 if (tree_view == NULL)
1524 return got_error_from_errno("view_open");
1526 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1527 if (err) {
1528 got_object_tree_close(tree);
1529 return err;
1531 s = &tree_view->state.tree;
1533 *new_view = tree_view;
1535 /* Walk the path and open corresponding tree objects. */
1536 p = path;
1537 while (*p) {
1538 struct got_object_id *tree_id;
1540 while (p[0] == '/')
1541 p++;
1543 /* Ensure the correct subtree entry is selected. */
1544 slash = strchr(p, '/');
1545 if (slash == NULL)
1546 slash = strchr(p, '\0');
1547 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1548 if (strncmp(p, te->name, slash - p) == 0) {
1549 s->selected_entry = te;
1550 break;
1552 s->selected++;
1554 if (s->selected_entry == NULL) {
1555 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1556 break;
1558 if (s->tree != s->root)
1559 s->selected++; /* skip '..' */
1561 if (!S_ISDIR(s->selected_entry->mode)) {
1562 /* Jump to this file's entry. */
1563 s->first_displayed_entry = s->selected_entry;
1564 s->selected = 0;
1565 break;
1568 slash = strchr(p, '/');
1569 if (slash)
1570 subpath = strndup(path, slash - path);
1571 else
1572 subpath = strdup(path);
1573 if (subpath == NULL) {
1574 err = got_error_from_errno("strdup");
1575 break;
1578 err = got_object_id_by_path(&tree_id, repo, entry->id,
1579 subpath);
1580 if (err)
1581 break;
1583 err = got_object_open_as_tree(&tree, repo, tree_id);
1584 free(tree_id);
1585 if (err)
1586 break;
1588 err = tree_view_visit_subtree(tree, s);
1589 if (err) {
1590 got_object_tree_close(tree);
1591 break;
1593 if (slash == NULL)
1594 break;
1595 free(subpath);
1596 subpath = NULL;
1597 p = slash;
1600 free(subpath);
1601 return err;
1604 static void *
1605 log_thread(void *arg)
1607 const struct got_error *err = NULL;
1608 int errcode = 0;
1609 struct tog_log_thread_args *a = arg;
1610 int done = 0;
1612 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1613 if (err)
1614 return (void *)err;
1616 while (!done && !err) {
1617 err = queue_commits(a->graph, a->commits, 1, a->repo,
1618 a->in_repo_path);
1619 if (err) {
1620 if (err->code != GOT_ERR_ITER_COMPLETED)
1621 return (void *)err;
1622 err = NULL;
1623 done = 1;
1624 } else if (a->commits_needed > 0)
1625 a->commits_needed--;
1627 errcode = pthread_mutex_lock(&tog_mutex);
1628 if (errcode) {
1629 err = got_error_set_errno(errcode,
1630 "pthread_mutex_lock");
1631 break;
1632 } else if (*a->quit)
1633 done = 1;
1634 else if (*a->first_displayed_entry == NULL) {
1635 *a->first_displayed_entry =
1636 TAILQ_FIRST(&a->commits->head);
1637 *a->selected_entry = *a->first_displayed_entry;
1640 if (done)
1641 a->commits_needed = 0;
1642 else if (a->commits_needed == 0) {
1643 errcode = pthread_cond_wait(&a->need_commits,
1644 &tog_mutex);
1645 if (errcode)
1646 err = got_error_set_errno(errcode,
1647 "pthread_cond_wait");
1650 errcode = pthread_mutex_unlock(&tog_mutex);
1651 if (errcode && err == NULL)
1652 err = got_error_set_errno(errcode,
1653 "pthread_mutex_unlock");
1655 a->log_complete = 1;
1656 return (void *)err;
1659 static const struct got_error *
1660 stop_log_thread(struct tog_log_view_state *s)
1662 const struct got_error *err = NULL;
1663 int errcode;
1665 if (s->thread) {
1666 s->quit = 1;
1667 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1668 if (errcode)
1669 return got_error_set_errno(errcode,
1670 "pthread_cond_signal");
1671 errcode = pthread_mutex_unlock(&tog_mutex);
1672 if (errcode)
1673 return got_error_set_errno(errcode,
1674 "pthread_mutex_unlock");
1675 errcode = pthread_join(s->thread, (void **)&err);
1676 if (errcode)
1677 return got_error_set_errno(errcode, "pthread_join");
1678 errcode = pthread_mutex_lock(&tog_mutex);
1679 if (errcode)
1680 return got_error_set_errno(errcode,
1681 "pthread_mutex_lock");
1682 s->thread = NULL;
1685 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1686 if (errcode && err == NULL)
1687 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1689 if (s->thread_args.repo) {
1690 got_repo_close(s->thread_args.repo);
1691 s->thread_args.repo = NULL;
1694 if (s->thread_args.graph) {
1695 got_commit_graph_close(s->thread_args.graph);
1696 s->thread_args.graph = NULL;
1699 return err;
1702 static const struct got_error *
1703 close_log_view(struct tog_view *view)
1705 const struct got_error *err = NULL;
1706 struct tog_log_view_state *s = &view->state.log;
1708 err = stop_log_thread(s);
1709 free_commits(&s->commits);
1710 free(s->in_repo_path);
1711 s->in_repo_path = NULL;
1712 free(s->start_id);
1713 s->start_id = NULL;
1714 return err;
1717 static const struct got_error *
1718 search_start_log_view(struct tog_view *view)
1720 struct tog_log_view_state *s = &view->state.log;
1722 s->matched_entry = NULL;
1723 s->search_entry = NULL;
1724 return NULL;
1727 static int
1728 match_commit(struct got_commit_object *commit, const char *id_str,
1729 regex_t *regex)
1731 regmatch_t regmatch;
1733 if (regexec(regex, got_object_commit_get_author(commit), 1,
1734 &regmatch, 0) == 0 ||
1735 regexec(regex, got_object_commit_get_committer(commit), 1,
1736 &regmatch, 0) == 0 ||
1737 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1738 &regmatch, 0) == 0 ||
1739 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1740 return 1;
1742 return 0;
1745 static const struct got_error *
1746 search_next_log_view(struct tog_view *view)
1748 const struct got_error *err = NULL;
1749 struct tog_log_view_state *s = &view->state.log;
1750 struct commit_queue_entry *entry;
1752 if (!view->searching) {
1753 view->search_next_done = 1;
1754 return NULL;
1757 if (s->search_entry) {
1758 if (wgetch(view->window) == KEY_BACKSPACE) {
1759 view->search_next_done = 1;
1760 return NULL;
1762 if (view->searching == TOG_SEARCH_FORWARD)
1763 entry = TAILQ_NEXT(s->search_entry, entry);
1764 else
1765 entry = TAILQ_PREV(s->search_entry,
1766 commit_queue_head, entry);
1767 } else if (s->matched_entry) {
1768 if (view->searching == TOG_SEARCH_FORWARD)
1769 entry = TAILQ_NEXT(s->selected_entry, entry);
1770 else
1771 entry = TAILQ_PREV(s->selected_entry,
1772 commit_queue_head, entry);
1773 } else {
1774 if (view->searching == TOG_SEARCH_FORWARD)
1775 entry = TAILQ_FIRST(&s->commits.head);
1776 else
1777 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1780 while (1) {
1781 char *id_str;
1782 if (entry == NULL) {
1783 if (s->thread_args.log_complete ||
1784 view->searching == TOG_SEARCH_BACKWARD) {
1785 view->search_next_done = 1;
1786 return NULL;
1789 * Poke the log thread for more commits and return,
1790 * allowing the main loop to make progress. Search
1791 * will resume at s->search_entry once we come back.
1793 s->thread_args.commits_needed++;
1794 return trigger_log_thread(1,
1795 &s->thread_args.commits_needed,
1796 &s->thread_args.log_complete,
1797 &s->thread_args.need_commits);
1800 err = got_object_id_str(&id_str, entry->id);
1801 if (err)
1802 return err;
1804 if (match_commit(entry->commit, id_str, &view->regex)) {
1805 view->search_next_done = 1;
1806 s->matched_entry = entry;
1807 free(id_str);
1808 break;
1810 free(id_str);
1811 s->search_entry = entry;
1812 if (view->searching == TOG_SEARCH_FORWARD)
1813 entry = TAILQ_NEXT(entry, entry);
1814 else
1815 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1818 if (s->matched_entry) {
1819 int cur = s->selected_entry->idx;
1820 while (cur < s->matched_entry->idx) {
1821 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1822 if (err)
1823 return err;
1824 cur++;
1826 while (cur > s->matched_entry->idx) {
1827 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1828 if (err)
1829 return err;
1830 cur--;
1834 s->search_entry = NULL;
1836 return NULL;
1839 static const struct got_error *
1840 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1841 struct got_reflist_head *refs, struct got_repository *repo,
1842 const char *head_ref_name, const char *path, int check_disk)
1844 const struct got_error *err = NULL;
1845 struct tog_log_view_state *s = &view->state.log;
1846 struct got_repository *thread_repo = NULL;
1847 struct got_commit_graph *thread_graph = NULL;
1848 int errcode;
1850 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1851 if (err != NULL)
1852 goto done;
1854 /* The commit queue only contains commits being displayed. */
1855 TAILQ_INIT(&s->commits.head);
1856 s->commits.ncommits = 0;
1858 s->refs = refs;
1859 s->repo = repo;
1860 s->head_ref_name = head_ref_name;
1861 s->start_id = got_object_id_dup(start_id);
1862 if (s->start_id == NULL) {
1863 err = got_error_from_errno("got_object_id_dup");
1864 goto done;
1867 view->show = show_log_view;
1868 view->input = input_log_view;
1869 view->close = close_log_view;
1870 view->search_start = search_start_log_view;
1871 view->search_next = search_next_log_view;
1873 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1874 if (err)
1875 goto done;
1876 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1877 0, thread_repo);
1878 if (err)
1879 goto done;
1881 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1882 if (errcode) {
1883 err = got_error_set_errno(errcode, "pthread_cond_init");
1884 goto done;
1887 s->thread_args.commits_needed = view->nlines;
1888 s->thread_args.graph = thread_graph;
1889 s->thread_args.commits = &s->commits;
1890 s->thread_args.in_repo_path = s->in_repo_path;
1891 s->thread_args.start_id = s->start_id;
1892 s->thread_args.repo = thread_repo;
1893 s->thread_args.log_complete = 0;
1894 s->thread_args.quit = &s->quit;
1895 s->thread_args.view = view;
1896 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1897 s->thread_args.selected_entry = &s->selected_entry;
1898 done:
1899 if (err)
1900 close_log_view(view);
1901 return err;
1904 static const struct got_error *
1905 show_log_view(struct tog_view *view)
1907 struct tog_log_view_state *s = &view->state.log;
1909 if (s->thread == NULL) {
1910 int errcode = pthread_create(&s->thread, NULL, log_thread,
1911 &s->thread_args);
1912 if (errcode)
1913 return got_error_set_errno(errcode, "pthread_create");
1916 return draw_commits(view, &s->last_displayed_entry,
1917 &s->selected_entry, s->first_displayed_entry,
1918 &s->commits, s->selected, view->nlines, s->refs,
1919 s->in_repo_path, s->thread_args.commits_needed);
1922 static const struct got_error *
1923 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1924 struct tog_view **focus_view, struct tog_view *view, int ch)
1926 const struct got_error *err = NULL;
1927 struct tog_log_view_state *s = &view->state.log;
1928 char *parent_path, *in_repo_path = NULL;
1929 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1930 int begin_x = 0;
1931 struct got_object_id *start_id;
1933 switch (ch) {
1934 case 'q':
1935 s->quit = 1;
1936 break;
1937 case 'k':
1938 case KEY_UP:
1939 case '<':
1940 case ',':
1941 if (s->first_displayed_entry == NULL)
1942 break;
1943 if (s->selected > 0)
1944 s->selected--;
1945 else
1946 scroll_up(view, &s->first_displayed_entry, 1,
1947 &s->commits);
1948 break;
1949 case KEY_PPAGE:
1950 case CTRL('b'):
1951 if (s->first_displayed_entry == NULL)
1952 break;
1953 if (TAILQ_FIRST(&s->commits.head) ==
1954 s->first_displayed_entry) {
1955 s->selected = 0;
1956 break;
1958 scroll_up(view, &s->first_displayed_entry,
1959 view->nlines, &s->commits);
1960 break;
1961 case 'j':
1962 case KEY_DOWN:
1963 case '>':
1964 case '.':
1965 if (s->first_displayed_entry == NULL)
1966 break;
1967 if (s->selected < MIN(view->nlines - 2,
1968 s->commits.ncommits - 1)) {
1969 s->selected++;
1970 break;
1972 err = scroll_down(view, &s->first_displayed_entry, 1,
1973 &s->last_displayed_entry, &s->commits,
1974 &s->thread_args.log_complete,
1975 &s->thread_args.commits_needed,
1976 &s->thread_args.need_commits);
1977 break;
1978 case KEY_NPAGE:
1979 case CTRL('f'): {
1980 struct commit_queue_entry *first;
1981 first = s->first_displayed_entry;
1982 if (first == NULL)
1983 break;
1984 err = scroll_down(view, &s->first_displayed_entry,
1985 view->nlines, &s->last_displayed_entry,
1986 &s->commits, &s->thread_args.log_complete,
1987 &s->thread_args.commits_needed,
1988 &s->thread_args.need_commits);
1989 if (first == s->first_displayed_entry &&
1990 s->selected < MIN(view->nlines - 2,
1991 s->commits.ncommits - 1)) {
1992 /* can't scroll further down */
1993 s->selected = MIN(view->nlines - 2,
1994 s->commits.ncommits - 1);
1996 err = NULL;
1997 break;
1999 case KEY_RESIZE:
2000 if (s->selected > view->nlines - 2)
2001 s->selected = view->nlines - 2;
2002 if (s->selected > s->commits.ncommits - 1)
2003 s->selected = s->commits.ncommits - 1;
2004 break;
2005 case KEY_ENTER:
2006 case ' ':
2007 case '\r':
2008 if (s->selected_entry == NULL)
2009 break;
2010 if (view_is_parent_view(view))
2011 begin_x = view_split_begin_x(view->begin_x);
2012 err = open_diff_view_for_commit(&diff_view, begin_x,
2013 s->selected_entry->commit, s->selected_entry->id,
2014 view, s->refs, s->repo);
2015 if (err)
2016 break;
2017 if (view_is_parent_view(view)) {
2018 err = view_close_child(view);
2019 if (err)
2020 return err;
2021 err = view_set_child(view, diff_view);
2022 if (err) {
2023 view_close(diff_view);
2024 break;
2026 *focus_view = diff_view;
2027 view->child_focussed = 1;
2028 } else
2029 *new_view = diff_view;
2030 break;
2031 case 't':
2032 if (s->selected_entry == NULL)
2033 break;
2034 if (view_is_parent_view(view))
2035 begin_x = view_split_begin_x(view->begin_x);
2036 err = browse_commit_tree(&tree_view, begin_x,
2037 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2038 if (err)
2039 break;
2040 if (view_is_parent_view(view)) {
2041 err = view_close_child(view);
2042 if (err)
2043 return err;
2044 err = view_set_child(view, tree_view);
2045 if (err) {
2046 view_close(tree_view);
2047 break;
2049 *focus_view = tree_view;
2050 view->child_focussed = 1;
2051 } else
2052 *new_view = tree_view;
2053 break;
2054 case KEY_BACKSPACE:
2055 if (strcmp(s->in_repo_path, "/") == 0)
2056 break;
2057 parent_path = dirname(s->in_repo_path);
2058 if (parent_path && strcmp(parent_path, ".") != 0) {
2059 err = stop_log_thread(s);
2060 if (err)
2061 return err;
2062 lv = view_open(view->nlines, view->ncols,
2063 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2064 if (lv == NULL)
2065 return got_error_from_errno(
2066 "view_open");
2067 err = open_log_view(lv, s->start_id, s->refs,
2068 s->repo, s->head_ref_name, parent_path, 0);
2069 if (err)
2070 return err;;
2071 if (view_is_parent_view(view))
2072 *new_view = lv;
2073 else {
2074 view_set_child(view->parent, lv);
2075 *focus_view = lv;
2077 return NULL;
2079 break;
2080 case CTRL('l'):
2081 err = stop_log_thread(s);
2082 if (err)
2083 return err;
2084 lv = view_open(view->nlines, view->ncols,
2085 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2086 if (lv == NULL)
2087 return got_error_from_errno("view_open");
2088 err = get_head_commit_id(&start_id, s->head_ref_name ?
2089 s->head_ref_name : GOT_REF_HEAD, s->repo);
2090 if (err)
2091 return err;
2092 in_repo_path = strdup(s->in_repo_path);
2093 if (in_repo_path == NULL) {
2094 free(start_id);
2095 return got_error_from_errno("strdup");
2097 err = open_log_view(lv, start_id, s->refs, s->repo,
2098 s->head_ref_name, in_repo_path, 0);
2099 if (err)
2100 return err;;
2101 *dead_view = view;
2102 *new_view = lv;
2103 break;
2104 default:
2105 break;
2108 return err;
2111 static const struct got_error *
2112 apply_unveil(const char *repo_path, const char *worktree_path)
2114 const struct got_error *error;
2116 #ifdef PROFILE
2117 if (unveil("gmon.out", "rwc") != 0)
2118 return got_error_from_errno2("unveil", "gmon.out");
2119 #endif
2120 if (repo_path && unveil(repo_path, "r") != 0)
2121 return got_error_from_errno2("unveil", repo_path);
2123 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2124 return got_error_from_errno2("unveil", worktree_path);
2126 if (unveil("/tmp", "rwc") != 0)
2127 return got_error_from_errno2("unveil", "/tmp");
2129 error = got_privsep_unveil_exec_helpers();
2130 if (error != NULL)
2131 return error;
2133 if (unveil(NULL, NULL) != 0)
2134 return got_error_from_errno("unveil");
2136 return NULL;
2139 static void
2140 init_curses(void)
2142 initscr();
2143 cbreak();
2144 halfdelay(1); /* Do fast refresh while initial view is loading. */
2145 noecho();
2146 nonl();
2147 intrflush(stdscr, FALSE);
2148 keypad(stdscr, TRUE);
2149 curs_set(0);
2150 signal(SIGWINCH, tog_sigwinch);
2153 static const struct got_error *
2154 cmd_log(int argc, char *argv[])
2156 const struct got_error *error;
2157 struct got_repository *repo = NULL;
2158 struct got_worktree *worktree = NULL;
2159 struct got_reflist_head refs;
2160 struct got_object_id *start_id = NULL;
2161 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2162 char *start_commit = NULL;
2163 int ch;
2164 struct tog_view *view;
2166 SIMPLEQ_INIT(&refs);
2168 #ifndef PROFILE
2169 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2170 NULL) == -1)
2171 err(1, "pledge");
2172 #endif
2174 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2175 switch (ch) {
2176 case 'c':
2177 start_commit = optarg;
2178 break;
2179 case 'r':
2180 repo_path = realpath(optarg, NULL);
2181 if (repo_path == NULL)
2182 err(1, "-r option");
2183 break;
2184 default:
2185 usage_log();
2186 /* NOTREACHED */
2190 argc -= optind;
2191 argv += optind;
2193 cwd = getcwd(NULL, 0);
2194 if (cwd == NULL) {
2195 error = got_error_from_errno("getcwd");
2196 goto done;
2198 error = got_worktree_open(&worktree, cwd);
2199 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2200 goto done;
2201 error = NULL;
2203 if (argc == 0) {
2204 path = strdup("");
2205 if (path == NULL) {
2206 error = got_error_from_errno("strdup");
2207 goto done;
2209 } else if (argc == 1) {
2210 if (worktree) {
2211 error = got_worktree_resolve_path(&path, worktree,
2212 argv[0]);
2213 if (error)
2214 goto done;
2215 } else {
2216 path = strdup(argv[0]);
2217 if (path == NULL) {
2218 error = got_error_from_errno("strdup");
2219 goto done;
2222 } else
2223 usage_log();
2225 repo_path = worktree ?
2226 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2227 if (repo_path == NULL) {
2228 error = got_error_from_errno("strdup");
2229 goto done;
2232 init_curses();
2234 error = got_repo_open(&repo, repo_path);
2235 if (error != NULL)
2236 goto done;
2238 error = apply_unveil(got_repo_get_path(repo),
2239 worktree ? got_worktree_get_root_path(worktree) : NULL);
2240 if (error)
2241 goto done;
2243 if (start_commit == NULL)
2244 error = get_head_commit_id(&start_id, worktree ?
2245 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2246 repo);
2247 else {
2248 error = get_head_commit_id(&start_id, start_commit, repo);
2249 if (error) {
2250 if (error->code != GOT_ERR_NOT_REF)
2251 goto done;
2252 error = got_repo_match_object_id_prefix(&start_id,
2253 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2256 if (error != NULL)
2257 goto done;
2259 error = got_ref_list(&refs, repo);
2260 if (error)
2261 goto done;
2263 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2264 if (view == NULL) {
2265 error = got_error_from_errno("view_open");
2266 goto done;
2268 error = open_log_view(view, start_id, &refs, repo, worktree ?
2269 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2270 if (error)
2271 goto done;
2272 error = view_loop(view);
2273 done:
2274 free(repo_path);
2275 free(cwd);
2276 free(path);
2277 free(start_id);
2278 if (repo)
2279 got_repo_close(repo);
2280 if (worktree)
2281 got_worktree_close(worktree);
2282 got_ref_list_free(&refs);
2283 return error;
2286 __dead static void
2287 usage_diff(void)
2289 endwin();
2290 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2291 getprogname());
2292 exit(1);
2295 static char *
2296 parse_next_line(FILE *f, size_t *len)
2298 char *line;
2299 size_t linelen;
2300 size_t lineno;
2301 const char delim[3] = { '\0', '\0', '\0'};
2303 line = fparseln(f, &linelen, &lineno, delim, 0);
2304 if (len)
2305 *len = linelen;
2306 return line;
2309 static const struct got_error *
2310 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2311 int *last_displayed_line, int *eof, int max_lines,
2312 char *header)
2314 const struct got_error *err;
2315 int nlines = 0, nprinted = 0;
2316 char *line;
2317 size_t len;
2318 wchar_t *wline;
2319 int width;
2321 rewind(f);
2322 werase(view->window);
2324 if (header) {
2325 err = format_line(&wline, &width, header, view->ncols);
2326 if (err) {
2327 return err;
2330 if (view_needs_focus_indication(view))
2331 wstandout(view->window);
2332 waddwstr(view->window, wline);
2333 if (view_needs_focus_indication(view))
2334 wstandend(view->window);
2335 if (width < view->ncols - 1)
2336 waddch(view->window, '\n');
2338 if (max_lines <= 1)
2339 return NULL;
2340 max_lines--;
2343 *eof = 0;
2344 while (nprinted < max_lines) {
2345 line = parse_next_line(f, &len);
2346 if (line == NULL) {
2347 *eof = 1;
2348 break;
2350 if (++nlines < *first_displayed_line) {
2351 free(line);
2352 continue;
2355 err = format_line(&wline, &width, line, view->ncols);
2356 if (err) {
2357 free(line);
2358 return err;
2360 waddwstr(view->window, wline);
2361 if (width < view->ncols - 1)
2362 waddch(view->window, '\n');
2363 if (++nprinted == 1)
2364 *first_displayed_line = nlines;
2365 free(line);
2366 free(wline);
2367 wline = NULL;
2369 *last_displayed_line = nlines;
2371 view_vborder(view);
2373 if (*eof) {
2374 while (nprinted < view->nlines) {
2375 waddch(view->window, '\n');
2376 nprinted++;
2379 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2380 if (err) {
2381 return err;
2384 wstandout(view->window);
2385 waddwstr(view->window, wline);
2386 wstandend(view->window);
2389 return NULL;
2392 static char *
2393 get_datestr(time_t *time, char *datebuf)
2395 char *p, *s = ctime_r(time, datebuf);
2396 p = strchr(s, '\n');
2397 if (p)
2398 *p = '\0';
2399 return s;
2402 static const struct got_error *
2403 write_commit_info(struct got_object_id *commit_id,
2404 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2406 const struct got_error *err = NULL;
2407 char datebuf[26];
2408 struct got_commit_object *commit;
2409 char *id_str = NULL;
2410 time_t committer_time;
2411 const char *author, *committer;
2412 char *refs_str = NULL;
2414 if (refs) {
2415 err = build_refs_str(&refs_str, refs, commit_id);
2416 if (err)
2417 return err;
2420 err = got_object_open_as_commit(&commit, repo, commit_id);
2421 if (err)
2422 return err;
2424 err = got_object_id_str(&id_str, commit_id);
2425 if (err) {
2426 err = got_error_from_errno("got_object_id_str");
2427 goto done;
2430 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2431 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2432 err = got_error_from_errno("fprintf");
2433 goto done;
2435 if (fprintf(outfile, "from: %s\n",
2436 got_object_commit_get_author(commit)) < 0) {
2437 err = got_error_from_errno("fprintf");
2438 goto done;
2440 committer_time = got_object_commit_get_committer_time(commit);
2441 if (fprintf(outfile, "date: %s UTC\n",
2442 get_datestr(&committer_time, datebuf)) < 0) {
2443 err = got_error_from_errno("fprintf");
2444 goto done;
2446 author = got_object_commit_get_author(commit);
2447 committer = got_object_commit_get_committer(commit);
2448 if (strcmp(author, committer) != 0 &&
2449 fprintf(outfile, "via: %s\n", committer) < 0) {
2450 err = got_error_from_errno("fprintf");
2451 goto done;
2453 if (fprintf(outfile, "%s\n",
2454 got_object_commit_get_logmsg(commit)) < 0) {
2455 err = got_error_from_errno("fprintf");
2456 goto done;
2458 done:
2459 free(id_str);
2460 free(refs_str);
2461 got_object_commit_close(commit);
2462 return err;
2465 static const struct got_error *
2466 create_diff(struct tog_diff_view_state *s)
2468 const struct got_error *err = NULL;
2469 FILE *f = NULL;
2470 int obj_type;
2472 f = got_opentemp();
2473 if (f == NULL) {
2474 err = got_error_from_errno("got_opentemp");
2475 goto done;
2477 if (s->f && fclose(s->f) != 0) {
2478 err = got_error_from_errno("fclose");
2479 goto done;
2481 s->f = f;
2483 if (s->id1)
2484 err = got_object_get_type(&obj_type, s->repo, s->id1);
2485 else
2486 err = got_object_get_type(&obj_type, s->repo, s->id2);
2487 if (err)
2488 goto done;
2490 switch (obj_type) {
2491 case GOT_OBJ_TYPE_BLOB:
2492 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2493 s->diff_context, s->repo, f);
2494 break;
2495 case GOT_OBJ_TYPE_TREE:
2496 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2497 s->diff_context, s->repo, f);
2498 break;
2499 case GOT_OBJ_TYPE_COMMIT: {
2500 const struct got_object_id_queue *parent_ids;
2501 struct got_object_qid *pid;
2502 struct got_commit_object *commit2;
2504 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2505 if (err)
2506 break;
2507 /* Show commit info if we're diffing to a parent/root commit. */
2508 if (s->id1 == NULL)
2509 write_commit_info(s->id2, s->refs, s->repo, f);
2510 else {
2511 parent_ids = got_object_commit_get_parent_ids(commit2);
2512 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2513 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2514 write_commit_info(s->id2, s->refs,
2515 s->repo, f);
2516 break;
2520 got_object_commit_close(commit2);
2522 err = got_diff_objects_as_commits(s->id1, s->id2,
2523 s->diff_context, s->repo, f);
2524 break;
2526 default:
2527 err = got_error(GOT_ERR_OBJ_TYPE);
2528 break;
2530 done:
2531 if (f && fflush(f) != 0 && err == NULL)
2532 err = got_error_from_errno("fflush");
2533 return err;
2536 static void
2537 diff_view_indicate_progress(struct tog_view *view)
2539 mvwaddstr(view->window, 0, 0, "diffing...");
2540 update_panels();
2541 doupdate();
2544 static const struct got_error *
2545 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2546 struct got_object_id *id2, struct tog_view *log_view,
2547 struct got_reflist_head *refs, struct got_repository *repo)
2549 const struct got_error *err;
2551 if (id1 != NULL && id2 != NULL) {
2552 int type1, type2;
2553 err = got_object_get_type(&type1, repo, id1);
2554 if (err)
2555 return err;
2556 err = got_object_get_type(&type2, repo, id2);
2557 if (err)
2558 return err;
2560 if (type1 != type2)
2561 return got_error(GOT_ERR_OBJ_TYPE);
2564 if (id1) {
2565 view->state.diff.id1 = got_object_id_dup(id1);
2566 if (view->state.diff.id1 == NULL)
2567 return got_error_from_errno("got_object_id_dup");
2568 } else
2569 view->state.diff.id1 = NULL;
2571 view->state.diff.id2 = got_object_id_dup(id2);
2572 if (view->state.diff.id2 == NULL) {
2573 free(view->state.diff.id1);
2574 view->state.diff.id1 = NULL;
2575 return got_error_from_errno("got_object_id_dup");
2577 view->state.diff.f = NULL;
2578 view->state.diff.first_displayed_line = 1;
2579 view->state.diff.last_displayed_line = view->nlines;
2580 view->state.diff.diff_context = 3;
2581 view->state.diff.log_view = log_view;
2582 view->state.diff.repo = repo;
2583 view->state.diff.refs = refs;
2585 if (log_view && view_is_splitscreen(view))
2586 show_log_view(log_view); /* draw vborder */
2587 diff_view_indicate_progress(view);
2589 err = create_diff(&view->state.diff);
2590 if (err) {
2591 free(view->state.diff.id1);
2592 view->state.diff.id1 = NULL;
2593 free(view->state.diff.id2);
2594 view->state.diff.id2 = NULL;
2595 return err;
2598 view->show = show_diff_view;
2599 view->input = input_diff_view;
2600 view->close = close_diff_view;
2602 return NULL;
2605 static const struct got_error *
2606 close_diff_view(struct tog_view *view)
2608 const struct got_error *err = NULL;
2610 free(view->state.diff.id1);
2611 view->state.diff.id1 = NULL;
2612 free(view->state.diff.id2);
2613 view->state.diff.id2 = NULL;
2614 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2615 err = got_error_from_errno("fclose");
2616 return err;
2619 static const struct got_error *
2620 show_diff_view(struct tog_view *view)
2622 const struct got_error *err;
2623 struct tog_diff_view_state *s = &view->state.diff;
2624 char *id_str1 = NULL, *id_str2, *header;
2626 if (s->id1) {
2627 err = got_object_id_str(&id_str1, s->id1);
2628 if (err)
2629 return err;
2631 err = got_object_id_str(&id_str2, s->id2);
2632 if (err)
2633 return err;
2635 if (asprintf(&header, "diff %s %s",
2636 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2637 err = got_error_from_errno("asprintf");
2638 free(id_str1);
2639 free(id_str2);
2640 return err;
2642 free(id_str1);
2643 free(id_str2);
2645 return draw_file(view, s->f, &s->first_displayed_line,
2646 &s->last_displayed_line, &s->eof, view->nlines,
2647 header);
2650 static const struct got_error *
2651 set_selected_commit(struct tog_diff_view_state *s,
2652 struct commit_queue_entry *entry)
2654 const struct got_error *err;
2655 const struct got_object_id_queue *parent_ids;
2656 struct got_commit_object *selected_commit;
2657 struct got_object_qid *pid;
2659 free(s->id2);
2660 s->id2 = got_object_id_dup(entry->id);
2661 if (s->id2 == NULL)
2662 return got_error_from_errno("got_object_id_dup");
2664 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2665 if (err)
2666 return err;
2667 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2668 free(s->id1);
2669 pid = SIMPLEQ_FIRST(parent_ids);
2670 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2671 got_object_commit_close(selected_commit);
2672 return NULL;
2675 static const struct got_error *
2676 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2677 struct tog_view **focus_view, struct tog_view *view, int ch)
2679 const struct got_error *err = NULL;
2680 struct tog_diff_view_state *s = &view->state.diff;
2681 struct tog_log_view_state *ls;
2682 struct commit_queue_entry *entry;
2683 int i;
2685 switch (ch) {
2686 case 'k':
2687 case KEY_UP:
2688 if (s->first_displayed_line > 1)
2689 s->first_displayed_line--;
2690 break;
2691 case KEY_PPAGE:
2692 case CTRL('b'):
2693 if (s->first_displayed_line == 1)
2694 break;
2695 i = 0;
2696 while (i++ < view->nlines - 1 &&
2697 s->first_displayed_line > 1)
2698 s->first_displayed_line--;
2699 break;
2700 case 'j':
2701 case KEY_DOWN:
2702 if (!s->eof)
2703 s->first_displayed_line++;
2704 break;
2705 case KEY_NPAGE:
2706 case CTRL('f'):
2707 case ' ':
2708 if (s->eof)
2709 break;
2710 i = 0;
2711 while (!s->eof && i++ < view->nlines - 1) {
2712 char *line;
2713 line = parse_next_line(s->f, NULL);
2714 s->first_displayed_line++;
2715 if (line == NULL)
2716 break;
2718 break;
2719 case '[':
2720 if (s->diff_context > 0) {
2721 s->diff_context--;
2722 diff_view_indicate_progress(view);
2723 err = create_diff(s);
2725 break;
2726 case ']':
2727 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2728 s->diff_context++;
2729 diff_view_indicate_progress(view);
2730 err = create_diff(s);
2732 break;
2733 case '<':
2734 case ',':
2735 if (s->log_view == NULL)
2736 break;
2737 ls = &s->log_view->state.log;
2738 entry = TAILQ_PREV(ls->selected_entry,
2739 commit_queue_head, entry);
2740 if (entry == NULL)
2741 break;
2743 err = input_log_view(NULL, NULL, NULL, s->log_view,
2744 KEY_UP);
2745 if (err)
2746 break;
2748 err = set_selected_commit(s, entry);
2749 if (err)
2750 break;
2752 s->first_displayed_line = 1;
2753 s->last_displayed_line = view->nlines;
2755 diff_view_indicate_progress(view);
2756 err = create_diff(s);
2757 break;
2758 case '>':
2759 case '.':
2760 if (s->log_view == NULL)
2761 break;
2762 ls = &s->log_view->state.log;
2764 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2765 ls->thread_args.commits_needed++;
2767 /* Display "loading..." in log view. */
2768 show_log_view(s->log_view);
2769 update_panels();
2770 doupdate();
2772 err = trigger_log_thread(1 /* load_all */,
2773 &ls->thread_args.commits_needed,
2774 &ls->thread_args.log_complete,
2775 &ls->thread_args.need_commits);
2776 if (err)
2777 break;
2779 err = input_log_view(NULL, NULL, NULL, s->log_view,
2780 KEY_DOWN);
2781 if (err)
2782 break;
2784 entry = TAILQ_NEXT(ls->selected_entry, entry);
2785 if (entry == NULL)
2786 break;
2788 err = set_selected_commit(s, entry);
2789 if (err)
2790 break;
2792 s->first_displayed_line = 1;
2793 s->last_displayed_line = view->nlines;
2795 diff_view_indicate_progress(view);
2796 err = create_diff(s);
2797 break;
2798 default:
2799 break;
2802 return err;
2805 static const struct got_error *
2806 cmd_diff(int argc, char *argv[])
2808 const struct got_error *error = NULL;
2809 struct got_repository *repo = NULL;
2810 struct got_reflist_head refs;
2811 struct got_object_id *id1 = NULL, *id2 = NULL;
2812 char *repo_path = NULL;
2813 char *id_str1 = NULL, *id_str2 = NULL;
2814 int ch;
2815 struct tog_view *view;
2817 SIMPLEQ_INIT(&refs);
2819 #ifndef PROFILE
2820 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2821 NULL) == -1)
2822 err(1, "pledge");
2823 #endif
2825 while ((ch = getopt(argc, argv, "")) != -1) {
2826 switch (ch) {
2827 default:
2828 usage_diff();
2829 /* NOTREACHED */
2833 argc -= optind;
2834 argv += optind;
2836 if (argc == 0) {
2837 usage_diff(); /* TODO show local worktree changes */
2838 } else if (argc == 2) {
2839 repo_path = getcwd(NULL, 0);
2840 if (repo_path == NULL)
2841 return got_error_from_errno("getcwd");
2842 id_str1 = argv[0];
2843 id_str2 = argv[1];
2844 } else if (argc == 3) {
2845 repo_path = realpath(argv[0], NULL);
2846 if (repo_path == NULL)
2847 return got_error_from_errno2("realpath", argv[0]);
2848 id_str1 = argv[1];
2849 id_str2 = argv[2];
2850 } else
2851 usage_diff();
2853 init_curses();
2855 error = got_repo_open(&repo, repo_path);
2856 if (error)
2857 goto done;
2859 error = apply_unveil(got_repo_get_path(repo), NULL);
2860 if (error)
2861 goto done;
2863 error = got_repo_match_object_id_prefix(&id1, id_str1,
2864 GOT_OBJ_TYPE_ANY, repo);
2865 if (error)
2866 goto done;
2868 error = got_repo_match_object_id_prefix(&id2, id_str2,
2869 GOT_OBJ_TYPE_ANY, repo);
2870 if (error)
2871 goto done;
2873 error = got_ref_list(&refs, repo);
2874 if (error)
2875 goto done;
2877 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2878 if (view == NULL) {
2879 error = got_error_from_errno("view_open");
2880 goto done;
2882 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2883 if (error)
2884 goto done;
2885 error = view_loop(view);
2886 done:
2887 free(repo_path);
2888 if (repo)
2889 got_repo_close(repo);
2890 got_ref_list_free(&refs);
2891 return error;
2894 __dead static void
2895 usage_blame(void)
2897 endwin();
2898 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2899 getprogname());
2900 exit(1);
2903 struct tog_blame_line {
2904 int annotated;
2905 struct got_object_id *id;
2908 static const struct got_error *
2909 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2910 const char *path, struct tog_blame_line *lines, int nlines,
2911 int blame_complete, int selected_line, int *first_displayed_line,
2912 int *last_displayed_line, int *eof, int max_lines)
2914 const struct got_error *err;
2915 int lineno = 0, nprinted = 0;
2916 char *line;
2917 size_t len;
2918 wchar_t *wline;
2919 int width, wlimit;
2920 struct tog_blame_line *blame_line;
2921 struct got_object_id *prev_id = NULL;
2922 char *id_str;
2924 err = got_object_id_str(&id_str, id);
2925 if (err)
2926 return err;
2928 rewind(f);
2929 werase(view->window);
2931 if (asprintf(&line, "commit %s", id_str) == -1) {
2932 err = got_error_from_errno("asprintf");
2933 free(id_str);
2934 return err;
2937 err = format_line(&wline, &width, line, view->ncols);
2938 free(line);
2939 line = NULL;
2940 if (view_needs_focus_indication(view))
2941 wstandout(view->window);
2942 waddwstr(view->window, wline);
2943 if (view_needs_focus_indication(view))
2944 wstandend(view->window);
2945 free(wline);
2946 wline = NULL;
2947 if (width < view->ncols - 1)
2948 waddch(view->window, '\n');
2950 if (asprintf(&line, "[%d/%d] %s%s",
2951 *first_displayed_line - 1 + selected_line, nlines,
2952 blame_complete ? "" : "annotating... ", path) == -1) {
2953 free(id_str);
2954 return got_error_from_errno("asprintf");
2956 free(id_str);
2957 err = format_line(&wline, &width, line, view->ncols);
2958 free(line);
2959 line = NULL;
2960 if (err)
2961 return err;
2962 waddwstr(view->window, wline);
2963 free(wline);
2964 wline = NULL;
2965 if (width < view->ncols - 1)
2966 waddch(view->window, '\n');
2968 *eof = 0;
2969 while (nprinted < max_lines - 2) {
2970 line = parse_next_line(f, &len);
2971 if (line == NULL) {
2972 *eof = 1;
2973 break;
2975 if (++lineno < *first_displayed_line) {
2976 free(line);
2977 continue;
2980 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2981 err = format_line(&wline, &width, line, wlimit);
2982 if (err) {
2983 free(line);
2984 return err;
2987 if (view->focussed && nprinted == selected_line - 1)
2988 wstandout(view->window);
2990 blame_line = &lines[lineno - 1];
2991 if (blame_line->annotated && prev_id &&
2992 got_object_id_cmp(prev_id, blame_line->id) == 0)
2993 waddstr(view->window, " ");
2994 else if (blame_line->annotated) {
2995 char *id_str;
2996 err = got_object_id_str(&id_str, blame_line->id);
2997 if (err) {
2998 free(line);
2999 free(wline);
3000 return err;
3002 wprintw(view->window, "%.8s ", id_str);
3003 free(id_str);
3004 prev_id = blame_line->id;
3005 } else {
3006 waddstr(view->window, "........ ");
3007 prev_id = NULL;
3010 waddwstr(view->window, wline);
3011 while (width < wlimit) {
3012 waddch(view->window, ' ');
3013 width++;
3015 if (view->focussed && nprinted == selected_line - 1)
3016 wstandend(view->window);
3017 if (++nprinted == 1)
3018 *first_displayed_line = lineno;
3019 free(line);
3020 free(wline);
3021 wline = NULL;
3023 *last_displayed_line = lineno;
3025 view_vborder(view);
3027 return NULL;
3030 static const struct got_error *
3031 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3033 const struct got_error *err = NULL;
3034 struct tog_blame_cb_args *a = arg;
3035 struct tog_blame_line *line;
3036 int errcode;
3038 if (nlines != a->nlines ||
3039 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3040 return got_error(GOT_ERR_RANGE);
3042 errcode = pthread_mutex_lock(&tog_mutex);
3043 if (errcode)
3044 return got_error_set_errno(errcode, "pthread_mutex_lock");
3046 if (*a->quit) { /* user has quit the blame view */
3047 err = got_error(GOT_ERR_ITER_COMPLETED);
3048 goto done;
3051 if (lineno == -1)
3052 goto done; /* no change in this commit */
3054 line = &a->lines[lineno - 1];
3055 if (line->annotated)
3056 goto done;
3058 line->id = got_object_id_dup(id);
3059 if (line->id == NULL) {
3060 err = got_error_from_errno("got_object_id_dup");
3061 goto done;
3063 line->annotated = 1;
3064 done:
3065 errcode = pthread_mutex_unlock(&tog_mutex);
3066 if (errcode)
3067 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3068 return err;
3071 static void *
3072 blame_thread(void *arg)
3074 const struct got_error *err;
3075 struct tog_blame_thread_args *ta = arg;
3076 struct tog_blame_cb_args *a = ta->cb_args;
3077 int errcode;
3079 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3080 blame_cb, ta->cb_args);
3082 errcode = pthread_mutex_lock(&tog_mutex);
3083 if (errcode)
3084 return (void *)got_error_set_errno(errcode,
3085 "pthread_mutex_lock");
3087 got_repo_close(ta->repo);
3088 ta->repo = NULL;
3089 *ta->complete = 1;
3091 errcode = pthread_mutex_unlock(&tog_mutex);
3092 if (errcode && err == NULL)
3093 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3095 return (void *)err;
3098 static struct got_object_id *
3099 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3100 int selected_line)
3102 struct tog_blame_line *line;
3104 line = &lines[first_displayed_line - 1 + selected_line - 1];
3105 if (!line->annotated)
3106 return NULL;
3108 return line->id;
3111 static const struct got_error *
3112 stop_blame(struct tog_blame *blame)
3114 const struct got_error *err = NULL;
3115 int i;
3117 if (blame->thread) {
3118 int errcode;
3119 errcode = pthread_mutex_unlock(&tog_mutex);
3120 if (errcode)
3121 return got_error_set_errno(errcode,
3122 "pthread_mutex_unlock");
3123 errcode = pthread_join(blame->thread, (void **)&err);
3124 if (errcode)
3125 return got_error_set_errno(errcode, "pthread_join");
3126 errcode = pthread_mutex_lock(&tog_mutex);
3127 if (errcode)
3128 return got_error_set_errno(errcode,
3129 "pthread_mutex_lock");
3130 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3131 err = NULL;
3132 blame->thread = NULL;
3134 if (blame->thread_args.repo) {
3135 got_repo_close(blame->thread_args.repo);
3136 blame->thread_args.repo = NULL;
3138 if (blame->f) {
3139 if (fclose(blame->f) != 0 && err == NULL)
3140 err = got_error_from_errno("fclose");
3141 blame->f = NULL;
3143 if (blame->lines) {
3144 for (i = 0; i < blame->nlines; i++)
3145 free(blame->lines[i].id);
3146 free(blame->lines);
3147 blame->lines = NULL;
3149 free(blame->cb_args.commit_id);
3150 blame->cb_args.commit_id = NULL;
3152 return err;
3155 static const struct got_error *
3156 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3157 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3158 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3159 struct got_repository *repo)
3161 const struct got_error *err = NULL;
3162 struct got_blob_object *blob = NULL;
3163 struct got_repository *thread_repo = NULL;
3164 struct got_object_id *obj_id = NULL;
3165 int obj_type;
3167 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3168 if (err)
3169 return err;
3170 if (obj_id == NULL)
3171 return got_error(GOT_ERR_NO_OBJ);
3173 err = got_object_get_type(&obj_type, repo, obj_id);
3174 if (err)
3175 goto done;
3177 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3178 err = got_error(GOT_ERR_OBJ_TYPE);
3179 goto done;
3182 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3183 if (err)
3184 goto done;
3185 blame->f = got_opentemp();
3186 if (blame->f == NULL) {
3187 err = got_error_from_errno("got_opentemp");
3188 goto done;
3190 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3191 &blame->line_offsets, blame->f, blob);
3192 if (err)
3193 goto done;
3195 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3196 if (blame->lines == NULL) {
3197 err = got_error_from_errno("calloc");
3198 goto done;
3201 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3202 if (err)
3203 goto done;
3205 blame->cb_args.view = view;
3206 blame->cb_args.lines = blame->lines;
3207 blame->cb_args.nlines = blame->nlines;
3208 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3209 if (blame->cb_args.commit_id == NULL) {
3210 err = got_error_from_errno("got_object_id_dup");
3211 goto done;
3213 blame->cb_args.quit = done;
3215 blame->thread_args.path = path;
3216 blame->thread_args.repo = thread_repo;
3217 blame->thread_args.cb_args = &blame->cb_args;
3218 blame->thread_args.complete = blame_complete;
3219 *blame_complete = 0;
3221 done:
3222 if (blob)
3223 got_object_blob_close(blob);
3224 free(obj_id);
3225 if (err)
3226 stop_blame(blame);
3227 return err;
3230 static const struct got_error *
3231 open_blame_view(struct tog_view *view, char *path,
3232 struct got_object_id *commit_id, struct got_reflist_head *refs,
3233 struct got_repository *repo)
3235 const struct got_error *err = NULL;
3236 struct tog_blame_view_state *s = &view->state.blame;
3238 SIMPLEQ_INIT(&s->blamed_commits);
3240 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3241 if (err)
3242 return err;
3244 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3245 s->first_displayed_line = 1;
3246 s->last_displayed_line = view->nlines;
3247 s->selected_line = 1;
3248 s->blame_complete = 0;
3249 s->path = path;
3250 if (s->path == NULL)
3251 return got_error_from_errno("open_blame_view");
3252 s->repo = repo;
3253 s->refs = refs;
3254 s->commit_id = commit_id;
3255 memset(&s->blame, 0, sizeof(s->blame));
3257 view->show = show_blame_view;
3258 view->input = input_blame_view;
3259 view->close = close_blame_view;
3260 view->search_start = search_start_blame_view;
3261 view->search_next = search_next_blame_view;
3263 return run_blame(&s->blame, view, &s->blame_complete,
3264 &s->first_displayed_line, &s->last_displayed_line,
3265 &s->selected_line, &s->done, &s->eof, s->path,
3266 s->blamed_commit->id, s->repo);
3269 static const struct got_error *
3270 close_blame_view(struct tog_view *view)
3272 const struct got_error *err = NULL;
3273 struct tog_blame_view_state *s = &view->state.blame;
3275 if (s->blame.thread)
3276 err = stop_blame(&s->blame);
3278 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3279 struct got_object_qid *blamed_commit;
3280 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3281 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3282 got_object_qid_free(blamed_commit);
3285 free(s->path);
3287 return err;
3290 static const struct got_error *
3291 search_start_blame_view(struct tog_view *view)
3293 struct tog_blame_view_state *s = &view->state.blame;
3295 s->matched_line = 0;
3296 return NULL;
3299 static int
3300 match_line(const char *line, regex_t *regex)
3302 regmatch_t regmatch;
3304 return regexec(regex, line, 1, &regmatch, 0) == 0;
3308 static const struct got_error *
3309 search_next_blame_view(struct tog_view *view)
3311 struct tog_blame_view_state *s = &view->state.blame;
3312 int lineno;
3314 if (!view->searching) {
3315 view->search_next_done = 1;
3316 return NULL;
3319 if (s->matched_line) {
3320 if (view->searching == TOG_SEARCH_FORWARD)
3321 lineno = s->matched_line + 1;
3322 else
3323 lineno = s->matched_line - 1;
3324 } else {
3325 if (view->searching == TOG_SEARCH_FORWARD)
3326 lineno = 1;
3327 else
3328 lineno = s->blame.nlines;
3331 while (1) {
3332 char *line = NULL;
3333 off_t offset;
3334 size_t len;
3336 if (lineno <= 0 || lineno > s->blame.nlines) {
3337 if (s->matched_line == 0) {
3338 view->search_next_done = 1;
3339 free(line);
3340 break;
3343 if (view->searching == TOG_SEARCH_FORWARD)
3344 lineno = 1;
3345 else
3346 lineno = s->blame.nlines;
3349 offset = s->blame.line_offsets[lineno - 1];
3350 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3351 free(line);
3352 return got_error_from_errno("fseeko");
3354 free(line);
3355 line = parse_next_line(s->blame.f, &len);
3356 if (line && match_line(line, &view->regex)) {
3357 view->search_next_done = 1;
3358 s->matched_line = lineno;
3359 free(line);
3360 break;
3362 free(line);
3363 if (view->searching == TOG_SEARCH_FORWARD)
3364 lineno++;
3365 else
3366 lineno--;
3369 if (s->matched_line) {
3370 s->first_displayed_line = s->matched_line;
3371 s->selected_line = 1;
3374 return NULL;
3377 static const struct got_error *
3378 show_blame_view(struct tog_view *view)
3380 const struct got_error *err = NULL;
3381 struct tog_blame_view_state *s = &view->state.blame;
3382 int errcode;
3384 if (s->blame.thread == NULL) {
3385 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3386 &s->blame.thread_args);
3387 if (errcode)
3388 return got_error_set_errno(errcode, "pthread_create");
3391 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3392 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3393 s->selected_line, &s->first_displayed_line,
3394 &s->last_displayed_line, &s->eof, view->nlines);
3396 view_vborder(view);
3397 return err;
3400 static const struct got_error *
3401 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3402 struct tog_view **focus_view, struct tog_view *view, int ch)
3404 const struct got_error *err = NULL, *thread_err = NULL;
3405 struct tog_view *diff_view;
3406 struct tog_blame_view_state *s = &view->state.blame;
3407 int begin_x = 0;
3409 switch (ch) {
3410 case 'q':
3411 s->done = 1;
3412 break;
3413 case 'k':
3414 case KEY_UP:
3415 if (s->selected_line > 1)
3416 s->selected_line--;
3417 else if (s->selected_line == 1 &&
3418 s->first_displayed_line > 1)
3419 s->first_displayed_line--;
3420 break;
3421 case KEY_PPAGE:
3422 if (s->first_displayed_line == 1) {
3423 s->selected_line = 1;
3424 break;
3426 if (s->first_displayed_line > view->nlines - 2)
3427 s->first_displayed_line -=
3428 (view->nlines - 2);
3429 else
3430 s->first_displayed_line = 1;
3431 break;
3432 case 'j':
3433 case KEY_DOWN:
3434 if (s->selected_line < view->nlines - 2 &&
3435 s->first_displayed_line +
3436 s->selected_line <= s->blame.nlines)
3437 s->selected_line++;
3438 else if (s->last_displayed_line <
3439 s->blame.nlines)
3440 s->first_displayed_line++;
3441 break;
3442 case 'b':
3443 case 'p': {
3444 struct got_object_id *id = NULL;
3445 id = get_selected_commit_id(s->blame.lines,
3446 s->first_displayed_line, s->selected_line);
3447 if (id == NULL)
3448 break;
3449 if (ch == 'p') {
3450 struct got_commit_object *commit;
3451 struct got_object_qid *pid;
3452 struct got_object_id *blob_id = NULL;
3453 int obj_type;
3454 err = got_object_open_as_commit(&commit,
3455 s->repo, id);
3456 if (err)
3457 break;
3458 pid = SIMPLEQ_FIRST(
3459 got_object_commit_get_parent_ids(commit));
3460 if (pid == NULL) {
3461 got_object_commit_close(commit);
3462 break;
3464 /* Check if path history ends here. */
3465 err = got_object_id_by_path(&blob_id, s->repo,
3466 pid->id, s->path);
3467 if (err) {
3468 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3469 err = NULL;
3470 got_object_commit_close(commit);
3471 break;
3473 err = got_object_get_type(&obj_type, s->repo,
3474 blob_id);
3475 free(blob_id);
3476 /* Can't blame non-blob type objects. */
3477 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3478 got_object_commit_close(commit);
3479 break;
3481 err = got_object_qid_alloc(&s->blamed_commit,
3482 pid->id);
3483 got_object_commit_close(commit);
3484 } else {
3485 if (got_object_id_cmp(id,
3486 s->blamed_commit->id) == 0)
3487 break;
3488 err = got_object_qid_alloc(&s->blamed_commit,
3489 id);
3491 if (err)
3492 break;
3493 s->done = 1;
3494 thread_err = stop_blame(&s->blame);
3495 s->done = 0;
3496 if (thread_err)
3497 break;
3498 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3499 s->blamed_commit, entry);
3500 err = run_blame(&s->blame, view, &s->blame_complete,
3501 &s->first_displayed_line, &s->last_displayed_line,
3502 &s->selected_line, &s->done, &s->eof,
3503 s->path, s->blamed_commit->id, s->repo);
3504 if (err)
3505 break;
3506 break;
3508 case 'B': {
3509 struct got_object_qid *first;
3510 first = SIMPLEQ_FIRST(&s->blamed_commits);
3511 if (!got_object_id_cmp(first->id, s->commit_id))
3512 break;
3513 s->done = 1;
3514 thread_err = stop_blame(&s->blame);
3515 s->done = 0;
3516 if (thread_err)
3517 break;
3518 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3519 got_object_qid_free(s->blamed_commit);
3520 s->blamed_commit =
3521 SIMPLEQ_FIRST(&s->blamed_commits);
3522 err = run_blame(&s->blame, view, &s->blame_complete,
3523 &s->first_displayed_line, &s->last_displayed_line,
3524 &s->selected_line, &s->done, &s->eof, s->path,
3525 s->blamed_commit->id, s->repo);
3526 if (err)
3527 break;
3528 break;
3530 case KEY_ENTER:
3531 case '\r': {
3532 struct got_object_id *id = NULL;
3533 struct got_object_qid *pid;
3534 struct got_commit_object *commit = NULL;
3535 id = get_selected_commit_id(s->blame.lines,
3536 s->first_displayed_line, s->selected_line);
3537 if (id == NULL)
3538 break;
3539 err = got_object_open_as_commit(&commit, s->repo, id);
3540 if (err)
3541 break;
3542 pid = SIMPLEQ_FIRST(
3543 got_object_commit_get_parent_ids(commit));
3544 if (view_is_parent_view(view))
3545 begin_x = view_split_begin_x(view->begin_x);
3546 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3547 if (diff_view == NULL) {
3548 got_object_commit_close(commit);
3549 err = got_error_from_errno("view_open");
3550 break;
3552 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3553 id, NULL, s->refs, s->repo);
3554 got_object_commit_close(commit);
3555 if (err) {
3556 view_close(diff_view);
3557 break;
3559 if (view_is_parent_view(view)) {
3560 err = view_close_child(view);
3561 if (err)
3562 break;
3563 err = view_set_child(view, diff_view);
3564 if (err) {
3565 view_close(diff_view);
3566 break;
3568 *focus_view = diff_view;
3569 view->child_focussed = 1;
3570 } else
3571 *new_view = diff_view;
3572 if (err)
3573 break;
3574 break;
3576 case KEY_NPAGE:
3577 case ' ':
3578 if (s->last_displayed_line >= s->blame.nlines &&
3579 s->selected_line >= MIN(s->blame.nlines,
3580 view->nlines - 2)) {
3581 break;
3583 if (s->last_displayed_line >= s->blame.nlines &&
3584 s->selected_line < view->nlines - 2) {
3585 s->selected_line = MIN(s->blame.nlines,
3586 view->nlines - 2);
3587 break;
3589 if (s->last_displayed_line + view->nlines - 2
3590 <= s->blame.nlines)
3591 s->first_displayed_line +=
3592 view->nlines - 2;
3593 else
3594 s->first_displayed_line =
3595 s->blame.nlines -
3596 (view->nlines - 3);
3597 break;
3598 case KEY_RESIZE:
3599 if (s->selected_line > view->nlines - 2) {
3600 s->selected_line = MIN(s->blame.nlines,
3601 view->nlines - 2);
3603 break;
3604 default:
3605 break;
3607 return thread_err ? thread_err : err;
3610 static const struct got_error *
3611 cmd_blame(int argc, char *argv[])
3613 const struct got_error *error;
3614 struct got_repository *repo = NULL;
3615 struct got_reflist_head refs;
3616 struct got_worktree *worktree = NULL;
3617 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3618 struct got_object_id *commit_id = NULL;
3619 char *commit_id_str = NULL;
3620 int ch;
3621 struct tog_view *view;
3623 SIMPLEQ_INIT(&refs);
3625 #ifndef PROFILE
3626 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3627 NULL) == -1)
3628 err(1, "pledge");
3629 #endif
3631 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3632 switch (ch) {
3633 case 'c':
3634 commit_id_str = optarg;
3635 break;
3636 case 'r':
3637 repo_path = realpath(optarg, NULL);
3638 if (repo_path == NULL)
3639 err(1, "-r option");
3640 break;
3641 default:
3642 usage_blame();
3643 /* NOTREACHED */
3647 argc -= optind;
3648 argv += optind;
3650 if (argc == 1)
3651 path = argv[0];
3652 else
3653 usage_blame();
3655 cwd = getcwd(NULL, 0);
3656 if (cwd == NULL) {
3657 error = got_error_from_errno("getcwd");
3658 goto done;
3660 if (repo_path == NULL) {
3661 error = got_worktree_open(&worktree, cwd);
3662 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3663 goto done;
3664 else
3665 error = NULL;
3666 if (worktree) {
3667 repo_path =
3668 strdup(got_worktree_get_repo_path(worktree));
3669 if (repo_path == NULL)
3670 error = got_error_from_errno("strdup");
3671 if (error)
3672 goto done;
3673 } else {
3674 repo_path = strdup(cwd);
3675 if (repo_path == NULL) {
3676 error = got_error_from_errno("strdup");
3677 goto done;
3682 init_curses();
3684 error = got_repo_open(&repo, repo_path);
3685 if (error != NULL)
3686 goto done;
3688 error = apply_unveil(got_repo_get_path(repo), NULL);
3689 if (error)
3690 goto done;
3692 if (worktree) {
3693 const char *prefix = got_worktree_get_path_prefix(worktree);
3694 char *p, *worktree_subdir = cwd +
3695 strlen(got_worktree_get_root_path(worktree));
3696 if (asprintf(&p, "%s%s%s%s%s",
3697 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3698 worktree_subdir, worktree_subdir[0] ? "/" : "",
3699 path) == -1) {
3700 error = got_error_from_errno("asprintf");
3701 goto done;
3703 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3704 free(p);
3705 } else {
3706 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3708 if (error)
3709 goto done;
3711 if (commit_id_str == NULL) {
3712 struct got_reference *head_ref;
3713 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3714 if (error != NULL)
3715 goto done;
3716 error = got_ref_resolve(&commit_id, repo, head_ref);
3717 got_ref_close(head_ref);
3718 } else {
3719 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3720 if (error) {
3721 if (error->code != GOT_ERR_NOT_REF)
3722 goto done;
3723 error = got_repo_match_object_id_prefix(&commit_id,
3724 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3727 if (error != NULL)
3728 goto done;
3730 error = got_ref_list(&refs, repo);
3731 if (error)
3732 goto done;
3734 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3735 if (view == NULL) {
3736 error = got_error_from_errno("view_open");
3737 goto done;
3739 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3740 if (error)
3741 goto done;
3742 error = view_loop(view);
3743 done:
3744 free(repo_path);
3745 free(cwd);
3746 free(commit_id);
3747 if (worktree)
3748 got_worktree_close(worktree);
3749 if (repo)
3750 got_repo_close(repo);
3751 got_ref_list_free(&refs);
3752 return error;
3755 static const struct got_error *
3756 draw_tree_entries(struct tog_view *view,
3757 struct got_tree_entry **first_displayed_entry,
3758 struct got_tree_entry **last_displayed_entry,
3759 struct got_tree_entry **selected_entry, int *ndisplayed,
3760 const char *label, int show_ids, const char *parent_path,
3761 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3763 const struct got_error *err = NULL;
3764 struct got_tree_entry *te;
3765 wchar_t *wline;
3766 int width, n;
3768 *ndisplayed = 0;
3770 werase(view->window);
3772 if (limit == 0)
3773 return NULL;
3775 err = format_line(&wline, &width, label, view->ncols);
3776 if (err)
3777 return err;
3778 if (view_needs_focus_indication(view))
3779 wstandout(view->window);
3780 waddwstr(view->window, wline);
3781 if (view_needs_focus_indication(view))
3782 wstandend(view->window);
3783 free(wline);
3784 wline = NULL;
3785 if (width < view->ncols - 1)
3786 waddch(view->window, '\n');
3787 if (--limit <= 0)
3788 return NULL;
3789 err = format_line(&wline, &width, parent_path, view->ncols);
3790 if (err)
3791 return err;
3792 waddwstr(view->window, wline);
3793 free(wline);
3794 wline = NULL;
3795 if (width < view->ncols - 1)
3796 waddch(view->window, '\n');
3797 if (--limit <= 0)
3798 return NULL;
3799 waddch(view->window, '\n');
3800 if (--limit <= 0)
3801 return NULL;
3803 te = SIMPLEQ_FIRST(&entries->head);
3804 if (*first_displayed_entry == NULL) {
3805 if (selected == 0) {
3806 if (view->focussed)
3807 wstandout(view->window);
3808 *selected_entry = NULL;
3810 waddstr(view->window, " ..\n"); /* parent directory */
3811 if (selected == 0 && view->focussed)
3812 wstandend(view->window);
3813 (*ndisplayed)++;
3814 if (--limit <= 0)
3815 return NULL;
3816 n = 1;
3817 } else {
3818 n = 0;
3819 while (te != *first_displayed_entry)
3820 te = SIMPLEQ_NEXT(te, entry);
3823 while (te) {
3824 char *line = NULL, *id_str = NULL;
3826 if (show_ids) {
3827 err = got_object_id_str(&id_str, te->id);
3828 if (err)
3829 return got_error_from_errno(
3830 "got_object_id_str");
3832 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3833 te->name, S_ISDIR(te->mode) ? "/" :
3834 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3835 free(id_str);
3836 return got_error_from_errno("asprintf");
3838 free(id_str);
3839 err = format_line(&wline, &width, line, view->ncols);
3840 if (err) {
3841 free(line);
3842 break;
3844 if (n == selected) {
3845 if (view->focussed)
3846 wstandout(view->window);
3847 *selected_entry = te;
3849 waddwstr(view->window, wline);
3850 if (width < view->ncols - 1)
3851 waddch(view->window, '\n');
3852 if (n == selected && view->focussed)
3853 wstandend(view->window);
3854 free(line);
3855 free(wline);
3856 wline = NULL;
3857 n++;
3858 (*ndisplayed)++;
3859 *last_displayed_entry = te;
3860 if (--limit <= 0)
3861 break;
3862 te = SIMPLEQ_NEXT(te, entry);
3865 return err;
3868 static void
3869 tree_scroll_up(struct tog_view *view,
3870 struct got_tree_entry **first_displayed_entry, int maxscroll,
3871 const struct got_tree_entries *entries, int isroot)
3873 struct got_tree_entry *te, *prev;
3874 int i;
3876 if (*first_displayed_entry == NULL)
3877 return;
3879 te = SIMPLEQ_FIRST(&entries->head);
3880 if (*first_displayed_entry == te) {
3881 if (!isroot)
3882 *first_displayed_entry = NULL;
3883 return;
3886 /* XXX this is stupid... switch to TAILQ? */
3887 for (i = 0; i < maxscroll; i++) {
3888 while (te != *first_displayed_entry) {
3889 prev = te;
3890 te = SIMPLEQ_NEXT(te, entry);
3892 *first_displayed_entry = prev;
3893 te = SIMPLEQ_FIRST(&entries->head);
3895 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3896 *first_displayed_entry = NULL;
3899 static int
3900 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3901 struct got_tree_entry *last_displayed_entry,
3902 const struct got_tree_entries *entries)
3904 struct got_tree_entry *next, *last;
3905 int n = 0;
3907 if (*first_displayed_entry)
3908 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3909 else
3910 next = SIMPLEQ_FIRST(&entries->head);
3911 last = last_displayed_entry;
3912 while (next && last && n++ < maxscroll) {
3913 last = SIMPLEQ_NEXT(last, entry);
3914 if (last) {
3915 *first_displayed_entry = next;
3916 next = SIMPLEQ_NEXT(next, entry);
3919 return n;
3922 static const struct got_error *
3923 tree_entry_path(char **path, struct tog_parent_trees *parents,
3924 struct got_tree_entry *te)
3926 const struct got_error *err = NULL;
3927 struct tog_parent_tree *pt;
3928 size_t len = 2; /* for leading slash and NUL */
3930 TAILQ_FOREACH(pt, parents, entry)
3931 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3932 if (te)
3933 len += strlen(te->name);
3935 *path = calloc(1, len);
3936 if (path == NULL)
3937 return got_error_from_errno("calloc");
3939 (*path)[0] = '/';
3940 pt = TAILQ_LAST(parents, tog_parent_trees);
3941 while (pt) {
3942 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3943 err = got_error(GOT_ERR_NO_SPACE);
3944 goto done;
3946 if (strlcat(*path, "/", len) >= len) {
3947 err = got_error(GOT_ERR_NO_SPACE);
3948 goto done;
3950 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3952 if (te) {
3953 if (strlcat(*path, te->name, len) >= len) {
3954 err = got_error(GOT_ERR_NO_SPACE);
3955 goto done;
3958 done:
3959 if (err) {
3960 free(*path);
3961 *path = NULL;
3963 return err;
3966 static const struct got_error *
3967 blame_tree_entry(struct tog_view **new_view, int begin_x,
3968 struct got_tree_entry *te, struct tog_parent_trees *parents,
3969 struct got_object_id *commit_id, struct got_reflist_head *refs,
3970 struct got_repository *repo)
3972 const struct got_error *err = NULL;
3973 char *path;
3974 struct tog_view *blame_view;
3976 err = tree_entry_path(&path, parents, te);
3977 if (err)
3978 return err;
3980 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3981 if (blame_view == NULL)
3982 return got_error_from_errno("view_open");
3984 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3985 if (err) {
3986 view_close(blame_view);
3987 free(path);
3988 } else
3989 *new_view = blame_view;
3990 return err;
3993 static const struct got_error *
3994 log_tree_entry(struct tog_view **new_view, int begin_x,
3995 struct got_tree_entry *te, struct tog_parent_trees *parents,
3996 struct got_object_id *commit_id, struct got_reflist_head *refs,
3997 struct got_repository *repo)
3999 struct tog_view *log_view;
4000 const struct got_error *err = NULL;
4001 char *path;
4003 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4004 if (log_view == NULL)
4005 return got_error_from_errno("view_open");
4007 err = tree_entry_path(&path, parents, te);
4008 if (err)
4009 return err;
4011 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4012 if (err)
4013 view_close(log_view);
4014 else
4015 *new_view = log_view;
4016 free(path);
4017 return err;
4020 static const struct got_error *
4021 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4022 struct got_object_id *commit_id, struct got_reflist_head *refs,
4023 struct got_repository *repo)
4025 const struct got_error *err = NULL;
4026 char *commit_id_str = NULL;
4027 struct tog_tree_view_state *s = &view->state.tree;
4029 TAILQ_INIT(&s->parents);
4031 err = got_object_id_str(&commit_id_str, commit_id);
4032 if (err != NULL)
4033 goto done;
4035 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4036 err = got_error_from_errno("asprintf");
4037 goto done;
4040 s->root = s->tree = root;
4041 s->entries = got_object_tree_get_entries(root);
4042 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4043 s->commit_id = got_object_id_dup(commit_id);
4044 if (s->commit_id == NULL) {
4045 err = got_error_from_errno("got_object_id_dup");
4046 goto done;
4048 s->refs = refs;
4049 s->repo = repo;
4051 view->show = show_tree_view;
4052 view->input = input_tree_view;
4053 view->close = close_tree_view;
4054 view->search_start = search_start_tree_view;
4055 view->search_next = search_next_tree_view;
4056 done:
4057 free(commit_id_str);
4058 if (err) {
4059 free(s->tree_label);
4060 s->tree_label = NULL;
4062 return err;
4065 static const struct got_error *
4066 close_tree_view(struct tog_view *view)
4068 struct tog_tree_view_state *s = &view->state.tree;
4070 free(s->tree_label);
4071 s->tree_label = NULL;
4072 free(s->commit_id);
4073 s->commit_id = NULL;
4074 while (!TAILQ_EMPTY(&s->parents)) {
4075 struct tog_parent_tree *parent;
4076 parent = TAILQ_FIRST(&s->parents);
4077 TAILQ_REMOVE(&s->parents, parent, entry);
4078 free(parent);
4081 if (s->tree != s->root)
4082 got_object_tree_close(s->tree);
4083 got_object_tree_close(s->root);
4085 return NULL;
4088 static const struct got_error *
4089 search_start_tree_view(struct tog_view *view)
4091 struct tog_tree_view_state *s = &view->state.tree;
4093 s->matched_entry = NULL;
4094 return NULL;
4097 static int
4098 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4100 regmatch_t regmatch;
4102 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4105 static const struct got_error *
4106 search_next_tree_view(struct tog_view *view)
4108 struct tog_tree_view_state *s = &view->state.tree;
4109 struct got_tree_entry *entry, *te;
4111 if (!view->searching) {
4112 view->search_next_done = 1;
4113 return NULL;
4116 if (s->matched_entry) {
4117 if (view->searching == TOG_SEARCH_FORWARD) {
4118 if (s->selected_entry)
4119 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4120 else
4121 entry = SIMPLEQ_FIRST(&s->entries->head);
4123 else {
4124 if (s->selected_entry == NULL) {
4125 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4126 entry = te;
4127 } else {
4128 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4129 entry = te;
4130 if (SIMPLEQ_NEXT(te, entry) ==
4131 s->selected_entry)
4132 break;
4136 } else {
4137 if (view->searching == TOG_SEARCH_FORWARD)
4138 entry = SIMPLEQ_FIRST(&s->entries->head);
4139 else {
4140 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4141 entry = te;
4145 while (1) {
4146 if (entry == NULL) {
4147 if (s->matched_entry == NULL) {
4148 view->search_next_done = 1;
4149 return NULL;
4151 if (view->searching == TOG_SEARCH_FORWARD)
4152 entry = SIMPLEQ_FIRST(&s->entries->head);
4153 else {
4154 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4155 entry = te;
4159 if (match_tree_entry(entry, &view->regex)) {
4160 view->search_next_done = 1;
4161 s->matched_entry = entry;
4162 break;
4165 if (view->searching == TOG_SEARCH_FORWARD)
4166 entry = SIMPLEQ_NEXT(entry, entry);
4167 else {
4168 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4169 entry = NULL;
4170 else {
4171 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4172 if (SIMPLEQ_NEXT(te, entry) == entry) {
4173 entry = te;
4174 break;
4181 if (s->matched_entry) {
4182 s->first_displayed_entry = s->matched_entry;
4183 s->selected = 0;
4186 return NULL;
4189 static const struct got_error *
4190 show_tree_view(struct tog_view *view)
4192 const struct got_error *err = NULL;
4193 struct tog_tree_view_state *s = &view->state.tree;
4194 char *parent_path;
4196 err = tree_entry_path(&parent_path, &s->parents, NULL);
4197 if (err)
4198 return err;
4200 err = draw_tree_entries(view, &s->first_displayed_entry,
4201 &s->last_displayed_entry, &s->selected_entry,
4202 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4203 s->entries, s->selected, view->nlines, s->tree == s->root);
4204 free(parent_path);
4206 view_vborder(view);
4207 return err;
4210 static const struct got_error *
4211 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4212 struct tog_view **focus_view, struct tog_view *view, int ch)
4214 const struct got_error *err = NULL;
4215 struct tog_tree_view_state *s = &view->state.tree;
4216 struct tog_view *log_view;
4217 int begin_x = 0, nscrolled;
4219 switch (ch) {
4220 case 'i':
4221 s->show_ids = !s->show_ids;
4222 break;
4223 case 'l':
4224 if (!s->selected_entry)
4225 break;
4226 if (view_is_parent_view(view))
4227 begin_x = view_split_begin_x(view->begin_x);
4228 err = log_tree_entry(&log_view, begin_x,
4229 s->selected_entry, &s->parents,
4230 s->commit_id, s->refs, s->repo);
4231 if (view_is_parent_view(view)) {
4232 err = view_close_child(view);
4233 if (err)
4234 return err;
4235 err = view_set_child(view, log_view);
4236 if (err) {
4237 view_close(log_view);
4238 break;
4240 *focus_view = log_view;
4241 view->child_focussed = 1;
4242 } else
4243 *new_view = log_view;
4244 break;
4245 case 'k':
4246 case KEY_UP:
4247 if (s->selected > 0) {
4248 s->selected--;
4249 if (s->selected == 0)
4250 break;
4252 if (s->selected > 0)
4253 break;
4254 tree_scroll_up(view, &s->first_displayed_entry, 1,
4255 s->entries, s->tree == s->root);
4256 break;
4257 case KEY_PPAGE:
4258 tree_scroll_up(view, &s->first_displayed_entry,
4259 MAX(0, view->nlines - 4 - s->selected), s->entries,
4260 s->tree == s->root);
4261 s->selected = 0;
4262 if (SIMPLEQ_FIRST(&s->entries->head) ==
4263 s->first_displayed_entry && s->tree != s->root)
4264 s->first_displayed_entry = NULL;
4265 break;
4266 case 'j':
4267 case KEY_DOWN:
4268 if (s->selected < s->ndisplayed - 1) {
4269 s->selected++;
4270 break;
4272 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4273 /* can't scroll any further */
4274 break;
4275 tree_scroll_down(&s->first_displayed_entry, 1,
4276 s->last_displayed_entry, s->entries);
4277 break;
4278 case KEY_NPAGE:
4279 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4280 == NULL) {
4281 /* can't scroll any further; move cursor down */
4282 if (s->selected < s->ndisplayed - 1)
4283 s->selected = s->ndisplayed - 1;
4284 break;
4286 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4287 view->nlines, s->last_displayed_entry, s->entries);
4288 if (nscrolled < view->nlines) {
4289 int ndisplayed = 0;
4290 struct got_tree_entry *te;
4291 te = s->first_displayed_entry;
4292 do {
4293 ndisplayed++;
4294 te = SIMPLEQ_NEXT(te, entry);
4295 } while (te);
4296 s->selected = ndisplayed - 1;
4298 break;
4299 case KEY_ENTER:
4300 case '\r':
4301 case KEY_BACKSPACE:
4302 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4303 struct tog_parent_tree *parent;
4304 /* user selected '..' */
4305 if (s->tree == s->root)
4306 break;
4307 parent = TAILQ_FIRST(&s->parents);
4308 TAILQ_REMOVE(&s->parents, parent,
4309 entry);
4310 got_object_tree_close(s->tree);
4311 s->tree = parent->tree;
4312 s->entries =
4313 got_object_tree_get_entries(s->tree);
4314 s->first_displayed_entry =
4315 parent->first_displayed_entry;
4316 s->selected_entry =
4317 parent->selected_entry;
4318 s->selected = parent->selected;
4319 free(parent);
4320 } else if (S_ISDIR(s->selected_entry->mode)) {
4321 struct got_tree_object *subtree;
4322 err = got_object_open_as_tree(&subtree,
4323 s->repo, s->selected_entry->id);
4324 if (err)
4325 break;
4326 err = tree_view_visit_subtree(subtree, s);
4327 if (err) {
4328 got_object_tree_close(subtree);
4329 break;
4331 } else if (S_ISREG(s->selected_entry->mode)) {
4332 struct tog_view *blame_view;
4333 int begin_x = view_is_parent_view(view) ?
4334 view_split_begin_x(view->begin_x) : 0;
4336 err = blame_tree_entry(&blame_view, begin_x,
4337 s->selected_entry, &s->parents,
4338 s->commit_id, s->refs, s->repo);
4339 if (err)
4340 break;
4341 if (view_is_parent_view(view)) {
4342 err = view_close_child(view);
4343 if (err)
4344 return err;
4345 err = view_set_child(view, blame_view);
4346 if (err) {
4347 view_close(blame_view);
4348 break;
4350 *focus_view = blame_view;
4351 view->child_focussed = 1;
4352 } else
4353 *new_view = blame_view;
4355 break;
4356 case KEY_RESIZE:
4357 if (s->selected > view->nlines)
4358 s->selected = s->ndisplayed - 1;
4359 break;
4360 default:
4361 break;
4364 return err;
4367 __dead static void
4368 usage_tree(void)
4370 endwin();
4371 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4372 getprogname());
4373 exit(1);
4376 static const struct got_error *
4377 cmd_tree(int argc, char *argv[])
4379 const struct got_error *error;
4380 struct got_repository *repo = NULL;
4381 struct got_reflist_head refs;
4382 char *repo_path = NULL;
4383 struct got_object_id *commit_id = NULL;
4384 char *commit_id_arg = NULL;
4385 struct got_commit_object *commit = NULL;
4386 struct got_tree_object *tree = NULL;
4387 int ch;
4388 struct tog_view *view;
4390 SIMPLEQ_INIT(&refs);
4392 #ifndef PROFILE
4393 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4394 NULL) == -1)
4395 err(1, "pledge");
4396 #endif
4398 while ((ch = getopt(argc, argv, "c:")) != -1) {
4399 switch (ch) {
4400 case 'c':
4401 commit_id_arg = optarg;
4402 break;
4403 default:
4404 usage_tree();
4405 /* NOTREACHED */
4409 argc -= optind;
4410 argv += optind;
4412 if (argc == 0) {
4413 struct got_worktree *worktree;
4414 char *cwd = getcwd(NULL, 0);
4415 if (cwd == NULL)
4416 return got_error_from_errno("getcwd");
4417 error = got_worktree_open(&worktree, cwd);
4418 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4419 goto done;
4420 if (worktree) {
4421 free(cwd);
4422 repo_path =
4423 strdup(got_worktree_get_repo_path(worktree));
4424 got_worktree_close(worktree);
4425 } else
4426 repo_path = cwd;
4427 if (repo_path == NULL) {
4428 error = got_error_from_errno("strdup");
4429 goto done;
4431 } else if (argc == 1) {
4432 repo_path = realpath(argv[0], NULL);
4433 if (repo_path == NULL)
4434 return got_error_from_errno2("realpath", argv[0]);
4435 } else
4436 usage_log();
4438 init_curses();
4440 error = got_repo_open(&repo, repo_path);
4441 if (error != NULL)
4442 goto done;
4444 error = apply_unveil(got_repo_get_path(repo), NULL);
4445 if (error)
4446 goto done;
4448 if (commit_id_arg == NULL)
4449 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4450 else {
4451 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4452 if (error) {
4453 if (error->code != GOT_ERR_NOT_REF)
4454 goto done;
4455 error = got_repo_match_object_id_prefix(&commit_id,
4456 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4459 if (error != NULL)
4460 goto done;
4462 error = got_object_open_as_commit(&commit, repo, commit_id);
4463 if (error != NULL)
4464 goto done;
4466 error = got_object_open_as_tree(&tree, repo,
4467 got_object_commit_get_tree_id(commit));
4468 if (error != NULL)
4469 goto done;
4471 error = got_ref_list(&refs, repo);
4472 if (error)
4473 goto done;
4475 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4476 if (view == NULL) {
4477 error = got_error_from_errno("view_open");
4478 goto done;
4480 error = open_tree_view(view, tree, commit_id, &refs, repo);
4481 if (error)
4482 goto done;
4483 error = view_loop(view);
4484 done:
4485 free(repo_path);
4486 free(commit_id);
4487 if (commit)
4488 got_object_commit_close(commit);
4489 if (tree)
4490 got_object_tree_close(tree);
4491 if (repo)
4492 got_repo_close(repo);
4493 got_ref_list_free(&refs);
4494 return error;
4497 static void
4498 list_commands(void)
4500 int i;
4502 fprintf(stderr, "commands:");
4503 for (i = 0; i < nitems(tog_commands); i++) {
4504 struct tog_cmd *cmd = &tog_commands[i];
4505 fprintf(stderr, " %s", cmd->name);
4507 fputc('\n', stderr);
4510 __dead static void
4511 usage(int hflag)
4513 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4514 if (hflag)
4515 list_commands();
4516 exit(1);
4519 static char **
4520 make_argv(const char *arg0, const char *arg1)
4522 char **argv;
4523 int argc = (arg1 == NULL ? 1 : 2);
4525 argv = calloc(argc, sizeof(char *));
4526 if (argv == NULL)
4527 err(1, "calloc");
4528 argv[0] = strdup(arg0);
4529 if (argv[0] == NULL)
4530 err(1, "calloc");
4531 if (arg1) {
4532 argv[1] = strdup(arg1);
4533 if (argv[1] == NULL)
4534 err(1, "calloc");
4537 return argv;
4540 int
4541 main(int argc, char *argv[])
4543 const struct got_error *error = NULL;
4544 struct tog_cmd *cmd = NULL;
4545 int ch, hflag = 0;
4546 char **cmd_argv = NULL;
4548 setlocale(LC_CTYPE, "");
4550 while ((ch = getopt(argc, argv, "h")) != -1) {
4551 switch (ch) {
4552 case 'h':
4553 hflag = 1;
4554 break;
4555 default:
4556 usage(hflag);
4557 /* NOTREACHED */
4561 argc -= optind;
4562 argv += optind;
4563 optind = 0;
4564 optreset = 1;
4566 if (argc == 0) {
4567 if (hflag)
4568 usage(hflag);
4569 /* Build an argument vector which runs a default command. */
4570 cmd = &tog_commands[0];
4571 cmd_argv = make_argv(cmd->name, NULL);
4572 argc = 1;
4573 } else {
4574 int i;
4576 /* Did the user specific a command? */
4577 for (i = 0; i < nitems(tog_commands); i++) {
4578 if (strncmp(tog_commands[i].name, argv[0],
4579 strlen(argv[0])) == 0) {
4580 cmd = &tog_commands[i];
4581 break;
4585 if (cmd == NULL) {
4586 fprintf(stderr, "%s: unknown command '%s'\n",
4587 getprogname(), argv[0]);
4588 list_commands();
4589 return 1;
4593 if (hflag)
4594 cmd->cmd_usage();
4595 else
4596 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4598 endwin();
4599 free(cmd_argv);
4600 if (error)
4601 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4602 return 0;