Blob


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