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 errcode = pthread_mutex_unlock(&tog_mutex);
759 if (errcode)
760 return got_error_set_errno(errcode,
761 "pthread_mutex_unlock");
762 pthread_yield();
763 errcode = pthread_mutex_lock(&tog_mutex);
764 if (errcode)
765 return got_error_set_errno(errcode,
766 "pthread_mutex_lock");
767 view->search_next(view);
768 return NULL;
771 nodelay(stdscr, FALSE);
772 /* Allow threads to make progress while we are waiting for input. */
773 errcode = pthread_mutex_unlock(&tog_mutex);
774 if (errcode)
775 return got_error_set_errno(errcode, "pthread_mutex_unlock");
776 ch = wgetch(view->window);
777 errcode = pthread_mutex_lock(&tog_mutex);
778 if (errcode)
779 return got_error_set_errno(errcode, "pthread_mutex_lock");
780 nodelay(stdscr, TRUE);
782 if (tog_sigwinch_received || tog_sigcont_received) {
783 tog_resizeterm();
784 tog_sigwinch_received = 0;
785 tog_sigcont_received = 0;
786 TAILQ_FOREACH(v, views, entry) {
787 err = view_resize(v);
788 if (err)
789 return err;
790 err = v->input(new, dead, focus, v, KEY_RESIZE);
791 if (err)
792 return err;
796 switch (ch) {
797 case ERR:
798 break;
799 case '\t':
800 if (view->child) {
801 *focus = view->child;
802 view->child_focussed = 1;
803 } else if (view->parent) {
804 *focus = view->parent;
805 view->parent->child_focussed = 0;
807 break;
808 case 'q':
809 err = view->input(new, dead, focus, view, ch);
810 *dead = view;
811 break;
812 case 'Q':
813 *done = 1;
814 break;
815 case 'f':
816 if (view_is_parent_view(view)) {
817 if (view->child == NULL)
818 break;
819 if (view_is_splitscreen(view->child)) {
820 *focus = view->child;
821 view->child_focussed = 1;
822 err = view_fullscreen(view->child);
823 } else
824 err = view_splitscreen(view->child);
825 if (err)
826 break;
827 err = view->child->input(new, dead, focus,
828 view->child, KEY_RESIZE);
829 } else {
830 if (view_is_splitscreen(view)) {
831 *focus = view;
832 view->parent->child_focussed = 1;
833 err = view_fullscreen(view);
834 } else {
835 err = view_splitscreen(view);
837 if (err)
838 break;
839 err = view->input(new, dead, focus, view,
840 KEY_RESIZE);
842 break;
843 case KEY_RESIZE:
844 break;
845 case '/':
846 if (view->search_start)
847 view_search_start(view);
848 else
849 err = view->input(new, dead, focus, view, ch);
850 break;
851 case 'N':
852 case 'n':
853 if (view->search_next && view->searching) {
854 view->searching = (ch == 'n' ?
855 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
856 view->search_next_done = 0;
857 view->search_next(view);
858 } else
859 err = view->input(new, dead, focus, view, ch);
860 break;
861 default:
862 err = view->input(new, dead, focus, view, ch);
863 break;
866 return err;
869 void
870 view_vborder(struct tog_view *view)
872 PANEL *panel;
873 struct tog_view *view_above;
875 if (view->parent)
876 return view_vborder(view->parent);
878 panel = panel_above(view->panel);
879 if (panel == NULL)
880 return;
882 view_above = panel_userptr(panel);
883 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
884 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
887 int
888 view_needs_focus_indication(struct tog_view *view)
890 if (view_is_parent_view(view)) {
891 if (view->child == NULL || view->child_focussed)
892 return 0;
893 if (!view_is_splitscreen(view->child))
894 return 0;
895 } else if (!view_is_splitscreen(view))
896 return 0;
898 return view->focussed;
901 static const struct got_error *
902 view_loop(struct tog_view *view)
904 const struct got_error *err = NULL;
905 struct tog_view_list_head views;
906 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
907 int fast_refresh = 10;
908 int done = 0, errcode;
910 errcode = pthread_mutex_lock(&tog_mutex);
911 if (errcode)
912 return got_error_set_errno(errcode, "pthread_mutex_lock");
914 TAILQ_INIT(&views);
915 TAILQ_INSERT_HEAD(&views, view, entry);
917 main_view = view;
918 view->focussed = 1;
919 err = view->show(view);
920 if (err)
921 return err;
922 update_panels();
923 doupdate();
924 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
925 /* Refresh fast during initialization, then become slower. */
926 if (fast_refresh && fast_refresh-- == 0)
927 halfdelay(10); /* switch to once per second */
929 err = view_input(&new_view, &dead_view, &focus_view, &done,
930 view, &views);
931 if (err)
932 break;
933 if (dead_view) {
934 struct tog_view *prev = NULL;
936 if (view_is_parent_view(dead_view))
937 prev = TAILQ_PREV(dead_view,
938 tog_view_list_head, entry);
939 else if (view->parent != dead_view)
940 prev = view->parent;
942 if (dead_view->parent)
943 dead_view->parent->child = NULL;
944 else
945 TAILQ_REMOVE(&views, dead_view, entry);
947 err = view_close(dead_view);
948 if (err || (dead_view == main_view && new_view == NULL))
949 goto done;
951 if (view == dead_view) {
952 if (focus_view)
953 view = focus_view;
954 else if (prev)
955 view = prev;
956 else if (!TAILQ_EMPTY(&views))
957 view = TAILQ_LAST(&views,
958 tog_view_list_head);
959 else
960 view = NULL;
961 if (view) {
962 if (view->child && view->child_focussed)
963 focus_view = view->child;
964 else
965 focus_view = view;
969 if (new_view) {
970 struct tog_view *v, *t;
971 /* Only allow one parent view per type. */
972 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
973 if (v->type != new_view->type)
974 continue;
975 TAILQ_REMOVE(&views, v, entry);
976 err = view_close(v);
977 if (err)
978 goto done;
979 break;
981 TAILQ_INSERT_TAIL(&views, new_view, entry);
982 view = new_view;
983 if (focus_view == NULL)
984 focus_view = new_view;
986 if (focus_view) {
987 show_panel(focus_view->panel);
988 if (view)
989 view->focussed = 0;
990 focus_view->focussed = 1;
991 view = focus_view;
992 if (new_view)
993 show_panel(new_view->panel);
994 if (view->child && view_is_splitscreen(view->child))
995 show_panel(view->child->panel);
997 if (view) {
998 if (focus_view == NULL) {
999 view->focussed = 1;
1000 show_panel(view->panel);
1001 if (view->child && view_is_splitscreen(view->child))
1002 show_panel(view->child->panel);
1003 focus_view = view;
1005 if (view->parent) {
1006 err = view->parent->show(view->parent);
1007 if (err)
1008 goto done;
1010 err = view->show(view);
1011 if (err)
1012 goto done;
1013 if (view->child) {
1014 err = view->child->show(view->child);
1015 if (err)
1016 goto done;
1018 update_panels();
1019 doupdate();
1022 done:
1023 while (!TAILQ_EMPTY(&views)) {
1024 view = TAILQ_FIRST(&views);
1025 TAILQ_REMOVE(&views, view, entry);
1026 view_close(view);
1029 errcode = pthread_mutex_unlock(&tog_mutex);
1030 if (errcode && err == NULL)
1031 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1033 return err;
1036 __dead static void
1037 usage_log(void)
1039 endwin();
1040 fprintf(stderr,
1041 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1042 getprogname());
1043 exit(1);
1046 /* Create newly allocated wide-character string equivalent to a byte string. */
1047 static const struct got_error *
1048 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1050 char *vis = NULL;
1051 const struct got_error *err = NULL;
1053 *ws = NULL;
1054 *wlen = mbstowcs(NULL, s, 0);
1055 if (*wlen == (size_t)-1) {
1056 int vislen;
1057 if (errno != EILSEQ)
1058 return got_error_from_errno("mbstowcs");
1060 /* byte string invalid in current encoding; try to "fix" it */
1061 err = got_mbsavis(&vis, &vislen, s);
1062 if (err)
1063 return err;
1064 *wlen = mbstowcs(NULL, vis, 0);
1065 if (*wlen == (size_t)-1) {
1066 err = got_error_from_errno("mbstowcs"); /* give up */
1067 goto done;
1071 *ws = calloc(*wlen + 1, sizeof(**ws));
1072 if (*ws == NULL) {
1073 err = got_error_from_errno("calloc");
1074 goto done;
1077 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1078 err = got_error_from_errno("mbstowcs");
1079 done:
1080 free(vis);
1081 if (err) {
1082 free(*ws);
1083 *ws = NULL;
1084 *wlen = 0;
1086 return err;
1089 /* Format a line for display, ensuring that it won't overflow a width limit. */
1090 static const struct got_error *
1091 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1092 int col_tab_align)
1094 const struct got_error *err = NULL;
1095 int cols = 0;
1096 wchar_t *wline = NULL;
1097 size_t wlen;
1098 int i;
1100 *wlinep = NULL;
1101 *widthp = 0;
1103 err = mbs2ws(&wline, &wlen, line);
1104 if (err)
1105 return err;
1107 i = 0;
1108 while (i < wlen) {
1109 int width = wcwidth(wline[i]);
1111 if (width == 0) {
1112 i++;
1113 continue;
1116 if (width == 1 || width == 2) {
1117 if (cols + width > wlimit)
1118 break;
1119 cols += width;
1120 i++;
1121 } else if (width == -1) {
1122 if (wline[i] == L'\t') {
1123 width = TABSIZE -
1124 ((cols + col_tab_align) % TABSIZE);
1125 if (cols + width > wlimit)
1126 break;
1127 cols += width;
1129 i++;
1130 } else {
1131 err = got_error_from_errno("wcwidth");
1132 goto done;
1135 wline[i] = L'\0';
1136 if (widthp)
1137 *widthp = cols;
1138 done:
1139 if (err)
1140 free(wline);
1141 else
1142 *wlinep = wline;
1143 return err;
1146 static const struct got_error*
1147 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1148 struct got_object_id *id, struct got_repository *repo)
1150 static const struct got_error *err = NULL;
1151 struct got_reflist_entry *re;
1152 char *s;
1153 const char *name;
1155 *refs_str = NULL;
1157 SIMPLEQ_FOREACH(re, refs, entry) {
1158 struct got_tag_object *tag = NULL;
1159 int cmp;
1161 name = got_ref_get_name(re->ref);
1162 if (strcmp(name, GOT_REF_HEAD) == 0)
1163 continue;
1164 if (strncmp(name, "refs/", 5) == 0)
1165 name += 5;
1166 if (strncmp(name, "got/", 4) == 0)
1167 continue;
1168 if (strncmp(name, "heads/", 6) == 0)
1169 name += 6;
1170 if (strncmp(name, "remotes/", 8) == 0)
1171 name += 8;
1172 if (strncmp(name, "tags/", 5) == 0) {
1173 err = got_object_open_as_tag(&tag, repo, re->id);
1174 if (err) {
1175 if (err->code != GOT_ERR_OBJ_TYPE)
1176 break;
1177 /* Ref points at something other than a tag. */
1178 err = NULL;
1179 tag = NULL;
1182 cmp = got_object_id_cmp(tag ?
1183 got_object_tag_get_object_id(tag) : re->id, id);
1184 if (tag)
1185 got_object_tag_close(tag);
1186 if (cmp != 0)
1187 continue;
1188 s = *refs_str;
1189 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1190 s ? ", " : "", name) == -1) {
1191 err = got_error_from_errno("asprintf");
1192 free(s);
1193 *refs_str = NULL;
1194 break;
1196 free(s);
1199 return err;
1202 static const struct got_error *
1203 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1204 int col_tab_align)
1206 char *smallerthan, *at;
1208 smallerthan = strchr(author, '<');
1209 if (smallerthan && smallerthan[1] != '\0')
1210 author = smallerthan + 1;
1211 at = strchr(author, '@');
1212 if (at)
1213 *at = '\0';
1214 return format_line(wauthor, author_width, author, limit, col_tab_align);
1217 static const struct got_error *
1218 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1219 struct got_object_id *id, struct got_reflist_head *refs,
1220 const size_t date_display_cols, int author_display_cols,
1221 struct tog_colors *colors)
1223 const struct got_error *err = NULL;
1224 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1225 char *logmsg0 = NULL, *logmsg = NULL;
1226 char *author = NULL;
1227 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1228 int author_width, logmsg_width;
1229 char *newline, *line = NULL;
1230 int col, limit;
1231 const int avail = view->ncols;
1232 struct tm tm;
1233 time_t committer_time;
1234 struct tog_color *tc;
1236 committer_time = got_object_commit_get_committer_time(commit);
1237 if (localtime_r(&committer_time, &tm) == NULL)
1238 return got_error_from_errno("localtime_r");
1239 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1240 >= sizeof(datebuf))
1241 return got_error(GOT_ERR_NO_SPACE);
1243 if (avail <= date_display_cols)
1244 limit = MIN(sizeof(datebuf) - 1, avail);
1245 else
1246 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1247 tc = get_color(colors, TOG_COLOR_DATE);
1248 if (tc)
1249 wattr_on(view->window,
1250 COLOR_PAIR(tc->colorpair), NULL);
1251 waddnstr(view->window, datebuf, limit);
1252 if (tc)
1253 wattr_off(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1255 col = limit;
1256 if (col > avail)
1257 goto done;
1259 if (avail >= 120) {
1260 char *id_str;
1261 err = got_object_id_str(&id_str, id);
1262 if (err)
1263 goto done;
1264 tc = get_color(colors, TOG_COLOR_COMMIT);
1265 if (tc)
1266 wattr_on(view->window,
1267 COLOR_PAIR(tc->colorpair), NULL);
1268 wprintw(view->window, "%.8s ", id_str);
1269 if (tc)
1270 wattr_off(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1272 free(id_str);
1273 col += 9;
1274 if (col > avail)
1275 goto done;
1278 author = strdup(got_object_commit_get_author(commit));
1279 if (author == NULL) {
1280 err = got_error_from_errno("strdup");
1281 goto done;
1283 err = format_author(&wauthor, &author_width, author, avail - col, col);
1284 if (err)
1285 goto done;
1286 tc = get_color(colors, TOG_COLOR_AUTHOR);
1287 if (tc)
1288 wattr_on(view->window,
1289 COLOR_PAIR(tc->colorpair), NULL);
1290 waddwstr(view->window, wauthor);
1291 if (tc)
1292 wattr_off(view->window,
1293 COLOR_PAIR(tc->colorpair), NULL);
1294 col += author_width;
1295 while (col < avail && author_width < author_display_cols + 2) {
1296 waddch(view->window, ' ');
1297 col++;
1298 author_width++;
1300 if (col > avail)
1301 goto done;
1303 err = got_object_commit_get_logmsg(&logmsg0, commit);
1304 if (err)
1305 goto done;
1306 logmsg = logmsg0;
1307 while (*logmsg == '\n')
1308 logmsg++;
1309 newline = strchr(logmsg, '\n');
1310 if (newline)
1311 *newline = '\0';
1312 limit = avail - col;
1313 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1314 if (err)
1315 goto done;
1316 waddwstr(view->window, wlogmsg);
1317 col += logmsg_width;
1318 while (col < avail) {
1319 waddch(view->window, ' ');
1320 col++;
1322 done:
1323 free(logmsg0);
1324 free(wlogmsg);
1325 free(author);
1326 free(wauthor);
1327 free(line);
1328 return err;
1331 static struct commit_queue_entry *
1332 alloc_commit_queue_entry(struct got_commit_object *commit,
1333 struct got_object_id *id)
1335 struct commit_queue_entry *entry;
1337 entry = calloc(1, sizeof(*entry));
1338 if (entry == NULL)
1339 return NULL;
1341 entry->id = id;
1342 entry->commit = commit;
1343 return entry;
1346 static void
1347 pop_commit(struct commit_queue *commits)
1349 struct commit_queue_entry *entry;
1351 entry = TAILQ_FIRST(&commits->head);
1352 TAILQ_REMOVE(&commits->head, entry, entry);
1353 got_object_commit_close(entry->commit);
1354 commits->ncommits--;
1355 /* Don't free entry->id! It is owned by the commit graph. */
1356 free(entry);
1359 static void
1360 free_commits(struct commit_queue *commits)
1362 while (!TAILQ_EMPTY(&commits->head))
1363 pop_commit(commits);
1366 static const struct got_error *
1367 match_commit(int *have_match, struct got_object_id *id,
1368 struct got_commit_object *commit, regex_t *regex)
1370 const struct got_error *err = NULL;
1371 regmatch_t regmatch;
1372 char *id_str = NULL, *logmsg = NULL;
1374 *have_match = 0;
1376 err = got_object_id_str(&id_str, id);
1377 if (err)
1378 return err;
1380 err = got_object_commit_get_logmsg(&logmsg, commit);
1381 if (err)
1382 goto done;
1384 if (regexec(regex, got_object_commit_get_author(commit), 1,
1385 &regmatch, 0) == 0 ||
1386 regexec(regex, got_object_commit_get_committer(commit), 1,
1387 &regmatch, 0) == 0 ||
1388 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1389 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1390 *have_match = 1;
1391 done:
1392 free(id_str);
1393 free(logmsg);
1394 return err;
1397 static const struct got_error *
1398 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1399 int minqueue, struct got_repository *repo, const char *path,
1400 int *searching, int *search_next_done, regex_t *regex)
1402 const struct got_error *err = NULL;
1403 int nqueued = 0, have_match = 0;
1406 * We keep all commits open throughout the lifetime of the log
1407 * view in order to avoid having to re-fetch commits from disk
1408 * while updating the display.
1410 while (nqueued < minqueue ||
1411 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1412 struct got_object_id *id;
1413 struct got_commit_object *commit;
1414 struct commit_queue_entry *entry;
1415 int errcode;
1417 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1418 if (err || id == NULL)
1419 break;
1421 err = got_object_open_as_commit(&commit, repo, id);
1422 if (err)
1423 break;
1424 entry = alloc_commit_queue_entry(commit, id);
1425 if (entry == NULL) {
1426 err = got_error_from_errno("alloc_commit_queue_entry");
1427 break;
1430 errcode = pthread_mutex_lock(&tog_mutex);
1431 if (errcode) {
1432 err = got_error_set_errno(errcode,
1433 "pthread_mutex_lock");
1434 break;
1437 entry->idx = commits->ncommits;
1438 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1439 nqueued++;
1440 commits->ncommits++;
1442 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1443 err = match_commit(&have_match, id, commit, regex);
1446 errcode = pthread_mutex_unlock(&tog_mutex);
1447 if (errcode && err == NULL)
1448 err = got_error_set_errno(errcode,
1449 "pthread_mutex_unlock");
1451 if (err || have_match)
1452 break;
1455 return err;
1458 static const struct got_error *
1459 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1460 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1461 struct commit_queue *commits, int selected_idx, int limit,
1462 struct got_reflist_head *refs, const char *path, int commits_needed,
1463 struct tog_colors *colors)
1465 const struct got_error *err = NULL;
1466 struct tog_log_view_state *s = &view->state.log;
1467 struct commit_queue_entry *entry;
1468 int width;
1469 int ncommits, author_cols = 4;
1470 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1471 char *refs_str = NULL;
1472 wchar_t *wline;
1473 struct tog_color *tc;
1474 static const size_t date_display_cols = 12;
1476 entry = first;
1477 ncommits = 0;
1478 while (entry) {
1479 if (ncommits == selected_idx) {
1480 *selected = entry;
1481 break;
1483 entry = TAILQ_NEXT(entry, entry);
1484 ncommits++;
1487 if (*selected && !(view->searching && view->search_next_done == 0)) {
1488 err = got_object_id_str(&id_str, (*selected)->id);
1489 if (err)
1490 return err;
1491 if (refs) {
1492 err = build_refs_str(&refs_str, refs, (*selected)->id,
1493 s->repo);
1494 if (err)
1495 goto done;
1499 if (commits_needed == 0)
1500 halfdelay(10); /* disable fast refresh */
1502 if (asprintf(&ncommits_str, " [%d/%d] %s",
1503 entry ? entry->idx + 1 : 0, commits->ncommits,
1504 commits_needed > 0 ?
1505 (view->searching && view->search_next_done == 0
1506 ? "searching..." : "loading... ") :
1507 (refs_str ? refs_str : "")) == -1) {
1508 err = got_error_from_errno("asprintf");
1509 goto done;
1512 if (path && strcmp(path, "/") != 0) {
1513 if (asprintf(&header, "commit %s %s%s",
1514 id_str ? id_str : "........................................",
1515 path, ncommits_str) == -1) {
1516 err = got_error_from_errno("asprintf");
1517 header = NULL;
1518 goto done;
1520 } else if (asprintf(&header, "commit %s%s",
1521 id_str ? id_str : "........................................",
1522 ncommits_str) == -1) {
1523 err = got_error_from_errno("asprintf");
1524 header = NULL;
1525 goto done;
1527 err = format_line(&wline, &width, header, view->ncols, 0);
1528 if (err)
1529 goto done;
1531 werase(view->window);
1533 if (view_needs_focus_indication(view))
1534 wstandout(view->window);
1535 tc = get_color(colors, TOG_COLOR_COMMIT);
1536 if (tc)
1537 wattr_on(view->window,
1538 COLOR_PAIR(tc->colorpair), NULL);
1539 waddwstr(view->window, wline);
1540 if (tc)
1541 wattr_off(view->window,
1542 COLOR_PAIR(tc->colorpair), NULL);
1543 while (width < view->ncols) {
1544 waddch(view->window, ' ');
1545 width++;
1547 if (view_needs_focus_indication(view))
1548 wstandend(view->window);
1549 free(wline);
1550 if (limit <= 1)
1551 goto done;
1553 /* Grow author column size if necessary. */
1554 entry = first;
1555 ncommits = 0;
1556 while (entry) {
1557 char *author;
1558 wchar_t *wauthor;
1559 int width;
1560 if (ncommits >= limit - 1)
1561 break;
1562 author = strdup(got_object_commit_get_author(entry->commit));
1563 if (author == NULL) {
1564 err = got_error_from_errno("strdup");
1565 goto done;
1567 err = format_author(&wauthor, &width, author, COLS,
1568 date_display_cols);
1569 if (author_cols < width)
1570 author_cols = width;
1571 free(wauthor);
1572 free(author);
1573 ncommits++;
1574 entry = TAILQ_NEXT(entry, entry);
1577 entry = first;
1578 *last = first;
1579 ncommits = 0;
1580 while (entry) {
1581 if (ncommits >= limit - 1)
1582 break;
1583 if (ncommits == selected_idx)
1584 wstandout(view->window);
1585 err = draw_commit(view, entry->commit, entry->id, refs,
1586 date_display_cols, author_cols, colors);
1587 if (ncommits == selected_idx)
1588 wstandend(view->window);
1589 if (err)
1590 goto done;
1591 ncommits++;
1592 *last = entry;
1593 entry = TAILQ_NEXT(entry, entry);
1596 view_vborder(view);
1597 done:
1598 free(id_str);
1599 free(refs_str);
1600 free(ncommits_str);
1601 free(header);
1602 return err;
1605 static void
1606 scroll_up(struct tog_view *view,
1607 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1608 struct commit_queue *commits)
1610 struct commit_queue_entry *entry;
1611 int nscrolled = 0;
1613 entry = TAILQ_FIRST(&commits->head);
1614 if (*first_displayed_entry == entry)
1615 return;
1617 entry = *first_displayed_entry;
1618 while (entry && nscrolled < maxscroll) {
1619 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1620 if (entry) {
1621 *first_displayed_entry = entry;
1622 nscrolled++;
1627 static const struct got_error *
1628 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1629 pthread_cond_t *need_commits)
1631 int errcode;
1632 int max_wait = 20;
1634 halfdelay(1); /* fast refresh while loading commits */
1636 while (*commits_needed > 0) {
1637 if (*log_complete)
1638 break;
1640 /* Wake the log thread. */
1641 errcode = pthread_cond_signal(need_commits);
1642 if (errcode)
1643 return got_error_set_errno(errcode,
1644 "pthread_cond_signal");
1645 errcode = pthread_mutex_unlock(&tog_mutex);
1646 if (errcode)
1647 return got_error_set_errno(errcode,
1648 "pthread_mutex_unlock");
1649 pthread_yield();
1650 errcode = pthread_mutex_lock(&tog_mutex);
1651 if (errcode)
1652 return got_error_set_errno(errcode,
1653 "pthread_mutex_lock");
1655 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1657 * Thread is not done yet; lose a key press
1658 * and let the user retry... this way the GUI
1659 * remains interactive while logging deep paths
1660 * with few commits in history.
1662 return NULL;
1666 return NULL;
1669 static const struct got_error *
1670 scroll_down(struct tog_view *view,
1671 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1672 struct commit_queue_entry **last_displayed_entry,
1673 struct commit_queue *commits, int *log_complete, int *commits_needed,
1674 pthread_cond_t *need_commits)
1676 const struct got_error *err = NULL;
1677 struct commit_queue_entry *pentry;
1678 int nscrolled = 0;
1680 if (*last_displayed_entry == NULL)
1681 return NULL;
1683 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1684 if (pentry == NULL && !*log_complete) {
1686 * Ask the log thread for required amount of commits
1687 * plus some amount of pre-fetching.
1689 (*commits_needed) += maxscroll + 20;
1690 err = trigger_log_thread(0, commits_needed, log_complete,
1691 need_commits);
1692 if (err)
1693 return err;
1696 do {
1697 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1698 if (pentry == NULL)
1699 break;
1701 *last_displayed_entry = pentry;
1703 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1704 if (pentry == NULL)
1705 break;
1706 *first_displayed_entry = pentry;
1707 } while (++nscrolled < maxscroll);
1709 return err;
1712 static const struct got_error *
1713 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1714 struct got_commit_object *commit, struct got_object_id *commit_id,
1715 struct tog_view *log_view, struct got_reflist_head *refs,
1716 struct got_repository *repo)
1718 const struct got_error *err;
1719 struct got_object_qid *parent_id;
1720 struct tog_view *diff_view;
1722 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1723 if (diff_view == NULL)
1724 return got_error_from_errno("view_open");
1726 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1727 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1728 commit_id, log_view, refs, repo);
1729 if (err == NULL)
1730 *new_view = diff_view;
1731 return err;
1734 static const struct got_error *
1735 tree_view_visit_subtree(struct got_tree_object *subtree,
1736 struct tog_tree_view_state *s)
1738 struct tog_parent_tree *parent;
1740 parent = calloc(1, sizeof(*parent));
1741 if (parent == NULL)
1742 return got_error_from_errno("calloc");
1744 parent->tree = s->tree;
1745 parent->first_displayed_entry = s->first_displayed_entry;
1746 parent->selected_entry = s->selected_entry;
1747 parent->selected = s->selected;
1748 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1749 s->tree = subtree;
1750 s->selected = 0;
1751 s->first_displayed_entry = NULL;
1752 return NULL;
1756 static const struct got_error *
1757 browse_commit_tree(struct tog_view **new_view, int begin_x,
1758 struct commit_queue_entry *entry, const char *path,
1759 struct got_reflist_head *refs, struct got_repository *repo)
1761 const struct got_error *err = NULL;
1762 struct got_tree_object *tree;
1763 struct tog_tree_view_state *s;
1764 struct tog_view *tree_view;
1765 char *slash, *subpath = NULL;
1766 const char *p;
1768 err = got_object_open_as_tree(&tree, repo,
1769 got_object_commit_get_tree_id(entry->commit));
1770 if (err)
1771 return err;
1773 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1774 if (tree_view == NULL)
1775 return got_error_from_errno("view_open");
1777 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1778 if (err) {
1779 got_object_tree_close(tree);
1780 return err;
1782 s = &tree_view->state.tree;
1784 *new_view = tree_view;
1786 if (got_path_is_root_dir(path))
1787 return NULL;
1789 /* Walk the path and open corresponding tree objects. */
1790 p = path;
1791 while (*p) {
1792 struct got_tree_entry *te;
1793 struct got_object_id *tree_id;
1794 char *te_name;
1796 while (p[0] == '/')
1797 p++;
1799 /* Ensure the correct subtree entry is selected. */
1800 slash = strchr(p, '/');
1801 if (slash == NULL)
1802 te_name = strdup(p);
1803 else
1804 te_name = strndup(p, slash - p);
1805 if (te_name == NULL) {
1806 err = got_error_from_errno("strndup");
1807 break;
1809 te = got_object_tree_find_entry(s->tree, te_name);
1810 if (te == NULL) {
1811 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1812 free(te_name);
1813 break;
1815 free(te_name);
1816 s->selected_entry = te;
1817 s->selected = got_tree_entry_get_index(te);
1818 if (s->tree != s->root)
1819 s->selected++; /* skip '..' */
1821 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1822 /* Jump to this file's entry. */
1823 s->first_displayed_entry = s->selected_entry;
1824 s->selected = 0;
1825 break;
1828 slash = strchr(p, '/');
1829 if (slash)
1830 subpath = strndup(path, slash - path);
1831 else
1832 subpath = strdup(path);
1833 if (subpath == NULL) {
1834 err = got_error_from_errno("strdup");
1835 break;
1838 err = got_object_id_by_path(&tree_id, repo, entry->id,
1839 subpath);
1840 if (err)
1841 break;
1843 err = got_object_open_as_tree(&tree, repo, tree_id);
1844 free(tree_id);
1845 if (err)
1846 break;
1848 err = tree_view_visit_subtree(tree, s);
1849 if (err) {
1850 got_object_tree_close(tree);
1851 break;
1853 if (slash == NULL)
1854 break;
1855 free(subpath);
1856 subpath = NULL;
1857 p = slash;
1860 free(subpath);
1861 return err;
1864 static const struct got_error *
1865 block_signals_used_by_main_thread(void)
1867 sigset_t sigset;
1868 int errcode;
1870 if (sigemptyset(&sigset) == -1)
1871 return got_error_from_errno("sigemptyset");
1873 /* tog handles SIGWINCH and SIGCONT */
1874 if (sigaddset(&sigset, SIGWINCH) == -1)
1875 return got_error_from_errno("sigaddset");
1876 if (sigaddset(&sigset, SIGCONT) == -1)
1877 return got_error_from_errno("sigaddset");
1879 /* ncurses handles SIGTSTP */
1880 if (sigaddset(&sigset, SIGTSTP) == -1)
1881 return got_error_from_errno("sigaddset");
1883 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1884 if (errcode)
1885 return got_error_set_errno(errcode, "pthread_sigmask");
1887 return NULL;
1890 static void *
1891 log_thread(void *arg)
1893 const struct got_error *err = NULL;
1894 int errcode = 0;
1895 struct tog_log_thread_args *a = arg;
1896 int done = 0;
1898 err = block_signals_used_by_main_thread();
1899 if (err)
1900 return (void *)err;
1902 while (!done && !err && !tog_sigpipe_received) {
1903 err = queue_commits(a->graph, a->commits, 1, a->repo,
1904 a->in_repo_path, a->searching, a->search_next_done,
1905 a->regex);
1906 if (err) {
1907 if (err->code != GOT_ERR_ITER_COMPLETED)
1908 return (void *)err;
1909 err = NULL;
1910 done = 1;
1911 } else if (a->commits_needed > 0)
1912 a->commits_needed--;
1914 errcode = pthread_mutex_lock(&tog_mutex);
1915 if (errcode) {
1916 err = got_error_set_errno(errcode,
1917 "pthread_mutex_lock");
1918 break;
1919 } else if (*a->quit)
1920 done = 1;
1921 else if (*a->first_displayed_entry == NULL) {
1922 *a->first_displayed_entry =
1923 TAILQ_FIRST(&a->commits->head);
1924 *a->selected_entry = *a->first_displayed_entry;
1927 if (done)
1928 a->commits_needed = 0;
1929 else if (a->commits_needed == 0) {
1930 errcode = pthread_cond_wait(&a->need_commits,
1931 &tog_mutex);
1932 if (errcode)
1933 err = got_error_set_errno(errcode,
1934 "pthread_cond_wait");
1937 errcode = pthread_mutex_unlock(&tog_mutex);
1938 if (errcode && err == NULL)
1939 err = got_error_set_errno(errcode,
1940 "pthread_mutex_unlock");
1942 a->log_complete = 1;
1943 return (void *)err;
1946 static const struct got_error *
1947 stop_log_thread(struct tog_log_view_state *s)
1949 const struct got_error *err = NULL;
1950 int errcode;
1952 if (s->thread) {
1953 s->quit = 1;
1954 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1955 if (errcode)
1956 return got_error_set_errno(errcode,
1957 "pthread_cond_signal");
1958 errcode = pthread_mutex_unlock(&tog_mutex);
1959 if (errcode)
1960 return got_error_set_errno(errcode,
1961 "pthread_mutex_unlock");
1962 errcode = pthread_join(s->thread, (void **)&err);
1963 if (errcode)
1964 return got_error_set_errno(errcode, "pthread_join");
1965 errcode = pthread_mutex_lock(&tog_mutex);
1966 if (errcode)
1967 return got_error_set_errno(errcode,
1968 "pthread_mutex_lock");
1969 s->thread = NULL;
1972 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1973 if (errcode && err == NULL)
1974 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1976 if (s->thread_args.repo) {
1977 got_repo_close(s->thread_args.repo);
1978 s->thread_args.repo = NULL;
1981 if (s->thread_args.graph) {
1982 got_commit_graph_close(s->thread_args.graph);
1983 s->thread_args.graph = NULL;
1986 return err;
1989 static const struct got_error *
1990 close_log_view(struct tog_view *view)
1992 const struct got_error *err = NULL;
1993 struct tog_log_view_state *s = &view->state.log;
1995 err = stop_log_thread(s);
1996 free_commits(&s->commits);
1997 free(s->in_repo_path);
1998 s->in_repo_path = NULL;
1999 free(s->start_id);
2000 s->start_id = NULL;
2001 return err;
2004 static const struct got_error *
2005 search_start_log_view(struct tog_view *view)
2007 struct tog_log_view_state *s = &view->state.log;
2009 s->matched_entry = NULL;
2010 s->search_entry = NULL;
2011 return NULL;
2014 static const struct got_error *
2015 search_next_log_view(struct tog_view *view)
2017 const struct got_error *err = NULL;
2018 struct tog_log_view_state *s = &view->state.log;
2019 struct commit_queue_entry *entry;
2021 if (!view->searching) {
2022 view->search_next_done = 1;
2023 return NULL;
2026 if (s->search_entry) {
2027 int errcode, ch;
2028 errcode = pthread_mutex_unlock(&tog_mutex);
2029 if (errcode)
2030 return got_error_set_errno(errcode,
2031 "pthread_mutex_unlock");
2032 ch = wgetch(view->window);
2033 errcode = pthread_mutex_lock(&tog_mutex);
2034 if (errcode)
2035 return got_error_set_errno(errcode,
2036 "pthread_mutex_lock");
2037 if (ch == KEY_BACKSPACE) {
2038 view->search_next_done = 1;
2039 return NULL;
2041 if (view->searching == TOG_SEARCH_FORWARD)
2042 entry = TAILQ_NEXT(s->search_entry, entry);
2043 else
2044 entry = TAILQ_PREV(s->search_entry,
2045 commit_queue_head, entry);
2046 } else if (s->matched_entry) {
2047 if (view->searching == TOG_SEARCH_FORWARD)
2048 entry = TAILQ_NEXT(s->selected_entry, entry);
2049 else
2050 entry = TAILQ_PREV(s->selected_entry,
2051 commit_queue_head, entry);
2052 } else {
2053 if (view->searching == TOG_SEARCH_FORWARD)
2054 entry = TAILQ_FIRST(&s->commits.head);
2055 else
2056 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2059 while (1) {
2060 int have_match = 0;
2062 if (entry == NULL) {
2063 if (s->thread_args.log_complete ||
2064 view->searching == TOG_SEARCH_BACKWARD) {
2065 view->search_next_done = 1;
2066 return NULL;
2069 * Poke the log thread for more commits and return,
2070 * allowing the main loop to make progress. Search
2071 * will resume at s->search_entry once we come back.
2073 s->thread_args.commits_needed++;
2074 return trigger_log_thread(1,
2075 &s->thread_args.commits_needed,
2076 &s->thread_args.log_complete,
2077 &s->thread_args.need_commits);
2080 err = match_commit(&have_match, entry->id, entry->commit,
2081 &view->regex);
2082 if (err)
2083 break;
2084 if (have_match) {
2085 view->search_next_done = 1;
2086 s->matched_entry = entry;
2087 break;
2090 s->search_entry = entry;
2091 if (view->searching == TOG_SEARCH_FORWARD)
2092 entry = TAILQ_NEXT(entry, entry);
2093 else
2094 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2097 if (s->matched_entry) {
2098 int cur = s->selected_entry->idx;
2099 while (cur < s->matched_entry->idx) {
2100 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2101 if (err)
2102 return err;
2103 cur++;
2105 while (cur > s->matched_entry->idx) {
2106 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2107 if (err)
2108 return err;
2109 cur--;
2113 s->search_entry = NULL;
2115 return NULL;
2118 static const struct got_error *
2119 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2120 struct got_reflist_head *refs, struct got_repository *repo,
2121 const char *head_ref_name, const char *path, int check_disk,
2122 int log_branches)
2124 const struct got_error *err = NULL;
2125 struct tog_log_view_state *s = &view->state.log;
2126 struct got_repository *thread_repo = NULL;
2127 struct got_commit_graph *thread_graph = NULL;
2128 int errcode;
2130 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2131 if (err != NULL)
2132 goto done;
2134 /* The commit queue only contains commits being displayed. */
2135 TAILQ_INIT(&s->commits.head);
2136 s->commits.ncommits = 0;
2138 s->refs = refs;
2139 s->repo = repo;
2140 s->head_ref_name = head_ref_name;
2141 s->start_id = got_object_id_dup(start_id);
2142 if (s->start_id == NULL) {
2143 err = got_error_from_errno("got_object_id_dup");
2144 goto done;
2146 s->log_branches = log_branches;
2148 SIMPLEQ_INIT(&s->colors);
2149 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2150 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2151 get_color_value("TOG_COLOR_COMMIT"));
2152 if (err)
2153 goto done;
2154 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2155 get_color_value("TOG_COLOR_AUTHOR"));
2156 if (err) {
2157 free_colors(&s->colors);
2158 goto done;
2160 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2161 get_color_value("TOG_COLOR_DATE"));
2162 if (err) {
2163 free_colors(&s->colors);
2164 goto done;
2168 view->show = show_log_view;
2169 view->input = input_log_view;
2170 view->close = close_log_view;
2171 view->search_start = search_start_log_view;
2172 view->search_next = search_next_log_view;
2174 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2175 if (err)
2176 goto done;
2177 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2178 !s->log_branches);
2179 if (err)
2180 goto done;
2181 err = got_commit_graph_iter_start(thread_graph,
2182 s->start_id, s->repo, NULL, NULL);
2183 if (err)
2184 goto done;
2186 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2187 if (errcode) {
2188 err = got_error_set_errno(errcode, "pthread_cond_init");
2189 goto done;
2192 s->thread_args.commits_needed = view->nlines;
2193 s->thread_args.graph = thread_graph;
2194 s->thread_args.commits = &s->commits;
2195 s->thread_args.in_repo_path = s->in_repo_path;
2196 s->thread_args.start_id = s->start_id;
2197 s->thread_args.repo = thread_repo;
2198 s->thread_args.log_complete = 0;
2199 s->thread_args.quit = &s->quit;
2200 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2201 s->thread_args.selected_entry = &s->selected_entry;
2202 s->thread_args.searching = &view->searching;
2203 s->thread_args.search_next_done = &view->search_next_done;
2204 s->thread_args.regex = &view->regex;
2205 done:
2206 if (err)
2207 close_log_view(view);
2208 return err;
2211 static const struct got_error *
2212 show_log_view(struct tog_view *view)
2214 struct tog_log_view_state *s = &view->state.log;
2216 if (s->thread == NULL) {
2217 int errcode = pthread_create(&s->thread, NULL, log_thread,
2218 &s->thread_args);
2219 if (errcode)
2220 return got_error_set_errno(errcode, "pthread_create");
2223 return draw_commits(view, &s->last_displayed_entry,
2224 &s->selected_entry, s->first_displayed_entry,
2225 &s->commits, s->selected, view->nlines, s->refs,
2226 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2229 static const struct got_error *
2230 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2231 struct tog_view **focus_view, struct tog_view *view, int ch)
2233 const struct got_error *err = NULL;
2234 struct tog_log_view_state *s = &view->state.log;
2235 char *parent_path, *in_repo_path = NULL;
2236 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2237 int begin_x = 0;
2238 struct got_object_id *start_id;
2240 switch (ch) {
2241 case 'q':
2242 s->quit = 1;
2243 break;
2244 case 'k':
2245 case KEY_UP:
2246 case '<':
2247 case ',':
2248 if (s->first_displayed_entry == NULL)
2249 break;
2250 if (s->selected > 0)
2251 s->selected--;
2252 else
2253 scroll_up(view, &s->first_displayed_entry, 1,
2254 &s->commits);
2255 break;
2256 case KEY_PPAGE:
2257 case CTRL('b'):
2258 if (s->first_displayed_entry == NULL)
2259 break;
2260 if (TAILQ_FIRST(&s->commits.head) ==
2261 s->first_displayed_entry) {
2262 s->selected = 0;
2263 break;
2265 scroll_up(view, &s->first_displayed_entry,
2266 view->nlines, &s->commits);
2267 break;
2268 case 'j':
2269 case KEY_DOWN:
2270 case '>':
2271 case '.':
2272 if (s->first_displayed_entry == NULL)
2273 break;
2274 if (s->selected < MIN(view->nlines - 2,
2275 s->commits.ncommits - 1)) {
2276 s->selected++;
2277 break;
2279 err = scroll_down(view, &s->first_displayed_entry, 1,
2280 &s->last_displayed_entry, &s->commits,
2281 &s->thread_args.log_complete,
2282 &s->thread_args.commits_needed,
2283 &s->thread_args.need_commits);
2284 break;
2285 case KEY_NPAGE:
2286 case CTRL('f'): {
2287 struct commit_queue_entry *first;
2288 first = s->first_displayed_entry;
2289 if (first == NULL)
2290 break;
2291 err = scroll_down(view, &s->first_displayed_entry,
2292 view->nlines, &s->last_displayed_entry,
2293 &s->commits, &s->thread_args.log_complete,
2294 &s->thread_args.commits_needed,
2295 &s->thread_args.need_commits);
2296 if (err)
2297 break;
2298 if (first == s->first_displayed_entry &&
2299 s->selected < MIN(view->nlines - 2,
2300 s->commits.ncommits - 1)) {
2301 /* can't scroll further down */
2302 s->selected = MIN(view->nlines - 2,
2303 s->commits.ncommits - 1);
2305 err = NULL;
2306 break;
2308 case KEY_RESIZE:
2309 if (s->selected > view->nlines - 2)
2310 s->selected = view->nlines - 2;
2311 if (s->selected > s->commits.ncommits - 1)
2312 s->selected = s->commits.ncommits - 1;
2313 break;
2314 case KEY_ENTER:
2315 case ' ':
2316 case '\r':
2317 if (s->selected_entry == NULL)
2318 break;
2319 if (view_is_parent_view(view))
2320 begin_x = view_split_begin_x(view->begin_x);
2321 err = open_diff_view_for_commit(&diff_view, begin_x,
2322 s->selected_entry->commit, s->selected_entry->id,
2323 view, s->refs, s->repo);
2324 if (err)
2325 break;
2326 if (view_is_parent_view(view)) {
2327 err = view_close_child(view);
2328 if (err)
2329 return err;
2330 err = view_set_child(view, diff_view);
2331 if (err) {
2332 view_close(diff_view);
2333 break;
2335 *focus_view = diff_view;
2336 view->child_focussed = 1;
2337 } else
2338 *new_view = diff_view;
2339 break;
2340 case 't':
2341 if (s->selected_entry == NULL)
2342 break;
2343 if (view_is_parent_view(view))
2344 begin_x = view_split_begin_x(view->begin_x);
2345 err = browse_commit_tree(&tree_view, begin_x,
2346 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2347 if (err)
2348 break;
2349 if (view_is_parent_view(view)) {
2350 err = view_close_child(view);
2351 if (err)
2352 return err;
2353 err = view_set_child(view, tree_view);
2354 if (err) {
2355 view_close(tree_view);
2356 break;
2358 *focus_view = tree_view;
2359 view->child_focussed = 1;
2360 } else
2361 *new_view = tree_view;
2362 break;
2363 case KEY_BACKSPACE:
2364 if (strcmp(s->in_repo_path, "/") == 0)
2365 break;
2366 parent_path = dirname(s->in_repo_path);
2367 if (parent_path && strcmp(parent_path, ".") != 0) {
2368 err = stop_log_thread(s);
2369 if (err)
2370 return err;
2371 lv = view_open(view->nlines, view->ncols,
2372 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2373 if (lv == NULL)
2374 return got_error_from_errno(
2375 "view_open");
2376 err = open_log_view(lv, s->start_id, s->refs,
2377 s->repo, s->head_ref_name, parent_path, 0,
2378 s->log_branches);
2379 if (err)
2380 return err;;
2381 if (view_is_parent_view(view))
2382 *new_view = lv;
2383 else {
2384 view_set_child(view->parent, lv);
2385 *focus_view = lv;
2387 return NULL;
2389 break;
2390 case CTRL('l'):
2391 err = stop_log_thread(s);
2392 if (err)
2393 return err;
2394 lv = view_open(view->nlines, view->ncols,
2395 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2396 if (lv == NULL)
2397 return got_error_from_errno("view_open");
2398 err = got_repo_match_object_id(&start_id, NULL,
2399 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2400 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2401 if (err) {
2402 view_close(lv);
2403 return err;
2405 in_repo_path = strdup(s->in_repo_path);
2406 if (in_repo_path == NULL) {
2407 free(start_id);
2408 view_close(lv);
2409 return got_error_from_errno("strdup");
2411 got_ref_list_free(s->refs);
2412 err = got_ref_list(s->refs, s->repo, NULL,
2413 got_ref_cmp_by_name, NULL);
2414 if (err) {
2415 free(start_id);
2416 view_close(lv);
2417 return err;
2419 err = open_log_view(lv, start_id, s->refs, s->repo,
2420 s->head_ref_name, in_repo_path, 0, s->log_branches);
2421 if (err) {
2422 free(start_id);
2423 view_close(lv);
2424 return err;;
2426 *dead_view = view;
2427 *new_view = lv;
2428 break;
2429 case 'B':
2430 s->log_branches = !s->log_branches;
2431 err = stop_log_thread(s);
2432 if (err)
2433 return err;
2434 lv = view_open(view->nlines, view->ncols,
2435 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2436 if (lv == NULL)
2437 return got_error_from_errno("view_open");
2438 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2439 s->head_ref_name, s->in_repo_path, 0, s->log_branches);
2440 if (err) {
2441 view_close(lv);
2442 return err;;
2444 *dead_view = view;
2445 *new_view = lv;
2446 break;
2447 default:
2448 break;
2451 return err;
2454 static const struct got_error *
2455 apply_unveil(const char *repo_path, const char *worktree_path)
2457 const struct got_error *error;
2459 #ifdef PROFILE
2460 if (unveil("gmon.out", "rwc") != 0)
2461 return got_error_from_errno2("unveil", "gmon.out");
2462 #endif
2463 if (repo_path && unveil(repo_path, "r") != 0)
2464 return got_error_from_errno2("unveil", repo_path);
2466 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2467 return got_error_from_errno2("unveil", worktree_path);
2469 if (unveil("/tmp", "rwc") != 0)
2470 return got_error_from_errno2("unveil", "/tmp");
2472 error = got_privsep_unveil_exec_helpers();
2473 if (error != NULL)
2474 return error;
2476 if (unveil(NULL, NULL) != 0)
2477 return got_error_from_errno("unveil");
2479 return NULL;
2482 static void
2483 init_curses(void)
2485 initscr();
2486 cbreak();
2487 halfdelay(1); /* Do fast refresh while initial view is loading. */
2488 noecho();
2489 nonl();
2490 intrflush(stdscr, FALSE);
2491 keypad(stdscr, TRUE);
2492 curs_set(0);
2493 if (getenv("TOG_COLORS") != NULL) {
2494 start_color();
2495 use_default_colors();
2497 signal(SIGWINCH, tog_sigwinch);
2498 signal(SIGPIPE, tog_sigpipe);
2499 signal(SIGCONT, tog_sigcont);
2502 static const struct got_error *
2503 cmd_log(int argc, char *argv[])
2505 const struct got_error *error;
2506 struct got_repository *repo = NULL;
2507 struct got_worktree *worktree = NULL;
2508 struct got_reflist_head refs;
2509 struct got_object_id *start_id = NULL;
2510 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2511 char *start_commit = NULL, *head_ref_name = NULL;
2512 int ch, log_branches = 0, check_disk = 1;
2513 struct tog_view *view;
2515 SIMPLEQ_INIT(&refs);
2517 #ifndef PROFILE
2518 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2519 NULL) == -1)
2520 err(1, "pledge");
2521 #endif
2523 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2524 switch (ch) {
2525 case 'b':
2526 log_branches = 1;
2527 break;
2528 case 'c':
2529 start_commit = optarg;
2530 break;
2531 case 'r':
2532 repo_path = realpath(optarg, NULL);
2533 if (repo_path == NULL)
2534 return got_error_from_errno2("realpath",
2535 optarg);
2536 break;
2537 default:
2538 usage_log();
2539 /* NOTREACHED */
2543 argc -= optind;
2544 argv += optind;
2546 cwd = getcwd(NULL, 0);
2547 if (cwd == NULL) {
2548 error = got_error_from_errno("getcwd");
2549 goto done;
2551 error = got_worktree_open(&worktree, cwd);
2552 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2553 goto done;
2554 error = NULL;
2556 if (argc == 0) {
2557 path = strdup("");
2558 if (path == NULL) {
2559 error = got_error_from_errno("strdup");
2560 goto done;
2562 } else if (argc == 1) {
2563 if (worktree) {
2564 error = got_worktree_resolve_path(&path, worktree,
2565 argv[0]);
2566 if (error)
2567 goto done;
2568 } else {
2569 path = strdup(argv[0]);
2570 if (path == NULL) {
2571 error = got_error_from_errno("strdup");
2572 goto done;
2575 } else
2576 usage_log();
2578 if (repo_path == NULL) {
2579 if (worktree)
2580 repo_path = strdup(
2581 got_worktree_get_repo_path(worktree));
2582 else
2583 repo_path = strdup(cwd);
2585 if (repo_path == NULL) {
2586 error = got_error_from_errno("strdup");
2587 goto done;
2590 init_curses();
2592 error = got_repo_open(&repo, repo_path, NULL);
2593 if (error != NULL)
2594 goto done;
2596 error = apply_unveil(got_repo_get_path(repo),
2597 worktree ? got_worktree_get_root_path(worktree) : NULL);
2598 if (error)
2599 goto done;
2601 if (start_commit == NULL)
2602 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2603 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2604 GOT_OBJ_TYPE_COMMIT, 1, repo);
2605 else
2606 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2607 GOT_OBJ_TYPE_COMMIT, 1, repo);
2608 if (error != NULL)
2609 goto done;
2611 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2612 if (error)
2613 goto done;
2615 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2616 if (view == NULL) {
2617 error = got_error_from_errno("view_open");
2618 goto done;
2620 if (worktree) {
2621 const char *prefix = got_worktree_get_path_prefix(worktree);
2622 char *p;
2623 if (asprintf(&p, "%s%s%s", prefix,
2624 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2625 error = got_error_from_errno("asprintf");
2626 goto done;
2628 free(path);
2629 path = p;
2630 check_disk = 0;
2632 head_ref_name = strdup(
2633 got_worktree_get_head_ref_name(worktree));
2634 if (head_ref_name == NULL) {
2635 error = got_error_from_errno("strdup");
2636 goto done;
2639 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2640 path, check_disk, log_branches);
2641 if (error)
2642 goto done;
2643 if (worktree) {
2644 /* Release work tree lock. */
2645 got_worktree_close(worktree);
2646 worktree = NULL;
2648 error = view_loop(view);
2649 done:
2650 free(repo_path);
2651 free(cwd);
2652 free(path);
2653 free(start_id);
2654 free(head_ref_name);
2655 if (repo)
2656 got_repo_close(repo);
2657 if (worktree)
2658 got_worktree_close(worktree);
2659 got_ref_list_free(&refs);
2660 return error;
2663 __dead static void
2664 usage_diff(void)
2666 endwin();
2667 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2668 getprogname());
2669 exit(1);
2672 static char *
2673 parse_next_line(FILE *f, size_t *len)
2675 char *line;
2676 size_t linelen;
2677 size_t lineno;
2678 const char delim[3] = { '\0', '\0', '\0'};
2680 line = fparseln(f, &linelen, &lineno, delim, 0);
2681 if (len)
2682 *len = linelen;
2683 return line;
2686 static int
2687 match_line(const char *line, regex_t *regex)
2689 regmatch_t regmatch;
2691 return regexec(regex, line, 1, &regmatch, 0) == 0;
2694 struct tog_color *
2695 match_color(struct tog_colors *colors, const char *line)
2697 struct tog_color *tc = NULL;
2699 SIMPLEQ_FOREACH(tc, colors, entry) {
2700 if (match_line(line, &tc->regex))
2701 return tc;
2704 return NULL;
2707 static const struct got_error *
2708 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2709 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2710 char *header, struct tog_colors *colors)
2712 const struct got_error *err;
2713 int lineno = 0, nprinted = 0;
2714 char *line;
2715 struct tog_color *tc;
2716 size_t len;
2717 wchar_t *wline;
2718 int width;
2720 rewind(f);
2721 werase(view->window);
2723 if (header) {
2724 err = format_line(&wline, &width, header, view->ncols, 0);
2725 if (err) {
2726 return err;
2729 if (view_needs_focus_indication(view))
2730 wstandout(view->window);
2731 waddwstr(view->window, wline);
2732 if (view_needs_focus_indication(view))
2733 wstandend(view->window);
2734 if (width <= view->ncols - 1)
2735 waddch(view->window, '\n');
2737 if (max_lines <= 1)
2738 return NULL;
2739 max_lines--;
2742 *eof = 0;
2743 while (nprinted < max_lines) {
2744 line = parse_next_line(f, &len);
2745 if (line == NULL) {
2746 *eof = 1;
2747 break;
2749 if (++lineno < *first_displayed_line) {
2750 free(line);
2751 continue;
2754 err = format_line(&wline, &width, line, view->ncols, 0);
2755 if (err) {
2756 free(line);
2757 return err;
2760 tc = match_color(colors, line);
2761 if (tc)
2762 wattr_on(view->window,
2763 COLOR_PAIR(tc->colorpair), NULL);
2764 waddwstr(view->window, wline);
2765 if (tc)
2766 wattr_off(view->window,
2767 COLOR_PAIR(tc->colorpair), NULL);
2768 if (width <= view->ncols - 1)
2769 waddch(view->window, '\n');
2770 if (++nprinted == 1)
2771 *first_displayed_line = lineno;
2772 free(line);
2773 free(wline);
2774 wline = NULL;
2776 *last_displayed_line = lineno;
2778 view_vborder(view);
2780 if (*eof) {
2781 while (nprinted < view->nlines) {
2782 waddch(view->window, '\n');
2783 nprinted++;
2786 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2787 if (err) {
2788 return err;
2791 wstandout(view->window);
2792 waddwstr(view->window, wline);
2793 wstandend(view->window);
2796 return NULL;
2799 static char *
2800 get_datestr(time_t *time, char *datebuf)
2802 struct tm mytm, *tm;
2803 char *p, *s;
2805 tm = gmtime_r(time, &mytm);
2806 if (tm == NULL)
2807 return NULL;
2808 s = asctime_r(tm, datebuf);
2809 if (s == NULL)
2810 return NULL;
2811 p = strchr(s, '\n');
2812 if (p)
2813 *p = '\0';
2814 return s;
2817 static const struct got_error *
2818 write_commit_info(struct got_object_id *commit_id,
2819 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2821 const struct got_error *err = NULL;
2822 char datebuf[26], *datestr;
2823 struct got_commit_object *commit;
2824 char *id_str = NULL, *logmsg = NULL;
2825 time_t committer_time;
2826 const char *author, *committer;
2827 char *refs_str = NULL;
2829 if (refs) {
2830 err = build_refs_str(&refs_str, refs, commit_id, repo);
2831 if (err)
2832 return err;
2835 err = got_object_open_as_commit(&commit, repo, commit_id);
2836 if (err)
2837 return err;
2839 err = got_object_id_str(&id_str, commit_id);
2840 if (err) {
2841 err = got_error_from_errno("got_object_id_str");
2842 goto done;
2845 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2846 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2847 err = got_error_from_errno("fprintf");
2848 goto done;
2850 if (fprintf(outfile, "from: %s\n",
2851 got_object_commit_get_author(commit)) < 0) {
2852 err = got_error_from_errno("fprintf");
2853 goto done;
2855 committer_time = got_object_commit_get_committer_time(commit);
2856 datestr = get_datestr(&committer_time, datebuf);
2857 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2858 err = got_error_from_errno("fprintf");
2859 goto done;
2861 author = got_object_commit_get_author(commit);
2862 committer = got_object_commit_get_committer(commit);
2863 if (strcmp(author, committer) != 0 &&
2864 fprintf(outfile, "via: %s\n", committer) < 0) {
2865 err = got_error_from_errno("fprintf");
2866 goto done;
2868 err = got_object_commit_get_logmsg(&logmsg, commit);
2869 if (err)
2870 goto done;
2871 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2872 err = got_error_from_errno("fprintf");
2873 goto done;
2875 done:
2876 free(id_str);
2877 free(logmsg);
2878 free(refs_str);
2879 got_object_commit_close(commit);
2880 return err;
2883 const struct got_error *
2884 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2885 FILE *infile)
2887 size_t len;
2888 char *buf = NULL;
2889 int i;
2890 size_t noffsets = 0;
2891 off_t off = 0;
2893 if (line_offsets)
2894 *line_offsets = NULL;
2895 if (filesize)
2896 *filesize = 0;
2897 if (nlines)
2898 *nlines = 0;
2900 if (fseek(infile, 0, SEEK_END) == -1)
2901 return got_error_from_errno("fseek");
2902 len = ftell(infile) + 1;
2903 if (ferror(infile))
2904 return got_error_from_errno("ftell");
2905 if (fseek(infile, 0, SEEK_SET) == -1)
2906 return got_error_from_errno("fseek");
2908 if (len == 0)
2909 return NULL;
2910 if ((buf = calloc(len, sizeof(char *))) == NULL)
2911 return got_error_from_errno("calloc");
2913 fread(buf, 1, len, infile);
2914 if (ferror(infile))
2915 return got_error_from_errno("fread");
2917 i = 0;
2918 if (line_offsets && nlines) {
2919 if (*line_offsets == NULL) {
2920 /* Have some data but perhaps no '\n'. */
2921 noffsets = 1;
2922 *nlines = 1;
2923 *line_offsets = calloc(1, sizeof(**line_offsets));
2924 if (*line_offsets == NULL)
2925 return got_error_from_errno("calloc");
2926 /* Skip forward over end of first line. */
2927 while (i < len) {
2928 if (buf[i] == '\n')
2929 break;
2930 i++;
2933 /* Scan '\n' offsets in remaining chunk of data. */
2934 while (i < len) {
2935 if (buf[i] != '\n') {
2936 i++;
2937 continue;
2939 (*nlines)++;
2940 if (noffsets < *nlines) {
2941 off_t *o = recallocarray(*line_offsets,
2942 noffsets, *nlines,
2943 sizeof(**line_offsets));
2944 if (o == NULL) {
2945 free(*line_offsets);
2946 *line_offsets = NULL;
2947 return got_error_from_errno(
2948 "recallocarray");
2950 *line_offsets = o;
2951 noffsets = *nlines;
2953 off = i + 1;
2954 (*line_offsets)[*nlines - 1] = off;
2955 i++;
2959 if (fflush(infile) != 0)
2960 return got_error_from_errno("fflush");
2961 rewind(infile);
2963 if (filesize)
2964 *filesize = len;
2966 return NULL;
2969 static const struct got_error *
2970 create_diff(struct tog_diff_view_state *s)
2972 const struct got_error *err = NULL;
2973 FILE *f = NULL;
2974 int obj_type;
2976 f = got_opentemp();
2977 if (f == NULL) {
2978 err = got_error_from_errno("got_opentemp");
2979 goto done;
2981 if (s->f && fclose(s->f) != 0) {
2982 err = got_error_from_errno("fclose");
2983 goto done;
2985 s->f = f;
2987 if (s->id1)
2988 err = got_object_get_type(&obj_type, s->repo, s->id1);
2989 else
2990 err = got_object_get_type(&obj_type, s->repo, s->id2);
2991 if (err)
2992 goto done;
2994 switch (obj_type) {
2995 case GOT_OBJ_TYPE_BLOB:
2996 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2997 s->diff_context, 0, s->repo, s->f);
2998 break;
2999 case GOT_OBJ_TYPE_TREE:
3000 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3001 s->diff_context, 0, s->repo, s->f);
3002 break;
3003 case GOT_OBJ_TYPE_COMMIT: {
3004 const struct got_object_id_queue *parent_ids;
3005 struct got_object_qid *pid;
3006 struct got_commit_object *commit2;
3008 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3009 if (err)
3010 goto done;
3011 /* Show commit info if we're diffing to a parent/root commit. */
3012 if (s->id1 == NULL) {
3013 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3014 if (err)
3015 goto done;
3016 } else {
3017 parent_ids = got_object_commit_get_parent_ids(commit2);
3018 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3019 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3020 err = write_commit_info(s->id2, s->refs,
3021 s->repo, s->f);
3022 if (err)
3023 goto done;
3024 break;
3028 got_object_commit_close(commit2);
3030 err = got_diff_objects_as_commits(s->id1, s->id2,
3031 s->diff_context, 0, s->repo, s->f);
3032 break;
3034 default:
3035 err = got_error(GOT_ERR_OBJ_TYPE);
3036 break;
3038 if (err)
3039 goto done;
3040 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3041 s->f);
3042 done:
3043 if (s->f && fflush(s->f) != 0 && err == NULL)
3044 err = got_error_from_errno("fflush");
3045 return err;
3048 static void
3049 diff_view_indicate_progress(struct tog_view *view)
3051 mvwaddstr(view->window, 0, 0, "diffing...");
3052 update_panels();
3053 doupdate();
3056 static const struct got_error *
3057 search_start_diff_view(struct tog_view *view)
3059 struct tog_diff_view_state *s = &view->state.diff;
3061 s->matched_line = 0;
3062 return NULL;
3065 static const struct got_error *
3066 search_next_diff_view(struct tog_view *view)
3068 struct tog_diff_view_state *s = &view->state.diff;
3069 int lineno;
3071 if (!view->searching) {
3072 view->search_next_done = 1;
3073 return NULL;
3076 if (s->matched_line) {
3077 if (view->searching == TOG_SEARCH_FORWARD)
3078 lineno = s->matched_line + 1;
3079 else
3080 lineno = s->matched_line - 1;
3081 } else {
3082 if (view->searching == TOG_SEARCH_FORWARD)
3083 lineno = 1;
3084 else
3085 lineno = s->nlines;
3088 while (1) {
3089 char *line = NULL;
3090 off_t offset;
3091 size_t len;
3093 if (lineno <= 0 || lineno > s->nlines) {
3094 if (s->matched_line == 0) {
3095 view->search_next_done = 1;
3096 free(line);
3097 break;
3100 if (view->searching == TOG_SEARCH_FORWARD)
3101 lineno = 1;
3102 else
3103 lineno = s->nlines;
3106 offset = s->line_offsets[lineno - 1];
3107 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3108 free(line);
3109 return got_error_from_errno("fseeko");
3111 free(line);
3112 line = parse_next_line(s->f, &len);
3113 if (line && match_line(line, &view->regex)) {
3114 view->search_next_done = 1;
3115 s->matched_line = lineno;
3116 free(line);
3117 break;
3119 free(line);
3120 if (view->searching == TOG_SEARCH_FORWARD)
3121 lineno++;
3122 else
3123 lineno--;
3126 if (s->matched_line) {
3127 s->first_displayed_line = s->matched_line;
3128 s->selected_line = 1;
3131 return NULL;
3134 static const struct got_error *
3135 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3136 struct got_object_id *id2, struct tog_view *log_view,
3137 struct got_reflist_head *refs, struct got_repository *repo)
3139 const struct got_error *err;
3140 struct tog_diff_view_state *s = &view->state.diff;
3142 if (id1 != NULL && id2 != NULL) {
3143 int type1, type2;
3144 err = got_object_get_type(&type1, repo, id1);
3145 if (err)
3146 return err;
3147 err = got_object_get_type(&type2, repo, id2);
3148 if (err)
3149 return err;
3151 if (type1 != type2)
3152 return got_error(GOT_ERR_OBJ_TYPE);
3154 s->first_displayed_line = 1;
3155 s->last_displayed_line = view->nlines;
3156 s->selected_line = 1;
3157 s->repo = repo;
3158 s->refs = refs;
3159 s->id1 = id1;
3160 s->id2 = id2;
3162 if (id1) {
3163 s->id1 = got_object_id_dup(id1);
3164 if (s->id1 == NULL)
3165 return got_error_from_errno("got_object_id_dup");
3166 } else
3167 s->id1 = NULL;
3169 s->id2 = got_object_id_dup(id2);
3170 if (s->id2 == NULL) {
3171 free(s->id1);
3172 s->id1 = NULL;
3173 return got_error_from_errno("got_object_id_dup");
3175 s->f = NULL;
3176 s->first_displayed_line = 1;
3177 s->last_displayed_line = view->nlines;
3178 s->diff_context = 3;
3179 s->log_view = log_view;
3180 s->repo = repo;
3181 s->refs = refs;
3183 SIMPLEQ_INIT(&s->colors);
3184 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3185 err = add_color(&s->colors,
3186 "^-", TOG_COLOR_DIFF_MINUS,
3187 get_color_value("TOG_COLOR_DIFF_MINUS"));
3188 if (err)
3189 return err;
3190 err = add_color(&s->colors, "^\\+",
3191 TOG_COLOR_DIFF_PLUS,
3192 get_color_value("TOG_COLOR_DIFF_PLUS"));
3193 if (err) {
3194 free_colors(&s->colors);
3195 return err;
3197 err = add_color(&s->colors,
3198 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3199 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3200 if (err) {
3201 free_colors(&s->colors);
3202 return err;
3205 err = add_color(&s->colors,
3206 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3207 get_color_value("TOG_COLOR_DIFF_META"));
3208 if (err) {
3209 free_colors(&s->colors);
3210 return err;
3213 err = add_color(&s->colors,
3214 "^(from|via): ", TOG_COLOR_AUTHOR,
3215 get_color_value("TOG_COLOR_AUTHOR"));
3216 if (err) {
3217 free_colors(&s->colors);
3218 return err;
3221 err = add_color(&s->colors,
3222 "^date: ", TOG_COLOR_DATE,
3223 get_color_value("TOG_COLOR_DATE"));
3224 if (err) {
3225 free_colors(&s->colors);
3226 return err;
3230 if (log_view && view_is_splitscreen(view))
3231 show_log_view(log_view); /* draw vborder */
3232 diff_view_indicate_progress(view);
3234 err = create_diff(s);
3235 if (err) {
3236 free(s->id1);
3237 s->id1 = NULL;
3238 free(s->id2);
3239 s->id2 = NULL;
3240 return err;
3243 view->show = show_diff_view;
3244 view->input = input_diff_view;
3245 view->close = close_diff_view;
3246 view->search_start = search_start_diff_view;
3247 view->search_next = search_next_diff_view;
3249 return NULL;
3252 static const struct got_error *
3253 close_diff_view(struct tog_view *view)
3255 const struct got_error *err = NULL;
3256 struct tog_diff_view_state *s = &view->state.diff;
3258 free(s->id1);
3259 s->id1 = NULL;
3260 free(s->id2);
3261 s->id2 = NULL;
3262 if (s->f && fclose(s->f) == EOF)
3263 err = got_error_from_errno("fclose");
3264 free_colors(&s->colors);
3265 free(s->line_offsets);
3266 return err;
3269 static const struct got_error *
3270 show_diff_view(struct tog_view *view)
3272 const struct got_error *err;
3273 struct tog_diff_view_state *s = &view->state.diff;
3274 char *id_str1 = NULL, *id_str2, *header;
3276 if (s->id1) {
3277 err = got_object_id_str(&id_str1, s->id1);
3278 if (err)
3279 return err;
3281 err = got_object_id_str(&id_str2, s->id2);
3282 if (err)
3283 return err;
3285 if (asprintf(&header, "diff %s %s",
3286 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3287 err = got_error_from_errno("asprintf");
3288 free(id_str1);
3289 free(id_str2);
3290 return err;
3292 free(id_str1);
3293 free(id_str2);
3295 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3296 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3297 header, &s->colors);
3300 static const struct got_error *
3301 set_selected_commit(struct tog_diff_view_state *s,
3302 struct commit_queue_entry *entry)
3304 const struct got_error *err;
3305 const struct got_object_id_queue *parent_ids;
3306 struct got_commit_object *selected_commit;
3307 struct got_object_qid *pid;
3309 free(s->id2);
3310 s->id2 = got_object_id_dup(entry->id);
3311 if (s->id2 == NULL)
3312 return got_error_from_errno("got_object_id_dup");
3314 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3315 if (err)
3316 return err;
3317 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3318 free(s->id1);
3319 pid = SIMPLEQ_FIRST(parent_ids);
3320 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3321 got_object_commit_close(selected_commit);
3322 return NULL;
3325 static const struct got_error *
3326 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3327 struct tog_view **focus_view, struct tog_view *view, int ch)
3329 const struct got_error *err = NULL;
3330 struct tog_diff_view_state *s = &view->state.diff;
3331 struct tog_log_view_state *ls;
3332 struct commit_queue_entry *entry;
3333 int i;
3335 switch (ch) {
3336 case 'k':
3337 case KEY_UP:
3338 if (s->first_displayed_line > 1)
3339 s->first_displayed_line--;
3340 break;
3341 case KEY_PPAGE:
3342 case CTRL('b'):
3343 if (s->first_displayed_line == 1)
3344 break;
3345 i = 0;
3346 while (i++ < view->nlines - 1 &&
3347 s->first_displayed_line > 1)
3348 s->first_displayed_line--;
3349 break;
3350 case 'j':
3351 case KEY_DOWN:
3352 if (!s->eof)
3353 s->first_displayed_line++;
3354 break;
3355 case KEY_NPAGE:
3356 case CTRL('f'):
3357 case ' ':
3358 if (s->eof)
3359 break;
3360 i = 0;
3361 while (!s->eof && i++ < view->nlines - 1) {
3362 char *line;
3363 line = parse_next_line(s->f, NULL);
3364 s->first_displayed_line++;
3365 if (line == NULL)
3366 break;
3368 break;
3369 case '[':
3370 if (s->diff_context > 0) {
3371 s->diff_context--;
3372 diff_view_indicate_progress(view);
3373 err = create_diff(s);
3375 break;
3376 case ']':
3377 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3378 s->diff_context++;
3379 diff_view_indicate_progress(view);
3380 err = create_diff(s);
3382 break;
3383 case '<':
3384 case ',':
3385 if (s->log_view == NULL)
3386 break;
3387 ls = &s->log_view->state.log;
3388 entry = TAILQ_PREV(ls->selected_entry,
3389 commit_queue_head, entry);
3390 if (entry == NULL)
3391 break;
3393 err = input_log_view(NULL, NULL, NULL, s->log_view,
3394 KEY_UP);
3395 if (err)
3396 break;
3398 err = set_selected_commit(s, entry);
3399 if (err)
3400 break;
3402 s->first_displayed_line = 1;
3403 s->last_displayed_line = view->nlines;
3405 diff_view_indicate_progress(view);
3406 err = create_diff(s);
3407 break;
3408 case '>':
3409 case '.':
3410 if (s->log_view == NULL)
3411 break;
3412 ls = &s->log_view->state.log;
3414 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3415 ls->thread_args.commits_needed++;
3417 /* Display "loading..." in log view. */
3418 show_log_view(s->log_view);
3419 update_panels();
3420 doupdate();
3422 err = trigger_log_thread(1 /* load_all */,
3423 &ls->thread_args.commits_needed,
3424 &ls->thread_args.log_complete,
3425 &ls->thread_args.need_commits);
3426 if (err)
3427 break;
3429 err = input_log_view(NULL, NULL, NULL, s->log_view,
3430 KEY_DOWN);
3431 if (err)
3432 break;
3434 entry = TAILQ_NEXT(ls->selected_entry, entry);
3435 if (entry == NULL)
3436 break;
3438 err = set_selected_commit(s, entry);
3439 if (err)
3440 break;
3442 s->first_displayed_line = 1;
3443 s->last_displayed_line = view->nlines;
3445 diff_view_indicate_progress(view);
3446 err = create_diff(s);
3447 break;
3448 default:
3449 break;
3452 return err;
3455 static const struct got_error *
3456 cmd_diff(int argc, char *argv[])
3458 const struct got_error *error = NULL;
3459 struct got_repository *repo = NULL;
3460 struct got_reflist_head refs;
3461 struct got_object_id *id1 = NULL, *id2 = NULL;
3462 char *repo_path = NULL;
3463 char *id_str1 = NULL, *id_str2 = NULL;
3464 int ch;
3465 struct tog_view *view;
3467 SIMPLEQ_INIT(&refs);
3469 #ifndef PROFILE
3470 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3471 NULL) == -1)
3472 err(1, "pledge");
3473 #endif
3475 while ((ch = getopt(argc, argv, "")) != -1) {
3476 switch (ch) {
3477 default:
3478 usage_diff();
3479 /* NOTREACHED */
3483 argc -= optind;
3484 argv += optind;
3486 if (argc == 0) {
3487 usage_diff(); /* TODO show local worktree changes */
3488 } else if (argc == 2) {
3489 repo_path = getcwd(NULL, 0);
3490 if (repo_path == NULL)
3491 return got_error_from_errno("getcwd");
3492 id_str1 = argv[0];
3493 id_str2 = argv[1];
3494 } else if (argc == 3) {
3495 repo_path = realpath(argv[0], NULL);
3496 if (repo_path == NULL)
3497 return got_error_from_errno2("realpath", argv[0]);
3498 id_str1 = argv[1];
3499 id_str2 = argv[2];
3500 } else
3501 usage_diff();
3503 init_curses();
3505 error = got_repo_open(&repo, repo_path, NULL);
3506 if (error)
3507 goto done;
3509 error = apply_unveil(got_repo_get_path(repo), NULL);
3510 if (error)
3511 goto done;
3513 error = got_repo_match_object_id_prefix(&id1, id_str1,
3514 GOT_OBJ_TYPE_ANY, repo);
3515 if (error)
3516 goto done;
3518 error = got_repo_match_object_id_prefix(&id2, id_str2,
3519 GOT_OBJ_TYPE_ANY, repo);
3520 if (error)
3521 goto done;
3523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3524 if (error)
3525 goto done;
3527 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3528 if (view == NULL) {
3529 error = got_error_from_errno("view_open");
3530 goto done;
3532 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3533 if (error)
3534 goto done;
3535 error = view_loop(view);
3536 done:
3537 free(repo_path);
3538 if (repo)
3539 got_repo_close(repo);
3540 got_ref_list_free(&refs);
3541 return error;
3544 __dead static void
3545 usage_blame(void)
3547 endwin();
3548 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3549 getprogname());
3550 exit(1);
3553 struct tog_blame_line {
3554 int annotated;
3555 struct got_object_id *id;
3558 static const struct got_error *
3559 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3560 const char *path, struct tog_blame_line *lines, int nlines,
3561 int blame_complete, int selected_line, int *first_displayed_line,
3562 int *last_displayed_line, int *eof, int max_lines,
3563 struct tog_colors *colors)
3565 const struct got_error *err;
3566 int lineno = 0, nprinted = 0;
3567 char *line;
3568 size_t len;
3569 wchar_t *wline;
3570 int width;
3571 struct tog_blame_line *blame_line;
3572 struct got_object_id *prev_id = NULL;
3573 char *id_str;
3574 struct tog_color *tc;
3576 err = got_object_id_str(&id_str, id);
3577 if (err)
3578 return err;
3580 rewind(f);
3581 werase(view->window);
3583 if (asprintf(&line, "commit %s", id_str) == -1) {
3584 err = got_error_from_errno("asprintf");
3585 free(id_str);
3586 return err;
3589 err = format_line(&wline, &width, line, view->ncols, 0);
3590 free(line);
3591 line = NULL;
3592 if (err)
3593 return err;
3594 if (view_needs_focus_indication(view))
3595 wstandout(view->window);
3596 tc = get_color(colors, TOG_COLOR_COMMIT);
3597 if (tc)
3598 wattr_on(view->window,
3599 COLOR_PAIR(tc->colorpair), NULL);
3600 waddwstr(view->window, wline);
3601 if (tc)
3602 wattr_off(view->window,
3603 COLOR_PAIR(tc->colorpair), NULL);
3604 if (view_needs_focus_indication(view))
3605 wstandend(view->window);
3606 free(wline);
3607 wline = NULL;
3608 if (width < view->ncols - 1)
3609 waddch(view->window, '\n');
3611 if (asprintf(&line, "[%d/%d] %s%s",
3612 *first_displayed_line - 1 + selected_line, nlines,
3613 blame_complete ? "" : "annotating... ", path) == -1) {
3614 free(id_str);
3615 return got_error_from_errno("asprintf");
3617 free(id_str);
3618 err = format_line(&wline, &width, line, view->ncols, 0);
3619 free(line);
3620 line = NULL;
3621 if (err)
3622 return err;
3623 waddwstr(view->window, wline);
3624 free(wline);
3625 wline = NULL;
3626 if (width < view->ncols - 1)
3627 waddch(view->window, '\n');
3629 *eof = 0;
3630 while (nprinted < max_lines - 2) {
3631 line = parse_next_line(f, &len);
3632 if (line == NULL) {
3633 *eof = 1;
3634 break;
3636 if (++lineno < *first_displayed_line) {
3637 free(line);
3638 continue;
3641 if (view->ncols <= 9) {
3642 width = 9;
3643 wline = wcsdup(L"");
3644 if (wline == NULL)
3645 err = got_error_from_errno("wcsdup");
3646 } else {
3647 err = format_line(&wline, &width, line,
3648 view->ncols - 9, 9);
3649 width += 9;
3651 if (err) {
3652 free(line);
3653 return err;
3656 if (view->focussed && nprinted == selected_line - 1)
3657 wstandout(view->window);
3659 if (nlines > 0) {
3660 blame_line = &lines[lineno - 1];
3661 if (blame_line->annotated && prev_id &&
3662 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3663 !(view->focussed &&
3664 nprinted == selected_line - 1)) {
3665 waddstr(view->window, " ");
3666 } else if (blame_line->annotated) {
3667 char *id_str;
3668 err = got_object_id_str(&id_str, blame_line->id);
3669 if (err) {
3670 free(line);
3671 free(wline);
3672 return err;
3674 tc = get_color(colors, TOG_COLOR_COMMIT);
3675 if (tc)
3676 wattr_on(view->window,
3677 COLOR_PAIR(tc->colorpair), NULL);
3678 wprintw(view->window, "%.8s", id_str);
3679 if (tc)
3680 wattr_off(view->window,
3681 COLOR_PAIR(tc->colorpair), NULL);
3682 free(id_str);
3683 prev_id = blame_line->id;
3684 } else {
3685 waddstr(view->window, "........");
3686 prev_id = NULL;
3688 } else {
3689 waddstr(view->window, "........");
3690 prev_id = NULL;
3693 if (view->focussed && nprinted == selected_line - 1)
3694 wstandend(view->window);
3695 waddstr(view->window, " ");
3697 waddwstr(view->window, wline);
3698 if (width <= view->ncols - 1)
3699 waddch(view->window, '\n');
3700 if (++nprinted == 1)
3701 *first_displayed_line = lineno;
3702 free(line);
3703 free(wline);
3704 wline = NULL;
3706 *last_displayed_line = lineno;
3708 view_vborder(view);
3710 return NULL;
3713 static const struct got_error *
3714 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3716 const struct got_error *err = NULL;
3717 struct tog_blame_cb_args *a = arg;
3718 struct tog_blame_line *line;
3719 int errcode;
3721 if (nlines != a->nlines ||
3722 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3723 return got_error(GOT_ERR_RANGE);
3725 errcode = pthread_mutex_lock(&tog_mutex);
3726 if (errcode)
3727 return got_error_set_errno(errcode, "pthread_mutex_lock");
3729 if (*a->quit) { /* user has quit the blame view */
3730 err = got_error(GOT_ERR_ITER_COMPLETED);
3731 goto done;
3734 if (lineno == -1)
3735 goto done; /* no change in this commit */
3737 line = &a->lines[lineno - 1];
3738 if (line->annotated)
3739 goto done;
3741 line->id = got_object_id_dup(id);
3742 if (line->id == NULL) {
3743 err = got_error_from_errno("got_object_id_dup");
3744 goto done;
3746 line->annotated = 1;
3747 done:
3748 errcode = pthread_mutex_unlock(&tog_mutex);
3749 if (errcode)
3750 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3751 return err;
3754 static void *
3755 blame_thread(void *arg)
3757 const struct got_error *err;
3758 struct tog_blame_thread_args *ta = arg;
3759 struct tog_blame_cb_args *a = ta->cb_args;
3760 int errcode;
3762 err = block_signals_used_by_main_thread();
3763 if (err)
3764 return (void *)err;
3766 err = got_blame(ta->path, a->commit_id, ta->repo,
3767 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3768 if (err && err->code == GOT_ERR_CANCELLED)
3769 err = NULL;
3771 errcode = pthread_mutex_lock(&tog_mutex);
3772 if (errcode)
3773 return (void *)got_error_set_errno(errcode,
3774 "pthread_mutex_lock");
3776 got_repo_close(ta->repo);
3777 ta->repo = NULL;
3778 *ta->complete = 1;
3780 errcode = pthread_mutex_unlock(&tog_mutex);
3781 if (errcode && err == NULL)
3782 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3784 return (void *)err;
3787 static struct got_object_id *
3788 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3789 int first_displayed_line, int selected_line)
3791 struct tog_blame_line *line;
3793 if (nlines <= 0)
3794 return NULL;
3796 line = &lines[first_displayed_line - 1 + selected_line - 1];
3797 if (!line->annotated)
3798 return NULL;
3800 return line->id;
3803 static const struct got_error *
3804 stop_blame(struct tog_blame *blame)
3806 const struct got_error *err = NULL;
3807 int i;
3809 if (blame->thread) {
3810 int errcode;
3811 errcode = pthread_mutex_unlock(&tog_mutex);
3812 if (errcode)
3813 return got_error_set_errno(errcode,
3814 "pthread_mutex_unlock");
3815 errcode = pthread_join(blame->thread, (void **)&err);
3816 if (errcode)
3817 return got_error_set_errno(errcode, "pthread_join");
3818 errcode = pthread_mutex_lock(&tog_mutex);
3819 if (errcode)
3820 return got_error_set_errno(errcode,
3821 "pthread_mutex_lock");
3822 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3823 err = NULL;
3824 blame->thread = NULL;
3826 if (blame->thread_args.repo) {
3827 got_repo_close(blame->thread_args.repo);
3828 blame->thread_args.repo = NULL;
3830 if (blame->f) {
3831 if (fclose(blame->f) != 0 && err == NULL)
3832 err = got_error_from_errno("fclose");
3833 blame->f = NULL;
3835 if (blame->lines) {
3836 for (i = 0; i < blame->nlines; i++)
3837 free(blame->lines[i].id);
3838 free(blame->lines);
3839 blame->lines = NULL;
3841 free(blame->cb_args.commit_id);
3842 blame->cb_args.commit_id = NULL;
3844 return err;
3847 static const struct got_error *
3848 cancel_blame_view(void *arg)
3850 const struct got_error *err = NULL;
3851 int *done = arg;
3852 int errcode;
3854 errcode = pthread_mutex_lock(&tog_mutex);
3855 if (errcode)
3856 return got_error_set_errno(errcode,
3857 "pthread_mutex_unlock");
3859 if (*done)
3860 err = got_error(GOT_ERR_CANCELLED);
3862 errcode = pthread_mutex_unlock(&tog_mutex);
3863 if (errcode)
3864 return got_error_set_errno(errcode,
3865 "pthread_mutex_lock");
3867 return err;
3870 static const struct got_error *
3871 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3872 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3873 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3874 struct got_repository *repo)
3876 const struct got_error *err = NULL;
3877 struct got_blob_object *blob = NULL;
3878 struct got_repository *thread_repo = NULL;
3879 struct got_object_id *obj_id = NULL;
3880 int obj_type;
3882 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3883 if (err)
3884 return err;
3886 err = got_object_get_type(&obj_type, repo, obj_id);
3887 if (err)
3888 goto done;
3890 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3891 err = got_error(GOT_ERR_OBJ_TYPE);
3892 goto done;
3895 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3896 if (err)
3897 goto done;
3898 blame->f = got_opentemp();
3899 if (blame->f == NULL) {
3900 err = got_error_from_errno("got_opentemp");
3901 goto done;
3903 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3904 &blame->line_offsets, blame->f, blob);
3905 if (err || blame->nlines == 0)
3906 goto done;
3908 /* Don't include \n at EOF in the blame line count. */
3909 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3910 blame->nlines--;
3912 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3913 if (blame->lines == NULL) {
3914 err = got_error_from_errno("calloc");
3915 goto done;
3918 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3919 if (err)
3920 goto done;
3922 blame->cb_args.view = view;
3923 blame->cb_args.lines = blame->lines;
3924 blame->cb_args.nlines = blame->nlines;
3925 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3926 if (blame->cb_args.commit_id == NULL) {
3927 err = got_error_from_errno("got_object_id_dup");
3928 goto done;
3930 blame->cb_args.quit = done;
3932 blame->thread_args.path = path;
3933 blame->thread_args.repo = thread_repo;
3934 blame->thread_args.cb_args = &blame->cb_args;
3935 blame->thread_args.complete = blame_complete;
3936 blame->thread_args.cancel_cb = cancel_blame_view;
3937 blame->thread_args.cancel_arg = done;
3938 *blame_complete = 0;
3940 done:
3941 if (blob)
3942 got_object_blob_close(blob);
3943 free(obj_id);
3944 if (err)
3945 stop_blame(blame);
3946 return err;
3949 static const struct got_error *
3950 open_blame_view(struct tog_view *view, char *path,
3951 struct got_object_id *commit_id, struct got_reflist_head *refs,
3952 struct got_repository *repo)
3954 const struct got_error *err = NULL;
3955 struct tog_blame_view_state *s = &view->state.blame;
3957 SIMPLEQ_INIT(&s->blamed_commits);
3959 s->path = strdup(path);
3960 if (s->path == NULL)
3961 return got_error_from_errno("strdup");
3963 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3964 if (err) {
3965 free(s->path);
3966 return err;
3969 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3970 s->first_displayed_line = 1;
3971 s->last_displayed_line = view->nlines;
3972 s->selected_line = 1;
3973 s->blame_complete = 0;
3974 s->repo = repo;
3975 s->refs = refs;
3976 s->commit_id = commit_id;
3977 memset(&s->blame, 0, sizeof(s->blame));
3979 SIMPLEQ_INIT(&s->colors);
3980 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3981 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3982 get_color_value("TOG_COLOR_COMMIT"));
3983 if (err)
3984 return err;
3987 view->show = show_blame_view;
3988 view->input = input_blame_view;
3989 view->close = close_blame_view;
3990 view->search_start = search_start_blame_view;
3991 view->search_next = search_next_blame_view;
3993 return run_blame(&s->blame, view, &s->blame_complete,
3994 &s->first_displayed_line, &s->last_displayed_line,
3995 &s->selected_line, &s->done, &s->eof, s->path,
3996 s->blamed_commit->id, s->repo);
3999 static const struct got_error *
4000 close_blame_view(struct tog_view *view)
4002 const struct got_error *err = NULL;
4003 struct tog_blame_view_state *s = &view->state.blame;
4005 if (s->blame.thread)
4006 err = stop_blame(&s->blame);
4008 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4009 struct got_object_qid *blamed_commit;
4010 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4011 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4012 got_object_qid_free(blamed_commit);
4015 free(s->path);
4016 free_colors(&s->colors);
4018 return err;
4021 static const struct got_error *
4022 search_start_blame_view(struct tog_view *view)
4024 struct tog_blame_view_state *s = &view->state.blame;
4026 s->matched_line = 0;
4027 return NULL;
4030 static const struct got_error *
4031 search_next_blame_view(struct tog_view *view)
4033 struct tog_blame_view_state *s = &view->state.blame;
4034 int lineno;
4036 if (!view->searching) {
4037 view->search_next_done = 1;
4038 return NULL;
4041 if (s->matched_line) {
4042 if (view->searching == TOG_SEARCH_FORWARD)
4043 lineno = s->matched_line + 1;
4044 else
4045 lineno = s->matched_line - 1;
4046 } else {
4047 if (view->searching == TOG_SEARCH_FORWARD)
4048 lineno = 1;
4049 else
4050 lineno = s->blame.nlines;
4053 while (1) {
4054 char *line = NULL;
4055 off_t offset;
4056 size_t len;
4058 if (lineno <= 0 || lineno > s->blame.nlines) {
4059 if (s->matched_line == 0) {
4060 view->search_next_done = 1;
4061 free(line);
4062 break;
4065 if (view->searching == TOG_SEARCH_FORWARD)
4066 lineno = 1;
4067 else
4068 lineno = s->blame.nlines;
4071 offset = s->blame.line_offsets[lineno - 1];
4072 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4073 free(line);
4074 return got_error_from_errno("fseeko");
4076 free(line);
4077 line = parse_next_line(s->blame.f, &len);
4078 if (line && match_line(line, &view->regex)) {
4079 view->search_next_done = 1;
4080 s->matched_line = lineno;
4081 free(line);
4082 break;
4084 free(line);
4085 if (view->searching == TOG_SEARCH_FORWARD)
4086 lineno++;
4087 else
4088 lineno--;
4091 if (s->matched_line) {
4092 s->first_displayed_line = s->matched_line;
4093 s->selected_line = 1;
4096 return NULL;
4099 static const struct got_error *
4100 show_blame_view(struct tog_view *view)
4102 const struct got_error *err = NULL;
4103 struct tog_blame_view_state *s = &view->state.blame;
4104 int errcode;
4106 if (s->blame.thread == NULL) {
4107 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4108 &s->blame.thread_args);
4109 if (errcode)
4110 return got_error_set_errno(errcode, "pthread_create");
4112 halfdelay(1); /* fast refresh while annotating */
4115 if (s->blame_complete)
4116 halfdelay(10); /* disable fast refresh */
4118 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4119 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4120 s->selected_line, &s->first_displayed_line,
4121 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4123 view_vborder(view);
4124 return err;
4127 static const struct got_error *
4128 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4129 struct tog_view **focus_view, struct tog_view *view, int ch)
4131 const struct got_error *err = NULL, *thread_err = NULL;
4132 struct tog_view *diff_view;
4133 struct tog_blame_view_state *s = &view->state.blame;
4134 int begin_x = 0;
4136 switch (ch) {
4137 case 'q':
4138 s->done = 1;
4139 break;
4140 case 'k':
4141 case KEY_UP:
4142 if (s->selected_line > 1)
4143 s->selected_line--;
4144 else if (s->selected_line == 1 &&
4145 s->first_displayed_line > 1)
4146 s->first_displayed_line--;
4147 break;
4148 case KEY_PPAGE:
4149 if (s->first_displayed_line == 1) {
4150 s->selected_line = 1;
4151 break;
4153 if (s->first_displayed_line > view->nlines - 2)
4154 s->first_displayed_line -=
4155 (view->nlines - 2);
4156 else
4157 s->first_displayed_line = 1;
4158 break;
4159 case 'j':
4160 case KEY_DOWN:
4161 if (s->selected_line < view->nlines - 2 &&
4162 s->first_displayed_line +
4163 s->selected_line <= s->blame.nlines)
4164 s->selected_line++;
4165 else if (s->last_displayed_line <
4166 s->blame.nlines)
4167 s->first_displayed_line++;
4168 break;
4169 case 'b':
4170 case 'p': {
4171 struct got_object_id *id = NULL;
4172 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4173 s->first_displayed_line, s->selected_line);
4174 if (id == NULL)
4175 break;
4176 if (ch == 'p') {
4177 struct got_commit_object *commit;
4178 struct got_object_qid *pid;
4179 struct got_object_id *blob_id = NULL;
4180 int obj_type;
4181 err = got_object_open_as_commit(&commit,
4182 s->repo, id);
4183 if (err)
4184 break;
4185 pid = SIMPLEQ_FIRST(
4186 got_object_commit_get_parent_ids(commit));
4187 if (pid == NULL) {
4188 got_object_commit_close(commit);
4189 break;
4191 /* Check if path history ends here. */
4192 err = got_object_id_by_path(&blob_id, s->repo,
4193 pid->id, s->path);
4194 if (err) {
4195 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4196 err = NULL;
4197 got_object_commit_close(commit);
4198 break;
4200 err = got_object_get_type(&obj_type, s->repo,
4201 blob_id);
4202 free(blob_id);
4203 /* Can't blame non-blob type objects. */
4204 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4205 got_object_commit_close(commit);
4206 break;
4208 err = got_object_qid_alloc(&s->blamed_commit,
4209 pid->id);
4210 got_object_commit_close(commit);
4211 } else {
4212 if (got_object_id_cmp(id,
4213 s->blamed_commit->id) == 0)
4214 break;
4215 err = got_object_qid_alloc(&s->blamed_commit,
4216 id);
4218 if (err)
4219 break;
4220 s->done = 1;
4221 thread_err = stop_blame(&s->blame);
4222 s->done = 0;
4223 if (thread_err)
4224 break;
4225 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4226 s->blamed_commit, entry);
4227 err = run_blame(&s->blame, view, &s->blame_complete,
4228 &s->first_displayed_line, &s->last_displayed_line,
4229 &s->selected_line, &s->done, &s->eof,
4230 s->path, s->blamed_commit->id, s->repo);
4231 if (err)
4232 break;
4233 break;
4235 case 'B': {
4236 struct got_object_qid *first;
4237 first = SIMPLEQ_FIRST(&s->blamed_commits);
4238 if (!got_object_id_cmp(first->id, s->commit_id))
4239 break;
4240 s->done = 1;
4241 thread_err = stop_blame(&s->blame);
4242 s->done = 0;
4243 if (thread_err)
4244 break;
4245 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4246 got_object_qid_free(s->blamed_commit);
4247 s->blamed_commit =
4248 SIMPLEQ_FIRST(&s->blamed_commits);
4249 err = run_blame(&s->blame, view, &s->blame_complete,
4250 &s->first_displayed_line, &s->last_displayed_line,
4251 &s->selected_line, &s->done, &s->eof, s->path,
4252 s->blamed_commit->id, s->repo);
4253 if (err)
4254 break;
4255 break;
4257 case KEY_ENTER:
4258 case '\r': {
4259 struct got_object_id *id = NULL;
4260 struct got_object_qid *pid;
4261 struct got_commit_object *commit = NULL;
4262 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4263 s->first_displayed_line, s->selected_line);
4264 if (id == NULL)
4265 break;
4266 err = got_object_open_as_commit(&commit, s->repo, id);
4267 if (err)
4268 break;
4269 pid = SIMPLEQ_FIRST(
4270 got_object_commit_get_parent_ids(commit));
4271 if (view_is_parent_view(view))
4272 begin_x = view_split_begin_x(view->begin_x);
4273 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4274 if (diff_view == NULL) {
4275 got_object_commit_close(commit);
4276 err = got_error_from_errno("view_open");
4277 break;
4279 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4280 id, NULL, s->refs, s->repo);
4281 got_object_commit_close(commit);
4282 if (err) {
4283 view_close(diff_view);
4284 break;
4286 if (view_is_parent_view(view)) {
4287 err = view_close_child(view);
4288 if (err)
4289 break;
4290 err = view_set_child(view, diff_view);
4291 if (err) {
4292 view_close(diff_view);
4293 break;
4295 *focus_view = diff_view;
4296 view->child_focussed = 1;
4297 } else
4298 *new_view = diff_view;
4299 if (err)
4300 break;
4301 break;
4303 case KEY_NPAGE:
4304 case ' ':
4305 if (s->last_displayed_line >= s->blame.nlines &&
4306 s->selected_line >= MIN(s->blame.nlines,
4307 view->nlines - 2)) {
4308 break;
4310 if (s->last_displayed_line >= s->blame.nlines &&
4311 s->selected_line < view->nlines - 2) {
4312 s->selected_line = MIN(s->blame.nlines,
4313 view->nlines - 2);
4314 break;
4316 if (s->last_displayed_line + view->nlines - 2
4317 <= s->blame.nlines)
4318 s->first_displayed_line +=
4319 view->nlines - 2;
4320 else
4321 s->first_displayed_line =
4322 s->blame.nlines -
4323 (view->nlines - 3);
4324 break;
4325 case KEY_RESIZE:
4326 if (s->selected_line > view->nlines - 2) {
4327 s->selected_line = MIN(s->blame.nlines,
4328 view->nlines - 2);
4330 break;
4331 default:
4332 break;
4334 return thread_err ? thread_err : err;
4337 static const struct got_error *
4338 cmd_blame(int argc, char *argv[])
4340 const struct got_error *error;
4341 struct got_repository *repo = NULL;
4342 struct got_reflist_head refs;
4343 struct got_worktree *worktree = NULL;
4344 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4345 struct got_object_id *commit_id = NULL;
4346 char *commit_id_str = NULL;
4347 int ch;
4348 struct tog_view *view;
4350 SIMPLEQ_INIT(&refs);
4352 #ifndef PROFILE
4353 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4354 NULL) == -1)
4355 err(1, "pledge");
4356 #endif
4358 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4359 switch (ch) {
4360 case 'c':
4361 commit_id_str = optarg;
4362 break;
4363 case 'r':
4364 repo_path = realpath(optarg, NULL);
4365 if (repo_path == NULL)
4366 return got_error_from_errno2("realpath",
4367 optarg);
4368 break;
4369 default:
4370 usage_blame();
4371 /* NOTREACHED */
4375 argc -= optind;
4376 argv += optind;
4378 if (argc == 1)
4379 path = argv[0];
4380 else
4381 usage_blame();
4383 cwd = getcwd(NULL, 0);
4384 if (cwd == NULL) {
4385 error = got_error_from_errno("getcwd");
4386 goto done;
4388 if (repo_path == NULL) {
4389 error = got_worktree_open(&worktree, cwd);
4390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4391 goto done;
4392 else
4393 error = NULL;
4394 if (worktree) {
4395 repo_path =
4396 strdup(got_worktree_get_repo_path(worktree));
4397 if (repo_path == NULL)
4398 error = got_error_from_errno("strdup");
4399 if (error)
4400 goto done;
4401 } else {
4402 repo_path = strdup(cwd);
4403 if (repo_path == NULL) {
4404 error = got_error_from_errno("strdup");
4405 goto done;
4410 init_curses();
4412 error = got_repo_open(&repo, repo_path, NULL);
4413 if (error != NULL)
4414 goto done;
4416 error = apply_unveil(got_repo_get_path(repo), NULL);
4417 if (error)
4418 goto done;
4420 if (worktree) {
4421 const char *prefix = got_worktree_get_path_prefix(worktree);
4422 char *p, *worktree_subdir = cwd +
4423 strlen(got_worktree_get_root_path(worktree));
4424 if (asprintf(&p, "%s%s%s%s%s",
4425 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4426 worktree_subdir, worktree_subdir[0] ? "/" : "",
4427 path) == -1) {
4428 error = got_error_from_errno("asprintf");
4429 goto done;
4431 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4432 free(p);
4433 } else {
4434 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4436 if (error)
4437 goto done;
4439 if (commit_id_str == NULL) {
4440 struct got_reference *head_ref;
4441 error = got_ref_open(&head_ref, repo, worktree ?
4442 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4443 if (error != NULL)
4444 goto done;
4445 error = got_ref_resolve(&commit_id, repo, head_ref);
4446 got_ref_close(head_ref);
4447 } else {
4448 error = got_repo_match_object_id(&commit_id, NULL,
4449 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4451 if (error != NULL)
4452 goto done;
4454 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4455 if (error)
4456 goto done;
4458 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4459 if (view == NULL) {
4460 error = got_error_from_errno("view_open");
4461 goto done;
4463 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4464 if (error)
4465 goto done;
4466 if (worktree) {
4467 /* Release work tree lock. */
4468 got_worktree_close(worktree);
4469 worktree = NULL;
4471 error = view_loop(view);
4472 done:
4473 free(repo_path);
4474 free(cwd);
4475 free(commit_id);
4476 if (worktree)
4477 got_worktree_close(worktree);
4478 if (repo)
4479 got_repo_close(repo);
4480 got_ref_list_free(&refs);
4481 return error;
4484 static const struct got_error *
4485 draw_tree_entries(struct tog_view *view,
4486 struct got_tree_entry **first_displayed_entry,
4487 struct got_tree_entry **last_displayed_entry,
4488 struct got_tree_entry **selected_entry, int *ndisplayed,
4489 const char *label, int show_ids, const char *parent_path,
4490 struct got_tree_object *tree, int selected, int limit,
4491 int isroot, struct tog_colors *colors)
4493 const struct got_error *err = NULL;
4494 struct got_tree_entry *te;
4495 wchar_t *wline;
4496 struct tog_color *tc;
4497 int width, n, i, nentries;
4499 *ndisplayed = 0;
4501 werase(view->window);
4503 if (limit == 0)
4504 return NULL;
4506 err = format_line(&wline, &width, label, view->ncols, 0);
4507 if (err)
4508 return err;
4509 if (view_needs_focus_indication(view))
4510 wstandout(view->window);
4511 tc = get_color(colors, TOG_COLOR_COMMIT);
4512 if (tc)
4513 wattr_on(view->window,
4514 COLOR_PAIR(tc->colorpair), NULL);
4515 waddwstr(view->window, wline);
4516 if (tc)
4517 wattr_off(view->window,
4518 COLOR_PAIR(tc->colorpair), NULL);
4519 if (view_needs_focus_indication(view))
4520 wstandend(view->window);
4521 free(wline);
4522 wline = NULL;
4523 if (width < view->ncols - 1)
4524 waddch(view->window, '\n');
4525 if (--limit <= 0)
4526 return NULL;
4527 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4528 if (err)
4529 return err;
4530 waddwstr(view->window, wline);
4531 free(wline);
4532 wline = NULL;
4533 if (width < view->ncols - 1)
4534 waddch(view->window, '\n');
4535 if (--limit <= 0)
4536 return NULL;
4537 waddch(view->window, '\n');
4538 if (--limit <= 0)
4539 return NULL;
4541 if (*first_displayed_entry == NULL) {
4542 te = got_object_tree_get_first_entry(tree);
4543 if (selected == 0) {
4544 if (view->focussed)
4545 wstandout(view->window);
4546 *selected_entry = NULL;
4548 waddstr(view->window, " ..\n"); /* parent directory */
4549 if (selected == 0 && view->focussed)
4550 wstandend(view->window);
4551 (*ndisplayed)++;
4552 if (--limit <= 0)
4553 return NULL;
4554 n = 1;
4555 } else {
4556 n = 0;
4557 te = *first_displayed_entry;
4560 nentries = got_object_tree_get_nentries(tree);
4561 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4562 char *line = NULL, *id_str = NULL;
4563 const char *modestr = "";
4564 mode_t mode;
4566 te = got_object_tree_get_entry(tree, i);
4567 mode = got_tree_entry_get_mode(te);
4569 if (show_ids) {
4570 err = got_object_id_str(&id_str,
4571 got_tree_entry_get_id(te));
4572 if (err)
4573 return got_error_from_errno(
4574 "got_object_id_str");
4576 if (got_object_tree_entry_is_submodule(te))
4577 modestr = "$";
4578 else if (S_ISLNK(mode))
4579 modestr = "@";
4580 else if (S_ISDIR(mode))
4581 modestr = "/";
4582 else if (mode & S_IXUSR)
4583 modestr = "*";
4584 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4585 got_tree_entry_get_name(te), modestr) == -1) {
4586 free(id_str);
4587 return got_error_from_errno("asprintf");
4589 free(id_str);
4590 err = format_line(&wline, &width, line, view->ncols, 0);
4591 if (err) {
4592 free(line);
4593 break;
4595 if (n == selected) {
4596 if (view->focussed)
4597 wstandout(view->window);
4598 *selected_entry = te;
4600 tc = match_color(colors, line);
4601 if (tc)
4602 wattr_on(view->window,
4603 COLOR_PAIR(tc->colorpair), NULL);
4604 waddwstr(view->window, wline);
4605 if (tc)
4606 wattr_off(view->window,
4607 COLOR_PAIR(tc->colorpair), NULL);
4608 if (width < view->ncols - 1)
4609 waddch(view->window, '\n');
4610 if (n == selected && view->focussed)
4611 wstandend(view->window);
4612 free(line);
4613 free(wline);
4614 wline = NULL;
4615 n++;
4616 (*ndisplayed)++;
4617 *last_displayed_entry = te;
4618 if (--limit <= 0)
4619 break;
4622 return err;
4625 static void
4626 tree_scroll_up(struct tog_view *view,
4627 struct got_tree_entry **first_displayed_entry, int maxscroll,
4628 struct got_tree_object *tree, int isroot)
4630 struct got_tree_entry *te;
4631 int i;
4633 if (*first_displayed_entry == NULL)
4634 return;
4636 te = got_object_tree_get_entry(tree, 0);
4637 if (*first_displayed_entry == te) {
4638 if (!isroot)
4639 *first_displayed_entry = NULL;
4640 return;
4643 i = 0;
4644 while (*first_displayed_entry && i < maxscroll) {
4645 *first_displayed_entry = got_tree_entry_get_prev(tree,
4646 *first_displayed_entry);
4647 i++;
4649 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4650 *first_displayed_entry = NULL;
4653 static int
4654 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4655 struct got_tree_entry *last_displayed_entry,
4656 struct got_tree_object *tree)
4658 struct got_tree_entry *next, *last;
4659 int n = 0;
4661 if (*first_displayed_entry)
4662 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4663 else
4664 next = got_object_tree_get_first_entry(tree);
4666 last = last_displayed_entry;
4667 while (next && last && n++ < maxscroll) {
4668 last = got_tree_entry_get_next(tree, last);
4669 if (last) {
4670 *first_displayed_entry = next;
4671 next = got_tree_entry_get_next(tree, next);
4674 return n;
4677 static const struct got_error *
4678 tree_entry_path(char **path, struct tog_parent_trees *parents,
4679 struct got_tree_entry *te)
4681 const struct got_error *err = NULL;
4682 struct tog_parent_tree *pt;
4683 size_t len = 2; /* for leading slash and NUL */
4685 TAILQ_FOREACH(pt, parents, entry)
4686 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4687 + 1 /* slash */;
4688 if (te)
4689 len += strlen(got_tree_entry_get_name(te));
4691 *path = calloc(1, len);
4692 if (path == NULL)
4693 return got_error_from_errno("calloc");
4695 (*path)[0] = '/';
4696 pt = TAILQ_LAST(parents, tog_parent_trees);
4697 while (pt) {
4698 const char *name = got_tree_entry_get_name(pt->selected_entry);
4699 if (strlcat(*path, name, len) >= len) {
4700 err = got_error(GOT_ERR_NO_SPACE);
4701 goto done;
4703 if (strlcat(*path, "/", len) >= len) {
4704 err = got_error(GOT_ERR_NO_SPACE);
4705 goto done;
4707 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4709 if (te) {
4710 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4711 err = got_error(GOT_ERR_NO_SPACE);
4712 goto done;
4715 done:
4716 if (err) {
4717 free(*path);
4718 *path = NULL;
4720 return err;
4723 static const struct got_error *
4724 blame_tree_entry(struct tog_view **new_view, int begin_x,
4725 struct got_tree_entry *te, struct tog_parent_trees *parents,
4726 struct got_object_id *commit_id, struct got_reflist_head *refs,
4727 struct got_repository *repo)
4729 const struct got_error *err = NULL;
4730 char *path;
4731 struct tog_view *blame_view;
4733 *new_view = NULL;
4735 err = tree_entry_path(&path, parents, te);
4736 if (err)
4737 return err;
4739 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4740 if (blame_view == NULL) {
4741 err = got_error_from_errno("view_open");
4742 goto done;
4745 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4746 if (err) {
4747 if (err->code == GOT_ERR_CANCELLED)
4748 err = NULL;
4749 view_close(blame_view);
4750 } else
4751 *new_view = blame_view;
4752 done:
4753 free(path);
4754 return err;
4757 static const struct got_error *
4758 log_tree_entry(struct tog_view **new_view, int begin_x,
4759 struct got_tree_entry *te, struct tog_parent_trees *parents,
4760 struct got_object_id *commit_id, struct got_reflist_head *refs,
4761 struct got_repository *repo)
4763 struct tog_view *log_view;
4764 const struct got_error *err = NULL;
4765 char *path;
4767 *new_view = NULL;
4769 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4770 if (log_view == NULL)
4771 return got_error_from_errno("view_open");
4773 err = tree_entry_path(&path, parents, te);
4774 if (err)
4775 return err;
4777 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0, 0);
4778 if (err)
4779 view_close(log_view);
4780 else
4781 *new_view = log_view;
4782 free(path);
4783 return err;
4786 static const struct got_error *
4787 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4788 struct got_object_id *commit_id, struct got_reflist_head *refs,
4789 struct got_repository *repo)
4791 const struct got_error *err = NULL;
4792 char *commit_id_str = NULL;
4793 struct tog_tree_view_state *s = &view->state.tree;
4795 TAILQ_INIT(&s->parents);
4797 err = got_object_id_str(&commit_id_str, commit_id);
4798 if (err != NULL)
4799 goto done;
4801 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4802 err = got_error_from_errno("asprintf");
4803 goto done;
4806 s->root = s->tree = root;
4807 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4808 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4809 s->commit_id = got_object_id_dup(commit_id);
4810 if (s->commit_id == NULL) {
4811 err = got_error_from_errno("got_object_id_dup");
4812 goto done;
4814 s->refs = refs;
4815 s->repo = repo;
4817 SIMPLEQ_INIT(&s->colors);
4819 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4820 err = add_color(&s->colors, "\\$$",
4821 TOG_COLOR_TREE_SUBMODULE,
4822 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4823 if (err)
4824 goto done;
4825 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4826 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4827 if (err) {
4828 free_colors(&s->colors);
4829 goto done;
4831 err = add_color(&s->colors, "/$",
4832 TOG_COLOR_TREE_DIRECTORY,
4833 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4834 if (err) {
4835 free_colors(&s->colors);
4836 goto done;
4839 err = add_color(&s->colors, "\\*$",
4840 TOG_COLOR_TREE_EXECUTABLE,
4841 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4842 if (err) {
4843 free_colors(&s->colors);
4844 goto done;
4847 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4848 get_color_value("TOG_COLOR_COMMIT"));
4849 if (err) {
4850 free_colors(&s->colors);
4851 goto done;
4855 view->show = show_tree_view;
4856 view->input = input_tree_view;
4857 view->close = close_tree_view;
4858 view->search_start = search_start_tree_view;
4859 view->search_next = search_next_tree_view;
4860 done:
4861 free(commit_id_str);
4862 if (err) {
4863 free(s->tree_label);
4864 s->tree_label = NULL;
4866 return err;
4869 static const struct got_error *
4870 close_tree_view(struct tog_view *view)
4872 struct tog_tree_view_state *s = &view->state.tree;
4874 free_colors(&s->colors);
4875 free(s->tree_label);
4876 s->tree_label = NULL;
4877 free(s->commit_id);
4878 s->commit_id = NULL;
4879 while (!TAILQ_EMPTY(&s->parents)) {
4880 struct tog_parent_tree *parent;
4881 parent = TAILQ_FIRST(&s->parents);
4882 TAILQ_REMOVE(&s->parents, parent, entry);
4883 free(parent);
4886 if (s->tree != s->root)
4887 got_object_tree_close(s->tree);
4888 got_object_tree_close(s->root);
4890 return NULL;
4893 static const struct got_error *
4894 search_start_tree_view(struct tog_view *view)
4896 struct tog_tree_view_state *s = &view->state.tree;
4898 s->matched_entry = NULL;
4899 return NULL;
4902 static int
4903 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4905 regmatch_t regmatch;
4907 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4908 0) == 0;
4911 static const struct got_error *
4912 search_next_tree_view(struct tog_view *view)
4914 struct tog_tree_view_state *s = &view->state.tree;
4915 struct got_tree_entry *te = NULL;
4917 if (!view->searching) {
4918 view->search_next_done = 1;
4919 return NULL;
4922 if (s->matched_entry) {
4923 if (view->searching == TOG_SEARCH_FORWARD) {
4924 if (s->selected_entry)
4925 te = got_tree_entry_get_next(s->tree,
4926 s->selected_entry);
4927 else
4928 te = got_object_tree_get_first_entry(s->tree);
4929 } else {
4930 if (s->selected_entry == NULL)
4931 te = got_object_tree_get_last_entry(s->tree);
4932 else
4933 te = got_tree_entry_get_prev(s->tree,
4934 s->selected_entry);
4936 } else {
4937 if (view->searching == TOG_SEARCH_FORWARD)
4938 te = got_object_tree_get_first_entry(s->tree);
4939 else
4940 te = got_object_tree_get_last_entry(s->tree);
4943 while (1) {
4944 if (te == NULL) {
4945 if (s->matched_entry == NULL) {
4946 view->search_next_done = 1;
4947 return NULL;
4949 if (view->searching == TOG_SEARCH_FORWARD)
4950 te = got_object_tree_get_first_entry(s->tree);
4951 else
4952 te = got_object_tree_get_last_entry(s->tree);
4955 if (match_tree_entry(te, &view->regex)) {
4956 view->search_next_done = 1;
4957 s->matched_entry = te;
4958 break;
4961 if (view->searching == TOG_SEARCH_FORWARD)
4962 te = got_tree_entry_get_next(s->tree, te);
4963 else
4964 te = got_tree_entry_get_prev(s->tree, te);
4967 if (s->matched_entry) {
4968 s->first_displayed_entry = s->matched_entry;
4969 s->selected = 0;
4972 return NULL;
4975 static const struct got_error *
4976 show_tree_view(struct tog_view *view)
4978 const struct got_error *err = NULL;
4979 struct tog_tree_view_state *s = &view->state.tree;
4980 char *parent_path;
4982 err = tree_entry_path(&parent_path, &s->parents, NULL);
4983 if (err)
4984 return err;
4986 err = draw_tree_entries(view, &s->first_displayed_entry,
4987 &s->last_displayed_entry, &s->selected_entry,
4988 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4989 s->tree, s->selected, view->nlines, s->tree == s->root,
4990 &s->colors);
4991 free(parent_path);
4993 view_vborder(view);
4994 return err;
4997 static const struct got_error *
4998 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4999 struct tog_view **focus_view, struct tog_view *view, int ch)
5001 const struct got_error *err = NULL;
5002 struct tog_tree_view_state *s = &view->state.tree;
5003 struct tog_view *log_view;
5004 int begin_x = 0, nscrolled;
5006 switch (ch) {
5007 case 'i':
5008 s->show_ids = !s->show_ids;
5009 break;
5010 case 'l':
5011 if (!s->selected_entry)
5012 break;
5013 if (view_is_parent_view(view))
5014 begin_x = view_split_begin_x(view->begin_x);
5015 err = log_tree_entry(&log_view, begin_x,
5016 s->selected_entry, &s->parents,
5017 s->commit_id, s->refs, s->repo);
5018 if (view_is_parent_view(view)) {
5019 err = view_close_child(view);
5020 if (err)
5021 return err;
5022 err = view_set_child(view, log_view);
5023 if (err) {
5024 view_close(log_view);
5025 break;
5027 *focus_view = log_view;
5028 view->child_focussed = 1;
5029 } else
5030 *new_view = log_view;
5031 break;
5032 case 'k':
5033 case KEY_UP:
5034 if (s->selected > 0) {
5035 s->selected--;
5036 if (s->selected == 0)
5037 break;
5039 if (s->selected > 0)
5040 break;
5041 tree_scroll_up(view, &s->first_displayed_entry, 1,
5042 s->tree, s->tree == s->root);
5043 break;
5044 case KEY_PPAGE:
5045 tree_scroll_up(view, &s->first_displayed_entry,
5046 MAX(0, view->nlines - 4 - s->selected), s->tree,
5047 s->tree == s->root);
5048 s->selected = 0;
5049 if (got_object_tree_get_first_entry(s->tree) ==
5050 s->first_displayed_entry && s->tree != s->root)
5051 s->first_displayed_entry = NULL;
5052 break;
5053 case 'j':
5054 case KEY_DOWN:
5055 if (s->selected < s->ndisplayed - 1) {
5056 s->selected++;
5057 break;
5059 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5060 == NULL)
5061 /* can't scroll any further */
5062 break;
5063 tree_scroll_down(&s->first_displayed_entry, 1,
5064 s->last_displayed_entry, s->tree);
5065 break;
5066 case KEY_NPAGE:
5067 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5068 == NULL) {
5069 /* can't scroll any further; move cursor down */
5070 if (s->selected < s->ndisplayed - 1)
5071 s->selected = s->ndisplayed - 1;
5072 break;
5074 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5075 view->nlines, s->last_displayed_entry, s->tree);
5076 if (nscrolled < view->nlines) {
5077 int ndisplayed = 0;
5078 struct got_tree_entry *te;
5079 te = s->first_displayed_entry;
5080 do {
5081 ndisplayed++;
5082 te = got_tree_entry_get_next(s->tree, te);
5083 } while (te);
5084 s->selected = ndisplayed - 1;
5086 break;
5087 case KEY_ENTER:
5088 case '\r':
5089 case KEY_BACKSPACE:
5090 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5091 struct tog_parent_tree *parent;
5092 /* user selected '..' */
5093 if (s->tree == s->root)
5094 break;
5095 parent = TAILQ_FIRST(&s->parents);
5096 TAILQ_REMOVE(&s->parents, parent,
5097 entry);
5098 got_object_tree_close(s->tree);
5099 s->tree = parent->tree;
5100 s->first_displayed_entry =
5101 parent->first_displayed_entry;
5102 s->selected_entry =
5103 parent->selected_entry;
5104 s->selected = parent->selected;
5105 free(parent);
5106 } else if (S_ISDIR(got_tree_entry_get_mode(
5107 s->selected_entry))) {
5108 struct got_tree_object *subtree;
5109 err = got_object_open_as_tree(&subtree, s->repo,
5110 got_tree_entry_get_id(s->selected_entry));
5111 if (err)
5112 break;
5113 err = tree_view_visit_subtree(subtree, s);
5114 if (err) {
5115 got_object_tree_close(subtree);
5116 break;
5118 } else if (S_ISREG(got_tree_entry_get_mode(
5119 s->selected_entry))) {
5120 struct tog_view *blame_view;
5121 int begin_x = view_is_parent_view(view) ?
5122 view_split_begin_x(view->begin_x) : 0;
5124 err = blame_tree_entry(&blame_view, begin_x,
5125 s->selected_entry, &s->parents,
5126 s->commit_id, s->refs, s->repo);
5127 if (err)
5128 break;
5129 if (view_is_parent_view(view)) {
5130 err = view_close_child(view);
5131 if (err)
5132 return err;
5133 err = view_set_child(view, blame_view);
5134 if (err) {
5135 view_close(blame_view);
5136 break;
5138 *focus_view = blame_view;
5139 view->child_focussed = 1;
5140 } else
5141 *new_view = blame_view;
5143 break;
5144 case KEY_RESIZE:
5145 if (s->selected > view->nlines)
5146 s->selected = s->ndisplayed - 1;
5147 break;
5148 default:
5149 break;
5152 return err;
5155 __dead static void
5156 usage_tree(void)
5158 endwin();
5159 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path]\n",
5160 getprogname());
5161 exit(1);
5164 static const struct got_error *
5165 cmd_tree(int argc, char *argv[])
5167 const struct got_error *error;
5168 struct got_repository *repo = NULL;
5169 struct got_reflist_head refs;
5170 char *repo_path = NULL;
5171 struct got_object_id *commit_id = NULL;
5172 char *commit_id_arg = NULL;
5173 struct got_commit_object *commit = NULL;
5174 struct got_tree_object *tree = NULL;
5175 int ch;
5176 struct tog_view *view;
5178 SIMPLEQ_INIT(&refs);
5180 #ifndef PROFILE
5181 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5182 NULL) == -1)
5183 err(1, "pledge");
5184 #endif
5186 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5187 switch (ch) {
5188 case 'c':
5189 commit_id_arg = optarg;
5190 break;
5191 case 'r':
5192 repo_path = realpath(optarg, NULL);
5193 if (repo_path == NULL)
5194 return got_error_from_errno2("realpath",
5195 optarg);
5196 break;
5197 default:
5198 usage_tree();
5199 /* NOTREACHED */
5203 argc -= optind;
5204 argv += optind;
5206 if (argc != 0)
5207 usage_tree();
5209 if (repo_path == NULL) {
5210 struct got_worktree *worktree;
5211 char *cwd = getcwd(NULL, 0);
5212 if (cwd == NULL)
5213 return got_error_from_errno("getcwd");
5214 error = got_worktree_open(&worktree, cwd);
5215 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5216 goto done;
5217 else
5218 error = NULL;
5219 if (worktree) {
5220 free(cwd);
5221 repo_path =
5222 strdup(got_worktree_get_repo_path(worktree));
5223 got_worktree_close(worktree);
5224 } else
5225 repo_path = cwd;
5226 if (repo_path == NULL) {
5227 error = got_error_from_errno("strdup");
5228 goto done;
5232 init_curses();
5234 error = got_repo_open(&repo, repo_path, NULL);
5235 if (error != NULL)
5236 goto done;
5238 error = apply_unveil(got_repo_get_path(repo), NULL);
5239 if (error)
5240 goto done;
5242 error = got_repo_match_object_id(&commit_id, NULL,
5243 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5244 GOT_OBJ_TYPE_COMMIT, 1, repo);
5245 if (error != NULL)
5246 goto done;
5248 error = got_object_open_as_commit(&commit, repo, commit_id);
5249 if (error != NULL)
5250 goto done;
5252 error = got_object_open_as_tree(&tree, repo,
5253 got_object_commit_get_tree_id(commit));
5254 if (error != NULL)
5255 goto done;
5257 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5258 if (error)
5259 goto done;
5261 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5262 if (view == NULL) {
5263 error = got_error_from_errno("view_open");
5264 goto done;
5266 error = open_tree_view(view, tree, commit_id, &refs, repo);
5267 if (error)
5268 goto done;
5269 error = view_loop(view);
5270 done:
5271 free(repo_path);
5272 free(commit_id);
5273 if (commit)
5274 got_object_commit_close(commit);
5275 if (tree)
5276 got_object_tree_close(tree);
5277 if (repo)
5278 got_repo_close(repo);
5279 got_ref_list_free(&refs);
5280 return error;
5283 static void
5284 list_commands(void)
5286 int i;
5288 fprintf(stderr, "commands:");
5289 for (i = 0; i < nitems(tog_commands); i++) {
5290 struct tog_cmd *cmd = &tog_commands[i];
5291 fprintf(stderr, " %s", cmd->name);
5293 fputc('\n', stderr);
5296 __dead static void
5297 usage(int hflag)
5299 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
5300 getprogname());
5301 if (hflag)
5302 list_commands();
5303 exit(1);
5306 static char **
5307 make_argv(const char *arg0, const char *arg1)
5309 char **argv;
5310 int argc = (arg1 == NULL ? 1 : 2);
5312 argv = calloc(argc, sizeof(char *));
5313 if (argv == NULL)
5314 err(1, "calloc");
5315 argv[0] = strdup(arg0);
5316 if (argv[0] == NULL)
5317 err(1, "strdup");
5318 if (arg1) {
5319 argv[1] = strdup(arg1);
5320 if (argv[1] == NULL)
5321 err(1, "strdup");
5324 return argv;
5327 int
5328 main(int argc, char *argv[])
5330 const struct got_error *error = NULL;
5331 struct tog_cmd *cmd = NULL;
5332 int ch, hflag = 0, Vflag = 0;
5333 char **cmd_argv = NULL;
5334 static struct option longopts[] = {
5335 { "version", no_argument, NULL, 'V' },
5336 { NULL, 0, NULL, 0}
5339 setlocale(LC_CTYPE, "");
5341 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5342 switch (ch) {
5343 case 'h':
5344 hflag = 1;
5345 break;
5346 case 'V':
5347 Vflag = 1;
5348 break;
5349 default:
5350 usage(hflag);
5351 /* NOTREACHED */
5355 argc -= optind;
5356 argv += optind;
5357 optind = 0;
5358 optreset = 1;
5360 if (Vflag) {
5361 got_version_print_str();
5362 return 1;
5365 if (argc == 0) {
5366 if (hflag)
5367 usage(hflag);
5368 /* Build an argument vector which runs a default command. */
5369 cmd = &tog_commands[0];
5370 cmd_argv = make_argv(cmd->name, NULL);
5371 argc = 1;
5372 } else {
5373 int i;
5375 /* Did the user specific a command? */
5376 for (i = 0; i < nitems(tog_commands); i++) {
5377 if (strncmp(tog_commands[i].name, argv[0],
5378 strlen(argv[0])) == 0) {
5379 cmd = &tog_commands[i];
5380 break;
5384 if (cmd == NULL) {
5385 fprintf(stderr, "%s: unknown command '%s'\n",
5386 getprogname(), argv[0]);
5387 list_commands();
5388 return 1;
5392 if (hflag)
5393 cmd->cmd_usage();
5394 else
5395 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5397 endwin();
5398 free(cmd_argv);
5399 if (error && error->code != GOT_ERR_CANCELLED)
5400 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5401 return 0;