Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_diff_view_state {
116 struct got_object_id *id1, *id2;
117 FILE *f;
118 int first_displayed_line;
119 int last_displayed_line;
120 int eof;
121 int diff_context;
122 struct got_repository *repo;
123 struct got_reflist_head *refs;
125 /* passed from log view; may be NULL */
126 struct tog_view *log_view;
127 };
129 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
131 struct tog_log_thread_args {
132 pthread_cond_t need_commits;
133 int commits_needed;
134 struct got_commit_graph *graph;
135 struct commit_queue *commits;
136 const char *in_repo_path;
137 struct got_object_id *start_id;
138 struct got_repository *repo;
139 int log_complete;
140 sig_atomic_t *quit;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 int *searching;
144 int *search_next_done;
145 regex_t *regex;
146 };
148 struct tog_log_view_state {
149 struct commit_queue commits;
150 struct commit_queue_entry *first_displayed_entry;
151 struct commit_queue_entry *last_displayed_entry;
152 struct commit_queue_entry *selected_entry;
153 int selected;
154 char *in_repo_path;
155 const char *head_ref_name;
156 struct got_repository *repo;
157 struct got_reflist_head *refs;
158 struct got_object_id *start_id;
159 sig_atomic_t quit;
160 pthread_t thread;
161 struct tog_log_thread_args thread_args;
162 struct commit_queue_entry *matched_entry;
163 struct commit_queue_entry *search_entry;
164 };
166 struct tog_blame_cb_args {
167 struct tog_blame_line *lines; /* one per line */
168 int nlines;
170 struct tog_view *view;
171 struct got_object_id *commit_id;
172 int *quit;
173 };
175 struct tog_blame_thread_args {
176 const char *path;
177 struct got_repository *repo;
178 struct tog_blame_cb_args *cb_args;
179 int *complete;
180 got_cancel_cb cancel_cb;
181 void *cancel_arg;
182 };
184 struct tog_blame {
185 FILE *f;
186 size_t filesize;
187 struct tog_blame_line *lines;
188 int nlines;
189 off_t *line_offsets;
190 pthread_t thread;
191 struct tog_blame_thread_args thread_args;
192 struct tog_blame_cb_args cb_args;
193 const char *path;
194 };
196 struct tog_blame_view_state {
197 int first_displayed_line;
198 int last_displayed_line;
199 int selected_line;
200 int blame_complete;
201 int eof;
202 int done;
203 struct got_object_id_queue blamed_commits;
204 struct got_object_qid *blamed_commit;
205 char *path;
206 struct got_repository *repo;
207 struct got_reflist_head *refs;
208 struct got_object_id *commit_id;
209 struct tog_blame blame;
210 int matched_line;
211 };
213 struct tog_parent_tree {
214 TAILQ_ENTRY(tog_parent_tree) entry;
215 struct got_tree_object *tree;
216 struct got_tree_entry *first_displayed_entry;
217 struct got_tree_entry *selected_entry;
218 int selected;
219 };
221 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
223 struct tog_tree_view_state {
224 char *tree_label;
225 struct got_tree_object *root;
226 struct got_tree_object *tree;
227 const struct got_tree_entries *entries;
228 struct got_tree_entry *first_displayed_entry;
229 struct got_tree_entry *last_displayed_entry;
230 struct got_tree_entry *selected_entry;
231 int ndisplayed, selected, show_ids;
232 struct tog_parent_trees parents;
233 struct got_object_id *commit_id;
234 struct got_repository *repo;
235 struct got_reflist_head *refs;
236 struct got_tree_entry *matched_entry;
237 };
239 /*
240 * We implement two types of views: parent views and child views.
242 * The 'Tab' key switches between a parent view and its child view.
243 * Child views are shown side-by-side to their parent view, provided
244 * there is enough screen estate.
246 * When a new view is opened from within a parent view, this new view
247 * becomes a child view of the parent view, replacing any existing child.
249 * When a new view is opened from within a child view, this new view
250 * becomes a parent view which will obscure the views below until the
251 * user quits the new parent view by typing 'q'.
253 * This list of views contains parent views only.
254 * Child views are only pointed to by their parent view.
255 */
256 TAILQ_HEAD(tog_view_list_head, tog_view);
258 struct tog_view {
259 TAILQ_ENTRY(tog_view) entry;
260 WINDOW *window;
261 PANEL *panel;
262 int nlines, ncols, begin_y, begin_x;
263 int lines, cols; /* copies of LINES and COLS */
264 int focussed;
265 struct tog_view *parent;
266 struct tog_view *child;
267 int child_focussed;
269 /* type-specific state */
270 enum tog_view_type type;
271 union {
272 struct tog_diff_view_state diff;
273 struct tog_log_view_state log;
274 struct tog_blame_view_state blame;
275 struct tog_tree_view_state tree;
276 } state;
278 const struct got_error *(*show)(struct tog_view *);
279 const struct got_error *(*input)(struct tog_view **,
280 struct tog_view **, struct tog_view**, struct tog_view *, int);
281 const struct got_error *(*close)(struct tog_view *);
283 const struct got_error *(*search_start)(struct tog_view *);
284 const struct got_error *(*search_next)(struct tog_view *);
285 int searching;
286 #define TOG_SEARCH_FORWARD 1
287 #define TOG_SEARCH_BACKWARD 2
288 int search_next_done;
289 regex_t regex;
290 };
292 static const struct got_error *open_diff_view(struct tog_view *,
293 struct got_object_id *, struct got_object_id *, struct tog_view *,
294 struct got_reflist_head *, struct got_repository *);
295 static const struct got_error *show_diff_view(struct tog_view *);
296 static const struct got_error *input_diff_view(struct tog_view **,
297 struct tog_view **, struct tog_view **, struct tog_view *, int);
298 static const struct got_error* close_diff_view(struct tog_view *);
300 static const struct got_error *open_log_view(struct tog_view *,
301 struct got_object_id *, struct got_reflist_head *,
302 struct got_repository *, const char *, const char *, int);
303 static const struct got_error * show_log_view(struct tog_view *);
304 static const struct got_error *input_log_view(struct tog_view **,
305 struct tog_view **, struct tog_view **, struct tog_view *, int);
306 static const struct got_error *close_log_view(struct tog_view *);
307 static const struct got_error *search_start_log_view(struct tog_view *);
308 static const struct got_error *search_next_log_view(struct tog_view *);
310 static const struct got_error *open_blame_view(struct tog_view *, char *,
311 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
312 static const struct got_error *show_blame_view(struct tog_view *);
313 static const struct got_error *input_blame_view(struct tog_view **,
314 struct tog_view **, struct tog_view **, struct tog_view *, int);
315 static const struct got_error *close_blame_view(struct tog_view *);
316 static const struct got_error *search_start_blame_view(struct tog_view *);
317 static const struct got_error *search_next_blame_view(struct tog_view *);
319 static const struct got_error *open_tree_view(struct tog_view *,
320 struct got_tree_object *, struct got_object_id *,
321 struct got_reflist_head *, struct got_repository *);
322 static const struct got_error *show_tree_view(struct tog_view *);
323 static const struct got_error *input_tree_view(struct tog_view **,
324 struct tog_view **, struct tog_view **, struct tog_view *, int);
325 static const struct got_error *close_tree_view(struct tog_view *);
326 static const struct got_error *search_start_tree_view(struct tog_view *);
327 static const struct got_error *search_next_tree_view(struct tog_view *);
329 static volatile sig_atomic_t tog_sigwinch_received;
330 static volatile sig_atomic_t tog_sigpipe_received;
332 static void
333 tog_sigwinch(int signo)
335 tog_sigwinch_received = 1;
338 static void
339 tog_sigpipe(int signo)
341 tog_sigpipe_received = 1;
344 static const struct got_error *
345 view_close(struct tog_view *view)
347 const struct got_error *err = NULL;
349 if (view->child) {
350 view_close(view->child);
351 view->child = NULL;
353 if (view->close)
354 err = view->close(view);
355 if (view->panel)
356 del_panel(view->panel);
357 if (view->window)
358 delwin(view->window);
359 free(view);
360 return err;
363 static struct tog_view *
364 view_open(int nlines, int ncols, int begin_y, int begin_x,
365 enum tog_view_type type)
367 struct tog_view *view = calloc(1, sizeof(*view));
369 if (view == NULL)
370 return NULL;
372 view->type = type;
373 view->lines = LINES;
374 view->cols = COLS;
375 view->nlines = nlines ? nlines : LINES - begin_y;
376 view->ncols = ncols ? ncols : COLS - begin_x;
377 view->begin_y = begin_y;
378 view->begin_x = begin_x;
379 view->window = newwin(nlines, ncols, begin_y, begin_x);
380 if (view->window == NULL) {
381 view_close(view);
382 return NULL;
384 view->panel = new_panel(view->window);
385 if (view->panel == NULL ||
386 set_panel_userptr(view->panel, view) != OK) {
387 view_close(view);
388 return NULL;
391 keypad(view->window, TRUE);
392 return view;
395 static int
396 view_split_begin_x(int begin_x)
398 if (begin_x > 0 || COLS < 120)
399 return 0;
400 return (COLS - MAX(COLS / 2, 80));
403 static const struct got_error *view_resize(struct tog_view *);
405 static const struct got_error *
406 view_splitscreen(struct tog_view *view)
408 const struct got_error *err = NULL;
410 view->begin_y = 0;
411 view->begin_x = view_split_begin_x(0);
412 view->nlines = LINES;
413 view->ncols = COLS - view->begin_x;
414 view->lines = LINES;
415 view->cols = COLS;
416 err = view_resize(view);
417 if (err)
418 return err;
420 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
421 return got_error_from_errno("mvwin");
423 return NULL;
426 static const struct got_error *
427 view_fullscreen(struct tog_view *view)
429 const struct got_error *err = NULL;
431 view->begin_x = 0;
432 view->begin_y = 0;
433 view->nlines = LINES;
434 view->ncols = COLS;
435 view->lines = LINES;
436 view->cols = COLS;
437 err = view_resize(view);
438 if (err)
439 return err;
441 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
442 return got_error_from_errno("mvwin");
444 return NULL;
447 static int
448 view_is_parent_view(struct tog_view *view)
450 return view->parent == NULL;
453 static const struct got_error *
454 view_resize(struct tog_view *view)
456 int nlines, ncols;
458 if (view->lines > LINES)
459 nlines = view->nlines - (view->lines - LINES);
460 else
461 nlines = view->nlines + (LINES - view->lines);
463 if (view->cols > COLS)
464 ncols = view->ncols - (view->cols - COLS);
465 else
466 ncols = view->ncols + (COLS - view->cols);
468 if (wresize(view->window, nlines, ncols) == ERR)
469 return got_error_from_errno("wresize");
470 if (replace_panel(view->panel, view->window) == ERR)
471 return got_error_from_errno("replace_panel");
472 wclear(view->window);
474 view->nlines = nlines;
475 view->ncols = ncols;
476 view->lines = LINES;
477 view->cols = COLS;
479 if (view->child) {
480 view->child->begin_x = view_split_begin_x(view->begin_x);
481 if (view->child->begin_x == 0) {
482 view_fullscreen(view->child);
483 if (view->child->focussed)
484 show_panel(view->child->panel);
485 else
486 show_panel(view->panel);
487 } else {
488 view_splitscreen(view->child);
489 show_panel(view->child->panel);
493 return NULL;
496 static const struct got_error *
497 view_close_child(struct tog_view *view)
499 const struct got_error *err = NULL;
501 if (view->child == NULL)
502 return NULL;
504 err = view_close(view->child);
505 view->child = NULL;
506 return err;
509 static const struct got_error *
510 view_set_child(struct tog_view *view, struct tog_view *child)
512 const struct got_error *err = NULL;
514 view->child = child;
515 child->parent = view;
516 return err;
519 static int
520 view_is_splitscreen(struct tog_view *view)
522 return view->begin_x > 0;
525 static void
526 tog_resizeterm(void)
528 int cols, lines;
529 struct winsize size;
531 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
532 cols = 80; /* Default */
533 lines = 24;
534 } else {
535 cols = size.ws_col;
536 lines = size.ws_row;
538 resize_term(lines, cols);
541 static const struct got_error *
542 view_search_start(struct tog_view *view)
544 const struct got_error *err = NULL;
545 char pattern[1024];
546 int ret;
547 int begin_x = 0;
549 if (view->nlines < 1)
550 return NULL;
552 if (!view_is_parent_view(view))
553 begin_x = view_split_begin_x(view->begin_x);
554 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
555 begin_x, "/");
556 wclrtoeol(view->window);
558 nocbreak();
559 echo();
560 ret = wgetnstr(view->window, pattern, sizeof(pattern));
561 cbreak();
562 noecho();
563 if (ret == ERR)
564 return NULL;
566 if (view->searching) {
567 regfree(&view->regex);
568 view->searching = 0;
571 if (regcomp(&view->regex, pattern,
572 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
573 err = view->search_start(view);
574 if (err) {
575 regfree(&view->regex);
576 return err;
578 view->searching = TOG_SEARCH_FORWARD;
579 view->search_next_done = 0;
580 view->search_next(view);
583 return NULL;
586 static const struct got_error *
587 view_input(struct tog_view **new, struct tog_view **dead,
588 struct tog_view **focus, int *done, struct tog_view *view,
589 struct tog_view_list_head *views)
591 const struct got_error *err = NULL;
592 struct tog_view *v;
593 int ch, errcode;
595 *new = NULL;
596 *dead = NULL;
597 *focus = NULL;
599 if (view->searching && !view->search_next_done) {
600 errcode = pthread_mutex_unlock(&tog_mutex);
601 if (errcode)
602 return got_error_set_errno(errcode,
603 "pthread_mutex_unlock");
604 pthread_yield();
605 errcode = pthread_mutex_lock(&tog_mutex);
606 if (errcode)
607 return got_error_set_errno(errcode,
608 "pthread_mutex_lock");
609 view->search_next(view);
610 return NULL;
613 nodelay(stdscr, FALSE);
614 /* Allow threads to make progress while we are waiting for input. */
615 errcode = pthread_mutex_unlock(&tog_mutex);
616 if (errcode)
617 return got_error_set_errno(errcode, "pthread_mutex_unlock");
618 ch = wgetch(view->window);
619 errcode = pthread_mutex_lock(&tog_mutex);
620 if (errcode)
621 return got_error_set_errno(errcode, "pthread_mutex_lock");
622 nodelay(stdscr, TRUE);
624 if (tog_sigwinch_received) {
625 tog_resizeterm();
626 tog_sigwinch_received = 0;
627 TAILQ_FOREACH(v, views, entry) {
628 err = view_resize(v);
629 if (err)
630 return err;
631 err = v->input(new, dead, focus, v, KEY_RESIZE);
632 if (err)
633 return err;
637 switch (ch) {
638 case ERR:
639 break;
640 case '\t':
641 if (view->child) {
642 *focus = view->child;
643 view->child_focussed = 1;
644 } else if (view->parent) {
645 *focus = view->parent;
646 view->parent->child_focussed = 0;
648 break;
649 case 'q':
650 err = view->input(new, dead, focus, view, ch);
651 *dead = view;
652 break;
653 case 'Q':
654 *done = 1;
655 break;
656 case 'f':
657 if (view_is_parent_view(view)) {
658 if (view->child == NULL)
659 break;
660 if (view_is_splitscreen(view->child)) {
661 *focus = view->child;
662 view->child_focussed = 1;
663 err = view_fullscreen(view->child);
664 } else
665 err = view_splitscreen(view->child);
666 if (err)
667 break;
668 err = view->child->input(new, dead, focus,
669 view->child, KEY_RESIZE);
670 } else {
671 if (view_is_splitscreen(view)) {
672 *focus = view;
673 view->parent->child_focussed = 1;
674 err = view_fullscreen(view);
675 } else {
676 err = view_splitscreen(view);
678 if (err)
679 break;
680 err = view->input(new, dead, focus, view,
681 KEY_RESIZE);
683 break;
684 case KEY_RESIZE:
685 break;
686 case '/':
687 if (view->search_start)
688 view_search_start(view);
689 else
690 err = view->input(new, dead, focus, view, ch);
691 break;
692 case 'N':
693 case 'n':
694 if (view->search_next && view->searching) {
695 view->searching = (ch == 'n' ?
696 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
697 view->search_next_done = 0;
698 view->search_next(view);
699 } else
700 err = view->input(new, dead, focus, view, ch);
701 break;
702 default:
703 err = view->input(new, dead, focus, view, ch);
704 break;
707 return err;
710 void
711 view_vborder(struct tog_view *view)
713 PANEL *panel;
714 struct tog_view *view_above;
716 if (view->parent)
717 return view_vborder(view->parent);
719 panel = panel_above(view->panel);
720 if (panel == NULL)
721 return;
723 view_above = panel_userptr(panel);
724 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
725 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
728 int
729 view_needs_focus_indication(struct tog_view *view)
731 if (view_is_parent_view(view)) {
732 if (view->child == NULL || view->child_focussed)
733 return 0;
734 if (!view_is_splitscreen(view->child))
735 return 0;
736 } else if (!view_is_splitscreen(view))
737 return 0;
739 return view->focussed;
742 static const struct got_error *
743 view_loop(struct tog_view *view)
745 const struct got_error *err = NULL;
746 struct tog_view_list_head views;
747 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
748 int fast_refresh = 10;
749 int done = 0, errcode;
751 errcode = pthread_mutex_lock(&tog_mutex);
752 if (errcode)
753 return got_error_set_errno(errcode, "pthread_mutex_lock");
755 TAILQ_INIT(&views);
756 TAILQ_INSERT_HEAD(&views, view, entry);
758 main_view = view;
759 view->focussed = 1;
760 err = view->show(view);
761 if (err)
762 return err;
763 update_panels();
764 doupdate();
765 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
766 /* Refresh fast during initialization, then become slower. */
767 if (fast_refresh && fast_refresh-- == 0)
768 halfdelay(10); /* switch to once per second */
770 err = view_input(&new_view, &dead_view, &focus_view, &done,
771 view, &views);
772 if (err)
773 break;
774 if (dead_view) {
775 struct tog_view *prev = NULL;
777 if (view_is_parent_view(dead_view))
778 prev = TAILQ_PREV(dead_view,
779 tog_view_list_head, entry);
780 else if (view->parent != dead_view)
781 prev = view->parent;
783 if (dead_view->parent)
784 dead_view->parent->child = NULL;
785 else
786 TAILQ_REMOVE(&views, dead_view, entry);
788 err = view_close(dead_view);
789 if (err || (dead_view == main_view && new_view == NULL))
790 goto done;
792 if (view == dead_view) {
793 if (focus_view)
794 view = focus_view;
795 else if (prev)
796 view = prev;
797 else if (!TAILQ_EMPTY(&views))
798 view = TAILQ_LAST(&views,
799 tog_view_list_head);
800 else
801 view = NULL;
802 if (view) {
803 if (view->child && view->child_focussed)
804 focus_view = view->child;
805 else
806 focus_view = view;
810 if (new_view) {
811 struct tog_view *v, *t;
812 /* Only allow one parent view per type. */
813 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
814 if (v->type != new_view->type)
815 continue;
816 TAILQ_REMOVE(&views, v, entry);
817 err = view_close(v);
818 if (err)
819 goto done;
820 break;
822 TAILQ_INSERT_TAIL(&views, new_view, entry);
823 view = new_view;
824 if (focus_view == NULL)
825 focus_view = new_view;
827 if (focus_view) {
828 show_panel(focus_view->panel);
829 if (view)
830 view->focussed = 0;
831 focus_view->focussed = 1;
832 view = focus_view;
833 if (new_view)
834 show_panel(new_view->panel);
835 if (view->child && view_is_splitscreen(view->child))
836 show_panel(view->child->panel);
838 if (view) {
839 if (focus_view == NULL) {
840 view->focussed = 1;
841 show_panel(view->panel);
842 if (view->child && view_is_splitscreen(view->child))
843 show_panel(view->child->panel);
844 focus_view = view;
846 if (view->parent) {
847 err = view->parent->show(view->parent);
848 if (err)
849 goto done;
851 err = view->show(view);
852 if (err)
853 goto done;
854 if (view->child) {
855 err = view->child->show(view->child);
856 if (err)
857 goto done;
859 update_panels();
860 doupdate();
863 done:
864 while (!TAILQ_EMPTY(&views)) {
865 view = TAILQ_FIRST(&views);
866 TAILQ_REMOVE(&views, view, entry);
867 view_close(view);
870 errcode = pthread_mutex_unlock(&tog_mutex);
871 if (errcode && err == NULL)
872 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
874 return err;
877 __dead static void
878 usage_log(void)
880 endwin();
881 fprintf(stderr,
882 "usage: %s log [-c commit] [-r repository-path] [path]\n",
883 getprogname());
884 exit(1);
887 /* Create newly allocated wide-character string equivalent to a byte string. */
888 static const struct got_error *
889 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
891 char *vis = NULL;
892 const struct got_error *err = NULL;
894 *ws = NULL;
895 *wlen = mbstowcs(NULL, s, 0);
896 if (*wlen == (size_t)-1) {
897 int vislen;
898 if (errno != EILSEQ)
899 return got_error_from_errno("mbstowcs");
901 /* byte string invalid in current encoding; try to "fix" it */
902 err = got_mbsavis(&vis, &vislen, s);
903 if (err)
904 return err;
905 *wlen = mbstowcs(NULL, vis, 0);
906 if (*wlen == (size_t)-1) {
907 err = got_error_from_errno("mbstowcs"); /* give up */
908 goto done;
912 *ws = calloc(*wlen + 1, sizeof(**ws));
913 if (*ws == NULL) {
914 err = got_error_from_errno("calloc");
915 goto done;
918 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
919 err = got_error_from_errno("mbstowcs");
920 done:
921 free(vis);
922 if (err) {
923 free(*ws);
924 *ws = NULL;
925 *wlen = 0;
927 return err;
930 /* Format a line for display, ensuring that it won't overflow a width limit. */
931 static const struct got_error *
932 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
933 int col_tab_align)
935 const struct got_error *err = NULL;
936 int cols = 0;
937 wchar_t *wline = NULL;
938 size_t wlen;
939 int i;
941 *wlinep = NULL;
942 *widthp = 0;
944 err = mbs2ws(&wline, &wlen, line);
945 if (err)
946 return err;
948 i = 0;
949 while (i < wlen) {
950 int width = wcwidth(wline[i]);
952 if (width == 0) {
953 i++;
954 continue;
957 if (width == 1 || width == 2) {
958 if (cols + width > wlimit)
959 break;
960 cols += width;
961 i++;
962 } else if (width == -1) {
963 if (wline[i] == L'\t') {
964 width = TABSIZE -
965 ((cols + col_tab_align) % TABSIZE);
966 if (cols + width > wlimit)
967 break;
968 cols += width;
970 i++;
971 } else {
972 err = got_error_from_errno("wcwidth");
973 goto done;
976 wline[i] = L'\0';
977 if (widthp)
978 *widthp = cols;
979 done:
980 if (err)
981 free(wline);
982 else
983 *wlinep = wline;
984 return err;
987 static const struct got_error*
988 build_refs_str(char **refs_str, struct got_reflist_head *refs,
989 struct got_object_id *id, struct got_repository *repo)
991 static const struct got_error *err = NULL;
992 struct got_reflist_entry *re;
993 char *s;
994 const char *name;
996 *refs_str = NULL;
998 SIMPLEQ_FOREACH(re, refs, entry) {
999 struct got_tag_object *tag = NULL;
1000 int cmp;
1002 name = got_ref_get_name(re->ref);
1003 if (strcmp(name, GOT_REF_HEAD) == 0)
1004 continue;
1005 if (strncmp(name, "refs/", 5) == 0)
1006 name += 5;
1007 if (strncmp(name, "got/", 4) == 0)
1008 continue;
1009 if (strncmp(name, "heads/", 6) == 0)
1010 name += 6;
1011 if (strncmp(name, "remotes/", 8) == 0)
1012 name += 8;
1013 if (strncmp(name, "tags/", 5) == 0) {
1014 err = got_object_open_as_tag(&tag, repo, re->id);
1015 if (err) {
1016 if (err->code != GOT_ERR_OBJ_TYPE)
1017 break;
1018 /* Ref points at something other than a tag. */
1019 err = NULL;
1020 tag = NULL;
1023 cmp = got_object_id_cmp(tag ?
1024 got_object_tag_get_object_id(tag) : re->id, id);
1025 if (tag)
1026 got_object_tag_close(tag);
1027 if (cmp != 0)
1028 continue;
1029 s = *refs_str;
1030 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1031 s ? ", " : "", name) == -1) {
1032 err = got_error_from_errno("asprintf");
1033 free(s);
1034 *refs_str = NULL;
1035 break;
1037 free(s);
1040 return err;
1043 static const struct got_error *
1044 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1045 int col_tab_align)
1047 char *smallerthan, *at;
1049 smallerthan = strchr(author, '<');
1050 if (smallerthan && smallerthan[1] != '\0')
1051 author = smallerthan + 1;
1052 at = strchr(author, '@');
1053 if (at)
1054 *at = '\0';
1055 return format_line(wauthor, author_width, author, limit, col_tab_align);
1058 static const struct got_error *
1059 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1060 struct got_object_id *id, struct got_reflist_head *refs,
1061 const size_t date_display_cols, int author_display_cols)
1063 const struct got_error *err = NULL;
1064 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1065 char *logmsg0 = NULL, *logmsg = NULL;
1066 char *author = NULL;
1067 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1068 int author_width, logmsg_width;
1069 char *newline, *line = NULL;
1070 int col, limit;
1071 const int avail = view->ncols;
1072 struct tm tm;
1073 time_t committer_time;
1075 committer_time = got_object_commit_get_committer_time(commit);
1076 if (localtime_r(&committer_time, &tm) == NULL)
1077 return got_error_from_errno("localtime_r");
1078 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1079 >= sizeof(datebuf))
1080 return got_error(GOT_ERR_NO_SPACE);
1082 if (avail <= date_display_cols)
1083 limit = MIN(sizeof(datebuf) - 1, avail);
1084 else
1085 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1086 waddnstr(view->window, datebuf, limit);
1087 col = limit;
1088 if (col > avail)
1089 goto done;
1091 author = strdup(got_object_commit_get_author(commit));
1092 if (author == NULL) {
1093 err = got_error_from_errno("strdup");
1094 goto done;
1096 err = format_author(&wauthor, &author_width, author, avail - col, col);
1097 if (err)
1098 goto done;
1099 waddwstr(view->window, wauthor);
1100 col += author_width;
1101 while (col < avail && author_width < author_display_cols + 2) {
1102 waddch(view->window, ' ');
1103 col++;
1104 author_width++;
1106 if (col > avail)
1107 goto done;
1109 err = got_object_commit_get_logmsg(&logmsg0, commit);
1110 if (err)
1111 goto done;
1112 logmsg = logmsg0;
1113 while (*logmsg == '\n')
1114 logmsg++;
1115 newline = strchr(logmsg, '\n');
1116 if (newline)
1117 *newline = '\0';
1118 limit = avail - col;
1119 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1120 if (err)
1121 goto done;
1122 waddwstr(view->window, wlogmsg);
1123 col += logmsg_width;
1124 while (col < avail) {
1125 waddch(view->window, ' ');
1126 col++;
1128 done:
1129 free(logmsg0);
1130 free(wlogmsg);
1131 free(author);
1132 free(wauthor);
1133 free(line);
1134 return err;
1137 static struct commit_queue_entry *
1138 alloc_commit_queue_entry(struct got_commit_object *commit,
1139 struct got_object_id *id)
1141 struct commit_queue_entry *entry;
1143 entry = calloc(1, sizeof(*entry));
1144 if (entry == NULL)
1145 return NULL;
1147 entry->id = id;
1148 entry->commit = commit;
1149 return entry;
1152 static void
1153 pop_commit(struct commit_queue *commits)
1155 struct commit_queue_entry *entry;
1157 entry = TAILQ_FIRST(&commits->head);
1158 TAILQ_REMOVE(&commits->head, entry, entry);
1159 got_object_commit_close(entry->commit);
1160 commits->ncommits--;
1161 /* Don't free entry->id! It is owned by the commit graph. */
1162 free(entry);
1165 static void
1166 free_commits(struct commit_queue *commits)
1168 while (!TAILQ_EMPTY(&commits->head))
1169 pop_commit(commits);
1172 static const struct got_error *
1173 match_commit(int *have_match, struct got_object_id *id,
1174 struct got_commit_object *commit, regex_t *regex)
1176 const struct got_error *err = NULL;
1177 regmatch_t regmatch;
1178 char *id_str = NULL, *logmsg = NULL;
1180 *have_match = 0;
1182 err = got_object_id_str(&id_str, id);
1183 if (err)
1184 return err;
1186 err = got_object_commit_get_logmsg(&logmsg, commit);
1187 if (err)
1188 goto done;
1190 if (regexec(regex, got_object_commit_get_author(commit), 1,
1191 &regmatch, 0) == 0 ||
1192 regexec(regex, got_object_commit_get_committer(commit), 1,
1193 &regmatch, 0) == 0 ||
1194 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1195 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1196 *have_match = 1;
1197 done:
1198 free(id_str);
1199 free(logmsg);
1200 return err;
1203 static const struct got_error *
1204 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1205 int minqueue, struct got_repository *repo, const char *path,
1206 int *searching, int *search_next_done, regex_t *regex)
1208 const struct got_error *err = NULL;
1209 int nqueued = 0, have_match = 0;
1212 * We keep all commits open throughout the lifetime of the log
1213 * view in order to avoid having to re-fetch commits from disk
1214 * while updating the display.
1216 while (nqueued < minqueue ||
1217 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1218 struct got_object_id *id;
1219 struct got_commit_object *commit;
1220 struct commit_queue_entry *entry;
1221 int errcode;
1223 err = got_commit_graph_iter_next(&id, graph);
1224 if (err) {
1225 if (err->code != GOT_ERR_ITER_NEED_MORE)
1226 break;
1227 err = got_commit_graph_fetch_commits(graph,
1228 minqueue, repo, NULL, NULL);
1229 if (err)
1230 return err;
1231 continue;
1234 if (id == NULL)
1235 break;
1237 err = got_object_open_as_commit(&commit, repo, id);
1238 if (err)
1239 break;
1240 entry = alloc_commit_queue_entry(commit, id);
1241 if (entry == NULL) {
1242 err = got_error_from_errno("alloc_commit_queue_entry");
1243 break;
1246 errcode = pthread_mutex_lock(&tog_mutex);
1247 if (errcode) {
1248 err = got_error_set_errno(errcode,
1249 "pthread_mutex_lock");
1250 break;
1253 entry->idx = commits->ncommits;
1254 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1255 nqueued++;
1256 commits->ncommits++;
1258 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1259 err = match_commit(&have_match, id, commit, regex);
1260 if (err) {
1261 pthread_mutex_lock(&tog_mutex);
1262 break;
1266 errcode = pthread_mutex_unlock(&tog_mutex);
1267 if (errcode && err == NULL)
1268 err = got_error_set_errno(errcode,
1269 "pthread_mutex_unlock");
1271 if (have_match)
1272 break;
1275 return err;
1278 static const struct got_error *
1279 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1280 struct got_repository *repo)
1282 const struct got_error *err = NULL;
1283 struct got_reference *head_ref;
1285 *head_id = NULL;
1287 err = got_ref_open(&head_ref, repo, branch_name, 0);
1288 if (err)
1289 return err;
1291 err = got_ref_resolve(head_id, repo, head_ref);
1292 got_ref_close(head_ref);
1293 if (err) {
1294 *head_id = NULL;
1295 return err;
1298 return NULL;
1301 static const struct got_error *
1302 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1303 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1304 struct commit_queue *commits, int selected_idx, int limit,
1305 struct got_reflist_head *refs, const char *path, int commits_needed)
1307 const struct got_error *err = NULL;
1308 struct tog_log_view_state *s = &view->state.log;
1309 struct commit_queue_entry *entry;
1310 int width;
1311 int ncommits, author_cols = 10;
1312 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1313 char *refs_str = NULL;
1314 wchar_t *wline;
1315 static const size_t date_display_cols = 9;
1317 entry = first;
1318 ncommits = 0;
1319 while (entry) {
1320 if (ncommits == selected_idx) {
1321 *selected = entry;
1322 break;
1324 entry = TAILQ_NEXT(entry, entry);
1325 ncommits++;
1328 if (*selected && !(view->searching && view->search_next_done == 0)) {
1329 err = got_object_id_str(&id_str, (*selected)->id);
1330 if (err)
1331 return err;
1332 if (refs) {
1333 err = build_refs_str(&refs_str, refs, (*selected)->id,
1334 s->repo);
1335 if (err)
1336 goto done;
1340 if (commits_needed == 0)
1341 halfdelay(10); /* disable fast refresh */
1343 if (asprintf(&ncommits_str, " [%d/%d] %s",
1344 entry ? entry->idx + 1 : 0, commits->ncommits,
1345 commits_needed > 0 ?
1346 (view->searching && view->search_next_done == 0
1347 ? "searching..." : "loading... ") :
1348 (refs_str ? refs_str : "")) == -1) {
1349 err = got_error_from_errno("asprintf");
1350 goto done;
1353 if (path && strcmp(path, "/") != 0) {
1354 if (asprintf(&header, "commit %s %s%s",
1355 id_str ? id_str : "........................................",
1356 path, ncommits_str) == -1) {
1357 err = got_error_from_errno("asprintf");
1358 header = NULL;
1359 goto done;
1361 } else if (asprintf(&header, "commit %s%s",
1362 id_str ? id_str : "........................................",
1363 ncommits_str) == -1) {
1364 err = got_error_from_errno("asprintf");
1365 header = NULL;
1366 goto done;
1368 err = format_line(&wline, &width, header, view->ncols, 0);
1369 if (err)
1370 goto done;
1372 werase(view->window);
1374 if (view_needs_focus_indication(view))
1375 wstandout(view->window);
1376 waddwstr(view->window, wline);
1377 while (width < view->ncols) {
1378 waddch(view->window, ' ');
1379 width++;
1381 if (view_needs_focus_indication(view))
1382 wstandend(view->window);
1383 free(wline);
1384 if (limit <= 1)
1385 goto done;
1387 /* Grow author column size if necessary. */
1388 entry = first;
1389 ncommits = 0;
1390 while (entry) {
1391 char *author;
1392 wchar_t *wauthor;
1393 int width;
1394 if (ncommits >= limit - 1)
1395 break;
1396 author = strdup(got_object_commit_get_author(entry->commit));
1397 if (author == NULL) {
1398 err = got_error_from_errno("strdup");
1399 goto done;
1401 err = format_author(&wauthor, &width, author, COLS,
1402 date_display_cols);
1403 if (author_cols < width)
1404 author_cols = width;
1405 free(wauthor);
1406 free(author);
1407 ncommits++;
1408 entry = TAILQ_NEXT(entry, entry);
1411 entry = first;
1412 *last = first;
1413 ncommits = 0;
1414 while (entry) {
1415 if (ncommits >= limit - 1)
1416 break;
1417 if (ncommits == selected_idx)
1418 wstandout(view->window);
1419 err = draw_commit(view, entry->commit, entry->id, refs,
1420 date_display_cols, author_cols);
1421 if (ncommits == selected_idx)
1422 wstandend(view->window);
1423 if (err)
1424 goto done;
1425 ncommits++;
1426 *last = entry;
1427 entry = TAILQ_NEXT(entry, entry);
1430 view_vborder(view);
1431 done:
1432 free(id_str);
1433 free(refs_str);
1434 free(ncommits_str);
1435 free(header);
1436 return err;
1439 static void
1440 scroll_up(struct tog_view *view,
1441 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1442 struct commit_queue *commits)
1444 struct commit_queue_entry *entry;
1445 int nscrolled = 0;
1447 entry = TAILQ_FIRST(&commits->head);
1448 if (*first_displayed_entry == entry)
1449 return;
1451 entry = *first_displayed_entry;
1452 while (entry && nscrolled < maxscroll) {
1453 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1454 if (entry) {
1455 *first_displayed_entry = entry;
1456 nscrolled++;
1461 static const struct got_error *
1462 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1463 pthread_cond_t *need_commits)
1465 int errcode;
1466 int max_wait = 20;
1468 halfdelay(1); /* fast refresh while loading commits */
1470 while (*commits_needed > 0) {
1471 if (*log_complete)
1472 break;
1474 /* Wake the log thread. */
1475 errcode = pthread_cond_signal(need_commits);
1476 if (errcode)
1477 return got_error_set_errno(errcode,
1478 "pthread_cond_signal");
1479 errcode = pthread_mutex_unlock(&tog_mutex);
1480 if (errcode)
1481 return got_error_set_errno(errcode,
1482 "pthread_mutex_unlock");
1483 pthread_yield();
1484 errcode = pthread_mutex_lock(&tog_mutex);
1485 if (errcode)
1486 return got_error_set_errno(errcode,
1487 "pthread_mutex_lock");
1489 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1491 * Thread is not done yet; lose a key press
1492 * and let the user retry... this way the GUI
1493 * remains interactive while logging deep paths
1494 * with few commits in history.
1496 return NULL;
1500 return NULL;
1503 static const struct got_error *
1504 scroll_down(struct tog_view *view,
1505 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1506 struct commit_queue_entry **last_displayed_entry,
1507 struct commit_queue *commits, int *log_complete, int *commits_needed,
1508 pthread_cond_t *need_commits)
1510 const struct got_error *err = NULL;
1511 struct commit_queue_entry *pentry;
1512 int nscrolled = 0;
1514 if (*last_displayed_entry == NULL)
1515 return NULL;
1517 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1518 if (pentry == NULL && !*log_complete) {
1520 * Ask the log thread for required amount of commits
1521 * plus some amount of pre-fetching.
1523 (*commits_needed) += maxscroll + 20;
1524 err = trigger_log_thread(0, commits_needed, log_complete,
1525 need_commits);
1526 if (err)
1527 return err;
1530 do {
1531 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1532 if (pentry == NULL)
1533 break;
1535 *last_displayed_entry = pentry;
1537 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1538 if (pentry == NULL)
1539 break;
1540 *first_displayed_entry = pentry;
1541 } while (++nscrolled < maxscroll);
1543 return err;
1546 static const struct got_error *
1547 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1548 struct got_commit_object *commit, struct got_object_id *commit_id,
1549 struct tog_view *log_view, struct got_reflist_head *refs,
1550 struct got_repository *repo)
1552 const struct got_error *err;
1553 struct got_object_qid *parent_id;
1554 struct tog_view *diff_view;
1556 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1557 if (diff_view == NULL)
1558 return got_error_from_errno("view_open");
1560 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1561 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1562 commit_id, log_view, refs, repo);
1563 if (err == NULL)
1564 *new_view = diff_view;
1565 return err;
1568 static const struct got_error *
1569 tree_view_visit_subtree(struct got_tree_object *subtree,
1570 struct tog_tree_view_state *s)
1572 struct tog_parent_tree *parent;
1574 parent = calloc(1, sizeof(*parent));
1575 if (parent == NULL)
1576 return got_error_from_errno("calloc");
1578 parent->tree = s->tree;
1579 parent->first_displayed_entry = s->first_displayed_entry;
1580 parent->selected_entry = s->selected_entry;
1581 parent->selected = s->selected;
1582 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1583 s->tree = subtree;
1584 s->entries = got_object_tree_get_entries(s->tree);
1585 s->selected = 0;
1586 s->first_displayed_entry = NULL;
1587 return NULL;
1591 static const struct got_error *
1592 browse_commit_tree(struct tog_view **new_view, int begin_x,
1593 struct commit_queue_entry *entry, const char *path,
1594 struct got_reflist_head *refs, struct got_repository *repo)
1596 const struct got_error *err = NULL;
1597 struct got_tree_object *tree;
1598 struct got_tree_entry *te;
1599 struct tog_tree_view_state *s;
1600 struct tog_view *tree_view;
1601 char *slash, *subpath = NULL;
1602 const char *p;
1604 err = got_object_open_as_tree(&tree, repo,
1605 got_object_commit_get_tree_id(entry->commit));
1606 if (err)
1607 return err;
1609 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1610 if (tree_view == NULL)
1611 return got_error_from_errno("view_open");
1613 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1614 if (err) {
1615 got_object_tree_close(tree);
1616 return err;
1618 s = &tree_view->state.tree;
1620 *new_view = tree_view;
1622 /* Walk the path and open corresponding tree objects. */
1623 p = path;
1624 while (p[0] == '/')
1625 p++;
1626 while (*p) {
1627 struct got_object_id *tree_id;
1629 /* Ensure the correct subtree entry is selected. */
1630 slash = strchr(p, '/');
1631 if (slash == NULL)
1632 slash = strchr(p, '\0');
1633 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1634 if (strncmp(p, te->name, slash - p) == 0) {
1635 s->selected_entry = te;
1636 break;
1638 s->selected++;
1640 if (s->selected_entry == NULL) {
1641 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1642 break;
1644 if (s->tree != s->root)
1645 s->selected++; /* skip '..' */
1647 if (!S_ISDIR(s->selected_entry->mode)) {
1648 /* Jump to this file's entry. */
1649 s->first_displayed_entry = s->selected_entry;
1650 s->selected = 0;
1651 break;
1654 slash = strchr(p, '/');
1655 if (slash)
1656 subpath = strndup(path, slash - path);
1657 else
1658 subpath = strdup(path);
1659 if (subpath == NULL) {
1660 err = got_error_from_errno("strdup");
1661 break;
1664 err = got_object_id_by_path(&tree_id, repo, entry->id,
1665 subpath);
1666 if (err)
1667 break;
1669 err = got_object_open_as_tree(&tree, repo, tree_id);
1670 free(tree_id);
1671 if (err)
1672 break;
1674 err = tree_view_visit_subtree(tree, s);
1675 if (err) {
1676 got_object_tree_close(tree);
1677 break;
1679 if (slash == NULL)
1680 break;
1681 free(subpath);
1682 subpath = NULL;
1683 p = slash;
1686 free(subpath);
1687 return err;
1690 static void *
1691 log_thread(void *arg)
1693 const struct got_error *err = NULL;
1694 int errcode = 0;
1695 struct tog_log_thread_args *a = arg;
1696 int done = 0;
1698 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1699 NULL, NULL);
1700 if (err)
1701 return (void *)err;
1703 while (!done && !err && !tog_sigpipe_received) {
1704 err = queue_commits(a->graph, a->commits, 1, a->repo,
1705 a->in_repo_path, a->searching, a->search_next_done,
1706 a->regex);
1707 if (err) {
1708 if (err->code != GOT_ERR_ITER_COMPLETED)
1709 return (void *)err;
1710 err = NULL;
1711 done = 1;
1712 } else if (a->commits_needed > 0)
1713 a->commits_needed--;
1715 errcode = pthread_mutex_lock(&tog_mutex);
1716 if (errcode) {
1717 err = got_error_set_errno(errcode,
1718 "pthread_mutex_lock");
1719 break;
1720 } else if (*a->quit)
1721 done = 1;
1722 else if (*a->first_displayed_entry == NULL) {
1723 *a->first_displayed_entry =
1724 TAILQ_FIRST(&a->commits->head);
1725 *a->selected_entry = *a->first_displayed_entry;
1728 if (done)
1729 a->commits_needed = 0;
1730 else if (a->commits_needed == 0) {
1731 errcode = pthread_cond_wait(&a->need_commits,
1732 &tog_mutex);
1733 if (errcode)
1734 err = got_error_set_errno(errcode,
1735 "pthread_cond_wait");
1738 errcode = pthread_mutex_unlock(&tog_mutex);
1739 if (errcode && err == NULL)
1740 err = got_error_set_errno(errcode,
1741 "pthread_mutex_unlock");
1743 a->log_complete = 1;
1744 return (void *)err;
1747 static const struct got_error *
1748 stop_log_thread(struct tog_log_view_state *s)
1750 const struct got_error *err = NULL;
1751 int errcode;
1753 if (s->thread) {
1754 s->quit = 1;
1755 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1756 if (errcode)
1757 return got_error_set_errno(errcode,
1758 "pthread_cond_signal");
1759 errcode = pthread_mutex_unlock(&tog_mutex);
1760 if (errcode)
1761 return got_error_set_errno(errcode,
1762 "pthread_mutex_unlock");
1763 errcode = pthread_join(s->thread, (void **)&err);
1764 if (errcode)
1765 return got_error_set_errno(errcode, "pthread_join");
1766 errcode = pthread_mutex_lock(&tog_mutex);
1767 if (errcode)
1768 return got_error_set_errno(errcode,
1769 "pthread_mutex_lock");
1770 s->thread = NULL;
1773 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1774 if (errcode && err == NULL)
1775 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1777 if (s->thread_args.repo) {
1778 got_repo_close(s->thread_args.repo);
1779 s->thread_args.repo = NULL;
1782 if (s->thread_args.graph) {
1783 got_commit_graph_close(s->thread_args.graph);
1784 s->thread_args.graph = NULL;
1787 return err;
1790 static const struct got_error *
1791 close_log_view(struct tog_view *view)
1793 const struct got_error *err = NULL;
1794 struct tog_log_view_state *s = &view->state.log;
1796 err = stop_log_thread(s);
1797 free_commits(&s->commits);
1798 free(s->in_repo_path);
1799 s->in_repo_path = NULL;
1800 free(s->start_id);
1801 s->start_id = NULL;
1802 return err;
1805 static const struct got_error *
1806 search_start_log_view(struct tog_view *view)
1808 struct tog_log_view_state *s = &view->state.log;
1810 s->matched_entry = NULL;
1811 s->search_entry = NULL;
1812 return NULL;
1815 static const struct got_error *
1816 search_next_log_view(struct tog_view *view)
1818 const struct got_error *err = NULL;
1819 struct tog_log_view_state *s = &view->state.log;
1820 struct commit_queue_entry *entry;
1822 if (!view->searching) {
1823 view->search_next_done = 1;
1824 return NULL;
1827 if (s->search_entry) {
1828 int errcode, ch;
1829 errcode = pthread_mutex_unlock(&tog_mutex);
1830 if (errcode)
1831 return got_error_set_errno(errcode,
1832 "pthread_mutex_unlock");
1833 ch = wgetch(view->window);
1834 errcode = pthread_mutex_lock(&tog_mutex);
1835 if (errcode)
1836 return got_error_set_errno(errcode,
1837 "pthread_mutex_lock");
1838 if (ch == KEY_BACKSPACE) {
1839 view->search_next_done = 1;
1840 return NULL;
1842 if (view->searching == TOG_SEARCH_FORWARD)
1843 entry = TAILQ_NEXT(s->search_entry, entry);
1844 else
1845 entry = TAILQ_PREV(s->search_entry,
1846 commit_queue_head, entry);
1847 } else if (s->matched_entry) {
1848 if (view->searching == TOG_SEARCH_FORWARD)
1849 entry = TAILQ_NEXT(s->selected_entry, entry);
1850 else
1851 entry = TAILQ_PREV(s->selected_entry,
1852 commit_queue_head, entry);
1853 } else {
1854 if (view->searching == TOG_SEARCH_FORWARD)
1855 entry = TAILQ_FIRST(&s->commits.head);
1856 else
1857 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1860 while (1) {
1861 int have_match = 0;
1863 if (entry == NULL) {
1864 if (s->thread_args.log_complete ||
1865 view->searching == TOG_SEARCH_BACKWARD) {
1866 view->search_next_done = 1;
1867 return NULL;
1870 * Poke the log thread for more commits and return,
1871 * allowing the main loop to make progress. Search
1872 * will resume at s->search_entry once we come back.
1874 s->thread_args.commits_needed++;
1875 return trigger_log_thread(1,
1876 &s->thread_args.commits_needed,
1877 &s->thread_args.log_complete,
1878 &s->thread_args.need_commits);
1881 err = match_commit(&have_match, entry->id, entry->commit,
1882 &view->regex);
1883 if (err)
1884 break;
1885 if (have_match) {
1886 view->search_next_done = 1;
1887 s->matched_entry = entry;
1888 break;
1891 s->search_entry = entry;
1892 if (view->searching == TOG_SEARCH_FORWARD)
1893 entry = TAILQ_NEXT(entry, entry);
1894 else
1895 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1898 if (s->matched_entry) {
1899 int cur = s->selected_entry->idx;
1900 while (cur < s->matched_entry->idx) {
1901 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1902 if (err)
1903 return err;
1904 cur++;
1906 while (cur > s->matched_entry->idx) {
1907 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1908 if (err)
1909 return err;
1910 cur--;
1914 s->search_entry = NULL;
1916 return NULL;
1919 static const struct got_error *
1920 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1921 struct got_reflist_head *refs, struct got_repository *repo,
1922 const char *head_ref_name, const char *path, int check_disk)
1924 const struct got_error *err = NULL;
1925 struct tog_log_view_state *s = &view->state.log;
1926 struct got_repository *thread_repo = NULL;
1927 struct got_commit_graph *thread_graph = NULL;
1928 int errcode;
1930 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1931 if (err != NULL)
1932 goto done;
1934 /* The commit queue only contains commits being displayed. */
1935 TAILQ_INIT(&s->commits.head);
1936 s->commits.ncommits = 0;
1938 s->refs = refs;
1939 s->repo = repo;
1940 s->head_ref_name = head_ref_name;
1941 s->start_id = got_object_id_dup(start_id);
1942 if (s->start_id == NULL) {
1943 err = got_error_from_errno("got_object_id_dup");
1944 goto done;
1947 view->show = show_log_view;
1948 view->input = input_log_view;
1949 view->close = close_log_view;
1950 view->search_start = search_start_log_view;
1951 view->search_next = search_next_log_view;
1953 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
1954 if (err)
1955 goto done;
1956 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1957 0, thread_repo);
1958 if (err)
1959 goto done;
1961 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1962 if (errcode) {
1963 err = got_error_set_errno(errcode, "pthread_cond_init");
1964 goto done;
1967 s->thread_args.commits_needed = view->nlines;
1968 s->thread_args.graph = thread_graph;
1969 s->thread_args.commits = &s->commits;
1970 s->thread_args.in_repo_path = s->in_repo_path;
1971 s->thread_args.start_id = s->start_id;
1972 s->thread_args.repo = thread_repo;
1973 s->thread_args.log_complete = 0;
1974 s->thread_args.quit = &s->quit;
1975 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1976 s->thread_args.selected_entry = &s->selected_entry;
1977 s->thread_args.searching = &view->searching;
1978 s->thread_args.search_next_done = &view->search_next_done;
1979 s->thread_args.regex = &view->regex;
1980 done:
1981 if (err)
1982 close_log_view(view);
1983 return err;
1986 static const struct got_error *
1987 show_log_view(struct tog_view *view)
1989 struct tog_log_view_state *s = &view->state.log;
1991 if (s->thread == NULL) {
1992 int errcode = pthread_create(&s->thread, NULL, log_thread,
1993 &s->thread_args);
1994 if (errcode)
1995 return got_error_set_errno(errcode, "pthread_create");
1998 return draw_commits(view, &s->last_displayed_entry,
1999 &s->selected_entry, s->first_displayed_entry,
2000 &s->commits, s->selected, view->nlines, s->refs,
2001 s->in_repo_path, s->thread_args.commits_needed);
2004 static const struct got_error *
2005 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2006 struct tog_view **focus_view, struct tog_view *view, int ch)
2008 const struct got_error *err = NULL;
2009 struct tog_log_view_state *s = &view->state.log;
2010 char *parent_path, *in_repo_path = NULL;
2011 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2012 int begin_x = 0;
2013 struct got_object_id *start_id;
2015 switch (ch) {
2016 case 'q':
2017 s->quit = 1;
2018 break;
2019 case 'k':
2020 case KEY_UP:
2021 case '<':
2022 case ',':
2023 if (s->first_displayed_entry == NULL)
2024 break;
2025 if (s->selected > 0)
2026 s->selected--;
2027 else
2028 scroll_up(view, &s->first_displayed_entry, 1,
2029 &s->commits);
2030 break;
2031 case KEY_PPAGE:
2032 case CTRL('b'):
2033 if (s->first_displayed_entry == NULL)
2034 break;
2035 if (TAILQ_FIRST(&s->commits.head) ==
2036 s->first_displayed_entry) {
2037 s->selected = 0;
2038 break;
2040 scroll_up(view, &s->first_displayed_entry,
2041 view->nlines, &s->commits);
2042 break;
2043 case 'j':
2044 case KEY_DOWN:
2045 case '>':
2046 case '.':
2047 if (s->first_displayed_entry == NULL)
2048 break;
2049 if (s->selected < MIN(view->nlines - 2,
2050 s->commits.ncommits - 1)) {
2051 s->selected++;
2052 break;
2054 err = scroll_down(view, &s->first_displayed_entry, 1,
2055 &s->last_displayed_entry, &s->commits,
2056 &s->thread_args.log_complete,
2057 &s->thread_args.commits_needed,
2058 &s->thread_args.need_commits);
2059 break;
2060 case KEY_NPAGE:
2061 case CTRL('f'): {
2062 struct commit_queue_entry *first;
2063 first = s->first_displayed_entry;
2064 if (first == NULL)
2065 break;
2066 err = scroll_down(view, &s->first_displayed_entry,
2067 view->nlines, &s->last_displayed_entry,
2068 &s->commits, &s->thread_args.log_complete,
2069 &s->thread_args.commits_needed,
2070 &s->thread_args.need_commits);
2071 if (err)
2072 break;
2073 if (first == s->first_displayed_entry &&
2074 s->selected < MIN(view->nlines - 2,
2075 s->commits.ncommits - 1)) {
2076 /* can't scroll further down */
2077 s->selected = MIN(view->nlines - 2,
2078 s->commits.ncommits - 1);
2080 err = NULL;
2081 break;
2083 case KEY_RESIZE:
2084 if (s->selected > view->nlines - 2)
2085 s->selected = view->nlines - 2;
2086 if (s->selected > s->commits.ncommits - 1)
2087 s->selected = s->commits.ncommits - 1;
2088 break;
2089 case KEY_ENTER:
2090 case ' ':
2091 case '\r':
2092 if (s->selected_entry == NULL)
2093 break;
2094 if (view_is_parent_view(view))
2095 begin_x = view_split_begin_x(view->begin_x);
2096 err = open_diff_view_for_commit(&diff_view, begin_x,
2097 s->selected_entry->commit, s->selected_entry->id,
2098 view, s->refs, s->repo);
2099 if (err)
2100 break;
2101 if (view_is_parent_view(view)) {
2102 err = view_close_child(view);
2103 if (err)
2104 return err;
2105 err = view_set_child(view, diff_view);
2106 if (err) {
2107 view_close(diff_view);
2108 break;
2110 *focus_view = diff_view;
2111 view->child_focussed = 1;
2112 } else
2113 *new_view = diff_view;
2114 break;
2115 case 't':
2116 if (s->selected_entry == NULL)
2117 break;
2118 if (view_is_parent_view(view))
2119 begin_x = view_split_begin_x(view->begin_x);
2120 err = browse_commit_tree(&tree_view, begin_x,
2121 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2122 if (err)
2123 break;
2124 if (view_is_parent_view(view)) {
2125 err = view_close_child(view);
2126 if (err)
2127 return err;
2128 err = view_set_child(view, tree_view);
2129 if (err) {
2130 view_close(tree_view);
2131 break;
2133 *focus_view = tree_view;
2134 view->child_focussed = 1;
2135 } else
2136 *new_view = tree_view;
2137 break;
2138 case KEY_BACKSPACE:
2139 if (strcmp(s->in_repo_path, "/") == 0)
2140 break;
2141 parent_path = dirname(s->in_repo_path);
2142 if (parent_path && strcmp(parent_path, ".") != 0) {
2143 err = stop_log_thread(s);
2144 if (err)
2145 return err;
2146 lv = view_open(view->nlines, view->ncols,
2147 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2148 if (lv == NULL)
2149 return got_error_from_errno(
2150 "view_open");
2151 err = open_log_view(lv, s->start_id, s->refs,
2152 s->repo, s->head_ref_name, parent_path, 0);
2153 if (err)
2154 return err;;
2155 if (view_is_parent_view(view))
2156 *new_view = lv;
2157 else {
2158 view_set_child(view->parent, lv);
2159 *focus_view = lv;
2161 return NULL;
2163 break;
2164 case CTRL('l'):
2165 err = stop_log_thread(s);
2166 if (err)
2167 return err;
2168 lv = view_open(view->nlines, view->ncols,
2169 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2170 if (lv == NULL)
2171 return got_error_from_errno("view_open");
2172 err = get_head_commit_id(&start_id, s->head_ref_name ?
2173 s->head_ref_name : GOT_REF_HEAD, s->repo);
2174 if (err) {
2175 view_close(lv);
2176 return err;
2178 in_repo_path = strdup(s->in_repo_path);
2179 if (in_repo_path == NULL) {
2180 free(start_id);
2181 view_close(lv);
2182 return got_error_from_errno("strdup");
2184 got_ref_list_free(s->refs);
2185 err = got_ref_list(s->refs, s->repo, NULL,
2186 got_ref_cmp_by_name, NULL);
2187 if (err) {
2188 free(start_id);
2189 view_close(lv);
2190 return err;
2192 err = open_log_view(lv, start_id, s->refs, s->repo,
2193 s->head_ref_name, in_repo_path, 0);
2194 if (err) {
2195 free(start_id);
2196 view_close(lv);
2197 return err;;
2199 *dead_view = view;
2200 *new_view = lv;
2201 break;
2202 default:
2203 break;
2206 return err;
2209 static const struct got_error *
2210 apply_unveil(const char *repo_path, const char *worktree_path)
2212 const struct got_error *error;
2214 #ifdef PROFILE
2215 if (unveil("gmon.out", "rwc") != 0)
2216 return got_error_from_errno2("unveil", "gmon.out");
2217 #endif
2218 if (repo_path && unveil(repo_path, "r") != 0)
2219 return got_error_from_errno2("unveil", repo_path);
2221 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2222 return got_error_from_errno2("unveil", worktree_path);
2224 if (unveil("/tmp", "rwc") != 0)
2225 return got_error_from_errno2("unveil", "/tmp");
2227 error = got_privsep_unveil_exec_helpers();
2228 if (error != NULL)
2229 return error;
2231 if (unveil(NULL, NULL) != 0)
2232 return got_error_from_errno("unveil");
2234 return NULL;
2237 static void
2238 init_curses(void)
2240 initscr();
2241 cbreak();
2242 halfdelay(1); /* Do fast refresh while initial view is loading. */
2243 noecho();
2244 nonl();
2245 intrflush(stdscr, FALSE);
2246 keypad(stdscr, TRUE);
2247 curs_set(0);
2248 signal(SIGWINCH, tog_sigwinch);
2249 signal(SIGPIPE, tog_sigpipe);
2252 static const struct got_error *
2253 cmd_log(int argc, char *argv[])
2255 const struct got_error *error;
2256 struct got_repository *repo = NULL;
2257 struct got_worktree *worktree = NULL;
2258 struct got_reflist_head refs;
2259 struct got_object_id *start_id = NULL;
2260 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2261 char *start_commit = NULL, *head_ref_name = NULL;
2262 int ch;
2263 struct tog_view *view;
2265 SIMPLEQ_INIT(&refs);
2267 #ifndef PROFILE
2268 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2269 NULL) == -1)
2270 err(1, "pledge");
2271 #endif
2273 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2274 switch (ch) {
2275 case 'c':
2276 start_commit = optarg;
2277 break;
2278 case 'r':
2279 repo_path = realpath(optarg, NULL);
2280 if (repo_path == NULL)
2281 return got_error_from_errno2("realpath",
2282 optarg);
2283 break;
2284 default:
2285 usage_log();
2286 /* NOTREACHED */
2290 argc -= optind;
2291 argv += optind;
2293 cwd = getcwd(NULL, 0);
2294 if (cwd == NULL) {
2295 error = got_error_from_errno("getcwd");
2296 goto done;
2298 error = got_worktree_open(&worktree, cwd);
2299 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2300 goto done;
2301 error = NULL;
2303 if (argc == 0) {
2304 path = strdup("");
2305 if (path == NULL) {
2306 error = got_error_from_errno("strdup");
2307 goto done;
2309 } else if (argc == 1) {
2310 if (worktree) {
2311 error = got_worktree_resolve_path(&path, worktree,
2312 argv[0]);
2313 if (error)
2314 goto done;
2315 } else {
2316 path = strdup(argv[0]);
2317 if (path == NULL) {
2318 error = got_error_from_errno("strdup");
2319 goto done;
2322 } else
2323 usage_log();
2325 if (repo_path == NULL) {
2326 if (worktree)
2327 repo_path = strdup(
2328 got_worktree_get_repo_path(worktree));
2329 else
2330 repo_path = strdup(cwd);
2332 if (repo_path == NULL) {
2333 error = got_error_from_errno("strdup");
2334 goto done;
2337 init_curses();
2339 error = got_repo_open(&repo, repo_path, NULL);
2340 if (error != NULL)
2341 goto done;
2343 error = apply_unveil(got_repo_get_path(repo),
2344 worktree ? got_worktree_get_root_path(worktree) : NULL);
2345 if (error)
2346 goto done;
2348 if (start_commit == NULL)
2349 error = get_head_commit_id(&start_id, worktree ?
2350 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2351 repo);
2352 else {
2353 error = get_head_commit_id(&start_id, start_commit, repo);
2354 if (error) {
2355 if (error->code != GOT_ERR_NOT_REF)
2356 goto done;
2357 error = got_repo_match_object_id_prefix(&start_id,
2358 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2361 if (error != NULL)
2362 goto done;
2364 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2365 if (error)
2366 goto done;
2368 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2369 if (view == NULL) {
2370 error = got_error_from_errno("view_open");
2371 goto done;
2373 if (worktree) {
2374 head_ref_name = strdup(
2375 got_worktree_get_head_ref_name(worktree));
2376 if (head_ref_name == NULL) {
2377 error = got_error_from_errno("strdup");
2378 goto done;
2381 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2382 path, 1);
2383 if (error)
2384 goto done;
2385 if (worktree) {
2386 /* Release work tree lock. */
2387 got_worktree_close(worktree);
2388 worktree = NULL;
2390 error = view_loop(view);
2391 done:
2392 free(repo_path);
2393 free(cwd);
2394 free(path);
2395 free(start_id);
2396 free(head_ref_name);
2397 if (repo)
2398 got_repo_close(repo);
2399 if (worktree)
2400 got_worktree_close(worktree);
2401 got_ref_list_free(&refs);
2402 return error;
2405 __dead static void
2406 usage_diff(void)
2408 endwin();
2409 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2410 getprogname());
2411 exit(1);
2414 static char *
2415 parse_next_line(FILE *f, size_t *len)
2417 char *line;
2418 size_t linelen;
2419 size_t lineno;
2420 const char delim[3] = { '\0', '\0', '\0'};
2422 line = fparseln(f, &linelen, &lineno, delim, 0);
2423 if (len)
2424 *len = linelen;
2425 return line;
2428 static const struct got_error *
2429 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2430 int *last_displayed_line, int *eof, int max_lines,
2431 char *header)
2433 const struct got_error *err;
2434 int nlines = 0, nprinted = 0;
2435 char *line;
2436 size_t len;
2437 wchar_t *wline;
2438 int width;
2440 rewind(f);
2441 werase(view->window);
2443 if (header) {
2444 err = format_line(&wline, &width, header, view->ncols, 0);
2445 if (err) {
2446 return err;
2449 if (view_needs_focus_indication(view))
2450 wstandout(view->window);
2451 waddwstr(view->window, wline);
2452 if (view_needs_focus_indication(view))
2453 wstandend(view->window);
2454 if (width <= view->ncols - 1)
2455 waddch(view->window, '\n');
2457 if (max_lines <= 1)
2458 return NULL;
2459 max_lines--;
2462 *eof = 0;
2463 while (nprinted < max_lines) {
2464 line = parse_next_line(f, &len);
2465 if (line == NULL) {
2466 *eof = 1;
2467 break;
2469 if (++nlines < *first_displayed_line) {
2470 free(line);
2471 continue;
2474 err = format_line(&wline, &width, line, view->ncols, 0);
2475 if (err) {
2476 free(line);
2477 return err;
2479 waddwstr(view->window, wline);
2480 if (width <= view->ncols - 1)
2481 waddch(view->window, '\n');
2482 if (++nprinted == 1)
2483 *first_displayed_line = nlines;
2484 free(line);
2485 free(wline);
2486 wline = NULL;
2488 *last_displayed_line = nlines;
2490 view_vborder(view);
2492 if (*eof) {
2493 while (nprinted < view->nlines) {
2494 waddch(view->window, '\n');
2495 nprinted++;
2498 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2499 if (err) {
2500 return err;
2503 wstandout(view->window);
2504 waddwstr(view->window, wline);
2505 wstandend(view->window);
2508 return NULL;
2511 static char *
2512 get_datestr(time_t *time, char *datebuf)
2514 struct tm mytm, *tm;
2515 char *p, *s;
2517 tm = gmtime_r(time, &mytm);
2518 if (tm == NULL)
2519 return NULL;
2520 s = asctime_r(tm, datebuf);
2521 if (s == NULL)
2522 return NULL;
2523 p = strchr(s, '\n');
2524 if (p)
2525 *p = '\0';
2526 return s;
2529 static const struct got_error *
2530 write_commit_info(struct got_object_id *commit_id,
2531 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2533 const struct got_error *err = NULL;
2534 char datebuf[26], *datestr;
2535 struct got_commit_object *commit;
2536 char *id_str = NULL, *logmsg = NULL;
2537 time_t committer_time;
2538 const char *author, *committer;
2539 char *refs_str = NULL;
2541 if (refs) {
2542 err = build_refs_str(&refs_str, refs, commit_id, repo);
2543 if (err)
2544 return err;
2547 err = got_object_open_as_commit(&commit, repo, commit_id);
2548 if (err)
2549 return err;
2551 err = got_object_id_str(&id_str, commit_id);
2552 if (err) {
2553 err = got_error_from_errno("got_object_id_str");
2554 goto done;
2557 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2558 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2559 err = got_error_from_errno("fprintf");
2560 goto done;
2562 if (fprintf(outfile, "from: %s\n",
2563 got_object_commit_get_author(commit)) < 0) {
2564 err = got_error_from_errno("fprintf");
2565 goto done;
2567 committer_time = got_object_commit_get_committer_time(commit);
2568 datestr = get_datestr(&committer_time, datebuf);
2569 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2570 err = got_error_from_errno("fprintf");
2571 goto done;
2573 author = got_object_commit_get_author(commit);
2574 committer = got_object_commit_get_committer(commit);
2575 if (strcmp(author, committer) != 0 &&
2576 fprintf(outfile, "via: %s\n", committer) < 0) {
2577 err = got_error_from_errno("fprintf");
2578 goto done;
2580 err = got_object_commit_get_logmsg(&logmsg, commit);
2581 if (err)
2582 goto done;
2583 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2584 err = got_error_from_errno("fprintf");
2585 goto done;
2587 done:
2588 free(id_str);
2589 free(logmsg);
2590 free(refs_str);
2591 got_object_commit_close(commit);
2592 return err;
2595 static const struct got_error *
2596 create_diff(struct tog_diff_view_state *s)
2598 const struct got_error *err = NULL;
2599 FILE *f = NULL;
2600 int obj_type;
2602 f = got_opentemp();
2603 if (f == NULL) {
2604 err = got_error_from_errno("got_opentemp");
2605 goto done;
2607 if (s->f && fclose(s->f) != 0) {
2608 err = got_error_from_errno("fclose");
2609 goto done;
2611 s->f = f;
2613 if (s->id1)
2614 err = got_object_get_type(&obj_type, s->repo, s->id1);
2615 else
2616 err = got_object_get_type(&obj_type, s->repo, s->id2);
2617 if (err)
2618 goto done;
2620 switch (obj_type) {
2621 case GOT_OBJ_TYPE_BLOB:
2622 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2623 s->diff_context, 0, s->repo, f);
2624 break;
2625 case GOT_OBJ_TYPE_TREE:
2626 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2627 s->diff_context, 0, s->repo, f);
2628 break;
2629 case GOT_OBJ_TYPE_COMMIT: {
2630 const struct got_object_id_queue *parent_ids;
2631 struct got_object_qid *pid;
2632 struct got_commit_object *commit2;
2634 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2635 if (err)
2636 break;
2637 /* Show commit info if we're diffing to a parent/root commit. */
2638 if (s->id1 == NULL)
2639 write_commit_info(s->id2, s->refs, s->repo, f);
2640 else {
2641 parent_ids = got_object_commit_get_parent_ids(commit2);
2642 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2643 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2644 write_commit_info(s->id2, s->refs,
2645 s->repo, f);
2646 break;
2650 got_object_commit_close(commit2);
2652 err = got_diff_objects_as_commits(s->id1, s->id2,
2653 s->diff_context, 0, s->repo, f);
2654 break;
2656 default:
2657 err = got_error(GOT_ERR_OBJ_TYPE);
2658 break;
2660 done:
2661 if (f && fflush(f) != 0 && err == NULL)
2662 err = got_error_from_errno("fflush");
2663 return err;
2666 static void
2667 diff_view_indicate_progress(struct tog_view *view)
2669 mvwaddstr(view->window, 0, 0, "diffing...");
2670 update_panels();
2671 doupdate();
2674 static const struct got_error *
2675 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2676 struct got_object_id *id2, struct tog_view *log_view,
2677 struct got_reflist_head *refs, struct got_repository *repo)
2679 const struct got_error *err;
2681 if (id1 != NULL && id2 != NULL) {
2682 int type1, type2;
2683 err = got_object_get_type(&type1, repo, id1);
2684 if (err)
2685 return err;
2686 err = got_object_get_type(&type2, repo, id2);
2687 if (err)
2688 return err;
2690 if (type1 != type2)
2691 return got_error(GOT_ERR_OBJ_TYPE);
2694 if (id1) {
2695 view->state.diff.id1 = got_object_id_dup(id1);
2696 if (view->state.diff.id1 == NULL)
2697 return got_error_from_errno("got_object_id_dup");
2698 } else
2699 view->state.diff.id1 = NULL;
2701 view->state.diff.id2 = got_object_id_dup(id2);
2702 if (view->state.diff.id2 == NULL) {
2703 free(view->state.diff.id1);
2704 view->state.diff.id1 = NULL;
2705 return got_error_from_errno("got_object_id_dup");
2707 view->state.diff.f = NULL;
2708 view->state.diff.first_displayed_line = 1;
2709 view->state.diff.last_displayed_line = view->nlines;
2710 view->state.diff.diff_context = 3;
2711 view->state.diff.log_view = log_view;
2712 view->state.diff.repo = repo;
2713 view->state.diff.refs = refs;
2715 if (log_view && view_is_splitscreen(view))
2716 show_log_view(log_view); /* draw vborder */
2717 diff_view_indicate_progress(view);
2719 err = create_diff(&view->state.diff);
2720 if (err) {
2721 free(view->state.diff.id1);
2722 view->state.diff.id1 = NULL;
2723 free(view->state.diff.id2);
2724 view->state.diff.id2 = NULL;
2725 return err;
2728 view->show = show_diff_view;
2729 view->input = input_diff_view;
2730 view->close = close_diff_view;
2732 return NULL;
2735 static const struct got_error *
2736 close_diff_view(struct tog_view *view)
2738 const struct got_error *err = NULL;
2740 free(view->state.diff.id1);
2741 view->state.diff.id1 = NULL;
2742 free(view->state.diff.id2);
2743 view->state.diff.id2 = NULL;
2744 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2745 err = got_error_from_errno("fclose");
2746 return err;
2749 static const struct got_error *
2750 show_diff_view(struct tog_view *view)
2752 const struct got_error *err;
2753 struct tog_diff_view_state *s = &view->state.diff;
2754 char *id_str1 = NULL, *id_str2, *header;
2756 if (s->id1) {
2757 err = got_object_id_str(&id_str1, s->id1);
2758 if (err)
2759 return err;
2761 err = got_object_id_str(&id_str2, s->id2);
2762 if (err)
2763 return err;
2765 if (asprintf(&header, "diff %s %s",
2766 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2767 err = got_error_from_errno("asprintf");
2768 free(id_str1);
2769 free(id_str2);
2770 return err;
2772 free(id_str1);
2773 free(id_str2);
2775 return draw_file(view, s->f, &s->first_displayed_line,
2776 &s->last_displayed_line, &s->eof, view->nlines,
2777 header);
2780 static const struct got_error *
2781 set_selected_commit(struct tog_diff_view_state *s,
2782 struct commit_queue_entry *entry)
2784 const struct got_error *err;
2785 const struct got_object_id_queue *parent_ids;
2786 struct got_commit_object *selected_commit;
2787 struct got_object_qid *pid;
2789 free(s->id2);
2790 s->id2 = got_object_id_dup(entry->id);
2791 if (s->id2 == NULL)
2792 return got_error_from_errno("got_object_id_dup");
2794 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2795 if (err)
2796 return err;
2797 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2798 free(s->id1);
2799 pid = SIMPLEQ_FIRST(parent_ids);
2800 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2801 got_object_commit_close(selected_commit);
2802 return NULL;
2805 static const struct got_error *
2806 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2807 struct tog_view **focus_view, struct tog_view *view, int ch)
2809 const struct got_error *err = NULL;
2810 struct tog_diff_view_state *s = &view->state.diff;
2811 struct tog_log_view_state *ls;
2812 struct commit_queue_entry *entry;
2813 int i;
2815 switch (ch) {
2816 case 'k':
2817 case KEY_UP:
2818 if (s->first_displayed_line > 1)
2819 s->first_displayed_line--;
2820 break;
2821 case KEY_PPAGE:
2822 case CTRL('b'):
2823 if (s->first_displayed_line == 1)
2824 break;
2825 i = 0;
2826 while (i++ < view->nlines - 1 &&
2827 s->first_displayed_line > 1)
2828 s->first_displayed_line--;
2829 break;
2830 case 'j':
2831 case KEY_DOWN:
2832 if (!s->eof)
2833 s->first_displayed_line++;
2834 break;
2835 case KEY_NPAGE:
2836 case CTRL('f'):
2837 case ' ':
2838 if (s->eof)
2839 break;
2840 i = 0;
2841 while (!s->eof && i++ < view->nlines - 1) {
2842 char *line;
2843 line = parse_next_line(s->f, NULL);
2844 s->first_displayed_line++;
2845 if (line == NULL)
2846 break;
2848 break;
2849 case '[':
2850 if (s->diff_context > 0) {
2851 s->diff_context--;
2852 diff_view_indicate_progress(view);
2853 err = create_diff(s);
2855 break;
2856 case ']':
2857 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2858 s->diff_context++;
2859 diff_view_indicate_progress(view);
2860 err = create_diff(s);
2862 break;
2863 case '<':
2864 case ',':
2865 if (s->log_view == NULL)
2866 break;
2867 ls = &s->log_view->state.log;
2868 entry = TAILQ_PREV(ls->selected_entry,
2869 commit_queue_head, entry);
2870 if (entry == NULL)
2871 break;
2873 err = input_log_view(NULL, NULL, NULL, s->log_view,
2874 KEY_UP);
2875 if (err)
2876 break;
2878 err = set_selected_commit(s, entry);
2879 if (err)
2880 break;
2882 s->first_displayed_line = 1;
2883 s->last_displayed_line = view->nlines;
2885 diff_view_indicate_progress(view);
2886 err = create_diff(s);
2887 break;
2888 case '>':
2889 case '.':
2890 if (s->log_view == NULL)
2891 break;
2892 ls = &s->log_view->state.log;
2894 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2895 ls->thread_args.commits_needed++;
2897 /* Display "loading..." in log view. */
2898 show_log_view(s->log_view);
2899 update_panels();
2900 doupdate();
2902 err = trigger_log_thread(1 /* load_all */,
2903 &ls->thread_args.commits_needed,
2904 &ls->thread_args.log_complete,
2905 &ls->thread_args.need_commits);
2906 if (err)
2907 break;
2909 err = input_log_view(NULL, NULL, NULL, s->log_view,
2910 KEY_DOWN);
2911 if (err)
2912 break;
2914 entry = TAILQ_NEXT(ls->selected_entry, entry);
2915 if (entry == NULL)
2916 break;
2918 err = set_selected_commit(s, entry);
2919 if (err)
2920 break;
2922 s->first_displayed_line = 1;
2923 s->last_displayed_line = view->nlines;
2925 diff_view_indicate_progress(view);
2926 err = create_diff(s);
2927 break;
2928 default:
2929 break;
2932 return err;
2935 static const struct got_error *
2936 cmd_diff(int argc, char *argv[])
2938 const struct got_error *error = NULL;
2939 struct got_repository *repo = NULL;
2940 struct got_reflist_head refs;
2941 struct got_object_id *id1 = NULL, *id2 = NULL;
2942 char *repo_path = NULL;
2943 char *id_str1 = NULL, *id_str2 = NULL;
2944 int ch;
2945 struct tog_view *view;
2947 SIMPLEQ_INIT(&refs);
2949 #ifndef PROFILE
2950 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2951 NULL) == -1)
2952 err(1, "pledge");
2953 #endif
2955 while ((ch = getopt(argc, argv, "")) != -1) {
2956 switch (ch) {
2957 default:
2958 usage_diff();
2959 /* NOTREACHED */
2963 argc -= optind;
2964 argv += optind;
2966 if (argc == 0) {
2967 usage_diff(); /* TODO show local worktree changes */
2968 } else if (argc == 2) {
2969 repo_path = getcwd(NULL, 0);
2970 if (repo_path == NULL)
2971 return got_error_from_errno("getcwd");
2972 id_str1 = argv[0];
2973 id_str2 = argv[1];
2974 } else if (argc == 3) {
2975 repo_path = realpath(argv[0], NULL);
2976 if (repo_path == NULL)
2977 return got_error_from_errno2("realpath", argv[0]);
2978 id_str1 = argv[1];
2979 id_str2 = argv[2];
2980 } else
2981 usage_diff();
2983 init_curses();
2985 error = got_repo_open(&repo, repo_path, NULL);
2986 if (error)
2987 goto done;
2989 error = apply_unveil(got_repo_get_path(repo), NULL);
2990 if (error)
2991 goto done;
2993 error = got_repo_match_object_id_prefix(&id1, id_str1,
2994 GOT_OBJ_TYPE_ANY, repo);
2995 if (error)
2996 goto done;
2998 error = got_repo_match_object_id_prefix(&id2, id_str2,
2999 GOT_OBJ_TYPE_ANY, repo);
3000 if (error)
3001 goto done;
3003 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3004 if (error)
3005 goto done;
3007 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3008 if (view == NULL) {
3009 error = got_error_from_errno("view_open");
3010 goto done;
3012 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3013 if (error)
3014 goto done;
3015 error = view_loop(view);
3016 done:
3017 free(repo_path);
3018 if (repo)
3019 got_repo_close(repo);
3020 got_ref_list_free(&refs);
3021 return error;
3024 __dead static void
3025 usage_blame(void)
3027 endwin();
3028 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3029 getprogname());
3030 exit(1);
3033 struct tog_blame_line {
3034 int annotated;
3035 struct got_object_id *id;
3038 static const struct got_error *
3039 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3040 const char *path, struct tog_blame_line *lines, int nlines,
3041 int blame_complete, int selected_line, int *first_displayed_line,
3042 int *last_displayed_line, int *eof, int max_lines)
3044 const struct got_error *err;
3045 int lineno = 0, nprinted = 0;
3046 char *line;
3047 size_t len;
3048 wchar_t *wline;
3049 int width;
3050 struct tog_blame_line *blame_line;
3051 struct got_object_id *prev_id = NULL;
3052 char *id_str;
3054 err = got_object_id_str(&id_str, id);
3055 if (err)
3056 return err;
3058 rewind(f);
3059 werase(view->window);
3061 if (asprintf(&line, "commit %s", id_str) == -1) {
3062 err = got_error_from_errno("asprintf");
3063 free(id_str);
3064 return err;
3067 err = format_line(&wline, &width, line, view->ncols, 0);
3068 free(line);
3069 line = NULL;
3070 if (err)
3071 return err;
3072 if (view_needs_focus_indication(view))
3073 wstandout(view->window);
3074 waddwstr(view->window, wline);
3075 if (view_needs_focus_indication(view))
3076 wstandend(view->window);
3077 free(wline);
3078 wline = NULL;
3079 if (width < view->ncols - 1)
3080 waddch(view->window, '\n');
3082 if (asprintf(&line, "[%d/%d] %s%s",
3083 *first_displayed_line - 1 + selected_line, nlines,
3084 blame_complete ? "" : "annotating... ", path) == -1) {
3085 free(id_str);
3086 return got_error_from_errno("asprintf");
3088 free(id_str);
3089 err = format_line(&wline, &width, line, view->ncols, 0);
3090 free(line);
3091 line = NULL;
3092 if (err)
3093 return err;
3094 waddwstr(view->window, wline);
3095 free(wline);
3096 wline = NULL;
3097 if (width < view->ncols - 1)
3098 waddch(view->window, '\n');
3100 *eof = 0;
3101 while (nprinted < max_lines - 2) {
3102 line = parse_next_line(f, &len);
3103 if (line == NULL) {
3104 *eof = 1;
3105 break;
3107 if (++lineno < *first_displayed_line) {
3108 free(line);
3109 continue;
3112 if (view->ncols <= 9) {
3113 width = 9;
3114 wline = wcsdup(L"");
3115 if (wline == NULL)
3116 err = got_error_from_errno("wcsdup");
3117 } else {
3118 err = format_line(&wline, &width, line,
3119 view->ncols - 9, 9);
3120 width += 9;
3122 if (err) {
3123 free(line);
3124 return err;
3127 if (view->focussed && nprinted == selected_line - 1)
3128 wstandout(view->window);
3130 if (nlines > 0) {
3131 blame_line = &lines[lineno - 1];
3132 if (blame_line->annotated && prev_id &&
3133 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3134 !(view->focussed &&
3135 nprinted == selected_line - 1)) {
3136 waddstr(view->window, " ");
3137 } else if (blame_line->annotated) {
3138 char *id_str;
3139 err = got_object_id_str(&id_str, blame_line->id);
3140 if (err) {
3141 free(line);
3142 free(wline);
3143 return err;
3145 wprintw(view->window, "%.8s", id_str);
3146 free(id_str);
3147 prev_id = blame_line->id;
3148 } else {
3149 waddstr(view->window, "........");
3150 prev_id = NULL;
3152 } else {
3153 waddstr(view->window, "........");
3154 prev_id = NULL;
3157 if (view->focussed && nprinted == selected_line - 1)
3158 wstandend(view->window);
3159 waddstr(view->window, " ");
3161 waddwstr(view->window, wline);
3162 if (width <= view->ncols - 1)
3163 waddch(view->window, '\n');
3164 if (++nprinted == 1)
3165 *first_displayed_line = lineno;
3166 free(line);
3167 free(wline);
3168 wline = NULL;
3170 *last_displayed_line = lineno;
3172 view_vborder(view);
3174 return NULL;
3177 static const struct got_error *
3178 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3180 const struct got_error *err = NULL;
3181 struct tog_blame_cb_args *a = arg;
3182 struct tog_blame_line *line;
3183 int errcode;
3185 if (nlines != a->nlines ||
3186 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3187 return got_error(GOT_ERR_RANGE);
3189 errcode = pthread_mutex_lock(&tog_mutex);
3190 if (errcode)
3191 return got_error_set_errno(errcode, "pthread_mutex_lock");
3193 if (*a->quit) { /* user has quit the blame view */
3194 err = got_error(GOT_ERR_ITER_COMPLETED);
3195 goto done;
3198 if (lineno == -1)
3199 goto done; /* no change in this commit */
3201 line = &a->lines[lineno - 1];
3202 if (line->annotated)
3203 goto done;
3205 line->id = got_object_id_dup(id);
3206 if (line->id == NULL) {
3207 err = got_error_from_errno("got_object_id_dup");
3208 goto done;
3210 line->annotated = 1;
3211 done:
3212 errcode = pthread_mutex_unlock(&tog_mutex);
3213 if (errcode)
3214 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3215 return err;
3218 static void *
3219 blame_thread(void *arg)
3221 const struct got_error *err;
3222 struct tog_blame_thread_args *ta = arg;
3223 struct tog_blame_cb_args *a = ta->cb_args;
3224 int errcode;
3226 err = got_blame(ta->path, a->commit_id, ta->repo,
3227 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3228 if (err && err->code == GOT_ERR_CANCELLED)
3229 err = NULL;
3231 errcode = pthread_mutex_lock(&tog_mutex);
3232 if (errcode)
3233 return (void *)got_error_set_errno(errcode,
3234 "pthread_mutex_lock");
3236 got_repo_close(ta->repo);
3237 ta->repo = NULL;
3238 *ta->complete = 1;
3240 errcode = pthread_mutex_unlock(&tog_mutex);
3241 if (errcode && err == NULL)
3242 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3244 return (void *)err;
3247 static struct got_object_id *
3248 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3249 int first_displayed_line, int selected_line)
3251 struct tog_blame_line *line;
3253 if (nlines <= 0)
3254 return NULL;
3256 line = &lines[first_displayed_line - 1 + selected_line - 1];
3257 if (!line->annotated)
3258 return NULL;
3260 return line->id;
3263 static const struct got_error *
3264 stop_blame(struct tog_blame *blame)
3266 const struct got_error *err = NULL;
3267 int i;
3269 if (blame->thread) {
3270 int errcode;
3271 errcode = pthread_mutex_unlock(&tog_mutex);
3272 if (errcode)
3273 return got_error_set_errno(errcode,
3274 "pthread_mutex_unlock");
3275 errcode = pthread_join(blame->thread, (void **)&err);
3276 if (errcode)
3277 return got_error_set_errno(errcode, "pthread_join");
3278 errcode = pthread_mutex_lock(&tog_mutex);
3279 if (errcode)
3280 return got_error_set_errno(errcode,
3281 "pthread_mutex_lock");
3282 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3283 err = NULL;
3284 blame->thread = NULL;
3286 if (blame->thread_args.repo) {
3287 got_repo_close(blame->thread_args.repo);
3288 blame->thread_args.repo = NULL;
3290 if (blame->f) {
3291 if (fclose(blame->f) != 0 && err == NULL)
3292 err = got_error_from_errno("fclose");
3293 blame->f = NULL;
3295 if (blame->lines) {
3296 for (i = 0; i < blame->nlines; i++)
3297 free(blame->lines[i].id);
3298 free(blame->lines);
3299 blame->lines = NULL;
3301 free(blame->cb_args.commit_id);
3302 blame->cb_args.commit_id = NULL;
3304 return err;
3307 static const struct got_error *
3308 cancel_blame_view(void *arg)
3310 const struct got_error *err = NULL;
3311 int *done = arg;
3312 int errcode;
3314 errcode = pthread_mutex_lock(&tog_mutex);
3315 if (errcode)
3316 return got_error_set_errno(errcode,
3317 "pthread_mutex_unlock");
3319 if (*done)
3320 err = got_error(GOT_ERR_CANCELLED);
3322 errcode = pthread_mutex_unlock(&tog_mutex);
3323 if (errcode)
3324 return got_error_set_errno(errcode,
3325 "pthread_mutex_lock");
3327 return err;
3330 static const struct got_error *
3331 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3332 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3333 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3334 struct got_repository *repo)
3336 const struct got_error *err = NULL;
3337 struct got_blob_object *blob = NULL;
3338 struct got_repository *thread_repo = NULL;
3339 struct got_object_id *obj_id = NULL;
3340 int obj_type;
3342 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3343 if (err)
3344 return err;
3345 if (obj_id == NULL)
3346 return got_error(GOT_ERR_NO_OBJ);
3348 err = got_object_get_type(&obj_type, repo, obj_id);
3349 if (err)
3350 goto done;
3352 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3353 err = got_error(GOT_ERR_OBJ_TYPE);
3354 goto done;
3357 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3358 if (err)
3359 goto done;
3360 blame->f = got_opentemp();
3361 if (blame->f == NULL) {
3362 err = got_error_from_errno("got_opentemp");
3363 goto done;
3365 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3366 &blame->line_offsets, blame->f, blob);
3367 if (err || blame->nlines == 0)
3368 goto done;
3370 /* Don't include \n at EOF in the blame line count. */
3371 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3372 blame->nlines--;
3374 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3375 if (blame->lines == NULL) {
3376 err = got_error_from_errno("calloc");
3377 goto done;
3380 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3381 if (err)
3382 goto done;
3384 blame->cb_args.view = view;
3385 blame->cb_args.lines = blame->lines;
3386 blame->cb_args.nlines = blame->nlines;
3387 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3388 if (blame->cb_args.commit_id == NULL) {
3389 err = got_error_from_errno("got_object_id_dup");
3390 goto done;
3392 blame->cb_args.quit = done;
3394 blame->thread_args.path = path;
3395 blame->thread_args.repo = thread_repo;
3396 blame->thread_args.cb_args = &blame->cb_args;
3397 blame->thread_args.complete = blame_complete;
3398 blame->thread_args.cancel_cb = cancel_blame_view;
3399 blame->thread_args.cancel_arg = done;
3400 *blame_complete = 0;
3402 done:
3403 if (blob)
3404 got_object_blob_close(blob);
3405 free(obj_id);
3406 if (err)
3407 stop_blame(blame);
3408 return err;
3411 static const struct got_error *
3412 open_blame_view(struct tog_view *view, char *path,
3413 struct got_object_id *commit_id, struct got_reflist_head *refs,
3414 struct got_repository *repo)
3416 const struct got_error *err = NULL;
3417 struct tog_blame_view_state *s = &view->state.blame;
3419 SIMPLEQ_INIT(&s->blamed_commits);
3421 s->path = strdup(path);
3422 if (s->path == NULL)
3423 return got_error_from_errno("strdup");
3425 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3426 if (err) {
3427 free(s->path);
3428 return err;
3431 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3432 s->first_displayed_line = 1;
3433 s->last_displayed_line = view->nlines;
3434 s->selected_line = 1;
3435 s->blame_complete = 0;
3436 s->repo = repo;
3437 s->refs = refs;
3438 s->commit_id = commit_id;
3439 memset(&s->blame, 0, sizeof(s->blame));
3441 view->show = show_blame_view;
3442 view->input = input_blame_view;
3443 view->close = close_blame_view;
3444 view->search_start = search_start_blame_view;
3445 view->search_next = search_next_blame_view;
3447 return run_blame(&s->blame, view, &s->blame_complete,
3448 &s->first_displayed_line, &s->last_displayed_line,
3449 &s->selected_line, &s->done, &s->eof, s->path,
3450 s->blamed_commit->id, s->repo);
3453 static const struct got_error *
3454 close_blame_view(struct tog_view *view)
3456 const struct got_error *err = NULL;
3457 struct tog_blame_view_state *s = &view->state.blame;
3459 if (s->blame.thread)
3460 err = stop_blame(&s->blame);
3462 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3463 struct got_object_qid *blamed_commit;
3464 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3465 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3466 got_object_qid_free(blamed_commit);
3469 free(s->path);
3471 return err;
3474 static const struct got_error *
3475 search_start_blame_view(struct tog_view *view)
3477 struct tog_blame_view_state *s = &view->state.blame;
3479 s->matched_line = 0;
3480 return NULL;
3483 static int
3484 match_line(const char *line, regex_t *regex)
3486 regmatch_t regmatch;
3488 return regexec(regex, line, 1, &regmatch, 0) == 0;
3492 static const struct got_error *
3493 search_next_blame_view(struct tog_view *view)
3495 struct tog_blame_view_state *s = &view->state.blame;
3496 int lineno;
3498 if (!view->searching) {
3499 view->search_next_done = 1;
3500 return NULL;
3503 if (s->matched_line) {
3504 if (view->searching == TOG_SEARCH_FORWARD)
3505 lineno = s->matched_line + 1;
3506 else
3507 lineno = s->matched_line - 1;
3508 } else {
3509 if (view->searching == TOG_SEARCH_FORWARD)
3510 lineno = 1;
3511 else
3512 lineno = s->blame.nlines;
3515 while (1) {
3516 char *line = NULL;
3517 off_t offset;
3518 size_t len;
3520 if (lineno <= 0 || lineno > s->blame.nlines) {
3521 if (s->matched_line == 0) {
3522 view->search_next_done = 1;
3523 free(line);
3524 break;
3527 if (view->searching == TOG_SEARCH_FORWARD)
3528 lineno = 1;
3529 else
3530 lineno = s->blame.nlines;
3533 offset = s->blame.line_offsets[lineno - 1];
3534 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3535 free(line);
3536 return got_error_from_errno("fseeko");
3538 free(line);
3539 line = parse_next_line(s->blame.f, &len);
3540 if (line && match_line(line, &view->regex)) {
3541 view->search_next_done = 1;
3542 s->matched_line = lineno;
3543 free(line);
3544 break;
3546 free(line);
3547 if (view->searching == TOG_SEARCH_FORWARD)
3548 lineno++;
3549 else
3550 lineno--;
3553 if (s->matched_line) {
3554 s->first_displayed_line = s->matched_line;
3555 s->selected_line = 1;
3558 return NULL;
3561 static const struct got_error *
3562 show_blame_view(struct tog_view *view)
3564 const struct got_error *err = NULL;
3565 struct tog_blame_view_state *s = &view->state.blame;
3566 int errcode;
3568 if (s->blame.thread == NULL) {
3569 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3570 &s->blame.thread_args);
3571 if (errcode)
3572 return got_error_set_errno(errcode, "pthread_create");
3574 halfdelay(1); /* fast refresh while annotating */
3577 if (s->blame_complete)
3578 halfdelay(10); /* disable fast refresh */
3580 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3581 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3582 s->selected_line, &s->first_displayed_line,
3583 &s->last_displayed_line, &s->eof, view->nlines);
3585 view_vborder(view);
3586 return err;
3589 static const struct got_error *
3590 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3591 struct tog_view **focus_view, struct tog_view *view, int ch)
3593 const struct got_error *err = NULL, *thread_err = NULL;
3594 struct tog_view *diff_view;
3595 struct tog_blame_view_state *s = &view->state.blame;
3596 int begin_x = 0;
3598 switch (ch) {
3599 case 'q':
3600 s->done = 1;
3601 break;
3602 case 'k':
3603 case KEY_UP:
3604 if (s->selected_line > 1)
3605 s->selected_line--;
3606 else if (s->selected_line == 1 &&
3607 s->first_displayed_line > 1)
3608 s->first_displayed_line--;
3609 break;
3610 case KEY_PPAGE:
3611 if (s->first_displayed_line == 1) {
3612 s->selected_line = 1;
3613 break;
3615 if (s->first_displayed_line > view->nlines - 2)
3616 s->first_displayed_line -=
3617 (view->nlines - 2);
3618 else
3619 s->first_displayed_line = 1;
3620 break;
3621 case 'j':
3622 case KEY_DOWN:
3623 if (s->selected_line < view->nlines - 2 &&
3624 s->first_displayed_line +
3625 s->selected_line <= s->blame.nlines)
3626 s->selected_line++;
3627 else if (s->last_displayed_line <
3628 s->blame.nlines)
3629 s->first_displayed_line++;
3630 break;
3631 case 'b':
3632 case 'p': {
3633 struct got_object_id *id = NULL;
3634 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3635 s->first_displayed_line, s->selected_line);
3636 if (id == NULL)
3637 break;
3638 if (ch == 'p') {
3639 struct got_commit_object *commit;
3640 struct got_object_qid *pid;
3641 struct got_object_id *blob_id = NULL;
3642 int obj_type;
3643 err = got_object_open_as_commit(&commit,
3644 s->repo, id);
3645 if (err)
3646 break;
3647 pid = SIMPLEQ_FIRST(
3648 got_object_commit_get_parent_ids(commit));
3649 if (pid == NULL) {
3650 got_object_commit_close(commit);
3651 break;
3653 /* Check if path history ends here. */
3654 err = got_object_id_by_path(&blob_id, s->repo,
3655 pid->id, s->path);
3656 if (err) {
3657 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3658 err = NULL;
3659 got_object_commit_close(commit);
3660 break;
3662 err = got_object_get_type(&obj_type, s->repo,
3663 blob_id);
3664 free(blob_id);
3665 /* Can't blame non-blob type objects. */
3666 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3667 got_object_commit_close(commit);
3668 break;
3670 err = got_object_qid_alloc(&s->blamed_commit,
3671 pid->id);
3672 got_object_commit_close(commit);
3673 } else {
3674 if (got_object_id_cmp(id,
3675 s->blamed_commit->id) == 0)
3676 break;
3677 err = got_object_qid_alloc(&s->blamed_commit,
3678 id);
3680 if (err)
3681 break;
3682 s->done = 1;
3683 thread_err = stop_blame(&s->blame);
3684 s->done = 0;
3685 if (thread_err)
3686 break;
3687 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3688 s->blamed_commit, entry);
3689 err = run_blame(&s->blame, view, &s->blame_complete,
3690 &s->first_displayed_line, &s->last_displayed_line,
3691 &s->selected_line, &s->done, &s->eof,
3692 s->path, s->blamed_commit->id, s->repo);
3693 if (err)
3694 break;
3695 break;
3697 case 'B': {
3698 struct got_object_qid *first;
3699 first = SIMPLEQ_FIRST(&s->blamed_commits);
3700 if (!got_object_id_cmp(first->id, s->commit_id))
3701 break;
3702 s->done = 1;
3703 thread_err = stop_blame(&s->blame);
3704 s->done = 0;
3705 if (thread_err)
3706 break;
3707 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3708 got_object_qid_free(s->blamed_commit);
3709 s->blamed_commit =
3710 SIMPLEQ_FIRST(&s->blamed_commits);
3711 err = run_blame(&s->blame, view, &s->blame_complete,
3712 &s->first_displayed_line, &s->last_displayed_line,
3713 &s->selected_line, &s->done, &s->eof, s->path,
3714 s->blamed_commit->id, s->repo);
3715 if (err)
3716 break;
3717 break;
3719 case KEY_ENTER:
3720 case '\r': {
3721 struct got_object_id *id = NULL;
3722 struct got_object_qid *pid;
3723 struct got_commit_object *commit = NULL;
3724 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3725 s->first_displayed_line, s->selected_line);
3726 if (id == NULL)
3727 break;
3728 err = got_object_open_as_commit(&commit, s->repo, id);
3729 if (err)
3730 break;
3731 pid = SIMPLEQ_FIRST(
3732 got_object_commit_get_parent_ids(commit));
3733 if (view_is_parent_view(view))
3734 begin_x = view_split_begin_x(view->begin_x);
3735 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3736 if (diff_view == NULL) {
3737 got_object_commit_close(commit);
3738 err = got_error_from_errno("view_open");
3739 break;
3741 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3742 id, NULL, s->refs, s->repo);
3743 got_object_commit_close(commit);
3744 if (err) {
3745 view_close(diff_view);
3746 break;
3748 if (view_is_parent_view(view)) {
3749 err = view_close_child(view);
3750 if (err)
3751 break;
3752 err = view_set_child(view, diff_view);
3753 if (err) {
3754 view_close(diff_view);
3755 break;
3757 *focus_view = diff_view;
3758 view->child_focussed = 1;
3759 } else
3760 *new_view = diff_view;
3761 if (err)
3762 break;
3763 break;
3765 case KEY_NPAGE:
3766 case ' ':
3767 if (s->last_displayed_line >= s->blame.nlines &&
3768 s->selected_line >= MIN(s->blame.nlines,
3769 view->nlines - 2)) {
3770 break;
3772 if (s->last_displayed_line >= s->blame.nlines &&
3773 s->selected_line < view->nlines - 2) {
3774 s->selected_line = MIN(s->blame.nlines,
3775 view->nlines - 2);
3776 break;
3778 if (s->last_displayed_line + view->nlines - 2
3779 <= s->blame.nlines)
3780 s->first_displayed_line +=
3781 view->nlines - 2;
3782 else
3783 s->first_displayed_line =
3784 s->blame.nlines -
3785 (view->nlines - 3);
3786 break;
3787 case KEY_RESIZE:
3788 if (s->selected_line > view->nlines - 2) {
3789 s->selected_line = MIN(s->blame.nlines,
3790 view->nlines - 2);
3792 break;
3793 default:
3794 break;
3796 return thread_err ? thread_err : err;
3799 static const struct got_error *
3800 cmd_blame(int argc, char *argv[])
3802 const struct got_error *error;
3803 struct got_repository *repo = NULL;
3804 struct got_reflist_head refs;
3805 struct got_worktree *worktree = NULL;
3806 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3807 struct got_object_id *commit_id = NULL;
3808 char *commit_id_str = NULL;
3809 int ch;
3810 struct tog_view *view;
3812 SIMPLEQ_INIT(&refs);
3814 #ifndef PROFILE
3815 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3816 NULL) == -1)
3817 err(1, "pledge");
3818 #endif
3820 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3821 switch (ch) {
3822 case 'c':
3823 commit_id_str = optarg;
3824 break;
3825 case 'r':
3826 repo_path = realpath(optarg, NULL);
3827 if (repo_path == NULL)
3828 return got_error_from_errno2("realpath",
3829 optarg);
3830 break;
3831 default:
3832 usage_blame();
3833 /* NOTREACHED */
3837 argc -= optind;
3838 argv += optind;
3840 if (argc == 1)
3841 path = argv[0];
3842 else
3843 usage_blame();
3845 cwd = getcwd(NULL, 0);
3846 if (cwd == NULL) {
3847 error = got_error_from_errno("getcwd");
3848 goto done;
3850 if (repo_path == NULL) {
3851 error = got_worktree_open(&worktree, cwd);
3852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3853 goto done;
3854 else
3855 error = NULL;
3856 if (worktree) {
3857 repo_path =
3858 strdup(got_worktree_get_repo_path(worktree));
3859 if (repo_path == NULL)
3860 error = got_error_from_errno("strdup");
3861 if (error)
3862 goto done;
3863 } else {
3864 repo_path = strdup(cwd);
3865 if (repo_path == NULL) {
3866 error = got_error_from_errno("strdup");
3867 goto done;
3872 init_curses();
3874 error = got_repo_open(&repo, repo_path, NULL);
3875 if (error != NULL)
3876 goto done;
3878 error = apply_unveil(got_repo_get_path(repo), NULL);
3879 if (error)
3880 goto done;
3882 if (worktree) {
3883 const char *prefix = got_worktree_get_path_prefix(worktree);
3884 char *p, *worktree_subdir = cwd +
3885 strlen(got_worktree_get_root_path(worktree));
3886 if (asprintf(&p, "%s%s%s%s%s",
3887 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3888 worktree_subdir, worktree_subdir[0] ? "/" : "",
3889 path) == -1) {
3890 error = got_error_from_errno("asprintf");
3891 goto done;
3893 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3894 free(p);
3895 } else {
3896 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3898 if (error)
3899 goto done;
3901 if (commit_id_str == NULL) {
3902 struct got_reference *head_ref;
3903 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3904 if (error != NULL)
3905 goto done;
3906 error = got_ref_resolve(&commit_id, repo, head_ref);
3907 got_ref_close(head_ref);
3908 } else {
3909 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3910 if (error) {
3911 if (error->code != GOT_ERR_NOT_REF)
3912 goto done;
3913 error = got_repo_match_object_id_prefix(&commit_id,
3914 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3917 if (error != NULL)
3918 goto done;
3920 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3921 if (error)
3922 goto done;
3924 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3925 if (view == NULL) {
3926 error = got_error_from_errno("view_open");
3927 goto done;
3929 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3930 if (error)
3931 goto done;
3932 if (worktree) {
3933 /* Release work tree lock. */
3934 got_worktree_close(worktree);
3935 worktree = NULL;
3937 error = view_loop(view);
3938 done:
3939 free(repo_path);
3940 free(cwd);
3941 free(commit_id);
3942 if (worktree)
3943 got_worktree_close(worktree);
3944 if (repo)
3945 got_repo_close(repo);
3946 got_ref_list_free(&refs);
3947 return error;
3950 static const struct got_error *
3951 draw_tree_entries(struct tog_view *view,
3952 struct got_tree_entry **first_displayed_entry,
3953 struct got_tree_entry **last_displayed_entry,
3954 struct got_tree_entry **selected_entry, int *ndisplayed,
3955 const char *label, int show_ids, const char *parent_path,
3956 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3958 const struct got_error *err = NULL;
3959 struct got_tree_entry *te;
3960 wchar_t *wline;
3961 int width, n;
3963 *ndisplayed = 0;
3965 werase(view->window);
3967 if (limit == 0)
3968 return NULL;
3970 err = format_line(&wline, &width, label, view->ncols, 0);
3971 if (err)
3972 return err;
3973 if (view_needs_focus_indication(view))
3974 wstandout(view->window);
3975 waddwstr(view->window, wline);
3976 if (view_needs_focus_indication(view))
3977 wstandend(view->window);
3978 free(wline);
3979 wline = NULL;
3980 if (width < view->ncols - 1)
3981 waddch(view->window, '\n');
3982 if (--limit <= 0)
3983 return NULL;
3984 err = format_line(&wline, &width, parent_path, view->ncols, 0);
3985 if (err)
3986 return err;
3987 waddwstr(view->window, wline);
3988 free(wline);
3989 wline = NULL;
3990 if (width < view->ncols - 1)
3991 waddch(view->window, '\n');
3992 if (--limit <= 0)
3993 return NULL;
3994 waddch(view->window, '\n');
3995 if (--limit <= 0)
3996 return NULL;
3998 te = SIMPLEQ_FIRST(&entries->head);
3999 if (*first_displayed_entry == NULL) {
4000 if (selected == 0) {
4001 if (view->focussed)
4002 wstandout(view->window);
4003 *selected_entry = NULL;
4005 waddstr(view->window, " ..\n"); /* parent directory */
4006 if (selected == 0 && view->focussed)
4007 wstandend(view->window);
4008 (*ndisplayed)++;
4009 if (--limit <= 0)
4010 return NULL;
4011 n = 1;
4012 } else {
4013 n = 0;
4014 while (te != *first_displayed_entry)
4015 te = SIMPLEQ_NEXT(te, entry);
4018 while (te) {
4019 char *line = NULL, *id_str = NULL;
4020 const char *modestr = "";
4022 if (show_ids) {
4023 err = got_object_id_str(&id_str, te->id);
4024 if (err)
4025 return got_error_from_errno(
4026 "got_object_id_str");
4028 if (got_object_tree_entry_is_submodule(te))
4029 modestr = "$";
4030 else if (S_ISLNK(te->mode))
4031 modestr = "@";
4032 else if (S_ISDIR(te->mode))
4033 modestr = "/";
4034 else if (te->mode & S_IXUSR)
4035 modestr = "*";
4036 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4037 te->name, modestr) == -1) {
4038 free(id_str);
4039 return got_error_from_errno("asprintf");
4041 free(id_str);
4042 err = format_line(&wline, &width, line, view->ncols, 0);
4043 if (err) {
4044 free(line);
4045 break;
4047 if (n == selected) {
4048 if (view->focussed)
4049 wstandout(view->window);
4050 *selected_entry = te;
4052 waddwstr(view->window, wline);
4053 if (width < view->ncols - 1)
4054 waddch(view->window, '\n');
4055 if (n == selected && view->focussed)
4056 wstandend(view->window);
4057 free(line);
4058 free(wline);
4059 wline = NULL;
4060 n++;
4061 (*ndisplayed)++;
4062 *last_displayed_entry = te;
4063 if (--limit <= 0)
4064 break;
4065 te = SIMPLEQ_NEXT(te, entry);
4068 return err;
4071 static void
4072 tree_scroll_up(struct tog_view *view,
4073 struct got_tree_entry **first_displayed_entry, int maxscroll,
4074 const struct got_tree_entries *entries, int isroot)
4076 struct got_tree_entry *te, *prev;
4077 int i;
4079 if (*first_displayed_entry == NULL)
4080 return;
4082 te = SIMPLEQ_FIRST(&entries->head);
4083 if (*first_displayed_entry == te) {
4084 if (!isroot)
4085 *first_displayed_entry = NULL;
4086 return;
4089 /* XXX this is stupid... switch to TAILQ? */
4090 for (i = 0; i < maxscroll; i++) {
4091 while (te != *first_displayed_entry) {
4092 prev = te;
4093 te = SIMPLEQ_NEXT(te, entry);
4095 *first_displayed_entry = prev;
4096 te = SIMPLEQ_FIRST(&entries->head);
4098 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4099 *first_displayed_entry = NULL;
4102 static int
4103 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4104 struct got_tree_entry *last_displayed_entry,
4105 const struct got_tree_entries *entries)
4107 struct got_tree_entry *next, *last;
4108 int n = 0;
4110 if (*first_displayed_entry)
4111 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4112 else
4113 next = SIMPLEQ_FIRST(&entries->head);
4114 last = last_displayed_entry;
4115 while (next && last && n++ < maxscroll) {
4116 last = SIMPLEQ_NEXT(last, entry);
4117 if (last) {
4118 *first_displayed_entry = next;
4119 next = SIMPLEQ_NEXT(next, entry);
4122 return n;
4125 static const struct got_error *
4126 tree_entry_path(char **path, struct tog_parent_trees *parents,
4127 struct got_tree_entry *te)
4129 const struct got_error *err = NULL;
4130 struct tog_parent_tree *pt;
4131 size_t len = 2; /* for leading slash and NUL */
4133 TAILQ_FOREACH(pt, parents, entry)
4134 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4135 if (te)
4136 len += strlen(te->name);
4138 *path = calloc(1, len);
4139 if (path == NULL)
4140 return got_error_from_errno("calloc");
4142 (*path)[0] = '/';
4143 pt = TAILQ_LAST(parents, tog_parent_trees);
4144 while (pt) {
4145 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4146 err = got_error(GOT_ERR_NO_SPACE);
4147 goto done;
4149 if (strlcat(*path, "/", len) >= len) {
4150 err = got_error(GOT_ERR_NO_SPACE);
4151 goto done;
4153 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4155 if (te) {
4156 if (strlcat(*path, te->name, len) >= len) {
4157 err = got_error(GOT_ERR_NO_SPACE);
4158 goto done;
4161 done:
4162 if (err) {
4163 free(*path);
4164 *path = NULL;
4166 return err;
4169 static const struct got_error *
4170 blame_tree_entry(struct tog_view **new_view, int begin_x,
4171 struct got_tree_entry *te, struct tog_parent_trees *parents,
4172 struct got_object_id *commit_id, struct got_reflist_head *refs,
4173 struct got_repository *repo)
4175 const struct got_error *err = NULL;
4176 char *path;
4177 struct tog_view *blame_view;
4179 *new_view = NULL;
4181 err = tree_entry_path(&path, parents, te);
4182 if (err)
4183 return err;
4185 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4186 if (blame_view == NULL) {
4187 err = got_error_from_errno("view_open");
4188 goto done;
4191 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4192 if (err) {
4193 if (err->code == GOT_ERR_CANCELLED)
4194 err = NULL;
4195 view_close(blame_view);
4196 } else
4197 *new_view = blame_view;
4198 done:
4199 free(path);
4200 return err;
4203 static const struct got_error *
4204 log_tree_entry(struct tog_view **new_view, int begin_x,
4205 struct got_tree_entry *te, struct tog_parent_trees *parents,
4206 struct got_object_id *commit_id, struct got_reflist_head *refs,
4207 struct got_repository *repo)
4209 struct tog_view *log_view;
4210 const struct got_error *err = NULL;
4211 char *path;
4213 *new_view = NULL;
4215 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4216 if (log_view == NULL)
4217 return got_error_from_errno("view_open");
4219 err = tree_entry_path(&path, parents, te);
4220 if (err)
4221 return err;
4223 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4224 if (err)
4225 view_close(log_view);
4226 else
4227 *new_view = log_view;
4228 free(path);
4229 return err;
4232 static const struct got_error *
4233 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4234 struct got_object_id *commit_id, struct got_reflist_head *refs,
4235 struct got_repository *repo)
4237 const struct got_error *err = NULL;
4238 char *commit_id_str = NULL;
4239 struct tog_tree_view_state *s = &view->state.tree;
4241 TAILQ_INIT(&s->parents);
4243 err = got_object_id_str(&commit_id_str, commit_id);
4244 if (err != NULL)
4245 goto done;
4247 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4248 err = got_error_from_errno("asprintf");
4249 goto done;
4252 s->root = s->tree = root;
4253 s->entries = got_object_tree_get_entries(root);
4254 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4255 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4256 s->commit_id = got_object_id_dup(commit_id);
4257 if (s->commit_id == NULL) {
4258 err = got_error_from_errno("got_object_id_dup");
4259 goto done;
4261 s->refs = refs;
4262 s->repo = repo;
4264 view->show = show_tree_view;
4265 view->input = input_tree_view;
4266 view->close = close_tree_view;
4267 view->search_start = search_start_tree_view;
4268 view->search_next = search_next_tree_view;
4269 done:
4270 free(commit_id_str);
4271 if (err) {
4272 free(s->tree_label);
4273 s->tree_label = NULL;
4275 return err;
4278 static const struct got_error *
4279 close_tree_view(struct tog_view *view)
4281 struct tog_tree_view_state *s = &view->state.tree;
4283 free(s->tree_label);
4284 s->tree_label = NULL;
4285 free(s->commit_id);
4286 s->commit_id = NULL;
4287 while (!TAILQ_EMPTY(&s->parents)) {
4288 struct tog_parent_tree *parent;
4289 parent = TAILQ_FIRST(&s->parents);
4290 TAILQ_REMOVE(&s->parents, parent, entry);
4291 free(parent);
4294 if (s->tree != s->root)
4295 got_object_tree_close(s->tree);
4296 got_object_tree_close(s->root);
4298 return NULL;
4301 static const struct got_error *
4302 search_start_tree_view(struct tog_view *view)
4304 struct tog_tree_view_state *s = &view->state.tree;
4306 s->matched_entry = NULL;
4307 return NULL;
4310 static int
4311 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4313 regmatch_t regmatch;
4315 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4318 static const struct got_error *
4319 search_next_tree_view(struct tog_view *view)
4321 struct tog_tree_view_state *s = &view->state.tree;
4322 struct got_tree_entry *entry = NULL, *te;
4324 if (!view->searching) {
4325 view->search_next_done = 1;
4326 return NULL;
4329 if (s->matched_entry) {
4330 if (view->searching == TOG_SEARCH_FORWARD) {
4331 if (s->selected_entry)
4332 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4333 else
4334 entry = SIMPLEQ_FIRST(&s->entries->head);
4336 else {
4337 if (s->selected_entry == NULL) {
4338 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4339 entry = te;
4340 } else {
4341 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4342 entry = te;
4343 if (SIMPLEQ_NEXT(te, entry) ==
4344 s->selected_entry)
4345 break;
4349 } else {
4350 if (view->searching == TOG_SEARCH_FORWARD)
4351 entry = SIMPLEQ_FIRST(&s->entries->head);
4352 else {
4353 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4354 entry = te;
4358 while (1) {
4359 if (entry == NULL) {
4360 if (s->matched_entry == NULL) {
4361 view->search_next_done = 1;
4362 return NULL;
4364 if (view->searching == TOG_SEARCH_FORWARD)
4365 entry = SIMPLEQ_FIRST(&s->entries->head);
4366 else {
4367 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4368 entry = te;
4372 if (match_tree_entry(entry, &view->regex)) {
4373 view->search_next_done = 1;
4374 s->matched_entry = entry;
4375 break;
4378 if (view->searching == TOG_SEARCH_FORWARD)
4379 entry = SIMPLEQ_NEXT(entry, entry);
4380 else {
4381 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4382 entry = NULL;
4383 else {
4384 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4385 if (SIMPLEQ_NEXT(te, entry) == entry) {
4386 entry = te;
4387 break;
4394 if (s->matched_entry) {
4395 s->first_displayed_entry = s->matched_entry;
4396 s->selected = 0;
4399 return NULL;
4402 static const struct got_error *
4403 show_tree_view(struct tog_view *view)
4405 const struct got_error *err = NULL;
4406 struct tog_tree_view_state *s = &view->state.tree;
4407 char *parent_path;
4409 err = tree_entry_path(&parent_path, &s->parents, NULL);
4410 if (err)
4411 return err;
4413 err = draw_tree_entries(view, &s->first_displayed_entry,
4414 &s->last_displayed_entry, &s->selected_entry,
4415 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4416 s->entries, s->selected, view->nlines, s->tree == s->root);
4417 free(parent_path);
4419 view_vborder(view);
4420 return err;
4423 static const struct got_error *
4424 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4425 struct tog_view **focus_view, struct tog_view *view, int ch)
4427 const struct got_error *err = NULL;
4428 struct tog_tree_view_state *s = &view->state.tree;
4429 struct tog_view *log_view;
4430 int begin_x = 0, nscrolled;
4432 switch (ch) {
4433 case 'i':
4434 s->show_ids = !s->show_ids;
4435 break;
4436 case 'l':
4437 if (!s->selected_entry)
4438 break;
4439 if (view_is_parent_view(view))
4440 begin_x = view_split_begin_x(view->begin_x);
4441 err = log_tree_entry(&log_view, begin_x,
4442 s->selected_entry, &s->parents,
4443 s->commit_id, s->refs, s->repo);
4444 if (view_is_parent_view(view)) {
4445 err = view_close_child(view);
4446 if (err)
4447 return err;
4448 err = view_set_child(view, log_view);
4449 if (err) {
4450 view_close(log_view);
4451 break;
4453 *focus_view = log_view;
4454 view->child_focussed = 1;
4455 } else
4456 *new_view = log_view;
4457 break;
4458 case 'k':
4459 case KEY_UP:
4460 if (s->selected > 0) {
4461 s->selected--;
4462 if (s->selected == 0)
4463 break;
4465 if (s->selected > 0)
4466 break;
4467 tree_scroll_up(view, &s->first_displayed_entry, 1,
4468 s->entries, s->tree == s->root);
4469 break;
4470 case KEY_PPAGE:
4471 tree_scroll_up(view, &s->first_displayed_entry,
4472 MAX(0, view->nlines - 4 - s->selected), s->entries,
4473 s->tree == s->root);
4474 s->selected = 0;
4475 if (SIMPLEQ_FIRST(&s->entries->head) ==
4476 s->first_displayed_entry && s->tree != s->root)
4477 s->first_displayed_entry = NULL;
4478 break;
4479 case 'j':
4480 case KEY_DOWN:
4481 if (s->selected < s->ndisplayed - 1) {
4482 s->selected++;
4483 break;
4485 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4486 /* can't scroll any further */
4487 break;
4488 tree_scroll_down(&s->first_displayed_entry, 1,
4489 s->last_displayed_entry, s->entries);
4490 break;
4491 case KEY_NPAGE:
4492 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4493 == NULL) {
4494 /* can't scroll any further; move cursor down */
4495 if (s->selected < s->ndisplayed - 1)
4496 s->selected = s->ndisplayed - 1;
4497 break;
4499 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4500 view->nlines, s->last_displayed_entry, s->entries);
4501 if (nscrolled < view->nlines) {
4502 int ndisplayed = 0;
4503 struct got_tree_entry *te;
4504 te = s->first_displayed_entry;
4505 do {
4506 ndisplayed++;
4507 te = SIMPLEQ_NEXT(te, entry);
4508 } while (te);
4509 s->selected = ndisplayed - 1;
4511 break;
4512 case KEY_ENTER:
4513 case '\r':
4514 case KEY_BACKSPACE:
4515 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4516 struct tog_parent_tree *parent;
4517 /* user selected '..' */
4518 if (s->tree == s->root)
4519 break;
4520 parent = TAILQ_FIRST(&s->parents);
4521 TAILQ_REMOVE(&s->parents, parent,
4522 entry);
4523 got_object_tree_close(s->tree);
4524 s->tree = parent->tree;
4525 s->entries =
4526 got_object_tree_get_entries(s->tree);
4527 s->first_displayed_entry =
4528 parent->first_displayed_entry;
4529 s->selected_entry =
4530 parent->selected_entry;
4531 s->selected = parent->selected;
4532 free(parent);
4533 } else if (S_ISDIR(s->selected_entry->mode)) {
4534 struct got_tree_object *subtree;
4535 err = got_object_open_as_tree(&subtree,
4536 s->repo, s->selected_entry->id);
4537 if (err)
4538 break;
4539 err = tree_view_visit_subtree(subtree, s);
4540 if (err) {
4541 got_object_tree_close(subtree);
4542 break;
4544 } else if (S_ISREG(s->selected_entry->mode)) {
4545 struct tog_view *blame_view;
4546 int begin_x = view_is_parent_view(view) ?
4547 view_split_begin_x(view->begin_x) : 0;
4549 err = blame_tree_entry(&blame_view, begin_x,
4550 s->selected_entry, &s->parents,
4551 s->commit_id, s->refs, s->repo);
4552 if (err)
4553 break;
4554 if (view_is_parent_view(view)) {
4555 err = view_close_child(view);
4556 if (err)
4557 return err;
4558 err = view_set_child(view, blame_view);
4559 if (err) {
4560 view_close(blame_view);
4561 break;
4563 *focus_view = blame_view;
4564 view->child_focussed = 1;
4565 } else
4566 *new_view = blame_view;
4568 break;
4569 case KEY_RESIZE:
4570 if (s->selected > view->nlines)
4571 s->selected = s->ndisplayed - 1;
4572 break;
4573 default:
4574 break;
4577 return err;
4580 __dead static void
4581 usage_tree(void)
4583 endwin();
4584 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4585 getprogname());
4586 exit(1);
4589 static const struct got_error *
4590 cmd_tree(int argc, char *argv[])
4592 const struct got_error *error;
4593 struct got_repository *repo = NULL;
4594 struct got_reflist_head refs;
4595 char *repo_path = NULL;
4596 struct got_object_id *commit_id = NULL;
4597 char *commit_id_arg = NULL;
4598 struct got_commit_object *commit = NULL;
4599 struct got_tree_object *tree = NULL;
4600 int ch;
4601 struct tog_view *view;
4603 SIMPLEQ_INIT(&refs);
4605 #ifndef PROFILE
4606 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4607 NULL) == -1)
4608 err(1, "pledge");
4609 #endif
4611 while ((ch = getopt(argc, argv, "c:")) != -1) {
4612 switch (ch) {
4613 case 'c':
4614 commit_id_arg = optarg;
4615 break;
4616 default:
4617 usage_tree();
4618 /* NOTREACHED */
4622 argc -= optind;
4623 argv += optind;
4625 if (argc == 0) {
4626 struct got_worktree *worktree;
4627 char *cwd = getcwd(NULL, 0);
4628 if (cwd == NULL)
4629 return got_error_from_errno("getcwd");
4630 error = got_worktree_open(&worktree, cwd);
4631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4632 goto done;
4633 if (worktree) {
4634 free(cwd);
4635 repo_path =
4636 strdup(got_worktree_get_repo_path(worktree));
4637 got_worktree_close(worktree);
4638 } else
4639 repo_path = cwd;
4640 if (repo_path == NULL) {
4641 error = got_error_from_errno("strdup");
4642 goto done;
4644 } else if (argc == 1) {
4645 repo_path = realpath(argv[0], NULL);
4646 if (repo_path == NULL)
4647 return got_error_from_errno2("realpath", argv[0]);
4648 } else
4649 usage_log();
4651 init_curses();
4653 error = got_repo_open(&repo, repo_path, NULL);
4654 if (error != NULL)
4655 goto done;
4657 error = apply_unveil(got_repo_get_path(repo), NULL);
4658 if (error)
4659 goto done;
4661 if (commit_id_arg == NULL)
4662 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4663 else {
4664 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4665 if (error) {
4666 if (error->code != GOT_ERR_NOT_REF)
4667 goto done;
4668 error = got_repo_match_object_id_prefix(&commit_id,
4669 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4672 if (error != NULL)
4673 goto done;
4675 error = got_object_open_as_commit(&commit, repo, commit_id);
4676 if (error != NULL)
4677 goto done;
4679 error = got_object_open_as_tree(&tree, repo,
4680 got_object_commit_get_tree_id(commit));
4681 if (error != NULL)
4682 goto done;
4684 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4685 if (error)
4686 goto done;
4688 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4689 if (view == NULL) {
4690 error = got_error_from_errno("view_open");
4691 goto done;
4693 error = open_tree_view(view, tree, commit_id, &refs, repo);
4694 if (error)
4695 goto done;
4696 error = view_loop(view);
4697 done:
4698 free(repo_path);
4699 free(commit_id);
4700 if (commit)
4701 got_object_commit_close(commit);
4702 if (tree)
4703 got_object_tree_close(tree);
4704 if (repo)
4705 got_repo_close(repo);
4706 got_ref_list_free(&refs);
4707 return error;
4710 static void
4711 list_commands(void)
4713 int i;
4715 fprintf(stderr, "commands:");
4716 for (i = 0; i < nitems(tog_commands); i++) {
4717 struct tog_cmd *cmd = &tog_commands[i];
4718 fprintf(stderr, " %s", cmd->name);
4720 fputc('\n', stderr);
4723 __dead static void
4724 usage(int hflag)
4726 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4727 getprogname());
4728 if (hflag)
4729 list_commands();
4730 exit(1);
4733 static char **
4734 make_argv(const char *arg0, const char *arg1)
4736 char **argv;
4737 int argc = (arg1 == NULL ? 1 : 2);
4739 argv = calloc(argc, sizeof(char *));
4740 if (argv == NULL)
4741 err(1, "calloc");
4742 argv[0] = strdup(arg0);
4743 if (argv[0] == NULL)
4744 err(1, "strdup");
4745 if (arg1) {
4746 argv[1] = strdup(arg1);
4747 if (argv[1] == NULL)
4748 err(1, "strdup");
4751 return argv;
4754 int
4755 main(int argc, char *argv[])
4757 const struct got_error *error = NULL;
4758 struct tog_cmd *cmd = NULL;
4759 int ch, hflag = 0, Vflag = 0;
4760 char **cmd_argv = NULL;
4762 setlocale(LC_CTYPE, "");
4764 while ((ch = getopt(argc, argv, "hV")) != -1) {
4765 switch (ch) {
4766 case 'h':
4767 hflag = 1;
4768 break;
4769 case 'V':
4770 Vflag = 1;
4771 break;
4772 default:
4773 usage(hflag);
4774 /* NOTREACHED */
4778 argc -= optind;
4779 argv += optind;
4780 optind = 0;
4781 optreset = 1;
4783 if (Vflag) {
4784 got_version_print_str();
4785 return 1;
4788 if (argc == 0) {
4789 if (hflag)
4790 usage(hflag);
4791 /* Build an argument vector which runs a default command. */
4792 cmd = &tog_commands[0];
4793 cmd_argv = make_argv(cmd->name, NULL);
4794 argc = 1;
4795 } else {
4796 int i;
4798 /* Did the user specific a command? */
4799 for (i = 0; i < nitems(tog_commands); i++) {
4800 if (strncmp(tog_commands[i].name, argv[0],
4801 strlen(argv[0])) == 0) {
4802 cmd = &tog_commands[i];
4803 break;
4807 if (cmd == NULL) {
4808 fprintf(stderr, "%s: unknown command '%s'\n",
4809 getprogname(), argv[0]);
4810 list_commands();
4811 return 1;
4815 if (hflag)
4816 cmd->cmd_usage();
4817 else
4818 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4820 endwin();
4821 free(cmd_argv);
4822 if (error && error->code != GOT_ERR_CANCELLED)
4823 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4824 return 0;