Blob


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