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_CYAN;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 return COLOR_BLUE;
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 return COLOR_GREEN;
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 return COLOR_GREEN;
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 return COLOR_YELLOW;
209 return -1;
212 static int
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
217 if (val == NULL)
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
221 return COLOR_BLACK;
222 if (strcasecmp(val, "red") == 0)
223 return COLOR_RED;
224 if (strcasecmp(val, "green") == 0)
225 return COLOR_GREEN;
226 if (strcasecmp(val, "yellow") == 0)
227 return COLOR_YELLOW;
228 if (strcasecmp(val, "blue") == 0)
229 return COLOR_BLUE;
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
233 return COLOR_CYAN;
234 if (strcasecmp(val, "white") == 0)
235 return COLOR_WHITE;
236 if (strcasecmp(val, "default") == 0)
237 return -1;
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
245 FILE *f;
246 int first_displayed_line;
247 int last_displayed_line;
248 int eof;
249 int diff_context;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
254 /* passed from log view; may be NULL */
255 struct tog_view *log_view;
256 };
258 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
260 struct tog_log_thread_args {
261 pthread_cond_t need_commits;
262 int commits_needed;
263 struct got_commit_graph *graph;
264 struct commit_queue *commits;
265 const char *in_repo_path;
266 struct got_object_id *start_id;
267 struct got_repository *repo;
268 int log_complete;
269 sig_atomic_t *quit;
270 struct commit_queue_entry **first_displayed_entry;
271 struct commit_queue_entry **selected_entry;
272 int *searching;
273 int *search_next_done;
274 regex_t *regex;
275 };
277 struct tog_log_view_state {
278 struct commit_queue commits;
279 struct commit_queue_entry *first_displayed_entry;
280 struct commit_queue_entry *last_displayed_entry;
281 struct commit_queue_entry *selected_entry;
282 int selected;
283 char *in_repo_path;
284 const char *head_ref_name;
285 struct got_repository *repo;
286 struct got_reflist_head *refs;
287 struct got_object_id *start_id;
288 sig_atomic_t quit;
289 pthread_t thread;
290 struct tog_log_thread_args thread_args;
291 struct commit_queue_entry *matched_entry;
292 struct commit_queue_entry *search_entry;
293 struct tog_colors colors;
294 };
296 #define TOG_COLOR_DIFF_MINUS 1
297 #define TOG_COLOR_DIFF_PLUS 2
298 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
299 #define TOG_COLOR_DIFF_META 4
300 #define TOG_COLOR_TREE_SUBMODULE 5
301 #define TOG_COLOR_TREE_SYMLINK 6
302 #define TOG_COLOR_TREE_DIRECTORY 7
303 #define TOG_COLOR_TREE_EXECUTABLE 8
304 #define TOG_COLOR_COMMIT 9
305 #define TOG_COLOR_AUTHOR 10
306 #define TOG_COLOR_DATE 11
308 struct tog_blame_cb_args {
309 struct tog_blame_line *lines; /* one per line */
310 int nlines;
312 struct tog_view *view;
313 struct got_object_id *commit_id;
314 int *quit;
315 };
317 struct tog_blame_thread_args {
318 const char *path;
319 struct got_repository *repo;
320 struct tog_blame_cb_args *cb_args;
321 int *complete;
322 got_cancel_cb cancel_cb;
323 void *cancel_arg;
324 };
326 struct tog_blame {
327 FILE *f;
328 size_t filesize;
329 struct tog_blame_line *lines;
330 int nlines;
331 off_t *line_offsets;
332 pthread_t thread;
333 struct tog_blame_thread_args thread_args;
334 struct tog_blame_cb_args cb_args;
335 const char *path;
336 };
338 struct tog_blame_view_state {
339 int first_displayed_line;
340 int last_displayed_line;
341 int selected_line;
342 int blame_complete;
343 int eof;
344 int done;
345 struct got_object_id_queue blamed_commits;
346 struct got_object_qid *blamed_commit;
347 char *path;
348 struct got_repository *repo;
349 struct got_reflist_head *refs;
350 struct got_object_id *commit_id;
351 struct tog_blame blame;
352 int matched_line;
353 struct tog_colors colors;
354 };
356 struct tog_parent_tree {
357 TAILQ_ENTRY(tog_parent_tree) entry;
358 struct got_tree_object *tree;
359 struct got_tree_entry *first_displayed_entry;
360 struct got_tree_entry *selected_entry;
361 int selected;
362 };
364 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
366 struct tog_tree_view_state {
367 char *tree_label;
368 struct got_tree_object *root;
369 struct got_tree_object *tree;
370 struct got_tree_entry *first_displayed_entry;
371 struct got_tree_entry *last_displayed_entry;
372 struct got_tree_entry *selected_entry;
373 int ndisplayed, selected, show_ids;
374 struct tog_parent_trees parents;
375 struct got_object_id *commit_id;
376 struct got_repository *repo;
377 struct got_reflist_head *refs;
378 struct got_tree_entry *matched_entry;
379 struct tog_colors colors;
380 };
382 /*
383 * We implement two types of views: parent views and child views.
385 * The 'Tab' key switches between a parent view and its child view.
386 * Child views are shown side-by-side to their parent view, provided
387 * there is enough screen estate.
389 * When a new view is opened from within a parent view, this new view
390 * becomes a child view of the parent view, replacing any existing child.
392 * When a new view is opened from within a child view, this new view
393 * becomes a parent view which will obscure the views below until the
394 * user quits the new parent view by typing 'q'.
396 * This list of views contains parent views only.
397 * Child views are only pointed to by their parent view.
398 */
399 TAILQ_HEAD(tog_view_list_head, tog_view);
401 struct tog_view {
402 TAILQ_ENTRY(tog_view) entry;
403 WINDOW *window;
404 PANEL *panel;
405 int nlines, ncols, begin_y, begin_x;
406 int lines, cols; /* copies of LINES and COLS */
407 int focussed;
408 struct tog_view *parent;
409 struct tog_view *child;
410 int child_focussed;
412 /* type-specific state */
413 enum tog_view_type type;
414 union {
415 struct tog_diff_view_state diff;
416 struct tog_log_view_state log;
417 struct tog_blame_view_state blame;
418 struct tog_tree_view_state tree;
419 } state;
421 const struct got_error *(*show)(struct tog_view *);
422 const struct got_error *(*input)(struct tog_view **,
423 struct tog_view **, struct tog_view**, struct tog_view *, int);
424 const struct got_error *(*close)(struct tog_view *);
426 const struct got_error *(*search_start)(struct tog_view *);
427 const struct got_error *(*search_next)(struct tog_view *);
428 int searching;
429 #define TOG_SEARCH_FORWARD 1
430 #define TOG_SEARCH_BACKWARD 2
431 int search_next_done;
432 regex_t regex;
433 };
435 static const struct got_error *open_diff_view(struct tog_view *,
436 struct got_object_id *, struct got_object_id *, struct tog_view *,
437 struct got_reflist_head *, struct got_repository *);
438 static const struct got_error *show_diff_view(struct tog_view *);
439 static const struct got_error *input_diff_view(struct tog_view **,
440 struct tog_view **, struct tog_view **, struct tog_view *, int);
441 static const struct got_error* close_diff_view(struct tog_view *);
443 static const struct got_error *open_log_view(struct tog_view *,
444 struct got_object_id *, struct got_reflist_head *,
445 struct got_repository *, const char *, const char *, int);
446 static const struct got_error * show_log_view(struct tog_view *);
447 static const struct got_error *input_log_view(struct tog_view **,
448 struct tog_view **, struct tog_view **, struct tog_view *, int);
449 static const struct got_error *close_log_view(struct tog_view *);
450 static const struct got_error *search_start_log_view(struct tog_view *);
451 static const struct got_error *search_next_log_view(struct tog_view *);
453 static const struct got_error *open_blame_view(struct tog_view *, char *,
454 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 static const struct got_error *show_blame_view(struct tog_view *);
456 static const struct got_error *input_blame_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_blame_view(struct tog_view *);
459 static const struct got_error *search_start_blame_view(struct tog_view *);
460 static const struct got_error *search_next_blame_view(struct tog_view *);
462 static const struct got_error *open_tree_view(struct tog_view *,
463 struct got_tree_object *, struct got_object_id *,
464 struct got_reflist_head *, struct got_repository *);
465 static const struct got_error *show_tree_view(struct tog_view *);
466 static const struct got_error *input_tree_view(struct tog_view **,
467 struct tog_view **, struct tog_view **, struct tog_view *, int);
468 static const struct got_error *close_tree_view(struct tog_view *);
469 static const struct got_error *search_start_tree_view(struct tog_view *);
470 static const struct got_error *search_next_tree_view(struct tog_view *);
472 static volatile sig_atomic_t tog_sigwinch_received;
473 static volatile sig_atomic_t tog_sigpipe_received;
474 static volatile sig_atomic_t tog_sigcont_received;
476 static void
477 tog_sigwinch(int signo)
479 tog_sigwinch_received = 1;
482 static void
483 tog_sigpipe(int signo)
485 tog_sigpipe_received = 1;
488 static void
489 tog_sigcont(int signo)
491 tog_sigcont_received = 1;
494 static const struct got_error *
495 view_close(struct tog_view *view)
497 const struct got_error *err = NULL;
499 if (view->child) {
500 view_close(view->child);
501 view->child = NULL;
503 if (view->close)
504 err = view->close(view);
505 if (view->panel)
506 del_panel(view->panel);
507 if (view->window)
508 delwin(view->window);
509 free(view);
510 return err;
513 static struct tog_view *
514 view_open(int nlines, int ncols, int begin_y, int begin_x,
515 enum tog_view_type type)
517 struct tog_view *view = calloc(1, sizeof(*view));
519 if (view == NULL)
520 return NULL;
522 view->type = type;
523 view->lines = LINES;
524 view->cols = COLS;
525 view->nlines = nlines ? nlines : LINES - begin_y;
526 view->ncols = ncols ? ncols : COLS - begin_x;
527 view->begin_y = begin_y;
528 view->begin_x = begin_x;
529 view->window = newwin(nlines, ncols, begin_y, begin_x);
530 if (view->window == NULL) {
531 view_close(view);
532 return NULL;
534 view->panel = new_panel(view->window);
535 if (view->panel == NULL ||
536 set_panel_userptr(view->panel, view) != OK) {
537 view_close(view);
538 return NULL;
541 keypad(view->window, TRUE);
542 return view;
545 static int
546 view_split_begin_x(int begin_x)
548 if (begin_x > 0 || COLS < 120)
549 return 0;
550 return (COLS - MAX(COLS / 2, 80));
553 static const struct got_error *view_resize(struct tog_view *);
555 static const struct got_error *
556 view_splitscreen(struct tog_view *view)
558 const struct got_error *err = NULL;
560 view->begin_y = 0;
561 view->begin_x = view_split_begin_x(0);
562 view->nlines = LINES;
563 view->ncols = COLS - view->begin_x;
564 view->lines = LINES;
565 view->cols = COLS;
566 err = view_resize(view);
567 if (err)
568 return err;
570 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
571 return got_error_from_errno("mvwin");
573 return NULL;
576 static const struct got_error *
577 view_fullscreen(struct tog_view *view)
579 const struct got_error *err = NULL;
581 view->begin_x = 0;
582 view->begin_y = 0;
583 view->nlines = LINES;
584 view->ncols = COLS;
585 view->lines = LINES;
586 view->cols = COLS;
587 err = view_resize(view);
588 if (err)
589 return err;
591 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
592 return got_error_from_errno("mvwin");
594 return NULL;
597 static int
598 view_is_parent_view(struct tog_view *view)
600 return view->parent == NULL;
603 static const struct got_error *
604 view_resize(struct tog_view *view)
606 int nlines, ncols;
608 if (view->lines > LINES)
609 nlines = view->nlines - (view->lines - LINES);
610 else
611 nlines = view->nlines + (LINES - view->lines);
613 if (view->cols > COLS)
614 ncols = view->ncols - (view->cols - COLS);
615 else
616 ncols = view->ncols + (COLS - view->cols);
618 if (wresize(view->window, nlines, ncols) == ERR)
619 return got_error_from_errno("wresize");
620 if (replace_panel(view->panel, view->window) == ERR)
621 return got_error_from_errno("replace_panel");
622 wclear(view->window);
624 view->nlines = nlines;
625 view->ncols = ncols;
626 view->lines = LINES;
627 view->cols = COLS;
629 if (view->child) {
630 view->child->begin_x = view_split_begin_x(view->begin_x);
631 if (view->child->begin_x == 0) {
632 view_fullscreen(view->child);
633 if (view->child->focussed)
634 show_panel(view->child->panel);
635 else
636 show_panel(view->panel);
637 } else {
638 view_splitscreen(view->child);
639 show_panel(view->child->panel);
643 return NULL;
646 static const struct got_error *
647 view_close_child(struct tog_view *view)
649 const struct got_error *err = NULL;
651 if (view->child == NULL)
652 return NULL;
654 err = view_close(view->child);
655 view->child = NULL;
656 return err;
659 static const struct got_error *
660 view_set_child(struct tog_view *view, struct tog_view *child)
662 const struct got_error *err = NULL;
664 view->child = child;
665 child->parent = view;
666 return err;
669 static int
670 view_is_splitscreen(struct tog_view *view)
672 return view->begin_x > 0;
675 static void
676 tog_resizeterm(void)
678 int cols, lines;
679 struct winsize size;
681 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
682 cols = 80; /* Default */
683 lines = 24;
684 } else {
685 cols = size.ws_col;
686 lines = size.ws_row;
688 resize_term(lines, cols);
691 static const struct got_error *
692 view_search_start(struct tog_view *view)
694 const struct got_error *err = NULL;
695 char pattern[1024];
696 int ret;
697 int begin_x = 0;
699 if (view->nlines < 1)
700 return NULL;
702 if (!view_is_parent_view(view))
703 begin_x = view_split_begin_x(view->begin_x);
704 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
705 begin_x, "/");
706 wclrtoeol(view->window);
708 nocbreak();
709 echo();
710 ret = wgetnstr(view->window, pattern, sizeof(pattern));
711 cbreak();
712 noecho();
713 if (ret == ERR)
714 return NULL;
716 if (view->searching) {
717 regfree(&view->regex);
718 view->searching = 0;
721 if (regcomp(&view->regex, pattern,
722 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
723 err = view->search_start(view);
724 if (err) {
725 regfree(&view->regex);
726 return err;
728 view->searching = TOG_SEARCH_FORWARD;
729 view->search_next_done = 0;
730 view->search_next(view);
733 return NULL;
736 static const struct got_error *
737 view_input(struct tog_view **new, struct tog_view **dead,
738 struct tog_view **focus, int *done, struct tog_view *view,
739 struct tog_view_list_head *views)
741 const struct got_error *err = NULL;
742 struct tog_view *v;
743 int ch, errcode;
745 *new = NULL;
746 *dead = NULL;
747 *focus = NULL;
749 if (view->searching && !view->search_next_done) {
750 errcode = pthread_mutex_unlock(&tog_mutex);
751 if (errcode)
752 return got_error_set_errno(errcode,
753 "pthread_mutex_unlock");
754 pthread_yield();
755 errcode = pthread_mutex_lock(&tog_mutex);
756 if (errcode)
757 return got_error_set_errno(errcode,
758 "pthread_mutex_lock");
759 view->search_next(view);
760 return NULL;
763 nodelay(stdscr, FALSE);
764 /* Allow threads to make progress while we are waiting for input. */
765 errcode = pthread_mutex_unlock(&tog_mutex);
766 if (errcode)
767 return got_error_set_errno(errcode, "pthread_mutex_unlock");
768 ch = wgetch(view->window);
769 errcode = pthread_mutex_lock(&tog_mutex);
770 if (errcode)
771 return got_error_set_errno(errcode, "pthread_mutex_lock");
772 nodelay(stdscr, TRUE);
774 if (tog_sigwinch_received || tog_sigcont_received) {
775 tog_resizeterm();
776 tog_sigwinch_received = 0;
777 tog_sigcont_received = 0;
778 TAILQ_FOREACH(v, views, entry) {
779 err = view_resize(v);
780 if (err)
781 return err;
782 err = v->input(new, dead, focus, v, KEY_RESIZE);
783 if (err)
784 return err;
788 switch (ch) {
789 case ERR:
790 break;
791 case '\t':
792 if (view->child) {
793 *focus = view->child;
794 view->child_focussed = 1;
795 } else if (view->parent) {
796 *focus = view->parent;
797 view->parent->child_focussed = 0;
799 break;
800 case 'q':
801 err = view->input(new, dead, focus, view, ch);
802 *dead = view;
803 break;
804 case 'Q':
805 *done = 1;
806 break;
807 case 'f':
808 if (view_is_parent_view(view)) {
809 if (view->child == NULL)
810 break;
811 if (view_is_splitscreen(view->child)) {
812 *focus = view->child;
813 view->child_focussed = 1;
814 err = view_fullscreen(view->child);
815 } else
816 err = view_splitscreen(view->child);
817 if (err)
818 break;
819 err = view->child->input(new, dead, focus,
820 view->child, KEY_RESIZE);
821 } else {
822 if (view_is_splitscreen(view)) {
823 *focus = view;
824 view->parent->child_focussed = 1;
825 err = view_fullscreen(view);
826 } else {
827 err = view_splitscreen(view);
829 if (err)
830 break;
831 err = view->input(new, dead, focus, view,
832 KEY_RESIZE);
834 break;
835 case KEY_RESIZE:
836 break;
837 case '/':
838 if (view->search_start)
839 view_search_start(view);
840 else
841 err = view->input(new, dead, focus, view, ch);
842 break;
843 case 'N':
844 case 'n':
845 if (view->search_next && view->searching) {
846 view->searching = (ch == 'n' ?
847 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
848 view->search_next_done = 0;
849 view->search_next(view);
850 } else
851 err = view->input(new, dead, focus, view, ch);
852 break;
853 default:
854 err = view->input(new, dead, focus, view, ch);
855 break;
858 return err;
861 void
862 view_vborder(struct tog_view *view)
864 PANEL *panel;
865 struct tog_view *view_above;
867 if (view->parent)
868 return view_vborder(view->parent);
870 panel = panel_above(view->panel);
871 if (panel == NULL)
872 return;
874 view_above = panel_userptr(panel);
875 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
876 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
879 int
880 view_needs_focus_indication(struct tog_view *view)
882 if (view_is_parent_view(view)) {
883 if (view->child == NULL || view->child_focussed)
884 return 0;
885 if (!view_is_splitscreen(view->child))
886 return 0;
887 } else if (!view_is_splitscreen(view))
888 return 0;
890 return view->focussed;
893 static const struct got_error *
894 view_loop(struct tog_view *view)
896 const struct got_error *err = NULL;
897 struct tog_view_list_head views;
898 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
899 int fast_refresh = 10;
900 int done = 0, errcode;
902 errcode = pthread_mutex_lock(&tog_mutex);
903 if (errcode)
904 return got_error_set_errno(errcode, "pthread_mutex_lock");
906 TAILQ_INIT(&views);
907 TAILQ_INSERT_HEAD(&views, view, entry);
909 main_view = view;
910 view->focussed = 1;
911 err = view->show(view);
912 if (err)
913 return err;
914 update_panels();
915 doupdate();
916 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
917 /* Refresh fast during initialization, then become slower. */
918 if (fast_refresh && fast_refresh-- == 0)
919 halfdelay(10); /* switch to once per second */
921 err = view_input(&new_view, &dead_view, &focus_view, &done,
922 view, &views);
923 if (err)
924 break;
925 if (dead_view) {
926 struct tog_view *prev = NULL;
928 if (view_is_parent_view(dead_view))
929 prev = TAILQ_PREV(dead_view,
930 tog_view_list_head, entry);
931 else if (view->parent != dead_view)
932 prev = view->parent;
934 if (dead_view->parent)
935 dead_view->parent->child = NULL;
936 else
937 TAILQ_REMOVE(&views, dead_view, entry);
939 err = view_close(dead_view);
940 if (err || (dead_view == main_view && new_view == NULL))
941 goto done;
943 if (view == dead_view) {
944 if (focus_view)
945 view = focus_view;
946 else if (prev)
947 view = prev;
948 else if (!TAILQ_EMPTY(&views))
949 view = TAILQ_LAST(&views,
950 tog_view_list_head);
951 else
952 view = NULL;
953 if (view) {
954 if (view->child && view->child_focussed)
955 focus_view = view->child;
956 else
957 focus_view = view;
961 if (new_view) {
962 struct tog_view *v, *t;
963 /* Only allow one parent view per type. */
964 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
965 if (v->type != new_view->type)
966 continue;
967 TAILQ_REMOVE(&views, v, entry);
968 err = view_close(v);
969 if (err)
970 goto done;
971 break;
973 TAILQ_INSERT_TAIL(&views, new_view, entry);
974 view = new_view;
975 if (focus_view == NULL)
976 focus_view = new_view;
978 if (focus_view) {
979 show_panel(focus_view->panel);
980 if (view)
981 view->focussed = 0;
982 focus_view->focussed = 1;
983 view = focus_view;
984 if (new_view)
985 show_panel(new_view->panel);
986 if (view->child && view_is_splitscreen(view->child))
987 show_panel(view->child->panel);
989 if (view) {
990 if (focus_view == NULL) {
991 view->focussed = 1;
992 show_panel(view->panel);
993 if (view->child && view_is_splitscreen(view->child))
994 show_panel(view->child->panel);
995 focus_view = view;
997 if (view->parent) {
998 err = view->parent->show(view->parent);
999 if (err)
1000 goto done;
1002 err = view->show(view);
1003 if (err)
1004 goto done;
1005 if (view->child) {
1006 err = view->child->show(view->child);
1007 if (err)
1008 goto done;
1010 update_panels();
1011 doupdate();
1014 done:
1015 while (!TAILQ_EMPTY(&views)) {
1016 view = TAILQ_FIRST(&views);
1017 TAILQ_REMOVE(&views, view, entry);
1018 view_close(view);
1021 errcode = pthread_mutex_unlock(&tog_mutex);
1022 if (errcode && err == NULL)
1023 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1025 return err;
1028 __dead static void
1029 usage_log(void)
1031 endwin();
1032 fprintf(stderr,
1033 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1034 getprogname());
1035 exit(1);
1038 /* Create newly allocated wide-character string equivalent to a byte string. */
1039 static const struct got_error *
1040 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1042 char *vis = NULL;
1043 const struct got_error *err = NULL;
1045 *ws = NULL;
1046 *wlen = mbstowcs(NULL, s, 0);
1047 if (*wlen == (size_t)-1) {
1048 int vislen;
1049 if (errno != EILSEQ)
1050 return got_error_from_errno("mbstowcs");
1052 /* byte string invalid in current encoding; try to "fix" it */
1053 err = got_mbsavis(&vis, &vislen, s);
1054 if (err)
1055 return err;
1056 *wlen = mbstowcs(NULL, vis, 0);
1057 if (*wlen == (size_t)-1) {
1058 err = got_error_from_errno("mbstowcs"); /* give up */
1059 goto done;
1063 *ws = calloc(*wlen + 1, sizeof(**ws));
1064 if (*ws == NULL) {
1065 err = got_error_from_errno("calloc");
1066 goto done;
1069 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1070 err = got_error_from_errno("mbstowcs");
1071 done:
1072 free(vis);
1073 if (err) {
1074 free(*ws);
1075 *ws = NULL;
1076 *wlen = 0;
1078 return err;
1081 /* Format a line for display, ensuring that it won't overflow a width limit. */
1082 static const struct got_error *
1083 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1084 int col_tab_align)
1086 const struct got_error *err = NULL;
1087 int cols = 0;
1088 wchar_t *wline = NULL;
1089 size_t wlen;
1090 int i;
1092 *wlinep = NULL;
1093 *widthp = 0;
1095 err = mbs2ws(&wline, &wlen, line);
1096 if (err)
1097 return err;
1099 i = 0;
1100 while (i < wlen) {
1101 int width = wcwidth(wline[i]);
1103 if (width == 0) {
1104 i++;
1105 continue;
1108 if (width == 1 || width == 2) {
1109 if (cols + width > wlimit)
1110 break;
1111 cols += width;
1112 i++;
1113 } else if (width == -1) {
1114 if (wline[i] == L'\t') {
1115 width = TABSIZE -
1116 ((cols + col_tab_align) % TABSIZE);
1117 if (cols + width > wlimit)
1118 break;
1119 cols += width;
1121 i++;
1122 } else {
1123 err = got_error_from_errno("wcwidth");
1124 goto done;
1127 wline[i] = L'\0';
1128 if (widthp)
1129 *widthp = cols;
1130 done:
1131 if (err)
1132 free(wline);
1133 else
1134 *wlinep = wline;
1135 return err;
1138 static const struct got_error*
1139 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1140 struct got_object_id *id, struct got_repository *repo)
1142 static const struct got_error *err = NULL;
1143 struct got_reflist_entry *re;
1144 char *s;
1145 const char *name;
1147 *refs_str = NULL;
1149 SIMPLEQ_FOREACH(re, refs, entry) {
1150 struct got_tag_object *tag = NULL;
1151 int cmp;
1153 name = got_ref_get_name(re->ref);
1154 if (strcmp(name, GOT_REF_HEAD) == 0)
1155 continue;
1156 if (strncmp(name, "refs/", 5) == 0)
1157 name += 5;
1158 if (strncmp(name, "got/", 4) == 0)
1159 continue;
1160 if (strncmp(name, "heads/", 6) == 0)
1161 name += 6;
1162 if (strncmp(name, "remotes/", 8) == 0)
1163 name += 8;
1164 if (strncmp(name, "tags/", 5) == 0) {
1165 err = got_object_open_as_tag(&tag, repo, re->id);
1166 if (err) {
1167 if (err->code != GOT_ERR_OBJ_TYPE)
1168 break;
1169 /* Ref points at something other than a tag. */
1170 err = NULL;
1171 tag = NULL;
1174 cmp = got_object_id_cmp(tag ?
1175 got_object_tag_get_object_id(tag) : re->id, id);
1176 if (tag)
1177 got_object_tag_close(tag);
1178 if (cmp != 0)
1179 continue;
1180 s = *refs_str;
1181 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1182 s ? ", " : "", name) == -1) {
1183 err = got_error_from_errno("asprintf");
1184 free(s);
1185 *refs_str = NULL;
1186 break;
1188 free(s);
1191 return err;
1194 static const struct got_error *
1195 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1196 int col_tab_align)
1198 char *smallerthan, *at;
1200 smallerthan = strchr(author, '<');
1201 if (smallerthan && smallerthan[1] != '\0')
1202 author = smallerthan + 1;
1203 at = strchr(author, '@');
1204 if (at)
1205 *at = '\0';
1206 return format_line(wauthor, author_width, author, limit, col_tab_align);
1209 static const struct got_error *
1210 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1211 struct got_object_id *id, struct got_reflist_head *refs,
1212 const size_t date_display_cols, int author_display_cols,
1213 struct tog_colors *colors)
1215 const struct got_error *err = NULL;
1216 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1217 char *logmsg0 = NULL, *logmsg = NULL;
1218 char *author = NULL;
1219 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1220 int author_width, logmsg_width;
1221 char *newline, *line = NULL;
1222 int col, limit;
1223 const int avail = view->ncols;
1224 struct tm tm;
1225 time_t committer_time;
1226 struct tog_color *tc;
1228 committer_time = got_object_commit_get_committer_time(commit);
1229 if (localtime_r(&committer_time, &tm) == NULL)
1230 return got_error_from_errno("localtime_r");
1231 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1232 >= sizeof(datebuf))
1233 return got_error(GOT_ERR_NO_SPACE);
1235 if (avail <= date_display_cols)
1236 limit = MIN(sizeof(datebuf) - 1, avail);
1237 else
1238 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1239 tc = get_color(colors, TOG_COLOR_DATE);
1240 if (tc)
1241 wattr_on(view->window,
1242 COLOR_PAIR(tc->colorpair), NULL);
1243 waddnstr(view->window, datebuf, limit);
1244 if (tc)
1245 wattr_off(view->window,
1246 COLOR_PAIR(tc->colorpair), NULL);
1247 col = limit;
1248 if (col > avail)
1249 goto done;
1251 if (avail >= 120) {
1252 char *id_str;
1253 err = got_object_id_str(&id_str, id);
1254 if (err)
1255 goto done;
1256 tc = get_color(colors, TOG_COLOR_COMMIT);
1257 if (tc)
1258 wattr_on(view->window,
1259 COLOR_PAIR(tc->colorpair), NULL);
1260 wprintw(view->window, "%.8s ", id_str);
1261 if (tc)
1262 wattr_off(view->window,
1263 COLOR_PAIR(tc->colorpair), NULL);
1264 free(id_str);
1265 col += 9;
1266 if (col > avail)
1267 goto done;
1270 author = strdup(got_object_commit_get_author(commit));
1271 if (author == NULL) {
1272 err = got_error_from_errno("strdup");
1273 goto done;
1275 err = format_author(&wauthor, &author_width, author, avail - col, col);
1276 if (err)
1277 goto done;
1278 tc = get_color(colors, TOG_COLOR_AUTHOR);
1279 if (tc)
1280 wattr_on(view->window,
1281 COLOR_PAIR(tc->colorpair), NULL);
1282 waddwstr(view->window, wauthor);
1283 if (tc)
1284 wattr_off(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 col += author_width;
1287 while (col < avail && author_width < author_display_cols + 2) {
1288 waddch(view->window, ' ');
1289 col++;
1290 author_width++;
1292 if (col > avail)
1293 goto done;
1295 err = got_object_commit_get_logmsg(&logmsg0, commit);
1296 if (err)
1297 goto done;
1298 logmsg = logmsg0;
1299 while (*logmsg == '\n')
1300 logmsg++;
1301 newline = strchr(logmsg, '\n');
1302 if (newline)
1303 *newline = '\0';
1304 limit = avail - col;
1305 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1306 if (err)
1307 goto done;
1308 waddwstr(view->window, wlogmsg);
1309 col += logmsg_width;
1310 while (col < avail) {
1311 waddch(view->window, ' ');
1312 col++;
1314 done:
1315 free(logmsg0);
1316 free(wlogmsg);
1317 free(author);
1318 free(wauthor);
1319 free(line);
1320 return err;
1323 static struct commit_queue_entry *
1324 alloc_commit_queue_entry(struct got_commit_object *commit,
1325 struct got_object_id *id)
1327 struct commit_queue_entry *entry;
1329 entry = calloc(1, sizeof(*entry));
1330 if (entry == NULL)
1331 return NULL;
1333 entry->id = id;
1334 entry->commit = commit;
1335 return entry;
1338 static void
1339 pop_commit(struct commit_queue *commits)
1341 struct commit_queue_entry *entry;
1343 entry = TAILQ_FIRST(&commits->head);
1344 TAILQ_REMOVE(&commits->head, entry, entry);
1345 got_object_commit_close(entry->commit);
1346 commits->ncommits--;
1347 /* Don't free entry->id! It is owned by the commit graph. */
1348 free(entry);
1351 static void
1352 free_commits(struct commit_queue *commits)
1354 while (!TAILQ_EMPTY(&commits->head))
1355 pop_commit(commits);
1358 static const struct got_error *
1359 match_commit(int *have_match, struct got_object_id *id,
1360 struct got_commit_object *commit, regex_t *regex)
1362 const struct got_error *err = NULL;
1363 regmatch_t regmatch;
1364 char *id_str = NULL, *logmsg = NULL;
1366 *have_match = 0;
1368 err = got_object_id_str(&id_str, id);
1369 if (err)
1370 return err;
1372 err = got_object_commit_get_logmsg(&logmsg, commit);
1373 if (err)
1374 goto done;
1376 if (regexec(regex, got_object_commit_get_author(commit), 1,
1377 &regmatch, 0) == 0 ||
1378 regexec(regex, got_object_commit_get_committer(commit), 1,
1379 &regmatch, 0) == 0 ||
1380 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1381 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1382 *have_match = 1;
1383 done:
1384 free(id_str);
1385 free(logmsg);
1386 return err;
1389 static const struct got_error *
1390 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1391 int minqueue, struct got_repository *repo, const char *path,
1392 int *searching, int *search_next_done, regex_t *regex)
1394 const struct got_error *err = NULL;
1395 int nqueued = 0, have_match = 0;
1398 * We keep all commits open throughout the lifetime of the log
1399 * view in order to avoid having to re-fetch commits from disk
1400 * while updating the display.
1402 while (nqueued < minqueue ||
1403 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1404 struct got_object_id *id;
1405 struct got_commit_object *commit;
1406 struct commit_queue_entry *entry;
1407 int errcode;
1409 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1410 if (err || id == NULL)
1411 break;
1413 err = got_object_open_as_commit(&commit, repo, id);
1414 if (err)
1415 break;
1416 entry = alloc_commit_queue_entry(commit, id);
1417 if (entry == NULL) {
1418 err = got_error_from_errno("alloc_commit_queue_entry");
1419 break;
1422 errcode = pthread_mutex_lock(&tog_mutex);
1423 if (errcode) {
1424 err = got_error_set_errno(errcode,
1425 "pthread_mutex_lock");
1426 break;
1429 entry->idx = commits->ncommits;
1430 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1431 nqueued++;
1432 commits->ncommits++;
1434 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1435 err = match_commit(&have_match, id, commit, regex);
1436 if (err) {
1437 pthread_mutex_lock(&tog_mutex);
1438 break;
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode,
1445 "pthread_mutex_unlock");
1447 if (have_match)
1448 break;
1451 return err;
1454 static const struct got_error *
1455 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 struct commit_queue *commits, int selected_idx, int limit,
1458 struct got_reflist_head *refs, const char *path, int commits_needed,
1459 struct tog_colors *colors)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 struct commit_queue_entry *entry;
1464 int width;
1465 int ncommits, author_cols = 4;
1466 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 char *refs_str = NULL;
1468 wchar_t *wline;
1469 struct tog_color *tc;
1470 static const size_t date_display_cols = 12;
1472 entry = first;
1473 ncommits = 0;
1474 while (entry) {
1475 if (ncommits == selected_idx) {
1476 *selected = entry;
1477 break;
1479 entry = TAILQ_NEXT(entry, entry);
1480 ncommits++;
1483 if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 err = got_object_id_str(&id_str, (*selected)->id);
1485 if (err)
1486 return err;
1487 if (refs) {
1488 err = build_refs_str(&refs_str, refs, (*selected)->id,
1489 s->repo);
1490 if (err)
1491 goto done;
1495 if (commits_needed == 0)
1496 halfdelay(10); /* disable fast refresh */
1498 if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 entry ? entry->idx + 1 : 0, commits->ncommits,
1500 commits_needed > 0 ?
1501 (view->searching && view->search_next_done == 0
1502 ? "searching..." : "loading... ") :
1503 (refs_str ? refs_str : "")) == -1) {
1504 err = got_error_from_errno("asprintf");
1505 goto done;
1508 if (path && strcmp(path, "/") != 0) {
1509 if (asprintf(&header, "commit %s %s%s",
1510 id_str ? id_str : "........................................",
1511 path, ncommits_str) == -1) {
1512 err = got_error_from_errno("asprintf");
1513 header = NULL;
1514 goto done;
1516 } else if (asprintf(&header, "commit %s%s",
1517 id_str ? id_str : "........................................",
1518 ncommits_str) == -1) {
1519 err = got_error_from_errno("asprintf");
1520 header = NULL;
1521 goto done;
1523 err = format_line(&wline, &width, header, view->ncols, 0);
1524 if (err)
1525 goto done;
1527 werase(view->window);
1529 if (view_needs_focus_indication(view))
1530 wstandout(view->window);
1531 tc = get_color(colors, TOG_COLOR_COMMIT);
1532 if (tc)
1533 wattr_on(view->window,
1534 COLOR_PAIR(tc->colorpair), NULL);
1535 waddwstr(view->window, wline);
1536 if (tc)
1537 wattr_off(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 while (width < view->ncols) {
1540 waddch(view->window, ' ');
1541 width++;
1543 if (view_needs_focus_indication(view))
1544 wstandend(view->window);
1545 free(wline);
1546 if (limit <= 1)
1547 goto done;
1549 /* Grow author column size if necessary. */
1550 entry = first;
1551 ncommits = 0;
1552 while (entry) {
1553 char *author;
1554 wchar_t *wauthor;
1555 int width;
1556 if (ncommits >= limit - 1)
1557 break;
1558 author = strdup(got_object_commit_get_author(entry->commit));
1559 if (author == NULL) {
1560 err = got_error_from_errno("strdup");
1561 goto done;
1563 err = format_author(&wauthor, &width, author, COLS,
1564 date_display_cols);
1565 if (author_cols < width)
1566 author_cols = width;
1567 free(wauthor);
1568 free(author);
1569 ncommits++;
1570 entry = TAILQ_NEXT(entry, entry);
1573 entry = first;
1574 *last = first;
1575 ncommits = 0;
1576 while (entry) {
1577 if (ncommits >= limit - 1)
1578 break;
1579 if (ncommits == selected_idx)
1580 wstandout(view->window);
1581 err = draw_commit(view, entry->commit, entry->id, refs,
1582 date_display_cols, author_cols, colors);
1583 if (ncommits == selected_idx)
1584 wstandend(view->window);
1585 if (err)
1586 goto done;
1587 ncommits++;
1588 *last = entry;
1589 entry = TAILQ_NEXT(entry, entry);
1592 view_vborder(view);
1593 done:
1594 free(id_str);
1595 free(refs_str);
1596 free(ncommits_str);
1597 free(header);
1598 return err;
1601 static void
1602 scroll_up(struct tog_view *view,
1603 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1607 int nscrolled = 0;
1609 entry = TAILQ_FIRST(&commits->head);
1610 if (*first_displayed_entry == entry)
1611 return;
1613 entry = *first_displayed_entry;
1614 while (entry && nscrolled < maxscroll) {
1615 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1616 if (entry) {
1617 *first_displayed_entry = entry;
1618 nscrolled++;
1623 static const struct got_error *
1624 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 pthread_cond_t *need_commits)
1627 int errcode;
1628 int max_wait = 20;
1630 halfdelay(1); /* fast refresh while loading commits */
1632 while (*commits_needed > 0) {
1633 if (*log_complete)
1634 break;
1636 /* Wake the log thread. */
1637 errcode = pthread_cond_signal(need_commits);
1638 if (errcode)
1639 return got_error_set_errno(errcode,
1640 "pthread_cond_signal");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1642 if (errcode)
1643 return got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1645 pthread_yield();
1646 errcode = pthread_mutex_lock(&tog_mutex);
1647 if (errcode)
1648 return got_error_set_errno(errcode,
1649 "pthread_mutex_lock");
1651 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 * Thread is not done yet; lose a key press
1654 * and let the user retry... this way the GUI
1655 * remains interactive while logging deep paths
1656 * with few commits in history.
1658 return NULL;
1662 return NULL;
1665 static const struct got_error *
1666 scroll_down(struct tog_view *view,
1667 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 struct commit_queue_entry **last_displayed_entry,
1669 struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 pthread_cond_t *need_commits)
1672 const struct got_error *err = NULL;
1673 struct commit_queue_entry *pentry;
1674 int nscrolled = 0;
1676 if (*last_displayed_entry == NULL)
1677 return NULL;
1679 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 if (pentry == NULL && !*log_complete) {
1682 * Ask the log thread for required amount of commits
1683 * plus some amount of pre-fetching.
1685 (*commits_needed) += maxscroll + 20;
1686 err = trigger_log_thread(0, commits_needed, log_complete,
1687 need_commits);
1688 if (err)
1689 return err;
1692 do {
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 if (pentry == NULL)
1695 break;
1697 *last_displayed_entry = pentry;
1699 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1700 if (pentry == NULL)
1701 break;
1702 *first_displayed_entry = pentry;
1703 } while (++nscrolled < maxscroll);
1705 return err;
1708 static const struct got_error *
1709 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 struct got_commit_object *commit, struct got_object_id *commit_id,
1711 struct tog_view *log_view, struct got_reflist_head *refs,
1712 struct got_repository *repo)
1714 const struct got_error *err;
1715 struct got_object_qid *parent_id;
1716 struct tog_view *diff_view;
1718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 if (diff_view == NULL)
1720 return got_error_from_errno("view_open");
1722 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 commit_id, log_view, refs, repo);
1725 if (err == NULL)
1726 *new_view = diff_view;
1727 return err;
1730 static const struct got_error *
1731 tree_view_visit_subtree(struct got_tree_object *subtree,
1732 struct tog_tree_view_state *s)
1734 struct tog_parent_tree *parent;
1736 parent = calloc(1, sizeof(*parent));
1737 if (parent == NULL)
1738 return got_error_from_errno("calloc");
1740 parent->tree = s->tree;
1741 parent->first_displayed_entry = s->first_displayed_entry;
1742 parent->selected_entry = s->selected_entry;
1743 parent->selected = s->selected;
1744 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1745 s->tree = subtree;
1746 s->selected = 0;
1747 s->first_displayed_entry = NULL;
1748 return NULL;
1752 static const struct got_error *
1753 browse_commit_tree(struct tog_view **new_view, int begin_x,
1754 struct commit_queue_entry *entry, const char *path,
1755 struct got_reflist_head *refs, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree;
1759 struct tog_tree_view_state *s;
1760 struct tog_view *tree_view;
1761 char *slash, *subpath = NULL;
1762 const char *p;
1764 err = got_object_open_as_tree(&tree, repo,
1765 got_object_commit_get_tree_id(entry->commit));
1766 if (err)
1767 return err;
1769 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1770 if (tree_view == NULL)
1771 return got_error_from_errno("view_open");
1773 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1774 if (err) {
1775 got_object_tree_close(tree);
1776 return err;
1778 s = &tree_view->state.tree;
1780 *new_view = tree_view;
1782 if (got_path_is_root_dir(path))
1783 return NULL;
1785 /* Walk the path and open corresponding tree objects. */
1786 p = path;
1787 while (*p) {
1788 struct got_tree_entry *te;
1789 struct got_object_id *tree_id;
1790 char *te_name;
1792 while (p[0] == '/')
1793 p++;
1795 /* Ensure the correct subtree entry is selected. */
1796 slash = strchr(p, '/');
1797 if (slash == NULL)
1798 te_name = strdup(p);
1799 else
1800 te_name = strndup(p, slash - p);
1801 if (te_name == NULL) {
1802 err = got_error_from_errno("strndup");
1803 break;
1805 te = got_object_tree_find_entry(s->tree, te_name);
1806 if (te == NULL) {
1807 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1808 free(te_name);
1809 break;
1811 free(te_name);
1812 s->selected_entry = te;
1813 s->selected = got_tree_entry_get_index(te);
1814 if (s->tree != s->root)
1815 s->selected++; /* skip '..' */
1817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1818 /* Jump to this file's entry. */
1819 s->first_displayed_entry = s->selected_entry;
1820 s->selected = 0;
1821 break;
1824 slash = strchr(p, '/');
1825 if (slash)
1826 subpath = strndup(path, slash - path);
1827 else
1828 subpath = strdup(path);
1829 if (subpath == NULL) {
1830 err = got_error_from_errno("strdup");
1831 break;
1834 err = got_object_id_by_path(&tree_id, repo, entry->id,
1835 subpath);
1836 if (err)
1837 break;
1839 err = got_object_open_as_tree(&tree, repo, tree_id);
1840 free(tree_id);
1841 if (err)
1842 break;
1844 err = tree_view_visit_subtree(tree, s);
1845 if (err) {
1846 got_object_tree_close(tree);
1847 break;
1849 if (slash == NULL)
1850 break;
1851 free(subpath);
1852 subpath = NULL;
1853 p = slash;
1856 free(subpath);
1857 return err;
1860 static const struct got_error *
1861 block_signals_used_by_main_thread(void)
1863 sigset_t sigset;
1864 int errcode;
1866 if (sigemptyset(&sigset) == -1)
1867 return got_error_from_errno("sigemptyset");
1869 /* tog handles SIGWINCH and SIGCONT */
1870 if (sigaddset(&sigset, SIGWINCH) == -1)
1871 return got_error_from_errno("sigaddset");
1872 if (sigaddset(&sigset, SIGCONT) == -1)
1873 return got_error_from_errno("sigaddset");
1875 /* ncurses handles SIGTSTP */
1876 if (sigaddset(&sigset, SIGTSTP) == -1)
1877 return got_error_from_errno("sigaddset");
1879 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1880 if (errcode)
1881 return got_error_set_errno(errcode, "pthread_sigmask");
1883 return NULL;
1886 static void *
1887 log_thread(void *arg)
1889 const struct got_error *err = NULL;
1890 int errcode = 0;
1891 struct tog_log_thread_args *a = arg;
1892 int done = 0;
1894 err = block_signals_used_by_main_thread();
1895 if (err)
1896 return (void *)err;
1898 while (!done && !err && !tog_sigpipe_received) {
1899 err = queue_commits(a->graph, a->commits, 1, a->repo,
1900 a->in_repo_path, a->searching, a->search_next_done,
1901 a->regex);
1902 if (err) {
1903 if (err->code != GOT_ERR_ITER_COMPLETED)
1904 return (void *)err;
1905 err = NULL;
1906 done = 1;
1907 } else if (a->commits_needed > 0)
1908 a->commits_needed--;
1910 errcode = pthread_mutex_lock(&tog_mutex);
1911 if (errcode) {
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_lock");
1914 break;
1915 } else if (*a->quit)
1916 done = 1;
1917 else if (*a->first_displayed_entry == NULL) {
1918 *a->first_displayed_entry =
1919 TAILQ_FIRST(&a->commits->head);
1920 *a->selected_entry = *a->first_displayed_entry;
1923 if (done)
1924 a->commits_needed = 0;
1925 else if (a->commits_needed == 0) {
1926 errcode = pthread_cond_wait(&a->need_commits,
1927 &tog_mutex);
1928 if (errcode)
1929 err = got_error_set_errno(errcode,
1930 "pthread_cond_wait");
1933 errcode = pthread_mutex_unlock(&tog_mutex);
1934 if (errcode && err == NULL)
1935 err = got_error_set_errno(errcode,
1936 "pthread_mutex_unlock");
1938 a->log_complete = 1;
1939 return (void *)err;
1942 static const struct got_error *
1943 stop_log_thread(struct tog_log_view_state *s)
1945 const struct got_error *err = NULL;
1946 int errcode;
1948 if (s->thread) {
1949 s->quit = 1;
1950 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1951 if (errcode)
1952 return got_error_set_errno(errcode,
1953 "pthread_cond_signal");
1954 errcode = pthread_mutex_unlock(&tog_mutex);
1955 if (errcode)
1956 return got_error_set_errno(errcode,
1957 "pthread_mutex_unlock");
1958 errcode = pthread_join(s->thread, (void **)&err);
1959 if (errcode)
1960 return got_error_set_errno(errcode, "pthread_join");
1961 errcode = pthread_mutex_lock(&tog_mutex);
1962 if (errcode)
1963 return got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1965 s->thread = NULL;
1968 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1969 if (errcode && err == NULL)
1970 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1972 if (s->thread_args.repo) {
1973 got_repo_close(s->thread_args.repo);
1974 s->thread_args.repo = NULL;
1977 if (s->thread_args.graph) {
1978 got_commit_graph_close(s->thread_args.graph);
1979 s->thread_args.graph = NULL;
1982 return err;
1985 static const struct got_error *
1986 close_log_view(struct tog_view *view)
1988 const struct got_error *err = NULL;
1989 struct tog_log_view_state *s = &view->state.log;
1991 err = stop_log_thread(s);
1992 free_commits(&s->commits);
1993 free(s->in_repo_path);
1994 s->in_repo_path = NULL;
1995 free(s->start_id);
1996 s->start_id = NULL;
1997 return err;
2000 static const struct got_error *
2001 search_start_log_view(struct tog_view *view)
2003 struct tog_log_view_state *s = &view->state.log;
2005 s->matched_entry = NULL;
2006 s->search_entry = NULL;
2007 return NULL;
2010 static const struct got_error *
2011 search_next_log_view(struct tog_view *view)
2013 const struct got_error *err = NULL;
2014 struct tog_log_view_state *s = &view->state.log;
2015 struct commit_queue_entry *entry;
2017 if (!view->searching) {
2018 view->search_next_done = 1;
2019 return NULL;
2022 if (s->search_entry) {
2023 int errcode, ch;
2024 errcode = pthread_mutex_unlock(&tog_mutex);
2025 if (errcode)
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_unlock");
2028 ch = wgetch(view->window);
2029 errcode = pthread_mutex_lock(&tog_mutex);
2030 if (errcode)
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2033 if (ch == KEY_BACKSPACE) {
2034 view->search_next_done = 1;
2035 return NULL;
2037 if (view->searching == TOG_SEARCH_FORWARD)
2038 entry = TAILQ_NEXT(s->search_entry, entry);
2039 else
2040 entry = TAILQ_PREV(s->search_entry,
2041 commit_queue_head, entry);
2042 } else if (s->matched_entry) {
2043 if (view->searching == TOG_SEARCH_FORWARD)
2044 entry = TAILQ_NEXT(s->selected_entry, entry);
2045 else
2046 entry = TAILQ_PREV(s->selected_entry,
2047 commit_queue_head, entry);
2048 } else {
2049 if (view->searching == TOG_SEARCH_FORWARD)
2050 entry = TAILQ_FIRST(&s->commits.head);
2051 else
2052 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2055 while (1) {
2056 int have_match = 0;
2058 if (entry == NULL) {
2059 if (s->thread_args.log_complete ||
2060 view->searching == TOG_SEARCH_BACKWARD) {
2061 view->search_next_done = 1;
2062 return NULL;
2065 * Poke the log thread for more commits and return,
2066 * allowing the main loop to make progress. Search
2067 * will resume at s->search_entry once we come back.
2069 s->thread_args.commits_needed++;
2070 return trigger_log_thread(1,
2071 &s->thread_args.commits_needed,
2072 &s->thread_args.log_complete,
2073 &s->thread_args.need_commits);
2076 err = match_commit(&have_match, entry->id, entry->commit,
2077 &view->regex);
2078 if (err)
2079 break;
2080 if (have_match) {
2081 view->search_next_done = 1;
2082 s->matched_entry = entry;
2083 break;
2086 s->search_entry = entry;
2087 if (view->searching == TOG_SEARCH_FORWARD)
2088 entry = TAILQ_NEXT(entry, entry);
2089 else
2090 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2093 if (s->matched_entry) {
2094 int cur = s->selected_entry->idx;
2095 while (cur < s->matched_entry->idx) {
2096 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2097 if (err)
2098 return err;
2099 cur++;
2101 while (cur > s->matched_entry->idx) {
2102 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2103 if (err)
2104 return err;
2105 cur--;
2109 s->search_entry = NULL;
2111 return NULL;
2114 static const struct got_error *
2115 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2116 struct got_reflist_head *refs, struct got_repository *repo,
2117 const char *head_ref_name, const char *path, int check_disk)
2119 const struct got_error *err = NULL;
2120 struct tog_log_view_state *s = &view->state.log;
2121 struct got_repository *thread_repo = NULL;
2122 struct got_commit_graph *thread_graph = NULL;
2123 int errcode;
2125 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2126 if (err != NULL)
2127 goto done;
2129 /* The commit queue only contains commits being displayed. */
2130 TAILQ_INIT(&s->commits.head);
2131 s->commits.ncommits = 0;
2133 s->refs = refs;
2134 s->repo = repo;
2135 s->head_ref_name = head_ref_name;
2136 s->start_id = got_object_id_dup(start_id);
2137 if (s->start_id == NULL) {
2138 err = got_error_from_errno("got_object_id_dup");
2139 goto done;
2142 SIMPLEQ_INIT(&s->colors);
2143 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2144 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2145 get_color_value("TOG_COLOR_COMMIT"));
2146 if (err)
2147 goto done;
2148 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2149 get_color_value("TOG_COLOR_AUTHOR"));
2150 if (err) {
2151 free_colors(&s->colors);
2152 goto done;
2154 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2155 get_color_value("TOG_COLOR_DATE"));
2156 if (err) {
2157 free_colors(&s->colors);
2158 goto done;
2162 view->show = show_log_view;
2163 view->input = input_log_view;
2164 view->close = close_log_view;
2165 view->search_start = search_start_log_view;
2166 view->search_next = search_next_log_view;
2168 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2169 if (err)
2170 goto done;
2171 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2172 if (err)
2173 goto done;
2174 err = got_commit_graph_iter_start(thread_graph,
2175 s->start_id, s->repo, NULL, NULL);
2176 if (err)
2177 goto done;
2179 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2180 if (errcode) {
2181 err = got_error_set_errno(errcode, "pthread_cond_init");
2182 goto done;
2185 s->thread_args.commits_needed = view->nlines;
2186 s->thread_args.graph = thread_graph;
2187 s->thread_args.commits = &s->commits;
2188 s->thread_args.in_repo_path = s->in_repo_path;
2189 s->thread_args.start_id = s->start_id;
2190 s->thread_args.repo = thread_repo;
2191 s->thread_args.log_complete = 0;
2192 s->thread_args.quit = &s->quit;
2193 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2194 s->thread_args.selected_entry = &s->selected_entry;
2195 s->thread_args.searching = &view->searching;
2196 s->thread_args.search_next_done = &view->search_next_done;
2197 s->thread_args.regex = &view->regex;
2198 done:
2199 if (err)
2200 close_log_view(view);
2201 return err;
2204 static const struct got_error *
2205 show_log_view(struct tog_view *view)
2207 struct tog_log_view_state *s = &view->state.log;
2209 if (s->thread == NULL) {
2210 int errcode = pthread_create(&s->thread, NULL, log_thread,
2211 &s->thread_args);
2212 if (errcode)
2213 return got_error_set_errno(errcode, "pthread_create");
2216 return draw_commits(view, &s->last_displayed_entry,
2217 &s->selected_entry, s->first_displayed_entry,
2218 &s->commits, s->selected, view->nlines, s->refs,
2219 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2222 static const struct got_error *
2223 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2224 struct tog_view **focus_view, struct tog_view *view, int ch)
2226 const struct got_error *err = NULL;
2227 struct tog_log_view_state *s = &view->state.log;
2228 char *parent_path, *in_repo_path = NULL;
2229 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2230 int begin_x = 0;
2231 struct got_object_id *start_id;
2233 switch (ch) {
2234 case 'q':
2235 s->quit = 1;
2236 break;
2237 case 'k':
2238 case KEY_UP:
2239 case '<':
2240 case ',':
2241 if (s->first_displayed_entry == NULL)
2242 break;
2243 if (s->selected > 0)
2244 s->selected--;
2245 else
2246 scroll_up(view, &s->first_displayed_entry, 1,
2247 &s->commits);
2248 break;
2249 case KEY_PPAGE:
2250 case CTRL('b'):
2251 if (s->first_displayed_entry == NULL)
2252 break;
2253 if (TAILQ_FIRST(&s->commits.head) ==
2254 s->first_displayed_entry) {
2255 s->selected = 0;
2256 break;
2258 scroll_up(view, &s->first_displayed_entry,
2259 view->nlines, &s->commits);
2260 break;
2261 case 'j':
2262 case KEY_DOWN:
2263 case '>':
2264 case '.':
2265 if (s->first_displayed_entry == NULL)
2266 break;
2267 if (s->selected < MIN(view->nlines - 2,
2268 s->commits.ncommits - 1)) {
2269 s->selected++;
2270 break;
2272 err = scroll_down(view, &s->first_displayed_entry, 1,
2273 &s->last_displayed_entry, &s->commits,
2274 &s->thread_args.log_complete,
2275 &s->thread_args.commits_needed,
2276 &s->thread_args.need_commits);
2277 break;
2278 case KEY_NPAGE:
2279 case CTRL('f'): {
2280 struct commit_queue_entry *first;
2281 first = s->first_displayed_entry;
2282 if (first == NULL)
2283 break;
2284 err = scroll_down(view, &s->first_displayed_entry,
2285 view->nlines, &s->last_displayed_entry,
2286 &s->commits, &s->thread_args.log_complete,
2287 &s->thread_args.commits_needed,
2288 &s->thread_args.need_commits);
2289 if (err)
2290 break;
2291 if (first == s->first_displayed_entry &&
2292 s->selected < MIN(view->nlines - 2,
2293 s->commits.ncommits - 1)) {
2294 /* can't scroll further down */
2295 s->selected = MIN(view->nlines - 2,
2296 s->commits.ncommits - 1);
2298 err = NULL;
2299 break;
2301 case KEY_RESIZE:
2302 if (s->selected > view->nlines - 2)
2303 s->selected = view->nlines - 2;
2304 if (s->selected > s->commits.ncommits - 1)
2305 s->selected = s->commits.ncommits - 1;
2306 break;
2307 case KEY_ENTER:
2308 case ' ':
2309 case '\r':
2310 if (s->selected_entry == NULL)
2311 break;
2312 if (view_is_parent_view(view))
2313 begin_x = view_split_begin_x(view->begin_x);
2314 err = open_diff_view_for_commit(&diff_view, begin_x,
2315 s->selected_entry->commit, s->selected_entry->id,
2316 view, s->refs, s->repo);
2317 if (err)
2318 break;
2319 if (view_is_parent_view(view)) {
2320 err = view_close_child(view);
2321 if (err)
2322 return err;
2323 err = view_set_child(view, diff_view);
2324 if (err) {
2325 view_close(diff_view);
2326 break;
2328 *focus_view = diff_view;
2329 view->child_focussed = 1;
2330 } else
2331 *new_view = diff_view;
2332 break;
2333 case 't':
2334 if (s->selected_entry == NULL)
2335 break;
2336 if (view_is_parent_view(view))
2337 begin_x = view_split_begin_x(view->begin_x);
2338 err = browse_commit_tree(&tree_view, begin_x,
2339 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2340 if (err)
2341 break;
2342 if (view_is_parent_view(view)) {
2343 err = view_close_child(view);
2344 if (err)
2345 return err;
2346 err = view_set_child(view, tree_view);
2347 if (err) {
2348 view_close(tree_view);
2349 break;
2351 *focus_view = tree_view;
2352 view->child_focussed = 1;
2353 } else
2354 *new_view = tree_view;
2355 break;
2356 case KEY_BACKSPACE:
2357 if (strcmp(s->in_repo_path, "/") == 0)
2358 break;
2359 parent_path = dirname(s->in_repo_path);
2360 if (parent_path && strcmp(parent_path, ".") != 0) {
2361 err = stop_log_thread(s);
2362 if (err)
2363 return err;
2364 lv = view_open(view->nlines, view->ncols,
2365 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2366 if (lv == NULL)
2367 return got_error_from_errno(
2368 "view_open");
2369 err = open_log_view(lv, s->start_id, s->refs,
2370 s->repo, s->head_ref_name, parent_path, 0);
2371 if (err)
2372 return err;;
2373 if (view_is_parent_view(view))
2374 *new_view = lv;
2375 else {
2376 view_set_child(view->parent, lv);
2377 *focus_view = lv;
2379 return NULL;
2381 break;
2382 case CTRL('l'):
2383 err = stop_log_thread(s);
2384 if (err)
2385 return err;
2386 lv = view_open(view->nlines, view->ncols,
2387 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2388 if (lv == NULL)
2389 return got_error_from_errno("view_open");
2390 err = got_repo_match_object_id(&start_id, NULL,
2391 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2392 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2393 if (err) {
2394 view_close(lv);
2395 return err;
2397 in_repo_path = strdup(s->in_repo_path);
2398 if (in_repo_path == NULL) {
2399 free(start_id);
2400 view_close(lv);
2401 return got_error_from_errno("strdup");
2403 got_ref_list_free(s->refs);
2404 err = got_ref_list(s->refs, s->repo, NULL,
2405 got_ref_cmp_by_name, NULL);
2406 if (err) {
2407 free(start_id);
2408 view_close(lv);
2409 return err;
2411 err = open_log_view(lv, start_id, s->refs, s->repo,
2412 s->head_ref_name, in_repo_path, 0);
2413 if (err) {
2414 free(start_id);
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;
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, "c:r:")) != -1) {
2498 switch (ch) {
2499 case 'c':
2500 start_commit = optarg;
2501 break;
2502 case 'r':
2503 repo_path = realpath(optarg, NULL);
2504 if (repo_path == NULL)
2505 return got_error_from_errno2("realpath",
2506 optarg);
2507 break;
2508 default:
2509 usage_log();
2510 /* NOTREACHED */
2514 argc -= optind;
2515 argv += optind;
2517 cwd = getcwd(NULL, 0);
2518 if (cwd == NULL) {
2519 error = got_error_from_errno("getcwd");
2520 goto done;
2522 error = got_worktree_open(&worktree, cwd);
2523 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2524 goto done;
2525 error = NULL;
2527 if (argc == 0) {
2528 path = strdup("");
2529 if (path == NULL) {
2530 error = got_error_from_errno("strdup");
2531 goto done;
2533 } else if (argc == 1) {
2534 if (worktree) {
2535 error = got_worktree_resolve_path(&path, worktree,
2536 argv[0]);
2537 if (error)
2538 goto done;
2539 } else {
2540 path = strdup(argv[0]);
2541 if (path == NULL) {
2542 error = got_error_from_errno("strdup");
2543 goto done;
2546 } else
2547 usage_log();
2549 if (repo_path == NULL) {
2550 if (worktree)
2551 repo_path = strdup(
2552 got_worktree_get_repo_path(worktree));
2553 else
2554 repo_path = strdup(cwd);
2556 if (repo_path == NULL) {
2557 error = got_error_from_errno("strdup");
2558 goto done;
2561 init_curses();
2563 error = got_repo_open(&repo, repo_path, NULL);
2564 if (error != NULL)
2565 goto done;
2567 error = apply_unveil(got_repo_get_path(repo),
2568 worktree ? got_worktree_get_root_path(worktree) : NULL);
2569 if (error)
2570 goto done;
2572 if (start_commit == NULL)
2573 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2574 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2575 GOT_OBJ_TYPE_COMMIT, 1, repo);
2576 else
2577 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2578 GOT_OBJ_TYPE_COMMIT, 1, repo);
2579 if (error != NULL)
2580 goto done;
2582 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2583 if (error)
2584 goto done;
2586 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2587 if (view == NULL) {
2588 error = got_error_from_errno("view_open");
2589 goto done;
2591 if (worktree) {
2592 head_ref_name = strdup(
2593 got_worktree_get_head_ref_name(worktree));
2594 if (head_ref_name == NULL) {
2595 error = got_error_from_errno("strdup");
2596 goto done;
2599 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2600 path, 1);
2601 if (error)
2602 goto done;
2603 if (worktree) {
2604 /* Release work tree lock. */
2605 got_worktree_close(worktree);
2606 worktree = NULL;
2608 error = view_loop(view);
2609 done:
2610 free(repo_path);
2611 free(cwd);
2612 free(path);
2613 free(start_id);
2614 free(head_ref_name);
2615 if (repo)
2616 got_repo_close(repo);
2617 if (worktree)
2618 got_worktree_close(worktree);
2619 got_ref_list_free(&refs);
2620 return error;
2623 __dead static void
2624 usage_diff(void)
2626 endwin();
2627 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2628 getprogname());
2629 exit(1);
2632 static char *
2633 parse_next_line(FILE *f, size_t *len)
2635 char *line;
2636 size_t linelen;
2637 size_t lineno;
2638 const char delim[3] = { '\0', '\0', '\0'};
2640 line = fparseln(f, &linelen, &lineno, delim, 0);
2641 if (len)
2642 *len = linelen;
2643 return line;
2646 static int
2647 match_line(const char *line, regex_t *regex)
2649 regmatch_t regmatch;
2651 return regexec(regex, line, 1, &regmatch, 0) == 0;
2654 struct tog_color *
2655 match_color(struct tog_colors *colors, const char *line)
2657 struct tog_color *tc = NULL;
2659 SIMPLEQ_FOREACH(tc, colors, entry) {
2660 if (match_line(line, &tc->regex))
2661 return tc;
2664 return NULL;
2667 static const struct got_error *
2668 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2669 int *last_displayed_line, int *eof, int max_lines, char *header,
2670 struct tog_colors *colors)
2672 const struct got_error *err;
2673 int nlines = 0, nprinted = 0;
2674 char *line;
2675 struct tog_color *tc;
2676 size_t len;
2677 wchar_t *wline;
2678 int width;
2680 rewind(f);
2681 werase(view->window);
2683 if (header) {
2684 err = format_line(&wline, &width, header, view->ncols, 0);
2685 if (err) {
2686 return err;
2689 if (view_needs_focus_indication(view))
2690 wstandout(view->window);
2691 waddwstr(view->window, wline);
2692 if (view_needs_focus_indication(view))
2693 wstandend(view->window);
2694 if (width <= view->ncols - 1)
2695 waddch(view->window, '\n');
2697 if (max_lines <= 1)
2698 return NULL;
2699 max_lines--;
2702 *eof = 0;
2703 while (nprinted < max_lines) {
2704 line = parse_next_line(f, &len);
2705 if (line == NULL) {
2706 *eof = 1;
2707 break;
2709 if (++nlines < *first_displayed_line) {
2710 free(line);
2711 continue;
2714 err = format_line(&wline, &width, line, view->ncols, 0);
2715 if (err) {
2716 free(line);
2717 return err;
2720 tc = match_color(colors, line);
2721 if (tc)
2722 wattr_on(view->window,
2723 COLOR_PAIR(tc->colorpair), NULL);
2724 waddwstr(view->window, wline);
2725 if (tc)
2726 wattr_off(view->window,
2727 COLOR_PAIR(tc->colorpair), NULL);
2728 if (width <= view->ncols - 1)
2729 waddch(view->window, '\n');
2730 if (++nprinted == 1)
2731 *first_displayed_line = nlines;
2732 free(line);
2733 free(wline);
2734 wline = NULL;
2736 *last_displayed_line = nlines;
2738 view_vborder(view);
2740 if (*eof) {
2741 while (nprinted < view->nlines) {
2742 waddch(view->window, '\n');
2743 nprinted++;
2746 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2747 if (err) {
2748 return err;
2751 wstandout(view->window);
2752 waddwstr(view->window, wline);
2753 wstandend(view->window);
2756 return NULL;
2759 static char *
2760 get_datestr(time_t *time, char *datebuf)
2762 struct tm mytm, *tm;
2763 char *p, *s;
2765 tm = gmtime_r(time, &mytm);
2766 if (tm == NULL)
2767 return NULL;
2768 s = asctime_r(tm, datebuf);
2769 if (s == NULL)
2770 return NULL;
2771 p = strchr(s, '\n');
2772 if (p)
2773 *p = '\0';
2774 return s;
2777 static const struct got_error *
2778 write_commit_info(struct got_object_id *commit_id,
2779 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2781 const struct got_error *err = NULL;
2782 char datebuf[26], *datestr;
2783 struct got_commit_object *commit;
2784 char *id_str = NULL, *logmsg = NULL;
2785 time_t committer_time;
2786 const char *author, *committer;
2787 char *refs_str = NULL;
2789 if (refs) {
2790 err = build_refs_str(&refs_str, refs, commit_id, repo);
2791 if (err)
2792 return err;
2795 err = got_object_open_as_commit(&commit, repo, commit_id);
2796 if (err)
2797 return err;
2799 err = got_object_id_str(&id_str, commit_id);
2800 if (err) {
2801 err = got_error_from_errno("got_object_id_str");
2802 goto done;
2805 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2806 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2807 err = got_error_from_errno("fprintf");
2808 goto done;
2810 if (fprintf(outfile, "from: %s\n",
2811 got_object_commit_get_author(commit)) < 0) {
2812 err = got_error_from_errno("fprintf");
2813 goto done;
2815 committer_time = got_object_commit_get_committer_time(commit);
2816 datestr = get_datestr(&committer_time, datebuf);
2817 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2818 err = got_error_from_errno("fprintf");
2819 goto done;
2821 author = got_object_commit_get_author(commit);
2822 committer = got_object_commit_get_committer(commit);
2823 if (strcmp(author, committer) != 0 &&
2824 fprintf(outfile, "via: %s\n", committer) < 0) {
2825 err = got_error_from_errno("fprintf");
2826 goto done;
2828 err = got_object_commit_get_logmsg(&logmsg, commit);
2829 if (err)
2830 goto done;
2831 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2832 err = got_error_from_errno("fprintf");
2833 goto done;
2835 done:
2836 free(id_str);
2837 free(logmsg);
2838 free(refs_str);
2839 got_object_commit_close(commit);
2840 return err;
2843 static const struct got_error *
2844 create_diff(struct tog_diff_view_state *s)
2846 const struct got_error *err = NULL;
2847 FILE *f = NULL;
2848 int obj_type;
2850 f = got_opentemp();
2851 if (f == NULL) {
2852 err = got_error_from_errno("got_opentemp");
2853 goto done;
2855 if (s->f && fclose(s->f) != 0) {
2856 err = got_error_from_errno("fclose");
2857 goto done;
2859 s->f = f;
2861 if (s->id1)
2862 err = got_object_get_type(&obj_type, s->repo, s->id1);
2863 else
2864 err = got_object_get_type(&obj_type, s->repo, s->id2);
2865 if (err)
2866 goto done;
2868 switch (obj_type) {
2869 case GOT_OBJ_TYPE_BLOB:
2870 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2871 s->diff_context, 0, s->repo, f);
2872 break;
2873 case GOT_OBJ_TYPE_TREE:
2874 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2875 s->diff_context, 0, s->repo, f);
2876 break;
2877 case GOT_OBJ_TYPE_COMMIT: {
2878 const struct got_object_id_queue *parent_ids;
2879 struct got_object_qid *pid;
2880 struct got_commit_object *commit2;
2882 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2883 if (err)
2884 break;
2885 /* Show commit info if we're diffing to a parent/root commit. */
2886 if (s->id1 == NULL)
2887 write_commit_info(s->id2, s->refs, s->repo, f);
2888 else {
2889 parent_ids = got_object_commit_get_parent_ids(commit2);
2890 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2891 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2892 write_commit_info(s->id2, s->refs,
2893 s->repo, f);
2894 break;
2898 got_object_commit_close(commit2);
2900 err = got_diff_objects_as_commits(s->id1, s->id2,
2901 s->diff_context, 0, s->repo, f);
2902 break;
2904 default:
2905 err = got_error(GOT_ERR_OBJ_TYPE);
2906 break;
2908 done:
2909 if (f && fflush(f) != 0 && err == NULL)
2910 err = got_error_from_errno("fflush");
2911 return err;
2914 static void
2915 diff_view_indicate_progress(struct tog_view *view)
2917 mvwaddstr(view->window, 0, 0, "diffing...");
2918 update_panels();
2919 doupdate();
2922 static const struct got_error *
2923 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2924 struct got_object_id *id2, struct tog_view *log_view,
2925 struct got_reflist_head *refs, struct got_repository *repo)
2927 const struct got_error *err;
2929 if (id1 != NULL && id2 != NULL) {
2930 int type1, type2;
2931 err = got_object_get_type(&type1, repo, id1);
2932 if (err)
2933 return err;
2934 err = got_object_get_type(&type2, repo, id2);
2935 if (err)
2936 return err;
2938 if (type1 != type2)
2939 return got_error(GOT_ERR_OBJ_TYPE);
2942 if (id1) {
2943 view->state.diff.id1 = got_object_id_dup(id1);
2944 if (view->state.diff.id1 == NULL)
2945 return got_error_from_errno("got_object_id_dup");
2946 } else
2947 view->state.diff.id1 = NULL;
2949 view->state.diff.id2 = got_object_id_dup(id2);
2950 if (view->state.diff.id2 == NULL) {
2951 free(view->state.diff.id1);
2952 view->state.diff.id1 = NULL;
2953 return got_error_from_errno("got_object_id_dup");
2955 view->state.diff.f = NULL;
2956 view->state.diff.first_displayed_line = 1;
2957 view->state.diff.last_displayed_line = view->nlines;
2958 view->state.diff.diff_context = 3;
2959 view->state.diff.log_view = log_view;
2960 view->state.diff.repo = repo;
2961 view->state.diff.refs = refs;
2962 SIMPLEQ_INIT(&view->state.diff.colors);
2964 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2965 err = add_color(&view->state.diff.colors,
2966 "^-", TOG_COLOR_DIFF_MINUS,
2967 get_color_value("TOG_COLOR_DIFF_MINUS"));
2968 if (err)
2969 return err;
2970 err = add_color(&view->state.diff.colors, "^\\+",
2971 TOG_COLOR_DIFF_PLUS,
2972 get_color_value("TOG_COLOR_DIFF_PLUS"));
2973 if (err) {
2974 free_colors(&view->state.diff.colors);
2975 return err;
2977 err = add_color(&view->state.diff.colors,
2978 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2979 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2980 if (err) {
2981 free_colors(&view->state.diff.colors);
2982 return err;
2985 err = add_color(&view->state.diff.colors,
2986 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2987 get_color_value("TOG_COLOR_DIFF_META"));
2988 if (err) {
2989 free_colors(&view->state.diff.colors);
2990 return err;
2993 err = add_color(&view->state.diff.colors,
2994 "^(from|via): ", TOG_COLOR_AUTHOR,
2995 get_color_value("TOG_COLOR_AUTHOR"));
2996 if (err) {
2997 free_colors(&view->state.diff.colors);
2998 return err;
3001 err = add_color(&view->state.diff.colors,
3002 "^date: ", TOG_COLOR_DATE,
3003 get_color_value("TOG_COLOR_DATE"));
3004 if (err) {
3005 free_colors(&view->state.diff.colors);
3006 return err;
3010 if (log_view && view_is_splitscreen(view))
3011 show_log_view(log_view); /* draw vborder */
3012 diff_view_indicate_progress(view);
3014 err = create_diff(&view->state.diff);
3015 if (err) {
3016 free(view->state.diff.id1);
3017 view->state.diff.id1 = NULL;
3018 free(view->state.diff.id2);
3019 view->state.diff.id2 = NULL;
3020 return err;
3023 view->show = show_diff_view;
3024 view->input = input_diff_view;
3025 view->close = close_diff_view;
3027 return NULL;
3030 static const struct got_error *
3031 close_diff_view(struct tog_view *view)
3033 const struct got_error *err = NULL;
3035 free(view->state.diff.id1);
3036 view->state.diff.id1 = NULL;
3037 free(view->state.diff.id2);
3038 view->state.diff.id2 = NULL;
3039 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3040 err = got_error_from_errno("fclose");
3041 free_colors(&view->state.diff.colors);
3042 return err;
3045 static const struct got_error *
3046 show_diff_view(struct tog_view *view)
3048 const struct got_error *err;
3049 struct tog_diff_view_state *s = &view->state.diff;
3050 char *id_str1 = NULL, *id_str2, *header;
3052 if (s->id1) {
3053 err = got_object_id_str(&id_str1, s->id1);
3054 if (err)
3055 return err;
3057 err = got_object_id_str(&id_str2, s->id2);
3058 if (err)
3059 return err;
3061 if (asprintf(&header, "diff %s %s",
3062 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3063 err = got_error_from_errno("asprintf");
3064 free(id_str1);
3065 free(id_str2);
3066 return err;
3068 free(id_str1);
3069 free(id_str2);
3071 return draw_file(view, s->f, &s->first_displayed_line,
3072 &s->last_displayed_line, &s->eof, view->nlines,
3073 header, &s->colors);
3076 static const struct got_error *
3077 set_selected_commit(struct tog_diff_view_state *s,
3078 struct commit_queue_entry *entry)
3080 const struct got_error *err;
3081 const struct got_object_id_queue *parent_ids;
3082 struct got_commit_object *selected_commit;
3083 struct got_object_qid *pid;
3085 free(s->id2);
3086 s->id2 = got_object_id_dup(entry->id);
3087 if (s->id2 == NULL)
3088 return got_error_from_errno("got_object_id_dup");
3090 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3091 if (err)
3092 return err;
3093 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3094 free(s->id1);
3095 pid = SIMPLEQ_FIRST(parent_ids);
3096 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3097 got_object_commit_close(selected_commit);
3098 return NULL;
3101 static const struct got_error *
3102 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3103 struct tog_view **focus_view, struct tog_view *view, int ch)
3105 const struct got_error *err = NULL;
3106 struct tog_diff_view_state *s = &view->state.diff;
3107 struct tog_log_view_state *ls;
3108 struct commit_queue_entry *entry;
3109 int i;
3111 switch (ch) {
3112 case 'k':
3113 case KEY_UP:
3114 if (s->first_displayed_line > 1)
3115 s->first_displayed_line--;
3116 break;
3117 case KEY_PPAGE:
3118 case CTRL('b'):
3119 if (s->first_displayed_line == 1)
3120 break;
3121 i = 0;
3122 while (i++ < view->nlines - 1 &&
3123 s->first_displayed_line > 1)
3124 s->first_displayed_line--;
3125 break;
3126 case 'j':
3127 case KEY_DOWN:
3128 if (!s->eof)
3129 s->first_displayed_line++;
3130 break;
3131 case KEY_NPAGE:
3132 case CTRL('f'):
3133 case ' ':
3134 if (s->eof)
3135 break;
3136 i = 0;
3137 while (!s->eof && i++ < view->nlines - 1) {
3138 char *line;
3139 line = parse_next_line(s->f, NULL);
3140 s->first_displayed_line++;
3141 if (line == NULL)
3142 break;
3144 break;
3145 case '[':
3146 if (s->diff_context > 0) {
3147 s->diff_context--;
3148 diff_view_indicate_progress(view);
3149 err = create_diff(s);
3151 break;
3152 case ']':
3153 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3154 s->diff_context++;
3155 diff_view_indicate_progress(view);
3156 err = create_diff(s);
3158 break;
3159 case '<':
3160 case ',':
3161 if (s->log_view == NULL)
3162 break;
3163 ls = &s->log_view->state.log;
3164 entry = TAILQ_PREV(ls->selected_entry,
3165 commit_queue_head, entry);
3166 if (entry == NULL)
3167 break;
3169 err = input_log_view(NULL, NULL, NULL, s->log_view,
3170 KEY_UP);
3171 if (err)
3172 break;
3174 err = set_selected_commit(s, entry);
3175 if (err)
3176 break;
3178 s->first_displayed_line = 1;
3179 s->last_displayed_line = view->nlines;
3181 diff_view_indicate_progress(view);
3182 err = create_diff(s);
3183 break;
3184 case '>':
3185 case '.':
3186 if (s->log_view == NULL)
3187 break;
3188 ls = &s->log_view->state.log;
3190 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3191 ls->thread_args.commits_needed++;
3193 /* Display "loading..." in log view. */
3194 show_log_view(s->log_view);
3195 update_panels();
3196 doupdate();
3198 err = trigger_log_thread(1 /* load_all */,
3199 &ls->thread_args.commits_needed,
3200 &ls->thread_args.log_complete,
3201 &ls->thread_args.need_commits);
3202 if (err)
3203 break;
3205 err = input_log_view(NULL, NULL, NULL, s->log_view,
3206 KEY_DOWN);
3207 if (err)
3208 break;
3210 entry = TAILQ_NEXT(ls->selected_entry, entry);
3211 if (entry == NULL)
3212 break;
3214 err = set_selected_commit(s, entry);
3215 if (err)
3216 break;
3218 s->first_displayed_line = 1;
3219 s->last_displayed_line = view->nlines;
3221 diff_view_indicate_progress(view);
3222 err = create_diff(s);
3223 break;
3224 default:
3225 break;
3228 return err;
3231 static const struct got_error *
3232 cmd_diff(int argc, char *argv[])
3234 const struct got_error *error = NULL;
3235 struct got_repository *repo = NULL;
3236 struct got_reflist_head refs;
3237 struct got_object_id *id1 = NULL, *id2 = NULL;
3238 char *repo_path = NULL;
3239 char *id_str1 = NULL, *id_str2 = NULL;
3240 int ch;
3241 struct tog_view *view;
3243 SIMPLEQ_INIT(&refs);
3245 #ifndef PROFILE
3246 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3247 NULL) == -1)
3248 err(1, "pledge");
3249 #endif
3251 while ((ch = getopt(argc, argv, "")) != -1) {
3252 switch (ch) {
3253 default:
3254 usage_diff();
3255 /* NOTREACHED */
3259 argc -= optind;
3260 argv += optind;
3262 if (argc == 0) {
3263 usage_diff(); /* TODO show local worktree changes */
3264 } else if (argc == 2) {
3265 repo_path = getcwd(NULL, 0);
3266 if (repo_path == NULL)
3267 return got_error_from_errno("getcwd");
3268 id_str1 = argv[0];
3269 id_str2 = argv[1];
3270 } else if (argc == 3) {
3271 repo_path = realpath(argv[0], NULL);
3272 if (repo_path == NULL)
3273 return got_error_from_errno2("realpath", argv[0]);
3274 id_str1 = argv[1];
3275 id_str2 = argv[2];
3276 } else
3277 usage_diff();
3279 init_curses();
3281 error = got_repo_open(&repo, repo_path, NULL);
3282 if (error)
3283 goto done;
3285 error = apply_unveil(got_repo_get_path(repo), NULL);
3286 if (error)
3287 goto done;
3289 error = got_repo_match_object_id_prefix(&id1, id_str1,
3290 GOT_OBJ_TYPE_ANY, repo);
3291 if (error)
3292 goto done;
3294 error = got_repo_match_object_id_prefix(&id2, id_str2,
3295 GOT_OBJ_TYPE_ANY, repo);
3296 if (error)
3297 goto done;
3299 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3300 if (error)
3301 goto done;
3303 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3304 if (view == NULL) {
3305 error = got_error_from_errno("view_open");
3306 goto done;
3308 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3309 if (error)
3310 goto done;
3311 error = view_loop(view);
3312 done:
3313 free(repo_path);
3314 if (repo)
3315 got_repo_close(repo);
3316 got_ref_list_free(&refs);
3317 return error;
3320 __dead static void
3321 usage_blame(void)
3323 endwin();
3324 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3325 getprogname());
3326 exit(1);
3329 struct tog_blame_line {
3330 int annotated;
3331 struct got_object_id *id;
3334 static const struct got_error *
3335 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3336 const char *path, struct tog_blame_line *lines, int nlines,
3337 int blame_complete, int selected_line, int *first_displayed_line,
3338 int *last_displayed_line, int *eof, int max_lines,
3339 struct tog_colors *colors)
3341 const struct got_error *err;
3342 int lineno = 0, nprinted = 0;
3343 char *line;
3344 size_t len;
3345 wchar_t *wline;
3346 int width;
3347 struct tog_blame_line *blame_line;
3348 struct got_object_id *prev_id = NULL;
3349 char *id_str;
3350 struct tog_color *tc;
3352 err = got_object_id_str(&id_str, id);
3353 if (err)
3354 return err;
3356 rewind(f);
3357 werase(view->window);
3359 if (asprintf(&line, "commit %s", id_str) == -1) {
3360 err = got_error_from_errno("asprintf");
3361 free(id_str);
3362 return err;
3365 err = format_line(&wline, &width, line, view->ncols, 0);
3366 free(line);
3367 line = NULL;
3368 if (err)
3369 return err;
3370 if (view_needs_focus_indication(view))
3371 wstandout(view->window);
3372 tc = get_color(colors, TOG_COLOR_COMMIT);
3373 if (tc)
3374 wattr_on(view->window,
3375 COLOR_PAIR(tc->colorpair), NULL);
3376 waddwstr(view->window, wline);
3377 if (tc)
3378 wattr_off(view->window,
3379 COLOR_PAIR(tc->colorpair), NULL);
3380 if (view_needs_focus_indication(view))
3381 wstandend(view->window);
3382 free(wline);
3383 wline = NULL;
3384 if (width < view->ncols - 1)
3385 waddch(view->window, '\n');
3387 if (asprintf(&line, "[%d/%d] %s%s",
3388 *first_displayed_line - 1 + selected_line, nlines,
3389 blame_complete ? "" : "annotating... ", path) == -1) {
3390 free(id_str);
3391 return got_error_from_errno("asprintf");
3393 free(id_str);
3394 err = format_line(&wline, &width, line, view->ncols, 0);
3395 free(line);
3396 line = NULL;
3397 if (err)
3398 return err;
3399 waddwstr(view->window, wline);
3400 free(wline);
3401 wline = NULL;
3402 if (width < view->ncols - 1)
3403 waddch(view->window, '\n');
3405 *eof = 0;
3406 while (nprinted < max_lines - 2) {
3407 line = parse_next_line(f, &len);
3408 if (line == NULL) {
3409 *eof = 1;
3410 break;
3412 if (++lineno < *first_displayed_line) {
3413 free(line);
3414 continue;
3417 if (view->ncols <= 9) {
3418 width = 9;
3419 wline = wcsdup(L"");
3420 if (wline == NULL)
3421 err = got_error_from_errno("wcsdup");
3422 } else {
3423 err = format_line(&wline, &width, line,
3424 view->ncols - 9, 9);
3425 width += 9;
3427 if (err) {
3428 free(line);
3429 return err;
3432 if (view->focussed && nprinted == selected_line - 1)
3433 wstandout(view->window);
3435 if (nlines > 0) {
3436 blame_line = &lines[lineno - 1];
3437 if (blame_line->annotated && prev_id &&
3438 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3439 !(view->focussed &&
3440 nprinted == selected_line - 1)) {
3441 waddstr(view->window, " ");
3442 } else if (blame_line->annotated) {
3443 char *id_str;
3444 err = got_object_id_str(&id_str, blame_line->id);
3445 if (err) {
3446 free(line);
3447 free(wline);
3448 return err;
3450 tc = get_color(colors, TOG_COLOR_COMMIT);
3451 if (tc)
3452 wattr_on(view->window,
3453 COLOR_PAIR(tc->colorpair), NULL);
3454 wprintw(view->window, "%.8s", id_str);
3455 if (tc)
3456 wattr_off(view->window,
3457 COLOR_PAIR(tc->colorpair), NULL);
3458 free(id_str);
3459 prev_id = blame_line->id;
3460 } else {
3461 waddstr(view->window, "........");
3462 prev_id = NULL;
3464 } else {
3465 waddstr(view->window, "........");
3466 prev_id = NULL;
3469 if (view->focussed && nprinted == selected_line - 1)
3470 wstandend(view->window);
3471 waddstr(view->window, " ");
3473 waddwstr(view->window, wline);
3474 if (width <= view->ncols - 1)
3475 waddch(view->window, '\n');
3476 if (++nprinted == 1)
3477 *first_displayed_line = lineno;
3478 free(line);
3479 free(wline);
3480 wline = NULL;
3482 *last_displayed_line = lineno;
3484 view_vborder(view);
3486 return NULL;
3489 static const struct got_error *
3490 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3492 const struct got_error *err = NULL;
3493 struct tog_blame_cb_args *a = arg;
3494 struct tog_blame_line *line;
3495 int errcode;
3497 if (nlines != a->nlines ||
3498 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3499 return got_error(GOT_ERR_RANGE);
3501 errcode = pthread_mutex_lock(&tog_mutex);
3502 if (errcode)
3503 return got_error_set_errno(errcode, "pthread_mutex_lock");
3505 if (*a->quit) { /* user has quit the blame view */
3506 err = got_error(GOT_ERR_ITER_COMPLETED);
3507 goto done;
3510 if (lineno == -1)
3511 goto done; /* no change in this commit */
3513 line = &a->lines[lineno - 1];
3514 if (line->annotated)
3515 goto done;
3517 line->id = got_object_id_dup(id);
3518 if (line->id == NULL) {
3519 err = got_error_from_errno("got_object_id_dup");
3520 goto done;
3522 line->annotated = 1;
3523 done:
3524 errcode = pthread_mutex_unlock(&tog_mutex);
3525 if (errcode)
3526 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3527 return err;
3530 static void *
3531 blame_thread(void *arg)
3533 const struct got_error *err;
3534 struct tog_blame_thread_args *ta = arg;
3535 struct tog_blame_cb_args *a = ta->cb_args;
3536 int errcode;
3538 err = block_signals_used_by_main_thread();
3539 if (err)
3540 return (void *)err;
3542 err = got_blame(ta->path, a->commit_id, ta->repo,
3543 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3544 if (err && err->code == GOT_ERR_CANCELLED)
3545 err = NULL;
3547 errcode = pthread_mutex_lock(&tog_mutex);
3548 if (errcode)
3549 return (void *)got_error_set_errno(errcode,
3550 "pthread_mutex_lock");
3552 got_repo_close(ta->repo);
3553 ta->repo = NULL;
3554 *ta->complete = 1;
3556 errcode = pthread_mutex_unlock(&tog_mutex);
3557 if (errcode && err == NULL)
3558 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3560 return (void *)err;
3563 static struct got_object_id *
3564 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3565 int first_displayed_line, int selected_line)
3567 struct tog_blame_line *line;
3569 if (nlines <= 0)
3570 return NULL;
3572 line = &lines[first_displayed_line - 1 + selected_line - 1];
3573 if (!line->annotated)
3574 return NULL;
3576 return line->id;
3579 static const struct got_error *
3580 stop_blame(struct tog_blame *blame)
3582 const struct got_error *err = NULL;
3583 int i;
3585 if (blame->thread) {
3586 int errcode;
3587 errcode = pthread_mutex_unlock(&tog_mutex);
3588 if (errcode)
3589 return got_error_set_errno(errcode,
3590 "pthread_mutex_unlock");
3591 errcode = pthread_join(blame->thread, (void **)&err);
3592 if (errcode)
3593 return got_error_set_errno(errcode, "pthread_join");
3594 errcode = pthread_mutex_lock(&tog_mutex);
3595 if (errcode)
3596 return got_error_set_errno(errcode,
3597 "pthread_mutex_lock");
3598 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3599 err = NULL;
3600 blame->thread = NULL;
3602 if (blame->thread_args.repo) {
3603 got_repo_close(blame->thread_args.repo);
3604 blame->thread_args.repo = NULL;
3606 if (blame->f) {
3607 if (fclose(blame->f) != 0 && err == NULL)
3608 err = got_error_from_errno("fclose");
3609 blame->f = NULL;
3611 if (blame->lines) {
3612 for (i = 0; i < blame->nlines; i++)
3613 free(blame->lines[i].id);
3614 free(blame->lines);
3615 blame->lines = NULL;
3617 free(blame->cb_args.commit_id);
3618 blame->cb_args.commit_id = NULL;
3620 return err;
3623 static const struct got_error *
3624 cancel_blame_view(void *arg)
3626 const struct got_error *err = NULL;
3627 int *done = arg;
3628 int errcode;
3630 errcode = pthread_mutex_lock(&tog_mutex);
3631 if (errcode)
3632 return got_error_set_errno(errcode,
3633 "pthread_mutex_unlock");
3635 if (*done)
3636 err = got_error(GOT_ERR_CANCELLED);
3638 errcode = pthread_mutex_unlock(&tog_mutex);
3639 if (errcode)
3640 return got_error_set_errno(errcode,
3641 "pthread_mutex_lock");
3643 return err;
3646 static const struct got_error *
3647 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3648 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3649 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3650 struct got_repository *repo)
3652 const struct got_error *err = NULL;
3653 struct got_blob_object *blob = NULL;
3654 struct got_repository *thread_repo = NULL;
3655 struct got_object_id *obj_id = NULL;
3656 int obj_type;
3658 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3659 if (err)
3660 return err;
3661 if (obj_id == NULL)
3662 return got_error(GOT_ERR_NO_OBJ);
3664 err = got_object_get_type(&obj_type, repo, obj_id);
3665 if (err)
3666 goto done;
3668 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3669 err = got_error(GOT_ERR_OBJ_TYPE);
3670 goto done;
3673 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3674 if (err)
3675 goto done;
3676 blame->f = got_opentemp();
3677 if (blame->f == NULL) {
3678 err = got_error_from_errno("got_opentemp");
3679 goto done;
3681 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3682 &blame->line_offsets, blame->f, blob);
3683 if (err || blame->nlines == 0)
3684 goto done;
3686 /* Don't include \n at EOF in the blame line count. */
3687 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3688 blame->nlines--;
3690 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3691 if (blame->lines == NULL) {
3692 err = got_error_from_errno("calloc");
3693 goto done;
3696 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3697 if (err)
3698 goto done;
3700 blame->cb_args.view = view;
3701 blame->cb_args.lines = blame->lines;
3702 blame->cb_args.nlines = blame->nlines;
3703 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3704 if (blame->cb_args.commit_id == NULL) {
3705 err = got_error_from_errno("got_object_id_dup");
3706 goto done;
3708 blame->cb_args.quit = done;
3710 blame->thread_args.path = path;
3711 blame->thread_args.repo = thread_repo;
3712 blame->thread_args.cb_args = &blame->cb_args;
3713 blame->thread_args.complete = blame_complete;
3714 blame->thread_args.cancel_cb = cancel_blame_view;
3715 blame->thread_args.cancel_arg = done;
3716 *blame_complete = 0;
3718 done:
3719 if (blob)
3720 got_object_blob_close(blob);
3721 free(obj_id);
3722 if (err)
3723 stop_blame(blame);
3724 return err;
3727 static const struct got_error *
3728 open_blame_view(struct tog_view *view, char *path,
3729 struct got_object_id *commit_id, struct got_reflist_head *refs,
3730 struct got_repository *repo)
3732 const struct got_error *err = NULL;
3733 struct tog_blame_view_state *s = &view->state.blame;
3735 SIMPLEQ_INIT(&s->blamed_commits);
3737 s->path = strdup(path);
3738 if (s->path == NULL)
3739 return got_error_from_errno("strdup");
3741 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3742 if (err) {
3743 free(s->path);
3744 return err;
3747 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3748 s->first_displayed_line = 1;
3749 s->last_displayed_line = view->nlines;
3750 s->selected_line = 1;
3751 s->blame_complete = 0;
3752 s->repo = repo;
3753 s->refs = refs;
3754 s->commit_id = commit_id;
3755 memset(&s->blame, 0, sizeof(s->blame));
3757 SIMPLEQ_INIT(&s->colors);
3758 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3759 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3760 get_color_value("TOG_COLOR_COMMIT"));
3761 if (err)
3762 return err;
3765 view->show = show_blame_view;
3766 view->input = input_blame_view;
3767 view->close = close_blame_view;
3768 view->search_start = search_start_blame_view;
3769 view->search_next = search_next_blame_view;
3771 return run_blame(&s->blame, view, &s->blame_complete,
3772 &s->first_displayed_line, &s->last_displayed_line,
3773 &s->selected_line, &s->done, &s->eof, s->path,
3774 s->blamed_commit->id, s->repo);
3777 static const struct got_error *
3778 close_blame_view(struct tog_view *view)
3780 const struct got_error *err = NULL;
3781 struct tog_blame_view_state *s = &view->state.blame;
3783 if (s->blame.thread)
3784 err = stop_blame(&s->blame);
3786 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3787 struct got_object_qid *blamed_commit;
3788 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3789 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3790 got_object_qid_free(blamed_commit);
3793 free(s->path);
3794 free_colors(&s->colors);
3796 return err;
3799 static const struct got_error *
3800 search_start_blame_view(struct tog_view *view)
3802 struct tog_blame_view_state *s = &view->state.blame;
3804 s->matched_line = 0;
3805 return NULL;
3808 static const struct got_error *
3809 search_next_blame_view(struct tog_view *view)
3811 struct tog_blame_view_state *s = &view->state.blame;
3812 int lineno;
3814 if (!view->searching) {
3815 view->search_next_done = 1;
3816 return NULL;
3819 if (s->matched_line) {
3820 if (view->searching == TOG_SEARCH_FORWARD)
3821 lineno = s->matched_line + 1;
3822 else
3823 lineno = s->matched_line - 1;
3824 } else {
3825 if (view->searching == TOG_SEARCH_FORWARD)
3826 lineno = 1;
3827 else
3828 lineno = s->blame.nlines;
3831 while (1) {
3832 char *line = NULL;
3833 off_t offset;
3834 size_t len;
3836 if (lineno <= 0 || lineno > s->blame.nlines) {
3837 if (s->matched_line == 0) {
3838 view->search_next_done = 1;
3839 free(line);
3840 break;
3843 if (view->searching == TOG_SEARCH_FORWARD)
3844 lineno = 1;
3845 else
3846 lineno = s->blame.nlines;
3849 offset = s->blame.line_offsets[lineno - 1];
3850 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3851 free(line);
3852 return got_error_from_errno("fseeko");
3854 free(line);
3855 line = parse_next_line(s->blame.f, &len);
3856 if (line && match_line(line, &view->regex)) {
3857 view->search_next_done = 1;
3858 s->matched_line = lineno;
3859 free(line);
3860 break;
3862 free(line);
3863 if (view->searching == TOG_SEARCH_FORWARD)
3864 lineno++;
3865 else
3866 lineno--;
3869 if (s->matched_line) {
3870 s->first_displayed_line = s->matched_line;
3871 s->selected_line = 1;
3874 return NULL;
3877 static const struct got_error *
3878 show_blame_view(struct tog_view *view)
3880 const struct got_error *err = NULL;
3881 struct tog_blame_view_state *s = &view->state.blame;
3882 int errcode;
3884 if (s->blame.thread == NULL) {
3885 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3886 &s->blame.thread_args);
3887 if (errcode)
3888 return got_error_set_errno(errcode, "pthread_create");
3890 halfdelay(1); /* fast refresh while annotating */
3893 if (s->blame_complete)
3894 halfdelay(10); /* disable fast refresh */
3896 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3897 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3898 s->selected_line, &s->first_displayed_line,
3899 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3901 view_vborder(view);
3902 return err;
3905 static const struct got_error *
3906 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3907 struct tog_view **focus_view, struct tog_view *view, int ch)
3909 const struct got_error *err = NULL, *thread_err = NULL;
3910 struct tog_view *diff_view;
3911 struct tog_blame_view_state *s = &view->state.blame;
3912 int begin_x = 0;
3914 switch (ch) {
3915 case 'q':
3916 s->done = 1;
3917 break;
3918 case 'k':
3919 case KEY_UP:
3920 if (s->selected_line > 1)
3921 s->selected_line--;
3922 else if (s->selected_line == 1 &&
3923 s->first_displayed_line > 1)
3924 s->first_displayed_line--;
3925 break;
3926 case KEY_PPAGE:
3927 if (s->first_displayed_line == 1) {
3928 s->selected_line = 1;
3929 break;
3931 if (s->first_displayed_line > view->nlines - 2)
3932 s->first_displayed_line -=
3933 (view->nlines - 2);
3934 else
3935 s->first_displayed_line = 1;
3936 break;
3937 case 'j':
3938 case KEY_DOWN:
3939 if (s->selected_line < view->nlines - 2 &&
3940 s->first_displayed_line +
3941 s->selected_line <= s->blame.nlines)
3942 s->selected_line++;
3943 else if (s->last_displayed_line <
3944 s->blame.nlines)
3945 s->first_displayed_line++;
3946 break;
3947 case 'b':
3948 case 'p': {
3949 struct got_object_id *id = NULL;
3950 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3951 s->first_displayed_line, s->selected_line);
3952 if (id == NULL)
3953 break;
3954 if (ch == 'p') {
3955 struct got_commit_object *commit;
3956 struct got_object_qid *pid;
3957 struct got_object_id *blob_id = NULL;
3958 int obj_type;
3959 err = got_object_open_as_commit(&commit,
3960 s->repo, id);
3961 if (err)
3962 break;
3963 pid = SIMPLEQ_FIRST(
3964 got_object_commit_get_parent_ids(commit));
3965 if (pid == NULL) {
3966 got_object_commit_close(commit);
3967 break;
3969 /* Check if path history ends here. */
3970 err = got_object_id_by_path(&blob_id, s->repo,
3971 pid->id, s->path);
3972 if (err) {
3973 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3974 err = NULL;
3975 got_object_commit_close(commit);
3976 break;
3978 err = got_object_get_type(&obj_type, s->repo,
3979 blob_id);
3980 free(blob_id);
3981 /* Can't blame non-blob type objects. */
3982 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3983 got_object_commit_close(commit);
3984 break;
3986 err = got_object_qid_alloc(&s->blamed_commit,
3987 pid->id);
3988 got_object_commit_close(commit);
3989 } else {
3990 if (got_object_id_cmp(id,
3991 s->blamed_commit->id) == 0)
3992 break;
3993 err = got_object_qid_alloc(&s->blamed_commit,
3994 id);
3996 if (err)
3997 break;
3998 s->done = 1;
3999 thread_err = stop_blame(&s->blame);
4000 s->done = 0;
4001 if (thread_err)
4002 break;
4003 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4004 s->blamed_commit, entry);
4005 err = run_blame(&s->blame, view, &s->blame_complete,
4006 &s->first_displayed_line, &s->last_displayed_line,
4007 &s->selected_line, &s->done, &s->eof,
4008 s->path, s->blamed_commit->id, s->repo);
4009 if (err)
4010 break;
4011 break;
4013 case 'B': {
4014 struct got_object_qid *first;
4015 first = SIMPLEQ_FIRST(&s->blamed_commits);
4016 if (!got_object_id_cmp(first->id, s->commit_id))
4017 break;
4018 s->done = 1;
4019 thread_err = stop_blame(&s->blame);
4020 s->done = 0;
4021 if (thread_err)
4022 break;
4023 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4024 got_object_qid_free(s->blamed_commit);
4025 s->blamed_commit =
4026 SIMPLEQ_FIRST(&s->blamed_commits);
4027 err = run_blame(&s->blame, view, &s->blame_complete,
4028 &s->first_displayed_line, &s->last_displayed_line,
4029 &s->selected_line, &s->done, &s->eof, s->path,
4030 s->blamed_commit->id, s->repo);
4031 if (err)
4032 break;
4033 break;
4035 case KEY_ENTER:
4036 case '\r': {
4037 struct got_object_id *id = NULL;
4038 struct got_object_qid *pid;
4039 struct got_commit_object *commit = NULL;
4040 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4041 s->first_displayed_line, s->selected_line);
4042 if (id == NULL)
4043 break;
4044 err = got_object_open_as_commit(&commit, s->repo, id);
4045 if (err)
4046 break;
4047 pid = SIMPLEQ_FIRST(
4048 got_object_commit_get_parent_ids(commit));
4049 if (view_is_parent_view(view))
4050 begin_x = view_split_begin_x(view->begin_x);
4051 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4052 if (diff_view == NULL) {
4053 got_object_commit_close(commit);
4054 err = got_error_from_errno("view_open");
4055 break;
4057 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4058 id, NULL, s->refs, s->repo);
4059 got_object_commit_close(commit);
4060 if (err) {
4061 view_close(diff_view);
4062 break;
4064 if (view_is_parent_view(view)) {
4065 err = view_close_child(view);
4066 if (err)
4067 break;
4068 err = view_set_child(view, diff_view);
4069 if (err) {
4070 view_close(diff_view);
4071 break;
4073 *focus_view = diff_view;
4074 view->child_focussed = 1;
4075 } else
4076 *new_view = diff_view;
4077 if (err)
4078 break;
4079 break;
4081 case KEY_NPAGE:
4082 case ' ':
4083 if (s->last_displayed_line >= s->blame.nlines &&
4084 s->selected_line >= MIN(s->blame.nlines,
4085 view->nlines - 2)) {
4086 break;
4088 if (s->last_displayed_line >= s->blame.nlines &&
4089 s->selected_line < view->nlines - 2) {
4090 s->selected_line = MIN(s->blame.nlines,
4091 view->nlines - 2);
4092 break;
4094 if (s->last_displayed_line + view->nlines - 2
4095 <= s->blame.nlines)
4096 s->first_displayed_line +=
4097 view->nlines - 2;
4098 else
4099 s->first_displayed_line =
4100 s->blame.nlines -
4101 (view->nlines - 3);
4102 break;
4103 case KEY_RESIZE:
4104 if (s->selected_line > view->nlines - 2) {
4105 s->selected_line = MIN(s->blame.nlines,
4106 view->nlines - 2);
4108 break;
4109 default:
4110 break;
4112 return thread_err ? thread_err : err;
4115 static const struct got_error *
4116 cmd_blame(int argc, char *argv[])
4118 const struct got_error *error;
4119 struct got_repository *repo = NULL;
4120 struct got_reflist_head refs;
4121 struct got_worktree *worktree = NULL;
4122 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4123 struct got_object_id *commit_id = NULL;
4124 char *commit_id_str = NULL;
4125 int ch;
4126 struct tog_view *view;
4128 SIMPLEQ_INIT(&refs);
4130 #ifndef PROFILE
4131 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4132 NULL) == -1)
4133 err(1, "pledge");
4134 #endif
4136 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4137 switch (ch) {
4138 case 'c':
4139 commit_id_str = optarg;
4140 break;
4141 case 'r':
4142 repo_path = realpath(optarg, NULL);
4143 if (repo_path == NULL)
4144 return got_error_from_errno2("realpath",
4145 optarg);
4146 break;
4147 default:
4148 usage_blame();
4149 /* NOTREACHED */
4153 argc -= optind;
4154 argv += optind;
4156 if (argc == 1)
4157 path = argv[0];
4158 else
4159 usage_blame();
4161 cwd = getcwd(NULL, 0);
4162 if (cwd == NULL) {
4163 error = got_error_from_errno("getcwd");
4164 goto done;
4166 if (repo_path == NULL) {
4167 error = got_worktree_open(&worktree, cwd);
4168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4169 goto done;
4170 else
4171 error = NULL;
4172 if (worktree) {
4173 repo_path =
4174 strdup(got_worktree_get_repo_path(worktree));
4175 if (repo_path == NULL)
4176 error = got_error_from_errno("strdup");
4177 if (error)
4178 goto done;
4179 } else {
4180 repo_path = strdup(cwd);
4181 if (repo_path == NULL) {
4182 error = got_error_from_errno("strdup");
4183 goto done;
4188 init_curses();
4190 error = got_repo_open(&repo, repo_path, NULL);
4191 if (error != NULL)
4192 goto done;
4194 error = apply_unveil(got_repo_get_path(repo), NULL);
4195 if (error)
4196 goto done;
4198 if (worktree) {
4199 const char *prefix = got_worktree_get_path_prefix(worktree);
4200 char *p, *worktree_subdir = cwd +
4201 strlen(got_worktree_get_root_path(worktree));
4202 if (asprintf(&p, "%s%s%s%s%s",
4203 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4204 worktree_subdir, worktree_subdir[0] ? "/" : "",
4205 path) == -1) {
4206 error = got_error_from_errno("asprintf");
4207 goto done;
4209 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4210 free(p);
4211 } else {
4212 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4214 if (error)
4215 goto done;
4217 if (commit_id_str == NULL) {
4218 struct got_reference *head_ref;
4219 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4220 if (error != NULL)
4221 goto done;
4222 error = got_ref_resolve(&commit_id, repo, head_ref);
4223 got_ref_close(head_ref);
4224 } else {
4225 error = got_repo_match_object_id(&commit_id, NULL,
4226 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4228 if (error != NULL)
4229 goto done;
4231 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4232 if (error)
4233 goto done;
4235 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4236 if (view == NULL) {
4237 error = got_error_from_errno("view_open");
4238 goto done;
4240 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4241 if (error)
4242 goto done;
4243 if (worktree) {
4244 /* Release work tree lock. */
4245 got_worktree_close(worktree);
4246 worktree = NULL;
4248 error = view_loop(view);
4249 done:
4250 free(repo_path);
4251 free(cwd);
4252 free(commit_id);
4253 if (worktree)
4254 got_worktree_close(worktree);
4255 if (repo)
4256 got_repo_close(repo);
4257 got_ref_list_free(&refs);
4258 return error;
4261 static const struct got_error *
4262 draw_tree_entries(struct tog_view *view,
4263 struct got_tree_entry **first_displayed_entry,
4264 struct got_tree_entry **last_displayed_entry,
4265 struct got_tree_entry **selected_entry, int *ndisplayed,
4266 const char *label, int show_ids, const char *parent_path,
4267 struct got_tree_object *tree, int selected, int limit,
4268 int isroot, struct tog_colors *colors)
4270 const struct got_error *err = NULL;
4271 struct got_tree_entry *te;
4272 wchar_t *wline;
4273 struct tog_color *tc;
4274 int width, n, i, nentries;
4276 *ndisplayed = 0;
4278 werase(view->window);
4280 if (limit == 0)
4281 return NULL;
4283 err = format_line(&wline, &width, label, view->ncols, 0);
4284 if (err)
4285 return err;
4286 if (view_needs_focus_indication(view))
4287 wstandout(view->window);
4288 tc = get_color(colors, TOG_COLOR_COMMIT);
4289 if (tc)
4290 wattr_on(view->window,
4291 COLOR_PAIR(tc->colorpair), NULL);
4292 waddwstr(view->window, wline);
4293 if (tc)
4294 wattr_off(view->window,
4295 COLOR_PAIR(tc->colorpair), NULL);
4296 if (view_needs_focus_indication(view))
4297 wstandend(view->window);
4298 free(wline);
4299 wline = NULL;
4300 if (width < view->ncols - 1)
4301 waddch(view->window, '\n');
4302 if (--limit <= 0)
4303 return NULL;
4304 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4305 if (err)
4306 return err;
4307 waddwstr(view->window, wline);
4308 free(wline);
4309 wline = NULL;
4310 if (width < view->ncols - 1)
4311 waddch(view->window, '\n');
4312 if (--limit <= 0)
4313 return NULL;
4314 waddch(view->window, '\n');
4315 if (--limit <= 0)
4316 return NULL;
4318 if (*first_displayed_entry == NULL) {
4319 te = got_object_tree_get_first_entry(tree);
4320 if (selected == 0) {
4321 if (view->focussed)
4322 wstandout(view->window);
4323 *selected_entry = NULL;
4325 waddstr(view->window, " ..\n"); /* parent directory */
4326 if (selected == 0 && view->focussed)
4327 wstandend(view->window);
4328 (*ndisplayed)++;
4329 if (--limit <= 0)
4330 return NULL;
4331 n = 1;
4332 } else {
4333 n = 0;
4334 te = *first_displayed_entry;
4337 nentries = got_object_tree_get_nentries(tree);
4338 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4339 char *line = NULL, *id_str = NULL;
4340 const char *modestr = "";
4341 mode_t mode;
4343 te = got_object_tree_get_entry(tree, i);
4344 mode = got_tree_entry_get_mode(te);
4346 if (show_ids) {
4347 err = got_object_id_str(&id_str,
4348 got_tree_entry_get_id(te));
4349 if (err)
4350 return got_error_from_errno(
4351 "got_object_id_str");
4353 if (got_object_tree_entry_is_submodule(te))
4354 modestr = "$";
4355 else if (S_ISLNK(mode))
4356 modestr = "@";
4357 else if (S_ISDIR(mode))
4358 modestr = "/";
4359 else if (mode & S_IXUSR)
4360 modestr = "*";
4361 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4362 got_tree_entry_get_name(te), modestr) == -1) {
4363 free(id_str);
4364 return got_error_from_errno("asprintf");
4366 free(id_str);
4367 err = format_line(&wline, &width, line, view->ncols, 0);
4368 if (err) {
4369 free(line);
4370 break;
4372 if (n == selected) {
4373 if (view->focussed)
4374 wstandout(view->window);
4375 *selected_entry = te;
4377 tc = match_color(colors, line);
4378 if (tc)
4379 wattr_on(view->window,
4380 COLOR_PAIR(tc->colorpair), NULL);
4381 waddwstr(view->window, wline);
4382 if (tc)
4383 wattr_off(view->window,
4384 COLOR_PAIR(tc->colorpair), NULL);
4385 if (width < view->ncols - 1)
4386 waddch(view->window, '\n');
4387 if (n == selected && view->focussed)
4388 wstandend(view->window);
4389 free(line);
4390 free(wline);
4391 wline = NULL;
4392 n++;
4393 (*ndisplayed)++;
4394 *last_displayed_entry = te;
4395 if (--limit <= 0)
4396 break;
4399 return err;
4402 static void
4403 tree_scroll_up(struct tog_view *view,
4404 struct got_tree_entry **first_displayed_entry, int maxscroll,
4405 struct got_tree_object *tree, int isroot)
4407 struct got_tree_entry *te;
4408 int i;
4410 if (*first_displayed_entry == NULL)
4411 return;
4413 te = got_object_tree_get_entry(tree, 0);
4414 if (*first_displayed_entry == te) {
4415 if (!isroot)
4416 *first_displayed_entry = NULL;
4417 return;
4420 i = 0;
4421 while (*first_displayed_entry && i < maxscroll) {
4422 *first_displayed_entry = got_tree_entry_get_prev(tree,
4423 *first_displayed_entry);
4424 i++;
4426 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4427 *first_displayed_entry = NULL;
4430 static int
4431 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4432 struct got_tree_entry *last_displayed_entry,
4433 struct got_tree_object *tree)
4435 struct got_tree_entry *next, *last;
4436 int n = 0;
4438 if (*first_displayed_entry)
4439 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4440 else
4441 next = got_object_tree_get_first_entry(tree);
4443 last = last_displayed_entry;
4444 while (next && last && n++ < maxscroll) {
4445 last = got_tree_entry_get_next(tree, last);
4446 if (last) {
4447 *first_displayed_entry = next;
4448 next = got_tree_entry_get_next(tree, next);
4451 return n;
4454 static const struct got_error *
4455 tree_entry_path(char **path, struct tog_parent_trees *parents,
4456 struct got_tree_entry *te)
4458 const struct got_error *err = NULL;
4459 struct tog_parent_tree *pt;
4460 size_t len = 2; /* for leading slash and NUL */
4462 TAILQ_FOREACH(pt, parents, entry)
4463 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4464 + 1 /* slash */;
4465 if (te)
4466 len += strlen(got_tree_entry_get_name(te));
4468 *path = calloc(1, len);
4469 if (path == NULL)
4470 return got_error_from_errno("calloc");
4472 (*path)[0] = '/';
4473 pt = TAILQ_LAST(parents, tog_parent_trees);
4474 while (pt) {
4475 const char *name = got_tree_entry_get_name(pt->selected_entry);
4476 if (strlcat(*path, name, len) >= len) {
4477 err = got_error(GOT_ERR_NO_SPACE);
4478 goto done;
4480 if (strlcat(*path, "/", len) >= len) {
4481 err = got_error(GOT_ERR_NO_SPACE);
4482 goto done;
4484 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4486 if (te) {
4487 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4488 err = got_error(GOT_ERR_NO_SPACE);
4489 goto done;
4492 done:
4493 if (err) {
4494 free(*path);
4495 *path = NULL;
4497 return err;
4500 static const struct got_error *
4501 blame_tree_entry(struct tog_view **new_view, int begin_x,
4502 struct got_tree_entry *te, struct tog_parent_trees *parents,
4503 struct got_object_id *commit_id, struct got_reflist_head *refs,
4504 struct got_repository *repo)
4506 const struct got_error *err = NULL;
4507 char *path;
4508 struct tog_view *blame_view;
4510 *new_view = NULL;
4512 err = tree_entry_path(&path, parents, te);
4513 if (err)
4514 return err;
4516 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4517 if (blame_view == NULL) {
4518 err = got_error_from_errno("view_open");
4519 goto done;
4522 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4523 if (err) {
4524 if (err->code == GOT_ERR_CANCELLED)
4525 err = NULL;
4526 view_close(blame_view);
4527 } else
4528 *new_view = blame_view;
4529 done:
4530 free(path);
4531 return err;
4534 static const struct got_error *
4535 log_tree_entry(struct tog_view **new_view, int begin_x,
4536 struct got_tree_entry *te, struct tog_parent_trees *parents,
4537 struct got_object_id *commit_id, struct got_reflist_head *refs,
4538 struct got_repository *repo)
4540 struct tog_view *log_view;
4541 const struct got_error *err = NULL;
4542 char *path;
4544 *new_view = NULL;
4546 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4547 if (log_view == NULL)
4548 return got_error_from_errno("view_open");
4550 err = tree_entry_path(&path, parents, te);
4551 if (err)
4552 return err;
4554 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4555 if (err)
4556 view_close(log_view);
4557 else
4558 *new_view = log_view;
4559 free(path);
4560 return err;
4563 static const struct got_error *
4564 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4565 struct got_object_id *commit_id, struct got_reflist_head *refs,
4566 struct got_repository *repo)
4568 const struct got_error *err = NULL;
4569 char *commit_id_str = NULL;
4570 struct tog_tree_view_state *s = &view->state.tree;
4572 TAILQ_INIT(&s->parents);
4574 err = got_object_id_str(&commit_id_str, commit_id);
4575 if (err != NULL)
4576 goto done;
4578 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4579 err = got_error_from_errno("asprintf");
4580 goto done;
4583 s->root = s->tree = root;
4584 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4585 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4586 s->commit_id = got_object_id_dup(commit_id);
4587 if (s->commit_id == NULL) {
4588 err = got_error_from_errno("got_object_id_dup");
4589 goto done;
4591 s->refs = refs;
4592 s->repo = repo;
4594 SIMPLEQ_INIT(&s->colors);
4596 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4597 err = add_color(&s->colors, "\\$$",
4598 TOG_COLOR_TREE_SUBMODULE,
4599 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4600 if (err)
4601 goto done;
4602 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4603 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4604 if (err) {
4605 free_colors(&s->colors);
4606 goto done;
4608 err = add_color(&s->colors, "/$",
4609 TOG_COLOR_TREE_DIRECTORY,
4610 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4611 if (err) {
4612 free_colors(&s->colors);
4613 goto done;
4616 err = add_color(&s->colors, "\\*$",
4617 TOG_COLOR_TREE_EXECUTABLE,
4618 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4619 if (err) {
4620 free_colors(&s->colors);
4621 goto done;
4624 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4625 get_color_value("TOG_COLOR_COMMIT"));
4626 if (err) {
4627 free_colors(&s->colors);
4628 goto done;
4632 view->show = show_tree_view;
4633 view->input = input_tree_view;
4634 view->close = close_tree_view;
4635 view->search_start = search_start_tree_view;
4636 view->search_next = search_next_tree_view;
4637 done:
4638 free(commit_id_str);
4639 if (err) {
4640 free(s->tree_label);
4641 s->tree_label = NULL;
4643 return err;
4646 static const struct got_error *
4647 close_tree_view(struct tog_view *view)
4649 struct tog_tree_view_state *s = &view->state.tree;
4651 free_colors(&s->colors);
4652 free(s->tree_label);
4653 s->tree_label = NULL;
4654 free(s->commit_id);
4655 s->commit_id = NULL;
4656 while (!TAILQ_EMPTY(&s->parents)) {
4657 struct tog_parent_tree *parent;
4658 parent = TAILQ_FIRST(&s->parents);
4659 TAILQ_REMOVE(&s->parents, parent, entry);
4660 free(parent);
4663 if (s->tree != s->root)
4664 got_object_tree_close(s->tree);
4665 got_object_tree_close(s->root);
4667 return NULL;
4670 static const struct got_error *
4671 search_start_tree_view(struct tog_view *view)
4673 struct tog_tree_view_state *s = &view->state.tree;
4675 s->matched_entry = NULL;
4676 return NULL;
4679 static int
4680 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4682 regmatch_t regmatch;
4684 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4685 0) == 0;
4688 static const struct got_error *
4689 search_next_tree_view(struct tog_view *view)
4691 struct tog_tree_view_state *s = &view->state.tree;
4692 struct got_tree_entry *te = NULL;
4694 if (!view->searching) {
4695 view->search_next_done = 1;
4696 return NULL;
4699 if (s->matched_entry) {
4700 if (view->searching == TOG_SEARCH_FORWARD) {
4701 if (s->selected_entry)
4702 te = got_tree_entry_get_next(s->tree,
4703 s->selected_entry);
4704 else
4705 te = got_object_tree_get_first_entry(s->tree);
4706 } else {
4707 if (s->selected_entry == NULL)
4708 te = got_object_tree_get_last_entry(s->tree);
4709 else
4710 te = got_tree_entry_get_prev(s->tree,
4711 s->selected_entry);
4713 } else {
4714 if (view->searching == TOG_SEARCH_FORWARD)
4715 te = got_object_tree_get_first_entry(s->tree);
4716 else
4717 te = got_object_tree_get_last_entry(s->tree);
4720 while (1) {
4721 if (te == NULL) {
4722 if (s->matched_entry == NULL) {
4723 view->search_next_done = 1;
4724 return NULL;
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 if (match_tree_entry(te, &view->regex)) {
4733 view->search_next_done = 1;
4734 s->matched_entry = te;
4735 break;
4738 if (view->searching == TOG_SEARCH_FORWARD)
4739 te = got_tree_entry_get_next(s->tree, te);
4740 else
4741 te = got_tree_entry_get_prev(s->tree, te);
4744 if (s->matched_entry) {
4745 s->first_displayed_entry = s->matched_entry;
4746 s->selected = 0;
4749 return NULL;
4752 static const struct got_error *
4753 show_tree_view(struct tog_view *view)
4755 const struct got_error *err = NULL;
4756 struct tog_tree_view_state *s = &view->state.tree;
4757 char *parent_path;
4759 err = tree_entry_path(&parent_path, &s->parents, NULL);
4760 if (err)
4761 return err;
4763 err = draw_tree_entries(view, &s->first_displayed_entry,
4764 &s->last_displayed_entry, &s->selected_entry,
4765 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4766 s->tree, s->selected, view->nlines, s->tree == s->root,
4767 &s->colors);
4768 free(parent_path);
4770 view_vborder(view);
4771 return err;
4774 static const struct got_error *
4775 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4776 struct tog_view **focus_view, struct tog_view *view, int ch)
4778 const struct got_error *err = NULL;
4779 struct tog_tree_view_state *s = &view->state.tree;
4780 struct tog_view *log_view;
4781 int begin_x = 0, nscrolled;
4783 switch (ch) {
4784 case 'i':
4785 s->show_ids = !s->show_ids;
4786 break;
4787 case 'l':
4788 if (!s->selected_entry)
4789 break;
4790 if (view_is_parent_view(view))
4791 begin_x = view_split_begin_x(view->begin_x);
4792 err = log_tree_entry(&log_view, begin_x,
4793 s->selected_entry, &s->parents,
4794 s->commit_id, s->refs, s->repo);
4795 if (view_is_parent_view(view)) {
4796 err = view_close_child(view);
4797 if (err)
4798 return err;
4799 err = view_set_child(view, log_view);
4800 if (err) {
4801 view_close(log_view);
4802 break;
4804 *focus_view = log_view;
4805 view->child_focussed = 1;
4806 } else
4807 *new_view = log_view;
4808 break;
4809 case 'k':
4810 case KEY_UP:
4811 if (s->selected > 0) {
4812 s->selected--;
4813 if (s->selected == 0)
4814 break;
4816 if (s->selected > 0)
4817 break;
4818 tree_scroll_up(view, &s->first_displayed_entry, 1,
4819 s->tree, s->tree == s->root);
4820 break;
4821 case KEY_PPAGE:
4822 tree_scroll_up(view, &s->first_displayed_entry,
4823 MAX(0, view->nlines - 4 - s->selected), s->tree,
4824 s->tree == s->root);
4825 s->selected = 0;
4826 if (got_object_tree_get_first_entry(s->tree) ==
4827 s->first_displayed_entry && s->tree != s->root)
4828 s->first_displayed_entry = NULL;
4829 break;
4830 case 'j':
4831 case KEY_DOWN:
4832 if (s->selected < s->ndisplayed - 1) {
4833 s->selected++;
4834 break;
4836 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4837 == NULL)
4838 /* can't scroll any further */
4839 break;
4840 tree_scroll_down(&s->first_displayed_entry, 1,
4841 s->last_displayed_entry, s->tree);
4842 break;
4843 case KEY_NPAGE:
4844 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4845 == NULL) {
4846 /* can't scroll any further; move cursor down */
4847 if (s->selected < s->ndisplayed - 1)
4848 s->selected = s->ndisplayed - 1;
4849 break;
4851 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4852 view->nlines, s->last_displayed_entry, s->tree);
4853 if (nscrolled < view->nlines) {
4854 int ndisplayed = 0;
4855 struct got_tree_entry *te;
4856 te = s->first_displayed_entry;
4857 do {
4858 ndisplayed++;
4859 te = got_tree_entry_get_next(s->tree, te);
4860 } while (te);
4861 s->selected = ndisplayed - 1;
4863 break;
4864 case KEY_ENTER:
4865 case '\r':
4866 case KEY_BACKSPACE:
4867 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4868 struct tog_parent_tree *parent;
4869 /* user selected '..' */
4870 if (s->tree == s->root)
4871 break;
4872 parent = TAILQ_FIRST(&s->parents);
4873 TAILQ_REMOVE(&s->parents, parent,
4874 entry);
4875 got_object_tree_close(s->tree);
4876 s->tree = parent->tree;
4877 s->first_displayed_entry =
4878 parent->first_displayed_entry;
4879 s->selected_entry =
4880 parent->selected_entry;
4881 s->selected = parent->selected;
4882 free(parent);
4883 } else if (S_ISDIR(got_tree_entry_get_mode(
4884 s->selected_entry))) {
4885 struct got_tree_object *subtree;
4886 err = got_object_open_as_tree(&subtree, s->repo,
4887 got_tree_entry_get_id(s->selected_entry));
4888 if (err)
4889 break;
4890 err = tree_view_visit_subtree(subtree, s);
4891 if (err) {
4892 got_object_tree_close(subtree);
4893 break;
4895 } else if (S_ISREG(got_tree_entry_get_mode(
4896 s->selected_entry))) {
4897 struct tog_view *blame_view;
4898 int begin_x = view_is_parent_view(view) ?
4899 view_split_begin_x(view->begin_x) : 0;
4901 err = blame_tree_entry(&blame_view, begin_x,
4902 s->selected_entry, &s->parents,
4903 s->commit_id, s->refs, s->repo);
4904 if (err)
4905 break;
4906 if (view_is_parent_view(view)) {
4907 err = view_close_child(view);
4908 if (err)
4909 return err;
4910 err = view_set_child(view, blame_view);
4911 if (err) {
4912 view_close(blame_view);
4913 break;
4915 *focus_view = blame_view;
4916 view->child_focussed = 1;
4917 } else
4918 *new_view = blame_view;
4920 break;
4921 case KEY_RESIZE:
4922 if (s->selected > view->nlines)
4923 s->selected = s->ndisplayed - 1;
4924 break;
4925 default:
4926 break;
4929 return err;
4932 __dead static void
4933 usage_tree(void)
4935 endwin();
4936 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4937 getprogname());
4938 exit(1);
4941 static const struct got_error *
4942 cmd_tree(int argc, char *argv[])
4944 const struct got_error *error;
4945 struct got_repository *repo = NULL;
4946 struct got_reflist_head refs;
4947 char *repo_path = NULL;
4948 struct got_object_id *commit_id = NULL;
4949 char *commit_id_arg = NULL;
4950 struct got_commit_object *commit = NULL;
4951 struct got_tree_object *tree = NULL;
4952 int ch;
4953 struct tog_view *view;
4955 SIMPLEQ_INIT(&refs);
4957 #ifndef PROFILE
4958 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4959 NULL) == -1)
4960 err(1, "pledge");
4961 #endif
4963 while ((ch = getopt(argc, argv, "c:")) != -1) {
4964 switch (ch) {
4965 case 'c':
4966 commit_id_arg = optarg;
4967 break;
4968 default:
4969 usage_tree();
4970 /* NOTREACHED */
4974 argc -= optind;
4975 argv += optind;
4977 if (argc == 0) {
4978 struct got_worktree *worktree;
4979 char *cwd = getcwd(NULL, 0);
4980 if (cwd == NULL)
4981 return got_error_from_errno("getcwd");
4982 error = got_worktree_open(&worktree, cwd);
4983 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4984 goto done;
4985 if (worktree) {
4986 free(cwd);
4987 repo_path =
4988 strdup(got_worktree_get_repo_path(worktree));
4989 got_worktree_close(worktree);
4990 } else
4991 repo_path = cwd;
4992 if (repo_path == NULL) {
4993 error = got_error_from_errno("strdup");
4994 goto done;
4996 } else if (argc == 1) {
4997 repo_path = realpath(argv[0], NULL);
4998 if (repo_path == NULL)
4999 return got_error_from_errno2("realpath", argv[0]);
5000 } else
5001 usage_tree();
5003 init_curses();
5005 error = got_repo_open(&repo, repo_path, NULL);
5006 if (error != NULL)
5007 goto done;
5009 error = apply_unveil(got_repo_get_path(repo), NULL);
5010 if (error)
5011 goto done;
5013 error = got_repo_match_object_id(&commit_id, NULL,
5014 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5015 GOT_OBJ_TYPE_COMMIT, 1, repo);
5016 if (error != NULL)
5017 goto done;
5019 error = got_object_open_as_commit(&commit, repo, commit_id);
5020 if (error != NULL)
5021 goto done;
5023 error = got_object_open_as_tree(&tree, repo,
5024 got_object_commit_get_tree_id(commit));
5025 if (error != NULL)
5026 goto done;
5028 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5029 if (error)
5030 goto done;
5032 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5033 if (view == NULL) {
5034 error = got_error_from_errno("view_open");
5035 goto done;
5037 error = open_tree_view(view, tree, commit_id, &refs, repo);
5038 if (error)
5039 goto done;
5040 error = view_loop(view);
5041 done:
5042 free(repo_path);
5043 free(commit_id);
5044 if (commit)
5045 got_object_commit_close(commit);
5046 if (tree)
5047 got_object_tree_close(tree);
5048 if (repo)
5049 got_repo_close(repo);
5050 got_ref_list_free(&refs);
5051 return error;
5054 static void
5055 list_commands(void)
5057 int i;
5059 fprintf(stderr, "commands:");
5060 for (i = 0; i < nitems(tog_commands); i++) {
5061 struct tog_cmd *cmd = &tog_commands[i];
5062 fprintf(stderr, " %s", cmd->name);
5064 fputc('\n', stderr);
5067 __dead static void
5068 usage(int hflag)
5070 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5071 getprogname());
5072 if (hflag)
5073 list_commands();
5074 exit(1);
5077 static char **
5078 make_argv(const char *arg0, const char *arg1)
5080 char **argv;
5081 int argc = (arg1 == NULL ? 1 : 2);
5083 argv = calloc(argc, sizeof(char *));
5084 if (argv == NULL)
5085 err(1, "calloc");
5086 argv[0] = strdup(arg0);
5087 if (argv[0] == NULL)
5088 err(1, "strdup");
5089 if (arg1) {
5090 argv[1] = strdup(arg1);
5091 if (argv[1] == NULL)
5092 err(1, "strdup");
5095 return argv;
5098 int
5099 main(int argc, char *argv[])
5101 const struct got_error *error = NULL;
5102 struct tog_cmd *cmd = NULL;
5103 int ch, hflag = 0, Vflag = 0;
5104 char **cmd_argv = NULL;
5105 static struct option longopts[] = {
5106 { "version", no_argument, NULL, 'V' },
5107 { NULL, 0, NULL, 0}
5110 setlocale(LC_CTYPE, "");
5112 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5113 switch (ch) {
5114 case 'h':
5115 hflag = 1;
5116 break;
5117 case 'V':
5118 Vflag = 1;
5119 break;
5120 default:
5121 usage(hflag);
5122 /* NOTREACHED */
5126 argc -= optind;
5127 argv += optind;
5128 optind = 0;
5129 optreset = 1;
5131 if (Vflag) {
5132 got_version_print_str();
5133 return 1;
5136 if (argc == 0) {
5137 if (hflag)
5138 usage(hflag);
5139 /* Build an argument vector which runs a default command. */
5140 cmd = &tog_commands[0];
5141 cmd_argv = make_argv(cmd->name, NULL);
5142 argc = 1;
5143 } else {
5144 int i;
5146 /* Did the user specific a command? */
5147 for (i = 0; i < nitems(tog_commands); i++) {
5148 if (strncmp(tog_commands[i].name, argv[0],
5149 strlen(argv[0])) == 0) {
5150 cmd = &tog_commands[i];
5151 break;
5155 if (cmd == NULL) {
5156 fprintf(stderr, "%s: unknown command '%s'\n",
5157 getprogname(), argv[0]);
5158 list_commands();
5159 return 1;
5163 if (hflag)
5164 cmd->cmd_usage();
5165 else
5166 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5168 endwin();
5169 free(cmd_argv);
5170 if (error && error->code != GOT_ERR_CANCELLED)
5171 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5172 return 0;