Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <err.h>
33 #include <unistd.h>
34 #include <util.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_diff.h"
48 #include "got_opentemp.h"
49 #include "got_utf8.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
54 #include "got_path.h"
55 #include "got_worktree.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #ifndef MAX
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #endif
65 #define CTRL(x) ((x) & 0x1f)
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 struct tog_cmd {
72 const char *name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 };
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
93 };
95 enum tog_view_type {
96 TOG_VIEW_DIFF,
97 TOG_VIEW_LOG,
98 TOG_VIEW_BLAME,
99 TOG_VIEW_TREE
100 };
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
108 int idx;
109 };
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
112 int ncommits;
113 struct commit_queue_head head;
114 };
116 struct tog_color {
117 SIMPLEQ_ENTRY(tog_color) entry;
118 regex_t regex;
119 short colorpair;
120 };
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
129 int regerr = 0;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
132 return NULL;
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
137 if (tc == NULL)
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 if (regerr) {
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
145 sizeof(regerr_msg));
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 regerr_msg);
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
149 free(tc);
150 return err;
152 tc->colorpair = idx;
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 return NULL;
157 static void
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
165 regfree(&tc->regex);
166 free(tc);
170 struct tog_color *
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
177 return tc;
180 return NULL;
183 static int
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 return COLOR_CYAN;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 return COLOR_YELLOW;
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 return COLOR_GREEN;
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 return COLOR_CYAN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 return COLOR_GREEN;
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 return COLOR_GREEN;
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 return COLOR_YELLOW;
209 return -1;
212 static int
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
217 if (val == NULL)
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
221 return COLOR_BLACK;
222 if (strcasecmp(val, "red") == 0)
223 return COLOR_RED;
224 if (strcasecmp(val, "green") == 0)
225 return COLOR_GREEN;
226 if (strcasecmp(val, "yellow") == 0)
227 return COLOR_YELLOW;
228 if (strcasecmp(val, "blue") == 0)
229 return COLOR_BLUE;
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
233 return COLOR_CYAN;
234 if (strcasecmp(val, "white") == 0)
235 return COLOR_WHITE;
236 if (strcasecmp(val, "default") == 0)
237 return -1;
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
245 FILE *f;
246 int first_displayed_line;
247 int last_displayed_line;
248 int eof;
249 int diff_context;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
253 int nlines;
254 off_t *line_offsets;
255 int matched_line;
256 int selected_line;
257 size_t filesize;
259 /* passed from log view; may be NULL */
260 struct tog_view *log_view;
261 };
263 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 struct tog_log_thread_args {
266 pthread_cond_t need_commits;
267 int commits_needed;
268 struct got_commit_graph *graph;
269 struct commit_queue *commits;
270 const char *in_repo_path;
271 struct got_object_id *start_id;
272 struct got_repository *repo;
273 int log_complete;
274 sig_atomic_t *quit;
275 struct commit_queue_entry **first_displayed_entry;
276 struct commit_queue_entry **selected_entry;
277 int *searching;
278 int *search_next_done;
279 regex_t *regex;
280 };
282 struct tog_log_view_state {
283 struct commit_queue commits;
284 struct commit_queue_entry *first_displayed_entry;
285 struct commit_queue_entry *last_displayed_entry;
286 struct commit_queue_entry *selected_entry;
287 int selected;
288 char *in_repo_path;
289 const char *head_ref_name;
290 int log_branches;
291 struct got_repository *repo;
292 struct got_reflist_head *refs;
293 struct got_object_id *start_id;
294 sig_atomic_t quit;
295 pthread_t thread;
296 struct tog_log_thread_args thread_args;
297 struct commit_queue_entry *matched_entry;
298 struct commit_queue_entry *search_entry;
299 struct tog_colors colors;
300 };
302 #define TOG_COLOR_DIFF_MINUS 1
303 #define TOG_COLOR_DIFF_PLUS 2
304 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 #define TOG_COLOR_DIFF_META 4
306 #define TOG_COLOR_TREE_SUBMODULE 5
307 #define TOG_COLOR_TREE_SYMLINK 6
308 #define TOG_COLOR_TREE_DIRECTORY 7
309 #define TOG_COLOR_TREE_EXECUTABLE 8
310 #define TOG_COLOR_COMMIT 9
311 #define TOG_COLOR_AUTHOR 10
312 #define TOG_COLOR_DATE 11
314 struct tog_blame_cb_args {
315 struct tog_blame_line *lines; /* one per line */
316 int nlines;
318 struct tog_view *view;
319 struct got_object_id *commit_id;
320 int *quit;
321 };
323 struct tog_blame_thread_args {
324 const char *path;
325 struct got_repository *repo;
326 struct tog_blame_cb_args *cb_args;
327 int *complete;
328 got_cancel_cb cancel_cb;
329 void *cancel_arg;
330 };
332 struct tog_blame {
333 FILE *f;
334 size_t filesize;
335 struct tog_blame_line *lines;
336 int nlines;
337 off_t *line_offsets;
338 pthread_t thread;
339 struct tog_blame_thread_args thread_args;
340 struct tog_blame_cb_args cb_args;
341 const char *path;
342 };
344 struct tog_blame_view_state {
345 int first_displayed_line;
346 int last_displayed_line;
347 int selected_line;
348 int blame_complete;
349 int eof;
350 int done;
351 struct got_object_id_queue blamed_commits;
352 struct got_object_qid *blamed_commit;
353 char *path;
354 struct got_repository *repo;
355 struct got_reflist_head *refs;
356 struct got_object_id *commit_id;
357 struct tog_blame blame;
358 int matched_line;
359 struct tog_colors colors;
360 };
362 struct tog_parent_tree {
363 TAILQ_ENTRY(tog_parent_tree) entry;
364 struct got_tree_object *tree;
365 struct got_tree_entry *first_displayed_entry;
366 struct got_tree_entry *selected_entry;
367 int selected;
368 };
370 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 struct tog_tree_view_state {
373 char *tree_label;
374 struct got_tree_object *root;
375 struct got_tree_object *tree;
376 struct got_tree_entry *first_displayed_entry;
377 struct got_tree_entry *last_displayed_entry;
378 struct got_tree_entry *selected_entry;
379 int ndisplayed, selected, show_ids;
380 struct tog_parent_trees parents;
381 struct got_object_id *commit_id;
382 struct got_repository *repo;
383 struct got_reflist_head *refs;
384 struct got_tree_entry *matched_entry;
385 struct tog_colors colors;
386 };
388 /*
389 * We implement two types of views: parent views and child views.
391 * The 'Tab' key switches between a parent view and its child view.
392 * Child views are shown side-by-side to their parent view, provided
393 * there is enough screen estate.
395 * When a new view is opened from within a parent view, this new view
396 * becomes a child view of the parent view, replacing any existing child.
398 * When a new view is opened from within a child view, this new view
399 * becomes a parent view which will obscure the views below until the
400 * user quits the new parent view by typing 'q'.
402 * This list of views contains parent views only.
403 * Child views are only pointed to by their parent view.
404 */
405 TAILQ_HEAD(tog_view_list_head, tog_view);
407 struct tog_view {
408 TAILQ_ENTRY(tog_view) entry;
409 WINDOW *window;
410 PANEL *panel;
411 int nlines, ncols, begin_y, begin_x;
412 int lines, cols; /* copies of LINES and COLS */
413 int focussed;
414 struct tog_view *parent;
415 struct tog_view *child;
416 int child_focussed;
418 /* type-specific state */
419 enum tog_view_type type;
420 union {
421 struct tog_diff_view_state diff;
422 struct tog_log_view_state log;
423 struct tog_blame_view_state blame;
424 struct tog_tree_view_state tree;
425 } state;
427 const struct got_error *(*show)(struct tog_view *);
428 const struct got_error *(*input)(struct tog_view **,
429 struct tog_view **, struct tog_view**, struct tog_view *, int);
430 const struct got_error *(*close)(struct tog_view *);
432 const struct got_error *(*search_start)(struct tog_view *);
433 const struct got_error *(*search_next)(struct tog_view *);
434 int searching;
435 #define TOG_SEARCH_FORWARD 1
436 #define TOG_SEARCH_BACKWARD 2
437 int search_next_done;
438 regex_t regex;
439 };
441 static const struct got_error *open_diff_view(struct tog_view *,
442 struct got_object_id *, struct got_object_id *, struct tog_view *,
443 struct got_reflist_head *, struct got_repository *);
444 static const struct got_error *show_diff_view(struct tog_view *);
445 static const struct got_error *input_diff_view(struct tog_view **,
446 struct tog_view **, struct tog_view **, struct tog_view *, int);
447 static const struct got_error* close_diff_view(struct tog_view *);
448 static const struct got_error *search_start_diff_view(struct tog_view *);
449 static const struct got_error *search_next_diff_view(struct tog_view *);
451 static const struct got_error *open_log_view(struct tog_view *,
452 struct got_object_id *, struct got_reflist_head *,
453 struct got_repository *, const char *, const char *, int);
454 static const struct got_error * show_log_view(struct tog_view *);
455 static const struct got_error *input_log_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_log_view(struct tog_view *);
458 static const struct got_error *search_start_log_view(struct tog_view *);
459 static const struct got_error *search_next_log_view(struct tog_view *);
461 static const struct got_error *open_blame_view(struct tog_view *, char *,
462 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 static const struct got_error *show_blame_view(struct tog_view *);
464 static const struct got_error *input_blame_view(struct tog_view **,
465 struct tog_view **, struct tog_view **, struct tog_view *, int);
466 static const struct got_error *close_blame_view(struct tog_view *);
467 static const struct got_error *search_start_blame_view(struct tog_view *);
468 static const struct got_error *search_next_blame_view(struct tog_view *);
470 static const struct got_error *open_tree_view(struct tog_view *,
471 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
472 struct got_repository *);
473 static const struct got_error *show_tree_view(struct tog_view *);
474 static const struct got_error *input_tree_view(struct tog_view **,
475 struct tog_view **, struct tog_view **, struct tog_view *, int);
476 static const struct got_error *close_tree_view(struct tog_view *);
477 static const struct got_error *search_start_tree_view(struct tog_view *);
478 static const struct got_error *search_next_tree_view(struct tog_view *);
480 static volatile sig_atomic_t tog_sigwinch_received;
481 static volatile sig_atomic_t tog_sigpipe_received;
482 static volatile sig_atomic_t tog_sigcont_received;
484 static void
485 tog_sigwinch(int signo)
487 tog_sigwinch_received = 1;
490 static void
491 tog_sigpipe(int signo)
493 tog_sigpipe_received = 1;
496 static void
497 tog_sigcont(int signo)
499 tog_sigcont_received = 1;
502 static const struct got_error *
503 view_close(struct tog_view *view)
505 const struct got_error *err = NULL;
507 if (view->child) {
508 view_close(view->child);
509 view->child = NULL;
511 if (view->close)
512 err = view->close(view);
513 if (view->panel)
514 del_panel(view->panel);
515 if (view->window)
516 delwin(view->window);
517 free(view);
518 return err;
521 static struct tog_view *
522 view_open(int nlines, int ncols, int begin_y, int begin_x,
523 enum tog_view_type type)
525 struct tog_view *view = calloc(1, sizeof(*view));
527 if (view == NULL)
528 return NULL;
530 view->type = type;
531 view->lines = LINES;
532 view->cols = COLS;
533 view->nlines = nlines ? nlines : LINES - begin_y;
534 view->ncols = ncols ? ncols : COLS - begin_x;
535 view->begin_y = begin_y;
536 view->begin_x = begin_x;
537 view->window = newwin(nlines, ncols, begin_y, begin_x);
538 if (view->window == NULL) {
539 view_close(view);
540 return NULL;
542 view->panel = new_panel(view->window);
543 if (view->panel == NULL ||
544 set_panel_userptr(view->panel, view) != OK) {
545 view_close(view);
546 return NULL;
549 keypad(view->window, TRUE);
550 return view;
553 static int
554 view_split_begin_x(int begin_x)
556 if (begin_x > 0 || COLS < 120)
557 return 0;
558 return (COLS - MAX(COLS / 2, 80));
561 static const struct got_error *view_resize(struct tog_view *);
563 static const struct got_error *
564 view_splitscreen(struct tog_view *view)
566 const struct got_error *err = NULL;
568 view->begin_y = 0;
569 view->begin_x = view_split_begin_x(0);
570 view->nlines = LINES;
571 view->ncols = COLS - view->begin_x;
572 view->lines = LINES;
573 view->cols = COLS;
574 err = view_resize(view);
575 if (err)
576 return err;
578 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 return got_error_from_errno("mvwin");
581 return NULL;
584 static const struct got_error *
585 view_fullscreen(struct tog_view *view)
587 const struct got_error *err = NULL;
589 view->begin_x = 0;
590 view->begin_y = 0;
591 view->nlines = LINES;
592 view->ncols = COLS;
593 view->lines = LINES;
594 view->cols = COLS;
595 err = view_resize(view);
596 if (err)
597 return err;
599 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 return got_error_from_errno("mvwin");
602 return NULL;
605 static int
606 view_is_parent_view(struct tog_view *view)
608 return view->parent == NULL;
611 static const struct got_error *
612 view_resize(struct tog_view *view)
614 int nlines, ncols;
616 if (view->lines > LINES)
617 nlines = view->nlines - (view->lines - LINES);
618 else
619 nlines = view->nlines + (LINES - view->lines);
621 if (view->cols > COLS)
622 ncols = view->ncols - (view->cols - COLS);
623 else
624 ncols = view->ncols + (COLS - view->cols);
626 if (wresize(view->window, nlines, ncols) == ERR)
627 return got_error_from_errno("wresize");
628 if (replace_panel(view->panel, view->window) == ERR)
629 return got_error_from_errno("replace_panel");
630 wclear(view->window);
632 view->nlines = nlines;
633 view->ncols = ncols;
634 view->lines = LINES;
635 view->cols = COLS;
637 if (view->child) {
638 view->child->begin_x = view_split_begin_x(view->begin_x);
639 if (view->child->begin_x == 0) {
640 view_fullscreen(view->child);
641 if (view->child->focussed)
642 show_panel(view->child->panel);
643 else
644 show_panel(view->panel);
645 } else {
646 view_splitscreen(view->child);
647 show_panel(view->child->panel);
651 return NULL;
654 static const struct got_error *
655 view_close_child(struct tog_view *view)
657 const struct got_error *err = NULL;
659 if (view->child == NULL)
660 return NULL;
662 err = view_close(view->child);
663 view->child = NULL;
664 return err;
667 static const struct got_error *
668 view_set_child(struct tog_view *view, struct tog_view *child)
670 const struct got_error *err = NULL;
672 view->child = child;
673 child->parent = view;
674 return err;
677 static int
678 view_is_splitscreen(struct tog_view *view)
680 return view->begin_x > 0;
683 static void
684 tog_resizeterm(void)
686 int cols, lines;
687 struct winsize size;
689 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 cols = 80; /* Default */
691 lines = 24;
692 } else {
693 cols = size.ws_col;
694 lines = size.ws_row;
696 resize_term(lines, cols);
699 static const struct got_error *
700 view_search_start(struct tog_view *view)
702 const struct got_error *err = NULL;
703 char pattern[1024];
704 int ret;
706 if (view->nlines < 1)
707 return NULL;
709 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
710 wclrtoeol(view->window);
712 nocbreak();
713 echo();
714 ret = wgetnstr(view->window, pattern, sizeof(pattern));
715 cbreak();
716 noecho();
717 if (ret == ERR)
718 return NULL;
720 if (view->searching) {
721 regfree(&view->regex);
722 view->searching = 0;
725 if (regcomp(&view->regex, pattern,
726 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
727 err = view->search_start(view);
728 if (err) {
729 regfree(&view->regex);
730 return err;
732 view->searching = TOG_SEARCH_FORWARD;
733 view->search_next_done = 0;
734 view->search_next(view);
737 return NULL;
740 static const struct got_error *
741 view_input(struct tog_view **new, struct tog_view **dead,
742 struct tog_view **focus, int *done, struct tog_view *view,
743 struct tog_view_list_head *views)
745 const struct got_error *err = NULL;
746 struct tog_view *v;
747 int ch, errcode;
749 *new = NULL;
750 *dead = NULL;
751 *focus = NULL;
753 if (view->searching && !view->search_next_done) {
754 errcode = pthread_mutex_unlock(&tog_mutex);
755 if (errcode)
756 return got_error_set_errno(errcode,
757 "pthread_mutex_unlock");
758 pthread_yield();
759 errcode = pthread_mutex_lock(&tog_mutex);
760 if (errcode)
761 return got_error_set_errno(errcode,
762 "pthread_mutex_lock");
763 view->search_next(view);
764 return NULL;
767 nodelay(stdscr, FALSE);
768 /* Allow threads to make progress while we are waiting for input. */
769 errcode = pthread_mutex_unlock(&tog_mutex);
770 if (errcode)
771 return got_error_set_errno(errcode, "pthread_mutex_unlock");
772 ch = wgetch(view->window);
773 errcode = pthread_mutex_lock(&tog_mutex);
774 if (errcode)
775 return got_error_set_errno(errcode, "pthread_mutex_lock");
776 nodelay(stdscr, TRUE);
778 if (tog_sigwinch_received || tog_sigcont_received) {
779 tog_resizeterm();
780 tog_sigwinch_received = 0;
781 tog_sigcont_received = 0;
782 TAILQ_FOREACH(v, views, entry) {
783 err = view_resize(v);
784 if (err)
785 return err;
786 err = v->input(new, dead, focus, v, KEY_RESIZE);
787 if (err)
788 return err;
792 switch (ch) {
793 case ERR:
794 break;
795 case '\t':
796 if (view->child) {
797 *focus = view->child;
798 view->child_focussed = 1;
799 } else if (view->parent) {
800 *focus = view->parent;
801 view->parent->child_focussed = 0;
803 break;
804 case 'q':
805 err = view->input(new, dead, focus, view, ch);
806 *dead = view;
807 break;
808 case 'Q':
809 *done = 1;
810 break;
811 case 'f':
812 if (view_is_parent_view(view)) {
813 if (view->child == NULL)
814 break;
815 if (view_is_splitscreen(view->child)) {
816 *focus = view->child;
817 view->child_focussed = 1;
818 err = view_fullscreen(view->child);
819 } else
820 err = view_splitscreen(view->child);
821 if (err)
822 break;
823 err = view->child->input(new, dead, focus,
824 view->child, KEY_RESIZE);
825 } else {
826 if (view_is_splitscreen(view)) {
827 *focus = view;
828 view->parent->child_focussed = 1;
829 err = view_fullscreen(view);
830 } else {
831 err = view_splitscreen(view);
833 if (err)
834 break;
835 err = view->input(new, dead, focus, view,
836 KEY_RESIZE);
838 break;
839 case KEY_RESIZE:
840 break;
841 case '/':
842 if (view->search_start)
843 view_search_start(view);
844 else
845 err = view->input(new, dead, focus, view, ch);
846 break;
847 case 'N':
848 case 'n':
849 if (view->search_next && view->searching) {
850 view->searching = (ch == 'n' ?
851 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
852 view->search_next_done = 0;
853 view->search_next(view);
854 } else
855 err = view->input(new, dead, focus, view, ch);
856 break;
857 default:
858 err = view->input(new, dead, focus, view, ch);
859 break;
862 return err;
865 void
866 view_vborder(struct tog_view *view)
868 PANEL *panel;
869 struct tog_view *view_above;
871 if (view->parent)
872 return view_vborder(view->parent);
874 panel = panel_above(view->panel);
875 if (panel == NULL)
876 return;
878 view_above = panel_userptr(panel);
879 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
880 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
883 int
884 view_needs_focus_indication(struct tog_view *view)
886 if (view_is_parent_view(view)) {
887 if (view->child == NULL || view->child_focussed)
888 return 0;
889 if (!view_is_splitscreen(view->child))
890 return 0;
891 } else if (!view_is_splitscreen(view))
892 return 0;
894 return view->focussed;
897 static const struct got_error *
898 view_loop(struct tog_view *view)
900 const struct got_error *err = NULL;
901 struct tog_view_list_head views;
902 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
903 int fast_refresh = 10;
904 int done = 0, errcode;
906 errcode = pthread_mutex_lock(&tog_mutex);
907 if (errcode)
908 return got_error_set_errno(errcode, "pthread_mutex_lock");
910 TAILQ_INIT(&views);
911 TAILQ_INSERT_HEAD(&views, view, entry);
913 main_view = view;
914 view->focussed = 1;
915 err = view->show(view);
916 if (err)
917 return err;
918 update_panels();
919 doupdate();
920 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
921 /* Refresh fast during initialization, then become slower. */
922 if (fast_refresh && fast_refresh-- == 0)
923 halfdelay(10); /* switch to once per second */
925 err = view_input(&new_view, &dead_view, &focus_view, &done,
926 view, &views);
927 if (err)
928 break;
929 if (dead_view) {
930 struct tog_view *prev = NULL;
932 if (view_is_parent_view(dead_view))
933 prev = TAILQ_PREV(dead_view,
934 tog_view_list_head, entry);
935 else if (view->parent != dead_view)
936 prev = view->parent;
938 if (dead_view->parent)
939 dead_view->parent->child = NULL;
940 else
941 TAILQ_REMOVE(&views, dead_view, entry);
943 err = view_close(dead_view);
944 if (err || (dead_view == main_view && new_view == NULL))
945 goto done;
947 if (view == dead_view) {
948 if (focus_view)
949 view = focus_view;
950 else if (prev)
951 view = prev;
952 else if (!TAILQ_EMPTY(&views))
953 view = TAILQ_LAST(&views,
954 tog_view_list_head);
955 else
956 view = NULL;
957 if (view) {
958 if (view->child && view->child_focussed)
959 focus_view = view->child;
960 else
961 focus_view = view;
965 if (new_view) {
966 struct tog_view *v, *t;
967 /* Only allow one parent view per type. */
968 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
969 if (v->type != new_view->type)
970 continue;
971 TAILQ_REMOVE(&views, v, entry);
972 err = view_close(v);
973 if (err)
974 goto done;
975 break;
977 TAILQ_INSERT_TAIL(&views, new_view, entry);
978 view = new_view;
979 if (focus_view == NULL)
980 focus_view = new_view;
982 if (focus_view) {
983 show_panel(focus_view->panel);
984 if (view)
985 view->focussed = 0;
986 focus_view->focussed = 1;
987 view = focus_view;
988 if (new_view)
989 show_panel(new_view->panel);
990 if (view->child && view_is_splitscreen(view->child))
991 show_panel(view->child->panel);
993 if (view) {
994 if (focus_view == NULL) {
995 view->focussed = 1;
996 show_panel(view->panel);
997 if (view->child && view_is_splitscreen(view->child))
998 show_panel(view->child->panel);
999 focus_view = view;
1001 if (view->parent) {
1002 err = view->parent->show(view->parent);
1003 if (err)
1004 goto done;
1006 err = view->show(view);
1007 if (err)
1008 goto done;
1009 if (view->child) {
1010 err = view->child->show(view->child);
1011 if (err)
1012 goto done;
1014 update_panels();
1015 doupdate();
1018 done:
1019 while (!TAILQ_EMPTY(&views)) {
1020 view = TAILQ_FIRST(&views);
1021 TAILQ_REMOVE(&views, view, entry);
1022 view_close(view);
1025 errcode = pthread_mutex_unlock(&tog_mutex);
1026 if (errcode && err == NULL)
1027 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1029 return err;
1032 __dead static void
1033 usage_log(void)
1035 endwin();
1036 fprintf(stderr,
1037 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1038 getprogname());
1039 exit(1);
1042 /* Create newly allocated wide-character string equivalent to a byte string. */
1043 static const struct got_error *
1044 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1046 char *vis = NULL;
1047 const struct got_error *err = NULL;
1049 *ws = NULL;
1050 *wlen = mbstowcs(NULL, s, 0);
1051 if (*wlen == (size_t)-1) {
1052 int vislen;
1053 if (errno != EILSEQ)
1054 return got_error_from_errno("mbstowcs");
1056 /* byte string invalid in current encoding; try to "fix" it */
1057 err = got_mbsavis(&vis, &vislen, s);
1058 if (err)
1059 return err;
1060 *wlen = mbstowcs(NULL, vis, 0);
1061 if (*wlen == (size_t)-1) {
1062 err = got_error_from_errno("mbstowcs"); /* give up */
1063 goto done;
1067 *ws = calloc(*wlen + 1, sizeof(**ws));
1068 if (*ws == NULL) {
1069 err = got_error_from_errno("calloc");
1070 goto done;
1073 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1074 err = got_error_from_errno("mbstowcs");
1075 done:
1076 free(vis);
1077 if (err) {
1078 free(*ws);
1079 *ws = NULL;
1080 *wlen = 0;
1082 return err;
1085 /* Format a line for display, ensuring that it won't overflow a width limit. */
1086 static const struct got_error *
1087 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1088 int col_tab_align)
1090 const struct got_error *err = NULL;
1091 int cols = 0;
1092 wchar_t *wline = NULL;
1093 size_t wlen;
1094 int i;
1096 *wlinep = NULL;
1097 *widthp = 0;
1099 err = mbs2ws(&wline, &wlen, line);
1100 if (err)
1101 return err;
1103 i = 0;
1104 while (i < wlen) {
1105 int width = wcwidth(wline[i]);
1107 if (width == 0) {
1108 i++;
1109 continue;
1112 if (width == 1 || width == 2) {
1113 if (cols + width > wlimit)
1114 break;
1115 cols += width;
1116 i++;
1117 } else if (width == -1) {
1118 if (wline[i] == L'\t') {
1119 width = TABSIZE -
1120 ((cols + col_tab_align) % TABSIZE);
1121 if (cols + width > wlimit)
1122 break;
1123 cols += width;
1125 i++;
1126 } else {
1127 err = got_error_from_errno("wcwidth");
1128 goto done;
1131 wline[i] = L'\0';
1132 if (widthp)
1133 *widthp = cols;
1134 done:
1135 if (err)
1136 free(wline);
1137 else
1138 *wlinep = wline;
1139 return err;
1142 static const struct got_error*
1143 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1144 struct got_object_id *id, struct got_repository *repo)
1146 static const struct got_error *err = NULL;
1147 struct got_reflist_entry *re;
1148 char *s;
1149 const char *name;
1151 *refs_str = NULL;
1153 SIMPLEQ_FOREACH(re, refs, entry) {
1154 struct got_tag_object *tag = NULL;
1155 int cmp;
1157 name = got_ref_get_name(re->ref);
1158 if (strcmp(name, GOT_REF_HEAD) == 0)
1159 continue;
1160 if (strncmp(name, "refs/", 5) == 0)
1161 name += 5;
1162 if (strncmp(name, "got/", 4) == 0)
1163 continue;
1164 if (strncmp(name, "heads/", 6) == 0)
1165 name += 6;
1166 if (strncmp(name, "remotes/", 8) == 0)
1167 name += 8;
1168 if (strncmp(name, "tags/", 5) == 0) {
1169 err = got_object_open_as_tag(&tag, repo, re->id);
1170 if (err) {
1171 if (err->code != GOT_ERR_OBJ_TYPE)
1172 break;
1173 /* Ref points at something other than a tag. */
1174 err = NULL;
1175 tag = NULL;
1178 cmp = got_object_id_cmp(tag ?
1179 got_object_tag_get_object_id(tag) : re->id, id);
1180 if (tag)
1181 got_object_tag_close(tag);
1182 if (cmp != 0)
1183 continue;
1184 s = *refs_str;
1185 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1186 s ? ", " : "", name) == -1) {
1187 err = got_error_from_errno("asprintf");
1188 free(s);
1189 *refs_str = NULL;
1190 break;
1192 free(s);
1195 return err;
1198 static const struct got_error *
1199 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1200 int col_tab_align)
1202 char *smallerthan, *at;
1204 smallerthan = strchr(author, '<');
1205 if (smallerthan && smallerthan[1] != '\0')
1206 author = smallerthan + 1;
1207 at = strchr(author, '@');
1208 if (at)
1209 *at = '\0';
1210 return format_line(wauthor, author_width, author, limit, col_tab_align);
1213 static const struct got_error *
1214 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1215 struct got_object_id *id, struct got_reflist_head *refs,
1216 const size_t date_display_cols, int author_display_cols,
1217 struct tog_colors *colors)
1219 const struct got_error *err = NULL;
1220 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1221 char *logmsg0 = NULL, *logmsg = NULL;
1222 char *author = NULL;
1223 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1224 int author_width, logmsg_width;
1225 char *newline, *line = NULL;
1226 int col, limit;
1227 const int avail = view->ncols;
1228 struct tm tm;
1229 time_t committer_time;
1230 struct tog_color *tc;
1232 committer_time = got_object_commit_get_committer_time(commit);
1233 if (localtime_r(&committer_time, &tm) == NULL)
1234 return got_error_from_errno("localtime_r");
1235 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1236 >= sizeof(datebuf))
1237 return got_error(GOT_ERR_NO_SPACE);
1239 if (avail <= date_display_cols)
1240 limit = MIN(sizeof(datebuf) - 1, avail);
1241 else
1242 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1243 tc = get_color(colors, TOG_COLOR_DATE);
1244 if (tc)
1245 wattr_on(view->window,
1246 COLOR_PAIR(tc->colorpair), NULL);
1247 waddnstr(view->window, datebuf, limit);
1248 if (tc)
1249 wattr_off(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 col = limit;
1252 if (col > avail)
1253 goto done;
1255 if (avail >= 120) {
1256 char *id_str;
1257 err = got_object_id_str(&id_str, id);
1258 if (err)
1259 goto done;
1260 tc = get_color(colors, TOG_COLOR_COMMIT);
1261 if (tc)
1262 wattr_on(view->window,
1263 COLOR_PAIR(tc->colorpair), NULL);
1264 wprintw(view->window, "%.8s ", id_str);
1265 if (tc)
1266 wattr_off(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1268 free(id_str);
1269 col += 9;
1270 if (col > avail)
1271 goto done;
1274 author = strdup(got_object_commit_get_author(commit));
1275 if (author == NULL) {
1276 err = got_error_from_errno("strdup");
1277 goto done;
1279 err = format_author(&wauthor, &author_width, author, avail - col, col);
1280 if (err)
1281 goto done;
1282 tc = get_color(colors, TOG_COLOR_AUTHOR);
1283 if (tc)
1284 wattr_on(view->window,
1285 COLOR_PAIR(tc->colorpair), NULL);
1286 waddwstr(view->window, wauthor);
1287 if (tc)
1288 wattr_off(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 col += author_width;
1291 while (col < avail && author_width < author_display_cols + 2) {
1292 waddch(view->window, ' ');
1293 col++;
1294 author_width++;
1296 if (col > avail)
1297 goto done;
1299 err = got_object_commit_get_logmsg(&logmsg0, commit);
1300 if (err)
1301 goto done;
1302 logmsg = logmsg0;
1303 while (*logmsg == '\n')
1304 logmsg++;
1305 newline = strchr(logmsg, '\n');
1306 if (newline)
1307 *newline = '\0';
1308 limit = avail - col;
1309 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1310 if (err)
1311 goto done;
1312 waddwstr(view->window, wlogmsg);
1313 col += logmsg_width;
1314 while (col < avail) {
1315 waddch(view->window, ' ');
1316 col++;
1318 done:
1319 free(logmsg0);
1320 free(wlogmsg);
1321 free(author);
1322 free(wauthor);
1323 free(line);
1324 return err;
1327 static struct commit_queue_entry *
1328 alloc_commit_queue_entry(struct got_commit_object *commit,
1329 struct got_object_id *id)
1331 struct commit_queue_entry *entry;
1333 entry = calloc(1, sizeof(*entry));
1334 if (entry == NULL)
1335 return NULL;
1337 entry->id = id;
1338 entry->commit = commit;
1339 return entry;
1342 static void
1343 pop_commit(struct commit_queue *commits)
1345 struct commit_queue_entry *entry;
1347 entry = TAILQ_FIRST(&commits->head);
1348 TAILQ_REMOVE(&commits->head, entry, entry);
1349 got_object_commit_close(entry->commit);
1350 commits->ncommits--;
1351 /* Don't free entry->id! It is owned by the commit graph. */
1352 free(entry);
1355 static void
1356 free_commits(struct commit_queue *commits)
1358 while (!TAILQ_EMPTY(&commits->head))
1359 pop_commit(commits);
1362 static const struct got_error *
1363 match_commit(int *have_match, struct got_object_id *id,
1364 struct got_commit_object *commit, regex_t *regex)
1366 const struct got_error *err = NULL;
1367 regmatch_t regmatch;
1368 char *id_str = NULL, *logmsg = NULL;
1370 *have_match = 0;
1372 err = got_object_id_str(&id_str, id);
1373 if (err)
1374 return err;
1376 err = got_object_commit_get_logmsg(&logmsg, commit);
1377 if (err)
1378 goto done;
1380 if (regexec(regex, got_object_commit_get_author(commit), 1,
1381 &regmatch, 0) == 0 ||
1382 regexec(regex, got_object_commit_get_committer(commit), 1,
1383 &regmatch, 0) == 0 ||
1384 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1385 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1386 *have_match = 1;
1387 done:
1388 free(id_str);
1389 free(logmsg);
1390 return err;
1393 static const struct got_error *
1394 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1395 int minqueue, struct got_repository *repo, const char *path,
1396 int *searching, int *search_next_done, regex_t *regex)
1398 const struct got_error *err = NULL;
1399 int nqueued = 0, have_match = 0;
1402 * We keep all commits open throughout the lifetime of the log
1403 * view in order to avoid having to re-fetch commits from disk
1404 * while updating the display.
1406 while (nqueued < minqueue ||
1407 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1408 struct got_object_id *id;
1409 struct got_commit_object *commit;
1410 struct commit_queue_entry *entry;
1411 int errcode;
1413 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1414 if (err || id == NULL)
1415 break;
1417 err = got_object_open_as_commit(&commit, repo, id);
1418 if (err)
1419 break;
1420 entry = alloc_commit_queue_entry(commit, id);
1421 if (entry == NULL) {
1422 err = got_error_from_errno("alloc_commit_queue_entry");
1423 break;
1426 errcode = pthread_mutex_lock(&tog_mutex);
1427 if (errcode) {
1428 err = got_error_set_errno(errcode,
1429 "pthread_mutex_lock");
1430 break;
1433 entry->idx = commits->ncommits;
1434 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1435 nqueued++;
1436 commits->ncommits++;
1438 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1439 err = match_commit(&have_match, id, commit, regex);
1442 errcode = pthread_mutex_unlock(&tog_mutex);
1443 if (errcode && err == NULL)
1444 err = got_error_set_errno(errcode,
1445 "pthread_mutex_unlock");
1447 if (err || have_match)
1448 break;
1451 return err;
1454 static const struct got_error *
1455 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1456 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1457 struct commit_queue *commits, int selected_idx, int limit,
1458 struct got_reflist_head *refs, const char *path, int commits_needed,
1459 struct tog_colors *colors)
1461 const struct got_error *err = NULL;
1462 struct tog_log_view_state *s = &view->state.log;
1463 struct commit_queue_entry *entry;
1464 int width;
1465 int ncommits, author_cols = 4;
1466 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1467 char *refs_str = NULL;
1468 wchar_t *wline;
1469 struct tog_color *tc;
1470 static const size_t date_display_cols = 12;
1472 entry = first;
1473 ncommits = 0;
1474 while (entry) {
1475 if (ncommits == selected_idx) {
1476 *selected = entry;
1477 break;
1479 entry = TAILQ_NEXT(entry, entry);
1480 ncommits++;
1483 if (*selected && !(view->searching && view->search_next_done == 0)) {
1484 err = got_object_id_str(&id_str, (*selected)->id);
1485 if (err)
1486 return err;
1487 if (refs) {
1488 err = build_refs_str(&refs_str, refs, (*selected)->id,
1489 s->repo);
1490 if (err)
1491 goto done;
1495 if (commits_needed == 0)
1496 halfdelay(10); /* disable fast refresh */
1498 if (asprintf(&ncommits_str, " [%d/%d] %s",
1499 entry ? entry->idx + 1 : 0, commits->ncommits,
1500 commits_needed > 0 ?
1501 (view->searching && view->search_next_done == 0
1502 ? "searching..." : "loading... ") :
1503 (refs_str ? refs_str : "")) == -1) {
1504 err = got_error_from_errno("asprintf");
1505 goto done;
1508 if (path && strcmp(path, "/") != 0) {
1509 if (asprintf(&header, "commit %s %s%s",
1510 id_str ? id_str : "........................................",
1511 path, ncommits_str) == -1) {
1512 err = got_error_from_errno("asprintf");
1513 header = NULL;
1514 goto done;
1516 } else if (asprintf(&header, "commit %s%s",
1517 id_str ? id_str : "........................................",
1518 ncommits_str) == -1) {
1519 err = got_error_from_errno("asprintf");
1520 header = NULL;
1521 goto done;
1523 err = format_line(&wline, &width, header, view->ncols, 0);
1524 if (err)
1525 goto done;
1527 werase(view->window);
1529 if (view_needs_focus_indication(view))
1530 wstandout(view->window);
1531 tc = get_color(colors, TOG_COLOR_COMMIT);
1532 if (tc)
1533 wattr_on(view->window,
1534 COLOR_PAIR(tc->colorpair), NULL);
1535 waddwstr(view->window, wline);
1536 if (tc)
1537 wattr_off(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 while (width < view->ncols) {
1540 waddch(view->window, ' ');
1541 width++;
1543 if (view_needs_focus_indication(view))
1544 wstandend(view->window);
1545 free(wline);
1546 if (limit <= 1)
1547 goto done;
1549 /* Grow author column size if necessary. */
1550 entry = first;
1551 ncommits = 0;
1552 while (entry) {
1553 char *author;
1554 wchar_t *wauthor;
1555 int width;
1556 if (ncommits >= limit - 1)
1557 break;
1558 author = strdup(got_object_commit_get_author(entry->commit));
1559 if (author == NULL) {
1560 err = got_error_from_errno("strdup");
1561 goto done;
1563 err = format_author(&wauthor, &width, author, COLS,
1564 date_display_cols);
1565 if (author_cols < width)
1566 author_cols = width;
1567 free(wauthor);
1568 free(author);
1569 ncommits++;
1570 entry = TAILQ_NEXT(entry, entry);
1573 entry = first;
1574 *last = first;
1575 ncommits = 0;
1576 while (entry) {
1577 if (ncommits >= limit - 1)
1578 break;
1579 if (ncommits == selected_idx)
1580 wstandout(view->window);
1581 err = draw_commit(view, entry->commit, entry->id, refs,
1582 date_display_cols, author_cols, colors);
1583 if (ncommits == selected_idx)
1584 wstandend(view->window);
1585 if (err)
1586 goto done;
1587 ncommits++;
1588 *last = entry;
1589 entry = TAILQ_NEXT(entry, entry);
1592 view_vborder(view);
1593 done:
1594 free(id_str);
1595 free(refs_str);
1596 free(ncommits_str);
1597 free(header);
1598 return err;
1601 static void
1602 scroll_up(struct tog_view *view,
1603 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1604 struct commit_queue *commits)
1606 struct commit_queue_entry *entry;
1607 int nscrolled = 0;
1609 entry = TAILQ_FIRST(&commits->head);
1610 if (*first_displayed_entry == entry)
1611 return;
1613 entry = *first_displayed_entry;
1614 while (entry && nscrolled < maxscroll) {
1615 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1616 if (entry) {
1617 *first_displayed_entry = entry;
1618 nscrolled++;
1623 static const struct got_error *
1624 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1625 pthread_cond_t *need_commits)
1627 int errcode;
1628 int max_wait = 20;
1630 halfdelay(1); /* fast refresh while loading commits */
1632 while (*commits_needed > 0) {
1633 if (*log_complete)
1634 break;
1636 /* Wake the log thread. */
1637 errcode = pthread_cond_signal(need_commits);
1638 if (errcode)
1639 return got_error_set_errno(errcode,
1640 "pthread_cond_signal");
1641 errcode = pthread_mutex_unlock(&tog_mutex);
1642 if (errcode)
1643 return got_error_set_errno(errcode,
1644 "pthread_mutex_unlock");
1645 pthread_yield();
1646 errcode = pthread_mutex_lock(&tog_mutex);
1647 if (errcode)
1648 return got_error_set_errno(errcode,
1649 "pthread_mutex_lock");
1651 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1653 * Thread is not done yet; lose a key press
1654 * and let the user retry... this way the GUI
1655 * remains interactive while logging deep paths
1656 * with few commits in history.
1658 return NULL;
1662 return NULL;
1665 static const struct got_error *
1666 scroll_down(struct tog_view *view,
1667 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1668 struct commit_queue_entry **last_displayed_entry,
1669 struct commit_queue *commits, int *log_complete, int *commits_needed,
1670 pthread_cond_t *need_commits)
1672 const struct got_error *err = NULL;
1673 struct commit_queue_entry *pentry;
1674 int nscrolled = 0;
1676 if (*last_displayed_entry == NULL)
1677 return NULL;
1679 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1680 if (pentry == NULL && !*log_complete) {
1682 * Ask the log thread for required amount of commits
1683 * plus some amount of pre-fetching.
1685 (*commits_needed) += maxscroll + 20;
1686 err = trigger_log_thread(0, commits_needed, log_complete,
1687 need_commits);
1688 if (err)
1689 return err;
1692 do {
1693 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1694 if (pentry == NULL)
1695 break;
1697 *last_displayed_entry = pentry;
1699 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1700 if (pentry == NULL)
1701 break;
1702 *first_displayed_entry = pentry;
1703 } while (++nscrolled < maxscroll);
1705 return err;
1708 static const struct got_error *
1709 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1710 struct got_commit_object *commit, struct got_object_id *commit_id,
1711 struct tog_view *log_view, struct got_reflist_head *refs,
1712 struct got_repository *repo)
1714 const struct got_error *err;
1715 struct got_object_qid *parent_id;
1716 struct tog_view *diff_view;
1718 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1719 if (diff_view == NULL)
1720 return got_error_from_errno("view_open");
1722 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1723 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1724 commit_id, log_view, refs, repo);
1725 if (err == NULL)
1726 *new_view = diff_view;
1727 return err;
1730 static const struct got_error *
1731 tree_view_visit_subtree(struct got_tree_object *subtree,
1732 struct tog_tree_view_state *s)
1734 struct tog_parent_tree *parent;
1736 parent = calloc(1, sizeof(*parent));
1737 if (parent == NULL)
1738 return got_error_from_errno("calloc");
1740 parent->tree = s->tree;
1741 parent->first_displayed_entry = s->first_displayed_entry;
1742 parent->selected_entry = s->selected_entry;
1743 parent->selected = s->selected;
1744 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1745 s->tree = subtree;
1746 s->selected = 0;
1747 s->first_displayed_entry = NULL;
1748 return NULL;
1751 static const struct got_error *
1752 tree_view_walk_path(struct tog_tree_view_state *s,
1753 struct got_object_id *commit_id,
1754 const char *path, struct got_repository *repo)
1756 const struct got_error *err = NULL;
1757 struct got_tree_object *tree = NULL;
1758 const char *p;
1759 char *slash, *subpath = NULL;
1761 /* Walk the path and open corresponding tree objects. */
1762 p = path;
1763 while (*p) {
1764 struct got_tree_entry *te;
1765 struct got_object_id *tree_id;
1766 char *te_name;
1768 while (p[0] == '/')
1769 p++;
1771 /* Ensure the correct subtree entry is selected. */
1772 slash = strchr(p, '/');
1773 if (slash == NULL)
1774 te_name = strdup(p);
1775 else
1776 te_name = strndup(p, slash - p);
1777 if (te_name == NULL) {
1778 err = got_error_from_errno("strndup");
1779 break;
1781 te = got_object_tree_find_entry(s->tree, te_name);
1782 if (te == NULL) {
1783 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1784 free(te_name);
1785 break;
1787 free(te_name);
1788 s->selected_entry = te;
1789 s->selected = got_tree_entry_get_index(te);
1790 if (s->tree != s->root)
1791 s->selected++; /* skip '..' */
1793 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1794 /* Jump to this file's entry. */
1795 s->first_displayed_entry = s->selected_entry;
1796 s->selected = 0;
1797 break;
1800 slash = strchr(p, '/');
1801 if (slash)
1802 subpath = strndup(path, slash - path);
1803 else
1804 subpath = strdup(path);
1805 if (subpath == NULL) {
1806 err = got_error_from_errno("strdup");
1807 break;
1810 err = got_object_id_by_path(&tree_id, repo, commit_id,
1811 subpath);
1812 if (err)
1813 break;
1815 err = got_object_open_as_tree(&tree, repo, tree_id);
1816 free(tree_id);
1817 if (err)
1818 break;
1820 err = tree_view_visit_subtree(tree, s);
1821 if (err) {
1822 got_object_tree_close(tree);
1823 break;
1825 if (slash == NULL)
1826 break;
1827 free(subpath);
1828 subpath = NULL;
1829 p = slash;
1832 free(subpath);
1833 return err;
1836 static const struct got_error *
1837 browse_commit_tree(struct tog_view **new_view, int begin_x,
1838 struct commit_queue_entry *entry, const char *path,
1839 struct got_reflist_head *refs, struct got_repository *repo)
1841 const struct got_error *err = NULL;
1842 struct got_tree_object *tree;
1843 struct tog_tree_view_state *s;
1844 struct tog_view *tree_view;
1846 err = got_object_open_as_tree(&tree, repo,
1847 got_object_commit_get_tree_id(entry->commit));
1848 if (err)
1849 return err;
1851 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1852 if (tree_view == NULL)
1853 return got_error_from_errno("view_open");
1855 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1856 if (err) {
1857 got_object_tree_close(tree);
1858 return err;
1860 s = &tree_view->state.tree;
1862 *new_view = tree_view;
1864 if (got_path_is_root_dir(path))
1865 return NULL;
1867 return tree_view_walk_path(s, entry->id, path, repo);
1870 static const struct got_error *
1871 block_signals_used_by_main_thread(void)
1873 sigset_t sigset;
1874 int errcode;
1876 if (sigemptyset(&sigset) == -1)
1877 return got_error_from_errno("sigemptyset");
1879 /* tog handles SIGWINCH and SIGCONT */
1880 if (sigaddset(&sigset, SIGWINCH) == -1)
1881 return got_error_from_errno("sigaddset");
1882 if (sigaddset(&sigset, SIGCONT) == -1)
1883 return got_error_from_errno("sigaddset");
1885 /* ncurses handles SIGTSTP */
1886 if (sigaddset(&sigset, SIGTSTP) == -1)
1887 return got_error_from_errno("sigaddset");
1889 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1890 if (errcode)
1891 return got_error_set_errno(errcode, "pthread_sigmask");
1893 return NULL;
1896 static void *
1897 log_thread(void *arg)
1899 const struct got_error *err = NULL;
1900 int errcode = 0;
1901 struct tog_log_thread_args *a = arg;
1902 int done = 0;
1904 err = block_signals_used_by_main_thread();
1905 if (err)
1906 return (void *)err;
1908 while (!done && !err && !tog_sigpipe_received) {
1909 err = queue_commits(a->graph, a->commits, 1, a->repo,
1910 a->in_repo_path, a->searching, a->search_next_done,
1911 a->regex);
1912 if (err) {
1913 if (err->code != GOT_ERR_ITER_COMPLETED)
1914 return (void *)err;
1915 err = NULL;
1916 done = 1;
1917 } else if (a->commits_needed > 0)
1918 a->commits_needed--;
1920 errcode = pthread_mutex_lock(&tog_mutex);
1921 if (errcode) {
1922 err = got_error_set_errno(errcode,
1923 "pthread_mutex_lock");
1924 break;
1925 } else if (*a->quit)
1926 done = 1;
1927 else if (*a->first_displayed_entry == NULL) {
1928 *a->first_displayed_entry =
1929 TAILQ_FIRST(&a->commits->head);
1930 *a->selected_entry = *a->first_displayed_entry;
1933 if (done)
1934 a->commits_needed = 0;
1935 else if (a->commits_needed == 0) {
1936 errcode = pthread_cond_wait(&a->need_commits,
1937 &tog_mutex);
1938 if (errcode)
1939 err = got_error_set_errno(errcode,
1940 "pthread_cond_wait");
1943 errcode = pthread_mutex_unlock(&tog_mutex);
1944 if (errcode && err == NULL)
1945 err = got_error_set_errno(errcode,
1946 "pthread_mutex_unlock");
1948 a->log_complete = 1;
1949 return (void *)err;
1952 static const struct got_error *
1953 stop_log_thread(struct tog_log_view_state *s)
1955 const struct got_error *err = NULL;
1956 int errcode;
1958 if (s->thread) {
1959 s->quit = 1;
1960 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1961 if (errcode)
1962 return got_error_set_errno(errcode,
1963 "pthread_cond_signal");
1964 errcode = pthread_mutex_unlock(&tog_mutex);
1965 if (errcode)
1966 return got_error_set_errno(errcode,
1967 "pthread_mutex_unlock");
1968 errcode = pthread_join(s->thread, (void **)&err);
1969 if (errcode)
1970 return got_error_set_errno(errcode, "pthread_join");
1971 errcode = pthread_mutex_lock(&tog_mutex);
1972 if (errcode)
1973 return got_error_set_errno(errcode,
1974 "pthread_mutex_lock");
1975 s->thread = NULL;
1978 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1979 if (errcode && err == NULL)
1980 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1982 if (s->thread_args.repo) {
1983 got_repo_close(s->thread_args.repo);
1984 s->thread_args.repo = NULL;
1987 if (s->thread_args.graph) {
1988 got_commit_graph_close(s->thread_args.graph);
1989 s->thread_args.graph = NULL;
1992 return err;
1995 static const struct got_error *
1996 close_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2001 err = stop_log_thread(s);
2002 free_commits(&s->commits);
2003 free(s->in_repo_path);
2004 s->in_repo_path = NULL;
2005 free(s->start_id);
2006 s->start_id = NULL;
2007 return err;
2010 static const struct got_error *
2011 search_start_log_view(struct tog_view *view)
2013 struct tog_log_view_state *s = &view->state.log;
2015 s->matched_entry = NULL;
2016 s->search_entry = NULL;
2017 return NULL;
2020 static const struct got_error *
2021 search_next_log_view(struct tog_view *view)
2023 const struct got_error *err = NULL;
2024 struct tog_log_view_state *s = &view->state.log;
2025 struct commit_queue_entry *entry;
2027 if (!view->searching) {
2028 view->search_next_done = 1;
2029 return NULL;
2032 if (s->search_entry) {
2033 int errcode, ch;
2034 errcode = pthread_mutex_unlock(&tog_mutex);
2035 if (errcode)
2036 return got_error_set_errno(errcode,
2037 "pthread_mutex_unlock");
2038 ch = wgetch(view->window);
2039 errcode = pthread_mutex_lock(&tog_mutex);
2040 if (errcode)
2041 return got_error_set_errno(errcode,
2042 "pthread_mutex_lock");
2043 if (ch == KEY_BACKSPACE) {
2044 view->search_next_done = 1;
2045 return NULL;
2047 if (view->searching == TOG_SEARCH_FORWARD)
2048 entry = TAILQ_NEXT(s->search_entry, entry);
2049 else
2050 entry = TAILQ_PREV(s->search_entry,
2051 commit_queue_head, entry);
2052 } else if (s->matched_entry) {
2053 if (view->searching == TOG_SEARCH_FORWARD)
2054 entry = TAILQ_NEXT(s->selected_entry, entry);
2055 else
2056 entry = TAILQ_PREV(s->selected_entry,
2057 commit_queue_head, entry);
2058 } else {
2059 if (view->searching == TOG_SEARCH_FORWARD)
2060 entry = TAILQ_FIRST(&s->commits.head);
2061 else
2062 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2065 while (1) {
2066 int have_match = 0;
2068 if (entry == NULL) {
2069 if (s->thread_args.log_complete ||
2070 view->searching == TOG_SEARCH_BACKWARD) {
2071 view->search_next_done = 1;
2072 return NULL;
2075 * Poke the log thread for more commits and return,
2076 * allowing the main loop to make progress. Search
2077 * will resume at s->search_entry once we come back.
2079 s->thread_args.commits_needed++;
2080 return trigger_log_thread(1,
2081 &s->thread_args.commits_needed,
2082 &s->thread_args.log_complete,
2083 &s->thread_args.need_commits);
2086 err = match_commit(&have_match, entry->id, entry->commit,
2087 &view->regex);
2088 if (err)
2089 break;
2090 if (have_match) {
2091 view->search_next_done = 1;
2092 s->matched_entry = entry;
2093 break;
2096 s->search_entry = entry;
2097 if (view->searching == TOG_SEARCH_FORWARD)
2098 entry = TAILQ_NEXT(entry, entry);
2099 else
2100 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2103 if (s->matched_entry) {
2104 int cur = s->selected_entry->idx;
2105 while (cur < s->matched_entry->idx) {
2106 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2107 if (err)
2108 return err;
2109 cur++;
2111 while (cur > s->matched_entry->idx) {
2112 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2113 if (err)
2114 return err;
2115 cur--;
2119 s->search_entry = NULL;
2121 return NULL;
2124 static const struct got_error *
2125 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2126 struct got_reflist_head *refs, struct got_repository *repo,
2127 const char *head_ref_name, const char *in_repo_path,
2128 int log_branches)
2130 const struct got_error *err = NULL;
2131 struct tog_log_view_state *s = &view->state.log;
2132 struct got_repository *thread_repo = NULL;
2133 struct got_commit_graph *thread_graph = NULL;
2134 int errcode;
2136 if (in_repo_path != s->in_repo_path) {
2137 free(s->in_repo_path);
2138 s->in_repo_path = strdup(in_repo_path);
2139 if (s->in_repo_path == NULL)
2140 return got_error_from_errno("strdup");
2143 /* The commit queue only contains commits being displayed. */
2144 TAILQ_INIT(&s->commits.head);
2145 s->commits.ncommits = 0;
2147 s->refs = refs;
2148 s->repo = repo;
2149 s->head_ref_name = head_ref_name;
2150 s->start_id = got_object_id_dup(start_id);
2151 if (s->start_id == NULL) {
2152 err = got_error_from_errno("got_object_id_dup");
2153 goto done;
2155 s->log_branches = log_branches;
2157 SIMPLEQ_INIT(&s->colors);
2158 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2159 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2160 get_color_value("TOG_COLOR_COMMIT"));
2161 if (err)
2162 goto done;
2163 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2164 get_color_value("TOG_COLOR_AUTHOR"));
2165 if (err) {
2166 free_colors(&s->colors);
2167 goto done;
2169 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2170 get_color_value("TOG_COLOR_DATE"));
2171 if (err) {
2172 free_colors(&s->colors);
2173 goto done;
2177 view->show = show_log_view;
2178 view->input = input_log_view;
2179 view->close = close_log_view;
2180 view->search_start = search_start_log_view;
2181 view->search_next = search_next_log_view;
2183 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2184 if (err)
2185 goto done;
2186 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2187 !s->log_branches);
2188 if (err)
2189 goto done;
2190 err = got_commit_graph_iter_start(thread_graph,
2191 s->start_id, s->repo, NULL, NULL);
2192 if (err)
2193 goto done;
2195 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2196 if (errcode) {
2197 err = got_error_set_errno(errcode, "pthread_cond_init");
2198 goto done;
2201 s->thread_args.commits_needed = view->nlines;
2202 s->thread_args.graph = thread_graph;
2203 s->thread_args.commits = &s->commits;
2204 s->thread_args.in_repo_path = s->in_repo_path;
2205 s->thread_args.start_id = s->start_id;
2206 s->thread_args.repo = thread_repo;
2207 s->thread_args.log_complete = 0;
2208 s->thread_args.quit = &s->quit;
2209 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2210 s->thread_args.selected_entry = &s->selected_entry;
2211 s->thread_args.searching = &view->searching;
2212 s->thread_args.search_next_done = &view->search_next_done;
2213 s->thread_args.regex = &view->regex;
2214 done:
2215 if (err)
2216 close_log_view(view);
2217 return err;
2220 static const struct got_error *
2221 show_log_view(struct tog_view *view)
2223 struct tog_log_view_state *s = &view->state.log;
2225 if (s->thread == NULL) {
2226 int errcode = pthread_create(&s->thread, NULL, log_thread,
2227 &s->thread_args);
2228 if (errcode)
2229 return got_error_set_errno(errcode, "pthread_create");
2232 return draw_commits(view, &s->last_displayed_entry,
2233 &s->selected_entry, s->first_displayed_entry,
2234 &s->commits, s->selected, view->nlines, s->refs,
2235 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2238 static const struct got_error *
2239 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2240 struct tog_view **focus_view, struct tog_view *view, int ch)
2242 const struct got_error *err = NULL;
2243 struct tog_log_view_state *s = &view->state.log;
2244 char *parent_path, *in_repo_path = NULL;
2245 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2246 int begin_x = 0;
2247 struct got_object_id *start_id;
2249 switch (ch) {
2250 case 'q':
2251 s->quit = 1;
2252 break;
2253 case 'k':
2254 case KEY_UP:
2255 case '<':
2256 case ',':
2257 if (s->first_displayed_entry == NULL)
2258 break;
2259 if (s->selected > 0)
2260 s->selected--;
2261 else
2262 scroll_up(view, &s->first_displayed_entry, 1,
2263 &s->commits);
2264 break;
2265 case KEY_PPAGE:
2266 case CTRL('b'):
2267 if (s->first_displayed_entry == NULL)
2268 break;
2269 if (TAILQ_FIRST(&s->commits.head) ==
2270 s->first_displayed_entry) {
2271 s->selected = 0;
2272 break;
2274 scroll_up(view, &s->first_displayed_entry,
2275 view->nlines, &s->commits);
2276 break;
2277 case 'j':
2278 case KEY_DOWN:
2279 case '>':
2280 case '.':
2281 if (s->first_displayed_entry == NULL)
2282 break;
2283 if (s->selected < MIN(view->nlines - 2,
2284 s->commits.ncommits - 1)) {
2285 s->selected++;
2286 break;
2288 err = scroll_down(view, &s->first_displayed_entry, 1,
2289 &s->last_displayed_entry, &s->commits,
2290 &s->thread_args.log_complete,
2291 &s->thread_args.commits_needed,
2292 &s->thread_args.need_commits);
2293 break;
2294 case KEY_NPAGE:
2295 case CTRL('f'): {
2296 struct commit_queue_entry *first;
2297 first = s->first_displayed_entry;
2298 if (first == NULL)
2299 break;
2300 err = scroll_down(view, &s->first_displayed_entry,
2301 view->nlines, &s->last_displayed_entry,
2302 &s->commits, &s->thread_args.log_complete,
2303 &s->thread_args.commits_needed,
2304 &s->thread_args.need_commits);
2305 if (err)
2306 break;
2307 if (first == s->first_displayed_entry &&
2308 s->selected < MIN(view->nlines - 2,
2309 s->commits.ncommits - 1)) {
2310 /* can't scroll further down */
2311 s->selected = MIN(view->nlines - 2,
2312 s->commits.ncommits - 1);
2314 err = NULL;
2315 break;
2317 case KEY_RESIZE:
2318 if (s->selected > view->nlines - 2)
2319 s->selected = view->nlines - 2;
2320 if (s->selected > s->commits.ncommits - 1)
2321 s->selected = s->commits.ncommits - 1;
2322 break;
2323 case KEY_ENTER:
2324 case ' ':
2325 case '\r':
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 = open_diff_view_for_commit(&diff_view, begin_x,
2331 s->selected_entry->commit, s->selected_entry->id,
2332 view, s->refs, s->repo);
2333 if (err)
2334 break;
2335 if (view_is_parent_view(view)) {
2336 err = view_close_child(view);
2337 if (err)
2338 return err;
2339 err = view_set_child(view, diff_view);
2340 if (err) {
2341 view_close(diff_view);
2342 break;
2344 *focus_view = diff_view;
2345 view->child_focussed = 1;
2346 } else
2347 *new_view = diff_view;
2348 break;
2349 case 't':
2350 if (s->selected_entry == NULL)
2351 break;
2352 if (view_is_parent_view(view))
2353 begin_x = view_split_begin_x(view->begin_x);
2354 err = browse_commit_tree(&tree_view, begin_x,
2355 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2356 if (err)
2357 break;
2358 if (view_is_parent_view(view)) {
2359 err = view_close_child(view);
2360 if (err)
2361 return err;
2362 err = view_set_child(view, tree_view);
2363 if (err) {
2364 view_close(tree_view);
2365 break;
2367 *focus_view = tree_view;
2368 view->child_focussed = 1;
2369 } else
2370 *new_view = tree_view;
2371 break;
2372 case KEY_BACKSPACE:
2373 if (strcmp(s->in_repo_path, "/") == 0)
2374 break;
2375 parent_path = dirname(s->in_repo_path);
2376 if (parent_path && strcmp(parent_path, ".") != 0) {
2377 err = stop_log_thread(s);
2378 if (err)
2379 return err;
2380 lv = view_open(view->nlines, view->ncols,
2381 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2382 if (lv == NULL)
2383 return got_error_from_errno(
2384 "view_open");
2385 err = open_log_view(lv, s->start_id, s->refs,
2386 s->repo, s->head_ref_name, parent_path,
2387 s->log_branches);
2388 if (err)
2389 return err;;
2390 if (view_is_parent_view(view))
2391 *new_view = lv;
2392 else {
2393 view_set_child(view->parent, lv);
2394 *focus_view = lv;
2396 return NULL;
2398 break;
2399 case CTRL('l'):
2400 err = stop_log_thread(s);
2401 if (err)
2402 return err;
2403 lv = view_open(view->nlines, view->ncols,
2404 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2405 if (lv == NULL)
2406 return got_error_from_errno("view_open");
2407 err = got_repo_match_object_id(&start_id, NULL,
2408 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2409 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2410 if (err) {
2411 view_close(lv);
2412 return err;
2414 in_repo_path = strdup(s->in_repo_path);
2415 if (in_repo_path == NULL) {
2416 free(start_id);
2417 view_close(lv);
2418 return got_error_from_errno("strdup");
2420 got_ref_list_free(s->refs);
2421 err = got_ref_list(s->refs, s->repo, NULL,
2422 got_ref_cmp_by_name, NULL);
2423 if (err) {
2424 free(start_id);
2425 view_close(lv);
2426 return err;
2428 err = open_log_view(lv, start_id, s->refs, s->repo,
2429 s->head_ref_name, in_repo_path, s->log_branches);
2430 if (err) {
2431 free(start_id);
2432 view_close(lv);
2433 return err;;
2435 *dead_view = view;
2436 *new_view = lv;
2437 break;
2438 case 'B':
2439 s->log_branches = !s->log_branches;
2440 err = stop_log_thread(s);
2441 if (err)
2442 return err;
2443 lv = view_open(view->nlines, view->ncols,
2444 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2445 if (lv == NULL)
2446 return got_error_from_errno("view_open");
2447 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2448 s->head_ref_name, s->in_repo_path, s->log_branches);
2449 if (err) {
2450 view_close(lv);
2451 return err;;
2453 *dead_view = view;
2454 *new_view = lv;
2455 break;
2456 default:
2457 break;
2460 return err;
2463 static const struct got_error *
2464 apply_unveil(const char *repo_path, const char *worktree_path)
2466 const struct got_error *error;
2468 #ifdef PROFILE
2469 if (unveil("gmon.out", "rwc") != 0)
2470 return got_error_from_errno2("unveil", "gmon.out");
2471 #endif
2472 if (repo_path && unveil(repo_path, "r") != 0)
2473 return got_error_from_errno2("unveil", repo_path);
2475 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2476 return got_error_from_errno2("unveil", worktree_path);
2478 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2479 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2481 error = got_privsep_unveil_exec_helpers();
2482 if (error != NULL)
2483 return error;
2485 if (unveil(NULL, NULL) != 0)
2486 return got_error_from_errno("unveil");
2488 return NULL;
2491 static void
2492 init_curses(void)
2494 initscr();
2495 cbreak();
2496 halfdelay(1); /* Do fast refresh while initial view is loading. */
2497 noecho();
2498 nonl();
2499 intrflush(stdscr, FALSE);
2500 keypad(stdscr, TRUE);
2501 curs_set(0);
2502 if (getenv("TOG_COLORS") != NULL) {
2503 start_color();
2504 use_default_colors();
2506 signal(SIGWINCH, tog_sigwinch);
2507 signal(SIGPIPE, tog_sigpipe);
2508 signal(SIGCONT, tog_sigcont);
2511 static const struct got_error *
2512 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2513 struct got_repository *repo, struct got_worktree *worktree)
2515 const struct got_error *err = NULL;
2517 if (argc == 0) {
2518 *in_repo_path = strdup("/");
2519 if (*in_repo_path == NULL)
2520 return got_error_from_errno("strdup");
2521 return NULL;
2524 if (worktree) {
2525 const char *prefix = got_worktree_get_path_prefix(worktree);
2526 char *wt_path, *p;
2528 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2529 if (err)
2530 return err;
2532 if (asprintf(&p, "%s%s%s", prefix,
2533 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2534 err = got_error_from_errno("asprintf");
2535 free(wt_path);
2536 return err;
2538 err = got_repo_map_path(in_repo_path, repo, p, 0);
2539 free(p);
2540 free(wt_path);
2541 } else
2542 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2544 return err;
2547 static const struct got_error *
2548 cmd_log(int argc, char *argv[])
2550 const struct got_error *error;
2551 struct got_repository *repo = NULL;
2552 struct got_worktree *worktree = NULL;
2553 struct got_reflist_head refs;
2554 struct got_object_id *start_id = NULL;
2555 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2556 char *start_commit = NULL, *head_ref_name = NULL;
2557 int ch, log_branches = 0;
2558 struct tog_view *view;
2560 SIMPLEQ_INIT(&refs);
2562 #ifndef PROFILE
2563 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2564 NULL) == -1)
2565 err(1, "pledge");
2566 #endif
2568 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2569 switch (ch) {
2570 case 'b':
2571 log_branches = 1;
2572 break;
2573 case 'c':
2574 start_commit = optarg;
2575 break;
2576 case 'r':
2577 repo_path = realpath(optarg, NULL);
2578 if (repo_path == NULL)
2579 return got_error_from_errno2("realpath",
2580 optarg);
2581 break;
2582 default:
2583 usage_log();
2584 /* NOTREACHED */
2588 argc -= optind;
2589 argv += optind;
2591 if (argc > 1)
2592 usage_log();
2594 cwd = getcwd(NULL, 0);
2595 if (cwd == NULL)
2596 return got_error_from_errno("getcwd");
2598 error = got_worktree_open(&worktree, cwd);
2599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2600 goto done;
2602 if (repo_path == NULL) {
2603 if (worktree)
2604 repo_path =
2605 strdup(got_worktree_get_repo_path(worktree));
2606 else
2607 repo_path = strdup(cwd);
2609 if (repo_path == NULL) {
2610 error = got_error_from_errno("strdup");
2611 goto done;
2614 error = got_repo_open(&repo, repo_path, NULL);
2615 if (error != NULL)
2616 goto done;
2618 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2619 repo, worktree);
2620 if (error)
2621 goto done;
2623 init_curses();
2625 error = apply_unveil(got_repo_get_path(repo),
2626 worktree ? got_worktree_get_root_path(worktree) : NULL);
2627 if (error)
2628 goto done;
2630 if (start_commit == NULL)
2631 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2632 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2633 GOT_OBJ_TYPE_COMMIT, 1, repo);
2634 else
2635 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2636 GOT_OBJ_TYPE_COMMIT, 1, repo);
2637 if (error != NULL)
2638 goto done;
2640 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2641 if (error)
2642 goto done;
2644 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2645 if (view == NULL) {
2646 error = got_error_from_errno("view_open");
2647 goto done;
2649 if (worktree) {
2650 head_ref_name = strdup(
2651 got_worktree_get_head_ref_name(worktree));
2652 if (head_ref_name == NULL) {
2653 error = got_error_from_errno("strdup");
2654 goto done;
2657 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2658 in_repo_path, log_branches);
2659 if (error)
2660 goto done;
2661 if (worktree) {
2662 /* Release work tree lock. */
2663 got_worktree_close(worktree);
2664 worktree = NULL;
2666 error = view_loop(view);
2667 done:
2668 free(in_repo_path);
2669 free(repo_path);
2670 free(cwd);
2671 free(start_id);
2672 free(head_ref_name);
2673 if (repo)
2674 got_repo_close(repo);
2675 if (worktree)
2676 got_worktree_close(worktree);
2677 got_ref_list_free(&refs);
2678 return error;
2681 __dead static void
2682 usage_diff(void)
2684 endwin();
2685 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2686 getprogname());
2687 exit(1);
2690 static char *
2691 parse_next_line(FILE *f, size_t *len)
2693 char *line;
2694 size_t linelen;
2695 size_t lineno;
2696 const char delim[3] = { '\0', '\0', '\0'};
2698 line = fparseln(f, &linelen, &lineno, delim, 0);
2699 if (len)
2700 *len = linelen;
2701 return line;
2704 static int
2705 match_line(const char *line, regex_t *regex)
2707 regmatch_t regmatch;
2709 return regexec(regex, line, 1, &regmatch, 0) == 0;
2712 struct tog_color *
2713 match_color(struct tog_colors *colors, const char *line)
2715 struct tog_color *tc = NULL;
2717 SIMPLEQ_FOREACH(tc, colors, entry) {
2718 if (match_line(line, &tc->regex))
2719 return tc;
2722 return NULL;
2725 static const struct got_error *
2726 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2727 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2728 char *header, struct tog_colors *colors)
2730 const struct got_error *err;
2731 int lineno = 0, nprinted = 0;
2732 char *line;
2733 struct tog_color *tc;
2734 size_t len;
2735 wchar_t *wline;
2736 int width;
2738 rewind(f);
2739 werase(view->window);
2741 if (header) {
2742 err = format_line(&wline, &width, header, view->ncols, 0);
2743 if (err) {
2744 return err;
2747 if (view_needs_focus_indication(view))
2748 wstandout(view->window);
2749 waddwstr(view->window, wline);
2750 if (view_needs_focus_indication(view))
2751 wstandend(view->window);
2752 if (width <= view->ncols - 1)
2753 waddch(view->window, '\n');
2755 if (max_lines <= 1)
2756 return NULL;
2757 max_lines--;
2760 *eof = 0;
2761 while (nprinted < max_lines) {
2762 line = parse_next_line(f, &len);
2763 if (line == NULL) {
2764 *eof = 1;
2765 break;
2767 if (++lineno < *first_displayed_line) {
2768 free(line);
2769 continue;
2772 err = format_line(&wline, &width, line, view->ncols, 0);
2773 if (err) {
2774 free(line);
2775 return err;
2778 tc = match_color(colors, line);
2779 if (tc)
2780 wattr_on(view->window,
2781 COLOR_PAIR(tc->colorpair), NULL);
2782 waddwstr(view->window, wline);
2783 if (tc)
2784 wattr_off(view->window,
2785 COLOR_PAIR(tc->colorpair), NULL);
2786 if (width <= view->ncols - 1)
2787 waddch(view->window, '\n');
2788 if (++nprinted == 1)
2789 *first_displayed_line = lineno;
2790 free(line);
2791 free(wline);
2792 wline = NULL;
2794 *last_displayed_line = lineno;
2796 view_vborder(view);
2798 if (*eof) {
2799 while (nprinted < view->nlines) {
2800 waddch(view->window, '\n');
2801 nprinted++;
2804 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2805 if (err) {
2806 return err;
2809 wstandout(view->window);
2810 waddwstr(view->window, wline);
2811 wstandend(view->window);
2814 return NULL;
2817 static char *
2818 get_datestr(time_t *time, char *datebuf)
2820 struct tm mytm, *tm;
2821 char *p, *s;
2823 tm = gmtime_r(time, &mytm);
2824 if (tm == NULL)
2825 return NULL;
2826 s = asctime_r(tm, datebuf);
2827 if (s == NULL)
2828 return NULL;
2829 p = strchr(s, '\n');
2830 if (p)
2831 *p = '\0';
2832 return s;
2835 static const struct got_error *
2836 write_commit_info(struct got_object_id *commit_id,
2837 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2839 const struct got_error *err = NULL;
2840 char datebuf[26], *datestr;
2841 struct got_commit_object *commit;
2842 char *id_str = NULL, *logmsg = NULL;
2843 time_t committer_time;
2844 const char *author, *committer;
2845 char *refs_str = NULL;
2847 if (refs) {
2848 err = build_refs_str(&refs_str, refs, commit_id, repo);
2849 if (err)
2850 return err;
2853 err = got_object_open_as_commit(&commit, repo, commit_id);
2854 if (err)
2855 return err;
2857 err = got_object_id_str(&id_str, commit_id);
2858 if (err) {
2859 err = got_error_from_errno("got_object_id_str");
2860 goto done;
2863 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2864 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2865 err = got_error_from_errno("fprintf");
2866 goto done;
2868 if (fprintf(outfile, "from: %s\n",
2869 got_object_commit_get_author(commit)) < 0) {
2870 err = got_error_from_errno("fprintf");
2871 goto done;
2873 committer_time = got_object_commit_get_committer_time(commit);
2874 datestr = get_datestr(&committer_time, datebuf);
2875 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2876 err = got_error_from_errno("fprintf");
2877 goto done;
2879 author = got_object_commit_get_author(commit);
2880 committer = got_object_commit_get_committer(commit);
2881 if (strcmp(author, committer) != 0 &&
2882 fprintf(outfile, "via: %s\n", committer) < 0) {
2883 err = got_error_from_errno("fprintf");
2884 goto done;
2886 err = got_object_commit_get_logmsg(&logmsg, commit);
2887 if (err)
2888 goto done;
2889 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2890 err = got_error_from_errno("fprintf");
2891 goto done;
2893 done:
2894 free(id_str);
2895 free(logmsg);
2896 free(refs_str);
2897 got_object_commit_close(commit);
2898 return err;
2901 const struct got_error *
2902 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2903 FILE *infile)
2905 size_t len;
2906 char *buf = NULL;
2907 int i;
2908 size_t noffsets = 0;
2909 off_t off = 0;
2911 if (line_offsets)
2912 *line_offsets = NULL;
2913 if (filesize)
2914 *filesize = 0;
2915 if (nlines)
2916 *nlines = 0;
2918 if (fseek(infile, 0, SEEK_END) == -1)
2919 return got_error_from_errno("fseek");
2920 len = ftell(infile) + 1;
2921 if (ferror(infile))
2922 return got_error_from_errno("ftell");
2923 if (fseek(infile, 0, SEEK_SET) == -1)
2924 return got_error_from_errno("fseek");
2926 if (len == 0)
2927 return NULL;
2928 if ((buf = calloc(len, sizeof(char *))) == NULL)
2929 return got_error_from_errno("calloc");
2931 fread(buf, 1, len, infile);
2932 if (ferror(infile))
2933 return got_error_from_errno("fread");
2935 i = 0;
2936 if (line_offsets && nlines) {
2937 if (*line_offsets == NULL) {
2938 /* Have some data but perhaps no '\n'. */
2939 noffsets = 1;
2940 *nlines = 1;
2941 *line_offsets = calloc(1, sizeof(**line_offsets));
2942 if (*line_offsets == NULL)
2943 return got_error_from_errno("calloc");
2944 /* Skip forward over end of first line. */
2945 while (i < len) {
2946 if (buf[i] == '\n')
2947 break;
2948 i++;
2951 /* Scan '\n' offsets in remaining chunk of data. */
2952 while (i < len) {
2953 if (buf[i] != '\n') {
2954 i++;
2955 continue;
2957 (*nlines)++;
2958 if (noffsets < *nlines) {
2959 off_t *o = recallocarray(*line_offsets,
2960 noffsets, *nlines,
2961 sizeof(**line_offsets));
2962 if (o == NULL) {
2963 free(*line_offsets);
2964 *line_offsets = NULL;
2965 return got_error_from_errno(
2966 "recallocarray");
2968 *line_offsets = o;
2969 noffsets = *nlines;
2971 off = i + 1;
2972 (*line_offsets)[*nlines - 1] = off;
2973 i++;
2977 if (fflush(infile) != 0)
2978 return got_error_from_errno("fflush");
2979 rewind(infile);
2981 if (filesize)
2982 *filesize = len;
2984 return NULL;
2987 static const struct got_error *
2988 create_diff(struct tog_diff_view_state *s)
2990 const struct got_error *err = NULL;
2991 FILE *f = NULL;
2992 int obj_type;
2994 f = got_opentemp();
2995 if (f == NULL) {
2996 err = got_error_from_errno("got_opentemp");
2997 goto done;
2999 if (s->f && fclose(s->f) != 0) {
3000 err = got_error_from_errno("fclose");
3001 goto done;
3003 s->f = f;
3005 if (s->id1)
3006 err = got_object_get_type(&obj_type, s->repo, s->id1);
3007 else
3008 err = got_object_get_type(&obj_type, s->repo, s->id2);
3009 if (err)
3010 goto done;
3012 switch (obj_type) {
3013 case GOT_OBJ_TYPE_BLOB:
3014 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3015 s->diff_context, 0, s->repo, s->f);
3016 break;
3017 case GOT_OBJ_TYPE_TREE:
3018 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3019 s->diff_context, 0, s->repo, s->f);
3020 break;
3021 case GOT_OBJ_TYPE_COMMIT: {
3022 const struct got_object_id_queue *parent_ids;
3023 struct got_object_qid *pid;
3024 struct got_commit_object *commit2;
3026 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3027 if (err)
3028 goto done;
3029 /* Show commit info if we're diffing to a parent/root commit. */
3030 if (s->id1 == NULL) {
3031 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3032 if (err)
3033 goto done;
3034 } else {
3035 parent_ids = got_object_commit_get_parent_ids(commit2);
3036 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3037 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3038 err = write_commit_info(s->id2, s->refs,
3039 s->repo, s->f);
3040 if (err)
3041 goto done;
3042 break;
3046 got_object_commit_close(commit2);
3048 err = got_diff_objects_as_commits(s->id1, s->id2,
3049 s->diff_context, 0, s->repo, s->f);
3050 break;
3052 default:
3053 err = got_error(GOT_ERR_OBJ_TYPE);
3054 break;
3056 if (err)
3057 goto done;
3058 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3059 s->f);
3060 done:
3061 if (s->f && fflush(s->f) != 0 && err == NULL)
3062 err = got_error_from_errno("fflush");
3063 return err;
3066 static void
3067 diff_view_indicate_progress(struct tog_view *view)
3069 mvwaddstr(view->window, 0, 0, "diffing...");
3070 update_panels();
3071 doupdate();
3074 static const struct got_error *
3075 search_start_diff_view(struct tog_view *view)
3077 struct tog_diff_view_state *s = &view->state.diff;
3079 s->matched_line = 0;
3080 return NULL;
3083 static const struct got_error *
3084 search_next_diff_view(struct tog_view *view)
3086 struct tog_diff_view_state *s = &view->state.diff;
3087 int lineno;
3089 if (!view->searching) {
3090 view->search_next_done = 1;
3091 return NULL;
3094 if (s->matched_line) {
3095 if (view->searching == TOG_SEARCH_FORWARD)
3096 lineno = s->matched_line + 1;
3097 else
3098 lineno = s->matched_line - 1;
3099 } else {
3100 if (view->searching == TOG_SEARCH_FORWARD)
3101 lineno = 1;
3102 else
3103 lineno = s->nlines;
3106 while (1) {
3107 char *line = NULL;
3108 off_t offset;
3109 size_t len;
3111 if (lineno <= 0 || lineno > s->nlines) {
3112 if (s->matched_line == 0) {
3113 view->search_next_done = 1;
3114 free(line);
3115 break;
3118 if (view->searching == TOG_SEARCH_FORWARD)
3119 lineno = 1;
3120 else
3121 lineno = s->nlines;
3124 offset = s->line_offsets[lineno - 1];
3125 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3126 free(line);
3127 return got_error_from_errno("fseeko");
3129 free(line);
3130 line = parse_next_line(s->f, &len);
3131 if (line && match_line(line, &view->regex)) {
3132 view->search_next_done = 1;
3133 s->matched_line = lineno;
3134 free(line);
3135 break;
3137 free(line);
3138 if (view->searching == TOG_SEARCH_FORWARD)
3139 lineno++;
3140 else
3141 lineno--;
3144 if (s->matched_line) {
3145 s->first_displayed_line = s->matched_line;
3146 s->selected_line = 1;
3149 return NULL;
3152 static const struct got_error *
3153 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3154 struct got_object_id *id2, struct tog_view *log_view,
3155 struct got_reflist_head *refs, struct got_repository *repo)
3157 const struct got_error *err;
3158 struct tog_diff_view_state *s = &view->state.diff;
3160 if (id1 != NULL && id2 != NULL) {
3161 int type1, type2;
3162 err = got_object_get_type(&type1, repo, id1);
3163 if (err)
3164 return err;
3165 err = got_object_get_type(&type2, repo, id2);
3166 if (err)
3167 return err;
3169 if (type1 != type2)
3170 return got_error(GOT_ERR_OBJ_TYPE);
3172 s->first_displayed_line = 1;
3173 s->last_displayed_line = view->nlines;
3174 s->selected_line = 1;
3175 s->repo = repo;
3176 s->refs = refs;
3177 s->id1 = id1;
3178 s->id2 = id2;
3180 if (id1) {
3181 s->id1 = got_object_id_dup(id1);
3182 if (s->id1 == NULL)
3183 return got_error_from_errno("got_object_id_dup");
3184 } else
3185 s->id1 = NULL;
3187 s->id2 = got_object_id_dup(id2);
3188 if (s->id2 == NULL) {
3189 free(s->id1);
3190 s->id1 = NULL;
3191 return got_error_from_errno("got_object_id_dup");
3193 s->f = NULL;
3194 s->first_displayed_line = 1;
3195 s->last_displayed_line = view->nlines;
3196 s->diff_context = 3;
3197 s->log_view = log_view;
3198 s->repo = repo;
3199 s->refs = refs;
3201 SIMPLEQ_INIT(&s->colors);
3202 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3203 err = add_color(&s->colors,
3204 "^-", TOG_COLOR_DIFF_MINUS,
3205 get_color_value("TOG_COLOR_DIFF_MINUS"));
3206 if (err)
3207 return err;
3208 err = add_color(&s->colors, "^\\+",
3209 TOG_COLOR_DIFF_PLUS,
3210 get_color_value("TOG_COLOR_DIFF_PLUS"));
3211 if (err) {
3212 free_colors(&s->colors);
3213 return err;
3215 err = add_color(&s->colors,
3216 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3217 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3218 if (err) {
3219 free_colors(&s->colors);
3220 return err;
3223 err = add_color(&s->colors,
3224 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3225 get_color_value("TOG_COLOR_DIFF_META"));
3226 if (err) {
3227 free_colors(&s->colors);
3228 return err;
3231 err = add_color(&s->colors,
3232 "^(from|via): ", TOG_COLOR_AUTHOR,
3233 get_color_value("TOG_COLOR_AUTHOR"));
3234 if (err) {
3235 free_colors(&s->colors);
3236 return err;
3239 err = add_color(&s->colors,
3240 "^date: ", TOG_COLOR_DATE,
3241 get_color_value("TOG_COLOR_DATE"));
3242 if (err) {
3243 free_colors(&s->colors);
3244 return err;
3248 if (log_view && view_is_splitscreen(view))
3249 show_log_view(log_view); /* draw vborder */
3250 diff_view_indicate_progress(view);
3252 err = create_diff(s);
3253 if (err) {
3254 free(s->id1);
3255 s->id1 = NULL;
3256 free(s->id2);
3257 s->id2 = NULL;
3258 return err;
3261 view->show = show_diff_view;
3262 view->input = input_diff_view;
3263 view->close = close_diff_view;
3264 view->search_start = search_start_diff_view;
3265 view->search_next = search_next_diff_view;
3267 return NULL;
3270 static const struct got_error *
3271 close_diff_view(struct tog_view *view)
3273 const struct got_error *err = NULL;
3274 struct tog_diff_view_state *s = &view->state.diff;
3276 free(s->id1);
3277 s->id1 = NULL;
3278 free(s->id2);
3279 s->id2 = NULL;
3280 if (s->f && fclose(s->f) == EOF)
3281 err = got_error_from_errno("fclose");
3282 free_colors(&s->colors);
3283 free(s->line_offsets);
3284 return err;
3287 static const struct got_error *
3288 show_diff_view(struct tog_view *view)
3290 const struct got_error *err;
3291 struct tog_diff_view_state *s = &view->state.diff;
3292 char *id_str1 = NULL, *id_str2, *header;
3294 if (s->id1) {
3295 err = got_object_id_str(&id_str1, s->id1);
3296 if (err)
3297 return err;
3299 err = got_object_id_str(&id_str2, s->id2);
3300 if (err)
3301 return err;
3303 if (asprintf(&header, "diff %s %s",
3304 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3305 err = got_error_from_errno("asprintf");
3306 free(id_str1);
3307 free(id_str2);
3308 return err;
3310 free(id_str1);
3311 free(id_str2);
3313 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3314 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3315 header, &s->colors);
3318 static const struct got_error *
3319 set_selected_commit(struct tog_diff_view_state *s,
3320 struct commit_queue_entry *entry)
3322 const struct got_error *err;
3323 const struct got_object_id_queue *parent_ids;
3324 struct got_commit_object *selected_commit;
3325 struct got_object_qid *pid;
3327 free(s->id2);
3328 s->id2 = got_object_id_dup(entry->id);
3329 if (s->id2 == NULL)
3330 return got_error_from_errno("got_object_id_dup");
3332 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3333 if (err)
3334 return err;
3335 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3336 free(s->id1);
3337 pid = SIMPLEQ_FIRST(parent_ids);
3338 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3339 got_object_commit_close(selected_commit);
3340 return NULL;
3343 static const struct got_error *
3344 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3345 struct tog_view **focus_view, struct tog_view *view, int ch)
3347 const struct got_error *err = NULL;
3348 struct tog_diff_view_state *s = &view->state.diff;
3349 struct tog_log_view_state *ls;
3350 struct commit_queue_entry *entry;
3351 int i;
3353 switch (ch) {
3354 case 'k':
3355 case KEY_UP:
3356 if (s->first_displayed_line > 1)
3357 s->first_displayed_line--;
3358 break;
3359 case KEY_PPAGE:
3360 case CTRL('b'):
3361 if (s->first_displayed_line == 1)
3362 break;
3363 i = 0;
3364 while (i++ < view->nlines - 1 &&
3365 s->first_displayed_line > 1)
3366 s->first_displayed_line--;
3367 break;
3368 case 'j':
3369 case KEY_DOWN:
3370 if (!s->eof)
3371 s->first_displayed_line++;
3372 break;
3373 case KEY_NPAGE:
3374 case CTRL('f'):
3375 case ' ':
3376 if (s->eof)
3377 break;
3378 i = 0;
3379 while (!s->eof && i++ < view->nlines - 1) {
3380 char *line;
3381 line = parse_next_line(s->f, NULL);
3382 s->first_displayed_line++;
3383 if (line == NULL)
3384 break;
3386 break;
3387 case '[':
3388 if (s->diff_context > 0) {
3389 s->diff_context--;
3390 diff_view_indicate_progress(view);
3391 err = create_diff(s);
3393 break;
3394 case ']':
3395 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3396 s->diff_context++;
3397 diff_view_indicate_progress(view);
3398 err = create_diff(s);
3400 break;
3401 case '<':
3402 case ',':
3403 if (s->log_view == NULL)
3404 break;
3405 ls = &s->log_view->state.log;
3406 entry = TAILQ_PREV(ls->selected_entry,
3407 commit_queue_head, entry);
3408 if (entry == NULL)
3409 break;
3411 err = input_log_view(NULL, NULL, NULL, s->log_view,
3412 KEY_UP);
3413 if (err)
3414 break;
3416 err = set_selected_commit(s, entry);
3417 if (err)
3418 break;
3420 s->first_displayed_line = 1;
3421 s->last_displayed_line = view->nlines;
3423 diff_view_indicate_progress(view);
3424 err = create_diff(s);
3425 break;
3426 case '>':
3427 case '.':
3428 if (s->log_view == NULL)
3429 break;
3430 ls = &s->log_view->state.log;
3432 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3433 ls->thread_args.commits_needed++;
3435 /* Display "loading..." in log view. */
3436 show_log_view(s->log_view);
3437 update_panels();
3438 doupdate();
3440 err = trigger_log_thread(1 /* load_all */,
3441 &ls->thread_args.commits_needed,
3442 &ls->thread_args.log_complete,
3443 &ls->thread_args.need_commits);
3444 if (err)
3445 break;
3447 err = input_log_view(NULL, NULL, NULL, s->log_view,
3448 KEY_DOWN);
3449 if (err)
3450 break;
3452 entry = TAILQ_NEXT(ls->selected_entry, entry);
3453 if (entry == NULL)
3454 break;
3456 err = set_selected_commit(s, entry);
3457 if (err)
3458 break;
3460 s->first_displayed_line = 1;
3461 s->last_displayed_line = view->nlines;
3463 diff_view_indicate_progress(view);
3464 err = create_diff(s);
3465 break;
3466 default:
3467 break;
3470 return err;
3473 static const struct got_error *
3474 cmd_diff(int argc, char *argv[])
3476 const struct got_error *error = NULL;
3477 struct got_repository *repo = NULL;
3478 struct got_worktree *worktree = NULL;
3479 struct got_reflist_head refs;
3480 struct got_object_id *id1 = NULL, *id2 = NULL;
3481 char *repo_path = NULL, *cwd = NULL;
3482 char *id_str1 = NULL, *id_str2 = NULL;
3483 int ch;
3484 struct tog_view *view;
3486 SIMPLEQ_INIT(&refs);
3488 #ifndef PROFILE
3489 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3490 NULL) == -1)
3491 err(1, "pledge");
3492 #endif
3494 while ((ch = getopt(argc, argv, "r:")) != -1) {
3495 switch (ch) {
3496 case 'r':
3497 repo_path = realpath(optarg, NULL);
3498 if (repo_path == NULL)
3499 return got_error_from_errno2("realpath",
3500 optarg);
3501 break;
3502 default:
3503 usage_diff();
3504 /* NOTREACHED */
3508 argc -= optind;
3509 argv += optind;
3511 if (argc == 0) {
3512 usage_diff(); /* TODO show local worktree changes */
3513 } else if (argc == 2) {
3514 id_str1 = argv[0];
3515 id_str2 = argv[1];
3516 } else
3517 usage_diff();
3519 cwd = getcwd(NULL, 0);
3520 if (cwd == NULL)
3521 return got_error_from_errno("getcwd");
3523 error = got_worktree_open(&worktree, cwd);
3524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3525 goto done;
3527 if (repo_path == NULL) {
3528 if (worktree)
3529 repo_path =
3530 strdup(got_worktree_get_repo_path(worktree));
3531 else
3532 repo_path = strdup(cwd);
3534 if (repo_path == NULL) {
3535 error = got_error_from_errno("strdup");
3536 goto done;
3539 error = got_repo_open(&repo, repo_path, NULL);
3540 if (error)
3541 goto done;
3543 init_curses();
3545 error = apply_unveil(got_repo_get_path(repo), NULL);
3546 if (error)
3547 goto done;
3549 error = got_repo_match_object_id_prefix(&id1, id_str1,
3550 GOT_OBJ_TYPE_ANY, repo);
3551 if (error)
3552 goto done;
3554 error = got_repo_match_object_id_prefix(&id2, id_str2,
3555 GOT_OBJ_TYPE_ANY, repo);
3556 if (error)
3557 goto done;
3559 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3560 if (error)
3561 goto done;
3563 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3564 if (view == NULL) {
3565 error = got_error_from_errno("view_open");
3566 goto done;
3568 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3569 if (error)
3570 goto done;
3571 error = view_loop(view);
3572 done:
3573 free(repo_path);
3574 free(cwd);
3575 if (repo)
3576 got_repo_close(repo);
3577 if (worktree)
3578 got_worktree_close(worktree);
3579 got_ref_list_free(&refs);
3580 return error;
3583 __dead static void
3584 usage_blame(void)
3586 endwin();
3587 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3588 getprogname());
3589 exit(1);
3592 struct tog_blame_line {
3593 int annotated;
3594 struct got_object_id *id;
3597 static const struct got_error *
3598 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3599 const char *path, struct tog_blame_line *lines, int nlines,
3600 int blame_complete, int selected_line, int *first_displayed_line,
3601 int *last_displayed_line, int *eof, int max_lines,
3602 struct tog_colors *colors)
3604 const struct got_error *err;
3605 int lineno = 0, nprinted = 0;
3606 char *line;
3607 size_t len;
3608 wchar_t *wline;
3609 int width;
3610 struct tog_blame_line *blame_line;
3611 struct got_object_id *prev_id = NULL;
3612 char *id_str;
3613 struct tog_color *tc;
3615 err = got_object_id_str(&id_str, id);
3616 if (err)
3617 return err;
3619 rewind(f);
3620 werase(view->window);
3622 if (asprintf(&line, "commit %s", id_str) == -1) {
3623 err = got_error_from_errno("asprintf");
3624 free(id_str);
3625 return err;
3628 err = format_line(&wline, &width, line, view->ncols, 0);
3629 free(line);
3630 line = NULL;
3631 if (err)
3632 return err;
3633 if (view_needs_focus_indication(view))
3634 wstandout(view->window);
3635 tc = get_color(colors, TOG_COLOR_COMMIT);
3636 if (tc)
3637 wattr_on(view->window,
3638 COLOR_PAIR(tc->colorpair), NULL);
3639 waddwstr(view->window, wline);
3640 if (tc)
3641 wattr_off(view->window,
3642 COLOR_PAIR(tc->colorpair), NULL);
3643 if (view_needs_focus_indication(view))
3644 wstandend(view->window);
3645 free(wline);
3646 wline = NULL;
3647 if (width < view->ncols - 1)
3648 waddch(view->window, '\n');
3650 if (asprintf(&line, "[%d/%d] %s%s",
3651 *first_displayed_line - 1 + selected_line, nlines,
3652 blame_complete ? "" : "annotating... ", path) == -1) {
3653 free(id_str);
3654 return got_error_from_errno("asprintf");
3656 free(id_str);
3657 err = format_line(&wline, &width, line, view->ncols, 0);
3658 free(line);
3659 line = NULL;
3660 if (err)
3661 return err;
3662 waddwstr(view->window, wline);
3663 free(wline);
3664 wline = NULL;
3665 if (width < view->ncols - 1)
3666 waddch(view->window, '\n');
3668 *eof = 0;
3669 while (nprinted < max_lines - 2) {
3670 line = parse_next_line(f, &len);
3671 if (line == NULL) {
3672 *eof = 1;
3673 break;
3675 if (++lineno < *first_displayed_line) {
3676 free(line);
3677 continue;
3680 if (view->ncols <= 9) {
3681 width = 9;
3682 wline = wcsdup(L"");
3683 if (wline == NULL)
3684 err = got_error_from_errno("wcsdup");
3685 } else {
3686 err = format_line(&wline, &width, line,
3687 view->ncols - 9, 9);
3688 width += 9;
3690 if (err) {
3691 free(line);
3692 return err;
3695 if (view->focussed && nprinted == selected_line - 1)
3696 wstandout(view->window);
3698 if (nlines > 0) {
3699 blame_line = &lines[lineno - 1];
3700 if (blame_line->annotated && prev_id &&
3701 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3702 !(view->focussed &&
3703 nprinted == selected_line - 1)) {
3704 waddstr(view->window, " ");
3705 } else if (blame_line->annotated) {
3706 char *id_str;
3707 err = got_object_id_str(&id_str, blame_line->id);
3708 if (err) {
3709 free(line);
3710 free(wline);
3711 return err;
3713 tc = get_color(colors, TOG_COLOR_COMMIT);
3714 if (tc)
3715 wattr_on(view->window,
3716 COLOR_PAIR(tc->colorpair), NULL);
3717 wprintw(view->window, "%.8s", id_str);
3718 if (tc)
3719 wattr_off(view->window,
3720 COLOR_PAIR(tc->colorpair), NULL);
3721 free(id_str);
3722 prev_id = blame_line->id;
3723 } else {
3724 waddstr(view->window, "........");
3725 prev_id = NULL;
3727 } else {
3728 waddstr(view->window, "........");
3729 prev_id = NULL;
3732 if (view->focussed && nprinted == selected_line - 1)
3733 wstandend(view->window);
3734 waddstr(view->window, " ");
3736 waddwstr(view->window, wline);
3737 if (width <= view->ncols - 1)
3738 waddch(view->window, '\n');
3739 if (++nprinted == 1)
3740 *first_displayed_line = lineno;
3741 free(line);
3742 free(wline);
3743 wline = NULL;
3745 *last_displayed_line = lineno;
3747 view_vborder(view);
3749 return NULL;
3752 static const struct got_error *
3753 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3755 const struct got_error *err = NULL;
3756 struct tog_blame_cb_args *a = arg;
3757 struct tog_blame_line *line;
3758 int errcode;
3760 if (nlines != a->nlines ||
3761 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3762 return got_error(GOT_ERR_RANGE);
3764 errcode = pthread_mutex_lock(&tog_mutex);
3765 if (errcode)
3766 return got_error_set_errno(errcode, "pthread_mutex_lock");
3768 if (*a->quit) { /* user has quit the blame view */
3769 err = got_error(GOT_ERR_ITER_COMPLETED);
3770 goto done;
3773 if (lineno == -1)
3774 goto done; /* no change in this commit */
3776 line = &a->lines[lineno - 1];
3777 if (line->annotated)
3778 goto done;
3780 line->id = got_object_id_dup(id);
3781 if (line->id == NULL) {
3782 err = got_error_from_errno("got_object_id_dup");
3783 goto done;
3785 line->annotated = 1;
3786 done:
3787 errcode = pthread_mutex_unlock(&tog_mutex);
3788 if (errcode)
3789 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3790 return err;
3793 static void *
3794 blame_thread(void *arg)
3796 const struct got_error *err;
3797 struct tog_blame_thread_args *ta = arg;
3798 struct tog_blame_cb_args *a = ta->cb_args;
3799 int errcode;
3801 err = block_signals_used_by_main_thread();
3802 if (err)
3803 return (void *)err;
3805 err = got_blame(ta->path, a->commit_id, ta->repo,
3806 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3807 if (err && err->code == GOT_ERR_CANCELLED)
3808 err = NULL;
3810 errcode = pthread_mutex_lock(&tog_mutex);
3811 if (errcode)
3812 return (void *)got_error_set_errno(errcode,
3813 "pthread_mutex_lock");
3815 got_repo_close(ta->repo);
3816 ta->repo = NULL;
3817 *ta->complete = 1;
3819 errcode = pthread_mutex_unlock(&tog_mutex);
3820 if (errcode && err == NULL)
3821 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3823 return (void *)err;
3826 static struct got_object_id *
3827 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3828 int first_displayed_line, int selected_line)
3830 struct tog_blame_line *line;
3832 if (nlines <= 0)
3833 return NULL;
3835 line = &lines[first_displayed_line - 1 + selected_line - 1];
3836 if (!line->annotated)
3837 return NULL;
3839 return line->id;
3842 static const struct got_error *
3843 stop_blame(struct tog_blame *blame)
3845 const struct got_error *err = NULL;
3846 int i;
3848 if (blame->thread) {
3849 int errcode;
3850 errcode = pthread_mutex_unlock(&tog_mutex);
3851 if (errcode)
3852 return got_error_set_errno(errcode,
3853 "pthread_mutex_unlock");
3854 errcode = pthread_join(blame->thread, (void **)&err);
3855 if (errcode)
3856 return got_error_set_errno(errcode, "pthread_join");
3857 errcode = pthread_mutex_lock(&tog_mutex);
3858 if (errcode)
3859 return got_error_set_errno(errcode,
3860 "pthread_mutex_lock");
3861 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3862 err = NULL;
3863 blame->thread = NULL;
3865 if (blame->thread_args.repo) {
3866 got_repo_close(blame->thread_args.repo);
3867 blame->thread_args.repo = NULL;
3869 if (blame->f) {
3870 if (fclose(blame->f) != 0 && err == NULL)
3871 err = got_error_from_errno("fclose");
3872 blame->f = NULL;
3874 if (blame->lines) {
3875 for (i = 0; i < blame->nlines; i++)
3876 free(blame->lines[i].id);
3877 free(blame->lines);
3878 blame->lines = NULL;
3880 free(blame->cb_args.commit_id);
3881 blame->cb_args.commit_id = NULL;
3883 return err;
3886 static const struct got_error *
3887 cancel_blame_view(void *arg)
3889 const struct got_error *err = NULL;
3890 int *done = arg;
3891 int errcode;
3893 errcode = pthread_mutex_lock(&tog_mutex);
3894 if (errcode)
3895 return got_error_set_errno(errcode,
3896 "pthread_mutex_unlock");
3898 if (*done)
3899 err = got_error(GOT_ERR_CANCELLED);
3901 errcode = pthread_mutex_unlock(&tog_mutex);
3902 if (errcode)
3903 return got_error_set_errno(errcode,
3904 "pthread_mutex_lock");
3906 return err;
3909 static const struct got_error *
3910 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3911 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3912 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3913 struct got_repository *repo)
3915 const struct got_error *err = NULL;
3916 struct got_blob_object *blob = NULL;
3917 struct got_repository *thread_repo = NULL;
3918 struct got_object_id *obj_id = NULL;
3919 int obj_type;
3921 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3922 if (err)
3923 return err;
3925 err = got_object_get_type(&obj_type, repo, obj_id);
3926 if (err)
3927 goto done;
3929 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3930 err = got_error(GOT_ERR_OBJ_TYPE);
3931 goto done;
3934 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3935 if (err)
3936 goto done;
3937 blame->f = got_opentemp();
3938 if (blame->f == NULL) {
3939 err = got_error_from_errno("got_opentemp");
3940 goto done;
3942 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3943 &blame->line_offsets, blame->f, blob);
3944 if (err || blame->nlines == 0)
3945 goto done;
3947 /* Don't include \n at EOF in the blame line count. */
3948 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3949 blame->nlines--;
3951 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3952 if (blame->lines == NULL) {
3953 err = got_error_from_errno("calloc");
3954 goto done;
3957 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3958 if (err)
3959 goto done;
3961 blame->cb_args.view = view;
3962 blame->cb_args.lines = blame->lines;
3963 blame->cb_args.nlines = blame->nlines;
3964 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3965 if (blame->cb_args.commit_id == NULL) {
3966 err = got_error_from_errno("got_object_id_dup");
3967 goto done;
3969 blame->cb_args.quit = done;
3971 blame->thread_args.path = path;
3972 blame->thread_args.repo = thread_repo;
3973 blame->thread_args.cb_args = &blame->cb_args;
3974 blame->thread_args.complete = blame_complete;
3975 blame->thread_args.cancel_cb = cancel_blame_view;
3976 blame->thread_args.cancel_arg = done;
3977 *blame_complete = 0;
3979 done:
3980 if (blob)
3981 got_object_blob_close(blob);
3982 free(obj_id);
3983 if (err)
3984 stop_blame(blame);
3985 return err;
3988 static const struct got_error *
3989 open_blame_view(struct tog_view *view, char *path,
3990 struct got_object_id *commit_id, struct got_reflist_head *refs,
3991 struct got_repository *repo)
3993 const struct got_error *err = NULL;
3994 struct tog_blame_view_state *s = &view->state.blame;
3996 SIMPLEQ_INIT(&s->blamed_commits);
3998 s->path = strdup(path);
3999 if (s->path == NULL)
4000 return got_error_from_errno("strdup");
4002 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4003 if (err) {
4004 free(s->path);
4005 return err;
4008 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4009 s->first_displayed_line = 1;
4010 s->last_displayed_line = view->nlines;
4011 s->selected_line = 1;
4012 s->blame_complete = 0;
4013 s->repo = repo;
4014 s->refs = refs;
4015 s->commit_id = commit_id;
4016 memset(&s->blame, 0, sizeof(s->blame));
4018 SIMPLEQ_INIT(&s->colors);
4019 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4020 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4021 get_color_value("TOG_COLOR_COMMIT"));
4022 if (err)
4023 return err;
4026 view->show = show_blame_view;
4027 view->input = input_blame_view;
4028 view->close = close_blame_view;
4029 view->search_start = search_start_blame_view;
4030 view->search_next = search_next_blame_view;
4032 return run_blame(&s->blame, view, &s->blame_complete,
4033 &s->first_displayed_line, &s->last_displayed_line,
4034 &s->selected_line, &s->done, &s->eof, s->path,
4035 s->blamed_commit->id, s->repo);
4038 static const struct got_error *
4039 close_blame_view(struct tog_view *view)
4041 const struct got_error *err = NULL;
4042 struct tog_blame_view_state *s = &view->state.blame;
4044 if (s->blame.thread)
4045 err = stop_blame(&s->blame);
4047 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4048 struct got_object_qid *blamed_commit;
4049 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4050 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4051 got_object_qid_free(blamed_commit);
4054 free(s->path);
4055 free_colors(&s->colors);
4057 return err;
4060 static const struct got_error *
4061 search_start_blame_view(struct tog_view *view)
4063 struct tog_blame_view_state *s = &view->state.blame;
4065 s->matched_line = 0;
4066 return NULL;
4069 static const struct got_error *
4070 search_next_blame_view(struct tog_view *view)
4072 struct tog_blame_view_state *s = &view->state.blame;
4073 int lineno;
4075 if (!view->searching) {
4076 view->search_next_done = 1;
4077 return NULL;
4080 if (s->matched_line) {
4081 if (view->searching == TOG_SEARCH_FORWARD)
4082 lineno = s->matched_line + 1;
4083 else
4084 lineno = s->matched_line - 1;
4085 } else {
4086 if (view->searching == TOG_SEARCH_FORWARD)
4087 lineno = 1;
4088 else
4089 lineno = s->blame.nlines;
4092 while (1) {
4093 char *line = NULL;
4094 off_t offset;
4095 size_t len;
4097 if (lineno <= 0 || lineno > s->blame.nlines) {
4098 if (s->matched_line == 0) {
4099 view->search_next_done = 1;
4100 free(line);
4101 break;
4104 if (view->searching == TOG_SEARCH_FORWARD)
4105 lineno = 1;
4106 else
4107 lineno = s->blame.nlines;
4110 offset = s->blame.line_offsets[lineno - 1];
4111 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4112 free(line);
4113 return got_error_from_errno("fseeko");
4115 free(line);
4116 line = parse_next_line(s->blame.f, &len);
4117 if (line && match_line(line, &view->regex)) {
4118 view->search_next_done = 1;
4119 s->matched_line = lineno;
4120 free(line);
4121 break;
4123 free(line);
4124 if (view->searching == TOG_SEARCH_FORWARD)
4125 lineno++;
4126 else
4127 lineno--;
4130 if (s->matched_line) {
4131 s->first_displayed_line = s->matched_line;
4132 s->selected_line = 1;
4135 return NULL;
4138 static const struct got_error *
4139 show_blame_view(struct tog_view *view)
4141 const struct got_error *err = NULL;
4142 struct tog_blame_view_state *s = &view->state.blame;
4143 int errcode;
4145 if (s->blame.thread == NULL) {
4146 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4147 &s->blame.thread_args);
4148 if (errcode)
4149 return got_error_set_errno(errcode, "pthread_create");
4151 halfdelay(1); /* fast refresh while annotating */
4154 if (s->blame_complete)
4155 halfdelay(10); /* disable fast refresh */
4157 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4158 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4159 s->selected_line, &s->first_displayed_line,
4160 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4162 view_vborder(view);
4163 return err;
4166 static const struct got_error *
4167 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4168 struct tog_view **focus_view, struct tog_view *view, int ch)
4170 const struct got_error *err = NULL, *thread_err = NULL;
4171 struct tog_view *diff_view;
4172 struct tog_blame_view_state *s = &view->state.blame;
4173 int begin_x = 0;
4175 switch (ch) {
4176 case 'q':
4177 s->done = 1;
4178 break;
4179 case 'k':
4180 case KEY_UP:
4181 if (s->selected_line > 1)
4182 s->selected_line--;
4183 else if (s->selected_line == 1 &&
4184 s->first_displayed_line > 1)
4185 s->first_displayed_line--;
4186 break;
4187 case KEY_PPAGE:
4188 case CTRL('b'):
4189 if (s->first_displayed_line == 1) {
4190 s->selected_line = 1;
4191 break;
4193 if (s->first_displayed_line > view->nlines - 2)
4194 s->first_displayed_line -=
4195 (view->nlines - 2);
4196 else
4197 s->first_displayed_line = 1;
4198 break;
4199 case 'j':
4200 case KEY_DOWN:
4201 if (s->selected_line < view->nlines - 2 &&
4202 s->first_displayed_line +
4203 s->selected_line <= s->blame.nlines)
4204 s->selected_line++;
4205 else if (s->last_displayed_line <
4206 s->blame.nlines)
4207 s->first_displayed_line++;
4208 break;
4209 case 'b':
4210 case 'p': {
4211 struct got_object_id *id = NULL;
4212 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4213 s->first_displayed_line, s->selected_line);
4214 if (id == NULL)
4215 break;
4216 if (ch == 'p') {
4217 struct got_commit_object *commit;
4218 struct got_object_qid *pid;
4219 struct got_object_id *blob_id = NULL;
4220 int obj_type;
4221 err = got_object_open_as_commit(&commit,
4222 s->repo, id);
4223 if (err)
4224 break;
4225 pid = SIMPLEQ_FIRST(
4226 got_object_commit_get_parent_ids(commit));
4227 if (pid == NULL) {
4228 got_object_commit_close(commit);
4229 break;
4231 /* Check if path history ends here. */
4232 err = got_object_id_by_path(&blob_id, s->repo,
4233 pid->id, s->path);
4234 if (err) {
4235 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4236 err = NULL;
4237 got_object_commit_close(commit);
4238 break;
4240 err = got_object_get_type(&obj_type, s->repo,
4241 blob_id);
4242 free(blob_id);
4243 /* Can't blame non-blob type objects. */
4244 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4245 got_object_commit_close(commit);
4246 break;
4248 err = got_object_qid_alloc(&s->blamed_commit,
4249 pid->id);
4250 got_object_commit_close(commit);
4251 } else {
4252 if (got_object_id_cmp(id,
4253 s->blamed_commit->id) == 0)
4254 break;
4255 err = got_object_qid_alloc(&s->blamed_commit,
4256 id);
4258 if (err)
4259 break;
4260 s->done = 1;
4261 thread_err = stop_blame(&s->blame);
4262 s->done = 0;
4263 if (thread_err)
4264 break;
4265 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4266 s->blamed_commit, entry);
4267 err = run_blame(&s->blame, view, &s->blame_complete,
4268 &s->first_displayed_line, &s->last_displayed_line,
4269 &s->selected_line, &s->done, &s->eof,
4270 s->path, s->blamed_commit->id, s->repo);
4271 if (err)
4272 break;
4273 break;
4275 case 'B': {
4276 struct got_object_qid *first;
4277 first = SIMPLEQ_FIRST(&s->blamed_commits);
4278 if (!got_object_id_cmp(first->id, s->commit_id))
4279 break;
4280 s->done = 1;
4281 thread_err = stop_blame(&s->blame);
4282 s->done = 0;
4283 if (thread_err)
4284 break;
4285 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4286 got_object_qid_free(s->blamed_commit);
4287 s->blamed_commit =
4288 SIMPLEQ_FIRST(&s->blamed_commits);
4289 err = run_blame(&s->blame, view, &s->blame_complete,
4290 &s->first_displayed_line, &s->last_displayed_line,
4291 &s->selected_line, &s->done, &s->eof, s->path,
4292 s->blamed_commit->id, s->repo);
4293 if (err)
4294 break;
4295 break;
4297 case KEY_ENTER:
4298 case '\r': {
4299 struct got_object_id *id = NULL;
4300 struct got_object_qid *pid;
4301 struct got_commit_object *commit = NULL;
4302 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4303 s->first_displayed_line, s->selected_line);
4304 if (id == NULL)
4305 break;
4306 err = got_object_open_as_commit(&commit, s->repo, id);
4307 if (err)
4308 break;
4309 pid = SIMPLEQ_FIRST(
4310 got_object_commit_get_parent_ids(commit));
4311 if (view_is_parent_view(view))
4312 begin_x = view_split_begin_x(view->begin_x);
4313 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4314 if (diff_view == NULL) {
4315 got_object_commit_close(commit);
4316 err = got_error_from_errno("view_open");
4317 break;
4319 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4320 id, NULL, s->refs, s->repo);
4321 got_object_commit_close(commit);
4322 if (err) {
4323 view_close(diff_view);
4324 break;
4326 if (view_is_parent_view(view)) {
4327 err = view_close_child(view);
4328 if (err)
4329 break;
4330 err = view_set_child(view, diff_view);
4331 if (err) {
4332 view_close(diff_view);
4333 break;
4335 *focus_view = diff_view;
4336 view->child_focussed = 1;
4337 } else
4338 *new_view = diff_view;
4339 if (err)
4340 break;
4341 break;
4343 case KEY_NPAGE:
4344 case CTRL('f'):
4345 case ' ':
4346 if (s->last_displayed_line >= s->blame.nlines &&
4347 s->selected_line >= MIN(s->blame.nlines,
4348 view->nlines - 2)) {
4349 break;
4351 if (s->last_displayed_line >= s->blame.nlines &&
4352 s->selected_line < view->nlines - 2) {
4353 s->selected_line = MIN(s->blame.nlines,
4354 view->nlines - 2);
4355 break;
4357 if (s->last_displayed_line + view->nlines - 2
4358 <= s->blame.nlines)
4359 s->first_displayed_line +=
4360 view->nlines - 2;
4361 else
4362 s->first_displayed_line =
4363 s->blame.nlines -
4364 (view->nlines - 3);
4365 break;
4366 case KEY_RESIZE:
4367 if (s->selected_line > view->nlines - 2) {
4368 s->selected_line = MIN(s->blame.nlines,
4369 view->nlines - 2);
4371 break;
4372 default:
4373 break;
4375 return thread_err ? thread_err : err;
4378 static const struct got_error *
4379 cmd_blame(int argc, char *argv[])
4381 const struct got_error *error;
4382 struct got_repository *repo = NULL;
4383 struct got_reflist_head refs;
4384 struct got_worktree *worktree = NULL;
4385 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4386 struct got_object_id *commit_id = NULL;
4387 char *commit_id_str = NULL;
4388 int ch;
4389 struct tog_view *view;
4391 SIMPLEQ_INIT(&refs);
4393 #ifndef PROFILE
4394 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4395 NULL) == -1)
4396 err(1, "pledge");
4397 #endif
4399 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4400 switch (ch) {
4401 case 'c':
4402 commit_id_str = optarg;
4403 break;
4404 case 'r':
4405 repo_path = realpath(optarg, NULL);
4406 if (repo_path == NULL)
4407 return got_error_from_errno2("realpath",
4408 optarg);
4409 break;
4410 default:
4411 usage_blame();
4412 /* NOTREACHED */
4416 argc -= optind;
4417 argv += optind;
4419 if (argc != 1)
4420 usage_blame();
4422 cwd = getcwd(NULL, 0);
4423 if (cwd == NULL)
4424 return got_error_from_errno("getcwd");
4426 error = got_worktree_open(&worktree, cwd);
4427 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4428 goto done;
4430 if (repo_path == NULL) {
4431 if (worktree)
4432 repo_path =
4433 strdup(got_worktree_get_repo_path(worktree));
4434 else
4435 repo_path = strdup(cwd);
4437 if (repo_path == NULL) {
4438 error = got_error_from_errno("strdup");
4439 goto done;
4442 error = got_repo_open(&repo, repo_path, NULL);
4443 if (error != NULL)
4444 goto done;
4446 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4447 worktree);
4448 if (error)
4449 goto done;
4451 init_curses();
4453 error = apply_unveil(got_repo_get_path(repo), NULL);
4454 if (error)
4455 goto done;
4457 if (commit_id_str == NULL) {
4458 struct got_reference *head_ref;
4459 error = got_ref_open(&head_ref, repo, worktree ?
4460 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4461 if (error != NULL)
4462 goto done;
4463 error = got_ref_resolve(&commit_id, repo, head_ref);
4464 got_ref_close(head_ref);
4465 } else {
4466 error = got_repo_match_object_id(&commit_id, NULL,
4467 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4469 if (error != NULL)
4470 goto done;
4472 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4473 if (error)
4474 goto done;
4476 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4477 if (view == NULL) {
4478 error = got_error_from_errno("view_open");
4479 goto done;
4481 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4482 if (error)
4483 goto done;
4484 if (worktree) {
4485 /* Release work tree lock. */
4486 got_worktree_close(worktree);
4487 worktree = NULL;
4489 error = view_loop(view);
4490 done:
4491 free(repo_path);
4492 free(in_repo_path);
4493 free(cwd);
4494 free(commit_id);
4495 if (worktree)
4496 got_worktree_close(worktree);
4497 if (repo)
4498 got_repo_close(repo);
4499 got_ref_list_free(&refs);
4500 return error;
4503 static const struct got_error *
4504 draw_tree_entries(struct tog_view *view,
4505 struct got_tree_entry **first_displayed_entry,
4506 struct got_tree_entry **last_displayed_entry,
4507 struct got_tree_entry **selected_entry, int *ndisplayed,
4508 const char *label, int show_ids, const char *parent_path,
4509 struct got_tree_object *tree, int selected, int limit,
4510 int isroot, struct tog_colors *colors)
4512 const struct got_error *err = NULL;
4513 struct got_tree_entry *te;
4514 wchar_t *wline;
4515 struct tog_color *tc;
4516 int width, n, i, nentries;
4518 *ndisplayed = 0;
4520 werase(view->window);
4522 if (limit == 0)
4523 return NULL;
4525 err = format_line(&wline, &width, label, view->ncols, 0);
4526 if (err)
4527 return err;
4528 if (view_needs_focus_indication(view))
4529 wstandout(view->window);
4530 tc = get_color(colors, TOG_COLOR_COMMIT);
4531 if (tc)
4532 wattr_on(view->window,
4533 COLOR_PAIR(tc->colorpair), NULL);
4534 waddwstr(view->window, wline);
4535 if (tc)
4536 wattr_off(view->window,
4537 COLOR_PAIR(tc->colorpair), NULL);
4538 if (view_needs_focus_indication(view))
4539 wstandend(view->window);
4540 free(wline);
4541 wline = NULL;
4542 if (width < view->ncols - 1)
4543 waddch(view->window, '\n');
4544 if (--limit <= 0)
4545 return NULL;
4546 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4547 if (err)
4548 return err;
4549 waddwstr(view->window, wline);
4550 free(wline);
4551 wline = NULL;
4552 if (width < view->ncols - 1)
4553 waddch(view->window, '\n');
4554 if (--limit <= 0)
4555 return NULL;
4556 waddch(view->window, '\n');
4557 if (--limit <= 0)
4558 return NULL;
4560 if (*first_displayed_entry == NULL) {
4561 te = got_object_tree_get_first_entry(tree);
4562 if (selected == 0) {
4563 if (view->focussed)
4564 wstandout(view->window);
4565 *selected_entry = NULL;
4567 waddstr(view->window, " ..\n"); /* parent directory */
4568 if (selected == 0 && view->focussed)
4569 wstandend(view->window);
4570 (*ndisplayed)++;
4571 if (--limit <= 0)
4572 return NULL;
4573 n = 1;
4574 } else {
4575 n = 0;
4576 te = *first_displayed_entry;
4579 nentries = got_object_tree_get_nentries(tree);
4580 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4581 char *line = NULL, *id_str = NULL;
4582 const char *modestr = "";
4583 mode_t mode;
4585 te = got_object_tree_get_entry(tree, i);
4586 mode = got_tree_entry_get_mode(te);
4588 if (show_ids) {
4589 err = got_object_id_str(&id_str,
4590 got_tree_entry_get_id(te));
4591 if (err)
4592 return got_error_from_errno(
4593 "got_object_id_str");
4595 if (got_object_tree_entry_is_submodule(te))
4596 modestr = "$";
4597 else if (S_ISLNK(mode))
4598 modestr = "@";
4599 else if (S_ISDIR(mode))
4600 modestr = "/";
4601 else if (mode & S_IXUSR)
4602 modestr = "*";
4603 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4604 got_tree_entry_get_name(te), modestr) == -1) {
4605 free(id_str);
4606 return got_error_from_errno("asprintf");
4608 free(id_str);
4609 err = format_line(&wline, &width, line, view->ncols, 0);
4610 if (err) {
4611 free(line);
4612 break;
4614 if (n == selected) {
4615 if (view->focussed)
4616 wstandout(view->window);
4617 *selected_entry = te;
4619 tc = match_color(colors, line);
4620 if (tc)
4621 wattr_on(view->window,
4622 COLOR_PAIR(tc->colorpair), NULL);
4623 waddwstr(view->window, wline);
4624 if (tc)
4625 wattr_off(view->window,
4626 COLOR_PAIR(tc->colorpair), NULL);
4627 if (width < view->ncols - 1)
4628 waddch(view->window, '\n');
4629 if (n == selected && view->focussed)
4630 wstandend(view->window);
4631 free(line);
4632 free(wline);
4633 wline = NULL;
4634 n++;
4635 (*ndisplayed)++;
4636 *last_displayed_entry = te;
4637 if (--limit <= 0)
4638 break;
4641 return err;
4644 static void
4645 tree_scroll_up(struct tog_view *view,
4646 struct got_tree_entry **first_displayed_entry, int maxscroll,
4647 struct got_tree_object *tree, int isroot)
4649 struct got_tree_entry *te;
4650 int i;
4652 if (*first_displayed_entry == NULL)
4653 return;
4655 te = got_object_tree_get_entry(tree, 0);
4656 if (*first_displayed_entry == te) {
4657 if (!isroot)
4658 *first_displayed_entry = NULL;
4659 return;
4662 i = 0;
4663 while (*first_displayed_entry && i < maxscroll) {
4664 *first_displayed_entry = got_tree_entry_get_prev(tree,
4665 *first_displayed_entry);
4666 i++;
4668 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4669 *first_displayed_entry = NULL;
4672 static int
4673 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4674 struct got_tree_entry *last_displayed_entry,
4675 struct got_tree_object *tree)
4677 struct got_tree_entry *next, *last;
4678 int n = 0;
4680 if (*first_displayed_entry)
4681 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4682 else
4683 next = got_object_tree_get_first_entry(tree);
4685 last = last_displayed_entry;
4686 while (next && last && n++ < maxscroll) {
4687 last = got_tree_entry_get_next(tree, last);
4688 if (last) {
4689 *first_displayed_entry = next;
4690 next = got_tree_entry_get_next(tree, next);
4693 return n;
4696 static const struct got_error *
4697 tree_entry_path(char **path, struct tog_parent_trees *parents,
4698 struct got_tree_entry *te)
4700 const struct got_error *err = NULL;
4701 struct tog_parent_tree *pt;
4702 size_t len = 2; /* for leading slash and NUL */
4704 TAILQ_FOREACH(pt, parents, entry)
4705 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4706 + 1 /* slash */;
4707 if (te)
4708 len += strlen(got_tree_entry_get_name(te));
4710 *path = calloc(1, len);
4711 if (path == NULL)
4712 return got_error_from_errno("calloc");
4714 (*path)[0] = '/';
4715 pt = TAILQ_LAST(parents, tog_parent_trees);
4716 while (pt) {
4717 const char *name = got_tree_entry_get_name(pt->selected_entry);
4718 if (strlcat(*path, name, len) >= len) {
4719 err = got_error(GOT_ERR_NO_SPACE);
4720 goto done;
4722 if (strlcat(*path, "/", len) >= len) {
4723 err = got_error(GOT_ERR_NO_SPACE);
4724 goto done;
4726 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4728 if (te) {
4729 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4730 err = got_error(GOT_ERR_NO_SPACE);
4731 goto done;
4734 done:
4735 if (err) {
4736 free(*path);
4737 *path = NULL;
4739 return err;
4742 static const struct got_error *
4743 blame_tree_entry(struct tog_view **new_view, int begin_x,
4744 struct got_tree_entry *te, struct tog_parent_trees *parents,
4745 struct got_object_id *commit_id, struct got_reflist_head *refs,
4746 struct got_repository *repo)
4748 const struct got_error *err = NULL;
4749 char *path;
4750 struct tog_view *blame_view;
4752 *new_view = NULL;
4754 err = tree_entry_path(&path, parents, te);
4755 if (err)
4756 return err;
4758 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4759 if (blame_view == NULL) {
4760 err = got_error_from_errno("view_open");
4761 goto done;
4764 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4765 if (err) {
4766 if (err->code == GOT_ERR_CANCELLED)
4767 err = NULL;
4768 view_close(blame_view);
4769 } else
4770 *new_view = blame_view;
4771 done:
4772 free(path);
4773 return err;
4776 static const struct got_error *
4777 log_tree_entry(struct tog_view **new_view, int begin_x,
4778 struct got_tree_entry *te, struct tog_parent_trees *parents,
4779 struct got_object_id *commit_id, struct got_reflist_head *refs,
4780 struct got_repository *repo)
4782 struct tog_view *log_view;
4783 const struct got_error *err = NULL;
4784 char *path;
4786 *new_view = NULL;
4788 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4789 if (log_view == NULL)
4790 return got_error_from_errno("view_open");
4792 err = tree_entry_path(&path, parents, te);
4793 if (err)
4794 return err;
4796 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4797 if (err)
4798 view_close(log_view);
4799 else
4800 *new_view = log_view;
4801 free(path);
4802 return err;
4805 static const struct got_error *
4806 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4807 struct got_object_id *commit_id, struct got_reflist_head *refs,
4808 struct got_repository *repo)
4810 const struct got_error *err = NULL;
4811 char *commit_id_str = NULL;
4812 struct tog_tree_view_state *s = &view->state.tree;
4814 TAILQ_INIT(&s->parents);
4816 err = got_object_id_str(&commit_id_str, commit_id);
4817 if (err != NULL)
4818 goto done;
4820 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4821 err = got_error_from_errno("asprintf");
4822 goto done;
4825 s->root = s->tree = root;
4826 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4827 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4828 s->commit_id = got_object_id_dup(commit_id);
4829 if (s->commit_id == NULL) {
4830 err = got_error_from_errno("got_object_id_dup");
4831 goto done;
4833 s->refs = refs;
4834 s->repo = repo;
4836 SIMPLEQ_INIT(&s->colors);
4838 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4839 err = add_color(&s->colors, "\\$$",
4840 TOG_COLOR_TREE_SUBMODULE,
4841 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4842 if (err)
4843 goto done;
4844 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4845 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4846 if (err) {
4847 free_colors(&s->colors);
4848 goto done;
4850 err = add_color(&s->colors, "/$",
4851 TOG_COLOR_TREE_DIRECTORY,
4852 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4853 if (err) {
4854 free_colors(&s->colors);
4855 goto done;
4858 err = add_color(&s->colors, "\\*$",
4859 TOG_COLOR_TREE_EXECUTABLE,
4860 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4861 if (err) {
4862 free_colors(&s->colors);
4863 goto done;
4866 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4867 get_color_value("TOG_COLOR_COMMIT"));
4868 if (err) {
4869 free_colors(&s->colors);
4870 goto done;
4874 view->show = show_tree_view;
4875 view->input = input_tree_view;
4876 view->close = close_tree_view;
4877 view->search_start = search_start_tree_view;
4878 view->search_next = search_next_tree_view;
4879 done:
4880 free(commit_id_str);
4881 if (err) {
4882 free(s->tree_label);
4883 s->tree_label = NULL;
4885 return err;
4888 static const struct got_error *
4889 close_tree_view(struct tog_view *view)
4891 struct tog_tree_view_state *s = &view->state.tree;
4893 free_colors(&s->colors);
4894 free(s->tree_label);
4895 s->tree_label = NULL;
4896 free(s->commit_id);
4897 s->commit_id = NULL;
4898 while (!TAILQ_EMPTY(&s->parents)) {
4899 struct tog_parent_tree *parent;
4900 parent = TAILQ_FIRST(&s->parents);
4901 TAILQ_REMOVE(&s->parents, parent, entry);
4902 free(parent);
4905 if (s->tree != s->root)
4906 got_object_tree_close(s->tree);
4907 got_object_tree_close(s->root);
4909 return NULL;
4912 static const struct got_error *
4913 search_start_tree_view(struct tog_view *view)
4915 struct tog_tree_view_state *s = &view->state.tree;
4917 s->matched_entry = NULL;
4918 return NULL;
4921 static int
4922 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4924 regmatch_t regmatch;
4926 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4927 0) == 0;
4930 static const struct got_error *
4931 search_next_tree_view(struct tog_view *view)
4933 struct tog_tree_view_state *s = &view->state.tree;
4934 struct got_tree_entry *te = NULL;
4936 if (!view->searching) {
4937 view->search_next_done = 1;
4938 return NULL;
4941 if (s->matched_entry) {
4942 if (view->searching == TOG_SEARCH_FORWARD) {
4943 if (s->selected_entry)
4944 te = got_tree_entry_get_next(s->tree,
4945 s->selected_entry);
4946 else
4947 te = got_object_tree_get_first_entry(s->tree);
4948 } else {
4949 if (s->selected_entry == NULL)
4950 te = got_object_tree_get_last_entry(s->tree);
4951 else
4952 te = got_tree_entry_get_prev(s->tree,
4953 s->selected_entry);
4955 } else {
4956 if (view->searching == TOG_SEARCH_FORWARD)
4957 te = got_object_tree_get_first_entry(s->tree);
4958 else
4959 te = got_object_tree_get_last_entry(s->tree);
4962 while (1) {
4963 if (te == NULL) {
4964 if (s->matched_entry == NULL) {
4965 view->search_next_done = 1;
4966 return NULL;
4968 if (view->searching == TOG_SEARCH_FORWARD)
4969 te = got_object_tree_get_first_entry(s->tree);
4970 else
4971 te = got_object_tree_get_last_entry(s->tree);
4974 if (match_tree_entry(te, &view->regex)) {
4975 view->search_next_done = 1;
4976 s->matched_entry = te;
4977 break;
4980 if (view->searching == TOG_SEARCH_FORWARD)
4981 te = got_tree_entry_get_next(s->tree, te);
4982 else
4983 te = got_tree_entry_get_prev(s->tree, te);
4986 if (s->matched_entry) {
4987 s->first_displayed_entry = s->matched_entry;
4988 s->selected = 0;
4991 return NULL;
4994 static const struct got_error *
4995 show_tree_view(struct tog_view *view)
4997 const struct got_error *err = NULL;
4998 struct tog_tree_view_state *s = &view->state.tree;
4999 char *parent_path;
5001 err = tree_entry_path(&parent_path, &s->parents, NULL);
5002 if (err)
5003 return err;
5005 err = draw_tree_entries(view, &s->first_displayed_entry,
5006 &s->last_displayed_entry, &s->selected_entry,
5007 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5008 s->tree, s->selected, view->nlines, s->tree == s->root,
5009 &s->colors);
5010 free(parent_path);
5012 view_vborder(view);
5013 return err;
5016 static const struct got_error *
5017 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5018 struct tog_view **focus_view, struct tog_view *view, int ch)
5020 const struct got_error *err = NULL;
5021 struct tog_tree_view_state *s = &view->state.tree;
5022 struct tog_view *log_view;
5023 int begin_x = 0, nscrolled;
5025 switch (ch) {
5026 case 'i':
5027 s->show_ids = !s->show_ids;
5028 break;
5029 case 'l':
5030 if (!s->selected_entry)
5031 break;
5032 if (view_is_parent_view(view))
5033 begin_x = view_split_begin_x(view->begin_x);
5034 err = log_tree_entry(&log_view, begin_x,
5035 s->selected_entry, &s->parents,
5036 s->commit_id, s->refs, s->repo);
5037 if (view_is_parent_view(view)) {
5038 err = view_close_child(view);
5039 if (err)
5040 return err;
5041 err = view_set_child(view, log_view);
5042 if (err) {
5043 view_close(log_view);
5044 break;
5046 *focus_view = log_view;
5047 view->child_focussed = 1;
5048 } else
5049 *new_view = log_view;
5050 break;
5051 case 'k':
5052 case KEY_UP:
5053 if (s->selected > 0) {
5054 s->selected--;
5055 if (s->selected == 0)
5056 break;
5058 if (s->selected > 0)
5059 break;
5060 tree_scroll_up(view, &s->first_displayed_entry, 1,
5061 s->tree, s->tree == s->root);
5062 break;
5063 case KEY_PPAGE:
5064 case CTRL('b'):
5065 tree_scroll_up(view, &s->first_displayed_entry,
5066 MAX(0, view->nlines - 4 - s->selected), s->tree,
5067 s->tree == s->root);
5068 s->selected = 0;
5069 if (got_object_tree_get_first_entry(s->tree) ==
5070 s->first_displayed_entry && s->tree != s->root)
5071 s->first_displayed_entry = NULL;
5072 break;
5073 case 'j':
5074 case KEY_DOWN:
5075 if (s->selected < s->ndisplayed - 1) {
5076 s->selected++;
5077 break;
5079 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5080 == NULL)
5081 /* can't scroll any further */
5082 break;
5083 tree_scroll_down(&s->first_displayed_entry, 1,
5084 s->last_displayed_entry, s->tree);
5085 break;
5086 case KEY_NPAGE:
5087 case CTRL('f'):
5088 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5089 == NULL) {
5090 /* can't scroll any further; move cursor down */
5091 if (s->selected < s->ndisplayed - 1)
5092 s->selected = s->ndisplayed - 1;
5093 break;
5095 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5096 view->nlines, s->last_displayed_entry, s->tree);
5097 if (nscrolled < view->nlines) {
5098 int ndisplayed = 0;
5099 struct got_tree_entry *te;
5100 te = s->first_displayed_entry;
5101 do {
5102 ndisplayed++;
5103 te = got_tree_entry_get_next(s->tree, te);
5104 } while (te);
5105 s->selected = ndisplayed - 1;
5107 break;
5108 case KEY_ENTER:
5109 case '\r':
5110 case KEY_BACKSPACE:
5111 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5112 struct tog_parent_tree *parent;
5113 /* user selected '..' */
5114 if (s->tree == s->root)
5115 break;
5116 parent = TAILQ_FIRST(&s->parents);
5117 TAILQ_REMOVE(&s->parents, parent,
5118 entry);
5119 got_object_tree_close(s->tree);
5120 s->tree = parent->tree;
5121 s->first_displayed_entry =
5122 parent->first_displayed_entry;
5123 s->selected_entry =
5124 parent->selected_entry;
5125 s->selected = parent->selected;
5126 free(parent);
5127 } else if (S_ISDIR(got_tree_entry_get_mode(
5128 s->selected_entry))) {
5129 struct got_tree_object *subtree;
5130 err = got_object_open_as_tree(&subtree, s->repo,
5131 got_tree_entry_get_id(s->selected_entry));
5132 if (err)
5133 break;
5134 err = tree_view_visit_subtree(subtree, s);
5135 if (err) {
5136 got_object_tree_close(subtree);
5137 break;
5139 } else if (S_ISREG(got_tree_entry_get_mode(
5140 s->selected_entry))) {
5141 struct tog_view *blame_view;
5142 int begin_x = view_is_parent_view(view) ?
5143 view_split_begin_x(view->begin_x) : 0;
5145 err = blame_tree_entry(&blame_view, begin_x,
5146 s->selected_entry, &s->parents,
5147 s->commit_id, s->refs, s->repo);
5148 if (err)
5149 break;
5150 if (view_is_parent_view(view)) {
5151 err = view_close_child(view);
5152 if (err)
5153 return err;
5154 err = view_set_child(view, blame_view);
5155 if (err) {
5156 view_close(blame_view);
5157 break;
5159 *focus_view = blame_view;
5160 view->child_focussed = 1;
5161 } else
5162 *new_view = blame_view;
5164 break;
5165 case KEY_RESIZE:
5166 if (s->selected > view->nlines)
5167 s->selected = s->ndisplayed - 1;
5168 break;
5169 default:
5170 break;
5173 return err;
5176 __dead static void
5177 usage_tree(void)
5179 endwin();
5180 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5181 getprogname());
5182 exit(1);
5185 static const struct got_error *
5186 cmd_tree(int argc, char *argv[])
5188 const struct got_error *error;
5189 struct got_repository *repo = NULL;
5190 struct got_worktree *worktree = NULL;
5191 struct got_reflist_head refs;
5192 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5193 struct got_object_id *commit_id = NULL;
5194 char *commit_id_arg = NULL;
5195 struct got_commit_object *commit = NULL;
5196 struct got_tree_object *tree = NULL;
5197 int ch;
5198 struct tog_view *view;
5200 SIMPLEQ_INIT(&refs);
5202 #ifndef PROFILE
5203 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5204 NULL) == -1)
5205 err(1, "pledge");
5206 #endif
5208 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5209 switch (ch) {
5210 case 'c':
5211 commit_id_arg = optarg;
5212 break;
5213 case 'r':
5214 repo_path = realpath(optarg, NULL);
5215 if (repo_path == NULL)
5216 return got_error_from_errno2("realpath",
5217 optarg);
5218 break;
5219 default:
5220 usage_tree();
5221 /* NOTREACHED */
5225 argc -= optind;
5226 argv += optind;
5228 if (argc > 1)
5229 usage_tree();
5231 cwd = getcwd(NULL, 0);
5232 if (cwd == NULL)
5233 return got_error_from_errno("getcwd");
5235 error = got_worktree_open(&worktree, cwd);
5236 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5237 goto done;
5239 if (repo_path == NULL) {
5240 if (worktree)
5241 repo_path =
5242 strdup(got_worktree_get_repo_path(worktree));
5243 else
5244 repo_path = strdup(cwd);
5246 if (repo_path == NULL) {
5247 error = got_error_from_errno("strdup");
5248 goto done;
5251 error = got_repo_open(&repo, repo_path, NULL);
5252 if (error != NULL)
5253 goto done;
5255 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5256 repo, worktree);
5257 if (error)
5258 goto done;
5260 init_curses();
5262 error = apply_unveil(got_repo_get_path(repo), NULL);
5263 if (error)
5264 goto done;
5266 error = got_repo_match_object_id(&commit_id, NULL,
5267 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5268 GOT_OBJ_TYPE_COMMIT, 1, repo);
5269 if (error)
5270 goto done;
5272 error = got_object_open_as_commit(&commit, repo, commit_id);
5273 if (error)
5274 goto done;
5276 error = got_object_open_as_tree(&tree, repo,
5277 got_object_commit_get_tree_id(commit));
5278 if (error)
5279 goto done;
5281 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5282 if (error)
5283 goto done;
5285 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5286 if (view == NULL) {
5287 error = got_error_from_errno("view_open");
5288 goto done;
5290 error = open_tree_view(view, tree, commit_id, &refs, repo);
5291 if (error)
5292 goto done;
5293 if (!got_path_is_root_dir(in_repo_path)) {
5294 error = tree_view_walk_path(&view->state.tree, commit_id,
5295 in_repo_path, repo);
5296 if (error)
5297 goto done;
5300 if (worktree) {
5301 /* Release work tree lock. */
5302 got_worktree_close(worktree);
5303 worktree = NULL;
5305 error = view_loop(view);
5306 done:
5307 free(repo_path);
5308 free(cwd);
5309 free(commit_id);
5310 if (commit)
5311 got_object_commit_close(commit);
5312 if (tree)
5313 got_object_tree_close(tree);
5314 if (repo)
5315 got_repo_close(repo);
5316 got_ref_list_free(&refs);
5317 return error;
5320 static void
5321 list_commands(void)
5323 int i;
5325 fprintf(stderr, "commands:");
5326 for (i = 0; i < nitems(tog_commands); i++) {
5327 struct tog_cmd *cmd = &tog_commands[i];
5328 fprintf(stderr, " %s", cmd->name);
5330 fputc('\n', stderr);
5333 __dead static void
5334 usage(int hflag)
5336 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5337 getprogname());
5338 if (hflag)
5339 list_commands();
5340 exit(1);
5343 static char **
5344 make_argv(const char *arg0, const char *arg1)
5346 char **argv;
5347 int argc = (arg1 == NULL ? 1 : 2);
5349 argv = calloc(argc, sizeof(char *));
5350 if (argv == NULL)
5351 err(1, "calloc");
5352 argv[0] = strdup(arg0);
5353 if (argv[0] == NULL)
5354 err(1, "strdup");
5355 if (arg1) {
5356 argv[1] = strdup(arg1);
5357 if (argv[1] == NULL)
5358 err(1, "strdup");
5361 return argv;
5364 int
5365 main(int argc, char *argv[])
5367 const struct got_error *error = NULL;
5368 struct tog_cmd *cmd = NULL;
5369 int ch, hflag = 0, Vflag = 0;
5370 char **cmd_argv = NULL;
5371 static struct option longopts[] = {
5372 { "version", no_argument, NULL, 'V' },
5373 { NULL, 0, NULL, 0}
5376 setlocale(LC_CTYPE, "");
5378 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5379 switch (ch) {
5380 case 'h':
5381 hflag = 1;
5382 break;
5383 case 'V':
5384 Vflag = 1;
5385 break;
5386 default:
5387 usage(hflag);
5388 /* NOTREACHED */
5392 argc -= optind;
5393 argv += optind;
5394 optind = 0;
5395 optreset = 1;
5397 if (Vflag) {
5398 got_version_print_str();
5399 return 1;
5402 if (argc == 0) {
5403 if (hflag)
5404 usage(hflag);
5405 /* Build an argument vector which runs a default command. */
5406 cmd = &tog_commands[0];
5407 cmd_argv = make_argv(cmd->name, NULL);
5408 argc = 1;
5409 } else {
5410 int i;
5412 /* Did the user specific a command? */
5413 for (i = 0; i < nitems(tog_commands); i++) {
5414 if (strncmp(tog_commands[i].name, argv[0],
5415 strlen(argv[0])) == 0) {
5416 cmd = &tog_commands[i];
5417 break;
5421 if (cmd == NULL) {
5422 fprintf(stderr, "%s: unknown command '%s'\n",
5423 getprogname(), argv[0]);
5424 list_commands();
5425 return 1;
5429 if (hflag)
5430 cmd->cmd_usage();
5431 else
5432 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5434 endwin();
5435 free(cmd_argv);
5436 if (error && error->code != GOT_ERR_CANCELLED)
5437 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5438 return 0;