Blob


1 /*
2 * Copyright (c) 2018, 2019 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 <stdlib.h>
28 #include <stdio.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <util.h>
34 #include <limits.h>
35 #include <wchar.h>
36 #include <time.h>
37 #include <pthread.h>
38 #include <libgen.h>
39 #include <regex.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
46 #include "got_diff.h"
47 #include "got_opentemp.h"
48 #include "got_utf8.h"
49 #include "got_cancel.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_path.h"
54 #include "got_worktree.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #define CTRL(x) ((x) & 0x1f)
66 #ifndef nitems
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 #endif
70 struct tog_cmd {
71 const char *name;
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 };
76 __dead static void usage(int);
77 __dead static void usage_log(void);
78 __dead static void usage_diff(void);
79 __dead static void usage_blame(void);
80 __dead static void usage_tree(void);
82 static const struct got_error* cmd_log(int, char *[]);
83 static const struct got_error* cmd_diff(int, char *[]);
84 static const struct got_error* cmd_blame(int, char *[]);
85 static const struct got_error* cmd_tree(int, char *[]);
87 static struct tog_cmd tog_commands[] = {
88 { "log", cmd_log, usage_log },
89 { "diff", cmd_diff, usage_diff },
90 { "blame", cmd_blame, usage_blame },
91 { "tree", cmd_tree, usage_tree },
92 };
94 enum tog_view_type {
95 TOG_VIEW_DIFF,
96 TOG_VIEW_LOG,
97 TOG_VIEW_BLAME,
98 TOG_VIEW_TREE
99 };
101 #define TOG_EOF_STRING "(END)"
103 struct commit_queue_entry {
104 TAILQ_ENTRY(commit_queue_entry) entry;
105 struct got_object_id *id;
106 struct got_commit_object *commit;
107 int idx;
108 };
109 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
110 struct commit_queue {
111 int ncommits;
112 struct commit_queue_head head;
113 };
115 struct tog_color {
116 SIMPLEQ_ENTRY(tog_color) entry;
117 regex_t regex;
118 short colorpair;
119 };
120 SIMPLEQ_HEAD(tog_colors, tog_color);
122 static const struct got_error *
123 add_color(struct tog_colors *colors, const char *pattern,
124 int idx, short color)
126 const struct got_error *err = NULL;
127 struct tog_color *tc;
128 int regerr = 0;
130 if (idx < 1 || idx > COLOR_PAIRS - 1)
131 return NULL;
133 init_pair(idx, color, -1);
135 tc = calloc(1, sizeof(*tc));
136 if (tc == NULL)
137 return got_error_from_errno("calloc");
138 regerr = regcomp(&tc->regex, pattern,
139 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
140 if (regerr) {
141 static char regerr_msg[512];
142 static char err_msg[512];
143 regerror(regerr, &tc->regex, regerr_msg,
144 sizeof(regerr_msg));
145 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
146 regerr_msg);
147 err = got_error_msg(GOT_ERR_REGEX, err_msg);
148 free(tc);
149 return err;
151 tc->colorpair = idx;
152 SIMPLEQ_INSERT_HEAD(colors, tc, entry);
153 return NULL;
156 static void
157 free_colors(struct tog_colors *colors)
159 struct tog_color *tc;
161 while (!SIMPLEQ_EMPTY(colors)) {
162 tc = SIMPLEQ_FIRST(colors);
163 SIMPLEQ_REMOVE_HEAD(colors, entry);
164 regfree(&tc->regex);
165 free(tc);
169 struct tog_color *
170 get_color(struct tog_colors *colors, int colorpair)
172 struct tog_color *tc = NULL;
174 SIMPLEQ_FOREACH(tc, colors, entry) {
175 if (tc->colorpair == colorpair)
176 return tc;
179 return NULL;
182 static int
183 default_color_value(const char *envvar)
185 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
186 return COLOR_MAGENTA;
187 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
188 return COLOR_CYAN;
189 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
190 return COLOR_YELLOW;
191 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
192 return COLOR_GREEN;
193 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
194 return COLOR_MAGENTA;
195 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
196 return COLOR_CYAN;
197 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
198 return COLOR_BLUE;
199 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
200 return COLOR_GREEN;
201 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
202 return COLOR_GREEN;
203 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
204 return COLOR_CYAN;
205 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
206 return COLOR_YELLOW;
208 return -1;
211 static int
212 get_color_value(const char *envvar)
214 const char *val = getenv(envvar);
216 if (val == NULL)
217 return default_color_value(envvar);
219 if (strcasecmp(val, "black") == 0)
220 return COLOR_BLACK;
221 if (strcasecmp(val, "red") == 0)
222 return COLOR_RED;
223 if (strcasecmp(val, "green") == 0)
224 return COLOR_GREEN;
225 if (strcasecmp(val, "yellow") == 0)
226 return COLOR_YELLOW;
227 if (strcasecmp(val, "blue") == 0)
228 return COLOR_BLUE;
229 if (strcasecmp(val, "magenta") == 0)
230 return COLOR_MAGENTA;
231 if (strcasecmp(val, "cyan") == 0)
232 return COLOR_CYAN;
233 if (strcasecmp(val, "white") == 0)
234 return COLOR_WHITE;
235 if (strcasecmp(val, "default") == 0)
236 return -1;
238 return default_color_value(envvar);
242 struct tog_diff_view_state {
243 struct got_object_id *id1, *id2;
244 FILE *f;
245 int first_displayed_line;
246 int last_displayed_line;
247 int eof;
248 int diff_context;
249 struct got_repository *repo;
250 struct got_reflist_head *refs;
251 struct tog_colors colors;
253 /* passed from log view; may be NULL */
254 struct tog_view *log_view;
255 };
257 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
259 struct tog_log_thread_args {
260 pthread_cond_t need_commits;
261 int commits_needed;
262 struct got_commit_graph *graph;
263 struct commit_queue *commits;
264 const char *in_repo_path;
265 struct got_object_id *start_id;
266 struct got_repository *repo;
267 int log_complete;
268 sig_atomic_t *quit;
269 struct commit_queue_entry **first_displayed_entry;
270 struct commit_queue_entry **selected_entry;
271 int *searching;
272 int *search_next_done;
273 regex_t *regex;
274 };
276 struct tog_log_view_state {
277 struct commit_queue commits;
278 struct commit_queue_entry *first_displayed_entry;
279 struct commit_queue_entry *last_displayed_entry;
280 struct commit_queue_entry *selected_entry;
281 int selected;
282 char *in_repo_path;
283 const char *head_ref_name;
284 struct got_repository *repo;
285 struct got_reflist_head *refs;
286 struct got_object_id *start_id;
287 sig_atomic_t quit;
288 pthread_t thread;
289 struct tog_log_thread_args thread_args;
290 struct commit_queue_entry *matched_entry;
291 struct commit_queue_entry *search_entry;
292 struct tog_colors colors;
293 };
295 #define TOG_COLOR_DIFF_MINUS 1
296 #define TOG_COLOR_DIFF_PLUS 2
297 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
298 #define TOG_COLOR_DIFF_META 4
299 #define TOG_COLOR_TREE_SUBMODULE 5
300 #define TOG_COLOR_TREE_SYMLINK 6
301 #define TOG_COLOR_TREE_DIRECTORY 7
302 #define TOG_COLOR_TREE_EXECUTABLE 8
303 #define TOG_COLOR_COMMIT 9
304 #define TOG_COLOR_AUTHOR 10
305 #define TOG_COLOR_DATE 11
307 struct tog_blame_cb_args {
308 struct tog_blame_line *lines; /* one per line */
309 int nlines;
311 struct tog_view *view;
312 struct got_object_id *commit_id;
313 int *quit;
314 };
316 struct tog_blame_thread_args {
317 const char *path;
318 struct got_repository *repo;
319 struct tog_blame_cb_args *cb_args;
320 int *complete;
321 got_cancel_cb cancel_cb;
322 void *cancel_arg;
323 };
325 struct tog_blame {
326 FILE *f;
327 size_t filesize;
328 struct tog_blame_line *lines;
329 int nlines;
330 off_t *line_offsets;
331 pthread_t thread;
332 struct tog_blame_thread_args thread_args;
333 struct tog_blame_cb_args cb_args;
334 const char *path;
335 };
337 struct tog_blame_view_state {
338 int first_displayed_line;
339 int last_displayed_line;
340 int selected_line;
341 int blame_complete;
342 int eof;
343 int done;
344 struct got_object_id_queue blamed_commits;
345 struct got_object_qid *blamed_commit;
346 char *path;
347 struct got_repository *repo;
348 struct got_reflist_head *refs;
349 struct got_object_id *commit_id;
350 struct tog_blame blame;
351 int matched_line;
352 struct tog_colors colors;
353 };
355 struct tog_parent_tree {
356 TAILQ_ENTRY(tog_parent_tree) entry;
357 struct got_tree_object *tree;
358 struct got_tree_entry *first_displayed_entry;
359 struct got_tree_entry *selected_entry;
360 int selected;
361 };
363 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
365 struct tog_tree_view_state {
366 char *tree_label;
367 struct got_tree_object *root;
368 struct got_tree_object *tree;
369 const struct got_tree_entries *entries;
370 struct got_tree_entry *first_displayed_entry;
371 struct got_tree_entry *last_displayed_entry;
372 struct got_tree_entry *selected_entry;
373 int ndisplayed, selected, show_ids;
374 struct tog_parent_trees parents;
375 struct got_object_id *commit_id;
376 struct got_repository *repo;
377 struct got_reflist_head *refs;
378 struct got_tree_entry *matched_entry;
379 struct tog_colors colors;
380 };
382 /*
383 * We implement two types of views: parent views and child views.
385 * The 'Tab' key switches between a parent view and its child view.
386 * Child views are shown side-by-side to their parent view, provided
387 * there is enough screen estate.
389 * When a new view is opened from within a parent view, this new view
390 * becomes a child view of the parent view, replacing any existing child.
392 * When a new view is opened from within a child view, this new view
393 * becomes a parent view which will obscure the views below until the
394 * user quits the new parent view by typing 'q'.
396 * This list of views contains parent views only.
397 * Child views are only pointed to by their parent view.
398 */
399 TAILQ_HEAD(tog_view_list_head, tog_view);
401 struct tog_view {
402 TAILQ_ENTRY(tog_view) entry;
403 WINDOW *window;
404 PANEL *panel;
405 int nlines, ncols, begin_y, begin_x;
406 int lines, cols; /* copies of LINES and COLS */
407 int focussed;
408 struct tog_view *parent;
409 struct tog_view *child;
410 int child_focussed;
412 /* type-specific state */
413 enum tog_view_type type;
414 union {
415 struct tog_diff_view_state diff;
416 struct tog_log_view_state log;
417 struct tog_blame_view_state blame;
418 struct tog_tree_view_state tree;
419 } state;
421 const struct got_error *(*show)(struct tog_view *);
422 const struct got_error *(*input)(struct tog_view **,
423 struct tog_view **, struct tog_view**, struct tog_view *, int);
424 const struct got_error *(*close)(struct tog_view *);
426 const struct got_error *(*search_start)(struct tog_view *);
427 const struct got_error *(*search_next)(struct tog_view *);
428 int searching;
429 #define TOG_SEARCH_FORWARD 1
430 #define TOG_SEARCH_BACKWARD 2
431 int search_next_done;
432 regex_t regex;
433 };
435 static const struct got_error *open_diff_view(struct tog_view *,
436 struct got_object_id *, struct got_object_id *, struct tog_view *,
437 struct got_reflist_head *, struct got_repository *);
438 static const struct got_error *show_diff_view(struct tog_view *);
439 static const struct got_error *input_diff_view(struct tog_view **,
440 struct tog_view **, struct tog_view **, struct tog_view *, int);
441 static const struct got_error* close_diff_view(struct tog_view *);
443 static const struct got_error *open_log_view(struct tog_view *,
444 struct got_object_id *, struct got_reflist_head *,
445 struct got_repository *, const char *, const char *, int);
446 static const struct got_error * show_log_view(struct tog_view *);
447 static const struct got_error *input_log_view(struct tog_view **,
448 struct tog_view **, struct tog_view **, struct tog_view *, int);
449 static const struct got_error *close_log_view(struct tog_view *);
450 static const struct got_error *search_start_log_view(struct tog_view *);
451 static const struct got_error *search_next_log_view(struct tog_view *);
453 static const struct got_error *open_blame_view(struct tog_view *, char *,
454 struct got_object_id *, struct got_reflist_head *, struct got_repository *);
455 static const struct got_error *show_blame_view(struct tog_view *);
456 static const struct got_error *input_blame_view(struct tog_view **,
457 struct tog_view **, struct tog_view **, struct tog_view *, int);
458 static const struct got_error *close_blame_view(struct tog_view *);
459 static const struct got_error *search_start_blame_view(struct tog_view *);
460 static const struct got_error *search_next_blame_view(struct tog_view *);
462 static const struct got_error *open_tree_view(struct tog_view *,
463 struct got_tree_object *, struct got_object_id *,
464 struct got_reflist_head *, struct got_repository *);
465 static const struct got_error *show_tree_view(struct tog_view *);
466 static const struct got_error *input_tree_view(struct tog_view **,
467 struct tog_view **, struct tog_view **, struct tog_view *, int);
468 static const struct got_error *close_tree_view(struct tog_view *);
469 static const struct got_error *search_start_tree_view(struct tog_view *);
470 static const struct got_error *search_next_tree_view(struct tog_view *);
472 static volatile sig_atomic_t tog_sigwinch_received;
473 static volatile sig_atomic_t tog_sigpipe_received;
475 static void
476 tog_sigwinch(int signo)
478 tog_sigwinch_received = 1;
481 static void
482 tog_sigpipe(int signo)
484 tog_sigpipe_received = 1;
487 static const struct got_error *
488 view_close(struct tog_view *view)
490 const struct got_error *err = NULL;
492 if (view->child) {
493 view_close(view->child);
494 view->child = NULL;
496 if (view->close)
497 err = view->close(view);
498 if (view->panel)
499 del_panel(view->panel);
500 if (view->window)
501 delwin(view->window);
502 free(view);
503 return err;
506 static struct tog_view *
507 view_open(int nlines, int ncols, int begin_y, int begin_x,
508 enum tog_view_type type)
510 struct tog_view *view = calloc(1, sizeof(*view));
512 if (view == NULL)
513 return NULL;
515 view->type = type;
516 view->lines = LINES;
517 view->cols = COLS;
518 view->nlines = nlines ? nlines : LINES - begin_y;
519 view->ncols = ncols ? ncols : COLS - begin_x;
520 view->begin_y = begin_y;
521 view->begin_x = begin_x;
522 view->window = newwin(nlines, ncols, begin_y, begin_x);
523 if (view->window == NULL) {
524 view_close(view);
525 return NULL;
527 view->panel = new_panel(view->window);
528 if (view->panel == NULL ||
529 set_panel_userptr(view->panel, view) != OK) {
530 view_close(view);
531 return NULL;
534 keypad(view->window, TRUE);
535 return view;
538 static int
539 view_split_begin_x(int begin_x)
541 if (begin_x > 0 || COLS < 120)
542 return 0;
543 return (COLS - MAX(COLS / 2, 80));
546 static const struct got_error *view_resize(struct tog_view *);
548 static const struct got_error *
549 view_splitscreen(struct tog_view *view)
551 const struct got_error *err = NULL;
553 view->begin_y = 0;
554 view->begin_x = view_split_begin_x(0);
555 view->nlines = LINES;
556 view->ncols = COLS - view->begin_x;
557 view->lines = LINES;
558 view->cols = COLS;
559 err = view_resize(view);
560 if (err)
561 return err;
563 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
564 return got_error_from_errno("mvwin");
566 return NULL;
569 static const struct got_error *
570 view_fullscreen(struct tog_view *view)
572 const struct got_error *err = NULL;
574 view->begin_x = 0;
575 view->begin_y = 0;
576 view->nlines = LINES;
577 view->ncols = COLS;
578 view->lines = LINES;
579 view->cols = COLS;
580 err = view_resize(view);
581 if (err)
582 return err;
584 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
585 return got_error_from_errno("mvwin");
587 return NULL;
590 static int
591 view_is_parent_view(struct tog_view *view)
593 return view->parent == NULL;
596 static const struct got_error *
597 view_resize(struct tog_view *view)
599 int nlines, ncols;
601 if (view->lines > LINES)
602 nlines = view->nlines - (view->lines - LINES);
603 else
604 nlines = view->nlines + (LINES - view->lines);
606 if (view->cols > COLS)
607 ncols = view->ncols - (view->cols - COLS);
608 else
609 ncols = view->ncols + (COLS - view->cols);
611 if (wresize(view->window, nlines, ncols) == ERR)
612 return got_error_from_errno("wresize");
613 if (replace_panel(view->panel, view->window) == ERR)
614 return got_error_from_errno("replace_panel");
615 wclear(view->window);
617 view->nlines = nlines;
618 view->ncols = ncols;
619 view->lines = LINES;
620 view->cols = COLS;
622 if (view->child) {
623 view->child->begin_x = view_split_begin_x(view->begin_x);
624 if (view->child->begin_x == 0) {
625 view_fullscreen(view->child);
626 if (view->child->focussed)
627 show_panel(view->child->panel);
628 else
629 show_panel(view->panel);
630 } else {
631 view_splitscreen(view->child);
632 show_panel(view->child->panel);
636 return NULL;
639 static const struct got_error *
640 view_close_child(struct tog_view *view)
642 const struct got_error *err = NULL;
644 if (view->child == NULL)
645 return NULL;
647 err = view_close(view->child);
648 view->child = NULL;
649 return err;
652 static const struct got_error *
653 view_set_child(struct tog_view *view, struct tog_view *child)
655 const struct got_error *err = NULL;
657 view->child = child;
658 child->parent = view;
659 return err;
662 static int
663 view_is_splitscreen(struct tog_view *view)
665 return view->begin_x > 0;
668 static void
669 tog_resizeterm(void)
671 int cols, lines;
672 struct winsize size;
674 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
675 cols = 80; /* Default */
676 lines = 24;
677 } else {
678 cols = size.ws_col;
679 lines = size.ws_row;
681 resize_term(lines, cols);
684 static const struct got_error *
685 view_search_start(struct tog_view *view)
687 const struct got_error *err = NULL;
688 char pattern[1024];
689 int ret;
690 int begin_x = 0;
692 if (view->nlines < 1)
693 return NULL;
695 if (!view_is_parent_view(view))
696 begin_x = view_split_begin_x(view->begin_x);
697 mvwaddstr(view->window, view->begin_y + view->nlines - 1,
698 begin_x, "/");
699 wclrtoeol(view->window);
701 nocbreak();
702 echo();
703 ret = wgetnstr(view->window, pattern, sizeof(pattern));
704 cbreak();
705 noecho();
706 if (ret == ERR)
707 return NULL;
709 if (view->searching) {
710 regfree(&view->regex);
711 view->searching = 0;
714 if (regcomp(&view->regex, pattern,
715 REG_EXTENDED | REG_NOSUB | REG_NEWLINE) == 0) {
716 err = view->search_start(view);
717 if (err) {
718 regfree(&view->regex);
719 return err;
721 view->searching = TOG_SEARCH_FORWARD;
722 view->search_next_done = 0;
723 view->search_next(view);
726 return NULL;
729 static const struct got_error *
730 view_input(struct tog_view **new, struct tog_view **dead,
731 struct tog_view **focus, int *done, struct tog_view *view,
732 struct tog_view_list_head *views)
734 const struct got_error *err = NULL;
735 struct tog_view *v;
736 int ch, errcode;
738 *new = NULL;
739 *dead = NULL;
740 *focus = NULL;
742 if (view->searching && !view->search_next_done) {
743 errcode = pthread_mutex_unlock(&tog_mutex);
744 if (errcode)
745 return got_error_set_errno(errcode,
746 "pthread_mutex_unlock");
747 pthread_yield();
748 errcode = pthread_mutex_lock(&tog_mutex);
749 if (errcode)
750 return got_error_set_errno(errcode,
751 "pthread_mutex_lock");
752 view->search_next(view);
753 return NULL;
756 nodelay(stdscr, FALSE);
757 /* Allow threads to make progress while we are waiting for input. */
758 errcode = pthread_mutex_unlock(&tog_mutex);
759 if (errcode)
760 return got_error_set_errno(errcode, "pthread_mutex_unlock");
761 ch = wgetch(view->window);
762 errcode = pthread_mutex_lock(&tog_mutex);
763 if (errcode)
764 return got_error_set_errno(errcode, "pthread_mutex_lock");
765 nodelay(stdscr, TRUE);
767 if (tog_sigwinch_received) {
768 tog_resizeterm();
769 tog_sigwinch_received = 0;
770 TAILQ_FOREACH(v, views, entry) {
771 err = view_resize(v);
772 if (err)
773 return err;
774 err = v->input(new, dead, focus, v, KEY_RESIZE);
775 if (err)
776 return err;
780 switch (ch) {
781 case ERR:
782 break;
783 case '\t':
784 if (view->child) {
785 *focus = view->child;
786 view->child_focussed = 1;
787 } else if (view->parent) {
788 *focus = view->parent;
789 view->parent->child_focussed = 0;
791 break;
792 case 'q':
793 err = view->input(new, dead, focus, view, ch);
794 *dead = view;
795 break;
796 case 'Q':
797 *done = 1;
798 break;
799 case 'f':
800 if (view_is_parent_view(view)) {
801 if (view->child == NULL)
802 break;
803 if (view_is_splitscreen(view->child)) {
804 *focus = view->child;
805 view->child_focussed = 1;
806 err = view_fullscreen(view->child);
807 } else
808 err = view_splitscreen(view->child);
809 if (err)
810 break;
811 err = view->child->input(new, dead, focus,
812 view->child, KEY_RESIZE);
813 } else {
814 if (view_is_splitscreen(view)) {
815 *focus = view;
816 view->parent->child_focussed = 1;
817 err = view_fullscreen(view);
818 } else {
819 err = view_splitscreen(view);
821 if (err)
822 break;
823 err = view->input(new, dead, focus, view,
824 KEY_RESIZE);
826 break;
827 case KEY_RESIZE:
828 break;
829 case '/':
830 if (view->search_start)
831 view_search_start(view);
832 else
833 err = view->input(new, dead, focus, view, ch);
834 break;
835 case 'N':
836 case 'n':
837 if (view->search_next && view->searching) {
838 view->searching = (ch == 'n' ?
839 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
840 view->search_next_done = 0;
841 view->search_next(view);
842 } else
843 err = view->input(new, dead, focus, view, ch);
844 break;
845 default:
846 err = view->input(new, dead, focus, view, ch);
847 break;
850 return err;
853 void
854 view_vborder(struct tog_view *view)
856 PANEL *panel;
857 struct tog_view *view_above;
859 if (view->parent)
860 return view_vborder(view->parent);
862 panel = panel_above(view->panel);
863 if (panel == NULL)
864 return;
866 view_above = panel_userptr(panel);
867 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
868 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
871 int
872 view_needs_focus_indication(struct tog_view *view)
874 if (view_is_parent_view(view)) {
875 if (view->child == NULL || view->child_focussed)
876 return 0;
877 if (!view_is_splitscreen(view->child))
878 return 0;
879 } else if (!view_is_splitscreen(view))
880 return 0;
882 return view->focussed;
885 static const struct got_error *
886 view_loop(struct tog_view *view)
888 const struct got_error *err = NULL;
889 struct tog_view_list_head views;
890 struct tog_view *new_view, *dead_view, *focus_view, *main_view;
891 int fast_refresh = 10;
892 int done = 0, errcode;
894 errcode = pthread_mutex_lock(&tog_mutex);
895 if (errcode)
896 return got_error_set_errno(errcode, "pthread_mutex_lock");
898 TAILQ_INIT(&views);
899 TAILQ_INSERT_HEAD(&views, view, entry);
901 main_view = view;
902 view->focussed = 1;
903 err = view->show(view);
904 if (err)
905 return err;
906 update_panels();
907 doupdate();
908 while (!TAILQ_EMPTY(&views) && !done && !tog_sigpipe_received) {
909 /* Refresh fast during initialization, then become slower. */
910 if (fast_refresh && fast_refresh-- == 0)
911 halfdelay(10); /* switch to once per second */
913 err = view_input(&new_view, &dead_view, &focus_view, &done,
914 view, &views);
915 if (err)
916 break;
917 if (dead_view) {
918 struct tog_view *prev = NULL;
920 if (view_is_parent_view(dead_view))
921 prev = TAILQ_PREV(dead_view,
922 tog_view_list_head, entry);
923 else if (view->parent != dead_view)
924 prev = view->parent;
926 if (dead_view->parent)
927 dead_view->parent->child = NULL;
928 else
929 TAILQ_REMOVE(&views, dead_view, entry);
931 err = view_close(dead_view);
932 if (err || (dead_view == main_view && new_view == NULL))
933 goto done;
935 if (view == dead_view) {
936 if (focus_view)
937 view = focus_view;
938 else if (prev)
939 view = prev;
940 else if (!TAILQ_EMPTY(&views))
941 view = TAILQ_LAST(&views,
942 tog_view_list_head);
943 else
944 view = NULL;
945 if (view) {
946 if (view->child && view->child_focussed)
947 focus_view = view->child;
948 else
949 focus_view = view;
953 if (new_view) {
954 struct tog_view *v, *t;
955 /* Only allow one parent view per type. */
956 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
957 if (v->type != new_view->type)
958 continue;
959 TAILQ_REMOVE(&views, v, entry);
960 err = view_close(v);
961 if (err)
962 goto done;
963 break;
965 TAILQ_INSERT_TAIL(&views, new_view, entry);
966 view = new_view;
967 if (focus_view == NULL)
968 focus_view = new_view;
970 if (focus_view) {
971 show_panel(focus_view->panel);
972 if (view)
973 view->focussed = 0;
974 focus_view->focussed = 1;
975 view = focus_view;
976 if (new_view)
977 show_panel(new_view->panel);
978 if (view->child && view_is_splitscreen(view->child))
979 show_panel(view->child->panel);
981 if (view) {
982 if (focus_view == NULL) {
983 view->focussed = 1;
984 show_panel(view->panel);
985 if (view->child && view_is_splitscreen(view->child))
986 show_panel(view->child->panel);
987 focus_view = view;
989 if (view->parent) {
990 err = view->parent->show(view->parent);
991 if (err)
992 goto done;
994 err = view->show(view);
995 if (err)
996 goto done;
997 if (view->child) {
998 err = view->child->show(view->child);
999 if (err)
1000 goto done;
1002 update_panels();
1003 doupdate();
1006 done:
1007 while (!TAILQ_EMPTY(&views)) {
1008 view = TAILQ_FIRST(&views);
1009 TAILQ_REMOVE(&views, view, entry);
1010 view_close(view);
1013 errcode = pthread_mutex_unlock(&tog_mutex);
1014 if (errcode && err == NULL)
1015 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1017 return err;
1020 __dead static void
1021 usage_log(void)
1023 endwin();
1024 fprintf(stderr,
1025 "usage: %s log [-c commit] [-r repository-path] [path]\n",
1026 getprogname());
1027 exit(1);
1030 /* Create newly allocated wide-character string equivalent to a byte string. */
1031 static const struct got_error *
1032 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1034 char *vis = NULL;
1035 const struct got_error *err = NULL;
1037 *ws = NULL;
1038 *wlen = mbstowcs(NULL, s, 0);
1039 if (*wlen == (size_t)-1) {
1040 int vislen;
1041 if (errno != EILSEQ)
1042 return got_error_from_errno("mbstowcs");
1044 /* byte string invalid in current encoding; try to "fix" it */
1045 err = got_mbsavis(&vis, &vislen, s);
1046 if (err)
1047 return err;
1048 *wlen = mbstowcs(NULL, vis, 0);
1049 if (*wlen == (size_t)-1) {
1050 err = got_error_from_errno("mbstowcs"); /* give up */
1051 goto done;
1055 *ws = calloc(*wlen + 1, sizeof(**ws));
1056 if (*ws == NULL) {
1057 err = got_error_from_errno("calloc");
1058 goto done;
1061 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1062 err = got_error_from_errno("mbstowcs");
1063 done:
1064 free(vis);
1065 if (err) {
1066 free(*ws);
1067 *ws = NULL;
1068 *wlen = 0;
1070 return err;
1073 /* Format a line for display, ensuring that it won't overflow a width limit. */
1074 static const struct got_error *
1075 format_line(wchar_t **wlinep, int *widthp, const char *line, int wlimit,
1076 int col_tab_align)
1078 const struct got_error *err = NULL;
1079 int cols = 0;
1080 wchar_t *wline = NULL;
1081 size_t wlen;
1082 int i;
1084 *wlinep = NULL;
1085 *widthp = 0;
1087 err = mbs2ws(&wline, &wlen, line);
1088 if (err)
1089 return err;
1091 i = 0;
1092 while (i < wlen) {
1093 int width = wcwidth(wline[i]);
1095 if (width == 0) {
1096 i++;
1097 continue;
1100 if (width == 1 || width == 2) {
1101 if (cols + width > wlimit)
1102 break;
1103 cols += width;
1104 i++;
1105 } else if (width == -1) {
1106 if (wline[i] == L'\t') {
1107 width = TABSIZE -
1108 ((cols + col_tab_align) % TABSIZE);
1109 if (cols + width > wlimit)
1110 break;
1111 cols += width;
1113 i++;
1114 } else {
1115 err = got_error_from_errno("wcwidth");
1116 goto done;
1119 wline[i] = L'\0';
1120 if (widthp)
1121 *widthp = cols;
1122 done:
1123 if (err)
1124 free(wline);
1125 else
1126 *wlinep = wline;
1127 return err;
1130 static const struct got_error*
1131 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1132 struct got_object_id *id, struct got_repository *repo)
1134 static const struct got_error *err = NULL;
1135 struct got_reflist_entry *re;
1136 char *s;
1137 const char *name;
1139 *refs_str = NULL;
1141 SIMPLEQ_FOREACH(re, refs, entry) {
1142 struct got_tag_object *tag = NULL;
1143 int cmp;
1145 name = got_ref_get_name(re->ref);
1146 if (strcmp(name, GOT_REF_HEAD) == 0)
1147 continue;
1148 if (strncmp(name, "refs/", 5) == 0)
1149 name += 5;
1150 if (strncmp(name, "got/", 4) == 0)
1151 continue;
1152 if (strncmp(name, "heads/", 6) == 0)
1153 name += 6;
1154 if (strncmp(name, "remotes/", 8) == 0)
1155 name += 8;
1156 if (strncmp(name, "tags/", 5) == 0) {
1157 err = got_object_open_as_tag(&tag, repo, re->id);
1158 if (err) {
1159 if (err->code != GOT_ERR_OBJ_TYPE)
1160 break;
1161 /* Ref points at something other than a tag. */
1162 err = NULL;
1163 tag = NULL;
1166 cmp = got_object_id_cmp(tag ?
1167 got_object_tag_get_object_id(tag) : re->id, id);
1168 if (tag)
1169 got_object_tag_close(tag);
1170 if (cmp != 0)
1171 continue;
1172 s = *refs_str;
1173 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1174 s ? ", " : "", name) == -1) {
1175 err = got_error_from_errno("asprintf");
1176 free(s);
1177 *refs_str = NULL;
1178 break;
1180 free(s);
1183 return err;
1186 static const struct got_error *
1187 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1188 int col_tab_align)
1190 char *smallerthan, *at;
1192 smallerthan = strchr(author, '<');
1193 if (smallerthan && smallerthan[1] != '\0')
1194 author = smallerthan + 1;
1195 at = strchr(author, '@');
1196 if (at)
1197 *at = '\0';
1198 return format_line(wauthor, author_width, author, limit, col_tab_align);
1201 static const struct got_error *
1202 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1203 struct got_object_id *id, struct got_reflist_head *refs,
1204 const size_t date_display_cols, int author_display_cols,
1205 struct tog_colors *colors)
1207 const struct got_error *err = NULL;
1208 char datebuf[10]; /* YY-MM-DD + SPACE + NUL */
1209 char *logmsg0 = NULL, *logmsg = NULL;
1210 char *author = NULL;
1211 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1212 int author_width, logmsg_width;
1213 char *newline, *line = NULL;
1214 int col, limit;
1215 const int avail = view->ncols;
1216 struct tm tm;
1217 time_t committer_time;
1218 struct tog_color *tc;
1220 committer_time = got_object_commit_get_committer_time(commit);
1221 if (localtime_r(&committer_time, &tm) == NULL)
1222 return got_error_from_errno("localtime_r");
1223 if (strftime(datebuf, sizeof(datebuf), "%g/%m/%d ", &tm)
1224 >= sizeof(datebuf))
1225 return got_error(GOT_ERR_NO_SPACE);
1227 if (avail <= date_display_cols)
1228 limit = MIN(sizeof(datebuf) - 1, avail);
1229 else
1230 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1231 tc = get_color(colors, TOG_COLOR_DATE);
1232 if (tc)
1233 wattr_on(view->window,
1234 COLOR_PAIR(tc->colorpair), NULL);
1235 waddnstr(view->window, datebuf, limit);
1236 if (tc)
1237 wattr_off(view->window,
1238 COLOR_PAIR(tc->colorpair), NULL);
1239 col = limit;
1240 if (col > avail)
1241 goto done;
1243 if (avail >= 120) {
1244 char *id_str;
1245 err = got_object_id_str(&id_str, id);
1246 if (err)
1247 goto done;
1248 tc = get_color(colors, TOG_COLOR_COMMIT);
1249 if (tc)
1250 wattr_on(view->window,
1251 COLOR_PAIR(tc->colorpair), NULL);
1252 wprintw(view->window, "%.8s ", id_str);
1253 if (tc)
1254 wattr_off(view->window,
1255 COLOR_PAIR(tc->colorpair), NULL);
1256 free(id_str);
1257 col += 9;
1258 if (col > avail)
1259 goto done;
1262 author = strdup(got_object_commit_get_author(commit));
1263 if (author == NULL) {
1264 err = got_error_from_errno("strdup");
1265 goto done;
1267 err = format_author(&wauthor, &author_width, author, avail - col, col);
1268 if (err)
1269 goto done;
1270 tc = get_color(colors, TOG_COLOR_AUTHOR);
1271 if (tc)
1272 wattr_on(view->window,
1273 COLOR_PAIR(tc->colorpair), NULL);
1274 waddwstr(view->window, wauthor);
1275 if (tc)
1276 wattr_off(view->window,
1277 COLOR_PAIR(tc->colorpair), NULL);
1278 col += author_width;
1279 while (col < avail && author_width < author_display_cols + 2) {
1280 waddch(view->window, ' ');
1281 col++;
1282 author_width++;
1284 if (col > avail)
1285 goto done;
1287 err = got_object_commit_get_logmsg(&logmsg0, commit);
1288 if (err)
1289 goto done;
1290 logmsg = logmsg0;
1291 while (*logmsg == '\n')
1292 logmsg++;
1293 newline = strchr(logmsg, '\n');
1294 if (newline)
1295 *newline = '\0';
1296 limit = avail - col;
1297 err = format_line(&wlogmsg, &logmsg_width, logmsg, limit, 0);
1298 if (err)
1299 goto done;
1300 waddwstr(view->window, wlogmsg);
1301 col += logmsg_width;
1302 while (col < avail) {
1303 waddch(view->window, ' ');
1304 col++;
1306 done:
1307 free(logmsg0);
1308 free(wlogmsg);
1309 free(author);
1310 free(wauthor);
1311 free(line);
1312 return err;
1315 static struct commit_queue_entry *
1316 alloc_commit_queue_entry(struct got_commit_object *commit,
1317 struct got_object_id *id)
1319 struct commit_queue_entry *entry;
1321 entry = calloc(1, sizeof(*entry));
1322 if (entry == NULL)
1323 return NULL;
1325 entry->id = id;
1326 entry->commit = commit;
1327 return entry;
1330 static void
1331 pop_commit(struct commit_queue *commits)
1333 struct commit_queue_entry *entry;
1335 entry = TAILQ_FIRST(&commits->head);
1336 TAILQ_REMOVE(&commits->head, entry, entry);
1337 got_object_commit_close(entry->commit);
1338 commits->ncommits--;
1339 /* Don't free entry->id! It is owned by the commit graph. */
1340 free(entry);
1343 static void
1344 free_commits(struct commit_queue *commits)
1346 while (!TAILQ_EMPTY(&commits->head))
1347 pop_commit(commits);
1350 static const struct got_error *
1351 match_commit(int *have_match, struct got_object_id *id,
1352 struct got_commit_object *commit, regex_t *regex)
1354 const struct got_error *err = NULL;
1355 regmatch_t regmatch;
1356 char *id_str = NULL, *logmsg = NULL;
1358 *have_match = 0;
1360 err = got_object_id_str(&id_str, id);
1361 if (err)
1362 return err;
1364 err = got_object_commit_get_logmsg(&logmsg, commit);
1365 if (err)
1366 goto done;
1368 if (regexec(regex, got_object_commit_get_author(commit), 1,
1369 &regmatch, 0) == 0 ||
1370 regexec(regex, got_object_commit_get_committer(commit), 1,
1371 &regmatch, 0) == 0 ||
1372 regexec(regex, id_str, 1, &regmatch, 0) == 0 ||
1373 regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1374 *have_match = 1;
1375 done:
1376 free(id_str);
1377 free(logmsg);
1378 return err;
1381 static const struct got_error *
1382 queue_commits(struct got_commit_graph *graph, struct commit_queue *commits,
1383 int minqueue, struct got_repository *repo, const char *path,
1384 int *searching, int *search_next_done, regex_t *regex)
1386 const struct got_error *err = NULL;
1387 int nqueued = 0, have_match = 0;
1390 * We keep all commits open throughout the lifetime of the log
1391 * view in order to avoid having to re-fetch commits from disk
1392 * while updating the display.
1394 while (nqueued < minqueue ||
1395 (*searching == TOG_SEARCH_FORWARD && !*search_next_done)) {
1396 struct got_object_id *id;
1397 struct got_commit_object *commit;
1398 struct commit_queue_entry *entry;
1399 int errcode;
1401 err = got_commit_graph_iter_next(&id, graph);
1402 if (err) {
1403 if (err->code != GOT_ERR_ITER_NEED_MORE)
1404 break;
1405 err = got_commit_graph_fetch_commits(graph,
1406 minqueue, repo, NULL, NULL);
1407 if (err)
1408 return err;
1409 continue;
1412 if (id == NULL)
1413 break;
1415 err = got_object_open_as_commit(&commit, repo, id);
1416 if (err)
1417 break;
1418 entry = alloc_commit_queue_entry(commit, id);
1419 if (entry == NULL) {
1420 err = got_error_from_errno("alloc_commit_queue_entry");
1421 break;
1424 errcode = pthread_mutex_lock(&tog_mutex);
1425 if (errcode) {
1426 err = got_error_set_errno(errcode,
1427 "pthread_mutex_lock");
1428 break;
1431 entry->idx = commits->ncommits;
1432 TAILQ_INSERT_TAIL(&commits->head, entry, entry);
1433 nqueued++;
1434 commits->ncommits++;
1436 if (*searching == TOG_SEARCH_FORWARD && !*search_next_done) {
1437 err = match_commit(&have_match, id, commit, regex);
1438 if (err) {
1439 pthread_mutex_lock(&tog_mutex);
1440 break;
1444 errcode = pthread_mutex_unlock(&tog_mutex);
1445 if (errcode && err == NULL)
1446 err = got_error_set_errno(errcode,
1447 "pthread_mutex_unlock");
1449 if (have_match)
1450 break;
1453 return err;
1456 static const struct got_error *
1457 get_head_commit_id(struct got_object_id **head_id, const char *branch_name,
1458 struct got_repository *repo)
1460 const struct got_error *err = NULL;
1461 struct got_reference *head_ref;
1463 *head_id = NULL;
1465 err = got_ref_open(&head_ref, repo, branch_name, 0);
1466 if (err)
1467 return err;
1469 err = got_ref_resolve(head_id, repo, head_ref);
1470 got_ref_close(head_ref);
1471 if (err) {
1472 *head_id = NULL;
1473 return err;
1476 return NULL;
1479 static const struct got_error *
1480 draw_commits(struct tog_view *view, struct commit_queue_entry **last,
1481 struct commit_queue_entry **selected, struct commit_queue_entry *first,
1482 struct commit_queue *commits, int selected_idx, int limit,
1483 struct got_reflist_head *refs, const char *path, int commits_needed,
1484 struct tog_colors *colors)
1486 const struct got_error *err = NULL;
1487 struct tog_log_view_state *s = &view->state.log;
1488 struct commit_queue_entry *entry;
1489 int width;
1490 int ncommits, author_cols = 4;
1491 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1492 char *refs_str = NULL;
1493 wchar_t *wline;
1494 struct tog_color *tc;
1495 static const size_t date_display_cols = 9;
1497 entry = first;
1498 ncommits = 0;
1499 while (entry) {
1500 if (ncommits == selected_idx) {
1501 *selected = entry;
1502 break;
1504 entry = TAILQ_NEXT(entry, entry);
1505 ncommits++;
1508 if (*selected && !(view->searching && view->search_next_done == 0)) {
1509 err = got_object_id_str(&id_str, (*selected)->id);
1510 if (err)
1511 return err;
1512 if (refs) {
1513 err = build_refs_str(&refs_str, refs, (*selected)->id,
1514 s->repo);
1515 if (err)
1516 goto done;
1520 if (commits_needed == 0)
1521 halfdelay(10); /* disable fast refresh */
1523 if (asprintf(&ncommits_str, " [%d/%d] %s",
1524 entry ? entry->idx + 1 : 0, commits->ncommits,
1525 commits_needed > 0 ?
1526 (view->searching && view->search_next_done == 0
1527 ? "searching..." : "loading... ") :
1528 (refs_str ? refs_str : "")) == -1) {
1529 err = got_error_from_errno("asprintf");
1530 goto done;
1533 if (path && strcmp(path, "/") != 0) {
1534 if (asprintf(&header, "commit %s %s%s",
1535 id_str ? id_str : "........................................",
1536 path, ncommits_str) == -1) {
1537 err = got_error_from_errno("asprintf");
1538 header = NULL;
1539 goto done;
1541 } else if (asprintf(&header, "commit %s%s",
1542 id_str ? id_str : "........................................",
1543 ncommits_str) == -1) {
1544 err = got_error_from_errno("asprintf");
1545 header = NULL;
1546 goto done;
1548 err = format_line(&wline, &width, header, view->ncols, 0);
1549 if (err)
1550 goto done;
1552 werase(view->window);
1554 if (view_needs_focus_indication(view))
1555 wstandout(view->window);
1556 tc = get_color(colors, TOG_COLOR_COMMIT);
1557 if (tc)
1558 wattr_on(view->window,
1559 COLOR_PAIR(tc->colorpair), NULL);
1560 waddwstr(view->window, wline);
1561 if (tc)
1562 wattr_off(view->window,
1563 COLOR_PAIR(tc->colorpair), NULL);
1564 while (width < view->ncols) {
1565 waddch(view->window, ' ');
1566 width++;
1568 if (view_needs_focus_indication(view))
1569 wstandend(view->window);
1570 free(wline);
1571 if (limit <= 1)
1572 goto done;
1574 /* Grow author column size if necessary. */
1575 entry = first;
1576 ncommits = 0;
1577 while (entry) {
1578 char *author;
1579 wchar_t *wauthor;
1580 int width;
1581 if (ncommits >= limit - 1)
1582 break;
1583 author = strdup(got_object_commit_get_author(entry->commit));
1584 if (author == NULL) {
1585 err = got_error_from_errno("strdup");
1586 goto done;
1588 err = format_author(&wauthor, &width, author, COLS,
1589 date_display_cols);
1590 if (author_cols < width)
1591 author_cols = width;
1592 free(wauthor);
1593 free(author);
1594 ncommits++;
1595 entry = TAILQ_NEXT(entry, entry);
1598 entry = first;
1599 *last = first;
1600 ncommits = 0;
1601 while (entry) {
1602 if (ncommits >= limit - 1)
1603 break;
1604 if (ncommits == selected_idx)
1605 wstandout(view->window);
1606 err = draw_commit(view, entry->commit, entry->id, refs,
1607 date_display_cols, author_cols, colors);
1608 if (ncommits == selected_idx)
1609 wstandend(view->window);
1610 if (err)
1611 goto done;
1612 ncommits++;
1613 *last = entry;
1614 entry = TAILQ_NEXT(entry, entry);
1617 view_vborder(view);
1618 done:
1619 free(id_str);
1620 free(refs_str);
1621 free(ncommits_str);
1622 free(header);
1623 return err;
1626 static void
1627 scroll_up(struct tog_view *view,
1628 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1629 struct commit_queue *commits)
1631 struct commit_queue_entry *entry;
1632 int nscrolled = 0;
1634 entry = TAILQ_FIRST(&commits->head);
1635 if (*first_displayed_entry == entry)
1636 return;
1638 entry = *first_displayed_entry;
1639 while (entry && nscrolled < maxscroll) {
1640 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1641 if (entry) {
1642 *first_displayed_entry = entry;
1643 nscrolled++;
1648 static const struct got_error *
1649 trigger_log_thread(int load_all, int *commits_needed, int *log_complete,
1650 pthread_cond_t *need_commits)
1652 int errcode;
1653 int max_wait = 20;
1655 halfdelay(1); /* fast refresh while loading commits */
1657 while (*commits_needed > 0) {
1658 if (*log_complete)
1659 break;
1661 /* Wake the log thread. */
1662 errcode = pthread_cond_signal(need_commits);
1663 if (errcode)
1664 return got_error_set_errno(errcode,
1665 "pthread_cond_signal");
1666 errcode = pthread_mutex_unlock(&tog_mutex);
1667 if (errcode)
1668 return got_error_set_errno(errcode,
1669 "pthread_mutex_unlock");
1670 pthread_yield();
1671 errcode = pthread_mutex_lock(&tog_mutex);
1672 if (errcode)
1673 return got_error_set_errno(errcode,
1674 "pthread_mutex_lock");
1676 if (*commits_needed > 0 && (!load_all || --max_wait <= 0)) {
1678 * Thread is not done yet; lose a key press
1679 * and let the user retry... this way the GUI
1680 * remains interactive while logging deep paths
1681 * with few commits in history.
1683 return NULL;
1687 return NULL;
1690 static const struct got_error *
1691 scroll_down(struct tog_view *view,
1692 struct commit_queue_entry **first_displayed_entry, int maxscroll,
1693 struct commit_queue_entry **last_displayed_entry,
1694 struct commit_queue *commits, int *log_complete, int *commits_needed,
1695 pthread_cond_t *need_commits)
1697 const struct got_error *err = NULL;
1698 struct commit_queue_entry *pentry;
1699 int nscrolled = 0;
1701 if (*last_displayed_entry == NULL)
1702 return NULL;
1704 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1705 if (pentry == NULL && !*log_complete) {
1707 * Ask the log thread for required amount of commits
1708 * plus some amount of pre-fetching.
1710 (*commits_needed) += maxscroll + 20;
1711 err = trigger_log_thread(0, commits_needed, log_complete,
1712 need_commits);
1713 if (err)
1714 return err;
1717 do {
1718 pentry = TAILQ_NEXT(*last_displayed_entry, entry);
1719 if (pentry == NULL)
1720 break;
1722 *last_displayed_entry = pentry;
1724 pentry = TAILQ_NEXT(*first_displayed_entry, entry);
1725 if (pentry == NULL)
1726 break;
1727 *first_displayed_entry = pentry;
1728 } while (++nscrolled < maxscroll);
1730 return err;
1733 static const struct got_error *
1734 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1735 struct got_commit_object *commit, struct got_object_id *commit_id,
1736 struct tog_view *log_view, struct got_reflist_head *refs,
1737 struct got_repository *repo)
1739 const struct got_error *err;
1740 struct got_object_qid *parent_id;
1741 struct tog_view *diff_view;
1743 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
1744 if (diff_view == NULL)
1745 return got_error_from_errno("view_open");
1747 parent_id = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1748 err = open_diff_view(diff_view, parent_id ? parent_id->id : NULL,
1749 commit_id, log_view, refs, repo);
1750 if (err == NULL)
1751 *new_view = diff_view;
1752 return err;
1755 static const struct got_error *
1756 tree_view_visit_subtree(struct got_tree_object *subtree,
1757 struct tog_tree_view_state *s)
1759 struct tog_parent_tree *parent;
1761 parent = calloc(1, sizeof(*parent));
1762 if (parent == NULL)
1763 return got_error_from_errno("calloc");
1765 parent->tree = s->tree;
1766 parent->first_displayed_entry = s->first_displayed_entry;
1767 parent->selected_entry = s->selected_entry;
1768 parent->selected = s->selected;
1769 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
1770 s->tree = subtree;
1771 s->entries = got_object_tree_get_entries(s->tree);
1772 s->selected = 0;
1773 s->first_displayed_entry = NULL;
1774 return NULL;
1778 static const struct got_error *
1779 browse_commit_tree(struct tog_view **new_view, int begin_x,
1780 struct commit_queue_entry *entry, const char *path,
1781 struct got_reflist_head *refs, struct got_repository *repo)
1783 const struct got_error *err = NULL;
1784 struct got_tree_object *tree;
1785 struct got_tree_entry *te;
1786 struct tog_tree_view_state *s;
1787 struct tog_view *tree_view;
1788 char *slash, *subpath = NULL;
1789 const char *p;
1791 err = got_object_open_as_tree(&tree, repo,
1792 got_object_commit_get_tree_id(entry->commit));
1793 if (err)
1794 return err;
1796 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
1797 if (tree_view == NULL)
1798 return got_error_from_errno("view_open");
1800 err = open_tree_view(tree_view, tree, entry->id, refs, repo);
1801 if (err) {
1802 got_object_tree_close(tree);
1803 return err;
1805 s = &tree_view->state.tree;
1807 *new_view = tree_view;
1809 /* Walk the path and open corresponding tree objects. */
1810 p = path;
1811 while (p[0] == '/')
1812 p++;
1813 while (*p) {
1814 struct got_object_id *tree_id;
1816 /* Ensure the correct subtree entry is selected. */
1817 slash = strchr(p, '/');
1818 if (slash == NULL)
1819 slash = strchr(p, '\0');
1820 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
1821 if (strncmp(p, te->name, slash - p) == 0) {
1822 s->selected_entry = te;
1823 break;
1825 s->selected++;
1827 if (s->selected_entry == NULL) {
1828 err = got_error(GOT_ERR_NO_TREE_ENTRY);
1829 break;
1831 if (s->tree != s->root)
1832 s->selected++; /* skip '..' */
1834 if (!S_ISDIR(s->selected_entry->mode)) {
1835 /* Jump to this file's entry. */
1836 s->first_displayed_entry = s->selected_entry;
1837 s->selected = 0;
1838 break;
1841 slash = strchr(p, '/');
1842 if (slash)
1843 subpath = strndup(path, slash - path);
1844 else
1845 subpath = strdup(path);
1846 if (subpath == NULL) {
1847 err = got_error_from_errno("strdup");
1848 break;
1851 err = got_object_id_by_path(&tree_id, repo, entry->id,
1852 subpath);
1853 if (err)
1854 break;
1856 err = got_object_open_as_tree(&tree, repo, tree_id);
1857 free(tree_id);
1858 if (err)
1859 break;
1861 err = tree_view_visit_subtree(tree, s);
1862 if (err) {
1863 got_object_tree_close(tree);
1864 break;
1866 if (slash == NULL)
1867 break;
1868 free(subpath);
1869 subpath = NULL;
1870 p = slash;
1873 free(subpath);
1874 return err;
1877 static void *
1878 log_thread(void *arg)
1880 const struct got_error *err = NULL;
1881 int errcode = 0;
1882 struct tog_log_thread_args *a = arg;
1883 int done = 0;
1885 err = got_commit_graph_iter_start(a->graph, a->start_id, a->repo,
1886 NULL, NULL);
1887 if (err)
1888 return (void *)err;
1890 while (!done && !err && !tog_sigpipe_received) {
1891 err = queue_commits(a->graph, a->commits, 1, a->repo,
1892 a->in_repo_path, a->searching, a->search_next_done,
1893 a->regex);
1894 if (err) {
1895 if (err->code != GOT_ERR_ITER_COMPLETED)
1896 return (void *)err;
1897 err = NULL;
1898 done = 1;
1899 } else if (a->commits_needed > 0)
1900 a->commits_needed--;
1902 errcode = pthread_mutex_lock(&tog_mutex);
1903 if (errcode) {
1904 err = got_error_set_errno(errcode,
1905 "pthread_mutex_lock");
1906 break;
1907 } else if (*a->quit)
1908 done = 1;
1909 else if (*a->first_displayed_entry == NULL) {
1910 *a->first_displayed_entry =
1911 TAILQ_FIRST(&a->commits->head);
1912 *a->selected_entry = *a->first_displayed_entry;
1915 if (done)
1916 a->commits_needed = 0;
1917 else if (a->commits_needed == 0) {
1918 errcode = pthread_cond_wait(&a->need_commits,
1919 &tog_mutex);
1920 if (errcode)
1921 err = got_error_set_errno(errcode,
1922 "pthread_cond_wait");
1925 errcode = pthread_mutex_unlock(&tog_mutex);
1926 if (errcode && err == NULL)
1927 err = got_error_set_errno(errcode,
1928 "pthread_mutex_unlock");
1930 a->log_complete = 1;
1931 return (void *)err;
1934 static const struct got_error *
1935 stop_log_thread(struct tog_log_view_state *s)
1937 const struct got_error *err = NULL;
1938 int errcode;
1940 if (s->thread) {
1941 s->quit = 1;
1942 errcode = pthread_cond_signal(&s->thread_args.need_commits);
1943 if (errcode)
1944 return got_error_set_errno(errcode,
1945 "pthread_cond_signal");
1946 errcode = pthread_mutex_unlock(&tog_mutex);
1947 if (errcode)
1948 return got_error_set_errno(errcode,
1949 "pthread_mutex_unlock");
1950 errcode = pthread_join(s->thread, (void **)&err);
1951 if (errcode)
1952 return got_error_set_errno(errcode, "pthread_join");
1953 errcode = pthread_mutex_lock(&tog_mutex);
1954 if (errcode)
1955 return got_error_set_errno(errcode,
1956 "pthread_mutex_lock");
1957 s->thread = NULL;
1960 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
1961 if (errcode && err == NULL)
1962 err = got_error_set_errno(errcode, "pthread_cond_destroy");
1964 if (s->thread_args.repo) {
1965 got_repo_close(s->thread_args.repo);
1966 s->thread_args.repo = NULL;
1969 if (s->thread_args.graph) {
1970 got_commit_graph_close(s->thread_args.graph);
1971 s->thread_args.graph = NULL;
1974 return err;
1977 static const struct got_error *
1978 close_log_view(struct tog_view *view)
1980 const struct got_error *err = NULL;
1981 struct tog_log_view_state *s = &view->state.log;
1983 err = stop_log_thread(s);
1984 free_commits(&s->commits);
1985 free(s->in_repo_path);
1986 s->in_repo_path = NULL;
1987 free(s->start_id);
1988 s->start_id = NULL;
1989 return err;
1992 static const struct got_error *
1993 search_start_log_view(struct tog_view *view)
1995 struct tog_log_view_state *s = &view->state.log;
1997 s->matched_entry = NULL;
1998 s->search_entry = NULL;
1999 return NULL;
2002 static const struct got_error *
2003 search_next_log_view(struct tog_view *view)
2005 const struct got_error *err = NULL;
2006 struct tog_log_view_state *s = &view->state.log;
2007 struct commit_queue_entry *entry;
2009 if (!view->searching) {
2010 view->search_next_done = 1;
2011 return NULL;
2014 if (s->search_entry) {
2015 int errcode, ch;
2016 errcode = pthread_mutex_unlock(&tog_mutex);
2017 if (errcode)
2018 return got_error_set_errno(errcode,
2019 "pthread_mutex_unlock");
2020 ch = wgetch(view->window);
2021 errcode = pthread_mutex_lock(&tog_mutex);
2022 if (errcode)
2023 return got_error_set_errno(errcode,
2024 "pthread_mutex_lock");
2025 if (ch == KEY_BACKSPACE) {
2026 view->search_next_done = 1;
2027 return NULL;
2029 if (view->searching == TOG_SEARCH_FORWARD)
2030 entry = TAILQ_NEXT(s->search_entry, entry);
2031 else
2032 entry = TAILQ_PREV(s->search_entry,
2033 commit_queue_head, entry);
2034 } else if (s->matched_entry) {
2035 if (view->searching == TOG_SEARCH_FORWARD)
2036 entry = TAILQ_NEXT(s->selected_entry, entry);
2037 else
2038 entry = TAILQ_PREV(s->selected_entry,
2039 commit_queue_head, entry);
2040 } else {
2041 if (view->searching == TOG_SEARCH_FORWARD)
2042 entry = TAILQ_FIRST(&s->commits.head);
2043 else
2044 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2047 while (1) {
2048 int have_match = 0;
2050 if (entry == NULL) {
2051 if (s->thread_args.log_complete ||
2052 view->searching == TOG_SEARCH_BACKWARD) {
2053 view->search_next_done = 1;
2054 return NULL;
2057 * Poke the log thread for more commits and return,
2058 * allowing the main loop to make progress. Search
2059 * will resume at s->search_entry once we come back.
2061 s->thread_args.commits_needed++;
2062 return trigger_log_thread(1,
2063 &s->thread_args.commits_needed,
2064 &s->thread_args.log_complete,
2065 &s->thread_args.need_commits);
2068 err = match_commit(&have_match, entry->id, entry->commit,
2069 &view->regex);
2070 if (err)
2071 break;
2072 if (have_match) {
2073 view->search_next_done = 1;
2074 s->matched_entry = entry;
2075 break;
2078 s->search_entry = entry;
2079 if (view->searching == TOG_SEARCH_FORWARD)
2080 entry = TAILQ_NEXT(entry, entry);
2081 else
2082 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2085 if (s->matched_entry) {
2086 int cur = s->selected_entry->idx;
2087 while (cur < s->matched_entry->idx) {
2088 err = input_log_view(NULL, NULL, NULL, view, KEY_DOWN);
2089 if (err)
2090 return err;
2091 cur++;
2093 while (cur > s->matched_entry->idx) {
2094 err = input_log_view(NULL, NULL, NULL, view, KEY_UP);
2095 if (err)
2096 return err;
2097 cur--;
2101 s->search_entry = NULL;
2103 return NULL;
2106 static const struct got_error *
2107 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2108 struct got_reflist_head *refs, struct got_repository *repo,
2109 const char *head_ref_name, const char *path, int check_disk)
2111 const struct got_error *err = NULL;
2112 struct tog_log_view_state *s = &view->state.log;
2113 struct got_repository *thread_repo = NULL;
2114 struct got_commit_graph *thread_graph = NULL;
2115 int errcode;
2117 err = got_repo_map_path(&s->in_repo_path, repo, path, check_disk);
2118 if (err != NULL)
2119 goto done;
2121 /* The commit queue only contains commits being displayed. */
2122 TAILQ_INIT(&s->commits.head);
2123 s->commits.ncommits = 0;
2125 s->refs = refs;
2126 s->repo = repo;
2127 s->head_ref_name = head_ref_name;
2128 s->start_id = got_object_id_dup(start_id);
2129 if (s->start_id == NULL) {
2130 err = got_error_from_errno("got_object_id_dup");
2131 goto done;
2134 SIMPLEQ_INIT(&s->colors);
2135 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2136 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2137 get_color_value("TOG_COLOR_COMMIT"));
2138 if (err)
2139 goto done;
2140 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2141 get_color_value("TOG_COLOR_AUTHOR"));
2142 if (err) {
2143 free_colors(&s->colors);
2144 goto done;
2146 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2147 get_color_value("TOG_COLOR_DATE"));
2148 if (err) {
2149 free_colors(&s->colors);
2150 goto done;
2154 view->show = show_log_view;
2155 view->input = input_log_view;
2156 view->close = close_log_view;
2157 view->search_start = search_start_log_view;
2158 view->search_next = search_next_log_view;
2160 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
2161 if (err)
2162 goto done;
2163 err = got_commit_graph_open(&thread_graph, start_id, s->in_repo_path,
2164 0, thread_repo);
2165 if (err)
2166 goto done;
2168 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2169 if (errcode) {
2170 err = got_error_set_errno(errcode, "pthread_cond_init");
2171 goto done;
2174 s->thread_args.commits_needed = view->nlines;
2175 s->thread_args.graph = thread_graph;
2176 s->thread_args.commits = &s->commits;
2177 s->thread_args.in_repo_path = s->in_repo_path;
2178 s->thread_args.start_id = s->start_id;
2179 s->thread_args.repo = thread_repo;
2180 s->thread_args.log_complete = 0;
2181 s->thread_args.quit = &s->quit;
2182 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2183 s->thread_args.selected_entry = &s->selected_entry;
2184 s->thread_args.searching = &view->searching;
2185 s->thread_args.search_next_done = &view->search_next_done;
2186 s->thread_args.regex = &view->regex;
2187 done:
2188 if (err)
2189 close_log_view(view);
2190 return err;
2193 static const struct got_error *
2194 show_log_view(struct tog_view *view)
2196 struct tog_log_view_state *s = &view->state.log;
2198 if (s->thread == NULL) {
2199 int errcode = pthread_create(&s->thread, NULL, log_thread,
2200 &s->thread_args);
2201 if (errcode)
2202 return got_error_set_errno(errcode, "pthread_create");
2205 return draw_commits(view, &s->last_displayed_entry,
2206 &s->selected_entry, s->first_displayed_entry,
2207 &s->commits, s->selected, view->nlines, s->refs,
2208 s->in_repo_path, s->thread_args.commits_needed, &s->colors);
2211 static const struct got_error *
2212 input_log_view(struct tog_view **new_view, struct tog_view **dead_view,
2213 struct tog_view **focus_view, struct tog_view *view, int ch)
2215 const struct got_error *err = NULL;
2216 struct tog_log_view_state *s = &view->state.log;
2217 char *parent_path, *in_repo_path = NULL;
2218 struct tog_view *diff_view = NULL, *tree_view = NULL, *lv = NULL;
2219 int begin_x = 0;
2220 struct got_object_id *start_id;
2222 switch (ch) {
2223 case 'q':
2224 s->quit = 1;
2225 break;
2226 case 'k':
2227 case KEY_UP:
2228 case '<':
2229 case ',':
2230 if (s->first_displayed_entry == NULL)
2231 break;
2232 if (s->selected > 0)
2233 s->selected--;
2234 else
2235 scroll_up(view, &s->first_displayed_entry, 1,
2236 &s->commits);
2237 break;
2238 case KEY_PPAGE:
2239 case CTRL('b'):
2240 if (s->first_displayed_entry == NULL)
2241 break;
2242 if (TAILQ_FIRST(&s->commits.head) ==
2243 s->first_displayed_entry) {
2244 s->selected = 0;
2245 break;
2247 scroll_up(view, &s->first_displayed_entry,
2248 view->nlines, &s->commits);
2249 break;
2250 case 'j':
2251 case KEY_DOWN:
2252 case '>':
2253 case '.':
2254 if (s->first_displayed_entry == NULL)
2255 break;
2256 if (s->selected < MIN(view->nlines - 2,
2257 s->commits.ncommits - 1)) {
2258 s->selected++;
2259 break;
2261 err = scroll_down(view, &s->first_displayed_entry, 1,
2262 &s->last_displayed_entry, &s->commits,
2263 &s->thread_args.log_complete,
2264 &s->thread_args.commits_needed,
2265 &s->thread_args.need_commits);
2266 break;
2267 case KEY_NPAGE:
2268 case CTRL('f'): {
2269 struct commit_queue_entry *first;
2270 first = s->first_displayed_entry;
2271 if (first == NULL)
2272 break;
2273 err = scroll_down(view, &s->first_displayed_entry,
2274 view->nlines, &s->last_displayed_entry,
2275 &s->commits, &s->thread_args.log_complete,
2276 &s->thread_args.commits_needed,
2277 &s->thread_args.need_commits);
2278 if (err)
2279 break;
2280 if (first == s->first_displayed_entry &&
2281 s->selected < MIN(view->nlines - 2,
2282 s->commits.ncommits - 1)) {
2283 /* can't scroll further down */
2284 s->selected = MIN(view->nlines - 2,
2285 s->commits.ncommits - 1);
2287 err = NULL;
2288 break;
2290 case KEY_RESIZE:
2291 if (s->selected > view->nlines - 2)
2292 s->selected = view->nlines - 2;
2293 if (s->selected > s->commits.ncommits - 1)
2294 s->selected = s->commits.ncommits - 1;
2295 break;
2296 case KEY_ENTER:
2297 case ' ':
2298 case '\r':
2299 if (s->selected_entry == NULL)
2300 break;
2301 if (view_is_parent_view(view))
2302 begin_x = view_split_begin_x(view->begin_x);
2303 err = open_diff_view_for_commit(&diff_view, begin_x,
2304 s->selected_entry->commit, s->selected_entry->id,
2305 view, s->refs, s->repo);
2306 if (err)
2307 break;
2308 if (view_is_parent_view(view)) {
2309 err = view_close_child(view);
2310 if (err)
2311 return err;
2312 err = view_set_child(view, diff_view);
2313 if (err) {
2314 view_close(diff_view);
2315 break;
2317 *focus_view = diff_view;
2318 view->child_focussed = 1;
2319 } else
2320 *new_view = diff_view;
2321 break;
2322 case 't':
2323 if (s->selected_entry == NULL)
2324 break;
2325 if (view_is_parent_view(view))
2326 begin_x = view_split_begin_x(view->begin_x);
2327 err = browse_commit_tree(&tree_view, begin_x,
2328 s->selected_entry, s->in_repo_path, s->refs, s->repo);
2329 if (err)
2330 break;
2331 if (view_is_parent_view(view)) {
2332 err = view_close_child(view);
2333 if (err)
2334 return err;
2335 err = view_set_child(view, tree_view);
2336 if (err) {
2337 view_close(tree_view);
2338 break;
2340 *focus_view = tree_view;
2341 view->child_focussed = 1;
2342 } else
2343 *new_view = tree_view;
2344 break;
2345 case KEY_BACKSPACE:
2346 if (strcmp(s->in_repo_path, "/") == 0)
2347 break;
2348 parent_path = dirname(s->in_repo_path);
2349 if (parent_path && strcmp(parent_path, ".") != 0) {
2350 err = stop_log_thread(s);
2351 if (err)
2352 return err;
2353 lv = view_open(view->nlines, view->ncols,
2354 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2355 if (lv == NULL)
2356 return got_error_from_errno(
2357 "view_open");
2358 err = open_log_view(lv, s->start_id, s->refs,
2359 s->repo, s->head_ref_name, parent_path, 0);
2360 if (err)
2361 return err;;
2362 if (view_is_parent_view(view))
2363 *new_view = lv;
2364 else {
2365 view_set_child(view->parent, lv);
2366 *focus_view = lv;
2368 return NULL;
2370 break;
2371 case CTRL('l'):
2372 err = stop_log_thread(s);
2373 if (err)
2374 return err;
2375 lv = view_open(view->nlines, view->ncols,
2376 view->begin_y, view->begin_x, TOG_VIEW_LOG);
2377 if (lv == NULL)
2378 return got_error_from_errno("view_open");
2379 err = get_head_commit_id(&start_id, s->head_ref_name ?
2380 s->head_ref_name : GOT_REF_HEAD, s->repo);
2381 if (err) {
2382 view_close(lv);
2383 return err;
2385 in_repo_path = strdup(s->in_repo_path);
2386 if (in_repo_path == NULL) {
2387 free(start_id);
2388 view_close(lv);
2389 return got_error_from_errno("strdup");
2391 got_ref_list_free(s->refs);
2392 err = got_ref_list(s->refs, s->repo, NULL,
2393 got_ref_cmp_by_name, NULL);
2394 if (err) {
2395 free(start_id);
2396 view_close(lv);
2397 return err;
2399 err = open_log_view(lv, start_id, s->refs, s->repo,
2400 s->head_ref_name, in_repo_path, 0);
2401 if (err) {
2402 free(start_id);
2403 view_close(lv);
2404 return err;;
2406 *dead_view = view;
2407 *new_view = lv;
2408 break;
2409 default:
2410 break;
2413 return err;
2416 static const struct got_error *
2417 apply_unveil(const char *repo_path, const char *worktree_path)
2419 const struct got_error *error;
2421 #ifdef PROFILE
2422 if (unveil("gmon.out", "rwc") != 0)
2423 return got_error_from_errno2("unveil", "gmon.out");
2424 #endif
2425 if (repo_path && unveil(repo_path, "r") != 0)
2426 return got_error_from_errno2("unveil", repo_path);
2428 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2429 return got_error_from_errno2("unveil", worktree_path);
2431 if (unveil("/tmp", "rwc") != 0)
2432 return got_error_from_errno2("unveil", "/tmp");
2434 error = got_privsep_unveil_exec_helpers();
2435 if (error != NULL)
2436 return error;
2438 if (unveil(NULL, NULL) != 0)
2439 return got_error_from_errno("unveil");
2441 return NULL;
2444 static void
2445 init_curses(void)
2447 initscr();
2448 cbreak();
2449 halfdelay(1); /* Do fast refresh while initial view is loading. */
2450 noecho();
2451 nonl();
2452 intrflush(stdscr, FALSE);
2453 keypad(stdscr, TRUE);
2454 curs_set(0);
2455 if (getenv("TOG_COLORS") != NULL) {
2456 start_color();
2457 use_default_colors();
2459 signal(SIGWINCH, tog_sigwinch);
2460 signal(SIGPIPE, tog_sigpipe);
2463 static const struct got_error *
2464 cmd_log(int argc, char *argv[])
2466 const struct got_error *error;
2467 struct got_repository *repo = NULL;
2468 struct got_worktree *worktree = NULL;
2469 struct got_reflist_head refs;
2470 struct got_object_id *start_id = NULL;
2471 char *path = NULL, *repo_path = NULL, *cwd = NULL;
2472 char *start_commit = NULL, *head_ref_name = NULL;
2473 int ch;
2474 struct tog_view *view;
2476 SIMPLEQ_INIT(&refs);
2478 #ifndef PROFILE
2479 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
2480 NULL) == -1)
2481 err(1, "pledge");
2482 #endif
2484 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2485 switch (ch) {
2486 case 'c':
2487 start_commit = optarg;
2488 break;
2489 case 'r':
2490 repo_path = realpath(optarg, NULL);
2491 if (repo_path == NULL)
2492 return got_error_from_errno2("realpath",
2493 optarg);
2494 break;
2495 default:
2496 usage_log();
2497 /* NOTREACHED */
2501 argc -= optind;
2502 argv += optind;
2504 cwd = getcwd(NULL, 0);
2505 if (cwd == NULL) {
2506 error = got_error_from_errno("getcwd");
2507 goto done;
2509 error = got_worktree_open(&worktree, cwd);
2510 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2511 goto done;
2512 error = NULL;
2514 if (argc == 0) {
2515 path = strdup("");
2516 if (path == NULL) {
2517 error = got_error_from_errno("strdup");
2518 goto done;
2520 } else if (argc == 1) {
2521 if (worktree) {
2522 error = got_worktree_resolve_path(&path, worktree,
2523 argv[0]);
2524 if (error)
2525 goto done;
2526 } else {
2527 path = strdup(argv[0]);
2528 if (path == NULL) {
2529 error = got_error_from_errno("strdup");
2530 goto done;
2533 } else
2534 usage_log();
2536 if (repo_path == NULL) {
2537 if (worktree)
2538 repo_path = strdup(
2539 got_worktree_get_repo_path(worktree));
2540 else
2541 repo_path = strdup(cwd);
2543 if (repo_path == NULL) {
2544 error = got_error_from_errno("strdup");
2545 goto done;
2548 init_curses();
2550 error = got_repo_open(&repo, repo_path, NULL);
2551 if (error != NULL)
2552 goto done;
2554 error = apply_unveil(got_repo_get_path(repo),
2555 worktree ? got_worktree_get_root_path(worktree) : NULL);
2556 if (error)
2557 goto done;
2559 if (start_commit == NULL)
2560 error = get_head_commit_id(&start_id, worktree ?
2561 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
2562 repo);
2563 else {
2564 error = get_head_commit_id(&start_id, start_commit, repo);
2565 if (error) {
2566 if (error->code != GOT_ERR_NOT_REF)
2567 goto done;
2568 error = got_repo_match_object_id_prefix(&start_id,
2569 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2572 if (error != NULL)
2573 goto done;
2575 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2576 if (error)
2577 goto done;
2579 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
2580 if (view == NULL) {
2581 error = got_error_from_errno("view_open");
2582 goto done;
2584 if (worktree) {
2585 head_ref_name = strdup(
2586 got_worktree_get_head_ref_name(worktree));
2587 if (head_ref_name == NULL) {
2588 error = got_error_from_errno("strdup");
2589 goto done;
2592 error = open_log_view(view, start_id, &refs, repo, head_ref_name,
2593 path, 1);
2594 if (error)
2595 goto done;
2596 if (worktree) {
2597 /* Release work tree lock. */
2598 got_worktree_close(worktree);
2599 worktree = NULL;
2601 error = view_loop(view);
2602 done:
2603 free(repo_path);
2604 free(cwd);
2605 free(path);
2606 free(start_id);
2607 free(head_ref_name);
2608 if (repo)
2609 got_repo_close(repo);
2610 if (worktree)
2611 got_worktree_close(worktree);
2612 got_ref_list_free(&refs);
2613 return error;
2616 __dead static void
2617 usage_diff(void)
2619 endwin();
2620 fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
2621 getprogname());
2622 exit(1);
2625 static char *
2626 parse_next_line(FILE *f, size_t *len)
2628 char *line;
2629 size_t linelen;
2630 size_t lineno;
2631 const char delim[3] = { '\0', '\0', '\0'};
2633 line = fparseln(f, &linelen, &lineno, delim, 0);
2634 if (len)
2635 *len = linelen;
2636 return line;
2639 static int
2640 match_line(const char *line, regex_t *regex)
2642 regmatch_t regmatch;
2644 return regexec(regex, line, 1, &regmatch, 0) == 0;
2647 struct tog_color *
2648 match_color(struct tog_colors *colors, const char *line)
2650 struct tog_color *tc = NULL;
2652 SIMPLEQ_FOREACH(tc, colors, entry) {
2653 if (match_line(line, &tc->regex))
2654 return tc;
2657 return NULL;
2660 static const struct got_error *
2661 draw_file(struct tog_view *view, FILE *f, int *first_displayed_line,
2662 int *last_displayed_line, int *eof, int max_lines, char *header,
2663 struct tog_colors *colors)
2665 const struct got_error *err;
2666 int nlines = 0, nprinted = 0;
2667 char *line;
2668 struct tog_color *tc;
2669 size_t len;
2670 wchar_t *wline;
2671 int width;
2673 rewind(f);
2674 werase(view->window);
2676 if (header) {
2677 err = format_line(&wline, &width, header, view->ncols, 0);
2678 if (err) {
2679 return err;
2682 if (view_needs_focus_indication(view))
2683 wstandout(view->window);
2684 waddwstr(view->window, wline);
2685 if (view_needs_focus_indication(view))
2686 wstandend(view->window);
2687 if (width <= view->ncols - 1)
2688 waddch(view->window, '\n');
2690 if (max_lines <= 1)
2691 return NULL;
2692 max_lines--;
2695 *eof = 0;
2696 while (nprinted < max_lines) {
2697 line = parse_next_line(f, &len);
2698 if (line == NULL) {
2699 *eof = 1;
2700 break;
2702 if (++nlines < *first_displayed_line) {
2703 free(line);
2704 continue;
2707 err = format_line(&wline, &width, line, view->ncols, 0);
2708 if (err) {
2709 free(line);
2710 return err;
2713 tc = match_color(colors, line);
2714 if (tc)
2715 wattr_on(view->window,
2716 COLOR_PAIR(tc->colorpair), NULL);
2717 waddwstr(view->window, wline);
2718 if (tc)
2719 wattr_off(view->window,
2720 COLOR_PAIR(tc->colorpair), NULL);
2721 if (width <= view->ncols - 1)
2722 waddch(view->window, '\n');
2723 if (++nprinted == 1)
2724 *first_displayed_line = nlines;
2725 free(line);
2726 free(wline);
2727 wline = NULL;
2729 *last_displayed_line = nlines;
2731 view_vborder(view);
2733 if (*eof) {
2734 while (nprinted < view->nlines) {
2735 waddch(view->window, '\n');
2736 nprinted++;
2739 err = format_line(&wline, &width, TOG_EOF_STRING, view->ncols, 0);
2740 if (err) {
2741 return err;
2744 wstandout(view->window);
2745 waddwstr(view->window, wline);
2746 wstandend(view->window);
2749 return NULL;
2752 static char *
2753 get_datestr(time_t *time, char *datebuf)
2755 struct tm mytm, *tm;
2756 char *p, *s;
2758 tm = gmtime_r(time, &mytm);
2759 if (tm == NULL)
2760 return NULL;
2761 s = asctime_r(tm, datebuf);
2762 if (s == NULL)
2763 return NULL;
2764 p = strchr(s, '\n');
2765 if (p)
2766 *p = '\0';
2767 return s;
2770 static const struct got_error *
2771 write_commit_info(struct got_object_id *commit_id,
2772 struct got_reflist_head *refs, struct got_repository *repo, FILE *outfile)
2774 const struct got_error *err = NULL;
2775 char datebuf[26], *datestr;
2776 struct got_commit_object *commit;
2777 char *id_str = NULL, *logmsg = NULL;
2778 time_t committer_time;
2779 const char *author, *committer;
2780 char *refs_str = NULL;
2782 if (refs) {
2783 err = build_refs_str(&refs_str, refs, commit_id, repo);
2784 if (err)
2785 return err;
2788 err = got_object_open_as_commit(&commit, repo, commit_id);
2789 if (err)
2790 return err;
2792 err = got_object_id_str(&id_str, commit_id);
2793 if (err) {
2794 err = got_error_from_errno("got_object_id_str");
2795 goto done;
2798 if (fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2799 refs_str ? refs_str : "", refs_str ? ")" : "") < 0) {
2800 err = got_error_from_errno("fprintf");
2801 goto done;
2803 if (fprintf(outfile, "from: %s\n",
2804 got_object_commit_get_author(commit)) < 0) {
2805 err = got_error_from_errno("fprintf");
2806 goto done;
2808 committer_time = got_object_commit_get_committer_time(commit);
2809 datestr = get_datestr(&committer_time, datebuf);
2810 if (datestr && fprintf(outfile, "date: %s UTC\n", datestr) < 0) {
2811 err = got_error_from_errno("fprintf");
2812 goto done;
2814 author = got_object_commit_get_author(commit);
2815 committer = got_object_commit_get_committer(commit);
2816 if (strcmp(author, committer) != 0 &&
2817 fprintf(outfile, "via: %s\n", committer) < 0) {
2818 err = got_error_from_errno("fprintf");
2819 goto done;
2821 err = got_object_commit_get_logmsg(&logmsg, commit);
2822 if (err)
2823 goto done;
2824 if (fprintf(outfile, "%s\n", logmsg) < 0) {
2825 err = got_error_from_errno("fprintf");
2826 goto done;
2828 done:
2829 free(id_str);
2830 free(logmsg);
2831 free(refs_str);
2832 got_object_commit_close(commit);
2833 return err;
2836 static const struct got_error *
2837 create_diff(struct tog_diff_view_state *s)
2839 const struct got_error *err = NULL;
2840 FILE *f = NULL;
2841 int obj_type;
2843 f = got_opentemp();
2844 if (f == NULL) {
2845 err = got_error_from_errno("got_opentemp");
2846 goto done;
2848 if (s->f && fclose(s->f) != 0) {
2849 err = got_error_from_errno("fclose");
2850 goto done;
2852 s->f = f;
2854 if (s->id1)
2855 err = got_object_get_type(&obj_type, s->repo, s->id1);
2856 else
2857 err = got_object_get_type(&obj_type, s->repo, s->id2);
2858 if (err)
2859 goto done;
2861 switch (obj_type) {
2862 case GOT_OBJ_TYPE_BLOB:
2863 err = got_diff_objects_as_blobs(s->id1, s->id2, NULL, NULL,
2864 s->diff_context, 0, s->repo, f);
2865 break;
2866 case GOT_OBJ_TYPE_TREE:
2867 err = got_diff_objects_as_trees(s->id1, s->id2, "", "",
2868 s->diff_context, 0, s->repo, f);
2869 break;
2870 case GOT_OBJ_TYPE_COMMIT: {
2871 const struct got_object_id_queue *parent_ids;
2872 struct got_object_qid *pid;
2873 struct got_commit_object *commit2;
2875 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
2876 if (err)
2877 break;
2878 /* Show commit info if we're diffing to a parent/root commit. */
2879 if (s->id1 == NULL)
2880 write_commit_info(s->id2, s->refs, s->repo, f);
2881 else {
2882 parent_ids = got_object_commit_get_parent_ids(commit2);
2883 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
2884 if (got_object_id_cmp(s->id1, pid->id) == 0) {
2885 write_commit_info(s->id2, s->refs,
2886 s->repo, f);
2887 break;
2891 got_object_commit_close(commit2);
2893 err = got_diff_objects_as_commits(s->id1, s->id2,
2894 s->diff_context, 0, s->repo, f);
2895 break;
2897 default:
2898 err = got_error(GOT_ERR_OBJ_TYPE);
2899 break;
2901 done:
2902 if (f && fflush(f) != 0 && err == NULL)
2903 err = got_error_from_errno("fflush");
2904 return err;
2907 static void
2908 diff_view_indicate_progress(struct tog_view *view)
2910 mvwaddstr(view->window, 0, 0, "diffing...");
2911 update_panels();
2912 doupdate();
2915 static const struct got_error *
2916 open_diff_view(struct tog_view *view, struct got_object_id *id1,
2917 struct got_object_id *id2, struct tog_view *log_view,
2918 struct got_reflist_head *refs, struct got_repository *repo)
2920 const struct got_error *err;
2922 if (id1 != NULL && id2 != NULL) {
2923 int type1, type2;
2924 err = got_object_get_type(&type1, repo, id1);
2925 if (err)
2926 return err;
2927 err = got_object_get_type(&type2, repo, id2);
2928 if (err)
2929 return err;
2931 if (type1 != type2)
2932 return got_error(GOT_ERR_OBJ_TYPE);
2935 if (id1) {
2936 view->state.diff.id1 = got_object_id_dup(id1);
2937 if (view->state.diff.id1 == NULL)
2938 return got_error_from_errno("got_object_id_dup");
2939 } else
2940 view->state.diff.id1 = NULL;
2942 view->state.diff.id2 = got_object_id_dup(id2);
2943 if (view->state.diff.id2 == NULL) {
2944 free(view->state.diff.id1);
2945 view->state.diff.id1 = NULL;
2946 return got_error_from_errno("got_object_id_dup");
2948 view->state.diff.f = NULL;
2949 view->state.diff.first_displayed_line = 1;
2950 view->state.diff.last_displayed_line = view->nlines;
2951 view->state.diff.diff_context = 3;
2952 view->state.diff.log_view = log_view;
2953 view->state.diff.repo = repo;
2954 view->state.diff.refs = refs;
2955 SIMPLEQ_INIT(&view->state.diff.colors);
2957 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2958 err = add_color(&view->state.diff.colors,
2959 "^-", TOG_COLOR_DIFF_MINUS,
2960 get_color_value("TOG_COLOR_DIFF_MINUS"));
2961 if (err)
2962 return err;
2963 err = add_color(&view->state.diff.colors, "^\\+",
2964 TOG_COLOR_DIFF_PLUS,
2965 get_color_value("TOG_COLOR_DIFF_PLUS"));
2966 if (err) {
2967 free_colors(&view->state.diff.colors);
2968 return err;
2970 err = add_color(&view->state.diff.colors,
2971 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
2972 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
2973 if (err) {
2974 free_colors(&view->state.diff.colors);
2975 return err;
2978 err = add_color(&view->state.diff.colors,
2979 "^(commit|(blob|file) [-+] )", TOG_COLOR_DIFF_META,
2980 get_color_value("TOG_COLOR_DIFF_META"));
2981 if (err) {
2982 free_colors(&view->state.diff.colors);
2983 return err;
2986 err = add_color(&view->state.diff.colors,
2987 "^(from|via): ", TOG_COLOR_AUTHOR,
2988 get_color_value("TOG_COLOR_AUTHOR"));
2989 if (err) {
2990 free_colors(&view->state.diff.colors);
2991 return err;
2994 err = add_color(&view->state.diff.colors,
2995 "^date: ", TOG_COLOR_DATE,
2996 get_color_value("TOG_COLOR_DATE"));
2997 if (err) {
2998 free_colors(&view->state.diff.colors);
2999 return err;
3003 if (log_view && view_is_splitscreen(view))
3004 show_log_view(log_view); /* draw vborder */
3005 diff_view_indicate_progress(view);
3007 err = create_diff(&view->state.diff);
3008 if (err) {
3009 free(view->state.diff.id1);
3010 view->state.diff.id1 = NULL;
3011 free(view->state.diff.id2);
3012 view->state.diff.id2 = NULL;
3013 return err;
3016 view->show = show_diff_view;
3017 view->input = input_diff_view;
3018 view->close = close_diff_view;
3020 return NULL;
3023 static const struct got_error *
3024 close_diff_view(struct tog_view *view)
3026 const struct got_error *err = NULL;
3028 free(view->state.diff.id1);
3029 view->state.diff.id1 = NULL;
3030 free(view->state.diff.id2);
3031 view->state.diff.id2 = NULL;
3032 if (view->state.diff.f && fclose(view->state.diff.f) == EOF)
3033 err = got_error_from_errno("fclose");
3034 free_colors(&view->state.diff.colors);
3035 return err;
3038 static const struct got_error *
3039 show_diff_view(struct tog_view *view)
3041 const struct got_error *err;
3042 struct tog_diff_view_state *s = &view->state.diff;
3043 char *id_str1 = NULL, *id_str2, *header;
3045 if (s->id1) {
3046 err = got_object_id_str(&id_str1, s->id1);
3047 if (err)
3048 return err;
3050 err = got_object_id_str(&id_str2, s->id2);
3051 if (err)
3052 return err;
3054 if (asprintf(&header, "diff %s %s",
3055 id_str1 ? id_str1 : "/dev/null", id_str2) == -1) {
3056 err = got_error_from_errno("asprintf");
3057 free(id_str1);
3058 free(id_str2);
3059 return err;
3061 free(id_str1);
3062 free(id_str2);
3064 return draw_file(view, s->f, &s->first_displayed_line,
3065 &s->last_displayed_line, &s->eof, view->nlines,
3066 header, &s->colors);
3069 static const struct got_error *
3070 set_selected_commit(struct tog_diff_view_state *s,
3071 struct commit_queue_entry *entry)
3073 const struct got_error *err;
3074 const struct got_object_id_queue *parent_ids;
3075 struct got_commit_object *selected_commit;
3076 struct got_object_qid *pid;
3078 free(s->id2);
3079 s->id2 = got_object_id_dup(entry->id);
3080 if (s->id2 == NULL)
3081 return got_error_from_errno("got_object_id_dup");
3083 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3084 if (err)
3085 return err;
3086 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3087 free(s->id1);
3088 pid = SIMPLEQ_FIRST(parent_ids);
3089 s->id1 = pid ? got_object_id_dup(pid->id) : NULL;
3090 got_object_commit_close(selected_commit);
3091 return NULL;
3094 static const struct got_error *
3095 input_diff_view(struct tog_view **new_view, struct tog_view **dead_view,
3096 struct tog_view **focus_view, struct tog_view *view, int ch)
3098 const struct got_error *err = NULL;
3099 struct tog_diff_view_state *s = &view->state.diff;
3100 struct tog_log_view_state *ls;
3101 struct commit_queue_entry *entry;
3102 int i;
3104 switch (ch) {
3105 case 'k':
3106 case KEY_UP:
3107 if (s->first_displayed_line > 1)
3108 s->first_displayed_line--;
3109 break;
3110 case KEY_PPAGE:
3111 case CTRL('b'):
3112 if (s->first_displayed_line == 1)
3113 break;
3114 i = 0;
3115 while (i++ < view->nlines - 1 &&
3116 s->first_displayed_line > 1)
3117 s->first_displayed_line--;
3118 break;
3119 case 'j':
3120 case KEY_DOWN:
3121 if (!s->eof)
3122 s->first_displayed_line++;
3123 break;
3124 case KEY_NPAGE:
3125 case CTRL('f'):
3126 case ' ':
3127 if (s->eof)
3128 break;
3129 i = 0;
3130 while (!s->eof && i++ < view->nlines - 1) {
3131 char *line;
3132 line = parse_next_line(s->f, NULL);
3133 s->first_displayed_line++;
3134 if (line == NULL)
3135 break;
3137 break;
3138 case '[':
3139 if (s->diff_context > 0) {
3140 s->diff_context--;
3141 diff_view_indicate_progress(view);
3142 err = create_diff(s);
3144 break;
3145 case ']':
3146 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
3147 s->diff_context++;
3148 diff_view_indicate_progress(view);
3149 err = create_diff(s);
3151 break;
3152 case '<':
3153 case ',':
3154 if (s->log_view == NULL)
3155 break;
3156 ls = &s->log_view->state.log;
3157 entry = TAILQ_PREV(ls->selected_entry,
3158 commit_queue_head, entry);
3159 if (entry == NULL)
3160 break;
3162 err = input_log_view(NULL, NULL, NULL, s->log_view,
3163 KEY_UP);
3164 if (err)
3165 break;
3167 err = set_selected_commit(s, entry);
3168 if (err)
3169 break;
3171 s->first_displayed_line = 1;
3172 s->last_displayed_line = view->nlines;
3174 diff_view_indicate_progress(view);
3175 err = create_diff(s);
3176 break;
3177 case '>':
3178 case '.':
3179 if (s->log_view == NULL)
3180 break;
3181 ls = &s->log_view->state.log;
3183 if (TAILQ_NEXT(ls->selected_entry, entry) == NULL) {
3184 ls->thread_args.commits_needed++;
3186 /* Display "loading..." in log view. */
3187 show_log_view(s->log_view);
3188 update_panels();
3189 doupdate();
3191 err = trigger_log_thread(1 /* load_all */,
3192 &ls->thread_args.commits_needed,
3193 &ls->thread_args.log_complete,
3194 &ls->thread_args.need_commits);
3195 if (err)
3196 break;
3198 err = input_log_view(NULL, NULL, NULL, s->log_view,
3199 KEY_DOWN);
3200 if (err)
3201 break;
3203 entry = TAILQ_NEXT(ls->selected_entry, entry);
3204 if (entry == NULL)
3205 break;
3207 err = set_selected_commit(s, entry);
3208 if (err)
3209 break;
3211 s->first_displayed_line = 1;
3212 s->last_displayed_line = view->nlines;
3214 diff_view_indicate_progress(view);
3215 err = create_diff(s);
3216 break;
3217 default:
3218 break;
3221 return err;
3224 static const struct got_error *
3225 cmd_diff(int argc, char *argv[])
3227 const struct got_error *error = NULL;
3228 struct got_repository *repo = NULL;
3229 struct got_reflist_head refs;
3230 struct got_object_id *id1 = NULL, *id2 = NULL;
3231 char *repo_path = NULL;
3232 char *id_str1 = NULL, *id_str2 = NULL;
3233 int ch;
3234 struct tog_view *view;
3236 SIMPLEQ_INIT(&refs);
3238 #ifndef PROFILE
3239 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
3240 NULL) == -1)
3241 err(1, "pledge");
3242 #endif
3244 while ((ch = getopt(argc, argv, "")) != -1) {
3245 switch (ch) {
3246 default:
3247 usage_diff();
3248 /* NOTREACHED */
3252 argc -= optind;
3253 argv += optind;
3255 if (argc == 0) {
3256 usage_diff(); /* TODO show local worktree changes */
3257 } else if (argc == 2) {
3258 repo_path = getcwd(NULL, 0);
3259 if (repo_path == NULL)
3260 return got_error_from_errno("getcwd");
3261 id_str1 = argv[0];
3262 id_str2 = argv[1];
3263 } else if (argc == 3) {
3264 repo_path = realpath(argv[0], NULL);
3265 if (repo_path == NULL)
3266 return got_error_from_errno2("realpath", argv[0]);
3267 id_str1 = argv[1];
3268 id_str2 = argv[2];
3269 } else
3270 usage_diff();
3272 init_curses();
3274 error = got_repo_open(&repo, repo_path, NULL);
3275 if (error)
3276 goto done;
3278 error = apply_unveil(got_repo_get_path(repo), NULL);
3279 if (error)
3280 goto done;
3282 error = got_repo_match_object_id_prefix(&id1, id_str1,
3283 GOT_OBJ_TYPE_ANY, repo);
3284 if (error)
3285 goto done;
3287 error = got_repo_match_object_id_prefix(&id2, id_str2,
3288 GOT_OBJ_TYPE_ANY, repo);
3289 if (error)
3290 goto done;
3292 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3293 if (error)
3294 goto done;
3296 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
3297 if (view == NULL) {
3298 error = got_error_from_errno("view_open");
3299 goto done;
3301 error = open_diff_view(view, id1, id2, NULL, &refs, repo);
3302 if (error)
3303 goto done;
3304 error = view_loop(view);
3305 done:
3306 free(repo_path);
3307 if (repo)
3308 got_repo_close(repo);
3309 got_ref_list_free(&refs);
3310 return error;
3313 __dead static void
3314 usage_blame(void)
3316 endwin();
3317 fprintf(stderr, "usage: %s blame [-c commit] [-r repository-path] path\n",
3318 getprogname());
3319 exit(1);
3322 struct tog_blame_line {
3323 int annotated;
3324 struct got_object_id *id;
3327 static const struct got_error *
3328 draw_blame(struct tog_view *view, struct got_object_id *id, FILE *f,
3329 const char *path, struct tog_blame_line *lines, int nlines,
3330 int blame_complete, int selected_line, int *first_displayed_line,
3331 int *last_displayed_line, int *eof, int max_lines,
3332 struct tog_colors *colors)
3334 const struct got_error *err;
3335 int lineno = 0, nprinted = 0;
3336 char *line;
3337 size_t len;
3338 wchar_t *wline;
3339 int width;
3340 struct tog_blame_line *blame_line;
3341 struct got_object_id *prev_id = NULL;
3342 char *id_str;
3343 struct tog_color *tc;
3345 err = got_object_id_str(&id_str, id);
3346 if (err)
3347 return err;
3349 rewind(f);
3350 werase(view->window);
3352 if (asprintf(&line, "commit %s", id_str) == -1) {
3353 err = got_error_from_errno("asprintf");
3354 free(id_str);
3355 return err;
3358 err = format_line(&wline, &width, line, view->ncols, 0);
3359 free(line);
3360 line = NULL;
3361 if (err)
3362 return err;
3363 if (view_needs_focus_indication(view))
3364 wstandout(view->window);
3365 tc = get_color(colors, TOG_COLOR_COMMIT);
3366 if (tc)
3367 wattr_on(view->window,
3368 COLOR_PAIR(tc->colorpair), NULL);
3369 waddwstr(view->window, wline);
3370 if (tc)
3371 wattr_off(view->window,
3372 COLOR_PAIR(tc->colorpair), NULL);
3373 if (view_needs_focus_indication(view))
3374 wstandend(view->window);
3375 free(wline);
3376 wline = NULL;
3377 if (width < view->ncols - 1)
3378 waddch(view->window, '\n');
3380 if (asprintf(&line, "[%d/%d] %s%s",
3381 *first_displayed_line - 1 + selected_line, nlines,
3382 blame_complete ? "" : "annotating... ", path) == -1) {
3383 free(id_str);
3384 return got_error_from_errno("asprintf");
3386 free(id_str);
3387 err = format_line(&wline, &width, line, view->ncols, 0);
3388 free(line);
3389 line = NULL;
3390 if (err)
3391 return err;
3392 waddwstr(view->window, wline);
3393 free(wline);
3394 wline = NULL;
3395 if (width < view->ncols - 1)
3396 waddch(view->window, '\n');
3398 *eof = 0;
3399 while (nprinted < max_lines - 2) {
3400 line = parse_next_line(f, &len);
3401 if (line == NULL) {
3402 *eof = 1;
3403 break;
3405 if (++lineno < *first_displayed_line) {
3406 free(line);
3407 continue;
3410 if (view->ncols <= 9) {
3411 width = 9;
3412 wline = wcsdup(L"");
3413 if (wline == NULL)
3414 err = got_error_from_errno("wcsdup");
3415 } else {
3416 err = format_line(&wline, &width, line,
3417 view->ncols - 9, 9);
3418 width += 9;
3420 if (err) {
3421 free(line);
3422 return err;
3425 if (view->focussed && nprinted == selected_line - 1)
3426 wstandout(view->window);
3428 if (nlines > 0) {
3429 blame_line = &lines[lineno - 1];
3430 if (blame_line->annotated && prev_id &&
3431 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
3432 !(view->focussed &&
3433 nprinted == selected_line - 1)) {
3434 waddstr(view->window, " ");
3435 } else if (blame_line->annotated) {
3436 char *id_str;
3437 err = got_object_id_str(&id_str, blame_line->id);
3438 if (err) {
3439 free(line);
3440 free(wline);
3441 return err;
3443 tc = get_color(colors, TOG_COLOR_COMMIT);
3444 if (tc)
3445 wattr_on(view->window,
3446 COLOR_PAIR(tc->colorpair), NULL);
3447 wprintw(view->window, "%.8s", id_str);
3448 if (tc)
3449 wattr_off(view->window,
3450 COLOR_PAIR(tc->colorpair), NULL);
3451 free(id_str);
3452 prev_id = blame_line->id;
3453 } else {
3454 waddstr(view->window, "........");
3455 prev_id = NULL;
3457 } else {
3458 waddstr(view->window, "........");
3459 prev_id = NULL;
3462 if (view->focussed && nprinted == selected_line - 1)
3463 wstandend(view->window);
3464 waddstr(view->window, " ");
3466 waddwstr(view->window, wline);
3467 if (width <= view->ncols - 1)
3468 waddch(view->window, '\n');
3469 if (++nprinted == 1)
3470 *first_displayed_line = lineno;
3471 free(line);
3472 free(wline);
3473 wline = NULL;
3475 *last_displayed_line = lineno;
3477 view_vborder(view);
3479 return NULL;
3482 static const struct got_error *
3483 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3485 const struct got_error *err = NULL;
3486 struct tog_blame_cb_args *a = arg;
3487 struct tog_blame_line *line;
3488 int errcode;
3490 if (nlines != a->nlines ||
3491 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3492 return got_error(GOT_ERR_RANGE);
3494 errcode = pthread_mutex_lock(&tog_mutex);
3495 if (errcode)
3496 return got_error_set_errno(errcode, "pthread_mutex_lock");
3498 if (*a->quit) { /* user has quit the blame view */
3499 err = got_error(GOT_ERR_ITER_COMPLETED);
3500 goto done;
3503 if (lineno == -1)
3504 goto done; /* no change in this commit */
3506 line = &a->lines[lineno - 1];
3507 if (line->annotated)
3508 goto done;
3510 line->id = got_object_id_dup(id);
3511 if (line->id == NULL) {
3512 err = got_error_from_errno("got_object_id_dup");
3513 goto done;
3515 line->annotated = 1;
3516 done:
3517 errcode = pthread_mutex_unlock(&tog_mutex);
3518 if (errcode)
3519 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3520 return err;
3523 static void *
3524 blame_thread(void *arg)
3526 const struct got_error *err;
3527 struct tog_blame_thread_args *ta = arg;
3528 struct tog_blame_cb_args *a = ta->cb_args;
3529 int errcode;
3531 err = got_blame(ta->path, a->commit_id, ta->repo,
3532 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
3533 if (err && err->code == GOT_ERR_CANCELLED)
3534 err = NULL;
3536 errcode = pthread_mutex_lock(&tog_mutex);
3537 if (errcode)
3538 return (void *)got_error_set_errno(errcode,
3539 "pthread_mutex_lock");
3541 got_repo_close(ta->repo);
3542 ta->repo = NULL;
3543 *ta->complete = 1;
3545 errcode = pthread_mutex_unlock(&tog_mutex);
3546 if (errcode && err == NULL)
3547 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
3549 return (void *)err;
3552 static struct got_object_id *
3553 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
3554 int first_displayed_line, int selected_line)
3556 struct tog_blame_line *line;
3558 if (nlines <= 0)
3559 return NULL;
3561 line = &lines[first_displayed_line - 1 + selected_line - 1];
3562 if (!line->annotated)
3563 return NULL;
3565 return line->id;
3568 static const struct got_error *
3569 stop_blame(struct tog_blame *blame)
3571 const struct got_error *err = NULL;
3572 int i;
3574 if (blame->thread) {
3575 int errcode;
3576 errcode = pthread_mutex_unlock(&tog_mutex);
3577 if (errcode)
3578 return got_error_set_errno(errcode,
3579 "pthread_mutex_unlock");
3580 errcode = pthread_join(blame->thread, (void **)&err);
3581 if (errcode)
3582 return got_error_set_errno(errcode, "pthread_join");
3583 errcode = pthread_mutex_lock(&tog_mutex);
3584 if (errcode)
3585 return got_error_set_errno(errcode,
3586 "pthread_mutex_lock");
3587 if (err && err->code == GOT_ERR_ITER_COMPLETED)
3588 err = NULL;
3589 blame->thread = NULL;
3591 if (blame->thread_args.repo) {
3592 got_repo_close(blame->thread_args.repo);
3593 blame->thread_args.repo = NULL;
3595 if (blame->f) {
3596 if (fclose(blame->f) != 0 && err == NULL)
3597 err = got_error_from_errno("fclose");
3598 blame->f = NULL;
3600 if (blame->lines) {
3601 for (i = 0; i < blame->nlines; i++)
3602 free(blame->lines[i].id);
3603 free(blame->lines);
3604 blame->lines = NULL;
3606 free(blame->cb_args.commit_id);
3607 blame->cb_args.commit_id = NULL;
3609 return err;
3612 static const struct got_error *
3613 cancel_blame_view(void *arg)
3615 const struct got_error *err = NULL;
3616 int *done = arg;
3617 int errcode;
3619 errcode = pthread_mutex_lock(&tog_mutex);
3620 if (errcode)
3621 return got_error_set_errno(errcode,
3622 "pthread_mutex_unlock");
3624 if (*done)
3625 err = got_error(GOT_ERR_CANCELLED);
3627 errcode = pthread_mutex_unlock(&tog_mutex);
3628 if (errcode)
3629 return got_error_set_errno(errcode,
3630 "pthread_mutex_lock");
3632 return err;
3635 static const struct got_error *
3636 run_blame(struct tog_blame *blame, struct tog_view *view, int *blame_complete,
3637 int *first_displayed_line, int *last_displayed_line, int *selected_line,
3638 int *done, int *eof, const char *path, struct got_object_id *commit_id,
3639 struct got_repository *repo)
3641 const struct got_error *err = NULL;
3642 struct got_blob_object *blob = NULL;
3643 struct got_repository *thread_repo = NULL;
3644 struct got_object_id *obj_id = NULL;
3645 int obj_type;
3647 err = got_object_id_by_path(&obj_id, repo, commit_id, path);
3648 if (err)
3649 return err;
3650 if (obj_id == NULL)
3651 return got_error(GOT_ERR_NO_OBJ);
3653 err = got_object_get_type(&obj_type, repo, obj_id);
3654 if (err)
3655 goto done;
3657 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3658 err = got_error(GOT_ERR_OBJ_TYPE);
3659 goto done;
3662 err = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3663 if (err)
3664 goto done;
3665 blame->f = got_opentemp();
3666 if (blame->f == NULL) {
3667 err = got_error_from_errno("got_opentemp");
3668 goto done;
3670 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
3671 &blame->line_offsets, blame->f, blob);
3672 if (err || blame->nlines == 0)
3673 goto done;
3675 /* Don't include \n at EOF in the blame line count. */
3676 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
3677 blame->nlines--;
3679 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
3680 if (blame->lines == NULL) {
3681 err = got_error_from_errno("calloc");
3682 goto done;
3685 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL);
3686 if (err)
3687 goto done;
3689 blame->cb_args.view = view;
3690 blame->cb_args.lines = blame->lines;
3691 blame->cb_args.nlines = blame->nlines;
3692 blame->cb_args.commit_id = got_object_id_dup(commit_id);
3693 if (blame->cb_args.commit_id == NULL) {
3694 err = got_error_from_errno("got_object_id_dup");
3695 goto done;
3697 blame->cb_args.quit = done;
3699 blame->thread_args.path = path;
3700 blame->thread_args.repo = thread_repo;
3701 blame->thread_args.cb_args = &blame->cb_args;
3702 blame->thread_args.complete = blame_complete;
3703 blame->thread_args.cancel_cb = cancel_blame_view;
3704 blame->thread_args.cancel_arg = done;
3705 *blame_complete = 0;
3707 done:
3708 if (blob)
3709 got_object_blob_close(blob);
3710 free(obj_id);
3711 if (err)
3712 stop_blame(blame);
3713 return err;
3716 static const struct got_error *
3717 open_blame_view(struct tog_view *view, char *path,
3718 struct got_object_id *commit_id, struct got_reflist_head *refs,
3719 struct got_repository *repo)
3721 const struct got_error *err = NULL;
3722 struct tog_blame_view_state *s = &view->state.blame;
3724 SIMPLEQ_INIT(&s->blamed_commits);
3726 s->path = strdup(path);
3727 if (s->path == NULL)
3728 return got_error_from_errno("strdup");
3730 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
3731 if (err) {
3732 free(s->path);
3733 return err;
3736 SIMPLEQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
3737 s->first_displayed_line = 1;
3738 s->last_displayed_line = view->nlines;
3739 s->selected_line = 1;
3740 s->blame_complete = 0;
3741 s->repo = repo;
3742 s->refs = refs;
3743 s->commit_id = commit_id;
3744 memset(&s->blame, 0, sizeof(s->blame));
3746 SIMPLEQ_INIT(&s->colors);
3747 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3748 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
3749 get_color_value("TOG_COLOR_COMMIT"));
3750 if (err)
3751 return err;
3754 view->show = show_blame_view;
3755 view->input = input_blame_view;
3756 view->close = close_blame_view;
3757 view->search_start = search_start_blame_view;
3758 view->search_next = search_next_blame_view;
3760 return run_blame(&s->blame, view, &s->blame_complete,
3761 &s->first_displayed_line, &s->last_displayed_line,
3762 &s->selected_line, &s->done, &s->eof, s->path,
3763 s->blamed_commit->id, s->repo);
3766 static const struct got_error *
3767 close_blame_view(struct tog_view *view)
3769 const struct got_error *err = NULL;
3770 struct tog_blame_view_state *s = &view->state.blame;
3772 if (s->blame.thread)
3773 err = stop_blame(&s->blame);
3775 while (!SIMPLEQ_EMPTY(&s->blamed_commits)) {
3776 struct got_object_qid *blamed_commit;
3777 blamed_commit = SIMPLEQ_FIRST(&s->blamed_commits);
3778 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
3779 got_object_qid_free(blamed_commit);
3782 free(s->path);
3783 free_colors(&s->colors);
3785 return err;
3788 static const struct got_error *
3789 search_start_blame_view(struct tog_view *view)
3791 struct tog_blame_view_state *s = &view->state.blame;
3793 s->matched_line = 0;
3794 return NULL;
3797 static const struct got_error *
3798 search_next_blame_view(struct tog_view *view)
3800 struct tog_blame_view_state *s = &view->state.blame;
3801 int lineno;
3803 if (!view->searching) {
3804 view->search_next_done = 1;
3805 return NULL;
3808 if (s->matched_line) {
3809 if (view->searching == TOG_SEARCH_FORWARD)
3810 lineno = s->matched_line + 1;
3811 else
3812 lineno = s->matched_line - 1;
3813 } else {
3814 if (view->searching == TOG_SEARCH_FORWARD)
3815 lineno = 1;
3816 else
3817 lineno = s->blame.nlines;
3820 while (1) {
3821 char *line = NULL;
3822 off_t offset;
3823 size_t len;
3825 if (lineno <= 0 || lineno > s->blame.nlines) {
3826 if (s->matched_line == 0) {
3827 view->search_next_done = 1;
3828 free(line);
3829 break;
3832 if (view->searching == TOG_SEARCH_FORWARD)
3833 lineno = 1;
3834 else
3835 lineno = s->blame.nlines;
3838 offset = s->blame.line_offsets[lineno - 1];
3839 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
3840 free(line);
3841 return got_error_from_errno("fseeko");
3843 free(line);
3844 line = parse_next_line(s->blame.f, &len);
3845 if (line && match_line(line, &view->regex)) {
3846 view->search_next_done = 1;
3847 s->matched_line = lineno;
3848 free(line);
3849 break;
3851 free(line);
3852 if (view->searching == TOG_SEARCH_FORWARD)
3853 lineno++;
3854 else
3855 lineno--;
3858 if (s->matched_line) {
3859 s->first_displayed_line = s->matched_line;
3860 s->selected_line = 1;
3863 return NULL;
3866 static const struct got_error *
3867 show_blame_view(struct tog_view *view)
3869 const struct got_error *err = NULL;
3870 struct tog_blame_view_state *s = &view->state.blame;
3871 int errcode;
3873 if (s->blame.thread == NULL) {
3874 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
3875 &s->blame.thread_args);
3876 if (errcode)
3877 return got_error_set_errno(errcode, "pthread_create");
3879 halfdelay(1); /* fast refresh while annotating */
3882 if (s->blame_complete)
3883 halfdelay(10); /* disable fast refresh */
3885 err = draw_blame(view, s->blamed_commit->id, s->blame.f,
3886 s->path, s->blame.lines, s->blame.nlines, s->blame_complete,
3887 s->selected_line, &s->first_displayed_line,
3888 &s->last_displayed_line, &s->eof, view->nlines, &s->colors);
3890 view_vborder(view);
3891 return err;
3894 static const struct got_error *
3895 input_blame_view(struct tog_view **new_view, struct tog_view **dead_view,
3896 struct tog_view **focus_view, struct tog_view *view, int ch)
3898 const struct got_error *err = NULL, *thread_err = NULL;
3899 struct tog_view *diff_view;
3900 struct tog_blame_view_state *s = &view->state.blame;
3901 int begin_x = 0;
3903 switch (ch) {
3904 case 'q':
3905 s->done = 1;
3906 break;
3907 case 'k':
3908 case KEY_UP:
3909 if (s->selected_line > 1)
3910 s->selected_line--;
3911 else if (s->selected_line == 1 &&
3912 s->first_displayed_line > 1)
3913 s->first_displayed_line--;
3914 break;
3915 case KEY_PPAGE:
3916 if (s->first_displayed_line == 1) {
3917 s->selected_line = 1;
3918 break;
3920 if (s->first_displayed_line > view->nlines - 2)
3921 s->first_displayed_line -=
3922 (view->nlines - 2);
3923 else
3924 s->first_displayed_line = 1;
3925 break;
3926 case 'j':
3927 case KEY_DOWN:
3928 if (s->selected_line < view->nlines - 2 &&
3929 s->first_displayed_line +
3930 s->selected_line <= s->blame.nlines)
3931 s->selected_line++;
3932 else if (s->last_displayed_line <
3933 s->blame.nlines)
3934 s->first_displayed_line++;
3935 break;
3936 case 'b':
3937 case 'p': {
3938 struct got_object_id *id = NULL;
3939 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
3940 s->first_displayed_line, s->selected_line);
3941 if (id == NULL)
3942 break;
3943 if (ch == 'p') {
3944 struct got_commit_object *commit;
3945 struct got_object_qid *pid;
3946 struct got_object_id *blob_id = NULL;
3947 int obj_type;
3948 err = got_object_open_as_commit(&commit,
3949 s->repo, id);
3950 if (err)
3951 break;
3952 pid = SIMPLEQ_FIRST(
3953 got_object_commit_get_parent_ids(commit));
3954 if (pid == NULL) {
3955 got_object_commit_close(commit);
3956 break;
3958 /* Check if path history ends here. */
3959 err = got_object_id_by_path(&blob_id, s->repo,
3960 pid->id, s->path);
3961 if (err) {
3962 if (err->code == GOT_ERR_NO_TREE_ENTRY)
3963 err = NULL;
3964 got_object_commit_close(commit);
3965 break;
3967 err = got_object_get_type(&obj_type, s->repo,
3968 blob_id);
3969 free(blob_id);
3970 /* Can't blame non-blob type objects. */
3971 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3972 got_object_commit_close(commit);
3973 break;
3975 err = got_object_qid_alloc(&s->blamed_commit,
3976 pid->id);
3977 got_object_commit_close(commit);
3978 } else {
3979 if (got_object_id_cmp(id,
3980 s->blamed_commit->id) == 0)
3981 break;
3982 err = got_object_qid_alloc(&s->blamed_commit,
3983 id);
3985 if (err)
3986 break;
3987 s->done = 1;
3988 thread_err = stop_blame(&s->blame);
3989 s->done = 0;
3990 if (thread_err)
3991 break;
3992 SIMPLEQ_INSERT_HEAD(&s->blamed_commits,
3993 s->blamed_commit, entry);
3994 err = run_blame(&s->blame, view, &s->blame_complete,
3995 &s->first_displayed_line, &s->last_displayed_line,
3996 &s->selected_line, &s->done, &s->eof,
3997 s->path, s->blamed_commit->id, s->repo);
3998 if (err)
3999 break;
4000 break;
4002 case 'B': {
4003 struct got_object_qid *first;
4004 first = SIMPLEQ_FIRST(&s->blamed_commits);
4005 if (!got_object_id_cmp(first->id, s->commit_id))
4006 break;
4007 s->done = 1;
4008 thread_err = stop_blame(&s->blame);
4009 s->done = 0;
4010 if (thread_err)
4011 break;
4012 SIMPLEQ_REMOVE_HEAD(&s->blamed_commits, entry);
4013 got_object_qid_free(s->blamed_commit);
4014 s->blamed_commit =
4015 SIMPLEQ_FIRST(&s->blamed_commits);
4016 err = run_blame(&s->blame, view, &s->blame_complete,
4017 &s->first_displayed_line, &s->last_displayed_line,
4018 &s->selected_line, &s->done, &s->eof, s->path,
4019 s->blamed_commit->id, s->repo);
4020 if (err)
4021 break;
4022 break;
4024 case KEY_ENTER:
4025 case '\r': {
4026 struct got_object_id *id = NULL;
4027 struct got_object_qid *pid;
4028 struct got_commit_object *commit = NULL;
4029 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
4030 s->first_displayed_line, s->selected_line);
4031 if (id == NULL)
4032 break;
4033 err = got_object_open_as_commit(&commit, s->repo, id);
4034 if (err)
4035 break;
4036 pid = SIMPLEQ_FIRST(
4037 got_object_commit_get_parent_ids(commit));
4038 if (view_is_parent_view(view))
4039 begin_x = view_split_begin_x(view->begin_x);
4040 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
4041 if (diff_view == NULL) {
4042 got_object_commit_close(commit);
4043 err = got_error_from_errno("view_open");
4044 break;
4046 err = open_diff_view(diff_view, pid ? pid->id : NULL,
4047 id, NULL, s->refs, s->repo);
4048 got_object_commit_close(commit);
4049 if (err) {
4050 view_close(diff_view);
4051 break;
4053 if (view_is_parent_view(view)) {
4054 err = view_close_child(view);
4055 if (err)
4056 break;
4057 err = view_set_child(view, diff_view);
4058 if (err) {
4059 view_close(diff_view);
4060 break;
4062 *focus_view = diff_view;
4063 view->child_focussed = 1;
4064 } else
4065 *new_view = diff_view;
4066 if (err)
4067 break;
4068 break;
4070 case KEY_NPAGE:
4071 case ' ':
4072 if (s->last_displayed_line >= s->blame.nlines &&
4073 s->selected_line >= MIN(s->blame.nlines,
4074 view->nlines - 2)) {
4075 break;
4077 if (s->last_displayed_line >= s->blame.nlines &&
4078 s->selected_line < view->nlines - 2) {
4079 s->selected_line = MIN(s->blame.nlines,
4080 view->nlines - 2);
4081 break;
4083 if (s->last_displayed_line + view->nlines - 2
4084 <= s->blame.nlines)
4085 s->first_displayed_line +=
4086 view->nlines - 2;
4087 else
4088 s->first_displayed_line =
4089 s->blame.nlines -
4090 (view->nlines - 3);
4091 break;
4092 case KEY_RESIZE:
4093 if (s->selected_line > view->nlines - 2) {
4094 s->selected_line = MIN(s->blame.nlines,
4095 view->nlines - 2);
4097 break;
4098 default:
4099 break;
4101 return thread_err ? thread_err : err;
4104 static const struct got_error *
4105 cmd_blame(int argc, char *argv[])
4107 const struct got_error *error;
4108 struct got_repository *repo = NULL;
4109 struct got_reflist_head refs;
4110 struct got_worktree *worktree = NULL;
4111 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4112 struct got_object_id *commit_id = NULL;
4113 char *commit_id_str = NULL;
4114 int ch;
4115 struct tog_view *view;
4117 SIMPLEQ_INIT(&refs);
4119 #ifndef PROFILE
4120 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4121 NULL) == -1)
4122 err(1, "pledge");
4123 #endif
4125 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4126 switch (ch) {
4127 case 'c':
4128 commit_id_str = optarg;
4129 break;
4130 case 'r':
4131 repo_path = realpath(optarg, NULL);
4132 if (repo_path == NULL)
4133 return got_error_from_errno2("realpath",
4134 optarg);
4135 break;
4136 default:
4137 usage_blame();
4138 /* NOTREACHED */
4142 argc -= optind;
4143 argv += optind;
4145 if (argc == 1)
4146 path = argv[0];
4147 else
4148 usage_blame();
4150 cwd = getcwd(NULL, 0);
4151 if (cwd == NULL) {
4152 error = got_error_from_errno("getcwd");
4153 goto done;
4155 if (repo_path == NULL) {
4156 error = got_worktree_open(&worktree, cwd);
4157 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4158 goto done;
4159 else
4160 error = NULL;
4161 if (worktree) {
4162 repo_path =
4163 strdup(got_worktree_get_repo_path(worktree));
4164 if (repo_path == NULL)
4165 error = got_error_from_errno("strdup");
4166 if (error)
4167 goto done;
4168 } else {
4169 repo_path = strdup(cwd);
4170 if (repo_path == NULL) {
4171 error = got_error_from_errno("strdup");
4172 goto done;
4177 init_curses();
4179 error = got_repo_open(&repo, repo_path, NULL);
4180 if (error != NULL)
4181 goto done;
4183 error = apply_unveil(got_repo_get_path(repo), NULL);
4184 if (error)
4185 goto done;
4187 if (worktree) {
4188 const char *prefix = got_worktree_get_path_prefix(worktree);
4189 char *p, *worktree_subdir = cwd +
4190 strlen(got_worktree_get_root_path(worktree));
4191 if (asprintf(&p, "%s%s%s%s%s",
4192 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4193 worktree_subdir, worktree_subdir[0] ? "/" : "",
4194 path) == -1) {
4195 error = got_error_from_errno("asprintf");
4196 goto done;
4198 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4199 free(p);
4200 } else {
4201 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4203 if (error)
4204 goto done;
4206 if (commit_id_str == NULL) {
4207 struct got_reference *head_ref;
4208 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4209 if (error != NULL)
4210 goto done;
4211 error = got_ref_resolve(&commit_id, repo, head_ref);
4212 got_ref_close(head_ref);
4213 } else {
4214 error = get_head_commit_id(&commit_id, commit_id_str, repo);
4215 if (error) {
4216 if (error->code != GOT_ERR_NOT_REF)
4217 goto done;
4218 error = got_repo_match_object_id_prefix(&commit_id,
4219 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
4222 if (error != NULL)
4223 goto done;
4225 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4226 if (error)
4227 goto done;
4229 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
4230 if (view == NULL) {
4231 error = got_error_from_errno("view_open");
4232 goto done;
4234 error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
4235 if (error)
4236 goto done;
4237 if (worktree) {
4238 /* Release work tree lock. */
4239 got_worktree_close(worktree);
4240 worktree = NULL;
4242 error = view_loop(view);
4243 done:
4244 free(repo_path);
4245 free(cwd);
4246 free(commit_id);
4247 if (worktree)
4248 got_worktree_close(worktree);
4249 if (repo)
4250 got_repo_close(repo);
4251 got_ref_list_free(&refs);
4252 return error;
4255 static const struct got_error *
4256 draw_tree_entries(struct tog_view *view,
4257 struct got_tree_entry **first_displayed_entry,
4258 struct got_tree_entry **last_displayed_entry,
4259 struct got_tree_entry **selected_entry, int *ndisplayed,
4260 const char *label, int show_ids, const char *parent_path,
4261 const struct got_tree_entries *entries, int selected, int limit,
4262 int isroot, struct tog_colors *colors)
4264 const struct got_error *err = NULL;
4265 struct got_tree_entry *te;
4266 wchar_t *wline;
4267 struct tog_color *tc;
4268 int width, n;
4270 *ndisplayed = 0;
4272 werase(view->window);
4274 if (limit == 0)
4275 return NULL;
4277 err = format_line(&wline, &width, label, view->ncols, 0);
4278 if (err)
4279 return err;
4280 if (view_needs_focus_indication(view))
4281 wstandout(view->window);
4282 tc = get_color(colors, TOG_COLOR_COMMIT);
4283 if (tc)
4284 wattr_on(view->window,
4285 COLOR_PAIR(tc->colorpair), NULL);
4286 waddwstr(view->window, wline);
4287 if (tc)
4288 wattr_off(view->window,
4289 COLOR_PAIR(tc->colorpair), NULL);
4290 if (view_needs_focus_indication(view))
4291 wstandend(view->window);
4292 free(wline);
4293 wline = NULL;
4294 if (width < view->ncols - 1)
4295 waddch(view->window, '\n');
4296 if (--limit <= 0)
4297 return NULL;
4298 err = format_line(&wline, &width, parent_path, view->ncols, 0);
4299 if (err)
4300 return err;
4301 waddwstr(view->window, wline);
4302 free(wline);
4303 wline = NULL;
4304 if (width < view->ncols - 1)
4305 waddch(view->window, '\n');
4306 if (--limit <= 0)
4307 return NULL;
4308 waddch(view->window, '\n');
4309 if (--limit <= 0)
4310 return NULL;
4312 te = SIMPLEQ_FIRST(&entries->head);
4313 if (*first_displayed_entry == NULL) {
4314 if (selected == 0) {
4315 if (view->focussed)
4316 wstandout(view->window);
4317 *selected_entry = NULL;
4319 waddstr(view->window, " ..\n"); /* parent directory */
4320 if (selected == 0 && view->focussed)
4321 wstandend(view->window);
4322 (*ndisplayed)++;
4323 if (--limit <= 0)
4324 return NULL;
4325 n = 1;
4326 } else {
4327 n = 0;
4328 while (te != *first_displayed_entry)
4329 te = SIMPLEQ_NEXT(te, entry);
4332 while (te) {
4333 char *line = NULL, *id_str = NULL;
4334 const char *modestr = "";
4336 if (show_ids) {
4337 err = got_object_id_str(&id_str, te->id);
4338 if (err)
4339 return got_error_from_errno(
4340 "got_object_id_str");
4342 if (got_object_tree_entry_is_submodule(te))
4343 modestr = "$";
4344 else if (S_ISLNK(te->mode))
4345 modestr = "@";
4346 else if (S_ISDIR(te->mode))
4347 modestr = "/";
4348 else if (te->mode & S_IXUSR)
4349 modestr = "*";
4350 if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
4351 te->name, modestr) == -1) {
4352 free(id_str);
4353 return got_error_from_errno("asprintf");
4355 free(id_str);
4356 err = format_line(&wline, &width, line, view->ncols, 0);
4357 if (err) {
4358 free(line);
4359 break;
4361 if (n == selected) {
4362 if (view->focussed)
4363 wstandout(view->window);
4364 *selected_entry = te;
4366 tc = match_color(colors, line);
4367 if (tc)
4368 wattr_on(view->window,
4369 COLOR_PAIR(tc->colorpair), NULL);
4370 waddwstr(view->window, wline);
4371 if (tc)
4372 wattr_off(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 if (width < view->ncols - 1)
4375 waddch(view->window, '\n');
4376 if (n == selected && view->focussed)
4377 wstandend(view->window);
4378 free(line);
4379 free(wline);
4380 wline = NULL;
4381 n++;
4382 (*ndisplayed)++;
4383 *last_displayed_entry = te;
4384 if (--limit <= 0)
4385 break;
4386 te = SIMPLEQ_NEXT(te, entry);
4389 return err;
4392 static void
4393 tree_scroll_up(struct tog_view *view,
4394 struct got_tree_entry **first_displayed_entry, int maxscroll,
4395 const struct got_tree_entries *entries, int isroot)
4397 struct got_tree_entry *te, *prev;
4398 int i;
4400 if (*first_displayed_entry == NULL)
4401 return;
4403 te = SIMPLEQ_FIRST(&entries->head);
4404 if (*first_displayed_entry == te) {
4405 if (!isroot)
4406 *first_displayed_entry = NULL;
4407 return;
4410 /* XXX this is stupid... switch to TAILQ? */
4411 for (i = 0; i < maxscroll; i++) {
4412 while (te != *first_displayed_entry) {
4413 prev = te;
4414 te = SIMPLEQ_NEXT(te, entry);
4416 *first_displayed_entry = prev;
4417 te = SIMPLEQ_FIRST(&entries->head);
4419 if (!isroot && te == SIMPLEQ_FIRST(&entries->head) && i < maxscroll)
4420 *first_displayed_entry = NULL;
4423 static int
4424 tree_scroll_down(struct got_tree_entry **first_displayed_entry, int maxscroll,
4425 struct got_tree_entry *last_displayed_entry,
4426 const struct got_tree_entries *entries)
4428 struct got_tree_entry *next, *last;
4429 int n = 0;
4431 if (*first_displayed_entry)
4432 next = SIMPLEQ_NEXT(*first_displayed_entry, entry);
4433 else
4434 next = SIMPLEQ_FIRST(&entries->head);
4435 last = last_displayed_entry;
4436 while (next && last && n++ < maxscroll) {
4437 last = SIMPLEQ_NEXT(last, entry);
4438 if (last) {
4439 *first_displayed_entry = next;
4440 next = SIMPLEQ_NEXT(next, entry);
4443 return n;
4446 static const struct got_error *
4447 tree_entry_path(char **path, struct tog_parent_trees *parents,
4448 struct got_tree_entry *te)
4450 const struct got_error *err = NULL;
4451 struct tog_parent_tree *pt;
4452 size_t len = 2; /* for leading slash and NUL */
4454 TAILQ_FOREACH(pt, parents, entry)
4455 len += strlen(pt->selected_entry->name) + 1 /* slash */;
4456 if (te)
4457 len += strlen(te->name);
4459 *path = calloc(1, len);
4460 if (path == NULL)
4461 return got_error_from_errno("calloc");
4463 (*path)[0] = '/';
4464 pt = TAILQ_LAST(parents, tog_parent_trees);
4465 while (pt) {
4466 if (strlcat(*path, pt->selected_entry->name, len) >= len) {
4467 err = got_error(GOT_ERR_NO_SPACE);
4468 goto done;
4470 if (strlcat(*path, "/", len) >= len) {
4471 err = got_error(GOT_ERR_NO_SPACE);
4472 goto done;
4474 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
4476 if (te) {
4477 if (strlcat(*path, te->name, len) >= len) {
4478 err = got_error(GOT_ERR_NO_SPACE);
4479 goto done;
4482 done:
4483 if (err) {
4484 free(*path);
4485 *path = NULL;
4487 return err;
4490 static const struct got_error *
4491 blame_tree_entry(struct tog_view **new_view, int begin_x,
4492 struct got_tree_entry *te, struct tog_parent_trees *parents,
4493 struct got_object_id *commit_id, struct got_reflist_head *refs,
4494 struct got_repository *repo)
4496 const struct got_error *err = NULL;
4497 char *path;
4498 struct tog_view *blame_view;
4500 *new_view = NULL;
4502 err = tree_entry_path(&path, parents, te);
4503 if (err)
4504 return err;
4506 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
4507 if (blame_view == NULL) {
4508 err = got_error_from_errno("view_open");
4509 goto done;
4512 err = open_blame_view(blame_view, path, commit_id, refs, repo);
4513 if (err) {
4514 if (err->code == GOT_ERR_CANCELLED)
4515 err = NULL;
4516 view_close(blame_view);
4517 } else
4518 *new_view = blame_view;
4519 done:
4520 free(path);
4521 return err;
4524 static const struct got_error *
4525 log_tree_entry(struct tog_view **new_view, int begin_x,
4526 struct got_tree_entry *te, struct tog_parent_trees *parents,
4527 struct got_object_id *commit_id, struct got_reflist_head *refs,
4528 struct got_repository *repo)
4530 struct tog_view *log_view;
4531 const struct got_error *err = NULL;
4532 char *path;
4534 *new_view = NULL;
4536 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
4537 if (log_view == NULL)
4538 return got_error_from_errno("view_open");
4540 err = tree_entry_path(&path, parents, te);
4541 if (err)
4542 return err;
4544 err = open_log_view(log_view, commit_id, refs, repo, NULL, path, 0);
4545 if (err)
4546 view_close(log_view);
4547 else
4548 *new_view = log_view;
4549 free(path);
4550 return err;
4553 static const struct got_error *
4554 open_tree_view(struct tog_view *view, struct got_tree_object *root,
4555 struct got_object_id *commit_id, struct got_reflist_head *refs,
4556 struct got_repository *repo)
4558 const struct got_error *err = NULL;
4559 char *commit_id_str = NULL;
4560 struct tog_tree_view_state *s = &view->state.tree;
4562 TAILQ_INIT(&s->parents);
4564 err = got_object_id_str(&commit_id_str, commit_id);
4565 if (err != NULL)
4566 goto done;
4568 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
4569 err = got_error_from_errno("asprintf");
4570 goto done;
4573 s->root = s->tree = root;
4574 s->entries = got_object_tree_get_entries(root);
4575 s->first_displayed_entry = SIMPLEQ_FIRST(&s->entries->head);
4576 s->selected_entry = SIMPLEQ_FIRST(&s->entries->head);
4577 s->commit_id = got_object_id_dup(commit_id);
4578 if (s->commit_id == NULL) {
4579 err = got_error_from_errno("got_object_id_dup");
4580 goto done;
4582 s->refs = refs;
4583 s->repo = repo;
4585 SIMPLEQ_INIT(&s->colors);
4587 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4588 err = add_color(&s->colors, "\\$$",
4589 TOG_COLOR_TREE_SUBMODULE,
4590 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
4591 if (err)
4592 goto done;
4593 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
4594 get_color_value("TOG_COLOR_TREE_SYMLINK"));
4595 if (err) {
4596 free_colors(&s->colors);
4597 goto done;
4599 err = add_color(&s->colors, "/$",
4600 TOG_COLOR_TREE_DIRECTORY,
4601 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
4602 if (err) {
4603 free_colors(&s->colors);
4604 goto done;
4607 err = add_color(&s->colors, "\\*$",
4608 TOG_COLOR_TREE_EXECUTABLE,
4609 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
4610 if (err) {
4611 free_colors(&s->colors);
4612 goto done;
4615 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
4616 get_color_value("TOG_COLOR_COMMIT"));
4617 if (err) {
4618 free_colors(&s->colors);
4619 goto done;
4623 view->show = show_tree_view;
4624 view->input = input_tree_view;
4625 view->close = close_tree_view;
4626 view->search_start = search_start_tree_view;
4627 view->search_next = search_next_tree_view;
4628 done:
4629 free(commit_id_str);
4630 if (err) {
4631 free(s->tree_label);
4632 s->tree_label = NULL;
4634 return err;
4637 static const struct got_error *
4638 close_tree_view(struct tog_view *view)
4640 struct tog_tree_view_state *s = &view->state.tree;
4642 free_colors(&s->colors);
4643 free(s->tree_label);
4644 s->tree_label = NULL;
4645 free(s->commit_id);
4646 s->commit_id = NULL;
4647 while (!TAILQ_EMPTY(&s->parents)) {
4648 struct tog_parent_tree *parent;
4649 parent = TAILQ_FIRST(&s->parents);
4650 TAILQ_REMOVE(&s->parents, parent, entry);
4651 free(parent);
4654 if (s->tree != s->root)
4655 got_object_tree_close(s->tree);
4656 got_object_tree_close(s->root);
4658 return NULL;
4661 static const struct got_error *
4662 search_start_tree_view(struct tog_view *view)
4664 struct tog_tree_view_state *s = &view->state.tree;
4666 s->matched_entry = NULL;
4667 return NULL;
4670 static int
4671 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
4673 regmatch_t regmatch;
4675 return regexec(regex, te->name, 1, &regmatch, 0) == 0;
4678 static const struct got_error *
4679 search_next_tree_view(struct tog_view *view)
4681 struct tog_tree_view_state *s = &view->state.tree;
4682 struct got_tree_entry *entry = NULL, *te;
4684 if (!view->searching) {
4685 view->search_next_done = 1;
4686 return NULL;
4689 if (s->matched_entry) {
4690 if (view->searching == TOG_SEARCH_FORWARD) {
4691 if (s->selected_entry)
4692 entry = SIMPLEQ_NEXT(s->selected_entry, entry);
4693 else
4694 entry = SIMPLEQ_FIRST(&s->entries->head);
4696 else {
4697 if (s->selected_entry == NULL) {
4698 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4699 entry = te;
4700 } else {
4701 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4702 entry = te;
4703 if (SIMPLEQ_NEXT(te, entry) ==
4704 s->selected_entry)
4705 break;
4709 } else {
4710 if (view->searching == TOG_SEARCH_FORWARD)
4711 entry = SIMPLEQ_FIRST(&s->entries->head);
4712 else {
4713 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4714 entry = te;
4718 while (1) {
4719 if (entry == NULL) {
4720 if (s->matched_entry == NULL) {
4721 view->search_next_done = 1;
4722 return NULL;
4724 if (view->searching == TOG_SEARCH_FORWARD)
4725 entry = SIMPLEQ_FIRST(&s->entries->head);
4726 else {
4727 SIMPLEQ_FOREACH(te, &s->entries->head, entry)
4728 entry = te;
4732 if (match_tree_entry(entry, &view->regex)) {
4733 view->search_next_done = 1;
4734 s->matched_entry = entry;
4735 break;
4738 if (view->searching == TOG_SEARCH_FORWARD)
4739 entry = SIMPLEQ_NEXT(entry, entry);
4740 else {
4741 if (SIMPLEQ_FIRST(&s->entries->head) == entry)
4742 entry = NULL;
4743 else {
4744 SIMPLEQ_FOREACH(te, &s->entries->head, entry) {
4745 if (SIMPLEQ_NEXT(te, entry) == entry) {
4746 entry = te;
4747 break;
4754 if (s->matched_entry) {
4755 s->first_displayed_entry = s->matched_entry;
4756 s->selected = 0;
4759 return NULL;
4762 static const struct got_error *
4763 show_tree_view(struct tog_view *view)
4765 const struct got_error *err = NULL;
4766 struct tog_tree_view_state *s = &view->state.tree;
4767 char *parent_path;
4769 err = tree_entry_path(&parent_path, &s->parents, NULL);
4770 if (err)
4771 return err;
4773 err = draw_tree_entries(view, &s->first_displayed_entry,
4774 &s->last_displayed_entry, &s->selected_entry,
4775 &s->ndisplayed, s->tree_label, s->show_ids, parent_path,
4776 s->entries, s->selected, view->nlines, s->tree == s->root,
4777 &s->colors);
4778 free(parent_path);
4780 view_vborder(view);
4781 return err;
4784 static const struct got_error *
4785 input_tree_view(struct tog_view **new_view, struct tog_view **dead_view,
4786 struct tog_view **focus_view, struct tog_view *view, int ch)
4788 const struct got_error *err = NULL;
4789 struct tog_tree_view_state *s = &view->state.tree;
4790 struct tog_view *log_view;
4791 int begin_x = 0, nscrolled;
4793 switch (ch) {
4794 case 'i':
4795 s->show_ids = !s->show_ids;
4796 break;
4797 case 'l':
4798 if (!s->selected_entry)
4799 break;
4800 if (view_is_parent_view(view))
4801 begin_x = view_split_begin_x(view->begin_x);
4802 err = log_tree_entry(&log_view, begin_x,
4803 s->selected_entry, &s->parents,
4804 s->commit_id, s->refs, s->repo);
4805 if (view_is_parent_view(view)) {
4806 err = view_close_child(view);
4807 if (err)
4808 return err;
4809 err = view_set_child(view, log_view);
4810 if (err) {
4811 view_close(log_view);
4812 break;
4814 *focus_view = log_view;
4815 view->child_focussed = 1;
4816 } else
4817 *new_view = log_view;
4818 break;
4819 case 'k':
4820 case KEY_UP:
4821 if (s->selected > 0) {
4822 s->selected--;
4823 if (s->selected == 0)
4824 break;
4826 if (s->selected > 0)
4827 break;
4828 tree_scroll_up(view, &s->first_displayed_entry, 1,
4829 s->entries, s->tree == s->root);
4830 break;
4831 case KEY_PPAGE:
4832 tree_scroll_up(view, &s->first_displayed_entry,
4833 MAX(0, view->nlines - 4 - s->selected), s->entries,
4834 s->tree == s->root);
4835 s->selected = 0;
4836 if (SIMPLEQ_FIRST(&s->entries->head) ==
4837 s->first_displayed_entry && s->tree != s->root)
4838 s->first_displayed_entry = NULL;
4839 break;
4840 case 'j':
4841 case KEY_DOWN:
4842 if (s->selected < s->ndisplayed - 1) {
4843 s->selected++;
4844 break;
4846 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry) == NULL)
4847 /* can't scroll any further */
4848 break;
4849 tree_scroll_down(&s->first_displayed_entry, 1,
4850 s->last_displayed_entry, s->entries);
4851 break;
4852 case KEY_NPAGE:
4853 if (SIMPLEQ_NEXT(s->last_displayed_entry, entry)
4854 == NULL) {
4855 /* can't scroll any further; move cursor down */
4856 if (s->selected < s->ndisplayed - 1)
4857 s->selected = s->ndisplayed - 1;
4858 break;
4860 nscrolled = tree_scroll_down(&s->first_displayed_entry,
4861 view->nlines, s->last_displayed_entry, s->entries);
4862 if (nscrolled < view->nlines) {
4863 int ndisplayed = 0;
4864 struct got_tree_entry *te;
4865 te = s->first_displayed_entry;
4866 do {
4867 ndisplayed++;
4868 te = SIMPLEQ_NEXT(te, entry);
4869 } while (te);
4870 s->selected = ndisplayed - 1;
4872 break;
4873 case KEY_ENTER:
4874 case '\r':
4875 case KEY_BACKSPACE:
4876 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
4877 struct tog_parent_tree *parent;
4878 /* user selected '..' */
4879 if (s->tree == s->root)
4880 break;
4881 parent = TAILQ_FIRST(&s->parents);
4882 TAILQ_REMOVE(&s->parents, parent,
4883 entry);
4884 got_object_tree_close(s->tree);
4885 s->tree = parent->tree;
4886 s->entries =
4887 got_object_tree_get_entries(s->tree);
4888 s->first_displayed_entry =
4889 parent->first_displayed_entry;
4890 s->selected_entry =
4891 parent->selected_entry;
4892 s->selected = parent->selected;
4893 free(parent);
4894 } else if (S_ISDIR(s->selected_entry->mode)) {
4895 struct got_tree_object *subtree;
4896 err = got_object_open_as_tree(&subtree,
4897 s->repo, s->selected_entry->id);
4898 if (err)
4899 break;
4900 err = tree_view_visit_subtree(subtree, s);
4901 if (err) {
4902 got_object_tree_close(subtree);
4903 break;
4905 } else if (S_ISREG(s->selected_entry->mode)) {
4906 struct tog_view *blame_view;
4907 int begin_x = view_is_parent_view(view) ?
4908 view_split_begin_x(view->begin_x) : 0;
4910 err = blame_tree_entry(&blame_view, begin_x,
4911 s->selected_entry, &s->parents,
4912 s->commit_id, s->refs, s->repo);
4913 if (err)
4914 break;
4915 if (view_is_parent_view(view)) {
4916 err = view_close_child(view);
4917 if (err)
4918 return err;
4919 err = view_set_child(view, blame_view);
4920 if (err) {
4921 view_close(blame_view);
4922 break;
4924 *focus_view = blame_view;
4925 view->child_focussed = 1;
4926 } else
4927 *new_view = blame_view;
4929 break;
4930 case KEY_RESIZE:
4931 if (s->selected > view->nlines)
4932 s->selected = s->ndisplayed - 1;
4933 break;
4934 default:
4935 break;
4938 return err;
4941 __dead static void
4942 usage_tree(void)
4944 endwin();
4945 fprintf(stderr, "usage: %s tree [-c commit] [repository-path]\n",
4946 getprogname());
4947 exit(1);
4950 static const struct got_error *
4951 cmd_tree(int argc, char *argv[])
4953 const struct got_error *error;
4954 struct got_repository *repo = NULL;
4955 struct got_reflist_head refs;
4956 char *repo_path = NULL;
4957 struct got_object_id *commit_id = NULL;
4958 char *commit_id_arg = NULL;
4959 struct got_commit_object *commit = NULL;
4960 struct got_tree_object *tree = NULL;
4961 int ch;
4962 struct tog_view *view;
4964 SIMPLEQ_INIT(&refs);
4966 #ifndef PROFILE
4967 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
4968 NULL) == -1)
4969 err(1, "pledge");
4970 #endif
4972 while ((ch = getopt(argc, argv, "c:")) != -1) {
4973 switch (ch) {
4974 case 'c':
4975 commit_id_arg = optarg;
4976 break;
4977 default:
4978 usage_tree();
4979 /* NOTREACHED */
4983 argc -= optind;
4984 argv += optind;
4986 if (argc == 0) {
4987 struct got_worktree *worktree;
4988 char *cwd = getcwd(NULL, 0);
4989 if (cwd == NULL)
4990 return got_error_from_errno("getcwd");
4991 error = got_worktree_open(&worktree, cwd);
4992 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4993 goto done;
4994 if (worktree) {
4995 free(cwd);
4996 repo_path =
4997 strdup(got_worktree_get_repo_path(worktree));
4998 got_worktree_close(worktree);
4999 } else
5000 repo_path = cwd;
5001 if (repo_path == NULL) {
5002 error = got_error_from_errno("strdup");
5003 goto done;
5005 } else if (argc == 1) {
5006 repo_path = realpath(argv[0], NULL);
5007 if (repo_path == NULL)
5008 return got_error_from_errno2("realpath", argv[0]);
5009 } else
5010 usage_log();
5012 init_curses();
5014 error = got_repo_open(&repo, repo_path, NULL);
5015 if (error != NULL)
5016 goto done;
5018 error = apply_unveil(got_repo_get_path(repo), NULL);
5019 if (error)
5020 goto done;
5022 if (commit_id_arg == NULL)
5023 error = get_head_commit_id(&commit_id, GOT_REF_HEAD, repo);
5024 else {
5025 error = get_head_commit_id(&commit_id, commit_id_arg, repo);
5026 if (error) {
5027 if (error->code != GOT_ERR_NOT_REF)
5028 goto done;
5029 error = got_repo_match_object_id_prefix(&commit_id,
5030 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
5033 if (error != NULL)
5034 goto done;
5036 error = got_object_open_as_commit(&commit, repo, commit_id);
5037 if (error != NULL)
5038 goto done;
5040 error = got_object_open_as_tree(&tree, repo,
5041 got_object_commit_get_tree_id(commit));
5042 if (error != NULL)
5043 goto done;
5045 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
5046 if (error)
5047 goto done;
5049 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
5050 if (view == NULL) {
5051 error = got_error_from_errno("view_open");
5052 goto done;
5054 error = open_tree_view(view, tree, commit_id, &refs, repo);
5055 if (error)
5056 goto done;
5057 error = view_loop(view);
5058 done:
5059 free(repo_path);
5060 free(commit_id);
5061 if (commit)
5062 got_object_commit_close(commit);
5063 if (tree)
5064 got_object_tree_close(tree);
5065 if (repo)
5066 got_repo_close(repo);
5067 got_ref_list_free(&refs);
5068 return error;
5071 static void
5072 list_commands(void)
5074 int i;
5076 fprintf(stderr, "commands:");
5077 for (i = 0; i < nitems(tog_commands); i++) {
5078 struct tog_cmd *cmd = &tog_commands[i];
5079 fprintf(stderr, " %s", cmd->name);
5081 fputc('\n', stderr);
5084 __dead static void
5085 usage(int hflag)
5087 fprintf(stderr, "usage: %s [-h] [-V] [command] [arg ...]\n",
5088 getprogname());
5089 if (hflag)
5090 list_commands();
5091 exit(1);
5094 static char **
5095 make_argv(const char *arg0, const char *arg1)
5097 char **argv;
5098 int argc = (arg1 == NULL ? 1 : 2);
5100 argv = calloc(argc, sizeof(char *));
5101 if (argv == NULL)
5102 err(1, "calloc");
5103 argv[0] = strdup(arg0);
5104 if (argv[0] == NULL)
5105 err(1, "strdup");
5106 if (arg1) {
5107 argv[1] = strdup(arg1);
5108 if (argv[1] == NULL)
5109 err(1, "strdup");
5112 return argv;
5115 int
5116 main(int argc, char *argv[])
5118 const struct got_error *error = NULL;
5119 struct tog_cmd *cmd = NULL;
5120 int ch, hflag = 0, Vflag = 0;
5121 char **cmd_argv = NULL;
5123 setlocale(LC_CTYPE, "");
5125 while ((ch = getopt(argc, argv, "hV")) != -1) {
5126 switch (ch) {
5127 case 'h':
5128 hflag = 1;
5129 break;
5130 case 'V':
5131 Vflag = 1;
5132 break;
5133 default:
5134 usage(hflag);
5135 /* NOTREACHED */
5139 argc -= optind;
5140 argv += optind;
5141 optind = 0;
5142 optreset = 1;
5144 if (Vflag) {
5145 got_version_print_str();
5146 return 1;
5149 if (argc == 0) {
5150 if (hflag)
5151 usage(hflag);
5152 /* Build an argument vector which runs a default command. */
5153 cmd = &tog_commands[0];
5154 cmd_argv = make_argv(cmd->name, NULL);
5155 argc = 1;
5156 } else {
5157 int i;
5159 /* Did the user specific a command? */
5160 for (i = 0; i < nitems(tog_commands); i++) {
5161 if (strncmp(tog_commands[i].name, argv[0],
5162 strlen(argv[0])) == 0) {
5163 cmd = &tog_commands[i];
5164 break;
5168 if (cmd == NULL) {
5169 fprintf(stderr, "%s: unknown command '%s'\n",
5170 getprogname(), argv[0]);
5171 list_commands();
5172 return 1;
5176 if (hflag)
5177 cmd->cmd_usage();
5178 else
5179 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
5181 endwin();
5182 free(cmd_argv);
5183 if (error && error->code != GOT_ERR_CANCELLED)
5184 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
5185 return 0;