Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_commit_graph.h"
49 #include "got_utf8.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_path.h"
53 #include "got_worktree.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 #define CTRL(x) ((x) & 0x1f)
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct tog_cmd {
70 const char *name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 };
75 __dead static void usage(int);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log },
88 { "diff", cmd_diff, usage_diff },
89 { "blame", cmd_blame, usage_blame },
90 { "tree", cmd_tree, usage_tree },
91 };
93 enum tog_view_type {
94 TOG_VIEW_DIFF,
95 TOG_VIEW_LOG,
96 TOG_VIEW_BLAME,
97 TOG_VIEW_TREE
98 };
100 #define TOG_EOF_STRING "(END)"
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
106 int idx;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
116 FILE *f;
117 int first_displayed_line;
118 int last_displayed_line;
119 int eof;
120 int diff_context;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
126 };
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
132 int commits_needed;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
138 int log_complete;
139 sig_atomic_t *quit;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 };
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
150 int selected;
151 char *in_repo_path;
152 const char *head_ref_name;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 struct commit_queue_entry *matched_entry;
160 struct commit_queue_entry *search_entry;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 off_t *line_offsets;
185 pthread_t thread;
186 struct tog_blame_thread_args thread_args;
187 struct tog_blame_cb_args cb_args;
188 const char *path;
189 };
191 struct tog_blame_view_state {
192 int first_displayed_line;
193 int last_displayed_line;
194 int selected_line;
195 int blame_complete;
196 int eof;
197 int done;
198 struct got_object_id_queue blamed_commits;
199 struct got_object_qid *blamed_commit;
200 char *path;
201 struct got_repository *repo;
202 struct got_reflist_head *refs;
203 struct got_object_id *commit_id;
204 struct tog_blame blame;
205 int matched_line;
206 };
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int selected;
214 };
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
219 char *tree_label;
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 struct got_tree_entry *matched_entry;
232 };
234 /*
235 * We implement two types of views: parent views and child views.
237 * The 'Tab' key switches between a parent view and its child view.
238 * Child views are shown side-by-side to their parent view, provided
239 * there is enough screen estate.
241 * When a new view is opened from within a parent view, this new view
242 * becomes a child view of the parent view, replacing any existing child.
244 * When a new view is opened from within a child view, this new view
245 * becomes a parent view which will obscure the views below until the
246 * user quits the new parent view by typing 'q'.
248 * This list of views contains parent views only.
249 * Child views are only pointed to by their parent view.
250 */
251 TAILQ_HEAD(tog_view_list_head, tog_view);
253 struct tog_view {
254 TAILQ_ENTRY(tog_view) entry;
255 WINDOW *window;
256 PANEL *panel;
257 int nlines, ncols, begin_y, begin_x;
258 int lines, cols; /* copies of LINES and COLS */
259 int focussed;
260 struct tog_view *parent;
261 struct tog_view *child;
262 int child_focussed;
264 /* type-specific state */
265 enum tog_view_type type;
266 union {
267 struct tog_diff_view_state diff;
268 struct tog_log_view_state log;
269 struct tog_blame_view_state blame;
270 struct tog_tree_view_state tree;
271 } state;
273 const struct got_error *(*show)(struct tog_view *);
274 const struct got_error *(*input)(struct tog_view **,
275 struct tog_view **, struct tog_view**, struct tog_view *, int);
276 const struct got_error *(*close)(struct tog_view *);
278 const struct got_error *(*search_start)(struct tog_view *);
279 const struct got_error *(*search_next)(struct tog_view *);
280 int searching;
281 #define TOG_SEARCH_FORWARD 1
282 #define TOG_SEARCH_BACKWARD 2
283 int search_next_done;
284 regex_t regex;
285 };
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *search_start_blame_view(struct tog_view *);
312 static const struct got_error *search_next_blame_view(struct tog_view *);
314 static const struct got_error *open_tree_view(struct tog_view *,
315 struct got_tree_object *, struct got_object_id *,
316 struct got_reflist_head *, struct got_repository *);
317 static const struct got_error *show_tree_view(struct tog_view *);
318 static const struct got_error *input_tree_view(struct tog_view **,
319 struct tog_view **, struct tog_view **, struct tog_view *, int);
320 static const struct got_error *close_tree_view(struct tog_view *);
321 static const struct got_error *search_start_tree_view(struct tog_view *);
322 static const struct got_error *search_next_tree_view(struct tog_view *);
324 static volatile sig_atomic_t tog_sigwinch_received;
326 static void
327 tog_sigwinch(int signo)
329 tog_sigwinch_received = 1;
332 static const struct got_error *
333 view_close(struct tog_view *view)
335 const struct got_error *err = NULL;
337 if (view->child) {
338 view_close(view->child);
339 view->child = NULL;
341 if (view->close)
342 err = view->close(view);
343 if (view->panel)
344 del_panel(view->panel);
345 if (view->window)
346 delwin(view->window);
347 free(view);
348 return err;
351 static struct tog_view *
352 view_open(int nlines, int ncols, int begin_y, int begin_x,
353 enum tog_view_type type)
355 struct tog_view *view = calloc(1, sizeof(*view));
357 if (view == NULL)
358 return NULL;
360 view->type = type;
361 view->lines = LINES;
362 view->cols = COLS;
363 view->nlines = nlines ? nlines : LINES - begin_y;
364 view->ncols = ncols ? ncols : COLS - begin_x;
365 view->begin_y = begin_y;
366 view->begin_x = begin_x;
367 view->window = newwin(nlines, ncols, begin_y, begin_x);
368 if (view->window == NULL) {
369 view_close(view);
370 return NULL;
372 view->panel = new_panel(view->window);
373 if (view->panel == NULL ||
374 set_panel_userptr(view->panel, view) != OK) {
375 view_close(view);
376 return NULL;
379 keypad(view->window, TRUE);
380 return view;
383 static int
384 view_split_begin_x(int begin_x)
386 if (begin_x > 0 || COLS < 120)
387 return 0;
388 return (COLS - MAX(COLS / 2, 80));
391 static const struct got_error *view_resize(struct tog_view *);
393 static const struct got_error *
394 view_splitscreen(struct tog_view *view)
396 const struct got_error *err = NULL;
398 view->begin_y = 0;
399 view->begin_x = view_split_begin_x(0);
400 view->nlines = LINES;
401 view->ncols = COLS - view->begin_x;
402 view->lines = LINES;
403 view->cols = COLS;
404 err = view_resize(view);
405 if (err)
406 return err;
408 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
409 return got_error_from_errno("mvwin");
411 return NULL;
414 static const struct got_error *
415 view_fullscreen(struct tog_view *view)
417 const struct got_error *err = NULL;
419 view->begin_x = 0;
420 view->begin_y = 0;
421 view->nlines = LINES;
422 view->ncols = COLS;
423 view->lines = LINES;
424 view->cols = COLS;
425 err = view_resize(view);
426 if (err)
427 return err;
429 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
430 return got_error_from_errno("mvwin");
432 return NULL;
435 static int
436 view_is_parent_view(struct tog_view *view)
438 return view->parent == NULL;
441 static const struct got_error *
442 view_resize(struct tog_view *view)
444 int nlines, ncols;
446 if (view->lines > LINES)
447 nlines = view->nlines - (view->lines - LINES);
448 else
449 nlines = view->nlines + (LINES - view->lines);
451 if (view->cols > COLS)
452 ncols = view->ncols - (view->cols - COLS);
453 else
454 ncols = view->ncols + (COLS - view->cols);
456 if (wresize(view->window, nlines, ncols) == ERR)
457 return got_error_from_errno("wresize");
458 if (replace_panel(view->panel, view->window) == ERR)
459 return got_error_from_errno("replace_panel");
460 wclear(view->window);
462 view->nlines = nlines;
463 view->ncols = ncols;
464 view->lines = LINES;
465 view->cols = COLS;
467 if (view->child) {
468 view->child->begin_x = view_split_begin_x(view->begin_x);
469 if (view->child->begin_x == 0) {
470 view_fullscreen(view->child);
471 if (view->child->focussed)
472 show_panel(view->child->panel);
473 else
474 show_panel(view->panel);
475 } else {
476 view_splitscreen(view->child);
477 show_panel(view->child->panel);
481 return NULL;
484 static const struct got_error *
485 view_close_child(struct tog_view *view)
487 const struct got_error *err = NULL;
489 if (view->child == NULL)
490 return NULL;
492 err = view_close(view->child);
493 view->child = NULL;
494 return err;
497 static const struct got_error *
498 view_set_child(struct tog_view *view, struct tog_view *child)
500 const struct got_error *err = NULL;
502 view->child = child;
503 child->parent = view;
504 return err;
507 static int
508 view_is_splitscreen(struct tog_view *view)
510 return view->begin_x > 0;
513 static void
514 tog_resizeterm(void)
516 int cols, lines;
517 struct winsize size;
519 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
520 cols = 80; /* Default */
521 lines = 24;
522 } else {
523 cols = size.ws_col;
524 lines = size.ws_row;
526 resize_term(lines, cols);
529 static const struct got_error *
530 view_search_start(struct tog_view *view)
532 const struct got_error *err = NULL;
533 char pattern[1024];
534 int ret;
536 if (view->nlines < 1)
537 return NULL;
539 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
540 view->begin_x, "/");
541 wclrtoeol(view->window);
543 nocbreak();
544 echo();
545 ret = wgetnstr(view->window, pattern, sizeof(pattern));
546 cbreak();
547 noecho();
548 if (ret == ERR)
549 return NULL;
551 if (view->searching) {
552 regfree(&view->regex);
553 view->searching = 0;
556 if (regcomp(&view->regex, pattern,
557 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
558 err = view->search_start(view);
559 if (err) {
560 regfree(&view->regex);
561 return err;
563 view->searching = TOG_SEARCH_FORWARD;
564 view->search_next_done = 0;
565 view->search_next(view);
568 return NULL;
571 static const struct got_error *
572 view_input(struct tog_view **new, struct tog_view **dead,
573 struct tog_view **focus, int *done, struct tog_view *view,
574 struct tog_view_list_head *views)
576 const struct got_error *err = NULL;
577 struct tog_view *v;
578 int ch, errcode;
580 *new = NULL;
581 *dead = NULL;
582 *focus = NULL;
584 if (view->searching && !view->search_next_done) {
585 errcode = pthread_mutex_unlock(&tog_mutex);
586 if (errcode)
587 return got_error_set_errno(errcode,
588 "pthread_mutex_unlock");
589 pthread_yield();
590 errcode = pthread_mutex_lock(&tog_mutex);
591 if (errcode)
592 return got_error_set_errno(errcode,
593 "pthread_mutex_lock");
594 view->search_next(view);
595 return NULL;
598 nodelay(stdscr, FALSE);
599 /* Allow threads to make progress while we are waiting for input. */
600 errcode = pthread_mutex_unlock(&tog_mutex);
601 if (errcode)
602 return got_error_set_errno(errcode, "pthread_mutex_unlock");
603 ch = wgetch(view->window);
604 errcode = pthread_mutex_lock(&tog_mutex);
605 if (errcode)
606 return got_error_set_errno(errcode, "pthread_mutex_lock");
607 nodelay(stdscr, TRUE);
609 if (tog_sigwinch_received) {
610 tog_resizeterm();
611 tog_sigwinch_received = 0;
612 TAILQ_FOREACH(v, views, entry) {
613 err = view_resize(v);
614 if (err)
615 return err;
616 err = v->input(new, dead, focus, v, KEY_RESIZE);
617 if (err)
618 return err;
622 switch (ch) {
623 case ERR:
624 break;
625 case '\t':
626 if (view->child) {
627 *focus = view->child;
628 view->child_focussed = 1;
629 } else if (view->parent) {
630 *focus = view->parent;
631 view->parent->child_focussed = 0;
633 break;
634 case 'q':
635 err = view->input(new, dead, focus, view, ch);
636 *dead = view;
637 break;
638 case 'Q':
639 *done = 1;
640 break;
641 case 'f':
642 if (view_is_parent_view(view)) {
643 if (view->child == NULL)
644 break;
645 if (view_is_splitscreen(view->child)) {
646 *focus = view->child;
647 view->child_focussed = 1;
648 err = view_fullscreen(view->child);
649 } else
650 err = view_splitscreen(view->child);
651 if (err)
652 break;
653 err = view->child->input(new, dead, focus,
654 view->child, KEY_RESIZE);
655 } else {
656 if (view_is_splitscreen(view)) {
657 *focus = view;
658 view->parent->child_focussed = 1;
659 err = view_fullscreen(view);
660 } else {
661 err = view_splitscreen(view);
663 if (err)
664 break;
665 err = view->input(new, dead, focus, view,
666 KEY_RESIZE);
668 break;
669 case KEY_RESIZE:
670 break;
671 case '/':
672 if (view->search_start)
673 view_search_start(view);
674 else
675 err = view->input(new, dead, focus, view, ch);
676 break;
677 case 'N':
678 case 'n':
679 if (view->search_next && view->searching) {
680 view->searching = (ch == 'n' ?
681 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
682 view->search_next_done = 0;
683 view->search_next(view);
684 } else
685 err = view->input(new, dead, focus, view, ch);
686 break;
687 default:
688 err = view->input(new, dead, focus, view, ch);
689 break;
692 return err;
695 void
696 view_vborder(struct tog_view *view)
698 PANEL *panel;
699 struct tog_view *view_above;
701 if (view->parent)
702 return view_vborder(view->parent);
704 panel = panel_above(view->panel);
705 if (panel == NULL)
706 return;
708 view_above = panel_userptr(panel);
709 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
710 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
713 int
714 view_needs_focus_indication(struct tog_view *view)
716 if (view_is_parent_view(view)) {
717 if (view->child == NULL || view->child_focussed)
718 return 0;
719 if (!view_is_splitscreen(view->child))
720 return 0;
721 } else if (!view_is_splitscreen(view))
722 return 0;
724 return view->focussed;
727 static const struct got_error *
728 view_loop(struct tog_view *view)
730 const struct got_error *err = NULL;
731 struct tog_view_list_head views;
732 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
733 int fast_refresh = 10;
734 int done = 0, errcode;
736 errcode = pthread_mutex_lock(&tog_mutex);
737 if (errcode)
738 return got_error_set_errno(errcode, "pthread_mutex_lock");
740 TAILQ_INIT(&views);
741 TAILQ_INSERT_HEAD(&views, view, entry);
743 main_view = view;
744 view->focussed = 1;
745 err = view->show(view);
746 if (err)
747 return err;
748 update_panels();
749 doupdate();
750 while (!TAILQ_EMPTY(&views) && !done) {
751 /* Refresh fast during initialization, then become slower. */
752 if (fast_refresh && fast_refresh-- == 0)
753 halfdelay(10); /* switch to once per second */
755 err = view_input(&new_view, &dead_view, &focus_view, &done,
756 view, &views);
757 if (err)
758 break;
759 if (dead_view) {
760 struct tog_view *prev = NULL;
762 if (view_is_parent_view(dead_view))
763 prev = TAILQ_PREV(dead_view,
764 tog_view_list_head, entry);
765 else if (view->parent != dead_view)
766 prev = view->parent;
768 if (dead_view->parent)
769 dead_view->parent->child = NULL;
770 else
771 TAILQ_REMOVE(&views, dead_view, entry);
773 err = view_close(dead_view);
774 if (err || (dead_view == main_view && new_view == NULL))
775 goto done;
777 if (view == dead_view) {
778 if (focus_view)
779 view = focus_view;
780 else if (prev)
781 view = prev;
782 else if (!TAILQ_EMPTY(&views))
783 view = TAILQ_LAST(&views,
784 tog_view_list_head);
785 else
786 view = NULL;
787 if (view) {
788 if (view->child && view->child_focussed)
789 focus_view = view->child;
790 else
791 focus_view = view;
795 if (new_view) {
796 struct tog_view *v, *t;
797 /* Only allow one parent view per type. */
798 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
799 if (v->type != new_view->type)
800 continue;
801 TAILQ_REMOVE(&views, v, entry);
802 err = view_close(v);
803 if (err)
804 goto done;
805 break;
807 TAILQ_INSERT_TAIL(&views, new_view, entry);
808 view = new_view;
809 if (focus_view == NULL)
810 focus_view = new_view;
812 if (focus_view) {
813 show_panel(focus_view->panel);
814 if (view)
815 view->focussed = 0;
816 focus_view->focussed = 1;
817 view = focus_view;
818 if (new_view)
819 show_panel(new_view->panel);
820 if (view->child && view_is_splitscreen(view->child))
821 show_panel(view->child->panel);
823 if (view) {
824 if (focus_view == NULL) {
825 view->focussed = 1;
826 show_panel(view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
829 focus_view = view;
831 if (view->parent) {
832 err = view->parent->show(view->parent);
833 if (err)
834 goto done;
836 err = view->show(view);
837 if (err)
838 goto done;
839 if (view->child) {
840 err = view->child->show(view->child);
841 if (err)
842 goto done;
844 update_panels();
845 doupdate();
848 done:
849 while (!TAILQ_EMPTY(&views)) {
850 view = TAILQ_FIRST(&views);
851 TAILQ_REMOVE(&views, view, entry);
852 view_close(view);
855 errcode = pthread_mutex_unlock(&tog_mutex);
856 if (errcode)
857 return got_error_set_errno(errcode, "pthread_mutex_unlock");
859 return err;
862 __dead static void
863 usage_log(void)
865 endwin();
866 fprintf(stderr,
867 "usage: %s log [-c commit] [-r repository-path] [path]\n",
868 getprogname());
869 exit(1);
872 /* Create newly allocated wide-character string equivalent to a byte string. */
873 static const struct got_error *
874 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
876 char *vis = NULL;
877 const struct got_error *err = NULL;
879 *ws = NULL;
880 *wlen = mbstowcs(NULL, s, 0);
881 if (*wlen == (size_t)-1) {
882 int vislen;
883 if (errno != EILSEQ)
884 return got_error_from_errno("mbstowcs");
886 /* byte string invalid in current encoding; try to "fix" it */
887 err = got_mbsavis(&vis, &vislen, s);
888 if (err)
889 return err;
890 *wlen = mbstowcs(NULL, vis, 0);
891 if (*wlen == (size_t)-1) {
892 err = got_error_from_errno("mbstowcs"); /* give up */
893 goto done;
897 *ws = calloc(*wlen + 1, sizeof(*ws));
898 if (*ws == NULL) {
899 err = got_error_from_errno("calloc");
900 goto done;
903 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
904 err = got_error_from_errno("mbstowcs");
905 done:
906 free(vis);
907 if (err) {
908 free(*ws);
909 *ws = NULL;
910 *wlen = 0;
912 return err;
915 /* Format a line for display, ensuring that it won't overflow a width limit. */
916 static const struct got_error *
917 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
919 const struct got_error *err = NULL;
920 int cols = 0;
921 wchar_t *wline = NULL;
922 size_t wlen;
923 int i;
925 *wlinep = NULL;
926 *widthp = 0;
928 err = mbs2ws(&wline, &wlen, line);
929 if (err)
930 return err;
932 i = 0;
933 while (i < wlen && cols < wlimit) {
934 int width = wcwidth(wline[i]);
935 switch (width) {
936 case 0:
937 i++;
938 break;
939 case 1:
940 case 2:
941 if (cols + width <= wlimit)
942 cols += width;
943 i++;
944 break;
945 case -1:
946 if (wline[i] == L'\t')
947 cols += TABSIZE - ((cols + 1) % TABSIZE);
948 i++;
949 break;
950 default:
951 err = got_error_from_errno("wcwidth");
952 goto done;
955 wline[i] = L'\0';
956 if (widthp)
957 *widthp = cols;
958 done:
959 if (err)
960 free(wline);
961 else
962 *wlinep = wline;
963 return err;
966 static const struct got_error*
967 build_refs_str(char **refs_str, struct got_reflist_head *refs,
968 struct got_object_id *id)
970 static const struct got_error *err = NULL;
971 struct got_reflist_entry *re;
972 char *s;
973 const char *name;
975 *refs_str = NULL;
977 SIMPLEQ_FOREACH(re, refs, entry) {
978 if (got_object_id_cmp(re->id, id) != 0)
979 continue;
980 name = got_ref_get_name(re->ref);
981 if (strcmp(name, GOT_REF_HEAD) == 0)
982 continue;
983 if (strncmp(name, "refs/", 5) == 0)
984 name += 5;
985 if (strncmp(name, "got/", 4) == 0)
986 continue;
987 if (strncmp(name, "heads/", 6) == 0)
988 name += 6;
989 if (strncmp(name, "remotes/", 8) == 0)
990 name += 8;
991 s = *refs_str;
992 if (asprintf(refs_str, "%s%s%s", s ? s : "",
993 s ? ", " : "", name) == -1) {
994 err = got_error_from_errno("asprintf");
995 free(s);
996 *refs_str = NULL;
997 break;
999 free(s);
1002 return err;
1005 static const struct got_error *
1006 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1008 char *smallerthan, *at;
1010 smallerthan = strchr(author, '<');
1011 if (smallerthan && smallerthan[1] != '\0')
1012 author = smallerthan + 1;
1013 at = strchr(author, '@');
1014 if (at)
1015 *at = '\0';
1016 return format_line(wauthor, author_width, author, limit);
1019 static const struct got_error *
1020 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1021 struct got_object_id *id, struct got_reflist_head *refs,
1022 int author_display_cols)
1024 const struct got_error *err = NULL;
1025 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1026 char *logmsg0 = NULL, *logmsg = NULL;
1027 char *author = NULL;
1028 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1029 int author_width, logmsg_width;
1030 char *newline, *line = NULL;
1031 int col, limit;
1032 static const size_t date_display_cols = 9;
1033 const int avail = view->ncols;
1034 struct tm tm;
1035 time_t committer_time;
1037 committer_time = got_object_commit_get_committer_time(commit);
1038 if (localtime_r(&committer_time, &tm) == NULL)
1039 return got_error_from_errno("localtime_r");
1040 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1041 >= sizeof(datebuf))
1042 return got_error(GOT_ERR_NO_SPACE);
1044 if (avail < date_display_cols)
1045 limit = MIN(sizeof(datebuf) - 1, avail);
1046 else
1047 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1048 waddnstr(view->window, datebuf, limit);
1049 col = limit + 1;
1050 if (col > avail)
1051 goto done;
1053 author = strdup(got_object_commit_get_author(commit));
1054 if (author == NULL) {
1055 err = got_error_from_errno("strdup");
1056 goto done;
1058 err = format_author(&wauthor, &author_width, author, avail - col);
1059 if (err)
1060 goto done;
1061 waddwstr(view->window, wauthor);
1062 col += author_width;
1063 while (col <= avail && author_width < author_display_cols + 2) {
1064 waddch(view->window, ' ');
1065 col++;
1066 author_width++;
1068 if (col > avail)
1069 goto done;
1071 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1072 if (logmsg0 == NULL) {
1073 err = got_error_from_errno("strdup");
1074 goto done;
1076 logmsg = logmsg0;
1077 while (*logmsg == '\n')
1078 logmsg++;
1079 newline = strchr(logmsg, '\n');
1080 if (newline)
1081 *newline = '\0';
1082 limit = avail - col;
1083 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1084 if (err)
1085 goto done;
1086 waddwstr(view->window, wlogmsg);
1087 col += logmsg_width;
1088 while (col <= avail) {
1089 waddch(view->window, ' ');
1090 col++;
1092 done:
1093 free(logmsg0);
1094 free(wlogmsg);
1095 free(author);
1096 free(wauthor);
1097 free(line);
1098 return err;
1101 static struct commit_queue_entry *
1102 alloc_commit_queue_entry(struct got_commit_object *commit,
1103 struct got_object_id *id)
1105 struct commit_queue_entry *entry;
1107 entry = calloc(1, sizeof(*entry));
1108 if (entry == NULL)
1109 return NULL;
1111 entry->id = id;
1112 entry->commit = commit;
1113 return entry;
1116 static void
1117 pop_commit(struct commit_queue *commits)
1119 struct commit_queue_entry *entry;
1121 entry = TAILQ_FIRST(&commits->head);
1122 TAILQ_REMOVE(&commits->head, entry, entry);
1123 got_object_commit_close(entry->commit);
1124 commits->ncommits--;
1125 /* Don't free entry->id! It is owned by the commit graph. */
1126 free(entry);
1129 static void
1130 free_commits(struct commit_queue *commits)
1132 while (!TAILQ_EMPTY(&commits->head))
1133 pop_commit(commits);
1136 static const struct got_error *
1137 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1138 int minqueue, struct got_repository *repo, const char *path)
1140 const struct got_error *err = NULL;
1141 int nqueued = 0;
1144 * We keep all commits open throughout the lifetime of the log
1145 * view in order to avoid having to re-fetch commits from disk
1146 * while updating the display.
1148 while (nqueued < minqueue) {
1149 struct got_object_id *id;
1150 struct got_commit_object *commit;
1151 struct commit_queue_entry *entry;
1152 int errcode;
1154 err = got_commit_graph_iter_next(&id, graph);
1155 if (err) {
1156 if (err->code != GOT_ERR_ITER_NEED_MORE)
1157 break;
1158 err = got_commit_graph_fetch_commits(graph,
1159 minqueue, repo);
1160 if (err)
1161 return err;
1162 continue;
1165 if (id == NULL)
1166 break;
1168 err = got_object_open_as_commit(&commit, repo, id);
1169 if (err)
1170 break;
1171 entry = alloc_commit_queue_entry(commit, id);
1172 if (entry == NULL) {
1173 err = got_error_from_errno("alloc_commit_queue_entry");
1174 break;
1177 errcode = pthread_mutex_lock(&tog_mutex);
1178 if (errcode) {
1179 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1180 break;
1183 entry->idx = commits->ncommits;
1184 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1185 nqueued++;
1186 commits->ncommits++;
1188 errcode = pthread_mutex_unlock(&tog_mutex);
1189 if (errcode && err == NULL)
1190 err = got_error_set_errno(errcode,
1191 "pthread_mutex_unlock");
1194 return err;
1197 static const struct got_error *
1198 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1199 struct got_repository *repo)
1201 const struct got_error *err = NULL;
1202 struct got_reference *head_ref;
1204 *head_id = NULL;
1206 err = got_ref_open(&head_ref, repo, branch_name, 0);
1207 if (err)
1208 return err;
1210 err = got_ref_resolve(head_id, repo, head_ref);
1211 got_ref_close(head_ref);
1212 if (err) {
1213 *head_id = NULL;
1214 return err;
1217 return NULL;
1220 static const struct got_error *
1221 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1222 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1223 struct commit_queue *commits, int selected_idx, int limit,
1224 struct got_reflist_head *refs, const char *path, int commits_needed)
1226 const struct got_error *err = NULL;
1227 struct commit_queue_entry *entry;
1228 int width;
1229 int ncommits, author_cols = 10;
1230 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1231 char *refs_str = NULL;
1232 wchar_t *wline;
1234 entry = first;
1235 ncommits = 0;
1236 while (entry) {
1237 if (ncommits == selected_idx) {
1238 *selected = entry;
1239 break;
1241 entry = TAILQ_NEXT(entry, entry);
1242 ncommits++;
1245 if (*selected && !(view->searching && view->search_next_done == 0)) {
1246 err = got_object_id_str(&id_str, (*selected)->id);
1247 if (err)
1248 return err;
1249 if (refs) {
1250 err = build_refs_str(&refs_str, refs, (*selected)->id);
1251 if (err)
1252 goto done;
1256 if (commits_needed == 0)
1257 halfdelay(10); /* disable fast refresh */
1259 if (asprintf(&ncommits_str, " [%d/%d] %s",
1260 entry ? entry->idx + 1 : 0, commits->ncommits,
1261 commits_needed > 0 ?
1262 (view->searching && view->search_next_done == 0
1263 ? "searching..." : "loading... ") :
1264 (refs_str ? refs_str : "")) == -1) {
1265 err = got_error_from_errno("asprintf");
1266 goto done;
1269 if (path && strcmp(path, "/") != 0) {
1270 if (asprintf(&header, "commit %s %s%s",
1271 id_str ? id_str : "........................................",
1272 path, ncommits_str) == -1) {
1273 err = got_error_from_errno("asprintf");
1274 header = NULL;
1275 goto done;
1277 } else if (asprintf(&header, "commit %s%s",
1278 id_str ? id_str : "........................................",
1279 ncommits_str) == -1) {
1280 err = got_error_from_errno("asprintf");
1281 header = NULL;
1282 goto done;
1284 err = format_line(&wline, &width, header, view->ncols);
1285 if (err)
1286 goto done;
1288 werase(view->window);
1290 if (view_needs_focus_indication(view))
1291 wstandout(view->window);
1292 waddwstr(view->window, wline);
1293 while (width < view->ncols) {
1294 waddch(view->window, ' ');
1295 width++;
1297 if (view_needs_focus_indication(view))
1298 wstandend(view->window);
1299 free(wline);
1300 if (limit <= 1)
1301 goto done;
1303 /* Grow author column size if necessary. */
1304 entry = first;
1305 ncommits = 0;
1306 while (entry) {
1307 char *author;
1308 wchar_t *wauthor;
1309 int width;
1310 if (ncommits >= limit - 1)
1311 break;
1312 author = strdup(got_object_commit_get_author(entry->commit));
1313 if (author == NULL) {
1314 err = got_error_from_errno("strdup");
1315 goto done;
1317 err = format_author(&wauthor, &width, author, COLS);
1318 if (author_cols < width)
1319 author_cols = width;
1320 free(wauthor);
1321 free(author);
1322 entry = TAILQ_NEXT(entry, entry);
1325 entry = first;
1326 *last = first;
1327 ncommits = 0;
1328 while (entry) {
1329 if (ncommits >= limit - 1)
1330 break;
1331 if (ncommits == selected_idx)
1332 wstandout(view->window);
1333 err = draw_commit(view, entry->commit, entry->id, refs,
1334 author_cols);
1335 if (ncommits == selected_idx)
1336 wstandend(view->window);
1337 if (err)
1338 goto done;
1339 ncommits++;
1340 *last = entry;
1341 entry = TAILQ_NEXT(entry, entry);
1344 view_vborder(view);
1345 done:
1346 free(id_str);
1347 free(refs_str);
1348 free(ncommits_str);
1349 free(header);
1350 return err;
1353 static void
1354 scroll_up(struct tog_view *view,
1355 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1356 struct commit_queue *commits)
1358 struct commit_queue_entry *entry;
1359 int nscrolled = 0;
1361 entry = TAILQ_FIRST(&commits->head);
1362 if (*first_displayed_entry == entry)
1363 return;
1365 entry = *first_displayed_entry;
1366 while (entry && nscrolled < maxscroll) {
1367 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1368 if (entry) {
1369 *first_displayed_entry = entry;
1370 nscrolled++;
1375 static const struct got_error *
1376 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1377 pthread_cond_t *need_commits)
1379 int errcode;
1380 int max_wait = 20;
1382 halfdelay(1); /* fast refresh while loading commits */
1384 while (*commits_needed > 0) {
1385 if (*log_complete)
1386 break;
1388 /* Wake the log thread. */
1389 errcode = pthread_cond_signal(need_commits);
1390 if (errcode)
1391 return got_error_set_errno(errcode,
1392 "pthread_cond_signal");
1393 errcode = pthread_mutex_unlock(&tog_mutex);
1394 if (errcode)
1395 return got_error_set_errno(errcode,
1396 "pthread_mutex_unlock");
1397 pthread_yield();
1398 errcode = pthread_mutex_lock(&tog_mutex);
1399 if (errcode)
1400 return got_error_set_errno(errcode,
1401 "pthread_mutex_lock");
1403 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1405 * Thread is not done yet; lose a key press
1406 * and let the user retry... this way the GUI
1407 * remains interactive while logging deep paths
1408 * with few commits in history.
1410 return NULL;
1414 return NULL;
1417 static const struct got_error *
1418 scroll_down(struct tog_view *view,
1419 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1420 struct commit_queue_entry **last_displayed_entry,
1421 struct commit_queue *commits, int *log_complete, int *commits_needed,
1422 pthread_cond_t *need_commits)
1424 const struct got_error *err = NULL;
1425 struct commit_queue_entry *pentry;
1426 int nscrolled = 0;
1428 if (*last_displayed_entry == NULL)
1429 return NULL;
1431 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1432 if (pentry == NULL && !*log_complete) {
1434 * Ask the log thread for required amount of commits
1435 * plus some amount of pre-fetching.
1437 (*commits_needed) += maxscroll + 20;
1438 err = trigger_log_thread(0, commits_needed, log_complete,
1439 need_commits);
1440 if (err)
1441 return err;
1444 do {
1445 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1446 if (pentry == NULL)
1447 break;
1449 *last_displayed_entry = pentry;
1451 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1452 if (pentry == NULL)
1453 break;
1454 *first_displayed_entry = pentry;
1455 } while (++nscrolled < maxscroll);
1457 return err;
1460 static const struct got_error *
1461 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1462 struct got_commit_object *commit, struct got_object_id *commit_id,
1463 struct tog_view *log_view, struct got_reflist_head *refs,
1464 struct got_repository *repo)
1466 const struct got_error *err;
1467 struct got_object_qid *parent_id;
1468 struct tog_view *diff_view;
1470 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1471 if (diff_view == NULL)
1472 return got_error_from_errno("view_open");
1474 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1475 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1476 commit_id, log_view, refs, repo);
1477 if (err == NULL)
1478 *new_view = diff_view;
1479 return err;
1482 static const struct got_error *
1483 tree_view_visit_subtree(struct got_tree_object *subtree,
1484 struct tog_tree_view_state *s)
1486 struct tog_parent_tree *parent;
1488 parent = calloc(1, sizeof(*parent));
1489 if (parent == NULL)
1490 return got_error_from_errno("calloc");
1492 parent->tree = s->tree;
1493 parent->first_displayed_entry = s->first_displayed_entry;
1494 parent->selected_entry = s->selected_entry;
1495 parent->selected = s->selected;
1496 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1497 s->tree = subtree;
1498 s->entries = got_object_tree_get_entries(s->tree);
1499 s->selected = 0;
1500 s->first_displayed_entry = NULL;
1501 return NULL;
1505 static const struct got_error *
1506 browse_commit_tree(struct tog_view **new_view, int begin_x,
1507 struct commit_queue_entry *entry, const char *path,
1508 struct got_reflist_head *refs, struct got_repository *repo)
1510 const struct got_error *err = NULL;
1511 struct got_tree_object *tree;
1512 struct got_tree_entry *te;
1513 struct tog_tree_view_state *s;
1514 struct tog_view *tree_view;
1515 char *slash, *subpath = NULL;
1516 const char *p;
1518 err = got_object_open_as_tree(&tree, repo,
1519 got_object_commit_get_tree_id(entry->commit));
1520 if (err)
1521 return err;
1523 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1524 if (tree_view == NULL)
1525 return got_error_from_errno("view_open");
1527 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1528 if (err) {
1529 got_object_tree_close(tree);
1530 return err;
1532 s = &tree_view->state.tree;
1534 *new_view = tree_view;
1536 /* Walk the path and open corresponding tree objects. */
1537 p = path;
1538 while (*p) {
1539 struct got_object_id *tree_id;
1541 while (p[0] == '/')
1542 p++;
1544 /* Ensure the correct subtree entry is selected. */
1545 slash = strchr(p, '/');
1546 if (slash == NULL)
1547 slash = strchr(p, '\0');
1548 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1549 if (strncmp(p, te->name, slash - p) == 0) {
1550 s->selected_entry = te;
1551 break;
1553 s->selected++;
1555 if (s->selected_entry == NULL) {
1556 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1557 break;
1559 if (s->tree != s->root)
1560 s->selected++; /* skip '..' */
1562 if (!S_ISDIR(s->selected_entry->mode)) {
1563 /* Jump to this file's entry. */
1564 s->first_displayed_entry = s->selected_entry;
1565 s->selected = 0;
1566 break;
1569 slash = strchr(p, '/');
1570 if (slash)
1571 subpath = strndup(path, slash - path);
1572 else
1573 subpath = strdup(path);
1574 if (subpath == NULL) {
1575 err = got_error_from_errno("strdup");
1576 break;
1579 err = got_object_id_by_path(&tree_id, repo, entry->id,
1580 subpath);
1581 if (err)
1582 break;
1584 err = got_object_open_as_tree(&tree, repo, tree_id);
1585 free(tree_id);
1586 if (err)
1587 break;
1589 err = tree_view_visit_subtree(tree, s);
1590 if (err) {
1591 got_object_tree_close(tree);
1592 break;
1594 if (slash == NULL)
1595 break;
1596 free(subpath);
1597 subpath = NULL;
1598 p = slash;
1601 free(subpath);
1602 return err;
1605 static void *
1606 log_thread(void *arg)
1608 const struct got_error *err = NULL;
1609 int errcode = 0;
1610 struct tog_log_thread_args *a = arg;
1611 int done = 0;
1613 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1614 if (err)
1615 return (void *)err;
1617 while (!done && !err) {
1618 err = queue_commits(a->graph, a->commits, 1, a->repo,
1619 a->in_repo_path);
1620 if (err) {
1621 if (err->code != GOT_ERR_ITER_COMPLETED)
1622 return (void *)err;
1623 err = NULL;
1624 done = 1;
1625 } else if (a->commits_needed > 0)
1626 a->commits_needed--;
1628 errcode = pthread_mutex_lock(&tog_mutex);
1629 if (errcode) {
1630 err = got_error_set_errno(errcode,
1631 "pthread_mutex_lock");
1632 break;
1633 } else if (*a->quit)
1634 done = 1;
1635 else if (*a->first_displayed_entry == NULL) {
1636 *a->first_displayed_entry =
1637 TAILQ_FIRST(&a->commits->head);
1638 *a->selected_entry = *a->first_displayed_entry;
1641 if (done)
1642 a->commits_needed = 0;
1643 else if (a->commits_needed == 0) {
1644 errcode = pthread_cond_wait(&a->need_commits,
1645 &tog_mutex);
1646 if (errcode)
1647 err = got_error_set_errno(errcode,
1648 "pthread_cond_wait");
1651 errcode = pthread_mutex_unlock(&tog_mutex);
1652 if (errcode && err == NULL)
1653 err = got_error_set_errno(errcode,
1654 "pthread_mutex_unlock");
1656 a->log_complete = 1;
1657 return (void *)err;
1660 static const struct got_error *
1661 stop_log_thread(struct tog_log_view_state *s)
1663 const struct got_error *err = NULL;
1664 int errcode;
1666 if (s->thread) {
1667 s->quit = 1;
1668 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1669 if (errcode)
1670 return got_error_set_errno(errcode,
1671 "pthread_cond_signal");
1672 errcode = pthread_mutex_unlock(&tog_mutex);
1673 if (errcode)
1674 return got_error_set_errno(errcode,
1675 "pthread_mutex_unlock");
1676 errcode = pthread_join(s->thread, (void **)&err);
1677 if (errcode)
1678 return got_error_set_errno(errcode, "pthread_join");
1679 errcode = pthread_mutex_lock(&tog_mutex);
1680 if (errcode)
1681 return got_error_set_errno(errcode,
1682 "pthread_mutex_lock");
1683 s->thread = NULL;
1686 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1687 if (errcode && err == NULL)
1688 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1690 if (s->thread_args.repo) {
1691 got_repo_close(s->thread_args.repo);
1692 s->thread_args.repo = NULL;
1695 if (s->thread_args.graph) {
1696 got_commit_graph_close(s->thread_args.graph);
1697 s->thread_args.graph = NULL;
1700 return err;
1703 static const struct got_error *
1704 close_log_view(struct tog_view *view)
1706 const struct got_error *err = NULL;
1707 struct tog_log_view_state *s = &view->state.log;
1709 err = stop_log_thread(s);
1710 free_commits(&s->commits);
1711 free(s->in_repo_path);
1712 s->in_repo_path = NULL;
1713 free(s->start_id);
1714 s->start_id = NULL;
1715 return err;
1718 static const struct got_error *
1719 search_start_log_view(struct tog_view *view)
1721 struct tog_log_view_state *s = &view->state.log;
1723 s->matched_entry = NULL;
1724 s->search_entry = NULL;
1725 return NULL;
1728 static int
1729 match_commit(struct got_commit_object *commit, const char *id_str,
1730 regex_t *regex)
1732 regmatch_t regmatch;
1734 if (regexec(regex, got_object_commit_get_author(commit), 1,
1735 &regmatch, 0) == 0 ||
1736 regexec(regex, got_object_commit_get_committer(commit), 1,
1737 &regmatch, 0) == 0 ||
1738 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1739 &regmatch, 0) == 0 ||
1740 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1741 return 1;
1743 return 0;
1746 static const struct got_error *
1747 search_next_log_view(struct tog_view *view)
1749 const struct got_error *err = NULL;
1750 struct tog_log_view_state *s = &view->state.log;
1751 struct commit_queue_entry *entry;
1753 if (!view->searching) {
1754 view->search_next_done = 1;
1755 return NULL;
1758 if (s->search_entry) {
1759 if (wgetch(view->window) == KEY_BACKSPACE) {
1760 view->search_next_done = 1;
1761 return NULL;
1763 if (view->searching == TOG_SEARCH_FORWARD)
1764 entry = TAILQ_NEXT(s->search_entry, entry);
1765 else
1766 entry = TAILQ_PREV(s->search_entry,
1767 commit_queue_head, entry);
1768 } else if (s->matched_entry) {
1769 if (view->searching == TOG_SEARCH_FORWARD)
1770 entry = TAILQ_NEXT(s->selected_entry, entry);
1771 else
1772 entry = TAILQ_PREV(s->selected_entry,
1773 commit_queue_head, entry);
1774 } else {
1775 if (view->searching == TOG_SEARCH_FORWARD)
1776 entry = TAILQ_FIRST(&s->commits.head);
1777 else
1778 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1781 while (1) {
1782 char *id_str;
1783 if (entry == NULL) {
1784 if (s->thread_args.log_complete ||
1785 view->searching == TOG_SEARCH_BACKWARD) {
1786 view->search_next_done = 1;
1787 return NULL;
1790 * Poke the log thread for more commits and return,
1791 * allowing the main loop to make progress. Search
1792 * will resume at s->search_entry once we come back.
1794 s->thread_args.commits_needed++;
1795 return trigger_log_thread(1,
1796 &s->thread_args.commits_needed,
1797 &s->thread_args.log_complete,
1798 &s->thread_args.need_commits);
1801 err = got_object_id_str(&id_str, entry->id);
1802 if (err)
1803 return err;
1805 if (match_commit(entry->commit, id_str, &view->regex)) {
1806 view->search_next_done = 1;
1807 s->matched_entry = entry;
1808 free(id_str);
1809 break;
1811 free(id_str);
1812 s->search_entry = entry;
1813 if (view->searching == TOG_SEARCH_FORWARD)
1814 entry = TAILQ_NEXT(entry, entry);
1815 else
1816 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1819 if (s->matched_entry) {
1820 int cur = s->selected_entry->idx;
1821 while (cur < s->matched_entry->idx) {
1822 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1823 if (err)
1824 return err;
1825 cur++;
1827 while (cur > s->matched_entry->idx) {
1828 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1829 if (err)
1830 return err;
1831 cur--;
1835 s->search_entry = NULL;
1837 return NULL;
1840 static const struct got_error *
1841 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1842 struct got_reflist_head *refs, struct got_repository *repo,
1843 const char *head_ref_name, const char *path, int check_disk)
1845 const struct got_error *err = NULL;
1846 struct tog_log_view_state *s = &view->state.log;
1847 struct got_repository *thread_repo = NULL;
1848 struct got_commit_graph *thread_graph = NULL;
1849 int errcode;
1851 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1852 if (err != NULL)
1853 goto done;
1855 /* The commit queue only contains commits being displayed. */
1856 TAILQ_INIT(&s->commits.head);
1857 s->commits.ncommits = 0;
1859 s->refs = refs;
1860 s->repo = repo;
1861 s->head_ref_name = head_ref_name;
1862 s->start_id = got_object_id_dup(start_id);
1863 if (s->start_id == NULL) {
1864 err = got_error_from_errno("got_object_id_dup");
1865 goto done;
1868 view->show = show_log_view;
1869 view->input = input_log_view;
1870 view->close = close_log_view;
1871 view->search_start = search_start_log_view;
1872 view->search_next = search_next_log_view;
1874 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1875 if (err)
1876 goto done;
1877 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1878 0, thread_repo);
1879 if (err)
1880 goto done;
1882 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1883 if (errcode) {
1884 err = got_error_set_errno(errcode, "pthread_cond_init");
1885 goto done;
1888 s->thread_args.commits_needed = view->nlines;
1889 s->thread_args.graph = thread_graph;
1890 s->thread_args.commits = &s->commits;
1891 s->thread_args.in_repo_path = s->in_repo_path;
1892 s->thread_args.start_id = s->start_id;
1893 s->thread_args.repo = thread_repo;
1894 s->thread_args.log_complete = 0;
1895 s->thread_args.quit = &s->quit;
1896 s->thread_args.view = view;
1897 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1898 s->thread_args.selected_entry = &s->selected_entry;
1899 done:
1900 if (err)
1901 close_log_view(view);
1902 return err;
1905 static const struct got_error *
1906 show_log_view(struct tog_view *view)
1908 struct tog_log_view_state *s = &view->state.log;
1910 if (s->thread == NULL) {
1911 int errcode = pthread_create(&s->thread, NULL, log_thread,
1912 &s->thread_args);
1913 if (errcode)
1914 return got_error_set_errno(errcode, "pthread_create");
1917 return draw_commits(view, &s->last_displayed_entry,
1918 &s->selected_entry, s->first_displayed_entry,
1919 &s->commits, s->selected, view->nlines, s->refs,
1920 s->in_repo_path, s->thread_args.commits_needed);
1923 static const struct got_error *
1924 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1925 struct tog_view **focus_view, struct tog_view *view, int ch)
1927 const struct got_error *err = NULL;
1928 struct tog_log_view_state *s = &view->state.log;
1929 char *parent_path, *in_repo_path = NULL;
1930 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1931 int begin_x = 0;
1932 struct got_object_id *start_id;
1934 switch (ch) {
1935 case 'q':
1936 s->quit = 1;
1937 break;
1938 case 'k':
1939 case KEY_UP:
1940 case '<':
1941 case ',':
1942 if (s->first_displayed_entry == NULL)
1943 break;
1944 if (s->selected > 0)
1945 s->selected--;
1946 else
1947 scroll_up(view, &s->first_displayed_entry, 1,
1948 &s->commits);
1949 break;
1950 case KEY_PPAGE:
1951 case CTRL('b'):
1952 if (s->first_displayed_entry == NULL)
1953 break;
1954 if (TAILQ_FIRST(&s->commits.head) ==
1955 s->first_displayed_entry) {
1956 s->selected = 0;
1957 break;
1959 scroll_up(view, &s->first_displayed_entry,
1960 view->nlines, &s->commits);
1961 break;
1962 case 'j':
1963 case KEY_DOWN:
1964 case '>':
1965 case '.':
1966 if (s->first_displayed_entry == NULL)
1967 break;
1968 if (s->selected < MIN(view->nlines - 2,
1969 s->commits.ncommits - 1)) {
1970 s->selected++;
1971 break;
1973 err = scroll_down(view, &s->first_displayed_entry, 1,
1974 &s->last_displayed_entry, &s->commits,
1975 &s->thread_args.log_complete,
1976 &s->thread_args.commits_needed,
1977 &s->thread_args.need_commits);
1978 break;
1979 case KEY_NPAGE:
1980 case CTRL('f'): {
1981 struct commit_queue_entry *first;
1982 first = s->first_displayed_entry;
1983 if (first == NULL)
1984 break;
1985 err = scroll_down(view, &s->first_displayed_entry,
1986 view->nlines, &s->last_displayed_entry,
1987 &s->commits, &s->thread_args.log_complete,
1988 &s->thread_args.commits_needed,
1989 &s->thread_args.need_commits);
1990 if (first == s->first_displayed_entry &&
1991 s->selected < MIN(view->nlines - 2,
1992 s->commits.ncommits - 1)) {
1993 /* can't scroll further down */
1994 s->selected = MIN(view->nlines - 2,
1995 s->commits.ncommits - 1);
1997 err = NULL;
1998 break;
2000 case KEY_RESIZE:
2001 if (s->selected > view->nlines - 2)
2002 s->selected = view->nlines - 2;
2003 if (s->selected > s->commits.ncommits - 1)
2004 s->selected = s->commits.ncommits - 1;
2005 break;
2006 case KEY_ENTER:
2007 case ' ':
2008 case '\r':
2009 if (s->selected_entry == NULL)
2010 break;
2011 if (view_is_parent_view(view))
2012 begin_x = view_split_begin_x(view->begin_x);
2013 err = open_diff_view_for_commit(&diff_view, begin_x,
2014 s->selected_entry->commit, s->selected_entry->id,
2015 view, s->refs, s->repo);
2016 if (err)
2017 break;
2018 if (view_is_parent_view(view)) {
2019 err = view_close_child(view);
2020 if (err)
2021 return err;
2022 err = view_set_child(view, diff_view);
2023 if (err) {
2024 view_close(diff_view);
2025 break;
2027 *focus_view = diff_view;
2028 view->child_focussed = 1;
2029 } else
2030 *new_view = diff_view;
2031 break;
2032 case 't':
2033 if (s->selected_entry == NULL)
2034 break;
2035 if (view_is_parent_view(view))
2036 begin_x = view_split_begin_x(view->begin_x);
2037 err = browse_commit_tree(&tree_view, begin_x,
2038 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2039 if (err)
2040 break;
2041 if (view_is_parent_view(view)) {
2042 err = view_close_child(view);
2043 if (err)
2044 return err;
2045 err = view_set_child(view, tree_view);
2046 if (err) {
2047 view_close(tree_view);
2048 break;
2050 *focus_view = tree_view;
2051 view->child_focussed = 1;
2052 } else
2053 *new_view = tree_view;
2054 break;
2055 case KEY_BACKSPACE:
2056 if (strcmp(s->in_repo_path, "/") == 0)
2057 break;
2058 parent_path = dirname(s->in_repo_path);
2059 if (parent_path && strcmp(parent_path, ".") != 0) {
2060 err = stop_log_thread(s);
2061 if (err)
2062 return err;
2063 lv = view_open(view->nlines, view->ncols,
2064 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2065 if (lv == NULL)
2066 return got_error_from_errno(
2067 "view_open");
2068 err = open_log_view(lv, s->start_id, s->refs,
2069 s->repo, s->head_ref_name, parent_path, 0);
2070 if (err)
2071 return err;;
2072 if (view_is_parent_view(view))
2073 *new_view = lv;
2074 else {
2075 view_set_child(view->parent, lv);
2076 *focus_view = lv;
2078 return NULL;
2080 break;
2081 case CTRL('l'):
2082 err = stop_log_thread(s);
2083 if (err)
2084 return err;
2085 lv = view_open(view->nlines, view->ncols,
2086 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2087 if (lv == NULL)
2088 return got_error_from_errno("view_open");
2089 err = get_head_commit_id(&start_id, s->head_ref_name ?
2090 s->head_ref_name : GOT_REF_HEAD, s->repo);
2091 if (err) {
2092 view_close(lv);
2093 return err;
2095 in_repo_path = strdup(s->in_repo_path);
2096 if (in_repo_path == NULL) {
2097 free(start_id);
2098 view_close(lv);
2099 return got_error_from_errno("strdup");
2101 err = open_log_view(lv, start_id, s->refs, s->repo,
2102 s->head_ref_name, in_repo_path, 0);
2103 if (err) {
2104 free(start_id);
2105 view_close(lv);
2106 return err;;
2108 *dead_view = view;
2109 *new_view = lv;
2110 break;
2111 default:
2112 break;
2115 return err;
2118 static const struct got_error *
2119 apply_unveil(const char *repo_path, const char *worktree_path)
2121 const struct got_error *error;
2123 #ifdef PROFILE
2124 if (unveil("gmon.out", "rwc") != 0)
2125 return got_error_from_errno2("unveil", "gmon.out");
2126 #endif
2127 if (repo_path && unveil(repo_path, "r") != 0)
2128 return got_error_from_errno2("unveil", repo_path);
2130 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2131 return got_error_from_errno2("unveil", worktree_path);
2133 if (unveil("/tmp", "rwc") != 0)
2134 return got_error_from_errno2("unveil", "/tmp");
2136 error = got_privsep_unveil_exec_helpers();
2137 if (error != NULL)
2138 return error;
2140 if (unveil(NULL, NULL) != 0)
2141 return got_error_from_errno("unveil");
2143 return NULL;
2146 static void
2147 init_curses(void)
2149 initscr();
2150 cbreak();
2151 halfdelay(1); /* Do fast refresh while initial view is loading. */
2152 noecho();
2153 nonl();
2154 intrflush(stdscr, FALSE);
2155 keypad(stdscr, TRUE);
2156 curs_set(0);
2157 signal(SIGWINCH, tog_sigwinch);
2160 static const struct got_error *
2161 cmd_log(int argc, char *argv[])
2163 const struct got_error *error;
2164 struct got_repository *repo = NULL;
2165 struct got_worktree *worktree = NULL;
2166 struct got_reflist_head refs;
2167 struct got_object_id *start_id = NULL;
2168 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2169 char *start_commit = NULL;
2170 int ch;
2171 struct tog_view *view;
2173 SIMPLEQ_INIT(&refs);
2175 #ifndef PROFILE
2176 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2177 NULL) == -1)
2178 err(1, "pledge");
2179 #endif
2181 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2182 switch (ch) {
2183 case 'c':
2184 start_commit = optarg;
2185 break;
2186 case 'r':
2187 repo_path = realpath(optarg, NULL);
2188 if (repo_path == NULL)
2189 err(1, "-r option");
2190 break;
2191 default:
2192 usage_log();
2193 /* NOTREACHED */
2197 argc -= optind;
2198 argv += optind;
2200 cwd = getcwd(NULL, 0);
2201 if (cwd == NULL) {
2202 error = got_error_from_errno("getcwd");
2203 goto done;
2205 error = got_worktree_open(&worktree, cwd);
2206 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2207 goto done;
2208 error = NULL;
2210 if (argc == 0) {
2211 path = strdup("");
2212 if (path == NULL) {
2213 error = got_error_from_errno("strdup");
2214 goto done;
2216 } else if (argc == 1) {
2217 if (worktree) {
2218 error = got_worktree_resolve_path(&path, worktree,
2219 argv[0]);
2220 if (error)
2221 goto done;
2222 } else {
2223 path = strdup(argv[0]);
2224 if (path == NULL) {
2225 error = got_error_from_errno("strdup");
2226 goto done;
2229 } else
2230 usage_log();
2232 if (repo_path == NULL) {
2233 if (worktree)
2234 repo_path = strdup(
2235 got_worktree_get_repo_path(worktree));
2236 else
2237 repo_path = strdup(cwd);
2239 if (repo_path == NULL) {
2240 error = got_error_from_errno("strdup");
2241 goto done;
2244 init_curses();
2246 error = got_repo_open(&repo, repo_path);
2247 if (error != NULL)
2248 goto done;
2250 error = apply_unveil(got_repo_get_path(repo),
2251 worktree ? got_worktree_get_root_path(worktree) : NULL);
2252 if (error)
2253 goto done;
2255 if (start_commit == NULL)
2256 error = get_head_commit_id(&start_id, worktree ?
2257 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2258 repo);
2259 else {
2260 error = get_head_commit_id(&start_id, start_commit, repo);
2261 if (error) {
2262 if (error->code != GOT_ERR_NOT_REF)
2263 goto done;
2264 error = got_repo_match_object_id_prefix(&start_id,
2265 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2268 if (error != NULL)
2269 goto done;
2271 error = got_ref_list(&refs, repo);
2272 if (error)
2273 goto done;
2275 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2276 if (view == NULL) {
2277 error = got_error_from_errno("view_open");
2278 goto done;
2280 error = open_log_view(view, start_id, &refs, repo, worktree ?
2281 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2282 if (error)
2283 goto done;
2284 error = view_loop(view);
2285 done:
2286 free(repo_path);
2287 free(cwd);
2288 free(path);
2289 free(start_id);
2290 if (repo)
2291 got_repo_close(repo);
2292 if (worktree)
2293 got_worktree_close(worktree);
2294 got_ref_list_free(&refs);
2295 return error;
2298 __dead static void
2299 usage_diff(void)
2301 endwin();
2302 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2303 getprogname());
2304 exit(1);
2307 static char *
2308 parse_next_line(FILE *f, size_t *len)
2310 char *line;
2311 size_t linelen;
2312 size_t lineno;
2313 const char delim[3] = { '\0', '\0', '\0'};
2315 line = fparseln(f, &linelen, &lineno, delim, 0);
2316 if (len)
2317 *len = linelen;
2318 return line;
2321 static const struct got_error *
2322 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2323 int *last_displayed_line, int *eof, int max_lines,
2324 char *header)
2326 const struct got_error *err;
2327 int nlines = 0, nprinted = 0;
2328 char *line;
2329 size_t len;
2330 wchar_t *wline;
2331 int width;
2333 rewind(f);
2334 werase(view->window);
2336 if (header) {
2337 err = format_line(&wline, &width, header, view->ncols);
2338 if (err) {
2339 return err;
2342 if (view_needs_focus_indication(view))
2343 wstandout(view->window);
2344 waddwstr(view->window, wline);
2345 if (view_needs_focus_indication(view))
2346 wstandend(view->window);
2347 if (width < view->ncols - 1)
2348 waddch(view->window, '\n');
2350 if (max_lines <= 1)
2351 return NULL;
2352 max_lines--;
2355 *eof = 0;
2356 while (nprinted < max_lines) {
2357 line = parse_next_line(f, &len);
2358 if (line == NULL) {
2359 *eof = 1;
2360 break;
2362 if (++nlines < *first_displayed_line) {
2363 free(line);
2364 continue;
2367 err = format_line(&wline, &width, line, view->ncols);
2368 if (err) {
2369 free(line);
2370 return err;
2372 waddwstr(view->window, wline);
2373 if (width < view->ncols - 1)
2374 waddch(view->window, '\n');
2375 if (++nprinted == 1)
2376 *first_displayed_line = nlines;
2377 free(line);
2378 free(wline);
2379 wline = NULL;
2381 *last_displayed_line = nlines;
2383 view_vborder(view);
2385 if (*eof) {
2386 while (nprinted < view->nlines) {
2387 waddch(view->window, '\n');
2388 nprinted++;
2391 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2392 if (err) {
2393 return err;
2396 wstandout(view->window);
2397 waddwstr(view->window, wline);
2398 wstandend(view->window);
2401 return NULL;
2404 static char *
2405 get_datestr(time_t *time, char *datebuf)
2407 char *p, *s = ctime_r(time, datebuf);
2408 p = strchr(s, '\n');
2409 if (p)
2410 *p = '\0';
2411 return s;
2414 static const struct got_error *
2415 write_commit_info(struct got_object_id *commit_id,
2416 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2418 const struct got_error *err = NULL;
2419 char datebuf[26];
2420 struct got_commit_object *commit;
2421 char *id_str = NULL;
2422 time_t committer_time;
2423 const char *author, *committer;
2424 char *refs_str = NULL;
2426 if (refs) {
2427 err = build_refs_str(&refs_str, refs, commit_id);
2428 if (err)
2429 return err;
2432 err = got_object_open_as_commit(&commit, repo, commit_id);
2433 if (err)
2434 return err;
2436 err = got_object_id_str(&id_str, commit_id);
2437 if (err) {
2438 err = got_error_from_errno("got_object_id_str");
2439 goto done;
2442 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2443 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2444 err = got_error_from_errno("fprintf");
2445 goto done;
2447 if (fprintf(outfile, "from: %s\n",
2448 got_object_commit_get_author(commit)) < 0) {
2449 err = got_error_from_errno("fprintf");
2450 goto done;
2452 committer_time = got_object_commit_get_committer_time(commit);
2453 if (fprintf(outfile, "date: %s UTC\n",
2454 get_datestr(&committer_time, datebuf)) < 0) {
2455 err = got_error_from_errno("fprintf");
2456 goto done;
2458 author = got_object_commit_get_author(commit);
2459 committer = got_object_commit_get_committer(commit);
2460 if (strcmp(author, committer) != 0 &&
2461 fprintf(outfile, "via: %s\n", committer) < 0) {
2462 err = got_error_from_errno("fprintf");
2463 goto done;
2465 if (fprintf(outfile, "%s\n",
2466 got_object_commit_get_logmsg(commit)) < 0) {
2467 err = got_error_from_errno("fprintf");
2468 goto done;
2470 done:
2471 free(id_str);
2472 free(refs_str);
2473 got_object_commit_close(commit);
2474 return err;
2477 static const struct got_error *
2478 create_diff(struct tog_diff_view_state *s)
2480 const struct got_error *err = NULL;
2481 FILE *f = NULL;
2482 int obj_type;
2484 f = got_opentemp();
2485 if (f == NULL) {
2486 err = got_error_from_errno("got_opentemp");
2487 goto done;
2489 if (s->f && fclose(s->f) != 0) {
2490 err = got_error_from_errno("fclose");
2491 goto done;
2493 s->f = f;
2495 if (s->id1)
2496 err = got_object_get_type(&obj_type, s->repo, s->id1);
2497 else
2498 err = got_object_get_type(&obj_type, s->repo, s->id2);
2499 if (err)
2500 goto done;
2502 switch (obj_type) {
2503 case GOT_OBJ_TYPE_BLOB:
2504 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2505 s->diff_context, s->repo, f);
2506 break;
2507 case GOT_OBJ_TYPE_TREE:
2508 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2509 s->diff_context, s->repo, f);
2510 break;
2511 case GOT_OBJ_TYPE_COMMIT: {
2512 const struct got_object_id_queue *parent_ids;
2513 struct got_object_qid *pid;
2514 struct got_commit_object *commit2;
2516 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2517 if (err)
2518 break;
2519 /* Show commit info if we're diffing to a parent/root commit. */
2520 if (s->id1 == NULL)
2521 write_commit_info(s->id2, s->refs, s->repo, f);
2522 else {
2523 parent_ids = got_object_commit_get_parent_ids(commit2);
2524 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2525 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2526 write_commit_info(s->id2, s->refs,
2527 s->repo, f);
2528 break;
2532 got_object_commit_close(commit2);
2534 err = got_diff_objects_as_commits(s->id1, s->id2,
2535 s->diff_context, s->repo, f);
2536 break;
2538 default:
2539 err = got_error(GOT_ERR_OBJ_TYPE);
2540 break;
2542 done:
2543 if (f && fflush(f) != 0 && err == NULL)
2544 err = got_error_from_errno("fflush");
2545 return err;
2548 static void
2549 diff_view_indicate_progress(struct tog_view *view)
2551 mvwaddstr(view->window, 0, 0, "diffing...");
2552 update_panels();
2553 doupdate();
2556 static const struct got_error *
2557 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2558 struct got_object_id *id2, struct tog_view *log_view,
2559 struct got_reflist_head *refs, struct got_repository *repo)
2561 const struct got_error *err;
2563 if (id1 != NULL && id2 != NULL) {
2564 int type1, type2;
2565 err = got_object_get_type(&type1, repo, id1);
2566 if (err)
2567 return err;
2568 err = got_object_get_type(&type2, repo, id2);
2569 if (err)
2570 return err;
2572 if (type1 != type2)
2573 return got_error(GOT_ERR_OBJ_TYPE);
2576 if (id1) {
2577 view->state.diff.id1 = got_object_id_dup(id1);
2578 if (view->state.diff.id1 == NULL)
2579 return got_error_from_errno("got_object_id_dup");
2580 } else
2581 view->state.diff.id1 = NULL;
2583 view->state.diff.id2 = got_object_id_dup(id2);
2584 if (view->state.diff.id2 == NULL) {
2585 free(view->state.diff.id1);
2586 view->state.diff.id1 = NULL;
2587 return got_error_from_errno("got_object_id_dup");
2589 view->state.diff.f = NULL;
2590 view->state.diff.first_displayed_line = 1;
2591 view->state.diff.last_displayed_line = view->nlines;
2592 view->state.diff.diff_context = 3;
2593 view->state.diff.log_view = log_view;
2594 view->state.diff.repo = repo;
2595 view->state.diff.refs = refs;
2597 if (log_view && view_is_splitscreen(view))
2598 show_log_view(log_view); /* draw vborder */
2599 diff_view_indicate_progress(view);
2601 err = create_diff(&view->state.diff);
2602 if (err) {
2603 free(view->state.diff.id1);
2604 view->state.diff.id1 = NULL;
2605 free(view->state.diff.id2);
2606 view->state.diff.id2 = NULL;
2607 return err;
2610 view->show = show_diff_view;
2611 view->input = input_diff_view;
2612 view->close = close_diff_view;
2614 return NULL;
2617 static const struct got_error *
2618 close_diff_view(struct tog_view *view)
2620 const struct got_error *err = NULL;
2622 free(view->state.diff.id1);
2623 view->state.diff.id1 = NULL;
2624 free(view->state.diff.id2);
2625 view->state.diff.id2 = NULL;
2626 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2627 err = got_error_from_errno("fclose");
2628 return err;
2631 static const struct got_error *
2632 show_diff_view(struct tog_view *view)
2634 const struct got_error *err;
2635 struct tog_diff_view_state *s = &view->state.diff;
2636 char *id_str1 = NULL, *id_str2, *header;
2638 if (s->id1) {
2639 err = got_object_id_str(&id_str1, s->id1);
2640 if (err)
2641 return err;
2643 err = got_object_id_str(&id_str2, s->id2);
2644 if (err)
2645 return err;
2647 if (asprintf(&header, "diff %s %s",
2648 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2649 err = got_error_from_errno("asprintf");
2650 free(id_str1);
2651 free(id_str2);
2652 return err;
2654 free(id_str1);
2655 free(id_str2);
2657 return draw_file(view, s->f, &s->first_displayed_line,
2658 &s->last_displayed_line, &s->eof, view->nlines,
2659 header);
2662 static const struct got_error *
2663 set_selected_commit(struct tog_diff_view_state *s,
2664 struct commit_queue_entry *entry)
2666 const struct got_error *err;
2667 const struct got_object_id_queue *parent_ids;
2668 struct got_commit_object *selected_commit;
2669 struct got_object_qid *pid;
2671 free(s->id2);
2672 s->id2 = got_object_id_dup(entry->id);
2673 if (s->id2 == NULL)
2674 return got_error_from_errno("got_object_id_dup");
2676 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2677 if (err)
2678 return err;
2679 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2680 free(s->id1);
2681 pid = SIMPLEQ_FIRST(parent_ids);
2682 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2683 got_object_commit_close(selected_commit);
2684 return NULL;
2687 static const struct got_error *
2688 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2689 struct tog_view **focus_view, struct tog_view *view, int ch)
2691 const struct got_error *err = NULL;
2692 struct tog_diff_view_state *s = &view->state.diff;
2693 struct tog_log_view_state *ls;
2694 struct commit_queue_entry *entry;
2695 int i;
2697 switch (ch) {
2698 case 'k':
2699 case KEY_UP:
2700 if (s->first_displayed_line > 1)
2701 s->first_displayed_line--;
2702 break;
2703 case KEY_PPAGE:
2704 case CTRL('b'):
2705 if (s->first_displayed_line == 1)
2706 break;
2707 i = 0;
2708 while (i++ < view->nlines - 1 &&
2709 s->first_displayed_line > 1)
2710 s->first_displayed_line--;
2711 break;
2712 case 'j':
2713 case KEY_DOWN:
2714 if (!s->eof)
2715 s->first_displayed_line++;
2716 break;
2717 case KEY_NPAGE:
2718 case CTRL('f'):
2719 case ' ':
2720 if (s->eof)
2721 break;
2722 i = 0;
2723 while (!s->eof && i++ < view->nlines - 1) {
2724 char *line;
2725 line = parse_next_line(s->f, NULL);
2726 s->first_displayed_line++;
2727 if (line == NULL)
2728 break;
2730 break;
2731 case '[':
2732 if (s->diff_context > 0) {
2733 s->diff_context--;
2734 diff_view_indicate_progress(view);
2735 err = create_diff(s);
2737 break;
2738 case ']':
2739 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2740 s->diff_context++;
2741 diff_view_indicate_progress(view);
2742 err = create_diff(s);
2744 break;
2745 case '<':
2746 case ',':
2747 if (s->log_view == NULL)
2748 break;
2749 ls = &s->log_view->state.log;
2750 entry = TAILQ_PREV(ls->selected_entry,
2751 commit_queue_head, entry);
2752 if (entry == NULL)
2753 break;
2755 err = input_log_view(NULL, NULL, NULL, s->log_view,
2756 KEY_UP);
2757 if (err)
2758 break;
2760 err = set_selected_commit(s, entry);
2761 if (err)
2762 break;
2764 s->first_displayed_line = 1;
2765 s->last_displayed_line = view->nlines;
2767 diff_view_indicate_progress(view);
2768 err = create_diff(s);
2769 break;
2770 case '>':
2771 case '.':
2772 if (s->log_view == NULL)
2773 break;
2774 ls = &s->log_view->state.log;
2776 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2777 ls->thread_args.commits_needed++;
2779 /* Display "loading..." in log view. */
2780 show_log_view(s->log_view);
2781 update_panels();
2782 doupdate();
2784 err = trigger_log_thread(1 /* load_all */,
2785 &ls->thread_args.commits_needed,
2786 &ls->thread_args.log_complete,
2787 &ls->thread_args.need_commits);
2788 if (err)
2789 break;
2791 err = input_log_view(NULL, NULL, NULL, s->log_view,
2792 KEY_DOWN);
2793 if (err)
2794 break;
2796 entry = TAILQ_NEXT(ls->selected_entry, entry);
2797 if (entry == NULL)
2798 break;
2800 err = set_selected_commit(s, entry);
2801 if (err)
2802 break;
2804 s->first_displayed_line = 1;
2805 s->last_displayed_line = view->nlines;
2807 diff_view_indicate_progress(view);
2808 err = create_diff(s);
2809 break;
2810 default:
2811 break;
2814 return err;
2817 static const struct got_error *
2818 cmd_diff(int argc, char *argv[])
2820 const struct got_error *error = NULL;
2821 struct got_repository *repo = NULL;
2822 struct got_reflist_head refs;
2823 struct got_object_id *id1 = NULL, *id2 = NULL;
2824 char *repo_path = NULL;
2825 char *id_str1 = NULL, *id_str2 = NULL;
2826 int ch;
2827 struct tog_view *view;
2829 SIMPLEQ_INIT(&refs);
2831 #ifndef PROFILE
2832 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2833 NULL) == -1)
2834 err(1, "pledge");
2835 #endif
2837 while ((ch = getopt(argc, argv, "")) != -1) {
2838 switch (ch) {
2839 default:
2840 usage_diff();
2841 /* NOTREACHED */
2845 argc -= optind;
2846 argv += optind;
2848 if (argc == 0) {
2849 usage_diff(); /* TODO show local worktree changes */
2850 } else if (argc == 2) {
2851 repo_path = getcwd(NULL, 0);
2852 if (repo_path == NULL)
2853 return got_error_from_errno("getcwd");
2854 id_str1 = argv[0];
2855 id_str2 = argv[1];
2856 } else if (argc == 3) {
2857 repo_path = realpath(argv[0], NULL);
2858 if (repo_path == NULL)
2859 return got_error_from_errno2("realpath", argv[0]);
2860 id_str1 = argv[1];
2861 id_str2 = argv[2];
2862 } else
2863 usage_diff();
2865 init_curses();
2867 error = got_repo_open(&repo, repo_path);
2868 if (error)
2869 goto done;
2871 error = apply_unveil(got_repo_get_path(repo), NULL);
2872 if (error)
2873 goto done;
2875 error = got_repo_match_object_id_prefix(&id1, id_str1,
2876 GOT_OBJ_TYPE_ANY, repo);
2877 if (error)
2878 goto done;
2880 error = got_repo_match_object_id_prefix(&id2, id_str2,
2881 GOT_OBJ_TYPE_ANY, repo);
2882 if (error)
2883 goto done;
2885 error = got_ref_list(&refs, repo);
2886 if (error)
2887 goto done;
2889 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2890 if (view == NULL) {
2891 error = got_error_from_errno("view_open");
2892 goto done;
2894 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2895 if (error)
2896 goto done;
2897 error = view_loop(view);
2898 done:
2899 free(repo_path);
2900 if (repo)
2901 got_repo_close(repo);
2902 got_ref_list_free(&refs);
2903 return error;
2906 __dead static void
2907 usage_blame(void)
2909 endwin();
2910 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2911 getprogname());
2912 exit(1);
2915 struct tog_blame_line {
2916 int annotated;
2917 struct got_object_id *id;
2920 static const struct got_error *
2921 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2922 const char *path, struct tog_blame_line *lines, int nlines,
2923 int blame_complete, int selected_line, int *first_displayed_line,
2924 int *last_displayed_line, int *eof, int max_lines)
2926 const struct got_error *err;
2927 int lineno = 0, nprinted = 0;
2928 char *line;
2929 size_t len;
2930 wchar_t *wline;
2931 int width, wlimit;
2932 struct tog_blame_line *blame_line;
2933 struct got_object_id *prev_id = NULL;
2934 char *id_str;
2936 err = got_object_id_str(&id_str, id);
2937 if (err)
2938 return err;
2940 rewind(f);
2941 werase(view->window);
2943 if (asprintf(&line, "commit %s", id_str) == -1) {
2944 err = got_error_from_errno("asprintf");
2945 free(id_str);
2946 return err;
2949 err = format_line(&wline, &width, line, view->ncols);
2950 free(line);
2951 line = NULL;
2952 if (view_needs_focus_indication(view))
2953 wstandout(view->window);
2954 waddwstr(view->window, wline);
2955 if (view_needs_focus_indication(view))
2956 wstandend(view->window);
2957 free(wline);
2958 wline = NULL;
2959 if (width < view->ncols - 1)
2960 waddch(view->window, '\n');
2962 if (asprintf(&line, "[%d/%d] %s%s",
2963 *first_displayed_line - 1 + selected_line, nlines,
2964 blame_complete ? "" : "annotating... ", path) == -1) {
2965 free(id_str);
2966 return got_error_from_errno("asprintf");
2968 free(id_str);
2969 err = format_line(&wline, &width, line, view->ncols);
2970 free(line);
2971 line = NULL;
2972 if (err)
2973 return err;
2974 waddwstr(view->window, wline);
2975 free(wline);
2976 wline = NULL;
2977 if (width < view->ncols - 1)
2978 waddch(view->window, '\n');
2980 *eof = 0;
2981 while (nprinted < max_lines - 2) {
2982 line = parse_next_line(f, &len);
2983 if (line == NULL) {
2984 *eof = 1;
2985 break;
2987 if (++lineno < *first_displayed_line) {
2988 free(line);
2989 continue;
2992 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2993 err = format_line(&wline, &width, line, wlimit);
2994 if (err) {
2995 free(line);
2996 return err;
2999 if (view->focussed && nprinted == selected_line - 1)
3000 wstandout(view->window);
3002 blame_line = &lines[lineno - 1];
3003 if (blame_line->annotated && prev_id &&
3004 got_object_id_cmp(prev_id, blame_line->id) == 0)
3005 waddstr(view->window, " ");
3006 else if (blame_line->annotated) {
3007 char *id_str;
3008 err = got_object_id_str(&id_str, blame_line->id);
3009 if (err) {
3010 free(line);
3011 free(wline);
3012 return err;
3014 wprintw(view->window, "%.8s ", id_str);
3015 free(id_str);
3016 prev_id = blame_line->id;
3017 } else {
3018 waddstr(view->window, "........ ");
3019 prev_id = NULL;
3022 waddwstr(view->window, wline);
3023 while (width < wlimit) {
3024 waddch(view->window, ' ');
3025 width++;
3027 if (view->focussed && nprinted == selected_line - 1)
3028 wstandend(view->window);
3029 if (++nprinted == 1)
3030 *first_displayed_line = lineno;
3031 free(line);
3032 free(wline);
3033 wline = NULL;
3035 *last_displayed_line = lineno;
3037 view_vborder(view);
3039 return NULL;
3042 static const struct got_error *
3043 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3045 const struct got_error *err = NULL;
3046 struct tog_blame_cb_args *a = arg;
3047 struct tog_blame_line *line;
3048 int errcode;
3050 if (nlines != a->nlines ||
3051 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3052 return got_error(GOT_ERR_RANGE);
3054 errcode = pthread_mutex_lock(&tog_mutex);
3055 if (errcode)
3056 return got_error_set_errno(errcode, "pthread_mutex_lock");
3058 if (*a->quit) { /* user has quit the blame view */
3059 err = got_error(GOT_ERR_ITER_COMPLETED);
3060 goto done;
3063 if (lineno == -1)
3064 goto done; /* no change in this commit */
3066 line = &a->lines[lineno - 1];
3067 if (line->annotated)
3068 goto done;
3070 line->id = got_object_id_dup(id);
3071 if (line->id == NULL) {
3072 err = got_error_from_errno("got_object_id_dup");
3073 goto done;
3075 line->annotated = 1;
3076 done:
3077 errcode = pthread_mutex_unlock(&tog_mutex);
3078 if (errcode)
3079 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3080 return err;
3083 static void *
3084 blame_thread(void *arg)
3086 const struct got_error *err;
3087 struct tog_blame_thread_args *ta = arg;
3088 struct tog_blame_cb_args *a = ta->cb_args;
3089 int errcode;
3091 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3092 blame_cb, ta->cb_args);
3094 errcode = pthread_mutex_lock(&tog_mutex);
3095 if (errcode)
3096 return (void *)got_error_set_errno(errcode,
3097 "pthread_mutex_lock");
3099 got_repo_close(ta->repo);
3100 ta->repo = NULL;
3101 *ta->complete = 1;
3103 errcode = pthread_mutex_unlock(&tog_mutex);
3104 if (errcode && err == NULL)
3105 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3107 return (void *)err;
3110 static struct got_object_id *
3111 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3112 int selected_line)
3114 struct tog_blame_line *line;
3116 line = &lines[first_displayed_line - 1 + selected_line - 1];
3117 if (!line->annotated)
3118 return NULL;
3120 return line->id;
3123 static const struct got_error *
3124 stop_blame(struct tog_blame *blame)
3126 const struct got_error *err = NULL;
3127 int i;
3129 if (blame->thread) {
3130 int errcode;
3131 errcode = pthread_mutex_unlock(&tog_mutex);
3132 if (errcode)
3133 return got_error_set_errno(errcode,
3134 "pthread_mutex_unlock");
3135 errcode = pthread_join(blame->thread, (void **)&err);
3136 if (errcode)
3137 return got_error_set_errno(errcode, "pthread_join");
3138 errcode = pthread_mutex_lock(&tog_mutex);
3139 if (errcode)
3140 return got_error_set_errno(errcode,
3141 "pthread_mutex_lock");
3142 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3143 err = NULL;
3144 blame->thread = NULL;
3146 if (blame->thread_args.repo) {
3147 got_repo_close(blame->thread_args.repo);
3148 blame->thread_args.repo = NULL;
3150 if (blame->f) {
3151 if (fclose(blame->f) != 0 && err == NULL)
3152 err = got_error_from_errno("fclose");
3153 blame->f = NULL;
3155 if (blame->lines) {
3156 for (i = 0; i < blame->nlines; i++)
3157 free(blame->lines[i].id);
3158 free(blame->lines);
3159 blame->lines = NULL;
3161 free(blame->cb_args.commit_id);
3162 blame->cb_args.commit_id = NULL;
3164 return err;
3167 static const struct got_error *
3168 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3169 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3170 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3171 struct got_repository *repo)
3173 const struct got_error *err = NULL;
3174 struct got_blob_object *blob = NULL;
3175 struct got_repository *thread_repo = NULL;
3176 struct got_object_id *obj_id = NULL;
3177 int obj_type;
3179 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3180 if (err)
3181 return err;
3182 if (obj_id == NULL)
3183 return got_error(GOT_ERR_NO_OBJ);
3185 err = got_object_get_type(&obj_type, repo, obj_id);
3186 if (err)
3187 goto done;
3189 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3190 err = got_error(GOT_ERR_OBJ_TYPE);
3191 goto done;
3194 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3195 if (err)
3196 goto done;
3197 blame->f = got_opentemp();
3198 if (blame->f == NULL) {
3199 err = got_error_from_errno("got_opentemp");
3200 goto done;
3202 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3203 &blame->line_offsets, blame->f, blob);
3204 if (err)
3205 goto done;
3207 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3208 if (blame->lines == NULL) {
3209 err = got_error_from_errno("calloc");
3210 goto done;
3213 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3214 if (err)
3215 goto done;
3217 blame->cb_args.view = view;
3218 blame->cb_args.lines = blame->lines;
3219 blame->cb_args.nlines = blame->nlines;
3220 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3221 if (blame->cb_args.commit_id == NULL) {
3222 err = got_error_from_errno("got_object_id_dup");
3223 goto done;
3225 blame->cb_args.quit = done;
3227 blame->thread_args.path = path;
3228 blame->thread_args.repo = thread_repo;
3229 blame->thread_args.cb_args = &blame->cb_args;
3230 blame->thread_args.complete = blame_complete;
3231 *blame_complete = 0;
3233 done:
3234 if (blob)
3235 got_object_blob_close(blob);
3236 free(obj_id);
3237 if (err)
3238 stop_blame(blame);
3239 return err;
3242 static const struct got_error *
3243 open_blame_view(struct tog_view *view, char *path,
3244 struct got_object_id *commit_id, struct got_reflist_head *refs,
3245 struct got_repository *repo)
3247 const struct got_error *err = NULL;
3248 struct tog_blame_view_state *s = &view->state.blame;
3250 SIMPLEQ_INIT(&s->blamed_commits);
3252 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3253 if (err)
3254 return err;
3256 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3257 s->first_displayed_line = 1;
3258 s->last_displayed_line = view->nlines;
3259 s->selected_line = 1;
3260 s->blame_complete = 0;
3261 s->path = path;
3262 if (s->path == NULL)
3263 return got_error_from_errno("open_blame_view");
3264 s->repo = repo;
3265 s->refs = refs;
3266 s->commit_id = commit_id;
3267 memset(&s->blame, 0, sizeof(s->blame));
3269 view->show = show_blame_view;
3270 view->input = input_blame_view;
3271 view->close = close_blame_view;
3272 view->search_start = search_start_blame_view;
3273 view->search_next = search_next_blame_view;
3275 return run_blame(&s->blame, view, &s->blame_complete,
3276 &s->first_displayed_line, &s->last_displayed_line,
3277 &s->selected_line, &s->done, &s->eof, s->path,
3278 s->blamed_commit->id, s->repo);
3281 static const struct got_error *
3282 close_blame_view(struct tog_view *view)
3284 const struct got_error *err = NULL;
3285 struct tog_blame_view_state *s = &view->state.blame;
3287 if (s->blame.thread)
3288 err = stop_blame(&s->blame);
3290 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3291 struct got_object_qid *blamed_commit;
3292 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3293 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3294 got_object_qid_free(blamed_commit);
3297 free(s->path);
3299 return err;
3302 static const struct got_error *
3303 search_start_blame_view(struct tog_view *view)
3305 struct tog_blame_view_state *s = &view->state.blame;
3307 s->matched_line = 0;
3308 return NULL;
3311 static int
3312 match_line(const char *line, regex_t *regex)
3314 regmatch_t regmatch;
3316 return regexec(regex, line, 1, &regmatch, 0) == 0;
3320 static const struct got_error *
3321 search_next_blame_view(struct tog_view *view)
3323 struct tog_blame_view_state *s = &view->state.blame;
3324 int lineno;
3326 if (!view->searching) {
3327 view->search_next_done = 1;
3328 return NULL;
3331 if (s->matched_line) {
3332 if (view->searching == TOG_SEARCH_FORWARD)
3333 lineno = s->matched_line + 1;
3334 else
3335 lineno = s->matched_line - 1;
3336 } else {
3337 if (view->searching == TOG_SEARCH_FORWARD)
3338 lineno = 1;
3339 else
3340 lineno = s->blame.nlines;
3343 while (1) {
3344 char *line = NULL;
3345 off_t offset;
3346 size_t len;
3348 if (lineno <= 0 || lineno > s->blame.nlines) {
3349 if (s->matched_line == 0) {
3350 view->search_next_done = 1;
3351 free(line);
3352 break;
3355 if (view->searching == TOG_SEARCH_FORWARD)
3356 lineno = 1;
3357 else
3358 lineno = s->blame.nlines;
3361 offset = s->blame.line_offsets[lineno - 1];
3362 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3363 free(line);
3364 return got_error_from_errno("fseeko");
3366 free(line);
3367 line = parse_next_line(s->blame.f, &len);
3368 if (line && match_line(line, &view->regex)) {
3369 view->search_next_done = 1;
3370 s->matched_line = lineno;
3371 free(line);
3372 break;
3374 free(line);
3375 if (view->searching == TOG_SEARCH_FORWARD)
3376 lineno++;
3377 else
3378 lineno--;
3381 if (s->matched_line) {
3382 s->first_displayed_line = s->matched_line;
3383 s->selected_line = 1;
3386 return NULL;
3389 static const struct got_error *
3390 show_blame_view(struct tog_view *view)
3392 const struct got_error *err = NULL;
3393 struct tog_blame_view_state *s = &view->state.blame;
3394 int errcode;
3396 if (s->blame.thread == NULL) {
3397 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3398 &s->blame.thread_args);
3399 if (errcode)
3400 return got_error_set_errno(errcode, "pthread_create");
3403 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3404 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3405 s->selected_line, &s->first_displayed_line,
3406 &s->last_displayed_line, &s->eof, view->nlines);
3408 view_vborder(view);
3409 return err;
3412 static const struct got_error *
3413 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3414 struct tog_view **focus_view, struct tog_view *view, int ch)
3416 const struct got_error *err = NULL, *thread_err = NULL;
3417 struct tog_view *diff_view;
3418 struct tog_blame_view_state *s = &view->state.blame;
3419 int begin_x = 0;
3421 switch (ch) {
3422 case 'q':
3423 s->done = 1;
3424 break;
3425 case 'k':
3426 case KEY_UP:
3427 if (s->selected_line > 1)
3428 s->selected_line--;
3429 else if (s->selected_line == 1 &&
3430 s->first_displayed_line > 1)
3431 s->first_displayed_line--;
3432 break;
3433 case KEY_PPAGE:
3434 if (s->first_displayed_line == 1) {
3435 s->selected_line = 1;
3436 break;
3438 if (s->first_displayed_line > view->nlines - 2)
3439 s->first_displayed_line -=
3440 (view->nlines - 2);
3441 else
3442 s->first_displayed_line = 1;
3443 break;
3444 case 'j':
3445 case KEY_DOWN:
3446 if (s->selected_line < view->nlines - 2 &&
3447 s->first_displayed_line +
3448 s->selected_line <= s->blame.nlines)
3449 s->selected_line++;
3450 else if (s->last_displayed_line <
3451 s->blame.nlines)
3452 s->first_displayed_line++;
3453 break;
3454 case 'b':
3455 case 'p': {
3456 struct got_object_id *id = NULL;
3457 id = get_selected_commit_id(s->blame.lines,
3458 s->first_displayed_line, s->selected_line);
3459 if (id == NULL)
3460 break;
3461 if (ch == 'p') {
3462 struct got_commit_object *commit;
3463 struct got_object_qid *pid;
3464 struct got_object_id *blob_id = NULL;
3465 int obj_type;
3466 err = got_object_open_as_commit(&commit,
3467 s->repo, id);
3468 if (err)
3469 break;
3470 pid = SIMPLEQ_FIRST(
3471 got_object_commit_get_parent_ids(commit));
3472 if (pid == NULL) {
3473 got_object_commit_close(commit);
3474 break;
3476 /* Check if path history ends here. */
3477 err = got_object_id_by_path(&blob_id, s->repo,
3478 pid->id, s->path);
3479 if (err) {
3480 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3481 err = NULL;
3482 got_object_commit_close(commit);
3483 break;
3485 err = got_object_get_type(&obj_type, s->repo,
3486 blob_id);
3487 free(blob_id);
3488 /* Can't blame non-blob type objects. */
3489 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3490 got_object_commit_close(commit);
3491 break;
3493 err = got_object_qid_alloc(&s->blamed_commit,
3494 pid->id);
3495 got_object_commit_close(commit);
3496 } else {
3497 if (got_object_id_cmp(id,
3498 s->blamed_commit->id) == 0)
3499 break;
3500 err = got_object_qid_alloc(&s->blamed_commit,
3501 id);
3503 if (err)
3504 break;
3505 s->done = 1;
3506 thread_err = stop_blame(&s->blame);
3507 s->done = 0;
3508 if (thread_err)
3509 break;
3510 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3511 s->blamed_commit, entry);
3512 err = run_blame(&s->blame, view, &s->blame_complete,
3513 &s->first_displayed_line, &s->last_displayed_line,
3514 &s->selected_line, &s->done, &s->eof,
3515 s->path, s->blamed_commit->id, s->repo);
3516 if (err)
3517 break;
3518 break;
3520 case 'B': {
3521 struct got_object_qid *first;
3522 first = SIMPLEQ_FIRST(&s->blamed_commits);
3523 if (!got_object_id_cmp(first->id, s->commit_id))
3524 break;
3525 s->done = 1;
3526 thread_err = stop_blame(&s->blame);
3527 s->done = 0;
3528 if (thread_err)
3529 break;
3530 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3531 got_object_qid_free(s->blamed_commit);
3532 s->blamed_commit =
3533 SIMPLEQ_FIRST(&s->blamed_commits);
3534 err = run_blame(&s->blame, view, &s->blame_complete,
3535 &s->first_displayed_line, &s->last_displayed_line,
3536 &s->selected_line, &s->done, &s->eof, s->path,
3537 s->blamed_commit->id, s->repo);
3538 if (err)
3539 break;
3540 break;
3542 case KEY_ENTER:
3543 case '\r': {
3544 struct got_object_id *id = NULL;
3545 struct got_object_qid *pid;
3546 struct got_commit_object *commit = NULL;
3547 id = get_selected_commit_id(s->blame.lines,
3548 s->first_displayed_line, s->selected_line);
3549 if (id == NULL)
3550 break;
3551 err = got_object_open_as_commit(&commit, s->repo, id);
3552 if (err)
3553 break;
3554 pid = SIMPLEQ_FIRST(
3555 got_object_commit_get_parent_ids(commit));
3556 if (view_is_parent_view(view))
3557 begin_x = view_split_begin_x(view->begin_x);
3558 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3559 if (diff_view == NULL) {
3560 got_object_commit_close(commit);
3561 err = got_error_from_errno("view_open");
3562 break;
3564 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3565 id, NULL, s->refs, s->repo);
3566 got_object_commit_close(commit);
3567 if (err) {
3568 view_close(diff_view);
3569 break;
3571 if (view_is_parent_view(view)) {
3572 err = view_close_child(view);
3573 if (err)
3574 break;
3575 err = view_set_child(view, diff_view);
3576 if (err) {
3577 view_close(diff_view);
3578 break;
3580 *focus_view = diff_view;
3581 view->child_focussed = 1;
3582 } else
3583 *new_view = diff_view;
3584 if (err)
3585 break;
3586 break;
3588 case KEY_NPAGE:
3589 case ' ':
3590 if (s->last_displayed_line >= s->blame.nlines &&
3591 s->selected_line >= MIN(s->blame.nlines,
3592 view->nlines - 2)) {
3593 break;
3595 if (s->last_displayed_line >= s->blame.nlines &&
3596 s->selected_line < view->nlines - 2) {
3597 s->selected_line = MIN(s->blame.nlines,
3598 view->nlines - 2);
3599 break;
3601 if (s->last_displayed_line + view->nlines - 2
3602 <= s->blame.nlines)
3603 s->first_displayed_line +=
3604 view->nlines - 2;
3605 else
3606 s->first_displayed_line =
3607 s->blame.nlines -
3608 (view->nlines - 3);
3609 break;
3610 case KEY_RESIZE:
3611 if (s->selected_line > view->nlines - 2) {
3612 s->selected_line = MIN(s->blame.nlines,
3613 view->nlines - 2);
3615 break;
3616 default:
3617 break;
3619 return thread_err ? thread_err : err;
3622 static const struct got_error *
3623 cmd_blame(int argc, char *argv[])
3625 const struct got_error *error;
3626 struct got_repository *repo = NULL;
3627 struct got_reflist_head refs;
3628 struct got_worktree *worktree = NULL;
3629 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3630 struct got_object_id *commit_id = NULL;
3631 char *commit_id_str = NULL;
3632 int ch;
3633 struct tog_view *view;
3635 SIMPLEQ_INIT(&refs);
3637 #ifndef PROFILE
3638 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3639 NULL) == -1)
3640 err(1, "pledge");
3641 #endif
3643 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3644 switch (ch) {
3645 case 'c':
3646 commit_id_str = optarg;
3647 break;
3648 case 'r':
3649 repo_path = realpath(optarg, NULL);
3650 if (repo_path == NULL)
3651 err(1, "-r option");
3652 break;
3653 default:
3654 usage_blame();
3655 /* NOTREACHED */
3659 argc -= optind;
3660 argv += optind;
3662 if (argc == 1)
3663 path = argv[0];
3664 else
3665 usage_blame();
3667 cwd = getcwd(NULL, 0);
3668 if (cwd == NULL) {
3669 error = got_error_from_errno("getcwd");
3670 goto done;
3672 if (repo_path == NULL) {
3673 error = got_worktree_open(&worktree, cwd);
3674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3675 goto done;
3676 else
3677 error = NULL;
3678 if (worktree) {
3679 repo_path =
3680 strdup(got_worktree_get_repo_path(worktree));
3681 if (repo_path == NULL)
3682 error = got_error_from_errno("strdup");
3683 if (error)
3684 goto done;
3685 } else {
3686 repo_path = strdup(cwd);
3687 if (repo_path == NULL) {
3688 error = got_error_from_errno("strdup");
3689 goto done;
3694 init_curses();
3696 error = got_repo_open(&repo, repo_path);
3697 if (error != NULL)
3698 goto done;
3700 error = apply_unveil(got_repo_get_path(repo), NULL);
3701 if (error)
3702 goto done;
3704 if (worktree) {
3705 const char *prefix = got_worktree_get_path_prefix(worktree);
3706 char *p, *worktree_subdir = cwd +
3707 strlen(got_worktree_get_root_path(worktree));
3708 if (asprintf(&p, "%s%s%s%s%s",
3709 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3710 worktree_subdir, worktree_subdir[0] ? "/" : "",
3711 path) == -1) {
3712 error = got_error_from_errno("asprintf");
3713 goto done;
3715 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3716 free(p);
3717 } else {
3718 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3720 if (error)
3721 goto done;
3723 if (commit_id_str == NULL) {
3724 struct got_reference *head_ref;
3725 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3726 if (error != NULL)
3727 goto done;
3728 error = got_ref_resolve(&commit_id, repo, head_ref);
3729 got_ref_close(head_ref);
3730 } else {
3731 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3732 if (error) {
3733 if (error->code != GOT_ERR_NOT_REF)
3734 goto done;
3735 error = got_repo_match_object_id_prefix(&commit_id,
3736 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3739 if (error != NULL)
3740 goto done;
3742 error = got_ref_list(&refs, repo);
3743 if (error)
3744 goto done;
3746 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3747 if (view == NULL) {
3748 error = got_error_from_errno("view_open");
3749 goto done;
3751 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3752 if (error)
3753 goto done;
3754 error = view_loop(view);
3755 done:
3756 free(repo_path);
3757 free(cwd);
3758 free(commit_id);
3759 if (worktree)
3760 got_worktree_close(worktree);
3761 if (repo)
3762 got_repo_close(repo);
3763 got_ref_list_free(&refs);
3764 return error;
3767 static const struct got_error *
3768 draw_tree_entries(struct tog_view *view,
3769 struct got_tree_entry **first_displayed_entry,
3770 struct got_tree_entry **last_displayed_entry,
3771 struct got_tree_entry **selected_entry, int *ndisplayed,
3772 const char *label, int show_ids, const char *parent_path,
3773 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3775 const struct got_error *err = NULL;
3776 struct got_tree_entry *te;
3777 wchar_t *wline;
3778 int width, n;
3780 *ndisplayed = 0;
3782 werase(view->window);
3784 if (limit == 0)
3785 return NULL;
3787 err = format_line(&wline, &width, label, view->ncols);
3788 if (err)
3789 return err;
3790 if (view_needs_focus_indication(view))
3791 wstandout(view->window);
3792 waddwstr(view->window, wline);
3793 if (view_needs_focus_indication(view))
3794 wstandend(view->window);
3795 free(wline);
3796 wline = NULL;
3797 if (width < view->ncols - 1)
3798 waddch(view->window, '\n');
3799 if (--limit <= 0)
3800 return NULL;
3801 err = format_line(&wline, &width, parent_path, view->ncols);
3802 if (err)
3803 return err;
3804 waddwstr(view->window, wline);
3805 free(wline);
3806 wline = NULL;
3807 if (width < view->ncols - 1)
3808 waddch(view->window, '\n');
3809 if (--limit <= 0)
3810 return NULL;
3811 waddch(view->window, '\n');
3812 if (--limit <= 0)
3813 return NULL;
3815 te = SIMPLEQ_FIRST(&entries->head);
3816 if (*first_displayed_entry == NULL) {
3817 if (selected == 0) {
3818 if (view->focussed)
3819 wstandout(view->window);
3820 *selected_entry = NULL;
3822 waddstr(view->window, " ..\n"); /* parent directory */
3823 if (selected == 0 && view->focussed)
3824 wstandend(view->window);
3825 (*ndisplayed)++;
3826 if (--limit <= 0)
3827 return NULL;
3828 n = 1;
3829 } else {
3830 n = 0;
3831 while (te != *first_displayed_entry)
3832 te = SIMPLEQ_NEXT(te, entry);
3835 while (te) {
3836 char *line = NULL, *id_str = NULL;
3838 if (show_ids) {
3839 err = got_object_id_str(&id_str, te->id);
3840 if (err)
3841 return got_error_from_errno(
3842 "got_object_id_str");
3844 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3845 te->name, S_ISDIR(te->mode) ? "/" :
3846 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3847 free(id_str);
3848 return got_error_from_errno("asprintf");
3850 free(id_str);
3851 err = format_line(&wline, &width, line, view->ncols);
3852 if (err) {
3853 free(line);
3854 break;
3856 if (n == selected) {
3857 if (view->focussed)
3858 wstandout(view->window);
3859 *selected_entry = te;
3861 waddwstr(view->window, wline);
3862 if (width < view->ncols - 1)
3863 waddch(view->window, '\n');
3864 if (n == selected && view->focussed)
3865 wstandend(view->window);
3866 free(line);
3867 free(wline);
3868 wline = NULL;
3869 n++;
3870 (*ndisplayed)++;
3871 *last_displayed_entry = te;
3872 if (--limit <= 0)
3873 break;
3874 te = SIMPLEQ_NEXT(te, entry);
3877 return err;
3880 static void
3881 tree_scroll_up(struct tog_view *view,
3882 struct got_tree_entry **first_displayed_entry, int maxscroll,
3883 const struct got_tree_entries *entries, int isroot)
3885 struct got_tree_entry *te, *prev;
3886 int i;
3888 if (*first_displayed_entry == NULL)
3889 return;
3891 te = SIMPLEQ_FIRST(&entries->head);
3892 if (*first_displayed_entry == te) {
3893 if (!isroot)
3894 *first_displayed_entry = NULL;
3895 return;
3898 /* XXX this is stupid... switch to TAILQ? */
3899 for (i = 0; i < maxscroll; i++) {
3900 while (te != *first_displayed_entry) {
3901 prev = te;
3902 te = SIMPLEQ_NEXT(te, entry);
3904 *first_displayed_entry = prev;
3905 te = SIMPLEQ_FIRST(&entries->head);
3907 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3908 *first_displayed_entry = NULL;
3911 static int
3912 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3913 struct got_tree_entry *last_displayed_entry,
3914 const struct got_tree_entries *entries)
3916 struct got_tree_entry *next, *last;
3917 int n = 0;
3919 if (*first_displayed_entry)
3920 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3921 else
3922 next = SIMPLEQ_FIRST(&entries->head);
3923 last = last_displayed_entry;
3924 while (next && last && n++ < maxscroll) {
3925 last = SIMPLEQ_NEXT(last, entry);
3926 if (last) {
3927 *first_displayed_entry = next;
3928 next = SIMPLEQ_NEXT(next, entry);
3931 return n;
3934 static const struct got_error *
3935 tree_entry_path(char **path, struct tog_parent_trees *parents,
3936 struct got_tree_entry *te)
3938 const struct got_error *err = NULL;
3939 struct tog_parent_tree *pt;
3940 size_t len = 2; /* for leading slash and NUL */
3942 TAILQ_FOREACH(pt, parents, entry)
3943 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3944 if (te)
3945 len += strlen(te->name);
3947 *path = calloc(1, len);
3948 if (path == NULL)
3949 return got_error_from_errno("calloc");
3951 (*path)[0] = '/';
3952 pt = TAILQ_LAST(parents, tog_parent_trees);
3953 while (pt) {
3954 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3955 err = got_error(GOT_ERR_NO_SPACE);
3956 goto done;
3958 if (strlcat(*path, "/", len) >= len) {
3959 err = got_error(GOT_ERR_NO_SPACE);
3960 goto done;
3962 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3964 if (te) {
3965 if (strlcat(*path, te->name, len) >= len) {
3966 err = got_error(GOT_ERR_NO_SPACE);
3967 goto done;
3970 done:
3971 if (err) {
3972 free(*path);
3973 *path = NULL;
3975 return err;
3978 static const struct got_error *
3979 blame_tree_entry(struct tog_view **new_view, int begin_x,
3980 struct got_tree_entry *te, struct tog_parent_trees *parents,
3981 struct got_object_id *commit_id, struct got_reflist_head *refs,
3982 struct got_repository *repo)
3984 const struct got_error *err = NULL;
3985 char *path;
3986 struct tog_view *blame_view;
3988 *new_view = NULL;
3990 err = tree_entry_path(&path, parents, te);
3991 if (err)
3992 return err;
3994 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3995 if (blame_view == NULL)
3996 return got_error_from_errno("view_open");
3998 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3999 if (err) {
4000 view_close(blame_view);
4001 free(path);
4002 } else
4003 *new_view = blame_view;
4004 return err;
4007 static const struct got_error *
4008 log_tree_entry(struct tog_view **new_view, int begin_x,
4009 struct got_tree_entry *te, struct tog_parent_trees *parents,
4010 struct got_object_id *commit_id, struct got_reflist_head *refs,
4011 struct got_repository *repo)
4013 struct tog_view *log_view;
4014 const struct got_error *err = NULL;
4015 char *path;
4017 *new_view = NULL;
4019 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4020 if (log_view == NULL)
4021 return got_error_from_errno("view_open");
4023 err = tree_entry_path(&path, parents, te);
4024 if (err)
4025 return err;
4027 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4028 if (err)
4029 view_close(log_view);
4030 else
4031 *new_view = log_view;
4032 free(path);
4033 return err;
4036 static const struct got_error *
4037 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4038 struct got_object_id *commit_id, struct got_reflist_head *refs,
4039 struct got_repository *repo)
4041 const struct got_error *err = NULL;
4042 char *commit_id_str = NULL;
4043 struct tog_tree_view_state *s = &view->state.tree;
4045 TAILQ_INIT(&s->parents);
4047 err = got_object_id_str(&commit_id_str, commit_id);
4048 if (err != NULL)
4049 goto done;
4051 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4052 err = got_error_from_errno("asprintf");
4053 goto done;
4056 s->root = s->tree = root;
4057 s->entries = got_object_tree_get_entries(root);
4058 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4059 s->commit_id = got_object_id_dup(commit_id);
4060 if (s->commit_id == NULL) {
4061 err = got_error_from_errno("got_object_id_dup");
4062 goto done;
4064 s->refs = refs;
4065 s->repo = repo;
4067 view->show = show_tree_view;
4068 view->input = input_tree_view;
4069 view->close = close_tree_view;
4070 view->search_start = search_start_tree_view;
4071 view->search_next = search_next_tree_view;
4072 done:
4073 free(commit_id_str);
4074 if (err) {
4075 free(s->tree_label);
4076 s->tree_label = NULL;
4078 return err;
4081 static const struct got_error *
4082 close_tree_view(struct tog_view *view)
4084 struct tog_tree_view_state *s = &view->state.tree;
4086 free(s->tree_label);
4087 s->tree_label = NULL;
4088 free(s->commit_id);
4089 s->commit_id = NULL;
4090 while (!TAILQ_EMPTY(&s->parents)) {
4091 struct tog_parent_tree *parent;
4092 parent = TAILQ_FIRST(&s->parents);
4093 TAILQ_REMOVE(&s->parents, parent, entry);
4094 free(parent);
4097 if (s->tree != s->root)
4098 got_object_tree_close(s->tree);
4099 got_object_tree_close(s->root);
4101 return NULL;
4104 static const struct got_error *
4105 search_start_tree_view(struct tog_view *view)
4107 struct tog_tree_view_state *s = &view->state.tree;
4109 s->matched_entry = NULL;
4110 return NULL;
4113 static int
4114 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4116 regmatch_t regmatch;
4118 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4121 static const struct got_error *
4122 search_next_tree_view(struct tog_view *view)
4124 struct tog_tree_view_state *s = &view->state.tree;
4125 struct got_tree_entry *entry = NULL, *te;
4127 if (!view->searching) {
4128 view->search_next_done = 1;
4129 return NULL;
4132 if (s->matched_entry) {
4133 if (view->searching == TOG_SEARCH_FORWARD) {
4134 if (s->selected_entry)
4135 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4136 else
4137 entry = SIMPLEQ_FIRST(&s->entries->head);
4139 else {
4140 if (s->selected_entry == NULL) {
4141 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4142 entry = te;
4143 } else {
4144 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4145 entry = te;
4146 if (SIMPLEQ_NEXT(te, entry) ==
4147 s->selected_entry)
4148 break;
4152 } else {
4153 if (view->searching == TOG_SEARCH_FORWARD)
4154 entry = SIMPLEQ_FIRST(&s->entries->head);
4155 else {
4156 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4157 entry = te;
4161 while (1) {
4162 if (entry == NULL) {
4163 if (s->matched_entry == NULL) {
4164 view->search_next_done = 1;
4165 return NULL;
4167 if (view->searching == TOG_SEARCH_FORWARD)
4168 entry = SIMPLEQ_FIRST(&s->entries->head);
4169 else {
4170 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4171 entry = te;
4175 if (match_tree_entry(entry, &view->regex)) {
4176 view->search_next_done = 1;
4177 s->matched_entry = entry;
4178 break;
4181 if (view->searching == TOG_SEARCH_FORWARD)
4182 entry = SIMPLEQ_NEXT(entry, entry);
4183 else {
4184 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4185 entry = NULL;
4186 else {
4187 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4188 if (SIMPLEQ_NEXT(te, entry) == entry) {
4189 entry = te;
4190 break;
4197 if (s->matched_entry) {
4198 s->first_displayed_entry = s->matched_entry;
4199 s->selected = 0;
4202 return NULL;
4205 static const struct got_error *
4206 show_tree_view(struct tog_view *view)
4208 const struct got_error *err = NULL;
4209 struct tog_tree_view_state *s = &view->state.tree;
4210 char *parent_path;
4212 err = tree_entry_path(&parent_path, &s->parents, NULL);
4213 if (err)
4214 return err;
4216 err = draw_tree_entries(view, &s->first_displayed_entry,
4217 &s->last_displayed_entry, &s->selected_entry,
4218 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4219 s->entries, s->selected, view->nlines, s->tree == s->root);
4220 free(parent_path);
4222 view_vborder(view);
4223 return err;
4226 static const struct got_error *
4227 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4228 struct tog_view **focus_view, struct tog_view *view, int ch)
4230 const struct got_error *err = NULL;
4231 struct tog_tree_view_state *s = &view->state.tree;
4232 struct tog_view *log_view;
4233 int begin_x = 0, nscrolled;
4235 switch (ch) {
4236 case 'i':
4237 s->show_ids = !s->show_ids;
4238 break;
4239 case 'l':
4240 if (!s->selected_entry)
4241 break;
4242 if (view_is_parent_view(view))
4243 begin_x = view_split_begin_x(view->begin_x);
4244 err = log_tree_entry(&log_view, begin_x,
4245 s->selected_entry, &s->parents,
4246 s->commit_id, s->refs, s->repo);
4247 if (view_is_parent_view(view)) {
4248 err = view_close_child(view);
4249 if (err)
4250 return err;
4251 err = view_set_child(view, log_view);
4252 if (err) {
4253 view_close(log_view);
4254 break;
4256 *focus_view = log_view;
4257 view->child_focussed = 1;
4258 } else
4259 *new_view = log_view;
4260 break;
4261 case 'k':
4262 case KEY_UP:
4263 if (s->selected > 0) {
4264 s->selected--;
4265 if (s->selected == 0)
4266 break;
4268 if (s->selected > 0)
4269 break;
4270 tree_scroll_up(view, &s->first_displayed_entry, 1,
4271 s->entries, s->tree == s->root);
4272 break;
4273 case KEY_PPAGE:
4274 tree_scroll_up(view, &s->first_displayed_entry,
4275 MAX(0, view->nlines - 4 - s->selected), s->entries,
4276 s->tree == s->root);
4277 s->selected = 0;
4278 if (SIMPLEQ_FIRST(&s->entries->head) ==
4279 s->first_displayed_entry && s->tree != s->root)
4280 s->first_displayed_entry = NULL;
4281 break;
4282 case 'j':
4283 case KEY_DOWN:
4284 if (s->selected < s->ndisplayed - 1) {
4285 s->selected++;
4286 break;
4288 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4289 /* can't scroll any further */
4290 break;
4291 tree_scroll_down(&s->first_displayed_entry, 1,
4292 s->last_displayed_entry, s->entries);
4293 break;
4294 case KEY_NPAGE:
4295 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4296 == NULL) {
4297 /* can't scroll any further; move cursor down */
4298 if (s->selected < s->ndisplayed - 1)
4299 s->selected = s->ndisplayed - 1;
4300 break;
4302 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4303 view->nlines, s->last_displayed_entry, s->entries);
4304 if (nscrolled < view->nlines) {
4305 int ndisplayed = 0;
4306 struct got_tree_entry *te;
4307 te = s->first_displayed_entry;
4308 do {
4309 ndisplayed++;
4310 te = SIMPLEQ_NEXT(te, entry);
4311 } while (te);
4312 s->selected = ndisplayed - 1;
4314 break;
4315 case KEY_ENTER:
4316 case '\r':
4317 case KEY_BACKSPACE:
4318 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4319 struct tog_parent_tree *parent;
4320 /* user selected '..' */
4321 if (s->tree == s->root)
4322 break;
4323 parent = TAILQ_FIRST(&s->parents);
4324 TAILQ_REMOVE(&s->parents, parent,
4325 entry);
4326 got_object_tree_close(s->tree);
4327 s->tree = parent->tree;
4328 s->entries =
4329 got_object_tree_get_entries(s->tree);
4330 s->first_displayed_entry =
4331 parent->first_displayed_entry;
4332 s->selected_entry =
4333 parent->selected_entry;
4334 s->selected = parent->selected;
4335 free(parent);
4336 } else if (S_ISDIR(s->selected_entry->mode)) {
4337 struct got_tree_object *subtree;
4338 err = got_object_open_as_tree(&subtree,
4339 s->repo, s->selected_entry->id);
4340 if (err)
4341 break;
4342 err = tree_view_visit_subtree(subtree, s);
4343 if (err) {
4344 got_object_tree_close(subtree);
4345 break;
4347 } else if (S_ISREG(s->selected_entry->mode)) {
4348 struct tog_view *blame_view;
4349 int begin_x = view_is_parent_view(view) ?
4350 view_split_begin_x(view->begin_x) : 0;
4352 err = blame_tree_entry(&blame_view, begin_x,
4353 s->selected_entry, &s->parents,
4354 s->commit_id, s->refs, s->repo);
4355 if (err)
4356 break;
4357 if (view_is_parent_view(view)) {
4358 err = view_close_child(view);
4359 if (err)
4360 return err;
4361 err = view_set_child(view, blame_view);
4362 if (err) {
4363 view_close(blame_view);
4364 break;
4366 *focus_view = blame_view;
4367 view->child_focussed = 1;
4368 } else
4369 *new_view = blame_view;
4371 break;
4372 case KEY_RESIZE:
4373 if (s->selected > view->nlines)
4374 s->selected = s->ndisplayed - 1;
4375 break;
4376 default:
4377 break;
4380 return err;
4383 __dead static void
4384 usage_tree(void)
4386 endwin();
4387 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4388 getprogname());
4389 exit(1);
4392 static const struct got_error *
4393 cmd_tree(int argc, char *argv[])
4395 const struct got_error *error;
4396 struct got_repository *repo = NULL;
4397 struct got_reflist_head refs;
4398 char *repo_path = NULL;
4399 struct got_object_id *commit_id = NULL;
4400 char *commit_id_arg = NULL;
4401 struct got_commit_object *commit = NULL;
4402 struct got_tree_object *tree = NULL;
4403 int ch;
4404 struct tog_view *view;
4406 SIMPLEQ_INIT(&refs);
4408 #ifndef PROFILE
4409 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4410 NULL) == -1)
4411 err(1, "pledge");
4412 #endif
4414 while ((ch = getopt(argc, argv, "c:")) != -1) {
4415 switch (ch) {
4416 case 'c':
4417 commit_id_arg = optarg;
4418 break;
4419 default:
4420 usage_tree();
4421 /* NOTREACHED */
4425 argc -= optind;
4426 argv += optind;
4428 if (argc == 0) {
4429 struct got_worktree *worktree;
4430 char *cwd = getcwd(NULL, 0);
4431 if (cwd == NULL)
4432 return got_error_from_errno("getcwd");
4433 error = got_worktree_open(&worktree, cwd);
4434 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4435 goto done;
4436 if (worktree) {
4437 free(cwd);
4438 repo_path =
4439 strdup(got_worktree_get_repo_path(worktree));
4440 got_worktree_close(worktree);
4441 } else
4442 repo_path = cwd;
4443 if (repo_path == NULL) {
4444 error = got_error_from_errno("strdup");
4445 goto done;
4447 } else if (argc == 1) {
4448 repo_path = realpath(argv[0], NULL);
4449 if (repo_path == NULL)
4450 return got_error_from_errno2("realpath", argv[0]);
4451 } else
4452 usage_log();
4454 init_curses();
4456 error = got_repo_open(&repo, repo_path);
4457 if (error != NULL)
4458 goto done;
4460 error = apply_unveil(got_repo_get_path(repo), NULL);
4461 if (error)
4462 goto done;
4464 if (commit_id_arg == NULL)
4465 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4466 else {
4467 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4468 if (error) {
4469 if (error->code != GOT_ERR_NOT_REF)
4470 goto done;
4471 error = got_repo_match_object_id_prefix(&commit_id,
4472 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4475 if (error != NULL)
4476 goto done;
4478 error = got_object_open_as_commit(&commit, repo, commit_id);
4479 if (error != NULL)
4480 goto done;
4482 error = got_object_open_as_tree(&tree, repo,
4483 got_object_commit_get_tree_id(commit));
4484 if (error != NULL)
4485 goto done;
4487 error = got_ref_list(&refs, repo);
4488 if (error)
4489 goto done;
4491 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4492 if (view == NULL) {
4493 error = got_error_from_errno("view_open");
4494 goto done;
4496 error = open_tree_view(view, tree, commit_id, &refs, repo);
4497 if (error)
4498 goto done;
4499 error = view_loop(view);
4500 done:
4501 free(repo_path);
4502 free(commit_id);
4503 if (commit)
4504 got_object_commit_close(commit);
4505 if (tree)
4506 got_object_tree_close(tree);
4507 if (repo)
4508 got_repo_close(repo);
4509 got_ref_list_free(&refs);
4510 return error;
4513 static void
4514 list_commands(void)
4516 int i;
4518 fprintf(stderr, "commands:");
4519 for (i = 0; i < nitems(tog_commands); i++) {
4520 struct tog_cmd *cmd = &tog_commands[i];
4521 fprintf(stderr, " %s", cmd->name);
4523 fputc('\n', stderr);
4526 __dead static void
4527 usage(int hflag)
4529 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4530 getprogname());
4531 if (hflag)
4532 list_commands();
4533 exit(1);
4536 static char **
4537 make_argv(const char *arg0, const char *arg1)
4539 char **argv;
4540 int argc = (arg1 == NULL ? 1 : 2);
4542 argv = calloc(argc, sizeof(char *));
4543 if (argv == NULL)
4544 err(1, "calloc");
4545 argv[0] = strdup(arg0);
4546 if (argv[0] == NULL)
4547 err(1, "calloc");
4548 if (arg1) {
4549 argv[1] = strdup(arg1);
4550 if (argv[1] == NULL)
4551 err(1, "calloc");
4554 return argv;
4557 int
4558 main(int argc, char *argv[])
4560 const struct got_error *error = NULL;
4561 struct tog_cmd *cmd = NULL;
4562 int ch, hflag = 0, Vflag = 0;
4563 char **cmd_argv = NULL;
4565 setlocale(LC_CTYPE, "");
4567 while ((ch = getopt(argc, argv, "hV")) != -1) {
4568 switch (ch) {
4569 case 'h':
4570 hflag = 1;
4571 break;
4572 case 'V':
4573 Vflag = 1;
4574 break;
4575 default:
4576 usage(hflag);
4577 /* NOTREACHED */
4581 argc -= optind;
4582 argv += optind;
4583 optind = 0;
4584 optreset = 1;
4586 if (Vflag) {
4587 got_version_print_str();
4588 return 1;
4591 if (argc == 0) {
4592 if (hflag)
4593 usage(hflag);
4594 /* Build an argument vector which runs a default command. */
4595 cmd = &tog_commands[0];
4596 cmd_argv = make_argv(cmd->name, NULL);
4597 argc = 1;
4598 } else {
4599 int i;
4601 /* Did the user specific a command? */
4602 for (i = 0; i < nitems(tog_commands); i++) {
4603 if (strncmp(tog_commands[i].name, argv[0],
4604 strlen(argv[0])) == 0) {
4605 cmd = &tog_commands[i];
4606 break;
4610 if (cmd == NULL) {
4611 fprintf(stderr, "%s: unknown command '%s'\n",
4612 getprogname(), argv[0]);
4613 list_commands();
4614 return 1;
4618 if (hflag)
4619 cmd->cmd_usage();
4620 else
4621 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4623 endwin();
4624 free(cmd_argv);
4625 if (error)
4626 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4627 return 0;