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 pthread_cond_t commit_loaded;
269 int commits_needed;
270 struct got_commit_graph *graph;
271 struct commit_queue *commits;
272 const char *in_repo_path;
273 struct got_object_id *start_id;
274 struct got_repository *repo;
275 int log_complete;
276 sig_atomic_t *quit;
277 struct commit_queue_entry **first_displayed_entry;
278 struct commit_queue_entry **selected_entry;
279 int *searching;
280 int *search_next_done;
281 regex_t *regex;
282 };
284 struct tog_log_view_state {
285 struct commit_queue commits;
286 struct commit_queue_entry *first_displayed_entry;
287 struct commit_queue_entry *last_displayed_entry;
288 struct commit_queue_entry *selected_entry;
289 int selected;
290 char *in_repo_path;
291 const char *head_ref_name;
292 int log_branches;
293 struct got_repository *repo;
294 struct got_reflist_head *refs;
295 struct got_object_id *start_id;
296 sig_atomic_t quit;
297 pthread_t thread;
298 struct tog_log_thread_args thread_args;
299 struct commit_queue_entry *matched_entry;
300 struct commit_queue_entry *search_entry;
301 struct tog_colors colors;
302 };
304 #define TOG_COLOR_DIFF_MINUS 1
305 #define TOG_COLOR_DIFF_PLUS 2
306 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
307 #define TOG_COLOR_DIFF_META 4
308 #define TOG_COLOR_TREE_SUBMODULE 5
309 #define TOG_COLOR_TREE_SYMLINK 6
310 #define TOG_COLOR_TREE_DIRECTORY 7
311 #define TOG_COLOR_TREE_EXECUTABLE 8
312 #define TOG_COLOR_COMMIT 9
313 #define TOG_COLOR_AUTHOR 10
314 #define TOG_COLOR_DATE 11
316 struct tog_blame_cb_args {
317 struct tog_blame_line *lines; /* one per line */
318 int nlines;
320 struct tog_view *view;
321 struct got_object_id *commit_id;
322 int *quit;
323 };
325 struct tog_blame_thread_args {
326 const char *path;
327 struct got_repository *repo;
328 struct tog_blame_cb_args *cb_args;
329 int *complete;
330 got_cancel_cb cancel_cb;
331 void *cancel_arg;
332 };
334 struct tog_blame {
335 FILE *f;
336 size_t filesize;
337 struct tog_blame_line *lines;
338 int nlines;
339 off_t *line_offsets;
340 pthread_t thread;
341 struct tog_blame_thread_args thread_args;
342 struct tog_blame_cb_args cb_args;
343 const char *path;
344 };
346 struct tog_blame_view_state {
347 int first_displayed_line;
348 int last_displayed_line;
349 int selected_line;
350 int blame_complete;
351 int eof;
352 int done;
353 struct got_object_id_queue blamed_commits;
354 struct got_object_qid *blamed_commit;
355 char *path;
356 struct got_repository *repo;
357 struct got_reflist_head *refs;
358 struct got_object_id *commit_id;
359 struct tog_blame blame;
360 int matched_line;
361 struct tog_colors colors;
362 };
364 struct tog_parent_tree {
365 TAILQ_ENTRY(tog_parent_tree) entry;
366 struct got_tree_object *tree;
367 struct got_tree_entry *first_displayed_entry;
368 struct got_tree_entry *selected_entry;
369 int selected;
370 };
372 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
374 struct tog_tree_view_state {
375 char *tree_label;
376 struct got_tree_object *root;
377 struct got_tree_object *tree;
378 struct got_tree_entry *first_displayed_entry;
379 struct got_tree_entry *last_displayed_entry;
380 struct got_tree_entry *selected_entry;
381 int ndisplayed, selected, show_ids;
382 struct tog_parent_trees parents;
383 struct got_object_id *commit_id;
384 struct got_repository *repo;
385 struct got_reflist_head *refs;
386 struct got_tree_entry *matched_entry;
387 struct tog_colors colors;
388 };
390 /*
391 * We implement two types of views: parent views and child views.
393 * The 'Tab' key switches between a parent view and its child view.
394 * Child views are shown side-by-side to their parent view, provided
395 * there is enough screen estate.
397 * When a new view is opened from within a parent view, this new view
398 * becomes a child view of the parent view, replacing any existing child.
400 * When a new view is opened from within a child view, this new view
401 * becomes a parent view which will obscure the views below until the
402 * user quits the new parent view by typing 'q'.
404 * This list of views contains parent views only.
405 * Child views are only pointed to by their parent view.
406 */
407 TAILQ_HEAD(tog_view_list_head, tog_view);
409 struct tog_view {
410 TAILQ_ENTRY(tog_view) entry;
411 WINDOW *window;
412 PANEL *panel;
413 int nlines, ncols, begin_y, begin_x;
414 int lines, cols; /* copies of LINES and COLS */
415 int focussed;
416 struct tog_view *parent;
417 struct tog_view *child;
418 int child_focussed;
420 /* type-specific state */
421 enum tog_view_type type;
422 union {
423 struct tog_diff_view_state diff;
424 struct tog_log_view_state log;
425 struct tog_blame_view_state blame;
426 struct tog_tree_view_state tree;
427 } state;
429 const struct got_error *(*show)(struct tog_view *);
430 const struct got_error *(*input)(struct tog_view **,
431 struct tog_view **, struct tog_view**, struct tog_view *, int);
432 const struct got_error *(*close)(struct tog_view *);
434 const struct got_error *(*search_start)(struct tog_view *);
435 const struct got_error *(*search_next)(struct tog_view *);
436 int searching;
437 #define TOG_SEARCH_FORWARD 1
438 #define TOG_SEARCH_BACKWARD 2
439 int search_next_done;
440 #define TOG_SEARCH_HAVE_MORE 1
441 #define TOG_SEARCH_NO_MORE 2
442 regex_t regex;
443 };
445 static const struct got_error *open_diff_view(struct tog_view *,
446 struct got_object_id *, struct got_object_id *, struct tog_view *,
447 struct got_reflist_head *, struct got_repository *);
448 static const struct got_error *show_diff_view(struct tog_view *);
449 static const struct got_error *input_diff_view(struct tog_view **,
450 struct tog_view **, struct tog_view **, struct tog_view *, int);
451 static const struct got_error* close_diff_view(struct tog_view *);
452 static const struct got_error *search_start_diff_view(struct tog_view *);
453 static const struct got_error *search_next_diff_view(struct tog_view *);
455 static const struct got_error *open_log_view(struct tog_view *,
456 struct got_object_id *, struct got_reflist_head *,
457 struct got_repository *, const char *, const char *, int);
458 static const struct got_error * show_log_view(struct tog_view *);
459 static const struct got_error *input_log_view(struct tog_view **,
460 struct tog_view **, struct tog_view **, struct tog_view *, int);
461 static const struct got_error *close_log_view(struct tog_view *);
462 static const struct got_error *search_start_log_view(struct tog_view *);
463 static const struct got_error *search_next_log_view(struct tog_view *);
465 static const struct got_error *open_blame_view(struct tog_view *, char *,
466 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
467 static const struct got_error *show_blame_view(struct tog_view *);
468 static const struct got_error *input_blame_view(struct tog_view **,
469 struct tog_view **, struct tog_view **, struct tog_view *, int);
470 static const struct got_error *close_blame_view(struct tog_view *);
471 static const struct got_error *search_start_blame_view(struct tog_view *);
472 static const struct got_error *search_next_blame_view(struct tog_view *);
474 static const struct got_error *open_tree_view(struct tog_view *,
475 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
476 struct got_repository *);
477 static const struct got_error *show_tree_view(struct tog_view *);
478 static const struct got_error *input_tree_view(struct tog_view **,
479 struct tog_view **, struct tog_view **, struct tog_view *, int);
480 static const struct got_error *close_tree_view(struct tog_view *);
481 static const struct got_error *search_start_tree_view(struct tog_view *);
482 static const struct got_error *search_next_tree_view(struct tog_view *);
484 static volatile sig_atomic_t tog_sigwinch_received;
485 static volatile sig_atomic_t tog_sigpipe_received;
486 static volatile sig_atomic_t tog_sigcont_received;
488 static void
489 tog_sigwinch(int signo)
491 tog_sigwinch_received = 1;
494 static void
495 tog_sigpipe(int signo)
497 tog_sigpipe_received = 1;
500 static void
501 tog_sigcont(int signo)
503 tog_sigcont_received = 1;
506 static const struct got_error *
507 view_close(struct tog_view *view)
509 const struct got_error *err = NULL;
511 if (view->child) {
512 view_close(view->child);
513 view->child = NULL;
515 if (view->close)
516 err = view->close(view);
517 if (view->panel)
518 del_panel(view->panel);
519 if (view->window)
520 delwin(view->window);
521 free(view);
522 return err;
525 static struct tog_view *
526 view_open(int nlines, int ncols, int begin_y, int begin_x,
527 enum tog_view_type type)
529 struct tog_view *view = calloc(1, sizeof(*view));
531 if (view == NULL)
532 return NULL;
534 view->type = type;
535 view->lines = LINES;
536 view->cols = COLS;
537 view->nlines = nlines ? nlines : LINES - begin_y;
538 view->ncols = ncols ? ncols : COLS - begin_x;
539 view->begin_y = begin_y;
540 view->begin_x = begin_x;
541 view->window = newwin(nlines, ncols, begin_y, begin_x);
542 if (view->window == NULL) {
543 view_close(view);
544 return NULL;
546 view->panel = new_panel(view->window);
547 if (view->panel == NULL ||
548 set_panel_userptr(view->panel, view) != OK) {
549 view_close(view);
550 return NULL;
553 keypad(view->window, TRUE);
554 return view;
557 static int
558 view_split_begin_x(int begin_x)
560 if (begin_x > 0 || COLS < 120)
561 return 0;
562 return (COLS - MAX(COLS / 2, 80));
565 static const struct got_error *view_resize(struct tog_view *);
567 static const struct got_error *
568 view_splitscreen(struct tog_view *view)
570 const struct got_error *err = NULL;
572 view->begin_y = 0;
573 view->begin_x = view_split_begin_x(0);
574 view->nlines = LINES;
575 view->ncols = COLS - view->begin_x;
576 view->lines = LINES;
577 view->cols = COLS;
578 err = view_resize(view);
579 if (err)
580 return err;
582 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
583 return got_error_from_errno("mvwin");
585 return NULL;
588 static const struct got_error *
589 view_fullscreen(struct tog_view *view)
591 const struct got_error *err = NULL;
593 view->begin_x = 0;
594 view->begin_y = 0;
595 view->nlines = LINES;
596 view->ncols = COLS;
597 view->lines = LINES;
598 view->cols = COLS;
599 err = view_resize(view);
600 if (err)
601 return err;
603 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
604 return got_error_from_errno("mvwin");
606 return NULL;
609 static int
610 view_is_parent_view(struct tog_view *view)
612 return view->parent == NULL;
615 static const struct got_error *
616 view_resize(struct tog_view *view)
618 int nlines, ncols;
620 if (view->lines > LINES)
621 nlines = view->nlines - (view->lines - LINES);
622 else
623 nlines = view->nlines + (LINES - view->lines);
625 if (view->cols > COLS)
626 ncols = view->ncols - (view->cols - COLS);
627 else
628 ncols = view->ncols + (COLS - view->cols);
630 if (wresize(view->window, nlines, ncols) == ERR)
631 return got_error_from_errno("wresize");
632 if (replace_panel(view->panel, view->window) == ERR)
633 return got_error_from_errno("replace_panel");
634 wclear(view->window);
636 view->nlines = nlines;
637 view->ncols = ncols;
638 view->lines = LINES;
639 view->cols = COLS;
641 if (view->child) {
642 view->child->begin_x = view_split_begin_x(view->begin_x);
643 if (view->child->begin_x == 0) {
644 view_fullscreen(view->child);
645 if (view->child->focussed)
646 show_panel(view->child->panel);
647 else
648 show_panel(view->panel);
649 } else {
650 view_splitscreen(view->child);
651 show_panel(view->child->panel);
655 return NULL;
658 static const struct got_error *
659 view_close_child(struct tog_view *view)
661 const struct got_error *err = NULL;
663 if (view->child == NULL)
664 return NULL;
666 err = view_close(view->child);
667 view->child = NULL;
668 return err;
671 static const struct got_error *
672 view_set_child(struct tog_view *view, struct tog_view *child)
674 const struct got_error *err = NULL;
676 view->child = child;
677 child->parent = view;
678 return err;
681 static int
682 view_is_splitscreen(struct tog_view *view)
684 return view->begin_x > 0;
687 static void
688 tog_resizeterm(void)
690 int cols, lines;
691 struct winsize size;
693 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
694 cols = 80; /* Default */
695 lines = 24;
696 } else {
697 cols = size.ws_col;
698 lines = size.ws_row;
700 resize_term(lines, cols);
703 static const struct got_error *
704 view_search_start(struct tog_view *view)
706 const struct got_error *err = NULL;
707 char pattern[1024];
708 int ret;
710 if (view->nlines < 1)
711 return NULL;
713 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
714 wclrtoeol(view->window);
716 nocbreak();
717 echo();
718 ret = wgetnstr(view->window, pattern, sizeof(pattern));
719 cbreak();
720 noecho();
721 if (ret == ERR)
722 return NULL;
724 if (view->searching) {
725 regfree(&view->regex);
726 view->searching = 0;
729 if (regcomp(&view->regex, pattern,
730 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
731 err = view->search_start(view);
732 if (err) {
733 regfree(&view->regex);
734 return err;
736 view->searching = TOG_SEARCH_FORWARD;
737 view->search_next_done = 0;
738 view->search_next(view);
741 return NULL;
744 static const struct got_error *
745 view_input(struct tog_view **new, struct tog_view **dead,
746 struct tog_view **focus, int *done, struct tog_view *view,
747 struct tog_view_list_head *views)
749 const struct got_error *err = NULL;
750 struct tog_view *v;
751 int ch, errcode;
753 *new = NULL;
754 *dead = NULL;
755 *focus = NULL;
757 /* Clear "no more matches" indicator. */
758 if (view->search_next_done == TOG_SEARCH_NO_MORE)
759 view->search_next_done = TOG_SEARCH_HAVE_MORE;
761 if (view->searching && !view->search_next_done) {
762 errcode = pthread_mutex_unlock(&tog_mutex);
763 if (errcode)
764 return got_error_set_errno(errcode,
765 "pthread_mutex_unlock");
766 pthread_yield();
767 errcode = pthread_mutex_lock(&tog_mutex);
768 if (errcode)
769 return got_error_set_errno(errcode,
770 "pthread_mutex_lock");
771 view->search_next(view);
772 return NULL;
775 nodelay(stdscr, FALSE);
776 /* Allow threads to make progress while we are waiting for input. */
777 errcode = pthread_mutex_unlock(&tog_mutex);
778 if (errcode)
779 return got_error_set_errno(errcode, "pthread_mutex_unlock");
780 ch = wgetch(view->window);
781 errcode = pthread_mutex_lock(&tog_mutex);
782 if (errcode)
783 return got_error_set_errno(errcode, "pthread_mutex_lock");
784 nodelay(stdscr, TRUE);
786 if (tog_sigwinch_received || tog_sigcont_received) {
787 tog_resizeterm();
788 tog_sigwinch_received = 0;
789 tog_sigcont_received = 0;
790 TAILQ_FOREACH(v, views, entry) {
791 err = view_resize(v);
792 if (err)
793 return err;
794 err = v->input(new, dead, focus, v, KEY_RESIZE);
795 if (err)
796 return err;
800 switch (ch) {
801 case ERR:
802 break;
803 case '\t':
804 if (view->child) {
805 *focus = view->child;
806 view->child_focussed = 1;
807 } else if (view->parent) {
808 *focus = view->parent;
809 view->parent->child_focussed = 0;
811 break;
812 case 'q':
813 err = view->input(new, dead, focus, view, ch);
814 *dead = view;
815 break;
816 case 'Q':
817 *done = 1;
818 break;
819 case 'f':
820 if (view_is_parent_view(view)) {
821 if (view->child == NULL)
822 break;
823 if (view_is_splitscreen(view->child)) {
824 *focus = view->child;
825 view->child_focussed = 1;
826 err = view_fullscreen(view->child);
827 } else
828 err = view_splitscreen(view->child);
829 if (err)
830 break;
831 err = view->child->input(new, dead, focus,
832 view->child, KEY_RESIZE);
833 } else {
834 if (view_is_splitscreen(view)) {
835 *focus = view;
836 view->parent->child_focussed = 1;
837 err = view_fullscreen(view);
838 } else {
839 err = view_splitscreen(view);
841 if (err)
842 break;
843 err = view->input(new, dead, focus, view,
844 KEY_RESIZE);
846 break;
847 case KEY_RESIZE:
848 break;
849 case '/':
850 if (view->search_start)
851 view_search_start(view);
852 else
853 err = view->input(new, dead, focus, view, ch);
854 break;
855 case 'N':
856 case 'n':
857 if (view->search_next) {
858 view->searching = (ch == 'n' ?
859 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
860 view->search_next_done = 0;
861 view->search_next(view);
862 } else
863 err = view->input(new, dead, focus, view, ch);
864 break;
865 default:
866 err = view->input(new, dead, focus, view, ch);
867 break;
870 return err;
873 void
874 view_vborder(struct tog_view *view)
876 PANEL *panel;
877 struct tog_view *view_above;
879 if (view->parent)
880 return view_vborder(view->parent);
882 panel = panel_above(view->panel);
883 if (panel == NULL)
884 return;
886 view_above = panel_userptr(panel);
887 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
888 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
891 int
892 view_needs_focus_indication(struct tog_view *view)
894 if (view_is_parent_view(view)) {
895 if (view->child == NULL || view->child_focussed)
896 return 0;
897 if (!view_is_splitscreen(view->child))
898 return 0;
899 } else if (!view_is_splitscreen(view))
900 return 0;
902 return view->focussed;
905 static const struct got_error *
906 view_loop(struct tog_view *view)
908 const struct got_error *err = NULL;
909 struct tog_view_list_head views;
910 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
911 int fast_refresh = 10;
912 int done = 0, errcode;
914 errcode = pthread_mutex_lock(&tog_mutex);
915 if (errcode)
916 return got_error_set_errno(errcode, "pthread_mutex_lock");
918 TAILQ_INIT(&views);
919 TAILQ_INSERT_HEAD(&views, view, entry);
921 main_view = view;
922 view->focussed = 1;
923 err = view->show(view);
924 if (err)
925 return err;
926 update_panels();
927 doupdate();
928 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
929 /* Refresh fast during initialization, then become slower. */
930 if (fast_refresh && fast_refresh-- == 0)
931 halfdelay(10); /* switch to once per second */
933 err = view_input(&new_view, &dead_view, &focus_view, &done,
934 view, &views);
935 if (err)
936 break;
937 if (dead_view) {
938 struct tog_view *prev = NULL;
940 if (view_is_parent_view(dead_view))
941 prev = TAILQ_PREV(dead_view,
942 tog_view_list_head, entry);
943 else if (view->parent != dead_view)
944 prev = view->parent;
946 if (dead_view->parent)
947 dead_view->parent->child = NULL;
948 else
949 TAILQ_REMOVE(&views, dead_view, entry);
951 err = view_close(dead_view);
952 if (err || (dead_view == main_view && new_view == NULL))
953 goto done;
955 if (view == dead_view) {
956 if (focus_view)
957 view = focus_view;
958 else if (prev)
959 view = prev;
960 else if (!TAILQ_EMPTY(&views))
961 view = TAILQ_LAST(&views,
962 tog_view_list_head);
963 else
964 view = NULL;
965 if (view) {
966 if (view->child && view->child_focussed)
967 focus_view = view->child;
968 else
969 focus_view = view;
973 if (new_view) {
974 struct tog_view *v, *t;
975 /* Only allow one parent view per type. */
976 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
977 if (v->type != new_view->type)
978 continue;
979 TAILQ_REMOVE(&views, v, entry);
980 err = view_close(v);
981 if (err)
982 goto done;
983 break;
985 TAILQ_INSERT_TAIL(&views, new_view, entry);
986 view = new_view;
987 if (focus_view == NULL)
988 focus_view = new_view;
990 if (focus_view) {
991 show_panel(focus_view->panel);
992 if (view)
993 view->focussed = 0;
994 focus_view->focussed = 1;
995 view = focus_view;
996 if (new_view)
997 show_panel(new_view->panel);
998 if (view->child && view_is_splitscreen(view->child))
999 show_panel(view->child->panel);
1001 if (view) {
1002 if (focus_view == NULL) {
1003 view->focussed = 1;
1004 show_panel(view->panel);
1005 if (view->child && view_is_splitscreen(view->child))
1006 show_panel(view->child->panel);
1007 focus_view = view;
1009 if (view->parent) {
1010 err = view->parent->show(view->parent);
1011 if (err)
1012 goto done;
1014 err = view->show(view);
1015 if (err)
1016 goto done;
1017 if (view->child) {
1018 err = view->child->show(view->child);
1019 if (err)
1020 goto done;
1022 update_panels();
1023 doupdate();
1026 done:
1027 while (!TAILQ_EMPTY(&views)) {
1028 view = TAILQ_FIRST(&views);
1029 TAILQ_REMOVE(&views, view, entry);
1030 view_close(view);
1033 errcode = pthread_mutex_unlock(&tog_mutex);
1034 if (errcode && err == NULL)
1035 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1037 return err;
1040 __dead static void
1041 usage_log(void)
1043 endwin();
1044 fprintf(stderr,
1045 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1046 getprogname());
1047 exit(1);
1050 /* Create newly allocated wide-character string equivalent to a byte string. */
1051 static const struct got_error *
1052 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1054 char *vis = NULL;
1055 const struct got_error *err = NULL;
1057 *ws = NULL;
1058 *wlen = mbstowcs(NULL, s, 0);
1059 if (*wlen == (size_t)-1) {
1060 int vislen;
1061 if (errno != EILSEQ)
1062 return got_error_from_errno("mbstowcs");
1064 /* byte string invalid in current encoding; try to "fix" it */
1065 err = got_mbsavis(&vis, &vislen, s);
1066 if (err)
1067 return err;
1068 *wlen = mbstowcs(NULL, vis, 0);
1069 if (*wlen == (size_t)-1) {
1070 err = got_error_from_errno("mbstowcs"); /* give up */
1071 goto done;
1075 *ws = calloc(*wlen + 1, sizeof(**ws));
1076 if (*ws == NULL) {
1077 err = got_error_from_errno("calloc");
1078 goto done;
1081 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1082 err = got_error_from_errno("mbstowcs");
1083 done:
1084 free(vis);
1085 if (err) {
1086 free(*ws);
1087 *ws = NULL;
1088 *wlen = 0;
1090 return err;
1093 /* Format a line for display, ensuring that it won't overflow a width limit. */
1094 static const struct got_error *
1095 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1096 int col_tab_align)
1098 const struct got_error *err = NULL;
1099 int cols = 0;
1100 wchar_t *wline = NULL;
1101 size_t wlen;
1102 int i;
1104 *wlinep = NULL;
1105 *widthp = 0;
1107 err = mbs2ws(&wline, &wlen, line);
1108 if (err)
1109 return err;
1111 i = 0;
1112 while (i < wlen) {
1113 int width = wcwidth(wline[i]);
1115 if (width == 0) {
1116 i++;
1117 continue;
1120 if (width == 1 || width == 2) {
1121 if (cols + width > wlimit)
1122 break;
1123 cols += width;
1124 i++;
1125 } else if (width == -1) {
1126 if (wline[i] == L'\t') {
1127 width = TABSIZE -
1128 ((cols + col_tab_align) % TABSIZE);
1129 if (cols + width > wlimit)
1130 break;
1131 cols += width;
1133 i++;
1134 } else {
1135 err = got_error_from_errno("wcwidth");
1136 goto done;
1139 wline[i] = L'\0';
1140 if (widthp)
1141 *widthp = cols;
1142 done:
1143 if (err)
1144 free(wline);
1145 else
1146 *wlinep = wline;
1147 return err;
1150 static const struct got_error*
1151 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1152 struct got_object_id *id, struct got_repository *repo)
1154 static const struct got_error *err = NULL;
1155 struct got_reflist_entry *re;
1156 char *s;
1157 const char *name;
1159 *refs_str = NULL;
1161 SIMPLEQ_FOREACH(re, refs, entry) {
1162 struct got_tag_object *tag = NULL;
1163 int cmp;
1165 name = got_ref_get_name(re->ref);
1166 if (strcmp(name, GOT_REF_HEAD) == 0)
1167 continue;
1168 if (strncmp(name, "refs/", 5) == 0)
1169 name += 5;
1170 if (strncmp(name, "got/", 4) == 0)
1171 continue;
1172 if (strncmp(name, "heads/", 6) == 0)
1173 name += 6;
1174 if (strncmp(name, "remotes/", 8) == 0)
1175 name += 8;
1176 if (strncmp(name, "tags/", 5) == 0) {
1177 err = got_object_open_as_tag(&tag, repo, re->id);
1178 if (err) {
1179 if (err->code != GOT_ERR_OBJ_TYPE)
1180 break;
1181 /* Ref points at something other than a tag. */
1182 err = NULL;
1183 tag = NULL;
1186 cmp = got_object_id_cmp(tag ?
1187 got_object_tag_get_object_id(tag) : re->id, id);
1188 if (tag)
1189 got_object_tag_close(tag);
1190 if (cmp != 0)
1191 continue;
1192 s = *refs_str;
1193 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1194 s ? ", " : "", name) == -1) {
1195 err = got_error_from_errno("asprintf");
1196 free(s);
1197 *refs_str = NULL;
1198 break;
1200 free(s);
1203 return err;
1206 static const struct got_error *
1207 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1208 int col_tab_align)
1210 char *smallerthan, *at;
1212 smallerthan = strchr(author, '<');
1213 if (smallerthan && smallerthan[1] != '\0')
1214 author = smallerthan + 1;
1215 at = strchr(author, '@');
1216 if (at)
1217 *at = '\0';
1218 return format_line(wauthor, author_width, author, limit, col_tab_align);
1221 static const struct got_error *
1222 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1223 struct got_object_id *id, struct got_reflist_head *refs,
1224 const size_t date_display_cols, int author_display_cols,
1225 struct tog_colors *colors)
1227 const struct got_error *err = NULL;
1228 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1229 char *logmsg0 = NULL, *logmsg = NULL;
1230 char *author = NULL;
1231 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1232 int author_width, logmsg_width;
1233 char *newline, *line = NULL;
1234 int col, limit;
1235 const int avail = view->ncols;
1236 struct tm tm;
1237 time_t committer_time;
1238 struct tog_color *tc;
1240 committer_time = got_object_commit_get_committer_time(commit);
1241 if (localtime_r(&committer_time, &tm) == NULL)
1242 return got_error_from_errno("localtime_r");
1243 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1244 >= sizeof(datebuf))
1245 return got_error(GOT_ERR_NO_SPACE);
1247 if (avail <= date_display_cols)
1248 limit = MIN(sizeof(datebuf) - 1, avail);
1249 else
1250 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1251 tc = get_color(colors, TOG_COLOR_DATE);
1252 if (tc)
1253 wattr_on(view->window,
1254 COLOR_PAIR(tc->colorpair), NULL);
1255 waddnstr(view->window, datebuf, limit);
1256 if (tc)
1257 wattr_off(view->window,
1258 COLOR_PAIR(tc->colorpair), NULL);
1259 col = limit;
1260 if (col > avail)
1261 goto done;
1263 if (avail >= 120) {
1264 char *id_str;
1265 err = got_object_id_str(&id_str, id);
1266 if (err)
1267 goto done;
1268 tc = get_color(colors, TOG_COLOR_COMMIT);
1269 if (tc)
1270 wattr_on(view->window,
1271 COLOR_PAIR(tc->colorpair), NULL);
1272 wprintw(view->window, "%.8s ", id_str);
1273 if (tc)
1274 wattr_off(view->window,
1275 COLOR_PAIR(tc->colorpair), NULL);
1276 free(id_str);
1277 col += 9;
1278 if (col > avail)
1279 goto done;
1282 author = strdup(got_object_commit_get_author(commit));
1283 if (author == NULL) {
1284 err = got_error_from_errno("strdup");
1285 goto done;
1287 err = format_author(&wauthor, &author_width, author, avail - col, col);
1288 if (err)
1289 goto done;
1290 tc = get_color(colors, TOG_COLOR_AUTHOR);
1291 if (tc)
1292 wattr_on(view->window,
1293 COLOR_PAIR(tc->colorpair), NULL);
1294 waddwstr(view->window, wauthor);
1295 if (tc)
1296 wattr_off(view->window,
1297 COLOR_PAIR(tc->colorpair), NULL);
1298 col += author_width;
1299 while (col < avail && author_width < author_display_cols + 2) {
1300 waddch(view->window, ' ');
1301 col++;
1302 author_width++;
1304 if (col > avail)
1305 goto done;
1307 err = got_object_commit_get_logmsg(&logmsg0, commit);
1308 if (err)
1309 goto done;
1310 logmsg = logmsg0;
1311 while (*logmsg == '\n')
1312 logmsg++;
1313 newline = strchr(logmsg, '\n');
1314 if (newline)
1315 *newline = '\0';
1316 limit = avail - col;
1317 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1318 if (err)
1319 goto done;
1320 waddwstr(view->window, wlogmsg);
1321 col += logmsg_width;
1322 while (col < avail) {
1323 waddch(view->window, ' ');
1324 col++;
1326 done:
1327 free(logmsg0);
1328 free(wlogmsg);
1329 free(author);
1330 free(wauthor);
1331 free(line);
1332 return err;
1335 static struct commit_queue_entry *
1336 alloc_commit_queue_entry(struct got_commit_object *commit,
1337 struct got_object_id *id)
1339 struct commit_queue_entry *entry;
1341 entry = calloc(1, sizeof(*entry));
1342 if (entry == NULL)
1343 return NULL;
1345 entry->id = id;
1346 entry->commit = commit;
1347 return entry;
1350 static void
1351 pop_commit(struct commit_queue *commits)
1353 struct commit_queue_entry *entry;
1355 entry = TAILQ_FIRST(&commits->head);
1356 TAILQ_REMOVE(&commits->head, entry, entry);
1357 got_object_commit_close(entry->commit);
1358 commits->ncommits--;
1359 /* Don't free entry->id! It is owned by the commit graph. */
1360 free(entry);
1363 static void
1364 free_commits(struct commit_queue *commits)
1366 while (!TAILQ_EMPTY(&commits->head))
1367 pop_commit(commits);
1370 static const struct got_error *
1371 match_commit(int *have_match, struct got_object_id *id,
1372 struct got_commit_object *commit, regex_t *regex)
1374 const struct got_error *err = NULL;
1375 regmatch_t regmatch;
1376 char *id_str = NULL, *logmsg = NULL;
1378 *have_match = 0;
1380 err = got_object_id_str(&id_str, id);
1381 if (err)
1382 return err;
1384 err = got_object_commit_get_logmsg(&logmsg, commit);
1385 if (err)
1386 goto done;
1388 if (regexec(regex, got_object_commit_get_author(commit), 1,
1389 &regmatch, 0) == 0 ||
1390 regexec(regex, got_object_commit_get_committer(commit), 1,
1391 &regmatch, 0) == 0 ||
1392 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1393 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1394 *have_match = 1;
1395 done:
1396 free(id_str);
1397 free(logmsg);
1398 return err;
1401 static const struct got_error *
1402 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1403 int minqueue, struct got_repository *repo, const char *path,
1404 int *searching, int *search_next_done, regex_t *regex)
1406 const struct got_error *err = NULL;
1407 int nqueued = 0;
1410 * We keep all commits open throughout the lifetime of the log
1411 * view in order to avoid having to re-fetch commits from disk
1412 * while updating the display.
1414 while (nqueued < minqueue ||
1415 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1416 struct got_object_id *id;
1417 struct got_commit_object *commit;
1418 struct commit_queue_entry *entry;
1419 int errcode;
1421 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1422 if (err || id == NULL)
1423 break;
1425 err = got_object_open_as_commit(&commit, repo, id);
1426 if (err)
1427 break;
1428 entry = alloc_commit_queue_entry(commit, id);
1429 if (entry == NULL) {
1430 err = got_error_from_errno("alloc_commit_queue_entry");
1431 break;
1434 errcode = pthread_mutex_lock(&tog_mutex);
1435 if (errcode) {
1436 err = got_error_set_errno(errcode,
1437 "pthread_mutex_lock");
1438 break;
1441 entry->idx = commits->ncommits;
1442 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1443 nqueued++;
1444 commits->ncommits++;
1446 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1447 int have_match;
1448 err = match_commit(&have_match, id, commit, regex);
1449 if (err)
1450 break;
1451 if (have_match)
1452 *search_next_done = TOG_SEARCH_HAVE_MORE;
1455 errcode = pthread_mutex_unlock(&tog_mutex);
1456 if (errcode && err == NULL)
1457 err = got_error_set_errno(errcode,
1458 "pthread_mutex_unlock");
1459 if (err)
1460 break;
1463 return err;
1466 static const struct got_error *
1467 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1468 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1469 struct commit_queue *commits, int selected_idx, int limit,
1470 struct got_reflist_head *refs, const char *path, int commits_needed,
1471 struct tog_colors *colors)
1473 const struct got_error *err = NULL;
1474 struct tog_log_view_state *s = &view->state.log;
1475 struct commit_queue_entry *entry;
1476 int width;
1477 int ncommits, author_cols = 4;
1478 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1479 char *refs_str = NULL;
1480 wchar_t *wline;
1481 struct tog_color *tc;
1482 static const size_t date_display_cols = 12;
1484 entry = first;
1485 ncommits = 0;
1486 while (entry) {
1487 if (ncommits == selected_idx) {
1488 *selected = entry;
1489 break;
1491 entry = TAILQ_NEXT(entry, entry);
1492 ncommits++;
1495 if (*selected && !(view->searching && view->search_next_done == 0)) {
1496 err = got_object_id_str(&id_str, (*selected)->id);
1497 if (err)
1498 return err;
1499 if (refs) {
1500 err = build_refs_str(&refs_str, refs, (*selected)->id,
1501 s->repo);
1502 if (err)
1503 goto done;
1507 if (commits_needed == 0)
1508 halfdelay(10); /* disable fast refresh */
1510 if (commits_needed > 0) {
1511 if (asprintf(&ncommits_str, " [%d/%d] %s",
1512 entry ? entry->idx + 1 : 0, commits->ncommits,
1513 view->searching ? "searching..." : "loading...") == -1) {
1514 err = got_error_from_errno("asprintf");
1515 goto done;
1517 } else {
1518 if (asprintf(&ncommits_str, " [%d/%d] %s",
1519 entry ? entry->idx + 1 : 0, commits->ncommits,
1520 view->search_next_done == TOG_SEARCH_NO_MORE ?
1521 "no more matches" : (refs_str ? refs_str : "")) == -1) {
1522 err = got_error_from_errno("asprintf");
1523 goto done;
1527 if (path && strcmp(path, "/") != 0) {
1528 if (asprintf(&header, "commit %s %s%s",
1529 id_str ? id_str : "........................................",
1530 path, ncommits_str) == -1) {
1531 err = got_error_from_errno("asprintf");
1532 header = NULL;
1533 goto done;
1535 } else if (asprintf(&header, "commit %s%s",
1536 id_str ? id_str : "........................................",
1537 ncommits_str) == -1) {
1538 err = got_error_from_errno("asprintf");
1539 header = NULL;
1540 goto done;
1542 err = format_line(&wline, &width, header, view->ncols, 0);
1543 if (err)
1544 goto done;
1546 werase(view->window);
1548 if (view_needs_focus_indication(view))
1549 wstandout(view->window);
1550 tc = get_color(colors, TOG_COLOR_COMMIT);
1551 if (tc)
1552 wattr_on(view->window,
1553 COLOR_PAIR(tc->colorpair), NULL);
1554 waddwstr(view->window, wline);
1555 if (tc)
1556 wattr_off(view->window,
1557 COLOR_PAIR(tc->colorpair), NULL);
1558 while (width < view->ncols) {
1559 waddch(view->window, ' ');
1560 width++;
1562 if (view_needs_focus_indication(view))
1563 wstandend(view->window);
1564 free(wline);
1565 if (limit <= 1)
1566 goto done;
1568 /* Grow author column size if necessary. */
1569 entry = first;
1570 ncommits = 0;
1571 while (entry) {
1572 char *author;
1573 wchar_t *wauthor;
1574 int width;
1575 if (ncommits >= limit - 1)
1576 break;
1577 author = strdup(got_object_commit_get_author(entry->commit));
1578 if (author == NULL) {
1579 err = got_error_from_errno("strdup");
1580 goto done;
1582 err = format_author(&wauthor, &width, author, COLS,
1583 date_display_cols);
1584 if (author_cols < width)
1585 author_cols = width;
1586 free(wauthor);
1587 free(author);
1588 ncommits++;
1589 entry = TAILQ_NEXT(entry, entry);
1592 entry = first;
1593 *last = first;
1594 ncommits = 0;
1595 while (entry) {
1596 if (ncommits >= limit - 1)
1597 break;
1598 if (ncommits == selected_idx)
1599 wstandout(view->window);
1600 err = draw_commit(view, entry->commit, entry->id, refs,
1601 date_display_cols, author_cols, colors);
1602 if (ncommits == selected_idx)
1603 wstandend(view->window);
1604 if (err)
1605 goto done;
1606 ncommits++;
1607 *last = entry;
1608 entry = TAILQ_NEXT(entry, entry);
1611 view_vborder(view);
1612 done:
1613 free(id_str);
1614 free(refs_str);
1615 free(ncommits_str);
1616 free(header);
1617 return err;
1620 static void
1621 scroll_up(struct tog_view *view,
1622 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1623 struct commit_queue *commits)
1625 struct commit_queue_entry *entry;
1626 int nscrolled = 0;
1628 entry = TAILQ_FIRST(&commits->head);
1629 if (*first_displayed_entry == entry)
1630 return;
1632 entry = *first_displayed_entry;
1633 while (entry && nscrolled < maxscroll) {
1634 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1635 if (entry) {
1636 *first_displayed_entry = entry;
1637 nscrolled++;
1642 static const struct got_error *
1643 trigger_log_thread(struct tog_view *log_view, int wait,
1644 int *commits_needed, int *log_complete,
1645 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1647 int errcode;
1649 halfdelay(1); /* fast refresh while loading commits */
1651 while (*commits_needed > 0) {
1652 if (*log_complete)
1653 break;
1655 /* Wake the log thread. */
1656 errcode = pthread_cond_signal(need_commits);
1657 if (errcode)
1658 return got_error_set_errno(errcode,
1659 "pthread_cond_signal");
1662 * The mutex will be released while the view loop waits
1663 * in wgetch(), at which time the log thread will run.
1665 if (!wait)
1666 break;
1668 /* Display progress update in log view. */
1669 show_log_view(log_view);
1670 update_panels();
1671 doupdate();
1673 /* Wait right here while next commit is being loaded. */
1674 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1675 if (errcode)
1676 return got_error_set_errno(errcode,
1677 "pthread_cond_wait");
1679 /* Display progress update in log view. */
1680 show_log_view(log_view);
1681 update_panels();
1682 doupdate();
1685 return NULL;
1688 static const struct got_error *
1689 scroll_down(struct tog_view *log_view,
1690 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1691 struct commit_queue_entry **last_displayed_entry,
1692 struct commit_queue *commits, int *log_complete, int *commits_needed,
1693 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1695 const struct got_error *err = NULL;
1696 struct commit_queue_entry *pentry;
1697 int nscrolled = 0, ncommits_needed;
1699 if (*last_displayed_entry == NULL)
1700 return NULL;
1702 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1703 if (commits->ncommits < ncommits_needed && !*log_complete) {
1705 * Ask the log thread for required amount of commits.
1707 (*commits_needed) += maxscroll;
1708 err = trigger_log_thread(log_view, 1, commits_needed,
1709 log_complete, need_commits, commit_loaded);
1710 if (err)
1711 return err;
1714 do {
1715 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1716 if (pentry == NULL)
1717 break;
1719 *last_displayed_entry = pentry;
1721 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1722 if (pentry == NULL)
1723 break;
1724 *first_displayed_entry = pentry;
1725 } while (++nscrolled < maxscroll);
1727 return err;
1730 static const struct got_error *
1731 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1732 struct got_commit_object *commit, struct got_object_id *commit_id,
1733 struct tog_view *log_view, struct got_reflist_head *refs,
1734 struct got_repository *repo)
1736 const struct got_error *err;
1737 struct got_object_qid *parent_id;
1738 struct tog_view *diff_view;
1740 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1741 if (diff_view == NULL)
1742 return got_error_from_errno("view_open");
1744 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1745 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1746 commit_id, log_view, refs, repo);
1747 if (err == NULL)
1748 *new_view = diff_view;
1749 return err;
1752 static const struct got_error *
1753 tree_view_visit_subtree(struct got_tree_object *subtree,
1754 struct tog_tree_view_state *s)
1756 struct tog_parent_tree *parent;
1758 parent = calloc(1, sizeof(*parent));
1759 if (parent == NULL)
1760 return got_error_from_errno("calloc");
1762 parent->tree = s->tree;
1763 parent->first_displayed_entry = s->first_displayed_entry;
1764 parent->selected_entry = s->selected_entry;
1765 parent->selected = s->selected;
1766 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1767 s->tree = subtree;
1768 s->selected = 0;
1769 s->first_displayed_entry = NULL;
1770 return NULL;
1773 static const struct got_error *
1774 tree_view_walk_path(struct tog_tree_view_state *s,
1775 struct got_object_id *commit_id,
1776 const char *path, struct got_repository *repo)
1778 const struct got_error *err = NULL;
1779 struct got_tree_object *tree = NULL;
1780 const char *p;
1781 char *slash, *subpath = NULL;
1783 /* Walk the path and open corresponding tree objects. */
1784 p = path;
1785 while (*p) {
1786 struct got_tree_entry *te;
1787 struct got_object_id *tree_id;
1788 char *te_name;
1790 while (p[0] == '/')
1791 p++;
1793 /* Ensure the correct subtree entry is selected. */
1794 slash = strchr(p, '/');
1795 if (slash == NULL)
1796 te_name = strdup(p);
1797 else
1798 te_name = strndup(p, slash - p);
1799 if (te_name == NULL) {
1800 err = got_error_from_errno("strndup");
1801 break;
1803 te = got_object_tree_find_entry(s->tree, te_name);
1804 if (te == NULL) {
1805 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1806 free(te_name);
1807 break;
1809 free(te_name);
1810 s->selected_entry = te;
1811 s->selected = got_tree_entry_get_index(te);
1812 if (s->tree != s->root)
1813 s->selected++; /* skip '..' */
1815 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1816 /* Jump to this file's entry. */
1817 s->first_displayed_entry = s->selected_entry;
1818 s->selected = 0;
1819 break;
1822 slash = strchr(p, '/');
1823 if (slash)
1824 subpath = strndup(path, slash - path);
1825 else
1826 subpath = strdup(path);
1827 if (subpath == NULL) {
1828 err = got_error_from_errno("strdup");
1829 break;
1832 err = got_object_id_by_path(&tree_id, repo, commit_id,
1833 subpath);
1834 if (err)
1835 break;
1837 err = got_object_open_as_tree(&tree, repo, tree_id);
1838 free(tree_id);
1839 if (err)
1840 break;
1842 err = tree_view_visit_subtree(tree, s);
1843 if (err) {
1844 got_object_tree_close(tree);
1845 break;
1847 if (slash == NULL)
1848 break;
1849 free(subpath);
1850 subpath = NULL;
1851 p = slash;
1854 free(subpath);
1855 return err;
1858 static const struct got_error *
1859 browse_commit_tree(struct tog_view **new_view, int begin_x,
1860 struct commit_queue_entry *entry, const char *path,
1861 struct got_reflist_head *refs, struct got_repository *repo)
1863 const struct got_error *err = NULL;
1864 struct got_tree_object *tree;
1865 struct tog_tree_view_state *s;
1866 struct tog_view *tree_view;
1868 err = got_object_open_as_tree(&tree, repo,
1869 got_object_commit_get_tree_id(entry->commit));
1870 if (err)
1871 return err;
1873 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1874 if (tree_view == NULL)
1875 return got_error_from_errno("view_open");
1877 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1878 if (err) {
1879 got_object_tree_close(tree);
1880 return err;
1882 s = &tree_view->state.tree;
1884 *new_view = tree_view;
1886 if (got_path_is_root_dir(path))
1887 return NULL;
1889 return tree_view_walk_path(s, entry->id, path, repo);
1892 static const struct got_error *
1893 block_signals_used_by_main_thread(void)
1895 sigset_t sigset;
1896 int errcode;
1898 if (sigemptyset(&sigset) == -1)
1899 return got_error_from_errno("sigemptyset");
1901 /* tog handles SIGWINCH and SIGCONT */
1902 if (sigaddset(&sigset, SIGWINCH) == -1)
1903 return got_error_from_errno("sigaddset");
1904 if (sigaddset(&sigset, SIGCONT) == -1)
1905 return got_error_from_errno("sigaddset");
1907 /* ncurses handles SIGTSTP */
1908 if (sigaddset(&sigset, SIGTSTP) == -1)
1909 return got_error_from_errno("sigaddset");
1911 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1912 if (errcode)
1913 return got_error_set_errno(errcode, "pthread_sigmask");
1915 return NULL;
1918 static void *
1919 log_thread(void *arg)
1921 const struct got_error *err = NULL;
1922 int errcode = 0;
1923 struct tog_log_thread_args *a = arg;
1924 int done = 0;
1926 err = block_signals_used_by_main_thread();
1927 if (err)
1928 return (void *)err;
1930 while (!done && !err && !tog_sigpipe_received) {
1931 err = queue_commits(a->graph, a->commits, 1, a->repo,
1932 a->in_repo_path, a->searching, a->search_next_done,
1933 a->regex);
1934 if (err) {
1935 if (err->code != GOT_ERR_ITER_COMPLETED)
1936 return (void *)err;
1937 err = NULL;
1938 done = 1;
1939 } else if (a->commits_needed > 0)
1940 a->commits_needed--;
1942 errcode = pthread_mutex_lock(&tog_mutex);
1943 if (errcode) {
1944 err = got_error_set_errno(errcode,
1945 "pthread_mutex_lock");
1946 break;
1947 } else if (*a->quit)
1948 done = 1;
1949 else if (*a->first_displayed_entry == NULL) {
1950 *a->first_displayed_entry =
1951 TAILQ_FIRST(&a->commits->head);
1952 *a->selected_entry = *a->first_displayed_entry;
1955 errcode = pthread_cond_signal(&a->commit_loaded);
1956 if (errcode) {
1957 err = got_error_set_errno(errcode,
1958 "pthread_cond_signal");
1959 pthread_mutex_unlock(&tog_mutex);
1960 break;
1963 if (done)
1964 a->commits_needed = 0;
1965 else {
1966 if (a->commits_needed == 0) {
1967 errcode = pthread_cond_wait(&a->need_commits,
1968 &tog_mutex);
1969 if (errcode)
1970 err = got_error_set_errno(errcode,
1971 "pthread_cond_wait");
1975 errcode = pthread_mutex_unlock(&tog_mutex);
1976 if (errcode && err == NULL)
1977 err = got_error_set_errno(errcode,
1978 "pthread_mutex_unlock");
1980 a->log_complete = 1;
1981 return (void *)err;
1984 static const struct got_error *
1985 stop_log_thread(struct tog_log_view_state *s)
1987 const struct got_error *err = NULL;
1988 int errcode;
1990 if (s->thread) {
1991 s->quit = 1;
1992 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1993 if (errcode)
1994 return got_error_set_errno(errcode,
1995 "pthread_cond_signal");
1996 errcode = pthread_mutex_unlock(&tog_mutex);
1997 if (errcode)
1998 return got_error_set_errno(errcode,
1999 "pthread_mutex_unlock");
2000 errcode = pthread_join(s->thread, (void **)&err);
2001 if (errcode)
2002 return got_error_set_errno(errcode, "pthread_join");
2003 errcode = pthread_mutex_lock(&tog_mutex);
2004 if (errcode)
2005 return got_error_set_errno(errcode,
2006 "pthread_mutex_lock");
2007 s->thread = NULL;
2010 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2011 if (errcode && err == NULL)
2012 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2014 if (s->thread_args.repo) {
2015 got_repo_close(s->thread_args.repo);
2016 s->thread_args.repo = NULL;
2019 if (s->thread_args.graph) {
2020 got_commit_graph_close(s->thread_args.graph);
2021 s->thread_args.graph = NULL;
2024 return err;
2027 static const struct got_error *
2028 close_log_view(struct tog_view *view)
2030 const struct got_error *err = NULL;
2031 struct tog_log_view_state *s = &view->state.log;
2033 err = stop_log_thread(s);
2034 free_commits(&s->commits);
2035 free(s->in_repo_path);
2036 s->in_repo_path = NULL;
2037 free(s->start_id);
2038 s->start_id = NULL;
2039 return err;
2042 static const struct got_error *
2043 search_start_log_view(struct tog_view *view)
2045 struct tog_log_view_state *s = &view->state.log;
2047 s->matched_entry = NULL;
2048 s->search_entry = NULL;
2049 return NULL;
2052 static const struct got_error *
2053 search_next_log_view(struct tog_view *view)
2055 const struct got_error *err = NULL;
2056 struct tog_log_view_state *s = &view->state.log;
2057 struct commit_queue_entry *entry;
2059 if (s->search_entry) {
2060 int errcode, ch;
2061 errcode = pthread_mutex_unlock(&tog_mutex);
2062 if (errcode)
2063 return got_error_set_errno(errcode,
2064 "pthread_mutex_unlock");
2065 ch = wgetch(view->window);
2066 errcode = pthread_mutex_lock(&tog_mutex);
2067 if (errcode)
2068 return got_error_set_errno(errcode,
2069 "pthread_mutex_lock");
2070 if (ch == KEY_BACKSPACE) {
2071 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2072 return NULL;
2074 if (view->searching == TOG_SEARCH_FORWARD)
2075 entry = TAILQ_NEXT(s->search_entry, entry);
2076 else
2077 entry = TAILQ_PREV(s->search_entry,
2078 commit_queue_head, entry);
2079 } else if (s->matched_entry) {
2080 if (view->searching == TOG_SEARCH_FORWARD)
2081 entry = TAILQ_NEXT(s->matched_entry, entry);
2082 else
2083 entry = TAILQ_PREV(s->matched_entry,
2084 commit_queue_head, entry);
2085 } else {
2086 if (view->searching == TOG_SEARCH_FORWARD)
2087 entry = TAILQ_FIRST(&s->commits.head);
2088 else
2089 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2092 while (1) {
2093 int have_match = 0;
2095 if (entry == NULL) {
2096 if (s->thread_args.log_complete ||
2097 view->searching == TOG_SEARCH_BACKWARD) {
2098 view->search_next_done = TOG_SEARCH_NO_MORE;
2099 s->search_entry = NULL;
2100 return NULL;
2103 * Poke the log thread for more commits and return,
2104 * allowing the main loop to make progress. Search
2105 * will resume at s->search_entry once we come back.
2107 s->thread_args.commits_needed++;
2108 return trigger_log_thread(view, 0,
2109 &s->thread_args.commits_needed,
2110 &s->thread_args.log_complete,
2111 &s->thread_args.need_commits,
2112 &s->thread_args.commit_loaded);
2115 err = match_commit(&have_match, entry->id, entry->commit,
2116 &view->regex);
2117 if (err)
2118 break;
2119 if (have_match) {
2120 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2121 s->matched_entry = entry;
2122 break;
2125 s->search_entry = entry;
2126 if (view->searching == TOG_SEARCH_FORWARD)
2127 entry = TAILQ_NEXT(entry, entry);
2128 else
2129 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2132 if (s->matched_entry) {
2133 int cur = s->selected_entry->idx;
2134 while (cur < s->matched_entry->idx) {
2135 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2136 if (err)
2137 return err;
2138 cur++;
2140 while (cur > s->matched_entry->idx) {
2141 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2142 if (err)
2143 return err;
2144 cur--;
2148 s->search_entry = NULL;
2150 return NULL;
2153 static const struct got_error *
2154 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2155 struct got_reflist_head *refs, struct got_repository *repo,
2156 const char *head_ref_name, const char *in_repo_path,
2157 int log_branches)
2159 const struct got_error *err = NULL;
2160 struct tog_log_view_state *s = &view->state.log;
2161 struct got_repository *thread_repo = NULL;
2162 struct got_commit_graph *thread_graph = NULL;
2163 int errcode;
2165 if (in_repo_path != s->in_repo_path) {
2166 free(s->in_repo_path);
2167 s->in_repo_path = strdup(in_repo_path);
2168 if (s->in_repo_path == NULL)
2169 return got_error_from_errno("strdup");
2172 /* The commit queue only contains commits being displayed. */
2173 TAILQ_INIT(&s->commits.head);
2174 s->commits.ncommits = 0;
2176 s->refs = refs;
2177 s->repo = repo;
2178 s->head_ref_name = head_ref_name;
2179 s->start_id = got_object_id_dup(start_id);
2180 if (s->start_id == NULL) {
2181 err = got_error_from_errno("got_object_id_dup");
2182 goto done;
2184 s->log_branches = log_branches;
2186 SIMPLEQ_INIT(&s->colors);
2187 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2188 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2189 get_color_value("TOG_COLOR_COMMIT"));
2190 if (err)
2191 goto done;
2192 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2193 get_color_value("TOG_COLOR_AUTHOR"));
2194 if (err) {
2195 free_colors(&s->colors);
2196 goto done;
2198 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2199 get_color_value("TOG_COLOR_DATE"));
2200 if (err) {
2201 free_colors(&s->colors);
2202 goto done;
2206 view->show = show_log_view;
2207 view->input = input_log_view;
2208 view->close = close_log_view;
2209 view->search_start = search_start_log_view;
2210 view->search_next = search_next_log_view;
2212 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2213 if (err)
2214 goto done;
2215 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2216 !s->log_branches);
2217 if (err)
2218 goto done;
2219 err = got_commit_graph_iter_start(thread_graph,
2220 s->start_id, s->repo, NULL, NULL);
2221 if (err)
2222 goto done;
2224 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2225 if (errcode) {
2226 err = got_error_set_errno(errcode, "pthread_cond_init");
2227 goto done;
2229 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2230 if (errcode) {
2231 err = got_error_set_errno(errcode, "pthread_cond_init");
2232 goto done;
2235 s->thread_args.commits_needed = view->nlines;
2236 s->thread_args.graph = thread_graph;
2237 s->thread_args.commits = &s->commits;
2238 s->thread_args.in_repo_path = s->in_repo_path;
2239 s->thread_args.start_id = s->start_id;
2240 s->thread_args.repo = thread_repo;
2241 s->thread_args.log_complete = 0;
2242 s->thread_args.quit = &s->quit;
2243 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2244 s->thread_args.selected_entry = &s->selected_entry;
2245 s->thread_args.searching = &view->searching;
2246 s->thread_args.search_next_done = &view->search_next_done;
2247 s->thread_args.regex = &view->regex;
2248 done:
2249 if (err)
2250 close_log_view(view);
2251 return err;
2254 static const struct got_error *
2255 show_log_view(struct tog_view *view)
2257 struct tog_log_view_state *s = &view->state.log;
2259 if (s->thread == NULL) {
2260 int errcode = pthread_create(&s->thread, NULL, log_thread,
2261 &s->thread_args);
2262 if (errcode)
2263 return got_error_set_errno(errcode, "pthread_create");
2266 return draw_commits(view, &s->last_displayed_entry,
2267 &s->selected_entry, s->first_displayed_entry,
2268 &s->commits, s->selected, view->nlines, s->refs,
2269 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2272 static const struct got_error *
2273 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2274 struct tog_view **focus_view, struct tog_view *view, int ch)
2276 const struct got_error *err = NULL;
2277 struct tog_log_view_state *s = &view->state.log;
2278 char *parent_path, *in_repo_path = NULL;
2279 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2280 int begin_x = 0;
2281 struct got_object_id *start_id;
2283 switch (ch) {
2284 case 'q':
2285 s->quit = 1;
2286 break;
2287 case 'k':
2288 case KEY_UP:
2289 case '<':
2290 case ',':
2291 if (s->first_displayed_entry == NULL)
2292 break;
2293 if (s->selected > 0)
2294 s->selected--;
2295 else
2296 scroll_up(view, &s->first_displayed_entry, 1,
2297 &s->commits);
2298 break;
2299 case KEY_PPAGE:
2300 case CTRL('b'):
2301 if (s->first_displayed_entry == NULL)
2302 break;
2303 if (TAILQ_FIRST(&s->commits.head) ==
2304 s->first_displayed_entry) {
2305 s->selected = 0;
2306 break;
2308 scroll_up(view, &s->first_displayed_entry,
2309 view->nlines - 1, &s->commits);
2310 break;
2311 case 'j':
2312 case KEY_DOWN:
2313 case '>':
2314 case '.':
2315 if (s->first_displayed_entry == NULL)
2316 break;
2317 if (s->selected < MIN(view->nlines - 2,
2318 s->commits.ncommits - 1)) {
2319 s->selected++;
2320 break;
2322 err = scroll_down(view, &s->first_displayed_entry, 1,
2323 &s->last_displayed_entry, &s->commits,
2324 &s->thread_args.log_complete,
2325 &s->thread_args.commits_needed,
2326 &s->thread_args.need_commits,
2327 &s->thread_args.commit_loaded);
2328 break;
2329 case KEY_NPAGE:
2330 case CTRL('f'): {
2331 struct commit_queue_entry *first;
2332 first = s->first_displayed_entry;
2333 if (first == NULL)
2334 break;
2335 err = scroll_down(view, &s->first_displayed_entry,
2336 view->nlines - 1, &s->last_displayed_entry,
2337 &s->commits, &s->thread_args.log_complete,
2338 &s->thread_args.commits_needed,
2339 &s->thread_args.need_commits,
2340 &s->thread_args.commit_loaded);
2341 if (err)
2342 break;
2343 if (first == s->first_displayed_entry &&
2344 s->selected < MIN(view->nlines - 2,
2345 s->commits.ncommits - 1)) {
2346 /* can't scroll further down */
2347 s->selected = MIN(view->nlines - 2,
2348 s->commits.ncommits - 1);
2350 err = NULL;
2351 break;
2353 case KEY_RESIZE:
2354 if (s->selected > view->nlines - 2)
2355 s->selected = view->nlines - 2;
2356 if (s->selected > s->commits.ncommits - 1)
2357 s->selected = s->commits.ncommits - 1;
2358 break;
2359 case KEY_ENTER:
2360 case ' ':
2361 case '\r':
2362 if (s->selected_entry == NULL)
2363 break;
2364 if (view_is_parent_view(view))
2365 begin_x = view_split_begin_x(view->begin_x);
2366 err = open_diff_view_for_commit(&diff_view, begin_x,
2367 s->selected_entry->commit, s->selected_entry->id,
2368 view, s->refs, s->repo);
2369 if (err)
2370 break;
2371 if (view_is_parent_view(view)) {
2372 err = view_close_child(view);
2373 if (err)
2374 return err;
2375 err = view_set_child(view, diff_view);
2376 if (err) {
2377 view_close(diff_view);
2378 break;
2380 *focus_view = diff_view;
2381 view->child_focussed = 1;
2382 } else
2383 *new_view = diff_view;
2384 break;
2385 case 't':
2386 if (s->selected_entry == NULL)
2387 break;
2388 if (view_is_parent_view(view))
2389 begin_x = view_split_begin_x(view->begin_x);
2390 err = browse_commit_tree(&tree_view, begin_x,
2391 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2392 if (err)
2393 break;
2394 if (view_is_parent_view(view)) {
2395 err = view_close_child(view);
2396 if (err)
2397 return err;
2398 err = view_set_child(view, tree_view);
2399 if (err) {
2400 view_close(tree_view);
2401 break;
2403 *focus_view = tree_view;
2404 view->child_focussed = 1;
2405 } else
2406 *new_view = tree_view;
2407 break;
2408 case KEY_BACKSPACE:
2409 if (strcmp(s->in_repo_path, "/") == 0)
2410 break;
2411 parent_path = dirname(s->in_repo_path);
2412 if (parent_path && strcmp(parent_path, ".") != 0) {
2413 err = stop_log_thread(s);
2414 if (err)
2415 return err;
2416 lv = view_open(view->nlines, view->ncols,
2417 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2418 if (lv == NULL)
2419 return got_error_from_errno(
2420 "view_open");
2421 err = open_log_view(lv, s->start_id, s->refs,
2422 s->repo, s->head_ref_name, parent_path,
2423 s->log_branches);
2424 if (err)
2425 return err;;
2426 if (view_is_parent_view(view))
2427 *new_view = lv;
2428 else {
2429 view_set_child(view->parent, lv);
2430 *focus_view = lv;
2432 return NULL;
2434 break;
2435 case CTRL('l'):
2436 err = stop_log_thread(s);
2437 if (err)
2438 return err;
2439 lv = view_open(view->nlines, view->ncols,
2440 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2441 if (lv == NULL)
2442 return got_error_from_errno("view_open");
2443 err = got_repo_match_object_id(&start_id, NULL,
2444 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2445 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2446 if (err) {
2447 view_close(lv);
2448 return err;
2450 in_repo_path = strdup(s->in_repo_path);
2451 if (in_repo_path == NULL) {
2452 free(start_id);
2453 view_close(lv);
2454 return got_error_from_errno("strdup");
2456 got_ref_list_free(s->refs);
2457 err = got_ref_list(s->refs, s->repo, NULL,
2458 got_ref_cmp_by_name, NULL);
2459 if (err) {
2460 free(start_id);
2461 view_close(lv);
2462 return err;
2464 err = open_log_view(lv, start_id, s->refs, s->repo,
2465 s->head_ref_name, in_repo_path, s->log_branches);
2466 if (err) {
2467 free(start_id);
2468 view_close(lv);
2469 return err;;
2471 *dead_view = view;
2472 *new_view = lv;
2473 break;
2474 case 'B':
2475 s->log_branches = !s->log_branches;
2476 err = stop_log_thread(s);
2477 if (err)
2478 return err;
2479 lv = view_open(view->nlines, view->ncols,
2480 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2481 if (lv == NULL)
2482 return got_error_from_errno("view_open");
2483 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2484 s->head_ref_name, s->in_repo_path, s->log_branches);
2485 if (err) {
2486 view_close(lv);
2487 return err;;
2489 *dead_view = view;
2490 *new_view = lv;
2491 break;
2492 default:
2493 break;
2496 return err;
2499 static const struct got_error *
2500 apply_unveil(const char *repo_path, const char *worktree_path)
2502 const struct got_error *error;
2504 #ifdef PROFILE
2505 if (unveil("gmon.out", "rwc") != 0)
2506 return got_error_from_errno2("unveil", "gmon.out");
2507 #endif
2508 if (repo_path && unveil(repo_path, "r") != 0)
2509 return got_error_from_errno2("unveil", repo_path);
2511 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2512 return got_error_from_errno2("unveil", worktree_path);
2514 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2515 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2517 error = got_privsep_unveil_exec_helpers();
2518 if (error != NULL)
2519 return error;
2521 if (unveil(NULL, NULL) != 0)
2522 return got_error_from_errno("unveil");
2524 return NULL;
2527 static void
2528 init_curses(void)
2530 initscr();
2531 cbreak();
2532 halfdelay(1); /* Do fast refresh while initial view is loading. */
2533 noecho();
2534 nonl();
2535 intrflush(stdscr, FALSE);
2536 keypad(stdscr, TRUE);
2537 curs_set(0);
2538 if (getenv("TOG_COLORS") != NULL) {
2539 start_color();
2540 use_default_colors();
2542 signal(SIGWINCH, tog_sigwinch);
2543 signal(SIGPIPE, tog_sigpipe);
2544 signal(SIGCONT, tog_sigcont);
2547 static const struct got_error *
2548 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2549 struct got_repository *repo, struct got_worktree *worktree)
2551 const struct got_error *err = NULL;
2553 if (argc == 0) {
2554 *in_repo_path = strdup("/");
2555 if (*in_repo_path == NULL)
2556 return got_error_from_errno("strdup");
2557 return NULL;
2560 if (worktree) {
2561 const char *prefix = got_worktree_get_path_prefix(worktree);
2562 char *wt_path, *p;
2564 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2565 if (err)
2566 return err;
2568 if (asprintf(&p, "%s%s%s", prefix,
2569 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2570 err = got_error_from_errno("asprintf");
2571 free(wt_path);
2572 return err;
2574 err = got_repo_map_path(in_repo_path, repo, p, 0);
2575 free(p);
2576 free(wt_path);
2577 } else
2578 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2580 return err;
2583 static const struct got_error *
2584 cmd_log(int argc, char *argv[])
2586 const struct got_error *error;
2587 struct got_repository *repo = NULL;
2588 struct got_worktree *worktree = NULL;
2589 struct got_reflist_head refs;
2590 struct got_object_id *start_id = NULL;
2591 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2592 char *start_commit = NULL, *head_ref_name = NULL;
2593 int ch, log_branches = 0;
2594 struct tog_view *view;
2596 SIMPLEQ_INIT(&refs);
2598 #ifndef PROFILE
2599 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2600 NULL) == -1)
2601 err(1, "pledge");
2602 #endif
2604 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2605 switch (ch) {
2606 case 'b':
2607 log_branches = 1;
2608 break;
2609 case 'c':
2610 start_commit = optarg;
2611 break;
2612 case 'r':
2613 repo_path = realpath(optarg, NULL);
2614 if (repo_path == NULL)
2615 return got_error_from_errno2("realpath",
2616 optarg);
2617 break;
2618 default:
2619 usage_log();
2620 /* NOTREACHED */
2624 argc -= optind;
2625 argv += optind;
2627 if (argc > 1)
2628 usage_log();
2630 cwd = getcwd(NULL, 0);
2631 if (cwd == NULL)
2632 return got_error_from_errno("getcwd");
2634 error = got_worktree_open(&worktree, cwd);
2635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2636 goto done;
2638 if (repo_path == NULL) {
2639 if (worktree)
2640 repo_path =
2641 strdup(got_worktree_get_repo_path(worktree));
2642 else
2643 repo_path = strdup(cwd);
2645 if (repo_path == NULL) {
2646 error = got_error_from_errno("strdup");
2647 goto done;
2650 error = got_repo_open(&repo, repo_path, NULL);
2651 if (error != NULL)
2652 goto done;
2654 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2655 repo, worktree);
2656 if (error)
2657 goto done;
2659 init_curses();
2661 error = apply_unveil(got_repo_get_path(repo),
2662 worktree ? got_worktree_get_root_path(worktree) : NULL);
2663 if (error)
2664 goto done;
2666 if (start_commit == NULL)
2667 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2668 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2669 GOT_OBJ_TYPE_COMMIT, 1, repo);
2670 else
2671 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2672 GOT_OBJ_TYPE_COMMIT, 1, repo);
2673 if (error != NULL)
2674 goto done;
2676 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2677 if (error)
2678 goto done;
2680 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2681 if (view == NULL) {
2682 error = got_error_from_errno("view_open");
2683 goto done;
2685 if (worktree) {
2686 head_ref_name = strdup(
2687 got_worktree_get_head_ref_name(worktree));
2688 if (head_ref_name == NULL) {
2689 error = got_error_from_errno("strdup");
2690 goto done;
2693 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2694 in_repo_path, log_branches);
2695 if (error)
2696 goto done;
2697 if (worktree) {
2698 /* Release work tree lock. */
2699 got_worktree_close(worktree);
2700 worktree = NULL;
2702 error = view_loop(view);
2703 done:
2704 free(in_repo_path);
2705 free(repo_path);
2706 free(cwd);
2707 free(start_id);
2708 free(head_ref_name);
2709 if (repo)
2710 got_repo_close(repo);
2711 if (worktree)
2712 got_worktree_close(worktree);
2713 got_ref_list_free(&refs);
2714 return error;
2717 __dead static void
2718 usage_diff(void)
2720 endwin();
2721 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2722 getprogname());
2723 exit(1);
2726 static char *
2727 parse_next_line(FILE *f, size_t *len)
2729 char *line;
2730 size_t linelen;
2731 size_t lineno;
2732 const char delim[3] = { '\0', '\0', '\0'};
2734 line = fparseln(f, &linelen, &lineno, delim, 0);
2735 if (len)
2736 *len = linelen;
2737 return line;
2740 static int
2741 match_line(const char *line, regex_t *regex)
2743 regmatch_t regmatch;
2745 return regexec(regex, line, 1, &regmatch, 0) == 0;
2748 struct tog_color *
2749 match_color(struct tog_colors *colors, const char *line)
2751 struct tog_color *tc = NULL;
2753 SIMPLEQ_FOREACH(tc, colors, entry) {
2754 if (match_line(line, &tc->regex))
2755 return tc;
2758 return NULL;
2761 static const struct got_error *
2762 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2763 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2764 char *header, struct tog_colors *colors)
2766 const struct got_error *err;
2767 int lineno = 0, nprinted = 0;
2768 char *line;
2769 struct tog_color *tc;
2770 size_t len;
2771 wchar_t *wline;
2772 int width;
2774 rewind(f);
2775 werase(view->window);
2777 if (header) {
2778 err = format_line(&wline, &width, header, view->ncols, 0);
2779 if (err) {
2780 return err;
2783 if (view_needs_focus_indication(view))
2784 wstandout(view->window);
2785 waddwstr(view->window, wline);
2786 if (view_needs_focus_indication(view))
2787 wstandend(view->window);
2788 if (width <= view->ncols - 1)
2789 waddch(view->window, '\n');
2791 if (max_lines <= 1)
2792 return NULL;
2793 max_lines--;
2796 *eof = 0;
2797 while (nprinted < max_lines) {
2798 line = parse_next_line(f, &len);
2799 if (line == NULL) {
2800 *eof = 1;
2801 break;
2803 if (++lineno < *first_displayed_line) {
2804 free(line);
2805 continue;
2808 err = format_line(&wline, &width, line, view->ncols, 0);
2809 if (err) {
2810 free(line);
2811 return err;
2814 tc = match_color(colors, line);
2815 if (tc)
2816 wattr_on(view->window,
2817 COLOR_PAIR(tc->colorpair), NULL);
2818 waddwstr(view->window, wline);
2819 if (tc)
2820 wattr_off(view->window,
2821 COLOR_PAIR(tc->colorpair), NULL);
2822 if (width <= view->ncols - 1)
2823 waddch(view->window, '\n');
2824 if (++nprinted == 1)
2825 *first_displayed_line = lineno;
2826 free(line);
2827 free(wline);
2828 wline = NULL;
2830 *last_displayed_line = lineno;
2832 view_vborder(view);
2834 if (*eof) {
2835 while (nprinted < view->nlines) {
2836 waddch(view->window, '\n');
2837 nprinted++;
2840 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2841 if (err) {
2842 return err;
2845 wstandout(view->window);
2846 waddwstr(view->window, wline);
2847 wstandend(view->window);
2850 return NULL;
2853 static char *
2854 get_datestr(time_t *time, char *datebuf)
2856 struct tm mytm, *tm;
2857 char *p, *s;
2859 tm = gmtime_r(time, &mytm);
2860 if (tm == NULL)
2861 return NULL;
2862 s = asctime_r(tm, datebuf);
2863 if (s == NULL)
2864 return NULL;
2865 p = strchr(s, '\n');
2866 if (p)
2867 *p = '\0';
2868 return s;
2871 static const struct got_error *
2872 write_commit_info(struct got_object_id *commit_id,
2873 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2875 const struct got_error *err = NULL;
2876 char datebuf[26], *datestr;
2877 struct got_commit_object *commit;
2878 char *id_str = NULL, *logmsg = NULL;
2879 time_t committer_time;
2880 const char *author, *committer;
2881 char *refs_str = NULL;
2883 if (refs) {
2884 err = build_refs_str(&refs_str, refs, commit_id, repo);
2885 if (err)
2886 return err;
2889 err = got_object_open_as_commit(&commit, repo, commit_id);
2890 if (err)
2891 return err;
2893 err = got_object_id_str(&id_str, commit_id);
2894 if (err) {
2895 err = got_error_from_errno("got_object_id_str");
2896 goto done;
2899 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2900 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2901 err = got_error_from_errno("fprintf");
2902 goto done;
2904 if (fprintf(outfile, "from: %s\n",
2905 got_object_commit_get_author(commit)) < 0) {
2906 err = got_error_from_errno("fprintf");
2907 goto done;
2909 committer_time = got_object_commit_get_committer_time(commit);
2910 datestr = get_datestr(&committer_time, datebuf);
2911 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2912 err = got_error_from_errno("fprintf");
2913 goto done;
2915 author = got_object_commit_get_author(commit);
2916 committer = got_object_commit_get_committer(commit);
2917 if (strcmp(author, committer) != 0 &&
2918 fprintf(outfile, "via: %s\n", committer) < 0) {
2919 err = got_error_from_errno("fprintf");
2920 goto done;
2922 err = got_object_commit_get_logmsg(&logmsg, commit);
2923 if (err)
2924 goto done;
2925 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2926 err = got_error_from_errno("fprintf");
2927 goto done;
2929 done:
2930 free(id_str);
2931 free(logmsg);
2932 free(refs_str);
2933 got_object_commit_close(commit);
2934 return err;
2937 const struct got_error *
2938 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2939 FILE *infile)
2941 size_t len;
2942 char *buf = NULL;
2943 int i;
2944 size_t noffsets = 0;
2945 off_t off = 0;
2947 if (line_offsets)
2948 *line_offsets = NULL;
2949 if (filesize)
2950 *filesize = 0;
2951 if (nlines)
2952 *nlines = 0;
2954 if (fseek(infile, 0, SEEK_END) == -1)
2955 return got_error_from_errno("fseek");
2956 len = ftell(infile) + 1;
2957 if (ferror(infile))
2958 return got_error_from_errno("ftell");
2959 if (fseek(infile, 0, SEEK_SET) == -1)
2960 return got_error_from_errno("fseek");
2962 if (len == 0)
2963 return NULL;
2964 if ((buf = calloc(len, sizeof(char *))) == NULL)
2965 return got_error_from_errno("calloc");
2967 fread(buf, 1, len, infile);
2968 if (ferror(infile))
2969 return got_error_from_errno("fread");
2971 i = 0;
2972 if (line_offsets && nlines) {
2973 if (*line_offsets == NULL) {
2974 /* Have some data but perhaps no '\n'. */
2975 noffsets = 1;
2976 *nlines = 1;
2977 *line_offsets = calloc(1, sizeof(**line_offsets));
2978 if (*line_offsets == NULL)
2979 return got_error_from_errno("calloc");
2980 /* Skip forward over end of first line. */
2981 while (i < len) {
2982 if (buf[i] == '\n')
2983 break;
2984 i++;
2987 /* Scan '\n' offsets in remaining chunk of data. */
2988 while (i < len) {
2989 if (buf[i] != '\n') {
2990 i++;
2991 continue;
2993 (*nlines)++;
2994 if (noffsets < *nlines) {
2995 off_t *o = recallocarray(*line_offsets,
2996 noffsets, *nlines,
2997 sizeof(**line_offsets));
2998 if (o == NULL) {
2999 free(*line_offsets);
3000 *line_offsets = NULL;
3001 return got_error_from_errno(
3002 "recallocarray");
3004 *line_offsets = o;
3005 noffsets = *nlines;
3007 off = i + 1;
3008 (*line_offsets)[*nlines - 1] = off;
3009 i++;
3013 if (fflush(infile) != 0)
3014 return got_error_from_errno("fflush");
3015 rewind(infile);
3017 if (filesize)
3018 *filesize = len;
3020 return NULL;
3023 static const struct got_error *
3024 create_diff(struct tog_diff_view_state *s)
3026 const struct got_error *err = NULL;
3027 FILE *f = NULL;
3028 int obj_type;
3030 f = got_opentemp();
3031 if (f == NULL) {
3032 err = got_error_from_errno("got_opentemp");
3033 goto done;
3035 if (s->f && fclose(s->f) != 0) {
3036 err = got_error_from_errno("fclose");
3037 goto done;
3039 s->f = f;
3041 if (s->id1)
3042 err = got_object_get_type(&obj_type, s->repo, s->id1);
3043 else
3044 err = got_object_get_type(&obj_type, s->repo, s->id2);
3045 if (err)
3046 goto done;
3048 switch (obj_type) {
3049 case GOT_OBJ_TYPE_BLOB:
3050 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3051 s->diff_context, 0, s->repo, s->f);
3052 break;
3053 case GOT_OBJ_TYPE_TREE:
3054 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3055 s->diff_context, 0, s->repo, s->f);
3056 break;
3057 case GOT_OBJ_TYPE_COMMIT: {
3058 const struct got_object_id_queue *parent_ids;
3059 struct got_object_qid *pid;
3060 struct got_commit_object *commit2;
3062 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3063 if (err)
3064 goto done;
3065 /* Show commit info if we're diffing to a parent/root commit. */
3066 if (s->id1 == NULL) {
3067 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3068 if (err)
3069 goto done;
3070 } else {
3071 parent_ids = got_object_commit_get_parent_ids(commit2);
3072 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3073 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3074 err = write_commit_info(s->id2, s->refs,
3075 s->repo, s->f);
3076 if (err)
3077 goto done;
3078 break;
3082 got_object_commit_close(commit2);
3084 err = got_diff_objects_as_commits(s->id1, s->id2,
3085 s->diff_context, 0, s->repo, s->f);
3086 break;
3088 default:
3089 err = got_error(GOT_ERR_OBJ_TYPE);
3090 break;
3092 if (err)
3093 goto done;
3094 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3095 s->f);
3096 done:
3097 if (s->f && fflush(s->f) != 0 && err == NULL)
3098 err = got_error_from_errno("fflush");
3099 return err;
3102 static void
3103 diff_view_indicate_progress(struct tog_view *view)
3105 mvwaddstr(view->window, 0, 0, "diffing...");
3106 update_panels();
3107 doupdate();
3110 static const struct got_error *
3111 search_start_diff_view(struct tog_view *view)
3113 struct tog_diff_view_state *s = &view->state.diff;
3115 s->matched_line = 0;
3116 return NULL;
3119 static const struct got_error *
3120 search_next_diff_view(struct tog_view *view)
3122 struct tog_diff_view_state *s = &view->state.diff;
3123 int lineno;
3125 if (!view->searching) {
3126 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3127 return NULL;
3130 if (s->matched_line) {
3131 if (view->searching == TOG_SEARCH_FORWARD)
3132 lineno = s->matched_line + 1;
3133 else
3134 lineno = s->matched_line - 1;
3135 } else {
3136 if (view->searching == TOG_SEARCH_FORWARD)
3137 lineno = 1;
3138 else
3139 lineno = s->nlines;
3142 while (1) {
3143 char *line = NULL;
3144 off_t offset;
3145 size_t len;
3147 if (lineno <= 0 || lineno > s->nlines) {
3148 if (s->matched_line == 0) {
3149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3150 free(line);
3151 break;
3154 if (view->searching == TOG_SEARCH_FORWARD)
3155 lineno = 1;
3156 else
3157 lineno = s->nlines;
3160 offset = s->line_offsets[lineno - 1];
3161 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3162 free(line);
3163 return got_error_from_errno("fseeko");
3165 free(line);
3166 line = parse_next_line(s->f, &len);
3167 if (line && match_line(line, &view->regex)) {
3168 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3169 s->matched_line = lineno;
3170 free(line);
3171 break;
3173 free(line);
3174 if (view->searching == TOG_SEARCH_FORWARD)
3175 lineno++;
3176 else
3177 lineno--;
3180 if (s->matched_line) {
3181 s->first_displayed_line = s->matched_line;
3182 s->selected_line = 1;
3185 return NULL;
3188 static const struct got_error *
3189 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3190 struct got_object_id *id2, struct tog_view *log_view,
3191 struct got_reflist_head *refs, struct got_repository *repo)
3193 const struct got_error *err;
3194 struct tog_diff_view_state *s = &view->state.diff;
3196 if (id1 != NULL && id2 != NULL) {
3197 int type1, type2;
3198 err = got_object_get_type(&type1, repo, id1);
3199 if (err)
3200 return err;
3201 err = got_object_get_type(&type2, repo, id2);
3202 if (err)
3203 return err;
3205 if (type1 != type2)
3206 return got_error(GOT_ERR_OBJ_TYPE);
3208 s->first_displayed_line = 1;
3209 s->last_displayed_line = view->nlines;
3210 s->selected_line = 1;
3211 s->repo = repo;
3212 s->refs = refs;
3213 s->id1 = id1;
3214 s->id2 = id2;
3216 if (id1) {
3217 s->id1 = got_object_id_dup(id1);
3218 if (s->id1 == NULL)
3219 return got_error_from_errno("got_object_id_dup");
3220 } else
3221 s->id1 = NULL;
3223 s->id2 = got_object_id_dup(id2);
3224 if (s->id2 == NULL) {
3225 free(s->id1);
3226 s->id1 = NULL;
3227 return got_error_from_errno("got_object_id_dup");
3229 s->f = NULL;
3230 s->first_displayed_line = 1;
3231 s->last_displayed_line = view->nlines;
3232 s->diff_context = 3;
3233 s->log_view = log_view;
3234 s->repo = repo;
3235 s->refs = refs;
3237 SIMPLEQ_INIT(&s->colors);
3238 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3239 err = add_color(&s->colors,
3240 "^-", TOG_COLOR_DIFF_MINUS,
3241 get_color_value("TOG_COLOR_DIFF_MINUS"));
3242 if (err)
3243 return err;
3244 err = add_color(&s->colors, "^\\+",
3245 TOG_COLOR_DIFF_PLUS,
3246 get_color_value("TOG_COLOR_DIFF_PLUS"));
3247 if (err) {
3248 free_colors(&s->colors);
3249 return err;
3251 err = add_color(&s->colors,
3252 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3253 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3254 if (err) {
3255 free_colors(&s->colors);
3256 return err;
3259 err = add_color(&s->colors,
3260 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3261 get_color_value("TOG_COLOR_DIFF_META"));
3262 if (err) {
3263 free_colors(&s->colors);
3264 return err;
3267 err = add_color(&s->colors,
3268 "^(from|via): ", TOG_COLOR_AUTHOR,
3269 get_color_value("TOG_COLOR_AUTHOR"));
3270 if (err) {
3271 free_colors(&s->colors);
3272 return err;
3275 err = add_color(&s->colors,
3276 "^date: ", TOG_COLOR_DATE,
3277 get_color_value("TOG_COLOR_DATE"));
3278 if (err) {
3279 free_colors(&s->colors);
3280 return err;
3284 if (log_view && view_is_splitscreen(view))
3285 show_log_view(log_view); /* draw vborder */
3286 diff_view_indicate_progress(view);
3288 err = create_diff(s);
3289 if (err) {
3290 free(s->id1);
3291 s->id1 = NULL;
3292 free(s->id2);
3293 s->id2 = NULL;
3294 return err;
3297 view->show = show_diff_view;
3298 view->input = input_diff_view;
3299 view->close = close_diff_view;
3300 view->search_start = search_start_diff_view;
3301 view->search_next = search_next_diff_view;
3303 return NULL;
3306 static const struct got_error *
3307 close_diff_view(struct tog_view *view)
3309 const struct got_error *err = NULL;
3310 struct tog_diff_view_state *s = &view->state.diff;
3312 free(s->id1);
3313 s->id1 = NULL;
3314 free(s->id2);
3315 s->id2 = NULL;
3316 if (s->f && fclose(s->f) == EOF)
3317 err = got_error_from_errno("fclose");
3318 free_colors(&s->colors);
3319 free(s->line_offsets);
3320 return err;
3323 static const struct got_error *
3324 show_diff_view(struct tog_view *view)
3326 const struct got_error *err;
3327 struct tog_diff_view_state *s = &view->state.diff;
3328 char *id_str1 = NULL, *id_str2, *header;
3330 if (s->id1) {
3331 err = got_object_id_str(&id_str1, s->id1);
3332 if (err)
3333 return err;
3335 err = got_object_id_str(&id_str2, s->id2);
3336 if (err)
3337 return err;
3339 if (asprintf(&header, "diff %s %s",
3340 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3341 err = got_error_from_errno("asprintf");
3342 free(id_str1);
3343 free(id_str2);
3344 return err;
3346 free(id_str1);
3347 free(id_str2);
3349 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3350 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3351 header, &s->colors);
3354 static const struct got_error *
3355 set_selected_commit(struct tog_diff_view_state *s,
3356 struct commit_queue_entry *entry)
3358 const struct got_error *err;
3359 const struct got_object_id_queue *parent_ids;
3360 struct got_commit_object *selected_commit;
3361 struct got_object_qid *pid;
3363 free(s->id2);
3364 s->id2 = got_object_id_dup(entry->id);
3365 if (s->id2 == NULL)
3366 return got_error_from_errno("got_object_id_dup");
3368 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3369 if (err)
3370 return err;
3371 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3372 free(s->id1);
3373 pid = SIMPLEQ_FIRST(parent_ids);
3374 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3375 got_object_commit_close(selected_commit);
3376 return NULL;
3379 static const struct got_error *
3380 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3381 struct tog_view **focus_view, struct tog_view *view, int ch)
3383 const struct got_error *err = NULL;
3384 struct tog_diff_view_state *s = &view->state.diff;
3385 struct tog_log_view_state *ls;
3386 struct commit_queue_entry *entry;
3387 int i;
3389 switch (ch) {
3390 case 'k':
3391 case KEY_UP:
3392 if (s->first_displayed_line > 1)
3393 s->first_displayed_line--;
3394 break;
3395 case KEY_PPAGE:
3396 case CTRL('b'):
3397 if (s->first_displayed_line == 1)
3398 break;
3399 i = 0;
3400 while (i++ < view->nlines - 1 &&
3401 s->first_displayed_line > 1)
3402 s->first_displayed_line--;
3403 break;
3404 case 'j':
3405 case KEY_DOWN:
3406 if (!s->eof)
3407 s->first_displayed_line++;
3408 break;
3409 case KEY_NPAGE:
3410 case CTRL('f'):
3411 case ' ':
3412 if (s->eof)
3413 break;
3414 i = 0;
3415 while (!s->eof && i++ < view->nlines - 1) {
3416 char *line;
3417 line = parse_next_line(s->f, NULL);
3418 s->first_displayed_line++;
3419 if (line == NULL)
3420 break;
3422 break;
3423 case '[':
3424 if (s->diff_context > 0) {
3425 s->diff_context--;
3426 diff_view_indicate_progress(view);
3427 err = create_diff(s);
3429 break;
3430 case ']':
3431 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3432 s->diff_context++;
3433 diff_view_indicate_progress(view);
3434 err = create_diff(s);
3436 break;
3437 case '<':
3438 case ',':
3439 if (s->log_view == NULL)
3440 break;
3441 ls = &s->log_view->state.log;
3442 entry = TAILQ_PREV(ls->selected_entry,
3443 commit_queue_head, entry);
3444 if (entry == NULL)
3445 break;
3447 err = input_log_view(NULL, NULL, NULL, s->log_view,
3448 KEY_UP);
3449 if (err)
3450 break;
3452 err = set_selected_commit(s, entry);
3453 if (err)
3454 break;
3456 s->first_displayed_line = 1;
3457 s->last_displayed_line = view->nlines;
3459 diff_view_indicate_progress(view);
3460 err = create_diff(s);
3461 break;
3462 case '>':
3463 case '.':
3464 if (s->log_view == NULL)
3465 break;
3466 ls = &s->log_view->state.log;
3468 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3469 ls->thread_args.commits_needed++;
3470 err = trigger_log_thread(s->log_view, 1,
3471 &ls->thread_args.commits_needed,
3472 &ls->thread_args.log_complete,
3473 &ls->thread_args.need_commits,
3474 &ls->thread_args.commit_loaded);
3475 if (err)
3476 break;
3478 err = input_log_view(NULL, NULL, NULL, s->log_view,
3479 KEY_DOWN);
3480 if (err)
3481 break;
3483 entry = TAILQ_NEXT(ls->selected_entry, entry);
3484 if (entry == NULL)
3485 break;
3487 err = set_selected_commit(s, entry);
3488 if (err)
3489 break;
3491 s->first_displayed_line = 1;
3492 s->last_displayed_line = view->nlines;
3494 diff_view_indicate_progress(view);
3495 err = create_diff(s);
3496 break;
3497 default:
3498 break;
3501 return err;
3504 static const struct got_error *
3505 cmd_diff(int argc, char *argv[])
3507 const struct got_error *error = NULL;
3508 struct got_repository *repo = NULL;
3509 struct got_worktree *worktree = NULL;
3510 struct got_reflist_head refs;
3511 struct got_object_id *id1 = NULL, *id2 = NULL;
3512 char *repo_path = NULL, *cwd = NULL;
3513 char *id_str1 = NULL, *id_str2 = NULL;
3514 int ch;
3515 struct tog_view *view;
3517 SIMPLEQ_INIT(&refs);
3519 #ifndef PROFILE
3520 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3521 NULL) == -1)
3522 err(1, "pledge");
3523 #endif
3525 while ((ch = getopt(argc, argv, "r:")) != -1) {
3526 switch (ch) {
3527 case 'r':
3528 repo_path = realpath(optarg, NULL);
3529 if (repo_path == NULL)
3530 return got_error_from_errno2("realpath",
3531 optarg);
3532 break;
3533 default:
3534 usage_diff();
3535 /* NOTREACHED */
3539 argc -= optind;
3540 argv += optind;
3542 if (argc == 0) {
3543 usage_diff(); /* TODO show local worktree changes */
3544 } else if (argc == 2) {
3545 id_str1 = argv[0];
3546 id_str2 = argv[1];
3547 } else
3548 usage_diff();
3550 cwd = getcwd(NULL, 0);
3551 if (cwd == NULL)
3552 return got_error_from_errno("getcwd");
3554 error = got_worktree_open(&worktree, cwd);
3555 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3556 goto done;
3558 if (repo_path == NULL) {
3559 if (worktree)
3560 repo_path =
3561 strdup(got_worktree_get_repo_path(worktree));
3562 else
3563 repo_path = strdup(cwd);
3565 if (repo_path == NULL) {
3566 error = got_error_from_errno("strdup");
3567 goto done;
3570 error = got_repo_open(&repo, repo_path, NULL);
3571 if (error)
3572 goto done;
3574 init_curses();
3576 error = apply_unveil(got_repo_get_path(repo), NULL);
3577 if (error)
3578 goto done;
3580 error = got_repo_match_object_id_prefix(&id1, id_str1,
3581 GOT_OBJ_TYPE_ANY, repo);
3582 if (error)
3583 goto done;
3585 error = got_repo_match_object_id_prefix(&id2, id_str2,
3586 GOT_OBJ_TYPE_ANY, repo);
3587 if (error)
3588 goto done;
3590 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3591 if (error)
3592 goto done;
3594 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3595 if (view == NULL) {
3596 error = got_error_from_errno("view_open");
3597 goto done;
3599 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3600 if (error)
3601 goto done;
3602 error = view_loop(view);
3603 done:
3604 free(repo_path);
3605 free(cwd);
3606 if (repo)
3607 got_repo_close(repo);
3608 if (worktree)
3609 got_worktree_close(worktree);
3610 got_ref_list_free(&refs);
3611 return error;
3614 __dead static void
3615 usage_blame(void)
3617 endwin();
3618 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3619 getprogname());
3620 exit(1);
3623 struct tog_blame_line {
3624 int annotated;
3625 struct got_object_id *id;
3628 static const struct got_error *
3629 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3630 const char *path, struct tog_blame_line *lines, int nlines,
3631 int blame_complete, int selected_line, int *first_displayed_line,
3632 int *last_displayed_line, int *eof, int max_lines,
3633 struct tog_colors *colors)
3635 const struct got_error *err;
3636 int lineno = 0, nprinted = 0;
3637 char *line;
3638 size_t len;
3639 wchar_t *wline;
3640 int width;
3641 struct tog_blame_line *blame_line;
3642 struct got_object_id *prev_id = NULL;
3643 char *id_str;
3644 struct tog_color *tc;
3646 err = got_object_id_str(&id_str, id);
3647 if (err)
3648 return err;
3650 rewind(f);
3651 werase(view->window);
3653 if (asprintf(&line, "commit %s", id_str) == -1) {
3654 err = got_error_from_errno("asprintf");
3655 free(id_str);
3656 return err;
3659 err = format_line(&wline, &width, line, view->ncols, 0);
3660 free(line);
3661 line = NULL;
3662 if (err)
3663 return err;
3664 if (view_needs_focus_indication(view))
3665 wstandout(view->window);
3666 tc = get_color(colors, TOG_COLOR_COMMIT);
3667 if (tc)
3668 wattr_on(view->window,
3669 COLOR_PAIR(tc->colorpair), NULL);
3670 waddwstr(view->window, wline);
3671 if (tc)
3672 wattr_off(view->window,
3673 COLOR_PAIR(tc->colorpair), NULL);
3674 if (view_needs_focus_indication(view))
3675 wstandend(view->window);
3676 free(wline);
3677 wline = NULL;
3678 if (width < view->ncols - 1)
3679 waddch(view->window, '\n');
3681 if (asprintf(&line, "[%d/%d] %s%s",
3682 *first_displayed_line - 1 + selected_line, nlines,
3683 blame_complete ? "" : "annotating... ", path) == -1) {
3684 free(id_str);
3685 return got_error_from_errno("asprintf");
3687 free(id_str);
3688 err = format_line(&wline, &width, line, view->ncols, 0);
3689 free(line);
3690 line = NULL;
3691 if (err)
3692 return err;
3693 waddwstr(view->window, wline);
3694 free(wline);
3695 wline = NULL;
3696 if (width < view->ncols - 1)
3697 waddch(view->window, '\n');
3699 *eof = 0;
3700 while (nprinted < max_lines - 2) {
3701 line = parse_next_line(f, &len);
3702 if (line == NULL) {
3703 *eof = 1;
3704 break;
3706 if (++lineno < *first_displayed_line) {
3707 free(line);
3708 continue;
3711 if (view->ncols <= 9) {
3712 width = 9;
3713 wline = wcsdup(L"");
3714 if (wline == NULL)
3715 err = got_error_from_errno("wcsdup");
3716 } else {
3717 err = format_line(&wline, &width, line,
3718 view->ncols - 9, 9);
3719 width += 9;
3721 if (err) {
3722 free(line);
3723 return err;
3726 if (view->focussed && nprinted == selected_line - 1)
3727 wstandout(view->window);
3729 if (nlines > 0) {
3730 blame_line = &lines[lineno - 1];
3731 if (blame_line->annotated && prev_id &&
3732 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3733 !(view->focussed &&
3734 nprinted == selected_line - 1)) {
3735 waddstr(view->window, " ");
3736 } else if (blame_line->annotated) {
3737 char *id_str;
3738 err = got_object_id_str(&id_str, blame_line->id);
3739 if (err) {
3740 free(line);
3741 free(wline);
3742 return err;
3744 tc = get_color(colors, TOG_COLOR_COMMIT);
3745 if (tc)
3746 wattr_on(view->window,
3747 COLOR_PAIR(tc->colorpair), NULL);
3748 wprintw(view->window, "%.8s", id_str);
3749 if (tc)
3750 wattr_off(view->window,
3751 COLOR_PAIR(tc->colorpair), NULL);
3752 free(id_str);
3753 prev_id = blame_line->id;
3754 } else {
3755 waddstr(view->window, "........");
3756 prev_id = NULL;
3758 } else {
3759 waddstr(view->window, "........");
3760 prev_id = NULL;
3763 if (view->focussed && nprinted == selected_line - 1)
3764 wstandend(view->window);
3765 waddstr(view->window, " ");
3767 waddwstr(view->window, wline);
3768 if (width <= view->ncols - 1)
3769 waddch(view->window, '\n');
3770 if (++nprinted == 1)
3771 *first_displayed_line = lineno;
3772 free(line);
3773 free(wline);
3774 wline = NULL;
3776 *last_displayed_line = lineno;
3778 view_vborder(view);
3780 return NULL;
3783 static const struct got_error *
3784 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3786 const struct got_error *err = NULL;
3787 struct tog_blame_cb_args *a = arg;
3788 struct tog_blame_line *line;
3789 int errcode;
3791 if (nlines != a->nlines ||
3792 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3793 return got_error(GOT_ERR_RANGE);
3795 errcode = pthread_mutex_lock(&tog_mutex);
3796 if (errcode)
3797 return got_error_set_errno(errcode, "pthread_mutex_lock");
3799 if (*a->quit) { /* user has quit the blame view */
3800 err = got_error(GOT_ERR_ITER_COMPLETED);
3801 goto done;
3804 if (lineno == -1)
3805 goto done; /* no change in this commit */
3807 line = &a->lines[lineno - 1];
3808 if (line->annotated)
3809 goto done;
3811 line->id = got_object_id_dup(id);
3812 if (line->id == NULL) {
3813 err = got_error_from_errno("got_object_id_dup");
3814 goto done;
3816 line->annotated = 1;
3817 done:
3818 errcode = pthread_mutex_unlock(&tog_mutex);
3819 if (errcode)
3820 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3821 return err;
3824 static void *
3825 blame_thread(void *arg)
3827 const struct got_error *err;
3828 struct tog_blame_thread_args *ta = arg;
3829 struct tog_blame_cb_args *a = ta->cb_args;
3830 int errcode;
3832 err = block_signals_used_by_main_thread();
3833 if (err)
3834 return (void *)err;
3836 err = got_blame(ta->path, a->commit_id, ta->repo,
3837 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3838 if (err && err->code == GOT_ERR_CANCELLED)
3839 err = NULL;
3841 errcode = pthread_mutex_lock(&tog_mutex);
3842 if (errcode)
3843 return (void *)got_error_set_errno(errcode,
3844 "pthread_mutex_lock");
3846 got_repo_close(ta->repo);
3847 ta->repo = NULL;
3848 *ta->complete = 1;
3850 errcode = pthread_mutex_unlock(&tog_mutex);
3851 if (errcode && err == NULL)
3852 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3854 return (void *)err;
3857 static struct got_object_id *
3858 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3859 int first_displayed_line, int selected_line)
3861 struct tog_blame_line *line;
3863 if (nlines <= 0)
3864 return NULL;
3866 line = &lines[first_displayed_line - 1 + selected_line - 1];
3867 if (!line->annotated)
3868 return NULL;
3870 return line->id;
3873 static const struct got_error *
3874 stop_blame(struct tog_blame *blame)
3876 const struct got_error *err = NULL;
3877 int i;
3879 if (blame->thread) {
3880 int errcode;
3881 errcode = pthread_mutex_unlock(&tog_mutex);
3882 if (errcode)
3883 return got_error_set_errno(errcode,
3884 "pthread_mutex_unlock");
3885 errcode = pthread_join(blame->thread, (void **)&err);
3886 if (errcode)
3887 return got_error_set_errno(errcode, "pthread_join");
3888 errcode = pthread_mutex_lock(&tog_mutex);
3889 if (errcode)
3890 return got_error_set_errno(errcode,
3891 "pthread_mutex_lock");
3892 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3893 err = NULL;
3894 blame->thread = NULL;
3896 if (blame->thread_args.repo) {
3897 got_repo_close(blame->thread_args.repo);
3898 blame->thread_args.repo = NULL;
3900 if (blame->f) {
3901 if (fclose(blame->f) != 0 && err == NULL)
3902 err = got_error_from_errno("fclose");
3903 blame->f = NULL;
3905 if (blame->lines) {
3906 for (i = 0; i < blame->nlines; i++)
3907 free(blame->lines[i].id);
3908 free(blame->lines);
3909 blame->lines = NULL;
3911 free(blame->cb_args.commit_id);
3912 blame->cb_args.commit_id = NULL;
3914 return err;
3917 static const struct got_error *
3918 cancel_blame_view(void *arg)
3920 const struct got_error *err = NULL;
3921 int *done = arg;
3922 int errcode;
3924 errcode = pthread_mutex_lock(&tog_mutex);
3925 if (errcode)
3926 return got_error_set_errno(errcode,
3927 "pthread_mutex_unlock");
3929 if (*done)
3930 err = got_error(GOT_ERR_CANCELLED);
3932 errcode = pthread_mutex_unlock(&tog_mutex);
3933 if (errcode)
3934 return got_error_set_errno(errcode,
3935 "pthread_mutex_lock");
3937 return err;
3940 static const struct got_error *
3941 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3942 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3943 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3944 struct got_repository *repo)
3946 const struct got_error *err = NULL;
3947 struct got_blob_object *blob = NULL;
3948 struct got_repository *thread_repo = NULL;
3949 struct got_object_id *obj_id = NULL;
3950 int obj_type;
3952 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3953 if (err)
3954 return err;
3956 err = got_object_get_type(&obj_type, repo, obj_id);
3957 if (err)
3958 goto done;
3960 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3961 err = got_error(GOT_ERR_OBJ_TYPE);
3962 goto done;
3965 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3966 if (err)
3967 goto done;
3968 blame->f = got_opentemp();
3969 if (blame->f == NULL) {
3970 err = got_error_from_errno("got_opentemp");
3971 goto done;
3973 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3974 &blame->line_offsets, blame->f, blob);
3975 if (err || blame->nlines == 0)
3976 goto done;
3978 /* Don't include \n at EOF in the blame line count. */
3979 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3980 blame->nlines--;
3982 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3983 if (blame->lines == NULL) {
3984 err = got_error_from_errno("calloc");
3985 goto done;
3988 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3989 if (err)
3990 goto done;
3992 blame->cb_args.view = view;
3993 blame->cb_args.lines = blame->lines;
3994 blame->cb_args.nlines = blame->nlines;
3995 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3996 if (blame->cb_args.commit_id == NULL) {
3997 err = got_error_from_errno("got_object_id_dup");
3998 goto done;
4000 blame->cb_args.quit = done;
4002 blame->thread_args.path = path;
4003 blame->thread_args.repo = thread_repo;
4004 blame->thread_args.cb_args = &blame->cb_args;
4005 blame->thread_args.complete = blame_complete;
4006 blame->thread_args.cancel_cb = cancel_blame_view;
4007 blame->thread_args.cancel_arg = done;
4008 *blame_complete = 0;
4010 done:
4011 if (blob)
4012 got_object_blob_close(blob);
4013 free(obj_id);
4014 if (err)
4015 stop_blame(blame);
4016 return err;
4019 static const struct got_error *
4020 open_blame_view(struct tog_view *view, char *path,
4021 struct got_object_id *commit_id, struct got_reflist_head *refs,
4022 struct got_repository *repo)
4024 const struct got_error *err = NULL;
4025 struct tog_blame_view_state *s = &view->state.blame;
4027 SIMPLEQ_INIT(&s->blamed_commits);
4029 s->path = strdup(path);
4030 if (s->path == NULL)
4031 return got_error_from_errno("strdup");
4033 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4034 if (err) {
4035 free(s->path);
4036 return err;
4039 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4040 s->first_displayed_line = 1;
4041 s->last_displayed_line = view->nlines;
4042 s->selected_line = 1;
4043 s->blame_complete = 0;
4044 s->repo = repo;
4045 s->refs = refs;
4046 s->commit_id = commit_id;
4047 memset(&s->blame, 0, sizeof(s->blame));
4049 SIMPLEQ_INIT(&s->colors);
4050 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4051 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4052 get_color_value("TOG_COLOR_COMMIT"));
4053 if (err)
4054 return err;
4057 view->show = show_blame_view;
4058 view->input = input_blame_view;
4059 view->close = close_blame_view;
4060 view->search_start = search_start_blame_view;
4061 view->search_next = search_next_blame_view;
4063 return run_blame(&s->blame, view, &s->blame_complete,
4064 &s->first_displayed_line, &s->last_displayed_line,
4065 &s->selected_line, &s->done, &s->eof, s->path,
4066 s->blamed_commit->id, s->repo);
4069 static const struct got_error *
4070 close_blame_view(struct tog_view *view)
4072 const struct got_error *err = NULL;
4073 struct tog_blame_view_state *s = &view->state.blame;
4075 if (s->blame.thread)
4076 err = stop_blame(&s->blame);
4078 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4079 struct got_object_qid *blamed_commit;
4080 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4081 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4082 got_object_qid_free(blamed_commit);
4085 free(s->path);
4086 free_colors(&s->colors);
4088 return err;
4091 static const struct got_error *
4092 search_start_blame_view(struct tog_view *view)
4094 struct tog_blame_view_state *s = &view->state.blame;
4096 s->matched_line = 0;
4097 return NULL;
4100 static const struct got_error *
4101 search_next_blame_view(struct tog_view *view)
4103 struct tog_blame_view_state *s = &view->state.blame;
4104 int lineno;
4106 if (!view->searching) {
4107 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4108 return NULL;
4111 if (s->matched_line) {
4112 if (view->searching == TOG_SEARCH_FORWARD)
4113 lineno = s->matched_line + 1;
4114 else
4115 lineno = s->matched_line - 1;
4116 } else {
4117 if (view->searching == TOG_SEARCH_FORWARD)
4118 lineno = 1;
4119 else
4120 lineno = s->blame.nlines;
4123 while (1) {
4124 char *line = NULL;
4125 off_t offset;
4126 size_t len;
4128 if (lineno <= 0 || lineno > s->blame.nlines) {
4129 if (s->matched_line == 0) {
4130 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4131 free(line);
4132 break;
4135 if (view->searching == TOG_SEARCH_FORWARD)
4136 lineno = 1;
4137 else
4138 lineno = s->blame.nlines;
4141 offset = s->blame.line_offsets[lineno - 1];
4142 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4143 free(line);
4144 return got_error_from_errno("fseeko");
4146 free(line);
4147 line = parse_next_line(s->blame.f, &len);
4148 if (line && match_line(line, &view->regex)) {
4149 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4150 s->matched_line = lineno;
4151 free(line);
4152 break;
4154 free(line);
4155 if (view->searching == TOG_SEARCH_FORWARD)
4156 lineno++;
4157 else
4158 lineno--;
4161 if (s->matched_line) {
4162 s->first_displayed_line = s->matched_line;
4163 s->selected_line = 1;
4166 return NULL;
4169 static const struct got_error *
4170 show_blame_view(struct tog_view *view)
4172 const struct got_error *err = NULL;
4173 struct tog_blame_view_state *s = &view->state.blame;
4174 int errcode;
4176 if (s->blame.thread == NULL) {
4177 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4178 &s->blame.thread_args);
4179 if (errcode)
4180 return got_error_set_errno(errcode, "pthread_create");
4182 halfdelay(1); /* fast refresh while annotating */
4185 if (s->blame_complete)
4186 halfdelay(10); /* disable fast refresh */
4188 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4189 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4190 s->selected_line, &s->first_displayed_line,
4191 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4193 view_vborder(view);
4194 return err;
4197 static const struct got_error *
4198 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4199 struct tog_view **focus_view, struct tog_view *view, int ch)
4201 const struct got_error *err = NULL, *thread_err = NULL;
4202 struct tog_view *diff_view;
4203 struct tog_blame_view_state *s = &view->state.blame;
4204 int begin_x = 0;
4206 switch (ch) {
4207 case 'q':
4208 s->done = 1;
4209 break;
4210 case 'k':
4211 case KEY_UP:
4212 if (s->selected_line > 1)
4213 s->selected_line--;
4214 else if (s->selected_line == 1 &&
4215 s->first_displayed_line > 1)
4216 s->first_displayed_line--;
4217 break;
4218 case KEY_PPAGE:
4219 case CTRL('b'):
4220 if (s->first_displayed_line == 1) {
4221 s->selected_line = 1;
4222 break;
4224 if (s->first_displayed_line > view->nlines - 2)
4225 s->first_displayed_line -=
4226 (view->nlines - 2);
4227 else
4228 s->first_displayed_line = 1;
4229 break;
4230 case 'j':
4231 case KEY_DOWN:
4232 if (s->selected_line < view->nlines - 2 &&
4233 s->first_displayed_line +
4234 s->selected_line <= s->blame.nlines)
4235 s->selected_line++;
4236 else if (s->last_displayed_line <
4237 s->blame.nlines)
4238 s->first_displayed_line++;
4239 break;
4240 case 'b':
4241 case 'p': {
4242 struct got_object_id *id = NULL;
4243 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4244 s->first_displayed_line, s->selected_line);
4245 if (id == NULL)
4246 break;
4247 if (ch == 'p') {
4248 struct got_commit_object *commit;
4249 struct got_object_qid *pid;
4250 struct got_object_id *blob_id = NULL;
4251 int obj_type;
4252 err = got_object_open_as_commit(&commit,
4253 s->repo, id);
4254 if (err)
4255 break;
4256 pid = SIMPLEQ_FIRST(
4257 got_object_commit_get_parent_ids(commit));
4258 if (pid == NULL) {
4259 got_object_commit_close(commit);
4260 break;
4262 /* Check if path history ends here. */
4263 err = got_object_id_by_path(&blob_id, s->repo,
4264 pid->id, s->path);
4265 if (err) {
4266 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4267 err = NULL;
4268 got_object_commit_close(commit);
4269 break;
4271 err = got_object_get_type(&obj_type, s->repo,
4272 blob_id);
4273 free(blob_id);
4274 /* Can't blame non-blob type objects. */
4275 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4276 got_object_commit_close(commit);
4277 break;
4279 err = got_object_qid_alloc(&s->blamed_commit,
4280 pid->id);
4281 got_object_commit_close(commit);
4282 } else {
4283 if (got_object_id_cmp(id,
4284 s->blamed_commit->id) == 0)
4285 break;
4286 err = got_object_qid_alloc(&s->blamed_commit,
4287 id);
4289 if (err)
4290 break;
4291 s->done = 1;
4292 thread_err = stop_blame(&s->blame);
4293 s->done = 0;
4294 if (thread_err)
4295 break;
4296 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4297 s->blamed_commit, entry);
4298 err = run_blame(&s->blame, view, &s->blame_complete,
4299 &s->first_displayed_line, &s->last_displayed_line,
4300 &s->selected_line, &s->done, &s->eof,
4301 s->path, s->blamed_commit->id, s->repo);
4302 if (err)
4303 break;
4304 break;
4306 case 'B': {
4307 struct got_object_qid *first;
4308 first = SIMPLEQ_FIRST(&s->blamed_commits);
4309 if (!got_object_id_cmp(first->id, s->commit_id))
4310 break;
4311 s->done = 1;
4312 thread_err = stop_blame(&s->blame);
4313 s->done = 0;
4314 if (thread_err)
4315 break;
4316 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4317 got_object_qid_free(s->blamed_commit);
4318 s->blamed_commit =
4319 SIMPLEQ_FIRST(&s->blamed_commits);
4320 err = run_blame(&s->blame, view, &s->blame_complete,
4321 &s->first_displayed_line, &s->last_displayed_line,
4322 &s->selected_line, &s->done, &s->eof, s->path,
4323 s->blamed_commit->id, s->repo);
4324 if (err)
4325 break;
4326 break;
4328 case KEY_ENTER:
4329 case '\r': {
4330 struct got_object_id *id = NULL;
4331 struct got_object_qid *pid;
4332 struct got_commit_object *commit = NULL;
4333 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4334 s->first_displayed_line, s->selected_line);
4335 if (id == NULL)
4336 break;
4337 err = got_object_open_as_commit(&commit, s->repo, id);
4338 if (err)
4339 break;
4340 pid = SIMPLEQ_FIRST(
4341 got_object_commit_get_parent_ids(commit));
4342 if (view_is_parent_view(view))
4343 begin_x = view_split_begin_x(view->begin_x);
4344 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4345 if (diff_view == NULL) {
4346 got_object_commit_close(commit);
4347 err = got_error_from_errno("view_open");
4348 break;
4350 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4351 id, NULL, s->refs, s->repo);
4352 got_object_commit_close(commit);
4353 if (err) {
4354 view_close(diff_view);
4355 break;
4357 if (view_is_parent_view(view)) {
4358 err = view_close_child(view);
4359 if (err)
4360 break;
4361 err = view_set_child(view, diff_view);
4362 if (err) {
4363 view_close(diff_view);
4364 break;
4366 *focus_view = diff_view;
4367 view->child_focussed = 1;
4368 } else
4369 *new_view = diff_view;
4370 if (err)
4371 break;
4372 break;
4374 case KEY_NPAGE:
4375 case CTRL('f'):
4376 case ' ':
4377 if (s->last_displayed_line >= s->blame.nlines &&
4378 s->selected_line >= MIN(s->blame.nlines,
4379 view->nlines - 2)) {
4380 break;
4382 if (s->last_displayed_line >= s->blame.nlines &&
4383 s->selected_line < view->nlines - 2) {
4384 s->selected_line = MIN(s->blame.nlines,
4385 view->nlines - 2);
4386 break;
4388 if (s->last_displayed_line + view->nlines - 2
4389 <= s->blame.nlines)
4390 s->first_displayed_line +=
4391 view->nlines - 2;
4392 else
4393 s->first_displayed_line =
4394 s->blame.nlines -
4395 (view->nlines - 3);
4396 break;
4397 case KEY_RESIZE:
4398 if (s->selected_line > view->nlines - 2) {
4399 s->selected_line = MIN(s->blame.nlines,
4400 view->nlines - 2);
4402 break;
4403 default:
4404 break;
4406 return thread_err ? thread_err : err;
4409 static const struct got_error *
4410 cmd_blame(int argc, char *argv[])
4412 const struct got_error *error;
4413 struct got_repository *repo = NULL;
4414 struct got_reflist_head refs;
4415 struct got_worktree *worktree = NULL;
4416 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4417 struct got_object_id *commit_id = NULL;
4418 char *commit_id_str = NULL;
4419 int ch;
4420 struct tog_view *view;
4422 SIMPLEQ_INIT(&refs);
4424 #ifndef PROFILE
4425 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4426 NULL) == -1)
4427 err(1, "pledge");
4428 #endif
4430 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4431 switch (ch) {
4432 case 'c':
4433 commit_id_str = optarg;
4434 break;
4435 case 'r':
4436 repo_path = realpath(optarg, NULL);
4437 if (repo_path == NULL)
4438 return got_error_from_errno2("realpath",
4439 optarg);
4440 break;
4441 default:
4442 usage_blame();
4443 /* NOTREACHED */
4447 argc -= optind;
4448 argv += optind;
4450 if (argc != 1)
4451 usage_blame();
4453 cwd = getcwd(NULL, 0);
4454 if (cwd == NULL)
4455 return got_error_from_errno("getcwd");
4457 error = got_worktree_open(&worktree, cwd);
4458 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4459 goto done;
4461 if (repo_path == NULL) {
4462 if (worktree)
4463 repo_path =
4464 strdup(got_worktree_get_repo_path(worktree));
4465 else
4466 repo_path = strdup(cwd);
4468 if (repo_path == NULL) {
4469 error = got_error_from_errno("strdup");
4470 goto done;
4473 error = got_repo_open(&repo, repo_path, NULL);
4474 if (error != NULL)
4475 goto done;
4477 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4478 worktree);
4479 if (error)
4480 goto done;
4482 init_curses();
4484 error = apply_unveil(got_repo_get_path(repo), NULL);
4485 if (error)
4486 goto done;
4488 if (commit_id_str == NULL) {
4489 struct got_reference *head_ref;
4490 error = got_ref_open(&head_ref, repo, worktree ?
4491 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4492 if (error != NULL)
4493 goto done;
4494 error = got_ref_resolve(&commit_id, repo, head_ref);
4495 got_ref_close(head_ref);
4496 } else {
4497 error = got_repo_match_object_id(&commit_id, NULL,
4498 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4500 if (error != NULL)
4501 goto done;
4503 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4504 if (error)
4505 goto done;
4507 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4508 if (view == NULL) {
4509 error = got_error_from_errno("view_open");
4510 goto done;
4512 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4513 if (error)
4514 goto done;
4515 if (worktree) {
4516 /* Release work tree lock. */
4517 got_worktree_close(worktree);
4518 worktree = NULL;
4520 error = view_loop(view);
4521 done:
4522 free(repo_path);
4523 free(in_repo_path);
4524 free(cwd);
4525 free(commit_id);
4526 if (worktree)
4527 got_worktree_close(worktree);
4528 if (repo)
4529 got_repo_close(repo);
4530 got_ref_list_free(&refs);
4531 return error;
4534 static const struct got_error *
4535 draw_tree_entries(struct tog_view *view,
4536 struct got_tree_entry **first_displayed_entry,
4537 struct got_tree_entry **last_displayed_entry,
4538 struct got_tree_entry **selected_entry, int *ndisplayed,
4539 const char *label, int show_ids, const char *parent_path,
4540 struct got_tree_object *tree, int selected, int limit,
4541 int isroot, struct tog_colors *colors)
4543 const struct got_error *err = NULL;
4544 struct got_tree_entry *te;
4545 wchar_t *wline;
4546 struct tog_color *tc;
4547 int width, n, i, nentries;
4549 *ndisplayed = 0;
4551 werase(view->window);
4553 if (limit == 0)
4554 return NULL;
4556 err = format_line(&wline, &width, label, view->ncols, 0);
4557 if (err)
4558 return err;
4559 if (view_needs_focus_indication(view))
4560 wstandout(view->window);
4561 tc = get_color(colors, TOG_COLOR_COMMIT);
4562 if (tc)
4563 wattr_on(view->window,
4564 COLOR_PAIR(tc->colorpair), NULL);
4565 waddwstr(view->window, wline);
4566 if (tc)
4567 wattr_off(view->window,
4568 COLOR_PAIR(tc->colorpair), NULL);
4569 if (view_needs_focus_indication(view))
4570 wstandend(view->window);
4571 free(wline);
4572 wline = NULL;
4573 if (width < view->ncols - 1)
4574 waddch(view->window, '\n');
4575 if (--limit <= 0)
4576 return NULL;
4577 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4578 if (err)
4579 return err;
4580 waddwstr(view->window, wline);
4581 free(wline);
4582 wline = NULL;
4583 if (width < view->ncols - 1)
4584 waddch(view->window, '\n');
4585 if (--limit <= 0)
4586 return NULL;
4587 waddch(view->window, '\n');
4588 if (--limit <= 0)
4589 return NULL;
4591 if (*first_displayed_entry == NULL) {
4592 te = got_object_tree_get_first_entry(tree);
4593 if (selected == 0) {
4594 if (view->focussed)
4595 wstandout(view->window);
4596 *selected_entry = NULL;
4598 waddstr(view->window, " ..\n"); /* parent directory */
4599 if (selected == 0 && view->focussed)
4600 wstandend(view->window);
4601 (*ndisplayed)++;
4602 if (--limit <= 0)
4603 return NULL;
4604 n = 1;
4605 } else {
4606 n = 0;
4607 te = *first_displayed_entry;
4610 nentries = got_object_tree_get_nentries(tree);
4611 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4612 char *line = NULL, *id_str = NULL;
4613 const char *modestr = "";
4614 mode_t mode;
4616 te = got_object_tree_get_entry(tree, i);
4617 mode = got_tree_entry_get_mode(te);
4619 if (show_ids) {
4620 err = got_object_id_str(&id_str,
4621 got_tree_entry_get_id(te));
4622 if (err)
4623 return got_error_from_errno(
4624 "got_object_id_str");
4626 if (got_object_tree_entry_is_submodule(te))
4627 modestr = "$";
4628 else if (S_ISLNK(mode))
4629 modestr = "@";
4630 else if (S_ISDIR(mode))
4631 modestr = "/";
4632 else if (mode & S_IXUSR)
4633 modestr = "*";
4634 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4635 got_tree_entry_get_name(te), modestr) == -1) {
4636 free(id_str);
4637 return got_error_from_errno("asprintf");
4639 free(id_str);
4640 err = format_line(&wline, &width, line, view->ncols, 0);
4641 if (err) {
4642 free(line);
4643 break;
4645 if (n == selected) {
4646 if (view->focussed)
4647 wstandout(view->window);
4648 *selected_entry = te;
4650 tc = match_color(colors, line);
4651 if (tc)
4652 wattr_on(view->window,
4653 COLOR_PAIR(tc->colorpair), NULL);
4654 waddwstr(view->window, wline);
4655 if (tc)
4656 wattr_off(view->window,
4657 COLOR_PAIR(tc->colorpair), NULL);
4658 if (width < view->ncols - 1)
4659 waddch(view->window, '\n');
4660 if (n == selected && view->focussed)
4661 wstandend(view->window);
4662 free(line);
4663 free(wline);
4664 wline = NULL;
4665 n++;
4666 (*ndisplayed)++;
4667 *last_displayed_entry = te;
4668 if (--limit <= 0)
4669 break;
4672 return err;
4675 static void
4676 tree_scroll_up(struct tog_view *view,
4677 struct got_tree_entry **first_displayed_entry, int maxscroll,
4678 struct got_tree_object *tree, int isroot)
4680 struct got_tree_entry *te;
4681 int i;
4683 if (*first_displayed_entry == NULL)
4684 return;
4686 te = got_object_tree_get_entry(tree, 0);
4687 if (*first_displayed_entry == te) {
4688 if (!isroot)
4689 *first_displayed_entry = NULL;
4690 return;
4693 i = 0;
4694 while (*first_displayed_entry && i < maxscroll) {
4695 *first_displayed_entry = got_tree_entry_get_prev(tree,
4696 *first_displayed_entry);
4697 i++;
4699 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4700 *first_displayed_entry = NULL;
4703 static int
4704 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4705 struct got_tree_entry *last_displayed_entry,
4706 struct got_tree_object *tree)
4708 struct got_tree_entry *next, *last;
4709 int n = 0;
4711 if (*first_displayed_entry)
4712 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4713 else
4714 next = got_object_tree_get_first_entry(tree);
4716 last = last_displayed_entry;
4717 while (next && last && n++ < maxscroll) {
4718 last = got_tree_entry_get_next(tree, last);
4719 if (last) {
4720 *first_displayed_entry = next;
4721 next = got_tree_entry_get_next(tree, next);
4724 return n;
4727 static const struct got_error *
4728 tree_entry_path(char **path, struct tog_parent_trees *parents,
4729 struct got_tree_entry *te)
4731 const struct got_error *err = NULL;
4732 struct tog_parent_tree *pt;
4733 size_t len = 2; /* for leading slash and NUL */
4735 TAILQ_FOREACH(pt, parents, entry)
4736 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4737 + 1 /* slash */;
4738 if (te)
4739 len += strlen(got_tree_entry_get_name(te));
4741 *path = calloc(1, len);
4742 if (path == NULL)
4743 return got_error_from_errno("calloc");
4745 (*path)[0] = '/';
4746 pt = TAILQ_LAST(parents, tog_parent_trees);
4747 while (pt) {
4748 const char *name = got_tree_entry_get_name(pt->selected_entry);
4749 if (strlcat(*path, name, len) >= len) {
4750 err = got_error(GOT_ERR_NO_SPACE);
4751 goto done;
4753 if (strlcat(*path, "/", len) >= len) {
4754 err = got_error(GOT_ERR_NO_SPACE);
4755 goto done;
4757 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4759 if (te) {
4760 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4761 err = got_error(GOT_ERR_NO_SPACE);
4762 goto done;
4765 done:
4766 if (err) {
4767 free(*path);
4768 *path = NULL;
4770 return err;
4773 static const struct got_error *
4774 blame_tree_entry(struct tog_view **new_view, int begin_x,
4775 struct got_tree_entry *te, struct tog_parent_trees *parents,
4776 struct got_object_id *commit_id, struct got_reflist_head *refs,
4777 struct got_repository *repo)
4779 const struct got_error *err = NULL;
4780 char *path;
4781 struct tog_view *blame_view;
4783 *new_view = NULL;
4785 err = tree_entry_path(&path, parents, te);
4786 if (err)
4787 return err;
4789 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4790 if (blame_view == NULL) {
4791 err = got_error_from_errno("view_open");
4792 goto done;
4795 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4796 if (err) {
4797 if (err->code == GOT_ERR_CANCELLED)
4798 err = NULL;
4799 view_close(blame_view);
4800 } else
4801 *new_view = blame_view;
4802 done:
4803 free(path);
4804 return err;
4807 static const struct got_error *
4808 log_tree_entry(struct tog_view **new_view, int begin_x,
4809 struct got_tree_entry *te, struct tog_parent_trees *parents,
4810 struct got_object_id *commit_id, struct got_reflist_head *refs,
4811 struct got_repository *repo)
4813 struct tog_view *log_view;
4814 const struct got_error *err = NULL;
4815 char *path;
4817 *new_view = NULL;
4819 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4820 if (log_view == NULL)
4821 return got_error_from_errno("view_open");
4823 err = tree_entry_path(&path, parents, te);
4824 if (err)
4825 return err;
4827 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4828 if (err)
4829 view_close(log_view);
4830 else
4831 *new_view = log_view;
4832 free(path);
4833 return err;
4836 static const struct got_error *
4837 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4838 struct got_object_id *commit_id, struct got_reflist_head *refs,
4839 struct got_repository *repo)
4841 const struct got_error *err = NULL;
4842 char *commit_id_str = NULL;
4843 struct tog_tree_view_state *s = &view->state.tree;
4845 TAILQ_INIT(&s->parents);
4847 err = got_object_id_str(&commit_id_str, commit_id);
4848 if (err != NULL)
4849 goto done;
4851 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4852 err = got_error_from_errno("asprintf");
4853 goto done;
4856 s->root = s->tree = root;
4857 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4858 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4859 s->commit_id = got_object_id_dup(commit_id);
4860 if (s->commit_id == NULL) {
4861 err = got_error_from_errno("got_object_id_dup");
4862 goto done;
4864 s->refs = refs;
4865 s->repo = repo;
4867 SIMPLEQ_INIT(&s->colors);
4869 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4870 err = add_color(&s->colors, "\\$$",
4871 TOG_COLOR_TREE_SUBMODULE,
4872 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4873 if (err)
4874 goto done;
4875 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4876 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4877 if (err) {
4878 free_colors(&s->colors);
4879 goto done;
4881 err = add_color(&s->colors, "/$",
4882 TOG_COLOR_TREE_DIRECTORY,
4883 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4884 if (err) {
4885 free_colors(&s->colors);
4886 goto done;
4889 err = add_color(&s->colors, "\\*$",
4890 TOG_COLOR_TREE_EXECUTABLE,
4891 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4892 if (err) {
4893 free_colors(&s->colors);
4894 goto done;
4897 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4898 get_color_value("TOG_COLOR_COMMIT"));
4899 if (err) {
4900 free_colors(&s->colors);
4901 goto done;
4905 view->show = show_tree_view;
4906 view->input = input_tree_view;
4907 view->close = close_tree_view;
4908 view->search_start = search_start_tree_view;
4909 view->search_next = search_next_tree_view;
4910 done:
4911 free(commit_id_str);
4912 if (err) {
4913 free(s->tree_label);
4914 s->tree_label = NULL;
4916 return err;
4919 static const struct got_error *
4920 close_tree_view(struct tog_view *view)
4922 struct tog_tree_view_state *s = &view->state.tree;
4924 free_colors(&s->colors);
4925 free(s->tree_label);
4926 s->tree_label = NULL;
4927 free(s->commit_id);
4928 s->commit_id = NULL;
4929 while (!TAILQ_EMPTY(&s->parents)) {
4930 struct tog_parent_tree *parent;
4931 parent = TAILQ_FIRST(&s->parents);
4932 TAILQ_REMOVE(&s->parents, parent, entry);
4933 free(parent);
4936 if (s->tree != s->root)
4937 got_object_tree_close(s->tree);
4938 got_object_tree_close(s->root);
4940 return NULL;
4943 static const struct got_error *
4944 search_start_tree_view(struct tog_view *view)
4946 struct tog_tree_view_state *s = &view->state.tree;
4948 s->matched_entry = NULL;
4949 return NULL;
4952 static int
4953 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4955 regmatch_t regmatch;
4957 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4958 0) == 0;
4961 static const struct got_error *
4962 search_next_tree_view(struct tog_view *view)
4964 struct tog_tree_view_state *s = &view->state.tree;
4965 struct got_tree_entry *te = NULL;
4967 if (!view->searching) {
4968 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4969 return NULL;
4972 if (s->matched_entry) {
4973 if (view->searching == TOG_SEARCH_FORWARD) {
4974 if (s->selected_entry)
4975 te = got_tree_entry_get_next(s->tree,
4976 s->selected_entry);
4977 else
4978 te = got_object_tree_get_first_entry(s->tree);
4979 } else {
4980 if (s->selected_entry == NULL)
4981 te = got_object_tree_get_last_entry(s->tree);
4982 else
4983 te = got_tree_entry_get_prev(s->tree,
4984 s->selected_entry);
4986 } else {
4987 if (view->searching == TOG_SEARCH_FORWARD)
4988 te = got_object_tree_get_first_entry(s->tree);
4989 else
4990 te = got_object_tree_get_last_entry(s->tree);
4993 while (1) {
4994 if (te == NULL) {
4995 if (s->matched_entry == NULL) {
4996 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4997 return NULL;
4999 if (view->searching == TOG_SEARCH_FORWARD)
5000 te = got_object_tree_get_first_entry(s->tree);
5001 else
5002 te = got_object_tree_get_last_entry(s->tree);
5005 if (match_tree_entry(te, &view->regex)) {
5006 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5007 s->matched_entry = te;
5008 break;
5011 if (view->searching == TOG_SEARCH_FORWARD)
5012 te = got_tree_entry_get_next(s->tree, te);
5013 else
5014 te = got_tree_entry_get_prev(s->tree, te);
5017 if (s->matched_entry) {
5018 s->first_displayed_entry = s->matched_entry;
5019 s->selected = 0;
5022 return NULL;
5025 static const struct got_error *
5026 show_tree_view(struct tog_view *view)
5028 const struct got_error *err = NULL;
5029 struct tog_tree_view_state *s = &view->state.tree;
5030 char *parent_path;
5032 err = tree_entry_path(&parent_path, &s->parents, NULL);
5033 if (err)
5034 return err;
5036 err = draw_tree_entries(view, &s->first_displayed_entry,
5037 &s->last_displayed_entry, &s->selected_entry,
5038 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5039 s->tree, s->selected, view->nlines, s->tree == s->root,
5040 &s->colors);
5041 free(parent_path);
5043 view_vborder(view);
5044 return err;
5047 static const struct got_error *
5048 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5049 struct tog_view **focus_view, struct tog_view *view, int ch)
5051 const struct got_error *err = NULL;
5052 struct tog_tree_view_state *s = &view->state.tree;
5053 struct tog_view *log_view;
5054 int begin_x = 0, nscrolled;
5056 switch (ch) {
5057 case 'i':
5058 s->show_ids = !s->show_ids;
5059 break;
5060 case 'l':
5061 if (!s->selected_entry)
5062 break;
5063 if (view_is_parent_view(view))
5064 begin_x = view_split_begin_x(view->begin_x);
5065 err = log_tree_entry(&log_view, begin_x,
5066 s->selected_entry, &s->parents,
5067 s->commit_id, s->refs, s->repo);
5068 if (view_is_parent_view(view)) {
5069 err = view_close_child(view);
5070 if (err)
5071 return err;
5072 err = view_set_child(view, log_view);
5073 if (err) {
5074 view_close(log_view);
5075 break;
5077 *focus_view = log_view;
5078 view->child_focussed = 1;
5079 } else
5080 *new_view = log_view;
5081 break;
5082 case 'k':
5083 case KEY_UP:
5084 if (s->selected > 0) {
5085 s->selected--;
5086 if (s->selected == 0)
5087 break;
5089 if (s->selected > 0)
5090 break;
5091 tree_scroll_up(view, &s->first_displayed_entry, 1,
5092 s->tree, s->tree == s->root);
5093 break;
5094 case KEY_PPAGE:
5095 case CTRL('b'):
5096 tree_scroll_up(view, &s->first_displayed_entry,
5097 MAX(0, view->nlines - 4 - s->selected), s->tree,
5098 s->tree == s->root);
5099 s->selected = 0;
5100 if (got_object_tree_get_first_entry(s->tree) ==
5101 s->first_displayed_entry && s->tree != s->root)
5102 s->first_displayed_entry = NULL;
5103 break;
5104 case 'j':
5105 case KEY_DOWN:
5106 if (s->selected < s->ndisplayed - 1) {
5107 s->selected++;
5108 break;
5110 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5111 == NULL)
5112 /* can't scroll any further */
5113 break;
5114 tree_scroll_down(&s->first_displayed_entry, 1,
5115 s->last_displayed_entry, s->tree);
5116 break;
5117 case KEY_NPAGE:
5118 case CTRL('f'):
5119 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5120 == NULL) {
5121 /* can't scroll any further; move cursor down */
5122 if (s->selected < s->ndisplayed - 1)
5123 s->selected = s->ndisplayed - 1;
5124 break;
5126 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5127 view->nlines, s->last_displayed_entry, s->tree);
5128 if (nscrolled < view->nlines) {
5129 int ndisplayed = 0;
5130 struct got_tree_entry *te;
5131 te = s->first_displayed_entry;
5132 do {
5133 ndisplayed++;
5134 te = got_tree_entry_get_next(s->tree, te);
5135 } while (te);
5136 s->selected = ndisplayed - 1;
5138 break;
5139 case KEY_ENTER:
5140 case '\r':
5141 case KEY_BACKSPACE:
5142 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5143 struct tog_parent_tree *parent;
5144 /* user selected '..' */
5145 if (s->tree == s->root)
5146 break;
5147 parent = TAILQ_FIRST(&s->parents);
5148 TAILQ_REMOVE(&s->parents, parent,
5149 entry);
5150 got_object_tree_close(s->tree);
5151 s->tree = parent->tree;
5152 s->first_displayed_entry =
5153 parent->first_displayed_entry;
5154 s->selected_entry =
5155 parent->selected_entry;
5156 s->selected = parent->selected;
5157 free(parent);
5158 } else if (S_ISDIR(got_tree_entry_get_mode(
5159 s->selected_entry))) {
5160 struct got_tree_object *subtree;
5161 err = got_object_open_as_tree(&subtree, s->repo,
5162 got_tree_entry_get_id(s->selected_entry));
5163 if (err)
5164 break;
5165 err = tree_view_visit_subtree(subtree, s);
5166 if (err) {
5167 got_object_tree_close(subtree);
5168 break;
5170 } else if (S_ISREG(got_tree_entry_get_mode(
5171 s->selected_entry))) {
5172 struct tog_view *blame_view;
5173 int begin_x = view_is_parent_view(view) ?
5174 view_split_begin_x(view->begin_x) : 0;
5176 err = blame_tree_entry(&blame_view, begin_x,
5177 s->selected_entry, &s->parents,
5178 s->commit_id, s->refs, s->repo);
5179 if (err)
5180 break;
5181 if (view_is_parent_view(view)) {
5182 err = view_close_child(view);
5183 if (err)
5184 return err;
5185 err = view_set_child(view, blame_view);
5186 if (err) {
5187 view_close(blame_view);
5188 break;
5190 *focus_view = blame_view;
5191 view->child_focussed = 1;
5192 } else
5193 *new_view = blame_view;
5195 break;
5196 case KEY_RESIZE:
5197 if (s->selected > view->nlines)
5198 s->selected = s->ndisplayed - 1;
5199 break;
5200 default:
5201 break;
5204 return err;
5207 __dead static void
5208 usage_tree(void)
5210 endwin();
5211 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5212 getprogname());
5213 exit(1);
5216 static const struct got_error *
5217 cmd_tree(int argc, char *argv[])
5219 const struct got_error *error;
5220 struct got_repository *repo = NULL;
5221 struct got_worktree *worktree = NULL;
5222 struct got_reflist_head refs;
5223 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5224 struct got_object_id *commit_id = NULL;
5225 char *commit_id_arg = NULL;
5226 struct got_commit_object *commit = NULL;
5227 struct got_tree_object *tree = NULL;
5228 int ch;
5229 struct tog_view *view;
5231 SIMPLEQ_INIT(&refs);
5233 #ifndef PROFILE
5234 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5235 NULL) == -1)
5236 err(1, "pledge");
5237 #endif
5239 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5240 switch (ch) {
5241 case 'c':
5242 commit_id_arg = optarg;
5243 break;
5244 case 'r':
5245 repo_path = realpath(optarg, NULL);
5246 if (repo_path == NULL)
5247 return got_error_from_errno2("realpath",
5248 optarg);
5249 break;
5250 default:
5251 usage_tree();
5252 /* NOTREACHED */
5256 argc -= optind;
5257 argv += optind;
5259 if (argc > 1)
5260 usage_tree();
5262 cwd = getcwd(NULL, 0);
5263 if (cwd == NULL)
5264 return got_error_from_errno("getcwd");
5266 error = got_worktree_open(&worktree, cwd);
5267 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5268 goto done;
5270 if (repo_path == NULL) {
5271 if (worktree)
5272 repo_path =
5273 strdup(got_worktree_get_repo_path(worktree));
5274 else
5275 repo_path = strdup(cwd);
5277 if (repo_path == NULL) {
5278 error = got_error_from_errno("strdup");
5279 goto done;
5282 error = got_repo_open(&repo, repo_path, NULL);
5283 if (error != NULL)
5284 goto done;
5286 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5287 repo, worktree);
5288 if (error)
5289 goto done;
5291 init_curses();
5293 error = apply_unveil(got_repo_get_path(repo), NULL);
5294 if (error)
5295 goto done;
5297 error = got_repo_match_object_id(&commit_id, NULL,
5298 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5299 GOT_OBJ_TYPE_COMMIT, 1, repo);
5300 if (error)
5301 goto done;
5303 error = got_object_open_as_commit(&commit, repo, commit_id);
5304 if (error)
5305 goto done;
5307 error = got_object_open_as_tree(&tree, repo,
5308 got_object_commit_get_tree_id(commit));
5309 if (error)
5310 goto done;
5312 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5313 if (error)
5314 goto done;
5316 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5317 if (view == NULL) {
5318 error = got_error_from_errno("view_open");
5319 goto done;
5321 error = open_tree_view(view, tree, commit_id, &refs, repo);
5322 if (error)
5323 goto done;
5324 if (!got_path_is_root_dir(in_repo_path)) {
5325 error = tree_view_walk_path(&view->state.tree, commit_id,
5326 in_repo_path, repo);
5327 if (error)
5328 goto done;
5331 if (worktree) {
5332 /* Release work tree lock. */
5333 got_worktree_close(worktree);
5334 worktree = NULL;
5336 error = view_loop(view);
5337 done:
5338 free(repo_path);
5339 free(cwd);
5340 free(commit_id);
5341 if (commit)
5342 got_object_commit_close(commit);
5343 if (tree)
5344 got_object_tree_close(tree);
5345 if (repo)
5346 got_repo_close(repo);
5347 got_ref_list_free(&refs);
5348 return error;
5351 static void
5352 list_commands(void)
5354 int i;
5356 fprintf(stderr, "commands:");
5357 for (i = 0; i < nitems(tog_commands); i++) {
5358 struct tog_cmd *cmd = &tog_commands[i];
5359 fprintf(stderr, " %s", cmd->name);
5361 fputc('\n', stderr);
5364 __dead static void
5365 usage(int hflag)
5367 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5368 "[arg ...]\n", getprogname());
5369 if (hflag) {
5370 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5371 list_commands();
5373 exit(1);
5376 static char **
5377 make_argv(int argc, ...)
5379 va_list ap;
5380 char **argv;
5381 int i;
5383 va_start(ap, argc);
5385 argv = calloc(argc, sizeof(char *));
5386 if (argv == NULL)
5387 err(1, "calloc");
5388 for (i = 0; i < argc; i++) {
5389 argv[i] = strdup(va_arg(ap, char *));
5390 if (argv[i] == NULL)
5391 err(1, "strdup");
5394 va_end(ap);
5395 return argv;
5399 * Try to convert 'tog path' into a 'tog log path' command.
5400 * The user could simply have mistyped the command rather than knowingly
5401 * provided a path. So check whether argv[0] can in fact be resolved
5402 * to a path in the HEAD commit and print a special error if not.
5403 * This hack is for mpi@ <3
5405 static const struct got_error *
5406 tog_log_with_path(int argc, char *argv[])
5408 const struct got_error *error = NULL;
5409 struct tog_cmd *cmd = NULL;
5410 struct got_repository *repo = NULL;
5411 struct got_worktree *worktree = NULL;
5412 struct got_object_id *commit_id = NULL, *id = NULL;
5413 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5414 char *commit_id_str = NULL, **cmd_argv = NULL;
5416 cwd = getcwd(NULL, 0);
5417 if (cwd == NULL)
5418 return got_error_from_errno("getcwd");
5420 error = got_worktree_open(&worktree, cwd);
5421 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5422 goto done;
5424 if (worktree)
5425 repo_path = strdup(got_worktree_get_repo_path(worktree));
5426 else
5427 repo_path = strdup(cwd);
5428 if (repo_path == NULL) {
5429 error = got_error_from_errno("strdup");
5430 goto done;
5433 error = got_repo_open(&repo, repo_path, NULL);
5434 if (error != NULL)
5435 goto done;
5437 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5438 repo, worktree);
5439 if (error)
5440 goto done;
5442 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5443 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5444 GOT_OBJ_TYPE_COMMIT, 1, repo);
5445 if (error)
5446 goto done;
5448 if (worktree) {
5449 got_worktree_close(worktree);
5450 worktree = NULL;
5453 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5454 if (error) {
5455 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5456 goto done;
5457 fprintf(stderr, "%s: '%s' is no known command or path\n",
5458 getprogname(), argv[0]);
5459 usage(1);
5460 /* not reached */
5463 got_repo_close(repo);
5464 repo = NULL;
5466 error = got_object_id_str(&commit_id_str, commit_id);
5467 if (error)
5468 goto done;
5470 cmd = &tog_commands[0]; /* log */
5471 argc = 4;
5472 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5473 error = cmd->cmd_main(argc, cmd_argv);
5474 done:
5475 if (repo)
5476 got_repo_close(repo);
5477 if (worktree)
5478 got_worktree_close(worktree);
5479 free(id);
5480 free(commit_id_str);
5481 free(commit_id);
5482 free(cwd);
5483 free(repo_path);
5484 free(in_repo_path);
5485 if (cmd_argv) {
5486 int i;
5487 for (i = 0; i < argc; i++)
5488 free(cmd_argv[i]);
5489 free(cmd_argv);
5491 return error;
5494 int
5495 main(int argc, char *argv[])
5497 const struct got_error *error = NULL;
5498 struct tog_cmd *cmd = NULL;
5499 int ch, hflag = 0, Vflag = 0;
5500 char **cmd_argv = NULL;
5501 static struct option longopts[] = {
5502 { "version", no_argument, NULL, 'V' },
5503 { NULL, 0, NULL, 0}
5506 setlocale(LC_CTYPE, "");
5508 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5509 switch (ch) {
5510 case 'h':
5511 hflag = 1;
5512 break;
5513 case 'V':
5514 Vflag = 1;
5515 break;
5516 default:
5517 usage(hflag);
5518 /* NOTREACHED */
5522 argc -= optind;
5523 argv += optind;
5524 optind = 0;
5525 optreset = 1;
5527 if (Vflag) {
5528 got_version_print_str();
5529 return 1;
5532 if (argc == 0) {
5533 if (hflag)
5534 usage(hflag);
5535 /* Build an argument vector which runs a default command. */
5536 cmd = &tog_commands[0];
5537 argc = 1;
5538 cmd_argv = make_argv(argc, cmd->name);
5539 } else {
5540 int i;
5542 /* Did the user specify a command? */
5543 for (i = 0; i < nitems(tog_commands); i++) {
5544 if (strncmp(tog_commands[i].name, argv[0],
5545 strlen(argv[0])) == 0) {
5546 cmd = &tog_commands[i];
5547 break;
5552 if (cmd == NULL) {
5553 if (argc != 1)
5554 usage(0);
5555 /* No command specified; try log with a path */
5556 error = tog_log_with_path(argc, argv);
5557 } else {
5558 if (hflag)
5559 cmd->cmd_usage();
5560 else
5561 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5564 endwin();
5565 if (cmd_argv) {
5566 int i;
5567 for (i = 0; i < argc; i++)
5568 free(cmd_argv[i]);
5569 free(cmd_argv);
5572 if (error && error->code != GOT_ERR_CANCELLED)
5573 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5574 return 0;