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, 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 *,
472 struct got_reflist_head *, 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;
1752 static const struct got_error *
1753 browse_commit_tree(struct tog_view **new_view, int begin_x,
1754 struct commit_queue_entry *entry, const char *path,
1755 struct got_reflist_head *refs, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree;
1759 struct tog_tree_view_state *s;
1760 struct tog_view *tree_view;
1761 char *slash, *subpath = NULL;
1762 const char *p;
1764 err = got_object_open_as_tree(&tree, repo,
1765 got_object_commit_get_tree_id(entry->commit));
1766 if (err)
1767 return err;
1769 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1770 if (tree_view == NULL)
1771 return got_error_from_errno("view_open");
1773 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1774 if (err) {
1775 got_object_tree_close(tree);
1776 return err;
1778 s = &tree_view->state.tree;
1780 *new_view = tree_view;
1782 if (got_path_is_root_dir(path))
1783 return NULL;
1785 /* Walk the path and open corresponding tree objects. */
1786 p = path;
1787 while (*p) {
1788 struct got_tree_entry *te;
1789 struct got_object_id *tree_id;
1790 char *te_name;
1792 while (p[0] == '/')
1793 p++;
1795 /* Ensure the correct subtree entry is selected. */
1796 slash = strchr(p, '/');
1797 if (slash == NULL)
1798 te_name = strdup(p);
1799 else
1800 te_name = strndup(p, slash - p);
1801 if (te_name == NULL) {
1802 err = got_error_from_errno("strndup");
1803 break;
1805 te = got_object_tree_find_entry(s->tree, te_name);
1806 if (te == NULL) {
1807 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1808 free(te_name);
1809 break;
1811 free(te_name);
1812 s->selected_entry = te;
1813 s->selected = got_tree_entry_get_index(te);
1814 if (s->tree != s->root)
1815 s->selected++; /* skip '..' */
1817 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1818 /* Jump to this file's entry. */
1819 s->first_displayed_entry = s->selected_entry;
1820 s->selected = 0;
1821 break;
1824 slash = strchr(p, '/');
1825 if (slash)
1826 subpath = strndup(path, slash - path);
1827 else
1828 subpath = strdup(path);
1829 if (subpath == NULL) {
1830 err = got_error_from_errno("strdup");
1831 break;
1834 err = got_object_id_by_path(&tree_id, repo, entry->id,
1835 subpath);
1836 if (err)
1837 break;
1839 err = got_object_open_as_tree(&tree, repo, tree_id);
1840 free(tree_id);
1841 if (err)
1842 break;
1844 err = tree_view_visit_subtree(tree, s);
1845 if (err) {
1846 got_object_tree_close(tree);
1847 break;
1849 if (slash == NULL)
1850 break;
1851 free(subpath);
1852 subpath = NULL;
1853 p = slash;
1856 free(subpath);
1857 return err;
1860 static const struct got_error *
1861 block_signals_used_by_main_thread(void)
1863 sigset_t sigset;
1864 int errcode;
1866 if (sigemptyset(&sigset) == -1)
1867 return got_error_from_errno("sigemptyset");
1869 /* tog handles SIGWINCH and SIGCONT */
1870 if (sigaddset(&sigset, SIGWINCH) == -1)
1871 return got_error_from_errno("sigaddset");
1872 if (sigaddset(&sigset, SIGCONT) == -1)
1873 return got_error_from_errno("sigaddset");
1875 /* ncurses handles SIGTSTP */
1876 if (sigaddset(&sigset, SIGTSTP) == -1)
1877 return got_error_from_errno("sigaddset");
1879 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1880 if (errcode)
1881 return got_error_set_errno(errcode, "pthread_sigmask");
1883 return NULL;
1886 static void *
1887 log_thread(void *arg)
1889 const struct got_error *err = NULL;
1890 int errcode = 0;
1891 struct tog_log_thread_args *a = arg;
1892 int done = 0;
1894 err = block_signals_used_by_main_thread();
1895 if (err)
1896 return (void *)err;
1898 while (!done && !err && !tog_sigpipe_received) {
1899 err = queue_commits(a->graph, a->commits, 1, a->repo,
1900 a->in_repo_path, a->searching, a->search_next_done,
1901 a->regex);
1902 if (err) {
1903 if (err->code != GOT_ERR_ITER_COMPLETED)
1904 return (void *)err;
1905 err = NULL;
1906 done = 1;
1907 } else if (a->commits_needed > 0)
1908 a->commits_needed--;
1910 errcode = pthread_mutex_lock(&tog_mutex);
1911 if (errcode) {
1912 err = got_error_set_errno(errcode,
1913 "pthread_mutex_lock");
1914 break;
1915 } else if (*a->quit)
1916 done = 1;
1917 else if (*a->first_displayed_entry == NULL) {
1918 *a->first_displayed_entry =
1919 TAILQ_FIRST(&a->commits->head);
1920 *a->selected_entry = *a->first_displayed_entry;
1923 if (done)
1924 a->commits_needed = 0;
1925 else if (a->commits_needed == 0) {
1926 errcode = pthread_cond_wait(&a->need_commits,
1927 &tog_mutex);
1928 if (errcode)
1929 err = got_error_set_errno(errcode,
1930 "pthread_cond_wait");
1933 errcode = pthread_mutex_unlock(&tog_mutex);
1934 if (errcode && err == NULL)
1935 err = got_error_set_errno(errcode,
1936 "pthread_mutex_unlock");
1938 a->log_complete = 1;
1939 return (void *)err;
1942 static const struct got_error *
1943 stop_log_thread(struct tog_log_view_state *s)
1945 const struct got_error *err = NULL;
1946 int errcode;
1948 if (s->thread) {
1949 s->quit = 1;
1950 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1951 if (errcode)
1952 return got_error_set_errno(errcode,
1953 "pthread_cond_signal");
1954 errcode = pthread_mutex_unlock(&tog_mutex);
1955 if (errcode)
1956 return got_error_set_errno(errcode,
1957 "pthread_mutex_unlock");
1958 errcode = pthread_join(s->thread, (void **)&err);
1959 if (errcode)
1960 return got_error_set_errno(errcode, "pthread_join");
1961 errcode = pthread_mutex_lock(&tog_mutex);
1962 if (errcode)
1963 return got_error_set_errno(errcode,
1964 "pthread_mutex_lock");
1965 s->thread = NULL;
1968 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1969 if (errcode && err == NULL)
1970 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1972 if (s->thread_args.repo) {
1973 got_repo_close(s->thread_args.repo);
1974 s->thread_args.repo = NULL;
1977 if (s->thread_args.graph) {
1978 got_commit_graph_close(s->thread_args.graph);
1979 s->thread_args.graph = NULL;
1982 return err;
1985 static const struct got_error *
1986 close_log_view(struct tog_view *view)
1988 const struct got_error *err = NULL;
1989 struct tog_log_view_state *s = &view->state.log;
1991 err = stop_log_thread(s);
1992 free_commits(&s->commits);
1993 free(s->in_repo_path);
1994 s->in_repo_path = NULL;
1995 free(s->start_id);
1996 s->start_id = NULL;
1997 return err;
2000 static const struct got_error *
2001 search_start_log_view(struct tog_view *view)
2003 struct tog_log_view_state *s = &view->state.log;
2005 s->matched_entry = NULL;
2006 s->search_entry = NULL;
2007 return NULL;
2010 static const struct got_error *
2011 search_next_log_view(struct tog_view *view)
2013 const struct got_error *err = NULL;
2014 struct tog_log_view_state *s = &view->state.log;
2015 struct commit_queue_entry *entry;
2017 if (!view->searching) {
2018 view->search_next_done = 1;
2019 return NULL;
2022 if (s->search_entry) {
2023 int errcode, ch;
2024 errcode = pthread_mutex_unlock(&tog_mutex);
2025 if (errcode)
2026 return got_error_set_errno(errcode,
2027 "pthread_mutex_unlock");
2028 ch = wgetch(view->window);
2029 errcode = pthread_mutex_lock(&tog_mutex);
2030 if (errcode)
2031 return got_error_set_errno(errcode,
2032 "pthread_mutex_lock");
2033 if (ch == KEY_BACKSPACE) {
2034 view->search_next_done = 1;
2035 return NULL;
2037 if (view->searching == TOG_SEARCH_FORWARD)
2038 entry = TAILQ_NEXT(s->search_entry, entry);
2039 else
2040 entry = TAILQ_PREV(s->search_entry,
2041 commit_queue_head, entry);
2042 } else if (s->matched_entry) {
2043 if (view->searching == TOG_SEARCH_FORWARD)
2044 entry = TAILQ_NEXT(s->selected_entry, entry);
2045 else
2046 entry = TAILQ_PREV(s->selected_entry,
2047 commit_queue_head, entry);
2048 } else {
2049 if (view->searching == TOG_SEARCH_FORWARD)
2050 entry = TAILQ_FIRST(&s->commits.head);
2051 else
2052 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2055 while (1) {
2056 int have_match = 0;
2058 if (entry == NULL) {
2059 if (s->thread_args.log_complete ||
2060 view->searching == TOG_SEARCH_BACKWARD) {
2061 view->search_next_done = 1;
2062 return NULL;
2065 * Poke the log thread for more commits and return,
2066 * allowing the main loop to make progress. Search
2067 * will resume at s->search_entry once we come back.
2069 s->thread_args.commits_needed++;
2070 return trigger_log_thread(1,
2071 &s->thread_args.commits_needed,
2072 &s->thread_args.log_complete,
2073 &s->thread_args.need_commits);
2076 err = match_commit(&have_match, entry->id, entry->commit,
2077 &view->regex);
2078 if (err)
2079 break;
2080 if (have_match) {
2081 view->search_next_done = 1;
2082 s->matched_entry = entry;
2083 break;
2086 s->search_entry = entry;
2087 if (view->searching == TOG_SEARCH_FORWARD)
2088 entry = TAILQ_NEXT(entry, entry);
2089 else
2090 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2093 if (s->matched_entry) {
2094 int cur = s->selected_entry->idx;
2095 while (cur < s->matched_entry->idx) {
2096 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2097 if (err)
2098 return err;
2099 cur++;
2101 while (cur > s->matched_entry->idx) {
2102 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2103 if (err)
2104 return err;
2105 cur--;
2109 s->search_entry = NULL;
2111 return NULL;
2114 static const struct got_error *
2115 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2116 struct got_reflist_head *refs, struct got_repository *repo,
2117 const char *head_ref_name, const char *path, int check_disk,
2118 int log_branches)
2120 const struct got_error *err = NULL;
2121 struct tog_log_view_state *s = &view->state.log;
2122 struct got_repository *thread_repo = NULL;
2123 struct got_commit_graph *thread_graph = NULL;
2124 int errcode;
2126 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2127 if (err != NULL)
2128 goto done;
2130 /* The commit queue only contains commits being displayed. */
2131 TAILQ_INIT(&s->commits.head);
2132 s->commits.ncommits = 0;
2134 s->refs = refs;
2135 s->repo = repo;
2136 s->head_ref_name = head_ref_name;
2137 s->start_id = got_object_id_dup(start_id);
2138 if (s->start_id == NULL) {
2139 err = got_error_from_errno("got_object_id_dup");
2140 goto done;
2142 s->log_branches = log_branches;
2144 SIMPLEQ_INIT(&s->colors);
2145 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2146 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2147 get_color_value("TOG_COLOR_COMMIT"));
2148 if (err)
2149 goto done;
2150 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2151 get_color_value("TOG_COLOR_AUTHOR"));
2152 if (err) {
2153 free_colors(&s->colors);
2154 goto done;
2156 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2157 get_color_value("TOG_COLOR_DATE"));
2158 if (err) {
2159 free_colors(&s->colors);
2160 goto done;
2164 view->show = show_log_view;
2165 view->input = input_log_view;
2166 view->close = close_log_view;
2167 view->search_start = search_start_log_view;
2168 view->search_next = search_next_log_view;
2170 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2171 if (err)
2172 goto done;
2173 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2174 !s->log_branches);
2175 if (err)
2176 goto done;
2177 err = got_commit_graph_iter_start(thread_graph,
2178 s->start_id, s->repo, NULL, NULL);
2179 if (err)
2180 goto done;
2182 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2183 if (errcode) {
2184 err = got_error_set_errno(errcode, "pthread_cond_init");
2185 goto done;
2188 s->thread_args.commits_needed = view->nlines;
2189 s->thread_args.graph = thread_graph;
2190 s->thread_args.commits = &s->commits;
2191 s->thread_args.in_repo_path = s->in_repo_path;
2192 s->thread_args.start_id = s->start_id;
2193 s->thread_args.repo = thread_repo;
2194 s->thread_args.log_complete = 0;
2195 s->thread_args.quit = &s->quit;
2196 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2197 s->thread_args.selected_entry = &s->selected_entry;
2198 s->thread_args.searching = &view->searching;
2199 s->thread_args.search_next_done = &view->search_next_done;
2200 s->thread_args.regex = &view->regex;
2201 done:
2202 if (err)
2203 close_log_view(view);
2204 return err;
2207 static const struct got_error *
2208 show_log_view(struct tog_view *view)
2210 struct tog_log_view_state *s = &view->state.log;
2212 if (s->thread == NULL) {
2213 int errcode = pthread_create(&s->thread, NULL, log_thread,
2214 &s->thread_args);
2215 if (errcode)
2216 return got_error_set_errno(errcode, "pthread_create");
2219 return draw_commits(view, &s->last_displayed_entry,
2220 &s->selected_entry, s->first_displayed_entry,
2221 &s->commits, s->selected, view->nlines, s->refs,
2222 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2225 static const struct got_error *
2226 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2227 struct tog_view **focus_view, struct tog_view *view, int ch)
2229 const struct got_error *err = NULL;
2230 struct tog_log_view_state *s = &view->state.log;
2231 char *parent_path, *in_repo_path = NULL;
2232 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2233 int begin_x = 0;
2234 struct got_object_id *start_id;
2236 switch (ch) {
2237 case 'q':
2238 s->quit = 1;
2239 break;
2240 case 'k':
2241 case KEY_UP:
2242 case '<':
2243 case ',':
2244 if (s->first_displayed_entry == NULL)
2245 break;
2246 if (s->selected > 0)
2247 s->selected--;
2248 else
2249 scroll_up(view, &s->first_displayed_entry, 1,
2250 &s->commits);
2251 break;
2252 case KEY_PPAGE:
2253 case CTRL('b'):
2254 if (s->first_displayed_entry == NULL)
2255 break;
2256 if (TAILQ_FIRST(&s->commits.head) ==
2257 s->first_displayed_entry) {
2258 s->selected = 0;
2259 break;
2261 scroll_up(view, &s->first_displayed_entry,
2262 view->nlines, &s->commits);
2263 break;
2264 case 'j':
2265 case KEY_DOWN:
2266 case '>':
2267 case '.':
2268 if (s->first_displayed_entry == NULL)
2269 break;
2270 if (s->selected < MIN(view->nlines - 2,
2271 s->commits.ncommits - 1)) {
2272 s->selected++;
2273 break;
2275 err = scroll_down(view, &s->first_displayed_entry, 1,
2276 &s->last_displayed_entry, &s->commits,
2277 &s->thread_args.log_complete,
2278 &s->thread_args.commits_needed,
2279 &s->thread_args.need_commits);
2280 break;
2281 case KEY_NPAGE:
2282 case CTRL('f'): {
2283 struct commit_queue_entry *first;
2284 first = s->first_displayed_entry;
2285 if (first == NULL)
2286 break;
2287 err = scroll_down(view, &s->first_displayed_entry,
2288 view->nlines, &s->last_displayed_entry,
2289 &s->commits, &s->thread_args.log_complete,
2290 &s->thread_args.commits_needed,
2291 &s->thread_args.need_commits);
2292 if (err)
2293 break;
2294 if (first == s->first_displayed_entry &&
2295 s->selected < MIN(view->nlines - 2,
2296 s->commits.ncommits - 1)) {
2297 /* can't scroll further down */
2298 s->selected = MIN(view->nlines - 2,
2299 s->commits.ncommits - 1);
2301 err = NULL;
2302 break;
2304 case KEY_RESIZE:
2305 if (s->selected > view->nlines - 2)
2306 s->selected = view->nlines - 2;
2307 if (s->selected > s->commits.ncommits - 1)
2308 s->selected = s->commits.ncommits - 1;
2309 break;
2310 case KEY_ENTER:
2311 case ' ':
2312 case '\r':
2313 if (s->selected_entry == NULL)
2314 break;
2315 if (view_is_parent_view(view))
2316 begin_x = view_split_begin_x(view->begin_x);
2317 err = open_diff_view_for_commit(&diff_view, begin_x,
2318 s->selected_entry->commit, s->selected_entry->id,
2319 view, s->refs, s->repo);
2320 if (err)
2321 break;
2322 if (view_is_parent_view(view)) {
2323 err = view_close_child(view);
2324 if (err)
2325 return err;
2326 err = view_set_child(view, diff_view);
2327 if (err) {
2328 view_close(diff_view);
2329 break;
2331 *focus_view = diff_view;
2332 view->child_focussed = 1;
2333 } else
2334 *new_view = diff_view;
2335 break;
2336 case 't':
2337 if (s->selected_entry == NULL)
2338 break;
2339 if (view_is_parent_view(view))
2340 begin_x = view_split_begin_x(view->begin_x);
2341 err = browse_commit_tree(&tree_view, begin_x,
2342 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2343 if (err)
2344 break;
2345 if (view_is_parent_view(view)) {
2346 err = view_close_child(view);
2347 if (err)
2348 return err;
2349 err = view_set_child(view, tree_view);
2350 if (err) {
2351 view_close(tree_view);
2352 break;
2354 *focus_view = tree_view;
2355 view->child_focussed = 1;
2356 } else
2357 *new_view = tree_view;
2358 break;
2359 case KEY_BACKSPACE:
2360 if (strcmp(s->in_repo_path, "/") == 0)
2361 break;
2362 parent_path = dirname(s->in_repo_path);
2363 if (parent_path && strcmp(parent_path, ".") != 0) {
2364 err = stop_log_thread(s);
2365 if (err)
2366 return err;
2367 lv = view_open(view->nlines, view->ncols,
2368 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2369 if (lv == NULL)
2370 return got_error_from_errno(
2371 "view_open");
2372 err = open_log_view(lv, s->start_id, s->refs,
2373 s->repo, s->head_ref_name, parent_path, 0,
2374 s->log_branches);
2375 if (err)
2376 return err;;
2377 if (view_is_parent_view(view))
2378 *new_view = lv;
2379 else {
2380 view_set_child(view->parent, lv);
2381 *focus_view = lv;
2383 return NULL;
2385 break;
2386 case CTRL('l'):
2387 err = stop_log_thread(s);
2388 if (err)
2389 return err;
2390 lv = view_open(view->nlines, view->ncols,
2391 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2392 if (lv == NULL)
2393 return got_error_from_errno("view_open");
2394 err = got_repo_match_object_id(&start_id, NULL,
2395 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2396 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2397 if (err) {
2398 view_close(lv);
2399 return err;
2401 in_repo_path = strdup(s->in_repo_path);
2402 if (in_repo_path == NULL) {
2403 free(start_id);
2404 view_close(lv);
2405 return got_error_from_errno("strdup");
2407 got_ref_list_free(s->refs);
2408 err = got_ref_list(s->refs, s->repo, NULL,
2409 got_ref_cmp_by_name, NULL);
2410 if (err) {
2411 free(start_id);
2412 view_close(lv);
2413 return err;
2415 err = open_log_view(lv, start_id, s->refs, s->repo,
2416 s->head_ref_name, in_repo_path, 0, s->log_branches);
2417 if (err) {
2418 free(start_id);
2419 view_close(lv);
2420 return err;;
2422 *dead_view = view;
2423 *new_view = lv;
2424 break;
2425 case 'B':
2426 s->log_branches = !s->log_branches;
2427 err = stop_log_thread(s);
2428 if (err)
2429 return err;
2430 lv = view_open(view->nlines, view->ncols,
2431 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2432 if (lv == NULL)
2433 return got_error_from_errno("view_open");
2434 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2435 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2436 if (err) {
2437 view_close(lv);
2438 return err;;
2440 *dead_view = view;
2441 *new_view = lv;
2442 break;
2443 default:
2444 break;
2447 return err;
2450 static const struct got_error *
2451 apply_unveil(const char *repo_path, const char *worktree_path)
2453 const struct got_error *error;
2455 #ifdef PROFILE
2456 if (unveil("gmon.out", "rwc") != 0)
2457 return got_error_from_errno2("unveil", "gmon.out");
2458 #endif
2459 if (repo_path && unveil(repo_path, "r") != 0)
2460 return got_error_from_errno2("unveil", repo_path);
2462 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2463 return got_error_from_errno2("unveil", worktree_path);
2465 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2466 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2468 error = got_privsep_unveil_exec_helpers();
2469 if (error != NULL)
2470 return error;
2472 if (unveil(NULL, NULL) != 0)
2473 return got_error_from_errno("unveil");
2475 return NULL;
2478 static void
2479 init_curses(void)
2481 initscr();
2482 cbreak();
2483 halfdelay(1); /* Do fast refresh while initial view is loading. */
2484 noecho();
2485 nonl();
2486 intrflush(stdscr, FALSE);
2487 keypad(stdscr, TRUE);
2488 curs_set(0);
2489 if (getenv("TOG_COLORS") != NULL) {
2490 start_color();
2491 use_default_colors();
2493 signal(SIGWINCH, tog_sigwinch);
2494 signal(SIGPIPE, tog_sigpipe);
2495 signal(SIGCONT, tog_sigcont);
2498 static const struct got_error *
2499 cmd_log(int argc, char *argv[])
2501 const struct got_error *error;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2504 struct got_reflist_head refs;
2505 struct got_object_id *start_id = NULL;
2506 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2507 char *start_commit = NULL, *head_ref_name = NULL;
2508 int ch, log_branches = 0, check_disk = 1;
2509 struct tog_view *view;
2511 SIMPLEQ_INIT(&refs);
2513 #ifndef PROFILE
2514 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2515 NULL) == -1)
2516 err(1, "pledge");
2517 #endif
2519 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2520 switch (ch) {
2521 case 'b':
2522 log_branches = 1;
2523 break;
2524 case 'c':
2525 start_commit = optarg;
2526 break;
2527 case 'r':
2528 repo_path = realpath(optarg, NULL);
2529 if (repo_path == NULL)
2530 return got_error_from_errno2("realpath",
2531 optarg);
2532 break;
2533 default:
2534 usage_log();
2535 /* NOTREACHED */
2539 argc -= optind;
2540 argv += optind;
2542 cwd = getcwd(NULL, 0);
2543 if (cwd == NULL) {
2544 error = got_error_from_errno("getcwd");
2545 goto done;
2547 error = got_worktree_open(&worktree, cwd);
2548 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2549 goto done;
2550 error = NULL;
2552 if (argc == 0) {
2553 path = strdup("");
2554 if (path == NULL) {
2555 error = got_error_from_errno("strdup");
2556 goto done;
2558 } else if (argc == 1) {
2559 if (worktree) {
2560 error = got_worktree_resolve_path(&path, worktree,
2561 argv[0]);
2562 if (error)
2563 goto done;
2564 } else {
2565 path = strdup(argv[0]);
2566 if (path == NULL) {
2567 error = got_error_from_errno("strdup");
2568 goto done;
2571 } else
2572 usage_log();
2574 if (repo_path == NULL) {
2575 if (worktree)
2576 repo_path = strdup(
2577 got_worktree_get_repo_path(worktree));
2578 else
2579 repo_path = strdup(cwd);
2581 if (repo_path == NULL) {
2582 error = got_error_from_errno("strdup");
2583 goto done;
2586 init_curses();
2588 error = got_repo_open(&repo, repo_path, NULL);
2589 if (error != NULL)
2590 goto done;
2592 error = apply_unveil(got_repo_get_path(repo),
2593 worktree ? got_worktree_get_root_path(worktree) : NULL);
2594 if (error)
2595 goto done;
2597 if (start_commit == NULL)
2598 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2599 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2600 GOT_OBJ_TYPE_COMMIT, 1, repo);
2601 else
2602 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2603 GOT_OBJ_TYPE_COMMIT, 1, repo);
2604 if (error != NULL)
2605 goto done;
2607 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2608 if (error)
2609 goto done;
2611 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2612 if (view == NULL) {
2613 error = got_error_from_errno("view_open");
2614 goto done;
2616 if (worktree) {
2617 const char *prefix = got_worktree_get_path_prefix(worktree);
2618 char *p;
2619 if (asprintf(&p, "%s%s%s", prefix,
2620 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2621 error = got_error_from_errno("asprintf");
2622 goto done;
2624 free(path);
2625 path = p;
2626 check_disk = 0;
2628 head_ref_name = strdup(
2629 got_worktree_get_head_ref_name(worktree));
2630 if (head_ref_name == NULL) {
2631 error = got_error_from_errno("strdup");
2632 goto done;
2635 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2636 path, check_disk, log_branches);
2637 if (error)
2638 goto done;
2639 if (worktree) {
2640 /* Release work tree lock. */
2641 got_worktree_close(worktree);
2642 worktree = NULL;
2644 error = view_loop(view);
2645 done:
2646 free(repo_path);
2647 free(cwd);
2648 free(path);
2649 free(start_id);
2650 free(head_ref_name);
2651 if (repo)
2652 got_repo_close(repo);
2653 if (worktree)
2654 got_worktree_close(worktree);
2655 got_ref_list_free(&refs);
2656 return error;
2659 __dead static void
2660 usage_diff(void)
2662 endwin();
2663 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2664 getprogname());
2665 exit(1);
2668 static char *
2669 parse_next_line(FILE *f, size_t *len)
2671 char *line;
2672 size_t linelen;
2673 size_t lineno;
2674 const char delim[3] = { '\0', '\0', '\0'};
2676 line = fparseln(f, &linelen, &lineno, delim, 0);
2677 if (len)
2678 *len = linelen;
2679 return line;
2682 static int
2683 match_line(const char *line, regex_t *regex)
2685 regmatch_t regmatch;
2687 return regexec(regex, line, 1, &regmatch, 0) == 0;
2690 struct tog_color *
2691 match_color(struct tog_colors *colors, const char *line)
2693 struct tog_color *tc = NULL;
2695 SIMPLEQ_FOREACH(tc, colors, entry) {
2696 if (match_line(line, &tc->regex))
2697 return tc;
2700 return NULL;
2703 static const struct got_error *
2704 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2705 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2706 char *header, struct tog_colors *colors)
2708 const struct got_error *err;
2709 int lineno = 0, nprinted = 0;
2710 char *line;
2711 struct tog_color *tc;
2712 size_t len;
2713 wchar_t *wline;
2714 int width;
2716 rewind(f);
2717 werase(view->window);
2719 if (header) {
2720 err = format_line(&wline, &width, header, view->ncols, 0);
2721 if (err) {
2722 return err;
2725 if (view_needs_focus_indication(view))
2726 wstandout(view->window);
2727 waddwstr(view->window, wline);
2728 if (view_needs_focus_indication(view))
2729 wstandend(view->window);
2730 if (width <= view->ncols - 1)
2731 waddch(view->window, '\n');
2733 if (max_lines <= 1)
2734 return NULL;
2735 max_lines--;
2738 *eof = 0;
2739 while (nprinted < max_lines) {
2740 line = parse_next_line(f, &len);
2741 if (line == NULL) {
2742 *eof = 1;
2743 break;
2745 if (++lineno < *first_displayed_line) {
2746 free(line);
2747 continue;
2750 err = format_line(&wline, &width, line, view->ncols, 0);
2751 if (err) {
2752 free(line);
2753 return err;
2756 tc = match_color(colors, line);
2757 if (tc)
2758 wattr_on(view->window,
2759 COLOR_PAIR(tc->colorpair), NULL);
2760 waddwstr(view->window, wline);
2761 if (tc)
2762 wattr_off(view->window,
2763 COLOR_PAIR(tc->colorpair), NULL);
2764 if (width <= view->ncols - 1)
2765 waddch(view->window, '\n');
2766 if (++nprinted == 1)
2767 *first_displayed_line = lineno;
2768 free(line);
2769 free(wline);
2770 wline = NULL;
2772 *last_displayed_line = lineno;
2774 view_vborder(view);
2776 if (*eof) {
2777 while (nprinted < view->nlines) {
2778 waddch(view->window, '\n');
2779 nprinted++;
2782 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2783 if (err) {
2784 return err;
2787 wstandout(view->window);
2788 waddwstr(view->window, wline);
2789 wstandend(view->window);
2792 return NULL;
2795 static char *
2796 get_datestr(time_t *time, char *datebuf)
2798 struct tm mytm, *tm;
2799 char *p, *s;
2801 tm = gmtime_r(time, &mytm);
2802 if (tm == NULL)
2803 return NULL;
2804 s = asctime_r(tm, datebuf);
2805 if (s == NULL)
2806 return NULL;
2807 p = strchr(s, '\n');
2808 if (p)
2809 *p = '\0';
2810 return s;
2813 static const struct got_error *
2814 write_commit_info(struct got_object_id *commit_id,
2815 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2817 const struct got_error *err = NULL;
2818 char datebuf[26], *datestr;
2819 struct got_commit_object *commit;
2820 char *id_str = NULL, *logmsg = NULL;
2821 time_t committer_time;
2822 const char *author, *committer;
2823 char *refs_str = NULL;
2825 if (refs) {
2826 err = build_refs_str(&refs_str, refs, commit_id, repo);
2827 if (err)
2828 return err;
2831 err = got_object_open_as_commit(&commit, repo, commit_id);
2832 if (err)
2833 return err;
2835 err = got_object_id_str(&id_str, commit_id);
2836 if (err) {
2837 err = got_error_from_errno("got_object_id_str");
2838 goto done;
2841 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2842 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2843 err = got_error_from_errno("fprintf");
2844 goto done;
2846 if (fprintf(outfile, "from: %s\n",
2847 got_object_commit_get_author(commit)) < 0) {
2848 err = got_error_from_errno("fprintf");
2849 goto done;
2851 committer_time = got_object_commit_get_committer_time(commit);
2852 datestr = get_datestr(&committer_time, datebuf);
2853 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2854 err = got_error_from_errno("fprintf");
2855 goto done;
2857 author = got_object_commit_get_author(commit);
2858 committer = got_object_commit_get_committer(commit);
2859 if (strcmp(author, committer) != 0 &&
2860 fprintf(outfile, "via: %s\n", committer) < 0) {
2861 err = got_error_from_errno("fprintf");
2862 goto done;
2864 err = got_object_commit_get_logmsg(&logmsg, commit);
2865 if (err)
2866 goto done;
2867 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2868 err = got_error_from_errno("fprintf");
2869 goto done;
2871 done:
2872 free(id_str);
2873 free(logmsg);
2874 free(refs_str);
2875 got_object_commit_close(commit);
2876 return err;
2879 const struct got_error *
2880 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2881 FILE *infile)
2883 size_t len;
2884 char *buf = NULL;
2885 int i;
2886 size_t noffsets = 0;
2887 off_t off = 0;
2889 if (line_offsets)
2890 *line_offsets = NULL;
2891 if (filesize)
2892 *filesize = 0;
2893 if (nlines)
2894 *nlines = 0;
2896 if (fseek(infile, 0, SEEK_END) == -1)
2897 return got_error_from_errno("fseek");
2898 len = ftell(infile) + 1;
2899 if (ferror(infile))
2900 return got_error_from_errno("ftell");
2901 if (fseek(infile, 0, SEEK_SET) == -1)
2902 return got_error_from_errno("fseek");
2904 if (len == 0)
2905 return NULL;
2906 if ((buf = calloc(len, sizeof(char *))) == NULL)
2907 return got_error_from_errno("calloc");
2909 fread(buf, 1, len, infile);
2910 if (ferror(infile))
2911 return got_error_from_errno("fread");
2913 i = 0;
2914 if (line_offsets && nlines) {
2915 if (*line_offsets == NULL) {
2916 /* Have some data but perhaps no '\n'. */
2917 noffsets = 1;
2918 *nlines = 1;
2919 *line_offsets = calloc(1, sizeof(**line_offsets));
2920 if (*line_offsets == NULL)
2921 return got_error_from_errno("calloc");
2922 /* Skip forward over end of first line. */
2923 while (i < len) {
2924 if (buf[i] == '\n')
2925 break;
2926 i++;
2929 /* Scan '\n' offsets in remaining chunk of data. */
2930 while (i < len) {
2931 if (buf[i] != '\n') {
2932 i++;
2933 continue;
2935 (*nlines)++;
2936 if (noffsets < *nlines) {
2937 off_t *o = recallocarray(*line_offsets,
2938 noffsets, *nlines,
2939 sizeof(**line_offsets));
2940 if (o == NULL) {
2941 free(*line_offsets);
2942 *line_offsets = NULL;
2943 return got_error_from_errno(
2944 "recallocarray");
2946 *line_offsets = o;
2947 noffsets = *nlines;
2949 off = i + 1;
2950 (*line_offsets)[*nlines - 1] = off;
2951 i++;
2955 if (fflush(infile) != 0)
2956 return got_error_from_errno("fflush");
2957 rewind(infile);
2959 if (filesize)
2960 *filesize = len;
2962 return NULL;
2965 static const struct got_error *
2966 create_diff(struct tog_diff_view_state *s)
2968 const struct got_error *err = NULL;
2969 FILE *f = NULL;
2970 int obj_type;
2972 f = got_opentemp();
2973 if (f == NULL) {
2974 err = got_error_from_errno("got_opentemp");
2975 goto done;
2977 if (s->f && fclose(s->f) != 0) {
2978 err = got_error_from_errno("fclose");
2979 goto done;
2981 s->f = f;
2983 if (s->id1)
2984 err = got_object_get_type(&obj_type, s->repo, s->id1);
2985 else
2986 err = got_object_get_type(&obj_type, s->repo, s->id2);
2987 if (err)
2988 goto done;
2990 switch (obj_type) {
2991 case GOT_OBJ_TYPE_BLOB:
2992 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2993 s->diff_context, 0, s->repo, s->f);
2994 break;
2995 case GOT_OBJ_TYPE_TREE:
2996 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2997 s->diff_context, 0, s->repo, s->f);
2998 break;
2999 case GOT_OBJ_TYPE_COMMIT: {
3000 const struct got_object_id_queue *parent_ids;
3001 struct got_object_qid *pid;
3002 struct got_commit_object *commit2;
3004 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3005 if (err)
3006 goto done;
3007 /* Show commit info if we're diffing to a parent/root commit. */
3008 if (s->id1 == NULL) {
3009 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3010 if (err)
3011 goto done;
3012 } else {
3013 parent_ids = got_object_commit_get_parent_ids(commit2);
3014 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3015 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3016 err = write_commit_info(s->id2, s->refs,
3017 s->repo, s->f);
3018 if (err)
3019 goto done;
3020 break;
3024 got_object_commit_close(commit2);
3026 err = got_diff_objects_as_commits(s->id1, s->id2,
3027 s->diff_context, 0, s->repo, s->f);
3028 break;
3030 default:
3031 err = got_error(GOT_ERR_OBJ_TYPE);
3032 break;
3034 if (err)
3035 goto done;
3036 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3037 s->f);
3038 done:
3039 if (s->f && fflush(s->f) != 0 && err == NULL)
3040 err = got_error_from_errno("fflush");
3041 return err;
3044 static void
3045 diff_view_indicate_progress(struct tog_view *view)
3047 mvwaddstr(view->window, 0, 0, "diffing...");
3048 update_panels();
3049 doupdate();
3052 static const struct got_error *
3053 search_start_diff_view(struct tog_view *view)
3055 struct tog_diff_view_state *s = &view->state.diff;
3057 s->matched_line = 0;
3058 return NULL;
3061 static const struct got_error *
3062 search_next_diff_view(struct tog_view *view)
3064 struct tog_diff_view_state *s = &view->state.diff;
3065 int lineno;
3067 if (!view->searching) {
3068 view->search_next_done = 1;
3069 return NULL;
3072 if (s->matched_line) {
3073 if (view->searching == TOG_SEARCH_FORWARD)
3074 lineno = s->matched_line + 1;
3075 else
3076 lineno = s->matched_line - 1;
3077 } else {
3078 if (view->searching == TOG_SEARCH_FORWARD)
3079 lineno = 1;
3080 else
3081 lineno = s->nlines;
3084 while (1) {
3085 char *line = NULL;
3086 off_t offset;
3087 size_t len;
3089 if (lineno <= 0 || lineno > s->nlines) {
3090 if (s->matched_line == 0) {
3091 view->search_next_done = 1;
3092 free(line);
3093 break;
3096 if (view->searching == TOG_SEARCH_FORWARD)
3097 lineno = 1;
3098 else
3099 lineno = s->nlines;
3102 offset = s->line_offsets[lineno - 1];
3103 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3104 free(line);
3105 return got_error_from_errno("fseeko");
3107 free(line);
3108 line = parse_next_line(s->f, &len);
3109 if (line && match_line(line, &view->regex)) {
3110 view->search_next_done = 1;
3111 s->matched_line = lineno;
3112 free(line);
3113 break;
3115 free(line);
3116 if (view->searching == TOG_SEARCH_FORWARD)
3117 lineno++;
3118 else
3119 lineno--;
3122 if (s->matched_line) {
3123 s->first_displayed_line = s->matched_line;
3124 s->selected_line = 1;
3127 return NULL;
3130 static const struct got_error *
3131 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3132 struct got_object_id *id2, struct tog_view *log_view,
3133 struct got_reflist_head *refs, struct got_repository *repo)
3135 const struct got_error *err;
3136 struct tog_diff_view_state *s = &view->state.diff;
3138 if (id1 != NULL && id2 != NULL) {
3139 int type1, type2;
3140 err = got_object_get_type(&type1, repo, id1);
3141 if (err)
3142 return err;
3143 err = got_object_get_type(&type2, repo, id2);
3144 if (err)
3145 return err;
3147 if (type1 != type2)
3148 return got_error(GOT_ERR_OBJ_TYPE);
3150 s->first_displayed_line = 1;
3151 s->last_displayed_line = view->nlines;
3152 s->selected_line = 1;
3153 s->repo = repo;
3154 s->refs = refs;
3155 s->id1 = id1;
3156 s->id2 = id2;
3158 if (id1) {
3159 s->id1 = got_object_id_dup(id1);
3160 if (s->id1 == NULL)
3161 return got_error_from_errno("got_object_id_dup");
3162 } else
3163 s->id1 = NULL;
3165 s->id2 = got_object_id_dup(id2);
3166 if (s->id2 == NULL) {
3167 free(s->id1);
3168 s->id1 = NULL;
3169 return got_error_from_errno("got_object_id_dup");
3171 s->f = NULL;
3172 s->first_displayed_line = 1;
3173 s->last_displayed_line = view->nlines;
3174 s->diff_context = 3;
3175 s->log_view = log_view;
3176 s->repo = repo;
3177 s->refs = refs;
3179 SIMPLEQ_INIT(&s->colors);
3180 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3181 err = add_color(&s->colors,
3182 "^-", TOG_COLOR_DIFF_MINUS,
3183 get_color_value("TOG_COLOR_DIFF_MINUS"));
3184 if (err)
3185 return err;
3186 err = add_color(&s->colors, "^\\+",
3187 TOG_COLOR_DIFF_PLUS,
3188 get_color_value("TOG_COLOR_DIFF_PLUS"));
3189 if (err) {
3190 free_colors(&s->colors);
3191 return err;
3193 err = add_color(&s->colors,
3194 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3195 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3196 if (err) {
3197 free_colors(&s->colors);
3198 return err;
3201 err = add_color(&s->colors,
3202 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3203 get_color_value("TOG_COLOR_DIFF_META"));
3204 if (err) {
3205 free_colors(&s->colors);
3206 return err;
3209 err = add_color(&s->colors,
3210 "^(from|via): ", TOG_COLOR_AUTHOR,
3211 get_color_value("TOG_COLOR_AUTHOR"));
3212 if (err) {
3213 free_colors(&s->colors);
3214 return err;
3217 err = add_color(&s->colors,
3218 "^date: ", TOG_COLOR_DATE,
3219 get_color_value("TOG_COLOR_DATE"));
3220 if (err) {
3221 free_colors(&s->colors);
3222 return err;
3226 if (log_view && view_is_splitscreen(view))
3227 show_log_view(log_view); /* draw vborder */
3228 diff_view_indicate_progress(view);
3230 err = create_diff(s);
3231 if (err) {
3232 free(s->id1);
3233 s->id1 = NULL;
3234 free(s->id2);
3235 s->id2 = NULL;
3236 return err;
3239 view->show = show_diff_view;
3240 view->input = input_diff_view;
3241 view->close = close_diff_view;
3242 view->search_start = search_start_diff_view;
3243 view->search_next = search_next_diff_view;
3245 return NULL;
3248 static const struct got_error *
3249 close_diff_view(struct tog_view *view)
3251 const struct got_error *err = NULL;
3252 struct tog_diff_view_state *s = &view->state.diff;
3254 free(s->id1);
3255 s->id1 = NULL;
3256 free(s->id2);
3257 s->id2 = NULL;
3258 if (s->f && fclose(s->f) == EOF)
3259 err = got_error_from_errno("fclose");
3260 free_colors(&s->colors);
3261 free(s->line_offsets);
3262 return err;
3265 static const struct got_error *
3266 show_diff_view(struct tog_view *view)
3268 const struct got_error *err;
3269 struct tog_diff_view_state *s = &view->state.diff;
3270 char *id_str1 = NULL, *id_str2, *header;
3272 if (s->id1) {
3273 err = got_object_id_str(&id_str1, s->id1);
3274 if (err)
3275 return err;
3277 err = got_object_id_str(&id_str2, s->id2);
3278 if (err)
3279 return err;
3281 if (asprintf(&header, "diff %s %s",
3282 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3283 err = got_error_from_errno("asprintf");
3284 free(id_str1);
3285 free(id_str2);
3286 return err;
3288 free(id_str1);
3289 free(id_str2);
3291 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3292 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3293 header, &s->colors);
3296 static const struct got_error *
3297 set_selected_commit(struct tog_diff_view_state *s,
3298 struct commit_queue_entry *entry)
3300 const struct got_error *err;
3301 const struct got_object_id_queue *parent_ids;
3302 struct got_commit_object *selected_commit;
3303 struct got_object_qid *pid;
3305 free(s->id2);
3306 s->id2 = got_object_id_dup(entry->id);
3307 if (s->id2 == NULL)
3308 return got_error_from_errno("got_object_id_dup");
3310 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3311 if (err)
3312 return err;
3313 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3314 free(s->id1);
3315 pid = SIMPLEQ_FIRST(parent_ids);
3316 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3317 got_object_commit_close(selected_commit);
3318 return NULL;
3321 static const struct got_error *
3322 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3323 struct tog_view **focus_view, struct tog_view *view, int ch)
3325 const struct got_error *err = NULL;
3326 struct tog_diff_view_state *s = &view->state.diff;
3327 struct tog_log_view_state *ls;
3328 struct commit_queue_entry *entry;
3329 int i;
3331 switch (ch) {
3332 case 'k':
3333 case KEY_UP:
3334 if (s->first_displayed_line > 1)
3335 s->first_displayed_line--;
3336 break;
3337 case KEY_PPAGE:
3338 case CTRL('b'):
3339 if (s->first_displayed_line == 1)
3340 break;
3341 i = 0;
3342 while (i++ < view->nlines - 1 &&
3343 s->first_displayed_line > 1)
3344 s->first_displayed_line--;
3345 break;
3346 case 'j':
3347 case KEY_DOWN:
3348 if (!s->eof)
3349 s->first_displayed_line++;
3350 break;
3351 case KEY_NPAGE:
3352 case CTRL('f'):
3353 case ' ':
3354 if (s->eof)
3355 break;
3356 i = 0;
3357 while (!s->eof && i++ < view->nlines - 1) {
3358 char *line;
3359 line = parse_next_line(s->f, NULL);
3360 s->first_displayed_line++;
3361 if (line == NULL)
3362 break;
3364 break;
3365 case '[':
3366 if (s->diff_context > 0) {
3367 s->diff_context--;
3368 diff_view_indicate_progress(view);
3369 err = create_diff(s);
3371 break;
3372 case ']':
3373 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3374 s->diff_context++;
3375 diff_view_indicate_progress(view);
3376 err = create_diff(s);
3378 break;
3379 case '<':
3380 case ',':
3381 if (s->log_view == NULL)
3382 break;
3383 ls = &s->log_view->state.log;
3384 entry = TAILQ_PREV(ls->selected_entry,
3385 commit_queue_head, entry);
3386 if (entry == NULL)
3387 break;
3389 err = input_log_view(NULL, NULL, NULL, s->log_view,
3390 KEY_UP);
3391 if (err)
3392 break;
3394 err = set_selected_commit(s, entry);
3395 if (err)
3396 break;
3398 s->first_displayed_line = 1;
3399 s->last_displayed_line = view->nlines;
3401 diff_view_indicate_progress(view);
3402 err = create_diff(s);
3403 break;
3404 case '>':
3405 case '.':
3406 if (s->log_view == NULL)
3407 break;
3408 ls = &s->log_view->state.log;
3410 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3411 ls->thread_args.commits_needed++;
3413 /* Display "loading..." in log view. */
3414 show_log_view(s->log_view);
3415 update_panels();
3416 doupdate();
3418 err = trigger_log_thread(1 /* load_all */,
3419 &ls->thread_args.commits_needed,
3420 &ls->thread_args.log_complete,
3421 &ls->thread_args.need_commits);
3422 if (err)
3423 break;
3425 err = input_log_view(NULL, NULL, NULL, s->log_view,
3426 KEY_DOWN);
3427 if (err)
3428 break;
3430 entry = TAILQ_NEXT(ls->selected_entry, entry);
3431 if (entry == NULL)
3432 break;
3434 err = set_selected_commit(s, entry);
3435 if (err)
3436 break;
3438 s->first_displayed_line = 1;
3439 s->last_displayed_line = view->nlines;
3441 diff_view_indicate_progress(view);
3442 err = create_diff(s);
3443 break;
3444 default:
3445 break;
3448 return err;
3451 static const struct got_error *
3452 cmd_diff(int argc, char *argv[])
3454 const struct got_error *error = NULL;
3455 struct got_repository *repo = NULL;
3456 struct got_reflist_head refs;
3457 struct got_object_id *id1 = NULL, *id2 = NULL;
3458 char *repo_path = NULL;
3459 char *id_str1 = NULL, *id_str2 = NULL;
3460 int ch;
3461 struct tog_view *view;
3463 SIMPLEQ_INIT(&refs);
3465 #ifndef PROFILE
3466 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3467 NULL) == -1)
3468 err(1, "pledge");
3469 #endif
3471 while ((ch = getopt(argc, argv, "")) != -1) {
3472 switch (ch) {
3473 default:
3474 usage_diff();
3475 /* NOTREACHED */
3479 argc -= optind;
3480 argv += optind;
3482 if (argc == 0) {
3483 usage_diff(); /* TODO show local worktree changes */
3484 } else if (argc == 2) {
3485 repo_path = getcwd(NULL, 0);
3486 if (repo_path == NULL)
3487 return got_error_from_errno("getcwd");
3488 id_str1 = argv[0];
3489 id_str2 = argv[1];
3490 } else if (argc == 3) {
3491 repo_path = realpath(argv[0], NULL);
3492 if (repo_path == NULL)
3493 return got_error_from_errno2("realpath", argv[0]);
3494 id_str1 = argv[1];
3495 id_str2 = argv[2];
3496 } else
3497 usage_diff();
3499 init_curses();
3501 error = got_repo_open(&repo, repo_path, NULL);
3502 if (error)
3503 goto done;
3505 error = apply_unveil(got_repo_get_path(repo), NULL);
3506 if (error)
3507 goto done;
3509 error = got_repo_match_object_id_prefix(&id1, id_str1,
3510 GOT_OBJ_TYPE_ANY, repo);
3511 if (error)
3512 goto done;
3514 error = got_repo_match_object_id_prefix(&id2, id_str2,
3515 GOT_OBJ_TYPE_ANY, repo);
3516 if (error)
3517 goto done;
3519 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3520 if (error)
3521 goto done;
3523 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3524 if (view == NULL) {
3525 error = got_error_from_errno("view_open");
3526 goto done;
3528 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3529 if (error)
3530 goto done;
3531 error = view_loop(view);
3532 done:
3533 free(repo_path);
3534 if (repo)
3535 got_repo_close(repo);
3536 got_ref_list_free(&refs);
3537 return error;
3540 __dead static void
3541 usage_blame(void)
3543 endwin();
3544 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3545 getprogname());
3546 exit(1);
3549 struct tog_blame_line {
3550 int annotated;
3551 struct got_object_id *id;
3554 static const struct got_error *
3555 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3556 const char *path, struct tog_blame_line *lines, int nlines,
3557 int blame_complete, int selected_line, int *first_displayed_line,
3558 int *last_displayed_line, int *eof, int max_lines,
3559 struct tog_colors *colors)
3561 const struct got_error *err;
3562 int lineno = 0, nprinted = 0;
3563 char *line;
3564 size_t len;
3565 wchar_t *wline;
3566 int width;
3567 struct tog_blame_line *blame_line;
3568 struct got_object_id *prev_id = NULL;
3569 char *id_str;
3570 struct tog_color *tc;
3572 err = got_object_id_str(&id_str, id);
3573 if (err)
3574 return err;
3576 rewind(f);
3577 werase(view->window);
3579 if (asprintf(&line, "commit %s", id_str) == -1) {
3580 err = got_error_from_errno("asprintf");
3581 free(id_str);
3582 return err;
3585 err = format_line(&wline, &width, line, view->ncols, 0);
3586 free(line);
3587 line = NULL;
3588 if (err)
3589 return err;
3590 if (view_needs_focus_indication(view))
3591 wstandout(view->window);
3592 tc = get_color(colors, TOG_COLOR_COMMIT);
3593 if (tc)
3594 wattr_on(view->window,
3595 COLOR_PAIR(tc->colorpair), NULL);
3596 waddwstr(view->window, wline);
3597 if (tc)
3598 wattr_off(view->window,
3599 COLOR_PAIR(tc->colorpair), NULL);
3600 if (view_needs_focus_indication(view))
3601 wstandend(view->window);
3602 free(wline);
3603 wline = NULL;
3604 if (width < view->ncols - 1)
3605 waddch(view->window, '\n');
3607 if (asprintf(&line, "[%d/%d] %s%s",
3608 *first_displayed_line - 1 + selected_line, nlines,
3609 blame_complete ? "" : "annotating... ", path) == -1) {
3610 free(id_str);
3611 return got_error_from_errno("asprintf");
3613 free(id_str);
3614 err = format_line(&wline, &width, line, view->ncols, 0);
3615 free(line);
3616 line = NULL;
3617 if (err)
3618 return err;
3619 waddwstr(view->window, wline);
3620 free(wline);
3621 wline = NULL;
3622 if (width < view->ncols - 1)
3623 waddch(view->window, '\n');
3625 *eof = 0;
3626 while (nprinted < max_lines - 2) {
3627 line = parse_next_line(f, &len);
3628 if (line == NULL) {
3629 *eof = 1;
3630 break;
3632 if (++lineno < *first_displayed_line) {
3633 free(line);
3634 continue;
3637 if (view->ncols <= 9) {
3638 width = 9;
3639 wline = wcsdup(L"");
3640 if (wline == NULL)
3641 err = got_error_from_errno("wcsdup");
3642 } else {
3643 err = format_line(&wline, &width, line,
3644 view->ncols - 9, 9);
3645 width += 9;
3647 if (err) {
3648 free(line);
3649 return err;
3652 if (view->focussed && nprinted == selected_line - 1)
3653 wstandout(view->window);
3655 if (nlines > 0) {
3656 blame_line = &lines[lineno - 1];
3657 if (blame_line->annotated && prev_id &&
3658 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3659 !(view->focussed &&
3660 nprinted == selected_line - 1)) {
3661 waddstr(view->window, " ");
3662 } else if (blame_line->annotated) {
3663 char *id_str;
3664 err = got_object_id_str(&id_str, blame_line->id);
3665 if (err) {
3666 free(line);
3667 free(wline);
3668 return err;
3670 tc = get_color(colors, TOG_COLOR_COMMIT);
3671 if (tc)
3672 wattr_on(view->window,
3673 COLOR_PAIR(tc->colorpair), NULL);
3674 wprintw(view->window, "%.8s", id_str);
3675 if (tc)
3676 wattr_off(view->window,
3677 COLOR_PAIR(tc->colorpair), NULL);
3678 free(id_str);
3679 prev_id = blame_line->id;
3680 } else {
3681 waddstr(view->window, "........");
3682 prev_id = NULL;
3684 } else {
3685 waddstr(view->window, "........");
3686 prev_id = NULL;
3689 if (view->focussed && nprinted == selected_line - 1)
3690 wstandend(view->window);
3691 waddstr(view->window, " ");
3693 waddwstr(view->window, wline);
3694 if (width <= view->ncols - 1)
3695 waddch(view->window, '\n');
3696 if (++nprinted == 1)
3697 *first_displayed_line = lineno;
3698 free(line);
3699 free(wline);
3700 wline = NULL;
3702 *last_displayed_line = lineno;
3704 view_vborder(view);
3706 return NULL;
3709 static const struct got_error *
3710 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3712 const struct got_error *err = NULL;
3713 struct tog_blame_cb_args *a = arg;
3714 struct tog_blame_line *line;
3715 int errcode;
3717 if (nlines != a->nlines ||
3718 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3719 return got_error(GOT_ERR_RANGE);
3721 errcode = pthread_mutex_lock(&tog_mutex);
3722 if (errcode)
3723 return got_error_set_errno(errcode, "pthread_mutex_lock");
3725 if (*a->quit) { /* user has quit the blame view */
3726 err = got_error(GOT_ERR_ITER_COMPLETED);
3727 goto done;
3730 if (lineno == -1)
3731 goto done; /* no change in this commit */
3733 line = &a->lines[lineno - 1];
3734 if (line->annotated)
3735 goto done;
3737 line->id = got_object_id_dup(id);
3738 if (line->id == NULL) {
3739 err = got_error_from_errno("got_object_id_dup");
3740 goto done;
3742 line->annotated = 1;
3743 done:
3744 errcode = pthread_mutex_unlock(&tog_mutex);
3745 if (errcode)
3746 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3747 return err;
3750 static void *
3751 blame_thread(void *arg)
3753 const struct got_error *err;
3754 struct tog_blame_thread_args *ta = arg;
3755 struct tog_blame_cb_args *a = ta->cb_args;
3756 int errcode;
3758 err = block_signals_used_by_main_thread();
3759 if (err)
3760 return (void *)err;
3762 err = got_blame(ta->path, a->commit_id, ta->repo,
3763 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3764 if (err && err->code == GOT_ERR_CANCELLED)
3765 err = NULL;
3767 errcode = pthread_mutex_lock(&tog_mutex);
3768 if (errcode)
3769 return (void *)got_error_set_errno(errcode,
3770 "pthread_mutex_lock");
3772 got_repo_close(ta->repo);
3773 ta->repo = NULL;
3774 *ta->complete = 1;
3776 errcode = pthread_mutex_unlock(&tog_mutex);
3777 if (errcode && err == NULL)
3778 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3780 return (void *)err;
3783 static struct got_object_id *
3784 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3785 int first_displayed_line, int selected_line)
3787 struct tog_blame_line *line;
3789 if (nlines <= 0)
3790 return NULL;
3792 line = &lines[first_displayed_line - 1 + selected_line - 1];
3793 if (!line->annotated)
3794 return NULL;
3796 return line->id;
3799 static const struct got_error *
3800 stop_blame(struct tog_blame *blame)
3802 const struct got_error *err = NULL;
3803 int i;
3805 if (blame->thread) {
3806 int errcode;
3807 errcode = pthread_mutex_unlock(&tog_mutex);
3808 if (errcode)
3809 return got_error_set_errno(errcode,
3810 "pthread_mutex_unlock");
3811 errcode = pthread_join(blame->thread, (void **)&err);
3812 if (errcode)
3813 return got_error_set_errno(errcode, "pthread_join");
3814 errcode = pthread_mutex_lock(&tog_mutex);
3815 if (errcode)
3816 return got_error_set_errno(errcode,
3817 "pthread_mutex_lock");
3818 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3819 err = NULL;
3820 blame->thread = NULL;
3822 if (blame->thread_args.repo) {
3823 got_repo_close(blame->thread_args.repo);
3824 blame->thread_args.repo = NULL;
3826 if (blame->f) {
3827 if (fclose(blame->f) != 0 && err == NULL)
3828 err = got_error_from_errno("fclose");
3829 blame->f = NULL;
3831 if (blame->lines) {
3832 for (i = 0; i < blame->nlines; i++)
3833 free(blame->lines[i].id);
3834 free(blame->lines);
3835 blame->lines = NULL;
3837 free(blame->cb_args.commit_id);
3838 blame->cb_args.commit_id = NULL;
3840 return err;
3843 static const struct got_error *
3844 cancel_blame_view(void *arg)
3846 const struct got_error *err = NULL;
3847 int *done = arg;
3848 int errcode;
3850 errcode = pthread_mutex_lock(&tog_mutex);
3851 if (errcode)
3852 return got_error_set_errno(errcode,
3853 "pthread_mutex_unlock");
3855 if (*done)
3856 err = got_error(GOT_ERR_CANCELLED);
3858 errcode = pthread_mutex_unlock(&tog_mutex);
3859 if (errcode)
3860 return got_error_set_errno(errcode,
3861 "pthread_mutex_lock");
3863 return err;
3866 static const struct got_error *
3867 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3868 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3869 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3870 struct got_repository *repo)
3872 const struct got_error *err = NULL;
3873 struct got_blob_object *blob = NULL;
3874 struct got_repository *thread_repo = NULL;
3875 struct got_object_id *obj_id = NULL;
3876 int obj_type;
3878 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3879 if (err)
3880 return err;
3882 err = got_object_get_type(&obj_type, repo, obj_id);
3883 if (err)
3884 goto done;
3886 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3887 err = got_error(GOT_ERR_OBJ_TYPE);
3888 goto done;
3891 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3892 if (err)
3893 goto done;
3894 blame->f = got_opentemp();
3895 if (blame->f == NULL) {
3896 err = got_error_from_errno("got_opentemp");
3897 goto done;
3899 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3900 &blame->line_offsets, blame->f, blob);
3901 if (err || blame->nlines == 0)
3902 goto done;
3904 /* Don't include \n at EOF in the blame line count. */
3905 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3906 blame->nlines--;
3908 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3909 if (blame->lines == NULL) {
3910 err = got_error_from_errno("calloc");
3911 goto done;
3914 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3915 if (err)
3916 goto done;
3918 blame->cb_args.view = view;
3919 blame->cb_args.lines = blame->lines;
3920 blame->cb_args.nlines = blame->nlines;
3921 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3922 if (blame->cb_args.commit_id == NULL) {
3923 err = got_error_from_errno("got_object_id_dup");
3924 goto done;
3926 blame->cb_args.quit = done;
3928 blame->thread_args.path = path;
3929 blame->thread_args.repo = thread_repo;
3930 blame->thread_args.cb_args = &blame->cb_args;
3931 blame->thread_args.complete = blame_complete;
3932 blame->thread_args.cancel_cb = cancel_blame_view;
3933 blame->thread_args.cancel_arg = done;
3934 *blame_complete = 0;
3936 done:
3937 if (blob)
3938 got_object_blob_close(blob);
3939 free(obj_id);
3940 if (err)
3941 stop_blame(blame);
3942 return err;
3945 static const struct got_error *
3946 open_blame_view(struct tog_view *view, char *path,
3947 struct got_object_id *commit_id, struct got_reflist_head *refs,
3948 struct got_repository *repo)
3950 const struct got_error *err = NULL;
3951 struct tog_blame_view_state *s = &view->state.blame;
3953 SIMPLEQ_INIT(&s->blamed_commits);
3955 s->path = strdup(path);
3956 if (s->path == NULL)
3957 return got_error_from_errno("strdup");
3959 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3960 if (err) {
3961 free(s->path);
3962 return err;
3965 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3966 s->first_displayed_line = 1;
3967 s->last_displayed_line = view->nlines;
3968 s->selected_line = 1;
3969 s->blame_complete = 0;
3970 s->repo = repo;
3971 s->refs = refs;
3972 s->commit_id = commit_id;
3973 memset(&s->blame, 0, sizeof(s->blame));
3975 SIMPLEQ_INIT(&s->colors);
3976 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3977 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3978 get_color_value("TOG_COLOR_COMMIT"));
3979 if (err)
3980 return err;
3983 view->show = show_blame_view;
3984 view->input = input_blame_view;
3985 view->close = close_blame_view;
3986 view->search_start = search_start_blame_view;
3987 view->search_next = search_next_blame_view;
3989 return run_blame(&s->blame, view, &s->blame_complete,
3990 &s->first_displayed_line, &s->last_displayed_line,
3991 &s->selected_line, &s->done, &s->eof, s->path,
3992 s->blamed_commit->id, s->repo);
3995 static const struct got_error *
3996 close_blame_view(struct tog_view *view)
3998 const struct got_error *err = NULL;
3999 struct tog_blame_view_state *s = &view->state.blame;
4001 if (s->blame.thread)
4002 err = stop_blame(&s->blame);
4004 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4005 struct got_object_qid *blamed_commit;
4006 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4007 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4008 got_object_qid_free(blamed_commit);
4011 free(s->path);
4012 free_colors(&s->colors);
4014 return err;
4017 static const struct got_error *
4018 search_start_blame_view(struct tog_view *view)
4020 struct tog_blame_view_state *s = &view->state.blame;
4022 s->matched_line = 0;
4023 return NULL;
4026 static const struct got_error *
4027 search_next_blame_view(struct tog_view *view)
4029 struct tog_blame_view_state *s = &view->state.blame;
4030 int lineno;
4032 if (!view->searching) {
4033 view->search_next_done = 1;
4034 return NULL;
4037 if (s->matched_line) {
4038 if (view->searching == TOG_SEARCH_FORWARD)
4039 lineno = s->matched_line + 1;
4040 else
4041 lineno = s->matched_line - 1;
4042 } else {
4043 if (view->searching == TOG_SEARCH_FORWARD)
4044 lineno = 1;
4045 else
4046 lineno = s->blame.nlines;
4049 while (1) {
4050 char *line = NULL;
4051 off_t offset;
4052 size_t len;
4054 if (lineno <= 0 || lineno > s->blame.nlines) {
4055 if (s->matched_line == 0) {
4056 view->search_next_done = 1;
4057 free(line);
4058 break;
4061 if (view->searching == TOG_SEARCH_FORWARD)
4062 lineno = 1;
4063 else
4064 lineno = s->blame.nlines;
4067 offset = s->blame.line_offsets[lineno - 1];
4068 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4069 free(line);
4070 return got_error_from_errno("fseeko");
4072 free(line);
4073 line = parse_next_line(s->blame.f, &len);
4074 if (line && match_line(line, &view->regex)) {
4075 view->search_next_done = 1;
4076 s->matched_line = lineno;
4077 free(line);
4078 break;
4080 free(line);
4081 if (view->searching == TOG_SEARCH_FORWARD)
4082 lineno++;
4083 else
4084 lineno--;
4087 if (s->matched_line) {
4088 s->first_displayed_line = s->matched_line;
4089 s->selected_line = 1;
4092 return NULL;
4095 static const struct got_error *
4096 show_blame_view(struct tog_view *view)
4098 const struct got_error *err = NULL;
4099 struct tog_blame_view_state *s = &view->state.blame;
4100 int errcode;
4102 if (s->blame.thread == NULL) {
4103 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4104 &s->blame.thread_args);
4105 if (errcode)
4106 return got_error_set_errno(errcode, "pthread_create");
4108 halfdelay(1); /* fast refresh while annotating */
4111 if (s->blame_complete)
4112 halfdelay(10); /* disable fast refresh */
4114 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4115 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4116 s->selected_line, &s->first_displayed_line,
4117 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4119 view_vborder(view);
4120 return err;
4123 static const struct got_error *
4124 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4125 struct tog_view **focus_view, struct tog_view *view, int ch)
4127 const struct got_error *err = NULL, *thread_err = NULL;
4128 struct tog_view *diff_view;
4129 struct tog_blame_view_state *s = &view->state.blame;
4130 int begin_x = 0;
4132 switch (ch) {
4133 case 'q':
4134 s->done = 1;
4135 break;
4136 case 'k':
4137 case KEY_UP:
4138 if (s->selected_line > 1)
4139 s->selected_line--;
4140 else if (s->selected_line == 1 &&
4141 s->first_displayed_line > 1)
4142 s->first_displayed_line--;
4143 break;
4144 case KEY_PPAGE:
4145 if (s->first_displayed_line == 1) {
4146 s->selected_line = 1;
4147 break;
4149 if (s->first_displayed_line > view->nlines - 2)
4150 s->first_displayed_line -=
4151 (view->nlines - 2);
4152 else
4153 s->first_displayed_line = 1;
4154 break;
4155 case 'j':
4156 case KEY_DOWN:
4157 if (s->selected_line < view->nlines - 2 &&
4158 s->first_displayed_line +
4159 s->selected_line <= s->blame.nlines)
4160 s->selected_line++;
4161 else if (s->last_displayed_line <
4162 s->blame.nlines)
4163 s->first_displayed_line++;
4164 break;
4165 case 'b':
4166 case 'p': {
4167 struct got_object_id *id = NULL;
4168 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4169 s->first_displayed_line, s->selected_line);
4170 if (id == NULL)
4171 break;
4172 if (ch == 'p') {
4173 struct got_commit_object *commit;
4174 struct got_object_qid *pid;
4175 struct got_object_id *blob_id = NULL;
4176 int obj_type;
4177 err = got_object_open_as_commit(&commit,
4178 s->repo, id);
4179 if (err)
4180 break;
4181 pid = SIMPLEQ_FIRST(
4182 got_object_commit_get_parent_ids(commit));
4183 if (pid == NULL) {
4184 got_object_commit_close(commit);
4185 break;
4187 /* Check if path history ends here. */
4188 err = got_object_id_by_path(&blob_id, s->repo,
4189 pid->id, s->path);
4190 if (err) {
4191 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4192 err = NULL;
4193 got_object_commit_close(commit);
4194 break;
4196 err = got_object_get_type(&obj_type, s->repo,
4197 blob_id);
4198 free(blob_id);
4199 /* Can't blame non-blob type objects. */
4200 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4201 got_object_commit_close(commit);
4202 break;
4204 err = got_object_qid_alloc(&s->blamed_commit,
4205 pid->id);
4206 got_object_commit_close(commit);
4207 } else {
4208 if (got_object_id_cmp(id,
4209 s->blamed_commit->id) == 0)
4210 break;
4211 err = got_object_qid_alloc(&s->blamed_commit,
4212 id);
4214 if (err)
4215 break;
4216 s->done = 1;
4217 thread_err = stop_blame(&s->blame);
4218 s->done = 0;
4219 if (thread_err)
4220 break;
4221 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4222 s->blamed_commit, entry);
4223 err = run_blame(&s->blame, view, &s->blame_complete,
4224 &s->first_displayed_line, &s->last_displayed_line,
4225 &s->selected_line, &s->done, &s->eof,
4226 s->path, s->blamed_commit->id, s->repo);
4227 if (err)
4228 break;
4229 break;
4231 case 'B': {
4232 struct got_object_qid *first;
4233 first = SIMPLEQ_FIRST(&s->blamed_commits);
4234 if (!got_object_id_cmp(first->id, s->commit_id))
4235 break;
4236 s->done = 1;
4237 thread_err = stop_blame(&s->blame);
4238 s->done = 0;
4239 if (thread_err)
4240 break;
4241 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4242 got_object_qid_free(s->blamed_commit);
4243 s->blamed_commit =
4244 SIMPLEQ_FIRST(&s->blamed_commits);
4245 err = run_blame(&s->blame, view, &s->blame_complete,
4246 &s->first_displayed_line, &s->last_displayed_line,
4247 &s->selected_line, &s->done, &s->eof, s->path,
4248 s->blamed_commit->id, s->repo);
4249 if (err)
4250 break;
4251 break;
4253 case KEY_ENTER:
4254 case '\r': {
4255 struct got_object_id *id = NULL;
4256 struct got_object_qid *pid;
4257 struct got_commit_object *commit = NULL;
4258 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4259 s->first_displayed_line, s->selected_line);
4260 if (id == NULL)
4261 break;
4262 err = got_object_open_as_commit(&commit, s->repo, id);
4263 if (err)
4264 break;
4265 pid = SIMPLEQ_FIRST(
4266 got_object_commit_get_parent_ids(commit));
4267 if (view_is_parent_view(view))
4268 begin_x = view_split_begin_x(view->begin_x);
4269 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4270 if (diff_view == NULL) {
4271 got_object_commit_close(commit);
4272 err = got_error_from_errno("view_open");
4273 break;
4275 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4276 id, NULL, s->refs, s->repo);
4277 got_object_commit_close(commit);
4278 if (err) {
4279 view_close(diff_view);
4280 break;
4282 if (view_is_parent_view(view)) {
4283 err = view_close_child(view);
4284 if (err)
4285 break;
4286 err = view_set_child(view, diff_view);
4287 if (err) {
4288 view_close(diff_view);
4289 break;
4291 *focus_view = diff_view;
4292 view->child_focussed = 1;
4293 } else
4294 *new_view = diff_view;
4295 if (err)
4296 break;
4297 break;
4299 case KEY_NPAGE:
4300 case ' ':
4301 if (s->last_displayed_line >= s->blame.nlines &&
4302 s->selected_line >= MIN(s->blame.nlines,
4303 view->nlines - 2)) {
4304 break;
4306 if (s->last_displayed_line >= s->blame.nlines &&
4307 s->selected_line < view->nlines - 2) {
4308 s->selected_line = MIN(s->blame.nlines,
4309 view->nlines - 2);
4310 break;
4312 if (s->last_displayed_line + view->nlines - 2
4313 <= s->blame.nlines)
4314 s->first_displayed_line +=
4315 view->nlines - 2;
4316 else
4317 s->first_displayed_line =
4318 s->blame.nlines -
4319 (view->nlines - 3);
4320 break;
4321 case KEY_RESIZE:
4322 if (s->selected_line > view->nlines - 2) {
4323 s->selected_line = MIN(s->blame.nlines,
4324 view->nlines - 2);
4326 break;
4327 default:
4328 break;
4330 return thread_err ? thread_err : err;
4333 static const struct got_error *
4334 cmd_blame(int argc, char *argv[])
4336 const struct got_error *error;
4337 struct got_repository *repo = NULL;
4338 struct got_reflist_head refs;
4339 struct got_worktree *worktree = NULL;
4340 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4341 struct got_object_id *commit_id = NULL;
4342 char *commit_id_str = NULL;
4343 int ch;
4344 struct tog_view *view;
4346 SIMPLEQ_INIT(&refs);
4348 #ifndef PROFILE
4349 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4350 NULL) == -1)
4351 err(1, "pledge");
4352 #endif
4354 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4355 switch (ch) {
4356 case 'c':
4357 commit_id_str = optarg;
4358 break;
4359 case 'r':
4360 repo_path = realpath(optarg, NULL);
4361 if (repo_path == NULL)
4362 return got_error_from_errno2("realpath",
4363 optarg);
4364 break;
4365 default:
4366 usage_blame();
4367 /* NOTREACHED */
4371 argc -= optind;
4372 argv += optind;
4374 if (argc == 1)
4375 path = argv[0];
4376 else
4377 usage_blame();
4379 cwd = getcwd(NULL, 0);
4380 if (cwd == NULL) {
4381 error = got_error_from_errno("getcwd");
4382 goto done;
4384 if (repo_path == NULL) {
4385 error = got_worktree_open(&worktree, cwd);
4386 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4387 goto done;
4388 else
4389 error = NULL;
4390 if (worktree) {
4391 repo_path =
4392 strdup(got_worktree_get_repo_path(worktree));
4393 if (repo_path == NULL)
4394 error = got_error_from_errno("strdup");
4395 if (error)
4396 goto done;
4397 } else {
4398 repo_path = strdup(cwd);
4399 if (repo_path == NULL) {
4400 error = got_error_from_errno("strdup");
4401 goto done;
4406 init_curses();
4408 error = got_repo_open(&repo, repo_path, NULL);
4409 if (error != NULL)
4410 goto done;
4412 error = apply_unveil(got_repo_get_path(repo), NULL);
4413 if (error)
4414 goto done;
4416 if (worktree) {
4417 const char *prefix = got_worktree_get_path_prefix(worktree);
4418 char *p, *worktree_subdir = cwd +
4419 strlen(got_worktree_get_root_path(worktree));
4420 if (asprintf(&p, "%s%s%s%s%s",
4421 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4422 worktree_subdir, worktree_subdir[0] ? "/" : "",
4423 path) == -1) {
4424 error = got_error_from_errno("asprintf");
4425 goto done;
4427 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4428 free(p);
4429 } else {
4430 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4432 if (error)
4433 goto done;
4435 if (commit_id_str == NULL) {
4436 struct got_reference *head_ref;
4437 error = got_ref_open(&head_ref, repo, worktree ?
4438 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4439 if (error != NULL)
4440 goto done;
4441 error = got_ref_resolve(&commit_id, repo, head_ref);
4442 got_ref_close(head_ref);
4443 } else {
4444 error = got_repo_match_object_id(&commit_id, NULL,
4445 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4447 if (error != NULL)
4448 goto done;
4450 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4451 if (error)
4452 goto done;
4454 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4455 if (view == NULL) {
4456 error = got_error_from_errno("view_open");
4457 goto done;
4459 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4460 if (error)
4461 goto done;
4462 if (worktree) {
4463 /* Release work tree lock. */
4464 got_worktree_close(worktree);
4465 worktree = NULL;
4467 error = view_loop(view);
4468 done:
4469 free(repo_path);
4470 free(cwd);
4471 free(commit_id);
4472 if (worktree)
4473 got_worktree_close(worktree);
4474 if (repo)
4475 got_repo_close(repo);
4476 got_ref_list_free(&refs);
4477 return error;
4480 static const struct got_error *
4481 draw_tree_entries(struct tog_view *view,
4482 struct got_tree_entry **first_displayed_entry,
4483 struct got_tree_entry **last_displayed_entry,
4484 struct got_tree_entry **selected_entry, int *ndisplayed,
4485 const char *label, int show_ids, const char *parent_path,
4486 struct got_tree_object *tree, int selected, int limit,
4487 int isroot, struct tog_colors *colors)
4489 const struct got_error *err = NULL;
4490 struct got_tree_entry *te;
4491 wchar_t *wline;
4492 struct tog_color *tc;
4493 int width, n, i, nentries;
4495 *ndisplayed = 0;
4497 werase(view->window);
4499 if (limit == 0)
4500 return NULL;
4502 err = format_line(&wline, &width, label, view->ncols, 0);
4503 if (err)
4504 return err;
4505 if (view_needs_focus_indication(view))
4506 wstandout(view->window);
4507 tc = get_color(colors, TOG_COLOR_COMMIT);
4508 if (tc)
4509 wattr_on(view->window,
4510 COLOR_PAIR(tc->colorpair), NULL);
4511 waddwstr(view->window, wline);
4512 if (tc)
4513 wattr_off(view->window,
4514 COLOR_PAIR(tc->colorpair), NULL);
4515 if (view_needs_focus_indication(view))
4516 wstandend(view->window);
4517 free(wline);
4518 wline = NULL;
4519 if (width < view->ncols - 1)
4520 waddch(view->window, '\n');
4521 if (--limit <= 0)
4522 return NULL;
4523 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4524 if (err)
4525 return err;
4526 waddwstr(view->window, wline);
4527 free(wline);
4528 wline = NULL;
4529 if (width < view->ncols - 1)
4530 waddch(view->window, '\n');
4531 if (--limit <= 0)
4532 return NULL;
4533 waddch(view->window, '\n');
4534 if (--limit <= 0)
4535 return NULL;
4537 if (*first_displayed_entry == NULL) {
4538 te = got_object_tree_get_first_entry(tree);
4539 if (selected == 0) {
4540 if (view->focussed)
4541 wstandout(view->window);
4542 *selected_entry = NULL;
4544 waddstr(view->window, " ..\n"); /* parent directory */
4545 if (selected == 0 && view->focussed)
4546 wstandend(view->window);
4547 (*ndisplayed)++;
4548 if (--limit <= 0)
4549 return NULL;
4550 n = 1;
4551 } else {
4552 n = 0;
4553 te = *first_displayed_entry;
4556 nentries = got_object_tree_get_nentries(tree);
4557 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4558 char *line = NULL, *id_str = NULL;
4559 const char *modestr = "";
4560 mode_t mode;
4562 te = got_object_tree_get_entry(tree, i);
4563 mode = got_tree_entry_get_mode(te);
4565 if (show_ids) {
4566 err = got_object_id_str(&id_str,
4567 got_tree_entry_get_id(te));
4568 if (err)
4569 return got_error_from_errno(
4570 "got_object_id_str");
4572 if (got_object_tree_entry_is_submodule(te))
4573 modestr = "$";
4574 else if (S_ISLNK(mode))
4575 modestr = "@";
4576 else if (S_ISDIR(mode))
4577 modestr = "/";
4578 else if (mode & S_IXUSR)
4579 modestr = "*";
4580 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4581 got_tree_entry_get_name(te), modestr) == -1) {
4582 free(id_str);
4583 return got_error_from_errno("asprintf");
4585 free(id_str);
4586 err = format_line(&wline, &width, line, view->ncols, 0);
4587 if (err) {
4588 free(line);
4589 break;
4591 if (n == selected) {
4592 if (view->focussed)
4593 wstandout(view->window);
4594 *selected_entry = te;
4596 tc = match_color(colors, line);
4597 if (tc)
4598 wattr_on(view->window,
4599 COLOR_PAIR(tc->colorpair), NULL);
4600 waddwstr(view->window, wline);
4601 if (tc)
4602 wattr_off(view->window,
4603 COLOR_PAIR(tc->colorpair), NULL);
4604 if (width < view->ncols - 1)
4605 waddch(view->window, '\n');
4606 if (n == selected && view->focussed)
4607 wstandend(view->window);
4608 free(line);
4609 free(wline);
4610 wline = NULL;
4611 n++;
4612 (*ndisplayed)++;
4613 *last_displayed_entry = te;
4614 if (--limit <= 0)
4615 break;
4618 return err;
4621 static void
4622 tree_scroll_up(struct tog_view *view,
4623 struct got_tree_entry **first_displayed_entry, int maxscroll,
4624 struct got_tree_object *tree, int isroot)
4626 struct got_tree_entry *te;
4627 int i;
4629 if (*first_displayed_entry == NULL)
4630 return;
4632 te = got_object_tree_get_entry(tree, 0);
4633 if (*first_displayed_entry == te) {
4634 if (!isroot)
4635 *first_displayed_entry = NULL;
4636 return;
4639 i = 0;
4640 while (*first_displayed_entry && i < maxscroll) {
4641 *first_displayed_entry = got_tree_entry_get_prev(tree,
4642 *first_displayed_entry);
4643 i++;
4645 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4646 *first_displayed_entry = NULL;
4649 static int
4650 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4651 struct got_tree_entry *last_displayed_entry,
4652 struct got_tree_object *tree)
4654 struct got_tree_entry *next, *last;
4655 int n = 0;
4657 if (*first_displayed_entry)
4658 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4659 else
4660 next = got_object_tree_get_first_entry(tree);
4662 last = last_displayed_entry;
4663 while (next && last && n++ < maxscroll) {
4664 last = got_tree_entry_get_next(tree, last);
4665 if (last) {
4666 *first_displayed_entry = next;
4667 next = got_tree_entry_get_next(tree, next);
4670 return n;
4673 static const struct got_error *
4674 tree_entry_path(char **path, struct tog_parent_trees *parents,
4675 struct got_tree_entry *te)
4677 const struct got_error *err = NULL;
4678 struct tog_parent_tree *pt;
4679 size_t len = 2; /* for leading slash and NUL */
4681 TAILQ_FOREACH(pt, parents, entry)
4682 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4683 + 1 /* slash */;
4684 if (te)
4685 len += strlen(got_tree_entry_get_name(te));
4687 *path = calloc(1, len);
4688 if (path == NULL)
4689 return got_error_from_errno("calloc");
4691 (*path)[0] = '/';
4692 pt = TAILQ_LAST(parents, tog_parent_trees);
4693 while (pt) {
4694 const char *name = got_tree_entry_get_name(pt->selected_entry);
4695 if (strlcat(*path, name, len) >= len) {
4696 err = got_error(GOT_ERR_NO_SPACE);
4697 goto done;
4699 if (strlcat(*path, "/", len) >= len) {
4700 err = got_error(GOT_ERR_NO_SPACE);
4701 goto done;
4703 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4705 if (te) {
4706 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4707 err = got_error(GOT_ERR_NO_SPACE);
4708 goto done;
4711 done:
4712 if (err) {
4713 free(*path);
4714 *path = NULL;
4716 return err;
4719 static const struct got_error *
4720 blame_tree_entry(struct tog_view **new_view, int begin_x,
4721 struct got_tree_entry *te, struct tog_parent_trees *parents,
4722 struct got_object_id *commit_id, struct got_reflist_head *refs,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL;
4726 char *path;
4727 struct tog_view *blame_view;
4729 *new_view = NULL;
4731 err = tree_entry_path(&path, parents, te);
4732 if (err)
4733 return err;
4735 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4736 if (blame_view == NULL) {
4737 err = got_error_from_errno("view_open");
4738 goto done;
4741 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4742 if (err) {
4743 if (err->code == GOT_ERR_CANCELLED)
4744 err = NULL;
4745 view_close(blame_view);
4746 } else
4747 *new_view = blame_view;
4748 done:
4749 free(path);
4750 return err;
4753 static const struct got_error *
4754 log_tree_entry(struct tog_view **new_view, int begin_x,
4755 struct got_tree_entry *te, struct tog_parent_trees *parents,
4756 struct got_object_id *commit_id, struct got_reflist_head *refs,
4757 struct got_repository *repo)
4759 struct tog_view *log_view;
4760 const struct got_error *err = NULL;
4761 char *path;
4763 *new_view = NULL;
4765 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4766 if (log_view == NULL)
4767 return got_error_from_errno("view_open");
4769 err = tree_entry_path(&path, parents, te);
4770 if (err)
4771 return err;
4773 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4774 if (err)
4775 view_close(log_view);
4776 else
4777 *new_view = log_view;
4778 free(path);
4779 return err;
4782 static const struct got_error *
4783 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4784 struct got_object_id *commit_id, struct got_reflist_head *refs,
4785 struct got_repository *repo)
4787 const struct got_error *err = NULL;
4788 char *commit_id_str = NULL;
4789 struct tog_tree_view_state *s = &view->state.tree;
4791 TAILQ_INIT(&s->parents);
4793 err = got_object_id_str(&commit_id_str, commit_id);
4794 if (err != NULL)
4795 goto done;
4797 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4798 err = got_error_from_errno("asprintf");
4799 goto done;
4802 s->root = s->tree = root;
4803 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4804 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4805 s->commit_id = got_object_id_dup(commit_id);
4806 if (s->commit_id == NULL) {
4807 err = got_error_from_errno("got_object_id_dup");
4808 goto done;
4810 s->refs = refs;
4811 s->repo = repo;
4813 SIMPLEQ_INIT(&s->colors);
4815 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4816 err = add_color(&s->colors, "\\$$",
4817 TOG_COLOR_TREE_SUBMODULE,
4818 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4819 if (err)
4820 goto done;
4821 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4822 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4823 if (err) {
4824 free_colors(&s->colors);
4825 goto done;
4827 err = add_color(&s->colors, "/$",
4828 TOG_COLOR_TREE_DIRECTORY,
4829 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4830 if (err) {
4831 free_colors(&s->colors);
4832 goto done;
4835 err = add_color(&s->colors, "\\*$",
4836 TOG_COLOR_TREE_EXECUTABLE,
4837 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4838 if (err) {
4839 free_colors(&s->colors);
4840 goto done;
4843 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4844 get_color_value("TOG_COLOR_COMMIT"));
4845 if (err) {
4846 free_colors(&s->colors);
4847 goto done;
4851 view->show = show_tree_view;
4852 view->input = input_tree_view;
4853 view->close = close_tree_view;
4854 view->search_start = search_start_tree_view;
4855 view->search_next = search_next_tree_view;
4856 done:
4857 free(commit_id_str);
4858 if (err) {
4859 free(s->tree_label);
4860 s->tree_label = NULL;
4862 return err;
4865 static const struct got_error *
4866 close_tree_view(struct tog_view *view)
4868 struct tog_tree_view_state *s = &view->state.tree;
4870 free_colors(&s->colors);
4871 free(s->tree_label);
4872 s->tree_label = NULL;
4873 free(s->commit_id);
4874 s->commit_id = NULL;
4875 while (!TAILQ_EMPTY(&s->parents)) {
4876 struct tog_parent_tree *parent;
4877 parent = TAILQ_FIRST(&s->parents);
4878 TAILQ_REMOVE(&s->parents, parent, entry);
4879 free(parent);
4882 if (s->tree != s->root)
4883 got_object_tree_close(s->tree);
4884 got_object_tree_close(s->root);
4886 return NULL;
4889 static const struct got_error *
4890 search_start_tree_view(struct tog_view *view)
4892 struct tog_tree_view_state *s = &view->state.tree;
4894 s->matched_entry = NULL;
4895 return NULL;
4898 static int
4899 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4901 regmatch_t regmatch;
4903 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4904 0) == 0;
4907 static const struct got_error *
4908 search_next_tree_view(struct tog_view *view)
4910 struct tog_tree_view_state *s = &view->state.tree;
4911 struct got_tree_entry *te = NULL;
4913 if (!view->searching) {
4914 view->search_next_done = 1;
4915 return NULL;
4918 if (s->matched_entry) {
4919 if (view->searching == TOG_SEARCH_FORWARD) {
4920 if (s->selected_entry)
4921 te = got_tree_entry_get_next(s->tree,
4922 s->selected_entry);
4923 else
4924 te = got_object_tree_get_first_entry(s->tree);
4925 } else {
4926 if (s->selected_entry == NULL)
4927 te = got_object_tree_get_last_entry(s->tree);
4928 else
4929 te = got_tree_entry_get_prev(s->tree,
4930 s->selected_entry);
4932 } else {
4933 if (view->searching == TOG_SEARCH_FORWARD)
4934 te = got_object_tree_get_first_entry(s->tree);
4935 else
4936 te = got_object_tree_get_last_entry(s->tree);
4939 while (1) {
4940 if (te == NULL) {
4941 if (s->matched_entry == NULL) {
4942 view->search_next_done = 1;
4943 return NULL;
4945 if (view->searching == TOG_SEARCH_FORWARD)
4946 te = got_object_tree_get_first_entry(s->tree);
4947 else
4948 te = got_object_tree_get_last_entry(s->tree);
4951 if (match_tree_entry(te, &view->regex)) {
4952 view->search_next_done = 1;
4953 s->matched_entry = te;
4954 break;
4957 if (view->searching == TOG_SEARCH_FORWARD)
4958 te = got_tree_entry_get_next(s->tree, te);
4959 else
4960 te = got_tree_entry_get_prev(s->tree, te);
4963 if (s->matched_entry) {
4964 s->first_displayed_entry = s->matched_entry;
4965 s->selected = 0;
4968 return NULL;
4971 static const struct got_error *
4972 show_tree_view(struct tog_view *view)
4974 const struct got_error *err = NULL;
4975 struct tog_tree_view_state *s = &view->state.tree;
4976 char *parent_path;
4978 err = tree_entry_path(&parent_path, &s->parents, NULL);
4979 if (err)
4980 return err;
4982 err = draw_tree_entries(view, &s->first_displayed_entry,
4983 &s->last_displayed_entry, &s->selected_entry,
4984 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4985 s->tree, s->selected, view->nlines, s->tree == s->root,
4986 &s->colors);
4987 free(parent_path);
4989 view_vborder(view);
4990 return err;
4993 static const struct got_error *
4994 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4995 struct tog_view **focus_view, struct tog_view *view, int ch)
4997 const struct got_error *err = NULL;
4998 struct tog_tree_view_state *s = &view->state.tree;
4999 struct tog_view *log_view;
5000 int begin_x = 0, nscrolled;
5002 switch (ch) {
5003 case 'i':
5004 s->show_ids = !s->show_ids;
5005 break;
5006 case 'l':
5007 if (!s->selected_entry)
5008 break;
5009 if (view_is_parent_view(view))
5010 begin_x = view_split_begin_x(view->begin_x);
5011 err = log_tree_entry(&log_view, begin_x,
5012 s->selected_entry, &s->parents,
5013 s->commit_id, s->refs, s->repo);
5014 if (view_is_parent_view(view)) {
5015 err = view_close_child(view);
5016 if (err)
5017 return err;
5018 err = view_set_child(view, log_view);
5019 if (err) {
5020 view_close(log_view);
5021 break;
5023 *focus_view = log_view;
5024 view->child_focussed = 1;
5025 } else
5026 *new_view = log_view;
5027 break;
5028 case 'k':
5029 case KEY_UP:
5030 if (s->selected > 0) {
5031 s->selected--;
5032 if (s->selected == 0)
5033 break;
5035 if (s->selected > 0)
5036 break;
5037 tree_scroll_up(view, &s->first_displayed_entry, 1,
5038 s->tree, s->tree == s->root);
5039 break;
5040 case KEY_PPAGE:
5041 tree_scroll_up(view, &s->first_displayed_entry,
5042 MAX(0, view->nlines - 4 - s->selected), s->tree,
5043 s->tree == s->root);
5044 s->selected = 0;
5045 if (got_object_tree_get_first_entry(s->tree) ==
5046 s->first_displayed_entry && s->tree != s->root)
5047 s->first_displayed_entry = NULL;
5048 break;
5049 case 'j':
5050 case KEY_DOWN:
5051 if (s->selected < s->ndisplayed - 1) {
5052 s->selected++;
5053 break;
5055 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5056 == NULL)
5057 /* can't scroll any further */
5058 break;
5059 tree_scroll_down(&s->first_displayed_entry, 1,
5060 s->last_displayed_entry, s->tree);
5061 break;
5062 case KEY_NPAGE:
5063 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5064 == NULL) {
5065 /* can't scroll any further; move cursor down */
5066 if (s->selected < s->ndisplayed - 1)
5067 s->selected = s->ndisplayed - 1;
5068 break;
5070 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5071 view->nlines, s->last_displayed_entry, s->tree);
5072 if (nscrolled < view->nlines) {
5073 int ndisplayed = 0;
5074 struct got_tree_entry *te;
5075 te = s->first_displayed_entry;
5076 do {
5077 ndisplayed++;
5078 te = got_tree_entry_get_next(s->tree, te);
5079 } while (te);
5080 s->selected = ndisplayed - 1;
5082 break;
5083 case KEY_ENTER:
5084 case '\r':
5085 case KEY_BACKSPACE:
5086 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5087 struct tog_parent_tree *parent;
5088 /* user selected '..' */
5089 if (s->tree == s->root)
5090 break;
5091 parent = TAILQ_FIRST(&s->parents);
5092 TAILQ_REMOVE(&s->parents, parent,
5093 entry);
5094 got_object_tree_close(s->tree);
5095 s->tree = parent->tree;
5096 s->first_displayed_entry =
5097 parent->first_displayed_entry;
5098 s->selected_entry =
5099 parent->selected_entry;
5100 s->selected = parent->selected;
5101 free(parent);
5102 } else if (S_ISDIR(got_tree_entry_get_mode(
5103 s->selected_entry))) {
5104 struct got_tree_object *subtree;
5105 err = got_object_open_as_tree(&subtree, s->repo,
5106 got_tree_entry_get_id(s->selected_entry));
5107 if (err)
5108 break;
5109 err = tree_view_visit_subtree(subtree, s);
5110 if (err) {
5111 got_object_tree_close(subtree);
5112 break;
5114 } else if (S_ISREG(got_tree_entry_get_mode(
5115 s->selected_entry))) {
5116 struct tog_view *blame_view;
5117 int begin_x = view_is_parent_view(view) ?
5118 view_split_begin_x(view->begin_x) : 0;
5120 err = blame_tree_entry(&blame_view, begin_x,
5121 s->selected_entry, &s->parents,
5122 s->commit_id, s->refs, s->repo);
5123 if (err)
5124 break;
5125 if (view_is_parent_view(view)) {
5126 err = view_close_child(view);
5127 if (err)
5128 return err;
5129 err = view_set_child(view, blame_view);
5130 if (err) {
5131 view_close(blame_view);
5132 break;
5134 *focus_view = blame_view;
5135 view->child_focussed = 1;
5136 } else
5137 *new_view = blame_view;
5139 break;
5140 case KEY_RESIZE:
5141 if (s->selected > view->nlines)
5142 s->selected = s->ndisplayed - 1;
5143 break;
5144 default:
5145 break;
5148 return err;
5151 __dead static void
5152 usage_tree(void)
5154 endwin();
5155 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path]\n",
5156 getprogname());
5157 exit(1);
5160 static const struct got_error *
5161 cmd_tree(int argc, char *argv[])
5163 const struct got_error *error;
5164 struct got_repository *repo = NULL;
5165 struct got_reflist_head refs;
5166 char *repo_path = NULL;
5167 struct got_object_id *commit_id = NULL;
5168 char *commit_id_arg = NULL;
5169 struct got_commit_object *commit = NULL;
5170 struct got_tree_object *tree = NULL;
5171 int ch;
5172 struct tog_view *view;
5174 SIMPLEQ_INIT(&refs);
5176 #ifndef PROFILE
5177 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5178 NULL) == -1)
5179 err(1, "pledge");
5180 #endif
5182 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5183 switch (ch) {
5184 case 'c':
5185 commit_id_arg = optarg;
5186 break;
5187 case 'r':
5188 repo_path = realpath(optarg, NULL);
5189 if (repo_path == NULL)
5190 return got_error_from_errno2("realpath",
5191 optarg);
5192 break;
5193 default:
5194 usage_tree();
5195 /* NOTREACHED */
5199 argc -= optind;
5200 argv += optind;
5202 if (argc != 0)
5203 usage_tree();
5205 if (repo_path == NULL) {
5206 struct got_worktree *worktree;
5207 char *cwd = getcwd(NULL, 0);
5208 if (cwd == NULL)
5209 return got_error_from_errno("getcwd");
5210 error = got_worktree_open(&worktree, cwd);
5211 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5212 goto done;
5213 else
5214 error = NULL;
5215 if (worktree) {
5216 free(cwd);
5217 repo_path =
5218 strdup(got_worktree_get_repo_path(worktree));
5219 got_worktree_close(worktree);
5220 } else
5221 repo_path = cwd;
5222 if (repo_path == NULL) {
5223 error = got_error_from_errno("strdup");
5224 goto done;
5228 init_curses();
5230 error = got_repo_open(&repo, repo_path, NULL);
5231 if (error != NULL)
5232 goto done;
5234 error = apply_unveil(got_repo_get_path(repo), NULL);
5235 if (error)
5236 goto done;
5238 error = got_repo_match_object_id(&commit_id, NULL,
5239 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5240 GOT_OBJ_TYPE_COMMIT, 1, repo);
5241 if (error != NULL)
5242 goto done;
5244 error = got_object_open_as_commit(&commit, repo, commit_id);
5245 if (error != NULL)
5246 goto done;
5248 error = got_object_open_as_tree(&tree, repo,
5249 got_object_commit_get_tree_id(commit));
5250 if (error != NULL)
5251 goto done;
5253 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5254 if (error)
5255 goto done;
5257 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5258 if (view == NULL) {
5259 error = got_error_from_errno("view_open");
5260 goto done;
5262 error = open_tree_view(view, tree, commit_id, &refs, repo);
5263 if (error)
5264 goto done;
5265 error = view_loop(view);
5266 done:
5267 free(repo_path);
5268 free(commit_id);
5269 if (commit)
5270 got_object_commit_close(commit);
5271 if (tree)
5272 got_object_tree_close(tree);
5273 if (repo)
5274 got_repo_close(repo);
5275 got_ref_list_free(&refs);
5276 return error;
5279 static void
5280 list_commands(void)
5282 int i;
5284 fprintf(stderr, "commands:");
5285 for (i = 0; i < nitems(tog_commands); i++) {
5286 struct tog_cmd *cmd = &tog_commands[i];
5287 fprintf(stderr, " %s", cmd->name);
5289 fputc('\n', stderr);
5292 __dead static void
5293 usage(int hflag)
5295 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5296 getprogname());
5297 if (hflag)
5298 list_commands();
5299 exit(1);
5302 static char **
5303 make_argv(const char *arg0, const char *arg1)
5305 char **argv;
5306 int argc = (arg1 == NULL ? 1 : 2);
5308 argv = calloc(argc, sizeof(char *));
5309 if (argv == NULL)
5310 err(1, "calloc");
5311 argv[0] = strdup(arg0);
5312 if (argv[0] == NULL)
5313 err(1, "strdup");
5314 if (arg1) {
5315 argv[1] = strdup(arg1);
5316 if (argv[1] == NULL)
5317 err(1, "strdup");
5320 return argv;
5323 int
5324 main(int argc, char *argv[])
5326 const struct got_error *error = NULL;
5327 struct tog_cmd *cmd = NULL;
5328 int ch, hflag = 0, Vflag = 0;
5329 char **cmd_argv = NULL;
5330 static struct option longopts[] = {
5331 { "version", no_argument, NULL, 'V' },
5332 { NULL, 0, NULL, 0}
5335 setlocale(LC_CTYPE, "");
5337 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5338 switch (ch) {
5339 case 'h':
5340 hflag = 1;
5341 break;
5342 case 'V':
5343 Vflag = 1;
5344 break;
5345 default:
5346 usage(hflag);
5347 /* NOTREACHED */
5351 argc -= optind;
5352 argv += optind;
5353 optind = 0;
5354 optreset = 1;
5356 if (Vflag) {
5357 got_version_print_str();
5358 return 1;
5361 if (argc == 0) {
5362 if (hflag)
5363 usage(hflag);
5364 /* Build an argument vector which runs a default command. */
5365 cmd = &tog_commands[0];
5366 cmd_argv = make_argv(cmd->name, NULL);
5367 argc = 1;
5368 } else {
5369 int i;
5371 /* Did the user specific a command? */
5372 for (i = 0; i < nitems(tog_commands); i++) {
5373 if (strncmp(tog_commands[i].name, argv[0],
5374 strlen(argv[0])) == 0) {
5375 cmd = &tog_commands[i];
5376 break;
5380 if (cmd == NULL) {
5381 fprintf(stderr, "%s: unknown command '%s'\n",
5382 getprogname(), argv[0]);
5383 list_commands();
5384 return 1;
5388 if (hflag)
5389 cmd->cmd_usage();
5390 else
5391 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5393 endwin();
5394 free(cmd_argv);
5395 if (error && error->code != GOT_ERR_CANCELLED)
5396 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5397 return 0;