Blob


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