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 return err;
2093 in_repo_path = strdup(s->in_repo_path);
2094 if (in_repo_path == NULL) {
2095 free(start_id);
2096 return got_error_from_errno("strdup");
2098 err = open_log_view(lv, start_id, s->refs, s->repo,
2099 s->head_ref_name, in_repo_path, 0);
2100 if (err)
2101 return err;;
2102 *dead_view = view;
2103 *new_view = lv;
2104 break;
2105 default:
2106 break;
2109 return err;
2112 static const struct got_error *
2113 apply_unveil(const char *repo_path, const char *worktree_path)
2115 const struct got_error *error;
2117 #ifdef PROFILE
2118 if (unveil("gmon.out", "rwc") != 0)
2119 return got_error_from_errno2("unveil", "gmon.out");
2120 #endif
2121 if (repo_path && unveil(repo_path, "r") != 0)
2122 return got_error_from_errno2("unveil", repo_path);
2124 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2125 return got_error_from_errno2("unveil", worktree_path);
2127 if (unveil("/tmp", "rwc") != 0)
2128 return got_error_from_errno2("unveil", "/tmp");
2130 error = got_privsep_unveil_exec_helpers();
2131 if (error != NULL)
2132 return error;
2134 if (unveil(NULL, NULL) != 0)
2135 return got_error_from_errno("unveil");
2137 return NULL;
2140 static void
2141 init_curses(void)
2143 initscr();
2144 cbreak();
2145 halfdelay(1); /* Do fast refresh while initial view is loading. */
2146 noecho();
2147 nonl();
2148 intrflush(stdscr, FALSE);
2149 keypad(stdscr, TRUE);
2150 curs_set(0);
2151 signal(SIGWINCH, tog_sigwinch);
2154 static const struct got_error *
2155 cmd_log(int argc, char *argv[])
2157 const struct got_error *error;
2158 struct got_repository *repo = NULL;
2159 struct got_worktree *worktree = NULL;
2160 struct got_reflist_head refs;
2161 struct got_object_id *start_id = NULL;
2162 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2163 char *start_commit = NULL;
2164 int ch;
2165 struct tog_view *view;
2167 SIMPLEQ_INIT(&refs);
2169 #ifndef PROFILE
2170 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2171 NULL) == -1)
2172 err(1, "pledge");
2173 #endif
2175 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2176 switch (ch) {
2177 case 'c':
2178 start_commit = optarg;
2179 break;
2180 case 'r':
2181 repo_path = realpath(optarg, NULL);
2182 if (repo_path == NULL)
2183 err(1, "-r option");
2184 break;
2185 default:
2186 usage_log();
2187 /* NOTREACHED */
2191 argc -= optind;
2192 argv += optind;
2194 cwd = getcwd(NULL, 0);
2195 if (cwd == NULL) {
2196 error = got_error_from_errno("getcwd");
2197 goto done;
2199 error = got_worktree_open(&worktree, cwd);
2200 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2201 goto done;
2202 error = NULL;
2204 if (argc == 0) {
2205 path = strdup("");
2206 if (path == NULL) {
2207 error = got_error_from_errno("strdup");
2208 goto done;
2210 } else if (argc == 1) {
2211 if (worktree) {
2212 error = got_worktree_resolve_path(&path, worktree,
2213 argv[0]);
2214 if (error)
2215 goto done;
2216 } else {
2217 path = strdup(argv[0]);
2218 if (path == NULL) {
2219 error = got_error_from_errno("strdup");
2220 goto done;
2223 } else
2224 usage_log();
2226 repo_path = worktree ?
2227 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2228 if (repo_path == NULL) {
2229 error = got_error_from_errno("strdup");
2230 goto done;
2233 init_curses();
2235 error = got_repo_open(&repo, repo_path);
2236 if (error != NULL)
2237 goto done;
2239 error = apply_unveil(got_repo_get_path(repo),
2240 worktree ? got_worktree_get_root_path(worktree) : NULL);
2241 if (error)
2242 goto done;
2244 if (start_commit == NULL)
2245 error = get_head_commit_id(&start_id, worktree ?
2246 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2247 repo);
2248 else {
2249 error = get_head_commit_id(&start_id, start_commit, repo);
2250 if (error) {
2251 if (error->code != GOT_ERR_NOT_REF)
2252 goto done;
2253 error = got_repo_match_object_id_prefix(&start_id,
2254 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2257 if (error != NULL)
2258 goto done;
2260 error = got_ref_list(&refs, repo);
2261 if (error)
2262 goto done;
2264 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2265 if (view == NULL) {
2266 error = got_error_from_errno("view_open");
2267 goto done;
2269 error = open_log_view(view, start_id, &refs, repo, worktree ?
2270 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2271 if (error)
2272 goto done;
2273 error = view_loop(view);
2274 done:
2275 free(repo_path);
2276 free(cwd);
2277 free(path);
2278 free(start_id);
2279 if (repo)
2280 got_repo_close(repo);
2281 if (worktree)
2282 got_worktree_close(worktree);
2283 got_ref_list_free(&refs);
2284 return error;
2287 __dead static void
2288 usage_diff(void)
2290 endwin();
2291 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2292 getprogname());
2293 exit(1);
2296 static char *
2297 parse_next_line(FILE *f, size_t *len)
2299 char *line;
2300 size_t linelen;
2301 size_t lineno;
2302 const char delim[3] = { '\0', '\0', '\0'};
2304 line = fparseln(f, &linelen, &lineno, delim, 0);
2305 if (len)
2306 *len = linelen;
2307 return line;
2310 static const struct got_error *
2311 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2312 int *last_displayed_line, int *eof, int max_lines,
2313 char *header)
2315 const struct got_error *err;
2316 int nlines = 0, nprinted = 0;
2317 char *line;
2318 size_t len;
2319 wchar_t *wline;
2320 int width;
2322 rewind(f);
2323 werase(view->window);
2325 if (header) {
2326 err = format_line(&wline, &width, header, view->ncols);
2327 if (err) {
2328 return err;
2331 if (view_needs_focus_indication(view))
2332 wstandout(view->window);
2333 waddwstr(view->window, wline);
2334 if (view_needs_focus_indication(view))
2335 wstandend(view->window);
2336 if (width < view->ncols - 1)
2337 waddch(view->window, '\n');
2339 if (max_lines <= 1)
2340 return NULL;
2341 max_lines--;
2344 *eof = 0;
2345 while (nprinted < max_lines) {
2346 line = parse_next_line(f, &len);
2347 if (line == NULL) {
2348 *eof = 1;
2349 break;
2351 if (++nlines < *first_displayed_line) {
2352 free(line);
2353 continue;
2356 err = format_line(&wline, &width, line, view->ncols);
2357 if (err) {
2358 free(line);
2359 return err;
2361 waddwstr(view->window, wline);
2362 if (width < view->ncols - 1)
2363 waddch(view->window, '\n');
2364 if (++nprinted == 1)
2365 *first_displayed_line = nlines;
2366 free(line);
2367 free(wline);
2368 wline = NULL;
2370 *last_displayed_line = nlines;
2372 view_vborder(view);
2374 if (*eof) {
2375 while (nprinted < view->nlines) {
2376 waddch(view->window, '\n');
2377 nprinted++;
2380 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2381 if (err) {
2382 return err;
2385 wstandout(view->window);
2386 waddwstr(view->window, wline);
2387 wstandend(view->window);
2390 return NULL;
2393 static char *
2394 get_datestr(time_t *time, char *datebuf)
2396 char *p, *s = ctime_r(time, datebuf);
2397 p = strchr(s, '\n');
2398 if (p)
2399 *p = '\0';
2400 return s;
2403 static const struct got_error *
2404 write_commit_info(struct got_object_id *commit_id,
2405 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2407 const struct got_error *err = NULL;
2408 char datebuf[26];
2409 struct got_commit_object *commit;
2410 char *id_str = NULL;
2411 time_t committer_time;
2412 const char *author, *committer;
2413 char *refs_str = NULL;
2415 if (refs) {
2416 err = build_refs_str(&refs_str, refs, commit_id);
2417 if (err)
2418 return err;
2421 err = got_object_open_as_commit(&commit, repo, commit_id);
2422 if (err)
2423 return err;
2425 err = got_object_id_str(&id_str, commit_id);
2426 if (err) {
2427 err = got_error_from_errno("got_object_id_str");
2428 goto done;
2431 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2432 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2433 err = got_error_from_errno("fprintf");
2434 goto done;
2436 if (fprintf(outfile, "from: %s\n",
2437 got_object_commit_get_author(commit)) < 0) {
2438 err = got_error_from_errno("fprintf");
2439 goto done;
2441 committer_time = got_object_commit_get_committer_time(commit);
2442 if (fprintf(outfile, "date: %s UTC\n",
2443 get_datestr(&committer_time, datebuf)) < 0) {
2444 err = got_error_from_errno("fprintf");
2445 goto done;
2447 author = got_object_commit_get_author(commit);
2448 committer = got_object_commit_get_committer(commit);
2449 if (strcmp(author, committer) != 0 &&
2450 fprintf(outfile, "via: %s\n", committer) < 0) {
2451 err = got_error_from_errno("fprintf");
2452 goto done;
2454 if (fprintf(outfile, "%s\n",
2455 got_object_commit_get_logmsg(commit)) < 0) {
2456 err = got_error_from_errno("fprintf");
2457 goto done;
2459 done:
2460 free(id_str);
2461 free(refs_str);
2462 got_object_commit_close(commit);
2463 return err;
2466 static const struct got_error *
2467 create_diff(struct tog_diff_view_state *s)
2469 const struct got_error *err = NULL;
2470 FILE *f = NULL;
2471 int obj_type;
2473 f = got_opentemp();
2474 if (f == NULL) {
2475 err = got_error_from_errno("got_opentemp");
2476 goto done;
2478 if (s->f && fclose(s->f) != 0) {
2479 err = got_error_from_errno("fclose");
2480 goto done;
2482 s->f = f;
2484 if (s->id1)
2485 err = got_object_get_type(&obj_type, s->repo, s->id1);
2486 else
2487 err = got_object_get_type(&obj_type, s->repo, s->id2);
2488 if (err)
2489 goto done;
2491 switch (obj_type) {
2492 case GOT_OBJ_TYPE_BLOB:
2493 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2494 s->diff_context, s->repo, f);
2495 break;
2496 case GOT_OBJ_TYPE_TREE:
2497 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2498 s->diff_context, s->repo, f);
2499 break;
2500 case GOT_OBJ_TYPE_COMMIT: {
2501 const struct got_object_id_queue *parent_ids;
2502 struct got_object_qid *pid;
2503 struct got_commit_object *commit2;
2505 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2506 if (err)
2507 break;
2508 /* Show commit info if we're diffing to a parent/root commit. */
2509 if (s->id1 == NULL)
2510 write_commit_info(s->id2, s->refs, s->repo, f);
2511 else {
2512 parent_ids = got_object_commit_get_parent_ids(commit2);
2513 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2514 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2515 write_commit_info(s->id2, s->refs,
2516 s->repo, f);
2517 break;
2521 got_object_commit_close(commit2);
2523 err = got_diff_objects_as_commits(s->id1, s->id2,
2524 s->diff_context, s->repo, f);
2525 break;
2527 default:
2528 err = got_error(GOT_ERR_OBJ_TYPE);
2529 break;
2531 done:
2532 if (f && fflush(f) != 0 && err == NULL)
2533 err = got_error_from_errno("fflush");
2534 return err;
2537 static void
2538 diff_view_indicate_progress(struct tog_view *view)
2540 mvwaddstr(view->window, 0, 0, "diffing...");
2541 update_panels();
2542 doupdate();
2545 static const struct got_error *
2546 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2547 struct got_object_id *id2, struct tog_view *log_view,
2548 struct got_reflist_head *refs, struct got_repository *repo)
2550 const struct got_error *err;
2552 if (id1 != NULL && id2 != NULL) {
2553 int type1, type2;
2554 err = got_object_get_type(&type1, repo, id1);
2555 if (err)
2556 return err;
2557 err = got_object_get_type(&type2, repo, id2);
2558 if (err)
2559 return err;
2561 if (type1 != type2)
2562 return got_error(GOT_ERR_OBJ_TYPE);
2565 if (id1) {
2566 view->state.diff.id1 = got_object_id_dup(id1);
2567 if (view->state.diff.id1 == NULL)
2568 return got_error_from_errno("got_object_id_dup");
2569 } else
2570 view->state.diff.id1 = NULL;
2572 view->state.diff.id2 = got_object_id_dup(id2);
2573 if (view->state.diff.id2 == NULL) {
2574 free(view->state.diff.id1);
2575 view->state.diff.id1 = NULL;
2576 return got_error_from_errno("got_object_id_dup");
2578 view->state.diff.f = NULL;
2579 view->state.diff.first_displayed_line = 1;
2580 view->state.diff.last_displayed_line = view->nlines;
2581 view->state.diff.diff_context = 3;
2582 view->state.diff.log_view = log_view;
2583 view->state.diff.repo = repo;
2584 view->state.diff.refs = refs;
2586 if (log_view && view_is_splitscreen(view))
2587 show_log_view(log_view); /* draw vborder */
2588 diff_view_indicate_progress(view);
2590 err = create_diff(&view->state.diff);
2591 if (err) {
2592 free(view->state.diff.id1);
2593 view->state.diff.id1 = NULL;
2594 free(view->state.diff.id2);
2595 view->state.diff.id2 = NULL;
2596 return err;
2599 view->show = show_diff_view;
2600 view->input = input_diff_view;
2601 view->close = close_diff_view;
2603 return NULL;
2606 static const struct got_error *
2607 close_diff_view(struct tog_view *view)
2609 const struct got_error *err = NULL;
2611 free(view->state.diff.id1);
2612 view->state.diff.id1 = NULL;
2613 free(view->state.diff.id2);
2614 view->state.diff.id2 = NULL;
2615 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2616 err = got_error_from_errno("fclose");
2617 return err;
2620 static const struct got_error *
2621 show_diff_view(struct tog_view *view)
2623 const struct got_error *err;
2624 struct tog_diff_view_state *s = &view->state.diff;
2625 char *id_str1 = NULL, *id_str2, *header;
2627 if (s->id1) {
2628 err = got_object_id_str(&id_str1, s->id1);
2629 if (err)
2630 return err;
2632 err = got_object_id_str(&id_str2, s->id2);
2633 if (err)
2634 return err;
2636 if (asprintf(&header, "diff %s %s",
2637 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2638 err = got_error_from_errno("asprintf");
2639 free(id_str1);
2640 free(id_str2);
2641 return err;
2643 free(id_str1);
2644 free(id_str2);
2646 return draw_file(view, s->f, &s->first_displayed_line,
2647 &s->last_displayed_line, &s->eof, view->nlines,
2648 header);
2651 static const struct got_error *
2652 set_selected_commit(struct tog_diff_view_state *s,
2653 struct commit_queue_entry *entry)
2655 const struct got_error *err;
2656 const struct got_object_id_queue *parent_ids;
2657 struct got_commit_object *selected_commit;
2658 struct got_object_qid *pid;
2660 free(s->id2);
2661 s->id2 = got_object_id_dup(entry->id);
2662 if (s->id2 == NULL)
2663 return got_error_from_errno("got_object_id_dup");
2665 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2666 if (err)
2667 return err;
2668 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2669 free(s->id1);
2670 pid = SIMPLEQ_FIRST(parent_ids);
2671 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2672 got_object_commit_close(selected_commit);
2673 return NULL;
2676 static const struct got_error *
2677 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2678 struct tog_view **focus_view, struct tog_view *view, int ch)
2680 const struct got_error *err = NULL;
2681 struct tog_diff_view_state *s = &view->state.diff;
2682 struct tog_log_view_state *ls;
2683 struct commit_queue_entry *entry;
2684 int i;
2686 switch (ch) {
2687 case 'k':
2688 case KEY_UP:
2689 if (s->first_displayed_line > 1)
2690 s->first_displayed_line--;
2691 break;
2692 case KEY_PPAGE:
2693 case CTRL('b'):
2694 if (s->first_displayed_line == 1)
2695 break;
2696 i = 0;
2697 while (i++ < view->nlines - 1 &&
2698 s->first_displayed_line > 1)
2699 s->first_displayed_line--;
2700 break;
2701 case 'j':
2702 case KEY_DOWN:
2703 if (!s->eof)
2704 s->first_displayed_line++;
2705 break;
2706 case KEY_NPAGE:
2707 case CTRL('f'):
2708 case ' ':
2709 if (s->eof)
2710 break;
2711 i = 0;
2712 while (!s->eof && i++ < view->nlines - 1) {
2713 char *line;
2714 line = parse_next_line(s->f, NULL);
2715 s->first_displayed_line++;
2716 if (line == NULL)
2717 break;
2719 break;
2720 case '[':
2721 if (s->diff_context > 0) {
2722 s->diff_context--;
2723 diff_view_indicate_progress(view);
2724 err = create_diff(s);
2726 break;
2727 case ']':
2728 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2729 s->diff_context++;
2730 diff_view_indicate_progress(view);
2731 err = create_diff(s);
2733 break;
2734 case '<':
2735 case ',':
2736 if (s->log_view == NULL)
2737 break;
2738 ls = &s->log_view->state.log;
2739 entry = TAILQ_PREV(ls->selected_entry,
2740 commit_queue_head, entry);
2741 if (entry == NULL)
2742 break;
2744 err = input_log_view(NULL, NULL, NULL, s->log_view,
2745 KEY_UP);
2746 if (err)
2747 break;
2749 err = set_selected_commit(s, entry);
2750 if (err)
2751 break;
2753 s->first_displayed_line = 1;
2754 s->last_displayed_line = view->nlines;
2756 diff_view_indicate_progress(view);
2757 err = create_diff(s);
2758 break;
2759 case '>':
2760 case '.':
2761 if (s->log_view == NULL)
2762 break;
2763 ls = &s->log_view->state.log;
2765 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2766 ls->thread_args.commits_needed++;
2768 /* Display "loading..." in log view. */
2769 show_log_view(s->log_view);
2770 update_panels();
2771 doupdate();
2773 err = trigger_log_thread(1 /* load_all */,
2774 &ls->thread_args.commits_needed,
2775 &ls->thread_args.log_complete,
2776 &ls->thread_args.need_commits);
2777 if (err)
2778 break;
2780 err = input_log_view(NULL, NULL, NULL, s->log_view,
2781 KEY_DOWN);
2782 if (err)
2783 break;
2785 entry = TAILQ_NEXT(ls->selected_entry, entry);
2786 if (entry == NULL)
2787 break;
2789 err = set_selected_commit(s, entry);
2790 if (err)
2791 break;
2793 s->first_displayed_line = 1;
2794 s->last_displayed_line = view->nlines;
2796 diff_view_indicate_progress(view);
2797 err = create_diff(s);
2798 break;
2799 default:
2800 break;
2803 return err;
2806 static const struct got_error *
2807 cmd_diff(int argc, char *argv[])
2809 const struct got_error *error = NULL;
2810 struct got_repository *repo = NULL;
2811 struct got_reflist_head refs;
2812 struct got_object_id *id1 = NULL, *id2 = NULL;
2813 char *repo_path = NULL;
2814 char *id_str1 = NULL, *id_str2 = NULL;
2815 int ch;
2816 struct tog_view *view;
2818 SIMPLEQ_INIT(&refs);
2820 #ifndef PROFILE
2821 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2822 NULL) == -1)
2823 err(1, "pledge");
2824 #endif
2826 while ((ch = getopt(argc, argv, "")) != -1) {
2827 switch (ch) {
2828 default:
2829 usage_diff();
2830 /* NOTREACHED */
2834 argc -= optind;
2835 argv += optind;
2837 if (argc == 0) {
2838 usage_diff(); /* TODO show local worktree changes */
2839 } else if (argc == 2) {
2840 repo_path = getcwd(NULL, 0);
2841 if (repo_path == NULL)
2842 return got_error_from_errno("getcwd");
2843 id_str1 = argv[0];
2844 id_str2 = argv[1];
2845 } else if (argc == 3) {
2846 repo_path = realpath(argv[0], NULL);
2847 if (repo_path == NULL)
2848 return got_error_from_errno2("realpath", argv[0]);
2849 id_str1 = argv[1];
2850 id_str2 = argv[2];
2851 } else
2852 usage_diff();
2854 init_curses();
2856 error = got_repo_open(&repo, repo_path);
2857 if (error)
2858 goto done;
2860 error = apply_unveil(got_repo_get_path(repo), NULL);
2861 if (error)
2862 goto done;
2864 error = got_repo_match_object_id_prefix(&id1, id_str1,
2865 GOT_OBJ_TYPE_ANY, repo);
2866 if (error)
2867 goto done;
2869 error = got_repo_match_object_id_prefix(&id2, id_str2,
2870 GOT_OBJ_TYPE_ANY, repo);
2871 if (error)
2872 goto done;
2874 error = got_ref_list(&refs, repo);
2875 if (error)
2876 goto done;
2878 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2879 if (view == NULL) {
2880 error = got_error_from_errno("view_open");
2881 goto done;
2883 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2884 if (error)
2885 goto done;
2886 error = view_loop(view);
2887 done:
2888 free(repo_path);
2889 if (repo)
2890 got_repo_close(repo);
2891 got_ref_list_free(&refs);
2892 return error;
2895 __dead static void
2896 usage_blame(void)
2898 endwin();
2899 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2900 getprogname());
2901 exit(1);
2904 struct tog_blame_line {
2905 int annotated;
2906 struct got_object_id *id;
2909 static const struct got_error *
2910 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2911 const char *path, struct tog_blame_line *lines, int nlines,
2912 int blame_complete, int selected_line, int *first_displayed_line,
2913 int *last_displayed_line, int *eof, int max_lines)
2915 const struct got_error *err;
2916 int lineno = 0, nprinted = 0;
2917 char *line;
2918 size_t len;
2919 wchar_t *wline;
2920 int width, wlimit;
2921 struct tog_blame_line *blame_line;
2922 struct got_object_id *prev_id = NULL;
2923 char *id_str;
2925 err = got_object_id_str(&id_str, id);
2926 if (err)
2927 return err;
2929 rewind(f);
2930 werase(view->window);
2932 if (asprintf(&line, "commit %s", id_str) == -1) {
2933 err = got_error_from_errno("asprintf");
2934 free(id_str);
2935 return err;
2938 err = format_line(&wline, &width, line, view->ncols);
2939 free(line);
2940 line = NULL;
2941 if (view_needs_focus_indication(view))
2942 wstandout(view->window);
2943 waddwstr(view->window, wline);
2944 if (view_needs_focus_indication(view))
2945 wstandend(view->window);
2946 free(wline);
2947 wline = NULL;
2948 if (width < view->ncols - 1)
2949 waddch(view->window, '\n');
2951 if (asprintf(&line, "[%d/%d] %s%s",
2952 *first_displayed_line - 1 + selected_line, nlines,
2953 blame_complete ? "" : "annotating... ", path) == -1) {
2954 free(id_str);
2955 return got_error_from_errno("asprintf");
2957 free(id_str);
2958 err = format_line(&wline, &width, line, view->ncols);
2959 free(line);
2960 line = NULL;
2961 if (err)
2962 return err;
2963 waddwstr(view->window, wline);
2964 free(wline);
2965 wline = NULL;
2966 if (width < view->ncols - 1)
2967 waddch(view->window, '\n');
2969 *eof = 0;
2970 while (nprinted < max_lines - 2) {
2971 line = parse_next_line(f, &len);
2972 if (line == NULL) {
2973 *eof = 1;
2974 break;
2976 if (++lineno < *first_displayed_line) {
2977 free(line);
2978 continue;
2981 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2982 err = format_line(&wline, &width, line, wlimit);
2983 if (err) {
2984 free(line);
2985 return err;
2988 if (view->focussed && nprinted == selected_line - 1)
2989 wstandout(view->window);
2991 blame_line = &lines[lineno - 1];
2992 if (blame_line->annotated && prev_id &&
2993 got_object_id_cmp(prev_id, blame_line->id) == 0)
2994 waddstr(view->window, " ");
2995 else if (blame_line->annotated) {
2996 char *id_str;
2997 err = got_object_id_str(&id_str, blame_line->id);
2998 if (err) {
2999 free(line);
3000 free(wline);
3001 return err;
3003 wprintw(view->window, "%.8s ", id_str);
3004 free(id_str);
3005 prev_id = blame_line->id;
3006 } else {
3007 waddstr(view->window, "........ ");
3008 prev_id = NULL;
3011 waddwstr(view->window, wline);
3012 while (width < wlimit) {
3013 waddch(view->window, ' ');
3014 width++;
3016 if (view->focussed && nprinted == selected_line - 1)
3017 wstandend(view->window);
3018 if (++nprinted == 1)
3019 *first_displayed_line = lineno;
3020 free(line);
3021 free(wline);
3022 wline = NULL;
3024 *last_displayed_line = lineno;
3026 view_vborder(view);
3028 return NULL;
3031 static const struct got_error *
3032 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3034 const struct got_error *err = NULL;
3035 struct tog_blame_cb_args *a = arg;
3036 struct tog_blame_line *line;
3037 int errcode;
3039 if (nlines != a->nlines ||
3040 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3041 return got_error(GOT_ERR_RANGE);
3043 errcode = pthread_mutex_lock(&tog_mutex);
3044 if (errcode)
3045 return got_error_set_errno(errcode, "pthread_mutex_lock");
3047 if (*a->quit) { /* user has quit the blame view */
3048 err = got_error(GOT_ERR_ITER_COMPLETED);
3049 goto done;
3052 if (lineno == -1)
3053 goto done; /* no change in this commit */
3055 line = &a->lines[lineno - 1];
3056 if (line->annotated)
3057 goto done;
3059 line->id = got_object_id_dup(id);
3060 if (line->id == NULL) {
3061 err = got_error_from_errno("got_object_id_dup");
3062 goto done;
3064 line->annotated = 1;
3065 done:
3066 errcode = pthread_mutex_unlock(&tog_mutex);
3067 if (errcode)
3068 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3069 return err;
3072 static void *
3073 blame_thread(void *arg)
3075 const struct got_error *err;
3076 struct tog_blame_thread_args *ta = arg;
3077 struct tog_blame_cb_args *a = ta->cb_args;
3078 int errcode;
3080 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3081 blame_cb, ta->cb_args);
3083 errcode = pthread_mutex_lock(&tog_mutex);
3084 if (errcode)
3085 return (void *)got_error_set_errno(errcode,
3086 "pthread_mutex_lock");
3088 got_repo_close(ta->repo);
3089 ta->repo = NULL;
3090 *ta->complete = 1;
3092 errcode = pthread_mutex_unlock(&tog_mutex);
3093 if (errcode && err == NULL)
3094 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3096 return (void *)err;
3099 static struct got_object_id *
3100 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3101 int selected_line)
3103 struct tog_blame_line *line;
3105 line = &lines[first_displayed_line - 1 + selected_line - 1];
3106 if (!line->annotated)
3107 return NULL;
3109 return line->id;
3112 static const struct got_error *
3113 stop_blame(struct tog_blame *blame)
3115 const struct got_error *err = NULL;
3116 int i;
3118 if (blame->thread) {
3119 int errcode;
3120 errcode = pthread_mutex_unlock(&tog_mutex);
3121 if (errcode)
3122 return got_error_set_errno(errcode,
3123 "pthread_mutex_unlock");
3124 errcode = pthread_join(blame->thread, (void **)&err);
3125 if (errcode)
3126 return got_error_set_errno(errcode, "pthread_join");
3127 errcode = pthread_mutex_lock(&tog_mutex);
3128 if (errcode)
3129 return got_error_set_errno(errcode,
3130 "pthread_mutex_lock");
3131 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3132 err = NULL;
3133 blame->thread = NULL;
3135 if (blame->thread_args.repo) {
3136 got_repo_close(blame->thread_args.repo);
3137 blame->thread_args.repo = NULL;
3139 if (blame->f) {
3140 if (fclose(blame->f) != 0 && err == NULL)
3141 err = got_error_from_errno("fclose");
3142 blame->f = NULL;
3144 if (blame->lines) {
3145 for (i = 0; i < blame->nlines; i++)
3146 free(blame->lines[i].id);
3147 free(blame->lines);
3148 blame->lines = NULL;
3150 free(blame->cb_args.commit_id);
3151 blame->cb_args.commit_id = NULL;
3153 return err;
3156 static const struct got_error *
3157 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3158 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3159 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3160 struct got_repository *repo)
3162 const struct got_error *err = NULL;
3163 struct got_blob_object *blob = NULL;
3164 struct got_repository *thread_repo = NULL;
3165 struct got_object_id *obj_id = NULL;
3166 int obj_type;
3168 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3169 if (err)
3170 return err;
3171 if (obj_id == NULL)
3172 return got_error(GOT_ERR_NO_OBJ);
3174 err = got_object_get_type(&obj_type, repo, obj_id);
3175 if (err)
3176 goto done;
3178 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3179 err = got_error(GOT_ERR_OBJ_TYPE);
3180 goto done;
3183 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3184 if (err)
3185 goto done;
3186 blame->f = got_opentemp();
3187 if (blame->f == NULL) {
3188 err = got_error_from_errno("got_opentemp");
3189 goto done;
3191 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3192 &blame->line_offsets, blame->f, blob);
3193 if (err)
3194 goto done;
3196 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3197 if (blame->lines == NULL) {
3198 err = got_error_from_errno("calloc");
3199 goto done;
3202 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3203 if (err)
3204 goto done;
3206 blame->cb_args.view = view;
3207 blame->cb_args.lines = blame->lines;
3208 blame->cb_args.nlines = blame->nlines;
3209 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3210 if (blame->cb_args.commit_id == NULL) {
3211 err = got_error_from_errno("got_object_id_dup");
3212 goto done;
3214 blame->cb_args.quit = done;
3216 blame->thread_args.path = path;
3217 blame->thread_args.repo = thread_repo;
3218 blame->thread_args.cb_args = &blame->cb_args;
3219 blame->thread_args.complete = blame_complete;
3220 *blame_complete = 0;
3222 done:
3223 if (blob)
3224 got_object_blob_close(blob);
3225 free(obj_id);
3226 if (err)
3227 stop_blame(blame);
3228 return err;
3231 static const struct got_error *
3232 open_blame_view(struct tog_view *view, char *path,
3233 struct got_object_id *commit_id, struct got_reflist_head *refs,
3234 struct got_repository *repo)
3236 const struct got_error *err = NULL;
3237 struct tog_blame_view_state *s = &view->state.blame;
3239 SIMPLEQ_INIT(&s->blamed_commits);
3241 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3242 if (err)
3243 return err;
3245 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3246 s->first_displayed_line = 1;
3247 s->last_displayed_line = view->nlines;
3248 s->selected_line = 1;
3249 s->blame_complete = 0;
3250 s->path = path;
3251 if (s->path == NULL)
3252 return got_error_from_errno("open_blame_view");
3253 s->repo = repo;
3254 s->refs = refs;
3255 s->commit_id = commit_id;
3256 memset(&s->blame, 0, sizeof(s->blame));
3258 view->show = show_blame_view;
3259 view->input = input_blame_view;
3260 view->close = close_blame_view;
3261 view->search_start = search_start_blame_view;
3262 view->search_next = search_next_blame_view;
3264 return run_blame(&s->blame, view, &s->blame_complete,
3265 &s->first_displayed_line, &s->last_displayed_line,
3266 &s->selected_line, &s->done, &s->eof, s->path,
3267 s->blamed_commit->id, s->repo);
3270 static const struct got_error *
3271 close_blame_view(struct tog_view *view)
3273 const struct got_error *err = NULL;
3274 struct tog_blame_view_state *s = &view->state.blame;
3276 if (s->blame.thread)
3277 err = stop_blame(&s->blame);
3279 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3280 struct got_object_qid *blamed_commit;
3281 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3282 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3283 got_object_qid_free(blamed_commit);
3286 free(s->path);
3288 return err;
3291 static const struct got_error *
3292 search_start_blame_view(struct tog_view *view)
3294 struct tog_blame_view_state *s = &view->state.blame;
3296 s->matched_line = 0;
3297 return NULL;
3300 static int
3301 match_line(const char *line, regex_t *regex)
3303 regmatch_t regmatch;
3305 return regexec(regex, line, 1, &regmatch, 0) == 0;
3309 static const struct got_error *
3310 search_next_blame_view(struct tog_view *view)
3312 struct tog_blame_view_state *s = &view->state.blame;
3313 int lineno;
3315 if (!view->searching) {
3316 view->search_next_done = 1;
3317 return NULL;
3320 if (s->matched_line) {
3321 if (view->searching == TOG_SEARCH_FORWARD)
3322 lineno = s->matched_line + 1;
3323 else
3324 lineno = s->matched_line - 1;
3325 } else {
3326 if (view->searching == TOG_SEARCH_FORWARD)
3327 lineno = 1;
3328 else
3329 lineno = s->blame.nlines;
3332 while (1) {
3333 char *line = NULL;
3334 off_t offset;
3335 size_t len;
3337 if (lineno <= 0 || lineno > s->blame.nlines) {
3338 if (s->matched_line == 0) {
3339 view->search_next_done = 1;
3340 free(line);
3341 break;
3344 if (view->searching == TOG_SEARCH_FORWARD)
3345 lineno = 1;
3346 else
3347 lineno = s->blame.nlines;
3350 offset = s->blame.line_offsets[lineno - 1];
3351 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3352 free(line);
3353 return got_error_from_errno("fseeko");
3355 free(line);
3356 line = parse_next_line(s->blame.f, &len);
3357 if (line && match_line(line, &view->regex)) {
3358 view->search_next_done = 1;
3359 s->matched_line = lineno;
3360 free(line);
3361 break;
3363 free(line);
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 lineno++;
3366 else
3367 lineno--;
3370 if (s->matched_line) {
3371 s->first_displayed_line = s->matched_line;
3372 s->selected_line = 1;
3375 return NULL;
3378 static const struct got_error *
3379 show_blame_view(struct tog_view *view)
3381 const struct got_error *err = NULL;
3382 struct tog_blame_view_state *s = &view->state.blame;
3383 int errcode;
3385 if (s->blame.thread == NULL) {
3386 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3387 &s->blame.thread_args);
3388 if (errcode)
3389 return got_error_set_errno(errcode, "pthread_create");
3392 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3393 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3394 s->selected_line, &s->first_displayed_line,
3395 &s->last_displayed_line, &s->eof, view->nlines);
3397 view_vborder(view);
3398 return err;
3401 static const struct got_error *
3402 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3403 struct tog_view **focus_view, struct tog_view *view, int ch)
3405 const struct got_error *err = NULL, *thread_err = NULL;
3406 struct tog_view *diff_view;
3407 struct tog_blame_view_state *s = &view->state.blame;
3408 int begin_x = 0;
3410 switch (ch) {
3411 case 'q':
3412 s->done = 1;
3413 break;
3414 case 'k':
3415 case KEY_UP:
3416 if (s->selected_line > 1)
3417 s->selected_line--;
3418 else if (s->selected_line == 1 &&
3419 s->first_displayed_line > 1)
3420 s->first_displayed_line--;
3421 break;
3422 case KEY_PPAGE:
3423 if (s->first_displayed_line == 1) {
3424 s->selected_line = 1;
3425 break;
3427 if (s->first_displayed_line > view->nlines - 2)
3428 s->first_displayed_line -=
3429 (view->nlines - 2);
3430 else
3431 s->first_displayed_line = 1;
3432 break;
3433 case 'j':
3434 case KEY_DOWN:
3435 if (s->selected_line < view->nlines - 2 &&
3436 s->first_displayed_line +
3437 s->selected_line <= s->blame.nlines)
3438 s->selected_line++;
3439 else if (s->last_displayed_line <
3440 s->blame.nlines)
3441 s->first_displayed_line++;
3442 break;
3443 case 'b':
3444 case 'p': {
3445 struct got_object_id *id = NULL;
3446 id = get_selected_commit_id(s->blame.lines,
3447 s->first_displayed_line, s->selected_line);
3448 if (id == NULL)
3449 break;
3450 if (ch == 'p') {
3451 struct got_commit_object *commit;
3452 struct got_object_qid *pid;
3453 struct got_object_id *blob_id = NULL;
3454 int obj_type;
3455 err = got_object_open_as_commit(&commit,
3456 s->repo, id);
3457 if (err)
3458 break;
3459 pid = SIMPLEQ_FIRST(
3460 got_object_commit_get_parent_ids(commit));
3461 if (pid == NULL) {
3462 got_object_commit_close(commit);
3463 break;
3465 /* Check if path history ends here. */
3466 err = got_object_id_by_path(&blob_id, s->repo,
3467 pid->id, s->path);
3468 if (err) {
3469 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3470 err = NULL;
3471 got_object_commit_close(commit);
3472 break;
3474 err = got_object_get_type(&obj_type, s->repo,
3475 blob_id);
3476 free(blob_id);
3477 /* Can't blame non-blob type objects. */
3478 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3479 got_object_commit_close(commit);
3480 break;
3482 err = got_object_qid_alloc(&s->blamed_commit,
3483 pid->id);
3484 got_object_commit_close(commit);
3485 } else {
3486 if (got_object_id_cmp(id,
3487 s->blamed_commit->id) == 0)
3488 break;
3489 err = got_object_qid_alloc(&s->blamed_commit,
3490 id);
3492 if (err)
3493 break;
3494 s->done = 1;
3495 thread_err = stop_blame(&s->blame);
3496 s->done = 0;
3497 if (thread_err)
3498 break;
3499 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3500 s->blamed_commit, entry);
3501 err = run_blame(&s->blame, view, &s->blame_complete,
3502 &s->first_displayed_line, &s->last_displayed_line,
3503 &s->selected_line, &s->done, &s->eof,
3504 s->path, s->blamed_commit->id, s->repo);
3505 if (err)
3506 break;
3507 break;
3509 case 'B': {
3510 struct got_object_qid *first;
3511 first = SIMPLEQ_FIRST(&s->blamed_commits);
3512 if (!got_object_id_cmp(first->id, s->commit_id))
3513 break;
3514 s->done = 1;
3515 thread_err = stop_blame(&s->blame);
3516 s->done = 0;
3517 if (thread_err)
3518 break;
3519 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3520 got_object_qid_free(s->blamed_commit);
3521 s->blamed_commit =
3522 SIMPLEQ_FIRST(&s->blamed_commits);
3523 err = run_blame(&s->blame, view, &s->blame_complete,
3524 &s->first_displayed_line, &s->last_displayed_line,
3525 &s->selected_line, &s->done, &s->eof, s->path,
3526 s->blamed_commit->id, s->repo);
3527 if (err)
3528 break;
3529 break;
3531 case KEY_ENTER:
3532 case '\r': {
3533 struct got_object_id *id = NULL;
3534 struct got_object_qid *pid;
3535 struct got_commit_object *commit = NULL;
3536 id = get_selected_commit_id(s->blame.lines,
3537 s->first_displayed_line, s->selected_line);
3538 if (id == NULL)
3539 break;
3540 err = got_object_open_as_commit(&commit, s->repo, id);
3541 if (err)
3542 break;
3543 pid = SIMPLEQ_FIRST(
3544 got_object_commit_get_parent_ids(commit));
3545 if (view_is_parent_view(view))
3546 begin_x = view_split_begin_x(view->begin_x);
3547 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3548 if (diff_view == NULL) {
3549 got_object_commit_close(commit);
3550 err = got_error_from_errno("view_open");
3551 break;
3553 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3554 id, NULL, s->refs, s->repo);
3555 got_object_commit_close(commit);
3556 if (err) {
3557 view_close(diff_view);
3558 break;
3560 if (view_is_parent_view(view)) {
3561 err = view_close_child(view);
3562 if (err)
3563 break;
3564 err = view_set_child(view, diff_view);
3565 if (err) {
3566 view_close(diff_view);
3567 break;
3569 *focus_view = diff_view;
3570 view->child_focussed = 1;
3571 } else
3572 *new_view = diff_view;
3573 if (err)
3574 break;
3575 break;
3577 case KEY_NPAGE:
3578 case ' ':
3579 if (s->last_displayed_line >= s->blame.nlines &&
3580 s->selected_line >= MIN(s->blame.nlines,
3581 view->nlines - 2)) {
3582 break;
3584 if (s->last_displayed_line >= s->blame.nlines &&
3585 s->selected_line < view->nlines - 2) {
3586 s->selected_line = MIN(s->blame.nlines,
3587 view->nlines - 2);
3588 break;
3590 if (s->last_displayed_line + view->nlines - 2
3591 <= s->blame.nlines)
3592 s->first_displayed_line +=
3593 view->nlines - 2;
3594 else
3595 s->first_displayed_line =
3596 s->blame.nlines -
3597 (view->nlines - 3);
3598 break;
3599 case KEY_RESIZE:
3600 if (s->selected_line > view->nlines - 2) {
3601 s->selected_line = MIN(s->blame.nlines,
3602 view->nlines - 2);
3604 break;
3605 default:
3606 break;
3608 return thread_err ? thread_err : err;
3611 static const struct got_error *
3612 cmd_blame(int argc, char *argv[])
3614 const struct got_error *error;
3615 struct got_repository *repo = NULL;
3616 struct got_reflist_head refs;
3617 struct got_worktree *worktree = NULL;
3618 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3619 struct got_object_id *commit_id = NULL;
3620 char *commit_id_str = NULL;
3621 int ch;
3622 struct tog_view *view;
3624 SIMPLEQ_INIT(&refs);
3626 #ifndef PROFILE
3627 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3628 NULL) == -1)
3629 err(1, "pledge");
3630 #endif
3632 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3633 switch (ch) {
3634 case 'c':
3635 commit_id_str = optarg;
3636 break;
3637 case 'r':
3638 repo_path = realpath(optarg, NULL);
3639 if (repo_path == NULL)
3640 err(1, "-r option");
3641 break;
3642 default:
3643 usage_blame();
3644 /* NOTREACHED */
3648 argc -= optind;
3649 argv += optind;
3651 if (argc == 1)
3652 path = argv[0];
3653 else
3654 usage_blame();
3656 cwd = getcwd(NULL, 0);
3657 if (cwd == NULL) {
3658 error = got_error_from_errno("getcwd");
3659 goto done;
3661 if (repo_path == NULL) {
3662 error = got_worktree_open(&worktree, cwd);
3663 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3664 goto done;
3665 else
3666 error = NULL;
3667 if (worktree) {
3668 repo_path =
3669 strdup(got_worktree_get_repo_path(worktree));
3670 if (repo_path == NULL)
3671 error = got_error_from_errno("strdup");
3672 if (error)
3673 goto done;
3674 } else {
3675 repo_path = strdup(cwd);
3676 if (repo_path == NULL) {
3677 error = got_error_from_errno("strdup");
3678 goto done;
3683 init_curses();
3685 error = got_repo_open(&repo, repo_path);
3686 if (error != NULL)
3687 goto done;
3689 error = apply_unveil(got_repo_get_path(repo), NULL);
3690 if (error)
3691 goto done;
3693 if (worktree) {
3694 const char *prefix = got_worktree_get_path_prefix(worktree);
3695 char *p, *worktree_subdir = cwd +
3696 strlen(got_worktree_get_root_path(worktree));
3697 if (asprintf(&p, "%s%s%s%s%s",
3698 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3699 worktree_subdir, worktree_subdir[0] ? "/" : "",
3700 path) == -1) {
3701 error = got_error_from_errno("asprintf");
3702 goto done;
3704 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3705 free(p);
3706 } else {
3707 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3709 if (error)
3710 goto done;
3712 if (commit_id_str == NULL) {
3713 struct got_reference *head_ref;
3714 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3715 if (error != NULL)
3716 goto done;
3717 error = got_ref_resolve(&commit_id, repo, head_ref);
3718 got_ref_close(head_ref);
3719 } else {
3720 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3721 if (error) {
3722 if (error->code != GOT_ERR_NOT_REF)
3723 goto done;
3724 error = got_repo_match_object_id_prefix(&commit_id,
3725 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3728 if (error != NULL)
3729 goto done;
3731 error = got_ref_list(&refs, repo);
3732 if (error)
3733 goto done;
3735 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3736 if (view == NULL) {
3737 error = got_error_from_errno("view_open");
3738 goto done;
3740 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3741 if (error)
3742 goto done;
3743 error = view_loop(view);
3744 done:
3745 free(repo_path);
3746 free(cwd);
3747 free(commit_id);
3748 if (worktree)
3749 got_worktree_close(worktree);
3750 if (repo)
3751 got_repo_close(repo);
3752 got_ref_list_free(&refs);
3753 return error;
3756 static const struct got_error *
3757 draw_tree_entries(struct tog_view *view,
3758 struct got_tree_entry **first_displayed_entry,
3759 struct got_tree_entry **last_displayed_entry,
3760 struct got_tree_entry **selected_entry, int *ndisplayed,
3761 const char *label, int show_ids, const char *parent_path,
3762 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3764 const struct got_error *err = NULL;
3765 struct got_tree_entry *te;
3766 wchar_t *wline;
3767 int width, n;
3769 *ndisplayed = 0;
3771 werase(view->window);
3773 if (limit == 0)
3774 return NULL;
3776 err = format_line(&wline, &width, label, view->ncols);
3777 if (err)
3778 return err;
3779 if (view_needs_focus_indication(view))
3780 wstandout(view->window);
3781 waddwstr(view->window, wline);
3782 if (view_needs_focus_indication(view))
3783 wstandend(view->window);
3784 free(wline);
3785 wline = NULL;
3786 if (width < view->ncols - 1)
3787 waddch(view->window, '\n');
3788 if (--limit <= 0)
3789 return NULL;
3790 err = format_line(&wline, &width, parent_path, view->ncols);
3791 if (err)
3792 return err;
3793 waddwstr(view->window, wline);
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 waddch(view->window, '\n');
3801 if (--limit <= 0)
3802 return NULL;
3804 te = SIMPLEQ_FIRST(&entries->head);
3805 if (*first_displayed_entry == NULL) {
3806 if (selected == 0) {
3807 if (view->focussed)
3808 wstandout(view->window);
3809 *selected_entry = NULL;
3811 waddstr(view->window, " ..\n"); /* parent directory */
3812 if (selected == 0 && view->focussed)
3813 wstandend(view->window);
3814 (*ndisplayed)++;
3815 if (--limit <= 0)
3816 return NULL;
3817 n = 1;
3818 } else {
3819 n = 0;
3820 while (te != *first_displayed_entry)
3821 te = SIMPLEQ_NEXT(te, entry);
3824 while (te) {
3825 char *line = NULL, *id_str = NULL;
3827 if (show_ids) {
3828 err = got_object_id_str(&id_str, te->id);
3829 if (err)
3830 return got_error_from_errno(
3831 "got_object_id_str");
3833 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3834 te->name, S_ISDIR(te->mode) ? "/" :
3835 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3836 free(id_str);
3837 return got_error_from_errno("asprintf");
3839 free(id_str);
3840 err = format_line(&wline, &width, line, view->ncols);
3841 if (err) {
3842 free(line);
3843 break;
3845 if (n == selected) {
3846 if (view->focussed)
3847 wstandout(view->window);
3848 *selected_entry = te;
3850 waddwstr(view->window, wline);
3851 if (width < view->ncols - 1)
3852 waddch(view->window, '\n');
3853 if (n == selected && view->focussed)
3854 wstandend(view->window);
3855 free(line);
3856 free(wline);
3857 wline = NULL;
3858 n++;
3859 (*ndisplayed)++;
3860 *last_displayed_entry = te;
3861 if (--limit <= 0)
3862 break;
3863 te = SIMPLEQ_NEXT(te, entry);
3866 return err;
3869 static void
3870 tree_scroll_up(struct tog_view *view,
3871 struct got_tree_entry **first_displayed_entry, int maxscroll,
3872 const struct got_tree_entries *entries, int isroot)
3874 struct got_tree_entry *te, *prev;
3875 int i;
3877 if (*first_displayed_entry == NULL)
3878 return;
3880 te = SIMPLEQ_FIRST(&entries->head);
3881 if (*first_displayed_entry == te) {
3882 if (!isroot)
3883 *first_displayed_entry = NULL;
3884 return;
3887 /* XXX this is stupid... switch to TAILQ? */
3888 for (i = 0; i < maxscroll; i++) {
3889 while (te != *first_displayed_entry) {
3890 prev = te;
3891 te = SIMPLEQ_NEXT(te, entry);
3893 *first_displayed_entry = prev;
3894 te = SIMPLEQ_FIRST(&entries->head);
3896 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3897 *first_displayed_entry = NULL;
3900 static int
3901 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3902 struct got_tree_entry *last_displayed_entry,
3903 const struct got_tree_entries *entries)
3905 struct got_tree_entry *next, *last;
3906 int n = 0;
3908 if (*first_displayed_entry)
3909 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3910 else
3911 next = SIMPLEQ_FIRST(&entries->head);
3912 last = last_displayed_entry;
3913 while (next && last && n++ < maxscroll) {
3914 last = SIMPLEQ_NEXT(last, entry);
3915 if (last) {
3916 *first_displayed_entry = next;
3917 next = SIMPLEQ_NEXT(next, entry);
3920 return n;
3923 static const struct got_error *
3924 tree_entry_path(char **path, struct tog_parent_trees *parents,
3925 struct got_tree_entry *te)
3927 const struct got_error *err = NULL;
3928 struct tog_parent_tree *pt;
3929 size_t len = 2; /* for leading slash and NUL */
3931 TAILQ_FOREACH(pt, parents, entry)
3932 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3933 if (te)
3934 len += strlen(te->name);
3936 *path = calloc(1, len);
3937 if (path == NULL)
3938 return got_error_from_errno("calloc");
3940 (*path)[0] = '/';
3941 pt = TAILQ_LAST(parents, tog_parent_trees);
3942 while (pt) {
3943 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3944 err = got_error(GOT_ERR_NO_SPACE);
3945 goto done;
3947 if (strlcat(*path, "/", len) >= len) {
3948 err = got_error(GOT_ERR_NO_SPACE);
3949 goto done;
3951 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3953 if (te) {
3954 if (strlcat(*path, te->name, len) >= len) {
3955 err = got_error(GOT_ERR_NO_SPACE);
3956 goto done;
3959 done:
3960 if (err) {
3961 free(*path);
3962 *path = NULL;
3964 return err;
3967 static const struct got_error *
3968 blame_tree_entry(struct tog_view **new_view, int begin_x,
3969 struct got_tree_entry *te, struct tog_parent_trees *parents,
3970 struct got_object_id *commit_id, struct got_reflist_head *refs,
3971 struct got_repository *repo)
3973 const struct got_error *err = NULL;
3974 char *path;
3975 struct tog_view *blame_view;
3977 err = tree_entry_path(&path, parents, te);
3978 if (err)
3979 return err;
3981 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3982 if (blame_view == NULL)
3983 return got_error_from_errno("view_open");
3985 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3986 if (err) {
3987 view_close(blame_view);
3988 free(path);
3989 } else
3990 *new_view = blame_view;
3991 return err;
3994 static const struct got_error *
3995 log_tree_entry(struct tog_view **new_view, int begin_x,
3996 struct got_tree_entry *te, struct tog_parent_trees *parents,
3997 struct got_object_id *commit_id, struct got_reflist_head *refs,
3998 struct got_repository *repo)
4000 struct tog_view *log_view;
4001 const struct got_error *err = NULL;
4002 char *path;
4004 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4005 if (log_view == NULL)
4006 return got_error_from_errno("view_open");
4008 err = tree_entry_path(&path, parents, te);
4009 if (err)
4010 return err;
4012 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4013 if (err)
4014 view_close(log_view);
4015 else
4016 *new_view = log_view;
4017 free(path);
4018 return err;
4021 static const struct got_error *
4022 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4023 struct got_object_id *commit_id, struct got_reflist_head *refs,
4024 struct got_repository *repo)
4026 const struct got_error *err = NULL;
4027 char *commit_id_str = NULL;
4028 struct tog_tree_view_state *s = &view->state.tree;
4030 TAILQ_INIT(&s->parents);
4032 err = got_object_id_str(&commit_id_str, commit_id);
4033 if (err != NULL)
4034 goto done;
4036 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4037 err = got_error_from_errno("asprintf");
4038 goto done;
4041 s->root = s->tree = root;
4042 s->entries = got_object_tree_get_entries(root);
4043 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4044 s->commit_id = got_object_id_dup(commit_id);
4045 if (s->commit_id == NULL) {
4046 err = got_error_from_errno("got_object_id_dup");
4047 goto done;
4049 s->refs = refs;
4050 s->repo = repo;
4052 view->show = show_tree_view;
4053 view->input = input_tree_view;
4054 view->close = close_tree_view;
4055 view->search_start = search_start_tree_view;
4056 view->search_next = search_next_tree_view;
4057 done:
4058 free(commit_id_str);
4059 if (err) {
4060 free(s->tree_label);
4061 s->tree_label = NULL;
4063 return err;
4066 static const struct got_error *
4067 close_tree_view(struct tog_view *view)
4069 struct tog_tree_view_state *s = &view->state.tree;
4071 free(s->tree_label);
4072 s->tree_label = NULL;
4073 free(s->commit_id);
4074 s->commit_id = NULL;
4075 while (!TAILQ_EMPTY(&s->parents)) {
4076 struct tog_parent_tree *parent;
4077 parent = TAILQ_FIRST(&s->parents);
4078 TAILQ_REMOVE(&s->parents, parent, entry);
4079 free(parent);
4082 if (s->tree != s->root)
4083 got_object_tree_close(s->tree);
4084 got_object_tree_close(s->root);
4086 return NULL;
4089 static const struct got_error *
4090 search_start_tree_view(struct tog_view *view)
4092 struct tog_tree_view_state *s = &view->state.tree;
4094 s->matched_entry = NULL;
4095 return NULL;
4098 static int
4099 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4101 regmatch_t regmatch;
4103 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4106 static const struct got_error *
4107 search_next_tree_view(struct tog_view *view)
4109 struct tog_tree_view_state *s = &view->state.tree;
4110 struct got_tree_entry *entry, *te;
4112 if (!view->searching) {
4113 view->search_next_done = 1;
4114 return NULL;
4117 if (s->matched_entry) {
4118 if (view->searching == TOG_SEARCH_FORWARD) {
4119 if (s->selected_entry)
4120 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4121 else
4122 entry = SIMPLEQ_FIRST(&s->entries->head);
4124 else {
4125 if (s->selected_entry == NULL) {
4126 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4127 entry = te;
4128 } else {
4129 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4130 entry = te;
4131 if (SIMPLEQ_NEXT(te, entry) ==
4132 s->selected_entry)
4133 break;
4137 } else {
4138 if (view->searching == TOG_SEARCH_FORWARD)
4139 entry = SIMPLEQ_FIRST(&s->entries->head);
4140 else {
4141 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4142 entry = te;
4146 while (1) {
4147 if (entry == NULL) {
4148 if (s->matched_entry == NULL) {
4149 view->search_next_done = 1;
4150 return NULL;
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 if (match_tree_entry(entry, &view->regex)) {
4161 view->search_next_done = 1;
4162 s->matched_entry = entry;
4163 break;
4166 if (view->searching == TOG_SEARCH_FORWARD)
4167 entry = SIMPLEQ_NEXT(entry, entry);
4168 else {
4169 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4170 entry = NULL;
4171 else {
4172 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4173 if (SIMPLEQ_NEXT(te, entry) == entry) {
4174 entry = te;
4175 break;
4182 if (s->matched_entry) {
4183 s->first_displayed_entry = s->matched_entry;
4184 s->selected = 0;
4187 return NULL;
4190 static const struct got_error *
4191 show_tree_view(struct tog_view *view)
4193 const struct got_error *err = NULL;
4194 struct tog_tree_view_state *s = &view->state.tree;
4195 char *parent_path;
4197 err = tree_entry_path(&parent_path, &s->parents, NULL);
4198 if (err)
4199 return err;
4201 err = draw_tree_entries(view, &s->first_displayed_entry,
4202 &s->last_displayed_entry, &s->selected_entry,
4203 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4204 s->entries, s->selected, view->nlines, s->tree == s->root);
4205 free(parent_path);
4207 view_vborder(view);
4208 return err;
4211 static const struct got_error *
4212 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4213 struct tog_view **focus_view, struct tog_view *view, int ch)
4215 const struct got_error *err = NULL;
4216 struct tog_tree_view_state *s = &view->state.tree;
4217 struct tog_view *log_view;
4218 int begin_x = 0, nscrolled;
4220 switch (ch) {
4221 case 'i':
4222 s->show_ids = !s->show_ids;
4223 break;
4224 case 'l':
4225 if (!s->selected_entry)
4226 break;
4227 if (view_is_parent_view(view))
4228 begin_x = view_split_begin_x(view->begin_x);
4229 err = log_tree_entry(&log_view, begin_x,
4230 s->selected_entry, &s->parents,
4231 s->commit_id, s->refs, s->repo);
4232 if (view_is_parent_view(view)) {
4233 err = view_close_child(view);
4234 if (err)
4235 return err;
4236 err = view_set_child(view, log_view);
4237 if (err) {
4238 view_close(log_view);
4239 break;
4241 *focus_view = log_view;
4242 view->child_focussed = 1;
4243 } else
4244 *new_view = log_view;
4245 break;
4246 case 'k':
4247 case KEY_UP:
4248 if (s->selected > 0) {
4249 s->selected--;
4250 if (s->selected == 0)
4251 break;
4253 if (s->selected > 0)
4254 break;
4255 tree_scroll_up(view, &s->first_displayed_entry, 1,
4256 s->entries, s->tree == s->root);
4257 break;
4258 case KEY_PPAGE:
4259 tree_scroll_up(view, &s->first_displayed_entry,
4260 MAX(0, view->nlines - 4 - s->selected), s->entries,
4261 s->tree == s->root);
4262 s->selected = 0;
4263 if (SIMPLEQ_FIRST(&s->entries->head) ==
4264 s->first_displayed_entry && s->tree != s->root)
4265 s->first_displayed_entry = NULL;
4266 break;
4267 case 'j':
4268 case KEY_DOWN:
4269 if (s->selected < s->ndisplayed - 1) {
4270 s->selected++;
4271 break;
4273 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4274 /* can't scroll any further */
4275 break;
4276 tree_scroll_down(&s->first_displayed_entry, 1,
4277 s->last_displayed_entry, s->entries);
4278 break;
4279 case KEY_NPAGE:
4280 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4281 == NULL) {
4282 /* can't scroll any further; move cursor down */
4283 if (s->selected < s->ndisplayed - 1)
4284 s->selected = s->ndisplayed - 1;
4285 break;
4287 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4288 view->nlines, s->last_displayed_entry, s->entries);
4289 if (nscrolled < view->nlines) {
4290 int ndisplayed = 0;
4291 struct got_tree_entry *te;
4292 te = s->first_displayed_entry;
4293 do {
4294 ndisplayed++;
4295 te = SIMPLEQ_NEXT(te, entry);
4296 } while (te);
4297 s->selected = ndisplayed - 1;
4299 break;
4300 case KEY_ENTER:
4301 case '\r':
4302 case KEY_BACKSPACE:
4303 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4304 struct tog_parent_tree *parent;
4305 /* user selected '..' */
4306 if (s->tree == s->root)
4307 break;
4308 parent = TAILQ_FIRST(&s->parents);
4309 TAILQ_REMOVE(&s->parents, parent,
4310 entry);
4311 got_object_tree_close(s->tree);
4312 s->tree = parent->tree;
4313 s->entries =
4314 got_object_tree_get_entries(s->tree);
4315 s->first_displayed_entry =
4316 parent->first_displayed_entry;
4317 s->selected_entry =
4318 parent->selected_entry;
4319 s->selected = parent->selected;
4320 free(parent);
4321 } else if (S_ISDIR(s->selected_entry->mode)) {
4322 struct got_tree_object *subtree;
4323 err = got_object_open_as_tree(&subtree,
4324 s->repo, s->selected_entry->id);
4325 if (err)
4326 break;
4327 err = tree_view_visit_subtree(subtree, s);
4328 if (err) {
4329 got_object_tree_close(subtree);
4330 break;
4332 } else if (S_ISREG(s->selected_entry->mode)) {
4333 struct tog_view *blame_view;
4334 int begin_x = view_is_parent_view(view) ?
4335 view_split_begin_x(view->begin_x) : 0;
4337 err = blame_tree_entry(&blame_view, begin_x,
4338 s->selected_entry, &s->parents,
4339 s->commit_id, s->refs, s->repo);
4340 if (err)
4341 break;
4342 if (view_is_parent_view(view)) {
4343 err = view_close_child(view);
4344 if (err)
4345 return err;
4346 err = view_set_child(view, blame_view);
4347 if (err) {
4348 view_close(blame_view);
4349 break;
4351 *focus_view = blame_view;
4352 view->child_focussed = 1;
4353 } else
4354 *new_view = blame_view;
4356 break;
4357 case KEY_RESIZE:
4358 if (s->selected > view->nlines)
4359 s->selected = s->ndisplayed - 1;
4360 break;
4361 default:
4362 break;
4365 return err;
4368 __dead static void
4369 usage_tree(void)
4371 endwin();
4372 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4373 getprogname());
4374 exit(1);
4377 static const struct got_error *
4378 cmd_tree(int argc, char *argv[])
4380 const struct got_error *error;
4381 struct got_repository *repo = NULL;
4382 struct got_reflist_head refs;
4383 char *repo_path = NULL;
4384 struct got_object_id *commit_id = NULL;
4385 char *commit_id_arg = NULL;
4386 struct got_commit_object *commit = NULL;
4387 struct got_tree_object *tree = NULL;
4388 int ch;
4389 struct tog_view *view;
4391 SIMPLEQ_INIT(&refs);
4393 #ifndef PROFILE
4394 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4395 NULL) == -1)
4396 err(1, "pledge");
4397 #endif
4399 while ((ch = getopt(argc, argv, "c:")) != -1) {
4400 switch (ch) {
4401 case 'c':
4402 commit_id_arg = optarg;
4403 break;
4404 default:
4405 usage_tree();
4406 /* NOTREACHED */
4410 argc -= optind;
4411 argv += optind;
4413 if (argc == 0) {
4414 struct got_worktree *worktree;
4415 char *cwd = getcwd(NULL, 0);
4416 if (cwd == NULL)
4417 return got_error_from_errno("getcwd");
4418 error = got_worktree_open(&worktree, cwd);
4419 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4420 goto done;
4421 if (worktree) {
4422 free(cwd);
4423 repo_path =
4424 strdup(got_worktree_get_repo_path(worktree));
4425 got_worktree_close(worktree);
4426 } else
4427 repo_path = cwd;
4428 if (repo_path == NULL) {
4429 error = got_error_from_errno("strdup");
4430 goto done;
4432 } else if (argc == 1) {
4433 repo_path = realpath(argv[0], NULL);
4434 if (repo_path == NULL)
4435 return got_error_from_errno2("realpath", argv[0]);
4436 } else
4437 usage_log();
4439 init_curses();
4441 error = got_repo_open(&repo, repo_path);
4442 if (error != NULL)
4443 goto done;
4445 error = apply_unveil(got_repo_get_path(repo), NULL);
4446 if (error)
4447 goto done;
4449 if (commit_id_arg == NULL)
4450 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4451 else {
4452 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4453 if (error) {
4454 if (error->code != GOT_ERR_NOT_REF)
4455 goto done;
4456 error = got_repo_match_object_id_prefix(&commit_id,
4457 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4460 if (error != NULL)
4461 goto done;
4463 error = got_object_open_as_commit(&commit, repo, commit_id);
4464 if (error != NULL)
4465 goto done;
4467 error = got_object_open_as_tree(&tree, repo,
4468 got_object_commit_get_tree_id(commit));
4469 if (error != NULL)
4470 goto done;
4472 error = got_ref_list(&refs, repo);
4473 if (error)
4474 goto done;
4476 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4477 if (view == NULL) {
4478 error = got_error_from_errno("view_open");
4479 goto done;
4481 error = open_tree_view(view, tree, commit_id, &refs, repo);
4482 if (error)
4483 goto done;
4484 error = view_loop(view);
4485 done:
4486 free(repo_path);
4487 free(commit_id);
4488 if (commit)
4489 got_object_commit_close(commit);
4490 if (tree)
4491 got_object_tree_close(tree);
4492 if (repo)
4493 got_repo_close(repo);
4494 got_ref_list_free(&refs);
4495 return error;
4498 static void
4499 list_commands(void)
4501 int i;
4503 fprintf(stderr, "commands:");
4504 for (i = 0; i < nitems(tog_commands); i++) {
4505 struct tog_cmd *cmd = &tog_commands[i];
4506 fprintf(stderr, " %s", cmd->name);
4508 fputc('\n', stderr);
4511 __dead static void
4512 usage(int hflag)
4514 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4515 getprogname());
4516 if (hflag)
4517 list_commands();
4518 exit(1);
4521 static char **
4522 make_argv(const char *arg0, const char *arg1)
4524 char **argv;
4525 int argc = (arg1 == NULL ? 1 : 2);
4527 argv = calloc(argc, sizeof(char *));
4528 if (argv == NULL)
4529 err(1, "calloc");
4530 argv[0] = strdup(arg0);
4531 if (argv[0] == NULL)
4532 err(1, "calloc");
4533 if (arg1) {
4534 argv[1] = strdup(arg1);
4535 if (argv[1] == NULL)
4536 err(1, "calloc");
4539 return argv;
4542 int
4543 main(int argc, char *argv[])
4545 const struct got_error *error = NULL;
4546 struct tog_cmd *cmd = NULL;
4547 int ch, hflag = 0, Vflag = 0;
4548 char **cmd_argv = NULL;
4550 setlocale(LC_CTYPE, "");
4552 while ((ch = getopt(argc, argv, "hV")) != -1) {
4553 switch (ch) {
4554 case 'h':
4555 hflag = 1;
4556 break;
4557 case 'V':
4558 Vflag = 1;
4559 break;
4560 default:
4561 usage(hflag);
4562 /* NOTREACHED */
4566 argc -= optind;
4567 argv += optind;
4568 optind = 0;
4569 optreset = 1;
4571 if (Vflag) {
4572 got_version_print_str();
4573 return 1;
4576 if (argc == 0) {
4577 if (hflag)
4578 usage(hflag);
4579 /* Build an argument vector which runs a default command. */
4580 cmd = &tog_commands[0];
4581 cmd_argv = make_argv(cmd->name, NULL);
4582 argc = 1;
4583 } else {
4584 int i;
4586 /* Did the user specific a command? */
4587 for (i = 0; i < nitems(tog_commands); i++) {
4588 if (strncmp(tog_commands[i].name, argv[0],
4589 strlen(argv[0])) == 0) {
4590 cmd = &tog_commands[i];
4591 break;
4595 if (cmd == NULL) {
4596 fprintf(stderr, "%s: unknown command '%s'\n",
4597 getprogname(), argv[0]);
4598 list_commands();
4599 return 1;
4603 if (hflag)
4604 cmd->cmd_usage();
4605 else
4606 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4608 endwin();
4609 free(cmd_argv);
4610 if (error)
4611 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4612 return 0;