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 ncommits++;
1408 entry = TAILQ_NEXT(entry, entry);
1411 entry = first;
1412 *last = first;
1413 ncommits = 0;
1414 while (entry) {
1415 if (ncommits >= limit - 1)
1416 break;
1417 if (ncommits == selected_idx)
1418 wstandout(view->window);
1419 err = draw_commit(view, entry->commit, entry->id, refs,
1420 date_display_cols, author_cols);
1421 if (ncommits == selected_idx)
1422 wstandend(view->window);
1423 if (err)
1424 goto done;
1425 ncommits++;
1426 *last = entry;
1427 entry = TAILQ_NEXT(entry, entry);
1430 view_vborder(view);
1431 done:
1432 free(id_str);
1433 free(refs_str);
1434 free(ncommits_str);
1435 free(header);
1436 return err;
1439 static void
1440 scroll_up(struct tog_view *view,
1441 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1442 struct commit_queue *commits)
1444 struct commit_queue_entry *entry;
1445 int nscrolled = 0;
1447 entry = TAILQ_FIRST(&commits->head);
1448 if (*first_displayed_entry == entry)
1449 return;
1451 entry = *first_displayed_entry;
1452 while (entry && nscrolled < maxscroll) {
1453 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1454 if (entry) {
1455 *first_displayed_entry = entry;
1456 nscrolled++;
1461 static const struct got_error *
1462 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1463 pthread_cond_t *need_commits)
1465 int errcode;
1466 int max_wait = 20;
1468 halfdelay(1); /* fast refresh while loading commits */
1470 while (*commits_needed > 0) {
1471 if (*log_complete)
1472 break;
1474 /* Wake the log thread. */
1475 errcode = pthread_cond_signal(need_commits);
1476 if (errcode)
1477 return got_error_set_errno(errcode,
1478 "pthread_cond_signal");
1479 errcode = pthread_mutex_unlock(&tog_mutex);
1480 if (errcode)
1481 return got_error_set_errno(errcode,
1482 "pthread_mutex_unlock");
1483 pthread_yield();
1484 errcode = pthread_mutex_lock(&tog_mutex);
1485 if (errcode)
1486 return got_error_set_errno(errcode,
1487 "pthread_mutex_lock");
1489 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1491 * Thread is not done yet; lose a key press
1492 * and let the user retry... this way the GUI
1493 * remains interactive while logging deep paths
1494 * with few commits in history.
1496 return NULL;
1500 return NULL;
1503 static const struct got_error *
1504 scroll_down(struct tog_view *view,
1505 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1506 struct commit_queue_entry **last_displayed_entry,
1507 struct commit_queue *commits, int *log_complete, int *commits_needed,
1508 pthread_cond_t *need_commits)
1510 const struct got_error *err = NULL;
1511 struct commit_queue_entry *pentry;
1512 int nscrolled = 0;
1514 if (*last_displayed_entry == NULL)
1515 return NULL;
1517 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1518 if (pentry == NULL && !*log_complete) {
1520 * Ask the log thread for required amount of commits
1521 * plus some amount of pre-fetching.
1523 (*commits_needed) += maxscroll + 20;
1524 err = trigger_log_thread(0, commits_needed, log_complete,
1525 need_commits);
1526 if (err)
1527 return err;
1530 do {
1531 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1532 if (pentry == NULL)
1533 break;
1535 *last_displayed_entry = pentry;
1537 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1538 if (pentry == NULL)
1539 break;
1540 *first_displayed_entry = pentry;
1541 } while (++nscrolled < maxscroll);
1543 return err;
1546 static const struct got_error *
1547 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1548 struct got_commit_object *commit, struct got_object_id *commit_id,
1549 struct tog_view *log_view, struct got_reflist_head *refs,
1550 struct got_repository *repo)
1552 const struct got_error *err;
1553 struct got_object_qid *parent_id;
1554 struct tog_view *diff_view;
1556 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1557 if (diff_view == NULL)
1558 return got_error_from_errno("view_open");
1560 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1561 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1562 commit_id, log_view, refs, repo);
1563 if (err == NULL)
1564 *new_view = diff_view;
1565 return err;
1568 static const struct got_error *
1569 tree_view_visit_subtree(struct got_tree_object *subtree,
1570 struct tog_tree_view_state *s)
1572 struct tog_parent_tree *parent;
1574 parent = calloc(1, sizeof(*parent));
1575 if (parent == NULL)
1576 return got_error_from_errno("calloc");
1578 parent->tree = s->tree;
1579 parent->first_displayed_entry = s->first_displayed_entry;
1580 parent->selected_entry = s->selected_entry;
1581 parent->selected = s->selected;
1582 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1583 s->tree = subtree;
1584 s->entries = got_object_tree_get_entries(s->tree);
1585 s->selected = 0;
1586 s->first_displayed_entry = NULL;
1587 return NULL;
1591 static const struct got_error *
1592 browse_commit_tree(struct tog_view **new_view, int begin_x,
1593 struct commit_queue_entry *entry, const char *path,
1594 struct got_reflist_head *refs, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_tree_object *tree;
1598 struct got_tree_entry *te;
1599 struct tog_tree_view_state *s;
1600 struct tog_view *tree_view;
1601 char *slash, *subpath = NULL;
1602 const char *p;
1604 err = got_object_open_as_tree(&tree, repo,
1605 got_object_commit_get_tree_id(entry->commit));
1606 if (err)
1607 return err;
1609 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1610 if (tree_view == NULL)
1611 return got_error_from_errno("view_open");
1613 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1614 if (err) {
1615 got_object_tree_close(tree);
1616 return err;
1618 s = &tree_view->state.tree;
1620 *new_view = tree_view;
1622 /* Walk the path and open corresponding tree objects. */
1623 p = path;
1624 while (p[0] == '/')
1625 p++;
1626 while (*p) {
1627 struct got_object_id *tree_id;
1629 /* Ensure the correct subtree entry is selected. */
1630 slash = strchr(p, '/');
1631 if (slash == NULL)
1632 slash = strchr(p, '\0');
1633 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1634 if (strncmp(p, te->name, slash - p) == 0) {
1635 s->selected_entry = te;
1636 break;
1638 s->selected++;
1640 if (s->selected_entry == NULL) {
1641 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1642 break;
1644 if (s->tree != s->root)
1645 s->selected++; /* skip '..' */
1647 if (!S_ISDIR(s->selected_entry->mode)) {
1648 /* Jump to this file's entry. */
1649 s->first_displayed_entry = s->selected_entry;
1650 s->selected = 0;
1651 break;
1654 slash = strchr(p, '/');
1655 if (slash)
1656 subpath = strndup(path, slash - path);
1657 else
1658 subpath = strdup(path);
1659 if (subpath == NULL) {
1660 err = got_error_from_errno("strdup");
1661 break;
1664 err = got_object_id_by_path(&tree_id, repo, entry->id,
1665 subpath);
1666 if (err)
1667 break;
1669 err = got_object_open_as_tree(&tree, repo, tree_id);
1670 free(tree_id);
1671 if (err)
1672 break;
1674 err = tree_view_visit_subtree(tree, s);
1675 if (err) {
1676 got_object_tree_close(tree);
1677 break;
1679 if (slash == NULL)
1680 break;
1681 free(subpath);
1682 subpath = NULL;
1683 p = slash;
1686 free(subpath);
1687 return err;
1690 static void *
1691 log_thread(void *arg)
1693 const struct got_error *err = NULL;
1694 int errcode = 0;
1695 struct tog_log_thread_args *a = arg;
1696 int done = 0;
1698 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1699 NULL, NULL);
1700 if (err)
1701 return (void *)err;
1703 while (!done && !err && !tog_sigpipe_received) {
1704 err = queue_commits(a->graph, a->commits, 1, a->repo,
1705 a->in_repo_path, a->searching, a->search_next_done,
1706 a->regex);
1707 if (err) {
1708 if (err->code != GOT_ERR_ITER_COMPLETED)
1709 return (void *)err;
1710 err = NULL;
1711 done = 1;
1712 } else if (a->commits_needed > 0)
1713 a->commits_needed--;
1715 errcode = pthread_mutex_lock(&tog_mutex);
1716 if (errcode) {
1717 err = got_error_set_errno(errcode,
1718 "pthread_mutex_lock");
1719 break;
1720 } else if (*a->quit)
1721 done = 1;
1722 else if (*a->first_displayed_entry == NULL) {
1723 *a->first_displayed_entry =
1724 TAILQ_FIRST(&a->commits->head);
1725 *a->selected_entry = *a->first_displayed_entry;
1728 if (done)
1729 a->commits_needed = 0;
1730 else if (a->commits_needed == 0) {
1731 errcode = pthread_cond_wait(&a->need_commits,
1732 &tog_mutex);
1733 if (errcode)
1734 err = got_error_set_errno(errcode,
1735 "pthread_cond_wait");
1738 errcode = pthread_mutex_unlock(&tog_mutex);
1739 if (errcode && err == NULL)
1740 err = got_error_set_errno(errcode,
1741 "pthread_mutex_unlock");
1743 a->log_complete = 1;
1744 return (void *)err;
1747 static const struct got_error *
1748 stop_log_thread(struct tog_log_view_state *s)
1750 const struct got_error *err = NULL;
1751 int errcode;
1753 if (s->thread) {
1754 s->quit = 1;
1755 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1756 if (errcode)
1757 return got_error_set_errno(errcode,
1758 "pthread_cond_signal");
1759 errcode = pthread_mutex_unlock(&tog_mutex);
1760 if (errcode)
1761 return got_error_set_errno(errcode,
1762 "pthread_mutex_unlock");
1763 errcode = pthread_join(s->thread, (void **)&err);
1764 if (errcode)
1765 return got_error_set_errno(errcode, "pthread_join");
1766 errcode = pthread_mutex_lock(&tog_mutex);
1767 if (errcode)
1768 return got_error_set_errno(errcode,
1769 "pthread_mutex_lock");
1770 s->thread = NULL;
1773 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1774 if (errcode && err == NULL)
1775 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1777 if (s->thread_args.repo) {
1778 got_repo_close(s->thread_args.repo);
1779 s->thread_args.repo = NULL;
1782 if (s->thread_args.graph) {
1783 got_commit_graph_close(s->thread_args.graph);
1784 s->thread_args.graph = NULL;
1787 return err;
1790 static const struct got_error *
1791 close_log_view(struct tog_view *view)
1793 const struct got_error *err = NULL;
1794 struct tog_log_view_state *s = &view->state.log;
1796 err = stop_log_thread(s);
1797 free_commits(&s->commits);
1798 free(s->in_repo_path);
1799 s->in_repo_path = NULL;
1800 free(s->start_id);
1801 s->start_id = NULL;
1802 return err;
1805 static const struct got_error *
1806 search_start_log_view(struct tog_view *view)
1808 struct tog_log_view_state *s = &view->state.log;
1810 s->matched_entry = NULL;
1811 s->search_entry = NULL;
1812 return NULL;
1815 static const struct got_error *
1816 search_next_log_view(struct tog_view *view)
1818 const struct got_error *err = NULL;
1819 struct tog_log_view_state *s = &view->state.log;
1820 struct commit_queue_entry *entry;
1822 if (!view->searching) {
1823 view->search_next_done = 1;
1824 return NULL;
1827 if (s->search_entry) {
1828 int errcode, ch;
1829 errcode = pthread_mutex_unlock(&tog_mutex);
1830 if (errcode)
1831 return got_error_set_errno(errcode,
1832 "pthread_mutex_unlock");
1833 ch = wgetch(view->window);
1834 errcode = pthread_mutex_lock(&tog_mutex);
1835 if (errcode)
1836 return got_error_set_errno(errcode,
1837 "pthread_mutex_lock");
1838 if (ch == KEY_BACKSPACE) {
1839 view->search_next_done = 1;
1840 return NULL;
1842 if (view->searching == TOG_SEARCH_FORWARD)
1843 entry = TAILQ_NEXT(s->search_entry, entry);
1844 else
1845 entry = TAILQ_PREV(s->search_entry,
1846 commit_queue_head, entry);
1847 } else if (s->matched_entry) {
1848 if (view->searching == TOG_SEARCH_FORWARD)
1849 entry = TAILQ_NEXT(s->selected_entry, entry);
1850 else
1851 entry = TAILQ_PREV(s->selected_entry,
1852 commit_queue_head, entry);
1853 } else {
1854 if (view->searching == TOG_SEARCH_FORWARD)
1855 entry = TAILQ_FIRST(&s->commits.head);
1856 else
1857 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1860 while (1) {
1861 int have_match = 0;
1863 if (entry == NULL) {
1864 if (s->thread_args.log_complete ||
1865 view->searching == TOG_SEARCH_BACKWARD) {
1866 view->search_next_done = 1;
1867 return NULL;
1870 * Poke the log thread for more commits and return,
1871 * allowing the main loop to make progress. Search
1872 * will resume at s->search_entry once we come back.
1874 s->thread_args.commits_needed++;
1875 return trigger_log_thread(1,
1876 &s->thread_args.commits_needed,
1877 &s->thread_args.log_complete,
1878 &s->thread_args.need_commits);
1881 err = match_commit(&have_match, entry->id, entry->commit,
1882 &view->regex);
1883 if (err)
1884 break;
1885 if (have_match) {
1886 view->search_next_done = 1;
1887 s->matched_entry = entry;
1888 break;
1891 s->search_entry = entry;
1892 if (view->searching == TOG_SEARCH_FORWARD)
1893 entry = TAILQ_NEXT(entry, entry);
1894 else
1895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1898 if (s->matched_entry) {
1899 int cur = s->selected_entry->idx;
1900 while (cur < s->matched_entry->idx) {
1901 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1902 if (err)
1903 return err;
1904 cur++;
1906 while (cur > s->matched_entry->idx) {
1907 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1908 if (err)
1909 return err;
1910 cur--;
1914 s->search_entry = NULL;
1916 return NULL;
1919 static const struct got_error *
1920 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1921 struct got_reflist_head *refs, struct got_repository *repo,
1922 const char *head_ref_name, const char *path, int check_disk)
1924 const struct got_error *err = NULL;
1925 struct tog_log_view_state *s = &view->state.log;
1926 struct got_repository *thread_repo = NULL;
1927 struct got_commit_graph *thread_graph = NULL;
1928 int errcode;
1930 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1931 if (err != NULL)
1932 goto done;
1934 /* The commit queue only contains commits being displayed. */
1935 TAILQ_INIT(&s->commits.head);
1936 s->commits.ncommits = 0;
1938 s->refs = refs;
1939 s->repo = repo;
1940 s->head_ref_name = head_ref_name;
1941 s->start_id = got_object_id_dup(start_id);
1942 if (s->start_id == NULL) {
1943 err = got_error_from_errno("got_object_id_dup");
1944 goto done;
1947 view->show = show_log_view;
1948 view->input = input_log_view;
1949 view->close = close_log_view;
1950 view->search_start = search_start_log_view;
1951 view->search_next = search_next_log_view;
1953 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1954 if (err)
1955 goto done;
1956 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1957 0, thread_repo);
1958 if (err)
1959 goto done;
1961 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1962 if (errcode) {
1963 err = got_error_set_errno(errcode, "pthread_cond_init");
1964 goto done;
1967 s->thread_args.commits_needed = view->nlines;
1968 s->thread_args.graph = thread_graph;
1969 s->thread_args.commits = &s->commits;
1970 s->thread_args.in_repo_path = s->in_repo_path;
1971 s->thread_args.start_id = s->start_id;
1972 s->thread_args.repo = thread_repo;
1973 s->thread_args.log_complete = 0;
1974 s->thread_args.quit = &s->quit;
1975 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1976 s->thread_args.selected_entry = &s->selected_entry;
1977 s->thread_args.searching = &view->searching;
1978 s->thread_args.search_next_done = &view->search_next_done;
1979 s->thread_args.regex = &view->regex;
1980 done:
1981 if (err)
1982 close_log_view(view);
1983 return err;
1986 static const struct got_error *
1987 show_log_view(struct tog_view *view)
1989 struct tog_log_view_state *s = &view->state.log;
1991 if (s->thread == NULL) {
1992 int errcode = pthread_create(&s->thread, NULL, log_thread,
1993 &s->thread_args);
1994 if (errcode)
1995 return got_error_set_errno(errcode, "pthread_create");
1998 return draw_commits(view, &s->last_displayed_entry,
1999 &s->selected_entry, s->first_displayed_entry,
2000 &s->commits, s->selected, view->nlines, s->refs,
2001 s->in_repo_path, s->thread_args.commits_needed);
2004 static const struct got_error *
2005 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2006 struct tog_view **focus_view, struct tog_view *view, int ch)
2008 const struct got_error *err = NULL;
2009 struct tog_log_view_state *s = &view->state.log;
2010 char *parent_path, *in_repo_path = NULL;
2011 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2012 int begin_x = 0;
2013 struct got_object_id *start_id;
2015 switch (ch) {
2016 case 'q':
2017 s->quit = 1;
2018 break;
2019 case 'k':
2020 case KEY_UP:
2021 case '<':
2022 case ',':
2023 if (s->first_displayed_entry == NULL)
2024 break;
2025 if (s->selected > 0)
2026 s->selected--;
2027 else
2028 scroll_up(view, &s->first_displayed_entry, 1,
2029 &s->commits);
2030 break;
2031 case KEY_PPAGE:
2032 case CTRL('b'):
2033 if (s->first_displayed_entry == NULL)
2034 break;
2035 if (TAILQ_FIRST(&s->commits.head) ==
2036 s->first_displayed_entry) {
2037 s->selected = 0;
2038 break;
2040 scroll_up(view, &s->first_displayed_entry,
2041 view->nlines, &s->commits);
2042 break;
2043 case 'j':
2044 case KEY_DOWN:
2045 case '>':
2046 case '.':
2047 if (s->first_displayed_entry == NULL)
2048 break;
2049 if (s->selected < MIN(view->nlines - 2,
2050 s->commits.ncommits - 1)) {
2051 s->selected++;
2052 break;
2054 err = scroll_down(view, &s->first_displayed_entry, 1,
2055 &s->last_displayed_entry, &s->commits,
2056 &s->thread_args.log_complete,
2057 &s->thread_args.commits_needed,
2058 &s->thread_args.need_commits);
2059 break;
2060 case KEY_NPAGE:
2061 case CTRL('f'): {
2062 struct commit_queue_entry *first;
2063 first = s->first_displayed_entry;
2064 if (first == NULL)
2065 break;
2066 err = scroll_down(view, &s->first_displayed_entry,
2067 view->nlines, &s->last_displayed_entry,
2068 &s->commits, &s->thread_args.log_complete,
2069 &s->thread_args.commits_needed,
2070 &s->thread_args.need_commits);
2071 if (err)
2072 break;
2073 if (first == s->first_displayed_entry &&
2074 s->selected < MIN(view->nlines - 2,
2075 s->commits.ncommits - 1)) {
2076 /* can't scroll further down */
2077 s->selected = MIN(view->nlines - 2,
2078 s->commits.ncommits - 1);
2080 err = NULL;
2081 break;
2083 case KEY_RESIZE:
2084 if (s->selected > view->nlines - 2)
2085 s->selected = view->nlines - 2;
2086 if (s->selected > s->commits.ncommits - 1)
2087 s->selected = s->commits.ncommits - 1;
2088 break;
2089 case KEY_ENTER:
2090 case ' ':
2091 case '\r':
2092 if (s->selected_entry == NULL)
2093 break;
2094 if (view_is_parent_view(view))
2095 begin_x = view_split_begin_x(view->begin_x);
2096 err = open_diff_view_for_commit(&diff_view, begin_x,
2097 s->selected_entry->commit, s->selected_entry->id,
2098 view, s->refs, s->repo);
2099 if (err)
2100 break;
2101 if (view_is_parent_view(view)) {
2102 err = view_close_child(view);
2103 if (err)
2104 return err;
2105 err = view_set_child(view, diff_view);
2106 if (err) {
2107 view_close(diff_view);
2108 break;
2110 *focus_view = diff_view;
2111 view->child_focussed = 1;
2112 } else
2113 *new_view = diff_view;
2114 break;
2115 case 't':
2116 if (s->selected_entry == NULL)
2117 break;
2118 if (view_is_parent_view(view))
2119 begin_x = view_split_begin_x(view->begin_x);
2120 err = browse_commit_tree(&tree_view, begin_x,
2121 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2122 if (err)
2123 break;
2124 if (view_is_parent_view(view)) {
2125 err = view_close_child(view);
2126 if (err)
2127 return err;
2128 err = view_set_child(view, tree_view);
2129 if (err) {
2130 view_close(tree_view);
2131 break;
2133 *focus_view = tree_view;
2134 view->child_focussed = 1;
2135 } else
2136 *new_view = tree_view;
2137 break;
2138 case KEY_BACKSPACE:
2139 if (strcmp(s->in_repo_path, "/") == 0)
2140 break;
2141 parent_path = dirname(s->in_repo_path);
2142 if (parent_path && strcmp(parent_path, ".") != 0) {
2143 err = stop_log_thread(s);
2144 if (err)
2145 return err;
2146 lv = view_open(view->nlines, view->ncols,
2147 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2148 if (lv == NULL)
2149 return got_error_from_errno(
2150 "view_open");
2151 err = open_log_view(lv, s->start_id, s->refs,
2152 s->repo, s->head_ref_name, parent_path, 0);
2153 if (err)
2154 return err;;
2155 if (view_is_parent_view(view))
2156 *new_view = lv;
2157 else {
2158 view_set_child(view->parent, lv);
2159 *focus_view = lv;
2161 return NULL;
2163 break;
2164 case CTRL('l'):
2165 err = stop_log_thread(s);
2166 if (err)
2167 return err;
2168 lv = view_open(view->nlines, view->ncols,
2169 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2170 if (lv == NULL)
2171 return got_error_from_errno("view_open");
2172 err = get_head_commit_id(&start_id, s->head_ref_name ?
2173 s->head_ref_name : GOT_REF_HEAD, s->repo);
2174 if (err) {
2175 view_close(lv);
2176 return err;
2178 in_repo_path = strdup(s->in_repo_path);
2179 if (in_repo_path == NULL) {
2180 free(start_id);
2181 view_close(lv);
2182 return got_error_from_errno("strdup");
2184 got_ref_list_free(s->refs);
2185 err = got_ref_list(s->refs, s->repo, NULL,
2186 got_ref_cmp_by_name, NULL);
2187 if (err) {
2188 free(start_id);
2189 view_close(lv);
2190 return err;
2192 err = open_log_view(lv, start_id, s->refs, s->repo,
2193 s->head_ref_name, in_repo_path, 0);
2194 if (err) {
2195 free(start_id);
2196 view_close(lv);
2197 return err;;
2199 *dead_view = view;
2200 *new_view = lv;
2201 break;
2202 default:
2203 break;
2206 return err;
2209 static const struct got_error *
2210 apply_unveil(const char *repo_path, const char *worktree_path)
2212 const struct got_error *error;
2214 #ifdef PROFILE
2215 if (unveil("gmon.out", "rwc") != 0)
2216 return got_error_from_errno2("unveil", "gmon.out");
2217 #endif
2218 if (repo_path && unveil(repo_path, "r") != 0)
2219 return got_error_from_errno2("unveil", repo_path);
2221 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2222 return got_error_from_errno2("unveil", worktree_path);
2224 if (unveil("/tmp", "rwc") != 0)
2225 return got_error_from_errno2("unveil", "/tmp");
2227 error = got_privsep_unveil_exec_helpers();
2228 if (error != NULL)
2229 return error;
2231 if (unveil(NULL, NULL) != 0)
2232 return got_error_from_errno("unveil");
2234 return NULL;
2237 static void
2238 init_curses(void)
2240 initscr();
2241 cbreak();
2242 halfdelay(1); /* Do fast refresh while initial view is loading. */
2243 noecho();
2244 nonl();
2245 intrflush(stdscr, FALSE);
2246 keypad(stdscr, TRUE);
2247 curs_set(0);
2248 signal(SIGWINCH, tog_sigwinch);
2249 signal(SIGPIPE, tog_sigpipe);
2252 static const struct got_error *
2253 cmd_log(int argc, char *argv[])
2255 const struct got_error *error;
2256 struct got_repository *repo = NULL;
2257 struct got_worktree *worktree = NULL;
2258 struct got_reflist_head refs;
2259 struct got_object_id *start_id = NULL;
2260 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2261 char *start_commit = NULL, *head_ref_name = NULL;
2262 int ch;
2263 struct tog_view *view;
2265 SIMPLEQ_INIT(&refs);
2267 #ifndef PROFILE
2268 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2269 NULL) == -1)
2270 err(1, "pledge");
2271 #endif
2273 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2274 switch (ch) {
2275 case 'c':
2276 start_commit = optarg;
2277 break;
2278 case 'r':
2279 repo_path = realpath(optarg, NULL);
2280 if (repo_path == NULL)
2281 err(1, "-r option");
2282 break;
2283 default:
2284 usage_log();
2285 /* NOTREACHED */
2289 argc -= optind;
2290 argv += optind;
2292 cwd = getcwd(NULL, 0);
2293 if (cwd == NULL) {
2294 error = got_error_from_errno("getcwd");
2295 goto done;
2297 error = got_worktree_open(&worktree, cwd);
2298 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2299 goto done;
2300 error = NULL;
2302 if (argc == 0) {
2303 path = strdup("");
2304 if (path == NULL) {
2305 error = got_error_from_errno("strdup");
2306 goto done;
2308 } else if (argc == 1) {
2309 if (worktree) {
2310 error = got_worktree_resolve_path(&path, worktree,
2311 argv[0]);
2312 if (error)
2313 goto done;
2314 } else {
2315 path = strdup(argv[0]);
2316 if (path == NULL) {
2317 error = got_error_from_errno("strdup");
2318 goto done;
2321 } else
2322 usage_log();
2324 if (repo_path == NULL) {
2325 if (worktree)
2326 repo_path = strdup(
2327 got_worktree_get_repo_path(worktree));
2328 else
2329 repo_path = strdup(cwd);
2331 if (repo_path == NULL) {
2332 error = got_error_from_errno("strdup");
2333 goto done;
2336 init_curses();
2338 error = got_repo_open(&repo, repo_path, NULL);
2339 if (error != NULL)
2340 goto done;
2342 error = apply_unveil(got_repo_get_path(repo),
2343 worktree ? got_worktree_get_root_path(worktree) : NULL);
2344 if (error)
2345 goto done;
2347 if (start_commit == NULL)
2348 error = get_head_commit_id(&start_id, worktree ?
2349 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2350 repo);
2351 else {
2352 error = get_head_commit_id(&start_id, start_commit, repo);
2353 if (error) {
2354 if (error->code != GOT_ERR_NOT_REF)
2355 goto done;
2356 error = got_repo_match_object_id_prefix(&start_id,
2357 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2360 if (error != NULL)
2361 goto done;
2363 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2364 if (error)
2365 goto done;
2367 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2368 if (view == NULL) {
2369 error = got_error_from_errno("view_open");
2370 goto done;
2372 if (worktree) {
2373 head_ref_name = strdup(
2374 got_worktree_get_head_ref_name(worktree));
2375 if (head_ref_name == NULL) {
2376 error = got_error_from_errno("strdup");
2377 goto done;
2380 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2381 path, 1);
2382 if (error)
2383 goto done;
2384 if (worktree) {
2385 /* Release work tree lock. */
2386 got_worktree_close(worktree);
2387 worktree = NULL;
2389 error = view_loop(view);
2390 done:
2391 free(repo_path);
2392 free(cwd);
2393 free(path);
2394 free(start_id);
2395 free(head_ref_name);
2396 if (repo)
2397 got_repo_close(repo);
2398 if (worktree)
2399 got_worktree_close(worktree);
2400 got_ref_list_free(&refs);
2401 return error;
2404 __dead static void
2405 usage_diff(void)
2407 endwin();
2408 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2409 getprogname());
2410 exit(1);
2413 static char *
2414 parse_next_line(FILE *f, size_t *len)
2416 char *line;
2417 size_t linelen;
2418 size_t lineno;
2419 const char delim[3] = { '\0', '\0', '\0'};
2421 line = fparseln(f, &linelen, &lineno, delim, 0);
2422 if (len)
2423 *len = linelen;
2424 return line;
2427 static const struct got_error *
2428 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2429 int *last_displayed_line, int *eof, int max_lines,
2430 char *header)
2432 const struct got_error *err;
2433 int nlines = 0, nprinted = 0;
2434 char *line;
2435 size_t len;
2436 wchar_t *wline;
2437 int width;
2439 rewind(f);
2440 werase(view->window);
2442 if (header) {
2443 err = format_line(&wline, &width, header, view->ncols, 0);
2444 if (err) {
2445 return err;
2448 if (view_needs_focus_indication(view))
2449 wstandout(view->window);
2450 waddwstr(view->window, wline);
2451 if (view_needs_focus_indication(view))
2452 wstandend(view->window);
2453 if (width <= view->ncols - 1)
2454 waddch(view->window, '\n');
2456 if (max_lines <= 1)
2457 return NULL;
2458 max_lines--;
2461 *eof = 0;
2462 while (nprinted < max_lines) {
2463 line = parse_next_line(f, &len);
2464 if (line == NULL) {
2465 *eof = 1;
2466 break;
2468 if (++nlines < *first_displayed_line) {
2469 free(line);
2470 continue;
2473 err = format_line(&wline, &width, line, view->ncols, 0);
2474 if (err) {
2475 free(line);
2476 return err;
2478 waddwstr(view->window, wline);
2479 if (width <= view->ncols - 1)
2480 waddch(view->window, '\n');
2481 if (++nprinted == 1)
2482 *first_displayed_line = nlines;
2483 free(line);
2484 free(wline);
2485 wline = NULL;
2487 *last_displayed_line = nlines;
2489 view_vborder(view);
2491 if (*eof) {
2492 while (nprinted < view->nlines) {
2493 waddch(view->window, '\n');
2494 nprinted++;
2497 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2498 if (err) {
2499 return err;
2502 wstandout(view->window);
2503 waddwstr(view->window, wline);
2504 wstandend(view->window);
2507 return NULL;
2510 static char *
2511 get_datestr(time_t *time, char *datebuf)
2513 struct tm mytm, *tm;
2514 char *p, *s;
2516 tm = gmtime_r(time, &mytm);
2517 if (tm == NULL)
2518 return NULL;
2519 s = asctime_r(tm, datebuf);
2520 if (s == NULL)
2521 return NULL;
2522 p = strchr(s, '\n');
2523 if (p)
2524 *p = '\0';
2525 return s;
2528 static const struct got_error *
2529 write_commit_info(struct got_object_id *commit_id,
2530 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2532 const struct got_error *err = NULL;
2533 char datebuf[26], *datestr;
2534 struct got_commit_object *commit;
2535 char *id_str = NULL, *logmsg = NULL;
2536 time_t committer_time;
2537 const char *author, *committer;
2538 char *refs_str = NULL;
2540 if (refs) {
2541 err = build_refs_str(&refs_str, refs, commit_id, repo);
2542 if (err)
2543 return err;
2546 err = got_object_open_as_commit(&commit, repo, commit_id);
2547 if (err)
2548 return err;
2550 err = got_object_id_str(&id_str, commit_id);
2551 if (err) {
2552 err = got_error_from_errno("got_object_id_str");
2553 goto done;
2556 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2557 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2558 err = got_error_from_errno("fprintf");
2559 goto done;
2561 if (fprintf(outfile, "from: %s\n",
2562 got_object_commit_get_author(commit)) < 0) {
2563 err = got_error_from_errno("fprintf");
2564 goto done;
2566 committer_time = got_object_commit_get_committer_time(commit);
2567 datestr = get_datestr(&committer_time, datebuf);
2568 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2569 err = got_error_from_errno("fprintf");
2570 goto done;
2572 author = got_object_commit_get_author(commit);
2573 committer = got_object_commit_get_committer(commit);
2574 if (strcmp(author, committer) != 0 &&
2575 fprintf(outfile, "via: %s\n", committer) < 0) {
2576 err = got_error_from_errno("fprintf");
2577 goto done;
2579 err = got_object_commit_get_logmsg(&logmsg, commit);
2580 if (err)
2581 goto done;
2582 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2583 err = got_error_from_errno("fprintf");
2584 goto done;
2586 done:
2587 free(id_str);
2588 free(logmsg);
2589 free(refs_str);
2590 got_object_commit_close(commit);
2591 return err;
2594 static const struct got_error *
2595 create_diff(struct tog_diff_view_state *s)
2597 const struct got_error *err = NULL;
2598 FILE *f = NULL;
2599 int obj_type;
2601 f = got_opentemp();
2602 if (f == NULL) {
2603 err = got_error_from_errno("got_opentemp");
2604 goto done;
2606 if (s->f && fclose(s->f) != 0) {
2607 err = got_error_from_errno("fclose");
2608 goto done;
2610 s->f = f;
2612 if (s->id1)
2613 err = got_object_get_type(&obj_type, s->repo, s->id1);
2614 else
2615 err = got_object_get_type(&obj_type, s->repo, s->id2);
2616 if (err)
2617 goto done;
2619 switch (obj_type) {
2620 case GOT_OBJ_TYPE_BLOB:
2621 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2622 s->diff_context, 0, s->repo, f);
2623 break;
2624 case GOT_OBJ_TYPE_TREE:
2625 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2626 s->diff_context, 0, s->repo, f);
2627 break;
2628 case GOT_OBJ_TYPE_COMMIT: {
2629 const struct got_object_id_queue *parent_ids;
2630 struct got_object_qid *pid;
2631 struct got_commit_object *commit2;
2633 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2634 if (err)
2635 break;
2636 /* Show commit info if we're diffing to a parent/root commit. */
2637 if (s->id1 == NULL)
2638 write_commit_info(s->id2, s->refs, s->repo, f);
2639 else {
2640 parent_ids = got_object_commit_get_parent_ids(commit2);
2641 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2642 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2643 write_commit_info(s->id2, s->refs,
2644 s->repo, f);
2645 break;
2649 got_object_commit_close(commit2);
2651 err = got_diff_objects_as_commits(s->id1, s->id2,
2652 s->diff_context, 0, s->repo, f);
2653 break;
2655 default:
2656 err = got_error(GOT_ERR_OBJ_TYPE);
2657 break;
2659 done:
2660 if (f && fflush(f) != 0 && err == NULL)
2661 err = got_error_from_errno("fflush");
2662 return err;
2665 static void
2666 diff_view_indicate_progress(struct tog_view *view)
2668 mvwaddstr(view->window, 0, 0, "diffing...");
2669 update_panels();
2670 doupdate();
2673 static const struct got_error *
2674 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2675 struct got_object_id *id2, struct tog_view *log_view,
2676 struct got_reflist_head *refs, struct got_repository *repo)
2678 const struct got_error *err;
2680 if (id1 != NULL && id2 != NULL) {
2681 int type1, type2;
2682 err = got_object_get_type(&type1, repo, id1);
2683 if (err)
2684 return err;
2685 err = got_object_get_type(&type2, repo, id2);
2686 if (err)
2687 return err;
2689 if (type1 != type2)
2690 return got_error(GOT_ERR_OBJ_TYPE);
2693 if (id1) {
2694 view->state.diff.id1 = got_object_id_dup(id1);
2695 if (view->state.diff.id1 == NULL)
2696 return got_error_from_errno("got_object_id_dup");
2697 } else
2698 view->state.diff.id1 = NULL;
2700 view->state.diff.id2 = got_object_id_dup(id2);
2701 if (view->state.diff.id2 == NULL) {
2702 free(view->state.diff.id1);
2703 view->state.diff.id1 = NULL;
2704 return got_error_from_errno("got_object_id_dup");
2706 view->state.diff.f = NULL;
2707 view->state.diff.first_displayed_line = 1;
2708 view->state.diff.last_displayed_line = view->nlines;
2709 view->state.diff.diff_context = 3;
2710 view->state.diff.log_view = log_view;
2711 view->state.diff.repo = repo;
2712 view->state.diff.refs = refs;
2714 if (log_view && view_is_splitscreen(view))
2715 show_log_view(log_view); /* draw vborder */
2716 diff_view_indicate_progress(view);
2718 err = create_diff(&view->state.diff);
2719 if (err) {
2720 free(view->state.diff.id1);
2721 view->state.diff.id1 = NULL;
2722 free(view->state.diff.id2);
2723 view->state.diff.id2 = NULL;
2724 return err;
2727 view->show = show_diff_view;
2728 view->input = input_diff_view;
2729 view->close = close_diff_view;
2731 return NULL;
2734 static const struct got_error *
2735 close_diff_view(struct tog_view *view)
2737 const struct got_error *err = NULL;
2739 free(view->state.diff.id1);
2740 view->state.diff.id1 = NULL;
2741 free(view->state.diff.id2);
2742 view->state.diff.id2 = NULL;
2743 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2744 err = got_error_from_errno("fclose");
2745 return err;
2748 static const struct got_error *
2749 show_diff_view(struct tog_view *view)
2751 const struct got_error *err;
2752 struct tog_diff_view_state *s = &view->state.diff;
2753 char *id_str1 = NULL, *id_str2, *header;
2755 if (s->id1) {
2756 err = got_object_id_str(&id_str1, s->id1);
2757 if (err)
2758 return err;
2760 err = got_object_id_str(&id_str2, s->id2);
2761 if (err)
2762 return err;
2764 if (asprintf(&header, "diff %s %s",
2765 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2766 err = got_error_from_errno("asprintf");
2767 free(id_str1);
2768 free(id_str2);
2769 return err;
2771 free(id_str1);
2772 free(id_str2);
2774 return draw_file(view, s->f, &s->first_displayed_line,
2775 &s->last_displayed_line, &s->eof, view->nlines,
2776 header);
2779 static const struct got_error *
2780 set_selected_commit(struct tog_diff_view_state *s,
2781 struct commit_queue_entry *entry)
2783 const struct got_error *err;
2784 const struct got_object_id_queue *parent_ids;
2785 struct got_commit_object *selected_commit;
2786 struct got_object_qid *pid;
2788 free(s->id2);
2789 s->id2 = got_object_id_dup(entry->id);
2790 if (s->id2 == NULL)
2791 return got_error_from_errno("got_object_id_dup");
2793 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2794 if (err)
2795 return err;
2796 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2797 free(s->id1);
2798 pid = SIMPLEQ_FIRST(parent_ids);
2799 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2800 got_object_commit_close(selected_commit);
2801 return NULL;
2804 static const struct got_error *
2805 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2806 struct tog_view **focus_view, struct tog_view *view, int ch)
2808 const struct got_error *err = NULL;
2809 struct tog_diff_view_state *s = &view->state.diff;
2810 struct tog_log_view_state *ls;
2811 struct commit_queue_entry *entry;
2812 int i;
2814 switch (ch) {
2815 case 'k':
2816 case KEY_UP:
2817 if (s->first_displayed_line > 1)
2818 s->first_displayed_line--;
2819 break;
2820 case KEY_PPAGE:
2821 case CTRL('b'):
2822 if (s->first_displayed_line == 1)
2823 break;
2824 i = 0;
2825 while (i++ < view->nlines - 1 &&
2826 s->first_displayed_line > 1)
2827 s->first_displayed_line--;
2828 break;
2829 case 'j':
2830 case KEY_DOWN:
2831 if (!s->eof)
2832 s->first_displayed_line++;
2833 break;
2834 case KEY_NPAGE:
2835 case CTRL('f'):
2836 case ' ':
2837 if (s->eof)
2838 break;
2839 i = 0;
2840 while (!s->eof && i++ < view->nlines - 1) {
2841 char *line;
2842 line = parse_next_line(s->f, NULL);
2843 s->first_displayed_line++;
2844 if (line == NULL)
2845 break;
2847 break;
2848 case '[':
2849 if (s->diff_context > 0) {
2850 s->diff_context--;
2851 diff_view_indicate_progress(view);
2852 err = create_diff(s);
2854 break;
2855 case ']':
2856 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2857 s->diff_context++;
2858 diff_view_indicate_progress(view);
2859 err = create_diff(s);
2861 break;
2862 case '<':
2863 case ',':
2864 if (s->log_view == NULL)
2865 break;
2866 ls = &s->log_view->state.log;
2867 entry = TAILQ_PREV(ls->selected_entry,
2868 commit_queue_head, entry);
2869 if (entry == NULL)
2870 break;
2872 err = input_log_view(NULL, NULL, NULL, s->log_view,
2873 KEY_UP);
2874 if (err)
2875 break;
2877 err = set_selected_commit(s, entry);
2878 if (err)
2879 break;
2881 s->first_displayed_line = 1;
2882 s->last_displayed_line = view->nlines;
2884 diff_view_indicate_progress(view);
2885 err = create_diff(s);
2886 break;
2887 case '>':
2888 case '.':
2889 if (s->log_view == NULL)
2890 break;
2891 ls = &s->log_view->state.log;
2893 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2894 ls->thread_args.commits_needed++;
2896 /* Display "loading..." in log view. */
2897 show_log_view(s->log_view);
2898 update_panels();
2899 doupdate();
2901 err = trigger_log_thread(1 /* load_all */,
2902 &ls->thread_args.commits_needed,
2903 &ls->thread_args.log_complete,
2904 &ls->thread_args.need_commits);
2905 if (err)
2906 break;
2908 err = input_log_view(NULL, NULL, NULL, s->log_view,
2909 KEY_DOWN);
2910 if (err)
2911 break;
2913 entry = TAILQ_NEXT(ls->selected_entry, entry);
2914 if (entry == NULL)
2915 break;
2917 err = set_selected_commit(s, entry);
2918 if (err)
2919 break;
2921 s->first_displayed_line = 1;
2922 s->last_displayed_line = view->nlines;
2924 diff_view_indicate_progress(view);
2925 err = create_diff(s);
2926 break;
2927 default:
2928 break;
2931 return err;
2934 static const struct got_error *
2935 cmd_diff(int argc, char *argv[])
2937 const struct got_error *error = NULL;
2938 struct got_repository *repo = NULL;
2939 struct got_reflist_head refs;
2940 struct got_object_id *id1 = NULL, *id2 = NULL;
2941 char *repo_path = NULL;
2942 char *id_str1 = NULL, *id_str2 = NULL;
2943 int ch;
2944 struct tog_view *view;
2946 SIMPLEQ_INIT(&refs);
2948 #ifndef PROFILE
2949 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2950 NULL) == -1)
2951 err(1, "pledge");
2952 #endif
2954 while ((ch = getopt(argc, argv, "")) != -1) {
2955 switch (ch) {
2956 default:
2957 usage_diff();
2958 /* NOTREACHED */
2962 argc -= optind;
2963 argv += optind;
2965 if (argc == 0) {
2966 usage_diff(); /* TODO show local worktree changes */
2967 } else if (argc == 2) {
2968 repo_path = getcwd(NULL, 0);
2969 if (repo_path == NULL)
2970 return got_error_from_errno("getcwd");
2971 id_str1 = argv[0];
2972 id_str2 = argv[1];
2973 } else if (argc == 3) {
2974 repo_path = realpath(argv[0], NULL);
2975 if (repo_path == NULL)
2976 return got_error_from_errno2("realpath", argv[0]);
2977 id_str1 = argv[1];
2978 id_str2 = argv[2];
2979 } else
2980 usage_diff();
2982 init_curses();
2984 error = got_repo_open(&repo, repo_path, NULL);
2985 if (error)
2986 goto done;
2988 error = apply_unveil(got_repo_get_path(repo), NULL);
2989 if (error)
2990 goto done;
2992 error = got_repo_match_object_id_prefix(&id1, id_str1,
2993 GOT_OBJ_TYPE_ANY, repo);
2994 if (error)
2995 goto done;
2997 error = got_repo_match_object_id_prefix(&id2, id_str2,
2998 GOT_OBJ_TYPE_ANY, repo);
2999 if (error)
3000 goto done;
3002 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3003 if (error)
3004 goto done;
3006 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3007 if (view == NULL) {
3008 error = got_error_from_errno("view_open");
3009 goto done;
3011 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3012 if (error)
3013 goto done;
3014 error = view_loop(view);
3015 done:
3016 free(repo_path);
3017 if (repo)
3018 got_repo_close(repo);
3019 got_ref_list_free(&refs);
3020 return error;
3023 __dead static void
3024 usage_blame(void)
3026 endwin();
3027 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3028 getprogname());
3029 exit(1);
3032 struct tog_blame_line {
3033 int annotated;
3034 struct got_object_id *id;
3037 static const struct got_error *
3038 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3039 const char *path, struct tog_blame_line *lines, int nlines,
3040 int blame_complete, int selected_line, int *first_displayed_line,
3041 int *last_displayed_line, int *eof, int max_lines)
3043 const struct got_error *err;
3044 int lineno = 0, nprinted = 0;
3045 char *line;
3046 size_t len;
3047 wchar_t *wline;
3048 int width;
3049 struct tog_blame_line *blame_line;
3050 struct got_object_id *prev_id = NULL;
3051 char *id_str;
3053 err = got_object_id_str(&id_str, id);
3054 if (err)
3055 return err;
3057 rewind(f);
3058 werase(view->window);
3060 if (asprintf(&line, "commit %s", id_str) == -1) {
3061 err = got_error_from_errno("asprintf");
3062 free(id_str);
3063 return err;
3066 err = format_line(&wline, &width, line, view->ncols, 0);
3067 free(line);
3068 line = NULL;
3069 if (err)
3070 return err;
3071 if (view_needs_focus_indication(view))
3072 wstandout(view->window);
3073 waddwstr(view->window, wline);
3074 if (view_needs_focus_indication(view))
3075 wstandend(view->window);
3076 free(wline);
3077 wline = NULL;
3078 if (width < view->ncols - 1)
3079 waddch(view->window, '\n');
3081 if (asprintf(&line, "[%d/%d] %s%s",
3082 *first_displayed_line - 1 + selected_line, nlines,
3083 blame_complete ? "" : "annotating... ", path) == -1) {
3084 free(id_str);
3085 return got_error_from_errno("asprintf");
3087 free(id_str);
3088 err = format_line(&wline, &width, line, view->ncols, 0);
3089 free(line);
3090 line = NULL;
3091 if (err)
3092 return err;
3093 waddwstr(view->window, wline);
3094 free(wline);
3095 wline = NULL;
3096 if (width < view->ncols - 1)
3097 waddch(view->window, '\n');
3099 *eof = 0;
3100 while (nprinted < max_lines - 2) {
3101 line = parse_next_line(f, &len);
3102 if (line == NULL) {
3103 *eof = 1;
3104 break;
3106 if (++lineno < *first_displayed_line) {
3107 free(line);
3108 continue;
3111 if (view->ncols <= 9) {
3112 width = 9;
3113 wline = wcsdup(L"");
3114 if (wline == NULL)
3115 err = got_error_from_errno("wcsdup");
3116 } else {
3117 err = format_line(&wline, &width, line,
3118 view->ncols - 9, 9);
3119 width += 9;
3121 if (err) {
3122 free(line);
3123 return err;
3126 if (view->focussed && nprinted == selected_line - 1)
3127 wstandout(view->window);
3129 if (nlines > 0) {
3130 blame_line = &lines[lineno - 1];
3131 if (blame_line->annotated && prev_id &&
3132 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3133 !(view->focussed &&
3134 nprinted == selected_line - 1)) {
3135 waddstr(view->window, " ");
3136 } else if (blame_line->annotated) {
3137 char *id_str;
3138 err = got_object_id_str(&id_str, blame_line->id);
3139 if (err) {
3140 free(line);
3141 free(wline);
3142 return err;
3144 wprintw(view->window, "%.8s", id_str);
3145 free(id_str);
3146 prev_id = blame_line->id;
3147 } else {
3148 waddstr(view->window, "........");
3149 prev_id = NULL;
3151 } else {
3152 waddstr(view->window, "........");
3153 prev_id = NULL;
3156 if (view->focussed && nprinted == selected_line - 1)
3157 wstandend(view->window);
3158 waddstr(view->window, " ");
3160 waddwstr(view->window, wline);
3161 if (width <= view->ncols - 1)
3162 waddch(view->window, '\n');
3163 if (++nprinted == 1)
3164 *first_displayed_line = lineno;
3165 free(line);
3166 free(wline);
3167 wline = NULL;
3169 *last_displayed_line = lineno;
3171 view_vborder(view);
3173 return NULL;
3176 static const struct got_error *
3177 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3179 const struct got_error *err = NULL;
3180 struct tog_blame_cb_args *a = arg;
3181 struct tog_blame_line *line;
3182 int errcode;
3184 if (nlines != a->nlines ||
3185 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3186 return got_error(GOT_ERR_RANGE);
3188 errcode = pthread_mutex_lock(&tog_mutex);
3189 if (errcode)
3190 return got_error_set_errno(errcode, "pthread_mutex_lock");
3192 if (*a->quit) { /* user has quit the blame view */
3193 err = got_error(GOT_ERR_ITER_COMPLETED);
3194 goto done;
3197 if (lineno == -1)
3198 goto done; /* no change in this commit */
3200 line = &a->lines[lineno - 1];
3201 if (line->annotated)
3202 goto done;
3204 line->id = got_object_id_dup(id);
3205 if (line->id == NULL) {
3206 err = got_error_from_errno("got_object_id_dup");
3207 goto done;
3209 line->annotated = 1;
3210 done:
3211 errcode = pthread_mutex_unlock(&tog_mutex);
3212 if (errcode)
3213 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3214 return err;
3217 static void *
3218 blame_thread(void *arg)
3220 const struct got_error *err;
3221 struct tog_blame_thread_args *ta = arg;
3222 struct tog_blame_cb_args *a = ta->cb_args;
3223 int errcode;
3225 err = got_blame(ta->path, a->commit_id, ta->repo,
3226 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3227 if (err && err->code == GOT_ERR_CANCELLED)
3228 err = NULL;
3230 errcode = pthread_mutex_lock(&tog_mutex);
3231 if (errcode)
3232 return (void *)got_error_set_errno(errcode,
3233 "pthread_mutex_lock");
3235 got_repo_close(ta->repo);
3236 ta->repo = NULL;
3237 *ta->complete = 1;
3239 errcode = pthread_mutex_unlock(&tog_mutex);
3240 if (errcode && err == NULL)
3241 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3243 return (void *)err;
3246 static struct got_object_id *
3247 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3248 int first_displayed_line, int selected_line)
3250 struct tog_blame_line *line;
3252 if (nlines <= 0)
3253 return NULL;
3255 line = &lines[first_displayed_line - 1 + selected_line - 1];
3256 if (!line->annotated)
3257 return NULL;
3259 return line->id;
3262 static const struct got_error *
3263 stop_blame(struct tog_blame *blame)
3265 const struct got_error *err = NULL;
3266 int i;
3268 if (blame->thread) {
3269 int errcode;
3270 errcode = pthread_mutex_unlock(&tog_mutex);
3271 if (errcode)
3272 return got_error_set_errno(errcode,
3273 "pthread_mutex_unlock");
3274 errcode = pthread_join(blame->thread, (void **)&err);
3275 if (errcode)
3276 return got_error_set_errno(errcode, "pthread_join");
3277 errcode = pthread_mutex_lock(&tog_mutex);
3278 if (errcode)
3279 return got_error_set_errno(errcode,
3280 "pthread_mutex_lock");
3281 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3282 err = NULL;
3283 blame->thread = NULL;
3285 if (blame->thread_args.repo) {
3286 got_repo_close(blame->thread_args.repo);
3287 blame->thread_args.repo = NULL;
3289 if (blame->f) {
3290 if (fclose(blame->f) != 0 && err == NULL)
3291 err = got_error_from_errno("fclose");
3292 blame->f = NULL;
3294 if (blame->lines) {
3295 for (i = 0; i < blame->nlines; i++)
3296 free(blame->lines[i].id);
3297 free(blame->lines);
3298 blame->lines = NULL;
3300 free(blame->cb_args.commit_id);
3301 blame->cb_args.commit_id = NULL;
3303 return err;
3306 static const struct got_error *
3307 cancel_blame_view(void *arg)
3309 const struct got_error *err = NULL;
3310 int *done = arg;
3311 int errcode;
3313 errcode = pthread_mutex_lock(&tog_mutex);
3314 if (errcode)
3315 return got_error_set_errno(errcode,
3316 "pthread_mutex_unlock");
3318 if (*done)
3319 err = got_error(GOT_ERR_CANCELLED);
3321 errcode = pthread_mutex_unlock(&tog_mutex);
3322 if (errcode)
3323 return got_error_set_errno(errcode,
3324 "pthread_mutex_lock");
3326 return err;
3329 static const struct got_error *
3330 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3331 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3332 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3333 struct got_repository *repo)
3335 const struct got_error *err = NULL;
3336 struct got_blob_object *blob = NULL;
3337 struct got_repository *thread_repo = NULL;
3338 struct got_object_id *obj_id = NULL;
3339 int obj_type;
3341 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3342 if (err)
3343 return err;
3344 if (obj_id == NULL)
3345 return got_error(GOT_ERR_NO_OBJ);
3347 err = got_object_get_type(&obj_type, repo, obj_id);
3348 if (err)
3349 goto done;
3351 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3352 err = got_error(GOT_ERR_OBJ_TYPE);
3353 goto done;
3356 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3357 if (err)
3358 goto done;
3359 blame->f = got_opentemp();
3360 if (blame->f == NULL) {
3361 err = got_error_from_errno("got_opentemp");
3362 goto done;
3364 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3365 &blame->line_offsets, blame->f, blob);
3366 if (err || blame->nlines == 0)
3367 goto done;
3369 /* Don't include \n at EOF in the blame line count. */
3370 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3371 blame->nlines--;
3373 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3374 if (blame->lines == NULL) {
3375 err = got_error_from_errno("calloc");
3376 goto done;
3379 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3380 if (err)
3381 goto done;
3383 blame->cb_args.view = view;
3384 blame->cb_args.lines = blame->lines;
3385 blame->cb_args.nlines = blame->nlines;
3386 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3387 if (blame->cb_args.commit_id == NULL) {
3388 err = got_error_from_errno("got_object_id_dup");
3389 goto done;
3391 blame->cb_args.quit = done;
3393 blame->thread_args.path = path;
3394 blame->thread_args.repo = thread_repo;
3395 blame->thread_args.cb_args = &blame->cb_args;
3396 blame->thread_args.complete = blame_complete;
3397 blame->thread_args.cancel_cb = cancel_blame_view;
3398 blame->thread_args.cancel_arg = done;
3399 *blame_complete = 0;
3401 done:
3402 if (blob)
3403 got_object_blob_close(blob);
3404 free(obj_id);
3405 if (err)
3406 stop_blame(blame);
3407 return err;
3410 static const struct got_error *
3411 open_blame_view(struct tog_view *view, char *path,
3412 struct got_object_id *commit_id, struct got_reflist_head *refs,
3413 struct got_repository *repo)
3415 const struct got_error *err = NULL;
3416 struct tog_blame_view_state *s = &view->state.blame;
3418 SIMPLEQ_INIT(&s->blamed_commits);
3420 s->path = strdup(path);
3421 if (s->path == NULL)
3422 return got_error_from_errno("strdup");
3424 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3425 if (err) {
3426 free(s->path);
3427 return err;
3430 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3431 s->first_displayed_line = 1;
3432 s->last_displayed_line = view->nlines;
3433 s->selected_line = 1;
3434 s->blame_complete = 0;
3435 s->repo = repo;
3436 s->refs = refs;
3437 s->commit_id = commit_id;
3438 memset(&s->blame, 0, sizeof(s->blame));
3440 view->show = show_blame_view;
3441 view->input = input_blame_view;
3442 view->close = close_blame_view;
3443 view->search_start = search_start_blame_view;
3444 view->search_next = search_next_blame_view;
3446 return run_blame(&s->blame, view, &s->blame_complete,
3447 &s->first_displayed_line, &s->last_displayed_line,
3448 &s->selected_line, &s->done, &s->eof, s->path,
3449 s->blamed_commit->id, s->repo);
3452 static const struct got_error *
3453 close_blame_view(struct tog_view *view)
3455 const struct got_error *err = NULL;
3456 struct tog_blame_view_state *s = &view->state.blame;
3458 if (s->blame.thread)
3459 err = stop_blame(&s->blame);
3461 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3462 struct got_object_qid *blamed_commit;
3463 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3464 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3465 got_object_qid_free(blamed_commit);
3468 free(s->path);
3470 return err;
3473 static const struct got_error *
3474 search_start_blame_view(struct tog_view *view)
3476 struct tog_blame_view_state *s = &view->state.blame;
3478 s->matched_line = 0;
3479 return NULL;
3482 static int
3483 match_line(const char *line, regex_t *regex)
3485 regmatch_t regmatch;
3487 return regexec(regex, line, 1, &regmatch, 0) == 0;
3491 static const struct got_error *
3492 search_next_blame_view(struct tog_view *view)
3494 struct tog_blame_view_state *s = &view->state.blame;
3495 int lineno;
3497 if (!view->searching) {
3498 view->search_next_done = 1;
3499 return NULL;
3502 if (s->matched_line) {
3503 if (view->searching == TOG_SEARCH_FORWARD)
3504 lineno = s->matched_line + 1;
3505 else
3506 lineno = s->matched_line - 1;
3507 } else {
3508 if (view->searching == TOG_SEARCH_FORWARD)
3509 lineno = 1;
3510 else
3511 lineno = s->blame.nlines;
3514 while (1) {
3515 char *line = NULL;
3516 off_t offset;
3517 size_t len;
3519 if (lineno <= 0 || lineno > s->blame.nlines) {
3520 if (s->matched_line == 0) {
3521 view->search_next_done = 1;
3522 free(line);
3523 break;
3526 if (view->searching == TOG_SEARCH_FORWARD)
3527 lineno = 1;
3528 else
3529 lineno = s->blame.nlines;
3532 offset = s->blame.line_offsets[lineno - 1];
3533 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3534 free(line);
3535 return got_error_from_errno("fseeko");
3537 free(line);
3538 line = parse_next_line(s->blame.f, &len);
3539 if (line && match_line(line, &view->regex)) {
3540 view->search_next_done = 1;
3541 s->matched_line = lineno;
3542 free(line);
3543 break;
3545 free(line);
3546 if (view->searching == TOG_SEARCH_FORWARD)
3547 lineno++;
3548 else
3549 lineno--;
3552 if (s->matched_line) {
3553 s->first_displayed_line = s->matched_line;
3554 s->selected_line = 1;
3557 return NULL;
3560 static const struct got_error *
3561 show_blame_view(struct tog_view *view)
3563 const struct got_error *err = NULL;
3564 struct tog_blame_view_state *s = &view->state.blame;
3565 int errcode;
3567 if (s->blame.thread == NULL) {
3568 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3569 &s->blame.thread_args);
3570 if (errcode)
3571 return got_error_set_errno(errcode, "pthread_create");
3573 halfdelay(1); /* fast refresh while annotating */
3576 if (s->blame_complete)
3577 halfdelay(10); /* disable fast refresh */
3579 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3580 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3581 s->selected_line, &s->first_displayed_line,
3582 &s->last_displayed_line, &s->eof, view->nlines);
3584 view_vborder(view);
3585 return err;
3588 static const struct got_error *
3589 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3590 struct tog_view **focus_view, struct tog_view *view, int ch)
3592 const struct got_error *err = NULL, *thread_err = NULL;
3593 struct tog_view *diff_view;
3594 struct tog_blame_view_state *s = &view->state.blame;
3595 int begin_x = 0;
3597 switch (ch) {
3598 case 'q':
3599 s->done = 1;
3600 break;
3601 case 'k':
3602 case KEY_UP:
3603 if (s->selected_line > 1)
3604 s->selected_line--;
3605 else if (s->selected_line == 1 &&
3606 s->first_displayed_line > 1)
3607 s->first_displayed_line--;
3608 break;
3609 case KEY_PPAGE:
3610 if (s->first_displayed_line == 1) {
3611 s->selected_line = 1;
3612 break;
3614 if (s->first_displayed_line > view->nlines - 2)
3615 s->first_displayed_line -=
3616 (view->nlines - 2);
3617 else
3618 s->first_displayed_line = 1;
3619 break;
3620 case 'j':
3621 case KEY_DOWN:
3622 if (s->selected_line < view->nlines - 2 &&
3623 s->first_displayed_line +
3624 s->selected_line <= s->blame.nlines)
3625 s->selected_line++;
3626 else if (s->last_displayed_line <
3627 s->blame.nlines)
3628 s->first_displayed_line++;
3629 break;
3630 case 'b':
3631 case 'p': {
3632 struct got_object_id *id = NULL;
3633 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3634 s->first_displayed_line, s->selected_line);
3635 if (id == NULL)
3636 break;
3637 if (ch == 'p') {
3638 struct got_commit_object *commit;
3639 struct got_object_qid *pid;
3640 struct got_object_id *blob_id = NULL;
3641 int obj_type;
3642 err = got_object_open_as_commit(&commit,
3643 s->repo, id);
3644 if (err)
3645 break;
3646 pid = SIMPLEQ_FIRST(
3647 got_object_commit_get_parent_ids(commit));
3648 if (pid == NULL) {
3649 got_object_commit_close(commit);
3650 break;
3652 /* Check if path history ends here. */
3653 err = got_object_id_by_path(&blob_id, s->repo,
3654 pid->id, s->path);
3655 if (err) {
3656 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3657 err = NULL;
3658 got_object_commit_close(commit);
3659 break;
3661 err = got_object_get_type(&obj_type, s->repo,
3662 blob_id);
3663 free(blob_id);
3664 /* Can't blame non-blob type objects. */
3665 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3666 got_object_commit_close(commit);
3667 break;
3669 err = got_object_qid_alloc(&s->blamed_commit,
3670 pid->id);
3671 got_object_commit_close(commit);
3672 } else {
3673 if (got_object_id_cmp(id,
3674 s->blamed_commit->id) == 0)
3675 break;
3676 err = got_object_qid_alloc(&s->blamed_commit,
3677 id);
3679 if (err)
3680 break;
3681 s->done = 1;
3682 thread_err = stop_blame(&s->blame);
3683 s->done = 0;
3684 if (thread_err)
3685 break;
3686 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3687 s->blamed_commit, entry);
3688 err = run_blame(&s->blame, view, &s->blame_complete,
3689 &s->first_displayed_line, &s->last_displayed_line,
3690 &s->selected_line, &s->done, &s->eof,
3691 s->path, s->blamed_commit->id, s->repo);
3692 if (err)
3693 break;
3694 break;
3696 case 'B': {
3697 struct got_object_qid *first;
3698 first = SIMPLEQ_FIRST(&s->blamed_commits);
3699 if (!got_object_id_cmp(first->id, s->commit_id))
3700 break;
3701 s->done = 1;
3702 thread_err = stop_blame(&s->blame);
3703 s->done = 0;
3704 if (thread_err)
3705 break;
3706 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3707 got_object_qid_free(s->blamed_commit);
3708 s->blamed_commit =
3709 SIMPLEQ_FIRST(&s->blamed_commits);
3710 err = run_blame(&s->blame, view, &s->blame_complete,
3711 &s->first_displayed_line, &s->last_displayed_line,
3712 &s->selected_line, &s->done, &s->eof, s->path,
3713 s->blamed_commit->id, s->repo);
3714 if (err)
3715 break;
3716 break;
3718 case KEY_ENTER:
3719 case '\r': {
3720 struct got_object_id *id = NULL;
3721 struct got_object_qid *pid;
3722 struct got_commit_object *commit = NULL;
3723 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3724 s->first_displayed_line, s->selected_line);
3725 if (id == NULL)
3726 break;
3727 err = got_object_open_as_commit(&commit, s->repo, id);
3728 if (err)
3729 break;
3730 pid = SIMPLEQ_FIRST(
3731 got_object_commit_get_parent_ids(commit));
3732 if (view_is_parent_view(view))
3733 begin_x = view_split_begin_x(view->begin_x);
3734 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3735 if (diff_view == NULL) {
3736 got_object_commit_close(commit);
3737 err = got_error_from_errno("view_open");
3738 break;
3740 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3741 id, NULL, s->refs, s->repo);
3742 got_object_commit_close(commit);
3743 if (err) {
3744 view_close(diff_view);
3745 break;
3747 if (view_is_parent_view(view)) {
3748 err = view_close_child(view);
3749 if (err)
3750 break;
3751 err = view_set_child(view, diff_view);
3752 if (err) {
3753 view_close(diff_view);
3754 break;
3756 *focus_view = diff_view;
3757 view->child_focussed = 1;
3758 } else
3759 *new_view = diff_view;
3760 if (err)
3761 break;
3762 break;
3764 case KEY_NPAGE:
3765 case ' ':
3766 if (s->last_displayed_line >= s->blame.nlines &&
3767 s->selected_line >= MIN(s->blame.nlines,
3768 view->nlines - 2)) {
3769 break;
3771 if (s->last_displayed_line >= s->blame.nlines &&
3772 s->selected_line < view->nlines - 2) {
3773 s->selected_line = MIN(s->blame.nlines,
3774 view->nlines - 2);
3775 break;
3777 if (s->last_displayed_line + view->nlines - 2
3778 <= s->blame.nlines)
3779 s->first_displayed_line +=
3780 view->nlines - 2;
3781 else
3782 s->first_displayed_line =
3783 s->blame.nlines -
3784 (view->nlines - 3);
3785 break;
3786 case KEY_RESIZE:
3787 if (s->selected_line > view->nlines - 2) {
3788 s->selected_line = MIN(s->blame.nlines,
3789 view->nlines - 2);
3791 break;
3792 default:
3793 break;
3795 return thread_err ? thread_err : err;
3798 static const struct got_error *
3799 cmd_blame(int argc, char *argv[])
3801 const struct got_error *error;
3802 struct got_repository *repo = NULL;
3803 struct got_reflist_head refs;
3804 struct got_worktree *worktree = NULL;
3805 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3806 struct got_object_id *commit_id = NULL;
3807 char *commit_id_str = NULL;
3808 int ch;
3809 struct tog_view *view;
3811 SIMPLEQ_INIT(&refs);
3813 #ifndef PROFILE
3814 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3815 NULL) == -1)
3816 err(1, "pledge");
3817 #endif
3819 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3820 switch (ch) {
3821 case 'c':
3822 commit_id_str = optarg;
3823 break;
3824 case 'r':
3825 repo_path = realpath(optarg, NULL);
3826 if (repo_path == NULL)
3827 err(1, "-r option");
3828 break;
3829 default:
3830 usage_blame();
3831 /* NOTREACHED */
3835 argc -= optind;
3836 argv += optind;
3838 if (argc == 1)
3839 path = argv[0];
3840 else
3841 usage_blame();
3843 cwd = getcwd(NULL, 0);
3844 if (cwd == NULL) {
3845 error = got_error_from_errno("getcwd");
3846 goto done;
3848 if (repo_path == NULL) {
3849 error = got_worktree_open(&worktree, cwd);
3850 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3851 goto done;
3852 else
3853 error = NULL;
3854 if (worktree) {
3855 repo_path =
3856 strdup(got_worktree_get_repo_path(worktree));
3857 if (repo_path == NULL)
3858 error = got_error_from_errno("strdup");
3859 if (error)
3860 goto done;
3861 } else {
3862 repo_path = strdup(cwd);
3863 if (repo_path == NULL) {
3864 error = got_error_from_errno("strdup");
3865 goto done;
3870 init_curses();
3872 error = got_repo_open(&repo, repo_path, NULL);
3873 if (error != NULL)
3874 goto done;
3876 error = apply_unveil(got_repo_get_path(repo), NULL);
3877 if (error)
3878 goto done;
3880 if (worktree) {
3881 const char *prefix = got_worktree_get_path_prefix(worktree);
3882 char *p, *worktree_subdir = cwd +
3883 strlen(got_worktree_get_root_path(worktree));
3884 if (asprintf(&p, "%s%s%s%s%s",
3885 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3886 worktree_subdir, worktree_subdir[0] ? "/" : "",
3887 path) == -1) {
3888 error = got_error_from_errno("asprintf");
3889 goto done;
3891 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3892 free(p);
3893 } else {
3894 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3896 if (error)
3897 goto done;
3899 if (commit_id_str == NULL) {
3900 struct got_reference *head_ref;
3901 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3902 if (error != NULL)
3903 goto done;
3904 error = got_ref_resolve(&commit_id, repo, head_ref);
3905 got_ref_close(head_ref);
3906 } else {
3907 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3908 if (error) {
3909 if (error->code != GOT_ERR_NOT_REF)
3910 goto done;
3911 error = got_repo_match_object_id_prefix(&commit_id,
3912 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3915 if (error != NULL)
3916 goto done;
3918 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3919 if (error)
3920 goto done;
3922 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3923 if (view == NULL) {
3924 error = got_error_from_errno("view_open");
3925 goto done;
3927 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3928 if (error)
3929 goto done;
3930 if (worktree) {
3931 /* Release work tree lock. */
3932 got_worktree_close(worktree);
3933 worktree = NULL;
3935 error = view_loop(view);
3936 done:
3937 free(repo_path);
3938 free(cwd);
3939 free(commit_id);
3940 if (worktree)
3941 got_worktree_close(worktree);
3942 if (repo)
3943 got_repo_close(repo);
3944 got_ref_list_free(&refs);
3945 return error;
3948 static const struct got_error *
3949 draw_tree_entries(struct tog_view *view,
3950 struct got_tree_entry **first_displayed_entry,
3951 struct got_tree_entry **last_displayed_entry,
3952 struct got_tree_entry **selected_entry, int *ndisplayed,
3953 const char *label, int show_ids, const char *parent_path,
3954 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3956 const struct got_error *err = NULL;
3957 struct got_tree_entry *te;
3958 wchar_t *wline;
3959 int width, n;
3961 *ndisplayed = 0;
3963 werase(view->window);
3965 if (limit == 0)
3966 return NULL;
3968 err = format_line(&wline, &width, label, view->ncols, 0);
3969 if (err)
3970 return err;
3971 if (view_needs_focus_indication(view))
3972 wstandout(view->window);
3973 waddwstr(view->window, wline);
3974 if (view_needs_focus_indication(view))
3975 wstandend(view->window);
3976 free(wline);
3977 wline = NULL;
3978 if (width < view->ncols - 1)
3979 waddch(view->window, '\n');
3980 if (--limit <= 0)
3981 return NULL;
3982 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3983 if (err)
3984 return err;
3985 waddwstr(view->window, wline);
3986 free(wline);
3987 wline = NULL;
3988 if (width < view->ncols - 1)
3989 waddch(view->window, '\n');
3990 if (--limit <= 0)
3991 return NULL;
3992 waddch(view->window, '\n');
3993 if (--limit <= 0)
3994 return NULL;
3996 te = SIMPLEQ_FIRST(&entries->head);
3997 if (*first_displayed_entry == NULL) {
3998 if (selected == 0) {
3999 if (view->focussed)
4000 wstandout(view->window);
4001 *selected_entry = NULL;
4003 waddstr(view->window, " ..\n"); /* parent directory */
4004 if (selected == 0 && view->focussed)
4005 wstandend(view->window);
4006 (*ndisplayed)++;
4007 if (--limit <= 0)
4008 return NULL;
4009 n = 1;
4010 } else {
4011 n = 0;
4012 while (te != *first_displayed_entry)
4013 te = SIMPLEQ_NEXT(te, entry);
4016 while (te) {
4017 char *line = NULL, *id_str = NULL;
4018 const char *modestr = "";
4020 if (show_ids) {
4021 err = got_object_id_str(&id_str, te->id);
4022 if (err)
4023 return got_error_from_errno(
4024 "got_object_id_str");
4026 if (got_object_tree_entry_is_submodule(te))
4027 modestr = "$";
4028 else if (S_ISLNK(te->mode))
4029 modestr = "@";
4030 else if (S_ISDIR(te->mode))
4031 modestr = "/";
4032 else if (te->mode & S_IXUSR)
4033 modestr = "*";
4034 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4035 te->name, modestr) == -1) {
4036 free(id_str);
4037 return got_error_from_errno("asprintf");
4039 free(id_str);
4040 err = format_line(&wline, &width, line, view->ncols, 0);
4041 if (err) {
4042 free(line);
4043 break;
4045 if (n == selected) {
4046 if (view->focussed)
4047 wstandout(view->window);
4048 *selected_entry = te;
4050 waddwstr(view->window, wline);
4051 if (width < view->ncols - 1)
4052 waddch(view->window, '\n');
4053 if (n == selected && view->focussed)
4054 wstandend(view->window);
4055 free(line);
4056 free(wline);
4057 wline = NULL;
4058 n++;
4059 (*ndisplayed)++;
4060 *last_displayed_entry = te;
4061 if (--limit <= 0)
4062 break;
4063 te = SIMPLEQ_NEXT(te, entry);
4066 return err;
4069 static void
4070 tree_scroll_up(struct tog_view *view,
4071 struct got_tree_entry **first_displayed_entry, int maxscroll,
4072 const struct got_tree_entries *entries, int isroot)
4074 struct got_tree_entry *te, *prev;
4075 int i;
4077 if (*first_displayed_entry == NULL)
4078 return;
4080 te = SIMPLEQ_FIRST(&entries->head);
4081 if (*first_displayed_entry == te) {
4082 if (!isroot)
4083 *first_displayed_entry = NULL;
4084 return;
4087 /* XXX this is stupid... switch to TAILQ? */
4088 for (i = 0; i < maxscroll; i++) {
4089 while (te != *first_displayed_entry) {
4090 prev = te;
4091 te = SIMPLEQ_NEXT(te, entry);
4093 *first_displayed_entry = prev;
4094 te = SIMPLEQ_FIRST(&entries->head);
4096 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4097 *first_displayed_entry = NULL;
4100 static int
4101 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4102 struct got_tree_entry *last_displayed_entry,
4103 const struct got_tree_entries *entries)
4105 struct got_tree_entry *next, *last;
4106 int n = 0;
4108 if (*first_displayed_entry)
4109 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4110 else
4111 next = SIMPLEQ_FIRST(&entries->head);
4112 last = last_displayed_entry;
4113 while (next && last && n++ < maxscroll) {
4114 last = SIMPLEQ_NEXT(last, entry);
4115 if (last) {
4116 *first_displayed_entry = next;
4117 next = SIMPLEQ_NEXT(next, entry);
4120 return n;
4123 static const struct got_error *
4124 tree_entry_path(char **path, struct tog_parent_trees *parents,
4125 struct got_tree_entry *te)
4127 const struct got_error *err = NULL;
4128 struct tog_parent_tree *pt;
4129 size_t len = 2; /* for leading slash and NUL */
4131 TAILQ_FOREACH(pt, parents, entry)
4132 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4133 if (te)
4134 len += strlen(te->name);
4136 *path = calloc(1, len);
4137 if (path == NULL)
4138 return got_error_from_errno("calloc");
4140 (*path)[0] = '/';
4141 pt = TAILQ_LAST(parents, tog_parent_trees);
4142 while (pt) {
4143 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4144 err = got_error(GOT_ERR_NO_SPACE);
4145 goto done;
4147 if (strlcat(*path, "/", len) >= len) {
4148 err = got_error(GOT_ERR_NO_SPACE);
4149 goto done;
4151 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4153 if (te) {
4154 if (strlcat(*path, te->name, len) >= len) {
4155 err = got_error(GOT_ERR_NO_SPACE);
4156 goto done;
4159 done:
4160 if (err) {
4161 free(*path);
4162 *path = NULL;
4164 return err;
4167 static const struct got_error *
4168 blame_tree_entry(struct tog_view **new_view, int begin_x,
4169 struct got_tree_entry *te, struct tog_parent_trees *parents,
4170 struct got_object_id *commit_id, struct got_reflist_head *refs,
4171 struct got_repository *repo)
4173 const struct got_error *err = NULL;
4174 char *path;
4175 struct tog_view *blame_view;
4177 *new_view = NULL;
4179 err = tree_entry_path(&path, parents, te);
4180 if (err)
4181 return err;
4183 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4184 if (blame_view == NULL) {
4185 err = got_error_from_errno("view_open");
4186 goto done;
4189 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4190 if (err) {
4191 if (err->code == GOT_ERR_CANCELLED)
4192 err = NULL;
4193 view_close(blame_view);
4194 } else
4195 *new_view = blame_view;
4196 done:
4197 free(path);
4198 return err;
4201 static const struct got_error *
4202 log_tree_entry(struct tog_view **new_view, int begin_x,
4203 struct got_tree_entry *te, struct tog_parent_trees *parents,
4204 struct got_object_id *commit_id, struct got_reflist_head *refs,
4205 struct got_repository *repo)
4207 struct tog_view *log_view;
4208 const struct got_error *err = NULL;
4209 char *path;
4211 *new_view = NULL;
4213 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4214 if (log_view == NULL)
4215 return got_error_from_errno("view_open");
4217 err = tree_entry_path(&path, parents, te);
4218 if (err)
4219 return err;
4221 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4222 if (err)
4223 view_close(log_view);
4224 else
4225 *new_view = log_view;
4226 free(path);
4227 return err;
4230 static const struct got_error *
4231 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4232 struct got_object_id *commit_id, struct got_reflist_head *refs,
4233 struct got_repository *repo)
4235 const struct got_error *err = NULL;
4236 char *commit_id_str = NULL;
4237 struct tog_tree_view_state *s = &view->state.tree;
4239 TAILQ_INIT(&s->parents);
4241 err = got_object_id_str(&commit_id_str, commit_id);
4242 if (err != NULL)
4243 goto done;
4245 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4246 err = got_error_from_errno("asprintf");
4247 goto done;
4250 s->root = s->tree = root;
4251 s->entries = got_object_tree_get_entries(root);
4252 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4253 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4254 s->commit_id = got_object_id_dup(commit_id);
4255 if (s->commit_id == NULL) {
4256 err = got_error_from_errno("got_object_id_dup");
4257 goto done;
4259 s->refs = refs;
4260 s->repo = repo;
4262 view->show = show_tree_view;
4263 view->input = input_tree_view;
4264 view->close = close_tree_view;
4265 view->search_start = search_start_tree_view;
4266 view->search_next = search_next_tree_view;
4267 done:
4268 free(commit_id_str);
4269 if (err) {
4270 free(s->tree_label);
4271 s->tree_label = NULL;
4273 return err;
4276 static const struct got_error *
4277 close_tree_view(struct tog_view *view)
4279 struct tog_tree_view_state *s = &view->state.tree;
4281 free(s->tree_label);
4282 s->tree_label = NULL;
4283 free(s->commit_id);
4284 s->commit_id = NULL;
4285 while (!TAILQ_EMPTY(&s->parents)) {
4286 struct tog_parent_tree *parent;
4287 parent = TAILQ_FIRST(&s->parents);
4288 TAILQ_REMOVE(&s->parents, parent, entry);
4289 free(parent);
4292 if (s->tree != s->root)
4293 got_object_tree_close(s->tree);
4294 got_object_tree_close(s->root);
4296 return NULL;
4299 static const struct got_error *
4300 search_start_tree_view(struct tog_view *view)
4302 struct tog_tree_view_state *s = &view->state.tree;
4304 s->matched_entry = NULL;
4305 return NULL;
4308 static int
4309 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4311 regmatch_t regmatch;
4313 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4316 static const struct got_error *
4317 search_next_tree_view(struct tog_view *view)
4319 struct tog_tree_view_state *s = &view->state.tree;
4320 struct got_tree_entry *entry = NULL, *te;
4322 if (!view->searching) {
4323 view->search_next_done = 1;
4324 return NULL;
4327 if (s->matched_entry) {
4328 if (view->searching == TOG_SEARCH_FORWARD) {
4329 if (s->selected_entry)
4330 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4331 else
4332 entry = SIMPLEQ_FIRST(&s->entries->head);
4334 else {
4335 if (s->selected_entry == NULL) {
4336 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4337 entry = te;
4338 } else {
4339 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4340 entry = te;
4341 if (SIMPLEQ_NEXT(te, entry) ==
4342 s->selected_entry)
4343 break;
4347 } else {
4348 if (view->searching == TOG_SEARCH_FORWARD)
4349 entry = SIMPLEQ_FIRST(&s->entries->head);
4350 else {
4351 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4352 entry = te;
4356 while (1) {
4357 if (entry == NULL) {
4358 if (s->matched_entry == NULL) {
4359 view->search_next_done = 1;
4360 return NULL;
4362 if (view->searching == TOG_SEARCH_FORWARD)
4363 entry = SIMPLEQ_FIRST(&s->entries->head);
4364 else {
4365 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4366 entry = te;
4370 if (match_tree_entry(entry, &view->regex)) {
4371 view->search_next_done = 1;
4372 s->matched_entry = entry;
4373 break;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4377 entry = SIMPLEQ_NEXT(entry, entry);
4378 else {
4379 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4380 entry = NULL;
4381 else {
4382 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4383 if (SIMPLEQ_NEXT(te, entry) == entry) {
4384 entry = te;
4385 break;
4392 if (s->matched_entry) {
4393 s->first_displayed_entry = s->matched_entry;
4394 s->selected = 0;
4397 return NULL;
4400 static const struct got_error *
4401 show_tree_view(struct tog_view *view)
4403 const struct got_error *err = NULL;
4404 struct tog_tree_view_state *s = &view->state.tree;
4405 char *parent_path;
4407 err = tree_entry_path(&parent_path, &s->parents, NULL);
4408 if (err)
4409 return err;
4411 err = draw_tree_entries(view, &s->first_displayed_entry,
4412 &s->last_displayed_entry, &s->selected_entry,
4413 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4414 s->entries, s->selected, view->nlines, s->tree == s->root);
4415 free(parent_path);
4417 view_vborder(view);
4418 return err;
4421 static const struct got_error *
4422 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4423 struct tog_view **focus_view, struct tog_view *view, int ch)
4425 const struct got_error *err = NULL;
4426 struct tog_tree_view_state *s = &view->state.tree;
4427 struct tog_view *log_view;
4428 int begin_x = 0, nscrolled;
4430 switch (ch) {
4431 case 'i':
4432 s->show_ids = !s->show_ids;
4433 break;
4434 case 'l':
4435 if (!s->selected_entry)
4436 break;
4437 if (view_is_parent_view(view))
4438 begin_x = view_split_begin_x(view->begin_x);
4439 err = log_tree_entry(&log_view, begin_x,
4440 s->selected_entry, &s->parents,
4441 s->commit_id, s->refs, s->repo);
4442 if (view_is_parent_view(view)) {
4443 err = view_close_child(view);
4444 if (err)
4445 return err;
4446 err = view_set_child(view, log_view);
4447 if (err) {
4448 view_close(log_view);
4449 break;
4451 *focus_view = log_view;
4452 view->child_focussed = 1;
4453 } else
4454 *new_view = log_view;
4455 break;
4456 case 'k':
4457 case KEY_UP:
4458 if (s->selected > 0) {
4459 s->selected--;
4460 if (s->selected == 0)
4461 break;
4463 if (s->selected > 0)
4464 break;
4465 tree_scroll_up(view, &s->first_displayed_entry, 1,
4466 s->entries, s->tree == s->root);
4467 break;
4468 case KEY_PPAGE:
4469 tree_scroll_up(view, &s->first_displayed_entry,
4470 MAX(0, view->nlines - 4 - s->selected), s->entries,
4471 s->tree == s->root);
4472 s->selected = 0;
4473 if (SIMPLEQ_FIRST(&s->entries->head) ==
4474 s->first_displayed_entry && s->tree != s->root)
4475 s->first_displayed_entry = NULL;
4476 break;
4477 case 'j':
4478 case KEY_DOWN:
4479 if (s->selected < s->ndisplayed - 1) {
4480 s->selected++;
4481 break;
4483 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4484 /* can't scroll any further */
4485 break;
4486 tree_scroll_down(&s->first_displayed_entry, 1,
4487 s->last_displayed_entry, s->entries);
4488 break;
4489 case KEY_NPAGE:
4490 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4491 == NULL) {
4492 /* can't scroll any further; move cursor down */
4493 if (s->selected < s->ndisplayed - 1)
4494 s->selected = s->ndisplayed - 1;
4495 break;
4497 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4498 view->nlines, s->last_displayed_entry, s->entries);
4499 if (nscrolled < view->nlines) {
4500 int ndisplayed = 0;
4501 struct got_tree_entry *te;
4502 te = s->first_displayed_entry;
4503 do {
4504 ndisplayed++;
4505 te = SIMPLEQ_NEXT(te, entry);
4506 } while (te);
4507 s->selected = ndisplayed - 1;
4509 break;
4510 case KEY_ENTER:
4511 case '\r':
4512 case KEY_BACKSPACE:
4513 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4514 struct tog_parent_tree *parent;
4515 /* user selected '..' */
4516 if (s->tree == s->root)
4517 break;
4518 parent = TAILQ_FIRST(&s->parents);
4519 TAILQ_REMOVE(&s->parents, parent,
4520 entry);
4521 got_object_tree_close(s->tree);
4522 s->tree = parent->tree;
4523 s->entries =
4524 got_object_tree_get_entries(s->tree);
4525 s->first_displayed_entry =
4526 parent->first_displayed_entry;
4527 s->selected_entry =
4528 parent->selected_entry;
4529 s->selected = parent->selected;
4530 free(parent);
4531 } else if (S_ISDIR(s->selected_entry->mode)) {
4532 struct got_tree_object *subtree;
4533 err = got_object_open_as_tree(&subtree,
4534 s->repo, s->selected_entry->id);
4535 if (err)
4536 break;
4537 err = tree_view_visit_subtree(subtree, s);
4538 if (err) {
4539 got_object_tree_close(subtree);
4540 break;
4542 } else if (S_ISREG(s->selected_entry->mode)) {
4543 struct tog_view *blame_view;
4544 int begin_x = view_is_parent_view(view) ?
4545 view_split_begin_x(view->begin_x) : 0;
4547 err = blame_tree_entry(&blame_view, begin_x,
4548 s->selected_entry, &s->parents,
4549 s->commit_id, s->refs, s->repo);
4550 if (err)
4551 break;
4552 if (view_is_parent_view(view)) {
4553 err = view_close_child(view);
4554 if (err)
4555 return err;
4556 err = view_set_child(view, blame_view);
4557 if (err) {
4558 view_close(blame_view);
4559 break;
4561 *focus_view = blame_view;
4562 view->child_focussed = 1;
4563 } else
4564 *new_view = blame_view;
4566 break;
4567 case KEY_RESIZE:
4568 if (s->selected > view->nlines)
4569 s->selected = s->ndisplayed - 1;
4570 break;
4571 default:
4572 break;
4575 return err;
4578 __dead static void
4579 usage_tree(void)
4581 endwin();
4582 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4583 getprogname());
4584 exit(1);
4587 static const struct got_error *
4588 cmd_tree(int argc, char *argv[])
4590 const struct got_error *error;
4591 struct got_repository *repo = NULL;
4592 struct got_reflist_head refs;
4593 char *repo_path = NULL;
4594 struct got_object_id *commit_id = NULL;
4595 char *commit_id_arg = NULL;
4596 struct got_commit_object *commit = NULL;
4597 struct got_tree_object *tree = NULL;
4598 int ch;
4599 struct tog_view *view;
4601 SIMPLEQ_INIT(&refs);
4603 #ifndef PROFILE
4604 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4605 NULL) == -1)
4606 err(1, "pledge");
4607 #endif
4609 while ((ch = getopt(argc, argv, "c:")) != -1) {
4610 switch (ch) {
4611 case 'c':
4612 commit_id_arg = optarg;
4613 break;
4614 default:
4615 usage_tree();
4616 /* NOTREACHED */
4620 argc -= optind;
4621 argv += optind;
4623 if (argc == 0) {
4624 struct got_worktree *worktree;
4625 char *cwd = getcwd(NULL, 0);
4626 if (cwd == NULL)
4627 return got_error_from_errno("getcwd");
4628 error = got_worktree_open(&worktree, cwd);
4629 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4630 goto done;
4631 if (worktree) {
4632 free(cwd);
4633 repo_path =
4634 strdup(got_worktree_get_repo_path(worktree));
4635 got_worktree_close(worktree);
4636 } else
4637 repo_path = cwd;
4638 if (repo_path == NULL) {
4639 error = got_error_from_errno("strdup");
4640 goto done;
4642 } else if (argc == 1) {
4643 repo_path = realpath(argv[0], NULL);
4644 if (repo_path == NULL)
4645 return got_error_from_errno2("realpath", argv[0]);
4646 } else
4647 usage_log();
4649 init_curses();
4651 error = got_repo_open(&repo, repo_path, NULL);
4652 if (error != NULL)
4653 goto done;
4655 error = apply_unveil(got_repo_get_path(repo), NULL);
4656 if (error)
4657 goto done;
4659 if (commit_id_arg == NULL)
4660 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4661 else {
4662 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4663 if (error) {
4664 if (error->code != GOT_ERR_NOT_REF)
4665 goto done;
4666 error = got_repo_match_object_id_prefix(&commit_id,
4667 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4670 if (error != NULL)
4671 goto done;
4673 error = got_object_open_as_commit(&commit, repo, commit_id);
4674 if (error != NULL)
4675 goto done;
4677 error = got_object_open_as_tree(&tree, repo,
4678 got_object_commit_get_tree_id(commit));
4679 if (error != NULL)
4680 goto done;
4682 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4683 if (error)
4684 goto done;
4686 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4687 if (view == NULL) {
4688 error = got_error_from_errno("view_open");
4689 goto done;
4691 error = open_tree_view(view, tree, commit_id, &refs, repo);
4692 if (error)
4693 goto done;
4694 error = view_loop(view);
4695 done:
4696 free(repo_path);
4697 free(commit_id);
4698 if (commit)
4699 got_object_commit_close(commit);
4700 if (tree)
4701 got_object_tree_close(tree);
4702 if (repo)
4703 got_repo_close(repo);
4704 got_ref_list_free(&refs);
4705 return error;
4708 static void
4709 list_commands(void)
4711 int i;
4713 fprintf(stderr, "commands:");
4714 for (i = 0; i < nitems(tog_commands); i++) {
4715 struct tog_cmd *cmd = &tog_commands[i];
4716 fprintf(stderr, " %s", cmd->name);
4718 fputc('\n', stderr);
4721 __dead static void
4722 usage(int hflag)
4724 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4725 getprogname());
4726 if (hflag)
4727 list_commands();
4728 exit(1);
4731 static char **
4732 make_argv(const char *arg0, const char *arg1)
4734 char **argv;
4735 int argc = (arg1 == NULL ? 1 : 2);
4737 argv = calloc(argc, sizeof(char *));
4738 if (argv == NULL)
4739 err(1, "calloc");
4740 argv[0] = strdup(arg0);
4741 if (argv[0] == NULL)
4742 err(1, "strdup");
4743 if (arg1) {
4744 argv[1] = strdup(arg1);
4745 if (argv[1] == NULL)
4746 err(1, "strdup");
4749 return argv;
4752 int
4753 main(int argc, char *argv[])
4755 const struct got_error *error = NULL;
4756 struct tog_cmd *cmd = NULL;
4757 int ch, hflag = 0, Vflag = 0;
4758 char **cmd_argv = NULL;
4760 setlocale(LC_CTYPE, "");
4762 while ((ch = getopt(argc, argv, "hV")) != -1) {
4763 switch (ch) {
4764 case 'h':
4765 hflag = 1;
4766 break;
4767 case 'V':
4768 Vflag = 1;
4769 break;
4770 default:
4771 usage(hflag);
4772 /* NOTREACHED */
4776 argc -= optind;
4777 argv += optind;
4778 optind = 0;
4779 optreset = 1;
4781 if (Vflag) {
4782 got_version_print_str();
4783 return 1;
4786 if (argc == 0) {
4787 if (hflag)
4788 usage(hflag);
4789 /* Build an argument vector which runs a default command. */
4790 cmd = &tog_commands[0];
4791 cmd_argv = make_argv(cmd->name, NULL);
4792 argc = 1;
4793 } else {
4794 int i;
4796 /* Did the user specific a command? */
4797 for (i = 0; i < nitems(tog_commands); i++) {
4798 if (strncmp(tog_commands[i].name, argv[0],
4799 strlen(argv[0])) == 0) {
4800 cmd = &tog_commands[i];
4801 break;
4805 if (cmd == NULL) {
4806 fprintf(stderr, "%s: unknown command '%s'\n",
4807 getprogname(), argv[0]);
4808 list_commands();
4809 return 1;
4813 if (hflag)
4814 cmd->cmd_usage();
4815 else
4816 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4818 endwin();
4819 free(cmd_argv);
4820 if (error && error->code != GOT_ERR_CANCELLED)
4821 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4822 return 0;