Blob


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