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 <stdarg.h>
30 #include <stdio.h>
31 #include <getopt.h>
32 #include <string.h>
33 #include <err.h>
34 #include <unistd.h>
35 #include <util.h>
36 #include <limits.h>
37 #include <wchar.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <libgen.h>
41 #include <regex.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
48 #include "got_diff.h"
49 #include "got_opentemp.h"
50 #include "got_utf8.h"
51 #include "got_cancel.h"
52 #include "got_commit_graph.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_path.h"
56 #include "got_worktree.h"
58 #ifndef MIN
59 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #endif
62 #ifndef MAX
63 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #endif
66 #define CTRL(x) ((x) & 0x1f)
68 #ifndef nitems
69 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #endif
72 struct tog_cmd {
73 const char *name;
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 };
78 __dead static void usage(int);
79 __dead static void usage_log(void);
80 __dead static void usage_diff(void);
81 __dead static void usage_blame(void);
82 __dead static void usage_tree(void);
84 static const struct got_error* cmd_log(int, char *[]);
85 static const struct got_error* cmd_diff(int, char *[]);
86 static const struct got_error* cmd_blame(int, char *[]);
87 static const struct got_error* cmd_tree(int, char *[]);
89 static struct tog_cmd tog_commands[] = {
90 { "log", cmd_log, usage_log },
91 { "diff", cmd_diff, usage_diff },
92 { "blame", cmd_blame, usage_blame },
93 { "tree", cmd_tree, usage_tree },
94 };
96 enum tog_view_type {
97 TOG_VIEW_DIFF,
98 TOG_VIEW_LOG,
99 TOG_VIEW_BLAME,
100 TOG_VIEW_TREE
101 };
103 #define TOG_EOF_STRING "(END)"
105 struct commit_queue_entry {
106 TAILQ_ENTRY(commit_queue_entry) entry;
107 struct got_object_id *id;
108 struct got_commit_object *commit;
109 int idx;
110 };
111 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
112 struct commit_queue {
113 int ncommits;
114 struct commit_queue_head head;
115 };
117 struct tog_color {
118 SIMPLEQ_ENTRY(tog_color) entry;
119 regex_t regex;
120 short colorpair;
121 };
122 SIMPLEQ_HEAD(tog_colors, tog_color);
124 static const struct got_error *
125 add_color(struct tog_colors *colors, const char *pattern,
126 int idx, short color)
128 const struct got_error *err = NULL;
129 struct tog_color *tc;
130 int regerr = 0;
132 if (idx < 1 || idx > COLOR_PAIRS - 1)
133 return NULL;
135 init_pair(idx, color, -1);
137 tc = calloc(1, sizeof(*tc));
138 if (tc == NULL)
139 return got_error_from_errno("calloc");
140 regerr = regcomp(&tc->regex, pattern,
141 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
142 if (regerr) {
143 static char regerr_msg[512];
144 static char err_msg[512];
145 regerror(regerr, &tc->regex, regerr_msg,
146 sizeof(regerr_msg));
147 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
148 regerr_msg);
149 err = got_error_msg(GOT_ERR_REGEX, err_msg);
150 free(tc);
151 return err;
153 tc->colorpair = idx;
154 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
155 return NULL;
158 static void
159 free_colors(struct tog_colors *colors)
161 struct tog_color *tc;
163 while (!SIMPLEQ_EMPTY(colors)) {
164 tc = SIMPLEQ_FIRST(colors);
165 SIMPLEQ_REMOVE_HEAD(colors, entry);
166 regfree(&tc->regex);
167 free(tc);
171 struct tog_color *
172 get_color(struct tog_colors *colors, int colorpair)
174 struct tog_color *tc = NULL;
176 SIMPLEQ_FOREACH(tc, colors, entry) {
177 if (tc->colorpair == colorpair)
178 return tc;
181 return NULL;
184 static int
185 default_color_value(const char *envvar)
187 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
188 return COLOR_MAGENTA;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
190 return COLOR_CYAN;
191 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
192 return COLOR_YELLOW;
193 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
194 return COLOR_GREEN;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
196 return COLOR_MAGENTA;
197 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
198 return COLOR_MAGENTA;
199 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
200 return COLOR_CYAN;
201 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
202 return COLOR_GREEN;
203 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
204 return COLOR_GREEN;
205 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
206 return COLOR_CYAN;
207 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
208 return COLOR_YELLOW;
210 return -1;
213 static int
214 get_color_value(const char *envvar)
216 const char *val = getenv(envvar);
218 if (val == NULL)
219 return default_color_value(envvar);
221 if (strcasecmp(val, "black") == 0)
222 return COLOR_BLACK;
223 if (strcasecmp(val, "red") == 0)
224 return COLOR_RED;
225 if (strcasecmp(val, "green") == 0)
226 return COLOR_GREEN;
227 if (strcasecmp(val, "yellow") == 0)
228 return COLOR_YELLOW;
229 if (strcasecmp(val, "blue") == 0)
230 return COLOR_BLUE;
231 if (strcasecmp(val, "magenta") == 0)
232 return COLOR_MAGENTA;
233 if (strcasecmp(val, "cyan") == 0)
234 return COLOR_CYAN;
235 if (strcasecmp(val, "white") == 0)
236 return COLOR_WHITE;
237 if (strcasecmp(val, "default") == 0)
238 return -1;
240 return default_color_value(envvar);
244 struct tog_diff_view_state {
245 struct got_object_id *id1, *id2;
246 FILE *f;
247 int first_displayed_line;
248 int last_displayed_line;
249 int eof;
250 int diff_context;
251 struct got_repository *repo;
252 struct got_reflist_head *refs;
253 struct tog_colors colors;
254 int nlines;
255 off_t *line_offsets;
256 int matched_line;
257 int selected_line;
258 size_t filesize;
260 /* passed from log view; may be NULL */
261 struct tog_view *log_view;
262 };
264 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
266 struct tog_log_thread_args {
267 pthread_cond_t need_commits;
268 int commits_needed;
269 struct got_commit_graph *graph;
270 struct commit_queue *commits;
271 const char *in_repo_path;
272 struct got_object_id *start_id;
273 struct got_repository *repo;
274 int log_complete;
275 sig_atomic_t *quit;
276 struct commit_queue_entry **first_displayed_entry;
277 struct commit_queue_entry **selected_entry;
278 int *searching;
279 int *search_next_done;
280 regex_t *regex;
281 };
283 struct tog_log_view_state {
284 struct commit_queue commits;
285 struct commit_queue_entry *first_displayed_entry;
286 struct commit_queue_entry *last_displayed_entry;
287 struct commit_queue_entry *selected_entry;
288 int selected;
289 char *in_repo_path;
290 const char *head_ref_name;
291 int log_branches;
292 struct got_repository *repo;
293 struct got_reflist_head *refs;
294 struct got_object_id *start_id;
295 sig_atomic_t quit;
296 pthread_t thread;
297 struct tog_log_thread_args thread_args;
298 struct commit_queue_entry *matched_entry;
299 struct commit_queue_entry *search_entry;
300 struct tog_colors colors;
301 };
303 #define TOG_COLOR_DIFF_MINUS 1
304 #define TOG_COLOR_DIFF_PLUS 2
305 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
306 #define TOG_COLOR_DIFF_META 4
307 #define TOG_COLOR_TREE_SUBMODULE 5
308 #define TOG_COLOR_TREE_SYMLINK 6
309 #define TOG_COLOR_TREE_DIRECTORY 7
310 #define TOG_COLOR_TREE_EXECUTABLE 8
311 #define TOG_COLOR_COMMIT 9
312 #define TOG_COLOR_AUTHOR 10
313 #define TOG_COLOR_DATE 11
315 struct tog_blame_cb_args {
316 struct tog_blame_line *lines; /* one per line */
317 int nlines;
319 struct tog_view *view;
320 struct got_object_id *commit_id;
321 int *quit;
322 };
324 struct tog_blame_thread_args {
325 const char *path;
326 struct got_repository *repo;
327 struct tog_blame_cb_args *cb_args;
328 int *complete;
329 got_cancel_cb cancel_cb;
330 void *cancel_arg;
331 };
333 struct tog_blame {
334 FILE *f;
335 size_t filesize;
336 struct tog_blame_line *lines;
337 int nlines;
338 off_t *line_offsets;
339 pthread_t thread;
340 struct tog_blame_thread_args thread_args;
341 struct tog_blame_cb_args cb_args;
342 const char *path;
343 };
345 struct tog_blame_view_state {
346 int first_displayed_line;
347 int last_displayed_line;
348 int selected_line;
349 int blame_complete;
350 int eof;
351 int done;
352 struct got_object_id_queue blamed_commits;
353 struct got_object_qid *blamed_commit;
354 char *path;
355 struct got_repository *repo;
356 struct got_reflist_head *refs;
357 struct got_object_id *commit_id;
358 struct tog_blame blame;
359 int matched_line;
360 struct tog_colors colors;
361 };
363 struct tog_parent_tree {
364 TAILQ_ENTRY(tog_parent_tree) entry;
365 struct got_tree_object *tree;
366 struct got_tree_entry *first_displayed_entry;
367 struct got_tree_entry *selected_entry;
368 int selected;
369 };
371 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
373 struct tog_tree_view_state {
374 char *tree_label;
375 struct got_tree_object *root;
376 struct got_tree_object *tree;
377 struct got_tree_entry *first_displayed_entry;
378 struct got_tree_entry *last_displayed_entry;
379 struct got_tree_entry *selected_entry;
380 int ndisplayed, selected, show_ids;
381 struct tog_parent_trees parents;
382 struct got_object_id *commit_id;
383 struct got_repository *repo;
384 struct got_reflist_head *refs;
385 struct got_tree_entry *matched_entry;
386 struct tog_colors colors;
387 };
389 /*
390 * We implement two types of views: parent views and child views.
392 * The 'Tab' key switches between a parent view and its child view.
393 * Child views are shown side-by-side to their parent view, provided
394 * there is enough screen estate.
396 * When a new view is opened from within a parent view, this new view
397 * becomes a child view of the parent view, replacing any existing child.
399 * When a new view is opened from within a child view, this new view
400 * becomes a parent view which will obscure the views below until the
401 * user quits the new parent view by typing 'q'.
403 * This list of views contains parent views only.
404 * Child views are only pointed to by their parent view.
405 */
406 TAILQ_HEAD(tog_view_list_head, tog_view);
408 struct tog_view {
409 TAILQ_ENTRY(tog_view) entry;
410 WINDOW *window;
411 PANEL *panel;
412 int nlines, ncols, begin_y, begin_x;
413 int lines, cols; /* copies of LINES and COLS */
414 int focussed;
415 struct tog_view *parent;
416 struct tog_view *child;
417 int child_focussed;
419 /* type-specific state */
420 enum tog_view_type type;
421 union {
422 struct tog_diff_view_state diff;
423 struct tog_log_view_state log;
424 struct tog_blame_view_state blame;
425 struct tog_tree_view_state tree;
426 } state;
428 const struct got_error *(*show)(struct tog_view *);
429 const struct got_error *(*input)(struct tog_view **,
430 struct tog_view **, struct tog_view**, struct tog_view *, int);
431 const struct got_error *(*close)(struct tog_view *);
433 const struct got_error *(*search_start)(struct tog_view *);
434 const struct got_error *(*search_next)(struct tog_view *);
435 int searching;
436 #define TOG_SEARCH_FORWARD 1
437 #define TOG_SEARCH_BACKWARD 2
438 int search_next_done;
439 regex_t regex;
440 };
442 static const struct got_error *open_diff_view(struct tog_view *,
443 struct got_object_id *, struct got_object_id *, struct tog_view *,
444 struct got_reflist_head *, struct got_repository *);
445 static const struct got_error *show_diff_view(struct tog_view *);
446 static const struct got_error *input_diff_view(struct tog_view **,
447 struct tog_view **, struct tog_view **, struct tog_view *, int);
448 static const struct got_error* close_diff_view(struct tog_view *);
449 static const struct got_error *search_start_diff_view(struct tog_view *);
450 static const struct got_error *search_next_diff_view(struct tog_view *);
452 static const struct got_error *open_log_view(struct tog_view *,
453 struct got_object_id *, struct got_reflist_head *,
454 struct got_repository *, const char *, const char *, int);
455 static const struct got_error * show_log_view(struct tog_view *);
456 static const struct got_error *input_log_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_log_view(struct tog_view *);
459 static const struct got_error *search_start_log_view(struct tog_view *);
460 static const struct got_error *search_next_log_view(struct tog_view *);
462 static const struct got_error *open_blame_view(struct tog_view *, char *,
463 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
464 static const struct got_error *show_blame_view(struct tog_view *);
465 static const struct got_error *input_blame_view(struct tog_view **,
466 struct tog_view **, struct tog_view **, struct tog_view *, int);
467 static const struct got_error *close_blame_view(struct tog_view *);
468 static const struct got_error *search_start_blame_view(struct tog_view *);
469 static const struct got_error *search_next_blame_view(struct tog_view *);
471 static const struct got_error *open_tree_view(struct tog_view *,
472 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
473 struct got_repository *);
474 static const struct got_error *show_tree_view(struct tog_view *);
475 static const struct got_error *input_tree_view(struct tog_view **,
476 struct tog_view **, struct tog_view **, struct tog_view *, int);
477 static const struct got_error *close_tree_view(struct tog_view *);
478 static const struct got_error *search_start_tree_view(struct tog_view *);
479 static const struct got_error *search_next_tree_view(struct tog_view *);
481 static volatile sig_atomic_t tog_sigwinch_received;
482 static volatile sig_atomic_t tog_sigpipe_received;
483 static volatile sig_atomic_t tog_sigcont_received;
485 static void
486 tog_sigwinch(int signo)
488 tog_sigwinch_received = 1;
491 static void
492 tog_sigpipe(int signo)
494 tog_sigpipe_received = 1;
497 static void
498 tog_sigcont(int signo)
500 tog_sigcont_received = 1;
503 static const struct got_error *
504 view_close(struct tog_view *view)
506 const struct got_error *err = NULL;
508 if (view->child) {
509 view_close(view->child);
510 view->child = NULL;
512 if (view->close)
513 err = view->close(view);
514 if (view->panel)
515 del_panel(view->panel);
516 if (view->window)
517 delwin(view->window);
518 free(view);
519 return err;
522 static struct tog_view *
523 view_open(int nlines, int ncols, int begin_y, int begin_x,
524 enum tog_view_type type)
526 struct tog_view *view = calloc(1, sizeof(*view));
528 if (view == NULL)
529 return NULL;
531 view->type = type;
532 view->lines = LINES;
533 view->cols = COLS;
534 view->nlines = nlines ? nlines : LINES - begin_y;
535 view->ncols = ncols ? ncols : COLS - begin_x;
536 view->begin_y = begin_y;
537 view->begin_x = begin_x;
538 view->window = newwin(nlines, ncols, begin_y, begin_x);
539 if (view->window == NULL) {
540 view_close(view);
541 return NULL;
543 view->panel = new_panel(view->window);
544 if (view->panel == NULL ||
545 set_panel_userptr(view->panel, view) != OK) {
546 view_close(view);
547 return NULL;
550 keypad(view->window, TRUE);
551 return view;
554 static int
555 view_split_begin_x(int begin_x)
557 if (begin_x > 0 || COLS < 120)
558 return 0;
559 return (COLS - MAX(COLS / 2, 80));
562 static const struct got_error *view_resize(struct tog_view *);
564 static const struct got_error *
565 view_splitscreen(struct tog_view *view)
567 const struct got_error *err = NULL;
569 view->begin_y = 0;
570 view->begin_x = view_split_begin_x(0);
571 view->nlines = LINES;
572 view->ncols = COLS - view->begin_x;
573 view->lines = LINES;
574 view->cols = COLS;
575 err = view_resize(view);
576 if (err)
577 return err;
579 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
580 return got_error_from_errno("mvwin");
582 return NULL;
585 static const struct got_error *
586 view_fullscreen(struct tog_view *view)
588 const struct got_error *err = NULL;
590 view->begin_x = 0;
591 view->begin_y = 0;
592 view->nlines = LINES;
593 view->ncols = COLS;
594 view->lines = LINES;
595 view->cols = COLS;
596 err = view_resize(view);
597 if (err)
598 return err;
600 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
601 return got_error_from_errno("mvwin");
603 return NULL;
606 static int
607 view_is_parent_view(struct tog_view *view)
609 return view->parent == NULL;
612 static const struct got_error *
613 view_resize(struct tog_view *view)
615 int nlines, ncols;
617 if (view->lines > LINES)
618 nlines = view->nlines - (view->lines - LINES);
619 else
620 nlines = view->nlines + (LINES - view->lines);
622 if (view->cols > COLS)
623 ncols = view->ncols - (view->cols - COLS);
624 else
625 ncols = view->ncols + (COLS - view->cols);
627 if (wresize(view->window, nlines, ncols) == ERR)
628 return got_error_from_errno("wresize");
629 if (replace_panel(view->panel, view->window) == ERR)
630 return got_error_from_errno("replace_panel");
631 wclear(view->window);
633 view->nlines = nlines;
634 view->ncols = ncols;
635 view->lines = LINES;
636 view->cols = COLS;
638 if (view->child) {
639 view->child->begin_x = view_split_begin_x(view->begin_x);
640 if (view->child->begin_x == 0) {
641 view_fullscreen(view->child);
642 if (view->child->focussed)
643 show_panel(view->child->panel);
644 else
645 show_panel(view->panel);
646 } else {
647 view_splitscreen(view->child);
648 show_panel(view->child->panel);
652 return NULL;
655 static const struct got_error *
656 view_close_child(struct tog_view *view)
658 const struct got_error *err = NULL;
660 if (view->child == NULL)
661 return NULL;
663 err = view_close(view->child);
664 view->child = NULL;
665 return err;
668 static const struct got_error *
669 view_set_child(struct tog_view *view, struct tog_view *child)
671 const struct got_error *err = NULL;
673 view->child = child;
674 child->parent = view;
675 return err;
678 static int
679 view_is_splitscreen(struct tog_view *view)
681 return view->begin_x > 0;
684 static void
685 tog_resizeterm(void)
687 int cols, lines;
688 struct winsize size;
690 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
691 cols = 80; /* Default */
692 lines = 24;
693 } else {
694 cols = size.ws_col;
695 lines = size.ws_row;
697 resize_term(lines, cols);
700 static const struct got_error *
701 view_search_start(struct tog_view *view)
703 const struct got_error *err = NULL;
704 char pattern[1024];
705 int ret;
707 if (view->nlines < 1)
708 return NULL;
710 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
711 wclrtoeol(view->window);
713 nocbreak();
714 echo();
715 ret = wgetnstr(view->window, pattern, sizeof(pattern));
716 cbreak();
717 noecho();
718 if (ret == ERR)
719 return NULL;
721 if (view->searching) {
722 regfree(&view->regex);
723 view->searching = 0;
726 if (regcomp(&view->regex, pattern,
727 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
728 err = view->search_start(view);
729 if (err) {
730 regfree(&view->regex);
731 return err;
733 view->searching = TOG_SEARCH_FORWARD;
734 view->search_next_done = 0;
735 view->search_next(view);
738 return NULL;
741 static const struct got_error *
742 view_input(struct tog_view **new, struct tog_view **dead,
743 struct tog_view **focus, int *done, struct tog_view *view,
744 struct tog_view_list_head *views)
746 const struct got_error *err = NULL;
747 struct tog_view *v;
748 int ch, errcode;
750 *new = NULL;
751 *dead = NULL;
752 *focus = NULL;
754 if (view->searching && !view->search_next_done) {
755 errcode = pthread_mutex_unlock(&tog_mutex);
756 if (errcode)
757 return got_error_set_errno(errcode,
758 "pthread_mutex_unlock");
759 pthread_yield();
760 errcode = pthread_mutex_lock(&tog_mutex);
761 if (errcode)
762 return got_error_set_errno(errcode,
763 "pthread_mutex_lock");
764 view->search_next(view);
765 return NULL;
768 nodelay(stdscr, FALSE);
769 /* Allow threads to make progress while we are waiting for input. */
770 errcode = pthread_mutex_unlock(&tog_mutex);
771 if (errcode)
772 return got_error_set_errno(errcode, "pthread_mutex_unlock");
773 ch = wgetch(view->window);
774 errcode = pthread_mutex_lock(&tog_mutex);
775 if (errcode)
776 return got_error_set_errno(errcode, "pthread_mutex_lock");
777 nodelay(stdscr, TRUE);
779 if (tog_sigwinch_received || tog_sigcont_received) {
780 tog_resizeterm();
781 tog_sigwinch_received = 0;
782 tog_sigcont_received = 0;
783 TAILQ_FOREACH(v, views, entry) {
784 err = view_resize(v);
785 if (err)
786 return err;
787 err = v->input(new, dead, focus, v, KEY_RESIZE);
788 if (err)
789 return err;
793 switch (ch) {
794 case ERR:
795 break;
796 case '\t':
797 if (view->child) {
798 *focus = view->child;
799 view->child_focussed = 1;
800 } else if (view->parent) {
801 *focus = view->parent;
802 view->parent->child_focussed = 0;
804 break;
805 case 'q':
806 err = view->input(new, dead, focus, view, ch);
807 *dead = view;
808 break;
809 case 'Q':
810 *done = 1;
811 break;
812 case 'f':
813 if (view_is_parent_view(view)) {
814 if (view->child == NULL)
815 break;
816 if (view_is_splitscreen(view->child)) {
817 *focus = view->child;
818 view->child_focussed = 1;
819 err = view_fullscreen(view->child);
820 } else
821 err = view_splitscreen(view->child);
822 if (err)
823 break;
824 err = view->child->input(new, dead, focus,
825 view->child, KEY_RESIZE);
826 } else {
827 if (view_is_splitscreen(view)) {
828 *focus = view;
829 view->parent->child_focussed = 1;
830 err = view_fullscreen(view);
831 } else {
832 err = view_splitscreen(view);
834 if (err)
835 break;
836 err = view->input(new, dead, focus, view,
837 KEY_RESIZE);
839 break;
840 case KEY_RESIZE:
841 break;
842 case '/':
843 if (view->search_start)
844 view_search_start(view);
845 else
846 err = view->input(new, dead, focus, view, ch);
847 break;
848 case 'N':
849 case 'n':
850 if (view->search_next && view->searching) {
851 view->searching = (ch == 'n' ?
852 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
853 view->search_next_done = 0;
854 view->search_next(view);
855 } else
856 err = view->input(new, dead, focus, view, ch);
857 break;
858 default:
859 err = view->input(new, dead, focus, view, ch);
860 break;
863 return err;
866 void
867 view_vborder(struct tog_view *view)
869 PANEL *panel;
870 struct tog_view *view_above;
872 if (view->parent)
873 return view_vborder(view->parent);
875 panel = panel_above(view->panel);
876 if (panel == NULL)
877 return;
879 view_above = panel_userptr(panel);
880 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
881 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
884 int
885 view_needs_focus_indication(struct tog_view *view)
887 if (view_is_parent_view(view)) {
888 if (view->child == NULL || view->child_focussed)
889 return 0;
890 if (!view_is_splitscreen(view->child))
891 return 0;
892 } else if (!view_is_splitscreen(view))
893 return 0;
895 return view->focussed;
898 static const struct got_error *
899 view_loop(struct tog_view *view)
901 const struct got_error *err = NULL;
902 struct tog_view_list_head views;
903 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
904 int fast_refresh = 10;
905 int done = 0, errcode;
907 errcode = pthread_mutex_lock(&tog_mutex);
908 if (errcode)
909 return got_error_set_errno(errcode, "pthread_mutex_lock");
911 TAILQ_INIT(&views);
912 TAILQ_INSERT_HEAD(&views, view, entry);
914 main_view = view;
915 view->focussed = 1;
916 err = view->show(view);
917 if (err)
918 return err;
919 update_panels();
920 doupdate();
921 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
922 /* Refresh fast during initialization, then become slower. */
923 if (fast_refresh && fast_refresh-- == 0)
924 halfdelay(10); /* switch to once per second */
926 err = view_input(&new_view, &dead_view, &focus_view, &done,
927 view, &views);
928 if (err)
929 break;
930 if (dead_view) {
931 struct tog_view *prev = NULL;
933 if (view_is_parent_view(dead_view))
934 prev = TAILQ_PREV(dead_view,
935 tog_view_list_head, entry);
936 else if (view->parent != dead_view)
937 prev = view->parent;
939 if (dead_view->parent)
940 dead_view->parent->child = NULL;
941 else
942 TAILQ_REMOVE(&views, dead_view, entry);
944 err = view_close(dead_view);
945 if (err || (dead_view == main_view && new_view == NULL))
946 goto done;
948 if (view == dead_view) {
949 if (focus_view)
950 view = focus_view;
951 else if (prev)
952 view = prev;
953 else if (!TAILQ_EMPTY(&views))
954 view = TAILQ_LAST(&views,
955 tog_view_list_head);
956 else
957 view = NULL;
958 if (view) {
959 if (view->child && view->child_focussed)
960 focus_view = view->child;
961 else
962 focus_view = view;
966 if (new_view) {
967 struct tog_view *v, *t;
968 /* Only allow one parent view per type. */
969 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
970 if (v->type != new_view->type)
971 continue;
972 TAILQ_REMOVE(&views, v, entry);
973 err = view_close(v);
974 if (err)
975 goto done;
976 break;
978 TAILQ_INSERT_TAIL(&views, new_view, entry);
979 view = new_view;
980 if (focus_view == NULL)
981 focus_view = new_view;
983 if (focus_view) {
984 show_panel(focus_view->panel);
985 if (view)
986 view->focussed = 0;
987 focus_view->focussed = 1;
988 view = focus_view;
989 if (new_view)
990 show_panel(new_view->panel);
991 if (view->child && view_is_splitscreen(view->child))
992 show_panel(view->child->panel);
994 if (view) {
995 if (focus_view == NULL) {
996 view->focussed = 1;
997 show_panel(view->panel);
998 if (view->child && view_is_splitscreen(view->child))
999 show_panel(view->child->panel);
1000 focus_view = view;
1002 if (view->parent) {
1003 err = view->parent->show(view->parent);
1004 if (err)
1005 goto done;
1007 err = view->show(view);
1008 if (err)
1009 goto done;
1010 if (view->child) {
1011 err = view->child->show(view->child);
1012 if (err)
1013 goto done;
1015 update_panels();
1016 doupdate();
1019 done:
1020 while (!TAILQ_EMPTY(&views)) {
1021 view = TAILQ_FIRST(&views);
1022 TAILQ_REMOVE(&views, view, entry);
1023 view_close(view);
1026 errcode = pthread_mutex_unlock(&tog_mutex);
1027 if (errcode && err == NULL)
1028 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1030 return err;
1033 __dead static void
1034 usage_log(void)
1036 endwin();
1037 fprintf(stderr,
1038 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1039 getprogname());
1040 exit(1);
1043 /* Create newly allocated wide-character string equivalent to a byte string. */
1044 static const struct got_error *
1045 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1047 char *vis = NULL;
1048 const struct got_error *err = NULL;
1050 *ws = NULL;
1051 *wlen = mbstowcs(NULL, s, 0);
1052 if (*wlen == (size_t)-1) {
1053 int vislen;
1054 if (errno != EILSEQ)
1055 return got_error_from_errno("mbstowcs");
1057 /* byte string invalid in current encoding; try to "fix" it */
1058 err = got_mbsavis(&vis, &vislen, s);
1059 if (err)
1060 return err;
1061 *wlen = mbstowcs(NULL, vis, 0);
1062 if (*wlen == (size_t)-1) {
1063 err = got_error_from_errno("mbstowcs"); /* give up */
1064 goto done;
1068 *ws = calloc(*wlen + 1, sizeof(**ws));
1069 if (*ws == NULL) {
1070 err = got_error_from_errno("calloc");
1071 goto done;
1074 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1075 err = got_error_from_errno("mbstowcs");
1076 done:
1077 free(vis);
1078 if (err) {
1079 free(*ws);
1080 *ws = NULL;
1081 *wlen = 0;
1083 return err;
1086 /* Format a line for display, ensuring that it won't overflow a width limit. */
1087 static const struct got_error *
1088 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1089 int col_tab_align)
1091 const struct got_error *err = NULL;
1092 int cols = 0;
1093 wchar_t *wline = NULL;
1094 size_t wlen;
1095 int i;
1097 *wlinep = NULL;
1098 *widthp = 0;
1100 err = mbs2ws(&wline, &wlen, line);
1101 if (err)
1102 return err;
1104 i = 0;
1105 while (i < wlen) {
1106 int width = wcwidth(wline[i]);
1108 if (width == 0) {
1109 i++;
1110 continue;
1113 if (width == 1 || width == 2) {
1114 if (cols + width > wlimit)
1115 break;
1116 cols += width;
1117 i++;
1118 } else if (width == -1) {
1119 if (wline[i] == L'\t') {
1120 width = TABSIZE -
1121 ((cols + col_tab_align) % TABSIZE);
1122 if (cols + width > wlimit)
1123 break;
1124 cols += width;
1126 i++;
1127 } else {
1128 err = got_error_from_errno("wcwidth");
1129 goto done;
1132 wline[i] = L'\0';
1133 if (widthp)
1134 *widthp = cols;
1135 done:
1136 if (err)
1137 free(wline);
1138 else
1139 *wlinep = wline;
1140 return err;
1143 static const struct got_error*
1144 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1145 struct got_object_id *id, struct got_repository *repo)
1147 static const struct got_error *err = NULL;
1148 struct got_reflist_entry *re;
1149 char *s;
1150 const char *name;
1152 *refs_str = NULL;
1154 SIMPLEQ_FOREACH(re, refs, entry) {
1155 struct got_tag_object *tag = NULL;
1156 int cmp;
1158 name = got_ref_get_name(re->ref);
1159 if (strcmp(name, GOT_REF_HEAD) == 0)
1160 continue;
1161 if (strncmp(name, "refs/", 5) == 0)
1162 name += 5;
1163 if (strncmp(name, "got/", 4) == 0)
1164 continue;
1165 if (strncmp(name, "heads/", 6) == 0)
1166 name += 6;
1167 if (strncmp(name, "remotes/", 8) == 0)
1168 name += 8;
1169 if (strncmp(name, "tags/", 5) == 0) {
1170 err = got_object_open_as_tag(&tag, repo, re->id);
1171 if (err) {
1172 if (err->code != GOT_ERR_OBJ_TYPE)
1173 break;
1174 /* Ref points at something other than a tag. */
1175 err = NULL;
1176 tag = NULL;
1179 cmp = got_object_id_cmp(tag ?
1180 got_object_tag_get_object_id(tag) : re->id, id);
1181 if (tag)
1182 got_object_tag_close(tag);
1183 if (cmp != 0)
1184 continue;
1185 s = *refs_str;
1186 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1187 s ? ", " : "", name) == -1) {
1188 err = got_error_from_errno("asprintf");
1189 free(s);
1190 *refs_str = NULL;
1191 break;
1193 free(s);
1196 return err;
1199 static const struct got_error *
1200 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1201 int col_tab_align)
1203 char *smallerthan, *at;
1205 smallerthan = strchr(author, '<');
1206 if (smallerthan && smallerthan[1] != '\0')
1207 author = smallerthan + 1;
1208 at = strchr(author, '@');
1209 if (at)
1210 *at = '\0';
1211 return format_line(wauthor, author_width, author, limit, col_tab_align);
1214 static const struct got_error *
1215 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1216 struct got_object_id *id, struct got_reflist_head *refs,
1217 const size_t date_display_cols, int author_display_cols,
1218 struct tog_colors *colors)
1220 const struct got_error *err = NULL;
1221 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1222 char *logmsg0 = NULL, *logmsg = NULL;
1223 char *author = NULL;
1224 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1225 int author_width, logmsg_width;
1226 char *newline, *line = NULL;
1227 int col, limit;
1228 const int avail = view->ncols;
1229 struct tm tm;
1230 time_t committer_time;
1231 struct tog_color *tc;
1233 committer_time = got_object_commit_get_committer_time(commit);
1234 if (localtime_r(&committer_time, &tm) == NULL)
1235 return got_error_from_errno("localtime_r");
1236 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1237 >= sizeof(datebuf))
1238 return got_error(GOT_ERR_NO_SPACE);
1240 if (avail <= date_display_cols)
1241 limit = MIN(sizeof(datebuf) - 1, avail);
1242 else
1243 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1244 tc = get_color(colors, TOG_COLOR_DATE);
1245 if (tc)
1246 wattr_on(view->window,
1247 COLOR_PAIR(tc->colorpair), NULL);
1248 waddnstr(view->window, datebuf, limit);
1249 if (tc)
1250 wattr_off(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1252 col = limit;
1253 if (col > avail)
1254 goto done;
1256 if (avail >= 120) {
1257 char *id_str;
1258 err = got_object_id_str(&id_str, id);
1259 if (err)
1260 goto done;
1261 tc = get_color(colors, TOG_COLOR_COMMIT);
1262 if (tc)
1263 wattr_on(view->window,
1264 COLOR_PAIR(tc->colorpair), NULL);
1265 wprintw(view->window, "%.8s ", id_str);
1266 if (tc)
1267 wattr_off(view->window,
1268 COLOR_PAIR(tc->colorpair), NULL);
1269 free(id_str);
1270 col += 9;
1271 if (col > avail)
1272 goto done;
1275 author = strdup(got_object_commit_get_author(commit));
1276 if (author == NULL) {
1277 err = got_error_from_errno("strdup");
1278 goto done;
1280 err = format_author(&wauthor, &author_width, author, avail - col, col);
1281 if (err)
1282 goto done;
1283 tc = get_color(colors, TOG_COLOR_AUTHOR);
1284 if (tc)
1285 wattr_on(view->window,
1286 COLOR_PAIR(tc->colorpair), NULL);
1287 waddwstr(view->window, wauthor);
1288 if (tc)
1289 wattr_off(view->window,
1290 COLOR_PAIR(tc->colorpair), NULL);
1291 col += author_width;
1292 while (col < avail && author_width < author_display_cols + 2) {
1293 waddch(view->window, ' ');
1294 col++;
1295 author_width++;
1297 if (col > avail)
1298 goto done;
1300 err = got_object_commit_get_logmsg(&logmsg0, commit);
1301 if (err)
1302 goto done;
1303 logmsg = logmsg0;
1304 while (*logmsg == '\n')
1305 logmsg++;
1306 newline = strchr(logmsg, '\n');
1307 if (newline)
1308 *newline = '\0';
1309 limit = avail - col;
1310 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1311 if (err)
1312 goto done;
1313 waddwstr(view->window, wlogmsg);
1314 col += logmsg_width;
1315 while (col < avail) {
1316 waddch(view->window, ' ');
1317 col++;
1319 done:
1320 free(logmsg0);
1321 free(wlogmsg);
1322 free(author);
1323 free(wauthor);
1324 free(line);
1325 return err;
1328 static struct commit_queue_entry *
1329 alloc_commit_queue_entry(struct got_commit_object *commit,
1330 struct got_object_id *id)
1332 struct commit_queue_entry *entry;
1334 entry = calloc(1, sizeof(*entry));
1335 if (entry == NULL)
1336 return NULL;
1338 entry->id = id;
1339 entry->commit = commit;
1340 return entry;
1343 static void
1344 pop_commit(struct commit_queue *commits)
1346 struct commit_queue_entry *entry;
1348 entry = TAILQ_FIRST(&commits->head);
1349 TAILQ_REMOVE(&commits->head, entry, entry);
1350 got_object_commit_close(entry->commit);
1351 commits->ncommits--;
1352 /* Don't free entry->id! It is owned by the commit graph. */
1353 free(entry);
1356 static void
1357 free_commits(struct commit_queue *commits)
1359 while (!TAILQ_EMPTY(&commits->head))
1360 pop_commit(commits);
1363 static const struct got_error *
1364 match_commit(int *have_match, struct got_object_id *id,
1365 struct got_commit_object *commit, regex_t *regex)
1367 const struct got_error *err = NULL;
1368 regmatch_t regmatch;
1369 char *id_str = NULL, *logmsg = NULL;
1371 *have_match = 0;
1373 err = got_object_id_str(&id_str, id);
1374 if (err)
1375 return err;
1377 err = got_object_commit_get_logmsg(&logmsg, commit);
1378 if (err)
1379 goto done;
1381 if (regexec(regex, got_object_commit_get_author(commit), 1,
1382 &regmatch, 0) == 0 ||
1383 regexec(regex, got_object_commit_get_committer(commit), 1,
1384 &regmatch, 0) == 0 ||
1385 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1386 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1387 *have_match = 1;
1388 done:
1389 free(id_str);
1390 free(logmsg);
1391 return err;
1394 static const struct got_error *
1395 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1396 int minqueue, struct got_repository *repo, const char *path,
1397 int *searching, int *search_next_done, regex_t *regex)
1399 const struct got_error *err = NULL;
1400 int nqueued = 0, have_match = 0;
1403 * We keep all commits open throughout the lifetime of the log
1404 * view in order to avoid having to re-fetch commits from disk
1405 * while updating the display.
1407 while (nqueued < minqueue ||
1408 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1409 struct got_object_id *id;
1410 struct got_commit_object *commit;
1411 struct commit_queue_entry *entry;
1412 int errcode;
1414 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1415 if (err || id == NULL)
1416 break;
1418 err = got_object_open_as_commit(&commit, repo, id);
1419 if (err)
1420 break;
1421 entry = alloc_commit_queue_entry(commit, id);
1422 if (entry == NULL) {
1423 err = got_error_from_errno("alloc_commit_queue_entry");
1424 break;
1427 errcode = pthread_mutex_lock(&tog_mutex);
1428 if (errcode) {
1429 err = got_error_set_errno(errcode,
1430 "pthread_mutex_lock");
1431 break;
1434 entry->idx = commits->ncommits;
1435 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1436 nqueued++;
1437 commits->ncommits++;
1439 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1440 err = match_commit(&have_match, id, commit, regex);
1443 errcode = pthread_mutex_unlock(&tog_mutex);
1444 if (errcode && err == NULL)
1445 err = got_error_set_errno(errcode,
1446 "pthread_mutex_unlock");
1448 if (err || have_match)
1449 break;
1452 return err;
1455 static const struct got_error *
1456 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1457 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1458 struct commit_queue *commits, int selected_idx, int limit,
1459 struct got_reflist_head *refs, const char *path, int commits_needed,
1460 struct tog_colors *colors)
1462 const struct got_error *err = NULL;
1463 struct tog_log_view_state *s = &view->state.log;
1464 struct commit_queue_entry *entry;
1465 int width;
1466 int ncommits, author_cols = 4;
1467 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1468 char *refs_str = NULL;
1469 wchar_t *wline;
1470 struct tog_color *tc;
1471 static const size_t date_display_cols = 12;
1473 entry = first;
1474 ncommits = 0;
1475 while (entry) {
1476 if (ncommits == selected_idx) {
1477 *selected = entry;
1478 break;
1480 entry = TAILQ_NEXT(entry, entry);
1481 ncommits++;
1484 if (*selected && !(view->searching && view->search_next_done == 0)) {
1485 err = got_object_id_str(&id_str, (*selected)->id);
1486 if (err)
1487 return err;
1488 if (refs) {
1489 err = build_refs_str(&refs_str, refs, (*selected)->id,
1490 s->repo);
1491 if (err)
1492 goto done;
1496 if (commits_needed == 0)
1497 halfdelay(10); /* disable fast refresh */
1499 if (asprintf(&ncommits_str, " [%d/%d] %s",
1500 entry ? entry->idx + 1 : 0, commits->ncommits,
1501 commits_needed > 0 ?
1502 (view->searching && view->search_next_done == 0
1503 ? "searching..." : "loading... ") :
1504 (refs_str ? refs_str : "")) == -1) {
1505 err = got_error_from_errno("asprintf");
1506 goto done;
1509 if (path && strcmp(path, "/") != 0) {
1510 if (asprintf(&header, "commit %s %s%s",
1511 id_str ? id_str : "........................................",
1512 path, ncommits_str) == -1) {
1513 err = got_error_from_errno("asprintf");
1514 header = NULL;
1515 goto done;
1517 } else if (asprintf(&header, "commit %s%s",
1518 id_str ? id_str : "........................................",
1519 ncommits_str) == -1) {
1520 err = got_error_from_errno("asprintf");
1521 header = NULL;
1522 goto done;
1524 err = format_line(&wline, &width, header, view->ncols, 0);
1525 if (err)
1526 goto done;
1528 werase(view->window);
1530 if (view_needs_focus_indication(view))
1531 wstandout(view->window);
1532 tc = get_color(colors, TOG_COLOR_COMMIT);
1533 if (tc)
1534 wattr_on(view->window,
1535 COLOR_PAIR(tc->colorpair), NULL);
1536 waddwstr(view->window, wline);
1537 if (tc)
1538 wattr_off(view->window,
1539 COLOR_PAIR(tc->colorpair), NULL);
1540 while (width < view->ncols) {
1541 waddch(view->window, ' ');
1542 width++;
1544 if (view_needs_focus_indication(view))
1545 wstandend(view->window);
1546 free(wline);
1547 if (limit <= 1)
1548 goto done;
1550 /* Grow author column size if necessary. */
1551 entry = first;
1552 ncommits = 0;
1553 while (entry) {
1554 char *author;
1555 wchar_t *wauthor;
1556 int width;
1557 if (ncommits >= limit - 1)
1558 break;
1559 author = strdup(got_object_commit_get_author(entry->commit));
1560 if (author == NULL) {
1561 err = got_error_from_errno("strdup");
1562 goto done;
1564 err = format_author(&wauthor, &width, author, COLS,
1565 date_display_cols);
1566 if (author_cols < width)
1567 author_cols = width;
1568 free(wauthor);
1569 free(author);
1570 ncommits++;
1571 entry = TAILQ_NEXT(entry, entry);
1574 entry = first;
1575 *last = first;
1576 ncommits = 0;
1577 while (entry) {
1578 if (ncommits >= limit - 1)
1579 break;
1580 if (ncommits == selected_idx)
1581 wstandout(view->window);
1582 err = draw_commit(view, entry->commit, entry->id, refs,
1583 date_display_cols, author_cols, colors);
1584 if (ncommits == selected_idx)
1585 wstandend(view->window);
1586 if (err)
1587 goto done;
1588 ncommits++;
1589 *last = entry;
1590 entry = TAILQ_NEXT(entry, entry);
1593 view_vborder(view);
1594 done:
1595 free(id_str);
1596 free(refs_str);
1597 free(ncommits_str);
1598 free(header);
1599 return err;
1602 static void
1603 scroll_up(struct tog_view *view,
1604 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1605 struct commit_queue *commits)
1607 struct commit_queue_entry *entry;
1608 int nscrolled = 0;
1610 entry = TAILQ_FIRST(&commits->head);
1611 if (*first_displayed_entry == entry)
1612 return;
1614 entry = *first_displayed_entry;
1615 while (entry && nscrolled < maxscroll) {
1616 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1617 if (entry) {
1618 *first_displayed_entry = entry;
1619 nscrolled++;
1624 static const struct got_error *
1625 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1626 pthread_cond_t *need_commits)
1628 int errcode;
1629 int max_wait = 20;
1631 halfdelay(1); /* fast refresh while loading commits */
1633 while (*commits_needed > 0) {
1634 if (*log_complete)
1635 break;
1637 /* Wake the log thread. */
1638 errcode = pthread_cond_signal(need_commits);
1639 if (errcode)
1640 return got_error_set_errno(errcode,
1641 "pthread_cond_signal");
1642 errcode = pthread_mutex_unlock(&tog_mutex);
1643 if (errcode)
1644 return got_error_set_errno(errcode,
1645 "pthread_mutex_unlock");
1646 pthread_yield();
1647 errcode = pthread_mutex_lock(&tog_mutex);
1648 if (errcode)
1649 return got_error_set_errno(errcode,
1650 "pthread_mutex_lock");
1652 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1654 * Thread is not done yet; lose a key press
1655 * and let the user retry... this way the GUI
1656 * remains interactive while logging deep paths
1657 * with few commits in history.
1659 return NULL;
1663 return NULL;
1666 static const struct got_error *
1667 scroll_down(struct tog_view *view,
1668 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1669 struct commit_queue_entry **last_displayed_entry,
1670 struct commit_queue *commits, int *log_complete, int *commits_needed,
1671 pthread_cond_t *need_commits)
1673 const struct got_error *err = NULL;
1674 struct commit_queue_entry *pentry;
1675 int nscrolled = 0;
1677 if (*last_displayed_entry == NULL)
1678 return NULL;
1680 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1681 if (pentry == NULL && !*log_complete) {
1683 * Ask the log thread for required amount of commits
1684 * plus some amount of pre-fetching.
1686 (*commits_needed) += maxscroll + 20;
1687 err = trigger_log_thread(0, commits_needed, log_complete,
1688 need_commits);
1689 if (err)
1690 return err;
1693 do {
1694 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1695 if (pentry == NULL)
1696 break;
1698 *last_displayed_entry = pentry;
1700 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1701 if (pentry == NULL)
1702 break;
1703 *first_displayed_entry = pentry;
1704 } while (++nscrolled < maxscroll);
1706 return err;
1709 static const struct got_error *
1710 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1711 struct got_commit_object *commit, struct got_object_id *commit_id,
1712 struct tog_view *log_view, struct got_reflist_head *refs,
1713 struct got_repository *repo)
1715 const struct got_error *err;
1716 struct got_object_qid *parent_id;
1717 struct tog_view *diff_view;
1719 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1720 if (diff_view == NULL)
1721 return got_error_from_errno("view_open");
1723 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1724 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1725 commit_id, log_view, refs, repo);
1726 if (err == NULL)
1727 *new_view = diff_view;
1728 return err;
1731 static const struct got_error *
1732 tree_view_visit_subtree(struct got_tree_object *subtree,
1733 struct tog_tree_view_state *s)
1735 struct tog_parent_tree *parent;
1737 parent = calloc(1, sizeof(*parent));
1738 if (parent == NULL)
1739 return got_error_from_errno("calloc");
1741 parent->tree = s->tree;
1742 parent->first_displayed_entry = s->first_displayed_entry;
1743 parent->selected_entry = s->selected_entry;
1744 parent->selected = s->selected;
1745 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1746 s->tree = subtree;
1747 s->selected = 0;
1748 s->first_displayed_entry = NULL;
1749 return NULL;
1752 static const struct got_error *
1753 tree_view_walk_path(struct tog_tree_view_state *s,
1754 struct got_object_id *commit_id,
1755 const char *path, struct got_repository *repo)
1757 const struct got_error *err = NULL;
1758 struct got_tree_object *tree = NULL;
1759 const char *p;
1760 char *slash, *subpath = NULL;
1762 /* Walk the path and open corresponding tree objects. */
1763 p = path;
1764 while (*p) {
1765 struct got_tree_entry *te;
1766 struct got_object_id *tree_id;
1767 char *te_name;
1769 while (p[0] == '/')
1770 p++;
1772 /* Ensure the correct subtree entry is selected. */
1773 slash = strchr(p, '/');
1774 if (slash == NULL)
1775 te_name = strdup(p);
1776 else
1777 te_name = strndup(p, slash - p);
1778 if (te_name == NULL) {
1779 err = got_error_from_errno("strndup");
1780 break;
1782 te = got_object_tree_find_entry(s->tree, te_name);
1783 if (te == NULL) {
1784 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1785 free(te_name);
1786 break;
1788 free(te_name);
1789 s->selected_entry = te;
1790 s->selected = got_tree_entry_get_index(te);
1791 if (s->tree != s->root)
1792 s->selected++; /* skip '..' */
1794 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1795 /* Jump to this file's entry. */
1796 s->first_displayed_entry = s->selected_entry;
1797 s->selected = 0;
1798 break;
1801 slash = strchr(p, '/');
1802 if (slash)
1803 subpath = strndup(path, slash - path);
1804 else
1805 subpath = strdup(path);
1806 if (subpath == NULL) {
1807 err = got_error_from_errno("strdup");
1808 break;
1811 err = got_object_id_by_path(&tree_id, repo, commit_id,
1812 subpath);
1813 if (err)
1814 break;
1816 err = got_object_open_as_tree(&tree, repo, tree_id);
1817 free(tree_id);
1818 if (err)
1819 break;
1821 err = tree_view_visit_subtree(tree, s);
1822 if (err) {
1823 got_object_tree_close(tree);
1824 break;
1826 if (slash == NULL)
1827 break;
1828 free(subpath);
1829 subpath = NULL;
1830 p = slash;
1833 free(subpath);
1834 return err;
1837 static const struct got_error *
1838 browse_commit_tree(struct tog_view **new_view, int begin_x,
1839 struct commit_queue_entry *entry, const char *path,
1840 struct got_reflist_head *refs, struct got_repository *repo)
1842 const struct got_error *err = NULL;
1843 struct got_tree_object *tree;
1844 struct tog_tree_view_state *s;
1845 struct tog_view *tree_view;
1847 err = got_object_open_as_tree(&tree, repo,
1848 got_object_commit_get_tree_id(entry->commit));
1849 if (err)
1850 return err;
1852 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1853 if (tree_view == NULL)
1854 return got_error_from_errno("view_open");
1856 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1857 if (err) {
1858 got_object_tree_close(tree);
1859 return err;
1861 s = &tree_view->state.tree;
1863 *new_view = tree_view;
1865 if (got_path_is_root_dir(path))
1866 return NULL;
1868 return tree_view_walk_path(s, entry->id, path, repo);
1871 static const struct got_error *
1872 block_signals_used_by_main_thread(void)
1874 sigset_t sigset;
1875 int errcode;
1877 if (sigemptyset(&sigset) == -1)
1878 return got_error_from_errno("sigemptyset");
1880 /* tog handles SIGWINCH and SIGCONT */
1881 if (sigaddset(&sigset, SIGWINCH) == -1)
1882 return got_error_from_errno("sigaddset");
1883 if (sigaddset(&sigset, SIGCONT) == -1)
1884 return got_error_from_errno("sigaddset");
1886 /* ncurses handles SIGTSTP */
1887 if (sigaddset(&sigset, SIGTSTP) == -1)
1888 return got_error_from_errno("sigaddset");
1890 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1891 if (errcode)
1892 return got_error_set_errno(errcode, "pthread_sigmask");
1894 return NULL;
1897 static void *
1898 log_thread(void *arg)
1900 const struct got_error *err = NULL;
1901 int errcode = 0;
1902 struct tog_log_thread_args *a = arg;
1903 int done = 0;
1905 err = block_signals_used_by_main_thread();
1906 if (err)
1907 return (void *)err;
1909 while (!done && !err && !tog_sigpipe_received) {
1910 err = queue_commits(a->graph, a->commits, 1, a->repo,
1911 a->in_repo_path, a->searching, a->search_next_done,
1912 a->regex);
1913 if (err) {
1914 if (err->code != GOT_ERR_ITER_COMPLETED)
1915 return (void *)err;
1916 err = NULL;
1917 done = 1;
1918 } else if (a->commits_needed > 0)
1919 a->commits_needed--;
1921 errcode = pthread_mutex_lock(&tog_mutex);
1922 if (errcode) {
1923 err = got_error_set_errno(errcode,
1924 "pthread_mutex_lock");
1925 break;
1926 } else if (*a->quit)
1927 done = 1;
1928 else if (*a->first_displayed_entry == NULL) {
1929 *a->first_displayed_entry =
1930 TAILQ_FIRST(&a->commits->head);
1931 *a->selected_entry = *a->first_displayed_entry;
1934 if (done)
1935 a->commits_needed = 0;
1936 else if (a->commits_needed == 0) {
1937 errcode = pthread_cond_wait(&a->need_commits,
1938 &tog_mutex);
1939 if (errcode)
1940 err = got_error_set_errno(errcode,
1941 "pthread_cond_wait");
1944 errcode = pthread_mutex_unlock(&tog_mutex);
1945 if (errcode && err == NULL)
1946 err = got_error_set_errno(errcode,
1947 "pthread_mutex_unlock");
1949 a->log_complete = 1;
1950 return (void *)err;
1953 static const struct got_error *
1954 stop_log_thread(struct tog_log_view_state *s)
1956 const struct got_error *err = NULL;
1957 int errcode;
1959 if (s->thread) {
1960 s->quit = 1;
1961 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1962 if (errcode)
1963 return got_error_set_errno(errcode,
1964 "pthread_cond_signal");
1965 errcode = pthread_mutex_unlock(&tog_mutex);
1966 if (errcode)
1967 return got_error_set_errno(errcode,
1968 "pthread_mutex_unlock");
1969 errcode = pthread_join(s->thread, (void **)&err);
1970 if (errcode)
1971 return got_error_set_errno(errcode, "pthread_join");
1972 errcode = pthread_mutex_lock(&tog_mutex);
1973 if (errcode)
1974 return got_error_set_errno(errcode,
1975 "pthread_mutex_lock");
1976 s->thread = NULL;
1979 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1980 if (errcode && err == NULL)
1981 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1983 if (s->thread_args.repo) {
1984 got_repo_close(s->thread_args.repo);
1985 s->thread_args.repo = NULL;
1988 if (s->thread_args.graph) {
1989 got_commit_graph_close(s->thread_args.graph);
1990 s->thread_args.graph = NULL;
1993 return err;
1996 static const struct got_error *
1997 close_log_view(struct tog_view *view)
1999 const struct got_error *err = NULL;
2000 struct tog_log_view_state *s = &view->state.log;
2002 err = stop_log_thread(s);
2003 free_commits(&s->commits);
2004 free(s->in_repo_path);
2005 s->in_repo_path = NULL;
2006 free(s->start_id);
2007 s->start_id = NULL;
2008 return err;
2011 static const struct got_error *
2012 search_start_log_view(struct tog_view *view)
2014 struct tog_log_view_state *s = &view->state.log;
2016 s->matched_entry = NULL;
2017 s->search_entry = NULL;
2018 return NULL;
2021 static const struct got_error *
2022 search_next_log_view(struct tog_view *view)
2024 const struct got_error *err = NULL;
2025 struct tog_log_view_state *s = &view->state.log;
2026 struct commit_queue_entry *entry;
2028 if (!view->searching) {
2029 view->search_next_done = 1;
2030 return NULL;
2033 if (s->search_entry) {
2034 int errcode, ch;
2035 errcode = pthread_mutex_unlock(&tog_mutex);
2036 if (errcode)
2037 return got_error_set_errno(errcode,
2038 "pthread_mutex_unlock");
2039 ch = wgetch(view->window);
2040 errcode = pthread_mutex_lock(&tog_mutex);
2041 if (errcode)
2042 return got_error_set_errno(errcode,
2043 "pthread_mutex_lock");
2044 if (ch == KEY_BACKSPACE) {
2045 view->search_next_done = 1;
2046 return NULL;
2048 if (view->searching == TOG_SEARCH_FORWARD)
2049 entry = TAILQ_NEXT(s->search_entry, entry);
2050 else
2051 entry = TAILQ_PREV(s->search_entry,
2052 commit_queue_head, entry);
2053 } else if (s->matched_entry) {
2054 if (view->searching == TOG_SEARCH_FORWARD)
2055 entry = TAILQ_NEXT(s->selected_entry, entry);
2056 else
2057 entry = TAILQ_PREV(s->selected_entry,
2058 commit_queue_head, entry);
2059 } else {
2060 if (view->searching == TOG_SEARCH_FORWARD)
2061 entry = TAILQ_FIRST(&s->commits.head);
2062 else
2063 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2066 while (1) {
2067 int have_match = 0;
2069 if (entry == NULL) {
2070 if (s->thread_args.log_complete ||
2071 view->searching == TOG_SEARCH_BACKWARD) {
2072 view->search_next_done = 1;
2073 return NULL;
2076 * Poke the log thread for more commits and return,
2077 * allowing the main loop to make progress. Search
2078 * will resume at s->search_entry once we come back.
2080 s->thread_args.commits_needed++;
2081 return trigger_log_thread(1,
2082 &s->thread_args.commits_needed,
2083 &s->thread_args.log_complete,
2084 &s->thread_args.need_commits);
2087 err = match_commit(&have_match, entry->id, entry->commit,
2088 &view->regex);
2089 if (err)
2090 break;
2091 if (have_match) {
2092 view->search_next_done = 1;
2093 s->matched_entry = entry;
2094 break;
2097 s->search_entry = entry;
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(entry, entry);
2100 else
2101 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2104 if (s->matched_entry) {
2105 int cur = s->selected_entry->idx;
2106 while (cur < s->matched_entry->idx) {
2107 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2108 if (err)
2109 return err;
2110 cur++;
2112 while (cur > s->matched_entry->idx) {
2113 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2114 if (err)
2115 return err;
2116 cur--;
2120 s->search_entry = NULL;
2122 return NULL;
2125 static const struct got_error *
2126 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2127 struct got_reflist_head *refs, struct got_repository *repo,
2128 const char *head_ref_name, const char *in_repo_path,
2129 int log_branches)
2131 const struct got_error *err = NULL;
2132 struct tog_log_view_state *s = &view->state.log;
2133 struct got_repository *thread_repo = NULL;
2134 struct got_commit_graph *thread_graph = NULL;
2135 int errcode;
2137 if (in_repo_path != s->in_repo_path) {
2138 free(s->in_repo_path);
2139 s->in_repo_path = strdup(in_repo_path);
2140 if (s->in_repo_path == NULL)
2141 return got_error_from_errno("strdup");
2144 /* The commit queue only contains commits being displayed. */
2145 TAILQ_INIT(&s->commits.head);
2146 s->commits.ncommits = 0;
2148 s->refs = refs;
2149 s->repo = repo;
2150 s->head_ref_name = head_ref_name;
2151 s->start_id = got_object_id_dup(start_id);
2152 if (s->start_id == NULL) {
2153 err = got_error_from_errno("got_object_id_dup");
2154 goto done;
2156 s->log_branches = log_branches;
2158 SIMPLEQ_INIT(&s->colors);
2159 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2160 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2161 get_color_value("TOG_COLOR_COMMIT"));
2162 if (err)
2163 goto done;
2164 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2165 get_color_value("TOG_COLOR_AUTHOR"));
2166 if (err) {
2167 free_colors(&s->colors);
2168 goto done;
2170 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2171 get_color_value("TOG_COLOR_DATE"));
2172 if (err) {
2173 free_colors(&s->colors);
2174 goto done;
2178 view->show = show_log_view;
2179 view->input = input_log_view;
2180 view->close = close_log_view;
2181 view->search_start = search_start_log_view;
2182 view->search_next = search_next_log_view;
2184 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2185 if (err)
2186 goto done;
2187 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2188 !s->log_branches);
2189 if (err)
2190 goto done;
2191 err = got_commit_graph_iter_start(thread_graph,
2192 s->start_id, s->repo, NULL, NULL);
2193 if (err)
2194 goto done;
2196 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2197 if (errcode) {
2198 err = got_error_set_errno(errcode, "pthread_cond_init");
2199 goto done;
2202 s->thread_args.commits_needed = view->nlines;
2203 s->thread_args.graph = thread_graph;
2204 s->thread_args.commits = &s->commits;
2205 s->thread_args.in_repo_path = s->in_repo_path;
2206 s->thread_args.start_id = s->start_id;
2207 s->thread_args.repo = thread_repo;
2208 s->thread_args.log_complete = 0;
2209 s->thread_args.quit = &s->quit;
2210 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2211 s->thread_args.selected_entry = &s->selected_entry;
2212 s->thread_args.searching = &view->searching;
2213 s->thread_args.search_next_done = &view->search_next_done;
2214 s->thread_args.regex = &view->regex;
2215 done:
2216 if (err)
2217 close_log_view(view);
2218 return err;
2221 static const struct got_error *
2222 show_log_view(struct tog_view *view)
2224 struct tog_log_view_state *s = &view->state.log;
2226 if (s->thread == NULL) {
2227 int errcode = pthread_create(&s->thread, NULL, log_thread,
2228 &s->thread_args);
2229 if (errcode)
2230 return got_error_set_errno(errcode, "pthread_create");
2233 return draw_commits(view, &s->last_displayed_entry,
2234 &s->selected_entry, s->first_displayed_entry,
2235 &s->commits, s->selected, view->nlines, s->refs,
2236 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2239 static const struct got_error *
2240 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2241 struct tog_view **focus_view, struct tog_view *view, int ch)
2243 const struct got_error *err = NULL;
2244 struct tog_log_view_state *s = &view->state.log;
2245 char *parent_path, *in_repo_path = NULL;
2246 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2247 int begin_x = 0;
2248 struct got_object_id *start_id;
2250 switch (ch) {
2251 case 'q':
2252 s->quit = 1;
2253 break;
2254 case 'k':
2255 case KEY_UP:
2256 case '<':
2257 case ',':
2258 if (s->first_displayed_entry == NULL)
2259 break;
2260 if (s->selected > 0)
2261 s->selected--;
2262 else
2263 scroll_up(view, &s->first_displayed_entry, 1,
2264 &s->commits);
2265 break;
2266 case KEY_PPAGE:
2267 case CTRL('b'):
2268 if (s->first_displayed_entry == NULL)
2269 break;
2270 if (TAILQ_FIRST(&s->commits.head) ==
2271 s->first_displayed_entry) {
2272 s->selected = 0;
2273 break;
2275 scroll_up(view, &s->first_displayed_entry,
2276 view->nlines, &s->commits);
2277 break;
2278 case 'j':
2279 case KEY_DOWN:
2280 case '>':
2281 case '.':
2282 if (s->first_displayed_entry == NULL)
2283 break;
2284 if (s->selected < MIN(view->nlines - 2,
2285 s->commits.ncommits - 1)) {
2286 s->selected++;
2287 break;
2289 err = scroll_down(view, &s->first_displayed_entry, 1,
2290 &s->last_displayed_entry, &s->commits,
2291 &s->thread_args.log_complete,
2292 &s->thread_args.commits_needed,
2293 &s->thread_args.need_commits);
2294 break;
2295 case KEY_NPAGE:
2296 case CTRL('f'): {
2297 struct commit_queue_entry *first;
2298 first = s->first_displayed_entry;
2299 if (first == NULL)
2300 break;
2301 err = scroll_down(view, &s->first_displayed_entry,
2302 view->nlines, &s->last_displayed_entry,
2303 &s->commits, &s->thread_args.log_complete,
2304 &s->thread_args.commits_needed,
2305 &s->thread_args.need_commits);
2306 if (err)
2307 break;
2308 if (first == s->first_displayed_entry &&
2309 s->selected < MIN(view->nlines - 2,
2310 s->commits.ncommits - 1)) {
2311 /* can't scroll further down */
2312 s->selected = MIN(view->nlines - 2,
2313 s->commits.ncommits - 1);
2315 err = NULL;
2316 break;
2318 case KEY_RESIZE:
2319 if (s->selected > view->nlines - 2)
2320 s->selected = view->nlines - 2;
2321 if (s->selected > s->commits.ncommits - 1)
2322 s->selected = s->commits.ncommits - 1;
2323 break;
2324 case KEY_ENTER:
2325 case ' ':
2326 case '\r':
2327 if (s->selected_entry == NULL)
2328 break;
2329 if (view_is_parent_view(view))
2330 begin_x = view_split_begin_x(view->begin_x);
2331 err = open_diff_view_for_commit(&diff_view, begin_x,
2332 s->selected_entry->commit, s->selected_entry->id,
2333 view, s->refs, s->repo);
2334 if (err)
2335 break;
2336 if (view_is_parent_view(view)) {
2337 err = view_close_child(view);
2338 if (err)
2339 return err;
2340 err = view_set_child(view, diff_view);
2341 if (err) {
2342 view_close(diff_view);
2343 break;
2345 *focus_view = diff_view;
2346 view->child_focussed = 1;
2347 } else
2348 *new_view = diff_view;
2349 break;
2350 case 't':
2351 if (s->selected_entry == NULL)
2352 break;
2353 if (view_is_parent_view(view))
2354 begin_x = view_split_begin_x(view->begin_x);
2355 err = browse_commit_tree(&tree_view, begin_x,
2356 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2357 if (err)
2358 break;
2359 if (view_is_parent_view(view)) {
2360 err = view_close_child(view);
2361 if (err)
2362 return err;
2363 err = view_set_child(view, tree_view);
2364 if (err) {
2365 view_close(tree_view);
2366 break;
2368 *focus_view = tree_view;
2369 view->child_focussed = 1;
2370 } else
2371 *new_view = tree_view;
2372 break;
2373 case KEY_BACKSPACE:
2374 if (strcmp(s->in_repo_path, "/") == 0)
2375 break;
2376 parent_path = dirname(s->in_repo_path);
2377 if (parent_path && strcmp(parent_path, ".") != 0) {
2378 err = stop_log_thread(s);
2379 if (err)
2380 return err;
2381 lv = view_open(view->nlines, view->ncols,
2382 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2383 if (lv == NULL)
2384 return got_error_from_errno(
2385 "view_open");
2386 err = open_log_view(lv, s->start_id, s->refs,
2387 s->repo, s->head_ref_name, parent_path,
2388 s->log_branches);
2389 if (err)
2390 return err;;
2391 if (view_is_parent_view(view))
2392 *new_view = lv;
2393 else {
2394 view_set_child(view->parent, lv);
2395 *focus_view = lv;
2397 return NULL;
2399 break;
2400 case CTRL('l'):
2401 err = stop_log_thread(s);
2402 if (err)
2403 return err;
2404 lv = view_open(view->nlines, view->ncols,
2405 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2406 if (lv == NULL)
2407 return got_error_from_errno("view_open");
2408 err = got_repo_match_object_id(&start_id, NULL,
2409 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2410 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2411 if (err) {
2412 view_close(lv);
2413 return err;
2415 in_repo_path = strdup(s->in_repo_path);
2416 if (in_repo_path == NULL) {
2417 free(start_id);
2418 view_close(lv);
2419 return got_error_from_errno("strdup");
2421 got_ref_list_free(s->refs);
2422 err = got_ref_list(s->refs, s->repo, NULL,
2423 got_ref_cmp_by_name, NULL);
2424 if (err) {
2425 free(start_id);
2426 view_close(lv);
2427 return err;
2429 err = open_log_view(lv, start_id, s->refs, s->repo,
2430 s->head_ref_name, in_repo_path, s->log_branches);
2431 if (err) {
2432 free(start_id);
2433 view_close(lv);
2434 return err;;
2436 *dead_view = view;
2437 *new_view = lv;
2438 break;
2439 case 'B':
2440 s->log_branches = !s->log_branches;
2441 err = stop_log_thread(s);
2442 if (err)
2443 return err;
2444 lv = view_open(view->nlines, view->ncols,
2445 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2446 if (lv == NULL)
2447 return got_error_from_errno("view_open");
2448 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2449 s->head_ref_name, s->in_repo_path, s->log_branches);
2450 if (err) {
2451 view_close(lv);
2452 return err;;
2454 *dead_view = view;
2455 *new_view = lv;
2456 break;
2457 default:
2458 break;
2461 return err;
2464 static const struct got_error *
2465 apply_unveil(const char *repo_path, const char *worktree_path)
2467 const struct got_error *error;
2469 #ifdef PROFILE
2470 if (unveil("gmon.out", "rwc") != 0)
2471 return got_error_from_errno2("unveil", "gmon.out");
2472 #endif
2473 if (repo_path && unveil(repo_path, "r") != 0)
2474 return got_error_from_errno2("unveil", repo_path);
2476 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2477 return got_error_from_errno2("unveil", worktree_path);
2479 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2480 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2482 error = got_privsep_unveil_exec_helpers();
2483 if (error != NULL)
2484 return error;
2486 if (unveil(NULL, NULL) != 0)
2487 return got_error_from_errno("unveil");
2489 return NULL;
2492 static void
2493 init_curses(void)
2495 initscr();
2496 cbreak();
2497 halfdelay(1); /* Do fast refresh while initial view is loading. */
2498 noecho();
2499 nonl();
2500 intrflush(stdscr, FALSE);
2501 keypad(stdscr, TRUE);
2502 curs_set(0);
2503 if (getenv("TOG_COLORS") != NULL) {
2504 start_color();
2505 use_default_colors();
2507 signal(SIGWINCH, tog_sigwinch);
2508 signal(SIGPIPE, tog_sigpipe);
2509 signal(SIGCONT, tog_sigcont);
2512 static const struct got_error *
2513 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2514 struct got_repository *repo, struct got_worktree *worktree)
2516 const struct got_error *err = NULL;
2518 if (argc == 0) {
2519 *in_repo_path = strdup("/");
2520 if (*in_repo_path == NULL)
2521 return got_error_from_errno("strdup");
2522 return NULL;
2525 if (worktree) {
2526 const char *prefix = got_worktree_get_path_prefix(worktree);
2527 char *wt_path, *p;
2529 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2530 if (err)
2531 return err;
2533 if (asprintf(&p, "%s%s%s", prefix,
2534 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2535 err = got_error_from_errno("asprintf");
2536 free(wt_path);
2537 return err;
2539 err = got_repo_map_path(in_repo_path, repo, p, 0);
2540 free(p);
2541 free(wt_path);
2542 } else
2543 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2545 return err;
2548 static const struct got_error *
2549 cmd_log(int argc, char *argv[])
2551 const struct got_error *error;
2552 struct got_repository *repo = NULL;
2553 struct got_worktree *worktree = NULL;
2554 struct got_reflist_head refs;
2555 struct got_object_id *start_id = NULL;
2556 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2557 char *start_commit = NULL, *head_ref_name = NULL;
2558 int ch, log_branches = 0;
2559 struct tog_view *view;
2561 SIMPLEQ_INIT(&refs);
2563 #ifndef PROFILE
2564 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2565 NULL) == -1)
2566 err(1, "pledge");
2567 #endif
2569 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2570 switch (ch) {
2571 case 'b':
2572 log_branches = 1;
2573 break;
2574 case 'c':
2575 start_commit = optarg;
2576 break;
2577 case 'r':
2578 repo_path = realpath(optarg, NULL);
2579 if (repo_path == NULL)
2580 return got_error_from_errno2("realpath",
2581 optarg);
2582 break;
2583 default:
2584 usage_log();
2585 /* NOTREACHED */
2589 argc -= optind;
2590 argv += optind;
2592 if (argc > 1)
2593 usage_log();
2595 cwd = getcwd(NULL, 0);
2596 if (cwd == NULL)
2597 return got_error_from_errno("getcwd");
2599 error = got_worktree_open(&worktree, cwd);
2600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2601 goto done;
2603 if (repo_path == NULL) {
2604 if (worktree)
2605 repo_path =
2606 strdup(got_worktree_get_repo_path(worktree));
2607 else
2608 repo_path = strdup(cwd);
2610 if (repo_path == NULL) {
2611 error = got_error_from_errno("strdup");
2612 goto done;
2615 error = got_repo_open(&repo, repo_path, NULL);
2616 if (error != NULL)
2617 goto done;
2619 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2620 repo, worktree);
2621 if (error)
2622 goto done;
2624 init_curses();
2626 error = apply_unveil(got_repo_get_path(repo),
2627 worktree ? got_worktree_get_root_path(worktree) : NULL);
2628 if (error)
2629 goto done;
2631 if (start_commit == NULL)
2632 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2633 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2634 GOT_OBJ_TYPE_COMMIT, 1, repo);
2635 else
2636 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2637 GOT_OBJ_TYPE_COMMIT, 1, repo);
2638 if (error != NULL)
2639 goto done;
2641 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2642 if (error)
2643 goto done;
2645 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2646 if (view == NULL) {
2647 error = got_error_from_errno("view_open");
2648 goto done;
2650 if (worktree) {
2651 head_ref_name = strdup(
2652 got_worktree_get_head_ref_name(worktree));
2653 if (head_ref_name == NULL) {
2654 error = got_error_from_errno("strdup");
2655 goto done;
2658 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2659 in_repo_path, log_branches);
2660 if (error)
2661 goto done;
2662 if (worktree) {
2663 /* Release work tree lock. */
2664 got_worktree_close(worktree);
2665 worktree = NULL;
2667 error = view_loop(view);
2668 done:
2669 free(in_repo_path);
2670 free(repo_path);
2671 free(cwd);
2672 free(start_id);
2673 free(head_ref_name);
2674 if (repo)
2675 got_repo_close(repo);
2676 if (worktree)
2677 got_worktree_close(worktree);
2678 got_ref_list_free(&refs);
2679 return error;
2682 __dead static void
2683 usage_diff(void)
2685 endwin();
2686 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2687 getprogname());
2688 exit(1);
2691 static char *
2692 parse_next_line(FILE *f, size_t *len)
2694 char *line;
2695 size_t linelen;
2696 size_t lineno;
2697 const char delim[3] = { '\0', '\0', '\0'};
2699 line = fparseln(f, &linelen, &lineno, delim, 0);
2700 if (len)
2701 *len = linelen;
2702 return line;
2705 static int
2706 match_line(const char *line, regex_t *regex)
2708 regmatch_t regmatch;
2710 return regexec(regex, line, 1, &regmatch, 0) == 0;
2713 struct tog_color *
2714 match_color(struct tog_colors *colors, const char *line)
2716 struct tog_color *tc = NULL;
2718 SIMPLEQ_FOREACH(tc, colors, entry) {
2719 if (match_line(line, &tc->regex))
2720 return tc;
2723 return NULL;
2726 static const struct got_error *
2727 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2728 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2729 char *header, struct tog_colors *colors)
2731 const struct got_error *err;
2732 int lineno = 0, nprinted = 0;
2733 char *line;
2734 struct tog_color *tc;
2735 size_t len;
2736 wchar_t *wline;
2737 int width;
2739 rewind(f);
2740 werase(view->window);
2742 if (header) {
2743 err = format_line(&wline, &width, header, view->ncols, 0);
2744 if (err) {
2745 return err;
2748 if (view_needs_focus_indication(view))
2749 wstandout(view->window);
2750 waddwstr(view->window, wline);
2751 if (view_needs_focus_indication(view))
2752 wstandend(view->window);
2753 if (width <= view->ncols - 1)
2754 waddch(view->window, '\n');
2756 if (max_lines <= 1)
2757 return NULL;
2758 max_lines--;
2761 *eof = 0;
2762 while (nprinted < max_lines) {
2763 line = parse_next_line(f, &len);
2764 if (line == NULL) {
2765 *eof = 1;
2766 break;
2768 if (++lineno < *first_displayed_line) {
2769 free(line);
2770 continue;
2773 err = format_line(&wline, &width, line, view->ncols, 0);
2774 if (err) {
2775 free(line);
2776 return err;
2779 tc = match_color(colors, line);
2780 if (tc)
2781 wattr_on(view->window,
2782 COLOR_PAIR(tc->colorpair), NULL);
2783 waddwstr(view->window, wline);
2784 if (tc)
2785 wattr_off(view->window,
2786 COLOR_PAIR(tc->colorpair), NULL);
2787 if (width <= view->ncols - 1)
2788 waddch(view->window, '\n');
2789 if (++nprinted == 1)
2790 *first_displayed_line = lineno;
2791 free(line);
2792 free(wline);
2793 wline = NULL;
2795 *last_displayed_line = lineno;
2797 view_vborder(view);
2799 if (*eof) {
2800 while (nprinted < view->nlines) {
2801 waddch(view->window, '\n');
2802 nprinted++;
2805 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2806 if (err) {
2807 return err;
2810 wstandout(view->window);
2811 waddwstr(view->window, wline);
2812 wstandend(view->window);
2815 return NULL;
2818 static char *
2819 get_datestr(time_t *time, char *datebuf)
2821 struct tm mytm, *tm;
2822 char *p, *s;
2824 tm = gmtime_r(time, &mytm);
2825 if (tm == NULL)
2826 return NULL;
2827 s = asctime_r(tm, datebuf);
2828 if (s == NULL)
2829 return NULL;
2830 p = strchr(s, '\n');
2831 if (p)
2832 *p = '\0';
2833 return s;
2836 static const struct got_error *
2837 write_commit_info(struct got_object_id *commit_id,
2838 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2840 const struct got_error *err = NULL;
2841 char datebuf[26], *datestr;
2842 struct got_commit_object *commit;
2843 char *id_str = NULL, *logmsg = NULL;
2844 time_t committer_time;
2845 const char *author, *committer;
2846 char *refs_str = NULL;
2848 if (refs) {
2849 err = build_refs_str(&refs_str, refs, commit_id, repo);
2850 if (err)
2851 return err;
2854 err = got_object_open_as_commit(&commit, repo, commit_id);
2855 if (err)
2856 return err;
2858 err = got_object_id_str(&id_str, commit_id);
2859 if (err) {
2860 err = got_error_from_errno("got_object_id_str");
2861 goto done;
2864 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2865 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2866 err = got_error_from_errno("fprintf");
2867 goto done;
2869 if (fprintf(outfile, "from: %s\n",
2870 got_object_commit_get_author(commit)) < 0) {
2871 err = got_error_from_errno("fprintf");
2872 goto done;
2874 committer_time = got_object_commit_get_committer_time(commit);
2875 datestr = get_datestr(&committer_time, datebuf);
2876 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2877 err = got_error_from_errno("fprintf");
2878 goto done;
2880 author = got_object_commit_get_author(commit);
2881 committer = got_object_commit_get_committer(commit);
2882 if (strcmp(author, committer) != 0 &&
2883 fprintf(outfile, "via: %s\n", committer) < 0) {
2884 err = got_error_from_errno("fprintf");
2885 goto done;
2887 err = got_object_commit_get_logmsg(&logmsg, commit);
2888 if (err)
2889 goto done;
2890 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2891 err = got_error_from_errno("fprintf");
2892 goto done;
2894 done:
2895 free(id_str);
2896 free(logmsg);
2897 free(refs_str);
2898 got_object_commit_close(commit);
2899 return err;
2902 const struct got_error *
2903 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2904 FILE *infile)
2906 size_t len;
2907 char *buf = NULL;
2908 int i;
2909 size_t noffsets = 0;
2910 off_t off = 0;
2912 if (line_offsets)
2913 *line_offsets = NULL;
2914 if (filesize)
2915 *filesize = 0;
2916 if (nlines)
2917 *nlines = 0;
2919 if (fseek(infile, 0, SEEK_END) == -1)
2920 return got_error_from_errno("fseek");
2921 len = ftell(infile) + 1;
2922 if (ferror(infile))
2923 return got_error_from_errno("ftell");
2924 if (fseek(infile, 0, SEEK_SET) == -1)
2925 return got_error_from_errno("fseek");
2927 if (len == 0)
2928 return NULL;
2929 if ((buf = calloc(len, sizeof(char *))) == NULL)
2930 return got_error_from_errno("calloc");
2932 fread(buf, 1, len, infile);
2933 if (ferror(infile))
2934 return got_error_from_errno("fread");
2936 i = 0;
2937 if (line_offsets && nlines) {
2938 if (*line_offsets == NULL) {
2939 /* Have some data but perhaps no '\n'. */
2940 noffsets = 1;
2941 *nlines = 1;
2942 *line_offsets = calloc(1, sizeof(**line_offsets));
2943 if (*line_offsets == NULL)
2944 return got_error_from_errno("calloc");
2945 /* Skip forward over end of first line. */
2946 while (i < len) {
2947 if (buf[i] == '\n')
2948 break;
2949 i++;
2952 /* Scan '\n' offsets in remaining chunk of data. */
2953 while (i < len) {
2954 if (buf[i] != '\n') {
2955 i++;
2956 continue;
2958 (*nlines)++;
2959 if (noffsets < *nlines) {
2960 off_t *o = recallocarray(*line_offsets,
2961 noffsets, *nlines,
2962 sizeof(**line_offsets));
2963 if (o == NULL) {
2964 free(*line_offsets);
2965 *line_offsets = NULL;
2966 return got_error_from_errno(
2967 "recallocarray");
2969 *line_offsets = o;
2970 noffsets = *nlines;
2972 off = i + 1;
2973 (*line_offsets)[*nlines - 1] = off;
2974 i++;
2978 if (fflush(infile) != 0)
2979 return got_error_from_errno("fflush");
2980 rewind(infile);
2982 if (filesize)
2983 *filesize = len;
2985 return NULL;
2988 static const struct got_error *
2989 create_diff(struct tog_diff_view_state *s)
2991 const struct got_error *err = NULL;
2992 FILE *f = NULL;
2993 int obj_type;
2995 f = got_opentemp();
2996 if (f == NULL) {
2997 err = got_error_from_errno("got_opentemp");
2998 goto done;
3000 if (s->f && fclose(s->f) != 0) {
3001 err = got_error_from_errno("fclose");
3002 goto done;
3004 s->f = f;
3006 if (s->id1)
3007 err = got_object_get_type(&obj_type, s->repo, s->id1);
3008 else
3009 err = got_object_get_type(&obj_type, s->repo, s->id2);
3010 if (err)
3011 goto done;
3013 switch (obj_type) {
3014 case GOT_OBJ_TYPE_BLOB:
3015 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3016 s->diff_context, 0, s->repo, s->f);
3017 break;
3018 case GOT_OBJ_TYPE_TREE:
3019 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3020 s->diff_context, 0, s->repo, s->f);
3021 break;
3022 case GOT_OBJ_TYPE_COMMIT: {
3023 const struct got_object_id_queue *parent_ids;
3024 struct got_object_qid *pid;
3025 struct got_commit_object *commit2;
3027 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3028 if (err)
3029 goto done;
3030 /* Show commit info if we're diffing to a parent/root commit. */
3031 if (s->id1 == NULL) {
3032 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3033 if (err)
3034 goto done;
3035 } else {
3036 parent_ids = got_object_commit_get_parent_ids(commit2);
3037 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3038 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3039 err = write_commit_info(s->id2, s->refs,
3040 s->repo, s->f);
3041 if (err)
3042 goto done;
3043 break;
3047 got_object_commit_close(commit2);
3049 err = got_diff_objects_as_commits(s->id1, s->id2,
3050 s->diff_context, 0, s->repo, s->f);
3051 break;
3053 default:
3054 err = got_error(GOT_ERR_OBJ_TYPE);
3055 break;
3057 if (err)
3058 goto done;
3059 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3060 s->f);
3061 done:
3062 if (s->f && fflush(s->f) != 0 && err == NULL)
3063 err = got_error_from_errno("fflush");
3064 return err;
3067 static void
3068 diff_view_indicate_progress(struct tog_view *view)
3070 mvwaddstr(view->window, 0, 0, "diffing...");
3071 update_panels();
3072 doupdate();
3075 static const struct got_error *
3076 search_start_diff_view(struct tog_view *view)
3078 struct tog_diff_view_state *s = &view->state.diff;
3080 s->matched_line = 0;
3081 return NULL;
3084 static const struct got_error *
3085 search_next_diff_view(struct tog_view *view)
3087 struct tog_diff_view_state *s = &view->state.diff;
3088 int lineno;
3090 if (!view->searching) {
3091 view->search_next_done = 1;
3092 return NULL;
3095 if (s->matched_line) {
3096 if (view->searching == TOG_SEARCH_FORWARD)
3097 lineno = s->matched_line + 1;
3098 else
3099 lineno = s->matched_line - 1;
3100 } else {
3101 if (view->searching == TOG_SEARCH_FORWARD)
3102 lineno = 1;
3103 else
3104 lineno = s->nlines;
3107 while (1) {
3108 char *line = NULL;
3109 off_t offset;
3110 size_t len;
3112 if (lineno <= 0 || lineno > s->nlines) {
3113 if (s->matched_line == 0) {
3114 view->search_next_done = 1;
3115 free(line);
3116 break;
3119 if (view->searching == TOG_SEARCH_FORWARD)
3120 lineno = 1;
3121 else
3122 lineno = s->nlines;
3125 offset = s->line_offsets[lineno - 1];
3126 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3127 free(line);
3128 return got_error_from_errno("fseeko");
3130 free(line);
3131 line = parse_next_line(s->f, &len);
3132 if (line && match_line(line, &view->regex)) {
3133 view->search_next_done = 1;
3134 s->matched_line = lineno;
3135 free(line);
3136 break;
3138 free(line);
3139 if (view->searching == TOG_SEARCH_FORWARD)
3140 lineno++;
3141 else
3142 lineno--;
3145 if (s->matched_line) {
3146 s->first_displayed_line = s->matched_line;
3147 s->selected_line = 1;
3150 return NULL;
3153 static const struct got_error *
3154 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3155 struct got_object_id *id2, struct tog_view *log_view,
3156 struct got_reflist_head *refs, struct got_repository *repo)
3158 const struct got_error *err;
3159 struct tog_diff_view_state *s = &view->state.diff;
3161 if (id1 != NULL && id2 != NULL) {
3162 int type1, type2;
3163 err = got_object_get_type(&type1, repo, id1);
3164 if (err)
3165 return err;
3166 err = got_object_get_type(&type2, repo, id2);
3167 if (err)
3168 return err;
3170 if (type1 != type2)
3171 return got_error(GOT_ERR_OBJ_TYPE);
3173 s->first_displayed_line = 1;
3174 s->last_displayed_line = view->nlines;
3175 s->selected_line = 1;
3176 s->repo = repo;
3177 s->refs = refs;
3178 s->id1 = id1;
3179 s->id2 = id2;
3181 if (id1) {
3182 s->id1 = got_object_id_dup(id1);
3183 if (s->id1 == NULL)
3184 return got_error_from_errno("got_object_id_dup");
3185 } else
3186 s->id1 = NULL;
3188 s->id2 = got_object_id_dup(id2);
3189 if (s->id2 == NULL) {
3190 free(s->id1);
3191 s->id1 = NULL;
3192 return got_error_from_errno("got_object_id_dup");
3194 s->f = NULL;
3195 s->first_displayed_line = 1;
3196 s->last_displayed_line = view->nlines;
3197 s->diff_context = 3;
3198 s->log_view = log_view;
3199 s->repo = repo;
3200 s->refs = refs;
3202 SIMPLEQ_INIT(&s->colors);
3203 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3204 err = add_color(&s->colors,
3205 "^-", TOG_COLOR_DIFF_MINUS,
3206 get_color_value("TOG_COLOR_DIFF_MINUS"));
3207 if (err)
3208 return err;
3209 err = add_color(&s->colors, "^\\+",
3210 TOG_COLOR_DIFF_PLUS,
3211 get_color_value("TOG_COLOR_DIFF_PLUS"));
3212 if (err) {
3213 free_colors(&s->colors);
3214 return err;
3216 err = add_color(&s->colors,
3217 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3218 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3219 if (err) {
3220 free_colors(&s->colors);
3221 return err;
3224 err = add_color(&s->colors,
3225 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3226 get_color_value("TOG_COLOR_DIFF_META"));
3227 if (err) {
3228 free_colors(&s->colors);
3229 return err;
3232 err = add_color(&s->colors,
3233 "^(from|via): ", TOG_COLOR_AUTHOR,
3234 get_color_value("TOG_COLOR_AUTHOR"));
3235 if (err) {
3236 free_colors(&s->colors);
3237 return err;
3240 err = add_color(&s->colors,
3241 "^date: ", TOG_COLOR_DATE,
3242 get_color_value("TOG_COLOR_DATE"));
3243 if (err) {
3244 free_colors(&s->colors);
3245 return err;
3249 if (log_view && view_is_splitscreen(view))
3250 show_log_view(log_view); /* draw vborder */
3251 diff_view_indicate_progress(view);
3253 err = create_diff(s);
3254 if (err) {
3255 free(s->id1);
3256 s->id1 = NULL;
3257 free(s->id2);
3258 s->id2 = NULL;
3259 return err;
3262 view->show = show_diff_view;
3263 view->input = input_diff_view;
3264 view->close = close_diff_view;
3265 view->search_start = search_start_diff_view;
3266 view->search_next = search_next_diff_view;
3268 return NULL;
3271 static const struct got_error *
3272 close_diff_view(struct tog_view *view)
3274 const struct got_error *err = NULL;
3275 struct tog_diff_view_state *s = &view->state.diff;
3277 free(s->id1);
3278 s->id1 = NULL;
3279 free(s->id2);
3280 s->id2 = NULL;
3281 if (s->f && fclose(s->f) == EOF)
3282 err = got_error_from_errno("fclose");
3283 free_colors(&s->colors);
3284 free(s->line_offsets);
3285 return err;
3288 static const struct got_error *
3289 show_diff_view(struct tog_view *view)
3291 const struct got_error *err;
3292 struct tog_diff_view_state *s = &view->state.diff;
3293 char *id_str1 = NULL, *id_str2, *header;
3295 if (s->id1) {
3296 err = got_object_id_str(&id_str1, s->id1);
3297 if (err)
3298 return err;
3300 err = got_object_id_str(&id_str2, s->id2);
3301 if (err)
3302 return err;
3304 if (asprintf(&header, "diff %s %s",
3305 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3306 err = got_error_from_errno("asprintf");
3307 free(id_str1);
3308 free(id_str2);
3309 return err;
3311 free(id_str1);
3312 free(id_str2);
3314 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3315 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3316 header, &s->colors);
3319 static const struct got_error *
3320 set_selected_commit(struct tog_diff_view_state *s,
3321 struct commit_queue_entry *entry)
3323 const struct got_error *err;
3324 const struct got_object_id_queue *parent_ids;
3325 struct got_commit_object *selected_commit;
3326 struct got_object_qid *pid;
3328 free(s->id2);
3329 s->id2 = got_object_id_dup(entry->id);
3330 if (s->id2 == NULL)
3331 return got_error_from_errno("got_object_id_dup");
3333 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3334 if (err)
3335 return err;
3336 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3337 free(s->id1);
3338 pid = SIMPLEQ_FIRST(parent_ids);
3339 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3340 got_object_commit_close(selected_commit);
3341 return NULL;
3344 static const struct got_error *
3345 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3346 struct tog_view **focus_view, struct tog_view *view, int ch)
3348 const struct got_error *err = NULL;
3349 struct tog_diff_view_state *s = &view->state.diff;
3350 struct tog_log_view_state *ls;
3351 struct commit_queue_entry *entry;
3352 int i;
3354 switch (ch) {
3355 case 'k':
3356 case KEY_UP:
3357 if (s->first_displayed_line > 1)
3358 s->first_displayed_line--;
3359 break;
3360 case KEY_PPAGE:
3361 case CTRL('b'):
3362 if (s->first_displayed_line == 1)
3363 break;
3364 i = 0;
3365 while (i++ < view->nlines - 1 &&
3366 s->first_displayed_line > 1)
3367 s->first_displayed_line--;
3368 break;
3369 case 'j':
3370 case KEY_DOWN:
3371 if (!s->eof)
3372 s->first_displayed_line++;
3373 break;
3374 case KEY_NPAGE:
3375 case CTRL('f'):
3376 case ' ':
3377 if (s->eof)
3378 break;
3379 i = 0;
3380 while (!s->eof && i++ < view->nlines - 1) {
3381 char *line;
3382 line = parse_next_line(s->f, NULL);
3383 s->first_displayed_line++;
3384 if (line == NULL)
3385 break;
3387 break;
3388 case '[':
3389 if (s->diff_context > 0) {
3390 s->diff_context--;
3391 diff_view_indicate_progress(view);
3392 err = create_diff(s);
3394 break;
3395 case ']':
3396 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3397 s->diff_context++;
3398 diff_view_indicate_progress(view);
3399 err = create_diff(s);
3401 break;
3402 case '<':
3403 case ',':
3404 if (s->log_view == NULL)
3405 break;
3406 ls = &s->log_view->state.log;
3407 entry = TAILQ_PREV(ls->selected_entry,
3408 commit_queue_head, entry);
3409 if (entry == NULL)
3410 break;
3412 err = input_log_view(NULL, NULL, NULL, s->log_view,
3413 KEY_UP);
3414 if (err)
3415 break;
3417 err = set_selected_commit(s, entry);
3418 if (err)
3419 break;
3421 s->first_displayed_line = 1;
3422 s->last_displayed_line = view->nlines;
3424 diff_view_indicate_progress(view);
3425 err = create_diff(s);
3426 break;
3427 case '>':
3428 case '.':
3429 if (s->log_view == NULL)
3430 break;
3431 ls = &s->log_view->state.log;
3433 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3434 ls->thread_args.commits_needed++;
3436 /* Display "loading..." in log view. */
3437 show_log_view(s->log_view);
3438 update_panels();
3439 doupdate();
3441 err = trigger_log_thread(1 /* load_all */,
3442 &ls->thread_args.commits_needed,
3443 &ls->thread_args.log_complete,
3444 &ls->thread_args.need_commits);
3445 if (err)
3446 break;
3448 err = input_log_view(NULL, NULL, NULL, s->log_view,
3449 KEY_DOWN);
3450 if (err)
3451 break;
3453 entry = TAILQ_NEXT(ls->selected_entry, entry);
3454 if (entry == NULL)
3455 break;
3457 err = set_selected_commit(s, entry);
3458 if (err)
3459 break;
3461 s->first_displayed_line = 1;
3462 s->last_displayed_line = view->nlines;
3464 diff_view_indicate_progress(view);
3465 err = create_diff(s);
3466 break;
3467 default:
3468 break;
3471 return err;
3474 static const struct got_error *
3475 cmd_diff(int argc, char *argv[])
3477 const struct got_error *error = NULL;
3478 struct got_repository *repo = NULL;
3479 struct got_worktree *worktree = NULL;
3480 struct got_reflist_head refs;
3481 struct got_object_id *id1 = NULL, *id2 = NULL;
3482 char *repo_path = NULL, *cwd = NULL;
3483 char *id_str1 = NULL, *id_str2 = NULL;
3484 int ch;
3485 struct tog_view *view;
3487 SIMPLEQ_INIT(&refs);
3489 #ifndef PROFILE
3490 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3491 NULL) == -1)
3492 err(1, "pledge");
3493 #endif
3495 while ((ch = getopt(argc, argv, "r:")) != -1) {
3496 switch (ch) {
3497 case 'r':
3498 repo_path = realpath(optarg, NULL);
3499 if (repo_path == NULL)
3500 return got_error_from_errno2("realpath",
3501 optarg);
3502 break;
3503 default:
3504 usage_diff();
3505 /* NOTREACHED */
3509 argc -= optind;
3510 argv += optind;
3512 if (argc == 0) {
3513 usage_diff(); /* TODO show local worktree changes */
3514 } else if (argc == 2) {
3515 id_str1 = argv[0];
3516 id_str2 = argv[1];
3517 } else
3518 usage_diff();
3520 cwd = getcwd(NULL, 0);
3521 if (cwd == NULL)
3522 return got_error_from_errno("getcwd");
3524 error = got_worktree_open(&worktree, cwd);
3525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3526 goto done;
3528 if (repo_path == NULL) {
3529 if (worktree)
3530 repo_path =
3531 strdup(got_worktree_get_repo_path(worktree));
3532 else
3533 repo_path = strdup(cwd);
3535 if (repo_path == NULL) {
3536 error = got_error_from_errno("strdup");
3537 goto done;
3540 error = got_repo_open(&repo, repo_path, NULL);
3541 if (error)
3542 goto done;
3544 init_curses();
3546 error = apply_unveil(got_repo_get_path(repo), NULL);
3547 if (error)
3548 goto done;
3550 error = got_repo_match_object_id_prefix(&id1, id_str1,
3551 GOT_OBJ_TYPE_ANY, repo);
3552 if (error)
3553 goto done;
3555 error = got_repo_match_object_id_prefix(&id2, id_str2,
3556 GOT_OBJ_TYPE_ANY, repo);
3557 if (error)
3558 goto done;
3560 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3561 if (error)
3562 goto done;
3564 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3565 if (view == NULL) {
3566 error = got_error_from_errno("view_open");
3567 goto done;
3569 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3570 if (error)
3571 goto done;
3572 error = view_loop(view);
3573 done:
3574 free(repo_path);
3575 free(cwd);
3576 if (repo)
3577 got_repo_close(repo);
3578 if (worktree)
3579 got_worktree_close(worktree);
3580 got_ref_list_free(&refs);
3581 return error;
3584 __dead static void
3585 usage_blame(void)
3587 endwin();
3588 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3589 getprogname());
3590 exit(1);
3593 struct tog_blame_line {
3594 int annotated;
3595 struct got_object_id *id;
3598 static const struct got_error *
3599 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3600 const char *path, struct tog_blame_line *lines, int nlines,
3601 int blame_complete, int selected_line, int *first_displayed_line,
3602 int *last_displayed_line, int *eof, int max_lines,
3603 struct tog_colors *colors)
3605 const struct got_error *err;
3606 int lineno = 0, nprinted = 0;
3607 char *line;
3608 size_t len;
3609 wchar_t *wline;
3610 int width;
3611 struct tog_blame_line *blame_line;
3612 struct got_object_id *prev_id = NULL;
3613 char *id_str;
3614 struct tog_color *tc;
3616 err = got_object_id_str(&id_str, id);
3617 if (err)
3618 return err;
3620 rewind(f);
3621 werase(view->window);
3623 if (asprintf(&line, "commit %s", id_str) == -1) {
3624 err = got_error_from_errno("asprintf");
3625 free(id_str);
3626 return err;
3629 err = format_line(&wline, &width, line, view->ncols, 0);
3630 free(line);
3631 line = NULL;
3632 if (err)
3633 return err;
3634 if (view_needs_focus_indication(view))
3635 wstandout(view->window);
3636 tc = get_color(colors, TOG_COLOR_COMMIT);
3637 if (tc)
3638 wattr_on(view->window,
3639 COLOR_PAIR(tc->colorpair), NULL);
3640 waddwstr(view->window, wline);
3641 if (tc)
3642 wattr_off(view->window,
3643 COLOR_PAIR(tc->colorpair), NULL);
3644 if (view_needs_focus_indication(view))
3645 wstandend(view->window);
3646 free(wline);
3647 wline = NULL;
3648 if (width < view->ncols - 1)
3649 waddch(view->window, '\n');
3651 if (asprintf(&line, "[%d/%d] %s%s",
3652 *first_displayed_line - 1 + selected_line, nlines,
3653 blame_complete ? "" : "annotating... ", path) == -1) {
3654 free(id_str);
3655 return got_error_from_errno("asprintf");
3657 free(id_str);
3658 err = format_line(&wline, &width, line, view->ncols, 0);
3659 free(line);
3660 line = NULL;
3661 if (err)
3662 return err;
3663 waddwstr(view->window, wline);
3664 free(wline);
3665 wline = NULL;
3666 if (width < view->ncols - 1)
3667 waddch(view->window, '\n');
3669 *eof = 0;
3670 while (nprinted < max_lines - 2) {
3671 line = parse_next_line(f, &len);
3672 if (line == NULL) {
3673 *eof = 1;
3674 break;
3676 if (++lineno < *first_displayed_line) {
3677 free(line);
3678 continue;
3681 if (view->ncols <= 9) {
3682 width = 9;
3683 wline = wcsdup(L"");
3684 if (wline == NULL)
3685 err = got_error_from_errno("wcsdup");
3686 } else {
3687 err = format_line(&wline, &width, line,
3688 view->ncols - 9, 9);
3689 width += 9;
3691 if (err) {
3692 free(line);
3693 return err;
3696 if (view->focussed && nprinted == selected_line - 1)
3697 wstandout(view->window);
3699 if (nlines > 0) {
3700 blame_line = &lines[lineno - 1];
3701 if (blame_line->annotated && prev_id &&
3702 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3703 !(view->focussed &&
3704 nprinted == selected_line - 1)) {
3705 waddstr(view->window, " ");
3706 } else if (blame_line->annotated) {
3707 char *id_str;
3708 err = got_object_id_str(&id_str, blame_line->id);
3709 if (err) {
3710 free(line);
3711 free(wline);
3712 return err;
3714 tc = get_color(colors, TOG_COLOR_COMMIT);
3715 if (tc)
3716 wattr_on(view->window,
3717 COLOR_PAIR(tc->colorpair), NULL);
3718 wprintw(view->window, "%.8s", id_str);
3719 if (tc)
3720 wattr_off(view->window,
3721 COLOR_PAIR(tc->colorpair), NULL);
3722 free(id_str);
3723 prev_id = blame_line->id;
3724 } else {
3725 waddstr(view->window, "........");
3726 prev_id = NULL;
3728 } else {
3729 waddstr(view->window, "........");
3730 prev_id = NULL;
3733 if (view->focussed && nprinted == selected_line - 1)
3734 wstandend(view->window);
3735 waddstr(view->window, " ");
3737 waddwstr(view->window, wline);
3738 if (width <= view->ncols - 1)
3739 waddch(view->window, '\n');
3740 if (++nprinted == 1)
3741 *first_displayed_line = lineno;
3742 free(line);
3743 free(wline);
3744 wline = NULL;
3746 *last_displayed_line = lineno;
3748 view_vborder(view);
3750 return NULL;
3753 static const struct got_error *
3754 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3756 const struct got_error *err = NULL;
3757 struct tog_blame_cb_args *a = arg;
3758 struct tog_blame_line *line;
3759 int errcode;
3761 if (nlines != a->nlines ||
3762 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3763 return got_error(GOT_ERR_RANGE);
3765 errcode = pthread_mutex_lock(&tog_mutex);
3766 if (errcode)
3767 return got_error_set_errno(errcode, "pthread_mutex_lock");
3769 if (*a->quit) { /* user has quit the blame view */
3770 err = got_error(GOT_ERR_ITER_COMPLETED);
3771 goto done;
3774 if (lineno == -1)
3775 goto done; /* no change in this commit */
3777 line = &a->lines[lineno - 1];
3778 if (line->annotated)
3779 goto done;
3781 line->id = got_object_id_dup(id);
3782 if (line->id == NULL) {
3783 err = got_error_from_errno("got_object_id_dup");
3784 goto done;
3786 line->annotated = 1;
3787 done:
3788 errcode = pthread_mutex_unlock(&tog_mutex);
3789 if (errcode)
3790 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3791 return err;
3794 static void *
3795 blame_thread(void *arg)
3797 const struct got_error *err;
3798 struct tog_blame_thread_args *ta = arg;
3799 struct tog_blame_cb_args *a = ta->cb_args;
3800 int errcode;
3802 err = block_signals_used_by_main_thread();
3803 if (err)
3804 return (void *)err;
3806 err = got_blame(ta->path, a->commit_id, ta->repo,
3807 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3808 if (err && err->code == GOT_ERR_CANCELLED)
3809 err = NULL;
3811 errcode = pthread_mutex_lock(&tog_mutex);
3812 if (errcode)
3813 return (void *)got_error_set_errno(errcode,
3814 "pthread_mutex_lock");
3816 got_repo_close(ta->repo);
3817 ta->repo = NULL;
3818 *ta->complete = 1;
3820 errcode = pthread_mutex_unlock(&tog_mutex);
3821 if (errcode && err == NULL)
3822 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3824 return (void *)err;
3827 static struct got_object_id *
3828 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3829 int first_displayed_line, int selected_line)
3831 struct tog_blame_line *line;
3833 if (nlines <= 0)
3834 return NULL;
3836 line = &lines[first_displayed_line - 1 + selected_line - 1];
3837 if (!line->annotated)
3838 return NULL;
3840 return line->id;
3843 static const struct got_error *
3844 stop_blame(struct tog_blame *blame)
3846 const struct got_error *err = NULL;
3847 int i;
3849 if (blame->thread) {
3850 int errcode;
3851 errcode = pthread_mutex_unlock(&tog_mutex);
3852 if (errcode)
3853 return got_error_set_errno(errcode,
3854 "pthread_mutex_unlock");
3855 errcode = pthread_join(blame->thread, (void **)&err);
3856 if (errcode)
3857 return got_error_set_errno(errcode, "pthread_join");
3858 errcode = pthread_mutex_lock(&tog_mutex);
3859 if (errcode)
3860 return got_error_set_errno(errcode,
3861 "pthread_mutex_lock");
3862 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3863 err = NULL;
3864 blame->thread = NULL;
3866 if (blame->thread_args.repo) {
3867 got_repo_close(blame->thread_args.repo);
3868 blame->thread_args.repo = NULL;
3870 if (blame->f) {
3871 if (fclose(blame->f) != 0 && err == NULL)
3872 err = got_error_from_errno("fclose");
3873 blame->f = NULL;
3875 if (blame->lines) {
3876 for (i = 0; i < blame->nlines; i++)
3877 free(blame->lines[i].id);
3878 free(blame->lines);
3879 blame->lines = NULL;
3881 free(blame->cb_args.commit_id);
3882 blame->cb_args.commit_id = NULL;
3884 return err;
3887 static const struct got_error *
3888 cancel_blame_view(void *arg)
3890 const struct got_error *err = NULL;
3891 int *done = arg;
3892 int errcode;
3894 errcode = pthread_mutex_lock(&tog_mutex);
3895 if (errcode)
3896 return got_error_set_errno(errcode,
3897 "pthread_mutex_unlock");
3899 if (*done)
3900 err = got_error(GOT_ERR_CANCELLED);
3902 errcode = pthread_mutex_unlock(&tog_mutex);
3903 if (errcode)
3904 return got_error_set_errno(errcode,
3905 "pthread_mutex_lock");
3907 return err;
3910 static const struct got_error *
3911 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3912 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3913 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3914 struct got_repository *repo)
3916 const struct got_error *err = NULL;
3917 struct got_blob_object *blob = NULL;
3918 struct got_repository *thread_repo = NULL;
3919 struct got_object_id *obj_id = NULL;
3920 int obj_type;
3922 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3923 if (err)
3924 return err;
3926 err = got_object_get_type(&obj_type, repo, obj_id);
3927 if (err)
3928 goto done;
3930 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3931 err = got_error(GOT_ERR_OBJ_TYPE);
3932 goto done;
3935 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3936 if (err)
3937 goto done;
3938 blame->f = got_opentemp();
3939 if (blame->f == NULL) {
3940 err = got_error_from_errno("got_opentemp");
3941 goto done;
3943 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3944 &blame->line_offsets, blame->f, blob);
3945 if (err || blame->nlines == 0)
3946 goto done;
3948 /* Don't include \n at EOF in the blame line count. */
3949 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3950 blame->nlines--;
3952 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3953 if (blame->lines == NULL) {
3954 err = got_error_from_errno("calloc");
3955 goto done;
3958 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3959 if (err)
3960 goto done;
3962 blame->cb_args.view = view;
3963 blame->cb_args.lines = blame->lines;
3964 blame->cb_args.nlines = blame->nlines;
3965 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3966 if (blame->cb_args.commit_id == NULL) {
3967 err = got_error_from_errno("got_object_id_dup");
3968 goto done;
3970 blame->cb_args.quit = done;
3972 blame->thread_args.path = path;
3973 blame->thread_args.repo = thread_repo;
3974 blame->thread_args.cb_args = &blame->cb_args;
3975 blame->thread_args.complete = blame_complete;
3976 blame->thread_args.cancel_cb = cancel_blame_view;
3977 blame->thread_args.cancel_arg = done;
3978 *blame_complete = 0;
3980 done:
3981 if (blob)
3982 got_object_blob_close(blob);
3983 free(obj_id);
3984 if (err)
3985 stop_blame(blame);
3986 return err;
3989 static const struct got_error *
3990 open_blame_view(struct tog_view *view, char *path,
3991 struct got_object_id *commit_id, struct got_reflist_head *refs,
3992 struct got_repository *repo)
3994 const struct got_error *err = NULL;
3995 struct tog_blame_view_state *s = &view->state.blame;
3997 SIMPLEQ_INIT(&s->blamed_commits);
3999 s->path = strdup(path);
4000 if (s->path == NULL)
4001 return got_error_from_errno("strdup");
4003 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4004 if (err) {
4005 free(s->path);
4006 return err;
4009 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4010 s->first_displayed_line = 1;
4011 s->last_displayed_line = view->nlines;
4012 s->selected_line = 1;
4013 s->blame_complete = 0;
4014 s->repo = repo;
4015 s->refs = refs;
4016 s->commit_id = commit_id;
4017 memset(&s->blame, 0, sizeof(s->blame));
4019 SIMPLEQ_INIT(&s->colors);
4020 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4021 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4022 get_color_value("TOG_COLOR_COMMIT"));
4023 if (err)
4024 return err;
4027 view->show = show_blame_view;
4028 view->input = input_blame_view;
4029 view->close = close_blame_view;
4030 view->search_start = search_start_blame_view;
4031 view->search_next = search_next_blame_view;
4033 return run_blame(&s->blame, view, &s->blame_complete,
4034 &s->first_displayed_line, &s->last_displayed_line,
4035 &s->selected_line, &s->done, &s->eof, s->path,
4036 s->blamed_commit->id, s->repo);
4039 static const struct got_error *
4040 close_blame_view(struct tog_view *view)
4042 const struct got_error *err = NULL;
4043 struct tog_blame_view_state *s = &view->state.blame;
4045 if (s->blame.thread)
4046 err = stop_blame(&s->blame);
4048 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4049 struct got_object_qid *blamed_commit;
4050 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4051 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4052 got_object_qid_free(blamed_commit);
4055 free(s->path);
4056 free_colors(&s->colors);
4058 return err;
4061 static const struct got_error *
4062 search_start_blame_view(struct tog_view *view)
4064 struct tog_blame_view_state *s = &view->state.blame;
4066 s->matched_line = 0;
4067 return NULL;
4070 static const struct got_error *
4071 search_next_blame_view(struct tog_view *view)
4073 struct tog_blame_view_state *s = &view->state.blame;
4074 int lineno;
4076 if (!view->searching) {
4077 view->search_next_done = 1;
4078 return NULL;
4081 if (s->matched_line) {
4082 if (view->searching == TOG_SEARCH_FORWARD)
4083 lineno = s->matched_line + 1;
4084 else
4085 lineno = s->matched_line - 1;
4086 } else {
4087 if (view->searching == TOG_SEARCH_FORWARD)
4088 lineno = 1;
4089 else
4090 lineno = s->blame.nlines;
4093 while (1) {
4094 char *line = NULL;
4095 off_t offset;
4096 size_t len;
4098 if (lineno <= 0 || lineno > s->blame.nlines) {
4099 if (s->matched_line == 0) {
4100 view->search_next_done = 1;
4101 free(line);
4102 break;
4105 if (view->searching == TOG_SEARCH_FORWARD)
4106 lineno = 1;
4107 else
4108 lineno = s->blame.nlines;
4111 offset = s->blame.line_offsets[lineno - 1];
4112 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4113 free(line);
4114 return got_error_from_errno("fseeko");
4116 free(line);
4117 line = parse_next_line(s->blame.f, &len);
4118 if (line && match_line(line, &view->regex)) {
4119 view->search_next_done = 1;
4120 s->matched_line = lineno;
4121 free(line);
4122 break;
4124 free(line);
4125 if (view->searching == TOG_SEARCH_FORWARD)
4126 lineno++;
4127 else
4128 lineno--;
4131 if (s->matched_line) {
4132 s->first_displayed_line = s->matched_line;
4133 s->selected_line = 1;
4136 return NULL;
4139 static const struct got_error *
4140 show_blame_view(struct tog_view *view)
4142 const struct got_error *err = NULL;
4143 struct tog_blame_view_state *s = &view->state.blame;
4144 int errcode;
4146 if (s->blame.thread == NULL) {
4147 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4148 &s->blame.thread_args);
4149 if (errcode)
4150 return got_error_set_errno(errcode, "pthread_create");
4152 halfdelay(1); /* fast refresh while annotating */
4155 if (s->blame_complete)
4156 halfdelay(10); /* disable fast refresh */
4158 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4159 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4160 s->selected_line, &s->first_displayed_line,
4161 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4163 view_vborder(view);
4164 return err;
4167 static const struct got_error *
4168 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4169 struct tog_view **focus_view, struct tog_view *view, int ch)
4171 const struct got_error *err = NULL, *thread_err = NULL;
4172 struct tog_view *diff_view;
4173 struct tog_blame_view_state *s = &view->state.blame;
4174 int begin_x = 0;
4176 switch (ch) {
4177 case 'q':
4178 s->done = 1;
4179 break;
4180 case 'k':
4181 case KEY_UP:
4182 if (s->selected_line > 1)
4183 s->selected_line--;
4184 else if (s->selected_line == 1 &&
4185 s->first_displayed_line > 1)
4186 s->first_displayed_line--;
4187 break;
4188 case KEY_PPAGE:
4189 case CTRL('b'):
4190 if (s->first_displayed_line == 1) {
4191 s->selected_line = 1;
4192 break;
4194 if (s->first_displayed_line > view->nlines - 2)
4195 s->first_displayed_line -=
4196 (view->nlines - 2);
4197 else
4198 s->first_displayed_line = 1;
4199 break;
4200 case 'j':
4201 case KEY_DOWN:
4202 if (s->selected_line < view->nlines - 2 &&
4203 s->first_displayed_line +
4204 s->selected_line <= s->blame.nlines)
4205 s->selected_line++;
4206 else if (s->last_displayed_line <
4207 s->blame.nlines)
4208 s->first_displayed_line++;
4209 break;
4210 case 'b':
4211 case 'p': {
4212 struct got_object_id *id = NULL;
4213 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4214 s->first_displayed_line, s->selected_line);
4215 if (id == NULL)
4216 break;
4217 if (ch == 'p') {
4218 struct got_commit_object *commit;
4219 struct got_object_qid *pid;
4220 struct got_object_id *blob_id = NULL;
4221 int obj_type;
4222 err = got_object_open_as_commit(&commit,
4223 s->repo, id);
4224 if (err)
4225 break;
4226 pid = SIMPLEQ_FIRST(
4227 got_object_commit_get_parent_ids(commit));
4228 if (pid == NULL) {
4229 got_object_commit_close(commit);
4230 break;
4232 /* Check if path history ends here. */
4233 err = got_object_id_by_path(&blob_id, s->repo,
4234 pid->id, s->path);
4235 if (err) {
4236 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4237 err = NULL;
4238 got_object_commit_close(commit);
4239 break;
4241 err = got_object_get_type(&obj_type, s->repo,
4242 blob_id);
4243 free(blob_id);
4244 /* Can't blame non-blob type objects. */
4245 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4246 got_object_commit_close(commit);
4247 break;
4249 err = got_object_qid_alloc(&s->blamed_commit,
4250 pid->id);
4251 got_object_commit_close(commit);
4252 } else {
4253 if (got_object_id_cmp(id,
4254 s->blamed_commit->id) == 0)
4255 break;
4256 err = got_object_qid_alloc(&s->blamed_commit,
4257 id);
4259 if (err)
4260 break;
4261 s->done = 1;
4262 thread_err = stop_blame(&s->blame);
4263 s->done = 0;
4264 if (thread_err)
4265 break;
4266 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4267 s->blamed_commit, entry);
4268 err = run_blame(&s->blame, view, &s->blame_complete,
4269 &s->first_displayed_line, &s->last_displayed_line,
4270 &s->selected_line, &s->done, &s->eof,
4271 s->path, s->blamed_commit->id, s->repo);
4272 if (err)
4273 break;
4274 break;
4276 case 'B': {
4277 struct got_object_qid *first;
4278 first = SIMPLEQ_FIRST(&s->blamed_commits);
4279 if (!got_object_id_cmp(first->id, s->commit_id))
4280 break;
4281 s->done = 1;
4282 thread_err = stop_blame(&s->blame);
4283 s->done = 0;
4284 if (thread_err)
4285 break;
4286 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4287 got_object_qid_free(s->blamed_commit);
4288 s->blamed_commit =
4289 SIMPLEQ_FIRST(&s->blamed_commits);
4290 err = run_blame(&s->blame, view, &s->blame_complete,
4291 &s->first_displayed_line, &s->last_displayed_line,
4292 &s->selected_line, &s->done, &s->eof, s->path,
4293 s->blamed_commit->id, s->repo);
4294 if (err)
4295 break;
4296 break;
4298 case KEY_ENTER:
4299 case '\r': {
4300 struct got_object_id *id = NULL;
4301 struct got_object_qid *pid;
4302 struct got_commit_object *commit = NULL;
4303 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4304 s->first_displayed_line, s->selected_line);
4305 if (id == NULL)
4306 break;
4307 err = got_object_open_as_commit(&commit, s->repo, id);
4308 if (err)
4309 break;
4310 pid = SIMPLEQ_FIRST(
4311 got_object_commit_get_parent_ids(commit));
4312 if (view_is_parent_view(view))
4313 begin_x = view_split_begin_x(view->begin_x);
4314 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4315 if (diff_view == NULL) {
4316 got_object_commit_close(commit);
4317 err = got_error_from_errno("view_open");
4318 break;
4320 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4321 id, NULL, s->refs, s->repo);
4322 got_object_commit_close(commit);
4323 if (err) {
4324 view_close(diff_view);
4325 break;
4327 if (view_is_parent_view(view)) {
4328 err = view_close_child(view);
4329 if (err)
4330 break;
4331 err = view_set_child(view, diff_view);
4332 if (err) {
4333 view_close(diff_view);
4334 break;
4336 *focus_view = diff_view;
4337 view->child_focussed = 1;
4338 } else
4339 *new_view = diff_view;
4340 if (err)
4341 break;
4342 break;
4344 case KEY_NPAGE:
4345 case CTRL('f'):
4346 case ' ':
4347 if (s->last_displayed_line >= s->blame.nlines &&
4348 s->selected_line >= MIN(s->blame.nlines,
4349 view->nlines - 2)) {
4350 break;
4352 if (s->last_displayed_line >= s->blame.nlines &&
4353 s->selected_line < view->nlines - 2) {
4354 s->selected_line = MIN(s->blame.nlines,
4355 view->nlines - 2);
4356 break;
4358 if (s->last_displayed_line + view->nlines - 2
4359 <= s->blame.nlines)
4360 s->first_displayed_line +=
4361 view->nlines - 2;
4362 else
4363 s->first_displayed_line =
4364 s->blame.nlines -
4365 (view->nlines - 3);
4366 break;
4367 case KEY_RESIZE:
4368 if (s->selected_line > view->nlines - 2) {
4369 s->selected_line = MIN(s->blame.nlines,
4370 view->nlines - 2);
4372 break;
4373 default:
4374 break;
4376 return thread_err ? thread_err : err;
4379 static const struct got_error *
4380 cmd_blame(int argc, char *argv[])
4382 const struct got_error *error;
4383 struct got_repository *repo = NULL;
4384 struct got_reflist_head refs;
4385 struct got_worktree *worktree = NULL;
4386 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4387 struct got_object_id *commit_id = NULL;
4388 char *commit_id_str = NULL;
4389 int ch;
4390 struct tog_view *view;
4392 SIMPLEQ_INIT(&refs);
4394 #ifndef PROFILE
4395 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4396 NULL) == -1)
4397 err(1, "pledge");
4398 #endif
4400 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4401 switch (ch) {
4402 case 'c':
4403 commit_id_str = optarg;
4404 break;
4405 case 'r':
4406 repo_path = realpath(optarg, NULL);
4407 if (repo_path == NULL)
4408 return got_error_from_errno2("realpath",
4409 optarg);
4410 break;
4411 default:
4412 usage_blame();
4413 /* NOTREACHED */
4417 argc -= optind;
4418 argv += optind;
4420 if (argc != 1)
4421 usage_blame();
4423 cwd = getcwd(NULL, 0);
4424 if (cwd == NULL)
4425 return got_error_from_errno("getcwd");
4427 error = got_worktree_open(&worktree, cwd);
4428 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4429 goto done;
4431 if (repo_path == NULL) {
4432 if (worktree)
4433 repo_path =
4434 strdup(got_worktree_get_repo_path(worktree));
4435 else
4436 repo_path = strdup(cwd);
4438 if (repo_path == NULL) {
4439 error = got_error_from_errno("strdup");
4440 goto done;
4443 error = got_repo_open(&repo, repo_path, NULL);
4444 if (error != NULL)
4445 goto done;
4447 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4448 worktree);
4449 if (error)
4450 goto done;
4452 init_curses();
4454 error = apply_unveil(got_repo_get_path(repo), NULL);
4455 if (error)
4456 goto done;
4458 if (commit_id_str == NULL) {
4459 struct got_reference *head_ref;
4460 error = got_ref_open(&head_ref, repo, worktree ?
4461 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4462 if (error != NULL)
4463 goto done;
4464 error = got_ref_resolve(&commit_id, repo, head_ref);
4465 got_ref_close(head_ref);
4466 } else {
4467 error = got_repo_match_object_id(&commit_id, NULL,
4468 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4470 if (error != NULL)
4471 goto done;
4473 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4474 if (error)
4475 goto done;
4477 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4478 if (view == NULL) {
4479 error = got_error_from_errno("view_open");
4480 goto done;
4482 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4483 if (error)
4484 goto done;
4485 if (worktree) {
4486 /* Release work tree lock. */
4487 got_worktree_close(worktree);
4488 worktree = NULL;
4490 error = view_loop(view);
4491 done:
4492 free(repo_path);
4493 free(in_repo_path);
4494 free(cwd);
4495 free(commit_id);
4496 if (worktree)
4497 got_worktree_close(worktree);
4498 if (repo)
4499 got_repo_close(repo);
4500 got_ref_list_free(&refs);
4501 return error;
4504 static const struct got_error *
4505 draw_tree_entries(struct tog_view *view,
4506 struct got_tree_entry **first_displayed_entry,
4507 struct got_tree_entry **last_displayed_entry,
4508 struct got_tree_entry **selected_entry, int *ndisplayed,
4509 const char *label, int show_ids, const char *parent_path,
4510 struct got_tree_object *tree, int selected, int limit,
4511 int isroot, struct tog_colors *colors)
4513 const struct got_error *err = NULL;
4514 struct got_tree_entry *te;
4515 wchar_t *wline;
4516 struct tog_color *tc;
4517 int width, n, i, nentries;
4519 *ndisplayed = 0;
4521 werase(view->window);
4523 if (limit == 0)
4524 return NULL;
4526 err = format_line(&wline, &width, label, view->ncols, 0);
4527 if (err)
4528 return err;
4529 if (view_needs_focus_indication(view))
4530 wstandout(view->window);
4531 tc = get_color(colors, TOG_COLOR_COMMIT);
4532 if (tc)
4533 wattr_on(view->window,
4534 COLOR_PAIR(tc->colorpair), NULL);
4535 waddwstr(view->window, wline);
4536 if (tc)
4537 wattr_off(view->window,
4538 COLOR_PAIR(tc->colorpair), NULL);
4539 if (view_needs_focus_indication(view))
4540 wstandend(view->window);
4541 free(wline);
4542 wline = NULL;
4543 if (width < view->ncols - 1)
4544 waddch(view->window, '\n');
4545 if (--limit <= 0)
4546 return NULL;
4547 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4548 if (err)
4549 return err;
4550 waddwstr(view->window, wline);
4551 free(wline);
4552 wline = NULL;
4553 if (width < view->ncols - 1)
4554 waddch(view->window, '\n');
4555 if (--limit <= 0)
4556 return NULL;
4557 waddch(view->window, '\n');
4558 if (--limit <= 0)
4559 return NULL;
4561 if (*first_displayed_entry == NULL) {
4562 te = got_object_tree_get_first_entry(tree);
4563 if (selected == 0) {
4564 if (view->focussed)
4565 wstandout(view->window);
4566 *selected_entry = NULL;
4568 waddstr(view->window, " ..\n"); /* parent directory */
4569 if (selected == 0 && view->focussed)
4570 wstandend(view->window);
4571 (*ndisplayed)++;
4572 if (--limit <= 0)
4573 return NULL;
4574 n = 1;
4575 } else {
4576 n = 0;
4577 te = *first_displayed_entry;
4580 nentries = got_object_tree_get_nentries(tree);
4581 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4582 char *line = NULL, *id_str = NULL;
4583 const char *modestr = "";
4584 mode_t mode;
4586 te = got_object_tree_get_entry(tree, i);
4587 mode = got_tree_entry_get_mode(te);
4589 if (show_ids) {
4590 err = got_object_id_str(&id_str,
4591 got_tree_entry_get_id(te));
4592 if (err)
4593 return got_error_from_errno(
4594 "got_object_id_str");
4596 if (got_object_tree_entry_is_submodule(te))
4597 modestr = "$";
4598 else if (S_ISLNK(mode))
4599 modestr = "@";
4600 else if (S_ISDIR(mode))
4601 modestr = "/";
4602 else if (mode & S_IXUSR)
4603 modestr = "*";
4604 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4605 got_tree_entry_get_name(te), modestr) == -1) {
4606 free(id_str);
4607 return got_error_from_errno("asprintf");
4609 free(id_str);
4610 err = format_line(&wline, &width, line, view->ncols, 0);
4611 if (err) {
4612 free(line);
4613 break;
4615 if (n == selected) {
4616 if (view->focussed)
4617 wstandout(view->window);
4618 *selected_entry = te;
4620 tc = match_color(colors, line);
4621 if (tc)
4622 wattr_on(view->window,
4623 COLOR_PAIR(tc->colorpair), NULL);
4624 waddwstr(view->window, wline);
4625 if (tc)
4626 wattr_off(view->window,
4627 COLOR_PAIR(tc->colorpair), NULL);
4628 if (width < view->ncols - 1)
4629 waddch(view->window, '\n');
4630 if (n == selected && view->focussed)
4631 wstandend(view->window);
4632 free(line);
4633 free(wline);
4634 wline = NULL;
4635 n++;
4636 (*ndisplayed)++;
4637 *last_displayed_entry = te;
4638 if (--limit <= 0)
4639 break;
4642 return err;
4645 static void
4646 tree_scroll_up(struct tog_view *view,
4647 struct got_tree_entry **first_displayed_entry, int maxscroll,
4648 struct got_tree_object *tree, int isroot)
4650 struct got_tree_entry *te;
4651 int i;
4653 if (*first_displayed_entry == NULL)
4654 return;
4656 te = got_object_tree_get_entry(tree, 0);
4657 if (*first_displayed_entry == te) {
4658 if (!isroot)
4659 *first_displayed_entry = NULL;
4660 return;
4663 i = 0;
4664 while (*first_displayed_entry && i < maxscroll) {
4665 *first_displayed_entry = got_tree_entry_get_prev(tree,
4666 *first_displayed_entry);
4667 i++;
4669 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4670 *first_displayed_entry = NULL;
4673 static int
4674 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4675 struct got_tree_entry *last_displayed_entry,
4676 struct got_tree_object *tree)
4678 struct got_tree_entry *next, *last;
4679 int n = 0;
4681 if (*first_displayed_entry)
4682 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4683 else
4684 next = got_object_tree_get_first_entry(tree);
4686 last = last_displayed_entry;
4687 while (next && last && n++ < maxscroll) {
4688 last = got_tree_entry_get_next(tree, last);
4689 if (last) {
4690 *first_displayed_entry = next;
4691 next = got_tree_entry_get_next(tree, next);
4694 return n;
4697 static const struct got_error *
4698 tree_entry_path(char **path, struct tog_parent_trees *parents,
4699 struct got_tree_entry *te)
4701 const struct got_error *err = NULL;
4702 struct tog_parent_tree *pt;
4703 size_t len = 2; /* for leading slash and NUL */
4705 TAILQ_FOREACH(pt, parents, entry)
4706 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4707 + 1 /* slash */;
4708 if (te)
4709 len += strlen(got_tree_entry_get_name(te));
4711 *path = calloc(1, len);
4712 if (path == NULL)
4713 return got_error_from_errno("calloc");
4715 (*path)[0] = '/';
4716 pt = TAILQ_LAST(parents, tog_parent_trees);
4717 while (pt) {
4718 const char *name = got_tree_entry_get_name(pt->selected_entry);
4719 if (strlcat(*path, name, len) >= len) {
4720 err = got_error(GOT_ERR_NO_SPACE);
4721 goto done;
4723 if (strlcat(*path, "/", len) >= len) {
4724 err = got_error(GOT_ERR_NO_SPACE);
4725 goto done;
4727 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4729 if (te) {
4730 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4731 err = got_error(GOT_ERR_NO_SPACE);
4732 goto done;
4735 done:
4736 if (err) {
4737 free(*path);
4738 *path = NULL;
4740 return err;
4743 static const struct got_error *
4744 blame_tree_entry(struct tog_view **new_view, int begin_x,
4745 struct got_tree_entry *te, struct tog_parent_trees *parents,
4746 struct got_object_id *commit_id, struct got_reflist_head *refs,
4747 struct got_repository *repo)
4749 const struct got_error *err = NULL;
4750 char *path;
4751 struct tog_view *blame_view;
4753 *new_view = NULL;
4755 err = tree_entry_path(&path, parents, te);
4756 if (err)
4757 return err;
4759 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4760 if (blame_view == NULL) {
4761 err = got_error_from_errno("view_open");
4762 goto done;
4765 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4766 if (err) {
4767 if (err->code == GOT_ERR_CANCELLED)
4768 err = NULL;
4769 view_close(blame_view);
4770 } else
4771 *new_view = blame_view;
4772 done:
4773 free(path);
4774 return err;
4777 static const struct got_error *
4778 log_tree_entry(struct tog_view **new_view, int begin_x,
4779 struct got_tree_entry *te, struct tog_parent_trees *parents,
4780 struct got_object_id *commit_id, struct got_reflist_head *refs,
4781 struct got_repository *repo)
4783 struct tog_view *log_view;
4784 const struct got_error *err = NULL;
4785 char *path;
4787 *new_view = NULL;
4789 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4790 if (log_view == NULL)
4791 return got_error_from_errno("view_open");
4793 err = tree_entry_path(&path, parents, te);
4794 if (err)
4795 return err;
4797 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4798 if (err)
4799 view_close(log_view);
4800 else
4801 *new_view = log_view;
4802 free(path);
4803 return err;
4806 static const struct got_error *
4807 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4808 struct got_object_id *commit_id, struct got_reflist_head *refs,
4809 struct got_repository *repo)
4811 const struct got_error *err = NULL;
4812 char *commit_id_str = NULL;
4813 struct tog_tree_view_state *s = &view->state.tree;
4815 TAILQ_INIT(&s->parents);
4817 err = got_object_id_str(&commit_id_str, commit_id);
4818 if (err != NULL)
4819 goto done;
4821 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4822 err = got_error_from_errno("asprintf");
4823 goto done;
4826 s->root = s->tree = root;
4827 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4828 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4829 s->commit_id = got_object_id_dup(commit_id);
4830 if (s->commit_id == NULL) {
4831 err = got_error_from_errno("got_object_id_dup");
4832 goto done;
4834 s->refs = refs;
4835 s->repo = repo;
4837 SIMPLEQ_INIT(&s->colors);
4839 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4840 err = add_color(&s->colors, "\\$$",
4841 TOG_COLOR_TREE_SUBMODULE,
4842 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4843 if (err)
4844 goto done;
4845 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4846 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4847 if (err) {
4848 free_colors(&s->colors);
4849 goto done;
4851 err = add_color(&s->colors, "/$",
4852 TOG_COLOR_TREE_DIRECTORY,
4853 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4854 if (err) {
4855 free_colors(&s->colors);
4856 goto done;
4859 err = add_color(&s->colors, "\\*$",
4860 TOG_COLOR_TREE_EXECUTABLE,
4861 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4862 if (err) {
4863 free_colors(&s->colors);
4864 goto done;
4867 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4868 get_color_value("TOG_COLOR_COMMIT"));
4869 if (err) {
4870 free_colors(&s->colors);
4871 goto done;
4875 view->show = show_tree_view;
4876 view->input = input_tree_view;
4877 view->close = close_tree_view;
4878 view->search_start = search_start_tree_view;
4879 view->search_next = search_next_tree_view;
4880 done:
4881 free(commit_id_str);
4882 if (err) {
4883 free(s->tree_label);
4884 s->tree_label = NULL;
4886 return err;
4889 static const struct got_error *
4890 close_tree_view(struct tog_view *view)
4892 struct tog_tree_view_state *s = &view->state.tree;
4894 free_colors(&s->colors);
4895 free(s->tree_label);
4896 s->tree_label = NULL;
4897 free(s->commit_id);
4898 s->commit_id = NULL;
4899 while (!TAILQ_EMPTY(&s->parents)) {
4900 struct tog_parent_tree *parent;
4901 parent = TAILQ_FIRST(&s->parents);
4902 TAILQ_REMOVE(&s->parents, parent, entry);
4903 free(parent);
4906 if (s->tree != s->root)
4907 got_object_tree_close(s->tree);
4908 got_object_tree_close(s->root);
4910 return NULL;
4913 static const struct got_error *
4914 search_start_tree_view(struct tog_view *view)
4916 struct tog_tree_view_state *s = &view->state.tree;
4918 s->matched_entry = NULL;
4919 return NULL;
4922 static int
4923 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4925 regmatch_t regmatch;
4927 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4928 0) == 0;
4931 static const struct got_error *
4932 search_next_tree_view(struct tog_view *view)
4934 struct tog_tree_view_state *s = &view->state.tree;
4935 struct got_tree_entry *te = NULL;
4937 if (!view->searching) {
4938 view->search_next_done = 1;
4939 return NULL;
4942 if (s->matched_entry) {
4943 if (view->searching == TOG_SEARCH_FORWARD) {
4944 if (s->selected_entry)
4945 te = got_tree_entry_get_next(s->tree,
4946 s->selected_entry);
4947 else
4948 te = got_object_tree_get_first_entry(s->tree);
4949 } else {
4950 if (s->selected_entry == NULL)
4951 te = got_object_tree_get_last_entry(s->tree);
4952 else
4953 te = got_tree_entry_get_prev(s->tree,
4954 s->selected_entry);
4956 } else {
4957 if (view->searching == TOG_SEARCH_FORWARD)
4958 te = got_object_tree_get_first_entry(s->tree);
4959 else
4960 te = got_object_tree_get_last_entry(s->tree);
4963 while (1) {
4964 if (te == NULL) {
4965 if (s->matched_entry == NULL) {
4966 view->search_next_done = 1;
4967 return NULL;
4969 if (view->searching == TOG_SEARCH_FORWARD)
4970 te = got_object_tree_get_first_entry(s->tree);
4971 else
4972 te = got_object_tree_get_last_entry(s->tree);
4975 if (match_tree_entry(te, &view->regex)) {
4976 view->search_next_done = 1;
4977 s->matched_entry = te;
4978 break;
4981 if (view->searching == TOG_SEARCH_FORWARD)
4982 te = got_tree_entry_get_next(s->tree, te);
4983 else
4984 te = got_tree_entry_get_prev(s->tree, te);
4987 if (s->matched_entry) {
4988 s->first_displayed_entry = s->matched_entry;
4989 s->selected = 0;
4992 return NULL;
4995 static const struct got_error *
4996 show_tree_view(struct tog_view *view)
4998 const struct got_error *err = NULL;
4999 struct tog_tree_view_state *s = &view->state.tree;
5000 char *parent_path;
5002 err = tree_entry_path(&parent_path, &s->parents, NULL);
5003 if (err)
5004 return err;
5006 err = draw_tree_entries(view, &s->first_displayed_entry,
5007 &s->last_displayed_entry, &s->selected_entry,
5008 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5009 s->tree, s->selected, view->nlines, s->tree == s->root,
5010 &s->colors);
5011 free(parent_path);
5013 view_vborder(view);
5014 return err;
5017 static const struct got_error *
5018 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5019 struct tog_view **focus_view, struct tog_view *view, int ch)
5021 const struct got_error *err = NULL;
5022 struct tog_tree_view_state *s = &view->state.tree;
5023 struct tog_view *log_view;
5024 int begin_x = 0, nscrolled;
5026 switch (ch) {
5027 case 'i':
5028 s->show_ids = !s->show_ids;
5029 break;
5030 case 'l':
5031 if (!s->selected_entry)
5032 break;
5033 if (view_is_parent_view(view))
5034 begin_x = view_split_begin_x(view->begin_x);
5035 err = log_tree_entry(&log_view, begin_x,
5036 s->selected_entry, &s->parents,
5037 s->commit_id, s->refs, s->repo);
5038 if (view_is_parent_view(view)) {
5039 err = view_close_child(view);
5040 if (err)
5041 return err;
5042 err = view_set_child(view, log_view);
5043 if (err) {
5044 view_close(log_view);
5045 break;
5047 *focus_view = log_view;
5048 view->child_focussed = 1;
5049 } else
5050 *new_view = log_view;
5051 break;
5052 case 'k':
5053 case KEY_UP:
5054 if (s->selected > 0) {
5055 s->selected--;
5056 if (s->selected == 0)
5057 break;
5059 if (s->selected > 0)
5060 break;
5061 tree_scroll_up(view, &s->first_displayed_entry, 1,
5062 s->tree, s->tree == s->root);
5063 break;
5064 case KEY_PPAGE:
5065 case CTRL('b'):
5066 tree_scroll_up(view, &s->first_displayed_entry,
5067 MAX(0, view->nlines - 4 - s->selected), s->tree,
5068 s->tree == s->root);
5069 s->selected = 0;
5070 if (got_object_tree_get_first_entry(s->tree) ==
5071 s->first_displayed_entry && s->tree != s->root)
5072 s->first_displayed_entry = NULL;
5073 break;
5074 case 'j':
5075 case KEY_DOWN:
5076 if (s->selected < s->ndisplayed - 1) {
5077 s->selected++;
5078 break;
5080 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5081 == NULL)
5082 /* can't scroll any further */
5083 break;
5084 tree_scroll_down(&s->first_displayed_entry, 1,
5085 s->last_displayed_entry, s->tree);
5086 break;
5087 case KEY_NPAGE:
5088 case CTRL('f'):
5089 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5090 == NULL) {
5091 /* can't scroll any further; move cursor down */
5092 if (s->selected < s->ndisplayed - 1)
5093 s->selected = s->ndisplayed - 1;
5094 break;
5096 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5097 view->nlines, s->last_displayed_entry, s->tree);
5098 if (nscrolled < view->nlines) {
5099 int ndisplayed = 0;
5100 struct got_tree_entry *te;
5101 te = s->first_displayed_entry;
5102 do {
5103 ndisplayed++;
5104 te = got_tree_entry_get_next(s->tree, te);
5105 } while (te);
5106 s->selected = ndisplayed - 1;
5108 break;
5109 case KEY_ENTER:
5110 case '\r':
5111 case KEY_BACKSPACE:
5112 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5113 struct tog_parent_tree *parent;
5114 /* user selected '..' */
5115 if (s->tree == s->root)
5116 break;
5117 parent = TAILQ_FIRST(&s->parents);
5118 TAILQ_REMOVE(&s->parents, parent,
5119 entry);
5120 got_object_tree_close(s->tree);
5121 s->tree = parent->tree;
5122 s->first_displayed_entry =
5123 parent->first_displayed_entry;
5124 s->selected_entry =
5125 parent->selected_entry;
5126 s->selected = parent->selected;
5127 free(parent);
5128 } else if (S_ISDIR(got_tree_entry_get_mode(
5129 s->selected_entry))) {
5130 struct got_tree_object *subtree;
5131 err = got_object_open_as_tree(&subtree, s->repo,
5132 got_tree_entry_get_id(s->selected_entry));
5133 if (err)
5134 break;
5135 err = tree_view_visit_subtree(subtree, s);
5136 if (err) {
5137 got_object_tree_close(subtree);
5138 break;
5140 } else if (S_ISREG(got_tree_entry_get_mode(
5141 s->selected_entry))) {
5142 struct tog_view *blame_view;
5143 int begin_x = view_is_parent_view(view) ?
5144 view_split_begin_x(view->begin_x) : 0;
5146 err = blame_tree_entry(&blame_view, begin_x,
5147 s->selected_entry, &s->parents,
5148 s->commit_id, s->refs, s->repo);
5149 if (err)
5150 break;
5151 if (view_is_parent_view(view)) {
5152 err = view_close_child(view);
5153 if (err)
5154 return err;
5155 err = view_set_child(view, blame_view);
5156 if (err) {
5157 view_close(blame_view);
5158 break;
5160 *focus_view = blame_view;
5161 view->child_focussed = 1;
5162 } else
5163 *new_view = blame_view;
5165 break;
5166 case KEY_RESIZE:
5167 if (s->selected > view->nlines)
5168 s->selected = s->ndisplayed - 1;
5169 break;
5170 default:
5171 break;
5174 return err;
5177 __dead static void
5178 usage_tree(void)
5180 endwin();
5181 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5182 getprogname());
5183 exit(1);
5186 static const struct got_error *
5187 cmd_tree(int argc, char *argv[])
5189 const struct got_error *error;
5190 struct got_repository *repo = NULL;
5191 struct got_worktree *worktree = NULL;
5192 struct got_reflist_head refs;
5193 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5194 struct got_object_id *commit_id = NULL;
5195 char *commit_id_arg = NULL;
5196 struct got_commit_object *commit = NULL;
5197 struct got_tree_object *tree = NULL;
5198 int ch;
5199 struct tog_view *view;
5201 SIMPLEQ_INIT(&refs);
5203 #ifndef PROFILE
5204 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5205 NULL) == -1)
5206 err(1, "pledge");
5207 #endif
5209 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5210 switch (ch) {
5211 case 'c':
5212 commit_id_arg = optarg;
5213 break;
5214 case 'r':
5215 repo_path = realpath(optarg, NULL);
5216 if (repo_path == NULL)
5217 return got_error_from_errno2("realpath",
5218 optarg);
5219 break;
5220 default:
5221 usage_tree();
5222 /* NOTREACHED */
5226 argc -= optind;
5227 argv += optind;
5229 if (argc > 1)
5230 usage_tree();
5232 cwd = getcwd(NULL, 0);
5233 if (cwd == NULL)
5234 return got_error_from_errno("getcwd");
5236 error = got_worktree_open(&worktree, cwd);
5237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5238 goto done;
5240 if (repo_path == NULL) {
5241 if (worktree)
5242 repo_path =
5243 strdup(got_worktree_get_repo_path(worktree));
5244 else
5245 repo_path = strdup(cwd);
5247 if (repo_path == NULL) {
5248 error = got_error_from_errno("strdup");
5249 goto done;
5252 error = got_repo_open(&repo, repo_path, NULL);
5253 if (error != NULL)
5254 goto done;
5256 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5257 repo, worktree);
5258 if (error)
5259 goto done;
5261 init_curses();
5263 error = apply_unveil(got_repo_get_path(repo), NULL);
5264 if (error)
5265 goto done;
5267 error = got_repo_match_object_id(&commit_id, NULL,
5268 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5269 GOT_OBJ_TYPE_COMMIT, 1, repo);
5270 if (error)
5271 goto done;
5273 error = got_object_open_as_commit(&commit, repo, commit_id);
5274 if (error)
5275 goto done;
5277 error = got_object_open_as_tree(&tree, repo,
5278 got_object_commit_get_tree_id(commit));
5279 if (error)
5280 goto done;
5282 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5283 if (error)
5284 goto done;
5286 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5287 if (view == NULL) {
5288 error = got_error_from_errno("view_open");
5289 goto done;
5291 error = open_tree_view(view, tree, commit_id, &refs, repo);
5292 if (error)
5293 goto done;
5294 if (!got_path_is_root_dir(in_repo_path)) {
5295 error = tree_view_walk_path(&view->state.tree, commit_id,
5296 in_repo_path, repo);
5297 if (error)
5298 goto done;
5301 if (worktree) {
5302 /* Release work tree lock. */
5303 got_worktree_close(worktree);
5304 worktree = NULL;
5306 error = view_loop(view);
5307 done:
5308 free(repo_path);
5309 free(cwd);
5310 free(commit_id);
5311 if (commit)
5312 got_object_commit_close(commit);
5313 if (tree)
5314 got_object_tree_close(tree);
5315 if (repo)
5316 got_repo_close(repo);
5317 got_ref_list_free(&refs);
5318 return error;
5321 static void
5322 list_commands(void)
5324 int i;
5326 fprintf(stderr, "commands:");
5327 for (i = 0; i < nitems(tog_commands); i++) {
5328 struct tog_cmd *cmd = &tog_commands[i];
5329 fprintf(stderr, " %s", cmd->name);
5331 fputc('\n', stderr);
5334 __dead static void
5335 usage(int hflag)
5337 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5338 "[arg ...]\n", getprogname());
5339 if (hflag) {
5340 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5341 list_commands();
5343 exit(1);
5346 static char **
5347 make_argv(int argc, ...)
5349 va_list ap;
5350 char **argv;
5351 int i;
5353 va_start(ap, argc);
5355 argv = calloc(argc, sizeof(char *));
5356 if (argv == NULL)
5357 err(1, "calloc");
5358 for (i = 0; i < argc; i++) {
5359 argv[i] = strdup(va_arg(ap, char *));
5360 if (argv[i] == NULL)
5361 err(1, "strdup");
5364 va_end(ap);
5365 return argv;
5369 * Try to convert 'tog path' into a 'tog log path' command.
5370 * The user could simply have mistyped the command rather than knowingly
5371 * provided a path. So check whether argv[0] can in fact be resolved
5372 * to a path in the HEAD commit and print a special error if not.
5373 * This hack is for mpi@ <3
5375 static const struct got_error *
5376 tog_log_with_path(int argc, char *argv[])
5378 const struct got_error *error = NULL;
5379 struct tog_cmd *cmd = NULL;
5380 struct got_repository *repo = NULL;
5381 struct got_worktree *worktree = NULL;
5382 struct got_object_id *commit_id = NULL, *id = NULL;
5383 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5384 char *commit_id_str = NULL, **cmd_argv = NULL;
5386 cwd = getcwd(NULL, 0);
5387 if (cwd == NULL)
5388 return got_error_from_errno("getcwd");
5390 error = got_worktree_open(&worktree, cwd);
5391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5392 goto done;
5394 if (worktree)
5395 repo_path = strdup(got_worktree_get_repo_path(worktree));
5396 else
5397 repo_path = strdup(cwd);
5398 if (repo_path == NULL) {
5399 error = got_error_from_errno("strdup");
5400 goto done;
5403 error = got_repo_open(&repo, repo_path, NULL);
5404 if (error != NULL)
5405 goto done;
5407 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5408 repo, worktree);
5409 if (error)
5410 goto done;
5412 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5413 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5414 GOT_OBJ_TYPE_COMMIT, 1, repo);
5415 if (error)
5416 goto done;
5418 if (worktree) {
5419 got_worktree_close(worktree);
5420 worktree = NULL;
5423 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5424 if (error) {
5425 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5426 goto done;
5427 fprintf(stderr, "%s: '%s' is no known command or path\n",
5428 getprogname(), argv[0]);
5429 usage(1);
5430 /* not reached */
5433 got_repo_close(repo);
5434 repo = NULL;
5436 error = got_object_id_str(&commit_id_str, commit_id);
5437 if (error)
5438 goto done;
5440 cmd = &tog_commands[0]; /* log */
5441 argc = 4;
5442 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5443 error = cmd->cmd_main(argc, cmd_argv);
5444 done:
5445 if (repo)
5446 got_repo_close(repo);
5447 if (worktree)
5448 got_worktree_close(worktree);
5449 free(id);
5450 free(commit_id_str);
5451 free(commit_id);
5452 free(cwd);
5453 free(repo_path);
5454 free(in_repo_path);
5455 if (cmd_argv) {
5456 int i;
5457 for (i = 0; i < argc; i++)
5458 free(cmd_argv[i]);
5459 free(cmd_argv);
5461 return error;
5464 int
5465 main(int argc, char *argv[])
5467 const struct got_error *error = NULL;
5468 struct tog_cmd *cmd = NULL;
5469 int ch, hflag = 0, Vflag = 0;
5470 char **cmd_argv = NULL;
5471 static struct option longopts[] = {
5472 { "version", no_argument, NULL, 'V' },
5473 { NULL, 0, NULL, 0}
5476 setlocale(LC_CTYPE, "");
5478 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5479 switch (ch) {
5480 case 'h':
5481 hflag = 1;
5482 break;
5483 case 'V':
5484 Vflag = 1;
5485 break;
5486 default:
5487 usage(hflag);
5488 /* NOTREACHED */
5492 argc -= optind;
5493 argv += optind;
5494 optind = 0;
5495 optreset = 1;
5497 if (Vflag) {
5498 got_version_print_str();
5499 return 1;
5502 if (argc == 0) {
5503 if (hflag)
5504 usage(hflag);
5505 /* Build an argument vector which runs a default command. */
5506 cmd = &tog_commands[0];
5507 argc = 1;
5508 cmd_argv = make_argv(argc, cmd->name);
5509 } else {
5510 int i;
5512 /* Did the user specify a command? */
5513 for (i = 0; i < nitems(tog_commands); i++) {
5514 if (strncmp(tog_commands[i].name, argv[0],
5515 strlen(argv[0])) == 0) {
5516 cmd = &tog_commands[i];
5517 break;
5522 if (cmd == NULL) {
5523 if (argc != 1)
5524 usage(0);
5525 /* No command specified; try log with a path */
5526 error = tog_log_with_path(argc, argv);
5527 } else {
5528 if (hflag)
5529 cmd->cmd_usage();
5530 else
5531 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5534 endwin();
5535 if (cmd_argv) {
5536 int i;
5537 for (i = 0; i < argc; i++)
5538 free(cmd_argv[i]);
5539 free(cmd_argv);
5542 if (error && error->code != GOT_ERR_CANCELLED)
5543 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5544 return 0;