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_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 int *searching;
144 int *search_next_done;
145 regex_t *regex;
146 };
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
153 int selected;
154 char *in_repo_path;
155 const char *head_ref_name;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
159 sig_atomic_t quit;
160 pthread_t thread;
161 struct tog_log_thread_args thread_args;
162 struct commit_queue_entry *matched_entry;
163 struct commit_queue_entry *search_entry;
164 };
166 struct tog_blame_cb_args {
167 struct tog_blame_line *lines; /* one per line */
168 int nlines;
170 struct tog_view *view;
171 struct got_object_id *commit_id;
172 int *quit;
173 };
175 struct tog_blame_thread_args {
176 const char *path;
177 struct got_repository *repo;
178 struct tog_blame_cb_args *cb_args;
179 int *complete;
180 got_cancel_cb cancel_cb;
181 void *cancel_arg;
182 };
184 struct tog_blame {
185 FILE *f;
186 size_t filesize;
187 struct tog_blame_line *lines;
188 int nlines;
189 off_t *line_offsets;
190 pthread_t thread;
191 struct tog_blame_thread_args thread_args;
192 struct tog_blame_cb_args cb_args;
193 const char *path;
194 };
196 struct tog_blame_view_state {
197 int first_displayed_line;
198 int last_displayed_line;
199 int selected_line;
200 int blame_complete;
201 int eof;
202 int done;
203 struct got_object_id_queue blamed_commits;
204 struct got_object_qid *blamed_commit;
205 char *path;
206 struct got_repository *repo;
207 struct got_reflist_head *refs;
208 struct got_object_id *commit_id;
209 struct tog_blame blame;
210 int matched_line;
211 };
213 struct tog_parent_tree {
214 TAILQ_ENTRY(tog_parent_tree) entry;
215 struct got_tree_object *tree;
216 struct got_tree_entry *first_displayed_entry;
217 struct got_tree_entry *selected_entry;
218 int selected;
219 };
221 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
223 struct tog_tree_view_state {
224 char *tree_label;
225 struct got_tree_object *root;
226 struct got_tree_object *tree;
227 const struct got_tree_entries *entries;
228 struct got_tree_entry *first_displayed_entry;
229 struct got_tree_entry *last_displayed_entry;
230 struct got_tree_entry *selected_entry;
231 int ndisplayed, selected, show_ids;
232 struct tog_parent_trees parents;
233 struct got_object_id *commit_id;
234 struct got_repository *repo;
235 struct got_reflist_head *refs;
236 struct got_tree_entry *matched_entry;
237 };
239 /*
240 * We implement two types of views: parent views and child views.
242 * The 'Tab' key switches between a parent view and its child view.
243 * Child views are shown side-by-side to their parent view, provided
244 * there is enough screen estate.
246 * When a new view is opened from within a parent view, this new view
247 * becomes a child view of the parent view, replacing any existing child.
249 * When a new view is opened from within a child view, this new view
250 * becomes a parent view which will obscure the views below until the
251 * user quits the new parent view by typing 'q'.
253 * This list of views contains parent views only.
254 * Child views are only pointed to by their parent view.
255 */
256 TAILQ_HEAD(tog_view_list_head, tog_view);
258 struct tog_view {
259 TAILQ_ENTRY(tog_view) entry;
260 WINDOW *window;
261 PANEL *panel;
262 int nlines, ncols, begin_y, begin_x;
263 int lines, cols; /* copies of LINES and COLS */
264 int focussed;
265 struct tog_view *parent;
266 struct tog_view *child;
267 int child_focussed;
269 /* type-specific state */
270 enum tog_view_type type;
271 union {
272 struct tog_diff_view_state diff;
273 struct tog_log_view_state log;
274 struct tog_blame_view_state blame;
275 struct tog_tree_view_state tree;
276 } state;
278 const struct got_error *(*show)(struct tog_view *);
279 const struct got_error *(*input)(struct tog_view **,
280 struct tog_view **, struct tog_view**, struct tog_view *, int);
281 const struct got_error *(*close)(struct tog_view *);
283 const struct got_error *(*search_start)(struct tog_view *);
284 const struct got_error *(*search_next)(struct tog_view *);
285 int searching;
286 #define TOG_SEARCH_FORWARD 1
287 #define TOG_SEARCH_BACKWARD 2
288 int search_next_done;
289 regex_t regex;
290 };
292 static const struct got_error *open_diff_view(struct tog_view *,
293 struct got_object_id *, struct got_object_id *, struct tog_view *,
294 struct got_reflist_head *, struct got_repository *);
295 static const struct got_error *show_diff_view(struct tog_view *);
296 static const struct got_error *input_diff_view(struct tog_view **,
297 struct tog_view **, struct tog_view **, struct tog_view *, int);
298 static const struct got_error* close_diff_view(struct tog_view *);
300 static const struct got_error *open_log_view(struct tog_view *,
301 struct got_object_id *, struct got_reflist_head *,
302 struct got_repository *, const char *, const char *, int);
303 static const struct got_error * show_log_view(struct tog_view *);
304 static const struct got_error *input_log_view(struct tog_view **,
305 struct tog_view **, struct tog_view **, struct tog_view *, int);
306 static const struct got_error *close_log_view(struct tog_view *);
307 static const struct got_error *search_start_log_view(struct tog_view *);
308 static const struct got_error *search_next_log_view(struct tog_view *);
310 static const struct got_error *open_blame_view(struct tog_view *, char *,
311 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
312 static const struct got_error *show_blame_view(struct tog_view *);
313 static const struct got_error *input_blame_view(struct tog_view **,
314 struct tog_view **, struct tog_view **, struct tog_view *, int);
315 static const struct got_error *close_blame_view(struct tog_view *);
316 static const struct got_error *search_start_blame_view(struct tog_view *);
317 static const struct got_error *search_next_blame_view(struct tog_view *);
319 static const struct got_error *open_tree_view(struct tog_view *,
320 struct got_tree_object *, struct got_object_id *,
321 struct got_reflist_head *, struct got_repository *);
322 static const struct got_error *show_tree_view(struct tog_view *);
323 static const struct got_error *input_tree_view(struct tog_view **,
324 struct tog_view **, struct tog_view **, struct tog_view *, int);
325 static const struct got_error *close_tree_view(struct tog_view *);
326 static const struct got_error *search_start_tree_view(struct tog_view *);
327 static const struct got_error *search_next_tree_view(struct tog_view *);
329 static volatile sig_atomic_t tog_sigwinch_received;
330 static volatile sig_atomic_t tog_sigpipe_received;
332 static void
333 tog_sigwinch(int signo)
335 tog_sigwinch_received = 1;
338 static void
339 tog_sigpipe(int signo)
341 tog_sigpipe_received = 1;
344 static const struct got_error *
345 view_close(struct tog_view *view)
347 const struct got_error *err = NULL;
349 if (view->child) {
350 view_close(view->child);
351 view->child = NULL;
353 if (view->close)
354 err = view->close(view);
355 if (view->panel)
356 del_panel(view->panel);
357 if (view->window)
358 delwin(view->window);
359 free(view);
360 return err;
363 static struct tog_view *
364 view_open(int nlines, int ncols, int begin_y, int begin_x,
365 enum tog_view_type type)
367 struct tog_view *view = calloc(1, sizeof(*view));
369 if (view == NULL)
370 return NULL;
372 view->type = type;
373 view->lines = LINES;
374 view->cols = COLS;
375 view->nlines = nlines ? nlines : LINES - begin_y;
376 view->ncols = ncols ? ncols : COLS - begin_x;
377 view->begin_y = begin_y;
378 view->begin_x = begin_x;
379 view->window = newwin(nlines, ncols, begin_y, begin_x);
380 if (view->window == NULL) {
381 view_close(view);
382 return NULL;
384 view->panel = new_panel(view->window);
385 if (view->panel == NULL ||
386 set_panel_userptr(view->panel, view) != OK) {
387 view_close(view);
388 return NULL;
391 keypad(view->window, TRUE);
392 return view;
395 static int
396 view_split_begin_x(int begin_x)
398 if (begin_x > 0 || COLS < 120)
399 return 0;
400 return (COLS - MAX(COLS / 2, 80));
403 static const struct got_error *view_resize(struct tog_view *);
405 static const struct got_error *
406 view_splitscreen(struct tog_view *view)
408 const struct got_error *err = NULL;
410 view->begin_y = 0;
411 view->begin_x = view_split_begin_x(0);
412 view->nlines = LINES;
413 view->ncols = COLS - view->begin_x;
414 view->lines = LINES;
415 view->cols = COLS;
416 err = view_resize(view);
417 if (err)
418 return err;
420 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
421 return got_error_from_errno("mvwin");
423 return NULL;
426 static const struct got_error *
427 view_fullscreen(struct tog_view *view)
429 const struct got_error *err = NULL;
431 view->begin_x = 0;
432 view->begin_y = 0;
433 view->nlines = LINES;
434 view->ncols = COLS;
435 view->lines = LINES;
436 view->cols = COLS;
437 err = view_resize(view);
438 if (err)
439 return err;
441 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
442 return got_error_from_errno("mvwin");
444 return NULL;
447 static int
448 view_is_parent_view(struct tog_view *view)
450 return view->parent == NULL;
453 static const struct got_error *
454 view_resize(struct tog_view *view)
456 int nlines, ncols;
458 if (view->lines > LINES)
459 nlines = view->nlines - (view->lines - LINES);
460 else
461 nlines = view->nlines + (LINES - view->lines);
463 if (view->cols > COLS)
464 ncols = view->ncols - (view->cols - COLS);
465 else
466 ncols = view->ncols + (COLS - view->cols);
468 if (wresize(view->window, nlines, ncols) == ERR)
469 return got_error_from_errno("wresize");
470 if (replace_panel(view->panel, view->window) == ERR)
471 return got_error_from_errno("replace_panel");
472 wclear(view->window);
474 view->nlines = nlines;
475 view->ncols = ncols;
476 view->lines = LINES;
477 view->cols = COLS;
479 if (view->child) {
480 view->child->begin_x = view_split_begin_x(view->begin_x);
481 if (view->child->begin_x == 0) {
482 view_fullscreen(view->child);
483 if (view->child->focussed)
484 show_panel(view->child->panel);
485 else
486 show_panel(view->panel);
487 } else {
488 view_splitscreen(view->child);
489 show_panel(view->child->panel);
493 return NULL;
496 static const struct got_error *
497 view_close_child(struct tog_view *view)
499 const struct got_error *err = NULL;
501 if (view->child == NULL)
502 return NULL;
504 err = view_close(view->child);
505 view->child = NULL;
506 return err;
509 static const struct got_error *
510 view_set_child(struct tog_view *view, struct tog_view *child)
512 const struct got_error *err = NULL;
514 view->child = child;
515 child->parent = view;
516 return err;
519 static int
520 view_is_splitscreen(struct tog_view *view)
522 return view->begin_x > 0;
525 static void
526 tog_resizeterm(void)
528 int cols, lines;
529 struct winsize size;
531 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
532 cols = 80; /* Default */
533 lines = 24;
534 } else {
535 cols = size.ws_col;
536 lines = size.ws_row;
538 resize_term(lines, cols);
541 static const struct got_error *
542 view_search_start(struct tog_view *view)
544 const struct got_error *err = NULL;
545 char pattern[1024];
546 int ret;
547 int begin_x = 0;
549 if (view->nlines < 1)
550 return NULL;
552 if (!view_is_parent_view(view))
553 begin_x = view_split_begin_x(view->begin_x);
554 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
555 begin_x, "/");
556 wclrtoeol(view->window);
558 nocbreak();
559 echo();
560 ret = wgetnstr(view->window, pattern, sizeof(pattern));
561 cbreak();
562 noecho();
563 if (ret == ERR)
564 return NULL;
566 if (view->searching) {
567 regfree(&view->regex);
568 view->searching = 0;
571 if (regcomp(&view->regex, pattern,
572 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
573 err = view->search_start(view);
574 if (err) {
575 regfree(&view->regex);
576 return err;
578 view->searching = TOG_SEARCH_FORWARD;
579 view->search_next_done = 0;
580 view->search_next(view);
583 return NULL;
586 static const struct got_error *
587 view_input(struct tog_view **new, struct tog_view **dead,
588 struct tog_view **focus, int *done, struct tog_view *view,
589 struct tog_view_list_head *views)
591 const struct got_error *err = NULL;
592 struct tog_view *v;
593 int ch, errcode;
595 *new = NULL;
596 *dead = NULL;
597 *focus = NULL;
599 if (view->searching && !view->search_next_done) {
600 errcode = pthread_mutex_unlock(&tog_mutex);
601 if (errcode)
602 return got_error_set_errno(errcode,
603 "pthread_mutex_unlock");
604 pthread_yield();
605 errcode = pthread_mutex_lock(&tog_mutex);
606 if (errcode)
607 return got_error_set_errno(errcode,
608 "pthread_mutex_lock");
609 view->search_next(view);
610 return NULL;
613 nodelay(stdscr, FALSE);
614 /* Allow threads to make progress while we are waiting for input. */
615 errcode = pthread_mutex_unlock(&tog_mutex);
616 if (errcode)
617 return got_error_set_errno(errcode, "pthread_mutex_unlock");
618 ch = wgetch(view->window);
619 errcode = pthread_mutex_lock(&tog_mutex);
620 if (errcode)
621 return got_error_set_errno(errcode, "pthread_mutex_lock");
622 nodelay(stdscr, TRUE);
624 if (tog_sigwinch_received) {
625 tog_resizeterm();
626 tog_sigwinch_received = 0;
627 TAILQ_FOREACH(v, views, entry) {
628 err = view_resize(v);
629 if (err)
630 return err;
631 err = v->input(new, dead, focus, v, KEY_RESIZE);
632 if (err)
633 return err;
637 switch (ch) {
638 case ERR:
639 break;
640 case '\t':
641 if (view->child) {
642 *focus = view->child;
643 view->child_focussed = 1;
644 } else if (view->parent) {
645 *focus = view->parent;
646 view->parent->child_focussed = 0;
648 break;
649 case 'q':
650 err = view->input(new, dead, focus, view, ch);
651 *dead = view;
652 break;
653 case 'Q':
654 *done = 1;
655 break;
656 case 'f':
657 if (view_is_parent_view(view)) {
658 if (view->child == NULL)
659 break;
660 if (view_is_splitscreen(view->child)) {
661 *focus = view->child;
662 view->child_focussed = 1;
663 err = view_fullscreen(view->child);
664 } else
665 err = view_splitscreen(view->child);
666 if (err)
667 break;
668 err = view->child->input(new, dead, focus,
669 view->child, KEY_RESIZE);
670 } else {
671 if (view_is_splitscreen(view)) {
672 *focus = view;
673 view->parent->child_focussed = 1;
674 err = view_fullscreen(view);
675 } else {
676 err = view_splitscreen(view);
678 if (err)
679 break;
680 err = view->input(new, dead, focus, view,
681 KEY_RESIZE);
683 break;
684 case KEY_RESIZE:
685 break;
686 case '/':
687 if (view->search_start)
688 view_search_start(view);
689 else
690 err = view->input(new, dead, focus, view, ch);
691 break;
692 case 'N':
693 case 'n':
694 if (view->search_next && view->searching) {
695 view->searching = (ch == 'n' ?
696 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
697 view->search_next_done = 0;
698 view->search_next(view);
699 } else
700 err = view->input(new, dead, focus, view, ch);
701 break;
702 default:
703 err = view->input(new, dead, focus, view, ch);
704 break;
707 return err;
710 void
711 view_vborder(struct tog_view *view)
713 PANEL *panel;
714 struct tog_view *view_above;
716 if (view->parent)
717 return view_vborder(view->parent);
719 panel = panel_above(view->panel);
720 if (panel == NULL)
721 return;
723 view_above = panel_userptr(panel);
724 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
725 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
728 int
729 view_needs_focus_indication(struct tog_view *view)
731 if (view_is_parent_view(view)) {
732 if (view->child == NULL || view->child_focussed)
733 return 0;
734 if (!view_is_splitscreen(view->child))
735 return 0;
736 } else if (!view_is_splitscreen(view))
737 return 0;
739 return view->focussed;
742 static const struct got_error *
743 view_loop(struct tog_view *view)
745 const struct got_error *err = NULL;
746 struct tog_view_list_head views;
747 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
748 int fast_refresh = 10;
749 int done = 0, errcode;
751 errcode = pthread_mutex_lock(&tog_mutex);
752 if (errcode)
753 return got_error_set_errno(errcode, "pthread_mutex_lock");
755 TAILQ_INIT(&views);
756 TAILQ_INSERT_HEAD(&views, view, entry);
758 main_view = view;
759 view->focussed = 1;
760 err = view->show(view);
761 if (err)
762 return err;
763 update_panels();
764 doupdate();
765 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
766 /* Refresh fast during initialization, then become slower. */
767 if (fast_refresh && fast_refresh-- == 0)
768 halfdelay(10); /* switch to once per second */
770 err = view_input(&new_view, &dead_view, &focus_view, &done,
771 view, &views);
772 if (err)
773 break;
774 if (dead_view) {
775 struct tog_view *prev = NULL;
777 if (view_is_parent_view(dead_view))
778 prev = TAILQ_PREV(dead_view,
779 tog_view_list_head, entry);
780 else if (view->parent != dead_view)
781 prev = view->parent;
783 if (dead_view->parent)
784 dead_view->parent->child = NULL;
785 else
786 TAILQ_REMOVE(&views, dead_view, entry);
788 err = view_close(dead_view);
789 if (err || (dead_view == main_view && new_view == NULL))
790 goto done;
792 if (view == dead_view) {
793 if (focus_view)
794 view = focus_view;
795 else if (prev)
796 view = prev;
797 else if (!TAILQ_EMPTY(&views))
798 view = TAILQ_LAST(&views,
799 tog_view_list_head);
800 else
801 view = NULL;
802 if (view) {
803 if (view->child && view->child_focussed)
804 focus_view = view->child;
805 else
806 focus_view = view;
810 if (new_view) {
811 struct tog_view *v, *t;
812 /* Only allow one parent view per type. */
813 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
814 if (v->type != new_view->type)
815 continue;
816 TAILQ_REMOVE(&views, v, entry);
817 err = view_close(v);
818 if (err)
819 goto done;
820 break;
822 TAILQ_INSERT_TAIL(&views, new_view, entry);
823 view = new_view;
824 if (focus_view == NULL)
825 focus_view = new_view;
827 if (focus_view) {
828 show_panel(focus_view->panel);
829 if (view)
830 view->focussed = 0;
831 focus_view->focussed = 1;
832 view = focus_view;
833 if (new_view)
834 show_panel(new_view->panel);
835 if (view->child && view_is_splitscreen(view->child))
836 show_panel(view->child->panel);
838 if (view) {
839 if (focus_view == NULL) {
840 view->focussed = 1;
841 show_panel(view->panel);
842 if (view->child && view_is_splitscreen(view->child))
843 show_panel(view->child->panel);
844 focus_view = view;
846 if (view->parent) {
847 err = view->parent->show(view->parent);
848 if (err)
849 goto done;
851 err = view->show(view);
852 if (err)
853 goto done;
854 if (view->child) {
855 err = view->child->show(view->child);
856 if (err)
857 goto done;
859 update_panels();
860 doupdate();
863 done:
864 while (!TAILQ_EMPTY(&views)) {
865 view = TAILQ_FIRST(&views);
866 TAILQ_REMOVE(&views, view, entry);
867 view_close(view);
870 errcode = pthread_mutex_unlock(&tog_mutex);
871 if (errcode && err == NULL)
872 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
874 return err;
877 __dead static void
878 usage_log(void)
880 endwin();
881 fprintf(stderr,
882 "usage: %s log [-c commit] [-r repository-path] [path]\n",
883 getprogname());
884 exit(1);
887 /* Create newly allocated wide-character string equivalent to a byte string. */
888 static const struct got_error *
889 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
891 char *vis = NULL;
892 const struct got_error *err = NULL;
894 *ws = NULL;
895 *wlen = mbstowcs(NULL, s, 0);
896 if (*wlen == (size_t)-1) {
897 int vislen;
898 if (errno != EILSEQ)
899 return got_error_from_errno("mbstowcs");
901 /* byte string invalid in current encoding; try to "fix" it */
902 err = got_mbsavis(&vis, &vislen, s);
903 if (err)
904 return err;
905 *wlen = mbstowcs(NULL, vis, 0);
906 if (*wlen == (size_t)-1) {
907 err = got_error_from_errno("mbstowcs"); /* give up */
908 goto done;
912 *ws = calloc(*wlen + 1, sizeof(**ws));
913 if (*ws == NULL) {
914 err = got_error_from_errno("calloc");
915 goto done;
918 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
919 err = got_error_from_errno("mbstowcs");
920 done:
921 free(vis);
922 if (err) {
923 free(*ws);
924 *ws = NULL;
925 *wlen = 0;
927 return err;
930 /* Format a line for display, ensuring that it won't overflow a width limit. */
931 static const struct got_error *
932 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
933 int col_tab_align)
935 const struct got_error *err = NULL;
936 int cols = 0;
937 wchar_t *wline = NULL;
938 size_t wlen;
939 int i;
941 *wlinep = NULL;
942 *widthp = 0;
944 err = mbs2ws(&wline, &wlen, line);
945 if (err)
946 return err;
948 i = 0;
949 while (i < wlen) {
950 int width = wcwidth(wline[i]);
952 if (width == 0) {
953 i++;
954 continue;
957 if (width == 1 || width == 2) {
958 if (cols + width > wlimit)
959 break;
960 cols += width;
961 i++;
962 } else if (width == -1) {
963 if (wline[i] == L'\t') {
964 width = TABSIZE -
965 ((cols + col_tab_align) % TABSIZE);
966 if (cols + width > wlimit)
967 break;
968 cols += width;
970 i++;
971 } else {
972 err = got_error_from_errno("wcwidth");
973 goto done;
976 wline[i] = L'\0';
977 if (widthp)
978 *widthp = cols;
979 done:
980 if (err)
981 free(wline);
982 else
983 *wlinep = wline;
984 return err;
987 static const struct got_error*
988 build_refs_str(char **refs_str, struct got_reflist_head *refs,
989 struct got_object_id *id, struct got_repository *repo)
991 static const struct got_error *err = NULL;
992 struct got_reflist_entry *re;
993 char *s;
994 const char *name;
996 *refs_str = NULL;
998 SIMPLEQ_FOREACH(re, refs, entry) {
999 struct got_tag_object *tag = NULL;
1000 int cmp;
1002 name = got_ref_get_name(re->ref);
1003 if (strcmp(name, GOT_REF_HEAD) == 0)
1004 continue;
1005 if (strncmp(name, "refs/", 5) == 0)
1006 name += 5;
1007 if (strncmp(name, "got/", 4) == 0)
1008 continue;
1009 if (strncmp(name, "heads/", 6) == 0)
1010 name += 6;
1011 if (strncmp(name, "remotes/", 8) == 0)
1012 name += 8;
1013 if (strncmp(name, "tags/", 5) == 0) {
1014 err = got_object_open_as_tag(&tag, repo, re->id);
1015 if (err) {
1016 if (err->code != GOT_ERR_OBJ_TYPE)
1017 break;
1018 /* Ref points at something other than a tag. */
1019 err = NULL;
1020 tag = NULL;
1023 cmp = got_object_id_cmp(tag ?
1024 got_object_tag_get_object_id(tag) : re->id, id);
1025 if (tag)
1026 got_object_tag_close(tag);
1027 if (cmp != 0)
1028 continue;
1029 s = *refs_str;
1030 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1031 s ? ", " : "", name) == -1) {
1032 err = got_error_from_errno("asprintf");
1033 free(s);
1034 *refs_str = NULL;
1035 break;
1037 free(s);
1040 return err;
1043 static const struct got_error *
1044 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1045 int col_tab_align)
1047 char *smallerthan, *at;
1049 smallerthan = strchr(author, '<');
1050 if (smallerthan && smallerthan[1] != '\0')
1051 author = smallerthan + 1;
1052 at = strchr(author, '@');
1053 if (at)
1054 *at = '\0';
1055 return format_line(wauthor, author_width, author, limit, col_tab_align);
1058 static const struct got_error *
1059 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1060 struct got_object_id *id, struct got_reflist_head *refs,
1061 const size_t date_display_cols, int author_display_cols)
1063 const struct got_error *err = NULL;
1064 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1065 char *logmsg0 = NULL, *logmsg = NULL;
1066 char *author = NULL;
1067 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1068 int author_width, logmsg_width;
1069 char *newline, *line = NULL;
1070 int col, limit;
1071 const int avail = view->ncols;
1072 struct tm tm;
1073 time_t committer_time;
1075 committer_time = got_object_commit_get_committer_time(commit);
1076 if (localtime_r(&committer_time, &tm) == NULL)
1077 return got_error_from_errno("localtime_r");
1078 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1079 >= sizeof(datebuf))
1080 return got_error(GOT_ERR_NO_SPACE);
1082 if (avail <= date_display_cols)
1083 limit = MIN(sizeof(datebuf) - 1, avail);
1084 else
1085 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1086 waddnstr(view->window, datebuf, limit);
1087 col = limit;
1088 if (col > avail)
1089 goto done;
1091 author = strdup(got_object_commit_get_author(commit));
1092 if (author == NULL) {
1093 err = got_error_from_errno("strdup");
1094 goto done;
1096 err = format_author(&wauthor, &author_width, author, avail - col, col);
1097 if (err)
1098 goto done;
1099 waddwstr(view->window, wauthor);
1100 col += author_width;
1101 while (col < avail && author_width < author_display_cols + 2) {
1102 waddch(view->window, ' ');
1103 col++;
1104 author_width++;
1106 if (col > avail)
1107 goto done;
1109 err = got_object_commit_get_logmsg(&logmsg0, commit);
1110 if (err)
1111 goto done;
1112 logmsg = logmsg0;
1113 while (*logmsg == '\n')
1114 logmsg++;
1115 newline = strchr(logmsg, '\n');
1116 if (newline)
1117 *newline = '\0';
1118 limit = avail - col;
1119 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1120 if (err)
1121 goto done;
1122 waddwstr(view->window, wlogmsg);
1123 col += logmsg_width;
1124 while (col < avail) {
1125 waddch(view->window, ' ');
1126 col++;
1128 done:
1129 free(logmsg0);
1130 free(wlogmsg);
1131 free(author);
1132 free(wauthor);
1133 free(line);
1134 return err;
1137 static struct commit_queue_entry *
1138 alloc_commit_queue_entry(struct got_commit_object *commit,
1139 struct got_object_id *id)
1141 struct commit_queue_entry *entry;
1143 entry = calloc(1, sizeof(*entry));
1144 if (entry == NULL)
1145 return NULL;
1147 entry->id = id;
1148 entry->commit = commit;
1149 return entry;
1152 static void
1153 pop_commit(struct commit_queue *commits)
1155 struct commit_queue_entry *entry;
1157 entry = TAILQ_FIRST(&commits->head);
1158 TAILQ_REMOVE(&commits->head, entry, entry);
1159 got_object_commit_close(entry->commit);
1160 commits->ncommits--;
1161 /* Don't free entry->id! It is owned by the commit graph. */
1162 free(entry);
1165 static void
1166 free_commits(struct commit_queue *commits)
1168 while (!TAILQ_EMPTY(&commits->head))
1169 pop_commit(commits);
1172 static const struct got_error *
1173 match_commit(int *have_match, struct got_object_id *id,
1174 struct got_commit_object *commit, regex_t *regex)
1176 const struct got_error *err = NULL;
1177 regmatch_t regmatch;
1178 char *id_str = NULL, *logmsg = NULL;
1180 *have_match = 0;
1182 err = got_object_id_str(&id_str, id);
1183 if (err)
1184 return err;
1186 err = got_object_commit_get_logmsg(&logmsg, commit);
1187 if (err)
1188 goto done;
1190 if (regexec(regex, got_object_commit_get_author(commit), 1,
1191 &regmatch, 0) == 0 ||
1192 regexec(regex, got_object_commit_get_committer(commit), 1,
1193 &regmatch, 0) == 0 ||
1194 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1195 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1196 *have_match = 1;
1197 done:
1198 free(id_str);
1199 free(logmsg);
1200 return err;
1203 static const struct got_error *
1204 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1205 int minqueue, struct got_repository *repo, const char *path,
1206 int *searching, int *search_next_done, regex_t *regex)
1208 const struct got_error *err = NULL;
1209 int nqueued = 0, have_match = 0;
1212 * We keep all commits open throughout the lifetime of the log
1213 * view in order to avoid having to re-fetch commits from disk
1214 * while updating the display.
1216 while (nqueued < minqueue ||
1217 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1218 struct got_object_id *id;
1219 struct got_commit_object *commit;
1220 struct commit_queue_entry *entry;
1221 int errcode;
1223 err = got_commit_graph_iter_next(&id, graph);
1224 if (err) {
1225 if (err->code != GOT_ERR_ITER_NEED_MORE)
1226 break;
1227 err = got_commit_graph_fetch_commits(graph,
1228 minqueue, repo, NULL, NULL);
1229 if (err)
1230 return err;
1231 continue;
1234 if (id == NULL)
1235 break;
1237 err = got_object_open_as_commit(&commit, repo, id);
1238 if (err)
1239 break;
1240 entry = alloc_commit_queue_entry(commit, id);
1241 if (entry == NULL) {
1242 err = got_error_from_errno("alloc_commit_queue_entry");
1243 break;
1246 errcode = pthread_mutex_lock(&tog_mutex);
1247 if (errcode) {
1248 err = got_error_set_errno(errcode,
1249 "pthread_mutex_lock");
1250 break;
1253 entry->idx = commits->ncommits;
1254 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1255 nqueued++;
1256 commits->ncommits++;
1258 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1259 err = match_commit(&have_match, id, commit, regex);
1260 if (err) {
1261 pthread_mutex_lock(&tog_mutex);
1262 break;
1266 errcode = pthread_mutex_unlock(&tog_mutex);
1267 if (errcode && err == NULL)
1268 err = got_error_set_errno(errcode,
1269 "pthread_mutex_unlock");
1271 if (have_match)
1272 break;
1275 return err;
1278 static const struct got_error *
1279 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1280 struct got_repository *repo)
1282 const struct got_error *err = NULL;
1283 struct got_reference *head_ref;
1285 *head_id = NULL;
1287 err = got_ref_open(&head_ref, repo, branch_name, 0);
1288 if (err)
1289 return err;
1291 err = got_ref_resolve(head_id, repo, head_ref);
1292 got_ref_close(head_ref);
1293 if (err) {
1294 *head_id = NULL;
1295 return err;
1298 return NULL;
1301 static const struct got_error *
1302 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1303 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1304 struct commit_queue *commits, int selected_idx, int limit,
1305 struct got_reflist_head *refs, const char *path, int commits_needed)
1307 const struct got_error *err = NULL;
1308 struct tog_log_view_state *s = &view->state.log;
1309 struct commit_queue_entry *entry;
1310 int width;
1311 int ncommits, author_cols = 10;
1312 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1313 char *refs_str = NULL;
1314 wchar_t *wline;
1315 static const size_t date_display_cols = 9;
1317 entry = first;
1318 ncommits = 0;
1319 while (entry) {
1320 if (ncommits == selected_idx) {
1321 *selected = entry;
1322 break;
1324 entry = TAILQ_NEXT(entry, entry);
1325 ncommits++;
1328 if (*selected && !(view->searching && view->search_next_done == 0)) {
1329 err = got_object_id_str(&id_str, (*selected)->id);
1330 if (err)
1331 return err;
1332 if (refs) {
1333 err = build_refs_str(&refs_str, refs, (*selected)->id,
1334 s->repo);
1335 if (err)
1336 goto done;
1340 if (commits_needed == 0)
1341 halfdelay(10); /* disable fast refresh */
1343 if (asprintf(&ncommits_str, " [%d/%d] %s",
1344 entry ? entry->idx + 1 : 0, commits->ncommits,
1345 commits_needed > 0 ?
1346 (view->searching && view->search_next_done == 0
1347 ? "searching..." : "loading... ") :
1348 (refs_str ? refs_str : "")) == -1) {
1349 err = got_error_from_errno("asprintf");
1350 goto done;
1353 if (path && strcmp(path, "/") != 0) {
1354 if (asprintf(&header, "commit %s %s%s",
1355 id_str ? id_str : "........................................",
1356 path, ncommits_str) == -1) {
1357 err = got_error_from_errno("asprintf");
1358 header = NULL;
1359 goto done;
1361 } else if (asprintf(&header, "commit %s%s",
1362 id_str ? id_str : "........................................",
1363 ncommits_str) == -1) {
1364 err = got_error_from_errno("asprintf");
1365 header = NULL;
1366 goto done;
1368 err = format_line(&wline, &width, header, view->ncols, 0);
1369 if (err)
1370 goto done;
1372 werase(view->window);
1374 if (view_needs_focus_indication(view))
1375 wstandout(view->window);
1376 waddwstr(view->window, wline);
1377 while (width < view->ncols) {
1378 waddch(view->window, ' ');
1379 width++;
1381 if (view_needs_focus_indication(view))
1382 wstandend(view->window);
1383 free(wline);
1384 if (limit <= 1)
1385 goto done;
1387 /* Grow author column size if necessary. */
1388 entry = first;
1389 ncommits = 0;
1390 while (entry) {
1391 char *author;
1392 wchar_t *wauthor;
1393 int width;
1394 if (ncommits >= limit - 1)
1395 break;
1396 author = strdup(got_object_commit_get_author(entry->commit));
1397 if (author == NULL) {
1398 err = got_error_from_errno("strdup");
1399 goto done;
1401 err = format_author(&wauthor, &width, author, COLS,
1402 date_display_cols);
1403 if (author_cols < width)
1404 author_cols = width;
1405 free(wauthor);
1406 free(author);
1407 entry = TAILQ_NEXT(entry, entry);
1410 entry = first;
1411 *last = first;
1412 ncommits = 0;
1413 while (entry) {
1414 if (ncommits >= limit - 1)
1415 break;
1416 if (ncommits == selected_idx)
1417 wstandout(view->window);
1418 err = draw_commit(view, entry->commit, entry->id, refs,
1419 date_display_cols, author_cols);
1420 if (ncommits == selected_idx)
1421 wstandend(view->window);
1422 if (err)
1423 goto done;
1424 ncommits++;
1425 *last = entry;
1426 entry = TAILQ_NEXT(entry, entry);
1429 view_vborder(view);
1430 done:
1431 free(id_str);
1432 free(refs_str);
1433 free(ncommits_str);
1434 free(header);
1435 return err;
1438 static void
1439 scroll_up(struct tog_view *view,
1440 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1441 struct commit_queue *commits)
1443 struct commit_queue_entry *entry;
1444 int nscrolled = 0;
1446 entry = TAILQ_FIRST(&commits->head);
1447 if (*first_displayed_entry == entry)
1448 return;
1450 entry = *first_displayed_entry;
1451 while (entry && nscrolled < maxscroll) {
1452 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1453 if (entry) {
1454 *first_displayed_entry = entry;
1455 nscrolled++;
1460 static const struct got_error *
1461 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1462 pthread_cond_t *need_commits)
1464 int errcode;
1465 int max_wait = 20;
1467 halfdelay(1); /* fast refresh while loading commits */
1469 while (*commits_needed > 0) {
1470 if (*log_complete)
1471 break;
1473 /* Wake the log thread. */
1474 errcode = pthread_cond_signal(need_commits);
1475 if (errcode)
1476 return got_error_set_errno(errcode,
1477 "pthread_cond_signal");
1478 errcode = pthread_mutex_unlock(&tog_mutex);
1479 if (errcode)
1480 return got_error_set_errno(errcode,
1481 "pthread_mutex_unlock");
1482 pthread_yield();
1483 errcode = pthread_mutex_lock(&tog_mutex);
1484 if (errcode)
1485 return got_error_set_errno(errcode,
1486 "pthread_mutex_lock");
1488 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1490 * Thread is not done yet; lose a key press
1491 * and let the user retry... this way the GUI
1492 * remains interactive while logging deep paths
1493 * with few commits in history.
1495 return NULL;
1499 return NULL;
1502 static const struct got_error *
1503 scroll_down(struct tog_view *view,
1504 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1505 struct commit_queue_entry **last_displayed_entry,
1506 struct commit_queue *commits, int *log_complete, int *commits_needed,
1507 pthread_cond_t *need_commits)
1509 const struct got_error *err = NULL;
1510 struct commit_queue_entry *pentry;
1511 int nscrolled = 0;
1513 if (*last_displayed_entry == NULL)
1514 return NULL;
1516 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1517 if (pentry == NULL && !*log_complete) {
1519 * Ask the log thread for required amount of commits
1520 * plus some amount of pre-fetching.
1522 (*commits_needed) += maxscroll + 20;
1523 err = trigger_log_thread(0, commits_needed, log_complete,
1524 need_commits);
1525 if (err)
1526 return err;
1529 do {
1530 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1531 if (pentry == NULL)
1532 break;
1534 *last_displayed_entry = pentry;
1536 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1537 if (pentry == NULL)
1538 break;
1539 *first_displayed_entry = pentry;
1540 } while (++nscrolled < maxscroll);
1542 return err;
1545 static const struct got_error *
1546 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1547 struct got_commit_object *commit, struct got_object_id *commit_id,
1548 struct tog_view *log_view, struct got_reflist_head *refs,
1549 struct got_repository *repo)
1551 const struct got_error *err;
1552 struct got_object_qid *parent_id;
1553 struct tog_view *diff_view;
1555 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1556 if (diff_view == NULL)
1557 return got_error_from_errno("view_open");
1559 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1560 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1561 commit_id, log_view, refs, repo);
1562 if (err == NULL)
1563 *new_view = diff_view;
1564 return err;
1567 static const struct got_error *
1568 tree_view_visit_subtree(struct got_tree_object *subtree,
1569 struct tog_tree_view_state *s)
1571 struct tog_parent_tree *parent;
1573 parent = calloc(1, sizeof(*parent));
1574 if (parent == NULL)
1575 return got_error_from_errno("calloc");
1577 parent->tree = s->tree;
1578 parent->first_displayed_entry = s->first_displayed_entry;
1579 parent->selected_entry = s->selected_entry;
1580 parent->selected = s->selected;
1581 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1582 s->tree = subtree;
1583 s->entries = got_object_tree_get_entries(s->tree);
1584 s->selected = 0;
1585 s->first_displayed_entry = NULL;
1586 return NULL;
1590 static const struct got_error *
1591 browse_commit_tree(struct tog_view **new_view, int begin_x,
1592 struct commit_queue_entry *entry, const char *path,
1593 struct got_reflist_head *refs, struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_tree_object *tree;
1597 struct got_tree_entry *te;
1598 struct tog_tree_view_state *s;
1599 struct tog_view *tree_view;
1600 char *slash, *subpath = NULL;
1601 const char *p;
1603 err = got_object_open_as_tree(&tree, repo,
1604 got_object_commit_get_tree_id(entry->commit));
1605 if (err)
1606 return err;
1608 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1609 if (tree_view == NULL)
1610 return got_error_from_errno("view_open");
1612 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1613 if (err) {
1614 got_object_tree_close(tree);
1615 return err;
1617 s = &tree_view->state.tree;
1619 *new_view = tree_view;
1621 /* Walk the path and open corresponding tree objects. */
1622 p = path;
1623 while (p[0] == '/')
1624 p++;
1625 while (*p) {
1626 struct got_object_id *tree_id;
1628 /* Ensure the correct subtree entry is selected. */
1629 slash = strchr(p, '/');
1630 if (slash == NULL)
1631 slash = strchr(p, '\0');
1632 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1633 if (strncmp(p, te->name, slash - p) == 0) {
1634 s->selected_entry = te;
1635 break;
1637 s->selected++;
1639 if (s->selected_entry == NULL) {
1640 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1641 break;
1643 if (s->tree != s->root)
1644 s->selected++; /* skip '..' */
1646 if (!S_ISDIR(s->selected_entry->mode)) {
1647 /* Jump to this file's entry. */
1648 s->first_displayed_entry = s->selected_entry;
1649 s->selected = 0;
1650 break;
1653 slash = strchr(p, '/');
1654 if (slash)
1655 subpath = strndup(path, slash - path);
1656 else
1657 subpath = strdup(path);
1658 if (subpath == NULL) {
1659 err = got_error_from_errno("strdup");
1660 break;
1663 err = got_object_id_by_path(&tree_id, repo, entry->id,
1664 subpath);
1665 if (err)
1666 break;
1668 err = got_object_open_as_tree(&tree, repo, tree_id);
1669 free(tree_id);
1670 if (err)
1671 break;
1673 err = tree_view_visit_subtree(tree, s);
1674 if (err) {
1675 got_object_tree_close(tree);
1676 break;
1678 if (slash == NULL)
1679 break;
1680 free(subpath);
1681 subpath = NULL;
1682 p = slash;
1685 free(subpath);
1686 return err;
1689 static void *
1690 log_thread(void *arg)
1692 const struct got_error *err = NULL;
1693 int errcode = 0;
1694 struct tog_log_thread_args *a = arg;
1695 int done = 0;
1697 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1698 NULL, NULL);
1699 if (err)
1700 return (void *)err;
1702 while (!done && !err && !tog_sigpipe_received) {
1703 err = queue_commits(a->graph, a->commits, 1, a->repo,
1704 a->in_repo_path, a->searching, a->search_next_done,
1705 a->regex);
1706 if (err) {
1707 if (err->code != GOT_ERR_ITER_COMPLETED)
1708 return (void *)err;
1709 err = NULL;
1710 done = 1;
1711 } else if (a->commits_needed > 0)
1712 a->commits_needed--;
1714 errcode = pthread_mutex_lock(&tog_mutex);
1715 if (errcode) {
1716 err = got_error_set_errno(errcode,
1717 "pthread_mutex_lock");
1718 break;
1719 } else if (*a->quit)
1720 done = 1;
1721 else if (*a->first_displayed_entry == NULL) {
1722 *a->first_displayed_entry =
1723 TAILQ_FIRST(&a->commits->head);
1724 *a->selected_entry = *a->first_displayed_entry;
1727 if (done)
1728 a->commits_needed = 0;
1729 else if (a->commits_needed == 0) {
1730 errcode = pthread_cond_wait(&a->need_commits,
1731 &tog_mutex);
1732 if (errcode)
1733 err = got_error_set_errno(errcode,
1734 "pthread_cond_wait");
1737 errcode = pthread_mutex_unlock(&tog_mutex);
1738 if (errcode && err == NULL)
1739 err = got_error_set_errno(errcode,
1740 "pthread_mutex_unlock");
1742 a->log_complete = 1;
1743 return (void *)err;
1746 static const struct got_error *
1747 stop_log_thread(struct tog_log_view_state *s)
1749 const struct got_error *err = NULL;
1750 int errcode;
1752 if (s->thread) {
1753 s->quit = 1;
1754 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1755 if (errcode)
1756 return got_error_set_errno(errcode,
1757 "pthread_cond_signal");
1758 errcode = pthread_mutex_unlock(&tog_mutex);
1759 if (errcode)
1760 return got_error_set_errno(errcode,
1761 "pthread_mutex_unlock");
1762 errcode = pthread_join(s->thread, (void **)&err);
1763 if (errcode)
1764 return got_error_set_errno(errcode, "pthread_join");
1765 errcode = pthread_mutex_lock(&tog_mutex);
1766 if (errcode)
1767 return got_error_set_errno(errcode,
1768 "pthread_mutex_lock");
1769 s->thread = NULL;
1772 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1773 if (errcode && err == NULL)
1774 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1776 if (s->thread_args.repo) {
1777 got_repo_close(s->thread_args.repo);
1778 s->thread_args.repo = NULL;
1781 if (s->thread_args.graph) {
1782 got_commit_graph_close(s->thread_args.graph);
1783 s->thread_args.graph = NULL;
1786 return err;
1789 static const struct got_error *
1790 close_log_view(struct tog_view *view)
1792 const struct got_error *err = NULL;
1793 struct tog_log_view_state *s = &view->state.log;
1795 err = stop_log_thread(s);
1796 free_commits(&s->commits);
1797 free(s->in_repo_path);
1798 s->in_repo_path = NULL;
1799 free(s->start_id);
1800 s->start_id = NULL;
1801 return err;
1804 static const struct got_error *
1805 search_start_log_view(struct tog_view *view)
1807 struct tog_log_view_state *s = &view->state.log;
1809 s->matched_entry = NULL;
1810 s->search_entry = NULL;
1811 return NULL;
1814 static const struct got_error *
1815 search_next_log_view(struct tog_view *view)
1817 const struct got_error *err = NULL;
1818 struct tog_log_view_state *s = &view->state.log;
1819 struct commit_queue_entry *entry;
1821 if (!view->searching) {
1822 view->search_next_done = 1;
1823 return NULL;
1826 if (s->search_entry) {
1827 int errcode, ch;
1828 errcode = pthread_mutex_unlock(&tog_mutex);
1829 if (errcode)
1830 return got_error_set_errno(errcode,
1831 "pthread_mutex_unlock");
1832 ch = wgetch(view->window);
1833 errcode = pthread_mutex_lock(&tog_mutex);
1834 if (errcode)
1835 return got_error_set_errno(errcode,
1836 "pthread_mutex_lock");
1837 if (ch == KEY_BACKSPACE) {
1838 view->search_next_done = 1;
1839 return NULL;
1841 if (view->searching == TOG_SEARCH_FORWARD)
1842 entry = TAILQ_NEXT(s->search_entry, entry);
1843 else
1844 entry = TAILQ_PREV(s->search_entry,
1845 commit_queue_head, entry);
1846 } else if (s->matched_entry) {
1847 if (view->searching == TOG_SEARCH_FORWARD)
1848 entry = TAILQ_NEXT(s->selected_entry, entry);
1849 else
1850 entry = TAILQ_PREV(s->selected_entry,
1851 commit_queue_head, entry);
1852 } else {
1853 if (view->searching == TOG_SEARCH_FORWARD)
1854 entry = TAILQ_FIRST(&s->commits.head);
1855 else
1856 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1859 while (1) {
1860 int have_match = 0;
1862 if (entry == NULL) {
1863 if (s->thread_args.log_complete ||
1864 view->searching == TOG_SEARCH_BACKWARD) {
1865 view->search_next_done = 1;
1866 return NULL;
1869 * Poke the log thread for more commits and return,
1870 * allowing the main loop to make progress. Search
1871 * will resume at s->search_entry once we come back.
1873 s->thread_args.commits_needed++;
1874 return trigger_log_thread(1,
1875 &s->thread_args.commits_needed,
1876 &s->thread_args.log_complete,
1877 &s->thread_args.need_commits);
1880 err = match_commit(&have_match, entry->id, entry->commit,
1881 &view->regex);
1882 if (err)
1883 break;
1884 if (have_match) {
1885 view->search_next_done = 1;
1886 s->matched_entry = entry;
1887 break;
1890 s->search_entry = entry;
1891 if (view->searching == TOG_SEARCH_FORWARD)
1892 entry = TAILQ_NEXT(entry, entry);
1893 else
1894 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1897 if (s->matched_entry) {
1898 int cur = s->selected_entry->idx;
1899 while (cur < s->matched_entry->idx) {
1900 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1901 if (err)
1902 return err;
1903 cur++;
1905 while (cur > s->matched_entry->idx) {
1906 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1907 if (err)
1908 return err;
1909 cur--;
1913 s->search_entry = NULL;
1915 return NULL;
1918 static const struct got_error *
1919 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1920 struct got_reflist_head *refs, struct got_repository *repo,
1921 const char *head_ref_name, const char *path, int check_disk)
1923 const struct got_error *err = NULL;
1924 struct tog_log_view_state *s = &view->state.log;
1925 struct got_repository *thread_repo = NULL;
1926 struct got_commit_graph *thread_graph = NULL;
1927 int errcode;
1929 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1930 if (err != NULL)
1931 goto done;
1933 /* The commit queue only contains commits being displayed. */
1934 TAILQ_INIT(&s->commits.head);
1935 s->commits.ncommits = 0;
1937 s->refs = refs;
1938 s->repo = repo;
1939 s->head_ref_name = head_ref_name;
1940 s->start_id = got_object_id_dup(start_id);
1941 if (s->start_id == NULL) {
1942 err = got_error_from_errno("got_object_id_dup");
1943 goto done;
1946 view->show = show_log_view;
1947 view->input = input_log_view;
1948 view->close = close_log_view;
1949 view->search_start = search_start_log_view;
1950 view->search_next = search_next_log_view;
1952 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1953 if (err)
1954 goto done;
1955 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1956 0, thread_repo);
1957 if (err)
1958 goto done;
1960 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1961 if (errcode) {
1962 err = got_error_set_errno(errcode, "pthread_cond_init");
1963 goto done;
1966 s->thread_args.commits_needed = view->nlines;
1967 s->thread_args.graph = thread_graph;
1968 s->thread_args.commits = &s->commits;
1969 s->thread_args.in_repo_path = s->in_repo_path;
1970 s->thread_args.start_id = s->start_id;
1971 s->thread_args.repo = thread_repo;
1972 s->thread_args.log_complete = 0;
1973 s->thread_args.quit = &s->quit;
1974 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1975 s->thread_args.selected_entry = &s->selected_entry;
1976 s->thread_args.searching = &view->searching;
1977 s->thread_args.search_next_done = &view->search_next_done;
1978 s->thread_args.regex = &view->regex;
1979 done:
1980 if (err)
1981 close_log_view(view);
1982 return err;
1985 static const struct got_error *
1986 show_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 if (s->thread == NULL) {
1991 int errcode = pthread_create(&s->thread, NULL, log_thread,
1992 &s->thread_args);
1993 if (errcode)
1994 return got_error_set_errno(errcode, "pthread_create");
1997 return draw_commits(view, &s->last_displayed_entry,
1998 &s->selected_entry, s->first_displayed_entry,
1999 &s->commits, s->selected, view->nlines, s->refs,
2000 s->in_repo_path, s->thread_args.commits_needed);
2003 static const struct got_error *
2004 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2005 struct tog_view **focus_view, struct tog_view *view, int ch)
2007 const struct got_error *err = NULL;
2008 struct tog_log_view_state *s = &view->state.log;
2009 char *parent_path, *in_repo_path = NULL;
2010 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2011 int begin_x = 0;
2012 struct got_object_id *start_id;
2014 switch (ch) {
2015 case 'q':
2016 s->quit = 1;
2017 break;
2018 case 'k':
2019 case KEY_UP:
2020 case '<':
2021 case ',':
2022 if (s->first_displayed_entry == NULL)
2023 break;
2024 if (s->selected > 0)
2025 s->selected--;
2026 else
2027 scroll_up(view, &s->first_displayed_entry, 1,
2028 &s->commits);
2029 break;
2030 case KEY_PPAGE:
2031 case CTRL('b'):
2032 if (s->first_displayed_entry == NULL)
2033 break;
2034 if (TAILQ_FIRST(&s->commits.head) ==
2035 s->first_displayed_entry) {
2036 s->selected = 0;
2037 break;
2039 scroll_up(view, &s->first_displayed_entry,
2040 view->nlines, &s->commits);
2041 break;
2042 case 'j':
2043 case KEY_DOWN:
2044 case '>':
2045 case '.':
2046 if (s->first_displayed_entry == NULL)
2047 break;
2048 if (s->selected < MIN(view->nlines - 2,
2049 s->commits.ncommits - 1)) {
2050 s->selected++;
2051 break;
2053 err = scroll_down(view, &s->first_displayed_entry, 1,
2054 &s->last_displayed_entry, &s->commits,
2055 &s->thread_args.log_complete,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.need_commits);
2058 break;
2059 case KEY_NPAGE:
2060 case CTRL('f'): {
2061 struct commit_queue_entry *first;
2062 first = s->first_displayed_entry;
2063 if (first == NULL)
2064 break;
2065 err = scroll_down(view, &s->first_displayed_entry,
2066 view->nlines, &s->last_displayed_entry,
2067 &s->commits, &s->thread_args.log_complete,
2068 &s->thread_args.commits_needed,
2069 &s->thread_args.need_commits);
2070 if (err)
2071 break;
2072 if (first == s->first_displayed_entry &&
2073 s->selected < MIN(view->nlines - 2,
2074 s->commits.ncommits - 1)) {
2075 /* can't scroll further down */
2076 s->selected = MIN(view->nlines - 2,
2077 s->commits.ncommits - 1);
2079 err = NULL;
2080 break;
2082 case KEY_RESIZE:
2083 if (s->selected > view->nlines - 2)
2084 s->selected = view->nlines - 2;
2085 if (s->selected > s->commits.ncommits - 1)
2086 s->selected = s->commits.ncommits - 1;
2087 break;
2088 case KEY_ENTER:
2089 case ' ':
2090 case '\r':
2091 if (s->selected_entry == NULL)
2092 break;
2093 if (view_is_parent_view(view))
2094 begin_x = view_split_begin_x(view->begin_x);
2095 err = open_diff_view_for_commit(&diff_view, begin_x,
2096 s->selected_entry->commit, s->selected_entry->id,
2097 view, s->refs, s->repo);
2098 if (err)
2099 break;
2100 if (view_is_parent_view(view)) {
2101 err = view_close_child(view);
2102 if (err)
2103 return err;
2104 err = view_set_child(view, diff_view);
2105 if (err) {
2106 view_close(diff_view);
2107 break;
2109 *focus_view = diff_view;
2110 view->child_focussed = 1;
2111 } else
2112 *new_view = diff_view;
2113 break;
2114 case 't':
2115 if (s->selected_entry == NULL)
2116 break;
2117 if (view_is_parent_view(view))
2118 begin_x = view_split_begin_x(view->begin_x);
2119 err = browse_commit_tree(&tree_view, begin_x,
2120 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2121 if (err)
2122 break;
2123 if (view_is_parent_view(view)) {
2124 err = view_close_child(view);
2125 if (err)
2126 return err;
2127 err = view_set_child(view, tree_view);
2128 if (err) {
2129 view_close(tree_view);
2130 break;
2132 *focus_view = tree_view;
2133 view->child_focussed = 1;
2134 } else
2135 *new_view = tree_view;
2136 break;
2137 case KEY_BACKSPACE:
2138 if (strcmp(s->in_repo_path, "/") == 0)
2139 break;
2140 parent_path = dirname(s->in_repo_path);
2141 if (parent_path && strcmp(parent_path, ".") != 0) {
2142 err = stop_log_thread(s);
2143 if (err)
2144 return err;
2145 lv = view_open(view->nlines, view->ncols,
2146 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2147 if (lv == NULL)
2148 return got_error_from_errno(
2149 "view_open");
2150 err = open_log_view(lv, s->start_id, s->refs,
2151 s->repo, s->head_ref_name, parent_path, 0);
2152 if (err)
2153 return err;;
2154 if (view_is_parent_view(view))
2155 *new_view = lv;
2156 else {
2157 view_set_child(view->parent, lv);
2158 *focus_view = lv;
2160 return NULL;
2162 break;
2163 case CTRL('l'):
2164 err = stop_log_thread(s);
2165 if (err)
2166 return err;
2167 lv = view_open(view->nlines, view->ncols,
2168 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2169 if (lv == NULL)
2170 return got_error_from_errno("view_open");
2171 err = get_head_commit_id(&start_id, s->head_ref_name ?
2172 s->head_ref_name : GOT_REF_HEAD, s->repo);
2173 if (err) {
2174 view_close(lv);
2175 return err;
2177 in_repo_path = strdup(s->in_repo_path);
2178 if (in_repo_path == NULL) {
2179 free(start_id);
2180 view_close(lv);
2181 return got_error_from_errno("strdup");
2183 got_ref_list_free(s->refs);
2184 err = got_ref_list(s->refs, s->repo, NULL,
2185 got_ref_cmp_by_name, NULL);
2186 if (err) {
2187 free(start_id);
2188 view_close(lv);
2189 return err;
2191 err = open_log_view(lv, start_id, s->refs, s->repo,
2192 s->head_ref_name, in_repo_path, 0);
2193 if (err) {
2194 free(start_id);
2195 view_close(lv);
2196 return err;;
2198 *dead_view = view;
2199 *new_view = lv;
2200 break;
2201 default:
2202 break;
2205 return err;
2208 static const struct got_error *
2209 apply_unveil(const char *repo_path, const char *worktree_path)
2211 const struct got_error *error;
2213 #ifdef PROFILE
2214 if (unveil("gmon.out", "rwc") != 0)
2215 return got_error_from_errno2("unveil", "gmon.out");
2216 #endif
2217 if (repo_path && unveil(repo_path, "r") != 0)
2218 return got_error_from_errno2("unveil", repo_path);
2220 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2221 return got_error_from_errno2("unveil", worktree_path);
2223 if (unveil("/tmp", "rwc") != 0)
2224 return got_error_from_errno2("unveil", "/tmp");
2226 error = got_privsep_unveil_exec_helpers();
2227 if (error != NULL)
2228 return error;
2230 if (unveil(NULL, NULL) != 0)
2231 return got_error_from_errno("unveil");
2233 return NULL;
2236 static void
2237 init_curses(void)
2239 initscr();
2240 cbreak();
2241 halfdelay(1); /* Do fast refresh while initial view is loading. */
2242 noecho();
2243 nonl();
2244 intrflush(stdscr, FALSE);
2245 keypad(stdscr, TRUE);
2246 curs_set(0);
2247 signal(SIGWINCH, tog_sigwinch);
2248 signal(SIGPIPE, tog_sigpipe);
2251 static const struct got_error *
2252 cmd_log(int argc, char *argv[])
2254 const struct got_error *error;
2255 struct got_repository *repo = NULL;
2256 struct got_worktree *worktree = NULL;
2257 struct got_reflist_head refs;
2258 struct got_object_id *start_id = NULL;
2259 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2260 char *start_commit = NULL, *head_ref_name = NULL;
2261 int ch;
2262 struct tog_view *view;
2264 SIMPLEQ_INIT(&refs);
2266 #ifndef PROFILE
2267 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2268 NULL) == -1)
2269 err(1, "pledge");
2270 #endif
2272 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2273 switch (ch) {
2274 case 'c':
2275 start_commit = optarg;
2276 break;
2277 case 'r':
2278 repo_path = realpath(optarg, NULL);
2279 if (repo_path == NULL)
2280 err(1, "-r option");
2281 break;
2282 default:
2283 usage_log();
2284 /* NOTREACHED */
2288 argc -= optind;
2289 argv += optind;
2291 cwd = getcwd(NULL, 0);
2292 if (cwd == NULL) {
2293 error = got_error_from_errno("getcwd");
2294 goto done;
2296 error = got_worktree_open(&worktree, cwd);
2297 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2298 goto done;
2299 error = NULL;
2301 if (argc == 0) {
2302 path = strdup("");
2303 if (path == NULL) {
2304 error = got_error_from_errno("strdup");
2305 goto done;
2307 } else if (argc == 1) {
2308 if (worktree) {
2309 error = got_worktree_resolve_path(&path, worktree,
2310 argv[0]);
2311 if (error)
2312 goto done;
2313 } else {
2314 path = strdup(argv[0]);
2315 if (path == NULL) {
2316 error = got_error_from_errno("strdup");
2317 goto done;
2320 } else
2321 usage_log();
2323 if (repo_path == NULL) {
2324 if (worktree)
2325 repo_path = strdup(
2326 got_worktree_get_repo_path(worktree));
2327 else
2328 repo_path = strdup(cwd);
2330 if (repo_path == NULL) {
2331 error = got_error_from_errno("strdup");
2332 goto done;
2335 init_curses();
2337 error = got_repo_open(&repo, repo_path, NULL);
2338 if (error != NULL)
2339 goto done;
2341 error = apply_unveil(got_repo_get_path(repo),
2342 worktree ? got_worktree_get_root_path(worktree) : NULL);
2343 if (error)
2344 goto done;
2346 if (start_commit == NULL)
2347 error = get_head_commit_id(&start_id, worktree ?
2348 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2349 repo);
2350 else {
2351 error = get_head_commit_id(&start_id, start_commit, repo);
2352 if (error) {
2353 if (error->code != GOT_ERR_NOT_REF)
2354 goto done;
2355 error = got_repo_match_object_id_prefix(&start_id,
2356 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2359 if (error != NULL)
2360 goto done;
2362 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2363 if (error)
2364 goto done;
2366 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2367 if (view == NULL) {
2368 error = got_error_from_errno("view_open");
2369 goto done;
2371 if (worktree) {
2372 head_ref_name = strdup(
2373 got_worktree_get_head_ref_name(worktree));
2374 if (head_ref_name == NULL) {
2375 error = got_error_from_errno("strdup");
2376 goto done;
2379 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2380 path, 1);
2381 if (error)
2382 goto done;
2383 if (worktree) {
2384 /* Release work tree lock. */
2385 got_worktree_close(worktree);
2386 worktree = NULL;
2388 error = view_loop(view);
2389 done:
2390 free(repo_path);
2391 free(cwd);
2392 free(path);
2393 free(start_id);
2394 free(head_ref_name);
2395 if (repo)
2396 got_repo_close(repo);
2397 if (worktree)
2398 got_worktree_close(worktree);
2399 got_ref_list_free(&refs);
2400 return error;
2403 __dead static void
2404 usage_diff(void)
2406 endwin();
2407 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2408 getprogname());
2409 exit(1);
2412 static char *
2413 parse_next_line(FILE *f, size_t *len)
2415 char *line;
2416 size_t linelen;
2417 size_t lineno;
2418 const char delim[3] = { '\0', '\0', '\0'};
2420 line = fparseln(f, &linelen, &lineno, delim, 0);
2421 if (len)
2422 *len = linelen;
2423 return line;
2426 static const struct got_error *
2427 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2428 int *last_displayed_line, int *eof, int max_lines,
2429 char *header)
2431 const struct got_error *err;
2432 int nlines = 0, nprinted = 0;
2433 char *line;
2434 size_t len;
2435 wchar_t *wline;
2436 int width;
2438 rewind(f);
2439 werase(view->window);
2441 if (header) {
2442 err = format_line(&wline, &width, header, view->ncols, 0);
2443 if (err) {
2444 return err;
2447 if (view_needs_focus_indication(view))
2448 wstandout(view->window);
2449 waddwstr(view->window, wline);
2450 if (view_needs_focus_indication(view))
2451 wstandend(view->window);
2452 if (width <= view->ncols - 1)
2453 waddch(view->window, '\n');
2455 if (max_lines <= 1)
2456 return NULL;
2457 max_lines--;
2460 *eof = 0;
2461 while (nprinted < max_lines) {
2462 line = parse_next_line(f, &len);
2463 if (line == NULL) {
2464 *eof = 1;
2465 break;
2467 if (++nlines < *first_displayed_line) {
2468 free(line);
2469 continue;
2472 err = format_line(&wline, &width, line, view->ncols, 0);
2473 if (err) {
2474 free(line);
2475 return err;
2477 waddwstr(view->window, wline);
2478 if (width <= view->ncols - 1)
2479 waddch(view->window, '\n');
2480 if (++nprinted == 1)
2481 *first_displayed_line = nlines;
2482 free(line);
2483 free(wline);
2484 wline = NULL;
2486 *last_displayed_line = nlines;
2488 view_vborder(view);
2490 if (*eof) {
2491 while (nprinted < view->nlines) {
2492 waddch(view->window, '\n');
2493 nprinted++;
2496 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2497 if (err) {
2498 return err;
2501 wstandout(view->window);
2502 waddwstr(view->window, wline);
2503 wstandend(view->window);
2506 return NULL;
2509 static char *
2510 get_datestr(time_t *time, char *datebuf)
2512 struct tm mytm, *tm;
2513 char *p, *s;
2515 tm = gmtime_r(time, &mytm);
2516 if (tm == NULL)
2517 return NULL;
2518 s = asctime_r(tm, datebuf);
2519 if (s == NULL)
2520 return NULL;
2521 p = strchr(s, '\n');
2522 if (p)
2523 *p = '\0';
2524 return s;
2527 static const struct got_error *
2528 write_commit_info(struct got_object_id *commit_id,
2529 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2531 const struct got_error *err = NULL;
2532 char datebuf[26], *datestr;
2533 struct got_commit_object *commit;
2534 char *id_str = NULL, *logmsg = NULL;
2535 time_t committer_time;
2536 const char *author, *committer;
2537 char *refs_str = NULL;
2539 if (refs) {
2540 err = build_refs_str(&refs_str, refs, commit_id, repo);
2541 if (err)
2542 return err;
2545 err = got_object_open_as_commit(&commit, repo, commit_id);
2546 if (err)
2547 return err;
2549 err = got_object_id_str(&id_str, commit_id);
2550 if (err) {
2551 err = got_error_from_errno("got_object_id_str");
2552 goto done;
2555 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2556 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2557 err = got_error_from_errno("fprintf");
2558 goto done;
2560 if (fprintf(outfile, "from: %s\n",
2561 got_object_commit_get_author(commit)) < 0) {
2562 err = got_error_from_errno("fprintf");
2563 goto done;
2565 committer_time = got_object_commit_get_committer_time(commit);
2566 datestr = get_datestr(&committer_time, datebuf);
2567 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2568 err = got_error_from_errno("fprintf");
2569 goto done;
2571 author = got_object_commit_get_author(commit);
2572 committer = got_object_commit_get_committer(commit);
2573 if (strcmp(author, committer) != 0 &&
2574 fprintf(outfile, "via: %s\n", committer) < 0) {
2575 err = got_error_from_errno("fprintf");
2576 goto done;
2578 err = got_object_commit_get_logmsg(&logmsg, commit);
2579 if (err)
2580 goto done;
2581 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2582 err = got_error_from_errno("fprintf");
2583 goto done;
2585 done:
2586 free(id_str);
2587 free(logmsg);
2588 free(refs_str);
2589 got_object_commit_close(commit);
2590 return err;
2593 static const struct got_error *
2594 create_diff(struct tog_diff_view_state *s)
2596 const struct got_error *err = NULL;
2597 FILE *f = NULL;
2598 int obj_type;
2600 f = got_opentemp();
2601 if (f == NULL) {
2602 err = got_error_from_errno("got_opentemp");
2603 goto done;
2605 if (s->f && fclose(s->f) != 0) {
2606 err = got_error_from_errno("fclose");
2607 goto done;
2609 s->f = f;
2611 if (s->id1)
2612 err = got_object_get_type(&obj_type, s->repo, s->id1);
2613 else
2614 err = got_object_get_type(&obj_type, s->repo, s->id2);
2615 if (err)
2616 goto done;
2618 switch (obj_type) {
2619 case GOT_OBJ_TYPE_BLOB:
2620 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2621 s->diff_context, 0, s->repo, f);
2622 break;
2623 case GOT_OBJ_TYPE_TREE:
2624 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2625 s->diff_context, 0, s->repo, f);
2626 break;
2627 case GOT_OBJ_TYPE_COMMIT: {
2628 const struct got_object_id_queue *parent_ids;
2629 struct got_object_qid *pid;
2630 struct got_commit_object *commit2;
2632 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2633 if (err)
2634 break;
2635 /* Show commit info if we're diffing to a parent/root commit. */
2636 if (s->id1 == NULL)
2637 write_commit_info(s->id2, s->refs, s->repo, f);
2638 else {
2639 parent_ids = got_object_commit_get_parent_ids(commit2);
2640 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2641 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2642 write_commit_info(s->id2, s->refs,
2643 s->repo, f);
2644 break;
2648 got_object_commit_close(commit2);
2650 err = got_diff_objects_as_commits(s->id1, s->id2,
2651 s->diff_context, 0, s->repo, f);
2652 break;
2654 default:
2655 err = got_error(GOT_ERR_OBJ_TYPE);
2656 break;
2658 done:
2659 if (f && fflush(f) != 0 && err == NULL)
2660 err = got_error_from_errno("fflush");
2661 return err;
2664 static void
2665 diff_view_indicate_progress(struct tog_view *view)
2667 mvwaddstr(view->window, 0, 0, "diffing...");
2668 update_panels();
2669 doupdate();
2672 static const struct got_error *
2673 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2674 struct got_object_id *id2, struct tog_view *log_view,
2675 struct got_reflist_head *refs, struct got_repository *repo)
2677 const struct got_error *err;
2679 if (id1 != NULL && id2 != NULL) {
2680 int type1, type2;
2681 err = got_object_get_type(&type1, repo, id1);
2682 if (err)
2683 return err;
2684 err = got_object_get_type(&type2, repo, id2);
2685 if (err)
2686 return err;
2688 if (type1 != type2)
2689 return got_error(GOT_ERR_OBJ_TYPE);
2692 if (id1) {
2693 view->state.diff.id1 = got_object_id_dup(id1);
2694 if (view->state.diff.id1 == NULL)
2695 return got_error_from_errno("got_object_id_dup");
2696 } else
2697 view->state.diff.id1 = NULL;
2699 view->state.diff.id2 = got_object_id_dup(id2);
2700 if (view->state.diff.id2 == NULL) {
2701 free(view->state.diff.id1);
2702 view->state.diff.id1 = NULL;
2703 return got_error_from_errno("got_object_id_dup");
2705 view->state.diff.f = NULL;
2706 view->state.diff.first_displayed_line = 1;
2707 view->state.diff.last_displayed_line = view->nlines;
2708 view->state.diff.diff_context = 3;
2709 view->state.diff.log_view = log_view;
2710 view->state.diff.repo = repo;
2711 view->state.diff.refs = refs;
2713 if (log_view && view_is_splitscreen(view))
2714 show_log_view(log_view); /* draw vborder */
2715 diff_view_indicate_progress(view);
2717 err = create_diff(&view->state.diff);
2718 if (err) {
2719 free(view->state.diff.id1);
2720 view->state.diff.id1 = NULL;
2721 free(view->state.diff.id2);
2722 view->state.diff.id2 = NULL;
2723 return err;
2726 view->show = show_diff_view;
2727 view->input = input_diff_view;
2728 view->close = close_diff_view;
2730 return NULL;
2733 static const struct got_error *
2734 close_diff_view(struct tog_view *view)
2736 const struct got_error *err = NULL;
2738 free(view->state.diff.id1);
2739 view->state.diff.id1 = NULL;
2740 free(view->state.diff.id2);
2741 view->state.diff.id2 = NULL;
2742 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2743 err = got_error_from_errno("fclose");
2744 return err;
2747 static const struct got_error *
2748 show_diff_view(struct tog_view *view)
2750 const struct got_error *err;
2751 struct tog_diff_view_state *s = &view->state.diff;
2752 char *id_str1 = NULL, *id_str2, *header;
2754 if (s->id1) {
2755 err = got_object_id_str(&id_str1, s->id1);
2756 if (err)
2757 return err;
2759 err = got_object_id_str(&id_str2, s->id2);
2760 if (err)
2761 return err;
2763 if (asprintf(&header, "diff %s %s",
2764 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2765 err = got_error_from_errno("asprintf");
2766 free(id_str1);
2767 free(id_str2);
2768 return err;
2770 free(id_str1);
2771 free(id_str2);
2773 return draw_file(view, s->f, &s->first_displayed_line,
2774 &s->last_displayed_line, &s->eof, view->nlines,
2775 header);
2778 static const struct got_error *
2779 set_selected_commit(struct tog_diff_view_state *s,
2780 struct commit_queue_entry *entry)
2782 const struct got_error *err;
2783 const struct got_object_id_queue *parent_ids;
2784 struct got_commit_object *selected_commit;
2785 struct got_object_qid *pid;
2787 free(s->id2);
2788 s->id2 = got_object_id_dup(entry->id);
2789 if (s->id2 == NULL)
2790 return got_error_from_errno("got_object_id_dup");
2792 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2793 if (err)
2794 return err;
2795 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2796 free(s->id1);
2797 pid = SIMPLEQ_FIRST(parent_ids);
2798 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2799 got_object_commit_close(selected_commit);
2800 return NULL;
2803 static const struct got_error *
2804 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2805 struct tog_view **focus_view, struct tog_view *view, int ch)
2807 const struct got_error *err = NULL;
2808 struct tog_diff_view_state *s = &view->state.diff;
2809 struct tog_log_view_state *ls;
2810 struct commit_queue_entry *entry;
2811 int i;
2813 switch (ch) {
2814 case 'k':
2815 case KEY_UP:
2816 if (s->first_displayed_line > 1)
2817 s->first_displayed_line--;
2818 break;
2819 case KEY_PPAGE:
2820 case CTRL('b'):
2821 if (s->first_displayed_line == 1)
2822 break;
2823 i = 0;
2824 while (i++ < view->nlines - 1 &&
2825 s->first_displayed_line > 1)
2826 s->first_displayed_line--;
2827 break;
2828 case 'j':
2829 case KEY_DOWN:
2830 if (!s->eof)
2831 s->first_displayed_line++;
2832 break;
2833 case KEY_NPAGE:
2834 case CTRL('f'):
2835 case ' ':
2836 if (s->eof)
2837 break;
2838 i = 0;
2839 while (!s->eof && i++ < view->nlines - 1) {
2840 char *line;
2841 line = parse_next_line(s->f, NULL);
2842 s->first_displayed_line++;
2843 if (line == NULL)
2844 break;
2846 break;
2847 case '[':
2848 if (s->diff_context > 0) {
2849 s->diff_context--;
2850 diff_view_indicate_progress(view);
2851 err = create_diff(s);
2853 break;
2854 case ']':
2855 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2856 s->diff_context++;
2857 diff_view_indicate_progress(view);
2858 err = create_diff(s);
2860 break;
2861 case '<':
2862 case ',':
2863 if (s->log_view == NULL)
2864 break;
2865 ls = &s->log_view->state.log;
2866 entry = TAILQ_PREV(ls->selected_entry,
2867 commit_queue_head, entry);
2868 if (entry == NULL)
2869 break;
2871 err = input_log_view(NULL, NULL, NULL, s->log_view,
2872 KEY_UP);
2873 if (err)
2874 break;
2876 err = set_selected_commit(s, entry);
2877 if (err)
2878 break;
2880 s->first_displayed_line = 1;
2881 s->last_displayed_line = view->nlines;
2883 diff_view_indicate_progress(view);
2884 err = create_diff(s);
2885 break;
2886 case '>':
2887 case '.':
2888 if (s->log_view == NULL)
2889 break;
2890 ls = &s->log_view->state.log;
2892 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2893 ls->thread_args.commits_needed++;
2895 /* Display "loading..." in log view. */
2896 show_log_view(s->log_view);
2897 update_panels();
2898 doupdate();
2900 err = trigger_log_thread(1 /* load_all */,
2901 &ls->thread_args.commits_needed,
2902 &ls->thread_args.log_complete,
2903 &ls->thread_args.need_commits);
2904 if (err)
2905 break;
2907 err = input_log_view(NULL, NULL, NULL, s->log_view,
2908 KEY_DOWN);
2909 if (err)
2910 break;
2912 entry = TAILQ_NEXT(ls->selected_entry, entry);
2913 if (entry == NULL)
2914 break;
2916 err = set_selected_commit(s, entry);
2917 if (err)
2918 break;
2920 s->first_displayed_line = 1;
2921 s->last_displayed_line = view->nlines;
2923 diff_view_indicate_progress(view);
2924 err = create_diff(s);
2925 break;
2926 default:
2927 break;
2930 return err;
2933 static const struct got_error *
2934 cmd_diff(int argc, char *argv[])
2936 const struct got_error *error = NULL;
2937 struct got_repository *repo = NULL;
2938 struct got_reflist_head refs;
2939 struct got_object_id *id1 = NULL, *id2 = NULL;
2940 char *repo_path = NULL;
2941 char *id_str1 = NULL, *id_str2 = NULL;
2942 int ch;
2943 struct tog_view *view;
2945 SIMPLEQ_INIT(&refs);
2947 #ifndef PROFILE
2948 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2949 NULL) == -1)
2950 err(1, "pledge");
2951 #endif
2953 while ((ch = getopt(argc, argv, "")) != -1) {
2954 switch (ch) {
2955 default:
2956 usage_diff();
2957 /* NOTREACHED */
2961 argc -= optind;
2962 argv += optind;
2964 if (argc == 0) {
2965 usage_diff(); /* TODO show local worktree changes */
2966 } else if (argc == 2) {
2967 repo_path = getcwd(NULL, 0);
2968 if (repo_path == NULL)
2969 return got_error_from_errno("getcwd");
2970 id_str1 = argv[0];
2971 id_str2 = argv[1];
2972 } else if (argc == 3) {
2973 repo_path = realpath(argv[0], NULL);
2974 if (repo_path == NULL)
2975 return got_error_from_errno2("realpath", argv[0]);
2976 id_str1 = argv[1];
2977 id_str2 = argv[2];
2978 } else
2979 usage_diff();
2981 init_curses();
2983 error = got_repo_open(&repo, repo_path, NULL);
2984 if (error)
2985 goto done;
2987 error = apply_unveil(got_repo_get_path(repo), NULL);
2988 if (error)
2989 goto done;
2991 error = got_repo_match_object_id_prefix(&id1, id_str1,
2992 GOT_OBJ_TYPE_ANY, repo);
2993 if (error)
2994 goto done;
2996 error = got_repo_match_object_id_prefix(&id2, id_str2,
2997 GOT_OBJ_TYPE_ANY, repo);
2998 if (error)
2999 goto done;
3001 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3002 if (error)
3003 goto done;
3005 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3006 if (view == NULL) {
3007 error = got_error_from_errno("view_open");
3008 goto done;
3010 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3011 if (error)
3012 goto done;
3013 error = view_loop(view);
3014 done:
3015 free(repo_path);
3016 if (repo)
3017 got_repo_close(repo);
3018 got_ref_list_free(&refs);
3019 return error;
3022 __dead static void
3023 usage_blame(void)
3025 endwin();
3026 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3027 getprogname());
3028 exit(1);
3031 struct tog_blame_line {
3032 int annotated;
3033 struct got_object_id *id;
3036 static const struct got_error *
3037 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3038 const char *path, struct tog_blame_line *lines, int nlines,
3039 int blame_complete, int selected_line, int *first_displayed_line,
3040 int *last_displayed_line, int *eof, int max_lines)
3042 const struct got_error *err;
3043 int lineno = 0, nprinted = 0;
3044 char *line;
3045 size_t len;
3046 wchar_t *wline;
3047 int width;
3048 struct tog_blame_line *blame_line;
3049 struct got_object_id *prev_id = NULL;
3050 char *id_str;
3052 err = got_object_id_str(&id_str, id);
3053 if (err)
3054 return err;
3056 rewind(f);
3057 werase(view->window);
3059 if (asprintf(&line, "commit %s", id_str) == -1) {
3060 err = got_error_from_errno("asprintf");
3061 free(id_str);
3062 return err;
3065 err = format_line(&wline, &width, line, view->ncols, 0);
3066 free(line);
3067 line = NULL;
3068 if (err)
3069 return err;
3070 if (view_needs_focus_indication(view))
3071 wstandout(view->window);
3072 waddwstr(view->window, wline);
3073 if (view_needs_focus_indication(view))
3074 wstandend(view->window);
3075 free(wline);
3076 wline = NULL;
3077 if (width < view->ncols - 1)
3078 waddch(view->window, '\n');
3080 if (asprintf(&line, "[%d/%d] %s%s",
3081 *first_displayed_line - 1 + selected_line, nlines,
3082 blame_complete ? "" : "annotating... ", path) == -1) {
3083 free(id_str);
3084 return got_error_from_errno("asprintf");
3086 free(id_str);
3087 err = format_line(&wline, &width, line, view->ncols, 0);
3088 free(line);
3089 line = NULL;
3090 if (err)
3091 return err;
3092 waddwstr(view->window, wline);
3093 free(wline);
3094 wline = NULL;
3095 if (width < view->ncols - 1)
3096 waddch(view->window, '\n');
3098 *eof = 0;
3099 while (nprinted < max_lines - 2) {
3100 line = parse_next_line(f, &len);
3101 if (line == NULL) {
3102 *eof = 1;
3103 break;
3105 if (++lineno < *first_displayed_line) {
3106 free(line);
3107 continue;
3110 if (view->ncols <= 9) {
3111 width = 9;
3112 wline = wcsdup(L"");
3113 if (wline == NULL)
3114 err = got_error_from_errno("wcsdup");
3115 } else {
3116 err = format_line(&wline, &width, line,
3117 view->ncols - 9, 9);
3118 width += 9;
3120 if (err) {
3121 free(line);
3122 return err;
3125 if (view->focussed && nprinted == selected_line - 1)
3126 wstandout(view->window);
3128 if (nlines > 0) {
3129 blame_line = &lines[lineno - 1];
3130 if (blame_line->annotated && prev_id &&
3131 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3132 !(view->focussed &&
3133 nprinted == selected_line - 1)) {
3134 waddstr(view->window, " ");
3135 } else if (blame_line->annotated) {
3136 char *id_str;
3137 err = got_object_id_str(&id_str, blame_line->id);
3138 if (err) {
3139 free(line);
3140 free(wline);
3141 return err;
3143 wprintw(view->window, "%.8s", id_str);
3144 free(id_str);
3145 prev_id = blame_line->id;
3146 } else {
3147 waddstr(view->window, "........");
3148 prev_id = NULL;
3150 } else {
3151 waddstr(view->window, "........");
3152 prev_id = NULL;
3155 if (view->focussed && nprinted == selected_line - 1)
3156 wstandend(view->window);
3157 waddstr(view->window, " ");
3159 waddwstr(view->window, wline);
3160 if (width <= view->ncols - 1)
3161 waddch(view->window, '\n');
3162 if (++nprinted == 1)
3163 *first_displayed_line = lineno;
3164 free(line);
3165 free(wline);
3166 wline = NULL;
3168 *last_displayed_line = lineno;
3170 view_vborder(view);
3172 return NULL;
3175 static const struct got_error *
3176 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3178 const struct got_error *err = NULL;
3179 struct tog_blame_cb_args *a = arg;
3180 struct tog_blame_line *line;
3181 int errcode;
3183 if (nlines != a->nlines ||
3184 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3185 return got_error(GOT_ERR_RANGE);
3187 errcode = pthread_mutex_lock(&tog_mutex);
3188 if (errcode)
3189 return got_error_set_errno(errcode, "pthread_mutex_lock");
3191 if (*a->quit) { /* user has quit the blame view */
3192 err = got_error(GOT_ERR_ITER_COMPLETED);
3193 goto done;
3196 if (lineno == -1)
3197 goto done; /* no change in this commit */
3199 line = &a->lines[lineno - 1];
3200 if (line->annotated)
3201 goto done;
3203 line->id = got_object_id_dup(id);
3204 if (line->id == NULL) {
3205 err = got_error_from_errno("got_object_id_dup");
3206 goto done;
3208 line->annotated = 1;
3209 done:
3210 errcode = pthread_mutex_unlock(&tog_mutex);
3211 if (errcode)
3212 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3213 return err;
3216 static void *
3217 blame_thread(void *arg)
3219 const struct got_error *err;
3220 struct tog_blame_thread_args *ta = arg;
3221 struct tog_blame_cb_args *a = ta->cb_args;
3222 int errcode;
3224 err = got_blame(ta->path, a->commit_id, ta->repo,
3225 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3226 if (err && err->code == GOT_ERR_CANCELLED)
3227 err = NULL;
3229 errcode = pthread_mutex_lock(&tog_mutex);
3230 if (errcode)
3231 return (void *)got_error_set_errno(errcode,
3232 "pthread_mutex_lock");
3234 got_repo_close(ta->repo);
3235 ta->repo = NULL;
3236 *ta->complete = 1;
3238 errcode = pthread_mutex_unlock(&tog_mutex);
3239 if (errcode && err == NULL)
3240 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3242 return (void *)err;
3245 static struct got_object_id *
3246 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3247 int first_displayed_line, int selected_line)
3249 struct tog_blame_line *line;
3251 if (nlines <= 0)
3252 return NULL;
3254 line = &lines[first_displayed_line - 1 + selected_line - 1];
3255 if (!line->annotated)
3256 return NULL;
3258 return line->id;
3261 static const struct got_error *
3262 stop_blame(struct tog_blame *blame)
3264 const struct got_error *err = NULL;
3265 int i;
3267 if (blame->thread) {
3268 int errcode;
3269 errcode = pthread_mutex_unlock(&tog_mutex);
3270 if (errcode)
3271 return got_error_set_errno(errcode,
3272 "pthread_mutex_unlock");
3273 errcode = pthread_join(blame->thread, (void **)&err);
3274 if (errcode)
3275 return got_error_set_errno(errcode, "pthread_join");
3276 errcode = pthread_mutex_lock(&tog_mutex);
3277 if (errcode)
3278 return got_error_set_errno(errcode,
3279 "pthread_mutex_lock");
3280 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3281 err = NULL;
3282 blame->thread = NULL;
3284 if (blame->thread_args.repo) {
3285 got_repo_close(blame->thread_args.repo);
3286 blame->thread_args.repo = NULL;
3288 if (blame->f) {
3289 if (fclose(blame->f) != 0 && err == NULL)
3290 err = got_error_from_errno("fclose");
3291 blame->f = NULL;
3293 if (blame->lines) {
3294 for (i = 0; i < blame->nlines; i++)
3295 free(blame->lines[i].id);
3296 free(blame->lines);
3297 blame->lines = NULL;
3299 free(blame->cb_args.commit_id);
3300 blame->cb_args.commit_id = NULL;
3302 return err;
3305 static const struct got_error *
3306 cancel_blame_view(void *arg)
3308 const struct got_error *err = NULL;
3309 int *done = arg;
3310 int errcode;
3312 errcode = pthread_mutex_lock(&tog_mutex);
3313 if (errcode)
3314 return got_error_set_errno(errcode,
3315 "pthread_mutex_unlock");
3317 if (*done)
3318 err = got_error(GOT_ERR_CANCELLED);
3320 errcode = pthread_mutex_unlock(&tog_mutex);
3321 if (errcode)
3322 return got_error_set_errno(errcode,
3323 "pthread_mutex_lock");
3325 return err;
3328 static const struct got_error *
3329 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3330 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3331 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3332 struct got_repository *repo)
3334 const struct got_error *err = NULL;
3335 struct got_blob_object *blob = NULL;
3336 struct got_repository *thread_repo = NULL;
3337 struct got_object_id *obj_id = NULL;
3338 int obj_type;
3340 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3341 if (err)
3342 return err;
3343 if (obj_id == NULL)
3344 return got_error(GOT_ERR_NO_OBJ);
3346 err = got_object_get_type(&obj_type, repo, obj_id);
3347 if (err)
3348 goto done;
3350 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3351 err = got_error(GOT_ERR_OBJ_TYPE);
3352 goto done;
3355 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3356 if (err)
3357 goto done;
3358 blame->f = got_opentemp();
3359 if (blame->f == NULL) {
3360 err = got_error_from_errno("got_opentemp");
3361 goto done;
3363 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3364 &blame->line_offsets, blame->f, blob);
3365 if (err || blame->nlines == 0)
3366 goto done;
3368 /* Don't include \n at EOF in the blame line count. */
3369 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3370 blame->nlines--;
3372 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3373 if (blame->lines == NULL) {
3374 err = got_error_from_errno("calloc");
3375 goto done;
3378 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3379 if (err)
3380 goto done;
3382 blame->cb_args.view = view;
3383 blame->cb_args.lines = blame->lines;
3384 blame->cb_args.nlines = blame->nlines;
3385 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3386 if (blame->cb_args.commit_id == NULL) {
3387 err = got_error_from_errno("got_object_id_dup");
3388 goto done;
3390 blame->cb_args.quit = done;
3392 blame->thread_args.path = path;
3393 blame->thread_args.repo = thread_repo;
3394 blame->thread_args.cb_args = &blame->cb_args;
3395 blame->thread_args.complete = blame_complete;
3396 blame->thread_args.cancel_cb = cancel_blame_view;
3397 blame->thread_args.cancel_arg = done;
3398 *blame_complete = 0;
3400 done:
3401 if (blob)
3402 got_object_blob_close(blob);
3403 free(obj_id);
3404 if (err)
3405 stop_blame(blame);
3406 return err;
3409 static const struct got_error *
3410 open_blame_view(struct tog_view *view, char *path,
3411 struct got_object_id *commit_id, struct got_reflist_head *refs,
3412 struct got_repository *repo)
3414 const struct got_error *err = NULL;
3415 struct tog_blame_view_state *s = &view->state.blame;
3417 SIMPLEQ_INIT(&s->blamed_commits);
3419 s->path = strdup(path);
3420 if (s->path == NULL)
3421 return got_error_from_errno("strdup");
3423 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3424 if (err) {
3425 free(s->path);
3426 return err;
3429 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3430 s->first_displayed_line = 1;
3431 s->last_displayed_line = view->nlines;
3432 s->selected_line = 1;
3433 s->blame_complete = 0;
3434 s->repo = repo;
3435 s->refs = refs;
3436 s->commit_id = commit_id;
3437 memset(&s->blame, 0, sizeof(s->blame));
3439 view->show = show_blame_view;
3440 view->input = input_blame_view;
3441 view->close = close_blame_view;
3442 view->search_start = search_start_blame_view;
3443 view->search_next = search_next_blame_view;
3445 return run_blame(&s->blame, view, &s->blame_complete,
3446 &s->first_displayed_line, &s->last_displayed_line,
3447 &s->selected_line, &s->done, &s->eof, s->path,
3448 s->blamed_commit->id, s->repo);
3451 static const struct got_error *
3452 close_blame_view(struct tog_view *view)
3454 const struct got_error *err = NULL;
3455 struct tog_blame_view_state *s = &view->state.blame;
3457 if (s->blame.thread)
3458 err = stop_blame(&s->blame);
3460 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3461 struct got_object_qid *blamed_commit;
3462 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3463 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3464 got_object_qid_free(blamed_commit);
3467 free(s->path);
3469 return err;
3472 static const struct got_error *
3473 search_start_blame_view(struct tog_view *view)
3475 struct tog_blame_view_state *s = &view->state.blame;
3477 s->matched_line = 0;
3478 return NULL;
3481 static int
3482 match_line(const char *line, regex_t *regex)
3484 regmatch_t regmatch;
3486 return regexec(regex, line, 1, &regmatch, 0) == 0;
3490 static const struct got_error *
3491 search_next_blame_view(struct tog_view *view)
3493 struct tog_blame_view_state *s = &view->state.blame;
3494 int lineno;
3496 if (!view->searching) {
3497 view->search_next_done = 1;
3498 return NULL;
3501 if (s->matched_line) {
3502 if (view->searching == TOG_SEARCH_FORWARD)
3503 lineno = s->matched_line + 1;
3504 else
3505 lineno = s->matched_line - 1;
3506 } else {
3507 if (view->searching == TOG_SEARCH_FORWARD)
3508 lineno = 1;
3509 else
3510 lineno = s->blame.nlines;
3513 while (1) {
3514 char *line = NULL;
3515 off_t offset;
3516 size_t len;
3518 if (lineno <= 0 || lineno > s->blame.nlines) {
3519 if (s->matched_line == 0) {
3520 view->search_next_done = 1;
3521 free(line);
3522 break;
3525 if (view->searching == TOG_SEARCH_FORWARD)
3526 lineno = 1;
3527 else
3528 lineno = s->blame.nlines;
3531 offset = s->blame.line_offsets[lineno - 1];
3532 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3533 free(line);
3534 return got_error_from_errno("fseeko");
3536 free(line);
3537 line = parse_next_line(s->blame.f, &len);
3538 if (line && match_line(line, &view->regex)) {
3539 view->search_next_done = 1;
3540 s->matched_line = lineno;
3541 free(line);
3542 break;
3544 free(line);
3545 if (view->searching == TOG_SEARCH_FORWARD)
3546 lineno++;
3547 else
3548 lineno--;
3551 if (s->matched_line) {
3552 s->first_displayed_line = s->matched_line;
3553 s->selected_line = 1;
3556 return NULL;
3559 static const struct got_error *
3560 show_blame_view(struct tog_view *view)
3562 const struct got_error *err = NULL;
3563 struct tog_blame_view_state *s = &view->state.blame;
3564 int errcode;
3566 if (s->blame.thread == NULL) {
3567 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3568 &s->blame.thread_args);
3569 if (errcode)
3570 return got_error_set_errno(errcode, "pthread_create");
3572 halfdelay(1); /* fast refresh while annotating */
3575 if (s->blame_complete)
3576 halfdelay(10); /* disable fast refresh */
3578 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3579 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3580 s->selected_line, &s->first_displayed_line,
3581 &s->last_displayed_line, &s->eof, view->nlines);
3583 view_vborder(view);
3584 return err;
3587 static const struct got_error *
3588 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3589 struct tog_view **focus_view, struct tog_view *view, int ch)
3591 const struct got_error *err = NULL, *thread_err = NULL;
3592 struct tog_view *diff_view;
3593 struct tog_blame_view_state *s = &view->state.blame;
3594 int begin_x = 0;
3596 switch (ch) {
3597 case 'q':
3598 s->done = 1;
3599 break;
3600 case 'k':
3601 case KEY_UP:
3602 if (s->selected_line > 1)
3603 s->selected_line--;
3604 else if (s->selected_line == 1 &&
3605 s->first_displayed_line > 1)
3606 s->first_displayed_line--;
3607 break;
3608 case KEY_PPAGE:
3609 if (s->first_displayed_line == 1) {
3610 s->selected_line = 1;
3611 break;
3613 if (s->first_displayed_line > view->nlines - 2)
3614 s->first_displayed_line -=
3615 (view->nlines - 2);
3616 else
3617 s->first_displayed_line = 1;
3618 break;
3619 case 'j':
3620 case KEY_DOWN:
3621 if (s->selected_line < view->nlines - 2 &&
3622 s->first_displayed_line +
3623 s->selected_line <= s->blame.nlines)
3624 s->selected_line++;
3625 else if (s->last_displayed_line <
3626 s->blame.nlines)
3627 s->first_displayed_line++;
3628 break;
3629 case 'b':
3630 case 'p': {
3631 struct got_object_id *id = NULL;
3632 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3633 s->first_displayed_line, s->selected_line);
3634 if (id == NULL)
3635 break;
3636 if (ch == 'p') {
3637 struct got_commit_object *commit;
3638 struct got_object_qid *pid;
3639 struct got_object_id *blob_id = NULL;
3640 int obj_type;
3641 err = got_object_open_as_commit(&commit,
3642 s->repo, id);
3643 if (err)
3644 break;
3645 pid = SIMPLEQ_FIRST(
3646 got_object_commit_get_parent_ids(commit));
3647 if (pid == NULL) {
3648 got_object_commit_close(commit);
3649 break;
3651 /* Check if path history ends here. */
3652 err = got_object_id_by_path(&blob_id, s->repo,
3653 pid->id, s->path);
3654 if (err) {
3655 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3656 err = NULL;
3657 got_object_commit_close(commit);
3658 break;
3660 err = got_object_get_type(&obj_type, s->repo,
3661 blob_id);
3662 free(blob_id);
3663 /* Can't blame non-blob type objects. */
3664 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3665 got_object_commit_close(commit);
3666 break;
3668 err = got_object_qid_alloc(&s->blamed_commit,
3669 pid->id);
3670 got_object_commit_close(commit);
3671 } else {
3672 if (got_object_id_cmp(id,
3673 s->blamed_commit->id) == 0)
3674 break;
3675 err = got_object_qid_alloc(&s->blamed_commit,
3676 id);
3678 if (err)
3679 break;
3680 s->done = 1;
3681 thread_err = stop_blame(&s->blame);
3682 s->done = 0;
3683 if (thread_err)
3684 break;
3685 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3686 s->blamed_commit, entry);
3687 err = run_blame(&s->blame, view, &s->blame_complete,
3688 &s->first_displayed_line, &s->last_displayed_line,
3689 &s->selected_line, &s->done, &s->eof,
3690 s->path, s->blamed_commit->id, s->repo);
3691 if (err)
3692 break;
3693 break;
3695 case 'B': {
3696 struct got_object_qid *first;
3697 first = SIMPLEQ_FIRST(&s->blamed_commits);
3698 if (!got_object_id_cmp(first->id, s->commit_id))
3699 break;
3700 s->done = 1;
3701 thread_err = stop_blame(&s->blame);
3702 s->done = 0;
3703 if (thread_err)
3704 break;
3705 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3706 got_object_qid_free(s->blamed_commit);
3707 s->blamed_commit =
3708 SIMPLEQ_FIRST(&s->blamed_commits);
3709 err = run_blame(&s->blame, view, &s->blame_complete,
3710 &s->first_displayed_line, &s->last_displayed_line,
3711 &s->selected_line, &s->done, &s->eof, s->path,
3712 s->blamed_commit->id, s->repo);
3713 if (err)
3714 break;
3715 break;
3717 case KEY_ENTER:
3718 case '\r': {
3719 struct got_object_id *id = NULL;
3720 struct got_object_qid *pid;
3721 struct got_commit_object *commit = NULL;
3722 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3723 s->first_displayed_line, s->selected_line);
3724 if (id == NULL)
3725 break;
3726 err = got_object_open_as_commit(&commit, s->repo, id);
3727 if (err)
3728 break;
3729 pid = SIMPLEQ_FIRST(
3730 got_object_commit_get_parent_ids(commit));
3731 if (view_is_parent_view(view))
3732 begin_x = view_split_begin_x(view->begin_x);
3733 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3734 if (diff_view == NULL) {
3735 got_object_commit_close(commit);
3736 err = got_error_from_errno("view_open");
3737 break;
3739 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3740 id, NULL, s->refs, s->repo);
3741 got_object_commit_close(commit);
3742 if (err) {
3743 view_close(diff_view);
3744 break;
3746 if (view_is_parent_view(view)) {
3747 err = view_close_child(view);
3748 if (err)
3749 break;
3750 err = view_set_child(view, diff_view);
3751 if (err) {
3752 view_close(diff_view);
3753 break;
3755 *focus_view = diff_view;
3756 view->child_focussed = 1;
3757 } else
3758 *new_view = diff_view;
3759 if (err)
3760 break;
3761 break;
3763 case KEY_NPAGE:
3764 case ' ':
3765 if (s->last_displayed_line >= s->blame.nlines &&
3766 s->selected_line >= MIN(s->blame.nlines,
3767 view->nlines - 2)) {
3768 break;
3770 if (s->last_displayed_line >= s->blame.nlines &&
3771 s->selected_line < view->nlines - 2) {
3772 s->selected_line = MIN(s->blame.nlines,
3773 view->nlines - 2);
3774 break;
3776 if (s->last_displayed_line + view->nlines - 2
3777 <= s->blame.nlines)
3778 s->first_displayed_line +=
3779 view->nlines - 2;
3780 else
3781 s->first_displayed_line =
3782 s->blame.nlines -
3783 (view->nlines - 3);
3784 break;
3785 case KEY_RESIZE:
3786 if (s->selected_line > view->nlines - 2) {
3787 s->selected_line = MIN(s->blame.nlines,
3788 view->nlines - 2);
3790 break;
3791 default:
3792 break;
3794 return thread_err ? thread_err : err;
3797 static const struct got_error *
3798 cmd_blame(int argc, char *argv[])
3800 const struct got_error *error;
3801 struct got_repository *repo = NULL;
3802 struct got_reflist_head refs;
3803 struct got_worktree *worktree = NULL;
3804 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3805 struct got_object_id *commit_id = NULL;
3806 char *commit_id_str = NULL;
3807 int ch;
3808 struct tog_view *view;
3810 SIMPLEQ_INIT(&refs);
3812 #ifndef PROFILE
3813 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3814 NULL) == -1)
3815 err(1, "pledge");
3816 #endif
3818 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3819 switch (ch) {
3820 case 'c':
3821 commit_id_str = optarg;
3822 break;
3823 case 'r':
3824 repo_path = realpath(optarg, NULL);
3825 if (repo_path == NULL)
3826 err(1, "-r option");
3827 break;
3828 default:
3829 usage_blame();
3830 /* NOTREACHED */
3834 argc -= optind;
3835 argv += optind;
3837 if (argc == 1)
3838 path = argv[0];
3839 else
3840 usage_blame();
3842 cwd = getcwd(NULL, 0);
3843 if (cwd == NULL) {
3844 error = got_error_from_errno("getcwd");
3845 goto done;
3847 if (repo_path == NULL) {
3848 error = got_worktree_open(&worktree, cwd);
3849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3850 goto done;
3851 else
3852 error = NULL;
3853 if (worktree) {
3854 repo_path =
3855 strdup(got_worktree_get_repo_path(worktree));
3856 if (repo_path == NULL)
3857 error = got_error_from_errno("strdup");
3858 if (error)
3859 goto done;
3860 } else {
3861 repo_path = strdup(cwd);
3862 if (repo_path == NULL) {
3863 error = got_error_from_errno("strdup");
3864 goto done;
3869 init_curses();
3871 error = got_repo_open(&repo, repo_path, NULL);
3872 if (error != NULL)
3873 goto done;
3875 error = apply_unveil(got_repo_get_path(repo), NULL);
3876 if (error)
3877 goto done;
3879 if (worktree) {
3880 const char *prefix = got_worktree_get_path_prefix(worktree);
3881 char *p, *worktree_subdir = cwd +
3882 strlen(got_worktree_get_root_path(worktree));
3883 if (asprintf(&p, "%s%s%s%s%s",
3884 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3885 worktree_subdir, worktree_subdir[0] ? "/" : "",
3886 path) == -1) {
3887 error = got_error_from_errno("asprintf");
3888 goto done;
3890 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3891 free(p);
3892 } else {
3893 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3895 if (error)
3896 goto done;
3898 if (commit_id_str == NULL) {
3899 struct got_reference *head_ref;
3900 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3901 if (error != NULL)
3902 goto done;
3903 error = got_ref_resolve(&commit_id, repo, head_ref);
3904 got_ref_close(head_ref);
3905 } else {
3906 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3907 if (error) {
3908 if (error->code != GOT_ERR_NOT_REF)
3909 goto done;
3910 error = got_repo_match_object_id_prefix(&commit_id,
3911 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3914 if (error != NULL)
3915 goto done;
3917 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3918 if (error)
3919 goto done;
3921 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3922 if (view == NULL) {
3923 error = got_error_from_errno("view_open");
3924 goto done;
3926 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3927 if (error)
3928 goto done;
3929 if (worktree) {
3930 /* Release work tree lock. */
3931 got_worktree_close(worktree);
3932 worktree = NULL;
3934 error = view_loop(view);
3935 done:
3936 free(repo_path);
3937 free(cwd);
3938 free(commit_id);
3939 if (worktree)
3940 got_worktree_close(worktree);
3941 if (repo)
3942 got_repo_close(repo);
3943 got_ref_list_free(&refs);
3944 return error;
3947 static const struct got_error *
3948 draw_tree_entries(struct tog_view *view,
3949 struct got_tree_entry **first_displayed_entry,
3950 struct got_tree_entry **last_displayed_entry,
3951 struct got_tree_entry **selected_entry, int *ndisplayed,
3952 const char *label, int show_ids, const char *parent_path,
3953 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3955 const struct got_error *err = NULL;
3956 struct got_tree_entry *te;
3957 wchar_t *wline;
3958 int width, n;
3960 *ndisplayed = 0;
3962 werase(view->window);
3964 if (limit == 0)
3965 return NULL;
3967 err = format_line(&wline, &width, label, view->ncols, 0);
3968 if (err)
3969 return err;
3970 if (view_needs_focus_indication(view))
3971 wstandout(view->window);
3972 waddwstr(view->window, wline);
3973 if (view_needs_focus_indication(view))
3974 wstandend(view->window);
3975 free(wline);
3976 wline = NULL;
3977 if (width < view->ncols - 1)
3978 waddch(view->window, '\n');
3979 if (--limit <= 0)
3980 return NULL;
3981 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3982 if (err)
3983 return err;
3984 waddwstr(view->window, wline);
3985 free(wline);
3986 wline = NULL;
3987 if (width < view->ncols - 1)
3988 waddch(view->window, '\n');
3989 if (--limit <= 0)
3990 return NULL;
3991 waddch(view->window, '\n');
3992 if (--limit <= 0)
3993 return NULL;
3995 te = SIMPLEQ_FIRST(&entries->head);
3996 if (*first_displayed_entry == NULL) {
3997 if (selected == 0) {
3998 if (view->focussed)
3999 wstandout(view->window);
4000 *selected_entry = NULL;
4002 waddstr(view->window, " ..\n"); /* parent directory */
4003 if (selected == 0 && view->focussed)
4004 wstandend(view->window);
4005 (*ndisplayed)++;
4006 if (--limit <= 0)
4007 return NULL;
4008 n = 1;
4009 } else {
4010 n = 0;
4011 while (te != *first_displayed_entry)
4012 te = SIMPLEQ_NEXT(te, entry);
4015 while (te) {
4016 char *line = NULL, *id_str = NULL;
4017 const char *modestr = "";
4019 if (show_ids) {
4020 err = got_object_id_str(&id_str, te->id);
4021 if (err)
4022 return got_error_from_errno(
4023 "got_object_id_str");
4025 if (got_object_tree_entry_is_submodule(te))
4026 modestr = "$";
4027 else if (S_ISLNK(te->mode))
4028 modestr = "@";
4029 else if (S_ISDIR(te->mode))
4030 modestr = "/";
4031 else if (te->mode & S_IXUSR)
4032 modestr = "*";
4033 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4034 te->name, modestr) == -1) {
4035 free(id_str);
4036 return got_error_from_errno("asprintf");
4038 free(id_str);
4039 err = format_line(&wline, &width, line, view->ncols, 0);
4040 if (err) {
4041 free(line);
4042 break;
4044 if (n == selected) {
4045 if (view->focussed)
4046 wstandout(view->window);
4047 *selected_entry = te;
4049 waddwstr(view->window, wline);
4050 if (width < view->ncols - 1)
4051 waddch(view->window, '\n');
4052 if (n == selected && view->focussed)
4053 wstandend(view->window);
4054 free(line);
4055 free(wline);
4056 wline = NULL;
4057 n++;
4058 (*ndisplayed)++;
4059 *last_displayed_entry = te;
4060 if (--limit <= 0)
4061 break;
4062 te = SIMPLEQ_NEXT(te, entry);
4065 return err;
4068 static void
4069 tree_scroll_up(struct tog_view *view,
4070 struct got_tree_entry **first_displayed_entry, int maxscroll,
4071 const struct got_tree_entries *entries, int isroot)
4073 struct got_tree_entry *te, *prev;
4074 int i;
4076 if (*first_displayed_entry == NULL)
4077 return;
4079 te = SIMPLEQ_FIRST(&entries->head);
4080 if (*first_displayed_entry == te) {
4081 if (!isroot)
4082 *first_displayed_entry = NULL;
4083 return;
4086 /* XXX this is stupid... switch to TAILQ? */
4087 for (i = 0; i < maxscroll; i++) {
4088 while (te != *first_displayed_entry) {
4089 prev = te;
4090 te = SIMPLEQ_NEXT(te, entry);
4092 *first_displayed_entry = prev;
4093 te = SIMPLEQ_FIRST(&entries->head);
4095 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4096 *first_displayed_entry = NULL;
4099 static int
4100 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4101 struct got_tree_entry *last_displayed_entry,
4102 const struct got_tree_entries *entries)
4104 struct got_tree_entry *next, *last;
4105 int n = 0;
4107 if (*first_displayed_entry)
4108 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4109 else
4110 next = SIMPLEQ_FIRST(&entries->head);
4111 last = last_displayed_entry;
4112 while (next && last && n++ < maxscroll) {
4113 last = SIMPLEQ_NEXT(last, entry);
4114 if (last) {
4115 *first_displayed_entry = next;
4116 next = SIMPLEQ_NEXT(next, entry);
4119 return n;
4122 static const struct got_error *
4123 tree_entry_path(char **path, struct tog_parent_trees *parents,
4124 struct got_tree_entry *te)
4126 const struct got_error *err = NULL;
4127 struct tog_parent_tree *pt;
4128 size_t len = 2; /* for leading slash and NUL */
4130 TAILQ_FOREACH(pt, parents, entry)
4131 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4132 if (te)
4133 len += strlen(te->name);
4135 *path = calloc(1, len);
4136 if (path == NULL)
4137 return got_error_from_errno("calloc");
4139 (*path)[0] = '/';
4140 pt = TAILQ_LAST(parents, tog_parent_trees);
4141 while (pt) {
4142 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4143 err = got_error(GOT_ERR_NO_SPACE);
4144 goto done;
4146 if (strlcat(*path, "/", len) >= len) {
4147 err = got_error(GOT_ERR_NO_SPACE);
4148 goto done;
4150 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4152 if (te) {
4153 if (strlcat(*path, te->name, len) >= len) {
4154 err = got_error(GOT_ERR_NO_SPACE);
4155 goto done;
4158 done:
4159 if (err) {
4160 free(*path);
4161 *path = NULL;
4163 return err;
4166 static const struct got_error *
4167 blame_tree_entry(struct tog_view **new_view, int begin_x,
4168 struct got_tree_entry *te, struct tog_parent_trees *parents,
4169 struct got_object_id *commit_id, struct got_reflist_head *refs,
4170 struct got_repository *repo)
4172 const struct got_error *err = NULL;
4173 char *path;
4174 struct tog_view *blame_view;
4176 *new_view = NULL;
4178 err = tree_entry_path(&path, parents, te);
4179 if (err)
4180 return err;
4182 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4183 if (blame_view == NULL) {
4184 err = got_error_from_errno("view_open");
4185 goto done;
4188 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4189 if (err) {
4190 if (err->code == GOT_ERR_CANCELLED)
4191 err = NULL;
4192 view_close(blame_view);
4193 } else
4194 *new_view = blame_view;
4195 done:
4196 free(path);
4197 return err;
4200 static const struct got_error *
4201 log_tree_entry(struct tog_view **new_view, int begin_x,
4202 struct got_tree_entry *te, struct tog_parent_trees *parents,
4203 struct got_object_id *commit_id, struct got_reflist_head *refs,
4204 struct got_repository *repo)
4206 struct tog_view *log_view;
4207 const struct got_error *err = NULL;
4208 char *path;
4210 *new_view = NULL;
4212 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4213 if (log_view == NULL)
4214 return got_error_from_errno("view_open");
4216 err = tree_entry_path(&path, parents, te);
4217 if (err)
4218 return err;
4220 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4221 if (err)
4222 view_close(log_view);
4223 else
4224 *new_view = log_view;
4225 free(path);
4226 return err;
4229 static const struct got_error *
4230 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4231 struct got_object_id *commit_id, struct got_reflist_head *refs,
4232 struct got_repository *repo)
4234 const struct got_error *err = NULL;
4235 char *commit_id_str = NULL;
4236 struct tog_tree_view_state *s = &view->state.tree;
4238 TAILQ_INIT(&s->parents);
4240 err = got_object_id_str(&commit_id_str, commit_id);
4241 if (err != NULL)
4242 goto done;
4244 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4245 err = got_error_from_errno("asprintf");
4246 goto done;
4249 s->root = s->tree = root;
4250 s->entries = got_object_tree_get_entries(root);
4251 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4252 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4253 s->commit_id = got_object_id_dup(commit_id);
4254 if (s->commit_id == NULL) {
4255 err = got_error_from_errno("got_object_id_dup");
4256 goto done;
4258 s->refs = refs;
4259 s->repo = repo;
4261 view->show = show_tree_view;
4262 view->input = input_tree_view;
4263 view->close = close_tree_view;
4264 view->search_start = search_start_tree_view;
4265 view->search_next = search_next_tree_view;
4266 done:
4267 free(commit_id_str);
4268 if (err) {
4269 free(s->tree_label);
4270 s->tree_label = NULL;
4272 return err;
4275 static const struct got_error *
4276 close_tree_view(struct tog_view *view)
4278 struct tog_tree_view_state *s = &view->state.tree;
4280 free(s->tree_label);
4281 s->tree_label = NULL;
4282 free(s->commit_id);
4283 s->commit_id = NULL;
4284 while (!TAILQ_EMPTY(&s->parents)) {
4285 struct tog_parent_tree *parent;
4286 parent = TAILQ_FIRST(&s->parents);
4287 TAILQ_REMOVE(&s->parents, parent, entry);
4288 free(parent);
4291 if (s->tree != s->root)
4292 got_object_tree_close(s->tree);
4293 got_object_tree_close(s->root);
4295 return NULL;
4298 static const struct got_error *
4299 search_start_tree_view(struct tog_view *view)
4301 struct tog_tree_view_state *s = &view->state.tree;
4303 s->matched_entry = NULL;
4304 return NULL;
4307 static int
4308 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4310 regmatch_t regmatch;
4312 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4315 static const struct got_error *
4316 search_next_tree_view(struct tog_view *view)
4318 struct tog_tree_view_state *s = &view->state.tree;
4319 struct got_tree_entry *entry = NULL, *te;
4321 if (!view->searching) {
4322 view->search_next_done = 1;
4323 return NULL;
4326 if (s->matched_entry) {
4327 if (view->searching == TOG_SEARCH_FORWARD) {
4328 if (s->selected_entry)
4329 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4330 else
4331 entry = SIMPLEQ_FIRST(&s->entries->head);
4333 else {
4334 if (s->selected_entry == NULL) {
4335 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4336 entry = te;
4337 } else {
4338 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4339 entry = te;
4340 if (SIMPLEQ_NEXT(te, entry) ==
4341 s->selected_entry)
4342 break;
4346 } else {
4347 if (view->searching == TOG_SEARCH_FORWARD)
4348 entry = SIMPLEQ_FIRST(&s->entries->head);
4349 else {
4350 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4351 entry = te;
4355 while (1) {
4356 if (entry == NULL) {
4357 if (s->matched_entry == NULL) {
4358 view->search_next_done = 1;
4359 return NULL;
4361 if (view->searching == TOG_SEARCH_FORWARD)
4362 entry = SIMPLEQ_FIRST(&s->entries->head);
4363 else {
4364 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4365 entry = te;
4369 if (match_tree_entry(entry, &view->regex)) {
4370 view->search_next_done = 1;
4371 s->matched_entry = entry;
4372 break;
4375 if (view->searching == TOG_SEARCH_FORWARD)
4376 entry = SIMPLEQ_NEXT(entry, entry);
4377 else {
4378 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4379 entry = NULL;
4380 else {
4381 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4382 if (SIMPLEQ_NEXT(te, entry) == entry) {
4383 entry = te;
4384 break;
4391 if (s->matched_entry) {
4392 s->first_displayed_entry = s->matched_entry;
4393 s->selected = 0;
4396 return NULL;
4399 static const struct got_error *
4400 show_tree_view(struct tog_view *view)
4402 const struct got_error *err = NULL;
4403 struct tog_tree_view_state *s = &view->state.tree;
4404 char *parent_path;
4406 err = tree_entry_path(&parent_path, &s->parents, NULL);
4407 if (err)
4408 return err;
4410 err = draw_tree_entries(view, &s->first_displayed_entry,
4411 &s->last_displayed_entry, &s->selected_entry,
4412 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4413 s->entries, s->selected, view->nlines, s->tree == s->root);
4414 free(parent_path);
4416 view_vborder(view);
4417 return err;
4420 static const struct got_error *
4421 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4422 struct tog_view **focus_view, struct tog_view *view, int ch)
4424 const struct got_error *err = NULL;
4425 struct tog_tree_view_state *s = &view->state.tree;
4426 struct tog_view *log_view;
4427 int begin_x = 0, nscrolled;
4429 switch (ch) {
4430 case 'i':
4431 s->show_ids = !s->show_ids;
4432 break;
4433 case 'l':
4434 if (!s->selected_entry)
4435 break;
4436 if (view_is_parent_view(view))
4437 begin_x = view_split_begin_x(view->begin_x);
4438 err = log_tree_entry(&log_view, begin_x,
4439 s->selected_entry, &s->parents,
4440 s->commit_id, s->refs, s->repo);
4441 if (view_is_parent_view(view)) {
4442 err = view_close_child(view);
4443 if (err)
4444 return err;
4445 err = view_set_child(view, log_view);
4446 if (err) {
4447 view_close(log_view);
4448 break;
4450 *focus_view = log_view;
4451 view->child_focussed = 1;
4452 } else
4453 *new_view = log_view;
4454 break;
4455 case 'k':
4456 case KEY_UP:
4457 if (s->selected > 0) {
4458 s->selected--;
4459 if (s->selected == 0)
4460 break;
4462 if (s->selected > 0)
4463 break;
4464 tree_scroll_up(view, &s->first_displayed_entry, 1,
4465 s->entries, s->tree == s->root);
4466 break;
4467 case KEY_PPAGE:
4468 tree_scroll_up(view, &s->first_displayed_entry,
4469 MAX(0, view->nlines - 4 - s->selected), s->entries,
4470 s->tree == s->root);
4471 s->selected = 0;
4472 if (SIMPLEQ_FIRST(&s->entries->head) ==
4473 s->first_displayed_entry && s->tree != s->root)
4474 s->first_displayed_entry = NULL;
4475 break;
4476 case 'j':
4477 case KEY_DOWN:
4478 if (s->selected < s->ndisplayed - 1) {
4479 s->selected++;
4480 break;
4482 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4483 /* can't scroll any further */
4484 break;
4485 tree_scroll_down(&s->first_displayed_entry, 1,
4486 s->last_displayed_entry, s->entries);
4487 break;
4488 case KEY_NPAGE:
4489 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4490 == NULL) {
4491 /* can't scroll any further; move cursor down */
4492 if (s->selected < s->ndisplayed - 1)
4493 s->selected = s->ndisplayed - 1;
4494 break;
4496 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4497 view->nlines, s->last_displayed_entry, s->entries);
4498 if (nscrolled < view->nlines) {
4499 int ndisplayed = 0;
4500 struct got_tree_entry *te;
4501 te = s->first_displayed_entry;
4502 do {
4503 ndisplayed++;
4504 te = SIMPLEQ_NEXT(te, entry);
4505 } while (te);
4506 s->selected = ndisplayed - 1;
4508 break;
4509 case KEY_ENTER:
4510 case '\r':
4511 case KEY_BACKSPACE:
4512 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4513 struct tog_parent_tree *parent;
4514 /* user selected '..' */
4515 if (s->tree == s->root)
4516 break;
4517 parent = TAILQ_FIRST(&s->parents);
4518 TAILQ_REMOVE(&s->parents, parent,
4519 entry);
4520 got_object_tree_close(s->tree);
4521 s->tree = parent->tree;
4522 s->entries =
4523 got_object_tree_get_entries(s->tree);
4524 s->first_displayed_entry =
4525 parent->first_displayed_entry;
4526 s->selected_entry =
4527 parent->selected_entry;
4528 s->selected = parent->selected;
4529 free(parent);
4530 } else if (S_ISDIR(s->selected_entry->mode)) {
4531 struct got_tree_object *subtree;
4532 err = got_object_open_as_tree(&subtree,
4533 s->repo, s->selected_entry->id);
4534 if (err)
4535 break;
4536 err = tree_view_visit_subtree(subtree, s);
4537 if (err) {
4538 got_object_tree_close(subtree);
4539 break;
4541 } else if (S_ISREG(s->selected_entry->mode)) {
4542 struct tog_view *blame_view;
4543 int begin_x = view_is_parent_view(view) ?
4544 view_split_begin_x(view->begin_x) : 0;
4546 err = blame_tree_entry(&blame_view, begin_x,
4547 s->selected_entry, &s->parents,
4548 s->commit_id, s->refs, s->repo);
4549 if (err)
4550 break;
4551 if (view_is_parent_view(view)) {
4552 err = view_close_child(view);
4553 if (err)
4554 return err;
4555 err = view_set_child(view, blame_view);
4556 if (err) {
4557 view_close(blame_view);
4558 break;
4560 *focus_view = blame_view;
4561 view->child_focussed = 1;
4562 } else
4563 *new_view = blame_view;
4565 break;
4566 case KEY_RESIZE:
4567 if (s->selected > view->nlines)
4568 s->selected = s->ndisplayed - 1;
4569 break;
4570 default:
4571 break;
4574 return err;
4577 __dead static void
4578 usage_tree(void)
4580 endwin();
4581 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4582 getprogname());
4583 exit(1);
4586 static const struct got_error *
4587 cmd_tree(int argc, char *argv[])
4589 const struct got_error *error;
4590 struct got_repository *repo = NULL;
4591 struct got_reflist_head refs;
4592 char *repo_path = NULL;
4593 struct got_object_id *commit_id = NULL;
4594 char *commit_id_arg = NULL;
4595 struct got_commit_object *commit = NULL;
4596 struct got_tree_object *tree = NULL;
4597 int ch;
4598 struct tog_view *view;
4600 SIMPLEQ_INIT(&refs);
4602 #ifndef PROFILE
4603 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4604 NULL) == -1)
4605 err(1, "pledge");
4606 #endif
4608 while ((ch = getopt(argc, argv, "c:")) != -1) {
4609 switch (ch) {
4610 case 'c':
4611 commit_id_arg = optarg;
4612 break;
4613 default:
4614 usage_tree();
4615 /* NOTREACHED */
4619 argc -= optind;
4620 argv += optind;
4622 if (argc == 0) {
4623 struct got_worktree *worktree;
4624 char *cwd = getcwd(NULL, 0);
4625 if (cwd == NULL)
4626 return got_error_from_errno("getcwd");
4627 error = got_worktree_open(&worktree, cwd);
4628 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4629 goto done;
4630 if (worktree) {
4631 free(cwd);
4632 repo_path =
4633 strdup(got_worktree_get_repo_path(worktree));
4634 got_worktree_close(worktree);
4635 } else
4636 repo_path = cwd;
4637 if (repo_path == NULL) {
4638 error = got_error_from_errno("strdup");
4639 goto done;
4641 } else if (argc == 1) {
4642 repo_path = realpath(argv[0], NULL);
4643 if (repo_path == NULL)
4644 return got_error_from_errno2("realpath", argv[0]);
4645 } else
4646 usage_log();
4648 init_curses();
4650 error = got_repo_open(&repo, repo_path, NULL);
4651 if (error != NULL)
4652 goto done;
4654 error = apply_unveil(got_repo_get_path(repo), NULL);
4655 if (error)
4656 goto done;
4658 if (commit_id_arg == NULL)
4659 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4660 else {
4661 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4662 if (error) {
4663 if (error->code != GOT_ERR_NOT_REF)
4664 goto done;
4665 error = got_repo_match_object_id_prefix(&commit_id,
4666 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4669 if (error != NULL)
4670 goto done;
4672 error = got_object_open_as_commit(&commit, repo, commit_id);
4673 if (error != NULL)
4674 goto done;
4676 error = got_object_open_as_tree(&tree, repo,
4677 got_object_commit_get_tree_id(commit));
4678 if (error != NULL)
4679 goto done;
4681 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4682 if (error)
4683 goto done;
4685 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4686 if (view == NULL) {
4687 error = got_error_from_errno("view_open");
4688 goto done;
4690 error = open_tree_view(view, tree, commit_id, &refs, repo);
4691 if (error)
4692 goto done;
4693 error = view_loop(view);
4694 done:
4695 free(repo_path);
4696 free(commit_id);
4697 if (commit)
4698 got_object_commit_close(commit);
4699 if (tree)
4700 got_object_tree_close(tree);
4701 if (repo)
4702 got_repo_close(repo);
4703 got_ref_list_free(&refs);
4704 return error;
4707 static void
4708 list_commands(void)
4710 int i;
4712 fprintf(stderr, "commands:");
4713 for (i = 0; i < nitems(tog_commands); i++) {
4714 struct tog_cmd *cmd = &tog_commands[i];
4715 fprintf(stderr, " %s", cmd->name);
4717 fputc('\n', stderr);
4720 __dead static void
4721 usage(int hflag)
4723 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4724 getprogname());
4725 if (hflag)
4726 list_commands();
4727 exit(1);
4730 static char **
4731 make_argv(const char *arg0, const char *arg1)
4733 char **argv;
4734 int argc = (arg1 == NULL ? 1 : 2);
4736 argv = calloc(argc, sizeof(char *));
4737 if (argv == NULL)
4738 err(1, "calloc");
4739 argv[0] = strdup(arg0);
4740 if (argv[0] == NULL)
4741 err(1, "strdup");
4742 if (arg1) {
4743 argv[1] = strdup(arg1);
4744 if (argv[1] == NULL)
4745 err(1, "strdup");
4748 return argv;
4751 int
4752 main(int argc, char *argv[])
4754 const struct got_error *error = NULL;
4755 struct tog_cmd *cmd = NULL;
4756 int ch, hflag = 0, Vflag = 0;
4757 char **cmd_argv = NULL;
4759 setlocale(LC_CTYPE, "");
4761 while ((ch = getopt(argc, argv, "hV")) != -1) {
4762 switch (ch) {
4763 case 'h':
4764 hflag = 1;
4765 break;
4766 case 'V':
4767 Vflag = 1;
4768 break;
4769 default:
4770 usage(hflag);
4771 /* NOTREACHED */
4775 argc -= optind;
4776 argv += optind;
4777 optind = 0;
4778 optreset = 1;
4780 if (Vflag) {
4781 got_version_print_str();
4782 return 1;
4785 if (argc == 0) {
4786 if (hflag)
4787 usage(hflag);
4788 /* Build an argument vector which runs a default command. */
4789 cmd = &tog_commands[0];
4790 cmd_argv = make_argv(cmd->name, NULL);
4791 argc = 1;
4792 } else {
4793 int i;
4795 /* Did the user specific a command? */
4796 for (i = 0; i < nitems(tog_commands); i++) {
4797 if (strncmp(tog_commands[i].name, argv[0],
4798 strlen(argv[0])) == 0) {
4799 cmd = &tog_commands[i];
4800 break;
4804 if (cmd == NULL) {
4805 fprintf(stderr, "%s: unknown command '%s'\n",
4806 getprogname(), argv[0]);
4807 list_commands();
4808 return 1;
4812 if (hflag)
4813 cmd->cmd_usage();
4814 else
4815 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4817 endwin();
4818 free(cmd_argv);
4819 if (error && error->code != GOT_ERR_CANCELLED)
4820 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4821 return 0;