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 #define TOG_SEARCH_HAVE_NONE 3
443 regex_t regex;
444 };
446 static const struct got_error *open_diff_view(struct tog_view *,
447 struct got_object_id *, struct got_object_id *, struct tog_view *,
448 struct got_reflist_head *, struct got_repository *);
449 static const struct got_error *show_diff_view(struct tog_view *);
450 static const struct got_error *input_diff_view(struct tog_view **,
451 struct tog_view **, struct tog_view **, struct tog_view *, int);
452 static const struct got_error* close_diff_view(struct tog_view *);
453 static const struct got_error *search_start_diff_view(struct tog_view *);
454 static const struct got_error *search_next_diff_view(struct tog_view *);
456 static const struct got_error *open_log_view(struct tog_view *,
457 struct got_object_id *, struct got_reflist_head *,
458 struct got_repository *, const char *, const char *, int);
459 static const struct got_error * show_log_view(struct tog_view *);
460 static const struct got_error *input_log_view(struct tog_view **,
461 struct tog_view **, struct tog_view **, struct tog_view *, int);
462 static const struct got_error *close_log_view(struct tog_view *);
463 static const struct got_error *search_start_log_view(struct tog_view *);
464 static const struct got_error *search_next_log_view(struct tog_view *);
466 static const struct got_error *open_blame_view(struct tog_view *, char *,
467 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
468 static const struct got_error *show_blame_view(struct tog_view *);
469 static const struct got_error *input_blame_view(struct tog_view **,
470 struct tog_view **, struct tog_view **, struct tog_view *, int);
471 static const struct got_error *close_blame_view(struct tog_view *);
472 static const struct got_error *search_start_blame_view(struct tog_view *);
473 static const struct got_error *search_next_blame_view(struct tog_view *);
475 static const struct got_error *open_tree_view(struct tog_view *,
476 struct got_tree_object *, struct got_object_id *, struct got_reflist_head *,
477 struct got_repository *);
478 static const struct got_error *show_tree_view(struct tog_view *);
479 static const struct got_error *input_tree_view(struct tog_view **,
480 struct tog_view **, struct tog_view **, struct tog_view *, int);
481 static const struct got_error *close_tree_view(struct tog_view *);
482 static const struct got_error *search_start_tree_view(struct tog_view *);
483 static const struct got_error *search_next_tree_view(struct tog_view *);
485 static volatile sig_atomic_t tog_sigwinch_received;
486 static volatile sig_atomic_t tog_sigpipe_received;
487 static volatile sig_atomic_t tog_sigcont_received;
489 static void
490 tog_sigwinch(int signo)
492 tog_sigwinch_received = 1;
495 static void
496 tog_sigpipe(int signo)
498 tog_sigpipe_received = 1;
501 static void
502 tog_sigcont(int signo)
504 tog_sigcont_received = 1;
507 static const struct got_error *
508 view_close(struct tog_view *view)
510 const struct got_error *err = NULL;
512 if (view->child) {
513 view_close(view->child);
514 view->child = NULL;
516 if (view->close)
517 err = view->close(view);
518 if (view->panel)
519 del_panel(view->panel);
520 if (view->window)
521 delwin(view->window);
522 free(view);
523 return err;
526 static struct tog_view *
527 view_open(int nlines, int ncols, int begin_y, int begin_x,
528 enum tog_view_type type)
530 struct tog_view *view = calloc(1, sizeof(*view));
532 if (view == NULL)
533 return NULL;
535 view->type = type;
536 view->lines = LINES;
537 view->cols = COLS;
538 view->nlines = nlines ? nlines : LINES - begin_y;
539 view->ncols = ncols ? ncols : COLS - begin_x;
540 view->begin_y = begin_y;
541 view->begin_x = begin_x;
542 view->window = newwin(nlines, ncols, begin_y, begin_x);
543 if (view->window == NULL) {
544 view_close(view);
545 return NULL;
547 view->panel = new_panel(view->window);
548 if (view->panel == NULL ||
549 set_panel_userptr(view->panel, view) != OK) {
550 view_close(view);
551 return NULL;
554 keypad(view->window, TRUE);
555 return view;
558 static int
559 view_split_begin_x(int begin_x)
561 if (begin_x > 0 || COLS < 120)
562 return 0;
563 return (COLS - MAX(COLS / 2, 80));
566 static const struct got_error *view_resize(struct tog_view *);
568 static const struct got_error *
569 view_splitscreen(struct tog_view *view)
571 const struct got_error *err = NULL;
573 view->begin_y = 0;
574 view->begin_x = view_split_begin_x(0);
575 view->nlines = LINES;
576 view->ncols = COLS - view->begin_x;
577 view->lines = LINES;
578 view->cols = COLS;
579 err = view_resize(view);
580 if (err)
581 return err;
583 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
584 return got_error_from_errno("mvwin");
586 return NULL;
589 static const struct got_error *
590 view_fullscreen(struct tog_view *view)
592 const struct got_error *err = NULL;
594 view->begin_x = 0;
595 view->begin_y = 0;
596 view->nlines = LINES;
597 view->ncols = COLS;
598 view->lines = LINES;
599 view->cols = COLS;
600 err = view_resize(view);
601 if (err)
602 return err;
604 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
605 return got_error_from_errno("mvwin");
607 return NULL;
610 static int
611 view_is_parent_view(struct tog_view *view)
613 return view->parent == NULL;
616 static const struct got_error *
617 view_resize(struct tog_view *view)
619 int nlines, ncols;
621 if (view->lines > LINES)
622 nlines = view->nlines - (view->lines - LINES);
623 else
624 nlines = view->nlines + (LINES - view->lines);
626 if (view->cols > COLS)
627 ncols = view->ncols - (view->cols - COLS);
628 else
629 ncols = view->ncols + (COLS - view->cols);
631 if (wresize(view->window, nlines, ncols) == ERR)
632 return got_error_from_errno("wresize");
633 if (replace_panel(view->panel, view->window) == ERR)
634 return got_error_from_errno("replace_panel");
635 wclear(view->window);
637 view->nlines = nlines;
638 view->ncols = ncols;
639 view->lines = LINES;
640 view->cols = COLS;
642 if (view->child) {
643 view->child->begin_x = view_split_begin_x(view->begin_x);
644 if (view->child->begin_x == 0) {
645 view_fullscreen(view->child);
646 if (view->child->focussed)
647 show_panel(view->child->panel);
648 else
649 show_panel(view->panel);
650 } else {
651 view_splitscreen(view->child);
652 show_panel(view->child->panel);
656 return NULL;
659 static const struct got_error *
660 view_close_child(struct tog_view *view)
662 const struct got_error *err = NULL;
664 if (view->child == NULL)
665 return NULL;
667 err = view_close(view->child);
668 view->child = NULL;
669 return err;
672 static const struct got_error *
673 view_set_child(struct tog_view *view, struct tog_view *child)
675 const struct got_error *err = NULL;
677 view->child = child;
678 child->parent = view;
679 return err;
682 static int
683 view_is_splitscreen(struct tog_view *view)
685 return view->begin_x > 0;
688 static void
689 tog_resizeterm(void)
691 int cols, lines;
692 struct winsize size;
694 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
695 cols = 80; /* Default */
696 lines = 24;
697 } else {
698 cols = size.ws_col;
699 lines = size.ws_row;
701 resize_term(lines, cols);
704 static const struct got_error *
705 view_search_start(struct tog_view *view)
707 const struct got_error *err = NULL;
708 char pattern[1024];
709 int ret;
711 if (view->nlines < 1)
712 return NULL;
714 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
715 wclrtoeol(view->window);
717 nocbreak();
718 echo();
719 ret = wgetnstr(view->window, pattern, sizeof(pattern));
720 cbreak();
721 noecho();
722 if (ret == ERR)
723 return NULL;
725 if (view->searching) {
726 regfree(&view->regex);
727 view->searching = 0;
730 if (regcomp(&view->regex, pattern,
731 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
732 err = view->search_start(view);
733 if (err) {
734 regfree(&view->regex);
735 return err;
737 view->searching = TOG_SEARCH_FORWARD;
738 view->search_next_done = 0;
739 view->search_next(view);
742 return NULL;
745 static const struct got_error *
746 view_input(struct tog_view **new, struct tog_view **dead,
747 struct tog_view **focus, int *done, struct tog_view *view,
748 struct tog_view_list_head *views)
750 const struct got_error *err = NULL;
751 struct tog_view *v;
752 int ch, errcode;
754 *new = NULL;
755 *dead = NULL;
756 *focus = NULL;
758 /* Clear "no matches" indicator. */
759 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
760 view->search_next_done == TOG_SEARCH_HAVE_NONE)
761 view->search_next_done = TOG_SEARCH_HAVE_MORE;
763 if (view->searching && !view->search_next_done) {
764 errcode = pthread_mutex_unlock(&tog_mutex);
765 if (errcode)
766 return got_error_set_errno(errcode,
767 "pthread_mutex_unlock");
768 pthread_yield();
769 errcode = pthread_mutex_lock(&tog_mutex);
770 if (errcode)
771 return got_error_set_errno(errcode,
772 "pthread_mutex_lock");
773 view->search_next(view);
774 return NULL;
777 nodelay(stdscr, FALSE);
778 /* Allow threads to make progress while we are waiting for input. */
779 errcode = pthread_mutex_unlock(&tog_mutex);
780 if (errcode)
781 return got_error_set_errno(errcode, "pthread_mutex_unlock");
782 ch = wgetch(view->window);
783 errcode = pthread_mutex_lock(&tog_mutex);
784 if (errcode)
785 return got_error_set_errno(errcode, "pthread_mutex_lock");
786 nodelay(stdscr, TRUE);
788 if (tog_sigwinch_received || tog_sigcont_received) {
789 tog_resizeterm();
790 tog_sigwinch_received = 0;
791 tog_sigcont_received = 0;
792 TAILQ_FOREACH(v, views, entry) {
793 err = view_resize(v);
794 if (err)
795 return err;
796 err = v->input(new, dead, focus, v, KEY_RESIZE);
797 if (err)
798 return err;
802 switch (ch) {
803 case ERR:
804 break;
805 case '\t':
806 if (view->child) {
807 *focus = view->child;
808 view->child_focussed = 1;
809 } else if (view->parent) {
810 *focus = view->parent;
811 view->parent->child_focussed = 0;
813 break;
814 case 'q':
815 err = view->input(new, dead, focus, view, ch);
816 *dead = view;
817 break;
818 case 'Q':
819 *done = 1;
820 break;
821 case 'f':
822 if (view_is_parent_view(view)) {
823 if (view->child == NULL)
824 break;
825 if (view_is_splitscreen(view->child)) {
826 *focus = view->child;
827 view->child_focussed = 1;
828 err = view_fullscreen(view->child);
829 } else
830 err = view_splitscreen(view->child);
831 if (err)
832 break;
833 err = view->child->input(new, dead, focus,
834 view->child, KEY_RESIZE);
835 } else {
836 if (view_is_splitscreen(view)) {
837 *focus = view;
838 view->parent->child_focussed = 1;
839 err = view_fullscreen(view);
840 } else {
841 err = view_splitscreen(view);
843 if (err)
844 break;
845 err = view->input(new, dead, focus, view,
846 KEY_RESIZE);
848 break;
849 case KEY_RESIZE:
850 break;
851 case '/':
852 if (view->search_start)
853 view_search_start(view);
854 else
855 err = view->input(new, dead, focus, view, ch);
856 break;
857 case 'N':
858 case 'n':
859 if (view->search_next) {
860 view->searching = (ch == 'n' ?
861 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
862 view->search_next_done = 0;
863 view->search_next(view);
864 } else
865 err = view->input(new, dead, focus, view, ch);
866 break;
867 default:
868 err = view->input(new, dead, focus, view, ch);
869 break;
872 return err;
875 void
876 view_vborder(struct tog_view *view)
878 PANEL *panel;
879 struct tog_view *view_above;
881 if (view->parent)
882 return view_vborder(view->parent);
884 panel = panel_above(view->panel);
885 if (panel == NULL)
886 return;
888 view_above = panel_userptr(panel);
889 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
890 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
893 int
894 view_needs_focus_indication(struct tog_view *view)
896 if (view_is_parent_view(view)) {
897 if (view->child == NULL || view->child_focussed)
898 return 0;
899 if (!view_is_splitscreen(view->child))
900 return 0;
901 } else if (!view_is_splitscreen(view))
902 return 0;
904 return view->focussed;
907 static const struct got_error *
908 view_loop(struct tog_view *view)
910 const struct got_error *err = NULL;
911 struct tog_view_list_head views;
912 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
913 int fast_refresh = 10;
914 int done = 0, errcode;
916 errcode = pthread_mutex_lock(&tog_mutex);
917 if (errcode)
918 return got_error_set_errno(errcode, "pthread_mutex_lock");
920 TAILQ_INIT(&views);
921 TAILQ_INSERT_HEAD(&views, view, entry);
923 main_view = view;
924 view->focussed = 1;
925 err = view->show(view);
926 if (err)
927 return err;
928 update_panels();
929 doupdate();
930 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
931 /* Refresh fast during initialization, then become slower. */
932 if (fast_refresh && fast_refresh-- == 0)
933 halfdelay(10); /* switch to once per second */
935 err = view_input(&new_view, &dead_view, &focus_view, &done,
936 view, &views);
937 if (err)
938 break;
939 if (dead_view) {
940 struct tog_view *prev = NULL;
942 if (view_is_parent_view(dead_view))
943 prev = TAILQ_PREV(dead_view,
944 tog_view_list_head, entry);
945 else if (view->parent != dead_view)
946 prev = view->parent;
948 if (dead_view->parent)
949 dead_view->parent->child = NULL;
950 else
951 TAILQ_REMOVE(&views, dead_view, entry);
953 err = view_close(dead_view);
954 if (err || (dead_view == main_view && new_view == NULL))
955 goto done;
957 if (view == dead_view) {
958 if (focus_view)
959 view = focus_view;
960 else if (prev)
961 view = prev;
962 else if (!TAILQ_EMPTY(&views))
963 view = TAILQ_LAST(&views,
964 tog_view_list_head);
965 else
966 view = NULL;
967 if (view) {
968 if (view->child && view->child_focussed)
969 focus_view = view->child;
970 else
971 focus_view = view;
975 if (new_view) {
976 struct tog_view *v, *t;
977 /* Only allow one parent view per type. */
978 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
979 if (v->type != new_view->type)
980 continue;
981 TAILQ_REMOVE(&views, v, entry);
982 err = view_close(v);
983 if (err)
984 goto done;
985 break;
987 TAILQ_INSERT_TAIL(&views, new_view, entry);
988 view = new_view;
989 if (focus_view == NULL)
990 focus_view = new_view;
992 if (focus_view) {
993 show_panel(focus_view->panel);
994 if (view)
995 view->focussed = 0;
996 focus_view->focussed = 1;
997 view = focus_view;
998 if (new_view)
999 show_panel(new_view->panel);
1000 if (view->child && view_is_splitscreen(view->child))
1001 show_panel(view->child->panel);
1003 if (view) {
1004 if (focus_view == NULL) {
1005 view->focussed = 1;
1006 show_panel(view->panel);
1007 if (view->child && view_is_splitscreen(view->child))
1008 show_panel(view->child->panel);
1009 focus_view = view;
1011 if (view->parent) {
1012 err = view->parent->show(view->parent);
1013 if (err)
1014 goto done;
1016 err = view->show(view);
1017 if (err)
1018 goto done;
1019 if (view->child) {
1020 err = view->child->show(view->child);
1021 if (err)
1022 goto done;
1024 update_panels();
1025 doupdate();
1028 done:
1029 while (!TAILQ_EMPTY(&views)) {
1030 view = TAILQ_FIRST(&views);
1031 TAILQ_REMOVE(&views, view, entry);
1032 view_close(view);
1035 errcode = pthread_mutex_unlock(&tog_mutex);
1036 if (errcode && err == NULL)
1037 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1039 return err;
1042 __dead static void
1043 usage_log(void)
1045 endwin();
1046 fprintf(stderr,
1047 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1048 getprogname());
1049 exit(1);
1052 /* Create newly allocated wide-character string equivalent to a byte string. */
1053 static const struct got_error *
1054 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1056 char *vis = NULL;
1057 const struct got_error *err = NULL;
1059 *ws = NULL;
1060 *wlen = mbstowcs(NULL, s, 0);
1061 if (*wlen == (size_t)-1) {
1062 int vislen;
1063 if (errno != EILSEQ)
1064 return got_error_from_errno("mbstowcs");
1066 /* byte string invalid in current encoding; try to "fix" it */
1067 err = got_mbsavis(&vis, &vislen, s);
1068 if (err)
1069 return err;
1070 *wlen = mbstowcs(NULL, vis, 0);
1071 if (*wlen == (size_t)-1) {
1072 err = got_error_from_errno("mbstowcs"); /* give up */
1073 goto done;
1077 *ws = calloc(*wlen + 1, sizeof(**ws));
1078 if (*ws == NULL) {
1079 err = got_error_from_errno("calloc");
1080 goto done;
1083 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1084 err = got_error_from_errno("mbstowcs");
1085 done:
1086 free(vis);
1087 if (err) {
1088 free(*ws);
1089 *ws = NULL;
1090 *wlen = 0;
1092 return err;
1095 /* Format a line for display, ensuring that it won't overflow a width limit. */
1096 static const struct got_error *
1097 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1098 int col_tab_align)
1100 const struct got_error *err = NULL;
1101 int cols = 0;
1102 wchar_t *wline = NULL;
1103 size_t wlen;
1104 int i;
1106 *wlinep = NULL;
1107 *widthp = 0;
1109 err = mbs2ws(&wline, &wlen, line);
1110 if (err)
1111 return err;
1113 i = 0;
1114 while (i < wlen) {
1115 int width = wcwidth(wline[i]);
1117 if (width == 0) {
1118 i++;
1119 continue;
1122 if (width == 1 || width == 2) {
1123 if (cols + width > wlimit)
1124 break;
1125 cols += width;
1126 i++;
1127 } else if (width == -1) {
1128 if (wline[i] == L'\t') {
1129 width = TABSIZE -
1130 ((cols + col_tab_align) % TABSIZE);
1131 if (cols + width > wlimit)
1132 break;
1133 cols += width;
1135 i++;
1136 } else {
1137 err = got_error_from_errno("wcwidth");
1138 goto done;
1141 wline[i] = L'\0';
1142 if (widthp)
1143 *widthp = cols;
1144 done:
1145 if (err)
1146 free(wline);
1147 else
1148 *wlinep = wline;
1149 return err;
1152 static const struct got_error*
1153 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1154 struct got_object_id *id, struct got_repository *repo)
1156 static const struct got_error *err = NULL;
1157 struct got_reflist_entry *re;
1158 char *s;
1159 const char *name;
1161 *refs_str = NULL;
1163 SIMPLEQ_FOREACH(re, refs, entry) {
1164 struct got_tag_object *tag = NULL;
1165 int cmp;
1167 name = got_ref_get_name(re->ref);
1168 if (strcmp(name, GOT_REF_HEAD) == 0)
1169 continue;
1170 if (strncmp(name, "refs/", 5) == 0)
1171 name += 5;
1172 if (strncmp(name, "got/", 4) == 0)
1173 continue;
1174 if (strncmp(name, "heads/", 6) == 0)
1175 name += 6;
1176 if (strncmp(name, "remotes/", 8) == 0)
1177 name += 8;
1178 if (strncmp(name, "tags/", 5) == 0) {
1179 err = got_object_open_as_tag(&tag, repo, re->id);
1180 if (err) {
1181 if (err->code != GOT_ERR_OBJ_TYPE)
1182 break;
1183 /* Ref points at something other than a tag. */
1184 err = NULL;
1185 tag = NULL;
1188 cmp = got_object_id_cmp(tag ?
1189 got_object_tag_get_object_id(tag) : re->id, id);
1190 if (tag)
1191 got_object_tag_close(tag);
1192 if (cmp != 0)
1193 continue;
1194 s = *refs_str;
1195 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1196 s ? ", " : "", name) == -1) {
1197 err = got_error_from_errno("asprintf");
1198 free(s);
1199 *refs_str = NULL;
1200 break;
1202 free(s);
1205 return err;
1208 static const struct got_error *
1209 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1210 int col_tab_align)
1212 char *smallerthan, *at;
1214 smallerthan = strchr(author, '<');
1215 if (smallerthan && smallerthan[1] != '\0')
1216 author = smallerthan + 1;
1217 at = strchr(author, '@');
1218 if (at)
1219 *at = '\0';
1220 return format_line(wauthor, author_width, author, limit, col_tab_align);
1223 static const struct got_error *
1224 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1225 struct got_object_id *id, struct got_reflist_head *refs,
1226 const size_t date_display_cols, int author_display_cols,
1227 struct tog_colors *colors)
1229 const struct got_error *err = NULL;
1230 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1231 char *logmsg0 = NULL, *logmsg = NULL;
1232 char *author = NULL;
1233 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1234 int author_width, logmsg_width;
1235 char *newline, *line = NULL;
1236 int col, limit;
1237 const int avail = view->ncols;
1238 struct tm tm;
1239 time_t committer_time;
1240 struct tog_color *tc;
1242 committer_time = got_object_commit_get_committer_time(commit);
1243 if (localtime_r(&committer_time, &tm) == NULL)
1244 return got_error_from_errno("localtime_r");
1245 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm)
1246 >= sizeof(datebuf))
1247 return got_error(GOT_ERR_NO_SPACE);
1249 if (avail <= date_display_cols)
1250 limit = MIN(sizeof(datebuf) - 1, avail);
1251 else
1252 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1253 tc = get_color(colors, TOG_COLOR_DATE);
1254 if (tc)
1255 wattr_on(view->window,
1256 COLOR_PAIR(tc->colorpair), NULL);
1257 waddnstr(view->window, datebuf, limit);
1258 if (tc)
1259 wattr_off(view->window,
1260 COLOR_PAIR(tc->colorpair), NULL);
1261 col = limit;
1262 if (col > avail)
1263 goto done;
1265 if (avail >= 120) {
1266 char *id_str;
1267 err = got_object_id_str(&id_str, id);
1268 if (err)
1269 goto done;
1270 tc = get_color(colors, TOG_COLOR_COMMIT);
1271 if (tc)
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 wprintw(view->window, "%.8s ", id_str);
1275 if (tc)
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 free(id_str);
1279 col += 9;
1280 if (col > avail)
1281 goto done;
1284 author = strdup(got_object_commit_get_author(commit));
1285 if (author == NULL) {
1286 err = got_error_from_errno("strdup");
1287 goto done;
1289 err = format_author(&wauthor, &author_width, author, avail - col, col);
1290 if (err)
1291 goto done;
1292 tc = get_color(colors, TOG_COLOR_AUTHOR);
1293 if (tc)
1294 wattr_on(view->window,
1295 COLOR_PAIR(tc->colorpair), NULL);
1296 waddwstr(view->window, wauthor);
1297 if (tc)
1298 wattr_off(view->window,
1299 COLOR_PAIR(tc->colorpair), NULL);
1300 col += author_width;
1301 while (col < avail && author_width < author_display_cols + 2) {
1302 waddch(view->window, ' ');
1303 col++;
1304 author_width++;
1306 if (col > avail)
1307 goto done;
1309 err = got_object_commit_get_logmsg(&logmsg0, commit);
1310 if (err)
1311 goto done;
1312 logmsg = logmsg0;
1313 while (*logmsg == '\n')
1314 logmsg++;
1315 newline = strchr(logmsg, '\n');
1316 if (newline)
1317 *newline = '\0';
1318 limit = avail - col;
1319 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, col);
1320 if (err)
1321 goto done;
1322 waddwstr(view->window, wlogmsg);
1323 col += logmsg_width;
1324 while (col < avail) {
1325 waddch(view->window, ' ');
1326 col++;
1328 done:
1329 free(logmsg0);
1330 free(wlogmsg);
1331 free(author);
1332 free(wauthor);
1333 free(line);
1334 return err;
1337 static struct commit_queue_entry *
1338 alloc_commit_queue_entry(struct got_commit_object *commit,
1339 struct got_object_id *id)
1341 struct commit_queue_entry *entry;
1343 entry = calloc(1, sizeof(*entry));
1344 if (entry == NULL)
1345 return NULL;
1347 entry->id = id;
1348 entry->commit = commit;
1349 return entry;
1352 static void
1353 pop_commit(struct commit_queue *commits)
1355 struct commit_queue_entry *entry;
1357 entry = TAILQ_FIRST(&commits->head);
1358 TAILQ_REMOVE(&commits->head, entry, entry);
1359 got_object_commit_close(entry->commit);
1360 commits->ncommits--;
1361 /* Don't free entry->id! It is owned by the commit graph. */
1362 free(entry);
1365 static void
1366 free_commits(struct commit_queue *commits)
1368 while (!TAILQ_EMPTY(&commits->head))
1369 pop_commit(commits);
1372 static const struct got_error *
1373 match_commit(int *have_match, struct got_object_id *id,
1374 struct got_commit_object *commit, regex_t *regex)
1376 const struct got_error *err = NULL;
1377 regmatch_t regmatch;
1378 char *id_str = NULL, *logmsg = NULL;
1380 *have_match = 0;
1382 err = got_object_id_str(&id_str, id);
1383 if (err)
1384 return err;
1386 err = got_object_commit_get_logmsg(&logmsg, commit);
1387 if (err)
1388 goto done;
1390 if (regexec(regex, got_object_commit_get_author(commit), 1,
1391 &regmatch, 0) == 0 ||
1392 regexec(regex, got_object_commit_get_committer(commit), 1,
1393 &regmatch, 0) == 0 ||
1394 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1395 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1396 *have_match = 1;
1397 done:
1398 free(id_str);
1399 free(logmsg);
1400 return err;
1403 static const struct got_error *
1404 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1405 int minqueue, struct got_repository *repo, const char *path,
1406 int *searching, int *search_next_done, regex_t *regex)
1408 const struct got_error *err = NULL;
1409 int nqueued = 0;
1412 * We keep all commits open throughout the lifetime of the log
1413 * view in order to avoid having to re-fetch commits from disk
1414 * while updating the display.
1416 while (nqueued < minqueue ||
1417 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1418 struct got_object_id *id;
1419 struct got_commit_object *commit;
1420 struct commit_queue_entry *entry;
1421 int errcode;
1423 err = got_commit_graph_iter_next(&id, graph, repo, NULL, NULL);
1424 if (err || id == NULL)
1425 break;
1427 err = got_object_open_as_commit(&commit, repo, id);
1428 if (err)
1429 break;
1430 entry = alloc_commit_queue_entry(commit, id);
1431 if (entry == NULL) {
1432 err = got_error_from_errno("alloc_commit_queue_entry");
1433 break;
1436 errcode = pthread_mutex_lock(&tog_mutex);
1437 if (errcode) {
1438 err = got_error_set_errno(errcode,
1439 "pthread_mutex_lock");
1440 break;
1443 entry->idx = commits->ncommits;
1444 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1445 nqueued++;
1446 commits->ncommits++;
1448 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1449 int have_match;
1450 err = match_commit(&have_match, id, commit, regex);
1451 if (err)
1452 break;
1453 if (have_match)
1454 *search_next_done = TOG_SEARCH_HAVE_MORE;
1457 errcode = pthread_mutex_unlock(&tog_mutex);
1458 if (errcode && err == NULL)
1459 err = got_error_set_errno(errcode,
1460 "pthread_mutex_unlock");
1461 if (err)
1462 break;
1465 return err;
1468 static const struct got_error *
1469 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1470 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1471 struct commit_queue *commits, int selected_idx, int limit,
1472 struct got_reflist_head *refs, const char *path, int commits_needed,
1473 struct tog_colors *colors)
1475 const struct got_error *err = NULL;
1476 struct tog_log_view_state *s = &view->state.log;
1477 struct commit_queue_entry *entry;
1478 int width;
1479 int ncommits, author_cols = 4;
1480 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1481 char *refs_str = NULL;
1482 wchar_t *wline;
1483 struct tog_color *tc;
1484 static const size_t date_display_cols = 12;
1486 entry = first;
1487 ncommits = 0;
1488 while (entry) {
1489 if (ncommits == selected_idx) {
1490 *selected = entry;
1491 break;
1493 entry = TAILQ_NEXT(entry, entry);
1494 ncommits++;
1497 if (*selected && !(view->searching && view->search_next_done == 0)) {
1498 err = got_object_id_str(&id_str, (*selected)->id);
1499 if (err)
1500 return err;
1501 if (refs) {
1502 err = build_refs_str(&refs_str, refs, (*selected)->id,
1503 s->repo);
1504 if (err)
1505 goto done;
1509 if (commits_needed == 0)
1510 halfdelay(10); /* disable fast refresh */
1512 if (commits_needed > 0) {
1513 if (asprintf(&ncommits_str, " [%d/%d] %s",
1514 entry ? entry->idx + 1 : 0, commits->ncommits,
1515 (view->searching && !view->search_next_done) ?
1516 "searching..." : "loading...") == -1) {
1517 err = got_error_from_errno("asprintf");
1518 goto done;
1520 } else {
1521 const char *search_str = NULL;
1523 if (view->searching) {
1524 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1525 search_str = "no more matches";
1526 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1527 search_str = "no matches found";
1528 else if (!view->search_next_done)
1529 search_str = "searching...";
1532 if (asprintf(&ncommits_str, " [%d/%d] %s",
1533 entry ? entry->idx + 1 : 0, commits->ncommits,
1534 search_str ? search_str :
1535 (refs_str ? refs_str : "")) == -1) {
1536 err = got_error_from_errno("asprintf");
1537 goto done;
1541 if (path && strcmp(path, "/") != 0) {
1542 if (asprintf(&header, "commit %s %s%s",
1543 id_str ? id_str : "........................................",
1544 path, ncommits_str) == -1) {
1545 err = got_error_from_errno("asprintf");
1546 header = NULL;
1547 goto done;
1549 } else if (asprintf(&header, "commit %s%s",
1550 id_str ? id_str : "........................................",
1551 ncommits_str) == -1) {
1552 err = got_error_from_errno("asprintf");
1553 header = NULL;
1554 goto done;
1556 err = format_line(&wline, &width, header, view->ncols, 0);
1557 if (err)
1558 goto done;
1560 werase(view->window);
1562 if (view_needs_focus_indication(view))
1563 wstandout(view->window);
1564 tc = get_color(colors, TOG_COLOR_COMMIT);
1565 if (tc)
1566 wattr_on(view->window,
1567 COLOR_PAIR(tc->colorpair), NULL);
1568 waddwstr(view->window, wline);
1569 if (tc)
1570 wattr_off(view->window,
1571 COLOR_PAIR(tc->colorpair), NULL);
1572 while (width < view->ncols) {
1573 waddch(view->window, ' ');
1574 width++;
1576 if (view_needs_focus_indication(view))
1577 wstandend(view->window);
1578 free(wline);
1579 if (limit <= 1)
1580 goto done;
1582 /* Grow author column size if necessary. */
1583 entry = first;
1584 ncommits = 0;
1585 while (entry) {
1586 char *author;
1587 wchar_t *wauthor;
1588 int width;
1589 if (ncommits >= limit - 1)
1590 break;
1591 author = strdup(got_object_commit_get_author(entry->commit));
1592 if (author == NULL) {
1593 err = got_error_from_errno("strdup");
1594 goto done;
1596 err = format_author(&wauthor, &width, author, COLS,
1597 date_display_cols);
1598 if (author_cols < width)
1599 author_cols = width;
1600 free(wauthor);
1601 free(author);
1602 ncommits++;
1603 entry = TAILQ_NEXT(entry, entry);
1606 entry = first;
1607 *last = first;
1608 ncommits = 0;
1609 while (entry) {
1610 if (ncommits >= limit - 1)
1611 break;
1612 if (ncommits == selected_idx)
1613 wstandout(view->window);
1614 err = draw_commit(view, entry->commit, entry->id, refs,
1615 date_display_cols, author_cols, colors);
1616 if (ncommits == selected_idx)
1617 wstandend(view->window);
1618 if (err)
1619 goto done;
1620 ncommits++;
1621 *last = entry;
1622 entry = TAILQ_NEXT(entry, entry);
1625 view_vborder(view);
1626 done:
1627 free(id_str);
1628 free(refs_str);
1629 free(ncommits_str);
1630 free(header);
1631 return err;
1634 static void
1635 scroll_up(struct tog_view *view,
1636 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1637 struct commit_queue *commits)
1639 struct commit_queue_entry *entry;
1640 int nscrolled = 0;
1642 entry = TAILQ_FIRST(&commits->head);
1643 if (*first_displayed_entry == entry)
1644 return;
1646 entry = *first_displayed_entry;
1647 while (entry && nscrolled < maxscroll) {
1648 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1649 if (entry) {
1650 *first_displayed_entry = entry;
1651 nscrolled++;
1656 static const struct got_error *
1657 trigger_log_thread(struct tog_view *log_view, int wait,
1658 int *commits_needed, int *log_complete,
1659 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1661 int errcode;
1663 halfdelay(1); /* fast refresh while loading commits */
1665 while (*commits_needed > 0) {
1666 if (*log_complete)
1667 break;
1669 /* Wake the log thread. */
1670 errcode = pthread_cond_signal(need_commits);
1671 if (errcode)
1672 return got_error_set_errno(errcode,
1673 "pthread_cond_signal");
1676 * The mutex will be released while the view loop waits
1677 * in wgetch(), at which time the log thread will run.
1679 if (!wait)
1680 break;
1682 /* Display progress update in log view. */
1683 show_log_view(log_view);
1684 update_panels();
1685 doupdate();
1687 /* Wait right here while next commit is being loaded. */
1688 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1689 if (errcode)
1690 return got_error_set_errno(errcode,
1691 "pthread_cond_wait");
1693 /* Display progress update in log view. */
1694 show_log_view(log_view);
1695 update_panels();
1696 doupdate();
1699 return NULL;
1702 static const struct got_error *
1703 scroll_down(struct tog_view *log_view,
1704 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1705 struct commit_queue_entry **last_displayed_entry,
1706 struct commit_queue *commits, int *log_complete, int *commits_needed,
1707 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1709 const struct got_error *err = NULL;
1710 struct commit_queue_entry *pentry;
1711 int nscrolled = 0, ncommits_needed;
1713 if (*last_displayed_entry == NULL)
1714 return NULL;
1716 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1717 if (commits->ncommits < ncommits_needed && !*log_complete) {
1719 * Ask the log thread for required amount of commits.
1721 (*commits_needed) += maxscroll;
1722 err = trigger_log_thread(log_view, 1, commits_needed,
1723 log_complete, need_commits, commit_loaded);
1724 if (err)
1725 return err;
1728 do {
1729 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1730 if (pentry == NULL)
1731 break;
1733 *last_displayed_entry = pentry;
1735 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1736 if (pentry == NULL)
1737 break;
1738 *first_displayed_entry = pentry;
1739 } while (++nscrolled < maxscroll);
1741 return err;
1744 static const struct got_error *
1745 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1746 struct got_commit_object *commit, struct got_object_id *commit_id,
1747 struct tog_view *log_view, struct got_reflist_head *refs,
1748 struct got_repository *repo)
1750 const struct got_error *err;
1751 struct got_object_qid *parent_id;
1752 struct tog_view *diff_view;
1754 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1755 if (diff_view == NULL)
1756 return got_error_from_errno("view_open");
1758 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1759 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1760 commit_id, log_view, refs, repo);
1761 if (err == NULL)
1762 *new_view = diff_view;
1763 return err;
1766 static const struct got_error *
1767 tree_view_visit_subtree(struct got_tree_object *subtree,
1768 struct tog_tree_view_state *s)
1770 struct tog_parent_tree *parent;
1772 parent = calloc(1, sizeof(*parent));
1773 if (parent == NULL)
1774 return got_error_from_errno("calloc");
1776 parent->tree = s->tree;
1777 parent->first_displayed_entry = s->first_displayed_entry;
1778 parent->selected_entry = s->selected_entry;
1779 parent->selected = s->selected;
1780 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1781 s->tree = subtree;
1782 s->selected = 0;
1783 s->first_displayed_entry = NULL;
1784 return NULL;
1787 static const struct got_error *
1788 tree_view_walk_path(struct tog_tree_view_state *s,
1789 struct got_object_id *commit_id,
1790 const char *path, struct got_repository *repo)
1792 const struct got_error *err = NULL;
1793 struct got_tree_object *tree = NULL;
1794 const char *p;
1795 char *slash, *subpath = NULL;
1797 /* Walk the path and open corresponding tree objects. */
1798 p = path;
1799 while (*p) {
1800 struct got_tree_entry *te;
1801 struct got_object_id *tree_id;
1802 char *te_name;
1804 while (p[0] == '/')
1805 p++;
1807 /* Ensure the correct subtree entry is selected. */
1808 slash = strchr(p, '/');
1809 if (slash == NULL)
1810 te_name = strdup(p);
1811 else
1812 te_name = strndup(p, slash - p);
1813 if (te_name == NULL) {
1814 err = got_error_from_errno("strndup");
1815 break;
1817 te = got_object_tree_find_entry(s->tree, te_name);
1818 if (te == NULL) {
1819 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1820 free(te_name);
1821 break;
1823 free(te_name);
1824 s->selected_entry = te;
1825 s->selected = got_tree_entry_get_index(te);
1826 if (s->tree != s->root)
1827 s->selected++; /* skip '..' */
1829 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1830 /* Jump to this file's entry. */
1831 s->first_displayed_entry = s->selected_entry;
1832 s->selected = 0;
1833 break;
1836 slash = strchr(p, '/');
1837 if (slash)
1838 subpath = strndup(path, slash - path);
1839 else
1840 subpath = strdup(path);
1841 if (subpath == NULL) {
1842 err = got_error_from_errno("strdup");
1843 break;
1846 err = got_object_id_by_path(&tree_id, repo, commit_id,
1847 subpath);
1848 if (err)
1849 break;
1851 err = got_object_open_as_tree(&tree, repo, tree_id);
1852 free(tree_id);
1853 if (err)
1854 break;
1856 err = tree_view_visit_subtree(tree, s);
1857 if (err) {
1858 got_object_tree_close(tree);
1859 break;
1861 if (slash == NULL)
1862 break;
1863 free(subpath);
1864 subpath = NULL;
1865 p = slash;
1868 free(subpath);
1869 return err;
1872 static const struct got_error *
1873 browse_commit_tree(struct tog_view **new_view, int begin_x,
1874 struct commit_queue_entry *entry, const char *path,
1875 struct got_reflist_head *refs, struct got_repository *repo)
1877 const struct got_error *err = NULL;
1878 struct got_tree_object *tree;
1879 struct tog_tree_view_state *s;
1880 struct tog_view *tree_view;
1882 err = got_object_open_as_tree(&tree, repo,
1883 got_object_commit_get_tree_id(entry->commit));
1884 if (err)
1885 return err;
1887 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1888 if (tree_view == NULL)
1889 return got_error_from_errno("view_open");
1891 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1892 if (err) {
1893 got_object_tree_close(tree);
1894 return err;
1896 s = &tree_view->state.tree;
1898 *new_view = tree_view;
1900 if (got_path_is_root_dir(path))
1901 return NULL;
1903 return tree_view_walk_path(s, entry->id, path, repo);
1906 static const struct got_error *
1907 block_signals_used_by_main_thread(void)
1909 sigset_t sigset;
1910 int errcode;
1912 if (sigemptyset(&sigset) == -1)
1913 return got_error_from_errno("sigemptyset");
1915 /* tog handles SIGWINCH and SIGCONT */
1916 if (sigaddset(&sigset, SIGWINCH) == -1)
1917 return got_error_from_errno("sigaddset");
1918 if (sigaddset(&sigset, SIGCONT) == -1)
1919 return got_error_from_errno("sigaddset");
1921 /* ncurses handles SIGTSTP */
1922 if (sigaddset(&sigset, SIGTSTP) == -1)
1923 return got_error_from_errno("sigaddset");
1925 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1926 if (errcode)
1927 return got_error_set_errno(errcode, "pthread_sigmask");
1929 return NULL;
1932 static void *
1933 log_thread(void *arg)
1935 const struct got_error *err = NULL;
1936 int errcode = 0;
1937 struct tog_log_thread_args *a = arg;
1938 int done = 0;
1940 err = block_signals_used_by_main_thread();
1941 if (err)
1942 return (void *)err;
1944 while (!done && !err && !tog_sigpipe_received) {
1945 err = queue_commits(a->graph, a->commits, 1, a->repo,
1946 a->in_repo_path, a->searching, a->search_next_done,
1947 a->regex);
1948 if (err) {
1949 if (err->code != GOT_ERR_ITER_COMPLETED)
1950 return (void *)err;
1951 err = NULL;
1952 done = 1;
1953 } else if (a->commits_needed > 0)
1954 a->commits_needed--;
1956 errcode = pthread_mutex_lock(&tog_mutex);
1957 if (errcode) {
1958 err = got_error_set_errno(errcode,
1959 "pthread_mutex_lock");
1960 break;
1961 } else if (*a->quit)
1962 done = 1;
1963 else if (*a->first_displayed_entry == NULL) {
1964 *a->first_displayed_entry =
1965 TAILQ_FIRST(&a->commits->head);
1966 *a->selected_entry = *a->first_displayed_entry;
1969 errcode = pthread_cond_signal(&a->commit_loaded);
1970 if (errcode) {
1971 err = got_error_set_errno(errcode,
1972 "pthread_cond_signal");
1973 pthread_mutex_unlock(&tog_mutex);
1974 break;
1977 if (done)
1978 a->commits_needed = 0;
1979 else {
1980 if (a->commits_needed == 0) {
1981 errcode = pthread_cond_wait(&a->need_commits,
1982 &tog_mutex);
1983 if (errcode)
1984 err = got_error_set_errno(errcode,
1985 "pthread_cond_wait");
1989 errcode = pthread_mutex_unlock(&tog_mutex);
1990 if (errcode && err == NULL)
1991 err = got_error_set_errno(errcode,
1992 "pthread_mutex_unlock");
1994 a->log_complete = 1;
1995 return (void *)err;
1998 static const struct got_error *
1999 stop_log_thread(struct tog_log_view_state *s)
2001 const struct got_error *err = NULL;
2002 int errcode;
2004 if (s->thread) {
2005 s->quit = 1;
2006 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2007 if (errcode)
2008 return got_error_set_errno(errcode,
2009 "pthread_cond_signal");
2010 errcode = pthread_mutex_unlock(&tog_mutex);
2011 if (errcode)
2012 return got_error_set_errno(errcode,
2013 "pthread_mutex_unlock");
2014 errcode = pthread_join(s->thread, (void **)&err);
2015 if (errcode)
2016 return got_error_set_errno(errcode, "pthread_join");
2017 errcode = pthread_mutex_lock(&tog_mutex);
2018 if (errcode)
2019 return got_error_set_errno(errcode,
2020 "pthread_mutex_lock");
2021 s->thread = NULL;
2024 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2025 if (errcode && err == NULL)
2026 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2028 if (s->thread_args.repo) {
2029 got_repo_close(s->thread_args.repo);
2030 s->thread_args.repo = NULL;
2033 if (s->thread_args.graph) {
2034 got_commit_graph_close(s->thread_args.graph);
2035 s->thread_args.graph = NULL;
2038 return err;
2041 static const struct got_error *
2042 close_log_view(struct tog_view *view)
2044 const struct got_error *err = NULL;
2045 struct tog_log_view_state *s = &view->state.log;
2047 err = stop_log_thread(s);
2048 free_commits(&s->commits);
2049 free(s->in_repo_path);
2050 s->in_repo_path = NULL;
2051 free(s->start_id);
2052 s->start_id = NULL;
2053 return err;
2056 static const struct got_error *
2057 search_start_log_view(struct tog_view *view)
2059 struct tog_log_view_state *s = &view->state.log;
2061 s->matched_entry = NULL;
2062 s->search_entry = NULL;
2063 return NULL;
2066 static const struct got_error *
2067 search_next_log_view(struct tog_view *view)
2069 const struct got_error *err = NULL;
2070 struct tog_log_view_state *s = &view->state.log;
2071 struct commit_queue_entry *entry;
2073 /* Display progress update in log view. */
2074 show_log_view(view);
2075 update_panels();
2076 doupdate();
2078 if (s->search_entry) {
2079 int errcode, ch;
2080 errcode = pthread_mutex_unlock(&tog_mutex);
2081 if (errcode)
2082 return got_error_set_errno(errcode,
2083 "pthread_mutex_unlock");
2084 ch = wgetch(view->window);
2085 errcode = pthread_mutex_lock(&tog_mutex);
2086 if (errcode)
2087 return got_error_set_errno(errcode,
2088 "pthread_mutex_lock");
2089 if (ch == KEY_BACKSPACE) {
2090 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2091 return NULL;
2093 if (view->searching == TOG_SEARCH_FORWARD)
2094 entry = TAILQ_NEXT(s->search_entry, entry);
2095 else
2096 entry = TAILQ_PREV(s->search_entry,
2097 commit_queue_head, entry);
2098 } else if (s->matched_entry) {
2099 if (view->searching == TOG_SEARCH_FORWARD)
2100 entry = TAILQ_NEXT(s->matched_entry, entry);
2101 else
2102 entry = TAILQ_PREV(s->matched_entry,
2103 commit_queue_head, entry);
2104 } else {
2105 if (view->searching == TOG_SEARCH_FORWARD)
2106 entry = TAILQ_FIRST(&s->commits.head);
2107 else
2108 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2111 while (1) {
2112 int have_match = 0;
2114 if (entry == NULL) {
2115 if (s->thread_args.log_complete ||
2116 view->searching == TOG_SEARCH_BACKWARD) {
2117 view->search_next_done =
2118 (s->matched_entry == NULL ?
2119 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2120 s->search_entry = NULL;
2121 return NULL;
2124 * Poke the log thread for more commits and return,
2125 * allowing the main loop to make progress. Search
2126 * will resume at s->search_entry once we come back.
2128 s->thread_args.commits_needed++;
2129 return trigger_log_thread(view, 0,
2130 &s->thread_args.commits_needed,
2131 &s->thread_args.log_complete,
2132 &s->thread_args.need_commits,
2133 &s->thread_args.commit_loaded);
2136 err = match_commit(&have_match, entry->id, entry->commit,
2137 &view->regex);
2138 if (err)
2139 break;
2140 if (have_match) {
2141 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2142 s->matched_entry = entry;
2143 break;
2146 s->search_entry = entry;
2147 if (view->searching == TOG_SEARCH_FORWARD)
2148 entry = TAILQ_NEXT(entry, entry);
2149 else
2150 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2153 if (s->matched_entry) {
2154 int cur = s->selected_entry->idx;
2155 while (cur < s->matched_entry->idx) {
2156 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2157 if (err)
2158 return err;
2159 cur++;
2161 while (cur > s->matched_entry->idx) {
2162 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2163 if (err)
2164 return err;
2165 cur--;
2169 s->search_entry = NULL;
2171 return NULL;
2174 static const struct got_error *
2175 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2176 struct got_reflist_head *refs, struct got_repository *repo,
2177 const char *head_ref_name, const char *in_repo_path,
2178 int log_branches)
2180 const struct got_error *err = NULL;
2181 struct tog_log_view_state *s = &view->state.log;
2182 struct got_repository *thread_repo = NULL;
2183 struct got_commit_graph *thread_graph = NULL;
2184 int errcode;
2186 if (in_repo_path != s->in_repo_path) {
2187 free(s->in_repo_path);
2188 s->in_repo_path = strdup(in_repo_path);
2189 if (s->in_repo_path == NULL)
2190 return got_error_from_errno("strdup");
2193 /* The commit queue only contains commits being displayed. */
2194 TAILQ_INIT(&s->commits.head);
2195 s->commits.ncommits = 0;
2197 s->refs = refs;
2198 s->repo = repo;
2199 s->head_ref_name = head_ref_name;
2200 s->start_id = got_object_id_dup(start_id);
2201 if (s->start_id == NULL) {
2202 err = got_error_from_errno("got_object_id_dup");
2203 goto done;
2205 s->log_branches = log_branches;
2207 SIMPLEQ_INIT(&s->colors);
2208 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2209 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2210 get_color_value("TOG_COLOR_COMMIT"));
2211 if (err)
2212 goto done;
2213 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2214 get_color_value("TOG_COLOR_AUTHOR"));
2215 if (err) {
2216 free_colors(&s->colors);
2217 goto done;
2219 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2220 get_color_value("TOG_COLOR_DATE"));
2221 if (err) {
2222 free_colors(&s->colors);
2223 goto done;
2227 view->show = show_log_view;
2228 view->input = input_log_view;
2229 view->close = close_log_view;
2230 view->search_start = search_start_log_view;
2231 view->search_next = search_next_log_view;
2233 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2234 if (err)
2235 goto done;
2236 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2237 !s->log_branches);
2238 if (err)
2239 goto done;
2240 err = got_commit_graph_iter_start(thread_graph,
2241 s->start_id, s->repo, NULL, NULL);
2242 if (err)
2243 goto done;
2245 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2246 if (errcode) {
2247 err = got_error_set_errno(errcode, "pthread_cond_init");
2248 goto done;
2250 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2251 if (errcode) {
2252 err = got_error_set_errno(errcode, "pthread_cond_init");
2253 goto done;
2256 s->thread_args.commits_needed = view->nlines;
2257 s->thread_args.graph = thread_graph;
2258 s->thread_args.commits = &s->commits;
2259 s->thread_args.in_repo_path = s->in_repo_path;
2260 s->thread_args.start_id = s->start_id;
2261 s->thread_args.repo = thread_repo;
2262 s->thread_args.log_complete = 0;
2263 s->thread_args.quit = &s->quit;
2264 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2265 s->thread_args.selected_entry = &s->selected_entry;
2266 s->thread_args.searching = &view->searching;
2267 s->thread_args.search_next_done = &view->search_next_done;
2268 s->thread_args.regex = &view->regex;
2269 done:
2270 if (err)
2271 close_log_view(view);
2272 return err;
2275 static const struct got_error *
2276 show_log_view(struct tog_view *view)
2278 struct tog_log_view_state *s = &view->state.log;
2280 if (s->thread == NULL) {
2281 int errcode = pthread_create(&s->thread, NULL, log_thread,
2282 &s->thread_args);
2283 if (errcode)
2284 return got_error_set_errno(errcode, "pthread_create");
2287 return draw_commits(view, &s->last_displayed_entry,
2288 &s->selected_entry, s->first_displayed_entry,
2289 &s->commits, s->selected, view->nlines, s->refs,
2290 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2293 static const struct got_error *
2294 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2295 struct tog_view **focus_view, struct tog_view *view, int ch)
2297 const struct got_error *err = NULL;
2298 struct tog_log_view_state *s = &view->state.log;
2299 char *parent_path, *in_repo_path = NULL;
2300 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2301 int begin_x = 0;
2302 struct got_object_id *start_id;
2304 switch (ch) {
2305 case 'q':
2306 s->quit = 1;
2307 break;
2308 case 'k':
2309 case KEY_UP:
2310 case '<':
2311 case ',':
2312 if (s->first_displayed_entry == NULL)
2313 break;
2314 if (s->selected > 0)
2315 s->selected--;
2316 else
2317 scroll_up(view, &s->first_displayed_entry, 1,
2318 &s->commits);
2319 break;
2320 case KEY_PPAGE:
2321 case CTRL('b'):
2322 if (s->first_displayed_entry == NULL)
2323 break;
2324 if (TAILQ_FIRST(&s->commits.head) ==
2325 s->first_displayed_entry) {
2326 s->selected = 0;
2327 break;
2329 scroll_up(view, &s->first_displayed_entry,
2330 view->nlines - 1, &s->commits);
2331 break;
2332 case 'j':
2333 case KEY_DOWN:
2334 case '>':
2335 case '.':
2336 if (s->first_displayed_entry == NULL)
2337 break;
2338 if (s->selected < MIN(view->nlines - 2,
2339 s->commits.ncommits - 1)) {
2340 s->selected++;
2341 break;
2343 err = scroll_down(view, &s->first_displayed_entry, 1,
2344 &s->last_displayed_entry, &s->commits,
2345 &s->thread_args.log_complete,
2346 &s->thread_args.commits_needed,
2347 &s->thread_args.need_commits,
2348 &s->thread_args.commit_loaded);
2349 break;
2350 case KEY_NPAGE:
2351 case CTRL('f'): {
2352 struct commit_queue_entry *first;
2353 first = s->first_displayed_entry;
2354 if (first == NULL)
2355 break;
2356 err = scroll_down(view, &s->first_displayed_entry,
2357 view->nlines - 1, &s->last_displayed_entry,
2358 &s->commits, &s->thread_args.log_complete,
2359 &s->thread_args.commits_needed,
2360 &s->thread_args.need_commits,
2361 &s->thread_args.commit_loaded);
2362 if (err)
2363 break;
2364 if (first == s->first_displayed_entry &&
2365 s->selected < MIN(view->nlines - 2,
2366 s->commits.ncommits - 1)) {
2367 /* can't scroll further down */
2368 s->selected = MIN(view->nlines - 2,
2369 s->commits.ncommits - 1);
2371 err = NULL;
2372 break;
2374 case KEY_RESIZE:
2375 if (s->selected > view->nlines - 2)
2376 s->selected = view->nlines - 2;
2377 if (s->selected > s->commits.ncommits - 1)
2378 s->selected = s->commits.ncommits - 1;
2379 break;
2380 case KEY_ENTER:
2381 case ' ':
2382 case '\r':
2383 if (s->selected_entry == NULL)
2384 break;
2385 if (view_is_parent_view(view))
2386 begin_x = view_split_begin_x(view->begin_x);
2387 err = open_diff_view_for_commit(&diff_view, begin_x,
2388 s->selected_entry->commit, s->selected_entry->id,
2389 view, s->refs, s->repo);
2390 if (err)
2391 break;
2392 if (view_is_parent_view(view)) {
2393 err = view_close_child(view);
2394 if (err)
2395 return err;
2396 err = view_set_child(view, diff_view);
2397 if (err) {
2398 view_close(diff_view);
2399 break;
2401 *focus_view = diff_view;
2402 view->child_focussed = 1;
2403 } else
2404 *new_view = diff_view;
2405 break;
2406 case 't':
2407 if (s->selected_entry == NULL)
2408 break;
2409 if (view_is_parent_view(view))
2410 begin_x = view_split_begin_x(view->begin_x);
2411 err = browse_commit_tree(&tree_view, begin_x,
2412 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2413 if (err)
2414 break;
2415 if (view_is_parent_view(view)) {
2416 err = view_close_child(view);
2417 if (err)
2418 return err;
2419 err = view_set_child(view, tree_view);
2420 if (err) {
2421 view_close(tree_view);
2422 break;
2424 *focus_view = tree_view;
2425 view->child_focussed = 1;
2426 } else
2427 *new_view = tree_view;
2428 break;
2429 case KEY_BACKSPACE:
2430 if (strcmp(s->in_repo_path, "/") == 0)
2431 break;
2432 parent_path = dirname(s->in_repo_path);
2433 if (parent_path && strcmp(parent_path, ".") != 0) {
2434 err = stop_log_thread(s);
2435 if (err)
2436 return err;
2437 lv = view_open(view->nlines, view->ncols,
2438 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2439 if (lv == NULL)
2440 return got_error_from_errno(
2441 "view_open");
2442 err = open_log_view(lv, s->start_id, s->refs,
2443 s->repo, s->head_ref_name, parent_path,
2444 s->log_branches);
2445 if (err)
2446 return err;;
2447 if (view_is_parent_view(view))
2448 *new_view = lv;
2449 else {
2450 view_set_child(view->parent, lv);
2451 *focus_view = lv;
2453 return NULL;
2455 break;
2456 case CTRL('l'):
2457 err = stop_log_thread(s);
2458 if (err)
2459 return err;
2460 lv = view_open(view->nlines, view->ncols,
2461 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2462 if (lv == NULL)
2463 return got_error_from_errno("view_open");
2464 err = got_repo_match_object_id(&start_id, NULL,
2465 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2466 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2467 if (err) {
2468 view_close(lv);
2469 return err;
2471 in_repo_path = strdup(s->in_repo_path);
2472 if (in_repo_path == NULL) {
2473 free(start_id);
2474 view_close(lv);
2475 return got_error_from_errno("strdup");
2477 got_ref_list_free(s->refs);
2478 err = got_ref_list(s->refs, s->repo, NULL,
2479 got_ref_cmp_by_name, NULL);
2480 if (err) {
2481 free(start_id);
2482 view_close(lv);
2483 return err;
2485 err = open_log_view(lv, start_id, s->refs, s->repo,
2486 s->head_ref_name, in_repo_path, s->log_branches);
2487 if (err) {
2488 free(start_id);
2489 view_close(lv);
2490 return err;;
2492 *dead_view = view;
2493 *new_view = lv;
2494 break;
2495 case 'B':
2496 s->log_branches = !s->log_branches;
2497 err = stop_log_thread(s);
2498 if (err)
2499 return err;
2500 lv = view_open(view->nlines, view->ncols,
2501 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2502 if (lv == NULL)
2503 return got_error_from_errno("view_open");
2504 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2505 s->head_ref_name, s->in_repo_path, s->log_branches);
2506 if (err) {
2507 view_close(lv);
2508 return err;;
2510 *dead_view = view;
2511 *new_view = lv;
2512 break;
2513 default:
2514 break;
2517 return err;
2520 static const struct got_error *
2521 apply_unveil(const char *repo_path, const char *worktree_path)
2523 const struct got_error *error;
2525 #ifdef PROFILE
2526 if (unveil("gmon.out", "rwc") != 0)
2527 return got_error_from_errno2("unveil", "gmon.out");
2528 #endif
2529 if (repo_path && unveil(repo_path, "r") != 0)
2530 return got_error_from_errno2("unveil", repo_path);
2532 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2533 return got_error_from_errno2("unveil", worktree_path);
2535 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2536 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2538 error = got_privsep_unveil_exec_helpers();
2539 if (error != NULL)
2540 return error;
2542 if (unveil(NULL, NULL) != 0)
2543 return got_error_from_errno("unveil");
2545 return NULL;
2548 static void
2549 init_curses(void)
2551 initscr();
2552 cbreak();
2553 halfdelay(1); /* Do fast refresh while initial view is loading. */
2554 noecho();
2555 nonl();
2556 intrflush(stdscr, FALSE);
2557 keypad(stdscr, TRUE);
2558 curs_set(0);
2559 if (getenv("TOG_COLORS") != NULL) {
2560 start_color();
2561 use_default_colors();
2563 signal(SIGWINCH, tog_sigwinch);
2564 signal(SIGPIPE, tog_sigpipe);
2565 signal(SIGCONT, tog_sigcont);
2568 static const struct got_error *
2569 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2570 struct got_repository *repo, struct got_worktree *worktree)
2572 const struct got_error *err = NULL;
2574 if (argc == 0) {
2575 *in_repo_path = strdup("/");
2576 if (*in_repo_path == NULL)
2577 return got_error_from_errno("strdup");
2578 return NULL;
2581 if (worktree) {
2582 const char *prefix = got_worktree_get_path_prefix(worktree);
2583 char *wt_path, *p;
2585 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2586 if (err)
2587 return err;
2589 if (asprintf(&p, "%s%s%s", prefix,
2590 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2591 err = got_error_from_errno("asprintf");
2592 free(wt_path);
2593 return err;
2595 err = got_repo_map_path(in_repo_path, repo, p, 0);
2596 free(p);
2597 free(wt_path);
2598 } else
2599 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2601 return err;
2604 static const struct got_error *
2605 cmd_log(int argc, char *argv[])
2607 const struct got_error *error;
2608 struct got_repository *repo = NULL;
2609 struct got_worktree *worktree = NULL;
2610 struct got_reflist_head refs;
2611 struct got_object_id *start_id = NULL;
2612 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2613 char *start_commit = NULL, *head_ref_name = NULL;
2614 int ch, log_branches = 0;
2615 struct tog_view *view;
2617 SIMPLEQ_INIT(&refs);
2619 #ifndef PROFILE
2620 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2621 NULL) == -1)
2622 err(1, "pledge");
2623 #endif
2625 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2626 switch (ch) {
2627 case 'b':
2628 log_branches = 1;
2629 break;
2630 case 'c':
2631 start_commit = optarg;
2632 break;
2633 case 'r':
2634 repo_path = realpath(optarg, NULL);
2635 if (repo_path == NULL)
2636 return got_error_from_errno2("realpath",
2637 optarg);
2638 break;
2639 default:
2640 usage_log();
2641 /* NOTREACHED */
2645 argc -= optind;
2646 argv += optind;
2648 if (argc > 1)
2649 usage_log();
2651 cwd = getcwd(NULL, 0);
2652 if (cwd == NULL)
2653 return got_error_from_errno("getcwd");
2655 error = got_worktree_open(&worktree, cwd);
2656 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2657 goto done;
2659 if (repo_path == NULL) {
2660 if (worktree)
2661 repo_path =
2662 strdup(got_worktree_get_repo_path(worktree));
2663 else
2664 repo_path = strdup(cwd);
2666 if (repo_path == NULL) {
2667 error = got_error_from_errno("strdup");
2668 goto done;
2671 error = got_repo_open(&repo, repo_path, NULL);
2672 if (error != NULL)
2673 goto done;
2675 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2676 repo, worktree);
2677 if (error)
2678 goto done;
2680 init_curses();
2682 error = apply_unveil(got_repo_get_path(repo),
2683 worktree ? got_worktree_get_root_path(worktree) : NULL);
2684 if (error)
2685 goto done;
2687 if (start_commit == NULL)
2688 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2689 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2690 GOT_OBJ_TYPE_COMMIT, 1, repo);
2691 else
2692 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2693 GOT_OBJ_TYPE_COMMIT, 1, repo);
2694 if (error != NULL)
2695 goto done;
2697 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2698 if (error)
2699 goto done;
2701 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2702 if (view == NULL) {
2703 error = got_error_from_errno("view_open");
2704 goto done;
2706 if (worktree) {
2707 head_ref_name = strdup(
2708 got_worktree_get_head_ref_name(worktree));
2709 if (head_ref_name == NULL) {
2710 error = got_error_from_errno("strdup");
2711 goto done;
2714 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2715 in_repo_path, log_branches);
2716 if (error)
2717 goto done;
2718 if (worktree) {
2719 /* Release work tree lock. */
2720 got_worktree_close(worktree);
2721 worktree = NULL;
2723 error = view_loop(view);
2724 done:
2725 free(in_repo_path);
2726 free(repo_path);
2727 free(cwd);
2728 free(start_id);
2729 free(head_ref_name);
2730 if (repo)
2731 got_repo_close(repo);
2732 if (worktree)
2733 got_worktree_close(worktree);
2734 got_ref_list_free(&refs);
2735 return error;
2738 __dead static void
2739 usage_diff(void)
2741 endwin();
2742 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2743 getprogname());
2744 exit(1);
2747 static char *
2748 parse_next_line(FILE *f, size_t *len)
2750 char *line;
2751 size_t linelen;
2752 size_t lineno;
2753 const char delim[3] = { '\0', '\0', '\0'};
2755 line = fparseln(f, &linelen, &lineno, delim, 0);
2756 if (len)
2757 *len = linelen;
2758 return line;
2761 static int
2762 match_line(const char *line, regex_t *regex)
2764 regmatch_t regmatch;
2766 return regexec(regex, line, 1, &regmatch, 0) == 0;
2769 struct tog_color *
2770 match_color(struct tog_colors *colors, const char *line)
2772 struct tog_color *tc = NULL;
2774 SIMPLEQ_FOREACH(tc, colors, entry) {
2775 if (match_line(line, &tc->regex))
2776 return tc;
2779 return NULL;
2782 static const struct got_error *
2783 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2784 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2785 char *header, struct tog_colors *colors)
2787 const struct got_error *err;
2788 int lineno = 0, nprinted = 0;
2789 char *line;
2790 struct tog_color *tc;
2791 size_t len;
2792 wchar_t *wline;
2793 int width;
2795 rewind(f);
2796 werase(view->window);
2798 if (header) {
2799 err = format_line(&wline, &width, header, view->ncols, 0);
2800 if (err) {
2801 return err;
2804 if (view_needs_focus_indication(view))
2805 wstandout(view->window);
2806 waddwstr(view->window, wline);
2807 if (view_needs_focus_indication(view))
2808 wstandend(view->window);
2809 if (width <= view->ncols - 1)
2810 waddch(view->window, '\n');
2812 if (max_lines <= 1)
2813 return NULL;
2814 max_lines--;
2817 *eof = 0;
2818 while (nprinted < max_lines) {
2819 line = parse_next_line(f, &len);
2820 if (line == NULL) {
2821 *eof = 1;
2822 break;
2824 if (++lineno < *first_displayed_line) {
2825 free(line);
2826 continue;
2829 err = format_line(&wline, &width, line, view->ncols, 0);
2830 if (err) {
2831 free(line);
2832 return err;
2835 tc = match_color(colors, line);
2836 if (tc)
2837 wattr_on(view->window,
2838 COLOR_PAIR(tc->colorpair), NULL);
2839 waddwstr(view->window, wline);
2840 if (tc)
2841 wattr_off(view->window,
2842 COLOR_PAIR(tc->colorpair), NULL);
2843 if (width <= view->ncols - 1)
2844 waddch(view->window, '\n');
2845 if (++nprinted == 1)
2846 *first_displayed_line = lineno;
2847 free(line);
2848 free(wline);
2849 wline = NULL;
2851 *last_displayed_line = lineno;
2853 view_vborder(view);
2855 if (*eof) {
2856 while (nprinted < view->nlines) {
2857 waddch(view->window, '\n');
2858 nprinted++;
2861 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2862 if (err) {
2863 return err;
2866 wstandout(view->window);
2867 waddwstr(view->window, wline);
2868 wstandend(view->window);
2871 return NULL;
2874 static char *
2875 get_datestr(time_t *time, char *datebuf)
2877 struct tm mytm, *tm;
2878 char *p, *s;
2880 tm = gmtime_r(time, &mytm);
2881 if (tm == NULL)
2882 return NULL;
2883 s = asctime_r(tm, datebuf);
2884 if (s == NULL)
2885 return NULL;
2886 p = strchr(s, '\n');
2887 if (p)
2888 *p = '\0';
2889 return s;
2892 static const struct got_error *
2893 write_commit_info(struct got_object_id *commit_id,
2894 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2896 const struct got_error *err = NULL;
2897 char datebuf[26], *datestr;
2898 struct got_commit_object *commit;
2899 char *id_str = NULL, *logmsg = NULL;
2900 time_t committer_time;
2901 const char *author, *committer;
2902 char *refs_str = NULL;
2904 if (refs) {
2905 err = build_refs_str(&refs_str, refs, commit_id, repo);
2906 if (err)
2907 return err;
2910 err = got_object_open_as_commit(&commit, repo, commit_id);
2911 if (err)
2912 return err;
2914 err = got_object_id_str(&id_str, commit_id);
2915 if (err) {
2916 err = got_error_from_errno("got_object_id_str");
2917 goto done;
2920 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2921 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2922 err = got_error_from_errno("fprintf");
2923 goto done;
2925 if (fprintf(outfile, "from: %s\n",
2926 got_object_commit_get_author(commit)) < 0) {
2927 err = got_error_from_errno("fprintf");
2928 goto done;
2930 committer_time = got_object_commit_get_committer_time(commit);
2931 datestr = get_datestr(&committer_time, datebuf);
2932 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2933 err = got_error_from_errno("fprintf");
2934 goto done;
2936 author = got_object_commit_get_author(commit);
2937 committer = got_object_commit_get_committer(commit);
2938 if (strcmp(author, committer) != 0 &&
2939 fprintf(outfile, "via: %s\n", committer) < 0) {
2940 err = got_error_from_errno("fprintf");
2941 goto done;
2943 err = got_object_commit_get_logmsg(&logmsg, commit);
2944 if (err)
2945 goto done;
2946 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2947 err = got_error_from_errno("fprintf");
2948 goto done;
2950 done:
2951 free(id_str);
2952 free(logmsg);
2953 free(refs_str);
2954 got_object_commit_close(commit);
2955 return err;
2958 const struct got_error *
2959 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2960 FILE *infile)
2962 size_t len;
2963 char *buf = NULL;
2964 int i;
2965 size_t noffsets = 0;
2966 off_t off = 0;
2968 if (line_offsets)
2969 *line_offsets = NULL;
2970 if (filesize)
2971 *filesize = 0;
2972 if (nlines)
2973 *nlines = 0;
2975 if (fseek(infile, 0, SEEK_END) == -1)
2976 return got_error_from_errno("fseek");
2977 len = ftell(infile) + 1;
2978 if (ferror(infile))
2979 return got_error_from_errno("ftell");
2980 if (fseek(infile, 0, SEEK_SET) == -1)
2981 return got_error_from_errno("fseek");
2983 if (len == 0)
2984 return NULL;
2985 if ((buf = calloc(len, sizeof(char *))) == NULL)
2986 return got_error_from_errno("calloc");
2988 fread(buf, 1, len, infile);
2989 if (ferror(infile))
2990 return got_error_from_errno("fread");
2992 i = 0;
2993 if (line_offsets && nlines) {
2994 if (*line_offsets == NULL) {
2995 /* Have some data but perhaps no '\n'. */
2996 noffsets = 1;
2997 *nlines = 1;
2998 *line_offsets = calloc(1, sizeof(**line_offsets));
2999 if (*line_offsets == NULL)
3000 return got_error_from_errno("calloc");
3001 /* Skip forward over end of first line. */
3002 while (i < len) {
3003 if (buf[i] == '\n')
3004 break;
3005 i++;
3008 /* Scan '\n' offsets in remaining chunk of data. */
3009 while (i < len) {
3010 if (buf[i] != '\n') {
3011 i++;
3012 continue;
3014 (*nlines)++;
3015 if (noffsets < *nlines) {
3016 off_t *o = recallocarray(*line_offsets,
3017 noffsets, *nlines,
3018 sizeof(**line_offsets));
3019 if (o == NULL) {
3020 free(*line_offsets);
3021 *line_offsets = NULL;
3022 return got_error_from_errno(
3023 "recallocarray");
3025 *line_offsets = o;
3026 noffsets = *nlines;
3028 off = i + 1;
3029 (*line_offsets)[*nlines - 1] = off;
3030 i++;
3034 if (fflush(infile) != 0)
3035 return got_error_from_errno("fflush");
3036 rewind(infile);
3038 if (filesize)
3039 *filesize = len;
3041 return NULL;
3044 static const struct got_error *
3045 create_diff(struct tog_diff_view_state *s)
3047 const struct got_error *err = NULL;
3048 FILE *f = NULL;
3049 int obj_type;
3051 f = got_opentemp();
3052 if (f == NULL) {
3053 err = got_error_from_errno("got_opentemp");
3054 goto done;
3056 if (s->f && fclose(s->f) != 0) {
3057 err = got_error_from_errno("fclose");
3058 goto done;
3060 s->f = f;
3062 if (s->id1)
3063 err = got_object_get_type(&obj_type, s->repo, s->id1);
3064 else
3065 err = got_object_get_type(&obj_type, s->repo, s->id2);
3066 if (err)
3067 goto done;
3069 switch (obj_type) {
3070 case GOT_OBJ_TYPE_BLOB:
3071 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3072 s->diff_context, 0, s->repo, s->f);
3073 break;
3074 case GOT_OBJ_TYPE_TREE:
3075 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3076 s->diff_context, 0, s->repo, s->f);
3077 break;
3078 case GOT_OBJ_TYPE_COMMIT: {
3079 const struct got_object_id_queue *parent_ids;
3080 struct got_object_qid *pid;
3081 struct got_commit_object *commit2;
3083 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3084 if (err)
3085 goto done;
3086 /* Show commit info if we're diffing to a parent/root commit. */
3087 if (s->id1 == NULL) {
3088 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3089 if (err)
3090 goto done;
3091 } else {
3092 parent_ids = got_object_commit_get_parent_ids(commit2);
3093 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3094 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3095 err = write_commit_info(s->id2, s->refs,
3096 s->repo, s->f);
3097 if (err)
3098 goto done;
3099 break;
3103 got_object_commit_close(commit2);
3105 err = got_diff_objects_as_commits(s->id1, s->id2,
3106 s->diff_context, 0, s->repo, s->f);
3107 break;
3109 default:
3110 err = got_error(GOT_ERR_OBJ_TYPE);
3111 break;
3113 if (err)
3114 goto done;
3115 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3116 s->f);
3117 done:
3118 if (s->f && fflush(s->f) != 0 && err == NULL)
3119 err = got_error_from_errno("fflush");
3120 return err;
3123 static void
3124 diff_view_indicate_progress(struct tog_view *view)
3126 mvwaddstr(view->window, 0, 0, "diffing...");
3127 update_panels();
3128 doupdate();
3131 static const struct got_error *
3132 search_start_diff_view(struct tog_view *view)
3134 struct tog_diff_view_state *s = &view->state.diff;
3136 s->matched_line = 0;
3137 return NULL;
3140 static const struct got_error *
3141 search_next_diff_view(struct tog_view *view)
3143 struct tog_diff_view_state *s = &view->state.diff;
3144 int lineno;
3146 if (!view->searching) {
3147 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3148 return NULL;
3151 if (s->matched_line) {
3152 if (view->searching == TOG_SEARCH_FORWARD)
3153 lineno = s->matched_line + 1;
3154 else
3155 lineno = s->matched_line - 1;
3156 } else {
3157 if (view->searching == TOG_SEARCH_FORWARD)
3158 lineno = 1;
3159 else
3160 lineno = s->nlines;
3163 while (1) {
3164 char *line = NULL;
3165 off_t offset;
3166 size_t len;
3168 if (lineno <= 0 || lineno > s->nlines) {
3169 if (s->matched_line == 0) {
3170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3171 free(line);
3172 break;
3175 if (view->searching == TOG_SEARCH_FORWARD)
3176 lineno = 1;
3177 else
3178 lineno = s->nlines;
3181 offset = s->line_offsets[lineno - 1];
3182 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3183 free(line);
3184 return got_error_from_errno("fseeko");
3186 free(line);
3187 line = parse_next_line(s->f, &len);
3188 if (line && match_line(line, &view->regex)) {
3189 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3190 s->matched_line = lineno;
3191 free(line);
3192 break;
3194 free(line);
3195 if (view->searching == TOG_SEARCH_FORWARD)
3196 lineno++;
3197 else
3198 lineno--;
3201 if (s->matched_line) {
3202 s->first_displayed_line = s->matched_line;
3203 s->selected_line = 1;
3206 return NULL;
3209 static const struct got_error *
3210 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3211 struct got_object_id *id2, struct tog_view *log_view,
3212 struct got_reflist_head *refs, struct got_repository *repo)
3214 const struct got_error *err;
3215 struct tog_diff_view_state *s = &view->state.diff;
3217 if (id1 != NULL && id2 != NULL) {
3218 int type1, type2;
3219 err = got_object_get_type(&type1, repo, id1);
3220 if (err)
3221 return err;
3222 err = got_object_get_type(&type2, repo, id2);
3223 if (err)
3224 return err;
3226 if (type1 != type2)
3227 return got_error(GOT_ERR_OBJ_TYPE);
3229 s->first_displayed_line = 1;
3230 s->last_displayed_line = view->nlines;
3231 s->selected_line = 1;
3232 s->repo = repo;
3233 s->refs = refs;
3234 s->id1 = id1;
3235 s->id2 = id2;
3237 if (id1) {
3238 s->id1 = got_object_id_dup(id1);
3239 if (s->id1 == NULL)
3240 return got_error_from_errno("got_object_id_dup");
3241 } else
3242 s->id1 = NULL;
3244 s->id2 = got_object_id_dup(id2);
3245 if (s->id2 == NULL) {
3246 free(s->id1);
3247 s->id1 = NULL;
3248 return got_error_from_errno("got_object_id_dup");
3250 s->f = NULL;
3251 s->first_displayed_line = 1;
3252 s->last_displayed_line = view->nlines;
3253 s->diff_context = 3;
3254 s->log_view = log_view;
3255 s->repo = repo;
3256 s->refs = refs;
3258 SIMPLEQ_INIT(&s->colors);
3259 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3260 err = add_color(&s->colors,
3261 "^-", TOG_COLOR_DIFF_MINUS,
3262 get_color_value("TOG_COLOR_DIFF_MINUS"));
3263 if (err)
3264 return err;
3265 err = add_color(&s->colors, "^\\+",
3266 TOG_COLOR_DIFF_PLUS,
3267 get_color_value("TOG_COLOR_DIFF_PLUS"));
3268 if (err) {
3269 free_colors(&s->colors);
3270 return err;
3272 err = add_color(&s->colors,
3273 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3274 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3275 if (err) {
3276 free_colors(&s->colors);
3277 return err;
3280 err = add_color(&s->colors,
3281 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3282 get_color_value("TOG_COLOR_DIFF_META"));
3283 if (err) {
3284 free_colors(&s->colors);
3285 return err;
3288 err = add_color(&s->colors,
3289 "^(from|via): ", TOG_COLOR_AUTHOR,
3290 get_color_value("TOG_COLOR_AUTHOR"));
3291 if (err) {
3292 free_colors(&s->colors);
3293 return err;
3296 err = add_color(&s->colors,
3297 "^date: ", TOG_COLOR_DATE,
3298 get_color_value("TOG_COLOR_DATE"));
3299 if (err) {
3300 free_colors(&s->colors);
3301 return err;
3305 if (log_view && view_is_splitscreen(view))
3306 show_log_view(log_view); /* draw vborder */
3307 diff_view_indicate_progress(view);
3309 err = create_diff(s);
3310 if (err) {
3311 free(s->id1);
3312 s->id1 = NULL;
3313 free(s->id2);
3314 s->id2 = NULL;
3315 return err;
3318 view->show = show_diff_view;
3319 view->input = input_diff_view;
3320 view->close = close_diff_view;
3321 view->search_start = search_start_diff_view;
3322 view->search_next = search_next_diff_view;
3324 return NULL;
3327 static const struct got_error *
3328 close_diff_view(struct tog_view *view)
3330 const struct got_error *err = NULL;
3331 struct tog_diff_view_state *s = &view->state.diff;
3333 free(s->id1);
3334 s->id1 = NULL;
3335 free(s->id2);
3336 s->id2 = NULL;
3337 if (s->f && fclose(s->f) == EOF)
3338 err = got_error_from_errno("fclose");
3339 free_colors(&s->colors);
3340 free(s->line_offsets);
3341 return err;
3344 static const struct got_error *
3345 show_diff_view(struct tog_view *view)
3347 const struct got_error *err;
3348 struct tog_diff_view_state *s = &view->state.diff;
3349 char *id_str1 = NULL, *id_str2, *header;
3351 if (s->id1) {
3352 err = got_object_id_str(&id_str1, s->id1);
3353 if (err)
3354 return err;
3356 err = got_object_id_str(&id_str2, s->id2);
3357 if (err)
3358 return err;
3360 if (asprintf(&header, "diff %s %s",
3361 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3362 err = got_error_from_errno("asprintf");
3363 free(id_str1);
3364 free(id_str2);
3365 return err;
3367 free(id_str1);
3368 free(id_str2);
3370 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3371 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3372 header, &s->colors);
3375 static const struct got_error *
3376 set_selected_commit(struct tog_diff_view_state *s,
3377 struct commit_queue_entry *entry)
3379 const struct got_error *err;
3380 const struct got_object_id_queue *parent_ids;
3381 struct got_commit_object *selected_commit;
3382 struct got_object_qid *pid;
3384 free(s->id2);
3385 s->id2 = got_object_id_dup(entry->id);
3386 if (s->id2 == NULL)
3387 return got_error_from_errno("got_object_id_dup");
3389 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3390 if (err)
3391 return err;
3392 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3393 free(s->id1);
3394 pid = SIMPLEQ_FIRST(parent_ids);
3395 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3396 got_object_commit_close(selected_commit);
3397 return NULL;
3400 static const struct got_error *
3401 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3402 struct tog_view **focus_view, struct tog_view *view, int ch)
3404 const struct got_error *err = NULL;
3405 struct tog_diff_view_state *s = &view->state.diff;
3406 struct tog_log_view_state *ls;
3407 struct commit_queue_entry *entry;
3408 int i;
3410 switch (ch) {
3411 case 'k':
3412 case KEY_UP:
3413 if (s->first_displayed_line > 1)
3414 s->first_displayed_line--;
3415 break;
3416 case KEY_PPAGE:
3417 case CTRL('b'):
3418 if (s->first_displayed_line == 1)
3419 break;
3420 i = 0;
3421 while (i++ < view->nlines - 1 &&
3422 s->first_displayed_line > 1)
3423 s->first_displayed_line--;
3424 break;
3425 case 'j':
3426 case KEY_DOWN:
3427 if (!s->eof)
3428 s->first_displayed_line++;
3429 break;
3430 case KEY_NPAGE:
3431 case CTRL('f'):
3432 case ' ':
3433 if (s->eof)
3434 break;
3435 i = 0;
3436 while (!s->eof && i++ < view->nlines - 1) {
3437 char *line;
3438 line = parse_next_line(s->f, NULL);
3439 s->first_displayed_line++;
3440 if (line == NULL)
3441 break;
3443 break;
3444 case '[':
3445 if (s->diff_context > 0) {
3446 s->diff_context--;
3447 diff_view_indicate_progress(view);
3448 err = create_diff(s);
3450 break;
3451 case ']':
3452 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3453 s->diff_context++;
3454 diff_view_indicate_progress(view);
3455 err = create_diff(s);
3457 break;
3458 case '<':
3459 case ',':
3460 if (s->log_view == NULL)
3461 break;
3462 ls = &s->log_view->state.log;
3463 entry = TAILQ_PREV(ls->selected_entry,
3464 commit_queue_head, entry);
3465 if (entry == NULL)
3466 break;
3468 err = input_log_view(NULL, NULL, NULL, s->log_view,
3469 KEY_UP);
3470 if (err)
3471 break;
3473 err = set_selected_commit(s, entry);
3474 if (err)
3475 break;
3477 s->first_displayed_line = 1;
3478 s->last_displayed_line = view->nlines;
3480 diff_view_indicate_progress(view);
3481 err = create_diff(s);
3482 break;
3483 case '>':
3484 case '.':
3485 if (s->log_view == NULL)
3486 break;
3487 ls = &s->log_view->state.log;
3489 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3490 ls->thread_args.commits_needed++;
3491 err = trigger_log_thread(s->log_view, 1,
3492 &ls->thread_args.commits_needed,
3493 &ls->thread_args.log_complete,
3494 &ls->thread_args.need_commits,
3495 &ls->thread_args.commit_loaded);
3496 if (err)
3497 break;
3499 err = input_log_view(NULL, NULL, NULL, s->log_view,
3500 KEY_DOWN);
3501 if (err)
3502 break;
3504 entry = TAILQ_NEXT(ls->selected_entry, entry);
3505 if (entry == NULL)
3506 break;
3508 err = set_selected_commit(s, entry);
3509 if (err)
3510 break;
3512 s->first_displayed_line = 1;
3513 s->last_displayed_line = view->nlines;
3515 diff_view_indicate_progress(view);
3516 err = create_diff(s);
3517 break;
3518 default:
3519 break;
3522 return err;
3525 static const struct got_error *
3526 cmd_diff(int argc, char *argv[])
3528 const struct got_error *error = NULL;
3529 struct got_repository *repo = NULL;
3530 struct got_worktree *worktree = NULL;
3531 struct got_reflist_head refs;
3532 struct got_object_id *id1 = NULL, *id2 = NULL;
3533 char *repo_path = NULL, *cwd = NULL;
3534 char *id_str1 = NULL, *id_str2 = NULL;
3535 int ch;
3536 struct tog_view *view;
3538 SIMPLEQ_INIT(&refs);
3540 #ifndef PROFILE
3541 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3542 NULL) == -1)
3543 err(1, "pledge");
3544 #endif
3546 while ((ch = getopt(argc, argv, "r:")) != -1) {
3547 switch (ch) {
3548 case 'r':
3549 repo_path = realpath(optarg, NULL);
3550 if (repo_path == NULL)
3551 return got_error_from_errno2("realpath",
3552 optarg);
3553 break;
3554 default:
3555 usage_diff();
3556 /* NOTREACHED */
3560 argc -= optind;
3561 argv += optind;
3563 if (argc == 0) {
3564 usage_diff(); /* TODO show local worktree changes */
3565 } else if (argc == 2) {
3566 id_str1 = argv[0];
3567 id_str2 = argv[1];
3568 } else
3569 usage_diff();
3571 cwd = getcwd(NULL, 0);
3572 if (cwd == NULL)
3573 return got_error_from_errno("getcwd");
3575 error = got_worktree_open(&worktree, cwd);
3576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3577 goto done;
3579 if (repo_path == NULL) {
3580 if (worktree)
3581 repo_path =
3582 strdup(got_worktree_get_repo_path(worktree));
3583 else
3584 repo_path = strdup(cwd);
3586 if (repo_path == NULL) {
3587 error = got_error_from_errno("strdup");
3588 goto done;
3591 error = got_repo_open(&repo, repo_path, NULL);
3592 if (error)
3593 goto done;
3595 init_curses();
3597 error = apply_unveil(got_repo_get_path(repo), NULL);
3598 if (error)
3599 goto done;
3601 error = got_repo_match_object_id_prefix(&id1, id_str1,
3602 GOT_OBJ_TYPE_ANY, repo);
3603 if (error)
3604 goto done;
3606 error = got_repo_match_object_id_prefix(&id2, id_str2,
3607 GOT_OBJ_TYPE_ANY, repo);
3608 if (error)
3609 goto done;
3611 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3612 if (error)
3613 goto done;
3615 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3616 if (view == NULL) {
3617 error = got_error_from_errno("view_open");
3618 goto done;
3620 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3621 if (error)
3622 goto done;
3623 error = view_loop(view);
3624 done:
3625 free(repo_path);
3626 free(cwd);
3627 if (repo)
3628 got_repo_close(repo);
3629 if (worktree)
3630 got_worktree_close(worktree);
3631 got_ref_list_free(&refs);
3632 return error;
3635 __dead static void
3636 usage_blame(void)
3638 endwin();
3639 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3640 getprogname());
3641 exit(1);
3644 struct tog_blame_line {
3645 int annotated;
3646 struct got_object_id *id;
3649 static const struct got_error *
3650 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3651 const char *path, struct tog_blame_line *lines, int nlines,
3652 int blame_complete, int selected_line, int *first_displayed_line,
3653 int *last_displayed_line, int *eof, int max_lines,
3654 struct tog_colors *colors)
3656 const struct got_error *err;
3657 int lineno = 0, nprinted = 0;
3658 char *line;
3659 size_t len;
3660 wchar_t *wline;
3661 int width;
3662 struct tog_blame_line *blame_line;
3663 struct got_object_id *prev_id = NULL;
3664 char *id_str;
3665 struct tog_color *tc;
3667 err = got_object_id_str(&id_str, id);
3668 if (err)
3669 return err;
3671 rewind(f);
3672 werase(view->window);
3674 if (asprintf(&line, "commit %s", id_str) == -1) {
3675 err = got_error_from_errno("asprintf");
3676 free(id_str);
3677 return err;
3680 err = format_line(&wline, &width, line, view->ncols, 0);
3681 free(line);
3682 line = NULL;
3683 if (err)
3684 return err;
3685 if (view_needs_focus_indication(view))
3686 wstandout(view->window);
3687 tc = get_color(colors, TOG_COLOR_COMMIT);
3688 if (tc)
3689 wattr_on(view->window,
3690 COLOR_PAIR(tc->colorpair), NULL);
3691 waddwstr(view->window, wline);
3692 if (tc)
3693 wattr_off(view->window,
3694 COLOR_PAIR(tc->colorpair), NULL);
3695 if (view_needs_focus_indication(view))
3696 wstandend(view->window);
3697 free(wline);
3698 wline = NULL;
3699 if (width < view->ncols - 1)
3700 waddch(view->window, '\n');
3702 if (asprintf(&line, "[%d/%d] %s%s",
3703 *first_displayed_line - 1 + selected_line, nlines,
3704 blame_complete ? "" : "annotating... ", path) == -1) {
3705 free(id_str);
3706 return got_error_from_errno("asprintf");
3708 free(id_str);
3709 err = format_line(&wline, &width, line, view->ncols, 0);
3710 free(line);
3711 line = NULL;
3712 if (err)
3713 return err;
3714 waddwstr(view->window, wline);
3715 free(wline);
3716 wline = NULL;
3717 if (width < view->ncols - 1)
3718 waddch(view->window, '\n');
3720 *eof = 0;
3721 while (nprinted < max_lines - 2) {
3722 line = parse_next_line(f, &len);
3723 if (line == NULL) {
3724 *eof = 1;
3725 break;
3727 if (++lineno < *first_displayed_line) {
3728 free(line);
3729 continue;
3732 if (view->ncols <= 9) {
3733 width = 9;
3734 wline = wcsdup(L"");
3735 if (wline == NULL)
3736 err = got_error_from_errno("wcsdup");
3737 } else {
3738 err = format_line(&wline, &width, line,
3739 view->ncols - 9, 9);
3740 width += 9;
3742 if (err) {
3743 free(line);
3744 return err;
3747 if (view->focussed && nprinted == selected_line - 1)
3748 wstandout(view->window);
3750 if (nlines > 0) {
3751 blame_line = &lines[lineno - 1];
3752 if (blame_line->annotated && prev_id &&
3753 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3754 !(view->focussed &&
3755 nprinted == selected_line - 1)) {
3756 waddstr(view->window, " ");
3757 } else if (blame_line->annotated) {
3758 char *id_str;
3759 err = got_object_id_str(&id_str, blame_line->id);
3760 if (err) {
3761 free(line);
3762 free(wline);
3763 return err;
3765 tc = get_color(colors, TOG_COLOR_COMMIT);
3766 if (tc)
3767 wattr_on(view->window,
3768 COLOR_PAIR(tc->colorpair), NULL);
3769 wprintw(view->window, "%.8s", id_str);
3770 if (tc)
3771 wattr_off(view->window,
3772 COLOR_PAIR(tc->colorpair), NULL);
3773 free(id_str);
3774 prev_id = blame_line->id;
3775 } else {
3776 waddstr(view->window, "........");
3777 prev_id = NULL;
3779 } else {
3780 waddstr(view->window, "........");
3781 prev_id = NULL;
3784 if (view->focussed && nprinted == selected_line - 1)
3785 wstandend(view->window);
3786 waddstr(view->window, " ");
3788 waddwstr(view->window, wline);
3789 if (width <= view->ncols - 1)
3790 waddch(view->window, '\n');
3791 if (++nprinted == 1)
3792 *first_displayed_line = lineno;
3793 free(line);
3794 free(wline);
3795 wline = NULL;
3797 *last_displayed_line = lineno;
3799 view_vborder(view);
3801 return NULL;
3804 static const struct got_error *
3805 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3807 const struct got_error *err = NULL;
3808 struct tog_blame_cb_args *a = arg;
3809 struct tog_blame_line *line;
3810 int errcode;
3812 if (nlines != a->nlines ||
3813 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3814 return got_error(GOT_ERR_RANGE);
3816 errcode = pthread_mutex_lock(&tog_mutex);
3817 if (errcode)
3818 return got_error_set_errno(errcode, "pthread_mutex_lock");
3820 if (*a->quit) { /* user has quit the blame view */
3821 err = got_error(GOT_ERR_ITER_COMPLETED);
3822 goto done;
3825 if (lineno == -1)
3826 goto done; /* no change in this commit */
3828 line = &a->lines[lineno - 1];
3829 if (line->annotated)
3830 goto done;
3832 line->id = got_object_id_dup(id);
3833 if (line->id == NULL) {
3834 err = got_error_from_errno("got_object_id_dup");
3835 goto done;
3837 line->annotated = 1;
3838 done:
3839 errcode = pthread_mutex_unlock(&tog_mutex);
3840 if (errcode)
3841 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3842 return err;
3845 static void *
3846 blame_thread(void *arg)
3848 const struct got_error *err;
3849 struct tog_blame_thread_args *ta = arg;
3850 struct tog_blame_cb_args *a = ta->cb_args;
3851 int errcode;
3853 err = block_signals_used_by_main_thread();
3854 if (err)
3855 return (void *)err;
3857 err = got_blame(ta->path, a->commit_id, ta->repo,
3858 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3859 if (err && err->code == GOT_ERR_CANCELLED)
3860 err = NULL;
3862 errcode = pthread_mutex_lock(&tog_mutex);
3863 if (errcode)
3864 return (void *)got_error_set_errno(errcode,
3865 "pthread_mutex_lock");
3867 got_repo_close(ta->repo);
3868 ta->repo = NULL;
3869 *ta->complete = 1;
3871 errcode = pthread_mutex_unlock(&tog_mutex);
3872 if (errcode && err == NULL)
3873 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3875 return (void *)err;
3878 static struct got_object_id *
3879 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3880 int first_displayed_line, int selected_line)
3882 struct tog_blame_line *line;
3884 if (nlines <= 0)
3885 return NULL;
3887 line = &lines[first_displayed_line - 1 + selected_line - 1];
3888 if (!line->annotated)
3889 return NULL;
3891 return line->id;
3894 static const struct got_error *
3895 stop_blame(struct tog_blame *blame)
3897 const struct got_error *err = NULL;
3898 int i;
3900 if (blame->thread) {
3901 int errcode;
3902 errcode = pthread_mutex_unlock(&tog_mutex);
3903 if (errcode)
3904 return got_error_set_errno(errcode,
3905 "pthread_mutex_unlock");
3906 errcode = pthread_join(blame->thread, (void **)&err);
3907 if (errcode)
3908 return got_error_set_errno(errcode, "pthread_join");
3909 errcode = pthread_mutex_lock(&tog_mutex);
3910 if (errcode)
3911 return got_error_set_errno(errcode,
3912 "pthread_mutex_lock");
3913 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3914 err = NULL;
3915 blame->thread = NULL;
3917 if (blame->thread_args.repo) {
3918 got_repo_close(blame->thread_args.repo);
3919 blame->thread_args.repo = NULL;
3921 if (blame->f) {
3922 if (fclose(blame->f) != 0 && err == NULL)
3923 err = got_error_from_errno("fclose");
3924 blame->f = NULL;
3926 if (blame->lines) {
3927 for (i = 0; i < blame->nlines; i++)
3928 free(blame->lines[i].id);
3929 free(blame->lines);
3930 blame->lines = NULL;
3932 free(blame->cb_args.commit_id);
3933 blame->cb_args.commit_id = NULL;
3935 return err;
3938 static const struct got_error *
3939 cancel_blame_view(void *arg)
3941 const struct got_error *err = NULL;
3942 int *done = arg;
3943 int errcode;
3945 errcode = pthread_mutex_lock(&tog_mutex);
3946 if (errcode)
3947 return got_error_set_errno(errcode,
3948 "pthread_mutex_unlock");
3950 if (*done)
3951 err = got_error(GOT_ERR_CANCELLED);
3953 errcode = pthread_mutex_unlock(&tog_mutex);
3954 if (errcode)
3955 return got_error_set_errno(errcode,
3956 "pthread_mutex_lock");
3958 return err;
3961 static const struct got_error *
3962 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3963 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3964 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3965 struct got_repository *repo)
3967 const struct got_error *err = NULL;
3968 struct got_blob_object *blob = NULL;
3969 struct got_repository *thread_repo = NULL;
3970 struct got_object_id *obj_id = NULL;
3971 int obj_type;
3973 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3974 if (err)
3975 return err;
3977 err = got_object_get_type(&obj_type, repo, obj_id);
3978 if (err)
3979 goto done;
3981 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3982 err = got_error(GOT_ERR_OBJ_TYPE);
3983 goto done;
3986 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3987 if (err)
3988 goto done;
3989 blame->f = got_opentemp();
3990 if (blame->f == NULL) {
3991 err = got_error_from_errno("got_opentemp");
3992 goto done;
3994 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3995 &blame->line_offsets, blame->f, blob);
3996 if (err || blame->nlines == 0)
3997 goto done;
3999 /* Don't include \n at EOF in the blame line count. */
4000 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4001 blame->nlines--;
4003 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4004 if (blame->lines == NULL) {
4005 err = got_error_from_errno("calloc");
4006 goto done;
4009 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4010 if (err)
4011 goto done;
4013 blame->cb_args.view = view;
4014 blame->cb_args.lines = blame->lines;
4015 blame->cb_args.nlines = blame->nlines;
4016 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4017 if (blame->cb_args.commit_id == NULL) {
4018 err = got_error_from_errno("got_object_id_dup");
4019 goto done;
4021 blame->cb_args.quit = done;
4023 blame->thread_args.path = path;
4024 blame->thread_args.repo = thread_repo;
4025 blame->thread_args.cb_args = &blame->cb_args;
4026 blame->thread_args.complete = blame_complete;
4027 blame->thread_args.cancel_cb = cancel_blame_view;
4028 blame->thread_args.cancel_arg = done;
4029 *blame_complete = 0;
4031 done:
4032 if (blob)
4033 got_object_blob_close(blob);
4034 free(obj_id);
4035 if (err)
4036 stop_blame(blame);
4037 return err;
4040 static const struct got_error *
4041 open_blame_view(struct tog_view *view, char *path,
4042 struct got_object_id *commit_id, struct got_reflist_head *refs,
4043 struct got_repository *repo)
4045 const struct got_error *err = NULL;
4046 struct tog_blame_view_state *s = &view->state.blame;
4048 SIMPLEQ_INIT(&s->blamed_commits);
4050 s->path = strdup(path);
4051 if (s->path == NULL)
4052 return got_error_from_errno("strdup");
4054 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4055 if (err) {
4056 free(s->path);
4057 return err;
4060 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4061 s->first_displayed_line = 1;
4062 s->last_displayed_line = view->nlines;
4063 s->selected_line = 1;
4064 s->blame_complete = 0;
4065 s->repo = repo;
4066 s->refs = refs;
4067 s->commit_id = commit_id;
4068 memset(&s->blame, 0, sizeof(s->blame));
4070 SIMPLEQ_INIT(&s->colors);
4071 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4072 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4073 get_color_value("TOG_COLOR_COMMIT"));
4074 if (err)
4075 return err;
4078 view->show = show_blame_view;
4079 view->input = input_blame_view;
4080 view->close = close_blame_view;
4081 view->search_start = search_start_blame_view;
4082 view->search_next = search_next_blame_view;
4084 return run_blame(&s->blame, view, &s->blame_complete,
4085 &s->first_displayed_line, &s->last_displayed_line,
4086 &s->selected_line, &s->done, &s->eof, s->path,
4087 s->blamed_commit->id, s->repo);
4090 static const struct got_error *
4091 close_blame_view(struct tog_view *view)
4093 const struct got_error *err = NULL;
4094 struct tog_blame_view_state *s = &view->state.blame;
4096 if (s->blame.thread)
4097 err = stop_blame(&s->blame);
4099 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4100 struct got_object_qid *blamed_commit;
4101 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4102 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4103 got_object_qid_free(blamed_commit);
4106 free(s->path);
4107 free_colors(&s->colors);
4109 return err;
4112 static const struct got_error *
4113 search_start_blame_view(struct tog_view *view)
4115 struct tog_blame_view_state *s = &view->state.blame;
4117 s->matched_line = 0;
4118 return NULL;
4121 static const struct got_error *
4122 search_next_blame_view(struct tog_view *view)
4124 struct tog_blame_view_state *s = &view->state.blame;
4125 int lineno;
4127 if (!view->searching) {
4128 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4129 return NULL;
4132 if (s->matched_line) {
4133 if (view->searching == TOG_SEARCH_FORWARD)
4134 lineno = s->matched_line + 1;
4135 else
4136 lineno = s->matched_line - 1;
4137 } else {
4138 if (view->searching == TOG_SEARCH_FORWARD)
4139 lineno = 1;
4140 else
4141 lineno = s->blame.nlines;
4144 while (1) {
4145 char *line = NULL;
4146 off_t offset;
4147 size_t len;
4149 if (lineno <= 0 || lineno > s->blame.nlines) {
4150 if (s->matched_line == 0) {
4151 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4152 free(line);
4153 break;
4156 if (view->searching == TOG_SEARCH_FORWARD)
4157 lineno = 1;
4158 else
4159 lineno = s->blame.nlines;
4162 offset = s->blame.line_offsets[lineno - 1];
4163 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4164 free(line);
4165 return got_error_from_errno("fseeko");
4167 free(line);
4168 line = parse_next_line(s->blame.f, &len);
4169 if (line && match_line(line, &view->regex)) {
4170 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4171 s->matched_line = lineno;
4172 free(line);
4173 break;
4175 free(line);
4176 if (view->searching == TOG_SEARCH_FORWARD)
4177 lineno++;
4178 else
4179 lineno--;
4182 if (s->matched_line) {
4183 s->first_displayed_line = s->matched_line;
4184 s->selected_line = 1;
4187 return NULL;
4190 static const struct got_error *
4191 show_blame_view(struct tog_view *view)
4193 const struct got_error *err = NULL;
4194 struct tog_blame_view_state *s = &view->state.blame;
4195 int errcode;
4197 if (s->blame.thread == NULL) {
4198 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4199 &s->blame.thread_args);
4200 if (errcode)
4201 return got_error_set_errno(errcode, "pthread_create");
4203 halfdelay(1); /* fast refresh while annotating */
4206 if (s->blame_complete)
4207 halfdelay(10); /* disable fast refresh */
4209 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4210 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4211 s->selected_line, &s->first_displayed_line,
4212 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4214 view_vborder(view);
4215 return err;
4218 static const struct got_error *
4219 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4220 struct tog_view **focus_view, struct tog_view *view, int ch)
4222 const struct got_error *err = NULL, *thread_err = NULL;
4223 struct tog_view *diff_view;
4224 struct tog_blame_view_state *s = &view->state.blame;
4225 int begin_x = 0;
4227 switch (ch) {
4228 case 'q':
4229 s->done = 1;
4230 break;
4231 case 'k':
4232 case KEY_UP:
4233 if (s->selected_line > 1)
4234 s->selected_line--;
4235 else if (s->selected_line == 1 &&
4236 s->first_displayed_line > 1)
4237 s->first_displayed_line--;
4238 break;
4239 case KEY_PPAGE:
4240 case CTRL('b'):
4241 if (s->first_displayed_line == 1) {
4242 s->selected_line = 1;
4243 break;
4245 if (s->first_displayed_line > view->nlines - 2)
4246 s->first_displayed_line -=
4247 (view->nlines - 2);
4248 else
4249 s->first_displayed_line = 1;
4250 break;
4251 case 'j':
4252 case KEY_DOWN:
4253 if (s->selected_line < view->nlines - 2 &&
4254 s->first_displayed_line +
4255 s->selected_line <= s->blame.nlines)
4256 s->selected_line++;
4257 else if (s->last_displayed_line <
4258 s->blame.nlines)
4259 s->first_displayed_line++;
4260 break;
4261 case 'b':
4262 case 'p': {
4263 struct got_object_id *id = NULL;
4264 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4265 s->first_displayed_line, s->selected_line);
4266 if (id == NULL)
4267 break;
4268 if (ch == 'p') {
4269 struct got_commit_object *commit;
4270 struct got_object_qid *pid;
4271 struct got_object_id *blob_id = NULL;
4272 int obj_type;
4273 err = got_object_open_as_commit(&commit,
4274 s->repo, id);
4275 if (err)
4276 break;
4277 pid = SIMPLEQ_FIRST(
4278 got_object_commit_get_parent_ids(commit));
4279 if (pid == NULL) {
4280 got_object_commit_close(commit);
4281 break;
4283 /* Check if path history ends here. */
4284 err = got_object_id_by_path(&blob_id, s->repo,
4285 pid->id, s->path);
4286 if (err) {
4287 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4288 err = NULL;
4289 got_object_commit_close(commit);
4290 break;
4292 err = got_object_get_type(&obj_type, s->repo,
4293 blob_id);
4294 free(blob_id);
4295 /* Can't blame non-blob type objects. */
4296 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4297 got_object_commit_close(commit);
4298 break;
4300 err = got_object_qid_alloc(&s->blamed_commit,
4301 pid->id);
4302 got_object_commit_close(commit);
4303 } else {
4304 if (got_object_id_cmp(id,
4305 s->blamed_commit->id) == 0)
4306 break;
4307 err = got_object_qid_alloc(&s->blamed_commit,
4308 id);
4310 if (err)
4311 break;
4312 s->done = 1;
4313 thread_err = stop_blame(&s->blame);
4314 s->done = 0;
4315 if (thread_err)
4316 break;
4317 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4318 s->blamed_commit, entry);
4319 err = run_blame(&s->blame, view, &s->blame_complete,
4320 &s->first_displayed_line, &s->last_displayed_line,
4321 &s->selected_line, &s->done, &s->eof,
4322 s->path, s->blamed_commit->id, s->repo);
4323 if (err)
4324 break;
4325 break;
4327 case 'B': {
4328 struct got_object_qid *first;
4329 first = SIMPLEQ_FIRST(&s->blamed_commits);
4330 if (!got_object_id_cmp(first->id, s->commit_id))
4331 break;
4332 s->done = 1;
4333 thread_err = stop_blame(&s->blame);
4334 s->done = 0;
4335 if (thread_err)
4336 break;
4337 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4338 got_object_qid_free(s->blamed_commit);
4339 s->blamed_commit =
4340 SIMPLEQ_FIRST(&s->blamed_commits);
4341 err = run_blame(&s->blame, view, &s->blame_complete,
4342 &s->first_displayed_line, &s->last_displayed_line,
4343 &s->selected_line, &s->done, &s->eof, s->path,
4344 s->blamed_commit->id, s->repo);
4345 if (err)
4346 break;
4347 break;
4349 case KEY_ENTER:
4350 case '\r': {
4351 struct got_object_id *id = NULL;
4352 struct got_object_qid *pid;
4353 struct got_commit_object *commit = NULL;
4354 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4355 s->first_displayed_line, s->selected_line);
4356 if (id == NULL)
4357 break;
4358 err = got_object_open_as_commit(&commit, s->repo, id);
4359 if (err)
4360 break;
4361 pid = SIMPLEQ_FIRST(
4362 got_object_commit_get_parent_ids(commit));
4363 if (view_is_parent_view(view))
4364 begin_x = view_split_begin_x(view->begin_x);
4365 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4366 if (diff_view == NULL) {
4367 got_object_commit_close(commit);
4368 err = got_error_from_errno("view_open");
4369 break;
4371 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4372 id, NULL, s->refs, s->repo);
4373 got_object_commit_close(commit);
4374 if (err) {
4375 view_close(diff_view);
4376 break;
4378 if (view_is_parent_view(view)) {
4379 err = view_close_child(view);
4380 if (err)
4381 break;
4382 err = view_set_child(view, diff_view);
4383 if (err) {
4384 view_close(diff_view);
4385 break;
4387 *focus_view = diff_view;
4388 view->child_focussed = 1;
4389 } else
4390 *new_view = diff_view;
4391 if (err)
4392 break;
4393 break;
4395 case KEY_NPAGE:
4396 case CTRL('f'):
4397 case ' ':
4398 if (s->last_displayed_line >= s->blame.nlines &&
4399 s->selected_line >= MIN(s->blame.nlines,
4400 view->nlines - 2)) {
4401 break;
4403 if (s->last_displayed_line >= s->blame.nlines &&
4404 s->selected_line < view->nlines - 2) {
4405 s->selected_line = MIN(s->blame.nlines,
4406 view->nlines - 2);
4407 break;
4409 if (s->last_displayed_line + view->nlines - 2
4410 <= s->blame.nlines)
4411 s->first_displayed_line +=
4412 view->nlines - 2;
4413 else
4414 s->first_displayed_line =
4415 s->blame.nlines -
4416 (view->nlines - 3);
4417 break;
4418 case KEY_RESIZE:
4419 if (s->selected_line > view->nlines - 2) {
4420 s->selected_line = MIN(s->blame.nlines,
4421 view->nlines - 2);
4423 break;
4424 default:
4425 break;
4427 return thread_err ? thread_err : err;
4430 static const struct got_error *
4431 cmd_blame(int argc, char *argv[])
4433 const struct got_error *error;
4434 struct got_repository *repo = NULL;
4435 struct got_reflist_head refs;
4436 struct got_worktree *worktree = NULL;
4437 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4438 struct got_object_id *commit_id = NULL;
4439 char *commit_id_str = NULL;
4440 int ch;
4441 struct tog_view *view;
4443 SIMPLEQ_INIT(&refs);
4445 #ifndef PROFILE
4446 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4447 NULL) == -1)
4448 err(1, "pledge");
4449 #endif
4451 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4452 switch (ch) {
4453 case 'c':
4454 commit_id_str = optarg;
4455 break;
4456 case 'r':
4457 repo_path = realpath(optarg, NULL);
4458 if (repo_path == NULL)
4459 return got_error_from_errno2("realpath",
4460 optarg);
4461 break;
4462 default:
4463 usage_blame();
4464 /* NOTREACHED */
4468 argc -= optind;
4469 argv += optind;
4471 if (argc != 1)
4472 usage_blame();
4474 cwd = getcwd(NULL, 0);
4475 if (cwd == NULL)
4476 return got_error_from_errno("getcwd");
4478 error = got_worktree_open(&worktree, cwd);
4479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4480 goto done;
4482 if (repo_path == NULL) {
4483 if (worktree)
4484 repo_path =
4485 strdup(got_worktree_get_repo_path(worktree));
4486 else
4487 repo_path = strdup(cwd);
4489 if (repo_path == NULL) {
4490 error = got_error_from_errno("strdup");
4491 goto done;
4494 error = got_repo_open(&repo, repo_path, NULL);
4495 if (error != NULL)
4496 goto done;
4498 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4499 worktree);
4500 if (error)
4501 goto done;
4503 init_curses();
4505 error = apply_unveil(got_repo_get_path(repo), NULL);
4506 if (error)
4507 goto done;
4509 if (commit_id_str == NULL) {
4510 struct got_reference *head_ref;
4511 error = got_ref_open(&head_ref, repo, worktree ?
4512 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4513 if (error != NULL)
4514 goto done;
4515 error = got_ref_resolve(&commit_id, repo, head_ref);
4516 got_ref_close(head_ref);
4517 } else {
4518 error = got_repo_match_object_id(&commit_id, NULL,
4519 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4521 if (error != NULL)
4522 goto done;
4524 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4525 if (error)
4526 goto done;
4528 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4529 if (view == NULL) {
4530 error = got_error_from_errno("view_open");
4531 goto done;
4533 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4534 if (error)
4535 goto done;
4536 if (worktree) {
4537 /* Release work tree lock. */
4538 got_worktree_close(worktree);
4539 worktree = NULL;
4541 error = view_loop(view);
4542 done:
4543 free(repo_path);
4544 free(in_repo_path);
4545 free(cwd);
4546 free(commit_id);
4547 if (worktree)
4548 got_worktree_close(worktree);
4549 if (repo)
4550 got_repo_close(repo);
4551 got_ref_list_free(&refs);
4552 return error;
4555 static const struct got_error *
4556 draw_tree_entries(struct tog_view *view,
4557 struct got_tree_entry **first_displayed_entry,
4558 struct got_tree_entry **last_displayed_entry,
4559 struct got_tree_entry **selected_entry, int *ndisplayed,
4560 const char *label, int show_ids, const char *parent_path,
4561 struct got_tree_object *tree, int selected, int limit,
4562 int isroot, struct tog_colors *colors)
4564 const struct got_error *err = NULL;
4565 struct got_tree_entry *te;
4566 wchar_t *wline;
4567 struct tog_color *tc;
4568 int width, n, i, nentries;
4570 *ndisplayed = 0;
4572 werase(view->window);
4574 if (limit == 0)
4575 return NULL;
4577 err = format_line(&wline, &width, label, view->ncols, 0);
4578 if (err)
4579 return err;
4580 if (view_needs_focus_indication(view))
4581 wstandout(view->window);
4582 tc = get_color(colors, TOG_COLOR_COMMIT);
4583 if (tc)
4584 wattr_on(view->window,
4585 COLOR_PAIR(tc->colorpair), NULL);
4586 waddwstr(view->window, wline);
4587 if (tc)
4588 wattr_off(view->window,
4589 COLOR_PAIR(tc->colorpair), NULL);
4590 if (view_needs_focus_indication(view))
4591 wstandend(view->window);
4592 free(wline);
4593 wline = NULL;
4594 if (width < view->ncols - 1)
4595 waddch(view->window, '\n');
4596 if (--limit <= 0)
4597 return NULL;
4598 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4599 if (err)
4600 return err;
4601 waddwstr(view->window, wline);
4602 free(wline);
4603 wline = NULL;
4604 if (width < view->ncols - 1)
4605 waddch(view->window, '\n');
4606 if (--limit <= 0)
4607 return NULL;
4608 waddch(view->window, '\n');
4609 if (--limit <= 0)
4610 return NULL;
4612 if (*first_displayed_entry == NULL) {
4613 te = got_object_tree_get_first_entry(tree);
4614 if (selected == 0) {
4615 if (view->focussed)
4616 wstandout(view->window);
4617 *selected_entry = NULL;
4619 waddstr(view->window, " ..\n"); /* parent directory */
4620 if (selected == 0 && view->focussed)
4621 wstandend(view->window);
4622 (*ndisplayed)++;
4623 if (--limit <= 0)
4624 return NULL;
4625 n = 1;
4626 } else {
4627 n = 0;
4628 te = *first_displayed_entry;
4631 nentries = got_object_tree_get_nentries(tree);
4632 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4633 char *line = NULL, *id_str = NULL;
4634 const char *modestr = "";
4635 mode_t mode;
4637 te = got_object_tree_get_entry(tree, i);
4638 mode = got_tree_entry_get_mode(te);
4640 if (show_ids) {
4641 err = got_object_id_str(&id_str,
4642 got_tree_entry_get_id(te));
4643 if (err)
4644 return got_error_from_errno(
4645 "got_object_id_str");
4647 if (got_object_tree_entry_is_submodule(te))
4648 modestr = "$";
4649 else if (S_ISLNK(mode))
4650 modestr = "@";
4651 else if (S_ISDIR(mode))
4652 modestr = "/";
4653 else if (mode & S_IXUSR)
4654 modestr = "*";
4655 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4656 got_tree_entry_get_name(te), modestr) == -1) {
4657 free(id_str);
4658 return got_error_from_errno("asprintf");
4660 free(id_str);
4661 err = format_line(&wline, &width, line, view->ncols, 0);
4662 if (err) {
4663 free(line);
4664 break;
4666 if (n == selected) {
4667 if (view->focussed)
4668 wstandout(view->window);
4669 *selected_entry = te;
4671 tc = match_color(colors, line);
4672 if (tc)
4673 wattr_on(view->window,
4674 COLOR_PAIR(tc->colorpair), NULL);
4675 waddwstr(view->window, wline);
4676 if (tc)
4677 wattr_off(view->window,
4678 COLOR_PAIR(tc->colorpair), NULL);
4679 if (width < view->ncols - 1)
4680 waddch(view->window, '\n');
4681 if (n == selected && view->focussed)
4682 wstandend(view->window);
4683 free(line);
4684 free(wline);
4685 wline = NULL;
4686 n++;
4687 (*ndisplayed)++;
4688 *last_displayed_entry = te;
4689 if (--limit <= 0)
4690 break;
4693 return err;
4696 static void
4697 tree_scroll_up(struct tog_view *view,
4698 struct got_tree_entry **first_displayed_entry, int maxscroll,
4699 struct got_tree_object *tree, int isroot)
4701 struct got_tree_entry *te;
4702 int i;
4704 if (*first_displayed_entry == NULL)
4705 return;
4707 te = got_object_tree_get_entry(tree, 0);
4708 if (*first_displayed_entry == te) {
4709 if (!isroot)
4710 *first_displayed_entry = NULL;
4711 return;
4714 i = 0;
4715 while (*first_displayed_entry && i < maxscroll) {
4716 *first_displayed_entry = got_tree_entry_get_prev(tree,
4717 *first_displayed_entry);
4718 i++;
4720 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4721 *first_displayed_entry = NULL;
4724 static int
4725 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4726 struct got_tree_entry *last_displayed_entry,
4727 struct got_tree_object *tree)
4729 struct got_tree_entry *next, *last;
4730 int n = 0;
4732 if (*first_displayed_entry)
4733 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4734 else
4735 next = got_object_tree_get_first_entry(tree);
4737 last = last_displayed_entry;
4738 while (next && last && n++ < maxscroll) {
4739 last = got_tree_entry_get_next(tree, last);
4740 if (last) {
4741 *first_displayed_entry = next;
4742 next = got_tree_entry_get_next(tree, next);
4745 return n;
4748 static const struct got_error *
4749 tree_entry_path(char **path, struct tog_parent_trees *parents,
4750 struct got_tree_entry *te)
4752 const struct got_error *err = NULL;
4753 struct tog_parent_tree *pt;
4754 size_t len = 2; /* for leading slash and NUL */
4756 TAILQ_FOREACH(pt, parents, entry)
4757 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4758 + 1 /* slash */;
4759 if (te)
4760 len += strlen(got_tree_entry_get_name(te));
4762 *path = calloc(1, len);
4763 if (path == NULL)
4764 return got_error_from_errno("calloc");
4766 (*path)[0] = '/';
4767 pt = TAILQ_LAST(parents, tog_parent_trees);
4768 while (pt) {
4769 const char *name = got_tree_entry_get_name(pt->selected_entry);
4770 if (strlcat(*path, name, len) >= len) {
4771 err = got_error(GOT_ERR_NO_SPACE);
4772 goto done;
4774 if (strlcat(*path, "/", len) >= len) {
4775 err = got_error(GOT_ERR_NO_SPACE);
4776 goto done;
4778 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4780 if (te) {
4781 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4782 err = got_error(GOT_ERR_NO_SPACE);
4783 goto done;
4786 done:
4787 if (err) {
4788 free(*path);
4789 *path = NULL;
4791 return err;
4794 static const struct got_error *
4795 blame_tree_entry(struct tog_view **new_view, int begin_x,
4796 struct got_tree_entry *te, struct tog_parent_trees *parents,
4797 struct got_object_id *commit_id, struct got_reflist_head *refs,
4798 struct got_repository *repo)
4800 const struct got_error *err = NULL;
4801 char *path;
4802 struct tog_view *blame_view;
4804 *new_view = NULL;
4806 err = tree_entry_path(&path, parents, te);
4807 if (err)
4808 return err;
4810 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4811 if (blame_view == NULL) {
4812 err = got_error_from_errno("view_open");
4813 goto done;
4816 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4817 if (err) {
4818 if (err->code == GOT_ERR_CANCELLED)
4819 err = NULL;
4820 view_close(blame_view);
4821 } else
4822 *new_view = blame_view;
4823 done:
4824 free(path);
4825 return err;
4828 static const struct got_error *
4829 log_tree_entry(struct tog_view **new_view, int begin_x,
4830 struct got_tree_entry *te, struct tog_parent_trees *parents,
4831 struct got_object_id *commit_id, struct got_reflist_head *refs,
4832 struct got_repository *repo)
4834 struct tog_view *log_view;
4835 const struct got_error *err = NULL;
4836 char *path;
4838 *new_view = NULL;
4840 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4841 if (log_view == NULL)
4842 return got_error_from_errno("view_open");
4844 err = tree_entry_path(&path, parents, te);
4845 if (err)
4846 return err;
4848 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4849 if (err)
4850 view_close(log_view);
4851 else
4852 *new_view = log_view;
4853 free(path);
4854 return err;
4857 static const struct got_error *
4858 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4859 struct got_object_id *commit_id, struct got_reflist_head *refs,
4860 struct got_repository *repo)
4862 const struct got_error *err = NULL;
4863 char *commit_id_str = NULL;
4864 struct tog_tree_view_state *s = &view->state.tree;
4866 TAILQ_INIT(&s->parents);
4868 err = got_object_id_str(&commit_id_str, commit_id);
4869 if (err != NULL)
4870 goto done;
4872 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4873 err = got_error_from_errno("asprintf");
4874 goto done;
4877 s->root = s->tree = root;
4878 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4879 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4880 s->commit_id = got_object_id_dup(commit_id);
4881 if (s->commit_id == NULL) {
4882 err = got_error_from_errno("got_object_id_dup");
4883 goto done;
4885 s->refs = refs;
4886 s->repo = repo;
4888 SIMPLEQ_INIT(&s->colors);
4890 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4891 err = add_color(&s->colors, "\\$$",
4892 TOG_COLOR_TREE_SUBMODULE,
4893 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4894 if (err)
4895 goto done;
4896 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4897 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4898 if (err) {
4899 free_colors(&s->colors);
4900 goto done;
4902 err = add_color(&s->colors, "/$",
4903 TOG_COLOR_TREE_DIRECTORY,
4904 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4905 if (err) {
4906 free_colors(&s->colors);
4907 goto done;
4910 err = add_color(&s->colors, "\\*$",
4911 TOG_COLOR_TREE_EXECUTABLE,
4912 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4913 if (err) {
4914 free_colors(&s->colors);
4915 goto done;
4918 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4919 get_color_value("TOG_COLOR_COMMIT"));
4920 if (err) {
4921 free_colors(&s->colors);
4922 goto done;
4926 view->show = show_tree_view;
4927 view->input = input_tree_view;
4928 view->close = close_tree_view;
4929 view->search_start = search_start_tree_view;
4930 view->search_next = search_next_tree_view;
4931 done:
4932 free(commit_id_str);
4933 if (err) {
4934 free(s->tree_label);
4935 s->tree_label = NULL;
4937 return err;
4940 static const struct got_error *
4941 close_tree_view(struct tog_view *view)
4943 struct tog_tree_view_state *s = &view->state.tree;
4945 free_colors(&s->colors);
4946 free(s->tree_label);
4947 s->tree_label = NULL;
4948 free(s->commit_id);
4949 s->commit_id = NULL;
4950 while (!TAILQ_EMPTY(&s->parents)) {
4951 struct tog_parent_tree *parent;
4952 parent = TAILQ_FIRST(&s->parents);
4953 TAILQ_REMOVE(&s->parents, parent, entry);
4954 free(parent);
4957 if (s->tree != s->root)
4958 got_object_tree_close(s->tree);
4959 got_object_tree_close(s->root);
4961 return NULL;
4964 static const struct got_error *
4965 search_start_tree_view(struct tog_view *view)
4967 struct tog_tree_view_state *s = &view->state.tree;
4969 s->matched_entry = NULL;
4970 return NULL;
4973 static int
4974 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4976 regmatch_t regmatch;
4978 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4979 0) == 0;
4982 static const struct got_error *
4983 search_next_tree_view(struct tog_view *view)
4985 struct tog_tree_view_state *s = &view->state.tree;
4986 struct got_tree_entry *te = NULL;
4988 if (!view->searching) {
4989 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4990 return NULL;
4993 if (s->matched_entry) {
4994 if (view->searching == TOG_SEARCH_FORWARD) {
4995 if (s->selected_entry)
4996 te = got_tree_entry_get_next(s->tree,
4997 s->selected_entry);
4998 else
4999 te = got_object_tree_get_first_entry(s->tree);
5000 } else {
5001 if (s->selected_entry == NULL)
5002 te = got_object_tree_get_last_entry(s->tree);
5003 else
5004 te = got_tree_entry_get_prev(s->tree,
5005 s->selected_entry);
5007 } else {
5008 if (view->searching == TOG_SEARCH_FORWARD)
5009 te = got_object_tree_get_first_entry(s->tree);
5010 else
5011 te = got_object_tree_get_last_entry(s->tree);
5014 while (1) {
5015 if (te == NULL) {
5016 if (s->matched_entry == NULL) {
5017 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5018 return NULL;
5020 if (view->searching == TOG_SEARCH_FORWARD)
5021 te = got_object_tree_get_first_entry(s->tree);
5022 else
5023 te = got_object_tree_get_last_entry(s->tree);
5026 if (match_tree_entry(te, &view->regex)) {
5027 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5028 s->matched_entry = te;
5029 break;
5032 if (view->searching == TOG_SEARCH_FORWARD)
5033 te = got_tree_entry_get_next(s->tree, te);
5034 else
5035 te = got_tree_entry_get_prev(s->tree, te);
5038 if (s->matched_entry) {
5039 s->first_displayed_entry = s->matched_entry;
5040 s->selected = 0;
5043 return NULL;
5046 static const struct got_error *
5047 show_tree_view(struct tog_view *view)
5049 const struct got_error *err = NULL;
5050 struct tog_tree_view_state *s = &view->state.tree;
5051 char *parent_path;
5053 err = tree_entry_path(&parent_path, &s->parents, NULL);
5054 if (err)
5055 return err;
5057 err = draw_tree_entries(view, &s->first_displayed_entry,
5058 &s->last_displayed_entry, &s->selected_entry,
5059 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5060 s->tree, s->selected, view->nlines, s->tree == s->root,
5061 &s->colors);
5062 free(parent_path);
5064 view_vborder(view);
5065 return err;
5068 static const struct got_error *
5069 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5070 struct tog_view **focus_view, struct tog_view *view, int ch)
5072 const struct got_error *err = NULL;
5073 struct tog_tree_view_state *s = &view->state.tree;
5074 struct tog_view *log_view;
5075 int begin_x = 0, nscrolled;
5077 switch (ch) {
5078 case 'i':
5079 s->show_ids = !s->show_ids;
5080 break;
5081 case 'l':
5082 if (!s->selected_entry)
5083 break;
5084 if (view_is_parent_view(view))
5085 begin_x = view_split_begin_x(view->begin_x);
5086 err = log_tree_entry(&log_view, begin_x,
5087 s->selected_entry, &s->parents,
5088 s->commit_id, s->refs, s->repo);
5089 if (view_is_parent_view(view)) {
5090 err = view_close_child(view);
5091 if (err)
5092 return err;
5093 err = view_set_child(view, log_view);
5094 if (err) {
5095 view_close(log_view);
5096 break;
5098 *focus_view = log_view;
5099 view->child_focussed = 1;
5100 } else
5101 *new_view = log_view;
5102 break;
5103 case 'k':
5104 case KEY_UP:
5105 if (s->selected > 0) {
5106 s->selected--;
5107 if (s->selected == 0)
5108 break;
5110 if (s->selected > 0)
5111 break;
5112 tree_scroll_up(view, &s->first_displayed_entry, 1,
5113 s->tree, s->tree == s->root);
5114 break;
5115 case KEY_PPAGE:
5116 case CTRL('b'):
5117 tree_scroll_up(view, &s->first_displayed_entry,
5118 MAX(0, view->nlines - 4 - s->selected), s->tree,
5119 s->tree == s->root);
5120 s->selected = 0;
5121 if (got_object_tree_get_first_entry(s->tree) ==
5122 s->first_displayed_entry && s->tree != s->root)
5123 s->first_displayed_entry = NULL;
5124 break;
5125 case 'j':
5126 case KEY_DOWN:
5127 if (s->selected < s->ndisplayed - 1) {
5128 s->selected++;
5129 break;
5131 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5132 == NULL)
5133 /* can't scroll any further */
5134 break;
5135 tree_scroll_down(&s->first_displayed_entry, 1,
5136 s->last_displayed_entry, s->tree);
5137 break;
5138 case KEY_NPAGE:
5139 case CTRL('f'):
5140 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5141 == NULL) {
5142 /* can't scroll any further; move cursor down */
5143 if (s->selected < s->ndisplayed - 1)
5144 s->selected = s->ndisplayed - 1;
5145 break;
5147 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5148 view->nlines, s->last_displayed_entry, s->tree);
5149 if (nscrolled < view->nlines) {
5150 int ndisplayed = 0;
5151 struct got_tree_entry *te;
5152 te = s->first_displayed_entry;
5153 do {
5154 ndisplayed++;
5155 te = got_tree_entry_get_next(s->tree, te);
5156 } while (te);
5157 s->selected = ndisplayed - 1;
5159 break;
5160 case KEY_ENTER:
5161 case '\r':
5162 case KEY_BACKSPACE:
5163 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5164 struct tog_parent_tree *parent;
5165 /* user selected '..' */
5166 if (s->tree == s->root)
5167 break;
5168 parent = TAILQ_FIRST(&s->parents);
5169 TAILQ_REMOVE(&s->parents, parent,
5170 entry);
5171 got_object_tree_close(s->tree);
5172 s->tree = parent->tree;
5173 s->first_displayed_entry =
5174 parent->first_displayed_entry;
5175 s->selected_entry =
5176 parent->selected_entry;
5177 s->selected = parent->selected;
5178 free(parent);
5179 } else if (S_ISDIR(got_tree_entry_get_mode(
5180 s->selected_entry))) {
5181 struct got_tree_object *subtree;
5182 err = got_object_open_as_tree(&subtree, s->repo,
5183 got_tree_entry_get_id(s->selected_entry));
5184 if (err)
5185 break;
5186 err = tree_view_visit_subtree(subtree, s);
5187 if (err) {
5188 got_object_tree_close(subtree);
5189 break;
5191 } else if (S_ISREG(got_tree_entry_get_mode(
5192 s->selected_entry))) {
5193 struct tog_view *blame_view;
5194 int begin_x = view_is_parent_view(view) ?
5195 view_split_begin_x(view->begin_x) : 0;
5197 err = blame_tree_entry(&blame_view, begin_x,
5198 s->selected_entry, &s->parents,
5199 s->commit_id, s->refs, s->repo);
5200 if (err)
5201 break;
5202 if (view_is_parent_view(view)) {
5203 err = view_close_child(view);
5204 if (err)
5205 return err;
5206 err = view_set_child(view, blame_view);
5207 if (err) {
5208 view_close(blame_view);
5209 break;
5211 *focus_view = blame_view;
5212 view->child_focussed = 1;
5213 } else
5214 *new_view = blame_view;
5216 break;
5217 case KEY_RESIZE:
5218 if (s->selected > view->nlines)
5219 s->selected = s->ndisplayed - 1;
5220 break;
5221 default:
5222 break;
5225 return err;
5228 __dead static void
5229 usage_tree(void)
5231 endwin();
5232 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5233 getprogname());
5234 exit(1);
5237 static const struct got_error *
5238 cmd_tree(int argc, char *argv[])
5240 const struct got_error *error;
5241 struct got_repository *repo = NULL;
5242 struct got_worktree *worktree = NULL;
5243 struct got_reflist_head refs;
5244 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5245 struct got_object_id *commit_id = NULL;
5246 char *commit_id_arg = NULL;
5247 struct got_commit_object *commit = NULL;
5248 struct got_tree_object *tree = NULL;
5249 int ch;
5250 struct tog_view *view;
5252 SIMPLEQ_INIT(&refs);
5254 #ifndef PROFILE
5255 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5256 NULL) == -1)
5257 err(1, "pledge");
5258 #endif
5260 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5261 switch (ch) {
5262 case 'c':
5263 commit_id_arg = optarg;
5264 break;
5265 case 'r':
5266 repo_path = realpath(optarg, NULL);
5267 if (repo_path == NULL)
5268 return got_error_from_errno2("realpath",
5269 optarg);
5270 break;
5271 default:
5272 usage_tree();
5273 /* NOTREACHED */
5277 argc -= optind;
5278 argv += optind;
5280 if (argc > 1)
5281 usage_tree();
5283 cwd = getcwd(NULL, 0);
5284 if (cwd == NULL)
5285 return got_error_from_errno("getcwd");
5287 error = got_worktree_open(&worktree, cwd);
5288 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5289 goto done;
5291 if (repo_path == NULL) {
5292 if (worktree)
5293 repo_path =
5294 strdup(got_worktree_get_repo_path(worktree));
5295 else
5296 repo_path = strdup(cwd);
5298 if (repo_path == NULL) {
5299 error = got_error_from_errno("strdup");
5300 goto done;
5303 error = got_repo_open(&repo, repo_path, NULL);
5304 if (error != NULL)
5305 goto done;
5307 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5308 repo, worktree);
5309 if (error)
5310 goto done;
5312 init_curses();
5314 error = apply_unveil(got_repo_get_path(repo), NULL);
5315 if (error)
5316 goto done;
5318 error = got_repo_match_object_id(&commit_id, NULL,
5319 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5320 GOT_OBJ_TYPE_COMMIT, 1, repo);
5321 if (error)
5322 goto done;
5324 error = got_object_open_as_commit(&commit, repo, commit_id);
5325 if (error)
5326 goto done;
5328 error = got_object_open_as_tree(&tree, repo,
5329 got_object_commit_get_tree_id(commit));
5330 if (error)
5331 goto done;
5333 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5334 if (error)
5335 goto done;
5337 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5338 if (view == NULL) {
5339 error = got_error_from_errno("view_open");
5340 goto done;
5342 error = open_tree_view(view, tree, commit_id, &refs, repo);
5343 if (error)
5344 goto done;
5345 if (!got_path_is_root_dir(in_repo_path)) {
5346 error = tree_view_walk_path(&view->state.tree, commit_id,
5347 in_repo_path, repo);
5348 if (error)
5349 goto done;
5352 if (worktree) {
5353 /* Release work tree lock. */
5354 got_worktree_close(worktree);
5355 worktree = NULL;
5357 error = view_loop(view);
5358 done:
5359 free(repo_path);
5360 free(cwd);
5361 free(commit_id);
5362 if (commit)
5363 got_object_commit_close(commit);
5364 if (tree)
5365 got_object_tree_close(tree);
5366 if (repo)
5367 got_repo_close(repo);
5368 got_ref_list_free(&refs);
5369 return error;
5372 static void
5373 list_commands(void)
5375 int i;
5377 fprintf(stderr, "commands:");
5378 for (i = 0; i < nitems(tog_commands); i++) {
5379 struct tog_cmd *cmd = &tog_commands[i];
5380 fprintf(stderr, " %s", cmd->name);
5382 fputc('\n', stderr);
5385 __dead static void
5386 usage(int hflag)
5388 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5389 "[arg ...]\n", getprogname());
5390 if (hflag) {
5391 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5392 list_commands();
5394 exit(1);
5397 static char **
5398 make_argv(int argc, ...)
5400 va_list ap;
5401 char **argv;
5402 int i;
5404 va_start(ap, argc);
5406 argv = calloc(argc, sizeof(char *));
5407 if (argv == NULL)
5408 err(1, "calloc");
5409 for (i = 0; i < argc; i++) {
5410 argv[i] = strdup(va_arg(ap, char *));
5411 if (argv[i] == NULL)
5412 err(1, "strdup");
5415 va_end(ap);
5416 return argv;
5420 * Try to convert 'tog path' into a 'tog log path' command.
5421 * The user could simply have mistyped the command rather than knowingly
5422 * provided a path. So check whether argv[0] can in fact be resolved
5423 * to a path in the HEAD commit and print a special error if not.
5424 * This hack is for mpi@ <3
5426 static const struct got_error *
5427 tog_log_with_path(int argc, char *argv[])
5429 const struct got_error *error = NULL;
5430 struct tog_cmd *cmd = NULL;
5431 struct got_repository *repo = NULL;
5432 struct got_worktree *worktree = NULL;
5433 struct got_object_id *commit_id = NULL, *id = NULL;
5434 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5435 char *commit_id_str = NULL, **cmd_argv = NULL;
5437 cwd = getcwd(NULL, 0);
5438 if (cwd == NULL)
5439 return got_error_from_errno("getcwd");
5441 error = got_worktree_open(&worktree, cwd);
5442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5443 goto done;
5445 if (worktree)
5446 repo_path = strdup(got_worktree_get_repo_path(worktree));
5447 else
5448 repo_path = strdup(cwd);
5449 if (repo_path == NULL) {
5450 error = got_error_from_errno("strdup");
5451 goto done;
5454 error = got_repo_open(&repo, repo_path, NULL);
5455 if (error != NULL)
5456 goto done;
5458 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5459 repo, worktree);
5460 if (error)
5461 goto done;
5463 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5464 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5465 GOT_OBJ_TYPE_COMMIT, 1, repo);
5466 if (error)
5467 goto done;
5469 if (worktree) {
5470 got_worktree_close(worktree);
5471 worktree = NULL;
5474 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5475 if (error) {
5476 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5477 goto done;
5478 fprintf(stderr, "%s: '%s' is no known command or path\n",
5479 getprogname(), argv[0]);
5480 usage(1);
5481 /* not reached */
5484 got_repo_close(repo);
5485 repo = NULL;
5487 error = got_object_id_str(&commit_id_str, commit_id);
5488 if (error)
5489 goto done;
5491 cmd = &tog_commands[0]; /* log */
5492 argc = 4;
5493 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5494 error = cmd->cmd_main(argc, cmd_argv);
5495 done:
5496 if (repo)
5497 got_repo_close(repo);
5498 if (worktree)
5499 got_worktree_close(worktree);
5500 free(id);
5501 free(commit_id_str);
5502 free(commit_id);
5503 free(cwd);
5504 free(repo_path);
5505 free(in_repo_path);
5506 if (cmd_argv) {
5507 int i;
5508 for (i = 0; i < argc; i++)
5509 free(cmd_argv[i]);
5510 free(cmd_argv);
5512 return error;
5515 int
5516 main(int argc, char *argv[])
5518 const struct got_error *error = NULL;
5519 struct tog_cmd *cmd = NULL;
5520 int ch, hflag = 0, Vflag = 0;
5521 char **cmd_argv = NULL;
5522 static struct option longopts[] = {
5523 { "version", no_argument, NULL, 'V' },
5524 { NULL, 0, NULL, 0}
5527 setlocale(LC_CTYPE, "");
5529 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5530 switch (ch) {
5531 case 'h':
5532 hflag = 1;
5533 break;
5534 case 'V':
5535 Vflag = 1;
5536 break;
5537 default:
5538 usage(hflag);
5539 /* NOTREACHED */
5543 argc -= optind;
5544 argv += optind;
5545 optind = 0;
5546 optreset = 1;
5548 if (Vflag) {
5549 got_version_print_str();
5550 return 1;
5553 if (argc == 0) {
5554 if (hflag)
5555 usage(hflag);
5556 /* Build an argument vector which runs a default command. */
5557 cmd = &tog_commands[0];
5558 argc = 1;
5559 cmd_argv = make_argv(argc, cmd->name);
5560 } else {
5561 int i;
5563 /* Did the user specify a command? */
5564 for (i = 0; i < nitems(tog_commands); i++) {
5565 if (strncmp(tog_commands[i].name, argv[0],
5566 strlen(argv[0])) == 0) {
5567 cmd = &tog_commands[i];
5568 break;
5573 if (cmd == NULL) {
5574 if (argc != 1)
5575 usage(0);
5576 /* No command specified; try log with a path */
5577 error = tog_log_with_path(argc, argv);
5578 } else {
5579 if (hflag)
5580 cmd->cmd_usage();
5581 else
5582 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5585 endwin();
5586 if (cmd_argv) {
5587 int i;
5588 for (i = 0; i < argc; i++)
5589 free(cmd_argv[i]);
5590 free(cmd_argv);
5593 if (error && error->code != GOT_ERR_CANCELLED)
5594 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5595 return 0;