Blob


1 /*
2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include <sys/queue.h>
18 #include <sys/stat.h>
19 #include <sys/ioctl.h>
21 #include <errno.h>
22 #define _XOPEN_SOURCE_EXTENDED
23 #include <curses.h>
24 #undef _XOPEN_SOURCE_EXTENDED
25 #include <panel.h>
26 #include <locale.h>
27 #include <signal.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <err.h>
33 #include <unistd.h>
34 #include <util.h>
35 #include <limits.h>
36 #include <wchar.h>
37 #include <time.h>
38 #include <pthread.h>
39 #include <libgen.h>
40 #include <regex.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
47 #include "got_diff.h"
48 #include "got_opentemp.h"
49 #include "got_utf8.h"
50 #include "got_cancel.h"
51 #include "got_commit_graph.h"
52 #include "got_blame.h"
53 #include "got_privsep.h"
54 #include "got_path.h"
55 #include "got_worktree.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #ifndef MAX
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #endif
65 #define CTRL(x) ((x) & 0x1f)
67 #ifndef nitems
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 #endif
71 struct tog_cmd {
72 const char *name;
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 };
77 __dead static void usage(int);
78 __dead static void usage_log(void);
79 __dead static void usage_diff(void);
80 __dead static void usage_blame(void);
81 __dead static void usage_tree(void);
83 static const struct got_error* cmd_log(int, char *[]);
84 static const struct got_error* cmd_diff(int, char *[]);
85 static const struct got_error* cmd_blame(int, char *[]);
86 static const struct got_error* cmd_tree(int, char *[]);
88 static struct tog_cmd tog_commands[] = {
89 { "log", cmd_log, usage_log },
90 { "diff", cmd_diff, usage_diff },
91 { "blame", cmd_blame, usage_blame },
92 { "tree", cmd_tree, usage_tree },
93 };
95 enum tog_view_type {
96 TOG_VIEW_DIFF,
97 TOG_VIEW_LOG,
98 TOG_VIEW_BLAME,
99 TOG_VIEW_TREE
100 };
102 #define TOG_EOF_STRING "(END)"
104 struct commit_queue_entry {
105 TAILQ_ENTRY(commit_queue_entry) entry;
106 struct got_object_id *id;
107 struct got_commit_object *commit;
108 int idx;
109 };
110 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
111 struct commit_queue {
112 int ncommits;
113 struct commit_queue_head head;
114 };
116 struct tog_color {
117 SIMPLEQ_ENTRY(tog_color) entry;
118 regex_t regex;
119 short colorpair;
120 };
121 SIMPLEQ_HEAD(tog_colors, tog_color);
123 static const struct got_error *
124 add_color(struct tog_colors *colors, const char *pattern,
125 int idx, short color)
127 const struct got_error *err = NULL;
128 struct tog_color *tc;
129 int regerr = 0;
131 if (idx < 1 || idx > COLOR_PAIRS - 1)
132 return NULL;
134 init_pair(idx, color, -1);
136 tc = calloc(1, sizeof(*tc));
137 if (tc == NULL)
138 return got_error_from_errno("calloc");
139 regerr = regcomp(&tc->regex, pattern,
140 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
141 if (regerr) {
142 static char regerr_msg[512];
143 static char err_msg[512];
144 regerror(regerr, &tc->regex, regerr_msg,
145 sizeof(regerr_msg));
146 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
147 regerr_msg);
148 err = got_error_msg(GOT_ERR_REGEX, err_msg);
149 free(tc);
150 return err;
152 tc->colorpair = idx;
153 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
154 return NULL;
157 static void
158 free_colors(struct tog_colors *colors)
160 struct tog_color *tc;
162 while (!SIMPLEQ_EMPTY(colors)) {
163 tc = SIMPLEQ_FIRST(colors);
164 SIMPLEQ_REMOVE_HEAD(colors, entry);
165 regfree(&tc->regex);
166 free(tc);
170 struct tog_color *
171 get_color(struct tog_colors *colors, int colorpair)
173 struct tog_color *tc = NULL;
175 SIMPLEQ_FOREACH(tc, colors, entry) {
176 if (tc->colorpair == colorpair)
177 return tc;
180 return NULL;
183 static int
184 default_color_value(const char *envvar)
186 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
187 return COLOR_MAGENTA;
188 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
189 return COLOR_CYAN;
190 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
191 return COLOR_YELLOW;
192 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
193 return COLOR_GREEN;
194 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
195 return COLOR_MAGENTA;
196 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
197 return COLOR_MAGENTA;
198 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
199 return COLOR_CYAN;
200 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
201 return COLOR_GREEN;
202 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
203 return COLOR_GREEN;
204 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
205 return COLOR_CYAN;
206 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
207 return COLOR_YELLOW;
209 return -1;
212 static int
213 get_color_value(const char *envvar)
215 const char *val = getenv(envvar);
217 if (val == NULL)
218 return default_color_value(envvar);
220 if (strcasecmp(val, "black") == 0)
221 return COLOR_BLACK;
222 if (strcasecmp(val, "red") == 0)
223 return COLOR_RED;
224 if (strcasecmp(val, "green") == 0)
225 return COLOR_GREEN;
226 if (strcasecmp(val, "yellow") == 0)
227 return COLOR_YELLOW;
228 if (strcasecmp(val, "blue") == 0)
229 return COLOR_BLUE;
230 if (strcasecmp(val, "magenta") == 0)
231 return COLOR_MAGENTA;
232 if (strcasecmp(val, "cyan") == 0)
233 return COLOR_CYAN;
234 if (strcasecmp(val, "white") == 0)
235 return COLOR_WHITE;
236 if (strcasecmp(val, "default") == 0)
237 return -1;
239 return default_color_value(envvar);
243 struct tog_diff_view_state {
244 struct got_object_id *id1, *id2;
245 FILE *f;
246 int first_displayed_line;
247 int last_displayed_line;
248 int eof;
249 int diff_context;
250 struct got_repository *repo;
251 struct got_reflist_head *refs;
252 struct tog_colors colors;
253 int nlines;
254 off_t *line_offsets;
255 int matched_line;
256 int selected_line;
257 size_t filesize;
259 /* passed from log view; may be NULL */
260 struct tog_view *log_view;
261 };
263 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
265 struct tog_log_thread_args {
266 pthread_cond_t need_commits;
267 int commits_needed;
268 struct got_commit_graph *graph;
269 struct commit_queue *commits;
270 const char *in_repo_path;
271 struct got_object_id *start_id;
272 struct got_repository *repo;
273 int log_complete;
274 sig_atomic_t *quit;
275 struct commit_queue_entry **first_displayed_entry;
276 struct commit_queue_entry **selected_entry;
277 int *searching;
278 int *search_next_done;
279 regex_t *regex;
280 };
282 struct tog_log_view_state {
283 struct commit_queue commits;
284 struct commit_queue_entry *first_displayed_entry;
285 struct commit_queue_entry *last_displayed_entry;
286 struct commit_queue_entry *selected_entry;
287 int selected;
288 char *in_repo_path;
289 const char *head_ref_name;
290 int log_branches;
291 struct got_repository *repo;
292 struct got_reflist_head *refs;
293 struct got_object_id *start_id;
294 sig_atomic_t quit;
295 pthread_t thread;
296 struct tog_log_thread_args thread_args;
297 struct commit_queue_entry *matched_entry;
298 struct commit_queue_entry *search_entry;
299 struct tog_colors colors;
300 };
302 #define TOG_COLOR_DIFF_MINUS 1
303 #define TOG_COLOR_DIFF_PLUS 2
304 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
305 #define TOG_COLOR_DIFF_META 4
306 #define TOG_COLOR_TREE_SUBMODULE 5
307 #define TOG_COLOR_TREE_SYMLINK 6
308 #define TOG_COLOR_TREE_DIRECTORY 7
309 #define TOG_COLOR_TREE_EXECUTABLE 8
310 #define TOG_COLOR_COMMIT 9
311 #define TOG_COLOR_AUTHOR 10
312 #define TOG_COLOR_DATE 11
314 struct tog_blame_cb_args {
315 struct tog_blame_line *lines; /* one per line */
316 int nlines;
318 struct tog_view *view;
319 struct got_object_id *commit_id;
320 int *quit;
321 };
323 struct tog_blame_thread_args {
324 const char *path;
325 struct got_repository *repo;
326 struct tog_blame_cb_args *cb_args;
327 int *complete;
328 got_cancel_cb cancel_cb;
329 void *cancel_arg;
330 };
332 struct tog_blame {
333 FILE *f;
334 size_t filesize;
335 struct tog_blame_line *lines;
336 int nlines;
337 off_t *line_offsets;
338 pthread_t thread;
339 struct tog_blame_thread_args thread_args;
340 struct tog_blame_cb_args cb_args;
341 const char *path;
342 };
344 struct tog_blame_view_state {
345 int first_displayed_line;
346 int last_displayed_line;
347 int selected_line;
348 int blame_complete;
349 int eof;
350 int done;
351 struct got_object_id_queue blamed_commits;
352 struct got_object_qid *blamed_commit;
353 char *path;
354 struct got_repository *repo;
355 struct got_reflist_head *refs;
356 struct got_object_id *commit_id;
357 struct tog_blame blame;
358 int matched_line;
359 struct tog_colors colors;
360 };
362 struct tog_parent_tree {
363 TAILQ_ENTRY(tog_parent_tree) entry;
364 struct got_tree_object *tree;
365 struct got_tree_entry *first_displayed_entry;
366 struct got_tree_entry *selected_entry;
367 int selected;
368 };
370 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
372 struct tog_tree_view_state {
373 char *tree_label;
374 struct got_tree_object *root;
375 struct got_tree_object *tree;
376 struct got_tree_entry *first_displayed_entry;
377 struct got_tree_entry *last_displayed_entry;
378 struct got_tree_entry *selected_entry;
379 int ndisplayed, selected, show_ids;
380 struct tog_parent_trees parents;
381 struct got_object_id *commit_id;
382 struct got_repository *repo;
383 struct got_reflist_head *refs;
384 struct got_tree_entry *matched_entry;
385 struct tog_colors colors;
386 };
388 /*
389 * We implement two types of views: parent views and child views.
391 * The 'Tab' key switches between a parent view and its child view.
392 * Child views are shown side-by-side to their parent view, provided
393 * there is enough screen estate.
395 * When a new view is opened from within a parent view, this new view
396 * becomes a child view of the parent view, replacing any existing child.
398 * When a new view is opened from within a child view, this new view
399 * becomes a parent view which will obscure the views below until the
400 * user quits the new parent view by typing 'q'.
402 * This list of views contains parent views only.
403 * Child views are only pointed to by their parent view.
404 */
405 TAILQ_HEAD(tog_view_list_head, tog_view);
407 struct tog_view {
408 TAILQ_ENTRY(tog_view) entry;
409 WINDOW *window;
410 PANEL *panel;
411 int nlines, ncols, begin_y, begin_x;
412 int lines, cols; /* copies of LINES and COLS */
413 int focussed;
414 struct tog_view *parent;
415 struct tog_view *child;
416 int child_focussed;
418 /* type-specific state */
419 enum tog_view_type type;
420 union {
421 struct tog_diff_view_state diff;
422 struct tog_log_view_state log;
423 struct tog_blame_view_state blame;
424 struct tog_tree_view_state tree;
425 } state;
427 const struct got_error *(*show)(struct tog_view *);
428 const struct got_error *(*input)(struct tog_view **,
429 struct tog_view **, struct tog_view**, struct tog_view *, int);
430 const struct got_error *(*close)(struct tog_view *);
432 const struct got_error *(*search_start)(struct tog_view *);
433 const struct got_error *(*search_next)(struct tog_view *);
434 int searching;
435 #define TOG_SEARCH_FORWARD 1
436 #define TOG_SEARCH_BACKWARD 2
437 int search_next_done;
438 regex_t regex;
439 };
441 static const struct got_error *open_diff_view(struct tog_view *,
442 struct got_object_id *, struct got_object_id *, struct tog_view *,
443 struct got_reflist_head *, struct got_repository *);
444 static const struct got_error *show_diff_view(struct tog_view *);
445 static const struct got_error *input_diff_view(struct tog_view **,
446 struct tog_view **, struct tog_view **, struct tog_view *, int);
447 static const struct got_error* close_diff_view(struct tog_view *);
448 static const struct got_error *search_start_diff_view(struct tog_view *);
449 static const struct got_error *search_next_diff_view(struct tog_view *);
451 static const struct got_error *open_log_view(struct tog_view *,
452 struct got_object_id *, struct got_reflist_head *,
453 struct got_repository *, const char *, const char *, int, int);
454 static const struct got_error * show_log_view(struct tog_view *);
455 static const struct got_error *input_log_view(struct tog_view **,
456 struct tog_view **, struct tog_view **, struct tog_view *, int);
457 static const struct got_error *close_log_view(struct tog_view *);
458 static const struct got_error *search_start_log_view(struct tog_view *);
459 static const struct got_error *search_next_log_view(struct tog_view *);
461 static const struct got_error *open_blame_view(struct tog_view *, char *,
462 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
463 static const struct got_error *show_blame_view(struct tog_view *);
464 static const struct got_error *input_blame_view(struct tog_view **,
465 struct tog_view **, struct tog_view **, struct tog_view *, int);
466 static const struct got_error *close_blame_view(struct tog_view *);
467 static const struct got_error *search_start_blame_view(struct tog_view *);
468 static const struct got_error *search_next_blame_view(struct tog_view *);
470 static const struct got_error *open_tree_view(struct tog_view *,
471 struct got_tree_object *, struct got_object_id *,
472 struct got_reflist_head *, struct got_repository *);
473 static const struct got_error *show_tree_view(struct tog_view *);
474 static const struct got_error *input_tree_view(struct tog_view **,
475 struct tog_view **, struct tog_view **, struct tog_view *, int);
476 static const struct got_error *close_tree_view(struct tog_view *);
477 static const struct got_error *search_start_tree_view(struct tog_view *);
478 static const struct got_error *search_next_tree_view(struct tog_view *);
480 static volatile sig_atomic_t tog_sigwinch_received;
481 static volatile sig_atomic_t tog_sigpipe_received;
482 static volatile sig_atomic_t tog_sigcont_received;
484 static void
485 tog_sigwinch(int signo)
487 tog_sigwinch_received = 1;
490 static void
491 tog_sigpipe(int signo)
493 tog_sigpipe_received = 1;
496 static void
497 tog_sigcont(int signo)
499 tog_sigcont_received = 1;
502 static const struct got_error *
503 view_close(struct tog_view *view)
505 const struct got_error *err = NULL;
507 if (view->child) {
508 view_close(view->child);
509 view->child = NULL;
511 if (view->close)
512 err = view->close(view);
513 if (view->panel)
514 del_panel(view->panel);
515 if (view->window)
516 delwin(view->window);
517 free(view);
518 return err;
521 static struct tog_view *
522 view_open(int nlines, int ncols, int begin_y, int begin_x,
523 enum tog_view_type type)
525 struct tog_view *view = calloc(1, sizeof(*view));
527 if (view == NULL)
528 return NULL;
530 view->type = type;
531 view->lines = LINES;
532 view->cols = COLS;
533 view->nlines = nlines ? nlines : LINES - begin_y;
534 view->ncols = ncols ? ncols : COLS - begin_x;
535 view->begin_y = begin_y;
536 view->begin_x = begin_x;
537 view->window = newwin(nlines, ncols, begin_y, begin_x);
538 if (view->window == NULL) {
539 view_close(view);
540 return NULL;
542 view->panel = new_panel(view->window);
543 if (view->panel == NULL ||
544 set_panel_userptr(view->panel, view) != OK) {
545 view_close(view);
546 return NULL;
549 keypad(view->window, TRUE);
550 return view;
553 static int
554 view_split_begin_x(int begin_x)
556 if (begin_x > 0 || COLS < 120)
557 return 0;
558 return (COLS - MAX(COLS / 2, 80));
561 static const struct got_error *view_resize(struct tog_view *);
563 static const struct got_error *
564 view_splitscreen(struct tog_view *view)
566 const struct got_error *err = NULL;
568 view->begin_y = 0;
569 view->begin_x = view_split_begin_x(0);
570 view->nlines = LINES;
571 view->ncols = COLS - view->begin_x;
572 view->lines = LINES;
573 view->cols = COLS;
574 err = view_resize(view);
575 if (err)
576 return err;
578 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
579 return got_error_from_errno("mvwin");
581 return NULL;
584 static const struct got_error *
585 view_fullscreen(struct tog_view *view)
587 const struct got_error *err = NULL;
589 view->begin_x = 0;
590 view->begin_y = 0;
591 view->nlines = LINES;
592 view->ncols = COLS;
593 view->lines = LINES;
594 view->cols = COLS;
595 err = view_resize(view);
596 if (err)
597 return err;
599 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
600 return got_error_from_errno("mvwin");
602 return NULL;
605 static int
606 view_is_parent_view(struct tog_view *view)
608 return view->parent == NULL;
611 static const struct got_error *
612 view_resize(struct tog_view *view)
614 int nlines, ncols;
616 if (view->lines > LINES)
617 nlines = view->nlines - (view->lines - LINES);
618 else
619 nlines = view->nlines + (LINES - view->lines);
621 if (view->cols > COLS)
622 ncols = view->ncols - (view->cols - COLS);
623 else
624 ncols = view->ncols + (COLS - view->cols);
626 if (wresize(view->window, nlines, ncols) == ERR)
627 return got_error_from_errno("wresize");
628 if (replace_panel(view->panel, view->window) == ERR)
629 return got_error_from_errno("replace_panel");
630 wclear(view->window);
632 view->nlines = nlines;
633 view->ncols = ncols;
634 view->lines = LINES;
635 view->cols = COLS;
637 if (view->child) {
638 view->child->begin_x = view_split_begin_x(view->begin_x);
639 if (view->child->begin_x == 0) {
640 view_fullscreen(view->child);
641 if (view->child->focussed)
642 show_panel(view->child->panel);
643 else
644 show_panel(view->panel);
645 } else {
646 view_splitscreen(view->child);
647 show_panel(view->child->panel);
651 return NULL;
654 static const struct got_error *
655 view_close_child(struct tog_view *view)
657 const struct got_error *err = NULL;
659 if (view->child == NULL)
660 return NULL;
662 err = view_close(view->child);
663 view->child = NULL;
664 return err;
667 static const struct got_error *
668 view_set_child(struct tog_view *view, struct tog_view *child)
670 const struct got_error *err = NULL;
672 view->child = child;
673 child->parent = view;
674 return err;
677 static int
678 view_is_splitscreen(struct tog_view *view)
680 return view->begin_x > 0;
683 static void
684 tog_resizeterm(void)
686 int cols, lines;
687 struct winsize size;
689 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
690 cols = 80; /* Default */
691 lines = 24;
692 } else {
693 cols = size.ws_col;
694 lines = size.ws_row;
696 resize_term(lines, cols);
699 static const struct got_error *
700 view_search_start(struct tog_view *view)
702 const struct got_error *err = NULL;
703 char pattern[1024];
704 int ret;
705 int begin_x = 0;
707 if (view->nlines < 1)
708 return NULL;
710 if (!view_is_parent_view(view))
711 begin_x = view_split_begin_x(view->begin_x);
712 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
713 begin_x, "/");
714 wclrtoeol(view->window);
716 nocbreak();
717 echo();
718 ret = wgetnstr(view->window, pattern, sizeof(pattern));
719 cbreak();
720 noecho();
721 if (ret == ERR)
722 return NULL;
724 if (view->searching) {
725 regfree(&view->regex);
726 view->searching = 0;
729 if (regcomp(&view->regex, pattern,
730 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 err = view->search_start(view);
732 if (err) {
733 regfree(&view->regex);
734 return err;
736 view->searching = TOG_SEARCH_FORWARD;
737 view->search_next_done = 0;
738 view->search_next(view);
741 return NULL;
744 static const struct got_error *
745 view_input(struct tog_view **new, struct tog_view **dead,
746 struct tog_view **focus, int *done, struct tog_view *view,
747 struct tog_view_list_head *views)
749 const struct got_error *err = NULL;
750 struct tog_view *v;
751 int ch, errcode;
753 *new = NULL;
754 *dead = NULL;
755 *focus = NULL;
757 if (view->searching && !view->search_next_done) {
758 view->search_next(view);
759 return NULL;
762 nodelay(stdscr, FALSE);
763 /* Allow threads to make progress while we are waiting for input. */
764 errcode = pthread_mutex_unlock(&tog_mutex);
765 if (errcode)
766 return got_error_set_errno(errcode, "pthread_mutex_unlock");
767 ch = wgetch(view->window);
768 errcode = pthread_mutex_lock(&tog_mutex);
769 if (errcode)
770 return got_error_set_errno(errcode, "pthread_mutex_lock");
771 nodelay(stdscr, TRUE);
773 if (tog_sigwinch_received || tog_sigcont_received) {
774 tog_resizeterm();
775 tog_sigwinch_received = 0;
776 tog_sigcont_received = 0;
777 TAILQ_FOREACH(v, views, entry) {
778 err = view_resize(v);
779 if (err)
780 return err;
781 err = v->input(new, dead, focus, v, KEY_RESIZE);
782 if (err)
783 return err;
787 switch (ch) {
788 case ERR:
789 break;
790 case '\t':
791 if (view->child) {
792 *focus = view->child;
793 view->child_focussed = 1;
794 } else if (view->parent) {
795 *focus = view->parent;
796 view->parent->child_focussed = 0;
798 break;
799 case 'q':
800 err = view->input(new, dead, focus, view, ch);
801 *dead = view;
802 break;
803 case 'Q':
804 *done = 1;
805 break;
806 case 'f':
807 if (view_is_parent_view(view)) {
808 if (view->child == NULL)
809 break;
810 if (view_is_splitscreen(view->child)) {
811 *focus = view->child;
812 view->child_focussed = 1;
813 err = view_fullscreen(view->child);
814 } else
815 err = view_splitscreen(view->child);
816 if (err)
817 break;
818 err = view->child->input(new, dead, focus,
819 view->child, KEY_RESIZE);
820 } else {
821 if (view_is_splitscreen(view)) {
822 *focus = view;
823 view->parent->child_focussed = 1;
824 err = view_fullscreen(view);
825 } else {
826 err = view_splitscreen(view);
828 if (err)
829 break;
830 err = view->input(new, dead, focus, view,
831 KEY_RESIZE);
833 break;
834 case KEY_RESIZE:
835 break;
836 case '/':
837 if (view->search_start)
838 view_search_start(view);
839 else
840 err = view->input(new, dead, focus, view, ch);
841 break;
842 case 'N':
843 case 'n':
844 if (view->search_next && view->searching) {
845 view->searching = (ch == 'n' ?
846 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
847 view->search_next_done = 0;
848 view->search_next(view);
849 } else
850 err = view->input(new, dead, focus, view, ch);
851 break;
852 default:
853 err = view->input(new, dead, focus, view, ch);
854 break;
857 return err;
860 void
861 view_vborder(struct tog_view *view)
863 PANEL *panel;
864 struct tog_view *view_above;
866 if (view->parent)
867 return view_vborder(view->parent);
869 panel = panel_above(view->panel);
870 if (panel == NULL)
871 return;
873 view_above = panel_userptr(panel);
874 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
875 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
878 int
879 view_needs_focus_indication(struct tog_view *view)
881 if (view_is_parent_view(view)) {
882 if (view->child == NULL || view->child_focussed)
883 return 0;
884 if (!view_is_splitscreen(view->child))
885 return 0;
886 } else if (!view_is_splitscreen(view))
887 return 0;
889 return view->focussed;
892 static const struct got_error *
893 view_loop(struct tog_view *view)
895 const struct got_error *err = NULL;
896 struct tog_view_list_head views;
897 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
898 int fast_refresh = 10;
899 int done = 0, errcode;
901 errcode = pthread_mutex_lock(&tog_mutex);
902 if (errcode)
903 return got_error_set_errno(errcode, "pthread_mutex_lock");
905 TAILQ_INIT(&views);
906 TAILQ_INSERT_HEAD(&views, view, entry);
908 main_view = view;
909 view->focussed = 1;
910 err = view->show(view);
911 if (err)
912 return err;
913 update_panels();
914 doupdate();
915 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
916 /* Refresh fast during initialization, then become slower. */
917 if (fast_refresh && fast_refresh-- == 0)
918 halfdelay(10); /* switch to once per second */
920 err = view_input(&new_view, &dead_view, &focus_view, &done,
921 view, &views);
922 if (err)
923 break;
924 if (dead_view) {
925 struct tog_view *prev = NULL;
927 if (view_is_parent_view(dead_view))
928 prev = TAILQ_PREV(dead_view,
929 tog_view_list_head, entry);
930 else if (view->parent != dead_view)
931 prev = view->parent;
933 if (dead_view->parent)
934 dead_view->parent->child = NULL;
935 else
936 TAILQ_REMOVE(&views, dead_view, entry);
938 err = view_close(dead_view);
939 if (err || (dead_view == main_view && new_view == NULL))
940 goto done;
942 if (view == dead_view) {
943 if (focus_view)
944 view = focus_view;
945 else if (prev)
946 view = prev;
947 else if (!TAILQ_EMPTY(&views))
948 view = TAILQ_LAST(&views,
949 tog_view_list_head);
950 else
951 view = NULL;
952 if (view) {
953 if (view->child && view->child_focussed)
954 focus_view = view->child;
955 else
956 focus_view = view;
960 if (new_view) {
961 struct tog_view *v, *t;
962 /* Only allow one parent view per type. */
963 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
964 if (v->type != new_view->type)
965 continue;
966 TAILQ_REMOVE(&views, v, entry);
967 err = view_close(v);
968 if (err)
969 goto done;
970 break;
972 TAILQ_INSERT_TAIL(&views, new_view, entry);
973 view = new_view;
974 if (focus_view == NULL)
975 focus_view = new_view;
977 if (focus_view) {
978 show_panel(focus_view->panel);
979 if (view)
980 view->focussed = 0;
981 focus_view->focussed = 1;
982 view = focus_view;
983 if (new_view)
984 show_panel(new_view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
988 if (view) {
989 if (focus_view == NULL) {
990 view->focussed = 1;
991 show_panel(view->panel);
992 if (view->child && view_is_splitscreen(view->child))
993 show_panel(view->child->panel);
994 focus_view = view;
996 if (view->parent) {
997 err = view->parent->show(view->parent);
998 if (err)
999 goto done;
1001 err = view->show(view);
1002 if (err)
1003 goto done;
1004 if (view->child) {
1005 err = view->child->show(view->child);
1006 if (err)
1007 goto done;
1009 update_panels();
1010 doupdate();
1013 done:
1014 while (!TAILQ_EMPTY(&views)) {
1015 view = TAILQ_FIRST(&views);
1016 TAILQ_REMOVE(&views, view, entry);
1017 view_close(view);
1020 errcode = pthread_mutex_unlock(&tog_mutex);
1021 if (errcode && err == NULL)
1022 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1024 return err;
1027 __dead static void
1028 usage_log(void)
1030 endwin();
1031 fprintf(stderr,
1032 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1033 getprogname());
1034 exit(1);
1037 /* Create newly allocated wide-character string equivalent to a byte string. */
1038 static const struct got_error *
1039 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1041 char *vis = NULL;
1042 const struct got_error *err = NULL;
1044 *ws = NULL;
1045 *wlen = mbstowcs(NULL, s, 0);
1046 if (*wlen == (size_t)-1) {
1047 int vislen;
1048 if (errno != EILSEQ)
1049 return got_error_from_errno("mbstowcs");
1051 /* byte string invalid in current encoding; try to "fix" it */
1052 err = got_mbsavis(&vis, &vislen, s);
1053 if (err)
1054 return err;
1055 *wlen = mbstowcs(NULL, vis, 0);
1056 if (*wlen == (size_t)-1) {
1057 err = got_error_from_errno("mbstowcs"); /* give up */
1058 goto done;
1062 *ws = calloc(*wlen + 1, sizeof(**ws));
1063 if (*ws == NULL) {
1064 err = got_error_from_errno("calloc");
1065 goto done;
1068 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1069 err = got_error_from_errno("mbstowcs");
1070 done:
1071 free(vis);
1072 if (err) {
1073 free(*ws);
1074 *ws = NULL;
1075 *wlen = 0;
1077 return err;
1080 /* Format a line for display, ensuring that it won't overflow a width limit. */
1081 static const struct got_error *
1082 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1083 int col_tab_align)
1085 const struct got_error *err = NULL;
1086 int cols = 0;
1087 wchar_t *wline = NULL;
1088 size_t wlen;
1089 int i;
1091 *wlinep = NULL;
1092 *widthp = 0;
1094 err = mbs2ws(&wline, &wlen, line);
1095 if (err)
1096 return err;
1098 i = 0;
1099 while (i < wlen) {
1100 int width = wcwidth(wline[i]);
1102 if (width == 0) {
1103 i++;
1104 continue;
1107 if (width == 1 || width == 2) {
1108 if (cols + width > wlimit)
1109 break;
1110 cols += width;
1111 i++;
1112 } else if (width == -1) {
1113 if (wline[i] == L'\t') {
1114 width = TABSIZE -
1115 ((cols + col_tab_align) % TABSIZE);
1116 if (cols + width > wlimit)
1117 break;
1118 cols += width;
1120 i++;
1121 } else {
1122 err = got_error_from_errno("wcwidth");
1123 goto done;
1126 wline[i] = L'\0';
1127 if (widthp)
1128 *widthp = cols;
1129 done:
1130 if (err)
1131 free(wline);
1132 else
1133 *wlinep = wline;
1134 return err;
1137 static const struct got_error*
1138 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1139 struct got_object_id *id, struct got_repository *repo)
1141 static const struct got_error *err = NULL;
1142 struct got_reflist_entry *re;
1143 char *s;
1144 const char *name;
1146 *refs_str = NULL;
1148 SIMPLEQ_FOREACH(re, refs, entry) {
1149 struct got_tag_object *tag = NULL;
1150 int cmp;
1152 name = got_ref_get_name(re->ref);
1153 if (strcmp(name, GOT_REF_HEAD) == 0)
1154 continue;
1155 if (strncmp(name, "refs/", 5) == 0)
1156 name += 5;
1157 if (strncmp(name, "got/", 4) == 0)
1158 continue;
1159 if (strncmp(name, "heads/", 6) == 0)
1160 name += 6;
1161 if (strncmp(name, "remotes/", 8) == 0)
1162 name += 8;
1163 if (strncmp(name, "tags/", 5) == 0) {
1164 err = got_object_open_as_tag(&tag, repo, re->id);
1165 if (err) {
1166 if (err->code != GOT_ERR_OBJ_TYPE)
1167 break;
1168 /* Ref points at something other than a tag. */
1169 err = NULL;
1170 tag = NULL;
1173 cmp = got_object_id_cmp(tag ?
1174 got_object_tag_get_object_id(tag) : re->id, id);
1175 if (tag)
1176 got_object_tag_close(tag);
1177 if (cmp != 0)
1178 continue;
1179 s = *refs_str;
1180 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1181 s ? ", " : "", name) == -1) {
1182 err = got_error_from_errno("asprintf");
1183 free(s);
1184 *refs_str = NULL;
1185 break;
1187 free(s);
1190 return err;
1193 static const struct got_error *
1194 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1195 int col_tab_align)
1197 char *smallerthan, *at;
1199 smallerthan = strchr(author, '<');
1200 if (smallerthan && smallerthan[1] != '\0')
1201 author = smallerthan + 1;
1202 at = strchr(author, '@');
1203 if (at)
1204 *at = '\0';
1205 return format_line(wauthor, author_width, author, limit, col_tab_align);
1208 static const struct got_error *
1209 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1210 struct got_object_id *id, struct got_reflist_head *refs,
1211 const size_t date_display_cols, int author_display_cols,
1212 struct tog_colors *colors)
1214 const struct got_error *err = NULL;
1215 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1216 char *logmsg0 = NULL, *logmsg = NULL;
1217 char *author = NULL;
1218 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1219 int author_width, logmsg_width;
1220 char *newline, *line = NULL;
1221 int col, limit;
1222 const int avail = view->ncols;
1223 struct tm tm;
1224 time_t committer_time;
1225 struct tog_color *tc;
1227 committer_time = got_object_commit_get_committer_time(commit);
1228 if (localtime_r(&committer_time, &tm) == NULL)
1229 return got_error_from_errno("localtime_r");
1230 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1231 >= sizeof(datebuf))
1232 return got_error(GOT_ERR_NO_SPACE);
1234 if (avail <= date_display_cols)
1235 limit = MIN(sizeof(datebuf) - 1, avail);
1236 else
1237 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1238 tc = get_color(colors, TOG_COLOR_DATE);
1239 if (tc)
1240 wattr_on(view->window,
1241 COLOR_PAIR(tc->colorpair), NULL);
1242 waddnstr(view->window, datebuf, limit);
1243 if (tc)
1244 wattr_off(view->window,
1245 COLOR_PAIR(tc->colorpair), NULL);
1246 col = limit;
1247 if (col > avail)
1248 goto done;
1250 if (avail >= 120) {
1251 char *id_str;
1252 err = got_object_id_str(&id_str, id);
1253 if (err)
1254 goto done;
1255 tc = get_color(colors, TOG_COLOR_COMMIT);
1256 if (tc)
1257 wattr_on(view->window,
1258 COLOR_PAIR(tc->colorpair), NULL);
1259 wprintw(view->window, "%.8s ", id_str);
1260 if (tc)
1261 wattr_off(view->window,
1262 COLOR_PAIR(tc->colorpair), NULL);
1263 free(id_str);
1264 col += 9;
1265 if (col > avail)
1266 goto done;
1269 author = strdup(got_object_commit_get_author(commit));
1270 if (author == NULL) {
1271 err = got_error_from_errno("strdup");
1272 goto done;
1274 err = format_author(&wauthor, &author_width, author, avail - col, col);
1275 if (err)
1276 goto done;
1277 tc = get_color(colors, TOG_COLOR_AUTHOR);
1278 if (tc)
1279 wattr_on(view->window,
1280 COLOR_PAIR(tc->colorpair), NULL);
1281 waddwstr(view->window, wauthor);
1282 if (tc)
1283 wattr_off(view->window,
1284 COLOR_PAIR(tc->colorpair), NULL);
1285 col += author_width;
1286 while (col < avail && author_width < author_display_cols + 2) {
1287 waddch(view->window, ' ');
1288 col++;
1289 author_width++;
1291 if (col > avail)
1292 goto done;
1294 err = got_object_commit_get_logmsg(&logmsg0, commit);
1295 if (err)
1296 goto done;
1297 logmsg = logmsg0;
1298 while (*logmsg == '\n')
1299 logmsg++;
1300 newline = strchr(logmsg, '\n');
1301 if (newline)
1302 *newline = '\0';
1303 limit = avail - col;
1304 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1305 if (err)
1306 goto done;
1307 waddwstr(view->window, wlogmsg);
1308 col += logmsg_width;
1309 while (col < avail) {
1310 waddch(view->window, ' ');
1311 col++;
1313 done:
1314 free(logmsg0);
1315 free(wlogmsg);
1316 free(author);
1317 free(wauthor);
1318 free(line);
1319 return err;
1322 static struct commit_queue_entry *
1323 alloc_commit_queue_entry(struct got_commit_object *commit,
1324 struct got_object_id *id)
1326 struct commit_queue_entry *entry;
1328 entry = calloc(1, sizeof(*entry));
1329 if (entry == NULL)
1330 return NULL;
1332 entry->id = id;
1333 entry->commit = commit;
1334 return entry;
1337 static void
1338 pop_commit(struct commit_queue *commits)
1340 struct commit_queue_entry *entry;
1342 entry = TAILQ_FIRST(&commits->head);
1343 TAILQ_REMOVE(&commits->head, entry, entry);
1344 got_object_commit_close(entry->commit);
1345 commits->ncommits--;
1346 /* Don't free entry->id! It is owned by the commit graph. */
1347 free(entry);
1350 static void
1351 free_commits(struct commit_queue *commits)
1353 while (!TAILQ_EMPTY(&commits->head))
1354 pop_commit(commits);
1357 static const struct got_error *
1358 match_commit(int *have_match, struct got_object_id *id,
1359 struct got_commit_object *commit, regex_t *regex)
1361 const struct got_error *err = NULL;
1362 regmatch_t regmatch;
1363 char *id_str = NULL, *logmsg = NULL;
1365 *have_match = 0;
1367 err = got_object_id_str(&id_str, id);
1368 if (err)
1369 return err;
1371 err = got_object_commit_get_logmsg(&logmsg, commit);
1372 if (err)
1373 goto done;
1375 if (regexec(regex, got_object_commit_get_author(commit), 1,
1376 &regmatch, 0) == 0 ||
1377 regexec(regex, got_object_commit_get_committer(commit), 1,
1378 &regmatch, 0) == 0 ||
1379 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1380 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1381 *have_match = 1;
1382 done:
1383 free(id_str);
1384 free(logmsg);
1385 return err;
1388 static const struct got_error *
1389 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1390 int minqueue, struct got_repository *repo, const char *path,
1391 int *searching, int *search_next_done, regex_t *regex)
1393 const struct got_error *err = NULL;
1394 int nqueued = 0, have_match = 0;
1397 * We keep all commits open throughout the lifetime of the log
1398 * view in order to avoid having to re-fetch commits from disk
1399 * while updating the display.
1401 while (nqueued < minqueue ||
1402 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1403 struct got_object_id *id;
1404 struct got_commit_object *commit;
1405 struct commit_queue_entry *entry;
1406 int errcode;
1408 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1409 if (err || id == NULL)
1410 break;
1412 err = got_object_open_as_commit(&commit, repo, id);
1413 if (err)
1414 break;
1415 entry = alloc_commit_queue_entry(commit, id);
1416 if (entry == NULL) {
1417 err = got_error_from_errno("alloc_commit_queue_entry");
1418 break;
1421 errcode = pthread_mutex_lock(&tog_mutex);
1422 if (errcode) {
1423 err = got_error_set_errno(errcode,
1424 "pthread_mutex_lock");
1425 break;
1428 entry->idx = commits->ncommits;
1429 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1430 nqueued++;
1431 commits->ncommits++;
1433 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1434 err = match_commit(&have_match, id, commit, regex);
1437 errcode = pthread_mutex_unlock(&tog_mutex);
1438 if (errcode && err == NULL)
1439 err = got_error_set_errno(errcode,
1440 "pthread_mutex_unlock");
1442 if (err || have_match)
1443 break;
1446 return err;
1449 static const struct got_error *
1450 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1451 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1452 struct commit_queue *commits, int selected_idx, int limit,
1453 struct got_reflist_head *refs, const char *path, int commits_needed,
1454 struct tog_colors *colors)
1456 const struct got_error *err = NULL;
1457 struct tog_log_view_state *s = &view->state.log;
1458 struct commit_queue_entry *entry;
1459 int width;
1460 int ncommits, author_cols = 4;
1461 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1462 char *refs_str = NULL;
1463 wchar_t *wline;
1464 struct tog_color *tc;
1465 static const size_t date_display_cols = 12;
1467 entry = first;
1468 ncommits = 0;
1469 while (entry) {
1470 if (ncommits == selected_idx) {
1471 *selected = entry;
1472 break;
1474 entry = TAILQ_NEXT(entry, entry);
1475 ncommits++;
1478 if (*selected && !(view->searching && view->search_next_done == 0)) {
1479 err = got_object_id_str(&id_str, (*selected)->id);
1480 if (err)
1481 return err;
1482 if (refs) {
1483 err = build_refs_str(&refs_str, refs, (*selected)->id,
1484 s->repo);
1485 if (err)
1486 goto done;
1490 if (commits_needed == 0)
1491 halfdelay(10); /* disable fast refresh */
1493 if (asprintf(&ncommits_str, " [%d/%d] %s",
1494 entry ? entry->idx + 1 : 0, commits->ncommits,
1495 commits_needed > 0 ?
1496 (view->searching && view->search_next_done == 0
1497 ? "searching..." : "loading... ") :
1498 (refs_str ? refs_str : "")) == -1) {
1499 err = got_error_from_errno("asprintf");
1500 goto done;
1503 if (path && strcmp(path, "/") != 0) {
1504 if (asprintf(&header, "commit %s %s%s",
1505 id_str ? id_str : "........................................",
1506 path, ncommits_str) == -1) {
1507 err = got_error_from_errno("asprintf");
1508 header = NULL;
1509 goto done;
1511 } else if (asprintf(&header, "commit %s%s",
1512 id_str ? id_str : "........................................",
1513 ncommits_str) == -1) {
1514 err = got_error_from_errno("asprintf");
1515 header = NULL;
1516 goto done;
1518 err = format_line(&wline, &width, header, view->ncols, 0);
1519 if (err)
1520 goto done;
1522 werase(view->window);
1524 if (view_needs_focus_indication(view))
1525 wstandout(view->window);
1526 tc = get_color(colors, TOG_COLOR_COMMIT);
1527 if (tc)
1528 wattr_on(view->window,
1529 COLOR_PAIR(tc->colorpair), NULL);
1530 waddwstr(view->window, wline);
1531 if (tc)
1532 wattr_off(view->window,
1533 COLOR_PAIR(tc->colorpair), NULL);
1534 while (width < view->ncols) {
1535 waddch(view->window, ' ');
1536 width++;
1538 if (view_needs_focus_indication(view))
1539 wstandend(view->window);
1540 free(wline);
1541 if (limit <= 1)
1542 goto done;
1544 /* Grow author column size if necessary. */
1545 entry = first;
1546 ncommits = 0;
1547 while (entry) {
1548 char *author;
1549 wchar_t *wauthor;
1550 int width;
1551 if (ncommits >= limit - 1)
1552 break;
1553 author = strdup(got_object_commit_get_author(entry->commit));
1554 if (author == NULL) {
1555 err = got_error_from_errno("strdup");
1556 goto done;
1558 err = format_author(&wauthor, &width, author, COLS,
1559 date_display_cols);
1560 if (author_cols < width)
1561 author_cols = width;
1562 free(wauthor);
1563 free(author);
1564 ncommits++;
1565 entry = TAILQ_NEXT(entry, entry);
1568 entry = first;
1569 *last = first;
1570 ncommits = 0;
1571 while (entry) {
1572 if (ncommits >= limit - 1)
1573 break;
1574 if (ncommits == selected_idx)
1575 wstandout(view->window);
1576 err = draw_commit(view, entry->commit, entry->id, refs,
1577 date_display_cols, author_cols, colors);
1578 if (ncommits == selected_idx)
1579 wstandend(view->window);
1580 if (err)
1581 goto done;
1582 ncommits++;
1583 *last = entry;
1584 entry = TAILQ_NEXT(entry, entry);
1587 view_vborder(view);
1588 done:
1589 free(id_str);
1590 free(refs_str);
1591 free(ncommits_str);
1592 free(header);
1593 return err;
1596 static void
1597 scroll_up(struct tog_view *view,
1598 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1599 struct commit_queue *commits)
1601 struct commit_queue_entry *entry;
1602 int nscrolled = 0;
1604 entry = TAILQ_FIRST(&commits->head);
1605 if (*first_displayed_entry == entry)
1606 return;
1608 entry = *first_displayed_entry;
1609 while (entry && nscrolled < maxscroll) {
1610 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1611 if (entry) {
1612 *first_displayed_entry = entry;
1613 nscrolled++;
1618 static const struct got_error *
1619 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1620 pthread_cond_t *need_commits)
1622 int errcode;
1623 int max_wait = 20;
1625 halfdelay(1); /* fast refresh while loading commits */
1627 while (*commits_needed > 0) {
1628 if (*log_complete)
1629 break;
1631 /* Wake the log thread. */
1632 errcode = pthread_cond_signal(need_commits);
1633 if (errcode)
1634 return got_error_set_errno(errcode,
1635 "pthread_cond_signal");
1636 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1638 * Thread is not done yet; lose a key press
1639 * and let the user retry... this way the GUI
1640 * remains interactive while logging deep paths
1641 * with few commits in history.
1643 return NULL;
1647 return NULL;
1650 static const struct got_error *
1651 scroll_down(struct tog_view *view,
1652 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1653 struct commit_queue_entry **last_displayed_entry,
1654 struct commit_queue *commits, int *log_complete, int *commits_needed,
1655 pthread_cond_t *need_commits)
1657 const struct got_error *err = NULL;
1658 struct commit_queue_entry *pentry;
1659 int nscrolled = 0;
1661 if (*last_displayed_entry == NULL)
1662 return NULL;
1664 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1665 if (pentry == NULL && !*log_complete) {
1667 * Ask the log thread for required amount of commits
1668 * plus some amount of pre-fetching.
1670 (*commits_needed) += maxscroll + 20;
1671 err = trigger_log_thread(0, commits_needed, log_complete,
1672 need_commits);
1673 if (err)
1674 return err;
1677 do {
1678 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1679 if (pentry == NULL)
1680 break;
1682 *last_displayed_entry = pentry;
1684 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1685 if (pentry == NULL)
1686 break;
1687 *first_displayed_entry = pentry;
1688 } while (++nscrolled < maxscroll);
1690 return err;
1693 static const struct got_error *
1694 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1695 struct got_commit_object *commit, struct got_object_id *commit_id,
1696 struct tog_view *log_view, struct got_reflist_head *refs,
1697 struct got_repository *repo)
1699 const struct got_error *err;
1700 struct got_object_qid *parent_id;
1701 struct tog_view *diff_view;
1703 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1704 if (diff_view == NULL)
1705 return got_error_from_errno("view_open");
1707 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1708 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1709 commit_id, log_view, refs, repo);
1710 if (err == NULL)
1711 *new_view = diff_view;
1712 return err;
1715 static const struct got_error *
1716 tree_view_visit_subtree(struct got_tree_object *subtree,
1717 struct tog_tree_view_state *s)
1719 struct tog_parent_tree *parent;
1721 parent = calloc(1, sizeof(*parent));
1722 if (parent == NULL)
1723 return got_error_from_errno("calloc");
1725 parent->tree = s->tree;
1726 parent->first_displayed_entry = s->first_displayed_entry;
1727 parent->selected_entry = s->selected_entry;
1728 parent->selected = s->selected;
1729 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1730 s->tree = subtree;
1731 s->selected = 0;
1732 s->first_displayed_entry = NULL;
1733 return NULL;
1737 static const struct got_error *
1738 browse_commit_tree(struct tog_view **new_view, int begin_x,
1739 struct commit_queue_entry *entry, const char *path,
1740 struct got_reflist_head *refs, struct got_repository *repo)
1742 const struct got_error *err = NULL;
1743 struct got_tree_object *tree;
1744 struct tog_tree_view_state *s;
1745 struct tog_view *tree_view;
1746 char *slash, *subpath = NULL;
1747 const char *p;
1749 err = got_object_open_as_tree(&tree, repo,
1750 got_object_commit_get_tree_id(entry->commit));
1751 if (err)
1752 return err;
1754 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1755 if (tree_view == NULL)
1756 return got_error_from_errno("view_open");
1758 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1759 if (err) {
1760 got_object_tree_close(tree);
1761 return err;
1763 s = &tree_view->state.tree;
1765 *new_view = tree_view;
1767 if (got_path_is_root_dir(path))
1768 return NULL;
1770 /* Walk the path and open corresponding tree objects. */
1771 p = path;
1772 while (*p) {
1773 struct got_tree_entry *te;
1774 struct got_object_id *tree_id;
1775 char *te_name;
1777 while (p[0] == '/')
1778 p++;
1780 /* Ensure the correct subtree entry is selected. */
1781 slash = strchr(p, '/');
1782 if (slash == NULL)
1783 te_name = strdup(p);
1784 else
1785 te_name = strndup(p, slash - p);
1786 if (te_name == NULL) {
1787 err = got_error_from_errno("strndup");
1788 break;
1790 te = got_object_tree_find_entry(s->tree, te_name);
1791 if (te == NULL) {
1792 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1793 free(te_name);
1794 break;
1796 free(te_name);
1797 s->selected_entry = te;
1798 s->selected = got_tree_entry_get_index(te);
1799 if (s->tree != s->root)
1800 s->selected++; /* skip '..' */
1802 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1803 /* Jump to this file's entry. */
1804 s->first_displayed_entry = s->selected_entry;
1805 s->selected = 0;
1806 break;
1809 slash = strchr(p, '/');
1810 if (slash)
1811 subpath = strndup(path, slash - path);
1812 else
1813 subpath = strdup(path);
1814 if (subpath == NULL) {
1815 err = got_error_from_errno("strdup");
1816 break;
1819 err = got_object_id_by_path(&tree_id, repo, entry->id,
1820 subpath);
1821 if (err)
1822 break;
1824 err = got_object_open_as_tree(&tree, repo, tree_id);
1825 free(tree_id);
1826 if (err)
1827 break;
1829 err = tree_view_visit_subtree(tree, s);
1830 if (err) {
1831 got_object_tree_close(tree);
1832 break;
1834 if (slash == NULL)
1835 break;
1836 free(subpath);
1837 subpath = NULL;
1838 p = slash;
1841 free(subpath);
1842 return err;
1845 static const struct got_error *
1846 block_signals_used_by_main_thread(void)
1848 sigset_t sigset;
1849 int errcode;
1851 if (sigemptyset(&sigset) == -1)
1852 return got_error_from_errno("sigemptyset");
1854 /* tog handles SIGWINCH and SIGCONT */
1855 if (sigaddset(&sigset, SIGWINCH) == -1)
1856 return got_error_from_errno("sigaddset");
1857 if (sigaddset(&sigset, SIGCONT) == -1)
1858 return got_error_from_errno("sigaddset");
1860 /* ncurses handles SIGTSTP */
1861 if (sigaddset(&sigset, SIGTSTP) == -1)
1862 return got_error_from_errno("sigaddset");
1864 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1865 if (errcode)
1866 return got_error_set_errno(errcode, "pthread_sigmask");
1868 return NULL;
1871 static void *
1872 log_thread(void *arg)
1874 const struct got_error *err = NULL;
1875 int errcode = 0;
1876 struct tog_log_thread_args *a = arg;
1877 int done = 0;
1879 err = block_signals_used_by_main_thread();
1880 if (err)
1881 return (void *)err;
1883 while (!done && !err && !tog_sigpipe_received) {
1884 err = queue_commits(a->graph, a->commits, 1, a->repo,
1885 a->in_repo_path, a->searching, a->search_next_done,
1886 a->regex);
1887 if (err) {
1888 if (err->code != GOT_ERR_ITER_COMPLETED)
1889 return (void *)err;
1890 err = NULL;
1891 done = 1;
1892 } else if (a->commits_needed > 0)
1893 a->commits_needed--;
1895 errcode = pthread_mutex_lock(&tog_mutex);
1896 if (errcode) {
1897 err = got_error_set_errno(errcode,
1898 "pthread_mutex_lock");
1899 break;
1900 } else if (*a->quit)
1901 done = 1;
1902 else if (*a->first_displayed_entry == NULL) {
1903 *a->first_displayed_entry =
1904 TAILQ_FIRST(&a->commits->head);
1905 *a->selected_entry = *a->first_displayed_entry;
1908 if (done)
1909 a->commits_needed = 0;
1910 else if (a->commits_needed == 0) {
1911 errcode = pthread_cond_wait(&a->need_commits,
1912 &tog_mutex);
1913 if (errcode)
1914 err = got_error_set_errno(errcode,
1915 "pthread_cond_wait");
1918 errcode = pthread_mutex_unlock(&tog_mutex);
1919 if (errcode && err == NULL)
1920 err = got_error_set_errno(errcode,
1921 "pthread_mutex_unlock");
1923 a->log_complete = 1;
1924 return (void *)err;
1927 static const struct got_error *
1928 stop_log_thread(struct tog_log_view_state *s)
1930 const struct got_error *err = NULL;
1931 int errcode;
1933 if (s->thread) {
1934 s->quit = 1;
1935 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1936 if (errcode)
1937 return got_error_set_errno(errcode,
1938 "pthread_cond_signal");
1939 errcode = pthread_mutex_unlock(&tog_mutex);
1940 if (errcode)
1941 return got_error_set_errno(errcode,
1942 "pthread_mutex_unlock");
1943 errcode = pthread_join(s->thread, (void **)&err);
1944 if (errcode)
1945 return got_error_set_errno(errcode, "pthread_join");
1946 errcode = pthread_mutex_lock(&tog_mutex);
1947 if (errcode)
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_lock");
1950 s->thread = NULL;
1953 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1954 if (errcode && err == NULL)
1955 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1957 if (s->thread_args.repo) {
1958 got_repo_close(s->thread_args.repo);
1959 s->thread_args.repo = NULL;
1962 if (s->thread_args.graph) {
1963 got_commit_graph_close(s->thread_args.graph);
1964 s->thread_args.graph = NULL;
1967 return err;
1970 static const struct got_error *
1971 close_log_view(struct tog_view *view)
1973 const struct got_error *err = NULL;
1974 struct tog_log_view_state *s = &view->state.log;
1976 err = stop_log_thread(s);
1977 free_commits(&s->commits);
1978 free(s->in_repo_path);
1979 s->in_repo_path = NULL;
1980 free(s->start_id);
1981 s->start_id = NULL;
1982 return err;
1985 static const struct got_error *
1986 search_start_log_view(struct tog_view *view)
1988 struct tog_log_view_state *s = &view->state.log;
1990 s->matched_entry = NULL;
1991 s->search_entry = NULL;
1992 return NULL;
1995 static const struct got_error *
1996 search_next_log_view(struct tog_view *view)
1998 const struct got_error *err = NULL;
1999 struct tog_log_view_state *s = &view->state.log;
2000 struct commit_queue_entry *entry;
2002 if (!view->searching) {
2003 view->search_next_done = 1;
2004 return NULL;
2007 if (s->search_entry) {
2008 int errcode, ch;
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2010 if (errcode)
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 ch = wgetch(view->window);
2014 errcode = pthread_mutex_lock(&tog_mutex);
2015 if (errcode)
2016 return got_error_set_errno(errcode,
2017 "pthread_mutex_lock");
2018 if (ch == KEY_BACKSPACE) {
2019 view->search_next_done = 1;
2020 return NULL;
2022 if (view->searching == TOG_SEARCH_FORWARD)
2023 entry = TAILQ_NEXT(s->search_entry, entry);
2024 else
2025 entry = TAILQ_PREV(s->search_entry,
2026 commit_queue_head, entry);
2027 } else if (s->matched_entry) {
2028 if (view->searching == TOG_SEARCH_FORWARD)
2029 entry = TAILQ_NEXT(s->selected_entry, entry);
2030 else
2031 entry = TAILQ_PREV(s->selected_entry,
2032 commit_queue_head, entry);
2033 } else {
2034 if (view->searching == TOG_SEARCH_FORWARD)
2035 entry = TAILQ_FIRST(&s->commits.head);
2036 else
2037 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2040 while (1) {
2041 int have_match = 0;
2043 if (entry == NULL) {
2044 if (s->thread_args.log_complete ||
2045 view->searching == TOG_SEARCH_BACKWARD) {
2046 view->search_next_done = 1;
2047 return NULL;
2050 * Poke the log thread for more commits and return,
2051 * allowing the main loop to make progress. Search
2052 * will resume at s->search_entry once we come back.
2054 s->thread_args.commits_needed++;
2055 return trigger_log_thread(1,
2056 &s->thread_args.commits_needed,
2057 &s->thread_args.log_complete,
2058 &s->thread_args.need_commits);
2061 err = match_commit(&have_match, entry->id, entry->commit,
2062 &view->regex);
2063 if (err)
2064 break;
2065 if (have_match) {
2066 view->search_next_done = 1;
2067 s->matched_entry = entry;
2068 break;
2071 s->search_entry = entry;
2072 if (view->searching == TOG_SEARCH_FORWARD)
2073 entry = TAILQ_NEXT(entry, entry);
2074 else
2075 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2078 if (s->matched_entry) {
2079 int cur = s->selected_entry->idx;
2080 while (cur < s->matched_entry->idx) {
2081 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2082 if (err)
2083 return err;
2084 cur++;
2086 while (cur > s->matched_entry->idx) {
2087 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2088 if (err)
2089 return err;
2090 cur--;
2094 s->search_entry = NULL;
2096 return NULL;
2099 static const struct got_error *
2100 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2101 struct got_reflist_head *refs, struct got_repository *repo,
2102 const char *head_ref_name, const char *path, int check_disk,
2103 int log_branches)
2105 const struct got_error *err = NULL;
2106 struct tog_log_view_state *s = &view->state.log;
2107 struct got_repository *thread_repo = NULL;
2108 struct got_commit_graph *thread_graph = NULL;
2109 int errcode;
2111 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2112 if (err != NULL)
2113 goto done;
2115 /* The commit queue only contains commits being displayed. */
2116 TAILQ_INIT(&s->commits.head);
2117 s->commits.ncommits = 0;
2119 s->refs = refs;
2120 s->repo = repo;
2121 s->head_ref_name = head_ref_name;
2122 s->start_id = got_object_id_dup(start_id);
2123 if (s->start_id == NULL) {
2124 err = got_error_from_errno("got_object_id_dup");
2125 goto done;
2127 s->log_branches = log_branches;
2129 SIMPLEQ_INIT(&s->colors);
2130 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2131 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2132 get_color_value("TOG_COLOR_COMMIT"));
2133 if (err)
2134 goto done;
2135 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2136 get_color_value("TOG_COLOR_AUTHOR"));
2137 if (err) {
2138 free_colors(&s->colors);
2139 goto done;
2141 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2142 get_color_value("TOG_COLOR_DATE"));
2143 if (err) {
2144 free_colors(&s->colors);
2145 goto done;
2149 view->show = show_log_view;
2150 view->input = input_log_view;
2151 view->close = close_log_view;
2152 view->search_start = search_start_log_view;
2153 view->search_next = search_next_log_view;
2155 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2156 if (err)
2157 goto done;
2158 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2159 !s->log_branches);
2160 if (err)
2161 goto done;
2162 err = got_commit_graph_iter_start(thread_graph,
2163 s->start_id, s->repo, NULL, NULL);
2164 if (err)
2165 goto done;
2167 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2168 if (errcode) {
2169 err = got_error_set_errno(errcode, "pthread_cond_init");
2170 goto done;
2173 s->thread_args.commits_needed = view->nlines;
2174 s->thread_args.graph = thread_graph;
2175 s->thread_args.commits = &s->commits;
2176 s->thread_args.in_repo_path = s->in_repo_path;
2177 s->thread_args.start_id = s->start_id;
2178 s->thread_args.repo = thread_repo;
2179 s->thread_args.log_complete = 0;
2180 s->thread_args.quit = &s->quit;
2181 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2182 s->thread_args.selected_entry = &s->selected_entry;
2183 s->thread_args.searching = &view->searching;
2184 s->thread_args.search_next_done = &view->search_next_done;
2185 s->thread_args.regex = &view->regex;
2186 done:
2187 if (err)
2188 close_log_view(view);
2189 return err;
2192 static const struct got_error *
2193 show_log_view(struct tog_view *view)
2195 struct tog_log_view_state *s = &view->state.log;
2197 if (s->thread == NULL) {
2198 int errcode = pthread_create(&s->thread, NULL, log_thread,
2199 &s->thread_args);
2200 if (errcode)
2201 return got_error_set_errno(errcode, "pthread_create");
2204 return draw_commits(view, &s->last_displayed_entry,
2205 &s->selected_entry, s->first_displayed_entry,
2206 &s->commits, s->selected, view->nlines, s->refs,
2207 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2210 static const struct got_error *
2211 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2212 struct tog_view **focus_view, struct tog_view *view, int ch)
2214 const struct got_error *err = NULL;
2215 struct tog_log_view_state *s = &view->state.log;
2216 char *parent_path, *in_repo_path = NULL;
2217 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2218 int begin_x = 0;
2219 struct got_object_id *start_id;
2221 switch (ch) {
2222 case 'q':
2223 s->quit = 1;
2224 break;
2225 case 'k':
2226 case KEY_UP:
2227 case '<':
2228 case ',':
2229 if (s->first_displayed_entry == NULL)
2230 break;
2231 if (s->selected > 0)
2232 s->selected--;
2233 else
2234 scroll_up(view, &s->first_displayed_entry, 1,
2235 &s->commits);
2236 break;
2237 case KEY_PPAGE:
2238 case CTRL('b'):
2239 if (s->first_displayed_entry == NULL)
2240 break;
2241 if (TAILQ_FIRST(&s->commits.head) ==
2242 s->first_displayed_entry) {
2243 s->selected = 0;
2244 break;
2246 scroll_up(view, &s->first_displayed_entry,
2247 view->nlines, &s->commits);
2248 break;
2249 case 'j':
2250 case KEY_DOWN:
2251 case '>':
2252 case '.':
2253 if (s->first_displayed_entry == NULL)
2254 break;
2255 if (s->selected < MIN(view->nlines - 2,
2256 s->commits.ncommits - 1)) {
2257 s->selected++;
2258 break;
2260 err = scroll_down(view, &s->first_displayed_entry, 1,
2261 &s->last_displayed_entry, &s->commits,
2262 &s->thread_args.log_complete,
2263 &s->thread_args.commits_needed,
2264 &s->thread_args.need_commits);
2265 break;
2266 case KEY_NPAGE:
2267 case CTRL('f'): {
2268 struct commit_queue_entry *first;
2269 first = s->first_displayed_entry;
2270 if (first == NULL)
2271 break;
2272 err = scroll_down(view, &s->first_displayed_entry,
2273 view->nlines, &s->last_displayed_entry,
2274 &s->commits, &s->thread_args.log_complete,
2275 &s->thread_args.commits_needed,
2276 &s->thread_args.need_commits);
2277 if (err)
2278 break;
2279 if (first == s->first_displayed_entry &&
2280 s->selected < MIN(view->nlines - 2,
2281 s->commits.ncommits - 1)) {
2282 /* can't scroll further down */
2283 s->selected = MIN(view->nlines - 2,
2284 s->commits.ncommits - 1);
2286 err = NULL;
2287 break;
2289 case KEY_RESIZE:
2290 if (s->selected > view->nlines - 2)
2291 s->selected = view->nlines - 2;
2292 if (s->selected > s->commits.ncommits - 1)
2293 s->selected = s->commits.ncommits - 1;
2294 break;
2295 case KEY_ENTER:
2296 case ' ':
2297 case '\r':
2298 if (s->selected_entry == NULL)
2299 break;
2300 if (view_is_parent_view(view))
2301 begin_x = view_split_begin_x(view->begin_x);
2302 err = open_diff_view_for_commit(&diff_view, begin_x,
2303 s->selected_entry->commit, s->selected_entry->id,
2304 view, s->refs, s->repo);
2305 if (err)
2306 break;
2307 if (view_is_parent_view(view)) {
2308 err = view_close_child(view);
2309 if (err)
2310 return err;
2311 err = view_set_child(view, diff_view);
2312 if (err) {
2313 view_close(diff_view);
2314 break;
2316 *focus_view = diff_view;
2317 view->child_focussed = 1;
2318 } else
2319 *new_view = diff_view;
2320 break;
2321 case 't':
2322 if (s->selected_entry == NULL)
2323 break;
2324 if (view_is_parent_view(view))
2325 begin_x = view_split_begin_x(view->begin_x);
2326 err = browse_commit_tree(&tree_view, begin_x,
2327 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2328 if (err)
2329 break;
2330 if (view_is_parent_view(view)) {
2331 err = view_close_child(view);
2332 if (err)
2333 return err;
2334 err = view_set_child(view, tree_view);
2335 if (err) {
2336 view_close(tree_view);
2337 break;
2339 *focus_view = tree_view;
2340 view->child_focussed = 1;
2341 } else
2342 *new_view = tree_view;
2343 break;
2344 case KEY_BACKSPACE:
2345 if (strcmp(s->in_repo_path, "/") == 0)
2346 break;
2347 parent_path = dirname(s->in_repo_path);
2348 if (parent_path && strcmp(parent_path, ".") != 0) {
2349 err = stop_log_thread(s);
2350 if (err)
2351 return err;
2352 lv = view_open(view->nlines, view->ncols,
2353 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2354 if (lv == NULL)
2355 return got_error_from_errno(
2356 "view_open");
2357 err = open_log_view(lv, s->start_id, s->refs,
2358 s->repo, s->head_ref_name, parent_path, 0,
2359 s->log_branches);
2360 if (err)
2361 return err;;
2362 if (view_is_parent_view(view))
2363 *new_view = lv;
2364 else {
2365 view_set_child(view->parent, lv);
2366 *focus_view = lv;
2368 return NULL;
2370 break;
2371 case CTRL('l'):
2372 err = stop_log_thread(s);
2373 if (err)
2374 return err;
2375 lv = view_open(view->nlines, view->ncols,
2376 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2377 if (lv == NULL)
2378 return got_error_from_errno("view_open");
2379 err = got_repo_match_object_id(&start_id, NULL,
2380 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2381 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2382 if (err) {
2383 view_close(lv);
2384 return err;
2386 in_repo_path = strdup(s->in_repo_path);
2387 if (in_repo_path == NULL) {
2388 free(start_id);
2389 view_close(lv);
2390 return got_error_from_errno("strdup");
2392 got_ref_list_free(s->refs);
2393 err = got_ref_list(s->refs, s->repo, NULL,
2394 got_ref_cmp_by_name, NULL);
2395 if (err) {
2396 free(start_id);
2397 view_close(lv);
2398 return err;
2400 err = open_log_view(lv, start_id, s->refs, s->repo,
2401 s->head_ref_name, in_repo_path, 0, s->log_branches);
2402 if (err) {
2403 free(start_id);
2404 view_close(lv);
2405 return err;;
2407 *dead_view = view;
2408 *new_view = lv;
2409 break;
2410 case 'B':
2411 s->log_branches = !s->log_branches;
2412 err = stop_log_thread(s);
2413 if (err)
2414 return err;
2415 lv = view_open(view->nlines, view->ncols,
2416 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2417 if (lv == NULL)
2418 return got_error_from_errno("view_open");
2419 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2420 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2421 if (err) {
2422 view_close(lv);
2423 return err;;
2425 *dead_view = view;
2426 *new_view = lv;
2427 break;
2428 default:
2429 break;
2432 return err;
2435 static const struct got_error *
2436 apply_unveil(const char *repo_path, const char *worktree_path)
2438 const struct got_error *error;
2440 #ifdef PROFILE
2441 if (unveil("gmon.out", "rwc") != 0)
2442 return got_error_from_errno2("unveil", "gmon.out");
2443 #endif
2444 if (repo_path && unveil(repo_path, "r") != 0)
2445 return got_error_from_errno2("unveil", repo_path);
2447 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2448 return got_error_from_errno2("unveil", worktree_path);
2450 if (unveil("/tmp", "rwc") != 0)
2451 return got_error_from_errno2("unveil", "/tmp");
2453 error = got_privsep_unveil_exec_helpers();
2454 if (error != NULL)
2455 return error;
2457 if (unveil(NULL, NULL) != 0)
2458 return got_error_from_errno("unveil");
2460 return NULL;
2463 static void
2464 init_curses(void)
2466 initscr();
2467 cbreak();
2468 halfdelay(1); /* Do fast refresh while initial view is loading. */
2469 noecho();
2470 nonl();
2471 intrflush(stdscr, FALSE);
2472 keypad(stdscr, TRUE);
2473 curs_set(0);
2474 if (getenv("TOG_COLORS") != NULL) {
2475 start_color();
2476 use_default_colors();
2478 signal(SIGWINCH, tog_sigwinch);
2479 signal(SIGPIPE, tog_sigpipe);
2480 signal(SIGCONT, tog_sigcont);
2483 static const struct got_error *
2484 cmd_log(int argc, char *argv[])
2486 const struct got_error *error;
2487 struct got_repository *repo = NULL;
2488 struct got_worktree *worktree = NULL;
2489 struct got_reflist_head refs;
2490 struct got_object_id *start_id = NULL;
2491 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2492 char *start_commit = NULL, *head_ref_name = NULL;
2493 int ch, log_branches = 0, check_disk = 1;
2494 struct tog_view *view;
2496 SIMPLEQ_INIT(&refs);
2498 #ifndef PROFILE
2499 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2500 NULL) == -1)
2501 err(1, "pledge");
2502 #endif
2504 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2505 switch (ch) {
2506 case 'b':
2507 log_branches = 1;
2508 break;
2509 case 'c':
2510 start_commit = optarg;
2511 break;
2512 case 'r':
2513 repo_path = realpath(optarg, NULL);
2514 if (repo_path == NULL)
2515 return got_error_from_errno2("realpath",
2516 optarg);
2517 break;
2518 default:
2519 usage_log();
2520 /* NOTREACHED */
2524 argc -= optind;
2525 argv += optind;
2527 cwd = getcwd(NULL, 0);
2528 if (cwd == NULL) {
2529 error = got_error_from_errno("getcwd");
2530 goto done;
2532 error = got_worktree_open(&worktree, cwd);
2533 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2534 goto done;
2535 error = NULL;
2537 if (argc == 0) {
2538 path = strdup("");
2539 if (path == NULL) {
2540 error = got_error_from_errno("strdup");
2541 goto done;
2543 } else if (argc == 1) {
2544 if (worktree) {
2545 error = got_worktree_resolve_path(&path, worktree,
2546 argv[0]);
2547 if (error)
2548 goto done;
2549 } else {
2550 path = strdup(argv[0]);
2551 if (path == NULL) {
2552 error = got_error_from_errno("strdup");
2553 goto done;
2556 } else
2557 usage_log();
2559 if (repo_path == NULL) {
2560 if (worktree)
2561 repo_path = strdup(
2562 got_worktree_get_repo_path(worktree));
2563 else
2564 repo_path = strdup(cwd);
2566 if (repo_path == NULL) {
2567 error = got_error_from_errno("strdup");
2568 goto done;
2571 init_curses();
2573 error = got_repo_open(&repo, repo_path, NULL);
2574 if (error != NULL)
2575 goto done;
2577 error = apply_unveil(got_repo_get_path(repo),
2578 worktree ? got_worktree_get_root_path(worktree) : NULL);
2579 if (error)
2580 goto done;
2582 if (start_commit == NULL)
2583 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2584 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2585 GOT_OBJ_TYPE_COMMIT, 1, repo);
2586 else
2587 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2588 GOT_OBJ_TYPE_COMMIT, 1, repo);
2589 if (error != NULL)
2590 goto done;
2592 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2593 if (error)
2594 goto done;
2596 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2597 if (view == NULL) {
2598 error = got_error_from_errno("view_open");
2599 goto done;
2601 if (worktree) {
2602 const char *prefix = got_worktree_get_path_prefix(worktree);
2603 char *p;
2604 if (asprintf(&p, "%s%s%s", prefix,
2605 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2606 error = got_error_from_errno("asprintf");
2607 goto done;
2609 free(path);
2610 path = p;
2611 check_disk = 0;
2613 head_ref_name = strdup(
2614 got_worktree_get_head_ref_name(worktree));
2615 if (head_ref_name == NULL) {
2616 error = got_error_from_errno("strdup");
2617 goto done;
2620 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2621 path, check_disk, log_branches);
2622 if (error)
2623 goto done;
2624 if (worktree) {
2625 /* Release work tree lock. */
2626 got_worktree_close(worktree);
2627 worktree = NULL;
2629 error = view_loop(view);
2630 done:
2631 free(repo_path);
2632 free(cwd);
2633 free(path);
2634 free(start_id);
2635 free(head_ref_name);
2636 if (repo)
2637 got_repo_close(repo);
2638 if (worktree)
2639 got_worktree_close(worktree);
2640 got_ref_list_free(&refs);
2641 return error;
2644 __dead static void
2645 usage_diff(void)
2647 endwin();
2648 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2649 getprogname());
2650 exit(1);
2653 static char *
2654 parse_next_line(FILE *f, size_t *len)
2656 char *line;
2657 size_t linelen;
2658 size_t lineno;
2659 const char delim[3] = { '\0', '\0', '\0'};
2661 line = fparseln(f, &linelen, &lineno, delim, 0);
2662 if (len)
2663 *len = linelen;
2664 return line;
2667 static int
2668 match_line(const char *line, regex_t *regex)
2670 regmatch_t regmatch;
2672 return regexec(regex, line, 1, &regmatch, 0) == 0;
2675 struct tog_color *
2676 match_color(struct tog_colors *colors, const char *line)
2678 struct tog_color *tc = NULL;
2680 SIMPLEQ_FOREACH(tc, colors, entry) {
2681 if (match_line(line, &tc->regex))
2682 return tc;
2685 return NULL;
2688 static const struct got_error *
2689 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2690 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2691 char *header, struct tog_colors *colors)
2693 const struct got_error *err;
2694 int lineno = 0, nprinted = 0;
2695 char *line;
2696 struct tog_color *tc;
2697 size_t len;
2698 wchar_t *wline;
2699 int width;
2701 rewind(f);
2702 werase(view->window);
2704 if (header) {
2705 err = format_line(&wline, &width, header, view->ncols, 0);
2706 if (err) {
2707 return err;
2710 if (view_needs_focus_indication(view))
2711 wstandout(view->window);
2712 waddwstr(view->window, wline);
2713 if (view_needs_focus_indication(view))
2714 wstandend(view->window);
2715 if (width <= view->ncols - 1)
2716 waddch(view->window, '\n');
2718 if (max_lines <= 1)
2719 return NULL;
2720 max_lines--;
2723 *eof = 0;
2724 while (nprinted < max_lines) {
2725 line = parse_next_line(f, &len);
2726 if (line == NULL) {
2727 *eof = 1;
2728 break;
2730 if (++lineno < *first_displayed_line) {
2731 free(line);
2732 continue;
2735 err = format_line(&wline, &width, line, view->ncols, 0);
2736 if (err) {
2737 free(line);
2738 return err;
2741 tc = match_color(colors, line);
2742 if (tc)
2743 wattr_on(view->window,
2744 COLOR_PAIR(tc->colorpair), NULL);
2745 waddwstr(view->window, wline);
2746 if (tc)
2747 wattr_off(view->window,
2748 COLOR_PAIR(tc->colorpair), NULL);
2749 if (width <= view->ncols - 1)
2750 waddch(view->window, '\n');
2751 if (++nprinted == 1)
2752 *first_displayed_line = lineno;
2753 free(line);
2754 free(wline);
2755 wline = NULL;
2757 *last_displayed_line = lineno;
2759 view_vborder(view);
2761 if (*eof) {
2762 while (nprinted < view->nlines) {
2763 waddch(view->window, '\n');
2764 nprinted++;
2767 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2768 if (err) {
2769 return err;
2772 wstandout(view->window);
2773 waddwstr(view->window, wline);
2774 wstandend(view->window);
2777 return NULL;
2780 static char *
2781 get_datestr(time_t *time, char *datebuf)
2783 struct tm mytm, *tm;
2784 char *p, *s;
2786 tm = gmtime_r(time, &mytm);
2787 if (tm == NULL)
2788 return NULL;
2789 s = asctime_r(tm, datebuf);
2790 if (s == NULL)
2791 return NULL;
2792 p = strchr(s, '\n');
2793 if (p)
2794 *p = '\0';
2795 return s;
2798 static const struct got_error *
2799 write_commit_info(struct got_object_id *commit_id,
2800 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2802 const struct got_error *err = NULL;
2803 char datebuf[26], *datestr;
2804 struct got_commit_object *commit;
2805 char *id_str = NULL, *logmsg = NULL;
2806 time_t committer_time;
2807 const char *author, *committer;
2808 char *refs_str = NULL;
2810 if (refs) {
2811 err = build_refs_str(&refs_str, refs, commit_id, repo);
2812 if (err)
2813 return err;
2816 err = got_object_open_as_commit(&commit, repo, commit_id);
2817 if (err)
2818 return err;
2820 err = got_object_id_str(&id_str, commit_id);
2821 if (err) {
2822 err = got_error_from_errno("got_object_id_str");
2823 goto done;
2826 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2827 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2828 err = got_error_from_errno("fprintf");
2829 goto done;
2831 if (fprintf(outfile, "from: %s\n",
2832 got_object_commit_get_author(commit)) < 0) {
2833 err = got_error_from_errno("fprintf");
2834 goto done;
2836 committer_time = got_object_commit_get_committer_time(commit);
2837 datestr = get_datestr(&committer_time, datebuf);
2838 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2839 err = got_error_from_errno("fprintf");
2840 goto done;
2842 author = got_object_commit_get_author(commit);
2843 committer = got_object_commit_get_committer(commit);
2844 if (strcmp(author, committer) != 0 &&
2845 fprintf(outfile, "via: %s\n", committer) < 0) {
2846 err = got_error_from_errno("fprintf");
2847 goto done;
2849 err = got_object_commit_get_logmsg(&logmsg, commit);
2850 if (err)
2851 goto done;
2852 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2853 err = got_error_from_errno("fprintf");
2854 goto done;
2856 done:
2857 free(id_str);
2858 free(logmsg);
2859 free(refs_str);
2860 got_object_commit_close(commit);
2861 return err;
2864 const struct got_error *
2865 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2866 FILE *infile)
2868 size_t len;
2869 char *buf = NULL;
2870 int i;
2871 size_t noffsets = 0;
2872 off_t off = 0;
2874 if (line_offsets)
2875 *line_offsets = NULL;
2876 if (filesize)
2877 *filesize = 0;
2878 if (nlines)
2879 *nlines = 0;
2881 if (fseek(infile, 0, SEEK_END) == -1)
2882 return got_error_from_errno("fseek");
2883 len = ftell(infile) + 1;
2884 if (ferror(infile))
2885 return got_error_from_errno("ftell");
2886 if (fseek(infile, 0, SEEK_SET) == -1)
2887 return got_error_from_errno("fseek");
2889 if (len == 0)
2890 return NULL;
2891 if ((buf = calloc(len, sizeof(char *))) == NULL)
2892 return got_error_from_errno("calloc");
2894 fread(buf, 1, len, infile);
2895 if (ferror(infile))
2896 return got_error_from_errno("fread");
2898 i = 0;
2899 if (line_offsets && nlines) {
2900 if (*line_offsets == NULL) {
2901 /* Have some data but perhaps no '\n'. */
2902 noffsets = 1;
2903 *nlines = 1;
2904 *line_offsets = calloc(1, sizeof(**line_offsets));
2905 if (*line_offsets == NULL)
2906 return got_error_from_errno("calloc");
2907 /* Skip forward over end of first line. */
2908 while (i < len) {
2909 if (buf[i] == '\n')
2910 break;
2911 i++;
2914 /* Scan '\n' offsets in remaining chunk of data. */
2915 while (i < len) {
2916 if (buf[i] != '\n') {
2917 i++;
2918 continue;
2920 (*nlines)++;
2921 if (noffsets < *nlines) {
2922 off_t *o = recallocarray(*line_offsets,
2923 noffsets, *nlines,
2924 sizeof(**line_offsets));
2925 if (o == NULL) {
2926 free(*line_offsets);
2927 *line_offsets = NULL;
2928 return got_error_from_errno(
2929 "recallocarray");
2931 *line_offsets = o;
2932 noffsets = *nlines;
2934 off = i + 1;
2935 (*line_offsets)[*nlines - 1] = off;
2936 i++;
2940 if (fflush(infile) != 0)
2941 return got_error_from_errno("fflush");
2942 rewind(infile);
2944 if (filesize)
2945 *filesize = len;
2947 return NULL;
2950 static const struct got_error *
2951 create_diff(struct tog_diff_view_state *s)
2953 const struct got_error *err = NULL;
2954 FILE *f = NULL;
2955 int obj_type;
2957 f = got_opentemp();
2958 if (f == NULL) {
2959 err = got_error_from_errno("got_opentemp");
2960 goto done;
2962 if (s->f && fclose(s->f) != 0) {
2963 err = got_error_from_errno("fclose");
2964 goto done;
2966 s->f = f;
2968 if (s->id1)
2969 err = got_object_get_type(&obj_type, s->repo, s->id1);
2970 else
2971 err = got_object_get_type(&obj_type, s->repo, s->id2);
2972 if (err)
2973 goto done;
2975 switch (obj_type) {
2976 case GOT_OBJ_TYPE_BLOB:
2977 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2978 s->diff_context, 0, s->repo, s->f);
2979 break;
2980 case GOT_OBJ_TYPE_TREE:
2981 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2982 s->diff_context, 0, s->repo, s->f);
2983 break;
2984 case GOT_OBJ_TYPE_COMMIT: {
2985 const struct got_object_id_queue *parent_ids;
2986 struct got_object_qid *pid;
2987 struct got_commit_object *commit2;
2989 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2990 if (err)
2991 goto done;
2992 /* Show commit info if we're diffing to a parent/root commit. */
2993 if (s->id1 == NULL) {
2994 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
2995 if (err)
2996 goto done;
2997 } else {
2998 parent_ids = got_object_commit_get_parent_ids(commit2);
2999 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3000 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3001 err = write_commit_info(s->id2, s->refs,
3002 s->repo, s->f);
3003 if (err)
3004 goto done;
3005 break;
3009 got_object_commit_close(commit2);
3011 err = got_diff_objects_as_commits(s->id1, s->id2,
3012 s->diff_context, 0, s->repo, s->f);
3013 break;
3015 default:
3016 err = got_error(GOT_ERR_OBJ_TYPE);
3017 break;
3019 if (err)
3020 goto done;
3021 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3022 s->f);
3023 done:
3024 if (s->f && fflush(s->f) != 0 && err == NULL)
3025 err = got_error_from_errno("fflush");
3026 return err;
3029 static void
3030 diff_view_indicate_progress(struct tog_view *view)
3032 mvwaddstr(view->window, 0, 0, "diffing...");
3033 update_panels();
3034 doupdate();
3037 static const struct got_error *
3038 search_start_diff_view(struct tog_view *view)
3040 struct tog_diff_view_state *s = &view->state.diff;
3042 s->matched_line = 0;
3043 return NULL;
3046 static const struct got_error *
3047 search_next_diff_view(struct tog_view *view)
3049 struct tog_diff_view_state *s = &view->state.diff;
3050 int lineno;
3052 if (!view->searching) {
3053 view->search_next_done = 1;
3054 return NULL;
3057 if (s->matched_line) {
3058 if (view->searching == TOG_SEARCH_FORWARD)
3059 lineno = s->matched_line + 1;
3060 else
3061 lineno = s->matched_line - 1;
3062 } else {
3063 if (view->searching == TOG_SEARCH_FORWARD)
3064 lineno = 1;
3065 else
3066 lineno = s->nlines;
3069 while (1) {
3070 char *line = NULL;
3071 off_t offset;
3072 size_t len;
3074 if (lineno <= 0 || lineno > s->nlines) {
3075 if (s->matched_line == 0) {
3076 view->search_next_done = 1;
3077 free(line);
3078 break;
3081 if (view->searching == TOG_SEARCH_FORWARD)
3082 lineno = 1;
3083 else
3084 lineno = s->nlines;
3087 offset = s->line_offsets[lineno - 1];
3088 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3089 free(line);
3090 return got_error_from_errno("fseeko");
3092 free(line);
3093 line = parse_next_line(s->f, &len);
3094 if (line && match_line(line, &view->regex)) {
3095 view->search_next_done = 1;
3096 s->matched_line = lineno;
3097 free(line);
3098 break;
3100 free(line);
3101 if (view->searching == TOG_SEARCH_FORWARD)
3102 lineno++;
3103 else
3104 lineno--;
3107 if (s->matched_line) {
3108 s->first_displayed_line = s->matched_line;
3109 s->selected_line = 1;
3112 return NULL;
3115 static const struct got_error *
3116 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3117 struct got_object_id *id2, struct tog_view *log_view,
3118 struct got_reflist_head *refs, struct got_repository *repo)
3120 const struct got_error *err;
3121 struct tog_diff_view_state *s = &view->state.diff;
3123 if (id1 != NULL && id2 != NULL) {
3124 int type1, type2;
3125 err = got_object_get_type(&type1, repo, id1);
3126 if (err)
3127 return err;
3128 err = got_object_get_type(&type2, repo, id2);
3129 if (err)
3130 return err;
3132 if (type1 != type2)
3133 return got_error(GOT_ERR_OBJ_TYPE);
3135 s->first_displayed_line = 1;
3136 s->last_displayed_line = view->nlines;
3137 s->selected_line = 1;
3138 s->repo = repo;
3139 s->refs = refs;
3140 s->id1 = id1;
3141 s->id2 = id2;
3143 if (id1) {
3144 s->id1 = got_object_id_dup(id1);
3145 if (s->id1 == NULL)
3146 return got_error_from_errno("got_object_id_dup");
3147 } else
3148 s->id1 = NULL;
3150 s->id2 = got_object_id_dup(id2);
3151 if (s->id2 == NULL) {
3152 free(s->id1);
3153 s->id1 = NULL;
3154 return got_error_from_errno("got_object_id_dup");
3156 s->f = NULL;
3157 s->first_displayed_line = 1;
3158 s->last_displayed_line = view->nlines;
3159 s->diff_context = 3;
3160 s->log_view = log_view;
3161 s->repo = repo;
3162 s->refs = refs;
3164 SIMPLEQ_INIT(&s->colors);
3165 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3166 err = add_color(&s->colors,
3167 "^-", TOG_COLOR_DIFF_MINUS,
3168 get_color_value("TOG_COLOR_DIFF_MINUS"));
3169 if (err)
3170 return err;
3171 err = add_color(&s->colors, "^\\+",
3172 TOG_COLOR_DIFF_PLUS,
3173 get_color_value("TOG_COLOR_DIFF_PLUS"));
3174 if (err) {
3175 free_colors(&s->colors);
3176 return err;
3178 err = add_color(&s->colors,
3179 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3180 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3181 if (err) {
3182 free_colors(&s->colors);
3183 return err;
3186 err = add_color(&s->colors,
3187 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3188 get_color_value("TOG_COLOR_DIFF_META"));
3189 if (err) {
3190 free_colors(&s->colors);
3191 return err;
3194 err = add_color(&s->colors,
3195 "^(from|via): ", TOG_COLOR_AUTHOR,
3196 get_color_value("TOG_COLOR_AUTHOR"));
3197 if (err) {
3198 free_colors(&s->colors);
3199 return err;
3202 err = add_color(&s->colors,
3203 "^date: ", TOG_COLOR_DATE,
3204 get_color_value("TOG_COLOR_DATE"));
3205 if (err) {
3206 free_colors(&s->colors);
3207 return err;
3211 if (log_view && view_is_splitscreen(view))
3212 show_log_view(log_view); /* draw vborder */
3213 diff_view_indicate_progress(view);
3215 err = create_diff(s);
3216 if (err) {
3217 free(s->id1);
3218 s->id1 = NULL;
3219 free(s->id2);
3220 s->id2 = NULL;
3221 return err;
3224 view->show = show_diff_view;
3225 view->input = input_diff_view;
3226 view->close = close_diff_view;
3227 view->search_start = search_start_diff_view;
3228 view->search_next = search_next_diff_view;
3230 return NULL;
3233 static const struct got_error *
3234 close_diff_view(struct tog_view *view)
3236 const struct got_error *err = NULL;
3237 struct tog_diff_view_state *s = &view->state.diff;
3239 free(s->id1);
3240 s->id1 = NULL;
3241 free(s->id2);
3242 s->id2 = NULL;
3243 if (s->f && fclose(s->f) == EOF)
3244 err = got_error_from_errno("fclose");
3245 free_colors(&s->colors);
3246 free(s->line_offsets);
3247 return err;
3250 static const struct got_error *
3251 show_diff_view(struct tog_view *view)
3253 const struct got_error *err;
3254 struct tog_diff_view_state *s = &view->state.diff;
3255 char *id_str1 = NULL, *id_str2, *header;
3257 if (s->id1) {
3258 err = got_object_id_str(&id_str1, s->id1);
3259 if (err)
3260 return err;
3262 err = got_object_id_str(&id_str2, s->id2);
3263 if (err)
3264 return err;
3266 if (asprintf(&header, "diff %s %s",
3267 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3268 err = got_error_from_errno("asprintf");
3269 free(id_str1);
3270 free(id_str2);
3271 return err;
3273 free(id_str1);
3274 free(id_str2);
3276 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3277 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3278 header, &s->colors);
3281 static const struct got_error *
3282 set_selected_commit(struct tog_diff_view_state *s,
3283 struct commit_queue_entry *entry)
3285 const struct got_error *err;
3286 const struct got_object_id_queue *parent_ids;
3287 struct got_commit_object *selected_commit;
3288 struct got_object_qid *pid;
3290 free(s->id2);
3291 s->id2 = got_object_id_dup(entry->id);
3292 if (s->id2 == NULL)
3293 return got_error_from_errno("got_object_id_dup");
3295 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3296 if (err)
3297 return err;
3298 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3299 free(s->id1);
3300 pid = SIMPLEQ_FIRST(parent_ids);
3301 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3302 got_object_commit_close(selected_commit);
3303 return NULL;
3306 static const struct got_error *
3307 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3308 struct tog_view **focus_view, struct tog_view *view, int ch)
3310 const struct got_error *err = NULL;
3311 struct tog_diff_view_state *s = &view->state.diff;
3312 struct tog_log_view_state *ls;
3313 struct commit_queue_entry *entry;
3314 int i;
3316 switch (ch) {
3317 case 'k':
3318 case KEY_UP:
3319 if (s->first_displayed_line > 1)
3320 s->first_displayed_line--;
3321 break;
3322 case KEY_PPAGE:
3323 case CTRL('b'):
3324 if (s->first_displayed_line == 1)
3325 break;
3326 i = 0;
3327 while (i++ < view->nlines - 1 &&
3328 s->first_displayed_line > 1)
3329 s->first_displayed_line--;
3330 break;
3331 case 'j':
3332 case KEY_DOWN:
3333 if (!s->eof)
3334 s->first_displayed_line++;
3335 break;
3336 case KEY_NPAGE:
3337 case CTRL('f'):
3338 case ' ':
3339 if (s->eof)
3340 break;
3341 i = 0;
3342 while (!s->eof && i++ < view->nlines - 1) {
3343 char *line;
3344 line = parse_next_line(s->f, NULL);
3345 s->first_displayed_line++;
3346 if (line == NULL)
3347 break;
3349 break;
3350 case '[':
3351 if (s->diff_context > 0) {
3352 s->diff_context--;
3353 diff_view_indicate_progress(view);
3354 err = create_diff(s);
3356 break;
3357 case ']':
3358 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3359 s->diff_context++;
3360 diff_view_indicate_progress(view);
3361 err = create_diff(s);
3363 break;
3364 case '<':
3365 case ',':
3366 if (s->log_view == NULL)
3367 break;
3368 ls = &s->log_view->state.log;
3369 entry = TAILQ_PREV(ls->selected_entry,
3370 commit_queue_head, entry);
3371 if (entry == NULL)
3372 break;
3374 err = input_log_view(NULL, NULL, NULL, s->log_view,
3375 KEY_UP);
3376 if (err)
3377 break;
3379 err = set_selected_commit(s, entry);
3380 if (err)
3381 break;
3383 s->first_displayed_line = 1;
3384 s->last_displayed_line = view->nlines;
3386 diff_view_indicate_progress(view);
3387 err = create_diff(s);
3388 break;
3389 case '>':
3390 case '.':
3391 if (s->log_view == NULL)
3392 break;
3393 ls = &s->log_view->state.log;
3395 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3396 ls->thread_args.commits_needed++;
3398 /* Display "loading..." in log view. */
3399 show_log_view(s->log_view);
3400 update_panels();
3401 doupdate();
3403 err = trigger_log_thread(1 /* load_all */,
3404 &ls->thread_args.commits_needed,
3405 &ls->thread_args.log_complete,
3406 &ls->thread_args.need_commits);
3407 if (err)
3408 break;
3410 err = input_log_view(NULL, NULL, NULL, s->log_view,
3411 KEY_DOWN);
3412 if (err)
3413 break;
3415 entry = TAILQ_NEXT(ls->selected_entry, entry);
3416 if (entry == NULL)
3417 break;
3419 err = set_selected_commit(s, entry);
3420 if (err)
3421 break;
3423 s->first_displayed_line = 1;
3424 s->last_displayed_line = view->nlines;
3426 diff_view_indicate_progress(view);
3427 err = create_diff(s);
3428 break;
3429 default:
3430 break;
3433 return err;
3436 static const struct got_error *
3437 cmd_diff(int argc, char *argv[])
3439 const struct got_error *error = NULL;
3440 struct got_repository *repo = NULL;
3441 struct got_reflist_head refs;
3442 struct got_object_id *id1 = NULL, *id2 = NULL;
3443 char *repo_path = NULL;
3444 char *id_str1 = NULL, *id_str2 = NULL;
3445 int ch;
3446 struct tog_view *view;
3448 SIMPLEQ_INIT(&refs);
3450 #ifndef PROFILE
3451 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3452 NULL) == -1)
3453 err(1, "pledge");
3454 #endif
3456 while ((ch = getopt(argc, argv, "")) != -1) {
3457 switch (ch) {
3458 default:
3459 usage_diff();
3460 /* NOTREACHED */
3464 argc -= optind;
3465 argv += optind;
3467 if (argc == 0) {
3468 usage_diff(); /* TODO show local worktree changes */
3469 } else if (argc == 2) {
3470 repo_path = getcwd(NULL, 0);
3471 if (repo_path == NULL)
3472 return got_error_from_errno("getcwd");
3473 id_str1 = argv[0];
3474 id_str2 = argv[1];
3475 } else if (argc == 3) {
3476 repo_path = realpath(argv[0], NULL);
3477 if (repo_path == NULL)
3478 return got_error_from_errno2("realpath", argv[0]);
3479 id_str1 = argv[1];
3480 id_str2 = argv[2];
3481 } else
3482 usage_diff();
3484 init_curses();
3486 error = got_repo_open(&repo, repo_path, NULL);
3487 if (error)
3488 goto done;
3490 error = apply_unveil(got_repo_get_path(repo), NULL);
3491 if (error)
3492 goto done;
3494 error = got_repo_match_object_id_prefix(&id1, id_str1,
3495 GOT_OBJ_TYPE_ANY, repo);
3496 if (error)
3497 goto done;
3499 error = got_repo_match_object_id_prefix(&id2, id_str2,
3500 GOT_OBJ_TYPE_ANY, repo);
3501 if (error)
3502 goto done;
3504 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3505 if (error)
3506 goto done;
3508 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3509 if (view == NULL) {
3510 error = got_error_from_errno("view_open");
3511 goto done;
3513 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3514 if (error)
3515 goto done;
3516 error = view_loop(view);
3517 done:
3518 free(repo_path);
3519 if (repo)
3520 got_repo_close(repo);
3521 got_ref_list_free(&refs);
3522 return error;
3525 __dead static void
3526 usage_blame(void)
3528 endwin();
3529 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3530 getprogname());
3531 exit(1);
3534 struct tog_blame_line {
3535 int annotated;
3536 struct got_object_id *id;
3539 static const struct got_error *
3540 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3541 const char *path, struct tog_blame_line *lines, int nlines,
3542 int blame_complete, int selected_line, int *first_displayed_line,
3543 int *last_displayed_line, int *eof, int max_lines,
3544 struct tog_colors *colors)
3546 const struct got_error *err;
3547 int lineno = 0, nprinted = 0;
3548 char *line;
3549 size_t len;
3550 wchar_t *wline;
3551 int width;
3552 struct tog_blame_line *blame_line;
3553 struct got_object_id *prev_id = NULL;
3554 char *id_str;
3555 struct tog_color *tc;
3557 err = got_object_id_str(&id_str, id);
3558 if (err)
3559 return err;
3561 rewind(f);
3562 werase(view->window);
3564 if (asprintf(&line, "commit %s", id_str) == -1) {
3565 err = got_error_from_errno("asprintf");
3566 free(id_str);
3567 return err;
3570 err = format_line(&wline, &width, line, view->ncols, 0);
3571 free(line);
3572 line = NULL;
3573 if (err)
3574 return err;
3575 if (view_needs_focus_indication(view))
3576 wstandout(view->window);
3577 tc = get_color(colors, TOG_COLOR_COMMIT);
3578 if (tc)
3579 wattr_on(view->window,
3580 COLOR_PAIR(tc->colorpair), NULL);
3581 waddwstr(view->window, wline);
3582 if (tc)
3583 wattr_off(view->window,
3584 COLOR_PAIR(tc->colorpair), NULL);
3585 if (view_needs_focus_indication(view))
3586 wstandend(view->window);
3587 free(wline);
3588 wline = NULL;
3589 if (width < view->ncols - 1)
3590 waddch(view->window, '\n');
3592 if (asprintf(&line, "[%d/%d] %s%s",
3593 *first_displayed_line - 1 + selected_line, nlines,
3594 blame_complete ? "" : "annotating... ", path) == -1) {
3595 free(id_str);
3596 return got_error_from_errno("asprintf");
3598 free(id_str);
3599 err = format_line(&wline, &width, line, view->ncols, 0);
3600 free(line);
3601 line = NULL;
3602 if (err)
3603 return err;
3604 waddwstr(view->window, wline);
3605 free(wline);
3606 wline = NULL;
3607 if (width < view->ncols - 1)
3608 waddch(view->window, '\n');
3610 *eof = 0;
3611 while (nprinted < max_lines - 2) {
3612 line = parse_next_line(f, &len);
3613 if (line == NULL) {
3614 *eof = 1;
3615 break;
3617 if (++lineno < *first_displayed_line) {
3618 free(line);
3619 continue;
3622 if (view->ncols <= 9) {
3623 width = 9;
3624 wline = wcsdup(L"");
3625 if (wline == NULL)
3626 err = got_error_from_errno("wcsdup");
3627 } else {
3628 err = format_line(&wline, &width, line,
3629 view->ncols - 9, 9);
3630 width += 9;
3632 if (err) {
3633 free(line);
3634 return err;
3637 if (view->focussed && nprinted == selected_line - 1)
3638 wstandout(view->window);
3640 if (nlines > 0) {
3641 blame_line = &lines[lineno - 1];
3642 if (blame_line->annotated && prev_id &&
3643 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3644 !(view->focussed &&
3645 nprinted == selected_line - 1)) {
3646 waddstr(view->window, " ");
3647 } else if (blame_line->annotated) {
3648 char *id_str;
3649 err = got_object_id_str(&id_str, blame_line->id);
3650 if (err) {
3651 free(line);
3652 free(wline);
3653 return err;
3655 tc = get_color(colors, TOG_COLOR_COMMIT);
3656 if (tc)
3657 wattr_on(view->window,
3658 COLOR_PAIR(tc->colorpair), NULL);
3659 wprintw(view->window, "%.8s", id_str);
3660 if (tc)
3661 wattr_off(view->window,
3662 COLOR_PAIR(tc->colorpair), NULL);
3663 free(id_str);
3664 prev_id = blame_line->id;
3665 } else {
3666 waddstr(view->window, "........");
3667 prev_id = NULL;
3669 } else {
3670 waddstr(view->window, "........");
3671 prev_id = NULL;
3674 if (view->focussed && nprinted == selected_line - 1)
3675 wstandend(view->window);
3676 waddstr(view->window, " ");
3678 waddwstr(view->window, wline);
3679 if (width <= view->ncols - 1)
3680 waddch(view->window, '\n');
3681 if (++nprinted == 1)
3682 *first_displayed_line = lineno;
3683 free(line);
3684 free(wline);
3685 wline = NULL;
3687 *last_displayed_line = lineno;
3689 view_vborder(view);
3691 return NULL;
3694 static const struct got_error *
3695 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3697 const struct got_error *err = NULL;
3698 struct tog_blame_cb_args *a = arg;
3699 struct tog_blame_line *line;
3700 int errcode;
3702 if (nlines != a->nlines ||
3703 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3704 return got_error(GOT_ERR_RANGE);
3706 errcode = pthread_mutex_lock(&tog_mutex);
3707 if (errcode)
3708 return got_error_set_errno(errcode, "pthread_mutex_lock");
3710 if (*a->quit) { /* user has quit the blame view */
3711 err = got_error(GOT_ERR_ITER_COMPLETED);
3712 goto done;
3715 if (lineno == -1)
3716 goto done; /* no change in this commit */
3718 line = &a->lines[lineno - 1];
3719 if (line->annotated)
3720 goto done;
3722 line->id = got_object_id_dup(id);
3723 if (line->id == NULL) {
3724 err = got_error_from_errno("got_object_id_dup");
3725 goto done;
3727 line->annotated = 1;
3728 done:
3729 errcode = pthread_mutex_unlock(&tog_mutex);
3730 if (errcode)
3731 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3732 return err;
3735 static void *
3736 blame_thread(void *arg)
3738 const struct got_error *err;
3739 struct tog_blame_thread_args *ta = arg;
3740 struct tog_blame_cb_args *a = ta->cb_args;
3741 int errcode;
3743 err = block_signals_used_by_main_thread();
3744 if (err)
3745 return (void *)err;
3747 err = got_blame(ta->path, a->commit_id, ta->repo,
3748 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3749 if (err && err->code == GOT_ERR_CANCELLED)
3750 err = NULL;
3752 errcode = pthread_mutex_lock(&tog_mutex);
3753 if (errcode)
3754 return (void *)got_error_set_errno(errcode,
3755 "pthread_mutex_lock");
3757 got_repo_close(ta->repo);
3758 ta->repo = NULL;
3759 *ta->complete = 1;
3761 errcode = pthread_mutex_unlock(&tog_mutex);
3762 if (errcode && err == NULL)
3763 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3765 return (void *)err;
3768 static struct got_object_id *
3769 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3770 int first_displayed_line, int selected_line)
3772 struct tog_blame_line *line;
3774 if (nlines <= 0)
3775 return NULL;
3777 line = &lines[first_displayed_line - 1 + selected_line - 1];
3778 if (!line->annotated)
3779 return NULL;
3781 return line->id;
3784 static const struct got_error *
3785 stop_blame(struct tog_blame *blame)
3787 const struct got_error *err = NULL;
3788 int i;
3790 if (blame->thread) {
3791 int errcode;
3792 errcode = pthread_mutex_unlock(&tog_mutex);
3793 if (errcode)
3794 return got_error_set_errno(errcode,
3795 "pthread_mutex_unlock");
3796 errcode = pthread_join(blame->thread, (void **)&err);
3797 if (errcode)
3798 return got_error_set_errno(errcode, "pthread_join");
3799 errcode = pthread_mutex_lock(&tog_mutex);
3800 if (errcode)
3801 return got_error_set_errno(errcode,
3802 "pthread_mutex_lock");
3803 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3804 err = NULL;
3805 blame->thread = NULL;
3807 if (blame->thread_args.repo) {
3808 got_repo_close(blame->thread_args.repo);
3809 blame->thread_args.repo = NULL;
3811 if (blame->f) {
3812 if (fclose(blame->f) != 0 && err == NULL)
3813 err = got_error_from_errno("fclose");
3814 blame->f = NULL;
3816 if (blame->lines) {
3817 for (i = 0; i < blame->nlines; i++)
3818 free(blame->lines[i].id);
3819 free(blame->lines);
3820 blame->lines = NULL;
3822 free(blame->cb_args.commit_id);
3823 blame->cb_args.commit_id = NULL;
3825 return err;
3828 static const struct got_error *
3829 cancel_blame_view(void *arg)
3831 const struct got_error *err = NULL;
3832 int *done = arg;
3833 int errcode;
3835 errcode = pthread_mutex_lock(&tog_mutex);
3836 if (errcode)
3837 return got_error_set_errno(errcode,
3838 "pthread_mutex_unlock");
3840 if (*done)
3841 err = got_error(GOT_ERR_CANCELLED);
3843 errcode = pthread_mutex_unlock(&tog_mutex);
3844 if (errcode)
3845 return got_error_set_errno(errcode,
3846 "pthread_mutex_lock");
3848 return err;
3851 static const struct got_error *
3852 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3853 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3854 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3855 struct got_repository *repo)
3857 const struct got_error *err = NULL;
3858 struct got_blob_object *blob = NULL;
3859 struct got_repository *thread_repo = NULL;
3860 struct got_object_id *obj_id = NULL;
3861 int obj_type;
3863 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3864 if (err)
3865 return err;
3867 err = got_object_get_type(&obj_type, repo, obj_id);
3868 if (err)
3869 goto done;
3871 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3872 err = got_error(GOT_ERR_OBJ_TYPE);
3873 goto done;
3876 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3877 if (err)
3878 goto done;
3879 blame->f = got_opentemp();
3880 if (blame->f == NULL) {
3881 err = got_error_from_errno("got_opentemp");
3882 goto done;
3884 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3885 &blame->line_offsets, blame->f, blob);
3886 if (err || blame->nlines == 0)
3887 goto done;
3889 /* Don't include \n at EOF in the blame line count. */
3890 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3891 blame->nlines--;
3893 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3894 if (blame->lines == NULL) {
3895 err = got_error_from_errno("calloc");
3896 goto done;
3899 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3900 if (err)
3901 goto done;
3903 blame->cb_args.view = view;
3904 blame->cb_args.lines = blame->lines;
3905 blame->cb_args.nlines = blame->nlines;
3906 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3907 if (blame->cb_args.commit_id == NULL) {
3908 err = got_error_from_errno("got_object_id_dup");
3909 goto done;
3911 blame->cb_args.quit = done;
3913 blame->thread_args.path = path;
3914 blame->thread_args.repo = thread_repo;
3915 blame->thread_args.cb_args = &blame->cb_args;
3916 blame->thread_args.complete = blame_complete;
3917 blame->thread_args.cancel_cb = cancel_blame_view;
3918 blame->thread_args.cancel_arg = done;
3919 *blame_complete = 0;
3921 done:
3922 if (blob)
3923 got_object_blob_close(blob);
3924 free(obj_id);
3925 if (err)
3926 stop_blame(blame);
3927 return err;
3930 static const struct got_error *
3931 open_blame_view(struct tog_view *view, char *path,
3932 struct got_object_id *commit_id, struct got_reflist_head *refs,
3933 struct got_repository *repo)
3935 const struct got_error *err = NULL;
3936 struct tog_blame_view_state *s = &view->state.blame;
3938 SIMPLEQ_INIT(&s->blamed_commits);
3940 s->path = strdup(path);
3941 if (s->path == NULL)
3942 return got_error_from_errno("strdup");
3944 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3945 if (err) {
3946 free(s->path);
3947 return err;
3950 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3951 s->first_displayed_line = 1;
3952 s->last_displayed_line = view->nlines;
3953 s->selected_line = 1;
3954 s->blame_complete = 0;
3955 s->repo = repo;
3956 s->refs = refs;
3957 s->commit_id = commit_id;
3958 memset(&s->blame, 0, sizeof(s->blame));
3960 SIMPLEQ_INIT(&s->colors);
3961 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3962 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3963 get_color_value("TOG_COLOR_COMMIT"));
3964 if (err)
3965 return err;
3968 view->show = show_blame_view;
3969 view->input = input_blame_view;
3970 view->close = close_blame_view;
3971 view->search_start = search_start_blame_view;
3972 view->search_next = search_next_blame_view;
3974 return run_blame(&s->blame, view, &s->blame_complete,
3975 &s->first_displayed_line, &s->last_displayed_line,
3976 &s->selected_line, &s->done, &s->eof, s->path,
3977 s->blamed_commit->id, s->repo);
3980 static const struct got_error *
3981 close_blame_view(struct tog_view *view)
3983 const struct got_error *err = NULL;
3984 struct tog_blame_view_state *s = &view->state.blame;
3986 if (s->blame.thread)
3987 err = stop_blame(&s->blame);
3989 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3990 struct got_object_qid *blamed_commit;
3991 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3992 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3993 got_object_qid_free(blamed_commit);
3996 free(s->path);
3997 free_colors(&s->colors);
3999 return err;
4002 static const struct got_error *
4003 search_start_blame_view(struct tog_view *view)
4005 struct tog_blame_view_state *s = &view->state.blame;
4007 s->matched_line = 0;
4008 return NULL;
4011 static const struct got_error *
4012 search_next_blame_view(struct tog_view *view)
4014 struct tog_blame_view_state *s = &view->state.blame;
4015 int lineno;
4017 if (!view->searching) {
4018 view->search_next_done = 1;
4019 return NULL;
4022 if (s->matched_line) {
4023 if (view->searching == TOG_SEARCH_FORWARD)
4024 lineno = s->matched_line + 1;
4025 else
4026 lineno = s->matched_line - 1;
4027 } else {
4028 if (view->searching == TOG_SEARCH_FORWARD)
4029 lineno = 1;
4030 else
4031 lineno = s->blame.nlines;
4034 while (1) {
4035 char *line = NULL;
4036 off_t offset;
4037 size_t len;
4039 if (lineno <= 0 || lineno > s->blame.nlines) {
4040 if (s->matched_line == 0) {
4041 view->search_next_done = 1;
4042 free(line);
4043 break;
4046 if (view->searching == TOG_SEARCH_FORWARD)
4047 lineno = 1;
4048 else
4049 lineno = s->blame.nlines;
4052 offset = s->blame.line_offsets[lineno - 1];
4053 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4054 free(line);
4055 return got_error_from_errno("fseeko");
4057 free(line);
4058 line = parse_next_line(s->blame.f, &len);
4059 if (line && match_line(line, &view->regex)) {
4060 view->search_next_done = 1;
4061 s->matched_line = lineno;
4062 free(line);
4063 break;
4065 free(line);
4066 if (view->searching == TOG_SEARCH_FORWARD)
4067 lineno++;
4068 else
4069 lineno--;
4072 if (s->matched_line) {
4073 s->first_displayed_line = s->matched_line;
4074 s->selected_line = 1;
4077 return NULL;
4080 static const struct got_error *
4081 show_blame_view(struct tog_view *view)
4083 const struct got_error *err = NULL;
4084 struct tog_blame_view_state *s = &view->state.blame;
4085 int errcode;
4087 if (s->blame.thread == NULL) {
4088 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4089 &s->blame.thread_args);
4090 if (errcode)
4091 return got_error_set_errno(errcode, "pthread_create");
4093 halfdelay(1); /* fast refresh while annotating */
4096 if (s->blame_complete)
4097 halfdelay(10); /* disable fast refresh */
4099 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4100 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4101 s->selected_line, &s->first_displayed_line,
4102 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4104 view_vborder(view);
4105 return err;
4108 static const struct got_error *
4109 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4110 struct tog_view **focus_view, struct tog_view *view, int ch)
4112 const struct got_error *err = NULL, *thread_err = NULL;
4113 struct tog_view *diff_view;
4114 struct tog_blame_view_state *s = &view->state.blame;
4115 int begin_x = 0;
4117 switch (ch) {
4118 case 'q':
4119 s->done = 1;
4120 break;
4121 case 'k':
4122 case KEY_UP:
4123 if (s->selected_line > 1)
4124 s->selected_line--;
4125 else if (s->selected_line == 1 &&
4126 s->first_displayed_line > 1)
4127 s->first_displayed_line--;
4128 break;
4129 case KEY_PPAGE:
4130 if (s->first_displayed_line == 1) {
4131 s->selected_line = 1;
4132 break;
4134 if (s->first_displayed_line > view->nlines - 2)
4135 s->first_displayed_line -=
4136 (view->nlines - 2);
4137 else
4138 s->first_displayed_line = 1;
4139 break;
4140 case 'j':
4141 case KEY_DOWN:
4142 if (s->selected_line < view->nlines - 2 &&
4143 s->first_displayed_line +
4144 s->selected_line <= s->blame.nlines)
4145 s->selected_line++;
4146 else if (s->last_displayed_line <
4147 s->blame.nlines)
4148 s->first_displayed_line++;
4149 break;
4150 case 'b':
4151 case 'p': {
4152 struct got_object_id *id = NULL;
4153 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4154 s->first_displayed_line, s->selected_line);
4155 if (id == NULL)
4156 break;
4157 if (ch == 'p') {
4158 struct got_commit_object *commit;
4159 struct got_object_qid *pid;
4160 struct got_object_id *blob_id = NULL;
4161 int obj_type;
4162 err = got_object_open_as_commit(&commit,
4163 s->repo, id);
4164 if (err)
4165 break;
4166 pid = SIMPLEQ_FIRST(
4167 got_object_commit_get_parent_ids(commit));
4168 if (pid == NULL) {
4169 got_object_commit_close(commit);
4170 break;
4172 /* Check if path history ends here. */
4173 err = got_object_id_by_path(&blob_id, s->repo,
4174 pid->id, s->path);
4175 if (err) {
4176 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4177 err = NULL;
4178 got_object_commit_close(commit);
4179 break;
4181 err = got_object_get_type(&obj_type, s->repo,
4182 blob_id);
4183 free(blob_id);
4184 /* Can't blame non-blob type objects. */
4185 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4186 got_object_commit_close(commit);
4187 break;
4189 err = got_object_qid_alloc(&s->blamed_commit,
4190 pid->id);
4191 got_object_commit_close(commit);
4192 } else {
4193 if (got_object_id_cmp(id,
4194 s->blamed_commit->id) == 0)
4195 break;
4196 err = got_object_qid_alloc(&s->blamed_commit,
4197 id);
4199 if (err)
4200 break;
4201 s->done = 1;
4202 thread_err = stop_blame(&s->blame);
4203 s->done = 0;
4204 if (thread_err)
4205 break;
4206 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4207 s->blamed_commit, entry);
4208 err = run_blame(&s->blame, view, &s->blame_complete,
4209 &s->first_displayed_line, &s->last_displayed_line,
4210 &s->selected_line, &s->done, &s->eof,
4211 s->path, s->blamed_commit->id, s->repo);
4212 if (err)
4213 break;
4214 break;
4216 case 'B': {
4217 struct got_object_qid *first;
4218 first = SIMPLEQ_FIRST(&s->blamed_commits);
4219 if (!got_object_id_cmp(first->id, s->commit_id))
4220 break;
4221 s->done = 1;
4222 thread_err = stop_blame(&s->blame);
4223 s->done = 0;
4224 if (thread_err)
4225 break;
4226 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4227 got_object_qid_free(s->blamed_commit);
4228 s->blamed_commit =
4229 SIMPLEQ_FIRST(&s->blamed_commits);
4230 err = run_blame(&s->blame, view, &s->blame_complete,
4231 &s->first_displayed_line, &s->last_displayed_line,
4232 &s->selected_line, &s->done, &s->eof, s->path,
4233 s->blamed_commit->id, s->repo);
4234 if (err)
4235 break;
4236 break;
4238 case KEY_ENTER:
4239 case '\r': {
4240 struct got_object_id *id = NULL;
4241 struct got_object_qid *pid;
4242 struct got_commit_object *commit = NULL;
4243 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4244 s->first_displayed_line, s->selected_line);
4245 if (id == NULL)
4246 break;
4247 err = got_object_open_as_commit(&commit, s->repo, id);
4248 if (err)
4249 break;
4250 pid = SIMPLEQ_FIRST(
4251 got_object_commit_get_parent_ids(commit));
4252 if (view_is_parent_view(view))
4253 begin_x = view_split_begin_x(view->begin_x);
4254 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4255 if (diff_view == NULL) {
4256 got_object_commit_close(commit);
4257 err = got_error_from_errno("view_open");
4258 break;
4260 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4261 id, NULL, s->refs, s->repo);
4262 got_object_commit_close(commit);
4263 if (err) {
4264 view_close(diff_view);
4265 break;
4267 if (view_is_parent_view(view)) {
4268 err = view_close_child(view);
4269 if (err)
4270 break;
4271 err = view_set_child(view, diff_view);
4272 if (err) {
4273 view_close(diff_view);
4274 break;
4276 *focus_view = diff_view;
4277 view->child_focussed = 1;
4278 } else
4279 *new_view = diff_view;
4280 if (err)
4281 break;
4282 break;
4284 case KEY_NPAGE:
4285 case ' ':
4286 if (s->last_displayed_line >= s->blame.nlines &&
4287 s->selected_line >= MIN(s->blame.nlines,
4288 view->nlines - 2)) {
4289 break;
4291 if (s->last_displayed_line >= s->blame.nlines &&
4292 s->selected_line < view->nlines - 2) {
4293 s->selected_line = MIN(s->blame.nlines,
4294 view->nlines - 2);
4295 break;
4297 if (s->last_displayed_line + view->nlines - 2
4298 <= s->blame.nlines)
4299 s->first_displayed_line +=
4300 view->nlines - 2;
4301 else
4302 s->first_displayed_line =
4303 s->blame.nlines -
4304 (view->nlines - 3);
4305 break;
4306 case KEY_RESIZE:
4307 if (s->selected_line > view->nlines - 2) {
4308 s->selected_line = MIN(s->blame.nlines,
4309 view->nlines - 2);
4311 break;
4312 default:
4313 break;
4315 return thread_err ? thread_err : err;
4318 static const struct got_error *
4319 cmd_blame(int argc, char *argv[])
4321 const struct got_error *error;
4322 struct got_repository *repo = NULL;
4323 struct got_reflist_head refs;
4324 struct got_worktree *worktree = NULL;
4325 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4326 struct got_object_id *commit_id = NULL;
4327 char *commit_id_str = NULL;
4328 int ch;
4329 struct tog_view *view;
4331 SIMPLEQ_INIT(&refs);
4333 #ifndef PROFILE
4334 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4335 NULL) == -1)
4336 err(1, "pledge");
4337 #endif
4339 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4340 switch (ch) {
4341 case 'c':
4342 commit_id_str = optarg;
4343 break;
4344 case 'r':
4345 repo_path = realpath(optarg, NULL);
4346 if (repo_path == NULL)
4347 return got_error_from_errno2("realpath",
4348 optarg);
4349 break;
4350 default:
4351 usage_blame();
4352 /* NOTREACHED */
4356 argc -= optind;
4357 argv += optind;
4359 if (argc == 1)
4360 path = argv[0];
4361 else
4362 usage_blame();
4364 cwd = getcwd(NULL, 0);
4365 if (cwd == NULL) {
4366 error = got_error_from_errno("getcwd");
4367 goto done;
4369 if (repo_path == NULL) {
4370 error = got_worktree_open(&worktree, cwd);
4371 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4372 goto done;
4373 else
4374 error = NULL;
4375 if (worktree) {
4376 repo_path =
4377 strdup(got_worktree_get_repo_path(worktree));
4378 if (repo_path == NULL)
4379 error = got_error_from_errno("strdup");
4380 if (error)
4381 goto done;
4382 } else {
4383 repo_path = strdup(cwd);
4384 if (repo_path == NULL) {
4385 error = got_error_from_errno("strdup");
4386 goto done;
4391 init_curses();
4393 error = got_repo_open(&repo, repo_path, NULL);
4394 if (error != NULL)
4395 goto done;
4397 error = apply_unveil(got_repo_get_path(repo), NULL);
4398 if (error)
4399 goto done;
4401 if (worktree) {
4402 const char *prefix = got_worktree_get_path_prefix(worktree);
4403 char *p, *worktree_subdir = cwd +
4404 strlen(got_worktree_get_root_path(worktree));
4405 if (asprintf(&p, "%s%s%s%s%s",
4406 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4407 worktree_subdir, worktree_subdir[0] ? "/" : "",
4408 path) == -1) {
4409 error = got_error_from_errno("asprintf");
4410 goto done;
4412 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4413 free(p);
4414 } else {
4415 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4417 if (error)
4418 goto done;
4420 if (commit_id_str == NULL) {
4421 struct got_reference *head_ref;
4422 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4423 if (error != NULL)
4424 goto done;
4425 error = got_ref_resolve(&commit_id, repo, head_ref);
4426 got_ref_close(head_ref);
4427 } else {
4428 error = got_repo_match_object_id(&commit_id, NULL,
4429 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4431 if (error != NULL)
4432 goto done;
4434 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4435 if (error)
4436 goto done;
4438 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4439 if (view == NULL) {
4440 error = got_error_from_errno("view_open");
4441 goto done;
4443 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4444 if (error)
4445 goto done;
4446 if (worktree) {
4447 /* Release work tree lock. */
4448 got_worktree_close(worktree);
4449 worktree = NULL;
4451 error = view_loop(view);
4452 done:
4453 free(repo_path);
4454 free(cwd);
4455 free(commit_id);
4456 if (worktree)
4457 got_worktree_close(worktree);
4458 if (repo)
4459 got_repo_close(repo);
4460 got_ref_list_free(&refs);
4461 return error;
4464 static const struct got_error *
4465 draw_tree_entries(struct tog_view *view,
4466 struct got_tree_entry **first_displayed_entry,
4467 struct got_tree_entry **last_displayed_entry,
4468 struct got_tree_entry **selected_entry, int *ndisplayed,
4469 const char *label, int show_ids, const char *parent_path,
4470 struct got_tree_object *tree, int selected, int limit,
4471 int isroot, struct tog_colors *colors)
4473 const struct got_error *err = NULL;
4474 struct got_tree_entry *te;
4475 wchar_t *wline;
4476 struct tog_color *tc;
4477 int width, n, i, nentries;
4479 *ndisplayed = 0;
4481 werase(view->window);
4483 if (limit == 0)
4484 return NULL;
4486 err = format_line(&wline, &width, label, view->ncols, 0);
4487 if (err)
4488 return err;
4489 if (view_needs_focus_indication(view))
4490 wstandout(view->window);
4491 tc = get_color(colors, TOG_COLOR_COMMIT);
4492 if (tc)
4493 wattr_on(view->window,
4494 COLOR_PAIR(tc->colorpair), NULL);
4495 waddwstr(view->window, wline);
4496 if (tc)
4497 wattr_off(view->window,
4498 COLOR_PAIR(tc->colorpair), NULL);
4499 if (view_needs_focus_indication(view))
4500 wstandend(view->window);
4501 free(wline);
4502 wline = NULL;
4503 if (width < view->ncols - 1)
4504 waddch(view->window, '\n');
4505 if (--limit <= 0)
4506 return NULL;
4507 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4508 if (err)
4509 return err;
4510 waddwstr(view->window, wline);
4511 free(wline);
4512 wline = NULL;
4513 if (width < view->ncols - 1)
4514 waddch(view->window, '\n');
4515 if (--limit <= 0)
4516 return NULL;
4517 waddch(view->window, '\n');
4518 if (--limit <= 0)
4519 return NULL;
4521 if (*first_displayed_entry == NULL) {
4522 te = got_object_tree_get_first_entry(tree);
4523 if (selected == 0) {
4524 if (view->focussed)
4525 wstandout(view->window);
4526 *selected_entry = NULL;
4528 waddstr(view->window, " ..\n"); /* parent directory */
4529 if (selected == 0 && view->focussed)
4530 wstandend(view->window);
4531 (*ndisplayed)++;
4532 if (--limit <= 0)
4533 return NULL;
4534 n = 1;
4535 } else {
4536 n = 0;
4537 te = *first_displayed_entry;
4540 nentries = got_object_tree_get_nentries(tree);
4541 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4542 char *line = NULL, *id_str = NULL;
4543 const char *modestr = "";
4544 mode_t mode;
4546 te = got_object_tree_get_entry(tree, i);
4547 mode = got_tree_entry_get_mode(te);
4549 if (show_ids) {
4550 err = got_object_id_str(&id_str,
4551 got_tree_entry_get_id(te));
4552 if (err)
4553 return got_error_from_errno(
4554 "got_object_id_str");
4556 if (got_object_tree_entry_is_submodule(te))
4557 modestr = "$";
4558 else if (S_ISLNK(mode))
4559 modestr = "@";
4560 else if (S_ISDIR(mode))
4561 modestr = "/";
4562 else if (mode & S_IXUSR)
4563 modestr = "*";
4564 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4565 got_tree_entry_get_name(te), modestr) == -1) {
4566 free(id_str);
4567 return got_error_from_errno("asprintf");
4569 free(id_str);
4570 err = format_line(&wline, &width, line, view->ncols, 0);
4571 if (err) {
4572 free(line);
4573 break;
4575 if (n == selected) {
4576 if (view->focussed)
4577 wstandout(view->window);
4578 *selected_entry = te;
4580 tc = match_color(colors, line);
4581 if (tc)
4582 wattr_on(view->window,
4583 COLOR_PAIR(tc->colorpair), NULL);
4584 waddwstr(view->window, wline);
4585 if (tc)
4586 wattr_off(view->window,
4587 COLOR_PAIR(tc->colorpair), NULL);
4588 if (width < view->ncols - 1)
4589 waddch(view->window, '\n');
4590 if (n == selected && view->focussed)
4591 wstandend(view->window);
4592 free(line);
4593 free(wline);
4594 wline = NULL;
4595 n++;
4596 (*ndisplayed)++;
4597 *last_displayed_entry = te;
4598 if (--limit <= 0)
4599 break;
4602 return err;
4605 static void
4606 tree_scroll_up(struct tog_view *view,
4607 struct got_tree_entry **first_displayed_entry, int maxscroll,
4608 struct got_tree_object *tree, int isroot)
4610 struct got_tree_entry *te;
4611 int i;
4613 if (*first_displayed_entry == NULL)
4614 return;
4616 te = got_object_tree_get_entry(tree, 0);
4617 if (*first_displayed_entry == te) {
4618 if (!isroot)
4619 *first_displayed_entry = NULL;
4620 return;
4623 i = 0;
4624 while (*first_displayed_entry && i < maxscroll) {
4625 *first_displayed_entry = got_tree_entry_get_prev(tree,
4626 *first_displayed_entry);
4627 i++;
4629 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4630 *first_displayed_entry = NULL;
4633 static int
4634 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4635 struct got_tree_entry *last_displayed_entry,
4636 struct got_tree_object *tree)
4638 struct got_tree_entry *next, *last;
4639 int n = 0;
4641 if (*first_displayed_entry)
4642 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4643 else
4644 next = got_object_tree_get_first_entry(tree);
4646 last = last_displayed_entry;
4647 while (next && last && n++ < maxscroll) {
4648 last = got_tree_entry_get_next(tree, last);
4649 if (last) {
4650 *first_displayed_entry = next;
4651 next = got_tree_entry_get_next(tree, next);
4654 return n;
4657 static const struct got_error *
4658 tree_entry_path(char **path, struct tog_parent_trees *parents,
4659 struct got_tree_entry *te)
4661 const struct got_error *err = NULL;
4662 struct tog_parent_tree *pt;
4663 size_t len = 2; /* for leading slash and NUL */
4665 TAILQ_FOREACH(pt, parents, entry)
4666 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4667 + 1 /* slash */;
4668 if (te)
4669 len += strlen(got_tree_entry_get_name(te));
4671 *path = calloc(1, len);
4672 if (path == NULL)
4673 return got_error_from_errno("calloc");
4675 (*path)[0] = '/';
4676 pt = TAILQ_LAST(parents, tog_parent_trees);
4677 while (pt) {
4678 const char *name = got_tree_entry_get_name(pt->selected_entry);
4679 if (strlcat(*path, name, len) >= len) {
4680 err = got_error(GOT_ERR_NO_SPACE);
4681 goto done;
4683 if (strlcat(*path, "/", len) >= len) {
4684 err = got_error(GOT_ERR_NO_SPACE);
4685 goto done;
4687 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4689 if (te) {
4690 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4691 err = got_error(GOT_ERR_NO_SPACE);
4692 goto done;
4695 done:
4696 if (err) {
4697 free(*path);
4698 *path = NULL;
4700 return err;
4703 static const struct got_error *
4704 blame_tree_entry(struct tog_view **new_view, int begin_x,
4705 struct got_tree_entry *te, struct tog_parent_trees *parents,
4706 struct got_object_id *commit_id, struct got_reflist_head *refs,
4707 struct got_repository *repo)
4709 const struct got_error *err = NULL;
4710 char *path;
4711 struct tog_view *blame_view;
4713 *new_view = NULL;
4715 err = tree_entry_path(&path, parents, te);
4716 if (err)
4717 return err;
4719 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4720 if (blame_view == NULL) {
4721 err = got_error_from_errno("view_open");
4722 goto done;
4725 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4726 if (err) {
4727 if (err->code == GOT_ERR_CANCELLED)
4728 err = NULL;
4729 view_close(blame_view);
4730 } else
4731 *new_view = blame_view;
4732 done:
4733 free(path);
4734 return err;
4737 static const struct got_error *
4738 log_tree_entry(struct tog_view **new_view, int begin_x,
4739 struct got_tree_entry *te, struct tog_parent_trees *parents,
4740 struct got_object_id *commit_id, struct got_reflist_head *refs,
4741 struct got_repository *repo)
4743 struct tog_view *log_view;
4744 const struct got_error *err = NULL;
4745 char *path;
4747 *new_view = NULL;
4749 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4750 if (log_view == NULL)
4751 return got_error_from_errno("view_open");
4753 err = tree_entry_path(&path, parents, te);
4754 if (err)
4755 return err;
4757 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4758 if (err)
4759 view_close(log_view);
4760 else
4761 *new_view = log_view;
4762 free(path);
4763 return err;
4766 static const struct got_error *
4767 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4768 struct got_object_id *commit_id, struct got_reflist_head *refs,
4769 struct got_repository *repo)
4771 const struct got_error *err = NULL;
4772 char *commit_id_str = NULL;
4773 struct tog_tree_view_state *s = &view->state.tree;
4775 TAILQ_INIT(&s->parents);
4777 err = got_object_id_str(&commit_id_str, commit_id);
4778 if (err != NULL)
4779 goto done;
4781 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4782 err = got_error_from_errno("asprintf");
4783 goto done;
4786 s->root = s->tree = root;
4787 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4788 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4789 s->commit_id = got_object_id_dup(commit_id);
4790 if (s->commit_id == NULL) {
4791 err = got_error_from_errno("got_object_id_dup");
4792 goto done;
4794 s->refs = refs;
4795 s->repo = repo;
4797 SIMPLEQ_INIT(&s->colors);
4799 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4800 err = add_color(&s->colors, "\\$$",
4801 TOG_COLOR_TREE_SUBMODULE,
4802 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4803 if (err)
4804 goto done;
4805 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4806 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4807 if (err) {
4808 free_colors(&s->colors);
4809 goto done;
4811 err = add_color(&s->colors, "/$",
4812 TOG_COLOR_TREE_DIRECTORY,
4813 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4814 if (err) {
4815 free_colors(&s->colors);
4816 goto done;
4819 err = add_color(&s->colors, "\\*$",
4820 TOG_COLOR_TREE_EXECUTABLE,
4821 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4822 if (err) {
4823 free_colors(&s->colors);
4824 goto done;
4827 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4828 get_color_value("TOG_COLOR_COMMIT"));
4829 if (err) {
4830 free_colors(&s->colors);
4831 goto done;
4835 view->show = show_tree_view;
4836 view->input = input_tree_view;
4837 view->close = close_tree_view;
4838 view->search_start = search_start_tree_view;
4839 view->search_next = search_next_tree_view;
4840 done:
4841 free(commit_id_str);
4842 if (err) {
4843 free(s->tree_label);
4844 s->tree_label = NULL;
4846 return err;
4849 static const struct got_error *
4850 close_tree_view(struct tog_view *view)
4852 struct tog_tree_view_state *s = &view->state.tree;
4854 free_colors(&s->colors);
4855 free(s->tree_label);
4856 s->tree_label = NULL;
4857 free(s->commit_id);
4858 s->commit_id = NULL;
4859 while (!TAILQ_EMPTY(&s->parents)) {
4860 struct tog_parent_tree *parent;
4861 parent = TAILQ_FIRST(&s->parents);
4862 TAILQ_REMOVE(&s->parents, parent, entry);
4863 free(parent);
4866 if (s->tree != s->root)
4867 got_object_tree_close(s->tree);
4868 got_object_tree_close(s->root);
4870 return NULL;
4873 static const struct got_error *
4874 search_start_tree_view(struct tog_view *view)
4876 struct tog_tree_view_state *s = &view->state.tree;
4878 s->matched_entry = NULL;
4879 return NULL;
4882 static int
4883 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4885 regmatch_t regmatch;
4887 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4888 0) == 0;
4891 static const struct got_error *
4892 search_next_tree_view(struct tog_view *view)
4894 struct tog_tree_view_state *s = &view->state.tree;
4895 struct got_tree_entry *te = NULL;
4897 if (!view->searching) {
4898 view->search_next_done = 1;
4899 return NULL;
4902 if (s->matched_entry) {
4903 if (view->searching == TOG_SEARCH_FORWARD) {
4904 if (s->selected_entry)
4905 te = got_tree_entry_get_next(s->tree,
4906 s->selected_entry);
4907 else
4908 te = got_object_tree_get_first_entry(s->tree);
4909 } else {
4910 if (s->selected_entry == NULL)
4911 te = got_object_tree_get_last_entry(s->tree);
4912 else
4913 te = got_tree_entry_get_prev(s->tree,
4914 s->selected_entry);
4916 } else {
4917 if (view->searching == TOG_SEARCH_FORWARD)
4918 te = got_object_tree_get_first_entry(s->tree);
4919 else
4920 te = got_object_tree_get_last_entry(s->tree);
4923 while (1) {
4924 if (te == NULL) {
4925 if (s->matched_entry == NULL) {
4926 view->search_next_done = 1;
4927 return NULL;
4929 if (view->searching == TOG_SEARCH_FORWARD)
4930 te = got_object_tree_get_first_entry(s->tree);
4931 else
4932 te = got_object_tree_get_last_entry(s->tree);
4935 if (match_tree_entry(te, &view->regex)) {
4936 view->search_next_done = 1;
4937 s->matched_entry = te;
4938 break;
4941 if (view->searching == TOG_SEARCH_FORWARD)
4942 te = got_tree_entry_get_next(s->tree, te);
4943 else
4944 te = got_tree_entry_get_prev(s->tree, te);
4947 if (s->matched_entry) {
4948 s->first_displayed_entry = s->matched_entry;
4949 s->selected = 0;
4952 return NULL;
4955 static const struct got_error *
4956 show_tree_view(struct tog_view *view)
4958 const struct got_error *err = NULL;
4959 struct tog_tree_view_state *s = &view->state.tree;
4960 char *parent_path;
4962 err = tree_entry_path(&parent_path, &s->parents, NULL);
4963 if (err)
4964 return err;
4966 err = draw_tree_entries(view, &s->first_displayed_entry,
4967 &s->last_displayed_entry, &s->selected_entry,
4968 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4969 s->tree, s->selected, view->nlines, s->tree == s->root,
4970 &s->colors);
4971 free(parent_path);
4973 view_vborder(view);
4974 return err;
4977 static const struct got_error *
4978 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4979 struct tog_view **focus_view, struct tog_view *view, int ch)
4981 const struct got_error *err = NULL;
4982 struct tog_tree_view_state *s = &view->state.tree;
4983 struct tog_view *log_view;
4984 int begin_x = 0, nscrolled;
4986 switch (ch) {
4987 case 'i':
4988 s->show_ids = !s->show_ids;
4989 break;
4990 case 'l':
4991 if (!s->selected_entry)
4992 break;
4993 if (view_is_parent_view(view))
4994 begin_x = view_split_begin_x(view->begin_x);
4995 err = log_tree_entry(&log_view, begin_x,
4996 s->selected_entry, &s->parents,
4997 s->commit_id, s->refs, s->repo);
4998 if (view_is_parent_view(view)) {
4999 err = view_close_child(view);
5000 if (err)
5001 return err;
5002 err = view_set_child(view, log_view);
5003 if (err) {
5004 view_close(log_view);
5005 break;
5007 *focus_view = log_view;
5008 view->child_focussed = 1;
5009 } else
5010 *new_view = log_view;
5011 break;
5012 case 'k':
5013 case KEY_UP:
5014 if (s->selected > 0) {
5015 s->selected--;
5016 if (s->selected == 0)
5017 break;
5019 if (s->selected > 0)
5020 break;
5021 tree_scroll_up(view, &s->first_displayed_entry, 1,
5022 s->tree, s->tree == s->root);
5023 break;
5024 case KEY_PPAGE:
5025 tree_scroll_up(view, &s->first_displayed_entry,
5026 MAX(0, view->nlines - 4 - s->selected), s->tree,
5027 s->tree == s->root);
5028 s->selected = 0;
5029 if (got_object_tree_get_first_entry(s->tree) ==
5030 s->first_displayed_entry && s->tree != s->root)
5031 s->first_displayed_entry = NULL;
5032 break;
5033 case 'j':
5034 case KEY_DOWN:
5035 if (s->selected < s->ndisplayed - 1) {
5036 s->selected++;
5037 break;
5039 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5040 == NULL)
5041 /* can't scroll any further */
5042 break;
5043 tree_scroll_down(&s->first_displayed_entry, 1,
5044 s->last_displayed_entry, s->tree);
5045 break;
5046 case KEY_NPAGE:
5047 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5048 == NULL) {
5049 /* can't scroll any further; move cursor down */
5050 if (s->selected < s->ndisplayed - 1)
5051 s->selected = s->ndisplayed - 1;
5052 break;
5054 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5055 view->nlines, s->last_displayed_entry, s->tree);
5056 if (nscrolled < view->nlines) {
5057 int ndisplayed = 0;
5058 struct got_tree_entry *te;
5059 te = s->first_displayed_entry;
5060 do {
5061 ndisplayed++;
5062 te = got_tree_entry_get_next(s->tree, te);
5063 } while (te);
5064 s->selected = ndisplayed - 1;
5066 break;
5067 case KEY_ENTER:
5068 case '\r':
5069 case KEY_BACKSPACE:
5070 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5071 struct tog_parent_tree *parent;
5072 /* user selected '..' */
5073 if (s->tree == s->root)
5074 break;
5075 parent = TAILQ_FIRST(&s->parents);
5076 TAILQ_REMOVE(&s->parents, parent,
5077 entry);
5078 got_object_tree_close(s->tree);
5079 s->tree = parent->tree;
5080 s->first_displayed_entry =
5081 parent->first_displayed_entry;
5082 s->selected_entry =
5083 parent->selected_entry;
5084 s->selected = parent->selected;
5085 free(parent);
5086 } else if (S_ISDIR(got_tree_entry_get_mode(
5087 s->selected_entry))) {
5088 struct got_tree_object *subtree;
5089 err = got_object_open_as_tree(&subtree, s->repo,
5090 got_tree_entry_get_id(s->selected_entry));
5091 if (err)
5092 break;
5093 err = tree_view_visit_subtree(subtree, s);
5094 if (err) {
5095 got_object_tree_close(subtree);
5096 break;
5098 } else if (S_ISREG(got_tree_entry_get_mode(
5099 s->selected_entry))) {
5100 struct tog_view *blame_view;
5101 int begin_x = view_is_parent_view(view) ?
5102 view_split_begin_x(view->begin_x) : 0;
5104 err = blame_tree_entry(&blame_view, begin_x,
5105 s->selected_entry, &s->parents,
5106 s->commit_id, s->refs, s->repo);
5107 if (err)
5108 break;
5109 if (view_is_parent_view(view)) {
5110 err = view_close_child(view);
5111 if (err)
5112 return err;
5113 err = view_set_child(view, blame_view);
5114 if (err) {
5115 view_close(blame_view);
5116 break;
5118 *focus_view = blame_view;
5119 view->child_focussed = 1;
5120 } else
5121 *new_view = blame_view;
5123 break;
5124 case KEY_RESIZE:
5125 if (s->selected > view->nlines)
5126 s->selected = s->ndisplayed - 1;
5127 break;
5128 default:
5129 break;
5132 return err;
5135 __dead static void
5136 usage_tree(void)
5138 endwin();
5139 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
5140 getprogname());
5141 exit(1);
5144 static const struct got_error *
5145 cmd_tree(int argc, char *argv[])
5147 const struct got_error *error;
5148 struct got_repository *repo = NULL;
5149 struct got_reflist_head refs;
5150 char *repo_path = NULL;
5151 struct got_object_id *commit_id = NULL;
5152 char *commit_id_arg = NULL;
5153 struct got_commit_object *commit = NULL;
5154 struct got_tree_object *tree = NULL;
5155 int ch;
5156 struct tog_view *view;
5158 SIMPLEQ_INIT(&refs);
5160 #ifndef PROFILE
5161 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5162 NULL) == -1)
5163 err(1, "pledge");
5164 #endif
5166 while ((ch = getopt(argc, argv, "c:")) != -1) {
5167 switch (ch) {
5168 case 'c':
5169 commit_id_arg = optarg;
5170 break;
5171 default:
5172 usage_tree();
5173 /* NOTREACHED */
5177 argc -= optind;
5178 argv += optind;
5180 if (argc == 0) {
5181 struct got_worktree *worktree;
5182 char *cwd = getcwd(NULL, 0);
5183 if (cwd == NULL)
5184 return got_error_from_errno("getcwd");
5185 error = got_worktree_open(&worktree, cwd);
5186 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5187 goto done;
5188 if (worktree) {
5189 free(cwd);
5190 repo_path =
5191 strdup(got_worktree_get_repo_path(worktree));
5192 got_worktree_close(worktree);
5193 } else
5194 repo_path = cwd;
5195 if (repo_path == NULL) {
5196 error = got_error_from_errno("strdup");
5197 goto done;
5199 } else if (argc == 1) {
5200 repo_path = realpath(argv[0], NULL);
5201 if (repo_path == NULL)
5202 return got_error_from_errno2("realpath", argv[0]);
5203 } else
5204 usage_tree();
5206 init_curses();
5208 error = got_repo_open(&repo, repo_path, NULL);
5209 if (error != NULL)
5210 goto done;
5212 error = apply_unveil(got_repo_get_path(repo), NULL);
5213 if (error)
5214 goto done;
5216 error = got_repo_match_object_id(&commit_id, NULL,
5217 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5218 GOT_OBJ_TYPE_COMMIT, 1, repo);
5219 if (error != NULL)
5220 goto done;
5222 error = got_object_open_as_commit(&commit, repo, commit_id);
5223 if (error != NULL)
5224 goto done;
5226 error = got_object_open_as_tree(&tree, repo,
5227 got_object_commit_get_tree_id(commit));
5228 if (error != NULL)
5229 goto done;
5231 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5232 if (error)
5233 goto done;
5235 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5236 if (view == NULL) {
5237 error = got_error_from_errno("view_open");
5238 goto done;
5240 error = open_tree_view(view, tree, commit_id, &refs, repo);
5241 if (error)
5242 goto done;
5243 error = view_loop(view);
5244 done:
5245 free(repo_path);
5246 free(commit_id);
5247 if (commit)
5248 got_object_commit_close(commit);
5249 if (tree)
5250 got_object_tree_close(tree);
5251 if (repo)
5252 got_repo_close(repo);
5253 got_ref_list_free(&refs);
5254 return error;
5257 static void
5258 list_commands(void)
5260 int i;
5262 fprintf(stderr, "commands:");
5263 for (i = 0; i < nitems(tog_commands); i++) {
5264 struct tog_cmd *cmd = &tog_commands[i];
5265 fprintf(stderr, " %s", cmd->name);
5267 fputc('\n', stderr);
5270 __dead static void
5271 usage(int hflag)
5273 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5274 getprogname());
5275 if (hflag)
5276 list_commands();
5277 exit(1);
5280 static char **
5281 make_argv(const char *arg0, const char *arg1)
5283 char **argv;
5284 int argc = (arg1 == NULL ? 1 : 2);
5286 argv = calloc(argc, sizeof(char *));
5287 if (argv == NULL)
5288 err(1, "calloc");
5289 argv[0] = strdup(arg0);
5290 if (argv[0] == NULL)
5291 err(1, "strdup");
5292 if (arg1) {
5293 argv[1] = strdup(arg1);
5294 if (argv[1] == NULL)
5295 err(1, "strdup");
5298 return argv;
5301 int
5302 main(int argc, char *argv[])
5304 const struct got_error *error = NULL;
5305 struct tog_cmd *cmd = NULL;
5306 int ch, hflag = 0, Vflag = 0;
5307 char **cmd_argv = NULL;
5308 static struct option longopts[] = {
5309 { "version", no_argument, NULL, 'V' },
5310 { NULL, 0, NULL, 0}
5313 setlocale(LC_CTYPE, "");
5315 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5316 switch (ch) {
5317 case 'h':
5318 hflag = 1;
5319 break;
5320 case 'V':
5321 Vflag = 1;
5322 break;
5323 default:
5324 usage(hflag);
5325 /* NOTREACHED */
5329 argc -= optind;
5330 argv += optind;
5331 optind = 0;
5332 optreset = 1;
5334 if (Vflag) {
5335 got_version_print_str();
5336 return 1;
5339 if (argc == 0) {
5340 if (hflag)
5341 usage(hflag);
5342 /* Build an argument vector which runs a default command. */
5343 cmd = &tog_commands[0];
5344 cmd_argv = make_argv(cmd->name, NULL);
5345 argc = 1;
5346 } else {
5347 int i;
5349 /* Did the user specific a command? */
5350 for (i = 0; i < nitems(tog_commands); i++) {
5351 if (strncmp(tog_commands[i].name, argv[0],
5352 strlen(argv[0])) == 0) {
5353 cmd = &tog_commands[i];
5354 break;
5358 if (cmd == NULL) {
5359 fprintf(stderr, "%s: unknown command '%s'\n",
5360 getprogname(), argv[0]);
5361 list_commands();
5362 return 1;
5366 if (hflag)
5367 cmd->cmd_usage();
5368 else
5369 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5371 endwin();
5372 free(cmd_argv);
5373 if (error && error->code != GOT_ERR_CANCELLED)
5374 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5375 return 0;