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 view->searching == TOG_SEARCH_BACKWARD) {
1769 view->search_next_done = 1;
1770 return NULL;
1772 s->thread_args.commits_needed = 1;
1773 return trigger_log_thread(0,
1774 &s->thread_args.commits_needed,
1775 &s->thread_args.log_complete,
1776 &s->thread_args.need_commits);
1779 if (match_commit(entry->commit, &view->regex)) {
1780 view->search_next_done = 1;
1781 s->matched_entry = entry;
1782 break;
1784 if (view->searching == TOG_SEARCH_FORWARD)
1785 entry = TAILQ_NEXT(entry, entry);
1786 else
1787 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1790 if (s->matched_entry) {
1791 int cur = s->selected_entry->idx;
1792 while (cur < s->matched_entry->idx) {
1793 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1794 if (err)
1795 return err;
1796 cur++;
1798 while (cur > s->matched_entry->idx) {
1799 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1800 if (err)
1801 return err;
1802 cur--;
1806 return NULL;
1809 static const struct got_error *
1810 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1811 struct got_reflist_head *refs, struct got_repository *repo,
1812 const char *path, int check_disk)
1814 const struct got_error *err = NULL;
1815 struct tog_log_view_state *s = &view->state.log;
1816 struct got_repository *thread_repo = NULL;
1817 struct got_commit_graph *thread_graph = NULL;
1818 int errcode;
1820 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1821 if (err != NULL)
1822 goto done;
1824 /* The commit queue only contains commits being displayed. */
1825 TAILQ_INIT(&s->commits.head);
1826 s->commits.ncommits = 0;
1828 s->refs = refs;
1829 s->repo = repo;
1830 s->start_id = got_object_id_dup(start_id);
1831 if (s->start_id == NULL) {
1832 err = got_error_from_errno("got_object_id_dup");
1833 goto done;
1836 view->show = show_log_view;
1837 view->input = input_log_view;
1838 view->close = close_log_view;
1839 view->search_start = search_start_log_view;
1840 view->search_next = search_next_log_view;
1842 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1843 if (err)
1844 goto done;
1845 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1846 0, thread_repo);
1847 if (err)
1848 goto done;
1850 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1851 if (errcode) {
1852 err = got_error_set_errno(errcode, "pthread_cond_init");
1853 goto done;
1856 s->thread_args.commits_needed = view->nlines;
1857 s->thread_args.graph = thread_graph;
1858 s->thread_args.commits = &s->commits;
1859 s->thread_args.in_repo_path = s->in_repo_path;
1860 s->thread_args.start_id = s->start_id;
1861 s->thread_args.repo = thread_repo;
1862 s->thread_args.log_complete = 0;
1863 s->thread_args.quit = &s->quit;
1864 s->thread_args.view = view;
1865 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1866 s->thread_args.selected_entry = &s->selected_entry;
1867 done:
1868 if (err)
1869 close_log_view(view);
1870 return err;
1873 static const struct got_error *
1874 show_log_view(struct tog_view *view)
1876 struct tog_log_view_state *s = &view->state.log;
1878 if (s->thread == NULL) {
1879 int errcode = pthread_create(&s->thread, NULL, log_thread,
1880 &s->thread_args);
1881 if (errcode)
1882 return got_error_set_errno(errcode, "pthread_create");
1885 return draw_commits(view, &s->last_displayed_entry,
1886 &s->selected_entry, s->first_displayed_entry,
1887 &s->commits, s->selected, view->nlines, s->refs,
1888 s->in_repo_path, s->thread_args.commits_needed);
1891 static const struct got_error *
1892 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1893 struct tog_view **focus_view, struct tog_view *view, int ch)
1895 const struct got_error *err = NULL;
1896 struct tog_log_view_state *s = &view->state.log;
1897 char *parent_path;
1898 struct tog_view *diff_view = NULL, *tree_view = NULL;
1899 int begin_x = 0;
1901 switch (ch) {
1902 case 'q':
1903 s->quit = 1;
1904 break;
1905 case 'k':
1906 case KEY_UP:
1907 case '<':
1908 case ',':
1909 if (s->first_displayed_entry == NULL)
1910 break;
1911 if (s->selected > 0)
1912 s->selected--;
1913 else
1914 scroll_up(view, &s->first_displayed_entry, 1,
1915 &s->commits);
1916 break;
1917 case KEY_PPAGE:
1918 case CTRL('b'):
1919 if (s->first_displayed_entry == NULL)
1920 break;
1921 if (TAILQ_FIRST(&s->commits.head) ==
1922 s->first_displayed_entry) {
1923 s->selected = 0;
1924 break;
1926 scroll_up(view, &s->first_displayed_entry,
1927 view->nlines, &s->commits);
1928 break;
1929 case 'j':
1930 case KEY_DOWN:
1931 case '>':
1932 case '.':
1933 if (s->first_displayed_entry == NULL)
1934 break;
1935 if (s->selected < MIN(view->nlines - 2,
1936 s->commits.ncommits - 1)) {
1937 s->selected++;
1938 break;
1940 err = scroll_down(view, &s->first_displayed_entry, 1,
1941 &s->last_displayed_entry, &s->commits,
1942 &s->thread_args.log_complete,
1943 &s->thread_args.commits_needed,
1944 &s->thread_args.need_commits);
1945 break;
1946 case KEY_NPAGE:
1947 case CTRL('f'): {
1948 struct commit_queue_entry *first;
1949 first = s->first_displayed_entry;
1950 if (first == NULL)
1951 break;
1952 err = scroll_down(view, &s->first_displayed_entry,
1953 view->nlines, &s->last_displayed_entry,
1954 &s->commits, &s->thread_args.log_complete,
1955 &s->thread_args.commits_needed,
1956 &s->thread_args.need_commits);
1957 if (first == s->first_displayed_entry &&
1958 s->selected < MIN(view->nlines - 2,
1959 s->commits.ncommits - 1)) {
1960 /* can't scroll further down */
1961 s->selected = MIN(view->nlines - 2,
1962 s->commits.ncommits - 1);
1964 err = NULL;
1965 break;
1967 case KEY_RESIZE:
1968 if (s->selected > view->nlines - 2)
1969 s->selected = view->nlines - 2;
1970 if (s->selected > s->commits.ncommits - 1)
1971 s->selected = s->commits.ncommits - 1;
1972 break;
1973 case KEY_ENTER:
1974 case ' ':
1975 case '\r':
1976 if (s->selected_entry == NULL)
1977 break;
1978 if (view_is_parent_view(view))
1979 begin_x = view_split_begin_x(view->begin_x);
1980 err = open_diff_view_for_commit(&diff_view, begin_x,
1981 s->selected_entry->commit, s->selected_entry->id,
1982 view, s->refs, s->repo);
1983 if (err)
1984 break;
1985 if (view_is_parent_view(view)) {
1986 err = view_close_child(view);
1987 if (err)
1988 return err;
1989 err = view_set_child(view, diff_view);
1990 if (err) {
1991 view_close(diff_view);
1992 break;
1994 *focus_view = diff_view;
1995 view->child_focussed = 1;
1996 } else
1997 *new_view = diff_view;
1998 break;
1999 case 't':
2000 if (s->selected_entry == NULL)
2001 break;
2002 if (view_is_parent_view(view))
2003 begin_x = view_split_begin_x(view->begin_x);
2004 err = browse_commit_tree(&tree_view, begin_x,
2005 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2006 if (err)
2007 break;
2008 if (view_is_parent_view(view)) {
2009 err = view_close_child(view);
2010 if (err)
2011 return err;
2012 err = view_set_child(view, tree_view);
2013 if (err) {
2014 view_close(tree_view);
2015 break;
2017 *focus_view = tree_view;
2018 view->child_focussed = 1;
2019 } else
2020 *new_view = tree_view;
2021 break;
2022 case KEY_BACKSPACE:
2023 if (strcmp(s->in_repo_path, "/") == 0)
2024 break;
2025 parent_path = dirname(s->in_repo_path);
2026 if (parent_path && strcmp(parent_path, ".") != 0) {
2027 struct tog_view *lv;
2028 err = stop_log_thread(s);
2029 if (err)
2030 return err;
2031 lv = view_open(view->nlines, view->ncols,
2032 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2033 if (lv == NULL)
2034 return got_error_from_errno(
2035 "view_open");
2036 err = open_log_view(lv, s->start_id, s->refs,
2037 s->repo, parent_path, 0);
2038 if (err)
2039 return err;;
2040 if (view_is_parent_view(view))
2041 *new_view = lv;
2042 else {
2043 view_set_child(view->parent, lv);
2044 *focus_view = lv;
2046 return NULL;
2048 break;
2049 default:
2050 break;
2053 return err;
2056 static const struct got_error *
2057 apply_unveil(const char *repo_path, const char *worktree_path)
2059 const struct got_error *error;
2061 if (repo_path && unveil(repo_path, "r") != 0)
2062 return got_error_from_errno2("unveil", repo_path);
2064 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2065 return got_error_from_errno2("unveil", worktree_path);
2067 if (unveil("/tmp", "rwc") != 0)
2068 return got_error_from_errno2("unveil", "/tmp");
2070 error = got_privsep_unveil_exec_helpers();
2071 if (error != NULL)
2072 return error;
2074 if (unveil(NULL, NULL) != 0)
2075 return got_error_from_errno("unveil");
2077 return NULL;
2080 static void
2081 init_curses(void)
2083 initscr();
2084 cbreak();
2085 halfdelay(1); /* Do fast refresh while initial view is loading. */
2086 noecho();
2087 nonl();
2088 intrflush(stdscr, FALSE);
2089 keypad(stdscr, TRUE);
2090 curs_set(0);
2091 signal(SIGWINCH, tog_sigwinch);
2094 static const struct got_error *
2095 cmd_log(int argc, char *argv[])
2097 const struct got_error *error;
2098 struct got_repository *repo = NULL;
2099 struct got_worktree *worktree = NULL;
2100 struct got_reflist_head refs;
2101 struct got_object_id *start_id = NULL;
2102 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2103 char *start_commit = NULL;
2104 int ch;
2105 struct tog_view *view;
2107 SIMPLEQ_INIT(&refs);
2109 #ifndef PROFILE
2110 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2111 NULL) == -1)
2112 err(1, "pledge");
2113 #endif
2115 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2116 switch (ch) {
2117 case 'c':
2118 start_commit = optarg;
2119 break;
2120 case 'r':
2121 repo_path = realpath(optarg, NULL);
2122 if (repo_path == NULL)
2123 err(1, "-r option");
2124 break;
2125 default:
2126 usage_log();
2127 /* NOTREACHED */
2131 argc -= optind;
2132 argv += optind;
2134 cwd = getcwd(NULL, 0);
2135 if (cwd == NULL) {
2136 error = got_error_from_errno("getcwd");
2137 goto done;
2139 error = got_worktree_open(&worktree, cwd);
2140 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2141 goto done;
2142 error = NULL;
2144 if (argc == 0) {
2145 path = strdup("");
2146 if (path == NULL) {
2147 error = got_error_from_errno("strdup");
2148 goto done;
2150 } else if (argc == 1) {
2151 if (worktree) {
2152 error = got_worktree_resolve_path(&path, worktree,
2153 argv[0]);
2154 if (error)
2155 goto done;
2156 } else {
2157 path = strdup(argv[0]);
2158 if (path == NULL) {
2159 error = got_error_from_errno("strdup");
2160 goto done;
2163 } else
2164 usage_log();
2166 repo_path = worktree ?
2167 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2168 if (repo_path == NULL) {
2169 error = got_error_from_errno("strdup");
2170 goto done;
2173 init_curses();
2175 error = got_repo_open(&repo, repo_path);
2176 if (error != NULL)
2177 goto done;
2179 error = apply_unveil(got_repo_get_path(repo),
2180 worktree ? got_worktree_get_root_path(worktree) : NULL);
2181 if (error)
2182 goto done;
2184 if (start_commit == NULL)
2185 error = get_head_commit_id(&start_id, worktree ?
2186 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2187 repo);
2188 else
2189 error = got_object_resolve_id_str(&start_id, repo,
2190 start_commit);
2191 if (error != NULL)
2192 goto done;
2194 error = got_ref_list(&refs, repo);
2195 if (error)
2196 goto done;
2198 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2199 if (view == NULL) {
2200 error = got_error_from_errno("view_open");
2201 goto done;
2203 error = open_log_view(view, start_id, &refs, repo, path, 1);
2204 if (error)
2205 goto done;
2206 error = view_loop(view);
2207 done:
2208 free(repo_path);
2209 free(cwd);
2210 free(path);
2211 free(start_id);
2212 if (repo)
2213 got_repo_close(repo);
2214 if (worktree)
2215 got_worktree_close(worktree);
2216 got_ref_list_free(&refs);
2217 return error;
2220 __dead static void
2221 usage_diff(void)
2223 endwin();
2224 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2225 getprogname());
2226 exit(1);
2229 static char *
2230 parse_next_line(FILE *f, size_t *len)
2232 char *line;
2233 size_t linelen;
2234 size_t lineno;
2235 const char delim[3] = { '\0', '\0', '\0'};
2237 line = fparseln(f, &linelen, &lineno, delim, 0);
2238 if (len)
2239 *len = linelen;
2240 return line;
2243 static const struct got_error *
2244 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2245 int *last_displayed_line, int *eof, int max_lines,
2246 char *header)
2248 const struct got_error *err;
2249 int nlines = 0, nprinted = 0;
2250 char *line;
2251 size_t len;
2252 wchar_t *wline;
2253 int width;
2255 rewind(f);
2256 werase(view->window);
2258 if (header) {
2259 err = format_line(&wline, &width, header, view->ncols);
2260 if (err) {
2261 return err;
2264 if (view_needs_focus_indication(view))
2265 wstandout(view->window);
2266 waddwstr(view->window, wline);
2267 if (view_needs_focus_indication(view))
2268 wstandend(view->window);
2269 if (width < view->ncols - 1)
2270 waddch(view->window, '\n');
2272 if (max_lines <= 1)
2273 return NULL;
2274 max_lines--;
2277 *eof = 0;
2278 while (nprinted < max_lines) {
2279 line = parse_next_line(f, &len);
2280 if (line == NULL) {
2281 *eof = 1;
2282 break;
2284 if (++nlines < *first_displayed_line) {
2285 free(line);
2286 continue;
2289 err = format_line(&wline, &width, line, view->ncols);
2290 if (err) {
2291 free(line);
2292 return err;
2294 waddwstr(view->window, wline);
2295 if (width < view->ncols - 1)
2296 waddch(view->window, '\n');
2297 if (++nprinted == 1)
2298 *first_displayed_line = nlines;
2299 free(line);
2300 free(wline);
2301 wline = NULL;
2303 *last_displayed_line = nlines;
2305 view_vborder(view);
2307 if (*eof) {
2308 while (nprinted < view->nlines) {
2309 waddch(view->window, '\n');
2310 nprinted++;
2313 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2314 if (err) {
2315 return err;
2318 wstandout(view->window);
2319 waddwstr(view->window, wline);
2320 wstandend(view->window);
2323 return NULL;
2326 static char *
2327 get_datestr(time_t *time, char *datebuf)
2329 char *p, *s = ctime_r(time, datebuf);
2330 p = strchr(s, '\n');
2331 if (p)
2332 *p = '\0';
2333 return s;
2336 static const struct got_error *
2337 write_commit_info(struct got_object_id *commit_id,
2338 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2340 const struct got_error *err = NULL;
2341 char datebuf[26];
2342 struct got_commit_object *commit;
2343 char *id_str = NULL;
2344 time_t committer_time;
2345 const char *author, *committer;
2346 char *refs_str = NULL;
2348 if (refs) {
2349 err = build_refs_str(&refs_str, refs, commit_id);
2350 if (err)
2351 return err;
2354 err = got_object_open_as_commit(&commit, repo, commit_id);
2355 if (err)
2356 return err;
2358 err = got_object_id_str(&id_str, commit_id);
2359 if (err) {
2360 err = got_error_from_errno("got_object_id_str");
2361 goto done;
2364 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2365 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2366 err = got_error_from_errno("fprintf");
2367 goto done;
2369 if (fprintf(outfile, "from: %s\n",
2370 got_object_commit_get_author(commit)) < 0) {
2371 err = got_error_from_errno("fprintf");
2372 goto done;
2374 committer_time = got_object_commit_get_committer_time(commit);
2375 if (fprintf(outfile, "date: %s UTC\n",
2376 get_datestr(&committer_time, datebuf)) < 0) {
2377 err = got_error_from_errno("fprintf");
2378 goto done;
2380 author = got_object_commit_get_author(commit);
2381 committer = got_object_commit_get_committer(commit);
2382 if (strcmp(author, committer) != 0 &&
2383 fprintf(outfile, "via: %s\n", committer) < 0) {
2384 err = got_error_from_errno("fprintf");
2385 goto done;
2387 if (fprintf(outfile, "%s\n",
2388 got_object_commit_get_logmsg(commit)) < 0) {
2389 err = got_error_from_errno("fprintf");
2390 goto done;
2392 done:
2393 free(id_str);
2394 free(refs_str);
2395 got_object_commit_close(commit);
2396 return err;
2399 static const struct got_error *
2400 create_diff(struct tog_diff_view_state *s)
2402 const struct got_error *err = NULL;
2403 FILE *f = NULL;
2404 int obj_type;
2406 f = got_opentemp();
2407 if (f == NULL) {
2408 err = got_error_from_errno("got_opentemp");
2409 goto done;
2411 if (s->f && fclose(s->f) != 0) {
2412 err = got_error_from_errno("fclose");
2413 goto done;
2415 s->f = f;
2417 if (s->id1)
2418 err = got_object_get_type(&obj_type, s->repo, s->id1);
2419 else
2420 err = got_object_get_type(&obj_type, s->repo, s->id2);
2421 if (err)
2422 goto done;
2424 switch (obj_type) {
2425 case GOT_OBJ_TYPE_BLOB:
2426 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2427 s->diff_context, s->repo, f);
2428 break;
2429 case GOT_OBJ_TYPE_TREE:
2430 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2431 s->diff_context, s->repo, f);
2432 break;
2433 case GOT_OBJ_TYPE_COMMIT: {
2434 const struct got_object_id_queue *parent_ids;
2435 struct got_object_qid *pid;
2436 struct got_commit_object *commit2;
2438 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2439 if (err)
2440 break;
2441 /* Show commit info if we're diffing to a parent/root commit. */
2442 if (s->id1 == NULL)
2443 write_commit_info(s->id2, s->refs, s->repo, f);
2444 else {
2445 parent_ids = got_object_commit_get_parent_ids(commit2);
2446 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2447 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2448 write_commit_info(s->id2, s->refs,
2449 s->repo, f);
2450 break;
2454 got_object_commit_close(commit2);
2456 err = got_diff_objects_as_commits(s->id1, s->id2,
2457 s->diff_context, s->repo, f);
2458 break;
2460 default:
2461 err = got_error(GOT_ERR_OBJ_TYPE);
2462 break;
2464 done:
2465 if (f && fflush(f) != 0 && err == NULL)
2466 err = got_error_from_errno("fflush");
2467 return err;
2470 static void
2471 diff_view_indicate_progress(struct tog_view *view)
2473 mvwaddstr(view->window, 0, 0, "diffing...");
2474 update_panels();
2475 doupdate();
2478 static const struct got_error *
2479 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2480 struct got_object_id *id2, struct tog_view *log_view,
2481 struct got_reflist_head *refs, struct got_repository *repo)
2483 const struct got_error *err;
2485 if (id1 != NULL && id2 != NULL) {
2486 int type1, type2;
2487 err = got_object_get_type(&type1, repo, id1);
2488 if (err)
2489 return err;
2490 err = got_object_get_type(&type2, repo, id2);
2491 if (err)
2492 return err;
2494 if (type1 != type2)
2495 return got_error(GOT_ERR_OBJ_TYPE);
2498 if (id1) {
2499 view->state.diff.id1 = got_object_id_dup(id1);
2500 if (view->state.diff.id1 == NULL)
2501 return got_error_from_errno("got_object_id_dup");
2502 } else
2503 view->state.diff.id1 = NULL;
2505 view->state.diff.id2 = got_object_id_dup(id2);
2506 if (view->state.diff.id2 == NULL) {
2507 free(view->state.diff.id1);
2508 view->state.diff.id1 = NULL;
2509 return got_error_from_errno("got_object_id_dup");
2511 view->state.diff.f = NULL;
2512 view->state.diff.first_displayed_line = 1;
2513 view->state.diff.last_displayed_line = view->nlines;
2514 view->state.diff.diff_context = 3;
2515 view->state.diff.log_view = log_view;
2516 view->state.diff.repo = repo;
2517 view->state.diff.refs = refs;
2519 if (log_view && view_is_splitscreen(view))
2520 show_log_view(log_view); /* draw vborder */
2521 diff_view_indicate_progress(view);
2523 err = create_diff(&view->state.diff);
2524 if (err) {
2525 free(view->state.diff.id1);
2526 view->state.diff.id1 = NULL;
2527 free(view->state.diff.id2);
2528 view->state.diff.id2 = NULL;
2529 return err;
2532 view->show = show_diff_view;
2533 view->input = input_diff_view;
2534 view->close = close_diff_view;
2536 return NULL;
2539 static const struct got_error *
2540 close_diff_view(struct tog_view *view)
2542 const struct got_error *err = NULL;
2544 free(view->state.diff.id1);
2545 view->state.diff.id1 = NULL;
2546 free(view->state.diff.id2);
2547 view->state.diff.id2 = NULL;
2548 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2549 err = got_error_from_errno("fclose");
2550 return err;
2553 static const struct got_error *
2554 show_diff_view(struct tog_view *view)
2556 const struct got_error *err;
2557 struct tog_diff_view_state *s = &view->state.diff;
2558 char *id_str1 = NULL, *id_str2, *header;
2560 if (s->id1) {
2561 err = got_object_id_str(&id_str1, s->id1);
2562 if (err)
2563 return err;
2565 err = got_object_id_str(&id_str2, s->id2);
2566 if (err)
2567 return err;
2569 if (asprintf(&header, "diff %s %s",
2570 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2571 err = got_error_from_errno("asprintf");
2572 free(id_str1);
2573 free(id_str2);
2574 return err;
2576 free(id_str1);
2577 free(id_str2);
2579 return draw_file(view, s->f, &s->first_displayed_line,
2580 &s->last_displayed_line, &s->eof, view->nlines,
2581 header);
2584 static const struct got_error *
2585 set_selected_commit(struct tog_diff_view_state *s,
2586 struct commit_queue_entry *entry)
2588 const struct got_error *err;
2589 const struct got_object_id_queue *parent_ids;
2590 struct got_commit_object *selected_commit;
2591 struct got_object_qid *pid;
2593 free(s->id2);
2594 s->id2 = got_object_id_dup(entry->id);
2595 if (s->id2 == NULL)
2596 return got_error_from_errno("got_object_id_dup");
2598 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2599 if (err)
2600 return err;
2601 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2602 free(s->id1);
2603 pid = SIMPLEQ_FIRST(parent_ids);
2604 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2605 got_object_commit_close(selected_commit);
2606 return NULL;
2609 static const struct got_error *
2610 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2611 struct tog_view **focus_view, struct tog_view *view, int ch)
2613 const struct got_error *err = NULL;
2614 struct tog_diff_view_state *s = &view->state.diff;
2615 struct tog_log_view_state *ls;
2616 struct commit_queue_entry *entry;
2617 int i;
2619 switch (ch) {
2620 case 'k':
2621 case KEY_UP:
2622 if (s->first_displayed_line > 1)
2623 s->first_displayed_line--;
2624 break;
2625 case KEY_PPAGE:
2626 case CTRL('b'):
2627 if (s->first_displayed_line == 1)
2628 break;
2629 i = 0;
2630 while (i++ < view->nlines - 1 &&
2631 s->first_displayed_line > 1)
2632 s->first_displayed_line--;
2633 break;
2634 case 'j':
2635 case KEY_DOWN:
2636 if (!s->eof)
2637 s->first_displayed_line++;
2638 break;
2639 case KEY_NPAGE:
2640 case CTRL('f'):
2641 case ' ':
2642 if (s->eof)
2643 break;
2644 i = 0;
2645 while (!s->eof && i++ < view->nlines - 1) {
2646 char *line;
2647 line = parse_next_line(s->f, NULL);
2648 s->first_displayed_line++;
2649 if (line == NULL)
2650 break;
2652 break;
2653 case '[':
2654 if (s->diff_context > 0) {
2655 s->diff_context--;
2656 diff_view_indicate_progress(view);
2657 err = create_diff(s);
2659 break;
2660 case ']':
2661 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2662 s->diff_context++;
2663 diff_view_indicate_progress(view);
2664 err = create_diff(s);
2666 break;
2667 case '<':
2668 case ',':
2669 if (s->log_view == NULL)
2670 break;
2671 ls = &s->log_view->state.log;
2672 entry = TAILQ_PREV(ls->selected_entry,
2673 commit_queue_head, entry);
2674 if (entry == NULL)
2675 break;
2677 err = input_log_view(NULL, NULL, NULL, s->log_view,
2678 KEY_UP);
2679 if (err)
2680 break;
2682 err = set_selected_commit(s, entry);
2683 if (err)
2684 break;
2686 s->first_displayed_line = 1;
2687 s->last_displayed_line = view->nlines;
2689 diff_view_indicate_progress(view);
2690 err = create_diff(s);
2691 break;
2692 case '>':
2693 case '.':
2694 if (s->log_view == NULL)
2695 break;
2696 ls = &s->log_view->state.log;
2698 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2699 ls->thread_args.commits_needed++;
2701 /* Display "loading..." in log view. */
2702 show_log_view(s->log_view);
2703 update_panels();
2704 doupdate();
2706 err = trigger_log_thread(1 /* load_all */,
2707 &ls->thread_args.commits_needed,
2708 &ls->thread_args.log_complete,
2709 &ls->thread_args.need_commits);
2710 if (err)
2711 break;
2713 err = input_log_view(NULL, NULL, NULL, s->log_view,
2714 KEY_DOWN);
2715 if (err)
2716 break;
2718 entry = TAILQ_NEXT(ls->selected_entry, entry);
2719 if (entry == NULL)
2720 break;
2722 err = set_selected_commit(s, entry);
2723 if (err)
2724 break;
2726 s->first_displayed_line = 1;
2727 s->last_displayed_line = view->nlines;
2729 diff_view_indicate_progress(view);
2730 err = create_diff(s);
2731 break;
2732 default:
2733 break;
2736 return err;
2739 static const struct got_error *
2740 cmd_diff(int argc, char *argv[])
2742 const struct got_error *error = NULL;
2743 struct got_repository *repo = NULL;
2744 struct got_reflist_head refs;
2745 struct got_object_id *id1 = NULL, *id2 = NULL;
2746 char *repo_path = NULL;
2747 char *id_str1 = NULL, *id_str2 = NULL;
2748 int ch;
2749 struct tog_view *view;
2751 SIMPLEQ_INIT(&refs);
2753 #ifndef PROFILE
2754 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2755 NULL) == -1)
2756 err(1, "pledge");
2757 #endif
2759 while ((ch = getopt(argc, argv, "")) != -1) {
2760 switch (ch) {
2761 default:
2762 usage_diff();
2763 /* NOTREACHED */
2767 argc -= optind;
2768 argv += optind;
2770 if (argc == 0) {
2771 usage_diff(); /* TODO show local worktree changes */
2772 } else if (argc == 2) {
2773 repo_path = getcwd(NULL, 0);
2774 if (repo_path == NULL)
2775 return got_error_from_errno("getcwd");
2776 id_str1 = argv[0];
2777 id_str2 = argv[1];
2778 } else if (argc == 3) {
2779 repo_path = realpath(argv[0], NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath", argv[0]);
2782 id_str1 = argv[1];
2783 id_str2 = argv[2];
2784 } else
2785 usage_diff();
2787 init_curses();
2789 error = got_repo_open(&repo, repo_path);
2790 if (error)
2791 goto done;
2793 error = apply_unveil(got_repo_get_path(repo), NULL);
2794 if (error)
2795 goto done;
2797 error = got_object_resolve_id_str(&id1, repo, id_str1);
2798 if (error)
2799 goto done;
2801 error = got_object_resolve_id_str(&id2, repo, id_str2);
2802 if (error)
2803 goto done;
2805 error = got_ref_list(&refs, repo);
2806 if (error)
2807 goto done;
2809 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2810 if (view == NULL) {
2811 error = got_error_from_errno("view_open");
2812 goto done;
2814 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2815 if (error)
2816 goto done;
2817 error = view_loop(view);
2818 done:
2819 free(repo_path);
2820 got_repo_close(repo);
2821 got_ref_list_free(&refs);
2822 return error;
2825 __dead static void
2826 usage_blame(void)
2828 endwin();
2829 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2830 getprogname());
2831 exit(1);
2834 struct tog_blame_line {
2835 int annotated;
2836 struct got_object_id *id;
2839 static const struct got_error *
2840 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2841 const char *path, struct tog_blame_line *lines, int nlines,
2842 int blame_complete, int selected_line, int *first_displayed_line,
2843 int *last_displayed_line, int *eof, int max_lines)
2845 const struct got_error *err;
2846 int lineno = 0, nprinted = 0;
2847 char *line;
2848 size_t len;
2849 wchar_t *wline;
2850 int width, wlimit;
2851 struct tog_blame_line *blame_line;
2852 struct got_object_id *prev_id = NULL;
2853 char *id_str;
2855 err = got_object_id_str(&id_str, id);
2856 if (err)
2857 return err;
2859 rewind(f);
2860 werase(view->window);
2862 if (asprintf(&line, "commit %s", id_str) == -1) {
2863 err = got_error_from_errno("asprintf");
2864 free(id_str);
2865 return err;
2868 err = format_line(&wline, &width, line, view->ncols);
2869 free(line);
2870 line = NULL;
2871 if (view_needs_focus_indication(view))
2872 wstandout(view->window);
2873 waddwstr(view->window, wline);
2874 if (view_needs_focus_indication(view))
2875 wstandend(view->window);
2876 free(wline);
2877 wline = NULL;
2878 if (width < view->ncols - 1)
2879 waddch(view->window, '\n');
2881 if (asprintf(&line, "[%d/%d] %s%s",
2882 *first_displayed_line - 1 + selected_line, nlines,
2883 blame_complete ? "" : "annotating... ", path) == -1) {
2884 free(id_str);
2885 return got_error_from_errno("asprintf");
2887 free(id_str);
2888 err = format_line(&wline, &width, line, view->ncols);
2889 free(line);
2890 line = NULL;
2891 if (err)
2892 return err;
2893 waddwstr(view->window, wline);
2894 free(wline);
2895 wline = NULL;
2896 if (width < view->ncols - 1)
2897 waddch(view->window, '\n');
2899 *eof = 0;
2900 while (nprinted < max_lines - 2) {
2901 line = parse_next_line(f, &len);
2902 if (line == NULL) {
2903 *eof = 1;
2904 break;
2906 if (++lineno < *first_displayed_line) {
2907 free(line);
2908 continue;
2911 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
2912 err = format_line(&wline, &width, line, wlimit);
2913 if (err) {
2914 free(line);
2915 return err;
2918 if (view->focussed && nprinted == selected_line - 1)
2919 wstandout(view->window);
2921 blame_line = &lines[lineno - 1];
2922 if (blame_line->annotated && prev_id &&
2923 got_object_id_cmp(prev_id, blame_line->id) == 0)
2924 waddstr(view->window, " ");
2925 else if (blame_line->annotated) {
2926 char *id_str;
2927 err = got_object_id_str(&id_str, blame_line->id);
2928 if (err) {
2929 free(line);
2930 free(wline);
2931 return err;
2933 wprintw(view->window, "%.8s ", id_str);
2934 free(id_str);
2935 prev_id = blame_line->id;
2936 } else {
2937 waddstr(view->window, "........ ");
2938 prev_id = NULL;
2941 waddwstr(view->window, wline);
2942 while (width < wlimit) {
2943 waddch(view->window, ' ');
2944 width++;
2946 if (view->focussed && nprinted == selected_line - 1)
2947 wstandend(view->window);
2948 if (++nprinted == 1)
2949 *first_displayed_line = lineno;
2950 free(line);
2951 free(wline);
2952 wline = NULL;
2954 *last_displayed_line = lineno;
2956 view_vborder(view);
2958 return NULL;
2961 static const struct got_error *
2962 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2964 const struct got_error *err = NULL;
2965 struct tog_blame_cb_args *a = arg;
2966 struct tog_blame_line *line;
2967 int errcode;
2969 if (nlines != a->nlines ||
2970 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2971 return got_error(GOT_ERR_RANGE);
2973 errcode = pthread_mutex_lock(&tog_mutex);
2974 if (errcode)
2975 return got_error_set_errno(errcode, "pthread_mutex_lock");
2977 if (*a->quit) { /* user has quit the blame view */
2978 err = got_error(GOT_ERR_ITER_COMPLETED);
2979 goto done;
2982 if (lineno == -1)
2983 goto done; /* no change in this commit */
2985 line = &a->lines[lineno - 1];
2986 if (line->annotated)
2987 goto done;
2989 line->id = got_object_id_dup(id);
2990 if (line->id == NULL) {
2991 err = got_error_from_errno("got_object_id_dup");
2992 goto done;
2994 line->annotated = 1;
2995 done:
2996 errcode = pthread_mutex_unlock(&tog_mutex);
2997 if (errcode)
2998 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
2999 return err;
3002 static void *
3003 blame_thread(void *arg)
3005 const struct got_error *err;
3006 struct tog_blame_thread_args *ta = arg;
3007 struct tog_blame_cb_args *a = ta->cb_args;
3008 int errcode;
3010 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3011 blame_cb, ta->cb_args);
3013 errcode = pthread_mutex_lock(&tog_mutex);
3014 if (errcode)
3015 return (void *)got_error_set_errno(errcode,
3016 "pthread_mutex_lock");
3018 got_repo_close(ta->repo);
3019 ta->repo = NULL;
3020 *ta->complete = 1;
3022 errcode = pthread_mutex_unlock(&tog_mutex);
3023 if (errcode && err == NULL)
3024 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3026 return (void *)err;
3029 static struct got_object_id *
3030 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3031 int selected_line)
3033 struct tog_blame_line *line;
3035 line = &lines[first_displayed_line - 1 + selected_line - 1];
3036 if (!line->annotated)
3037 return NULL;
3039 return line->id;
3042 static const struct got_error *
3043 stop_blame(struct tog_blame *blame)
3045 const struct got_error *err = NULL;
3046 int i;
3048 if (blame->thread) {
3049 int errcode;
3050 errcode = pthread_mutex_unlock(&tog_mutex);
3051 if (errcode)
3052 return got_error_set_errno(errcode,
3053 "pthread_mutex_unlock");
3054 errcode = pthread_join(blame->thread, (void **)&err);
3055 if (errcode)
3056 return got_error_set_errno(errcode, "pthread_join");
3057 errcode = pthread_mutex_lock(&tog_mutex);
3058 if (errcode)
3059 return got_error_set_errno(errcode,
3060 "pthread_mutex_lock");
3061 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3062 err = NULL;
3063 blame->thread = NULL;
3065 if (blame->thread_args.repo) {
3066 got_repo_close(blame->thread_args.repo);
3067 blame->thread_args.repo = NULL;
3069 if (blame->f) {
3070 if (fclose(blame->f) != 0 && err == NULL)
3071 err = got_error_from_errno("fclose");
3072 blame->f = NULL;
3074 if (blame->lines) {
3075 for (i = 0; i < blame->nlines; i++)
3076 free(blame->lines[i].id);
3077 free(blame->lines);
3078 blame->lines = NULL;
3080 free(blame->cb_args.commit_id);
3081 blame->cb_args.commit_id = NULL;
3083 return err;
3086 static const struct got_error *
3087 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3088 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3089 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3090 struct got_repository *repo)
3092 const struct got_error *err = NULL;
3093 struct got_blob_object *blob = NULL;
3094 struct got_repository *thread_repo = NULL;
3095 struct got_object_id *obj_id = NULL;
3096 int obj_type;
3098 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3099 if (err)
3100 return err;
3101 if (obj_id == NULL)
3102 return got_error(GOT_ERR_NO_OBJ);
3104 err = got_object_get_type(&obj_type, repo, obj_id);
3105 if (err)
3106 goto done;
3108 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3109 err = got_error(GOT_ERR_OBJ_TYPE);
3110 goto done;
3113 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3114 if (err)
3115 goto done;
3116 blame->f = got_opentemp();
3117 if (blame->f == NULL) {
3118 err = got_error_from_errno("got_opentemp");
3119 goto done;
3121 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3122 &blame->line_offsets, blame->f, blob);
3123 if (err)
3124 goto done;
3126 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3127 if (blame->lines == NULL) {
3128 err = got_error_from_errno("calloc");
3129 goto done;
3132 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3133 if (err)
3134 goto done;
3136 blame->cb_args.view = view;
3137 blame->cb_args.lines = blame->lines;
3138 blame->cb_args.nlines = blame->nlines;
3139 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3140 if (blame->cb_args.commit_id == NULL) {
3141 err = got_error_from_errno("got_object_id_dup");
3142 goto done;
3144 blame->cb_args.quit = done;
3146 blame->thread_args.path = path;
3147 blame->thread_args.repo = thread_repo;
3148 blame->thread_args.cb_args = &blame->cb_args;
3149 blame->thread_args.complete = blame_complete;
3150 *blame_complete = 0;
3152 done:
3153 if (blob)
3154 got_object_blob_close(blob);
3155 free(obj_id);
3156 if (err)
3157 stop_blame(blame);
3158 return err;
3161 static const struct got_error *
3162 open_blame_view(struct tog_view *view, char *path,
3163 struct got_object_id *commit_id, struct got_reflist_head *refs,
3164 struct got_repository *repo)
3166 const struct got_error *err = NULL;
3167 struct tog_blame_view_state *s = &view->state.blame;
3169 SIMPLEQ_INIT(&s->blamed_commits);
3171 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3172 if (err)
3173 return err;
3175 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3176 s->first_displayed_line = 1;
3177 s->last_displayed_line = view->nlines;
3178 s->selected_line = 1;
3179 s->blame_complete = 0;
3180 s->path = path;
3181 if (s->path == NULL)
3182 return got_error_from_errno("open_blame_view");
3183 s->repo = repo;
3184 s->refs = refs;
3185 s->commit_id = commit_id;
3186 memset(&s->blame, 0, sizeof(s->blame));
3188 view->show = show_blame_view;
3189 view->input = input_blame_view;
3190 view->close = close_blame_view;
3191 view->search_start = search_start_blame_view;
3192 view->search_next = search_next_blame_view;
3194 return run_blame(&s->blame, view, &s->blame_complete,
3195 &s->first_displayed_line, &s->last_displayed_line,
3196 &s->selected_line, &s->done, &s->eof, s->path,
3197 s->blamed_commit->id, s->repo);
3200 static const struct got_error *
3201 close_blame_view(struct tog_view *view)
3203 const struct got_error *err = NULL;
3204 struct tog_blame_view_state *s = &view->state.blame;
3206 if (s->blame.thread)
3207 err = stop_blame(&s->blame);
3209 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3210 struct got_object_qid *blamed_commit;
3211 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3212 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3213 got_object_qid_free(blamed_commit);
3216 free(s->path);
3218 return err;
3221 static const struct got_error *
3222 search_start_blame_view(struct tog_view *view)
3224 struct tog_blame_view_state *s = &view->state.blame;
3226 s->matched_line = 0;
3227 return NULL;
3230 static int
3231 match_line(const char *line, regex_t *regex)
3233 regmatch_t regmatch;
3235 return regexec(regex, line, 1, &regmatch, 0) == 0;
3239 static const struct got_error *
3240 search_next_blame_view(struct tog_view *view)
3242 struct tog_blame_view_state *s = &view->state.blame;
3243 int lineno;
3245 if (!view->searching) {
3246 view->search_next_done = 1;
3247 return NULL;
3250 if (s->matched_line) {
3251 if (view->searching == TOG_SEARCH_FORWARD)
3252 lineno = s->matched_line + 1;
3253 else
3254 lineno = s->matched_line - 1;
3255 } else {
3256 if (view->searching == TOG_SEARCH_FORWARD)
3257 lineno = 1;
3258 else
3259 lineno = s->blame.nlines;
3262 while (1) {
3263 char *line = NULL;
3264 off_t offset;
3265 size_t len;
3267 if (lineno <= 0 || lineno > s->blame.nlines) {
3268 if (s->matched_line == 0) {
3269 view->search_next_done = 1;
3270 free(line);
3271 break;
3274 if (view->searching == TOG_SEARCH_FORWARD)
3275 lineno = 1;
3276 else
3277 lineno = s->blame.nlines;
3280 offset = s->blame.line_offsets[lineno - 1];
3281 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3282 free(line);
3283 return got_error_from_errno("fseeko");
3285 free(line);
3286 line = parse_next_line(s->blame.f, &len);
3287 if (line && match_line(line, &view->regex)) {
3288 view->search_next_done = 1;
3289 s->matched_line = lineno;
3290 free(line);
3291 break;
3293 free(line);
3294 if (view->searching == TOG_SEARCH_FORWARD)
3295 lineno++;
3296 else
3297 lineno--;
3300 if (s->matched_line) {
3301 s->first_displayed_line = s->matched_line;
3302 s->selected_line = 1;
3305 return NULL;
3308 static const struct got_error *
3309 show_blame_view(struct tog_view *view)
3311 const struct got_error *err = NULL;
3312 struct tog_blame_view_state *s = &view->state.blame;
3313 int errcode;
3315 if (s->blame.thread == NULL) {
3316 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3317 &s->blame.thread_args);
3318 if (errcode)
3319 return got_error_set_errno(errcode, "pthread_create");
3322 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3323 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3324 s->selected_line, &s->first_displayed_line,
3325 &s->last_displayed_line, &s->eof, view->nlines);
3327 view_vborder(view);
3328 return err;
3331 static const struct got_error *
3332 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3333 struct tog_view **focus_view, struct tog_view *view, int ch)
3335 const struct got_error *err = NULL, *thread_err = NULL;
3336 struct tog_view *diff_view;
3337 struct tog_blame_view_state *s = &view->state.blame;
3338 int begin_x = 0;
3340 switch (ch) {
3341 case 'q':
3342 s->done = 1;
3343 break;
3344 case 'k':
3345 case KEY_UP:
3346 if (s->selected_line > 1)
3347 s->selected_line--;
3348 else if (s->selected_line == 1 &&
3349 s->first_displayed_line > 1)
3350 s->first_displayed_line--;
3351 break;
3352 case KEY_PPAGE:
3353 if (s->first_displayed_line == 1) {
3354 s->selected_line = 1;
3355 break;
3357 if (s->first_displayed_line > view->nlines - 2)
3358 s->first_displayed_line -=
3359 (view->nlines - 2);
3360 else
3361 s->first_displayed_line = 1;
3362 break;
3363 case 'j':
3364 case KEY_DOWN:
3365 if (s->selected_line < view->nlines - 2 &&
3366 s->first_displayed_line +
3367 s->selected_line <= s->blame.nlines)
3368 s->selected_line++;
3369 else if (s->last_displayed_line <
3370 s->blame.nlines)
3371 s->first_displayed_line++;
3372 break;
3373 case 'b':
3374 case 'p': {
3375 struct got_object_id *id = NULL;
3376 id = get_selected_commit_id(s->blame.lines,
3377 s->first_displayed_line, s->selected_line);
3378 if (id == NULL)
3379 break;
3380 if (ch == 'p') {
3381 struct got_commit_object *commit;
3382 struct got_object_qid *pid;
3383 struct got_object_id *blob_id = NULL;
3384 int obj_type;
3385 err = got_object_open_as_commit(&commit,
3386 s->repo, id);
3387 if (err)
3388 break;
3389 pid = SIMPLEQ_FIRST(
3390 got_object_commit_get_parent_ids(commit));
3391 if (pid == NULL) {
3392 got_object_commit_close(commit);
3393 break;
3395 /* Check if path history ends here. */
3396 err = got_object_id_by_path(&blob_id, s->repo,
3397 pid->id, s->path);
3398 if (err) {
3399 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3400 err = NULL;
3401 got_object_commit_close(commit);
3402 break;
3404 err = got_object_get_type(&obj_type, s->repo,
3405 blob_id);
3406 free(blob_id);
3407 /* Can't blame non-blob type objects. */
3408 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3409 got_object_commit_close(commit);
3410 break;
3412 err = got_object_qid_alloc(&s->blamed_commit,
3413 pid->id);
3414 got_object_commit_close(commit);
3415 } else {
3416 if (got_object_id_cmp(id,
3417 s->blamed_commit->id) == 0)
3418 break;
3419 err = got_object_qid_alloc(&s->blamed_commit,
3420 id);
3422 if (err)
3423 break;
3424 s->done = 1;
3425 thread_err = stop_blame(&s->blame);
3426 s->done = 0;
3427 if (thread_err)
3428 break;
3429 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3430 s->blamed_commit, entry);
3431 err = run_blame(&s->blame, view, &s->blame_complete,
3432 &s->first_displayed_line, &s->last_displayed_line,
3433 &s->selected_line, &s->done, &s->eof,
3434 s->path, s->blamed_commit->id, s->repo);
3435 if (err)
3436 break;
3437 break;
3439 case 'B': {
3440 struct got_object_qid *first;
3441 first = SIMPLEQ_FIRST(&s->blamed_commits);
3442 if (!got_object_id_cmp(first->id, s->commit_id))
3443 break;
3444 s->done = 1;
3445 thread_err = stop_blame(&s->blame);
3446 s->done = 0;
3447 if (thread_err)
3448 break;
3449 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3450 got_object_qid_free(s->blamed_commit);
3451 s->blamed_commit =
3452 SIMPLEQ_FIRST(&s->blamed_commits);
3453 err = run_blame(&s->blame, view, &s->blame_complete,
3454 &s->first_displayed_line, &s->last_displayed_line,
3455 &s->selected_line, &s->done, &s->eof, s->path,
3456 s->blamed_commit->id, s->repo);
3457 if (err)
3458 break;
3459 break;
3461 case KEY_ENTER:
3462 case '\r': {
3463 struct got_object_id *id = NULL;
3464 struct got_object_qid *pid;
3465 struct got_commit_object *commit = NULL;
3466 id = get_selected_commit_id(s->blame.lines,
3467 s->first_displayed_line, s->selected_line);
3468 if (id == NULL)
3469 break;
3470 err = got_object_open_as_commit(&commit, s->repo, id);
3471 if (err)
3472 break;
3473 pid = SIMPLEQ_FIRST(
3474 got_object_commit_get_parent_ids(commit));
3475 if (view_is_parent_view(view))
3476 begin_x = view_split_begin_x(view->begin_x);
3477 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3478 if (diff_view == NULL) {
3479 got_object_commit_close(commit);
3480 err = got_error_from_errno("view_open");
3481 break;
3483 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3484 id, NULL, s->refs, s->repo);
3485 got_object_commit_close(commit);
3486 if (err) {
3487 view_close(diff_view);
3488 break;
3490 if (view_is_parent_view(view)) {
3491 err = view_close_child(view);
3492 if (err)
3493 break;
3494 err = view_set_child(view, diff_view);
3495 if (err) {
3496 view_close(diff_view);
3497 break;
3499 *focus_view = diff_view;
3500 view->child_focussed = 1;
3501 } else
3502 *new_view = diff_view;
3503 if (err)
3504 break;
3505 break;
3507 case KEY_NPAGE:
3508 case ' ':
3509 if (s->last_displayed_line >= s->blame.nlines &&
3510 s->selected_line >= MIN(s->blame.nlines,
3511 view->nlines - 2)) {
3512 break;
3514 if (s->last_displayed_line >= s->blame.nlines &&
3515 s->selected_line < view->nlines - 2) {
3516 s->selected_line = MIN(s->blame.nlines,
3517 view->nlines - 2);
3518 break;
3520 if (s->last_displayed_line + view->nlines - 2
3521 <= s->blame.nlines)
3522 s->first_displayed_line +=
3523 view->nlines - 2;
3524 else
3525 s->first_displayed_line =
3526 s->blame.nlines -
3527 (view->nlines - 3);
3528 break;
3529 case KEY_RESIZE:
3530 if (s->selected_line > view->nlines - 2) {
3531 s->selected_line = MIN(s->blame.nlines,
3532 view->nlines - 2);
3534 break;
3535 default:
3536 break;
3538 return thread_err ? thread_err : err;
3541 static const struct got_error *
3542 cmd_blame(int argc, char *argv[])
3544 const struct got_error *error;
3545 struct got_repository *repo = NULL;
3546 struct got_reflist_head refs;
3547 struct got_worktree *worktree = NULL;
3548 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3549 struct got_object_id *commit_id = NULL;
3550 char *commit_id_str = NULL;
3551 int ch;
3552 struct tog_view *view;
3554 SIMPLEQ_INIT(&refs);
3556 #ifndef PROFILE
3557 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3558 NULL) == -1)
3559 err(1, "pledge");
3560 #endif
3562 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3563 switch (ch) {
3564 case 'c':
3565 commit_id_str = optarg;
3566 break;
3567 case 'r':
3568 repo_path = realpath(optarg, NULL);
3569 if (repo_path == NULL)
3570 err(1, "-r option");
3571 break;
3572 default:
3573 usage_blame();
3574 /* NOTREACHED */
3578 argc -= optind;
3579 argv += optind;
3581 if (argc == 1)
3582 path = argv[0];
3583 else
3584 usage_blame();
3586 cwd = getcwd(NULL, 0);
3587 if (cwd == NULL) {
3588 error = got_error_from_errno("getcwd");
3589 goto done;
3591 if (repo_path == NULL) {
3592 error = got_worktree_open(&worktree, cwd);
3593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3594 goto done;
3595 else
3596 error = NULL;
3597 if (worktree) {
3598 repo_path =
3599 strdup(got_worktree_get_repo_path(worktree));
3600 if (repo_path == NULL)
3601 error = got_error_from_errno("strdup");
3602 if (error)
3603 goto done;
3604 } else {
3605 repo_path = strdup(cwd);
3606 if (repo_path == NULL) {
3607 error = got_error_from_errno("strdup");
3608 goto done;
3613 init_curses();
3615 error = got_repo_open(&repo, repo_path);
3616 if (error != NULL)
3617 goto done;
3619 error = apply_unveil(got_repo_get_path(repo), NULL);
3620 if (error)
3621 goto done;
3623 if (worktree) {
3624 const char *prefix = got_worktree_get_path_prefix(worktree);
3625 char *p, *worktree_subdir = cwd +
3626 strlen(got_worktree_get_root_path(worktree));
3627 if (asprintf(&p, "%s%s%s%s%s",
3628 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3629 worktree_subdir, worktree_subdir[0] ? "/" : "",
3630 path) == -1) {
3631 error = got_error_from_errno("asprintf");
3632 goto done;
3634 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3635 free(p);
3636 } else {
3637 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3639 if (error)
3640 goto done;
3642 if (commit_id_str == NULL) {
3643 struct got_reference *head_ref;
3644 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3645 if (error != NULL)
3646 goto done;
3647 error = got_ref_resolve(&commit_id, repo, head_ref);
3648 got_ref_close(head_ref);
3649 } else {
3650 error = got_object_resolve_id_str(&commit_id, repo,
3651 commit_id_str);
3653 if (error != NULL)
3654 goto done;
3656 error = got_ref_list(&refs, repo);
3657 if (error)
3658 goto done;
3660 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3661 if (view == NULL) {
3662 error = got_error_from_errno("view_open");
3663 goto done;
3665 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3666 if (error)
3667 goto done;
3668 error = view_loop(view);
3669 done:
3670 free(repo_path);
3671 free(cwd);
3672 free(commit_id);
3673 if (worktree)
3674 got_worktree_close(worktree);
3675 if (repo)
3676 got_repo_close(repo);
3677 got_ref_list_free(&refs);
3678 return error;
3681 static const struct got_error *
3682 draw_tree_entries(struct tog_view *view,
3683 struct got_tree_entry **first_displayed_entry,
3684 struct got_tree_entry **last_displayed_entry,
3685 struct got_tree_entry **selected_entry, int *ndisplayed,
3686 const char *label, int show_ids, const char *parent_path,
3687 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3689 const struct got_error *err = NULL;
3690 struct got_tree_entry *te;
3691 wchar_t *wline;
3692 int width, n;
3694 *ndisplayed = 0;
3696 werase(view->window);
3698 if (limit == 0)
3699 return NULL;
3701 err = format_line(&wline, &width, label, view->ncols);
3702 if (err)
3703 return err;
3704 if (view_needs_focus_indication(view))
3705 wstandout(view->window);
3706 waddwstr(view->window, wline);
3707 if (view_needs_focus_indication(view))
3708 wstandend(view->window);
3709 free(wline);
3710 wline = NULL;
3711 if (width < view->ncols - 1)
3712 waddch(view->window, '\n');
3713 if (--limit <= 0)
3714 return NULL;
3715 err = format_line(&wline, &width, parent_path, view->ncols);
3716 if (err)
3717 return err;
3718 waddwstr(view->window, wline);
3719 free(wline);
3720 wline = NULL;
3721 if (width < view->ncols - 1)
3722 waddch(view->window, '\n');
3723 if (--limit <= 0)
3724 return NULL;
3725 waddch(view->window, '\n');
3726 if (--limit <= 0)
3727 return NULL;
3729 te = SIMPLEQ_FIRST(&entries->head);
3730 if (*first_displayed_entry == NULL) {
3731 if (selected == 0) {
3732 if (view->focussed)
3733 wstandout(view->window);
3734 *selected_entry = NULL;
3736 waddstr(view->window, " ..\n"); /* parent directory */
3737 if (selected == 0 && view->focussed)
3738 wstandend(view->window);
3739 (*ndisplayed)++;
3740 if (--limit <= 0)
3741 return NULL;
3742 n = 1;
3743 } else {
3744 n = 0;
3745 while (te != *first_displayed_entry)
3746 te = SIMPLEQ_NEXT(te, entry);
3749 while (te) {
3750 char *line = NULL, *id_str = NULL;
3752 if (show_ids) {
3753 err = got_object_id_str(&id_str, te->id);
3754 if (err)
3755 return got_error_from_errno(
3756 "got_object_id_str");
3758 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3759 te->name, S_ISDIR(te->mode) ? "/" :
3760 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3761 free(id_str);
3762 return got_error_from_errno("asprintf");
3764 free(id_str);
3765 err = format_line(&wline, &width, line, view->ncols);
3766 if (err) {
3767 free(line);
3768 break;
3770 if (n == selected) {
3771 if (view->focussed)
3772 wstandout(view->window);
3773 *selected_entry = te;
3775 waddwstr(view->window, wline);
3776 if (width < view->ncols - 1)
3777 waddch(view->window, '\n');
3778 if (n == selected && view->focussed)
3779 wstandend(view->window);
3780 free(line);
3781 free(wline);
3782 wline = NULL;
3783 n++;
3784 (*ndisplayed)++;
3785 *last_displayed_entry = te;
3786 if (--limit <= 0)
3787 break;
3788 te = SIMPLEQ_NEXT(te, entry);
3791 return err;
3794 static void
3795 tree_scroll_up(struct tog_view *view,
3796 struct got_tree_entry **first_displayed_entry, int maxscroll,
3797 const struct got_tree_entries *entries, int isroot)
3799 struct got_tree_entry *te, *prev;
3800 int i;
3802 if (*first_displayed_entry == NULL)
3803 return;
3805 te = SIMPLEQ_FIRST(&entries->head);
3806 if (*first_displayed_entry == te) {
3807 if (!isroot)
3808 *first_displayed_entry = NULL;
3809 return;
3812 /* XXX this is stupid... switch to TAILQ? */
3813 for (i = 0; i < maxscroll; i++) {
3814 while (te != *first_displayed_entry) {
3815 prev = te;
3816 te = SIMPLEQ_NEXT(te, entry);
3818 *first_displayed_entry = prev;
3819 te = SIMPLEQ_FIRST(&entries->head);
3821 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3822 *first_displayed_entry = NULL;
3825 static int
3826 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3827 struct got_tree_entry *last_displayed_entry,
3828 const struct got_tree_entries *entries)
3830 struct got_tree_entry *next, *last;
3831 int n = 0;
3833 if (*first_displayed_entry)
3834 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3835 else
3836 next = SIMPLEQ_FIRST(&entries->head);
3837 last = last_displayed_entry;
3838 while (next && last && n++ < maxscroll) {
3839 last = SIMPLEQ_NEXT(last, entry);
3840 if (last) {
3841 *first_displayed_entry = next;
3842 next = SIMPLEQ_NEXT(next, entry);
3845 return n;
3848 static const struct got_error *
3849 tree_entry_path(char **path, struct tog_parent_trees *parents,
3850 struct got_tree_entry *te)
3852 const struct got_error *err = NULL;
3853 struct tog_parent_tree *pt;
3854 size_t len = 2; /* for leading slash and NUL */
3856 TAILQ_FOREACH(pt, parents, entry)
3857 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3858 if (te)
3859 len += strlen(te->name);
3861 *path = calloc(1, len);
3862 if (path == NULL)
3863 return got_error_from_errno("calloc");
3865 (*path)[0] = '/';
3866 pt = TAILQ_LAST(parents, tog_parent_trees);
3867 while (pt) {
3868 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3869 err = got_error(GOT_ERR_NO_SPACE);
3870 goto done;
3872 if (strlcat(*path, "/", len) >= len) {
3873 err = got_error(GOT_ERR_NO_SPACE);
3874 goto done;
3876 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3878 if (te) {
3879 if (strlcat(*path, te->name, len) >= len) {
3880 err = got_error(GOT_ERR_NO_SPACE);
3881 goto done;
3884 done:
3885 if (err) {
3886 free(*path);
3887 *path = NULL;
3889 return err;
3892 static const struct got_error *
3893 blame_tree_entry(struct tog_view **new_view, int begin_x,
3894 struct got_tree_entry *te, struct tog_parent_trees *parents,
3895 struct got_object_id *commit_id, struct got_reflist_head *refs,
3896 struct got_repository *repo)
3898 const struct got_error *err = NULL;
3899 char *path;
3900 struct tog_view *blame_view;
3902 err = tree_entry_path(&path, parents, te);
3903 if (err)
3904 return err;
3906 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
3907 if (blame_view == NULL)
3908 return got_error_from_errno("view_open");
3910 err = open_blame_view(blame_view, path, commit_id, refs, repo);
3911 if (err) {
3912 view_close(blame_view);
3913 free(path);
3914 } else
3915 *new_view = blame_view;
3916 return err;
3919 static const struct got_error *
3920 log_tree_entry(struct tog_view **new_view, int begin_x,
3921 struct got_tree_entry *te, struct tog_parent_trees *parents,
3922 struct got_object_id *commit_id, struct got_reflist_head *refs,
3923 struct got_repository *repo)
3925 struct tog_view *log_view;
3926 const struct got_error *err = NULL;
3927 char *path;
3929 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
3930 if (log_view == NULL)
3931 return got_error_from_errno("view_open");
3933 err = tree_entry_path(&path, parents, te);
3934 if (err)
3935 return err;
3937 err = open_log_view(log_view, commit_id, refs, repo, path, 0);
3938 if (err)
3939 view_close(log_view);
3940 else
3941 *new_view = log_view;
3942 free(path);
3943 return err;
3946 static const struct got_error *
3947 open_tree_view(struct tog_view *view, struct got_tree_object *root,
3948 struct got_object_id *commit_id, struct got_reflist_head *refs,
3949 struct got_repository *repo)
3951 const struct got_error *err = NULL;
3952 char *commit_id_str = NULL;
3953 struct tog_tree_view_state *s = &view->state.tree;
3955 TAILQ_INIT(&s->parents);
3957 err = got_object_id_str(&commit_id_str, commit_id);
3958 if (err != NULL)
3959 goto done;
3961 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
3962 err = got_error_from_errno("asprintf");
3963 goto done;
3966 s->root = s->tree = root;
3967 s->entries = got_object_tree_get_entries(root);
3968 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
3969 s->commit_id = got_object_id_dup(commit_id);
3970 if (s->commit_id == NULL) {
3971 err = got_error_from_errno("got_object_id_dup");
3972 goto done;
3974 s->refs = refs;
3975 s->repo = repo;
3977 view->show = show_tree_view;
3978 view->input = input_tree_view;
3979 view->close = close_tree_view;
3980 view->search_start = search_start_tree_view;
3981 view->search_next = search_next_tree_view;
3982 done:
3983 free(commit_id_str);
3984 if (err) {
3985 free(s->tree_label);
3986 s->tree_label = NULL;
3988 return err;
3991 static const struct got_error *
3992 close_tree_view(struct tog_view *view)
3994 struct tog_tree_view_state *s = &view->state.tree;
3996 free(s->tree_label);
3997 s->tree_label = NULL;
3998 free(s->commit_id);
3999 s->commit_id = NULL;
4000 while (!TAILQ_EMPTY(&s->parents)) {
4001 struct tog_parent_tree *parent;
4002 parent = TAILQ_FIRST(&s->parents);
4003 TAILQ_REMOVE(&s->parents, parent, entry);
4004 free(parent);
4007 if (s->tree != s->root)
4008 got_object_tree_close(s->tree);
4009 got_object_tree_close(s->root);
4011 return NULL;
4014 static const struct got_error *
4015 search_start_tree_view(struct tog_view *view)
4017 struct tog_tree_view_state *s = &view->state.tree;
4019 s->matched_entry = NULL;
4020 return NULL;
4023 static int
4024 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4026 regmatch_t regmatch;
4028 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4031 static const struct got_error *
4032 search_next_tree_view(struct tog_view *view)
4034 struct tog_tree_view_state *s = &view->state.tree;
4035 struct got_tree_entry *entry, *te;
4037 if (!view->searching) {
4038 view->search_next_done = 1;
4039 return NULL;
4042 if (s->matched_entry) {
4043 if (view->searching == TOG_SEARCH_FORWARD) {
4044 if (s->selected_entry)
4045 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4046 else
4047 entry = SIMPLEQ_FIRST(&s->entries->head);
4049 else {
4050 if (s->selected_entry == NULL) {
4051 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4052 entry = te;
4053 } else {
4054 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4055 entry = te;
4056 if (SIMPLEQ_NEXT(te, entry) ==
4057 s->selected_entry)
4058 break;
4062 } else {
4063 if (view->searching == TOG_SEARCH_FORWARD)
4064 entry = SIMPLEQ_FIRST(&s->entries->head);
4065 else {
4066 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4067 entry = te;
4071 while (1) {
4072 if (entry == NULL) {
4073 if (s->matched_entry == NULL) {
4074 view->search_next_done = 1;
4075 return NULL;
4077 if (view->searching == TOG_SEARCH_FORWARD)
4078 entry = SIMPLEQ_FIRST(&s->entries->head);
4079 else {
4080 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4081 entry = te;
4085 if (match_tree_entry(entry, &view->regex)) {
4086 view->search_next_done = 1;
4087 s->matched_entry = entry;
4088 break;
4091 if (view->searching == TOG_SEARCH_FORWARD)
4092 entry = SIMPLEQ_NEXT(entry, entry);
4093 else {
4094 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4095 entry = NULL;
4096 else {
4097 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4098 if (SIMPLEQ_NEXT(te, entry) == entry) {
4099 entry = te;
4100 break;
4107 if (s->matched_entry) {
4108 s->first_displayed_entry = s->matched_entry;
4109 s->selected = 0;
4112 return NULL;
4115 static const struct got_error *
4116 show_tree_view(struct tog_view *view)
4118 const struct got_error *err = NULL;
4119 struct tog_tree_view_state *s = &view->state.tree;
4120 char *parent_path;
4122 err = tree_entry_path(&parent_path, &s->parents, NULL);
4123 if (err)
4124 return err;
4126 err = draw_tree_entries(view, &s->first_displayed_entry,
4127 &s->last_displayed_entry, &s->selected_entry,
4128 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4129 s->entries, s->selected, view->nlines, s->tree == s->root);
4130 free(parent_path);
4132 view_vborder(view);
4133 return err;
4136 static const struct got_error *
4137 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4138 struct tog_view **focus_view, struct tog_view *view, int ch)
4140 const struct got_error *err = NULL;
4141 struct tog_tree_view_state *s = &view->state.tree;
4142 struct tog_view *log_view;
4143 int begin_x = 0, nscrolled;
4145 switch (ch) {
4146 case 'i':
4147 s->show_ids = !s->show_ids;
4148 break;
4149 case 'l':
4150 if (!s->selected_entry)
4151 break;
4152 if (view_is_parent_view(view))
4153 begin_x = view_split_begin_x(view->begin_x);
4154 err = log_tree_entry(&log_view, begin_x,
4155 s->selected_entry, &s->parents,
4156 s->commit_id, s->refs, s->repo);
4157 if (view_is_parent_view(view)) {
4158 err = view_close_child(view);
4159 if (err)
4160 return err;
4161 err = view_set_child(view, log_view);
4162 if (err) {
4163 view_close(log_view);
4164 break;
4166 *focus_view = log_view;
4167 view->child_focussed = 1;
4168 } else
4169 *new_view = log_view;
4170 break;
4171 case 'k':
4172 case KEY_UP:
4173 if (s->selected > 0) {
4174 s->selected--;
4175 if (s->selected == 0)
4176 break;
4178 if (s->selected > 0)
4179 break;
4180 tree_scroll_up(view, &s->first_displayed_entry, 1,
4181 s->entries, s->tree == s->root);
4182 break;
4183 case KEY_PPAGE:
4184 tree_scroll_up(view, &s->first_displayed_entry,
4185 MAX(0, view->nlines - 4 - s->selected), s->entries,
4186 s->tree == s->root);
4187 s->selected = 0;
4188 if (SIMPLEQ_FIRST(&s->entries->head) ==
4189 s->first_displayed_entry && s->tree != s->root)
4190 s->first_displayed_entry = NULL;
4191 break;
4192 case 'j':
4193 case KEY_DOWN:
4194 if (s->selected < s->ndisplayed - 1) {
4195 s->selected++;
4196 break;
4198 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4199 /* can't scroll any further */
4200 break;
4201 tree_scroll_down(&s->first_displayed_entry, 1,
4202 s->last_displayed_entry, s->entries);
4203 break;
4204 case KEY_NPAGE:
4205 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4206 == NULL) {
4207 /* can't scroll any further; move cursor down */
4208 if (s->selected < s->ndisplayed - 1)
4209 s->selected = s->ndisplayed - 1;
4210 break;
4212 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4213 view->nlines, s->last_displayed_entry, s->entries);
4214 if (nscrolled < view->nlines) {
4215 int ndisplayed = 0;
4216 struct got_tree_entry *te;
4217 te = s->first_displayed_entry;
4218 do {
4219 ndisplayed++;
4220 te = SIMPLEQ_NEXT(te, entry);
4221 } while (te);
4222 s->selected = ndisplayed - 1;
4224 break;
4225 case KEY_ENTER:
4226 case '\r':
4227 case KEY_BACKSPACE:
4228 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4229 struct tog_parent_tree *parent;
4230 /* user selected '..' */
4231 if (s->tree == s->root)
4232 break;
4233 parent = TAILQ_FIRST(&s->parents);
4234 TAILQ_REMOVE(&s->parents, parent,
4235 entry);
4236 got_object_tree_close(s->tree);
4237 s->tree = parent->tree;
4238 s->entries =
4239 got_object_tree_get_entries(s->tree);
4240 s->first_displayed_entry =
4241 parent->first_displayed_entry;
4242 s->selected_entry =
4243 parent->selected_entry;
4244 s->selected = parent->selected;
4245 free(parent);
4246 } else if (S_ISDIR(s->selected_entry->mode)) {
4247 struct got_tree_object *subtree;
4248 err = got_object_open_as_tree(&subtree,
4249 s->repo, s->selected_entry->id);
4250 if (err)
4251 break;
4252 err = tree_view_visit_subtree(subtree, s);
4253 if (err) {
4254 got_object_tree_close(subtree);
4255 break;
4257 } else if (S_ISREG(s->selected_entry->mode)) {
4258 struct tog_view *blame_view;
4259 int begin_x = view_is_parent_view(view) ?
4260 view_split_begin_x(view->begin_x) : 0;
4262 err = blame_tree_entry(&blame_view, begin_x,
4263 s->selected_entry, &s->parents,
4264 s->commit_id, s->refs, s->repo);
4265 if (err)
4266 break;
4267 if (view_is_parent_view(view)) {
4268 err = view_close_child(view);
4269 if (err)
4270 return err;
4271 err = view_set_child(view, blame_view);
4272 if (err) {
4273 view_close(blame_view);
4274 break;
4276 *focus_view = blame_view;
4277 view->child_focussed = 1;
4278 } else
4279 *new_view = blame_view;
4281 break;
4282 case KEY_RESIZE:
4283 if (s->selected > view->nlines)
4284 s->selected = s->ndisplayed - 1;
4285 break;
4286 default:
4287 break;
4290 return err;
4293 __dead static void
4294 usage_tree(void)
4296 endwin();
4297 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4298 getprogname());
4299 exit(1);
4302 static const struct got_error *
4303 cmd_tree(int argc, char *argv[])
4305 const struct got_error *error;
4306 struct got_repository *repo = NULL;
4307 struct got_reflist_head refs;
4308 char *repo_path = NULL;
4309 struct got_object_id *commit_id = NULL;
4310 char *commit_id_arg = NULL;
4311 struct got_commit_object *commit = NULL;
4312 struct got_tree_object *tree = NULL;
4313 int ch;
4314 struct tog_view *view;
4316 SIMPLEQ_INIT(&refs);
4318 #ifndef PROFILE
4319 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4320 NULL) == -1)
4321 err(1, "pledge");
4322 #endif
4324 while ((ch = getopt(argc, argv, "c:")) != -1) {
4325 switch (ch) {
4326 case 'c':
4327 commit_id_arg = optarg;
4328 break;
4329 default:
4330 usage_tree();
4331 /* NOTREACHED */
4335 argc -= optind;
4336 argv += optind;
4338 if (argc == 0) {
4339 struct got_worktree *worktree;
4340 char *cwd = getcwd(NULL, 0);
4341 if (cwd == NULL)
4342 return got_error_from_errno("getcwd");
4343 error = got_worktree_open(&worktree, cwd);
4344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4345 goto done;
4346 if (worktree) {
4347 free(cwd);
4348 repo_path =
4349 strdup(got_worktree_get_repo_path(worktree));
4350 got_worktree_close(worktree);
4351 } else
4352 repo_path = cwd;
4353 if (repo_path == NULL) {
4354 error = got_error_from_errno("strdup");
4355 goto done;
4357 } else if (argc == 1) {
4358 repo_path = realpath(argv[0], NULL);
4359 if (repo_path == NULL)
4360 return got_error_from_errno2("realpath", argv[0]);
4361 } else
4362 usage_log();
4364 init_curses();
4366 error = got_repo_open(&repo, repo_path);
4367 if (error != NULL)
4368 goto done;
4370 error = apply_unveil(got_repo_get_path(repo), NULL);
4371 if (error)
4372 goto done;
4374 if (commit_id_arg == NULL)
4375 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4376 else
4377 error = got_object_resolve_id_str(&commit_id, repo,
4378 commit_id_arg);
4379 if (error != NULL)
4380 goto done;
4382 error = got_object_open_as_commit(&commit, repo, commit_id);
4383 if (error != NULL)
4384 goto done;
4386 error = got_object_open_as_tree(&tree, repo,
4387 got_object_commit_get_tree_id(commit));
4388 if (error != NULL)
4389 goto done;
4391 error = got_ref_list(&refs, repo);
4392 if (error)
4393 goto done;
4395 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4396 if (view == NULL) {
4397 error = got_error_from_errno("view_open");
4398 goto done;
4400 error = open_tree_view(view, tree, commit_id, &refs, repo);
4401 if (error)
4402 goto done;
4403 error = view_loop(view);
4404 done:
4405 free(repo_path);
4406 free(commit_id);
4407 if (commit)
4408 got_object_commit_close(commit);
4409 if (tree)
4410 got_object_tree_close(tree);
4411 if (repo)
4412 got_repo_close(repo);
4413 got_ref_list_free(&refs);
4414 return error;
4417 __dead static void
4418 usage(void)
4420 fprintf(stderr, "usage: %s [-h] [command] [arg ...]\n", getprogname());
4421 exit(1);
4424 static char **
4425 make_argv(const char *arg0, const char *arg1)
4427 char **argv;
4428 int argc = (arg1 == NULL ? 1 : 2);
4430 argv = calloc(argc, sizeof(char *));
4431 if (argv == NULL)
4432 err(1, "calloc");
4433 argv[0] = strdup(arg0);
4434 if (argv[0] == NULL)
4435 err(1, "calloc");
4436 if (arg1) {
4437 argv[1] = strdup(arg1);
4438 if (argv[1] == NULL)
4439 err(1, "calloc");
4442 return argv;
4445 int
4446 main(int argc, char *argv[])
4448 const struct got_error *error = NULL;
4449 struct tog_cmd *cmd = NULL;
4450 int ch, hflag = 0;
4451 char **cmd_argv = NULL;
4453 setlocale(LC_CTYPE, "");
4455 while ((ch = getopt(argc, argv, "h")) != -1) {
4456 switch (ch) {
4457 case 'h':
4458 hflag = 1;
4459 break;
4460 default:
4461 usage();
4462 /* NOTREACHED */
4466 argc -= optind;
4467 argv += optind;
4468 optind = 0;
4469 optreset = 1;
4471 if (argc == 0) {
4472 if (hflag)
4473 usage();
4474 /* Build an argument vector which runs a default command. */
4475 cmd = &tog_commands[0];
4476 cmd_argv = make_argv(cmd->name, NULL);
4477 argc = 1;
4478 } else {
4479 int i;
4481 /* Did the user specific a command? */
4482 for (i = 0; i < nitems(tog_commands); i++) {
4483 if (strncmp(tog_commands[i].name, argv[0],
4484 strlen(argv[0])) == 0) {
4485 cmd = &tog_commands[i];
4486 if (hflag)
4487 tog_commands[i].cmd_usage();
4488 break;
4491 if (cmd == NULL) {
4492 /* Did the user specify a repository? */
4493 char *repo_path = realpath(argv[0], NULL);
4494 if (repo_path) {
4495 struct got_repository *repo;
4496 error = got_repo_open(&repo, repo_path);
4497 if (error == NULL)
4498 got_repo_close(repo);
4499 } else
4500 error = got_error_from_errno2("realpath",
4501 argv[0]);
4502 if (error) {
4503 if (hflag) {
4504 fprintf(stderr, "%s: '%s' is not a "
4505 "known command\n", getprogname(),
4506 argv[0]);
4507 usage();
4509 fprintf(stderr, "%s: '%s' is neither a known "
4510 "command nor a path to a repository\n",
4511 getprogname(), argv[0]);
4512 free(repo_path);
4513 return 1;
4515 cmd = &tog_commands[0];
4516 cmd_argv = make_argv(cmd->name, repo_path);
4517 argc = 2;
4518 free(repo_path);
4522 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4523 if (error)
4524 goto done;
4525 done:
4526 endwin();
4527 free(cmd_argv);
4528 if (error)
4529 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4530 return 0;