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_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_diff.h"
46 #include "got_opentemp.h"
47 #include "got_commit_graph.h"
48 #include "got_utf8.h"
49 #include "got_blame.h"
50 #include "got_privsep.h"
51 #include "got_path.h"
52 #include "got_worktree.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 #define CTRL(x) ((x) & 0x1f)
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct tog_cmd {
69 const char *name;
70 const struct got_error *(*cmd_main)(int, char *[]);
71 void (*cmd_usage)(void);
72 };
74 __dead static void usage(void);
75 __dead static void usage_log(void);
76 __dead static void usage_diff(void);
77 __dead static void usage_blame(void);
78 __dead static void usage_tree(void);
80 static const struct got_error* cmd_log(int, char *[]);
81 static const struct got_error* cmd_diff(int, char *[]);
82 static const struct got_error* cmd_blame(int, char *[]);
83 static const struct got_error* cmd_tree(int, char *[]);
85 static struct tog_cmd tog_commands[] = {
86 { "log", cmd_log, usage_log },
87 { "diff", cmd_diff, usage_diff },
88 { "blame", cmd_blame, usage_blame },
89 { "tree", cmd_tree, usage_tree },
90 };
92 enum tog_view_type {
93 TOG_VIEW_DIFF,
94 TOG_VIEW_LOG,
95 TOG_VIEW_BLAME,
96 TOG_VIEW_TREE
97 };
99 #define TOG_EOF_STRING "(END)"
101 struct commit_queue_entry {
102 TAILQ_ENTRY(commit_queue_entry) entry;
103 struct got_object_id *id;
104 struct got_commit_object *commit;
105 int idx;
106 };
107 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
108 struct commit_queue {
109 int ncommits;
110 struct commit_queue_head head;
111 };
113 struct tog_diff_view_state {
114 struct got_object_id *id1, *id2;
115 FILE *f;
116 int first_displayed_line;
117 int last_displayed_line;
118 int eof;
119 int diff_context;
120 struct got_repository *repo;
121 struct got_reflist_head *refs;
123 /* passed from log view; may be NULL */
124 struct tog_view *log_view;
125 };
127 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
129 struct tog_log_thread_args {
130 pthread_cond_t need_commits;
131 int commits_needed;
132 struct got_commit_graph *graph;
133 struct commit_queue *commits;
134 const char *in_repo_path;
135 struct got_object_id *start_id;
136 struct got_repository *repo;
137 int log_complete;
138 sig_atomic_t *quit;
139 struct tog_view *view;
140 struct commit_queue_entry **first_displayed_entry;
141 struct commit_queue_entry **selected_entry;
142 };
144 struct tog_log_view_state {
145 struct commit_queue commits;
146 struct commit_queue_entry *first_displayed_entry;
147 struct commit_queue_entry *last_displayed_entry;
148 struct commit_queue_entry *selected_entry;
149 int selected;
150 char *in_repo_path;
151 struct got_repository *repo;
152 struct got_reflist_head *refs;
153 struct got_object_id *start_id;
154 sig_atomic_t quit;
155 pthread_t thread;
156 struct tog_log_thread_args thread_args;
157 struct commit_queue_entry *matched_entry;
158 };
160 struct tog_blame_cb_args {
161 struct tog_blame_line *lines; /* one per line */
162 int nlines;
164 struct tog_view *view;
165 struct got_object_id *commit_id;
166 int *quit;
167 };
169 struct tog_blame_thread_args {
170 const char *path;
171 struct got_repository *repo;
172 struct tog_blame_cb_args *cb_args;
173 int *complete;
174 };
176 struct tog_blame {
177 FILE *f;
178 size_t filesize;
179 struct tog_blame_line *lines;
180 int nlines;
181 off_t *line_offsets;
182 pthread_t thread;
183 struct tog_blame_thread_args thread_args;
184 struct tog_blame_cb_args cb_args;
185 const char *path;
186 };
188 struct tog_blame_view_state {
189 int first_displayed_line;
190 int last_displayed_line;
191 int selected_line;
192 int blame_complete;
193 int eof;
194 int done;
195 struct got_object_id_queue blamed_commits;
196 struct got_object_qid *blamed_commit;
197 char *path;
198 struct got_repository *repo;
199 struct got_reflist_head *refs;
200 struct got_object_id *commit_id;
201 struct tog_blame blame;
202 int matched_line;
203 };
205 struct tog_parent_tree {
206 TAILQ_ENTRY(tog_parent_tree) entry;
207 struct got_tree_object *tree;
208 struct got_tree_entry *first_displayed_entry;
209 struct got_tree_entry *selected_entry;
210 int selected;
211 };
213 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
215 struct tog_tree_view_state {
216 char *tree_label;
217 struct got_tree_object *root;
218 struct got_tree_object *tree;
219 const struct got_tree_entries *entries;
220 struct got_tree_entry *first_displayed_entry;
221 struct got_tree_entry *last_displayed_entry;
222 struct got_tree_entry *selected_entry;
223 int ndisplayed, selected, show_ids;
224 struct tog_parent_trees parents;
225 struct got_object_id *commit_id;
226 struct got_repository *repo;
227 struct got_reflist_head *refs;
228 struct got_tree_entry *matched_entry;
229 };
231 /*
232 * We implement two types of views: parent views and child views.
234 * The 'Tab' key switches between a parent view and its child view.
235 * Child views are shown side-by-side to their parent view, provided
236 * there is enough screen estate.
238 * When a new view is opened from within a parent view, this new view
239 * becomes a child view of the parent view, replacing any existing child.
241 * When a new view is opened from within a child view, this new view
242 * becomes a parent view which will obscure the views below until the
243 * user quits the new parent view by typing 'q'.
245 * This list of views contains parent views only.
246 * Child views are only pointed to by their parent view.
247 */
248 TAILQ_HEAD(tog_view_list_head, tog_view);
250 struct tog_view {
251 TAILQ_ENTRY(tog_view) entry;
252 WINDOW *window;
253 PANEL *panel;
254 int nlines, ncols, begin_y, begin_x;
255 int lines, cols; /* copies of LINES and COLS */
256 int focussed;
257 struct tog_view *parent;
258 struct tog_view *child;
259 int child_focussed;
261 /* type-specific state */
262 enum tog_view_type type;
263 union {
264 struct tog_diff_view_state diff;
265 struct tog_log_view_state log;
266 struct tog_blame_view_state blame;
267 struct tog_tree_view_state tree;
268 } state;
270 const struct got_error *(*show)(struct tog_view *);
271 const struct got_error *(*input)(struct tog_view **,
272 struct tog_view **, struct tog_view**, struct tog_view *, int);
273 const struct got_error *(*close)(struct tog_view *);
275 const struct got_error *(*search_start)(struct tog_view *);
276 const struct got_error *(*search_next)(struct tog_view *);
277 int searching;
278 #define TOG_SEARCH_FORWARD 1
279 #define TOG_SEARCH_BACKWARD 2
280 int search_next_done;
281 regex_t regex;
282 };
284 static const struct got_error *open_diff_view(struct tog_view *,
285 struct got_object_id *, struct got_object_id *, struct tog_view *,
286 struct got_reflist_head *, struct got_repository *);
287 static const struct got_error *show_diff_view(struct tog_view *);
288 static const struct got_error *input_diff_view(struct tog_view **,
289 struct tog_view **, struct tog_view **, struct tog_view *, int);
290 static const struct got_error* close_diff_view(struct tog_view *);
292 static const struct got_error *open_log_view(struct tog_view *,
293 struct got_object_id *, struct got_reflist_head *,
294 struct got_repository *, const char *, int);
295 static const struct got_error * show_log_view(struct tog_view *);
296 static const struct got_error *input_log_view(struct tog_view **,
297 struct tog_view **, struct tog_view **, struct tog_view *, int);
298 static const struct got_error *close_log_view(struct tog_view *);
299 static const struct got_error *search_start_log_view(struct tog_view *);
300 static const struct got_error *search_next_log_view(struct tog_view *);
302 static const struct got_error *open_blame_view(struct tog_view *, char *,
303 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
304 static const struct got_error *show_blame_view(struct tog_view *);
305 static const struct got_error *input_blame_view(struct tog_view **,
306 struct tog_view **, struct tog_view **, struct tog_view *, int);
307 static const struct got_error *close_blame_view(struct tog_view *);
308 static const struct got_error *search_start_blame_view(struct tog_view *);
309 static const struct got_error *search_next_blame_view(struct tog_view *);
311 static const struct got_error *open_tree_view(struct tog_view *,
312 struct got_tree_object *, struct got_object_id *,
313 struct got_reflist_head *, struct got_repository *);
314 static const struct got_error *show_tree_view(struct tog_view *);
315 static const struct got_error *input_tree_view(struct tog_view **,
316 struct tog_view **, struct tog_view **, struct tog_view *, int);
317 static const struct got_error *close_tree_view(struct tog_view *);
318 static const struct got_error *search_start_tree_view(struct tog_view *);
319 static const struct got_error *search_next_tree_view(struct tog_view *);
321 static volatile sig_atomic_t tog_sigwinch_received;
323 static void
324 tog_sigwinch(int signo)
326 tog_sigwinch_received = 1;
329 static const struct got_error *
330 view_close(struct tog_view *view)
332 const struct got_error *err = NULL;
334 if (view->child) {
335 view_close(view->child);
336 view->child = NULL;
338 if (view->close)
339 err = view->close(view);
340 if (view->panel)
341 del_panel(view->panel);
342 if (view->window)
343 delwin(view->window);
344 free(view);
345 return err;
348 static struct tog_view *
349 view_open(int nlines, int ncols, int begin_y, int begin_x,
350 enum tog_view_type type)
352 struct tog_view *view = calloc(1, sizeof(*view));
354 if (view == NULL)
355 return NULL;
357 view->type = type;
358 view->lines = LINES;
359 view->cols = COLS;
360 view->nlines = nlines ? nlines : LINES - begin_y;
361 view->ncols = ncols ? ncols : COLS - begin_x;
362 view->begin_y = begin_y;
363 view->begin_x = begin_x;
364 view->window = newwin(nlines, ncols, begin_y, begin_x);
365 if (view->window == NULL) {
366 view_close(view);
367 return NULL;
369 view->panel = new_panel(view->window);
370 if (view->panel == NULL ||
371 set_panel_userptr(view->panel, view) != OK) {
372 view_close(view);
373 return NULL;
376 keypad(view->window, TRUE);
377 return view;
380 static int
381 view_split_begin_x(int begin_x)
383 if (begin_x > 0 || COLS < 120)
384 return 0;
385 return (COLS - MAX(COLS / 2, 80));
388 static const struct got_error *view_resize(struct tog_view *);
390 static const struct got_error *
391 view_splitscreen(struct tog_view *view)
393 const struct got_error *err = NULL;
395 view->begin_y = 0;
396 view->begin_x = view_split_begin_x(0);
397 view->nlines = LINES;
398 view->ncols = COLS - view->begin_x;
399 view->lines = LINES;
400 view->cols = COLS;
401 err = view_resize(view);
402 if (err)
403 return err;
405 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
406 return got_error_from_errno("mvwin");
408 return NULL;
411 static const struct got_error *
412 view_fullscreen(struct tog_view *view)
414 const struct got_error *err = NULL;
416 view->begin_x = 0;
417 view->begin_y = 0;
418 view->nlines = LINES;
419 view->ncols = COLS;
420 view->lines = LINES;
421 view->cols = COLS;
422 err = view_resize(view);
423 if (err)
424 return err;
426 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
427 return got_error_from_errno("mvwin");
429 return NULL;
432 static int
433 view_is_parent_view(struct tog_view *view)
435 return view->parent == NULL;
438 static const struct got_error *
439 view_resize(struct tog_view *view)
441 int nlines, ncols;
443 if (view->lines > LINES)
444 nlines = view->nlines - (view->lines - LINES);
445 else
446 nlines = view->nlines + (LINES - view->lines);
448 if (view->cols > COLS)
449 ncols = view->ncols - (view->cols - COLS);
450 else
451 ncols = view->ncols + (COLS - view->cols);
453 if (wresize(view->window, nlines, ncols) == ERR)
454 return got_error_from_errno("wresize");
455 if (replace_panel(view->panel, view->window) == ERR)
456 return got_error_from_errno("replace_panel");
457 wclear(view->window);
459 view->nlines = nlines;
460 view->ncols = ncols;
461 view->lines = LINES;
462 view->cols = COLS;
464 if (view->child) {
465 view->child->begin_x = view_split_begin_x(view->begin_x);
466 if (view->child->begin_x == 0) {
467 view_fullscreen(view->child);
468 if (view->child->focussed)
469 show_panel(view->child->panel);
470 else
471 show_panel(view->panel);
472 } else {
473 view_splitscreen(view->child);
474 show_panel(view->child->panel);
478 return NULL;
481 static const struct got_error *
482 view_close_child(struct tog_view *view)
484 const struct got_error *err = NULL;
486 if (view->child == NULL)
487 return NULL;
489 err = view_close(view->child);
490 view->child = NULL;
491 return err;
494 static const struct got_error *
495 view_set_child(struct tog_view *view, struct tog_view *child)
497 const struct got_error *err = NULL;
499 view->child = child;
500 child->parent = view;
501 return err;
504 static int
505 view_is_splitscreen(struct tog_view *view)
507 return view->begin_x > 0;
510 static void
511 tog_resizeterm(void)
513 int cols, lines;
514 struct winsize size;
516 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
517 cols = 80; /* Default */
518 lines = 24;
519 } else {
520 cols = size.ws_col;
521 lines = size.ws_row;
523 resize_term(lines, cols);
526 static const struct got_error *
527 view_search_start(struct tog_view *view)
529 const struct got_error *err = NULL;
530 char pattern[1024];
531 int ret;
533 if (view->nlines < 1)
534 return NULL;
536 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
537 view->begin_x, "/");
538 wclrtoeol(view->window);
540 nocbreak();
541 echo();
542 ret = wgetnstr(view->window, pattern, sizeof(pattern));
543 cbreak();
544 noecho();
545 if (ret == ERR)
546 return NULL;
548 if (view->searching) {
549 regfree(&view->regex);
550 view->searching = 0;
553 if (regcomp(&view->regex, pattern,
554 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
555 err = view->search_start(view);
556 if (err) {
557 regfree(&view->regex);
558 return err;
560 view->searching = TOG_SEARCH_FORWARD;
561 view->search_next_done = 0;
562 view->search_next(view);
565 return NULL;
568 static const struct got_error *
569 view_input(struct tog_view **new, struct tog_view **dead,
570 struct tog_view **focus, int *done, struct tog_view *view,
571 struct tog_view_list_head *views)
573 const struct got_error *err = NULL;
574 struct tog_view *v;
575 int ch, errcode;
577 *new = NULL;
578 *dead = NULL;
579 *focus = NULL;
581 if (view->searching && !view->search_next_done) {
582 errcode = pthread_mutex_unlock(&tog_mutex);
583 if (errcode)
584 return got_error_set_errno(errcode,
585 "pthread_mutex_unlock");
586 pthread_yield();
587 errcode = pthread_mutex_lock(&tog_mutex);
588 if (errcode)
589 return got_error_set_errno(errcode,
590 "pthread_mutex_lock");
591 view->search_next(view);
592 return NULL;
595 nodelay(stdscr, FALSE);
596 /* Allow threads to make progress while we are waiting for input. */
597 errcode = pthread_mutex_unlock(&tog_mutex);
598 if (errcode)
599 return got_error_set_errno(errcode, "pthread_mutex_unlock");
600 ch = wgetch(view->window);
601 errcode = pthread_mutex_lock(&tog_mutex);
602 if (errcode)
603 return got_error_set_errno(errcode, "pthread_mutex_lock");
604 nodelay(stdscr, TRUE);
606 if (tog_sigwinch_received) {
607 tog_resizeterm();
608 tog_sigwinch_received = 0;
609 TAILQ_FOREACH(v, views, entry) {
610 err = view_resize(v);
611 if (err)
612 return err;
613 err = v->input(new, dead, focus, v, KEY_RESIZE);
614 if (err)
615 return err;
619 switch (ch) {
620 case ERR:
621 break;
622 case '\t':
623 if (view->child) {
624 *focus = view->child;
625 view->child_focussed = 1;
626 } else if (view->parent) {
627 *focus = view->parent;
628 view->parent->child_focussed = 0;
630 break;
631 case 'q':
632 err = view->input(new, dead, focus, view, ch);
633 *dead = view;
634 break;
635 case 'Q':
636 *done = 1;
637 break;
638 case 'f':
639 if (view_is_parent_view(view)) {
640 if (view->child == NULL)
641 break;
642 if (view_is_splitscreen(view->child)) {
643 *focus = view->child;
644 view->child_focussed = 1;
645 err = view_fullscreen(view->child);
646 } else
647 err = view_splitscreen(view->child);
648 if (err)
649 break;
650 err = view->child->input(new, dead, focus,
651 view->child, KEY_RESIZE);
652 } else {
653 if (view_is_splitscreen(view)) {
654 *focus = view;
655 view->parent->child_focussed = 1;
656 err = view_fullscreen(view);
657 } else {
658 err = view_splitscreen(view);
660 if (err)
661 break;
662 err = view->input(new, dead, focus, view,
663 KEY_RESIZE);
665 break;
666 case KEY_RESIZE:
667 break;
668 case '/':
669 if (view->search_start)
670 view_search_start(view);
671 else
672 err = view->input(new, dead, focus, view, ch);
673 break;
674 case 'N':
675 case 'n':
676 if (view->search_next && view->searching) {
677 view->searching = (ch == 'n' ?
678 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
679 view->search_next_done = 0;
680 view->search_next(view);
681 } else
682 err = view->input(new, dead, focus, view, ch);
683 break;
684 default:
685 err = view->input(new, dead, focus, view, ch);
686 break;
689 return err;
692 void
693 view_vborder(struct tog_view *view)
695 PANEL *panel;
696 struct tog_view *view_above;
698 if (view->parent)
699 return view_vborder(view->parent);
701 panel = panel_above(view->panel);
702 if (panel == NULL)
703 return;
705 view_above = panel_userptr(panel);
706 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
707 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
710 int
711 view_needs_focus_indication(struct tog_view *view)
713 if (view_is_parent_view(view)) {
714 if (view->child == NULL || view->child_focussed)
715 return 0;
716 if (!view_is_splitscreen(view->child))
717 return 0;
718 } else if (!view_is_splitscreen(view))
719 return 0;
721 return view->focussed;
724 static const struct got_error *
725 view_loop(struct tog_view *view)
727 const struct got_error *err = NULL;
728 struct tog_view_list_head views;
729 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
730 int fast_refresh = 10;
731 int done = 0, errcode;
733 errcode = pthread_mutex_lock(&tog_mutex);
734 if (errcode)
735 return got_error_set_errno(errcode, "pthread_mutex_lock");
737 TAILQ_INIT(&views);
738 TAILQ_INSERT_HEAD(&views, view, entry);
740 main_view = view;
741 view->focussed = 1;
742 err = view->show(view);
743 if (err)
744 return err;
745 update_panels();
746 doupdate();
747 while (!TAILQ_EMPTY(&views) && !done) {
748 /* Refresh fast during initialization, then become slower. */
749 if (fast_refresh && fast_refresh-- == 0)
750 halfdelay(10); /* switch to once per second */
752 err = view_input(&new_view, &dead_view, &focus_view, &done,
753 view, &views);
754 if (err)
755 break;
756 if (dead_view) {
757 struct tog_view *prev = NULL;
759 if (view_is_parent_view(dead_view))
760 prev = TAILQ_PREV(dead_view,
761 tog_view_list_head, entry);
762 else if (view->parent != dead_view)
763 prev = view->parent;
765 if (dead_view->parent)
766 dead_view->parent->child = NULL;
767 else
768 TAILQ_REMOVE(&views, dead_view, entry);
770 err = view_close(dead_view);
771 if (err || dead_view == main_view)
772 goto done;
774 if (view == dead_view) {
775 if (focus_view)
776 view = focus_view;
777 else if (prev)
778 view = prev;
779 else if (!TAILQ_EMPTY(&views))
780 view = TAILQ_LAST(&views,
781 tog_view_list_head);
782 else
783 view = NULL;
784 if (view) {
785 if (view->child && view->child_focussed)
786 focus_view = view->child;
787 else
788 focus_view = view;
792 if (new_view) {
793 struct tog_view *v, *t;
794 /* Only allow one parent view per type. */
795 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
796 if (v->type != new_view->type)
797 continue;
798 TAILQ_REMOVE(&views, v, entry);
799 err = view_close(v);
800 if (err)
801 goto done;
802 break;
804 TAILQ_INSERT_TAIL(&views, new_view, entry);
805 view = new_view;
806 if (focus_view == NULL)
807 focus_view = new_view;
809 if (focus_view) {
810 show_panel(focus_view->panel);
811 if (view)
812 view->focussed = 0;
813 focus_view->focussed = 1;
814 view = focus_view;
815 if (new_view)
816 show_panel(new_view->panel);
817 if (view->child && view_is_splitscreen(view->child))
818 show_panel(view->child->panel);
820 if (view) {
821 if (focus_view == NULL) {
822 view->focussed = 1;
823 show_panel(view->panel);
824 if (view->child && view_is_splitscreen(view->child))
825 show_panel(view->child->panel);
826 focus_view = view;
828 if (view->parent) {
829 err = view->parent->show(view->parent);
830 if (err)
831 goto done;
833 err = view->show(view);
834 if (err)
835 goto done;
836 if (view->child) {
837 err = view->child->show(view->child);
838 if (err)
839 goto done;
841 update_panels();
842 doupdate();
845 done:
846 while (!TAILQ_EMPTY(&views)) {
847 view = TAILQ_FIRST(&views);
848 TAILQ_REMOVE(&views, view, entry);
849 view_close(view);
852 errcode = pthread_mutex_unlock(&tog_mutex);
853 if (errcode)
854 return got_error_set_errno(errcode, "pthread_mutex_unlock");
856 return err;
859 __dead static void
860 usage_log(void)
862 endwin();
863 fprintf(stderr,
864 "usage: %s log [-c commit] [-r repository-path] [path]\n",
865 getprogname());
866 exit(1);
869 /* Create newly allocated wide-character string equivalent to a byte string. */
870 static const struct got_error *
871 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
873 char *vis = NULL;
874 const struct got_error *err = NULL;
876 *ws = NULL;
877 *wlen = mbstowcs(NULL, s, 0);
878 if (*wlen == (size_t)-1) {
879 int vislen;
880 if (errno != EILSEQ)
881 return got_error_from_errno("mbstowcs");
883 /* byte string invalid in current encoding; try to "fix" it */
884 err = got_mbsavis(&vis, &vislen, s);
885 if (err)
886 return err;
887 *wlen = mbstowcs(NULL, vis, 0);
888 if (*wlen == (size_t)-1) {
889 err = got_error_from_errno("mbstowcs"); /* give up */
890 goto done;
894 *ws = calloc(*wlen + 1, sizeof(*ws));
895 if (*ws == NULL) {
896 err = got_error_from_errno("calloc");
897 goto done;
900 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
901 err = got_error_from_errno("mbstowcs");
902 done:
903 free(vis);
904 if (err) {
905 free(*ws);
906 *ws = NULL;
907 *wlen = 0;
909 return err;
912 /* Format a line for display, ensuring that it won't overflow a width limit. */
913 static const struct got_error *
914 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
916 const struct got_error *err = NULL;
917 int cols = 0;
918 wchar_t *wline = NULL;
919 size_t wlen;
920 int i;
922 *wlinep = NULL;
923 *widthp = 0;
925 err = mbs2ws(&wline, &wlen, line);
926 if (err)
927 return err;
929 i = 0;
930 while (i < wlen && cols < wlimit) {
931 int width = wcwidth(wline[i]);
932 switch (width) {
933 case 0:
934 i++;
935 break;
936 case 1:
937 case 2:
938 if (cols + width <= wlimit)
939 cols += width;
940 i++;
941 break;
942 case -1:
943 if (wline[i] == L'\t')
944 cols += TABSIZE - ((cols + 1) % TABSIZE);
945 i++;
946 break;
947 default:
948 err = got_error_from_errno("wcwidth");
949 goto done;
952 wline[i] = L'\0';
953 if (widthp)
954 *widthp = cols;
955 done:
956 if (err)
957 free(wline);
958 else
959 *wlinep = wline;
960 return err;
963 static const struct got_error*
964 build_refs_str(char **refs_str, struct got_reflist_head *refs,
965 struct got_object_id *id)
967 static const struct got_error *err = NULL;
968 struct got_reflist_entry *re;
969 char *s;
970 const char *name;
972 *refs_str = NULL;
974 SIMPLEQ_FOREACH(re, refs, entry) {
975 if (got_object_id_cmp(re->id, id) != 0)
976 continue;
977 name = got_ref_get_name(re->ref);
978 if (strcmp(name, GOT_REF_HEAD) == 0)
979 continue;
980 if (strncmp(name, "refs/", 5) == 0)
981 name += 5;
982 if (strncmp(name, "got/", 4) == 0)
983 continue;
984 if (strncmp(name, "heads/", 6) == 0)
985 name += 6;
986 if (strncmp(name, "remotes/", 8) == 0)
987 name += 8;
988 s = *refs_str;
989 if (asprintf(refs_str, "%s%s%s", s ? s : "",
990 s ? ", " : "", name) == -1) {
991 err = got_error_from_errno("asprintf");
992 free(s);
993 *refs_str = NULL;
994 break;
996 free(s);
999 return err;
1002 static const struct got_error *
1003 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1005 char *smallerthan, *at;
1007 smallerthan = strchr(author, '<');
1008 if (smallerthan && smallerthan[1] != '\0')
1009 author = smallerthan + 1;
1010 at = strchr(author, '@');
1011 if (at)
1012 *at = '\0';
1013 return format_line(wauthor, author_width, author, limit);
1016 static const struct got_error *
1017 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1018 struct got_object_id *id, struct got_reflist_head *refs,
1019 int author_display_cols)
1021 const struct got_error *err = NULL;
1022 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1023 char *logmsg0 = NULL, *logmsg = NULL;
1024 char *author = NULL;
1025 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1026 int author_width, logmsg_width;
1027 char *newline, *line = NULL;
1028 int col, limit;
1029 static const size_t date_display_cols = 9;
1030 const int avail = view->ncols;
1031 struct tm tm;
1032 time_t committer_time;
1034 committer_time = got_object_commit_get_committer_time(commit);
1035 if (localtime_r(&committer_time, &tm) == NULL)
1036 return got_error_from_errno("localtime_r");
1037 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1038 >= sizeof(datebuf))
1039 return got_error(GOT_ERR_NO_SPACE);
1041 if (avail < date_display_cols)
1042 limit = MIN(sizeof(datebuf) - 1, avail);
1043 else
1044 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1045 waddnstr(view->window, datebuf, limit);
1046 col = limit + 1;
1047 if (col > avail)
1048 goto done;
1050 author = strdup(got_object_commit_get_author(commit));
1051 if (author == NULL) {
1052 err = got_error_from_errno("strdup");
1053 goto done;
1055 err = format_author(&wauthor, &author_width, author, avail - col);
1056 if (err)
1057 goto done;
1058 waddwstr(view->window, wauthor);
1059 col += author_width;
1060 while (col <= avail && author_width < author_display_cols + 2) {
1061 waddch(view->window, ' ');
1062 col++;
1063 author_width++;
1065 if (col > avail)
1066 goto done;
1068 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1069 if (logmsg0 == NULL) {
1070 err = got_error_from_errno("strdup");
1071 goto done;
1073 logmsg = logmsg0;
1074 while (*logmsg == '\n')
1075 logmsg++;
1076 newline = strchr(logmsg, '\n');
1077 if (newline)
1078 *newline = '\0';
1079 limit = avail - col;
1080 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1081 if (err)
1082 goto done;
1083 waddwstr(view->window, wlogmsg);
1084 col += logmsg_width;
1085 while (col <= avail) {
1086 waddch(view->window, ' ');
1087 col++;
1089 done:
1090 free(logmsg0);
1091 free(wlogmsg);
1092 free(author);
1093 free(wauthor);
1094 free(line);
1095 return err;
1098 static struct commit_queue_entry *
1099 alloc_commit_queue_entry(struct got_commit_object *commit,
1100 struct got_object_id *id)
1102 struct commit_queue_entry *entry;
1104 entry = calloc(1, sizeof(*entry));
1105 if (entry == NULL)
1106 return NULL;
1108 entry->id = id;
1109 entry->commit = commit;
1110 return entry;
1113 static void
1114 pop_commit(struct commit_queue *commits)
1116 struct commit_queue_entry *entry;
1118 entry = TAILQ_FIRST(&commits->head);
1119 TAILQ_REMOVE(&commits->head, entry, entry);
1120 got_object_commit_close(entry->commit);
1121 commits->ncommits--;
1122 /* Don't free entry->id! It is owned by the commit graph. */
1123 free(entry);
1126 static void
1127 free_commits(struct commit_queue *commits)
1129 while (!TAILQ_EMPTY(&commits->head))
1130 pop_commit(commits);
1133 static const struct got_error *
1134 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1135 int minqueue, struct got_repository *repo, const char *path)
1137 const struct got_error *err = NULL;
1138 int nqueued = 0;
1141 * We keep all commits open throughout the lifetime of the log
1142 * view in order to avoid having to re-fetch commits from disk
1143 * while updating the display.
1145 while (nqueued < minqueue) {
1146 struct got_object_id *id;
1147 struct got_commit_object *commit;
1148 struct commit_queue_entry *entry;
1149 int errcode;
1151 err = got_commit_graph_iter_next(&id, graph);
1152 if (err) {
1153 if (err->code != GOT_ERR_ITER_NEED_MORE)
1154 break;
1155 err = got_commit_graph_fetch_commits(graph,
1156 minqueue, repo);
1157 if (err)
1158 return err;
1159 continue;
1162 if (id == NULL)
1163 break;
1165 err = got_object_open_as_commit(&commit, repo, id);
1166 if (err)
1167 break;
1168 entry = alloc_commit_queue_entry(commit, id);
1169 if (entry == NULL) {
1170 err = got_error_from_errno("alloc_commit_queue_entry");
1171 break;
1174 errcode = pthread_mutex_lock(&tog_mutex);
1175 if (errcode) {
1176 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1177 break;
1180 entry->idx = commits->ncommits;
1181 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1182 nqueued++;
1183 commits->ncommits++;
1185 errcode = pthread_mutex_unlock(&tog_mutex);
1186 if (errcode && err == NULL)
1187 err = got_error_set_errno(errcode,
1188 "pthread_mutex_unlock");
1191 return err;
1194 static const struct got_error *
1195 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1196 struct got_repository *repo)
1198 const struct got_error *err = NULL;
1199 struct got_reference *head_ref;
1201 *head_id = NULL;
1203 err = got_ref_open(&head_ref, repo, branch_name, 0);
1204 if (err)
1205 return err;
1207 err = got_ref_resolve(head_id, repo, head_ref);
1208 got_ref_close(head_ref);
1209 if (err) {
1210 *head_id = NULL;
1211 return err;
1214 return NULL;
1217 static const struct got_error *
1218 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1219 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1220 struct commit_queue *commits, int selected_idx, int limit,
1221 struct got_reflist_head *refs, const char *path, int commits_needed)
1223 const struct got_error *err = NULL;
1224 struct commit_queue_entry *entry;
1225 int width;
1226 int ncommits, author_cols = 10;
1227 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1228 char *refs_str = NULL;
1229 wchar_t *wline;
1231 entry = first;
1232 ncommits = 0;
1233 while (entry) {
1234 if (ncommits == selected_idx) {
1235 *selected = entry;
1236 break;
1238 entry = TAILQ_NEXT(entry, entry);
1239 ncommits++;
1242 if (*selected && !(view->searching && view->search_next_done == 0)) {
1243 err = got_object_id_str(&id_str, (*selected)->id);
1244 if (err)
1245 return err;
1246 if (refs) {
1247 err = build_refs_str(&refs_str, refs, (*selected)->id);
1248 if (err)
1249 goto done;
1253 if (commits_needed == 0)
1254 halfdelay(10); /* disable fast refresh */
1256 if (asprintf(&ncommits_str, " [%d/%d] %s",
1257 entry ? entry->idx + 1 : 0, commits->ncommits,
1258 commits_needed > 0 ?
1259 (view->searching && view->search_next_done == 0
1260 ? "searching..." : "loading... ") :
1261 (refs_str ? refs_str : "")) == -1) {
1262 err = got_error_from_errno("asprintf");
1263 goto done;
1266 if (path && strcmp(path, "/") != 0) {
1267 if (asprintf(&header, "commit %s %s%s",
1268 id_str ? id_str : "........................................",
1269 path, ncommits_str) == -1) {
1270 err = got_error_from_errno("asprintf");
1271 header = NULL;
1272 goto done;
1274 } else if (asprintf(&header, "commit %s%s",
1275 id_str ? id_str : "........................................",
1276 ncommits_str) == -1) {
1277 err = got_error_from_errno("asprintf");
1278 header = NULL;
1279 goto done;
1281 err = format_line(&wline, &width, header, view->ncols);
1282 if (err)
1283 goto done;
1285 werase(view->window);
1287 if (view_needs_focus_indication(view))
1288 wstandout(view->window);
1289 waddwstr(view->window, wline);
1290 while (width < view->ncols) {
1291 waddch(view->window, ' ');
1292 width++;
1294 if (view_needs_focus_indication(view))
1295 wstandend(view->window);
1296 free(wline);
1297 if (limit <= 1)
1298 goto done;
1300 /* Grow author column size if necessary. */
1301 entry = first;
1302 ncommits = 0;
1303 while (entry) {
1304 char *author;
1305 wchar_t *wauthor;
1306 int width;
1307 if (ncommits >= limit - 1)
1308 break;
1309 author = strdup(got_object_commit_get_author(entry->commit));
1310 if (author == NULL) {
1311 err = got_error_from_errno("strdup");
1312 goto done;
1314 err = format_author(&wauthor, &width, author, COLS);
1315 if (author_cols < width)
1316 author_cols = width;
1317 free(wauthor);
1318 free(author);
1319 entry = TAILQ_NEXT(entry, entry);
1322 entry = first;
1323 *last = first;
1324 ncommits = 0;
1325 while (entry) {
1326 if (ncommits >= limit - 1)
1327 break;
1328 if (ncommits == selected_idx)
1329 wstandout(view->window);
1330 err = draw_commit(view, entry->commit, entry->id, refs,
1331 author_cols);
1332 if (ncommits == selected_idx)
1333 wstandend(view->window);
1334 if (err)
1335 goto done;
1336 ncommits++;
1337 *last = entry;
1338 entry = TAILQ_NEXT(entry, entry);
1341 view_vborder(view);
1342 done:
1343 free(id_str);
1344 free(refs_str);
1345 free(ncommits_str);
1346 free(header);
1347 return err;
1350 static void
1351 scroll_up(struct tog_view *view,
1352 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1353 struct commit_queue *commits)
1355 struct commit_queue_entry *entry;
1356 int nscrolled = 0;
1358 entry = TAILQ_FIRST(&commits->head);
1359 if (*first_displayed_entry == entry)
1360 return;
1362 entry = *first_displayed_entry;
1363 while (entry && nscrolled < maxscroll) {
1364 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1365 if (entry) {
1366 *first_displayed_entry = entry;
1367 nscrolled++;
1372 static const struct got_error *
1373 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1374 pthread_cond_t *need_commits)
1376 int errcode;
1377 int max_wait = 20;
1379 halfdelay(1); /* fast refresh while loading commits */
1381 while (*commits_needed > 0) {
1382 if (*log_complete)
1383 break;
1385 /* Wake the log thread. */
1386 errcode = pthread_cond_signal(need_commits);
1387 if (errcode)
1388 return got_error_set_errno(errcode,
1389 "pthread_cond_signal");
1390 errcode = pthread_mutex_unlock(&tog_mutex);
1391 if (errcode)
1392 return got_error_set_errno(errcode,
1393 "pthread_mutex_unlock");
1394 pthread_yield();
1395 errcode = pthread_mutex_lock(&tog_mutex);
1396 if (errcode)
1397 return got_error_set_errno(errcode,
1398 "pthread_mutex_lock");
1400 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1402 * Thread is not done yet; lose a key press
1403 * and let the user retry... this way the GUI
1404 * remains interactive while logging deep paths
1405 * with few commits in history.
1407 return NULL;
1411 return NULL;
1414 static const struct got_error *
1415 scroll_down(struct tog_view *view,
1416 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1417 struct commit_queue_entry **last_displayed_entry,
1418 struct commit_queue *commits, int *log_complete, int *commits_needed,
1419 pthread_cond_t *need_commits)
1421 const struct got_error *err = NULL;
1422 struct commit_queue_entry *pentry;
1423 int nscrolled = 0;
1425 if (*last_displayed_entry == NULL)
1426 return NULL;
1428 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1429 if (pentry == NULL && !*log_complete) {
1431 * Ask the log thread for required amount of commits
1432 * plus some amount of pre-fetching.
1434 (*commits_needed) += maxscroll + 20;
1435 err = trigger_log_thread(0, commits_needed, log_complete,
1436 need_commits);
1437 if (err)
1438 return err;
1441 do {
1442 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1443 if (pentry == NULL)
1444 break;
1446 *last_displayed_entry = pentry;
1448 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1449 if (pentry == NULL)
1450 break;
1451 *first_displayed_entry = pentry;
1452 } while (++nscrolled < maxscroll);
1454 return err;
1457 static const struct got_error *
1458 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1459 struct got_commit_object *commit, struct got_object_id *commit_id,
1460 struct tog_view *log_view, struct got_reflist_head *refs,
1461 struct got_repository *repo)
1463 const struct got_error *err;
1464 struct got_object_qid *parent_id;
1465 struct tog_view *diff_view;
1467 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1468 if (diff_view == NULL)
1469 return got_error_from_errno("view_open");
1471 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1472 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1473 commit_id, log_view, refs, repo);
1474 if (err == NULL)
1475 *new_view = diff_view;
1476 return err;
1479 static const struct got_error *
1480 tree_view_visit_subtree(struct got_tree_object *subtree,
1481 struct tog_tree_view_state *s)
1483 struct tog_parent_tree *parent;
1485 parent = calloc(1, sizeof(*parent));
1486 if (parent == NULL)
1487 return got_error_from_errno("calloc");
1489 parent->tree = s->tree;
1490 parent->first_displayed_entry = s->first_displayed_entry;
1491 parent->selected_entry = s->selected_entry;
1492 parent->selected = s->selected;
1493 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1494 s->tree = subtree;
1495 s->entries = got_object_tree_get_entries(s->tree);
1496 s->selected = 0;
1497 s->first_displayed_entry = NULL;
1498 return NULL;
1502 static const struct got_error *
1503 browse_commit_tree(struct tog_view **new_view, int begin_x,
1504 struct commit_queue_entry *entry, const char *path,
1505 struct got_reflist_head *refs, struct got_repository *repo)
1507 const struct got_error *err = NULL;
1508 struct got_tree_object *tree;
1509 struct got_tree_entry *te;
1510 struct tog_tree_view_state *s;
1511 struct tog_view *tree_view;
1512 char *slash, *subpath = NULL;
1513 const char *p;
1515 err = got_object_open_as_tree(&tree, repo,
1516 got_object_commit_get_tree_id(entry->commit));
1517 if (err)
1518 return err;
1520 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1521 if (tree_view == NULL)
1522 return got_error_from_errno("view_open");
1524 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1525 if (err) {
1526 got_object_tree_close(tree);
1527 return err;
1529 s = &tree_view->state.tree;
1531 *new_view = tree_view;
1533 /* Walk the path and open corresponding tree objects. */
1534 p = path;
1535 while (*p) {
1536 struct got_object_id *tree_id;
1538 while (p[0] == '/')
1539 p++;
1541 /* Ensure the correct subtree entry is selected. */
1542 slash = strchr(p, '/');
1543 if (slash == NULL)
1544 slash = strchr(p, '\0');
1545 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1546 if (strncmp(p, te->name, slash - p) == 0) {
1547 s->selected_entry = te;
1548 break;
1550 s->selected++;
1552 if (s->selected_entry == NULL) {
1553 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1554 break;
1556 if (s->tree != s->root)
1557 s->selected++; /* skip '..' */
1559 if (!S_ISDIR(s->selected_entry->mode)) {
1560 /* Jump to this file's entry. */
1561 s->first_displayed_entry = s->selected_entry;
1562 s->selected = 0;
1563 break;
1566 slash = strchr(p, '/');
1567 if (slash)
1568 subpath = strndup(path, slash - path);
1569 else
1570 subpath = strdup(path);
1571 if (subpath == NULL) {
1572 err = got_error_from_errno("strdup");
1573 break;
1576 err = got_object_id_by_path(&tree_id, repo, entry->id,
1577 subpath);
1578 if (err)
1579 break;
1581 err = got_object_open_as_tree(&tree, repo, tree_id);
1582 free(tree_id);
1583 if (err)
1584 break;
1586 err = tree_view_visit_subtree(tree, s);
1587 if (err) {
1588 got_object_tree_close(tree);
1589 break;
1591 if (slash == NULL)
1592 break;
1593 free(subpath);
1594 subpath = NULL;
1595 p = slash;
1598 free(subpath);
1599 return err;
1602 static void *
1603 log_thread(void *arg)
1605 const struct got_error *err = NULL;
1606 int errcode = 0;
1607 struct tog_log_thread_args *a = arg;
1608 int done = 0;
1610 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1611 if (err)
1612 return (void *)err;
1614 while (!done && !err) {
1615 err = queue_commits(a->graph, a->commits, 1, a->repo,
1616 a->in_repo_path);
1617 if (err) {
1618 if (err->code != GOT_ERR_ITER_COMPLETED)
1619 return (void *)err;
1620 err = NULL;
1621 done = 1;
1622 } else if (a->commits_needed > 0)
1623 a->commits_needed--;
1625 errcode = pthread_mutex_lock(&tog_mutex);
1626 if (errcode) {
1627 err = got_error_set_errno(errcode,
1628 "pthread_mutex_lock");
1629 break;
1630 } else if (*a->quit)
1631 done = 1;
1632 else if (*a->first_displayed_entry == NULL) {
1633 *a->first_displayed_entry =
1634 TAILQ_FIRST(&a->commits->head);
1635 *a->selected_entry = *a->first_displayed_entry;
1638 if (done)
1639 a->commits_needed = 0;
1640 else if (a->commits_needed == 0) {
1641 errcode = pthread_cond_wait(&a->need_commits,
1642 &tog_mutex);
1643 if (errcode)
1644 err = got_error_set_errno(errcode,
1645 "pthread_cond_wait");
1648 errcode = pthread_mutex_unlock(&tog_mutex);
1649 if (errcode && err == NULL)
1650 err = got_error_set_errno(errcode,
1651 "pthread_mutex_unlock");
1653 a->log_complete = 1;
1654 return (void *)err;
1657 static const struct got_error *
1658 stop_log_thread(struct tog_log_view_state *s)
1660 const struct got_error *err = NULL;
1661 int errcode;
1663 if (s->thread) {
1664 s->quit = 1;
1665 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1666 if (errcode)
1667 return got_error_set_errno(errcode,
1668 "pthread_cond_signal");
1669 errcode = pthread_mutex_unlock(&tog_mutex);
1670 if (errcode)
1671 return got_error_set_errno(errcode,
1672 "pthread_mutex_unlock");
1673 errcode = pthread_join(s->thread, (void **)&err);
1674 if (errcode)
1675 return got_error_set_errno(errcode, "pthread_join");
1676 errcode = pthread_mutex_lock(&tog_mutex);
1677 if (errcode)
1678 return got_error_set_errno(errcode,
1679 "pthread_mutex_lock");
1680 s->thread = NULL;
1683 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1684 if (errcode && err == NULL)
1685 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1687 if (s->thread_args.repo) {
1688 got_repo_close(s->thread_args.repo);
1689 s->thread_args.repo = NULL;
1692 if (s->thread_args.graph) {
1693 got_commit_graph_close(s->thread_args.graph);
1694 s->thread_args.graph = NULL;
1697 return err;
1700 static const struct got_error *
1701 close_log_view(struct tog_view *view)
1703 const struct got_error *err = NULL;
1704 struct tog_log_view_state *s = &view->state.log;
1706 err = stop_log_thread(s);
1707 free_commits(&s->commits);
1708 free(s->in_repo_path);
1709 s->in_repo_path = NULL;
1710 free(s->start_id);
1711 s->start_id = NULL;
1712 return err;
1715 static const struct got_error *
1716 search_start_log_view(struct tog_view *view)
1718 struct tog_log_view_state *s = &view->state.log;
1720 s->matched_entry = NULL;
1721 return NULL;
1724 static int
1725 match_commit(struct got_commit_object *commit, regex_t *regex)
1727 regmatch_t regmatch;
1729 if (regexec(regex, got_object_commit_get_author(commit), 1,
1730 &regmatch, 0) == 0 ||
1731 regexec(regex, got_object_commit_get_committer(commit), 1,
1732 &regmatch, 0) == 0 ||
1733 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1734 &regmatch, 0) == 0)
1735 return 1;
1737 return 0;
1740 static const struct got_error *
1741 search_next_log_view(struct tog_view *view)
1743 const struct got_error *err = NULL;
1744 struct tog_log_view_state *s = &view->state.log;
1745 struct commit_queue_entry *entry;
1747 if (!view->searching) {
1748 view->search_next_done = 1;
1749 return NULL;
1752 if (s->matched_entry) {
1753 if (view->searching == TOG_SEARCH_FORWARD)
1754 entry = TAILQ_NEXT(s->selected_entry, entry);
1755 else
1756 entry = TAILQ_PREV(s->selected_entry,
1757 commit_queue_head, entry);
1758 } else {
1759 if (view->searching == TOG_SEARCH_FORWARD)
1760 entry = TAILQ_FIRST(&s->commits.head);
1761 else
1762 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1765 while (1) {
1766 if (entry == NULL) {
1767 if (s->thread_args.log_complete) {
1768 if (s->matched_entry == NULL) {
1769 view->search_next_done = 1;
1770 return NULL;
1772 if (view->searching == TOG_SEARCH_FORWARD)
1773 entry = TAILQ_FIRST(&s->commits.head);
1774 else
1775 entry = TAILQ_LAST(&s->commits.head,
1776 commit_queue_head);
1777 } else {
1778 s->thread_args.commits_needed = 1;
1779 return trigger_log_thread(0,
1780 &s->thread_args.commits_needed,
1781 &s->thread_args.log_complete,
1782 &s->thread_args.need_commits);
1786 if (match_commit(entry->commit, &view->regex)) {
1787 view->search_next_done = 1;
1788 s->matched_entry = entry;
1789 break;
1791 if (view->searching == TOG_SEARCH_FORWARD)
1792 entry = TAILQ_NEXT(entry, entry);
1793 else
1794 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1797 if (s->matched_entry) {
1798 int cur = s->selected_entry->idx;
1799 while (cur < s->matched_entry->idx) {
1800 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1801 if (err)
1802 return err;
1803 cur++;
1805 while (cur > s->matched_entry->idx) {
1806 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1807 if (err)
1808 return err;
1809 cur--;
1813 return NULL;
1816 static const struct got_error *
1817 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1818 struct got_reflist_head *refs, struct got_repository *repo,
1819 const char *path, int check_disk)
1821 const struct got_error *err = NULL;
1822 struct tog_log_view_state *s = &view->state.log;
1823 struct got_repository *thread_repo = NULL;
1824 struct got_commit_graph *thread_graph = NULL;
1825 int errcode;
1827 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1828 if (err != NULL)
1829 goto done;
1831 /* The commit queue only contains commits being displayed. */
1832 TAILQ_INIT(&s->commits.head);
1833 s->commits.ncommits = 0;
1835 s->refs = refs;
1836 s->repo = repo;
1837 s->start_id = got_object_id_dup(start_id);
1838 if (s->start_id == NULL) {
1839 err = got_error_from_errno("got_object_id_dup");
1840 goto done;
1843 view->show = show_log_view;
1844 view->input = input_log_view;
1845 view->close = close_log_view;
1846 view->search_start = search_start_log_view;
1847 view->search_next = search_next_log_view;
1849 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1850 if (err)
1851 goto done;
1852 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1853 0, thread_repo);
1854 if (err)
1855 goto done;
1857 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1858 if (errcode) {
1859 err = got_error_set_errno(errcode, "pthread_cond_init");
1860 goto done;
1863 s->thread_args.commits_needed = view->nlines;
1864 s->thread_args.graph = thread_graph;
1865 s->thread_args.commits = &s->commits;
1866 s->thread_args.in_repo_path = s->in_repo_path;
1867 s->thread_args.start_id = s->start_id;
1868 s->thread_args.repo = thread_repo;
1869 s->thread_args.log_complete = 0;
1870 s->thread_args.quit = &s->quit;
1871 s->thread_args.view = view;
1872 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1873 s->thread_args.selected_entry = &s->selected_entry;
1874 done:
1875 if (err)
1876 close_log_view(view);
1877 return err;
1880 static const struct got_error *
1881 show_log_view(struct tog_view *view)
1883 struct tog_log_view_state *s = &view->state.log;
1885 if (s->thread == NULL) {
1886 int errcode = pthread_create(&s->thread, NULL, log_thread,
1887 &s->thread_args);
1888 if (errcode)
1889 return got_error_set_errno(errcode, "pthread_create");
1892 return draw_commits(view, &s->last_displayed_entry,
1893 &s->selected_entry, s->first_displayed_entry,
1894 &s->commits, s->selected, view->nlines, s->refs,
1895 s->in_repo_path, s->thread_args.commits_needed);
1898 static const struct got_error *
1899 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1900 struct tog_view **focus_view, struct tog_view *view, int ch)
1902 const struct got_error *err = NULL;
1903 struct tog_log_view_state *s = &view->state.log;
1904 char *parent_path;
1905 struct tog_view *diff_view = NULL, *tree_view = NULL;
1906 int begin_x = 0;
1908 switch (ch) {
1909 case 'q':
1910 s->quit = 1;
1911 break;
1912 case 'k':
1913 case KEY_UP:
1914 case '<':
1915 case ',':
1916 if (s->first_displayed_entry == NULL)
1917 break;
1918 if (s->selected > 0)
1919 s->selected--;
1920 else
1921 scroll_up(view, &s->first_displayed_entry, 1,
1922 &s->commits);
1923 break;
1924 case KEY_PPAGE:
1925 case CTRL('b'):
1926 if (s->first_displayed_entry == NULL)
1927 break;
1928 if (TAILQ_FIRST(&s->commits.head) ==
1929 s->first_displayed_entry) {
1930 s->selected = 0;
1931 break;
1933 scroll_up(view, &s->first_displayed_entry,
1934 view->nlines, &s->commits);
1935 break;
1936 case 'j':
1937 case KEY_DOWN:
1938 case '>':
1939 case '.':
1940 if (s->first_displayed_entry == NULL)
1941 break;
1942 if (s->selected < MIN(view->nlines - 2,
1943 s->commits.ncommits - 1)) {
1944 s->selected++;
1945 break;
1947 err = scroll_down(view, &s->first_displayed_entry, 1,
1948 &s->last_displayed_entry, &s->commits,
1949 &s->thread_args.log_complete,
1950 &s->thread_args.commits_needed,
1951 &s->thread_args.need_commits);
1952 break;
1953 case KEY_NPAGE:
1954 case CTRL('f'): {
1955 struct commit_queue_entry *first;
1956 first = s->first_displayed_entry;
1957 if (first == NULL)
1958 break;
1959 err = scroll_down(view, &s->first_displayed_entry,
1960 view->nlines, &s->last_displayed_entry,
1961 &s->commits, &s->thread_args.log_complete,
1962 &s->thread_args.commits_needed,
1963 &s->thread_args.need_commits);
1964 if (first == s->first_displayed_entry &&
1965 s->selected < MIN(view->nlines - 2,
1966 s->commits.ncommits - 1)) {
1967 /* can't scroll further down */
1968 s->selected = MIN(view->nlines - 2,
1969 s->commits.ncommits - 1);
1971 err = NULL;
1972 break;
1974 case KEY_RESIZE:
1975 if (s->selected > view->nlines - 2)
1976 s->selected = view->nlines - 2;
1977 if (s->selected > s->commits.ncommits - 1)
1978 s->selected = s->commits.ncommits - 1;
1979 break;
1980 case KEY_ENTER:
1981 case ' ':
1982 case '\r':
1983 if (s->selected_entry == NULL)
1984 break;
1985 if (view_is_parent_view(view))
1986 begin_x = view_split_begin_x(view->begin_x);
1987 err = open_diff_view_for_commit(&diff_view, begin_x,
1988 s->selected_entry->commit, s->selected_entry->id,
1989 view, s->refs, s->repo);
1990 if (err)
1991 break;
1992 if (view_is_parent_view(view)) {
1993 err = view_close_child(view);
1994 if (err)
1995 return err;
1996 err = view_set_child(view, diff_view);
1997 if (err) {
1998 view_close(diff_view);
1999 break;
2001 *focus_view = diff_view;
2002 view->child_focussed = 1;
2003 } else
2004 *new_view = diff_view;
2005 break;
2006 case 't':
2007 if (s->selected_entry == NULL)
2008 break;
2009 if (view_is_parent_view(view))
2010 begin_x = view_split_begin_x(view->begin_x);
2011 err = browse_commit_tree(&tree_view, begin_x,
2012 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2013 if (err)
2014 break;
2015 if (view_is_parent_view(view)) {
2016 err = view_close_child(view);
2017 if (err)
2018 return err;
2019 err = view_set_child(view, tree_view);
2020 if (err) {
2021 view_close(tree_view);
2022 break;
2024 *focus_view = tree_view;
2025 view->child_focussed = 1;
2026 } else
2027 *new_view = tree_view;
2028 break;
2029 case KEY_BACKSPACE:
2030 if (strcmp(s->in_repo_path, "/") == 0)
2031 break;
2032 parent_path = dirname(s->in_repo_path);
2033 if (parent_path && strcmp(parent_path, ".") != 0) {
2034 struct tog_view *lv;
2035 err = stop_log_thread(s);
2036 if (err)
2037 return err;
2038 lv = view_open(view->nlines, view->ncols,
2039 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2040 if (lv == NULL)
2041 return got_error_from_errno(
2042 "view_open");
2043 err = open_log_view(lv, s->start_id, s->refs,
2044 s->repo, parent_path, 0);
2045 if (err)
2046 return err;;
2047 if (view_is_parent_view(view))
2048 *new_view = lv;
2049 else {
2050 view_set_child(view->parent, lv);
2051 *focus_view = lv;
2053 return NULL;
2055 break;
2056 default:
2057 break;
2060 return err;
2063 static const struct got_error *
2064 apply_unveil(const char *repo_path, const char *worktree_path)
2066 const struct got_error *error;
2068 if (repo_path && unveil(repo_path, "r") != 0)
2069 return got_error_from_errno2("unveil", repo_path);
2071 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2072 return got_error_from_errno2("unveil", worktree_path);
2074 if (unveil("/tmp", "rwc") != 0)
2075 return got_error_from_errno2("unveil", "/tmp");
2077 error = got_privsep_unveil_exec_helpers();
2078 if (error != NULL)
2079 return error;
2081 if (unveil(NULL, NULL) != 0)
2082 return got_error_from_errno("unveil");
2084 return NULL;
2087 static void
2088 init_curses(void)
2090 initscr();
2091 cbreak();
2092 halfdelay(1); /* Do fast refresh while initial view is loading. */
2093 noecho();
2094 nonl();
2095 intrflush(stdscr, FALSE);
2096 keypad(stdscr, TRUE);
2097 curs_set(0);
2098 signal(SIGWINCH, tog_sigwinch);
2101 static const struct got_error *
2102 cmd_log(int argc, char *argv[])
2104 const struct got_error *error;
2105 struct got_repository *repo = NULL;
2106 struct got_worktree *worktree = NULL;
2107 struct got_reflist_head refs;
2108 struct got_object_id *start_id = NULL;
2109 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2110 char *start_commit = NULL;
2111 int ch;
2112 struct tog_view *view;
2114 SIMPLEQ_INIT(&refs);
2116 #ifndef PROFILE
2117 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2118 NULL) == -1)
2119 err(1, "pledge");
2120 #endif
2122 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2123 switch (ch) {
2124 case 'c':
2125 start_commit = optarg;
2126 break;
2127 case 'r':
2128 repo_path = realpath(optarg, NULL);
2129 if (repo_path == NULL)
2130 err(1, "-r option");
2131 break;
2132 default:
2133 usage_log();
2134 /* NOTREACHED */
2138 argc -= optind;
2139 argv += optind;
2141 cwd = getcwd(NULL, 0);
2142 if (cwd == NULL) {
2143 error = got_error_from_errno("getcwd");
2144 goto done;
2146 error = got_worktree_open(&worktree, cwd);
2147 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2148 goto done;
2149 error = NULL;
2151 if (argc == 0) {
2152 path = strdup("");
2153 if (path == NULL) {
2154 error = got_error_from_errno("strdup");
2155 goto done;
2157 } else if (argc == 1) {
2158 if (worktree) {
2159 error = got_worktree_resolve_path(&path, worktree,
2160 argv[0]);
2161 if (error)
2162 goto done;
2163 } else {
2164 path = strdup(argv[0]);
2165 if (path == NULL) {
2166 error = got_error_from_errno("strdup");
2167 goto done;
2170 } else
2171 usage_log();
2173 repo_path = worktree ?
2174 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2175 if (repo_path == NULL) {
2176 error = got_error_from_errno("strdup");
2177 goto done;
2180 init_curses();
2182 error = got_repo_open(&repo, repo_path);
2183 if (error != NULL)
2184 goto done;
2186 error = apply_unveil(got_repo_get_path(repo),
2187 worktree ? got_worktree_get_root_path(worktree) : NULL);
2188 if (error)
2189 goto done;
2191 if (start_commit == NULL)
2192 error = get_head_commit_id(&start_id, worktree ?
2193 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2194 repo);
2195 else
2196 error = got_object_resolve_id_str(&start_id, repo,
2197 start_commit);
2198 if (error != NULL)
2199 goto done;
2201 error = got_ref_list(&refs, repo);
2202 if (error)
2203 goto done;
2205 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2206 if (view == NULL) {
2207 error = got_error_from_errno("view_open");
2208 goto done;
2210 error = open_log_view(view, start_id, &refs, repo, path, 1);
2211 if (error)
2212 goto done;
2213 error = view_loop(view);
2214 done:
2215 free(repo_path);
2216 free(cwd);
2217 free(path);
2218 free(start_id);
2219 if (repo)
2220 got_repo_close(repo);
2221 if (worktree)
2222 got_worktree_close(worktree);
2223 got_ref_list_free(&refs);
2224 return error;
2227 __dead static void
2228 usage_diff(void)
2230 endwin();
2231 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2232 getprogname());
2233 exit(1);
2236 static char *
2237 parse_next_line(FILE *f, size_t *len)
2239 char *line;
2240 size_t linelen;
2241 size_t lineno;
2242 const char delim[3] = { '\0', '\0', '\0'};
2244 line = fparseln(f, &linelen, &lineno, delim, 0);
2245 if (len)
2246 *len = linelen;
2247 return line;
2250 static const struct got_error *
2251 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2252 int *last_displayed_line, int *eof, int max_lines,
2253 char *header)
2255 const struct got_error *err;
2256 int nlines = 0, nprinted = 0;
2257 char *line;
2258 size_t len;
2259 wchar_t *wline;
2260 int width;
2262 rewind(f);
2263 werase(view->window);
2265 if (header) {
2266 err = format_line(&wline, &width, header, view->ncols);
2267 if (err) {
2268 return err;
2271 if (view_needs_focus_indication(view))
2272 wstandout(view->window);
2273 waddwstr(view->window, wline);
2274 if (view_needs_focus_indication(view))
2275 wstandend(view->window);
2276 if (width < view->ncols - 1)
2277 waddch(view->window, '\n');
2279 if (max_lines <= 1)
2280 return NULL;
2281 max_lines--;
2284 *eof = 0;
2285 while (nprinted < max_lines) {
2286 line = parse_next_line(f, &len);
2287 if (line == NULL) {
2288 *eof = 1;
2289 break;
2291 if (++nlines < *first_displayed_line) {
2292 free(line);
2293 continue;
2296 err = format_line(&wline, &width, line, view->ncols);
2297 if (err) {
2298 free(line);
2299 return err;
2301 waddwstr(view->window, wline);
2302 if (width < view->ncols - 1)
2303 waddch(view->window, '\n');
2304 if (++nprinted == 1)
2305 *first_displayed_line = nlines;
2306 free(line);
2307 free(wline);
2308 wline = NULL;
2310 *last_displayed_line = nlines;
2312 view_vborder(view);
2314 if (*eof) {
2315 while (nprinted < view->nlines) {
2316 waddch(view->window, '\n');
2317 nprinted++;
2320 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2321 if (err) {
2322 return err;
2325 wstandout(view->window);
2326 waddwstr(view->window, wline);
2327 wstandend(view->window);
2330 return NULL;
2333 static char *
2334 get_datestr(time_t *time, char *datebuf)
2336 char *p, *s = ctime_r(time, datebuf);
2337 p = strchr(s, '\n');
2338 if (p)
2339 *p = '\0';
2340 return s;
2343 static const struct got_error *
2344 write_commit_info(struct got_object_id *commit_id,
2345 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2347 const struct got_error *err = NULL;
2348 char datebuf[26];
2349 struct got_commit_object *commit;
2350 char *id_str = NULL;
2351 time_t committer_time;
2352 const char *author, *committer;
2353 char *refs_str = NULL;
2355 if (refs) {
2356 err = build_refs_str(&refs_str, refs, commit_id);
2357 if (err)
2358 return err;
2361 err = got_object_open_as_commit(&commit, repo, commit_id);
2362 if (err)
2363 return err;
2365 err = got_object_id_str(&id_str, commit_id);
2366 if (err) {
2367 err = got_error_from_errno("got_object_id_str");
2368 goto done;
2371 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2372 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2373 err = got_error_from_errno("fprintf");
2374 goto done;
2376 if (fprintf(outfile, "from: %s\n",
2377 got_object_commit_get_author(commit)) < 0) {
2378 err = got_error_from_errno("fprintf");
2379 goto done;
2381 committer_time = got_object_commit_get_committer_time(commit);
2382 if (fprintf(outfile, "date: %s UTC\n",
2383 get_datestr(&committer_time, datebuf)) < 0) {
2384 err = got_error_from_errno("fprintf");
2385 goto done;
2387 author = got_object_commit_get_author(commit);
2388 committer = got_object_commit_get_committer(commit);
2389 if (strcmp(author, committer) != 0 &&
2390 fprintf(outfile, "via: %s\n", committer) < 0) {
2391 err = got_error_from_errno("fprintf");
2392 goto done;
2394 if (fprintf(outfile, "%s\n",
2395 got_object_commit_get_logmsg(commit)) < 0) {
2396 err = got_error_from_errno("fprintf");
2397 goto done;
2399 done:
2400 free(id_str);
2401 free(refs_str);
2402 got_object_commit_close(commit);
2403 return err;
2406 static const struct got_error *
2407 create_diff(struct tog_diff_view_state *s)
2409 const struct got_error *err = NULL;
2410 FILE *f = NULL;
2411 int obj_type;
2413 f = got_opentemp();
2414 if (f == NULL) {
2415 err = got_error_from_errno("got_opentemp");
2416 goto done;
2418 if (s->f && fclose(s->f) != 0) {
2419 err = got_error_from_errno("fclose");
2420 goto done;
2422 s->f = f;
2424 if (s->id1)
2425 err = got_object_get_type(&obj_type, s->repo, s->id1);
2426 else
2427 err = got_object_get_type(&obj_type, s->repo, s->id2);
2428 if (err)
2429 goto done;
2431 switch (obj_type) {
2432 case GOT_OBJ_TYPE_BLOB:
2433 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2434 s->diff_context, s->repo, f);
2435 break;
2436 case GOT_OBJ_TYPE_TREE:
2437 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2438 s->diff_context, s->repo, f);
2439 break;
2440 case GOT_OBJ_TYPE_COMMIT: {
2441 const struct got_object_id_queue *parent_ids;
2442 struct got_object_qid *pid;
2443 struct got_commit_object *commit2;
2445 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2446 if (err)
2447 break;
2448 /* Show commit info if we're diffing to a parent/root commit. */
2449 if (s->id1 == NULL)
2450 write_commit_info(s->id2, s->refs, s->repo, f);
2451 else {
2452 parent_ids = got_object_commit_get_parent_ids(commit2);
2453 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2454 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2455 write_commit_info(s->id2, s->refs,
2456 s->repo, f);
2457 break;
2461 got_object_commit_close(commit2);
2463 err = got_diff_objects_as_commits(s->id1, s->id2,
2464 s->diff_context, s->repo, f);
2465 break;
2467 default:
2468 err = got_error(GOT_ERR_OBJ_TYPE);
2469 break;
2471 done:
2472 if (f && fflush(f) != 0 && err == NULL)
2473 err = got_error_from_errno("fflush");
2474 return err;
2477 static void
2478 diff_view_indicate_progress(struct tog_view *view)
2480 mvwaddstr(view->window, 0, 0, "diffing...");
2481 update_panels();
2482 doupdate();
2485 static const struct got_error *
2486 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2487 struct got_object_id *id2, struct tog_view *log_view,
2488 struct got_reflist_head *refs, struct got_repository *repo)
2490 const struct got_error *err;
2492 if (id1 != NULL && id2 != NULL) {
2493 int type1, type2;
2494 err = got_object_get_type(&type1, repo, id1);
2495 if (err)
2496 return err;
2497 err = got_object_get_type(&type2, repo, id2);
2498 if (err)
2499 return err;
2501 if (type1 != type2)
2502 return got_error(GOT_ERR_OBJ_TYPE);
2505 if (id1) {
2506 view->state.diff.id1 = got_object_id_dup(id1);
2507 if (view->state.diff.id1 == NULL)
2508 return got_error_from_errno("got_object_id_dup");
2509 } else
2510 view->state.diff.id1 = NULL;
2512 view->state.diff.id2 = got_object_id_dup(id2);
2513 if (view->state.diff.id2 == NULL) {
2514 free(view->state.diff.id1);
2515 view->state.diff.id1 = NULL;
2516 return got_error_from_errno("got_object_id_dup");
2518 view->state.diff.f = NULL;
2519 view->state.diff.first_displayed_line = 1;
2520 view->state.diff.last_displayed_line = view->nlines;
2521 view->state.diff.diff_context = 3;
2522 view->state.diff.log_view = log_view;
2523 view->state.diff.repo = repo;
2524 view->state.diff.refs = refs;
2526 if (log_view && view_is_splitscreen(view))
2527 show_log_view(log_view); /* draw vborder */
2528 diff_view_indicate_progress(view);
2530 err = create_diff(&view->state.diff);
2531 if (err) {
2532 free(view->state.diff.id1);
2533 view->state.diff.id1 = NULL;
2534 free(view->state.diff.id2);
2535 view->state.diff.id2 = NULL;
2536 return err;
2539 view->show = show_diff_view;
2540 view->input = input_diff_view;
2541 view->close = close_diff_view;
2543 return NULL;
2546 static const struct got_error *
2547 close_diff_view(struct tog_view *view)
2549 const struct got_error *err = NULL;
2551 free(view->state.diff.id1);
2552 view->state.diff.id1 = NULL;
2553 free(view->state.diff.id2);
2554 view->state.diff.id2 = NULL;
2555 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2556 err = got_error_from_errno("fclose");
2557 return err;
2560 static const struct got_error *
2561 show_diff_view(struct tog_view *view)
2563 const struct got_error *err;
2564 struct tog_diff_view_state *s = &view->state.diff;
2565 char *id_str1 = NULL, *id_str2, *header;
2567 if (s->id1) {
2568 err = got_object_id_str(&id_str1, s->id1);
2569 if (err)
2570 return err;
2572 err = got_object_id_str(&id_str2, s->id2);
2573 if (err)
2574 return err;
2576 if (asprintf(&header, "diff %s %s",
2577 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2578 err = got_error_from_errno("asprintf");
2579 free(id_str1);
2580 free(id_str2);
2581 return err;
2583 free(id_str1);
2584 free(id_str2);
2586 return draw_file(view, s->f, &s->first_displayed_line,
2587 &s->last_displayed_line, &s->eof, view->nlines,
2588 header);
2591 static const struct got_error *
2592 set_selected_commit(struct tog_diff_view_state *s,
2593 struct commit_queue_entry *entry)
2595 const struct got_error *err;
2596 const struct got_object_id_queue *parent_ids;
2597 struct got_commit_object *selected_commit;
2598 struct got_object_qid *pid;
2600 free(s->id2);
2601 s->id2 = got_object_id_dup(entry->id);
2602 if (s->id2 == NULL)
2603 return got_error_from_errno("got_object_id_dup");
2605 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2606 if (err)
2607 return err;
2608 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2609 free(s->id1);
2610 pid = SIMPLEQ_FIRST(parent_ids);
2611 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2612 got_object_commit_close(selected_commit);
2613 return NULL;
2616 static const struct got_error *
2617 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2618 struct tog_view **focus_view, struct tog_view *view, int ch)
2620 const struct got_error *err = NULL;
2621 struct tog_diff_view_state *s = &view->state.diff;
2622 struct tog_log_view_state *ls;
2623 struct commit_queue_entry *entry;
2624 int i;
2626 switch (ch) {
2627 case 'k':
2628 case KEY_UP:
2629 if (s->first_displayed_line > 1)
2630 s->first_displayed_line--;
2631 break;
2632 case KEY_PPAGE:
2633 case CTRL('b'):
2634 if (s->first_displayed_line == 1)
2635 break;
2636 i = 0;
2637 while (i++ < view->nlines - 1 &&
2638 s->first_displayed_line > 1)
2639 s->first_displayed_line--;
2640 break;
2641 case 'j':
2642 case KEY_DOWN:
2643 if (!s->eof)
2644 s->first_displayed_line++;
2645 break;
2646 case KEY_NPAGE:
2647 case CTRL('f'):
2648 case ' ':
2649 if (s->eof)
2650 break;
2651 i = 0;
2652 while (!s->eof && i++ < view->nlines - 1) {
2653 char *line;
2654 line = parse_next_line(s->f, NULL);
2655 s->first_displayed_line++;
2656 if (line == NULL)
2657 break;
2659 break;
2660 case '[':
2661 if (s->diff_context > 0) {
2662 s->diff_context--;
2663 diff_view_indicate_progress(view);
2664 err = create_diff(s);
2666 break;
2667 case ']':
2668 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2669 s->diff_context++;
2670 diff_view_indicate_progress(view);
2671 err = create_diff(s);
2673 break;
2674 case '<':
2675 case ',':
2676 if (s->log_view == NULL)
2677 break;
2678 ls = &s->log_view->state.log;
2679 entry = TAILQ_PREV(ls->selected_entry,
2680 commit_queue_head, entry);
2681 if (entry == NULL)
2682 break;
2684 err = input_log_view(NULL, NULL, NULL, s->log_view,
2685 KEY_UP);
2686 if (err)
2687 break;
2689 err = set_selected_commit(s, entry);
2690 if (err)
2691 break;
2693 s->first_displayed_line = 1;
2694 s->last_displayed_line = view->nlines;
2696 diff_view_indicate_progress(view);
2697 err = create_diff(s);
2698 break;
2699 case '>':
2700 case '.':
2701 if (s->log_view == NULL)
2702 break;
2703 ls = &s->log_view->state.log;
2705 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2706 ls->thread_args.commits_needed++;
2708 /* Display "loading..." in log view. */
2709 show_log_view(s->log_view);
2710 update_panels();
2711 doupdate();
2713 err = trigger_log_thread(1 /* load_all */,
2714 &ls->thread_args.commits_needed,
2715 &ls->thread_args.log_complete,
2716 &ls->thread_args.need_commits);
2717 if (err)
2718 break;
2720 err = input_log_view(NULL, NULL, NULL, s->log_view,
2721 KEY_DOWN);
2722 if (err)
2723 break;
2725 entry = TAILQ_NEXT(ls->selected_entry, entry);
2726 if (entry == NULL)
2727 break;
2729 err = set_selected_commit(s, entry);
2730 if (err)
2731 break;
2733 s->first_displayed_line = 1;
2734 s->last_displayed_line = view->nlines;
2736 diff_view_indicate_progress(view);
2737 err = create_diff(s);
2738 break;
2739 default:
2740 break;
2743 return err;
2746 static const struct got_error *
2747 cmd_diff(int argc, char *argv[])
2749 const struct got_error *error = NULL;
2750 struct got_repository *repo = NULL;
2751 struct got_reflist_head refs;
2752 struct got_object_id *id1 = NULL, *id2 = NULL;
2753 char *repo_path = NULL;
2754 char *id_str1 = NULL, *id_str2 = NULL;
2755 int ch;
2756 struct tog_view *view;
2758 SIMPLEQ_INIT(&refs);
2760 #ifndef PROFILE
2761 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2762 NULL) == -1)
2763 err(1, "pledge");
2764 #endif
2766 while ((ch = getopt(argc, argv, "")) != -1) {
2767 switch (ch) {
2768 default:
2769 usage_diff();
2770 /* NOTREACHED */
2774 argc -= optind;
2775 argv += optind;
2777 if (argc == 0) {
2778 usage_diff(); /* TODO show local worktree changes */
2779 } else if (argc == 2) {
2780 repo_path = getcwd(NULL, 0);
2781 if (repo_path == NULL)
2782 return got_error_from_errno("getcwd");
2783 id_str1 = argv[0];
2784 id_str2 = argv[1];
2785 } else if (argc == 3) {
2786 repo_path = realpath(argv[0], NULL);
2787 if (repo_path == NULL)
2788 return got_error_from_errno2("realpath", argv[0]);
2789 id_str1 = argv[1];
2790 id_str2 = argv[2];
2791 } else
2792 usage_diff();
2794 init_curses();
2796 error = got_repo_open(&repo, repo_path);
2797 if (error)
2798 goto done;
2800 error = apply_unveil(got_repo_get_path(repo), NULL);
2801 if (error)
2802 goto done;
2804 error = got_object_resolve_id_str(&id1, repo, id_str1);
2805 if (error)
2806 goto done;
2808 error = got_object_resolve_id_str(&id2, repo, id_str2);
2809 if (error)
2810 goto done;
2812 error = got_ref_list(&refs, repo);
2813 if (error)
2814 goto done;
2816 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2817 if (view == NULL) {
2818 error = got_error_from_errno("view_open");
2819 goto done;
2821 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2822 if (error)
2823 goto done;
2824 error = view_loop(view);
2825 done:
2826 free(repo_path);
2827 got_repo_close(repo);
2828 got_ref_list_free(&refs);
2829 return error;
2832 __dead static void
2833 usage_blame(void)
2835 endwin();
2836 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2837 getprogname());
2838 exit(1);
2841 struct tog_blame_line {
2842 int annotated;
2843 struct got_object_id *id;
2846 static const struct got_error *
2847 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2848 const char *path, struct tog_blame_line *lines, int nlines,
2849 int blame_complete, int selected_line, int *first_displayed_line,
2850 int *last_displayed_line, int *eof, int max_lines)
2852 const struct got_error *err;
2853 int lineno = 0, nprinted = 0;
2854 char *line;
2855 size_t len;
2856 wchar_t *wline;
2857 int width, wlimit;
2858 struct tog_blame_line *blame_line;
2859 struct got_object_id *prev_id = NULL;
2860 char *id_str;
2862 err = got_object_id_str(&id_str, id);
2863 if (err)
2864 return err;
2866 rewind(f);
2867 werase(view->window);
2869 if (asprintf(&line, "commit %s", id_str) == -1) {
2870 err = got_error_from_errno("asprintf");
2871 free(id_str);
2872 return err;
2875 err = format_line(&wline, &width, line, view->ncols);
2876 free(line);
2877 line = NULL;
2878 if (view_needs_focus_indication(view))
2879 wstandout(view->window);
2880 waddwstr(view->window, wline);
2881 if (view_needs_focus_indication(view))
2882 wstandend(view->window);
2883 free(wline);
2884 wline = NULL;
2885 if (width < view->ncols - 1)
2886 waddch(view->window, '\n');
2888 if (asprintf(&line, "[%d/%d] %s%s",
2889 *first_displayed_line - 1 + selected_line, nlines,
2890 blame_complete ? "" : "annotating... ", path) == -1) {
2891 free(id_str);
2892 return got_error_from_errno("asprintf");
2894 free(id_str);
2895 err = format_line(&wline, &width, line, view->ncols);
2896 free(line);
2897 line = NULL;
2898 if (err)
2899 return err;
2900 waddwstr(view->window, wline);
2901 free(wline);
2902 wline = NULL;
2903 if (width < view->ncols - 1)
2904 waddch(view->window, '\n');
2906 *eof = 0;
2907 while (nprinted < max_lines - 2) {
2908 line = parse_next_line(f, &len);
2909 if (line == NULL) {
2910 *eof = 1;
2911 break;
2913 if (++lineno < *first_displayed_line) {
2914 free(line);
2915 continue;
2918 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2919 err = format_line(&wline, &width, line, wlimit);
2920 if (err) {
2921 free(line);
2922 return err;
2925 if (view->focussed && nprinted == selected_line - 1)
2926 wstandout(view->window);
2928 blame_line = &lines[lineno - 1];
2929 if (blame_line->annotated && prev_id &&
2930 got_object_id_cmp(prev_id, blame_line->id) == 0)
2931 waddstr(view->window, " ");
2932 else if (blame_line->annotated) {
2933 char *id_str;
2934 err = got_object_id_str(&id_str, blame_line->id);
2935 if (err) {
2936 free(line);
2937 free(wline);
2938 return err;
2940 wprintw(view->window, "%.8s ", id_str);
2941 free(id_str);
2942 prev_id = blame_line->id;
2943 } else {
2944 waddstr(view->window, "........ ");
2945 prev_id = NULL;
2948 waddwstr(view->window, wline);
2949 while (width < wlimit) {
2950 waddch(view->window, ' ');
2951 width++;
2953 if (view->focussed && nprinted == selected_line - 1)
2954 wstandend(view->window);
2955 if (++nprinted == 1)
2956 *first_displayed_line = lineno;
2957 free(line);
2958 free(wline);
2959 wline = NULL;
2961 *last_displayed_line = lineno;
2963 view_vborder(view);
2965 return NULL;
2968 static const struct got_error *
2969 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2971 const struct got_error *err = NULL;
2972 struct tog_blame_cb_args *a = arg;
2973 struct tog_blame_line *line;
2974 int errcode;
2976 if (nlines != a->nlines ||
2977 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2978 return got_error(GOT_ERR_RANGE);
2980 errcode = pthread_mutex_lock(&tog_mutex);
2981 if (errcode)
2982 return got_error_set_errno(errcode, "pthread_mutex_lock");
2984 if (*a->quit) { /* user has quit the blame view */
2985 err = got_error(GOT_ERR_ITER_COMPLETED);
2986 goto done;
2989 if (lineno == -1)
2990 goto done; /* no change in this commit */
2992 line = &a->lines[lineno - 1];
2993 if (line->annotated)
2994 goto done;
2996 line->id = got_object_id_dup(id);
2997 if (line->id == NULL) {
2998 err = got_error_from_errno("got_object_id_dup");
2999 goto done;
3001 line->annotated = 1;
3002 done:
3003 errcode = pthread_mutex_unlock(&tog_mutex);
3004 if (errcode)
3005 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3006 return err;
3009 static void *
3010 blame_thread(void *arg)
3012 const struct got_error *err;
3013 struct tog_blame_thread_args *ta = arg;
3014 struct tog_blame_cb_args *a = ta->cb_args;
3015 int errcode;
3017 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3018 blame_cb, ta->cb_args);
3020 errcode = pthread_mutex_lock(&tog_mutex);
3021 if (errcode)
3022 return (void *)got_error_set_errno(errcode,
3023 "pthread_mutex_lock");
3025 got_repo_close(ta->repo);
3026 ta->repo = NULL;
3027 *ta->complete = 1;
3029 errcode = pthread_mutex_unlock(&tog_mutex);
3030 if (errcode && err == NULL)
3031 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3033 return (void *)err;
3036 static struct got_object_id *
3037 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3038 int selected_line)
3040 struct tog_blame_line *line;
3042 line = &lines[first_displayed_line - 1 + selected_line - 1];
3043 if (!line->annotated)
3044 return NULL;
3046 return line->id;
3049 static const struct got_error *
3050 stop_blame(struct tog_blame *blame)
3052 const struct got_error *err = NULL;
3053 int i;
3055 if (blame->thread) {
3056 int errcode;
3057 errcode = pthread_mutex_unlock(&tog_mutex);
3058 if (errcode)
3059 return got_error_set_errno(errcode,
3060 "pthread_mutex_unlock");
3061 errcode = pthread_join(blame->thread, (void **)&err);
3062 if (errcode)
3063 return got_error_set_errno(errcode, "pthread_join");
3064 errcode = pthread_mutex_lock(&tog_mutex);
3065 if (errcode)
3066 return got_error_set_errno(errcode,
3067 "pthread_mutex_lock");
3068 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3069 err = NULL;
3070 blame->thread = NULL;
3072 if (blame->thread_args.repo) {
3073 got_repo_close(blame->thread_args.repo);
3074 blame->thread_args.repo = NULL;
3076 if (blame->f) {
3077 if (fclose(blame->f) != 0 && err == NULL)
3078 err = got_error_from_errno("fclose");
3079 blame->f = NULL;
3081 if (blame->lines) {
3082 for (i = 0; i < blame->nlines; i++)
3083 free(blame->lines[i].id);
3084 free(blame->lines);
3085 blame->lines = NULL;
3087 free(blame->cb_args.commit_id);
3088 blame->cb_args.commit_id = NULL;
3090 return err;
3093 static const struct got_error *
3094 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3095 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3096 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3097 struct got_repository *repo)
3099 const struct got_error *err = NULL;
3100 struct got_blob_object *blob = NULL;
3101 struct got_repository *thread_repo = NULL;
3102 struct got_object_id *obj_id = NULL;
3103 int obj_type;
3105 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3106 if (err)
3107 return err;
3108 if (obj_id == NULL)
3109 return got_error(GOT_ERR_NO_OBJ);
3111 err = got_object_get_type(&obj_type, repo, obj_id);
3112 if (err)
3113 goto done;
3115 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3116 err = got_error(GOT_ERR_OBJ_TYPE);
3117 goto done;
3120 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3121 if (err)
3122 goto done;
3123 blame->f = got_opentemp();
3124 if (blame->f == NULL) {
3125 err = got_error_from_errno("got_opentemp");
3126 goto done;
3128 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3129 &blame->line_offsets, blame->f, blob);
3130 if (err)
3131 goto done;
3133 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3134 if (blame->lines == NULL) {
3135 err = got_error_from_errno("calloc");
3136 goto done;
3139 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3140 if (err)
3141 goto done;
3143 blame->cb_args.view = view;
3144 blame->cb_args.lines = blame->lines;
3145 blame->cb_args.nlines = blame->nlines;
3146 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3147 if (blame->cb_args.commit_id == NULL) {
3148 err = got_error_from_errno("got_object_id_dup");
3149 goto done;
3151 blame->cb_args.quit = done;
3153 blame->thread_args.path = path;
3154 blame->thread_args.repo = thread_repo;
3155 blame->thread_args.cb_args = &blame->cb_args;
3156 blame->thread_args.complete = blame_complete;
3157 *blame_complete = 0;
3159 done:
3160 if (blob)
3161 got_object_blob_close(blob);
3162 free(obj_id);
3163 if (err)
3164 stop_blame(blame);
3165 return err;
3168 static const struct got_error *
3169 open_blame_view(struct tog_view *view, char *path,
3170 struct got_object_id *commit_id, struct got_reflist_head *refs,
3171 struct got_repository *repo)
3173 const struct got_error *err = NULL;
3174 struct tog_blame_view_state *s = &view->state.blame;
3176 SIMPLEQ_INIT(&s->blamed_commits);
3178 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3179 if (err)
3180 return err;
3182 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3183 s->first_displayed_line = 1;
3184 s->last_displayed_line = view->nlines;
3185 s->selected_line = 1;
3186 s->blame_complete = 0;
3187 s->path = path;
3188 if (s->path == NULL)
3189 return got_error_from_errno("open_blame_view");
3190 s->repo = repo;
3191 s->refs = refs;
3192 s->commit_id = commit_id;
3193 memset(&s->blame, 0, sizeof(s->blame));
3195 view->show = show_blame_view;
3196 view->input = input_blame_view;
3197 view->close = close_blame_view;
3198 view->search_start = search_start_blame_view;
3199 view->search_next = search_next_blame_view;
3201 return run_blame(&s->blame, view, &s->blame_complete,
3202 &s->first_displayed_line, &s->last_displayed_line,
3203 &s->selected_line, &s->done, &s->eof, s->path,
3204 s->blamed_commit->id, s->repo);
3207 static const struct got_error *
3208 close_blame_view(struct tog_view *view)
3210 const struct got_error *err = NULL;
3211 struct tog_blame_view_state *s = &view->state.blame;
3213 if (s->blame.thread)
3214 err = stop_blame(&s->blame);
3216 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3217 struct got_object_qid *blamed_commit;
3218 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3219 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3220 got_object_qid_free(blamed_commit);
3223 free(s->path);
3225 return err;
3228 static const struct got_error *
3229 search_start_blame_view(struct tog_view *view)
3231 struct tog_blame_view_state *s = &view->state.blame;
3233 s->matched_line = 0;
3234 return NULL;
3237 static int
3238 match_line(const char *line, regex_t *regex)
3240 regmatch_t regmatch;
3242 return regexec(regex, line, 1, &regmatch, 0) == 0;
3246 static const struct got_error *
3247 search_next_blame_view(struct tog_view *view)
3249 const struct got_error *err = NULL;
3250 struct tog_blame_view_state *s = &view->state.blame;
3251 int lineno;
3253 if (!view->searching) {
3254 view->search_next_done = 1;
3255 return NULL;
3258 if (s->matched_line) {
3259 if (view->searching == TOG_SEARCH_FORWARD)
3260 lineno = s->first_displayed_line - 1 + s->selected_line + 1;
3261 else
3262 lineno = s->first_displayed_line - 1 + s->selected_line - 1;
3263 } else {
3264 if (view->searching == TOG_SEARCH_FORWARD)
3265 lineno = 1;
3266 else
3267 lineno = s->blame.nlines;
3270 while (1) {
3271 char *line = NULL;
3272 off_t offset;
3273 size_t len;
3275 if (lineno <= 0 || lineno > s->blame.nlines) {
3276 if (s->matched_line == 0) {
3277 view->search_next_done = 1;
3278 free(line);
3279 break;
3281 if (view->searching == TOG_SEARCH_FORWARD)
3282 lineno = 1;
3283 else
3284 lineno = s->blame.nlines;
3287 offset = s->blame.line_offsets[lineno - 1];
3288 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3289 free(line);
3290 return got_error_from_errno("fseeko");
3292 free(line);
3293 line = parse_next_line(s->blame.f, &len);
3294 if (line == NULL)
3295 break;
3296 if (match_line(line, &view->regex)) {
3297 view->search_next_done = 1;
3298 s->matched_line = lineno;
3299 free(line);
3300 break;
3302 free(line);
3303 line = NULL;
3304 if (view->searching == TOG_SEARCH_FORWARD)
3305 lineno++;
3306 else
3307 lineno--;
3310 if (s->matched_line) {
3311 int cur = s->first_displayed_line - 1 + s->selected_line;
3312 while (cur < s->matched_line) {
3313 err = input_blame_view(NULL, NULL, NULL, view, KEY_DOWN);
3314 if (err)
3315 return err;
3316 cur++;
3318 while (cur > s->matched_line) {
3319 err = input_blame_view(NULL, NULL, NULL, view, KEY_UP);
3320 if (err)
3321 return err;
3322 cur--;
3324 s->first_displayed_line = s->matched_line;
3325 s->selected_line = 1;
3328 return NULL;
3331 static const struct got_error *
3332 show_blame_view(struct tog_view *view)
3334 const struct got_error *err = NULL;
3335 struct tog_blame_view_state *s = &view->state.blame;
3336 int errcode;
3338 if (s->blame.thread == NULL) {
3339 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3340 &s->blame.thread_args);
3341 if (errcode)
3342 return got_error_set_errno(errcode, "pthread_create");
3345 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3346 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3347 s->selected_line, &s->first_displayed_line,
3348 &s->last_displayed_line, &s->eof, view->nlines);
3350 view_vborder(view);
3351 return err;
3354 static const struct got_error *
3355 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3356 struct tog_view **focus_view, struct tog_view *view, int ch)
3358 const struct got_error *err = NULL, *thread_err = NULL;
3359 struct tog_view *diff_view;
3360 struct tog_blame_view_state *s = &view->state.blame;
3361 int begin_x = 0;
3363 switch (ch) {
3364 case 'q':
3365 s->done = 1;
3366 break;
3367 case 'k':
3368 case KEY_UP:
3369 if (s->selected_line > 1)
3370 s->selected_line--;
3371 else if (s->selected_line == 1 &&
3372 s->first_displayed_line > 1)
3373 s->first_displayed_line--;
3374 break;
3375 case KEY_PPAGE:
3376 if (s->first_displayed_line == 1) {
3377 s->selected_line = 1;
3378 break;
3380 if (s->first_displayed_line > view->nlines - 2)
3381 s->first_displayed_line -=
3382 (view->nlines - 2);
3383 else
3384 s->first_displayed_line = 1;
3385 break;
3386 case 'j':
3387 case KEY_DOWN:
3388 if (s->selected_line < view->nlines - 2 &&
3389 s->first_displayed_line +
3390 s->selected_line <= s->blame.nlines)
3391 s->selected_line++;
3392 else if (s->last_displayed_line <
3393 s->blame.nlines)
3394 s->first_displayed_line++;
3395 break;
3396 case 'b':
3397 case 'p': {
3398 struct got_object_id *id = NULL;
3399 id = get_selected_commit_id(s->blame.lines,
3400 s->first_displayed_line, s->selected_line);
3401 if (id == NULL)
3402 break;
3403 if (ch == 'p') {
3404 struct got_commit_object *commit;
3405 struct got_object_qid *pid;
3406 struct got_object_id *blob_id = NULL;
3407 int obj_type;
3408 err = got_object_open_as_commit(&commit,
3409 s->repo, id);
3410 if (err)
3411 break;
3412 pid = SIMPLEQ_FIRST(
3413 got_object_commit_get_parent_ids(commit));
3414 if (pid == NULL) {
3415 got_object_commit_close(commit);
3416 break;
3418 /* Check if path history ends here. */
3419 err = got_object_id_by_path(&blob_id, s->repo,
3420 pid->id, s->path);
3421 if (err) {
3422 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3423 err = NULL;
3424 got_object_commit_close(commit);
3425 break;
3427 err = got_object_get_type(&obj_type, s->repo,
3428 blob_id);
3429 free(blob_id);
3430 /* Can't blame non-blob type objects. */
3431 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3432 got_object_commit_close(commit);
3433 break;
3435 err = got_object_qid_alloc(&s->blamed_commit,
3436 pid->id);
3437 got_object_commit_close(commit);
3438 } else {
3439 if (got_object_id_cmp(id,
3440 s->blamed_commit->id) == 0)
3441 break;
3442 err = got_object_qid_alloc(&s->blamed_commit,
3443 id);
3445 if (err)
3446 break;
3447 s->done = 1;
3448 thread_err = stop_blame(&s->blame);
3449 s->done = 0;
3450 if (thread_err)
3451 break;
3452 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3453 s->blamed_commit, entry);
3454 err = run_blame(&s->blame, view, &s->blame_complete,
3455 &s->first_displayed_line, &s->last_displayed_line,
3456 &s->selected_line, &s->done, &s->eof,
3457 s->path, s->blamed_commit->id, s->repo);
3458 if (err)
3459 break;
3460 break;
3462 case 'B': {
3463 struct got_object_qid *first;
3464 first = SIMPLEQ_FIRST(&s->blamed_commits);
3465 if (!got_object_id_cmp(first->id, s->commit_id))
3466 break;
3467 s->done = 1;
3468 thread_err = stop_blame(&s->blame);
3469 s->done = 0;
3470 if (thread_err)
3471 break;
3472 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3473 got_object_qid_free(s->blamed_commit);
3474 s->blamed_commit =
3475 SIMPLEQ_FIRST(&s->blamed_commits);
3476 err = run_blame(&s->blame, view, &s->blame_complete,
3477 &s->first_displayed_line, &s->last_displayed_line,
3478 &s->selected_line, &s->done, &s->eof, s->path,
3479 s->blamed_commit->id, s->repo);
3480 if (err)
3481 break;
3482 break;
3484 case KEY_ENTER:
3485 case '\r': {
3486 struct got_object_id *id = NULL;
3487 struct got_object_qid *pid;
3488 struct got_commit_object *commit = NULL;
3489 id = get_selected_commit_id(s->blame.lines,
3490 s->first_displayed_line, s->selected_line);
3491 if (id == NULL)
3492 break;
3493 err = got_object_open_as_commit(&commit, s->repo, id);
3494 if (err)
3495 break;
3496 pid = SIMPLEQ_FIRST(
3497 got_object_commit_get_parent_ids(commit));
3498 if (view_is_parent_view(view))
3499 begin_x = view_split_begin_x(view->begin_x);
3500 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3501 if (diff_view == NULL) {
3502 got_object_commit_close(commit);
3503 err = got_error_from_errno("view_open");
3504 break;
3506 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3507 id, NULL, s->refs, s->repo);
3508 got_object_commit_close(commit);
3509 if (err) {
3510 view_close(diff_view);
3511 break;
3513 if (view_is_parent_view(view)) {
3514 err = view_close_child(view);
3515 if (err)
3516 break;
3517 err = view_set_child(view, diff_view);
3518 if (err) {
3519 view_close(diff_view);
3520 break;
3522 *focus_view = diff_view;
3523 view->child_focussed = 1;
3524 } else
3525 *new_view = diff_view;
3526 if (err)
3527 break;
3528 break;
3530 case KEY_NPAGE:
3531 case ' ':
3532 if (s->last_displayed_line >= s->blame.nlines &&
3533 s->selected_line >= MIN(s->blame.nlines,
3534 view->nlines - 2)) {
3535 break;
3537 if (s->last_displayed_line >= s->blame.nlines &&
3538 s->selected_line < view->nlines - 2) {
3539 s->selected_line = MIN(s->blame.nlines,
3540 view->nlines - 2);
3541 break;
3543 if (s->last_displayed_line + view->nlines - 2
3544 <= s->blame.nlines)
3545 s->first_displayed_line +=
3546 view->nlines - 2;
3547 else
3548 s->first_displayed_line =
3549 s->blame.nlines -
3550 (view->nlines - 3);
3551 break;
3552 case KEY_RESIZE:
3553 if (s->selected_line > view->nlines - 2) {
3554 s->selected_line = MIN(s->blame.nlines,
3555 view->nlines - 2);
3557 break;
3558 default:
3559 break;
3561 return thread_err ? thread_err : err;
3564 static const struct got_error *
3565 cmd_blame(int argc, char *argv[])
3567 const struct got_error *error;
3568 struct got_repository *repo = NULL;
3569 struct got_reflist_head refs;
3570 struct got_worktree *worktree = NULL;
3571 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3572 struct got_object_id *commit_id = NULL;
3573 char *commit_id_str = NULL;
3574 int ch;
3575 struct tog_view *view;
3577 SIMPLEQ_INIT(&refs);
3579 #ifndef PROFILE
3580 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3581 NULL) == -1)
3582 err(1, "pledge");
3583 #endif
3585 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3586 switch (ch) {
3587 case 'c':
3588 commit_id_str = optarg;
3589 break;
3590 case 'r':
3591 repo_path = realpath(optarg, NULL);
3592 if (repo_path == NULL)
3593 err(1, "-r option");
3594 break;
3595 default:
3596 usage_blame();
3597 /* NOTREACHED */
3601 argc -= optind;
3602 argv += optind;
3604 if (argc == 1)
3605 path = argv[0];
3606 else
3607 usage_blame();
3609 cwd = getcwd(NULL, 0);
3610 if (cwd == NULL) {
3611 error = got_error_from_errno("getcwd");
3612 goto done;
3614 if (repo_path == NULL) {
3615 error = got_worktree_open(&worktree, cwd);
3616 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3617 goto done;
3618 else
3619 error = NULL;
3620 if (worktree) {
3621 repo_path =
3622 strdup(got_worktree_get_repo_path(worktree));
3623 if (repo_path == NULL)
3624 error = got_error_from_errno("strdup");
3625 if (error)
3626 goto done;
3627 } else {
3628 repo_path = strdup(cwd);
3629 if (repo_path == NULL) {
3630 error = got_error_from_errno("strdup");
3631 goto done;
3636 init_curses();
3638 error = got_repo_open(&repo, repo_path);
3639 if (error != NULL)
3640 goto done;
3642 error = apply_unveil(got_repo_get_path(repo), NULL);
3643 if (error)
3644 goto done;
3646 if (worktree) {
3647 const char *prefix = got_worktree_get_path_prefix(worktree);
3648 char *p, *worktree_subdir = cwd +
3649 strlen(got_worktree_get_root_path(worktree));
3650 if (asprintf(&p, "%s%s%s%s%s",
3651 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3652 worktree_subdir, worktree_subdir[0] ? "/" : "",
3653 path) == -1) {
3654 error = got_error_from_errno("asprintf");
3655 goto done;
3657 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3658 free(p);
3659 } else {
3660 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3662 if (error)
3663 goto done;
3665 if (commit_id_str == NULL) {
3666 struct got_reference *head_ref;
3667 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3668 if (error != NULL)
3669 goto done;
3670 error = got_ref_resolve(&commit_id, repo, head_ref);
3671 got_ref_close(head_ref);
3672 } else {
3673 error = got_object_resolve_id_str(&commit_id, repo,
3674 commit_id_str);
3676 if (error != NULL)
3677 goto done;
3679 error = got_ref_list(&refs, repo);
3680 if (error)
3681 goto done;
3683 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3684 if (view == NULL) {
3685 error = got_error_from_errno("view_open");
3686 goto done;
3688 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3689 if (error)
3690 goto done;
3691 error = view_loop(view);
3692 done:
3693 free(repo_path);
3694 free(cwd);
3695 free(commit_id);
3696 if (worktree)
3697 got_worktree_close(worktree);
3698 if (repo)
3699 got_repo_close(repo);
3700 got_ref_list_free(&refs);
3701 return error;
3704 static const struct got_error *
3705 draw_tree_entries(struct tog_view *view,
3706 struct got_tree_entry **first_displayed_entry,
3707 struct got_tree_entry **last_displayed_entry,
3708 struct got_tree_entry **selected_entry, int *ndisplayed,
3709 const char *label, int show_ids, const char *parent_path,
3710 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3712 const struct got_error *err = NULL;
3713 struct got_tree_entry *te;
3714 wchar_t *wline;
3715 int width, n;
3717 *ndisplayed = 0;
3719 werase(view->window);
3721 if (limit == 0)
3722 return NULL;
3724 err = format_line(&wline, &width, label, view->ncols);
3725 if (err)
3726 return err;
3727 if (view_needs_focus_indication(view))
3728 wstandout(view->window);
3729 waddwstr(view->window, wline);
3730 if (view_needs_focus_indication(view))
3731 wstandend(view->window);
3732 free(wline);
3733 wline = NULL;
3734 if (width < view->ncols - 1)
3735 waddch(view->window, '\n');
3736 if (--limit <= 0)
3737 return NULL;
3738 err = format_line(&wline, &width, parent_path, view->ncols);
3739 if (err)
3740 return err;
3741 waddwstr(view->window, wline);
3742 free(wline);
3743 wline = NULL;
3744 if (width < view->ncols - 1)
3745 waddch(view->window, '\n');
3746 if (--limit <= 0)
3747 return NULL;
3748 waddch(view->window, '\n');
3749 if (--limit <= 0)
3750 return NULL;
3752 te = SIMPLEQ_FIRST(&entries->head);
3753 if (*first_displayed_entry == NULL) {
3754 if (selected == 0) {
3755 if (view->focussed)
3756 wstandout(view->window);
3757 *selected_entry = NULL;
3759 waddstr(view->window, " ..\n"); /* parent directory */
3760 if (selected == 0 && view->focussed)
3761 wstandend(view->window);
3762 (*ndisplayed)++;
3763 if (--limit <= 0)
3764 return NULL;
3765 n = 1;
3766 } else {
3767 n = 0;
3768 while (te != *first_displayed_entry)
3769 te = SIMPLEQ_NEXT(te, entry);
3772 while (te) {
3773 char *line = NULL, *id_str = NULL;
3775 if (show_ids) {
3776 err = got_object_id_str(&id_str, te->id);
3777 if (err)
3778 return got_error_from_errno(
3779 "got_object_id_str");
3781 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3782 te->name, S_ISDIR(te->mode) ? "/" :
3783 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3784 free(id_str);
3785 return got_error_from_errno("asprintf");
3787 free(id_str);
3788 err = format_line(&wline, &width, line, view->ncols);
3789 if (err) {
3790 free(line);
3791 break;
3793 if (n == selected) {
3794 if (view->focussed)
3795 wstandout(view->window);
3796 *selected_entry = te;
3798 waddwstr(view->window, wline);
3799 if (width < view->ncols - 1)
3800 waddch(view->window, '\n');
3801 if (n == selected && view->focussed)
3802 wstandend(view->window);
3803 free(line);
3804 free(wline);
3805 wline = NULL;
3806 n++;
3807 (*ndisplayed)++;
3808 *last_displayed_entry = te;
3809 if (--limit <= 0)
3810 break;
3811 te = SIMPLEQ_NEXT(te, entry);
3814 return err;
3817 static void
3818 tree_scroll_up(struct tog_view *view,
3819 struct got_tree_entry **first_displayed_entry, int maxscroll,
3820 const struct got_tree_entries *entries, int isroot)
3822 struct got_tree_entry *te, *prev;
3823 int i;
3825 if (*first_displayed_entry == NULL)
3826 return;
3828 te = SIMPLEQ_FIRST(&entries->head);
3829 if (*first_displayed_entry == te) {
3830 if (!isroot)
3831 *first_displayed_entry = NULL;
3832 return;
3835 /* XXX this is stupid... switch to TAILQ? */
3836 for (i = 0; i < maxscroll; i++) {
3837 while (te != *first_displayed_entry) {
3838 prev = te;
3839 te = SIMPLEQ_NEXT(te, entry);
3841 *first_displayed_entry = prev;
3842 te = SIMPLEQ_FIRST(&entries->head);
3844 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3845 *first_displayed_entry = NULL;
3848 static int
3849 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3850 struct got_tree_entry *last_displayed_entry,
3851 const struct got_tree_entries *entries)
3853 struct got_tree_entry *next, *last;
3854 int n = 0;
3856 if (*first_displayed_entry)
3857 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3858 else
3859 next = SIMPLEQ_FIRST(&entries->head);
3860 last = last_displayed_entry;
3861 while (next && last && n++ < maxscroll) {
3862 last = SIMPLEQ_NEXT(last, entry);
3863 if (last) {
3864 *first_displayed_entry = next;
3865 next = SIMPLEQ_NEXT(next, entry);
3868 return n;
3871 static const struct got_error *
3872 tree_entry_path(char **path, struct tog_parent_trees *parents,
3873 struct got_tree_entry *te)
3875 const struct got_error *err = NULL;
3876 struct tog_parent_tree *pt;
3877 size_t len = 2; /* for leading slash and NUL */
3879 TAILQ_FOREACH(pt, parents, entry)
3880 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3881 if (te)
3882 len += strlen(te->name);
3884 *path = calloc(1, len);
3885 if (path == NULL)
3886 return got_error_from_errno("calloc");
3888 (*path)[0] = '/';
3889 pt = TAILQ_LAST(parents, tog_parent_trees);
3890 while (pt) {
3891 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3892 err = got_error(GOT_ERR_NO_SPACE);
3893 goto done;
3895 if (strlcat(*path, "/", len) >= len) {
3896 err = got_error(GOT_ERR_NO_SPACE);
3897 goto done;
3899 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3901 if (te) {
3902 if (strlcat(*path, te->name, len) >= len) {
3903 err = got_error(GOT_ERR_NO_SPACE);
3904 goto done;
3907 done:
3908 if (err) {
3909 free(*path);
3910 *path = NULL;
3912 return err;
3915 static const struct got_error *
3916 blame_tree_entry(struct tog_view **new_view, int begin_x,
3917 struct got_tree_entry *te, struct tog_parent_trees *parents,
3918 struct got_object_id *commit_id, struct got_reflist_head *refs,
3919 struct got_repository *repo)
3921 const struct got_error *err = NULL;
3922 char *path;
3923 struct tog_view *blame_view;
3925 err = tree_entry_path(&path, parents, te);
3926 if (err)
3927 return err;
3929 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3930 if (blame_view == NULL)
3931 return got_error_from_errno("view_open");
3933 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3934 if (err) {
3935 view_close(blame_view);
3936 free(path);
3937 } else
3938 *new_view = blame_view;
3939 return err;
3942 static const struct got_error *
3943 log_tree_entry(struct tog_view **new_view, int begin_x,
3944 struct got_tree_entry *te, struct tog_parent_trees *parents,
3945 struct got_object_id *commit_id, struct got_reflist_head *refs,
3946 struct got_repository *repo)
3948 struct tog_view *log_view;
3949 const struct got_error *err = NULL;
3950 char *path;
3952 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3953 if (log_view == NULL)
3954 return got_error_from_errno("view_open");
3956 err = tree_entry_path(&path, parents, te);
3957 if (err)
3958 return err;
3960 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3961 if (err)
3962 view_close(log_view);
3963 else
3964 *new_view = log_view;
3965 free(path);
3966 return err;
3969 static const struct got_error *
3970 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3971 struct got_object_id *commit_id, struct got_reflist_head *refs,
3972 struct got_repository *repo)
3974 const struct got_error *err = NULL;
3975 char *commit_id_str = NULL;
3976 struct tog_tree_view_state *s = &view->state.tree;
3978 TAILQ_INIT(&s->parents);
3980 err = got_object_id_str(&commit_id_str, commit_id);
3981 if (err != NULL)
3982 goto done;
3984 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3985 err = got_error_from_errno("asprintf");
3986 goto done;
3989 s->root = s->tree = root;
3990 s->entries = got_object_tree_get_entries(root);
3991 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3992 s->commit_id = got_object_id_dup(commit_id);
3993 if (s->commit_id == NULL) {
3994 err = got_error_from_errno("got_object_id_dup");
3995 goto done;
3997 s->refs = refs;
3998 s->repo = repo;
4000 view->show = show_tree_view;
4001 view->input = input_tree_view;
4002 view->close = close_tree_view;
4003 view->search_start = search_start_tree_view;
4004 view->search_next = search_next_tree_view;
4005 done:
4006 free(commit_id_str);
4007 if (err) {
4008 free(s->tree_label);
4009 s->tree_label = NULL;
4011 return err;
4014 static const struct got_error *
4015 close_tree_view(struct tog_view *view)
4017 struct tog_tree_view_state *s = &view->state.tree;
4019 free(s->tree_label);
4020 s->tree_label = NULL;
4021 free(s->commit_id);
4022 s->commit_id = NULL;
4023 while (!TAILQ_EMPTY(&s->parents)) {
4024 struct tog_parent_tree *parent;
4025 parent = TAILQ_FIRST(&s->parents);
4026 TAILQ_REMOVE(&s->parents, parent, entry);
4027 free(parent);
4030 if (s->tree != s->root)
4031 got_object_tree_close(s->tree);
4032 got_object_tree_close(s->root);
4034 return NULL;
4037 static const struct got_error *
4038 search_start_tree_view(struct tog_view *view)
4040 struct tog_tree_view_state *s = &view->state.tree;
4042 s->matched_entry = NULL;
4043 return NULL;
4046 static int
4047 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4049 regmatch_t regmatch;
4051 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4054 static const struct got_error *
4055 search_next_tree_view(struct tog_view *view)
4057 struct tog_tree_view_state *s = &view->state.tree;
4058 struct got_tree_entry *entry, *te;
4060 if (!view->searching) {
4061 view->search_next_done = 1;
4062 return NULL;
4065 if (s->matched_entry) {
4066 if (view->searching == TOG_SEARCH_FORWARD) {
4067 if (s->selected_entry)
4068 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4069 else
4070 entry = SIMPLEQ_FIRST(&s->entries->head);
4072 else {
4073 if (s->selected_entry == NULL) {
4074 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4075 entry = te;
4076 } else {
4077 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4078 entry = te;
4079 if (SIMPLEQ_NEXT(te, entry) ==
4080 s->selected_entry)
4081 break;
4085 } else {
4086 if (view->searching == TOG_SEARCH_FORWARD)
4087 entry = SIMPLEQ_FIRST(&s->entries->head);
4088 else {
4089 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4090 entry = te;
4094 while (1) {
4095 if (entry == NULL) {
4096 if (view->searching == TOG_SEARCH_FORWARD)
4097 entry = SIMPLEQ_FIRST(&s->entries->head);
4098 else {
4099 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4100 entry = te;
4104 if (match_tree_entry(entry, &view->regex)) {
4105 view->search_next_done = 1;
4106 s->matched_entry = entry;
4107 break;
4110 if (view->searching == TOG_SEARCH_FORWARD)
4111 entry = SIMPLEQ_NEXT(entry, entry);
4112 else {
4113 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4114 entry = NULL;
4115 else {
4116 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4117 if (SIMPLEQ_NEXT(te, entry) == entry) {
4118 entry = te;
4119 break;
4126 if (s->matched_entry) {
4127 s->first_displayed_entry = s->matched_entry;
4128 s->selected = 0;
4131 return NULL;
4134 static const struct got_error *
4135 show_tree_view(struct tog_view *view)
4137 const struct got_error *err = NULL;
4138 struct tog_tree_view_state *s = &view->state.tree;
4139 char *parent_path;
4141 err = tree_entry_path(&parent_path, &s->parents, NULL);
4142 if (err)
4143 return err;
4145 err = draw_tree_entries(view, &s->first_displayed_entry,
4146 &s->last_displayed_entry, &s->selected_entry,
4147 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4148 s->entries, s->selected, view->nlines, s->tree == s->root);
4149 free(parent_path);
4151 view_vborder(view);
4152 return err;
4155 static const struct got_error *
4156 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4157 struct tog_view **focus_view, struct tog_view *view, int ch)
4159 const struct got_error *err = NULL;
4160 struct tog_tree_view_state *s = &view->state.tree;
4161 struct tog_view *log_view;
4162 int begin_x = 0, nscrolled;
4164 switch (ch) {
4165 case 'i':
4166 s->show_ids = !s->show_ids;
4167 break;
4168 case 'l':
4169 if (!s->selected_entry)
4170 break;
4171 if (view_is_parent_view(view))
4172 begin_x = view_split_begin_x(view->begin_x);
4173 err = log_tree_entry(&log_view, begin_x,
4174 s->selected_entry, &s->parents,
4175 s->commit_id, s->refs, s->repo);
4176 if (view_is_parent_view(view)) {
4177 err = view_close_child(view);
4178 if (err)
4179 return err;
4180 err = view_set_child(view, log_view);
4181 if (err) {
4182 view_close(log_view);
4183 break;
4185 *focus_view = log_view;
4186 view->child_focussed = 1;
4187 } else
4188 *new_view = log_view;
4189 break;
4190 case 'k':
4191 case KEY_UP:
4192 if (s->selected > 0) {
4193 s->selected--;
4194 if (s->selected == 0)
4195 break;
4197 if (s->selected > 0)
4198 break;
4199 tree_scroll_up(view, &s->first_displayed_entry, 1,
4200 s->entries, s->tree == s->root);
4201 break;
4202 case KEY_PPAGE:
4203 tree_scroll_up(view, &s->first_displayed_entry,
4204 MAX(0, view->nlines - 4 - s->selected), s->entries,
4205 s->tree == s->root);
4206 s->selected = 0;
4207 if (SIMPLEQ_FIRST(&s->entries->head) ==
4208 s->first_displayed_entry && s->tree != s->root)
4209 s->first_displayed_entry = NULL;
4210 break;
4211 case 'j':
4212 case KEY_DOWN:
4213 if (s->selected < s->ndisplayed - 1) {
4214 s->selected++;
4215 break;
4217 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4218 /* can't scroll any further */
4219 break;
4220 tree_scroll_down(&s->first_displayed_entry, 1,
4221 s->last_displayed_entry, s->entries);
4222 break;
4223 case KEY_NPAGE:
4224 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4225 == NULL) {
4226 /* can't scroll any further; move cursor down */
4227 if (s->selected < s->ndisplayed - 1)
4228 s->selected = s->ndisplayed - 1;
4229 break;
4231 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4232 view->nlines, s->last_displayed_entry, s->entries);
4233 if (nscrolled < view->nlines) {
4234 int ndisplayed = 0;
4235 struct got_tree_entry *te;
4236 te = s->first_displayed_entry;
4237 do {
4238 ndisplayed++;
4239 te = SIMPLEQ_NEXT(te, entry);
4240 } while (te);
4241 s->selected = ndisplayed - 1;
4243 break;
4244 case KEY_ENTER:
4245 case '\r':
4246 case KEY_BACKSPACE:
4247 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4248 struct tog_parent_tree *parent;
4249 /* user selected '..' */
4250 if (s->tree == s->root)
4251 break;
4252 parent = TAILQ_FIRST(&s->parents);
4253 TAILQ_REMOVE(&s->parents, parent,
4254 entry);
4255 got_object_tree_close(s->tree);
4256 s->tree = parent->tree;
4257 s->entries =
4258 got_object_tree_get_entries(s->tree);
4259 s->first_displayed_entry =
4260 parent->first_displayed_entry;
4261 s->selected_entry =
4262 parent->selected_entry;
4263 s->selected = parent->selected;
4264 free(parent);
4265 } else if (S_ISDIR(s->selected_entry->mode)) {
4266 struct got_tree_object *subtree;
4267 err = got_object_open_as_tree(&subtree,
4268 s->repo, s->selected_entry->id);
4269 if (err)
4270 break;
4271 err = tree_view_visit_subtree(subtree, s);
4272 if (err) {
4273 got_object_tree_close(subtree);
4274 break;
4276 } else if (S_ISREG(s->selected_entry->mode)) {
4277 struct tog_view *blame_view;
4278 int begin_x = view_is_parent_view(view) ?
4279 view_split_begin_x(view->begin_x) : 0;
4281 err = blame_tree_entry(&blame_view, begin_x,
4282 s->selected_entry, &s->parents,
4283 s->commit_id, s->refs, s->repo);
4284 if (err)
4285 break;
4286 if (view_is_parent_view(view)) {
4287 err = view_close_child(view);
4288 if (err)
4289 return err;
4290 err = view_set_child(view, blame_view);
4291 if (err) {
4292 view_close(blame_view);
4293 break;
4295 *focus_view = blame_view;
4296 view->child_focussed = 1;
4297 } else
4298 *new_view = blame_view;
4300 break;
4301 case KEY_RESIZE:
4302 if (s->selected > view->nlines)
4303 s->selected = s->ndisplayed - 1;
4304 break;
4305 default:
4306 break;
4309 return err;
4312 __dead static void
4313 usage_tree(void)
4315 endwin();
4316 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4317 getprogname());
4318 exit(1);
4321 static const struct got_error *
4322 cmd_tree(int argc, char *argv[])
4324 const struct got_error *error;
4325 struct got_repository *repo = NULL;
4326 struct got_reflist_head refs;
4327 char *repo_path = NULL;
4328 struct got_object_id *commit_id = NULL;
4329 char *commit_id_arg = NULL;
4330 struct got_commit_object *commit = NULL;
4331 struct got_tree_object *tree = NULL;
4332 int ch;
4333 struct tog_view *view;
4335 SIMPLEQ_INIT(&refs);
4337 #ifndef PROFILE
4338 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4339 NULL) == -1)
4340 err(1, "pledge");
4341 #endif
4343 while ((ch = getopt(argc, argv, "c:")) != -1) {
4344 switch (ch) {
4345 case 'c':
4346 commit_id_arg = optarg;
4347 break;
4348 default:
4349 usage_tree();
4350 /* NOTREACHED */
4354 argc -= optind;
4355 argv += optind;
4357 if (argc == 0) {
4358 struct got_worktree *worktree;
4359 char *cwd = getcwd(NULL, 0);
4360 if (cwd == NULL)
4361 return got_error_from_errno("getcwd");
4362 error = got_worktree_open(&worktree, cwd);
4363 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4364 goto done;
4365 if (worktree) {
4366 free(cwd);
4367 repo_path =
4368 strdup(got_worktree_get_repo_path(worktree));
4369 got_worktree_close(worktree);
4370 } else
4371 repo_path = cwd;
4372 if (repo_path == NULL) {
4373 error = got_error_from_errno("strdup");
4374 goto done;
4376 } else if (argc == 1) {
4377 repo_path = realpath(argv[0], NULL);
4378 if (repo_path == NULL)
4379 return got_error_from_errno2("realpath", argv[0]);
4380 } else
4381 usage_log();
4383 init_curses();
4385 error = got_repo_open(&repo, repo_path);
4386 if (error != NULL)
4387 goto done;
4389 error = apply_unveil(got_repo_get_path(repo), NULL);
4390 if (error)
4391 goto done;
4393 if (commit_id_arg == NULL)
4394 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4395 else
4396 error = got_object_resolve_id_str(&commit_id, repo,
4397 commit_id_arg);
4398 if (error != NULL)
4399 goto done;
4401 error = got_object_open_as_commit(&commit, repo, commit_id);
4402 if (error != NULL)
4403 goto done;
4405 error = got_object_open_as_tree(&tree, repo,
4406 got_object_commit_get_tree_id(commit));
4407 if (error != NULL)
4408 goto done;
4410 error = got_ref_list(&refs, repo);
4411 if (error)
4412 goto done;
4414 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4415 if (view == NULL) {
4416 error = got_error_from_errno("view_open");
4417 goto done;
4419 error = open_tree_view(view, tree, commit_id, &refs, repo);
4420 if (error)
4421 goto done;
4422 error = view_loop(view);
4423 done:
4424 free(repo_path);
4425 free(commit_id);
4426 if (commit)
4427 got_object_commit_close(commit);
4428 if (tree)
4429 got_object_tree_close(tree);
4430 if (repo)
4431 got_repo_close(repo);
4432 got_ref_list_free(&refs);
4433 return error;
4436 __dead static void
4437 usage(void)
4439 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4440 exit(1);
4443 static char **
4444 make_argv(const char *arg0, const char *arg1)
4446 char **argv;
4447 int argc = (arg1 == NULL ? 1 : 2);
4449 argv = calloc(argc, sizeof(char *));
4450 if (argv == NULL)
4451 err(1, "calloc");
4452 argv[0] = strdup(arg0);
4453 if (argv[0] == NULL)
4454 err(1, "calloc");
4455 if (arg1) {
4456 argv[1] = strdup(arg1);
4457 if (argv[1] == NULL)
4458 err(1, "calloc");
4461 return argv;
4464 int
4465 main(int argc, char *argv[])
4467 const struct got_error *error = NULL;
4468 struct tog_cmd *cmd = NULL;
4469 int ch, hflag = 0;
4470 char **cmd_argv = NULL;
4472 setlocale(LC_CTYPE, "");
4474 while ((ch = getopt(argc, argv, "h")) != -1) {
4475 switch (ch) {
4476 case 'h':
4477 hflag = 1;
4478 break;
4479 default:
4480 usage();
4481 /* NOTREACHED */
4485 argc -= optind;
4486 argv += optind;
4487 optind = 0;
4488 optreset = 1;
4490 if (argc == 0) {
4491 if (hflag)
4492 usage();
4493 /* Build an argument vector which runs a default command. */
4494 cmd = &tog_commands[0];
4495 cmd_argv = make_argv(cmd->name, NULL);
4496 argc = 1;
4497 } else {
4498 int i;
4500 /* Did the user specific a command? */
4501 for (i = 0; i < nitems(tog_commands); i++) {
4502 if (strncmp(tog_commands[i].name, argv[0],
4503 strlen(argv[0])) == 0) {
4504 cmd = &tog_commands[i];
4505 if (hflag)
4506 tog_commands[i].cmd_usage();
4507 break;
4510 if (cmd == NULL) {
4511 /* Did the user specify a repository? */
4512 char *repo_path = realpath(argv[0], NULL);
4513 if (repo_path) {
4514 struct got_repository *repo;
4515 error = got_repo_open(&repo, repo_path);
4516 if (error == NULL)
4517 got_repo_close(repo);
4518 } else
4519 error = got_error_from_errno2("realpath",
4520 argv[0]);
4521 if (error) {
4522 if (hflag) {
4523 fprintf(stderr, "%s: '%s' is not a "
4524 "known command\n", getprogname(),
4525 argv[0]);
4526 usage();
4528 fprintf(stderr, "%s: '%s' is neither a known "
4529 "command nor a path to a repository\n",
4530 getprogname(), argv[0]);
4531 free(repo_path);
4532 return 1;
4534 cmd = &tog_commands[0];
4535 cmd_argv = make_argv(cmd->name, repo_path);
4536 argc = 2;
4537 free(repo_path);
4541 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4542 if (error)
4543 goto done;
4544 done:
4545 endwin();
4546 free(cmd_argv);
4547 if (error)
4548 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4549 return 0;