Blob


1 /*
2 * Copyright (c) 2018, 2019 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[10]; /* YY-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);
1401 if (err) {
1402 if (err->code != GOT_ERR_ITER_NEED_MORE)
1403 break;
1404 err = got_commit_graph_fetch_commits(graph,
1405 minqueue, repo, NULL, NULL);
1406 if (err)
1407 return err;
1408 continue;
1411 if (id == NULL)
1412 break;
1414 err = got_object_open_as_commit(&commit, repo, id);
1415 if (err)
1416 break;
1417 entry = alloc_commit_queue_entry(commit, id);
1418 if (entry == NULL) {
1419 err = got_error_from_errno("alloc_commit_queue_entry");
1420 break;
1423 errcode = pthread_mutex_lock(&tog_mutex);
1424 if (errcode) {
1425 err = got_error_set_errno(errcode,
1426 "pthread_mutex_lock");
1427 break;
1430 entry->idx = commits->ncommits;
1431 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1432 nqueued++;
1433 commits->ncommits++;
1435 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1436 err = match_commit(&have_match, id, commit, regex);
1437 if (err) {
1438 pthread_mutex_lock(&tog_mutex);
1439 break;
1443 errcode = pthread_mutex_unlock(&tog_mutex);
1444 if (errcode && err == NULL)
1445 err = got_error_set_errno(errcode,
1446 "pthread_mutex_unlock");
1448 if (have_match)
1449 break;
1452 return err;
1455 static const struct got_error *
1456 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1457 struct got_repository *repo)
1459 const struct got_error *err = NULL;
1460 struct got_reference *head_ref;
1462 *head_id = NULL;
1464 err = got_ref_open(&head_ref, repo, branch_name, 0);
1465 if (err)
1466 return err;
1468 err = got_ref_resolve(head_id, repo, head_ref);
1469 got_ref_close(head_ref);
1470 if (err) {
1471 *head_id = NULL;
1472 return err;
1475 return NULL;
1478 static const struct got_error *
1479 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1480 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1481 struct commit_queue *commits, int selected_idx, int limit,
1482 struct got_reflist_head *refs, const char *path, int commits_needed,
1483 struct tog_colors *colors)
1485 const struct got_error *err = NULL;
1486 struct tog_log_view_state *s = &view->state.log;
1487 struct commit_queue_entry *entry;
1488 int width;
1489 int ncommits, author_cols = 4;
1490 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1491 char *refs_str = NULL;
1492 wchar_t *wline;
1493 struct tog_color *tc;
1494 static const size_t date_display_cols = 9;
1496 entry = first;
1497 ncommits = 0;
1498 while (entry) {
1499 if (ncommits == selected_idx) {
1500 *selected = entry;
1501 break;
1503 entry = TAILQ_NEXT(entry, entry);
1504 ncommits++;
1507 if (*selected && !(view->searching && view->search_next_done == 0)) {
1508 err = got_object_id_str(&id_str, (*selected)->id);
1509 if (err)
1510 return err;
1511 if (refs) {
1512 err = build_refs_str(&refs_str, refs, (*selected)->id,
1513 s->repo);
1514 if (err)
1515 goto done;
1519 if (commits_needed == 0)
1520 halfdelay(10); /* disable fast refresh */
1522 if (asprintf(&ncommits_str, " [%d/%d] %s",
1523 entry ? entry->idx + 1 : 0, commits->ncommits,
1524 commits_needed > 0 ?
1525 (view->searching && view->search_next_done == 0
1526 ? "searching..." : "loading... ") :
1527 (refs_str ? refs_str : "")) == -1) {
1528 err = got_error_from_errno("asprintf");
1529 goto done;
1532 if (path && strcmp(path, "/") != 0) {
1533 if (asprintf(&header, "commit %s %s%s",
1534 id_str ? id_str : "........................................",
1535 path, ncommits_str) == -1) {
1536 err = got_error_from_errno("asprintf");
1537 header = NULL;
1538 goto done;
1540 } else if (asprintf(&header, "commit %s%s",
1541 id_str ? id_str : "........................................",
1542 ncommits_str) == -1) {
1543 err = got_error_from_errno("asprintf");
1544 header = NULL;
1545 goto done;
1547 err = format_line(&wline, &width, header, view->ncols, 0);
1548 if (err)
1549 goto done;
1551 werase(view->window);
1553 if (view_needs_focus_indication(view))
1554 wstandout(view->window);
1555 tc = get_color(colors, TOG_COLOR_COMMIT);
1556 if (tc)
1557 wattr_on(view->window,
1558 COLOR_PAIR(tc->colorpair), NULL);
1559 waddwstr(view->window, wline);
1560 if (tc)
1561 wattr_off(view->window,
1562 COLOR_PAIR(tc->colorpair), NULL);
1563 while (width < view->ncols) {
1564 waddch(view->window, ' ');
1565 width++;
1567 if (view_needs_focus_indication(view))
1568 wstandend(view->window);
1569 free(wline);
1570 if (limit <= 1)
1571 goto done;
1573 /* Grow author column size if necessary. */
1574 entry = first;
1575 ncommits = 0;
1576 while (entry) {
1577 char *author;
1578 wchar_t *wauthor;
1579 int width;
1580 if (ncommits >= limit - 1)
1581 break;
1582 author = strdup(got_object_commit_get_author(entry->commit));
1583 if (author == NULL) {
1584 err = got_error_from_errno("strdup");
1585 goto done;
1587 err = format_author(&wauthor, &width, author, COLS,
1588 date_display_cols);
1589 if (author_cols < width)
1590 author_cols = width;
1591 free(wauthor);
1592 free(author);
1593 ncommits++;
1594 entry = TAILQ_NEXT(entry, entry);
1597 entry = first;
1598 *last = first;
1599 ncommits = 0;
1600 while (entry) {
1601 if (ncommits >= limit - 1)
1602 break;
1603 if (ncommits == selected_idx)
1604 wstandout(view->window);
1605 err = draw_commit(view, entry->commit, entry->id, refs,
1606 date_display_cols, author_cols, colors);
1607 if (ncommits == selected_idx)
1608 wstandend(view->window);
1609 if (err)
1610 goto done;
1611 ncommits++;
1612 *last = entry;
1613 entry = TAILQ_NEXT(entry, entry);
1616 view_vborder(view);
1617 done:
1618 free(id_str);
1619 free(refs_str);
1620 free(ncommits_str);
1621 free(header);
1622 return err;
1625 static void
1626 scroll_up(struct tog_view *view,
1627 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1628 struct commit_queue *commits)
1630 struct commit_queue_entry *entry;
1631 int nscrolled = 0;
1633 entry = TAILQ_FIRST(&commits->head);
1634 if (*first_displayed_entry == entry)
1635 return;
1637 entry = *first_displayed_entry;
1638 while (entry && nscrolled < maxscroll) {
1639 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1640 if (entry) {
1641 *first_displayed_entry = entry;
1642 nscrolled++;
1647 static const struct got_error *
1648 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1649 pthread_cond_t *need_commits)
1651 int errcode;
1652 int max_wait = 20;
1654 halfdelay(1); /* fast refresh while loading commits */
1656 while (*commits_needed > 0) {
1657 if (*log_complete)
1658 break;
1660 /* Wake the log thread. */
1661 errcode = pthread_cond_signal(need_commits);
1662 if (errcode)
1663 return got_error_set_errno(errcode,
1664 "pthread_cond_signal");
1665 errcode = pthread_mutex_unlock(&tog_mutex);
1666 if (errcode)
1667 return got_error_set_errno(errcode,
1668 "pthread_mutex_unlock");
1669 pthread_yield();
1670 errcode = pthread_mutex_lock(&tog_mutex);
1671 if (errcode)
1672 return got_error_set_errno(errcode,
1673 "pthread_mutex_lock");
1675 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1677 * Thread is not done yet; lose a key press
1678 * and let the user retry... this way the GUI
1679 * remains interactive while logging deep paths
1680 * with few commits in history.
1682 return NULL;
1686 return NULL;
1689 static const struct got_error *
1690 scroll_down(struct tog_view *view,
1691 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1692 struct commit_queue_entry **last_displayed_entry,
1693 struct commit_queue *commits, int *log_complete, int *commits_needed,
1694 pthread_cond_t *need_commits)
1696 const struct got_error *err = NULL;
1697 struct commit_queue_entry *pentry;
1698 int nscrolled = 0;
1700 if (*last_displayed_entry == NULL)
1701 return NULL;
1703 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1704 if (pentry == NULL && !*log_complete) {
1706 * Ask the log thread for required amount of commits
1707 * plus some amount of pre-fetching.
1709 (*commits_needed) += maxscroll + 20;
1710 err = trigger_log_thread(0, commits_needed, log_complete,
1711 need_commits);
1712 if (err)
1713 return err;
1716 do {
1717 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1718 if (pentry == NULL)
1719 break;
1721 *last_displayed_entry = pentry;
1723 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1724 if (pentry == NULL)
1725 break;
1726 *first_displayed_entry = pentry;
1727 } while (++nscrolled < maxscroll);
1729 return err;
1732 static const struct got_error *
1733 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1734 struct got_commit_object *commit, struct got_object_id *commit_id,
1735 struct tog_view *log_view, struct got_reflist_head *refs,
1736 struct got_repository *repo)
1738 const struct got_error *err;
1739 struct got_object_qid *parent_id;
1740 struct tog_view *diff_view;
1742 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1743 if (diff_view == NULL)
1744 return got_error_from_errno("view_open");
1746 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1747 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1748 commit_id, log_view, refs, repo);
1749 if (err == NULL)
1750 *new_view = diff_view;
1751 return err;
1754 static const struct got_error *
1755 tree_view_visit_subtree(struct got_tree_object *subtree,
1756 struct tog_tree_view_state *s)
1758 struct tog_parent_tree *parent;
1760 parent = calloc(1, sizeof(*parent));
1761 if (parent == NULL)
1762 return got_error_from_errno("calloc");
1764 parent->tree = s->tree;
1765 parent->first_displayed_entry = s->first_displayed_entry;
1766 parent->selected_entry = s->selected_entry;
1767 parent->selected = s->selected;
1768 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1769 s->tree = subtree;
1770 s->selected = 0;
1771 s->first_displayed_entry = NULL;
1772 return NULL;
1776 static const struct got_error *
1777 browse_commit_tree(struct tog_view **new_view, int begin_x,
1778 struct commit_queue_entry *entry, const char *path,
1779 struct got_reflist_head *refs, struct got_repository *repo)
1781 const struct got_error *err = NULL;
1782 struct got_tree_object *tree;
1783 struct tog_tree_view_state *s;
1784 struct tog_view *tree_view;
1785 char *slash, *subpath = NULL;
1786 const char *p;
1788 err = got_object_open_as_tree(&tree, repo,
1789 got_object_commit_get_tree_id(entry->commit));
1790 if (err)
1791 return err;
1793 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1794 if (tree_view == NULL)
1795 return got_error_from_errno("view_open");
1797 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1798 if (err) {
1799 got_object_tree_close(tree);
1800 return err;
1802 s = &tree_view->state.tree;
1804 *new_view = tree_view;
1806 /* Walk the path and open corresponding tree objects. */
1807 p = path;
1808 while (p[0] == '/')
1809 p++;
1810 while (*p) {
1811 struct got_tree_entry *te;
1812 struct got_object_id *tree_id;
1813 char *te_name;
1815 /* Ensure the correct subtree entry is selected. */
1816 slash = strchr(p, '/');
1817 if (slash == NULL)
1818 slash = strchr(p, '\0');
1820 te_name = strndup(p, slash -p);
1821 if (te_name == NULL) {
1822 err = got_error_from_errno("strndup");
1823 break;
1825 te = got_object_tree_find_entry(s->tree, te_name);
1826 if (te == NULL) {
1827 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1828 free(te_name);
1829 break;
1831 free(te_name);
1832 s->selected_entry = te;
1833 s->selected = got_tree_entry_get_index(te);
1834 if (s->tree != s->root)
1835 s->selected++; /* skip '..' */
1837 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1838 /* Jump to this file's entry. */
1839 s->first_displayed_entry = s->selected_entry;
1840 s->selected = 0;
1841 break;
1844 slash = strchr(p, '/');
1845 if (slash)
1846 subpath = strndup(path, slash - path);
1847 else
1848 subpath = strdup(path);
1849 if (subpath == NULL) {
1850 err = got_error_from_errno("strdup");
1851 break;
1854 err = got_object_id_by_path(&tree_id, repo, entry->id,
1855 subpath);
1856 if (err)
1857 break;
1859 err = got_object_open_as_tree(&tree, repo, tree_id);
1860 free(tree_id);
1861 if (err)
1862 break;
1864 err = tree_view_visit_subtree(tree, s);
1865 if (err) {
1866 got_object_tree_close(tree);
1867 break;
1869 if (slash == NULL)
1870 break;
1871 free(subpath);
1872 subpath = NULL;
1873 p = slash;
1876 free(subpath);
1877 return err;
1880 static void *
1881 log_thread(void *arg)
1883 const struct got_error *err = NULL;
1884 int errcode = 0;
1885 struct tog_log_thread_args *a = arg;
1886 int done = 0;
1888 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1889 NULL, NULL);
1890 if (err)
1891 return (void *)err;
1893 while (!done && !err && !tog_sigpipe_received) {
1894 err = queue_commits(a->graph, a->commits, 1, a->repo,
1895 a->in_repo_path, a->searching, a->search_next_done,
1896 a->regex);
1897 if (err) {
1898 if (err->code != GOT_ERR_ITER_COMPLETED)
1899 return (void *)err;
1900 err = NULL;
1901 done = 1;
1902 } else if (a->commits_needed > 0)
1903 a->commits_needed--;
1905 errcode = pthread_mutex_lock(&tog_mutex);
1906 if (errcode) {
1907 err = got_error_set_errno(errcode,
1908 "pthread_mutex_lock");
1909 break;
1910 } else if (*a->quit)
1911 done = 1;
1912 else if (*a->first_displayed_entry == NULL) {
1913 *a->first_displayed_entry =
1914 TAILQ_FIRST(&a->commits->head);
1915 *a->selected_entry = *a->first_displayed_entry;
1918 if (done)
1919 a->commits_needed = 0;
1920 else if (a->commits_needed == 0) {
1921 errcode = pthread_cond_wait(&a->need_commits,
1922 &tog_mutex);
1923 if (errcode)
1924 err = got_error_set_errno(errcode,
1925 "pthread_cond_wait");
1928 errcode = pthread_mutex_unlock(&tog_mutex);
1929 if (errcode && err == NULL)
1930 err = got_error_set_errno(errcode,
1931 "pthread_mutex_unlock");
1933 a->log_complete = 1;
1934 return (void *)err;
1937 static const struct got_error *
1938 stop_log_thread(struct tog_log_view_state *s)
1940 const struct got_error *err = NULL;
1941 int errcode;
1943 if (s->thread) {
1944 s->quit = 1;
1945 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1946 if (errcode)
1947 return got_error_set_errno(errcode,
1948 "pthread_cond_signal");
1949 errcode = pthread_mutex_unlock(&tog_mutex);
1950 if (errcode)
1951 return got_error_set_errno(errcode,
1952 "pthread_mutex_unlock");
1953 errcode = pthread_join(s->thread, (void **)&err);
1954 if (errcode)
1955 return got_error_set_errno(errcode, "pthread_join");
1956 errcode = pthread_mutex_lock(&tog_mutex);
1957 if (errcode)
1958 return got_error_set_errno(errcode,
1959 "pthread_mutex_lock");
1960 s->thread = NULL;
1963 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1964 if (errcode && err == NULL)
1965 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1967 if (s->thread_args.repo) {
1968 got_repo_close(s->thread_args.repo);
1969 s->thread_args.repo = NULL;
1972 if (s->thread_args.graph) {
1973 got_commit_graph_close(s->thread_args.graph);
1974 s->thread_args.graph = NULL;
1977 return err;
1980 static const struct got_error *
1981 close_log_view(struct tog_view *view)
1983 const struct got_error *err = NULL;
1984 struct tog_log_view_state *s = &view->state.log;
1986 err = stop_log_thread(s);
1987 free_commits(&s->commits);
1988 free(s->in_repo_path);
1989 s->in_repo_path = NULL;
1990 free(s->start_id);
1991 s->start_id = NULL;
1992 return err;
1995 static const struct got_error *
1996 search_start_log_view(struct tog_view *view)
1998 struct tog_log_view_state *s = &view->state.log;
2000 s->matched_entry = NULL;
2001 s->search_entry = NULL;
2002 return NULL;
2005 static const struct got_error *
2006 search_next_log_view(struct tog_view *view)
2008 const struct got_error *err = NULL;
2009 struct tog_log_view_state *s = &view->state.log;
2010 struct commit_queue_entry *entry;
2012 if (!view->searching) {
2013 view->search_next_done = 1;
2014 return NULL;
2017 if (s->search_entry) {
2018 int errcode, ch;
2019 errcode = pthread_mutex_unlock(&tog_mutex);
2020 if (errcode)
2021 return got_error_set_errno(errcode,
2022 "pthread_mutex_unlock");
2023 ch = wgetch(view->window);
2024 errcode = pthread_mutex_lock(&tog_mutex);
2025 if (errcode)
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_lock");
2028 if (ch == KEY_BACKSPACE) {
2029 view->search_next_done = 1;
2030 return NULL;
2032 if (view->searching == TOG_SEARCH_FORWARD)
2033 entry = TAILQ_NEXT(s->search_entry, entry);
2034 else
2035 entry = TAILQ_PREV(s->search_entry,
2036 commit_queue_head, entry);
2037 } else if (s->matched_entry) {
2038 if (view->searching == TOG_SEARCH_FORWARD)
2039 entry = TAILQ_NEXT(s->selected_entry, entry);
2040 else
2041 entry = TAILQ_PREV(s->selected_entry,
2042 commit_queue_head, entry);
2043 } else {
2044 if (view->searching == TOG_SEARCH_FORWARD)
2045 entry = TAILQ_FIRST(&s->commits.head);
2046 else
2047 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2050 while (1) {
2051 int have_match = 0;
2053 if (entry == NULL) {
2054 if (s->thread_args.log_complete ||
2055 view->searching == TOG_SEARCH_BACKWARD) {
2056 view->search_next_done = 1;
2057 return NULL;
2060 * Poke the log thread for more commits and return,
2061 * allowing the main loop to make progress. Search
2062 * will resume at s->search_entry once we come back.
2064 s->thread_args.commits_needed++;
2065 return trigger_log_thread(1,
2066 &s->thread_args.commits_needed,
2067 &s->thread_args.log_complete,
2068 &s->thread_args.need_commits);
2071 err = match_commit(&have_match, entry->id, entry->commit,
2072 &view->regex);
2073 if (err)
2074 break;
2075 if (have_match) {
2076 view->search_next_done = 1;
2077 s->matched_entry = entry;
2078 break;
2081 s->search_entry = entry;
2082 if (view->searching == TOG_SEARCH_FORWARD)
2083 entry = TAILQ_NEXT(entry, entry);
2084 else
2085 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2088 if (s->matched_entry) {
2089 int cur = s->selected_entry->idx;
2090 while (cur < s->matched_entry->idx) {
2091 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2092 if (err)
2093 return err;
2094 cur++;
2096 while (cur > s->matched_entry->idx) {
2097 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2098 if (err)
2099 return err;
2100 cur--;
2104 s->search_entry = NULL;
2106 return NULL;
2109 static const struct got_error *
2110 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2111 struct got_reflist_head *refs, struct got_repository *repo,
2112 const char *head_ref_name, const char *path, int check_disk)
2114 const struct got_error *err = NULL;
2115 struct tog_log_view_state *s = &view->state.log;
2116 struct got_repository *thread_repo = NULL;
2117 struct got_commit_graph *thread_graph = NULL;
2118 int errcode;
2120 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2121 if (err != NULL)
2122 goto done;
2124 /* The commit queue only contains commits being displayed. */
2125 TAILQ_INIT(&s->commits.head);
2126 s->commits.ncommits = 0;
2128 s->refs = refs;
2129 s->repo = repo;
2130 s->head_ref_name = head_ref_name;
2131 s->start_id = got_object_id_dup(start_id);
2132 if (s->start_id == NULL) {
2133 err = got_error_from_errno("got_object_id_dup");
2134 goto done;
2137 SIMPLEQ_INIT(&s->colors);
2138 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2139 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2140 get_color_value("TOG_COLOR_COMMIT"));
2141 if (err)
2142 goto done;
2143 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2144 get_color_value("TOG_COLOR_AUTHOR"));
2145 if (err) {
2146 free_colors(&s->colors);
2147 goto done;
2149 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2150 get_color_value("TOG_COLOR_DATE"));
2151 if (err) {
2152 free_colors(&s->colors);
2153 goto done;
2157 view->show = show_log_view;
2158 view->input = input_log_view;
2159 view->close = close_log_view;
2160 view->search_start = search_start_log_view;
2161 view->search_next = search_next_log_view;
2163 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2164 if (err)
2165 goto done;
2166 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
2167 0, thread_repo);
2168 if (err)
2169 goto done;
2171 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2172 if (errcode) {
2173 err = got_error_set_errno(errcode, "pthread_cond_init");
2174 goto done;
2177 s->thread_args.commits_needed = view->nlines;
2178 s->thread_args.graph = thread_graph;
2179 s->thread_args.commits = &s->commits;
2180 s->thread_args.in_repo_path = s->in_repo_path;
2181 s->thread_args.start_id = s->start_id;
2182 s->thread_args.repo = thread_repo;
2183 s->thread_args.log_complete = 0;
2184 s->thread_args.quit = &s->quit;
2185 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2186 s->thread_args.selected_entry = &s->selected_entry;
2187 s->thread_args.searching = &view->searching;
2188 s->thread_args.search_next_done = &view->search_next_done;
2189 s->thread_args.regex = &view->regex;
2190 done:
2191 if (err)
2192 close_log_view(view);
2193 return err;
2196 static const struct got_error *
2197 show_log_view(struct tog_view *view)
2199 struct tog_log_view_state *s = &view->state.log;
2201 if (s->thread == NULL) {
2202 int errcode = pthread_create(&s->thread, NULL, log_thread,
2203 &s->thread_args);
2204 if (errcode)
2205 return got_error_set_errno(errcode, "pthread_create");
2208 return draw_commits(view, &s->last_displayed_entry,
2209 &s->selected_entry, s->first_displayed_entry,
2210 &s->commits, s->selected, view->nlines, s->refs,
2211 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2214 static const struct got_error *
2215 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2216 struct tog_view **focus_view, struct tog_view *view, int ch)
2218 const struct got_error *err = NULL;
2219 struct tog_log_view_state *s = &view->state.log;
2220 char *parent_path, *in_repo_path = NULL;
2221 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2222 int begin_x = 0;
2223 struct got_object_id *start_id;
2225 switch (ch) {
2226 case 'q':
2227 s->quit = 1;
2228 break;
2229 case 'k':
2230 case KEY_UP:
2231 case '<':
2232 case ',':
2233 if (s->first_displayed_entry == NULL)
2234 break;
2235 if (s->selected > 0)
2236 s->selected--;
2237 else
2238 scroll_up(view, &s->first_displayed_entry, 1,
2239 &s->commits);
2240 break;
2241 case KEY_PPAGE:
2242 case CTRL('b'):
2243 if (s->first_displayed_entry == NULL)
2244 break;
2245 if (TAILQ_FIRST(&s->commits.head) ==
2246 s->first_displayed_entry) {
2247 s->selected = 0;
2248 break;
2250 scroll_up(view, &s->first_displayed_entry,
2251 view->nlines, &s->commits);
2252 break;
2253 case 'j':
2254 case KEY_DOWN:
2255 case '>':
2256 case '.':
2257 if (s->first_displayed_entry == NULL)
2258 break;
2259 if (s->selected < MIN(view->nlines - 2,
2260 s->commits.ncommits - 1)) {
2261 s->selected++;
2262 break;
2264 err = scroll_down(view, &s->first_displayed_entry, 1,
2265 &s->last_displayed_entry, &s->commits,
2266 &s->thread_args.log_complete,
2267 &s->thread_args.commits_needed,
2268 &s->thread_args.need_commits);
2269 break;
2270 case KEY_NPAGE:
2271 case CTRL('f'): {
2272 struct commit_queue_entry *first;
2273 first = s->first_displayed_entry;
2274 if (first == NULL)
2275 break;
2276 err = scroll_down(view, &s->first_displayed_entry,
2277 view->nlines, &s->last_displayed_entry,
2278 &s->commits, &s->thread_args.log_complete,
2279 &s->thread_args.commits_needed,
2280 &s->thread_args.need_commits);
2281 if (err)
2282 break;
2283 if (first == s->first_displayed_entry &&
2284 s->selected < MIN(view->nlines - 2,
2285 s->commits.ncommits - 1)) {
2286 /* can't scroll further down */
2287 s->selected = MIN(view->nlines - 2,
2288 s->commits.ncommits - 1);
2290 err = NULL;
2291 break;
2293 case KEY_RESIZE:
2294 if (s->selected > view->nlines - 2)
2295 s->selected = view->nlines - 2;
2296 if (s->selected > s->commits.ncommits - 1)
2297 s->selected = s->commits.ncommits - 1;
2298 break;
2299 case KEY_ENTER:
2300 case ' ':
2301 case '\r':
2302 if (s->selected_entry == NULL)
2303 break;
2304 if (view_is_parent_view(view))
2305 begin_x = view_split_begin_x(view->begin_x);
2306 err = open_diff_view_for_commit(&diff_view, begin_x,
2307 s->selected_entry->commit, s->selected_entry->id,
2308 view, s->refs, s->repo);
2309 if (err)
2310 break;
2311 if (view_is_parent_view(view)) {
2312 err = view_close_child(view);
2313 if (err)
2314 return err;
2315 err = view_set_child(view, diff_view);
2316 if (err) {
2317 view_close(diff_view);
2318 break;
2320 *focus_view = diff_view;
2321 view->child_focussed = 1;
2322 } else
2323 *new_view = diff_view;
2324 break;
2325 case 't':
2326 if (s->selected_entry == NULL)
2327 break;
2328 if (view_is_parent_view(view))
2329 begin_x = view_split_begin_x(view->begin_x);
2330 err = browse_commit_tree(&tree_view, begin_x,
2331 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2332 if (err)
2333 break;
2334 if (view_is_parent_view(view)) {
2335 err = view_close_child(view);
2336 if (err)
2337 return err;
2338 err = view_set_child(view, tree_view);
2339 if (err) {
2340 view_close(tree_view);
2341 break;
2343 *focus_view = tree_view;
2344 view->child_focussed = 1;
2345 } else
2346 *new_view = tree_view;
2347 break;
2348 case KEY_BACKSPACE:
2349 if (strcmp(s->in_repo_path, "/") == 0)
2350 break;
2351 parent_path = dirname(s->in_repo_path);
2352 if (parent_path && strcmp(parent_path, ".") != 0) {
2353 err = stop_log_thread(s);
2354 if (err)
2355 return err;
2356 lv = view_open(view->nlines, view->ncols,
2357 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2358 if (lv == NULL)
2359 return got_error_from_errno(
2360 "view_open");
2361 err = open_log_view(lv, s->start_id, s->refs,
2362 s->repo, s->head_ref_name, parent_path, 0);
2363 if (err)
2364 return err;;
2365 if (view_is_parent_view(view))
2366 *new_view = lv;
2367 else {
2368 view_set_child(view->parent, lv);
2369 *focus_view = lv;
2371 return NULL;
2373 break;
2374 case CTRL('l'):
2375 err = stop_log_thread(s);
2376 if (err)
2377 return err;
2378 lv = view_open(view->nlines, view->ncols,
2379 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2380 if (lv == NULL)
2381 return got_error_from_errno("view_open");
2382 err = get_head_commit_id(&start_id, s->head_ref_name ?
2383 s->head_ref_name : GOT_REF_HEAD, s->repo);
2384 if (err) {
2385 view_close(lv);
2386 return err;
2388 in_repo_path = strdup(s->in_repo_path);
2389 if (in_repo_path == NULL) {
2390 free(start_id);
2391 view_close(lv);
2392 return got_error_from_errno("strdup");
2394 got_ref_list_free(s->refs);
2395 err = got_ref_list(s->refs, s->repo, NULL,
2396 got_ref_cmp_by_name, NULL);
2397 if (err) {
2398 free(start_id);
2399 view_close(lv);
2400 return err;
2402 err = open_log_view(lv, start_id, s->refs, s->repo,
2403 s->head_ref_name, in_repo_path, 0);
2404 if (err) {
2405 free(start_id);
2406 view_close(lv);
2407 return err;;
2409 *dead_view = view;
2410 *new_view = lv;
2411 break;
2412 default:
2413 break;
2416 return err;
2419 static const struct got_error *
2420 apply_unveil(const char *repo_path, const char *worktree_path)
2422 const struct got_error *error;
2424 #ifdef PROFILE
2425 if (unveil("gmon.out", "rwc") != 0)
2426 return got_error_from_errno2("unveil", "gmon.out");
2427 #endif
2428 if (repo_path && unveil(repo_path, "r") != 0)
2429 return got_error_from_errno2("unveil", repo_path);
2431 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2432 return got_error_from_errno2("unveil", worktree_path);
2434 if (unveil("/tmp", "rwc") != 0)
2435 return got_error_from_errno2("unveil", "/tmp");
2437 error = got_privsep_unveil_exec_helpers();
2438 if (error != NULL)
2439 return error;
2441 if (unveil(NULL, NULL) != 0)
2442 return got_error_from_errno("unveil");
2444 return NULL;
2447 static void
2448 init_curses(void)
2450 initscr();
2451 cbreak();
2452 halfdelay(1); /* Do fast refresh while initial view is loading. */
2453 noecho();
2454 nonl();
2455 intrflush(stdscr, FALSE);
2456 keypad(stdscr, TRUE);
2457 curs_set(0);
2458 if (getenv("TOG_COLORS") != NULL) {
2459 start_color();
2460 use_default_colors();
2462 signal(SIGWINCH, tog_sigwinch);
2463 signal(SIGPIPE, tog_sigpipe);
2466 static const struct got_error *
2467 cmd_log(int argc, char *argv[])
2469 const struct got_error *error;
2470 struct got_repository *repo = NULL;
2471 struct got_worktree *worktree = NULL;
2472 struct got_reflist_head refs;
2473 struct got_object_id *start_id = NULL;
2474 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2475 char *start_commit = NULL, *head_ref_name = NULL;
2476 int ch;
2477 struct tog_view *view;
2479 SIMPLEQ_INIT(&refs);
2481 #ifndef PROFILE
2482 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2483 NULL) == -1)
2484 err(1, "pledge");
2485 #endif
2487 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2488 switch (ch) {
2489 case 'c':
2490 start_commit = optarg;
2491 break;
2492 case 'r':
2493 repo_path = realpath(optarg, NULL);
2494 if (repo_path == NULL)
2495 return got_error_from_errno2("realpath",
2496 optarg);
2497 break;
2498 default:
2499 usage_log();
2500 /* NOTREACHED */
2504 argc -= optind;
2505 argv += optind;
2507 cwd = getcwd(NULL, 0);
2508 if (cwd == NULL) {
2509 error = got_error_from_errno("getcwd");
2510 goto done;
2512 error = got_worktree_open(&worktree, cwd);
2513 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2514 goto done;
2515 error = NULL;
2517 if (argc == 0) {
2518 path = strdup("");
2519 if (path == NULL) {
2520 error = got_error_from_errno("strdup");
2521 goto done;
2523 } else if (argc == 1) {
2524 if (worktree) {
2525 error = got_worktree_resolve_path(&path, worktree,
2526 argv[0]);
2527 if (error)
2528 goto done;
2529 } else {
2530 path = strdup(argv[0]);
2531 if (path == NULL) {
2532 error = got_error_from_errno("strdup");
2533 goto done;
2536 } else
2537 usage_log();
2539 if (repo_path == NULL) {
2540 if (worktree)
2541 repo_path = strdup(
2542 got_worktree_get_repo_path(worktree));
2543 else
2544 repo_path = strdup(cwd);
2546 if (repo_path == NULL) {
2547 error = got_error_from_errno("strdup");
2548 goto done;
2551 init_curses();
2553 error = got_repo_open(&repo, repo_path, NULL);
2554 if (error != NULL)
2555 goto done;
2557 error = apply_unveil(got_repo_get_path(repo),
2558 worktree ? got_worktree_get_root_path(worktree) : NULL);
2559 if (error)
2560 goto done;
2562 if (start_commit == NULL)
2563 error = get_head_commit_id(&start_id, worktree ?
2564 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2565 repo);
2566 else {
2567 error = get_head_commit_id(&start_id, start_commit, repo);
2568 if (error) {
2569 if (error->code != GOT_ERR_NOT_REF)
2570 goto done;
2571 error = got_repo_match_object_id_prefix(&start_id,
2572 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2575 if (error != NULL)
2576 goto done;
2578 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2579 if (error)
2580 goto done;
2582 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2583 if (view == NULL) {
2584 error = got_error_from_errno("view_open");
2585 goto done;
2587 if (worktree) {
2588 head_ref_name = strdup(
2589 got_worktree_get_head_ref_name(worktree));
2590 if (head_ref_name == NULL) {
2591 error = got_error_from_errno("strdup");
2592 goto done;
2595 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2596 path, 1);
2597 if (error)
2598 goto done;
2599 if (worktree) {
2600 /* Release work tree lock. */
2601 got_worktree_close(worktree);
2602 worktree = NULL;
2604 error = view_loop(view);
2605 done:
2606 free(repo_path);
2607 free(cwd);
2608 free(path);
2609 free(start_id);
2610 free(head_ref_name);
2611 if (repo)
2612 got_repo_close(repo);
2613 if (worktree)
2614 got_worktree_close(worktree);
2615 got_ref_list_free(&refs);
2616 return error;
2619 __dead static void
2620 usage_diff(void)
2622 endwin();
2623 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2624 getprogname());
2625 exit(1);
2628 static char *
2629 parse_next_line(FILE *f, size_t *len)
2631 char *line;
2632 size_t linelen;
2633 size_t lineno;
2634 const char delim[3] = { '\0', '\0', '\0'};
2636 line = fparseln(f, &linelen, &lineno, delim, 0);
2637 if (len)
2638 *len = linelen;
2639 return line;
2642 static int
2643 match_line(const char *line, regex_t *regex)
2645 regmatch_t regmatch;
2647 return regexec(regex, line, 1, &regmatch, 0) == 0;
2650 struct tog_color *
2651 match_color(struct tog_colors *colors, const char *line)
2653 struct tog_color *tc = NULL;
2655 SIMPLEQ_FOREACH(tc, colors, entry) {
2656 if (match_line(line, &tc->regex))
2657 return tc;
2660 return NULL;
2663 static const struct got_error *
2664 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2665 int *last_displayed_line, int *eof, int max_lines, char *header,
2666 struct tog_colors *colors)
2668 const struct got_error *err;
2669 int nlines = 0, nprinted = 0;
2670 char *line;
2671 struct tog_color *tc;
2672 size_t len;
2673 wchar_t *wline;
2674 int width;
2676 rewind(f);
2677 werase(view->window);
2679 if (header) {
2680 err = format_line(&wline, &width, header, view->ncols, 0);
2681 if (err) {
2682 return err;
2685 if (view_needs_focus_indication(view))
2686 wstandout(view->window);
2687 waddwstr(view->window, wline);
2688 if (view_needs_focus_indication(view))
2689 wstandend(view->window);
2690 if (width <= view->ncols - 1)
2691 waddch(view->window, '\n');
2693 if (max_lines <= 1)
2694 return NULL;
2695 max_lines--;
2698 *eof = 0;
2699 while (nprinted < max_lines) {
2700 line = parse_next_line(f, &len);
2701 if (line == NULL) {
2702 *eof = 1;
2703 break;
2705 if (++nlines < *first_displayed_line) {
2706 free(line);
2707 continue;
2710 err = format_line(&wline, &width, line, view->ncols, 0);
2711 if (err) {
2712 free(line);
2713 return err;
2716 tc = match_color(colors, line);
2717 if (tc)
2718 wattr_on(view->window,
2719 COLOR_PAIR(tc->colorpair), NULL);
2720 waddwstr(view->window, wline);
2721 if (tc)
2722 wattr_off(view->window,
2723 COLOR_PAIR(tc->colorpair), NULL);
2724 if (width <= view->ncols - 1)
2725 waddch(view->window, '\n');
2726 if (++nprinted == 1)
2727 *first_displayed_line = nlines;
2728 free(line);
2729 free(wline);
2730 wline = NULL;
2732 *last_displayed_line = nlines;
2734 view_vborder(view);
2736 if (*eof) {
2737 while (nprinted < view->nlines) {
2738 waddch(view->window, '\n');
2739 nprinted++;
2742 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2743 if (err) {
2744 return err;
2747 wstandout(view->window);
2748 waddwstr(view->window, wline);
2749 wstandend(view->window);
2752 return NULL;
2755 static char *
2756 get_datestr(time_t *time, char *datebuf)
2758 struct tm mytm, *tm;
2759 char *p, *s;
2761 tm = gmtime_r(time, &mytm);
2762 if (tm == NULL)
2763 return NULL;
2764 s = asctime_r(tm, datebuf);
2765 if (s == NULL)
2766 return NULL;
2767 p = strchr(s, '\n');
2768 if (p)
2769 *p = '\0';
2770 return s;
2773 static const struct got_error *
2774 write_commit_info(struct got_object_id *commit_id,
2775 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2777 const struct got_error *err = NULL;
2778 char datebuf[26], *datestr;
2779 struct got_commit_object *commit;
2780 char *id_str = NULL, *logmsg = NULL;
2781 time_t committer_time;
2782 const char *author, *committer;
2783 char *refs_str = NULL;
2785 if (refs) {
2786 err = build_refs_str(&refs_str, refs, commit_id, repo);
2787 if (err)
2788 return err;
2791 err = got_object_open_as_commit(&commit, repo, commit_id);
2792 if (err)
2793 return err;
2795 err = got_object_id_str(&id_str, commit_id);
2796 if (err) {
2797 err = got_error_from_errno("got_object_id_str");
2798 goto done;
2801 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2802 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2803 err = got_error_from_errno("fprintf");
2804 goto done;
2806 if (fprintf(outfile, "from: %s\n",
2807 got_object_commit_get_author(commit)) < 0) {
2808 err = got_error_from_errno("fprintf");
2809 goto done;
2811 committer_time = got_object_commit_get_committer_time(commit);
2812 datestr = get_datestr(&committer_time, datebuf);
2813 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2814 err = got_error_from_errno("fprintf");
2815 goto done;
2817 author = got_object_commit_get_author(commit);
2818 committer = got_object_commit_get_committer(commit);
2819 if (strcmp(author, committer) != 0 &&
2820 fprintf(outfile, "via: %s\n", committer) < 0) {
2821 err = got_error_from_errno("fprintf");
2822 goto done;
2824 err = got_object_commit_get_logmsg(&logmsg, commit);
2825 if (err)
2826 goto done;
2827 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2828 err = got_error_from_errno("fprintf");
2829 goto done;
2831 done:
2832 free(id_str);
2833 free(logmsg);
2834 free(refs_str);
2835 got_object_commit_close(commit);
2836 return err;
2839 static const struct got_error *
2840 create_diff(struct tog_diff_view_state *s)
2842 const struct got_error *err = NULL;
2843 FILE *f = NULL;
2844 int obj_type;
2846 f = got_opentemp();
2847 if (f == NULL) {
2848 err = got_error_from_errno("got_opentemp");
2849 goto done;
2851 if (s->f && fclose(s->f) != 0) {
2852 err = got_error_from_errno("fclose");
2853 goto done;
2855 s->f = f;
2857 if (s->id1)
2858 err = got_object_get_type(&obj_type, s->repo, s->id1);
2859 else
2860 err = got_object_get_type(&obj_type, s->repo, s->id2);
2861 if (err)
2862 goto done;
2864 switch (obj_type) {
2865 case GOT_OBJ_TYPE_BLOB:
2866 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2867 s->diff_context, 0, s->repo, f);
2868 break;
2869 case GOT_OBJ_TYPE_TREE:
2870 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2871 s->diff_context, 0, s->repo, f);
2872 break;
2873 case GOT_OBJ_TYPE_COMMIT: {
2874 const struct got_object_id_queue *parent_ids;
2875 struct got_object_qid *pid;
2876 struct got_commit_object *commit2;
2878 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2879 if (err)
2880 break;
2881 /* Show commit info if we're diffing to a parent/root commit. */
2882 if (s->id1 == NULL)
2883 write_commit_info(s->id2, s->refs, s->repo, f);
2884 else {
2885 parent_ids = got_object_commit_get_parent_ids(commit2);
2886 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2887 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2888 write_commit_info(s->id2, s->refs,
2889 s->repo, f);
2890 break;
2894 got_object_commit_close(commit2);
2896 err = got_diff_objects_as_commits(s->id1, s->id2,
2897 s->diff_context, 0, s->repo, f);
2898 break;
2900 default:
2901 err = got_error(GOT_ERR_OBJ_TYPE);
2902 break;
2904 done:
2905 if (f && fflush(f) != 0 && err == NULL)
2906 err = got_error_from_errno("fflush");
2907 return err;
2910 static void
2911 diff_view_indicate_progress(struct tog_view *view)
2913 mvwaddstr(view->window, 0, 0, "diffing...");
2914 update_panels();
2915 doupdate();
2918 static const struct got_error *
2919 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2920 struct got_object_id *id2, struct tog_view *log_view,
2921 struct got_reflist_head *refs, struct got_repository *repo)
2923 const struct got_error *err;
2925 if (id1 != NULL && id2 != NULL) {
2926 int type1, type2;
2927 err = got_object_get_type(&type1, repo, id1);
2928 if (err)
2929 return err;
2930 err = got_object_get_type(&type2, repo, id2);
2931 if (err)
2932 return err;
2934 if (type1 != type2)
2935 return got_error(GOT_ERR_OBJ_TYPE);
2938 if (id1) {
2939 view->state.diff.id1 = got_object_id_dup(id1);
2940 if (view->state.diff.id1 == NULL)
2941 return got_error_from_errno("got_object_id_dup");
2942 } else
2943 view->state.diff.id1 = NULL;
2945 view->state.diff.id2 = got_object_id_dup(id2);
2946 if (view->state.diff.id2 == NULL) {
2947 free(view->state.diff.id1);
2948 view->state.diff.id1 = NULL;
2949 return got_error_from_errno("got_object_id_dup");
2951 view->state.diff.f = NULL;
2952 view->state.diff.first_displayed_line = 1;
2953 view->state.diff.last_displayed_line = view->nlines;
2954 view->state.diff.diff_context = 3;
2955 view->state.diff.log_view = log_view;
2956 view->state.diff.repo = repo;
2957 view->state.diff.refs = refs;
2958 SIMPLEQ_INIT(&view->state.diff.colors);
2960 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2961 err = add_color(&view->state.diff.colors,
2962 "^-", TOG_COLOR_DIFF_MINUS,
2963 get_color_value("TOG_COLOR_DIFF_MINUS"));
2964 if (err)
2965 return err;
2966 err = add_color(&view->state.diff.colors, "^\\+",
2967 TOG_COLOR_DIFF_PLUS,
2968 get_color_value("TOG_COLOR_DIFF_PLUS"));
2969 if (err) {
2970 free_colors(&view->state.diff.colors);
2971 return err;
2973 err = add_color(&view->state.diff.colors,
2974 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2975 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2976 if (err) {
2977 free_colors(&view->state.diff.colors);
2978 return err;
2981 err = add_color(&view->state.diff.colors,
2982 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2983 get_color_value("TOG_COLOR_DIFF_META"));
2984 if (err) {
2985 free_colors(&view->state.diff.colors);
2986 return err;
2989 err = add_color(&view->state.diff.colors,
2990 "^(from|via): ", TOG_COLOR_AUTHOR,
2991 get_color_value("TOG_COLOR_AUTHOR"));
2992 if (err) {
2993 free_colors(&view->state.diff.colors);
2994 return err;
2997 err = add_color(&view->state.diff.colors,
2998 "^date: ", TOG_COLOR_DATE,
2999 get_color_value("TOG_COLOR_DATE"));
3000 if (err) {
3001 free_colors(&view->state.diff.colors);
3002 return err;
3006 if (log_view && view_is_splitscreen(view))
3007 show_log_view(log_view); /* draw vborder */
3008 diff_view_indicate_progress(view);
3010 err = create_diff(&view->state.diff);
3011 if (err) {
3012 free(view->state.diff.id1);
3013 view->state.diff.id1 = NULL;
3014 free(view->state.diff.id2);
3015 view->state.diff.id2 = NULL;
3016 return err;
3019 view->show = show_diff_view;
3020 view->input = input_diff_view;
3021 view->close = close_diff_view;
3023 return NULL;
3026 static const struct got_error *
3027 close_diff_view(struct tog_view *view)
3029 const struct got_error *err = NULL;
3031 free(view->state.diff.id1);
3032 view->state.diff.id1 = NULL;
3033 free(view->state.diff.id2);
3034 view->state.diff.id2 = NULL;
3035 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3036 err = got_error_from_errno("fclose");
3037 free_colors(&view->state.diff.colors);
3038 return err;
3041 static const struct got_error *
3042 show_diff_view(struct tog_view *view)
3044 const struct got_error *err;
3045 struct tog_diff_view_state *s = &view->state.diff;
3046 char *id_str1 = NULL, *id_str2, *header;
3048 if (s->id1) {
3049 err = got_object_id_str(&id_str1, s->id1);
3050 if (err)
3051 return err;
3053 err = got_object_id_str(&id_str2, s->id2);
3054 if (err)
3055 return err;
3057 if (asprintf(&header, "diff %s %s",
3058 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3059 err = got_error_from_errno("asprintf");
3060 free(id_str1);
3061 free(id_str2);
3062 return err;
3064 free(id_str1);
3065 free(id_str2);
3067 return draw_file(view, s->f, &s->first_displayed_line,
3068 &s->last_displayed_line, &s->eof, view->nlines,
3069 header, &s->colors);
3072 static const struct got_error *
3073 set_selected_commit(struct tog_diff_view_state *s,
3074 struct commit_queue_entry *entry)
3076 const struct got_error *err;
3077 const struct got_object_id_queue *parent_ids;
3078 struct got_commit_object *selected_commit;
3079 struct got_object_qid *pid;
3081 free(s->id2);
3082 s->id2 = got_object_id_dup(entry->id);
3083 if (s->id2 == NULL)
3084 return got_error_from_errno("got_object_id_dup");
3086 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3087 if (err)
3088 return err;
3089 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3090 free(s->id1);
3091 pid = SIMPLEQ_FIRST(parent_ids);
3092 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3093 got_object_commit_close(selected_commit);
3094 return NULL;
3097 static const struct got_error *
3098 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3099 struct tog_view **focus_view, struct tog_view *view, int ch)
3101 const struct got_error *err = NULL;
3102 struct tog_diff_view_state *s = &view->state.diff;
3103 struct tog_log_view_state *ls;
3104 struct commit_queue_entry *entry;
3105 int i;
3107 switch (ch) {
3108 case 'k':
3109 case KEY_UP:
3110 if (s->first_displayed_line > 1)
3111 s->first_displayed_line--;
3112 break;
3113 case KEY_PPAGE:
3114 case CTRL('b'):
3115 if (s->first_displayed_line == 1)
3116 break;
3117 i = 0;
3118 while (i++ < view->nlines - 1 &&
3119 s->first_displayed_line > 1)
3120 s->first_displayed_line--;
3121 break;
3122 case 'j':
3123 case KEY_DOWN:
3124 if (!s->eof)
3125 s->first_displayed_line++;
3126 break;
3127 case KEY_NPAGE:
3128 case CTRL('f'):
3129 case ' ':
3130 if (s->eof)
3131 break;
3132 i = 0;
3133 while (!s->eof && i++ < view->nlines - 1) {
3134 char *line;
3135 line = parse_next_line(s->f, NULL);
3136 s->first_displayed_line++;
3137 if (line == NULL)
3138 break;
3140 break;
3141 case '[':
3142 if (s->diff_context > 0) {
3143 s->diff_context--;
3144 diff_view_indicate_progress(view);
3145 err = create_diff(s);
3147 break;
3148 case ']':
3149 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3150 s->diff_context++;
3151 diff_view_indicate_progress(view);
3152 err = create_diff(s);
3154 break;
3155 case '<':
3156 case ',':
3157 if (s->log_view == NULL)
3158 break;
3159 ls = &s->log_view->state.log;
3160 entry = TAILQ_PREV(ls->selected_entry,
3161 commit_queue_head, entry);
3162 if (entry == NULL)
3163 break;
3165 err = input_log_view(NULL, NULL, NULL, s->log_view,
3166 KEY_UP);
3167 if (err)
3168 break;
3170 err = set_selected_commit(s, entry);
3171 if (err)
3172 break;
3174 s->first_displayed_line = 1;
3175 s->last_displayed_line = view->nlines;
3177 diff_view_indicate_progress(view);
3178 err = create_diff(s);
3179 break;
3180 case '>':
3181 case '.':
3182 if (s->log_view == NULL)
3183 break;
3184 ls = &s->log_view->state.log;
3186 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3187 ls->thread_args.commits_needed++;
3189 /* Display "loading..." in log view. */
3190 show_log_view(s->log_view);
3191 update_panels();
3192 doupdate();
3194 err = trigger_log_thread(1 /* load_all */,
3195 &ls->thread_args.commits_needed,
3196 &ls->thread_args.log_complete,
3197 &ls->thread_args.need_commits);
3198 if (err)
3199 break;
3201 err = input_log_view(NULL, NULL, NULL, s->log_view,
3202 KEY_DOWN);
3203 if (err)
3204 break;
3206 entry = TAILQ_NEXT(ls->selected_entry, entry);
3207 if (entry == NULL)
3208 break;
3210 err = set_selected_commit(s, entry);
3211 if (err)
3212 break;
3214 s->first_displayed_line = 1;
3215 s->last_displayed_line = view->nlines;
3217 diff_view_indicate_progress(view);
3218 err = create_diff(s);
3219 break;
3220 default:
3221 break;
3224 return err;
3227 static const struct got_error *
3228 cmd_diff(int argc, char *argv[])
3230 const struct got_error *error = NULL;
3231 struct got_repository *repo = NULL;
3232 struct got_reflist_head refs;
3233 struct got_object_id *id1 = NULL, *id2 = NULL;
3234 char *repo_path = NULL;
3235 char *id_str1 = NULL, *id_str2 = NULL;
3236 int ch;
3237 struct tog_view *view;
3239 SIMPLEQ_INIT(&refs);
3241 #ifndef PROFILE
3242 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3243 NULL) == -1)
3244 err(1, "pledge");
3245 #endif
3247 while ((ch = getopt(argc, argv, "")) != -1) {
3248 switch (ch) {
3249 default:
3250 usage_diff();
3251 /* NOTREACHED */
3255 argc -= optind;
3256 argv += optind;
3258 if (argc == 0) {
3259 usage_diff(); /* TODO show local worktree changes */
3260 } else if (argc == 2) {
3261 repo_path = getcwd(NULL, 0);
3262 if (repo_path == NULL)
3263 return got_error_from_errno("getcwd");
3264 id_str1 = argv[0];
3265 id_str2 = argv[1];
3266 } else if (argc == 3) {
3267 repo_path = realpath(argv[0], NULL);
3268 if (repo_path == NULL)
3269 return got_error_from_errno2("realpath", argv[0]);
3270 id_str1 = argv[1];
3271 id_str2 = argv[2];
3272 } else
3273 usage_diff();
3275 init_curses();
3277 error = got_repo_open(&repo, repo_path, NULL);
3278 if (error)
3279 goto done;
3281 error = apply_unveil(got_repo_get_path(repo), NULL);
3282 if (error)
3283 goto done;
3285 error = got_repo_match_object_id_prefix(&id1, id_str1,
3286 GOT_OBJ_TYPE_ANY, repo);
3287 if (error)
3288 goto done;
3290 error = got_repo_match_object_id_prefix(&id2, id_str2,
3291 GOT_OBJ_TYPE_ANY, repo);
3292 if (error)
3293 goto done;
3295 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3296 if (error)
3297 goto done;
3299 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3300 if (view == NULL) {
3301 error = got_error_from_errno("view_open");
3302 goto done;
3304 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3305 if (error)
3306 goto done;
3307 error = view_loop(view);
3308 done:
3309 free(repo_path);
3310 if (repo)
3311 got_repo_close(repo);
3312 got_ref_list_free(&refs);
3313 return error;
3316 __dead static void
3317 usage_blame(void)
3319 endwin();
3320 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3321 getprogname());
3322 exit(1);
3325 struct tog_blame_line {
3326 int annotated;
3327 struct got_object_id *id;
3330 static const struct got_error *
3331 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3332 const char *path, struct tog_blame_line *lines, int nlines,
3333 int blame_complete, int selected_line, int *first_displayed_line,
3334 int *last_displayed_line, int *eof, int max_lines,
3335 struct tog_colors *colors)
3337 const struct got_error *err;
3338 int lineno = 0, nprinted = 0;
3339 char *line;
3340 size_t len;
3341 wchar_t *wline;
3342 int width;
3343 struct tog_blame_line *blame_line;
3344 struct got_object_id *prev_id = NULL;
3345 char *id_str;
3346 struct tog_color *tc;
3348 err = got_object_id_str(&id_str, id);
3349 if (err)
3350 return err;
3352 rewind(f);
3353 werase(view->window);
3355 if (asprintf(&line, "commit %s", id_str) == -1) {
3356 err = got_error_from_errno("asprintf");
3357 free(id_str);
3358 return err;
3361 err = format_line(&wline, &width, line, view->ncols, 0);
3362 free(line);
3363 line = NULL;
3364 if (err)
3365 return err;
3366 if (view_needs_focus_indication(view))
3367 wstandout(view->window);
3368 tc = get_color(colors, TOG_COLOR_COMMIT);
3369 if (tc)
3370 wattr_on(view->window,
3371 COLOR_PAIR(tc->colorpair), NULL);
3372 waddwstr(view->window, wline);
3373 if (tc)
3374 wattr_off(view->window,
3375 COLOR_PAIR(tc->colorpair), NULL);
3376 if (view_needs_focus_indication(view))
3377 wstandend(view->window);
3378 free(wline);
3379 wline = NULL;
3380 if (width < view->ncols - 1)
3381 waddch(view->window, '\n');
3383 if (asprintf(&line, "[%d/%d] %s%s",
3384 *first_displayed_line - 1 + selected_line, nlines,
3385 blame_complete ? "" : "annotating... ", path) == -1) {
3386 free(id_str);
3387 return got_error_from_errno("asprintf");
3389 free(id_str);
3390 err = format_line(&wline, &width, line, view->ncols, 0);
3391 free(line);
3392 line = NULL;
3393 if (err)
3394 return err;
3395 waddwstr(view->window, wline);
3396 free(wline);
3397 wline = NULL;
3398 if (width < view->ncols - 1)
3399 waddch(view->window, '\n');
3401 *eof = 0;
3402 while (nprinted < max_lines - 2) {
3403 line = parse_next_line(f, &len);
3404 if (line == NULL) {
3405 *eof = 1;
3406 break;
3408 if (++lineno < *first_displayed_line) {
3409 free(line);
3410 continue;
3413 if (view->ncols <= 9) {
3414 width = 9;
3415 wline = wcsdup(L"");
3416 if (wline == NULL)
3417 err = got_error_from_errno("wcsdup");
3418 } else {
3419 err = format_line(&wline, &width, line,
3420 view->ncols - 9, 9);
3421 width += 9;
3423 if (err) {
3424 free(line);
3425 return err;
3428 if (view->focussed && nprinted == selected_line - 1)
3429 wstandout(view->window);
3431 if (nlines > 0) {
3432 blame_line = &lines[lineno - 1];
3433 if (blame_line->annotated && prev_id &&
3434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3435 !(view->focussed &&
3436 nprinted == selected_line - 1)) {
3437 waddstr(view->window, " ");
3438 } else if (blame_line->annotated) {
3439 char *id_str;
3440 err = got_object_id_str(&id_str, blame_line->id);
3441 if (err) {
3442 free(line);
3443 free(wline);
3444 return err;
3446 tc = get_color(colors, TOG_COLOR_COMMIT);
3447 if (tc)
3448 wattr_on(view->window,
3449 COLOR_PAIR(tc->colorpair), NULL);
3450 wprintw(view->window, "%.8s", id_str);
3451 if (tc)
3452 wattr_off(view->window,
3453 COLOR_PAIR(tc->colorpair), NULL);
3454 free(id_str);
3455 prev_id = blame_line->id;
3456 } else {
3457 waddstr(view->window, "........");
3458 prev_id = NULL;
3460 } else {
3461 waddstr(view->window, "........");
3462 prev_id = NULL;
3465 if (view->focussed && nprinted == selected_line - 1)
3466 wstandend(view->window);
3467 waddstr(view->window, " ");
3469 waddwstr(view->window, wline);
3470 if (width <= view->ncols - 1)
3471 waddch(view->window, '\n');
3472 if (++nprinted == 1)
3473 *first_displayed_line = lineno;
3474 free(line);
3475 free(wline);
3476 wline = NULL;
3478 *last_displayed_line = lineno;
3480 view_vborder(view);
3482 return NULL;
3485 static const struct got_error *
3486 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3488 const struct got_error *err = NULL;
3489 struct tog_blame_cb_args *a = arg;
3490 struct tog_blame_line *line;
3491 int errcode;
3493 if (nlines != a->nlines ||
3494 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3495 return got_error(GOT_ERR_RANGE);
3497 errcode = pthread_mutex_lock(&tog_mutex);
3498 if (errcode)
3499 return got_error_set_errno(errcode, "pthread_mutex_lock");
3501 if (*a->quit) { /* user has quit the blame view */
3502 err = got_error(GOT_ERR_ITER_COMPLETED);
3503 goto done;
3506 if (lineno == -1)
3507 goto done; /* no change in this commit */
3509 line = &a->lines[lineno - 1];
3510 if (line->annotated)
3511 goto done;
3513 line->id = got_object_id_dup(id);
3514 if (line->id == NULL) {
3515 err = got_error_from_errno("got_object_id_dup");
3516 goto done;
3518 line->annotated = 1;
3519 done:
3520 errcode = pthread_mutex_unlock(&tog_mutex);
3521 if (errcode)
3522 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3523 return err;
3526 static void *
3527 blame_thread(void *arg)
3529 const struct got_error *err;
3530 struct tog_blame_thread_args *ta = arg;
3531 struct tog_blame_cb_args *a = ta->cb_args;
3532 int errcode;
3534 err = got_blame(ta->path, a->commit_id, ta->repo,
3535 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3536 if (err && err->code == GOT_ERR_CANCELLED)
3537 err = NULL;
3539 errcode = pthread_mutex_lock(&tog_mutex);
3540 if (errcode)
3541 return (void *)got_error_set_errno(errcode,
3542 "pthread_mutex_lock");
3544 got_repo_close(ta->repo);
3545 ta->repo = NULL;
3546 *ta->complete = 1;
3548 errcode = pthread_mutex_unlock(&tog_mutex);
3549 if (errcode && err == NULL)
3550 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3552 return (void *)err;
3555 static struct got_object_id *
3556 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3557 int first_displayed_line, int selected_line)
3559 struct tog_blame_line *line;
3561 if (nlines <= 0)
3562 return NULL;
3564 line = &lines[first_displayed_line - 1 + selected_line - 1];
3565 if (!line->annotated)
3566 return NULL;
3568 return line->id;
3571 static const struct got_error *
3572 stop_blame(struct tog_blame *blame)
3574 const struct got_error *err = NULL;
3575 int i;
3577 if (blame->thread) {
3578 int errcode;
3579 errcode = pthread_mutex_unlock(&tog_mutex);
3580 if (errcode)
3581 return got_error_set_errno(errcode,
3582 "pthread_mutex_unlock");
3583 errcode = pthread_join(blame->thread, (void **)&err);
3584 if (errcode)
3585 return got_error_set_errno(errcode, "pthread_join");
3586 errcode = pthread_mutex_lock(&tog_mutex);
3587 if (errcode)
3588 return got_error_set_errno(errcode,
3589 "pthread_mutex_lock");
3590 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3591 err = NULL;
3592 blame->thread = NULL;
3594 if (blame->thread_args.repo) {
3595 got_repo_close(blame->thread_args.repo);
3596 blame->thread_args.repo = NULL;
3598 if (blame->f) {
3599 if (fclose(blame->f) != 0 && err == NULL)
3600 err = got_error_from_errno("fclose");
3601 blame->f = NULL;
3603 if (blame->lines) {
3604 for (i = 0; i < blame->nlines; i++)
3605 free(blame->lines[i].id);
3606 free(blame->lines);
3607 blame->lines = NULL;
3609 free(blame->cb_args.commit_id);
3610 blame->cb_args.commit_id = NULL;
3612 return err;
3615 static const struct got_error *
3616 cancel_blame_view(void *arg)
3618 const struct got_error *err = NULL;
3619 int *done = arg;
3620 int errcode;
3622 errcode = pthread_mutex_lock(&tog_mutex);
3623 if (errcode)
3624 return got_error_set_errno(errcode,
3625 "pthread_mutex_unlock");
3627 if (*done)
3628 err = got_error(GOT_ERR_CANCELLED);
3630 errcode = pthread_mutex_unlock(&tog_mutex);
3631 if (errcode)
3632 return got_error_set_errno(errcode,
3633 "pthread_mutex_lock");
3635 return err;
3638 static const struct got_error *
3639 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3640 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3641 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3642 struct got_repository *repo)
3644 const struct got_error *err = NULL;
3645 struct got_blob_object *blob = NULL;
3646 struct got_repository *thread_repo = NULL;
3647 struct got_object_id *obj_id = NULL;
3648 int obj_type;
3650 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3651 if (err)
3652 return err;
3653 if (obj_id == NULL)
3654 return got_error(GOT_ERR_NO_OBJ);
3656 err = got_object_get_type(&obj_type, repo, obj_id);
3657 if (err)
3658 goto done;
3660 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3661 err = got_error(GOT_ERR_OBJ_TYPE);
3662 goto done;
3665 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3666 if (err)
3667 goto done;
3668 blame->f = got_opentemp();
3669 if (blame->f == NULL) {
3670 err = got_error_from_errno("got_opentemp");
3671 goto done;
3673 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3674 &blame->line_offsets, blame->f, blob);
3675 if (err || blame->nlines == 0)
3676 goto done;
3678 /* Don't include \n at EOF in the blame line count. */
3679 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3680 blame->nlines--;
3682 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3683 if (blame->lines == NULL) {
3684 err = got_error_from_errno("calloc");
3685 goto done;
3688 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3689 if (err)
3690 goto done;
3692 blame->cb_args.view = view;
3693 blame->cb_args.lines = blame->lines;
3694 blame->cb_args.nlines = blame->nlines;
3695 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3696 if (blame->cb_args.commit_id == NULL) {
3697 err = got_error_from_errno("got_object_id_dup");
3698 goto done;
3700 blame->cb_args.quit = done;
3702 blame->thread_args.path = path;
3703 blame->thread_args.repo = thread_repo;
3704 blame->thread_args.cb_args = &blame->cb_args;
3705 blame->thread_args.complete = blame_complete;
3706 blame->thread_args.cancel_cb = cancel_blame_view;
3707 blame->thread_args.cancel_arg = done;
3708 *blame_complete = 0;
3710 done:
3711 if (blob)
3712 got_object_blob_close(blob);
3713 free(obj_id);
3714 if (err)
3715 stop_blame(blame);
3716 return err;
3719 static const struct got_error *
3720 open_blame_view(struct tog_view *view, char *path,
3721 struct got_object_id *commit_id, struct got_reflist_head *refs,
3722 struct got_repository *repo)
3724 const struct got_error *err = NULL;
3725 struct tog_blame_view_state *s = &view->state.blame;
3727 SIMPLEQ_INIT(&s->blamed_commits);
3729 s->path = strdup(path);
3730 if (s->path == NULL)
3731 return got_error_from_errno("strdup");
3733 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3734 if (err) {
3735 free(s->path);
3736 return err;
3739 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3740 s->first_displayed_line = 1;
3741 s->last_displayed_line = view->nlines;
3742 s->selected_line = 1;
3743 s->blame_complete = 0;
3744 s->repo = repo;
3745 s->refs = refs;
3746 s->commit_id = commit_id;
3747 memset(&s->blame, 0, sizeof(s->blame));
3749 SIMPLEQ_INIT(&s->colors);
3750 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3751 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3752 get_color_value("TOG_COLOR_COMMIT"));
3753 if (err)
3754 return err;
3757 view->show = show_blame_view;
3758 view->input = input_blame_view;
3759 view->close = close_blame_view;
3760 view->search_start = search_start_blame_view;
3761 view->search_next = search_next_blame_view;
3763 return run_blame(&s->blame, view, &s->blame_complete,
3764 &s->first_displayed_line, &s->last_displayed_line,
3765 &s->selected_line, &s->done, &s->eof, s->path,
3766 s->blamed_commit->id, s->repo);
3769 static const struct got_error *
3770 close_blame_view(struct tog_view *view)
3772 const struct got_error *err = NULL;
3773 struct tog_blame_view_state *s = &view->state.blame;
3775 if (s->blame.thread)
3776 err = stop_blame(&s->blame);
3778 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3779 struct got_object_qid *blamed_commit;
3780 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3781 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3782 got_object_qid_free(blamed_commit);
3785 free(s->path);
3786 free_colors(&s->colors);
3788 return err;
3791 static const struct got_error *
3792 search_start_blame_view(struct tog_view *view)
3794 struct tog_blame_view_state *s = &view->state.blame;
3796 s->matched_line = 0;
3797 return NULL;
3800 static const struct got_error *
3801 search_next_blame_view(struct tog_view *view)
3803 struct tog_blame_view_state *s = &view->state.blame;
3804 int lineno;
3806 if (!view->searching) {
3807 view->search_next_done = 1;
3808 return NULL;
3811 if (s->matched_line) {
3812 if (view->searching == TOG_SEARCH_FORWARD)
3813 lineno = s->matched_line + 1;
3814 else
3815 lineno = s->matched_line - 1;
3816 } else {
3817 if (view->searching == TOG_SEARCH_FORWARD)
3818 lineno = 1;
3819 else
3820 lineno = s->blame.nlines;
3823 while (1) {
3824 char *line = NULL;
3825 off_t offset;
3826 size_t len;
3828 if (lineno <= 0 || lineno > s->blame.nlines) {
3829 if (s->matched_line == 0) {
3830 view->search_next_done = 1;
3831 free(line);
3832 break;
3835 if (view->searching == TOG_SEARCH_FORWARD)
3836 lineno = 1;
3837 else
3838 lineno = s->blame.nlines;
3841 offset = s->blame.line_offsets[lineno - 1];
3842 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3843 free(line);
3844 return got_error_from_errno("fseeko");
3846 free(line);
3847 line = parse_next_line(s->blame.f, &len);
3848 if (line && match_line(line, &view->regex)) {
3849 view->search_next_done = 1;
3850 s->matched_line = lineno;
3851 free(line);
3852 break;
3854 free(line);
3855 if (view->searching == TOG_SEARCH_FORWARD)
3856 lineno++;
3857 else
3858 lineno--;
3861 if (s->matched_line) {
3862 s->first_displayed_line = s->matched_line;
3863 s->selected_line = 1;
3866 return NULL;
3869 static const struct got_error *
3870 show_blame_view(struct tog_view *view)
3872 const struct got_error *err = NULL;
3873 struct tog_blame_view_state *s = &view->state.blame;
3874 int errcode;
3876 if (s->blame.thread == NULL) {
3877 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3878 &s->blame.thread_args);
3879 if (errcode)
3880 return got_error_set_errno(errcode, "pthread_create");
3882 halfdelay(1); /* fast refresh while annotating */
3885 if (s->blame_complete)
3886 halfdelay(10); /* disable fast refresh */
3888 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3889 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3890 s->selected_line, &s->first_displayed_line,
3891 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3893 view_vborder(view);
3894 return err;
3897 static const struct got_error *
3898 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3899 struct tog_view **focus_view, struct tog_view *view, int ch)
3901 const struct got_error *err = NULL, *thread_err = NULL;
3902 struct tog_view *diff_view;
3903 struct tog_blame_view_state *s = &view->state.blame;
3904 int begin_x = 0;
3906 switch (ch) {
3907 case 'q':
3908 s->done = 1;
3909 break;
3910 case 'k':
3911 case KEY_UP:
3912 if (s->selected_line > 1)
3913 s->selected_line--;
3914 else if (s->selected_line == 1 &&
3915 s->first_displayed_line > 1)
3916 s->first_displayed_line--;
3917 break;
3918 case KEY_PPAGE:
3919 if (s->first_displayed_line == 1) {
3920 s->selected_line = 1;
3921 break;
3923 if (s->first_displayed_line > view->nlines - 2)
3924 s->first_displayed_line -=
3925 (view->nlines - 2);
3926 else
3927 s->first_displayed_line = 1;
3928 break;
3929 case 'j':
3930 case KEY_DOWN:
3931 if (s->selected_line < view->nlines - 2 &&
3932 s->first_displayed_line +
3933 s->selected_line <= s->blame.nlines)
3934 s->selected_line++;
3935 else if (s->last_displayed_line <
3936 s->blame.nlines)
3937 s->first_displayed_line++;
3938 break;
3939 case 'b':
3940 case 'p': {
3941 struct got_object_id *id = NULL;
3942 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3943 s->first_displayed_line, s->selected_line);
3944 if (id == NULL)
3945 break;
3946 if (ch == 'p') {
3947 struct got_commit_object *commit;
3948 struct got_object_qid *pid;
3949 struct got_object_id *blob_id = NULL;
3950 int obj_type;
3951 err = got_object_open_as_commit(&commit,
3952 s->repo, id);
3953 if (err)
3954 break;
3955 pid = SIMPLEQ_FIRST(
3956 got_object_commit_get_parent_ids(commit));
3957 if (pid == NULL) {
3958 got_object_commit_close(commit);
3959 break;
3961 /* Check if path history ends here. */
3962 err = got_object_id_by_path(&blob_id, s->repo,
3963 pid->id, s->path);
3964 if (err) {
3965 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3966 err = NULL;
3967 got_object_commit_close(commit);
3968 break;
3970 err = got_object_get_type(&obj_type, s->repo,
3971 blob_id);
3972 free(blob_id);
3973 /* Can't blame non-blob type objects. */
3974 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3975 got_object_commit_close(commit);
3976 break;
3978 err = got_object_qid_alloc(&s->blamed_commit,
3979 pid->id);
3980 got_object_commit_close(commit);
3981 } else {
3982 if (got_object_id_cmp(id,
3983 s->blamed_commit->id) == 0)
3984 break;
3985 err = got_object_qid_alloc(&s->blamed_commit,
3986 id);
3988 if (err)
3989 break;
3990 s->done = 1;
3991 thread_err = stop_blame(&s->blame);
3992 s->done = 0;
3993 if (thread_err)
3994 break;
3995 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3996 s->blamed_commit, entry);
3997 err = run_blame(&s->blame, view, &s->blame_complete,
3998 &s->first_displayed_line, &s->last_displayed_line,
3999 &s->selected_line, &s->done, &s->eof,
4000 s->path, s->blamed_commit->id, s->repo);
4001 if (err)
4002 break;
4003 break;
4005 case 'B': {
4006 struct got_object_qid *first;
4007 first = SIMPLEQ_FIRST(&s->blamed_commits);
4008 if (!got_object_id_cmp(first->id, s->commit_id))
4009 break;
4010 s->done = 1;
4011 thread_err = stop_blame(&s->blame);
4012 s->done = 0;
4013 if (thread_err)
4014 break;
4015 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4016 got_object_qid_free(s->blamed_commit);
4017 s->blamed_commit =
4018 SIMPLEQ_FIRST(&s->blamed_commits);
4019 err = run_blame(&s->blame, view, &s->blame_complete,
4020 &s->first_displayed_line, &s->last_displayed_line,
4021 &s->selected_line, &s->done, &s->eof, s->path,
4022 s->blamed_commit->id, s->repo);
4023 if (err)
4024 break;
4025 break;
4027 case KEY_ENTER:
4028 case '\r': {
4029 struct got_object_id *id = NULL;
4030 struct got_object_qid *pid;
4031 struct got_commit_object *commit = NULL;
4032 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4033 s->first_displayed_line, s->selected_line);
4034 if (id == NULL)
4035 break;
4036 err = got_object_open_as_commit(&commit, s->repo, id);
4037 if (err)
4038 break;
4039 pid = SIMPLEQ_FIRST(
4040 got_object_commit_get_parent_ids(commit));
4041 if (view_is_parent_view(view))
4042 begin_x = view_split_begin_x(view->begin_x);
4043 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4044 if (diff_view == NULL) {
4045 got_object_commit_close(commit);
4046 err = got_error_from_errno("view_open");
4047 break;
4049 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4050 id, NULL, s->refs, s->repo);
4051 got_object_commit_close(commit);
4052 if (err) {
4053 view_close(diff_view);
4054 break;
4056 if (view_is_parent_view(view)) {
4057 err = view_close_child(view);
4058 if (err)
4059 break;
4060 err = view_set_child(view, diff_view);
4061 if (err) {
4062 view_close(diff_view);
4063 break;
4065 *focus_view = diff_view;
4066 view->child_focussed = 1;
4067 } else
4068 *new_view = diff_view;
4069 if (err)
4070 break;
4071 break;
4073 case KEY_NPAGE:
4074 case ' ':
4075 if (s->last_displayed_line >= s->blame.nlines &&
4076 s->selected_line >= MIN(s->blame.nlines,
4077 view->nlines - 2)) {
4078 break;
4080 if (s->last_displayed_line >= s->blame.nlines &&
4081 s->selected_line < view->nlines - 2) {
4082 s->selected_line = MIN(s->blame.nlines,
4083 view->nlines - 2);
4084 break;
4086 if (s->last_displayed_line + view->nlines - 2
4087 <= s->blame.nlines)
4088 s->first_displayed_line +=
4089 view->nlines - 2;
4090 else
4091 s->first_displayed_line =
4092 s->blame.nlines -
4093 (view->nlines - 3);
4094 break;
4095 case KEY_RESIZE:
4096 if (s->selected_line > view->nlines - 2) {
4097 s->selected_line = MIN(s->blame.nlines,
4098 view->nlines - 2);
4100 break;
4101 default:
4102 break;
4104 return thread_err ? thread_err : err;
4107 static const struct got_error *
4108 cmd_blame(int argc, char *argv[])
4110 const struct got_error *error;
4111 struct got_repository *repo = NULL;
4112 struct got_reflist_head refs;
4113 struct got_worktree *worktree = NULL;
4114 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4115 struct got_object_id *commit_id = NULL;
4116 char *commit_id_str = NULL;
4117 int ch;
4118 struct tog_view *view;
4120 SIMPLEQ_INIT(&refs);
4122 #ifndef PROFILE
4123 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4124 NULL) == -1)
4125 err(1, "pledge");
4126 #endif
4128 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4129 switch (ch) {
4130 case 'c':
4131 commit_id_str = optarg;
4132 break;
4133 case 'r':
4134 repo_path = realpath(optarg, NULL);
4135 if (repo_path == NULL)
4136 return got_error_from_errno2("realpath",
4137 optarg);
4138 break;
4139 default:
4140 usage_blame();
4141 /* NOTREACHED */
4145 argc -= optind;
4146 argv += optind;
4148 if (argc == 1)
4149 path = argv[0];
4150 else
4151 usage_blame();
4153 cwd = getcwd(NULL, 0);
4154 if (cwd == NULL) {
4155 error = got_error_from_errno("getcwd");
4156 goto done;
4158 if (repo_path == NULL) {
4159 error = got_worktree_open(&worktree, cwd);
4160 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4161 goto done;
4162 else
4163 error = NULL;
4164 if (worktree) {
4165 repo_path =
4166 strdup(got_worktree_get_repo_path(worktree));
4167 if (repo_path == NULL)
4168 error = got_error_from_errno("strdup");
4169 if (error)
4170 goto done;
4171 } else {
4172 repo_path = strdup(cwd);
4173 if (repo_path == NULL) {
4174 error = got_error_from_errno("strdup");
4175 goto done;
4180 init_curses();
4182 error = got_repo_open(&repo, repo_path, NULL);
4183 if (error != NULL)
4184 goto done;
4186 error = apply_unveil(got_repo_get_path(repo), NULL);
4187 if (error)
4188 goto done;
4190 if (worktree) {
4191 const char *prefix = got_worktree_get_path_prefix(worktree);
4192 char *p, *worktree_subdir = cwd +
4193 strlen(got_worktree_get_root_path(worktree));
4194 if (asprintf(&p, "%s%s%s%s%s",
4195 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4196 worktree_subdir, worktree_subdir[0] ? "/" : "",
4197 path) == -1) {
4198 error = got_error_from_errno("asprintf");
4199 goto done;
4201 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4202 free(p);
4203 } else {
4204 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4206 if (error)
4207 goto done;
4209 if (commit_id_str == NULL) {
4210 struct got_reference *head_ref;
4211 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4212 if (error != NULL)
4213 goto done;
4214 error = got_ref_resolve(&commit_id, repo, head_ref);
4215 got_ref_close(head_ref);
4216 } else {
4217 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4218 if (error) {
4219 if (error->code != GOT_ERR_NOT_REF)
4220 goto done;
4221 error = got_repo_match_object_id_prefix(&commit_id,
4222 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4225 if (error != NULL)
4226 goto done;
4228 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4229 if (error)
4230 goto done;
4232 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4233 if (view == NULL) {
4234 error = got_error_from_errno("view_open");
4235 goto done;
4237 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4238 if (error)
4239 goto done;
4240 if (worktree) {
4241 /* Release work tree lock. */
4242 got_worktree_close(worktree);
4243 worktree = NULL;
4245 error = view_loop(view);
4246 done:
4247 free(repo_path);
4248 free(cwd);
4249 free(commit_id);
4250 if (worktree)
4251 got_worktree_close(worktree);
4252 if (repo)
4253 got_repo_close(repo);
4254 got_ref_list_free(&refs);
4255 return error;
4258 static const struct got_error *
4259 draw_tree_entries(struct tog_view *view,
4260 struct got_tree_entry **first_displayed_entry,
4261 struct got_tree_entry **last_displayed_entry,
4262 struct got_tree_entry **selected_entry, int *ndisplayed,
4263 const char *label, int show_ids, const char *parent_path,
4264 struct got_tree_object *tree, int selected, int limit,
4265 int isroot, struct tog_colors *colors)
4267 const struct got_error *err = NULL;
4268 struct got_tree_entry *te;
4269 wchar_t *wline;
4270 struct tog_color *tc;
4271 int width, n, i, nentries;
4273 *ndisplayed = 0;
4275 werase(view->window);
4277 if (limit == 0)
4278 return NULL;
4280 err = format_line(&wline, &width, label, view->ncols, 0);
4281 if (err)
4282 return err;
4283 if (view_needs_focus_indication(view))
4284 wstandout(view->window);
4285 tc = get_color(colors, TOG_COLOR_COMMIT);
4286 if (tc)
4287 wattr_on(view->window,
4288 COLOR_PAIR(tc->colorpair), NULL);
4289 waddwstr(view->window, wline);
4290 if (tc)
4291 wattr_off(view->window,
4292 COLOR_PAIR(tc->colorpair), NULL);
4293 if (view_needs_focus_indication(view))
4294 wstandend(view->window);
4295 free(wline);
4296 wline = NULL;
4297 if (width < view->ncols - 1)
4298 waddch(view->window, '\n');
4299 if (--limit <= 0)
4300 return NULL;
4301 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4302 if (err)
4303 return err;
4304 waddwstr(view->window, wline);
4305 free(wline);
4306 wline = NULL;
4307 if (width < view->ncols - 1)
4308 waddch(view->window, '\n');
4309 if (--limit <= 0)
4310 return NULL;
4311 waddch(view->window, '\n');
4312 if (--limit <= 0)
4313 return NULL;
4315 if (*first_displayed_entry == NULL) {
4316 te = got_object_tree_get_first_entry(tree);
4317 if (selected == 0) {
4318 if (view->focussed)
4319 wstandout(view->window);
4320 *selected_entry = NULL;
4322 waddstr(view->window, " ..\n"); /* parent directory */
4323 if (selected == 0 && view->focussed)
4324 wstandend(view->window);
4325 (*ndisplayed)++;
4326 if (--limit <= 0)
4327 return NULL;
4328 n = 1;
4329 } else {
4330 n = 0;
4331 te = *first_displayed_entry;
4334 nentries = got_object_tree_get_nentries(tree);
4335 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4336 char *line = NULL, *id_str = NULL;
4337 const char *modestr = "";
4338 mode_t mode;
4340 te = got_object_tree_get_entry(tree, i);
4341 mode = got_tree_entry_get_mode(te);
4343 if (show_ids) {
4344 err = got_object_id_str(&id_str,
4345 got_tree_entry_get_id(te));
4346 if (err)
4347 return got_error_from_errno(
4348 "got_object_id_str");
4350 if (got_object_tree_entry_is_submodule(te))
4351 modestr = "$";
4352 else if (S_ISLNK(mode))
4353 modestr = "@";
4354 else if (S_ISDIR(mode))
4355 modestr = "/";
4356 else if (mode & S_IXUSR)
4357 modestr = "*";
4358 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4359 got_tree_entry_get_name(te), modestr) == -1) {
4360 free(id_str);
4361 return got_error_from_errno("asprintf");
4363 free(id_str);
4364 err = format_line(&wline, &width, line, view->ncols, 0);
4365 if (err) {
4366 free(line);
4367 break;
4369 if (n == selected) {
4370 if (view->focussed)
4371 wstandout(view->window);
4372 *selected_entry = te;
4374 tc = match_color(colors, line);
4375 if (tc)
4376 wattr_on(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 waddwstr(view->window, wline);
4379 if (tc)
4380 wattr_off(view->window,
4381 COLOR_PAIR(tc->colorpair), NULL);
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4384 if (n == selected && view->focussed)
4385 wstandend(view->window);
4386 free(line);
4387 free(wline);
4388 wline = NULL;
4389 n++;
4390 (*ndisplayed)++;
4391 *last_displayed_entry = te;
4392 if (--limit <= 0)
4393 break;
4396 return err;
4399 static void
4400 tree_scroll_up(struct tog_view *view,
4401 struct got_tree_entry **first_displayed_entry, int maxscroll,
4402 struct got_tree_object *tree, int isroot)
4404 struct got_tree_entry *te;
4405 int i;
4407 if (*first_displayed_entry == NULL)
4408 return;
4410 te = got_object_tree_get_entry(tree, 0);
4411 if (*first_displayed_entry == te) {
4412 if (!isroot)
4413 *first_displayed_entry = NULL;
4414 return;
4417 i = 0;
4418 while (*first_displayed_entry && i < maxscroll) {
4419 *first_displayed_entry = got_tree_entry_get_prev(tree,
4420 *first_displayed_entry);
4421 i++;
4423 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4424 *first_displayed_entry = NULL;
4427 static int
4428 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4429 struct got_tree_entry *last_displayed_entry,
4430 struct got_tree_object *tree)
4432 struct got_tree_entry *next, *last;
4433 int n = 0;
4435 if (*first_displayed_entry)
4436 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4437 else
4438 next = got_object_tree_get_first_entry(tree);
4440 last = last_displayed_entry;
4441 while (next && last && n++ < maxscroll) {
4442 last = got_tree_entry_get_next(tree, last);
4443 if (last) {
4444 *first_displayed_entry = next;
4445 next = got_tree_entry_get_next(tree, next);
4448 return n;
4451 static const struct got_error *
4452 tree_entry_path(char **path, struct tog_parent_trees *parents,
4453 struct got_tree_entry *te)
4455 const struct got_error *err = NULL;
4456 struct tog_parent_tree *pt;
4457 size_t len = 2; /* for leading slash and NUL */
4459 TAILQ_FOREACH(pt, parents, entry)
4460 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4461 + 1 /* slash */;
4462 if (te)
4463 len += strlen(got_tree_entry_get_name(te));
4465 *path = calloc(1, len);
4466 if (path == NULL)
4467 return got_error_from_errno("calloc");
4469 (*path)[0] = '/';
4470 pt = TAILQ_LAST(parents, tog_parent_trees);
4471 while (pt) {
4472 const char *name = got_tree_entry_get_name(pt->selected_entry);
4473 if (strlcat(*path, name, len) >= len) {
4474 err = got_error(GOT_ERR_NO_SPACE);
4475 goto done;
4477 if (strlcat(*path, "/", len) >= len) {
4478 err = got_error(GOT_ERR_NO_SPACE);
4479 goto done;
4481 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4483 if (te) {
4484 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4485 err = got_error(GOT_ERR_NO_SPACE);
4486 goto done;
4489 done:
4490 if (err) {
4491 free(*path);
4492 *path = NULL;
4494 return err;
4497 static const struct got_error *
4498 blame_tree_entry(struct tog_view **new_view, int begin_x,
4499 struct got_tree_entry *te, struct tog_parent_trees *parents,
4500 struct got_object_id *commit_id, struct got_reflist_head *refs,
4501 struct got_repository *repo)
4503 const struct got_error *err = NULL;
4504 char *path;
4505 struct tog_view *blame_view;
4507 *new_view = NULL;
4509 err = tree_entry_path(&path, parents, te);
4510 if (err)
4511 return err;
4513 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4514 if (blame_view == NULL) {
4515 err = got_error_from_errno("view_open");
4516 goto done;
4519 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4520 if (err) {
4521 if (err->code == GOT_ERR_CANCELLED)
4522 err = NULL;
4523 view_close(blame_view);
4524 } else
4525 *new_view = blame_view;
4526 done:
4527 free(path);
4528 return err;
4531 static const struct got_error *
4532 log_tree_entry(struct tog_view **new_view, int begin_x,
4533 struct got_tree_entry *te, struct tog_parent_trees *parents,
4534 struct got_object_id *commit_id, struct got_reflist_head *refs,
4535 struct got_repository *repo)
4537 struct tog_view *log_view;
4538 const struct got_error *err = NULL;
4539 char *path;
4541 *new_view = NULL;
4543 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4544 if (log_view == NULL)
4545 return got_error_from_errno("view_open");
4547 err = tree_entry_path(&path, parents, te);
4548 if (err)
4549 return err;
4551 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4552 if (err)
4553 view_close(log_view);
4554 else
4555 *new_view = log_view;
4556 free(path);
4557 return err;
4560 static const struct got_error *
4561 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4562 struct got_object_id *commit_id, struct got_reflist_head *refs,
4563 struct got_repository *repo)
4565 const struct got_error *err = NULL;
4566 char *commit_id_str = NULL;
4567 struct tog_tree_view_state *s = &view->state.tree;
4569 TAILQ_INIT(&s->parents);
4571 err = got_object_id_str(&commit_id_str, commit_id);
4572 if (err != NULL)
4573 goto done;
4575 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4576 err = got_error_from_errno("asprintf");
4577 goto done;
4580 s->root = s->tree = root;
4581 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4582 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4583 s->commit_id = got_object_id_dup(commit_id);
4584 if (s->commit_id == NULL) {
4585 err = got_error_from_errno("got_object_id_dup");
4586 goto done;
4588 s->refs = refs;
4589 s->repo = repo;
4591 SIMPLEQ_INIT(&s->colors);
4593 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4594 err = add_color(&s->colors, "\\$$",
4595 TOG_COLOR_TREE_SUBMODULE,
4596 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4597 if (err)
4598 goto done;
4599 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4600 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4601 if (err) {
4602 free_colors(&s->colors);
4603 goto done;
4605 err = add_color(&s->colors, "/$",
4606 TOG_COLOR_TREE_DIRECTORY,
4607 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4608 if (err) {
4609 free_colors(&s->colors);
4610 goto done;
4613 err = add_color(&s->colors, "\\*$",
4614 TOG_COLOR_TREE_EXECUTABLE,
4615 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4616 if (err) {
4617 free_colors(&s->colors);
4618 goto done;
4621 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4622 get_color_value("TOG_COLOR_COMMIT"));
4623 if (err) {
4624 free_colors(&s->colors);
4625 goto done;
4629 view->show = show_tree_view;
4630 view->input = input_tree_view;
4631 view->close = close_tree_view;
4632 view->search_start = search_start_tree_view;
4633 view->search_next = search_next_tree_view;
4634 done:
4635 free(commit_id_str);
4636 if (err) {
4637 free(s->tree_label);
4638 s->tree_label = NULL;
4640 return err;
4643 static const struct got_error *
4644 close_tree_view(struct tog_view *view)
4646 struct tog_tree_view_state *s = &view->state.tree;
4648 free_colors(&s->colors);
4649 free(s->tree_label);
4650 s->tree_label = NULL;
4651 free(s->commit_id);
4652 s->commit_id = NULL;
4653 while (!TAILQ_EMPTY(&s->parents)) {
4654 struct tog_parent_tree *parent;
4655 parent = TAILQ_FIRST(&s->parents);
4656 TAILQ_REMOVE(&s->parents, parent, entry);
4657 free(parent);
4660 if (s->tree != s->root)
4661 got_object_tree_close(s->tree);
4662 got_object_tree_close(s->root);
4664 return NULL;
4667 static const struct got_error *
4668 search_start_tree_view(struct tog_view *view)
4670 struct tog_tree_view_state *s = &view->state.tree;
4672 s->matched_entry = NULL;
4673 return NULL;
4676 static int
4677 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4679 regmatch_t regmatch;
4681 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4682 0) == 0;
4685 static const struct got_error *
4686 search_next_tree_view(struct tog_view *view)
4688 struct tog_tree_view_state *s = &view->state.tree;
4689 struct got_tree_entry *te = NULL;
4691 if (!view->searching) {
4692 view->search_next_done = 1;
4693 return NULL;
4696 if (s->matched_entry) {
4697 if (view->searching == TOG_SEARCH_FORWARD) {
4698 if (s->selected_entry)
4699 te = got_tree_entry_get_next(s->tree,
4700 s->selected_entry);
4701 else
4702 te = got_object_tree_get_first_entry(s->tree);
4703 } else {
4704 if (s->selected_entry == NULL)
4705 te = got_object_tree_get_last_entry(s->tree);
4706 else
4707 te = got_tree_entry_get_prev(s->tree,
4708 s->selected_entry);
4710 } else {
4711 if (view->searching == TOG_SEARCH_FORWARD)
4712 te = got_object_tree_get_first_entry(s->tree);
4713 else
4714 te = got_object_tree_get_last_entry(s->tree);
4717 while (1) {
4718 if (te == NULL) {
4719 if (s->matched_entry == NULL) {
4720 view->search_next_done = 1;
4721 return NULL;
4723 if (view->searching == TOG_SEARCH_FORWARD)
4724 te = got_object_tree_get_first_entry(s->tree);
4725 else
4726 te = got_object_tree_get_last_entry(s->tree);
4729 if (match_tree_entry(te, &view->regex)) {
4730 view->search_next_done = 1;
4731 s->matched_entry = te;
4732 break;
4735 if (view->searching == TOG_SEARCH_FORWARD)
4736 te = got_tree_entry_get_next(s->tree, te);
4737 else
4738 te = got_tree_entry_get_prev(s->tree, te);
4741 if (s->matched_entry) {
4742 s->first_displayed_entry = s->matched_entry;
4743 s->selected = 0;
4746 return NULL;
4749 static const struct got_error *
4750 show_tree_view(struct tog_view *view)
4752 const struct got_error *err = NULL;
4753 struct tog_tree_view_state *s = &view->state.tree;
4754 char *parent_path;
4756 err = tree_entry_path(&parent_path, &s->parents, NULL);
4757 if (err)
4758 return err;
4760 err = draw_tree_entries(view, &s->first_displayed_entry,
4761 &s->last_displayed_entry, &s->selected_entry,
4762 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4763 s->tree, s->selected, view->nlines, s->tree == s->root,
4764 &s->colors);
4765 free(parent_path);
4767 view_vborder(view);
4768 return err;
4771 static const struct got_error *
4772 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4773 struct tog_view **focus_view, struct tog_view *view, int ch)
4775 const struct got_error *err = NULL;
4776 struct tog_tree_view_state *s = &view->state.tree;
4777 struct tog_view *log_view;
4778 int begin_x = 0, nscrolled;
4780 switch (ch) {
4781 case 'i':
4782 s->show_ids = !s->show_ids;
4783 break;
4784 case 'l':
4785 if (!s->selected_entry)
4786 break;
4787 if (view_is_parent_view(view))
4788 begin_x = view_split_begin_x(view->begin_x);
4789 err = log_tree_entry(&log_view, begin_x,
4790 s->selected_entry, &s->parents,
4791 s->commit_id, s->refs, s->repo);
4792 if (view_is_parent_view(view)) {
4793 err = view_close_child(view);
4794 if (err)
4795 return err;
4796 err = view_set_child(view, log_view);
4797 if (err) {
4798 view_close(log_view);
4799 break;
4801 *focus_view = log_view;
4802 view->child_focussed = 1;
4803 } else
4804 *new_view = log_view;
4805 break;
4806 case 'k':
4807 case KEY_UP:
4808 if (s->selected > 0) {
4809 s->selected--;
4810 if (s->selected == 0)
4811 break;
4813 if (s->selected > 0)
4814 break;
4815 tree_scroll_up(view, &s->first_displayed_entry, 1,
4816 s->tree, s->tree == s->root);
4817 break;
4818 case KEY_PPAGE:
4819 tree_scroll_up(view, &s->first_displayed_entry,
4820 MAX(0, view->nlines - 4 - s->selected), s->tree,
4821 s->tree == s->root);
4822 s->selected = 0;
4823 if (got_object_tree_get_first_entry(s->tree) ==
4824 s->first_displayed_entry && s->tree != s->root)
4825 s->first_displayed_entry = NULL;
4826 break;
4827 case 'j':
4828 case KEY_DOWN:
4829 if (s->selected < s->ndisplayed - 1) {
4830 s->selected++;
4831 break;
4833 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4834 == NULL)
4835 /* can't scroll any further */
4836 break;
4837 tree_scroll_down(&s->first_displayed_entry, 1,
4838 s->last_displayed_entry, s->tree);
4839 break;
4840 case KEY_NPAGE:
4841 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
4842 == NULL) {
4843 /* can't scroll any further; move cursor down */
4844 if (s->selected < s->ndisplayed - 1)
4845 s->selected = s->ndisplayed - 1;
4846 break;
4848 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4849 view->nlines, s->last_displayed_entry, s->tree);
4850 if (nscrolled < view->nlines) {
4851 int ndisplayed = 0;
4852 struct got_tree_entry *te;
4853 te = s->first_displayed_entry;
4854 do {
4855 ndisplayed++;
4856 te = got_tree_entry_get_next(s->tree, te);
4857 } while (te);
4858 s->selected = ndisplayed - 1;
4860 break;
4861 case KEY_ENTER:
4862 case '\r':
4863 case KEY_BACKSPACE:
4864 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4865 struct tog_parent_tree *parent;
4866 /* user selected '..' */
4867 if (s->tree == s->root)
4868 break;
4869 parent = TAILQ_FIRST(&s->parents);
4870 TAILQ_REMOVE(&s->parents, parent,
4871 entry);
4872 got_object_tree_close(s->tree);
4873 s->tree = parent->tree;
4874 s->first_displayed_entry =
4875 parent->first_displayed_entry;
4876 s->selected_entry =
4877 parent->selected_entry;
4878 s->selected = parent->selected;
4879 free(parent);
4880 } else if (S_ISDIR(got_tree_entry_get_mode(
4881 s->selected_entry))) {
4882 struct got_tree_object *subtree;
4883 err = got_object_open_as_tree(&subtree, s->repo,
4884 got_tree_entry_get_id(s->selected_entry));
4885 if (err)
4886 break;
4887 err = tree_view_visit_subtree(subtree, s);
4888 if (err) {
4889 got_object_tree_close(subtree);
4890 break;
4892 } else if (S_ISREG(got_tree_entry_get_mode(
4893 s->selected_entry))) {
4894 struct tog_view *blame_view;
4895 int begin_x = view_is_parent_view(view) ?
4896 view_split_begin_x(view->begin_x) : 0;
4898 err = blame_tree_entry(&blame_view, begin_x,
4899 s->selected_entry, &s->parents,
4900 s->commit_id, s->refs, s->repo);
4901 if (err)
4902 break;
4903 if (view_is_parent_view(view)) {
4904 err = view_close_child(view);
4905 if (err)
4906 return err;
4907 err = view_set_child(view, blame_view);
4908 if (err) {
4909 view_close(blame_view);
4910 break;
4912 *focus_view = blame_view;
4913 view->child_focussed = 1;
4914 } else
4915 *new_view = blame_view;
4917 break;
4918 case KEY_RESIZE:
4919 if (s->selected > view->nlines)
4920 s->selected = s->ndisplayed - 1;
4921 break;
4922 default:
4923 break;
4926 return err;
4929 __dead static void
4930 usage_tree(void)
4932 endwin();
4933 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4934 getprogname());
4935 exit(1);
4938 static const struct got_error *
4939 cmd_tree(int argc, char *argv[])
4941 const struct got_error *error;
4942 struct got_repository *repo = NULL;
4943 struct got_reflist_head refs;
4944 char *repo_path = NULL;
4945 struct got_object_id *commit_id = NULL;
4946 char *commit_id_arg = NULL;
4947 struct got_commit_object *commit = NULL;
4948 struct got_tree_object *tree = NULL;
4949 int ch;
4950 struct tog_view *view;
4952 SIMPLEQ_INIT(&refs);
4954 #ifndef PROFILE
4955 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4956 NULL) == -1)
4957 err(1, "pledge");
4958 #endif
4960 while ((ch = getopt(argc, argv, "c:")) != -1) {
4961 switch (ch) {
4962 case 'c':
4963 commit_id_arg = optarg;
4964 break;
4965 default:
4966 usage_tree();
4967 /* NOTREACHED */
4971 argc -= optind;
4972 argv += optind;
4974 if (argc == 0) {
4975 struct got_worktree *worktree;
4976 char *cwd = getcwd(NULL, 0);
4977 if (cwd == NULL)
4978 return got_error_from_errno("getcwd");
4979 error = got_worktree_open(&worktree, cwd);
4980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4981 goto done;
4982 if (worktree) {
4983 free(cwd);
4984 repo_path =
4985 strdup(got_worktree_get_repo_path(worktree));
4986 got_worktree_close(worktree);
4987 } else
4988 repo_path = cwd;
4989 if (repo_path == NULL) {
4990 error = got_error_from_errno("strdup");
4991 goto done;
4993 } else if (argc == 1) {
4994 repo_path = realpath(argv[0], NULL);
4995 if (repo_path == NULL)
4996 return got_error_from_errno2("realpath", argv[0]);
4997 } else
4998 usage_log();
5000 init_curses();
5002 error = got_repo_open(&repo, repo_path, NULL);
5003 if (error != NULL)
5004 goto done;
5006 error = apply_unveil(got_repo_get_path(repo), NULL);
5007 if (error)
5008 goto done;
5010 if (commit_id_arg == NULL)
5011 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5012 else {
5013 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5014 if (error) {
5015 if (error->code != GOT_ERR_NOT_REF)
5016 goto done;
5017 error = got_repo_match_object_id_prefix(&commit_id,
5018 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5021 if (error != NULL)
5022 goto done;
5024 error = got_object_open_as_commit(&commit, repo, commit_id);
5025 if (error != NULL)
5026 goto done;
5028 error = got_object_open_as_tree(&tree, repo,
5029 got_object_commit_get_tree_id(commit));
5030 if (error != NULL)
5031 goto done;
5033 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5034 if (error)
5035 goto done;
5037 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5038 if (view == NULL) {
5039 error = got_error_from_errno("view_open");
5040 goto done;
5042 error = open_tree_view(view, tree, commit_id, &refs, repo);
5043 if (error)
5044 goto done;
5045 error = view_loop(view);
5046 done:
5047 free(repo_path);
5048 free(commit_id);
5049 if (commit)
5050 got_object_commit_close(commit);
5051 if (tree)
5052 got_object_tree_close(tree);
5053 if (repo)
5054 got_repo_close(repo);
5055 got_ref_list_free(&refs);
5056 return error;
5059 static void
5060 list_commands(void)
5062 int i;
5064 fprintf(stderr, "commands:");
5065 for (i = 0; i < nitems(tog_commands); i++) {
5066 struct tog_cmd *cmd = &tog_commands[i];
5067 fprintf(stderr, " %s", cmd->name);
5069 fputc('\n', stderr);
5072 __dead static void
5073 usage(int hflag)
5075 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5076 getprogname());
5077 if (hflag)
5078 list_commands();
5079 exit(1);
5082 static char **
5083 make_argv(const char *arg0, const char *arg1)
5085 char **argv;
5086 int argc = (arg1 == NULL ? 1 : 2);
5088 argv = calloc(argc, sizeof(char *));
5089 if (argv == NULL)
5090 err(1, "calloc");
5091 argv[0] = strdup(arg0);
5092 if (argv[0] == NULL)
5093 err(1, "strdup");
5094 if (arg1) {
5095 argv[1] = strdup(arg1);
5096 if (argv[1] == NULL)
5097 err(1, "strdup");
5100 return argv;
5103 int
5104 main(int argc, char *argv[])
5106 const struct got_error *error = NULL;
5107 struct tog_cmd *cmd = NULL;
5108 int ch, hflag = 0, Vflag = 0;
5109 char **cmd_argv = NULL;
5111 setlocale(LC_CTYPE, "");
5113 while ((ch = getopt(argc, argv, "hV")) != -1) {
5114 switch (ch) {
5115 case 'h':
5116 hflag = 1;
5117 break;
5118 case 'V':
5119 Vflag = 1;
5120 break;
5121 default:
5122 usage(hflag);
5123 /* NOTREACHED */
5127 argc -= optind;
5128 argv += optind;
5129 optind = 0;
5130 optreset = 1;
5132 if (Vflag) {
5133 got_version_print_str();
5134 return 1;
5137 if (argc == 0) {
5138 if (hflag)
5139 usage(hflag);
5140 /* Build an argument vector which runs a default command. */
5141 cmd = &tog_commands[0];
5142 cmd_argv = make_argv(cmd->name, NULL);
5143 argc = 1;
5144 } else {
5145 int i;
5147 /* Did the user specific a command? */
5148 for (i = 0; i < nitems(tog_commands); i++) {
5149 if (strncmp(tog_commands[i].name, argv[0],
5150 strlen(argv[0])) == 0) {
5151 cmd = &tog_commands[i];
5152 break;
5156 if (cmd == NULL) {
5157 fprintf(stderr, "%s: unknown command '%s'\n",
5158 getprogname(), argv[0]);
5159 list_commands();
5160 return 1;
5164 if (hflag)
5165 cmd->cmd_usage();
5166 else
5167 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5169 endwin();
5170 free(cmd_argv);
5171 if (error && error->code != GOT_ERR_CANCELLED)
5172 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5173 return 0;