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, struct got_repository *repo)
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 struct got_tag_object *tag = NULL;
986 int cmp;
988 name = got_ref_get_name(re->ref);
989 if (strcmp(name, GOT_REF_HEAD) == 0)
990 continue;
991 if (strncmp(name, "refs/", 5) == 0)
992 name += 5;
993 if (strncmp(name, "got/", 4) == 0)
994 continue;
995 if (strncmp(name, "heads/", 6) == 0)
996 name += 6;
997 if (strncmp(name, "remotes/", 8) == 0)
998 name += 8;
999 if (strncmp(name, "tags/", 5) == 0) {
1000 err = got_object_open_as_tag(&tag, repo, re->id);
1001 if (err)
1002 break;
1004 cmp = got_object_id_cmp(tag ?
1005 got_object_tag_get_object_id(tag) : re->id, id);
1006 if (tag)
1007 got_object_tag_close(tag);
1008 if (cmp != 0)
1009 continue;
1010 s = *refs_str;
1011 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1012 s ? ", " : "", name) == -1) {
1013 err = got_error_from_errno("asprintf");
1014 free(s);
1015 *refs_str = NULL;
1016 break;
1018 free(s);
1021 return err;
1024 static const struct got_error *
1025 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1027 char *smallerthan, *at;
1029 smallerthan = strchr(author, '<');
1030 if (smallerthan && smallerthan[1] != '\0')
1031 author = smallerthan + 1;
1032 at = strchr(author, '@');
1033 if (at)
1034 *at = '\0';
1035 return format_line(wauthor, author_width, author, limit);
1038 static const struct got_error *
1039 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1040 struct got_object_id *id, struct got_reflist_head *refs,
1041 int author_display_cols)
1043 const struct got_error *err = NULL;
1044 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1045 char *logmsg0 = NULL, *logmsg = NULL;
1046 char *author = NULL;
1047 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1048 int author_width, logmsg_width;
1049 char *newline, *line = NULL;
1050 int col, limit;
1051 static const size_t date_display_cols = 9;
1052 const int avail = view->ncols;
1053 struct tm tm;
1054 time_t committer_time;
1056 committer_time = got_object_commit_get_committer_time(commit);
1057 if (localtime_r(&committer_time, &tm) == NULL)
1058 return got_error_from_errno("localtime_r");
1059 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1060 >= sizeof(datebuf))
1061 return got_error(GOT_ERR_NO_SPACE);
1063 if (avail < date_display_cols)
1064 limit = MIN(sizeof(datebuf) - 1, avail);
1065 else
1066 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1067 waddnstr(view->window, datebuf, limit);
1068 col = limit + 1;
1069 if (col > avail)
1070 goto done;
1072 author = strdup(got_object_commit_get_author(commit));
1073 if (author == NULL) {
1074 err = got_error_from_errno("strdup");
1075 goto done;
1077 err = format_author(&wauthor, &author_width, author, avail - col);
1078 if (err)
1079 goto done;
1080 waddwstr(view->window, wauthor);
1081 col += author_width;
1082 while (col <= avail && author_width < author_display_cols + 2) {
1083 waddch(view->window, ' ');
1084 col++;
1085 author_width++;
1087 if (col > avail)
1088 goto done;
1090 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1091 if (logmsg0 == NULL) {
1092 err = got_error_from_errno("strdup");
1093 goto done;
1095 logmsg = logmsg0;
1096 while (*logmsg == '\n')
1097 logmsg++;
1098 newline = strchr(logmsg, '\n');
1099 if (newline)
1100 *newline = '\0';
1101 limit = avail - col;
1102 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1103 if (err)
1104 goto done;
1105 waddwstr(view->window, wlogmsg);
1106 col += logmsg_width;
1107 while (col <= avail) {
1108 waddch(view->window, ' ');
1109 col++;
1111 done:
1112 free(logmsg0);
1113 free(wlogmsg);
1114 free(author);
1115 free(wauthor);
1116 free(line);
1117 return err;
1120 static struct commit_queue_entry *
1121 alloc_commit_queue_entry(struct got_commit_object *commit,
1122 struct got_object_id *id)
1124 struct commit_queue_entry *entry;
1126 entry = calloc(1, sizeof(*entry));
1127 if (entry == NULL)
1128 return NULL;
1130 entry->id = id;
1131 entry->commit = commit;
1132 return entry;
1135 static void
1136 pop_commit(struct commit_queue *commits)
1138 struct commit_queue_entry *entry;
1140 entry = TAILQ_FIRST(&commits->head);
1141 TAILQ_REMOVE(&commits->head, entry, entry);
1142 got_object_commit_close(entry->commit);
1143 commits->ncommits--;
1144 /* Don't free entry->id! It is owned by the commit graph. */
1145 free(entry);
1148 static void
1149 free_commits(struct commit_queue *commits)
1151 while (!TAILQ_EMPTY(&commits->head))
1152 pop_commit(commits);
1155 static const struct got_error *
1156 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1157 int minqueue, struct got_repository *repo, const char *path)
1159 const struct got_error *err = NULL;
1160 int nqueued = 0;
1163 * We keep all commits open throughout the lifetime of the log
1164 * view in order to avoid having to re-fetch commits from disk
1165 * while updating the display.
1167 while (nqueued < minqueue) {
1168 struct got_object_id *id;
1169 struct got_commit_object *commit;
1170 struct commit_queue_entry *entry;
1171 int errcode;
1173 err = got_commit_graph_iter_next(&id, graph);
1174 if (err) {
1175 if (err->code != GOT_ERR_ITER_NEED_MORE)
1176 break;
1177 err = got_commit_graph_fetch_commits(graph,
1178 minqueue, repo);
1179 if (err)
1180 return err;
1181 continue;
1184 if (id == NULL)
1185 break;
1187 err = got_object_open_as_commit(&commit, repo, id);
1188 if (err)
1189 break;
1190 entry = alloc_commit_queue_entry(commit, id);
1191 if (entry == NULL) {
1192 err = got_error_from_errno("alloc_commit_queue_entry");
1193 break;
1196 errcode = pthread_mutex_lock(&tog_mutex);
1197 if (errcode) {
1198 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1199 break;
1202 entry->idx = commits->ncommits;
1203 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1204 nqueued++;
1205 commits->ncommits++;
1207 errcode = pthread_mutex_unlock(&tog_mutex);
1208 if (errcode && err == NULL)
1209 err = got_error_set_errno(errcode,
1210 "pthread_mutex_unlock");
1213 return err;
1216 static const struct got_error *
1217 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1218 struct got_repository *repo)
1220 const struct got_error *err = NULL;
1221 struct got_reference *head_ref;
1223 *head_id = NULL;
1225 err = got_ref_open(&head_ref, repo, branch_name, 0);
1226 if (err)
1227 return err;
1229 err = got_ref_resolve(head_id, repo, head_ref);
1230 got_ref_close(head_ref);
1231 if (err) {
1232 *head_id = NULL;
1233 return err;
1236 return NULL;
1239 static const struct got_error *
1240 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1241 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1242 struct commit_queue *commits, int selected_idx, int limit,
1243 struct got_reflist_head *refs, const char *path, int commits_needed)
1245 const struct got_error *err = NULL;
1246 struct tog_log_view_state *s = &view->state.log;
1247 struct commit_queue_entry *entry;
1248 int width;
1249 int ncommits, author_cols = 10;
1250 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1251 char *refs_str = NULL;
1252 wchar_t *wline;
1254 entry = first;
1255 ncommits = 0;
1256 while (entry) {
1257 if (ncommits == selected_idx) {
1258 *selected = entry;
1259 break;
1261 entry = TAILQ_NEXT(entry, entry);
1262 ncommits++;
1265 if (*selected && !(view->searching && view->search_next_done == 0)) {
1266 err = got_object_id_str(&id_str, (*selected)->id);
1267 if (err)
1268 return err;
1269 if (refs) {
1270 err = build_refs_str(&refs_str, refs, (*selected)->id,
1271 s->repo);
1272 if (err)
1273 goto done;
1277 if (commits_needed == 0)
1278 halfdelay(10); /* disable fast refresh */
1280 if (asprintf(&ncommits_str, " [%d/%d] %s",
1281 entry ? entry->idx + 1 : 0, commits->ncommits,
1282 commits_needed > 0 ?
1283 (view->searching && view->search_next_done == 0
1284 ? "searching..." : "loading... ") :
1285 (refs_str ? refs_str : "")) == -1) {
1286 err = got_error_from_errno("asprintf");
1287 goto done;
1290 if (path && strcmp(path, "/") != 0) {
1291 if (asprintf(&header, "commit %s %s%s",
1292 id_str ? id_str : "........................................",
1293 path, ncommits_str) == -1) {
1294 err = got_error_from_errno("asprintf");
1295 header = NULL;
1296 goto done;
1298 } else if (asprintf(&header, "commit %s%s",
1299 id_str ? id_str : "........................................",
1300 ncommits_str) == -1) {
1301 err = got_error_from_errno("asprintf");
1302 header = NULL;
1303 goto done;
1305 err = format_line(&wline, &width, header, view->ncols);
1306 if (err)
1307 goto done;
1309 werase(view->window);
1311 if (view_needs_focus_indication(view))
1312 wstandout(view->window);
1313 waddwstr(view->window, wline);
1314 while (width < view->ncols) {
1315 waddch(view->window, ' ');
1316 width++;
1318 if (view_needs_focus_indication(view))
1319 wstandend(view->window);
1320 free(wline);
1321 if (limit <= 1)
1322 goto done;
1324 /* Grow author column size if necessary. */
1325 entry = first;
1326 ncommits = 0;
1327 while (entry) {
1328 char *author;
1329 wchar_t *wauthor;
1330 int width;
1331 if (ncommits >= limit - 1)
1332 break;
1333 author = strdup(got_object_commit_get_author(entry->commit));
1334 if (author == NULL) {
1335 err = got_error_from_errno("strdup");
1336 goto done;
1338 err = format_author(&wauthor, &width, author, COLS);
1339 if (author_cols < width)
1340 author_cols = width;
1341 free(wauthor);
1342 free(author);
1343 entry = TAILQ_NEXT(entry, entry);
1346 entry = first;
1347 *last = first;
1348 ncommits = 0;
1349 while (entry) {
1350 if (ncommits >= limit - 1)
1351 break;
1352 if (ncommits == selected_idx)
1353 wstandout(view->window);
1354 err = draw_commit(view, entry->commit, entry->id, refs,
1355 author_cols);
1356 if (ncommits == selected_idx)
1357 wstandend(view->window);
1358 if (err)
1359 goto done;
1360 ncommits++;
1361 *last = entry;
1362 entry = TAILQ_NEXT(entry, entry);
1365 view_vborder(view);
1366 done:
1367 free(id_str);
1368 free(refs_str);
1369 free(ncommits_str);
1370 free(header);
1371 return err;
1374 static void
1375 scroll_up(struct tog_view *view,
1376 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1377 struct commit_queue *commits)
1379 struct commit_queue_entry *entry;
1380 int nscrolled = 0;
1382 entry = TAILQ_FIRST(&commits->head);
1383 if (*first_displayed_entry == entry)
1384 return;
1386 entry = *first_displayed_entry;
1387 while (entry && nscrolled < maxscroll) {
1388 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1389 if (entry) {
1390 *first_displayed_entry = entry;
1391 nscrolled++;
1396 static const struct got_error *
1397 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1398 pthread_cond_t *need_commits)
1400 int errcode;
1401 int max_wait = 20;
1403 halfdelay(1); /* fast refresh while loading commits */
1405 while (*commits_needed > 0) {
1406 if (*log_complete)
1407 break;
1409 /* Wake the log thread. */
1410 errcode = pthread_cond_signal(need_commits);
1411 if (errcode)
1412 return got_error_set_errno(errcode,
1413 "pthread_cond_signal");
1414 errcode = pthread_mutex_unlock(&tog_mutex);
1415 if (errcode)
1416 return got_error_set_errno(errcode,
1417 "pthread_mutex_unlock");
1418 pthread_yield();
1419 errcode = pthread_mutex_lock(&tog_mutex);
1420 if (errcode)
1421 return got_error_set_errno(errcode,
1422 "pthread_mutex_lock");
1424 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1426 * Thread is not done yet; lose a key press
1427 * and let the user retry... this way the GUI
1428 * remains interactive while logging deep paths
1429 * with few commits in history.
1431 return NULL;
1435 return NULL;
1438 static const struct got_error *
1439 scroll_down(struct tog_view *view,
1440 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1441 struct commit_queue_entry **last_displayed_entry,
1442 struct commit_queue *commits, int *log_complete, int *commits_needed,
1443 pthread_cond_t *need_commits)
1445 const struct got_error *err = NULL;
1446 struct commit_queue_entry *pentry;
1447 int nscrolled = 0;
1449 if (*last_displayed_entry == NULL)
1450 return NULL;
1452 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1453 if (pentry == NULL && !*log_complete) {
1455 * Ask the log thread for required amount of commits
1456 * plus some amount of pre-fetching.
1458 (*commits_needed) += maxscroll + 20;
1459 err = trigger_log_thread(0, commits_needed, log_complete,
1460 need_commits);
1461 if (err)
1462 return err;
1465 do {
1466 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1467 if (pentry == NULL)
1468 break;
1470 *last_displayed_entry = pentry;
1472 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1473 if (pentry == NULL)
1474 break;
1475 *first_displayed_entry = pentry;
1476 } while (++nscrolled < maxscroll);
1478 return err;
1481 static const struct got_error *
1482 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1483 struct got_commit_object *commit, struct got_object_id *commit_id,
1484 struct tog_view *log_view, struct got_reflist_head *refs,
1485 struct got_repository *repo)
1487 const struct got_error *err;
1488 struct got_object_qid *parent_id;
1489 struct tog_view *diff_view;
1491 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1492 if (diff_view == NULL)
1493 return got_error_from_errno("view_open");
1495 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1496 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1497 commit_id, log_view, refs, repo);
1498 if (err == NULL)
1499 *new_view = diff_view;
1500 return err;
1503 static const struct got_error *
1504 tree_view_visit_subtree(struct got_tree_object *subtree,
1505 struct tog_tree_view_state *s)
1507 struct tog_parent_tree *parent;
1509 parent = calloc(1, sizeof(*parent));
1510 if (parent == NULL)
1511 return got_error_from_errno("calloc");
1513 parent->tree = s->tree;
1514 parent->first_displayed_entry = s->first_displayed_entry;
1515 parent->selected_entry = s->selected_entry;
1516 parent->selected = s->selected;
1517 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1518 s->tree = subtree;
1519 s->entries = got_object_tree_get_entries(s->tree);
1520 s->selected = 0;
1521 s->first_displayed_entry = NULL;
1522 return NULL;
1526 static const struct got_error *
1527 browse_commit_tree(struct tog_view **new_view, int begin_x,
1528 struct commit_queue_entry *entry, const char *path,
1529 struct got_reflist_head *refs, struct got_repository *repo)
1531 const struct got_error *err = NULL;
1532 struct got_tree_object *tree;
1533 struct got_tree_entry *te;
1534 struct tog_tree_view_state *s;
1535 struct tog_view *tree_view;
1536 char *slash, *subpath = NULL;
1537 const char *p;
1539 err = got_object_open_as_tree(&tree, repo,
1540 got_object_commit_get_tree_id(entry->commit));
1541 if (err)
1542 return err;
1544 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1545 if (tree_view == NULL)
1546 return got_error_from_errno("view_open");
1548 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1549 if (err) {
1550 got_object_tree_close(tree);
1551 return err;
1553 s = &tree_view->state.tree;
1555 *new_view = tree_view;
1557 /* Walk the path and open corresponding tree objects. */
1558 p = path;
1559 while (p[0] == '/')
1560 p++;
1561 while (*p) {
1562 struct got_object_id *tree_id;
1564 /* Ensure the correct subtree entry is selected. */
1565 slash = strchr(p, '/');
1566 if (slash == NULL)
1567 slash = strchr(p, '\0');
1568 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1569 if (strncmp(p, te->name, slash - p) == 0) {
1570 s->selected_entry = te;
1571 break;
1573 s->selected++;
1575 if (s->selected_entry == NULL) {
1576 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1577 break;
1579 if (s->tree != s->root)
1580 s->selected++; /* skip '..' */
1582 if (!S_ISDIR(s->selected_entry->mode)) {
1583 /* Jump to this file's entry. */
1584 s->first_displayed_entry = s->selected_entry;
1585 s->selected = 0;
1586 break;
1589 slash = strchr(p, '/');
1590 if (slash)
1591 subpath = strndup(path, slash - path);
1592 else
1593 subpath = strdup(path);
1594 if (subpath == NULL) {
1595 err = got_error_from_errno("strdup");
1596 break;
1599 err = got_object_id_by_path(&tree_id, repo, entry->id,
1600 subpath);
1601 if (err)
1602 break;
1604 err = got_object_open_as_tree(&tree, repo, tree_id);
1605 free(tree_id);
1606 if (err)
1607 break;
1609 err = tree_view_visit_subtree(tree, s);
1610 if (err) {
1611 got_object_tree_close(tree);
1612 break;
1614 if (slash == NULL)
1615 break;
1616 free(subpath);
1617 subpath = NULL;
1618 p = slash;
1621 free(subpath);
1622 return err;
1625 static void *
1626 log_thread(void *arg)
1628 const struct got_error *err = NULL;
1629 int errcode = 0;
1630 struct tog_log_thread_args *a = arg;
1631 int done = 0;
1633 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1634 if (err)
1635 return (void *)err;
1637 while (!done && !err && !tog_sigpipe_received) {
1638 err = queue_commits(a->graph, a->commits, 1, a->repo,
1639 a->in_repo_path);
1640 if (err) {
1641 if (err->code != GOT_ERR_ITER_COMPLETED)
1642 return (void *)err;
1643 err = NULL;
1644 done = 1;
1645 } else if (a->commits_needed > 0)
1646 a->commits_needed--;
1648 errcode = pthread_mutex_lock(&tog_mutex);
1649 if (errcode) {
1650 err = got_error_set_errno(errcode,
1651 "pthread_mutex_lock");
1652 break;
1653 } else if (*a->quit)
1654 done = 1;
1655 else if (*a->first_displayed_entry == NULL) {
1656 *a->first_displayed_entry =
1657 TAILQ_FIRST(&a->commits->head);
1658 *a->selected_entry = *a->first_displayed_entry;
1661 if (done)
1662 a->commits_needed = 0;
1663 else if (a->commits_needed == 0) {
1664 errcode = pthread_cond_wait(&a->need_commits,
1665 &tog_mutex);
1666 if (errcode)
1667 err = got_error_set_errno(errcode,
1668 "pthread_cond_wait");
1671 errcode = pthread_mutex_unlock(&tog_mutex);
1672 if (errcode && err == NULL)
1673 err = got_error_set_errno(errcode,
1674 "pthread_mutex_unlock");
1676 a->log_complete = 1;
1677 return (void *)err;
1680 static const struct got_error *
1681 stop_log_thread(struct tog_log_view_state *s)
1683 const struct got_error *err = NULL;
1684 int errcode;
1686 if (s->thread) {
1687 s->quit = 1;
1688 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1689 if (errcode)
1690 return got_error_set_errno(errcode,
1691 "pthread_cond_signal");
1692 errcode = pthread_mutex_unlock(&tog_mutex);
1693 if (errcode)
1694 return got_error_set_errno(errcode,
1695 "pthread_mutex_unlock");
1696 errcode = pthread_join(s->thread, (void **)&err);
1697 if (errcode)
1698 return got_error_set_errno(errcode, "pthread_join");
1699 errcode = pthread_mutex_lock(&tog_mutex);
1700 if (errcode)
1701 return got_error_set_errno(errcode,
1702 "pthread_mutex_lock");
1703 s->thread = NULL;
1706 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1707 if (errcode && err == NULL)
1708 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1710 if (s->thread_args.repo) {
1711 got_repo_close(s->thread_args.repo);
1712 s->thread_args.repo = NULL;
1715 if (s->thread_args.graph) {
1716 got_commit_graph_close(s->thread_args.graph);
1717 s->thread_args.graph = NULL;
1720 return err;
1723 static const struct got_error *
1724 close_log_view(struct tog_view *view)
1726 const struct got_error *err = NULL;
1727 struct tog_log_view_state *s = &view->state.log;
1729 err = stop_log_thread(s);
1730 free_commits(&s->commits);
1731 free(s->in_repo_path);
1732 s->in_repo_path = NULL;
1733 free(s->start_id);
1734 s->start_id = NULL;
1735 return err;
1738 static const struct got_error *
1739 search_start_log_view(struct tog_view *view)
1741 struct tog_log_view_state *s = &view->state.log;
1743 s->matched_entry = NULL;
1744 s->search_entry = NULL;
1745 return NULL;
1748 static int
1749 match_commit(struct got_commit_object *commit, const char *id_str,
1750 regex_t *regex)
1752 regmatch_t regmatch;
1754 if (regexec(regex, got_object_commit_get_author(commit), 1,
1755 &regmatch, 0) == 0 ||
1756 regexec(regex, got_object_commit_get_committer(commit), 1,
1757 &regmatch, 0) == 0 ||
1758 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1759 &regmatch, 0) == 0 ||
1760 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1761 return 1;
1763 return 0;
1766 static const struct got_error *
1767 search_next_log_view(struct tog_view *view)
1769 const struct got_error *err = NULL;
1770 struct tog_log_view_state *s = &view->state.log;
1771 struct commit_queue_entry *entry;
1773 if (!view->searching) {
1774 view->search_next_done = 1;
1775 return NULL;
1778 if (s->search_entry) {
1779 if (wgetch(view->window) == KEY_BACKSPACE) {
1780 view->search_next_done = 1;
1781 return NULL;
1783 if (view->searching == TOG_SEARCH_FORWARD)
1784 entry = TAILQ_NEXT(s->search_entry, entry);
1785 else
1786 entry = TAILQ_PREV(s->search_entry,
1787 commit_queue_head, entry);
1788 } else if (s->matched_entry) {
1789 if (view->searching == TOG_SEARCH_FORWARD)
1790 entry = TAILQ_NEXT(s->selected_entry, entry);
1791 else
1792 entry = TAILQ_PREV(s->selected_entry,
1793 commit_queue_head, entry);
1794 } else {
1795 if (view->searching == TOG_SEARCH_FORWARD)
1796 entry = TAILQ_FIRST(&s->commits.head);
1797 else
1798 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1801 while (1) {
1802 char *id_str;
1803 if (entry == NULL) {
1804 if (s->thread_args.log_complete ||
1805 view->searching == TOG_SEARCH_BACKWARD) {
1806 view->search_next_done = 1;
1807 return NULL;
1810 * Poke the log thread for more commits and return,
1811 * allowing the main loop to make progress. Search
1812 * will resume at s->search_entry once we come back.
1814 s->thread_args.commits_needed++;
1815 return trigger_log_thread(1,
1816 &s->thread_args.commits_needed,
1817 &s->thread_args.log_complete,
1818 &s->thread_args.need_commits);
1821 err = got_object_id_str(&id_str, entry->id);
1822 if (err)
1823 return err;
1825 if (match_commit(entry->commit, id_str, &view->regex)) {
1826 view->search_next_done = 1;
1827 s->matched_entry = entry;
1828 free(id_str);
1829 break;
1831 free(id_str);
1832 s->search_entry = entry;
1833 if (view->searching == TOG_SEARCH_FORWARD)
1834 entry = TAILQ_NEXT(entry, entry);
1835 else
1836 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1839 if (s->matched_entry) {
1840 int cur = s->selected_entry->idx;
1841 while (cur < s->matched_entry->idx) {
1842 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1843 if (err)
1844 return err;
1845 cur++;
1847 while (cur > s->matched_entry->idx) {
1848 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1849 if (err)
1850 return err;
1851 cur--;
1855 s->search_entry = NULL;
1857 return NULL;
1860 static const struct got_error *
1861 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1862 struct got_reflist_head *refs, struct got_repository *repo,
1863 const char *head_ref_name, const char *path, int check_disk)
1865 const struct got_error *err = NULL;
1866 struct tog_log_view_state *s = &view->state.log;
1867 struct got_repository *thread_repo = NULL;
1868 struct got_commit_graph *thread_graph = NULL;
1869 int errcode;
1871 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1872 if (err != NULL)
1873 goto done;
1875 /* The commit queue only contains commits being displayed. */
1876 TAILQ_INIT(&s->commits.head);
1877 s->commits.ncommits = 0;
1879 s->refs = refs;
1880 s->repo = repo;
1881 s->head_ref_name = head_ref_name;
1882 s->start_id = got_object_id_dup(start_id);
1883 if (s->start_id == NULL) {
1884 err = got_error_from_errno("got_object_id_dup");
1885 goto done;
1888 view->show = show_log_view;
1889 view->input = input_log_view;
1890 view->close = close_log_view;
1891 view->search_start = search_start_log_view;
1892 view->search_next = search_next_log_view;
1894 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1895 if (err)
1896 goto done;
1897 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1898 0, thread_repo);
1899 if (err)
1900 goto done;
1902 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1903 if (errcode) {
1904 err = got_error_set_errno(errcode, "pthread_cond_init");
1905 goto done;
1908 s->thread_args.commits_needed = view->nlines;
1909 s->thread_args.graph = thread_graph;
1910 s->thread_args.commits = &s->commits;
1911 s->thread_args.in_repo_path = s->in_repo_path;
1912 s->thread_args.start_id = s->start_id;
1913 s->thread_args.repo = thread_repo;
1914 s->thread_args.log_complete = 0;
1915 s->thread_args.quit = &s->quit;
1916 s->thread_args.view = view;
1917 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1918 s->thread_args.selected_entry = &s->selected_entry;
1919 done:
1920 if (err)
1921 close_log_view(view);
1922 return err;
1925 static const struct got_error *
1926 show_log_view(struct tog_view *view)
1928 struct tog_log_view_state *s = &view->state.log;
1930 if (s->thread == NULL) {
1931 int errcode = pthread_create(&s->thread, NULL, log_thread,
1932 &s->thread_args);
1933 if (errcode)
1934 return got_error_set_errno(errcode, "pthread_create");
1937 return draw_commits(view, &s->last_displayed_entry,
1938 &s->selected_entry, s->first_displayed_entry,
1939 &s->commits, s->selected, view->nlines, s->refs,
1940 s->in_repo_path, s->thread_args.commits_needed);
1943 static const struct got_error *
1944 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1945 struct tog_view **focus_view, struct tog_view *view, int ch)
1947 const struct got_error *err = NULL;
1948 struct tog_log_view_state *s = &view->state.log;
1949 char *parent_path, *in_repo_path = NULL;
1950 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1951 int begin_x = 0;
1952 struct got_object_id *start_id;
1954 switch (ch) {
1955 case 'q':
1956 s->quit = 1;
1957 break;
1958 case 'k':
1959 case KEY_UP:
1960 case '<':
1961 case ',':
1962 if (s->first_displayed_entry == NULL)
1963 break;
1964 if (s->selected > 0)
1965 s->selected--;
1966 else
1967 scroll_up(view, &s->first_displayed_entry, 1,
1968 &s->commits);
1969 break;
1970 case KEY_PPAGE:
1971 case CTRL('b'):
1972 if (s->first_displayed_entry == NULL)
1973 break;
1974 if (TAILQ_FIRST(&s->commits.head) ==
1975 s->first_displayed_entry) {
1976 s->selected = 0;
1977 break;
1979 scroll_up(view, &s->first_displayed_entry,
1980 view->nlines, &s->commits);
1981 break;
1982 case 'j':
1983 case KEY_DOWN:
1984 case '>':
1985 case '.':
1986 if (s->first_displayed_entry == NULL)
1987 break;
1988 if (s->selected < MIN(view->nlines - 2,
1989 s->commits.ncommits - 1)) {
1990 s->selected++;
1991 break;
1993 err = scroll_down(view, &s->first_displayed_entry, 1,
1994 &s->last_displayed_entry, &s->commits,
1995 &s->thread_args.log_complete,
1996 &s->thread_args.commits_needed,
1997 &s->thread_args.need_commits);
1998 break;
1999 case KEY_NPAGE:
2000 case CTRL('f'): {
2001 struct commit_queue_entry *first;
2002 first = s->first_displayed_entry;
2003 if (first == NULL)
2004 break;
2005 err = scroll_down(view, &s->first_displayed_entry,
2006 view->nlines, &s->last_displayed_entry,
2007 &s->commits, &s->thread_args.log_complete,
2008 &s->thread_args.commits_needed,
2009 &s->thread_args.need_commits);
2010 if (first == s->first_displayed_entry &&
2011 s->selected < MIN(view->nlines - 2,
2012 s->commits.ncommits - 1)) {
2013 /* can't scroll further down */
2014 s->selected = MIN(view->nlines - 2,
2015 s->commits.ncommits - 1);
2017 err = NULL;
2018 break;
2020 case KEY_RESIZE:
2021 if (s->selected > view->nlines - 2)
2022 s->selected = view->nlines - 2;
2023 if (s->selected > s->commits.ncommits - 1)
2024 s->selected = s->commits.ncommits - 1;
2025 break;
2026 case KEY_ENTER:
2027 case ' ':
2028 case '\r':
2029 if (s->selected_entry == NULL)
2030 break;
2031 if (view_is_parent_view(view))
2032 begin_x = view_split_begin_x(view->begin_x);
2033 err = open_diff_view_for_commit(&diff_view, begin_x,
2034 s->selected_entry->commit, s->selected_entry->id,
2035 view, s->refs, s->repo);
2036 if (err)
2037 break;
2038 if (view_is_parent_view(view)) {
2039 err = view_close_child(view);
2040 if (err)
2041 return err;
2042 err = view_set_child(view, diff_view);
2043 if (err) {
2044 view_close(diff_view);
2045 break;
2047 *focus_view = diff_view;
2048 view->child_focussed = 1;
2049 } else
2050 *new_view = diff_view;
2051 break;
2052 case 't':
2053 if (s->selected_entry == NULL)
2054 break;
2055 if (view_is_parent_view(view))
2056 begin_x = view_split_begin_x(view->begin_x);
2057 err = browse_commit_tree(&tree_view, begin_x,
2058 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2059 if (err)
2060 break;
2061 if (view_is_parent_view(view)) {
2062 err = view_close_child(view);
2063 if (err)
2064 return err;
2065 err = view_set_child(view, tree_view);
2066 if (err) {
2067 view_close(tree_view);
2068 break;
2070 *focus_view = tree_view;
2071 view->child_focussed = 1;
2072 } else
2073 *new_view = tree_view;
2074 break;
2075 case KEY_BACKSPACE:
2076 if (strcmp(s->in_repo_path, "/") == 0)
2077 break;
2078 parent_path = dirname(s->in_repo_path);
2079 if (parent_path && strcmp(parent_path, ".") != 0) {
2080 err = stop_log_thread(s);
2081 if (err)
2082 return err;
2083 lv = view_open(view->nlines, view->ncols,
2084 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2085 if (lv == NULL)
2086 return got_error_from_errno(
2087 "view_open");
2088 err = open_log_view(lv, s->start_id, s->refs,
2089 s->repo, s->head_ref_name, parent_path, 0);
2090 if (err)
2091 return err;;
2092 if (view_is_parent_view(view))
2093 *new_view = lv;
2094 else {
2095 view_set_child(view->parent, lv);
2096 *focus_view = lv;
2098 return NULL;
2100 break;
2101 case CTRL('l'):
2102 err = stop_log_thread(s);
2103 if (err)
2104 return err;
2105 lv = view_open(view->nlines, view->ncols,
2106 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2107 if (lv == NULL)
2108 return got_error_from_errno("view_open");
2109 err = get_head_commit_id(&start_id, s->head_ref_name ?
2110 s->head_ref_name : GOT_REF_HEAD, s->repo);
2111 if (err) {
2112 view_close(lv);
2113 return err;
2115 in_repo_path = strdup(s->in_repo_path);
2116 if (in_repo_path == NULL) {
2117 free(start_id);
2118 view_close(lv);
2119 return got_error_from_errno("strdup");
2121 err = open_log_view(lv, start_id, s->refs, s->repo,
2122 s->head_ref_name, in_repo_path, 0);
2123 if (err) {
2124 free(start_id);
2125 view_close(lv);
2126 return err;;
2128 *dead_view = view;
2129 *new_view = lv;
2130 break;
2131 default:
2132 break;
2135 return err;
2138 static const struct got_error *
2139 apply_unveil(const char *repo_path, const char *worktree_path)
2141 const struct got_error *error;
2143 #ifdef PROFILE
2144 if (unveil("gmon.out", "rwc") != 0)
2145 return got_error_from_errno2("unveil", "gmon.out");
2146 #endif
2147 if (repo_path && unveil(repo_path, "r") != 0)
2148 return got_error_from_errno2("unveil", repo_path);
2150 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2151 return got_error_from_errno2("unveil", worktree_path);
2153 if (unveil("/tmp", "rwc") != 0)
2154 return got_error_from_errno2("unveil", "/tmp");
2156 error = got_privsep_unveil_exec_helpers();
2157 if (error != NULL)
2158 return error;
2160 if (unveil(NULL, NULL) != 0)
2161 return got_error_from_errno("unveil");
2163 return NULL;
2166 static void
2167 init_curses(void)
2169 initscr();
2170 cbreak();
2171 halfdelay(1); /* Do fast refresh while initial view is loading. */
2172 noecho();
2173 nonl();
2174 intrflush(stdscr, FALSE);
2175 keypad(stdscr, TRUE);
2176 curs_set(0);
2177 signal(SIGWINCH, tog_sigwinch);
2178 signal(SIGPIPE, tog_sigpipe);
2181 static const struct got_error *
2182 cmd_log(int argc, char *argv[])
2184 const struct got_error *error;
2185 struct got_repository *repo = NULL;
2186 struct got_worktree *worktree = NULL;
2187 struct got_reflist_head refs;
2188 struct got_object_id *start_id = NULL;
2189 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2190 char *start_commit = NULL;
2191 int ch;
2192 struct tog_view *view;
2194 SIMPLEQ_INIT(&refs);
2196 #ifndef PROFILE
2197 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2198 NULL) == -1)
2199 err(1, "pledge");
2200 #endif
2202 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2203 switch (ch) {
2204 case 'c':
2205 start_commit = optarg;
2206 break;
2207 case 'r':
2208 repo_path = realpath(optarg, NULL);
2209 if (repo_path == NULL)
2210 err(1, "-r option");
2211 break;
2212 default:
2213 usage_log();
2214 /* NOTREACHED */
2218 argc -= optind;
2219 argv += optind;
2221 cwd = getcwd(NULL, 0);
2222 if (cwd == NULL) {
2223 error = got_error_from_errno("getcwd");
2224 goto done;
2226 error = got_worktree_open(&worktree, cwd);
2227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2228 goto done;
2229 error = NULL;
2231 if (argc == 0) {
2232 path = strdup("");
2233 if (path == NULL) {
2234 error = got_error_from_errno("strdup");
2235 goto done;
2237 } else if (argc == 1) {
2238 if (worktree) {
2239 error = got_worktree_resolve_path(&path, worktree,
2240 argv[0]);
2241 if (error)
2242 goto done;
2243 } else {
2244 path = strdup(argv[0]);
2245 if (path == NULL) {
2246 error = got_error_from_errno("strdup");
2247 goto done;
2250 } else
2251 usage_log();
2253 if (repo_path == NULL) {
2254 if (worktree)
2255 repo_path = strdup(
2256 got_worktree_get_repo_path(worktree));
2257 else
2258 repo_path = strdup(cwd);
2260 if (repo_path == NULL) {
2261 error = got_error_from_errno("strdup");
2262 goto done;
2265 init_curses();
2267 error = got_repo_open(&repo, repo_path);
2268 if (error != NULL)
2269 goto done;
2271 error = apply_unveil(got_repo_get_path(repo),
2272 worktree ? got_worktree_get_root_path(worktree) : NULL);
2273 if (error)
2274 goto done;
2276 if (start_commit == NULL)
2277 error = get_head_commit_id(&start_id, worktree ?
2278 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2279 repo);
2280 else {
2281 error = get_head_commit_id(&start_id, start_commit, repo);
2282 if (error) {
2283 if (error->code != GOT_ERR_NOT_REF)
2284 goto done;
2285 error = got_repo_match_object_id_prefix(&start_id,
2286 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2289 if (error != NULL)
2290 goto done;
2292 error = got_ref_list(&refs, repo);
2293 if (error)
2294 goto done;
2296 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2297 if (view == NULL) {
2298 error = got_error_from_errno("view_open");
2299 goto done;
2301 error = open_log_view(view, start_id, &refs, repo, worktree ?
2302 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2303 if (error)
2304 goto done;
2305 error = view_loop(view);
2306 done:
2307 free(repo_path);
2308 free(cwd);
2309 free(path);
2310 free(start_id);
2311 if (repo)
2312 got_repo_close(repo);
2313 if (worktree)
2314 got_worktree_close(worktree);
2315 got_ref_list_free(&refs);
2316 return error;
2319 __dead static void
2320 usage_diff(void)
2322 endwin();
2323 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2324 getprogname());
2325 exit(1);
2328 static char *
2329 parse_next_line(FILE *f, size_t *len)
2331 char *line;
2332 size_t linelen;
2333 size_t lineno;
2334 const char delim[3] = { '\0', '\0', '\0'};
2336 line = fparseln(f, &linelen, &lineno, delim, 0);
2337 if (len)
2338 *len = linelen;
2339 return line;
2342 static const struct got_error *
2343 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2344 int *last_displayed_line, int *eof, int max_lines,
2345 char *header)
2347 const struct got_error *err;
2348 int nlines = 0, nprinted = 0;
2349 char *line;
2350 size_t len;
2351 wchar_t *wline;
2352 int width;
2354 rewind(f);
2355 werase(view->window);
2357 if (header) {
2358 err = format_line(&wline, &width, header, view->ncols);
2359 if (err) {
2360 return err;
2363 if (view_needs_focus_indication(view))
2364 wstandout(view->window);
2365 waddwstr(view->window, wline);
2366 if (view_needs_focus_indication(view))
2367 wstandend(view->window);
2368 if (width < view->ncols - 1)
2369 waddch(view->window, '\n');
2371 if (max_lines <= 1)
2372 return NULL;
2373 max_lines--;
2376 *eof = 0;
2377 while (nprinted < max_lines) {
2378 line = parse_next_line(f, &len);
2379 if (line == NULL) {
2380 *eof = 1;
2381 break;
2383 if (++nlines < *first_displayed_line) {
2384 free(line);
2385 continue;
2388 err = format_line(&wline, &width, line, view->ncols);
2389 if (err) {
2390 free(line);
2391 return err;
2393 waddwstr(view->window, wline);
2394 if (width < view->ncols - 1)
2395 waddch(view->window, '\n');
2396 if (++nprinted == 1)
2397 *first_displayed_line = nlines;
2398 free(line);
2399 free(wline);
2400 wline = NULL;
2402 *last_displayed_line = nlines;
2404 view_vborder(view);
2406 if (*eof) {
2407 while (nprinted < view->nlines) {
2408 waddch(view->window, '\n');
2409 nprinted++;
2412 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2413 if (err) {
2414 return err;
2417 wstandout(view->window);
2418 waddwstr(view->window, wline);
2419 wstandend(view->window);
2422 return NULL;
2425 static char *
2426 get_datestr(time_t *time, char *datebuf)
2428 char *p, *s = ctime_r(time, datebuf);
2429 p = strchr(s, '\n');
2430 if (p)
2431 *p = '\0';
2432 return s;
2435 static const struct got_error *
2436 write_commit_info(struct got_object_id *commit_id,
2437 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2439 const struct got_error *err = NULL;
2440 char datebuf[26];
2441 struct got_commit_object *commit;
2442 char *id_str = NULL;
2443 time_t committer_time;
2444 const char *author, *committer;
2445 char *refs_str = NULL;
2447 if (refs) {
2448 err = build_refs_str(&refs_str, refs, commit_id, repo);
2449 if (err)
2450 return err;
2453 err = got_object_open_as_commit(&commit, repo, commit_id);
2454 if (err)
2455 return err;
2457 err = got_object_id_str(&id_str, commit_id);
2458 if (err) {
2459 err = got_error_from_errno("got_object_id_str");
2460 goto done;
2463 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2464 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2465 err = got_error_from_errno("fprintf");
2466 goto done;
2468 if (fprintf(outfile, "from: %s\n",
2469 got_object_commit_get_author(commit)) < 0) {
2470 err = got_error_from_errno("fprintf");
2471 goto done;
2473 committer_time = got_object_commit_get_committer_time(commit);
2474 if (fprintf(outfile, "date: %s UTC\n",
2475 get_datestr(&committer_time, datebuf)) < 0) {
2476 err = got_error_from_errno("fprintf");
2477 goto done;
2479 author = got_object_commit_get_author(commit);
2480 committer = got_object_commit_get_committer(commit);
2481 if (strcmp(author, committer) != 0 &&
2482 fprintf(outfile, "via: %s\n", committer) < 0) {
2483 err = got_error_from_errno("fprintf");
2484 goto done;
2486 if (fprintf(outfile, "%s\n",
2487 got_object_commit_get_logmsg(commit)) < 0) {
2488 err = got_error_from_errno("fprintf");
2489 goto done;
2491 done:
2492 free(id_str);
2493 free(refs_str);
2494 got_object_commit_close(commit);
2495 return err;
2498 static const struct got_error *
2499 create_diff(struct tog_diff_view_state *s)
2501 const struct got_error *err = NULL;
2502 FILE *f = NULL;
2503 int obj_type;
2505 f = got_opentemp();
2506 if (f == NULL) {
2507 err = got_error_from_errno("got_opentemp");
2508 goto done;
2510 if (s->f && fclose(s->f) != 0) {
2511 err = got_error_from_errno("fclose");
2512 goto done;
2514 s->f = f;
2516 if (s->id1)
2517 err = got_object_get_type(&obj_type, s->repo, s->id1);
2518 else
2519 err = got_object_get_type(&obj_type, s->repo, s->id2);
2520 if (err)
2521 goto done;
2523 switch (obj_type) {
2524 case GOT_OBJ_TYPE_BLOB:
2525 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2526 s->diff_context, s->repo, f);
2527 break;
2528 case GOT_OBJ_TYPE_TREE:
2529 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2530 s->diff_context, s->repo, f);
2531 break;
2532 case GOT_OBJ_TYPE_COMMIT: {
2533 const struct got_object_id_queue *parent_ids;
2534 struct got_object_qid *pid;
2535 struct got_commit_object *commit2;
2537 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2538 if (err)
2539 break;
2540 /* Show commit info if we're diffing to a parent/root commit. */
2541 if (s->id1 == NULL)
2542 write_commit_info(s->id2, s->refs, s->repo, f);
2543 else {
2544 parent_ids = got_object_commit_get_parent_ids(commit2);
2545 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2546 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2547 write_commit_info(s->id2, s->refs,
2548 s->repo, f);
2549 break;
2553 got_object_commit_close(commit2);
2555 err = got_diff_objects_as_commits(s->id1, s->id2,
2556 s->diff_context, s->repo, f);
2557 break;
2559 default:
2560 err = got_error(GOT_ERR_OBJ_TYPE);
2561 break;
2563 done:
2564 if (f && fflush(f) != 0 && err == NULL)
2565 err = got_error_from_errno("fflush");
2566 return err;
2569 static void
2570 diff_view_indicate_progress(struct tog_view *view)
2572 mvwaddstr(view->window, 0, 0, "diffing...");
2573 update_panels();
2574 doupdate();
2577 static const struct got_error *
2578 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2579 struct got_object_id *id2, struct tog_view *log_view,
2580 struct got_reflist_head *refs, struct got_repository *repo)
2582 const struct got_error *err;
2584 if (id1 != NULL && id2 != NULL) {
2585 int type1, type2;
2586 err = got_object_get_type(&type1, repo, id1);
2587 if (err)
2588 return err;
2589 err = got_object_get_type(&type2, repo, id2);
2590 if (err)
2591 return err;
2593 if (type1 != type2)
2594 return got_error(GOT_ERR_OBJ_TYPE);
2597 if (id1) {
2598 view->state.diff.id1 = got_object_id_dup(id1);
2599 if (view->state.diff.id1 == NULL)
2600 return got_error_from_errno("got_object_id_dup");
2601 } else
2602 view->state.diff.id1 = NULL;
2604 view->state.diff.id2 = got_object_id_dup(id2);
2605 if (view->state.diff.id2 == NULL) {
2606 free(view->state.diff.id1);
2607 view->state.diff.id1 = NULL;
2608 return got_error_from_errno("got_object_id_dup");
2610 view->state.diff.f = NULL;
2611 view->state.diff.first_displayed_line = 1;
2612 view->state.diff.last_displayed_line = view->nlines;
2613 view->state.diff.diff_context = 3;
2614 view->state.diff.log_view = log_view;
2615 view->state.diff.repo = repo;
2616 view->state.diff.refs = refs;
2618 if (log_view && view_is_splitscreen(view))
2619 show_log_view(log_view); /* draw vborder */
2620 diff_view_indicate_progress(view);
2622 err = create_diff(&view->state.diff);
2623 if (err) {
2624 free(view->state.diff.id1);
2625 view->state.diff.id1 = NULL;
2626 free(view->state.diff.id2);
2627 view->state.diff.id2 = NULL;
2628 return err;
2631 view->show = show_diff_view;
2632 view->input = input_diff_view;
2633 view->close = close_diff_view;
2635 return NULL;
2638 static const struct got_error *
2639 close_diff_view(struct tog_view *view)
2641 const struct got_error *err = NULL;
2643 free(view->state.diff.id1);
2644 view->state.diff.id1 = NULL;
2645 free(view->state.diff.id2);
2646 view->state.diff.id2 = NULL;
2647 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2648 err = got_error_from_errno("fclose");
2649 return err;
2652 static const struct got_error *
2653 show_diff_view(struct tog_view *view)
2655 const struct got_error *err;
2656 struct tog_diff_view_state *s = &view->state.diff;
2657 char *id_str1 = NULL, *id_str2, *header;
2659 if (s->id1) {
2660 err = got_object_id_str(&id_str1, s->id1);
2661 if (err)
2662 return err;
2664 err = got_object_id_str(&id_str2, s->id2);
2665 if (err)
2666 return err;
2668 if (asprintf(&header, "diff %s %s",
2669 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2670 err = got_error_from_errno("asprintf");
2671 free(id_str1);
2672 free(id_str2);
2673 return err;
2675 free(id_str1);
2676 free(id_str2);
2678 return draw_file(view, s->f, &s->first_displayed_line,
2679 &s->last_displayed_line, &s->eof, view->nlines,
2680 header);
2683 static const struct got_error *
2684 set_selected_commit(struct tog_diff_view_state *s,
2685 struct commit_queue_entry *entry)
2687 const struct got_error *err;
2688 const struct got_object_id_queue *parent_ids;
2689 struct got_commit_object *selected_commit;
2690 struct got_object_qid *pid;
2692 free(s->id2);
2693 s->id2 = got_object_id_dup(entry->id);
2694 if (s->id2 == NULL)
2695 return got_error_from_errno("got_object_id_dup");
2697 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2698 if (err)
2699 return err;
2700 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2701 free(s->id1);
2702 pid = SIMPLEQ_FIRST(parent_ids);
2703 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2704 got_object_commit_close(selected_commit);
2705 return NULL;
2708 static const struct got_error *
2709 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2710 struct tog_view **focus_view, struct tog_view *view, int ch)
2712 const struct got_error *err = NULL;
2713 struct tog_diff_view_state *s = &view->state.diff;
2714 struct tog_log_view_state *ls;
2715 struct commit_queue_entry *entry;
2716 int i;
2718 switch (ch) {
2719 case 'k':
2720 case KEY_UP:
2721 if (s->first_displayed_line > 1)
2722 s->first_displayed_line--;
2723 break;
2724 case KEY_PPAGE:
2725 case CTRL('b'):
2726 if (s->first_displayed_line == 1)
2727 break;
2728 i = 0;
2729 while (i++ < view->nlines - 1 &&
2730 s->first_displayed_line > 1)
2731 s->first_displayed_line--;
2732 break;
2733 case 'j':
2734 case KEY_DOWN:
2735 if (!s->eof)
2736 s->first_displayed_line++;
2737 break;
2738 case KEY_NPAGE:
2739 case CTRL('f'):
2740 case ' ':
2741 if (s->eof)
2742 break;
2743 i = 0;
2744 while (!s->eof && i++ < view->nlines - 1) {
2745 char *line;
2746 line = parse_next_line(s->f, NULL);
2747 s->first_displayed_line++;
2748 if (line == NULL)
2749 break;
2751 break;
2752 case '[':
2753 if (s->diff_context > 0) {
2754 s->diff_context--;
2755 diff_view_indicate_progress(view);
2756 err = create_diff(s);
2758 break;
2759 case ']':
2760 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2761 s->diff_context++;
2762 diff_view_indicate_progress(view);
2763 err = create_diff(s);
2765 break;
2766 case '<':
2767 case ',':
2768 if (s->log_view == NULL)
2769 break;
2770 ls = &s->log_view->state.log;
2771 entry = TAILQ_PREV(ls->selected_entry,
2772 commit_queue_head, entry);
2773 if (entry == NULL)
2774 break;
2776 err = input_log_view(NULL, NULL, NULL, s->log_view,
2777 KEY_UP);
2778 if (err)
2779 break;
2781 err = set_selected_commit(s, entry);
2782 if (err)
2783 break;
2785 s->first_displayed_line = 1;
2786 s->last_displayed_line = view->nlines;
2788 diff_view_indicate_progress(view);
2789 err = create_diff(s);
2790 break;
2791 case '>':
2792 case '.':
2793 if (s->log_view == NULL)
2794 break;
2795 ls = &s->log_view->state.log;
2797 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2798 ls->thread_args.commits_needed++;
2800 /* Display "loading..." in log view. */
2801 show_log_view(s->log_view);
2802 update_panels();
2803 doupdate();
2805 err = trigger_log_thread(1 /* load_all */,
2806 &ls->thread_args.commits_needed,
2807 &ls->thread_args.log_complete,
2808 &ls->thread_args.need_commits);
2809 if (err)
2810 break;
2812 err = input_log_view(NULL, NULL, NULL, s->log_view,
2813 KEY_DOWN);
2814 if (err)
2815 break;
2817 entry = TAILQ_NEXT(ls->selected_entry, entry);
2818 if (entry == NULL)
2819 break;
2821 err = set_selected_commit(s, entry);
2822 if (err)
2823 break;
2825 s->first_displayed_line = 1;
2826 s->last_displayed_line = view->nlines;
2828 diff_view_indicate_progress(view);
2829 err = create_diff(s);
2830 break;
2831 default:
2832 break;
2835 return err;
2838 static const struct got_error *
2839 cmd_diff(int argc, char *argv[])
2841 const struct got_error *error = NULL;
2842 struct got_repository *repo = NULL;
2843 struct got_reflist_head refs;
2844 struct got_object_id *id1 = NULL, *id2 = NULL;
2845 char *repo_path = NULL;
2846 char *id_str1 = NULL, *id_str2 = NULL;
2847 int ch;
2848 struct tog_view *view;
2850 SIMPLEQ_INIT(&refs);
2852 #ifndef PROFILE
2853 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2854 NULL) == -1)
2855 err(1, "pledge");
2856 #endif
2858 while ((ch = getopt(argc, argv, "")) != -1) {
2859 switch (ch) {
2860 default:
2861 usage_diff();
2862 /* NOTREACHED */
2866 argc -= optind;
2867 argv += optind;
2869 if (argc == 0) {
2870 usage_diff(); /* TODO show local worktree changes */
2871 } else if (argc == 2) {
2872 repo_path = getcwd(NULL, 0);
2873 if (repo_path == NULL)
2874 return got_error_from_errno("getcwd");
2875 id_str1 = argv[0];
2876 id_str2 = argv[1];
2877 } else if (argc == 3) {
2878 repo_path = realpath(argv[0], NULL);
2879 if (repo_path == NULL)
2880 return got_error_from_errno2("realpath", argv[0]);
2881 id_str1 = argv[1];
2882 id_str2 = argv[2];
2883 } else
2884 usage_diff();
2886 init_curses();
2888 error = got_repo_open(&repo, repo_path);
2889 if (error)
2890 goto done;
2892 error = apply_unveil(got_repo_get_path(repo), NULL);
2893 if (error)
2894 goto done;
2896 error = got_repo_match_object_id_prefix(&id1, id_str1,
2897 GOT_OBJ_TYPE_ANY, repo);
2898 if (error)
2899 goto done;
2901 error = got_repo_match_object_id_prefix(&id2, id_str2,
2902 GOT_OBJ_TYPE_ANY, repo);
2903 if (error)
2904 goto done;
2906 error = got_ref_list(&refs, repo);
2907 if (error)
2908 goto done;
2910 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2911 if (view == NULL) {
2912 error = got_error_from_errno("view_open");
2913 goto done;
2915 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2916 if (error)
2917 goto done;
2918 error = view_loop(view);
2919 done:
2920 free(repo_path);
2921 if (repo)
2922 got_repo_close(repo);
2923 got_ref_list_free(&refs);
2924 return error;
2927 __dead static void
2928 usage_blame(void)
2930 endwin();
2931 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2932 getprogname());
2933 exit(1);
2936 struct tog_blame_line {
2937 int annotated;
2938 struct got_object_id *id;
2941 static const struct got_error *
2942 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2943 const char *path, struct tog_blame_line *lines, int nlines,
2944 int blame_complete, int selected_line, int *first_displayed_line,
2945 int *last_displayed_line, int *eof, int max_lines)
2947 const struct got_error *err;
2948 int lineno = 0, nprinted = 0;
2949 char *line;
2950 size_t len;
2951 wchar_t *wline;
2952 int width, wlimit;
2953 struct tog_blame_line *blame_line;
2954 struct got_object_id *prev_id = NULL;
2955 char *id_str;
2957 err = got_object_id_str(&id_str, id);
2958 if (err)
2959 return err;
2961 rewind(f);
2962 werase(view->window);
2964 if (asprintf(&line, "commit %s", id_str) == -1) {
2965 err = got_error_from_errno("asprintf");
2966 free(id_str);
2967 return err;
2970 err = format_line(&wline, &width, line, view->ncols);
2971 free(line);
2972 line = NULL;
2973 if (view_needs_focus_indication(view))
2974 wstandout(view->window);
2975 waddwstr(view->window, wline);
2976 if (view_needs_focus_indication(view))
2977 wstandend(view->window);
2978 free(wline);
2979 wline = NULL;
2980 if (width < view->ncols - 1)
2981 waddch(view->window, '\n');
2983 if (asprintf(&line, "[%d/%d] %s%s",
2984 *first_displayed_line - 1 + selected_line, nlines,
2985 blame_complete ? "" : "annotating... ", path) == -1) {
2986 free(id_str);
2987 return got_error_from_errno("asprintf");
2989 free(id_str);
2990 err = format_line(&wline, &width, line, view->ncols);
2991 free(line);
2992 line = NULL;
2993 if (err)
2994 return err;
2995 waddwstr(view->window, wline);
2996 free(wline);
2997 wline = NULL;
2998 if (width < view->ncols - 1)
2999 waddch(view->window, '\n');
3001 *eof = 0;
3002 while (nprinted < max_lines - 2) {
3003 line = parse_next_line(f, &len);
3004 if (line == NULL) {
3005 *eof = 1;
3006 break;
3008 if (++lineno < *first_displayed_line) {
3009 free(line);
3010 continue;
3013 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3014 err = format_line(&wline, &width, line, wlimit);
3015 if (err) {
3016 free(line);
3017 return err;
3020 if (view->focussed && nprinted == selected_line - 1)
3021 wstandout(view->window);
3023 if (nlines > 0) {
3024 blame_line = &lines[lineno - 1];
3025 if (blame_line->annotated && prev_id &&
3026 got_object_id_cmp(prev_id, blame_line->id) == 0)
3027 waddstr(view->window, " ");
3028 else if (blame_line->annotated) {
3029 char *id_str;
3030 err = got_object_id_str(&id_str, blame_line->id);
3031 if (err) {
3032 free(line);
3033 free(wline);
3034 return err;
3036 wprintw(view->window, "%.8s ", id_str);
3037 free(id_str);
3038 prev_id = blame_line->id;
3039 } else {
3040 waddstr(view->window, "........ ");
3041 prev_id = NULL;
3043 } else {
3044 waddstr(view->window, "........ ");
3045 prev_id = NULL;
3048 waddwstr(view->window, wline);
3049 while (width < wlimit) {
3050 waddch(view->window, ' ');
3051 width++;
3053 if (view->focussed && nprinted == selected_line - 1)
3054 wstandend(view->window);
3055 if (++nprinted == 1)
3056 *first_displayed_line = lineno;
3057 free(line);
3058 free(wline);
3059 wline = NULL;
3061 *last_displayed_line = lineno;
3063 view_vborder(view);
3065 return NULL;
3068 static const struct got_error *
3069 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3071 const struct got_error *err = NULL;
3072 struct tog_blame_cb_args *a = arg;
3073 struct tog_blame_line *line;
3074 int errcode;
3076 if (nlines != a->nlines ||
3077 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3078 return got_error(GOT_ERR_RANGE);
3080 errcode = pthread_mutex_lock(&tog_mutex);
3081 if (errcode)
3082 return got_error_set_errno(errcode, "pthread_mutex_lock");
3084 if (*a->quit) { /* user has quit the blame view */
3085 err = got_error(GOT_ERR_ITER_COMPLETED);
3086 goto done;
3089 if (lineno == -1)
3090 goto done; /* no change in this commit */
3092 line = &a->lines[lineno - 1];
3093 if (line->annotated)
3094 goto done;
3096 line->id = got_object_id_dup(id);
3097 if (line->id == NULL) {
3098 err = got_error_from_errno("got_object_id_dup");
3099 goto done;
3101 line->annotated = 1;
3102 done:
3103 errcode = pthread_mutex_unlock(&tog_mutex);
3104 if (errcode)
3105 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3106 return err;
3109 static void *
3110 blame_thread(void *arg)
3112 const struct got_error *err;
3113 struct tog_blame_thread_args *ta = arg;
3114 struct tog_blame_cb_args *a = ta->cb_args;
3115 int errcode;
3117 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3118 blame_cb, ta->cb_args);
3120 errcode = pthread_mutex_lock(&tog_mutex);
3121 if (errcode)
3122 return (void *)got_error_set_errno(errcode,
3123 "pthread_mutex_lock");
3125 got_repo_close(ta->repo);
3126 ta->repo = NULL;
3127 *ta->complete = 1;
3129 errcode = pthread_mutex_unlock(&tog_mutex);
3130 if (errcode && err == NULL)
3131 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3133 return (void *)err;
3136 static struct got_object_id *
3137 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3138 int first_displayed_line, int selected_line)
3140 struct tog_blame_line *line;
3142 if (nlines <= 0)
3143 return NULL;
3145 line = &lines[first_displayed_line - 1 + selected_line - 1];
3146 if (!line->annotated)
3147 return NULL;
3149 return line->id;
3152 static const struct got_error *
3153 stop_blame(struct tog_blame *blame)
3155 const struct got_error *err = NULL;
3156 int i;
3158 if (blame->thread) {
3159 int errcode;
3160 errcode = pthread_mutex_unlock(&tog_mutex);
3161 if (errcode)
3162 return got_error_set_errno(errcode,
3163 "pthread_mutex_unlock");
3164 errcode = pthread_join(blame->thread, (void **)&err);
3165 if (errcode)
3166 return got_error_set_errno(errcode, "pthread_join");
3167 errcode = pthread_mutex_lock(&tog_mutex);
3168 if (errcode)
3169 return got_error_set_errno(errcode,
3170 "pthread_mutex_lock");
3171 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3172 err = NULL;
3173 blame->thread = NULL;
3175 if (blame->thread_args.repo) {
3176 got_repo_close(blame->thread_args.repo);
3177 blame->thread_args.repo = NULL;
3179 if (blame->f) {
3180 if (fclose(blame->f) != 0 && err == NULL)
3181 err = got_error_from_errno("fclose");
3182 blame->f = NULL;
3184 if (blame->lines) {
3185 for (i = 0; i < blame->nlines; i++)
3186 free(blame->lines[i].id);
3187 free(blame->lines);
3188 blame->lines = NULL;
3190 free(blame->cb_args.commit_id);
3191 blame->cb_args.commit_id = NULL;
3193 return err;
3196 static const struct got_error *
3197 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3198 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3199 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3200 struct got_repository *repo)
3202 const struct got_error *err = NULL;
3203 struct got_blob_object *blob = NULL;
3204 struct got_repository *thread_repo = NULL;
3205 struct got_object_id *obj_id = NULL;
3206 int obj_type;
3208 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3209 if (err)
3210 return err;
3211 if (obj_id == NULL)
3212 return got_error(GOT_ERR_NO_OBJ);
3214 err = got_object_get_type(&obj_type, repo, obj_id);
3215 if (err)
3216 goto done;
3218 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3219 err = got_error(GOT_ERR_OBJ_TYPE);
3220 goto done;
3223 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3224 if (err)
3225 goto done;
3226 blame->f = got_opentemp();
3227 if (blame->f == NULL) {
3228 err = got_error_from_errno("got_opentemp");
3229 goto done;
3231 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3232 &blame->line_offsets, blame->f, blob);
3233 if (err)
3234 goto done;
3236 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3237 if (blame->lines == NULL) {
3238 err = got_error_from_errno("calloc");
3239 goto done;
3242 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3243 if (err)
3244 goto done;
3246 blame->cb_args.view = view;
3247 blame->cb_args.lines = blame->lines;
3248 blame->cb_args.nlines = blame->nlines;
3249 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3250 if (blame->cb_args.commit_id == NULL) {
3251 err = got_error_from_errno("got_object_id_dup");
3252 goto done;
3254 blame->cb_args.quit = done;
3256 blame->thread_args.path = path;
3257 blame->thread_args.repo = thread_repo;
3258 blame->thread_args.cb_args = &blame->cb_args;
3259 blame->thread_args.complete = blame_complete;
3260 *blame_complete = 0;
3262 done:
3263 if (blob)
3264 got_object_blob_close(blob);
3265 free(obj_id);
3266 if (err)
3267 stop_blame(blame);
3268 return err;
3271 static const struct got_error *
3272 open_blame_view(struct tog_view *view, char *path,
3273 struct got_object_id *commit_id, struct got_reflist_head *refs,
3274 struct got_repository *repo)
3276 const struct got_error *err = NULL;
3277 struct tog_blame_view_state *s = &view->state.blame;
3279 SIMPLEQ_INIT(&s->blamed_commits);
3281 s->path = strdup(path);
3282 if (s->path == NULL)
3283 return got_error_from_errno("strdup");
3285 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3286 if (err) {
3287 free(s->path);
3288 return err;
3291 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3292 s->first_displayed_line = 1;
3293 s->last_displayed_line = view->nlines;
3294 s->selected_line = 1;
3295 s->blame_complete = 0;
3296 s->repo = repo;
3297 s->refs = refs;
3298 s->commit_id = commit_id;
3299 memset(&s->blame, 0, sizeof(s->blame));
3301 view->show = show_blame_view;
3302 view->input = input_blame_view;
3303 view->close = close_blame_view;
3304 view->search_start = search_start_blame_view;
3305 view->search_next = search_next_blame_view;
3307 return run_blame(&s->blame, view, &s->blame_complete,
3308 &s->first_displayed_line, &s->last_displayed_line,
3309 &s->selected_line, &s->done, &s->eof, s->path,
3310 s->blamed_commit->id, s->repo);
3313 static const struct got_error *
3314 close_blame_view(struct tog_view *view)
3316 const struct got_error *err = NULL;
3317 struct tog_blame_view_state *s = &view->state.blame;
3319 if (s->blame.thread)
3320 err = stop_blame(&s->blame);
3322 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3323 struct got_object_qid *blamed_commit;
3324 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3325 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3326 got_object_qid_free(blamed_commit);
3329 free(s->path);
3331 return err;
3334 static const struct got_error *
3335 search_start_blame_view(struct tog_view *view)
3337 struct tog_blame_view_state *s = &view->state.blame;
3339 s->matched_line = 0;
3340 return NULL;
3343 static int
3344 match_line(const char *line, regex_t *regex)
3346 regmatch_t regmatch;
3348 return regexec(regex, line, 1, &regmatch, 0) == 0;
3352 static const struct got_error *
3353 search_next_blame_view(struct tog_view *view)
3355 struct tog_blame_view_state *s = &view->state.blame;
3356 int lineno;
3358 if (!view->searching) {
3359 view->search_next_done = 1;
3360 return NULL;
3363 if (s->matched_line) {
3364 if (view->searching == TOG_SEARCH_FORWARD)
3365 lineno = s->matched_line + 1;
3366 else
3367 lineno = s->matched_line - 1;
3368 } else {
3369 if (view->searching == TOG_SEARCH_FORWARD)
3370 lineno = 1;
3371 else
3372 lineno = s->blame.nlines;
3375 while (1) {
3376 char *line = NULL;
3377 off_t offset;
3378 size_t len;
3380 if (lineno <= 0 || lineno > s->blame.nlines) {
3381 if (s->matched_line == 0) {
3382 view->search_next_done = 1;
3383 free(line);
3384 break;
3387 if (view->searching == TOG_SEARCH_FORWARD)
3388 lineno = 1;
3389 else
3390 lineno = s->blame.nlines;
3393 offset = s->blame.line_offsets[lineno - 1];
3394 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3395 free(line);
3396 return got_error_from_errno("fseeko");
3398 free(line);
3399 line = parse_next_line(s->blame.f, &len);
3400 if (line && match_line(line, &view->regex)) {
3401 view->search_next_done = 1;
3402 s->matched_line = lineno;
3403 free(line);
3404 break;
3406 free(line);
3407 if (view->searching == TOG_SEARCH_FORWARD)
3408 lineno++;
3409 else
3410 lineno--;
3413 if (s->matched_line) {
3414 s->first_displayed_line = s->matched_line;
3415 s->selected_line = 1;
3418 return NULL;
3421 static const struct got_error *
3422 show_blame_view(struct tog_view *view)
3424 const struct got_error *err = NULL;
3425 struct tog_blame_view_state *s = &view->state.blame;
3426 int errcode;
3428 if (s->blame.thread == NULL) {
3429 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3430 &s->blame.thread_args);
3431 if (errcode)
3432 return got_error_set_errno(errcode, "pthread_create");
3435 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3436 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3437 s->selected_line, &s->first_displayed_line,
3438 &s->last_displayed_line, &s->eof, view->nlines);
3440 view_vborder(view);
3441 return err;
3444 static const struct got_error *
3445 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3446 struct tog_view **focus_view, struct tog_view *view, int ch)
3448 const struct got_error *err = NULL, *thread_err = NULL;
3449 struct tog_view *diff_view;
3450 struct tog_blame_view_state *s = &view->state.blame;
3451 int begin_x = 0;
3453 switch (ch) {
3454 case 'q':
3455 s->done = 1;
3456 break;
3457 case 'k':
3458 case KEY_UP:
3459 if (s->selected_line > 1)
3460 s->selected_line--;
3461 else if (s->selected_line == 1 &&
3462 s->first_displayed_line > 1)
3463 s->first_displayed_line--;
3464 break;
3465 case KEY_PPAGE:
3466 if (s->first_displayed_line == 1) {
3467 s->selected_line = 1;
3468 break;
3470 if (s->first_displayed_line > view->nlines - 2)
3471 s->first_displayed_line -=
3472 (view->nlines - 2);
3473 else
3474 s->first_displayed_line = 1;
3475 break;
3476 case 'j':
3477 case KEY_DOWN:
3478 if (s->selected_line < view->nlines - 2 &&
3479 s->first_displayed_line +
3480 s->selected_line <= s->blame.nlines)
3481 s->selected_line++;
3482 else if (s->last_displayed_line <
3483 s->blame.nlines)
3484 s->first_displayed_line++;
3485 break;
3486 case 'b':
3487 case 'p': {
3488 struct got_object_id *id = NULL;
3489 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3490 s->first_displayed_line, s->selected_line);
3491 if (id == NULL)
3492 break;
3493 if (ch == 'p') {
3494 struct got_commit_object *commit;
3495 struct got_object_qid *pid;
3496 struct got_object_id *blob_id = NULL;
3497 int obj_type;
3498 err = got_object_open_as_commit(&commit,
3499 s->repo, id);
3500 if (err)
3501 break;
3502 pid = SIMPLEQ_FIRST(
3503 got_object_commit_get_parent_ids(commit));
3504 if (pid == NULL) {
3505 got_object_commit_close(commit);
3506 break;
3508 /* Check if path history ends here. */
3509 err = got_object_id_by_path(&blob_id, s->repo,
3510 pid->id, s->path);
3511 if (err) {
3512 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3513 err = NULL;
3514 got_object_commit_close(commit);
3515 break;
3517 err = got_object_get_type(&obj_type, s->repo,
3518 blob_id);
3519 free(blob_id);
3520 /* Can't blame non-blob type objects. */
3521 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3522 got_object_commit_close(commit);
3523 break;
3525 err = got_object_qid_alloc(&s->blamed_commit,
3526 pid->id);
3527 got_object_commit_close(commit);
3528 } else {
3529 if (got_object_id_cmp(id,
3530 s->blamed_commit->id) == 0)
3531 break;
3532 err = got_object_qid_alloc(&s->blamed_commit,
3533 id);
3535 if (err)
3536 break;
3537 s->done = 1;
3538 thread_err = stop_blame(&s->blame);
3539 s->done = 0;
3540 if (thread_err)
3541 break;
3542 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3543 s->blamed_commit, entry);
3544 err = run_blame(&s->blame, view, &s->blame_complete,
3545 &s->first_displayed_line, &s->last_displayed_line,
3546 &s->selected_line, &s->done, &s->eof,
3547 s->path, s->blamed_commit->id, s->repo);
3548 if (err)
3549 break;
3550 break;
3552 case 'B': {
3553 struct got_object_qid *first;
3554 first = SIMPLEQ_FIRST(&s->blamed_commits);
3555 if (!got_object_id_cmp(first->id, s->commit_id))
3556 break;
3557 s->done = 1;
3558 thread_err = stop_blame(&s->blame);
3559 s->done = 0;
3560 if (thread_err)
3561 break;
3562 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3563 got_object_qid_free(s->blamed_commit);
3564 s->blamed_commit =
3565 SIMPLEQ_FIRST(&s->blamed_commits);
3566 err = run_blame(&s->blame, view, &s->blame_complete,
3567 &s->first_displayed_line, &s->last_displayed_line,
3568 &s->selected_line, &s->done, &s->eof, s->path,
3569 s->blamed_commit->id, s->repo);
3570 if (err)
3571 break;
3572 break;
3574 case KEY_ENTER:
3575 case '\r': {
3576 struct got_object_id *id = NULL;
3577 struct got_object_qid *pid;
3578 struct got_commit_object *commit = NULL;
3579 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3580 s->first_displayed_line, s->selected_line);
3581 if (id == NULL)
3582 break;
3583 err = got_object_open_as_commit(&commit, s->repo, id);
3584 if (err)
3585 break;
3586 pid = SIMPLEQ_FIRST(
3587 got_object_commit_get_parent_ids(commit));
3588 if (view_is_parent_view(view))
3589 begin_x = view_split_begin_x(view->begin_x);
3590 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3591 if (diff_view == NULL) {
3592 got_object_commit_close(commit);
3593 err = got_error_from_errno("view_open");
3594 break;
3596 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3597 id, NULL, s->refs, s->repo);
3598 got_object_commit_close(commit);
3599 if (err) {
3600 view_close(diff_view);
3601 break;
3603 if (view_is_parent_view(view)) {
3604 err = view_close_child(view);
3605 if (err)
3606 break;
3607 err = view_set_child(view, diff_view);
3608 if (err) {
3609 view_close(diff_view);
3610 break;
3612 *focus_view = diff_view;
3613 view->child_focussed = 1;
3614 } else
3615 *new_view = diff_view;
3616 if (err)
3617 break;
3618 break;
3620 case KEY_NPAGE:
3621 case ' ':
3622 if (s->last_displayed_line >= s->blame.nlines &&
3623 s->selected_line >= MIN(s->blame.nlines,
3624 view->nlines - 2)) {
3625 break;
3627 if (s->last_displayed_line >= s->blame.nlines &&
3628 s->selected_line < view->nlines - 2) {
3629 s->selected_line = MIN(s->blame.nlines,
3630 view->nlines - 2);
3631 break;
3633 if (s->last_displayed_line + view->nlines - 2
3634 <= s->blame.nlines)
3635 s->first_displayed_line +=
3636 view->nlines - 2;
3637 else
3638 s->first_displayed_line =
3639 s->blame.nlines -
3640 (view->nlines - 3);
3641 break;
3642 case KEY_RESIZE:
3643 if (s->selected_line > view->nlines - 2) {
3644 s->selected_line = MIN(s->blame.nlines,
3645 view->nlines - 2);
3647 break;
3648 default:
3649 break;
3651 return thread_err ? thread_err : err;
3654 static const struct got_error *
3655 cmd_blame(int argc, char *argv[])
3657 const struct got_error *error;
3658 struct got_repository *repo = NULL;
3659 struct got_reflist_head refs;
3660 struct got_worktree *worktree = NULL;
3661 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3662 struct got_object_id *commit_id = NULL;
3663 char *commit_id_str = NULL;
3664 int ch;
3665 struct tog_view *view;
3667 SIMPLEQ_INIT(&refs);
3669 #ifndef PROFILE
3670 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3671 NULL) == -1)
3672 err(1, "pledge");
3673 #endif
3675 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3676 switch (ch) {
3677 case 'c':
3678 commit_id_str = optarg;
3679 break;
3680 case 'r':
3681 repo_path = realpath(optarg, NULL);
3682 if (repo_path == NULL)
3683 err(1, "-r option");
3684 break;
3685 default:
3686 usage_blame();
3687 /* NOTREACHED */
3691 argc -= optind;
3692 argv += optind;
3694 if (argc == 1)
3695 path = argv[0];
3696 else
3697 usage_blame();
3699 cwd = getcwd(NULL, 0);
3700 if (cwd == NULL) {
3701 error = got_error_from_errno("getcwd");
3702 goto done;
3704 if (repo_path == NULL) {
3705 error = got_worktree_open(&worktree, cwd);
3706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3707 goto done;
3708 else
3709 error = NULL;
3710 if (worktree) {
3711 repo_path =
3712 strdup(got_worktree_get_repo_path(worktree));
3713 if (repo_path == NULL)
3714 error = got_error_from_errno("strdup");
3715 if (error)
3716 goto done;
3717 } else {
3718 repo_path = strdup(cwd);
3719 if (repo_path == NULL) {
3720 error = got_error_from_errno("strdup");
3721 goto done;
3726 init_curses();
3728 error = got_repo_open(&repo, repo_path);
3729 if (error != NULL)
3730 goto done;
3732 error = apply_unveil(got_repo_get_path(repo), NULL);
3733 if (error)
3734 goto done;
3736 if (worktree) {
3737 const char *prefix = got_worktree_get_path_prefix(worktree);
3738 char *p, *worktree_subdir = cwd +
3739 strlen(got_worktree_get_root_path(worktree));
3740 if (asprintf(&p, "%s%s%s%s%s",
3741 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3742 worktree_subdir, worktree_subdir[0] ? "/" : "",
3743 path) == -1) {
3744 error = got_error_from_errno("asprintf");
3745 goto done;
3747 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3748 free(p);
3749 } else {
3750 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3752 if (error)
3753 goto done;
3755 if (commit_id_str == NULL) {
3756 struct got_reference *head_ref;
3757 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3758 if (error != NULL)
3759 goto done;
3760 error = got_ref_resolve(&commit_id, repo, head_ref);
3761 got_ref_close(head_ref);
3762 } else {
3763 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3764 if (error) {
3765 if (error->code != GOT_ERR_NOT_REF)
3766 goto done;
3767 error = got_repo_match_object_id_prefix(&commit_id,
3768 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3771 if (error != NULL)
3772 goto done;
3774 error = got_ref_list(&refs, repo);
3775 if (error)
3776 goto done;
3778 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3779 if (view == NULL) {
3780 error = got_error_from_errno("view_open");
3781 goto done;
3783 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3784 if (error)
3785 goto done;
3786 error = view_loop(view);
3787 done:
3788 free(repo_path);
3789 free(cwd);
3790 free(commit_id);
3791 if (worktree)
3792 got_worktree_close(worktree);
3793 if (repo)
3794 got_repo_close(repo);
3795 got_ref_list_free(&refs);
3796 return error;
3799 static const struct got_error *
3800 draw_tree_entries(struct tog_view *view,
3801 struct got_tree_entry **first_displayed_entry,
3802 struct got_tree_entry **last_displayed_entry,
3803 struct got_tree_entry **selected_entry, int *ndisplayed,
3804 const char *label, int show_ids, const char *parent_path,
3805 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3807 const struct got_error *err = NULL;
3808 struct got_tree_entry *te;
3809 wchar_t *wline;
3810 int width, n;
3812 *ndisplayed = 0;
3814 werase(view->window);
3816 if (limit == 0)
3817 return NULL;
3819 err = format_line(&wline, &width, label, view->ncols);
3820 if (err)
3821 return err;
3822 if (view_needs_focus_indication(view))
3823 wstandout(view->window);
3824 waddwstr(view->window, wline);
3825 if (view_needs_focus_indication(view))
3826 wstandend(view->window);
3827 free(wline);
3828 wline = NULL;
3829 if (width < view->ncols - 1)
3830 waddch(view->window, '\n');
3831 if (--limit <= 0)
3832 return NULL;
3833 err = format_line(&wline, &width, parent_path, view->ncols);
3834 if (err)
3835 return err;
3836 waddwstr(view->window, wline);
3837 free(wline);
3838 wline = NULL;
3839 if (width < view->ncols - 1)
3840 waddch(view->window, '\n');
3841 if (--limit <= 0)
3842 return NULL;
3843 waddch(view->window, '\n');
3844 if (--limit <= 0)
3845 return NULL;
3847 te = SIMPLEQ_FIRST(&entries->head);
3848 if (*first_displayed_entry == NULL) {
3849 if (selected == 0) {
3850 if (view->focussed)
3851 wstandout(view->window);
3852 *selected_entry = NULL;
3854 waddstr(view->window, " ..\n"); /* parent directory */
3855 if (selected == 0 && view->focussed)
3856 wstandend(view->window);
3857 (*ndisplayed)++;
3858 if (--limit <= 0)
3859 return NULL;
3860 n = 1;
3861 } else {
3862 n = 0;
3863 while (te != *first_displayed_entry)
3864 te = SIMPLEQ_NEXT(te, entry);
3867 while (te) {
3868 char *line = NULL, *id_str = NULL;
3869 const char *modestr = "";
3871 if (show_ids) {
3872 err = got_object_id_str(&id_str, te->id);
3873 if (err)
3874 return got_error_from_errno(
3875 "got_object_id_str");
3877 if (S_ISLNK(te->mode))
3878 modestr = "@";
3879 else if (S_ISDIR(te->mode))
3880 modestr = "/";
3881 else if (te->mode & S_IXUSR)
3882 modestr = "*";
3883 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3884 te->name, modestr) == -1) {
3885 free(id_str);
3886 return got_error_from_errno("asprintf");
3888 free(id_str);
3889 err = format_line(&wline, &width, line, view->ncols);
3890 if (err) {
3891 free(line);
3892 break;
3894 if (n == selected) {
3895 if (view->focussed)
3896 wstandout(view->window);
3897 *selected_entry = te;
3899 waddwstr(view->window, wline);
3900 if (width < view->ncols - 1)
3901 waddch(view->window, '\n');
3902 if (n == selected && view->focussed)
3903 wstandend(view->window);
3904 free(line);
3905 free(wline);
3906 wline = NULL;
3907 n++;
3908 (*ndisplayed)++;
3909 *last_displayed_entry = te;
3910 if (--limit <= 0)
3911 break;
3912 te = SIMPLEQ_NEXT(te, entry);
3915 return err;
3918 static void
3919 tree_scroll_up(struct tog_view *view,
3920 struct got_tree_entry **first_displayed_entry, int maxscroll,
3921 const struct got_tree_entries *entries, int isroot)
3923 struct got_tree_entry *te, *prev;
3924 int i;
3926 if (*first_displayed_entry == NULL)
3927 return;
3929 te = SIMPLEQ_FIRST(&entries->head);
3930 if (*first_displayed_entry == te) {
3931 if (!isroot)
3932 *first_displayed_entry = NULL;
3933 return;
3936 /* XXX this is stupid... switch to TAILQ? */
3937 for (i = 0; i < maxscroll; i++) {
3938 while (te != *first_displayed_entry) {
3939 prev = te;
3940 te = SIMPLEQ_NEXT(te, entry);
3942 *first_displayed_entry = prev;
3943 te = SIMPLEQ_FIRST(&entries->head);
3945 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3946 *first_displayed_entry = NULL;
3949 static int
3950 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3951 struct got_tree_entry *last_displayed_entry,
3952 const struct got_tree_entries *entries)
3954 struct got_tree_entry *next, *last;
3955 int n = 0;
3957 if (*first_displayed_entry)
3958 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3959 else
3960 next = SIMPLEQ_FIRST(&entries->head);
3961 last = last_displayed_entry;
3962 while (next && last && n++ < maxscroll) {
3963 last = SIMPLEQ_NEXT(last, entry);
3964 if (last) {
3965 *first_displayed_entry = next;
3966 next = SIMPLEQ_NEXT(next, entry);
3969 return n;
3972 static const struct got_error *
3973 tree_entry_path(char **path, struct tog_parent_trees *parents,
3974 struct got_tree_entry *te)
3976 const struct got_error *err = NULL;
3977 struct tog_parent_tree *pt;
3978 size_t len = 2; /* for leading slash and NUL */
3980 TAILQ_FOREACH(pt, parents, entry)
3981 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3982 if (te)
3983 len += strlen(te->name);
3985 *path = calloc(1, len);
3986 if (path == NULL)
3987 return got_error_from_errno("calloc");
3989 (*path)[0] = '/';
3990 pt = TAILQ_LAST(parents, tog_parent_trees);
3991 while (pt) {
3992 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3993 err = got_error(GOT_ERR_NO_SPACE);
3994 goto done;
3996 if (strlcat(*path, "/", len) >= len) {
3997 err = got_error(GOT_ERR_NO_SPACE);
3998 goto done;
4000 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4002 if (te) {
4003 if (strlcat(*path, te->name, len) >= len) {
4004 err = got_error(GOT_ERR_NO_SPACE);
4005 goto done;
4008 done:
4009 if (err) {
4010 free(*path);
4011 *path = NULL;
4013 return err;
4016 static const struct got_error *
4017 blame_tree_entry(struct tog_view **new_view, int begin_x,
4018 struct got_tree_entry *te, struct tog_parent_trees *parents,
4019 struct got_object_id *commit_id, struct got_reflist_head *refs,
4020 struct got_repository *repo)
4022 const struct got_error *err = NULL;
4023 char *path;
4024 struct tog_view *blame_view;
4026 *new_view = NULL;
4028 err = tree_entry_path(&path, parents, te);
4029 if (err)
4030 return err;
4032 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4033 if (blame_view == NULL) {
4034 err = got_error_from_errno("view_open");
4035 goto done;
4038 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4039 if (err) {
4040 view_close(blame_view);
4041 } else
4042 *new_view = blame_view;
4043 done:
4044 free(path);
4045 return err;
4048 static const struct got_error *
4049 log_tree_entry(struct tog_view **new_view, int begin_x,
4050 struct got_tree_entry *te, struct tog_parent_trees *parents,
4051 struct got_object_id *commit_id, struct got_reflist_head *refs,
4052 struct got_repository *repo)
4054 struct tog_view *log_view;
4055 const struct got_error *err = NULL;
4056 char *path;
4058 *new_view = NULL;
4060 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4061 if (log_view == NULL)
4062 return got_error_from_errno("view_open");
4064 err = tree_entry_path(&path, parents, te);
4065 if (err)
4066 return err;
4068 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4069 if (err)
4070 view_close(log_view);
4071 else
4072 *new_view = log_view;
4073 free(path);
4074 return err;
4077 static const struct got_error *
4078 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4079 struct got_object_id *commit_id, struct got_reflist_head *refs,
4080 struct got_repository *repo)
4082 const struct got_error *err = NULL;
4083 char *commit_id_str = NULL;
4084 struct tog_tree_view_state *s = &view->state.tree;
4086 TAILQ_INIT(&s->parents);
4088 err = got_object_id_str(&commit_id_str, commit_id);
4089 if (err != NULL)
4090 goto done;
4092 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4093 err = got_error_from_errno("asprintf");
4094 goto done;
4097 s->root = s->tree = root;
4098 s->entries = got_object_tree_get_entries(root);
4099 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4100 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4101 s->commit_id = got_object_id_dup(commit_id);
4102 if (s->commit_id == NULL) {
4103 err = got_error_from_errno("got_object_id_dup");
4104 goto done;
4106 s->refs = refs;
4107 s->repo = repo;
4109 view->show = show_tree_view;
4110 view->input = input_tree_view;
4111 view->close = close_tree_view;
4112 view->search_start = search_start_tree_view;
4113 view->search_next = search_next_tree_view;
4114 done:
4115 free(commit_id_str);
4116 if (err) {
4117 free(s->tree_label);
4118 s->tree_label = NULL;
4120 return err;
4123 static const struct got_error *
4124 close_tree_view(struct tog_view *view)
4126 struct tog_tree_view_state *s = &view->state.tree;
4128 free(s->tree_label);
4129 s->tree_label = NULL;
4130 free(s->commit_id);
4131 s->commit_id = NULL;
4132 while (!TAILQ_EMPTY(&s->parents)) {
4133 struct tog_parent_tree *parent;
4134 parent = TAILQ_FIRST(&s->parents);
4135 TAILQ_REMOVE(&s->parents, parent, entry);
4136 free(parent);
4139 if (s->tree != s->root)
4140 got_object_tree_close(s->tree);
4141 got_object_tree_close(s->root);
4143 return NULL;
4146 static const struct got_error *
4147 search_start_tree_view(struct tog_view *view)
4149 struct tog_tree_view_state *s = &view->state.tree;
4151 s->matched_entry = NULL;
4152 return NULL;
4155 static int
4156 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4158 regmatch_t regmatch;
4160 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4163 static const struct got_error *
4164 search_next_tree_view(struct tog_view *view)
4166 struct tog_tree_view_state *s = &view->state.tree;
4167 struct got_tree_entry *entry = NULL, *te;
4169 if (!view->searching) {
4170 view->search_next_done = 1;
4171 return NULL;
4174 if (s->matched_entry) {
4175 if (view->searching == TOG_SEARCH_FORWARD) {
4176 if (s->selected_entry)
4177 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4178 else
4179 entry = SIMPLEQ_FIRST(&s->entries->head);
4181 else {
4182 if (s->selected_entry == NULL) {
4183 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4184 entry = te;
4185 } else {
4186 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4187 entry = te;
4188 if (SIMPLEQ_NEXT(te, entry) ==
4189 s->selected_entry)
4190 break;
4194 } else {
4195 if (view->searching == TOG_SEARCH_FORWARD)
4196 entry = SIMPLEQ_FIRST(&s->entries->head);
4197 else {
4198 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4199 entry = te;
4203 while (1) {
4204 if (entry == NULL) {
4205 if (s->matched_entry == NULL) {
4206 view->search_next_done = 1;
4207 return NULL;
4209 if (view->searching == TOG_SEARCH_FORWARD)
4210 entry = SIMPLEQ_FIRST(&s->entries->head);
4211 else {
4212 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4213 entry = te;
4217 if (match_tree_entry(entry, &view->regex)) {
4218 view->search_next_done = 1;
4219 s->matched_entry = entry;
4220 break;
4223 if (view->searching == TOG_SEARCH_FORWARD)
4224 entry = SIMPLEQ_NEXT(entry, entry);
4225 else {
4226 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4227 entry = NULL;
4228 else {
4229 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4230 if (SIMPLEQ_NEXT(te, entry) == entry) {
4231 entry = te;
4232 break;
4239 if (s->matched_entry) {
4240 s->first_displayed_entry = s->matched_entry;
4241 s->selected = 0;
4244 return NULL;
4247 static const struct got_error *
4248 show_tree_view(struct tog_view *view)
4250 const struct got_error *err = NULL;
4251 struct tog_tree_view_state *s = &view->state.tree;
4252 char *parent_path;
4254 err = tree_entry_path(&parent_path, &s->parents, NULL);
4255 if (err)
4256 return err;
4258 err = draw_tree_entries(view, &s->first_displayed_entry,
4259 &s->last_displayed_entry, &s->selected_entry,
4260 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4261 s->entries, s->selected, view->nlines, s->tree == s->root);
4262 free(parent_path);
4264 view_vborder(view);
4265 return err;
4268 static const struct got_error *
4269 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4270 struct tog_view **focus_view, struct tog_view *view, int ch)
4272 const struct got_error *err = NULL;
4273 struct tog_tree_view_state *s = &view->state.tree;
4274 struct tog_view *log_view;
4275 int begin_x = 0, nscrolled;
4277 switch (ch) {
4278 case 'i':
4279 s->show_ids = !s->show_ids;
4280 break;
4281 case 'l':
4282 if (!s->selected_entry)
4283 break;
4284 if (view_is_parent_view(view))
4285 begin_x = view_split_begin_x(view->begin_x);
4286 err = log_tree_entry(&log_view, begin_x,
4287 s->selected_entry, &s->parents,
4288 s->commit_id, s->refs, s->repo);
4289 if (view_is_parent_view(view)) {
4290 err = view_close_child(view);
4291 if (err)
4292 return err;
4293 err = view_set_child(view, log_view);
4294 if (err) {
4295 view_close(log_view);
4296 break;
4298 *focus_view = log_view;
4299 view->child_focussed = 1;
4300 } else
4301 *new_view = log_view;
4302 break;
4303 case 'k':
4304 case KEY_UP:
4305 if (s->selected > 0) {
4306 s->selected--;
4307 if (s->selected == 0)
4308 break;
4310 if (s->selected > 0)
4311 break;
4312 tree_scroll_up(view, &s->first_displayed_entry, 1,
4313 s->entries, s->tree == s->root);
4314 break;
4315 case KEY_PPAGE:
4316 tree_scroll_up(view, &s->first_displayed_entry,
4317 MAX(0, view->nlines - 4 - s->selected), s->entries,
4318 s->tree == s->root);
4319 s->selected = 0;
4320 if (SIMPLEQ_FIRST(&s->entries->head) ==
4321 s->first_displayed_entry && s->tree != s->root)
4322 s->first_displayed_entry = NULL;
4323 break;
4324 case 'j':
4325 case KEY_DOWN:
4326 if (s->selected < s->ndisplayed - 1) {
4327 s->selected++;
4328 break;
4330 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4331 /* can't scroll any further */
4332 break;
4333 tree_scroll_down(&s->first_displayed_entry, 1,
4334 s->last_displayed_entry, s->entries);
4335 break;
4336 case KEY_NPAGE:
4337 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4338 == NULL) {
4339 /* can't scroll any further; move cursor down */
4340 if (s->selected < s->ndisplayed - 1)
4341 s->selected = s->ndisplayed - 1;
4342 break;
4344 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4345 view->nlines, s->last_displayed_entry, s->entries);
4346 if (nscrolled < view->nlines) {
4347 int ndisplayed = 0;
4348 struct got_tree_entry *te;
4349 te = s->first_displayed_entry;
4350 do {
4351 ndisplayed++;
4352 te = SIMPLEQ_NEXT(te, entry);
4353 } while (te);
4354 s->selected = ndisplayed - 1;
4356 break;
4357 case KEY_ENTER:
4358 case '\r':
4359 case KEY_BACKSPACE:
4360 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4361 struct tog_parent_tree *parent;
4362 /* user selected '..' */
4363 if (s->tree == s->root)
4364 break;
4365 parent = TAILQ_FIRST(&s->parents);
4366 TAILQ_REMOVE(&s->parents, parent,
4367 entry);
4368 got_object_tree_close(s->tree);
4369 s->tree = parent->tree;
4370 s->entries =
4371 got_object_tree_get_entries(s->tree);
4372 s->first_displayed_entry =
4373 parent->first_displayed_entry;
4374 s->selected_entry =
4375 parent->selected_entry;
4376 s->selected = parent->selected;
4377 free(parent);
4378 } else if (S_ISDIR(s->selected_entry->mode)) {
4379 struct got_tree_object *subtree;
4380 err = got_object_open_as_tree(&subtree,
4381 s->repo, s->selected_entry->id);
4382 if (err)
4383 break;
4384 err = tree_view_visit_subtree(subtree, s);
4385 if (err) {
4386 got_object_tree_close(subtree);
4387 break;
4389 } else if (S_ISREG(s->selected_entry->mode)) {
4390 struct tog_view *blame_view;
4391 int begin_x = view_is_parent_view(view) ?
4392 view_split_begin_x(view->begin_x) : 0;
4394 err = blame_tree_entry(&blame_view, begin_x,
4395 s->selected_entry, &s->parents,
4396 s->commit_id, s->refs, s->repo);
4397 if (err)
4398 break;
4399 if (view_is_parent_view(view)) {
4400 err = view_close_child(view);
4401 if (err)
4402 return err;
4403 err = view_set_child(view, blame_view);
4404 if (err) {
4405 view_close(blame_view);
4406 break;
4408 *focus_view = blame_view;
4409 view->child_focussed = 1;
4410 } else
4411 *new_view = blame_view;
4413 break;
4414 case KEY_RESIZE:
4415 if (s->selected > view->nlines)
4416 s->selected = s->ndisplayed - 1;
4417 break;
4418 default:
4419 break;
4422 return err;
4425 __dead static void
4426 usage_tree(void)
4428 endwin();
4429 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4430 getprogname());
4431 exit(1);
4434 static const struct got_error *
4435 cmd_tree(int argc, char *argv[])
4437 const struct got_error *error;
4438 struct got_repository *repo = NULL;
4439 struct got_reflist_head refs;
4440 char *repo_path = NULL;
4441 struct got_object_id *commit_id = NULL;
4442 char *commit_id_arg = NULL;
4443 struct got_commit_object *commit = NULL;
4444 struct got_tree_object *tree = NULL;
4445 int ch;
4446 struct tog_view *view;
4448 SIMPLEQ_INIT(&refs);
4450 #ifndef PROFILE
4451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4452 NULL) == -1)
4453 err(1, "pledge");
4454 #endif
4456 while ((ch = getopt(argc, argv, "c:")) != -1) {
4457 switch (ch) {
4458 case 'c':
4459 commit_id_arg = optarg;
4460 break;
4461 default:
4462 usage_tree();
4463 /* NOTREACHED */
4467 argc -= optind;
4468 argv += optind;
4470 if (argc == 0) {
4471 struct got_worktree *worktree;
4472 char *cwd = getcwd(NULL, 0);
4473 if (cwd == NULL)
4474 return got_error_from_errno("getcwd");
4475 error = got_worktree_open(&worktree, cwd);
4476 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4477 goto done;
4478 if (worktree) {
4479 free(cwd);
4480 repo_path =
4481 strdup(got_worktree_get_repo_path(worktree));
4482 got_worktree_close(worktree);
4483 } else
4484 repo_path = cwd;
4485 if (repo_path == NULL) {
4486 error = got_error_from_errno("strdup");
4487 goto done;
4489 } else if (argc == 1) {
4490 repo_path = realpath(argv[0], NULL);
4491 if (repo_path == NULL)
4492 return got_error_from_errno2("realpath", argv[0]);
4493 } else
4494 usage_log();
4496 init_curses();
4498 error = got_repo_open(&repo, repo_path);
4499 if (error != NULL)
4500 goto done;
4502 error = apply_unveil(got_repo_get_path(repo), NULL);
4503 if (error)
4504 goto done;
4506 if (commit_id_arg == NULL)
4507 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4508 else {
4509 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4510 if (error) {
4511 if (error->code != GOT_ERR_NOT_REF)
4512 goto done;
4513 error = got_repo_match_object_id_prefix(&commit_id,
4514 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4517 if (error != NULL)
4518 goto done;
4520 error = got_object_open_as_commit(&commit, repo, commit_id);
4521 if (error != NULL)
4522 goto done;
4524 error = got_object_open_as_tree(&tree, repo,
4525 got_object_commit_get_tree_id(commit));
4526 if (error != NULL)
4527 goto done;
4529 error = got_ref_list(&refs, repo);
4530 if (error)
4531 goto done;
4533 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4534 if (view == NULL) {
4535 error = got_error_from_errno("view_open");
4536 goto done;
4538 error = open_tree_view(view, tree, commit_id, &refs, repo);
4539 if (error)
4540 goto done;
4541 error = view_loop(view);
4542 done:
4543 free(repo_path);
4544 free(commit_id);
4545 if (commit)
4546 got_object_commit_close(commit);
4547 if (tree)
4548 got_object_tree_close(tree);
4549 if (repo)
4550 got_repo_close(repo);
4551 got_ref_list_free(&refs);
4552 return error;
4555 static void
4556 list_commands(void)
4558 int i;
4560 fprintf(stderr, "commands:");
4561 for (i = 0; i < nitems(tog_commands); i++) {
4562 struct tog_cmd *cmd = &tog_commands[i];
4563 fprintf(stderr, " %s", cmd->name);
4565 fputc('\n', stderr);
4568 __dead static void
4569 usage(int hflag)
4571 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4572 getprogname());
4573 if (hflag)
4574 list_commands();
4575 exit(1);
4578 static char **
4579 make_argv(const char *arg0, const char *arg1)
4581 char **argv;
4582 int argc = (arg1 == NULL ? 1 : 2);
4584 argv = calloc(argc, sizeof(char *));
4585 if (argv == NULL)
4586 err(1, "calloc");
4587 argv[0] = strdup(arg0);
4588 if (argv[0] == NULL)
4589 err(1, "calloc");
4590 if (arg1) {
4591 argv[1] = strdup(arg1);
4592 if (argv[1] == NULL)
4593 err(1, "calloc");
4596 return argv;
4599 int
4600 main(int argc, char *argv[])
4602 const struct got_error *error = NULL;
4603 struct tog_cmd *cmd = NULL;
4604 int ch, hflag = 0, Vflag = 0;
4605 char **cmd_argv = NULL;
4607 setlocale(LC_CTYPE, "");
4609 while ((ch = getopt(argc, argv, "hV")) != -1) {
4610 switch (ch) {
4611 case 'h':
4612 hflag = 1;
4613 break;
4614 case 'V':
4615 Vflag = 1;
4616 break;
4617 default:
4618 usage(hflag);
4619 /* NOTREACHED */
4623 argc -= optind;
4624 argv += optind;
4625 optind = 0;
4626 optreset = 1;
4628 if (Vflag) {
4629 got_version_print_str();
4630 return 1;
4633 if (argc == 0) {
4634 if (hflag)
4635 usage(hflag);
4636 /* Build an argument vector which runs a default command. */
4637 cmd = &tog_commands[0];
4638 cmd_argv = make_argv(cmd->name, NULL);
4639 argc = 1;
4640 } else {
4641 int i;
4643 /* Did the user specific a command? */
4644 for (i = 0; i < nitems(tog_commands); i++) {
4645 if (strncmp(tog_commands[i].name, argv[0],
4646 strlen(argv[0])) == 0) {
4647 cmd = &tog_commands[i];
4648 break;
4652 if (cmd == NULL) {
4653 fprintf(stderr, "%s: unknown command '%s'\n",
4654 getprogname(), argv[0]);
4655 list_commands();
4656 return 1;
4660 if (hflag)
4661 cmd->cmd_usage();
4662 else
4663 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4665 endwin();
4666 free(cmd_argv);
4667 if (error)
4668 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4669 return 0;