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 <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_color {
116 SIMPLEQ_ENTRY(tog_color) entry;
117 regex_t regex;
118 short colorpair;
119 };
120 SIMPLEQ_HEAD(tog_colors, tog_color);
122 static const struct got_error *
123 add_color(struct tog_colors *colors, const char *pattern,
124 int idx, short color)
126 const struct got_error *err = NULL;
127 struct tog_color *tc;
128 int regerr = 0;
130 if (idx < 1 || idx > COLOR_PAIRS - 1)
131 return NULL;
133 init_pair(idx, color, -1);
135 tc = calloc(1, sizeof(*tc));
136 if (tc == NULL)
137 return got_error_from_errno("calloc");
138 regerr = regcomp(&tc->regex, pattern,
139 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
140 if (regerr) {
141 static char regerr_msg[512];
142 static char err_msg[512];
143 regerror(regerr, &tc->regex, regerr_msg,
144 sizeof(regerr_msg));
145 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
146 regerr_msg);
147 err = got_error_msg(GOT_ERR_REGEX, err_msg);
148 free(tc);
149 return err;
151 tc->colorpair = idx;
152 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
153 return NULL;
156 static void
157 free_colors(struct tog_colors *colors)
159 struct tog_color *tc;
161 while (!SIMPLEQ_EMPTY(colors)) {
162 tc = SIMPLEQ_FIRST(colors);
163 SIMPLEQ_REMOVE_HEAD(colors, entry);
164 regfree(&tc->regex);
165 free(tc);
169 struct tog_color *
170 get_color(struct tog_colors *colors, int colorpair)
172 struct tog_color *tc = NULL;
174 SIMPLEQ_FOREACH(tc, colors, entry) {
175 if (tc->colorpair == colorpair)
176 return tc;
179 return NULL;
182 static int
183 default_color_value(const char *envvar)
185 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
186 return COLOR_MAGENTA;
187 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
188 return COLOR_CYAN;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
190 return COLOR_YELLOW;
191 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
192 return COLOR_GREEN;
193 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
194 return COLOR_MAGENTA;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
196 return COLOR_CYAN;
197 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
198 return COLOR_BLUE;
199 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
200 return COLOR_GREEN;
201 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
202 return COLOR_GREEN;
203 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
204 return COLOR_CYAN;
205 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
206 return COLOR_YELLOW;
208 return -1;
211 static int
212 get_color_value(const char *envvar)
214 const char *val = getenv(envvar);
216 if (val == NULL)
217 return default_color_value(envvar);
219 if (strcasecmp(val, "black") == 0)
220 return COLOR_BLACK;
221 if (strcasecmp(val, "red") == 0)
222 return COLOR_RED;
223 if (strcasecmp(val, "green") == 0)
224 return COLOR_GREEN;
225 if (strcasecmp(val, "yellow") == 0)
226 return COLOR_YELLOW;
227 if (strcasecmp(val, "blue") == 0)
228 return COLOR_BLUE;
229 if (strcasecmp(val, "magenta") == 0)
230 return COLOR_MAGENTA;
231 if (strcasecmp(val, "cyan") == 0)
232 return COLOR_CYAN;
233 if (strcasecmp(val, "white") == 0)
234 return COLOR_WHITE;
235 if (strcasecmp(val, "default") == 0)
236 return -1;
238 return default_color_value(envvar);
242 struct tog_diff_view_state {
243 struct got_object_id *id1, *id2;
244 FILE *f;
245 int first_displayed_line;
246 int last_displayed_line;
247 int eof;
248 int diff_context;
249 struct got_repository *repo;
250 struct got_reflist_head *refs;
251 struct tog_colors colors;
253 /* passed from log view; may be NULL */
254 struct tog_view *log_view;
255 };
257 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
259 struct tog_log_thread_args {
260 pthread_cond_t need_commits;
261 int commits_needed;
262 struct got_commit_graph *graph;
263 struct commit_queue *commits;
264 const char *in_repo_path;
265 struct got_object_id *start_id;
266 struct got_repository *repo;
267 int log_complete;
268 sig_atomic_t *quit;
269 struct commit_queue_entry **first_displayed_entry;
270 struct commit_queue_entry **selected_entry;
271 int *searching;
272 int *search_next_done;
273 regex_t *regex;
274 };
276 struct tog_log_view_state {
277 struct commit_queue commits;
278 struct commit_queue_entry *first_displayed_entry;
279 struct commit_queue_entry *last_displayed_entry;
280 struct commit_queue_entry *selected_entry;
281 int selected;
282 char *in_repo_path;
283 const char *head_ref_name;
284 struct got_repository *repo;
285 struct got_reflist_head *refs;
286 struct got_object_id *start_id;
287 sig_atomic_t quit;
288 pthread_t thread;
289 struct tog_log_thread_args thread_args;
290 struct commit_queue_entry *matched_entry;
291 struct commit_queue_entry *search_entry;
292 struct tog_colors colors;
293 };
295 #define TOG_COLOR_DIFF_MINUS 1
296 #define TOG_COLOR_DIFF_PLUS 2
297 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
298 #define TOG_COLOR_DIFF_META 4
299 #define TOG_COLOR_TREE_SUBMODULE 5
300 #define TOG_COLOR_TREE_SYMLINK 6
301 #define TOG_COLOR_TREE_DIRECTORY 7
302 #define TOG_COLOR_TREE_EXECUTABLE 8
303 #define TOG_COLOR_COMMIT 9
304 #define TOG_COLOR_AUTHOR 10
305 #define TOG_COLOR_DATE 11
307 struct tog_blame_cb_args {
308 struct tog_blame_line *lines; /* one per line */
309 int nlines;
311 struct tog_view *view;
312 struct got_object_id *commit_id;
313 int *quit;
314 };
316 struct tog_blame_thread_args {
317 const char *path;
318 struct got_repository *repo;
319 struct tog_blame_cb_args *cb_args;
320 int *complete;
321 got_cancel_cb cancel_cb;
322 void *cancel_arg;
323 };
325 struct tog_blame {
326 FILE *f;
327 size_t filesize;
328 struct tog_blame_line *lines;
329 int nlines;
330 off_t *line_offsets;
331 pthread_t thread;
332 struct tog_blame_thread_args thread_args;
333 struct tog_blame_cb_args cb_args;
334 const char *path;
335 };
337 struct tog_blame_view_state {
338 int first_displayed_line;
339 int last_displayed_line;
340 int selected_line;
341 int blame_complete;
342 int eof;
343 int done;
344 struct got_object_id_queue blamed_commits;
345 struct got_object_qid *blamed_commit;
346 char *path;
347 struct got_repository *repo;
348 struct got_reflist_head *refs;
349 struct got_object_id *commit_id;
350 struct tog_blame blame;
351 int matched_line;
352 struct tog_colors colors;
353 };
355 struct tog_parent_tree {
356 TAILQ_ENTRY(tog_parent_tree) entry;
357 struct got_tree_object *tree;
358 struct got_tree_entry *first_displayed_entry;
359 struct got_tree_entry *selected_entry;
360 int selected;
361 };
363 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
365 struct tog_tree_view_state {
366 char *tree_label;
367 struct got_tree_object *root;
368 struct got_tree_object *tree;
369 struct got_tree_entry *first_displayed_entry;
370 struct got_tree_entry *last_displayed_entry;
371 struct got_tree_entry *selected_entry;
372 int ndisplayed, selected, show_ids;
373 struct tog_parent_trees parents;
374 struct got_object_id *commit_id;
375 struct got_repository *repo;
376 struct got_reflist_head *refs;
377 struct got_tree_entry *matched_entry;
378 struct tog_colors colors;
379 };
381 /*
382 * We implement two types of views: parent views and child views.
384 * The 'Tab' key switches between a parent view and its child view.
385 * Child views are shown side-by-side to their parent view, provided
386 * there is enough screen estate.
388 * When a new view is opened from within a parent view, this new view
389 * becomes a child view of the parent view, replacing any existing child.
391 * When a new view is opened from within a child view, this new view
392 * becomes a parent view which will obscure the views below until the
393 * user quits the new parent view by typing 'q'.
395 * This list of views contains parent views only.
396 * Child views are only pointed to by their parent view.
397 */
398 TAILQ_HEAD(tog_view_list_head, tog_view);
400 struct tog_view {
401 TAILQ_ENTRY(tog_view) entry;
402 WINDOW *window;
403 PANEL *panel;
404 int nlines, ncols, begin_y, begin_x;
405 int lines, cols; /* copies of LINES and COLS */
406 int focussed;
407 struct tog_view *parent;
408 struct tog_view *child;
409 int child_focussed;
411 /* type-specific state */
412 enum tog_view_type type;
413 union {
414 struct tog_diff_view_state diff;
415 struct tog_log_view_state log;
416 struct tog_blame_view_state blame;
417 struct tog_tree_view_state tree;
418 } state;
420 const struct got_error *(*show)(struct tog_view *);
421 const struct got_error *(*input)(struct tog_view **,
422 struct tog_view **, struct tog_view**, struct tog_view *, int);
423 const struct got_error *(*close)(struct tog_view *);
425 const struct got_error *(*search_start)(struct tog_view *);
426 const struct got_error *(*search_next)(struct tog_view *);
427 int searching;
428 #define TOG_SEARCH_FORWARD 1
429 #define TOG_SEARCH_BACKWARD 2
430 int search_next_done;
431 regex_t regex;
432 };
434 static const struct got_error *open_diff_view(struct tog_view *,
435 struct got_object_id *, struct got_object_id *, struct tog_view *,
436 struct got_reflist_head *, struct got_repository *);
437 static const struct got_error *show_diff_view(struct tog_view *);
438 static const struct got_error *input_diff_view(struct tog_view **,
439 struct tog_view **, struct tog_view **, struct tog_view *, int);
440 static const struct got_error* close_diff_view(struct tog_view *);
442 static const struct got_error *open_log_view(struct tog_view *,
443 struct got_object_id *, struct got_reflist_head *,
444 struct got_repository *, const char *, const char *, int);
445 static const struct got_error * show_log_view(struct tog_view *);
446 static const struct got_error *input_log_view(struct tog_view **,
447 struct tog_view **, struct tog_view **, struct tog_view *, int);
448 static const struct got_error *close_log_view(struct tog_view *);
449 static const struct got_error *search_start_log_view(struct tog_view *);
450 static const struct got_error *search_next_log_view(struct tog_view *);
452 static const struct got_error *open_blame_view(struct tog_view *, char *,
453 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
454 static const struct got_error *show_blame_view(struct tog_view *);
455 static const struct got_error *input_blame_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_blame_view(struct tog_view *);
458 static const struct got_error *search_start_blame_view(struct tog_view *);
459 static const struct got_error *search_next_blame_view(struct tog_view *);
461 static const struct got_error *open_tree_view(struct tog_view *,
462 struct got_tree_object *, struct got_object_id *,
463 struct got_reflist_head *, struct got_repository *);
464 static const struct got_error *show_tree_view(struct tog_view *);
465 static const struct got_error *input_tree_view(struct tog_view **,
466 struct tog_view **, struct tog_view **, struct tog_view *, int);
467 static const struct got_error *close_tree_view(struct tog_view *);
468 static const struct got_error *search_start_tree_view(struct tog_view *);
469 static const struct got_error *search_next_tree_view(struct tog_view *);
471 static volatile sig_atomic_t tog_sigwinch_received;
472 static volatile sig_atomic_t tog_sigpipe_received;
474 static void
475 tog_sigwinch(int signo)
477 tog_sigwinch_received = 1;
480 static void
481 tog_sigpipe(int signo)
483 tog_sigpipe_received = 1;
486 static const struct got_error *
487 view_close(struct tog_view *view)
489 const struct got_error *err = NULL;
491 if (view->child) {
492 view_close(view->child);
493 view->child = NULL;
495 if (view->close)
496 err = view->close(view);
497 if (view->panel)
498 del_panel(view->panel);
499 if (view->window)
500 delwin(view->window);
501 free(view);
502 return err;
505 static struct tog_view *
506 view_open(int nlines, int ncols, int begin_y, int begin_x,
507 enum tog_view_type type)
509 struct tog_view *view = calloc(1, sizeof(*view));
511 if (view == NULL)
512 return NULL;
514 view->type = type;
515 view->lines = LINES;
516 view->cols = COLS;
517 view->nlines = nlines ? nlines : LINES - begin_y;
518 view->ncols = ncols ? ncols : COLS - begin_x;
519 view->begin_y = begin_y;
520 view->begin_x = begin_x;
521 view->window = newwin(nlines, ncols, begin_y, begin_x);
522 if (view->window == NULL) {
523 view_close(view);
524 return NULL;
526 view->panel = new_panel(view->window);
527 if (view->panel == NULL ||
528 set_panel_userptr(view->panel, view) != OK) {
529 view_close(view);
530 return NULL;
533 keypad(view->window, TRUE);
534 return view;
537 static int
538 view_split_begin_x(int begin_x)
540 if (begin_x > 0 || COLS < 120)
541 return 0;
542 return (COLS - MAX(COLS / 2, 80));
545 static const struct got_error *view_resize(struct tog_view *);
547 static const struct got_error *
548 view_splitscreen(struct tog_view *view)
550 const struct got_error *err = NULL;
552 view->begin_y = 0;
553 view->begin_x = view_split_begin_x(0);
554 view->nlines = LINES;
555 view->ncols = COLS - view->begin_x;
556 view->lines = LINES;
557 view->cols = COLS;
558 err = view_resize(view);
559 if (err)
560 return err;
562 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
563 return got_error_from_errno("mvwin");
565 return NULL;
568 static const struct got_error *
569 view_fullscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
573 view->begin_x = 0;
574 view->begin_y = 0;
575 view->nlines = LINES;
576 view->ncols = COLS;
577 view->lines = LINES;
578 view->cols = COLS;
579 err = view_resize(view);
580 if (err)
581 return err;
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
586 return NULL;
589 static int
590 view_is_parent_view(struct tog_view *view)
592 return view->parent == NULL;
595 static const struct got_error *
596 view_resize(struct tog_view *view)
598 int nlines, ncols;
600 if (view->lines > LINES)
601 nlines = view->nlines - (view->lines - LINES);
602 else
603 nlines = view->nlines + (LINES - view->lines);
605 if (view->cols > COLS)
606 ncols = view->ncols - (view->cols - COLS);
607 else
608 ncols = view->ncols + (COLS - view->cols);
610 if (wresize(view->window, nlines, ncols) == ERR)
611 return got_error_from_errno("wresize");
612 if (replace_panel(view->panel, view->window) == ERR)
613 return got_error_from_errno("replace_panel");
614 wclear(view->window);
616 view->nlines = nlines;
617 view->ncols = ncols;
618 view->lines = LINES;
619 view->cols = COLS;
621 if (view->child) {
622 view->child->begin_x = view_split_begin_x(view->begin_x);
623 if (view->child->begin_x == 0) {
624 view_fullscreen(view->child);
625 if (view->child->focussed)
626 show_panel(view->child->panel);
627 else
628 show_panel(view->panel);
629 } else {
630 view_splitscreen(view->child);
631 show_panel(view->child->panel);
635 return NULL;
638 static const struct got_error *
639 view_close_child(struct tog_view *view)
641 const struct got_error *err = NULL;
643 if (view->child == NULL)
644 return NULL;
646 err = view_close(view->child);
647 view->child = NULL;
648 return err;
651 static const struct got_error *
652 view_set_child(struct tog_view *view, struct tog_view *child)
654 const struct got_error *err = NULL;
656 view->child = child;
657 child->parent = view;
658 return err;
661 static int
662 view_is_splitscreen(struct tog_view *view)
664 return view->begin_x > 0;
667 static void
668 tog_resizeterm(void)
670 int cols, lines;
671 struct winsize size;
673 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
674 cols = 80; /* Default */
675 lines = 24;
676 } else {
677 cols = size.ws_col;
678 lines = size.ws_row;
680 resize_term(lines, cols);
683 static const struct got_error *
684 view_search_start(struct tog_view *view)
686 const struct got_error *err = NULL;
687 char pattern[1024];
688 int ret;
689 int begin_x = 0;
691 if (view->nlines < 1)
692 return NULL;
694 if (!view_is_parent_view(view))
695 begin_x = view_split_begin_x(view->begin_x);
696 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
697 begin_x, "/");
698 wclrtoeol(view->window);
700 nocbreak();
701 echo();
702 ret = wgetnstr(view->window, pattern, sizeof(pattern));
703 cbreak();
704 noecho();
705 if (ret == ERR)
706 return NULL;
708 if (view->searching) {
709 regfree(&view->regex);
710 view->searching = 0;
713 if (regcomp(&view->regex, pattern,
714 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
715 err = view->search_start(view);
716 if (err) {
717 regfree(&view->regex);
718 return err;
720 view->searching = TOG_SEARCH_FORWARD;
721 view->search_next_done = 0;
722 view->search_next(view);
725 return NULL;
728 static const struct got_error *
729 view_input(struct tog_view **new, struct tog_view **dead,
730 struct tog_view **focus, int *done, struct tog_view *view,
731 struct tog_view_list_head *views)
733 const struct got_error *err = NULL;
734 struct tog_view *v;
735 int ch, errcode;
737 *new = NULL;
738 *dead = NULL;
739 *focus = NULL;
741 if (view->searching && !view->search_next_done) {
742 errcode = pthread_mutex_unlock(&tog_mutex);
743 if (errcode)
744 return got_error_set_errno(errcode,
745 "pthread_mutex_unlock");
746 pthread_yield();
747 errcode = pthread_mutex_lock(&tog_mutex);
748 if (errcode)
749 return got_error_set_errno(errcode,
750 "pthread_mutex_lock");
751 view->search_next(view);
752 return NULL;
755 nodelay(stdscr, FALSE);
756 /* Allow threads to make progress while we are waiting for input. */
757 errcode = pthread_mutex_unlock(&tog_mutex);
758 if (errcode)
759 return got_error_set_errno(errcode, "pthread_mutex_unlock");
760 ch = wgetch(view->window);
761 errcode = pthread_mutex_lock(&tog_mutex);
762 if (errcode)
763 return got_error_set_errno(errcode, "pthread_mutex_lock");
764 nodelay(stdscr, TRUE);
766 if (tog_sigwinch_received) {
767 tog_resizeterm();
768 tog_sigwinch_received = 0;
769 TAILQ_FOREACH(v, views, entry) {
770 err = view_resize(v);
771 if (err)
772 return err;
773 err = v->input(new, dead, focus, v, KEY_RESIZE);
774 if (err)
775 return err;
779 switch (ch) {
780 case ERR:
781 break;
782 case '\t':
783 if (view->child) {
784 *focus = view->child;
785 view->child_focussed = 1;
786 } else if (view->parent) {
787 *focus = view->parent;
788 view->parent->child_focussed = 0;
790 break;
791 case 'q':
792 err = view->input(new, dead, focus, view, ch);
793 *dead = view;
794 break;
795 case 'Q':
796 *done = 1;
797 break;
798 case 'f':
799 if (view_is_parent_view(view)) {
800 if (view->child == NULL)
801 break;
802 if (view_is_splitscreen(view->child)) {
803 *focus = view->child;
804 view->child_focussed = 1;
805 err = view_fullscreen(view->child);
806 } else
807 err = view_splitscreen(view->child);
808 if (err)
809 break;
810 err = view->child->input(new, dead, focus,
811 view->child, KEY_RESIZE);
812 } else {
813 if (view_is_splitscreen(view)) {
814 *focus = view;
815 view->parent->child_focussed = 1;
816 err = view_fullscreen(view);
817 } else {
818 err = view_splitscreen(view);
820 if (err)
821 break;
822 err = view->input(new, dead, focus, view,
823 KEY_RESIZE);
825 break;
826 case KEY_RESIZE:
827 break;
828 case '/':
829 if (view->search_start)
830 view_search_start(view);
831 else
832 err = view->input(new, dead, focus, view, ch);
833 break;
834 case 'N':
835 case 'n':
836 if (view->search_next && view->searching) {
837 view->searching = (ch == 'n' ?
838 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
839 view->search_next_done = 0;
840 view->search_next(view);
841 } else
842 err = view->input(new, dead, focus, view, ch);
843 break;
844 default:
845 err = view->input(new, dead, focus, view, ch);
846 break;
849 return err;
852 void
853 view_vborder(struct tog_view *view)
855 PANEL *panel;
856 struct tog_view *view_above;
858 if (view->parent)
859 return view_vborder(view->parent);
861 panel = panel_above(view->panel);
862 if (panel == NULL)
863 return;
865 view_above = panel_userptr(panel);
866 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
867 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
870 int
871 view_needs_focus_indication(struct tog_view *view)
873 if (view_is_parent_view(view)) {
874 if (view->child == NULL || view->child_focussed)
875 return 0;
876 if (!view_is_splitscreen(view->child))
877 return 0;
878 } else if (!view_is_splitscreen(view))
879 return 0;
881 return view->focussed;
884 static const struct got_error *
885 view_loop(struct tog_view *view)
887 const struct got_error *err = NULL;
888 struct tog_view_list_head views;
889 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
890 int fast_refresh = 10;
891 int done = 0, errcode;
893 errcode = pthread_mutex_lock(&tog_mutex);
894 if (errcode)
895 return got_error_set_errno(errcode, "pthread_mutex_lock");
897 TAILQ_INIT(&views);
898 TAILQ_INSERT_HEAD(&views, view, entry);
900 main_view = view;
901 view->focussed = 1;
902 err = view->show(view);
903 if (err)
904 return err;
905 update_panels();
906 doupdate();
907 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
908 /* Refresh fast during initialization, then become slower. */
909 if (fast_refresh && fast_refresh-- == 0)
910 halfdelay(10); /* switch to once per second */
912 err = view_input(&new_view, &dead_view, &focus_view, &done,
913 view, &views);
914 if (err)
915 break;
916 if (dead_view) {
917 struct tog_view *prev = NULL;
919 if (view_is_parent_view(dead_view))
920 prev = TAILQ_PREV(dead_view,
921 tog_view_list_head, entry);
922 else if (view->parent != dead_view)
923 prev = view->parent;
925 if (dead_view->parent)
926 dead_view->parent->child = NULL;
927 else
928 TAILQ_REMOVE(&views, dead_view, entry);
930 err = view_close(dead_view);
931 if (err || (dead_view == main_view && new_view == NULL))
932 goto done;
934 if (view == dead_view) {
935 if (focus_view)
936 view = focus_view;
937 else if (prev)
938 view = prev;
939 else if (!TAILQ_EMPTY(&views))
940 view = TAILQ_LAST(&views,
941 tog_view_list_head);
942 else
943 view = NULL;
944 if (view) {
945 if (view->child && view->child_focussed)
946 focus_view = view->child;
947 else
948 focus_view = view;
952 if (new_view) {
953 struct tog_view *v, *t;
954 /* Only allow one parent view per type. */
955 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
956 if (v->type != new_view->type)
957 continue;
958 TAILQ_REMOVE(&views, v, entry);
959 err = view_close(v);
960 if (err)
961 goto done;
962 break;
964 TAILQ_INSERT_TAIL(&views, new_view, entry);
965 view = new_view;
966 if (focus_view == NULL)
967 focus_view = new_view;
969 if (focus_view) {
970 show_panel(focus_view->panel);
971 if (view)
972 view->focussed = 0;
973 focus_view->focussed = 1;
974 view = focus_view;
975 if (new_view)
976 show_panel(new_view->panel);
977 if (view->child && view_is_splitscreen(view->child))
978 show_panel(view->child->panel);
980 if (view) {
981 if (focus_view == NULL) {
982 view->focussed = 1;
983 show_panel(view->panel);
984 if (view->child && view_is_splitscreen(view->child))
985 show_panel(view->child->panel);
986 focus_view = view;
988 if (view->parent) {
989 err = view->parent->show(view->parent);
990 if (err)
991 goto done;
993 err = view->show(view);
994 if (err)
995 goto done;
996 if (view->child) {
997 err = view->child->show(view->child);
998 if (err)
999 goto done;
1001 update_panels();
1002 doupdate();
1005 done:
1006 while (!TAILQ_EMPTY(&views)) {
1007 view = TAILQ_FIRST(&views);
1008 TAILQ_REMOVE(&views, view, entry);
1009 view_close(view);
1012 errcode = pthread_mutex_unlock(&tog_mutex);
1013 if (errcode && err == NULL)
1014 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1016 return err;
1019 __dead static void
1020 usage_log(void)
1022 endwin();
1023 fprintf(stderr,
1024 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1025 getprogname());
1026 exit(1);
1029 /* Create newly allocated wide-character string equivalent to a byte string. */
1030 static const struct got_error *
1031 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1033 char *vis = NULL;
1034 const struct got_error *err = NULL;
1036 *ws = NULL;
1037 *wlen = mbstowcs(NULL, s, 0);
1038 if (*wlen == (size_t)-1) {
1039 int vislen;
1040 if (errno != EILSEQ)
1041 return got_error_from_errno("mbstowcs");
1043 /* byte string invalid in current encoding; try to "fix" it */
1044 err = got_mbsavis(&vis, &vislen, s);
1045 if (err)
1046 return err;
1047 *wlen = mbstowcs(NULL, vis, 0);
1048 if (*wlen == (size_t)-1) {
1049 err = got_error_from_errno("mbstowcs"); /* give up */
1050 goto done;
1054 *ws = calloc(*wlen + 1, sizeof(**ws));
1055 if (*ws == NULL) {
1056 err = got_error_from_errno("calloc");
1057 goto done;
1060 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1061 err = got_error_from_errno("mbstowcs");
1062 done:
1063 free(vis);
1064 if (err) {
1065 free(*ws);
1066 *ws = NULL;
1067 *wlen = 0;
1069 return err;
1072 /* Format a line for display, ensuring that it won't overflow a width limit. */
1073 static const struct got_error *
1074 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1075 int col_tab_align)
1077 const struct got_error *err = NULL;
1078 int cols = 0;
1079 wchar_t *wline = NULL;
1080 size_t wlen;
1081 int i;
1083 *wlinep = NULL;
1084 *widthp = 0;
1086 err = mbs2ws(&wline, &wlen, line);
1087 if (err)
1088 return err;
1090 i = 0;
1091 while (i < wlen) {
1092 int width = wcwidth(wline[i]);
1094 if (width == 0) {
1095 i++;
1096 continue;
1099 if (width == 1 || width == 2) {
1100 if (cols + width > wlimit)
1101 break;
1102 cols += width;
1103 i++;
1104 } else if (width == -1) {
1105 if (wline[i] == L'\t') {
1106 width = TABSIZE -
1107 ((cols + col_tab_align) % TABSIZE);
1108 if (cols + width > wlimit)
1109 break;
1110 cols += width;
1112 i++;
1113 } else {
1114 err = got_error_from_errno("wcwidth");
1115 goto done;
1118 wline[i] = L'\0';
1119 if (widthp)
1120 *widthp = cols;
1121 done:
1122 if (err)
1123 free(wline);
1124 else
1125 *wlinep = wline;
1126 return err;
1129 static const struct got_error*
1130 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1131 struct got_object_id *id, struct got_repository *repo)
1133 static const struct got_error *err = NULL;
1134 struct got_reflist_entry *re;
1135 char *s;
1136 const char *name;
1138 *refs_str = NULL;
1140 SIMPLEQ_FOREACH(re, refs, entry) {
1141 struct got_tag_object *tag = NULL;
1142 int cmp;
1144 name = got_ref_get_name(re->ref);
1145 if (strcmp(name, GOT_REF_HEAD) == 0)
1146 continue;
1147 if (strncmp(name, "refs/", 5) == 0)
1148 name += 5;
1149 if (strncmp(name, "got/", 4) == 0)
1150 continue;
1151 if (strncmp(name, "heads/", 6) == 0)
1152 name += 6;
1153 if (strncmp(name, "remotes/", 8) == 0)
1154 name += 8;
1155 if (strncmp(name, "tags/", 5) == 0) {
1156 err = got_object_open_as_tag(&tag, repo, re->id);
1157 if (err) {
1158 if (err->code != GOT_ERR_OBJ_TYPE)
1159 break;
1160 /* Ref points at something other than a tag. */
1161 err = NULL;
1162 tag = NULL;
1165 cmp = got_object_id_cmp(tag ?
1166 got_object_tag_get_object_id(tag) : re->id, id);
1167 if (tag)
1168 got_object_tag_close(tag);
1169 if (cmp != 0)
1170 continue;
1171 s = *refs_str;
1172 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1173 s ? ", " : "", name) == -1) {
1174 err = got_error_from_errno("asprintf");
1175 free(s);
1176 *refs_str = NULL;
1177 break;
1179 free(s);
1182 return err;
1185 static const struct got_error *
1186 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1187 int col_tab_align)
1189 char *smallerthan, *at;
1191 smallerthan = strchr(author, '<');
1192 if (smallerthan && smallerthan[1] != '\0')
1193 author = smallerthan + 1;
1194 at = strchr(author, '@');
1195 if (at)
1196 *at = '\0';
1197 return format_line(wauthor, author_width, author, limit, col_tab_align);
1200 static const struct got_error *
1201 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1202 struct got_object_id *id, struct got_reflist_head *refs,
1203 const size_t date_display_cols, int author_display_cols,
1204 struct tog_colors *colors)
1206 const struct got_error *err = NULL;
1207 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1208 char *logmsg0 = NULL, *logmsg = NULL;
1209 char *author = NULL;
1210 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1211 int author_width, logmsg_width;
1212 char *newline, *line = NULL;
1213 int col, limit;
1214 const int avail = view->ncols;
1215 struct tm tm;
1216 time_t committer_time;
1217 struct tog_color *tc;
1219 committer_time = got_object_commit_get_committer_time(commit);
1220 if (localtime_r(&committer_time, &tm) == NULL)
1221 return got_error_from_errno("localtime_r");
1222 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1223 >= sizeof(datebuf))
1224 return got_error(GOT_ERR_NO_SPACE);
1226 if (avail <= date_display_cols)
1227 limit = MIN(sizeof(datebuf) - 1, avail);
1228 else
1229 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1230 tc = get_color(colors, TOG_COLOR_DATE);
1231 if (tc)
1232 wattr_on(view->window,
1233 COLOR_PAIR(tc->colorpair), NULL);
1234 waddnstr(view->window, datebuf, limit);
1235 if (tc)
1236 wattr_off(view->window,
1237 COLOR_PAIR(tc->colorpair), NULL);
1238 col = limit;
1239 if (col > avail)
1240 goto done;
1242 if (avail >= 120) {
1243 char *id_str;
1244 err = got_object_id_str(&id_str, id);
1245 if (err)
1246 goto done;
1247 tc = get_color(colors, TOG_COLOR_COMMIT);
1248 if (tc)
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 wprintw(view->window, "%.8s ", id_str);
1252 if (tc)
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1255 free(id_str);
1256 col += 9;
1257 if (col > avail)
1258 goto done;
1261 author = strdup(got_object_commit_get_author(commit));
1262 if (author == NULL) {
1263 err = got_error_from_errno("strdup");
1264 goto done;
1266 err = format_author(&wauthor, &author_width, author, avail - col, col);
1267 if (err)
1268 goto done;
1269 tc = get_color(colors, TOG_COLOR_AUTHOR);
1270 if (tc)
1271 wattr_on(view->window,
1272 COLOR_PAIR(tc->colorpair), NULL);
1273 waddwstr(view->window, wauthor);
1274 if (tc)
1275 wattr_off(view->window,
1276 COLOR_PAIR(tc->colorpair), NULL);
1277 col += author_width;
1278 while (col < avail && author_width < author_display_cols + 2) {
1279 waddch(view->window, ' ');
1280 col++;
1281 author_width++;
1283 if (col > avail)
1284 goto done;
1286 err = got_object_commit_get_logmsg(&logmsg0, commit);
1287 if (err)
1288 goto done;
1289 logmsg = logmsg0;
1290 while (*logmsg == '\n')
1291 logmsg++;
1292 newline = strchr(logmsg, '\n');
1293 if (newline)
1294 *newline = '\0';
1295 limit = avail - col;
1296 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1297 if (err)
1298 goto done;
1299 waddwstr(view->window, wlogmsg);
1300 col += logmsg_width;
1301 while (col < avail) {
1302 waddch(view->window, ' ');
1303 col++;
1305 done:
1306 free(logmsg0);
1307 free(wlogmsg);
1308 free(author);
1309 free(wauthor);
1310 free(line);
1311 return err;
1314 static struct commit_queue_entry *
1315 alloc_commit_queue_entry(struct got_commit_object *commit,
1316 struct got_object_id *id)
1318 struct commit_queue_entry *entry;
1320 entry = calloc(1, sizeof(*entry));
1321 if (entry == NULL)
1322 return NULL;
1324 entry->id = id;
1325 entry->commit = commit;
1326 return entry;
1329 static void
1330 pop_commit(struct commit_queue *commits)
1332 struct commit_queue_entry *entry;
1334 entry = TAILQ_FIRST(&commits->head);
1335 TAILQ_REMOVE(&commits->head, entry, entry);
1336 got_object_commit_close(entry->commit);
1337 commits->ncommits--;
1338 /* Don't free entry->id! It is owned by the commit graph. */
1339 free(entry);
1342 static void
1343 free_commits(struct commit_queue *commits)
1345 while (!TAILQ_EMPTY(&commits->head))
1346 pop_commit(commits);
1349 static const struct got_error *
1350 match_commit(int *have_match, struct got_object_id *id,
1351 struct got_commit_object *commit, regex_t *regex)
1353 const struct got_error *err = NULL;
1354 regmatch_t regmatch;
1355 char *id_str = NULL, *logmsg = NULL;
1357 *have_match = 0;
1359 err = got_object_id_str(&id_str, id);
1360 if (err)
1361 return err;
1363 err = got_object_commit_get_logmsg(&logmsg, commit);
1364 if (err)
1365 goto done;
1367 if (regexec(regex, got_object_commit_get_author(commit), 1,
1368 &regmatch, 0) == 0 ||
1369 regexec(regex, got_object_commit_get_committer(commit), 1,
1370 &regmatch, 0) == 0 ||
1371 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1372 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1373 *have_match = 1;
1374 done:
1375 free(id_str);
1376 free(logmsg);
1377 return err;
1380 static const struct got_error *
1381 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1382 int minqueue, struct got_repository *repo, const char *path,
1383 int *searching, int *search_next_done, regex_t *regex)
1385 const struct got_error *err = NULL;
1386 int nqueued = 0, have_match = 0;
1389 * We keep all commits open throughout the lifetime of the log
1390 * view in order to avoid having to re-fetch commits from disk
1391 * while updating the display.
1393 while (nqueued < minqueue ||
1394 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1395 struct got_object_id *id;
1396 struct got_commit_object *commit;
1397 struct commit_queue_entry *entry;
1398 int errcode;
1400 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1401 if (err || id == NULL)
1402 break;
1404 err = got_object_open_as_commit(&commit, repo, id);
1405 if (err)
1406 break;
1407 entry = alloc_commit_queue_entry(commit, id);
1408 if (entry == NULL) {
1409 err = got_error_from_errno("alloc_commit_queue_entry");
1410 break;
1413 errcode = pthread_mutex_lock(&tog_mutex);
1414 if (errcode) {
1415 err = got_error_set_errno(errcode,
1416 "pthread_mutex_lock");
1417 break;
1420 entry->idx = commits->ncommits;
1421 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1422 nqueued++;
1423 commits->ncommits++;
1425 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1426 err = match_commit(&have_match, id, commit, regex);
1427 if (err) {
1428 pthread_mutex_lock(&tog_mutex);
1429 break;
1433 errcode = pthread_mutex_unlock(&tog_mutex);
1434 if (errcode && err == NULL)
1435 err = got_error_set_errno(errcode,
1436 "pthread_mutex_unlock");
1438 if (have_match)
1439 break;
1442 return err;
1445 static const struct got_error *
1446 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1447 struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_reference *head_ref;
1452 *head_id = NULL;
1454 err = got_ref_open(&head_ref, repo, branch_name, 0);
1455 if (err)
1456 return err;
1458 err = got_ref_resolve(head_id, repo, head_ref);
1459 got_ref_close(head_ref);
1460 if (err) {
1461 *head_id = NULL;
1462 return err;
1465 return NULL;
1468 static const struct got_error *
1469 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 struct commit_queue *commits, int selected_idx, int limit,
1472 struct got_reflist_head *refs, const char *path, int commits_needed,
1473 struct tog_colors *colors)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 struct commit_queue_entry *entry;
1478 int width;
1479 int ncommits, author_cols = 4;
1480 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 char *refs_str = NULL;
1482 wchar_t *wline;
1483 struct tog_color *tc;
1484 static const size_t date_display_cols = 12;
1486 entry = first;
1487 ncommits = 0;
1488 while (entry) {
1489 if (ncommits == selected_idx) {
1490 *selected = entry;
1491 break;
1493 entry = TAILQ_NEXT(entry, entry);
1494 ncommits++;
1497 if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 err = got_object_id_str(&id_str, (*selected)->id);
1499 if (err)
1500 return err;
1501 if (refs) {
1502 err = build_refs_str(&refs_str, refs, (*selected)->id,
1503 s->repo);
1504 if (err)
1505 goto done;
1509 if (commits_needed == 0)
1510 halfdelay(10); /* disable fast refresh */
1512 if (asprintf(&ncommits_str, " [%d/%d] %s",
1513 entry ? entry->idx + 1 : 0, commits->ncommits,
1514 commits_needed > 0 ?
1515 (view->searching && view->search_next_done == 0
1516 ? "searching..." : "loading... ") :
1517 (refs_str ? refs_str : "")) == -1) {
1518 err = got_error_from_errno("asprintf");
1519 goto done;
1522 if (path && strcmp(path, "/") != 0) {
1523 if (asprintf(&header, "commit %s %s%s",
1524 id_str ? id_str : "........................................",
1525 path, ncommits_str) == -1) {
1526 err = got_error_from_errno("asprintf");
1527 header = NULL;
1528 goto done;
1530 } else if (asprintf(&header, "commit %s%s",
1531 id_str ? id_str : "........................................",
1532 ncommits_str) == -1) {
1533 err = got_error_from_errno("asprintf");
1534 header = NULL;
1535 goto done;
1537 err = format_line(&wline, &width, header, view->ncols, 0);
1538 if (err)
1539 goto done;
1541 werase(view->window);
1543 if (view_needs_focus_indication(view))
1544 wstandout(view->window);
1545 tc = get_color(colors, TOG_COLOR_COMMIT);
1546 if (tc)
1547 wattr_on(view->window,
1548 COLOR_PAIR(tc->colorpair), NULL);
1549 waddwstr(view->window, wline);
1550 if (tc)
1551 wattr_off(view->window,
1552 COLOR_PAIR(tc->colorpair), NULL);
1553 while (width < view->ncols) {
1554 waddch(view->window, ' ');
1555 width++;
1557 if (view_needs_focus_indication(view))
1558 wstandend(view->window);
1559 free(wline);
1560 if (limit <= 1)
1561 goto done;
1563 /* Grow author column size if necessary. */
1564 entry = first;
1565 ncommits = 0;
1566 while (entry) {
1567 char *author;
1568 wchar_t *wauthor;
1569 int width;
1570 if (ncommits >= limit - 1)
1571 break;
1572 author = strdup(got_object_commit_get_author(entry->commit));
1573 if (author == NULL) {
1574 err = got_error_from_errno("strdup");
1575 goto done;
1577 err = format_author(&wauthor, &width, author, COLS,
1578 date_display_cols);
1579 if (author_cols < width)
1580 author_cols = width;
1581 free(wauthor);
1582 free(author);
1583 ncommits++;
1584 entry = TAILQ_NEXT(entry, entry);
1587 entry = first;
1588 *last = first;
1589 ncommits = 0;
1590 while (entry) {
1591 if (ncommits >= limit - 1)
1592 break;
1593 if (ncommits == selected_idx)
1594 wstandout(view->window);
1595 err = draw_commit(view, entry->commit, entry->id, refs,
1596 date_display_cols, author_cols, colors);
1597 if (ncommits == selected_idx)
1598 wstandend(view->window);
1599 if (err)
1600 goto done;
1601 ncommits++;
1602 *last = entry;
1603 entry = TAILQ_NEXT(entry, entry);
1606 view_vborder(view);
1607 done:
1608 free(id_str);
1609 free(refs_str);
1610 free(ncommits_str);
1611 free(header);
1612 return err;
1615 static void
1616 scroll_up(struct tog_view *view,
1617 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1618 struct commit_queue *commits)
1620 struct commit_queue_entry *entry;
1621 int nscrolled = 0;
1623 entry = TAILQ_FIRST(&commits->head);
1624 if (*first_displayed_entry == entry)
1625 return;
1627 entry = *first_displayed_entry;
1628 while (entry && nscrolled < maxscroll) {
1629 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1630 if (entry) {
1631 *first_displayed_entry = entry;
1632 nscrolled++;
1637 static const struct got_error *
1638 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1639 pthread_cond_t *need_commits)
1641 int errcode;
1642 int max_wait = 20;
1644 halfdelay(1); /* fast refresh while loading commits */
1646 while (*commits_needed > 0) {
1647 if (*log_complete)
1648 break;
1650 /* Wake the log thread. */
1651 errcode = pthread_cond_signal(need_commits);
1652 if (errcode)
1653 return got_error_set_errno(errcode,
1654 "pthread_cond_signal");
1655 errcode = pthread_mutex_unlock(&tog_mutex);
1656 if (errcode)
1657 return got_error_set_errno(errcode,
1658 "pthread_mutex_unlock");
1659 pthread_yield();
1660 errcode = pthread_mutex_lock(&tog_mutex);
1661 if (errcode)
1662 return got_error_set_errno(errcode,
1663 "pthread_mutex_lock");
1665 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1667 * Thread is not done yet; lose a key press
1668 * and let the user retry... this way the GUI
1669 * remains interactive while logging deep paths
1670 * with few commits in history.
1672 return NULL;
1676 return NULL;
1679 static const struct got_error *
1680 scroll_down(struct tog_view *view,
1681 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1682 struct commit_queue_entry **last_displayed_entry,
1683 struct commit_queue *commits, int *log_complete, int *commits_needed,
1684 pthread_cond_t *need_commits)
1686 const struct got_error *err = NULL;
1687 struct commit_queue_entry *pentry;
1688 int nscrolled = 0;
1690 if (*last_displayed_entry == NULL)
1691 return NULL;
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 if (pentry == NULL && !*log_complete) {
1696 * Ask the log thread for required amount of commits
1697 * plus some amount of pre-fetching.
1699 (*commits_needed) += maxscroll + 20;
1700 err = trigger_log_thread(0, commits_needed, log_complete,
1701 need_commits);
1702 if (err)
1703 return err;
1706 do {
1707 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1708 if (pentry == NULL)
1709 break;
1711 *last_displayed_entry = pentry;
1713 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1714 if (pentry == NULL)
1715 break;
1716 *first_displayed_entry = pentry;
1717 } while (++nscrolled < maxscroll);
1719 return err;
1722 static const struct got_error *
1723 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1724 struct got_commit_object *commit, struct got_object_id *commit_id,
1725 struct tog_view *log_view, struct got_reflist_head *refs,
1726 struct got_repository *repo)
1728 const struct got_error *err;
1729 struct got_object_qid *parent_id;
1730 struct tog_view *diff_view;
1732 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1733 if (diff_view == NULL)
1734 return got_error_from_errno("view_open");
1736 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1737 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1738 commit_id, log_view, refs, repo);
1739 if (err == NULL)
1740 *new_view = diff_view;
1741 return err;
1744 static const struct got_error *
1745 tree_view_visit_subtree(struct got_tree_object *subtree,
1746 struct tog_tree_view_state *s)
1748 struct tog_parent_tree *parent;
1750 parent = calloc(1, sizeof(*parent));
1751 if (parent == NULL)
1752 return got_error_from_errno("calloc");
1754 parent->tree = s->tree;
1755 parent->first_displayed_entry = s->first_displayed_entry;
1756 parent->selected_entry = s->selected_entry;
1757 parent->selected = s->selected;
1758 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1759 s->tree = subtree;
1760 s->selected = 0;
1761 s->first_displayed_entry = NULL;
1762 return NULL;
1766 static const struct got_error *
1767 browse_commit_tree(struct tog_view **new_view, int begin_x,
1768 struct commit_queue_entry *entry, const char *path,
1769 struct got_reflist_head *refs, struct got_repository *repo)
1771 const struct got_error *err = NULL;
1772 struct got_tree_object *tree;
1773 struct tog_tree_view_state *s;
1774 struct tog_view *tree_view;
1775 char *slash, *subpath = NULL;
1776 const char *p;
1778 err = got_object_open_as_tree(&tree, repo,
1779 got_object_commit_get_tree_id(entry->commit));
1780 if (err)
1781 return err;
1783 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1784 if (tree_view == NULL)
1785 return got_error_from_errno("view_open");
1787 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1788 if (err) {
1789 got_object_tree_close(tree);
1790 return err;
1792 s = &tree_view->state.tree;
1794 *new_view = tree_view;
1796 if (got_path_is_root_dir(path))
1797 return NULL;
1799 /* Walk the path and open corresponding tree objects. */
1800 p = path;
1801 while (*p) {
1802 struct got_tree_entry *te;
1803 struct got_object_id *tree_id;
1804 char *te_name;
1806 while (p[0] == '/')
1807 p++;
1809 /* Ensure the correct subtree entry is selected. */
1810 slash = strchr(p, '/');
1811 if (slash == NULL)
1812 te_name = strdup(p);
1813 else
1814 te_name = strndup(p, slash - p);
1815 if (te_name == NULL) {
1816 err = got_error_from_errno("strndup");
1817 break;
1819 te = got_object_tree_find_entry(s->tree, te_name);
1820 if (te == NULL) {
1821 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1822 free(te_name);
1823 break;
1825 free(te_name);
1826 s->selected_entry = te;
1827 s->selected = got_tree_entry_get_index(te);
1828 if (s->tree != s->root)
1829 s->selected++; /* skip '..' */
1831 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1832 /* Jump to this file's entry. */
1833 s->first_displayed_entry = s->selected_entry;
1834 s->selected = 0;
1835 break;
1838 slash = strchr(p, '/');
1839 if (slash)
1840 subpath = strndup(path, slash - path);
1841 else
1842 subpath = strdup(path);
1843 if (subpath == NULL) {
1844 err = got_error_from_errno("strdup");
1845 break;
1848 err = got_object_id_by_path(&tree_id, repo, entry->id,
1849 subpath);
1850 if (err)
1851 break;
1853 err = got_object_open_as_tree(&tree, repo, tree_id);
1854 free(tree_id);
1855 if (err)
1856 break;
1858 err = tree_view_visit_subtree(tree, s);
1859 if (err) {
1860 got_object_tree_close(tree);
1861 break;
1863 if (slash == NULL)
1864 break;
1865 free(subpath);
1866 subpath = NULL;
1867 p = slash;
1870 free(subpath);
1871 return err;
1874 static void *
1875 log_thread(void *arg)
1877 const struct got_error *err = NULL;
1878 int errcode = 0;
1879 struct tog_log_thread_args *a = arg;
1880 int done = 0;
1882 while (!done && !err && !tog_sigpipe_received) {
1883 err = queue_commits(a->graph, a->commits, 1, a->repo,
1884 a->in_repo_path, a->searching, a->search_next_done,
1885 a->regex);
1886 if (err) {
1887 if (err->code != GOT_ERR_ITER_COMPLETED)
1888 return (void *)err;
1889 err = NULL;
1890 done = 1;
1891 } else if (a->commits_needed > 0)
1892 a->commits_needed--;
1894 errcode = pthread_mutex_lock(&tog_mutex);
1895 if (errcode) {
1896 err = got_error_set_errno(errcode,
1897 "pthread_mutex_lock");
1898 break;
1899 } else if (*a->quit)
1900 done = 1;
1901 else if (*a->first_displayed_entry == NULL) {
1902 *a->first_displayed_entry =
1903 TAILQ_FIRST(&a->commits->head);
1904 *a->selected_entry = *a->first_displayed_entry;
1907 if (done)
1908 a->commits_needed = 0;
1909 else if (a->commits_needed == 0) {
1910 errcode = pthread_cond_wait(&a->need_commits,
1911 &tog_mutex);
1912 if (errcode)
1913 err = got_error_set_errno(errcode,
1914 "pthread_cond_wait");
1917 errcode = pthread_mutex_unlock(&tog_mutex);
1918 if (errcode && err == NULL)
1919 err = got_error_set_errno(errcode,
1920 "pthread_mutex_unlock");
1922 a->log_complete = 1;
1923 return (void *)err;
1926 static const struct got_error *
1927 stop_log_thread(struct tog_log_view_state *s)
1929 const struct got_error *err = NULL;
1930 int errcode;
1932 if (s->thread) {
1933 s->quit = 1;
1934 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1935 if (errcode)
1936 return got_error_set_errno(errcode,
1937 "pthread_cond_signal");
1938 errcode = pthread_mutex_unlock(&tog_mutex);
1939 if (errcode)
1940 return got_error_set_errno(errcode,
1941 "pthread_mutex_unlock");
1942 errcode = pthread_join(s->thread, (void **)&err);
1943 if (errcode)
1944 return got_error_set_errno(errcode, "pthread_join");
1945 errcode = pthread_mutex_lock(&tog_mutex);
1946 if (errcode)
1947 return got_error_set_errno(errcode,
1948 "pthread_mutex_lock");
1949 s->thread = NULL;
1952 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1953 if (errcode && err == NULL)
1954 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1956 if (s->thread_args.repo) {
1957 got_repo_close(s->thread_args.repo);
1958 s->thread_args.repo = NULL;
1961 if (s->thread_args.graph) {
1962 got_commit_graph_close(s->thread_args.graph);
1963 s->thread_args.graph = NULL;
1966 return err;
1969 static const struct got_error *
1970 close_log_view(struct tog_view *view)
1972 const struct got_error *err = NULL;
1973 struct tog_log_view_state *s = &view->state.log;
1975 err = stop_log_thread(s);
1976 free_commits(&s->commits);
1977 free(s->in_repo_path);
1978 s->in_repo_path = NULL;
1979 free(s->start_id);
1980 s->start_id = NULL;
1981 return err;
1984 static const struct got_error *
1985 search_start_log_view(struct tog_view *view)
1987 struct tog_log_view_state *s = &view->state.log;
1989 s->matched_entry = NULL;
1990 s->search_entry = NULL;
1991 return NULL;
1994 static const struct got_error *
1995 search_next_log_view(struct tog_view *view)
1997 const struct got_error *err = NULL;
1998 struct tog_log_view_state *s = &view->state.log;
1999 struct commit_queue_entry *entry;
2001 if (!view->searching) {
2002 view->search_next_done = 1;
2003 return NULL;
2006 if (s->search_entry) {
2007 int errcode, ch;
2008 errcode = pthread_mutex_unlock(&tog_mutex);
2009 if (errcode)
2010 return got_error_set_errno(errcode,
2011 "pthread_mutex_unlock");
2012 ch = wgetch(view->window);
2013 errcode = pthread_mutex_lock(&tog_mutex);
2014 if (errcode)
2015 return got_error_set_errno(errcode,
2016 "pthread_mutex_lock");
2017 if (ch == KEY_BACKSPACE) {
2018 view->search_next_done = 1;
2019 return NULL;
2021 if (view->searching == TOG_SEARCH_FORWARD)
2022 entry = TAILQ_NEXT(s->search_entry, entry);
2023 else
2024 entry = TAILQ_PREV(s->search_entry,
2025 commit_queue_head, entry);
2026 } else if (s->matched_entry) {
2027 if (view->searching == TOG_SEARCH_FORWARD)
2028 entry = TAILQ_NEXT(s->selected_entry, entry);
2029 else
2030 entry = TAILQ_PREV(s->selected_entry,
2031 commit_queue_head, entry);
2032 } else {
2033 if (view->searching == TOG_SEARCH_FORWARD)
2034 entry = TAILQ_FIRST(&s->commits.head);
2035 else
2036 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2039 while (1) {
2040 int have_match = 0;
2042 if (entry == NULL) {
2043 if (s->thread_args.log_complete ||
2044 view->searching == TOG_SEARCH_BACKWARD) {
2045 view->search_next_done = 1;
2046 return NULL;
2049 * Poke the log thread for more commits and return,
2050 * allowing the main loop to make progress. Search
2051 * will resume at s->search_entry once we come back.
2053 s->thread_args.commits_needed++;
2054 return trigger_log_thread(1,
2055 &s->thread_args.commits_needed,
2056 &s->thread_args.log_complete,
2057 &s->thread_args.need_commits);
2060 err = match_commit(&have_match, entry->id, entry->commit,
2061 &view->regex);
2062 if (err)
2063 break;
2064 if (have_match) {
2065 view->search_next_done = 1;
2066 s->matched_entry = entry;
2067 break;
2070 s->search_entry = entry;
2071 if (view->searching == TOG_SEARCH_FORWARD)
2072 entry = TAILQ_NEXT(entry, entry);
2073 else
2074 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2077 if (s->matched_entry) {
2078 int cur = s->selected_entry->idx;
2079 while (cur < s->matched_entry->idx) {
2080 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2081 if (err)
2082 return err;
2083 cur++;
2085 while (cur > s->matched_entry->idx) {
2086 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2087 if (err)
2088 return err;
2089 cur--;
2093 s->search_entry = NULL;
2095 return NULL;
2098 static const struct got_error *
2099 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2100 struct got_reflist_head *refs, struct got_repository *repo,
2101 const char *head_ref_name, const char *path, int check_disk)
2103 const struct got_error *err = NULL;
2104 struct tog_log_view_state *s = &view->state.log;
2105 struct got_repository *thread_repo = NULL;
2106 struct got_commit_graph *thread_graph = NULL;
2107 int errcode;
2109 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2110 if (err != NULL)
2111 goto done;
2113 /* The commit queue only contains commits being displayed. */
2114 TAILQ_INIT(&s->commits.head);
2115 s->commits.ncommits = 0;
2117 s->refs = refs;
2118 s->repo = repo;
2119 s->head_ref_name = head_ref_name;
2120 s->start_id = got_object_id_dup(start_id);
2121 if (s->start_id == NULL) {
2122 err = got_error_from_errno("got_object_id_dup");
2123 goto done;
2126 SIMPLEQ_INIT(&s->colors);
2127 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2128 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2129 get_color_value("TOG_COLOR_COMMIT"));
2130 if (err)
2131 goto done;
2132 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2133 get_color_value("TOG_COLOR_AUTHOR"));
2134 if (err) {
2135 free_colors(&s->colors);
2136 goto done;
2138 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2139 get_color_value("TOG_COLOR_DATE"));
2140 if (err) {
2141 free_colors(&s->colors);
2142 goto done;
2146 view->show = show_log_view;
2147 view->input = input_log_view;
2148 view->close = close_log_view;
2149 view->search_start = search_start_log_view;
2150 view->search_next = search_next_log_view;
2152 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2153 if (err)
2154 goto done;
2155 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2156 if (err)
2157 goto done;
2158 err = got_commit_graph_iter_start(thread_graph,
2159 s->start_id, s->repo, NULL, NULL);
2160 if (err)
2161 goto done;
2163 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2164 if (errcode) {
2165 err = got_error_set_errno(errcode, "pthread_cond_init");
2166 goto done;
2169 s->thread_args.commits_needed = view->nlines;
2170 s->thread_args.graph = thread_graph;
2171 s->thread_args.commits = &s->commits;
2172 s->thread_args.in_repo_path = s->in_repo_path;
2173 s->thread_args.start_id = s->start_id;
2174 s->thread_args.repo = thread_repo;
2175 s->thread_args.log_complete = 0;
2176 s->thread_args.quit = &s->quit;
2177 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2178 s->thread_args.selected_entry = &s->selected_entry;
2179 s->thread_args.searching = &view->searching;
2180 s->thread_args.search_next_done = &view->search_next_done;
2181 s->thread_args.regex = &view->regex;
2182 done:
2183 if (err)
2184 close_log_view(view);
2185 return err;
2188 static const struct got_error *
2189 show_log_view(struct tog_view *view)
2191 struct tog_log_view_state *s = &view->state.log;
2193 if (s->thread == NULL) {
2194 int errcode = pthread_create(&s->thread, NULL, log_thread,
2195 &s->thread_args);
2196 if (errcode)
2197 return got_error_set_errno(errcode, "pthread_create");
2200 return draw_commits(view, &s->last_displayed_entry,
2201 &s->selected_entry, s->first_displayed_entry,
2202 &s->commits, s->selected, view->nlines, s->refs,
2203 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2206 static const struct got_error *
2207 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2208 struct tog_view **focus_view, struct tog_view *view, int ch)
2210 const struct got_error *err = NULL;
2211 struct tog_log_view_state *s = &view->state.log;
2212 char *parent_path, *in_repo_path = NULL;
2213 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2214 int begin_x = 0;
2215 struct got_object_id *start_id;
2217 switch (ch) {
2218 case 'q':
2219 s->quit = 1;
2220 break;
2221 case 'k':
2222 case KEY_UP:
2223 case '<':
2224 case ',':
2225 if (s->first_displayed_entry == NULL)
2226 break;
2227 if (s->selected > 0)
2228 s->selected--;
2229 else
2230 scroll_up(view, &s->first_displayed_entry, 1,
2231 &s->commits);
2232 break;
2233 case KEY_PPAGE:
2234 case CTRL('b'):
2235 if (s->first_displayed_entry == NULL)
2236 break;
2237 if (TAILQ_FIRST(&s->commits.head) ==
2238 s->first_displayed_entry) {
2239 s->selected = 0;
2240 break;
2242 scroll_up(view, &s->first_displayed_entry,
2243 view->nlines, &s->commits);
2244 break;
2245 case 'j':
2246 case KEY_DOWN:
2247 case '>':
2248 case '.':
2249 if (s->first_displayed_entry == NULL)
2250 break;
2251 if (s->selected < MIN(view->nlines - 2,
2252 s->commits.ncommits - 1)) {
2253 s->selected++;
2254 break;
2256 err = scroll_down(view, &s->first_displayed_entry, 1,
2257 &s->last_displayed_entry, &s->commits,
2258 &s->thread_args.log_complete,
2259 &s->thread_args.commits_needed,
2260 &s->thread_args.need_commits);
2261 break;
2262 case KEY_NPAGE:
2263 case CTRL('f'): {
2264 struct commit_queue_entry *first;
2265 first = s->first_displayed_entry;
2266 if (first == NULL)
2267 break;
2268 err = scroll_down(view, &s->first_displayed_entry,
2269 view->nlines, &s->last_displayed_entry,
2270 &s->commits, &s->thread_args.log_complete,
2271 &s->thread_args.commits_needed,
2272 &s->thread_args.need_commits);
2273 if (err)
2274 break;
2275 if (first == s->first_displayed_entry &&
2276 s->selected < MIN(view->nlines - 2,
2277 s->commits.ncommits - 1)) {
2278 /* can't scroll further down */
2279 s->selected = MIN(view->nlines - 2,
2280 s->commits.ncommits - 1);
2282 err = NULL;
2283 break;
2285 case KEY_RESIZE:
2286 if (s->selected > view->nlines - 2)
2287 s->selected = view->nlines - 2;
2288 if (s->selected > s->commits.ncommits - 1)
2289 s->selected = s->commits.ncommits - 1;
2290 break;
2291 case KEY_ENTER:
2292 case ' ':
2293 case '\r':
2294 if (s->selected_entry == NULL)
2295 break;
2296 if (view_is_parent_view(view))
2297 begin_x = view_split_begin_x(view->begin_x);
2298 err = open_diff_view_for_commit(&diff_view, begin_x,
2299 s->selected_entry->commit, s->selected_entry->id,
2300 view, s->refs, s->repo);
2301 if (err)
2302 break;
2303 if (view_is_parent_view(view)) {
2304 err = view_close_child(view);
2305 if (err)
2306 return err;
2307 err = view_set_child(view, diff_view);
2308 if (err) {
2309 view_close(diff_view);
2310 break;
2312 *focus_view = diff_view;
2313 view->child_focussed = 1;
2314 } else
2315 *new_view = diff_view;
2316 break;
2317 case 't':
2318 if (s->selected_entry == NULL)
2319 break;
2320 if (view_is_parent_view(view))
2321 begin_x = view_split_begin_x(view->begin_x);
2322 err = browse_commit_tree(&tree_view, begin_x,
2323 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2324 if (err)
2325 break;
2326 if (view_is_parent_view(view)) {
2327 err = view_close_child(view);
2328 if (err)
2329 return err;
2330 err = view_set_child(view, tree_view);
2331 if (err) {
2332 view_close(tree_view);
2333 break;
2335 *focus_view = tree_view;
2336 view->child_focussed = 1;
2337 } else
2338 *new_view = tree_view;
2339 break;
2340 case KEY_BACKSPACE:
2341 if (strcmp(s->in_repo_path, "/") == 0)
2342 break;
2343 parent_path = dirname(s->in_repo_path);
2344 if (parent_path && strcmp(parent_path, ".") != 0) {
2345 err = stop_log_thread(s);
2346 if (err)
2347 return err;
2348 lv = view_open(view->nlines, view->ncols,
2349 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2350 if (lv == NULL)
2351 return got_error_from_errno(
2352 "view_open");
2353 err = open_log_view(lv, s->start_id, s->refs,
2354 s->repo, s->head_ref_name, parent_path, 0);
2355 if (err)
2356 return err;;
2357 if (view_is_parent_view(view))
2358 *new_view = lv;
2359 else {
2360 view_set_child(view->parent, lv);
2361 *focus_view = lv;
2363 return NULL;
2365 break;
2366 case CTRL('l'):
2367 err = stop_log_thread(s);
2368 if (err)
2369 return err;
2370 lv = view_open(view->nlines, view->ncols,
2371 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2372 if (lv == NULL)
2373 return got_error_from_errno("view_open");
2374 err = get_head_commit_id(&start_id, s->head_ref_name ?
2375 s->head_ref_name : GOT_REF_HEAD, s->repo);
2376 if (err) {
2377 view_close(lv);
2378 return err;
2380 in_repo_path = strdup(s->in_repo_path);
2381 if (in_repo_path == NULL) {
2382 free(start_id);
2383 view_close(lv);
2384 return got_error_from_errno("strdup");
2386 got_ref_list_free(s->refs);
2387 err = got_ref_list(s->refs, s->repo, NULL,
2388 got_ref_cmp_by_name, NULL);
2389 if (err) {
2390 free(start_id);
2391 view_close(lv);
2392 return err;
2394 err = open_log_view(lv, start_id, s->refs, s->repo,
2395 s->head_ref_name, in_repo_path, 0);
2396 if (err) {
2397 free(start_id);
2398 view_close(lv);
2399 return err;;
2401 *dead_view = view;
2402 *new_view = lv;
2403 break;
2404 default:
2405 break;
2408 return err;
2411 static const struct got_error *
2412 apply_unveil(const char *repo_path, const char *worktree_path)
2414 const struct got_error *error;
2416 #ifdef PROFILE
2417 if (unveil("gmon.out", "rwc") != 0)
2418 return got_error_from_errno2("unveil", "gmon.out");
2419 #endif
2420 if (repo_path && unveil(repo_path, "r") != 0)
2421 return got_error_from_errno2("unveil", repo_path);
2423 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2424 return got_error_from_errno2("unveil", worktree_path);
2426 if (unveil("/tmp", "rwc") != 0)
2427 return got_error_from_errno2("unveil", "/tmp");
2429 error = got_privsep_unveil_exec_helpers();
2430 if (error != NULL)
2431 return error;
2433 if (unveil(NULL, NULL) != 0)
2434 return got_error_from_errno("unveil");
2436 return NULL;
2439 static void
2440 init_curses(void)
2442 initscr();
2443 cbreak();
2444 halfdelay(1); /* Do fast refresh while initial view is loading. */
2445 noecho();
2446 nonl();
2447 intrflush(stdscr, FALSE);
2448 keypad(stdscr, TRUE);
2449 curs_set(0);
2450 if (getenv("TOG_COLORS") != NULL) {
2451 start_color();
2452 use_default_colors();
2454 signal(SIGWINCH, tog_sigwinch);
2455 signal(SIGPIPE, tog_sigpipe);
2458 static const struct got_error *
2459 cmd_log(int argc, char *argv[])
2461 const struct got_error *error;
2462 struct got_repository *repo = NULL;
2463 struct got_worktree *worktree = NULL;
2464 struct got_reflist_head refs;
2465 struct got_object_id *start_id = NULL;
2466 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2467 char *start_commit = NULL, *head_ref_name = NULL;
2468 int ch;
2469 struct tog_view *view;
2471 SIMPLEQ_INIT(&refs);
2473 #ifndef PROFILE
2474 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2475 NULL) == -1)
2476 err(1, "pledge");
2477 #endif
2479 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2480 switch (ch) {
2481 case 'c':
2482 start_commit = optarg;
2483 break;
2484 case 'r':
2485 repo_path = realpath(optarg, NULL);
2486 if (repo_path == NULL)
2487 return got_error_from_errno2("realpath",
2488 optarg);
2489 break;
2490 default:
2491 usage_log();
2492 /* NOTREACHED */
2496 argc -= optind;
2497 argv += optind;
2499 cwd = getcwd(NULL, 0);
2500 if (cwd == NULL) {
2501 error = got_error_from_errno("getcwd");
2502 goto done;
2504 error = got_worktree_open(&worktree, cwd);
2505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2506 goto done;
2507 error = NULL;
2509 if (argc == 0) {
2510 path = strdup("");
2511 if (path == NULL) {
2512 error = got_error_from_errno("strdup");
2513 goto done;
2515 } else if (argc == 1) {
2516 if (worktree) {
2517 error = got_worktree_resolve_path(&path, worktree,
2518 argv[0]);
2519 if (error)
2520 goto done;
2521 } else {
2522 path = strdup(argv[0]);
2523 if (path == NULL) {
2524 error = got_error_from_errno("strdup");
2525 goto done;
2528 } else
2529 usage_log();
2531 if (repo_path == NULL) {
2532 if (worktree)
2533 repo_path = strdup(
2534 got_worktree_get_repo_path(worktree));
2535 else
2536 repo_path = strdup(cwd);
2538 if (repo_path == NULL) {
2539 error = got_error_from_errno("strdup");
2540 goto done;
2543 init_curses();
2545 error = got_repo_open(&repo, repo_path, NULL);
2546 if (error != NULL)
2547 goto done;
2549 error = apply_unveil(got_repo_get_path(repo),
2550 worktree ? got_worktree_get_root_path(worktree) : NULL);
2551 if (error)
2552 goto done;
2554 if (start_commit == NULL)
2555 error = get_head_commit_id(&start_id, worktree ?
2556 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2557 repo);
2558 else {
2559 error = get_head_commit_id(&start_id, start_commit, repo);
2560 if (error) {
2561 if (error->code != GOT_ERR_NOT_REF)
2562 goto done;
2563 error = got_repo_match_object_id_prefix(&start_id,
2564 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2567 if (error != NULL)
2568 goto done;
2570 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2571 if (error)
2572 goto done;
2574 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2575 if (view == NULL) {
2576 error = got_error_from_errno("view_open");
2577 goto done;
2579 if (worktree) {
2580 head_ref_name = strdup(
2581 got_worktree_get_head_ref_name(worktree));
2582 if (head_ref_name == NULL) {
2583 error = got_error_from_errno("strdup");
2584 goto done;
2587 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2588 path, 1);
2589 if (error)
2590 goto done;
2591 if (worktree) {
2592 /* Release work tree lock. */
2593 got_worktree_close(worktree);
2594 worktree = NULL;
2596 error = view_loop(view);
2597 done:
2598 free(repo_path);
2599 free(cwd);
2600 free(path);
2601 free(start_id);
2602 free(head_ref_name);
2603 if (repo)
2604 got_repo_close(repo);
2605 if (worktree)
2606 got_worktree_close(worktree);
2607 got_ref_list_free(&refs);
2608 return error;
2611 __dead static void
2612 usage_diff(void)
2614 endwin();
2615 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2616 getprogname());
2617 exit(1);
2620 static char *
2621 parse_next_line(FILE *f, size_t *len)
2623 char *line;
2624 size_t linelen;
2625 size_t lineno;
2626 const char delim[3] = { '\0', '\0', '\0'};
2628 line = fparseln(f, &linelen, &lineno, delim, 0);
2629 if (len)
2630 *len = linelen;
2631 return line;
2634 static int
2635 match_line(const char *line, regex_t *regex)
2637 regmatch_t regmatch;
2639 return regexec(regex, line, 1, &regmatch, 0) == 0;
2642 struct tog_color *
2643 match_color(struct tog_colors *colors, const char *line)
2645 struct tog_color *tc = NULL;
2647 SIMPLEQ_FOREACH(tc, colors, entry) {
2648 if (match_line(line, &tc->regex))
2649 return tc;
2652 return NULL;
2655 static const struct got_error *
2656 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2657 int *last_displayed_line, int *eof, int max_lines, char *header,
2658 struct tog_colors *colors)
2660 const struct got_error *err;
2661 int nlines = 0, nprinted = 0;
2662 char *line;
2663 struct tog_color *tc;
2664 size_t len;
2665 wchar_t *wline;
2666 int width;
2668 rewind(f);
2669 werase(view->window);
2671 if (header) {
2672 err = format_line(&wline, &width, header, view->ncols, 0);
2673 if (err) {
2674 return err;
2677 if (view_needs_focus_indication(view))
2678 wstandout(view->window);
2679 waddwstr(view->window, wline);
2680 if (view_needs_focus_indication(view))
2681 wstandend(view->window);
2682 if (width <= view->ncols - 1)
2683 waddch(view->window, '\n');
2685 if (max_lines <= 1)
2686 return NULL;
2687 max_lines--;
2690 *eof = 0;
2691 while (nprinted < max_lines) {
2692 line = parse_next_line(f, &len);
2693 if (line == NULL) {
2694 *eof = 1;
2695 break;
2697 if (++nlines < *first_displayed_line) {
2698 free(line);
2699 continue;
2702 err = format_line(&wline, &width, line, view->ncols, 0);
2703 if (err) {
2704 free(line);
2705 return err;
2708 tc = match_color(colors, line);
2709 if (tc)
2710 wattr_on(view->window,
2711 COLOR_PAIR(tc->colorpair), NULL);
2712 waddwstr(view->window, wline);
2713 if (tc)
2714 wattr_off(view->window,
2715 COLOR_PAIR(tc->colorpair), NULL);
2716 if (width <= view->ncols - 1)
2717 waddch(view->window, '\n');
2718 if (++nprinted == 1)
2719 *first_displayed_line = nlines;
2720 free(line);
2721 free(wline);
2722 wline = NULL;
2724 *last_displayed_line = nlines;
2726 view_vborder(view);
2728 if (*eof) {
2729 while (nprinted < view->nlines) {
2730 waddch(view->window, '\n');
2731 nprinted++;
2734 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2735 if (err) {
2736 return err;
2739 wstandout(view->window);
2740 waddwstr(view->window, wline);
2741 wstandend(view->window);
2744 return NULL;
2747 static char *
2748 get_datestr(time_t *time, char *datebuf)
2750 struct tm mytm, *tm;
2751 char *p, *s;
2753 tm = gmtime_r(time, &mytm);
2754 if (tm == NULL)
2755 return NULL;
2756 s = asctime_r(tm, datebuf);
2757 if (s == NULL)
2758 return NULL;
2759 p = strchr(s, '\n');
2760 if (p)
2761 *p = '\0';
2762 return s;
2765 static const struct got_error *
2766 write_commit_info(struct got_object_id *commit_id,
2767 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2769 const struct got_error *err = NULL;
2770 char datebuf[26], *datestr;
2771 struct got_commit_object *commit;
2772 char *id_str = NULL, *logmsg = NULL;
2773 time_t committer_time;
2774 const char *author, *committer;
2775 char *refs_str = NULL;
2777 if (refs) {
2778 err = build_refs_str(&refs_str, refs, commit_id, repo);
2779 if (err)
2780 return err;
2783 err = got_object_open_as_commit(&commit, repo, commit_id);
2784 if (err)
2785 return err;
2787 err = got_object_id_str(&id_str, commit_id);
2788 if (err) {
2789 err = got_error_from_errno("got_object_id_str");
2790 goto done;
2793 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2794 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2795 err = got_error_from_errno("fprintf");
2796 goto done;
2798 if (fprintf(outfile, "from: %s\n",
2799 got_object_commit_get_author(commit)) < 0) {
2800 err = got_error_from_errno("fprintf");
2801 goto done;
2803 committer_time = got_object_commit_get_committer_time(commit);
2804 datestr = get_datestr(&committer_time, datebuf);
2805 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2806 err = got_error_from_errno("fprintf");
2807 goto done;
2809 author = got_object_commit_get_author(commit);
2810 committer = got_object_commit_get_committer(commit);
2811 if (strcmp(author, committer) != 0 &&
2812 fprintf(outfile, "via: %s\n", committer) < 0) {
2813 err = got_error_from_errno("fprintf");
2814 goto done;
2816 err = got_object_commit_get_logmsg(&logmsg, commit);
2817 if (err)
2818 goto done;
2819 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2820 err = got_error_from_errno("fprintf");
2821 goto done;
2823 done:
2824 free(id_str);
2825 free(logmsg);
2826 free(refs_str);
2827 got_object_commit_close(commit);
2828 return err;
2831 static const struct got_error *
2832 create_diff(struct tog_diff_view_state *s)
2834 const struct got_error *err = NULL;
2835 FILE *f = NULL;
2836 int obj_type;
2838 f = got_opentemp();
2839 if (f == NULL) {
2840 err = got_error_from_errno("got_opentemp");
2841 goto done;
2843 if (s->f && fclose(s->f) != 0) {
2844 err = got_error_from_errno("fclose");
2845 goto done;
2847 s->f = f;
2849 if (s->id1)
2850 err = got_object_get_type(&obj_type, s->repo, s->id1);
2851 else
2852 err = got_object_get_type(&obj_type, s->repo, s->id2);
2853 if (err)
2854 goto done;
2856 switch (obj_type) {
2857 case GOT_OBJ_TYPE_BLOB:
2858 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2859 s->diff_context, 0, s->repo, f);
2860 break;
2861 case GOT_OBJ_TYPE_TREE:
2862 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2863 s->diff_context, 0, s->repo, f);
2864 break;
2865 case GOT_OBJ_TYPE_COMMIT: {
2866 const struct got_object_id_queue *parent_ids;
2867 struct got_object_qid *pid;
2868 struct got_commit_object *commit2;
2870 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2871 if (err)
2872 break;
2873 /* Show commit info if we're diffing to a parent/root commit. */
2874 if (s->id1 == NULL)
2875 write_commit_info(s->id2, s->refs, s->repo, f);
2876 else {
2877 parent_ids = got_object_commit_get_parent_ids(commit2);
2878 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2879 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2880 write_commit_info(s->id2, s->refs,
2881 s->repo, f);
2882 break;
2886 got_object_commit_close(commit2);
2888 err = got_diff_objects_as_commits(s->id1, s->id2,
2889 s->diff_context, 0, s->repo, f);
2890 break;
2892 default:
2893 err = got_error(GOT_ERR_OBJ_TYPE);
2894 break;
2896 done:
2897 if (f && fflush(f) != 0 && err == NULL)
2898 err = got_error_from_errno("fflush");
2899 return err;
2902 static void
2903 diff_view_indicate_progress(struct tog_view *view)
2905 mvwaddstr(view->window, 0, 0, "diffing...");
2906 update_panels();
2907 doupdate();
2910 static const struct got_error *
2911 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2912 struct got_object_id *id2, struct tog_view *log_view,
2913 struct got_reflist_head *refs, struct got_repository *repo)
2915 const struct got_error *err;
2917 if (id1 != NULL && id2 != NULL) {
2918 int type1, type2;
2919 err = got_object_get_type(&type1, repo, id1);
2920 if (err)
2921 return err;
2922 err = got_object_get_type(&type2, repo, id2);
2923 if (err)
2924 return err;
2926 if (type1 != type2)
2927 return got_error(GOT_ERR_OBJ_TYPE);
2930 if (id1) {
2931 view->state.diff.id1 = got_object_id_dup(id1);
2932 if (view->state.diff.id1 == NULL)
2933 return got_error_from_errno("got_object_id_dup");
2934 } else
2935 view->state.diff.id1 = NULL;
2937 view->state.diff.id2 = got_object_id_dup(id2);
2938 if (view->state.diff.id2 == NULL) {
2939 free(view->state.diff.id1);
2940 view->state.diff.id1 = NULL;
2941 return got_error_from_errno("got_object_id_dup");
2943 view->state.diff.f = NULL;
2944 view->state.diff.first_displayed_line = 1;
2945 view->state.diff.last_displayed_line = view->nlines;
2946 view->state.diff.diff_context = 3;
2947 view->state.diff.log_view = log_view;
2948 view->state.diff.repo = repo;
2949 view->state.diff.refs = refs;
2950 SIMPLEQ_INIT(&view->state.diff.colors);
2952 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2953 err = add_color(&view->state.diff.colors,
2954 "^-", TOG_COLOR_DIFF_MINUS,
2955 get_color_value("TOG_COLOR_DIFF_MINUS"));
2956 if (err)
2957 return err;
2958 err = add_color(&view->state.diff.colors, "^\\+",
2959 TOG_COLOR_DIFF_PLUS,
2960 get_color_value("TOG_COLOR_DIFF_PLUS"));
2961 if (err) {
2962 free_colors(&view->state.diff.colors);
2963 return err;
2965 err = add_color(&view->state.diff.colors,
2966 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2967 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2968 if (err) {
2969 free_colors(&view->state.diff.colors);
2970 return err;
2973 err = add_color(&view->state.diff.colors,
2974 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2975 get_color_value("TOG_COLOR_DIFF_META"));
2976 if (err) {
2977 free_colors(&view->state.diff.colors);
2978 return err;
2981 err = add_color(&view->state.diff.colors,
2982 "^(from|via): ", TOG_COLOR_AUTHOR,
2983 get_color_value("TOG_COLOR_AUTHOR"));
2984 if (err) {
2985 free_colors(&view->state.diff.colors);
2986 return err;
2989 err = add_color(&view->state.diff.colors,
2990 "^date: ", TOG_COLOR_DATE,
2991 get_color_value("TOG_COLOR_DATE"));
2992 if (err) {
2993 free_colors(&view->state.diff.colors);
2994 return err;
2998 if (log_view && view_is_splitscreen(view))
2999 show_log_view(log_view); /* draw vborder */
3000 diff_view_indicate_progress(view);
3002 err = create_diff(&view->state.diff);
3003 if (err) {
3004 free(view->state.diff.id1);
3005 view->state.diff.id1 = NULL;
3006 free(view->state.diff.id2);
3007 view->state.diff.id2 = NULL;
3008 return err;
3011 view->show = show_diff_view;
3012 view->input = input_diff_view;
3013 view->close = close_diff_view;
3015 return NULL;
3018 static const struct got_error *
3019 close_diff_view(struct tog_view *view)
3021 const struct got_error *err = NULL;
3023 free(view->state.diff.id1);
3024 view->state.diff.id1 = NULL;
3025 free(view->state.diff.id2);
3026 view->state.diff.id2 = NULL;
3027 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3028 err = got_error_from_errno("fclose");
3029 free_colors(&view->state.diff.colors);
3030 return err;
3033 static const struct got_error *
3034 show_diff_view(struct tog_view *view)
3036 const struct got_error *err;
3037 struct tog_diff_view_state *s = &view->state.diff;
3038 char *id_str1 = NULL, *id_str2, *header;
3040 if (s->id1) {
3041 err = got_object_id_str(&id_str1, s->id1);
3042 if (err)
3043 return err;
3045 err = got_object_id_str(&id_str2, s->id2);
3046 if (err)
3047 return err;
3049 if (asprintf(&header, "diff %s %s",
3050 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3051 err = got_error_from_errno("asprintf");
3052 free(id_str1);
3053 free(id_str2);
3054 return err;
3056 free(id_str1);
3057 free(id_str2);
3059 return draw_file(view, s->f, &s->first_displayed_line,
3060 &s->last_displayed_line, &s->eof, view->nlines,
3061 header, &s->colors);
3064 static const struct got_error *
3065 set_selected_commit(struct tog_diff_view_state *s,
3066 struct commit_queue_entry *entry)
3068 const struct got_error *err;
3069 const struct got_object_id_queue *parent_ids;
3070 struct got_commit_object *selected_commit;
3071 struct got_object_qid *pid;
3073 free(s->id2);
3074 s->id2 = got_object_id_dup(entry->id);
3075 if (s->id2 == NULL)
3076 return got_error_from_errno("got_object_id_dup");
3078 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3079 if (err)
3080 return err;
3081 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3082 free(s->id1);
3083 pid = SIMPLEQ_FIRST(parent_ids);
3084 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3085 got_object_commit_close(selected_commit);
3086 return NULL;
3089 static const struct got_error *
3090 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3091 struct tog_view **focus_view, struct tog_view *view, int ch)
3093 const struct got_error *err = NULL;
3094 struct tog_diff_view_state *s = &view->state.diff;
3095 struct tog_log_view_state *ls;
3096 struct commit_queue_entry *entry;
3097 int i;
3099 switch (ch) {
3100 case 'k':
3101 case KEY_UP:
3102 if (s->first_displayed_line > 1)
3103 s->first_displayed_line--;
3104 break;
3105 case KEY_PPAGE:
3106 case CTRL('b'):
3107 if (s->first_displayed_line == 1)
3108 break;
3109 i = 0;
3110 while (i++ < view->nlines - 1 &&
3111 s->first_displayed_line > 1)
3112 s->first_displayed_line--;
3113 break;
3114 case 'j':
3115 case KEY_DOWN:
3116 if (!s->eof)
3117 s->first_displayed_line++;
3118 break;
3119 case KEY_NPAGE:
3120 case CTRL('f'):
3121 case ' ':
3122 if (s->eof)
3123 break;
3124 i = 0;
3125 while (!s->eof && i++ < view->nlines - 1) {
3126 char *line;
3127 line = parse_next_line(s->f, NULL);
3128 s->first_displayed_line++;
3129 if (line == NULL)
3130 break;
3132 break;
3133 case '[':
3134 if (s->diff_context > 0) {
3135 s->diff_context--;
3136 diff_view_indicate_progress(view);
3137 err = create_diff(s);
3139 break;
3140 case ']':
3141 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3142 s->diff_context++;
3143 diff_view_indicate_progress(view);
3144 err = create_diff(s);
3146 break;
3147 case '<':
3148 case ',':
3149 if (s->log_view == NULL)
3150 break;
3151 ls = &s->log_view->state.log;
3152 entry = TAILQ_PREV(ls->selected_entry,
3153 commit_queue_head, entry);
3154 if (entry == NULL)
3155 break;
3157 err = input_log_view(NULL, NULL, NULL, s->log_view,
3158 KEY_UP);
3159 if (err)
3160 break;
3162 err = set_selected_commit(s, entry);
3163 if (err)
3164 break;
3166 s->first_displayed_line = 1;
3167 s->last_displayed_line = view->nlines;
3169 diff_view_indicate_progress(view);
3170 err = create_diff(s);
3171 break;
3172 case '>':
3173 case '.':
3174 if (s->log_view == NULL)
3175 break;
3176 ls = &s->log_view->state.log;
3178 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3179 ls->thread_args.commits_needed++;
3181 /* Display "loading..." in log view. */
3182 show_log_view(s->log_view);
3183 update_panels();
3184 doupdate();
3186 err = trigger_log_thread(1 /* load_all */,
3187 &ls->thread_args.commits_needed,
3188 &ls->thread_args.log_complete,
3189 &ls->thread_args.need_commits);
3190 if (err)
3191 break;
3193 err = input_log_view(NULL, NULL, NULL, s->log_view,
3194 KEY_DOWN);
3195 if (err)
3196 break;
3198 entry = TAILQ_NEXT(ls->selected_entry, entry);
3199 if (entry == NULL)
3200 break;
3202 err = set_selected_commit(s, entry);
3203 if (err)
3204 break;
3206 s->first_displayed_line = 1;
3207 s->last_displayed_line = view->nlines;
3209 diff_view_indicate_progress(view);
3210 err = create_diff(s);
3211 break;
3212 default:
3213 break;
3216 return err;
3219 static const struct got_error *
3220 cmd_diff(int argc, char *argv[])
3222 const struct got_error *error = NULL;
3223 struct got_repository *repo = NULL;
3224 struct got_reflist_head refs;
3225 struct got_object_id *id1 = NULL, *id2 = NULL;
3226 char *repo_path = NULL;
3227 char *id_str1 = NULL, *id_str2 = NULL;
3228 int ch;
3229 struct tog_view *view;
3231 SIMPLEQ_INIT(&refs);
3233 #ifndef PROFILE
3234 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3235 NULL) == -1)
3236 err(1, "pledge");
3237 #endif
3239 while ((ch = getopt(argc, argv, "")) != -1) {
3240 switch (ch) {
3241 default:
3242 usage_diff();
3243 /* NOTREACHED */
3247 argc -= optind;
3248 argv += optind;
3250 if (argc == 0) {
3251 usage_diff(); /* TODO show local worktree changes */
3252 } else if (argc == 2) {
3253 repo_path = getcwd(NULL, 0);
3254 if (repo_path == NULL)
3255 return got_error_from_errno("getcwd");
3256 id_str1 = argv[0];
3257 id_str2 = argv[1];
3258 } else if (argc == 3) {
3259 repo_path = realpath(argv[0], NULL);
3260 if (repo_path == NULL)
3261 return got_error_from_errno2("realpath", argv[0]);
3262 id_str1 = argv[1];
3263 id_str2 = argv[2];
3264 } else
3265 usage_diff();
3267 init_curses();
3269 error = got_repo_open(&repo, repo_path, NULL);
3270 if (error)
3271 goto done;
3273 error = apply_unveil(got_repo_get_path(repo), NULL);
3274 if (error)
3275 goto done;
3277 error = got_repo_match_object_id_prefix(&id1, id_str1,
3278 GOT_OBJ_TYPE_ANY, repo);
3279 if (error)
3280 goto done;
3282 error = got_repo_match_object_id_prefix(&id2, id_str2,
3283 GOT_OBJ_TYPE_ANY, repo);
3284 if (error)
3285 goto done;
3287 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3288 if (error)
3289 goto done;
3291 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3292 if (view == NULL) {
3293 error = got_error_from_errno("view_open");
3294 goto done;
3296 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3297 if (error)
3298 goto done;
3299 error = view_loop(view);
3300 done:
3301 free(repo_path);
3302 if (repo)
3303 got_repo_close(repo);
3304 got_ref_list_free(&refs);
3305 return error;
3308 __dead static void
3309 usage_blame(void)
3311 endwin();
3312 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3313 getprogname());
3314 exit(1);
3317 struct tog_blame_line {
3318 int annotated;
3319 struct got_object_id *id;
3322 static const struct got_error *
3323 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3324 const char *path, struct tog_blame_line *lines, int nlines,
3325 int blame_complete, int selected_line, int *first_displayed_line,
3326 int *last_displayed_line, int *eof, int max_lines,
3327 struct tog_colors *colors)
3329 const struct got_error *err;
3330 int lineno = 0, nprinted = 0;
3331 char *line;
3332 size_t len;
3333 wchar_t *wline;
3334 int width;
3335 struct tog_blame_line *blame_line;
3336 struct got_object_id *prev_id = NULL;
3337 char *id_str;
3338 struct tog_color *tc;
3340 err = got_object_id_str(&id_str, id);
3341 if (err)
3342 return err;
3344 rewind(f);
3345 werase(view->window);
3347 if (asprintf(&line, "commit %s", id_str) == -1) {
3348 err = got_error_from_errno("asprintf");
3349 free(id_str);
3350 return err;
3353 err = format_line(&wline, &width, line, view->ncols, 0);
3354 free(line);
3355 line = NULL;
3356 if (err)
3357 return err;
3358 if (view_needs_focus_indication(view))
3359 wstandout(view->window);
3360 tc = get_color(colors, TOG_COLOR_COMMIT);
3361 if (tc)
3362 wattr_on(view->window,
3363 COLOR_PAIR(tc->colorpair), NULL);
3364 waddwstr(view->window, wline);
3365 if (tc)
3366 wattr_off(view->window,
3367 COLOR_PAIR(tc->colorpair), NULL);
3368 if (view_needs_focus_indication(view))
3369 wstandend(view->window);
3370 free(wline);
3371 wline = NULL;
3372 if (width < view->ncols - 1)
3373 waddch(view->window, '\n');
3375 if (asprintf(&line, "[%d/%d] %s%s",
3376 *first_displayed_line - 1 + selected_line, nlines,
3377 blame_complete ? "" : "annotating... ", path) == -1) {
3378 free(id_str);
3379 return got_error_from_errno("asprintf");
3381 free(id_str);
3382 err = format_line(&wline, &width, line, view->ncols, 0);
3383 free(line);
3384 line = NULL;
3385 if (err)
3386 return err;
3387 waddwstr(view->window, wline);
3388 free(wline);
3389 wline = NULL;
3390 if (width < view->ncols - 1)
3391 waddch(view->window, '\n');
3393 *eof = 0;
3394 while (nprinted < max_lines - 2) {
3395 line = parse_next_line(f, &len);
3396 if (line == NULL) {
3397 *eof = 1;
3398 break;
3400 if (++lineno < *first_displayed_line) {
3401 free(line);
3402 continue;
3405 if (view->ncols <= 9) {
3406 width = 9;
3407 wline = wcsdup(L"");
3408 if (wline == NULL)
3409 err = got_error_from_errno("wcsdup");
3410 } else {
3411 err = format_line(&wline, &width, line,
3412 view->ncols - 9, 9);
3413 width += 9;
3415 if (err) {
3416 free(line);
3417 return err;
3420 if (view->focussed && nprinted == selected_line - 1)
3421 wstandout(view->window);
3423 if (nlines > 0) {
3424 blame_line = &lines[lineno - 1];
3425 if (blame_line->annotated && prev_id &&
3426 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3427 !(view->focussed &&
3428 nprinted == selected_line - 1)) {
3429 waddstr(view->window, " ");
3430 } else if (blame_line->annotated) {
3431 char *id_str;
3432 err = got_object_id_str(&id_str, blame_line->id);
3433 if (err) {
3434 free(line);
3435 free(wline);
3436 return err;
3438 tc = get_color(colors, TOG_COLOR_COMMIT);
3439 if (tc)
3440 wattr_on(view->window,
3441 COLOR_PAIR(tc->colorpair), NULL);
3442 wprintw(view->window, "%.8s", id_str);
3443 if (tc)
3444 wattr_off(view->window,
3445 COLOR_PAIR(tc->colorpair), NULL);
3446 free(id_str);
3447 prev_id = blame_line->id;
3448 } else {
3449 waddstr(view->window, "........");
3450 prev_id = NULL;
3452 } else {
3453 waddstr(view->window, "........");
3454 prev_id = NULL;
3457 if (view->focussed && nprinted == selected_line - 1)
3458 wstandend(view->window);
3459 waddstr(view->window, " ");
3461 waddwstr(view->window, wline);
3462 if (width <= view->ncols - 1)
3463 waddch(view->window, '\n');
3464 if (++nprinted == 1)
3465 *first_displayed_line = lineno;
3466 free(line);
3467 free(wline);
3468 wline = NULL;
3470 *last_displayed_line = lineno;
3472 view_vborder(view);
3474 return NULL;
3477 static const struct got_error *
3478 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3480 const struct got_error *err = NULL;
3481 struct tog_blame_cb_args *a = arg;
3482 struct tog_blame_line *line;
3483 int errcode;
3485 if (nlines != a->nlines ||
3486 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3487 return got_error(GOT_ERR_RANGE);
3489 errcode = pthread_mutex_lock(&tog_mutex);
3490 if (errcode)
3491 return got_error_set_errno(errcode, "pthread_mutex_lock");
3493 if (*a->quit) { /* user has quit the blame view */
3494 err = got_error(GOT_ERR_ITER_COMPLETED);
3495 goto done;
3498 if (lineno == -1)
3499 goto done; /* no change in this commit */
3501 line = &a->lines[lineno - 1];
3502 if (line->annotated)
3503 goto done;
3505 line->id = got_object_id_dup(id);
3506 if (line->id == NULL) {
3507 err = got_error_from_errno("got_object_id_dup");
3508 goto done;
3510 line->annotated = 1;
3511 done:
3512 errcode = pthread_mutex_unlock(&tog_mutex);
3513 if (errcode)
3514 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3515 return err;
3518 static void *
3519 blame_thread(void *arg)
3521 const struct got_error *err;
3522 struct tog_blame_thread_args *ta = arg;
3523 struct tog_blame_cb_args *a = ta->cb_args;
3524 int errcode;
3526 err = got_blame(ta->path, a->commit_id, ta->repo,
3527 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3528 if (err && err->code == GOT_ERR_CANCELLED)
3529 err = NULL;
3531 errcode = pthread_mutex_lock(&tog_mutex);
3532 if (errcode)
3533 return (void *)got_error_set_errno(errcode,
3534 "pthread_mutex_lock");
3536 got_repo_close(ta->repo);
3537 ta->repo = NULL;
3538 *ta->complete = 1;
3540 errcode = pthread_mutex_unlock(&tog_mutex);
3541 if (errcode && err == NULL)
3542 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3544 return (void *)err;
3547 static struct got_object_id *
3548 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3549 int first_displayed_line, int selected_line)
3551 struct tog_blame_line *line;
3553 if (nlines <= 0)
3554 return NULL;
3556 line = &lines[first_displayed_line - 1 + selected_line - 1];
3557 if (!line->annotated)
3558 return NULL;
3560 return line->id;
3563 static const struct got_error *
3564 stop_blame(struct tog_blame *blame)
3566 const struct got_error *err = NULL;
3567 int i;
3569 if (blame->thread) {
3570 int errcode;
3571 errcode = pthread_mutex_unlock(&tog_mutex);
3572 if (errcode)
3573 return got_error_set_errno(errcode,
3574 "pthread_mutex_unlock");
3575 errcode = pthread_join(blame->thread, (void **)&err);
3576 if (errcode)
3577 return got_error_set_errno(errcode, "pthread_join");
3578 errcode = pthread_mutex_lock(&tog_mutex);
3579 if (errcode)
3580 return got_error_set_errno(errcode,
3581 "pthread_mutex_lock");
3582 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3583 err = NULL;
3584 blame->thread = NULL;
3586 if (blame->thread_args.repo) {
3587 got_repo_close(blame->thread_args.repo);
3588 blame->thread_args.repo = NULL;
3590 if (blame->f) {
3591 if (fclose(blame->f) != 0 && err == NULL)
3592 err = got_error_from_errno("fclose");
3593 blame->f = NULL;
3595 if (blame->lines) {
3596 for (i = 0; i < blame->nlines; i++)
3597 free(blame->lines[i].id);
3598 free(blame->lines);
3599 blame->lines = NULL;
3601 free(blame->cb_args.commit_id);
3602 blame->cb_args.commit_id = NULL;
3604 return err;
3607 static const struct got_error *
3608 cancel_blame_view(void *arg)
3610 const struct got_error *err = NULL;
3611 int *done = arg;
3612 int errcode;
3614 errcode = pthread_mutex_lock(&tog_mutex);
3615 if (errcode)
3616 return got_error_set_errno(errcode,
3617 "pthread_mutex_unlock");
3619 if (*done)
3620 err = got_error(GOT_ERR_CANCELLED);
3622 errcode = pthread_mutex_unlock(&tog_mutex);
3623 if (errcode)
3624 return got_error_set_errno(errcode,
3625 "pthread_mutex_lock");
3627 return err;
3630 static const struct got_error *
3631 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3632 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3633 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3634 struct got_repository *repo)
3636 const struct got_error *err = NULL;
3637 struct got_blob_object *blob = NULL;
3638 struct got_repository *thread_repo = NULL;
3639 struct got_object_id *obj_id = NULL;
3640 int obj_type;
3642 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3643 if (err)
3644 return err;
3645 if (obj_id == NULL)
3646 return got_error(GOT_ERR_NO_OBJ);
3648 err = got_object_get_type(&obj_type, repo, obj_id);
3649 if (err)
3650 goto done;
3652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3653 err = got_error(GOT_ERR_OBJ_TYPE);
3654 goto done;
3657 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3658 if (err)
3659 goto done;
3660 blame->f = got_opentemp();
3661 if (blame->f == NULL) {
3662 err = got_error_from_errno("got_opentemp");
3663 goto done;
3665 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3666 &blame->line_offsets, blame->f, blob);
3667 if (err || blame->nlines == 0)
3668 goto done;
3670 /* Don't include \n at EOF in the blame line count. */
3671 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3672 blame->nlines--;
3674 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3675 if (blame->lines == NULL) {
3676 err = got_error_from_errno("calloc");
3677 goto done;
3680 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3681 if (err)
3682 goto done;
3684 blame->cb_args.view = view;
3685 blame->cb_args.lines = blame->lines;
3686 blame->cb_args.nlines = blame->nlines;
3687 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3688 if (blame->cb_args.commit_id == NULL) {
3689 err = got_error_from_errno("got_object_id_dup");
3690 goto done;
3692 blame->cb_args.quit = done;
3694 blame->thread_args.path = path;
3695 blame->thread_args.repo = thread_repo;
3696 blame->thread_args.cb_args = &blame->cb_args;
3697 blame->thread_args.complete = blame_complete;
3698 blame->thread_args.cancel_cb = cancel_blame_view;
3699 blame->thread_args.cancel_arg = done;
3700 *blame_complete = 0;
3702 done:
3703 if (blob)
3704 got_object_blob_close(blob);
3705 free(obj_id);
3706 if (err)
3707 stop_blame(blame);
3708 return err;
3711 static const struct got_error *
3712 open_blame_view(struct tog_view *view, char *path,
3713 struct got_object_id *commit_id, struct got_reflist_head *refs,
3714 struct got_repository *repo)
3716 const struct got_error *err = NULL;
3717 struct tog_blame_view_state *s = &view->state.blame;
3719 SIMPLEQ_INIT(&s->blamed_commits);
3721 s->path = strdup(path);
3722 if (s->path == NULL)
3723 return got_error_from_errno("strdup");
3725 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3726 if (err) {
3727 free(s->path);
3728 return err;
3731 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3732 s->first_displayed_line = 1;
3733 s->last_displayed_line = view->nlines;
3734 s->selected_line = 1;
3735 s->blame_complete = 0;
3736 s->repo = repo;
3737 s->refs = refs;
3738 s->commit_id = commit_id;
3739 memset(&s->blame, 0, sizeof(s->blame));
3741 SIMPLEQ_INIT(&s->colors);
3742 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3743 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3744 get_color_value("TOG_COLOR_COMMIT"));
3745 if (err)
3746 return err;
3749 view->show = show_blame_view;
3750 view->input = input_blame_view;
3751 view->close = close_blame_view;
3752 view->search_start = search_start_blame_view;
3753 view->search_next = search_next_blame_view;
3755 return run_blame(&s->blame, view, &s->blame_complete,
3756 &s->first_displayed_line, &s->last_displayed_line,
3757 &s->selected_line, &s->done, &s->eof, s->path,
3758 s->blamed_commit->id, s->repo);
3761 static const struct got_error *
3762 close_blame_view(struct tog_view *view)
3764 const struct got_error *err = NULL;
3765 struct tog_blame_view_state *s = &view->state.blame;
3767 if (s->blame.thread)
3768 err = stop_blame(&s->blame);
3770 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3771 struct got_object_qid *blamed_commit;
3772 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3773 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3774 got_object_qid_free(blamed_commit);
3777 free(s->path);
3778 free_colors(&s->colors);
3780 return err;
3783 static const struct got_error *
3784 search_start_blame_view(struct tog_view *view)
3786 struct tog_blame_view_state *s = &view->state.blame;
3788 s->matched_line = 0;
3789 return NULL;
3792 static const struct got_error *
3793 search_next_blame_view(struct tog_view *view)
3795 struct tog_blame_view_state *s = &view->state.blame;
3796 int lineno;
3798 if (!view->searching) {
3799 view->search_next_done = 1;
3800 return NULL;
3803 if (s->matched_line) {
3804 if (view->searching == TOG_SEARCH_FORWARD)
3805 lineno = s->matched_line + 1;
3806 else
3807 lineno = s->matched_line - 1;
3808 } else {
3809 if (view->searching == TOG_SEARCH_FORWARD)
3810 lineno = 1;
3811 else
3812 lineno = s->blame.nlines;
3815 while (1) {
3816 char *line = NULL;
3817 off_t offset;
3818 size_t len;
3820 if (lineno <= 0 || lineno > s->blame.nlines) {
3821 if (s->matched_line == 0) {
3822 view->search_next_done = 1;
3823 free(line);
3824 break;
3827 if (view->searching == TOG_SEARCH_FORWARD)
3828 lineno = 1;
3829 else
3830 lineno = s->blame.nlines;
3833 offset = s->blame.line_offsets[lineno - 1];
3834 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3835 free(line);
3836 return got_error_from_errno("fseeko");
3838 free(line);
3839 line = parse_next_line(s->blame.f, &len);
3840 if (line && match_line(line, &view->regex)) {
3841 view->search_next_done = 1;
3842 s->matched_line = lineno;
3843 free(line);
3844 break;
3846 free(line);
3847 if (view->searching == TOG_SEARCH_FORWARD)
3848 lineno++;
3849 else
3850 lineno--;
3853 if (s->matched_line) {
3854 s->first_displayed_line = s->matched_line;
3855 s->selected_line = 1;
3858 return NULL;
3861 static const struct got_error *
3862 show_blame_view(struct tog_view *view)
3864 const struct got_error *err = NULL;
3865 struct tog_blame_view_state *s = &view->state.blame;
3866 int errcode;
3868 if (s->blame.thread == NULL) {
3869 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3870 &s->blame.thread_args);
3871 if (errcode)
3872 return got_error_set_errno(errcode, "pthread_create");
3874 halfdelay(1); /* fast refresh while annotating */
3877 if (s->blame_complete)
3878 halfdelay(10); /* disable fast refresh */
3880 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3881 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3882 s->selected_line, &s->first_displayed_line,
3883 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3885 view_vborder(view);
3886 return err;
3889 static const struct got_error *
3890 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3891 struct tog_view **focus_view, struct tog_view *view, int ch)
3893 const struct got_error *err = NULL, *thread_err = NULL;
3894 struct tog_view *diff_view;
3895 struct tog_blame_view_state *s = &view->state.blame;
3896 int begin_x = 0;
3898 switch (ch) {
3899 case 'q':
3900 s->done = 1;
3901 break;
3902 case 'k':
3903 case KEY_UP:
3904 if (s->selected_line > 1)
3905 s->selected_line--;
3906 else if (s->selected_line == 1 &&
3907 s->first_displayed_line > 1)
3908 s->first_displayed_line--;
3909 break;
3910 case KEY_PPAGE:
3911 if (s->first_displayed_line == 1) {
3912 s->selected_line = 1;
3913 break;
3915 if (s->first_displayed_line > view->nlines - 2)
3916 s->first_displayed_line -=
3917 (view->nlines - 2);
3918 else
3919 s->first_displayed_line = 1;
3920 break;
3921 case 'j':
3922 case KEY_DOWN:
3923 if (s->selected_line < view->nlines - 2 &&
3924 s->first_displayed_line +
3925 s->selected_line <= s->blame.nlines)
3926 s->selected_line++;
3927 else if (s->last_displayed_line <
3928 s->blame.nlines)
3929 s->first_displayed_line++;
3930 break;
3931 case 'b':
3932 case 'p': {
3933 struct got_object_id *id = NULL;
3934 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3935 s->first_displayed_line, s->selected_line);
3936 if (id == NULL)
3937 break;
3938 if (ch == 'p') {
3939 struct got_commit_object *commit;
3940 struct got_object_qid *pid;
3941 struct got_object_id *blob_id = NULL;
3942 int obj_type;
3943 err = got_object_open_as_commit(&commit,
3944 s->repo, id);
3945 if (err)
3946 break;
3947 pid = SIMPLEQ_FIRST(
3948 got_object_commit_get_parent_ids(commit));
3949 if (pid == NULL) {
3950 got_object_commit_close(commit);
3951 break;
3953 /* Check if path history ends here. */
3954 err = got_object_id_by_path(&blob_id, s->repo,
3955 pid->id, s->path);
3956 if (err) {
3957 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3958 err = NULL;
3959 got_object_commit_close(commit);
3960 break;
3962 err = got_object_get_type(&obj_type, s->repo,
3963 blob_id);
3964 free(blob_id);
3965 /* Can't blame non-blob type objects. */
3966 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3967 got_object_commit_close(commit);
3968 break;
3970 err = got_object_qid_alloc(&s->blamed_commit,
3971 pid->id);
3972 got_object_commit_close(commit);
3973 } else {
3974 if (got_object_id_cmp(id,
3975 s->blamed_commit->id) == 0)
3976 break;
3977 err = got_object_qid_alloc(&s->blamed_commit,
3978 id);
3980 if (err)
3981 break;
3982 s->done = 1;
3983 thread_err = stop_blame(&s->blame);
3984 s->done = 0;
3985 if (thread_err)
3986 break;
3987 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3988 s->blamed_commit, entry);
3989 err = run_blame(&s->blame, view, &s->blame_complete,
3990 &s->first_displayed_line, &s->last_displayed_line,
3991 &s->selected_line, &s->done, &s->eof,
3992 s->path, s->blamed_commit->id, s->repo);
3993 if (err)
3994 break;
3995 break;
3997 case 'B': {
3998 struct got_object_qid *first;
3999 first = SIMPLEQ_FIRST(&s->blamed_commits);
4000 if (!got_object_id_cmp(first->id, s->commit_id))
4001 break;
4002 s->done = 1;
4003 thread_err = stop_blame(&s->blame);
4004 s->done = 0;
4005 if (thread_err)
4006 break;
4007 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4008 got_object_qid_free(s->blamed_commit);
4009 s->blamed_commit =
4010 SIMPLEQ_FIRST(&s->blamed_commits);
4011 err = run_blame(&s->blame, view, &s->blame_complete,
4012 &s->first_displayed_line, &s->last_displayed_line,
4013 &s->selected_line, &s->done, &s->eof, s->path,
4014 s->blamed_commit->id, s->repo);
4015 if (err)
4016 break;
4017 break;
4019 case KEY_ENTER:
4020 case '\r': {
4021 struct got_object_id *id = NULL;
4022 struct got_object_qid *pid;
4023 struct got_commit_object *commit = NULL;
4024 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4025 s->first_displayed_line, s->selected_line);
4026 if (id == NULL)
4027 break;
4028 err = got_object_open_as_commit(&commit, s->repo, id);
4029 if (err)
4030 break;
4031 pid = SIMPLEQ_FIRST(
4032 got_object_commit_get_parent_ids(commit));
4033 if (view_is_parent_view(view))
4034 begin_x = view_split_begin_x(view->begin_x);
4035 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4036 if (diff_view == NULL) {
4037 got_object_commit_close(commit);
4038 err = got_error_from_errno("view_open");
4039 break;
4041 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4042 id, NULL, s->refs, s->repo);
4043 got_object_commit_close(commit);
4044 if (err) {
4045 view_close(diff_view);
4046 break;
4048 if (view_is_parent_view(view)) {
4049 err = view_close_child(view);
4050 if (err)
4051 break;
4052 err = view_set_child(view, diff_view);
4053 if (err) {
4054 view_close(diff_view);
4055 break;
4057 *focus_view = diff_view;
4058 view->child_focussed = 1;
4059 } else
4060 *new_view = diff_view;
4061 if (err)
4062 break;
4063 break;
4065 case KEY_NPAGE:
4066 case ' ':
4067 if (s->last_displayed_line >= s->blame.nlines &&
4068 s->selected_line >= MIN(s->blame.nlines,
4069 view->nlines - 2)) {
4070 break;
4072 if (s->last_displayed_line >= s->blame.nlines &&
4073 s->selected_line < view->nlines - 2) {
4074 s->selected_line = MIN(s->blame.nlines,
4075 view->nlines - 2);
4076 break;
4078 if (s->last_displayed_line + view->nlines - 2
4079 <= s->blame.nlines)
4080 s->first_displayed_line +=
4081 view->nlines - 2;
4082 else
4083 s->first_displayed_line =
4084 s->blame.nlines -
4085 (view->nlines - 3);
4086 break;
4087 case KEY_RESIZE:
4088 if (s->selected_line > view->nlines - 2) {
4089 s->selected_line = MIN(s->blame.nlines,
4090 view->nlines - 2);
4092 break;
4093 default:
4094 break;
4096 return thread_err ? thread_err : err;
4099 static const struct got_error *
4100 cmd_blame(int argc, char *argv[])
4102 const struct got_error *error;
4103 struct got_repository *repo = NULL;
4104 struct got_reflist_head refs;
4105 struct got_worktree *worktree = NULL;
4106 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4107 struct got_object_id *commit_id = NULL;
4108 char *commit_id_str = NULL;
4109 int ch;
4110 struct tog_view *view;
4112 SIMPLEQ_INIT(&refs);
4114 #ifndef PROFILE
4115 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4116 NULL) == -1)
4117 err(1, "pledge");
4118 #endif
4120 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4121 switch (ch) {
4122 case 'c':
4123 commit_id_str = optarg;
4124 break;
4125 case 'r':
4126 repo_path = realpath(optarg, NULL);
4127 if (repo_path == NULL)
4128 return got_error_from_errno2("realpath",
4129 optarg);
4130 break;
4131 default:
4132 usage_blame();
4133 /* NOTREACHED */
4137 argc -= optind;
4138 argv += optind;
4140 if (argc == 1)
4141 path = argv[0];
4142 else
4143 usage_blame();
4145 cwd = getcwd(NULL, 0);
4146 if (cwd == NULL) {
4147 error = got_error_from_errno("getcwd");
4148 goto done;
4150 if (repo_path == NULL) {
4151 error = got_worktree_open(&worktree, cwd);
4152 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4153 goto done;
4154 else
4155 error = NULL;
4156 if (worktree) {
4157 repo_path =
4158 strdup(got_worktree_get_repo_path(worktree));
4159 if (repo_path == NULL)
4160 error = got_error_from_errno("strdup");
4161 if (error)
4162 goto done;
4163 } else {
4164 repo_path = strdup(cwd);
4165 if (repo_path == NULL) {
4166 error = got_error_from_errno("strdup");
4167 goto done;
4172 init_curses();
4174 error = got_repo_open(&repo, repo_path, NULL);
4175 if (error != NULL)
4176 goto done;
4178 error = apply_unveil(got_repo_get_path(repo), NULL);
4179 if (error)
4180 goto done;
4182 if (worktree) {
4183 const char *prefix = got_worktree_get_path_prefix(worktree);
4184 char *p, *worktree_subdir = cwd +
4185 strlen(got_worktree_get_root_path(worktree));
4186 if (asprintf(&p, "%s%s%s%s%s",
4187 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4188 worktree_subdir, worktree_subdir[0] ? "/" : "",
4189 path) == -1) {
4190 error = got_error_from_errno("asprintf");
4191 goto done;
4193 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4194 free(p);
4195 } else {
4196 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4198 if (error)
4199 goto done;
4201 if (commit_id_str == NULL) {
4202 struct got_reference *head_ref;
4203 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4204 if (error != NULL)
4205 goto done;
4206 error = got_ref_resolve(&commit_id, repo, head_ref);
4207 got_ref_close(head_ref);
4208 } else {
4209 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4210 if (error) {
4211 if (error->code != GOT_ERR_NOT_REF)
4212 goto done;
4213 error = got_repo_match_object_id_prefix(&commit_id,
4214 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4217 if (error != NULL)
4218 goto done;
4220 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4221 if (error)
4222 goto done;
4224 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4225 if (view == NULL) {
4226 error = got_error_from_errno("view_open");
4227 goto done;
4229 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4230 if (error)
4231 goto done;
4232 if (worktree) {
4233 /* Release work tree lock. */
4234 got_worktree_close(worktree);
4235 worktree = NULL;
4237 error = view_loop(view);
4238 done:
4239 free(repo_path);
4240 free(cwd);
4241 free(commit_id);
4242 if (worktree)
4243 got_worktree_close(worktree);
4244 if (repo)
4245 got_repo_close(repo);
4246 got_ref_list_free(&refs);
4247 return error;
4250 static const struct got_error *
4251 draw_tree_entries(struct tog_view *view,
4252 struct got_tree_entry **first_displayed_entry,
4253 struct got_tree_entry **last_displayed_entry,
4254 struct got_tree_entry **selected_entry, int *ndisplayed,
4255 const char *label, int show_ids, const char *parent_path,
4256 struct got_tree_object *tree, int selected, int limit,
4257 int isroot, struct tog_colors *colors)
4259 const struct got_error *err = NULL;
4260 struct got_tree_entry *te;
4261 wchar_t *wline;
4262 struct tog_color *tc;
4263 int width, n, i, nentries;
4265 *ndisplayed = 0;
4267 werase(view->window);
4269 if (limit == 0)
4270 return NULL;
4272 err = format_line(&wline, &width, label, view->ncols, 0);
4273 if (err)
4274 return err;
4275 if (view_needs_focus_indication(view))
4276 wstandout(view->window);
4277 tc = get_color(colors, TOG_COLOR_COMMIT);
4278 if (tc)
4279 wattr_on(view->window,
4280 COLOR_PAIR(tc->colorpair), NULL);
4281 waddwstr(view->window, wline);
4282 if (tc)
4283 wattr_off(view->window,
4284 COLOR_PAIR(tc->colorpair), NULL);
4285 if (view_needs_focus_indication(view))
4286 wstandend(view->window);
4287 free(wline);
4288 wline = NULL;
4289 if (width < view->ncols - 1)
4290 waddch(view->window, '\n');
4291 if (--limit <= 0)
4292 return NULL;
4293 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4294 if (err)
4295 return err;
4296 waddwstr(view->window, wline);
4297 free(wline);
4298 wline = NULL;
4299 if (width < view->ncols - 1)
4300 waddch(view->window, '\n');
4301 if (--limit <= 0)
4302 return NULL;
4303 waddch(view->window, '\n');
4304 if (--limit <= 0)
4305 return NULL;
4307 if (*first_displayed_entry == NULL) {
4308 te = got_object_tree_get_first_entry(tree);
4309 if (selected == 0) {
4310 if (view->focussed)
4311 wstandout(view->window);
4312 *selected_entry = NULL;
4314 waddstr(view->window, " ..\n"); /* parent directory */
4315 if (selected == 0 && view->focussed)
4316 wstandend(view->window);
4317 (*ndisplayed)++;
4318 if (--limit <= 0)
4319 return NULL;
4320 n = 1;
4321 } else {
4322 n = 0;
4323 te = *first_displayed_entry;
4326 nentries = got_object_tree_get_nentries(tree);
4327 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4328 char *line = NULL, *id_str = NULL;
4329 const char *modestr = "";
4330 mode_t mode;
4332 te = got_object_tree_get_entry(tree, i);
4333 mode = got_tree_entry_get_mode(te);
4335 if (show_ids) {
4336 err = got_object_id_str(&id_str,
4337 got_tree_entry_get_id(te));
4338 if (err)
4339 return got_error_from_errno(
4340 "got_object_id_str");
4342 if (got_object_tree_entry_is_submodule(te))
4343 modestr = "$";
4344 else if (S_ISLNK(mode))
4345 modestr = "@";
4346 else if (S_ISDIR(mode))
4347 modestr = "/";
4348 else if (mode & S_IXUSR)
4349 modestr = "*";
4350 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4351 got_tree_entry_get_name(te), modestr) == -1) {
4352 free(id_str);
4353 return got_error_from_errno("asprintf");
4355 free(id_str);
4356 err = format_line(&wline, &width, line, view->ncols, 0);
4357 if (err) {
4358 free(line);
4359 break;
4361 if (n == selected) {
4362 if (view->focussed)
4363 wstandout(view->window);
4364 *selected_entry = te;
4366 tc = match_color(colors, line);
4367 if (tc)
4368 wattr_on(view->window,
4369 COLOR_PAIR(tc->colorpair), NULL);
4370 waddwstr(view->window, wline);
4371 if (tc)
4372 wattr_off(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 if (width < view->ncols - 1)
4375 waddch(view->window, '\n');
4376 if (n == selected && view->focussed)
4377 wstandend(view->window);
4378 free(line);
4379 free(wline);
4380 wline = NULL;
4381 n++;
4382 (*ndisplayed)++;
4383 *last_displayed_entry = te;
4384 if (--limit <= 0)
4385 break;
4388 return err;
4391 static void
4392 tree_scroll_up(struct tog_view *view,
4393 struct got_tree_entry **first_displayed_entry, int maxscroll,
4394 struct got_tree_object *tree, int isroot)
4396 struct got_tree_entry *te;
4397 int i;
4399 if (*first_displayed_entry == NULL)
4400 return;
4402 te = got_object_tree_get_entry(tree, 0);
4403 if (*first_displayed_entry == te) {
4404 if (!isroot)
4405 *first_displayed_entry = NULL;
4406 return;
4409 i = 0;
4410 while (*first_displayed_entry && i < maxscroll) {
4411 *first_displayed_entry = got_tree_entry_get_prev(tree,
4412 *first_displayed_entry);
4413 i++;
4415 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4416 *first_displayed_entry = NULL;
4419 static int
4420 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4421 struct got_tree_entry *last_displayed_entry,
4422 struct got_tree_object *tree)
4424 struct got_tree_entry *next, *last;
4425 int n = 0;
4427 if (*first_displayed_entry)
4428 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4429 else
4430 next = got_object_tree_get_first_entry(tree);
4432 last = last_displayed_entry;
4433 while (next && last && n++ < maxscroll) {
4434 last = got_tree_entry_get_next(tree, last);
4435 if (last) {
4436 *first_displayed_entry = next;
4437 next = got_tree_entry_get_next(tree, next);
4440 return n;
4443 static const struct got_error *
4444 tree_entry_path(char **path, struct tog_parent_trees *parents,
4445 struct got_tree_entry *te)
4447 const struct got_error *err = NULL;
4448 struct tog_parent_tree *pt;
4449 size_t len = 2; /* for leading slash and NUL */
4451 TAILQ_FOREACH(pt, parents, entry)
4452 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4453 + 1 /* slash */;
4454 if (te)
4455 len += strlen(got_tree_entry_get_name(te));
4457 *path = calloc(1, len);
4458 if (path == NULL)
4459 return got_error_from_errno("calloc");
4461 (*path)[0] = '/';
4462 pt = TAILQ_LAST(parents, tog_parent_trees);
4463 while (pt) {
4464 const char *name = got_tree_entry_get_name(pt->selected_entry);
4465 if (strlcat(*path, name, len) >= len) {
4466 err = got_error(GOT_ERR_NO_SPACE);
4467 goto done;
4469 if (strlcat(*path, "/", len) >= len) {
4470 err = got_error(GOT_ERR_NO_SPACE);
4471 goto done;
4473 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4475 if (te) {
4476 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4477 err = got_error(GOT_ERR_NO_SPACE);
4478 goto done;
4481 done:
4482 if (err) {
4483 free(*path);
4484 *path = NULL;
4486 return err;
4489 static const struct got_error *
4490 blame_tree_entry(struct tog_view **new_view, int begin_x,
4491 struct got_tree_entry *te, struct tog_parent_trees *parents,
4492 struct got_object_id *commit_id, struct got_reflist_head *refs,
4493 struct got_repository *repo)
4495 const struct got_error *err = NULL;
4496 char *path;
4497 struct tog_view *blame_view;
4499 *new_view = NULL;
4501 err = tree_entry_path(&path, parents, te);
4502 if (err)
4503 return err;
4505 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4506 if (blame_view == NULL) {
4507 err = got_error_from_errno("view_open");
4508 goto done;
4511 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4512 if (err) {
4513 if (err->code == GOT_ERR_CANCELLED)
4514 err = NULL;
4515 view_close(blame_view);
4516 } else
4517 *new_view = blame_view;
4518 done:
4519 free(path);
4520 return err;
4523 static const struct got_error *
4524 log_tree_entry(struct tog_view **new_view, int begin_x,
4525 struct got_tree_entry *te, struct tog_parent_trees *parents,
4526 struct got_object_id *commit_id, struct got_reflist_head *refs,
4527 struct got_repository *repo)
4529 struct tog_view *log_view;
4530 const struct got_error *err = NULL;
4531 char *path;
4533 *new_view = NULL;
4535 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4536 if (log_view == NULL)
4537 return got_error_from_errno("view_open");
4539 err = tree_entry_path(&path, parents, te);
4540 if (err)
4541 return err;
4543 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4544 if (err)
4545 view_close(log_view);
4546 else
4547 *new_view = log_view;
4548 free(path);
4549 return err;
4552 static const struct got_error *
4553 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4554 struct got_object_id *commit_id, struct got_reflist_head *refs,
4555 struct got_repository *repo)
4557 const struct got_error *err = NULL;
4558 char *commit_id_str = NULL;
4559 struct tog_tree_view_state *s = &view->state.tree;
4561 TAILQ_INIT(&s->parents);
4563 err = got_object_id_str(&commit_id_str, commit_id);
4564 if (err != NULL)
4565 goto done;
4567 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4568 err = got_error_from_errno("asprintf");
4569 goto done;
4572 s->root = s->tree = root;
4573 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4574 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4575 s->commit_id = got_object_id_dup(commit_id);
4576 if (s->commit_id == NULL) {
4577 err = got_error_from_errno("got_object_id_dup");
4578 goto done;
4580 s->refs = refs;
4581 s->repo = repo;
4583 SIMPLEQ_INIT(&s->colors);
4585 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4586 err = add_color(&s->colors, "\\$$",
4587 TOG_COLOR_TREE_SUBMODULE,
4588 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4589 if (err)
4590 goto done;
4591 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4592 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4593 if (err) {
4594 free_colors(&s->colors);
4595 goto done;
4597 err = add_color(&s->colors, "/$",
4598 TOG_COLOR_TREE_DIRECTORY,
4599 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4600 if (err) {
4601 free_colors(&s->colors);
4602 goto done;
4605 err = add_color(&s->colors, "\\*$",
4606 TOG_COLOR_TREE_EXECUTABLE,
4607 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4608 if (err) {
4609 free_colors(&s->colors);
4610 goto done;
4613 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4614 get_color_value("TOG_COLOR_COMMIT"));
4615 if (err) {
4616 free_colors(&s->colors);
4617 goto done;
4621 view->show = show_tree_view;
4622 view->input = input_tree_view;
4623 view->close = close_tree_view;
4624 view->search_start = search_start_tree_view;
4625 view->search_next = search_next_tree_view;
4626 done:
4627 free(commit_id_str);
4628 if (err) {
4629 free(s->tree_label);
4630 s->tree_label = NULL;
4632 return err;
4635 static const struct got_error *
4636 close_tree_view(struct tog_view *view)
4638 struct tog_tree_view_state *s = &view->state.tree;
4640 free_colors(&s->colors);
4641 free(s->tree_label);
4642 s->tree_label = NULL;
4643 free(s->commit_id);
4644 s->commit_id = NULL;
4645 while (!TAILQ_EMPTY(&s->parents)) {
4646 struct tog_parent_tree *parent;
4647 parent = TAILQ_FIRST(&s->parents);
4648 TAILQ_REMOVE(&s->parents, parent, entry);
4649 free(parent);
4652 if (s->tree != s->root)
4653 got_object_tree_close(s->tree);
4654 got_object_tree_close(s->root);
4656 return NULL;
4659 static const struct got_error *
4660 search_start_tree_view(struct tog_view *view)
4662 struct tog_tree_view_state *s = &view->state.tree;
4664 s->matched_entry = NULL;
4665 return NULL;
4668 static int
4669 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4671 regmatch_t regmatch;
4673 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4674 0) == 0;
4677 static const struct got_error *
4678 search_next_tree_view(struct tog_view *view)
4680 struct tog_tree_view_state *s = &view->state.tree;
4681 struct got_tree_entry *te = NULL;
4683 if (!view->searching) {
4684 view->search_next_done = 1;
4685 return NULL;
4688 if (s->matched_entry) {
4689 if (view->searching == TOG_SEARCH_FORWARD) {
4690 if (s->selected_entry)
4691 te = got_tree_entry_get_next(s->tree,
4692 s->selected_entry);
4693 else
4694 te = got_object_tree_get_first_entry(s->tree);
4695 } else {
4696 if (s->selected_entry == NULL)
4697 te = got_object_tree_get_last_entry(s->tree);
4698 else
4699 te = got_tree_entry_get_prev(s->tree,
4700 s->selected_entry);
4702 } else {
4703 if (view->searching == TOG_SEARCH_FORWARD)
4704 te = got_object_tree_get_first_entry(s->tree);
4705 else
4706 te = got_object_tree_get_last_entry(s->tree);
4709 while (1) {
4710 if (te == NULL) {
4711 if (s->matched_entry == NULL) {
4712 view->search_next_done = 1;
4713 return NULL;
4715 if (view->searching == TOG_SEARCH_FORWARD)
4716 te = got_object_tree_get_first_entry(s->tree);
4717 else
4718 te = got_object_tree_get_last_entry(s->tree);
4721 if (match_tree_entry(te, &view->regex)) {
4722 view->search_next_done = 1;
4723 s->matched_entry = te;
4724 break;
4727 if (view->searching == TOG_SEARCH_FORWARD)
4728 te = got_tree_entry_get_next(s->tree, te);
4729 else
4730 te = got_tree_entry_get_prev(s->tree, te);
4733 if (s->matched_entry) {
4734 s->first_displayed_entry = s->matched_entry;
4735 s->selected = 0;
4738 return NULL;
4741 static const struct got_error *
4742 show_tree_view(struct tog_view *view)
4744 const struct got_error *err = NULL;
4745 struct tog_tree_view_state *s = &view->state.tree;
4746 char *parent_path;
4748 err = tree_entry_path(&parent_path, &s->parents, NULL);
4749 if (err)
4750 return err;
4752 err = draw_tree_entries(view, &s->first_displayed_entry,
4753 &s->last_displayed_entry, &s->selected_entry,
4754 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4755 s->tree, s->selected, view->nlines, s->tree == s->root,
4756 &s->colors);
4757 free(parent_path);
4759 view_vborder(view);
4760 return err;
4763 static const struct got_error *
4764 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4765 struct tog_view **focus_view, struct tog_view *view, int ch)
4767 const struct got_error *err = NULL;
4768 struct tog_tree_view_state *s = &view->state.tree;
4769 struct tog_view *log_view;
4770 int begin_x = 0, nscrolled;
4772 switch (ch) {
4773 case 'i':
4774 s->show_ids = !s->show_ids;
4775 break;
4776 case 'l':
4777 if (!s->selected_entry)
4778 break;
4779 if (view_is_parent_view(view))
4780 begin_x = view_split_begin_x(view->begin_x);
4781 err = log_tree_entry(&log_view, begin_x,
4782 s->selected_entry, &s->parents,
4783 s->commit_id, s->refs, s->repo);
4784 if (view_is_parent_view(view)) {
4785 err = view_close_child(view);
4786 if (err)
4787 return err;
4788 err = view_set_child(view, log_view);
4789 if (err) {
4790 view_close(log_view);
4791 break;
4793 *focus_view = log_view;
4794 view->child_focussed = 1;
4795 } else
4796 *new_view = log_view;
4797 break;
4798 case 'k':
4799 case KEY_UP:
4800 if (s->selected > 0) {
4801 s->selected--;
4802 if (s->selected == 0)
4803 break;
4805 if (s->selected > 0)
4806 break;
4807 tree_scroll_up(view, &s->first_displayed_entry, 1,
4808 s->tree, s->tree == s->root);
4809 break;
4810 case KEY_PPAGE:
4811 tree_scroll_up(view, &s->first_displayed_entry,
4812 MAX(0, view->nlines - 4 - s->selected), s->tree,
4813 s->tree == s->root);
4814 s->selected = 0;
4815 if (got_object_tree_get_first_entry(s->tree) ==
4816 s->first_displayed_entry && s->tree != s->root)
4817 s->first_displayed_entry = NULL;
4818 break;
4819 case 'j':
4820 case KEY_DOWN:
4821 if (s->selected < s->ndisplayed - 1) {
4822 s->selected++;
4823 break;
4825 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4826 == NULL)
4827 /* can't scroll any further */
4828 break;
4829 tree_scroll_down(&s->first_displayed_entry, 1,
4830 s->last_displayed_entry, s->tree);
4831 break;
4832 case KEY_NPAGE:
4833 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4834 == NULL) {
4835 /* can't scroll any further; move cursor down */
4836 if (s->selected < s->ndisplayed - 1)
4837 s->selected = s->ndisplayed - 1;
4838 break;
4840 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4841 view->nlines, s->last_displayed_entry, s->tree);
4842 if (nscrolled < view->nlines) {
4843 int ndisplayed = 0;
4844 struct got_tree_entry *te;
4845 te = s->first_displayed_entry;
4846 do {
4847 ndisplayed++;
4848 te = got_tree_entry_get_next(s->tree, te);
4849 } while (te);
4850 s->selected = ndisplayed - 1;
4852 break;
4853 case KEY_ENTER:
4854 case '\r':
4855 case KEY_BACKSPACE:
4856 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4857 struct tog_parent_tree *parent;
4858 /* user selected '..' */
4859 if (s->tree == s->root)
4860 break;
4861 parent = TAILQ_FIRST(&s->parents);
4862 TAILQ_REMOVE(&s->parents, parent,
4863 entry);
4864 got_object_tree_close(s->tree);
4865 s->tree = parent->tree;
4866 s->first_displayed_entry =
4867 parent->first_displayed_entry;
4868 s->selected_entry =
4869 parent->selected_entry;
4870 s->selected = parent->selected;
4871 free(parent);
4872 } else if (S_ISDIR(got_tree_entry_get_mode(
4873 s->selected_entry))) {
4874 struct got_tree_object *subtree;
4875 err = got_object_open_as_tree(&subtree, s->repo,
4876 got_tree_entry_get_id(s->selected_entry));
4877 if (err)
4878 break;
4879 err = tree_view_visit_subtree(subtree, s);
4880 if (err) {
4881 got_object_tree_close(subtree);
4882 break;
4884 } else if (S_ISREG(got_tree_entry_get_mode(
4885 s->selected_entry))) {
4886 struct tog_view *blame_view;
4887 int begin_x = view_is_parent_view(view) ?
4888 view_split_begin_x(view->begin_x) : 0;
4890 err = blame_tree_entry(&blame_view, begin_x,
4891 s->selected_entry, &s->parents,
4892 s->commit_id, s->refs, s->repo);
4893 if (err)
4894 break;
4895 if (view_is_parent_view(view)) {
4896 err = view_close_child(view);
4897 if (err)
4898 return err;
4899 err = view_set_child(view, blame_view);
4900 if (err) {
4901 view_close(blame_view);
4902 break;
4904 *focus_view = blame_view;
4905 view->child_focussed = 1;
4906 } else
4907 *new_view = blame_view;
4909 break;
4910 case KEY_RESIZE:
4911 if (s->selected > view->nlines)
4912 s->selected = s->ndisplayed - 1;
4913 break;
4914 default:
4915 break;
4918 return err;
4921 __dead static void
4922 usage_tree(void)
4924 endwin();
4925 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4926 getprogname());
4927 exit(1);
4930 static const struct got_error *
4931 cmd_tree(int argc, char *argv[])
4933 const struct got_error *error;
4934 struct got_repository *repo = NULL;
4935 struct got_reflist_head refs;
4936 char *repo_path = NULL;
4937 struct got_object_id *commit_id = NULL;
4938 char *commit_id_arg = NULL;
4939 struct got_commit_object *commit = NULL;
4940 struct got_tree_object *tree = NULL;
4941 int ch;
4942 struct tog_view *view;
4944 SIMPLEQ_INIT(&refs);
4946 #ifndef PROFILE
4947 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4948 NULL) == -1)
4949 err(1, "pledge");
4950 #endif
4952 while ((ch = getopt(argc, argv, "c:")) != -1) {
4953 switch (ch) {
4954 case 'c':
4955 commit_id_arg = optarg;
4956 break;
4957 default:
4958 usage_tree();
4959 /* NOTREACHED */
4963 argc -= optind;
4964 argv += optind;
4966 if (argc == 0) {
4967 struct got_worktree *worktree;
4968 char *cwd = getcwd(NULL, 0);
4969 if (cwd == NULL)
4970 return got_error_from_errno("getcwd");
4971 error = got_worktree_open(&worktree, cwd);
4972 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4973 goto done;
4974 if (worktree) {
4975 free(cwd);
4976 repo_path =
4977 strdup(got_worktree_get_repo_path(worktree));
4978 got_worktree_close(worktree);
4979 } else
4980 repo_path = cwd;
4981 if (repo_path == NULL) {
4982 error = got_error_from_errno("strdup");
4983 goto done;
4985 } else if (argc == 1) {
4986 repo_path = realpath(argv[0], NULL);
4987 if (repo_path == NULL)
4988 return got_error_from_errno2("realpath", argv[0]);
4989 } else
4990 usage_log();
4992 init_curses();
4994 error = got_repo_open(&repo, repo_path, NULL);
4995 if (error != NULL)
4996 goto done;
4998 error = apply_unveil(got_repo_get_path(repo), NULL);
4999 if (error)
5000 goto done;
5002 if (commit_id_arg == NULL)
5003 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5004 else {
5005 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5006 if (error) {
5007 if (error->code != GOT_ERR_NOT_REF)
5008 goto done;
5009 error = got_repo_match_object_id_prefix(&commit_id,
5010 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5013 if (error != NULL)
5014 goto done;
5016 error = got_object_open_as_commit(&commit, repo, commit_id);
5017 if (error != NULL)
5018 goto done;
5020 error = got_object_open_as_tree(&tree, repo,
5021 got_object_commit_get_tree_id(commit));
5022 if (error != NULL)
5023 goto done;
5025 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5026 if (error)
5027 goto done;
5029 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5030 if (view == NULL) {
5031 error = got_error_from_errno("view_open");
5032 goto done;
5034 error = open_tree_view(view, tree, commit_id, &refs, repo);
5035 if (error)
5036 goto done;
5037 error = view_loop(view);
5038 done:
5039 free(repo_path);
5040 free(commit_id);
5041 if (commit)
5042 got_object_commit_close(commit);
5043 if (tree)
5044 got_object_tree_close(tree);
5045 if (repo)
5046 got_repo_close(repo);
5047 got_ref_list_free(&refs);
5048 return error;
5051 static void
5052 list_commands(void)
5054 int i;
5056 fprintf(stderr, "commands:");
5057 for (i = 0; i < nitems(tog_commands); i++) {
5058 struct tog_cmd *cmd = &tog_commands[i];
5059 fprintf(stderr, " %s", cmd->name);
5061 fputc('\n', stderr);
5064 __dead static void
5065 usage(int hflag)
5067 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5068 getprogname());
5069 if (hflag)
5070 list_commands();
5071 exit(1);
5074 static char **
5075 make_argv(const char *arg0, const char *arg1)
5077 char **argv;
5078 int argc = (arg1 == NULL ? 1 : 2);
5080 argv = calloc(argc, sizeof(char *));
5081 if (argv == NULL)
5082 err(1, "calloc");
5083 argv[0] = strdup(arg0);
5084 if (argv[0] == NULL)
5085 err(1, "strdup");
5086 if (arg1) {
5087 argv[1] = strdup(arg1);
5088 if (argv[1] == NULL)
5089 err(1, "strdup");
5092 return argv;
5095 int
5096 main(int argc, char *argv[])
5098 const struct got_error *error = NULL;
5099 struct tog_cmd *cmd = NULL;
5100 int ch, hflag = 0, Vflag = 0;
5101 char **cmd_argv = NULL;
5103 setlocale(LC_CTYPE, "");
5105 while ((ch = getopt(argc, argv, "hV")) != -1) {
5106 switch (ch) {
5107 case 'h':
5108 hflag = 1;
5109 break;
5110 case 'V':
5111 Vflag = 1;
5112 break;
5113 default:
5114 usage(hflag);
5115 /* NOTREACHED */
5119 argc -= optind;
5120 argv += optind;
5121 optind = 0;
5122 optreset = 1;
5124 if (Vflag) {
5125 got_version_print_str();
5126 return 1;
5129 if (argc == 0) {
5130 if (hflag)
5131 usage(hflag);
5132 /* Build an argument vector which runs a default command. */
5133 cmd = &tog_commands[0];
5134 cmd_argv = make_argv(cmd->name, NULL);
5135 argc = 1;
5136 } else {
5137 int i;
5139 /* Did the user specific a command? */
5140 for (i = 0; i < nitems(tog_commands); i++) {
5141 if (strncmp(tog_commands[i].name, argv[0],
5142 strlen(argv[0])) == 0) {
5143 cmd = &tog_commands[i];
5144 break;
5148 if (cmd == NULL) {
5149 fprintf(stderr, "%s: unknown command '%s'\n",
5150 getprogname(), argv[0]);
5151 list_commands();
5152 return 1;
5156 if (hflag)
5157 cmd->cmd_usage();
5158 else
5159 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5161 endwin();
5162 free(cmd_argv);
5163 if (error && error->code != GOT_ERR_CANCELLED)
5164 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5165 return 0;