Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 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 <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <err.h>
33 #include <unistd.h>
34 #include <util.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_diff.h"
48 #include "got_opentemp.h"
49 #include "got_utf8.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
54 #include "got_path.h"
55 #include "got_worktree.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #ifndef MAX
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #endif
65 #define CTRL(x) ((x) & 0x1f)
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 struct tog_cmd {
72 const char *name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 };
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
93 };
95 enum tog_view_type {
96 TOG_VIEW_DIFF,
97 TOG_VIEW_LOG,
98 TOG_VIEW_BLAME,
99 TOG_VIEW_TREE
100 };
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
108 int idx;
109 };
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
112 int ncommits;
113 struct commit_queue_head head;
114 };
116 struct tog_color {
117 SIMPLEQ_ENTRY(tog_color) entry;
118 regex_t regex;
119 short colorpair;
120 };
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
129 int regerr = 0;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
132 return NULL;
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
137 if (tc == NULL)
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 if (regerr) {
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
145 sizeof(regerr_msg));
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 regerr_msg);
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
149 free(tc);
150 return err;
152 tc->colorpair = idx;
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 return NULL;
157 static void
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
165 regfree(&tc->regex);
166 free(tc);
170 struct tog_color *
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
177 return tc;
180 return NULL;
183 static int
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 return COLOR_CYAN;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 return COLOR_YELLOW;
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 return COLOR_GREEN;
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 return COLOR_CYAN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 return COLOR_GREEN;
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 return COLOR_GREEN;
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 return COLOR_YELLOW;
209 return -1;
212 static int
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
217 if (val == NULL)
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
221 return COLOR_BLACK;
222 if (strcasecmp(val, "red") == 0)
223 return COLOR_RED;
224 if (strcasecmp(val, "green") == 0)
225 return COLOR_GREEN;
226 if (strcasecmp(val, "yellow") == 0)
227 return COLOR_YELLOW;
228 if (strcasecmp(val, "blue") == 0)
229 return COLOR_BLUE;
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
233 return COLOR_CYAN;
234 if (strcasecmp(val, "white") == 0)
235 return COLOR_WHITE;
236 if (strcasecmp(val, "default") == 0)
237 return -1;
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
245 FILE *f;
246 int first_displayed_line;
247 int last_displayed_line;
248 int eof;
249 int diff_context;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
254 /* passed from log view; may be NULL */
255 struct tog_view *log_view;
256 };
258 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
260 struct tog_log_thread_args {
261 pthread_cond_t need_commits;
262 int commits_needed;
263 struct got_commit_graph *graph;
264 struct commit_queue *commits;
265 const char *in_repo_path;
266 struct got_object_id *start_id;
267 struct got_repository *repo;
268 int log_complete;
269 sig_atomic_t *quit;
270 struct commit_queue_entry **first_displayed_entry;
271 struct commit_queue_entry **selected_entry;
272 int *searching;
273 int *search_next_done;
274 regex_t *regex;
275 };
277 struct tog_log_view_state {
278 struct commit_queue commits;
279 struct commit_queue_entry *first_displayed_entry;
280 struct commit_queue_entry *last_displayed_entry;
281 struct commit_queue_entry *selected_entry;
282 int selected;
283 char *in_repo_path;
284 const char *head_ref_name;
285 int log_branches;
286 struct got_repository *repo;
287 struct got_reflist_head *refs;
288 struct got_object_id *start_id;
289 sig_atomic_t quit;
290 pthread_t thread;
291 struct tog_log_thread_args thread_args;
292 struct commit_queue_entry *matched_entry;
293 struct commit_queue_entry *search_entry;
294 struct tog_colors colors;
295 };
297 #define TOG_COLOR_DIFF_MINUS 1
298 #define TOG_COLOR_DIFF_PLUS 2
299 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
300 #define TOG_COLOR_DIFF_META 4
301 #define TOG_COLOR_TREE_SUBMODULE 5
302 #define TOG_COLOR_TREE_SYMLINK 6
303 #define TOG_COLOR_TREE_DIRECTORY 7
304 #define TOG_COLOR_TREE_EXECUTABLE 8
305 #define TOG_COLOR_COMMIT 9
306 #define TOG_COLOR_AUTHOR 10
307 #define TOG_COLOR_DATE 11
309 struct tog_blame_cb_args {
310 struct tog_blame_line *lines; /* one per line */
311 int nlines;
313 struct tog_view *view;
314 struct got_object_id *commit_id;
315 int *quit;
316 };
318 struct tog_blame_thread_args {
319 const char *path;
320 struct got_repository *repo;
321 struct tog_blame_cb_args *cb_args;
322 int *complete;
323 got_cancel_cb cancel_cb;
324 void *cancel_arg;
325 };
327 struct tog_blame {
328 FILE *f;
329 size_t filesize;
330 struct tog_blame_line *lines;
331 int nlines;
332 off_t *line_offsets;
333 pthread_t thread;
334 struct tog_blame_thread_args thread_args;
335 struct tog_blame_cb_args cb_args;
336 const char *path;
337 };
339 struct tog_blame_view_state {
340 int first_displayed_line;
341 int last_displayed_line;
342 int selected_line;
343 int blame_complete;
344 int eof;
345 int done;
346 struct got_object_id_queue blamed_commits;
347 struct got_object_qid *blamed_commit;
348 char *path;
349 struct got_repository *repo;
350 struct got_reflist_head *refs;
351 struct got_object_id *commit_id;
352 struct tog_blame blame;
353 int matched_line;
354 struct tog_colors colors;
355 };
357 struct tog_parent_tree {
358 TAILQ_ENTRY(tog_parent_tree) entry;
359 struct got_tree_object *tree;
360 struct got_tree_entry *first_displayed_entry;
361 struct got_tree_entry *selected_entry;
362 int selected;
363 };
365 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
367 struct tog_tree_view_state {
368 char *tree_label;
369 struct got_tree_object *root;
370 struct got_tree_object *tree;
371 struct got_tree_entry *first_displayed_entry;
372 struct got_tree_entry *last_displayed_entry;
373 struct got_tree_entry *selected_entry;
374 int ndisplayed, selected, show_ids;
375 struct tog_parent_trees parents;
376 struct got_object_id *commit_id;
377 struct got_repository *repo;
378 struct got_reflist_head *refs;
379 struct got_tree_entry *matched_entry;
380 struct tog_colors colors;
381 };
383 /*
384 * We implement two types of views: parent views and child views.
386 * The 'Tab' key switches between a parent view and its child view.
387 * Child views are shown side-by-side to their parent view, provided
388 * there is enough screen estate.
390 * When a new view is opened from within a parent view, this new view
391 * becomes a child view of the parent view, replacing any existing child.
393 * When a new view is opened from within a child view, this new view
394 * becomes a parent view which will obscure the views below until the
395 * user quits the new parent view by typing 'q'.
397 * This list of views contains parent views only.
398 * Child views are only pointed to by their parent view.
399 */
400 TAILQ_HEAD(tog_view_list_head, tog_view);
402 struct tog_view {
403 TAILQ_ENTRY(tog_view) entry;
404 WINDOW *window;
405 PANEL *panel;
406 int nlines, ncols, begin_y, begin_x;
407 int lines, cols; /* copies of LINES and COLS */
408 int focussed;
409 struct tog_view *parent;
410 struct tog_view *child;
411 int child_focussed;
413 /* type-specific state */
414 enum tog_view_type type;
415 union {
416 struct tog_diff_view_state diff;
417 struct tog_log_view_state log;
418 struct tog_blame_view_state blame;
419 struct tog_tree_view_state tree;
420 } state;
422 const struct got_error *(*show)(struct tog_view *);
423 const struct got_error *(*input)(struct tog_view **,
424 struct tog_view **, struct tog_view**, struct tog_view *, int);
425 const struct got_error *(*close)(struct tog_view *);
427 const struct got_error *(*search_start)(struct tog_view *);
428 const struct got_error *(*search_next)(struct tog_view *);
429 int searching;
430 #define TOG_SEARCH_FORWARD 1
431 #define TOG_SEARCH_BACKWARD 2
432 int search_next_done;
433 regex_t regex;
434 };
436 static const struct got_error *open_diff_view(struct tog_view *,
437 struct got_object_id *, struct got_object_id *, struct tog_view *,
438 struct got_reflist_head *, struct got_repository *);
439 static const struct got_error *show_diff_view(struct tog_view *);
440 static const struct got_error *input_diff_view(struct tog_view **,
441 struct tog_view **, struct tog_view **, struct tog_view *, int);
442 static const struct got_error* close_diff_view(struct tog_view *);
444 static const struct got_error *open_log_view(struct tog_view *,
445 struct got_object_id *, struct got_reflist_head *,
446 struct got_repository *, const char *, const char *, int, int);
447 static const struct got_error * show_log_view(struct tog_view *);
448 static const struct got_error *input_log_view(struct tog_view **,
449 struct tog_view **, struct tog_view **, struct tog_view *, int);
450 static const struct got_error *close_log_view(struct tog_view *);
451 static const struct got_error *search_start_log_view(struct tog_view *);
452 static const struct got_error *search_next_log_view(struct tog_view *);
454 static const struct got_error *open_blame_view(struct tog_view *, char *,
455 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
456 static const struct got_error *show_blame_view(struct tog_view *);
457 static const struct got_error *input_blame_view(struct tog_view **,
458 struct tog_view **, struct tog_view **, struct tog_view *, int);
459 static const struct got_error *close_blame_view(struct tog_view *);
460 static const struct got_error *search_start_blame_view(struct tog_view *);
461 static const struct got_error *search_next_blame_view(struct tog_view *);
463 static const struct got_error *open_tree_view(struct tog_view *,
464 struct got_tree_object *, struct got_object_id *,
465 struct got_reflist_head *, struct got_repository *);
466 static const struct got_error *show_tree_view(struct tog_view *);
467 static const struct got_error *input_tree_view(struct tog_view **,
468 struct tog_view **, struct tog_view **, struct tog_view *, int);
469 static const struct got_error *close_tree_view(struct tog_view *);
470 static const struct got_error *search_start_tree_view(struct tog_view *);
471 static const struct got_error *search_next_tree_view(struct tog_view *);
473 static volatile sig_atomic_t tog_sigwinch_received;
474 static volatile sig_atomic_t tog_sigpipe_received;
475 static volatile sig_atomic_t tog_sigcont_received;
477 static void
478 tog_sigwinch(int signo)
480 tog_sigwinch_received = 1;
483 static void
484 tog_sigpipe(int signo)
486 tog_sigpipe_received = 1;
489 static void
490 tog_sigcont(int signo)
492 tog_sigcont_received = 1;
495 static const struct got_error *
496 view_close(struct tog_view *view)
498 const struct got_error *err = NULL;
500 if (view->child) {
501 view_close(view->child);
502 view->child = NULL;
504 if (view->close)
505 err = view->close(view);
506 if (view->panel)
507 del_panel(view->panel);
508 if (view->window)
509 delwin(view->window);
510 free(view);
511 return err;
514 static struct tog_view *
515 view_open(int nlines, int ncols, int begin_y, int begin_x,
516 enum tog_view_type type)
518 struct tog_view *view = calloc(1, sizeof(*view));
520 if (view == NULL)
521 return NULL;
523 view->type = type;
524 view->lines = LINES;
525 view->cols = COLS;
526 view->nlines = nlines ? nlines : LINES - begin_y;
527 view->ncols = ncols ? ncols : COLS - begin_x;
528 view->begin_y = begin_y;
529 view->begin_x = begin_x;
530 view->window = newwin(nlines, ncols, begin_y, begin_x);
531 if (view->window == NULL) {
532 view_close(view);
533 return NULL;
535 view->panel = new_panel(view->window);
536 if (view->panel == NULL ||
537 set_panel_userptr(view->panel, view) != OK) {
538 view_close(view);
539 return NULL;
542 keypad(view->window, TRUE);
543 return view;
546 static int
547 view_split_begin_x(int begin_x)
549 if (begin_x > 0 || COLS < 120)
550 return 0;
551 return (COLS - MAX(COLS / 2, 80));
554 static const struct got_error *view_resize(struct tog_view *);
556 static const struct got_error *
557 view_splitscreen(struct tog_view *view)
559 const struct got_error *err = NULL;
561 view->begin_y = 0;
562 view->begin_x = view_split_begin_x(0);
563 view->nlines = LINES;
564 view->ncols = COLS - view->begin_x;
565 view->lines = LINES;
566 view->cols = COLS;
567 err = view_resize(view);
568 if (err)
569 return err;
571 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
572 return got_error_from_errno("mvwin");
574 return NULL;
577 static const struct got_error *
578 view_fullscreen(struct tog_view *view)
580 const struct got_error *err = NULL;
582 view->begin_x = 0;
583 view->begin_y = 0;
584 view->nlines = LINES;
585 view->ncols = COLS;
586 view->lines = LINES;
587 view->cols = COLS;
588 err = view_resize(view);
589 if (err)
590 return err;
592 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
593 return got_error_from_errno("mvwin");
595 return NULL;
598 static int
599 view_is_parent_view(struct tog_view *view)
601 return view->parent == NULL;
604 static const struct got_error *
605 view_resize(struct tog_view *view)
607 int nlines, ncols;
609 if (view->lines > LINES)
610 nlines = view->nlines - (view->lines - LINES);
611 else
612 nlines = view->nlines + (LINES - view->lines);
614 if (view->cols > COLS)
615 ncols = view->ncols - (view->cols - COLS);
616 else
617 ncols = view->ncols + (COLS - view->cols);
619 if (wresize(view->window, nlines, ncols) == ERR)
620 return got_error_from_errno("wresize");
621 if (replace_panel(view->panel, view->window) == ERR)
622 return got_error_from_errno("replace_panel");
623 wclear(view->window);
625 view->nlines = nlines;
626 view->ncols = ncols;
627 view->lines = LINES;
628 view->cols = COLS;
630 if (view->child) {
631 view->child->begin_x = view_split_begin_x(view->begin_x);
632 if (view->child->begin_x == 0) {
633 view_fullscreen(view->child);
634 if (view->child->focussed)
635 show_panel(view->child->panel);
636 else
637 show_panel(view->panel);
638 } else {
639 view_splitscreen(view->child);
640 show_panel(view->child->panel);
644 return NULL;
647 static const struct got_error *
648 view_close_child(struct tog_view *view)
650 const struct got_error *err = NULL;
652 if (view->child == NULL)
653 return NULL;
655 err = view_close(view->child);
656 view->child = NULL;
657 return err;
660 static const struct got_error *
661 view_set_child(struct tog_view *view, struct tog_view *child)
663 const struct got_error *err = NULL;
665 view->child = child;
666 child->parent = view;
667 return err;
670 static int
671 view_is_splitscreen(struct tog_view *view)
673 return view->begin_x > 0;
676 static void
677 tog_resizeterm(void)
679 int cols, lines;
680 struct winsize size;
682 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
683 cols = 80; /* Default */
684 lines = 24;
685 } else {
686 cols = size.ws_col;
687 lines = size.ws_row;
689 resize_term(lines, cols);
692 static const struct got_error *
693 view_search_start(struct tog_view *view)
695 const struct got_error *err = NULL;
696 char pattern[1024];
697 int ret;
698 int begin_x = 0;
700 if (view->nlines < 1)
701 return NULL;
703 if (!view_is_parent_view(view))
704 begin_x = view_split_begin_x(view->begin_x);
705 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
706 begin_x, "/");
707 wclrtoeol(view->window);
709 nocbreak();
710 echo();
711 ret = wgetnstr(view->window, pattern, sizeof(pattern));
712 cbreak();
713 noecho();
714 if (ret == ERR)
715 return NULL;
717 if (view->searching) {
718 regfree(&view->regex);
719 view->searching = 0;
722 if (regcomp(&view->regex, pattern,
723 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
724 err = view->search_start(view);
725 if (err) {
726 regfree(&view->regex);
727 return err;
729 view->searching = TOG_SEARCH_FORWARD;
730 view->search_next_done = 0;
731 view->search_next(view);
734 return NULL;
737 static const struct got_error *
738 view_input(struct tog_view **new, struct tog_view **dead,
739 struct tog_view **focus, int *done, struct tog_view *view,
740 struct tog_view_list_head *views)
742 const struct got_error *err = NULL;
743 struct tog_view *v;
744 int ch, errcode;
746 *new = NULL;
747 *dead = NULL;
748 *focus = NULL;
750 if (view->searching && !view->search_next_done) {
751 view->search_next(view);
752 return NULL;
755 nodelay(stdscr, FALSE);
756 /* Allow threads to make progress while we are waiting for input. */
757 errcode = pthread_mutex_unlock(&tog_mutex);
758 if (errcode)
759 return got_error_set_errno(errcode, "pthread_mutex_unlock");
760 ch = wgetch(view->window);
761 errcode = pthread_mutex_lock(&tog_mutex);
762 if (errcode)
763 return got_error_set_errno(errcode, "pthread_mutex_lock");
764 nodelay(stdscr, TRUE);
766 if (tog_sigwinch_received || tog_sigcont_received) {
767 tog_resizeterm();
768 tog_sigwinch_received = 0;
769 tog_sigcont_received = 0;
770 TAILQ_FOREACH(v, views, entry) {
771 err = view_resize(v);
772 if (err)
773 return err;
774 err = v->input(new, dead, focus, v, KEY_RESIZE);
775 if (err)
776 return err;
780 switch (ch) {
781 case ERR:
782 break;
783 case '\t':
784 if (view->child) {
785 *focus = view->child;
786 view->child_focussed = 1;
787 } else if (view->parent) {
788 *focus = view->parent;
789 view->parent->child_focussed = 0;
791 break;
792 case 'q':
793 err = view->input(new, dead, focus, view, ch);
794 *dead = view;
795 break;
796 case 'Q':
797 *done = 1;
798 break;
799 case 'f':
800 if (view_is_parent_view(view)) {
801 if (view->child == NULL)
802 break;
803 if (view_is_splitscreen(view->child)) {
804 *focus = view->child;
805 view->child_focussed = 1;
806 err = view_fullscreen(view->child);
807 } else
808 err = view_splitscreen(view->child);
809 if (err)
810 break;
811 err = view->child->input(new, dead, focus,
812 view->child, KEY_RESIZE);
813 } else {
814 if (view_is_splitscreen(view)) {
815 *focus = view;
816 view->parent->child_focussed = 1;
817 err = view_fullscreen(view);
818 } else {
819 err = view_splitscreen(view);
821 if (err)
822 break;
823 err = view->input(new, dead, focus, view,
824 KEY_RESIZE);
826 break;
827 case KEY_RESIZE:
828 break;
829 case '/':
830 if (view->search_start)
831 view_search_start(view);
832 else
833 err = view->input(new, dead, focus, view, ch);
834 break;
835 case 'N':
836 case 'n':
837 if (view->search_next && view->searching) {
838 view->searching = (ch == 'n' ?
839 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
840 view->search_next_done = 0;
841 view->search_next(view);
842 } else
843 err = view->input(new, dead, focus, view, ch);
844 break;
845 default:
846 err = view->input(new, dead, focus, view, ch);
847 break;
850 return err;
853 void
854 view_vborder(struct tog_view *view)
856 PANEL *panel;
857 struct tog_view *view_above;
859 if (view->parent)
860 return view_vborder(view->parent);
862 panel = panel_above(view->panel);
863 if (panel == NULL)
864 return;
866 view_above = panel_userptr(panel);
867 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
868 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 int
872 view_needs_focus_indication(struct tog_view *view)
874 if (view_is_parent_view(view)) {
875 if (view->child == NULL || view->child_focussed)
876 return 0;
877 if (!view_is_splitscreen(view->child))
878 return 0;
879 } else if (!view_is_splitscreen(view))
880 return 0;
882 return view->focussed;
885 static const struct got_error *
886 view_loop(struct tog_view *view)
888 const struct got_error *err = NULL;
889 struct tog_view_list_head views;
890 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
891 int fast_refresh = 10;
892 int done = 0, errcode;
894 errcode = pthread_mutex_lock(&tog_mutex);
895 if (errcode)
896 return got_error_set_errno(errcode, "pthread_mutex_lock");
898 TAILQ_INIT(&views);
899 TAILQ_INSERT_HEAD(&views, view, entry);
901 main_view = view;
902 view->focussed = 1;
903 err = view->show(view);
904 if (err)
905 return err;
906 update_panels();
907 doupdate();
908 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
909 /* Refresh fast during initialization, then become slower. */
910 if (fast_refresh && fast_refresh-- == 0)
911 halfdelay(10); /* switch to once per second */
913 err = view_input(&new_view, &dead_view, &focus_view, &done,
914 view, &views);
915 if (err)
916 break;
917 if (dead_view) {
918 struct tog_view *prev = NULL;
920 if (view_is_parent_view(dead_view))
921 prev = TAILQ_PREV(dead_view,
922 tog_view_list_head, entry);
923 else if (view->parent != dead_view)
924 prev = view->parent;
926 if (dead_view->parent)
927 dead_view->parent->child = NULL;
928 else
929 TAILQ_REMOVE(&views, dead_view, entry);
931 err = view_close(dead_view);
932 if (err || (dead_view == main_view && new_view == NULL))
933 goto done;
935 if (view == dead_view) {
936 if (focus_view)
937 view = focus_view;
938 else if (prev)
939 view = prev;
940 else if (!TAILQ_EMPTY(&views))
941 view = TAILQ_LAST(&views,
942 tog_view_list_head);
943 else
944 view = NULL;
945 if (view) {
946 if (view->child && view->child_focussed)
947 focus_view = view->child;
948 else
949 focus_view = view;
953 if (new_view) {
954 struct tog_view *v, *t;
955 /* Only allow one parent view per type. */
956 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
957 if (v->type != new_view->type)
958 continue;
959 TAILQ_REMOVE(&views, v, entry);
960 err = view_close(v);
961 if (err)
962 goto done;
963 break;
965 TAILQ_INSERT_TAIL(&views, new_view, entry);
966 view = new_view;
967 if (focus_view == NULL)
968 focus_view = new_view;
970 if (focus_view) {
971 show_panel(focus_view->panel);
972 if (view)
973 view->focussed = 0;
974 focus_view->focussed = 1;
975 view = focus_view;
976 if (new_view)
977 show_panel(new_view->panel);
978 if (view->child && view_is_splitscreen(view->child))
979 show_panel(view->child->panel);
981 if (view) {
982 if (focus_view == NULL) {
983 view->focussed = 1;
984 show_panel(view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
987 focus_view = view;
989 if (view->parent) {
990 err = view->parent->show(view->parent);
991 if (err)
992 goto done;
994 err = view->show(view);
995 if (err)
996 goto done;
997 if (view->child) {
998 err = view->child->show(view->child);
999 if (err)
1000 goto done;
1002 update_panels();
1003 doupdate();
1006 done:
1007 while (!TAILQ_EMPTY(&views)) {
1008 view = TAILQ_FIRST(&views);
1009 TAILQ_REMOVE(&views, view, entry);
1010 view_close(view);
1013 errcode = pthread_mutex_unlock(&tog_mutex);
1014 if (errcode && err == NULL)
1015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1017 return err;
1020 __dead static void
1021 usage_log(void)
1023 endwin();
1024 fprintf(stderr,
1025 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1026 getprogname());
1027 exit(1);
1030 /* Create newly allocated wide-character string equivalent to a byte string. */
1031 static const struct got_error *
1032 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1034 char *vis = NULL;
1035 const struct got_error *err = NULL;
1037 *ws = NULL;
1038 *wlen = mbstowcs(NULL, s, 0);
1039 if (*wlen == (size_t)-1) {
1040 int vislen;
1041 if (errno != EILSEQ)
1042 return got_error_from_errno("mbstowcs");
1044 /* byte string invalid in current encoding; try to "fix" it */
1045 err = got_mbsavis(&vis, &vislen, s);
1046 if (err)
1047 return err;
1048 *wlen = mbstowcs(NULL, vis, 0);
1049 if (*wlen == (size_t)-1) {
1050 err = got_error_from_errno("mbstowcs"); /* give up */
1051 goto done;
1055 *ws = calloc(*wlen + 1, sizeof(**ws));
1056 if (*ws == NULL) {
1057 err = got_error_from_errno("calloc");
1058 goto done;
1061 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1062 err = got_error_from_errno("mbstowcs");
1063 done:
1064 free(vis);
1065 if (err) {
1066 free(*ws);
1067 *ws = NULL;
1068 *wlen = 0;
1070 return err;
1073 /* Format a line for display, ensuring that it won't overflow a width limit. */
1074 static const struct got_error *
1075 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1076 int col_tab_align)
1078 const struct got_error *err = NULL;
1079 int cols = 0;
1080 wchar_t *wline = NULL;
1081 size_t wlen;
1082 int i;
1084 *wlinep = NULL;
1085 *widthp = 0;
1087 err = mbs2ws(&wline, &wlen, line);
1088 if (err)
1089 return err;
1091 i = 0;
1092 while (i < wlen) {
1093 int width = wcwidth(wline[i]);
1095 if (width == 0) {
1096 i++;
1097 continue;
1100 if (width == 1 || width == 2) {
1101 if (cols + width > wlimit)
1102 break;
1103 cols += width;
1104 i++;
1105 } else if (width == -1) {
1106 if (wline[i] == L'\t') {
1107 width = TABSIZE -
1108 ((cols + col_tab_align) % TABSIZE);
1109 if (cols + width > wlimit)
1110 break;
1111 cols += width;
1113 i++;
1114 } else {
1115 err = got_error_from_errno("wcwidth");
1116 goto done;
1119 wline[i] = L'\0';
1120 if (widthp)
1121 *widthp = cols;
1122 done:
1123 if (err)
1124 free(wline);
1125 else
1126 *wlinep = wline;
1127 return err;
1130 static const struct got_error*
1131 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1132 struct got_object_id *id, struct got_repository *repo)
1134 static const struct got_error *err = NULL;
1135 struct got_reflist_entry *re;
1136 char *s;
1137 const char *name;
1139 *refs_str = NULL;
1141 SIMPLEQ_FOREACH(re, refs, entry) {
1142 struct got_tag_object *tag = NULL;
1143 int cmp;
1145 name = got_ref_get_name(re->ref);
1146 if (strcmp(name, GOT_REF_HEAD) == 0)
1147 continue;
1148 if (strncmp(name, "refs/", 5) == 0)
1149 name += 5;
1150 if (strncmp(name, "got/", 4) == 0)
1151 continue;
1152 if (strncmp(name, "heads/", 6) == 0)
1153 name += 6;
1154 if (strncmp(name, "remotes/", 8) == 0)
1155 name += 8;
1156 if (strncmp(name, "tags/", 5) == 0) {
1157 err = got_object_open_as_tag(&tag, repo, re->id);
1158 if (err) {
1159 if (err->code != GOT_ERR_OBJ_TYPE)
1160 break;
1161 /* Ref points at something other than a tag. */
1162 err = NULL;
1163 tag = NULL;
1166 cmp = got_object_id_cmp(tag ?
1167 got_object_tag_get_object_id(tag) : re->id, id);
1168 if (tag)
1169 got_object_tag_close(tag);
1170 if (cmp != 0)
1171 continue;
1172 s = *refs_str;
1173 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1174 s ? ", " : "", name) == -1) {
1175 err = got_error_from_errno("asprintf");
1176 free(s);
1177 *refs_str = NULL;
1178 break;
1180 free(s);
1183 return err;
1186 static const struct got_error *
1187 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1188 int col_tab_align)
1190 char *smallerthan, *at;
1192 smallerthan = strchr(author, '<');
1193 if (smallerthan && smallerthan[1] != '\0')
1194 author = smallerthan + 1;
1195 at = strchr(author, '@');
1196 if (at)
1197 *at = '\0';
1198 return format_line(wauthor, author_width, author, limit, col_tab_align);
1201 static const struct got_error *
1202 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1203 struct got_object_id *id, struct got_reflist_head *refs,
1204 const size_t date_display_cols, int author_display_cols,
1205 struct tog_colors *colors)
1207 const struct got_error *err = NULL;
1208 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1209 char *logmsg0 = NULL, *logmsg = NULL;
1210 char *author = NULL;
1211 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1212 int author_width, logmsg_width;
1213 char *newline, *line = NULL;
1214 int col, limit;
1215 const int avail = view->ncols;
1216 struct tm tm;
1217 time_t committer_time;
1218 struct tog_color *tc;
1220 committer_time = got_object_commit_get_committer_time(commit);
1221 if (localtime_r(&committer_time, &tm) == NULL)
1222 return got_error_from_errno("localtime_r");
1223 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1224 >= sizeof(datebuf))
1225 return got_error(GOT_ERR_NO_SPACE);
1227 if (avail <= date_display_cols)
1228 limit = MIN(sizeof(datebuf) - 1, avail);
1229 else
1230 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1231 tc = get_color(colors, TOG_COLOR_DATE);
1232 if (tc)
1233 wattr_on(view->window,
1234 COLOR_PAIR(tc->colorpair), NULL);
1235 waddnstr(view->window, datebuf, limit);
1236 if (tc)
1237 wattr_off(view->window,
1238 COLOR_PAIR(tc->colorpair), NULL);
1239 col = limit;
1240 if (col > avail)
1241 goto done;
1243 if (avail >= 120) {
1244 char *id_str;
1245 err = got_object_id_str(&id_str, id);
1246 if (err)
1247 goto done;
1248 tc = get_color(colors, TOG_COLOR_COMMIT);
1249 if (tc)
1250 wattr_on(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1252 wprintw(view->window, "%.8s ", id_str);
1253 if (tc)
1254 wattr_off(view->window,
1255 COLOR_PAIR(tc->colorpair), NULL);
1256 free(id_str);
1257 col += 9;
1258 if (col > avail)
1259 goto done;
1262 author = strdup(got_object_commit_get_author(commit));
1263 if (author == NULL) {
1264 err = got_error_from_errno("strdup");
1265 goto done;
1267 err = format_author(&wauthor, &author_width, author, avail - col, col);
1268 if (err)
1269 goto done;
1270 tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 if (tc)
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 waddwstr(view->window, wauthor);
1275 if (tc)
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 col += author_width;
1279 while (col < avail && author_width < author_display_cols + 2) {
1280 waddch(view->window, ' ');
1281 col++;
1282 author_width++;
1284 if (col > avail)
1285 goto done;
1287 err = got_object_commit_get_logmsg(&logmsg0, commit);
1288 if (err)
1289 goto done;
1290 logmsg = logmsg0;
1291 while (*logmsg == '\n')
1292 logmsg++;
1293 newline = strchr(logmsg, '\n');
1294 if (newline)
1295 *newline = '\0';
1296 limit = avail - col;
1297 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1298 if (err)
1299 goto done;
1300 waddwstr(view->window, wlogmsg);
1301 col += logmsg_width;
1302 while (col < avail) {
1303 waddch(view->window, ' ');
1304 col++;
1306 done:
1307 free(logmsg0);
1308 free(wlogmsg);
1309 free(author);
1310 free(wauthor);
1311 free(line);
1312 return err;
1315 static struct commit_queue_entry *
1316 alloc_commit_queue_entry(struct got_commit_object *commit,
1317 struct got_object_id *id)
1319 struct commit_queue_entry *entry;
1321 entry = calloc(1, sizeof(*entry));
1322 if (entry == NULL)
1323 return NULL;
1325 entry->id = id;
1326 entry->commit = commit;
1327 return entry;
1330 static void
1331 pop_commit(struct commit_queue *commits)
1333 struct commit_queue_entry *entry;
1335 entry = TAILQ_FIRST(&commits->head);
1336 TAILQ_REMOVE(&commits->head, entry, entry);
1337 got_object_commit_close(entry->commit);
1338 commits->ncommits--;
1339 /* Don't free entry->id! It is owned by the commit graph. */
1340 free(entry);
1343 static void
1344 free_commits(struct commit_queue *commits)
1346 while (!TAILQ_EMPTY(&commits->head))
1347 pop_commit(commits);
1350 static const struct got_error *
1351 match_commit(int *have_match, struct got_object_id *id,
1352 struct got_commit_object *commit, regex_t *regex)
1354 const struct got_error *err = NULL;
1355 regmatch_t regmatch;
1356 char *id_str = NULL, *logmsg = NULL;
1358 *have_match = 0;
1360 err = got_object_id_str(&id_str, id);
1361 if (err)
1362 return err;
1364 err = got_object_commit_get_logmsg(&logmsg, commit);
1365 if (err)
1366 goto done;
1368 if (regexec(regex, got_object_commit_get_author(commit), 1,
1369 &regmatch, 0) == 0 ||
1370 regexec(regex, got_object_commit_get_committer(commit), 1,
1371 &regmatch, 0) == 0 ||
1372 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1373 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1374 *have_match = 1;
1375 done:
1376 free(id_str);
1377 free(logmsg);
1378 return err;
1381 static const struct got_error *
1382 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1383 int minqueue, struct got_repository *repo, const char *path,
1384 int *searching, int *search_next_done, regex_t *regex)
1386 const struct got_error *err = NULL;
1387 int nqueued = 0, have_match = 0;
1390 * We keep all commits open throughout the lifetime of the log
1391 * view in order to avoid having to re-fetch commits from disk
1392 * while updating the display.
1394 while (nqueued < minqueue ||
1395 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1396 struct got_object_id *id;
1397 struct got_commit_object *commit;
1398 struct commit_queue_entry *entry;
1399 int errcode;
1401 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1402 if (err || id == NULL)
1403 break;
1405 err = got_object_open_as_commit(&commit, repo, id);
1406 if (err)
1407 break;
1408 entry = alloc_commit_queue_entry(commit, id);
1409 if (entry == NULL) {
1410 err = got_error_from_errno("alloc_commit_queue_entry");
1411 break;
1414 errcode = pthread_mutex_lock(&tog_mutex);
1415 if (errcode) {
1416 err = got_error_set_errno(errcode,
1417 "pthread_mutex_lock");
1418 break;
1421 entry->idx = commits->ncommits;
1422 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1423 nqueued++;
1424 commits->ncommits++;
1426 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1427 err = match_commit(&have_match, id, commit, regex);
1430 errcode = pthread_mutex_unlock(&tog_mutex);
1431 if (errcode && err == NULL)
1432 err = got_error_set_errno(errcode,
1433 "pthread_mutex_unlock");
1435 if (err || have_match)
1436 break;
1439 return err;
1442 static const struct got_error *
1443 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1444 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1445 struct commit_queue *commits, int selected_idx, int limit,
1446 struct got_reflist_head *refs, const char *path, int commits_needed,
1447 struct tog_colors *colors)
1449 const struct got_error *err = NULL;
1450 struct tog_log_view_state *s = &view->state.log;
1451 struct commit_queue_entry *entry;
1452 int width;
1453 int ncommits, author_cols = 4;
1454 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1455 char *refs_str = NULL;
1456 wchar_t *wline;
1457 struct tog_color *tc;
1458 static const size_t date_display_cols = 12;
1460 entry = first;
1461 ncommits = 0;
1462 while (entry) {
1463 if (ncommits == selected_idx) {
1464 *selected = entry;
1465 break;
1467 entry = TAILQ_NEXT(entry, entry);
1468 ncommits++;
1471 if (*selected && !(view->searching && view->search_next_done == 0)) {
1472 err = got_object_id_str(&id_str, (*selected)->id);
1473 if (err)
1474 return err;
1475 if (refs) {
1476 err = build_refs_str(&refs_str, refs, (*selected)->id,
1477 s->repo);
1478 if (err)
1479 goto done;
1483 if (commits_needed == 0)
1484 halfdelay(10); /* disable fast refresh */
1486 if (asprintf(&ncommits_str, " [%d/%d] %s",
1487 entry ? entry->idx + 1 : 0, commits->ncommits,
1488 commits_needed > 0 ?
1489 (view->searching && view->search_next_done == 0
1490 ? "searching..." : "loading... ") :
1491 (refs_str ? refs_str : "")) == -1) {
1492 err = got_error_from_errno("asprintf");
1493 goto done;
1496 if (path && strcmp(path, "/") != 0) {
1497 if (asprintf(&header, "commit %s %s%s",
1498 id_str ? id_str : "........................................",
1499 path, ncommits_str) == -1) {
1500 err = got_error_from_errno("asprintf");
1501 header = NULL;
1502 goto done;
1504 } else if (asprintf(&header, "commit %s%s",
1505 id_str ? id_str : "........................................",
1506 ncommits_str) == -1) {
1507 err = got_error_from_errno("asprintf");
1508 header = NULL;
1509 goto done;
1511 err = format_line(&wline, &width, header, view->ncols, 0);
1512 if (err)
1513 goto done;
1515 werase(view->window);
1517 if (view_needs_focus_indication(view))
1518 wstandout(view->window);
1519 tc = get_color(colors, TOG_COLOR_COMMIT);
1520 if (tc)
1521 wattr_on(view->window,
1522 COLOR_PAIR(tc->colorpair), NULL);
1523 waddwstr(view->window, wline);
1524 if (tc)
1525 wattr_off(view->window,
1526 COLOR_PAIR(tc->colorpair), NULL);
1527 while (width < view->ncols) {
1528 waddch(view->window, ' ');
1529 width++;
1531 if (view_needs_focus_indication(view))
1532 wstandend(view->window);
1533 free(wline);
1534 if (limit <= 1)
1535 goto done;
1537 /* Grow author column size if necessary. */
1538 entry = first;
1539 ncommits = 0;
1540 while (entry) {
1541 char *author;
1542 wchar_t *wauthor;
1543 int width;
1544 if (ncommits >= limit - 1)
1545 break;
1546 author = strdup(got_object_commit_get_author(entry->commit));
1547 if (author == NULL) {
1548 err = got_error_from_errno("strdup");
1549 goto done;
1551 err = format_author(&wauthor, &width, author, COLS,
1552 date_display_cols);
1553 if (author_cols < width)
1554 author_cols = width;
1555 free(wauthor);
1556 free(author);
1557 ncommits++;
1558 entry = TAILQ_NEXT(entry, entry);
1561 entry = first;
1562 *last = first;
1563 ncommits = 0;
1564 while (entry) {
1565 if (ncommits >= limit - 1)
1566 break;
1567 if (ncommits == selected_idx)
1568 wstandout(view->window);
1569 err = draw_commit(view, entry->commit, entry->id, refs,
1570 date_display_cols, author_cols, colors);
1571 if (ncommits == selected_idx)
1572 wstandend(view->window);
1573 if (err)
1574 goto done;
1575 ncommits++;
1576 *last = entry;
1577 entry = TAILQ_NEXT(entry, entry);
1580 view_vborder(view);
1581 done:
1582 free(id_str);
1583 free(refs_str);
1584 free(ncommits_str);
1585 free(header);
1586 return err;
1589 static void
1590 scroll_up(struct tog_view *view,
1591 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1592 struct commit_queue *commits)
1594 struct commit_queue_entry *entry;
1595 int nscrolled = 0;
1597 entry = TAILQ_FIRST(&commits->head);
1598 if (*first_displayed_entry == entry)
1599 return;
1601 entry = *first_displayed_entry;
1602 while (entry && nscrolled < maxscroll) {
1603 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1604 if (entry) {
1605 *first_displayed_entry = entry;
1606 nscrolled++;
1611 static const struct got_error *
1612 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1613 pthread_cond_t *need_commits)
1615 int errcode;
1616 int max_wait = 20;
1618 halfdelay(1); /* fast refresh while loading commits */
1620 while (*commits_needed > 0) {
1621 if (*log_complete)
1622 break;
1624 /* Wake the log thread. */
1625 errcode = pthread_cond_signal(need_commits);
1626 if (errcode)
1627 return got_error_set_errno(errcode,
1628 "pthread_cond_signal");
1629 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1631 * Thread is not done yet; lose a key press
1632 * and let the user retry... this way the GUI
1633 * remains interactive while logging deep paths
1634 * with few commits in history.
1636 return NULL;
1640 return NULL;
1643 static const struct got_error *
1644 scroll_down(struct tog_view *view,
1645 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1646 struct commit_queue_entry **last_displayed_entry,
1647 struct commit_queue *commits, int *log_complete, int *commits_needed,
1648 pthread_cond_t *need_commits)
1650 const struct got_error *err = NULL;
1651 struct commit_queue_entry *pentry;
1652 int nscrolled = 0;
1654 if (*last_displayed_entry == NULL)
1655 return NULL;
1657 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1658 if (pentry == NULL && !*log_complete) {
1660 * Ask the log thread for required amount of commits
1661 * plus some amount of pre-fetching.
1663 (*commits_needed) += maxscroll + 20;
1664 err = trigger_log_thread(0, commits_needed, log_complete,
1665 need_commits);
1666 if (err)
1667 return err;
1670 do {
1671 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1672 if (pentry == NULL)
1673 break;
1675 *last_displayed_entry = pentry;
1677 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1678 if (pentry == NULL)
1679 break;
1680 *first_displayed_entry = pentry;
1681 } while (++nscrolled < maxscroll);
1683 return err;
1686 static const struct got_error *
1687 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1688 struct got_commit_object *commit, struct got_object_id *commit_id,
1689 struct tog_view *log_view, struct got_reflist_head *refs,
1690 struct got_repository *repo)
1692 const struct got_error *err;
1693 struct got_object_qid *parent_id;
1694 struct tog_view *diff_view;
1696 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1697 if (diff_view == NULL)
1698 return got_error_from_errno("view_open");
1700 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1701 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1702 commit_id, log_view, refs, repo);
1703 if (err == NULL)
1704 *new_view = diff_view;
1705 return err;
1708 static const struct got_error *
1709 tree_view_visit_subtree(struct got_tree_object *subtree,
1710 struct tog_tree_view_state *s)
1712 struct tog_parent_tree *parent;
1714 parent = calloc(1, sizeof(*parent));
1715 if (parent == NULL)
1716 return got_error_from_errno("calloc");
1718 parent->tree = s->tree;
1719 parent->first_displayed_entry = s->first_displayed_entry;
1720 parent->selected_entry = s->selected_entry;
1721 parent->selected = s->selected;
1722 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1723 s->tree = subtree;
1724 s->selected = 0;
1725 s->first_displayed_entry = NULL;
1726 return NULL;
1730 static const struct got_error *
1731 browse_commit_tree(struct tog_view **new_view, int begin_x,
1732 struct commit_queue_entry *entry, const char *path,
1733 struct got_reflist_head *refs, struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_tree_object *tree;
1737 struct tog_tree_view_state *s;
1738 struct tog_view *tree_view;
1739 char *slash, *subpath = NULL;
1740 const char *p;
1742 err = got_object_open_as_tree(&tree, repo,
1743 got_object_commit_get_tree_id(entry->commit));
1744 if (err)
1745 return err;
1747 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1748 if (tree_view == NULL)
1749 return got_error_from_errno("view_open");
1751 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1752 if (err) {
1753 got_object_tree_close(tree);
1754 return err;
1756 s = &tree_view->state.tree;
1758 *new_view = tree_view;
1760 if (got_path_is_root_dir(path))
1761 return NULL;
1763 /* Walk the path and open corresponding tree objects. */
1764 p = path;
1765 while (*p) {
1766 struct got_tree_entry *te;
1767 struct got_object_id *tree_id;
1768 char *te_name;
1770 while (p[0] == '/')
1771 p++;
1773 /* Ensure the correct subtree entry is selected. */
1774 slash = strchr(p, '/');
1775 if (slash == NULL)
1776 te_name = strdup(p);
1777 else
1778 te_name = strndup(p, slash - p);
1779 if (te_name == NULL) {
1780 err = got_error_from_errno("strndup");
1781 break;
1783 te = got_object_tree_find_entry(s->tree, te_name);
1784 if (te == NULL) {
1785 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1786 free(te_name);
1787 break;
1789 free(te_name);
1790 s->selected_entry = te;
1791 s->selected = got_tree_entry_get_index(te);
1792 if (s->tree != s->root)
1793 s->selected++; /* skip '..' */
1795 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1796 /* Jump to this file's entry. */
1797 s->first_displayed_entry = s->selected_entry;
1798 s->selected = 0;
1799 break;
1802 slash = strchr(p, '/');
1803 if (slash)
1804 subpath = strndup(path, slash - path);
1805 else
1806 subpath = strdup(path);
1807 if (subpath == NULL) {
1808 err = got_error_from_errno("strdup");
1809 break;
1812 err = got_object_id_by_path(&tree_id, repo, entry->id,
1813 subpath);
1814 if (err)
1815 break;
1817 err = got_object_open_as_tree(&tree, repo, tree_id);
1818 free(tree_id);
1819 if (err)
1820 break;
1822 err = tree_view_visit_subtree(tree, s);
1823 if (err) {
1824 got_object_tree_close(tree);
1825 break;
1827 if (slash == NULL)
1828 break;
1829 free(subpath);
1830 subpath = NULL;
1831 p = slash;
1834 free(subpath);
1835 return err;
1838 static const struct got_error *
1839 block_signals_used_by_main_thread(void)
1841 sigset_t sigset;
1842 int errcode;
1844 if (sigemptyset(&sigset) == -1)
1845 return got_error_from_errno("sigemptyset");
1847 /* tog handles SIGWINCH and SIGCONT */
1848 if (sigaddset(&sigset, SIGWINCH) == -1)
1849 return got_error_from_errno("sigaddset");
1850 if (sigaddset(&sigset, SIGCONT) == -1)
1851 return got_error_from_errno("sigaddset");
1853 /* ncurses handles SIGTSTP */
1854 if (sigaddset(&sigset, SIGTSTP) == -1)
1855 return got_error_from_errno("sigaddset");
1857 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1858 if (errcode)
1859 return got_error_set_errno(errcode, "pthread_sigmask");
1861 return NULL;
1864 static void *
1865 log_thread(void *arg)
1867 const struct got_error *err = NULL;
1868 int errcode = 0;
1869 struct tog_log_thread_args *a = arg;
1870 int done = 0;
1872 err = block_signals_used_by_main_thread();
1873 if (err)
1874 return (void *)err;
1876 while (!done && !err && !tog_sigpipe_received) {
1877 err = queue_commits(a->graph, a->commits, 1, a->repo,
1878 a->in_repo_path, a->searching, a->search_next_done,
1879 a->regex);
1880 if (err) {
1881 if (err->code != GOT_ERR_ITER_COMPLETED)
1882 return (void *)err;
1883 err = NULL;
1884 done = 1;
1885 } else if (a->commits_needed > 0)
1886 a->commits_needed--;
1888 errcode = pthread_mutex_lock(&tog_mutex);
1889 if (errcode) {
1890 err = got_error_set_errno(errcode,
1891 "pthread_mutex_lock");
1892 break;
1893 } else if (*a->quit)
1894 done = 1;
1895 else if (*a->first_displayed_entry == NULL) {
1896 *a->first_displayed_entry =
1897 TAILQ_FIRST(&a->commits->head);
1898 *a->selected_entry = *a->first_displayed_entry;
1901 if (done)
1902 a->commits_needed = 0;
1903 else if (a->commits_needed == 0) {
1904 errcode = pthread_cond_wait(&a->need_commits,
1905 &tog_mutex);
1906 if (errcode)
1907 err = got_error_set_errno(errcode,
1908 "pthread_cond_wait");
1911 errcode = pthread_mutex_unlock(&tog_mutex);
1912 if (errcode && err == NULL)
1913 err = got_error_set_errno(errcode,
1914 "pthread_mutex_unlock");
1916 a->log_complete = 1;
1917 return (void *)err;
1920 static const struct got_error *
1921 stop_log_thread(struct tog_log_view_state *s)
1923 const struct got_error *err = NULL;
1924 int errcode;
1926 if (s->thread) {
1927 s->quit = 1;
1928 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1929 if (errcode)
1930 return got_error_set_errno(errcode,
1931 "pthread_cond_signal");
1932 errcode = pthread_mutex_unlock(&tog_mutex);
1933 if (errcode)
1934 return got_error_set_errno(errcode,
1935 "pthread_mutex_unlock");
1936 errcode = pthread_join(s->thread, (void **)&err);
1937 if (errcode)
1938 return got_error_set_errno(errcode, "pthread_join");
1939 errcode = pthread_mutex_lock(&tog_mutex);
1940 if (errcode)
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_lock");
1943 s->thread = NULL;
1946 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1947 if (errcode && err == NULL)
1948 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1950 if (s->thread_args.repo) {
1951 got_repo_close(s->thread_args.repo);
1952 s->thread_args.repo = NULL;
1955 if (s->thread_args.graph) {
1956 got_commit_graph_close(s->thread_args.graph);
1957 s->thread_args.graph = NULL;
1960 return err;
1963 static const struct got_error *
1964 close_log_view(struct tog_view *view)
1966 const struct got_error *err = NULL;
1967 struct tog_log_view_state *s = &view->state.log;
1969 err = stop_log_thread(s);
1970 free_commits(&s->commits);
1971 free(s->in_repo_path);
1972 s->in_repo_path = NULL;
1973 free(s->start_id);
1974 s->start_id = NULL;
1975 return err;
1978 static const struct got_error *
1979 search_start_log_view(struct tog_view *view)
1981 struct tog_log_view_state *s = &view->state.log;
1983 s->matched_entry = NULL;
1984 s->search_entry = NULL;
1985 return NULL;
1988 static const struct got_error *
1989 search_next_log_view(struct tog_view *view)
1991 const struct got_error *err = NULL;
1992 struct tog_log_view_state *s = &view->state.log;
1993 struct commit_queue_entry *entry;
1995 if (!view->searching) {
1996 view->search_next_done = 1;
1997 return NULL;
2000 if (s->search_entry) {
2001 int errcode, ch;
2002 errcode = pthread_mutex_unlock(&tog_mutex);
2003 if (errcode)
2004 return got_error_set_errno(errcode,
2005 "pthread_mutex_unlock");
2006 ch = wgetch(view->window);
2007 errcode = pthread_mutex_lock(&tog_mutex);
2008 if (errcode)
2009 return got_error_set_errno(errcode,
2010 "pthread_mutex_lock");
2011 if (ch == KEY_BACKSPACE) {
2012 view->search_next_done = 1;
2013 return NULL;
2015 if (view->searching == TOG_SEARCH_FORWARD)
2016 entry = TAILQ_NEXT(s->search_entry, entry);
2017 else
2018 entry = TAILQ_PREV(s->search_entry,
2019 commit_queue_head, entry);
2020 } else if (s->matched_entry) {
2021 if (view->searching == TOG_SEARCH_FORWARD)
2022 entry = TAILQ_NEXT(s->selected_entry, entry);
2023 else
2024 entry = TAILQ_PREV(s->selected_entry,
2025 commit_queue_head, entry);
2026 } else {
2027 if (view->searching == TOG_SEARCH_FORWARD)
2028 entry = TAILQ_FIRST(&s->commits.head);
2029 else
2030 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2033 while (1) {
2034 int have_match = 0;
2036 if (entry == NULL) {
2037 if (s->thread_args.log_complete ||
2038 view->searching == TOG_SEARCH_BACKWARD) {
2039 view->search_next_done = 1;
2040 return NULL;
2043 * Poke the log thread for more commits and return,
2044 * allowing the main loop to make progress. Search
2045 * will resume at s->search_entry once we come back.
2047 s->thread_args.commits_needed++;
2048 return trigger_log_thread(1,
2049 &s->thread_args.commits_needed,
2050 &s->thread_args.log_complete,
2051 &s->thread_args.need_commits);
2054 err = match_commit(&have_match, entry->id, entry->commit,
2055 &view->regex);
2056 if (err)
2057 break;
2058 if (have_match) {
2059 view->search_next_done = 1;
2060 s->matched_entry = entry;
2061 break;
2064 s->search_entry = entry;
2065 if (view->searching == TOG_SEARCH_FORWARD)
2066 entry = TAILQ_NEXT(entry, entry);
2067 else
2068 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2071 if (s->matched_entry) {
2072 int cur = s->selected_entry->idx;
2073 while (cur < s->matched_entry->idx) {
2074 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2075 if (err)
2076 return err;
2077 cur++;
2079 while (cur > s->matched_entry->idx) {
2080 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2081 if (err)
2082 return err;
2083 cur--;
2087 s->search_entry = NULL;
2089 return NULL;
2092 static const struct got_error *
2093 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2094 struct got_reflist_head *refs, struct got_repository *repo,
2095 const char *head_ref_name, const char *path, int check_disk,
2096 int log_branches)
2098 const struct got_error *err = NULL;
2099 struct tog_log_view_state *s = &view->state.log;
2100 struct got_repository *thread_repo = NULL;
2101 struct got_commit_graph *thread_graph = NULL;
2102 int errcode;
2104 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2105 if (err != NULL)
2106 goto done;
2108 /* The commit queue only contains commits being displayed. */
2109 TAILQ_INIT(&s->commits.head);
2110 s->commits.ncommits = 0;
2112 s->refs = refs;
2113 s->repo = repo;
2114 s->head_ref_name = head_ref_name;
2115 s->start_id = got_object_id_dup(start_id);
2116 if (s->start_id == NULL) {
2117 err = got_error_from_errno("got_object_id_dup");
2118 goto done;
2120 s->log_branches = log_branches;
2122 SIMPLEQ_INIT(&s->colors);
2123 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2124 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2125 get_color_value("TOG_COLOR_COMMIT"));
2126 if (err)
2127 goto done;
2128 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2129 get_color_value("TOG_COLOR_AUTHOR"));
2130 if (err) {
2131 free_colors(&s->colors);
2132 goto done;
2134 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2135 get_color_value("TOG_COLOR_DATE"));
2136 if (err) {
2137 free_colors(&s->colors);
2138 goto done;
2142 view->show = show_log_view;
2143 view->input = input_log_view;
2144 view->close = close_log_view;
2145 view->search_start = search_start_log_view;
2146 view->search_next = search_next_log_view;
2148 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2149 if (err)
2150 goto done;
2151 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2152 !s->log_branches);
2153 if (err)
2154 goto done;
2155 err = got_commit_graph_iter_start(thread_graph,
2156 s->start_id, s->repo, NULL, NULL);
2157 if (err)
2158 goto done;
2160 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2161 if (errcode) {
2162 err = got_error_set_errno(errcode, "pthread_cond_init");
2163 goto done;
2166 s->thread_args.commits_needed = view->nlines;
2167 s->thread_args.graph = thread_graph;
2168 s->thread_args.commits = &s->commits;
2169 s->thread_args.in_repo_path = s->in_repo_path;
2170 s->thread_args.start_id = s->start_id;
2171 s->thread_args.repo = thread_repo;
2172 s->thread_args.log_complete = 0;
2173 s->thread_args.quit = &s->quit;
2174 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2175 s->thread_args.selected_entry = &s->selected_entry;
2176 s->thread_args.searching = &view->searching;
2177 s->thread_args.search_next_done = &view->search_next_done;
2178 s->thread_args.regex = &view->regex;
2179 done:
2180 if (err)
2181 close_log_view(view);
2182 return err;
2185 static const struct got_error *
2186 show_log_view(struct tog_view *view)
2188 struct tog_log_view_state *s = &view->state.log;
2190 if (s->thread == NULL) {
2191 int errcode = pthread_create(&s->thread, NULL, log_thread,
2192 &s->thread_args);
2193 if (errcode)
2194 return got_error_set_errno(errcode, "pthread_create");
2197 return draw_commits(view, &s->last_displayed_entry,
2198 &s->selected_entry, s->first_displayed_entry,
2199 &s->commits, s->selected, view->nlines, s->refs,
2200 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2203 static const struct got_error *
2204 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2205 struct tog_view **focus_view, struct tog_view *view, int ch)
2207 const struct got_error *err = NULL;
2208 struct tog_log_view_state *s = &view->state.log;
2209 char *parent_path, *in_repo_path = NULL;
2210 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2211 int begin_x = 0;
2212 struct got_object_id *start_id;
2214 switch (ch) {
2215 case 'q':
2216 s->quit = 1;
2217 break;
2218 case 'k':
2219 case KEY_UP:
2220 case '<':
2221 case ',':
2222 if (s->first_displayed_entry == NULL)
2223 break;
2224 if (s->selected > 0)
2225 s->selected--;
2226 else
2227 scroll_up(view, &s->first_displayed_entry, 1,
2228 &s->commits);
2229 break;
2230 case KEY_PPAGE:
2231 case CTRL('b'):
2232 if (s->first_displayed_entry == NULL)
2233 break;
2234 if (TAILQ_FIRST(&s->commits.head) ==
2235 s->first_displayed_entry) {
2236 s->selected = 0;
2237 break;
2239 scroll_up(view, &s->first_displayed_entry,
2240 view->nlines, &s->commits);
2241 break;
2242 case 'j':
2243 case KEY_DOWN:
2244 case '>':
2245 case '.':
2246 if (s->first_displayed_entry == NULL)
2247 break;
2248 if (s->selected < MIN(view->nlines - 2,
2249 s->commits.ncommits - 1)) {
2250 s->selected++;
2251 break;
2253 err = scroll_down(view, &s->first_displayed_entry, 1,
2254 &s->last_displayed_entry, &s->commits,
2255 &s->thread_args.log_complete,
2256 &s->thread_args.commits_needed,
2257 &s->thread_args.need_commits);
2258 break;
2259 case KEY_NPAGE:
2260 case CTRL('f'): {
2261 struct commit_queue_entry *first;
2262 first = s->first_displayed_entry;
2263 if (first == NULL)
2264 break;
2265 err = scroll_down(view, &s->first_displayed_entry,
2266 view->nlines, &s->last_displayed_entry,
2267 &s->commits, &s->thread_args.log_complete,
2268 &s->thread_args.commits_needed,
2269 &s->thread_args.need_commits);
2270 if (err)
2271 break;
2272 if (first == s->first_displayed_entry &&
2273 s->selected < MIN(view->nlines - 2,
2274 s->commits.ncommits - 1)) {
2275 /* can't scroll further down */
2276 s->selected = MIN(view->nlines - 2,
2277 s->commits.ncommits - 1);
2279 err = NULL;
2280 break;
2282 case KEY_RESIZE:
2283 if (s->selected > view->nlines - 2)
2284 s->selected = view->nlines - 2;
2285 if (s->selected > s->commits.ncommits - 1)
2286 s->selected = s->commits.ncommits - 1;
2287 break;
2288 case KEY_ENTER:
2289 case ' ':
2290 case '\r':
2291 if (s->selected_entry == NULL)
2292 break;
2293 if (view_is_parent_view(view))
2294 begin_x = view_split_begin_x(view->begin_x);
2295 err = open_diff_view_for_commit(&diff_view, begin_x,
2296 s->selected_entry->commit, s->selected_entry->id,
2297 view, s->refs, s->repo);
2298 if (err)
2299 break;
2300 if (view_is_parent_view(view)) {
2301 err = view_close_child(view);
2302 if (err)
2303 return err;
2304 err = view_set_child(view, diff_view);
2305 if (err) {
2306 view_close(diff_view);
2307 break;
2309 *focus_view = diff_view;
2310 view->child_focussed = 1;
2311 } else
2312 *new_view = diff_view;
2313 break;
2314 case 't':
2315 if (s->selected_entry == NULL)
2316 break;
2317 if (view_is_parent_view(view))
2318 begin_x = view_split_begin_x(view->begin_x);
2319 err = browse_commit_tree(&tree_view, begin_x,
2320 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2321 if (err)
2322 break;
2323 if (view_is_parent_view(view)) {
2324 err = view_close_child(view);
2325 if (err)
2326 return err;
2327 err = view_set_child(view, tree_view);
2328 if (err) {
2329 view_close(tree_view);
2330 break;
2332 *focus_view = tree_view;
2333 view->child_focussed = 1;
2334 } else
2335 *new_view = tree_view;
2336 break;
2337 case KEY_BACKSPACE:
2338 if (strcmp(s->in_repo_path, "/") == 0)
2339 break;
2340 parent_path = dirname(s->in_repo_path);
2341 if (parent_path && strcmp(parent_path, ".") != 0) {
2342 err = stop_log_thread(s);
2343 if (err)
2344 return err;
2345 lv = view_open(view->nlines, view->ncols,
2346 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2347 if (lv == NULL)
2348 return got_error_from_errno(
2349 "view_open");
2350 err = open_log_view(lv, s->start_id, s->refs,
2351 s->repo, s->head_ref_name, parent_path, 0,
2352 s->log_branches);
2353 if (err)
2354 return err;;
2355 if (view_is_parent_view(view))
2356 *new_view = lv;
2357 else {
2358 view_set_child(view->parent, lv);
2359 *focus_view = lv;
2361 return NULL;
2363 break;
2364 case CTRL('l'):
2365 err = stop_log_thread(s);
2366 if (err)
2367 return err;
2368 lv = view_open(view->nlines, view->ncols,
2369 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2370 if (lv == NULL)
2371 return got_error_from_errno("view_open");
2372 err = got_repo_match_object_id(&start_id, NULL,
2373 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2374 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2375 if (err) {
2376 view_close(lv);
2377 return err;
2379 in_repo_path = strdup(s->in_repo_path);
2380 if (in_repo_path == NULL) {
2381 free(start_id);
2382 view_close(lv);
2383 return got_error_from_errno("strdup");
2385 got_ref_list_free(s->refs);
2386 err = got_ref_list(s->refs, s->repo, NULL,
2387 got_ref_cmp_by_name, NULL);
2388 if (err) {
2389 free(start_id);
2390 view_close(lv);
2391 return err;
2393 err = open_log_view(lv, start_id, s->refs, s->repo,
2394 s->head_ref_name, in_repo_path, 0, s->log_branches);
2395 if (err) {
2396 free(start_id);
2397 view_close(lv);
2398 return err;;
2400 *dead_view = view;
2401 *new_view = lv;
2402 break;
2403 case 'B':
2404 s->log_branches = !s->log_branches;
2405 err = stop_log_thread(s);
2406 if (err)
2407 return err;
2408 lv = view_open(view->nlines, view->ncols,
2409 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2410 if (lv == NULL)
2411 return got_error_from_errno("view_open");
2412 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2413 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2414 if (err) {
2415 view_close(lv);
2416 return err;;
2418 *dead_view = view;
2419 *new_view = lv;
2420 break;
2421 default:
2422 break;
2425 return err;
2428 static const struct got_error *
2429 apply_unveil(const char *repo_path, const char *worktree_path)
2431 const struct got_error *error;
2433 #ifdef PROFILE
2434 if (unveil("gmon.out", "rwc") != 0)
2435 return got_error_from_errno2("unveil", "gmon.out");
2436 #endif
2437 if (repo_path && unveil(repo_path, "r") != 0)
2438 return got_error_from_errno2("unveil", repo_path);
2440 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2441 return got_error_from_errno2("unveil", worktree_path);
2443 if (unveil("/tmp", "rwc") != 0)
2444 return got_error_from_errno2("unveil", "/tmp");
2446 error = got_privsep_unveil_exec_helpers();
2447 if (error != NULL)
2448 return error;
2450 if (unveil(NULL, NULL) != 0)
2451 return got_error_from_errno("unveil");
2453 return NULL;
2456 static void
2457 init_curses(void)
2459 initscr();
2460 cbreak();
2461 halfdelay(1); /* Do fast refresh while initial view is loading. */
2462 noecho();
2463 nonl();
2464 intrflush(stdscr, FALSE);
2465 keypad(stdscr, TRUE);
2466 curs_set(0);
2467 if (getenv("TOG_COLORS") != NULL) {
2468 start_color();
2469 use_default_colors();
2471 signal(SIGWINCH, tog_sigwinch);
2472 signal(SIGPIPE, tog_sigpipe);
2473 signal(SIGCONT, tog_sigcont);
2476 static const struct got_error *
2477 cmd_log(int argc, char *argv[])
2479 const struct got_error *error;
2480 struct got_repository *repo = NULL;
2481 struct got_worktree *worktree = NULL;
2482 struct got_reflist_head refs;
2483 struct got_object_id *start_id = NULL;
2484 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2485 char *start_commit = NULL, *head_ref_name = NULL;
2486 int ch, log_branches = 0, check_disk = 1;
2487 struct tog_view *view;
2489 SIMPLEQ_INIT(&refs);
2491 #ifndef PROFILE
2492 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2493 NULL) == -1)
2494 err(1, "pledge");
2495 #endif
2497 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2498 switch (ch) {
2499 case 'b':
2500 log_branches = 1;
2501 break;
2502 case 'c':
2503 start_commit = optarg;
2504 break;
2505 case 'r':
2506 repo_path = realpath(optarg, NULL);
2507 if (repo_path == NULL)
2508 return got_error_from_errno2("realpath",
2509 optarg);
2510 break;
2511 default:
2512 usage_log();
2513 /* NOTREACHED */
2517 argc -= optind;
2518 argv += optind;
2520 cwd = getcwd(NULL, 0);
2521 if (cwd == NULL) {
2522 error = got_error_from_errno("getcwd");
2523 goto done;
2525 error = got_worktree_open(&worktree, cwd);
2526 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2527 goto done;
2528 error = NULL;
2530 if (argc == 0) {
2531 path = strdup("");
2532 if (path == NULL) {
2533 error = got_error_from_errno("strdup");
2534 goto done;
2536 } else if (argc == 1) {
2537 if (worktree) {
2538 error = got_worktree_resolve_path(&path, worktree,
2539 argv[0]);
2540 if (error)
2541 goto done;
2542 } else {
2543 path = strdup(argv[0]);
2544 if (path == NULL) {
2545 error = got_error_from_errno("strdup");
2546 goto done;
2549 } else
2550 usage_log();
2552 if (repo_path == NULL) {
2553 if (worktree)
2554 repo_path = strdup(
2555 got_worktree_get_repo_path(worktree));
2556 else
2557 repo_path = strdup(cwd);
2559 if (repo_path == NULL) {
2560 error = got_error_from_errno("strdup");
2561 goto done;
2564 init_curses();
2566 error = got_repo_open(&repo, repo_path, NULL);
2567 if (error != NULL)
2568 goto done;
2570 error = apply_unveil(got_repo_get_path(repo),
2571 worktree ? got_worktree_get_root_path(worktree) : NULL);
2572 if (error)
2573 goto done;
2575 if (start_commit == NULL)
2576 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2577 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2578 GOT_OBJ_TYPE_COMMIT, 1, repo);
2579 else
2580 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2581 GOT_OBJ_TYPE_COMMIT, 1, repo);
2582 if (error != NULL)
2583 goto done;
2585 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2586 if (error)
2587 goto done;
2589 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2590 if (view == NULL) {
2591 error = got_error_from_errno("view_open");
2592 goto done;
2594 if (worktree) {
2595 const char *prefix = got_worktree_get_path_prefix(worktree);
2596 char *p;
2597 if (asprintf(&p, "%s%s%s", prefix,
2598 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2599 error = got_error_from_errno("asprintf");
2600 goto done;
2602 free(path);
2603 path = p;
2604 check_disk = 0;
2606 head_ref_name = strdup(
2607 got_worktree_get_head_ref_name(worktree));
2608 if (head_ref_name == NULL) {
2609 error = got_error_from_errno("strdup");
2610 goto done;
2613 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2614 path, check_disk, log_branches);
2615 if (error)
2616 goto done;
2617 if (worktree) {
2618 /* Release work tree lock. */
2619 got_worktree_close(worktree);
2620 worktree = NULL;
2622 error = view_loop(view);
2623 done:
2624 free(repo_path);
2625 free(cwd);
2626 free(path);
2627 free(start_id);
2628 free(head_ref_name);
2629 if (repo)
2630 got_repo_close(repo);
2631 if (worktree)
2632 got_worktree_close(worktree);
2633 got_ref_list_free(&refs);
2634 return error;
2637 __dead static void
2638 usage_diff(void)
2640 endwin();
2641 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2642 getprogname());
2643 exit(1);
2646 static char *
2647 parse_next_line(FILE *f, size_t *len)
2649 char *line;
2650 size_t linelen;
2651 size_t lineno;
2652 const char delim[3] = { '\0', '\0', '\0'};
2654 line = fparseln(f, &linelen, &lineno, delim, 0);
2655 if (len)
2656 *len = linelen;
2657 return line;
2660 static int
2661 match_line(const char *line, regex_t *regex)
2663 regmatch_t regmatch;
2665 return regexec(regex, line, 1, &regmatch, 0) == 0;
2668 struct tog_color *
2669 match_color(struct tog_colors *colors, const char *line)
2671 struct tog_color *tc = NULL;
2673 SIMPLEQ_FOREACH(tc, colors, entry) {
2674 if (match_line(line, &tc->regex))
2675 return tc;
2678 return NULL;
2681 static const struct got_error *
2682 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2683 int *last_displayed_line, int *eof, int max_lines, char *header,
2684 struct tog_colors *colors)
2686 const struct got_error *err;
2687 int nlines = 0, nprinted = 0;
2688 char *line;
2689 struct tog_color *tc;
2690 size_t len;
2691 wchar_t *wline;
2692 int width;
2694 rewind(f);
2695 werase(view->window);
2697 if (header) {
2698 err = format_line(&wline, &width, header, view->ncols, 0);
2699 if (err) {
2700 return err;
2703 if (view_needs_focus_indication(view))
2704 wstandout(view->window);
2705 waddwstr(view->window, wline);
2706 if (view_needs_focus_indication(view))
2707 wstandend(view->window);
2708 if (width <= view->ncols - 1)
2709 waddch(view->window, '\n');
2711 if (max_lines <= 1)
2712 return NULL;
2713 max_lines--;
2716 *eof = 0;
2717 while (nprinted < max_lines) {
2718 line = parse_next_line(f, &len);
2719 if (line == NULL) {
2720 *eof = 1;
2721 break;
2723 if (++nlines < *first_displayed_line) {
2724 free(line);
2725 continue;
2728 err = format_line(&wline, &width, line, view->ncols, 0);
2729 if (err) {
2730 free(line);
2731 return err;
2734 tc = match_color(colors, line);
2735 if (tc)
2736 wattr_on(view->window,
2737 COLOR_PAIR(tc->colorpair), NULL);
2738 waddwstr(view->window, wline);
2739 if (tc)
2740 wattr_off(view->window,
2741 COLOR_PAIR(tc->colorpair), NULL);
2742 if (width <= view->ncols - 1)
2743 waddch(view->window, '\n');
2744 if (++nprinted == 1)
2745 *first_displayed_line = nlines;
2746 free(line);
2747 free(wline);
2748 wline = NULL;
2750 *last_displayed_line = nlines;
2752 view_vborder(view);
2754 if (*eof) {
2755 while (nprinted < view->nlines) {
2756 waddch(view->window, '\n');
2757 nprinted++;
2760 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2761 if (err) {
2762 return err;
2765 wstandout(view->window);
2766 waddwstr(view->window, wline);
2767 wstandend(view->window);
2770 return NULL;
2773 static char *
2774 get_datestr(time_t *time, char *datebuf)
2776 struct tm mytm, *tm;
2777 char *p, *s;
2779 tm = gmtime_r(time, &mytm);
2780 if (tm == NULL)
2781 return NULL;
2782 s = asctime_r(tm, datebuf);
2783 if (s == NULL)
2784 return NULL;
2785 p = strchr(s, '\n');
2786 if (p)
2787 *p = '\0';
2788 return s;
2791 static const struct got_error *
2792 write_commit_info(struct got_object_id *commit_id,
2793 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2795 const struct got_error *err = NULL;
2796 char datebuf[26], *datestr;
2797 struct got_commit_object *commit;
2798 char *id_str = NULL, *logmsg = NULL;
2799 time_t committer_time;
2800 const char *author, *committer;
2801 char *refs_str = NULL;
2803 if (refs) {
2804 err = build_refs_str(&refs_str, refs, commit_id, repo);
2805 if (err)
2806 return err;
2809 err = got_object_open_as_commit(&commit, repo, commit_id);
2810 if (err)
2811 return err;
2813 err = got_object_id_str(&id_str, commit_id);
2814 if (err) {
2815 err = got_error_from_errno("got_object_id_str");
2816 goto done;
2819 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2820 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2821 err = got_error_from_errno("fprintf");
2822 goto done;
2824 if (fprintf(outfile, "from: %s\n",
2825 got_object_commit_get_author(commit)) < 0) {
2826 err = got_error_from_errno("fprintf");
2827 goto done;
2829 committer_time = got_object_commit_get_committer_time(commit);
2830 datestr = get_datestr(&committer_time, datebuf);
2831 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2832 err = got_error_from_errno("fprintf");
2833 goto done;
2835 author = got_object_commit_get_author(commit);
2836 committer = got_object_commit_get_committer(commit);
2837 if (strcmp(author, committer) != 0 &&
2838 fprintf(outfile, "via: %s\n", committer) < 0) {
2839 err = got_error_from_errno("fprintf");
2840 goto done;
2842 err = got_object_commit_get_logmsg(&logmsg, commit);
2843 if (err)
2844 goto done;
2845 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2846 err = got_error_from_errno("fprintf");
2847 goto done;
2849 done:
2850 free(id_str);
2851 free(logmsg);
2852 free(refs_str);
2853 got_object_commit_close(commit);
2854 return err;
2857 static const struct got_error *
2858 create_diff(struct tog_diff_view_state *s)
2860 const struct got_error *err = NULL;
2861 FILE *f = NULL;
2862 int obj_type;
2864 f = got_opentemp();
2865 if (f == NULL) {
2866 err = got_error_from_errno("got_opentemp");
2867 goto done;
2869 if (s->f && fclose(s->f) != 0) {
2870 err = got_error_from_errno("fclose");
2871 goto done;
2873 s->f = f;
2875 if (s->id1)
2876 err = got_object_get_type(&obj_type, s->repo, s->id1);
2877 else
2878 err = got_object_get_type(&obj_type, s->repo, s->id2);
2879 if (err)
2880 goto done;
2882 switch (obj_type) {
2883 case GOT_OBJ_TYPE_BLOB:
2884 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2885 s->diff_context, 0, s->repo, f);
2886 break;
2887 case GOT_OBJ_TYPE_TREE:
2888 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2889 s->diff_context, 0, s->repo, f);
2890 break;
2891 case GOT_OBJ_TYPE_COMMIT: {
2892 const struct got_object_id_queue *parent_ids;
2893 struct got_object_qid *pid;
2894 struct got_commit_object *commit2;
2896 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2897 if (err)
2898 break;
2899 /* Show commit info if we're diffing to a parent/root commit. */
2900 if (s->id1 == NULL)
2901 write_commit_info(s->id2, s->refs, s->repo, f);
2902 else {
2903 parent_ids = got_object_commit_get_parent_ids(commit2);
2904 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2905 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2906 write_commit_info(s->id2, s->refs,
2907 s->repo, f);
2908 break;
2912 got_object_commit_close(commit2);
2914 err = got_diff_objects_as_commits(s->id1, s->id2,
2915 s->diff_context, 0, s->repo, f);
2916 break;
2918 default:
2919 err = got_error(GOT_ERR_OBJ_TYPE);
2920 break;
2922 done:
2923 if (f && fflush(f) != 0 && err == NULL)
2924 err = got_error_from_errno("fflush");
2925 return err;
2928 static void
2929 diff_view_indicate_progress(struct tog_view *view)
2931 mvwaddstr(view->window, 0, 0, "diffing...");
2932 update_panels();
2933 doupdate();
2936 static const struct got_error *
2937 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2938 struct got_object_id *id2, struct tog_view *log_view,
2939 struct got_reflist_head *refs, struct got_repository *repo)
2941 const struct got_error *err;
2942 struct tog_diff_view_state *s = &view->state.diff;
2944 if (id1 != NULL && id2 != NULL) {
2945 int type1, type2;
2946 err = got_object_get_type(&type1, repo, id1);
2947 if (err)
2948 return err;
2949 err = got_object_get_type(&type2, repo, id2);
2950 if (err)
2951 return err;
2953 if (type1 != type2)
2954 return got_error(GOT_ERR_OBJ_TYPE);
2957 if (id1) {
2958 s->id1 = got_object_id_dup(id1);
2959 if (s->id1 == NULL)
2960 return got_error_from_errno("got_object_id_dup");
2961 } else
2962 s->id1 = NULL;
2964 s->id2 = got_object_id_dup(id2);
2965 if (s->id2 == NULL) {
2966 free(s->id1);
2967 s->id1 = NULL;
2968 return got_error_from_errno("got_object_id_dup");
2970 s->f = NULL;
2971 s->first_displayed_line = 1;
2972 s->last_displayed_line = view->nlines;
2973 s->diff_context = 3;
2974 s->log_view = log_view;
2975 s->repo = repo;
2976 s->refs = refs;
2977 SIMPLEQ_INIT(&s->colors);
2979 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2980 err = add_color(&s->colors,
2981 "^-", TOG_COLOR_DIFF_MINUS,
2982 get_color_value("TOG_COLOR_DIFF_MINUS"));
2983 if (err)
2984 return err;
2985 err = add_color(&s->colors, "^\\+",
2986 TOG_COLOR_DIFF_PLUS,
2987 get_color_value("TOG_COLOR_DIFF_PLUS"));
2988 if (err) {
2989 free_colors(&s->colors);
2990 return err;
2992 err = add_color(&s->colors,
2993 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2994 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2995 if (err) {
2996 free_colors(&s->colors);
2997 return err;
3000 err = add_color(&s->colors,
3001 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3002 get_color_value("TOG_COLOR_DIFF_META"));
3003 if (err) {
3004 free_colors(&s->colors);
3005 return err;
3008 err = add_color(&s->colors,
3009 "^(from|via): ", TOG_COLOR_AUTHOR,
3010 get_color_value("TOG_COLOR_AUTHOR"));
3011 if (err) {
3012 free_colors(&s->colors);
3013 return err;
3016 err = add_color(&s->colors,
3017 "^date: ", TOG_COLOR_DATE,
3018 get_color_value("TOG_COLOR_DATE"));
3019 if (err) {
3020 free_colors(&s->colors);
3021 return err;
3025 if (log_view && view_is_splitscreen(view))
3026 show_log_view(log_view); /* draw vborder */
3027 diff_view_indicate_progress(view);
3029 err = create_diff(s);
3030 if (err) {
3031 free(s->id1);
3032 s->id1 = NULL;
3033 free(s->id2);
3034 s->id2 = NULL;
3035 return err;
3038 view->show = show_diff_view;
3039 view->input = input_diff_view;
3040 view->close = close_diff_view;
3042 return NULL;
3045 static const struct got_error *
3046 close_diff_view(struct tog_view *view)
3048 const struct got_error *err = NULL;
3049 struct tog_diff_view_state *s = &view->state.diff;
3051 free(s->id1);
3052 s->id1 = NULL;
3053 free(s->id2);
3054 s->id2 = NULL;
3055 if (s->f && fclose(s->f) == EOF)
3056 err = got_error_from_errno("fclose");
3057 free_colors(&s->colors);
3058 return err;
3061 static const struct got_error *
3062 show_diff_view(struct tog_view *view)
3064 const struct got_error *err;
3065 struct tog_diff_view_state *s = &view->state.diff;
3066 char *id_str1 = NULL, *id_str2, *header;
3068 if (s->id1) {
3069 err = got_object_id_str(&id_str1, s->id1);
3070 if (err)
3071 return err;
3073 err = got_object_id_str(&id_str2, s->id2);
3074 if (err)
3075 return err;
3077 if (asprintf(&header, "diff %s %s",
3078 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3079 err = got_error_from_errno("asprintf");
3080 free(id_str1);
3081 free(id_str2);
3082 return err;
3084 free(id_str1);
3085 free(id_str2);
3087 return draw_file(view, s->f, &s->first_displayed_line,
3088 &s->last_displayed_line, &s->eof, view->nlines,
3089 header, &s->colors);
3092 static const struct got_error *
3093 set_selected_commit(struct tog_diff_view_state *s,
3094 struct commit_queue_entry *entry)
3096 const struct got_error *err;
3097 const struct got_object_id_queue *parent_ids;
3098 struct got_commit_object *selected_commit;
3099 struct got_object_qid *pid;
3101 free(s->id2);
3102 s->id2 = got_object_id_dup(entry->id);
3103 if (s->id2 == NULL)
3104 return got_error_from_errno("got_object_id_dup");
3106 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3107 if (err)
3108 return err;
3109 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3110 free(s->id1);
3111 pid = SIMPLEQ_FIRST(parent_ids);
3112 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3113 got_object_commit_close(selected_commit);
3114 return NULL;
3117 static const struct got_error *
3118 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3119 struct tog_view **focus_view, struct tog_view *view, int ch)
3121 const struct got_error *err = NULL;
3122 struct tog_diff_view_state *s = &view->state.diff;
3123 struct tog_log_view_state *ls;
3124 struct commit_queue_entry *entry;
3125 int i;
3127 switch (ch) {
3128 case 'k':
3129 case KEY_UP:
3130 if (s->first_displayed_line > 1)
3131 s->first_displayed_line--;
3132 break;
3133 case KEY_PPAGE:
3134 case CTRL('b'):
3135 if (s->first_displayed_line == 1)
3136 break;
3137 i = 0;
3138 while (i++ < view->nlines - 1 &&
3139 s->first_displayed_line > 1)
3140 s->first_displayed_line--;
3141 break;
3142 case 'j':
3143 case KEY_DOWN:
3144 if (!s->eof)
3145 s->first_displayed_line++;
3146 break;
3147 case KEY_NPAGE:
3148 case CTRL('f'):
3149 case ' ':
3150 if (s->eof)
3151 break;
3152 i = 0;
3153 while (!s->eof && i++ < view->nlines - 1) {
3154 char *line;
3155 line = parse_next_line(s->f, NULL);
3156 s->first_displayed_line++;
3157 if (line == NULL)
3158 break;
3160 break;
3161 case '[':
3162 if (s->diff_context > 0) {
3163 s->diff_context--;
3164 diff_view_indicate_progress(view);
3165 err = create_diff(s);
3167 break;
3168 case ']':
3169 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3170 s->diff_context++;
3171 diff_view_indicate_progress(view);
3172 err = create_diff(s);
3174 break;
3175 case '<':
3176 case ',':
3177 if (s->log_view == NULL)
3178 break;
3179 ls = &s->log_view->state.log;
3180 entry = TAILQ_PREV(ls->selected_entry,
3181 commit_queue_head, entry);
3182 if (entry == NULL)
3183 break;
3185 err = input_log_view(NULL, NULL, NULL, s->log_view,
3186 KEY_UP);
3187 if (err)
3188 break;
3190 err = set_selected_commit(s, entry);
3191 if (err)
3192 break;
3194 s->first_displayed_line = 1;
3195 s->last_displayed_line = view->nlines;
3197 diff_view_indicate_progress(view);
3198 err = create_diff(s);
3199 break;
3200 case '>':
3201 case '.':
3202 if (s->log_view == NULL)
3203 break;
3204 ls = &s->log_view->state.log;
3206 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3207 ls->thread_args.commits_needed++;
3209 /* Display "loading..." in log view. */
3210 show_log_view(s->log_view);
3211 update_panels();
3212 doupdate();
3214 err = trigger_log_thread(1 /* load_all */,
3215 &ls->thread_args.commits_needed,
3216 &ls->thread_args.log_complete,
3217 &ls->thread_args.need_commits);
3218 if (err)
3219 break;
3221 err = input_log_view(NULL, NULL, NULL, s->log_view,
3222 KEY_DOWN);
3223 if (err)
3224 break;
3226 entry = TAILQ_NEXT(ls->selected_entry, entry);
3227 if (entry == NULL)
3228 break;
3230 err = set_selected_commit(s, entry);
3231 if (err)
3232 break;
3234 s->first_displayed_line = 1;
3235 s->last_displayed_line = view->nlines;
3237 diff_view_indicate_progress(view);
3238 err = create_diff(s);
3239 break;
3240 default:
3241 break;
3244 return err;
3247 static const struct got_error *
3248 cmd_diff(int argc, char *argv[])
3250 const struct got_error *error = NULL;
3251 struct got_repository *repo = NULL;
3252 struct got_reflist_head refs;
3253 struct got_object_id *id1 = NULL, *id2 = NULL;
3254 char *repo_path = NULL;
3255 char *id_str1 = NULL, *id_str2 = NULL;
3256 int ch;
3257 struct tog_view *view;
3259 SIMPLEQ_INIT(&refs);
3261 #ifndef PROFILE
3262 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3263 NULL) == -1)
3264 err(1, "pledge");
3265 #endif
3267 while ((ch = getopt(argc, argv, "")) != -1) {
3268 switch (ch) {
3269 default:
3270 usage_diff();
3271 /* NOTREACHED */
3275 argc -= optind;
3276 argv += optind;
3278 if (argc == 0) {
3279 usage_diff(); /* TODO show local worktree changes */
3280 } else if (argc == 2) {
3281 repo_path = getcwd(NULL, 0);
3282 if (repo_path == NULL)
3283 return got_error_from_errno("getcwd");
3284 id_str1 = argv[0];
3285 id_str2 = argv[1];
3286 } else if (argc == 3) {
3287 repo_path = realpath(argv[0], NULL);
3288 if (repo_path == NULL)
3289 return got_error_from_errno2("realpath", argv[0]);
3290 id_str1 = argv[1];
3291 id_str2 = argv[2];
3292 } else
3293 usage_diff();
3295 init_curses();
3297 error = got_repo_open(&repo, repo_path, NULL);
3298 if (error)
3299 goto done;
3301 error = apply_unveil(got_repo_get_path(repo), NULL);
3302 if (error)
3303 goto done;
3305 error = got_repo_match_object_id_prefix(&id1, id_str1,
3306 GOT_OBJ_TYPE_ANY, repo);
3307 if (error)
3308 goto done;
3310 error = got_repo_match_object_id_prefix(&id2, id_str2,
3311 GOT_OBJ_TYPE_ANY, repo);
3312 if (error)
3313 goto done;
3315 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3316 if (error)
3317 goto done;
3319 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3320 if (view == NULL) {
3321 error = got_error_from_errno("view_open");
3322 goto done;
3324 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3325 if (error)
3326 goto done;
3327 error = view_loop(view);
3328 done:
3329 free(repo_path);
3330 if (repo)
3331 got_repo_close(repo);
3332 got_ref_list_free(&refs);
3333 return error;
3336 __dead static void
3337 usage_blame(void)
3339 endwin();
3340 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3341 getprogname());
3342 exit(1);
3345 struct tog_blame_line {
3346 int annotated;
3347 struct got_object_id *id;
3350 static const struct got_error *
3351 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3352 const char *path, struct tog_blame_line *lines, int nlines,
3353 int blame_complete, int selected_line, int *first_displayed_line,
3354 int *last_displayed_line, int *eof, int max_lines,
3355 struct tog_colors *colors)
3357 const struct got_error *err;
3358 int lineno = 0, nprinted = 0;
3359 char *line;
3360 size_t len;
3361 wchar_t *wline;
3362 int width;
3363 struct tog_blame_line *blame_line;
3364 struct got_object_id *prev_id = NULL;
3365 char *id_str;
3366 struct tog_color *tc;
3368 err = got_object_id_str(&id_str, id);
3369 if (err)
3370 return err;
3372 rewind(f);
3373 werase(view->window);
3375 if (asprintf(&line, "commit %s", id_str) == -1) {
3376 err = got_error_from_errno("asprintf");
3377 free(id_str);
3378 return err;
3381 err = format_line(&wline, &width, line, view->ncols, 0);
3382 free(line);
3383 line = NULL;
3384 if (err)
3385 return err;
3386 if (view_needs_focus_indication(view))
3387 wstandout(view->window);
3388 tc = get_color(colors, TOG_COLOR_COMMIT);
3389 if (tc)
3390 wattr_on(view->window,
3391 COLOR_PAIR(tc->colorpair), NULL);
3392 waddwstr(view->window, wline);
3393 if (tc)
3394 wattr_off(view->window,
3395 COLOR_PAIR(tc->colorpair), NULL);
3396 if (view_needs_focus_indication(view))
3397 wstandend(view->window);
3398 free(wline);
3399 wline = NULL;
3400 if (width < view->ncols - 1)
3401 waddch(view->window, '\n');
3403 if (asprintf(&line, "[%d/%d] %s%s",
3404 *first_displayed_line - 1 + selected_line, nlines,
3405 blame_complete ? "" : "annotating... ", path) == -1) {
3406 free(id_str);
3407 return got_error_from_errno("asprintf");
3409 free(id_str);
3410 err = format_line(&wline, &width, line, view->ncols, 0);
3411 free(line);
3412 line = NULL;
3413 if (err)
3414 return err;
3415 waddwstr(view->window, wline);
3416 free(wline);
3417 wline = NULL;
3418 if (width < view->ncols - 1)
3419 waddch(view->window, '\n');
3421 *eof = 0;
3422 while (nprinted < max_lines - 2) {
3423 line = parse_next_line(f, &len);
3424 if (line == NULL) {
3425 *eof = 1;
3426 break;
3428 if (++lineno < *first_displayed_line) {
3429 free(line);
3430 continue;
3433 if (view->ncols <= 9) {
3434 width = 9;
3435 wline = wcsdup(L"");
3436 if (wline == NULL)
3437 err = got_error_from_errno("wcsdup");
3438 } else {
3439 err = format_line(&wline, &width, line,
3440 view->ncols - 9, 9);
3441 width += 9;
3443 if (err) {
3444 free(line);
3445 return err;
3448 if (view->focussed && nprinted == selected_line - 1)
3449 wstandout(view->window);
3451 if (nlines > 0) {
3452 blame_line = &lines[lineno - 1];
3453 if (blame_line->annotated && prev_id &&
3454 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3455 !(view->focussed &&
3456 nprinted == selected_line - 1)) {
3457 waddstr(view->window, " ");
3458 } else if (blame_line->annotated) {
3459 char *id_str;
3460 err = got_object_id_str(&id_str, blame_line->id);
3461 if (err) {
3462 free(line);
3463 free(wline);
3464 return err;
3466 tc = get_color(colors, TOG_COLOR_COMMIT);
3467 if (tc)
3468 wattr_on(view->window,
3469 COLOR_PAIR(tc->colorpair), NULL);
3470 wprintw(view->window, "%.8s", id_str);
3471 if (tc)
3472 wattr_off(view->window,
3473 COLOR_PAIR(tc->colorpair), NULL);
3474 free(id_str);
3475 prev_id = blame_line->id;
3476 } else {
3477 waddstr(view->window, "........");
3478 prev_id = NULL;
3480 } else {
3481 waddstr(view->window, "........");
3482 prev_id = NULL;
3485 if (view->focussed && nprinted == selected_line - 1)
3486 wstandend(view->window);
3487 waddstr(view->window, " ");
3489 waddwstr(view->window, wline);
3490 if (width <= view->ncols - 1)
3491 waddch(view->window, '\n');
3492 if (++nprinted == 1)
3493 *first_displayed_line = lineno;
3494 free(line);
3495 free(wline);
3496 wline = NULL;
3498 *last_displayed_line = lineno;
3500 view_vborder(view);
3502 return NULL;
3505 static const struct got_error *
3506 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3508 const struct got_error *err = NULL;
3509 struct tog_blame_cb_args *a = arg;
3510 struct tog_blame_line *line;
3511 int errcode;
3513 if (nlines != a->nlines ||
3514 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3515 return got_error(GOT_ERR_RANGE);
3517 errcode = pthread_mutex_lock(&tog_mutex);
3518 if (errcode)
3519 return got_error_set_errno(errcode, "pthread_mutex_lock");
3521 if (*a->quit) { /* user has quit the blame view */
3522 err = got_error(GOT_ERR_ITER_COMPLETED);
3523 goto done;
3526 if (lineno == -1)
3527 goto done; /* no change in this commit */
3529 line = &a->lines[lineno - 1];
3530 if (line->annotated)
3531 goto done;
3533 line->id = got_object_id_dup(id);
3534 if (line->id == NULL) {
3535 err = got_error_from_errno("got_object_id_dup");
3536 goto done;
3538 line->annotated = 1;
3539 done:
3540 errcode = pthread_mutex_unlock(&tog_mutex);
3541 if (errcode)
3542 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3543 return err;
3546 static void *
3547 blame_thread(void *arg)
3549 const struct got_error *err;
3550 struct tog_blame_thread_args *ta = arg;
3551 struct tog_blame_cb_args *a = ta->cb_args;
3552 int errcode;
3554 err = block_signals_used_by_main_thread();
3555 if (err)
3556 return (void *)err;
3558 err = got_blame(ta->path, a->commit_id, ta->repo,
3559 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3560 if (err && err->code == GOT_ERR_CANCELLED)
3561 err = NULL;
3563 errcode = pthread_mutex_lock(&tog_mutex);
3564 if (errcode)
3565 return (void *)got_error_set_errno(errcode,
3566 "pthread_mutex_lock");
3568 got_repo_close(ta->repo);
3569 ta->repo = NULL;
3570 *ta->complete = 1;
3572 errcode = pthread_mutex_unlock(&tog_mutex);
3573 if (errcode && err == NULL)
3574 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3576 return (void *)err;
3579 static struct got_object_id *
3580 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3581 int first_displayed_line, int selected_line)
3583 struct tog_blame_line *line;
3585 if (nlines <= 0)
3586 return NULL;
3588 line = &lines[first_displayed_line - 1 + selected_line - 1];
3589 if (!line->annotated)
3590 return NULL;
3592 return line->id;
3595 static const struct got_error *
3596 stop_blame(struct tog_blame *blame)
3598 const struct got_error *err = NULL;
3599 int i;
3601 if (blame->thread) {
3602 int errcode;
3603 errcode = pthread_mutex_unlock(&tog_mutex);
3604 if (errcode)
3605 return got_error_set_errno(errcode,
3606 "pthread_mutex_unlock");
3607 errcode = pthread_join(blame->thread, (void **)&err);
3608 if (errcode)
3609 return got_error_set_errno(errcode, "pthread_join");
3610 errcode = pthread_mutex_lock(&tog_mutex);
3611 if (errcode)
3612 return got_error_set_errno(errcode,
3613 "pthread_mutex_lock");
3614 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3615 err = NULL;
3616 blame->thread = NULL;
3618 if (blame->thread_args.repo) {
3619 got_repo_close(blame->thread_args.repo);
3620 blame->thread_args.repo = NULL;
3622 if (blame->f) {
3623 if (fclose(blame->f) != 0 && err == NULL)
3624 err = got_error_from_errno("fclose");
3625 blame->f = NULL;
3627 if (blame->lines) {
3628 for (i = 0; i < blame->nlines; i++)
3629 free(blame->lines[i].id);
3630 free(blame->lines);
3631 blame->lines = NULL;
3633 free(blame->cb_args.commit_id);
3634 blame->cb_args.commit_id = NULL;
3636 return err;
3639 static const struct got_error *
3640 cancel_blame_view(void *arg)
3642 const struct got_error *err = NULL;
3643 int *done = arg;
3644 int errcode;
3646 errcode = pthread_mutex_lock(&tog_mutex);
3647 if (errcode)
3648 return got_error_set_errno(errcode,
3649 "pthread_mutex_unlock");
3651 if (*done)
3652 err = got_error(GOT_ERR_CANCELLED);
3654 errcode = pthread_mutex_unlock(&tog_mutex);
3655 if (errcode)
3656 return got_error_set_errno(errcode,
3657 "pthread_mutex_lock");
3659 return err;
3662 static const struct got_error *
3663 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3664 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3665 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3666 struct got_repository *repo)
3668 const struct got_error *err = NULL;
3669 struct got_blob_object *blob = NULL;
3670 struct got_repository *thread_repo = NULL;
3671 struct got_object_id *obj_id = NULL;
3672 int obj_type;
3674 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3675 if (err)
3676 return err;
3678 err = got_object_get_type(&obj_type, repo, obj_id);
3679 if (err)
3680 goto done;
3682 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3683 err = got_error(GOT_ERR_OBJ_TYPE);
3684 goto done;
3687 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3688 if (err)
3689 goto done;
3690 blame->f = got_opentemp();
3691 if (blame->f == NULL) {
3692 err = got_error_from_errno("got_opentemp");
3693 goto done;
3695 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3696 &blame->line_offsets, blame->f, blob);
3697 if (err || blame->nlines == 0)
3698 goto done;
3700 /* Don't include \n at EOF in the blame line count. */
3701 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3702 blame->nlines--;
3704 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3705 if (blame->lines == NULL) {
3706 err = got_error_from_errno("calloc");
3707 goto done;
3710 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3711 if (err)
3712 goto done;
3714 blame->cb_args.view = view;
3715 blame->cb_args.lines = blame->lines;
3716 blame->cb_args.nlines = blame->nlines;
3717 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3718 if (blame->cb_args.commit_id == NULL) {
3719 err = got_error_from_errno("got_object_id_dup");
3720 goto done;
3722 blame->cb_args.quit = done;
3724 blame->thread_args.path = path;
3725 blame->thread_args.repo = thread_repo;
3726 blame->thread_args.cb_args = &blame->cb_args;
3727 blame->thread_args.complete = blame_complete;
3728 blame->thread_args.cancel_cb = cancel_blame_view;
3729 blame->thread_args.cancel_arg = done;
3730 *blame_complete = 0;
3732 done:
3733 if (blob)
3734 got_object_blob_close(blob);
3735 free(obj_id);
3736 if (err)
3737 stop_blame(blame);
3738 return err;
3741 static const struct got_error *
3742 open_blame_view(struct tog_view *view, char *path,
3743 struct got_object_id *commit_id, struct got_reflist_head *refs,
3744 struct got_repository *repo)
3746 const struct got_error *err = NULL;
3747 struct tog_blame_view_state *s = &view->state.blame;
3749 SIMPLEQ_INIT(&s->blamed_commits);
3751 s->path = strdup(path);
3752 if (s->path == NULL)
3753 return got_error_from_errno("strdup");
3755 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3756 if (err) {
3757 free(s->path);
3758 return err;
3761 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3762 s->first_displayed_line = 1;
3763 s->last_displayed_line = view->nlines;
3764 s->selected_line = 1;
3765 s->blame_complete = 0;
3766 s->repo = repo;
3767 s->refs = refs;
3768 s->commit_id = commit_id;
3769 memset(&s->blame, 0, sizeof(s->blame));
3771 SIMPLEQ_INIT(&s->colors);
3772 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3773 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3774 get_color_value("TOG_COLOR_COMMIT"));
3775 if (err)
3776 return err;
3779 view->show = show_blame_view;
3780 view->input = input_blame_view;
3781 view->close = close_blame_view;
3782 view->search_start = search_start_blame_view;
3783 view->search_next = search_next_blame_view;
3785 return run_blame(&s->blame, view, &s->blame_complete,
3786 &s->first_displayed_line, &s->last_displayed_line,
3787 &s->selected_line, &s->done, &s->eof, s->path,
3788 s->blamed_commit->id, s->repo);
3791 static const struct got_error *
3792 close_blame_view(struct tog_view *view)
3794 const struct got_error *err = NULL;
3795 struct tog_blame_view_state *s = &view->state.blame;
3797 if (s->blame.thread)
3798 err = stop_blame(&s->blame);
3800 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3801 struct got_object_qid *blamed_commit;
3802 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3803 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3804 got_object_qid_free(blamed_commit);
3807 free(s->path);
3808 free_colors(&s->colors);
3810 return err;
3813 static const struct got_error *
3814 search_start_blame_view(struct tog_view *view)
3816 struct tog_blame_view_state *s = &view->state.blame;
3818 s->matched_line = 0;
3819 return NULL;
3822 static const struct got_error *
3823 search_next_blame_view(struct tog_view *view)
3825 struct tog_blame_view_state *s = &view->state.blame;
3826 int lineno;
3828 if (!view->searching) {
3829 view->search_next_done = 1;
3830 return NULL;
3833 if (s->matched_line) {
3834 if (view->searching == TOG_SEARCH_FORWARD)
3835 lineno = s->matched_line + 1;
3836 else
3837 lineno = s->matched_line - 1;
3838 } else {
3839 if (view->searching == TOG_SEARCH_FORWARD)
3840 lineno = 1;
3841 else
3842 lineno = s->blame.nlines;
3845 while (1) {
3846 char *line = NULL;
3847 off_t offset;
3848 size_t len;
3850 if (lineno <= 0 || lineno > s->blame.nlines) {
3851 if (s->matched_line == 0) {
3852 view->search_next_done = 1;
3853 free(line);
3854 break;
3857 if (view->searching == TOG_SEARCH_FORWARD)
3858 lineno = 1;
3859 else
3860 lineno = s->blame.nlines;
3863 offset = s->blame.line_offsets[lineno - 1];
3864 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3865 free(line);
3866 return got_error_from_errno("fseeko");
3868 free(line);
3869 line = parse_next_line(s->blame.f, &len);
3870 if (line && match_line(line, &view->regex)) {
3871 view->search_next_done = 1;
3872 s->matched_line = lineno;
3873 free(line);
3874 break;
3876 free(line);
3877 if (view->searching == TOG_SEARCH_FORWARD)
3878 lineno++;
3879 else
3880 lineno--;
3883 if (s->matched_line) {
3884 s->first_displayed_line = s->matched_line;
3885 s->selected_line = 1;
3888 return NULL;
3891 static const struct got_error *
3892 show_blame_view(struct tog_view *view)
3894 const struct got_error *err = NULL;
3895 struct tog_blame_view_state *s = &view->state.blame;
3896 int errcode;
3898 if (s->blame.thread == NULL) {
3899 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3900 &s->blame.thread_args);
3901 if (errcode)
3902 return got_error_set_errno(errcode, "pthread_create");
3904 halfdelay(1); /* fast refresh while annotating */
3907 if (s->blame_complete)
3908 halfdelay(10); /* disable fast refresh */
3910 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3911 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3912 s->selected_line, &s->first_displayed_line,
3913 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3915 view_vborder(view);
3916 return err;
3919 static const struct got_error *
3920 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3921 struct tog_view **focus_view, struct tog_view *view, int ch)
3923 const struct got_error *err = NULL, *thread_err = NULL;
3924 struct tog_view *diff_view;
3925 struct tog_blame_view_state *s = &view->state.blame;
3926 int begin_x = 0;
3928 switch (ch) {
3929 case 'q':
3930 s->done = 1;
3931 break;
3932 case 'k':
3933 case KEY_UP:
3934 if (s->selected_line > 1)
3935 s->selected_line--;
3936 else if (s->selected_line == 1 &&
3937 s->first_displayed_line > 1)
3938 s->first_displayed_line--;
3939 break;
3940 case KEY_PPAGE:
3941 if (s->first_displayed_line == 1) {
3942 s->selected_line = 1;
3943 break;
3945 if (s->first_displayed_line > view->nlines - 2)
3946 s->first_displayed_line -=
3947 (view->nlines - 2);
3948 else
3949 s->first_displayed_line = 1;
3950 break;
3951 case 'j':
3952 case KEY_DOWN:
3953 if (s->selected_line < view->nlines - 2 &&
3954 s->first_displayed_line +
3955 s->selected_line <= s->blame.nlines)
3956 s->selected_line++;
3957 else if (s->last_displayed_line <
3958 s->blame.nlines)
3959 s->first_displayed_line++;
3960 break;
3961 case 'b':
3962 case 'p': {
3963 struct got_object_id *id = NULL;
3964 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3965 s->first_displayed_line, s->selected_line);
3966 if (id == NULL)
3967 break;
3968 if (ch == 'p') {
3969 struct got_commit_object *commit;
3970 struct got_object_qid *pid;
3971 struct got_object_id *blob_id = NULL;
3972 int obj_type;
3973 err = got_object_open_as_commit(&commit,
3974 s->repo, id);
3975 if (err)
3976 break;
3977 pid = SIMPLEQ_FIRST(
3978 got_object_commit_get_parent_ids(commit));
3979 if (pid == NULL) {
3980 got_object_commit_close(commit);
3981 break;
3983 /* Check if path history ends here. */
3984 err = got_object_id_by_path(&blob_id, s->repo,
3985 pid->id, s->path);
3986 if (err) {
3987 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3988 err = NULL;
3989 got_object_commit_close(commit);
3990 break;
3992 err = got_object_get_type(&obj_type, s->repo,
3993 blob_id);
3994 free(blob_id);
3995 /* Can't blame non-blob type objects. */
3996 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3997 got_object_commit_close(commit);
3998 break;
4000 err = got_object_qid_alloc(&s->blamed_commit,
4001 pid->id);
4002 got_object_commit_close(commit);
4003 } else {
4004 if (got_object_id_cmp(id,
4005 s->blamed_commit->id) == 0)
4006 break;
4007 err = got_object_qid_alloc(&s->blamed_commit,
4008 id);
4010 if (err)
4011 break;
4012 s->done = 1;
4013 thread_err = stop_blame(&s->blame);
4014 s->done = 0;
4015 if (thread_err)
4016 break;
4017 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4018 s->blamed_commit, entry);
4019 err = run_blame(&s->blame, view, &s->blame_complete,
4020 &s->first_displayed_line, &s->last_displayed_line,
4021 &s->selected_line, &s->done, &s->eof,
4022 s->path, s->blamed_commit->id, s->repo);
4023 if (err)
4024 break;
4025 break;
4027 case 'B': {
4028 struct got_object_qid *first;
4029 first = SIMPLEQ_FIRST(&s->blamed_commits);
4030 if (!got_object_id_cmp(first->id, s->commit_id))
4031 break;
4032 s->done = 1;
4033 thread_err = stop_blame(&s->blame);
4034 s->done = 0;
4035 if (thread_err)
4036 break;
4037 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4038 got_object_qid_free(s->blamed_commit);
4039 s->blamed_commit =
4040 SIMPLEQ_FIRST(&s->blamed_commits);
4041 err = run_blame(&s->blame, view, &s->blame_complete,
4042 &s->first_displayed_line, &s->last_displayed_line,
4043 &s->selected_line, &s->done, &s->eof, s->path,
4044 s->blamed_commit->id, s->repo);
4045 if (err)
4046 break;
4047 break;
4049 case KEY_ENTER:
4050 case '\r': {
4051 struct got_object_id *id = NULL;
4052 struct got_object_qid *pid;
4053 struct got_commit_object *commit = NULL;
4054 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4055 s->first_displayed_line, s->selected_line);
4056 if (id == NULL)
4057 break;
4058 err = got_object_open_as_commit(&commit, s->repo, id);
4059 if (err)
4060 break;
4061 pid = SIMPLEQ_FIRST(
4062 got_object_commit_get_parent_ids(commit));
4063 if (view_is_parent_view(view))
4064 begin_x = view_split_begin_x(view->begin_x);
4065 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4066 if (diff_view == NULL) {
4067 got_object_commit_close(commit);
4068 err = got_error_from_errno("view_open");
4069 break;
4071 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4072 id, NULL, s->refs, s->repo);
4073 got_object_commit_close(commit);
4074 if (err) {
4075 view_close(diff_view);
4076 break;
4078 if (view_is_parent_view(view)) {
4079 err = view_close_child(view);
4080 if (err)
4081 break;
4082 err = view_set_child(view, diff_view);
4083 if (err) {
4084 view_close(diff_view);
4085 break;
4087 *focus_view = diff_view;
4088 view->child_focussed = 1;
4089 } else
4090 *new_view = diff_view;
4091 if (err)
4092 break;
4093 break;
4095 case KEY_NPAGE:
4096 case ' ':
4097 if (s->last_displayed_line >= s->blame.nlines &&
4098 s->selected_line >= MIN(s->blame.nlines,
4099 view->nlines - 2)) {
4100 break;
4102 if (s->last_displayed_line >= s->blame.nlines &&
4103 s->selected_line < view->nlines - 2) {
4104 s->selected_line = MIN(s->blame.nlines,
4105 view->nlines - 2);
4106 break;
4108 if (s->last_displayed_line + view->nlines - 2
4109 <= s->blame.nlines)
4110 s->first_displayed_line +=
4111 view->nlines - 2;
4112 else
4113 s->first_displayed_line =
4114 s->blame.nlines -
4115 (view->nlines - 3);
4116 break;
4117 case KEY_RESIZE:
4118 if (s->selected_line > view->nlines - 2) {
4119 s->selected_line = MIN(s->blame.nlines,
4120 view->nlines - 2);
4122 break;
4123 default:
4124 break;
4126 return thread_err ? thread_err : err;
4129 static const struct got_error *
4130 cmd_blame(int argc, char *argv[])
4132 const struct got_error *error;
4133 struct got_repository *repo = NULL;
4134 struct got_reflist_head refs;
4135 struct got_worktree *worktree = NULL;
4136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4137 struct got_object_id *commit_id = NULL;
4138 char *commit_id_str = NULL;
4139 int ch;
4140 struct tog_view *view;
4142 SIMPLEQ_INIT(&refs);
4144 #ifndef PROFILE
4145 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4146 NULL) == -1)
4147 err(1, "pledge");
4148 #endif
4150 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4151 switch (ch) {
4152 case 'c':
4153 commit_id_str = optarg;
4154 break;
4155 case 'r':
4156 repo_path = realpath(optarg, NULL);
4157 if (repo_path == NULL)
4158 return got_error_from_errno2("realpath",
4159 optarg);
4160 break;
4161 default:
4162 usage_blame();
4163 /* NOTREACHED */
4167 argc -= optind;
4168 argv += optind;
4170 if (argc == 1)
4171 path = argv[0];
4172 else
4173 usage_blame();
4175 cwd = getcwd(NULL, 0);
4176 if (cwd == NULL) {
4177 error = got_error_from_errno("getcwd");
4178 goto done;
4180 if (repo_path == NULL) {
4181 error = got_worktree_open(&worktree, cwd);
4182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4183 goto done;
4184 else
4185 error = NULL;
4186 if (worktree) {
4187 repo_path =
4188 strdup(got_worktree_get_repo_path(worktree));
4189 if (repo_path == NULL)
4190 error = got_error_from_errno("strdup");
4191 if (error)
4192 goto done;
4193 } else {
4194 repo_path = strdup(cwd);
4195 if (repo_path == NULL) {
4196 error = got_error_from_errno("strdup");
4197 goto done;
4202 init_curses();
4204 error = got_repo_open(&repo, repo_path, NULL);
4205 if (error != NULL)
4206 goto done;
4208 error = apply_unveil(got_repo_get_path(repo), NULL);
4209 if (error)
4210 goto done;
4212 if (worktree) {
4213 const char *prefix = got_worktree_get_path_prefix(worktree);
4214 char *p, *worktree_subdir = cwd +
4215 strlen(got_worktree_get_root_path(worktree));
4216 if (asprintf(&p, "%s%s%s%s%s",
4217 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4218 worktree_subdir, worktree_subdir[0] ? "/" : "",
4219 path) == -1) {
4220 error = got_error_from_errno("asprintf");
4221 goto done;
4223 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4224 free(p);
4225 } else {
4226 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4228 if (error)
4229 goto done;
4231 if (commit_id_str == NULL) {
4232 struct got_reference *head_ref;
4233 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4234 if (error != NULL)
4235 goto done;
4236 error = got_ref_resolve(&commit_id, repo, head_ref);
4237 got_ref_close(head_ref);
4238 } else {
4239 error = got_repo_match_object_id(&commit_id, NULL,
4240 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4242 if (error != NULL)
4243 goto done;
4245 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4246 if (error)
4247 goto done;
4249 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4250 if (view == NULL) {
4251 error = got_error_from_errno("view_open");
4252 goto done;
4254 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4255 if (error)
4256 goto done;
4257 if (worktree) {
4258 /* Release work tree lock. */
4259 got_worktree_close(worktree);
4260 worktree = NULL;
4262 error = view_loop(view);
4263 done:
4264 free(repo_path);
4265 free(cwd);
4266 free(commit_id);
4267 if (worktree)
4268 got_worktree_close(worktree);
4269 if (repo)
4270 got_repo_close(repo);
4271 got_ref_list_free(&refs);
4272 return error;
4275 static const struct got_error *
4276 draw_tree_entries(struct tog_view *view,
4277 struct got_tree_entry **first_displayed_entry,
4278 struct got_tree_entry **last_displayed_entry,
4279 struct got_tree_entry **selected_entry, int *ndisplayed,
4280 const char *label, int show_ids, const char *parent_path,
4281 struct got_tree_object *tree, int selected, int limit,
4282 int isroot, struct tog_colors *colors)
4284 const struct got_error *err = NULL;
4285 struct got_tree_entry *te;
4286 wchar_t *wline;
4287 struct tog_color *tc;
4288 int width, n, i, nentries;
4290 *ndisplayed = 0;
4292 werase(view->window);
4294 if (limit == 0)
4295 return NULL;
4297 err = format_line(&wline, &width, label, view->ncols, 0);
4298 if (err)
4299 return err;
4300 if (view_needs_focus_indication(view))
4301 wstandout(view->window);
4302 tc = get_color(colors, TOG_COLOR_COMMIT);
4303 if (tc)
4304 wattr_on(view->window,
4305 COLOR_PAIR(tc->colorpair), NULL);
4306 waddwstr(view->window, wline);
4307 if (tc)
4308 wattr_off(view->window,
4309 COLOR_PAIR(tc->colorpair), NULL);
4310 if (view_needs_focus_indication(view))
4311 wstandend(view->window);
4312 free(wline);
4313 wline = NULL;
4314 if (width < view->ncols - 1)
4315 waddch(view->window, '\n');
4316 if (--limit <= 0)
4317 return NULL;
4318 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4319 if (err)
4320 return err;
4321 waddwstr(view->window, wline);
4322 free(wline);
4323 wline = NULL;
4324 if (width < view->ncols - 1)
4325 waddch(view->window, '\n');
4326 if (--limit <= 0)
4327 return NULL;
4328 waddch(view->window, '\n');
4329 if (--limit <= 0)
4330 return NULL;
4332 if (*first_displayed_entry == NULL) {
4333 te = got_object_tree_get_first_entry(tree);
4334 if (selected == 0) {
4335 if (view->focussed)
4336 wstandout(view->window);
4337 *selected_entry = NULL;
4339 waddstr(view->window, " ..\n"); /* parent directory */
4340 if (selected == 0 && view->focussed)
4341 wstandend(view->window);
4342 (*ndisplayed)++;
4343 if (--limit <= 0)
4344 return NULL;
4345 n = 1;
4346 } else {
4347 n = 0;
4348 te = *first_displayed_entry;
4351 nentries = got_object_tree_get_nentries(tree);
4352 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4353 char *line = NULL, *id_str = NULL;
4354 const char *modestr = "";
4355 mode_t mode;
4357 te = got_object_tree_get_entry(tree, i);
4358 mode = got_tree_entry_get_mode(te);
4360 if (show_ids) {
4361 err = got_object_id_str(&id_str,
4362 got_tree_entry_get_id(te));
4363 if (err)
4364 return got_error_from_errno(
4365 "got_object_id_str");
4367 if (got_object_tree_entry_is_submodule(te))
4368 modestr = "$";
4369 else if (S_ISLNK(mode))
4370 modestr = "@";
4371 else if (S_ISDIR(mode))
4372 modestr = "/";
4373 else if (mode & S_IXUSR)
4374 modestr = "*";
4375 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4376 got_tree_entry_get_name(te), modestr) == -1) {
4377 free(id_str);
4378 return got_error_from_errno("asprintf");
4380 free(id_str);
4381 err = format_line(&wline, &width, line, view->ncols, 0);
4382 if (err) {
4383 free(line);
4384 break;
4386 if (n == selected) {
4387 if (view->focussed)
4388 wstandout(view->window);
4389 *selected_entry = te;
4391 tc = match_color(colors, line);
4392 if (tc)
4393 wattr_on(view->window,
4394 COLOR_PAIR(tc->colorpair), NULL);
4395 waddwstr(view->window, wline);
4396 if (tc)
4397 wattr_off(view->window,
4398 COLOR_PAIR(tc->colorpair), NULL);
4399 if (width < view->ncols - 1)
4400 waddch(view->window, '\n');
4401 if (n == selected && view->focussed)
4402 wstandend(view->window);
4403 free(line);
4404 free(wline);
4405 wline = NULL;
4406 n++;
4407 (*ndisplayed)++;
4408 *last_displayed_entry = te;
4409 if (--limit <= 0)
4410 break;
4413 return err;
4416 static void
4417 tree_scroll_up(struct tog_view *view,
4418 struct got_tree_entry **first_displayed_entry, int maxscroll,
4419 struct got_tree_object *tree, int isroot)
4421 struct got_tree_entry *te;
4422 int i;
4424 if (*first_displayed_entry == NULL)
4425 return;
4427 te = got_object_tree_get_entry(tree, 0);
4428 if (*first_displayed_entry == te) {
4429 if (!isroot)
4430 *first_displayed_entry = NULL;
4431 return;
4434 i = 0;
4435 while (*first_displayed_entry && i < maxscroll) {
4436 *first_displayed_entry = got_tree_entry_get_prev(tree,
4437 *first_displayed_entry);
4438 i++;
4440 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4441 *first_displayed_entry = NULL;
4444 static int
4445 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4446 struct got_tree_entry *last_displayed_entry,
4447 struct got_tree_object *tree)
4449 struct got_tree_entry *next, *last;
4450 int n = 0;
4452 if (*first_displayed_entry)
4453 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4454 else
4455 next = got_object_tree_get_first_entry(tree);
4457 last = last_displayed_entry;
4458 while (next && last && n++ < maxscroll) {
4459 last = got_tree_entry_get_next(tree, last);
4460 if (last) {
4461 *first_displayed_entry = next;
4462 next = got_tree_entry_get_next(tree, next);
4465 return n;
4468 static const struct got_error *
4469 tree_entry_path(char **path, struct tog_parent_trees *parents,
4470 struct got_tree_entry *te)
4472 const struct got_error *err = NULL;
4473 struct tog_parent_tree *pt;
4474 size_t len = 2; /* for leading slash and NUL */
4476 TAILQ_FOREACH(pt, parents, entry)
4477 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4478 + 1 /* slash */;
4479 if (te)
4480 len += strlen(got_tree_entry_get_name(te));
4482 *path = calloc(1, len);
4483 if (path == NULL)
4484 return got_error_from_errno("calloc");
4486 (*path)[0] = '/';
4487 pt = TAILQ_LAST(parents, tog_parent_trees);
4488 while (pt) {
4489 const char *name = got_tree_entry_get_name(pt->selected_entry);
4490 if (strlcat(*path, name, len) >= len) {
4491 err = got_error(GOT_ERR_NO_SPACE);
4492 goto done;
4494 if (strlcat(*path, "/", len) >= len) {
4495 err = got_error(GOT_ERR_NO_SPACE);
4496 goto done;
4498 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4500 if (te) {
4501 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4502 err = got_error(GOT_ERR_NO_SPACE);
4503 goto done;
4506 done:
4507 if (err) {
4508 free(*path);
4509 *path = NULL;
4511 return err;
4514 static const struct got_error *
4515 blame_tree_entry(struct tog_view **new_view, int begin_x,
4516 struct got_tree_entry *te, struct tog_parent_trees *parents,
4517 struct got_object_id *commit_id, struct got_reflist_head *refs,
4518 struct got_repository *repo)
4520 const struct got_error *err = NULL;
4521 char *path;
4522 struct tog_view *blame_view;
4524 *new_view = NULL;
4526 err = tree_entry_path(&path, parents, te);
4527 if (err)
4528 return err;
4530 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4531 if (blame_view == NULL) {
4532 err = got_error_from_errno("view_open");
4533 goto done;
4536 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4537 if (err) {
4538 if (err->code == GOT_ERR_CANCELLED)
4539 err = NULL;
4540 view_close(blame_view);
4541 } else
4542 *new_view = blame_view;
4543 done:
4544 free(path);
4545 return err;
4548 static const struct got_error *
4549 log_tree_entry(struct tog_view **new_view, int begin_x,
4550 struct got_tree_entry *te, struct tog_parent_trees *parents,
4551 struct got_object_id *commit_id, struct got_reflist_head *refs,
4552 struct got_repository *repo)
4554 struct tog_view *log_view;
4555 const struct got_error *err = NULL;
4556 char *path;
4558 *new_view = NULL;
4560 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4561 if (log_view == NULL)
4562 return got_error_from_errno("view_open");
4564 err = tree_entry_path(&path, parents, te);
4565 if (err)
4566 return err;
4568 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4569 if (err)
4570 view_close(log_view);
4571 else
4572 *new_view = log_view;
4573 free(path);
4574 return err;
4577 static const struct got_error *
4578 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4579 struct got_object_id *commit_id, struct got_reflist_head *refs,
4580 struct got_repository *repo)
4582 const struct got_error *err = NULL;
4583 char *commit_id_str = NULL;
4584 struct tog_tree_view_state *s = &view->state.tree;
4586 TAILQ_INIT(&s->parents);
4588 err = got_object_id_str(&commit_id_str, commit_id);
4589 if (err != NULL)
4590 goto done;
4592 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4593 err = got_error_from_errno("asprintf");
4594 goto done;
4597 s->root = s->tree = root;
4598 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4599 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4600 s->commit_id = got_object_id_dup(commit_id);
4601 if (s->commit_id == NULL) {
4602 err = got_error_from_errno("got_object_id_dup");
4603 goto done;
4605 s->refs = refs;
4606 s->repo = repo;
4608 SIMPLEQ_INIT(&s->colors);
4610 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4611 err = add_color(&s->colors, "\\$$",
4612 TOG_COLOR_TREE_SUBMODULE,
4613 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4614 if (err)
4615 goto done;
4616 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4617 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4618 if (err) {
4619 free_colors(&s->colors);
4620 goto done;
4622 err = add_color(&s->colors, "/$",
4623 TOG_COLOR_TREE_DIRECTORY,
4624 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4625 if (err) {
4626 free_colors(&s->colors);
4627 goto done;
4630 err = add_color(&s->colors, "\\*$",
4631 TOG_COLOR_TREE_EXECUTABLE,
4632 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4633 if (err) {
4634 free_colors(&s->colors);
4635 goto done;
4638 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4639 get_color_value("TOG_COLOR_COMMIT"));
4640 if (err) {
4641 free_colors(&s->colors);
4642 goto done;
4646 view->show = show_tree_view;
4647 view->input = input_tree_view;
4648 view->close = close_tree_view;
4649 view->search_start = search_start_tree_view;
4650 view->search_next = search_next_tree_view;
4651 done:
4652 free(commit_id_str);
4653 if (err) {
4654 free(s->tree_label);
4655 s->tree_label = NULL;
4657 return err;
4660 static const struct got_error *
4661 close_tree_view(struct tog_view *view)
4663 struct tog_tree_view_state *s = &view->state.tree;
4665 free_colors(&s->colors);
4666 free(s->tree_label);
4667 s->tree_label = NULL;
4668 free(s->commit_id);
4669 s->commit_id = NULL;
4670 while (!TAILQ_EMPTY(&s->parents)) {
4671 struct tog_parent_tree *parent;
4672 parent = TAILQ_FIRST(&s->parents);
4673 TAILQ_REMOVE(&s->parents, parent, entry);
4674 free(parent);
4677 if (s->tree != s->root)
4678 got_object_tree_close(s->tree);
4679 got_object_tree_close(s->root);
4681 return NULL;
4684 static const struct got_error *
4685 search_start_tree_view(struct tog_view *view)
4687 struct tog_tree_view_state *s = &view->state.tree;
4689 s->matched_entry = NULL;
4690 return NULL;
4693 static int
4694 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4696 regmatch_t regmatch;
4698 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4699 0) == 0;
4702 static const struct got_error *
4703 search_next_tree_view(struct tog_view *view)
4705 struct tog_tree_view_state *s = &view->state.tree;
4706 struct got_tree_entry *te = NULL;
4708 if (!view->searching) {
4709 view->search_next_done = 1;
4710 return NULL;
4713 if (s->matched_entry) {
4714 if (view->searching == TOG_SEARCH_FORWARD) {
4715 if (s->selected_entry)
4716 te = got_tree_entry_get_next(s->tree,
4717 s->selected_entry);
4718 else
4719 te = got_object_tree_get_first_entry(s->tree);
4720 } else {
4721 if (s->selected_entry == NULL)
4722 te = got_object_tree_get_last_entry(s->tree);
4723 else
4724 te = got_tree_entry_get_prev(s->tree,
4725 s->selected_entry);
4727 } else {
4728 if (view->searching == TOG_SEARCH_FORWARD)
4729 te = got_object_tree_get_first_entry(s->tree);
4730 else
4731 te = got_object_tree_get_last_entry(s->tree);
4734 while (1) {
4735 if (te == NULL) {
4736 if (s->matched_entry == NULL) {
4737 view->search_next_done = 1;
4738 return NULL;
4740 if (view->searching == TOG_SEARCH_FORWARD)
4741 te = got_object_tree_get_first_entry(s->tree);
4742 else
4743 te = got_object_tree_get_last_entry(s->tree);
4746 if (match_tree_entry(te, &view->regex)) {
4747 view->search_next_done = 1;
4748 s->matched_entry = te;
4749 break;
4752 if (view->searching == TOG_SEARCH_FORWARD)
4753 te = got_tree_entry_get_next(s->tree, te);
4754 else
4755 te = got_tree_entry_get_prev(s->tree, te);
4758 if (s->matched_entry) {
4759 s->first_displayed_entry = s->matched_entry;
4760 s->selected = 0;
4763 return NULL;
4766 static const struct got_error *
4767 show_tree_view(struct tog_view *view)
4769 const struct got_error *err = NULL;
4770 struct tog_tree_view_state *s = &view->state.tree;
4771 char *parent_path;
4773 err = tree_entry_path(&parent_path, &s->parents, NULL);
4774 if (err)
4775 return err;
4777 err = draw_tree_entries(view, &s->first_displayed_entry,
4778 &s->last_displayed_entry, &s->selected_entry,
4779 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4780 s->tree, s->selected, view->nlines, s->tree == s->root,
4781 &s->colors);
4782 free(parent_path);
4784 view_vborder(view);
4785 return err;
4788 static const struct got_error *
4789 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4790 struct tog_view **focus_view, struct tog_view *view, int ch)
4792 const struct got_error *err = NULL;
4793 struct tog_tree_view_state *s = &view->state.tree;
4794 struct tog_view *log_view;
4795 int begin_x = 0, nscrolled;
4797 switch (ch) {
4798 case 'i':
4799 s->show_ids = !s->show_ids;
4800 break;
4801 case 'l':
4802 if (!s->selected_entry)
4803 break;
4804 if (view_is_parent_view(view))
4805 begin_x = view_split_begin_x(view->begin_x);
4806 err = log_tree_entry(&log_view, begin_x,
4807 s->selected_entry, &s->parents,
4808 s->commit_id, s->refs, s->repo);
4809 if (view_is_parent_view(view)) {
4810 err = view_close_child(view);
4811 if (err)
4812 return err;
4813 err = view_set_child(view, log_view);
4814 if (err) {
4815 view_close(log_view);
4816 break;
4818 *focus_view = log_view;
4819 view->child_focussed = 1;
4820 } else
4821 *new_view = log_view;
4822 break;
4823 case 'k':
4824 case KEY_UP:
4825 if (s->selected > 0) {
4826 s->selected--;
4827 if (s->selected == 0)
4828 break;
4830 if (s->selected > 0)
4831 break;
4832 tree_scroll_up(view, &s->first_displayed_entry, 1,
4833 s->tree, s->tree == s->root);
4834 break;
4835 case KEY_PPAGE:
4836 tree_scroll_up(view, &s->first_displayed_entry,
4837 MAX(0, view->nlines - 4 - s->selected), s->tree,
4838 s->tree == s->root);
4839 s->selected = 0;
4840 if (got_object_tree_get_first_entry(s->tree) ==
4841 s->first_displayed_entry && s->tree != s->root)
4842 s->first_displayed_entry = NULL;
4843 break;
4844 case 'j':
4845 case KEY_DOWN:
4846 if (s->selected < s->ndisplayed - 1) {
4847 s->selected++;
4848 break;
4850 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4851 == NULL)
4852 /* can't scroll any further */
4853 break;
4854 tree_scroll_down(&s->first_displayed_entry, 1,
4855 s->last_displayed_entry, s->tree);
4856 break;
4857 case KEY_NPAGE:
4858 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4859 == NULL) {
4860 /* can't scroll any further; move cursor down */
4861 if (s->selected < s->ndisplayed - 1)
4862 s->selected = s->ndisplayed - 1;
4863 break;
4865 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4866 view->nlines, s->last_displayed_entry, s->tree);
4867 if (nscrolled < view->nlines) {
4868 int ndisplayed = 0;
4869 struct got_tree_entry *te;
4870 te = s->first_displayed_entry;
4871 do {
4872 ndisplayed++;
4873 te = got_tree_entry_get_next(s->tree, te);
4874 } while (te);
4875 s->selected = ndisplayed - 1;
4877 break;
4878 case KEY_ENTER:
4879 case '\r':
4880 case KEY_BACKSPACE:
4881 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4882 struct tog_parent_tree *parent;
4883 /* user selected '..' */
4884 if (s->tree == s->root)
4885 break;
4886 parent = TAILQ_FIRST(&s->parents);
4887 TAILQ_REMOVE(&s->parents, parent,
4888 entry);
4889 got_object_tree_close(s->tree);
4890 s->tree = parent->tree;
4891 s->first_displayed_entry =
4892 parent->first_displayed_entry;
4893 s->selected_entry =
4894 parent->selected_entry;
4895 s->selected = parent->selected;
4896 free(parent);
4897 } else if (S_ISDIR(got_tree_entry_get_mode(
4898 s->selected_entry))) {
4899 struct got_tree_object *subtree;
4900 err = got_object_open_as_tree(&subtree, s->repo,
4901 got_tree_entry_get_id(s->selected_entry));
4902 if (err)
4903 break;
4904 err = tree_view_visit_subtree(subtree, s);
4905 if (err) {
4906 got_object_tree_close(subtree);
4907 break;
4909 } else if (S_ISREG(got_tree_entry_get_mode(
4910 s->selected_entry))) {
4911 struct tog_view *blame_view;
4912 int begin_x = view_is_parent_view(view) ?
4913 view_split_begin_x(view->begin_x) : 0;
4915 err = blame_tree_entry(&blame_view, begin_x,
4916 s->selected_entry, &s->parents,
4917 s->commit_id, s->refs, s->repo);
4918 if (err)
4919 break;
4920 if (view_is_parent_view(view)) {
4921 err = view_close_child(view);
4922 if (err)
4923 return err;
4924 err = view_set_child(view, blame_view);
4925 if (err) {
4926 view_close(blame_view);
4927 break;
4929 *focus_view = blame_view;
4930 view->child_focussed = 1;
4931 } else
4932 *new_view = blame_view;
4934 break;
4935 case KEY_RESIZE:
4936 if (s->selected > view->nlines)
4937 s->selected = s->ndisplayed - 1;
4938 break;
4939 default:
4940 break;
4943 return err;
4946 __dead static void
4947 usage_tree(void)
4949 endwin();
4950 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4951 getprogname());
4952 exit(1);
4955 static const struct got_error *
4956 cmd_tree(int argc, char *argv[])
4958 const struct got_error *error;
4959 struct got_repository *repo = NULL;
4960 struct got_reflist_head refs;
4961 char *repo_path = NULL;
4962 struct got_object_id *commit_id = NULL;
4963 char *commit_id_arg = NULL;
4964 struct got_commit_object *commit = NULL;
4965 struct got_tree_object *tree = NULL;
4966 int ch;
4967 struct tog_view *view;
4969 SIMPLEQ_INIT(&refs);
4971 #ifndef PROFILE
4972 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4973 NULL) == -1)
4974 err(1, "pledge");
4975 #endif
4977 while ((ch = getopt(argc, argv, "c:")) != -1) {
4978 switch (ch) {
4979 case 'c':
4980 commit_id_arg = optarg;
4981 break;
4982 default:
4983 usage_tree();
4984 /* NOTREACHED */
4988 argc -= optind;
4989 argv += optind;
4991 if (argc == 0) {
4992 struct got_worktree *worktree;
4993 char *cwd = getcwd(NULL, 0);
4994 if (cwd == NULL)
4995 return got_error_from_errno("getcwd");
4996 error = got_worktree_open(&worktree, cwd);
4997 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4998 goto done;
4999 if (worktree) {
5000 free(cwd);
5001 repo_path =
5002 strdup(got_worktree_get_repo_path(worktree));
5003 got_worktree_close(worktree);
5004 } else
5005 repo_path = cwd;
5006 if (repo_path == NULL) {
5007 error = got_error_from_errno("strdup");
5008 goto done;
5010 } else if (argc == 1) {
5011 repo_path = realpath(argv[0], NULL);
5012 if (repo_path == NULL)
5013 return got_error_from_errno2("realpath", argv[0]);
5014 } else
5015 usage_tree();
5017 init_curses();
5019 error = got_repo_open(&repo, repo_path, NULL);
5020 if (error != NULL)
5021 goto done;
5023 error = apply_unveil(got_repo_get_path(repo), NULL);
5024 if (error)
5025 goto done;
5027 error = got_repo_match_object_id(&commit_id, NULL,
5028 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5029 GOT_OBJ_TYPE_COMMIT, 1, repo);
5030 if (error != NULL)
5031 goto done;
5033 error = got_object_open_as_commit(&commit, repo, commit_id);
5034 if (error != NULL)
5035 goto done;
5037 error = got_object_open_as_tree(&tree, repo,
5038 got_object_commit_get_tree_id(commit));
5039 if (error != NULL)
5040 goto done;
5042 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5043 if (error)
5044 goto done;
5046 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5047 if (view == NULL) {
5048 error = got_error_from_errno("view_open");
5049 goto done;
5051 error = open_tree_view(view, tree, commit_id, &refs, repo);
5052 if (error)
5053 goto done;
5054 error = view_loop(view);
5055 done:
5056 free(repo_path);
5057 free(commit_id);
5058 if (commit)
5059 got_object_commit_close(commit);
5060 if (tree)
5061 got_object_tree_close(tree);
5062 if (repo)
5063 got_repo_close(repo);
5064 got_ref_list_free(&refs);
5065 return error;
5068 static void
5069 list_commands(void)
5071 int i;
5073 fprintf(stderr, "commands:");
5074 for (i = 0; i < nitems(tog_commands); i++) {
5075 struct tog_cmd *cmd = &tog_commands[i];
5076 fprintf(stderr, " %s", cmd->name);
5078 fputc('\n', stderr);
5081 __dead static void
5082 usage(int hflag)
5084 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5085 getprogname());
5086 if (hflag)
5087 list_commands();
5088 exit(1);
5091 static char **
5092 make_argv(const char *arg0, const char *arg1)
5094 char **argv;
5095 int argc = (arg1 == NULL ? 1 : 2);
5097 argv = calloc(argc, sizeof(char *));
5098 if (argv == NULL)
5099 err(1, "calloc");
5100 argv[0] = strdup(arg0);
5101 if (argv[0] == NULL)
5102 err(1, "strdup");
5103 if (arg1) {
5104 argv[1] = strdup(arg1);
5105 if (argv[1] == NULL)
5106 err(1, "strdup");
5109 return argv;
5112 int
5113 main(int argc, char *argv[])
5115 const struct got_error *error = NULL;
5116 struct tog_cmd *cmd = NULL;
5117 int ch, hflag = 0, Vflag = 0;
5118 char **cmd_argv = NULL;
5119 static struct option longopts[] = {
5120 { "version", no_argument, NULL, 'V' },
5121 { NULL, 0, NULL, 0}
5124 setlocale(LC_CTYPE, "");
5126 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5127 switch (ch) {
5128 case 'h':
5129 hflag = 1;
5130 break;
5131 case 'V':
5132 Vflag = 1;
5133 break;
5134 default:
5135 usage(hflag);
5136 /* NOTREACHED */
5140 argc -= optind;
5141 argv += optind;
5142 optind = 0;
5143 optreset = 1;
5145 if (Vflag) {
5146 got_version_print_str();
5147 return 1;
5150 if (argc == 0) {
5151 if (hflag)
5152 usage(hflag);
5153 /* Build an argument vector which runs a default command. */
5154 cmd = &tog_commands[0];
5155 cmd_argv = make_argv(cmd->name, NULL);
5156 argc = 1;
5157 } else {
5158 int i;
5160 /* Did the user specific a command? */
5161 for (i = 0; i < nitems(tog_commands); i++) {
5162 if (strncmp(tog_commands[i].name, argv[0],
5163 strlen(argv[0])) == 0) {
5164 cmd = &tog_commands[i];
5165 break;
5169 if (cmd == NULL) {
5170 fprintf(stderr, "%s: unknown command '%s'\n",
5171 getprogname(), argv[0]);
5172 list_commands();
5173 return 1;
5177 if (hflag)
5178 cmd->cmd_usage();
5179 else
5180 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5182 endwin();
5183 free(cmd_argv);
5184 if (error && error->code != GOT_ERR_CANCELLED)
5185 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5186 return 0;