Blob


1 /*
2 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_commit_graph.h"
49 #include "got_utf8.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_path.h"
53 #include "got_worktree.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 #define CTRL(x) ((x) & 0x1f)
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct tog_cmd {
70 const char *name;
71 const struct got_error *(*cmd_main)(int, char *[]);
72 void (*cmd_usage)(void);
73 };
75 __dead static void usage(int);
76 __dead static void usage_log(void);
77 __dead static void usage_diff(void);
78 __dead static void usage_blame(void);
79 __dead static void usage_tree(void);
81 static const struct got_error* cmd_log(int, char *[]);
82 static const struct got_error* cmd_diff(int, char *[]);
83 static const struct got_error* cmd_blame(int, char *[]);
84 static const struct got_error* cmd_tree(int, char *[]);
86 static struct tog_cmd tog_commands[] = {
87 { "log", cmd_log, usage_log },
88 { "diff", cmd_diff, usage_diff },
89 { "blame", cmd_blame, usage_blame },
90 { "tree", cmd_tree, usage_tree },
91 };
93 enum tog_view_type {
94 TOG_VIEW_DIFF,
95 TOG_VIEW_LOG,
96 TOG_VIEW_BLAME,
97 TOG_VIEW_TREE
98 };
100 #define TOG_EOF_STRING "(END)"
102 struct commit_queue_entry {
103 TAILQ_ENTRY(commit_queue_entry) entry;
104 struct got_object_id *id;
105 struct got_commit_object *commit;
106 int idx;
107 };
108 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
109 struct commit_queue {
110 int ncommits;
111 struct commit_queue_head head;
112 };
114 struct tog_diff_view_state {
115 struct got_object_id *id1, *id2;
116 FILE *f;
117 int first_displayed_line;
118 int last_displayed_line;
119 int eof;
120 int diff_context;
121 struct got_repository *repo;
122 struct got_reflist_head *refs;
124 /* passed from log view; may be NULL */
125 struct tog_view *log_view;
126 };
128 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
130 struct tog_log_thread_args {
131 pthread_cond_t need_commits;
132 int commits_needed;
133 struct got_commit_graph *graph;
134 struct commit_queue *commits;
135 const char *in_repo_path;
136 struct got_object_id *start_id;
137 struct got_repository *repo;
138 int log_complete;
139 sig_atomic_t *quit;
140 struct tog_view *view;
141 struct commit_queue_entry **first_displayed_entry;
142 struct commit_queue_entry **selected_entry;
143 };
145 struct tog_log_view_state {
146 struct commit_queue commits;
147 struct commit_queue_entry *first_displayed_entry;
148 struct commit_queue_entry *last_displayed_entry;
149 struct commit_queue_entry *selected_entry;
150 int selected;
151 char *in_repo_path;
152 const char *head_ref_name;
153 struct got_repository *repo;
154 struct got_reflist_head *refs;
155 struct got_object_id *start_id;
156 sig_atomic_t quit;
157 pthread_t thread;
158 struct tog_log_thread_args thread_args;
159 struct commit_queue_entry *matched_entry;
160 struct commit_queue_entry *search_entry;
161 };
163 struct tog_blame_cb_args {
164 struct tog_blame_line *lines; /* one per line */
165 int nlines;
167 struct tog_view *view;
168 struct got_object_id *commit_id;
169 int *quit;
170 };
172 struct tog_blame_thread_args {
173 const char *path;
174 struct got_repository *repo;
175 struct tog_blame_cb_args *cb_args;
176 int *complete;
177 };
179 struct tog_blame {
180 FILE *f;
181 size_t filesize;
182 struct tog_blame_line *lines;
183 int nlines;
184 off_t *line_offsets;
185 pthread_t thread;
186 struct tog_blame_thread_args thread_args;
187 struct tog_blame_cb_args cb_args;
188 const char *path;
189 };
191 struct tog_blame_view_state {
192 int first_displayed_line;
193 int last_displayed_line;
194 int selected_line;
195 int blame_complete;
196 int eof;
197 int done;
198 struct got_object_id_queue blamed_commits;
199 struct got_object_qid *blamed_commit;
200 char *path;
201 struct got_repository *repo;
202 struct got_reflist_head *refs;
203 struct got_object_id *commit_id;
204 struct tog_blame blame;
205 int matched_line;
206 };
208 struct tog_parent_tree {
209 TAILQ_ENTRY(tog_parent_tree) entry;
210 struct got_tree_object *tree;
211 struct got_tree_entry *first_displayed_entry;
212 struct got_tree_entry *selected_entry;
213 int selected;
214 };
216 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
218 struct tog_tree_view_state {
219 char *tree_label;
220 struct got_tree_object *root;
221 struct got_tree_object *tree;
222 const struct got_tree_entries *entries;
223 struct got_tree_entry *first_displayed_entry;
224 struct got_tree_entry *last_displayed_entry;
225 struct got_tree_entry *selected_entry;
226 int ndisplayed, selected, show_ids;
227 struct tog_parent_trees parents;
228 struct got_object_id *commit_id;
229 struct got_repository *repo;
230 struct got_reflist_head *refs;
231 struct got_tree_entry *matched_entry;
232 };
234 /*
235 * We implement two types of views: parent views and child views.
237 * The 'Tab' key switches between a parent view and its child view.
238 * Child views are shown side-by-side to their parent view, provided
239 * there is enough screen estate.
241 * When a new view is opened from within a parent view, this new view
242 * becomes a child view of the parent view, replacing any existing child.
244 * When a new view is opened from within a child view, this new view
245 * becomes a parent view which will obscure the views below until the
246 * user quits the new parent view by typing 'q'.
248 * This list of views contains parent views only.
249 * Child views are only pointed to by their parent view.
250 */
251 TAILQ_HEAD(tog_view_list_head, tog_view);
253 struct tog_view {
254 TAILQ_ENTRY(tog_view) entry;
255 WINDOW *window;
256 PANEL *panel;
257 int nlines, ncols, begin_y, begin_x;
258 int lines, cols; /* copies of LINES and COLS */
259 int focussed;
260 struct tog_view *parent;
261 struct tog_view *child;
262 int child_focussed;
264 /* type-specific state */
265 enum tog_view_type type;
266 union {
267 struct tog_diff_view_state diff;
268 struct tog_log_view_state log;
269 struct tog_blame_view_state blame;
270 struct tog_tree_view_state tree;
271 } state;
273 const struct got_error *(*show)(struct tog_view *);
274 const struct got_error *(*input)(struct tog_view **,
275 struct tog_view **, struct tog_view**, struct tog_view *, int);
276 const struct got_error *(*close)(struct tog_view *);
278 const struct got_error *(*search_start)(struct tog_view *);
279 const struct got_error *(*search_next)(struct tog_view *);
280 int searching;
281 #define TOG_SEARCH_FORWARD 1
282 #define TOG_SEARCH_BACKWARD 2
283 int search_next_done;
284 regex_t regex;
285 };
287 static const struct got_error *open_diff_view(struct tog_view *,
288 struct got_object_id *, struct got_object_id *, struct tog_view *,
289 struct got_reflist_head *, struct got_repository *);
290 static const struct got_error *show_diff_view(struct tog_view *);
291 static const struct got_error *input_diff_view(struct tog_view **,
292 struct tog_view **, struct tog_view **, struct tog_view *, int);
293 static const struct got_error* close_diff_view(struct tog_view *);
295 static const struct got_error *open_log_view(struct tog_view *,
296 struct got_object_id *, struct got_reflist_head *,
297 struct got_repository *, const char *, const char *, int);
298 static const struct got_error * show_log_view(struct tog_view *);
299 static const struct got_error *input_log_view(struct tog_view **,
300 struct tog_view **, struct tog_view **, struct tog_view *, int);
301 static const struct got_error *close_log_view(struct tog_view *);
302 static const struct got_error *search_start_log_view(struct tog_view *);
303 static const struct got_error *search_next_log_view(struct tog_view *);
305 static const struct got_error *open_blame_view(struct tog_view *, char *,
306 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
307 static const struct got_error *show_blame_view(struct tog_view *);
308 static const struct got_error *input_blame_view(struct tog_view **,
309 struct tog_view **, struct tog_view **, struct tog_view *, int);
310 static const struct got_error *close_blame_view(struct tog_view *);
311 static const struct got_error *search_start_blame_view(struct tog_view *);
312 static const struct got_error *search_next_blame_view(struct tog_view *);
314 static const struct got_error *open_tree_view(struct tog_view *,
315 struct got_tree_object *, struct got_object_id *,
316 struct got_reflist_head *, struct got_repository *);
317 static const struct got_error *show_tree_view(struct tog_view *);
318 static const struct got_error *input_tree_view(struct tog_view **,
319 struct tog_view **, struct tog_view **, struct tog_view *, int);
320 static const struct got_error *close_tree_view(struct tog_view *);
321 static const struct got_error *search_start_tree_view(struct tog_view *);
322 static const struct got_error *search_next_tree_view(struct tog_view *);
324 static volatile sig_atomic_t tog_sigwinch_received;
325 static volatile sig_atomic_t tog_sigpipe_received;
327 static void
328 tog_sigwinch(int signo)
330 tog_sigwinch_received = 1;
333 static void
334 tog_sigpipe(int signo)
336 tog_sigpipe_received = 1;
339 static const struct got_error *
340 view_close(struct tog_view *view)
342 const struct got_error *err = NULL;
344 if (view->child) {
345 view_close(view->child);
346 view->child = NULL;
348 if (view->close)
349 err = view->close(view);
350 if (view->panel)
351 del_panel(view->panel);
352 if (view->window)
353 delwin(view->window);
354 free(view);
355 return err;
358 static struct tog_view *
359 view_open(int nlines, int ncols, int begin_y, int begin_x,
360 enum tog_view_type type)
362 struct tog_view *view = calloc(1, sizeof(*view));
364 if (view == NULL)
365 return NULL;
367 view->type = type;
368 view->lines = LINES;
369 view->cols = COLS;
370 view->nlines = nlines ? nlines : LINES - begin_y;
371 view->ncols = ncols ? ncols : COLS - begin_x;
372 view->begin_y = begin_y;
373 view->begin_x = begin_x;
374 view->window = newwin(nlines, ncols, begin_y, begin_x);
375 if (view->window == NULL) {
376 view_close(view);
377 return NULL;
379 view->panel = new_panel(view->window);
380 if (view->panel == NULL ||
381 set_panel_userptr(view->panel, view) != OK) {
382 view_close(view);
383 return NULL;
386 keypad(view->window, TRUE);
387 return view;
390 static int
391 view_split_begin_x(int begin_x)
393 if (begin_x > 0 || COLS < 120)
394 return 0;
395 return (COLS - MAX(COLS / 2, 80));
398 static const struct got_error *view_resize(struct tog_view *);
400 static const struct got_error *
401 view_splitscreen(struct tog_view *view)
403 const struct got_error *err = NULL;
405 view->begin_y = 0;
406 view->begin_x = view_split_begin_x(0);
407 view->nlines = LINES;
408 view->ncols = COLS - view->begin_x;
409 view->lines = LINES;
410 view->cols = COLS;
411 err = view_resize(view);
412 if (err)
413 return err;
415 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
416 return got_error_from_errno("mvwin");
418 return NULL;
421 static const struct got_error *
422 view_fullscreen(struct tog_view *view)
424 const struct got_error *err = NULL;
426 view->begin_x = 0;
427 view->begin_y = 0;
428 view->nlines = LINES;
429 view->ncols = COLS;
430 view->lines = LINES;
431 view->cols = COLS;
432 err = view_resize(view);
433 if (err)
434 return err;
436 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
437 return got_error_from_errno("mvwin");
439 return NULL;
442 static int
443 view_is_parent_view(struct tog_view *view)
445 return view->parent == NULL;
448 static const struct got_error *
449 view_resize(struct tog_view *view)
451 int nlines, ncols;
453 if (view->lines > LINES)
454 nlines = view->nlines - (view->lines - LINES);
455 else
456 nlines = view->nlines + (LINES - view->lines);
458 if (view->cols > COLS)
459 ncols = view->ncols - (view->cols - COLS);
460 else
461 ncols = view->ncols + (COLS - view->cols);
463 if (wresize(view->window, nlines, ncols) == ERR)
464 return got_error_from_errno("wresize");
465 if (replace_panel(view->panel, view->window) == ERR)
466 return got_error_from_errno("replace_panel");
467 wclear(view->window);
469 view->nlines = nlines;
470 view->ncols = ncols;
471 view->lines = LINES;
472 view->cols = COLS;
474 if (view->child) {
475 view->child->begin_x = view_split_begin_x(view->begin_x);
476 if (view->child->begin_x == 0) {
477 view_fullscreen(view->child);
478 if (view->child->focussed)
479 show_panel(view->child->panel);
480 else
481 show_panel(view->panel);
482 } else {
483 view_splitscreen(view->child);
484 show_panel(view->child->panel);
488 return NULL;
491 static const struct got_error *
492 view_close_child(struct tog_view *view)
494 const struct got_error *err = NULL;
496 if (view->child == NULL)
497 return NULL;
499 err = view_close(view->child);
500 view->child = NULL;
501 return err;
504 static const struct got_error *
505 view_set_child(struct tog_view *view, struct tog_view *child)
507 const struct got_error *err = NULL;
509 view->child = child;
510 child->parent = view;
511 return err;
514 static int
515 view_is_splitscreen(struct tog_view *view)
517 return view->begin_x > 0;
520 static void
521 tog_resizeterm(void)
523 int cols, lines;
524 struct winsize size;
526 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
527 cols = 80; /* Default */
528 lines = 24;
529 } else {
530 cols = size.ws_col;
531 lines = size.ws_row;
533 resize_term(lines, cols);
536 static const struct got_error *
537 view_search_start(struct tog_view *view)
539 const struct got_error *err = NULL;
540 char pattern[1024];
541 int ret;
543 if (view->nlines < 1)
544 return NULL;
546 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
547 view->begin_x, "/");
548 wclrtoeol(view->window);
550 nocbreak();
551 echo();
552 ret = wgetnstr(view->window, pattern, sizeof(pattern));
553 cbreak();
554 noecho();
555 if (ret == ERR)
556 return NULL;
558 if (view->searching) {
559 regfree(&view->regex);
560 view->searching = 0;
563 if (regcomp(&view->regex, pattern,
564 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
565 err = view->search_start(view);
566 if (err) {
567 regfree(&view->regex);
568 return err;
570 view->searching = TOG_SEARCH_FORWARD;
571 view->search_next_done = 0;
572 view->search_next(view);
575 return NULL;
578 static const struct got_error *
579 view_input(struct tog_view **new, struct tog_view **dead,
580 struct tog_view **focus, int *done, struct tog_view *view,
581 struct tog_view_list_head *views)
583 const struct got_error *err = NULL;
584 struct tog_view *v;
585 int ch, errcode;
587 *new = NULL;
588 *dead = NULL;
589 *focus = NULL;
591 if (view->searching && !view->search_next_done) {
592 errcode = pthread_mutex_unlock(&tog_mutex);
593 if (errcode)
594 return got_error_set_errno(errcode,
595 "pthread_mutex_unlock");
596 pthread_yield();
597 errcode = pthread_mutex_lock(&tog_mutex);
598 if (errcode)
599 return got_error_set_errno(errcode,
600 "pthread_mutex_lock");
601 view->search_next(view);
602 return NULL;
605 nodelay(stdscr, FALSE);
606 /* Allow threads to make progress while we are waiting for input. */
607 errcode = pthread_mutex_unlock(&tog_mutex);
608 if (errcode)
609 return got_error_set_errno(errcode, "pthread_mutex_unlock");
610 ch = wgetch(view->window);
611 errcode = pthread_mutex_lock(&tog_mutex);
612 if (errcode)
613 return got_error_set_errno(errcode, "pthread_mutex_lock");
614 nodelay(stdscr, TRUE);
616 if (tog_sigwinch_received) {
617 tog_resizeterm();
618 tog_sigwinch_received = 0;
619 TAILQ_FOREACH(v, views, entry) {
620 err = view_resize(v);
621 if (err)
622 return err;
623 err = v->input(new, dead, focus, v, KEY_RESIZE);
624 if (err)
625 return err;
629 switch (ch) {
630 case ERR:
631 break;
632 case '\t':
633 if (view->child) {
634 *focus = view->child;
635 view->child_focussed = 1;
636 } else if (view->parent) {
637 *focus = view->parent;
638 view->parent->child_focussed = 0;
640 break;
641 case 'q':
642 err = view->input(new, dead, focus, view, ch);
643 *dead = view;
644 break;
645 case 'Q':
646 *done = 1;
647 break;
648 case 'f':
649 if (view_is_parent_view(view)) {
650 if (view->child == NULL)
651 break;
652 if (view_is_splitscreen(view->child)) {
653 *focus = view->child;
654 view->child_focussed = 1;
655 err = view_fullscreen(view->child);
656 } else
657 err = view_splitscreen(view->child);
658 if (err)
659 break;
660 err = view->child->input(new, dead, focus,
661 view->child, KEY_RESIZE);
662 } else {
663 if (view_is_splitscreen(view)) {
664 *focus = view;
665 view->parent->child_focussed = 1;
666 err = view_fullscreen(view);
667 } else {
668 err = view_splitscreen(view);
670 if (err)
671 break;
672 err = view->input(new, dead, focus, view,
673 KEY_RESIZE);
675 break;
676 case KEY_RESIZE:
677 break;
678 case '/':
679 if (view->search_start)
680 view_search_start(view);
681 else
682 err = view->input(new, dead, focus, view, ch);
683 break;
684 case 'N':
685 case 'n':
686 if (view->search_next && view->searching) {
687 view->searching = (ch == 'n' ?
688 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
689 view->search_next_done = 0;
690 view->search_next(view);
691 } else
692 err = view->input(new, dead, focus, view, ch);
693 break;
694 default:
695 err = view->input(new, dead, focus, view, ch);
696 break;
699 return err;
702 void
703 view_vborder(struct tog_view *view)
705 PANEL *panel;
706 struct tog_view *view_above;
708 if (view->parent)
709 return view_vborder(view->parent);
711 panel = panel_above(view->panel);
712 if (panel == NULL)
713 return;
715 view_above = panel_userptr(panel);
716 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
717 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
720 int
721 view_needs_focus_indication(struct tog_view *view)
723 if (view_is_parent_view(view)) {
724 if (view->child == NULL || view->child_focussed)
725 return 0;
726 if (!view_is_splitscreen(view->child))
727 return 0;
728 } else if (!view_is_splitscreen(view))
729 return 0;
731 return view->focussed;
734 static const struct got_error *
735 view_loop(struct tog_view *view)
737 const struct got_error *err = NULL;
738 struct tog_view_list_head views;
739 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
740 int fast_refresh = 10;
741 int done = 0, errcode;
743 errcode = pthread_mutex_lock(&tog_mutex);
744 if (errcode)
745 return got_error_set_errno(errcode, "pthread_mutex_lock");
747 TAILQ_INIT(&views);
748 TAILQ_INSERT_HEAD(&views, view, entry);
750 main_view = view;
751 view->focussed = 1;
752 err = view->show(view);
753 if (err)
754 return err;
755 update_panels();
756 doupdate();
757 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
758 /* Refresh fast during initialization, then become slower. */
759 if (fast_refresh && fast_refresh-- == 0)
760 halfdelay(10); /* switch to once per second */
762 err = view_input(&new_view, &dead_view, &focus_view, &done,
763 view, &views);
764 if (err)
765 break;
766 if (dead_view) {
767 struct tog_view *prev = NULL;
769 if (view_is_parent_view(dead_view))
770 prev = TAILQ_PREV(dead_view,
771 tog_view_list_head, entry);
772 else if (view->parent != dead_view)
773 prev = view->parent;
775 if (dead_view->parent)
776 dead_view->parent->child = NULL;
777 else
778 TAILQ_REMOVE(&views, dead_view, entry);
780 err = view_close(dead_view);
781 if (err || (dead_view == main_view && new_view == NULL))
782 goto done;
784 if (view == dead_view) {
785 if (focus_view)
786 view = focus_view;
787 else if (prev)
788 view = prev;
789 else if (!TAILQ_EMPTY(&views))
790 view = TAILQ_LAST(&views,
791 tog_view_list_head);
792 else
793 view = NULL;
794 if (view) {
795 if (view->child && view->child_focussed)
796 focus_view = view->child;
797 else
798 focus_view = view;
802 if (new_view) {
803 struct tog_view *v, *t;
804 /* Only allow one parent view per type. */
805 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
806 if (v->type != new_view->type)
807 continue;
808 TAILQ_REMOVE(&views, v, entry);
809 err = view_close(v);
810 if (err)
811 goto done;
812 break;
814 TAILQ_INSERT_TAIL(&views, new_view, entry);
815 view = new_view;
816 if (focus_view == NULL)
817 focus_view = new_view;
819 if (focus_view) {
820 show_panel(focus_view->panel);
821 if (view)
822 view->focussed = 0;
823 focus_view->focussed = 1;
824 view = focus_view;
825 if (new_view)
826 show_panel(new_view->panel);
827 if (view->child && view_is_splitscreen(view->child))
828 show_panel(view->child->panel);
830 if (view) {
831 if (focus_view == NULL) {
832 view->focussed = 1;
833 show_panel(view->panel);
834 if (view->child && view_is_splitscreen(view->child))
835 show_panel(view->child->panel);
836 focus_view = view;
838 if (view->parent) {
839 err = view->parent->show(view->parent);
840 if (err)
841 goto done;
843 err = view->show(view);
844 if (err)
845 goto done;
846 if (view->child) {
847 err = view->child->show(view->child);
848 if (err)
849 goto done;
851 update_panels();
852 doupdate();
855 done:
856 while (!TAILQ_EMPTY(&views)) {
857 view = TAILQ_FIRST(&views);
858 TAILQ_REMOVE(&views, view, entry);
859 view_close(view);
862 errcode = pthread_mutex_unlock(&tog_mutex);
863 if (errcode && err == NULL)
864 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
866 return err;
869 __dead static void
870 usage_log(void)
872 endwin();
873 fprintf(stderr,
874 "usage: %s log [-c commit] [-r repository-path] [path]\n",
875 getprogname());
876 exit(1);
879 /* Create newly allocated wide-character string equivalent to a byte string. */
880 static const struct got_error *
881 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
883 char *vis = NULL;
884 const struct got_error *err = NULL;
886 *ws = NULL;
887 *wlen = mbstowcs(NULL, s, 0);
888 if (*wlen == (size_t)-1) {
889 int vislen;
890 if (errno != EILSEQ)
891 return got_error_from_errno("mbstowcs");
893 /* byte string invalid in current encoding; try to "fix" it */
894 err = got_mbsavis(&vis, &vislen, s);
895 if (err)
896 return err;
897 *wlen = mbstowcs(NULL, vis, 0);
898 if (*wlen == (size_t)-1) {
899 err = got_error_from_errno("mbstowcs"); /* give up */
900 goto done;
904 *ws = calloc(*wlen + 1, sizeof(*ws));
905 if (*ws == NULL) {
906 err = got_error_from_errno("calloc");
907 goto done;
910 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
911 err = got_error_from_errno("mbstowcs");
912 done:
913 free(vis);
914 if (err) {
915 free(*ws);
916 *ws = NULL;
917 *wlen = 0;
919 return err;
922 /* Format a line for display, ensuring that it won't overflow a width limit. */
923 static const struct got_error *
924 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit)
926 const struct got_error *err = NULL;
927 int cols = 0;
928 wchar_t *wline = NULL;
929 size_t wlen;
930 int i;
932 *wlinep = NULL;
933 *widthp = 0;
935 err = mbs2ws(&wline, &wlen, line);
936 if (err)
937 return err;
939 i = 0;
940 while (i < wlen && cols < wlimit) {
941 int width = wcwidth(wline[i]);
942 switch (width) {
943 case 0:
944 i++;
945 break;
946 case 1:
947 case 2:
948 if (cols + width <= wlimit)
949 cols += width;
950 i++;
951 break;
952 case -1:
953 if (wline[i] == L'\t')
954 cols += TABSIZE - ((cols + 1) % TABSIZE);
955 i++;
956 break;
957 default:
958 err = got_error_from_errno("wcwidth");
959 goto done;
962 wline[i] = L'\0';
963 if (widthp)
964 *widthp = cols;
965 done:
966 if (err)
967 free(wline);
968 else
969 *wlinep = wline;
970 return err;
973 static const struct got_error*
974 build_refs_str(char **refs_str, struct got_reflist_head *refs,
975 struct got_object_id *id)
977 static const struct got_error *err = NULL;
978 struct got_reflist_entry *re;
979 char *s;
980 const char *name;
982 *refs_str = NULL;
984 SIMPLEQ_FOREACH(re, refs, entry) {
985 if (got_object_id_cmp(re->id, id) != 0)
986 continue;
987 name = got_ref_get_name(re->ref);
988 if (strcmp(name, GOT_REF_HEAD) == 0)
989 continue;
990 if (strncmp(name, "refs/", 5) == 0)
991 name += 5;
992 if (strncmp(name, "got/", 4) == 0)
993 continue;
994 if (strncmp(name, "heads/", 6) == 0)
995 name += 6;
996 if (strncmp(name, "remotes/", 8) == 0)
997 name += 8;
998 s = *refs_str;
999 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1000 s ? ", " : "", name) == -1) {
1001 err = got_error_from_errno("asprintf");
1002 free(s);
1003 *refs_str = NULL;
1004 break;
1006 free(s);
1009 return err;
1012 static const struct got_error *
1013 format_author(wchar_t **wauthor, int *author_width, char *author, int limit)
1015 char *smallerthan, *at;
1017 smallerthan = strchr(author, '<');
1018 if (smallerthan && smallerthan[1] != '\0')
1019 author = smallerthan + 1;
1020 at = strchr(author, '@');
1021 if (at)
1022 *at = '\0';
1023 return format_line(wauthor, author_width, author, limit);
1026 static const struct got_error *
1027 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1028 struct got_object_id *id, struct got_reflist_head *refs,
1029 int author_display_cols)
1031 const struct got_error *err = NULL;
1032 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1033 char *logmsg0 = NULL, *logmsg = NULL;
1034 char *author = NULL;
1035 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1036 int author_width, logmsg_width;
1037 char *newline, *line = NULL;
1038 int col, limit;
1039 static const size_t date_display_cols = 9;
1040 const int avail = view->ncols;
1041 struct tm tm;
1042 time_t committer_time;
1044 committer_time = got_object_commit_get_committer_time(commit);
1045 if (localtime_r(&committer_time, &tm) == NULL)
1046 return got_error_from_errno("localtime_r");
1047 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1048 >= sizeof(datebuf))
1049 return got_error(GOT_ERR_NO_SPACE);
1051 if (avail < date_display_cols)
1052 limit = MIN(sizeof(datebuf) - 1, avail);
1053 else
1054 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1055 waddnstr(view->window, datebuf, limit);
1056 col = limit + 1;
1057 if (col > avail)
1058 goto done;
1060 author = strdup(got_object_commit_get_author(commit));
1061 if (author == NULL) {
1062 err = got_error_from_errno("strdup");
1063 goto done;
1065 err = format_author(&wauthor, &author_width, author, avail - col);
1066 if (err)
1067 goto done;
1068 waddwstr(view->window, wauthor);
1069 col += author_width;
1070 while (col <= avail && author_width < author_display_cols + 2) {
1071 waddch(view->window, ' ');
1072 col++;
1073 author_width++;
1075 if (col > avail)
1076 goto done;
1078 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1079 if (logmsg0 == NULL) {
1080 err = got_error_from_errno("strdup");
1081 goto done;
1083 logmsg = logmsg0;
1084 while (*logmsg == '\n')
1085 logmsg++;
1086 newline = strchr(logmsg, '\n');
1087 if (newline)
1088 *newline = '\0';
1089 limit = avail - col;
1090 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit);
1091 if (err)
1092 goto done;
1093 waddwstr(view->window, wlogmsg);
1094 col += logmsg_width;
1095 while (col <= avail) {
1096 waddch(view->window, ' ');
1097 col++;
1099 done:
1100 free(logmsg0);
1101 free(wlogmsg);
1102 free(author);
1103 free(wauthor);
1104 free(line);
1105 return err;
1108 static struct commit_queue_entry *
1109 alloc_commit_queue_entry(struct got_commit_object *commit,
1110 struct got_object_id *id)
1112 struct commit_queue_entry *entry;
1114 entry = calloc(1, sizeof(*entry));
1115 if (entry == NULL)
1116 return NULL;
1118 entry->id = id;
1119 entry->commit = commit;
1120 return entry;
1123 static void
1124 pop_commit(struct commit_queue *commits)
1126 struct commit_queue_entry *entry;
1128 entry = TAILQ_FIRST(&commits->head);
1129 TAILQ_REMOVE(&commits->head, entry, entry);
1130 got_object_commit_close(entry->commit);
1131 commits->ncommits--;
1132 /* Don't free entry->id! It is owned by the commit graph. */
1133 free(entry);
1136 static void
1137 free_commits(struct commit_queue *commits)
1139 while (!TAILQ_EMPTY(&commits->head))
1140 pop_commit(commits);
1143 static const struct got_error *
1144 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1145 int minqueue, struct got_repository *repo, const char *path)
1147 const struct got_error *err = NULL;
1148 int nqueued = 0;
1151 * We keep all commits open throughout the lifetime of the log
1152 * view in order to avoid having to re-fetch commits from disk
1153 * while updating the display.
1155 while (nqueued < minqueue) {
1156 struct got_object_id *id;
1157 struct got_commit_object *commit;
1158 struct commit_queue_entry *entry;
1159 int errcode;
1161 err = got_commit_graph_iter_next(&id, graph);
1162 if (err) {
1163 if (err->code != GOT_ERR_ITER_NEED_MORE)
1164 break;
1165 err = got_commit_graph_fetch_commits(graph,
1166 minqueue, repo);
1167 if (err)
1168 return err;
1169 continue;
1172 if (id == NULL)
1173 break;
1175 err = got_object_open_as_commit(&commit, repo, id);
1176 if (err)
1177 break;
1178 entry = alloc_commit_queue_entry(commit, id);
1179 if (entry == NULL) {
1180 err = got_error_from_errno("alloc_commit_queue_entry");
1181 break;
1184 errcode = pthread_mutex_lock(&tog_mutex);
1185 if (errcode) {
1186 err = got_error_set_errno(errcode, "pthread_mutex_lock");
1187 break;
1190 entry->idx = commits->ncommits;
1191 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1192 nqueued++;
1193 commits->ncommits++;
1195 errcode = pthread_mutex_unlock(&tog_mutex);
1196 if (errcode && err == NULL)
1197 err = got_error_set_errno(errcode,
1198 "pthread_mutex_unlock");
1201 return err;
1204 static const struct got_error *
1205 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1206 struct got_repository *repo)
1208 const struct got_error *err = NULL;
1209 struct got_reference *head_ref;
1211 *head_id = NULL;
1213 err = got_ref_open(&head_ref, repo, branch_name, 0);
1214 if (err)
1215 return err;
1217 err = got_ref_resolve(head_id, repo, head_ref);
1218 got_ref_close(head_ref);
1219 if (err) {
1220 *head_id = NULL;
1221 return err;
1224 return NULL;
1227 static const struct got_error *
1228 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1229 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1230 struct commit_queue *commits, int selected_idx, int limit,
1231 struct got_reflist_head *refs, const char *path, int commits_needed)
1233 const struct got_error *err = NULL;
1234 struct commit_queue_entry *entry;
1235 int width;
1236 int ncommits, author_cols = 10;
1237 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1238 char *refs_str = NULL;
1239 wchar_t *wline;
1241 entry = first;
1242 ncommits = 0;
1243 while (entry) {
1244 if (ncommits == selected_idx) {
1245 *selected = entry;
1246 break;
1248 entry = TAILQ_NEXT(entry, entry);
1249 ncommits++;
1252 if (*selected && !(view->searching && view->search_next_done == 0)) {
1253 err = got_object_id_str(&id_str, (*selected)->id);
1254 if (err)
1255 return err;
1256 if (refs) {
1257 err = build_refs_str(&refs_str, refs, (*selected)->id);
1258 if (err)
1259 goto done;
1263 if (commits_needed == 0)
1264 halfdelay(10); /* disable fast refresh */
1266 if (asprintf(&ncommits_str, " [%d/%d] %s",
1267 entry ? entry->idx + 1 : 0, commits->ncommits,
1268 commits_needed > 0 ?
1269 (view->searching && view->search_next_done == 0
1270 ? "searching..." : "loading... ") :
1271 (refs_str ? refs_str : "")) == -1) {
1272 err = got_error_from_errno("asprintf");
1273 goto done;
1276 if (path && strcmp(path, "/") != 0) {
1277 if (asprintf(&header, "commit %s %s%s",
1278 id_str ? id_str : "........................................",
1279 path, ncommits_str) == -1) {
1280 err = got_error_from_errno("asprintf");
1281 header = NULL;
1282 goto done;
1284 } else if (asprintf(&header, "commit %s%s",
1285 id_str ? id_str : "........................................",
1286 ncommits_str) == -1) {
1287 err = got_error_from_errno("asprintf");
1288 header = NULL;
1289 goto done;
1291 err = format_line(&wline, &width, header, view->ncols);
1292 if (err)
1293 goto done;
1295 werase(view->window);
1297 if (view_needs_focus_indication(view))
1298 wstandout(view->window);
1299 waddwstr(view->window, wline);
1300 while (width < view->ncols) {
1301 waddch(view->window, ' ');
1302 width++;
1304 if (view_needs_focus_indication(view))
1305 wstandend(view->window);
1306 free(wline);
1307 if (limit <= 1)
1308 goto done;
1310 /* Grow author column size if necessary. */
1311 entry = first;
1312 ncommits = 0;
1313 while (entry) {
1314 char *author;
1315 wchar_t *wauthor;
1316 int width;
1317 if (ncommits >= limit - 1)
1318 break;
1319 author = strdup(got_object_commit_get_author(entry->commit));
1320 if (author == NULL) {
1321 err = got_error_from_errno("strdup");
1322 goto done;
1324 err = format_author(&wauthor, &width, author, COLS);
1325 if (author_cols < width)
1326 author_cols = width;
1327 free(wauthor);
1328 free(author);
1329 entry = TAILQ_NEXT(entry, entry);
1332 entry = first;
1333 *last = first;
1334 ncommits = 0;
1335 while (entry) {
1336 if (ncommits >= limit - 1)
1337 break;
1338 if (ncommits == selected_idx)
1339 wstandout(view->window);
1340 err = draw_commit(view, entry->commit, entry->id, refs,
1341 author_cols);
1342 if (ncommits == selected_idx)
1343 wstandend(view->window);
1344 if (err)
1345 goto done;
1346 ncommits++;
1347 *last = entry;
1348 entry = TAILQ_NEXT(entry, entry);
1351 view_vborder(view);
1352 done:
1353 free(id_str);
1354 free(refs_str);
1355 free(ncommits_str);
1356 free(header);
1357 return err;
1360 static void
1361 scroll_up(struct tog_view *view,
1362 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1363 struct commit_queue *commits)
1365 struct commit_queue_entry *entry;
1366 int nscrolled = 0;
1368 entry = TAILQ_FIRST(&commits->head);
1369 if (*first_displayed_entry == entry)
1370 return;
1372 entry = *first_displayed_entry;
1373 while (entry && nscrolled < maxscroll) {
1374 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1375 if (entry) {
1376 *first_displayed_entry = entry;
1377 nscrolled++;
1382 static const struct got_error *
1383 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1384 pthread_cond_t *need_commits)
1386 int errcode;
1387 int max_wait = 20;
1389 halfdelay(1); /* fast refresh while loading commits */
1391 while (*commits_needed > 0) {
1392 if (*log_complete)
1393 break;
1395 /* Wake the log thread. */
1396 errcode = pthread_cond_signal(need_commits);
1397 if (errcode)
1398 return got_error_set_errno(errcode,
1399 "pthread_cond_signal");
1400 errcode = pthread_mutex_unlock(&tog_mutex);
1401 if (errcode)
1402 return got_error_set_errno(errcode,
1403 "pthread_mutex_unlock");
1404 pthread_yield();
1405 errcode = pthread_mutex_lock(&tog_mutex);
1406 if (errcode)
1407 return got_error_set_errno(errcode,
1408 "pthread_mutex_lock");
1410 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1412 * Thread is not done yet; lose a key press
1413 * and let the user retry... this way the GUI
1414 * remains interactive while logging deep paths
1415 * with few commits in history.
1417 return NULL;
1421 return NULL;
1424 static const struct got_error *
1425 scroll_down(struct tog_view *view,
1426 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1427 struct commit_queue_entry **last_displayed_entry,
1428 struct commit_queue *commits, int *log_complete, int *commits_needed,
1429 pthread_cond_t *need_commits)
1431 const struct got_error *err = NULL;
1432 struct commit_queue_entry *pentry;
1433 int nscrolled = 0;
1435 if (*last_displayed_entry == NULL)
1436 return NULL;
1438 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1439 if (pentry == NULL && !*log_complete) {
1441 * Ask the log thread for required amount of commits
1442 * plus some amount of pre-fetching.
1444 (*commits_needed) += maxscroll + 20;
1445 err = trigger_log_thread(0, commits_needed, log_complete,
1446 need_commits);
1447 if (err)
1448 return err;
1451 do {
1452 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1453 if (pentry == NULL)
1454 break;
1456 *last_displayed_entry = pentry;
1458 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1459 if (pentry == NULL)
1460 break;
1461 *first_displayed_entry = pentry;
1462 } while (++nscrolled < maxscroll);
1464 return err;
1467 static const struct got_error *
1468 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1469 struct got_commit_object *commit, struct got_object_id *commit_id,
1470 struct tog_view *log_view, struct got_reflist_head *refs,
1471 struct got_repository *repo)
1473 const struct got_error *err;
1474 struct got_object_qid *parent_id;
1475 struct tog_view *diff_view;
1477 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1478 if (diff_view == NULL)
1479 return got_error_from_errno("view_open");
1481 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1482 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1483 commit_id, log_view, refs, repo);
1484 if (err == NULL)
1485 *new_view = diff_view;
1486 return err;
1489 static const struct got_error *
1490 tree_view_visit_subtree(struct got_tree_object *subtree,
1491 struct tog_tree_view_state *s)
1493 struct tog_parent_tree *parent;
1495 parent = calloc(1, sizeof(*parent));
1496 if (parent == NULL)
1497 return got_error_from_errno("calloc");
1499 parent->tree = s->tree;
1500 parent->first_displayed_entry = s->first_displayed_entry;
1501 parent->selected_entry = s->selected_entry;
1502 parent->selected = s->selected;
1503 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1504 s->tree = subtree;
1505 s->entries = got_object_tree_get_entries(s->tree);
1506 s->selected = 0;
1507 s->first_displayed_entry = NULL;
1508 return NULL;
1512 static const struct got_error *
1513 browse_commit_tree(struct tog_view **new_view, int begin_x,
1514 struct commit_queue_entry *entry, const char *path,
1515 struct got_reflist_head *refs, struct got_repository *repo)
1517 const struct got_error *err = NULL;
1518 struct got_tree_object *tree;
1519 struct got_tree_entry *te;
1520 struct tog_tree_view_state *s;
1521 struct tog_view *tree_view;
1522 char *slash, *subpath = NULL;
1523 const char *p;
1525 err = got_object_open_as_tree(&tree, repo,
1526 got_object_commit_get_tree_id(entry->commit));
1527 if (err)
1528 return err;
1530 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1531 if (tree_view == NULL)
1532 return got_error_from_errno("view_open");
1534 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1535 if (err) {
1536 got_object_tree_close(tree);
1537 return err;
1539 s = &tree_view->state.tree;
1541 *new_view = tree_view;
1543 /* Walk the path and open corresponding tree objects. */
1544 p = path;
1545 while (*p) {
1546 struct got_object_id *tree_id;
1548 while (p[0] == '/')
1549 p++;
1551 /* Ensure the correct subtree entry is selected. */
1552 slash = strchr(p, '/');
1553 if (slash == NULL)
1554 slash = strchr(p, '\0');
1555 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1556 if (strncmp(p, te->name, slash - p) == 0) {
1557 s->selected_entry = te;
1558 break;
1560 s->selected++;
1562 if (s->selected_entry == NULL) {
1563 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1564 break;
1566 if (s->tree != s->root)
1567 s->selected++; /* skip '..' */
1569 if (!S_ISDIR(s->selected_entry->mode)) {
1570 /* Jump to this file's entry. */
1571 s->first_displayed_entry = s->selected_entry;
1572 s->selected = 0;
1573 break;
1576 slash = strchr(p, '/');
1577 if (slash)
1578 subpath = strndup(path, slash - path);
1579 else
1580 subpath = strdup(path);
1581 if (subpath == NULL) {
1582 err = got_error_from_errno("strdup");
1583 break;
1586 err = got_object_id_by_path(&tree_id, repo, entry->id,
1587 subpath);
1588 if (err)
1589 break;
1591 err = got_object_open_as_tree(&tree, repo, tree_id);
1592 free(tree_id);
1593 if (err)
1594 break;
1596 err = tree_view_visit_subtree(tree, s);
1597 if (err) {
1598 got_object_tree_close(tree);
1599 break;
1601 if (slash == NULL)
1602 break;
1603 free(subpath);
1604 subpath = NULL;
1605 p = slash;
1608 free(subpath);
1609 return err;
1612 static void *
1613 log_thread(void *arg)
1615 const struct got_error *err = NULL;
1616 int errcode = 0;
1617 struct tog_log_thread_args *a = arg;
1618 int done = 0;
1620 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo);
1621 if (err)
1622 return (void *)err;
1624 while (!done && !err && !tog_sigpipe_received) {
1625 err = queue_commits(a->graph, a->commits, 1, a->repo,
1626 a->in_repo_path);
1627 if (err) {
1628 if (err->code != GOT_ERR_ITER_COMPLETED)
1629 return (void *)err;
1630 err = NULL;
1631 done = 1;
1632 } else if (a->commits_needed > 0)
1633 a->commits_needed--;
1635 errcode = pthread_mutex_lock(&tog_mutex);
1636 if (errcode) {
1637 err = got_error_set_errno(errcode,
1638 "pthread_mutex_lock");
1639 break;
1640 } else if (*a->quit)
1641 done = 1;
1642 else if (*a->first_displayed_entry == NULL) {
1643 *a->first_displayed_entry =
1644 TAILQ_FIRST(&a->commits->head);
1645 *a->selected_entry = *a->first_displayed_entry;
1648 if (done)
1649 a->commits_needed = 0;
1650 else if (a->commits_needed == 0) {
1651 errcode = pthread_cond_wait(&a->need_commits,
1652 &tog_mutex);
1653 if (errcode)
1654 err = got_error_set_errno(errcode,
1655 "pthread_cond_wait");
1658 errcode = pthread_mutex_unlock(&tog_mutex);
1659 if (errcode && err == NULL)
1660 err = got_error_set_errno(errcode,
1661 "pthread_mutex_unlock");
1663 a->log_complete = 1;
1664 return (void *)err;
1667 static const struct got_error *
1668 stop_log_thread(struct tog_log_view_state *s)
1670 const struct got_error *err = NULL;
1671 int errcode;
1673 if (s->thread) {
1674 s->quit = 1;
1675 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1676 if (errcode)
1677 return got_error_set_errno(errcode,
1678 "pthread_cond_signal");
1679 errcode = pthread_mutex_unlock(&tog_mutex);
1680 if (errcode)
1681 return got_error_set_errno(errcode,
1682 "pthread_mutex_unlock");
1683 errcode = pthread_join(s->thread, (void **)&err);
1684 if (errcode)
1685 return got_error_set_errno(errcode, "pthread_join");
1686 errcode = pthread_mutex_lock(&tog_mutex);
1687 if (errcode)
1688 return got_error_set_errno(errcode,
1689 "pthread_mutex_lock");
1690 s->thread = NULL;
1693 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1694 if (errcode && err == NULL)
1695 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1697 if (s->thread_args.repo) {
1698 got_repo_close(s->thread_args.repo);
1699 s->thread_args.repo = NULL;
1702 if (s->thread_args.graph) {
1703 got_commit_graph_close(s->thread_args.graph);
1704 s->thread_args.graph = NULL;
1707 return err;
1710 static const struct got_error *
1711 close_log_view(struct tog_view *view)
1713 const struct got_error *err = NULL;
1714 struct tog_log_view_state *s = &view->state.log;
1716 err = stop_log_thread(s);
1717 free_commits(&s->commits);
1718 free(s->in_repo_path);
1719 s->in_repo_path = NULL;
1720 free(s->start_id);
1721 s->start_id = NULL;
1722 return err;
1725 static const struct got_error *
1726 search_start_log_view(struct tog_view *view)
1728 struct tog_log_view_state *s = &view->state.log;
1730 s->matched_entry = NULL;
1731 s->search_entry = NULL;
1732 return NULL;
1735 static int
1736 match_commit(struct got_commit_object *commit, const char *id_str,
1737 regex_t *regex)
1739 regmatch_t regmatch;
1741 if (regexec(regex, got_object_commit_get_author(commit), 1,
1742 &regmatch, 0) == 0 ||
1743 regexec(regex, got_object_commit_get_committer(commit), 1,
1744 &regmatch, 0) == 0 ||
1745 regexec(regex, got_object_commit_get_logmsg(commit), 1,
1746 &regmatch, 0) == 0 ||
1747 regexec(regex, id_str, 1, &regmatch, 0) == 0)
1748 return 1;
1750 return 0;
1753 static const struct got_error *
1754 search_next_log_view(struct tog_view *view)
1756 const struct got_error *err = NULL;
1757 struct tog_log_view_state *s = &view->state.log;
1758 struct commit_queue_entry *entry;
1760 if (!view->searching) {
1761 view->search_next_done = 1;
1762 return NULL;
1765 if (s->search_entry) {
1766 if (wgetch(view->window) == KEY_BACKSPACE) {
1767 view->search_next_done = 1;
1768 return NULL;
1770 if (view->searching == TOG_SEARCH_FORWARD)
1771 entry = TAILQ_NEXT(s->search_entry, entry);
1772 else
1773 entry = TAILQ_PREV(s->search_entry,
1774 commit_queue_head, entry);
1775 } else if (s->matched_entry) {
1776 if (view->searching == TOG_SEARCH_FORWARD)
1777 entry = TAILQ_NEXT(s->selected_entry, entry);
1778 else
1779 entry = TAILQ_PREV(s->selected_entry,
1780 commit_queue_head, entry);
1781 } else {
1782 if (view->searching == TOG_SEARCH_FORWARD)
1783 entry = TAILQ_FIRST(&s->commits.head);
1784 else
1785 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
1788 while (1) {
1789 char *id_str;
1790 if (entry == NULL) {
1791 if (s->thread_args.log_complete ||
1792 view->searching == TOG_SEARCH_BACKWARD) {
1793 view->search_next_done = 1;
1794 return NULL;
1797 * Poke the log thread for more commits and return,
1798 * allowing the main loop to make progress. Search
1799 * will resume at s->search_entry once we come back.
1801 s->thread_args.commits_needed++;
1802 return trigger_log_thread(1,
1803 &s->thread_args.commits_needed,
1804 &s->thread_args.log_complete,
1805 &s->thread_args.need_commits);
1808 err = got_object_id_str(&id_str, entry->id);
1809 if (err)
1810 return err;
1812 if (match_commit(entry->commit, id_str, &view->regex)) {
1813 view->search_next_done = 1;
1814 s->matched_entry = entry;
1815 free(id_str);
1816 break;
1818 free(id_str);
1819 s->search_entry = entry;
1820 if (view->searching == TOG_SEARCH_FORWARD)
1821 entry = TAILQ_NEXT(entry, entry);
1822 else
1823 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1826 if (s->matched_entry) {
1827 int cur = s->selected_entry->idx;
1828 while (cur < s->matched_entry->idx) {
1829 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
1830 if (err)
1831 return err;
1832 cur++;
1834 while (cur > s->matched_entry->idx) {
1835 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
1836 if (err)
1837 return err;
1838 cur--;
1842 s->search_entry = NULL;
1844 return NULL;
1847 static const struct got_error *
1848 open_log_view(struct tog_view *view, struct got_object_id *start_id,
1849 struct got_reflist_head *refs, struct got_repository *repo,
1850 const char *head_ref_name, const char *path, int check_disk)
1852 const struct got_error *err = NULL;
1853 struct tog_log_view_state *s = &view->state.log;
1854 struct got_repository *thread_repo = NULL;
1855 struct got_commit_graph *thread_graph = NULL;
1856 int errcode;
1858 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
1859 if (err != NULL)
1860 goto done;
1862 /* The commit queue only contains commits being displayed. */
1863 TAILQ_INIT(&s->commits.head);
1864 s->commits.ncommits = 0;
1866 s->refs = refs;
1867 s->repo = repo;
1868 s->head_ref_name = head_ref_name;
1869 s->start_id = got_object_id_dup(start_id);
1870 if (s->start_id == NULL) {
1871 err = got_error_from_errno("got_object_id_dup");
1872 goto done;
1875 view->show = show_log_view;
1876 view->input = input_log_view;
1877 view->close = close_log_view;
1878 view->search_start = search_start_log_view;
1879 view->search_next = search_next_log_view;
1881 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
1882 if (err)
1883 goto done;
1884 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
1885 0, thread_repo);
1886 if (err)
1887 goto done;
1889 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
1890 if (errcode) {
1891 err = got_error_set_errno(errcode, "pthread_cond_init");
1892 goto done;
1895 s->thread_args.commits_needed = view->nlines;
1896 s->thread_args.graph = thread_graph;
1897 s->thread_args.commits = &s->commits;
1898 s->thread_args.in_repo_path = s->in_repo_path;
1899 s->thread_args.start_id = s->start_id;
1900 s->thread_args.repo = thread_repo;
1901 s->thread_args.log_complete = 0;
1902 s->thread_args.quit = &s->quit;
1903 s->thread_args.view = view;
1904 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
1905 s->thread_args.selected_entry = &s->selected_entry;
1906 done:
1907 if (err)
1908 close_log_view(view);
1909 return err;
1912 static const struct got_error *
1913 show_log_view(struct tog_view *view)
1915 struct tog_log_view_state *s = &view->state.log;
1917 if (s->thread == NULL) {
1918 int errcode = pthread_create(&s->thread, NULL, log_thread,
1919 &s->thread_args);
1920 if (errcode)
1921 return got_error_set_errno(errcode, "pthread_create");
1924 return draw_commits(view, &s->last_displayed_entry,
1925 &s->selected_entry, s->first_displayed_entry,
1926 &s->commits, s->selected, view->nlines, s->refs,
1927 s->in_repo_path, s->thread_args.commits_needed);
1930 static const struct got_error *
1931 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
1932 struct tog_view **focus_view, struct tog_view *view, int ch)
1934 const struct got_error *err = NULL;
1935 struct tog_log_view_state *s = &view->state.log;
1936 char *parent_path, *in_repo_path = NULL;
1937 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
1938 int begin_x = 0;
1939 struct got_object_id *start_id;
1941 switch (ch) {
1942 case 'q':
1943 s->quit = 1;
1944 break;
1945 case 'k':
1946 case KEY_UP:
1947 case '<':
1948 case ',':
1949 if (s->first_displayed_entry == NULL)
1950 break;
1951 if (s->selected > 0)
1952 s->selected--;
1953 else
1954 scroll_up(view, &s->first_displayed_entry, 1,
1955 &s->commits);
1956 break;
1957 case KEY_PPAGE:
1958 case CTRL('b'):
1959 if (s->first_displayed_entry == NULL)
1960 break;
1961 if (TAILQ_FIRST(&s->commits.head) ==
1962 s->first_displayed_entry) {
1963 s->selected = 0;
1964 break;
1966 scroll_up(view, &s->first_displayed_entry,
1967 view->nlines, &s->commits);
1968 break;
1969 case 'j':
1970 case KEY_DOWN:
1971 case '>':
1972 case '.':
1973 if (s->first_displayed_entry == NULL)
1974 break;
1975 if (s->selected < MIN(view->nlines - 2,
1976 s->commits.ncommits - 1)) {
1977 s->selected++;
1978 break;
1980 err = scroll_down(view, &s->first_displayed_entry, 1,
1981 &s->last_displayed_entry, &s->commits,
1982 &s->thread_args.log_complete,
1983 &s->thread_args.commits_needed,
1984 &s->thread_args.need_commits);
1985 break;
1986 case KEY_NPAGE:
1987 case CTRL('f'): {
1988 struct commit_queue_entry *first;
1989 first = s->first_displayed_entry;
1990 if (first == NULL)
1991 break;
1992 err = scroll_down(view, &s->first_displayed_entry,
1993 view->nlines, &s->last_displayed_entry,
1994 &s->commits, &s->thread_args.log_complete,
1995 &s->thread_args.commits_needed,
1996 &s->thread_args.need_commits);
1997 if (first == s->first_displayed_entry &&
1998 s->selected < MIN(view->nlines - 2,
1999 s->commits.ncommits - 1)) {
2000 /* can't scroll further down */
2001 s->selected = MIN(view->nlines - 2,
2002 s->commits.ncommits - 1);
2004 err = NULL;
2005 break;
2007 case KEY_RESIZE:
2008 if (s->selected > view->nlines - 2)
2009 s->selected = view->nlines - 2;
2010 if (s->selected > s->commits.ncommits - 1)
2011 s->selected = s->commits.ncommits - 1;
2012 break;
2013 case KEY_ENTER:
2014 case ' ':
2015 case '\r':
2016 if (s->selected_entry == NULL)
2017 break;
2018 if (view_is_parent_view(view))
2019 begin_x = view_split_begin_x(view->begin_x);
2020 err = open_diff_view_for_commit(&diff_view, begin_x,
2021 s->selected_entry->commit, s->selected_entry->id,
2022 view, s->refs, s->repo);
2023 if (err)
2024 break;
2025 if (view_is_parent_view(view)) {
2026 err = view_close_child(view);
2027 if (err)
2028 return err;
2029 err = view_set_child(view, diff_view);
2030 if (err) {
2031 view_close(diff_view);
2032 break;
2034 *focus_view = diff_view;
2035 view->child_focussed = 1;
2036 } else
2037 *new_view = diff_view;
2038 break;
2039 case 't':
2040 if (s->selected_entry == NULL)
2041 break;
2042 if (view_is_parent_view(view))
2043 begin_x = view_split_begin_x(view->begin_x);
2044 err = browse_commit_tree(&tree_view, begin_x,
2045 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2046 if (err)
2047 break;
2048 if (view_is_parent_view(view)) {
2049 err = view_close_child(view);
2050 if (err)
2051 return err;
2052 err = view_set_child(view, tree_view);
2053 if (err) {
2054 view_close(tree_view);
2055 break;
2057 *focus_view = tree_view;
2058 view->child_focussed = 1;
2059 } else
2060 *new_view = tree_view;
2061 break;
2062 case KEY_BACKSPACE:
2063 if (strcmp(s->in_repo_path, "/") == 0)
2064 break;
2065 parent_path = dirname(s->in_repo_path);
2066 if (parent_path && strcmp(parent_path, ".") != 0) {
2067 err = stop_log_thread(s);
2068 if (err)
2069 return err;
2070 lv = view_open(view->nlines, view->ncols,
2071 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2072 if (lv == NULL)
2073 return got_error_from_errno(
2074 "view_open");
2075 err = open_log_view(lv, s->start_id, s->refs,
2076 s->repo, s->head_ref_name, parent_path, 0);
2077 if (err)
2078 return err;;
2079 if (view_is_parent_view(view))
2080 *new_view = lv;
2081 else {
2082 view_set_child(view->parent, lv);
2083 *focus_view = lv;
2085 return NULL;
2087 break;
2088 case CTRL('l'):
2089 err = stop_log_thread(s);
2090 if (err)
2091 return err;
2092 lv = view_open(view->nlines, view->ncols,
2093 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2094 if (lv == NULL)
2095 return got_error_from_errno("view_open");
2096 err = get_head_commit_id(&start_id, s->head_ref_name ?
2097 s->head_ref_name : GOT_REF_HEAD, s->repo);
2098 if (err) {
2099 view_close(lv);
2100 return err;
2102 in_repo_path = strdup(s->in_repo_path);
2103 if (in_repo_path == NULL) {
2104 free(start_id);
2105 view_close(lv);
2106 return got_error_from_errno("strdup");
2108 err = open_log_view(lv, start_id, s->refs, s->repo,
2109 s->head_ref_name, in_repo_path, 0);
2110 if (err) {
2111 free(start_id);
2112 view_close(lv);
2113 return err;;
2115 *dead_view = view;
2116 *new_view = lv;
2117 break;
2118 default:
2119 break;
2122 return err;
2125 static const struct got_error *
2126 apply_unveil(const char *repo_path, const char *worktree_path)
2128 const struct got_error *error;
2130 #ifdef PROFILE
2131 if (unveil("gmon.out", "rwc") != 0)
2132 return got_error_from_errno2("unveil", "gmon.out");
2133 #endif
2134 if (repo_path && unveil(repo_path, "r") != 0)
2135 return got_error_from_errno2("unveil", repo_path);
2137 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2138 return got_error_from_errno2("unveil", worktree_path);
2140 if (unveil("/tmp", "rwc") != 0)
2141 return got_error_from_errno2("unveil", "/tmp");
2143 error = got_privsep_unveil_exec_helpers();
2144 if (error != NULL)
2145 return error;
2147 if (unveil(NULL, NULL) != 0)
2148 return got_error_from_errno("unveil");
2150 return NULL;
2153 static void
2154 init_curses(void)
2156 initscr();
2157 cbreak();
2158 halfdelay(1); /* Do fast refresh while initial view is loading. */
2159 noecho();
2160 nonl();
2161 intrflush(stdscr, FALSE);
2162 keypad(stdscr, TRUE);
2163 curs_set(0);
2164 signal(SIGWINCH, tog_sigwinch);
2165 signal(SIGPIPE, tog_sigpipe);
2168 static const struct got_error *
2169 cmd_log(int argc, char *argv[])
2171 const struct got_error *error;
2172 struct got_repository *repo = NULL;
2173 struct got_worktree *worktree = NULL;
2174 struct got_reflist_head refs;
2175 struct got_object_id *start_id = NULL;
2176 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2177 char *start_commit = NULL;
2178 int ch;
2179 struct tog_view *view;
2181 SIMPLEQ_INIT(&refs);
2183 #ifndef PROFILE
2184 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2185 NULL) == -1)
2186 err(1, "pledge");
2187 #endif
2189 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2190 switch (ch) {
2191 case 'c':
2192 start_commit = optarg;
2193 break;
2194 case 'r':
2195 repo_path = realpath(optarg, NULL);
2196 if (repo_path == NULL)
2197 err(1, "-r option");
2198 break;
2199 default:
2200 usage_log();
2201 /* NOTREACHED */
2205 argc -= optind;
2206 argv += optind;
2208 cwd = getcwd(NULL, 0);
2209 if (cwd == NULL) {
2210 error = got_error_from_errno("getcwd");
2211 goto done;
2213 error = got_worktree_open(&worktree, cwd);
2214 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2215 goto done;
2216 error = NULL;
2218 if (argc == 0) {
2219 path = strdup("");
2220 if (path == NULL) {
2221 error = got_error_from_errno("strdup");
2222 goto done;
2224 } else if (argc == 1) {
2225 if (worktree) {
2226 error = got_worktree_resolve_path(&path, worktree,
2227 argv[0]);
2228 if (error)
2229 goto done;
2230 } else {
2231 path = strdup(argv[0]);
2232 if (path == NULL) {
2233 error = got_error_from_errno("strdup");
2234 goto done;
2237 } else
2238 usage_log();
2240 if (repo_path == NULL) {
2241 if (worktree)
2242 repo_path = strdup(
2243 got_worktree_get_repo_path(worktree));
2244 else
2245 repo_path = strdup(cwd);
2247 if (repo_path == NULL) {
2248 error = got_error_from_errno("strdup");
2249 goto done;
2252 init_curses();
2254 error = got_repo_open(&repo, repo_path);
2255 if (error != NULL)
2256 goto done;
2258 error = apply_unveil(got_repo_get_path(repo),
2259 worktree ? got_worktree_get_root_path(worktree) : NULL);
2260 if (error)
2261 goto done;
2263 if (start_commit == NULL)
2264 error = get_head_commit_id(&start_id, worktree ?
2265 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2266 repo);
2267 else {
2268 error = get_head_commit_id(&start_id, start_commit, repo);
2269 if (error) {
2270 if (error->code != GOT_ERR_NOT_REF)
2271 goto done;
2272 error = got_repo_match_object_id_prefix(&start_id,
2273 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2276 if (error != NULL)
2277 goto done;
2279 error = got_ref_list(&refs, repo);
2280 if (error)
2281 goto done;
2283 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2284 if (view == NULL) {
2285 error = got_error_from_errno("view_open");
2286 goto done;
2288 error = open_log_view(view, start_id, &refs, repo, worktree ?
2289 got_worktree_get_head_ref_name(worktree) : NULL, path, 1);
2290 if (error)
2291 goto done;
2292 error = view_loop(view);
2293 done:
2294 free(repo_path);
2295 free(cwd);
2296 free(path);
2297 free(start_id);
2298 if (repo)
2299 got_repo_close(repo);
2300 if (worktree)
2301 got_worktree_close(worktree);
2302 got_ref_list_free(&refs);
2303 return error;
2306 __dead static void
2307 usage_diff(void)
2309 endwin();
2310 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2311 getprogname());
2312 exit(1);
2315 static char *
2316 parse_next_line(FILE *f, size_t *len)
2318 char *line;
2319 size_t linelen;
2320 size_t lineno;
2321 const char delim[3] = { '\0', '\0', '\0'};
2323 line = fparseln(f, &linelen, &lineno, delim, 0);
2324 if (len)
2325 *len = linelen;
2326 return line;
2329 static const struct got_error *
2330 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2331 int *last_displayed_line, int *eof, int max_lines,
2332 char *header)
2334 const struct got_error *err;
2335 int nlines = 0, nprinted = 0;
2336 char *line;
2337 size_t len;
2338 wchar_t *wline;
2339 int width;
2341 rewind(f);
2342 werase(view->window);
2344 if (header) {
2345 err = format_line(&wline, &width, header, view->ncols);
2346 if (err) {
2347 return err;
2350 if (view_needs_focus_indication(view))
2351 wstandout(view->window);
2352 waddwstr(view->window, wline);
2353 if (view_needs_focus_indication(view))
2354 wstandend(view->window);
2355 if (width < view->ncols - 1)
2356 waddch(view->window, '\n');
2358 if (max_lines <= 1)
2359 return NULL;
2360 max_lines--;
2363 *eof = 0;
2364 while (nprinted < max_lines) {
2365 line = parse_next_line(f, &len);
2366 if (line == NULL) {
2367 *eof = 1;
2368 break;
2370 if (++nlines < *first_displayed_line) {
2371 free(line);
2372 continue;
2375 err = format_line(&wline, &width, line, view->ncols);
2376 if (err) {
2377 free(line);
2378 return err;
2380 waddwstr(view->window, wline);
2381 if (width < view->ncols - 1)
2382 waddch(view->window, '\n');
2383 if (++nprinted == 1)
2384 *first_displayed_line = nlines;
2385 free(line);
2386 free(wline);
2387 wline = NULL;
2389 *last_displayed_line = nlines;
2391 view_vborder(view);
2393 if (*eof) {
2394 while (nprinted < view->nlines) {
2395 waddch(view->window, '\n');
2396 nprinted++;
2399 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols);
2400 if (err) {
2401 return err;
2404 wstandout(view->window);
2405 waddwstr(view->window, wline);
2406 wstandend(view->window);
2409 return NULL;
2412 static char *
2413 get_datestr(time_t *time, char *datebuf)
2415 char *p, *s = ctime_r(time, datebuf);
2416 p = strchr(s, '\n');
2417 if (p)
2418 *p = '\0';
2419 return s;
2422 static const struct got_error *
2423 write_commit_info(struct got_object_id *commit_id,
2424 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2426 const struct got_error *err = NULL;
2427 char datebuf[26];
2428 struct got_commit_object *commit;
2429 char *id_str = NULL;
2430 time_t committer_time;
2431 const char *author, *committer;
2432 char *refs_str = NULL;
2434 if (refs) {
2435 err = build_refs_str(&refs_str, refs, commit_id);
2436 if (err)
2437 return err;
2440 err = got_object_open_as_commit(&commit, repo, commit_id);
2441 if (err)
2442 return err;
2444 err = got_object_id_str(&id_str, commit_id);
2445 if (err) {
2446 err = got_error_from_errno("got_object_id_str");
2447 goto done;
2450 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2451 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2452 err = got_error_from_errno("fprintf");
2453 goto done;
2455 if (fprintf(outfile, "from: %s\n",
2456 got_object_commit_get_author(commit)) < 0) {
2457 err = got_error_from_errno("fprintf");
2458 goto done;
2460 committer_time = got_object_commit_get_committer_time(commit);
2461 if (fprintf(outfile, "date: %s UTC\n",
2462 get_datestr(&committer_time, datebuf)) < 0) {
2463 err = got_error_from_errno("fprintf");
2464 goto done;
2466 author = got_object_commit_get_author(commit);
2467 committer = got_object_commit_get_committer(commit);
2468 if (strcmp(author, committer) != 0 &&
2469 fprintf(outfile, "via: %s\n", committer) < 0) {
2470 err = got_error_from_errno("fprintf");
2471 goto done;
2473 if (fprintf(outfile, "%s\n",
2474 got_object_commit_get_logmsg(commit)) < 0) {
2475 err = got_error_from_errno("fprintf");
2476 goto done;
2478 done:
2479 free(id_str);
2480 free(refs_str);
2481 got_object_commit_close(commit);
2482 return err;
2485 static const struct got_error *
2486 create_diff(struct tog_diff_view_state *s)
2488 const struct got_error *err = NULL;
2489 FILE *f = NULL;
2490 int obj_type;
2492 f = got_opentemp();
2493 if (f == NULL) {
2494 err = got_error_from_errno("got_opentemp");
2495 goto done;
2497 if (s->f && fclose(s->f) != 0) {
2498 err = got_error_from_errno("fclose");
2499 goto done;
2501 s->f = f;
2503 if (s->id1)
2504 err = got_object_get_type(&obj_type, s->repo, s->id1);
2505 else
2506 err = got_object_get_type(&obj_type, s->repo, s->id2);
2507 if (err)
2508 goto done;
2510 switch (obj_type) {
2511 case GOT_OBJ_TYPE_BLOB:
2512 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2513 s->diff_context, s->repo, f);
2514 break;
2515 case GOT_OBJ_TYPE_TREE:
2516 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2517 s->diff_context, s->repo, f);
2518 break;
2519 case GOT_OBJ_TYPE_COMMIT: {
2520 const struct got_object_id_queue *parent_ids;
2521 struct got_object_qid *pid;
2522 struct got_commit_object *commit2;
2524 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2525 if (err)
2526 break;
2527 /* Show commit info if we're diffing to a parent/root commit. */
2528 if (s->id1 == NULL)
2529 write_commit_info(s->id2, s->refs, s->repo, f);
2530 else {
2531 parent_ids = got_object_commit_get_parent_ids(commit2);
2532 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2533 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2534 write_commit_info(s->id2, s->refs,
2535 s->repo, f);
2536 break;
2540 got_object_commit_close(commit2);
2542 err = got_diff_objects_as_commits(s->id1, s->id2,
2543 s->diff_context, s->repo, f);
2544 break;
2546 default:
2547 err = got_error(GOT_ERR_OBJ_TYPE);
2548 break;
2550 done:
2551 if (f && fflush(f) != 0 && err == NULL)
2552 err = got_error_from_errno("fflush");
2553 return err;
2556 static void
2557 diff_view_indicate_progress(struct tog_view *view)
2559 mvwaddstr(view->window, 0, 0, "diffing...");
2560 update_panels();
2561 doupdate();
2564 static const struct got_error *
2565 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2566 struct got_object_id *id2, struct tog_view *log_view,
2567 struct got_reflist_head *refs, struct got_repository *repo)
2569 const struct got_error *err;
2571 if (id1 != NULL && id2 != NULL) {
2572 int type1, type2;
2573 err = got_object_get_type(&type1, repo, id1);
2574 if (err)
2575 return err;
2576 err = got_object_get_type(&type2, repo, id2);
2577 if (err)
2578 return err;
2580 if (type1 != type2)
2581 return got_error(GOT_ERR_OBJ_TYPE);
2584 if (id1) {
2585 view->state.diff.id1 = got_object_id_dup(id1);
2586 if (view->state.diff.id1 == NULL)
2587 return got_error_from_errno("got_object_id_dup");
2588 } else
2589 view->state.diff.id1 = NULL;
2591 view->state.diff.id2 = got_object_id_dup(id2);
2592 if (view->state.diff.id2 == NULL) {
2593 free(view->state.diff.id1);
2594 view->state.diff.id1 = NULL;
2595 return got_error_from_errno("got_object_id_dup");
2597 view->state.diff.f = NULL;
2598 view->state.diff.first_displayed_line = 1;
2599 view->state.diff.last_displayed_line = view->nlines;
2600 view->state.diff.diff_context = 3;
2601 view->state.diff.log_view = log_view;
2602 view->state.diff.repo = repo;
2603 view->state.diff.refs = refs;
2605 if (log_view && view_is_splitscreen(view))
2606 show_log_view(log_view); /* draw vborder */
2607 diff_view_indicate_progress(view);
2609 err = create_diff(&view->state.diff);
2610 if (err) {
2611 free(view->state.diff.id1);
2612 view->state.diff.id1 = NULL;
2613 free(view->state.diff.id2);
2614 view->state.diff.id2 = NULL;
2615 return err;
2618 view->show = show_diff_view;
2619 view->input = input_diff_view;
2620 view->close = close_diff_view;
2622 return NULL;
2625 static const struct got_error *
2626 close_diff_view(struct tog_view *view)
2628 const struct got_error *err = NULL;
2630 free(view->state.diff.id1);
2631 view->state.diff.id1 = NULL;
2632 free(view->state.diff.id2);
2633 view->state.diff.id2 = NULL;
2634 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
2635 err = got_error_from_errno("fclose");
2636 return err;
2639 static const struct got_error *
2640 show_diff_view(struct tog_view *view)
2642 const struct got_error *err;
2643 struct tog_diff_view_state *s = &view->state.diff;
2644 char *id_str1 = NULL, *id_str2, *header;
2646 if (s->id1) {
2647 err = got_object_id_str(&id_str1, s->id1);
2648 if (err)
2649 return err;
2651 err = got_object_id_str(&id_str2, s->id2);
2652 if (err)
2653 return err;
2655 if (asprintf(&header, "diff %s %s",
2656 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
2657 err = got_error_from_errno("asprintf");
2658 free(id_str1);
2659 free(id_str2);
2660 return err;
2662 free(id_str1);
2663 free(id_str2);
2665 return draw_file(view, s->f, &s->first_displayed_line,
2666 &s->last_displayed_line, &s->eof, view->nlines,
2667 header);
2670 static const struct got_error *
2671 set_selected_commit(struct tog_diff_view_state *s,
2672 struct commit_queue_entry *entry)
2674 const struct got_error *err;
2675 const struct got_object_id_queue *parent_ids;
2676 struct got_commit_object *selected_commit;
2677 struct got_object_qid *pid;
2679 free(s->id2);
2680 s->id2 = got_object_id_dup(entry->id);
2681 if (s->id2 == NULL)
2682 return got_error_from_errno("got_object_id_dup");
2684 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
2685 if (err)
2686 return err;
2687 parent_ids = got_object_commit_get_parent_ids(selected_commit);
2688 free(s->id1);
2689 pid = SIMPLEQ_FIRST(parent_ids);
2690 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
2691 got_object_commit_close(selected_commit);
2692 return NULL;
2695 static const struct got_error *
2696 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
2697 struct tog_view **focus_view, struct tog_view *view, int ch)
2699 const struct got_error *err = NULL;
2700 struct tog_diff_view_state *s = &view->state.diff;
2701 struct tog_log_view_state *ls;
2702 struct commit_queue_entry *entry;
2703 int i;
2705 switch (ch) {
2706 case 'k':
2707 case KEY_UP:
2708 if (s->first_displayed_line > 1)
2709 s->first_displayed_line--;
2710 break;
2711 case KEY_PPAGE:
2712 case CTRL('b'):
2713 if (s->first_displayed_line == 1)
2714 break;
2715 i = 0;
2716 while (i++ < view->nlines - 1 &&
2717 s->first_displayed_line > 1)
2718 s->first_displayed_line--;
2719 break;
2720 case 'j':
2721 case KEY_DOWN:
2722 if (!s->eof)
2723 s->first_displayed_line++;
2724 break;
2725 case KEY_NPAGE:
2726 case CTRL('f'):
2727 case ' ':
2728 if (s->eof)
2729 break;
2730 i = 0;
2731 while (!s->eof && i++ < view->nlines - 1) {
2732 char *line;
2733 line = parse_next_line(s->f, NULL);
2734 s->first_displayed_line++;
2735 if (line == NULL)
2736 break;
2738 break;
2739 case '[':
2740 if (s->diff_context > 0) {
2741 s->diff_context--;
2742 diff_view_indicate_progress(view);
2743 err = create_diff(s);
2745 break;
2746 case ']':
2747 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
2748 s->diff_context++;
2749 diff_view_indicate_progress(view);
2750 err = create_diff(s);
2752 break;
2753 case '<':
2754 case ',':
2755 if (s->log_view == NULL)
2756 break;
2757 ls = &s->log_view->state.log;
2758 entry = TAILQ_PREV(ls->selected_entry,
2759 commit_queue_head, entry);
2760 if (entry == NULL)
2761 break;
2763 err = input_log_view(NULL, NULL, NULL, s->log_view,
2764 KEY_UP);
2765 if (err)
2766 break;
2768 err = set_selected_commit(s, entry);
2769 if (err)
2770 break;
2772 s->first_displayed_line = 1;
2773 s->last_displayed_line = view->nlines;
2775 diff_view_indicate_progress(view);
2776 err = create_diff(s);
2777 break;
2778 case '>':
2779 case '.':
2780 if (s->log_view == NULL)
2781 break;
2782 ls = &s->log_view->state.log;
2784 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
2785 ls->thread_args.commits_needed++;
2787 /* Display "loading..." in log view. */
2788 show_log_view(s->log_view);
2789 update_panels();
2790 doupdate();
2792 err = trigger_log_thread(1 /* load_all */,
2793 &ls->thread_args.commits_needed,
2794 &ls->thread_args.log_complete,
2795 &ls->thread_args.need_commits);
2796 if (err)
2797 break;
2799 err = input_log_view(NULL, NULL, NULL, s->log_view,
2800 KEY_DOWN);
2801 if (err)
2802 break;
2804 entry = TAILQ_NEXT(ls->selected_entry, entry);
2805 if (entry == NULL)
2806 break;
2808 err = set_selected_commit(s, entry);
2809 if (err)
2810 break;
2812 s->first_displayed_line = 1;
2813 s->last_displayed_line = view->nlines;
2815 diff_view_indicate_progress(view);
2816 err = create_diff(s);
2817 break;
2818 default:
2819 break;
2822 return err;
2825 static const struct got_error *
2826 cmd_diff(int argc, char *argv[])
2828 const struct got_error *error = NULL;
2829 struct got_repository *repo = NULL;
2830 struct got_reflist_head refs;
2831 struct got_object_id *id1 = NULL, *id2 = NULL;
2832 char *repo_path = NULL;
2833 char *id_str1 = NULL, *id_str2 = NULL;
2834 int ch;
2835 struct tog_view *view;
2837 SIMPLEQ_INIT(&refs);
2839 #ifndef PROFILE
2840 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2841 NULL) == -1)
2842 err(1, "pledge");
2843 #endif
2845 while ((ch = getopt(argc, argv, "")) != -1) {
2846 switch (ch) {
2847 default:
2848 usage_diff();
2849 /* NOTREACHED */
2853 argc -= optind;
2854 argv += optind;
2856 if (argc == 0) {
2857 usage_diff(); /* TODO show local worktree changes */
2858 } else if (argc == 2) {
2859 repo_path = getcwd(NULL, 0);
2860 if (repo_path == NULL)
2861 return got_error_from_errno("getcwd");
2862 id_str1 = argv[0];
2863 id_str2 = argv[1];
2864 } else if (argc == 3) {
2865 repo_path = realpath(argv[0], NULL);
2866 if (repo_path == NULL)
2867 return got_error_from_errno2("realpath", argv[0]);
2868 id_str1 = argv[1];
2869 id_str2 = argv[2];
2870 } else
2871 usage_diff();
2873 init_curses();
2875 error = got_repo_open(&repo, repo_path);
2876 if (error)
2877 goto done;
2879 error = apply_unveil(got_repo_get_path(repo), NULL);
2880 if (error)
2881 goto done;
2883 error = got_repo_match_object_id_prefix(&id1, id_str1,
2884 GOT_OBJ_TYPE_ANY, repo);
2885 if (error)
2886 goto done;
2888 error = got_repo_match_object_id_prefix(&id2, id_str2,
2889 GOT_OBJ_TYPE_ANY, repo);
2890 if (error)
2891 goto done;
2893 error = got_ref_list(&refs, repo);
2894 if (error)
2895 goto done;
2897 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
2898 if (view == NULL) {
2899 error = got_error_from_errno("view_open");
2900 goto done;
2902 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
2903 if (error)
2904 goto done;
2905 error = view_loop(view);
2906 done:
2907 free(repo_path);
2908 if (repo)
2909 got_repo_close(repo);
2910 got_ref_list_free(&refs);
2911 return error;
2914 __dead static void
2915 usage_blame(void)
2917 endwin();
2918 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
2919 getprogname());
2920 exit(1);
2923 struct tog_blame_line {
2924 int annotated;
2925 struct got_object_id *id;
2928 static const struct got_error *
2929 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
2930 const char *path, struct tog_blame_line *lines, int nlines,
2931 int blame_complete, int selected_line, int *first_displayed_line,
2932 int *last_displayed_line, int *eof, int max_lines)
2934 const struct got_error *err;
2935 int lineno = 0, nprinted = 0;
2936 char *line;
2937 size_t len;
2938 wchar_t *wline;
2939 int width, wlimit;
2940 struct tog_blame_line *blame_line;
2941 struct got_object_id *prev_id = NULL;
2942 char *id_str;
2944 err = got_object_id_str(&id_str, id);
2945 if (err)
2946 return err;
2948 rewind(f);
2949 werase(view->window);
2951 if (asprintf(&line, "commit %s", id_str) == -1) {
2952 err = got_error_from_errno("asprintf");
2953 free(id_str);
2954 return err;
2957 err = format_line(&wline, &width, line, view->ncols);
2958 free(line);
2959 line = NULL;
2960 if (view_needs_focus_indication(view))
2961 wstandout(view->window);
2962 waddwstr(view->window, wline);
2963 if (view_needs_focus_indication(view))
2964 wstandend(view->window);
2965 free(wline);
2966 wline = NULL;
2967 if (width < view->ncols - 1)
2968 waddch(view->window, '\n');
2970 if (asprintf(&line, "[%d/%d] %s%s",
2971 *first_displayed_line - 1 + selected_line, nlines,
2972 blame_complete ? "" : "annotating... ", path) == -1) {
2973 free(id_str);
2974 return got_error_from_errno("asprintf");
2976 free(id_str);
2977 err = format_line(&wline, &width, line, view->ncols);
2978 free(line);
2979 line = NULL;
2980 if (err)
2981 return err;
2982 waddwstr(view->window, wline);
2983 free(wline);
2984 wline = NULL;
2985 if (width < view->ncols - 1)
2986 waddch(view->window, '\n');
2988 *eof = 0;
2989 while (nprinted < max_lines - 2) {
2990 line = parse_next_line(f, &len);
2991 if (line == NULL) {
2992 *eof = 1;
2993 break;
2995 if (++lineno < *first_displayed_line) {
2996 free(line);
2997 continue;
3000 wlimit = view->ncols < 9 ? 0 : view->ncols - 9;
3001 err = format_line(&wline, &width, line, wlimit);
3002 if (err) {
3003 free(line);
3004 return err;
3007 if (view->focussed && nprinted == selected_line - 1)
3008 wstandout(view->window);
3010 blame_line = &lines[lineno - 1];
3011 if (blame_line->annotated && prev_id &&
3012 got_object_id_cmp(prev_id, blame_line->id) == 0)
3013 waddstr(view->window, " ");
3014 else if (blame_line->annotated) {
3015 char *id_str;
3016 err = got_object_id_str(&id_str, blame_line->id);
3017 if (err) {
3018 free(line);
3019 free(wline);
3020 return err;
3022 wprintw(view->window, "%.8s ", id_str);
3023 free(id_str);
3024 prev_id = blame_line->id;
3025 } else {
3026 waddstr(view->window, "........ ");
3027 prev_id = NULL;
3030 waddwstr(view->window, wline);
3031 while (width < wlimit) {
3032 waddch(view->window, ' ');
3033 width++;
3035 if (view->focussed && nprinted == selected_line - 1)
3036 wstandend(view->window);
3037 if (++nprinted == 1)
3038 *first_displayed_line = lineno;
3039 free(line);
3040 free(wline);
3041 wline = NULL;
3043 *last_displayed_line = lineno;
3045 view_vborder(view);
3047 return NULL;
3050 static const struct got_error *
3051 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3053 const struct got_error *err = NULL;
3054 struct tog_blame_cb_args *a = arg;
3055 struct tog_blame_line *line;
3056 int errcode;
3058 if (nlines != a->nlines ||
3059 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3060 return got_error(GOT_ERR_RANGE);
3062 errcode = pthread_mutex_lock(&tog_mutex);
3063 if (errcode)
3064 return got_error_set_errno(errcode, "pthread_mutex_lock");
3066 if (*a->quit) { /* user has quit the blame view */
3067 err = got_error(GOT_ERR_ITER_COMPLETED);
3068 goto done;
3071 if (lineno == -1)
3072 goto done; /* no change in this commit */
3074 line = &a->lines[lineno - 1];
3075 if (line->annotated)
3076 goto done;
3078 line->id = got_object_id_dup(id);
3079 if (line->id == NULL) {
3080 err = got_error_from_errno("got_object_id_dup");
3081 goto done;
3083 line->annotated = 1;
3084 done:
3085 errcode = pthread_mutex_unlock(&tog_mutex);
3086 if (errcode)
3087 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3088 return err;
3091 static void *
3092 blame_thread(void *arg)
3094 const struct got_error *err;
3095 struct tog_blame_thread_args *ta = arg;
3096 struct tog_blame_cb_args *a = ta->cb_args;
3097 int errcode;
3099 err = got_blame_incremental(ta->path, a->commit_id, ta->repo,
3100 blame_cb, ta->cb_args);
3102 errcode = pthread_mutex_lock(&tog_mutex);
3103 if (errcode)
3104 return (void *)got_error_set_errno(errcode,
3105 "pthread_mutex_lock");
3107 got_repo_close(ta->repo);
3108 ta->repo = NULL;
3109 *ta->complete = 1;
3111 errcode = pthread_mutex_unlock(&tog_mutex);
3112 if (errcode && err == NULL)
3113 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3115 return (void *)err;
3118 static struct got_object_id *
3119 get_selected_commit_id(struct tog_blame_line *lines, int first_displayed_line,
3120 int selected_line)
3122 struct tog_blame_line *line;
3124 line = &lines[first_displayed_line - 1 + selected_line - 1];
3125 if (!line->annotated)
3126 return NULL;
3128 return line->id;
3131 static const struct got_error *
3132 stop_blame(struct tog_blame *blame)
3134 const struct got_error *err = NULL;
3135 int i;
3137 if (blame->thread) {
3138 int errcode;
3139 errcode = pthread_mutex_unlock(&tog_mutex);
3140 if (errcode)
3141 return got_error_set_errno(errcode,
3142 "pthread_mutex_unlock");
3143 errcode = pthread_join(blame->thread, (void **)&err);
3144 if (errcode)
3145 return got_error_set_errno(errcode, "pthread_join");
3146 errcode = pthread_mutex_lock(&tog_mutex);
3147 if (errcode)
3148 return got_error_set_errno(errcode,
3149 "pthread_mutex_lock");
3150 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3151 err = NULL;
3152 blame->thread = NULL;
3154 if (blame->thread_args.repo) {
3155 got_repo_close(blame->thread_args.repo);
3156 blame->thread_args.repo = NULL;
3158 if (blame->f) {
3159 if (fclose(blame->f) != 0 && err == NULL)
3160 err = got_error_from_errno("fclose");
3161 blame->f = NULL;
3163 if (blame->lines) {
3164 for (i = 0; i < blame->nlines; i++)
3165 free(blame->lines[i].id);
3166 free(blame->lines);
3167 blame->lines = NULL;
3169 free(blame->cb_args.commit_id);
3170 blame->cb_args.commit_id = NULL;
3172 return err;
3175 static const struct got_error *
3176 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3177 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3178 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3179 struct got_repository *repo)
3181 const struct got_error *err = NULL;
3182 struct got_blob_object *blob = NULL;
3183 struct got_repository *thread_repo = NULL;
3184 struct got_object_id *obj_id = NULL;
3185 int obj_type;
3187 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3188 if (err)
3189 return err;
3190 if (obj_id == NULL)
3191 return got_error(GOT_ERR_NO_OBJ);
3193 err = got_object_get_type(&obj_type, repo, obj_id);
3194 if (err)
3195 goto done;
3197 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3198 err = got_error(GOT_ERR_OBJ_TYPE);
3199 goto done;
3202 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3203 if (err)
3204 goto done;
3205 blame->f = got_opentemp();
3206 if (blame->f == NULL) {
3207 err = got_error_from_errno("got_opentemp");
3208 goto done;
3210 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3211 &blame->line_offsets, blame->f, blob);
3212 if (err)
3213 goto done;
3215 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3216 if (blame->lines == NULL) {
3217 err = got_error_from_errno("calloc");
3218 goto done;
3221 err = got_repo_open(&thread_repo, got_repo_get_path(repo));
3222 if (err)
3223 goto done;
3225 blame->cb_args.view = view;
3226 blame->cb_args.lines = blame->lines;
3227 blame->cb_args.nlines = blame->nlines;
3228 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3229 if (blame->cb_args.commit_id == NULL) {
3230 err = got_error_from_errno("got_object_id_dup");
3231 goto done;
3233 blame->cb_args.quit = done;
3235 blame->thread_args.path = path;
3236 blame->thread_args.repo = thread_repo;
3237 blame->thread_args.cb_args = &blame->cb_args;
3238 blame->thread_args.complete = blame_complete;
3239 *blame_complete = 0;
3241 done:
3242 if (blob)
3243 got_object_blob_close(blob);
3244 free(obj_id);
3245 if (err)
3246 stop_blame(blame);
3247 return err;
3250 static const struct got_error *
3251 open_blame_view(struct tog_view *view, char *path,
3252 struct got_object_id *commit_id, struct got_reflist_head *refs,
3253 struct got_repository *repo)
3255 const struct got_error *err = NULL;
3256 struct tog_blame_view_state *s = &view->state.blame;
3258 SIMPLEQ_INIT(&s->blamed_commits);
3260 s->path = strdup(path);
3261 if (s->path == NULL)
3262 return got_error_from_errno("strdup");
3264 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3265 if (err) {
3266 free(s->path);
3267 return err;
3270 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3271 s->first_displayed_line = 1;
3272 s->last_displayed_line = view->nlines;
3273 s->selected_line = 1;
3274 s->blame_complete = 0;
3275 s->repo = repo;
3276 s->refs = refs;
3277 s->commit_id = commit_id;
3278 memset(&s->blame, 0, sizeof(s->blame));
3280 view->show = show_blame_view;
3281 view->input = input_blame_view;
3282 view->close = close_blame_view;
3283 view->search_start = search_start_blame_view;
3284 view->search_next = search_next_blame_view;
3286 return run_blame(&s->blame, view, &s->blame_complete,
3287 &s->first_displayed_line, &s->last_displayed_line,
3288 &s->selected_line, &s->done, &s->eof, s->path,
3289 s->blamed_commit->id, s->repo);
3292 static const struct got_error *
3293 close_blame_view(struct tog_view *view)
3295 const struct got_error *err = NULL;
3296 struct tog_blame_view_state *s = &view->state.blame;
3298 if (s->blame.thread)
3299 err = stop_blame(&s->blame);
3301 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3302 struct got_object_qid *blamed_commit;
3303 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3304 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3305 got_object_qid_free(blamed_commit);
3308 free(s->path);
3310 return err;
3313 static const struct got_error *
3314 search_start_blame_view(struct tog_view *view)
3316 struct tog_blame_view_state *s = &view->state.blame;
3318 s->matched_line = 0;
3319 return NULL;
3322 static int
3323 match_line(const char *line, regex_t *regex)
3325 regmatch_t regmatch;
3327 return regexec(regex, line, 1, &regmatch, 0) == 0;
3331 static const struct got_error *
3332 search_next_blame_view(struct tog_view *view)
3334 struct tog_blame_view_state *s = &view->state.blame;
3335 int lineno;
3337 if (!view->searching) {
3338 view->search_next_done = 1;
3339 return NULL;
3342 if (s->matched_line) {
3343 if (view->searching == TOG_SEARCH_FORWARD)
3344 lineno = s->matched_line + 1;
3345 else
3346 lineno = s->matched_line - 1;
3347 } else {
3348 if (view->searching == TOG_SEARCH_FORWARD)
3349 lineno = 1;
3350 else
3351 lineno = s->blame.nlines;
3354 while (1) {
3355 char *line = NULL;
3356 off_t offset;
3357 size_t len;
3359 if (lineno <= 0 || lineno > s->blame.nlines) {
3360 if (s->matched_line == 0) {
3361 view->search_next_done = 1;
3362 free(line);
3363 break;
3366 if (view->searching == TOG_SEARCH_FORWARD)
3367 lineno = 1;
3368 else
3369 lineno = s->blame.nlines;
3372 offset = s->blame.line_offsets[lineno - 1];
3373 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3374 free(line);
3375 return got_error_from_errno("fseeko");
3377 free(line);
3378 line = parse_next_line(s->blame.f, &len);
3379 if (line && match_line(line, &view->regex)) {
3380 view->search_next_done = 1;
3381 s->matched_line = lineno;
3382 free(line);
3383 break;
3385 free(line);
3386 if (view->searching == TOG_SEARCH_FORWARD)
3387 lineno++;
3388 else
3389 lineno--;
3392 if (s->matched_line) {
3393 s->first_displayed_line = s->matched_line;
3394 s->selected_line = 1;
3397 return NULL;
3400 static const struct got_error *
3401 show_blame_view(struct tog_view *view)
3403 const struct got_error *err = NULL;
3404 struct tog_blame_view_state *s = &view->state.blame;
3405 int errcode;
3407 if (s->blame.thread == NULL) {
3408 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3409 &s->blame.thread_args);
3410 if (errcode)
3411 return got_error_set_errno(errcode, "pthread_create");
3414 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3415 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3416 s->selected_line, &s->first_displayed_line,
3417 &s->last_displayed_line, &s->eof, view->nlines);
3419 view_vborder(view);
3420 return err;
3423 static const struct got_error *
3424 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3425 struct tog_view **focus_view, struct tog_view *view, int ch)
3427 const struct got_error *err = NULL, *thread_err = NULL;
3428 struct tog_view *diff_view;
3429 struct tog_blame_view_state *s = &view->state.blame;
3430 int begin_x = 0;
3432 switch (ch) {
3433 case 'q':
3434 s->done = 1;
3435 break;
3436 case 'k':
3437 case KEY_UP:
3438 if (s->selected_line > 1)
3439 s->selected_line--;
3440 else if (s->selected_line == 1 &&
3441 s->first_displayed_line > 1)
3442 s->first_displayed_line--;
3443 break;
3444 case KEY_PPAGE:
3445 if (s->first_displayed_line == 1) {
3446 s->selected_line = 1;
3447 break;
3449 if (s->first_displayed_line > view->nlines - 2)
3450 s->first_displayed_line -=
3451 (view->nlines - 2);
3452 else
3453 s->first_displayed_line = 1;
3454 break;
3455 case 'j':
3456 case KEY_DOWN:
3457 if (s->selected_line < view->nlines - 2 &&
3458 s->first_displayed_line +
3459 s->selected_line <= s->blame.nlines)
3460 s->selected_line++;
3461 else if (s->last_displayed_line <
3462 s->blame.nlines)
3463 s->first_displayed_line++;
3464 break;
3465 case 'b':
3466 case 'p': {
3467 struct got_object_id *id = NULL;
3468 id = get_selected_commit_id(s->blame.lines,
3469 s->first_displayed_line, s->selected_line);
3470 if (id == NULL)
3471 break;
3472 if (ch == 'p') {
3473 struct got_commit_object *commit;
3474 struct got_object_qid *pid;
3475 struct got_object_id *blob_id = NULL;
3476 int obj_type;
3477 err = got_object_open_as_commit(&commit,
3478 s->repo, id);
3479 if (err)
3480 break;
3481 pid = SIMPLEQ_FIRST(
3482 got_object_commit_get_parent_ids(commit));
3483 if (pid == NULL) {
3484 got_object_commit_close(commit);
3485 break;
3487 /* Check if path history ends here. */
3488 err = got_object_id_by_path(&blob_id, s->repo,
3489 pid->id, s->path);
3490 if (err) {
3491 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3492 err = NULL;
3493 got_object_commit_close(commit);
3494 break;
3496 err = got_object_get_type(&obj_type, s->repo,
3497 blob_id);
3498 free(blob_id);
3499 /* Can't blame non-blob type objects. */
3500 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3501 got_object_commit_close(commit);
3502 break;
3504 err = got_object_qid_alloc(&s->blamed_commit,
3505 pid->id);
3506 got_object_commit_close(commit);
3507 } else {
3508 if (got_object_id_cmp(id,
3509 s->blamed_commit->id) == 0)
3510 break;
3511 err = got_object_qid_alloc(&s->blamed_commit,
3512 id);
3514 if (err)
3515 break;
3516 s->done = 1;
3517 thread_err = stop_blame(&s->blame);
3518 s->done = 0;
3519 if (thread_err)
3520 break;
3521 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3522 s->blamed_commit, entry);
3523 err = run_blame(&s->blame, view, &s->blame_complete,
3524 &s->first_displayed_line, &s->last_displayed_line,
3525 &s->selected_line, &s->done, &s->eof,
3526 s->path, s->blamed_commit->id, s->repo);
3527 if (err)
3528 break;
3529 break;
3531 case 'B': {
3532 struct got_object_qid *first;
3533 first = SIMPLEQ_FIRST(&s->blamed_commits);
3534 if (!got_object_id_cmp(first->id, s->commit_id))
3535 break;
3536 s->done = 1;
3537 thread_err = stop_blame(&s->blame);
3538 s->done = 0;
3539 if (thread_err)
3540 break;
3541 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3542 got_object_qid_free(s->blamed_commit);
3543 s->blamed_commit =
3544 SIMPLEQ_FIRST(&s->blamed_commits);
3545 err = run_blame(&s->blame, view, &s->blame_complete,
3546 &s->first_displayed_line, &s->last_displayed_line,
3547 &s->selected_line, &s->done, &s->eof, s->path,
3548 s->blamed_commit->id, s->repo);
3549 if (err)
3550 break;
3551 break;
3553 case KEY_ENTER:
3554 case '\r': {
3555 struct got_object_id *id = NULL;
3556 struct got_object_qid *pid;
3557 struct got_commit_object *commit = NULL;
3558 id = get_selected_commit_id(s->blame.lines,
3559 s->first_displayed_line, s->selected_line);
3560 if (id == NULL)
3561 break;
3562 err = got_object_open_as_commit(&commit, s->repo, id);
3563 if (err)
3564 break;
3565 pid = SIMPLEQ_FIRST(
3566 got_object_commit_get_parent_ids(commit));
3567 if (view_is_parent_view(view))
3568 begin_x = view_split_begin_x(view->begin_x);
3569 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
3570 if (diff_view == NULL) {
3571 got_object_commit_close(commit);
3572 err = got_error_from_errno("view_open");
3573 break;
3575 err = open_diff_view(diff_view, pid ? pid->id : NULL,
3576 id, NULL, s->refs, s->repo);
3577 got_object_commit_close(commit);
3578 if (err) {
3579 view_close(diff_view);
3580 break;
3582 if (view_is_parent_view(view)) {
3583 err = view_close_child(view);
3584 if (err)
3585 break;
3586 err = view_set_child(view, diff_view);
3587 if (err) {
3588 view_close(diff_view);
3589 break;
3591 *focus_view = diff_view;
3592 view->child_focussed = 1;
3593 } else
3594 *new_view = diff_view;
3595 if (err)
3596 break;
3597 break;
3599 case KEY_NPAGE:
3600 case ' ':
3601 if (s->last_displayed_line >= s->blame.nlines &&
3602 s->selected_line >= MIN(s->blame.nlines,
3603 view->nlines - 2)) {
3604 break;
3606 if (s->last_displayed_line >= s->blame.nlines &&
3607 s->selected_line < view->nlines - 2) {
3608 s->selected_line = MIN(s->blame.nlines,
3609 view->nlines - 2);
3610 break;
3612 if (s->last_displayed_line + view->nlines - 2
3613 <= s->blame.nlines)
3614 s->first_displayed_line +=
3615 view->nlines - 2;
3616 else
3617 s->first_displayed_line =
3618 s->blame.nlines -
3619 (view->nlines - 3);
3620 break;
3621 case KEY_RESIZE:
3622 if (s->selected_line > view->nlines - 2) {
3623 s->selected_line = MIN(s->blame.nlines,
3624 view->nlines - 2);
3626 break;
3627 default:
3628 break;
3630 return thread_err ? thread_err : err;
3633 static const struct got_error *
3634 cmd_blame(int argc, char *argv[])
3636 const struct got_error *error;
3637 struct got_repository *repo = NULL;
3638 struct got_reflist_head refs;
3639 struct got_worktree *worktree = NULL;
3640 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3641 struct got_object_id *commit_id = NULL;
3642 char *commit_id_str = NULL;
3643 int ch;
3644 struct tog_view *view;
3646 SIMPLEQ_INIT(&refs);
3648 #ifndef PROFILE
3649 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3650 NULL) == -1)
3651 err(1, "pledge");
3652 #endif
3654 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3655 switch (ch) {
3656 case 'c':
3657 commit_id_str = optarg;
3658 break;
3659 case 'r':
3660 repo_path = realpath(optarg, NULL);
3661 if (repo_path == NULL)
3662 err(1, "-r option");
3663 break;
3664 default:
3665 usage_blame();
3666 /* NOTREACHED */
3670 argc -= optind;
3671 argv += optind;
3673 if (argc == 1)
3674 path = argv[0];
3675 else
3676 usage_blame();
3678 cwd = getcwd(NULL, 0);
3679 if (cwd == NULL) {
3680 error = got_error_from_errno("getcwd");
3681 goto done;
3683 if (repo_path == NULL) {
3684 error = got_worktree_open(&worktree, cwd);
3685 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3686 goto done;
3687 else
3688 error = NULL;
3689 if (worktree) {
3690 repo_path =
3691 strdup(got_worktree_get_repo_path(worktree));
3692 if (repo_path == NULL)
3693 error = got_error_from_errno("strdup");
3694 if (error)
3695 goto done;
3696 } else {
3697 repo_path = strdup(cwd);
3698 if (repo_path == NULL) {
3699 error = got_error_from_errno("strdup");
3700 goto done;
3705 init_curses();
3707 error = got_repo_open(&repo, repo_path);
3708 if (error != NULL)
3709 goto done;
3711 error = apply_unveil(got_repo_get_path(repo), NULL);
3712 if (error)
3713 goto done;
3715 if (worktree) {
3716 const char *prefix = got_worktree_get_path_prefix(worktree);
3717 char *p, *worktree_subdir = cwd +
3718 strlen(got_worktree_get_root_path(worktree));
3719 if (asprintf(&p, "%s%s%s%s%s",
3720 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3721 worktree_subdir, worktree_subdir[0] ? "/" : "",
3722 path) == -1) {
3723 error = got_error_from_errno("asprintf");
3724 goto done;
3726 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3727 free(p);
3728 } else {
3729 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3731 if (error)
3732 goto done;
3734 if (commit_id_str == NULL) {
3735 struct got_reference *head_ref;
3736 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3737 if (error != NULL)
3738 goto done;
3739 error = got_ref_resolve(&commit_id, repo, head_ref);
3740 got_ref_close(head_ref);
3741 } else {
3742 error = get_head_commit_id(&commit_id, commit_id_str, repo);
3743 if (error) {
3744 if (error->code != GOT_ERR_NOT_REF)
3745 goto done;
3746 error = got_repo_match_object_id_prefix(&commit_id,
3747 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
3750 if (error != NULL)
3751 goto done;
3753 error = got_ref_list(&refs, repo);
3754 if (error)
3755 goto done;
3757 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
3758 if (view == NULL) {
3759 error = got_error_from_errno("view_open");
3760 goto done;
3762 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
3763 if (error)
3764 goto done;
3765 error = view_loop(view);
3766 done:
3767 free(repo_path);
3768 free(cwd);
3769 free(commit_id);
3770 if (worktree)
3771 got_worktree_close(worktree);
3772 if (repo)
3773 got_repo_close(repo);
3774 got_ref_list_free(&refs);
3775 return error;
3778 static const struct got_error *
3779 draw_tree_entries(struct tog_view *view,
3780 struct got_tree_entry **first_displayed_entry,
3781 struct got_tree_entry **last_displayed_entry,
3782 struct got_tree_entry **selected_entry, int *ndisplayed,
3783 const char *label, int show_ids, const char *parent_path,
3784 const struct got_tree_entries *entries, int selected, int limit, int isroot)
3786 const struct got_error *err = NULL;
3787 struct got_tree_entry *te;
3788 wchar_t *wline;
3789 int width, n;
3791 *ndisplayed = 0;
3793 werase(view->window);
3795 if (limit == 0)
3796 return NULL;
3798 err = format_line(&wline, &width, label, view->ncols);
3799 if (err)
3800 return err;
3801 if (view_needs_focus_indication(view))
3802 wstandout(view->window);
3803 waddwstr(view->window, wline);
3804 if (view_needs_focus_indication(view))
3805 wstandend(view->window);
3806 free(wline);
3807 wline = NULL;
3808 if (width < view->ncols - 1)
3809 waddch(view->window, '\n');
3810 if (--limit <= 0)
3811 return NULL;
3812 err = format_line(&wline, &width, parent_path, view->ncols);
3813 if (err)
3814 return err;
3815 waddwstr(view->window, wline);
3816 free(wline);
3817 wline = NULL;
3818 if (width < view->ncols - 1)
3819 waddch(view->window, '\n');
3820 if (--limit <= 0)
3821 return NULL;
3822 waddch(view->window, '\n');
3823 if (--limit <= 0)
3824 return NULL;
3826 te = SIMPLEQ_FIRST(&entries->head);
3827 if (*first_displayed_entry == NULL) {
3828 if (selected == 0) {
3829 if (view->focussed)
3830 wstandout(view->window);
3831 *selected_entry = NULL;
3833 waddstr(view->window, " ..\n"); /* parent directory */
3834 if (selected == 0 && view->focussed)
3835 wstandend(view->window);
3836 (*ndisplayed)++;
3837 if (--limit <= 0)
3838 return NULL;
3839 n = 1;
3840 } else {
3841 n = 0;
3842 while (te != *first_displayed_entry)
3843 te = SIMPLEQ_NEXT(te, entry);
3846 while (te) {
3847 char *line = NULL, *id_str = NULL;
3849 if (show_ids) {
3850 err = got_object_id_str(&id_str, te->id);
3851 if (err)
3852 return got_error_from_errno(
3853 "got_object_id_str");
3855 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
3856 te->name, S_ISDIR(te->mode) ? "/" :
3857 ((te->mode & S_IXUSR) ? "*" : "")) == -1) {
3858 free(id_str);
3859 return got_error_from_errno("asprintf");
3861 free(id_str);
3862 err = format_line(&wline, &width, line, view->ncols);
3863 if (err) {
3864 free(line);
3865 break;
3867 if (n == selected) {
3868 if (view->focussed)
3869 wstandout(view->window);
3870 *selected_entry = te;
3872 waddwstr(view->window, wline);
3873 if (width < view->ncols - 1)
3874 waddch(view->window, '\n');
3875 if (n == selected && view->focussed)
3876 wstandend(view->window);
3877 free(line);
3878 free(wline);
3879 wline = NULL;
3880 n++;
3881 (*ndisplayed)++;
3882 *last_displayed_entry = te;
3883 if (--limit <= 0)
3884 break;
3885 te = SIMPLEQ_NEXT(te, entry);
3888 return err;
3891 static void
3892 tree_scroll_up(struct tog_view *view,
3893 struct got_tree_entry **first_displayed_entry, int maxscroll,
3894 const struct got_tree_entries *entries, int isroot)
3896 struct got_tree_entry *te, *prev;
3897 int i;
3899 if (*first_displayed_entry == NULL)
3900 return;
3902 te = SIMPLEQ_FIRST(&entries->head);
3903 if (*first_displayed_entry == te) {
3904 if (!isroot)
3905 *first_displayed_entry = NULL;
3906 return;
3909 /* XXX this is stupid... switch to TAILQ? */
3910 for (i = 0; i < maxscroll; i++) {
3911 while (te != *first_displayed_entry) {
3912 prev = te;
3913 te = SIMPLEQ_NEXT(te, entry);
3915 *first_displayed_entry = prev;
3916 te = SIMPLEQ_FIRST(&entries->head);
3918 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
3919 *first_displayed_entry = NULL;
3922 static int
3923 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
3924 struct got_tree_entry *last_displayed_entry,
3925 const struct got_tree_entries *entries)
3927 struct got_tree_entry *next, *last;
3928 int n = 0;
3930 if (*first_displayed_entry)
3931 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
3932 else
3933 next = SIMPLEQ_FIRST(&entries->head);
3934 last = last_displayed_entry;
3935 while (next && last && n++ < maxscroll) {
3936 last = SIMPLEQ_NEXT(last, entry);
3937 if (last) {
3938 *first_displayed_entry = next;
3939 next = SIMPLEQ_NEXT(next, entry);
3942 return n;
3945 static const struct got_error *
3946 tree_entry_path(char **path, struct tog_parent_trees *parents,
3947 struct got_tree_entry *te)
3949 const struct got_error *err = NULL;
3950 struct tog_parent_tree *pt;
3951 size_t len = 2; /* for leading slash and NUL */
3953 TAILQ_FOREACH(pt, parents, entry)
3954 len += strlen(pt->selected_entry->name) + 1 /* slash */;
3955 if (te)
3956 len += strlen(te->name);
3958 *path = calloc(1, len);
3959 if (path == NULL)
3960 return got_error_from_errno("calloc");
3962 (*path)[0] = '/';
3963 pt = TAILQ_LAST(parents, tog_parent_trees);
3964 while (pt) {
3965 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
3966 err = got_error(GOT_ERR_NO_SPACE);
3967 goto done;
3969 if (strlcat(*path, "/", len) >= len) {
3970 err = got_error(GOT_ERR_NO_SPACE);
3971 goto done;
3973 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
3975 if (te) {
3976 if (strlcat(*path, te->name, len) >= len) {
3977 err = got_error(GOT_ERR_NO_SPACE);
3978 goto done;
3981 done:
3982 if (err) {
3983 free(*path);
3984 *path = NULL;
3986 return err;
3989 static const struct got_error *
3990 blame_tree_entry(struct tog_view **new_view, int begin_x,
3991 struct got_tree_entry *te, struct tog_parent_trees *parents,
3992 struct got_object_id *commit_id, struct got_reflist_head *refs,
3993 struct got_repository *repo)
3995 const struct got_error *err = NULL;
3996 char *path;
3997 struct tog_view *blame_view;
3999 *new_view = NULL;
4001 err = tree_entry_path(&path, parents, te);
4002 if (err)
4003 return err;
4005 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4006 if (blame_view == NULL)
4007 return got_error_from_errno("view_open");
4009 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4010 if (err) {
4011 view_close(blame_view);
4012 free(path);
4013 } else
4014 *new_view = blame_view;
4015 return err;
4018 static const struct got_error *
4019 log_tree_entry(struct tog_view **new_view, int begin_x,
4020 struct got_tree_entry *te, struct tog_parent_trees *parents,
4021 struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 struct got_repository *repo)
4024 struct tog_view *log_view;
4025 const struct got_error *err = NULL;
4026 char *path;
4028 *new_view = NULL;
4030 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4031 if (log_view == NULL)
4032 return got_error_from_errno("view_open");
4034 err = tree_entry_path(&path, parents, te);
4035 if (err)
4036 return err;
4038 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4039 if (err)
4040 view_close(log_view);
4041 else
4042 *new_view = log_view;
4043 free(path);
4044 return err;
4047 static const struct got_error *
4048 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4049 struct got_object_id *commit_id, struct got_reflist_head *refs,
4050 struct got_repository *repo)
4052 const struct got_error *err = NULL;
4053 char *commit_id_str = NULL;
4054 struct tog_tree_view_state *s = &view->state.tree;
4056 TAILQ_INIT(&s->parents);
4058 err = got_object_id_str(&commit_id_str, commit_id);
4059 if (err != NULL)
4060 goto done;
4062 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4063 err = got_error_from_errno("asprintf");
4064 goto done;
4067 s->root = s->tree = root;
4068 s->entries = got_object_tree_get_entries(root);
4069 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4070 s->commit_id = got_object_id_dup(commit_id);
4071 if (s->commit_id == NULL) {
4072 err = got_error_from_errno("got_object_id_dup");
4073 goto done;
4075 s->refs = refs;
4076 s->repo = repo;
4078 view->show = show_tree_view;
4079 view->input = input_tree_view;
4080 view->close = close_tree_view;
4081 view->search_start = search_start_tree_view;
4082 view->search_next = search_next_tree_view;
4083 done:
4084 free(commit_id_str);
4085 if (err) {
4086 free(s->tree_label);
4087 s->tree_label = NULL;
4089 return err;
4092 static const struct got_error *
4093 close_tree_view(struct tog_view *view)
4095 struct tog_tree_view_state *s = &view->state.tree;
4097 free(s->tree_label);
4098 s->tree_label = NULL;
4099 free(s->commit_id);
4100 s->commit_id = NULL;
4101 while (!TAILQ_EMPTY(&s->parents)) {
4102 struct tog_parent_tree *parent;
4103 parent = TAILQ_FIRST(&s->parents);
4104 TAILQ_REMOVE(&s->parents, parent, entry);
4105 free(parent);
4108 if (s->tree != s->root)
4109 got_object_tree_close(s->tree);
4110 got_object_tree_close(s->root);
4112 return NULL;
4115 static const struct got_error *
4116 search_start_tree_view(struct tog_view *view)
4118 struct tog_tree_view_state *s = &view->state.tree;
4120 s->matched_entry = NULL;
4121 return NULL;
4124 static int
4125 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4127 regmatch_t regmatch;
4129 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4132 static const struct got_error *
4133 search_next_tree_view(struct tog_view *view)
4135 struct tog_tree_view_state *s = &view->state.tree;
4136 struct got_tree_entry *entry = NULL, *te;
4138 if (!view->searching) {
4139 view->search_next_done = 1;
4140 return NULL;
4143 if (s->matched_entry) {
4144 if (view->searching == TOG_SEARCH_FORWARD) {
4145 if (s->selected_entry)
4146 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4147 else
4148 entry = SIMPLEQ_FIRST(&s->entries->head);
4150 else {
4151 if (s->selected_entry == NULL) {
4152 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4153 entry = te;
4154 } else {
4155 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4156 entry = te;
4157 if (SIMPLEQ_NEXT(te, entry) ==
4158 s->selected_entry)
4159 break;
4163 } else {
4164 if (view->searching == TOG_SEARCH_FORWARD)
4165 entry = SIMPLEQ_FIRST(&s->entries->head);
4166 else {
4167 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4168 entry = te;
4172 while (1) {
4173 if (entry == NULL) {
4174 if (s->matched_entry == NULL) {
4175 view->search_next_done = 1;
4176 return NULL;
4178 if (view->searching == TOG_SEARCH_FORWARD)
4179 entry = SIMPLEQ_FIRST(&s->entries->head);
4180 else {
4181 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4182 entry = te;
4186 if (match_tree_entry(entry, &view->regex)) {
4187 view->search_next_done = 1;
4188 s->matched_entry = entry;
4189 break;
4192 if (view->searching == TOG_SEARCH_FORWARD)
4193 entry = SIMPLEQ_NEXT(entry, entry);
4194 else {
4195 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4196 entry = NULL;
4197 else {
4198 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4199 if (SIMPLEQ_NEXT(te, entry) == entry) {
4200 entry = te;
4201 break;
4208 if (s->matched_entry) {
4209 s->first_displayed_entry = s->matched_entry;
4210 s->selected = 0;
4213 return NULL;
4216 static const struct got_error *
4217 show_tree_view(struct tog_view *view)
4219 const struct got_error *err = NULL;
4220 struct tog_tree_view_state *s = &view->state.tree;
4221 char *parent_path;
4223 err = tree_entry_path(&parent_path, &s->parents, NULL);
4224 if (err)
4225 return err;
4227 err = draw_tree_entries(view, &s->first_displayed_entry,
4228 &s->last_displayed_entry, &s->selected_entry,
4229 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4230 s->entries, s->selected, view->nlines, s->tree == s->root);
4231 free(parent_path);
4233 view_vborder(view);
4234 return err;
4237 static const struct got_error *
4238 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4239 struct tog_view **focus_view, struct tog_view *view, int ch)
4241 const struct got_error *err = NULL;
4242 struct tog_tree_view_state *s = &view->state.tree;
4243 struct tog_view *log_view;
4244 int begin_x = 0, nscrolled;
4246 switch (ch) {
4247 case 'i':
4248 s->show_ids = !s->show_ids;
4249 break;
4250 case 'l':
4251 if (!s->selected_entry)
4252 break;
4253 if (view_is_parent_view(view))
4254 begin_x = view_split_begin_x(view->begin_x);
4255 err = log_tree_entry(&log_view, begin_x,
4256 s->selected_entry, &s->parents,
4257 s->commit_id, s->refs, s->repo);
4258 if (view_is_parent_view(view)) {
4259 err = view_close_child(view);
4260 if (err)
4261 return err;
4262 err = view_set_child(view, log_view);
4263 if (err) {
4264 view_close(log_view);
4265 break;
4267 *focus_view = log_view;
4268 view->child_focussed = 1;
4269 } else
4270 *new_view = log_view;
4271 break;
4272 case 'k':
4273 case KEY_UP:
4274 if (s->selected > 0) {
4275 s->selected--;
4276 if (s->selected == 0)
4277 break;
4279 if (s->selected > 0)
4280 break;
4281 tree_scroll_up(view, &s->first_displayed_entry, 1,
4282 s->entries, s->tree == s->root);
4283 break;
4284 case KEY_PPAGE:
4285 tree_scroll_up(view, &s->first_displayed_entry,
4286 MAX(0, view->nlines - 4 - s->selected), s->entries,
4287 s->tree == s->root);
4288 s->selected = 0;
4289 if (SIMPLEQ_FIRST(&s->entries->head) ==
4290 s->first_displayed_entry && s->tree != s->root)
4291 s->first_displayed_entry = NULL;
4292 break;
4293 case 'j':
4294 case KEY_DOWN:
4295 if (s->selected < s->ndisplayed - 1) {
4296 s->selected++;
4297 break;
4299 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4300 /* can't scroll any further */
4301 break;
4302 tree_scroll_down(&s->first_displayed_entry, 1,
4303 s->last_displayed_entry, s->entries);
4304 break;
4305 case KEY_NPAGE:
4306 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4307 == NULL) {
4308 /* can't scroll any further; move cursor down */
4309 if (s->selected < s->ndisplayed - 1)
4310 s->selected = s->ndisplayed - 1;
4311 break;
4313 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4314 view->nlines, s->last_displayed_entry, s->entries);
4315 if (nscrolled < view->nlines) {
4316 int ndisplayed = 0;
4317 struct got_tree_entry *te;
4318 te = s->first_displayed_entry;
4319 do {
4320 ndisplayed++;
4321 te = SIMPLEQ_NEXT(te, entry);
4322 } while (te);
4323 s->selected = ndisplayed - 1;
4325 break;
4326 case KEY_ENTER:
4327 case '\r':
4328 case KEY_BACKSPACE:
4329 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4330 struct tog_parent_tree *parent;
4331 /* user selected '..' */
4332 if (s->tree == s->root)
4333 break;
4334 parent = TAILQ_FIRST(&s->parents);
4335 TAILQ_REMOVE(&s->parents, parent,
4336 entry);
4337 got_object_tree_close(s->tree);
4338 s->tree = parent->tree;
4339 s->entries =
4340 got_object_tree_get_entries(s->tree);
4341 s->first_displayed_entry =
4342 parent->first_displayed_entry;
4343 s->selected_entry =
4344 parent->selected_entry;
4345 s->selected = parent->selected;
4346 free(parent);
4347 } else if (S_ISDIR(s->selected_entry->mode)) {
4348 struct got_tree_object *subtree;
4349 err = got_object_open_as_tree(&subtree,
4350 s->repo, s->selected_entry->id);
4351 if (err)
4352 break;
4353 err = tree_view_visit_subtree(subtree, s);
4354 if (err) {
4355 got_object_tree_close(subtree);
4356 break;
4358 } else if (S_ISREG(s->selected_entry->mode)) {
4359 struct tog_view *blame_view;
4360 int begin_x = view_is_parent_view(view) ?
4361 view_split_begin_x(view->begin_x) : 0;
4363 err = blame_tree_entry(&blame_view, begin_x,
4364 s->selected_entry, &s->parents,
4365 s->commit_id, s->refs, s->repo);
4366 if (err)
4367 break;
4368 if (view_is_parent_view(view)) {
4369 err = view_close_child(view);
4370 if (err)
4371 return err;
4372 err = view_set_child(view, blame_view);
4373 if (err) {
4374 view_close(blame_view);
4375 break;
4377 *focus_view = blame_view;
4378 view->child_focussed = 1;
4379 } else
4380 *new_view = blame_view;
4382 break;
4383 case KEY_RESIZE:
4384 if (s->selected > view->nlines)
4385 s->selected = s->ndisplayed - 1;
4386 break;
4387 default:
4388 break;
4391 return err;
4394 __dead static void
4395 usage_tree(void)
4397 endwin();
4398 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4399 getprogname());
4400 exit(1);
4403 static const struct got_error *
4404 cmd_tree(int argc, char *argv[])
4406 const struct got_error *error;
4407 struct got_repository *repo = NULL;
4408 struct got_reflist_head refs;
4409 char *repo_path = NULL;
4410 struct got_object_id *commit_id = NULL;
4411 char *commit_id_arg = NULL;
4412 struct got_commit_object *commit = NULL;
4413 struct got_tree_object *tree = NULL;
4414 int ch;
4415 struct tog_view *view;
4417 SIMPLEQ_INIT(&refs);
4419 #ifndef PROFILE
4420 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4421 NULL) == -1)
4422 err(1, "pledge");
4423 #endif
4425 while ((ch = getopt(argc, argv, "c:")) != -1) {
4426 switch (ch) {
4427 case 'c':
4428 commit_id_arg = optarg;
4429 break;
4430 default:
4431 usage_tree();
4432 /* NOTREACHED */
4436 argc -= optind;
4437 argv += optind;
4439 if (argc == 0) {
4440 struct got_worktree *worktree;
4441 char *cwd = getcwd(NULL, 0);
4442 if (cwd == NULL)
4443 return got_error_from_errno("getcwd");
4444 error = got_worktree_open(&worktree, cwd);
4445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4446 goto done;
4447 if (worktree) {
4448 free(cwd);
4449 repo_path =
4450 strdup(got_worktree_get_repo_path(worktree));
4451 got_worktree_close(worktree);
4452 } else
4453 repo_path = cwd;
4454 if (repo_path == NULL) {
4455 error = got_error_from_errno("strdup");
4456 goto done;
4458 } else if (argc == 1) {
4459 repo_path = realpath(argv[0], NULL);
4460 if (repo_path == NULL)
4461 return got_error_from_errno2("realpath", argv[0]);
4462 } else
4463 usage_log();
4465 init_curses();
4467 error = got_repo_open(&repo, repo_path);
4468 if (error != NULL)
4469 goto done;
4471 error = apply_unveil(got_repo_get_path(repo), NULL);
4472 if (error)
4473 goto done;
4475 if (commit_id_arg == NULL)
4476 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
4477 else {
4478 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
4479 if (error) {
4480 if (error->code != GOT_ERR_NOT_REF)
4481 goto done;
4482 error = got_repo_match_object_id_prefix(&commit_id,
4483 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
4486 if (error != NULL)
4487 goto done;
4489 error = got_object_open_as_commit(&commit, repo, commit_id);
4490 if (error != NULL)
4491 goto done;
4493 error = got_object_open_as_tree(&tree, repo,
4494 got_object_commit_get_tree_id(commit));
4495 if (error != NULL)
4496 goto done;
4498 error = got_ref_list(&refs, repo);
4499 if (error)
4500 goto done;
4502 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
4503 if (view == NULL) {
4504 error = got_error_from_errno("view_open");
4505 goto done;
4507 error = open_tree_view(view, tree, commit_id, &refs, repo);
4508 if (error)
4509 goto done;
4510 error = view_loop(view);
4511 done:
4512 free(repo_path);
4513 free(commit_id);
4514 if (commit)
4515 got_object_commit_close(commit);
4516 if (tree)
4517 got_object_tree_close(tree);
4518 if (repo)
4519 got_repo_close(repo);
4520 got_ref_list_free(&refs);
4521 return error;
4524 static void
4525 list_commands(void)
4527 int i;
4529 fprintf(stderr, "commands:");
4530 for (i = 0; i < nitems(tog_commands); i++) {
4531 struct tog_cmd *cmd = &tog_commands[i];
4532 fprintf(stderr, " %s", cmd->name);
4534 fputc('\n', stderr);
4537 __dead static void
4538 usage(int hflag)
4540 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
4541 getprogname());
4542 if (hflag)
4543 list_commands();
4544 exit(1);
4547 static char **
4548 make_argv(const char *arg0, const char *arg1)
4550 char **argv;
4551 int argc = (arg1 == NULL ? 1 : 2);
4553 argv = calloc(argc, sizeof(char *));
4554 if (argv == NULL)
4555 err(1, "calloc");
4556 argv[0] = strdup(arg0);
4557 if (argv[0] == NULL)
4558 err(1, "calloc");
4559 if (arg1) {
4560 argv[1] = strdup(arg1);
4561 if (argv[1] == NULL)
4562 err(1, "calloc");
4565 return argv;
4568 int
4569 main(int argc, char *argv[])
4571 const struct got_error *error = NULL;
4572 struct tog_cmd *cmd = NULL;
4573 int ch, hflag = 0, Vflag = 0;
4574 char **cmd_argv = NULL;
4576 setlocale(LC_CTYPE, "");
4578 while ((ch = getopt(argc, argv, "hV")) != -1) {
4579 switch (ch) {
4580 case 'h':
4581 hflag = 1;
4582 break;
4583 case 'V':
4584 Vflag = 1;
4585 break;
4586 default:
4587 usage(hflag);
4588 /* NOTREACHED */
4592 argc -= optind;
4593 argv += optind;
4594 optind = 0;
4595 optreset = 1;
4597 if (Vflag) {
4598 got_version_print_str();
4599 return 1;
4602 if (argc == 0) {
4603 if (hflag)
4604 usage(hflag);
4605 /* Build an argument vector which runs a default command. */
4606 cmd = &tog_commands[0];
4607 cmd_argv = make_argv(cmd->name, NULL);
4608 argc = 1;
4609 } else {
4610 int i;
4612 /* Did the user specific a command? */
4613 for (i = 0; i < nitems(tog_commands); i++) {
4614 if (strncmp(tog_commands[i].name, argv[0],
4615 strlen(argv[0])) == 0) {
4616 cmd = &tog_commands[i];
4617 break;
4621 if (cmd == NULL) {
4622 fprintf(stderr, "%s: unknown command '%s'\n",
4623 getprogname(), argv[0]);
4624 list_commands();
4625 return 1;
4629 if (hflag)
4630 cmd->cmd_usage();
4631 else
4632 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
4634 endwin();
4635 free(cmd_argv);
4636 if (error)
4637 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
4638 return 0;