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 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1883 NULL, NULL);
1884 if (err)
1885 return (void *)err;
1887 while (!done && !err && !tog_sigpipe_received) {
1888 err = queue_commits(a->graph, a->commits, 1, a->repo,
1889 a->in_repo_path, a->searching, a->search_next_done,
1890 a->regex);
1891 if (err) {
1892 if (err->code != GOT_ERR_ITER_COMPLETED)
1893 return (void *)err;
1894 err = NULL;
1895 done = 1;
1896 } else if (a->commits_needed > 0)
1897 a->commits_needed--;
1899 errcode = pthread_mutex_lock(&tog_mutex);
1900 if (errcode) {
1901 err = got_error_set_errno(errcode,
1902 "pthread_mutex_lock");
1903 break;
1904 } else if (*a->quit)
1905 done = 1;
1906 else if (*a->first_displayed_entry == NULL) {
1907 *a->first_displayed_entry =
1908 TAILQ_FIRST(&a->commits->head);
1909 *a->selected_entry = *a->first_displayed_entry;
1912 if (done)
1913 a->commits_needed = 0;
1914 else if (a->commits_needed == 0) {
1915 errcode = pthread_cond_wait(&a->need_commits,
1916 &tog_mutex);
1917 if (errcode)
1918 err = got_error_set_errno(errcode,
1919 "pthread_cond_wait");
1922 errcode = pthread_mutex_unlock(&tog_mutex);
1923 if (errcode && err == NULL)
1924 err = got_error_set_errno(errcode,
1925 "pthread_mutex_unlock");
1927 a->log_complete = 1;
1928 return (void *)err;
1931 static const struct got_error *
1932 stop_log_thread(struct tog_log_view_state *s)
1934 const struct got_error *err = NULL;
1935 int errcode;
1937 if (s->thread) {
1938 s->quit = 1;
1939 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1940 if (errcode)
1941 return got_error_set_errno(errcode,
1942 "pthread_cond_signal");
1943 errcode = pthread_mutex_unlock(&tog_mutex);
1944 if (errcode)
1945 return got_error_set_errno(errcode,
1946 "pthread_mutex_unlock");
1947 errcode = pthread_join(s->thread, (void **)&err);
1948 if (errcode)
1949 return got_error_set_errno(errcode, "pthread_join");
1950 errcode = pthread_mutex_lock(&tog_mutex);
1951 if (errcode)
1952 return got_error_set_errno(errcode,
1953 "pthread_mutex_lock");
1954 s->thread = NULL;
1957 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1958 if (errcode && err == NULL)
1959 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1961 if (s->thread_args.repo) {
1962 got_repo_close(s->thread_args.repo);
1963 s->thread_args.repo = NULL;
1966 if (s->thread_args.graph) {
1967 got_commit_graph_close(s->thread_args.graph);
1968 s->thread_args.graph = NULL;
1971 return err;
1974 static const struct got_error *
1975 close_log_view(struct tog_view *view)
1977 const struct got_error *err = NULL;
1978 struct tog_log_view_state *s = &view->state.log;
1980 err = stop_log_thread(s);
1981 free_commits(&s->commits);
1982 free(s->in_repo_path);
1983 s->in_repo_path = NULL;
1984 free(s->start_id);
1985 s->start_id = NULL;
1986 return err;
1989 static const struct got_error *
1990 search_start_log_view(struct tog_view *view)
1992 struct tog_log_view_state *s = &view->state.log;
1994 s->matched_entry = NULL;
1995 s->search_entry = NULL;
1996 return NULL;
1999 static const struct got_error *
2000 search_next_log_view(struct tog_view *view)
2002 const struct got_error *err = NULL;
2003 struct tog_log_view_state *s = &view->state.log;
2004 struct commit_queue_entry *entry;
2006 if (!view->searching) {
2007 view->search_next_done = 1;
2008 return NULL;
2011 if (s->search_entry) {
2012 int errcode, ch;
2013 errcode = pthread_mutex_unlock(&tog_mutex);
2014 if (errcode)
2015 return got_error_set_errno(errcode,
2016 "pthread_mutex_unlock");
2017 ch = wgetch(view->window);
2018 errcode = pthread_mutex_lock(&tog_mutex);
2019 if (errcode)
2020 return got_error_set_errno(errcode,
2021 "pthread_mutex_lock");
2022 if (ch == KEY_BACKSPACE) {
2023 view->search_next_done = 1;
2024 return NULL;
2026 if (view->searching == TOG_SEARCH_FORWARD)
2027 entry = TAILQ_NEXT(s->search_entry, entry);
2028 else
2029 entry = TAILQ_PREV(s->search_entry,
2030 commit_queue_head, entry);
2031 } else if (s->matched_entry) {
2032 if (view->searching == TOG_SEARCH_FORWARD)
2033 entry = TAILQ_NEXT(s->selected_entry, entry);
2034 else
2035 entry = TAILQ_PREV(s->selected_entry,
2036 commit_queue_head, entry);
2037 } else {
2038 if (view->searching == TOG_SEARCH_FORWARD)
2039 entry = TAILQ_FIRST(&s->commits.head);
2040 else
2041 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2044 while (1) {
2045 int have_match = 0;
2047 if (entry == NULL) {
2048 if (s->thread_args.log_complete ||
2049 view->searching == TOG_SEARCH_BACKWARD) {
2050 view->search_next_done = 1;
2051 return NULL;
2054 * Poke the log thread for more commits and return,
2055 * allowing the main loop to make progress. Search
2056 * will resume at s->search_entry once we come back.
2058 s->thread_args.commits_needed++;
2059 return trigger_log_thread(1,
2060 &s->thread_args.commits_needed,
2061 &s->thread_args.log_complete,
2062 &s->thread_args.need_commits);
2065 err = match_commit(&have_match, entry->id, entry->commit,
2066 &view->regex);
2067 if (err)
2068 break;
2069 if (have_match) {
2070 view->search_next_done = 1;
2071 s->matched_entry = entry;
2072 break;
2075 s->search_entry = entry;
2076 if (view->searching == TOG_SEARCH_FORWARD)
2077 entry = TAILQ_NEXT(entry, entry);
2078 else
2079 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2082 if (s->matched_entry) {
2083 int cur = s->selected_entry->idx;
2084 while (cur < s->matched_entry->idx) {
2085 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2086 if (err)
2087 return err;
2088 cur++;
2090 while (cur > s->matched_entry->idx) {
2091 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2092 if (err)
2093 return err;
2094 cur--;
2098 s->search_entry = NULL;
2100 return NULL;
2103 static const struct got_error *
2104 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2105 struct got_reflist_head *refs, struct got_repository *repo,
2106 const char *head_ref_name, const char *path, int check_disk)
2108 const struct got_error *err = NULL;
2109 struct tog_log_view_state *s = &view->state.log;
2110 struct got_repository *thread_repo = NULL;
2111 struct got_commit_graph *thread_graph = NULL;
2112 int errcode;
2114 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2115 if (err != NULL)
2116 goto done;
2118 /* The commit queue only contains commits being displayed. */
2119 TAILQ_INIT(&s->commits.head);
2120 s->commits.ncommits = 0;
2122 s->refs = refs;
2123 s->repo = repo;
2124 s->head_ref_name = head_ref_name;
2125 s->start_id = got_object_id_dup(start_id);
2126 if (s->start_id == NULL) {
2127 err = got_error_from_errno("got_object_id_dup");
2128 goto done;
2131 SIMPLEQ_INIT(&s->colors);
2132 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2133 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2134 get_color_value("TOG_COLOR_COMMIT"));
2135 if (err)
2136 goto done;
2137 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2138 get_color_value("TOG_COLOR_AUTHOR"));
2139 if (err) {
2140 free_colors(&s->colors);
2141 goto done;
2143 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2144 get_color_value("TOG_COLOR_DATE"));
2145 if (err) {
2146 free_colors(&s->colors);
2147 goto done;
2151 view->show = show_log_view;
2152 view->input = input_log_view;
2153 view->close = close_log_view;
2154 view->search_start = search_start_log_view;
2155 view->search_next = search_next_log_view;
2157 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2158 if (err)
2159 goto done;
2160 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2161 if (err)
2162 goto done;
2164 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2165 if (errcode) {
2166 err = got_error_set_errno(errcode, "pthread_cond_init");
2167 goto done;
2170 s->thread_args.commits_needed = view->nlines;
2171 s->thread_args.graph = thread_graph;
2172 s->thread_args.commits = &s->commits;
2173 s->thread_args.in_repo_path = s->in_repo_path;
2174 s->thread_args.start_id = s->start_id;
2175 s->thread_args.repo = thread_repo;
2176 s->thread_args.log_complete = 0;
2177 s->thread_args.quit = &s->quit;
2178 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2179 s->thread_args.selected_entry = &s->selected_entry;
2180 s->thread_args.searching = &view->searching;
2181 s->thread_args.search_next_done = &view->search_next_done;
2182 s->thread_args.regex = &view->regex;
2183 done:
2184 if (err)
2185 close_log_view(view);
2186 return err;
2189 static const struct got_error *
2190 show_log_view(struct tog_view *view)
2192 struct tog_log_view_state *s = &view->state.log;
2194 if (s->thread == NULL) {
2195 int errcode = pthread_create(&s->thread, NULL, log_thread,
2196 &s->thread_args);
2197 if (errcode)
2198 return got_error_set_errno(errcode, "pthread_create");
2201 return draw_commits(view, &s->last_displayed_entry,
2202 &s->selected_entry, s->first_displayed_entry,
2203 &s->commits, s->selected, view->nlines, s->refs,
2204 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2207 static const struct got_error *
2208 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2209 struct tog_view **focus_view, struct tog_view *view, int ch)
2211 const struct got_error *err = NULL;
2212 struct tog_log_view_state *s = &view->state.log;
2213 char *parent_path, *in_repo_path = NULL;
2214 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2215 int begin_x = 0;
2216 struct got_object_id *start_id;
2218 switch (ch) {
2219 case 'q':
2220 s->quit = 1;
2221 break;
2222 case 'k':
2223 case KEY_UP:
2224 case '<':
2225 case ',':
2226 if (s->first_displayed_entry == NULL)
2227 break;
2228 if (s->selected > 0)
2229 s->selected--;
2230 else
2231 scroll_up(view, &s->first_displayed_entry, 1,
2232 &s->commits);
2233 break;
2234 case KEY_PPAGE:
2235 case CTRL('b'):
2236 if (s->first_displayed_entry == NULL)
2237 break;
2238 if (TAILQ_FIRST(&s->commits.head) ==
2239 s->first_displayed_entry) {
2240 s->selected = 0;
2241 break;
2243 scroll_up(view, &s->first_displayed_entry,
2244 view->nlines, &s->commits);
2245 break;
2246 case 'j':
2247 case KEY_DOWN:
2248 case '>':
2249 case '.':
2250 if (s->first_displayed_entry == NULL)
2251 break;
2252 if (s->selected < MIN(view->nlines - 2,
2253 s->commits.ncommits - 1)) {
2254 s->selected++;
2255 break;
2257 err = scroll_down(view, &s->first_displayed_entry, 1,
2258 &s->last_displayed_entry, &s->commits,
2259 &s->thread_args.log_complete,
2260 &s->thread_args.commits_needed,
2261 &s->thread_args.need_commits);
2262 break;
2263 case KEY_NPAGE:
2264 case CTRL('f'): {
2265 struct commit_queue_entry *first;
2266 first = s->first_displayed_entry;
2267 if (first == NULL)
2268 break;
2269 err = scroll_down(view, &s->first_displayed_entry,
2270 view->nlines, &s->last_displayed_entry,
2271 &s->commits, &s->thread_args.log_complete,
2272 &s->thread_args.commits_needed,
2273 &s->thread_args.need_commits);
2274 if (err)
2275 break;
2276 if (first == s->first_displayed_entry &&
2277 s->selected < MIN(view->nlines - 2,
2278 s->commits.ncommits - 1)) {
2279 /* can't scroll further down */
2280 s->selected = MIN(view->nlines - 2,
2281 s->commits.ncommits - 1);
2283 err = NULL;
2284 break;
2286 case KEY_RESIZE:
2287 if (s->selected > view->nlines - 2)
2288 s->selected = view->nlines - 2;
2289 if (s->selected > s->commits.ncommits - 1)
2290 s->selected = s->commits.ncommits - 1;
2291 break;
2292 case KEY_ENTER:
2293 case ' ':
2294 case '\r':
2295 if (s->selected_entry == NULL)
2296 break;
2297 if (view_is_parent_view(view))
2298 begin_x = view_split_begin_x(view->begin_x);
2299 err = open_diff_view_for_commit(&diff_view, begin_x,
2300 s->selected_entry->commit, s->selected_entry->id,
2301 view, s->refs, s->repo);
2302 if (err)
2303 break;
2304 if (view_is_parent_view(view)) {
2305 err = view_close_child(view);
2306 if (err)
2307 return err;
2308 err = view_set_child(view, diff_view);
2309 if (err) {
2310 view_close(diff_view);
2311 break;
2313 *focus_view = diff_view;
2314 view->child_focussed = 1;
2315 } else
2316 *new_view = diff_view;
2317 break;
2318 case 't':
2319 if (s->selected_entry == NULL)
2320 break;
2321 if (view_is_parent_view(view))
2322 begin_x = view_split_begin_x(view->begin_x);
2323 err = browse_commit_tree(&tree_view, begin_x,
2324 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2325 if (err)
2326 break;
2327 if (view_is_parent_view(view)) {
2328 err = view_close_child(view);
2329 if (err)
2330 return err;
2331 err = view_set_child(view, tree_view);
2332 if (err) {
2333 view_close(tree_view);
2334 break;
2336 *focus_view = tree_view;
2337 view->child_focussed = 1;
2338 } else
2339 *new_view = tree_view;
2340 break;
2341 case KEY_BACKSPACE:
2342 if (strcmp(s->in_repo_path, "/") == 0)
2343 break;
2344 parent_path = dirname(s->in_repo_path);
2345 if (parent_path && strcmp(parent_path, ".") != 0) {
2346 err = stop_log_thread(s);
2347 if (err)
2348 return err;
2349 lv = view_open(view->nlines, view->ncols,
2350 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2351 if (lv == NULL)
2352 return got_error_from_errno(
2353 "view_open");
2354 err = open_log_view(lv, s->start_id, s->refs,
2355 s->repo, s->head_ref_name, parent_path, 0);
2356 if (err)
2357 return err;;
2358 if (view_is_parent_view(view))
2359 *new_view = lv;
2360 else {
2361 view_set_child(view->parent, lv);
2362 *focus_view = lv;
2364 return NULL;
2366 break;
2367 case CTRL('l'):
2368 err = stop_log_thread(s);
2369 if (err)
2370 return err;
2371 lv = view_open(view->nlines, view->ncols,
2372 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2373 if (lv == NULL)
2374 return got_error_from_errno("view_open");
2375 err = get_head_commit_id(&start_id, s->head_ref_name ?
2376 s->head_ref_name : GOT_REF_HEAD, s->repo);
2377 if (err) {
2378 view_close(lv);
2379 return err;
2381 in_repo_path = strdup(s->in_repo_path);
2382 if (in_repo_path == NULL) {
2383 free(start_id);
2384 view_close(lv);
2385 return got_error_from_errno("strdup");
2387 got_ref_list_free(s->refs);
2388 err = got_ref_list(s->refs, s->repo, NULL,
2389 got_ref_cmp_by_name, NULL);
2390 if (err) {
2391 free(start_id);
2392 view_close(lv);
2393 return err;
2395 err = open_log_view(lv, start_id, s->refs, s->repo,
2396 s->head_ref_name, in_repo_path, 0);
2397 if (err) {
2398 free(start_id);
2399 view_close(lv);
2400 return err;;
2402 *dead_view = view;
2403 *new_view = lv;
2404 break;
2405 default:
2406 break;
2409 return err;
2412 static const struct got_error *
2413 apply_unveil(const char *repo_path, const char *worktree_path)
2415 const struct got_error *error;
2417 #ifdef PROFILE
2418 if (unveil("gmon.out", "rwc") != 0)
2419 return got_error_from_errno2("unveil", "gmon.out");
2420 #endif
2421 if (repo_path && unveil(repo_path, "r") != 0)
2422 return got_error_from_errno2("unveil", repo_path);
2424 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2425 return got_error_from_errno2("unveil", worktree_path);
2427 if (unveil("/tmp", "rwc") != 0)
2428 return got_error_from_errno2("unveil", "/tmp");
2430 error = got_privsep_unveil_exec_helpers();
2431 if (error != NULL)
2432 return error;
2434 if (unveil(NULL, NULL) != 0)
2435 return got_error_from_errno("unveil");
2437 return NULL;
2440 static void
2441 init_curses(void)
2443 initscr();
2444 cbreak();
2445 halfdelay(1); /* Do fast refresh while initial view is loading. */
2446 noecho();
2447 nonl();
2448 intrflush(stdscr, FALSE);
2449 keypad(stdscr, TRUE);
2450 curs_set(0);
2451 if (getenv("TOG_COLORS") != NULL) {
2452 start_color();
2453 use_default_colors();
2455 signal(SIGWINCH, tog_sigwinch);
2456 signal(SIGPIPE, tog_sigpipe);
2459 static const struct got_error *
2460 cmd_log(int argc, char *argv[])
2462 const struct got_error *error;
2463 struct got_repository *repo = NULL;
2464 struct got_worktree *worktree = NULL;
2465 struct got_reflist_head refs;
2466 struct got_object_id *start_id = NULL;
2467 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2468 char *start_commit = NULL, *head_ref_name = NULL;
2469 int ch;
2470 struct tog_view *view;
2472 SIMPLEQ_INIT(&refs);
2474 #ifndef PROFILE
2475 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2476 NULL) == -1)
2477 err(1, "pledge");
2478 #endif
2480 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2481 switch (ch) {
2482 case 'c':
2483 start_commit = optarg;
2484 break;
2485 case 'r':
2486 repo_path = realpath(optarg, NULL);
2487 if (repo_path == NULL)
2488 return got_error_from_errno2("realpath",
2489 optarg);
2490 break;
2491 default:
2492 usage_log();
2493 /* NOTREACHED */
2497 argc -= optind;
2498 argv += optind;
2500 cwd = getcwd(NULL, 0);
2501 if (cwd == NULL) {
2502 error = got_error_from_errno("getcwd");
2503 goto done;
2505 error = got_worktree_open(&worktree, cwd);
2506 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2507 goto done;
2508 error = NULL;
2510 if (argc == 0) {
2511 path = strdup("");
2512 if (path == NULL) {
2513 error = got_error_from_errno("strdup");
2514 goto done;
2516 } else if (argc == 1) {
2517 if (worktree) {
2518 error = got_worktree_resolve_path(&path, worktree,
2519 argv[0]);
2520 if (error)
2521 goto done;
2522 } else {
2523 path = strdup(argv[0]);
2524 if (path == NULL) {
2525 error = got_error_from_errno("strdup");
2526 goto done;
2529 } else
2530 usage_log();
2532 if (repo_path == NULL) {
2533 if (worktree)
2534 repo_path = strdup(
2535 got_worktree_get_repo_path(worktree));
2536 else
2537 repo_path = strdup(cwd);
2539 if (repo_path == NULL) {
2540 error = got_error_from_errno("strdup");
2541 goto done;
2544 init_curses();
2546 error = got_repo_open(&repo, repo_path, NULL);
2547 if (error != NULL)
2548 goto done;
2550 error = apply_unveil(got_repo_get_path(repo),
2551 worktree ? got_worktree_get_root_path(worktree) : NULL);
2552 if (error)
2553 goto done;
2555 if (start_commit == NULL)
2556 error = get_head_commit_id(&start_id, worktree ?
2557 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2558 repo);
2559 else {
2560 error = get_head_commit_id(&start_id, start_commit, repo);
2561 if (error) {
2562 if (error->code != GOT_ERR_NOT_REF)
2563 goto done;
2564 error = got_repo_match_object_id_prefix(&start_id,
2565 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2568 if (error != NULL)
2569 goto done;
2571 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2572 if (error)
2573 goto done;
2575 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2576 if (view == NULL) {
2577 error = got_error_from_errno("view_open");
2578 goto done;
2580 if (worktree) {
2581 head_ref_name = strdup(
2582 got_worktree_get_head_ref_name(worktree));
2583 if (head_ref_name == NULL) {
2584 error = got_error_from_errno("strdup");
2585 goto done;
2588 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2589 path, 1);
2590 if (error)
2591 goto done;
2592 if (worktree) {
2593 /* Release work tree lock. */
2594 got_worktree_close(worktree);
2595 worktree = NULL;
2597 error = view_loop(view);
2598 done:
2599 free(repo_path);
2600 free(cwd);
2601 free(path);
2602 free(start_id);
2603 free(head_ref_name);
2604 if (repo)
2605 got_repo_close(repo);
2606 if (worktree)
2607 got_worktree_close(worktree);
2608 got_ref_list_free(&refs);
2609 return error;
2612 __dead static void
2613 usage_diff(void)
2615 endwin();
2616 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2617 getprogname());
2618 exit(1);
2621 static char *
2622 parse_next_line(FILE *f, size_t *len)
2624 char *line;
2625 size_t linelen;
2626 size_t lineno;
2627 const char delim[3] = { '\0', '\0', '\0'};
2629 line = fparseln(f, &linelen, &lineno, delim, 0);
2630 if (len)
2631 *len = linelen;
2632 return line;
2635 static int
2636 match_line(const char *line, regex_t *regex)
2638 regmatch_t regmatch;
2640 return regexec(regex, line, 1, &regmatch, 0) == 0;
2643 struct tog_color *
2644 match_color(struct tog_colors *colors, const char *line)
2646 struct tog_color *tc = NULL;
2648 SIMPLEQ_FOREACH(tc, colors, entry) {
2649 if (match_line(line, &tc->regex))
2650 return tc;
2653 return NULL;
2656 static const struct got_error *
2657 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2658 int *last_displayed_line, int *eof, int max_lines, char *header,
2659 struct tog_colors *colors)
2661 const struct got_error *err;
2662 int nlines = 0, nprinted = 0;
2663 char *line;
2664 struct tog_color *tc;
2665 size_t len;
2666 wchar_t *wline;
2667 int width;
2669 rewind(f);
2670 werase(view->window);
2672 if (header) {
2673 err = format_line(&wline, &width, header, view->ncols, 0);
2674 if (err) {
2675 return err;
2678 if (view_needs_focus_indication(view))
2679 wstandout(view->window);
2680 waddwstr(view->window, wline);
2681 if (view_needs_focus_indication(view))
2682 wstandend(view->window);
2683 if (width <= view->ncols - 1)
2684 waddch(view->window, '\n');
2686 if (max_lines <= 1)
2687 return NULL;
2688 max_lines--;
2691 *eof = 0;
2692 while (nprinted < max_lines) {
2693 line = parse_next_line(f, &len);
2694 if (line == NULL) {
2695 *eof = 1;
2696 break;
2698 if (++nlines < *first_displayed_line) {
2699 free(line);
2700 continue;
2703 err = format_line(&wline, &width, line, view->ncols, 0);
2704 if (err) {
2705 free(line);
2706 return err;
2709 tc = match_color(colors, line);
2710 if (tc)
2711 wattr_on(view->window,
2712 COLOR_PAIR(tc->colorpair), NULL);
2713 waddwstr(view->window, wline);
2714 if (tc)
2715 wattr_off(view->window,
2716 COLOR_PAIR(tc->colorpair), NULL);
2717 if (width <= view->ncols - 1)
2718 waddch(view->window, '\n');
2719 if (++nprinted == 1)
2720 *first_displayed_line = nlines;
2721 free(line);
2722 free(wline);
2723 wline = NULL;
2725 *last_displayed_line = nlines;
2727 view_vborder(view);
2729 if (*eof) {
2730 while (nprinted < view->nlines) {
2731 waddch(view->window, '\n');
2732 nprinted++;
2735 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2736 if (err) {
2737 return err;
2740 wstandout(view->window);
2741 waddwstr(view->window, wline);
2742 wstandend(view->window);
2745 return NULL;
2748 static char *
2749 get_datestr(time_t *time, char *datebuf)
2751 struct tm mytm, *tm;
2752 char *p, *s;
2754 tm = gmtime_r(time, &mytm);
2755 if (tm == NULL)
2756 return NULL;
2757 s = asctime_r(tm, datebuf);
2758 if (s == NULL)
2759 return NULL;
2760 p = strchr(s, '\n');
2761 if (p)
2762 *p = '\0';
2763 return s;
2766 static const struct got_error *
2767 write_commit_info(struct got_object_id *commit_id,
2768 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2770 const struct got_error *err = NULL;
2771 char datebuf[26], *datestr;
2772 struct got_commit_object *commit;
2773 char *id_str = NULL, *logmsg = NULL;
2774 time_t committer_time;
2775 const char *author, *committer;
2776 char *refs_str = NULL;
2778 if (refs) {
2779 err = build_refs_str(&refs_str, refs, commit_id, repo);
2780 if (err)
2781 return err;
2784 err = got_object_open_as_commit(&commit, repo, commit_id);
2785 if (err)
2786 return err;
2788 err = got_object_id_str(&id_str, commit_id);
2789 if (err) {
2790 err = got_error_from_errno("got_object_id_str");
2791 goto done;
2794 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2795 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2796 err = got_error_from_errno("fprintf");
2797 goto done;
2799 if (fprintf(outfile, "from: %s\n",
2800 got_object_commit_get_author(commit)) < 0) {
2801 err = got_error_from_errno("fprintf");
2802 goto done;
2804 committer_time = got_object_commit_get_committer_time(commit);
2805 datestr = get_datestr(&committer_time, datebuf);
2806 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2807 err = got_error_from_errno("fprintf");
2808 goto done;
2810 author = got_object_commit_get_author(commit);
2811 committer = got_object_commit_get_committer(commit);
2812 if (strcmp(author, committer) != 0 &&
2813 fprintf(outfile, "via: %s\n", committer) < 0) {
2814 err = got_error_from_errno("fprintf");
2815 goto done;
2817 err = got_object_commit_get_logmsg(&logmsg, commit);
2818 if (err)
2819 goto done;
2820 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2821 err = got_error_from_errno("fprintf");
2822 goto done;
2824 done:
2825 free(id_str);
2826 free(logmsg);
2827 free(refs_str);
2828 got_object_commit_close(commit);
2829 return err;
2832 static const struct got_error *
2833 create_diff(struct tog_diff_view_state *s)
2835 const struct got_error *err = NULL;
2836 FILE *f = NULL;
2837 int obj_type;
2839 f = got_opentemp();
2840 if (f == NULL) {
2841 err = got_error_from_errno("got_opentemp");
2842 goto done;
2844 if (s->f && fclose(s->f) != 0) {
2845 err = got_error_from_errno("fclose");
2846 goto done;
2848 s->f = f;
2850 if (s->id1)
2851 err = got_object_get_type(&obj_type, s->repo, s->id1);
2852 else
2853 err = got_object_get_type(&obj_type, s->repo, s->id2);
2854 if (err)
2855 goto done;
2857 switch (obj_type) {
2858 case GOT_OBJ_TYPE_BLOB:
2859 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2860 s->diff_context, 0, s->repo, f);
2861 break;
2862 case GOT_OBJ_TYPE_TREE:
2863 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2864 s->diff_context, 0, s->repo, f);
2865 break;
2866 case GOT_OBJ_TYPE_COMMIT: {
2867 const struct got_object_id_queue *parent_ids;
2868 struct got_object_qid *pid;
2869 struct got_commit_object *commit2;
2871 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2872 if (err)
2873 break;
2874 /* Show commit info if we're diffing to a parent/root commit. */
2875 if (s->id1 == NULL)
2876 write_commit_info(s->id2, s->refs, s->repo, f);
2877 else {
2878 parent_ids = got_object_commit_get_parent_ids(commit2);
2879 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2880 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2881 write_commit_info(s->id2, s->refs,
2882 s->repo, f);
2883 break;
2887 got_object_commit_close(commit2);
2889 err = got_diff_objects_as_commits(s->id1, s->id2,
2890 s->diff_context, 0, s->repo, f);
2891 break;
2893 default:
2894 err = got_error(GOT_ERR_OBJ_TYPE);
2895 break;
2897 done:
2898 if (f && fflush(f) != 0 && err == NULL)
2899 err = got_error_from_errno("fflush");
2900 return err;
2903 static void
2904 diff_view_indicate_progress(struct tog_view *view)
2906 mvwaddstr(view->window, 0, 0, "diffing...");
2907 update_panels();
2908 doupdate();
2911 static const struct got_error *
2912 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2913 struct got_object_id *id2, struct tog_view *log_view,
2914 struct got_reflist_head *refs, struct got_repository *repo)
2916 const struct got_error *err;
2918 if (id1 != NULL && id2 != NULL) {
2919 int type1, type2;
2920 err = got_object_get_type(&type1, repo, id1);
2921 if (err)
2922 return err;
2923 err = got_object_get_type(&type2, repo, id2);
2924 if (err)
2925 return err;
2927 if (type1 != type2)
2928 return got_error(GOT_ERR_OBJ_TYPE);
2931 if (id1) {
2932 view->state.diff.id1 = got_object_id_dup(id1);
2933 if (view->state.diff.id1 == NULL)
2934 return got_error_from_errno("got_object_id_dup");
2935 } else
2936 view->state.diff.id1 = NULL;
2938 view->state.diff.id2 = got_object_id_dup(id2);
2939 if (view->state.diff.id2 == NULL) {
2940 free(view->state.diff.id1);
2941 view->state.diff.id1 = NULL;
2942 return got_error_from_errno("got_object_id_dup");
2944 view->state.diff.f = NULL;
2945 view->state.diff.first_displayed_line = 1;
2946 view->state.diff.last_displayed_line = view->nlines;
2947 view->state.diff.diff_context = 3;
2948 view->state.diff.log_view = log_view;
2949 view->state.diff.repo = repo;
2950 view->state.diff.refs = refs;
2951 SIMPLEQ_INIT(&view->state.diff.colors);
2953 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2954 err = add_color(&view->state.diff.colors,
2955 "^-", TOG_COLOR_DIFF_MINUS,
2956 get_color_value("TOG_COLOR_DIFF_MINUS"));
2957 if (err)
2958 return err;
2959 err = add_color(&view->state.diff.colors, "^\\+",
2960 TOG_COLOR_DIFF_PLUS,
2961 get_color_value("TOG_COLOR_DIFF_PLUS"));
2962 if (err) {
2963 free_colors(&view->state.diff.colors);
2964 return err;
2966 err = add_color(&view->state.diff.colors,
2967 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2968 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2969 if (err) {
2970 free_colors(&view->state.diff.colors);
2971 return err;
2974 err = add_color(&view->state.diff.colors,
2975 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2976 get_color_value("TOG_COLOR_DIFF_META"));
2977 if (err) {
2978 free_colors(&view->state.diff.colors);
2979 return err;
2982 err = add_color(&view->state.diff.colors,
2983 "^(from|via): ", TOG_COLOR_AUTHOR,
2984 get_color_value("TOG_COLOR_AUTHOR"));
2985 if (err) {
2986 free_colors(&view->state.diff.colors);
2987 return err;
2990 err = add_color(&view->state.diff.colors,
2991 "^date: ", TOG_COLOR_DATE,
2992 get_color_value("TOG_COLOR_DATE"));
2993 if (err) {
2994 free_colors(&view->state.diff.colors);
2995 return err;
2999 if (log_view && view_is_splitscreen(view))
3000 show_log_view(log_view); /* draw vborder */
3001 diff_view_indicate_progress(view);
3003 err = create_diff(&view->state.diff);
3004 if (err) {
3005 free(view->state.diff.id1);
3006 view->state.diff.id1 = NULL;
3007 free(view->state.diff.id2);
3008 view->state.diff.id2 = NULL;
3009 return err;
3012 view->show = show_diff_view;
3013 view->input = input_diff_view;
3014 view->close = close_diff_view;
3016 return NULL;
3019 static const struct got_error *
3020 close_diff_view(struct tog_view *view)
3022 const struct got_error *err = NULL;
3024 free(view->state.diff.id1);
3025 view->state.diff.id1 = NULL;
3026 free(view->state.diff.id2);
3027 view->state.diff.id2 = NULL;
3028 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3029 err = got_error_from_errno("fclose");
3030 free_colors(&view->state.diff.colors);
3031 return err;
3034 static const struct got_error *
3035 show_diff_view(struct tog_view *view)
3037 const struct got_error *err;
3038 struct tog_diff_view_state *s = &view->state.diff;
3039 char *id_str1 = NULL, *id_str2, *header;
3041 if (s->id1) {
3042 err = got_object_id_str(&id_str1, s->id1);
3043 if (err)
3044 return err;
3046 err = got_object_id_str(&id_str2, s->id2);
3047 if (err)
3048 return err;
3050 if (asprintf(&header, "diff %s %s",
3051 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3052 err = got_error_from_errno("asprintf");
3053 free(id_str1);
3054 free(id_str2);
3055 return err;
3057 free(id_str1);
3058 free(id_str2);
3060 return draw_file(view, s->f, &s->first_displayed_line,
3061 &s->last_displayed_line, &s->eof, view->nlines,
3062 header, &s->colors);
3065 static const struct got_error *
3066 set_selected_commit(struct tog_diff_view_state *s,
3067 struct commit_queue_entry *entry)
3069 const struct got_error *err;
3070 const struct got_object_id_queue *parent_ids;
3071 struct got_commit_object *selected_commit;
3072 struct got_object_qid *pid;
3074 free(s->id2);
3075 s->id2 = got_object_id_dup(entry->id);
3076 if (s->id2 == NULL)
3077 return got_error_from_errno("got_object_id_dup");
3079 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3080 if (err)
3081 return err;
3082 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3083 free(s->id1);
3084 pid = SIMPLEQ_FIRST(parent_ids);
3085 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3086 got_object_commit_close(selected_commit);
3087 return NULL;
3090 static const struct got_error *
3091 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3092 struct tog_view **focus_view, struct tog_view *view, int ch)
3094 const struct got_error *err = NULL;
3095 struct tog_diff_view_state *s = &view->state.diff;
3096 struct tog_log_view_state *ls;
3097 struct commit_queue_entry *entry;
3098 int i;
3100 switch (ch) {
3101 case 'k':
3102 case KEY_UP:
3103 if (s->first_displayed_line > 1)
3104 s->first_displayed_line--;
3105 break;
3106 case KEY_PPAGE:
3107 case CTRL('b'):
3108 if (s->first_displayed_line == 1)
3109 break;
3110 i = 0;
3111 while (i++ < view->nlines - 1 &&
3112 s->first_displayed_line > 1)
3113 s->first_displayed_line--;
3114 break;
3115 case 'j':
3116 case KEY_DOWN:
3117 if (!s->eof)
3118 s->first_displayed_line++;
3119 break;
3120 case KEY_NPAGE:
3121 case CTRL('f'):
3122 case ' ':
3123 if (s->eof)
3124 break;
3125 i = 0;
3126 while (!s->eof && i++ < view->nlines - 1) {
3127 char *line;
3128 line = parse_next_line(s->f, NULL);
3129 s->first_displayed_line++;
3130 if (line == NULL)
3131 break;
3133 break;
3134 case '[':
3135 if (s->diff_context > 0) {
3136 s->diff_context--;
3137 diff_view_indicate_progress(view);
3138 err = create_diff(s);
3140 break;
3141 case ']':
3142 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3143 s->diff_context++;
3144 diff_view_indicate_progress(view);
3145 err = create_diff(s);
3147 break;
3148 case '<':
3149 case ',':
3150 if (s->log_view == NULL)
3151 break;
3152 ls = &s->log_view->state.log;
3153 entry = TAILQ_PREV(ls->selected_entry,
3154 commit_queue_head, entry);
3155 if (entry == NULL)
3156 break;
3158 err = input_log_view(NULL, NULL, NULL, s->log_view,
3159 KEY_UP);
3160 if (err)
3161 break;
3163 err = set_selected_commit(s, entry);
3164 if (err)
3165 break;
3167 s->first_displayed_line = 1;
3168 s->last_displayed_line = view->nlines;
3170 diff_view_indicate_progress(view);
3171 err = create_diff(s);
3172 break;
3173 case '>':
3174 case '.':
3175 if (s->log_view == NULL)
3176 break;
3177 ls = &s->log_view->state.log;
3179 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3180 ls->thread_args.commits_needed++;
3182 /* Display "loading..." in log view. */
3183 show_log_view(s->log_view);
3184 update_panels();
3185 doupdate();
3187 err = trigger_log_thread(1 /* load_all */,
3188 &ls->thread_args.commits_needed,
3189 &ls->thread_args.log_complete,
3190 &ls->thread_args.need_commits);
3191 if (err)
3192 break;
3194 err = input_log_view(NULL, NULL, NULL, s->log_view,
3195 KEY_DOWN);
3196 if (err)
3197 break;
3199 entry = TAILQ_NEXT(ls->selected_entry, entry);
3200 if (entry == NULL)
3201 break;
3203 err = set_selected_commit(s, entry);
3204 if (err)
3205 break;
3207 s->first_displayed_line = 1;
3208 s->last_displayed_line = view->nlines;
3210 diff_view_indicate_progress(view);
3211 err = create_diff(s);
3212 break;
3213 default:
3214 break;
3217 return err;
3220 static const struct got_error *
3221 cmd_diff(int argc, char *argv[])
3223 const struct got_error *error = NULL;
3224 struct got_repository *repo = NULL;
3225 struct got_reflist_head refs;
3226 struct got_object_id *id1 = NULL, *id2 = NULL;
3227 char *repo_path = NULL;
3228 char *id_str1 = NULL, *id_str2 = NULL;
3229 int ch;
3230 struct tog_view *view;
3232 SIMPLEQ_INIT(&refs);
3234 #ifndef PROFILE
3235 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3236 NULL) == -1)
3237 err(1, "pledge");
3238 #endif
3240 while ((ch = getopt(argc, argv, "")) != -1) {
3241 switch (ch) {
3242 default:
3243 usage_diff();
3244 /* NOTREACHED */
3248 argc -= optind;
3249 argv += optind;
3251 if (argc == 0) {
3252 usage_diff(); /* TODO show local worktree changes */
3253 } else if (argc == 2) {
3254 repo_path = getcwd(NULL, 0);
3255 if (repo_path == NULL)
3256 return got_error_from_errno("getcwd");
3257 id_str1 = argv[0];
3258 id_str2 = argv[1];
3259 } else if (argc == 3) {
3260 repo_path = realpath(argv[0], NULL);
3261 if (repo_path == NULL)
3262 return got_error_from_errno2("realpath", argv[0]);
3263 id_str1 = argv[1];
3264 id_str2 = argv[2];
3265 } else
3266 usage_diff();
3268 init_curses();
3270 error = got_repo_open(&repo, repo_path, NULL);
3271 if (error)
3272 goto done;
3274 error = apply_unveil(got_repo_get_path(repo), NULL);
3275 if (error)
3276 goto done;
3278 error = got_repo_match_object_id_prefix(&id1, id_str1,
3279 GOT_OBJ_TYPE_ANY, repo);
3280 if (error)
3281 goto done;
3283 error = got_repo_match_object_id_prefix(&id2, id_str2,
3284 GOT_OBJ_TYPE_ANY, repo);
3285 if (error)
3286 goto done;
3288 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3289 if (error)
3290 goto done;
3292 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3293 if (view == NULL) {
3294 error = got_error_from_errno("view_open");
3295 goto done;
3297 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3298 if (error)
3299 goto done;
3300 error = view_loop(view);
3301 done:
3302 free(repo_path);
3303 if (repo)
3304 got_repo_close(repo);
3305 got_ref_list_free(&refs);
3306 return error;
3309 __dead static void
3310 usage_blame(void)
3312 endwin();
3313 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3314 getprogname());
3315 exit(1);
3318 struct tog_blame_line {
3319 int annotated;
3320 struct got_object_id *id;
3323 static const struct got_error *
3324 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3325 const char *path, struct tog_blame_line *lines, int nlines,
3326 int blame_complete, int selected_line, int *first_displayed_line,
3327 int *last_displayed_line, int *eof, int max_lines,
3328 struct tog_colors *colors)
3330 const struct got_error *err;
3331 int lineno = 0, nprinted = 0;
3332 char *line;
3333 size_t len;
3334 wchar_t *wline;
3335 int width;
3336 struct tog_blame_line *blame_line;
3337 struct got_object_id *prev_id = NULL;
3338 char *id_str;
3339 struct tog_color *tc;
3341 err = got_object_id_str(&id_str, id);
3342 if (err)
3343 return err;
3345 rewind(f);
3346 werase(view->window);
3348 if (asprintf(&line, "commit %s", id_str) == -1) {
3349 err = got_error_from_errno("asprintf");
3350 free(id_str);
3351 return err;
3354 err = format_line(&wline, &width, line, view->ncols, 0);
3355 free(line);
3356 line = NULL;
3357 if (err)
3358 return err;
3359 if (view_needs_focus_indication(view))
3360 wstandout(view->window);
3361 tc = get_color(colors, TOG_COLOR_COMMIT);
3362 if (tc)
3363 wattr_on(view->window,
3364 COLOR_PAIR(tc->colorpair), NULL);
3365 waddwstr(view->window, wline);
3366 if (tc)
3367 wattr_off(view->window,
3368 COLOR_PAIR(tc->colorpair), NULL);
3369 if (view_needs_focus_indication(view))
3370 wstandend(view->window);
3371 free(wline);
3372 wline = NULL;
3373 if (width < view->ncols - 1)
3374 waddch(view->window, '\n');
3376 if (asprintf(&line, "[%d/%d] %s%s",
3377 *first_displayed_line - 1 + selected_line, nlines,
3378 blame_complete ? "" : "annotating... ", path) == -1) {
3379 free(id_str);
3380 return got_error_from_errno("asprintf");
3382 free(id_str);
3383 err = format_line(&wline, &width, line, view->ncols, 0);
3384 free(line);
3385 line = NULL;
3386 if (err)
3387 return err;
3388 waddwstr(view->window, wline);
3389 free(wline);
3390 wline = NULL;
3391 if (width < view->ncols - 1)
3392 waddch(view->window, '\n');
3394 *eof = 0;
3395 while (nprinted < max_lines - 2) {
3396 line = parse_next_line(f, &len);
3397 if (line == NULL) {
3398 *eof = 1;
3399 break;
3401 if (++lineno < *first_displayed_line) {
3402 free(line);
3403 continue;
3406 if (view->ncols <= 9) {
3407 width = 9;
3408 wline = wcsdup(L"");
3409 if (wline == NULL)
3410 err = got_error_from_errno("wcsdup");
3411 } else {
3412 err = format_line(&wline, &width, line,
3413 view->ncols - 9, 9);
3414 width += 9;
3416 if (err) {
3417 free(line);
3418 return err;
3421 if (view->focussed && nprinted == selected_line - 1)
3422 wstandout(view->window);
3424 if (nlines > 0) {
3425 blame_line = &lines[lineno - 1];
3426 if (blame_line->annotated && prev_id &&
3427 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3428 !(view->focussed &&
3429 nprinted == selected_line - 1)) {
3430 waddstr(view->window, " ");
3431 } else if (blame_line->annotated) {
3432 char *id_str;
3433 err = got_object_id_str(&id_str, blame_line->id);
3434 if (err) {
3435 free(line);
3436 free(wline);
3437 return err;
3439 tc = get_color(colors, TOG_COLOR_COMMIT);
3440 if (tc)
3441 wattr_on(view->window,
3442 COLOR_PAIR(tc->colorpair), NULL);
3443 wprintw(view->window, "%.8s", id_str);
3444 if (tc)
3445 wattr_off(view->window,
3446 COLOR_PAIR(tc->colorpair), NULL);
3447 free(id_str);
3448 prev_id = blame_line->id;
3449 } else {
3450 waddstr(view->window, "........");
3451 prev_id = NULL;
3453 } else {
3454 waddstr(view->window, "........");
3455 prev_id = NULL;
3458 if (view->focussed && nprinted == selected_line - 1)
3459 wstandend(view->window);
3460 waddstr(view->window, " ");
3462 waddwstr(view->window, wline);
3463 if (width <= view->ncols - 1)
3464 waddch(view->window, '\n');
3465 if (++nprinted == 1)
3466 *first_displayed_line = lineno;
3467 free(line);
3468 free(wline);
3469 wline = NULL;
3471 *last_displayed_line = lineno;
3473 view_vborder(view);
3475 return NULL;
3478 static const struct got_error *
3479 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3481 const struct got_error *err = NULL;
3482 struct tog_blame_cb_args *a = arg;
3483 struct tog_blame_line *line;
3484 int errcode;
3486 if (nlines != a->nlines ||
3487 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3488 return got_error(GOT_ERR_RANGE);
3490 errcode = pthread_mutex_lock(&tog_mutex);
3491 if (errcode)
3492 return got_error_set_errno(errcode, "pthread_mutex_lock");
3494 if (*a->quit) { /* user has quit the blame view */
3495 err = got_error(GOT_ERR_ITER_COMPLETED);
3496 goto done;
3499 if (lineno == -1)
3500 goto done; /* no change in this commit */
3502 line = &a->lines[lineno - 1];
3503 if (line->annotated)
3504 goto done;
3506 line->id = got_object_id_dup(id);
3507 if (line->id == NULL) {
3508 err = got_error_from_errno("got_object_id_dup");
3509 goto done;
3511 line->annotated = 1;
3512 done:
3513 errcode = pthread_mutex_unlock(&tog_mutex);
3514 if (errcode)
3515 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3516 return err;
3519 static void *
3520 blame_thread(void *arg)
3522 const struct got_error *err;
3523 struct tog_blame_thread_args *ta = arg;
3524 struct tog_blame_cb_args *a = ta->cb_args;
3525 int errcode;
3527 err = got_blame(ta->path, a->commit_id, ta->repo,
3528 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3529 if (err && err->code == GOT_ERR_CANCELLED)
3530 err = NULL;
3532 errcode = pthread_mutex_lock(&tog_mutex);
3533 if (errcode)
3534 return (void *)got_error_set_errno(errcode,
3535 "pthread_mutex_lock");
3537 got_repo_close(ta->repo);
3538 ta->repo = NULL;
3539 *ta->complete = 1;
3541 errcode = pthread_mutex_unlock(&tog_mutex);
3542 if (errcode && err == NULL)
3543 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3545 return (void *)err;
3548 static struct got_object_id *
3549 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3550 int first_displayed_line, int selected_line)
3552 struct tog_blame_line *line;
3554 if (nlines <= 0)
3555 return NULL;
3557 line = &lines[first_displayed_line - 1 + selected_line - 1];
3558 if (!line->annotated)
3559 return NULL;
3561 return line->id;
3564 static const struct got_error *
3565 stop_blame(struct tog_blame *blame)
3567 const struct got_error *err = NULL;
3568 int i;
3570 if (blame->thread) {
3571 int errcode;
3572 errcode = pthread_mutex_unlock(&tog_mutex);
3573 if (errcode)
3574 return got_error_set_errno(errcode,
3575 "pthread_mutex_unlock");
3576 errcode = pthread_join(blame->thread, (void **)&err);
3577 if (errcode)
3578 return got_error_set_errno(errcode, "pthread_join");
3579 errcode = pthread_mutex_lock(&tog_mutex);
3580 if (errcode)
3581 return got_error_set_errno(errcode,
3582 "pthread_mutex_lock");
3583 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3584 err = NULL;
3585 blame->thread = NULL;
3587 if (blame->thread_args.repo) {
3588 got_repo_close(blame->thread_args.repo);
3589 blame->thread_args.repo = NULL;
3591 if (blame->f) {
3592 if (fclose(blame->f) != 0 && err == NULL)
3593 err = got_error_from_errno("fclose");
3594 blame->f = NULL;
3596 if (blame->lines) {
3597 for (i = 0; i < blame->nlines; i++)
3598 free(blame->lines[i].id);
3599 free(blame->lines);
3600 blame->lines = NULL;
3602 free(blame->cb_args.commit_id);
3603 blame->cb_args.commit_id = NULL;
3605 return err;
3608 static const struct got_error *
3609 cancel_blame_view(void *arg)
3611 const struct got_error *err = NULL;
3612 int *done = arg;
3613 int errcode;
3615 errcode = pthread_mutex_lock(&tog_mutex);
3616 if (errcode)
3617 return got_error_set_errno(errcode,
3618 "pthread_mutex_unlock");
3620 if (*done)
3621 err = got_error(GOT_ERR_CANCELLED);
3623 errcode = pthread_mutex_unlock(&tog_mutex);
3624 if (errcode)
3625 return got_error_set_errno(errcode,
3626 "pthread_mutex_lock");
3628 return err;
3631 static const struct got_error *
3632 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3633 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3634 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3635 struct got_repository *repo)
3637 const struct got_error *err = NULL;
3638 struct got_blob_object *blob = NULL;
3639 struct got_repository *thread_repo = NULL;
3640 struct got_object_id *obj_id = NULL;
3641 int obj_type;
3643 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3644 if (err)
3645 return err;
3646 if (obj_id == NULL)
3647 return got_error(GOT_ERR_NO_OBJ);
3649 err = got_object_get_type(&obj_type, repo, obj_id);
3650 if (err)
3651 goto done;
3653 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3654 err = got_error(GOT_ERR_OBJ_TYPE);
3655 goto done;
3658 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3659 if (err)
3660 goto done;
3661 blame->f = got_opentemp();
3662 if (blame->f == NULL) {
3663 err = got_error_from_errno("got_opentemp");
3664 goto done;
3666 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3667 &blame->line_offsets, blame->f, blob);
3668 if (err || blame->nlines == 0)
3669 goto done;
3671 /* Don't include \n at EOF in the blame line count. */
3672 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3673 blame->nlines--;
3675 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3676 if (blame->lines == NULL) {
3677 err = got_error_from_errno("calloc");
3678 goto done;
3681 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3682 if (err)
3683 goto done;
3685 blame->cb_args.view = view;
3686 blame->cb_args.lines = blame->lines;
3687 blame->cb_args.nlines = blame->nlines;
3688 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3689 if (blame->cb_args.commit_id == NULL) {
3690 err = got_error_from_errno("got_object_id_dup");
3691 goto done;
3693 blame->cb_args.quit = done;
3695 blame->thread_args.path = path;
3696 blame->thread_args.repo = thread_repo;
3697 blame->thread_args.cb_args = &blame->cb_args;
3698 blame->thread_args.complete = blame_complete;
3699 blame->thread_args.cancel_cb = cancel_blame_view;
3700 blame->thread_args.cancel_arg = done;
3701 *blame_complete = 0;
3703 done:
3704 if (blob)
3705 got_object_blob_close(blob);
3706 free(obj_id);
3707 if (err)
3708 stop_blame(blame);
3709 return err;
3712 static const struct got_error *
3713 open_blame_view(struct tog_view *view, char *path,
3714 struct got_object_id *commit_id, struct got_reflist_head *refs,
3715 struct got_repository *repo)
3717 const struct got_error *err = NULL;
3718 struct tog_blame_view_state *s = &view->state.blame;
3720 SIMPLEQ_INIT(&s->blamed_commits);
3722 s->path = strdup(path);
3723 if (s->path == NULL)
3724 return got_error_from_errno("strdup");
3726 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3727 if (err) {
3728 free(s->path);
3729 return err;
3732 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3733 s->first_displayed_line = 1;
3734 s->last_displayed_line = view->nlines;
3735 s->selected_line = 1;
3736 s->blame_complete = 0;
3737 s->repo = repo;
3738 s->refs = refs;
3739 s->commit_id = commit_id;
3740 memset(&s->blame, 0, sizeof(s->blame));
3742 SIMPLEQ_INIT(&s->colors);
3743 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3744 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3745 get_color_value("TOG_COLOR_COMMIT"));
3746 if (err)
3747 return err;
3750 view->show = show_blame_view;
3751 view->input = input_blame_view;
3752 view->close = close_blame_view;
3753 view->search_start = search_start_blame_view;
3754 view->search_next = search_next_blame_view;
3756 return run_blame(&s->blame, view, &s->blame_complete,
3757 &s->first_displayed_line, &s->last_displayed_line,
3758 &s->selected_line, &s->done, &s->eof, s->path,
3759 s->blamed_commit->id, s->repo);
3762 static const struct got_error *
3763 close_blame_view(struct tog_view *view)
3765 const struct got_error *err = NULL;
3766 struct tog_blame_view_state *s = &view->state.blame;
3768 if (s->blame.thread)
3769 err = stop_blame(&s->blame);
3771 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3772 struct got_object_qid *blamed_commit;
3773 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3774 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3775 got_object_qid_free(blamed_commit);
3778 free(s->path);
3779 free_colors(&s->colors);
3781 return err;
3784 static const struct got_error *
3785 search_start_blame_view(struct tog_view *view)
3787 struct tog_blame_view_state *s = &view->state.blame;
3789 s->matched_line = 0;
3790 return NULL;
3793 static const struct got_error *
3794 search_next_blame_view(struct tog_view *view)
3796 struct tog_blame_view_state *s = &view->state.blame;
3797 int lineno;
3799 if (!view->searching) {
3800 view->search_next_done = 1;
3801 return NULL;
3804 if (s->matched_line) {
3805 if (view->searching == TOG_SEARCH_FORWARD)
3806 lineno = s->matched_line + 1;
3807 else
3808 lineno = s->matched_line - 1;
3809 } else {
3810 if (view->searching == TOG_SEARCH_FORWARD)
3811 lineno = 1;
3812 else
3813 lineno = s->blame.nlines;
3816 while (1) {
3817 char *line = NULL;
3818 off_t offset;
3819 size_t len;
3821 if (lineno <= 0 || lineno > s->blame.nlines) {
3822 if (s->matched_line == 0) {
3823 view->search_next_done = 1;
3824 free(line);
3825 break;
3828 if (view->searching == TOG_SEARCH_FORWARD)
3829 lineno = 1;
3830 else
3831 lineno = s->blame.nlines;
3834 offset = s->blame.line_offsets[lineno - 1];
3835 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3836 free(line);
3837 return got_error_from_errno("fseeko");
3839 free(line);
3840 line = parse_next_line(s->blame.f, &len);
3841 if (line && match_line(line, &view->regex)) {
3842 view->search_next_done = 1;
3843 s->matched_line = lineno;
3844 free(line);
3845 break;
3847 free(line);
3848 if (view->searching == TOG_SEARCH_FORWARD)
3849 lineno++;
3850 else
3851 lineno--;
3854 if (s->matched_line) {
3855 s->first_displayed_line = s->matched_line;
3856 s->selected_line = 1;
3859 return NULL;
3862 static const struct got_error *
3863 show_blame_view(struct tog_view *view)
3865 const struct got_error *err = NULL;
3866 struct tog_blame_view_state *s = &view->state.blame;
3867 int errcode;
3869 if (s->blame.thread == NULL) {
3870 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3871 &s->blame.thread_args);
3872 if (errcode)
3873 return got_error_set_errno(errcode, "pthread_create");
3875 halfdelay(1); /* fast refresh while annotating */
3878 if (s->blame_complete)
3879 halfdelay(10); /* disable fast refresh */
3881 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3882 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3883 s->selected_line, &s->first_displayed_line,
3884 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3886 view_vborder(view);
3887 return err;
3890 static const struct got_error *
3891 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3892 struct tog_view **focus_view, struct tog_view *view, int ch)
3894 const struct got_error *err = NULL, *thread_err = NULL;
3895 struct tog_view *diff_view;
3896 struct tog_blame_view_state *s = &view->state.blame;
3897 int begin_x = 0;
3899 switch (ch) {
3900 case 'q':
3901 s->done = 1;
3902 break;
3903 case 'k':
3904 case KEY_UP:
3905 if (s->selected_line > 1)
3906 s->selected_line--;
3907 else if (s->selected_line == 1 &&
3908 s->first_displayed_line > 1)
3909 s->first_displayed_line--;
3910 break;
3911 case KEY_PPAGE:
3912 if (s->first_displayed_line == 1) {
3913 s->selected_line = 1;
3914 break;
3916 if (s->first_displayed_line > view->nlines - 2)
3917 s->first_displayed_line -=
3918 (view->nlines - 2);
3919 else
3920 s->first_displayed_line = 1;
3921 break;
3922 case 'j':
3923 case KEY_DOWN:
3924 if (s->selected_line < view->nlines - 2 &&
3925 s->first_displayed_line +
3926 s->selected_line <= s->blame.nlines)
3927 s->selected_line++;
3928 else if (s->last_displayed_line <
3929 s->blame.nlines)
3930 s->first_displayed_line++;
3931 break;
3932 case 'b':
3933 case 'p': {
3934 struct got_object_id *id = NULL;
3935 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3936 s->first_displayed_line, s->selected_line);
3937 if (id == NULL)
3938 break;
3939 if (ch == 'p') {
3940 struct got_commit_object *commit;
3941 struct got_object_qid *pid;
3942 struct got_object_id *blob_id = NULL;
3943 int obj_type;
3944 err = got_object_open_as_commit(&commit,
3945 s->repo, id);
3946 if (err)
3947 break;
3948 pid = SIMPLEQ_FIRST(
3949 got_object_commit_get_parent_ids(commit));
3950 if (pid == NULL) {
3951 got_object_commit_close(commit);
3952 break;
3954 /* Check if path history ends here. */
3955 err = got_object_id_by_path(&blob_id, s->repo,
3956 pid->id, s->path);
3957 if (err) {
3958 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3959 err = NULL;
3960 got_object_commit_close(commit);
3961 break;
3963 err = got_object_get_type(&obj_type, s->repo,
3964 blob_id);
3965 free(blob_id);
3966 /* Can't blame non-blob type objects. */
3967 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3968 got_object_commit_close(commit);
3969 break;
3971 err = got_object_qid_alloc(&s->blamed_commit,
3972 pid->id);
3973 got_object_commit_close(commit);
3974 } else {
3975 if (got_object_id_cmp(id,
3976 s->blamed_commit->id) == 0)
3977 break;
3978 err = got_object_qid_alloc(&s->blamed_commit,
3979 id);
3981 if (err)
3982 break;
3983 s->done = 1;
3984 thread_err = stop_blame(&s->blame);
3985 s->done = 0;
3986 if (thread_err)
3987 break;
3988 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3989 s->blamed_commit, entry);
3990 err = run_blame(&s->blame, view, &s->blame_complete,
3991 &s->first_displayed_line, &s->last_displayed_line,
3992 &s->selected_line, &s->done, &s->eof,
3993 s->path, s->blamed_commit->id, s->repo);
3994 if (err)
3995 break;
3996 break;
3998 case 'B': {
3999 struct got_object_qid *first;
4000 first = SIMPLEQ_FIRST(&s->blamed_commits);
4001 if (!got_object_id_cmp(first->id, s->commit_id))
4002 break;
4003 s->done = 1;
4004 thread_err = stop_blame(&s->blame);
4005 s->done = 0;
4006 if (thread_err)
4007 break;
4008 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4009 got_object_qid_free(s->blamed_commit);
4010 s->blamed_commit =
4011 SIMPLEQ_FIRST(&s->blamed_commits);
4012 err = run_blame(&s->blame, view, &s->blame_complete,
4013 &s->first_displayed_line, &s->last_displayed_line,
4014 &s->selected_line, &s->done, &s->eof, s->path,
4015 s->blamed_commit->id, s->repo);
4016 if (err)
4017 break;
4018 break;
4020 case KEY_ENTER:
4021 case '\r': {
4022 struct got_object_id *id = NULL;
4023 struct got_object_qid *pid;
4024 struct got_commit_object *commit = NULL;
4025 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4026 s->first_displayed_line, s->selected_line);
4027 if (id == NULL)
4028 break;
4029 err = got_object_open_as_commit(&commit, s->repo, id);
4030 if (err)
4031 break;
4032 pid = SIMPLEQ_FIRST(
4033 got_object_commit_get_parent_ids(commit));
4034 if (view_is_parent_view(view))
4035 begin_x = view_split_begin_x(view->begin_x);
4036 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4037 if (diff_view == NULL) {
4038 got_object_commit_close(commit);
4039 err = got_error_from_errno("view_open");
4040 break;
4042 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4043 id, NULL, s->refs, s->repo);
4044 got_object_commit_close(commit);
4045 if (err) {
4046 view_close(diff_view);
4047 break;
4049 if (view_is_parent_view(view)) {
4050 err = view_close_child(view);
4051 if (err)
4052 break;
4053 err = view_set_child(view, diff_view);
4054 if (err) {
4055 view_close(diff_view);
4056 break;
4058 *focus_view = diff_view;
4059 view->child_focussed = 1;
4060 } else
4061 *new_view = diff_view;
4062 if (err)
4063 break;
4064 break;
4066 case KEY_NPAGE:
4067 case ' ':
4068 if (s->last_displayed_line >= s->blame.nlines &&
4069 s->selected_line >= MIN(s->blame.nlines,
4070 view->nlines - 2)) {
4071 break;
4073 if (s->last_displayed_line >= s->blame.nlines &&
4074 s->selected_line < view->nlines - 2) {
4075 s->selected_line = MIN(s->blame.nlines,
4076 view->nlines - 2);
4077 break;
4079 if (s->last_displayed_line + view->nlines - 2
4080 <= s->blame.nlines)
4081 s->first_displayed_line +=
4082 view->nlines - 2;
4083 else
4084 s->first_displayed_line =
4085 s->blame.nlines -
4086 (view->nlines - 3);
4087 break;
4088 case KEY_RESIZE:
4089 if (s->selected_line > view->nlines - 2) {
4090 s->selected_line = MIN(s->blame.nlines,
4091 view->nlines - 2);
4093 break;
4094 default:
4095 break;
4097 return thread_err ? thread_err : err;
4100 static const struct got_error *
4101 cmd_blame(int argc, char *argv[])
4103 const struct got_error *error;
4104 struct got_repository *repo = NULL;
4105 struct got_reflist_head refs;
4106 struct got_worktree *worktree = NULL;
4107 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4108 struct got_object_id *commit_id = NULL;
4109 char *commit_id_str = NULL;
4110 int ch;
4111 struct tog_view *view;
4113 SIMPLEQ_INIT(&refs);
4115 #ifndef PROFILE
4116 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4117 NULL) == -1)
4118 err(1, "pledge");
4119 #endif
4121 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4122 switch (ch) {
4123 case 'c':
4124 commit_id_str = optarg;
4125 break;
4126 case 'r':
4127 repo_path = realpath(optarg, NULL);
4128 if (repo_path == NULL)
4129 return got_error_from_errno2("realpath",
4130 optarg);
4131 break;
4132 default:
4133 usage_blame();
4134 /* NOTREACHED */
4138 argc -= optind;
4139 argv += optind;
4141 if (argc == 1)
4142 path = argv[0];
4143 else
4144 usage_blame();
4146 cwd = getcwd(NULL, 0);
4147 if (cwd == NULL) {
4148 error = got_error_from_errno("getcwd");
4149 goto done;
4151 if (repo_path == NULL) {
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4154 goto done;
4155 else
4156 error = NULL;
4157 if (worktree) {
4158 repo_path =
4159 strdup(got_worktree_get_repo_path(worktree));
4160 if (repo_path == NULL)
4161 error = got_error_from_errno("strdup");
4162 if (error)
4163 goto done;
4164 } else {
4165 repo_path = strdup(cwd);
4166 if (repo_path == NULL) {
4167 error = got_error_from_errno("strdup");
4168 goto done;
4173 init_curses();
4175 error = got_repo_open(&repo, repo_path, NULL);
4176 if (error != NULL)
4177 goto done;
4179 error = apply_unveil(got_repo_get_path(repo), NULL);
4180 if (error)
4181 goto done;
4183 if (worktree) {
4184 const char *prefix = got_worktree_get_path_prefix(worktree);
4185 char *p, *worktree_subdir = cwd +
4186 strlen(got_worktree_get_root_path(worktree));
4187 if (asprintf(&p, "%s%s%s%s%s",
4188 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4189 worktree_subdir, worktree_subdir[0] ? "/" : "",
4190 path) == -1) {
4191 error = got_error_from_errno("asprintf");
4192 goto done;
4194 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4195 free(p);
4196 } else {
4197 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4199 if (error)
4200 goto done;
4202 if (commit_id_str == NULL) {
4203 struct got_reference *head_ref;
4204 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4205 if (error != NULL)
4206 goto done;
4207 error = got_ref_resolve(&commit_id, repo, head_ref);
4208 got_ref_close(head_ref);
4209 } else {
4210 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4211 if (error) {
4212 if (error->code != GOT_ERR_NOT_REF)
4213 goto done;
4214 error = got_repo_match_object_id_prefix(&commit_id,
4215 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4218 if (error != NULL)
4219 goto done;
4221 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4222 if (error)
4223 goto done;
4225 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4226 if (view == NULL) {
4227 error = got_error_from_errno("view_open");
4228 goto done;
4230 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4231 if (error)
4232 goto done;
4233 if (worktree) {
4234 /* Release work tree lock. */
4235 got_worktree_close(worktree);
4236 worktree = NULL;
4238 error = view_loop(view);
4239 done:
4240 free(repo_path);
4241 free(cwd);
4242 free(commit_id);
4243 if (worktree)
4244 got_worktree_close(worktree);
4245 if (repo)
4246 got_repo_close(repo);
4247 got_ref_list_free(&refs);
4248 return error;
4251 static const struct got_error *
4252 draw_tree_entries(struct tog_view *view,
4253 struct got_tree_entry **first_displayed_entry,
4254 struct got_tree_entry **last_displayed_entry,
4255 struct got_tree_entry **selected_entry, int *ndisplayed,
4256 const char *label, int show_ids, const char *parent_path,
4257 struct got_tree_object *tree, int selected, int limit,
4258 int isroot, struct tog_colors *colors)
4260 const struct got_error *err = NULL;
4261 struct got_tree_entry *te;
4262 wchar_t *wline;
4263 struct tog_color *tc;
4264 int width, n, i, nentries;
4266 *ndisplayed = 0;
4268 werase(view->window);
4270 if (limit == 0)
4271 return NULL;
4273 err = format_line(&wline, &width, label, view->ncols, 0);
4274 if (err)
4275 return err;
4276 if (view_needs_focus_indication(view))
4277 wstandout(view->window);
4278 tc = get_color(colors, TOG_COLOR_COMMIT);
4279 if (tc)
4280 wattr_on(view->window,
4281 COLOR_PAIR(tc->colorpair), NULL);
4282 waddwstr(view->window, wline);
4283 if (tc)
4284 wattr_off(view->window,
4285 COLOR_PAIR(tc->colorpair), NULL);
4286 if (view_needs_focus_indication(view))
4287 wstandend(view->window);
4288 free(wline);
4289 wline = NULL;
4290 if (width < view->ncols - 1)
4291 waddch(view->window, '\n');
4292 if (--limit <= 0)
4293 return NULL;
4294 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4295 if (err)
4296 return err;
4297 waddwstr(view->window, wline);
4298 free(wline);
4299 wline = NULL;
4300 if (width < view->ncols - 1)
4301 waddch(view->window, '\n');
4302 if (--limit <= 0)
4303 return NULL;
4304 waddch(view->window, '\n');
4305 if (--limit <= 0)
4306 return NULL;
4308 if (*first_displayed_entry == NULL) {
4309 te = got_object_tree_get_first_entry(tree);
4310 if (selected == 0) {
4311 if (view->focussed)
4312 wstandout(view->window);
4313 *selected_entry = NULL;
4315 waddstr(view->window, " ..\n"); /* parent directory */
4316 if (selected == 0 && view->focussed)
4317 wstandend(view->window);
4318 (*ndisplayed)++;
4319 if (--limit <= 0)
4320 return NULL;
4321 n = 1;
4322 } else {
4323 n = 0;
4324 te = *first_displayed_entry;
4327 nentries = got_object_tree_get_nentries(tree);
4328 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4329 char *line = NULL, *id_str = NULL;
4330 const char *modestr = "";
4331 mode_t mode;
4333 te = got_object_tree_get_entry(tree, i);
4334 mode = got_tree_entry_get_mode(te);
4336 if (show_ids) {
4337 err = got_object_id_str(&id_str,
4338 got_tree_entry_get_id(te));
4339 if (err)
4340 return got_error_from_errno(
4341 "got_object_id_str");
4343 if (got_object_tree_entry_is_submodule(te))
4344 modestr = "$";
4345 else if (S_ISLNK(mode))
4346 modestr = "@";
4347 else if (S_ISDIR(mode))
4348 modestr = "/";
4349 else if (mode & S_IXUSR)
4350 modestr = "*";
4351 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4352 got_tree_entry_get_name(te), modestr) == -1) {
4353 free(id_str);
4354 return got_error_from_errno("asprintf");
4356 free(id_str);
4357 err = format_line(&wline, &width, line, view->ncols, 0);
4358 if (err) {
4359 free(line);
4360 break;
4362 if (n == selected) {
4363 if (view->focussed)
4364 wstandout(view->window);
4365 *selected_entry = te;
4367 tc = match_color(colors, line);
4368 if (tc)
4369 wattr_on(view->window,
4370 COLOR_PAIR(tc->colorpair), NULL);
4371 waddwstr(view->window, wline);
4372 if (tc)
4373 wattr_off(view->window,
4374 COLOR_PAIR(tc->colorpair), NULL);
4375 if (width < view->ncols - 1)
4376 waddch(view->window, '\n');
4377 if (n == selected && view->focussed)
4378 wstandend(view->window);
4379 free(line);
4380 free(wline);
4381 wline = NULL;
4382 n++;
4383 (*ndisplayed)++;
4384 *last_displayed_entry = te;
4385 if (--limit <= 0)
4386 break;
4389 return err;
4392 static void
4393 tree_scroll_up(struct tog_view *view,
4394 struct got_tree_entry **first_displayed_entry, int maxscroll,
4395 struct got_tree_object *tree, int isroot)
4397 struct got_tree_entry *te;
4398 int i;
4400 if (*first_displayed_entry == NULL)
4401 return;
4403 te = got_object_tree_get_entry(tree, 0);
4404 if (*first_displayed_entry == te) {
4405 if (!isroot)
4406 *first_displayed_entry = NULL;
4407 return;
4410 i = 0;
4411 while (*first_displayed_entry && i < maxscroll) {
4412 *first_displayed_entry = got_tree_entry_get_prev(tree,
4413 *first_displayed_entry);
4414 i++;
4416 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4417 *first_displayed_entry = NULL;
4420 static int
4421 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4422 struct got_tree_entry *last_displayed_entry,
4423 struct got_tree_object *tree)
4425 struct got_tree_entry *next, *last;
4426 int n = 0;
4428 if (*first_displayed_entry)
4429 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4430 else
4431 next = got_object_tree_get_first_entry(tree);
4433 last = last_displayed_entry;
4434 while (next && last && n++ < maxscroll) {
4435 last = got_tree_entry_get_next(tree, last);
4436 if (last) {
4437 *first_displayed_entry = next;
4438 next = got_tree_entry_get_next(tree, next);
4441 return n;
4444 static const struct got_error *
4445 tree_entry_path(char **path, struct tog_parent_trees *parents,
4446 struct got_tree_entry *te)
4448 const struct got_error *err = NULL;
4449 struct tog_parent_tree *pt;
4450 size_t len = 2; /* for leading slash and NUL */
4452 TAILQ_FOREACH(pt, parents, entry)
4453 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4454 + 1 /* slash */;
4455 if (te)
4456 len += strlen(got_tree_entry_get_name(te));
4458 *path = calloc(1, len);
4459 if (path == NULL)
4460 return got_error_from_errno("calloc");
4462 (*path)[0] = '/';
4463 pt = TAILQ_LAST(parents, tog_parent_trees);
4464 while (pt) {
4465 const char *name = got_tree_entry_get_name(pt->selected_entry);
4466 if (strlcat(*path, name, len) >= len) {
4467 err = got_error(GOT_ERR_NO_SPACE);
4468 goto done;
4470 if (strlcat(*path, "/", len) >= len) {
4471 err = got_error(GOT_ERR_NO_SPACE);
4472 goto done;
4474 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4476 if (te) {
4477 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4478 err = got_error(GOT_ERR_NO_SPACE);
4479 goto done;
4482 done:
4483 if (err) {
4484 free(*path);
4485 *path = NULL;
4487 return err;
4490 static const struct got_error *
4491 blame_tree_entry(struct tog_view **new_view, int begin_x,
4492 struct got_tree_entry *te, struct tog_parent_trees *parents,
4493 struct got_object_id *commit_id, struct got_reflist_head *refs,
4494 struct got_repository *repo)
4496 const struct got_error *err = NULL;
4497 char *path;
4498 struct tog_view *blame_view;
4500 *new_view = NULL;
4502 err = tree_entry_path(&path, parents, te);
4503 if (err)
4504 return err;
4506 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4507 if (blame_view == NULL) {
4508 err = got_error_from_errno("view_open");
4509 goto done;
4512 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4513 if (err) {
4514 if (err->code == GOT_ERR_CANCELLED)
4515 err = NULL;
4516 view_close(blame_view);
4517 } else
4518 *new_view = blame_view;
4519 done:
4520 free(path);
4521 return err;
4524 static const struct got_error *
4525 log_tree_entry(struct tog_view **new_view, int begin_x,
4526 struct got_tree_entry *te, struct tog_parent_trees *parents,
4527 struct got_object_id *commit_id, struct got_reflist_head *refs,
4528 struct got_repository *repo)
4530 struct tog_view *log_view;
4531 const struct got_error *err = NULL;
4532 char *path;
4534 *new_view = NULL;
4536 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4537 if (log_view == NULL)
4538 return got_error_from_errno("view_open");
4540 err = tree_entry_path(&path, parents, te);
4541 if (err)
4542 return err;
4544 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4545 if (err)
4546 view_close(log_view);
4547 else
4548 *new_view = log_view;
4549 free(path);
4550 return err;
4553 static const struct got_error *
4554 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4555 struct got_object_id *commit_id, struct got_reflist_head *refs,
4556 struct got_repository *repo)
4558 const struct got_error *err = NULL;
4559 char *commit_id_str = NULL;
4560 struct tog_tree_view_state *s = &view->state.tree;
4562 TAILQ_INIT(&s->parents);
4564 err = got_object_id_str(&commit_id_str, commit_id);
4565 if (err != NULL)
4566 goto done;
4568 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4569 err = got_error_from_errno("asprintf");
4570 goto done;
4573 s->root = s->tree = root;
4574 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4575 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4576 s->commit_id = got_object_id_dup(commit_id);
4577 if (s->commit_id == NULL) {
4578 err = got_error_from_errno("got_object_id_dup");
4579 goto done;
4581 s->refs = refs;
4582 s->repo = repo;
4584 SIMPLEQ_INIT(&s->colors);
4586 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4587 err = add_color(&s->colors, "\\$$",
4588 TOG_COLOR_TREE_SUBMODULE,
4589 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4590 if (err)
4591 goto done;
4592 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4593 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4594 if (err) {
4595 free_colors(&s->colors);
4596 goto done;
4598 err = add_color(&s->colors, "/$",
4599 TOG_COLOR_TREE_DIRECTORY,
4600 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4601 if (err) {
4602 free_colors(&s->colors);
4603 goto done;
4606 err = add_color(&s->colors, "\\*$",
4607 TOG_COLOR_TREE_EXECUTABLE,
4608 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4609 if (err) {
4610 free_colors(&s->colors);
4611 goto done;
4614 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4615 get_color_value("TOG_COLOR_COMMIT"));
4616 if (err) {
4617 free_colors(&s->colors);
4618 goto done;
4622 view->show = show_tree_view;
4623 view->input = input_tree_view;
4624 view->close = close_tree_view;
4625 view->search_start = search_start_tree_view;
4626 view->search_next = search_next_tree_view;
4627 done:
4628 free(commit_id_str);
4629 if (err) {
4630 free(s->tree_label);
4631 s->tree_label = NULL;
4633 return err;
4636 static const struct got_error *
4637 close_tree_view(struct tog_view *view)
4639 struct tog_tree_view_state *s = &view->state.tree;
4641 free_colors(&s->colors);
4642 free(s->tree_label);
4643 s->tree_label = NULL;
4644 free(s->commit_id);
4645 s->commit_id = NULL;
4646 while (!TAILQ_EMPTY(&s->parents)) {
4647 struct tog_parent_tree *parent;
4648 parent = TAILQ_FIRST(&s->parents);
4649 TAILQ_REMOVE(&s->parents, parent, entry);
4650 free(parent);
4653 if (s->tree != s->root)
4654 got_object_tree_close(s->tree);
4655 got_object_tree_close(s->root);
4657 return NULL;
4660 static const struct got_error *
4661 search_start_tree_view(struct tog_view *view)
4663 struct tog_tree_view_state *s = &view->state.tree;
4665 s->matched_entry = NULL;
4666 return NULL;
4669 static int
4670 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4672 regmatch_t regmatch;
4674 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4675 0) == 0;
4678 static const struct got_error *
4679 search_next_tree_view(struct tog_view *view)
4681 struct tog_tree_view_state *s = &view->state.tree;
4682 struct got_tree_entry *te = NULL;
4684 if (!view->searching) {
4685 view->search_next_done = 1;
4686 return NULL;
4689 if (s->matched_entry) {
4690 if (view->searching == TOG_SEARCH_FORWARD) {
4691 if (s->selected_entry)
4692 te = got_tree_entry_get_next(s->tree,
4693 s->selected_entry);
4694 else
4695 te = got_object_tree_get_first_entry(s->tree);
4696 } else {
4697 if (s->selected_entry == NULL)
4698 te = got_object_tree_get_last_entry(s->tree);
4699 else
4700 te = got_tree_entry_get_prev(s->tree,
4701 s->selected_entry);
4703 } else {
4704 if (view->searching == TOG_SEARCH_FORWARD)
4705 te = got_object_tree_get_first_entry(s->tree);
4706 else
4707 te = got_object_tree_get_last_entry(s->tree);
4710 while (1) {
4711 if (te == NULL) {
4712 if (s->matched_entry == NULL) {
4713 view->search_next_done = 1;
4714 return NULL;
4716 if (view->searching == TOG_SEARCH_FORWARD)
4717 te = got_object_tree_get_first_entry(s->tree);
4718 else
4719 te = got_object_tree_get_last_entry(s->tree);
4722 if (match_tree_entry(te, &view->regex)) {
4723 view->search_next_done = 1;
4724 s->matched_entry = te;
4725 break;
4728 if (view->searching == TOG_SEARCH_FORWARD)
4729 te = got_tree_entry_get_next(s->tree, te);
4730 else
4731 te = got_tree_entry_get_prev(s->tree, te);
4734 if (s->matched_entry) {
4735 s->first_displayed_entry = s->matched_entry;
4736 s->selected = 0;
4739 return NULL;
4742 static const struct got_error *
4743 show_tree_view(struct tog_view *view)
4745 const struct got_error *err = NULL;
4746 struct tog_tree_view_state *s = &view->state.tree;
4747 char *parent_path;
4749 err = tree_entry_path(&parent_path, &s->parents, NULL);
4750 if (err)
4751 return err;
4753 err = draw_tree_entries(view, &s->first_displayed_entry,
4754 &s->last_displayed_entry, &s->selected_entry,
4755 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4756 s->tree, s->selected, view->nlines, s->tree == s->root,
4757 &s->colors);
4758 free(parent_path);
4760 view_vborder(view);
4761 return err;
4764 static const struct got_error *
4765 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4766 struct tog_view **focus_view, struct tog_view *view, int ch)
4768 const struct got_error *err = NULL;
4769 struct tog_tree_view_state *s = &view->state.tree;
4770 struct tog_view *log_view;
4771 int begin_x = 0, nscrolled;
4773 switch (ch) {
4774 case 'i':
4775 s->show_ids = !s->show_ids;
4776 break;
4777 case 'l':
4778 if (!s->selected_entry)
4779 break;
4780 if (view_is_parent_view(view))
4781 begin_x = view_split_begin_x(view->begin_x);
4782 err = log_tree_entry(&log_view, begin_x,
4783 s->selected_entry, &s->parents,
4784 s->commit_id, s->refs, s->repo);
4785 if (view_is_parent_view(view)) {
4786 err = view_close_child(view);
4787 if (err)
4788 return err;
4789 err = view_set_child(view, log_view);
4790 if (err) {
4791 view_close(log_view);
4792 break;
4794 *focus_view = log_view;
4795 view->child_focussed = 1;
4796 } else
4797 *new_view = log_view;
4798 break;
4799 case 'k':
4800 case KEY_UP:
4801 if (s->selected > 0) {
4802 s->selected--;
4803 if (s->selected == 0)
4804 break;
4806 if (s->selected > 0)
4807 break;
4808 tree_scroll_up(view, &s->first_displayed_entry, 1,
4809 s->tree, s->tree == s->root);
4810 break;
4811 case KEY_PPAGE:
4812 tree_scroll_up(view, &s->first_displayed_entry,
4813 MAX(0, view->nlines - 4 - s->selected), s->tree,
4814 s->tree == s->root);
4815 s->selected = 0;
4816 if (got_object_tree_get_first_entry(s->tree) ==
4817 s->first_displayed_entry && s->tree != s->root)
4818 s->first_displayed_entry = NULL;
4819 break;
4820 case 'j':
4821 case KEY_DOWN:
4822 if (s->selected < s->ndisplayed - 1) {
4823 s->selected++;
4824 break;
4826 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4827 == NULL)
4828 /* can't scroll any further */
4829 break;
4830 tree_scroll_down(&s->first_displayed_entry, 1,
4831 s->last_displayed_entry, s->tree);
4832 break;
4833 case KEY_NPAGE:
4834 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4835 == NULL) {
4836 /* can't scroll any further; move cursor down */
4837 if (s->selected < s->ndisplayed - 1)
4838 s->selected = s->ndisplayed - 1;
4839 break;
4841 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4842 view->nlines, s->last_displayed_entry, s->tree);
4843 if (nscrolled < view->nlines) {
4844 int ndisplayed = 0;
4845 struct got_tree_entry *te;
4846 te = s->first_displayed_entry;
4847 do {
4848 ndisplayed++;
4849 te = got_tree_entry_get_next(s->tree, te);
4850 } while (te);
4851 s->selected = ndisplayed - 1;
4853 break;
4854 case KEY_ENTER:
4855 case '\r':
4856 case KEY_BACKSPACE:
4857 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4858 struct tog_parent_tree *parent;
4859 /* user selected '..' */
4860 if (s->tree == s->root)
4861 break;
4862 parent = TAILQ_FIRST(&s->parents);
4863 TAILQ_REMOVE(&s->parents, parent,
4864 entry);
4865 got_object_tree_close(s->tree);
4866 s->tree = parent->tree;
4867 s->first_displayed_entry =
4868 parent->first_displayed_entry;
4869 s->selected_entry =
4870 parent->selected_entry;
4871 s->selected = parent->selected;
4872 free(parent);
4873 } else if (S_ISDIR(got_tree_entry_get_mode(
4874 s->selected_entry))) {
4875 struct got_tree_object *subtree;
4876 err = got_object_open_as_tree(&subtree, s->repo,
4877 got_tree_entry_get_id(s->selected_entry));
4878 if (err)
4879 break;
4880 err = tree_view_visit_subtree(subtree, s);
4881 if (err) {
4882 got_object_tree_close(subtree);
4883 break;
4885 } else if (S_ISREG(got_tree_entry_get_mode(
4886 s->selected_entry))) {
4887 struct tog_view *blame_view;
4888 int begin_x = view_is_parent_view(view) ?
4889 view_split_begin_x(view->begin_x) : 0;
4891 err = blame_tree_entry(&blame_view, begin_x,
4892 s->selected_entry, &s->parents,
4893 s->commit_id, s->refs, s->repo);
4894 if (err)
4895 break;
4896 if (view_is_parent_view(view)) {
4897 err = view_close_child(view);
4898 if (err)
4899 return err;
4900 err = view_set_child(view, blame_view);
4901 if (err) {
4902 view_close(blame_view);
4903 break;
4905 *focus_view = blame_view;
4906 view->child_focussed = 1;
4907 } else
4908 *new_view = blame_view;
4910 break;
4911 case KEY_RESIZE:
4912 if (s->selected > view->nlines)
4913 s->selected = s->ndisplayed - 1;
4914 break;
4915 default:
4916 break;
4919 return err;
4922 __dead static void
4923 usage_tree(void)
4925 endwin();
4926 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4927 getprogname());
4928 exit(1);
4931 static const struct got_error *
4932 cmd_tree(int argc, char *argv[])
4934 const struct got_error *error;
4935 struct got_repository *repo = NULL;
4936 struct got_reflist_head refs;
4937 char *repo_path = NULL;
4938 struct got_object_id *commit_id = NULL;
4939 char *commit_id_arg = NULL;
4940 struct got_commit_object *commit = NULL;
4941 struct got_tree_object *tree = NULL;
4942 int ch;
4943 struct tog_view *view;
4945 SIMPLEQ_INIT(&refs);
4947 #ifndef PROFILE
4948 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4949 NULL) == -1)
4950 err(1, "pledge");
4951 #endif
4953 while ((ch = getopt(argc, argv, "c:")) != -1) {
4954 switch (ch) {
4955 case 'c':
4956 commit_id_arg = optarg;
4957 break;
4958 default:
4959 usage_tree();
4960 /* NOTREACHED */
4964 argc -= optind;
4965 argv += optind;
4967 if (argc == 0) {
4968 struct got_worktree *worktree;
4969 char *cwd = getcwd(NULL, 0);
4970 if (cwd == NULL)
4971 return got_error_from_errno("getcwd");
4972 error = got_worktree_open(&worktree, cwd);
4973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4974 goto done;
4975 if (worktree) {
4976 free(cwd);
4977 repo_path =
4978 strdup(got_worktree_get_repo_path(worktree));
4979 got_worktree_close(worktree);
4980 } else
4981 repo_path = cwd;
4982 if (repo_path == NULL) {
4983 error = got_error_from_errno("strdup");
4984 goto done;
4986 } else if (argc == 1) {
4987 repo_path = realpath(argv[0], NULL);
4988 if (repo_path == NULL)
4989 return got_error_from_errno2("realpath", argv[0]);
4990 } else
4991 usage_log();
4993 init_curses();
4995 error = got_repo_open(&repo, repo_path, NULL);
4996 if (error != NULL)
4997 goto done;
4999 error = apply_unveil(got_repo_get_path(repo), NULL);
5000 if (error)
5001 goto done;
5003 if (commit_id_arg == NULL)
5004 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5005 else {
5006 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5007 if (error) {
5008 if (error->code != GOT_ERR_NOT_REF)
5009 goto done;
5010 error = got_repo_match_object_id_prefix(&commit_id,
5011 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5014 if (error != NULL)
5015 goto done;
5017 error = got_object_open_as_commit(&commit, repo, commit_id);
5018 if (error != NULL)
5019 goto done;
5021 error = got_object_open_as_tree(&tree, repo,
5022 got_object_commit_get_tree_id(commit));
5023 if (error != NULL)
5024 goto done;
5026 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5027 if (error)
5028 goto done;
5030 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5031 if (view == NULL) {
5032 error = got_error_from_errno("view_open");
5033 goto done;
5035 error = open_tree_view(view, tree, commit_id, &refs, repo);
5036 if (error)
5037 goto done;
5038 error = view_loop(view);
5039 done:
5040 free(repo_path);
5041 free(commit_id);
5042 if (commit)
5043 got_object_commit_close(commit);
5044 if (tree)
5045 got_object_tree_close(tree);
5046 if (repo)
5047 got_repo_close(repo);
5048 got_ref_list_free(&refs);
5049 return error;
5052 static void
5053 list_commands(void)
5055 int i;
5057 fprintf(stderr, "commands:");
5058 for (i = 0; i < nitems(tog_commands); i++) {
5059 struct tog_cmd *cmd = &tog_commands[i];
5060 fprintf(stderr, " %s", cmd->name);
5062 fputc('\n', stderr);
5065 __dead static void
5066 usage(int hflag)
5068 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5069 getprogname());
5070 if (hflag)
5071 list_commands();
5072 exit(1);
5075 static char **
5076 make_argv(const char *arg0, const char *arg1)
5078 char **argv;
5079 int argc = (arg1 == NULL ? 1 : 2);
5081 argv = calloc(argc, sizeof(char *));
5082 if (argv == NULL)
5083 err(1, "calloc");
5084 argv[0] = strdup(arg0);
5085 if (argv[0] == NULL)
5086 err(1, "strdup");
5087 if (arg1) {
5088 argv[1] = strdup(arg1);
5089 if (argv[1] == NULL)
5090 err(1, "strdup");
5093 return argv;
5096 int
5097 main(int argc, char *argv[])
5099 const struct got_error *error = NULL;
5100 struct tog_cmd *cmd = NULL;
5101 int ch, hflag = 0, Vflag = 0;
5102 char **cmd_argv = NULL;
5104 setlocale(LC_CTYPE, "");
5106 while ((ch = getopt(argc, argv, "hV")) != -1) {
5107 switch (ch) {
5108 case 'h':
5109 hflag = 1;
5110 break;
5111 case 'V':
5112 Vflag = 1;
5113 break;
5114 default:
5115 usage(hflag);
5116 /* NOTREACHED */
5120 argc -= optind;
5121 argv += optind;
5122 optind = 0;
5123 optreset = 1;
5125 if (Vflag) {
5126 got_version_print_str();
5127 return 1;
5130 if (argc == 0) {
5131 if (hflag)
5132 usage(hflag);
5133 /* Build an argument vector which runs a default command. */
5134 cmd = &tog_commands[0];
5135 cmd_argv = make_argv(cmd->name, NULL);
5136 argc = 1;
5137 } else {
5138 int i;
5140 /* Did the user specific a command? */
5141 for (i = 0; i < nitems(tog_commands); i++) {
5142 if (strncmp(tog_commands[i].name, argv[0],
5143 strlen(argv[0])) == 0) {
5144 cmd = &tog_commands[i];
5145 break;
5149 if (cmd == NULL) {
5150 fprintf(stderr, "%s: unknown command '%s'\n",
5151 getprogname(), argv[0]);
5152 list_commands();
5153 return 1;
5157 if (hflag)
5158 cmd->cmd_usage();
5159 else
5160 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5162 endwin();
5163 free(cmd_argv);
5164 if (error && error->code != GOT_ERR_CANCELLED)
5165 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5166 return 0;