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 ? "searching..." : "loading...") == -1) {
1516 err = got_error_from_errno("asprintf");
1517 goto done;
1519 } else {
1520 const char *search_str = NULL;
1522 if (view->searching) {
1523 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1524 search_str = "no more matches";
1525 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1526 search_str = "no matches found";
1527 else if (!view->search_next_done)
1528 search_str = "searching...";
1531 if (asprintf(&ncommits_str, " [%d/%d] %s",
1532 entry ? entry->idx + 1 : 0, commits->ncommits,
1533 search_str ? search_str :
1534 (refs_str ? refs_str : "")) == -1) {
1535 err = got_error_from_errno("asprintf");
1536 goto done;
1540 if (path && strcmp(path, "/") != 0) {
1541 if (asprintf(&header, "commit %s %s%s",
1542 id_str ? id_str : "........................................",
1543 path, ncommits_str) == -1) {
1544 err = got_error_from_errno("asprintf");
1545 header = NULL;
1546 goto done;
1548 } else if (asprintf(&header, "commit %s%s",
1549 id_str ? id_str : "........................................",
1550 ncommits_str) == -1) {
1551 err = got_error_from_errno("asprintf");
1552 header = NULL;
1553 goto done;
1555 err = format_line(&wline, &width, header, view->ncols, 0);
1556 if (err)
1557 goto done;
1559 werase(view->window);
1561 if (view_needs_focus_indication(view))
1562 wstandout(view->window);
1563 tc = get_color(colors, TOG_COLOR_COMMIT);
1564 if (tc)
1565 wattr_on(view->window,
1566 COLOR_PAIR(tc->colorpair), NULL);
1567 waddwstr(view->window, wline);
1568 if (tc)
1569 wattr_off(view->window,
1570 COLOR_PAIR(tc->colorpair), NULL);
1571 while (width < view->ncols) {
1572 waddch(view->window, ' ');
1573 width++;
1575 if (view_needs_focus_indication(view))
1576 wstandend(view->window);
1577 free(wline);
1578 if (limit <= 1)
1579 goto done;
1581 /* Grow author column size if necessary. */
1582 entry = first;
1583 ncommits = 0;
1584 while (entry) {
1585 char *author;
1586 wchar_t *wauthor;
1587 int width;
1588 if (ncommits >= limit - 1)
1589 break;
1590 author = strdup(got_object_commit_get_author(entry->commit));
1591 if (author == NULL) {
1592 err = got_error_from_errno("strdup");
1593 goto done;
1595 err = format_author(&wauthor, &width, author, COLS,
1596 date_display_cols);
1597 if (author_cols < width)
1598 author_cols = width;
1599 free(wauthor);
1600 free(author);
1601 ncommits++;
1602 entry = TAILQ_NEXT(entry, entry);
1605 entry = first;
1606 *last = first;
1607 ncommits = 0;
1608 while (entry) {
1609 if (ncommits >= limit - 1)
1610 break;
1611 if (ncommits == selected_idx)
1612 wstandout(view->window);
1613 err = draw_commit(view, entry->commit, entry->id, refs,
1614 date_display_cols, author_cols, colors);
1615 if (ncommits == selected_idx)
1616 wstandend(view->window);
1617 if (err)
1618 goto done;
1619 ncommits++;
1620 *last = entry;
1621 entry = TAILQ_NEXT(entry, entry);
1624 view_vborder(view);
1625 done:
1626 free(id_str);
1627 free(refs_str);
1628 free(ncommits_str);
1629 free(header);
1630 return err;
1633 static void
1634 scroll_up(struct tog_view *view,
1635 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1636 struct commit_queue *commits)
1638 struct commit_queue_entry *entry;
1639 int nscrolled = 0;
1641 entry = TAILQ_FIRST(&commits->head);
1642 if (*first_displayed_entry == entry)
1643 return;
1645 entry = *first_displayed_entry;
1646 while (entry && nscrolled < maxscroll) {
1647 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1648 if (entry) {
1649 *first_displayed_entry = entry;
1650 nscrolled++;
1655 static const struct got_error *
1656 trigger_log_thread(struct tog_view *log_view, int wait,
1657 int *commits_needed, int *log_complete,
1658 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1660 int errcode;
1662 halfdelay(1); /* fast refresh while loading commits */
1664 while (*commits_needed > 0) {
1665 if (*log_complete)
1666 break;
1668 /* Wake the log thread. */
1669 errcode = pthread_cond_signal(need_commits);
1670 if (errcode)
1671 return got_error_set_errno(errcode,
1672 "pthread_cond_signal");
1675 * The mutex will be released while the view loop waits
1676 * in wgetch(), at which time the log thread will run.
1678 if (!wait)
1679 break;
1681 /* Display progress update in log view. */
1682 show_log_view(log_view);
1683 update_panels();
1684 doupdate();
1686 /* Wait right here while next commit is being loaded. */
1687 errcode = pthread_cond_wait(commit_loaded, &tog_mutex);
1688 if (errcode)
1689 return got_error_set_errno(errcode,
1690 "pthread_cond_wait");
1692 /* Display progress update in log view. */
1693 show_log_view(log_view);
1694 update_panels();
1695 doupdate();
1698 return NULL;
1701 static const struct got_error *
1702 scroll_down(struct tog_view *log_view,
1703 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1704 struct commit_queue_entry **last_displayed_entry,
1705 struct commit_queue *commits, int *log_complete, int *commits_needed,
1706 pthread_cond_t *need_commits, pthread_cond_t *commit_loaded)
1708 const struct got_error *err = NULL;
1709 struct commit_queue_entry *pentry;
1710 int nscrolled = 0, ncommits_needed;
1712 if (*last_displayed_entry == NULL)
1713 return NULL;
1715 ncommits_needed = (*last_displayed_entry)->idx + 1 + maxscroll;
1716 if (commits->ncommits < ncommits_needed && !*log_complete) {
1718 * Ask the log thread for required amount of commits.
1720 (*commits_needed) += maxscroll;
1721 err = trigger_log_thread(log_view, 1, commits_needed,
1722 log_complete, need_commits, commit_loaded);
1723 if (err)
1724 return err;
1727 do {
1728 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1729 if (pentry == NULL)
1730 break;
1732 *last_displayed_entry = pentry;
1734 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1735 if (pentry == NULL)
1736 break;
1737 *first_displayed_entry = pentry;
1738 } while (++nscrolled < maxscroll);
1740 return err;
1743 static const struct got_error *
1744 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1745 struct got_commit_object *commit, struct got_object_id *commit_id,
1746 struct tog_view *log_view, struct got_reflist_head *refs,
1747 struct got_repository *repo)
1749 const struct got_error *err;
1750 struct got_object_qid *parent_id;
1751 struct tog_view *diff_view;
1753 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1754 if (diff_view == NULL)
1755 return got_error_from_errno("view_open");
1757 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1758 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1759 commit_id, log_view, refs, repo);
1760 if (err == NULL)
1761 *new_view = diff_view;
1762 return err;
1765 static const struct got_error *
1766 tree_view_visit_subtree(struct got_tree_object *subtree,
1767 struct tog_tree_view_state *s)
1769 struct tog_parent_tree *parent;
1771 parent = calloc(1, sizeof(*parent));
1772 if (parent == NULL)
1773 return got_error_from_errno("calloc");
1775 parent->tree = s->tree;
1776 parent->first_displayed_entry = s->first_displayed_entry;
1777 parent->selected_entry = s->selected_entry;
1778 parent->selected = s->selected;
1779 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1780 s->tree = subtree;
1781 s->selected = 0;
1782 s->first_displayed_entry = NULL;
1783 return NULL;
1786 static const struct got_error *
1787 tree_view_walk_path(struct tog_tree_view_state *s,
1788 struct got_object_id *commit_id,
1789 const char *path, struct got_repository *repo)
1791 const struct got_error *err = NULL;
1792 struct got_tree_object *tree = NULL;
1793 const char *p;
1794 char *slash, *subpath = NULL;
1796 /* Walk the path and open corresponding tree objects. */
1797 p = path;
1798 while (*p) {
1799 struct got_tree_entry *te;
1800 struct got_object_id *tree_id;
1801 char *te_name;
1803 while (p[0] == '/')
1804 p++;
1806 /* Ensure the correct subtree entry is selected. */
1807 slash = strchr(p, '/');
1808 if (slash == NULL)
1809 te_name = strdup(p);
1810 else
1811 te_name = strndup(p, slash - p);
1812 if (te_name == NULL) {
1813 err = got_error_from_errno("strndup");
1814 break;
1816 te = got_object_tree_find_entry(s->tree, te_name);
1817 if (te == NULL) {
1818 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
1819 free(te_name);
1820 break;
1822 free(te_name);
1823 s->selected_entry = te;
1824 s->selected = got_tree_entry_get_index(te);
1825 if (s->tree != s->root)
1826 s->selected++; /* skip '..' */
1828 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry))) {
1829 /* Jump to this file's entry. */
1830 s->first_displayed_entry = s->selected_entry;
1831 s->selected = 0;
1832 break;
1835 slash = strchr(p, '/');
1836 if (slash)
1837 subpath = strndup(path, slash - path);
1838 else
1839 subpath = strdup(path);
1840 if (subpath == NULL) {
1841 err = got_error_from_errno("strdup");
1842 break;
1845 err = got_object_id_by_path(&tree_id, repo, commit_id,
1846 subpath);
1847 if (err)
1848 break;
1850 err = got_object_open_as_tree(&tree, repo, tree_id);
1851 free(tree_id);
1852 if (err)
1853 break;
1855 err = tree_view_visit_subtree(tree, s);
1856 if (err) {
1857 got_object_tree_close(tree);
1858 break;
1860 if (slash == NULL)
1861 break;
1862 free(subpath);
1863 subpath = NULL;
1864 p = slash;
1867 free(subpath);
1868 return err;
1871 static const struct got_error *
1872 browse_commit_tree(struct tog_view **new_view, int begin_x,
1873 struct commit_queue_entry *entry, const char *path,
1874 struct got_reflist_head *refs, struct got_repository *repo)
1876 const struct got_error *err = NULL;
1877 struct got_tree_object *tree;
1878 struct tog_tree_view_state *s;
1879 struct tog_view *tree_view;
1881 err = got_object_open_as_tree(&tree, repo,
1882 got_object_commit_get_tree_id(entry->commit));
1883 if (err)
1884 return err;
1886 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1887 if (tree_view == NULL)
1888 return got_error_from_errno("view_open");
1890 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1891 if (err) {
1892 got_object_tree_close(tree);
1893 return err;
1895 s = &tree_view->state.tree;
1897 *new_view = tree_view;
1899 if (got_path_is_root_dir(path))
1900 return NULL;
1902 return tree_view_walk_path(s, entry->id, path, repo);
1905 static const struct got_error *
1906 block_signals_used_by_main_thread(void)
1908 sigset_t sigset;
1909 int errcode;
1911 if (sigemptyset(&sigset) == -1)
1912 return got_error_from_errno("sigemptyset");
1914 /* tog handles SIGWINCH and SIGCONT */
1915 if (sigaddset(&sigset, SIGWINCH) == -1)
1916 return got_error_from_errno("sigaddset");
1917 if (sigaddset(&sigset, SIGCONT) == -1)
1918 return got_error_from_errno("sigaddset");
1920 /* ncurses handles SIGTSTP */
1921 if (sigaddset(&sigset, SIGTSTP) == -1)
1922 return got_error_from_errno("sigaddset");
1924 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
1925 if (errcode)
1926 return got_error_set_errno(errcode, "pthread_sigmask");
1928 return NULL;
1931 static void *
1932 log_thread(void *arg)
1934 const struct got_error *err = NULL;
1935 int errcode = 0;
1936 struct tog_log_thread_args *a = arg;
1937 int done = 0;
1939 err = block_signals_used_by_main_thread();
1940 if (err)
1941 return (void *)err;
1943 while (!done && !err && !tog_sigpipe_received) {
1944 err = queue_commits(a->graph, a->commits, 1, a->repo,
1945 a->in_repo_path, a->searching, a->search_next_done,
1946 a->regex);
1947 if (err) {
1948 if (err->code != GOT_ERR_ITER_COMPLETED)
1949 return (void *)err;
1950 err = NULL;
1951 done = 1;
1952 } else if (a->commits_needed > 0)
1953 a->commits_needed--;
1955 errcode = pthread_mutex_lock(&tog_mutex);
1956 if (errcode) {
1957 err = got_error_set_errno(errcode,
1958 "pthread_mutex_lock");
1959 break;
1960 } else if (*a->quit)
1961 done = 1;
1962 else if (*a->first_displayed_entry == NULL) {
1963 *a->first_displayed_entry =
1964 TAILQ_FIRST(&a->commits->head);
1965 *a->selected_entry = *a->first_displayed_entry;
1968 errcode = pthread_cond_signal(&a->commit_loaded);
1969 if (errcode) {
1970 err = got_error_set_errno(errcode,
1971 "pthread_cond_signal");
1972 pthread_mutex_unlock(&tog_mutex);
1973 break;
1976 if (done)
1977 a->commits_needed = 0;
1978 else {
1979 if (a->commits_needed == 0) {
1980 errcode = pthread_cond_wait(&a->need_commits,
1981 &tog_mutex);
1982 if (errcode)
1983 err = got_error_set_errno(errcode,
1984 "pthread_cond_wait");
1988 errcode = pthread_mutex_unlock(&tog_mutex);
1989 if (errcode && err == NULL)
1990 err = got_error_set_errno(errcode,
1991 "pthread_mutex_unlock");
1993 a->log_complete = 1;
1994 return (void *)err;
1997 static const struct got_error *
1998 stop_log_thread(struct tog_log_view_state *s)
2000 const struct got_error *err = NULL;
2001 int errcode;
2003 if (s->thread) {
2004 s->quit = 1;
2005 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2006 if (errcode)
2007 return got_error_set_errno(errcode,
2008 "pthread_cond_signal");
2009 errcode = pthread_mutex_unlock(&tog_mutex);
2010 if (errcode)
2011 return got_error_set_errno(errcode,
2012 "pthread_mutex_unlock");
2013 errcode = pthread_join(s->thread, (void **)&err);
2014 if (errcode)
2015 return got_error_set_errno(errcode, "pthread_join");
2016 errcode = pthread_mutex_lock(&tog_mutex);
2017 if (errcode)
2018 return got_error_set_errno(errcode,
2019 "pthread_mutex_lock");
2020 s->thread = NULL;
2023 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2024 if (errcode && err == NULL)
2025 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2027 if (s->thread_args.repo) {
2028 got_repo_close(s->thread_args.repo);
2029 s->thread_args.repo = NULL;
2032 if (s->thread_args.graph) {
2033 got_commit_graph_close(s->thread_args.graph);
2034 s->thread_args.graph = NULL;
2037 return err;
2040 static const struct got_error *
2041 close_log_view(struct tog_view *view)
2043 const struct got_error *err = NULL;
2044 struct tog_log_view_state *s = &view->state.log;
2046 err = stop_log_thread(s);
2047 free_commits(&s->commits);
2048 free(s->in_repo_path);
2049 s->in_repo_path = NULL;
2050 free(s->start_id);
2051 s->start_id = NULL;
2052 return err;
2055 static const struct got_error *
2056 search_start_log_view(struct tog_view *view)
2058 struct tog_log_view_state *s = &view->state.log;
2060 s->matched_entry = NULL;
2061 s->search_entry = NULL;
2062 return NULL;
2065 static const struct got_error *
2066 search_next_log_view(struct tog_view *view)
2068 const struct got_error *err = NULL;
2069 struct tog_log_view_state *s = &view->state.log;
2070 struct commit_queue_entry *entry;
2072 /* Display progress update in log view. */
2073 show_log_view(view);
2074 update_panels();
2075 doupdate();
2077 if (s->search_entry) {
2078 int errcode, ch;
2079 errcode = pthread_mutex_unlock(&tog_mutex);
2080 if (errcode)
2081 return got_error_set_errno(errcode,
2082 "pthread_mutex_unlock");
2083 ch = wgetch(view->window);
2084 errcode = pthread_mutex_lock(&tog_mutex);
2085 if (errcode)
2086 return got_error_set_errno(errcode,
2087 "pthread_mutex_lock");
2088 if (ch == KEY_BACKSPACE) {
2089 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2090 return NULL;
2092 if (view->searching == TOG_SEARCH_FORWARD)
2093 entry = TAILQ_NEXT(s->search_entry, entry);
2094 else
2095 entry = TAILQ_PREV(s->search_entry,
2096 commit_queue_head, entry);
2097 } else if (s->matched_entry) {
2098 if (view->searching == TOG_SEARCH_FORWARD)
2099 entry = TAILQ_NEXT(s->matched_entry, entry);
2100 else
2101 entry = TAILQ_PREV(s->matched_entry,
2102 commit_queue_head, entry);
2103 } else {
2104 if (view->searching == TOG_SEARCH_FORWARD)
2105 entry = TAILQ_FIRST(&s->commits.head);
2106 else
2107 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2110 while (1) {
2111 int have_match = 0;
2113 if (entry == NULL) {
2114 if (s->thread_args.log_complete ||
2115 view->searching == TOG_SEARCH_BACKWARD) {
2116 view->search_next_done =
2117 (s->matched_entry == NULL ?
2118 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2119 s->search_entry = NULL;
2120 return NULL;
2123 * Poke the log thread for more commits and return,
2124 * allowing the main loop to make progress. Search
2125 * will resume at s->search_entry once we come back.
2127 s->thread_args.commits_needed++;
2128 return trigger_log_thread(view, 0,
2129 &s->thread_args.commits_needed,
2130 &s->thread_args.log_complete,
2131 &s->thread_args.need_commits,
2132 &s->thread_args.commit_loaded);
2135 err = match_commit(&have_match, entry->id, entry->commit,
2136 &view->regex);
2137 if (err)
2138 break;
2139 if (have_match) {
2140 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2141 s->matched_entry = entry;
2142 break;
2145 s->search_entry = entry;
2146 if (view->searching == TOG_SEARCH_FORWARD)
2147 entry = TAILQ_NEXT(entry, entry);
2148 else
2149 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2152 if (s->matched_entry) {
2153 int cur = s->selected_entry->idx;
2154 while (cur < s->matched_entry->idx) {
2155 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2156 if (err)
2157 return err;
2158 cur++;
2160 while (cur > s->matched_entry->idx) {
2161 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2162 if (err)
2163 return err;
2164 cur--;
2168 s->search_entry = NULL;
2170 return NULL;
2173 static const struct got_error *
2174 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2175 struct got_reflist_head *refs, struct got_repository *repo,
2176 const char *head_ref_name, const char *in_repo_path,
2177 int log_branches)
2179 const struct got_error *err = NULL;
2180 struct tog_log_view_state *s = &view->state.log;
2181 struct got_repository *thread_repo = NULL;
2182 struct got_commit_graph *thread_graph = NULL;
2183 int errcode;
2185 if (in_repo_path != s->in_repo_path) {
2186 free(s->in_repo_path);
2187 s->in_repo_path = strdup(in_repo_path);
2188 if (s->in_repo_path == NULL)
2189 return got_error_from_errno("strdup");
2192 /* The commit queue only contains commits being displayed. */
2193 TAILQ_INIT(&s->commits.head);
2194 s->commits.ncommits = 0;
2196 s->refs = refs;
2197 s->repo = repo;
2198 s->head_ref_name = head_ref_name;
2199 s->start_id = got_object_id_dup(start_id);
2200 if (s->start_id == NULL) {
2201 err = got_error_from_errno("got_object_id_dup");
2202 goto done;
2204 s->log_branches = log_branches;
2206 SIMPLEQ_INIT(&s->colors);
2207 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2208 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2209 get_color_value("TOG_COLOR_COMMIT"));
2210 if (err)
2211 goto done;
2212 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2213 get_color_value("TOG_COLOR_AUTHOR"));
2214 if (err) {
2215 free_colors(&s->colors);
2216 goto done;
2218 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2219 get_color_value("TOG_COLOR_DATE"));
2220 if (err) {
2221 free_colors(&s->colors);
2222 goto done;
2226 view->show = show_log_view;
2227 view->input = input_log_view;
2228 view->close = close_log_view;
2229 view->search_start = search_start_log_view;
2230 view->search_next = search_next_log_view;
2232 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2233 if (err)
2234 goto done;
2235 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2236 !s->log_branches);
2237 if (err)
2238 goto done;
2239 err = got_commit_graph_iter_start(thread_graph,
2240 s->start_id, s->repo, NULL, NULL);
2241 if (err)
2242 goto done;
2244 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2245 if (errcode) {
2246 err = got_error_set_errno(errcode, "pthread_cond_init");
2247 goto done;
2249 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2250 if (errcode) {
2251 err = got_error_set_errno(errcode, "pthread_cond_init");
2252 goto done;
2255 s->thread_args.commits_needed = view->nlines;
2256 s->thread_args.graph = thread_graph;
2257 s->thread_args.commits = &s->commits;
2258 s->thread_args.in_repo_path = s->in_repo_path;
2259 s->thread_args.start_id = s->start_id;
2260 s->thread_args.repo = thread_repo;
2261 s->thread_args.log_complete = 0;
2262 s->thread_args.quit = &s->quit;
2263 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2264 s->thread_args.selected_entry = &s->selected_entry;
2265 s->thread_args.searching = &view->searching;
2266 s->thread_args.search_next_done = &view->search_next_done;
2267 s->thread_args.regex = &view->regex;
2268 done:
2269 if (err)
2270 close_log_view(view);
2271 return err;
2274 static const struct got_error *
2275 show_log_view(struct tog_view *view)
2277 struct tog_log_view_state *s = &view->state.log;
2279 if (s->thread == NULL) {
2280 int errcode = pthread_create(&s->thread, NULL, log_thread,
2281 &s->thread_args);
2282 if (errcode)
2283 return got_error_set_errno(errcode, "pthread_create");
2286 return draw_commits(view, &s->last_displayed_entry,
2287 &s->selected_entry, s->first_displayed_entry,
2288 &s->commits, s->selected, view->nlines, s->refs,
2289 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2292 static const struct got_error *
2293 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2294 struct tog_view **focus_view, struct tog_view *view, int ch)
2296 const struct got_error *err = NULL;
2297 struct tog_log_view_state *s = &view->state.log;
2298 char *parent_path, *in_repo_path = NULL;
2299 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2300 int begin_x = 0;
2301 struct got_object_id *start_id;
2303 switch (ch) {
2304 case 'q':
2305 s->quit = 1;
2306 break;
2307 case 'k':
2308 case KEY_UP:
2309 case '<':
2310 case ',':
2311 if (s->first_displayed_entry == NULL)
2312 break;
2313 if (s->selected > 0)
2314 s->selected--;
2315 else
2316 scroll_up(view, &s->first_displayed_entry, 1,
2317 &s->commits);
2318 break;
2319 case KEY_PPAGE:
2320 case CTRL('b'):
2321 if (s->first_displayed_entry == NULL)
2322 break;
2323 if (TAILQ_FIRST(&s->commits.head) ==
2324 s->first_displayed_entry) {
2325 s->selected = 0;
2326 break;
2328 scroll_up(view, &s->first_displayed_entry,
2329 view->nlines - 1, &s->commits);
2330 break;
2331 case 'j':
2332 case KEY_DOWN:
2333 case '>':
2334 case '.':
2335 if (s->first_displayed_entry == NULL)
2336 break;
2337 if (s->selected < MIN(view->nlines - 2,
2338 s->commits.ncommits - 1)) {
2339 s->selected++;
2340 break;
2342 err = scroll_down(view, &s->first_displayed_entry, 1,
2343 &s->last_displayed_entry, &s->commits,
2344 &s->thread_args.log_complete,
2345 &s->thread_args.commits_needed,
2346 &s->thread_args.need_commits,
2347 &s->thread_args.commit_loaded);
2348 break;
2349 case KEY_NPAGE:
2350 case CTRL('f'): {
2351 struct commit_queue_entry *first;
2352 first = s->first_displayed_entry;
2353 if (first == NULL)
2354 break;
2355 err = scroll_down(view, &s->first_displayed_entry,
2356 view->nlines - 1, &s->last_displayed_entry,
2357 &s->commits, &s->thread_args.log_complete,
2358 &s->thread_args.commits_needed,
2359 &s->thread_args.need_commits,
2360 &s->thread_args.commit_loaded);
2361 if (err)
2362 break;
2363 if (first == s->first_displayed_entry &&
2364 s->selected < MIN(view->nlines - 2,
2365 s->commits.ncommits - 1)) {
2366 /* can't scroll further down */
2367 s->selected = MIN(view->nlines - 2,
2368 s->commits.ncommits - 1);
2370 err = NULL;
2371 break;
2373 case KEY_RESIZE:
2374 if (s->selected > view->nlines - 2)
2375 s->selected = view->nlines - 2;
2376 if (s->selected > s->commits.ncommits - 1)
2377 s->selected = s->commits.ncommits - 1;
2378 break;
2379 case KEY_ENTER:
2380 case ' ':
2381 case '\r':
2382 if (s->selected_entry == NULL)
2383 break;
2384 if (view_is_parent_view(view))
2385 begin_x = view_split_begin_x(view->begin_x);
2386 err = open_diff_view_for_commit(&diff_view, begin_x,
2387 s->selected_entry->commit, s->selected_entry->id,
2388 view, s->refs, s->repo);
2389 if (err)
2390 break;
2391 if (view_is_parent_view(view)) {
2392 err = view_close_child(view);
2393 if (err)
2394 return err;
2395 err = view_set_child(view, diff_view);
2396 if (err) {
2397 view_close(diff_view);
2398 break;
2400 *focus_view = diff_view;
2401 view->child_focussed = 1;
2402 } else
2403 *new_view = diff_view;
2404 break;
2405 case 't':
2406 if (s->selected_entry == NULL)
2407 break;
2408 if (view_is_parent_view(view))
2409 begin_x = view_split_begin_x(view->begin_x);
2410 err = browse_commit_tree(&tree_view, begin_x,
2411 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2412 if (err)
2413 break;
2414 if (view_is_parent_view(view)) {
2415 err = view_close_child(view);
2416 if (err)
2417 return err;
2418 err = view_set_child(view, tree_view);
2419 if (err) {
2420 view_close(tree_view);
2421 break;
2423 *focus_view = tree_view;
2424 view->child_focussed = 1;
2425 } else
2426 *new_view = tree_view;
2427 break;
2428 case KEY_BACKSPACE:
2429 if (strcmp(s->in_repo_path, "/") == 0)
2430 break;
2431 parent_path = dirname(s->in_repo_path);
2432 if (parent_path && strcmp(parent_path, ".") != 0) {
2433 err = stop_log_thread(s);
2434 if (err)
2435 return err;
2436 lv = view_open(view->nlines, view->ncols,
2437 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2438 if (lv == NULL)
2439 return got_error_from_errno(
2440 "view_open");
2441 err = open_log_view(lv, s->start_id, s->refs,
2442 s->repo, s->head_ref_name, parent_path,
2443 s->log_branches);
2444 if (err)
2445 return err;;
2446 if (view_is_parent_view(view))
2447 *new_view = lv;
2448 else {
2449 view_set_child(view->parent, lv);
2450 *focus_view = lv;
2452 return NULL;
2454 break;
2455 case CTRL('l'):
2456 err = stop_log_thread(s);
2457 if (err)
2458 return err;
2459 lv = view_open(view->nlines, view->ncols,
2460 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2461 if (lv == NULL)
2462 return got_error_from_errno("view_open");
2463 err = got_repo_match_object_id(&start_id, NULL,
2464 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2465 GOT_OBJ_TYPE_COMMIT, 1, s->repo);
2466 if (err) {
2467 view_close(lv);
2468 return err;
2470 in_repo_path = strdup(s->in_repo_path);
2471 if (in_repo_path == NULL) {
2472 free(start_id);
2473 view_close(lv);
2474 return got_error_from_errno("strdup");
2476 got_ref_list_free(s->refs);
2477 err = got_ref_list(s->refs, s->repo, NULL,
2478 got_ref_cmp_by_name, NULL);
2479 if (err) {
2480 free(start_id);
2481 view_close(lv);
2482 return err;
2484 err = open_log_view(lv, start_id, s->refs, s->repo,
2485 s->head_ref_name, in_repo_path, s->log_branches);
2486 if (err) {
2487 free(start_id);
2488 view_close(lv);
2489 return err;;
2491 *dead_view = view;
2492 *new_view = lv;
2493 break;
2494 case 'B':
2495 s->log_branches = !s->log_branches;
2496 err = stop_log_thread(s);
2497 if (err)
2498 return err;
2499 lv = view_open(view->nlines, view->ncols,
2500 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2501 if (lv == NULL)
2502 return got_error_from_errno("view_open");
2503 err = open_log_view(lv, s->start_id, s->refs, s->repo,
2504 s->head_ref_name, s->in_repo_path, s->log_branches);
2505 if (err) {
2506 view_close(lv);
2507 return err;;
2509 *dead_view = view;
2510 *new_view = lv;
2511 break;
2512 default:
2513 break;
2516 return err;
2519 static const struct got_error *
2520 apply_unveil(const char *repo_path, const char *worktree_path)
2522 const struct got_error *error;
2524 #ifdef PROFILE
2525 if (unveil("gmon.out", "rwc") != 0)
2526 return got_error_from_errno2("unveil", "gmon.out");
2527 #endif
2528 if (repo_path && unveil(repo_path, "r") != 0)
2529 return got_error_from_errno2("unveil", repo_path);
2531 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2532 return got_error_from_errno2("unveil", worktree_path);
2534 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2535 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2537 error = got_privsep_unveil_exec_helpers();
2538 if (error != NULL)
2539 return error;
2541 if (unveil(NULL, NULL) != 0)
2542 return got_error_from_errno("unveil");
2544 return NULL;
2547 static void
2548 init_curses(void)
2550 initscr();
2551 cbreak();
2552 halfdelay(1); /* Do fast refresh while initial view is loading. */
2553 noecho();
2554 nonl();
2555 intrflush(stdscr, FALSE);
2556 keypad(stdscr, TRUE);
2557 curs_set(0);
2558 if (getenv("TOG_COLORS") != NULL) {
2559 start_color();
2560 use_default_colors();
2562 signal(SIGWINCH, tog_sigwinch);
2563 signal(SIGPIPE, tog_sigpipe);
2564 signal(SIGCONT, tog_sigcont);
2567 static const struct got_error *
2568 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2569 struct got_repository *repo, struct got_worktree *worktree)
2571 const struct got_error *err = NULL;
2573 if (argc == 0) {
2574 *in_repo_path = strdup("/");
2575 if (*in_repo_path == NULL)
2576 return got_error_from_errno("strdup");
2577 return NULL;
2580 if (worktree) {
2581 const char *prefix = got_worktree_get_path_prefix(worktree);
2582 char *wt_path, *p;
2584 err = got_worktree_resolve_path(&wt_path, worktree, argv[0]);
2585 if (err)
2586 return err;
2588 if (asprintf(&p, "%s%s%s", prefix,
2589 (strcmp(prefix, "/") != 0) ? "/" : "", wt_path) == -1) {
2590 err = got_error_from_errno("asprintf");
2591 free(wt_path);
2592 return err;
2594 err = got_repo_map_path(in_repo_path, repo, p, 0);
2595 free(p);
2596 free(wt_path);
2597 } else
2598 err = got_repo_map_path(in_repo_path, repo, argv[0], 1);
2600 return err;
2603 static const struct got_error *
2604 cmd_log(int argc, char *argv[])
2606 const struct got_error *error;
2607 struct got_repository *repo = NULL;
2608 struct got_worktree *worktree = NULL;
2609 struct got_reflist_head refs;
2610 struct got_object_id *start_id = NULL;
2611 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2612 char *start_commit = NULL, *head_ref_name = NULL;
2613 int ch, log_branches = 0;
2614 struct tog_view *view;
2616 SIMPLEQ_INIT(&refs);
2618 #ifndef PROFILE
2619 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2620 NULL) == -1)
2621 err(1, "pledge");
2622 #endif
2624 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2625 switch (ch) {
2626 case 'b':
2627 log_branches = 1;
2628 break;
2629 case 'c':
2630 start_commit = optarg;
2631 break;
2632 case 'r':
2633 repo_path = realpath(optarg, NULL);
2634 if (repo_path == NULL)
2635 return got_error_from_errno2("realpath",
2636 optarg);
2637 break;
2638 default:
2639 usage_log();
2640 /* NOTREACHED */
2644 argc -= optind;
2645 argv += optind;
2647 if (argc > 1)
2648 usage_log();
2650 cwd = getcwd(NULL, 0);
2651 if (cwd == NULL)
2652 return got_error_from_errno("getcwd");
2654 error = got_worktree_open(&worktree, cwd);
2655 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2656 goto done;
2658 if (repo_path == NULL) {
2659 if (worktree)
2660 repo_path =
2661 strdup(got_worktree_get_repo_path(worktree));
2662 else
2663 repo_path = strdup(cwd);
2665 if (repo_path == NULL) {
2666 error = got_error_from_errno("strdup");
2667 goto done;
2670 error = got_repo_open(&repo, repo_path, NULL);
2671 if (error != NULL)
2672 goto done;
2674 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
2675 repo, worktree);
2676 if (error)
2677 goto done;
2679 init_curses();
2681 error = apply_unveil(got_repo_get_path(repo),
2682 worktree ? got_worktree_get_root_path(worktree) : NULL);
2683 if (error)
2684 goto done;
2686 if (start_commit == NULL)
2687 error = got_repo_match_object_id(&start_id, NULL, worktree ?
2688 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2689 GOT_OBJ_TYPE_COMMIT, 1, repo);
2690 else
2691 error = got_repo_match_object_id(&start_id, NULL, start_commit,
2692 GOT_OBJ_TYPE_COMMIT, 1, repo);
2693 if (error != NULL)
2694 goto done;
2696 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2697 if (error)
2698 goto done;
2700 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2701 if (view == NULL) {
2702 error = got_error_from_errno("view_open");
2703 goto done;
2705 if (worktree) {
2706 head_ref_name = strdup(
2707 got_worktree_get_head_ref_name(worktree));
2708 if (head_ref_name == NULL) {
2709 error = got_error_from_errno("strdup");
2710 goto done;
2713 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2714 in_repo_path, log_branches);
2715 if (error)
2716 goto done;
2717 if (worktree) {
2718 /* Release work tree lock. */
2719 got_worktree_close(worktree);
2720 worktree = NULL;
2722 error = view_loop(view);
2723 done:
2724 free(in_repo_path);
2725 free(repo_path);
2726 free(cwd);
2727 free(start_id);
2728 free(head_ref_name);
2729 if (repo)
2730 got_repo_close(repo);
2731 if (worktree)
2732 got_worktree_close(worktree);
2733 got_ref_list_free(&refs);
2734 return error;
2737 __dead static void
2738 usage_diff(void)
2740 endwin();
2741 fprintf(stderr, "usage: %s diff [-r repository-path] object1 object2\n",
2742 getprogname());
2743 exit(1);
2746 static char *
2747 parse_next_line(FILE *f, size_t *len)
2749 char *line;
2750 size_t linelen;
2751 size_t lineno;
2752 const char delim[3] = { '\0', '\0', '\0'};
2754 line = fparseln(f, &linelen, &lineno, delim, 0);
2755 if (len)
2756 *len = linelen;
2757 return line;
2760 static int
2761 match_line(const char *line, regex_t *regex)
2763 regmatch_t regmatch;
2765 return regexec(regex, line, 1, &regmatch, 0) == 0;
2768 struct tog_color *
2769 match_color(struct tog_colors *colors, const char *line)
2771 struct tog_color *tc = NULL;
2773 SIMPLEQ_FOREACH(tc, colors, entry) {
2774 if (match_line(line, &tc->regex))
2775 return tc;
2778 return NULL;
2781 static const struct got_error *
2782 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line, int nlines,
2783 int selected_line, int max_lines, int *last_displayed_line, int *eof,
2784 char *header, struct tog_colors *colors)
2786 const struct got_error *err;
2787 int lineno = 0, nprinted = 0;
2788 char *line;
2789 struct tog_color *tc;
2790 size_t len;
2791 wchar_t *wline;
2792 int width;
2794 rewind(f);
2795 werase(view->window);
2797 if (header) {
2798 err = format_line(&wline, &width, header, view->ncols, 0);
2799 if (err) {
2800 return err;
2803 if (view_needs_focus_indication(view))
2804 wstandout(view->window);
2805 waddwstr(view->window, wline);
2806 if (view_needs_focus_indication(view))
2807 wstandend(view->window);
2808 if (width <= view->ncols - 1)
2809 waddch(view->window, '\n');
2811 if (max_lines <= 1)
2812 return NULL;
2813 max_lines--;
2816 *eof = 0;
2817 while (nprinted < max_lines) {
2818 line = parse_next_line(f, &len);
2819 if (line == NULL) {
2820 *eof = 1;
2821 break;
2823 if (++lineno < *first_displayed_line) {
2824 free(line);
2825 continue;
2828 err = format_line(&wline, &width, line, view->ncols, 0);
2829 if (err) {
2830 free(line);
2831 return err;
2834 tc = match_color(colors, line);
2835 if (tc)
2836 wattr_on(view->window,
2837 COLOR_PAIR(tc->colorpair), NULL);
2838 waddwstr(view->window, wline);
2839 if (tc)
2840 wattr_off(view->window,
2841 COLOR_PAIR(tc->colorpair), NULL);
2842 if (width <= view->ncols - 1)
2843 waddch(view->window, '\n');
2844 if (++nprinted == 1)
2845 *first_displayed_line = lineno;
2846 free(line);
2847 free(wline);
2848 wline = NULL;
2850 *last_displayed_line = lineno;
2852 view_vborder(view);
2854 if (*eof) {
2855 while (nprinted < view->nlines) {
2856 waddch(view->window, '\n');
2857 nprinted++;
2860 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2861 if (err) {
2862 return err;
2865 wstandout(view->window);
2866 waddwstr(view->window, wline);
2867 wstandend(view->window);
2870 return NULL;
2873 static char *
2874 get_datestr(time_t *time, char *datebuf)
2876 struct tm mytm, *tm;
2877 char *p, *s;
2879 tm = gmtime_r(time, &mytm);
2880 if (tm == NULL)
2881 return NULL;
2882 s = asctime_r(tm, datebuf);
2883 if (s == NULL)
2884 return NULL;
2885 p = strchr(s, '\n');
2886 if (p)
2887 *p = '\0';
2888 return s;
2891 static const struct got_error *
2892 write_commit_info(struct got_object_id *commit_id,
2893 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2895 const struct got_error *err = NULL;
2896 char datebuf[26], *datestr;
2897 struct got_commit_object *commit;
2898 char *id_str = NULL, *logmsg = NULL;
2899 time_t committer_time;
2900 const char *author, *committer;
2901 char *refs_str = NULL;
2903 if (refs) {
2904 err = build_refs_str(&refs_str, refs, commit_id, repo);
2905 if (err)
2906 return err;
2909 err = got_object_open_as_commit(&commit, repo, commit_id);
2910 if (err)
2911 return err;
2913 err = got_object_id_str(&id_str, commit_id);
2914 if (err) {
2915 err = got_error_from_errno("got_object_id_str");
2916 goto done;
2919 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2920 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2921 err = got_error_from_errno("fprintf");
2922 goto done;
2924 if (fprintf(outfile, "from: %s\n",
2925 got_object_commit_get_author(commit)) < 0) {
2926 err = got_error_from_errno("fprintf");
2927 goto done;
2929 committer_time = got_object_commit_get_committer_time(commit);
2930 datestr = get_datestr(&committer_time, datebuf);
2931 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2932 err = got_error_from_errno("fprintf");
2933 goto done;
2935 author = got_object_commit_get_author(commit);
2936 committer = got_object_commit_get_committer(commit);
2937 if (strcmp(author, committer) != 0 &&
2938 fprintf(outfile, "via: %s\n", committer) < 0) {
2939 err = got_error_from_errno("fprintf");
2940 goto done;
2942 err = got_object_commit_get_logmsg(&logmsg, commit);
2943 if (err)
2944 goto done;
2945 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2946 err = got_error_from_errno("fprintf");
2947 goto done;
2949 done:
2950 free(id_str);
2951 free(logmsg);
2952 free(refs_str);
2953 got_object_commit_close(commit);
2954 return err;
2957 const struct got_error *
2958 get_filestream_info(size_t *filesize, int *nlines, off_t **line_offsets,
2959 FILE *infile)
2961 size_t len;
2962 char *buf = NULL;
2963 int i;
2964 size_t noffsets = 0;
2965 off_t off = 0;
2967 if (line_offsets)
2968 *line_offsets = NULL;
2969 if (filesize)
2970 *filesize = 0;
2971 if (nlines)
2972 *nlines = 0;
2974 if (fseek(infile, 0, SEEK_END) == -1)
2975 return got_error_from_errno("fseek");
2976 len = ftell(infile) + 1;
2977 if (ferror(infile))
2978 return got_error_from_errno("ftell");
2979 if (fseek(infile, 0, SEEK_SET) == -1)
2980 return got_error_from_errno("fseek");
2982 if (len == 0)
2983 return NULL;
2984 if ((buf = calloc(len, sizeof(char *))) == NULL)
2985 return got_error_from_errno("calloc");
2987 fread(buf, 1, len, infile);
2988 if (ferror(infile))
2989 return got_error_from_errno("fread");
2991 i = 0;
2992 if (line_offsets && nlines) {
2993 if (*line_offsets == NULL) {
2994 /* Have some data but perhaps no '\n'. */
2995 noffsets = 1;
2996 *nlines = 1;
2997 *line_offsets = calloc(1, sizeof(**line_offsets));
2998 if (*line_offsets == NULL)
2999 return got_error_from_errno("calloc");
3000 /* Skip forward over end of first line. */
3001 while (i < len) {
3002 if (buf[i] == '\n')
3003 break;
3004 i++;
3007 /* Scan '\n' offsets in remaining chunk of data. */
3008 while (i < len) {
3009 if (buf[i] != '\n') {
3010 i++;
3011 continue;
3013 (*nlines)++;
3014 if (noffsets < *nlines) {
3015 off_t *o = recallocarray(*line_offsets,
3016 noffsets, *nlines,
3017 sizeof(**line_offsets));
3018 if (o == NULL) {
3019 free(*line_offsets);
3020 *line_offsets = NULL;
3021 return got_error_from_errno(
3022 "recallocarray");
3024 *line_offsets = o;
3025 noffsets = *nlines;
3027 off = i + 1;
3028 (*line_offsets)[*nlines - 1] = off;
3029 i++;
3033 if (fflush(infile) != 0)
3034 return got_error_from_errno("fflush");
3035 rewind(infile);
3037 if (filesize)
3038 *filesize = len;
3040 return NULL;
3043 static const struct got_error *
3044 create_diff(struct tog_diff_view_state *s)
3046 const struct got_error *err = NULL;
3047 FILE *f = NULL;
3048 int obj_type;
3050 f = got_opentemp();
3051 if (f == NULL) {
3052 err = got_error_from_errno("got_opentemp");
3053 goto done;
3055 if (s->f && fclose(s->f) != 0) {
3056 err = got_error_from_errno("fclose");
3057 goto done;
3059 s->f = f;
3061 if (s->id1)
3062 err = got_object_get_type(&obj_type, s->repo, s->id1);
3063 else
3064 err = got_object_get_type(&obj_type, s->repo, s->id2);
3065 if (err)
3066 goto done;
3068 switch (obj_type) {
3069 case GOT_OBJ_TYPE_BLOB:
3070 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
3071 s->diff_context, 0, s->repo, s->f);
3072 break;
3073 case GOT_OBJ_TYPE_TREE:
3074 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
3075 s->diff_context, 0, s->repo, s->f);
3076 break;
3077 case GOT_OBJ_TYPE_COMMIT: {
3078 const struct got_object_id_queue *parent_ids;
3079 struct got_object_qid *pid;
3080 struct got_commit_object *commit2;
3082 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3083 if (err)
3084 goto done;
3085 /* Show commit info if we're diffing to a parent/root commit. */
3086 if (s->id1 == NULL) {
3087 err =write_commit_info(s->id2, s->refs, s->repo, s->f);
3088 if (err)
3089 goto done;
3090 } else {
3091 parent_ids = got_object_commit_get_parent_ids(commit2);
3092 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
3093 if (got_object_id_cmp(s->id1, pid->id) == 0) {
3094 err = write_commit_info(s->id2, s->refs,
3095 s->repo, s->f);
3096 if (err)
3097 goto done;
3098 break;
3102 got_object_commit_close(commit2);
3104 err = got_diff_objects_as_commits(s->id1, s->id2,
3105 s->diff_context, 0, s->repo, s->f);
3106 break;
3108 default:
3109 err = got_error(GOT_ERR_OBJ_TYPE);
3110 break;
3112 if (err)
3113 goto done;
3114 err = get_filestream_info(&s->filesize, &s->nlines, &s->line_offsets,
3115 s->f);
3116 done:
3117 if (s->f && fflush(s->f) != 0 && err == NULL)
3118 err = got_error_from_errno("fflush");
3119 return err;
3122 static void
3123 diff_view_indicate_progress(struct tog_view *view)
3125 mvwaddstr(view->window, 0, 0, "diffing...");
3126 update_panels();
3127 doupdate();
3130 static const struct got_error *
3131 search_start_diff_view(struct tog_view *view)
3133 struct tog_diff_view_state *s = &view->state.diff;
3135 s->matched_line = 0;
3136 return NULL;
3139 static const struct got_error *
3140 search_next_diff_view(struct tog_view *view)
3142 struct tog_diff_view_state *s = &view->state.diff;
3143 int lineno;
3145 if (!view->searching) {
3146 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3147 return NULL;
3150 if (s->matched_line) {
3151 if (view->searching == TOG_SEARCH_FORWARD)
3152 lineno = s->matched_line + 1;
3153 else
3154 lineno = s->matched_line - 1;
3155 } else {
3156 if (view->searching == TOG_SEARCH_FORWARD)
3157 lineno = 1;
3158 else
3159 lineno = s->nlines;
3162 while (1) {
3163 char *line = NULL;
3164 off_t offset;
3165 size_t len;
3167 if (lineno <= 0 || lineno > s->nlines) {
3168 if (s->matched_line == 0) {
3169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3170 free(line);
3171 break;
3174 if (view->searching == TOG_SEARCH_FORWARD)
3175 lineno = 1;
3176 else
3177 lineno = s->nlines;
3180 offset = s->line_offsets[lineno - 1];
3181 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3182 free(line);
3183 return got_error_from_errno("fseeko");
3185 free(line);
3186 line = parse_next_line(s->f, &len);
3187 if (line && match_line(line, &view->regex)) {
3188 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3189 s->matched_line = lineno;
3190 free(line);
3191 break;
3193 free(line);
3194 if (view->searching == TOG_SEARCH_FORWARD)
3195 lineno++;
3196 else
3197 lineno--;
3200 if (s->matched_line) {
3201 s->first_displayed_line = s->matched_line;
3202 s->selected_line = 1;
3205 return NULL;
3208 static const struct got_error *
3209 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3210 struct got_object_id *id2, struct tog_view *log_view,
3211 struct got_reflist_head *refs, struct got_repository *repo)
3213 const struct got_error *err;
3214 struct tog_diff_view_state *s = &view->state.diff;
3216 if (id1 != NULL && id2 != NULL) {
3217 int type1, type2;
3218 err = got_object_get_type(&type1, repo, id1);
3219 if (err)
3220 return err;
3221 err = got_object_get_type(&type2, repo, id2);
3222 if (err)
3223 return err;
3225 if (type1 != type2)
3226 return got_error(GOT_ERR_OBJ_TYPE);
3228 s->first_displayed_line = 1;
3229 s->last_displayed_line = view->nlines;
3230 s->selected_line = 1;
3231 s->repo = repo;
3232 s->refs = refs;
3233 s->id1 = id1;
3234 s->id2 = id2;
3236 if (id1) {
3237 s->id1 = got_object_id_dup(id1);
3238 if (s->id1 == NULL)
3239 return got_error_from_errno("got_object_id_dup");
3240 } else
3241 s->id1 = NULL;
3243 s->id2 = got_object_id_dup(id2);
3244 if (s->id2 == NULL) {
3245 free(s->id1);
3246 s->id1 = NULL;
3247 return got_error_from_errno("got_object_id_dup");
3249 s->f = NULL;
3250 s->first_displayed_line = 1;
3251 s->last_displayed_line = view->nlines;
3252 s->diff_context = 3;
3253 s->log_view = log_view;
3254 s->repo = repo;
3255 s->refs = refs;
3257 SIMPLEQ_INIT(&s->colors);
3258 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3259 err = add_color(&s->colors,
3260 "^-", TOG_COLOR_DIFF_MINUS,
3261 get_color_value("TOG_COLOR_DIFF_MINUS"));
3262 if (err)
3263 return err;
3264 err = add_color(&s->colors, "^\\+",
3265 TOG_COLOR_DIFF_PLUS,
3266 get_color_value("TOG_COLOR_DIFF_PLUS"));
3267 if (err) {
3268 free_colors(&s->colors);
3269 return err;
3271 err = add_color(&s->colors,
3272 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3273 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3274 if (err) {
3275 free_colors(&s->colors);
3276 return err;
3279 err = add_color(&s->colors,
3280 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
3281 get_color_value("TOG_COLOR_DIFF_META"));
3282 if (err) {
3283 free_colors(&s->colors);
3284 return err;
3287 err = add_color(&s->colors,
3288 "^(from|via): ", TOG_COLOR_AUTHOR,
3289 get_color_value("TOG_COLOR_AUTHOR"));
3290 if (err) {
3291 free_colors(&s->colors);
3292 return err;
3295 err = add_color(&s->colors,
3296 "^date: ", TOG_COLOR_DATE,
3297 get_color_value("TOG_COLOR_DATE"));
3298 if (err) {
3299 free_colors(&s->colors);
3300 return err;
3304 if (log_view && view_is_splitscreen(view))
3305 show_log_view(log_view); /* draw vborder */
3306 diff_view_indicate_progress(view);
3308 err = create_diff(s);
3309 if (err) {
3310 free(s->id1);
3311 s->id1 = NULL;
3312 free(s->id2);
3313 s->id2 = NULL;
3314 return err;
3317 view->show = show_diff_view;
3318 view->input = input_diff_view;
3319 view->close = close_diff_view;
3320 view->search_start = search_start_diff_view;
3321 view->search_next = search_next_diff_view;
3323 return NULL;
3326 static const struct got_error *
3327 close_diff_view(struct tog_view *view)
3329 const struct got_error *err = NULL;
3330 struct tog_diff_view_state *s = &view->state.diff;
3332 free(s->id1);
3333 s->id1 = NULL;
3334 free(s->id2);
3335 s->id2 = NULL;
3336 if (s->f && fclose(s->f) == EOF)
3337 err = got_error_from_errno("fclose");
3338 free_colors(&s->colors);
3339 free(s->line_offsets);
3340 return err;
3343 static const struct got_error *
3344 show_diff_view(struct tog_view *view)
3346 const struct got_error *err;
3347 struct tog_diff_view_state *s = &view->state.diff;
3348 char *id_str1 = NULL, *id_str2, *header;
3350 if (s->id1) {
3351 err = got_object_id_str(&id_str1, s->id1);
3352 if (err)
3353 return err;
3355 err = got_object_id_str(&id_str2, s->id2);
3356 if (err)
3357 return err;
3359 if (asprintf(&header, "diff %s %s",
3360 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3361 err = got_error_from_errno("asprintf");
3362 free(id_str1);
3363 free(id_str2);
3364 return err;
3366 free(id_str1);
3367 free(id_str2);
3369 return draw_file(view, s->f, &s->first_displayed_line, s->nlines,
3370 s->selected_line, view->nlines, &s->last_displayed_line, &s->eof,
3371 header, &s->colors);
3374 static const struct got_error *
3375 set_selected_commit(struct tog_diff_view_state *s,
3376 struct commit_queue_entry *entry)
3378 const struct got_error *err;
3379 const struct got_object_id_queue *parent_ids;
3380 struct got_commit_object *selected_commit;
3381 struct got_object_qid *pid;
3383 free(s->id2);
3384 s->id2 = got_object_id_dup(entry->id);
3385 if (s->id2 == NULL)
3386 return got_error_from_errno("got_object_id_dup");
3388 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3389 if (err)
3390 return err;
3391 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3392 free(s->id1);
3393 pid = SIMPLEQ_FIRST(parent_ids);
3394 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3395 got_object_commit_close(selected_commit);
3396 return NULL;
3399 static const struct got_error *
3400 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3401 struct tog_view **focus_view, struct tog_view *view, int ch)
3403 const struct got_error *err = NULL;
3404 struct tog_diff_view_state *s = &view->state.diff;
3405 struct tog_log_view_state *ls;
3406 struct commit_queue_entry *entry;
3407 int i;
3409 switch (ch) {
3410 case 'k':
3411 case KEY_UP:
3412 if (s->first_displayed_line > 1)
3413 s->first_displayed_line--;
3414 break;
3415 case KEY_PPAGE:
3416 case CTRL('b'):
3417 if (s->first_displayed_line == 1)
3418 break;
3419 i = 0;
3420 while (i++ < view->nlines - 1 &&
3421 s->first_displayed_line > 1)
3422 s->first_displayed_line--;
3423 break;
3424 case 'j':
3425 case KEY_DOWN:
3426 if (!s->eof)
3427 s->first_displayed_line++;
3428 break;
3429 case KEY_NPAGE:
3430 case CTRL('f'):
3431 case ' ':
3432 if (s->eof)
3433 break;
3434 i = 0;
3435 while (!s->eof && i++ < view->nlines - 1) {
3436 char *line;
3437 line = parse_next_line(s->f, NULL);
3438 s->first_displayed_line++;
3439 if (line == NULL)
3440 break;
3442 break;
3443 case '[':
3444 if (s->diff_context > 0) {
3445 s->diff_context--;
3446 diff_view_indicate_progress(view);
3447 err = create_diff(s);
3449 break;
3450 case ']':
3451 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3452 s->diff_context++;
3453 diff_view_indicate_progress(view);
3454 err = create_diff(s);
3456 break;
3457 case '<':
3458 case ',':
3459 if (s->log_view == NULL)
3460 break;
3461 ls = &s->log_view->state.log;
3462 entry = TAILQ_PREV(ls->selected_entry,
3463 commit_queue_head, entry);
3464 if (entry == NULL)
3465 break;
3467 err = input_log_view(NULL, NULL, NULL, s->log_view,
3468 KEY_UP);
3469 if (err)
3470 break;
3472 err = set_selected_commit(s, entry);
3473 if (err)
3474 break;
3476 s->first_displayed_line = 1;
3477 s->last_displayed_line = view->nlines;
3479 diff_view_indicate_progress(view);
3480 err = create_diff(s);
3481 break;
3482 case '>':
3483 case '.':
3484 if (s->log_view == NULL)
3485 break;
3486 ls = &s->log_view->state.log;
3488 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3489 ls->thread_args.commits_needed++;
3490 err = trigger_log_thread(s->log_view, 1,
3491 &ls->thread_args.commits_needed,
3492 &ls->thread_args.log_complete,
3493 &ls->thread_args.need_commits,
3494 &ls->thread_args.commit_loaded);
3495 if (err)
3496 break;
3498 err = input_log_view(NULL, NULL, NULL, s->log_view,
3499 KEY_DOWN);
3500 if (err)
3501 break;
3503 entry = TAILQ_NEXT(ls->selected_entry, entry);
3504 if (entry == NULL)
3505 break;
3507 err = set_selected_commit(s, entry);
3508 if (err)
3509 break;
3511 s->first_displayed_line = 1;
3512 s->last_displayed_line = view->nlines;
3514 diff_view_indicate_progress(view);
3515 err = create_diff(s);
3516 break;
3517 default:
3518 break;
3521 return err;
3524 static const struct got_error *
3525 cmd_diff(int argc, char *argv[])
3527 const struct got_error *error = NULL;
3528 struct got_repository *repo = NULL;
3529 struct got_worktree *worktree = NULL;
3530 struct got_reflist_head refs;
3531 struct got_object_id *id1 = NULL, *id2 = NULL;
3532 char *repo_path = NULL, *cwd = NULL;
3533 char *id_str1 = NULL, *id_str2 = NULL;
3534 int ch;
3535 struct tog_view *view;
3537 SIMPLEQ_INIT(&refs);
3539 #ifndef PROFILE
3540 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3541 NULL) == -1)
3542 err(1, "pledge");
3543 #endif
3545 while ((ch = getopt(argc, argv, "r:")) != -1) {
3546 switch (ch) {
3547 case 'r':
3548 repo_path = realpath(optarg, NULL);
3549 if (repo_path == NULL)
3550 return got_error_from_errno2("realpath",
3551 optarg);
3552 break;
3553 default:
3554 usage_diff();
3555 /* NOTREACHED */
3559 argc -= optind;
3560 argv += optind;
3562 if (argc == 0) {
3563 usage_diff(); /* TODO show local worktree changes */
3564 } else if (argc == 2) {
3565 id_str1 = argv[0];
3566 id_str2 = argv[1];
3567 } else
3568 usage_diff();
3570 cwd = getcwd(NULL, 0);
3571 if (cwd == NULL)
3572 return got_error_from_errno("getcwd");
3574 error = got_worktree_open(&worktree, cwd);
3575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3576 goto done;
3578 if (repo_path == NULL) {
3579 if (worktree)
3580 repo_path =
3581 strdup(got_worktree_get_repo_path(worktree));
3582 else
3583 repo_path = strdup(cwd);
3585 if (repo_path == NULL) {
3586 error = got_error_from_errno("strdup");
3587 goto done;
3590 error = got_repo_open(&repo, repo_path, NULL);
3591 if (error)
3592 goto done;
3594 init_curses();
3596 error = apply_unveil(got_repo_get_path(repo), NULL);
3597 if (error)
3598 goto done;
3600 error = got_repo_match_object_id_prefix(&id1, id_str1,
3601 GOT_OBJ_TYPE_ANY, repo);
3602 if (error)
3603 goto done;
3605 error = got_repo_match_object_id_prefix(&id2, id_str2,
3606 GOT_OBJ_TYPE_ANY, repo);
3607 if (error)
3608 goto done;
3610 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3611 if (error)
3612 goto done;
3614 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3615 if (view == NULL) {
3616 error = got_error_from_errno("view_open");
3617 goto done;
3619 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3620 if (error)
3621 goto done;
3622 error = view_loop(view);
3623 done:
3624 free(repo_path);
3625 free(cwd);
3626 if (repo)
3627 got_repo_close(repo);
3628 if (worktree)
3629 got_worktree_close(worktree);
3630 got_ref_list_free(&refs);
3631 return error;
3634 __dead static void
3635 usage_blame(void)
3637 endwin();
3638 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3639 getprogname());
3640 exit(1);
3643 struct tog_blame_line {
3644 int annotated;
3645 struct got_object_id *id;
3648 static const struct got_error *
3649 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3650 const char *path, struct tog_blame_line *lines, int nlines,
3651 int blame_complete, int selected_line, int *first_displayed_line,
3652 int *last_displayed_line, int *eof, int max_lines,
3653 struct tog_colors *colors)
3655 const struct got_error *err;
3656 int lineno = 0, nprinted = 0;
3657 char *line;
3658 size_t len;
3659 wchar_t *wline;
3660 int width;
3661 struct tog_blame_line *blame_line;
3662 struct got_object_id *prev_id = NULL;
3663 char *id_str;
3664 struct tog_color *tc;
3666 err = got_object_id_str(&id_str, id);
3667 if (err)
3668 return err;
3670 rewind(f);
3671 werase(view->window);
3673 if (asprintf(&line, "commit %s", id_str) == -1) {
3674 err = got_error_from_errno("asprintf");
3675 free(id_str);
3676 return err;
3679 err = format_line(&wline, &width, line, view->ncols, 0);
3680 free(line);
3681 line = NULL;
3682 if (err)
3683 return err;
3684 if (view_needs_focus_indication(view))
3685 wstandout(view->window);
3686 tc = get_color(colors, TOG_COLOR_COMMIT);
3687 if (tc)
3688 wattr_on(view->window,
3689 COLOR_PAIR(tc->colorpair), NULL);
3690 waddwstr(view->window, wline);
3691 if (tc)
3692 wattr_off(view->window,
3693 COLOR_PAIR(tc->colorpair), NULL);
3694 if (view_needs_focus_indication(view))
3695 wstandend(view->window);
3696 free(wline);
3697 wline = NULL;
3698 if (width < view->ncols - 1)
3699 waddch(view->window, '\n');
3701 if (asprintf(&line, "[%d/%d] %s%s",
3702 *first_displayed_line - 1 + selected_line, nlines,
3703 blame_complete ? "" : "annotating... ", path) == -1) {
3704 free(id_str);
3705 return got_error_from_errno("asprintf");
3707 free(id_str);
3708 err = format_line(&wline, &width, line, view->ncols, 0);
3709 free(line);
3710 line = NULL;
3711 if (err)
3712 return err;
3713 waddwstr(view->window, wline);
3714 free(wline);
3715 wline = NULL;
3716 if (width < view->ncols - 1)
3717 waddch(view->window, '\n');
3719 *eof = 0;
3720 while (nprinted < max_lines - 2) {
3721 line = parse_next_line(f, &len);
3722 if (line == NULL) {
3723 *eof = 1;
3724 break;
3726 if (++lineno < *first_displayed_line) {
3727 free(line);
3728 continue;
3731 if (view->ncols <= 9) {
3732 width = 9;
3733 wline = wcsdup(L"");
3734 if (wline == NULL)
3735 err = got_error_from_errno("wcsdup");
3736 } else {
3737 err = format_line(&wline, &width, line,
3738 view->ncols - 9, 9);
3739 width += 9;
3741 if (err) {
3742 free(line);
3743 return err;
3746 if (view->focussed && nprinted == selected_line - 1)
3747 wstandout(view->window);
3749 if (nlines > 0) {
3750 blame_line = &lines[lineno - 1];
3751 if (blame_line->annotated && prev_id &&
3752 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3753 !(view->focussed &&
3754 nprinted == selected_line - 1)) {
3755 waddstr(view->window, " ");
3756 } else if (blame_line->annotated) {
3757 char *id_str;
3758 err = got_object_id_str(&id_str, blame_line->id);
3759 if (err) {
3760 free(line);
3761 free(wline);
3762 return err;
3764 tc = get_color(colors, TOG_COLOR_COMMIT);
3765 if (tc)
3766 wattr_on(view->window,
3767 COLOR_PAIR(tc->colorpair), NULL);
3768 wprintw(view->window, "%.8s", id_str);
3769 if (tc)
3770 wattr_off(view->window,
3771 COLOR_PAIR(tc->colorpair), NULL);
3772 free(id_str);
3773 prev_id = blame_line->id;
3774 } else {
3775 waddstr(view->window, "........");
3776 prev_id = NULL;
3778 } else {
3779 waddstr(view->window, "........");
3780 prev_id = NULL;
3783 if (view->focussed && nprinted == selected_line - 1)
3784 wstandend(view->window);
3785 waddstr(view->window, " ");
3787 waddwstr(view->window, wline);
3788 if (width <= view->ncols - 1)
3789 waddch(view->window, '\n');
3790 if (++nprinted == 1)
3791 *first_displayed_line = lineno;
3792 free(line);
3793 free(wline);
3794 wline = NULL;
3796 *last_displayed_line = lineno;
3798 view_vborder(view);
3800 return NULL;
3803 static const struct got_error *
3804 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3806 const struct got_error *err = NULL;
3807 struct tog_blame_cb_args *a = arg;
3808 struct tog_blame_line *line;
3809 int errcode;
3811 if (nlines != a->nlines ||
3812 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3813 return got_error(GOT_ERR_RANGE);
3815 errcode = pthread_mutex_lock(&tog_mutex);
3816 if (errcode)
3817 return got_error_set_errno(errcode, "pthread_mutex_lock");
3819 if (*a->quit) { /* user has quit the blame view */
3820 err = got_error(GOT_ERR_ITER_COMPLETED);
3821 goto done;
3824 if (lineno == -1)
3825 goto done; /* no change in this commit */
3827 line = &a->lines[lineno - 1];
3828 if (line->annotated)
3829 goto done;
3831 line->id = got_object_id_dup(id);
3832 if (line->id == NULL) {
3833 err = got_error_from_errno("got_object_id_dup");
3834 goto done;
3836 line->annotated = 1;
3837 done:
3838 errcode = pthread_mutex_unlock(&tog_mutex);
3839 if (errcode)
3840 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3841 return err;
3844 static void *
3845 blame_thread(void *arg)
3847 const struct got_error *err;
3848 struct tog_blame_thread_args *ta = arg;
3849 struct tog_blame_cb_args *a = ta->cb_args;
3850 int errcode;
3852 err = block_signals_used_by_main_thread();
3853 if (err)
3854 return (void *)err;
3856 err = got_blame(ta->path, a->commit_id, ta->repo,
3857 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3858 if (err && err->code == GOT_ERR_CANCELLED)
3859 err = NULL;
3861 errcode = pthread_mutex_lock(&tog_mutex);
3862 if (errcode)
3863 return (void *)got_error_set_errno(errcode,
3864 "pthread_mutex_lock");
3866 got_repo_close(ta->repo);
3867 ta->repo = NULL;
3868 *ta->complete = 1;
3870 errcode = pthread_mutex_unlock(&tog_mutex);
3871 if (errcode && err == NULL)
3872 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3874 return (void *)err;
3877 static struct got_object_id *
3878 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3879 int first_displayed_line, int selected_line)
3881 struct tog_blame_line *line;
3883 if (nlines <= 0)
3884 return NULL;
3886 line = &lines[first_displayed_line - 1 + selected_line - 1];
3887 if (!line->annotated)
3888 return NULL;
3890 return line->id;
3893 static const struct got_error *
3894 stop_blame(struct tog_blame *blame)
3896 const struct got_error *err = NULL;
3897 int i;
3899 if (blame->thread) {
3900 int errcode;
3901 errcode = pthread_mutex_unlock(&tog_mutex);
3902 if (errcode)
3903 return got_error_set_errno(errcode,
3904 "pthread_mutex_unlock");
3905 errcode = pthread_join(blame->thread, (void **)&err);
3906 if (errcode)
3907 return got_error_set_errno(errcode, "pthread_join");
3908 errcode = pthread_mutex_lock(&tog_mutex);
3909 if (errcode)
3910 return got_error_set_errno(errcode,
3911 "pthread_mutex_lock");
3912 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3913 err = NULL;
3914 blame->thread = NULL;
3916 if (blame->thread_args.repo) {
3917 got_repo_close(blame->thread_args.repo);
3918 blame->thread_args.repo = NULL;
3920 if (blame->f) {
3921 if (fclose(blame->f) != 0 && err == NULL)
3922 err = got_error_from_errno("fclose");
3923 blame->f = NULL;
3925 if (blame->lines) {
3926 for (i = 0; i < blame->nlines; i++)
3927 free(blame->lines[i].id);
3928 free(blame->lines);
3929 blame->lines = NULL;
3931 free(blame->cb_args.commit_id);
3932 blame->cb_args.commit_id = NULL;
3934 return err;
3937 static const struct got_error *
3938 cancel_blame_view(void *arg)
3940 const struct got_error *err = NULL;
3941 int *done = arg;
3942 int errcode;
3944 errcode = pthread_mutex_lock(&tog_mutex);
3945 if (errcode)
3946 return got_error_set_errno(errcode,
3947 "pthread_mutex_unlock");
3949 if (*done)
3950 err = got_error(GOT_ERR_CANCELLED);
3952 errcode = pthread_mutex_unlock(&tog_mutex);
3953 if (errcode)
3954 return got_error_set_errno(errcode,
3955 "pthread_mutex_lock");
3957 return err;
3960 static const struct got_error *
3961 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3962 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3963 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3964 struct got_repository *repo)
3966 const struct got_error *err = NULL;
3967 struct got_blob_object *blob = NULL;
3968 struct got_repository *thread_repo = NULL;
3969 struct got_object_id *obj_id = NULL;
3970 int obj_type;
3972 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3973 if (err)
3974 return err;
3976 err = got_object_get_type(&obj_type, repo, obj_id);
3977 if (err)
3978 goto done;
3980 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3981 err = got_error(GOT_ERR_OBJ_TYPE);
3982 goto done;
3985 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3986 if (err)
3987 goto done;
3988 blame->f = got_opentemp();
3989 if (blame->f == NULL) {
3990 err = got_error_from_errno("got_opentemp");
3991 goto done;
3993 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3994 &blame->line_offsets, blame->f, blob);
3995 if (err || blame->nlines == 0)
3996 goto done;
3998 /* Don't include \n at EOF in the blame line count. */
3999 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4000 blame->nlines--;
4002 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4003 if (blame->lines == NULL) {
4004 err = got_error_from_errno("calloc");
4005 goto done;
4008 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
4009 if (err)
4010 goto done;
4012 blame->cb_args.view = view;
4013 blame->cb_args.lines = blame->lines;
4014 blame->cb_args.nlines = blame->nlines;
4015 blame->cb_args.commit_id = got_object_id_dup(commit_id);
4016 if (blame->cb_args.commit_id == NULL) {
4017 err = got_error_from_errno("got_object_id_dup");
4018 goto done;
4020 blame->cb_args.quit = done;
4022 blame->thread_args.path = path;
4023 blame->thread_args.repo = thread_repo;
4024 blame->thread_args.cb_args = &blame->cb_args;
4025 blame->thread_args.complete = blame_complete;
4026 blame->thread_args.cancel_cb = cancel_blame_view;
4027 blame->thread_args.cancel_arg = done;
4028 *blame_complete = 0;
4030 done:
4031 if (blob)
4032 got_object_blob_close(blob);
4033 free(obj_id);
4034 if (err)
4035 stop_blame(blame);
4036 return err;
4039 static const struct got_error *
4040 open_blame_view(struct tog_view *view, char *path,
4041 struct got_object_id *commit_id, struct got_reflist_head *refs,
4042 struct got_repository *repo)
4044 const struct got_error *err = NULL;
4045 struct tog_blame_view_state *s = &view->state.blame;
4047 SIMPLEQ_INIT(&s->blamed_commits);
4049 s->path = strdup(path);
4050 if (s->path == NULL)
4051 return got_error_from_errno("strdup");
4053 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4054 if (err) {
4055 free(s->path);
4056 return err;
4059 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4060 s->first_displayed_line = 1;
4061 s->last_displayed_line = view->nlines;
4062 s->selected_line = 1;
4063 s->blame_complete = 0;
4064 s->repo = repo;
4065 s->refs = refs;
4066 s->commit_id = commit_id;
4067 memset(&s->blame, 0, sizeof(s->blame));
4069 SIMPLEQ_INIT(&s->colors);
4070 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4071 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4072 get_color_value("TOG_COLOR_COMMIT"));
4073 if (err)
4074 return err;
4077 view->show = show_blame_view;
4078 view->input = input_blame_view;
4079 view->close = close_blame_view;
4080 view->search_start = search_start_blame_view;
4081 view->search_next = search_next_blame_view;
4083 return run_blame(&s->blame, view, &s->blame_complete,
4084 &s->first_displayed_line, &s->last_displayed_line,
4085 &s->selected_line, &s->done, &s->eof, s->path,
4086 s->blamed_commit->id, s->repo);
4089 static const struct got_error *
4090 close_blame_view(struct tog_view *view)
4092 const struct got_error *err = NULL;
4093 struct tog_blame_view_state *s = &view->state.blame;
4095 if (s->blame.thread)
4096 err = stop_blame(&s->blame);
4098 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
4099 struct got_object_qid *blamed_commit;
4100 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
4101 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4102 got_object_qid_free(blamed_commit);
4105 free(s->path);
4106 free_colors(&s->colors);
4108 return err;
4111 static const struct got_error *
4112 search_start_blame_view(struct tog_view *view)
4114 struct tog_blame_view_state *s = &view->state.blame;
4116 s->matched_line = 0;
4117 return NULL;
4120 static const struct got_error *
4121 search_next_blame_view(struct tog_view *view)
4123 struct tog_blame_view_state *s = &view->state.blame;
4124 int lineno;
4126 if (!view->searching) {
4127 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4128 return NULL;
4131 if (s->matched_line) {
4132 if (view->searching == TOG_SEARCH_FORWARD)
4133 lineno = s->matched_line + 1;
4134 else
4135 lineno = s->matched_line - 1;
4136 } else {
4137 if (view->searching == TOG_SEARCH_FORWARD)
4138 lineno = 1;
4139 else
4140 lineno = s->blame.nlines;
4143 while (1) {
4144 char *line = NULL;
4145 off_t offset;
4146 size_t len;
4148 if (lineno <= 0 || lineno > s->blame.nlines) {
4149 if (s->matched_line == 0) {
4150 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4151 free(line);
4152 break;
4155 if (view->searching == TOG_SEARCH_FORWARD)
4156 lineno = 1;
4157 else
4158 lineno = s->blame.nlines;
4161 offset = s->blame.line_offsets[lineno - 1];
4162 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4163 free(line);
4164 return got_error_from_errno("fseeko");
4166 free(line);
4167 line = parse_next_line(s->blame.f, &len);
4168 if (line && match_line(line, &view->regex)) {
4169 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4170 s->matched_line = lineno;
4171 free(line);
4172 break;
4174 free(line);
4175 if (view->searching == TOG_SEARCH_FORWARD)
4176 lineno++;
4177 else
4178 lineno--;
4181 if (s->matched_line) {
4182 s->first_displayed_line = s->matched_line;
4183 s->selected_line = 1;
4186 return NULL;
4189 static const struct got_error *
4190 show_blame_view(struct tog_view *view)
4192 const struct got_error *err = NULL;
4193 struct tog_blame_view_state *s = &view->state.blame;
4194 int errcode;
4196 if (s->blame.thread == NULL) {
4197 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4198 &s->blame.thread_args);
4199 if (errcode)
4200 return got_error_set_errno(errcode, "pthread_create");
4202 halfdelay(1); /* fast refresh while annotating */
4205 if (s->blame_complete)
4206 halfdelay(10); /* disable fast refresh */
4208 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
4209 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
4210 s->selected_line, &s->first_displayed_line,
4211 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
4213 view_vborder(view);
4214 return err;
4217 static const struct got_error *
4218 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
4219 struct tog_view **focus_view, struct tog_view *view, int ch)
4221 const struct got_error *err = NULL, *thread_err = NULL;
4222 struct tog_view *diff_view;
4223 struct tog_blame_view_state *s = &view->state.blame;
4224 int begin_x = 0;
4226 switch (ch) {
4227 case 'q':
4228 s->done = 1;
4229 break;
4230 case 'k':
4231 case KEY_UP:
4232 if (s->selected_line > 1)
4233 s->selected_line--;
4234 else if (s->selected_line == 1 &&
4235 s->first_displayed_line > 1)
4236 s->first_displayed_line--;
4237 break;
4238 case KEY_PPAGE:
4239 case CTRL('b'):
4240 if (s->first_displayed_line == 1) {
4241 s->selected_line = 1;
4242 break;
4244 if (s->first_displayed_line > view->nlines - 2)
4245 s->first_displayed_line -=
4246 (view->nlines - 2);
4247 else
4248 s->first_displayed_line = 1;
4249 break;
4250 case 'j':
4251 case KEY_DOWN:
4252 if (s->selected_line < view->nlines - 2 &&
4253 s->first_displayed_line +
4254 s->selected_line <= s->blame.nlines)
4255 s->selected_line++;
4256 else if (s->last_displayed_line <
4257 s->blame.nlines)
4258 s->first_displayed_line++;
4259 break;
4260 case 'b':
4261 case 'p': {
4262 struct got_object_id *id = NULL;
4263 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4264 s->first_displayed_line, s->selected_line);
4265 if (id == NULL)
4266 break;
4267 if (ch == 'p') {
4268 struct got_commit_object *commit;
4269 struct got_object_qid *pid;
4270 struct got_object_id *blob_id = NULL;
4271 int obj_type;
4272 err = got_object_open_as_commit(&commit,
4273 s->repo, id);
4274 if (err)
4275 break;
4276 pid = SIMPLEQ_FIRST(
4277 got_object_commit_get_parent_ids(commit));
4278 if (pid == NULL) {
4279 got_object_commit_close(commit);
4280 break;
4282 /* Check if path history ends here. */
4283 err = got_object_id_by_path(&blob_id, s->repo,
4284 pid->id, s->path);
4285 if (err) {
4286 if (err->code == GOT_ERR_NO_TREE_ENTRY)
4287 err = NULL;
4288 got_object_commit_close(commit);
4289 break;
4291 err = got_object_get_type(&obj_type, s->repo,
4292 blob_id);
4293 free(blob_id);
4294 /* Can't blame non-blob type objects. */
4295 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4296 got_object_commit_close(commit);
4297 break;
4299 err = got_object_qid_alloc(&s->blamed_commit,
4300 pid->id);
4301 got_object_commit_close(commit);
4302 } else {
4303 if (got_object_id_cmp(id,
4304 s->blamed_commit->id) == 0)
4305 break;
4306 err = got_object_qid_alloc(&s->blamed_commit,
4307 id);
4309 if (err)
4310 break;
4311 s->done = 1;
4312 thread_err = stop_blame(&s->blame);
4313 s->done = 0;
4314 if (thread_err)
4315 break;
4316 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
4317 s->blamed_commit, entry);
4318 err = run_blame(&s->blame, view, &s->blame_complete,
4319 &s->first_displayed_line, &s->last_displayed_line,
4320 &s->selected_line, &s->done, &s->eof,
4321 s->path, s->blamed_commit->id, s->repo);
4322 if (err)
4323 break;
4324 break;
4326 case 'B': {
4327 struct got_object_qid *first;
4328 first = SIMPLEQ_FIRST(&s->blamed_commits);
4329 if (!got_object_id_cmp(first->id, s->commit_id))
4330 break;
4331 s->done = 1;
4332 thread_err = stop_blame(&s->blame);
4333 s->done = 0;
4334 if (thread_err)
4335 break;
4336 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4337 got_object_qid_free(s->blamed_commit);
4338 s->blamed_commit =
4339 SIMPLEQ_FIRST(&s->blamed_commits);
4340 err = run_blame(&s->blame, view, &s->blame_complete,
4341 &s->first_displayed_line, &s->last_displayed_line,
4342 &s->selected_line, &s->done, &s->eof, s->path,
4343 s->blamed_commit->id, s->repo);
4344 if (err)
4345 break;
4346 break;
4348 case KEY_ENTER:
4349 case '\r': {
4350 struct got_object_id *id = NULL;
4351 struct got_object_qid *pid;
4352 struct got_commit_object *commit = NULL;
4353 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4354 s->first_displayed_line, s->selected_line);
4355 if (id == NULL)
4356 break;
4357 err = got_object_open_as_commit(&commit, s->repo, id);
4358 if (err)
4359 break;
4360 pid = SIMPLEQ_FIRST(
4361 got_object_commit_get_parent_ids(commit));
4362 if (view_is_parent_view(view))
4363 begin_x = view_split_begin_x(view->begin_x);
4364 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4365 if (diff_view == NULL) {
4366 got_object_commit_close(commit);
4367 err = got_error_from_errno("view_open");
4368 break;
4370 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4371 id, NULL, s->refs, s->repo);
4372 got_object_commit_close(commit);
4373 if (err) {
4374 view_close(diff_view);
4375 break;
4377 if (view_is_parent_view(view)) {
4378 err = view_close_child(view);
4379 if (err)
4380 break;
4381 err = view_set_child(view, diff_view);
4382 if (err) {
4383 view_close(diff_view);
4384 break;
4386 *focus_view = diff_view;
4387 view->child_focussed = 1;
4388 } else
4389 *new_view = diff_view;
4390 if (err)
4391 break;
4392 break;
4394 case KEY_NPAGE:
4395 case CTRL('f'):
4396 case ' ':
4397 if (s->last_displayed_line >= s->blame.nlines &&
4398 s->selected_line >= MIN(s->blame.nlines,
4399 view->nlines - 2)) {
4400 break;
4402 if (s->last_displayed_line >= s->blame.nlines &&
4403 s->selected_line < view->nlines - 2) {
4404 s->selected_line = MIN(s->blame.nlines,
4405 view->nlines - 2);
4406 break;
4408 if (s->last_displayed_line + view->nlines - 2
4409 <= s->blame.nlines)
4410 s->first_displayed_line +=
4411 view->nlines - 2;
4412 else
4413 s->first_displayed_line =
4414 s->blame.nlines -
4415 (view->nlines - 3);
4416 break;
4417 case KEY_RESIZE:
4418 if (s->selected_line > view->nlines - 2) {
4419 s->selected_line = MIN(s->blame.nlines,
4420 view->nlines - 2);
4422 break;
4423 default:
4424 break;
4426 return thread_err ? thread_err : err;
4429 static const struct got_error *
4430 cmd_blame(int argc, char *argv[])
4432 const struct got_error *error;
4433 struct got_repository *repo = NULL;
4434 struct got_reflist_head refs;
4435 struct got_worktree *worktree = NULL;
4436 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4437 struct got_object_id *commit_id = NULL;
4438 char *commit_id_str = NULL;
4439 int ch;
4440 struct tog_view *view;
4442 SIMPLEQ_INIT(&refs);
4444 #ifndef PROFILE
4445 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4446 NULL) == -1)
4447 err(1, "pledge");
4448 #endif
4450 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4451 switch (ch) {
4452 case 'c':
4453 commit_id_str = optarg;
4454 break;
4455 case 'r':
4456 repo_path = realpath(optarg, NULL);
4457 if (repo_path == NULL)
4458 return got_error_from_errno2("realpath",
4459 optarg);
4460 break;
4461 default:
4462 usage_blame();
4463 /* NOTREACHED */
4467 argc -= optind;
4468 argv += optind;
4470 if (argc != 1)
4471 usage_blame();
4473 cwd = getcwd(NULL, 0);
4474 if (cwd == NULL)
4475 return got_error_from_errno("getcwd");
4477 error = got_worktree_open(&worktree, cwd);
4478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4479 goto done;
4481 if (repo_path == NULL) {
4482 if (worktree)
4483 repo_path =
4484 strdup(got_worktree_get_repo_path(worktree));
4485 else
4486 repo_path = strdup(cwd);
4488 if (repo_path == NULL) {
4489 error = got_error_from_errno("strdup");
4490 goto done;
4493 error = got_repo_open(&repo, repo_path, NULL);
4494 if (error != NULL)
4495 goto done;
4497 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
4498 worktree);
4499 if (error)
4500 goto done;
4502 init_curses();
4504 error = apply_unveil(got_repo_get_path(repo), NULL);
4505 if (error)
4506 goto done;
4508 if (commit_id_str == NULL) {
4509 struct got_reference *head_ref;
4510 error = got_ref_open(&head_ref, repo, worktree ?
4511 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4512 if (error != NULL)
4513 goto done;
4514 error = got_ref_resolve(&commit_id, repo, head_ref);
4515 got_ref_close(head_ref);
4516 } else {
4517 error = got_repo_match_object_id(&commit_id, NULL,
4518 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4520 if (error != NULL)
4521 goto done;
4523 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4524 if (error)
4525 goto done;
4527 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4528 if (view == NULL) {
4529 error = got_error_from_errno("view_open");
4530 goto done;
4532 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4533 if (error)
4534 goto done;
4535 if (worktree) {
4536 /* Release work tree lock. */
4537 got_worktree_close(worktree);
4538 worktree = NULL;
4540 error = view_loop(view);
4541 done:
4542 free(repo_path);
4543 free(in_repo_path);
4544 free(cwd);
4545 free(commit_id);
4546 if (worktree)
4547 got_worktree_close(worktree);
4548 if (repo)
4549 got_repo_close(repo);
4550 got_ref_list_free(&refs);
4551 return error;
4554 static const struct got_error *
4555 draw_tree_entries(struct tog_view *view,
4556 struct got_tree_entry **first_displayed_entry,
4557 struct got_tree_entry **last_displayed_entry,
4558 struct got_tree_entry **selected_entry, int *ndisplayed,
4559 const char *label, int show_ids, const char *parent_path,
4560 struct got_tree_object *tree, int selected, int limit,
4561 int isroot, struct tog_colors *colors)
4563 const struct got_error *err = NULL;
4564 struct got_tree_entry *te;
4565 wchar_t *wline;
4566 struct tog_color *tc;
4567 int width, n, i, nentries;
4569 *ndisplayed = 0;
4571 werase(view->window);
4573 if (limit == 0)
4574 return NULL;
4576 err = format_line(&wline, &width, label, view->ncols, 0);
4577 if (err)
4578 return err;
4579 if (view_needs_focus_indication(view))
4580 wstandout(view->window);
4581 tc = get_color(colors, TOG_COLOR_COMMIT);
4582 if (tc)
4583 wattr_on(view->window,
4584 COLOR_PAIR(tc->colorpair), NULL);
4585 waddwstr(view->window, wline);
4586 if (tc)
4587 wattr_off(view->window,
4588 COLOR_PAIR(tc->colorpair), NULL);
4589 if (view_needs_focus_indication(view))
4590 wstandend(view->window);
4591 free(wline);
4592 wline = NULL;
4593 if (width < view->ncols - 1)
4594 waddch(view->window, '\n');
4595 if (--limit <= 0)
4596 return NULL;
4597 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4598 if (err)
4599 return err;
4600 waddwstr(view->window, wline);
4601 free(wline);
4602 wline = NULL;
4603 if (width < view->ncols - 1)
4604 waddch(view->window, '\n');
4605 if (--limit <= 0)
4606 return NULL;
4607 waddch(view->window, '\n');
4608 if (--limit <= 0)
4609 return NULL;
4611 if (*first_displayed_entry == NULL) {
4612 te = got_object_tree_get_first_entry(tree);
4613 if (selected == 0) {
4614 if (view->focussed)
4615 wstandout(view->window);
4616 *selected_entry = NULL;
4618 waddstr(view->window, " ..\n"); /* parent directory */
4619 if (selected == 0 && view->focussed)
4620 wstandend(view->window);
4621 (*ndisplayed)++;
4622 if (--limit <= 0)
4623 return NULL;
4624 n = 1;
4625 } else {
4626 n = 0;
4627 te = *first_displayed_entry;
4630 nentries = got_object_tree_get_nentries(tree);
4631 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
4632 char *line = NULL, *id_str = NULL;
4633 const char *modestr = "";
4634 mode_t mode;
4636 te = got_object_tree_get_entry(tree, i);
4637 mode = got_tree_entry_get_mode(te);
4639 if (show_ids) {
4640 err = got_object_id_str(&id_str,
4641 got_tree_entry_get_id(te));
4642 if (err)
4643 return got_error_from_errno(
4644 "got_object_id_str");
4646 if (got_object_tree_entry_is_submodule(te))
4647 modestr = "$";
4648 else if (S_ISLNK(mode))
4649 modestr = "@";
4650 else if (S_ISDIR(mode))
4651 modestr = "/";
4652 else if (mode & S_IXUSR)
4653 modestr = "*";
4654 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4655 got_tree_entry_get_name(te), modestr) == -1) {
4656 free(id_str);
4657 return got_error_from_errno("asprintf");
4659 free(id_str);
4660 err = format_line(&wline, &width, line, view->ncols, 0);
4661 if (err) {
4662 free(line);
4663 break;
4665 if (n == selected) {
4666 if (view->focussed)
4667 wstandout(view->window);
4668 *selected_entry = te;
4670 tc = match_color(colors, line);
4671 if (tc)
4672 wattr_on(view->window,
4673 COLOR_PAIR(tc->colorpair), NULL);
4674 waddwstr(view->window, wline);
4675 if (tc)
4676 wattr_off(view->window,
4677 COLOR_PAIR(tc->colorpair), NULL);
4678 if (width < view->ncols - 1)
4679 waddch(view->window, '\n');
4680 if (n == selected && view->focussed)
4681 wstandend(view->window);
4682 free(line);
4683 free(wline);
4684 wline = NULL;
4685 n++;
4686 (*ndisplayed)++;
4687 *last_displayed_entry = te;
4688 if (--limit <= 0)
4689 break;
4692 return err;
4695 static void
4696 tree_scroll_up(struct tog_view *view,
4697 struct got_tree_entry **first_displayed_entry, int maxscroll,
4698 struct got_tree_object *tree, int isroot)
4700 struct got_tree_entry *te;
4701 int i;
4703 if (*first_displayed_entry == NULL)
4704 return;
4706 te = got_object_tree_get_entry(tree, 0);
4707 if (*first_displayed_entry == te) {
4708 if (!isroot)
4709 *first_displayed_entry = NULL;
4710 return;
4713 i = 0;
4714 while (*first_displayed_entry && i < maxscroll) {
4715 *first_displayed_entry = got_tree_entry_get_prev(tree,
4716 *first_displayed_entry);
4717 i++;
4719 if (!isroot && te == got_object_tree_get_first_entry(tree) && i < maxscroll)
4720 *first_displayed_entry = NULL;
4723 static int
4724 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4725 struct got_tree_entry *last_displayed_entry,
4726 struct got_tree_object *tree)
4728 struct got_tree_entry *next, *last;
4729 int n = 0;
4731 if (*first_displayed_entry)
4732 next = got_tree_entry_get_next(tree, *first_displayed_entry);
4733 else
4734 next = got_object_tree_get_first_entry(tree);
4736 last = last_displayed_entry;
4737 while (next && last && n++ < maxscroll) {
4738 last = got_tree_entry_get_next(tree, last);
4739 if (last) {
4740 *first_displayed_entry = next;
4741 next = got_tree_entry_get_next(tree, next);
4744 return n;
4747 static const struct got_error *
4748 tree_entry_path(char **path, struct tog_parent_trees *parents,
4749 struct got_tree_entry *te)
4751 const struct got_error *err = NULL;
4752 struct tog_parent_tree *pt;
4753 size_t len = 2; /* for leading slash and NUL */
4755 TAILQ_FOREACH(pt, parents, entry)
4756 len += strlen(got_tree_entry_get_name(pt->selected_entry))
4757 + 1 /* slash */;
4758 if (te)
4759 len += strlen(got_tree_entry_get_name(te));
4761 *path = calloc(1, len);
4762 if (path == NULL)
4763 return got_error_from_errno("calloc");
4765 (*path)[0] = '/';
4766 pt = TAILQ_LAST(parents, tog_parent_trees);
4767 while (pt) {
4768 const char *name = got_tree_entry_get_name(pt->selected_entry);
4769 if (strlcat(*path, name, len) >= len) {
4770 err = got_error(GOT_ERR_NO_SPACE);
4771 goto done;
4773 if (strlcat(*path, "/", len) >= len) {
4774 err = got_error(GOT_ERR_NO_SPACE);
4775 goto done;
4777 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4779 if (te) {
4780 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
4781 err = got_error(GOT_ERR_NO_SPACE);
4782 goto done;
4785 done:
4786 if (err) {
4787 free(*path);
4788 *path = NULL;
4790 return err;
4793 static const struct got_error *
4794 blame_tree_entry(struct tog_view **new_view, int begin_x,
4795 struct got_tree_entry *te, struct tog_parent_trees *parents,
4796 struct got_object_id *commit_id, struct got_reflist_head *refs,
4797 struct got_repository *repo)
4799 const struct got_error *err = NULL;
4800 char *path;
4801 struct tog_view *blame_view;
4803 *new_view = NULL;
4805 err = tree_entry_path(&path, parents, te);
4806 if (err)
4807 return err;
4809 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4810 if (blame_view == NULL) {
4811 err = got_error_from_errno("view_open");
4812 goto done;
4815 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4816 if (err) {
4817 if (err->code == GOT_ERR_CANCELLED)
4818 err = NULL;
4819 view_close(blame_view);
4820 } else
4821 *new_view = blame_view;
4822 done:
4823 free(path);
4824 return err;
4827 static const struct got_error *
4828 log_tree_entry(struct tog_view **new_view, int begin_x,
4829 struct got_tree_entry *te, struct tog_parent_trees *parents,
4830 struct got_object_id *commit_id, struct got_reflist_head *refs,
4831 struct got_repository *repo)
4833 struct tog_view *log_view;
4834 const struct got_error *err = NULL;
4835 char *path;
4837 *new_view = NULL;
4839 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4840 if (log_view == NULL)
4841 return got_error_from_errno("view_open");
4843 err = tree_entry_path(&path, parents, te);
4844 if (err)
4845 return err;
4847 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4848 if (err)
4849 view_close(log_view);
4850 else
4851 *new_view = log_view;
4852 free(path);
4853 return err;
4856 static const struct got_error *
4857 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4858 struct got_object_id *commit_id, struct got_reflist_head *refs,
4859 struct got_repository *repo)
4861 const struct got_error *err = NULL;
4862 char *commit_id_str = NULL;
4863 struct tog_tree_view_state *s = &view->state.tree;
4865 TAILQ_INIT(&s->parents);
4867 err = got_object_id_str(&commit_id_str, commit_id);
4868 if (err != NULL)
4869 goto done;
4871 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4872 err = got_error_from_errno("asprintf");
4873 goto done;
4876 s->root = s->tree = root;
4877 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
4878 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
4879 s->commit_id = got_object_id_dup(commit_id);
4880 if (s->commit_id == NULL) {
4881 err = got_error_from_errno("got_object_id_dup");
4882 goto done;
4884 s->refs = refs;
4885 s->repo = repo;
4887 SIMPLEQ_INIT(&s->colors);
4889 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4890 err = add_color(&s->colors, "\\$$",
4891 TOG_COLOR_TREE_SUBMODULE,
4892 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4893 if (err)
4894 goto done;
4895 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4896 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4897 if (err) {
4898 free_colors(&s->colors);
4899 goto done;
4901 err = add_color(&s->colors, "/$",
4902 TOG_COLOR_TREE_DIRECTORY,
4903 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4904 if (err) {
4905 free_colors(&s->colors);
4906 goto done;
4909 err = add_color(&s->colors, "\\*$",
4910 TOG_COLOR_TREE_EXECUTABLE,
4911 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4912 if (err) {
4913 free_colors(&s->colors);
4914 goto done;
4917 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4918 get_color_value("TOG_COLOR_COMMIT"));
4919 if (err) {
4920 free_colors(&s->colors);
4921 goto done;
4925 view->show = show_tree_view;
4926 view->input = input_tree_view;
4927 view->close = close_tree_view;
4928 view->search_start = search_start_tree_view;
4929 view->search_next = search_next_tree_view;
4930 done:
4931 free(commit_id_str);
4932 if (err) {
4933 free(s->tree_label);
4934 s->tree_label = NULL;
4936 return err;
4939 static const struct got_error *
4940 close_tree_view(struct tog_view *view)
4942 struct tog_tree_view_state *s = &view->state.tree;
4944 free_colors(&s->colors);
4945 free(s->tree_label);
4946 s->tree_label = NULL;
4947 free(s->commit_id);
4948 s->commit_id = NULL;
4949 while (!TAILQ_EMPTY(&s->parents)) {
4950 struct tog_parent_tree *parent;
4951 parent = TAILQ_FIRST(&s->parents);
4952 TAILQ_REMOVE(&s->parents, parent, entry);
4953 free(parent);
4956 if (s->tree != s->root)
4957 got_object_tree_close(s->tree);
4958 got_object_tree_close(s->root);
4960 return NULL;
4963 static const struct got_error *
4964 search_start_tree_view(struct tog_view *view)
4966 struct tog_tree_view_state *s = &view->state.tree;
4968 s->matched_entry = NULL;
4969 return NULL;
4972 static int
4973 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4975 regmatch_t regmatch;
4977 return regexec(regex, got_tree_entry_get_name(te), 1, &regmatch,
4978 0) == 0;
4981 static const struct got_error *
4982 search_next_tree_view(struct tog_view *view)
4984 struct tog_tree_view_state *s = &view->state.tree;
4985 struct got_tree_entry *te = NULL;
4987 if (!view->searching) {
4988 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4989 return NULL;
4992 if (s->matched_entry) {
4993 if (view->searching == TOG_SEARCH_FORWARD) {
4994 if (s->selected_entry)
4995 te = got_tree_entry_get_next(s->tree,
4996 s->selected_entry);
4997 else
4998 te = got_object_tree_get_first_entry(s->tree);
4999 } else {
5000 if (s->selected_entry == NULL)
5001 te = got_object_tree_get_last_entry(s->tree);
5002 else
5003 te = got_tree_entry_get_prev(s->tree,
5004 s->selected_entry);
5006 } else {
5007 if (view->searching == TOG_SEARCH_FORWARD)
5008 te = got_object_tree_get_first_entry(s->tree);
5009 else
5010 te = got_object_tree_get_last_entry(s->tree);
5013 while (1) {
5014 if (te == NULL) {
5015 if (s->matched_entry == NULL) {
5016 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5017 return NULL;
5019 if (view->searching == TOG_SEARCH_FORWARD)
5020 te = got_object_tree_get_first_entry(s->tree);
5021 else
5022 te = got_object_tree_get_last_entry(s->tree);
5025 if (match_tree_entry(te, &view->regex)) {
5026 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5027 s->matched_entry = te;
5028 break;
5031 if (view->searching == TOG_SEARCH_FORWARD)
5032 te = got_tree_entry_get_next(s->tree, te);
5033 else
5034 te = got_tree_entry_get_prev(s->tree, te);
5037 if (s->matched_entry) {
5038 s->first_displayed_entry = s->matched_entry;
5039 s->selected = 0;
5042 return NULL;
5045 static const struct got_error *
5046 show_tree_view(struct tog_view *view)
5048 const struct got_error *err = NULL;
5049 struct tog_tree_view_state *s = &view->state.tree;
5050 char *parent_path;
5052 err = tree_entry_path(&parent_path, &s->parents, NULL);
5053 if (err)
5054 return err;
5056 err = draw_tree_entries(view, &s->first_displayed_entry,
5057 &s->last_displayed_entry, &s->selected_entry,
5058 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
5059 s->tree, s->selected, view->nlines, s->tree == s->root,
5060 &s->colors);
5061 free(parent_path);
5063 view_vborder(view);
5064 return err;
5067 static const struct got_error *
5068 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
5069 struct tog_view **focus_view, struct tog_view *view, int ch)
5071 const struct got_error *err = NULL;
5072 struct tog_tree_view_state *s = &view->state.tree;
5073 struct tog_view *log_view;
5074 int begin_x = 0, nscrolled;
5076 switch (ch) {
5077 case 'i':
5078 s->show_ids = !s->show_ids;
5079 break;
5080 case 'l':
5081 if (!s->selected_entry)
5082 break;
5083 if (view_is_parent_view(view))
5084 begin_x = view_split_begin_x(view->begin_x);
5085 err = log_tree_entry(&log_view, begin_x,
5086 s->selected_entry, &s->parents,
5087 s->commit_id, s->refs, s->repo);
5088 if (view_is_parent_view(view)) {
5089 err = view_close_child(view);
5090 if (err)
5091 return err;
5092 err = view_set_child(view, log_view);
5093 if (err) {
5094 view_close(log_view);
5095 break;
5097 *focus_view = log_view;
5098 view->child_focussed = 1;
5099 } else
5100 *new_view = log_view;
5101 break;
5102 case 'k':
5103 case KEY_UP:
5104 if (s->selected > 0) {
5105 s->selected--;
5106 if (s->selected == 0)
5107 break;
5109 if (s->selected > 0)
5110 break;
5111 tree_scroll_up(view, &s->first_displayed_entry, 1,
5112 s->tree, s->tree == s->root);
5113 break;
5114 case KEY_PPAGE:
5115 case CTRL('b'):
5116 tree_scroll_up(view, &s->first_displayed_entry,
5117 MAX(0, view->nlines - 4 - s->selected), s->tree,
5118 s->tree == s->root);
5119 s->selected = 0;
5120 if (got_object_tree_get_first_entry(s->tree) ==
5121 s->first_displayed_entry && s->tree != s->root)
5122 s->first_displayed_entry = NULL;
5123 break;
5124 case 'j':
5125 case KEY_DOWN:
5126 if (s->selected < s->ndisplayed - 1) {
5127 s->selected++;
5128 break;
5130 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5131 == NULL)
5132 /* can't scroll any further */
5133 break;
5134 tree_scroll_down(&s->first_displayed_entry, 1,
5135 s->last_displayed_entry, s->tree);
5136 break;
5137 case KEY_NPAGE:
5138 case CTRL('f'):
5139 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5140 == NULL) {
5141 /* can't scroll any further; move cursor down */
5142 if (s->selected < s->ndisplayed - 1)
5143 s->selected = s->ndisplayed - 1;
5144 break;
5146 nscrolled = tree_scroll_down(&s->first_displayed_entry,
5147 view->nlines, s->last_displayed_entry, s->tree);
5148 if (nscrolled < view->nlines) {
5149 int ndisplayed = 0;
5150 struct got_tree_entry *te;
5151 te = s->first_displayed_entry;
5152 do {
5153 ndisplayed++;
5154 te = got_tree_entry_get_next(s->tree, te);
5155 } while (te);
5156 s->selected = ndisplayed - 1;
5158 break;
5159 case KEY_ENTER:
5160 case '\r':
5161 case KEY_BACKSPACE:
5162 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5163 struct tog_parent_tree *parent;
5164 /* user selected '..' */
5165 if (s->tree == s->root)
5166 break;
5167 parent = TAILQ_FIRST(&s->parents);
5168 TAILQ_REMOVE(&s->parents, parent,
5169 entry);
5170 got_object_tree_close(s->tree);
5171 s->tree = parent->tree;
5172 s->first_displayed_entry =
5173 parent->first_displayed_entry;
5174 s->selected_entry =
5175 parent->selected_entry;
5176 s->selected = parent->selected;
5177 free(parent);
5178 } else if (S_ISDIR(got_tree_entry_get_mode(
5179 s->selected_entry))) {
5180 struct got_tree_object *subtree;
5181 err = got_object_open_as_tree(&subtree, s->repo,
5182 got_tree_entry_get_id(s->selected_entry));
5183 if (err)
5184 break;
5185 err = tree_view_visit_subtree(subtree, s);
5186 if (err) {
5187 got_object_tree_close(subtree);
5188 break;
5190 } else if (S_ISREG(got_tree_entry_get_mode(
5191 s->selected_entry))) {
5192 struct tog_view *blame_view;
5193 int begin_x = view_is_parent_view(view) ?
5194 view_split_begin_x(view->begin_x) : 0;
5196 err = blame_tree_entry(&blame_view, begin_x,
5197 s->selected_entry, &s->parents,
5198 s->commit_id, s->refs, s->repo);
5199 if (err)
5200 break;
5201 if (view_is_parent_view(view)) {
5202 err = view_close_child(view);
5203 if (err)
5204 return err;
5205 err = view_set_child(view, blame_view);
5206 if (err) {
5207 view_close(blame_view);
5208 break;
5210 *focus_view = blame_view;
5211 view->child_focussed = 1;
5212 } else
5213 *new_view = blame_view;
5215 break;
5216 case KEY_RESIZE:
5217 if (s->selected > view->nlines)
5218 s->selected = s->ndisplayed - 1;
5219 break;
5220 default:
5221 break;
5224 return err;
5227 __dead static void
5228 usage_tree(void)
5230 endwin();
5231 fprintf(stderr, "usage: %s tree [-c commit] [-r repository-path] [path]\n",
5232 getprogname());
5233 exit(1);
5236 static const struct got_error *
5237 cmd_tree(int argc, char *argv[])
5239 const struct got_error *error;
5240 struct got_repository *repo = NULL;
5241 struct got_worktree *worktree = NULL;
5242 struct got_reflist_head refs;
5243 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5244 struct got_object_id *commit_id = NULL;
5245 char *commit_id_arg = NULL;
5246 struct got_commit_object *commit = NULL;
5247 struct got_tree_object *tree = NULL;
5248 int ch;
5249 struct tog_view *view;
5251 SIMPLEQ_INIT(&refs);
5253 #ifndef PROFILE
5254 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
5255 NULL) == -1)
5256 err(1, "pledge");
5257 #endif
5259 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5260 switch (ch) {
5261 case 'c':
5262 commit_id_arg = optarg;
5263 break;
5264 case 'r':
5265 repo_path = realpath(optarg, NULL);
5266 if (repo_path == NULL)
5267 return got_error_from_errno2("realpath",
5268 optarg);
5269 break;
5270 default:
5271 usage_tree();
5272 /* NOTREACHED */
5276 argc -= optind;
5277 argv += optind;
5279 if (argc > 1)
5280 usage_tree();
5282 cwd = getcwd(NULL, 0);
5283 if (cwd == NULL)
5284 return got_error_from_errno("getcwd");
5286 error = got_worktree_open(&worktree, cwd);
5287 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5288 goto done;
5290 if (repo_path == NULL) {
5291 if (worktree)
5292 repo_path =
5293 strdup(got_worktree_get_repo_path(worktree));
5294 else
5295 repo_path = strdup(cwd);
5297 if (repo_path == NULL) {
5298 error = got_error_from_errno("strdup");
5299 goto done;
5302 error = got_repo_open(&repo, repo_path, NULL);
5303 if (error != NULL)
5304 goto done;
5306 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5307 repo, worktree);
5308 if (error)
5309 goto done;
5311 init_curses();
5313 error = apply_unveil(got_repo_get_path(repo), NULL);
5314 if (error)
5315 goto done;
5317 error = got_repo_match_object_id(&commit_id, NULL,
5318 commit_id_arg ? commit_id_arg : GOT_REF_HEAD,
5319 GOT_OBJ_TYPE_COMMIT, 1, repo);
5320 if (error)
5321 goto done;
5323 error = got_object_open_as_commit(&commit, repo, commit_id);
5324 if (error)
5325 goto done;
5327 error = got_object_open_as_tree(&tree, repo,
5328 got_object_commit_get_tree_id(commit));
5329 if (error)
5330 goto done;
5332 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5333 if (error)
5334 goto done;
5336 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5337 if (view == NULL) {
5338 error = got_error_from_errno("view_open");
5339 goto done;
5341 error = open_tree_view(view, tree, commit_id, &refs, repo);
5342 if (error)
5343 goto done;
5344 if (!got_path_is_root_dir(in_repo_path)) {
5345 error = tree_view_walk_path(&view->state.tree, commit_id,
5346 in_repo_path, repo);
5347 if (error)
5348 goto done;
5351 if (worktree) {
5352 /* Release work tree lock. */
5353 got_worktree_close(worktree);
5354 worktree = NULL;
5356 error = view_loop(view);
5357 done:
5358 free(repo_path);
5359 free(cwd);
5360 free(commit_id);
5361 if (commit)
5362 got_object_commit_close(commit);
5363 if (tree)
5364 got_object_tree_close(tree);
5365 if (repo)
5366 got_repo_close(repo);
5367 got_ref_list_free(&refs);
5368 return error;
5371 static void
5372 list_commands(void)
5374 int i;
5376 fprintf(stderr, "commands:");
5377 for (i = 0; i < nitems(tog_commands); i++) {
5378 struct tog_cmd *cmd = &tog_commands[i];
5379 fprintf(stderr, " %s", cmd->name);
5381 fputc('\n', stderr);
5384 __dead static void
5385 usage(int hflag)
5387 fprintf(stderr, "usage: %s [-h] [-V | --version] [command] "
5388 "[arg ...]\n", getprogname());
5389 if (hflag) {
5390 fprintf(stderr, "lazy usage: %s path\n", getprogname());
5391 list_commands();
5393 exit(1);
5396 static char **
5397 make_argv(int argc, ...)
5399 va_list ap;
5400 char **argv;
5401 int i;
5403 va_start(ap, argc);
5405 argv = calloc(argc, sizeof(char *));
5406 if (argv == NULL)
5407 err(1, "calloc");
5408 for (i = 0; i < argc; i++) {
5409 argv[i] = strdup(va_arg(ap, char *));
5410 if (argv[i] == NULL)
5411 err(1, "strdup");
5414 va_end(ap);
5415 return argv;
5419 * Try to convert 'tog path' into a 'tog log path' command.
5420 * The user could simply have mistyped the command rather than knowingly
5421 * provided a path. So check whether argv[0] can in fact be resolved
5422 * to a path in the HEAD commit and print a special error if not.
5423 * This hack is for mpi@ <3
5425 static const struct got_error *
5426 tog_log_with_path(int argc, char *argv[])
5428 const struct got_error *error = NULL;
5429 struct tog_cmd *cmd = NULL;
5430 struct got_repository *repo = NULL;
5431 struct got_worktree *worktree = NULL;
5432 struct got_object_id *commit_id = NULL, *id = NULL;
5433 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5434 char *commit_id_str = NULL, **cmd_argv = NULL;
5436 cwd = getcwd(NULL, 0);
5437 if (cwd == NULL)
5438 return got_error_from_errno("getcwd");
5440 error = got_worktree_open(&worktree, cwd);
5441 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5442 goto done;
5444 if (worktree)
5445 repo_path = strdup(got_worktree_get_repo_path(worktree));
5446 else
5447 repo_path = strdup(cwd);
5448 if (repo_path == NULL) {
5449 error = got_error_from_errno("strdup");
5450 goto done;
5453 error = got_repo_open(&repo, repo_path, NULL);
5454 if (error != NULL)
5455 goto done;
5457 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
5458 repo, worktree);
5459 if (error)
5460 goto done;
5462 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
5463 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
5464 GOT_OBJ_TYPE_COMMIT, 1, repo);
5465 if (error)
5466 goto done;
5468 if (worktree) {
5469 got_worktree_close(worktree);
5470 worktree = NULL;
5473 error = got_object_id_by_path(&id, repo, commit_id, in_repo_path);
5474 if (error) {
5475 if (error->code != GOT_ERR_NO_TREE_ENTRY)
5476 goto done;
5477 fprintf(stderr, "%s: '%s' is no known command or path\n",
5478 getprogname(), argv[0]);
5479 usage(1);
5480 /* not reached */
5483 got_repo_close(repo);
5484 repo = NULL;
5486 error = got_object_id_str(&commit_id_str, commit_id);
5487 if (error)
5488 goto done;
5490 cmd = &tog_commands[0]; /* log */
5491 argc = 4;
5492 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
5493 error = cmd->cmd_main(argc, cmd_argv);
5494 done:
5495 if (repo)
5496 got_repo_close(repo);
5497 if (worktree)
5498 got_worktree_close(worktree);
5499 free(id);
5500 free(commit_id_str);
5501 free(commit_id);
5502 free(cwd);
5503 free(repo_path);
5504 free(in_repo_path);
5505 if (cmd_argv) {
5506 int i;
5507 for (i = 0; i < argc; i++)
5508 free(cmd_argv[i]);
5509 free(cmd_argv);
5511 return error;
5514 int
5515 main(int argc, char *argv[])
5517 const struct got_error *error = NULL;
5518 struct tog_cmd *cmd = NULL;
5519 int ch, hflag = 0, Vflag = 0;
5520 char **cmd_argv = NULL;
5521 static struct option longopts[] = {
5522 { "version", no_argument, NULL, 'V' },
5523 { NULL, 0, NULL, 0}
5526 setlocale(LC_CTYPE, "");
5528 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
5529 switch (ch) {
5530 case 'h':
5531 hflag = 1;
5532 break;
5533 case 'V':
5534 Vflag = 1;
5535 break;
5536 default:
5537 usage(hflag);
5538 /* NOTREACHED */
5542 argc -= optind;
5543 argv += optind;
5544 optind = 0;
5545 optreset = 1;
5547 if (Vflag) {
5548 got_version_print_str();
5549 return 1;
5552 if (argc == 0) {
5553 if (hflag)
5554 usage(hflag);
5555 /* Build an argument vector which runs a default command. */
5556 cmd = &tog_commands[0];
5557 argc = 1;
5558 cmd_argv = make_argv(argc, cmd->name);
5559 } else {
5560 int i;
5562 /* Did the user specify a command? */
5563 for (i = 0; i < nitems(tog_commands); i++) {
5564 if (strncmp(tog_commands[i].name, argv[0],
5565 strlen(argv[0])) == 0) {
5566 cmd = &tog_commands[i];
5567 break;
5572 if (cmd == NULL) {
5573 if (argc != 1)
5574 usage(0);
5575 /* No command specified; try log with a path */
5576 error = tog_log_with_path(argc, argv);
5577 } else {
5578 if (hflag)
5579 cmd->cmd_usage();
5580 else
5581 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5584 endwin();
5585 if (cmd_argv) {
5586 int i;
5587 for (i = 0; i < argc; i++)
5588 free(cmd_argv[i]);
5589 free(cmd_argv);
5592 if (error && error->code != GOT_ERR_CANCELLED)
5593 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5594 return 0;