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 if (avail >= 120) {
1092 char *id_str;
1093 err = got_object_id_str(&id_str, id);
1094 if (err)
1095 goto done;
1096 wprintw(view->window, "%.8s ", id_str);
1097 free(id_str);
1098 col += 9;
1099 if (col > avail)
1100 goto done;
1103 author = strdup(got_object_commit_get_author(commit));
1104 if (author == NULL) {
1105 err = got_error_from_errno("strdup");
1106 goto done;
1108 err = format_author(&wauthor, &author_width, author, avail - col, col);
1109 if (err)
1110 goto done;
1111 waddwstr(view->window, wauthor);
1112 col += author_width;
1113 while (col < avail && author_width < author_display_cols + 2) {
1114 waddch(view->window, ' ');
1115 col++;
1116 author_width++;
1118 if (col > avail)
1119 goto done;
1121 err = got_object_commit_get_logmsg(&logmsg0, commit);
1122 if (err)
1123 goto done;
1124 logmsg = logmsg0;
1125 while (*logmsg == '\n')
1126 logmsg++;
1127 newline = strchr(logmsg, '\n');
1128 if (newline)
1129 *newline = '\0';
1130 limit = avail - col;
1131 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1132 if (err)
1133 goto done;
1134 waddwstr(view->window, wlogmsg);
1135 col += logmsg_width;
1136 while (col < avail) {
1137 waddch(view->window, ' ');
1138 col++;
1140 done:
1141 free(logmsg0);
1142 free(wlogmsg);
1143 free(author);
1144 free(wauthor);
1145 free(line);
1146 return err;
1149 static struct commit_queue_entry *
1150 alloc_commit_queue_entry(struct got_commit_object *commit,
1151 struct got_object_id *id)
1153 struct commit_queue_entry *entry;
1155 entry = calloc(1, sizeof(*entry));
1156 if (entry == NULL)
1157 return NULL;
1159 entry->id = id;
1160 entry->commit = commit;
1161 return entry;
1164 static void
1165 pop_commit(struct commit_queue *commits)
1167 struct commit_queue_entry *entry;
1169 entry = TAILQ_FIRST(&commits->head);
1170 TAILQ_REMOVE(&commits->head, entry, entry);
1171 got_object_commit_close(entry->commit);
1172 commits->ncommits--;
1173 /* Don't free entry->id! It is owned by the commit graph. */
1174 free(entry);
1177 static void
1178 free_commits(struct commit_queue *commits)
1180 while (!TAILQ_EMPTY(&commits->head))
1181 pop_commit(commits);
1184 static const struct got_error *
1185 match_commit(int *have_match, struct got_object_id *id,
1186 struct got_commit_object *commit, regex_t *regex)
1188 const struct got_error *err = NULL;
1189 regmatch_t regmatch;
1190 char *id_str = NULL, *logmsg = NULL;
1192 *have_match = 0;
1194 err = got_object_id_str(&id_str, id);
1195 if (err)
1196 return err;
1198 err = got_object_commit_get_logmsg(&logmsg, commit);
1199 if (err)
1200 goto done;
1202 if (regexec(regex, got_object_commit_get_author(commit), 1,
1203 &regmatch, 0) == 0 ||
1204 regexec(regex, got_object_commit_get_committer(commit), 1,
1205 &regmatch, 0) == 0 ||
1206 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1207 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1208 *have_match = 1;
1209 done:
1210 free(id_str);
1211 free(logmsg);
1212 return err;
1215 static const struct got_error *
1216 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1217 int minqueue, struct got_repository *repo, const char *path,
1218 int *searching, int *search_next_done, regex_t *regex)
1220 const struct got_error *err = NULL;
1221 int nqueued = 0, have_match = 0;
1224 * We keep all commits open throughout the lifetime of the log
1225 * view in order to avoid having to re-fetch commits from disk
1226 * while updating the display.
1228 while (nqueued < minqueue ||
1229 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1230 struct got_object_id *id;
1231 struct got_commit_object *commit;
1232 struct commit_queue_entry *entry;
1233 int errcode;
1235 err = got_commit_graph_iter_next(&id, graph);
1236 if (err) {
1237 if (err->code != GOT_ERR_ITER_NEED_MORE)
1238 break;
1239 err = got_commit_graph_fetch_commits(graph,
1240 minqueue, repo, NULL, NULL);
1241 if (err)
1242 return err;
1243 continue;
1246 if (id == NULL)
1247 break;
1249 err = got_object_open_as_commit(&commit, repo, id);
1250 if (err)
1251 break;
1252 entry = alloc_commit_queue_entry(commit, id);
1253 if (entry == NULL) {
1254 err = got_error_from_errno("alloc_commit_queue_entry");
1255 break;
1258 errcode = pthread_mutex_lock(&tog_mutex);
1259 if (errcode) {
1260 err = got_error_set_errno(errcode,
1261 "pthread_mutex_lock");
1262 break;
1265 entry->idx = commits->ncommits;
1266 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1267 nqueued++;
1268 commits->ncommits++;
1270 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1271 err = match_commit(&have_match, id, commit, regex);
1272 if (err) {
1273 pthread_mutex_lock(&tog_mutex);
1274 break;
1278 errcode = pthread_mutex_unlock(&tog_mutex);
1279 if (errcode && err == NULL)
1280 err = got_error_set_errno(errcode,
1281 "pthread_mutex_unlock");
1283 if (have_match)
1284 break;
1287 return err;
1290 static const struct got_error *
1291 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1292 struct got_repository *repo)
1294 const struct got_error *err = NULL;
1295 struct got_reference *head_ref;
1297 *head_id = NULL;
1299 err = got_ref_open(&head_ref, repo, branch_name, 0);
1300 if (err)
1301 return err;
1303 err = got_ref_resolve(head_id, repo, head_ref);
1304 got_ref_close(head_ref);
1305 if (err) {
1306 *head_id = NULL;
1307 return err;
1310 return NULL;
1313 static const struct got_error *
1314 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1315 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1316 struct commit_queue *commits, int selected_idx, int limit,
1317 struct got_reflist_head *refs, const char *path, int commits_needed)
1319 const struct got_error *err = NULL;
1320 struct tog_log_view_state *s = &view->state.log;
1321 struct commit_queue_entry *entry;
1322 int width;
1323 int ncommits, author_cols = 10;
1324 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1325 char *refs_str = NULL;
1326 wchar_t *wline;
1327 static const size_t date_display_cols = 9;
1329 entry = first;
1330 ncommits = 0;
1331 while (entry) {
1332 if (ncommits == selected_idx) {
1333 *selected = entry;
1334 break;
1336 entry = TAILQ_NEXT(entry, entry);
1337 ncommits++;
1340 if (*selected && !(view->searching && view->search_next_done == 0)) {
1341 err = got_object_id_str(&id_str, (*selected)->id);
1342 if (err)
1343 return err;
1344 if (refs) {
1345 err = build_refs_str(&refs_str, refs, (*selected)->id,
1346 s->repo);
1347 if (err)
1348 goto done;
1352 if (commits_needed == 0)
1353 halfdelay(10); /* disable fast refresh */
1355 if (asprintf(&ncommits_str, " [%d/%d] %s",
1356 entry ? entry->idx + 1 : 0, commits->ncommits,
1357 commits_needed > 0 ?
1358 (view->searching && view->search_next_done == 0
1359 ? "searching..." : "loading... ") :
1360 (refs_str ? refs_str : "")) == -1) {
1361 err = got_error_from_errno("asprintf");
1362 goto done;
1365 if (path && strcmp(path, "/") != 0) {
1366 if (asprintf(&header, "commit %s %s%s",
1367 id_str ? id_str : "........................................",
1368 path, ncommits_str) == -1) {
1369 err = got_error_from_errno("asprintf");
1370 header = NULL;
1371 goto done;
1373 } else if (asprintf(&header, "commit %s%s",
1374 id_str ? id_str : "........................................",
1375 ncommits_str) == -1) {
1376 err = got_error_from_errno("asprintf");
1377 header = NULL;
1378 goto done;
1380 err = format_line(&wline, &width, header, view->ncols, 0);
1381 if (err)
1382 goto done;
1384 werase(view->window);
1386 if (view_needs_focus_indication(view))
1387 wstandout(view->window);
1388 waddwstr(view->window, wline);
1389 while (width < view->ncols) {
1390 waddch(view->window, ' ');
1391 width++;
1393 if (view_needs_focus_indication(view))
1394 wstandend(view->window);
1395 free(wline);
1396 if (limit <= 1)
1397 goto done;
1399 /* Grow author column size if necessary. */
1400 entry = first;
1401 ncommits = 0;
1402 while (entry) {
1403 char *author;
1404 wchar_t *wauthor;
1405 int width;
1406 if (ncommits >= limit - 1)
1407 break;
1408 author = strdup(got_object_commit_get_author(entry->commit));
1409 if (author == NULL) {
1410 err = got_error_from_errno("strdup");
1411 goto done;
1413 err = format_author(&wauthor, &width, author, COLS,
1414 date_display_cols);
1415 if (author_cols < width)
1416 author_cols = width;
1417 free(wauthor);
1418 free(author);
1419 ncommits++;
1420 entry = TAILQ_NEXT(entry, entry);
1423 entry = first;
1424 *last = first;
1425 ncommits = 0;
1426 while (entry) {
1427 if (ncommits >= limit - 1)
1428 break;
1429 if (ncommits == selected_idx)
1430 wstandout(view->window);
1431 err = draw_commit(view, entry->commit, entry->id, refs,
1432 date_display_cols, author_cols);
1433 if (ncommits == selected_idx)
1434 wstandend(view->window);
1435 if (err)
1436 goto done;
1437 ncommits++;
1438 *last = entry;
1439 entry = TAILQ_NEXT(entry, entry);
1442 view_vborder(view);
1443 done:
1444 free(id_str);
1445 free(refs_str);
1446 free(ncommits_str);
1447 free(header);
1448 return err;
1451 static void
1452 scroll_up(struct tog_view *view,
1453 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1454 struct commit_queue *commits)
1456 struct commit_queue_entry *entry;
1457 int nscrolled = 0;
1459 entry = TAILQ_FIRST(&commits->head);
1460 if (*first_displayed_entry == entry)
1461 return;
1463 entry = *first_displayed_entry;
1464 while (entry && nscrolled < maxscroll) {
1465 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1466 if (entry) {
1467 *first_displayed_entry = entry;
1468 nscrolled++;
1473 static const struct got_error *
1474 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1475 pthread_cond_t *need_commits)
1477 int errcode;
1478 int max_wait = 20;
1480 halfdelay(1); /* fast refresh while loading commits */
1482 while (*commits_needed > 0) {
1483 if (*log_complete)
1484 break;
1486 /* Wake the log thread. */
1487 errcode = pthread_cond_signal(need_commits);
1488 if (errcode)
1489 return got_error_set_errno(errcode,
1490 "pthread_cond_signal");
1491 errcode = pthread_mutex_unlock(&tog_mutex);
1492 if (errcode)
1493 return got_error_set_errno(errcode,
1494 "pthread_mutex_unlock");
1495 pthread_yield();
1496 errcode = pthread_mutex_lock(&tog_mutex);
1497 if (errcode)
1498 return got_error_set_errno(errcode,
1499 "pthread_mutex_lock");
1501 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1503 * Thread is not done yet; lose a key press
1504 * and let the user retry... this way the GUI
1505 * remains interactive while logging deep paths
1506 * with few commits in history.
1508 return NULL;
1512 return NULL;
1515 static const struct got_error *
1516 scroll_down(struct tog_view *view,
1517 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1518 struct commit_queue_entry **last_displayed_entry,
1519 struct commit_queue *commits, int *log_complete, int *commits_needed,
1520 pthread_cond_t *need_commits)
1522 const struct got_error *err = NULL;
1523 struct commit_queue_entry *pentry;
1524 int nscrolled = 0;
1526 if (*last_displayed_entry == NULL)
1527 return NULL;
1529 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1530 if (pentry == NULL && !*log_complete) {
1532 * Ask the log thread for required amount of commits
1533 * plus some amount of pre-fetching.
1535 (*commits_needed) += maxscroll + 20;
1536 err = trigger_log_thread(0, commits_needed, log_complete,
1537 need_commits);
1538 if (err)
1539 return err;
1542 do {
1543 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1544 if (pentry == NULL)
1545 break;
1547 *last_displayed_entry = pentry;
1549 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1550 if (pentry == NULL)
1551 break;
1552 *first_displayed_entry = pentry;
1553 } while (++nscrolled < maxscroll);
1555 return err;
1558 static const struct got_error *
1559 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1560 struct got_commit_object *commit, struct got_object_id *commit_id,
1561 struct tog_view *log_view, struct got_reflist_head *refs,
1562 struct got_repository *repo)
1564 const struct got_error *err;
1565 struct got_object_qid *parent_id;
1566 struct tog_view *diff_view;
1568 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1569 if (diff_view == NULL)
1570 return got_error_from_errno("view_open");
1572 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1573 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1574 commit_id, log_view, refs, repo);
1575 if (err == NULL)
1576 *new_view = diff_view;
1577 return err;
1580 static const struct got_error *
1581 tree_view_visit_subtree(struct got_tree_object *subtree,
1582 struct tog_tree_view_state *s)
1584 struct tog_parent_tree *parent;
1586 parent = calloc(1, sizeof(*parent));
1587 if (parent == NULL)
1588 return got_error_from_errno("calloc");
1590 parent->tree = s->tree;
1591 parent->first_displayed_entry = s->first_displayed_entry;
1592 parent->selected_entry = s->selected_entry;
1593 parent->selected = s->selected;
1594 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1595 s->tree = subtree;
1596 s->entries = got_object_tree_get_entries(s->tree);
1597 s->selected = 0;
1598 s->first_displayed_entry = NULL;
1599 return NULL;
1603 static const struct got_error *
1604 browse_commit_tree(struct tog_view **new_view, int begin_x,
1605 struct commit_queue_entry *entry, const char *path,
1606 struct got_reflist_head *refs, struct got_repository *repo)
1608 const struct got_error *err = NULL;
1609 struct got_tree_object *tree;
1610 struct got_tree_entry *te;
1611 struct tog_tree_view_state *s;
1612 struct tog_view *tree_view;
1613 char *slash, *subpath = NULL;
1614 const char *p;
1616 err = got_object_open_as_tree(&tree, repo,
1617 got_object_commit_get_tree_id(entry->commit));
1618 if (err)
1619 return err;
1621 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1622 if (tree_view == NULL)
1623 return got_error_from_errno("view_open");
1625 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1626 if (err) {
1627 got_object_tree_close(tree);
1628 return err;
1630 s = &tree_view->state.tree;
1632 *new_view = tree_view;
1634 /* Walk the path and open corresponding tree objects. */
1635 p = path;
1636 while (p[0] == '/')
1637 p++;
1638 while (*p) {
1639 struct got_object_id *tree_id;
1641 /* Ensure the correct subtree entry is selected. */
1642 slash = strchr(p, '/');
1643 if (slash == NULL)
1644 slash = strchr(p, '\0');
1645 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1646 if (strncmp(p, te->name, slash - p) == 0) {
1647 s->selected_entry = te;
1648 break;
1650 s->selected++;
1652 if (s->selected_entry == NULL) {
1653 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1654 break;
1656 if (s->tree != s->root)
1657 s->selected++; /* skip '..' */
1659 if (!S_ISDIR(s->selected_entry->mode)) {
1660 /* Jump to this file's entry. */
1661 s->first_displayed_entry = s->selected_entry;
1662 s->selected = 0;
1663 break;
1666 slash = strchr(p, '/');
1667 if (slash)
1668 subpath = strndup(path, slash - path);
1669 else
1670 subpath = strdup(path);
1671 if (subpath == NULL) {
1672 err = got_error_from_errno("strdup");
1673 break;
1676 err = got_object_id_by_path(&tree_id, repo, entry->id,
1677 subpath);
1678 if (err)
1679 break;
1681 err = got_object_open_as_tree(&tree, repo, tree_id);
1682 free(tree_id);
1683 if (err)
1684 break;
1686 err = tree_view_visit_subtree(tree, s);
1687 if (err) {
1688 got_object_tree_close(tree);
1689 break;
1691 if (slash == NULL)
1692 break;
1693 free(subpath);
1694 subpath = NULL;
1695 p = slash;
1698 free(subpath);
1699 return err;
1702 static void *
1703 log_thread(void *arg)
1705 const struct got_error *err = NULL;
1706 int errcode = 0;
1707 struct tog_log_thread_args *a = arg;
1708 int done = 0;
1710 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1711 NULL, NULL);
1712 if (err)
1713 return (void *)err;
1715 while (!done && !err && !tog_sigpipe_received) {
1716 err = queue_commits(a->graph, a->commits, 1, a->repo,
1717 a->in_repo_path, a->searching, a->search_next_done,
1718 a->regex);
1719 if (err) {
1720 if (err->code != GOT_ERR_ITER_COMPLETED)
1721 return (void *)err;
1722 err = NULL;
1723 done = 1;
1724 } else if (a->commits_needed > 0)
1725 a->commits_needed--;
1727 errcode = pthread_mutex_lock(&tog_mutex);
1728 if (errcode) {
1729 err = got_error_set_errno(errcode,
1730 "pthread_mutex_lock");
1731 break;
1732 } else if (*a->quit)
1733 done = 1;
1734 else if (*a->first_displayed_entry == NULL) {
1735 *a->first_displayed_entry =
1736 TAILQ_FIRST(&a->commits->head);
1737 *a->selected_entry = *a->first_displayed_entry;
1740 if (done)
1741 a->commits_needed = 0;
1742 else if (a->commits_needed == 0) {
1743 errcode = pthread_cond_wait(&a->need_commits,
1744 &tog_mutex);
1745 if (errcode)
1746 err = got_error_set_errno(errcode,
1747 "pthread_cond_wait");
1750 errcode = pthread_mutex_unlock(&tog_mutex);
1751 if (errcode && err == NULL)
1752 err = got_error_set_errno(errcode,
1753 "pthread_mutex_unlock");
1755 a->log_complete = 1;
1756 return (void *)err;
1759 static const struct got_error *
1760 stop_log_thread(struct tog_log_view_state *s)
1762 const struct got_error *err = NULL;
1763 int errcode;
1765 if (s->thread) {
1766 s->quit = 1;
1767 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1768 if (errcode)
1769 return got_error_set_errno(errcode,
1770 "pthread_cond_signal");
1771 errcode = pthread_mutex_unlock(&tog_mutex);
1772 if (errcode)
1773 return got_error_set_errno(errcode,
1774 "pthread_mutex_unlock");
1775 errcode = pthread_join(s->thread, (void **)&err);
1776 if (errcode)
1777 return got_error_set_errno(errcode, "pthread_join");
1778 errcode = pthread_mutex_lock(&tog_mutex);
1779 if (errcode)
1780 return got_error_set_errno(errcode,
1781 "pthread_mutex_lock");
1782 s->thread = NULL;
1785 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1786 if (errcode && err == NULL)
1787 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1789 if (s->thread_args.repo) {
1790 got_repo_close(s->thread_args.repo);
1791 s->thread_args.repo = NULL;
1794 if (s->thread_args.graph) {
1795 got_commit_graph_close(s->thread_args.graph);
1796 s->thread_args.graph = NULL;
1799 return err;
1802 static const struct got_error *
1803 close_log_view(struct tog_view *view)
1805 const struct got_error *err = NULL;
1806 struct tog_log_view_state *s = &view->state.log;
1808 err = stop_log_thread(s);
1809 free_commits(&s->commits);
1810 free(s->in_repo_path);
1811 s->in_repo_path = NULL;
1812 free(s->start_id);
1813 s->start_id = NULL;
1814 return err;
1817 static const struct got_error *
1818 search_start_log_view(struct tog_view *view)
1820 struct tog_log_view_state *s = &view->state.log;
1822 s->matched_entry = NULL;
1823 s->search_entry = NULL;
1824 return NULL;
1827 static const struct got_error *
1828 search_next_log_view(struct tog_view *view)
1830 const struct got_error *err = NULL;
1831 struct tog_log_view_state *s = &view->state.log;
1832 struct commit_queue_entry *entry;
1834 if (!view->searching) {
1835 view->search_next_done = 1;
1836 return NULL;
1839 if (s->search_entry) {
1840 int errcode, ch;
1841 errcode = pthread_mutex_unlock(&tog_mutex);
1842 if (errcode)
1843 return got_error_set_errno(errcode,
1844 "pthread_mutex_unlock");
1845 ch = wgetch(view->window);
1846 errcode = pthread_mutex_lock(&tog_mutex);
1847 if (errcode)
1848 return got_error_set_errno(errcode,
1849 "pthread_mutex_lock");
1850 if (ch == KEY_BACKSPACE) {
1851 view->search_next_done = 1;
1852 return NULL;
1854 if (view->searching == TOG_SEARCH_FORWARD)
1855 entry = TAILQ_NEXT(s->search_entry, entry);
1856 else
1857 entry = TAILQ_PREV(s->search_entry,
1858 commit_queue_head, entry);
1859 } else if (s->matched_entry) {
1860 if (view->searching == TOG_SEARCH_FORWARD)
1861 entry = TAILQ_NEXT(s->selected_entry, entry);
1862 else
1863 entry = TAILQ_PREV(s->selected_entry,
1864 commit_queue_head, entry);
1865 } else {
1866 if (view->searching == TOG_SEARCH_FORWARD)
1867 entry = TAILQ_FIRST(&s->commits.head);
1868 else
1869 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1872 while (1) {
1873 int have_match = 0;
1875 if (entry == NULL) {
1876 if (s->thread_args.log_complete ||
1877 view->searching == TOG_SEARCH_BACKWARD) {
1878 view->search_next_done = 1;
1879 return NULL;
1882 * Poke the log thread for more commits and return,
1883 * allowing the main loop to make progress. Search
1884 * will resume at s->search_entry once we come back.
1886 s->thread_args.commits_needed++;
1887 return trigger_log_thread(1,
1888 &s->thread_args.commits_needed,
1889 &s->thread_args.log_complete,
1890 &s->thread_args.need_commits);
1893 err = match_commit(&have_match, entry->id, entry->commit,
1894 &view->regex);
1895 if (err)
1896 break;
1897 if (have_match) {
1898 view->search_next_done = 1;
1899 s->matched_entry = entry;
1900 break;
1903 s->search_entry = entry;
1904 if (view->searching == TOG_SEARCH_FORWARD)
1905 entry = TAILQ_NEXT(entry, entry);
1906 else
1907 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1910 if (s->matched_entry) {
1911 int cur = s->selected_entry->idx;
1912 while (cur < s->matched_entry->idx) {
1913 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1914 if (err)
1915 return err;
1916 cur++;
1918 while (cur > s->matched_entry->idx) {
1919 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1920 if (err)
1921 return err;
1922 cur--;
1926 s->search_entry = NULL;
1928 return NULL;
1931 static const struct got_error *
1932 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1933 struct got_reflist_head *refs, struct got_repository *repo,
1934 const char *head_ref_name, const char *path, int check_disk)
1936 const struct got_error *err = NULL;
1937 struct tog_log_view_state *s = &view->state.log;
1938 struct got_repository *thread_repo = NULL;
1939 struct got_commit_graph *thread_graph = NULL;
1940 int errcode;
1942 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1943 if (err != NULL)
1944 goto done;
1946 /* The commit queue only contains commits being displayed. */
1947 TAILQ_INIT(&s->commits.head);
1948 s->commits.ncommits = 0;
1950 s->refs = refs;
1951 s->repo = repo;
1952 s->head_ref_name = head_ref_name;
1953 s->start_id = got_object_id_dup(start_id);
1954 if (s->start_id == NULL) {
1955 err = got_error_from_errno("got_object_id_dup");
1956 goto done;
1959 view->show = show_log_view;
1960 view->input = input_log_view;
1961 view->close = close_log_view;
1962 view->search_start = search_start_log_view;
1963 view->search_next = search_next_log_view;
1965 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1966 if (err)
1967 goto done;
1968 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1969 0, thread_repo);
1970 if (err)
1971 goto done;
1973 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1974 if (errcode) {
1975 err = got_error_set_errno(errcode, "pthread_cond_init");
1976 goto done;
1979 s->thread_args.commits_needed = view->nlines;
1980 s->thread_args.graph = thread_graph;
1981 s->thread_args.commits = &s->commits;
1982 s->thread_args.in_repo_path = s->in_repo_path;
1983 s->thread_args.start_id = s->start_id;
1984 s->thread_args.repo = thread_repo;
1985 s->thread_args.log_complete = 0;
1986 s->thread_args.quit = &s->quit;
1987 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1988 s->thread_args.selected_entry = &s->selected_entry;
1989 s->thread_args.searching = &view->searching;
1990 s->thread_args.search_next_done = &view->search_next_done;
1991 s->thread_args.regex = &view->regex;
1992 done:
1993 if (err)
1994 close_log_view(view);
1995 return err;
1998 static const struct got_error *
1999 show_log_view(struct tog_view *view)
2001 struct tog_log_view_state *s = &view->state.log;
2003 if (s->thread == NULL) {
2004 int errcode = pthread_create(&s->thread, NULL, log_thread,
2005 &s->thread_args);
2006 if (errcode)
2007 return got_error_set_errno(errcode, "pthread_create");
2010 return draw_commits(view, &s->last_displayed_entry,
2011 &s->selected_entry, s->first_displayed_entry,
2012 &s->commits, s->selected, view->nlines, s->refs,
2013 s->in_repo_path, s->thread_args.commits_needed);
2016 static const struct got_error *
2017 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2018 struct tog_view **focus_view, struct tog_view *view, int ch)
2020 const struct got_error *err = NULL;
2021 struct tog_log_view_state *s = &view->state.log;
2022 char *parent_path, *in_repo_path = NULL;
2023 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2024 int begin_x = 0;
2025 struct got_object_id *start_id;
2027 switch (ch) {
2028 case 'q':
2029 s->quit = 1;
2030 break;
2031 case 'k':
2032 case KEY_UP:
2033 case '<':
2034 case ',':
2035 if (s->first_displayed_entry == NULL)
2036 break;
2037 if (s->selected > 0)
2038 s->selected--;
2039 else
2040 scroll_up(view, &s->first_displayed_entry, 1,
2041 &s->commits);
2042 break;
2043 case KEY_PPAGE:
2044 case CTRL('b'):
2045 if (s->first_displayed_entry == NULL)
2046 break;
2047 if (TAILQ_FIRST(&s->commits.head) ==
2048 s->first_displayed_entry) {
2049 s->selected = 0;
2050 break;
2052 scroll_up(view, &s->first_displayed_entry,
2053 view->nlines, &s->commits);
2054 break;
2055 case 'j':
2056 case KEY_DOWN:
2057 case '>':
2058 case '.':
2059 if (s->first_displayed_entry == NULL)
2060 break;
2061 if (s->selected < MIN(view->nlines - 2,
2062 s->commits.ncommits - 1)) {
2063 s->selected++;
2064 break;
2066 err = scroll_down(view, &s->first_displayed_entry, 1,
2067 &s->last_displayed_entry, &s->commits,
2068 &s->thread_args.log_complete,
2069 &s->thread_args.commits_needed,
2070 &s->thread_args.need_commits);
2071 break;
2072 case KEY_NPAGE:
2073 case CTRL('f'): {
2074 struct commit_queue_entry *first;
2075 first = s->first_displayed_entry;
2076 if (first == NULL)
2077 break;
2078 err = scroll_down(view, &s->first_displayed_entry,
2079 view->nlines, &s->last_displayed_entry,
2080 &s->commits, &s->thread_args.log_complete,
2081 &s->thread_args.commits_needed,
2082 &s->thread_args.need_commits);
2083 if (err)
2084 break;
2085 if (first == s->first_displayed_entry &&
2086 s->selected < MIN(view->nlines - 2,
2087 s->commits.ncommits - 1)) {
2088 /* can't scroll further down */
2089 s->selected = MIN(view->nlines - 2,
2090 s->commits.ncommits - 1);
2092 err = NULL;
2093 break;
2095 case KEY_RESIZE:
2096 if (s->selected > view->nlines - 2)
2097 s->selected = view->nlines - 2;
2098 if (s->selected > s->commits.ncommits - 1)
2099 s->selected = s->commits.ncommits - 1;
2100 break;
2101 case KEY_ENTER:
2102 case ' ':
2103 case '\r':
2104 if (s->selected_entry == NULL)
2105 break;
2106 if (view_is_parent_view(view))
2107 begin_x = view_split_begin_x(view->begin_x);
2108 err = open_diff_view_for_commit(&diff_view, begin_x,
2109 s->selected_entry->commit, s->selected_entry->id,
2110 view, s->refs, s->repo);
2111 if (err)
2112 break;
2113 if (view_is_parent_view(view)) {
2114 err = view_close_child(view);
2115 if (err)
2116 return err;
2117 err = view_set_child(view, diff_view);
2118 if (err) {
2119 view_close(diff_view);
2120 break;
2122 *focus_view = diff_view;
2123 view->child_focussed = 1;
2124 } else
2125 *new_view = diff_view;
2126 break;
2127 case 't':
2128 if (s->selected_entry == NULL)
2129 break;
2130 if (view_is_parent_view(view))
2131 begin_x = view_split_begin_x(view->begin_x);
2132 err = browse_commit_tree(&tree_view, begin_x,
2133 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2134 if (err)
2135 break;
2136 if (view_is_parent_view(view)) {
2137 err = view_close_child(view);
2138 if (err)
2139 return err;
2140 err = view_set_child(view, tree_view);
2141 if (err) {
2142 view_close(tree_view);
2143 break;
2145 *focus_view = tree_view;
2146 view->child_focussed = 1;
2147 } else
2148 *new_view = tree_view;
2149 break;
2150 case KEY_BACKSPACE:
2151 if (strcmp(s->in_repo_path, "/") == 0)
2152 break;
2153 parent_path = dirname(s->in_repo_path);
2154 if (parent_path && strcmp(parent_path, ".") != 0) {
2155 err = stop_log_thread(s);
2156 if (err)
2157 return err;
2158 lv = view_open(view->nlines, view->ncols,
2159 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2160 if (lv == NULL)
2161 return got_error_from_errno(
2162 "view_open");
2163 err = open_log_view(lv, s->start_id, s->refs,
2164 s->repo, s->head_ref_name, parent_path, 0);
2165 if (err)
2166 return err;;
2167 if (view_is_parent_view(view))
2168 *new_view = lv;
2169 else {
2170 view_set_child(view->parent, lv);
2171 *focus_view = lv;
2173 return NULL;
2175 break;
2176 case CTRL('l'):
2177 err = stop_log_thread(s);
2178 if (err)
2179 return err;
2180 lv = view_open(view->nlines, view->ncols,
2181 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2182 if (lv == NULL)
2183 return got_error_from_errno("view_open");
2184 err = get_head_commit_id(&start_id, s->head_ref_name ?
2185 s->head_ref_name : GOT_REF_HEAD, s->repo);
2186 if (err) {
2187 view_close(lv);
2188 return err;
2190 in_repo_path = strdup(s->in_repo_path);
2191 if (in_repo_path == NULL) {
2192 free(start_id);
2193 view_close(lv);
2194 return got_error_from_errno("strdup");
2196 got_ref_list_free(s->refs);
2197 err = got_ref_list(s->refs, s->repo, NULL,
2198 got_ref_cmp_by_name, NULL);
2199 if (err) {
2200 free(start_id);
2201 view_close(lv);
2202 return err;
2204 err = open_log_view(lv, start_id, s->refs, s->repo,
2205 s->head_ref_name, in_repo_path, 0);
2206 if (err) {
2207 free(start_id);
2208 view_close(lv);
2209 return err;;
2211 *dead_view = view;
2212 *new_view = lv;
2213 break;
2214 default:
2215 break;
2218 return err;
2221 static const struct got_error *
2222 apply_unveil(const char *repo_path, const char *worktree_path)
2224 const struct got_error *error;
2226 #ifdef PROFILE
2227 if (unveil("gmon.out", "rwc") != 0)
2228 return got_error_from_errno2("unveil", "gmon.out");
2229 #endif
2230 if (repo_path && unveil(repo_path, "r") != 0)
2231 return got_error_from_errno2("unveil", repo_path);
2233 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2234 return got_error_from_errno2("unveil", worktree_path);
2236 if (unveil("/tmp", "rwc") != 0)
2237 return got_error_from_errno2("unveil", "/tmp");
2239 error = got_privsep_unveil_exec_helpers();
2240 if (error != NULL)
2241 return error;
2243 if (unveil(NULL, NULL) != 0)
2244 return got_error_from_errno("unveil");
2246 return NULL;
2249 static void
2250 init_curses(void)
2252 initscr();
2253 cbreak();
2254 halfdelay(1); /* Do fast refresh while initial view is loading. */
2255 noecho();
2256 nonl();
2257 intrflush(stdscr, FALSE);
2258 keypad(stdscr, TRUE);
2259 curs_set(0);
2260 signal(SIGWINCH, tog_sigwinch);
2261 signal(SIGPIPE, tog_sigpipe);
2264 static const struct got_error *
2265 cmd_log(int argc, char *argv[])
2267 const struct got_error *error;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2270 struct got_reflist_head refs;
2271 struct got_object_id *start_id = NULL;
2272 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2273 char *start_commit = NULL, *head_ref_name = NULL;
2274 int ch;
2275 struct tog_view *view;
2277 SIMPLEQ_INIT(&refs);
2279 #ifndef PROFILE
2280 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2281 NULL) == -1)
2282 err(1, "pledge");
2283 #endif
2285 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2286 switch (ch) {
2287 case 'c':
2288 start_commit = optarg;
2289 break;
2290 case 'r':
2291 repo_path = realpath(optarg, NULL);
2292 if (repo_path == NULL)
2293 return got_error_from_errno2("realpath",
2294 optarg);
2295 break;
2296 default:
2297 usage_log();
2298 /* NOTREACHED */
2302 argc -= optind;
2303 argv += optind;
2305 cwd = getcwd(NULL, 0);
2306 if (cwd == NULL) {
2307 error = got_error_from_errno("getcwd");
2308 goto done;
2310 error = got_worktree_open(&worktree, cwd);
2311 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2312 goto done;
2313 error = NULL;
2315 if (argc == 0) {
2316 path = strdup("");
2317 if (path == NULL) {
2318 error = got_error_from_errno("strdup");
2319 goto done;
2321 } else if (argc == 1) {
2322 if (worktree) {
2323 error = got_worktree_resolve_path(&path, worktree,
2324 argv[0]);
2325 if (error)
2326 goto done;
2327 } else {
2328 path = strdup(argv[0]);
2329 if (path == NULL) {
2330 error = got_error_from_errno("strdup");
2331 goto done;
2334 } else
2335 usage_log();
2337 if (repo_path == NULL) {
2338 if (worktree)
2339 repo_path = strdup(
2340 got_worktree_get_repo_path(worktree));
2341 else
2342 repo_path = strdup(cwd);
2344 if (repo_path == NULL) {
2345 error = got_error_from_errno("strdup");
2346 goto done;
2349 init_curses();
2351 error = got_repo_open(&repo, repo_path, NULL);
2352 if (error != NULL)
2353 goto done;
2355 error = apply_unveil(got_repo_get_path(repo),
2356 worktree ? got_worktree_get_root_path(worktree) : NULL);
2357 if (error)
2358 goto done;
2360 if (start_commit == NULL)
2361 error = get_head_commit_id(&start_id, worktree ?
2362 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2363 repo);
2364 else {
2365 error = get_head_commit_id(&start_id, start_commit, repo);
2366 if (error) {
2367 if (error->code != GOT_ERR_NOT_REF)
2368 goto done;
2369 error = got_repo_match_object_id_prefix(&start_id,
2370 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2373 if (error != NULL)
2374 goto done;
2376 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2377 if (error)
2378 goto done;
2380 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2381 if (view == NULL) {
2382 error = got_error_from_errno("view_open");
2383 goto done;
2385 if (worktree) {
2386 head_ref_name = strdup(
2387 got_worktree_get_head_ref_name(worktree));
2388 if (head_ref_name == NULL) {
2389 error = got_error_from_errno("strdup");
2390 goto done;
2393 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2394 path, 1);
2395 if (error)
2396 goto done;
2397 if (worktree) {
2398 /* Release work tree lock. */
2399 got_worktree_close(worktree);
2400 worktree = NULL;
2402 error = view_loop(view);
2403 done:
2404 free(repo_path);
2405 free(cwd);
2406 free(path);
2407 free(start_id);
2408 free(head_ref_name);
2409 if (repo)
2410 got_repo_close(repo);
2411 if (worktree)
2412 got_worktree_close(worktree);
2413 got_ref_list_free(&refs);
2414 return error;
2417 __dead static void
2418 usage_diff(void)
2420 endwin();
2421 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2422 getprogname());
2423 exit(1);
2426 static char *
2427 parse_next_line(FILE *f, size_t *len)
2429 char *line;
2430 size_t linelen;
2431 size_t lineno;
2432 const char delim[3] = { '\0', '\0', '\0'};
2434 line = fparseln(f, &linelen, &lineno, delim, 0);
2435 if (len)
2436 *len = linelen;
2437 return line;
2440 static const struct got_error *
2441 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2442 int *last_displayed_line, int *eof, int max_lines,
2443 char *header)
2445 const struct got_error *err;
2446 int nlines = 0, nprinted = 0;
2447 char *line;
2448 size_t len;
2449 wchar_t *wline;
2450 int width;
2452 rewind(f);
2453 werase(view->window);
2455 if (header) {
2456 err = format_line(&wline, &width, header, view->ncols, 0);
2457 if (err) {
2458 return err;
2461 if (view_needs_focus_indication(view))
2462 wstandout(view->window);
2463 waddwstr(view->window, wline);
2464 if (view_needs_focus_indication(view))
2465 wstandend(view->window);
2466 if (width <= view->ncols - 1)
2467 waddch(view->window, '\n');
2469 if (max_lines <= 1)
2470 return NULL;
2471 max_lines--;
2474 *eof = 0;
2475 while (nprinted < max_lines) {
2476 line = parse_next_line(f, &len);
2477 if (line == NULL) {
2478 *eof = 1;
2479 break;
2481 if (++nlines < *first_displayed_line) {
2482 free(line);
2483 continue;
2486 err = format_line(&wline, &width, line, view->ncols, 0);
2487 if (err) {
2488 free(line);
2489 return err;
2491 waddwstr(view->window, wline);
2492 if (width <= view->ncols - 1)
2493 waddch(view->window, '\n');
2494 if (++nprinted == 1)
2495 *first_displayed_line = nlines;
2496 free(line);
2497 free(wline);
2498 wline = NULL;
2500 *last_displayed_line = nlines;
2502 view_vborder(view);
2504 if (*eof) {
2505 while (nprinted < view->nlines) {
2506 waddch(view->window, '\n');
2507 nprinted++;
2510 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2511 if (err) {
2512 return err;
2515 wstandout(view->window);
2516 waddwstr(view->window, wline);
2517 wstandend(view->window);
2520 return NULL;
2523 static char *
2524 get_datestr(time_t *time, char *datebuf)
2526 struct tm mytm, *tm;
2527 char *p, *s;
2529 tm = gmtime_r(time, &mytm);
2530 if (tm == NULL)
2531 return NULL;
2532 s = asctime_r(tm, datebuf);
2533 if (s == NULL)
2534 return NULL;
2535 p = strchr(s, '\n');
2536 if (p)
2537 *p = '\0';
2538 return s;
2541 static const struct got_error *
2542 write_commit_info(struct got_object_id *commit_id,
2543 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2545 const struct got_error *err = NULL;
2546 char datebuf[26], *datestr;
2547 struct got_commit_object *commit;
2548 char *id_str = NULL, *logmsg = NULL;
2549 time_t committer_time;
2550 const char *author, *committer;
2551 char *refs_str = NULL;
2553 if (refs) {
2554 err = build_refs_str(&refs_str, refs, commit_id, repo);
2555 if (err)
2556 return err;
2559 err = got_object_open_as_commit(&commit, repo, commit_id);
2560 if (err)
2561 return err;
2563 err = got_object_id_str(&id_str, commit_id);
2564 if (err) {
2565 err = got_error_from_errno("got_object_id_str");
2566 goto done;
2569 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2570 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2571 err = got_error_from_errno("fprintf");
2572 goto done;
2574 if (fprintf(outfile, "from: %s\n",
2575 got_object_commit_get_author(commit)) < 0) {
2576 err = got_error_from_errno("fprintf");
2577 goto done;
2579 committer_time = got_object_commit_get_committer_time(commit);
2580 datestr = get_datestr(&committer_time, datebuf);
2581 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2582 err = got_error_from_errno("fprintf");
2583 goto done;
2585 author = got_object_commit_get_author(commit);
2586 committer = got_object_commit_get_committer(commit);
2587 if (strcmp(author, committer) != 0 &&
2588 fprintf(outfile, "via: %s\n", committer) < 0) {
2589 err = got_error_from_errno("fprintf");
2590 goto done;
2592 err = got_object_commit_get_logmsg(&logmsg, commit);
2593 if (err)
2594 goto done;
2595 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2596 err = got_error_from_errno("fprintf");
2597 goto done;
2599 done:
2600 free(id_str);
2601 free(logmsg);
2602 free(refs_str);
2603 got_object_commit_close(commit);
2604 return err;
2607 static const struct got_error *
2608 create_diff(struct tog_diff_view_state *s)
2610 const struct got_error *err = NULL;
2611 FILE *f = NULL;
2612 int obj_type;
2614 f = got_opentemp();
2615 if (f == NULL) {
2616 err = got_error_from_errno("got_opentemp");
2617 goto done;
2619 if (s->f && fclose(s->f) != 0) {
2620 err = got_error_from_errno("fclose");
2621 goto done;
2623 s->f = f;
2625 if (s->id1)
2626 err = got_object_get_type(&obj_type, s->repo, s->id1);
2627 else
2628 err = got_object_get_type(&obj_type, s->repo, s->id2);
2629 if (err)
2630 goto done;
2632 switch (obj_type) {
2633 case GOT_OBJ_TYPE_BLOB:
2634 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2635 s->diff_context, 0, s->repo, f);
2636 break;
2637 case GOT_OBJ_TYPE_TREE:
2638 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2639 s->diff_context, 0, s->repo, f);
2640 break;
2641 case GOT_OBJ_TYPE_COMMIT: {
2642 const struct got_object_id_queue *parent_ids;
2643 struct got_object_qid *pid;
2644 struct got_commit_object *commit2;
2646 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2647 if (err)
2648 break;
2649 /* Show commit info if we're diffing to a parent/root commit. */
2650 if (s->id1 == NULL)
2651 write_commit_info(s->id2, s->refs, s->repo, f);
2652 else {
2653 parent_ids = got_object_commit_get_parent_ids(commit2);
2654 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2655 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2656 write_commit_info(s->id2, s->refs,
2657 s->repo, f);
2658 break;
2662 got_object_commit_close(commit2);
2664 err = got_diff_objects_as_commits(s->id1, s->id2,
2665 s->diff_context, 0, s->repo, f);
2666 break;
2668 default:
2669 err = got_error(GOT_ERR_OBJ_TYPE);
2670 break;
2672 done:
2673 if (f && fflush(f) != 0 && err == NULL)
2674 err = got_error_from_errno("fflush");
2675 return err;
2678 static void
2679 diff_view_indicate_progress(struct tog_view *view)
2681 mvwaddstr(view->window, 0, 0, "diffing...");
2682 update_panels();
2683 doupdate();
2686 static const struct got_error *
2687 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2688 struct got_object_id *id2, struct tog_view *log_view,
2689 struct got_reflist_head *refs, struct got_repository *repo)
2691 const struct got_error *err;
2693 if (id1 != NULL && id2 != NULL) {
2694 int type1, type2;
2695 err = got_object_get_type(&type1, repo, id1);
2696 if (err)
2697 return err;
2698 err = got_object_get_type(&type2, repo, id2);
2699 if (err)
2700 return err;
2702 if (type1 != type2)
2703 return got_error(GOT_ERR_OBJ_TYPE);
2706 if (id1) {
2707 view->state.diff.id1 = got_object_id_dup(id1);
2708 if (view->state.diff.id1 == NULL)
2709 return got_error_from_errno("got_object_id_dup");
2710 } else
2711 view->state.diff.id1 = NULL;
2713 view->state.diff.id2 = got_object_id_dup(id2);
2714 if (view->state.diff.id2 == NULL) {
2715 free(view->state.diff.id1);
2716 view->state.diff.id1 = NULL;
2717 return got_error_from_errno("got_object_id_dup");
2719 view->state.diff.f = NULL;
2720 view->state.diff.first_displayed_line = 1;
2721 view->state.diff.last_displayed_line = view->nlines;
2722 view->state.diff.diff_context = 3;
2723 view->state.diff.log_view = log_view;
2724 view->state.diff.repo = repo;
2725 view->state.diff.refs = refs;
2727 if (log_view && view_is_splitscreen(view))
2728 show_log_view(log_view); /* draw vborder */
2729 diff_view_indicate_progress(view);
2731 err = create_diff(&view->state.diff);
2732 if (err) {
2733 free(view->state.diff.id1);
2734 view->state.diff.id1 = NULL;
2735 free(view->state.diff.id2);
2736 view->state.diff.id2 = NULL;
2737 return err;
2740 view->show = show_diff_view;
2741 view->input = input_diff_view;
2742 view->close = close_diff_view;
2744 return NULL;
2747 static const struct got_error *
2748 close_diff_view(struct tog_view *view)
2750 const struct got_error *err = NULL;
2752 free(view->state.diff.id1);
2753 view->state.diff.id1 = NULL;
2754 free(view->state.diff.id2);
2755 view->state.diff.id2 = NULL;
2756 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2757 err = got_error_from_errno("fclose");
2758 return err;
2761 static const struct got_error *
2762 show_diff_view(struct tog_view *view)
2764 const struct got_error *err;
2765 struct tog_diff_view_state *s = &view->state.diff;
2766 char *id_str1 = NULL, *id_str2, *header;
2768 if (s->id1) {
2769 err = got_object_id_str(&id_str1, s->id1);
2770 if (err)
2771 return err;
2773 err = got_object_id_str(&id_str2, s->id2);
2774 if (err)
2775 return err;
2777 if (asprintf(&header, "diff %s %s",
2778 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2779 err = got_error_from_errno("asprintf");
2780 free(id_str1);
2781 free(id_str2);
2782 return err;
2784 free(id_str1);
2785 free(id_str2);
2787 return draw_file(view, s->f, &s->first_displayed_line,
2788 &s->last_displayed_line, &s->eof, view->nlines,
2789 header);
2792 static const struct got_error *
2793 set_selected_commit(struct tog_diff_view_state *s,
2794 struct commit_queue_entry *entry)
2796 const struct got_error *err;
2797 const struct got_object_id_queue *parent_ids;
2798 struct got_commit_object *selected_commit;
2799 struct got_object_qid *pid;
2801 free(s->id2);
2802 s->id2 = got_object_id_dup(entry->id);
2803 if (s->id2 == NULL)
2804 return got_error_from_errno("got_object_id_dup");
2806 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2807 if (err)
2808 return err;
2809 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2810 free(s->id1);
2811 pid = SIMPLEQ_FIRST(parent_ids);
2812 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2813 got_object_commit_close(selected_commit);
2814 return NULL;
2817 static const struct got_error *
2818 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2819 struct tog_view **focus_view, struct tog_view *view, int ch)
2821 const struct got_error *err = NULL;
2822 struct tog_diff_view_state *s = &view->state.diff;
2823 struct tog_log_view_state *ls;
2824 struct commit_queue_entry *entry;
2825 int i;
2827 switch (ch) {
2828 case 'k':
2829 case KEY_UP:
2830 if (s->first_displayed_line > 1)
2831 s->first_displayed_line--;
2832 break;
2833 case KEY_PPAGE:
2834 case CTRL('b'):
2835 if (s->first_displayed_line == 1)
2836 break;
2837 i = 0;
2838 while (i++ < view->nlines - 1 &&
2839 s->first_displayed_line > 1)
2840 s->first_displayed_line--;
2841 break;
2842 case 'j':
2843 case KEY_DOWN:
2844 if (!s->eof)
2845 s->first_displayed_line++;
2846 break;
2847 case KEY_NPAGE:
2848 case CTRL('f'):
2849 case ' ':
2850 if (s->eof)
2851 break;
2852 i = 0;
2853 while (!s->eof && i++ < view->nlines - 1) {
2854 char *line;
2855 line = parse_next_line(s->f, NULL);
2856 s->first_displayed_line++;
2857 if (line == NULL)
2858 break;
2860 break;
2861 case '[':
2862 if (s->diff_context > 0) {
2863 s->diff_context--;
2864 diff_view_indicate_progress(view);
2865 err = create_diff(s);
2867 break;
2868 case ']':
2869 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2870 s->diff_context++;
2871 diff_view_indicate_progress(view);
2872 err = create_diff(s);
2874 break;
2875 case '<':
2876 case ',':
2877 if (s->log_view == NULL)
2878 break;
2879 ls = &s->log_view->state.log;
2880 entry = TAILQ_PREV(ls->selected_entry,
2881 commit_queue_head, entry);
2882 if (entry == NULL)
2883 break;
2885 err = input_log_view(NULL, NULL, NULL, s->log_view,
2886 KEY_UP);
2887 if (err)
2888 break;
2890 err = set_selected_commit(s, entry);
2891 if (err)
2892 break;
2894 s->first_displayed_line = 1;
2895 s->last_displayed_line = view->nlines;
2897 diff_view_indicate_progress(view);
2898 err = create_diff(s);
2899 break;
2900 case '>':
2901 case '.':
2902 if (s->log_view == NULL)
2903 break;
2904 ls = &s->log_view->state.log;
2906 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2907 ls->thread_args.commits_needed++;
2909 /* Display "loading..." in log view. */
2910 show_log_view(s->log_view);
2911 update_panels();
2912 doupdate();
2914 err = trigger_log_thread(1 /* load_all */,
2915 &ls->thread_args.commits_needed,
2916 &ls->thread_args.log_complete,
2917 &ls->thread_args.need_commits);
2918 if (err)
2919 break;
2921 err = input_log_view(NULL, NULL, NULL, s->log_view,
2922 KEY_DOWN);
2923 if (err)
2924 break;
2926 entry = TAILQ_NEXT(ls->selected_entry, entry);
2927 if (entry == NULL)
2928 break;
2930 err = set_selected_commit(s, entry);
2931 if (err)
2932 break;
2934 s->first_displayed_line = 1;
2935 s->last_displayed_line = view->nlines;
2937 diff_view_indicate_progress(view);
2938 err = create_diff(s);
2939 break;
2940 default:
2941 break;
2944 return err;
2947 static const struct got_error *
2948 cmd_diff(int argc, char *argv[])
2950 const struct got_error *error = NULL;
2951 struct got_repository *repo = NULL;
2952 struct got_reflist_head refs;
2953 struct got_object_id *id1 = NULL, *id2 = NULL;
2954 char *repo_path = NULL;
2955 char *id_str1 = NULL, *id_str2 = NULL;
2956 int ch;
2957 struct tog_view *view;
2959 SIMPLEQ_INIT(&refs);
2961 #ifndef PROFILE
2962 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2963 NULL) == -1)
2964 err(1, "pledge");
2965 #endif
2967 while ((ch = getopt(argc, argv, "")) != -1) {
2968 switch (ch) {
2969 default:
2970 usage_diff();
2971 /* NOTREACHED */
2975 argc -= optind;
2976 argv += optind;
2978 if (argc == 0) {
2979 usage_diff(); /* TODO show local worktree changes */
2980 } else if (argc == 2) {
2981 repo_path = getcwd(NULL, 0);
2982 if (repo_path == NULL)
2983 return got_error_from_errno("getcwd");
2984 id_str1 = argv[0];
2985 id_str2 = argv[1];
2986 } else if (argc == 3) {
2987 repo_path = realpath(argv[0], NULL);
2988 if (repo_path == NULL)
2989 return got_error_from_errno2("realpath", argv[0]);
2990 id_str1 = argv[1];
2991 id_str2 = argv[2];
2992 } else
2993 usage_diff();
2995 init_curses();
2997 error = got_repo_open(&repo, repo_path, NULL);
2998 if (error)
2999 goto done;
3001 error = apply_unveil(got_repo_get_path(repo), NULL);
3002 if (error)
3003 goto done;
3005 error = got_repo_match_object_id_prefix(&id1, id_str1,
3006 GOT_OBJ_TYPE_ANY, repo);
3007 if (error)
3008 goto done;
3010 error = got_repo_match_object_id_prefix(&id2, id_str2,
3011 GOT_OBJ_TYPE_ANY, repo);
3012 if (error)
3013 goto done;
3015 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3016 if (error)
3017 goto done;
3019 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3020 if (view == NULL) {
3021 error = got_error_from_errno("view_open");
3022 goto done;
3024 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3025 if (error)
3026 goto done;
3027 error = view_loop(view);
3028 done:
3029 free(repo_path);
3030 if (repo)
3031 got_repo_close(repo);
3032 got_ref_list_free(&refs);
3033 return error;
3036 __dead static void
3037 usage_blame(void)
3039 endwin();
3040 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3041 getprogname());
3042 exit(1);
3045 struct tog_blame_line {
3046 int annotated;
3047 struct got_object_id *id;
3050 static const struct got_error *
3051 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3052 const char *path, struct tog_blame_line *lines, int nlines,
3053 int blame_complete, int selected_line, int *first_displayed_line,
3054 int *last_displayed_line, int *eof, int max_lines)
3056 const struct got_error *err;
3057 int lineno = 0, nprinted = 0;
3058 char *line;
3059 size_t len;
3060 wchar_t *wline;
3061 int width;
3062 struct tog_blame_line *blame_line;
3063 struct got_object_id *prev_id = NULL;
3064 char *id_str;
3066 err = got_object_id_str(&id_str, id);
3067 if (err)
3068 return err;
3070 rewind(f);
3071 werase(view->window);
3073 if (asprintf(&line, "commit %s", id_str) == -1) {
3074 err = got_error_from_errno("asprintf");
3075 free(id_str);
3076 return err;
3079 err = format_line(&wline, &width, line, view->ncols, 0);
3080 free(line);
3081 line = NULL;
3082 if (err)
3083 return err;
3084 if (view_needs_focus_indication(view))
3085 wstandout(view->window);
3086 waddwstr(view->window, wline);
3087 if (view_needs_focus_indication(view))
3088 wstandend(view->window);
3089 free(wline);
3090 wline = NULL;
3091 if (width < view->ncols - 1)
3092 waddch(view->window, '\n');
3094 if (asprintf(&line, "[%d/%d] %s%s",
3095 *first_displayed_line - 1 + selected_line, nlines,
3096 blame_complete ? "" : "annotating... ", path) == -1) {
3097 free(id_str);
3098 return got_error_from_errno("asprintf");
3100 free(id_str);
3101 err = format_line(&wline, &width, line, view->ncols, 0);
3102 free(line);
3103 line = NULL;
3104 if (err)
3105 return err;
3106 waddwstr(view->window, wline);
3107 free(wline);
3108 wline = NULL;
3109 if (width < view->ncols - 1)
3110 waddch(view->window, '\n');
3112 *eof = 0;
3113 while (nprinted < max_lines - 2) {
3114 line = parse_next_line(f, &len);
3115 if (line == NULL) {
3116 *eof = 1;
3117 break;
3119 if (++lineno < *first_displayed_line) {
3120 free(line);
3121 continue;
3124 if (view->ncols <= 9) {
3125 width = 9;
3126 wline = wcsdup(L"");
3127 if (wline == NULL)
3128 err = got_error_from_errno("wcsdup");
3129 } else {
3130 err = format_line(&wline, &width, line,
3131 view->ncols - 9, 9);
3132 width += 9;
3134 if (err) {
3135 free(line);
3136 return err;
3139 if (view->focussed && nprinted == selected_line - 1)
3140 wstandout(view->window);
3142 if (nlines > 0) {
3143 blame_line = &lines[lineno - 1];
3144 if (blame_line->annotated && prev_id &&
3145 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3146 !(view->focussed &&
3147 nprinted == selected_line - 1)) {
3148 waddstr(view->window, " ");
3149 } else if (blame_line->annotated) {
3150 char *id_str;
3151 err = got_object_id_str(&id_str, blame_line->id);
3152 if (err) {
3153 free(line);
3154 free(wline);
3155 return err;
3157 wprintw(view->window, "%.8s", id_str);
3158 free(id_str);
3159 prev_id = blame_line->id;
3160 } else {
3161 waddstr(view->window, "........");
3162 prev_id = NULL;
3164 } else {
3165 waddstr(view->window, "........");
3166 prev_id = NULL;
3169 if (view->focussed && nprinted == selected_line - 1)
3170 wstandend(view->window);
3171 waddstr(view->window, " ");
3173 waddwstr(view->window, wline);
3174 if (width <= view->ncols - 1)
3175 waddch(view->window, '\n');
3176 if (++nprinted == 1)
3177 *first_displayed_line = lineno;
3178 free(line);
3179 free(wline);
3180 wline = NULL;
3182 *last_displayed_line = lineno;
3184 view_vborder(view);
3186 return NULL;
3189 static const struct got_error *
3190 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3192 const struct got_error *err = NULL;
3193 struct tog_blame_cb_args *a = arg;
3194 struct tog_blame_line *line;
3195 int errcode;
3197 if (nlines != a->nlines ||
3198 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3199 return got_error(GOT_ERR_RANGE);
3201 errcode = pthread_mutex_lock(&tog_mutex);
3202 if (errcode)
3203 return got_error_set_errno(errcode, "pthread_mutex_lock");
3205 if (*a->quit) { /* user has quit the blame view */
3206 err = got_error(GOT_ERR_ITER_COMPLETED);
3207 goto done;
3210 if (lineno == -1)
3211 goto done; /* no change in this commit */
3213 line = &a->lines[lineno - 1];
3214 if (line->annotated)
3215 goto done;
3217 line->id = got_object_id_dup(id);
3218 if (line->id == NULL) {
3219 err = got_error_from_errno("got_object_id_dup");
3220 goto done;
3222 line->annotated = 1;
3223 done:
3224 errcode = pthread_mutex_unlock(&tog_mutex);
3225 if (errcode)
3226 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3227 return err;
3230 static void *
3231 blame_thread(void *arg)
3233 const struct got_error *err;
3234 struct tog_blame_thread_args *ta = arg;
3235 struct tog_blame_cb_args *a = ta->cb_args;
3236 int errcode;
3238 err = got_blame(ta->path, a->commit_id, ta->repo,
3239 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3240 if (err && err->code == GOT_ERR_CANCELLED)
3241 err = NULL;
3243 errcode = pthread_mutex_lock(&tog_mutex);
3244 if (errcode)
3245 return (void *)got_error_set_errno(errcode,
3246 "pthread_mutex_lock");
3248 got_repo_close(ta->repo);
3249 ta->repo = NULL;
3250 *ta->complete = 1;
3252 errcode = pthread_mutex_unlock(&tog_mutex);
3253 if (errcode && err == NULL)
3254 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3256 return (void *)err;
3259 static struct got_object_id *
3260 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3261 int first_displayed_line, int selected_line)
3263 struct tog_blame_line *line;
3265 if (nlines <= 0)
3266 return NULL;
3268 line = &lines[first_displayed_line - 1 + selected_line - 1];
3269 if (!line->annotated)
3270 return NULL;
3272 return line->id;
3275 static const struct got_error *
3276 stop_blame(struct tog_blame *blame)
3278 const struct got_error *err = NULL;
3279 int i;
3281 if (blame->thread) {
3282 int errcode;
3283 errcode = pthread_mutex_unlock(&tog_mutex);
3284 if (errcode)
3285 return got_error_set_errno(errcode,
3286 "pthread_mutex_unlock");
3287 errcode = pthread_join(blame->thread, (void **)&err);
3288 if (errcode)
3289 return got_error_set_errno(errcode, "pthread_join");
3290 errcode = pthread_mutex_lock(&tog_mutex);
3291 if (errcode)
3292 return got_error_set_errno(errcode,
3293 "pthread_mutex_lock");
3294 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3295 err = NULL;
3296 blame->thread = NULL;
3298 if (blame->thread_args.repo) {
3299 got_repo_close(blame->thread_args.repo);
3300 blame->thread_args.repo = NULL;
3302 if (blame->f) {
3303 if (fclose(blame->f) != 0 && err == NULL)
3304 err = got_error_from_errno("fclose");
3305 blame->f = NULL;
3307 if (blame->lines) {
3308 for (i = 0; i < blame->nlines; i++)
3309 free(blame->lines[i].id);
3310 free(blame->lines);
3311 blame->lines = NULL;
3313 free(blame->cb_args.commit_id);
3314 blame->cb_args.commit_id = NULL;
3316 return err;
3319 static const struct got_error *
3320 cancel_blame_view(void *arg)
3322 const struct got_error *err = NULL;
3323 int *done = arg;
3324 int errcode;
3326 errcode = pthread_mutex_lock(&tog_mutex);
3327 if (errcode)
3328 return got_error_set_errno(errcode,
3329 "pthread_mutex_unlock");
3331 if (*done)
3332 err = got_error(GOT_ERR_CANCELLED);
3334 errcode = pthread_mutex_unlock(&tog_mutex);
3335 if (errcode)
3336 return got_error_set_errno(errcode,
3337 "pthread_mutex_lock");
3339 return err;
3342 static const struct got_error *
3343 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3344 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3345 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3346 struct got_repository *repo)
3348 const struct got_error *err = NULL;
3349 struct got_blob_object *blob = NULL;
3350 struct got_repository *thread_repo = NULL;
3351 struct got_object_id *obj_id = NULL;
3352 int obj_type;
3354 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3355 if (err)
3356 return err;
3357 if (obj_id == NULL)
3358 return got_error(GOT_ERR_NO_OBJ);
3360 err = got_object_get_type(&obj_type, repo, obj_id);
3361 if (err)
3362 goto done;
3364 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 err = got_error(GOT_ERR_OBJ_TYPE);
3366 goto done;
3369 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3370 if (err)
3371 goto done;
3372 blame->f = got_opentemp();
3373 if (blame->f == NULL) {
3374 err = got_error_from_errno("got_opentemp");
3375 goto done;
3377 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3378 &blame->line_offsets, blame->f, blob);
3379 if (err || blame->nlines == 0)
3380 goto done;
3382 /* Don't include \n at EOF in the blame line count. */
3383 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3384 blame->nlines--;
3386 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3387 if (blame->lines == NULL) {
3388 err = got_error_from_errno("calloc");
3389 goto done;
3392 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3393 if (err)
3394 goto done;
3396 blame->cb_args.view = view;
3397 blame->cb_args.lines = blame->lines;
3398 blame->cb_args.nlines = blame->nlines;
3399 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3400 if (blame->cb_args.commit_id == NULL) {
3401 err = got_error_from_errno("got_object_id_dup");
3402 goto done;
3404 blame->cb_args.quit = done;
3406 blame->thread_args.path = path;
3407 blame->thread_args.repo = thread_repo;
3408 blame->thread_args.cb_args = &blame->cb_args;
3409 blame->thread_args.complete = blame_complete;
3410 blame->thread_args.cancel_cb = cancel_blame_view;
3411 blame->thread_args.cancel_arg = done;
3412 *blame_complete = 0;
3414 done:
3415 if (blob)
3416 got_object_blob_close(blob);
3417 free(obj_id);
3418 if (err)
3419 stop_blame(blame);
3420 return err;
3423 static const struct got_error *
3424 open_blame_view(struct tog_view *view, char *path,
3425 struct got_object_id *commit_id, struct got_reflist_head *refs,
3426 struct got_repository *repo)
3428 const struct got_error *err = NULL;
3429 struct tog_blame_view_state *s = &view->state.blame;
3431 SIMPLEQ_INIT(&s->blamed_commits);
3433 s->path = strdup(path);
3434 if (s->path == NULL)
3435 return got_error_from_errno("strdup");
3437 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3438 if (err) {
3439 free(s->path);
3440 return err;
3443 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3444 s->first_displayed_line = 1;
3445 s->last_displayed_line = view->nlines;
3446 s->selected_line = 1;
3447 s->blame_complete = 0;
3448 s->repo = repo;
3449 s->refs = refs;
3450 s->commit_id = commit_id;
3451 memset(&s->blame, 0, sizeof(s->blame));
3453 view->show = show_blame_view;
3454 view->input = input_blame_view;
3455 view->close = close_blame_view;
3456 view->search_start = search_start_blame_view;
3457 view->search_next = search_next_blame_view;
3459 return run_blame(&s->blame, view, &s->blame_complete,
3460 &s->first_displayed_line, &s->last_displayed_line,
3461 &s->selected_line, &s->done, &s->eof, s->path,
3462 s->blamed_commit->id, s->repo);
3465 static const struct got_error *
3466 close_blame_view(struct tog_view *view)
3468 const struct got_error *err = NULL;
3469 struct tog_blame_view_state *s = &view->state.blame;
3471 if (s->blame.thread)
3472 err = stop_blame(&s->blame);
3474 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3475 struct got_object_qid *blamed_commit;
3476 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3477 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3478 got_object_qid_free(blamed_commit);
3481 free(s->path);
3483 return err;
3486 static const struct got_error *
3487 search_start_blame_view(struct tog_view *view)
3489 struct tog_blame_view_state *s = &view->state.blame;
3491 s->matched_line = 0;
3492 return NULL;
3495 static int
3496 match_line(const char *line, regex_t *regex)
3498 regmatch_t regmatch;
3500 return regexec(regex, line, 1, &regmatch, 0) == 0;
3504 static const struct got_error *
3505 search_next_blame_view(struct tog_view *view)
3507 struct tog_blame_view_state *s = &view->state.blame;
3508 int lineno;
3510 if (!view->searching) {
3511 view->search_next_done = 1;
3512 return NULL;
3515 if (s->matched_line) {
3516 if (view->searching == TOG_SEARCH_FORWARD)
3517 lineno = s->matched_line + 1;
3518 else
3519 lineno = s->matched_line - 1;
3520 } else {
3521 if (view->searching == TOG_SEARCH_FORWARD)
3522 lineno = 1;
3523 else
3524 lineno = s->blame.nlines;
3527 while (1) {
3528 char *line = NULL;
3529 off_t offset;
3530 size_t len;
3532 if (lineno <= 0 || lineno > s->blame.nlines) {
3533 if (s->matched_line == 0) {
3534 view->search_next_done = 1;
3535 free(line);
3536 break;
3539 if (view->searching == TOG_SEARCH_FORWARD)
3540 lineno = 1;
3541 else
3542 lineno = s->blame.nlines;
3545 offset = s->blame.line_offsets[lineno - 1];
3546 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3547 free(line);
3548 return got_error_from_errno("fseeko");
3550 free(line);
3551 line = parse_next_line(s->blame.f, &len);
3552 if (line && match_line(line, &view->regex)) {
3553 view->search_next_done = 1;
3554 s->matched_line = lineno;
3555 free(line);
3556 break;
3558 free(line);
3559 if (view->searching == TOG_SEARCH_FORWARD)
3560 lineno++;
3561 else
3562 lineno--;
3565 if (s->matched_line) {
3566 s->first_displayed_line = s->matched_line;
3567 s->selected_line = 1;
3570 return NULL;
3573 static const struct got_error *
3574 show_blame_view(struct tog_view *view)
3576 const struct got_error *err = NULL;
3577 struct tog_blame_view_state *s = &view->state.blame;
3578 int errcode;
3580 if (s->blame.thread == NULL) {
3581 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3582 &s->blame.thread_args);
3583 if (errcode)
3584 return got_error_set_errno(errcode, "pthread_create");
3586 halfdelay(1); /* fast refresh while annotating */
3589 if (s->blame_complete)
3590 halfdelay(10); /* disable fast refresh */
3592 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3593 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3594 s->selected_line, &s->first_displayed_line,
3595 &s->last_displayed_line, &s->eof, view->nlines);
3597 view_vborder(view);
3598 return err;
3601 static const struct got_error *
3602 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3603 struct tog_view **focus_view, struct tog_view *view, int ch)
3605 const struct got_error *err = NULL, *thread_err = NULL;
3606 struct tog_view *diff_view;
3607 struct tog_blame_view_state *s = &view->state.blame;
3608 int begin_x = 0;
3610 switch (ch) {
3611 case 'q':
3612 s->done = 1;
3613 break;
3614 case 'k':
3615 case KEY_UP:
3616 if (s->selected_line > 1)
3617 s->selected_line--;
3618 else if (s->selected_line == 1 &&
3619 s->first_displayed_line > 1)
3620 s->first_displayed_line--;
3621 break;
3622 case KEY_PPAGE:
3623 if (s->first_displayed_line == 1) {
3624 s->selected_line = 1;
3625 break;
3627 if (s->first_displayed_line > view->nlines - 2)
3628 s->first_displayed_line -=
3629 (view->nlines - 2);
3630 else
3631 s->first_displayed_line = 1;
3632 break;
3633 case 'j':
3634 case KEY_DOWN:
3635 if (s->selected_line < view->nlines - 2 &&
3636 s->first_displayed_line +
3637 s->selected_line <= s->blame.nlines)
3638 s->selected_line++;
3639 else if (s->last_displayed_line <
3640 s->blame.nlines)
3641 s->first_displayed_line++;
3642 break;
3643 case 'b':
3644 case 'p': {
3645 struct got_object_id *id = NULL;
3646 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3647 s->first_displayed_line, s->selected_line);
3648 if (id == NULL)
3649 break;
3650 if (ch == 'p') {
3651 struct got_commit_object *commit;
3652 struct got_object_qid *pid;
3653 struct got_object_id *blob_id = NULL;
3654 int obj_type;
3655 err = got_object_open_as_commit(&commit,
3656 s->repo, id);
3657 if (err)
3658 break;
3659 pid = SIMPLEQ_FIRST(
3660 got_object_commit_get_parent_ids(commit));
3661 if (pid == NULL) {
3662 got_object_commit_close(commit);
3663 break;
3665 /* Check if path history ends here. */
3666 err = got_object_id_by_path(&blob_id, s->repo,
3667 pid->id, s->path);
3668 if (err) {
3669 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3670 err = NULL;
3671 got_object_commit_close(commit);
3672 break;
3674 err = got_object_get_type(&obj_type, s->repo,
3675 blob_id);
3676 free(blob_id);
3677 /* Can't blame non-blob type objects. */
3678 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3679 got_object_commit_close(commit);
3680 break;
3682 err = got_object_qid_alloc(&s->blamed_commit,
3683 pid->id);
3684 got_object_commit_close(commit);
3685 } else {
3686 if (got_object_id_cmp(id,
3687 s->blamed_commit->id) == 0)
3688 break;
3689 err = got_object_qid_alloc(&s->blamed_commit,
3690 id);
3692 if (err)
3693 break;
3694 s->done = 1;
3695 thread_err = stop_blame(&s->blame);
3696 s->done = 0;
3697 if (thread_err)
3698 break;
3699 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3700 s->blamed_commit, entry);
3701 err = run_blame(&s->blame, view, &s->blame_complete,
3702 &s->first_displayed_line, &s->last_displayed_line,
3703 &s->selected_line, &s->done, &s->eof,
3704 s->path, s->blamed_commit->id, s->repo);
3705 if (err)
3706 break;
3707 break;
3709 case 'B': {
3710 struct got_object_qid *first;
3711 first = SIMPLEQ_FIRST(&s->blamed_commits);
3712 if (!got_object_id_cmp(first->id, s->commit_id))
3713 break;
3714 s->done = 1;
3715 thread_err = stop_blame(&s->blame);
3716 s->done = 0;
3717 if (thread_err)
3718 break;
3719 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3720 got_object_qid_free(s->blamed_commit);
3721 s->blamed_commit =
3722 SIMPLEQ_FIRST(&s->blamed_commits);
3723 err = run_blame(&s->blame, view, &s->blame_complete,
3724 &s->first_displayed_line, &s->last_displayed_line,
3725 &s->selected_line, &s->done, &s->eof, s->path,
3726 s->blamed_commit->id, s->repo);
3727 if (err)
3728 break;
3729 break;
3731 case KEY_ENTER:
3732 case '\r': {
3733 struct got_object_id *id = NULL;
3734 struct got_object_qid *pid;
3735 struct got_commit_object *commit = NULL;
3736 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3737 s->first_displayed_line, s->selected_line);
3738 if (id == NULL)
3739 break;
3740 err = got_object_open_as_commit(&commit, s->repo, id);
3741 if (err)
3742 break;
3743 pid = SIMPLEQ_FIRST(
3744 got_object_commit_get_parent_ids(commit));
3745 if (view_is_parent_view(view))
3746 begin_x = view_split_begin_x(view->begin_x);
3747 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3748 if (diff_view == NULL) {
3749 got_object_commit_close(commit);
3750 err = got_error_from_errno("view_open");
3751 break;
3753 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3754 id, NULL, s->refs, s->repo);
3755 got_object_commit_close(commit);
3756 if (err) {
3757 view_close(diff_view);
3758 break;
3760 if (view_is_parent_view(view)) {
3761 err = view_close_child(view);
3762 if (err)
3763 break;
3764 err = view_set_child(view, diff_view);
3765 if (err) {
3766 view_close(diff_view);
3767 break;
3769 *focus_view = diff_view;
3770 view->child_focussed = 1;
3771 } else
3772 *new_view = diff_view;
3773 if (err)
3774 break;
3775 break;
3777 case KEY_NPAGE:
3778 case ' ':
3779 if (s->last_displayed_line >= s->blame.nlines &&
3780 s->selected_line >= MIN(s->blame.nlines,
3781 view->nlines - 2)) {
3782 break;
3784 if (s->last_displayed_line >= s->blame.nlines &&
3785 s->selected_line < view->nlines - 2) {
3786 s->selected_line = MIN(s->blame.nlines,
3787 view->nlines - 2);
3788 break;
3790 if (s->last_displayed_line + view->nlines - 2
3791 <= s->blame.nlines)
3792 s->first_displayed_line +=
3793 view->nlines - 2;
3794 else
3795 s->first_displayed_line =
3796 s->blame.nlines -
3797 (view->nlines - 3);
3798 break;
3799 case KEY_RESIZE:
3800 if (s->selected_line > view->nlines - 2) {
3801 s->selected_line = MIN(s->blame.nlines,
3802 view->nlines - 2);
3804 break;
3805 default:
3806 break;
3808 return thread_err ? thread_err : err;
3811 static const struct got_error *
3812 cmd_blame(int argc, char *argv[])
3814 const struct got_error *error;
3815 struct got_repository *repo = NULL;
3816 struct got_reflist_head refs;
3817 struct got_worktree *worktree = NULL;
3818 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3819 struct got_object_id *commit_id = NULL;
3820 char *commit_id_str = NULL;
3821 int ch;
3822 struct tog_view *view;
3824 SIMPLEQ_INIT(&refs);
3826 #ifndef PROFILE
3827 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3828 NULL) == -1)
3829 err(1, "pledge");
3830 #endif
3832 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3833 switch (ch) {
3834 case 'c':
3835 commit_id_str = optarg;
3836 break;
3837 case 'r':
3838 repo_path = realpath(optarg, NULL);
3839 if (repo_path == NULL)
3840 return got_error_from_errno2("realpath",
3841 optarg);
3842 break;
3843 default:
3844 usage_blame();
3845 /* NOTREACHED */
3849 argc -= optind;
3850 argv += optind;
3852 if (argc == 1)
3853 path = argv[0];
3854 else
3855 usage_blame();
3857 cwd = getcwd(NULL, 0);
3858 if (cwd == NULL) {
3859 error = got_error_from_errno("getcwd");
3860 goto done;
3862 if (repo_path == NULL) {
3863 error = got_worktree_open(&worktree, cwd);
3864 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3865 goto done;
3866 else
3867 error = NULL;
3868 if (worktree) {
3869 repo_path =
3870 strdup(got_worktree_get_repo_path(worktree));
3871 if (repo_path == NULL)
3872 error = got_error_from_errno("strdup");
3873 if (error)
3874 goto done;
3875 } else {
3876 repo_path = strdup(cwd);
3877 if (repo_path == NULL) {
3878 error = got_error_from_errno("strdup");
3879 goto done;
3884 init_curses();
3886 error = got_repo_open(&repo, repo_path, NULL);
3887 if (error != NULL)
3888 goto done;
3890 error = apply_unveil(got_repo_get_path(repo), NULL);
3891 if (error)
3892 goto done;
3894 if (worktree) {
3895 const char *prefix = got_worktree_get_path_prefix(worktree);
3896 char *p, *worktree_subdir = cwd +
3897 strlen(got_worktree_get_root_path(worktree));
3898 if (asprintf(&p, "%s%s%s%s%s",
3899 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3900 worktree_subdir, worktree_subdir[0] ? "/" : "",
3901 path) == -1) {
3902 error = got_error_from_errno("asprintf");
3903 goto done;
3905 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3906 free(p);
3907 } else {
3908 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3910 if (error)
3911 goto done;
3913 if (commit_id_str == NULL) {
3914 struct got_reference *head_ref;
3915 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3916 if (error != NULL)
3917 goto done;
3918 error = got_ref_resolve(&commit_id, repo, head_ref);
3919 got_ref_close(head_ref);
3920 } else {
3921 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3922 if (error) {
3923 if (error->code != GOT_ERR_NOT_REF)
3924 goto done;
3925 error = got_repo_match_object_id_prefix(&commit_id,
3926 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3929 if (error != NULL)
3930 goto done;
3932 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3933 if (error)
3934 goto done;
3936 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3937 if (view == NULL) {
3938 error = got_error_from_errno("view_open");
3939 goto done;
3941 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3942 if (error)
3943 goto done;
3944 if (worktree) {
3945 /* Release work tree lock. */
3946 got_worktree_close(worktree);
3947 worktree = NULL;
3949 error = view_loop(view);
3950 done:
3951 free(repo_path);
3952 free(cwd);
3953 free(commit_id);
3954 if (worktree)
3955 got_worktree_close(worktree);
3956 if (repo)
3957 got_repo_close(repo);
3958 got_ref_list_free(&refs);
3959 return error;
3962 static const struct got_error *
3963 draw_tree_entries(struct tog_view *view,
3964 struct got_tree_entry **first_displayed_entry,
3965 struct got_tree_entry **last_displayed_entry,
3966 struct got_tree_entry **selected_entry, int *ndisplayed,
3967 const char *label, int show_ids, const char *parent_path,
3968 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3970 const struct got_error *err = NULL;
3971 struct got_tree_entry *te;
3972 wchar_t *wline;
3973 int width, n;
3975 *ndisplayed = 0;
3977 werase(view->window);
3979 if (limit == 0)
3980 return NULL;
3982 err = format_line(&wline, &width, label, view->ncols, 0);
3983 if (err)
3984 return err;
3985 if (view_needs_focus_indication(view))
3986 wstandout(view->window);
3987 waddwstr(view->window, wline);
3988 if (view_needs_focus_indication(view))
3989 wstandend(view->window);
3990 free(wline);
3991 wline = NULL;
3992 if (width < view->ncols - 1)
3993 waddch(view->window, '\n');
3994 if (--limit <= 0)
3995 return NULL;
3996 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3997 if (err)
3998 return err;
3999 waddwstr(view->window, wline);
4000 free(wline);
4001 wline = NULL;
4002 if (width < view->ncols - 1)
4003 waddch(view->window, '\n');
4004 if (--limit <= 0)
4005 return NULL;
4006 waddch(view->window, '\n');
4007 if (--limit <= 0)
4008 return NULL;
4010 te = SIMPLEQ_FIRST(&entries->head);
4011 if (*first_displayed_entry == NULL) {
4012 if (selected == 0) {
4013 if (view->focussed)
4014 wstandout(view->window);
4015 *selected_entry = NULL;
4017 waddstr(view->window, " ..\n"); /* parent directory */
4018 if (selected == 0 && view->focussed)
4019 wstandend(view->window);
4020 (*ndisplayed)++;
4021 if (--limit <= 0)
4022 return NULL;
4023 n = 1;
4024 } else {
4025 n = 0;
4026 while (te != *first_displayed_entry)
4027 te = SIMPLEQ_NEXT(te, entry);
4030 while (te) {
4031 char *line = NULL, *id_str = NULL;
4032 const char *modestr = "";
4034 if (show_ids) {
4035 err = got_object_id_str(&id_str, te->id);
4036 if (err)
4037 return got_error_from_errno(
4038 "got_object_id_str");
4040 if (got_object_tree_entry_is_submodule(te))
4041 modestr = "$";
4042 else if (S_ISLNK(te->mode))
4043 modestr = "@";
4044 else if (S_ISDIR(te->mode))
4045 modestr = "/";
4046 else if (te->mode & S_IXUSR)
4047 modestr = "*";
4048 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4049 te->name, modestr) == -1) {
4050 free(id_str);
4051 return got_error_from_errno("asprintf");
4053 free(id_str);
4054 err = format_line(&wline, &width, line, view->ncols, 0);
4055 if (err) {
4056 free(line);
4057 break;
4059 if (n == selected) {
4060 if (view->focussed)
4061 wstandout(view->window);
4062 *selected_entry = te;
4064 waddwstr(view->window, wline);
4065 if (width < view->ncols - 1)
4066 waddch(view->window, '\n');
4067 if (n == selected && view->focussed)
4068 wstandend(view->window);
4069 free(line);
4070 free(wline);
4071 wline = NULL;
4072 n++;
4073 (*ndisplayed)++;
4074 *last_displayed_entry = te;
4075 if (--limit <= 0)
4076 break;
4077 te = SIMPLEQ_NEXT(te, entry);
4080 return err;
4083 static void
4084 tree_scroll_up(struct tog_view *view,
4085 struct got_tree_entry **first_displayed_entry, int maxscroll,
4086 const struct got_tree_entries *entries, int isroot)
4088 struct got_tree_entry *te, *prev;
4089 int i;
4091 if (*first_displayed_entry == NULL)
4092 return;
4094 te = SIMPLEQ_FIRST(&entries->head);
4095 if (*first_displayed_entry == te) {
4096 if (!isroot)
4097 *first_displayed_entry = NULL;
4098 return;
4101 /* XXX this is stupid... switch to TAILQ? */
4102 for (i = 0; i < maxscroll; i++) {
4103 while (te != *first_displayed_entry) {
4104 prev = te;
4105 te = SIMPLEQ_NEXT(te, entry);
4107 *first_displayed_entry = prev;
4108 te = SIMPLEQ_FIRST(&entries->head);
4110 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4111 *first_displayed_entry = NULL;
4114 static int
4115 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4116 struct got_tree_entry *last_displayed_entry,
4117 const struct got_tree_entries *entries)
4119 struct got_tree_entry *next, *last;
4120 int n = 0;
4122 if (*first_displayed_entry)
4123 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4124 else
4125 next = SIMPLEQ_FIRST(&entries->head);
4126 last = last_displayed_entry;
4127 while (next && last && n++ < maxscroll) {
4128 last = SIMPLEQ_NEXT(last, entry);
4129 if (last) {
4130 *first_displayed_entry = next;
4131 next = SIMPLEQ_NEXT(next, entry);
4134 return n;
4137 static const struct got_error *
4138 tree_entry_path(char **path, struct tog_parent_trees *parents,
4139 struct got_tree_entry *te)
4141 const struct got_error *err = NULL;
4142 struct tog_parent_tree *pt;
4143 size_t len = 2; /* for leading slash and NUL */
4145 TAILQ_FOREACH(pt, parents, entry)
4146 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4147 if (te)
4148 len += strlen(te->name);
4150 *path = calloc(1, len);
4151 if (path == NULL)
4152 return got_error_from_errno("calloc");
4154 (*path)[0] = '/';
4155 pt = TAILQ_LAST(parents, tog_parent_trees);
4156 while (pt) {
4157 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4158 err = got_error(GOT_ERR_NO_SPACE);
4159 goto done;
4161 if (strlcat(*path, "/", len) >= len) {
4162 err = got_error(GOT_ERR_NO_SPACE);
4163 goto done;
4165 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4167 if (te) {
4168 if (strlcat(*path, te->name, len) >= len) {
4169 err = got_error(GOT_ERR_NO_SPACE);
4170 goto done;
4173 done:
4174 if (err) {
4175 free(*path);
4176 *path = NULL;
4178 return err;
4181 static const struct got_error *
4182 blame_tree_entry(struct tog_view **new_view, int begin_x,
4183 struct got_tree_entry *te, struct tog_parent_trees *parents,
4184 struct got_object_id *commit_id, struct got_reflist_head *refs,
4185 struct got_repository *repo)
4187 const struct got_error *err = NULL;
4188 char *path;
4189 struct tog_view *blame_view;
4191 *new_view = NULL;
4193 err = tree_entry_path(&path, parents, te);
4194 if (err)
4195 return err;
4197 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4198 if (blame_view == NULL) {
4199 err = got_error_from_errno("view_open");
4200 goto done;
4203 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4204 if (err) {
4205 if (err->code == GOT_ERR_CANCELLED)
4206 err = NULL;
4207 view_close(blame_view);
4208 } else
4209 *new_view = blame_view;
4210 done:
4211 free(path);
4212 return err;
4215 static const struct got_error *
4216 log_tree_entry(struct tog_view **new_view, int begin_x,
4217 struct got_tree_entry *te, struct tog_parent_trees *parents,
4218 struct got_object_id *commit_id, struct got_reflist_head *refs,
4219 struct got_repository *repo)
4221 struct tog_view *log_view;
4222 const struct got_error *err = NULL;
4223 char *path;
4225 *new_view = NULL;
4227 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4228 if (log_view == NULL)
4229 return got_error_from_errno("view_open");
4231 err = tree_entry_path(&path, parents, te);
4232 if (err)
4233 return err;
4235 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4236 if (err)
4237 view_close(log_view);
4238 else
4239 *new_view = log_view;
4240 free(path);
4241 return err;
4244 static const struct got_error *
4245 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4246 struct got_object_id *commit_id, struct got_reflist_head *refs,
4247 struct got_repository *repo)
4249 const struct got_error *err = NULL;
4250 char *commit_id_str = NULL;
4251 struct tog_tree_view_state *s = &view->state.tree;
4253 TAILQ_INIT(&s->parents);
4255 err = got_object_id_str(&commit_id_str, commit_id);
4256 if (err != NULL)
4257 goto done;
4259 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4260 err = got_error_from_errno("asprintf");
4261 goto done;
4264 s->root = s->tree = root;
4265 s->entries = got_object_tree_get_entries(root);
4266 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4267 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4268 s->commit_id = got_object_id_dup(commit_id);
4269 if (s->commit_id == NULL) {
4270 err = got_error_from_errno("got_object_id_dup");
4271 goto done;
4273 s->refs = refs;
4274 s->repo = repo;
4276 view->show = show_tree_view;
4277 view->input = input_tree_view;
4278 view->close = close_tree_view;
4279 view->search_start = search_start_tree_view;
4280 view->search_next = search_next_tree_view;
4281 done:
4282 free(commit_id_str);
4283 if (err) {
4284 free(s->tree_label);
4285 s->tree_label = NULL;
4287 return err;
4290 static const struct got_error *
4291 close_tree_view(struct tog_view *view)
4293 struct tog_tree_view_state *s = &view->state.tree;
4295 free(s->tree_label);
4296 s->tree_label = NULL;
4297 free(s->commit_id);
4298 s->commit_id = NULL;
4299 while (!TAILQ_EMPTY(&s->parents)) {
4300 struct tog_parent_tree *parent;
4301 parent = TAILQ_FIRST(&s->parents);
4302 TAILQ_REMOVE(&s->parents, parent, entry);
4303 free(parent);
4306 if (s->tree != s->root)
4307 got_object_tree_close(s->tree);
4308 got_object_tree_close(s->root);
4310 return NULL;
4313 static const struct got_error *
4314 search_start_tree_view(struct tog_view *view)
4316 struct tog_tree_view_state *s = &view->state.tree;
4318 s->matched_entry = NULL;
4319 return NULL;
4322 static int
4323 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4325 regmatch_t regmatch;
4327 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4330 static const struct got_error *
4331 search_next_tree_view(struct tog_view *view)
4333 struct tog_tree_view_state *s = &view->state.tree;
4334 struct got_tree_entry *entry = NULL, *te;
4336 if (!view->searching) {
4337 view->search_next_done = 1;
4338 return NULL;
4341 if (s->matched_entry) {
4342 if (view->searching == TOG_SEARCH_FORWARD) {
4343 if (s->selected_entry)
4344 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4345 else
4346 entry = SIMPLEQ_FIRST(&s->entries->head);
4348 else {
4349 if (s->selected_entry == NULL) {
4350 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4351 entry = te;
4352 } else {
4353 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4354 entry = te;
4355 if (SIMPLEQ_NEXT(te, entry) ==
4356 s->selected_entry)
4357 break;
4361 } else {
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 while (1) {
4371 if (entry == NULL) {
4372 if (s->matched_entry == NULL) {
4373 view->search_next_done = 1;
4374 return NULL;
4376 if (view->searching == TOG_SEARCH_FORWARD)
4377 entry = SIMPLEQ_FIRST(&s->entries->head);
4378 else {
4379 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4380 entry = te;
4384 if (match_tree_entry(entry, &view->regex)) {
4385 view->search_next_done = 1;
4386 s->matched_entry = entry;
4387 break;
4390 if (view->searching == TOG_SEARCH_FORWARD)
4391 entry = SIMPLEQ_NEXT(entry, entry);
4392 else {
4393 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4394 entry = NULL;
4395 else {
4396 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4397 if (SIMPLEQ_NEXT(te, entry) == entry) {
4398 entry = te;
4399 break;
4406 if (s->matched_entry) {
4407 s->first_displayed_entry = s->matched_entry;
4408 s->selected = 0;
4411 return NULL;
4414 static const struct got_error *
4415 show_tree_view(struct tog_view *view)
4417 const struct got_error *err = NULL;
4418 struct tog_tree_view_state *s = &view->state.tree;
4419 char *parent_path;
4421 err = tree_entry_path(&parent_path, &s->parents, NULL);
4422 if (err)
4423 return err;
4425 err = draw_tree_entries(view, &s->first_displayed_entry,
4426 &s->last_displayed_entry, &s->selected_entry,
4427 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4428 s->entries, s->selected, view->nlines, s->tree == s->root);
4429 free(parent_path);
4431 view_vborder(view);
4432 return err;
4435 static const struct got_error *
4436 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4437 struct tog_view **focus_view, struct tog_view *view, int ch)
4439 const struct got_error *err = NULL;
4440 struct tog_tree_view_state *s = &view->state.tree;
4441 struct tog_view *log_view;
4442 int begin_x = 0, nscrolled;
4444 switch (ch) {
4445 case 'i':
4446 s->show_ids = !s->show_ids;
4447 break;
4448 case 'l':
4449 if (!s->selected_entry)
4450 break;
4451 if (view_is_parent_view(view))
4452 begin_x = view_split_begin_x(view->begin_x);
4453 err = log_tree_entry(&log_view, begin_x,
4454 s->selected_entry, &s->parents,
4455 s->commit_id, s->refs, s->repo);
4456 if (view_is_parent_view(view)) {
4457 err = view_close_child(view);
4458 if (err)
4459 return err;
4460 err = view_set_child(view, log_view);
4461 if (err) {
4462 view_close(log_view);
4463 break;
4465 *focus_view = log_view;
4466 view->child_focussed = 1;
4467 } else
4468 *new_view = log_view;
4469 break;
4470 case 'k':
4471 case KEY_UP:
4472 if (s->selected > 0) {
4473 s->selected--;
4474 if (s->selected == 0)
4475 break;
4477 if (s->selected > 0)
4478 break;
4479 tree_scroll_up(view, &s->first_displayed_entry, 1,
4480 s->entries, s->tree == s->root);
4481 break;
4482 case KEY_PPAGE:
4483 tree_scroll_up(view, &s->first_displayed_entry,
4484 MAX(0, view->nlines - 4 - s->selected), s->entries,
4485 s->tree == s->root);
4486 s->selected = 0;
4487 if (SIMPLEQ_FIRST(&s->entries->head) ==
4488 s->first_displayed_entry && s->tree != s->root)
4489 s->first_displayed_entry = NULL;
4490 break;
4491 case 'j':
4492 case KEY_DOWN:
4493 if (s->selected < s->ndisplayed - 1) {
4494 s->selected++;
4495 break;
4497 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4498 /* can't scroll any further */
4499 break;
4500 tree_scroll_down(&s->first_displayed_entry, 1,
4501 s->last_displayed_entry, s->entries);
4502 break;
4503 case KEY_NPAGE:
4504 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4505 == NULL) {
4506 /* can't scroll any further; move cursor down */
4507 if (s->selected < s->ndisplayed - 1)
4508 s->selected = s->ndisplayed - 1;
4509 break;
4511 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4512 view->nlines, s->last_displayed_entry, s->entries);
4513 if (nscrolled < view->nlines) {
4514 int ndisplayed = 0;
4515 struct got_tree_entry *te;
4516 te = s->first_displayed_entry;
4517 do {
4518 ndisplayed++;
4519 te = SIMPLEQ_NEXT(te, entry);
4520 } while (te);
4521 s->selected = ndisplayed - 1;
4523 break;
4524 case KEY_ENTER:
4525 case '\r':
4526 case KEY_BACKSPACE:
4527 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4528 struct tog_parent_tree *parent;
4529 /* user selected '..' */
4530 if (s->tree == s->root)
4531 break;
4532 parent = TAILQ_FIRST(&s->parents);
4533 TAILQ_REMOVE(&s->parents, parent,
4534 entry);
4535 got_object_tree_close(s->tree);
4536 s->tree = parent->tree;
4537 s->entries =
4538 got_object_tree_get_entries(s->tree);
4539 s->first_displayed_entry =
4540 parent->first_displayed_entry;
4541 s->selected_entry =
4542 parent->selected_entry;
4543 s->selected = parent->selected;
4544 free(parent);
4545 } else if (S_ISDIR(s->selected_entry->mode)) {
4546 struct got_tree_object *subtree;
4547 err = got_object_open_as_tree(&subtree,
4548 s->repo, s->selected_entry->id);
4549 if (err)
4550 break;
4551 err = tree_view_visit_subtree(subtree, s);
4552 if (err) {
4553 got_object_tree_close(subtree);
4554 break;
4556 } else if (S_ISREG(s->selected_entry->mode)) {
4557 struct tog_view *blame_view;
4558 int begin_x = view_is_parent_view(view) ?
4559 view_split_begin_x(view->begin_x) : 0;
4561 err = blame_tree_entry(&blame_view, begin_x,
4562 s->selected_entry, &s->parents,
4563 s->commit_id, s->refs, s->repo);
4564 if (err)
4565 break;
4566 if (view_is_parent_view(view)) {
4567 err = view_close_child(view);
4568 if (err)
4569 return err;
4570 err = view_set_child(view, blame_view);
4571 if (err) {
4572 view_close(blame_view);
4573 break;
4575 *focus_view = blame_view;
4576 view->child_focussed = 1;
4577 } else
4578 *new_view = blame_view;
4580 break;
4581 case KEY_RESIZE:
4582 if (s->selected > view->nlines)
4583 s->selected = s->ndisplayed - 1;
4584 break;
4585 default:
4586 break;
4589 return err;
4592 __dead static void
4593 usage_tree(void)
4595 endwin();
4596 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4597 getprogname());
4598 exit(1);
4601 static const struct got_error *
4602 cmd_tree(int argc, char *argv[])
4604 const struct got_error *error;
4605 struct got_repository *repo = NULL;
4606 struct got_reflist_head refs;
4607 char *repo_path = NULL;
4608 struct got_object_id *commit_id = NULL;
4609 char *commit_id_arg = NULL;
4610 struct got_commit_object *commit = NULL;
4611 struct got_tree_object *tree = NULL;
4612 int ch;
4613 struct tog_view *view;
4615 SIMPLEQ_INIT(&refs);
4617 #ifndef PROFILE
4618 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4619 NULL) == -1)
4620 err(1, "pledge");
4621 #endif
4623 while ((ch = getopt(argc, argv, "c:")) != -1) {
4624 switch (ch) {
4625 case 'c':
4626 commit_id_arg = optarg;
4627 break;
4628 default:
4629 usage_tree();
4630 /* NOTREACHED */
4634 argc -= optind;
4635 argv += optind;
4637 if (argc == 0) {
4638 struct got_worktree *worktree;
4639 char *cwd = getcwd(NULL, 0);
4640 if (cwd == NULL)
4641 return got_error_from_errno("getcwd");
4642 error = got_worktree_open(&worktree, cwd);
4643 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4644 goto done;
4645 if (worktree) {
4646 free(cwd);
4647 repo_path =
4648 strdup(got_worktree_get_repo_path(worktree));
4649 got_worktree_close(worktree);
4650 } else
4651 repo_path = cwd;
4652 if (repo_path == NULL) {
4653 error = got_error_from_errno("strdup");
4654 goto done;
4656 } else if (argc == 1) {
4657 repo_path = realpath(argv[0], NULL);
4658 if (repo_path == NULL)
4659 return got_error_from_errno2("realpath", argv[0]);
4660 } else
4661 usage_log();
4663 init_curses();
4665 error = got_repo_open(&repo, repo_path, NULL);
4666 if (error != NULL)
4667 goto done;
4669 error = apply_unveil(got_repo_get_path(repo), NULL);
4670 if (error)
4671 goto done;
4673 if (commit_id_arg == NULL)
4674 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4675 else {
4676 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4677 if (error) {
4678 if (error->code != GOT_ERR_NOT_REF)
4679 goto done;
4680 error = got_repo_match_object_id_prefix(&commit_id,
4681 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4684 if (error != NULL)
4685 goto done;
4687 error = got_object_open_as_commit(&commit, repo, commit_id);
4688 if (error != NULL)
4689 goto done;
4691 error = got_object_open_as_tree(&tree, repo,
4692 got_object_commit_get_tree_id(commit));
4693 if (error != NULL)
4694 goto done;
4696 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4697 if (error)
4698 goto done;
4700 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4701 if (view == NULL) {
4702 error = got_error_from_errno("view_open");
4703 goto done;
4705 error = open_tree_view(view, tree, commit_id, &refs, repo);
4706 if (error)
4707 goto done;
4708 error = view_loop(view);
4709 done:
4710 free(repo_path);
4711 free(commit_id);
4712 if (commit)
4713 got_object_commit_close(commit);
4714 if (tree)
4715 got_object_tree_close(tree);
4716 if (repo)
4717 got_repo_close(repo);
4718 got_ref_list_free(&refs);
4719 return error;
4722 static void
4723 list_commands(void)
4725 int i;
4727 fprintf(stderr, "commands:");
4728 for (i = 0; i < nitems(tog_commands); i++) {
4729 struct tog_cmd *cmd = &tog_commands[i];
4730 fprintf(stderr, " %s", cmd->name);
4732 fputc('\n', stderr);
4735 __dead static void
4736 usage(int hflag)
4738 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4739 getprogname());
4740 if (hflag)
4741 list_commands();
4742 exit(1);
4745 static char **
4746 make_argv(const char *arg0, const char *arg1)
4748 char **argv;
4749 int argc = (arg1 == NULL ? 1 : 2);
4751 argv = calloc(argc, sizeof(char *));
4752 if (argv == NULL)
4753 err(1, "calloc");
4754 argv[0] = strdup(arg0);
4755 if (argv[0] == NULL)
4756 err(1, "strdup");
4757 if (arg1) {
4758 argv[1] = strdup(arg1);
4759 if (argv[1] == NULL)
4760 err(1, "strdup");
4763 return argv;
4766 int
4767 main(int argc, char *argv[])
4769 const struct got_error *error = NULL;
4770 struct tog_cmd *cmd = NULL;
4771 int ch, hflag = 0, Vflag = 0;
4772 char **cmd_argv = NULL;
4774 setlocale(LC_CTYPE, "");
4776 while ((ch = getopt(argc, argv, "hV")) != -1) {
4777 switch (ch) {
4778 case 'h':
4779 hflag = 1;
4780 break;
4781 case 'V':
4782 Vflag = 1;
4783 break;
4784 default:
4785 usage(hflag);
4786 /* NOTREACHED */
4790 argc -= optind;
4791 argv += optind;
4792 optind = 0;
4793 optreset = 1;
4795 if (Vflag) {
4796 got_version_print_str();
4797 return 1;
4800 if (argc == 0) {
4801 if (hflag)
4802 usage(hflag);
4803 /* Build an argument vector which runs a default command. */
4804 cmd = &tog_commands[0];
4805 cmd_argv = make_argv(cmd->name, NULL);
4806 argc = 1;
4807 } else {
4808 int i;
4810 /* Did the user specific a command? */
4811 for (i = 0; i < nitems(tog_commands); i++) {
4812 if (strncmp(tog_commands[i].name, argv[0],
4813 strlen(argv[0])) == 0) {
4814 cmd = &tog_commands[i];
4815 break;
4819 if (cmd == NULL) {
4820 fprintf(stderr, "%s: unknown command '%s'\n",
4821 getprogname(), argv[0]);
4822 list_commands();
4823 return 1;
4827 if (hflag)
4828 cmd->cmd_usage();
4829 else
4830 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4832 endwin();
4833 free(cmd_argv);
4834 if (error && error->code != GOT_ERR_CANCELLED)
4835 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4836 return 0;