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 repo_path = worktree ?
2233 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2234 if (repo_path == NULL) {
2235 error = got_error_from_errno("strdup");
2236 goto done;
2239 init_curses();
2241 error = got_repo_open(&repo, repo_path);
2242 if (error != NULL)
2243 goto done;
2245 error = apply_unveil(got_repo_get_path(repo),
2246 worktree ? got_worktree_get_root_path(worktree) : NULL);
2247 if (error)
2248 goto done;
2250 if (start_commit == NULL)
2251 error = get_head_commit_id(&start_id, worktree ?
2252 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2253 repo);
2254 else {
2255 error = get_head_commit_id(&start_id, start_commit, repo);
2256 if (error) {
2257 if (error->code != GOT_ERR_NOT_REF)
2258 goto done;
2259 error = got_repo_match_object_id_prefix(&start_id,
2260 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2263 if (error != NULL)
2264 goto done;
2266 error = got_ref_list(&refs, repo);
2267 if (error)
2268 goto done;
2270 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2271 if (view == NULL) {
2272 error = got_error_from_errno("view_open");
2273 goto done;
2275 error = open_log_view(view, start_id, &refs, repo, worktree ?
2276 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2277 if (error)
2278 goto done;
2279 error = view_loop(view);
2280 done:
2281 free(repo_path);
2282 free(cwd);
2283 free(path);
2284 free(start_id);
2285 if (repo)
2286 got_repo_close(repo);
2287 if (worktree)
2288 got_worktree_close(worktree);
2289 got_ref_list_free(&refs);
2290 return error;
2293 __dead static void
2294 usage_diff(void)
2296 endwin();
2297 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2298 getprogname());
2299 exit(1);
2302 static char *
2303 parse_next_line(FILE *f, size_t *len)
2305 char *line;
2306 size_t linelen;
2307 size_t lineno;
2308 const char delim[3] = { '\0', '\0', '\0'};
2310 line = fparseln(f, &linelen, &lineno, delim, 0);
2311 if (len)
2312 *len = linelen;
2313 return line;
2316 static const struct got_error *
2317 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2318 int *last_displayed_line, int *eof, int max_lines,
2319 char *header)
2321 const struct got_error *err;
2322 int nlines = 0, nprinted = 0;
2323 char *line;
2324 size_t len;
2325 wchar_t *wline;
2326 int width;
2328 rewind(f);
2329 werase(view->window);
2331 if (header) {
2332 err = format_line(&wline, &width, header, view->ncols);
2333 if (err) {
2334 return err;
2337 if (view_needs_focus_indication(view))
2338 wstandout(view->window);
2339 waddwstr(view->window, wline);
2340 if (view_needs_focus_indication(view))
2341 wstandend(view->window);
2342 if (width < view->ncols - 1)
2343 waddch(view->window, '\n');
2345 if (max_lines <= 1)
2346 return NULL;
2347 max_lines--;
2350 *eof = 0;
2351 while (nprinted < max_lines) {
2352 line = parse_next_line(f, &len);
2353 if (line == NULL) {
2354 *eof = 1;
2355 break;
2357 if (++nlines < *first_displayed_line) {
2358 free(line);
2359 continue;
2362 err = format_line(&wline, &width, line, view->ncols);
2363 if (err) {
2364 free(line);
2365 return err;
2367 waddwstr(view->window, wline);
2368 if (width < view->ncols - 1)
2369 waddch(view->window, '\n');
2370 if (++nprinted == 1)
2371 *first_displayed_line = nlines;
2372 free(line);
2373 free(wline);
2374 wline = NULL;
2376 *last_displayed_line = nlines;
2378 view_vborder(view);
2380 if (*eof) {
2381 while (nprinted < view->nlines) {
2382 waddch(view->window, '\n');
2383 nprinted++;
2386 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2387 if (err) {
2388 return err;
2391 wstandout(view->window);
2392 waddwstr(view->window, wline);
2393 wstandend(view->window);
2396 return NULL;
2399 static char *
2400 get_datestr(time_t *time, char *datebuf)
2402 char *p, *s = ctime_r(time, datebuf);
2403 p = strchr(s, '\n');
2404 if (p)
2405 *p = '\0';
2406 return s;
2409 static const struct got_error *
2410 write_commit_info(struct got_object_id *commit_id,
2411 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2413 const struct got_error *err = NULL;
2414 char datebuf[26];
2415 struct got_commit_object *commit;
2416 char *id_str = NULL;
2417 time_t committer_time;
2418 const char *author, *committer;
2419 char *refs_str = NULL;
2421 if (refs) {
2422 err = build_refs_str(&refs_str, refs, commit_id);
2423 if (err)
2424 return err;
2427 err = got_object_open_as_commit(&commit, repo, commit_id);
2428 if (err)
2429 return err;
2431 err = got_object_id_str(&id_str, commit_id);
2432 if (err) {
2433 err = got_error_from_errno("got_object_id_str");
2434 goto done;
2437 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2438 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2439 err = got_error_from_errno("fprintf");
2440 goto done;
2442 if (fprintf(outfile, "from: %s\n",
2443 got_object_commit_get_author(commit)) < 0) {
2444 err = got_error_from_errno("fprintf");
2445 goto done;
2447 committer_time = got_object_commit_get_committer_time(commit);
2448 if (fprintf(outfile, "date: %s UTC\n",
2449 get_datestr(&committer_time, datebuf)) < 0) {
2450 err = got_error_from_errno("fprintf");
2451 goto done;
2453 author = got_object_commit_get_author(commit);
2454 committer = got_object_commit_get_committer(commit);
2455 if (strcmp(author, committer) != 0 &&
2456 fprintf(outfile, "via: %s\n", committer) < 0) {
2457 err = got_error_from_errno("fprintf");
2458 goto done;
2460 if (fprintf(outfile, "%s\n",
2461 got_object_commit_get_logmsg(commit)) < 0) {
2462 err = got_error_from_errno("fprintf");
2463 goto done;
2465 done:
2466 free(id_str);
2467 free(refs_str);
2468 got_object_commit_close(commit);
2469 return err;
2472 static const struct got_error *
2473 create_diff(struct tog_diff_view_state *s)
2475 const struct got_error *err = NULL;
2476 FILE *f = NULL;
2477 int obj_type;
2479 f = got_opentemp();
2480 if (f == NULL) {
2481 err = got_error_from_errno("got_opentemp");
2482 goto done;
2484 if (s->f && fclose(s->f) != 0) {
2485 err = got_error_from_errno("fclose");
2486 goto done;
2488 s->f = f;
2490 if (s->id1)
2491 err = got_object_get_type(&obj_type, s->repo, s->id1);
2492 else
2493 err = got_object_get_type(&obj_type, s->repo, s->id2);
2494 if (err)
2495 goto done;
2497 switch (obj_type) {
2498 case GOT_OBJ_TYPE_BLOB:
2499 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2500 s->diff_context, s->repo, f);
2501 break;
2502 case GOT_OBJ_TYPE_TREE:
2503 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2504 s->diff_context, s->repo, f);
2505 break;
2506 case GOT_OBJ_TYPE_COMMIT: {
2507 const struct got_object_id_queue *parent_ids;
2508 struct got_object_qid *pid;
2509 struct got_commit_object *commit2;
2511 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2512 if (err)
2513 break;
2514 /* Show commit info if we're diffing to a parent/root commit. */
2515 if (s->id1 == NULL)
2516 write_commit_info(s->id2, s->refs, s->repo, f);
2517 else {
2518 parent_ids = got_object_commit_get_parent_ids(commit2);
2519 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2520 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2521 write_commit_info(s->id2, s->refs,
2522 s->repo, f);
2523 break;
2527 got_object_commit_close(commit2);
2529 err = got_diff_objects_as_commits(s->id1, s->id2,
2530 s->diff_context, s->repo, f);
2531 break;
2533 default:
2534 err = got_error(GOT_ERR_OBJ_TYPE);
2535 break;
2537 done:
2538 if (f && fflush(f) != 0 && err == NULL)
2539 err = got_error_from_errno("fflush");
2540 return err;
2543 static void
2544 diff_view_indicate_progress(struct tog_view *view)
2546 mvwaddstr(view->window, 0, 0, "diffing...");
2547 update_panels();
2548 doupdate();
2551 static const struct got_error *
2552 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2553 struct got_object_id *id2, struct tog_view *log_view,
2554 struct got_reflist_head *refs, struct got_repository *repo)
2556 const struct got_error *err;
2558 if (id1 != NULL && id2 != NULL) {
2559 int type1, type2;
2560 err = got_object_get_type(&type1, repo, id1);
2561 if (err)
2562 return err;
2563 err = got_object_get_type(&type2, repo, id2);
2564 if (err)
2565 return err;
2567 if (type1 != type2)
2568 return got_error(GOT_ERR_OBJ_TYPE);
2571 if (id1) {
2572 view->state.diff.id1 = got_object_id_dup(id1);
2573 if (view->state.diff.id1 == NULL)
2574 return got_error_from_errno("got_object_id_dup");
2575 } else
2576 view->state.diff.id1 = NULL;
2578 view->state.diff.id2 = got_object_id_dup(id2);
2579 if (view->state.diff.id2 == NULL) {
2580 free(view->state.diff.id1);
2581 view->state.diff.id1 = NULL;
2582 return got_error_from_errno("got_object_id_dup");
2584 view->state.diff.f = NULL;
2585 view->state.diff.first_displayed_line = 1;
2586 view->state.diff.last_displayed_line = view->nlines;
2587 view->state.diff.diff_context = 3;
2588 view->state.diff.log_view = log_view;
2589 view->state.diff.repo = repo;
2590 view->state.diff.refs = refs;
2592 if (log_view && view_is_splitscreen(view))
2593 show_log_view(log_view); /* draw vborder */
2594 diff_view_indicate_progress(view);
2596 err = create_diff(&view->state.diff);
2597 if (err) {
2598 free(view->state.diff.id1);
2599 view->state.diff.id1 = NULL;
2600 free(view->state.diff.id2);
2601 view->state.diff.id2 = NULL;
2602 return err;
2605 view->show = show_diff_view;
2606 view->input = input_diff_view;
2607 view->close = close_diff_view;
2609 return NULL;
2612 static const struct got_error *
2613 close_diff_view(struct tog_view *view)
2615 const struct got_error *err = NULL;
2617 free(view->state.diff.id1);
2618 view->state.diff.id1 = NULL;
2619 free(view->state.diff.id2);
2620 view->state.diff.id2 = NULL;
2621 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2622 err = got_error_from_errno("fclose");
2623 return err;
2626 static const struct got_error *
2627 show_diff_view(struct tog_view *view)
2629 const struct got_error *err;
2630 struct tog_diff_view_state *s = &view->state.diff;
2631 char *id_str1 = NULL, *id_str2, *header;
2633 if (s->id1) {
2634 err = got_object_id_str(&id_str1, s->id1);
2635 if (err)
2636 return err;
2638 err = got_object_id_str(&id_str2, s->id2);
2639 if (err)
2640 return err;
2642 if (asprintf(&header, "diff %s %s",
2643 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2644 err = got_error_from_errno("asprintf");
2645 free(id_str1);
2646 free(id_str2);
2647 return err;
2649 free(id_str1);
2650 free(id_str2);
2652 return draw_file(view, s->f, &s->first_displayed_line,
2653 &s->last_displayed_line, &s->eof, view->nlines,
2654 header);
2657 static const struct got_error *
2658 set_selected_commit(struct tog_diff_view_state *s,
2659 struct commit_queue_entry *entry)
2661 const struct got_error *err;
2662 const struct got_object_id_queue *parent_ids;
2663 struct got_commit_object *selected_commit;
2664 struct got_object_qid *pid;
2666 free(s->id2);
2667 s->id2 = got_object_id_dup(entry->id);
2668 if (s->id2 == NULL)
2669 return got_error_from_errno("got_object_id_dup");
2671 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2672 if (err)
2673 return err;
2674 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2675 free(s->id1);
2676 pid = SIMPLEQ_FIRST(parent_ids);
2677 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2678 got_object_commit_close(selected_commit);
2679 return NULL;
2682 static const struct got_error *
2683 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2684 struct tog_view **focus_view, struct tog_view *view, int ch)
2686 const struct got_error *err = NULL;
2687 struct tog_diff_view_state *s = &view->state.diff;
2688 struct tog_log_view_state *ls;
2689 struct commit_queue_entry *entry;
2690 int i;
2692 switch (ch) {
2693 case 'k':
2694 case KEY_UP:
2695 if (s->first_displayed_line > 1)
2696 s->first_displayed_line--;
2697 break;
2698 case KEY_PPAGE:
2699 case CTRL('b'):
2700 if (s->first_displayed_line == 1)
2701 break;
2702 i = 0;
2703 while (i++ < view->nlines - 1 &&
2704 s->first_displayed_line > 1)
2705 s->first_displayed_line--;
2706 break;
2707 case 'j':
2708 case KEY_DOWN:
2709 if (!s->eof)
2710 s->first_displayed_line++;
2711 break;
2712 case KEY_NPAGE:
2713 case CTRL('f'):
2714 case ' ':
2715 if (s->eof)
2716 break;
2717 i = 0;
2718 while (!s->eof && i++ < view->nlines - 1) {
2719 char *line;
2720 line = parse_next_line(s->f, NULL);
2721 s->first_displayed_line++;
2722 if (line == NULL)
2723 break;
2725 break;
2726 case '[':
2727 if (s->diff_context > 0) {
2728 s->diff_context--;
2729 diff_view_indicate_progress(view);
2730 err = create_diff(s);
2732 break;
2733 case ']':
2734 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2735 s->diff_context++;
2736 diff_view_indicate_progress(view);
2737 err = create_diff(s);
2739 break;
2740 case '<':
2741 case ',':
2742 if (s->log_view == NULL)
2743 break;
2744 ls = &s->log_view->state.log;
2745 entry = TAILQ_PREV(ls->selected_entry,
2746 commit_queue_head, entry);
2747 if (entry == NULL)
2748 break;
2750 err = input_log_view(NULL, NULL, NULL, s->log_view,
2751 KEY_UP);
2752 if (err)
2753 break;
2755 err = set_selected_commit(s, entry);
2756 if (err)
2757 break;
2759 s->first_displayed_line = 1;
2760 s->last_displayed_line = view->nlines;
2762 diff_view_indicate_progress(view);
2763 err = create_diff(s);
2764 break;
2765 case '>':
2766 case '.':
2767 if (s->log_view == NULL)
2768 break;
2769 ls = &s->log_view->state.log;
2771 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2772 ls->thread_args.commits_needed++;
2774 /* Display "loading..." in log view. */
2775 show_log_view(s->log_view);
2776 update_panels();
2777 doupdate();
2779 err = trigger_log_thread(1 /* load_all */,
2780 &ls->thread_args.commits_needed,
2781 &ls->thread_args.log_complete,
2782 &ls->thread_args.need_commits);
2783 if (err)
2784 break;
2786 err = input_log_view(NULL, NULL, NULL, s->log_view,
2787 KEY_DOWN);
2788 if (err)
2789 break;
2791 entry = TAILQ_NEXT(ls->selected_entry, entry);
2792 if (entry == NULL)
2793 break;
2795 err = set_selected_commit(s, entry);
2796 if (err)
2797 break;
2799 s->first_displayed_line = 1;
2800 s->last_displayed_line = view->nlines;
2802 diff_view_indicate_progress(view);
2803 err = create_diff(s);
2804 break;
2805 default:
2806 break;
2809 return err;
2812 static const struct got_error *
2813 cmd_diff(int argc, char *argv[])
2815 const struct got_error *error = NULL;
2816 struct got_repository *repo = NULL;
2817 struct got_reflist_head refs;
2818 struct got_object_id *id1 = NULL, *id2 = NULL;
2819 char *repo_path = NULL;
2820 char *id_str1 = NULL, *id_str2 = NULL;
2821 int ch;
2822 struct tog_view *view;
2824 SIMPLEQ_INIT(&refs);
2826 #ifndef PROFILE
2827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2828 NULL) == -1)
2829 err(1, "pledge");
2830 #endif
2832 while ((ch = getopt(argc, argv, "")) != -1) {
2833 switch (ch) {
2834 default:
2835 usage_diff();
2836 /* NOTREACHED */
2840 argc -= optind;
2841 argv += optind;
2843 if (argc == 0) {
2844 usage_diff(); /* TODO show local worktree changes */
2845 } else if (argc == 2) {
2846 repo_path = getcwd(NULL, 0);
2847 if (repo_path == NULL)
2848 return got_error_from_errno("getcwd");
2849 id_str1 = argv[0];
2850 id_str2 = argv[1];
2851 } else if (argc == 3) {
2852 repo_path = realpath(argv[0], NULL);
2853 if (repo_path == NULL)
2854 return got_error_from_errno2("realpath", argv[0]);
2855 id_str1 = argv[1];
2856 id_str2 = argv[2];
2857 } else
2858 usage_diff();
2860 init_curses();
2862 error = got_repo_open(&repo, repo_path);
2863 if (error)
2864 goto done;
2866 error = apply_unveil(got_repo_get_path(repo), NULL);
2867 if (error)
2868 goto done;
2870 error = got_repo_match_object_id_prefix(&id1, id_str1,
2871 GOT_OBJ_TYPE_ANY, repo);
2872 if (error)
2873 goto done;
2875 error = got_repo_match_object_id_prefix(&id2, id_str2,
2876 GOT_OBJ_TYPE_ANY, repo);
2877 if (error)
2878 goto done;
2880 error = got_ref_list(&refs, repo);
2881 if (error)
2882 goto done;
2884 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2885 if (view == NULL) {
2886 error = got_error_from_errno("view_open");
2887 goto done;
2889 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2890 if (error)
2891 goto done;
2892 error = view_loop(view);
2893 done:
2894 free(repo_path);
2895 if (repo)
2896 got_repo_close(repo);
2897 got_ref_list_free(&refs);
2898 return error;
2901 __dead static void
2902 usage_blame(void)
2904 endwin();
2905 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2906 getprogname());
2907 exit(1);
2910 struct tog_blame_line {
2911 int annotated;
2912 struct got_object_id *id;
2915 static const struct got_error *
2916 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2917 const char *path, struct tog_blame_line *lines, int nlines,
2918 int blame_complete, int selected_line, int *first_displayed_line,
2919 int *last_displayed_line, int *eof, int max_lines)
2921 const struct got_error *err;
2922 int lineno = 0, nprinted = 0;
2923 char *line;
2924 size_t len;
2925 wchar_t *wline;
2926 int width, wlimit;
2927 struct tog_blame_line *blame_line;
2928 struct got_object_id *prev_id = NULL;
2929 char *id_str;
2931 err = got_object_id_str(&id_str, id);
2932 if (err)
2933 return err;
2935 rewind(f);
2936 werase(view->window);
2938 if (asprintf(&line, "commit %s", id_str) == -1) {
2939 err = got_error_from_errno("asprintf");
2940 free(id_str);
2941 return err;
2944 err = format_line(&wline, &width, line, view->ncols);
2945 free(line);
2946 line = NULL;
2947 if (view_needs_focus_indication(view))
2948 wstandout(view->window);
2949 waddwstr(view->window, wline);
2950 if (view_needs_focus_indication(view))
2951 wstandend(view->window);
2952 free(wline);
2953 wline = NULL;
2954 if (width < view->ncols - 1)
2955 waddch(view->window, '\n');
2957 if (asprintf(&line, "[%d/%d] %s%s",
2958 *first_displayed_line - 1 + selected_line, nlines,
2959 blame_complete ? "" : "annotating... ", path) == -1) {
2960 free(id_str);
2961 return got_error_from_errno("asprintf");
2963 free(id_str);
2964 err = format_line(&wline, &width, line, view->ncols);
2965 free(line);
2966 line = NULL;
2967 if (err)
2968 return err;
2969 waddwstr(view->window, wline);
2970 free(wline);
2971 wline = NULL;
2972 if (width < view->ncols - 1)
2973 waddch(view->window, '\n');
2975 *eof = 0;
2976 while (nprinted < max_lines - 2) {
2977 line = parse_next_line(f, &len);
2978 if (line == NULL) {
2979 *eof = 1;
2980 break;
2982 if (++lineno < *first_displayed_line) {
2983 free(line);
2984 continue;
2987 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2988 err = format_line(&wline, &width, line, wlimit);
2989 if (err) {
2990 free(line);
2991 return err;
2994 if (view->focussed && nprinted == selected_line - 1)
2995 wstandout(view->window);
2997 blame_line = &lines[lineno - 1];
2998 if (blame_line->annotated && prev_id &&
2999 got_object_id_cmp(prev_id, blame_line->id) == 0)
3000 waddstr(view->window, " ");
3001 else if (blame_line->annotated) {
3002 char *id_str;
3003 err = got_object_id_str(&id_str, blame_line->id);
3004 if (err) {
3005 free(line);
3006 free(wline);
3007 return err;
3009 wprintw(view->window, "%.8s ", id_str);
3010 free(id_str);
3011 prev_id = blame_line->id;
3012 } else {
3013 waddstr(view->window, "........ ");
3014 prev_id = NULL;
3017 waddwstr(view->window, wline);
3018 while (width < wlimit) {
3019 waddch(view->window, ' ');
3020 width++;
3022 if (view->focussed && nprinted == selected_line - 1)
3023 wstandend(view->window);
3024 if (++nprinted == 1)
3025 *first_displayed_line = lineno;
3026 free(line);
3027 free(wline);
3028 wline = NULL;
3030 *last_displayed_line = lineno;
3032 view_vborder(view);
3034 return NULL;
3037 static const struct got_error *
3038 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3040 const struct got_error *err = NULL;
3041 struct tog_blame_cb_args *a = arg;
3042 struct tog_blame_line *line;
3043 int errcode;
3045 if (nlines != a->nlines ||
3046 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3047 return got_error(GOT_ERR_RANGE);
3049 errcode = pthread_mutex_lock(&tog_mutex);
3050 if (errcode)
3051 return got_error_set_errno(errcode, "pthread_mutex_lock");
3053 if (*a->quit) { /* user has quit the blame view */
3054 err = got_error(GOT_ERR_ITER_COMPLETED);
3055 goto done;
3058 if (lineno == -1)
3059 goto done; /* no change in this commit */
3061 line = &a->lines[lineno - 1];
3062 if (line->annotated)
3063 goto done;
3065 line->id = got_object_id_dup(id);
3066 if (line->id == NULL) {
3067 err = got_error_from_errno("got_object_id_dup");
3068 goto done;
3070 line->annotated = 1;
3071 done:
3072 errcode = pthread_mutex_unlock(&tog_mutex);
3073 if (errcode)
3074 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3075 return err;
3078 static void *
3079 blame_thread(void *arg)
3081 const struct got_error *err;
3082 struct tog_blame_thread_args *ta = arg;
3083 struct tog_blame_cb_args *a = ta->cb_args;
3084 int errcode;
3086 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3087 blame_cb, ta->cb_args);
3089 errcode = pthread_mutex_lock(&tog_mutex);
3090 if (errcode)
3091 return (void *)got_error_set_errno(errcode,
3092 "pthread_mutex_lock");
3094 got_repo_close(ta->repo);
3095 ta->repo = NULL;
3096 *ta->complete = 1;
3098 errcode = pthread_mutex_unlock(&tog_mutex);
3099 if (errcode && err == NULL)
3100 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3102 return (void *)err;
3105 static struct got_object_id *
3106 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3107 int selected_line)
3109 struct tog_blame_line *line;
3111 line = &lines[first_displayed_line - 1 + selected_line - 1];
3112 if (!line->annotated)
3113 return NULL;
3115 return line->id;
3118 static const struct got_error *
3119 stop_blame(struct tog_blame *blame)
3121 const struct got_error *err = NULL;
3122 int i;
3124 if (blame->thread) {
3125 int errcode;
3126 errcode = pthread_mutex_unlock(&tog_mutex);
3127 if (errcode)
3128 return got_error_set_errno(errcode,
3129 "pthread_mutex_unlock");
3130 errcode = pthread_join(blame->thread, (void **)&err);
3131 if (errcode)
3132 return got_error_set_errno(errcode, "pthread_join");
3133 errcode = pthread_mutex_lock(&tog_mutex);
3134 if (errcode)
3135 return got_error_set_errno(errcode,
3136 "pthread_mutex_lock");
3137 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3138 err = NULL;
3139 blame->thread = NULL;
3141 if (blame->thread_args.repo) {
3142 got_repo_close(blame->thread_args.repo);
3143 blame->thread_args.repo = NULL;
3145 if (blame->f) {
3146 if (fclose(blame->f) != 0 && err == NULL)
3147 err = got_error_from_errno("fclose");
3148 blame->f = NULL;
3150 if (blame->lines) {
3151 for (i = 0; i < blame->nlines; i++)
3152 free(blame->lines[i].id);
3153 free(blame->lines);
3154 blame->lines = NULL;
3156 free(blame->cb_args.commit_id);
3157 blame->cb_args.commit_id = NULL;
3159 return err;
3162 static const struct got_error *
3163 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3164 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3165 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3166 struct got_repository *repo)
3168 const struct got_error *err = NULL;
3169 struct got_blob_object *blob = NULL;
3170 struct got_repository *thread_repo = NULL;
3171 struct got_object_id *obj_id = NULL;
3172 int obj_type;
3174 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3175 if (err)
3176 return err;
3177 if (obj_id == NULL)
3178 return got_error(GOT_ERR_NO_OBJ);
3180 err = got_object_get_type(&obj_type, repo, obj_id);
3181 if (err)
3182 goto done;
3184 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3185 err = got_error(GOT_ERR_OBJ_TYPE);
3186 goto done;
3189 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3190 if (err)
3191 goto done;
3192 blame->f = got_opentemp();
3193 if (blame->f == NULL) {
3194 err = got_error_from_errno("got_opentemp");
3195 goto done;
3197 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3198 &blame->line_offsets, blame->f, blob);
3199 if (err)
3200 goto done;
3202 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3203 if (blame->lines == NULL) {
3204 err = got_error_from_errno("calloc");
3205 goto done;
3208 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3209 if (err)
3210 goto done;
3212 blame->cb_args.view = view;
3213 blame->cb_args.lines = blame->lines;
3214 blame->cb_args.nlines = blame->nlines;
3215 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3216 if (blame->cb_args.commit_id == NULL) {
3217 err = got_error_from_errno("got_object_id_dup");
3218 goto done;
3220 blame->cb_args.quit = done;
3222 blame->thread_args.path = path;
3223 blame->thread_args.repo = thread_repo;
3224 blame->thread_args.cb_args = &blame->cb_args;
3225 blame->thread_args.complete = blame_complete;
3226 *blame_complete = 0;
3228 done:
3229 if (blob)
3230 got_object_blob_close(blob);
3231 free(obj_id);
3232 if (err)
3233 stop_blame(blame);
3234 return err;
3237 static const struct got_error *
3238 open_blame_view(struct tog_view *view, char *path,
3239 struct got_object_id *commit_id, struct got_reflist_head *refs,
3240 struct got_repository *repo)
3242 const struct got_error *err = NULL;
3243 struct tog_blame_view_state *s = &view->state.blame;
3245 SIMPLEQ_INIT(&s->blamed_commits);
3247 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3248 if (err)
3249 return err;
3251 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3252 s->first_displayed_line = 1;
3253 s->last_displayed_line = view->nlines;
3254 s->selected_line = 1;
3255 s->blame_complete = 0;
3256 s->path = path;
3257 if (s->path == NULL)
3258 return got_error_from_errno("open_blame_view");
3259 s->repo = repo;
3260 s->refs = refs;
3261 s->commit_id = commit_id;
3262 memset(&s->blame, 0, sizeof(s->blame));
3264 view->show = show_blame_view;
3265 view->input = input_blame_view;
3266 view->close = close_blame_view;
3267 view->search_start = search_start_blame_view;
3268 view->search_next = search_next_blame_view;
3270 return run_blame(&s->blame, view, &s->blame_complete,
3271 &s->first_displayed_line, &s->last_displayed_line,
3272 &s->selected_line, &s->done, &s->eof, s->path,
3273 s->blamed_commit->id, s->repo);
3276 static const struct got_error *
3277 close_blame_view(struct tog_view *view)
3279 const struct got_error *err = NULL;
3280 struct tog_blame_view_state *s = &view->state.blame;
3282 if (s->blame.thread)
3283 err = stop_blame(&s->blame);
3285 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3286 struct got_object_qid *blamed_commit;
3287 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3288 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3289 got_object_qid_free(blamed_commit);
3292 free(s->path);
3294 return err;
3297 static const struct got_error *
3298 search_start_blame_view(struct tog_view *view)
3300 struct tog_blame_view_state *s = &view->state.blame;
3302 s->matched_line = 0;
3303 return NULL;
3306 static int
3307 match_line(const char *line, regex_t *regex)
3309 regmatch_t regmatch;
3311 return regexec(regex, line, 1, &regmatch, 0) == 0;
3315 static const struct got_error *
3316 search_next_blame_view(struct tog_view *view)
3318 struct tog_blame_view_state *s = &view->state.blame;
3319 int lineno;
3321 if (!view->searching) {
3322 view->search_next_done = 1;
3323 return NULL;
3326 if (s->matched_line) {
3327 if (view->searching == TOG_SEARCH_FORWARD)
3328 lineno = s->matched_line + 1;
3329 else
3330 lineno = s->matched_line - 1;
3331 } else {
3332 if (view->searching == TOG_SEARCH_FORWARD)
3333 lineno = 1;
3334 else
3335 lineno = s->blame.nlines;
3338 while (1) {
3339 char *line = NULL;
3340 off_t offset;
3341 size_t len;
3343 if (lineno <= 0 || lineno > s->blame.nlines) {
3344 if (s->matched_line == 0) {
3345 view->search_next_done = 1;
3346 free(line);
3347 break;
3350 if (view->searching == TOG_SEARCH_FORWARD)
3351 lineno = 1;
3352 else
3353 lineno = s->blame.nlines;
3356 offset = s->blame.line_offsets[lineno - 1];
3357 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3358 free(line);
3359 return got_error_from_errno("fseeko");
3361 free(line);
3362 line = parse_next_line(s->blame.f, &len);
3363 if (line && match_line(line, &view->regex)) {
3364 view->search_next_done = 1;
3365 s->matched_line = lineno;
3366 free(line);
3367 break;
3369 free(line);
3370 if (view->searching == TOG_SEARCH_FORWARD)
3371 lineno++;
3372 else
3373 lineno--;
3376 if (s->matched_line) {
3377 s->first_displayed_line = s->matched_line;
3378 s->selected_line = 1;
3381 return NULL;
3384 static const struct got_error *
3385 show_blame_view(struct tog_view *view)
3387 const struct got_error *err = NULL;
3388 struct tog_blame_view_state *s = &view->state.blame;
3389 int errcode;
3391 if (s->blame.thread == NULL) {
3392 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3393 &s->blame.thread_args);
3394 if (errcode)
3395 return got_error_set_errno(errcode, "pthread_create");
3398 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3399 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3400 s->selected_line, &s->first_displayed_line,
3401 &s->last_displayed_line, &s->eof, view->nlines);
3403 view_vborder(view);
3404 return err;
3407 static const struct got_error *
3408 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3409 struct tog_view **focus_view, struct tog_view *view, int ch)
3411 const struct got_error *err = NULL, *thread_err = NULL;
3412 struct tog_view *diff_view;
3413 struct tog_blame_view_state *s = &view->state.blame;
3414 int begin_x = 0;
3416 switch (ch) {
3417 case 'q':
3418 s->done = 1;
3419 break;
3420 case 'k':
3421 case KEY_UP:
3422 if (s->selected_line > 1)
3423 s->selected_line--;
3424 else if (s->selected_line == 1 &&
3425 s->first_displayed_line > 1)
3426 s->first_displayed_line--;
3427 break;
3428 case KEY_PPAGE:
3429 if (s->first_displayed_line == 1) {
3430 s->selected_line = 1;
3431 break;
3433 if (s->first_displayed_line > view->nlines - 2)
3434 s->first_displayed_line -=
3435 (view->nlines - 2);
3436 else
3437 s->first_displayed_line = 1;
3438 break;
3439 case 'j':
3440 case KEY_DOWN:
3441 if (s->selected_line < view->nlines - 2 &&
3442 s->first_displayed_line +
3443 s->selected_line <= s->blame.nlines)
3444 s->selected_line++;
3445 else if (s->last_displayed_line <
3446 s->blame.nlines)
3447 s->first_displayed_line++;
3448 break;
3449 case 'b':
3450 case 'p': {
3451 struct got_object_id *id = NULL;
3452 id = get_selected_commit_id(s->blame.lines,
3453 s->first_displayed_line, s->selected_line);
3454 if (id == NULL)
3455 break;
3456 if (ch == 'p') {
3457 struct got_commit_object *commit;
3458 struct got_object_qid *pid;
3459 struct got_object_id *blob_id = NULL;
3460 int obj_type;
3461 err = got_object_open_as_commit(&commit,
3462 s->repo, id);
3463 if (err)
3464 break;
3465 pid = SIMPLEQ_FIRST(
3466 got_object_commit_get_parent_ids(commit));
3467 if (pid == NULL) {
3468 got_object_commit_close(commit);
3469 break;
3471 /* Check if path history ends here. */
3472 err = got_object_id_by_path(&blob_id, s->repo,
3473 pid->id, s->path);
3474 if (err) {
3475 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3476 err = NULL;
3477 got_object_commit_close(commit);
3478 break;
3480 err = got_object_get_type(&obj_type, s->repo,
3481 blob_id);
3482 free(blob_id);
3483 /* Can't blame non-blob type objects. */
3484 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3485 got_object_commit_close(commit);
3486 break;
3488 err = got_object_qid_alloc(&s->blamed_commit,
3489 pid->id);
3490 got_object_commit_close(commit);
3491 } else {
3492 if (got_object_id_cmp(id,
3493 s->blamed_commit->id) == 0)
3494 break;
3495 err = got_object_qid_alloc(&s->blamed_commit,
3496 id);
3498 if (err)
3499 break;
3500 s->done = 1;
3501 thread_err = stop_blame(&s->blame);
3502 s->done = 0;
3503 if (thread_err)
3504 break;
3505 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3506 s->blamed_commit, entry);
3507 err = run_blame(&s->blame, view, &s->blame_complete,
3508 &s->first_displayed_line, &s->last_displayed_line,
3509 &s->selected_line, &s->done, &s->eof,
3510 s->path, s->blamed_commit->id, s->repo);
3511 if (err)
3512 break;
3513 break;
3515 case 'B': {
3516 struct got_object_qid *first;
3517 first = SIMPLEQ_FIRST(&s->blamed_commits);
3518 if (!got_object_id_cmp(first->id, s->commit_id))
3519 break;
3520 s->done = 1;
3521 thread_err = stop_blame(&s->blame);
3522 s->done = 0;
3523 if (thread_err)
3524 break;
3525 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3526 got_object_qid_free(s->blamed_commit);
3527 s->blamed_commit =
3528 SIMPLEQ_FIRST(&s->blamed_commits);
3529 err = run_blame(&s->blame, view, &s->blame_complete,
3530 &s->first_displayed_line, &s->last_displayed_line,
3531 &s->selected_line, &s->done, &s->eof, s->path,
3532 s->blamed_commit->id, s->repo);
3533 if (err)
3534 break;
3535 break;
3537 case KEY_ENTER:
3538 case '\r': {
3539 struct got_object_id *id = NULL;
3540 struct got_object_qid *pid;
3541 struct got_commit_object *commit = NULL;
3542 id = get_selected_commit_id(s->blame.lines,
3543 s->first_displayed_line, s->selected_line);
3544 if (id == NULL)
3545 break;
3546 err = got_object_open_as_commit(&commit, s->repo, id);
3547 if (err)
3548 break;
3549 pid = SIMPLEQ_FIRST(
3550 got_object_commit_get_parent_ids(commit));
3551 if (view_is_parent_view(view))
3552 begin_x = view_split_begin_x(view->begin_x);
3553 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3554 if (diff_view == NULL) {
3555 got_object_commit_close(commit);
3556 err = got_error_from_errno("view_open");
3557 break;
3559 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3560 id, NULL, s->refs, s->repo);
3561 got_object_commit_close(commit);
3562 if (err) {
3563 view_close(diff_view);
3564 break;
3566 if (view_is_parent_view(view)) {
3567 err = view_close_child(view);
3568 if (err)
3569 break;
3570 err = view_set_child(view, diff_view);
3571 if (err) {
3572 view_close(diff_view);
3573 break;
3575 *focus_view = diff_view;
3576 view->child_focussed = 1;
3577 } else
3578 *new_view = diff_view;
3579 if (err)
3580 break;
3581 break;
3583 case KEY_NPAGE:
3584 case ' ':
3585 if (s->last_displayed_line >= s->blame.nlines &&
3586 s->selected_line >= MIN(s->blame.nlines,
3587 view->nlines - 2)) {
3588 break;
3590 if (s->last_displayed_line >= s->blame.nlines &&
3591 s->selected_line < view->nlines - 2) {
3592 s->selected_line = MIN(s->blame.nlines,
3593 view->nlines - 2);
3594 break;
3596 if (s->last_displayed_line + view->nlines - 2
3597 <= s->blame.nlines)
3598 s->first_displayed_line +=
3599 view->nlines - 2;
3600 else
3601 s->first_displayed_line =
3602 s->blame.nlines -
3603 (view->nlines - 3);
3604 break;
3605 case KEY_RESIZE:
3606 if (s->selected_line > view->nlines - 2) {
3607 s->selected_line = MIN(s->blame.nlines,
3608 view->nlines - 2);
3610 break;
3611 default:
3612 break;
3614 return thread_err ? thread_err : err;
3617 static const struct got_error *
3618 cmd_blame(int argc, char *argv[])
3620 const struct got_error *error;
3621 struct got_repository *repo = NULL;
3622 struct got_reflist_head refs;
3623 struct got_worktree *worktree = NULL;
3624 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3625 struct got_object_id *commit_id = NULL;
3626 char *commit_id_str = NULL;
3627 int ch;
3628 struct tog_view *view;
3630 SIMPLEQ_INIT(&refs);
3632 #ifndef PROFILE
3633 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3634 NULL) == -1)
3635 err(1, "pledge");
3636 #endif
3638 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3639 switch (ch) {
3640 case 'c':
3641 commit_id_str = optarg;
3642 break;
3643 case 'r':
3644 repo_path = realpath(optarg, NULL);
3645 if (repo_path == NULL)
3646 err(1, "-r option");
3647 break;
3648 default:
3649 usage_blame();
3650 /* NOTREACHED */
3654 argc -= optind;
3655 argv += optind;
3657 if (argc == 1)
3658 path = argv[0];
3659 else
3660 usage_blame();
3662 cwd = getcwd(NULL, 0);
3663 if (cwd == NULL) {
3664 error = got_error_from_errno("getcwd");
3665 goto done;
3667 if (repo_path == NULL) {
3668 error = got_worktree_open(&worktree, cwd);
3669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3670 goto done;
3671 else
3672 error = NULL;
3673 if (worktree) {
3674 repo_path =
3675 strdup(got_worktree_get_repo_path(worktree));
3676 if (repo_path == NULL)
3677 error = got_error_from_errno("strdup");
3678 if (error)
3679 goto done;
3680 } else {
3681 repo_path = strdup(cwd);
3682 if (repo_path == NULL) {
3683 error = got_error_from_errno("strdup");
3684 goto done;
3689 init_curses();
3691 error = got_repo_open(&repo, repo_path);
3692 if (error != NULL)
3693 goto done;
3695 error = apply_unveil(got_repo_get_path(repo), NULL);
3696 if (error)
3697 goto done;
3699 if (worktree) {
3700 const char *prefix = got_worktree_get_path_prefix(worktree);
3701 char *p, *worktree_subdir = cwd +
3702 strlen(got_worktree_get_root_path(worktree));
3703 if (asprintf(&p, "%s%s%s%s%s",
3704 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3705 worktree_subdir, worktree_subdir[0] ? "/" : "",
3706 path) == -1) {
3707 error = got_error_from_errno("asprintf");
3708 goto done;
3710 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3711 free(p);
3712 } else {
3713 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3715 if (error)
3716 goto done;
3718 if (commit_id_str == NULL) {
3719 struct got_reference *head_ref;
3720 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3721 if (error != NULL)
3722 goto done;
3723 error = got_ref_resolve(&commit_id, repo, head_ref);
3724 got_ref_close(head_ref);
3725 } else {
3726 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3727 if (error) {
3728 if (error->code != GOT_ERR_NOT_REF)
3729 goto done;
3730 error = got_repo_match_object_id_prefix(&commit_id,
3731 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3734 if (error != NULL)
3735 goto done;
3737 error = got_ref_list(&refs, repo);
3738 if (error)
3739 goto done;
3741 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3742 if (view == NULL) {
3743 error = got_error_from_errno("view_open");
3744 goto done;
3746 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3747 if (error)
3748 goto done;
3749 error = view_loop(view);
3750 done:
3751 free(repo_path);
3752 free(cwd);
3753 free(commit_id);
3754 if (worktree)
3755 got_worktree_close(worktree);
3756 if (repo)
3757 got_repo_close(repo);
3758 got_ref_list_free(&refs);
3759 return error;
3762 static const struct got_error *
3763 draw_tree_entries(struct tog_view *view,
3764 struct got_tree_entry **first_displayed_entry,
3765 struct got_tree_entry **last_displayed_entry,
3766 struct got_tree_entry **selected_entry, int *ndisplayed,
3767 const char *label, int show_ids, const char *parent_path,
3768 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3770 const struct got_error *err = NULL;
3771 struct got_tree_entry *te;
3772 wchar_t *wline;
3773 int width, n;
3775 *ndisplayed = 0;
3777 werase(view->window);
3779 if (limit == 0)
3780 return NULL;
3782 err = format_line(&wline, &width, label, view->ncols);
3783 if (err)
3784 return err;
3785 if (view_needs_focus_indication(view))
3786 wstandout(view->window);
3787 waddwstr(view->window, wline);
3788 if (view_needs_focus_indication(view))
3789 wstandend(view->window);
3790 free(wline);
3791 wline = NULL;
3792 if (width < view->ncols - 1)
3793 waddch(view->window, '\n');
3794 if (--limit <= 0)
3795 return NULL;
3796 err = format_line(&wline, &width, parent_path, view->ncols);
3797 if (err)
3798 return err;
3799 waddwstr(view->window, wline);
3800 free(wline);
3801 wline = NULL;
3802 if (width < view->ncols - 1)
3803 waddch(view->window, '\n');
3804 if (--limit <= 0)
3805 return NULL;
3806 waddch(view->window, '\n');
3807 if (--limit <= 0)
3808 return NULL;
3810 te = SIMPLEQ_FIRST(&entries->head);
3811 if (*first_displayed_entry == NULL) {
3812 if (selected == 0) {
3813 if (view->focussed)
3814 wstandout(view->window);
3815 *selected_entry = NULL;
3817 waddstr(view->window, " ..\n"); /* parent directory */
3818 if (selected == 0 && view->focussed)
3819 wstandend(view->window);
3820 (*ndisplayed)++;
3821 if (--limit <= 0)
3822 return NULL;
3823 n = 1;
3824 } else {
3825 n = 0;
3826 while (te != *first_displayed_entry)
3827 te = SIMPLEQ_NEXT(te, entry);
3830 while (te) {
3831 char *line = NULL, *id_str = NULL;
3833 if (show_ids) {
3834 err = got_object_id_str(&id_str, te->id);
3835 if (err)
3836 return got_error_from_errno(
3837 "got_object_id_str");
3839 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3840 te->name, S_ISDIR(te->mode) ? "/" :
3841 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3842 free(id_str);
3843 return got_error_from_errno("asprintf");
3845 free(id_str);
3846 err = format_line(&wline, &width, line, view->ncols);
3847 if (err) {
3848 free(line);
3849 break;
3851 if (n == selected) {
3852 if (view->focussed)
3853 wstandout(view->window);
3854 *selected_entry = te;
3856 waddwstr(view->window, wline);
3857 if (width < view->ncols - 1)
3858 waddch(view->window, '\n');
3859 if (n == selected && view->focussed)
3860 wstandend(view->window);
3861 free(line);
3862 free(wline);
3863 wline = NULL;
3864 n++;
3865 (*ndisplayed)++;
3866 *last_displayed_entry = te;
3867 if (--limit <= 0)
3868 break;
3869 te = SIMPLEQ_NEXT(te, entry);
3872 return err;
3875 static void
3876 tree_scroll_up(struct tog_view *view,
3877 struct got_tree_entry **first_displayed_entry, int maxscroll,
3878 const struct got_tree_entries *entries, int isroot)
3880 struct got_tree_entry *te, *prev;
3881 int i;
3883 if (*first_displayed_entry == NULL)
3884 return;
3886 te = SIMPLEQ_FIRST(&entries->head);
3887 if (*first_displayed_entry == te) {
3888 if (!isroot)
3889 *first_displayed_entry = NULL;
3890 return;
3893 /* XXX this is stupid... switch to TAILQ? */
3894 for (i = 0; i < maxscroll; i++) {
3895 while (te != *first_displayed_entry) {
3896 prev = te;
3897 te = SIMPLEQ_NEXT(te, entry);
3899 *first_displayed_entry = prev;
3900 te = SIMPLEQ_FIRST(&entries->head);
3902 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3903 *first_displayed_entry = NULL;
3906 static int
3907 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3908 struct got_tree_entry *last_displayed_entry,
3909 const struct got_tree_entries *entries)
3911 struct got_tree_entry *next, *last;
3912 int n = 0;
3914 if (*first_displayed_entry)
3915 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3916 else
3917 next = SIMPLEQ_FIRST(&entries->head);
3918 last = last_displayed_entry;
3919 while (next && last && n++ < maxscroll) {
3920 last = SIMPLEQ_NEXT(last, entry);
3921 if (last) {
3922 *first_displayed_entry = next;
3923 next = SIMPLEQ_NEXT(next, entry);
3926 return n;
3929 static const struct got_error *
3930 tree_entry_path(char **path, struct tog_parent_trees *parents,
3931 struct got_tree_entry *te)
3933 const struct got_error *err = NULL;
3934 struct tog_parent_tree *pt;
3935 size_t len = 2; /* for leading slash and NUL */
3937 TAILQ_FOREACH(pt, parents, entry)
3938 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3939 if (te)
3940 len += strlen(te->name);
3942 *path = calloc(1, len);
3943 if (path == NULL)
3944 return got_error_from_errno("calloc");
3946 (*path)[0] = '/';
3947 pt = TAILQ_LAST(parents, tog_parent_trees);
3948 while (pt) {
3949 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3950 err = got_error(GOT_ERR_NO_SPACE);
3951 goto done;
3953 if (strlcat(*path, "/", len) >= len) {
3954 err = got_error(GOT_ERR_NO_SPACE);
3955 goto done;
3957 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3959 if (te) {
3960 if (strlcat(*path, te->name, len) >= len) {
3961 err = got_error(GOT_ERR_NO_SPACE);
3962 goto done;
3965 done:
3966 if (err) {
3967 free(*path);
3968 *path = NULL;
3970 return err;
3973 static const struct got_error *
3974 blame_tree_entry(struct tog_view **new_view, int begin_x,
3975 struct got_tree_entry *te, struct tog_parent_trees *parents,
3976 struct got_object_id *commit_id, struct got_reflist_head *refs,
3977 struct got_repository *repo)
3979 const struct got_error *err = NULL;
3980 char *path;
3981 struct tog_view *blame_view;
3983 err = tree_entry_path(&path, parents, te);
3984 if (err)
3985 return err;
3987 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3988 if (blame_view == NULL)
3989 return got_error_from_errno("view_open");
3991 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3992 if (err) {
3993 view_close(blame_view);
3994 free(path);
3995 } else
3996 *new_view = blame_view;
3997 return err;
4000 static const struct got_error *
4001 log_tree_entry(struct tog_view **new_view, int begin_x,
4002 struct got_tree_entry *te, struct tog_parent_trees *parents,
4003 struct got_object_id *commit_id, struct got_reflist_head *refs,
4004 struct got_repository *repo)
4006 struct tog_view *log_view;
4007 const struct got_error *err = NULL;
4008 char *path;
4010 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4011 if (log_view == NULL)
4012 return got_error_from_errno("view_open");
4014 err = tree_entry_path(&path, parents, te);
4015 if (err)
4016 return err;
4018 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4019 if (err)
4020 view_close(log_view);
4021 else
4022 *new_view = log_view;
4023 free(path);
4024 return err;
4027 static const struct got_error *
4028 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4029 struct got_object_id *commit_id, struct got_reflist_head *refs,
4030 struct got_repository *repo)
4032 const struct got_error *err = NULL;
4033 char *commit_id_str = NULL;
4034 struct tog_tree_view_state *s = &view->state.tree;
4036 TAILQ_INIT(&s->parents);
4038 err = got_object_id_str(&commit_id_str, commit_id);
4039 if (err != NULL)
4040 goto done;
4042 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4043 err = got_error_from_errno("asprintf");
4044 goto done;
4047 s->root = s->tree = root;
4048 s->entries = got_object_tree_get_entries(root);
4049 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4050 s->commit_id = got_object_id_dup(commit_id);
4051 if (s->commit_id == NULL) {
4052 err = got_error_from_errno("got_object_id_dup");
4053 goto done;
4055 s->refs = refs;
4056 s->repo = repo;
4058 view->show = show_tree_view;
4059 view->input = input_tree_view;
4060 view->close = close_tree_view;
4061 view->search_start = search_start_tree_view;
4062 view->search_next = search_next_tree_view;
4063 done:
4064 free(commit_id_str);
4065 if (err) {
4066 free(s->tree_label);
4067 s->tree_label = NULL;
4069 return err;
4072 static const struct got_error *
4073 close_tree_view(struct tog_view *view)
4075 struct tog_tree_view_state *s = &view->state.tree;
4077 free(s->tree_label);
4078 s->tree_label = NULL;
4079 free(s->commit_id);
4080 s->commit_id = NULL;
4081 while (!TAILQ_EMPTY(&s->parents)) {
4082 struct tog_parent_tree *parent;
4083 parent = TAILQ_FIRST(&s->parents);
4084 TAILQ_REMOVE(&s->parents, parent, entry);
4085 free(parent);
4088 if (s->tree != s->root)
4089 got_object_tree_close(s->tree);
4090 got_object_tree_close(s->root);
4092 return NULL;
4095 static const struct got_error *
4096 search_start_tree_view(struct tog_view *view)
4098 struct tog_tree_view_state *s = &view->state.tree;
4100 s->matched_entry = NULL;
4101 return NULL;
4104 static int
4105 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4107 regmatch_t regmatch;
4109 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4112 static const struct got_error *
4113 search_next_tree_view(struct tog_view *view)
4115 struct tog_tree_view_state *s = &view->state.tree;
4116 struct got_tree_entry *entry, *te;
4118 if (!view->searching) {
4119 view->search_next_done = 1;
4120 return NULL;
4123 if (s->matched_entry) {
4124 if (view->searching == TOG_SEARCH_FORWARD) {
4125 if (s->selected_entry)
4126 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4127 else
4128 entry = SIMPLEQ_FIRST(&s->entries->head);
4130 else {
4131 if (s->selected_entry == NULL) {
4132 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4133 entry = te;
4134 } else {
4135 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4136 entry = te;
4137 if (SIMPLEQ_NEXT(te, entry) ==
4138 s->selected_entry)
4139 break;
4143 } else {
4144 if (view->searching == TOG_SEARCH_FORWARD)
4145 entry = SIMPLEQ_FIRST(&s->entries->head);
4146 else {
4147 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4148 entry = te;
4152 while (1) {
4153 if (entry == NULL) {
4154 if (s->matched_entry == NULL) {
4155 view->search_next_done = 1;
4156 return NULL;
4158 if (view->searching == TOG_SEARCH_FORWARD)
4159 entry = SIMPLEQ_FIRST(&s->entries->head);
4160 else {
4161 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4162 entry = te;
4166 if (match_tree_entry(entry, &view->regex)) {
4167 view->search_next_done = 1;
4168 s->matched_entry = entry;
4169 break;
4172 if (view->searching == TOG_SEARCH_FORWARD)
4173 entry = SIMPLEQ_NEXT(entry, entry);
4174 else {
4175 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4176 entry = NULL;
4177 else {
4178 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4179 if (SIMPLEQ_NEXT(te, entry) == entry) {
4180 entry = te;
4181 break;
4188 if (s->matched_entry) {
4189 s->first_displayed_entry = s->matched_entry;
4190 s->selected = 0;
4193 return NULL;
4196 static const struct got_error *
4197 show_tree_view(struct tog_view *view)
4199 const struct got_error *err = NULL;
4200 struct tog_tree_view_state *s = &view->state.tree;
4201 char *parent_path;
4203 err = tree_entry_path(&parent_path, &s->parents, NULL);
4204 if (err)
4205 return err;
4207 err = draw_tree_entries(view, &s->first_displayed_entry,
4208 &s->last_displayed_entry, &s->selected_entry,
4209 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4210 s->entries, s->selected, view->nlines, s->tree == s->root);
4211 free(parent_path);
4213 view_vborder(view);
4214 return err;
4217 static const struct got_error *
4218 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4219 struct tog_view **focus_view, struct tog_view *view, int ch)
4221 const struct got_error *err = NULL;
4222 struct tog_tree_view_state *s = &view->state.tree;
4223 struct tog_view *log_view;
4224 int begin_x = 0, nscrolled;
4226 switch (ch) {
4227 case 'i':
4228 s->show_ids = !s->show_ids;
4229 break;
4230 case 'l':
4231 if (!s->selected_entry)
4232 break;
4233 if (view_is_parent_view(view))
4234 begin_x = view_split_begin_x(view->begin_x);
4235 err = log_tree_entry(&log_view, begin_x,
4236 s->selected_entry, &s->parents,
4237 s->commit_id, s->refs, s->repo);
4238 if (view_is_parent_view(view)) {
4239 err = view_close_child(view);
4240 if (err)
4241 return err;
4242 err = view_set_child(view, log_view);
4243 if (err) {
4244 view_close(log_view);
4245 break;
4247 *focus_view = log_view;
4248 view->child_focussed = 1;
4249 } else
4250 *new_view = log_view;
4251 break;
4252 case 'k':
4253 case KEY_UP:
4254 if (s->selected > 0) {
4255 s->selected--;
4256 if (s->selected == 0)
4257 break;
4259 if (s->selected > 0)
4260 break;
4261 tree_scroll_up(view, &s->first_displayed_entry, 1,
4262 s->entries, s->tree == s->root);
4263 break;
4264 case KEY_PPAGE:
4265 tree_scroll_up(view, &s->first_displayed_entry,
4266 MAX(0, view->nlines - 4 - s->selected), s->entries,
4267 s->tree == s->root);
4268 s->selected = 0;
4269 if (SIMPLEQ_FIRST(&s->entries->head) ==
4270 s->first_displayed_entry && s->tree != s->root)
4271 s->first_displayed_entry = NULL;
4272 break;
4273 case 'j':
4274 case KEY_DOWN:
4275 if (s->selected < s->ndisplayed - 1) {
4276 s->selected++;
4277 break;
4279 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4280 /* can't scroll any further */
4281 break;
4282 tree_scroll_down(&s->first_displayed_entry, 1,
4283 s->last_displayed_entry, s->entries);
4284 break;
4285 case KEY_NPAGE:
4286 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4287 == NULL) {
4288 /* can't scroll any further; move cursor down */
4289 if (s->selected < s->ndisplayed - 1)
4290 s->selected = s->ndisplayed - 1;
4291 break;
4293 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4294 view->nlines, s->last_displayed_entry, s->entries);
4295 if (nscrolled < view->nlines) {
4296 int ndisplayed = 0;
4297 struct got_tree_entry *te;
4298 te = s->first_displayed_entry;
4299 do {
4300 ndisplayed++;
4301 te = SIMPLEQ_NEXT(te, entry);
4302 } while (te);
4303 s->selected = ndisplayed - 1;
4305 break;
4306 case KEY_ENTER:
4307 case '\r':
4308 case KEY_BACKSPACE:
4309 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4310 struct tog_parent_tree *parent;
4311 /* user selected '..' */
4312 if (s->tree == s->root)
4313 break;
4314 parent = TAILQ_FIRST(&s->parents);
4315 TAILQ_REMOVE(&s->parents, parent,
4316 entry);
4317 got_object_tree_close(s->tree);
4318 s->tree = parent->tree;
4319 s->entries =
4320 got_object_tree_get_entries(s->tree);
4321 s->first_displayed_entry =
4322 parent->first_displayed_entry;
4323 s->selected_entry =
4324 parent->selected_entry;
4325 s->selected = parent->selected;
4326 free(parent);
4327 } else if (S_ISDIR(s->selected_entry->mode)) {
4328 struct got_tree_object *subtree;
4329 err = got_object_open_as_tree(&subtree,
4330 s->repo, s->selected_entry->id);
4331 if (err)
4332 break;
4333 err = tree_view_visit_subtree(subtree, s);
4334 if (err) {
4335 got_object_tree_close(subtree);
4336 break;
4338 } else if (S_ISREG(s->selected_entry->mode)) {
4339 struct tog_view *blame_view;
4340 int begin_x = view_is_parent_view(view) ?
4341 view_split_begin_x(view->begin_x) : 0;
4343 err = blame_tree_entry(&blame_view, begin_x,
4344 s->selected_entry, &s->parents,
4345 s->commit_id, s->refs, s->repo);
4346 if (err)
4347 break;
4348 if (view_is_parent_view(view)) {
4349 err = view_close_child(view);
4350 if (err)
4351 return err;
4352 err = view_set_child(view, blame_view);
4353 if (err) {
4354 view_close(blame_view);
4355 break;
4357 *focus_view = blame_view;
4358 view->child_focussed = 1;
4359 } else
4360 *new_view = blame_view;
4362 break;
4363 case KEY_RESIZE:
4364 if (s->selected > view->nlines)
4365 s->selected = s->ndisplayed - 1;
4366 break;
4367 default:
4368 break;
4371 return err;
4374 __dead static void
4375 usage_tree(void)
4377 endwin();
4378 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4379 getprogname());
4380 exit(1);
4383 static const struct got_error *
4384 cmd_tree(int argc, char *argv[])
4386 const struct got_error *error;
4387 struct got_repository *repo = NULL;
4388 struct got_reflist_head refs;
4389 char *repo_path = NULL;
4390 struct got_object_id *commit_id = NULL;
4391 char *commit_id_arg = NULL;
4392 struct got_commit_object *commit = NULL;
4393 struct got_tree_object *tree = NULL;
4394 int ch;
4395 struct tog_view *view;
4397 SIMPLEQ_INIT(&refs);
4399 #ifndef PROFILE
4400 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4401 NULL) == -1)
4402 err(1, "pledge");
4403 #endif
4405 while ((ch = getopt(argc, argv, "c:")) != -1) {
4406 switch (ch) {
4407 case 'c':
4408 commit_id_arg = optarg;
4409 break;
4410 default:
4411 usage_tree();
4412 /* NOTREACHED */
4416 argc -= optind;
4417 argv += optind;
4419 if (argc == 0) {
4420 struct got_worktree *worktree;
4421 char *cwd = getcwd(NULL, 0);
4422 if (cwd == NULL)
4423 return got_error_from_errno("getcwd");
4424 error = got_worktree_open(&worktree, cwd);
4425 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4426 goto done;
4427 if (worktree) {
4428 free(cwd);
4429 repo_path =
4430 strdup(got_worktree_get_repo_path(worktree));
4431 got_worktree_close(worktree);
4432 } else
4433 repo_path = cwd;
4434 if (repo_path == NULL) {
4435 error = got_error_from_errno("strdup");
4436 goto done;
4438 } else if (argc == 1) {
4439 repo_path = realpath(argv[0], NULL);
4440 if (repo_path == NULL)
4441 return got_error_from_errno2("realpath", argv[0]);
4442 } else
4443 usage_log();
4445 init_curses();
4447 error = got_repo_open(&repo, repo_path);
4448 if (error != NULL)
4449 goto done;
4451 error = apply_unveil(got_repo_get_path(repo), NULL);
4452 if (error)
4453 goto done;
4455 if (commit_id_arg == NULL)
4456 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4457 else {
4458 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4459 if (error) {
4460 if (error->code != GOT_ERR_NOT_REF)
4461 goto done;
4462 error = got_repo_match_object_id_prefix(&commit_id,
4463 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4466 if (error != NULL)
4467 goto done;
4469 error = got_object_open_as_commit(&commit, repo, commit_id);
4470 if (error != NULL)
4471 goto done;
4473 error = got_object_open_as_tree(&tree, repo,
4474 got_object_commit_get_tree_id(commit));
4475 if (error != NULL)
4476 goto done;
4478 error = got_ref_list(&refs, repo);
4479 if (error)
4480 goto done;
4482 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4483 if (view == NULL) {
4484 error = got_error_from_errno("view_open");
4485 goto done;
4487 error = open_tree_view(view, tree, commit_id, &refs, repo);
4488 if (error)
4489 goto done;
4490 error = view_loop(view);
4491 done:
4492 free(repo_path);
4493 free(commit_id);
4494 if (commit)
4495 got_object_commit_close(commit);
4496 if (tree)
4497 got_object_tree_close(tree);
4498 if (repo)
4499 got_repo_close(repo);
4500 got_ref_list_free(&refs);
4501 return error;
4504 static void
4505 list_commands(void)
4507 int i;
4509 fprintf(stderr, "commands:");
4510 for (i = 0; i < nitems(tog_commands); i++) {
4511 struct tog_cmd *cmd = &tog_commands[i];
4512 fprintf(stderr, " %s", cmd->name);
4514 fputc('\n', stderr);
4517 __dead static void
4518 usage(int hflag)
4520 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4521 getprogname());
4522 if (hflag)
4523 list_commands();
4524 exit(1);
4527 static char **
4528 make_argv(const char *arg0, const char *arg1)
4530 char **argv;
4531 int argc = (arg1 == NULL ? 1 : 2);
4533 argv = calloc(argc, sizeof(char *));
4534 if (argv == NULL)
4535 err(1, "calloc");
4536 argv[0] = strdup(arg0);
4537 if (argv[0] == NULL)
4538 err(1, "calloc");
4539 if (arg1) {
4540 argv[1] = strdup(arg1);
4541 if (argv[1] == NULL)
4542 err(1, "calloc");
4545 return argv;
4548 int
4549 main(int argc, char *argv[])
4551 const struct got_error *error = NULL;
4552 struct tog_cmd *cmd = NULL;
4553 int ch, hflag = 0, Vflag = 0;
4554 char **cmd_argv = NULL;
4556 setlocale(LC_CTYPE, "");
4558 while ((ch = getopt(argc, argv, "hV")) != -1) {
4559 switch (ch) {
4560 case 'h':
4561 hflag = 1;
4562 break;
4563 case 'V':
4564 Vflag = 1;
4565 break;
4566 default:
4567 usage(hflag);
4568 /* NOTREACHED */
4572 argc -= optind;
4573 argv += optind;
4574 optind = 0;
4575 optreset = 1;
4577 if (Vflag) {
4578 got_version_print_str();
4579 return 1;
4582 if (argc == 0) {
4583 if (hflag)
4584 usage(hflag);
4585 /* Build an argument vector which runs a default command. */
4586 cmd = &tog_commands[0];
4587 cmd_argv = make_argv(cmd->name, NULL);
4588 argc = 1;
4589 } else {
4590 int i;
4592 /* Did the user specific a command? */
4593 for (i = 0; i < nitems(tog_commands); i++) {
4594 if (strncmp(tog_commands[i].name, argv[0],
4595 strlen(argv[0])) == 0) {
4596 cmd = &tog_commands[i];
4597 break;
4601 if (cmd == NULL) {
4602 fprintf(stderr, "%s: unknown command '%s'\n",
4603 getprogname(), argv[0]);
4604 list_commands();
4605 return 1;
4609 if (hflag)
4610 cmd->cmd_usage();
4611 else
4612 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4614 endwin();
4615 free(cmd_argv);
4616 if (error)
4617 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4618 return 0;