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 struct got_repository *repo;
286 struct got_reflist_head *refs;
287 struct got_object_id *start_id;
288 sig_atomic_t quit;
289 pthread_t thread;
290 struct tog_log_thread_args thread_args;
291 struct commit_queue_entry *matched_entry;
292 struct commit_queue_entry *search_entry;
293 struct tog_colors colors;
294 };
296 #define TOG_COLOR_DIFF_MINUS 1
297 #define TOG_COLOR_DIFF_PLUS 2
298 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
299 #define TOG_COLOR_DIFF_META 4
300 #define TOG_COLOR_TREE_SUBMODULE 5
301 #define TOG_COLOR_TREE_SYMLINK 6
302 #define TOG_COLOR_TREE_DIRECTORY 7
303 #define TOG_COLOR_TREE_EXECUTABLE 8
304 #define TOG_COLOR_COMMIT 9
305 #define TOG_COLOR_AUTHOR 10
306 #define TOG_COLOR_DATE 11
308 struct tog_blame_cb_args {
309 struct tog_blame_line *lines; /* one per line */
310 int nlines;
312 struct tog_view *view;
313 struct got_object_id *commit_id;
314 int *quit;
315 };
317 struct tog_blame_thread_args {
318 const char *path;
319 struct got_repository *repo;
320 struct tog_blame_cb_args *cb_args;
321 int *complete;
322 got_cancel_cb cancel_cb;
323 void *cancel_arg;
324 };
326 struct tog_blame {
327 FILE *f;
328 size_t filesize;
329 struct tog_blame_line *lines;
330 int nlines;
331 off_t *line_offsets;
332 pthread_t thread;
333 struct tog_blame_thread_args thread_args;
334 struct tog_blame_cb_args cb_args;
335 const char *path;
336 };
338 struct tog_blame_view_state {
339 int first_displayed_line;
340 int last_displayed_line;
341 int selected_line;
342 int blame_complete;
343 int eof;
344 int done;
345 struct got_object_id_queue blamed_commits;
346 struct got_object_qid *blamed_commit;
347 char *path;
348 struct got_repository *repo;
349 struct got_reflist_head *refs;
350 struct got_object_id *commit_id;
351 struct tog_blame blame;
352 int matched_line;
353 struct tog_colors colors;
354 };
356 struct tog_parent_tree {
357 TAILQ_ENTRY(tog_parent_tree) entry;
358 struct got_tree_object *tree;
359 struct got_tree_entry *first_displayed_entry;
360 struct got_tree_entry *selected_entry;
361 int selected;
362 };
364 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
366 struct tog_tree_view_state {
367 char *tree_label;
368 struct got_tree_object *root;
369 struct got_tree_object *tree;
370 struct got_tree_entry *first_displayed_entry;
371 struct got_tree_entry *last_displayed_entry;
372 struct got_tree_entry *selected_entry;
373 int ndisplayed, selected, show_ids;
374 struct tog_parent_trees parents;
375 struct got_object_id *commit_id;
376 struct got_repository *repo;
377 struct got_reflist_head *refs;
378 struct got_tree_entry *matched_entry;
379 struct tog_colors colors;
380 };
382 /*
383 * We implement two types of views: parent views and child views.
385 * The 'Tab' key switches between a parent view and its child view.
386 * Child views are shown side-by-side to their parent view, provided
387 * there is enough screen estate.
389 * When a new view is opened from within a parent view, this new view
390 * becomes a child view of the parent view, replacing any existing child.
392 * When a new view is opened from within a child view, this new view
393 * becomes a parent view which will obscure the views below until the
394 * user quits the new parent view by typing 'q'.
396 * This list of views contains parent views only.
397 * Child views are only pointed to by their parent view.
398 */
399 TAILQ_HEAD(tog_view_list_head, tog_view);
401 struct tog_view {
402 TAILQ_ENTRY(tog_view) entry;
403 WINDOW *window;
404 PANEL *panel;
405 int nlines, ncols, begin_y, begin_x;
406 int lines, cols; /* copies of LINES and COLS */
407 int focussed;
408 struct tog_view *parent;
409 struct tog_view *child;
410 int child_focussed;
412 /* type-specific state */
413 enum tog_view_type type;
414 union {
415 struct tog_diff_view_state diff;
416 struct tog_log_view_state log;
417 struct tog_blame_view_state blame;
418 struct tog_tree_view_state tree;
419 } state;
421 const struct got_error *(*show)(struct tog_view *);
422 const struct got_error *(*input)(struct tog_view **,
423 struct tog_view **, struct tog_view**, struct tog_view *, int);
424 const struct got_error *(*close)(struct tog_view *);
426 const struct got_error *(*search_start)(struct tog_view *);
427 const struct got_error *(*search_next)(struct tog_view *);
428 int searching;
429 #define TOG_SEARCH_FORWARD 1
430 #define TOG_SEARCH_BACKWARD 2
431 int search_next_done;
432 regex_t regex;
433 };
435 static const struct got_error *open_diff_view(struct tog_view *,
436 struct got_object_id *, struct got_object_id *, struct tog_view *,
437 struct got_reflist_head *, struct got_repository *);
438 static const struct got_error *show_diff_view(struct tog_view *);
439 static const struct got_error *input_diff_view(struct tog_view **,
440 struct tog_view **, struct tog_view **, struct tog_view *, int);
441 static const struct got_error* close_diff_view(struct tog_view *);
443 static const struct got_error *open_log_view(struct tog_view *,
444 struct got_object_id *, struct got_reflist_head *,
445 struct got_repository *, const char *, const char *, int);
446 static const struct got_error * show_log_view(struct tog_view *);
447 static const struct got_error *input_log_view(struct tog_view **,
448 struct tog_view **, struct tog_view **, struct tog_view *, int);
449 static const struct got_error *close_log_view(struct tog_view *);
450 static const struct got_error *search_start_log_view(struct tog_view *);
451 static const struct got_error *search_next_log_view(struct tog_view *);
453 static const struct got_error *open_blame_view(struct tog_view *, char *,
454 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 static const struct got_error *show_blame_view(struct tog_view *);
456 static const struct got_error *input_blame_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_blame_view(struct tog_view *);
459 static const struct got_error *search_start_blame_view(struct tog_view *);
460 static const struct got_error *search_next_blame_view(struct tog_view *);
462 static const struct got_error *open_tree_view(struct tog_view *,
463 struct got_tree_object *, struct got_object_id *,
464 struct got_reflist_head *, struct got_repository *);
465 static const struct got_error *show_tree_view(struct tog_view *);
466 static const struct got_error *input_tree_view(struct tog_view **,
467 struct tog_view **, struct tog_view **, struct tog_view *, int);
468 static const struct got_error *close_tree_view(struct tog_view *);
469 static const struct got_error *search_start_tree_view(struct tog_view *);
470 static const struct got_error *search_next_tree_view(struct tog_view *);
472 static volatile sig_atomic_t tog_sigwinch_received;
473 static volatile sig_atomic_t tog_sigpipe_received;
474 static volatile sig_atomic_t tog_sigcont_received;
476 static void
477 tog_sigwinch(int signo)
479 tog_sigwinch_received = 1;
482 static void
483 tog_sigpipe(int signo)
485 tog_sigpipe_received = 1;
488 static void
489 tog_sigcont(int signo)
491 tog_sigcont_received = 1;
494 static const struct got_error *
495 view_close(struct tog_view *view)
497 const struct got_error *err = NULL;
499 if (view->child) {
500 view_close(view->child);
501 view->child = NULL;
503 if (view->close)
504 err = view->close(view);
505 if (view->panel)
506 del_panel(view->panel);
507 if (view->window)
508 delwin(view->window);
509 free(view);
510 return err;
513 static struct tog_view *
514 view_open(int nlines, int ncols, int begin_y, int begin_x,
515 enum tog_view_type type)
517 struct tog_view *view = calloc(1, sizeof(*view));
519 if (view == NULL)
520 return NULL;
522 view->type = type;
523 view->lines = LINES;
524 view->cols = COLS;
525 view->nlines = nlines ? nlines : LINES - begin_y;
526 view->ncols = ncols ? ncols : COLS - begin_x;
527 view->begin_y = begin_y;
528 view->begin_x = begin_x;
529 view->window = newwin(nlines, ncols, begin_y, begin_x);
530 if (view->window == NULL) {
531 view_close(view);
532 return NULL;
534 view->panel = new_panel(view->window);
535 if (view->panel == NULL ||
536 set_panel_userptr(view->panel, view) != OK) {
537 view_close(view);
538 return NULL;
541 keypad(view->window, TRUE);
542 return view;
545 static int
546 view_split_begin_x(int begin_x)
548 if (begin_x > 0 || COLS < 120)
549 return 0;
550 return (COLS - MAX(COLS / 2, 80));
553 static const struct got_error *view_resize(struct tog_view *);
555 static const struct got_error *
556 view_splitscreen(struct tog_view *view)
558 const struct got_error *err = NULL;
560 view->begin_y = 0;
561 view->begin_x = view_split_begin_x(0);
562 view->nlines = LINES;
563 view->ncols = COLS - view->begin_x;
564 view->lines = LINES;
565 view->cols = COLS;
566 err = view_resize(view);
567 if (err)
568 return err;
570 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
571 return got_error_from_errno("mvwin");
573 return NULL;
576 static const struct got_error *
577 view_fullscreen(struct tog_view *view)
579 const struct got_error *err = NULL;
581 view->begin_x = 0;
582 view->begin_y = 0;
583 view->nlines = LINES;
584 view->ncols = COLS;
585 view->lines = LINES;
586 view->cols = COLS;
587 err = view_resize(view);
588 if (err)
589 return err;
591 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
592 return got_error_from_errno("mvwin");
594 return NULL;
597 static int
598 view_is_parent_view(struct tog_view *view)
600 return view->parent == NULL;
603 static const struct got_error *
604 view_resize(struct tog_view *view)
606 int nlines, ncols;
608 if (view->lines > LINES)
609 nlines = view->nlines - (view->lines - LINES);
610 else
611 nlines = view->nlines + (LINES - view->lines);
613 if (view->cols > COLS)
614 ncols = view->ncols - (view->cols - COLS);
615 else
616 ncols = view->ncols + (COLS - view->cols);
618 if (wresize(view->window, nlines, ncols) == ERR)
619 return got_error_from_errno("wresize");
620 if (replace_panel(view->panel, view->window) == ERR)
621 return got_error_from_errno("replace_panel");
622 wclear(view->window);
624 view->nlines = nlines;
625 view->ncols = ncols;
626 view->lines = LINES;
627 view->cols = COLS;
629 if (view->child) {
630 view->child->begin_x = view_split_begin_x(view->begin_x);
631 if (view->child->begin_x == 0) {
632 view_fullscreen(view->child);
633 if (view->child->focussed)
634 show_panel(view->child->panel);
635 else
636 show_panel(view->panel);
637 } else {
638 view_splitscreen(view->child);
639 show_panel(view->child->panel);
643 return NULL;
646 static const struct got_error *
647 view_close_child(struct tog_view *view)
649 const struct got_error *err = NULL;
651 if (view->child == NULL)
652 return NULL;
654 err = view_close(view->child);
655 view->child = NULL;
656 return err;
659 static const struct got_error *
660 view_set_child(struct tog_view *view, struct tog_view *child)
662 const struct got_error *err = NULL;
664 view->child = child;
665 child->parent = view;
666 return err;
669 static int
670 view_is_splitscreen(struct tog_view *view)
672 return view->begin_x > 0;
675 static void
676 tog_resizeterm(void)
678 int cols, lines;
679 struct winsize size;
681 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
682 cols = 80; /* Default */
683 lines = 24;
684 } else {
685 cols = size.ws_col;
686 lines = size.ws_row;
688 resize_term(lines, cols);
691 static const struct got_error *
692 view_search_start(struct tog_view *view)
694 const struct got_error *err = NULL;
695 char pattern[1024];
696 int ret;
697 int begin_x = 0;
699 if (view->nlines < 1)
700 return NULL;
702 if (!view_is_parent_view(view))
703 begin_x = view_split_begin_x(view->begin_x);
704 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
705 begin_x, "/");
706 wclrtoeol(view->window);
708 nocbreak();
709 echo();
710 ret = wgetnstr(view->window, pattern, sizeof(pattern));
711 cbreak();
712 noecho();
713 if (ret == ERR)
714 return NULL;
716 if (view->searching) {
717 regfree(&view->regex);
718 view->searching = 0;
721 if (regcomp(&view->regex, pattern,
722 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
723 err = view->search_start(view);
724 if (err) {
725 regfree(&view->regex);
726 return err;
728 view->searching = TOG_SEARCH_FORWARD;
729 view->search_next_done = 0;
730 view->search_next(view);
733 return NULL;
736 static const struct got_error *
737 view_input(struct tog_view **new, struct tog_view **dead,
738 struct tog_view **focus, int *done, struct tog_view *view,
739 struct tog_view_list_head *views)
741 const struct got_error *err = NULL;
742 struct tog_view *v;
743 int ch, errcode;
745 *new = NULL;
746 *dead = NULL;
747 *focus = NULL;
749 if (view->searching && !view->search_next_done) {
750 view->search_next(view);
751 return NULL;
754 nodelay(stdscr, FALSE);
755 /* Allow threads to make progress while we are waiting for input. */
756 errcode = pthread_mutex_unlock(&tog_mutex);
757 if (errcode)
758 return got_error_set_errno(errcode, "pthread_mutex_unlock");
759 ch = wgetch(view->window);
760 errcode = pthread_mutex_lock(&tog_mutex);
761 if (errcode)
762 return got_error_set_errno(errcode, "pthread_mutex_lock");
763 nodelay(stdscr, TRUE);
765 if (tog_sigwinch_received || tog_sigcont_received) {
766 tog_resizeterm();
767 tog_sigwinch_received = 0;
768 tog_sigcont_received = 0;
769 TAILQ_FOREACH(v, views, entry) {
770 err = view_resize(v);
771 if (err)
772 return err;
773 err = v->input(new, dead, focus, v, KEY_RESIZE);
774 if (err)
775 return err;
779 switch (ch) {
780 case ERR:
781 break;
782 case '\t':
783 if (view->child) {
784 *focus = view->child;
785 view->child_focussed = 1;
786 } else if (view->parent) {
787 *focus = view->parent;
788 view->parent->child_focussed = 0;
790 break;
791 case 'q':
792 err = view->input(new, dead, focus, view, ch);
793 *dead = view;
794 break;
795 case 'Q':
796 *done = 1;
797 break;
798 case 'f':
799 if (view_is_parent_view(view)) {
800 if (view->child == NULL)
801 break;
802 if (view_is_splitscreen(view->child)) {
803 *focus = view->child;
804 view->child_focussed = 1;
805 err = view_fullscreen(view->child);
806 } else
807 err = view_splitscreen(view->child);
808 if (err)
809 break;
810 err = view->child->input(new, dead, focus,
811 view->child, KEY_RESIZE);
812 } else {
813 if (view_is_splitscreen(view)) {
814 *focus = view;
815 view->parent->child_focussed = 1;
816 err = view_fullscreen(view);
817 } else {
818 err = view_splitscreen(view);
820 if (err)
821 break;
822 err = view->input(new, dead, focus, view,
823 KEY_RESIZE);
825 break;
826 case KEY_RESIZE:
827 break;
828 case '/':
829 if (view->search_start)
830 view_search_start(view);
831 else
832 err = view->input(new, dead, focus, view, ch);
833 break;
834 case 'N':
835 case 'n':
836 if (view->search_next && view->searching) {
837 view->searching = (ch == 'n' ?
838 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 view->search_next_done = 0;
840 view->search_next(view);
841 } else
842 err = view->input(new, dead, focus, view, ch);
843 break;
844 default:
845 err = view->input(new, dead, focus, view, ch);
846 break;
849 return err;
852 void
853 view_vborder(struct tog_view *view)
855 PANEL *panel;
856 struct tog_view *view_above;
858 if (view->parent)
859 return view_vborder(view->parent);
861 panel = panel_above(view->panel);
862 if (panel == NULL)
863 return;
865 view_above = panel_userptr(panel);
866 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
870 int
871 view_needs_focus_indication(struct tog_view *view)
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL || view->child_focussed)
875 return 0;
876 if (!view_is_splitscreen(view->child))
877 return 0;
878 } else if (!view_is_splitscreen(view))
879 return 0;
881 return view->focussed;
884 static const struct got_error *
885 view_loop(struct tog_view *view)
887 const struct got_error *err = NULL;
888 struct tog_view_list_head views;
889 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 int fast_refresh = 10;
891 int done = 0, errcode;
893 errcode = pthread_mutex_lock(&tog_mutex);
894 if (errcode)
895 return got_error_set_errno(errcode, "pthread_mutex_lock");
897 TAILQ_INIT(&views);
898 TAILQ_INSERT_HEAD(&views, view, entry);
900 main_view = view;
901 view->focussed = 1;
902 err = view->show(view);
903 if (err)
904 return err;
905 update_panels();
906 doupdate();
907 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 /* Refresh fast during initialization, then become slower. */
909 if (fast_refresh && fast_refresh-- == 0)
910 halfdelay(10); /* switch to once per second */
912 err = view_input(&new_view, &dead_view, &focus_view, &done,
913 view, &views);
914 if (err)
915 break;
916 if (dead_view) {
917 struct tog_view *prev = NULL;
919 if (view_is_parent_view(dead_view))
920 prev = TAILQ_PREV(dead_view,
921 tog_view_list_head, entry);
922 else if (view->parent != dead_view)
923 prev = view->parent;
925 if (dead_view->parent)
926 dead_view->parent->child = NULL;
927 else
928 TAILQ_REMOVE(&views, dead_view, entry);
930 err = view_close(dead_view);
931 if (err || (dead_view == main_view && new_view == NULL))
932 goto done;
934 if (view == dead_view) {
935 if (focus_view)
936 view = focus_view;
937 else if (prev)
938 view = prev;
939 else if (!TAILQ_EMPTY(&views))
940 view = TAILQ_LAST(&views,
941 tog_view_list_head);
942 else
943 view = NULL;
944 if (view) {
945 if (view->child && view->child_focussed)
946 focus_view = view->child;
947 else
948 focus_view = view;
952 if (new_view) {
953 struct tog_view *v, *t;
954 /* Only allow one parent view per type. */
955 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 if (v->type != new_view->type)
957 continue;
958 TAILQ_REMOVE(&views, v, entry);
959 err = view_close(v);
960 if (err)
961 goto done;
962 break;
964 TAILQ_INSERT_TAIL(&views, new_view, entry);
965 view = new_view;
966 if (focus_view == NULL)
967 focus_view = new_view;
969 if (focus_view) {
970 show_panel(focus_view->panel);
971 if (view)
972 view->focussed = 0;
973 focus_view->focussed = 1;
974 view = focus_view;
975 if (new_view)
976 show_panel(new_view->panel);
977 if (view->child && view_is_splitscreen(view->child))
978 show_panel(view->child->panel);
980 if (view) {
981 if (focus_view == NULL) {
982 view->focussed = 1;
983 show_panel(view->panel);
984 if (view->child && view_is_splitscreen(view->child))
985 show_panel(view->child->panel);
986 focus_view = view;
988 if (view->parent) {
989 err = view->parent->show(view->parent);
990 if (err)
991 goto done;
993 err = view->show(view);
994 if (err)
995 goto done;
996 if (view->child) {
997 err = view->child->show(view->child);
998 if (err)
999 goto done;
1001 update_panels();
1002 doupdate();
1005 done:
1006 while (!TAILQ_EMPTY(&views)) {
1007 view = TAILQ_FIRST(&views);
1008 TAILQ_REMOVE(&views, view, entry);
1009 view_close(view);
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1016 return err;
1019 __dead static void
1020 usage_log(void)
1022 endwin();
1023 fprintf(stderr,
1024 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1025 getprogname());
1026 exit(1);
1029 /* Create newly allocated wide-character string equivalent to a byte string. */
1030 static const struct got_error *
1031 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1033 char *vis = NULL;
1034 const struct got_error *err = NULL;
1036 *ws = NULL;
1037 *wlen = mbstowcs(NULL, s, 0);
1038 if (*wlen == (size_t)-1) {
1039 int vislen;
1040 if (errno != EILSEQ)
1041 return got_error_from_errno("mbstowcs");
1043 /* byte string invalid in current encoding; try to "fix" it */
1044 err = got_mbsavis(&vis, &vislen, s);
1045 if (err)
1046 return err;
1047 *wlen = mbstowcs(NULL, vis, 0);
1048 if (*wlen == (size_t)-1) {
1049 err = got_error_from_errno("mbstowcs"); /* give up */
1050 goto done;
1054 *ws = calloc(*wlen + 1, sizeof(**ws));
1055 if (*ws == NULL) {
1056 err = got_error_from_errno("calloc");
1057 goto done;
1060 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 err = got_error_from_errno("mbstowcs");
1062 done:
1063 free(vis);
1064 if (err) {
1065 free(*ws);
1066 *ws = NULL;
1067 *wlen = 0;
1069 return err;
1072 /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 static const struct got_error *
1074 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1075 int col_tab_align)
1077 const struct got_error *err = NULL;
1078 int cols = 0;
1079 wchar_t *wline = NULL;
1080 size_t wlen;
1081 int i;
1083 *wlinep = NULL;
1084 *widthp = 0;
1086 err = mbs2ws(&wline, &wlen, line);
1087 if (err)
1088 return err;
1090 i = 0;
1091 while (i < wlen) {
1092 int width = wcwidth(wline[i]);
1094 if (width == 0) {
1095 i++;
1096 continue;
1099 if (width == 1 || width == 2) {
1100 if (cols + width > wlimit)
1101 break;
1102 cols += width;
1103 i++;
1104 } else if (width == -1) {
1105 if (wline[i] == L'\t') {
1106 width = TABSIZE -
1107 ((cols + col_tab_align) % TABSIZE);
1108 if (cols + width > wlimit)
1109 break;
1110 cols += width;
1112 i++;
1113 } else {
1114 err = got_error_from_errno("wcwidth");
1115 goto done;
1118 wline[i] = L'\0';
1119 if (widthp)
1120 *widthp = cols;
1121 done:
1122 if (err)
1123 free(wline);
1124 else
1125 *wlinep = wline;
1126 return err;
1129 static const struct got_error*
1130 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 struct got_object_id *id, struct got_repository *repo)
1133 static const struct got_error *err = NULL;
1134 struct got_reflist_entry *re;
1135 char *s;
1136 const char *name;
1138 *refs_str = NULL;
1140 SIMPLEQ_FOREACH(re, refs, entry) {
1141 struct got_tag_object *tag = NULL;
1142 int cmp;
1144 name = got_ref_get_name(re->ref);
1145 if (strcmp(name, GOT_REF_HEAD) == 0)
1146 continue;
1147 if (strncmp(name, "refs/", 5) == 0)
1148 name += 5;
1149 if (strncmp(name, "got/", 4) == 0)
1150 continue;
1151 if (strncmp(name, "heads/", 6) == 0)
1152 name += 6;
1153 if (strncmp(name, "remotes/", 8) == 0)
1154 name += 8;
1155 if (strncmp(name, "tags/", 5) == 0) {
1156 err = got_object_open_as_tag(&tag, repo, re->id);
1157 if (err) {
1158 if (err->code != GOT_ERR_OBJ_TYPE)
1159 break;
1160 /* Ref points at something other than a tag. */
1161 err = NULL;
1162 tag = NULL;
1165 cmp = got_object_id_cmp(tag ?
1166 got_object_tag_get_object_id(tag) : re->id, id);
1167 if (tag)
1168 got_object_tag_close(tag);
1169 if (cmp != 0)
1170 continue;
1171 s = *refs_str;
1172 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 s ? ", " : "", name) == -1) {
1174 err = got_error_from_errno("asprintf");
1175 free(s);
1176 *refs_str = NULL;
1177 break;
1179 free(s);
1182 return err;
1185 static const struct got_error *
1186 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1187 int col_tab_align)
1189 char *smallerthan, *at;
1191 smallerthan = strchr(author, '<');
1192 if (smallerthan && smallerthan[1] != '\0')
1193 author = smallerthan + 1;
1194 at = strchr(author, '@');
1195 if (at)
1196 *at = '\0';
1197 return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 static const struct got_error *
1201 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 struct got_object_id *id, struct got_reflist_head *refs,
1203 const size_t date_display_cols, int author_display_cols,
1204 struct tog_colors *colors)
1206 const struct got_error *err = NULL;
1207 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 char *logmsg0 = NULL, *logmsg = NULL;
1209 char *author = NULL;
1210 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 int author_width, logmsg_width;
1212 char *newline, *line = NULL;
1213 int col, limit;
1214 const int avail = view->ncols;
1215 struct tm tm;
1216 time_t committer_time;
1217 struct tog_color *tc;
1219 committer_time = got_object_commit_get_committer_time(commit);
1220 if (localtime_r(&committer_time, &tm) == NULL)
1221 return got_error_from_errno("localtime_r");
1222 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1223 >= sizeof(datebuf))
1224 return got_error(GOT_ERR_NO_SPACE);
1226 if (avail <= date_display_cols)
1227 limit = MIN(sizeof(datebuf) - 1, avail);
1228 else
1229 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 tc = get_color(colors, TOG_COLOR_DATE);
1231 if (tc)
1232 wattr_on(view->window,
1233 COLOR_PAIR(tc->colorpair), NULL);
1234 waddnstr(view->window, datebuf, limit);
1235 if (tc)
1236 wattr_off(view->window,
1237 COLOR_PAIR(tc->colorpair), NULL);
1238 col = limit;
1239 if (col > avail)
1240 goto done;
1242 if (avail >= 120) {
1243 char *id_str;
1244 err = got_object_id_str(&id_str, id);
1245 if (err)
1246 goto done;
1247 tc = get_color(colors, TOG_COLOR_COMMIT);
1248 if (tc)
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 wprintw(view->window, "%.8s ", id_str);
1252 if (tc)
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1255 free(id_str);
1256 col += 9;
1257 if (col > avail)
1258 goto done;
1261 author = strdup(got_object_commit_get_author(commit));
1262 if (author == NULL) {
1263 err = got_error_from_errno("strdup");
1264 goto done;
1266 err = format_author(&wauthor, &author_width, author, avail - col, col);
1267 if (err)
1268 goto done;
1269 tc = get_color(colors, TOG_COLOR_AUTHOR);
1270 if (tc)
1271 wattr_on(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1273 waddwstr(view->window, wauthor);
1274 if (tc)
1275 wattr_off(view->window,
1276 COLOR_PAIR(tc->colorpair), NULL);
1277 col += author_width;
1278 while (col < avail && author_width < author_display_cols + 2) {
1279 waddch(view->window, ' ');
1280 col++;
1281 author_width++;
1283 if (col > avail)
1284 goto done;
1286 err = got_object_commit_get_logmsg(&logmsg0, commit);
1287 if (err)
1288 goto done;
1289 logmsg = logmsg0;
1290 while (*logmsg == '\n')
1291 logmsg++;
1292 newline = strchr(logmsg, '\n');
1293 if (newline)
1294 *newline = '\0';
1295 limit = avail - col;
1296 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1297 if (err)
1298 goto done;
1299 waddwstr(view->window, wlogmsg);
1300 col += logmsg_width;
1301 while (col < avail) {
1302 waddch(view->window, ' ');
1303 col++;
1305 done:
1306 free(logmsg0);
1307 free(wlogmsg);
1308 free(author);
1309 free(wauthor);
1310 free(line);
1311 return err;
1314 static struct commit_queue_entry *
1315 alloc_commit_queue_entry(struct got_commit_object *commit,
1316 struct got_object_id *id)
1318 struct commit_queue_entry *entry;
1320 entry = calloc(1, sizeof(*entry));
1321 if (entry == NULL)
1322 return NULL;
1324 entry->id = id;
1325 entry->commit = commit;
1326 return entry;
1329 static void
1330 pop_commit(struct commit_queue *commits)
1332 struct commit_queue_entry *entry;
1334 entry = TAILQ_FIRST(&commits->head);
1335 TAILQ_REMOVE(&commits->head, entry, entry);
1336 got_object_commit_close(entry->commit);
1337 commits->ncommits--;
1338 /* Don't free entry->id! It is owned by the commit graph. */
1339 free(entry);
1342 static void
1343 free_commits(struct commit_queue *commits)
1345 while (!TAILQ_EMPTY(&commits->head))
1346 pop_commit(commits);
1349 static const struct got_error *
1350 match_commit(int *have_match, struct got_object_id *id,
1351 struct got_commit_object *commit, regex_t *regex)
1353 const struct got_error *err = NULL;
1354 regmatch_t regmatch;
1355 char *id_str = NULL, *logmsg = NULL;
1357 *have_match = 0;
1359 err = got_object_id_str(&id_str, id);
1360 if (err)
1361 return err;
1363 err = got_object_commit_get_logmsg(&logmsg, commit);
1364 if (err)
1365 goto done;
1367 if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 &regmatch, 0) == 0 ||
1369 regexec(regex, got_object_commit_get_committer(commit), 1,
1370 &regmatch, 0) == 0 ||
1371 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1372 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1373 *have_match = 1;
1374 done:
1375 free(id_str);
1376 free(logmsg);
1377 return err;
1380 static const struct got_error *
1381 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 int minqueue, struct got_repository *repo, const char *path,
1383 int *searching, int *search_next_done, regex_t *regex)
1385 const struct got_error *err = NULL;
1386 int nqueued = 0, have_match = 0;
1389 * We keep all commits open throughout the lifetime of the log
1390 * view in order to avoid having to re-fetch commits from disk
1391 * while updating the display.
1393 while (nqueued < minqueue ||
1394 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 struct got_object_id *id;
1396 struct got_commit_object *commit;
1397 struct commit_queue_entry *entry;
1398 int errcode;
1400 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 if (err || id == NULL)
1402 break;
1404 err = got_object_open_as_commit(&commit, repo, id);
1405 if (err)
1406 break;
1407 entry = alloc_commit_queue_entry(commit, id);
1408 if (entry == NULL) {
1409 err = got_error_from_errno("alloc_commit_queue_entry");
1410 break;
1413 errcode = pthread_mutex_lock(&tog_mutex);
1414 if (errcode) {
1415 err = got_error_set_errno(errcode,
1416 "pthread_mutex_lock");
1417 break;
1420 entry->idx = commits->ncommits;
1421 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1422 nqueued++;
1423 commits->ncommits++;
1425 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 err = match_commit(&have_match, id, commit, regex);
1429 errcode = pthread_mutex_unlock(&tog_mutex);
1430 if (errcode && err == NULL)
1431 err = got_error_set_errno(errcode,
1432 "pthread_mutex_unlock");
1434 if (err || have_match)
1435 break;
1438 return err;
1441 static const struct got_error *
1442 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1443 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1444 struct commit_queue *commits, int selected_idx, int limit,
1445 struct got_reflist_head *refs, const char *path, int commits_needed,
1446 struct tog_colors *colors)
1448 const struct got_error *err = NULL;
1449 struct tog_log_view_state *s = &view->state.log;
1450 struct commit_queue_entry *entry;
1451 int width;
1452 int ncommits, author_cols = 4;
1453 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1454 char *refs_str = NULL;
1455 wchar_t *wline;
1456 struct tog_color *tc;
1457 static const size_t date_display_cols = 12;
1459 entry = first;
1460 ncommits = 0;
1461 while (entry) {
1462 if (ncommits == selected_idx) {
1463 *selected = entry;
1464 break;
1466 entry = TAILQ_NEXT(entry, entry);
1467 ncommits++;
1470 if (*selected && !(view->searching && view->search_next_done == 0)) {
1471 err = got_object_id_str(&id_str, (*selected)->id);
1472 if (err)
1473 return err;
1474 if (refs) {
1475 err = build_refs_str(&refs_str, refs, (*selected)->id,
1476 s->repo);
1477 if (err)
1478 goto done;
1482 if (commits_needed == 0)
1483 halfdelay(10); /* disable fast refresh */
1485 if (asprintf(&ncommits_str, " [%d/%d] %s",
1486 entry ? entry->idx + 1 : 0, commits->ncommits,
1487 commits_needed > 0 ?
1488 (view->searching && view->search_next_done == 0
1489 ? "searching..." : "loading... ") :
1490 (refs_str ? refs_str : "")) == -1) {
1491 err = got_error_from_errno("asprintf");
1492 goto done;
1495 if (path && strcmp(path, "/") != 0) {
1496 if (asprintf(&header, "commit %s %s%s",
1497 id_str ? id_str : "........................................",
1498 path, ncommits_str) == -1) {
1499 err = got_error_from_errno("asprintf");
1500 header = NULL;
1501 goto done;
1503 } else if (asprintf(&header, "commit %s%s",
1504 id_str ? id_str : "........................................",
1505 ncommits_str) == -1) {
1506 err = got_error_from_errno("asprintf");
1507 header = NULL;
1508 goto done;
1510 err = format_line(&wline, &width, header, view->ncols, 0);
1511 if (err)
1512 goto done;
1514 werase(view->window);
1516 if (view_needs_focus_indication(view))
1517 wstandout(view->window);
1518 tc = get_color(colors, TOG_COLOR_COMMIT);
1519 if (tc)
1520 wattr_on(view->window,
1521 COLOR_PAIR(tc->colorpair), NULL);
1522 waddwstr(view->window, wline);
1523 if (tc)
1524 wattr_off(view->window,
1525 COLOR_PAIR(tc->colorpair), NULL);
1526 while (width < view->ncols) {
1527 waddch(view->window, ' ');
1528 width++;
1530 if (view_needs_focus_indication(view))
1531 wstandend(view->window);
1532 free(wline);
1533 if (limit <= 1)
1534 goto done;
1536 /* Grow author column size if necessary. */
1537 entry = first;
1538 ncommits = 0;
1539 while (entry) {
1540 char *author;
1541 wchar_t *wauthor;
1542 int width;
1543 if (ncommits >= limit - 1)
1544 break;
1545 author = strdup(got_object_commit_get_author(entry->commit));
1546 if (author == NULL) {
1547 err = got_error_from_errno("strdup");
1548 goto done;
1550 err = format_author(&wauthor, &width, author, COLS,
1551 date_display_cols);
1552 if (author_cols < width)
1553 author_cols = width;
1554 free(wauthor);
1555 free(author);
1556 ncommits++;
1557 entry = TAILQ_NEXT(entry, entry);
1560 entry = first;
1561 *last = first;
1562 ncommits = 0;
1563 while (entry) {
1564 if (ncommits >= limit - 1)
1565 break;
1566 if (ncommits == selected_idx)
1567 wstandout(view->window);
1568 err = draw_commit(view, entry->commit, entry->id, refs,
1569 date_display_cols, author_cols, colors);
1570 if (ncommits == selected_idx)
1571 wstandend(view->window);
1572 if (err)
1573 goto done;
1574 ncommits++;
1575 *last = entry;
1576 entry = TAILQ_NEXT(entry, entry);
1579 view_vborder(view);
1580 done:
1581 free(id_str);
1582 free(refs_str);
1583 free(ncommits_str);
1584 free(header);
1585 return err;
1588 static void
1589 scroll_up(struct tog_view *view,
1590 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1591 struct commit_queue *commits)
1593 struct commit_queue_entry *entry;
1594 int nscrolled = 0;
1596 entry = TAILQ_FIRST(&commits->head);
1597 if (*first_displayed_entry == entry)
1598 return;
1600 entry = *first_displayed_entry;
1601 while (entry && nscrolled < maxscroll) {
1602 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1603 if (entry) {
1604 *first_displayed_entry = entry;
1605 nscrolled++;
1610 static const struct got_error *
1611 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1612 pthread_cond_t *need_commits)
1614 int errcode;
1615 int max_wait = 20;
1617 halfdelay(1); /* fast refresh while loading commits */
1619 while (*commits_needed > 0) {
1620 if (*log_complete)
1621 break;
1623 /* Wake the log thread. */
1624 errcode = pthread_cond_signal(need_commits);
1625 if (errcode)
1626 return got_error_set_errno(errcode,
1627 "pthread_cond_signal");
1628 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1630 * Thread is not done yet; lose a key press
1631 * and let the user retry... this way the GUI
1632 * remains interactive while logging deep paths
1633 * with few commits in history.
1635 return NULL;
1639 return NULL;
1642 static const struct got_error *
1643 scroll_down(struct tog_view *view,
1644 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1645 struct commit_queue_entry **last_displayed_entry,
1646 struct commit_queue *commits, int *log_complete, int *commits_needed,
1647 pthread_cond_t *need_commits)
1649 const struct got_error *err = NULL;
1650 struct commit_queue_entry *pentry;
1651 int nscrolled = 0;
1653 if (*last_displayed_entry == NULL)
1654 return NULL;
1656 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1657 if (pentry == NULL && !*log_complete) {
1659 * Ask the log thread for required amount of commits
1660 * plus some amount of pre-fetching.
1662 (*commits_needed) += maxscroll + 20;
1663 err = trigger_log_thread(0, commits_needed, log_complete,
1664 need_commits);
1665 if (err)
1666 return err;
1669 do {
1670 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1671 if (pentry == NULL)
1672 break;
1674 *last_displayed_entry = pentry;
1676 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1677 if (pentry == NULL)
1678 break;
1679 *first_displayed_entry = pentry;
1680 } while (++nscrolled < maxscroll);
1682 return err;
1685 static const struct got_error *
1686 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1687 struct got_commit_object *commit, struct got_object_id *commit_id,
1688 struct tog_view *log_view, struct got_reflist_head *refs,
1689 struct got_repository *repo)
1691 const struct got_error *err;
1692 struct got_object_qid *parent_id;
1693 struct tog_view *diff_view;
1695 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1696 if (diff_view == NULL)
1697 return got_error_from_errno("view_open");
1699 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1700 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1701 commit_id, log_view, refs, repo);
1702 if (err == NULL)
1703 *new_view = diff_view;
1704 return err;
1707 static const struct got_error *
1708 tree_view_visit_subtree(struct got_tree_object *subtree,
1709 struct tog_tree_view_state *s)
1711 struct tog_parent_tree *parent;
1713 parent = calloc(1, sizeof(*parent));
1714 if (parent == NULL)
1715 return got_error_from_errno("calloc");
1717 parent->tree = s->tree;
1718 parent->first_displayed_entry = s->first_displayed_entry;
1719 parent->selected_entry = s->selected_entry;
1720 parent->selected = s->selected;
1721 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1722 s->tree = subtree;
1723 s->selected = 0;
1724 s->first_displayed_entry = NULL;
1725 return NULL;
1729 static const struct got_error *
1730 browse_commit_tree(struct tog_view **new_view, int begin_x,
1731 struct commit_queue_entry *entry, const char *path,
1732 struct got_reflist_head *refs, struct got_repository *repo)
1734 const struct got_error *err = NULL;
1735 struct got_tree_object *tree;
1736 struct tog_tree_view_state *s;
1737 struct tog_view *tree_view;
1738 char *slash, *subpath = NULL;
1739 const char *p;
1741 err = got_object_open_as_tree(&tree, repo,
1742 got_object_commit_get_tree_id(entry->commit));
1743 if (err)
1744 return err;
1746 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1747 if (tree_view == NULL)
1748 return got_error_from_errno("view_open");
1750 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1751 if (err) {
1752 got_object_tree_close(tree);
1753 return err;
1755 s = &tree_view->state.tree;
1757 *new_view = tree_view;
1759 if (got_path_is_root_dir(path))
1760 return NULL;
1762 /* Walk the path and open corresponding tree objects. */
1763 p = path;
1764 while (*p) {
1765 struct got_tree_entry *te;
1766 struct got_object_id *tree_id;
1767 char *te_name;
1769 while (p[0] == '/')
1770 p++;
1772 /* Ensure the correct subtree entry is selected. */
1773 slash = strchr(p, '/');
1774 if (slash == NULL)
1775 te_name = strdup(p);
1776 else
1777 te_name = strndup(p, slash - p);
1778 if (te_name == NULL) {
1779 err = got_error_from_errno("strndup");
1780 break;
1782 te = got_object_tree_find_entry(s->tree, te_name);
1783 if (te == NULL) {
1784 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1785 free(te_name);
1786 break;
1788 free(te_name);
1789 s->selected_entry = te;
1790 s->selected = got_tree_entry_get_index(te);
1791 if (s->tree != s->root)
1792 s->selected++; /* skip '..' */
1794 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1795 /* Jump to this file's entry. */
1796 s->first_displayed_entry = s->selected_entry;
1797 s->selected = 0;
1798 break;
1801 slash = strchr(p, '/');
1802 if (slash)
1803 subpath = strndup(path, slash - path);
1804 else
1805 subpath = strdup(path);
1806 if (subpath == NULL) {
1807 err = got_error_from_errno("strdup");
1808 break;
1811 err = got_object_id_by_path(&tree_id, repo, entry->id,
1812 subpath);
1813 if (err)
1814 break;
1816 err = got_object_open_as_tree(&tree, repo, tree_id);
1817 free(tree_id);
1818 if (err)
1819 break;
1821 err = tree_view_visit_subtree(tree, s);
1822 if (err) {
1823 got_object_tree_close(tree);
1824 break;
1826 if (slash == NULL)
1827 break;
1828 free(subpath);
1829 subpath = NULL;
1830 p = slash;
1833 free(subpath);
1834 return err;
1837 static const struct got_error *
1838 block_signals_used_by_main_thread(void)
1840 sigset_t sigset;
1841 int errcode;
1843 if (sigemptyset(&sigset) == -1)
1844 return got_error_from_errno("sigemptyset");
1846 /* tog handles SIGWINCH and SIGCONT */
1847 if (sigaddset(&sigset, SIGWINCH) == -1)
1848 return got_error_from_errno("sigaddset");
1849 if (sigaddset(&sigset, SIGCONT) == -1)
1850 return got_error_from_errno("sigaddset");
1852 /* ncurses handles SIGTSTP */
1853 if (sigaddset(&sigset, SIGTSTP) == -1)
1854 return got_error_from_errno("sigaddset");
1856 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1857 if (errcode)
1858 return got_error_set_errno(errcode, "pthread_sigmask");
1860 return NULL;
1863 static void *
1864 log_thread(void *arg)
1866 const struct got_error *err = NULL;
1867 int errcode = 0;
1868 struct tog_log_thread_args *a = arg;
1869 int done = 0;
1871 err = block_signals_used_by_main_thread();
1872 if (err)
1873 return (void *)err;
1875 while (!done && !err && !tog_sigpipe_received) {
1876 err = queue_commits(a->graph, a->commits, 1, a->repo,
1877 a->in_repo_path, a->searching, a->search_next_done,
1878 a->regex);
1879 if (err) {
1880 if (err->code != GOT_ERR_ITER_COMPLETED)
1881 return (void *)err;
1882 err = NULL;
1883 done = 1;
1884 } else if (a->commits_needed > 0)
1885 a->commits_needed--;
1887 errcode = pthread_mutex_lock(&tog_mutex);
1888 if (errcode) {
1889 err = got_error_set_errno(errcode,
1890 "pthread_mutex_lock");
1891 break;
1892 } else if (*a->quit)
1893 done = 1;
1894 else if (*a->first_displayed_entry == NULL) {
1895 *a->first_displayed_entry =
1896 TAILQ_FIRST(&a->commits->head);
1897 *a->selected_entry = *a->first_displayed_entry;
1900 if (done)
1901 a->commits_needed = 0;
1902 else if (a->commits_needed == 0) {
1903 errcode = pthread_cond_wait(&a->need_commits,
1904 &tog_mutex);
1905 if (errcode)
1906 err = got_error_set_errno(errcode,
1907 "pthread_cond_wait");
1910 errcode = pthread_mutex_unlock(&tog_mutex);
1911 if (errcode && err == NULL)
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_unlock");
1915 a->log_complete = 1;
1916 return (void *)err;
1919 static const struct got_error *
1920 stop_log_thread(struct tog_log_view_state *s)
1922 const struct got_error *err = NULL;
1923 int errcode;
1925 if (s->thread) {
1926 s->quit = 1;
1927 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1928 if (errcode)
1929 return got_error_set_errno(errcode,
1930 "pthread_cond_signal");
1931 errcode = pthread_mutex_unlock(&tog_mutex);
1932 if (errcode)
1933 return got_error_set_errno(errcode,
1934 "pthread_mutex_unlock");
1935 errcode = pthread_join(s->thread, (void **)&err);
1936 if (errcode)
1937 return got_error_set_errno(errcode, "pthread_join");
1938 errcode = pthread_mutex_lock(&tog_mutex);
1939 if (errcode)
1940 return got_error_set_errno(errcode,
1941 "pthread_mutex_lock");
1942 s->thread = NULL;
1945 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1946 if (errcode && err == NULL)
1947 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1949 if (s->thread_args.repo) {
1950 got_repo_close(s->thread_args.repo);
1951 s->thread_args.repo = NULL;
1954 if (s->thread_args.graph) {
1955 got_commit_graph_close(s->thread_args.graph);
1956 s->thread_args.graph = NULL;
1959 return err;
1962 static const struct got_error *
1963 close_log_view(struct tog_view *view)
1965 const struct got_error *err = NULL;
1966 struct tog_log_view_state *s = &view->state.log;
1968 err = stop_log_thread(s);
1969 free_commits(&s->commits);
1970 free(s->in_repo_path);
1971 s->in_repo_path = NULL;
1972 free(s->start_id);
1973 s->start_id = NULL;
1974 return err;
1977 static const struct got_error *
1978 search_start_log_view(struct tog_view *view)
1980 struct tog_log_view_state *s = &view->state.log;
1982 s->matched_entry = NULL;
1983 s->search_entry = NULL;
1984 return NULL;
1987 static const struct got_error *
1988 search_next_log_view(struct tog_view *view)
1990 const struct got_error *err = NULL;
1991 struct tog_log_view_state *s = &view->state.log;
1992 struct commit_queue_entry *entry;
1994 if (!view->searching) {
1995 view->search_next_done = 1;
1996 return NULL;
1999 if (s->search_entry) {
2000 int errcode, ch;
2001 errcode = pthread_mutex_unlock(&tog_mutex);
2002 if (errcode)
2003 return got_error_set_errno(errcode,
2004 "pthread_mutex_unlock");
2005 ch = wgetch(view->window);
2006 errcode = pthread_mutex_lock(&tog_mutex);
2007 if (errcode)
2008 return got_error_set_errno(errcode,
2009 "pthread_mutex_lock");
2010 if (ch == KEY_BACKSPACE) {
2011 view->search_next_done = 1;
2012 return NULL;
2014 if (view->searching == TOG_SEARCH_FORWARD)
2015 entry = TAILQ_NEXT(s->search_entry, entry);
2016 else
2017 entry = TAILQ_PREV(s->search_entry,
2018 commit_queue_head, entry);
2019 } else if (s->matched_entry) {
2020 if (view->searching == TOG_SEARCH_FORWARD)
2021 entry = TAILQ_NEXT(s->selected_entry, entry);
2022 else
2023 entry = TAILQ_PREV(s->selected_entry,
2024 commit_queue_head, entry);
2025 } else {
2026 if (view->searching == TOG_SEARCH_FORWARD)
2027 entry = TAILQ_FIRST(&s->commits.head);
2028 else
2029 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2032 while (1) {
2033 int have_match = 0;
2035 if (entry == NULL) {
2036 if (s->thread_args.log_complete ||
2037 view->searching == TOG_SEARCH_BACKWARD) {
2038 view->search_next_done = 1;
2039 return NULL;
2042 * Poke the log thread for more commits and return,
2043 * allowing the main loop to make progress. Search
2044 * will resume at s->search_entry once we come back.
2046 s->thread_args.commits_needed++;
2047 return trigger_log_thread(1,
2048 &s->thread_args.commits_needed,
2049 &s->thread_args.log_complete,
2050 &s->thread_args.need_commits);
2053 err = match_commit(&have_match, entry->id, entry->commit,
2054 &view->regex);
2055 if (err)
2056 break;
2057 if (have_match) {
2058 view->search_next_done = 1;
2059 s->matched_entry = entry;
2060 break;
2063 s->search_entry = entry;
2064 if (view->searching == TOG_SEARCH_FORWARD)
2065 entry = TAILQ_NEXT(entry, entry);
2066 else
2067 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2070 if (s->matched_entry) {
2071 int cur = s->selected_entry->idx;
2072 while (cur < s->matched_entry->idx) {
2073 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2074 if (err)
2075 return err;
2076 cur++;
2078 while (cur > s->matched_entry->idx) {
2079 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2080 if (err)
2081 return err;
2082 cur--;
2086 s->search_entry = NULL;
2088 return NULL;
2091 static const struct got_error *
2092 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2093 struct got_reflist_head *refs, struct got_repository *repo,
2094 const char *head_ref_name, const char *path, int check_disk)
2096 const struct got_error *err = NULL;
2097 struct tog_log_view_state *s = &view->state.log;
2098 struct got_repository *thread_repo = NULL;
2099 struct got_commit_graph *thread_graph = NULL;
2100 int errcode;
2102 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2103 if (err != NULL)
2104 goto done;
2106 /* The commit queue only contains commits being displayed. */
2107 TAILQ_INIT(&s->commits.head);
2108 s->commits.ncommits = 0;
2110 s->refs = refs;
2111 s->repo = repo;
2112 s->head_ref_name = head_ref_name;
2113 s->start_id = got_object_id_dup(start_id);
2114 if (s->start_id == NULL) {
2115 err = got_error_from_errno("got_object_id_dup");
2116 goto done;
2119 SIMPLEQ_INIT(&s->colors);
2120 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2121 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2122 get_color_value("TOG_COLOR_COMMIT"));
2123 if (err)
2124 goto done;
2125 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2126 get_color_value("TOG_COLOR_AUTHOR"));
2127 if (err) {
2128 free_colors(&s->colors);
2129 goto done;
2131 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2132 get_color_value("TOG_COLOR_DATE"));
2133 if (err) {
2134 free_colors(&s->colors);
2135 goto done;
2139 view->show = show_log_view;
2140 view->input = input_log_view;
2141 view->close = close_log_view;
2142 view->search_start = search_start_log_view;
2143 view->search_next = search_next_log_view;
2145 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2146 if (err)
2147 goto done;
2148 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2149 if (err)
2150 goto done;
2151 err = got_commit_graph_iter_start(thread_graph,
2152 s->start_id, s->repo, NULL, NULL);
2153 if (err)
2154 goto done;
2156 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2157 if (errcode) {
2158 err = got_error_set_errno(errcode, "pthread_cond_init");
2159 goto done;
2162 s->thread_args.commits_needed = view->nlines;
2163 s->thread_args.graph = thread_graph;
2164 s->thread_args.commits = &s->commits;
2165 s->thread_args.in_repo_path = s->in_repo_path;
2166 s->thread_args.start_id = s->start_id;
2167 s->thread_args.repo = thread_repo;
2168 s->thread_args.log_complete = 0;
2169 s->thread_args.quit = &s->quit;
2170 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2171 s->thread_args.selected_entry = &s->selected_entry;
2172 s->thread_args.searching = &view->searching;
2173 s->thread_args.search_next_done = &view->search_next_done;
2174 s->thread_args.regex = &view->regex;
2175 done:
2176 if (err)
2177 close_log_view(view);
2178 return err;
2181 static const struct got_error *
2182 show_log_view(struct tog_view *view)
2184 struct tog_log_view_state *s = &view->state.log;
2186 if (s->thread == NULL) {
2187 int errcode = pthread_create(&s->thread, NULL, log_thread,
2188 &s->thread_args);
2189 if (errcode)
2190 return got_error_set_errno(errcode, "pthread_create");
2193 return draw_commits(view, &s->last_displayed_entry,
2194 &s->selected_entry, s->first_displayed_entry,
2195 &s->commits, s->selected, view->nlines, s->refs,
2196 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2199 static const struct got_error *
2200 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2201 struct tog_view **focus_view, struct tog_view *view, int ch)
2203 const struct got_error *err = NULL;
2204 struct tog_log_view_state *s = &view->state.log;
2205 char *parent_path, *in_repo_path = NULL;
2206 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2207 int begin_x = 0;
2208 struct got_object_id *start_id;
2210 switch (ch) {
2211 case 'q':
2212 s->quit = 1;
2213 break;
2214 case 'k':
2215 case KEY_UP:
2216 case '<':
2217 case ',':
2218 if (s->first_displayed_entry == NULL)
2219 break;
2220 if (s->selected > 0)
2221 s->selected--;
2222 else
2223 scroll_up(view, &s->first_displayed_entry, 1,
2224 &s->commits);
2225 break;
2226 case KEY_PPAGE:
2227 case CTRL('b'):
2228 if (s->first_displayed_entry == NULL)
2229 break;
2230 if (TAILQ_FIRST(&s->commits.head) ==
2231 s->first_displayed_entry) {
2232 s->selected = 0;
2233 break;
2235 scroll_up(view, &s->first_displayed_entry,
2236 view->nlines, &s->commits);
2237 break;
2238 case 'j':
2239 case KEY_DOWN:
2240 case '>':
2241 case '.':
2242 if (s->first_displayed_entry == NULL)
2243 break;
2244 if (s->selected < MIN(view->nlines - 2,
2245 s->commits.ncommits - 1)) {
2246 s->selected++;
2247 break;
2249 err = scroll_down(view, &s->first_displayed_entry, 1,
2250 &s->last_displayed_entry, &s->commits,
2251 &s->thread_args.log_complete,
2252 &s->thread_args.commits_needed,
2253 &s->thread_args.need_commits);
2254 break;
2255 case KEY_NPAGE:
2256 case CTRL('f'): {
2257 struct commit_queue_entry *first;
2258 first = s->first_displayed_entry;
2259 if (first == NULL)
2260 break;
2261 err = scroll_down(view, &s->first_displayed_entry,
2262 view->nlines, &s->last_displayed_entry,
2263 &s->commits, &s->thread_args.log_complete,
2264 &s->thread_args.commits_needed,
2265 &s->thread_args.need_commits);
2266 if (err)
2267 break;
2268 if (first == s->first_displayed_entry &&
2269 s->selected < MIN(view->nlines - 2,
2270 s->commits.ncommits - 1)) {
2271 /* can't scroll further down */
2272 s->selected = MIN(view->nlines - 2,
2273 s->commits.ncommits - 1);
2275 err = NULL;
2276 break;
2278 case KEY_RESIZE:
2279 if (s->selected > view->nlines - 2)
2280 s->selected = view->nlines - 2;
2281 if (s->selected > s->commits.ncommits - 1)
2282 s->selected = s->commits.ncommits - 1;
2283 break;
2284 case KEY_ENTER:
2285 case ' ':
2286 case '\r':
2287 if (s->selected_entry == NULL)
2288 break;
2289 if (view_is_parent_view(view))
2290 begin_x = view_split_begin_x(view->begin_x);
2291 err = open_diff_view_for_commit(&diff_view, begin_x,
2292 s->selected_entry->commit, s->selected_entry->id,
2293 view, s->refs, s->repo);
2294 if (err)
2295 break;
2296 if (view_is_parent_view(view)) {
2297 err = view_close_child(view);
2298 if (err)
2299 return err;
2300 err = view_set_child(view, diff_view);
2301 if (err) {
2302 view_close(diff_view);
2303 break;
2305 *focus_view = diff_view;
2306 view->child_focussed = 1;
2307 } else
2308 *new_view = diff_view;
2309 break;
2310 case 't':
2311 if (s->selected_entry == NULL)
2312 break;
2313 if (view_is_parent_view(view))
2314 begin_x = view_split_begin_x(view->begin_x);
2315 err = browse_commit_tree(&tree_view, begin_x,
2316 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2317 if (err)
2318 break;
2319 if (view_is_parent_view(view)) {
2320 err = view_close_child(view);
2321 if (err)
2322 return err;
2323 err = view_set_child(view, tree_view);
2324 if (err) {
2325 view_close(tree_view);
2326 break;
2328 *focus_view = tree_view;
2329 view->child_focussed = 1;
2330 } else
2331 *new_view = tree_view;
2332 break;
2333 case KEY_BACKSPACE:
2334 if (strcmp(s->in_repo_path, "/") == 0)
2335 break;
2336 parent_path = dirname(s->in_repo_path);
2337 if (parent_path && strcmp(parent_path, ".") != 0) {
2338 err = stop_log_thread(s);
2339 if (err)
2340 return err;
2341 lv = view_open(view->nlines, view->ncols,
2342 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2343 if (lv == NULL)
2344 return got_error_from_errno(
2345 "view_open");
2346 err = open_log_view(lv, s->start_id, s->refs,
2347 s->repo, s->head_ref_name, parent_path, 0);
2348 if (err)
2349 return err;;
2350 if (view_is_parent_view(view))
2351 *new_view = lv;
2352 else {
2353 view_set_child(view->parent, lv);
2354 *focus_view = lv;
2356 return NULL;
2358 break;
2359 case CTRL('l'):
2360 err = stop_log_thread(s);
2361 if (err)
2362 return err;
2363 lv = view_open(view->nlines, view->ncols,
2364 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2365 if (lv == NULL)
2366 return got_error_from_errno("view_open");
2367 err = got_repo_match_object_id(&start_id, NULL,
2368 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2369 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2370 if (err) {
2371 view_close(lv);
2372 return err;
2374 in_repo_path = strdup(s->in_repo_path);
2375 if (in_repo_path == NULL) {
2376 free(start_id);
2377 view_close(lv);
2378 return got_error_from_errno("strdup");
2380 got_ref_list_free(s->refs);
2381 err = got_ref_list(s->refs, s->repo, NULL,
2382 got_ref_cmp_by_name, NULL);
2383 if (err) {
2384 free(start_id);
2385 view_close(lv);
2386 return err;
2388 err = open_log_view(lv, start_id, s->refs, s->repo,
2389 s->head_ref_name, in_repo_path, 0);
2390 if (err) {
2391 free(start_id);
2392 view_close(lv);
2393 return err;;
2395 *dead_view = view;
2396 *new_view = lv;
2397 break;
2398 default:
2399 break;
2402 return err;
2405 static const struct got_error *
2406 apply_unveil(const char *repo_path, const char *worktree_path)
2408 const struct got_error *error;
2410 #ifdef PROFILE
2411 if (unveil("gmon.out", "rwc") != 0)
2412 return got_error_from_errno2("unveil", "gmon.out");
2413 #endif
2414 if (repo_path && unveil(repo_path, "r") != 0)
2415 return got_error_from_errno2("unveil", repo_path);
2417 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2418 return got_error_from_errno2("unveil", worktree_path);
2420 if (unveil("/tmp", "rwc") != 0)
2421 return got_error_from_errno2("unveil", "/tmp");
2423 error = got_privsep_unveil_exec_helpers();
2424 if (error != NULL)
2425 return error;
2427 if (unveil(NULL, NULL) != 0)
2428 return got_error_from_errno("unveil");
2430 return NULL;
2433 static void
2434 init_curses(void)
2436 initscr();
2437 cbreak();
2438 halfdelay(1); /* Do fast refresh while initial view is loading. */
2439 noecho();
2440 nonl();
2441 intrflush(stdscr, FALSE);
2442 keypad(stdscr, TRUE);
2443 curs_set(0);
2444 if (getenv("TOG_COLORS") != NULL) {
2445 start_color();
2446 use_default_colors();
2448 signal(SIGWINCH, tog_sigwinch);
2449 signal(SIGPIPE, tog_sigpipe);
2450 signal(SIGCONT, tog_sigcont);
2453 static const struct got_error *
2454 cmd_log(int argc, char *argv[])
2456 const struct got_error *error;
2457 struct got_repository *repo = NULL;
2458 struct got_worktree *worktree = NULL;
2459 struct got_reflist_head refs;
2460 struct got_object_id *start_id = NULL;
2461 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2462 char *start_commit = NULL, *head_ref_name = NULL;
2463 int ch;
2464 struct tog_view *view;
2466 SIMPLEQ_INIT(&refs);
2468 #ifndef PROFILE
2469 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2470 NULL) == -1)
2471 err(1, "pledge");
2472 #endif
2474 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2475 switch (ch) {
2476 case 'c':
2477 start_commit = optarg;
2478 break;
2479 case 'r':
2480 repo_path = realpath(optarg, NULL);
2481 if (repo_path == NULL)
2482 return got_error_from_errno2("realpath",
2483 optarg);
2484 break;
2485 default:
2486 usage_log();
2487 /* NOTREACHED */
2491 argc -= optind;
2492 argv += optind;
2494 cwd = getcwd(NULL, 0);
2495 if (cwd == NULL) {
2496 error = got_error_from_errno("getcwd");
2497 goto done;
2499 error = got_worktree_open(&worktree, cwd);
2500 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2501 goto done;
2502 error = NULL;
2504 if (argc == 0) {
2505 path = strdup("");
2506 if (path == NULL) {
2507 error = got_error_from_errno("strdup");
2508 goto done;
2510 } else if (argc == 1) {
2511 if (worktree) {
2512 error = got_worktree_resolve_path(&path, worktree,
2513 argv[0]);
2514 if (error)
2515 goto done;
2516 } else {
2517 path = strdup(argv[0]);
2518 if (path == NULL) {
2519 error = got_error_from_errno("strdup");
2520 goto done;
2523 } else
2524 usage_log();
2526 if (repo_path == NULL) {
2527 if (worktree)
2528 repo_path = strdup(
2529 got_worktree_get_repo_path(worktree));
2530 else
2531 repo_path = strdup(cwd);
2533 if (repo_path == NULL) {
2534 error = got_error_from_errno("strdup");
2535 goto done;
2538 init_curses();
2540 error = got_repo_open(&repo, repo_path, NULL);
2541 if (error != NULL)
2542 goto done;
2544 error = apply_unveil(got_repo_get_path(repo),
2545 worktree ? got_worktree_get_root_path(worktree) : NULL);
2546 if (error)
2547 goto done;
2549 if (start_commit == NULL)
2550 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2551 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2552 GOT_OBJ_TYPE_COMMIT, 1, repo);
2553 else
2554 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2555 GOT_OBJ_TYPE_COMMIT, 1, repo);
2556 if (error != NULL)
2557 goto done;
2559 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2560 if (error)
2561 goto done;
2563 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2564 if (view == NULL) {
2565 error = got_error_from_errno("view_open");
2566 goto done;
2568 if (worktree) {
2569 head_ref_name = strdup(
2570 got_worktree_get_head_ref_name(worktree));
2571 if (head_ref_name == NULL) {
2572 error = got_error_from_errno("strdup");
2573 goto done;
2576 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2577 path, 1);
2578 if (error)
2579 goto done;
2580 if (worktree) {
2581 /* Release work tree lock. */
2582 got_worktree_close(worktree);
2583 worktree = NULL;
2585 error = view_loop(view);
2586 done:
2587 free(repo_path);
2588 free(cwd);
2589 free(path);
2590 free(start_id);
2591 free(head_ref_name);
2592 if (repo)
2593 got_repo_close(repo);
2594 if (worktree)
2595 got_worktree_close(worktree);
2596 got_ref_list_free(&refs);
2597 return error;
2600 __dead static void
2601 usage_diff(void)
2603 endwin();
2604 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2605 getprogname());
2606 exit(1);
2609 static char *
2610 parse_next_line(FILE *f, size_t *len)
2612 char *line;
2613 size_t linelen;
2614 size_t lineno;
2615 const char delim[3] = { '\0', '\0', '\0'};
2617 line = fparseln(f, &linelen, &lineno, delim, 0);
2618 if (len)
2619 *len = linelen;
2620 return line;
2623 static int
2624 match_line(const char *line, regex_t *regex)
2626 regmatch_t regmatch;
2628 return regexec(regex, line, 1, &regmatch, 0) == 0;
2631 struct tog_color *
2632 match_color(struct tog_colors *colors, const char *line)
2634 struct tog_color *tc = NULL;
2636 SIMPLEQ_FOREACH(tc, colors, entry) {
2637 if (match_line(line, &tc->regex))
2638 return tc;
2641 return NULL;
2644 static const struct got_error *
2645 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2646 int *last_displayed_line, int *eof, int max_lines, char *header,
2647 struct tog_colors *colors)
2649 const struct got_error *err;
2650 int nlines = 0, nprinted = 0;
2651 char *line;
2652 struct tog_color *tc;
2653 size_t len;
2654 wchar_t *wline;
2655 int width;
2657 rewind(f);
2658 werase(view->window);
2660 if (header) {
2661 err = format_line(&wline, &width, header, view->ncols, 0);
2662 if (err) {
2663 return err;
2666 if (view_needs_focus_indication(view))
2667 wstandout(view->window);
2668 waddwstr(view->window, wline);
2669 if (view_needs_focus_indication(view))
2670 wstandend(view->window);
2671 if (width <= view->ncols - 1)
2672 waddch(view->window, '\n');
2674 if (max_lines <= 1)
2675 return NULL;
2676 max_lines--;
2679 *eof = 0;
2680 while (nprinted < max_lines) {
2681 line = parse_next_line(f, &len);
2682 if (line == NULL) {
2683 *eof = 1;
2684 break;
2686 if (++nlines < *first_displayed_line) {
2687 free(line);
2688 continue;
2691 err = format_line(&wline, &width, line, view->ncols, 0);
2692 if (err) {
2693 free(line);
2694 return err;
2697 tc = match_color(colors, line);
2698 if (tc)
2699 wattr_on(view->window,
2700 COLOR_PAIR(tc->colorpair), NULL);
2701 waddwstr(view->window, wline);
2702 if (tc)
2703 wattr_off(view->window,
2704 COLOR_PAIR(tc->colorpair), NULL);
2705 if (width <= view->ncols - 1)
2706 waddch(view->window, '\n');
2707 if (++nprinted == 1)
2708 *first_displayed_line = nlines;
2709 free(line);
2710 free(wline);
2711 wline = NULL;
2713 *last_displayed_line = nlines;
2715 view_vborder(view);
2717 if (*eof) {
2718 while (nprinted < view->nlines) {
2719 waddch(view->window, '\n');
2720 nprinted++;
2723 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2724 if (err) {
2725 return err;
2728 wstandout(view->window);
2729 waddwstr(view->window, wline);
2730 wstandend(view->window);
2733 return NULL;
2736 static char *
2737 get_datestr(time_t *time, char *datebuf)
2739 struct tm mytm, *tm;
2740 char *p, *s;
2742 tm = gmtime_r(time, &mytm);
2743 if (tm == NULL)
2744 return NULL;
2745 s = asctime_r(tm, datebuf);
2746 if (s == NULL)
2747 return NULL;
2748 p = strchr(s, '\n');
2749 if (p)
2750 *p = '\0';
2751 return s;
2754 static const struct got_error *
2755 write_commit_info(struct got_object_id *commit_id,
2756 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2758 const struct got_error *err = NULL;
2759 char datebuf[26], *datestr;
2760 struct got_commit_object *commit;
2761 char *id_str = NULL, *logmsg = NULL;
2762 time_t committer_time;
2763 const char *author, *committer;
2764 char *refs_str = NULL;
2766 if (refs) {
2767 err = build_refs_str(&refs_str, refs, commit_id, repo);
2768 if (err)
2769 return err;
2772 err = got_object_open_as_commit(&commit, repo, commit_id);
2773 if (err)
2774 return err;
2776 err = got_object_id_str(&id_str, commit_id);
2777 if (err) {
2778 err = got_error_from_errno("got_object_id_str");
2779 goto done;
2782 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2783 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2784 err = got_error_from_errno("fprintf");
2785 goto done;
2787 if (fprintf(outfile, "from: %s\n",
2788 got_object_commit_get_author(commit)) < 0) {
2789 err = got_error_from_errno("fprintf");
2790 goto done;
2792 committer_time = got_object_commit_get_committer_time(commit);
2793 datestr = get_datestr(&committer_time, datebuf);
2794 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2795 err = got_error_from_errno("fprintf");
2796 goto done;
2798 author = got_object_commit_get_author(commit);
2799 committer = got_object_commit_get_committer(commit);
2800 if (strcmp(author, committer) != 0 &&
2801 fprintf(outfile, "via: %s\n", committer) < 0) {
2802 err = got_error_from_errno("fprintf");
2803 goto done;
2805 err = got_object_commit_get_logmsg(&logmsg, commit);
2806 if (err)
2807 goto done;
2808 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2809 err = got_error_from_errno("fprintf");
2810 goto done;
2812 done:
2813 free(id_str);
2814 free(logmsg);
2815 free(refs_str);
2816 got_object_commit_close(commit);
2817 return err;
2820 static const struct got_error *
2821 create_diff(struct tog_diff_view_state *s)
2823 const struct got_error *err = NULL;
2824 FILE *f = NULL;
2825 int obj_type;
2827 f = got_opentemp();
2828 if (f == NULL) {
2829 err = got_error_from_errno("got_opentemp");
2830 goto done;
2832 if (s->f && fclose(s->f) != 0) {
2833 err = got_error_from_errno("fclose");
2834 goto done;
2836 s->f = f;
2838 if (s->id1)
2839 err = got_object_get_type(&obj_type, s->repo, s->id1);
2840 else
2841 err = got_object_get_type(&obj_type, s->repo, s->id2);
2842 if (err)
2843 goto done;
2845 switch (obj_type) {
2846 case GOT_OBJ_TYPE_BLOB:
2847 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2848 s->diff_context, 0, s->repo, f);
2849 break;
2850 case GOT_OBJ_TYPE_TREE:
2851 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2852 s->diff_context, 0, s->repo, f);
2853 break;
2854 case GOT_OBJ_TYPE_COMMIT: {
2855 const struct got_object_id_queue *parent_ids;
2856 struct got_object_qid *pid;
2857 struct got_commit_object *commit2;
2859 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2860 if (err)
2861 break;
2862 /* Show commit info if we're diffing to a parent/root commit. */
2863 if (s->id1 == NULL)
2864 write_commit_info(s->id2, s->refs, s->repo, f);
2865 else {
2866 parent_ids = got_object_commit_get_parent_ids(commit2);
2867 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2868 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2869 write_commit_info(s->id2, s->refs,
2870 s->repo, f);
2871 break;
2875 got_object_commit_close(commit2);
2877 err = got_diff_objects_as_commits(s->id1, s->id2,
2878 s->diff_context, 0, s->repo, f);
2879 break;
2881 default:
2882 err = got_error(GOT_ERR_OBJ_TYPE);
2883 break;
2885 done:
2886 if (f && fflush(f) != 0 && err == NULL)
2887 err = got_error_from_errno("fflush");
2888 return err;
2891 static void
2892 diff_view_indicate_progress(struct tog_view *view)
2894 mvwaddstr(view->window, 0, 0, "diffing...");
2895 update_panels();
2896 doupdate();
2899 static const struct got_error *
2900 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2901 struct got_object_id *id2, struct tog_view *log_view,
2902 struct got_reflist_head *refs, struct got_repository *repo)
2904 const struct got_error *err;
2906 if (id1 != NULL && id2 != NULL) {
2907 int type1, type2;
2908 err = got_object_get_type(&type1, repo, id1);
2909 if (err)
2910 return err;
2911 err = got_object_get_type(&type2, repo, id2);
2912 if (err)
2913 return err;
2915 if (type1 != type2)
2916 return got_error(GOT_ERR_OBJ_TYPE);
2919 if (id1) {
2920 view->state.diff.id1 = got_object_id_dup(id1);
2921 if (view->state.diff.id1 == NULL)
2922 return got_error_from_errno("got_object_id_dup");
2923 } else
2924 view->state.diff.id1 = NULL;
2926 view->state.diff.id2 = got_object_id_dup(id2);
2927 if (view->state.diff.id2 == NULL) {
2928 free(view->state.diff.id1);
2929 view->state.diff.id1 = NULL;
2930 return got_error_from_errno("got_object_id_dup");
2932 view->state.diff.f = NULL;
2933 view->state.diff.first_displayed_line = 1;
2934 view->state.diff.last_displayed_line = view->nlines;
2935 view->state.diff.diff_context = 3;
2936 view->state.diff.log_view = log_view;
2937 view->state.diff.repo = repo;
2938 view->state.diff.refs = refs;
2939 SIMPLEQ_INIT(&view->state.diff.colors);
2941 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2942 err = add_color(&view->state.diff.colors,
2943 "^-", TOG_COLOR_DIFF_MINUS,
2944 get_color_value("TOG_COLOR_DIFF_MINUS"));
2945 if (err)
2946 return err;
2947 err = add_color(&view->state.diff.colors, "^\\+",
2948 TOG_COLOR_DIFF_PLUS,
2949 get_color_value("TOG_COLOR_DIFF_PLUS"));
2950 if (err) {
2951 free_colors(&view->state.diff.colors);
2952 return err;
2954 err = add_color(&view->state.diff.colors,
2955 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2956 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2957 if (err) {
2958 free_colors(&view->state.diff.colors);
2959 return err;
2962 err = add_color(&view->state.diff.colors,
2963 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2964 get_color_value("TOG_COLOR_DIFF_META"));
2965 if (err) {
2966 free_colors(&view->state.diff.colors);
2967 return err;
2970 err = add_color(&view->state.diff.colors,
2971 "^(from|via): ", TOG_COLOR_AUTHOR,
2972 get_color_value("TOG_COLOR_AUTHOR"));
2973 if (err) {
2974 free_colors(&view->state.diff.colors);
2975 return err;
2978 err = add_color(&view->state.diff.colors,
2979 "^date: ", TOG_COLOR_DATE,
2980 get_color_value("TOG_COLOR_DATE"));
2981 if (err) {
2982 free_colors(&view->state.diff.colors);
2983 return err;
2987 if (log_view && view_is_splitscreen(view))
2988 show_log_view(log_view); /* draw vborder */
2989 diff_view_indicate_progress(view);
2991 err = create_diff(&view->state.diff);
2992 if (err) {
2993 free(view->state.diff.id1);
2994 view->state.diff.id1 = NULL;
2995 free(view->state.diff.id2);
2996 view->state.diff.id2 = NULL;
2997 return err;
3000 view->show = show_diff_view;
3001 view->input = input_diff_view;
3002 view->close = close_diff_view;
3004 return NULL;
3007 static const struct got_error *
3008 close_diff_view(struct tog_view *view)
3010 const struct got_error *err = NULL;
3012 free(view->state.diff.id1);
3013 view->state.diff.id1 = NULL;
3014 free(view->state.diff.id2);
3015 view->state.diff.id2 = NULL;
3016 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3017 err = got_error_from_errno("fclose");
3018 free_colors(&view->state.diff.colors);
3019 return err;
3022 static const struct got_error *
3023 show_diff_view(struct tog_view *view)
3025 const struct got_error *err;
3026 struct tog_diff_view_state *s = &view->state.diff;
3027 char *id_str1 = NULL, *id_str2, *header;
3029 if (s->id1) {
3030 err = got_object_id_str(&id_str1, s->id1);
3031 if (err)
3032 return err;
3034 err = got_object_id_str(&id_str2, s->id2);
3035 if (err)
3036 return err;
3038 if (asprintf(&header, "diff %s %s",
3039 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3040 err = got_error_from_errno("asprintf");
3041 free(id_str1);
3042 free(id_str2);
3043 return err;
3045 free(id_str1);
3046 free(id_str2);
3048 return draw_file(view, s->f, &s->first_displayed_line,
3049 &s->last_displayed_line, &s->eof, view->nlines,
3050 header, &s->colors);
3053 static const struct got_error *
3054 set_selected_commit(struct tog_diff_view_state *s,
3055 struct commit_queue_entry *entry)
3057 const struct got_error *err;
3058 const struct got_object_id_queue *parent_ids;
3059 struct got_commit_object *selected_commit;
3060 struct got_object_qid *pid;
3062 free(s->id2);
3063 s->id2 = got_object_id_dup(entry->id);
3064 if (s->id2 == NULL)
3065 return got_error_from_errno("got_object_id_dup");
3067 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3068 if (err)
3069 return err;
3070 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3071 free(s->id1);
3072 pid = SIMPLEQ_FIRST(parent_ids);
3073 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3074 got_object_commit_close(selected_commit);
3075 return NULL;
3078 static const struct got_error *
3079 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3080 struct tog_view **focus_view, struct tog_view *view, int ch)
3082 const struct got_error *err = NULL;
3083 struct tog_diff_view_state *s = &view->state.diff;
3084 struct tog_log_view_state *ls;
3085 struct commit_queue_entry *entry;
3086 int i;
3088 switch (ch) {
3089 case 'k':
3090 case KEY_UP:
3091 if (s->first_displayed_line > 1)
3092 s->first_displayed_line--;
3093 break;
3094 case KEY_PPAGE:
3095 case CTRL('b'):
3096 if (s->first_displayed_line == 1)
3097 break;
3098 i = 0;
3099 while (i++ < view->nlines - 1 &&
3100 s->first_displayed_line > 1)
3101 s->first_displayed_line--;
3102 break;
3103 case 'j':
3104 case KEY_DOWN:
3105 if (!s->eof)
3106 s->first_displayed_line++;
3107 break;
3108 case KEY_NPAGE:
3109 case CTRL('f'):
3110 case ' ':
3111 if (s->eof)
3112 break;
3113 i = 0;
3114 while (!s->eof && i++ < view->nlines - 1) {
3115 char *line;
3116 line = parse_next_line(s->f, NULL);
3117 s->first_displayed_line++;
3118 if (line == NULL)
3119 break;
3121 break;
3122 case '[':
3123 if (s->diff_context > 0) {
3124 s->diff_context--;
3125 diff_view_indicate_progress(view);
3126 err = create_diff(s);
3128 break;
3129 case ']':
3130 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3131 s->diff_context++;
3132 diff_view_indicate_progress(view);
3133 err = create_diff(s);
3135 break;
3136 case '<':
3137 case ',':
3138 if (s->log_view == NULL)
3139 break;
3140 ls = &s->log_view->state.log;
3141 entry = TAILQ_PREV(ls->selected_entry,
3142 commit_queue_head, entry);
3143 if (entry == NULL)
3144 break;
3146 err = input_log_view(NULL, NULL, NULL, s->log_view,
3147 KEY_UP);
3148 if (err)
3149 break;
3151 err = set_selected_commit(s, entry);
3152 if (err)
3153 break;
3155 s->first_displayed_line = 1;
3156 s->last_displayed_line = view->nlines;
3158 diff_view_indicate_progress(view);
3159 err = create_diff(s);
3160 break;
3161 case '>':
3162 case '.':
3163 if (s->log_view == NULL)
3164 break;
3165 ls = &s->log_view->state.log;
3167 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3168 ls->thread_args.commits_needed++;
3170 /* Display "loading..." in log view. */
3171 show_log_view(s->log_view);
3172 update_panels();
3173 doupdate();
3175 err = trigger_log_thread(1 /* load_all */,
3176 &ls->thread_args.commits_needed,
3177 &ls->thread_args.log_complete,
3178 &ls->thread_args.need_commits);
3179 if (err)
3180 break;
3182 err = input_log_view(NULL, NULL, NULL, s->log_view,
3183 KEY_DOWN);
3184 if (err)
3185 break;
3187 entry = TAILQ_NEXT(ls->selected_entry, entry);
3188 if (entry == NULL)
3189 break;
3191 err = set_selected_commit(s, entry);
3192 if (err)
3193 break;
3195 s->first_displayed_line = 1;
3196 s->last_displayed_line = view->nlines;
3198 diff_view_indicate_progress(view);
3199 err = create_diff(s);
3200 break;
3201 default:
3202 break;
3205 return err;
3208 static const struct got_error *
3209 cmd_diff(int argc, char *argv[])
3211 const struct got_error *error = NULL;
3212 struct got_repository *repo = NULL;
3213 struct got_reflist_head refs;
3214 struct got_object_id *id1 = NULL, *id2 = NULL;
3215 char *repo_path = NULL;
3216 char *id_str1 = NULL, *id_str2 = NULL;
3217 int ch;
3218 struct tog_view *view;
3220 SIMPLEQ_INIT(&refs);
3222 #ifndef PROFILE
3223 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3224 NULL) == -1)
3225 err(1, "pledge");
3226 #endif
3228 while ((ch = getopt(argc, argv, "")) != -1) {
3229 switch (ch) {
3230 default:
3231 usage_diff();
3232 /* NOTREACHED */
3236 argc -= optind;
3237 argv += optind;
3239 if (argc == 0) {
3240 usage_diff(); /* TODO show local worktree changes */
3241 } else if (argc == 2) {
3242 repo_path = getcwd(NULL, 0);
3243 if (repo_path == NULL)
3244 return got_error_from_errno("getcwd");
3245 id_str1 = argv[0];
3246 id_str2 = argv[1];
3247 } else if (argc == 3) {
3248 repo_path = realpath(argv[0], NULL);
3249 if (repo_path == NULL)
3250 return got_error_from_errno2("realpath", argv[0]);
3251 id_str1 = argv[1];
3252 id_str2 = argv[2];
3253 } else
3254 usage_diff();
3256 init_curses();
3258 error = got_repo_open(&repo, repo_path, NULL);
3259 if (error)
3260 goto done;
3262 error = apply_unveil(got_repo_get_path(repo), NULL);
3263 if (error)
3264 goto done;
3266 error = got_repo_match_object_id_prefix(&id1, id_str1,
3267 GOT_OBJ_TYPE_ANY, repo);
3268 if (error)
3269 goto done;
3271 error = got_repo_match_object_id_prefix(&id2, id_str2,
3272 GOT_OBJ_TYPE_ANY, repo);
3273 if (error)
3274 goto done;
3276 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3277 if (error)
3278 goto done;
3280 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3281 if (view == NULL) {
3282 error = got_error_from_errno("view_open");
3283 goto done;
3285 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3286 if (error)
3287 goto done;
3288 error = view_loop(view);
3289 done:
3290 free(repo_path);
3291 if (repo)
3292 got_repo_close(repo);
3293 got_ref_list_free(&refs);
3294 return error;
3297 __dead static void
3298 usage_blame(void)
3300 endwin();
3301 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3302 getprogname());
3303 exit(1);
3306 struct tog_blame_line {
3307 int annotated;
3308 struct got_object_id *id;
3311 static const struct got_error *
3312 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3313 const char *path, struct tog_blame_line *lines, int nlines,
3314 int blame_complete, int selected_line, int *first_displayed_line,
3315 int *last_displayed_line, int *eof, int max_lines,
3316 struct tog_colors *colors)
3318 const struct got_error *err;
3319 int lineno = 0, nprinted = 0;
3320 char *line;
3321 size_t len;
3322 wchar_t *wline;
3323 int width;
3324 struct tog_blame_line *blame_line;
3325 struct got_object_id *prev_id = NULL;
3326 char *id_str;
3327 struct tog_color *tc;
3329 err = got_object_id_str(&id_str, id);
3330 if (err)
3331 return err;
3333 rewind(f);
3334 werase(view->window);
3336 if (asprintf(&line, "commit %s", id_str) == -1) {
3337 err = got_error_from_errno("asprintf");
3338 free(id_str);
3339 return err;
3342 err = format_line(&wline, &width, line, view->ncols, 0);
3343 free(line);
3344 line = NULL;
3345 if (err)
3346 return err;
3347 if (view_needs_focus_indication(view))
3348 wstandout(view->window);
3349 tc = get_color(colors, TOG_COLOR_COMMIT);
3350 if (tc)
3351 wattr_on(view->window,
3352 COLOR_PAIR(tc->colorpair), NULL);
3353 waddwstr(view->window, wline);
3354 if (tc)
3355 wattr_off(view->window,
3356 COLOR_PAIR(tc->colorpair), NULL);
3357 if (view_needs_focus_indication(view))
3358 wstandend(view->window);
3359 free(wline);
3360 wline = NULL;
3361 if (width < view->ncols - 1)
3362 waddch(view->window, '\n');
3364 if (asprintf(&line, "[%d/%d] %s%s",
3365 *first_displayed_line - 1 + selected_line, nlines,
3366 blame_complete ? "" : "annotating... ", path) == -1) {
3367 free(id_str);
3368 return got_error_from_errno("asprintf");
3370 free(id_str);
3371 err = format_line(&wline, &width, line, view->ncols, 0);
3372 free(line);
3373 line = NULL;
3374 if (err)
3375 return err;
3376 waddwstr(view->window, wline);
3377 free(wline);
3378 wline = NULL;
3379 if (width < view->ncols - 1)
3380 waddch(view->window, '\n');
3382 *eof = 0;
3383 while (nprinted < max_lines - 2) {
3384 line = parse_next_line(f, &len);
3385 if (line == NULL) {
3386 *eof = 1;
3387 break;
3389 if (++lineno < *first_displayed_line) {
3390 free(line);
3391 continue;
3394 if (view->ncols <= 9) {
3395 width = 9;
3396 wline = wcsdup(L"");
3397 if (wline == NULL)
3398 err = got_error_from_errno("wcsdup");
3399 } else {
3400 err = format_line(&wline, &width, line,
3401 view->ncols - 9, 9);
3402 width += 9;
3404 if (err) {
3405 free(line);
3406 return err;
3409 if (view->focussed && nprinted == selected_line - 1)
3410 wstandout(view->window);
3412 if (nlines > 0) {
3413 blame_line = &lines[lineno - 1];
3414 if (blame_line->annotated && prev_id &&
3415 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3416 !(view->focussed &&
3417 nprinted == selected_line - 1)) {
3418 waddstr(view->window, " ");
3419 } else if (blame_line->annotated) {
3420 char *id_str;
3421 err = got_object_id_str(&id_str, blame_line->id);
3422 if (err) {
3423 free(line);
3424 free(wline);
3425 return err;
3427 tc = get_color(colors, TOG_COLOR_COMMIT);
3428 if (tc)
3429 wattr_on(view->window,
3430 COLOR_PAIR(tc->colorpair), NULL);
3431 wprintw(view->window, "%.8s", id_str);
3432 if (tc)
3433 wattr_off(view->window,
3434 COLOR_PAIR(tc->colorpair), NULL);
3435 free(id_str);
3436 prev_id = blame_line->id;
3437 } else {
3438 waddstr(view->window, "........");
3439 prev_id = NULL;
3441 } else {
3442 waddstr(view->window, "........");
3443 prev_id = NULL;
3446 if (view->focussed && nprinted == selected_line - 1)
3447 wstandend(view->window);
3448 waddstr(view->window, " ");
3450 waddwstr(view->window, wline);
3451 if (width <= view->ncols - 1)
3452 waddch(view->window, '\n');
3453 if (++nprinted == 1)
3454 *first_displayed_line = lineno;
3455 free(line);
3456 free(wline);
3457 wline = NULL;
3459 *last_displayed_line = lineno;
3461 view_vborder(view);
3463 return NULL;
3466 static const struct got_error *
3467 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3469 const struct got_error *err = NULL;
3470 struct tog_blame_cb_args *a = arg;
3471 struct tog_blame_line *line;
3472 int errcode;
3474 if (nlines != a->nlines ||
3475 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3476 return got_error(GOT_ERR_RANGE);
3478 errcode = pthread_mutex_lock(&tog_mutex);
3479 if (errcode)
3480 return got_error_set_errno(errcode, "pthread_mutex_lock");
3482 if (*a->quit) { /* user has quit the blame view */
3483 err = got_error(GOT_ERR_ITER_COMPLETED);
3484 goto done;
3487 if (lineno == -1)
3488 goto done; /* no change in this commit */
3490 line = &a->lines[lineno - 1];
3491 if (line->annotated)
3492 goto done;
3494 line->id = got_object_id_dup(id);
3495 if (line->id == NULL) {
3496 err = got_error_from_errno("got_object_id_dup");
3497 goto done;
3499 line->annotated = 1;
3500 done:
3501 errcode = pthread_mutex_unlock(&tog_mutex);
3502 if (errcode)
3503 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3504 return err;
3507 static void *
3508 blame_thread(void *arg)
3510 const struct got_error *err;
3511 struct tog_blame_thread_args *ta = arg;
3512 struct tog_blame_cb_args *a = ta->cb_args;
3513 int errcode;
3515 err = block_signals_used_by_main_thread();
3516 if (err)
3517 return (void *)err;
3519 err = got_blame(ta->path, a->commit_id, ta->repo,
3520 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3521 if (err && err->code == GOT_ERR_CANCELLED)
3522 err = NULL;
3524 errcode = pthread_mutex_lock(&tog_mutex);
3525 if (errcode)
3526 return (void *)got_error_set_errno(errcode,
3527 "pthread_mutex_lock");
3529 got_repo_close(ta->repo);
3530 ta->repo = NULL;
3531 *ta->complete = 1;
3533 errcode = pthread_mutex_unlock(&tog_mutex);
3534 if (errcode && err == NULL)
3535 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3537 return (void *)err;
3540 static struct got_object_id *
3541 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3542 int first_displayed_line, int selected_line)
3544 struct tog_blame_line *line;
3546 if (nlines <= 0)
3547 return NULL;
3549 line = &lines[first_displayed_line - 1 + selected_line - 1];
3550 if (!line->annotated)
3551 return NULL;
3553 return line->id;
3556 static const struct got_error *
3557 stop_blame(struct tog_blame *blame)
3559 const struct got_error *err = NULL;
3560 int i;
3562 if (blame->thread) {
3563 int errcode;
3564 errcode = pthread_mutex_unlock(&tog_mutex);
3565 if (errcode)
3566 return got_error_set_errno(errcode,
3567 "pthread_mutex_unlock");
3568 errcode = pthread_join(blame->thread, (void **)&err);
3569 if (errcode)
3570 return got_error_set_errno(errcode, "pthread_join");
3571 errcode = pthread_mutex_lock(&tog_mutex);
3572 if (errcode)
3573 return got_error_set_errno(errcode,
3574 "pthread_mutex_lock");
3575 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3576 err = NULL;
3577 blame->thread = NULL;
3579 if (blame->thread_args.repo) {
3580 got_repo_close(blame->thread_args.repo);
3581 blame->thread_args.repo = NULL;
3583 if (blame->f) {
3584 if (fclose(blame->f) != 0 && err == NULL)
3585 err = got_error_from_errno("fclose");
3586 blame->f = NULL;
3588 if (blame->lines) {
3589 for (i = 0; i < blame->nlines; i++)
3590 free(blame->lines[i].id);
3591 free(blame->lines);
3592 blame->lines = NULL;
3594 free(blame->cb_args.commit_id);
3595 blame->cb_args.commit_id = NULL;
3597 return err;
3600 static const struct got_error *
3601 cancel_blame_view(void *arg)
3603 const struct got_error *err = NULL;
3604 int *done = arg;
3605 int errcode;
3607 errcode = pthread_mutex_lock(&tog_mutex);
3608 if (errcode)
3609 return got_error_set_errno(errcode,
3610 "pthread_mutex_unlock");
3612 if (*done)
3613 err = got_error(GOT_ERR_CANCELLED);
3615 errcode = pthread_mutex_unlock(&tog_mutex);
3616 if (errcode)
3617 return got_error_set_errno(errcode,
3618 "pthread_mutex_lock");
3620 return err;
3623 static const struct got_error *
3624 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3625 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3626 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3627 struct got_repository *repo)
3629 const struct got_error *err = NULL;
3630 struct got_blob_object *blob = NULL;
3631 struct got_repository *thread_repo = NULL;
3632 struct got_object_id *obj_id = NULL;
3633 int obj_type;
3635 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3636 if (err)
3637 return err;
3639 err = got_object_get_type(&obj_type, repo, obj_id);
3640 if (err)
3641 goto done;
3643 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3644 err = got_error(GOT_ERR_OBJ_TYPE);
3645 goto done;
3648 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3649 if (err)
3650 goto done;
3651 blame->f = got_opentemp();
3652 if (blame->f == NULL) {
3653 err = got_error_from_errno("got_opentemp");
3654 goto done;
3656 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3657 &blame->line_offsets, blame->f, blob);
3658 if (err || blame->nlines == 0)
3659 goto done;
3661 /* Don't include \n at EOF in the blame line count. */
3662 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3663 blame->nlines--;
3665 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3666 if (blame->lines == NULL) {
3667 err = got_error_from_errno("calloc");
3668 goto done;
3671 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3672 if (err)
3673 goto done;
3675 blame->cb_args.view = view;
3676 blame->cb_args.lines = blame->lines;
3677 blame->cb_args.nlines = blame->nlines;
3678 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3679 if (blame->cb_args.commit_id == NULL) {
3680 err = got_error_from_errno("got_object_id_dup");
3681 goto done;
3683 blame->cb_args.quit = done;
3685 blame->thread_args.path = path;
3686 blame->thread_args.repo = thread_repo;
3687 blame->thread_args.cb_args = &blame->cb_args;
3688 blame->thread_args.complete = blame_complete;
3689 blame->thread_args.cancel_cb = cancel_blame_view;
3690 blame->thread_args.cancel_arg = done;
3691 *blame_complete = 0;
3693 done:
3694 if (blob)
3695 got_object_blob_close(blob);
3696 free(obj_id);
3697 if (err)
3698 stop_blame(blame);
3699 return err;
3702 static const struct got_error *
3703 open_blame_view(struct tog_view *view, char *path,
3704 struct got_object_id *commit_id, struct got_reflist_head *refs,
3705 struct got_repository *repo)
3707 const struct got_error *err = NULL;
3708 struct tog_blame_view_state *s = &view->state.blame;
3710 SIMPLEQ_INIT(&s->blamed_commits);
3712 s->path = strdup(path);
3713 if (s->path == NULL)
3714 return got_error_from_errno("strdup");
3716 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3717 if (err) {
3718 free(s->path);
3719 return err;
3722 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3723 s->first_displayed_line = 1;
3724 s->last_displayed_line = view->nlines;
3725 s->selected_line = 1;
3726 s->blame_complete = 0;
3727 s->repo = repo;
3728 s->refs = refs;
3729 s->commit_id = commit_id;
3730 memset(&s->blame, 0, sizeof(s->blame));
3732 SIMPLEQ_INIT(&s->colors);
3733 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3734 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3735 get_color_value("TOG_COLOR_COMMIT"));
3736 if (err)
3737 return err;
3740 view->show = show_blame_view;
3741 view->input = input_blame_view;
3742 view->close = close_blame_view;
3743 view->search_start = search_start_blame_view;
3744 view->search_next = search_next_blame_view;
3746 return run_blame(&s->blame, view, &s->blame_complete,
3747 &s->first_displayed_line, &s->last_displayed_line,
3748 &s->selected_line, &s->done, &s->eof, s->path,
3749 s->blamed_commit->id, s->repo);
3752 static const struct got_error *
3753 close_blame_view(struct tog_view *view)
3755 const struct got_error *err = NULL;
3756 struct tog_blame_view_state *s = &view->state.blame;
3758 if (s->blame.thread)
3759 err = stop_blame(&s->blame);
3761 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3762 struct got_object_qid *blamed_commit;
3763 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3764 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3765 got_object_qid_free(blamed_commit);
3768 free(s->path);
3769 free_colors(&s->colors);
3771 return err;
3774 static const struct got_error *
3775 search_start_blame_view(struct tog_view *view)
3777 struct tog_blame_view_state *s = &view->state.blame;
3779 s->matched_line = 0;
3780 return NULL;
3783 static const struct got_error *
3784 search_next_blame_view(struct tog_view *view)
3786 struct tog_blame_view_state *s = &view->state.blame;
3787 int lineno;
3789 if (!view->searching) {
3790 view->search_next_done = 1;
3791 return NULL;
3794 if (s->matched_line) {
3795 if (view->searching == TOG_SEARCH_FORWARD)
3796 lineno = s->matched_line + 1;
3797 else
3798 lineno = s->matched_line - 1;
3799 } else {
3800 if (view->searching == TOG_SEARCH_FORWARD)
3801 lineno = 1;
3802 else
3803 lineno = s->blame.nlines;
3806 while (1) {
3807 char *line = NULL;
3808 off_t offset;
3809 size_t len;
3811 if (lineno <= 0 || lineno > s->blame.nlines) {
3812 if (s->matched_line == 0) {
3813 view->search_next_done = 1;
3814 free(line);
3815 break;
3818 if (view->searching == TOG_SEARCH_FORWARD)
3819 lineno = 1;
3820 else
3821 lineno = s->blame.nlines;
3824 offset = s->blame.line_offsets[lineno - 1];
3825 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3826 free(line);
3827 return got_error_from_errno("fseeko");
3829 free(line);
3830 line = parse_next_line(s->blame.f, &len);
3831 if (line && match_line(line, &view->regex)) {
3832 view->search_next_done = 1;
3833 s->matched_line = lineno;
3834 free(line);
3835 break;
3837 free(line);
3838 if (view->searching == TOG_SEARCH_FORWARD)
3839 lineno++;
3840 else
3841 lineno--;
3844 if (s->matched_line) {
3845 s->first_displayed_line = s->matched_line;
3846 s->selected_line = 1;
3849 return NULL;
3852 static const struct got_error *
3853 show_blame_view(struct tog_view *view)
3855 const struct got_error *err = NULL;
3856 struct tog_blame_view_state *s = &view->state.blame;
3857 int errcode;
3859 if (s->blame.thread == NULL) {
3860 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3861 &s->blame.thread_args);
3862 if (errcode)
3863 return got_error_set_errno(errcode, "pthread_create");
3865 halfdelay(1); /* fast refresh while annotating */
3868 if (s->blame_complete)
3869 halfdelay(10); /* disable fast refresh */
3871 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3872 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3873 s->selected_line, &s->first_displayed_line,
3874 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3876 view_vborder(view);
3877 return err;
3880 static const struct got_error *
3881 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3882 struct tog_view **focus_view, struct tog_view *view, int ch)
3884 const struct got_error *err = NULL, *thread_err = NULL;
3885 struct tog_view *diff_view;
3886 struct tog_blame_view_state *s = &view->state.blame;
3887 int begin_x = 0;
3889 switch (ch) {
3890 case 'q':
3891 s->done = 1;
3892 break;
3893 case 'k':
3894 case KEY_UP:
3895 if (s->selected_line > 1)
3896 s->selected_line--;
3897 else if (s->selected_line == 1 &&
3898 s->first_displayed_line > 1)
3899 s->first_displayed_line--;
3900 break;
3901 case KEY_PPAGE:
3902 if (s->first_displayed_line == 1) {
3903 s->selected_line = 1;
3904 break;
3906 if (s->first_displayed_line > view->nlines - 2)
3907 s->first_displayed_line -=
3908 (view->nlines - 2);
3909 else
3910 s->first_displayed_line = 1;
3911 break;
3912 case 'j':
3913 case KEY_DOWN:
3914 if (s->selected_line < view->nlines - 2 &&
3915 s->first_displayed_line +
3916 s->selected_line <= s->blame.nlines)
3917 s->selected_line++;
3918 else if (s->last_displayed_line <
3919 s->blame.nlines)
3920 s->first_displayed_line++;
3921 break;
3922 case 'b':
3923 case 'p': {
3924 struct got_object_id *id = NULL;
3925 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3926 s->first_displayed_line, s->selected_line);
3927 if (id == NULL)
3928 break;
3929 if (ch == 'p') {
3930 struct got_commit_object *commit;
3931 struct got_object_qid *pid;
3932 struct got_object_id *blob_id = NULL;
3933 int obj_type;
3934 err = got_object_open_as_commit(&commit,
3935 s->repo, id);
3936 if (err)
3937 break;
3938 pid = SIMPLEQ_FIRST(
3939 got_object_commit_get_parent_ids(commit));
3940 if (pid == NULL) {
3941 got_object_commit_close(commit);
3942 break;
3944 /* Check if path history ends here. */
3945 err = got_object_id_by_path(&blob_id, s->repo,
3946 pid->id, s->path);
3947 if (err) {
3948 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3949 err = NULL;
3950 got_object_commit_close(commit);
3951 break;
3953 err = got_object_get_type(&obj_type, s->repo,
3954 blob_id);
3955 free(blob_id);
3956 /* Can't blame non-blob type objects. */
3957 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3958 got_object_commit_close(commit);
3959 break;
3961 err = got_object_qid_alloc(&s->blamed_commit,
3962 pid->id);
3963 got_object_commit_close(commit);
3964 } else {
3965 if (got_object_id_cmp(id,
3966 s->blamed_commit->id) == 0)
3967 break;
3968 err = got_object_qid_alloc(&s->blamed_commit,
3969 id);
3971 if (err)
3972 break;
3973 s->done = 1;
3974 thread_err = stop_blame(&s->blame);
3975 s->done = 0;
3976 if (thread_err)
3977 break;
3978 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3979 s->blamed_commit, entry);
3980 err = run_blame(&s->blame, view, &s->blame_complete,
3981 &s->first_displayed_line, &s->last_displayed_line,
3982 &s->selected_line, &s->done, &s->eof,
3983 s->path, s->blamed_commit->id, s->repo);
3984 if (err)
3985 break;
3986 break;
3988 case 'B': {
3989 struct got_object_qid *first;
3990 first = SIMPLEQ_FIRST(&s->blamed_commits);
3991 if (!got_object_id_cmp(first->id, s->commit_id))
3992 break;
3993 s->done = 1;
3994 thread_err = stop_blame(&s->blame);
3995 s->done = 0;
3996 if (thread_err)
3997 break;
3998 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3999 got_object_qid_free(s->blamed_commit);
4000 s->blamed_commit =
4001 SIMPLEQ_FIRST(&s->blamed_commits);
4002 err = run_blame(&s->blame, view, &s->blame_complete,
4003 &s->first_displayed_line, &s->last_displayed_line,
4004 &s->selected_line, &s->done, &s->eof, s->path,
4005 s->blamed_commit->id, s->repo);
4006 if (err)
4007 break;
4008 break;
4010 case KEY_ENTER:
4011 case '\r': {
4012 struct got_object_id *id = NULL;
4013 struct got_object_qid *pid;
4014 struct got_commit_object *commit = NULL;
4015 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4016 s->first_displayed_line, s->selected_line);
4017 if (id == NULL)
4018 break;
4019 err = got_object_open_as_commit(&commit, s->repo, id);
4020 if (err)
4021 break;
4022 pid = SIMPLEQ_FIRST(
4023 got_object_commit_get_parent_ids(commit));
4024 if (view_is_parent_view(view))
4025 begin_x = view_split_begin_x(view->begin_x);
4026 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4027 if (diff_view == NULL) {
4028 got_object_commit_close(commit);
4029 err = got_error_from_errno("view_open");
4030 break;
4032 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4033 id, NULL, s->refs, s->repo);
4034 got_object_commit_close(commit);
4035 if (err) {
4036 view_close(diff_view);
4037 break;
4039 if (view_is_parent_view(view)) {
4040 err = view_close_child(view);
4041 if (err)
4042 break;
4043 err = view_set_child(view, diff_view);
4044 if (err) {
4045 view_close(diff_view);
4046 break;
4048 *focus_view = diff_view;
4049 view->child_focussed = 1;
4050 } else
4051 *new_view = diff_view;
4052 if (err)
4053 break;
4054 break;
4056 case KEY_NPAGE:
4057 case ' ':
4058 if (s->last_displayed_line >= s->blame.nlines &&
4059 s->selected_line >= MIN(s->blame.nlines,
4060 view->nlines - 2)) {
4061 break;
4063 if (s->last_displayed_line >= s->blame.nlines &&
4064 s->selected_line < view->nlines - 2) {
4065 s->selected_line = MIN(s->blame.nlines,
4066 view->nlines - 2);
4067 break;
4069 if (s->last_displayed_line + view->nlines - 2
4070 <= s->blame.nlines)
4071 s->first_displayed_line +=
4072 view->nlines - 2;
4073 else
4074 s->first_displayed_line =
4075 s->blame.nlines -
4076 (view->nlines - 3);
4077 break;
4078 case KEY_RESIZE:
4079 if (s->selected_line > view->nlines - 2) {
4080 s->selected_line = MIN(s->blame.nlines,
4081 view->nlines - 2);
4083 break;
4084 default:
4085 break;
4087 return thread_err ? thread_err : err;
4090 static const struct got_error *
4091 cmd_blame(int argc, char *argv[])
4093 const struct got_error *error;
4094 struct got_repository *repo = NULL;
4095 struct got_reflist_head refs;
4096 struct got_worktree *worktree = NULL;
4097 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4098 struct got_object_id *commit_id = NULL;
4099 char *commit_id_str = NULL;
4100 int ch;
4101 struct tog_view *view;
4103 SIMPLEQ_INIT(&refs);
4105 #ifndef PROFILE
4106 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4107 NULL) == -1)
4108 err(1, "pledge");
4109 #endif
4111 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4112 switch (ch) {
4113 case 'c':
4114 commit_id_str = optarg;
4115 break;
4116 case 'r':
4117 repo_path = realpath(optarg, NULL);
4118 if (repo_path == NULL)
4119 return got_error_from_errno2("realpath",
4120 optarg);
4121 break;
4122 default:
4123 usage_blame();
4124 /* NOTREACHED */
4128 argc -= optind;
4129 argv += optind;
4131 if (argc == 1)
4132 path = argv[0];
4133 else
4134 usage_blame();
4136 cwd = getcwd(NULL, 0);
4137 if (cwd == NULL) {
4138 error = got_error_from_errno("getcwd");
4139 goto done;
4141 if (repo_path == NULL) {
4142 error = got_worktree_open(&worktree, cwd);
4143 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4144 goto done;
4145 else
4146 error = NULL;
4147 if (worktree) {
4148 repo_path =
4149 strdup(got_worktree_get_repo_path(worktree));
4150 if (repo_path == NULL)
4151 error = got_error_from_errno("strdup");
4152 if (error)
4153 goto done;
4154 } else {
4155 repo_path = strdup(cwd);
4156 if (repo_path == NULL) {
4157 error = got_error_from_errno("strdup");
4158 goto done;
4163 init_curses();
4165 error = got_repo_open(&repo, repo_path, NULL);
4166 if (error != NULL)
4167 goto done;
4169 error = apply_unveil(got_repo_get_path(repo), NULL);
4170 if (error)
4171 goto done;
4173 if (worktree) {
4174 const char *prefix = got_worktree_get_path_prefix(worktree);
4175 char *p, *worktree_subdir = cwd +
4176 strlen(got_worktree_get_root_path(worktree));
4177 if (asprintf(&p, "%s%s%s%s%s",
4178 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4179 worktree_subdir, worktree_subdir[0] ? "/" : "",
4180 path) == -1) {
4181 error = got_error_from_errno("asprintf");
4182 goto done;
4184 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4185 free(p);
4186 } else {
4187 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4189 if (error)
4190 goto done;
4192 if (commit_id_str == NULL) {
4193 struct got_reference *head_ref;
4194 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4195 if (error != NULL)
4196 goto done;
4197 error = got_ref_resolve(&commit_id, repo, head_ref);
4198 got_ref_close(head_ref);
4199 } else {
4200 error = got_repo_match_object_id(&commit_id, NULL,
4201 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4203 if (error != NULL)
4204 goto done;
4206 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4207 if (error)
4208 goto done;
4210 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4211 if (view == NULL) {
4212 error = got_error_from_errno("view_open");
4213 goto done;
4215 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4216 if (error)
4217 goto done;
4218 if (worktree) {
4219 /* Release work tree lock. */
4220 got_worktree_close(worktree);
4221 worktree = NULL;
4223 error = view_loop(view);
4224 done:
4225 free(repo_path);
4226 free(cwd);
4227 free(commit_id);
4228 if (worktree)
4229 got_worktree_close(worktree);
4230 if (repo)
4231 got_repo_close(repo);
4232 got_ref_list_free(&refs);
4233 return error;
4236 static const struct got_error *
4237 draw_tree_entries(struct tog_view *view,
4238 struct got_tree_entry **first_displayed_entry,
4239 struct got_tree_entry **last_displayed_entry,
4240 struct got_tree_entry **selected_entry, int *ndisplayed,
4241 const char *label, int show_ids, const char *parent_path,
4242 struct got_tree_object *tree, int selected, int limit,
4243 int isroot, struct tog_colors *colors)
4245 const struct got_error *err = NULL;
4246 struct got_tree_entry *te;
4247 wchar_t *wline;
4248 struct tog_color *tc;
4249 int width, n, i, nentries;
4251 *ndisplayed = 0;
4253 werase(view->window);
4255 if (limit == 0)
4256 return NULL;
4258 err = format_line(&wline, &width, label, view->ncols, 0);
4259 if (err)
4260 return err;
4261 if (view_needs_focus_indication(view))
4262 wstandout(view->window);
4263 tc = get_color(colors, TOG_COLOR_COMMIT);
4264 if (tc)
4265 wattr_on(view->window,
4266 COLOR_PAIR(tc->colorpair), NULL);
4267 waddwstr(view->window, wline);
4268 if (tc)
4269 wattr_off(view->window,
4270 COLOR_PAIR(tc->colorpair), NULL);
4271 if (view_needs_focus_indication(view))
4272 wstandend(view->window);
4273 free(wline);
4274 wline = NULL;
4275 if (width < view->ncols - 1)
4276 waddch(view->window, '\n');
4277 if (--limit <= 0)
4278 return NULL;
4279 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4280 if (err)
4281 return err;
4282 waddwstr(view->window, wline);
4283 free(wline);
4284 wline = NULL;
4285 if (width < view->ncols - 1)
4286 waddch(view->window, '\n');
4287 if (--limit <= 0)
4288 return NULL;
4289 waddch(view->window, '\n');
4290 if (--limit <= 0)
4291 return NULL;
4293 if (*first_displayed_entry == NULL) {
4294 te = got_object_tree_get_first_entry(tree);
4295 if (selected == 0) {
4296 if (view->focussed)
4297 wstandout(view->window);
4298 *selected_entry = NULL;
4300 waddstr(view->window, " ..\n"); /* parent directory */
4301 if (selected == 0 && view->focussed)
4302 wstandend(view->window);
4303 (*ndisplayed)++;
4304 if (--limit <= 0)
4305 return NULL;
4306 n = 1;
4307 } else {
4308 n = 0;
4309 te = *first_displayed_entry;
4312 nentries = got_object_tree_get_nentries(tree);
4313 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4314 char *line = NULL, *id_str = NULL;
4315 const char *modestr = "";
4316 mode_t mode;
4318 te = got_object_tree_get_entry(tree, i);
4319 mode = got_tree_entry_get_mode(te);
4321 if (show_ids) {
4322 err = got_object_id_str(&id_str,
4323 got_tree_entry_get_id(te));
4324 if (err)
4325 return got_error_from_errno(
4326 "got_object_id_str");
4328 if (got_object_tree_entry_is_submodule(te))
4329 modestr = "$";
4330 else if (S_ISLNK(mode))
4331 modestr = "@";
4332 else if (S_ISDIR(mode))
4333 modestr = "/";
4334 else if (mode & S_IXUSR)
4335 modestr = "*";
4336 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4337 got_tree_entry_get_name(te), modestr) == -1) {
4338 free(id_str);
4339 return got_error_from_errno("asprintf");
4341 free(id_str);
4342 err = format_line(&wline, &width, line, view->ncols, 0);
4343 if (err) {
4344 free(line);
4345 break;
4347 if (n == selected) {
4348 if (view->focussed)
4349 wstandout(view->window);
4350 *selected_entry = te;
4352 tc = match_color(colors, line);
4353 if (tc)
4354 wattr_on(view->window,
4355 COLOR_PAIR(tc->colorpair), NULL);
4356 waddwstr(view->window, wline);
4357 if (tc)
4358 wattr_off(view->window,
4359 COLOR_PAIR(tc->colorpair), NULL);
4360 if (width < view->ncols - 1)
4361 waddch(view->window, '\n');
4362 if (n == selected && view->focussed)
4363 wstandend(view->window);
4364 free(line);
4365 free(wline);
4366 wline = NULL;
4367 n++;
4368 (*ndisplayed)++;
4369 *last_displayed_entry = te;
4370 if (--limit <= 0)
4371 break;
4374 return err;
4377 static void
4378 tree_scroll_up(struct tog_view *view,
4379 struct got_tree_entry **first_displayed_entry, int maxscroll,
4380 struct got_tree_object *tree, int isroot)
4382 struct got_tree_entry *te;
4383 int i;
4385 if (*first_displayed_entry == NULL)
4386 return;
4388 te = got_object_tree_get_entry(tree, 0);
4389 if (*first_displayed_entry == te) {
4390 if (!isroot)
4391 *first_displayed_entry = NULL;
4392 return;
4395 i = 0;
4396 while (*first_displayed_entry && i < maxscroll) {
4397 *first_displayed_entry = got_tree_entry_get_prev(tree,
4398 *first_displayed_entry);
4399 i++;
4401 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4402 *first_displayed_entry = NULL;
4405 static int
4406 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4407 struct got_tree_entry *last_displayed_entry,
4408 struct got_tree_object *tree)
4410 struct got_tree_entry *next, *last;
4411 int n = 0;
4413 if (*first_displayed_entry)
4414 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4415 else
4416 next = got_object_tree_get_first_entry(tree);
4418 last = last_displayed_entry;
4419 while (next && last && n++ < maxscroll) {
4420 last = got_tree_entry_get_next(tree, last);
4421 if (last) {
4422 *first_displayed_entry = next;
4423 next = got_tree_entry_get_next(tree, next);
4426 return n;
4429 static const struct got_error *
4430 tree_entry_path(char **path, struct tog_parent_trees *parents,
4431 struct got_tree_entry *te)
4433 const struct got_error *err = NULL;
4434 struct tog_parent_tree *pt;
4435 size_t len = 2; /* for leading slash and NUL */
4437 TAILQ_FOREACH(pt, parents, entry)
4438 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4439 + 1 /* slash */;
4440 if (te)
4441 len += strlen(got_tree_entry_get_name(te));
4443 *path = calloc(1, len);
4444 if (path == NULL)
4445 return got_error_from_errno("calloc");
4447 (*path)[0] = '/';
4448 pt = TAILQ_LAST(parents, tog_parent_trees);
4449 while (pt) {
4450 const char *name = got_tree_entry_get_name(pt->selected_entry);
4451 if (strlcat(*path, name, len) >= len) {
4452 err = got_error(GOT_ERR_NO_SPACE);
4453 goto done;
4455 if (strlcat(*path, "/", len) >= len) {
4456 err = got_error(GOT_ERR_NO_SPACE);
4457 goto done;
4459 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4461 if (te) {
4462 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4463 err = got_error(GOT_ERR_NO_SPACE);
4464 goto done;
4467 done:
4468 if (err) {
4469 free(*path);
4470 *path = NULL;
4472 return err;
4475 static const struct got_error *
4476 blame_tree_entry(struct tog_view **new_view, int begin_x,
4477 struct got_tree_entry *te, struct tog_parent_trees *parents,
4478 struct got_object_id *commit_id, struct got_reflist_head *refs,
4479 struct got_repository *repo)
4481 const struct got_error *err = NULL;
4482 char *path;
4483 struct tog_view *blame_view;
4485 *new_view = NULL;
4487 err = tree_entry_path(&path, parents, te);
4488 if (err)
4489 return err;
4491 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4492 if (blame_view == NULL) {
4493 err = got_error_from_errno("view_open");
4494 goto done;
4497 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4498 if (err) {
4499 if (err->code == GOT_ERR_CANCELLED)
4500 err = NULL;
4501 view_close(blame_view);
4502 } else
4503 *new_view = blame_view;
4504 done:
4505 free(path);
4506 return err;
4509 static const struct got_error *
4510 log_tree_entry(struct tog_view **new_view, int begin_x,
4511 struct got_tree_entry *te, struct tog_parent_trees *parents,
4512 struct got_object_id *commit_id, struct got_reflist_head *refs,
4513 struct got_repository *repo)
4515 struct tog_view *log_view;
4516 const struct got_error *err = NULL;
4517 char *path;
4519 *new_view = NULL;
4521 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4522 if (log_view == NULL)
4523 return got_error_from_errno("view_open");
4525 err = tree_entry_path(&path, parents, te);
4526 if (err)
4527 return err;
4529 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4530 if (err)
4531 view_close(log_view);
4532 else
4533 *new_view = log_view;
4534 free(path);
4535 return err;
4538 static const struct got_error *
4539 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4540 struct got_object_id *commit_id, struct got_reflist_head *refs,
4541 struct got_repository *repo)
4543 const struct got_error *err = NULL;
4544 char *commit_id_str = NULL;
4545 struct tog_tree_view_state *s = &view->state.tree;
4547 TAILQ_INIT(&s->parents);
4549 err = got_object_id_str(&commit_id_str, commit_id);
4550 if (err != NULL)
4551 goto done;
4553 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4554 err = got_error_from_errno("asprintf");
4555 goto done;
4558 s->root = s->tree = root;
4559 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4560 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4561 s->commit_id = got_object_id_dup(commit_id);
4562 if (s->commit_id == NULL) {
4563 err = got_error_from_errno("got_object_id_dup");
4564 goto done;
4566 s->refs = refs;
4567 s->repo = repo;
4569 SIMPLEQ_INIT(&s->colors);
4571 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4572 err = add_color(&s->colors, "\\$$",
4573 TOG_COLOR_TREE_SUBMODULE,
4574 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4575 if (err)
4576 goto done;
4577 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4578 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4579 if (err) {
4580 free_colors(&s->colors);
4581 goto done;
4583 err = add_color(&s->colors, "/$",
4584 TOG_COLOR_TREE_DIRECTORY,
4585 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4586 if (err) {
4587 free_colors(&s->colors);
4588 goto done;
4591 err = add_color(&s->colors, "\\*$",
4592 TOG_COLOR_TREE_EXECUTABLE,
4593 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4594 if (err) {
4595 free_colors(&s->colors);
4596 goto done;
4599 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4600 get_color_value("TOG_COLOR_COMMIT"));
4601 if (err) {
4602 free_colors(&s->colors);
4603 goto done;
4607 view->show = show_tree_view;
4608 view->input = input_tree_view;
4609 view->close = close_tree_view;
4610 view->search_start = search_start_tree_view;
4611 view->search_next = search_next_tree_view;
4612 done:
4613 free(commit_id_str);
4614 if (err) {
4615 free(s->tree_label);
4616 s->tree_label = NULL;
4618 return err;
4621 static const struct got_error *
4622 close_tree_view(struct tog_view *view)
4624 struct tog_tree_view_state *s = &view->state.tree;
4626 free_colors(&s->colors);
4627 free(s->tree_label);
4628 s->tree_label = NULL;
4629 free(s->commit_id);
4630 s->commit_id = NULL;
4631 while (!TAILQ_EMPTY(&s->parents)) {
4632 struct tog_parent_tree *parent;
4633 parent = TAILQ_FIRST(&s->parents);
4634 TAILQ_REMOVE(&s->parents, parent, entry);
4635 free(parent);
4638 if (s->tree != s->root)
4639 got_object_tree_close(s->tree);
4640 got_object_tree_close(s->root);
4642 return NULL;
4645 static const struct got_error *
4646 search_start_tree_view(struct tog_view *view)
4648 struct tog_tree_view_state *s = &view->state.tree;
4650 s->matched_entry = NULL;
4651 return NULL;
4654 static int
4655 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4657 regmatch_t regmatch;
4659 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4660 0) == 0;
4663 static const struct got_error *
4664 search_next_tree_view(struct tog_view *view)
4666 struct tog_tree_view_state *s = &view->state.tree;
4667 struct got_tree_entry *te = NULL;
4669 if (!view->searching) {
4670 view->search_next_done = 1;
4671 return NULL;
4674 if (s->matched_entry) {
4675 if (view->searching == TOG_SEARCH_FORWARD) {
4676 if (s->selected_entry)
4677 te = got_tree_entry_get_next(s->tree,
4678 s->selected_entry);
4679 else
4680 te = got_object_tree_get_first_entry(s->tree);
4681 } else {
4682 if (s->selected_entry == NULL)
4683 te = got_object_tree_get_last_entry(s->tree);
4684 else
4685 te = got_tree_entry_get_prev(s->tree,
4686 s->selected_entry);
4688 } else {
4689 if (view->searching == TOG_SEARCH_FORWARD)
4690 te = got_object_tree_get_first_entry(s->tree);
4691 else
4692 te = got_object_tree_get_last_entry(s->tree);
4695 while (1) {
4696 if (te == NULL) {
4697 if (s->matched_entry == NULL) {
4698 view->search_next_done = 1;
4699 return NULL;
4701 if (view->searching == TOG_SEARCH_FORWARD)
4702 te = got_object_tree_get_first_entry(s->tree);
4703 else
4704 te = got_object_tree_get_last_entry(s->tree);
4707 if (match_tree_entry(te, &view->regex)) {
4708 view->search_next_done = 1;
4709 s->matched_entry = te;
4710 break;
4713 if (view->searching == TOG_SEARCH_FORWARD)
4714 te = got_tree_entry_get_next(s->tree, te);
4715 else
4716 te = got_tree_entry_get_prev(s->tree, te);
4719 if (s->matched_entry) {
4720 s->first_displayed_entry = s->matched_entry;
4721 s->selected = 0;
4724 return NULL;
4727 static const struct got_error *
4728 show_tree_view(struct tog_view *view)
4730 const struct got_error *err = NULL;
4731 struct tog_tree_view_state *s = &view->state.tree;
4732 char *parent_path;
4734 err = tree_entry_path(&parent_path, &s->parents, NULL);
4735 if (err)
4736 return err;
4738 err = draw_tree_entries(view, &s->first_displayed_entry,
4739 &s->last_displayed_entry, &s->selected_entry,
4740 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4741 s->tree, s->selected, view->nlines, s->tree == s->root,
4742 &s->colors);
4743 free(parent_path);
4745 view_vborder(view);
4746 return err;
4749 static const struct got_error *
4750 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4751 struct tog_view **focus_view, struct tog_view *view, int ch)
4753 const struct got_error *err = NULL;
4754 struct tog_tree_view_state *s = &view->state.tree;
4755 struct tog_view *log_view;
4756 int begin_x = 0, nscrolled;
4758 switch (ch) {
4759 case 'i':
4760 s->show_ids = !s->show_ids;
4761 break;
4762 case 'l':
4763 if (!s->selected_entry)
4764 break;
4765 if (view_is_parent_view(view))
4766 begin_x = view_split_begin_x(view->begin_x);
4767 err = log_tree_entry(&log_view, begin_x,
4768 s->selected_entry, &s->parents,
4769 s->commit_id, s->refs, s->repo);
4770 if (view_is_parent_view(view)) {
4771 err = view_close_child(view);
4772 if (err)
4773 return err;
4774 err = view_set_child(view, log_view);
4775 if (err) {
4776 view_close(log_view);
4777 break;
4779 *focus_view = log_view;
4780 view->child_focussed = 1;
4781 } else
4782 *new_view = log_view;
4783 break;
4784 case 'k':
4785 case KEY_UP:
4786 if (s->selected > 0) {
4787 s->selected--;
4788 if (s->selected == 0)
4789 break;
4791 if (s->selected > 0)
4792 break;
4793 tree_scroll_up(view, &s->first_displayed_entry, 1,
4794 s->tree, s->tree == s->root);
4795 break;
4796 case KEY_PPAGE:
4797 tree_scroll_up(view, &s->first_displayed_entry,
4798 MAX(0, view->nlines - 4 - s->selected), s->tree,
4799 s->tree == s->root);
4800 s->selected = 0;
4801 if (got_object_tree_get_first_entry(s->tree) ==
4802 s->first_displayed_entry && s->tree != s->root)
4803 s->first_displayed_entry = NULL;
4804 break;
4805 case 'j':
4806 case KEY_DOWN:
4807 if (s->selected < s->ndisplayed - 1) {
4808 s->selected++;
4809 break;
4811 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4812 == NULL)
4813 /* can't scroll any further */
4814 break;
4815 tree_scroll_down(&s->first_displayed_entry, 1,
4816 s->last_displayed_entry, s->tree);
4817 break;
4818 case KEY_NPAGE:
4819 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4820 == NULL) {
4821 /* can't scroll any further; move cursor down */
4822 if (s->selected < s->ndisplayed - 1)
4823 s->selected = s->ndisplayed - 1;
4824 break;
4826 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4827 view->nlines, s->last_displayed_entry, s->tree);
4828 if (nscrolled < view->nlines) {
4829 int ndisplayed = 0;
4830 struct got_tree_entry *te;
4831 te = s->first_displayed_entry;
4832 do {
4833 ndisplayed++;
4834 te = got_tree_entry_get_next(s->tree, te);
4835 } while (te);
4836 s->selected = ndisplayed - 1;
4838 break;
4839 case KEY_ENTER:
4840 case '\r':
4841 case KEY_BACKSPACE:
4842 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4843 struct tog_parent_tree *parent;
4844 /* user selected '..' */
4845 if (s->tree == s->root)
4846 break;
4847 parent = TAILQ_FIRST(&s->parents);
4848 TAILQ_REMOVE(&s->parents, parent,
4849 entry);
4850 got_object_tree_close(s->tree);
4851 s->tree = parent->tree;
4852 s->first_displayed_entry =
4853 parent->first_displayed_entry;
4854 s->selected_entry =
4855 parent->selected_entry;
4856 s->selected = parent->selected;
4857 free(parent);
4858 } else if (S_ISDIR(got_tree_entry_get_mode(
4859 s->selected_entry))) {
4860 struct got_tree_object *subtree;
4861 err = got_object_open_as_tree(&subtree, s->repo,
4862 got_tree_entry_get_id(s->selected_entry));
4863 if (err)
4864 break;
4865 err = tree_view_visit_subtree(subtree, s);
4866 if (err) {
4867 got_object_tree_close(subtree);
4868 break;
4870 } else if (S_ISREG(got_tree_entry_get_mode(
4871 s->selected_entry))) {
4872 struct tog_view *blame_view;
4873 int begin_x = view_is_parent_view(view) ?
4874 view_split_begin_x(view->begin_x) : 0;
4876 err = blame_tree_entry(&blame_view, begin_x,
4877 s->selected_entry, &s->parents,
4878 s->commit_id, s->refs, s->repo);
4879 if (err)
4880 break;
4881 if (view_is_parent_view(view)) {
4882 err = view_close_child(view);
4883 if (err)
4884 return err;
4885 err = view_set_child(view, blame_view);
4886 if (err) {
4887 view_close(blame_view);
4888 break;
4890 *focus_view = blame_view;
4891 view->child_focussed = 1;
4892 } else
4893 *new_view = blame_view;
4895 break;
4896 case KEY_RESIZE:
4897 if (s->selected > view->nlines)
4898 s->selected = s->ndisplayed - 1;
4899 break;
4900 default:
4901 break;
4904 return err;
4907 __dead static void
4908 usage_tree(void)
4910 endwin();
4911 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4912 getprogname());
4913 exit(1);
4916 static const struct got_error *
4917 cmd_tree(int argc, char *argv[])
4919 const struct got_error *error;
4920 struct got_repository *repo = NULL;
4921 struct got_reflist_head refs;
4922 char *repo_path = NULL;
4923 struct got_object_id *commit_id = NULL;
4924 char *commit_id_arg = NULL;
4925 struct got_commit_object *commit = NULL;
4926 struct got_tree_object *tree = NULL;
4927 int ch;
4928 struct tog_view *view;
4930 SIMPLEQ_INIT(&refs);
4932 #ifndef PROFILE
4933 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4934 NULL) == -1)
4935 err(1, "pledge");
4936 #endif
4938 while ((ch = getopt(argc, argv, "c:")) != -1) {
4939 switch (ch) {
4940 case 'c':
4941 commit_id_arg = optarg;
4942 break;
4943 default:
4944 usage_tree();
4945 /* NOTREACHED */
4949 argc -= optind;
4950 argv += optind;
4952 if (argc == 0) {
4953 struct got_worktree *worktree;
4954 char *cwd = getcwd(NULL, 0);
4955 if (cwd == NULL)
4956 return got_error_from_errno("getcwd");
4957 error = got_worktree_open(&worktree, cwd);
4958 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4959 goto done;
4960 if (worktree) {
4961 free(cwd);
4962 repo_path =
4963 strdup(got_worktree_get_repo_path(worktree));
4964 got_worktree_close(worktree);
4965 } else
4966 repo_path = cwd;
4967 if (repo_path == NULL) {
4968 error = got_error_from_errno("strdup");
4969 goto done;
4971 } else if (argc == 1) {
4972 repo_path = realpath(argv[0], NULL);
4973 if (repo_path == NULL)
4974 return got_error_from_errno2("realpath", argv[0]);
4975 } else
4976 usage_tree();
4978 init_curses();
4980 error = got_repo_open(&repo, repo_path, NULL);
4981 if (error != NULL)
4982 goto done;
4984 error = apply_unveil(got_repo_get_path(repo), NULL);
4985 if (error)
4986 goto done;
4988 error = got_repo_match_object_id(&commit_id, NULL,
4989 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
4990 GOT_OBJ_TYPE_COMMIT, 1, repo);
4991 if (error != NULL)
4992 goto done;
4994 error = got_object_open_as_commit(&commit, repo, commit_id);
4995 if (error != NULL)
4996 goto done;
4998 error = got_object_open_as_tree(&tree, repo,
4999 got_object_commit_get_tree_id(commit));
5000 if (error != NULL)
5001 goto done;
5003 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5004 if (error)
5005 goto done;
5007 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5008 if (view == NULL) {
5009 error = got_error_from_errno("view_open");
5010 goto done;
5012 error = open_tree_view(view, tree, commit_id, &refs, repo);
5013 if (error)
5014 goto done;
5015 error = view_loop(view);
5016 done:
5017 free(repo_path);
5018 free(commit_id);
5019 if (commit)
5020 got_object_commit_close(commit);
5021 if (tree)
5022 got_object_tree_close(tree);
5023 if (repo)
5024 got_repo_close(repo);
5025 got_ref_list_free(&refs);
5026 return error;
5029 static void
5030 list_commands(void)
5032 int i;
5034 fprintf(stderr, "commands:");
5035 for (i = 0; i < nitems(tog_commands); i++) {
5036 struct tog_cmd *cmd = &tog_commands[i];
5037 fprintf(stderr, " %s", cmd->name);
5039 fputc('\n', stderr);
5042 __dead static void
5043 usage(int hflag)
5045 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5046 getprogname());
5047 if (hflag)
5048 list_commands();
5049 exit(1);
5052 static char **
5053 make_argv(const char *arg0, const char *arg1)
5055 char **argv;
5056 int argc = (arg1 == NULL ? 1 : 2);
5058 argv = calloc(argc, sizeof(char *));
5059 if (argv == NULL)
5060 err(1, "calloc");
5061 argv[0] = strdup(arg0);
5062 if (argv[0] == NULL)
5063 err(1, "strdup");
5064 if (arg1) {
5065 argv[1] = strdup(arg1);
5066 if (argv[1] == NULL)
5067 err(1, "strdup");
5070 return argv;
5073 int
5074 main(int argc, char *argv[])
5076 const struct got_error *error = NULL;
5077 struct tog_cmd *cmd = NULL;
5078 int ch, hflag = 0, Vflag = 0;
5079 char **cmd_argv = NULL;
5080 static struct option longopts[] = {
5081 { "version", no_argument, NULL, 'V' },
5082 { NULL, 0, NULL, 0}
5085 setlocale(LC_CTYPE, "");
5087 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5088 switch (ch) {
5089 case 'h':
5090 hflag = 1;
5091 break;
5092 case 'V':
5093 Vflag = 1;
5094 break;
5095 default:
5096 usage(hflag);
5097 /* NOTREACHED */
5101 argc -= optind;
5102 argv += optind;
5103 optind = 0;
5104 optreset = 1;
5106 if (Vflag) {
5107 got_version_print_str();
5108 return 1;
5111 if (argc == 0) {
5112 if (hflag)
5113 usage(hflag);
5114 /* Build an argument vector which runs a default command. */
5115 cmd = &tog_commands[0];
5116 cmd_argv = make_argv(cmd->name, NULL);
5117 argc = 1;
5118 } else {
5119 int i;
5121 /* Did the user specific a command? */
5122 for (i = 0; i < nitems(tog_commands); i++) {
5123 if (strncmp(tog_commands[i].name, argv[0],
5124 strlen(argv[0])) == 0) {
5125 cmd = &tog_commands[i];
5126 break;
5130 if (cmd == NULL) {
5131 fprintf(stderr, "%s: unknown command '%s'\n",
5132 getprogname(), argv[0]);
5133 list_commands();
5134 return 1;
5138 if (hflag)
5139 cmd->cmd_usage();
5140 else
5141 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5143 endwin();
5144 free(cmd_argv);
5145 if (error && error->code != GOT_ERR_CANCELLED)
5146 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5147 return 0;