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;
325 static volatile sig_atomic_t tog_sigpipe_received;
327 static void
328 tog_sigwinch(int signo)
330 tog_sigwinch_received = 1;
333 static void
334 tog_sigpipe(int signo)
336 tog_sigpipe_received = 1;
339 static const struct got_error *
340 view_close(struct tog_view *view)
342 const struct got_error *err = NULL;
344 if (view->child) {
345 view_close(view->child);
346 view->child = NULL;
348 if (view->close)
349 err = view->close(view);
350 if (view->panel)
351 del_panel(view->panel);
352 if (view->window)
353 delwin(view->window);
354 free(view);
355 return err;
358 static struct tog_view *
359 view_open(int nlines, int ncols, int begin_y, int begin_x,
360 enum tog_view_type type)
362 struct tog_view *view = calloc(1, sizeof(*view));
364 if (view == NULL)
365 return NULL;
367 view->type = type;
368 view->lines = LINES;
369 view->cols = COLS;
370 view->nlines = nlines ? nlines : LINES - begin_y;
371 view->ncols = ncols ? ncols : COLS - begin_x;
372 view->begin_y = begin_y;
373 view->begin_x = begin_x;
374 view->window = newwin(nlines, ncols, begin_y, begin_x);
375 if (view->window == NULL) {
376 view_close(view);
377 return NULL;
379 view->panel = new_panel(view->window);
380 if (view->panel == NULL ||
381 set_panel_userptr(view->panel, view) != OK) {
382 view_close(view);
383 return NULL;
386 keypad(view->window, TRUE);
387 return view;
390 static int
391 view_split_begin_x(int begin_x)
393 if (begin_x > 0 || COLS < 120)
394 return 0;
395 return (COLS - MAX(COLS / 2, 80));
398 static const struct got_error *view_resize(struct tog_view *);
400 static const struct got_error *
401 view_splitscreen(struct tog_view *view)
403 const struct got_error *err = NULL;
405 view->begin_y = 0;
406 view->begin_x = view_split_begin_x(0);
407 view->nlines = LINES;
408 view->ncols = COLS - view->begin_x;
409 view->lines = LINES;
410 view->cols = COLS;
411 err = view_resize(view);
412 if (err)
413 return err;
415 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
416 return got_error_from_errno("mvwin");
418 return NULL;
421 static const struct got_error *
422 view_fullscreen(struct tog_view *view)
424 const struct got_error *err = NULL;
426 view->begin_x = 0;
427 view->begin_y = 0;
428 view->nlines = LINES;
429 view->ncols = COLS;
430 view->lines = LINES;
431 view->cols = COLS;
432 err = view_resize(view);
433 if (err)
434 return err;
436 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
437 return got_error_from_errno("mvwin");
439 return NULL;
442 static int
443 view_is_parent_view(struct tog_view *view)
445 return view->parent == NULL;
448 static const struct got_error *
449 view_resize(struct tog_view *view)
451 int nlines, ncols;
453 if (view->lines > LINES)
454 nlines = view->nlines - (view->lines - LINES);
455 else
456 nlines = view->nlines + (LINES - view->lines);
458 if (view->cols > COLS)
459 ncols = view->ncols - (view->cols - COLS);
460 else
461 ncols = view->ncols + (COLS - view->cols);
463 if (wresize(view->window, nlines, ncols) == ERR)
464 return got_error_from_errno("wresize");
465 if (replace_panel(view->panel, view->window) == ERR)
466 return got_error_from_errno("replace_panel");
467 wclear(view->window);
469 view->nlines = nlines;
470 view->ncols = ncols;
471 view->lines = LINES;
472 view->cols = COLS;
474 if (view->child) {
475 view->child->begin_x = view_split_begin_x(view->begin_x);
476 if (view->child->begin_x == 0) {
477 view_fullscreen(view->child);
478 if (view->child->focussed)
479 show_panel(view->child->panel);
480 else
481 show_panel(view->panel);
482 } else {
483 view_splitscreen(view->child);
484 show_panel(view->child->panel);
488 return NULL;
491 static const struct got_error *
492 view_close_child(struct tog_view *view)
494 const struct got_error *err = NULL;
496 if (view->child == NULL)
497 return NULL;
499 err = view_close(view->child);
500 view->child = NULL;
501 return err;
504 static const struct got_error *
505 view_set_child(struct tog_view *view, struct tog_view *child)
507 const struct got_error *err = NULL;
509 view->child = child;
510 child->parent = view;
511 return err;
514 static int
515 view_is_splitscreen(struct tog_view *view)
517 return view->begin_x > 0;
520 static void
521 tog_resizeterm(void)
523 int cols, lines;
524 struct winsize size;
526 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
527 cols = 80; /* Default */
528 lines = 24;
529 } else {
530 cols = size.ws_col;
531 lines = size.ws_row;
533 resize_term(lines, cols);
536 static const struct got_error *
537 view_search_start(struct tog_view *view)
539 const struct got_error *err = NULL;
540 char pattern[1024];
541 int ret;
543 if (view->nlines < 1)
544 return NULL;
546 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
547 view->begin_x, "/");
548 wclrtoeol(view->window);
550 nocbreak();
551 echo();
552 ret = wgetnstr(view->window, pattern, sizeof(pattern));
553 cbreak();
554 noecho();
555 if (ret == ERR)
556 return NULL;
558 if (view->searching) {
559 regfree(&view->regex);
560 view->searching = 0;
563 if (regcomp(&view->regex, pattern,
564 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
565 err = view->search_start(view);
566 if (err) {
567 regfree(&view->regex);
568 return err;
570 view->searching = TOG_SEARCH_FORWARD;
571 view->search_next_done = 0;
572 view->search_next(view);
575 return NULL;
578 static const struct got_error *
579 view_input(struct tog_view **new, struct tog_view **dead,
580 struct tog_view **focus, int *done, struct tog_view *view,
581 struct tog_view_list_head *views)
583 const struct got_error *err = NULL;
584 struct tog_view *v;
585 int ch, errcode;
587 *new = NULL;
588 *dead = NULL;
589 *focus = NULL;
591 if (view->searching && !view->search_next_done) {
592 errcode = pthread_mutex_unlock(&tog_mutex);
593 if (errcode)
594 return got_error_set_errno(errcode,
595 "pthread_mutex_unlock");
596 pthread_yield();
597 errcode = pthread_mutex_lock(&tog_mutex);
598 if (errcode)
599 return got_error_set_errno(errcode,
600 "pthread_mutex_lock");
601 view->search_next(view);
602 return NULL;
605 nodelay(stdscr, FALSE);
606 /* Allow threads to make progress while we are waiting for input. */
607 errcode = pthread_mutex_unlock(&tog_mutex);
608 if (errcode)
609 return got_error_set_errno(errcode, "pthread_mutex_unlock");
610 ch = wgetch(view->window);
611 errcode = pthread_mutex_lock(&tog_mutex);
612 if (errcode)
613 return got_error_set_errno(errcode, "pthread_mutex_lock");
614 nodelay(stdscr, TRUE);
616 if (tog_sigwinch_received) {
617 tog_resizeterm();
618 tog_sigwinch_received = 0;
619 TAILQ_FOREACH(v, views, entry) {
620 err = view_resize(v);
621 if (err)
622 return err;
623 err = v->input(new, dead, focus, v, KEY_RESIZE);
624 if (err)
625 return err;
629 switch (ch) {
630 case ERR:
631 break;
632 case '\t':
633 if (view->child) {
634 *focus = view->child;
635 view->child_focussed = 1;
636 } else if (view->parent) {
637 *focus = view->parent;
638 view->parent->child_focussed = 0;
640 break;
641 case 'q':
642 err = view->input(new, dead, focus, view, ch);
643 *dead = view;
644 break;
645 case 'Q':
646 *done = 1;
647 break;
648 case 'f':
649 if (view_is_parent_view(view)) {
650 if (view->child == NULL)
651 break;
652 if (view_is_splitscreen(view->child)) {
653 *focus = view->child;
654 view->child_focussed = 1;
655 err = view_fullscreen(view->child);
656 } else
657 err = view_splitscreen(view->child);
658 if (err)
659 break;
660 err = view->child->input(new, dead, focus,
661 view->child, KEY_RESIZE);
662 } else {
663 if (view_is_splitscreen(view)) {
664 *focus = view;
665 view->parent->child_focussed = 1;
666 err = view_fullscreen(view);
667 } else {
668 err = view_splitscreen(view);
670 if (err)
671 break;
672 err = view->input(new, dead, focus, view,
673 KEY_RESIZE);
675 break;
676 case KEY_RESIZE:
677 break;
678 case '/':
679 if (view->search_start)
680 view_search_start(view);
681 else
682 err = view->input(new, dead, focus, view, ch);
683 break;
684 case 'N':
685 case 'n':
686 if (view->search_next && view->searching) {
687 view->searching = (ch == 'n' ?
688 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
689 view->search_next_done = 0;
690 view->search_next(view);
691 } else
692 err = view->input(new, dead, focus, view, ch);
693 break;
694 default:
695 err = view->input(new, dead, focus, view, ch);
696 break;
699 return err;
702 void
703 view_vborder(struct tog_view *view)
705 PANEL *panel;
706 struct tog_view *view_above;
708 if (view->parent)
709 return view_vborder(view->parent);
711 panel = panel_above(view->panel);
712 if (panel == NULL)
713 return;
715 view_above = panel_userptr(panel);
716 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
717 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
720 int
721 view_needs_focus_indication(struct tog_view *view)
723 if (view_is_parent_view(view)) {
724 if (view->child == NULL || view->child_focussed)
725 return 0;
726 if (!view_is_splitscreen(view->child))
727 return 0;
728 } else if (!view_is_splitscreen(view))
729 return 0;
731 return view->focussed;
734 static const struct got_error *
735 view_loop(struct tog_view *view)
737 const struct got_error *err = NULL;
738 struct tog_view_list_head views;
739 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
740 int fast_refresh = 10;
741 int done = 0, errcode;
743 errcode = pthread_mutex_lock(&tog_mutex);
744 if (errcode)
745 return got_error_set_errno(errcode, "pthread_mutex_lock");
747 TAILQ_INIT(&views);
748 TAILQ_INSERT_HEAD(&views, view, entry);
750 main_view = view;
751 view->focussed = 1;
752 err = view->show(view);
753 if (err)
754 return err;
755 update_panels();
756 doupdate();
757 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
758 /* Refresh fast during initialization, then become slower. */
759 if (fast_refresh && fast_refresh-- == 0)
760 halfdelay(10); /* switch to once per second */
762 err = view_input(&new_view, &dead_view, &focus_view, &done,
763 view, &views);
764 if (err)
765 break;
766 if (dead_view) {
767 struct tog_view *prev = NULL;
769 if (view_is_parent_view(dead_view))
770 prev = TAILQ_PREV(dead_view,
771 tog_view_list_head, entry);
772 else if (view->parent != dead_view)
773 prev = view->parent;
775 if (dead_view->parent)
776 dead_view->parent->child = NULL;
777 else
778 TAILQ_REMOVE(&views, dead_view, entry);
780 err = view_close(dead_view);
781 if (err || (dead_view == main_view && new_view == NULL))
782 goto done;
784 if (view == dead_view) {
785 if (focus_view)
786 view = focus_view;
787 else if (prev)
788 view = prev;
789 else if (!TAILQ_EMPTY(&views))
790 view = TAILQ_LAST(&views,
791 tog_view_list_head);
792 else
793 view = NULL;
794 if (view) {
795 if (view->child && view->child_focussed)
796 focus_view = view->child;
797 else
798 focus_view = view;
802 if (new_view) {
803 struct tog_view *v, *t;
804 /* Only allow one parent view per type. */
805 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
806 if (v->type != new_view->type)
807 continue;
808 TAILQ_REMOVE(&views, v, entry);
809 err = view_close(v);
810 if (err)
811 goto done;
812 break;
814 TAILQ_INSERT_TAIL(&views, new_view, entry);
815 view = new_view;
816 if (focus_view == NULL)
817 focus_view = new_view;
819 if (focus_view) {
820 show_panel(focus_view->panel);
821 if (view)
822 view->focussed = 0;
823 focus_view->focussed = 1;
824 view = focus_view;
825 if (new_view)
826 show_panel(new_view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
830 if (view) {
831 if (focus_view == NULL) {
832 view->focussed = 1;
833 show_panel(view->panel);
834 if (view->child && view_is_splitscreen(view->child))
835 show_panel(view->child->panel);
836 focus_view = view;
838 if (view->parent) {
839 err = view->parent->show(view->parent);
840 if (err)
841 goto done;
843 err = view->show(view);
844 if (err)
845 goto done;
846 if (view->child) {
847 err = view->child->show(view->child);
848 if (err)
849 goto done;
851 update_panels();
852 doupdate();
855 done:
856 while (!TAILQ_EMPTY(&views)) {
857 view = TAILQ_FIRST(&views);
858 TAILQ_REMOVE(&views, view, entry);
859 view_close(view);
862 errcode = pthread_mutex_unlock(&tog_mutex);
863 if (errcode && err == NULL)
864 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
866 return err;
869 __dead static void
870 usage_log(void)
872 endwin();
873 fprintf(stderr,
874 "usage: %s log [-c commit] [-r repository-path] [path]\n",
875 getprogname());
876 exit(1);
879 /* Create newly allocated wide-character string equivalent to a byte string. */
880 static const struct got_error *
881 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
883 char *vis = NULL;
884 const struct got_error *err = NULL;
886 *ws = NULL;
887 *wlen = mbstowcs(NULL, s, 0);
888 if (*wlen == (size_t)-1) {
889 int vislen;
890 if (errno != EILSEQ)
891 return got_error_from_errno("mbstowcs");
893 /* byte string invalid in current encoding; try to "fix" it */
894 err = got_mbsavis(&vis, &vislen, s);
895 if (err)
896 return err;
897 *wlen = mbstowcs(NULL, vis, 0);
898 if (*wlen == (size_t)-1) {
899 err = got_error_from_errno("mbstowcs"); /* give up */
900 goto done;
904 *ws = calloc(*wlen + 1, sizeof(*ws));
905 if (*ws == NULL) {
906 err = got_error_from_errno("calloc");
907 goto done;
910 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
911 err = got_error_from_errno("mbstowcs");
912 done:
913 free(vis);
914 if (err) {
915 free(*ws);
916 *ws = NULL;
917 *wlen = 0;
919 return err;
922 /* Format a line for display, ensuring that it won't overflow a width limit. */
923 static const struct got_error *
924 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
926 const struct got_error *err = NULL;
927 int cols = 0;
928 wchar_t *wline = NULL;
929 size_t wlen;
930 int i;
932 *wlinep = NULL;
933 *widthp = 0;
935 err = mbs2ws(&wline, &wlen, line);
936 if (err)
937 return err;
939 i = 0;
940 while (i < wlen && cols < wlimit) {
941 int width = wcwidth(wline[i]);
942 switch (width) {
943 case 0:
944 i++;
945 break;
946 case 1:
947 case 2:
948 if (cols + width <= wlimit)
949 cols += width;
950 i++;
951 break;
952 case -1:
953 if (wline[i] == L'\t')
954 cols += TABSIZE - ((cols + 1) % TABSIZE);
955 i++;
956 break;
957 default:
958 err = got_error_from_errno("wcwidth");
959 goto done;
962 wline[i] = L'\0';
963 if (widthp)
964 *widthp = cols;
965 done:
966 if (err)
967 free(wline);
968 else
969 *wlinep = wline;
970 return err;
973 static const struct got_error*
974 build_refs_str(char **refs_str, struct got_reflist_head *refs,
975 struct got_object_id *id)
977 static const struct got_error *err = NULL;
978 struct got_reflist_entry *re;
979 char *s;
980 const char *name;
982 *refs_str = NULL;
984 SIMPLEQ_FOREACH(re, refs, entry) {
985 if (got_object_id_cmp(re->id, id) != 0)
986 continue;
987 name = got_ref_get_name(re->ref);
988 if (strcmp(name, GOT_REF_HEAD) == 0)
989 continue;
990 if (strncmp(name, "refs/", 5) == 0)
991 name += 5;
992 if (strncmp(name, "got/", 4) == 0)
993 continue;
994 if (strncmp(name, "heads/", 6) == 0)
995 name += 6;
996 if (strncmp(name, "remotes/", 8) == 0)
997 name += 8;
998 s = *refs_str;
999 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1000 s ? ", " : "", name) == -1) {
1001 err = got_error_from_errno("asprintf");
1002 free(s);
1003 *refs_str = NULL;
1004 break;
1006 free(s);
1009 return err;
1012 static const struct got_error *
1013 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1015 char *smallerthan, *at;
1017 smallerthan = strchr(author, '<');
1018 if (smallerthan && smallerthan[1] != '\0')
1019 author = smallerthan + 1;
1020 at = strchr(author, '@');
1021 if (at)
1022 *at = '\0';
1023 return format_line(wauthor, author_width, author, limit);
1026 static const struct got_error *
1027 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1028 struct got_object_id *id, struct got_reflist_head *refs,
1029 int author_display_cols)
1031 const struct got_error *err = NULL;
1032 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1033 char *logmsg0 = NULL, *logmsg = NULL;
1034 char *author = NULL;
1035 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1036 int author_width, logmsg_width;
1037 char *newline, *line = NULL;
1038 int col, limit;
1039 static const size_t date_display_cols = 9;
1040 const int avail = view->ncols;
1041 struct tm tm;
1042 time_t committer_time;
1044 committer_time = got_object_commit_get_committer_time(commit);
1045 if (localtime_r(&committer_time, &tm) == NULL)
1046 return got_error_from_errno("localtime_r");
1047 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1048 >= sizeof(datebuf))
1049 return got_error(GOT_ERR_NO_SPACE);
1051 if (avail < date_display_cols)
1052 limit = MIN(sizeof(datebuf) - 1, avail);
1053 else
1054 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1055 waddnstr(view->window, datebuf, limit);
1056 col = limit + 1;
1057 if (col > avail)
1058 goto done;
1060 author = strdup(got_object_commit_get_author(commit));
1061 if (author == NULL) {
1062 err = got_error_from_errno("strdup");
1063 goto done;
1065 err = format_author(&wauthor, &author_width, author, avail - col);
1066 if (err)
1067 goto done;
1068 waddwstr(view->window, wauthor);
1069 col += author_width;
1070 while (col <= avail && author_width < author_display_cols + 2) {
1071 waddch(view->window, ' ');
1072 col++;
1073 author_width++;
1075 if (col > avail)
1076 goto done;
1078 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1079 if (logmsg0 == NULL) {
1080 err = got_error_from_errno("strdup");
1081 goto done;
1083 logmsg = logmsg0;
1084 while (*logmsg == '\n')
1085 logmsg++;
1086 newline = strchr(logmsg, '\n');
1087 if (newline)
1088 *newline = '\0';
1089 limit = avail - col;
1090 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1091 if (err)
1092 goto done;
1093 waddwstr(view->window, wlogmsg);
1094 col += logmsg_width;
1095 while (col <= avail) {
1096 waddch(view->window, ' ');
1097 col++;
1099 done:
1100 free(logmsg0);
1101 free(wlogmsg);
1102 free(author);
1103 free(wauthor);
1104 free(line);
1105 return err;
1108 static struct commit_queue_entry *
1109 alloc_commit_queue_entry(struct got_commit_object *commit,
1110 struct got_object_id *id)
1112 struct commit_queue_entry *entry;
1114 entry = calloc(1, sizeof(*entry));
1115 if (entry == NULL)
1116 return NULL;
1118 entry->id = id;
1119 entry->commit = commit;
1120 return entry;
1123 static void
1124 pop_commit(struct commit_queue *commits)
1126 struct commit_queue_entry *entry;
1128 entry = TAILQ_FIRST(&commits->head);
1129 TAILQ_REMOVE(&commits->head, entry, entry);
1130 got_object_commit_close(entry->commit);
1131 commits->ncommits--;
1132 /* Don't free entry->id! It is owned by the commit graph. */
1133 free(entry);
1136 static void
1137 free_commits(struct commit_queue *commits)
1139 while (!TAILQ_EMPTY(&commits->head))
1140 pop_commit(commits);
1143 static const struct got_error *
1144 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1145 int minqueue, struct got_repository *repo, const char *path)
1147 const struct got_error *err = NULL;
1148 int nqueued = 0;
1151 * We keep all commits open throughout the lifetime of the log
1152 * view in order to avoid having to re-fetch commits from disk
1153 * while updating the display.
1155 while (nqueued < minqueue) {
1156 struct got_object_id *id;
1157 struct got_commit_object *commit;
1158 struct commit_queue_entry *entry;
1159 int errcode;
1161 err = got_commit_graph_iter_next(&id, graph);
1162 if (err) {
1163 if (err->code != GOT_ERR_ITER_NEED_MORE)
1164 break;
1165 err = got_commit_graph_fetch_commits(graph,
1166 minqueue, repo);
1167 if (err)
1168 return err;
1169 continue;
1172 if (id == NULL)
1173 break;
1175 err = got_object_open_as_commit(&commit, repo, id);
1176 if (err)
1177 break;
1178 entry = alloc_commit_queue_entry(commit, id);
1179 if (entry == NULL) {
1180 err = got_error_from_errno("alloc_commit_queue_entry");
1181 break;
1184 errcode = pthread_mutex_lock(&tog_mutex);
1185 if (errcode) {
1186 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1187 break;
1190 entry->idx = commits->ncommits;
1191 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1192 nqueued++;
1193 commits->ncommits++;
1195 errcode = pthread_mutex_unlock(&tog_mutex);
1196 if (errcode && err == NULL)
1197 err = got_error_set_errno(errcode,
1198 "pthread_mutex_unlock");
1201 return err;
1204 static const struct got_error *
1205 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 struct got_reference *head_ref;
1211 *head_id = NULL;
1213 err = got_ref_open(&head_ref, repo, branch_name, 0);
1214 if (err)
1215 return err;
1217 err = got_ref_resolve(head_id, repo, head_ref);
1218 got_ref_close(head_ref);
1219 if (err) {
1220 *head_id = NULL;
1221 return err;
1224 return NULL;
1227 static const struct got_error *
1228 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1229 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1230 struct commit_queue *commits, int selected_idx, int limit,
1231 struct got_reflist_head *refs, const char *path, int commits_needed)
1233 const struct got_error *err = NULL;
1234 struct commit_queue_entry *entry;
1235 int width;
1236 int ncommits, author_cols = 10;
1237 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1238 char *refs_str = NULL;
1239 wchar_t *wline;
1241 entry = first;
1242 ncommits = 0;
1243 while (entry) {
1244 if (ncommits == selected_idx) {
1245 *selected = entry;
1246 break;
1248 entry = TAILQ_NEXT(entry, entry);
1249 ncommits++;
1252 if (*selected && !(view->searching && view->search_next_done == 0)) {
1253 err = got_object_id_str(&id_str, (*selected)->id);
1254 if (err)
1255 return err;
1256 if (refs) {
1257 err = build_refs_str(&refs_str, refs, (*selected)->id);
1258 if (err)
1259 goto done;
1263 if (commits_needed == 0)
1264 halfdelay(10); /* disable fast refresh */
1266 if (asprintf(&ncommits_str, " [%d/%d] %s",
1267 entry ? entry->idx + 1 : 0, commits->ncommits,
1268 commits_needed > 0 ?
1269 (view->searching && view->search_next_done == 0
1270 ? "searching..." : "loading... ") :
1271 (refs_str ? refs_str : "")) == -1) {
1272 err = got_error_from_errno("asprintf");
1273 goto done;
1276 if (path && strcmp(path, "/") != 0) {
1277 if (asprintf(&header, "commit %s %s%s",
1278 id_str ? id_str : "........................................",
1279 path, ncommits_str) == -1) {
1280 err = got_error_from_errno("asprintf");
1281 header = NULL;
1282 goto done;
1284 } else if (asprintf(&header, "commit %s%s",
1285 id_str ? id_str : "........................................",
1286 ncommits_str) == -1) {
1287 err = got_error_from_errno("asprintf");
1288 header = NULL;
1289 goto done;
1291 err = format_line(&wline, &width, header, view->ncols);
1292 if (err)
1293 goto done;
1295 werase(view->window);
1297 if (view_needs_focus_indication(view))
1298 wstandout(view->window);
1299 waddwstr(view->window, wline);
1300 while (width < view->ncols) {
1301 waddch(view->window, ' ');
1302 width++;
1304 if (view_needs_focus_indication(view))
1305 wstandend(view->window);
1306 free(wline);
1307 if (limit <= 1)
1308 goto done;
1310 /* Grow author column size if necessary. */
1311 entry = first;
1312 ncommits = 0;
1313 while (entry) {
1314 char *author;
1315 wchar_t *wauthor;
1316 int width;
1317 if (ncommits >= limit - 1)
1318 break;
1319 author = strdup(got_object_commit_get_author(entry->commit));
1320 if (author == NULL) {
1321 err = got_error_from_errno("strdup");
1322 goto done;
1324 err = format_author(&wauthor, &width, author, COLS);
1325 if (author_cols < width)
1326 author_cols = width;
1327 free(wauthor);
1328 free(author);
1329 entry = TAILQ_NEXT(entry, entry);
1332 entry = first;
1333 *last = first;
1334 ncommits = 0;
1335 while (entry) {
1336 if (ncommits >= limit - 1)
1337 break;
1338 if (ncommits == selected_idx)
1339 wstandout(view->window);
1340 err = draw_commit(view, entry->commit, entry->id, refs,
1341 author_cols);
1342 if (ncommits == selected_idx)
1343 wstandend(view->window);
1344 if (err)
1345 goto done;
1346 ncommits++;
1347 *last = entry;
1348 entry = TAILQ_NEXT(entry, entry);
1351 view_vborder(view);
1352 done:
1353 free(id_str);
1354 free(refs_str);
1355 free(ncommits_str);
1356 free(header);
1357 return err;
1360 static void
1361 scroll_up(struct tog_view *view,
1362 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1363 struct commit_queue *commits)
1365 struct commit_queue_entry *entry;
1366 int nscrolled = 0;
1368 entry = TAILQ_FIRST(&commits->head);
1369 if (*first_displayed_entry == entry)
1370 return;
1372 entry = *first_displayed_entry;
1373 while (entry && nscrolled < maxscroll) {
1374 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1375 if (entry) {
1376 *first_displayed_entry = entry;
1377 nscrolled++;
1382 static const struct got_error *
1383 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1384 pthread_cond_t *need_commits)
1386 int errcode;
1387 int max_wait = 20;
1389 halfdelay(1); /* fast refresh while loading commits */
1391 while (*commits_needed > 0) {
1392 if (*log_complete)
1393 break;
1395 /* Wake the log thread. */
1396 errcode = pthread_cond_signal(need_commits);
1397 if (errcode)
1398 return got_error_set_errno(errcode,
1399 "pthread_cond_signal");
1400 errcode = pthread_mutex_unlock(&tog_mutex);
1401 if (errcode)
1402 return got_error_set_errno(errcode,
1403 "pthread_mutex_unlock");
1404 pthread_yield();
1405 errcode = pthread_mutex_lock(&tog_mutex);
1406 if (errcode)
1407 return got_error_set_errno(errcode,
1408 "pthread_mutex_lock");
1410 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1412 * Thread is not done yet; lose a key press
1413 * and let the user retry... this way the GUI
1414 * remains interactive while logging deep paths
1415 * with few commits in history.
1417 return NULL;
1421 return NULL;
1424 static const struct got_error *
1425 scroll_down(struct tog_view *view,
1426 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1427 struct commit_queue_entry **last_displayed_entry,
1428 struct commit_queue *commits, int *log_complete, int *commits_needed,
1429 pthread_cond_t *need_commits)
1431 const struct got_error *err = NULL;
1432 struct commit_queue_entry *pentry;
1433 int nscrolled = 0;
1435 if (*last_displayed_entry == NULL)
1436 return NULL;
1438 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1439 if (pentry == NULL && !*log_complete) {
1441 * Ask the log thread for required amount of commits
1442 * plus some amount of pre-fetching.
1444 (*commits_needed) += maxscroll + 20;
1445 err = trigger_log_thread(0, commits_needed, log_complete,
1446 need_commits);
1447 if (err)
1448 return err;
1451 do {
1452 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1453 if (pentry == NULL)
1454 break;
1456 *last_displayed_entry = pentry;
1458 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1459 if (pentry == NULL)
1460 break;
1461 *first_displayed_entry = pentry;
1462 } while (++nscrolled < maxscroll);
1464 return err;
1467 static const struct got_error *
1468 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1469 struct got_commit_object *commit, struct got_object_id *commit_id,
1470 struct tog_view *log_view, struct got_reflist_head *refs,
1471 struct got_repository *repo)
1473 const struct got_error *err;
1474 struct got_object_qid *parent_id;
1475 struct tog_view *diff_view;
1477 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1478 if (diff_view == NULL)
1479 return got_error_from_errno("view_open");
1481 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1482 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1483 commit_id, log_view, refs, repo);
1484 if (err == NULL)
1485 *new_view = diff_view;
1486 return err;
1489 static const struct got_error *
1490 tree_view_visit_subtree(struct got_tree_object *subtree,
1491 struct tog_tree_view_state *s)
1493 struct tog_parent_tree *parent;
1495 parent = calloc(1, sizeof(*parent));
1496 if (parent == NULL)
1497 return got_error_from_errno("calloc");
1499 parent->tree = s->tree;
1500 parent->first_displayed_entry = s->first_displayed_entry;
1501 parent->selected_entry = s->selected_entry;
1502 parent->selected = s->selected;
1503 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1504 s->tree = subtree;
1505 s->entries = got_object_tree_get_entries(s->tree);
1506 s->selected = 0;
1507 s->first_displayed_entry = NULL;
1508 return NULL;
1512 static const struct got_error *
1513 browse_commit_tree(struct tog_view **new_view, int begin_x,
1514 struct commit_queue_entry *entry, const char *path,
1515 struct got_reflist_head *refs, struct got_repository *repo)
1517 const struct got_error *err = NULL;
1518 struct got_tree_object *tree;
1519 struct got_tree_entry *te;
1520 struct tog_tree_view_state *s;
1521 struct tog_view *tree_view;
1522 char *slash, *subpath = NULL;
1523 const char *p;
1525 err = got_object_open_as_tree(&tree, repo,
1526 got_object_commit_get_tree_id(entry->commit));
1527 if (err)
1528 return err;
1530 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1531 if (tree_view == NULL)
1532 return got_error_from_errno("view_open");
1534 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1535 if (err) {
1536 got_object_tree_close(tree);
1537 return err;
1539 s = &tree_view->state.tree;
1541 *new_view = tree_view;
1543 /* Walk the path and open corresponding tree objects. */
1544 p = path;
1545 while (p[0] == '/')
1546 p++;
1547 while (*p) {
1548 struct got_object_id *tree_id;
1550 /* Ensure the correct subtree entry is selected. */
1551 slash = strchr(p, '/');
1552 if (slash == NULL)
1553 slash = strchr(p, '\0');
1554 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1555 if (strncmp(p, te->name, slash - p) == 0) {
1556 s->selected_entry = te;
1557 break;
1559 s->selected++;
1561 if (s->selected_entry == NULL) {
1562 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1563 break;
1565 if (s->tree != s->root)
1566 s->selected++; /* skip '..' */
1568 if (!S_ISDIR(s->selected_entry->mode)) {
1569 /* Jump to this file's entry. */
1570 s->first_displayed_entry = s->selected_entry;
1571 s->selected = 0;
1572 break;
1575 slash = strchr(p, '/');
1576 if (slash)
1577 subpath = strndup(path, slash - path);
1578 else
1579 subpath = strdup(path);
1580 if (subpath == NULL) {
1581 err = got_error_from_errno("strdup");
1582 break;
1585 err = got_object_id_by_path(&tree_id, repo, entry->id,
1586 subpath);
1587 if (err)
1588 break;
1590 err = got_object_open_as_tree(&tree, repo, tree_id);
1591 free(tree_id);
1592 if (err)
1593 break;
1595 err = tree_view_visit_subtree(tree, s);
1596 if (err) {
1597 got_object_tree_close(tree);
1598 break;
1600 if (slash == NULL)
1601 break;
1602 free(subpath);
1603 subpath = NULL;
1604 p = slash;
1607 free(subpath);
1608 return err;
1611 static void *
1612 log_thread(void *arg)
1614 const struct got_error *err = NULL;
1615 int errcode = 0;
1616 struct tog_log_thread_args *a = arg;
1617 int done = 0;
1619 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1620 if (err)
1621 return (void *)err;
1623 while (!done && !err && !tog_sigpipe_received) {
1624 err = queue_commits(a->graph, a->commits, 1, a->repo,
1625 a->in_repo_path);
1626 if (err) {
1627 if (err->code != GOT_ERR_ITER_COMPLETED)
1628 return (void *)err;
1629 err = NULL;
1630 done = 1;
1631 } else if (a->commits_needed > 0)
1632 a->commits_needed--;
1634 errcode = pthread_mutex_lock(&tog_mutex);
1635 if (errcode) {
1636 err = got_error_set_errno(errcode,
1637 "pthread_mutex_lock");
1638 break;
1639 } else if (*a->quit)
1640 done = 1;
1641 else if (*a->first_displayed_entry == NULL) {
1642 *a->first_displayed_entry =
1643 TAILQ_FIRST(&a->commits->head);
1644 *a->selected_entry = *a->first_displayed_entry;
1647 if (done)
1648 a->commits_needed = 0;
1649 else if (a->commits_needed == 0) {
1650 errcode = pthread_cond_wait(&a->need_commits,
1651 &tog_mutex);
1652 if (errcode)
1653 err = got_error_set_errno(errcode,
1654 "pthread_cond_wait");
1657 errcode = pthread_mutex_unlock(&tog_mutex);
1658 if (errcode && err == NULL)
1659 err = got_error_set_errno(errcode,
1660 "pthread_mutex_unlock");
1662 a->log_complete = 1;
1663 return (void *)err;
1666 static const struct got_error *
1667 stop_log_thread(struct tog_log_view_state *s)
1669 const struct got_error *err = NULL;
1670 int errcode;
1672 if (s->thread) {
1673 s->quit = 1;
1674 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1675 if (errcode)
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_signal");
1678 errcode = pthread_mutex_unlock(&tog_mutex);
1679 if (errcode)
1680 return got_error_set_errno(errcode,
1681 "pthread_mutex_unlock");
1682 errcode = pthread_join(s->thread, (void **)&err);
1683 if (errcode)
1684 return got_error_set_errno(errcode, "pthread_join");
1685 errcode = pthread_mutex_lock(&tog_mutex);
1686 if (errcode)
1687 return got_error_set_errno(errcode,
1688 "pthread_mutex_lock");
1689 s->thread = NULL;
1692 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1693 if (errcode && err == NULL)
1694 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1696 if (s->thread_args.repo) {
1697 got_repo_close(s->thread_args.repo);
1698 s->thread_args.repo = NULL;
1701 if (s->thread_args.graph) {
1702 got_commit_graph_close(s->thread_args.graph);
1703 s->thread_args.graph = NULL;
1706 return err;
1709 static const struct got_error *
1710 close_log_view(struct tog_view *view)
1712 const struct got_error *err = NULL;
1713 struct tog_log_view_state *s = &view->state.log;
1715 err = stop_log_thread(s);
1716 free_commits(&s->commits);
1717 free(s->in_repo_path);
1718 s->in_repo_path = NULL;
1719 free(s->start_id);
1720 s->start_id = NULL;
1721 return err;
1724 static const struct got_error *
1725 search_start_log_view(struct tog_view *view)
1727 struct tog_log_view_state *s = &view->state.log;
1729 s->matched_entry = NULL;
1730 s->search_entry = NULL;
1731 return NULL;
1734 static int
1735 match_commit(struct got_commit_object *commit, const char *id_str,
1736 regex_t *regex)
1738 regmatch_t regmatch;
1740 if (regexec(regex, got_object_commit_get_author(commit), 1,
1741 &regmatch, 0) == 0 ||
1742 regexec(regex, got_object_commit_get_committer(commit), 1,
1743 &regmatch, 0) == 0 ||
1744 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1745 &regmatch, 0) == 0 ||
1746 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1747 return 1;
1749 return 0;
1752 static const struct got_error *
1753 search_next_log_view(struct tog_view *view)
1755 const struct got_error *err = NULL;
1756 struct tog_log_view_state *s = &view->state.log;
1757 struct commit_queue_entry *entry;
1759 if (!view->searching) {
1760 view->search_next_done = 1;
1761 return NULL;
1764 if (s->search_entry) {
1765 if (wgetch(view->window) == KEY_BACKSPACE) {
1766 view->search_next_done = 1;
1767 return NULL;
1769 if (view->searching == TOG_SEARCH_FORWARD)
1770 entry = TAILQ_NEXT(s->search_entry, entry);
1771 else
1772 entry = TAILQ_PREV(s->search_entry,
1773 commit_queue_head, entry);
1774 } else if (s->matched_entry) {
1775 if (view->searching == TOG_SEARCH_FORWARD)
1776 entry = TAILQ_NEXT(s->selected_entry, entry);
1777 else
1778 entry = TAILQ_PREV(s->selected_entry,
1779 commit_queue_head, entry);
1780 } else {
1781 if (view->searching == TOG_SEARCH_FORWARD)
1782 entry = TAILQ_FIRST(&s->commits.head);
1783 else
1784 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1787 while (1) {
1788 char *id_str;
1789 if (entry == NULL) {
1790 if (s->thread_args.log_complete ||
1791 view->searching == TOG_SEARCH_BACKWARD) {
1792 view->search_next_done = 1;
1793 return NULL;
1796 * Poke the log thread for more commits and return,
1797 * allowing the main loop to make progress. Search
1798 * will resume at s->search_entry once we come back.
1800 s->thread_args.commits_needed++;
1801 return trigger_log_thread(1,
1802 &s->thread_args.commits_needed,
1803 &s->thread_args.log_complete,
1804 &s->thread_args.need_commits);
1807 err = got_object_id_str(&id_str, entry->id);
1808 if (err)
1809 return err;
1811 if (match_commit(entry->commit, id_str, &view->regex)) {
1812 view->search_next_done = 1;
1813 s->matched_entry = entry;
1814 free(id_str);
1815 break;
1817 free(id_str);
1818 s->search_entry = entry;
1819 if (view->searching == TOG_SEARCH_FORWARD)
1820 entry = TAILQ_NEXT(entry, entry);
1821 else
1822 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1825 if (s->matched_entry) {
1826 int cur = s->selected_entry->idx;
1827 while (cur < s->matched_entry->idx) {
1828 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1829 if (err)
1830 return err;
1831 cur++;
1833 while (cur > s->matched_entry->idx) {
1834 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1835 if (err)
1836 return err;
1837 cur--;
1841 s->search_entry = NULL;
1843 return NULL;
1846 static const struct got_error *
1847 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1848 struct got_reflist_head *refs, struct got_repository *repo,
1849 const char *head_ref_name, const char *path, int check_disk)
1851 const struct got_error *err = NULL;
1852 struct tog_log_view_state *s = &view->state.log;
1853 struct got_repository *thread_repo = NULL;
1854 struct got_commit_graph *thread_graph = NULL;
1855 int errcode;
1857 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1858 if (err != NULL)
1859 goto done;
1861 /* The commit queue only contains commits being displayed. */
1862 TAILQ_INIT(&s->commits.head);
1863 s->commits.ncommits = 0;
1865 s->refs = refs;
1866 s->repo = repo;
1867 s->head_ref_name = head_ref_name;
1868 s->start_id = got_object_id_dup(start_id);
1869 if (s->start_id == NULL) {
1870 err = got_error_from_errno("got_object_id_dup");
1871 goto done;
1874 view->show = show_log_view;
1875 view->input = input_log_view;
1876 view->close = close_log_view;
1877 view->search_start = search_start_log_view;
1878 view->search_next = search_next_log_view;
1880 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1881 if (err)
1882 goto done;
1883 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1884 0, thread_repo);
1885 if (err)
1886 goto done;
1888 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1889 if (errcode) {
1890 err = got_error_set_errno(errcode, "pthread_cond_init");
1891 goto done;
1894 s->thread_args.commits_needed = view->nlines;
1895 s->thread_args.graph = thread_graph;
1896 s->thread_args.commits = &s->commits;
1897 s->thread_args.in_repo_path = s->in_repo_path;
1898 s->thread_args.start_id = s->start_id;
1899 s->thread_args.repo = thread_repo;
1900 s->thread_args.log_complete = 0;
1901 s->thread_args.quit = &s->quit;
1902 s->thread_args.view = view;
1903 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1904 s->thread_args.selected_entry = &s->selected_entry;
1905 done:
1906 if (err)
1907 close_log_view(view);
1908 return err;
1911 static const struct got_error *
1912 show_log_view(struct tog_view *view)
1914 struct tog_log_view_state *s = &view->state.log;
1916 if (s->thread == NULL) {
1917 int errcode = pthread_create(&s->thread, NULL, log_thread,
1918 &s->thread_args);
1919 if (errcode)
1920 return got_error_set_errno(errcode, "pthread_create");
1923 return draw_commits(view, &s->last_displayed_entry,
1924 &s->selected_entry, s->first_displayed_entry,
1925 &s->commits, s->selected, view->nlines, s->refs,
1926 s->in_repo_path, s->thread_args.commits_needed);
1929 static const struct got_error *
1930 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1931 struct tog_view **focus_view, struct tog_view *view, int ch)
1933 const struct got_error *err = NULL;
1934 struct tog_log_view_state *s = &view->state.log;
1935 char *parent_path, *in_repo_path = NULL;
1936 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1937 int begin_x = 0;
1938 struct got_object_id *start_id;
1940 switch (ch) {
1941 case 'q':
1942 s->quit = 1;
1943 break;
1944 case 'k':
1945 case KEY_UP:
1946 case '<':
1947 case ',':
1948 if (s->first_displayed_entry == NULL)
1949 break;
1950 if (s->selected > 0)
1951 s->selected--;
1952 else
1953 scroll_up(view, &s->first_displayed_entry, 1,
1954 &s->commits);
1955 break;
1956 case KEY_PPAGE:
1957 case CTRL('b'):
1958 if (s->first_displayed_entry == NULL)
1959 break;
1960 if (TAILQ_FIRST(&s->commits.head) ==
1961 s->first_displayed_entry) {
1962 s->selected = 0;
1963 break;
1965 scroll_up(view, &s->first_displayed_entry,
1966 view->nlines, &s->commits);
1967 break;
1968 case 'j':
1969 case KEY_DOWN:
1970 case '>':
1971 case '.':
1972 if (s->first_displayed_entry == NULL)
1973 break;
1974 if (s->selected < MIN(view->nlines - 2,
1975 s->commits.ncommits - 1)) {
1976 s->selected++;
1977 break;
1979 err = scroll_down(view, &s->first_displayed_entry, 1,
1980 &s->last_displayed_entry, &s->commits,
1981 &s->thread_args.log_complete,
1982 &s->thread_args.commits_needed,
1983 &s->thread_args.need_commits);
1984 break;
1985 case KEY_NPAGE:
1986 case CTRL('f'): {
1987 struct commit_queue_entry *first;
1988 first = s->first_displayed_entry;
1989 if (first == NULL)
1990 break;
1991 err = scroll_down(view, &s->first_displayed_entry,
1992 view->nlines, &s->last_displayed_entry,
1993 &s->commits, &s->thread_args.log_complete,
1994 &s->thread_args.commits_needed,
1995 &s->thread_args.need_commits);
1996 if (first == s->first_displayed_entry &&
1997 s->selected < MIN(view->nlines - 2,
1998 s->commits.ncommits - 1)) {
1999 /* can't scroll further down */
2000 s->selected = MIN(view->nlines - 2,
2001 s->commits.ncommits - 1);
2003 err = NULL;
2004 break;
2006 case KEY_RESIZE:
2007 if (s->selected > view->nlines - 2)
2008 s->selected = view->nlines - 2;
2009 if (s->selected > s->commits.ncommits - 1)
2010 s->selected = s->commits.ncommits - 1;
2011 break;
2012 case KEY_ENTER:
2013 case ' ':
2014 case '\r':
2015 if (s->selected_entry == NULL)
2016 break;
2017 if (view_is_parent_view(view))
2018 begin_x = view_split_begin_x(view->begin_x);
2019 err = open_diff_view_for_commit(&diff_view, begin_x,
2020 s->selected_entry->commit, s->selected_entry->id,
2021 view, s->refs, s->repo);
2022 if (err)
2023 break;
2024 if (view_is_parent_view(view)) {
2025 err = view_close_child(view);
2026 if (err)
2027 return err;
2028 err = view_set_child(view, diff_view);
2029 if (err) {
2030 view_close(diff_view);
2031 break;
2033 *focus_view = diff_view;
2034 view->child_focussed = 1;
2035 } else
2036 *new_view = diff_view;
2037 break;
2038 case 't':
2039 if (s->selected_entry == NULL)
2040 break;
2041 if (view_is_parent_view(view))
2042 begin_x = view_split_begin_x(view->begin_x);
2043 err = browse_commit_tree(&tree_view, begin_x,
2044 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2045 if (err)
2046 break;
2047 if (view_is_parent_view(view)) {
2048 err = view_close_child(view);
2049 if (err)
2050 return err;
2051 err = view_set_child(view, tree_view);
2052 if (err) {
2053 view_close(tree_view);
2054 break;
2056 *focus_view = tree_view;
2057 view->child_focussed = 1;
2058 } else
2059 *new_view = tree_view;
2060 break;
2061 case KEY_BACKSPACE:
2062 if (strcmp(s->in_repo_path, "/") == 0)
2063 break;
2064 parent_path = dirname(s->in_repo_path);
2065 if (parent_path && strcmp(parent_path, ".") != 0) {
2066 err = stop_log_thread(s);
2067 if (err)
2068 return err;
2069 lv = view_open(view->nlines, view->ncols,
2070 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2071 if (lv == NULL)
2072 return got_error_from_errno(
2073 "view_open");
2074 err = open_log_view(lv, s->start_id, s->refs,
2075 s->repo, s->head_ref_name, parent_path, 0);
2076 if (err)
2077 return err;;
2078 if (view_is_parent_view(view))
2079 *new_view = lv;
2080 else {
2081 view_set_child(view->parent, lv);
2082 *focus_view = lv;
2084 return NULL;
2086 break;
2087 case CTRL('l'):
2088 err = stop_log_thread(s);
2089 if (err)
2090 return err;
2091 lv = view_open(view->nlines, view->ncols,
2092 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2093 if (lv == NULL)
2094 return got_error_from_errno("view_open");
2095 err = get_head_commit_id(&start_id, s->head_ref_name ?
2096 s->head_ref_name : GOT_REF_HEAD, s->repo);
2097 if (err) {
2098 view_close(lv);
2099 return err;
2101 in_repo_path = strdup(s->in_repo_path);
2102 if (in_repo_path == NULL) {
2103 free(start_id);
2104 view_close(lv);
2105 return got_error_from_errno("strdup");
2107 err = open_log_view(lv, start_id, s->refs, s->repo,
2108 s->head_ref_name, in_repo_path, 0);
2109 if (err) {
2110 free(start_id);
2111 view_close(lv);
2112 return err;;
2114 *dead_view = view;
2115 *new_view = lv;
2116 break;
2117 default:
2118 break;
2121 return err;
2124 static const struct got_error *
2125 apply_unveil(const char *repo_path, const char *worktree_path)
2127 const struct got_error *error;
2129 #ifdef PROFILE
2130 if (unveil("gmon.out", "rwc") != 0)
2131 return got_error_from_errno2("unveil", "gmon.out");
2132 #endif
2133 if (repo_path && unveil(repo_path, "r") != 0)
2134 return got_error_from_errno2("unveil", repo_path);
2136 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2137 return got_error_from_errno2("unveil", worktree_path);
2139 if (unveil("/tmp", "rwc") != 0)
2140 return got_error_from_errno2("unveil", "/tmp");
2142 error = got_privsep_unveil_exec_helpers();
2143 if (error != NULL)
2144 return error;
2146 if (unveil(NULL, NULL) != 0)
2147 return got_error_from_errno("unveil");
2149 return NULL;
2152 static void
2153 init_curses(void)
2155 initscr();
2156 cbreak();
2157 halfdelay(1); /* Do fast refresh while initial view is loading. */
2158 noecho();
2159 nonl();
2160 intrflush(stdscr, FALSE);
2161 keypad(stdscr, TRUE);
2162 curs_set(0);
2163 signal(SIGWINCH, tog_sigwinch);
2164 signal(SIGPIPE, tog_sigpipe);
2167 static const struct got_error *
2168 cmd_log(int argc, char *argv[])
2170 const struct got_error *error;
2171 struct got_repository *repo = NULL;
2172 struct got_worktree *worktree = NULL;
2173 struct got_reflist_head refs;
2174 struct got_object_id *start_id = NULL;
2175 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2176 char *start_commit = NULL;
2177 int ch;
2178 struct tog_view *view;
2180 SIMPLEQ_INIT(&refs);
2182 #ifndef PROFILE
2183 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2184 NULL) == -1)
2185 err(1, "pledge");
2186 #endif
2188 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2189 switch (ch) {
2190 case 'c':
2191 start_commit = optarg;
2192 break;
2193 case 'r':
2194 repo_path = realpath(optarg, NULL);
2195 if (repo_path == NULL)
2196 err(1, "-r option");
2197 break;
2198 default:
2199 usage_log();
2200 /* NOTREACHED */
2204 argc -= optind;
2205 argv += optind;
2207 cwd = getcwd(NULL, 0);
2208 if (cwd == NULL) {
2209 error = got_error_from_errno("getcwd");
2210 goto done;
2212 error = got_worktree_open(&worktree, cwd);
2213 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2214 goto done;
2215 error = NULL;
2217 if (argc == 0) {
2218 path = strdup("");
2219 if (path == NULL) {
2220 error = got_error_from_errno("strdup");
2221 goto done;
2223 } else if (argc == 1) {
2224 if (worktree) {
2225 error = got_worktree_resolve_path(&path, worktree,
2226 argv[0]);
2227 if (error)
2228 goto done;
2229 } else {
2230 path = strdup(argv[0]);
2231 if (path == NULL) {
2232 error = got_error_from_errno("strdup");
2233 goto done;
2236 } else
2237 usage_log();
2239 if (repo_path == NULL) {
2240 if (worktree)
2241 repo_path = strdup(
2242 got_worktree_get_repo_path(worktree));
2243 else
2244 repo_path = strdup(cwd);
2246 if (repo_path == NULL) {
2247 error = got_error_from_errno("strdup");
2248 goto done;
2251 init_curses();
2253 error = got_repo_open(&repo, repo_path);
2254 if (error != NULL)
2255 goto done;
2257 error = apply_unveil(got_repo_get_path(repo),
2258 worktree ? got_worktree_get_root_path(worktree) : NULL);
2259 if (error)
2260 goto done;
2262 if (start_commit == NULL)
2263 error = get_head_commit_id(&start_id, worktree ?
2264 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2265 repo);
2266 else {
2267 error = get_head_commit_id(&start_id, start_commit, repo);
2268 if (error) {
2269 if (error->code != GOT_ERR_NOT_REF)
2270 goto done;
2271 error = got_repo_match_object_id_prefix(&start_id,
2272 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2275 if (error != NULL)
2276 goto done;
2278 error = got_ref_list(&refs, repo);
2279 if (error)
2280 goto done;
2282 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2283 if (view == NULL) {
2284 error = got_error_from_errno("view_open");
2285 goto done;
2287 error = open_log_view(view, start_id, &refs, repo, worktree ?
2288 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2289 if (error)
2290 goto done;
2291 error = view_loop(view);
2292 done:
2293 free(repo_path);
2294 free(cwd);
2295 free(path);
2296 free(start_id);
2297 if (repo)
2298 got_repo_close(repo);
2299 if (worktree)
2300 got_worktree_close(worktree);
2301 got_ref_list_free(&refs);
2302 return error;
2305 __dead static void
2306 usage_diff(void)
2308 endwin();
2309 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2310 getprogname());
2311 exit(1);
2314 static char *
2315 parse_next_line(FILE *f, size_t *len)
2317 char *line;
2318 size_t linelen;
2319 size_t lineno;
2320 const char delim[3] = { '\0', '\0', '\0'};
2322 line = fparseln(f, &linelen, &lineno, delim, 0);
2323 if (len)
2324 *len = linelen;
2325 return line;
2328 static const struct got_error *
2329 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2330 int *last_displayed_line, int *eof, int max_lines,
2331 char *header)
2333 const struct got_error *err;
2334 int nlines = 0, nprinted = 0;
2335 char *line;
2336 size_t len;
2337 wchar_t *wline;
2338 int width;
2340 rewind(f);
2341 werase(view->window);
2343 if (header) {
2344 err = format_line(&wline, &width, header, view->ncols);
2345 if (err) {
2346 return err;
2349 if (view_needs_focus_indication(view))
2350 wstandout(view->window);
2351 waddwstr(view->window, wline);
2352 if (view_needs_focus_indication(view))
2353 wstandend(view->window);
2354 if (width < view->ncols - 1)
2355 waddch(view->window, '\n');
2357 if (max_lines <= 1)
2358 return NULL;
2359 max_lines--;
2362 *eof = 0;
2363 while (nprinted < max_lines) {
2364 line = parse_next_line(f, &len);
2365 if (line == NULL) {
2366 *eof = 1;
2367 break;
2369 if (++nlines < *first_displayed_line) {
2370 free(line);
2371 continue;
2374 err = format_line(&wline, &width, line, view->ncols);
2375 if (err) {
2376 free(line);
2377 return err;
2379 waddwstr(view->window, wline);
2380 if (width < view->ncols - 1)
2381 waddch(view->window, '\n');
2382 if (++nprinted == 1)
2383 *first_displayed_line = nlines;
2384 free(line);
2385 free(wline);
2386 wline = NULL;
2388 *last_displayed_line = nlines;
2390 view_vborder(view);
2392 if (*eof) {
2393 while (nprinted < view->nlines) {
2394 waddch(view->window, '\n');
2395 nprinted++;
2398 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2399 if (err) {
2400 return err;
2403 wstandout(view->window);
2404 waddwstr(view->window, wline);
2405 wstandend(view->window);
2408 return NULL;
2411 static char *
2412 get_datestr(time_t *time, char *datebuf)
2414 char *p, *s = ctime_r(time, datebuf);
2415 p = strchr(s, '\n');
2416 if (p)
2417 *p = '\0';
2418 return s;
2421 static const struct got_error *
2422 write_commit_info(struct got_object_id *commit_id,
2423 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2425 const struct got_error *err = NULL;
2426 char datebuf[26];
2427 struct got_commit_object *commit;
2428 char *id_str = NULL;
2429 time_t committer_time;
2430 const char *author, *committer;
2431 char *refs_str = NULL;
2433 if (refs) {
2434 err = build_refs_str(&refs_str, refs, commit_id);
2435 if (err)
2436 return err;
2439 err = got_object_open_as_commit(&commit, repo, commit_id);
2440 if (err)
2441 return err;
2443 err = got_object_id_str(&id_str, commit_id);
2444 if (err) {
2445 err = got_error_from_errno("got_object_id_str");
2446 goto done;
2449 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2450 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2451 err = got_error_from_errno("fprintf");
2452 goto done;
2454 if (fprintf(outfile, "from: %s\n",
2455 got_object_commit_get_author(commit)) < 0) {
2456 err = got_error_from_errno("fprintf");
2457 goto done;
2459 committer_time = got_object_commit_get_committer_time(commit);
2460 if (fprintf(outfile, "date: %s UTC\n",
2461 get_datestr(&committer_time, datebuf)) < 0) {
2462 err = got_error_from_errno("fprintf");
2463 goto done;
2465 author = got_object_commit_get_author(commit);
2466 committer = got_object_commit_get_committer(commit);
2467 if (strcmp(author, committer) != 0 &&
2468 fprintf(outfile, "via: %s\n", committer) < 0) {
2469 err = got_error_from_errno("fprintf");
2470 goto done;
2472 if (fprintf(outfile, "%s\n",
2473 got_object_commit_get_logmsg(commit)) < 0) {
2474 err = got_error_from_errno("fprintf");
2475 goto done;
2477 done:
2478 free(id_str);
2479 free(refs_str);
2480 got_object_commit_close(commit);
2481 return err;
2484 static const struct got_error *
2485 create_diff(struct tog_diff_view_state *s)
2487 const struct got_error *err = NULL;
2488 FILE *f = NULL;
2489 int obj_type;
2491 f = got_opentemp();
2492 if (f == NULL) {
2493 err = got_error_from_errno("got_opentemp");
2494 goto done;
2496 if (s->f && fclose(s->f) != 0) {
2497 err = got_error_from_errno("fclose");
2498 goto done;
2500 s->f = f;
2502 if (s->id1)
2503 err = got_object_get_type(&obj_type, s->repo, s->id1);
2504 else
2505 err = got_object_get_type(&obj_type, s->repo, s->id2);
2506 if (err)
2507 goto done;
2509 switch (obj_type) {
2510 case GOT_OBJ_TYPE_BLOB:
2511 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2512 s->diff_context, s->repo, f);
2513 break;
2514 case GOT_OBJ_TYPE_TREE:
2515 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2516 s->diff_context, s->repo, f);
2517 break;
2518 case GOT_OBJ_TYPE_COMMIT: {
2519 const struct got_object_id_queue *parent_ids;
2520 struct got_object_qid *pid;
2521 struct got_commit_object *commit2;
2523 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2524 if (err)
2525 break;
2526 /* Show commit info if we're diffing to a parent/root commit. */
2527 if (s->id1 == NULL)
2528 write_commit_info(s->id2, s->refs, s->repo, f);
2529 else {
2530 parent_ids = got_object_commit_get_parent_ids(commit2);
2531 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2532 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2533 write_commit_info(s->id2, s->refs,
2534 s->repo, f);
2535 break;
2539 got_object_commit_close(commit2);
2541 err = got_diff_objects_as_commits(s->id1, s->id2,
2542 s->diff_context, s->repo, f);
2543 break;
2545 default:
2546 err = got_error(GOT_ERR_OBJ_TYPE);
2547 break;
2549 done:
2550 if (f && fflush(f) != 0 && err == NULL)
2551 err = got_error_from_errno("fflush");
2552 return err;
2555 static void
2556 diff_view_indicate_progress(struct tog_view *view)
2558 mvwaddstr(view->window, 0, 0, "diffing...");
2559 update_panels();
2560 doupdate();
2563 static const struct got_error *
2564 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2565 struct got_object_id *id2, struct tog_view *log_view,
2566 struct got_reflist_head *refs, struct got_repository *repo)
2568 const struct got_error *err;
2570 if (id1 != NULL && id2 != NULL) {
2571 int type1, type2;
2572 err = got_object_get_type(&type1, repo, id1);
2573 if (err)
2574 return err;
2575 err = got_object_get_type(&type2, repo, id2);
2576 if (err)
2577 return err;
2579 if (type1 != type2)
2580 return got_error(GOT_ERR_OBJ_TYPE);
2583 if (id1) {
2584 view->state.diff.id1 = got_object_id_dup(id1);
2585 if (view->state.diff.id1 == NULL)
2586 return got_error_from_errno("got_object_id_dup");
2587 } else
2588 view->state.diff.id1 = NULL;
2590 view->state.diff.id2 = got_object_id_dup(id2);
2591 if (view->state.diff.id2 == NULL) {
2592 free(view->state.diff.id1);
2593 view->state.diff.id1 = NULL;
2594 return got_error_from_errno("got_object_id_dup");
2596 view->state.diff.f = NULL;
2597 view->state.diff.first_displayed_line = 1;
2598 view->state.diff.last_displayed_line = view->nlines;
2599 view->state.diff.diff_context = 3;
2600 view->state.diff.log_view = log_view;
2601 view->state.diff.repo = repo;
2602 view->state.diff.refs = refs;
2604 if (log_view && view_is_splitscreen(view))
2605 show_log_view(log_view); /* draw vborder */
2606 diff_view_indicate_progress(view);
2608 err = create_diff(&view->state.diff);
2609 if (err) {
2610 free(view->state.diff.id1);
2611 view->state.diff.id1 = NULL;
2612 free(view->state.diff.id2);
2613 view->state.diff.id2 = NULL;
2614 return err;
2617 view->show = show_diff_view;
2618 view->input = input_diff_view;
2619 view->close = close_diff_view;
2621 return NULL;
2624 static const struct got_error *
2625 close_diff_view(struct tog_view *view)
2627 const struct got_error *err = NULL;
2629 free(view->state.diff.id1);
2630 view->state.diff.id1 = NULL;
2631 free(view->state.diff.id2);
2632 view->state.diff.id2 = NULL;
2633 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2634 err = got_error_from_errno("fclose");
2635 return err;
2638 static const struct got_error *
2639 show_diff_view(struct tog_view *view)
2641 const struct got_error *err;
2642 struct tog_diff_view_state *s = &view->state.diff;
2643 char *id_str1 = NULL, *id_str2, *header;
2645 if (s->id1) {
2646 err = got_object_id_str(&id_str1, s->id1);
2647 if (err)
2648 return err;
2650 err = got_object_id_str(&id_str2, s->id2);
2651 if (err)
2652 return err;
2654 if (asprintf(&header, "diff %s %s",
2655 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2656 err = got_error_from_errno("asprintf");
2657 free(id_str1);
2658 free(id_str2);
2659 return err;
2661 free(id_str1);
2662 free(id_str2);
2664 return draw_file(view, s->f, &s->first_displayed_line,
2665 &s->last_displayed_line, &s->eof, view->nlines,
2666 header);
2669 static const struct got_error *
2670 set_selected_commit(struct tog_diff_view_state *s,
2671 struct commit_queue_entry *entry)
2673 const struct got_error *err;
2674 const struct got_object_id_queue *parent_ids;
2675 struct got_commit_object *selected_commit;
2676 struct got_object_qid *pid;
2678 free(s->id2);
2679 s->id2 = got_object_id_dup(entry->id);
2680 if (s->id2 == NULL)
2681 return got_error_from_errno("got_object_id_dup");
2683 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2684 if (err)
2685 return err;
2686 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2687 free(s->id1);
2688 pid = SIMPLEQ_FIRST(parent_ids);
2689 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2690 got_object_commit_close(selected_commit);
2691 return NULL;
2694 static const struct got_error *
2695 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2696 struct tog_view **focus_view, struct tog_view *view, int ch)
2698 const struct got_error *err = NULL;
2699 struct tog_diff_view_state *s = &view->state.diff;
2700 struct tog_log_view_state *ls;
2701 struct commit_queue_entry *entry;
2702 int i;
2704 switch (ch) {
2705 case 'k':
2706 case KEY_UP:
2707 if (s->first_displayed_line > 1)
2708 s->first_displayed_line--;
2709 break;
2710 case KEY_PPAGE:
2711 case CTRL('b'):
2712 if (s->first_displayed_line == 1)
2713 break;
2714 i = 0;
2715 while (i++ < view->nlines - 1 &&
2716 s->first_displayed_line > 1)
2717 s->first_displayed_line--;
2718 break;
2719 case 'j':
2720 case KEY_DOWN:
2721 if (!s->eof)
2722 s->first_displayed_line++;
2723 break;
2724 case KEY_NPAGE:
2725 case CTRL('f'):
2726 case ' ':
2727 if (s->eof)
2728 break;
2729 i = 0;
2730 while (!s->eof && i++ < view->nlines - 1) {
2731 char *line;
2732 line = parse_next_line(s->f, NULL);
2733 s->first_displayed_line++;
2734 if (line == NULL)
2735 break;
2737 break;
2738 case '[':
2739 if (s->diff_context > 0) {
2740 s->diff_context--;
2741 diff_view_indicate_progress(view);
2742 err = create_diff(s);
2744 break;
2745 case ']':
2746 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2747 s->diff_context++;
2748 diff_view_indicate_progress(view);
2749 err = create_diff(s);
2751 break;
2752 case '<':
2753 case ',':
2754 if (s->log_view == NULL)
2755 break;
2756 ls = &s->log_view->state.log;
2757 entry = TAILQ_PREV(ls->selected_entry,
2758 commit_queue_head, entry);
2759 if (entry == NULL)
2760 break;
2762 err = input_log_view(NULL, NULL, NULL, s->log_view,
2763 KEY_UP);
2764 if (err)
2765 break;
2767 err = set_selected_commit(s, entry);
2768 if (err)
2769 break;
2771 s->first_displayed_line = 1;
2772 s->last_displayed_line = view->nlines;
2774 diff_view_indicate_progress(view);
2775 err = create_diff(s);
2776 break;
2777 case '>':
2778 case '.':
2779 if (s->log_view == NULL)
2780 break;
2781 ls = &s->log_view->state.log;
2783 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2784 ls->thread_args.commits_needed++;
2786 /* Display "loading..." in log view. */
2787 show_log_view(s->log_view);
2788 update_panels();
2789 doupdate();
2791 err = trigger_log_thread(1 /* load_all */,
2792 &ls->thread_args.commits_needed,
2793 &ls->thread_args.log_complete,
2794 &ls->thread_args.need_commits);
2795 if (err)
2796 break;
2798 err = input_log_view(NULL, NULL, NULL, s->log_view,
2799 KEY_DOWN);
2800 if (err)
2801 break;
2803 entry = TAILQ_NEXT(ls->selected_entry, entry);
2804 if (entry == NULL)
2805 break;
2807 err = set_selected_commit(s, entry);
2808 if (err)
2809 break;
2811 s->first_displayed_line = 1;
2812 s->last_displayed_line = view->nlines;
2814 diff_view_indicate_progress(view);
2815 err = create_diff(s);
2816 break;
2817 default:
2818 break;
2821 return err;
2824 static const struct got_error *
2825 cmd_diff(int argc, char *argv[])
2827 const struct got_error *error = NULL;
2828 struct got_repository *repo = NULL;
2829 struct got_reflist_head refs;
2830 struct got_object_id *id1 = NULL, *id2 = NULL;
2831 char *repo_path = NULL;
2832 char *id_str1 = NULL, *id_str2 = NULL;
2833 int ch;
2834 struct tog_view *view;
2836 SIMPLEQ_INIT(&refs);
2838 #ifndef PROFILE
2839 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2840 NULL) == -1)
2841 err(1, "pledge");
2842 #endif
2844 while ((ch = getopt(argc, argv, "")) != -1) {
2845 switch (ch) {
2846 default:
2847 usage_diff();
2848 /* NOTREACHED */
2852 argc -= optind;
2853 argv += optind;
2855 if (argc == 0) {
2856 usage_diff(); /* TODO show local worktree changes */
2857 } else if (argc == 2) {
2858 repo_path = getcwd(NULL, 0);
2859 if (repo_path == NULL)
2860 return got_error_from_errno("getcwd");
2861 id_str1 = argv[0];
2862 id_str2 = argv[1];
2863 } else if (argc == 3) {
2864 repo_path = realpath(argv[0], NULL);
2865 if (repo_path == NULL)
2866 return got_error_from_errno2("realpath", argv[0]);
2867 id_str1 = argv[1];
2868 id_str2 = argv[2];
2869 } else
2870 usage_diff();
2872 init_curses();
2874 error = got_repo_open(&repo, repo_path);
2875 if (error)
2876 goto done;
2878 error = apply_unveil(got_repo_get_path(repo), NULL);
2879 if (error)
2880 goto done;
2882 error = got_repo_match_object_id_prefix(&id1, id_str1,
2883 GOT_OBJ_TYPE_ANY, repo);
2884 if (error)
2885 goto done;
2887 error = got_repo_match_object_id_prefix(&id2, id_str2,
2888 GOT_OBJ_TYPE_ANY, repo);
2889 if (error)
2890 goto done;
2892 error = got_ref_list(&refs, repo);
2893 if (error)
2894 goto done;
2896 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2897 if (view == NULL) {
2898 error = got_error_from_errno("view_open");
2899 goto done;
2901 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2902 if (error)
2903 goto done;
2904 error = view_loop(view);
2905 done:
2906 free(repo_path);
2907 if (repo)
2908 got_repo_close(repo);
2909 got_ref_list_free(&refs);
2910 return error;
2913 __dead static void
2914 usage_blame(void)
2916 endwin();
2917 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2918 getprogname());
2919 exit(1);
2922 struct tog_blame_line {
2923 int annotated;
2924 struct got_object_id *id;
2927 static const struct got_error *
2928 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2929 const char *path, struct tog_blame_line *lines, int nlines,
2930 int blame_complete, int selected_line, int *first_displayed_line,
2931 int *last_displayed_line, int *eof, int max_lines)
2933 const struct got_error *err;
2934 int lineno = 0, nprinted = 0;
2935 char *line;
2936 size_t len;
2937 wchar_t *wline;
2938 int width, wlimit;
2939 struct tog_blame_line *blame_line;
2940 struct got_object_id *prev_id = NULL;
2941 char *id_str;
2943 err = got_object_id_str(&id_str, id);
2944 if (err)
2945 return err;
2947 rewind(f);
2948 werase(view->window);
2950 if (asprintf(&line, "commit %s", id_str) == -1) {
2951 err = got_error_from_errno("asprintf");
2952 free(id_str);
2953 return err;
2956 err = format_line(&wline, &width, line, view->ncols);
2957 free(line);
2958 line = NULL;
2959 if (view_needs_focus_indication(view))
2960 wstandout(view->window);
2961 waddwstr(view->window, wline);
2962 if (view_needs_focus_indication(view))
2963 wstandend(view->window);
2964 free(wline);
2965 wline = NULL;
2966 if (width < view->ncols - 1)
2967 waddch(view->window, '\n');
2969 if (asprintf(&line, "[%d/%d] %s%s",
2970 *first_displayed_line - 1 + selected_line, nlines,
2971 blame_complete ? "" : "annotating... ", path) == -1) {
2972 free(id_str);
2973 return got_error_from_errno("asprintf");
2975 free(id_str);
2976 err = format_line(&wline, &width, line, view->ncols);
2977 free(line);
2978 line = NULL;
2979 if (err)
2980 return err;
2981 waddwstr(view->window, wline);
2982 free(wline);
2983 wline = NULL;
2984 if (width < view->ncols - 1)
2985 waddch(view->window, '\n');
2987 *eof = 0;
2988 while (nprinted < max_lines - 2) {
2989 line = parse_next_line(f, &len);
2990 if (line == NULL) {
2991 *eof = 1;
2992 break;
2994 if (++lineno < *first_displayed_line) {
2995 free(line);
2996 continue;
2999 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3000 err = format_line(&wline, &width, line, wlimit);
3001 if (err) {
3002 free(line);
3003 return err;
3006 if (view->focussed && nprinted == selected_line - 1)
3007 wstandout(view->window);
3009 if (nlines > 0) {
3010 blame_line = &lines[lineno - 1];
3011 if (blame_line->annotated && prev_id &&
3012 got_object_id_cmp(prev_id, blame_line->id) == 0)
3013 waddstr(view->window, " ");
3014 else if (blame_line->annotated) {
3015 char *id_str;
3016 err = got_object_id_str(&id_str, blame_line->id);
3017 if (err) {
3018 free(line);
3019 free(wline);
3020 return err;
3022 wprintw(view->window, "%.8s ", id_str);
3023 free(id_str);
3024 prev_id = blame_line->id;
3025 } else {
3026 waddstr(view->window, "........ ");
3027 prev_id = NULL;
3029 } else {
3030 waddstr(view->window, "........ ");
3031 prev_id = NULL;
3034 waddwstr(view->window, wline);
3035 while (width < wlimit) {
3036 waddch(view->window, ' ');
3037 width++;
3039 if (view->focussed && nprinted == selected_line - 1)
3040 wstandend(view->window);
3041 if (++nprinted == 1)
3042 *first_displayed_line = lineno;
3043 free(line);
3044 free(wline);
3045 wline = NULL;
3047 *last_displayed_line = lineno;
3049 view_vborder(view);
3051 return NULL;
3054 static const struct got_error *
3055 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3057 const struct got_error *err = NULL;
3058 struct tog_blame_cb_args *a = arg;
3059 struct tog_blame_line *line;
3060 int errcode;
3062 if (nlines != a->nlines ||
3063 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3064 return got_error(GOT_ERR_RANGE);
3066 errcode = pthread_mutex_lock(&tog_mutex);
3067 if (errcode)
3068 return got_error_set_errno(errcode, "pthread_mutex_lock");
3070 if (*a->quit) { /* user has quit the blame view */
3071 err = got_error(GOT_ERR_ITER_COMPLETED);
3072 goto done;
3075 if (lineno == -1)
3076 goto done; /* no change in this commit */
3078 line = &a->lines[lineno - 1];
3079 if (line->annotated)
3080 goto done;
3082 line->id = got_object_id_dup(id);
3083 if (line->id == NULL) {
3084 err = got_error_from_errno("got_object_id_dup");
3085 goto done;
3087 line->annotated = 1;
3088 done:
3089 errcode = pthread_mutex_unlock(&tog_mutex);
3090 if (errcode)
3091 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3092 return err;
3095 static void *
3096 blame_thread(void *arg)
3098 const struct got_error *err;
3099 struct tog_blame_thread_args *ta = arg;
3100 struct tog_blame_cb_args *a = ta->cb_args;
3101 int errcode;
3103 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3104 blame_cb, ta->cb_args);
3106 errcode = pthread_mutex_lock(&tog_mutex);
3107 if (errcode)
3108 return (void *)got_error_set_errno(errcode,
3109 "pthread_mutex_lock");
3111 got_repo_close(ta->repo);
3112 ta->repo = NULL;
3113 *ta->complete = 1;
3115 errcode = pthread_mutex_unlock(&tog_mutex);
3116 if (errcode && err == NULL)
3117 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3119 return (void *)err;
3122 static struct got_object_id *
3123 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3124 int first_displayed_line, int selected_line)
3126 struct tog_blame_line *line;
3128 if (nlines <= 0)
3129 return NULL;
3131 line = &lines[first_displayed_line - 1 + selected_line - 1];
3132 if (!line->annotated)
3133 return NULL;
3135 return line->id;
3138 static const struct got_error *
3139 stop_blame(struct tog_blame *blame)
3141 const struct got_error *err = NULL;
3142 int i;
3144 if (blame->thread) {
3145 int errcode;
3146 errcode = pthread_mutex_unlock(&tog_mutex);
3147 if (errcode)
3148 return got_error_set_errno(errcode,
3149 "pthread_mutex_unlock");
3150 errcode = pthread_join(blame->thread, (void **)&err);
3151 if (errcode)
3152 return got_error_set_errno(errcode, "pthread_join");
3153 errcode = pthread_mutex_lock(&tog_mutex);
3154 if (errcode)
3155 return got_error_set_errno(errcode,
3156 "pthread_mutex_lock");
3157 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3158 err = NULL;
3159 blame->thread = NULL;
3161 if (blame->thread_args.repo) {
3162 got_repo_close(blame->thread_args.repo);
3163 blame->thread_args.repo = NULL;
3165 if (blame->f) {
3166 if (fclose(blame->f) != 0 && err == NULL)
3167 err = got_error_from_errno("fclose");
3168 blame->f = NULL;
3170 if (blame->lines) {
3171 for (i = 0; i < blame->nlines; i++)
3172 free(blame->lines[i].id);
3173 free(blame->lines);
3174 blame->lines = NULL;
3176 free(blame->cb_args.commit_id);
3177 blame->cb_args.commit_id = NULL;
3179 return err;
3182 static const struct got_error *
3183 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3184 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3185 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3186 struct got_repository *repo)
3188 const struct got_error *err = NULL;
3189 struct got_blob_object *blob = NULL;
3190 struct got_repository *thread_repo = NULL;
3191 struct got_object_id *obj_id = NULL;
3192 int obj_type;
3194 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3195 if (err)
3196 return err;
3197 if (obj_id == NULL)
3198 return got_error(GOT_ERR_NO_OBJ);
3200 err = got_object_get_type(&obj_type, repo, obj_id);
3201 if (err)
3202 goto done;
3204 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3205 err = got_error(GOT_ERR_OBJ_TYPE);
3206 goto done;
3209 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3210 if (err)
3211 goto done;
3212 blame->f = got_opentemp();
3213 if (blame->f == NULL) {
3214 err = got_error_from_errno("got_opentemp");
3215 goto done;
3217 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3218 &blame->line_offsets, blame->f, blob);
3219 if (err)
3220 goto done;
3222 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3223 if (blame->lines == NULL) {
3224 err = got_error_from_errno("calloc");
3225 goto done;
3228 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3229 if (err)
3230 goto done;
3232 blame->cb_args.view = view;
3233 blame->cb_args.lines = blame->lines;
3234 blame->cb_args.nlines = blame->nlines;
3235 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3236 if (blame->cb_args.commit_id == NULL) {
3237 err = got_error_from_errno("got_object_id_dup");
3238 goto done;
3240 blame->cb_args.quit = done;
3242 blame->thread_args.path = path;
3243 blame->thread_args.repo = thread_repo;
3244 blame->thread_args.cb_args = &blame->cb_args;
3245 blame->thread_args.complete = blame_complete;
3246 *blame_complete = 0;
3248 done:
3249 if (blob)
3250 got_object_blob_close(blob);
3251 free(obj_id);
3252 if (err)
3253 stop_blame(blame);
3254 return err;
3257 static const struct got_error *
3258 open_blame_view(struct tog_view *view, char *path,
3259 struct got_object_id *commit_id, struct got_reflist_head *refs,
3260 struct got_repository *repo)
3262 const struct got_error *err = NULL;
3263 struct tog_blame_view_state *s = &view->state.blame;
3265 SIMPLEQ_INIT(&s->blamed_commits);
3267 s->path = strdup(path);
3268 if (s->path == NULL)
3269 return got_error_from_errno("strdup");
3271 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3272 if (err) {
3273 free(s->path);
3274 return err;
3277 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3278 s->first_displayed_line = 1;
3279 s->last_displayed_line = view->nlines;
3280 s->selected_line = 1;
3281 s->blame_complete = 0;
3282 s->repo = repo;
3283 s->refs = refs;
3284 s->commit_id = commit_id;
3285 memset(&s->blame, 0, sizeof(s->blame));
3287 view->show = show_blame_view;
3288 view->input = input_blame_view;
3289 view->close = close_blame_view;
3290 view->search_start = search_start_blame_view;
3291 view->search_next = search_next_blame_view;
3293 return run_blame(&s->blame, view, &s->blame_complete,
3294 &s->first_displayed_line, &s->last_displayed_line,
3295 &s->selected_line, &s->done, &s->eof, s->path,
3296 s->blamed_commit->id, s->repo);
3299 static const struct got_error *
3300 close_blame_view(struct tog_view *view)
3302 const struct got_error *err = NULL;
3303 struct tog_blame_view_state *s = &view->state.blame;
3305 if (s->blame.thread)
3306 err = stop_blame(&s->blame);
3308 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3309 struct got_object_qid *blamed_commit;
3310 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3311 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3312 got_object_qid_free(blamed_commit);
3315 free(s->path);
3317 return err;
3320 static const struct got_error *
3321 search_start_blame_view(struct tog_view *view)
3323 struct tog_blame_view_state *s = &view->state.blame;
3325 s->matched_line = 0;
3326 return NULL;
3329 static int
3330 match_line(const char *line, regex_t *regex)
3332 regmatch_t regmatch;
3334 return regexec(regex, line, 1, &regmatch, 0) == 0;
3338 static const struct got_error *
3339 search_next_blame_view(struct tog_view *view)
3341 struct tog_blame_view_state *s = &view->state.blame;
3342 int lineno;
3344 if (!view->searching) {
3345 view->search_next_done = 1;
3346 return NULL;
3349 if (s->matched_line) {
3350 if (view->searching == TOG_SEARCH_FORWARD)
3351 lineno = s->matched_line + 1;
3352 else
3353 lineno = s->matched_line - 1;
3354 } else {
3355 if (view->searching == TOG_SEARCH_FORWARD)
3356 lineno = 1;
3357 else
3358 lineno = s->blame.nlines;
3361 while (1) {
3362 char *line = NULL;
3363 off_t offset;
3364 size_t len;
3366 if (lineno <= 0 || lineno > s->blame.nlines) {
3367 if (s->matched_line == 0) {
3368 view->search_next_done = 1;
3369 free(line);
3370 break;
3373 if (view->searching == TOG_SEARCH_FORWARD)
3374 lineno = 1;
3375 else
3376 lineno = s->blame.nlines;
3379 offset = s->blame.line_offsets[lineno - 1];
3380 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3381 free(line);
3382 return got_error_from_errno("fseeko");
3384 free(line);
3385 line = parse_next_line(s->blame.f, &len);
3386 if (line && match_line(line, &view->regex)) {
3387 view->search_next_done = 1;
3388 s->matched_line = lineno;
3389 free(line);
3390 break;
3392 free(line);
3393 if (view->searching == TOG_SEARCH_FORWARD)
3394 lineno++;
3395 else
3396 lineno--;
3399 if (s->matched_line) {
3400 s->first_displayed_line = s->matched_line;
3401 s->selected_line = 1;
3404 return NULL;
3407 static const struct got_error *
3408 show_blame_view(struct tog_view *view)
3410 const struct got_error *err = NULL;
3411 struct tog_blame_view_state *s = &view->state.blame;
3412 int errcode;
3414 if (s->blame.thread == NULL) {
3415 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3416 &s->blame.thread_args);
3417 if (errcode)
3418 return got_error_set_errno(errcode, "pthread_create");
3421 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3422 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3423 s->selected_line, &s->first_displayed_line,
3424 &s->last_displayed_line, &s->eof, view->nlines);
3426 view_vborder(view);
3427 return err;
3430 static const struct got_error *
3431 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3432 struct tog_view **focus_view, struct tog_view *view, int ch)
3434 const struct got_error *err = NULL, *thread_err = NULL;
3435 struct tog_view *diff_view;
3436 struct tog_blame_view_state *s = &view->state.blame;
3437 int begin_x = 0;
3439 switch (ch) {
3440 case 'q':
3441 s->done = 1;
3442 break;
3443 case 'k':
3444 case KEY_UP:
3445 if (s->selected_line > 1)
3446 s->selected_line--;
3447 else if (s->selected_line == 1 &&
3448 s->first_displayed_line > 1)
3449 s->first_displayed_line--;
3450 break;
3451 case KEY_PPAGE:
3452 if (s->first_displayed_line == 1) {
3453 s->selected_line = 1;
3454 break;
3456 if (s->first_displayed_line > view->nlines - 2)
3457 s->first_displayed_line -=
3458 (view->nlines - 2);
3459 else
3460 s->first_displayed_line = 1;
3461 break;
3462 case 'j':
3463 case KEY_DOWN:
3464 if (s->selected_line < view->nlines - 2 &&
3465 s->first_displayed_line +
3466 s->selected_line <= s->blame.nlines)
3467 s->selected_line++;
3468 else if (s->last_displayed_line <
3469 s->blame.nlines)
3470 s->first_displayed_line++;
3471 break;
3472 case 'b':
3473 case 'p': {
3474 struct got_object_id *id = NULL;
3475 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3476 s->first_displayed_line, s->selected_line);
3477 if (id == NULL)
3478 break;
3479 if (ch == 'p') {
3480 struct got_commit_object *commit;
3481 struct got_object_qid *pid;
3482 struct got_object_id *blob_id = NULL;
3483 int obj_type;
3484 err = got_object_open_as_commit(&commit,
3485 s->repo, id);
3486 if (err)
3487 break;
3488 pid = SIMPLEQ_FIRST(
3489 got_object_commit_get_parent_ids(commit));
3490 if (pid == NULL) {
3491 got_object_commit_close(commit);
3492 break;
3494 /* Check if path history ends here. */
3495 err = got_object_id_by_path(&blob_id, s->repo,
3496 pid->id, s->path);
3497 if (err) {
3498 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3499 err = NULL;
3500 got_object_commit_close(commit);
3501 break;
3503 err = got_object_get_type(&obj_type, s->repo,
3504 blob_id);
3505 free(blob_id);
3506 /* Can't blame non-blob type objects. */
3507 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3508 got_object_commit_close(commit);
3509 break;
3511 err = got_object_qid_alloc(&s->blamed_commit,
3512 pid->id);
3513 got_object_commit_close(commit);
3514 } else {
3515 if (got_object_id_cmp(id,
3516 s->blamed_commit->id) == 0)
3517 break;
3518 err = got_object_qid_alloc(&s->blamed_commit,
3519 id);
3521 if (err)
3522 break;
3523 s->done = 1;
3524 thread_err = stop_blame(&s->blame);
3525 s->done = 0;
3526 if (thread_err)
3527 break;
3528 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3529 s->blamed_commit, entry);
3530 err = run_blame(&s->blame, view, &s->blame_complete,
3531 &s->first_displayed_line, &s->last_displayed_line,
3532 &s->selected_line, &s->done, &s->eof,
3533 s->path, s->blamed_commit->id, s->repo);
3534 if (err)
3535 break;
3536 break;
3538 case 'B': {
3539 struct got_object_qid *first;
3540 first = SIMPLEQ_FIRST(&s->blamed_commits);
3541 if (!got_object_id_cmp(first->id, s->commit_id))
3542 break;
3543 s->done = 1;
3544 thread_err = stop_blame(&s->blame);
3545 s->done = 0;
3546 if (thread_err)
3547 break;
3548 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3549 got_object_qid_free(s->blamed_commit);
3550 s->blamed_commit =
3551 SIMPLEQ_FIRST(&s->blamed_commits);
3552 err = run_blame(&s->blame, view, &s->blame_complete,
3553 &s->first_displayed_line, &s->last_displayed_line,
3554 &s->selected_line, &s->done, &s->eof, s->path,
3555 s->blamed_commit->id, s->repo);
3556 if (err)
3557 break;
3558 break;
3560 case KEY_ENTER:
3561 case '\r': {
3562 struct got_object_id *id = NULL;
3563 struct got_object_qid *pid;
3564 struct got_commit_object *commit = NULL;
3565 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3566 s->first_displayed_line, s->selected_line);
3567 if (id == NULL)
3568 break;
3569 err = got_object_open_as_commit(&commit, s->repo, id);
3570 if (err)
3571 break;
3572 pid = SIMPLEQ_FIRST(
3573 got_object_commit_get_parent_ids(commit));
3574 if (view_is_parent_view(view))
3575 begin_x = view_split_begin_x(view->begin_x);
3576 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3577 if (diff_view == NULL) {
3578 got_object_commit_close(commit);
3579 err = got_error_from_errno("view_open");
3580 break;
3582 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3583 id, NULL, s->refs, s->repo);
3584 got_object_commit_close(commit);
3585 if (err) {
3586 view_close(diff_view);
3587 break;
3589 if (view_is_parent_view(view)) {
3590 err = view_close_child(view);
3591 if (err)
3592 break;
3593 err = view_set_child(view, diff_view);
3594 if (err) {
3595 view_close(diff_view);
3596 break;
3598 *focus_view = diff_view;
3599 view->child_focussed = 1;
3600 } else
3601 *new_view = diff_view;
3602 if (err)
3603 break;
3604 break;
3606 case KEY_NPAGE:
3607 case ' ':
3608 if (s->last_displayed_line >= s->blame.nlines &&
3609 s->selected_line >= MIN(s->blame.nlines,
3610 view->nlines - 2)) {
3611 break;
3613 if (s->last_displayed_line >= s->blame.nlines &&
3614 s->selected_line < view->nlines - 2) {
3615 s->selected_line = MIN(s->blame.nlines,
3616 view->nlines - 2);
3617 break;
3619 if (s->last_displayed_line + view->nlines - 2
3620 <= s->blame.nlines)
3621 s->first_displayed_line +=
3622 view->nlines - 2;
3623 else
3624 s->first_displayed_line =
3625 s->blame.nlines -
3626 (view->nlines - 3);
3627 break;
3628 case KEY_RESIZE:
3629 if (s->selected_line > view->nlines - 2) {
3630 s->selected_line = MIN(s->blame.nlines,
3631 view->nlines - 2);
3633 break;
3634 default:
3635 break;
3637 return thread_err ? thread_err : err;
3640 static const struct got_error *
3641 cmd_blame(int argc, char *argv[])
3643 const struct got_error *error;
3644 struct got_repository *repo = NULL;
3645 struct got_reflist_head refs;
3646 struct got_worktree *worktree = NULL;
3647 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3648 struct got_object_id *commit_id = NULL;
3649 char *commit_id_str = NULL;
3650 int ch;
3651 struct tog_view *view;
3653 SIMPLEQ_INIT(&refs);
3655 #ifndef PROFILE
3656 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3657 NULL) == -1)
3658 err(1, "pledge");
3659 #endif
3661 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3662 switch (ch) {
3663 case 'c':
3664 commit_id_str = optarg;
3665 break;
3666 case 'r':
3667 repo_path = realpath(optarg, NULL);
3668 if (repo_path == NULL)
3669 err(1, "-r option");
3670 break;
3671 default:
3672 usage_blame();
3673 /* NOTREACHED */
3677 argc -= optind;
3678 argv += optind;
3680 if (argc == 1)
3681 path = argv[0];
3682 else
3683 usage_blame();
3685 cwd = getcwd(NULL, 0);
3686 if (cwd == NULL) {
3687 error = got_error_from_errno("getcwd");
3688 goto done;
3690 if (repo_path == NULL) {
3691 error = got_worktree_open(&worktree, cwd);
3692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3693 goto done;
3694 else
3695 error = NULL;
3696 if (worktree) {
3697 repo_path =
3698 strdup(got_worktree_get_repo_path(worktree));
3699 if (repo_path == NULL)
3700 error = got_error_from_errno("strdup");
3701 if (error)
3702 goto done;
3703 } else {
3704 repo_path = strdup(cwd);
3705 if (repo_path == NULL) {
3706 error = got_error_from_errno("strdup");
3707 goto done;
3712 init_curses();
3714 error = got_repo_open(&repo, repo_path);
3715 if (error != NULL)
3716 goto done;
3718 error = apply_unveil(got_repo_get_path(repo), NULL);
3719 if (error)
3720 goto done;
3722 if (worktree) {
3723 const char *prefix = got_worktree_get_path_prefix(worktree);
3724 char *p, *worktree_subdir = cwd +
3725 strlen(got_worktree_get_root_path(worktree));
3726 if (asprintf(&p, "%s%s%s%s%s",
3727 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3728 worktree_subdir, worktree_subdir[0] ? "/" : "",
3729 path) == -1) {
3730 error = got_error_from_errno("asprintf");
3731 goto done;
3733 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3734 free(p);
3735 } else {
3736 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3738 if (error)
3739 goto done;
3741 if (commit_id_str == NULL) {
3742 struct got_reference *head_ref;
3743 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3744 if (error != NULL)
3745 goto done;
3746 error = got_ref_resolve(&commit_id, repo, head_ref);
3747 got_ref_close(head_ref);
3748 } else {
3749 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3750 if (error) {
3751 if (error->code != GOT_ERR_NOT_REF)
3752 goto done;
3753 error = got_repo_match_object_id_prefix(&commit_id,
3754 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3757 if (error != NULL)
3758 goto done;
3760 error = got_ref_list(&refs, repo);
3761 if (error)
3762 goto done;
3764 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3765 if (view == NULL) {
3766 error = got_error_from_errno("view_open");
3767 goto done;
3769 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3770 if (error)
3771 goto done;
3772 error = view_loop(view);
3773 done:
3774 free(repo_path);
3775 free(cwd);
3776 free(commit_id);
3777 if (worktree)
3778 got_worktree_close(worktree);
3779 if (repo)
3780 got_repo_close(repo);
3781 got_ref_list_free(&refs);
3782 return error;
3785 static const struct got_error *
3786 draw_tree_entries(struct tog_view *view,
3787 struct got_tree_entry **first_displayed_entry,
3788 struct got_tree_entry **last_displayed_entry,
3789 struct got_tree_entry **selected_entry, int *ndisplayed,
3790 const char *label, int show_ids, const char *parent_path,
3791 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3793 const struct got_error *err = NULL;
3794 struct got_tree_entry *te;
3795 wchar_t *wline;
3796 int width, n;
3798 *ndisplayed = 0;
3800 werase(view->window);
3802 if (limit == 0)
3803 return NULL;
3805 err = format_line(&wline, &width, label, view->ncols);
3806 if (err)
3807 return err;
3808 if (view_needs_focus_indication(view))
3809 wstandout(view->window);
3810 waddwstr(view->window, wline);
3811 if (view_needs_focus_indication(view))
3812 wstandend(view->window);
3813 free(wline);
3814 wline = NULL;
3815 if (width < view->ncols - 1)
3816 waddch(view->window, '\n');
3817 if (--limit <= 0)
3818 return NULL;
3819 err = format_line(&wline, &width, parent_path, view->ncols);
3820 if (err)
3821 return err;
3822 waddwstr(view->window, wline);
3823 free(wline);
3824 wline = NULL;
3825 if (width < view->ncols - 1)
3826 waddch(view->window, '\n');
3827 if (--limit <= 0)
3828 return NULL;
3829 waddch(view->window, '\n');
3830 if (--limit <= 0)
3831 return NULL;
3833 te = SIMPLEQ_FIRST(&entries->head);
3834 if (*first_displayed_entry == NULL) {
3835 if (selected == 0) {
3836 if (view->focussed)
3837 wstandout(view->window);
3838 *selected_entry = NULL;
3840 waddstr(view->window, " ..\n"); /* parent directory */
3841 if (selected == 0 && view->focussed)
3842 wstandend(view->window);
3843 (*ndisplayed)++;
3844 if (--limit <= 0)
3845 return NULL;
3846 n = 1;
3847 } else {
3848 n = 0;
3849 while (te != *first_displayed_entry)
3850 te = SIMPLEQ_NEXT(te, entry);
3853 while (te) {
3854 char *line = NULL, *id_str = NULL;
3856 if (show_ids) {
3857 err = got_object_id_str(&id_str, te->id);
3858 if (err)
3859 return got_error_from_errno(
3860 "got_object_id_str");
3862 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3863 te->name, S_ISDIR(te->mode) ? "/" :
3864 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3865 free(id_str);
3866 return got_error_from_errno("asprintf");
3868 free(id_str);
3869 err = format_line(&wline, &width, line, view->ncols);
3870 if (err) {
3871 free(line);
3872 break;
3874 if (n == selected) {
3875 if (view->focussed)
3876 wstandout(view->window);
3877 *selected_entry = te;
3879 waddwstr(view->window, wline);
3880 if (width < view->ncols - 1)
3881 waddch(view->window, '\n');
3882 if (n == selected && view->focussed)
3883 wstandend(view->window);
3884 free(line);
3885 free(wline);
3886 wline = NULL;
3887 n++;
3888 (*ndisplayed)++;
3889 *last_displayed_entry = te;
3890 if (--limit <= 0)
3891 break;
3892 te = SIMPLEQ_NEXT(te, entry);
3895 return err;
3898 static void
3899 tree_scroll_up(struct tog_view *view,
3900 struct got_tree_entry **first_displayed_entry, int maxscroll,
3901 const struct got_tree_entries *entries, int isroot)
3903 struct got_tree_entry *te, *prev;
3904 int i;
3906 if (*first_displayed_entry == NULL)
3907 return;
3909 te = SIMPLEQ_FIRST(&entries->head);
3910 if (*first_displayed_entry == te) {
3911 if (!isroot)
3912 *first_displayed_entry = NULL;
3913 return;
3916 /* XXX this is stupid... switch to TAILQ? */
3917 for (i = 0; i < maxscroll; i++) {
3918 while (te != *first_displayed_entry) {
3919 prev = te;
3920 te = SIMPLEQ_NEXT(te, entry);
3922 *first_displayed_entry = prev;
3923 te = SIMPLEQ_FIRST(&entries->head);
3925 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3926 *first_displayed_entry = NULL;
3929 static int
3930 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3931 struct got_tree_entry *last_displayed_entry,
3932 const struct got_tree_entries *entries)
3934 struct got_tree_entry *next, *last;
3935 int n = 0;
3937 if (*first_displayed_entry)
3938 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3939 else
3940 next = SIMPLEQ_FIRST(&entries->head);
3941 last = last_displayed_entry;
3942 while (next && last && n++ < maxscroll) {
3943 last = SIMPLEQ_NEXT(last, entry);
3944 if (last) {
3945 *first_displayed_entry = next;
3946 next = SIMPLEQ_NEXT(next, entry);
3949 return n;
3952 static const struct got_error *
3953 tree_entry_path(char **path, struct tog_parent_trees *parents,
3954 struct got_tree_entry *te)
3956 const struct got_error *err = NULL;
3957 struct tog_parent_tree *pt;
3958 size_t len = 2; /* for leading slash and NUL */
3960 TAILQ_FOREACH(pt, parents, entry)
3961 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3962 if (te)
3963 len += strlen(te->name);
3965 *path = calloc(1, len);
3966 if (path == NULL)
3967 return got_error_from_errno("calloc");
3969 (*path)[0] = '/';
3970 pt = TAILQ_LAST(parents, tog_parent_trees);
3971 while (pt) {
3972 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3973 err = got_error(GOT_ERR_NO_SPACE);
3974 goto done;
3976 if (strlcat(*path, "/", len) >= len) {
3977 err = got_error(GOT_ERR_NO_SPACE);
3978 goto done;
3980 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3982 if (te) {
3983 if (strlcat(*path, te->name, len) >= len) {
3984 err = got_error(GOT_ERR_NO_SPACE);
3985 goto done;
3988 done:
3989 if (err) {
3990 free(*path);
3991 *path = NULL;
3993 return err;
3996 static const struct got_error *
3997 blame_tree_entry(struct tog_view **new_view, int begin_x,
3998 struct got_tree_entry *te, struct tog_parent_trees *parents,
3999 struct got_object_id *commit_id, struct got_reflist_head *refs,
4000 struct got_repository *repo)
4002 const struct got_error *err = NULL;
4003 char *path;
4004 struct tog_view *blame_view;
4006 *new_view = NULL;
4008 err = tree_entry_path(&path, parents, te);
4009 if (err)
4010 return err;
4012 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4013 if (blame_view == NULL) {
4014 err = got_error_from_errno("view_open");
4015 goto done;
4018 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4019 if (err) {
4020 view_close(blame_view);
4021 } else
4022 *new_view = blame_view;
4023 done:
4024 free(path);
4025 return err;
4028 static const struct got_error *
4029 log_tree_entry(struct tog_view **new_view, int begin_x,
4030 struct got_tree_entry *te, struct tog_parent_trees *parents,
4031 struct got_object_id *commit_id, struct got_reflist_head *refs,
4032 struct got_repository *repo)
4034 struct tog_view *log_view;
4035 const struct got_error *err = NULL;
4036 char *path;
4038 *new_view = NULL;
4040 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4041 if (log_view == NULL)
4042 return got_error_from_errno("view_open");
4044 err = tree_entry_path(&path, parents, te);
4045 if (err)
4046 return err;
4048 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4049 if (err)
4050 view_close(log_view);
4051 else
4052 *new_view = log_view;
4053 free(path);
4054 return err;
4057 static const struct got_error *
4058 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4059 struct got_object_id *commit_id, struct got_reflist_head *refs,
4060 struct got_repository *repo)
4062 const struct got_error *err = NULL;
4063 char *commit_id_str = NULL;
4064 struct tog_tree_view_state *s = &view->state.tree;
4066 TAILQ_INIT(&s->parents);
4068 err = got_object_id_str(&commit_id_str, commit_id);
4069 if (err != NULL)
4070 goto done;
4072 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4073 err = got_error_from_errno("asprintf");
4074 goto done;
4077 s->root = s->tree = root;
4078 s->entries = got_object_tree_get_entries(root);
4079 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4080 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4081 s->commit_id = got_object_id_dup(commit_id);
4082 if (s->commit_id == NULL) {
4083 err = got_error_from_errno("got_object_id_dup");
4084 goto done;
4086 s->refs = refs;
4087 s->repo = repo;
4089 view->show = show_tree_view;
4090 view->input = input_tree_view;
4091 view->close = close_tree_view;
4092 view->search_start = search_start_tree_view;
4093 view->search_next = search_next_tree_view;
4094 done:
4095 free(commit_id_str);
4096 if (err) {
4097 free(s->tree_label);
4098 s->tree_label = NULL;
4100 return err;
4103 static const struct got_error *
4104 close_tree_view(struct tog_view *view)
4106 struct tog_tree_view_state *s = &view->state.tree;
4108 free(s->tree_label);
4109 s->tree_label = NULL;
4110 free(s->commit_id);
4111 s->commit_id = NULL;
4112 while (!TAILQ_EMPTY(&s->parents)) {
4113 struct tog_parent_tree *parent;
4114 parent = TAILQ_FIRST(&s->parents);
4115 TAILQ_REMOVE(&s->parents, parent, entry);
4116 free(parent);
4119 if (s->tree != s->root)
4120 got_object_tree_close(s->tree);
4121 got_object_tree_close(s->root);
4123 return NULL;
4126 static const struct got_error *
4127 search_start_tree_view(struct tog_view *view)
4129 struct tog_tree_view_state *s = &view->state.tree;
4131 s->matched_entry = NULL;
4132 return NULL;
4135 static int
4136 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4138 regmatch_t regmatch;
4140 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4143 static const struct got_error *
4144 search_next_tree_view(struct tog_view *view)
4146 struct tog_tree_view_state *s = &view->state.tree;
4147 struct got_tree_entry *entry = NULL, *te;
4149 if (!view->searching) {
4150 view->search_next_done = 1;
4151 return NULL;
4154 if (s->matched_entry) {
4155 if (view->searching == TOG_SEARCH_FORWARD) {
4156 if (s->selected_entry)
4157 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4158 else
4159 entry = SIMPLEQ_FIRST(&s->entries->head);
4161 else {
4162 if (s->selected_entry == NULL) {
4163 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4164 entry = te;
4165 } else {
4166 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4167 entry = te;
4168 if (SIMPLEQ_NEXT(te, entry) ==
4169 s->selected_entry)
4170 break;
4174 } else {
4175 if (view->searching == TOG_SEARCH_FORWARD)
4176 entry = SIMPLEQ_FIRST(&s->entries->head);
4177 else {
4178 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4179 entry = te;
4183 while (1) {
4184 if (entry == NULL) {
4185 if (s->matched_entry == NULL) {
4186 view->search_next_done = 1;
4187 return NULL;
4189 if (view->searching == TOG_SEARCH_FORWARD)
4190 entry = SIMPLEQ_FIRST(&s->entries->head);
4191 else {
4192 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4193 entry = te;
4197 if (match_tree_entry(entry, &view->regex)) {
4198 view->search_next_done = 1;
4199 s->matched_entry = entry;
4200 break;
4203 if (view->searching == TOG_SEARCH_FORWARD)
4204 entry = SIMPLEQ_NEXT(entry, entry);
4205 else {
4206 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4207 entry = NULL;
4208 else {
4209 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4210 if (SIMPLEQ_NEXT(te, entry) == entry) {
4211 entry = te;
4212 break;
4219 if (s->matched_entry) {
4220 s->first_displayed_entry = s->matched_entry;
4221 s->selected = 0;
4224 return NULL;
4227 static const struct got_error *
4228 show_tree_view(struct tog_view *view)
4230 const struct got_error *err = NULL;
4231 struct tog_tree_view_state *s = &view->state.tree;
4232 char *parent_path;
4234 err = tree_entry_path(&parent_path, &s->parents, NULL);
4235 if (err)
4236 return err;
4238 err = draw_tree_entries(view, &s->first_displayed_entry,
4239 &s->last_displayed_entry, &s->selected_entry,
4240 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4241 s->entries, s->selected, view->nlines, s->tree == s->root);
4242 free(parent_path);
4244 view_vborder(view);
4245 return err;
4248 static const struct got_error *
4249 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4250 struct tog_view **focus_view, struct tog_view *view, int ch)
4252 const struct got_error *err = NULL;
4253 struct tog_tree_view_state *s = &view->state.tree;
4254 struct tog_view *log_view;
4255 int begin_x = 0, nscrolled;
4257 switch (ch) {
4258 case 'i':
4259 s->show_ids = !s->show_ids;
4260 break;
4261 case 'l':
4262 if (!s->selected_entry)
4263 break;
4264 if (view_is_parent_view(view))
4265 begin_x = view_split_begin_x(view->begin_x);
4266 err = log_tree_entry(&log_view, begin_x,
4267 s->selected_entry, &s->parents,
4268 s->commit_id, s->refs, s->repo);
4269 if (view_is_parent_view(view)) {
4270 err = view_close_child(view);
4271 if (err)
4272 return err;
4273 err = view_set_child(view, log_view);
4274 if (err) {
4275 view_close(log_view);
4276 break;
4278 *focus_view = log_view;
4279 view->child_focussed = 1;
4280 } else
4281 *new_view = log_view;
4282 break;
4283 case 'k':
4284 case KEY_UP:
4285 if (s->selected > 0) {
4286 s->selected--;
4287 if (s->selected == 0)
4288 break;
4290 if (s->selected > 0)
4291 break;
4292 tree_scroll_up(view, &s->first_displayed_entry, 1,
4293 s->entries, s->tree == s->root);
4294 break;
4295 case KEY_PPAGE:
4296 tree_scroll_up(view, &s->first_displayed_entry,
4297 MAX(0, view->nlines - 4 - s->selected), s->entries,
4298 s->tree == s->root);
4299 s->selected = 0;
4300 if (SIMPLEQ_FIRST(&s->entries->head) ==
4301 s->first_displayed_entry && s->tree != s->root)
4302 s->first_displayed_entry = NULL;
4303 break;
4304 case 'j':
4305 case KEY_DOWN:
4306 if (s->selected < s->ndisplayed - 1) {
4307 s->selected++;
4308 break;
4310 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4311 /* can't scroll any further */
4312 break;
4313 tree_scroll_down(&s->first_displayed_entry, 1,
4314 s->last_displayed_entry, s->entries);
4315 break;
4316 case KEY_NPAGE:
4317 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4318 == NULL) {
4319 /* can't scroll any further; move cursor down */
4320 if (s->selected < s->ndisplayed - 1)
4321 s->selected = s->ndisplayed - 1;
4322 break;
4324 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4325 view->nlines, s->last_displayed_entry, s->entries);
4326 if (nscrolled < view->nlines) {
4327 int ndisplayed = 0;
4328 struct got_tree_entry *te;
4329 te = s->first_displayed_entry;
4330 do {
4331 ndisplayed++;
4332 te = SIMPLEQ_NEXT(te, entry);
4333 } while (te);
4334 s->selected = ndisplayed - 1;
4336 break;
4337 case KEY_ENTER:
4338 case '\r':
4339 case KEY_BACKSPACE:
4340 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4341 struct tog_parent_tree *parent;
4342 /* user selected '..' */
4343 if (s->tree == s->root)
4344 break;
4345 parent = TAILQ_FIRST(&s->parents);
4346 TAILQ_REMOVE(&s->parents, parent,
4347 entry);
4348 got_object_tree_close(s->tree);
4349 s->tree = parent->tree;
4350 s->entries =
4351 got_object_tree_get_entries(s->tree);
4352 s->first_displayed_entry =
4353 parent->first_displayed_entry;
4354 s->selected_entry =
4355 parent->selected_entry;
4356 s->selected = parent->selected;
4357 free(parent);
4358 } else if (S_ISDIR(s->selected_entry->mode)) {
4359 struct got_tree_object *subtree;
4360 err = got_object_open_as_tree(&subtree,
4361 s->repo, s->selected_entry->id);
4362 if (err)
4363 break;
4364 err = tree_view_visit_subtree(subtree, s);
4365 if (err) {
4366 got_object_tree_close(subtree);
4367 break;
4369 } else if (S_ISREG(s->selected_entry->mode)) {
4370 struct tog_view *blame_view;
4371 int begin_x = view_is_parent_view(view) ?
4372 view_split_begin_x(view->begin_x) : 0;
4374 err = blame_tree_entry(&blame_view, begin_x,
4375 s->selected_entry, &s->parents,
4376 s->commit_id, s->refs, s->repo);
4377 if (err)
4378 break;
4379 if (view_is_parent_view(view)) {
4380 err = view_close_child(view);
4381 if (err)
4382 return err;
4383 err = view_set_child(view, blame_view);
4384 if (err) {
4385 view_close(blame_view);
4386 break;
4388 *focus_view = blame_view;
4389 view->child_focussed = 1;
4390 } else
4391 *new_view = blame_view;
4393 break;
4394 case KEY_RESIZE:
4395 if (s->selected > view->nlines)
4396 s->selected = s->ndisplayed - 1;
4397 break;
4398 default:
4399 break;
4402 return err;
4405 __dead static void
4406 usage_tree(void)
4408 endwin();
4409 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4410 getprogname());
4411 exit(1);
4414 static const struct got_error *
4415 cmd_tree(int argc, char *argv[])
4417 const struct got_error *error;
4418 struct got_repository *repo = NULL;
4419 struct got_reflist_head refs;
4420 char *repo_path = NULL;
4421 struct got_object_id *commit_id = NULL;
4422 char *commit_id_arg = NULL;
4423 struct got_commit_object *commit = NULL;
4424 struct got_tree_object *tree = NULL;
4425 int ch;
4426 struct tog_view *view;
4428 SIMPLEQ_INIT(&refs);
4430 #ifndef PROFILE
4431 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4432 NULL) == -1)
4433 err(1, "pledge");
4434 #endif
4436 while ((ch = getopt(argc, argv, "c:")) != -1) {
4437 switch (ch) {
4438 case 'c':
4439 commit_id_arg = optarg;
4440 break;
4441 default:
4442 usage_tree();
4443 /* NOTREACHED */
4447 argc -= optind;
4448 argv += optind;
4450 if (argc == 0) {
4451 struct got_worktree *worktree;
4452 char *cwd = getcwd(NULL, 0);
4453 if (cwd == NULL)
4454 return got_error_from_errno("getcwd");
4455 error = got_worktree_open(&worktree, cwd);
4456 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4457 goto done;
4458 if (worktree) {
4459 free(cwd);
4460 repo_path =
4461 strdup(got_worktree_get_repo_path(worktree));
4462 got_worktree_close(worktree);
4463 } else
4464 repo_path = cwd;
4465 if (repo_path == NULL) {
4466 error = got_error_from_errno("strdup");
4467 goto done;
4469 } else if (argc == 1) {
4470 repo_path = realpath(argv[0], NULL);
4471 if (repo_path == NULL)
4472 return got_error_from_errno2("realpath", argv[0]);
4473 } else
4474 usage_log();
4476 init_curses();
4478 error = got_repo_open(&repo, repo_path);
4479 if (error != NULL)
4480 goto done;
4482 error = apply_unveil(got_repo_get_path(repo), NULL);
4483 if (error)
4484 goto done;
4486 if (commit_id_arg == NULL)
4487 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4488 else {
4489 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4490 if (error) {
4491 if (error->code != GOT_ERR_NOT_REF)
4492 goto done;
4493 error = got_repo_match_object_id_prefix(&commit_id,
4494 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4497 if (error != NULL)
4498 goto done;
4500 error = got_object_open_as_commit(&commit, repo, commit_id);
4501 if (error != NULL)
4502 goto done;
4504 error = got_object_open_as_tree(&tree, repo,
4505 got_object_commit_get_tree_id(commit));
4506 if (error != NULL)
4507 goto done;
4509 error = got_ref_list(&refs, repo);
4510 if (error)
4511 goto done;
4513 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4514 if (view == NULL) {
4515 error = got_error_from_errno("view_open");
4516 goto done;
4518 error = open_tree_view(view, tree, commit_id, &refs, repo);
4519 if (error)
4520 goto done;
4521 error = view_loop(view);
4522 done:
4523 free(repo_path);
4524 free(commit_id);
4525 if (commit)
4526 got_object_commit_close(commit);
4527 if (tree)
4528 got_object_tree_close(tree);
4529 if (repo)
4530 got_repo_close(repo);
4531 got_ref_list_free(&refs);
4532 return error;
4535 static void
4536 list_commands(void)
4538 int i;
4540 fprintf(stderr, "commands:");
4541 for (i = 0; i < nitems(tog_commands); i++) {
4542 struct tog_cmd *cmd = &tog_commands[i];
4543 fprintf(stderr, " %s", cmd->name);
4545 fputc('\n', stderr);
4548 __dead static void
4549 usage(int hflag)
4551 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4552 getprogname());
4553 if (hflag)
4554 list_commands();
4555 exit(1);
4558 static char **
4559 make_argv(const char *arg0, const char *arg1)
4561 char **argv;
4562 int argc = (arg1 == NULL ? 1 : 2);
4564 argv = calloc(argc, sizeof(char *));
4565 if (argv == NULL)
4566 err(1, "calloc");
4567 argv[0] = strdup(arg0);
4568 if (argv[0] == NULL)
4569 err(1, "calloc");
4570 if (arg1) {
4571 argv[1] = strdup(arg1);
4572 if (argv[1] == NULL)
4573 err(1, "calloc");
4576 return argv;
4579 int
4580 main(int argc, char *argv[])
4582 const struct got_error *error = NULL;
4583 struct tog_cmd *cmd = NULL;
4584 int ch, hflag = 0, Vflag = 0;
4585 char **cmd_argv = NULL;
4587 setlocale(LC_CTYPE, "");
4589 while ((ch = getopt(argc, argv, "hV")) != -1) {
4590 switch (ch) {
4591 case 'h':
4592 hflag = 1;
4593 break;
4594 case 'V':
4595 Vflag = 1;
4596 break;
4597 default:
4598 usage(hflag);
4599 /* NOTREACHED */
4603 argc -= optind;
4604 argv += optind;
4605 optind = 0;
4606 optreset = 1;
4608 if (Vflag) {
4609 got_version_print_str();
4610 return 1;
4613 if (argc == 0) {
4614 if (hflag)
4615 usage(hflag);
4616 /* Build an argument vector which runs a default command. */
4617 cmd = &tog_commands[0];
4618 cmd_argv = make_argv(cmd->name, NULL);
4619 argc = 1;
4620 } else {
4621 int i;
4623 /* Did the user specific a command? */
4624 for (i = 0; i < nitems(tog_commands); i++) {
4625 if (strncmp(tog_commands[i].name, argv[0],
4626 strlen(argv[0])) == 0) {
4627 cmd = &tog_commands[i];
4628 break;
4632 if (cmd == NULL) {
4633 fprintf(stderr, "%s: unknown command '%s'\n",
4634 getprogname(), argv[0]);
4635 list_commands();
4636 return 1;
4640 if (hflag)
4641 cmd->cmd_usage();
4642 else
4643 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4645 endwin();
4646 free(cmd_argv);
4647 if (error)
4648 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4649 return 0;