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 /* Walk the path and open corresponding tree objects. */
1797 p = path;
1798 while (p[0] == '/')
1799 p++;
1800 while (*p) {
1801 struct got_tree_entry *te;
1802 struct got_object_id *tree_id;
1803 char *te_name;
1805 /* Ensure the correct subtree entry is selected. */
1806 slash = strchr(p, '/');
1807 if (slash == NULL)
1808 slash = strchr(p, '\0');
1810 te_name = strndup(p, slash -p);
1811 if (te_name == NULL) {
1812 err = got_error_from_errno("strndup");
1813 break;
1815 te = got_object_tree_find_entry(s->tree, te_name);
1816 if (te == NULL) {
1817 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1818 free(te_name);
1819 break;
1821 free(te_name);
1822 s->selected_entry = te;
1823 s->selected = got_tree_entry_get_index(te);
1824 if (s->tree != s->root)
1825 s->selected++; /* skip '..' */
1827 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1828 /* Jump to this file's entry. */
1829 s->first_displayed_entry = s->selected_entry;
1830 s->selected = 0;
1831 break;
1834 slash = strchr(p, '/');
1835 if (slash)
1836 subpath = strndup(path, slash - path);
1837 else
1838 subpath = strdup(path);
1839 if (subpath == NULL) {
1840 err = got_error_from_errno("strdup");
1841 break;
1844 err = got_object_id_by_path(&tree_id, repo, entry->id,
1845 subpath);
1846 if (err)
1847 break;
1849 err = got_object_open_as_tree(&tree, repo, tree_id);
1850 free(tree_id);
1851 if (err)
1852 break;
1854 err = tree_view_visit_subtree(tree, s);
1855 if (err) {
1856 got_object_tree_close(tree);
1857 break;
1859 if (slash == NULL)
1860 break;
1861 free(subpath);
1862 subpath = NULL;
1863 p = slash;
1866 free(subpath);
1867 return err;
1870 static void *
1871 log_thread(void *arg)
1873 const struct got_error *err = NULL;
1874 int errcode = 0;
1875 struct tog_log_thread_args *a = arg;
1876 int done = 0;
1878 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1879 NULL, NULL);
1880 if (err)
1881 return (void *)err;
1883 while (!done && !err && !tog_sigpipe_received) {
1884 err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 a->in_repo_path, a->searching, a->search_next_done,
1886 a->regex);
1887 if (err) {
1888 if (err->code != GOT_ERR_ITER_COMPLETED)
1889 return (void *)err;
1890 err = NULL;
1891 done = 1;
1892 } else if (a->commits_needed > 0)
1893 a->commits_needed--;
1895 errcode = pthread_mutex_lock(&tog_mutex);
1896 if (errcode) {
1897 err = got_error_set_errno(errcode,
1898 "pthread_mutex_lock");
1899 break;
1900 } else if (*a->quit)
1901 done = 1;
1902 else if (*a->first_displayed_entry == NULL) {
1903 *a->first_displayed_entry =
1904 TAILQ_FIRST(&a->commits->head);
1905 *a->selected_entry = *a->first_displayed_entry;
1908 if (done)
1909 a->commits_needed = 0;
1910 else if (a->commits_needed == 0) {
1911 errcode = pthread_cond_wait(&a->need_commits,
1912 &tog_mutex);
1913 if (errcode)
1914 err = got_error_set_errno(errcode,
1915 "pthread_cond_wait");
1918 errcode = pthread_mutex_unlock(&tog_mutex);
1919 if (errcode && err == NULL)
1920 err = got_error_set_errno(errcode,
1921 "pthread_mutex_unlock");
1923 a->log_complete = 1;
1924 return (void *)err;
1927 static const struct got_error *
1928 stop_log_thread(struct tog_log_view_state *s)
1930 const struct got_error *err = NULL;
1931 int errcode;
1933 if (s->thread) {
1934 s->quit = 1;
1935 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1936 if (errcode)
1937 return got_error_set_errno(errcode,
1938 "pthread_cond_signal");
1939 errcode = pthread_mutex_unlock(&tog_mutex);
1940 if (errcode)
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_unlock");
1943 errcode = pthread_join(s->thread, (void **)&err);
1944 if (errcode)
1945 return got_error_set_errno(errcode, "pthread_join");
1946 errcode = pthread_mutex_lock(&tog_mutex);
1947 if (errcode)
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_lock");
1950 s->thread = NULL;
1953 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 if (errcode && err == NULL)
1955 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 if (s->thread_args.repo) {
1958 got_repo_close(s->thread_args.repo);
1959 s->thread_args.repo = NULL;
1962 if (s->thread_args.graph) {
1963 got_commit_graph_close(s->thread_args.graph);
1964 s->thread_args.graph = NULL;
1967 return err;
1970 static const struct got_error *
1971 close_log_view(struct tog_view *view)
1973 const struct got_error *err = NULL;
1974 struct tog_log_view_state *s = &view->state.log;
1976 err = stop_log_thread(s);
1977 free_commits(&s->commits);
1978 free(s->in_repo_path);
1979 s->in_repo_path = NULL;
1980 free(s->start_id);
1981 s->start_id = NULL;
1982 return err;
1985 static const struct got_error *
1986 search_start_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 s->matched_entry = NULL;
1991 s->search_entry = NULL;
1992 return NULL;
1995 static const struct got_error *
1996 search_next_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2000 struct commit_queue_entry *entry;
2002 if (!view->searching) {
2003 view->search_next_done = 1;
2004 return NULL;
2007 if (s->search_entry) {
2008 int errcode, ch;
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2010 if (errcode)
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 ch = wgetch(view->window);
2014 errcode = pthread_mutex_lock(&tog_mutex);
2015 if (errcode)
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 if (ch == KEY_BACKSPACE) {
2019 view->search_next_done = 1;
2020 return NULL;
2022 if (view->searching == TOG_SEARCH_FORWARD)
2023 entry = TAILQ_NEXT(s->search_entry, entry);
2024 else
2025 entry = TAILQ_PREV(s->search_entry,
2026 commit_queue_head, entry);
2027 } else if (s->matched_entry) {
2028 if (view->searching == TOG_SEARCH_FORWARD)
2029 entry = TAILQ_NEXT(s->selected_entry, entry);
2030 else
2031 entry = TAILQ_PREV(s->selected_entry,
2032 commit_queue_head, entry);
2033 } else {
2034 if (view->searching == TOG_SEARCH_FORWARD)
2035 entry = TAILQ_FIRST(&s->commits.head);
2036 else
2037 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2040 while (1) {
2041 int have_match = 0;
2043 if (entry == NULL) {
2044 if (s->thread_args.log_complete ||
2045 view->searching == TOG_SEARCH_BACKWARD) {
2046 view->search_next_done = 1;
2047 return NULL;
2050 * Poke the log thread for more commits and return,
2051 * allowing the main loop to make progress. Search
2052 * will resume at s->search_entry once we come back.
2054 s->thread_args.commits_needed++;
2055 return trigger_log_thread(1,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.log_complete,
2058 &s->thread_args.need_commits);
2061 err = match_commit(&have_match, entry->id, entry->commit,
2062 &view->regex);
2063 if (err)
2064 break;
2065 if (have_match) {
2066 view->search_next_done = 1;
2067 s->matched_entry = entry;
2068 break;
2071 s->search_entry = entry;
2072 if (view->searching == TOG_SEARCH_FORWARD)
2073 entry = TAILQ_NEXT(entry, entry);
2074 else
2075 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 if (s->matched_entry) {
2079 int cur = s->selected_entry->idx;
2080 while (cur < s->matched_entry->idx) {
2081 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2082 if (err)
2083 return err;
2084 cur++;
2086 while (cur > s->matched_entry->idx) {
2087 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2088 if (err)
2089 return err;
2090 cur--;
2094 s->search_entry = NULL;
2096 return NULL;
2099 static const struct got_error *
2100 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 struct got_reflist_head *refs, struct got_repository *repo,
2102 const char *head_ref_name, const char *path, int check_disk)
2104 const struct got_error *err = NULL;
2105 struct tog_log_view_state *s = &view->state.log;
2106 struct got_repository *thread_repo = NULL;
2107 struct got_commit_graph *thread_graph = NULL;
2108 int errcode;
2110 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2111 if (err != NULL)
2112 goto done;
2114 /* The commit queue only contains commits being displayed. */
2115 TAILQ_INIT(&s->commits.head);
2116 s->commits.ncommits = 0;
2118 s->refs = refs;
2119 s->repo = repo;
2120 s->head_ref_name = head_ref_name;
2121 s->start_id = got_object_id_dup(start_id);
2122 if (s->start_id == NULL) {
2123 err = got_error_from_errno("got_object_id_dup");
2124 goto done;
2127 SIMPLEQ_INIT(&s->colors);
2128 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2129 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2130 get_color_value("TOG_COLOR_COMMIT"));
2131 if (err)
2132 goto done;
2133 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2134 get_color_value("TOG_COLOR_AUTHOR"));
2135 if (err) {
2136 free_colors(&s->colors);
2137 goto done;
2139 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2140 get_color_value("TOG_COLOR_DATE"));
2141 if (err) {
2142 free_colors(&s->colors);
2143 goto done;
2147 view->show = show_log_view;
2148 view->input = input_log_view;
2149 view->close = close_log_view;
2150 view->search_start = search_start_log_view;
2151 view->search_next = search_next_log_view;
2153 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2154 if (err)
2155 goto done;
2156 err = got_commit_graph_open(&thread_graph, s->in_repo_path, 0);
2157 if (err)
2158 goto done;
2160 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2161 if (errcode) {
2162 err = got_error_set_errno(errcode, "pthread_cond_init");
2163 goto done;
2166 s->thread_args.commits_needed = view->nlines;
2167 s->thread_args.graph = thread_graph;
2168 s->thread_args.commits = &s->commits;
2169 s->thread_args.in_repo_path = s->in_repo_path;
2170 s->thread_args.start_id = s->start_id;
2171 s->thread_args.repo = thread_repo;
2172 s->thread_args.log_complete = 0;
2173 s->thread_args.quit = &s->quit;
2174 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2175 s->thread_args.selected_entry = &s->selected_entry;
2176 s->thread_args.searching = &view->searching;
2177 s->thread_args.search_next_done = &view->search_next_done;
2178 s->thread_args.regex = &view->regex;
2179 done:
2180 if (err)
2181 close_log_view(view);
2182 return err;
2185 static const struct got_error *
2186 show_log_view(struct tog_view *view)
2188 struct tog_log_view_state *s = &view->state.log;
2190 if (s->thread == NULL) {
2191 int errcode = pthread_create(&s->thread, NULL, log_thread,
2192 &s->thread_args);
2193 if (errcode)
2194 return got_error_set_errno(errcode, "pthread_create");
2197 return draw_commits(view, &s->last_displayed_entry,
2198 &s->selected_entry, s->first_displayed_entry,
2199 &s->commits, s->selected, view->nlines, s->refs,
2200 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2203 static const struct got_error *
2204 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2205 struct tog_view **focus_view, struct tog_view *view, int ch)
2207 const struct got_error *err = NULL;
2208 struct tog_log_view_state *s = &view->state.log;
2209 char *parent_path, *in_repo_path = NULL;
2210 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2211 int begin_x = 0;
2212 struct got_object_id *start_id;
2214 switch (ch) {
2215 case 'q':
2216 s->quit = 1;
2217 break;
2218 case 'k':
2219 case KEY_UP:
2220 case '<':
2221 case ',':
2222 if (s->first_displayed_entry == NULL)
2223 break;
2224 if (s->selected > 0)
2225 s->selected--;
2226 else
2227 scroll_up(view, &s->first_displayed_entry, 1,
2228 &s->commits);
2229 break;
2230 case KEY_PPAGE:
2231 case CTRL('b'):
2232 if (s->first_displayed_entry == NULL)
2233 break;
2234 if (TAILQ_FIRST(&s->commits.head) ==
2235 s->first_displayed_entry) {
2236 s->selected = 0;
2237 break;
2239 scroll_up(view, &s->first_displayed_entry,
2240 view->nlines, &s->commits);
2241 break;
2242 case 'j':
2243 case KEY_DOWN:
2244 case '>':
2245 case '.':
2246 if (s->first_displayed_entry == NULL)
2247 break;
2248 if (s->selected < MIN(view->nlines - 2,
2249 s->commits.ncommits - 1)) {
2250 s->selected++;
2251 break;
2253 err = scroll_down(view, &s->first_displayed_entry, 1,
2254 &s->last_displayed_entry, &s->commits,
2255 &s->thread_args.log_complete,
2256 &s->thread_args.commits_needed,
2257 &s->thread_args.need_commits);
2258 break;
2259 case KEY_NPAGE:
2260 case CTRL('f'): {
2261 struct commit_queue_entry *first;
2262 first = s->first_displayed_entry;
2263 if (first == NULL)
2264 break;
2265 err = scroll_down(view, &s->first_displayed_entry,
2266 view->nlines, &s->last_displayed_entry,
2267 &s->commits, &s->thread_args.log_complete,
2268 &s->thread_args.commits_needed,
2269 &s->thread_args.need_commits);
2270 if (err)
2271 break;
2272 if (first == s->first_displayed_entry &&
2273 s->selected < MIN(view->nlines - 2,
2274 s->commits.ncommits - 1)) {
2275 /* can't scroll further down */
2276 s->selected = MIN(view->nlines - 2,
2277 s->commits.ncommits - 1);
2279 err = NULL;
2280 break;
2282 case KEY_RESIZE:
2283 if (s->selected > view->nlines - 2)
2284 s->selected = view->nlines - 2;
2285 if (s->selected > s->commits.ncommits - 1)
2286 s->selected = s->commits.ncommits - 1;
2287 break;
2288 case KEY_ENTER:
2289 case ' ':
2290 case '\r':
2291 if (s->selected_entry == NULL)
2292 break;
2293 if (view_is_parent_view(view))
2294 begin_x = view_split_begin_x(view->begin_x);
2295 err = open_diff_view_for_commit(&diff_view, begin_x,
2296 s->selected_entry->commit, s->selected_entry->id,
2297 view, s->refs, s->repo);
2298 if (err)
2299 break;
2300 if (view_is_parent_view(view)) {
2301 err = view_close_child(view);
2302 if (err)
2303 return err;
2304 err = view_set_child(view, diff_view);
2305 if (err) {
2306 view_close(diff_view);
2307 break;
2309 *focus_view = diff_view;
2310 view->child_focussed = 1;
2311 } else
2312 *new_view = diff_view;
2313 break;
2314 case 't':
2315 if (s->selected_entry == NULL)
2316 break;
2317 if (view_is_parent_view(view))
2318 begin_x = view_split_begin_x(view->begin_x);
2319 err = browse_commit_tree(&tree_view, begin_x,
2320 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2321 if (err)
2322 break;
2323 if (view_is_parent_view(view)) {
2324 err = view_close_child(view);
2325 if (err)
2326 return err;
2327 err = view_set_child(view, tree_view);
2328 if (err) {
2329 view_close(tree_view);
2330 break;
2332 *focus_view = tree_view;
2333 view->child_focussed = 1;
2334 } else
2335 *new_view = tree_view;
2336 break;
2337 case KEY_BACKSPACE:
2338 if (strcmp(s->in_repo_path, "/") == 0)
2339 break;
2340 parent_path = dirname(s->in_repo_path);
2341 if (parent_path && strcmp(parent_path, ".") != 0) {
2342 err = stop_log_thread(s);
2343 if (err)
2344 return err;
2345 lv = view_open(view->nlines, view->ncols,
2346 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2347 if (lv == NULL)
2348 return got_error_from_errno(
2349 "view_open");
2350 err = open_log_view(lv, s->start_id, s->refs,
2351 s->repo, s->head_ref_name, parent_path, 0);
2352 if (err)
2353 return err;;
2354 if (view_is_parent_view(view))
2355 *new_view = lv;
2356 else {
2357 view_set_child(view->parent, lv);
2358 *focus_view = lv;
2360 return NULL;
2362 break;
2363 case CTRL('l'):
2364 err = stop_log_thread(s);
2365 if (err)
2366 return err;
2367 lv = view_open(view->nlines, view->ncols,
2368 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2369 if (lv == NULL)
2370 return got_error_from_errno("view_open");
2371 err = get_head_commit_id(&start_id, s->head_ref_name ?
2372 s->head_ref_name : GOT_REF_HEAD, s->repo);
2373 if (err) {
2374 view_close(lv);
2375 return err;
2377 in_repo_path = strdup(s->in_repo_path);
2378 if (in_repo_path == NULL) {
2379 free(start_id);
2380 view_close(lv);
2381 return got_error_from_errno("strdup");
2383 got_ref_list_free(s->refs);
2384 err = got_ref_list(s->refs, s->repo, NULL,
2385 got_ref_cmp_by_name, NULL);
2386 if (err) {
2387 free(start_id);
2388 view_close(lv);
2389 return err;
2391 err = open_log_view(lv, start_id, s->refs, s->repo,
2392 s->head_ref_name, in_repo_path, 0);
2393 if (err) {
2394 free(start_id);
2395 view_close(lv);
2396 return err;;
2398 *dead_view = view;
2399 *new_view = lv;
2400 break;
2401 default:
2402 break;
2405 return err;
2408 static const struct got_error *
2409 apply_unveil(const char *repo_path, const char *worktree_path)
2411 const struct got_error *error;
2413 #ifdef PROFILE
2414 if (unveil("gmon.out", "rwc") != 0)
2415 return got_error_from_errno2("unveil", "gmon.out");
2416 #endif
2417 if (repo_path && unveil(repo_path, "r") != 0)
2418 return got_error_from_errno2("unveil", repo_path);
2420 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2421 return got_error_from_errno2("unveil", worktree_path);
2423 if (unveil("/tmp", "rwc") != 0)
2424 return got_error_from_errno2("unveil", "/tmp");
2426 error = got_privsep_unveil_exec_helpers();
2427 if (error != NULL)
2428 return error;
2430 if (unveil(NULL, NULL) != 0)
2431 return got_error_from_errno("unveil");
2433 return NULL;
2436 static void
2437 init_curses(void)
2439 initscr();
2440 cbreak();
2441 halfdelay(1); /* Do fast refresh while initial view is loading. */
2442 noecho();
2443 nonl();
2444 intrflush(stdscr, FALSE);
2445 keypad(stdscr, TRUE);
2446 curs_set(0);
2447 if (getenv("TOG_COLORS") != NULL) {
2448 start_color();
2449 use_default_colors();
2451 signal(SIGWINCH, tog_sigwinch);
2452 signal(SIGPIPE, tog_sigpipe);
2455 static const struct got_error *
2456 cmd_log(int argc, char *argv[])
2458 const struct got_error *error;
2459 struct got_repository *repo = NULL;
2460 struct got_worktree *worktree = NULL;
2461 struct got_reflist_head refs;
2462 struct got_object_id *start_id = NULL;
2463 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2464 char *start_commit = NULL, *head_ref_name = NULL;
2465 int ch;
2466 struct tog_view *view;
2468 SIMPLEQ_INIT(&refs);
2470 #ifndef PROFILE
2471 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2472 NULL) == -1)
2473 err(1, "pledge");
2474 #endif
2476 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2477 switch (ch) {
2478 case 'c':
2479 start_commit = optarg;
2480 break;
2481 case 'r':
2482 repo_path = realpath(optarg, NULL);
2483 if (repo_path == NULL)
2484 return got_error_from_errno2("realpath",
2485 optarg);
2486 break;
2487 default:
2488 usage_log();
2489 /* NOTREACHED */
2493 argc -= optind;
2494 argv += optind;
2496 cwd = getcwd(NULL, 0);
2497 if (cwd == NULL) {
2498 error = got_error_from_errno("getcwd");
2499 goto done;
2501 error = got_worktree_open(&worktree, cwd);
2502 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2503 goto done;
2504 error = NULL;
2506 if (argc == 0) {
2507 path = strdup("");
2508 if (path == NULL) {
2509 error = got_error_from_errno("strdup");
2510 goto done;
2512 } else if (argc == 1) {
2513 if (worktree) {
2514 error = got_worktree_resolve_path(&path, worktree,
2515 argv[0]);
2516 if (error)
2517 goto done;
2518 } else {
2519 path = strdup(argv[0]);
2520 if (path == NULL) {
2521 error = got_error_from_errno("strdup");
2522 goto done;
2525 } else
2526 usage_log();
2528 if (repo_path == NULL) {
2529 if (worktree)
2530 repo_path = strdup(
2531 got_worktree_get_repo_path(worktree));
2532 else
2533 repo_path = strdup(cwd);
2535 if (repo_path == NULL) {
2536 error = got_error_from_errno("strdup");
2537 goto done;
2540 init_curses();
2542 error = got_repo_open(&repo, repo_path, NULL);
2543 if (error != NULL)
2544 goto done;
2546 error = apply_unveil(got_repo_get_path(repo),
2547 worktree ? got_worktree_get_root_path(worktree) : NULL);
2548 if (error)
2549 goto done;
2551 if (start_commit == NULL)
2552 error = get_head_commit_id(&start_id, worktree ?
2553 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2554 repo);
2555 else {
2556 error = get_head_commit_id(&start_id, start_commit, repo);
2557 if (error) {
2558 if (error->code != GOT_ERR_NOT_REF)
2559 goto done;
2560 error = got_repo_match_object_id_prefix(&start_id,
2561 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2564 if (error != NULL)
2565 goto done;
2567 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2568 if (error)
2569 goto done;
2571 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2572 if (view == NULL) {
2573 error = got_error_from_errno("view_open");
2574 goto done;
2576 if (worktree) {
2577 head_ref_name = strdup(
2578 got_worktree_get_head_ref_name(worktree));
2579 if (head_ref_name == NULL) {
2580 error = got_error_from_errno("strdup");
2581 goto done;
2584 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2585 path, 1);
2586 if (error)
2587 goto done;
2588 if (worktree) {
2589 /* Release work tree lock. */
2590 got_worktree_close(worktree);
2591 worktree = NULL;
2593 error = view_loop(view);
2594 done:
2595 free(repo_path);
2596 free(cwd);
2597 free(path);
2598 free(start_id);
2599 free(head_ref_name);
2600 if (repo)
2601 got_repo_close(repo);
2602 if (worktree)
2603 got_worktree_close(worktree);
2604 got_ref_list_free(&refs);
2605 return error;
2608 __dead static void
2609 usage_diff(void)
2611 endwin();
2612 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2613 getprogname());
2614 exit(1);
2617 static char *
2618 parse_next_line(FILE *f, size_t *len)
2620 char *line;
2621 size_t linelen;
2622 size_t lineno;
2623 const char delim[3] = { '\0', '\0', '\0'};
2625 line = fparseln(f, &linelen, &lineno, delim, 0);
2626 if (len)
2627 *len = linelen;
2628 return line;
2631 static int
2632 match_line(const char *line, regex_t *regex)
2634 regmatch_t regmatch;
2636 return regexec(regex, line, 1, &regmatch, 0) == 0;
2639 struct tog_color *
2640 match_color(struct tog_colors *colors, const char *line)
2642 struct tog_color *tc = NULL;
2644 SIMPLEQ_FOREACH(tc, colors, entry) {
2645 if (match_line(line, &tc->regex))
2646 return tc;
2649 return NULL;
2652 static const struct got_error *
2653 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2654 int *last_displayed_line, int *eof, int max_lines, char *header,
2655 struct tog_colors *colors)
2657 const struct got_error *err;
2658 int nlines = 0, nprinted = 0;
2659 char *line;
2660 struct tog_color *tc;
2661 size_t len;
2662 wchar_t *wline;
2663 int width;
2665 rewind(f);
2666 werase(view->window);
2668 if (header) {
2669 err = format_line(&wline, &width, header, view->ncols, 0);
2670 if (err) {
2671 return err;
2674 if (view_needs_focus_indication(view))
2675 wstandout(view->window);
2676 waddwstr(view->window, wline);
2677 if (view_needs_focus_indication(view))
2678 wstandend(view->window);
2679 if (width <= view->ncols - 1)
2680 waddch(view->window, '\n');
2682 if (max_lines <= 1)
2683 return NULL;
2684 max_lines--;
2687 *eof = 0;
2688 while (nprinted < max_lines) {
2689 line = parse_next_line(f, &len);
2690 if (line == NULL) {
2691 *eof = 1;
2692 break;
2694 if (++nlines < *first_displayed_line) {
2695 free(line);
2696 continue;
2699 err = format_line(&wline, &width, line, view->ncols, 0);
2700 if (err) {
2701 free(line);
2702 return err;
2705 tc = match_color(colors, line);
2706 if (tc)
2707 wattr_on(view->window,
2708 COLOR_PAIR(tc->colorpair), NULL);
2709 waddwstr(view->window, wline);
2710 if (tc)
2711 wattr_off(view->window,
2712 COLOR_PAIR(tc->colorpair), NULL);
2713 if (width <= view->ncols - 1)
2714 waddch(view->window, '\n');
2715 if (++nprinted == 1)
2716 *first_displayed_line = nlines;
2717 free(line);
2718 free(wline);
2719 wline = NULL;
2721 *last_displayed_line = nlines;
2723 view_vborder(view);
2725 if (*eof) {
2726 while (nprinted < view->nlines) {
2727 waddch(view->window, '\n');
2728 nprinted++;
2731 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2732 if (err) {
2733 return err;
2736 wstandout(view->window);
2737 waddwstr(view->window, wline);
2738 wstandend(view->window);
2741 return NULL;
2744 static char *
2745 get_datestr(time_t *time, char *datebuf)
2747 struct tm mytm, *tm;
2748 char *p, *s;
2750 tm = gmtime_r(time, &mytm);
2751 if (tm == NULL)
2752 return NULL;
2753 s = asctime_r(tm, datebuf);
2754 if (s == NULL)
2755 return NULL;
2756 p = strchr(s, '\n');
2757 if (p)
2758 *p = '\0';
2759 return s;
2762 static const struct got_error *
2763 write_commit_info(struct got_object_id *commit_id,
2764 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2766 const struct got_error *err = NULL;
2767 char datebuf[26], *datestr;
2768 struct got_commit_object *commit;
2769 char *id_str = NULL, *logmsg = NULL;
2770 time_t committer_time;
2771 const char *author, *committer;
2772 char *refs_str = NULL;
2774 if (refs) {
2775 err = build_refs_str(&refs_str, refs, commit_id, repo);
2776 if (err)
2777 return err;
2780 err = got_object_open_as_commit(&commit, repo, commit_id);
2781 if (err)
2782 return err;
2784 err = got_object_id_str(&id_str, commit_id);
2785 if (err) {
2786 err = got_error_from_errno("got_object_id_str");
2787 goto done;
2790 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2791 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2792 err = got_error_from_errno("fprintf");
2793 goto done;
2795 if (fprintf(outfile, "from: %s\n",
2796 got_object_commit_get_author(commit)) < 0) {
2797 err = got_error_from_errno("fprintf");
2798 goto done;
2800 committer_time = got_object_commit_get_committer_time(commit);
2801 datestr = get_datestr(&committer_time, datebuf);
2802 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2803 err = got_error_from_errno("fprintf");
2804 goto done;
2806 author = got_object_commit_get_author(commit);
2807 committer = got_object_commit_get_committer(commit);
2808 if (strcmp(author, committer) != 0 &&
2809 fprintf(outfile, "via: %s\n", committer) < 0) {
2810 err = got_error_from_errno("fprintf");
2811 goto done;
2813 err = got_object_commit_get_logmsg(&logmsg, commit);
2814 if (err)
2815 goto done;
2816 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2817 err = got_error_from_errno("fprintf");
2818 goto done;
2820 done:
2821 free(id_str);
2822 free(logmsg);
2823 free(refs_str);
2824 got_object_commit_close(commit);
2825 return err;
2828 static const struct got_error *
2829 create_diff(struct tog_diff_view_state *s)
2831 const struct got_error *err = NULL;
2832 FILE *f = NULL;
2833 int obj_type;
2835 f = got_opentemp();
2836 if (f == NULL) {
2837 err = got_error_from_errno("got_opentemp");
2838 goto done;
2840 if (s->f && fclose(s->f) != 0) {
2841 err = got_error_from_errno("fclose");
2842 goto done;
2844 s->f = f;
2846 if (s->id1)
2847 err = got_object_get_type(&obj_type, s->repo, s->id1);
2848 else
2849 err = got_object_get_type(&obj_type, s->repo, s->id2);
2850 if (err)
2851 goto done;
2853 switch (obj_type) {
2854 case GOT_OBJ_TYPE_BLOB:
2855 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2856 s->diff_context, 0, s->repo, f);
2857 break;
2858 case GOT_OBJ_TYPE_TREE:
2859 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2860 s->diff_context, 0, s->repo, f);
2861 break;
2862 case GOT_OBJ_TYPE_COMMIT: {
2863 const struct got_object_id_queue *parent_ids;
2864 struct got_object_qid *pid;
2865 struct got_commit_object *commit2;
2867 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2868 if (err)
2869 break;
2870 /* Show commit info if we're diffing to a parent/root commit. */
2871 if (s->id1 == NULL)
2872 write_commit_info(s->id2, s->refs, s->repo, f);
2873 else {
2874 parent_ids = got_object_commit_get_parent_ids(commit2);
2875 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2876 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2877 write_commit_info(s->id2, s->refs,
2878 s->repo, f);
2879 break;
2883 got_object_commit_close(commit2);
2885 err = got_diff_objects_as_commits(s->id1, s->id2,
2886 s->diff_context, 0, s->repo, f);
2887 break;
2889 default:
2890 err = got_error(GOT_ERR_OBJ_TYPE);
2891 break;
2893 done:
2894 if (f && fflush(f) != 0 && err == NULL)
2895 err = got_error_from_errno("fflush");
2896 return err;
2899 static void
2900 diff_view_indicate_progress(struct tog_view *view)
2902 mvwaddstr(view->window, 0, 0, "diffing...");
2903 update_panels();
2904 doupdate();
2907 static const struct got_error *
2908 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2909 struct got_object_id *id2, struct tog_view *log_view,
2910 struct got_reflist_head *refs, struct got_repository *repo)
2912 const struct got_error *err;
2914 if (id1 != NULL && id2 != NULL) {
2915 int type1, type2;
2916 err = got_object_get_type(&type1, repo, id1);
2917 if (err)
2918 return err;
2919 err = got_object_get_type(&type2, repo, id2);
2920 if (err)
2921 return err;
2923 if (type1 != type2)
2924 return got_error(GOT_ERR_OBJ_TYPE);
2927 if (id1) {
2928 view->state.diff.id1 = got_object_id_dup(id1);
2929 if (view->state.diff.id1 == NULL)
2930 return got_error_from_errno("got_object_id_dup");
2931 } else
2932 view->state.diff.id1 = NULL;
2934 view->state.diff.id2 = got_object_id_dup(id2);
2935 if (view->state.diff.id2 == NULL) {
2936 free(view->state.diff.id1);
2937 view->state.diff.id1 = NULL;
2938 return got_error_from_errno("got_object_id_dup");
2940 view->state.diff.f = NULL;
2941 view->state.diff.first_displayed_line = 1;
2942 view->state.diff.last_displayed_line = view->nlines;
2943 view->state.diff.diff_context = 3;
2944 view->state.diff.log_view = log_view;
2945 view->state.diff.repo = repo;
2946 view->state.diff.refs = refs;
2947 SIMPLEQ_INIT(&view->state.diff.colors);
2949 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2950 err = add_color(&view->state.diff.colors,
2951 "^-", TOG_COLOR_DIFF_MINUS,
2952 get_color_value("TOG_COLOR_DIFF_MINUS"));
2953 if (err)
2954 return err;
2955 err = add_color(&view->state.diff.colors, "^\\+",
2956 TOG_COLOR_DIFF_PLUS,
2957 get_color_value("TOG_COLOR_DIFF_PLUS"));
2958 if (err) {
2959 free_colors(&view->state.diff.colors);
2960 return err;
2962 err = add_color(&view->state.diff.colors,
2963 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2964 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2965 if (err) {
2966 free_colors(&view->state.diff.colors);
2967 return err;
2970 err = add_color(&view->state.diff.colors,
2971 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2972 get_color_value("TOG_COLOR_DIFF_META"));
2973 if (err) {
2974 free_colors(&view->state.diff.colors);
2975 return err;
2978 err = add_color(&view->state.diff.colors,
2979 "^(from|via): ", TOG_COLOR_AUTHOR,
2980 get_color_value("TOG_COLOR_AUTHOR"));
2981 if (err) {
2982 free_colors(&view->state.diff.colors);
2983 return err;
2986 err = add_color(&view->state.diff.colors,
2987 "^date: ", TOG_COLOR_DATE,
2988 get_color_value("TOG_COLOR_DATE"));
2989 if (err) {
2990 free_colors(&view->state.diff.colors);
2991 return err;
2995 if (log_view && view_is_splitscreen(view))
2996 show_log_view(log_view); /* draw vborder */
2997 diff_view_indicate_progress(view);
2999 err = create_diff(&view->state.diff);
3000 if (err) {
3001 free(view->state.diff.id1);
3002 view->state.diff.id1 = NULL;
3003 free(view->state.diff.id2);
3004 view->state.diff.id2 = NULL;
3005 return err;
3008 view->show = show_diff_view;
3009 view->input = input_diff_view;
3010 view->close = close_diff_view;
3012 return NULL;
3015 static const struct got_error *
3016 close_diff_view(struct tog_view *view)
3018 const struct got_error *err = NULL;
3020 free(view->state.diff.id1);
3021 view->state.diff.id1 = NULL;
3022 free(view->state.diff.id2);
3023 view->state.diff.id2 = NULL;
3024 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3025 err = got_error_from_errno("fclose");
3026 free_colors(&view->state.diff.colors);
3027 return err;
3030 static const struct got_error *
3031 show_diff_view(struct tog_view *view)
3033 const struct got_error *err;
3034 struct tog_diff_view_state *s = &view->state.diff;
3035 char *id_str1 = NULL, *id_str2, *header;
3037 if (s->id1) {
3038 err = got_object_id_str(&id_str1, s->id1);
3039 if (err)
3040 return err;
3042 err = got_object_id_str(&id_str2, s->id2);
3043 if (err)
3044 return err;
3046 if (asprintf(&header, "diff %s %s",
3047 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3048 err = got_error_from_errno("asprintf");
3049 free(id_str1);
3050 free(id_str2);
3051 return err;
3053 free(id_str1);
3054 free(id_str2);
3056 return draw_file(view, s->f, &s->first_displayed_line,
3057 &s->last_displayed_line, &s->eof, view->nlines,
3058 header, &s->colors);
3061 static const struct got_error *
3062 set_selected_commit(struct tog_diff_view_state *s,
3063 struct commit_queue_entry *entry)
3065 const struct got_error *err;
3066 const struct got_object_id_queue *parent_ids;
3067 struct got_commit_object *selected_commit;
3068 struct got_object_qid *pid;
3070 free(s->id2);
3071 s->id2 = got_object_id_dup(entry->id);
3072 if (s->id2 == NULL)
3073 return got_error_from_errno("got_object_id_dup");
3075 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3076 if (err)
3077 return err;
3078 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3079 free(s->id1);
3080 pid = SIMPLEQ_FIRST(parent_ids);
3081 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3082 got_object_commit_close(selected_commit);
3083 return NULL;
3086 static const struct got_error *
3087 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3088 struct tog_view **focus_view, struct tog_view *view, int ch)
3090 const struct got_error *err = NULL;
3091 struct tog_diff_view_state *s = &view->state.diff;
3092 struct tog_log_view_state *ls;
3093 struct commit_queue_entry *entry;
3094 int i;
3096 switch (ch) {
3097 case 'k':
3098 case KEY_UP:
3099 if (s->first_displayed_line > 1)
3100 s->first_displayed_line--;
3101 break;
3102 case KEY_PPAGE:
3103 case CTRL('b'):
3104 if (s->first_displayed_line == 1)
3105 break;
3106 i = 0;
3107 while (i++ < view->nlines - 1 &&
3108 s->first_displayed_line > 1)
3109 s->first_displayed_line--;
3110 break;
3111 case 'j':
3112 case KEY_DOWN:
3113 if (!s->eof)
3114 s->first_displayed_line++;
3115 break;
3116 case KEY_NPAGE:
3117 case CTRL('f'):
3118 case ' ':
3119 if (s->eof)
3120 break;
3121 i = 0;
3122 while (!s->eof && i++ < view->nlines - 1) {
3123 char *line;
3124 line = parse_next_line(s->f, NULL);
3125 s->first_displayed_line++;
3126 if (line == NULL)
3127 break;
3129 break;
3130 case '[':
3131 if (s->diff_context > 0) {
3132 s->diff_context--;
3133 diff_view_indicate_progress(view);
3134 err = create_diff(s);
3136 break;
3137 case ']':
3138 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3139 s->diff_context++;
3140 diff_view_indicate_progress(view);
3141 err = create_diff(s);
3143 break;
3144 case '<':
3145 case ',':
3146 if (s->log_view == NULL)
3147 break;
3148 ls = &s->log_view->state.log;
3149 entry = TAILQ_PREV(ls->selected_entry,
3150 commit_queue_head, entry);
3151 if (entry == NULL)
3152 break;
3154 err = input_log_view(NULL, NULL, NULL, s->log_view,
3155 KEY_UP);
3156 if (err)
3157 break;
3159 err = set_selected_commit(s, entry);
3160 if (err)
3161 break;
3163 s->first_displayed_line = 1;
3164 s->last_displayed_line = view->nlines;
3166 diff_view_indicate_progress(view);
3167 err = create_diff(s);
3168 break;
3169 case '>':
3170 case '.':
3171 if (s->log_view == NULL)
3172 break;
3173 ls = &s->log_view->state.log;
3175 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3176 ls->thread_args.commits_needed++;
3178 /* Display "loading..." in log view. */
3179 show_log_view(s->log_view);
3180 update_panels();
3181 doupdate();
3183 err = trigger_log_thread(1 /* load_all */,
3184 &ls->thread_args.commits_needed,
3185 &ls->thread_args.log_complete,
3186 &ls->thread_args.need_commits);
3187 if (err)
3188 break;
3190 err = input_log_view(NULL, NULL, NULL, s->log_view,
3191 KEY_DOWN);
3192 if (err)
3193 break;
3195 entry = TAILQ_NEXT(ls->selected_entry, entry);
3196 if (entry == NULL)
3197 break;
3199 err = set_selected_commit(s, entry);
3200 if (err)
3201 break;
3203 s->first_displayed_line = 1;
3204 s->last_displayed_line = view->nlines;
3206 diff_view_indicate_progress(view);
3207 err = create_diff(s);
3208 break;
3209 default:
3210 break;
3213 return err;
3216 static const struct got_error *
3217 cmd_diff(int argc, char *argv[])
3219 const struct got_error *error = NULL;
3220 struct got_repository *repo = NULL;
3221 struct got_reflist_head refs;
3222 struct got_object_id *id1 = NULL, *id2 = NULL;
3223 char *repo_path = NULL;
3224 char *id_str1 = NULL, *id_str2 = NULL;
3225 int ch;
3226 struct tog_view *view;
3228 SIMPLEQ_INIT(&refs);
3230 #ifndef PROFILE
3231 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3232 NULL) == -1)
3233 err(1, "pledge");
3234 #endif
3236 while ((ch = getopt(argc, argv, "")) != -1) {
3237 switch (ch) {
3238 default:
3239 usage_diff();
3240 /* NOTREACHED */
3244 argc -= optind;
3245 argv += optind;
3247 if (argc == 0) {
3248 usage_diff(); /* TODO show local worktree changes */
3249 } else if (argc == 2) {
3250 repo_path = getcwd(NULL, 0);
3251 if (repo_path == NULL)
3252 return got_error_from_errno("getcwd");
3253 id_str1 = argv[0];
3254 id_str2 = argv[1];
3255 } else if (argc == 3) {
3256 repo_path = realpath(argv[0], NULL);
3257 if (repo_path == NULL)
3258 return got_error_from_errno2("realpath", argv[0]);
3259 id_str1 = argv[1];
3260 id_str2 = argv[2];
3261 } else
3262 usage_diff();
3264 init_curses();
3266 error = got_repo_open(&repo, repo_path, NULL);
3267 if (error)
3268 goto done;
3270 error = apply_unveil(got_repo_get_path(repo), NULL);
3271 if (error)
3272 goto done;
3274 error = got_repo_match_object_id_prefix(&id1, id_str1,
3275 GOT_OBJ_TYPE_ANY, repo);
3276 if (error)
3277 goto done;
3279 error = got_repo_match_object_id_prefix(&id2, id_str2,
3280 GOT_OBJ_TYPE_ANY, repo);
3281 if (error)
3282 goto done;
3284 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3285 if (error)
3286 goto done;
3288 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3289 if (view == NULL) {
3290 error = got_error_from_errno("view_open");
3291 goto done;
3293 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3294 if (error)
3295 goto done;
3296 error = view_loop(view);
3297 done:
3298 free(repo_path);
3299 if (repo)
3300 got_repo_close(repo);
3301 got_ref_list_free(&refs);
3302 return error;
3305 __dead static void
3306 usage_blame(void)
3308 endwin();
3309 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3310 getprogname());
3311 exit(1);
3314 struct tog_blame_line {
3315 int annotated;
3316 struct got_object_id *id;
3319 static const struct got_error *
3320 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3321 const char *path, struct tog_blame_line *lines, int nlines,
3322 int blame_complete, int selected_line, int *first_displayed_line,
3323 int *last_displayed_line, int *eof, int max_lines,
3324 struct tog_colors *colors)
3326 const struct got_error *err;
3327 int lineno = 0, nprinted = 0;
3328 char *line;
3329 size_t len;
3330 wchar_t *wline;
3331 int width;
3332 struct tog_blame_line *blame_line;
3333 struct got_object_id *prev_id = NULL;
3334 char *id_str;
3335 struct tog_color *tc;
3337 err = got_object_id_str(&id_str, id);
3338 if (err)
3339 return err;
3341 rewind(f);
3342 werase(view->window);
3344 if (asprintf(&line, "commit %s", id_str) == -1) {
3345 err = got_error_from_errno("asprintf");
3346 free(id_str);
3347 return err;
3350 err = format_line(&wline, &width, line, view->ncols, 0);
3351 free(line);
3352 line = NULL;
3353 if (err)
3354 return err;
3355 if (view_needs_focus_indication(view))
3356 wstandout(view->window);
3357 tc = get_color(colors, TOG_COLOR_COMMIT);
3358 if (tc)
3359 wattr_on(view->window,
3360 COLOR_PAIR(tc->colorpair), NULL);
3361 waddwstr(view->window, wline);
3362 if (tc)
3363 wattr_off(view->window,
3364 COLOR_PAIR(tc->colorpair), NULL);
3365 if (view_needs_focus_indication(view))
3366 wstandend(view->window);
3367 free(wline);
3368 wline = NULL;
3369 if (width < view->ncols - 1)
3370 waddch(view->window, '\n');
3372 if (asprintf(&line, "[%d/%d] %s%s",
3373 *first_displayed_line - 1 + selected_line, nlines,
3374 blame_complete ? "" : "annotating... ", path) == -1) {
3375 free(id_str);
3376 return got_error_from_errno("asprintf");
3378 free(id_str);
3379 err = format_line(&wline, &width, line, view->ncols, 0);
3380 free(line);
3381 line = NULL;
3382 if (err)
3383 return err;
3384 waddwstr(view->window, wline);
3385 free(wline);
3386 wline = NULL;
3387 if (width < view->ncols - 1)
3388 waddch(view->window, '\n');
3390 *eof = 0;
3391 while (nprinted < max_lines - 2) {
3392 line = parse_next_line(f, &len);
3393 if (line == NULL) {
3394 *eof = 1;
3395 break;
3397 if (++lineno < *first_displayed_line) {
3398 free(line);
3399 continue;
3402 if (view->ncols <= 9) {
3403 width = 9;
3404 wline = wcsdup(L"");
3405 if (wline == NULL)
3406 err = got_error_from_errno("wcsdup");
3407 } else {
3408 err = format_line(&wline, &width, line,
3409 view->ncols - 9, 9);
3410 width += 9;
3412 if (err) {
3413 free(line);
3414 return err;
3417 if (view->focussed && nprinted == selected_line - 1)
3418 wstandout(view->window);
3420 if (nlines > 0) {
3421 blame_line = &lines[lineno - 1];
3422 if (blame_line->annotated && prev_id &&
3423 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3424 !(view->focussed &&
3425 nprinted == selected_line - 1)) {
3426 waddstr(view->window, " ");
3427 } else if (blame_line->annotated) {
3428 char *id_str;
3429 err = got_object_id_str(&id_str, blame_line->id);
3430 if (err) {
3431 free(line);
3432 free(wline);
3433 return err;
3435 tc = get_color(colors, TOG_COLOR_COMMIT);
3436 if (tc)
3437 wattr_on(view->window,
3438 COLOR_PAIR(tc->colorpair), NULL);
3439 wprintw(view->window, "%.8s", id_str);
3440 if (tc)
3441 wattr_off(view->window,
3442 COLOR_PAIR(tc->colorpair), NULL);
3443 free(id_str);
3444 prev_id = blame_line->id;
3445 } else {
3446 waddstr(view->window, "........");
3447 prev_id = NULL;
3449 } else {
3450 waddstr(view->window, "........");
3451 prev_id = NULL;
3454 if (view->focussed && nprinted == selected_line - 1)
3455 wstandend(view->window);
3456 waddstr(view->window, " ");
3458 waddwstr(view->window, wline);
3459 if (width <= view->ncols - 1)
3460 waddch(view->window, '\n');
3461 if (++nprinted == 1)
3462 *first_displayed_line = lineno;
3463 free(line);
3464 free(wline);
3465 wline = NULL;
3467 *last_displayed_line = lineno;
3469 view_vborder(view);
3471 return NULL;
3474 static const struct got_error *
3475 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3477 const struct got_error *err = NULL;
3478 struct tog_blame_cb_args *a = arg;
3479 struct tog_blame_line *line;
3480 int errcode;
3482 if (nlines != a->nlines ||
3483 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3484 return got_error(GOT_ERR_RANGE);
3486 errcode = pthread_mutex_lock(&tog_mutex);
3487 if (errcode)
3488 return got_error_set_errno(errcode, "pthread_mutex_lock");
3490 if (*a->quit) { /* user has quit the blame view */
3491 err = got_error(GOT_ERR_ITER_COMPLETED);
3492 goto done;
3495 if (lineno == -1)
3496 goto done; /* no change in this commit */
3498 line = &a->lines[lineno - 1];
3499 if (line->annotated)
3500 goto done;
3502 line->id = got_object_id_dup(id);
3503 if (line->id == NULL) {
3504 err = got_error_from_errno("got_object_id_dup");
3505 goto done;
3507 line->annotated = 1;
3508 done:
3509 errcode = pthread_mutex_unlock(&tog_mutex);
3510 if (errcode)
3511 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3512 return err;
3515 static void *
3516 blame_thread(void *arg)
3518 const struct got_error *err;
3519 struct tog_blame_thread_args *ta = arg;
3520 struct tog_blame_cb_args *a = ta->cb_args;
3521 int errcode;
3523 err = got_blame(ta->path, a->commit_id, ta->repo,
3524 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3525 if (err && err->code == GOT_ERR_CANCELLED)
3526 err = NULL;
3528 errcode = pthread_mutex_lock(&tog_mutex);
3529 if (errcode)
3530 return (void *)got_error_set_errno(errcode,
3531 "pthread_mutex_lock");
3533 got_repo_close(ta->repo);
3534 ta->repo = NULL;
3535 *ta->complete = 1;
3537 errcode = pthread_mutex_unlock(&tog_mutex);
3538 if (errcode && err == NULL)
3539 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3541 return (void *)err;
3544 static struct got_object_id *
3545 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3546 int first_displayed_line, int selected_line)
3548 struct tog_blame_line *line;
3550 if (nlines <= 0)
3551 return NULL;
3553 line = &lines[first_displayed_line - 1 + selected_line - 1];
3554 if (!line->annotated)
3555 return NULL;
3557 return line->id;
3560 static const struct got_error *
3561 stop_blame(struct tog_blame *blame)
3563 const struct got_error *err = NULL;
3564 int i;
3566 if (blame->thread) {
3567 int errcode;
3568 errcode = pthread_mutex_unlock(&tog_mutex);
3569 if (errcode)
3570 return got_error_set_errno(errcode,
3571 "pthread_mutex_unlock");
3572 errcode = pthread_join(blame->thread, (void **)&err);
3573 if (errcode)
3574 return got_error_set_errno(errcode, "pthread_join");
3575 errcode = pthread_mutex_lock(&tog_mutex);
3576 if (errcode)
3577 return got_error_set_errno(errcode,
3578 "pthread_mutex_lock");
3579 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3580 err = NULL;
3581 blame->thread = NULL;
3583 if (blame->thread_args.repo) {
3584 got_repo_close(blame->thread_args.repo);
3585 blame->thread_args.repo = NULL;
3587 if (blame->f) {
3588 if (fclose(blame->f) != 0 && err == NULL)
3589 err = got_error_from_errno("fclose");
3590 blame->f = NULL;
3592 if (blame->lines) {
3593 for (i = 0; i < blame->nlines; i++)
3594 free(blame->lines[i].id);
3595 free(blame->lines);
3596 blame->lines = NULL;
3598 free(blame->cb_args.commit_id);
3599 blame->cb_args.commit_id = NULL;
3601 return err;
3604 static const struct got_error *
3605 cancel_blame_view(void *arg)
3607 const struct got_error *err = NULL;
3608 int *done = arg;
3609 int errcode;
3611 errcode = pthread_mutex_lock(&tog_mutex);
3612 if (errcode)
3613 return got_error_set_errno(errcode,
3614 "pthread_mutex_unlock");
3616 if (*done)
3617 err = got_error(GOT_ERR_CANCELLED);
3619 errcode = pthread_mutex_unlock(&tog_mutex);
3620 if (errcode)
3621 return got_error_set_errno(errcode,
3622 "pthread_mutex_lock");
3624 return err;
3627 static const struct got_error *
3628 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3629 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3630 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3631 struct got_repository *repo)
3633 const struct got_error *err = NULL;
3634 struct got_blob_object *blob = NULL;
3635 struct got_repository *thread_repo = NULL;
3636 struct got_object_id *obj_id = NULL;
3637 int obj_type;
3639 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3640 if (err)
3641 return err;
3642 if (obj_id == NULL)
3643 return got_error(GOT_ERR_NO_OBJ);
3645 err = got_object_get_type(&obj_type, repo, obj_id);
3646 if (err)
3647 goto done;
3649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3650 err = got_error(GOT_ERR_OBJ_TYPE);
3651 goto done;
3654 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3655 if (err)
3656 goto done;
3657 blame->f = got_opentemp();
3658 if (blame->f == NULL) {
3659 err = got_error_from_errno("got_opentemp");
3660 goto done;
3662 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3663 &blame->line_offsets, blame->f, blob);
3664 if (err || blame->nlines == 0)
3665 goto done;
3667 /* Don't include \n at EOF in the blame line count. */
3668 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3669 blame->nlines--;
3671 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3672 if (blame->lines == NULL) {
3673 err = got_error_from_errno("calloc");
3674 goto done;
3677 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3678 if (err)
3679 goto done;
3681 blame->cb_args.view = view;
3682 blame->cb_args.lines = blame->lines;
3683 blame->cb_args.nlines = blame->nlines;
3684 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3685 if (blame->cb_args.commit_id == NULL) {
3686 err = got_error_from_errno("got_object_id_dup");
3687 goto done;
3689 blame->cb_args.quit = done;
3691 blame->thread_args.path = path;
3692 blame->thread_args.repo = thread_repo;
3693 blame->thread_args.cb_args = &blame->cb_args;
3694 blame->thread_args.complete = blame_complete;
3695 blame->thread_args.cancel_cb = cancel_blame_view;
3696 blame->thread_args.cancel_arg = done;
3697 *blame_complete = 0;
3699 done:
3700 if (blob)
3701 got_object_blob_close(blob);
3702 free(obj_id);
3703 if (err)
3704 stop_blame(blame);
3705 return err;
3708 static const struct got_error *
3709 open_blame_view(struct tog_view *view, char *path,
3710 struct got_object_id *commit_id, struct got_reflist_head *refs,
3711 struct got_repository *repo)
3713 const struct got_error *err = NULL;
3714 struct tog_blame_view_state *s = &view->state.blame;
3716 SIMPLEQ_INIT(&s->blamed_commits);
3718 s->path = strdup(path);
3719 if (s->path == NULL)
3720 return got_error_from_errno("strdup");
3722 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3723 if (err) {
3724 free(s->path);
3725 return err;
3728 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3729 s->first_displayed_line = 1;
3730 s->last_displayed_line = view->nlines;
3731 s->selected_line = 1;
3732 s->blame_complete = 0;
3733 s->repo = repo;
3734 s->refs = refs;
3735 s->commit_id = commit_id;
3736 memset(&s->blame, 0, sizeof(s->blame));
3738 SIMPLEQ_INIT(&s->colors);
3739 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3740 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3741 get_color_value("TOG_COLOR_COMMIT"));
3742 if (err)
3743 return err;
3746 view->show = show_blame_view;
3747 view->input = input_blame_view;
3748 view->close = close_blame_view;
3749 view->search_start = search_start_blame_view;
3750 view->search_next = search_next_blame_view;
3752 return run_blame(&s->blame, view, &s->blame_complete,
3753 &s->first_displayed_line, &s->last_displayed_line,
3754 &s->selected_line, &s->done, &s->eof, s->path,
3755 s->blamed_commit->id, s->repo);
3758 static const struct got_error *
3759 close_blame_view(struct tog_view *view)
3761 const struct got_error *err = NULL;
3762 struct tog_blame_view_state *s = &view->state.blame;
3764 if (s->blame.thread)
3765 err = stop_blame(&s->blame);
3767 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3768 struct got_object_qid *blamed_commit;
3769 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3770 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3771 got_object_qid_free(blamed_commit);
3774 free(s->path);
3775 free_colors(&s->colors);
3777 return err;
3780 static const struct got_error *
3781 search_start_blame_view(struct tog_view *view)
3783 struct tog_blame_view_state *s = &view->state.blame;
3785 s->matched_line = 0;
3786 return NULL;
3789 static const struct got_error *
3790 search_next_blame_view(struct tog_view *view)
3792 struct tog_blame_view_state *s = &view->state.blame;
3793 int lineno;
3795 if (!view->searching) {
3796 view->search_next_done = 1;
3797 return NULL;
3800 if (s->matched_line) {
3801 if (view->searching == TOG_SEARCH_FORWARD)
3802 lineno = s->matched_line + 1;
3803 else
3804 lineno = s->matched_line - 1;
3805 } else {
3806 if (view->searching == TOG_SEARCH_FORWARD)
3807 lineno = 1;
3808 else
3809 lineno = s->blame.nlines;
3812 while (1) {
3813 char *line = NULL;
3814 off_t offset;
3815 size_t len;
3817 if (lineno <= 0 || lineno > s->blame.nlines) {
3818 if (s->matched_line == 0) {
3819 view->search_next_done = 1;
3820 free(line);
3821 break;
3824 if (view->searching == TOG_SEARCH_FORWARD)
3825 lineno = 1;
3826 else
3827 lineno = s->blame.nlines;
3830 offset = s->blame.line_offsets[lineno - 1];
3831 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3832 free(line);
3833 return got_error_from_errno("fseeko");
3835 free(line);
3836 line = parse_next_line(s->blame.f, &len);
3837 if (line && match_line(line, &view->regex)) {
3838 view->search_next_done = 1;
3839 s->matched_line = lineno;
3840 free(line);
3841 break;
3843 free(line);
3844 if (view->searching == TOG_SEARCH_FORWARD)
3845 lineno++;
3846 else
3847 lineno--;
3850 if (s->matched_line) {
3851 s->first_displayed_line = s->matched_line;
3852 s->selected_line = 1;
3855 return NULL;
3858 static const struct got_error *
3859 show_blame_view(struct tog_view *view)
3861 const struct got_error *err = NULL;
3862 struct tog_blame_view_state *s = &view->state.blame;
3863 int errcode;
3865 if (s->blame.thread == NULL) {
3866 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3867 &s->blame.thread_args);
3868 if (errcode)
3869 return got_error_set_errno(errcode, "pthread_create");
3871 halfdelay(1); /* fast refresh while annotating */
3874 if (s->blame_complete)
3875 halfdelay(10); /* disable fast refresh */
3877 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3878 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3879 s->selected_line, &s->first_displayed_line,
3880 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3882 view_vborder(view);
3883 return err;
3886 static const struct got_error *
3887 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3888 struct tog_view **focus_view, struct tog_view *view, int ch)
3890 const struct got_error *err = NULL, *thread_err = NULL;
3891 struct tog_view *diff_view;
3892 struct tog_blame_view_state *s = &view->state.blame;
3893 int begin_x = 0;
3895 switch (ch) {
3896 case 'q':
3897 s->done = 1;
3898 break;
3899 case 'k':
3900 case KEY_UP:
3901 if (s->selected_line > 1)
3902 s->selected_line--;
3903 else if (s->selected_line == 1 &&
3904 s->first_displayed_line > 1)
3905 s->first_displayed_line--;
3906 break;
3907 case KEY_PPAGE:
3908 if (s->first_displayed_line == 1) {
3909 s->selected_line = 1;
3910 break;
3912 if (s->first_displayed_line > view->nlines - 2)
3913 s->first_displayed_line -=
3914 (view->nlines - 2);
3915 else
3916 s->first_displayed_line = 1;
3917 break;
3918 case 'j':
3919 case KEY_DOWN:
3920 if (s->selected_line < view->nlines - 2 &&
3921 s->first_displayed_line +
3922 s->selected_line <= s->blame.nlines)
3923 s->selected_line++;
3924 else if (s->last_displayed_line <
3925 s->blame.nlines)
3926 s->first_displayed_line++;
3927 break;
3928 case 'b':
3929 case 'p': {
3930 struct got_object_id *id = NULL;
3931 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3932 s->first_displayed_line, s->selected_line);
3933 if (id == NULL)
3934 break;
3935 if (ch == 'p') {
3936 struct got_commit_object *commit;
3937 struct got_object_qid *pid;
3938 struct got_object_id *blob_id = NULL;
3939 int obj_type;
3940 err = got_object_open_as_commit(&commit,
3941 s->repo, id);
3942 if (err)
3943 break;
3944 pid = SIMPLEQ_FIRST(
3945 got_object_commit_get_parent_ids(commit));
3946 if (pid == NULL) {
3947 got_object_commit_close(commit);
3948 break;
3950 /* Check if path history ends here. */
3951 err = got_object_id_by_path(&blob_id, s->repo,
3952 pid->id, s->path);
3953 if (err) {
3954 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3955 err = NULL;
3956 got_object_commit_close(commit);
3957 break;
3959 err = got_object_get_type(&obj_type, s->repo,
3960 blob_id);
3961 free(blob_id);
3962 /* Can't blame non-blob type objects. */
3963 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3964 got_object_commit_close(commit);
3965 break;
3967 err = got_object_qid_alloc(&s->blamed_commit,
3968 pid->id);
3969 got_object_commit_close(commit);
3970 } else {
3971 if (got_object_id_cmp(id,
3972 s->blamed_commit->id) == 0)
3973 break;
3974 err = got_object_qid_alloc(&s->blamed_commit,
3975 id);
3977 if (err)
3978 break;
3979 s->done = 1;
3980 thread_err = stop_blame(&s->blame);
3981 s->done = 0;
3982 if (thread_err)
3983 break;
3984 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3985 s->blamed_commit, entry);
3986 err = run_blame(&s->blame, view, &s->blame_complete,
3987 &s->first_displayed_line, &s->last_displayed_line,
3988 &s->selected_line, &s->done, &s->eof,
3989 s->path, s->blamed_commit->id, s->repo);
3990 if (err)
3991 break;
3992 break;
3994 case 'B': {
3995 struct got_object_qid *first;
3996 first = SIMPLEQ_FIRST(&s->blamed_commits);
3997 if (!got_object_id_cmp(first->id, s->commit_id))
3998 break;
3999 s->done = 1;
4000 thread_err = stop_blame(&s->blame);
4001 s->done = 0;
4002 if (thread_err)
4003 break;
4004 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4005 got_object_qid_free(s->blamed_commit);
4006 s->blamed_commit =
4007 SIMPLEQ_FIRST(&s->blamed_commits);
4008 err = run_blame(&s->blame, view, &s->blame_complete,
4009 &s->first_displayed_line, &s->last_displayed_line,
4010 &s->selected_line, &s->done, &s->eof, s->path,
4011 s->blamed_commit->id, s->repo);
4012 if (err)
4013 break;
4014 break;
4016 case KEY_ENTER:
4017 case '\r': {
4018 struct got_object_id *id = NULL;
4019 struct got_object_qid *pid;
4020 struct got_commit_object *commit = NULL;
4021 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4022 s->first_displayed_line, s->selected_line);
4023 if (id == NULL)
4024 break;
4025 err = got_object_open_as_commit(&commit, s->repo, id);
4026 if (err)
4027 break;
4028 pid = SIMPLEQ_FIRST(
4029 got_object_commit_get_parent_ids(commit));
4030 if (view_is_parent_view(view))
4031 begin_x = view_split_begin_x(view->begin_x);
4032 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4033 if (diff_view == NULL) {
4034 got_object_commit_close(commit);
4035 err = got_error_from_errno("view_open");
4036 break;
4038 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4039 id, NULL, s->refs, s->repo);
4040 got_object_commit_close(commit);
4041 if (err) {
4042 view_close(diff_view);
4043 break;
4045 if (view_is_parent_view(view)) {
4046 err = view_close_child(view);
4047 if (err)
4048 break;
4049 err = view_set_child(view, diff_view);
4050 if (err) {
4051 view_close(diff_view);
4052 break;
4054 *focus_view = diff_view;
4055 view->child_focussed = 1;
4056 } else
4057 *new_view = diff_view;
4058 if (err)
4059 break;
4060 break;
4062 case KEY_NPAGE:
4063 case ' ':
4064 if (s->last_displayed_line >= s->blame.nlines &&
4065 s->selected_line >= MIN(s->blame.nlines,
4066 view->nlines - 2)) {
4067 break;
4069 if (s->last_displayed_line >= s->blame.nlines &&
4070 s->selected_line < view->nlines - 2) {
4071 s->selected_line = MIN(s->blame.nlines,
4072 view->nlines - 2);
4073 break;
4075 if (s->last_displayed_line + view->nlines - 2
4076 <= s->blame.nlines)
4077 s->first_displayed_line +=
4078 view->nlines - 2;
4079 else
4080 s->first_displayed_line =
4081 s->blame.nlines -
4082 (view->nlines - 3);
4083 break;
4084 case KEY_RESIZE:
4085 if (s->selected_line > view->nlines - 2) {
4086 s->selected_line = MIN(s->blame.nlines,
4087 view->nlines - 2);
4089 break;
4090 default:
4091 break;
4093 return thread_err ? thread_err : err;
4096 static const struct got_error *
4097 cmd_blame(int argc, char *argv[])
4099 const struct got_error *error;
4100 struct got_repository *repo = NULL;
4101 struct got_reflist_head refs;
4102 struct got_worktree *worktree = NULL;
4103 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4104 struct got_object_id *commit_id = NULL;
4105 char *commit_id_str = NULL;
4106 int ch;
4107 struct tog_view *view;
4109 SIMPLEQ_INIT(&refs);
4111 #ifndef PROFILE
4112 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4113 NULL) == -1)
4114 err(1, "pledge");
4115 #endif
4117 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4118 switch (ch) {
4119 case 'c':
4120 commit_id_str = optarg;
4121 break;
4122 case 'r':
4123 repo_path = realpath(optarg, NULL);
4124 if (repo_path == NULL)
4125 return got_error_from_errno2("realpath",
4126 optarg);
4127 break;
4128 default:
4129 usage_blame();
4130 /* NOTREACHED */
4134 argc -= optind;
4135 argv += optind;
4137 if (argc == 1)
4138 path = argv[0];
4139 else
4140 usage_blame();
4142 cwd = getcwd(NULL, 0);
4143 if (cwd == NULL) {
4144 error = got_error_from_errno("getcwd");
4145 goto done;
4147 if (repo_path == NULL) {
4148 error = got_worktree_open(&worktree, cwd);
4149 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4150 goto done;
4151 else
4152 error = NULL;
4153 if (worktree) {
4154 repo_path =
4155 strdup(got_worktree_get_repo_path(worktree));
4156 if (repo_path == NULL)
4157 error = got_error_from_errno("strdup");
4158 if (error)
4159 goto done;
4160 } else {
4161 repo_path = strdup(cwd);
4162 if (repo_path == NULL) {
4163 error = got_error_from_errno("strdup");
4164 goto done;
4169 init_curses();
4171 error = got_repo_open(&repo, repo_path, NULL);
4172 if (error != NULL)
4173 goto done;
4175 error = apply_unveil(got_repo_get_path(repo), NULL);
4176 if (error)
4177 goto done;
4179 if (worktree) {
4180 const char *prefix = got_worktree_get_path_prefix(worktree);
4181 char *p, *worktree_subdir = cwd +
4182 strlen(got_worktree_get_root_path(worktree));
4183 if (asprintf(&p, "%s%s%s%s%s",
4184 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4185 worktree_subdir, worktree_subdir[0] ? "/" : "",
4186 path) == -1) {
4187 error = got_error_from_errno("asprintf");
4188 goto done;
4190 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4191 free(p);
4192 } else {
4193 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4195 if (error)
4196 goto done;
4198 if (commit_id_str == NULL) {
4199 struct got_reference *head_ref;
4200 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4201 if (error != NULL)
4202 goto done;
4203 error = got_ref_resolve(&commit_id, repo, head_ref);
4204 got_ref_close(head_ref);
4205 } else {
4206 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4207 if (error) {
4208 if (error->code != GOT_ERR_NOT_REF)
4209 goto done;
4210 error = got_repo_match_object_id_prefix(&commit_id,
4211 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4214 if (error != NULL)
4215 goto done;
4217 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4218 if (error)
4219 goto done;
4221 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4222 if (view == NULL) {
4223 error = got_error_from_errno("view_open");
4224 goto done;
4226 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4227 if (error)
4228 goto done;
4229 if (worktree) {
4230 /* Release work tree lock. */
4231 got_worktree_close(worktree);
4232 worktree = NULL;
4234 error = view_loop(view);
4235 done:
4236 free(repo_path);
4237 free(cwd);
4238 free(commit_id);
4239 if (worktree)
4240 got_worktree_close(worktree);
4241 if (repo)
4242 got_repo_close(repo);
4243 got_ref_list_free(&refs);
4244 return error;
4247 static const struct got_error *
4248 draw_tree_entries(struct tog_view *view,
4249 struct got_tree_entry **first_displayed_entry,
4250 struct got_tree_entry **last_displayed_entry,
4251 struct got_tree_entry **selected_entry, int *ndisplayed,
4252 const char *label, int show_ids, const char *parent_path,
4253 struct got_tree_object *tree, int selected, int limit,
4254 int isroot, struct tog_colors *colors)
4256 const struct got_error *err = NULL;
4257 struct got_tree_entry *te;
4258 wchar_t *wline;
4259 struct tog_color *tc;
4260 int width, n, i, nentries;
4262 *ndisplayed = 0;
4264 werase(view->window);
4266 if (limit == 0)
4267 return NULL;
4269 err = format_line(&wline, &width, label, view->ncols, 0);
4270 if (err)
4271 return err;
4272 if (view_needs_focus_indication(view))
4273 wstandout(view->window);
4274 tc = get_color(colors, TOG_COLOR_COMMIT);
4275 if (tc)
4276 wattr_on(view->window,
4277 COLOR_PAIR(tc->colorpair), NULL);
4278 waddwstr(view->window, wline);
4279 if (tc)
4280 wattr_off(view->window,
4281 COLOR_PAIR(tc->colorpair), NULL);
4282 if (view_needs_focus_indication(view))
4283 wstandend(view->window);
4284 free(wline);
4285 wline = NULL;
4286 if (width < view->ncols - 1)
4287 waddch(view->window, '\n');
4288 if (--limit <= 0)
4289 return NULL;
4290 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4291 if (err)
4292 return err;
4293 waddwstr(view->window, wline);
4294 free(wline);
4295 wline = NULL;
4296 if (width < view->ncols - 1)
4297 waddch(view->window, '\n');
4298 if (--limit <= 0)
4299 return NULL;
4300 waddch(view->window, '\n');
4301 if (--limit <= 0)
4302 return NULL;
4304 if (*first_displayed_entry == NULL) {
4305 te = got_object_tree_get_first_entry(tree);
4306 if (selected == 0) {
4307 if (view->focussed)
4308 wstandout(view->window);
4309 *selected_entry = NULL;
4311 waddstr(view->window, " ..\n"); /* parent directory */
4312 if (selected == 0 && view->focussed)
4313 wstandend(view->window);
4314 (*ndisplayed)++;
4315 if (--limit <= 0)
4316 return NULL;
4317 n = 1;
4318 } else {
4319 n = 0;
4320 te = *first_displayed_entry;
4323 nentries = got_object_tree_get_nentries(tree);
4324 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4325 char *line = NULL, *id_str = NULL;
4326 const char *modestr = "";
4327 mode_t mode;
4329 te = got_object_tree_get_entry(tree, i);
4330 mode = got_tree_entry_get_mode(te);
4332 if (show_ids) {
4333 err = got_object_id_str(&id_str,
4334 got_tree_entry_get_id(te));
4335 if (err)
4336 return got_error_from_errno(
4337 "got_object_id_str");
4339 if (got_object_tree_entry_is_submodule(te))
4340 modestr = "$";
4341 else if (S_ISLNK(mode))
4342 modestr = "@";
4343 else if (S_ISDIR(mode))
4344 modestr = "/";
4345 else if (mode & S_IXUSR)
4346 modestr = "*";
4347 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4348 got_tree_entry_get_name(te), modestr) == -1) {
4349 free(id_str);
4350 return got_error_from_errno("asprintf");
4352 free(id_str);
4353 err = format_line(&wline, &width, line, view->ncols, 0);
4354 if (err) {
4355 free(line);
4356 break;
4358 if (n == selected) {
4359 if (view->focussed)
4360 wstandout(view->window);
4361 *selected_entry = te;
4363 tc = match_color(colors, line);
4364 if (tc)
4365 wattr_on(view->window,
4366 COLOR_PAIR(tc->colorpair), NULL);
4367 waddwstr(view->window, wline);
4368 if (tc)
4369 wattr_off(view->window,
4370 COLOR_PAIR(tc->colorpair), NULL);
4371 if (width < view->ncols - 1)
4372 waddch(view->window, '\n');
4373 if (n == selected && view->focussed)
4374 wstandend(view->window);
4375 free(line);
4376 free(wline);
4377 wline = NULL;
4378 n++;
4379 (*ndisplayed)++;
4380 *last_displayed_entry = te;
4381 if (--limit <= 0)
4382 break;
4385 return err;
4388 static void
4389 tree_scroll_up(struct tog_view *view,
4390 struct got_tree_entry **first_displayed_entry, int maxscroll,
4391 struct got_tree_object *tree, int isroot)
4393 struct got_tree_entry *te;
4394 int i;
4396 if (*first_displayed_entry == NULL)
4397 return;
4399 te = got_object_tree_get_entry(tree, 0);
4400 if (*first_displayed_entry == te) {
4401 if (!isroot)
4402 *first_displayed_entry = NULL;
4403 return;
4406 i = 0;
4407 while (*first_displayed_entry && i < maxscroll) {
4408 *first_displayed_entry = got_tree_entry_get_prev(tree,
4409 *first_displayed_entry);
4410 i++;
4412 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4413 *first_displayed_entry = NULL;
4416 static int
4417 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4418 struct got_tree_entry *last_displayed_entry,
4419 struct got_tree_object *tree)
4421 struct got_tree_entry *next, *last;
4422 int n = 0;
4424 if (*first_displayed_entry)
4425 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4426 else
4427 next = got_object_tree_get_first_entry(tree);
4429 last = last_displayed_entry;
4430 while (next && last && n++ < maxscroll) {
4431 last = got_tree_entry_get_next(tree, last);
4432 if (last) {
4433 *first_displayed_entry = next;
4434 next = got_tree_entry_get_next(tree, next);
4437 return n;
4440 static const struct got_error *
4441 tree_entry_path(char **path, struct tog_parent_trees *parents,
4442 struct got_tree_entry *te)
4444 const struct got_error *err = NULL;
4445 struct tog_parent_tree *pt;
4446 size_t len = 2; /* for leading slash and NUL */
4448 TAILQ_FOREACH(pt, parents, entry)
4449 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4450 + 1 /* slash */;
4451 if (te)
4452 len += strlen(got_tree_entry_get_name(te));
4454 *path = calloc(1, len);
4455 if (path == NULL)
4456 return got_error_from_errno("calloc");
4458 (*path)[0] = '/';
4459 pt = TAILQ_LAST(parents, tog_parent_trees);
4460 while (pt) {
4461 const char *name = got_tree_entry_get_name(pt->selected_entry);
4462 if (strlcat(*path, name, len) >= len) {
4463 err = got_error(GOT_ERR_NO_SPACE);
4464 goto done;
4466 if (strlcat(*path, "/", len) >= len) {
4467 err = got_error(GOT_ERR_NO_SPACE);
4468 goto done;
4470 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4472 if (te) {
4473 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4474 err = got_error(GOT_ERR_NO_SPACE);
4475 goto done;
4478 done:
4479 if (err) {
4480 free(*path);
4481 *path = NULL;
4483 return err;
4486 static const struct got_error *
4487 blame_tree_entry(struct tog_view **new_view, int begin_x,
4488 struct got_tree_entry *te, struct tog_parent_trees *parents,
4489 struct got_object_id *commit_id, struct got_reflist_head *refs,
4490 struct got_repository *repo)
4492 const struct got_error *err = NULL;
4493 char *path;
4494 struct tog_view *blame_view;
4496 *new_view = NULL;
4498 err = tree_entry_path(&path, parents, te);
4499 if (err)
4500 return err;
4502 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4503 if (blame_view == NULL) {
4504 err = got_error_from_errno("view_open");
4505 goto done;
4508 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4509 if (err) {
4510 if (err->code == GOT_ERR_CANCELLED)
4511 err = NULL;
4512 view_close(blame_view);
4513 } else
4514 *new_view = blame_view;
4515 done:
4516 free(path);
4517 return err;
4520 static const struct got_error *
4521 log_tree_entry(struct tog_view **new_view, int begin_x,
4522 struct got_tree_entry *te, struct tog_parent_trees *parents,
4523 struct got_object_id *commit_id, struct got_reflist_head *refs,
4524 struct got_repository *repo)
4526 struct tog_view *log_view;
4527 const struct got_error *err = NULL;
4528 char *path;
4530 *new_view = NULL;
4532 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4533 if (log_view == NULL)
4534 return got_error_from_errno("view_open");
4536 err = tree_entry_path(&path, parents, te);
4537 if (err)
4538 return err;
4540 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4541 if (err)
4542 view_close(log_view);
4543 else
4544 *new_view = log_view;
4545 free(path);
4546 return err;
4549 static const struct got_error *
4550 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4551 struct got_object_id *commit_id, struct got_reflist_head *refs,
4552 struct got_repository *repo)
4554 const struct got_error *err = NULL;
4555 char *commit_id_str = NULL;
4556 struct tog_tree_view_state *s = &view->state.tree;
4558 TAILQ_INIT(&s->parents);
4560 err = got_object_id_str(&commit_id_str, commit_id);
4561 if (err != NULL)
4562 goto done;
4564 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4565 err = got_error_from_errno("asprintf");
4566 goto done;
4569 s->root = s->tree = root;
4570 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4571 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4572 s->commit_id = got_object_id_dup(commit_id);
4573 if (s->commit_id == NULL) {
4574 err = got_error_from_errno("got_object_id_dup");
4575 goto done;
4577 s->refs = refs;
4578 s->repo = repo;
4580 SIMPLEQ_INIT(&s->colors);
4582 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4583 err = add_color(&s->colors, "\\$$",
4584 TOG_COLOR_TREE_SUBMODULE,
4585 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4586 if (err)
4587 goto done;
4588 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4589 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4590 if (err) {
4591 free_colors(&s->colors);
4592 goto done;
4594 err = add_color(&s->colors, "/$",
4595 TOG_COLOR_TREE_DIRECTORY,
4596 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4597 if (err) {
4598 free_colors(&s->colors);
4599 goto done;
4602 err = add_color(&s->colors, "\\*$",
4603 TOG_COLOR_TREE_EXECUTABLE,
4604 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4605 if (err) {
4606 free_colors(&s->colors);
4607 goto done;
4610 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4611 get_color_value("TOG_COLOR_COMMIT"));
4612 if (err) {
4613 free_colors(&s->colors);
4614 goto done;
4618 view->show = show_tree_view;
4619 view->input = input_tree_view;
4620 view->close = close_tree_view;
4621 view->search_start = search_start_tree_view;
4622 view->search_next = search_next_tree_view;
4623 done:
4624 free(commit_id_str);
4625 if (err) {
4626 free(s->tree_label);
4627 s->tree_label = NULL;
4629 return err;
4632 static const struct got_error *
4633 close_tree_view(struct tog_view *view)
4635 struct tog_tree_view_state *s = &view->state.tree;
4637 free_colors(&s->colors);
4638 free(s->tree_label);
4639 s->tree_label = NULL;
4640 free(s->commit_id);
4641 s->commit_id = NULL;
4642 while (!TAILQ_EMPTY(&s->parents)) {
4643 struct tog_parent_tree *parent;
4644 parent = TAILQ_FIRST(&s->parents);
4645 TAILQ_REMOVE(&s->parents, parent, entry);
4646 free(parent);
4649 if (s->tree != s->root)
4650 got_object_tree_close(s->tree);
4651 got_object_tree_close(s->root);
4653 return NULL;
4656 static const struct got_error *
4657 search_start_tree_view(struct tog_view *view)
4659 struct tog_tree_view_state *s = &view->state.tree;
4661 s->matched_entry = NULL;
4662 return NULL;
4665 static int
4666 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4668 regmatch_t regmatch;
4670 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4671 0) == 0;
4674 static const struct got_error *
4675 search_next_tree_view(struct tog_view *view)
4677 struct tog_tree_view_state *s = &view->state.tree;
4678 struct got_tree_entry *te = NULL;
4680 if (!view->searching) {
4681 view->search_next_done = 1;
4682 return NULL;
4685 if (s->matched_entry) {
4686 if (view->searching == TOG_SEARCH_FORWARD) {
4687 if (s->selected_entry)
4688 te = got_tree_entry_get_next(s->tree,
4689 s->selected_entry);
4690 else
4691 te = got_object_tree_get_first_entry(s->tree);
4692 } else {
4693 if (s->selected_entry == NULL)
4694 te = got_object_tree_get_last_entry(s->tree);
4695 else
4696 te = got_tree_entry_get_prev(s->tree,
4697 s->selected_entry);
4699 } else {
4700 if (view->searching == TOG_SEARCH_FORWARD)
4701 te = got_object_tree_get_first_entry(s->tree);
4702 else
4703 te = got_object_tree_get_last_entry(s->tree);
4706 while (1) {
4707 if (te == NULL) {
4708 if (s->matched_entry == NULL) {
4709 view->search_next_done = 1;
4710 return NULL;
4712 if (view->searching == TOG_SEARCH_FORWARD)
4713 te = got_object_tree_get_first_entry(s->tree);
4714 else
4715 te = got_object_tree_get_last_entry(s->tree);
4718 if (match_tree_entry(te, &view->regex)) {
4719 view->search_next_done = 1;
4720 s->matched_entry = te;
4721 break;
4724 if (view->searching == TOG_SEARCH_FORWARD)
4725 te = got_tree_entry_get_next(s->tree, te);
4726 else
4727 te = got_tree_entry_get_prev(s->tree, te);
4730 if (s->matched_entry) {
4731 s->first_displayed_entry = s->matched_entry;
4732 s->selected = 0;
4735 return NULL;
4738 static const struct got_error *
4739 show_tree_view(struct tog_view *view)
4741 const struct got_error *err = NULL;
4742 struct tog_tree_view_state *s = &view->state.tree;
4743 char *parent_path;
4745 err = tree_entry_path(&parent_path, &s->parents, NULL);
4746 if (err)
4747 return err;
4749 err = draw_tree_entries(view, &s->first_displayed_entry,
4750 &s->last_displayed_entry, &s->selected_entry,
4751 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4752 s->tree, s->selected, view->nlines, s->tree == s->root,
4753 &s->colors);
4754 free(parent_path);
4756 view_vborder(view);
4757 return err;
4760 static const struct got_error *
4761 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4762 struct tog_view **focus_view, struct tog_view *view, int ch)
4764 const struct got_error *err = NULL;
4765 struct tog_tree_view_state *s = &view->state.tree;
4766 struct tog_view *log_view;
4767 int begin_x = 0, nscrolled;
4769 switch (ch) {
4770 case 'i':
4771 s->show_ids = !s->show_ids;
4772 break;
4773 case 'l':
4774 if (!s->selected_entry)
4775 break;
4776 if (view_is_parent_view(view))
4777 begin_x = view_split_begin_x(view->begin_x);
4778 err = log_tree_entry(&log_view, begin_x,
4779 s->selected_entry, &s->parents,
4780 s->commit_id, s->refs, s->repo);
4781 if (view_is_parent_view(view)) {
4782 err = view_close_child(view);
4783 if (err)
4784 return err;
4785 err = view_set_child(view, log_view);
4786 if (err) {
4787 view_close(log_view);
4788 break;
4790 *focus_view = log_view;
4791 view->child_focussed = 1;
4792 } else
4793 *new_view = log_view;
4794 break;
4795 case 'k':
4796 case KEY_UP:
4797 if (s->selected > 0) {
4798 s->selected--;
4799 if (s->selected == 0)
4800 break;
4802 if (s->selected > 0)
4803 break;
4804 tree_scroll_up(view, &s->first_displayed_entry, 1,
4805 s->tree, s->tree == s->root);
4806 break;
4807 case KEY_PPAGE:
4808 tree_scroll_up(view, &s->first_displayed_entry,
4809 MAX(0, view->nlines - 4 - s->selected), s->tree,
4810 s->tree == s->root);
4811 s->selected = 0;
4812 if (got_object_tree_get_first_entry(s->tree) ==
4813 s->first_displayed_entry && s->tree != s->root)
4814 s->first_displayed_entry = NULL;
4815 break;
4816 case 'j':
4817 case KEY_DOWN:
4818 if (s->selected < s->ndisplayed - 1) {
4819 s->selected++;
4820 break;
4822 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4823 == NULL)
4824 /* can't scroll any further */
4825 break;
4826 tree_scroll_down(&s->first_displayed_entry, 1,
4827 s->last_displayed_entry, s->tree);
4828 break;
4829 case KEY_NPAGE:
4830 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4831 == NULL) {
4832 /* can't scroll any further; move cursor down */
4833 if (s->selected < s->ndisplayed - 1)
4834 s->selected = s->ndisplayed - 1;
4835 break;
4837 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4838 view->nlines, s->last_displayed_entry, s->tree);
4839 if (nscrolled < view->nlines) {
4840 int ndisplayed = 0;
4841 struct got_tree_entry *te;
4842 te = s->first_displayed_entry;
4843 do {
4844 ndisplayed++;
4845 te = got_tree_entry_get_next(s->tree, te);
4846 } while (te);
4847 s->selected = ndisplayed - 1;
4849 break;
4850 case KEY_ENTER:
4851 case '\r':
4852 case KEY_BACKSPACE:
4853 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4854 struct tog_parent_tree *parent;
4855 /* user selected '..' */
4856 if (s->tree == s->root)
4857 break;
4858 parent = TAILQ_FIRST(&s->parents);
4859 TAILQ_REMOVE(&s->parents, parent,
4860 entry);
4861 got_object_tree_close(s->tree);
4862 s->tree = parent->tree;
4863 s->first_displayed_entry =
4864 parent->first_displayed_entry;
4865 s->selected_entry =
4866 parent->selected_entry;
4867 s->selected = parent->selected;
4868 free(parent);
4869 } else if (S_ISDIR(got_tree_entry_get_mode(
4870 s->selected_entry))) {
4871 struct got_tree_object *subtree;
4872 err = got_object_open_as_tree(&subtree, s->repo,
4873 got_tree_entry_get_id(s->selected_entry));
4874 if (err)
4875 break;
4876 err = tree_view_visit_subtree(subtree, s);
4877 if (err) {
4878 got_object_tree_close(subtree);
4879 break;
4881 } else if (S_ISREG(got_tree_entry_get_mode(
4882 s->selected_entry))) {
4883 struct tog_view *blame_view;
4884 int begin_x = view_is_parent_view(view) ?
4885 view_split_begin_x(view->begin_x) : 0;
4887 err = blame_tree_entry(&blame_view, begin_x,
4888 s->selected_entry, &s->parents,
4889 s->commit_id, s->refs, s->repo);
4890 if (err)
4891 break;
4892 if (view_is_parent_view(view)) {
4893 err = view_close_child(view);
4894 if (err)
4895 return err;
4896 err = view_set_child(view, blame_view);
4897 if (err) {
4898 view_close(blame_view);
4899 break;
4901 *focus_view = blame_view;
4902 view->child_focussed = 1;
4903 } else
4904 *new_view = blame_view;
4906 break;
4907 case KEY_RESIZE:
4908 if (s->selected > view->nlines)
4909 s->selected = s->ndisplayed - 1;
4910 break;
4911 default:
4912 break;
4915 return err;
4918 __dead static void
4919 usage_tree(void)
4921 endwin();
4922 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4923 getprogname());
4924 exit(1);
4927 static const struct got_error *
4928 cmd_tree(int argc, char *argv[])
4930 const struct got_error *error;
4931 struct got_repository *repo = NULL;
4932 struct got_reflist_head refs;
4933 char *repo_path = NULL;
4934 struct got_object_id *commit_id = NULL;
4935 char *commit_id_arg = NULL;
4936 struct got_commit_object *commit = NULL;
4937 struct got_tree_object *tree = NULL;
4938 int ch;
4939 struct tog_view *view;
4941 SIMPLEQ_INIT(&refs);
4943 #ifndef PROFILE
4944 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4945 NULL) == -1)
4946 err(1, "pledge");
4947 #endif
4949 while ((ch = getopt(argc, argv, "c:")) != -1) {
4950 switch (ch) {
4951 case 'c':
4952 commit_id_arg = optarg;
4953 break;
4954 default:
4955 usage_tree();
4956 /* NOTREACHED */
4960 argc -= optind;
4961 argv += optind;
4963 if (argc == 0) {
4964 struct got_worktree *worktree;
4965 char *cwd = getcwd(NULL, 0);
4966 if (cwd == NULL)
4967 return got_error_from_errno("getcwd");
4968 error = got_worktree_open(&worktree, cwd);
4969 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4970 goto done;
4971 if (worktree) {
4972 free(cwd);
4973 repo_path =
4974 strdup(got_worktree_get_repo_path(worktree));
4975 got_worktree_close(worktree);
4976 } else
4977 repo_path = cwd;
4978 if (repo_path == NULL) {
4979 error = got_error_from_errno("strdup");
4980 goto done;
4982 } else if (argc == 1) {
4983 repo_path = realpath(argv[0], NULL);
4984 if (repo_path == NULL)
4985 return got_error_from_errno2("realpath", argv[0]);
4986 } else
4987 usage_log();
4989 init_curses();
4991 error = got_repo_open(&repo, repo_path, NULL);
4992 if (error != NULL)
4993 goto done;
4995 error = apply_unveil(got_repo_get_path(repo), NULL);
4996 if (error)
4997 goto done;
4999 if (commit_id_arg == NULL)
5000 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5001 else {
5002 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5003 if (error) {
5004 if (error->code != GOT_ERR_NOT_REF)
5005 goto done;
5006 error = got_repo_match_object_id_prefix(&commit_id,
5007 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5010 if (error != NULL)
5011 goto done;
5013 error = got_object_open_as_commit(&commit, repo, commit_id);
5014 if (error != NULL)
5015 goto done;
5017 error = got_object_open_as_tree(&tree, repo,
5018 got_object_commit_get_tree_id(commit));
5019 if (error != NULL)
5020 goto done;
5022 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5023 if (error)
5024 goto done;
5026 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5027 if (view == NULL) {
5028 error = got_error_from_errno("view_open");
5029 goto done;
5031 error = open_tree_view(view, tree, commit_id, &refs, repo);
5032 if (error)
5033 goto done;
5034 error = view_loop(view);
5035 done:
5036 free(repo_path);
5037 free(commit_id);
5038 if (commit)
5039 got_object_commit_close(commit);
5040 if (tree)
5041 got_object_tree_close(tree);
5042 if (repo)
5043 got_repo_close(repo);
5044 got_ref_list_free(&refs);
5045 return error;
5048 static void
5049 list_commands(void)
5051 int i;
5053 fprintf(stderr, "commands:");
5054 for (i = 0; i < nitems(tog_commands); i++) {
5055 struct tog_cmd *cmd = &tog_commands[i];
5056 fprintf(stderr, " %s", cmd->name);
5058 fputc('\n', stderr);
5061 __dead static void
5062 usage(int hflag)
5064 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5065 getprogname());
5066 if (hflag)
5067 list_commands();
5068 exit(1);
5071 static char **
5072 make_argv(const char *arg0, const char *arg1)
5074 char **argv;
5075 int argc = (arg1 == NULL ? 1 : 2);
5077 argv = calloc(argc, sizeof(char *));
5078 if (argv == NULL)
5079 err(1, "calloc");
5080 argv[0] = strdup(arg0);
5081 if (argv[0] == NULL)
5082 err(1, "strdup");
5083 if (arg1) {
5084 argv[1] = strdup(arg1);
5085 if (argv[1] == NULL)
5086 err(1, "strdup");
5089 return argv;
5092 int
5093 main(int argc, char *argv[])
5095 const struct got_error *error = NULL;
5096 struct tog_cmd *cmd = NULL;
5097 int ch, hflag = 0, Vflag = 0;
5098 char **cmd_argv = NULL;
5100 setlocale(LC_CTYPE, "");
5102 while ((ch = getopt(argc, argv, "hV")) != -1) {
5103 switch (ch) {
5104 case 'h':
5105 hflag = 1;
5106 break;
5107 case 'V':
5108 Vflag = 1;
5109 break;
5110 default:
5111 usage(hflag);
5112 /* NOTREACHED */
5116 argc -= optind;
5117 argv += optind;
5118 optind = 0;
5119 optreset = 1;
5121 if (Vflag) {
5122 got_version_print_str();
5123 return 1;
5126 if (argc == 0) {
5127 if (hflag)
5128 usage(hflag);
5129 /* Build an argument vector which runs a default command. */
5130 cmd = &tog_commands[0];
5131 cmd_argv = make_argv(cmd->name, NULL);
5132 argc = 1;
5133 } else {
5134 int i;
5136 /* Did the user specific a command? */
5137 for (i = 0; i < nitems(tog_commands); i++) {
5138 if (strncmp(tog_commands[i].name, argv[0],
5139 strlen(argv[0])) == 0) {
5140 cmd = &tog_commands[i];
5141 break;
5145 if (cmd == NULL) {
5146 fprintf(stderr, "%s: unknown command '%s'\n",
5147 getprogname(), argv[0]);
5148 list_commands();
5149 return 1;
5153 if (hflag)
5154 cmd->cmd_usage();
5155 else
5156 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5158 endwin();
5159 free(cmd_argv);
5160 if (error && error->code != GOT_ERR_CANCELLED)
5161 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5162 return 0;