2 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/ioctl.h>
22 #if defined(__FreeBSD__) || defined(__APPLE__)
23 #define _XOPEN_SOURCE_EXTENDED /* for ncurses wide-character functions */
44 #include "got_compat.h"
46 #include "got_version.h"
47 #include "got_error.h"
48 #include "got_object.h"
49 #include "got_reference.h"
50 #include "got_repository.h"
52 #include "got_opentemp.h"
54 #include "got_cancel.h"
55 #include "got_commit_graph.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
59 #include "got_worktree.h"
61 //#define update_panels() (0)
62 //#define doupdate() (0)
65 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
73 #define CTRL(x) ((x) & 0x1f)
77 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
82 const struct got_error *(*cmd_main)(int, char *[]);
83 void (*cmd_usage)(void);
86 __dead static void usage(int, int);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_ref(void);
93 static const struct got_error* cmd_log(int, char *[]);
94 static const struct got_error* cmd_diff(int, char *[]);
95 static const struct got_error* cmd_blame(int, char *[]);
96 static const struct got_error* cmd_tree(int, char *[]);
97 static const struct got_error* cmd_ref(int, char *[]);
99 static const struct tog_cmd tog_commands[] = {
100 { "log", cmd_log, usage_log },
101 { "diff", cmd_diff, usage_diff },
102 { "blame", cmd_blame, usage_blame },
103 { "tree", cmd_tree, usage_tree },
104 { "ref", cmd_ref, usage_ref },
115 #define TOG_EOF_STRING "(END)"
117 struct commit_queue_entry {
118 TAILQ_ENTRY(commit_queue_entry) entry;
119 struct got_object_id *id;
120 struct got_commit_object *commit;
123 TAILQ_HEAD(commit_queue_head, commit_queue_entry);
124 struct commit_queue {
126 struct commit_queue_head head;
130 STAILQ_ENTRY(tog_color) entry;
134 STAILQ_HEAD(tog_colors, tog_color);
136 static struct got_reflist_head tog_refs = TAILQ_HEAD_INITIALIZER(tog_refs);
137 static struct got_reflist_object_id_map *tog_refs_idmap;
139 static const struct got_error *
140 tog_ref_cmp_by_name(void *arg, int *cmp, struct got_reference *re1,
141 struct got_reference* re2)
143 const char *name1 = got_ref_get_name(re1);
144 const char *name2 = got_ref_get_name(re2);
145 int isbackup1, isbackup2;
147 /* Sort backup refs towards the bottom of the list. */
148 isbackup1 = strncmp(name1, "refs/got/backup/", 16) == 0;
149 isbackup2 = strncmp(name2, "refs/got/backup/", 16) == 0;
150 if (!isbackup1 && isbackup2) {
153 } else if (isbackup1 && !isbackup2) {
158 *cmp = got_path_cmp(name1, name2, strlen(name1), strlen(name2));
162 static const struct got_error *
163 tog_load_refs(struct got_repository *repo, int sort_by_date)
165 const struct got_error *err;
167 err = got_ref_list(&tog_refs, repo, NULL, sort_by_date ?
168 got_ref_cmp_by_commit_timestamp_descending : tog_ref_cmp_by_name,
173 return got_reflist_object_id_map_create(&tog_refs_idmap, &tog_refs,
180 if (tog_refs_idmap) {
181 got_reflist_object_id_map_free(tog_refs_idmap);
182 tog_refs_idmap = NULL;
184 got_ref_list_free(&tog_refs);
187 static const struct got_error *
188 add_color(struct tog_colors *colors, const char *pattern,
189 int idx, short color)
191 const struct got_error *err = NULL;
192 struct tog_color *tc;
195 if (idx < 1 || idx > COLOR_PAIRS - 1)
198 init_pair(idx, color, -1);
200 tc = calloc(1, sizeof(*tc));
202 return got_error_from_errno("calloc");
203 regerr = regcomp(&tc->regex, pattern,
204 REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
206 static char regerr_msg[512];
207 static char err_msg[512];
208 regerror(regerr, &tc->regex, regerr_msg,
210 snprintf(err_msg, sizeof(err_msg), "regcomp: %s",
212 err = got_error_msg(GOT_ERR_REGEX, err_msg);
217 STAILQ_INSERT_HEAD(colors, tc, entry);
222 free_colors(struct tog_colors *colors)
224 struct tog_color *tc;
226 while (!STAILQ_EMPTY(colors)) {
227 tc = STAILQ_FIRST(colors);
228 STAILQ_REMOVE_HEAD(colors, entry);
234 static struct tog_color *
235 get_color(struct tog_colors *colors, int colorpair)
237 struct tog_color *tc = NULL;
239 STAILQ_FOREACH(tc, colors, entry) {
240 if (tc->colorpair == colorpair)
248 default_color_value(const char *envvar)
250 if (strcmp(envvar, "TOG_COLOR_DIFF_MINUS") == 0)
251 return COLOR_MAGENTA;
252 if (strcmp(envvar, "TOG_COLOR_DIFF_PLUS") == 0)
254 if (strcmp(envvar, "TOG_COLOR_DIFF_CHUNK_HEADER") == 0)
256 if (strcmp(envvar, "TOG_COLOR_DIFF_META") == 0)
258 if (strcmp(envvar, "TOG_COLOR_TREE_SUBMODULE") == 0)
259 return COLOR_MAGENTA;
260 if (strcmp(envvar, "TOG_COLOR_TREE_SYMLINK") == 0)
261 return COLOR_MAGENTA;
262 if (strcmp(envvar, "TOG_COLOR_TREE_DIRECTORY") == 0)
264 if (strcmp(envvar, "TOG_COLOR_TREE_EXECUTABLE") == 0)
266 if (strcmp(envvar, "TOG_COLOR_COMMIT") == 0)
268 if (strcmp(envvar, "TOG_COLOR_AUTHOR") == 0)
270 if (strcmp(envvar, "TOG_COLOR_DATE") == 0)
272 if (strcmp(envvar, "TOG_COLOR_REFS_HEADS") == 0)
274 if (strcmp(envvar, "TOG_COLOR_REFS_TAGS") == 0)
275 return COLOR_MAGENTA;
276 if (strcmp(envvar, "TOG_COLOR_REFS_REMOTES") == 0)
278 if (strcmp(envvar, "TOG_COLOR_REFS_BACKUP") == 0)
285 get_color_value(const char *envvar)
287 const char *val = getenv(envvar);
290 return default_color_value(envvar);
292 if (strcasecmp(val, "black") == 0)
294 if (strcasecmp(val, "red") == 0)
296 if (strcasecmp(val, "green") == 0)
298 if (strcasecmp(val, "yellow") == 0)
300 if (strcasecmp(val, "blue") == 0)
302 if (strcasecmp(val, "magenta") == 0)
303 return COLOR_MAGENTA;
304 if (strcasecmp(val, "cyan") == 0)
306 if (strcasecmp(val, "white") == 0)
308 if (strcasecmp(val, "default") == 0)
311 return default_color_value(envvar);
315 struct tog_diff_view_state {
316 struct got_object_id *id1, *id2;
317 const char *label1, *label2;
319 int first_displayed_line;
320 int last_displayed_line;
323 int ignore_whitespace;
325 struct got_repository *repo;
326 struct tog_colors colors;
332 /* passed from log view; may be NULL */
333 struct tog_view *log_view;
336 pthread_mutex_t tog_mutex = PTHREAD_MUTEX_INITIALIZER;
338 struct tog_log_thread_args {
339 pthread_cond_t need_commits;
340 pthread_cond_t commit_loaded;
343 struct got_commit_graph *graph;
344 struct commit_queue *commits;
345 const char *in_repo_path;
346 struct got_object_id *start_id;
347 struct got_repository *repo;
351 struct commit_queue_entry **first_displayed_entry;
352 struct commit_queue_entry **selected_entry;
354 int *search_next_done;
358 struct tog_log_view_state {
359 struct commit_queue commits;
360 struct commit_queue_entry *first_displayed_entry;
361 struct commit_queue_entry *last_displayed_entry;
362 struct commit_queue_entry *selected_entry;
367 struct got_repository *repo;
368 struct got_object_id *start_id;
371 struct tog_log_thread_args thread_args;
372 struct commit_queue_entry *matched_entry;
373 struct commit_queue_entry *search_entry;
374 struct tog_colors colors;
377 #define TOG_COLOR_DIFF_MINUS 1
378 #define TOG_COLOR_DIFF_PLUS 2
379 #define TOG_COLOR_DIFF_CHUNK_HEADER 3
380 #define TOG_COLOR_DIFF_META 4
381 #define TOG_COLOR_TREE_SUBMODULE 5
382 #define TOG_COLOR_TREE_SYMLINK 6
383 #define TOG_COLOR_TREE_DIRECTORY 7
384 #define TOG_COLOR_TREE_EXECUTABLE 8
385 #define TOG_COLOR_COMMIT 9
386 #define TOG_COLOR_AUTHOR 10
387 #define TOG_COLOR_DATE 11
388 #define TOG_COLOR_REFS_HEADS 12
389 #define TOG_COLOR_REFS_TAGS 13
390 #define TOG_COLOR_REFS_REMOTES 14
391 #define TOG_COLOR_REFS_BACKUP 15
393 struct tog_blame_cb_args {
394 struct tog_blame_line *lines; /* one per line */
397 struct tog_view *view;
398 struct got_object_id *commit_id;
402 struct tog_blame_thread_args {
404 struct got_repository *repo;
405 struct tog_blame_cb_args *cb_args;
407 got_cancel_cb cancel_cb;
414 struct tog_blame_line *lines;
418 struct tog_blame_thread_args thread_args;
419 struct tog_blame_cb_args cb_args;
424 struct tog_blame_view_state {
425 int first_displayed_line;
426 int last_displayed_line;
431 struct got_object_id_queue blamed_commits;
432 struct got_object_qid *blamed_commit;
434 struct got_repository *repo;
435 struct got_object_id *commit_id;
436 struct tog_blame blame;
438 struct tog_colors colors;
441 struct tog_parent_tree {
442 TAILQ_ENTRY(tog_parent_tree) entry;
443 struct got_tree_object *tree;
444 struct got_tree_entry *first_displayed_entry;
445 struct got_tree_entry *selected_entry;
449 TAILQ_HEAD(tog_parent_trees, tog_parent_tree);
451 struct tog_tree_view_state {
453 struct got_object_id *commit_id;/* commit which this tree belongs to */
454 struct got_tree_object *root; /* the commit's root tree entry */
455 struct got_tree_object *tree; /* currently displayed (sub-)tree */
456 struct got_tree_entry *first_displayed_entry;
457 struct got_tree_entry *last_displayed_entry;
458 struct got_tree_entry *selected_entry;
459 int ndisplayed, selected, show_ids;
460 struct tog_parent_trees parents; /* parent trees of current sub-tree */
462 struct got_repository *repo;
463 struct got_tree_entry *matched_entry;
464 struct tog_colors colors;
467 struct tog_reflist_entry {
468 TAILQ_ENTRY(tog_reflist_entry) entry;
469 struct got_reference *ref;
473 TAILQ_HEAD(tog_reflist_head, tog_reflist_entry);
475 struct tog_ref_view_state {
476 struct tog_reflist_head refs;
477 struct tog_reflist_entry *first_displayed_entry;
478 struct tog_reflist_entry *last_displayed_entry;
479 struct tog_reflist_entry *selected_entry;
480 int nrefs, ndisplayed, selected, show_date, show_ids, sort_by_date;
481 struct got_repository *repo;
482 struct tog_reflist_entry *matched_entry;
483 struct tog_colors colors;
487 * We implement two types of views: parent views and child views.
489 * The 'Tab' key switches focus between a parent view and its child view.
490 * Child views are shown side-by-side to their parent view, provided
491 * there is enough screen estate.
493 * When a new view is opened from within a parent view, this new view
494 * becomes a child view of the parent view, replacing any existing child.
496 * When a new view is opened from within a child view, this new view
497 * becomes a parent view which will obscure the views below until the
498 * user quits the new parent view by typing 'q'.
500 * This list of views contains parent views only.
501 * Child views are only pointed to by their parent view.
503 TAILQ_HEAD(tog_view_list_head, tog_view);
506 TAILQ_ENTRY(tog_view) entry;
509 int nlines, ncols, begin_y, begin_x;
510 int maxx, x; /* max column and current start column */
511 int lines, cols; /* copies of LINES and COLS */
512 int ch, count; /* current keymap and count prefix */
513 int focussed; /* Only set on one parent or child view at a time. */
515 struct tog_view *parent;
516 struct tog_view *child;
519 * This flag is initially set on parent views when a new child view
520 * is created. It gets toggled when the 'Tab' key switches focus
521 * between parent and child.
522 * The flag indicates whether focus should be passed on to our child
523 * view if this parent view gets picked for focus after another parent
524 * view was closed. This prevents child views from losing focus in such
529 /* type-specific state */
530 enum tog_view_type type;
532 struct tog_diff_view_state diff;
533 struct tog_log_view_state log;
534 struct tog_blame_view_state blame;
535 struct tog_tree_view_state tree;
536 struct tog_ref_view_state ref;
539 const struct got_error *(*show)(struct tog_view *);
540 const struct got_error *(*input)(struct tog_view **,
541 struct tog_view *, int);
542 const struct got_error *(*close)(struct tog_view *);
544 const struct got_error *(*search_start)(struct tog_view *);
545 const struct got_error *(*search_next)(struct tog_view *);
548 #define TOG_SEARCH_FORWARD 1
549 #define TOG_SEARCH_BACKWARD 2
550 int search_next_done;
551 #define TOG_SEARCH_HAVE_MORE 1
552 #define TOG_SEARCH_NO_MORE 2
553 #define TOG_SEARCH_HAVE_NONE 3
558 static const struct got_error *open_diff_view(struct tog_view *,
559 struct got_object_id *, struct got_object_id *,
560 const char *, const char *, int, int, int, struct tog_view *,
561 struct got_repository *);
562 static const struct got_error *show_diff_view(struct tog_view *);
563 static const struct got_error *input_diff_view(struct tog_view **,
564 struct tog_view *, int);
565 static const struct got_error* close_diff_view(struct tog_view *);
566 static const struct got_error *search_start_diff_view(struct tog_view *);
567 static const struct got_error *search_next_diff_view(struct tog_view *);
569 static const struct got_error *open_log_view(struct tog_view *,
570 struct got_object_id *, struct got_repository *,
571 const char *, const char *, int);
572 static const struct got_error * show_log_view(struct tog_view *);
573 static const struct got_error *input_log_view(struct tog_view **,
574 struct tog_view *, int);
575 static const struct got_error *close_log_view(struct tog_view *);
576 static const struct got_error *search_start_log_view(struct tog_view *);
577 static const struct got_error *search_next_log_view(struct tog_view *);
579 static const struct got_error *open_blame_view(struct tog_view *, char *,
580 struct got_object_id *, struct got_repository *);
581 static const struct got_error *show_blame_view(struct tog_view *);
582 static const struct got_error *input_blame_view(struct tog_view **,
583 struct tog_view *, int);
584 static const struct got_error *close_blame_view(struct tog_view *);
585 static const struct got_error *search_start_blame_view(struct tog_view *);
586 static const struct got_error *search_next_blame_view(struct tog_view *);
588 static const struct got_error *open_tree_view(struct tog_view *,
589 struct got_object_id *, const char *, struct got_repository *);
590 static const struct got_error *show_tree_view(struct tog_view *);
591 static const struct got_error *input_tree_view(struct tog_view **,
592 struct tog_view *, int);
593 static const struct got_error *close_tree_view(struct tog_view *);
594 static const struct got_error *search_start_tree_view(struct tog_view *);
595 static const struct got_error *search_next_tree_view(struct tog_view *);
597 static const struct got_error *open_ref_view(struct tog_view *,
598 struct got_repository *);
599 static const struct got_error *show_ref_view(struct tog_view *);
600 static const struct got_error *input_ref_view(struct tog_view **,
601 struct tog_view *, int);
602 static const struct got_error *close_ref_view(struct tog_view *);
603 static const struct got_error *search_start_ref_view(struct tog_view *);
604 static const struct got_error *search_next_ref_view(struct tog_view *);
606 static volatile sig_atomic_t tog_sigwinch_received;
607 static volatile sig_atomic_t tog_sigpipe_received;
608 static volatile sig_atomic_t tog_sigcont_received;
609 static volatile sig_atomic_t tog_sigint_received;
610 static volatile sig_atomic_t tog_sigterm_received;
613 tog_sigwinch(int signo)
615 tog_sigwinch_received = 1;
619 tog_sigpipe(int signo)
621 tog_sigpipe_received = 1;
625 tog_sigcont(int signo)
627 tog_sigcont_received = 1;
631 tog_sigint(int signo)
633 tog_sigint_received = 1;
637 tog_sigterm(int signo)
639 tog_sigterm_received = 1;
643 tog_fatal_signal_received(void)
645 return (tog_sigpipe_received ||
646 tog_sigint_received || tog_sigint_received);
650 static const struct got_error *
651 view_close(struct tog_view *view)
653 const struct got_error *err = NULL;
656 view_close(view->child);
660 err = view->close(view);
662 del_panel(view->panel);
664 delwin(view->window);
669 static struct tog_view *
670 view_open(int nlines, int ncols, int begin_y, int begin_x,
671 enum tog_view_type type)
673 struct tog_view *view = calloc(1, sizeof(*view));
683 view->nlines = nlines ? nlines : LINES - begin_y;
684 view->ncols = ncols ? ncols : COLS - begin_x;
685 view->begin_y = begin_y;
686 view->begin_x = begin_x;
687 view->window = newwin(nlines, ncols, begin_y, begin_x);
688 if (view->window == NULL) {
692 view->panel = new_panel(view->window);
693 if (view->panel == NULL ||
694 set_panel_userptr(view->panel, view) != OK) {
699 keypad(view->window, TRUE);
704 view_split_begin_x(int begin_x)
706 if (begin_x > 0 || COLS < 120)
708 return (COLS - MAX(COLS / 2, 80));
711 static const struct got_error *view_resize(struct tog_view *);
713 static const struct got_error *
714 view_splitscreen(struct tog_view *view)
716 const struct got_error *err = NULL;
719 view->begin_x = view_split_begin_x(0);
720 view->nlines = LINES;
721 view->ncols = COLS - view->begin_x;
724 err = view_resize(view);
728 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
729 return got_error_from_errno("mvwin");
734 static const struct got_error *
735 view_fullscreen(struct tog_view *view)
737 const struct got_error *err = NULL;
741 view->nlines = LINES;
745 err = view_resize(view);
749 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
750 return got_error_from_errno("mvwin");
756 view_is_parent_view(struct tog_view *view)
758 return view->parent == NULL;
762 view_is_splitscreen(struct tog_view *view)
764 return view->begin_x > 0;
768 static const struct got_error *
769 view_resize(struct tog_view *view)
773 if (view->lines > LINES)
774 nlines = view->nlines - (view->lines - LINES);
776 nlines = view->nlines + (LINES - view->lines);
778 if (view->cols > COLS)
779 ncols = view->ncols - (view->cols - COLS);
781 ncols = view->ncols + (COLS - view->cols);
783 if (view->child && view_is_splitscreen(view->child)) {
784 view->child->begin_x = view_split_begin_x(view->begin_x);
785 if (view->child->begin_x == 0) {
788 view_fullscreen(view->child);
789 if (view->child->focussed)
790 show_panel(view->child->panel);
792 show_panel(view->panel);
794 ncols = view->child->begin_x;
796 view_splitscreen(view->child);
797 show_panel(view->child->panel);
799 } else if (view->parent == NULL)
802 if (wresize(view->window, nlines, ncols) == ERR)
803 return got_error_from_errno("wresize");
804 if (replace_panel(view->panel, view->window) == ERR)
805 return got_error_from_errno("replace_panel");
806 wclear(view->window);
808 view->nlines = nlines;
816 static const struct got_error *
817 view_close_child(struct tog_view *view)
819 const struct got_error *err = NULL;
821 if (view->child == NULL)
824 err = view_close(view->child);
829 static const struct got_error *
830 view_set_child(struct tog_view *view, struct tog_view *child)
833 child->parent = view;
835 return view_resize(view);
844 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
845 cols = 80; /* Default */
851 resize_term(lines, cols);
854 static const struct got_error *
855 view_search_start(struct tog_view *view)
857 const struct got_error *err = NULL;
861 if (view->search_started) {
862 regfree(&view->regex);
864 memset(&view->regmatch, 0, sizeof(view->regmatch));
866 view->search_started = 0;
868 if (view->nlines < 1)
871 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
872 wclrtoeol(view->window);
876 ret = wgetnstr(view->window, pattern, sizeof(pattern));
882 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
883 err = view->search_start(view);
885 regfree(&view->regex);
888 view->search_started = 1;
889 view->searching = TOG_SEARCH_FORWARD;
890 view->search_next_done = 0;
891 view->search_next(view);
898 * Compute view->count from numeric user input. User has five-tenths of a
899 * second to follow each numeric keypress with another number to form count.
900 * Return first non-numeric input or ERR and assign total to view->count.
901 * XXX Should we add support for user-defined timeout?
904 get_compound_key(struct tog_view *view, int c)
909 halfdelay(5); /* block for half a second */
910 wattron(view->window, A_BOLD);
911 wmove(view->window, view->nlines - 1, 0);
912 wclrtoeol(view->window);
913 waddch(view->window, ':');
916 x = getcurx(view->window);
917 if (x != ERR && x < view->ncols)
918 waddch(view->window, c);
920 * Don't overflow. Max valid request should be the greatest
921 * between the longest and total lines; cap at 10 million.
926 n = n * 10 + (c - '0');
927 } while (((c = wgetch(view->window))) >= '0' && c <= '9' && c != ERR);
929 /* Massage excessive or inapplicable values at the input handler. */
932 wattroff(view->window, A_BOLD);
933 cbreak(); /* return to blocking */
937 static const struct got_error *
938 view_input(struct tog_view **new, int *done, struct tog_view *view,
939 struct tog_view_list_head *views)
941 const struct got_error *err = NULL;
947 /* Clear "no matches" indicator. */
948 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
949 view->search_next_done == TOG_SEARCH_HAVE_NONE) {
950 view->search_next_done = TOG_SEARCH_HAVE_MORE;
954 if (view->searching && !view->search_next_done) {
955 errcode = pthread_mutex_unlock(&tog_mutex);
957 return got_error_set_errno(errcode,
958 "pthread_mutex_unlock");
960 errcode = pthread_mutex_lock(&tog_mutex);
962 return got_error_set_errno(errcode,
963 "pthread_mutex_lock");
964 view->search_next(view);
968 nodelay(stdscr, FALSE);
969 /* Allow threads to make progress while we are waiting for input. */
970 errcode = pthread_mutex_unlock(&tog_mutex);
972 return got_error_set_errno(errcode, "pthread_mutex_unlock");
973 /* If we have an unfinished count, don't get a new key map. */
975 if ((view->count && --view->count == 0) || !view->count) {
976 ch = wgetch(view->window);
977 if (ch >= '1' && ch <= '9')
978 view->ch = ch = get_compound_key(view, ch);
980 errcode = pthread_mutex_lock(&tog_mutex);
982 return got_error_set_errno(errcode, "pthread_mutex_lock");
983 nodelay(stdscr, TRUE);
985 if (tog_sigwinch_received || tog_sigcont_received) {
987 tog_sigwinch_received = 0;
988 tog_sigcont_received = 0;
989 TAILQ_FOREACH(v, views, entry) {
990 err = view_resize(v);
993 err = v->input(new, v, KEY_RESIZE);
997 err = view_resize(v->child);
1000 err = v->child->input(new, v->child,
1013 view->child->focussed = 1;
1014 view->focus_child = 1;
1015 } else if (view->parent) {
1017 view->parent->focussed = 1;
1018 view->parent->focus_child = 0;
1019 if (!view_is_splitscreen(view))
1020 err = view_fullscreen(view->parent);
1024 err = view->input(new, view, ch);
1032 if (view_is_parent_view(view)) {
1033 if (view->child == NULL)
1035 if (view_is_splitscreen(view->child)) {
1037 view->child->focussed = 1;
1038 err = view_fullscreen(view->child);
1040 err = view_splitscreen(view->child);
1043 err = view->child->input(new, view->child,
1046 if (view_is_splitscreen(view)) {
1047 view->parent->focussed = 0;
1049 err = view_fullscreen(view);
1051 err = view_splitscreen(view);
1053 err = view_resize(view->parent);
1057 err = view->input(new, view, KEY_RESIZE);
1064 if (view->search_start)
1065 view_search_start(view);
1067 err = view->input(new, view, ch);
1071 if (view->search_started && view->search_next) {
1072 view->searching = (ch == 'n' ?
1073 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1074 view->search_next_done = 0;
1075 view->search_next(view);
1077 err = view->input(new, view, ch);
1080 err = view->input(new, view, ch);
1088 view_vborder(struct tog_view *view)
1091 const struct tog_view *view_above;
1094 return view_vborder(view->parent);
1096 panel = panel_above(view->panel);
1100 view_above = panel_userptr(panel);
1101 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1102 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1106 view_needs_focus_indication(struct tog_view *view)
1108 if (view_is_parent_view(view)) {
1109 if (view->child == NULL || view->child->focussed)
1111 if (!view_is_splitscreen(view->child))
1113 } else if (!view_is_splitscreen(view))
1116 return view->focussed;
1119 static const struct got_error *
1120 view_loop(struct tog_view *view)
1122 const struct got_error *err = NULL;
1123 struct tog_view_list_head views;
1124 struct tog_view *new_view;
1125 int fast_refresh = 10;
1126 int done = 0, errcode;
1128 errcode = pthread_mutex_lock(&tog_mutex);
1130 return got_error_set_errno(errcode, "pthread_mutex_lock");
1133 TAILQ_INSERT_HEAD(&views, view, entry);
1136 err = view->show(view);
1141 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1142 /* Refresh fast during initialization, then become slower. */
1143 if (fast_refresh && fast_refresh-- == 0)
1144 halfdelay(10); /* switch to once per second */
1146 err = view_input(&new_view, &done, view, &views);
1150 struct tog_view *v, *prev = NULL;
1152 if (view_is_parent_view(view))
1153 prev = TAILQ_PREV(view, tog_view_list_head,
1155 else if (view->parent)
1156 prev = view->parent;
1159 view->parent->child = NULL;
1160 view->parent->focus_child = 0;
1162 err = view_resize(view->parent);
1166 TAILQ_REMOVE(&views, view, entry);
1168 err = view_close(view);
1173 TAILQ_FOREACH(v, &views, entry) {
1177 if (view == NULL && new_view == NULL) {
1178 /* No view has focus. Try to pick one. */
1181 else if (!TAILQ_EMPTY(&views)) {
1182 view = TAILQ_LAST(&views,
1183 tog_view_list_head);
1186 if (view->focus_child) {
1187 view->child->focussed = 1;
1195 struct tog_view *v, *t;
1196 /* Only allow one parent view per type. */
1197 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1198 if (v->type != new_view->type)
1200 TAILQ_REMOVE(&views, v, entry);
1201 err = view_close(v);
1206 TAILQ_INSERT_TAIL(&views, new_view, entry);
1210 if (view_is_parent_view(view)) {
1211 if (view->child && view->child->focussed)
1214 if (view->parent && view->parent->focussed)
1215 view = view->parent;
1217 show_panel(view->panel);
1218 if (view->child && view_is_splitscreen(view->child))
1219 show_panel(view->child->panel);
1220 if (view->parent && view_is_splitscreen(view)) {
1221 err = view->parent->show(view->parent);
1225 err = view->show(view);
1229 err = view->child->show(view->child);
1238 while (!TAILQ_EMPTY(&views)) {
1239 view = TAILQ_FIRST(&views);
1240 TAILQ_REMOVE(&views, view, entry);
1244 errcode = pthread_mutex_unlock(&tog_mutex);
1245 if (errcode && err == NULL)
1246 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1256 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1261 /* Create newly allocated wide-character string equivalent to a byte string. */
1262 static const struct got_error *
1263 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1266 const struct got_error *err = NULL;
1269 *wlen = mbstowcs(NULL, s, 0);
1270 if (*wlen == (size_t)-1) {
1272 if (errno != EILSEQ)
1273 return got_error_from_errno("mbstowcs");
1275 /* byte string invalid in current encoding; try to "fix" it */
1276 err = got_mbsavis(&vis, &vislen, s);
1279 *wlen = mbstowcs(NULL, vis, 0);
1280 if (*wlen == (size_t)-1) {
1281 err = got_error_from_errno("mbstowcs"); /* give up */
1286 *ws = calloc(*wlen + 1, sizeof(**ws));
1288 err = got_error_from_errno("calloc");
1292 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1293 err = got_error_from_errno("mbstowcs");
1304 static const struct got_error *
1305 expand_tab(char **ptr, const char *src)
1308 size_t len, n, idx = 0, sz = 0;
1311 n = len = strlen(src);
1312 dst = malloc(n + 1);
1314 return got_error_from_errno("malloc");
1316 while (idx < len && src[idx]) {
1317 const char c = src[idx];
1320 size_t nb = TABSIZE - sz % TABSIZE;
1323 p = realloc(dst, n + nb);
1326 return got_error_from_errno("realloc");
1331 memset(dst + sz, ' ', nb);
1334 dst[sz++] = src[idx];
1344 * Advance at most n columns from wline starting at offset off.
1345 * Return the index to the first character after the span operation.
1346 * Return the combined column width of all spanned wide character in
1350 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1352 int width, i, cols = 0;
1359 for (i = off; wline[i] != L'\0'; ++i) {
1360 if (wline[i] == L'\t')
1361 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1363 width = wcwidth(wline[i]);
1370 if (cols + width > n)
1380 * Format a line for display, ensuring that it won't overflow a width limit.
1381 * With scrolling, the width returned refers to the scrolled version of the
1382 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1384 static const struct got_error *
1385 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1386 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1388 const struct got_error *err = NULL;
1390 wchar_t *wline = NULL;
1399 err = expand_tab(&exstr, line);
1404 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1409 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1411 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1412 wline[wlen - 1] = L'\0';
1415 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1416 wline[wlen - 1] = L'\0';
1420 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1426 *scrollxp = scrollx;
1434 static const struct got_error*
1435 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1436 struct got_object_id *id, struct got_repository *repo)
1438 static const struct got_error *err = NULL;
1439 struct got_reflist_entry *re;
1445 TAILQ_FOREACH(re, refs, entry) {
1446 struct got_tag_object *tag = NULL;
1447 struct got_object_id *ref_id;
1450 name = got_ref_get_name(re->ref);
1451 if (strcmp(name, GOT_REF_HEAD) == 0)
1453 if (strncmp(name, "refs/", 5) == 0)
1455 if (strncmp(name, "got/", 4) == 0 &&
1456 strncmp(name, "got/backup/", 11) != 0)
1458 if (strncmp(name, "heads/", 6) == 0)
1460 if (strncmp(name, "remotes/", 8) == 0) {
1462 s = strstr(name, "/" GOT_REF_HEAD);
1463 if (s != NULL && s[strlen(s)] == '\0')
1466 err = got_ref_resolve(&ref_id, repo, re->ref);
1469 if (strncmp(name, "tags/", 5) == 0) {
1470 err = got_object_open_as_tag(&tag, repo, ref_id);
1472 if (err->code != GOT_ERR_OBJ_TYPE) {
1476 /* Ref points at something other than a tag. */
1481 cmp = got_object_id_cmp(tag ?
1482 got_object_tag_get_object_id(tag) : ref_id, id);
1485 got_object_tag_close(tag);
1489 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1490 s ? ", " : "", name) == -1) {
1491 err = got_error_from_errno("asprintf");
1502 static const struct got_error *
1503 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1508 smallerthan = strchr(author, '<');
1509 if (smallerthan && smallerthan[1] != '\0')
1510 author = smallerthan + 1;
1511 author[strcspn(author, "@>")] = '\0';
1512 return format_line(wauthor, author_width, NULL, author, 0, limit,
1516 static const struct got_error *
1517 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1518 struct got_object_id *id, const size_t date_display_cols,
1519 int author_display_cols)
1521 struct tog_log_view_state *s = &view->state.log;
1522 const struct got_error *err = NULL;
1523 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1524 char *logmsg0 = NULL, *logmsg = NULL;
1525 char *author = NULL;
1526 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1527 int author_width, logmsg_width;
1528 char *newline, *line = NULL;
1529 int col, limit, scrollx;
1530 const int avail = view->ncols;
1532 time_t committer_time;
1533 struct tog_color *tc;
1535 committer_time = got_object_commit_get_committer_time(commit);
1536 if (gmtime_r(&committer_time, &tm) == NULL)
1537 return got_error_from_errno("gmtime_r");
1538 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1539 return got_error(GOT_ERR_NO_SPACE);
1541 if (avail <= date_display_cols)
1542 limit = MIN(sizeof(datebuf) - 1, avail);
1544 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1545 tc = get_color(&s->colors, TOG_COLOR_DATE);
1547 wattr_on(view->window,
1548 COLOR_PAIR(tc->colorpair), NULL);
1549 waddnstr(view->window, datebuf, limit);
1551 wattr_off(view->window,
1552 COLOR_PAIR(tc->colorpair), NULL);
1559 err = got_object_id_str(&id_str, id);
1562 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1564 wattr_on(view->window,
1565 COLOR_PAIR(tc->colorpair), NULL);
1566 wprintw(view->window, "%.8s ", id_str);
1568 wattr_off(view->window,
1569 COLOR_PAIR(tc->colorpair), NULL);
1576 author = strdup(got_object_commit_get_author(commit));
1577 if (author == NULL) {
1578 err = got_error_from_errno("strdup");
1581 err = format_author(&wauthor, &author_width, author, avail - col, col);
1584 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1586 wattr_on(view->window,
1587 COLOR_PAIR(tc->colorpair), NULL);
1588 waddwstr(view->window, wauthor);
1590 wattr_off(view->window,
1591 COLOR_PAIR(tc->colorpair), NULL);
1592 col += author_width;
1593 while (col < avail && author_width < author_display_cols + 2) {
1594 waddch(view->window, ' ');
1601 err = got_object_commit_get_logmsg(&logmsg0, commit);
1605 while (*logmsg == '\n')
1607 newline = strchr(logmsg, '\n');
1610 limit = avail - col;
1611 if (view->child && view_is_splitscreen(view->child) && limit > 0)
1612 limit--; /* for the border */
1613 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1617 waddwstr(view->window, &wlogmsg[scrollx]);
1618 col += MAX(logmsg_width, 0);
1619 while (col < avail) {
1620 waddch(view->window, ' ');
1632 static struct commit_queue_entry *
1633 alloc_commit_queue_entry(struct got_commit_object *commit,
1634 struct got_object_id *id)
1636 struct commit_queue_entry *entry;
1638 entry = calloc(1, sizeof(*entry));
1643 entry->commit = commit;
1648 pop_commit(struct commit_queue *commits)
1650 struct commit_queue_entry *entry;
1652 entry = TAILQ_FIRST(&commits->head);
1653 TAILQ_REMOVE(&commits->head, entry, entry);
1654 got_object_commit_close(entry->commit);
1655 commits->ncommits--;
1656 /* Don't free entry->id! It is owned by the commit graph. */
1661 free_commits(struct commit_queue *commits)
1663 while (!TAILQ_EMPTY(&commits->head))
1664 pop_commit(commits);
1667 static const struct got_error *
1668 match_commit(int *have_match, struct got_object_id *id,
1669 struct got_commit_object *commit, regex_t *regex)
1671 const struct got_error *err = NULL;
1672 regmatch_t regmatch;
1673 char *id_str = NULL, *logmsg = NULL;
1677 err = got_object_id_str(&id_str, id);
1681 err = got_object_commit_get_logmsg(&logmsg, commit);
1685 if (regexec(regex, got_object_commit_get_author(commit), 1,
1686 ®match, 0) == 0 ||
1687 regexec(regex, got_object_commit_get_committer(commit), 1,
1688 ®match, 0) == 0 ||
1689 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1690 regexec(regex, logmsg, 1, ®match, 0) == 0)
1698 static const struct got_error *
1699 queue_commits(struct tog_log_thread_args *a)
1701 const struct got_error *err = NULL;
1704 * We keep all commits open throughout the lifetime of the log
1705 * view in order to avoid having to re-fetch commits from disk
1706 * while updating the display.
1709 struct got_object_id *id;
1710 struct got_commit_object *commit;
1711 struct commit_queue_entry *entry;
1714 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1716 if (err || id == NULL)
1719 err = got_object_open_as_commit(&commit, a->repo, id);
1722 entry = alloc_commit_queue_entry(commit, id);
1723 if (entry == NULL) {
1724 err = got_error_from_errno("alloc_commit_queue_entry");
1728 errcode = pthread_mutex_lock(&tog_mutex);
1730 err = got_error_set_errno(errcode,
1731 "pthread_mutex_lock");
1735 entry->idx = a->commits->ncommits;
1736 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1737 a->commits->ncommits++;
1739 if (*a->searching == TOG_SEARCH_FORWARD &&
1740 !*a->search_next_done) {
1742 err = match_commit(&have_match, id, commit, a->regex);
1746 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1749 errcode = pthread_mutex_unlock(&tog_mutex);
1750 if (errcode && err == NULL)
1751 err = got_error_set_errno(errcode,
1752 "pthread_mutex_unlock");
1755 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1761 select_commit(struct tog_log_view_state *s)
1763 struct commit_queue_entry *entry;
1766 entry = s->first_displayed_entry;
1768 if (ncommits == s->selected) {
1769 s->selected_entry = entry;
1772 entry = TAILQ_NEXT(entry, entry);
1777 static const struct got_error *
1778 draw_commits(struct tog_view *view)
1780 const struct got_error *err = NULL;
1781 struct tog_log_view_state *s = &view->state.log;
1782 struct commit_queue_entry *entry = s->selected_entry;
1783 const int limit = view->nlines;
1785 int ncommits, author_cols = 4;
1786 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1787 char *refs_str = NULL;
1789 struct tog_color *tc;
1790 static const size_t date_display_cols = 12;
1792 if (s->selected_entry &&
1793 !(view->searching && view->search_next_done == 0)) {
1794 struct got_reflist_head *refs;
1795 err = got_object_id_str(&id_str, s->selected_entry->id);
1798 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1799 s->selected_entry->id);
1801 err = build_refs_str(&refs_str, refs,
1802 s->selected_entry->id, s->repo);
1808 if (s->thread_args.commits_needed == 0)
1809 halfdelay(10); /* disable fast refresh */
1811 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1812 if (asprintf(&ncommits_str, " [%d/%d] %s",
1813 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1814 (view->searching && !view->search_next_done) ?
1815 "searching..." : "loading...") == -1) {
1816 err = got_error_from_errno("asprintf");
1820 const char *search_str = NULL;
1822 if (view->searching) {
1823 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1824 search_str = "no more matches";
1825 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1826 search_str = "no matches found";
1827 else if (!view->search_next_done)
1828 search_str = "searching...";
1831 if (asprintf(&ncommits_str, " [%d/%d] %s",
1832 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1833 search_str ? search_str :
1834 (refs_str ? refs_str : "")) == -1) {
1835 err = got_error_from_errno("asprintf");
1840 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1841 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1842 "........................................",
1843 s->in_repo_path, ncommits_str) == -1) {
1844 err = got_error_from_errno("asprintf");
1848 } else if (asprintf(&header, "commit %s%s",
1849 id_str ? id_str : "........................................",
1850 ncommits_str) == -1) {
1851 err = got_error_from_errno("asprintf");
1855 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1859 werase(view->window);
1861 if (view_needs_focus_indication(view))
1862 wstandout(view->window);
1863 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1865 wattr_on(view->window,
1866 COLOR_PAIR(tc->colorpair), NULL);
1867 waddwstr(view->window, wline);
1869 wattr_off(view->window,
1870 COLOR_PAIR(tc->colorpair), NULL);
1871 while (width < view->ncols) {
1872 waddch(view->window, ' ');
1875 if (view_needs_focus_indication(view))
1876 wstandend(view->window);
1881 /* Grow author column size if necessary, and set view->maxx. */
1882 entry = s->first_displayed_entry;
1886 char *author, *eol, *msg, *msg0;
1887 wchar_t *wauthor, *wmsg;
1889 if (ncommits >= limit - 1)
1891 author = strdup(got_object_commit_get_author(entry->commit));
1892 if (author == NULL) {
1893 err = got_error_from_errno("strdup");
1896 err = format_author(&wauthor, &width, author, COLS,
1898 if (author_cols < width)
1899 author_cols = width;
1902 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1906 while (*msg == '\n')
1908 if ((eol = strchr(msg, '\n')))
1910 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1911 date_display_cols + author_cols, 0);
1914 view->maxx = MAX(view->maxx, width);
1918 entry = TAILQ_NEXT(entry, entry);
1921 entry = s->first_displayed_entry;
1922 s->last_displayed_entry = s->first_displayed_entry;
1925 if (ncommits >= limit - 1)
1927 if (ncommits == s->selected)
1928 wstandout(view->window);
1929 err = draw_commit(view, entry->commit, entry->id,
1930 date_display_cols, author_cols);
1931 if (ncommits == s->selected)
1932 wstandend(view->window);
1936 s->last_displayed_entry = entry;
1937 entry = TAILQ_NEXT(entry, entry);
1952 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1954 struct commit_queue_entry *entry;
1957 entry = TAILQ_FIRST(&s->commits.head);
1958 if (s->first_displayed_entry == entry)
1961 entry = s->first_displayed_entry;
1962 while (entry && nscrolled < maxscroll) {
1963 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1965 s->first_displayed_entry = entry;
1971 static const struct got_error *
1972 trigger_log_thread(struct tog_view *view, int wait)
1974 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1977 halfdelay(1); /* fast refresh while loading commits */
1979 while (ta->commits_needed > 0 || ta->load_all) {
1980 if (ta->log_complete)
1983 /* Wake the log thread. */
1984 errcode = pthread_cond_signal(&ta->need_commits);
1986 return got_error_set_errno(errcode,
1987 "pthread_cond_signal");
1990 * The mutex will be released while the view loop waits
1991 * in wgetch(), at which time the log thread will run.
1996 /* Display progress update in log view. */
1997 show_log_view(view);
2001 /* Wait right here while next commit is being loaded. */
2002 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
2004 return got_error_set_errno(errcode,
2005 "pthread_cond_wait");
2007 /* Display progress update in log view. */
2008 show_log_view(view);
2016 static const struct got_error *
2017 log_scroll_down(struct tog_view *view, int maxscroll)
2019 struct tog_log_view_state *s = &view->state.log;
2020 const struct got_error *err = NULL;
2021 struct commit_queue_entry *pentry;
2022 int nscrolled = 0, ncommits_needed;
2024 if (s->last_displayed_entry == NULL)
2027 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
2028 if (s->commits.ncommits < ncommits_needed &&
2029 !s->thread_args.log_complete) {
2031 * Ask the log thread for required amount of commits.
2033 s->thread_args.commits_needed += maxscroll;
2034 err = trigger_log_thread(view, 1);
2040 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
2044 s->last_displayed_entry = pentry;
2046 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
2049 s->first_displayed_entry = pentry;
2050 } while (++nscrolled < maxscroll);
2055 static const struct got_error *
2056 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
2057 struct got_commit_object *commit, struct got_object_id *commit_id,
2058 struct tog_view *log_view, struct got_repository *repo)
2060 const struct got_error *err;
2061 struct got_object_qid *parent_id;
2062 struct tog_view *diff_view;
2064 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2065 if (diff_view == NULL)
2066 return got_error_from_errno("view_open");
2068 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2069 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2070 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2072 *new_view = diff_view;
2076 static const struct got_error *
2077 tree_view_visit_subtree(struct tog_tree_view_state *s,
2078 struct got_tree_object *subtree)
2080 struct tog_parent_tree *parent;
2082 parent = calloc(1, sizeof(*parent));
2084 return got_error_from_errno("calloc");
2086 parent->tree = s->tree;
2087 parent->first_displayed_entry = s->first_displayed_entry;
2088 parent->selected_entry = s->selected_entry;
2089 parent->selected = s->selected;
2090 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2093 s->first_displayed_entry = NULL;
2097 static const struct got_error *
2098 tree_view_walk_path(struct tog_tree_view_state *s,
2099 struct got_commit_object *commit, const char *path)
2101 const struct got_error *err = NULL;
2102 struct got_tree_object *tree = NULL;
2104 char *slash, *subpath = NULL;
2106 /* Walk the path and open corresponding tree objects. */
2109 struct got_tree_entry *te;
2110 struct got_object_id *tree_id;
2116 /* Ensure the correct subtree entry is selected. */
2117 slash = strchr(p, '/');
2119 te_name = strdup(p);
2121 te_name = strndup(p, slash - p);
2122 if (te_name == NULL) {
2123 err = got_error_from_errno("strndup");
2126 te = got_object_tree_find_entry(s->tree, te_name);
2128 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2133 s->first_displayed_entry = s->selected_entry = te;
2135 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2136 break; /* jump to this file's entry */
2138 slash = strchr(p, '/');
2140 subpath = strndup(path, slash - path);
2142 subpath = strdup(path);
2143 if (subpath == NULL) {
2144 err = got_error_from_errno("strdup");
2148 err = got_object_id_by_path(&tree_id, s->repo, commit,
2153 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2158 err = tree_view_visit_subtree(s, tree);
2160 got_object_tree_close(tree);
2174 static const struct got_error *
2175 browse_commit_tree(struct tog_view **new_view, int begin_x,
2176 struct commit_queue_entry *entry, const char *path,
2177 const char *head_ref_name, struct got_repository *repo)
2179 const struct got_error *err = NULL;
2180 struct tog_tree_view_state *s;
2181 struct tog_view *tree_view;
2183 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2184 if (tree_view == NULL)
2185 return got_error_from_errno("view_open");
2187 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2190 s = &tree_view->state.tree;
2192 *new_view = tree_view;
2194 if (got_path_is_root_dir(path))
2197 return tree_view_walk_path(s, entry->commit, path);
2200 static const struct got_error *
2201 block_signals_used_by_main_thread(void)
2206 if (sigemptyset(&sigset) == -1)
2207 return got_error_from_errno("sigemptyset");
2209 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2210 if (sigaddset(&sigset, SIGWINCH) == -1)
2211 return got_error_from_errno("sigaddset");
2212 if (sigaddset(&sigset, SIGCONT) == -1)
2213 return got_error_from_errno("sigaddset");
2214 if (sigaddset(&sigset, SIGINT) == -1)
2215 return got_error_from_errno("sigaddset");
2216 if (sigaddset(&sigset, SIGTERM) == -1)
2217 return got_error_from_errno("sigaddset");
2219 /* ncurses handles SIGTSTP */
2220 if (sigaddset(&sigset, SIGTSTP) == -1)
2221 return got_error_from_errno("sigaddset");
2223 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2225 return got_error_set_errno(errcode, "pthread_sigmask");
2231 log_thread(void *arg)
2233 const struct got_error *err = NULL;
2235 struct tog_log_thread_args *a = arg;
2238 err = block_signals_used_by_main_thread();
2242 while (!done && !err && !tog_fatal_signal_received()) {
2243 err = queue_commits(a);
2245 if (err->code != GOT_ERR_ITER_COMPLETED)
2249 } else if (a->commits_needed > 0 && !a->load_all)
2250 a->commits_needed--;
2252 errcode = pthread_mutex_lock(&tog_mutex);
2254 err = got_error_set_errno(errcode,
2255 "pthread_mutex_lock");
2257 } else if (*a->quit)
2259 else if (*a->first_displayed_entry == NULL) {
2260 *a->first_displayed_entry =
2261 TAILQ_FIRST(&a->commits->head);
2262 *a->selected_entry = *a->first_displayed_entry;
2265 errcode = pthread_cond_signal(&a->commit_loaded);
2267 err = got_error_set_errno(errcode,
2268 "pthread_cond_signal");
2269 pthread_mutex_unlock(&tog_mutex);
2274 a->commits_needed = 0;
2276 if (a->commits_needed == 0 && !a->load_all) {
2277 errcode = pthread_cond_wait(&a->need_commits,
2280 err = got_error_set_errno(errcode,
2281 "pthread_cond_wait");
2287 errcode = pthread_mutex_unlock(&tog_mutex);
2288 if (errcode && err == NULL)
2289 err = got_error_set_errno(errcode,
2290 "pthread_mutex_unlock");
2292 a->log_complete = 1;
2296 static const struct got_error *
2297 stop_log_thread(struct tog_log_view_state *s)
2299 const struct got_error *err = NULL;
2304 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2306 return got_error_set_errno(errcode,
2307 "pthread_cond_signal");
2308 errcode = pthread_mutex_unlock(&tog_mutex);
2310 return got_error_set_errno(errcode,
2311 "pthread_mutex_unlock");
2312 errcode = pthread_join(s->thread, (void **)&err);
2314 return got_error_set_errno(errcode, "pthread_join");
2315 errcode = pthread_mutex_lock(&tog_mutex);
2317 return got_error_set_errno(errcode,
2318 "pthread_mutex_lock");
2319 s->thread = 0; //NULL;
2322 if (s->thread_args.repo) {
2323 err = got_repo_close(s->thread_args.repo);
2324 s->thread_args.repo = NULL;
2327 if (s->thread_args.pack_fds) {
2328 const struct got_error *pack_err =
2329 got_repo_pack_fds_close(s->thread_args.pack_fds);
2332 s->thread_args.pack_fds = NULL;
2335 if (s->thread_args.graph) {
2336 got_commit_graph_close(s->thread_args.graph);
2337 s->thread_args.graph = NULL;
2343 static const struct got_error *
2344 close_log_view(struct tog_view *view)
2346 const struct got_error *err = NULL;
2347 struct tog_log_view_state *s = &view->state.log;
2350 err = stop_log_thread(s);
2352 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2353 if (errcode && err == NULL)
2354 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2356 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2357 if (errcode && err == NULL)
2358 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2360 free_commits(&s->commits);
2361 free(s->in_repo_path);
2362 s->in_repo_path = NULL;
2365 free(s->head_ref_name);
2366 s->head_ref_name = NULL;
2370 static const struct got_error *
2371 search_start_log_view(struct tog_view *view)
2373 struct tog_log_view_state *s = &view->state.log;
2375 s->matched_entry = NULL;
2376 s->search_entry = NULL;
2380 static const struct got_error *
2381 search_next_log_view(struct tog_view *view)
2383 const struct got_error *err = NULL;
2384 struct tog_log_view_state *s = &view->state.log;
2385 struct commit_queue_entry *entry;
2387 /* Display progress update in log view. */
2388 show_log_view(view);
2392 if (s->search_entry) {
2394 errcode = pthread_mutex_unlock(&tog_mutex);
2396 return got_error_set_errno(errcode,
2397 "pthread_mutex_unlock");
2398 ch = wgetch(view->window);
2399 errcode = pthread_mutex_lock(&tog_mutex);
2401 return got_error_set_errno(errcode,
2402 "pthread_mutex_lock");
2403 if (ch == KEY_BACKSPACE) {
2404 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2407 if (view->searching == TOG_SEARCH_FORWARD)
2408 entry = TAILQ_NEXT(s->search_entry, entry);
2410 entry = TAILQ_PREV(s->search_entry,
2411 commit_queue_head, entry);
2412 } else if (s->matched_entry) {
2413 int matched_idx = s->matched_entry->idx;
2414 int selected_idx = s->selected_entry->idx;
2417 * If the user has moved the cursor after we hit a match,
2418 * the position from where we should continue searching
2419 * might have changed.
2421 if (view->searching == TOG_SEARCH_FORWARD) {
2422 if (matched_idx > selected_idx)
2423 entry = TAILQ_NEXT(s->selected_entry, entry);
2425 entry = TAILQ_NEXT(s->matched_entry, entry);
2427 if (matched_idx < selected_idx)
2428 entry = TAILQ_PREV(s->selected_entry,
2429 commit_queue_head, entry);
2431 entry = TAILQ_PREV(s->matched_entry,
2432 commit_queue_head, entry);
2435 entry = s->selected_entry;
2441 if (entry == NULL) {
2442 if (s->thread_args.log_complete ||
2443 view->searching == TOG_SEARCH_BACKWARD) {
2444 view->search_next_done =
2445 (s->matched_entry == NULL ?
2446 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2447 s->search_entry = NULL;
2451 * Poke the log thread for more commits and return,
2452 * allowing the main loop to make progress. Search
2453 * will resume at s->search_entry once we come back.
2455 s->thread_args.commits_needed++;
2456 return trigger_log_thread(view, 0);
2459 err = match_commit(&have_match, entry->id, entry->commit,
2464 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2465 s->matched_entry = entry;
2469 s->search_entry = entry;
2470 if (view->searching == TOG_SEARCH_FORWARD)
2471 entry = TAILQ_NEXT(entry, entry);
2473 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2476 if (s->matched_entry) {
2477 int cur = s->selected_entry->idx;
2478 while (cur < s->matched_entry->idx) {
2479 err = input_log_view(NULL, view, KEY_DOWN);
2484 while (cur > s->matched_entry->idx) {
2485 err = input_log_view(NULL, view, KEY_UP);
2492 s->search_entry = NULL;
2497 static const struct got_error *
2498 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2499 struct got_repository *repo, const char *head_ref_name,
2500 const char *in_repo_path, int log_branches)
2502 const struct got_error *err = NULL;
2503 struct tog_log_view_state *s = &view->state.log;
2504 struct got_repository *thread_repo = NULL;
2505 struct got_commit_graph *thread_graph = NULL;
2508 if (in_repo_path != s->in_repo_path) {
2509 free(s->in_repo_path);
2510 s->in_repo_path = strdup(in_repo_path);
2511 if (s->in_repo_path == NULL)
2512 return got_error_from_errno("strdup");
2515 /* The commit queue only contains commits being displayed. */
2516 TAILQ_INIT(&s->commits.head);
2517 s->commits.ncommits = 0;
2520 if (head_ref_name) {
2521 s->head_ref_name = strdup(head_ref_name);
2522 if (s->head_ref_name == NULL) {
2523 err = got_error_from_errno("strdup");
2527 s->start_id = got_object_id_dup(start_id);
2528 if (s->start_id == NULL) {
2529 err = got_error_from_errno("got_object_id_dup");
2532 s->log_branches = log_branches;
2534 STAILQ_INIT(&s->colors);
2535 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2536 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2537 get_color_value("TOG_COLOR_COMMIT"));
2540 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2541 get_color_value("TOG_COLOR_AUTHOR"));
2543 free_colors(&s->colors);
2546 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2547 get_color_value("TOG_COLOR_DATE"));
2549 free_colors(&s->colors);
2554 view->show = show_log_view;
2555 view->input = input_log_view;
2556 view->close = close_log_view;
2557 view->search_start = search_start_log_view;
2558 view->search_next = search_next_log_view;
2560 if (s->thread_args.pack_fds == NULL) {
2561 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2565 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2566 s->thread_args.pack_fds);
2569 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2573 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2574 s->repo, NULL, NULL);
2578 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2580 err = got_error_set_errno(errcode, "pthread_cond_init");
2583 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2585 err = got_error_set_errno(errcode, "pthread_cond_init");
2589 s->thread_args.commits_needed = view->nlines;
2590 s->thread_args.graph = thread_graph;
2591 s->thread_args.commits = &s->commits;
2592 s->thread_args.in_repo_path = s->in_repo_path;
2593 s->thread_args.start_id = s->start_id;
2594 s->thread_args.repo = thread_repo;
2595 s->thread_args.log_complete = 0;
2596 s->thread_args.quit = &s->quit;
2597 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2598 s->thread_args.selected_entry = &s->selected_entry;
2599 s->thread_args.searching = &view->searching;
2600 s->thread_args.search_next_done = &view->search_next_done;
2601 s->thread_args.regex = &view->regex;
2604 close_log_view(view);
2608 static const struct got_error *
2609 show_log_view(struct tog_view *view)
2611 const struct got_error *err;
2612 struct tog_log_view_state *s = &view->state.log;
2614 if (s->thread == 0) { //NULL) {
2615 int errcode = pthread_create(&s->thread, NULL, log_thread,
2618 return got_error_set_errno(errcode, "pthread_create");
2619 if (s->thread_args.commits_needed > 0) {
2620 err = trigger_log_thread(view, 1);
2626 return draw_commits(view);
2629 static const struct got_error *
2630 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2632 const struct got_error *err = NULL;
2633 struct tog_log_view_state *s = &view->state.log;
2634 struct tog_view *diff_view = NULL, *tree_view = NULL;
2635 struct tog_view *ref_view = NULL;
2636 struct commit_queue_entry *entry;
2637 int begin_x = 0, n, nscroll = view->nlines - 1;
2639 if (s->thread_args.load_all) {
2640 if (ch == KEY_BACKSPACE)
2641 s->thread_args.load_all = 0;
2642 else if (s->thread_args.log_complete) {
2643 s->thread_args.load_all = 0;
2644 log_scroll_down(view, s->commits.ncommits);
2645 s->selected = MIN(view->nlines - 2,
2646 s->commits.ncommits - 1);
2660 view->x = MAX(view->maxx - view->ncols / 2, 0);
2665 if (view->x + view->ncols / 2 < view->maxx)
2666 view->x += 2; /* move two columns right */
2672 view->x -= MIN(view->x, 2); /* move two columns back */
2681 if (s->selected_entry->idx == 0)
2683 if (s->first_displayed_entry == NULL)
2685 if (s->selected > 0)
2688 log_scroll_up(s, 1);
2694 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2705 if (s->first_displayed_entry == NULL)
2707 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2708 s->selected = MAX(0, s->selected - nscroll - 1);
2710 log_scroll_up(s, nscroll);
2712 if (s->selected_entry->idx == 0)
2720 if (s->first_displayed_entry == NULL)
2722 if (s->selected < MIN(view->nlines - 2,
2723 s->commits.ncommits - 1))
2726 err = log_scroll_down(view, 1);
2731 if (s->thread_args.log_complete &&
2732 s->selected_entry->idx == s->commits.ncommits - 1)
2737 /* We don't know yet how many commits, so we're forced to
2738 * traverse them all. */
2740 if (!s->thread_args.log_complete) {
2741 s->thread_args.load_all = 1;
2742 return trigger_log_thread(view, 0);
2746 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2747 for (n = 0; n < view->nlines - 1; n++) {
2750 s->first_displayed_entry = entry;
2751 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2754 s->selected = n - 1;
2766 struct commit_queue_entry *first;
2767 first = s->first_displayed_entry;
2768 if (first == NULL) {
2772 err = log_scroll_down(view, nscroll);
2775 if (first == s->first_displayed_entry &&
2776 s->selected < MIN(view->nlines - 2,
2777 s->commits.ncommits - 1)) {
2778 /* can't scroll further down */
2779 s->selected += MIN(s->last_displayed_entry->idx -
2780 s->selected_entry->idx, nscroll + 1);
2783 if (s->thread_args.log_complete &&
2784 s->selected_entry->idx == s->commits.ncommits - 1)
2789 if (s->selected > view->nlines - 2)
2790 s->selected = view->nlines - 2;
2791 if (s->selected > s->commits.ncommits - 1)
2792 s->selected = s->commits.ncommits - 1;
2794 if (s->commits.ncommits < view->nlines - 1 &&
2795 !s->thread_args.log_complete) {
2796 s->thread_args.commits_needed += (view->nlines - 1) -
2797 s->commits.ncommits;
2798 err = trigger_log_thread(view, 1);
2804 if (s->selected_entry == NULL)
2806 if (view_is_parent_view(view))
2807 begin_x = view_split_begin_x(view->begin_x);
2808 err = open_diff_view_for_commit(&diff_view, begin_x,
2809 s->selected_entry->commit, s->selected_entry->id,
2814 diff_view->focussed = 1;
2815 if (view_is_parent_view(view)) {
2816 err = view_close_child(view);
2819 err = view_set_child(view, diff_view);
2822 view->focus_child = 1;
2824 *new_view = diff_view;
2828 if (s->selected_entry == NULL)
2830 if (view_is_parent_view(view))
2831 begin_x = view_split_begin_x(view->begin_x);
2832 err = browse_commit_tree(&tree_view, begin_x,
2833 s->selected_entry, s->in_repo_path, s->head_ref_name,
2838 tree_view->focussed = 1;
2839 if (view_is_parent_view(view)) {
2840 err = view_close_child(view);
2843 err = view_set_child(view, tree_view);
2846 view->focus_child = 1;
2848 *new_view = tree_view;
2854 if (ch == KEY_BACKSPACE &&
2855 got_path_is_root_dir(s->in_repo_path))
2857 err = stop_log_thread(s);
2860 if (ch == KEY_BACKSPACE) {
2862 err = got_path_dirname(&parent_path, s->in_repo_path);
2865 free(s->in_repo_path);
2866 s->in_repo_path = parent_path;
2867 s->thread_args.in_repo_path = s->in_repo_path;
2868 } else if (ch == CTRL('l')) {
2869 struct got_object_id *start_id;
2870 err = got_repo_match_object_id(&start_id, NULL,
2871 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2872 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2876 s->start_id = start_id;
2877 s->thread_args.start_id = s->start_id;
2879 s->log_branches = !s->log_branches;
2881 if (s->thread_args.pack_fds == NULL) {
2882 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2886 err = got_repo_open(&s->thread_args.repo,
2887 got_repo_get_path(s->repo), NULL,
2888 s->thread_args.pack_fds);
2892 err = tog_load_refs(s->repo, 0);
2895 err = got_commit_graph_open(&s->thread_args.graph,
2896 s->in_repo_path, !s->log_branches);
2899 err = got_commit_graph_iter_start(s->thread_args.graph,
2900 s->start_id, s->repo, NULL, NULL);
2903 free_commits(&s->commits);
2904 s->first_displayed_entry = NULL;
2905 s->last_displayed_entry = NULL;
2906 s->selected_entry = NULL;
2908 s->thread_args.log_complete = 0;
2910 s->thread_args.commits_needed = view->nlines;
2911 s->matched_entry = NULL;
2912 s->search_entry = NULL;
2916 if (view_is_parent_view(view))
2917 begin_x = view_split_begin_x(view->begin_x);
2918 ref_view = view_open(view->nlines, view->ncols,
2919 view->begin_y, begin_x, TOG_VIEW_REF);
2920 if (ref_view == NULL)
2921 return got_error_from_errno("view_open");
2922 err = open_ref_view(ref_view, s->repo);
2924 view_close(ref_view);
2928 ref_view->focussed = 1;
2929 if (view_is_parent_view(view)) {
2930 err = view_close_child(view);
2933 err = view_set_child(view, ref_view);
2936 view->focus_child = 1;
2938 *new_view = ref_view;
2948 static const struct got_error *
2949 apply_unveil(const char *repo_path, const char *worktree_path)
2951 const struct got_error *error;
2954 if (unveil("gmon.out", "rwc") != 0)
2955 return got_error_from_errno2("unveil", "gmon.out");
2957 if (repo_path && unveil(repo_path, "r") != 0)
2958 return got_error_from_errno2("unveil", repo_path);
2960 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2961 return got_error_from_errno2("unveil", worktree_path);
2963 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2964 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2966 error = got_privsep_unveil_exec_helpers();
2970 if (unveil(NULL, NULL) != 0)
2971 return got_error_from_errno("unveil");
2980 * Override default signal handlers before starting ncurses.
2981 * This should prevent ncurses from installing its own
2982 * broken cleanup() signal handler.
2984 signal(SIGWINCH, tog_sigwinch);
2985 signal(SIGPIPE, tog_sigpipe);
2986 signal(SIGCONT, tog_sigcont);
2987 signal(SIGINT, tog_sigint);
2988 signal(SIGTERM, tog_sigterm);
2992 halfdelay(1); /* Do fast refresh while initial view is loading. */
2995 intrflush(stdscr, FALSE);
2996 keypad(stdscr, TRUE);
2998 if (getenv("TOG_COLORS") != NULL) {
3000 use_default_colors();
3004 static const struct got_error *
3005 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
3006 struct got_repository *repo, struct got_worktree *worktree)
3008 const struct got_error *err = NULL;
3011 *in_repo_path = strdup("/");
3012 if (*in_repo_path == NULL)
3013 return got_error_from_errno("strdup");
3018 const char *prefix = got_worktree_get_path_prefix(worktree);
3021 err = got_worktree_resolve_path(&p, worktree, argv[0]);
3024 if (asprintf(in_repo_path, "%s%s%s", prefix,
3025 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
3027 err = got_error_from_errno("asprintf");
3028 *in_repo_path = NULL;
3032 err = got_repo_map_path(in_repo_path, repo, argv[0]);
3037 static const struct got_error *
3038 cmd_log(int argc, char *argv[])
3040 const struct got_error *error;
3041 struct got_repository *repo = NULL;
3042 struct got_worktree *worktree = NULL;
3043 struct got_object_id *start_id = NULL;
3044 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
3045 char *start_commit = NULL, *label = NULL;
3046 struct got_reference *ref = NULL;
3047 const char *head_ref_name = NULL;
3048 int ch, log_branches = 0;
3049 struct tog_view *view;
3050 int *pack_fds = NULL;
3052 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
3058 start_commit = optarg;
3061 repo_path = realpath(optarg, NULL);
3062 if (repo_path == NULL)
3063 return got_error_from_errno2("realpath",
3078 error = got_repo_pack_fds_open(&pack_fds);
3082 if (repo_path == NULL) {
3083 cwd = getcwd(NULL, 0);
3085 return got_error_from_errno("getcwd");
3086 error = got_worktree_open(&worktree, cwd);
3087 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3091 strdup(got_worktree_get_repo_path(worktree));
3093 repo_path = strdup(cwd);
3094 if (repo_path == NULL) {
3095 error = got_error_from_errno("strdup");
3100 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3104 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3111 error = apply_unveil(got_repo_get_path(repo),
3112 worktree ? got_worktree_get_root_path(worktree) : NULL);
3116 /* already loaded by tog_log_with_path()? */
3117 if (TAILQ_EMPTY(&tog_refs)) {
3118 error = tog_load_refs(repo, 0);
3123 if (start_commit == NULL) {
3124 error = got_repo_match_object_id(&start_id, &label,
3125 worktree ? got_worktree_get_head_ref_name(worktree) :
3126 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3129 head_ref_name = label;
3131 error = got_ref_open(&ref, repo, start_commit, 0);
3133 head_ref_name = got_ref_get_name(ref);
3134 else if (error->code != GOT_ERR_NOT_REF)
3136 error = got_repo_match_object_id(&start_id, NULL,
3137 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3142 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3144 error = got_error_from_errno("view_open");
3147 error = open_log_view(view, start_id, repo, head_ref_name,
3148 in_repo_path, log_branches);
3152 /* Release work tree lock. */
3153 got_worktree_close(worktree);
3156 error = view_loop(view);
3166 const struct got_error *close_err = got_repo_close(repo);
3171 got_worktree_close(worktree);
3173 const struct got_error *pack_err =
3174 got_repo_pack_fds_close(pack_fds);
3186 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3187 "[-w] object1 object2\n", getprogname());
3192 match_line(const char *line, regex_t *regex, size_t nmatch,
3193 regmatch_t *regmatch)
3195 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3198 static struct tog_color *
3199 match_color(struct tog_colors *colors, const char *line)
3201 struct tog_color *tc = NULL;
3203 STAILQ_FOREACH(tc, colors, entry) {
3204 if (match_line(line, &tc->regex, 0, NULL))
3211 static const struct got_error *
3212 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3213 WINDOW *window, int skipcol, regmatch_t *regmatch)
3215 const struct got_error *err = NULL;
3217 wchar_t *wline = NULL;
3218 int rme, rms, n, width, scrollx;
3219 int width0 = 0, width1 = 0, width2 = 0;
3220 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3224 rms = regmatch->rm_so;
3225 rme = regmatch->rm_eo;
3227 err = expand_tab(&exstr, line);
3231 /* Split the line into 3 segments, according to match offsets. */
3232 seg0 = strndup(exstr, rms);
3234 err = got_error_from_errno("strndup");
3237 seg1 = strndup(exstr + rms, rme - rms);
3239 err = got_error_from_errno("strndup");
3242 seg2 = strdup(exstr + rme);
3244 err = got_error_from_errno("strndup");
3248 /* draw up to matched token if we haven't scrolled past it */
3249 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3253 n = MAX(width0 - skipcol, 0);
3256 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3257 wlimit, col_tab_align, 1);
3260 waddwstr(window, &wline[scrollx]);
3270 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3274 wlen = wcslen(wline);
3276 width = wcwidth(wline[i]);
3278 /* should not happen, tabs are expanded */
3279 err = got_error(GOT_ERR_RANGE);
3282 if (width0 + w + width > skipcol)
3287 /* draw (visible part of) matched token (if scrolled into it) */
3288 if (width1 - w > 0) {
3289 wattron(window, A_STANDOUT);
3290 waddwstr(window, &wline[i]);
3291 wattroff(window, A_STANDOUT);
3292 wlimit -= (width1 - w);
3293 *wtotal += (width1 - w);
3297 if (wlimit > 0) { /* draw rest of line */
3299 if (skipcol > width0 + width1) {
3300 err = format_line(&wline, &width2, &scrollx, seg2,
3301 skipcol - (width0 + width1), wlimit,
3305 waddwstr(window, &wline[scrollx]);
3307 err = format_line(&wline, &width2, NULL, seg2, 0,
3308 wlimit, col_tab_align, 1);
3311 waddwstr(window, wline);
3324 static const struct got_error *
3325 draw_file(struct tog_view *view, const char *header)
3327 struct tog_diff_view_state *s = &view->state.diff;
3328 regmatch_t *regmatch = &view->regmatch;
3329 const struct got_error *err;
3332 size_t linesize = 0;
3334 struct tog_color *tc;
3337 int max_lines = view->nlines;
3338 int nlines = s->nlines;
3341 line_offset = s->line_offsets[s->first_displayed_line - 1];
3342 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3343 return got_error_from_errno("fseek");
3345 werase(view->window);
3348 if (asprintf(&line, "[%d/%d] %s",
3349 s->first_displayed_line - 1 + s->selected_line, nlines,
3351 return got_error_from_errno("asprintf");
3352 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3358 if (view_needs_focus_indication(view))
3359 wstandout(view->window);
3360 waddwstr(view->window, wline);
3363 if (view_needs_focus_indication(view))
3364 wstandend(view->window);
3365 if (width <= view->ncols - 1)
3366 waddch(view->window, '\n');
3376 while (max_lines > 0 && nprinted < max_lines) {
3377 linelen = getline(&line, &linesize, s->f);
3378 if (linelen == -1) {
3384 return got_ferror(s->f, GOT_ERR_IO);
3387 /* Set view->maxx based on full line length. */
3388 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3394 view->maxx = MAX(view->maxx, width);
3398 tc = match_color(&s->colors, line);
3400 wattr_on(view->window,
3401 COLOR_PAIR(tc->colorpair), NULL);
3402 if (s->first_displayed_line + nprinted == s->matched_line &&
3403 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3404 err = add_matched_line(&width, line, view->ncols, 0,
3405 view->window, view->x, regmatch);
3412 err = format_line(&wline, &width, &skip, line,
3413 view->x, view->ncols, 0, view->x ? 1 : 0);
3418 waddwstr(view->window, &wline[skip]);
3423 wattr_off(view->window,
3424 COLOR_PAIR(tc->colorpair), NULL);
3425 if (width <= view->ncols - 1)
3426 waddch(view->window, '\n');
3431 s->last_displayed_line = s->first_displayed_line +
3434 s->last_displayed_line = s->first_displayed_line;
3439 while (nprinted < view->nlines) {
3440 waddch(view->window, '\n');
3444 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3450 wstandout(view->window);
3451 waddwstr(view->window, wline);
3454 wstandend(view->window);
3461 get_datestr(time_t *time, char *datebuf)
3463 struct tm mytm, *tm;
3466 tm = gmtime_r(time, &mytm);
3469 s = asctime_r(tm, datebuf);
3472 p = strchr(s, '\n');
3478 static const struct got_error *
3479 get_changed_paths(struct got_pathlist_head *paths,
3480 struct got_commit_object *commit, struct got_repository *repo)
3482 const struct got_error *err = NULL;
3483 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3484 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3485 struct got_object_qid *qid;
3487 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3489 struct got_commit_object *pcommit;
3490 err = got_object_open_as_commit(&pcommit, repo,
3495 tree_id1 = got_object_id_dup(
3496 got_object_commit_get_tree_id(pcommit));
3497 if (tree_id1 == NULL) {
3498 got_object_commit_close(pcommit);
3499 return got_error_from_errno("got_object_id_dup");
3501 got_object_commit_close(pcommit);
3506 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3511 tree_id2 = got_object_commit_get_tree_id(commit);
3512 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3516 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3517 got_diff_tree_collect_changed_paths, paths, 0);
3520 got_object_tree_close(tree1);
3522 got_object_tree_close(tree2);
3527 static const struct got_error *
3528 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3532 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3534 return got_error_from_errno("reallocarray");
3536 (*line_offsets)[*nlines] = off;
3541 static const struct got_error *
3542 write_commit_info(off_t **line_offsets, size_t *nlines,
3543 struct got_object_id *commit_id, struct got_reflist_head *refs,
3544 struct got_repository *repo, FILE *outfile)
3546 const struct got_error *err = NULL;
3547 char datebuf[26], *datestr;
3548 struct got_commit_object *commit;
3549 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3550 time_t committer_time;
3551 const char *author, *committer;
3552 char *refs_str = NULL;
3553 struct got_pathlist_head changed_paths;
3554 struct got_pathlist_entry *pe;
3558 TAILQ_INIT(&changed_paths);
3561 err = build_refs_str(&refs_str, refs, commit_id, repo);
3566 err = got_object_open_as_commit(&commit, repo, commit_id);
3570 err = got_object_id_str(&id_str, commit_id);
3572 err = got_error_from_errno("got_object_id_str");
3576 err = add_line_offset(line_offsets, nlines, 0);
3580 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3581 refs_str ? refs_str : "", refs_str ? ")" : "");
3583 err = got_error_from_errno("fprintf");
3587 err = add_line_offset(line_offsets, nlines, outoff);
3591 n = fprintf(outfile, "from: %s\n",
3592 got_object_commit_get_author(commit));
3594 err = got_error_from_errno("fprintf");
3598 err = add_line_offset(line_offsets, nlines, outoff);
3602 committer_time = got_object_commit_get_committer_time(commit);
3603 datestr = get_datestr(&committer_time, datebuf);
3605 n = fprintf(outfile, "date: %s UTC\n", datestr);
3607 err = got_error_from_errno("fprintf");
3611 err = add_line_offset(line_offsets, nlines, outoff);
3615 author = got_object_commit_get_author(commit);
3616 committer = got_object_commit_get_committer(commit);
3617 if (strcmp(author, committer) != 0) {
3618 n = fprintf(outfile, "via: %s\n", committer);
3620 err = got_error_from_errno("fprintf");
3624 err = add_line_offset(line_offsets, nlines, outoff);
3628 if (got_object_commit_get_nparents(commit) > 1) {
3629 const struct got_object_id_queue *parent_ids;
3630 struct got_object_qid *qid;
3632 parent_ids = got_object_commit_get_parent_ids(commit);
3633 STAILQ_FOREACH(qid, parent_ids, entry) {
3634 err = got_object_id_str(&id_str, &qid->id);
3637 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3639 err = got_error_from_errno("fprintf");
3643 err = add_line_offset(line_offsets, nlines, outoff);
3651 err = got_object_commit_get_logmsg(&logmsg, commit);
3655 while ((line = strsep(&s, "\n")) != NULL) {
3656 n = fprintf(outfile, "%s\n", line);
3658 err = got_error_from_errno("fprintf");
3662 err = add_line_offset(line_offsets, nlines, outoff);
3667 err = get_changed_paths(&changed_paths, commit, repo);
3670 TAILQ_FOREACH(pe, &changed_paths, entry) {
3671 struct got_diff_changed_path *cp = pe->data;
3672 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3674 err = got_error_from_errno("fprintf");
3678 err = add_line_offset(line_offsets, nlines, outoff);
3681 free((char *)pe->path);
3685 fputc('\n', outfile);
3687 err = add_line_offset(line_offsets, nlines, outoff);
3689 got_pathlist_free(&changed_paths);
3693 got_object_commit_close(commit);
3695 free(*line_offsets);
3696 *line_offsets = NULL;
3702 static const struct got_error *
3703 create_diff(struct tog_diff_view_state *s)
3705 const struct got_error *err = NULL;
3709 free(s->line_offsets);
3710 s->line_offsets = malloc(sizeof(off_t));
3711 if (s->line_offsets == NULL)
3712 return got_error_from_errno("malloc");
3717 err = got_error_from_errno("got_opentemp");
3720 if (s->f && fclose(s->f) == EOF) {
3721 err = got_error_from_errno("fclose");
3727 err = got_object_get_type(&obj_type, s->repo, s->id1);
3729 err = got_object_get_type(&obj_type, s->repo, s->id2);
3734 case GOT_OBJ_TYPE_BLOB:
3735 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3736 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3737 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3740 case GOT_OBJ_TYPE_TREE:
3741 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3742 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3743 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3745 case GOT_OBJ_TYPE_COMMIT: {
3746 const struct got_object_id_queue *parent_ids;
3747 struct got_object_qid *pid;
3748 struct got_commit_object *commit2;
3749 struct got_reflist_head *refs;
3751 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3754 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3755 /* Show commit info if we're diffing to a parent/root commit. */
3756 if (s->id1 == NULL) {
3757 err = write_commit_info(&s->line_offsets, &s->nlines,
3758 s->id2, refs, s->repo, s->f);
3762 parent_ids = got_object_commit_get_parent_ids(commit2);
3763 STAILQ_FOREACH(pid, parent_ids, entry) {
3764 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3765 err = write_commit_info(
3766 &s->line_offsets, &s->nlines,
3767 s->id2, refs, s->repo, s->f);
3774 got_object_commit_close(commit2);
3776 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3777 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3778 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3782 err = got_error(GOT_ERR_OBJ_TYPE);
3788 if (s->f && fflush(s->f) != 0 && err == NULL)
3789 err = got_error_from_errno("fflush");
3794 diff_view_indicate_progress(struct tog_view *view)
3796 mvwaddstr(view->window, 0, 0, "diffing...");
3801 static const struct got_error *
3802 search_start_diff_view(struct tog_view *view)
3804 struct tog_diff_view_state *s = &view->state.diff;
3806 s->matched_line = 0;
3810 static const struct got_error *
3811 search_next_diff_view(struct tog_view *view)
3813 struct tog_diff_view_state *s = &view->state.diff;
3814 const struct got_error *err = NULL;
3817 size_t linesize = 0;
3820 if (!view->searching) {
3821 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3825 if (s->matched_line) {
3826 if (view->searching == TOG_SEARCH_FORWARD)
3827 lineno = s->matched_line + 1;
3829 lineno = s->matched_line - 1;
3831 lineno = s->first_displayed_line;
3836 if (lineno <= 0 || lineno > s->nlines) {
3837 if (s->matched_line == 0) {
3838 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3842 if (view->searching == TOG_SEARCH_FORWARD)
3848 offset = s->line_offsets[lineno - 1];
3849 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3851 return got_error_from_errno("fseeko");
3853 linelen = getline(&line, &linesize, s->f);
3854 if (linelen != -1) {
3856 err = expand_tab(&exstr, line);
3859 if (match_line(exstr, &view->regex, 1,
3861 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3862 s->matched_line = lineno;
3868 if (view->searching == TOG_SEARCH_FORWARD)
3875 if (s->matched_line) {
3876 s->first_displayed_line = s->matched_line;
3877 s->selected_line = 1;
3883 static const struct got_error *
3884 close_diff_view(struct tog_view *view)
3886 const struct got_error *err = NULL;
3887 struct tog_diff_view_state *s = &view->state.diff;
3893 if (s->f && fclose(s->f) == EOF)
3894 err = got_error_from_errno("fclose");
3896 if (s->f1 && fclose(s->f1) == EOF)
3897 err = got_error_from_errno("fclose");
3899 if (s->f2 && fclose(s->f2) == EOF)
3900 err = got_error_from_errno("fclose");
3902 free_colors(&s->colors);
3903 free(s->line_offsets);
3904 s->line_offsets = NULL;
3909 static const struct got_error *
3910 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3911 struct got_object_id *id2, const char *label1, const char *label2,
3912 int diff_context, int ignore_whitespace, int force_text_diff,
3913 struct tog_view *log_view, struct got_repository *repo)
3915 const struct got_error *err;
3916 struct tog_diff_view_state *s = &view->state.diff;
3918 memset(s, 0, sizeof(*s));
3920 if (id1 != NULL && id2 != NULL) {
3922 err = got_object_get_type(&type1, repo, id1);
3925 err = got_object_get_type(&type2, repo, id2);
3930 return got_error(GOT_ERR_OBJ_TYPE);
3932 s->first_displayed_line = 1;
3933 s->last_displayed_line = view->nlines;
3934 s->selected_line = 1;
3942 s->id1 = got_object_id_dup(id1);
3944 return got_error_from_errno("got_object_id_dup");
3948 s->id2 = got_object_id_dup(id2);
3949 if (s->id2 == NULL) {
3950 err = got_error_from_errno("got_object_id_dup");
3954 s->f1 = got_opentemp();
3955 if (s->f1 == NULL) {
3956 err = got_error_from_errno("got_opentemp");
3960 s->f2 = got_opentemp();
3961 if (s->f2 == NULL) {
3962 err = got_error_from_errno("got_opentemp");
3966 s->first_displayed_line = 1;
3967 s->last_displayed_line = view->nlines;
3968 s->diff_context = diff_context;
3969 s->ignore_whitespace = ignore_whitespace;
3970 s->force_text_diff = force_text_diff;
3971 s->log_view = log_view;
3974 STAILQ_INIT(&s->colors);
3975 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3976 err = add_color(&s->colors,
3977 "^-", TOG_COLOR_DIFF_MINUS,
3978 get_color_value("TOG_COLOR_DIFF_MINUS"));
3981 err = add_color(&s->colors, "^\\+",
3982 TOG_COLOR_DIFF_PLUS,
3983 get_color_value("TOG_COLOR_DIFF_PLUS"));
3986 err = add_color(&s->colors,
3987 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3988 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3992 err = add_color(&s->colors,
3993 "^(commit [0-9a-f]|parent [0-9]|"
3994 "(blob|file|tree|commit) [-+] |"
3995 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3996 get_color_value("TOG_COLOR_DIFF_META"));
4000 err = add_color(&s->colors,
4001 "^(from|via): ", TOG_COLOR_AUTHOR,
4002 get_color_value("TOG_COLOR_AUTHOR"));
4006 err = add_color(&s->colors,
4007 "^date: ", TOG_COLOR_DATE,
4008 get_color_value("TOG_COLOR_DATE"));
4013 if (log_view && view_is_splitscreen(view))
4014 show_log_view(log_view); /* draw vborder */
4015 diff_view_indicate_progress(view);
4017 err = create_diff(s);
4019 view->show = show_diff_view;
4020 view->input = input_diff_view;
4021 view->close = close_diff_view;
4022 view->search_start = search_start_diff_view;
4023 view->search_next = search_next_diff_view;
4026 close_diff_view(view);
4030 static const struct got_error *
4031 show_diff_view(struct tog_view *view)
4033 const struct got_error *err;
4034 struct tog_diff_view_state *s = &view->state.diff;
4035 char *id_str1 = NULL, *id_str2, *header;
4036 const char *label1, *label2;
4039 err = got_object_id_str(&id_str1, s->id1);
4042 label1 = s->label1 ? : id_str1;
4044 label1 = "/dev/null";
4046 err = got_object_id_str(&id_str2, s->id2);
4049 label2 = s->label2 ? : id_str2;
4051 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
4052 err = got_error_from_errno("asprintf");
4060 err = draw_file(view, header);
4065 static const struct got_error *
4066 set_selected_commit(struct tog_diff_view_state *s,
4067 struct commit_queue_entry *entry)
4069 const struct got_error *err;
4070 const struct got_object_id_queue *parent_ids;
4071 struct got_commit_object *selected_commit;
4072 struct got_object_qid *pid;
4075 s->id2 = got_object_id_dup(entry->id);
4077 return got_error_from_errno("got_object_id_dup");
4079 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
4082 parent_ids = got_object_commit_get_parent_ids(selected_commit);
4084 pid = STAILQ_FIRST(parent_ids);
4085 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4086 got_object_commit_close(selected_commit);
4090 static const struct got_error *
4091 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4093 const struct got_error *err = NULL;
4094 struct tog_diff_view_state *s = &view->state.diff;
4095 struct tog_log_view_state *ls;
4096 struct commit_queue_entry *old_selected_entry;
4098 size_t linesize = 0;
4100 int i, nscroll = view->nlines - 1;
4107 view->x = MAX(view->maxx - view->ncols / 3, 0);
4112 if (view->x + view->ncols / 3 < view->maxx)
4113 view->x += 2; /* move two columns right */
4119 view->x -= MIN(view->x, 2); /* move two columns back */
4126 s->force_text_diff = !s->force_text_diff;
4128 s->ignore_whitespace = !s->ignore_whitespace;
4129 wclear(view->window);
4130 s->first_displayed_line = 1;
4131 s->last_displayed_line = view->nlines;
4132 s->matched_line = 0;
4133 diff_view_indicate_progress(view);
4134 err = create_diff(s);
4139 s->first_displayed_line = 1;
4148 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4154 if (s->first_displayed_line > 1)
4155 s->first_displayed_line--;
4166 if (s->first_displayed_line == 1) {
4171 while (i++ < nscroll && s->first_displayed_line > 1)
4172 s->first_displayed_line--;
4178 s->first_displayed_line++;
4195 while (!s->eof && i++ < nscroll) {
4196 linelen = getline(&line, &linesize, s->f);
4197 s->first_displayed_line++;
4198 if (linelen == -1) {
4202 err = got_ferror(s->f, GOT_ERR_IO);
4209 if (s->diff_context > 0) {
4211 s->matched_line = 0;
4212 diff_view_indicate_progress(view);
4213 err = create_diff(s);
4214 if (s->first_displayed_line + view->nlines - 1 >
4216 s->first_displayed_line = 1;
4217 s->last_displayed_line = view->nlines;
4223 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4225 s->matched_line = 0;
4226 diff_view_indicate_progress(view);
4227 err = create_diff(s);
4233 if (s->log_view == NULL) {
4237 ls = &s->log_view->state.log;
4238 old_selected_entry = ls->selected_entry;
4240 /* view->count handled in input_log_view() */
4241 err = input_log_view(NULL, s->log_view, KEY_UP);
4245 if (old_selected_entry == ls->selected_entry)
4248 err = set_selected_commit(s, ls->selected_entry);
4252 s->first_displayed_line = 1;
4253 s->last_displayed_line = view->nlines;
4254 s->matched_line = 0;
4257 diff_view_indicate_progress(view);
4258 err = create_diff(s);
4262 if (s->log_view == NULL) {
4266 ls = &s->log_view->state.log;
4267 old_selected_entry = ls->selected_entry;
4269 /* view->count handled in input_log_view() */
4270 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4274 if (old_selected_entry == ls->selected_entry)
4277 err = set_selected_commit(s, ls->selected_entry);
4281 s->first_displayed_line = 1;
4282 s->last_displayed_line = view->nlines;
4283 s->matched_line = 0;
4286 diff_view_indicate_progress(view);
4287 err = create_diff(s);
4297 static const struct got_error *
4298 cmd_diff(int argc, char *argv[])
4300 const struct got_error *error = NULL;
4301 struct got_repository *repo = NULL;
4302 struct got_worktree *worktree = NULL;
4303 struct got_object_id *id1 = NULL, *id2 = NULL;
4304 char *repo_path = NULL, *cwd = NULL;
4305 char *id_str1 = NULL, *id_str2 = NULL;
4306 char *label1 = NULL, *label2 = NULL;
4307 int diff_context = 3, ignore_whitespace = 0;
4308 int ch, force_text_diff = 0;
4310 struct tog_view *view;
4311 int *pack_fds = NULL;
4313 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4316 force_text_diff = 1;
4319 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4322 errx(1, "number of context lines is %s: %s",
4326 repo_path = realpath(optarg, NULL);
4327 if (repo_path == NULL)
4328 return got_error_from_errno2("realpath",
4330 got_path_strip_trailing_slashes(repo_path);
4333 ignore_whitespace = 1;
4345 usage_diff(); /* TODO show local worktree changes */
4346 } else if (argc == 2) {
4352 error = got_repo_pack_fds_open(&pack_fds);
4356 if (repo_path == NULL) {
4357 cwd = getcwd(NULL, 0);
4359 return got_error_from_errno("getcwd");
4360 error = got_worktree_open(&worktree, cwd);
4361 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4365 strdup(got_worktree_get_repo_path(worktree));
4367 repo_path = strdup(cwd);
4368 if (repo_path == NULL) {
4369 error = got_error_from_errno("strdup");
4374 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4380 error = apply_unveil(got_repo_get_path(repo), NULL);
4384 error = tog_load_refs(repo, 0);
4388 error = got_repo_match_object_id(&id1, &label1, id_str1,
4389 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4393 error = got_repo_match_object_id(&id2, &label2, id_str2,
4394 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4398 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4400 error = got_error_from_errno("view_open");
4403 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4404 ignore_whitespace, force_text_diff, NULL, repo);
4407 error = view_loop(view);
4414 const struct got_error *close_err = got_repo_close(repo);
4419 got_worktree_close(worktree);
4421 const struct got_error *pack_err =
4422 got_repo_pack_fds_close(pack_fds);
4435 "usage: %s blame [-c commit] [-r repository-path] path\n",
4440 struct tog_blame_line {
4442 struct got_object_id *id;
4445 static const struct got_error *
4446 draw_blame(struct tog_view *view)
4448 struct tog_blame_view_state *s = &view->state.blame;
4449 struct tog_blame *blame = &s->blame;
4450 regmatch_t *regmatch = &view->regmatch;
4451 const struct got_error *err;
4452 int lineno = 0, nprinted = 0;
4454 size_t linesize = 0;
4458 struct tog_blame_line *blame_line;
4459 struct got_object_id *prev_id = NULL;
4461 struct tog_color *tc;
4463 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4468 werase(view->window);
4470 if (asprintf(&line, "commit %s", id_str) == -1) {
4471 err = got_error_from_errno("asprintf");
4476 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4481 if (view_needs_focus_indication(view))
4482 wstandout(view->window);
4483 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4485 wattr_on(view->window,
4486 COLOR_PAIR(tc->colorpair), NULL);
4487 waddwstr(view->window, wline);
4489 wattr_off(view->window,
4490 COLOR_PAIR(tc->colorpair), NULL);
4491 if (view_needs_focus_indication(view))
4492 wstandend(view->window);
4495 if (width < view->ncols - 1)
4496 waddch(view->window, '\n');
4498 if (asprintf(&line, "[%d/%d] %s%s",
4499 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4500 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4502 return got_error_from_errno("asprintf");
4505 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4510 waddwstr(view->window, wline);
4513 if (width < view->ncols - 1)
4514 waddch(view->window, '\n');
4518 while (nprinted < view->nlines - 2) {
4519 linelen = getline(&line, &linesize, blame->f);
4520 if (linelen == -1) {
4521 if (feof(blame->f)) {
4526 return got_ferror(blame->f, GOT_ERR_IO);
4528 if (++lineno < s->first_displayed_line)
4531 /* Set view->maxx based on full line length. */
4532 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4539 view->maxx = MAX(view->maxx, width);
4541 if (view->focussed && nprinted == s->selected_line - 1)
4542 wstandout(view->window);
4544 if (blame->nlines > 0) {
4545 blame_line = &blame->lines[lineno - 1];
4546 if (blame_line->annotated && prev_id &&
4547 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4549 nprinted == s->selected_line - 1)) {
4550 waddstr(view->window, " ");
4551 } else if (blame_line->annotated) {
4553 err = got_object_id_str(&id_str,
4559 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4561 wattr_on(view->window,
4562 COLOR_PAIR(tc->colorpair), NULL);
4563 wprintw(view->window, "%.8s", id_str);
4565 wattr_off(view->window,
4566 COLOR_PAIR(tc->colorpair), NULL);
4568 prev_id = blame_line->id;
4570 waddstr(view->window, "........");
4574 waddstr(view->window, "........");
4578 if (view->focussed && nprinted == s->selected_line - 1)
4579 wstandend(view->window);
4580 waddstr(view->window, " ");
4582 if (view->ncols <= 9) {
4584 } else if (s->first_displayed_line + nprinted ==
4586 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4587 err = add_matched_line(&width, line, view->ncols - 9, 9,
4588 view->window, view->x, regmatch);
4596 err = format_line(&wline, &width, &skip, line,
4597 view->x, view->ncols - 9, 9, 1);
4602 waddwstr(view->window, &wline[skip]);
4608 if (width <= view->ncols - 1)
4609 waddch(view->window, '\n');
4610 if (++nprinted == 1)
4611 s->first_displayed_line = lineno;
4614 s->last_displayed_line = lineno;
4621 static const struct got_error *
4622 blame_cb(void *arg, int nlines, int lineno,
4623 struct got_commit_object *commit, struct got_object_id *id)
4625 const struct got_error *err = NULL;
4626 struct tog_blame_cb_args *a = arg;
4627 struct tog_blame_line *line;
4630 if (nlines != a->nlines ||
4631 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4632 return got_error(GOT_ERR_RANGE);
4634 errcode = pthread_mutex_lock(&tog_mutex);
4636 return got_error_set_errno(errcode, "pthread_mutex_lock");
4638 if (*a->quit) { /* user has quit the blame view */
4639 err = got_error(GOT_ERR_ITER_COMPLETED);
4644 goto done; /* no change in this commit */
4646 line = &a->lines[lineno - 1];
4647 if (line->annotated)
4650 line->id = got_object_id_dup(id);
4651 if (line->id == NULL) {
4652 err = got_error_from_errno("got_object_id_dup");
4655 line->annotated = 1;
4657 errcode = pthread_mutex_unlock(&tog_mutex);
4659 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4664 blame_thread(void *arg)
4666 const struct got_error *err, *close_err;
4667 struct tog_blame_thread_args *ta = arg;
4668 struct tog_blame_cb_args *a = ta->cb_args;
4671 err = block_signals_used_by_main_thread();
4675 err = got_blame(ta->path, a->commit_id, ta->repo,
4676 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4677 if (err && err->code == GOT_ERR_CANCELLED)
4680 errcode = pthread_mutex_lock(&tog_mutex);
4682 return (void *)got_error_set_errno(errcode,
4683 "pthread_mutex_lock");
4685 close_err = got_repo_close(ta->repo);
4691 errcode = pthread_mutex_unlock(&tog_mutex);
4692 if (errcode && err == NULL)
4693 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4698 static struct got_object_id *
4699 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4700 int first_displayed_line, int selected_line)
4702 struct tog_blame_line *line;
4707 line = &lines[first_displayed_line - 1 + selected_line - 1];
4708 if (!line->annotated)
4714 static const struct got_error *
4715 stop_blame(struct tog_blame *blame)
4717 const struct got_error *err = NULL;
4720 if (blame->thread) {
4722 errcode = pthread_mutex_unlock(&tog_mutex);
4724 return got_error_set_errno(errcode,
4725 "pthread_mutex_unlock");
4726 errcode = pthread_join(blame->thread, (void **)&err);
4728 return got_error_set_errno(errcode, "pthread_join");
4729 errcode = pthread_mutex_lock(&tog_mutex);
4731 return got_error_set_errno(errcode,
4732 "pthread_mutex_lock");
4733 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4735 blame->thread = 0; //NULL;
4737 if (blame->thread_args.repo) {
4738 const struct got_error *close_err;
4739 close_err = got_repo_close(blame->thread_args.repo);
4742 blame->thread_args.repo = NULL;
4745 if (fclose(blame->f) == EOF && err == NULL)
4746 err = got_error_from_errno("fclose");
4750 for (i = 0; i < blame->nlines; i++)
4751 free(blame->lines[i].id);
4753 blame->lines = NULL;
4755 free(blame->cb_args.commit_id);
4756 blame->cb_args.commit_id = NULL;
4757 if (blame->pack_fds) {
4758 const struct got_error *pack_err =
4759 got_repo_pack_fds_close(blame->pack_fds);
4762 blame->pack_fds = NULL;
4767 static const struct got_error *
4768 cancel_blame_view(void *arg)
4770 const struct got_error *err = NULL;
4774 errcode = pthread_mutex_lock(&tog_mutex);
4776 return got_error_set_errno(errcode,
4777 "pthread_mutex_unlock");
4780 err = got_error(GOT_ERR_CANCELLED);
4782 errcode = pthread_mutex_unlock(&tog_mutex);
4784 return got_error_set_errno(errcode,
4785 "pthread_mutex_lock");
4790 static const struct got_error *
4791 run_blame(struct tog_view *view)
4793 struct tog_blame_view_state *s = &view->state.blame;
4794 struct tog_blame *blame = &s->blame;
4795 const struct got_error *err = NULL;
4796 struct got_commit_object *commit = NULL;
4797 struct got_blob_object *blob = NULL;
4798 struct got_repository *thread_repo = NULL;
4799 struct got_object_id *obj_id = NULL;
4800 int obj_type, fd = -1;
4801 int *pack_fds = NULL;
4803 err = got_object_open_as_commit(&commit, s->repo,
4804 &s->blamed_commit->id);
4808 fd = got_opentempfd();
4810 err = got_error_from_errno("got_opentempfd");
4814 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4818 err = got_object_get_type(&obj_type, s->repo, obj_id);
4822 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4823 err = got_error(GOT_ERR_OBJ_TYPE);
4827 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192, fd);
4830 blame->f = got_opentemp();
4831 if (blame->f == NULL) {
4832 err = got_error_from_errno("got_opentemp");
4835 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4836 &blame->line_offsets, blame->f, blob);
4839 if (blame->nlines == 0) {
4840 s->blame_complete = 1;
4844 /* Don't include \n at EOF in the blame line count. */
4845 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4848 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4849 if (blame->lines == NULL) {
4850 err = got_error_from_errno("calloc");
4854 err = got_repo_pack_fds_open(&pack_fds);
4857 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4862 blame->pack_fds = pack_fds;
4863 blame->cb_args.view = view;
4864 blame->cb_args.lines = blame->lines;
4865 blame->cb_args.nlines = blame->nlines;
4866 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4867 if (blame->cb_args.commit_id == NULL) {
4868 err = got_error_from_errno("got_object_id_dup");
4871 blame->cb_args.quit = &s->done;
4873 blame->thread_args.path = s->path;
4874 blame->thread_args.repo = thread_repo;
4875 blame->thread_args.cb_args = &blame->cb_args;
4876 blame->thread_args.complete = &s->blame_complete;
4877 blame->thread_args.cancel_cb = cancel_blame_view;
4878 blame->thread_args.cancel_arg = &s->done;
4879 s->blame_complete = 0;
4881 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4882 s->first_displayed_line = 1;
4883 s->last_displayed_line = view->nlines;
4884 s->selected_line = 1;
4886 s->matched_line = 0;
4890 got_object_commit_close(commit);
4891 if (fd != -1 && close(fd) == -1 && err == NULL)
4892 err = got_error_from_errno("close");
4894 got_object_blob_close(blob);
4901 static const struct got_error *
4902 open_blame_view(struct tog_view *view, char *path,
4903 struct got_object_id *commit_id, struct got_repository *repo)
4905 const struct got_error *err = NULL;
4906 struct tog_blame_view_state *s = &view->state.blame;
4908 STAILQ_INIT(&s->blamed_commits);
4910 s->path = strdup(path);
4911 if (s->path == NULL)
4912 return got_error_from_errno("strdup");
4914 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4920 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4921 s->first_displayed_line = 1;
4922 s->last_displayed_line = view->nlines;
4923 s->selected_line = 1;
4924 s->blame_complete = 0;
4926 s->commit_id = commit_id;
4927 memset(&s->blame, 0, sizeof(s->blame));
4929 STAILQ_INIT(&s->colors);
4930 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4931 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4932 get_color_value("TOG_COLOR_COMMIT"));
4937 view->show = show_blame_view;
4938 view->input = input_blame_view;
4939 view->close = close_blame_view;
4940 view->search_start = search_start_blame_view;
4941 view->search_next = search_next_blame_view;
4943 return run_blame(view);
4946 static const struct got_error *
4947 close_blame_view(struct tog_view *view)
4949 const struct got_error *err = NULL;
4950 struct tog_blame_view_state *s = &view->state.blame;
4952 if (s->blame.thread)
4953 err = stop_blame(&s->blame);
4955 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4956 struct got_object_qid *blamed_commit;
4957 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4958 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4959 got_object_qid_free(blamed_commit);
4963 free_colors(&s->colors);
4967 static const struct got_error *
4968 search_start_blame_view(struct tog_view *view)
4970 struct tog_blame_view_state *s = &view->state.blame;
4972 s->matched_line = 0;
4976 static const struct got_error *
4977 search_next_blame_view(struct tog_view *view)
4979 struct tog_blame_view_state *s = &view->state.blame;
4980 const struct got_error *err = NULL;
4983 size_t linesize = 0;
4986 if (!view->searching) {
4987 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4991 if (s->matched_line) {
4992 if (view->searching == TOG_SEARCH_FORWARD)
4993 lineno = s->matched_line + 1;
4995 lineno = s->matched_line - 1;
4997 lineno = s->first_displayed_line - 1 + s->selected_line;
5002 if (lineno <= 0 || lineno > s->blame.nlines) {
5003 if (s->matched_line == 0) {
5004 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5008 if (view->searching == TOG_SEARCH_FORWARD)
5011 lineno = s->blame.nlines;
5014 offset = s->blame.line_offsets[lineno - 1];
5015 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
5017 return got_error_from_errno("fseeko");
5019 linelen = getline(&line, &linesize, s->blame.f);
5020 if (linelen != -1) {
5022 err = expand_tab(&exstr, line);
5025 if (match_line(exstr, &view->regex, 1,
5027 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5028 s->matched_line = lineno;
5034 if (view->searching == TOG_SEARCH_FORWARD)
5041 if (s->matched_line) {
5042 s->first_displayed_line = s->matched_line;
5043 s->selected_line = 1;
5049 static const struct got_error *
5050 show_blame_view(struct tog_view *view)
5052 const struct got_error *err = NULL;
5053 struct tog_blame_view_state *s = &view->state.blame;
5056 if (s->blame.thread == 0 && !s->blame_complete) {
5057 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
5058 &s->blame.thread_args);
5060 return got_error_set_errno(errcode, "pthread_create");
5062 halfdelay(1); /* fast refresh while annotating */
5065 if (s->blame_complete)
5066 halfdelay(10); /* disable fast refresh */
5068 err = draw_blame(view);
5074 static const struct got_error *
5075 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
5077 const struct got_error *err = NULL, *thread_err = NULL;
5078 struct tog_view *diff_view;
5079 struct tog_blame_view_state *s = &view->state.blame;
5080 int begin_x = 0, nscroll = view->nlines - 2;
5087 view->x = MAX(view->maxx - view->ncols / 3, 0);
5092 if (view->x + view->ncols / 3 < view->maxx)
5093 view->x += 2; /* move two columns right */
5099 view->x -= MIN(view->x, 2); /* move two columns back */
5108 s->selected_line = 1;
5109 s->first_displayed_line = 1;
5114 if (s->blame.nlines < view->nlines - 2) {
5115 s->selected_line = s->blame.nlines;
5116 s->first_displayed_line = 1;
5118 s->selected_line = view->nlines - 2;
5119 s->first_displayed_line = s->blame.nlines -
5127 if (s->selected_line > 1)
5129 else if (s->selected_line == 1 &&
5130 s->first_displayed_line > 1)
5131 s->first_displayed_line--;
5142 if (s->first_displayed_line == 1) {
5143 if (view->count > 1)
5145 s->selected_line = MAX(1, s->selected_line - nscroll);
5149 if (s->first_displayed_line > nscroll)
5150 s->first_displayed_line -= nscroll;
5152 s->first_displayed_line = 1;
5157 if (s->selected_line < view->nlines - 2 &&
5158 s->first_displayed_line +
5159 s->selected_line <= s->blame.nlines)
5161 else if (s->last_displayed_line <
5163 s->first_displayed_line++;
5169 struct got_object_id *id = NULL;
5172 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5173 s->first_displayed_line, s->selected_line);
5177 struct got_commit_object *commit, *pcommit;
5178 struct got_object_qid *pid;
5179 struct got_object_id *blob_id = NULL;
5181 err = got_object_open_as_commit(&commit,
5186 got_object_commit_get_parent_ids(commit));
5188 got_object_commit_close(commit);
5191 /* Check if path history ends here. */
5192 err = got_object_open_as_commit(&pcommit,
5196 err = got_object_id_by_path(&blob_id, s->repo,
5198 got_object_commit_close(pcommit);
5200 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5202 got_object_commit_close(commit);
5205 err = got_object_get_type(&obj_type, s->repo,
5208 /* Can't blame non-blob type objects. */
5209 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5210 got_object_commit_close(commit);
5213 err = got_object_qid_alloc(&s->blamed_commit,
5215 got_object_commit_close(commit);
5217 if (got_object_id_cmp(id,
5218 &s->blamed_commit->id) == 0)
5220 err = got_object_qid_alloc(&s->blamed_commit,
5226 thread_err = stop_blame(&s->blame);
5230 STAILQ_INSERT_HEAD(&s->blamed_commits,
5231 s->blamed_commit, entry);
5232 err = run_blame(view);
5238 struct got_object_qid *first;
5241 first = STAILQ_FIRST(&s->blamed_commits);
5242 if (!got_object_id_cmp(&first->id, s->commit_id))
5245 thread_err = stop_blame(&s->blame);
5249 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5250 got_object_qid_free(s->blamed_commit);
5252 STAILQ_FIRST(&s->blamed_commits);
5253 err = run_blame(view);
5260 struct got_object_id *id = NULL;
5261 struct got_object_qid *pid;
5262 struct got_commit_object *commit = NULL;
5265 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5266 s->first_displayed_line, s->selected_line);
5269 err = got_object_open_as_commit(&commit, s->repo, id);
5273 got_object_commit_get_parent_ids(commit));
5274 if (view_is_parent_view(view))
5275 begin_x = view_split_begin_x(view->begin_x);
5276 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5277 if (diff_view == NULL) {
5278 got_object_commit_close(commit);
5279 err = got_error_from_errno("view_open");
5282 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5283 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5284 got_object_commit_close(commit);
5286 view_close(diff_view);
5290 diff_view->focussed = 1;
5291 if (view_is_parent_view(view)) {
5292 err = view_close_child(view);
5295 err = view_set_child(view, diff_view);
5298 view->focus_child = 1;
5300 *new_view = diff_view;
5313 if (s->last_displayed_line >= s->blame.nlines &&
5314 s->selected_line >= MIN(s->blame.nlines,
5315 view->nlines - 2)) {
5319 if (s->last_displayed_line >= s->blame.nlines &&
5320 s->selected_line < view->nlines - 2) {
5322 MIN(nscroll, s->last_displayed_line -
5323 s->first_displayed_line - s->selected_line + 1);
5325 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5326 s->first_displayed_line += nscroll;
5328 s->first_displayed_line =
5329 s->blame.nlines - (view->nlines - 3);
5332 if (s->selected_line > view->nlines - 2) {
5333 s->selected_line = MIN(s->blame.nlines,
5341 return thread_err ? thread_err : err;
5344 static const struct got_error *
5345 cmd_blame(int argc, char *argv[])
5347 const struct got_error *error;
5348 struct got_repository *repo = NULL;
5349 struct got_worktree *worktree = NULL;
5350 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5351 char *link_target = NULL;
5352 struct got_object_id *commit_id = NULL;
5353 struct got_commit_object *commit = NULL;
5354 char *commit_id_str = NULL;
5356 struct tog_view *view;
5357 int *pack_fds = NULL;
5359 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5362 commit_id_str = optarg;
5365 repo_path = realpath(optarg, NULL);
5366 if (repo_path == NULL)
5367 return got_error_from_errno2("realpath",
5382 error = got_repo_pack_fds_open(&pack_fds);
5386 if (repo_path == NULL) {
5387 cwd = getcwd(NULL, 0);
5389 return got_error_from_errno("getcwd");
5390 error = got_worktree_open(&worktree, cwd);
5391 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5395 strdup(got_worktree_get_repo_path(worktree));
5397 repo_path = strdup(cwd);
5398 if (repo_path == NULL) {
5399 error = got_error_from_errno("strdup");
5404 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5408 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5415 error = apply_unveil(got_repo_get_path(repo), NULL);
5419 error = tog_load_refs(repo, 0);
5423 if (commit_id_str == NULL) {
5424 struct got_reference *head_ref;
5425 error = got_ref_open(&head_ref, repo, worktree ?
5426 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5429 error = got_ref_resolve(&commit_id, repo, head_ref);
5430 got_ref_close(head_ref);
5432 error = got_repo_match_object_id(&commit_id, NULL,
5433 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5438 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5440 error = got_error_from_errno("view_open");
5444 error = got_object_open_as_commit(&commit, repo, commit_id);
5448 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5453 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5458 /* Release work tree lock. */
5459 got_worktree_close(worktree);
5462 error = view_loop(view);
5470 got_object_commit_close(commit);
5472 got_worktree_close(worktree);
5474 const struct got_error *close_err = got_repo_close(repo);
5479 const struct got_error *pack_err =
5480 got_repo_pack_fds_close(pack_fds);
5488 static const struct got_error *
5489 draw_tree_entries(struct tog_view *view, const char *parent_path)
5491 struct tog_tree_view_state *s = &view->state.tree;
5492 const struct got_error *err = NULL;
5493 struct got_tree_entry *te;
5495 struct tog_color *tc;
5496 int width, n, i, nentries;
5497 int limit = view->nlines;
5501 werase(view->window);
5506 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5510 if (view_needs_focus_indication(view))
5511 wstandout(view->window);
5512 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5514 wattr_on(view->window,
5515 COLOR_PAIR(tc->colorpair), NULL);
5516 waddwstr(view->window, wline);
5518 wattr_off(view->window,
5519 COLOR_PAIR(tc->colorpair), NULL);
5520 if (view_needs_focus_indication(view))
5521 wstandend(view->window);
5524 if (width < view->ncols - 1)
5525 waddch(view->window, '\n');
5528 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5532 waddwstr(view->window, wline);
5535 if (width < view->ncols - 1)
5536 waddch(view->window, '\n');
5539 waddch(view->window, '\n');
5543 if (s->first_displayed_entry == NULL) {
5544 te = got_object_tree_get_first_entry(s->tree);
5545 if (s->selected == 0) {
5547 wstandout(view->window);
5548 s->selected_entry = NULL;
5550 waddstr(view->window, " ..\n"); /* parent directory */
5551 if (s->selected == 0 && view->focussed)
5552 wstandend(view->window);
5559 te = s->first_displayed_entry;
5562 nentries = got_object_tree_get_nentries(s->tree);
5563 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5564 char *line = NULL, *id_str = NULL, *link_target = NULL;
5565 const char *modestr = "";
5568 te = got_object_tree_get_entry(s->tree, i);
5569 mode = got_tree_entry_get_mode(te);
5572 err = got_object_id_str(&id_str,
5573 got_tree_entry_get_id(te));
5575 return got_error_from_errno(
5576 "got_object_id_str");
5578 if (got_object_tree_entry_is_submodule(te))
5580 else if (S_ISLNK(mode)) {
5583 err = got_tree_entry_get_symlink_target(&link_target,
5589 for (i = 0; i < strlen(link_target); i++) {
5590 if (!isprint((unsigned char)link_target[i]))
5591 link_target[i] = '?';
5595 else if (S_ISDIR(mode))
5597 else if (mode & S_IXUSR)
5599 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5600 got_tree_entry_get_name(te), modestr,
5601 link_target ? " -> ": "",
5602 link_target ? link_target : "") == -1) {
5605 return got_error_from_errno("asprintf");
5609 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5615 if (n == s->selected) {
5617 wstandout(view->window);
5618 s->selected_entry = te;
5620 tc = match_color(&s->colors, line);
5622 wattr_on(view->window,
5623 COLOR_PAIR(tc->colorpair), NULL);
5624 waddwstr(view->window, wline);
5626 wattr_off(view->window,
5627 COLOR_PAIR(tc->colorpair), NULL);
5628 if (width < view->ncols - 1)
5629 waddch(view->window, '\n');
5630 if (n == s->selected && view->focussed)
5631 wstandend(view->window);
5637 s->last_displayed_entry = te;
5646 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5648 struct got_tree_entry *te;
5649 int isroot = s->tree == s->root;
5652 if (s->first_displayed_entry == NULL)
5655 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5656 while (i++ < maxscroll) {
5659 s->first_displayed_entry = NULL;
5662 s->first_displayed_entry = te;
5663 te = got_tree_entry_get_prev(s->tree, te);
5668 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5670 struct got_tree_entry *next, *last;
5673 if (s->first_displayed_entry)
5674 next = got_tree_entry_get_next(s->tree,
5675 s->first_displayed_entry);
5677 next = got_object_tree_get_first_entry(s->tree);
5679 last = s->last_displayed_entry;
5680 while (next && last && n++ < maxscroll) {
5681 last = got_tree_entry_get_next(s->tree, last);
5683 s->first_displayed_entry = next;
5684 next = got_tree_entry_get_next(s->tree, next);
5689 static const struct got_error *
5690 tree_entry_path(char **path, struct tog_parent_trees *parents,
5691 struct got_tree_entry *te)
5693 const struct got_error *err = NULL;
5694 struct tog_parent_tree *pt;
5695 size_t len = 2; /* for leading slash and NUL */
5697 TAILQ_FOREACH(pt, parents, entry)
5698 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5701 len += strlen(got_tree_entry_get_name(te));
5703 *path = calloc(1, len);
5705 return got_error_from_errno("calloc");
5708 pt = TAILQ_LAST(parents, tog_parent_trees);
5710 const char *name = got_tree_entry_get_name(pt->selected_entry);
5711 if (strlcat(*path, name, len) >= len) {
5712 err = got_error(GOT_ERR_NO_SPACE);
5715 if (strlcat(*path, "/", len) >= len) {
5716 err = got_error(GOT_ERR_NO_SPACE);
5719 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5722 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5723 err = got_error(GOT_ERR_NO_SPACE);
5735 static const struct got_error *
5736 blame_tree_entry(struct tog_view **new_view, int begin_x,
5737 struct got_tree_entry *te, struct tog_parent_trees *parents,
5738 struct got_object_id *commit_id, struct got_repository *repo)
5740 const struct got_error *err = NULL;
5742 struct tog_view *blame_view;
5746 err = tree_entry_path(&path, parents, te);
5750 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5751 if (blame_view == NULL) {
5752 err = got_error_from_errno("view_open");
5756 err = open_blame_view(blame_view, path, commit_id, repo);
5758 if (err->code == GOT_ERR_CANCELLED)
5760 view_close(blame_view);
5762 *new_view = blame_view;
5768 static const struct got_error *
5769 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5770 struct tog_tree_view_state *s)
5772 struct tog_view *log_view;
5773 const struct got_error *err = NULL;
5778 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5779 if (log_view == NULL)
5780 return got_error_from_errno("view_open");
5782 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5786 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5789 view_close(log_view);
5791 *new_view = log_view;
5796 static const struct got_error *
5797 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5798 const char *head_ref_name, struct got_repository *repo)
5800 const struct got_error *err = NULL;
5801 char *commit_id_str = NULL;
5802 struct tog_tree_view_state *s = &view->state.tree;
5803 struct got_commit_object *commit = NULL;
5805 TAILQ_INIT(&s->parents);
5806 STAILQ_INIT(&s->colors);
5808 s->commit_id = got_object_id_dup(commit_id);
5809 if (s->commit_id == NULL)
5810 return got_error_from_errno("got_object_id_dup");
5812 err = got_object_open_as_commit(&commit, repo, commit_id);
5817 * The root is opened here and will be closed when the view is closed.
5818 * Any visited subtrees and their path-wise parents are opened and
5821 err = got_object_open_as_tree(&s->root, repo,
5822 got_object_commit_get_tree_id(commit));
5827 err = got_object_id_str(&commit_id_str, commit_id);
5831 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5832 err = got_error_from_errno("asprintf");
5836 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5837 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5838 if (head_ref_name) {
5839 s->head_ref_name = strdup(head_ref_name);
5840 if (s->head_ref_name == NULL) {
5841 err = got_error_from_errno("strdup");
5847 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5848 err = add_color(&s->colors, "\\$$",
5849 TOG_COLOR_TREE_SUBMODULE,
5850 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5853 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5854 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5857 err = add_color(&s->colors, "/$",
5858 TOG_COLOR_TREE_DIRECTORY,
5859 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5863 err = add_color(&s->colors, "\\*$",
5864 TOG_COLOR_TREE_EXECUTABLE,
5865 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5869 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5870 get_color_value("TOG_COLOR_COMMIT"));
5875 view->show = show_tree_view;
5876 view->input = input_tree_view;
5877 view->close = close_tree_view;
5878 view->search_start = search_start_tree_view;
5879 view->search_next = search_next_tree_view;
5881 free(commit_id_str);
5883 got_object_commit_close(commit);
5885 close_tree_view(view);
5889 static const struct got_error *
5890 close_tree_view(struct tog_view *view)
5892 struct tog_tree_view_state *s = &view->state.tree;
5894 free_colors(&s->colors);
5895 free(s->tree_label);
5896 s->tree_label = NULL;
5898 s->commit_id = NULL;
5899 free(s->head_ref_name);
5900 s->head_ref_name = NULL;
5901 while (!TAILQ_EMPTY(&s->parents)) {
5902 struct tog_parent_tree *parent;
5903 parent = TAILQ_FIRST(&s->parents);
5904 TAILQ_REMOVE(&s->parents, parent, entry);
5905 if (parent->tree != s->root)
5906 got_object_tree_close(parent->tree);
5910 if (s->tree != NULL && s->tree != s->root)
5911 got_object_tree_close(s->tree);
5913 got_object_tree_close(s->root);
5917 static const struct got_error *
5918 search_start_tree_view(struct tog_view *view)
5920 struct tog_tree_view_state *s = &view->state.tree;
5922 s->matched_entry = NULL;
5927 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5929 regmatch_t regmatch;
5931 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5935 static const struct got_error *
5936 search_next_tree_view(struct tog_view *view)
5938 struct tog_tree_view_state *s = &view->state.tree;
5939 struct got_tree_entry *te = NULL;
5941 if (!view->searching) {
5942 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5946 if (s->matched_entry) {
5947 if (view->searching == TOG_SEARCH_FORWARD) {
5948 if (s->selected_entry)
5949 te = got_tree_entry_get_next(s->tree,
5952 te = got_object_tree_get_first_entry(s->tree);
5954 if (s->selected_entry == NULL)
5955 te = got_object_tree_get_last_entry(s->tree);
5957 te = got_tree_entry_get_prev(s->tree,
5961 if (s->selected_entry)
5962 te = s->selected_entry;
5963 else if (view->searching == TOG_SEARCH_FORWARD)
5964 te = got_object_tree_get_first_entry(s->tree);
5966 te = got_object_tree_get_last_entry(s->tree);
5971 if (s->matched_entry == NULL) {
5972 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5975 if (view->searching == TOG_SEARCH_FORWARD)
5976 te = got_object_tree_get_first_entry(s->tree);
5978 te = got_object_tree_get_last_entry(s->tree);
5981 if (match_tree_entry(te, &view->regex)) {
5982 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5983 s->matched_entry = te;
5987 if (view->searching == TOG_SEARCH_FORWARD)
5988 te = got_tree_entry_get_next(s->tree, te);
5990 te = got_tree_entry_get_prev(s->tree, te);
5993 if (s->matched_entry) {
5994 s->first_displayed_entry = s->matched_entry;
6001 static const struct got_error *
6002 show_tree_view(struct tog_view *view)
6004 const struct got_error *err = NULL;
6005 struct tog_tree_view_state *s = &view->state.tree;
6008 err = tree_entry_path(&parent_path, &s->parents, NULL);
6012 err = draw_tree_entries(view, parent_path);
6019 static const struct got_error *
6020 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
6022 const struct got_error *err = NULL;
6023 struct tog_tree_view_state *s = &view->state.tree;
6024 struct tog_view *log_view, *ref_view;
6025 struct got_tree_entry *te;
6026 int begin_x = 0, n, nscroll = view->nlines - 3;
6030 s->show_ids = !s->show_ids;
6035 if (!s->selected_entry)
6037 if (view_is_parent_view(view))
6038 begin_x = view_split_begin_x(view->begin_x);
6039 err = log_selected_tree_entry(&log_view, begin_x, s);
6041 log_view->focussed = 1;
6042 if (view_is_parent_view(view)) {
6043 err = view_close_child(view);
6046 err = view_set_child(view, log_view);
6049 view->focus_child = 1;
6051 *new_view = log_view;
6055 if (view_is_parent_view(view))
6056 begin_x = view_split_begin_x(view->begin_x);
6057 ref_view = view_open(view->nlines, view->ncols,
6058 view->begin_y, begin_x, TOG_VIEW_REF);
6059 if (ref_view == NULL)
6060 return got_error_from_errno("view_open");
6061 err = open_ref_view(ref_view, s->repo);
6063 view_close(ref_view);
6067 ref_view->focussed = 1;
6068 if (view_is_parent_view(view)) {
6069 err = view_close_child(view);
6072 err = view_set_child(view, ref_view);
6075 view->focus_child = 1;
6077 *new_view = ref_view;
6083 if (s->tree == s->root)
6084 s->first_displayed_entry =
6085 got_object_tree_get_first_entry(s->tree);
6087 s->first_displayed_entry = NULL;
6093 te = got_object_tree_get_last_entry(s->tree);
6094 for (n = 0; n < view->nlines - 3; n++) {
6096 if(s->tree != s->root) {
6097 s->first_displayed_entry = NULL;
6102 s->first_displayed_entry = te;
6103 te = got_tree_entry_get_prev(s->tree, te);
6106 s->selected = n - 1;
6111 if (s->selected > 0) {
6115 tree_scroll_up(s, 1);
6116 if (s->selected_entry == NULL ||
6117 (s->tree == s->root && s->selected_entry ==
6118 got_object_tree_get_first_entry(s->tree)))
6128 if (s->tree == s->root) {
6129 if (got_object_tree_get_first_entry(s->tree) ==
6130 s->first_displayed_entry)
6131 s->selected -= MIN(s->selected, nscroll);
6133 if (s->first_displayed_entry == NULL)
6134 s->selected -= MIN(s->selected, nscroll);
6136 tree_scroll_up(s, MAX(0, nscroll));
6137 if (s->selected_entry == NULL ||
6138 (s->tree == s->root && s->selected_entry ==
6139 got_object_tree_get_first_entry(s->tree)))
6145 if (s->selected < s->ndisplayed - 1) {
6149 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6151 /* can't scroll any further */
6155 tree_scroll_down(s, 1);
6165 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6167 /* can't scroll any further; move cursor down */
6168 if (s->selected < s->ndisplayed - 1)
6169 s->selected += MIN(nscroll,
6170 s->ndisplayed - s->selected - 1);
6175 tree_scroll_down(s, nscroll);
6180 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6181 struct tog_parent_tree *parent;
6182 /* user selected '..' */
6183 if (s->tree == s->root) {
6187 parent = TAILQ_FIRST(&s->parents);
6188 TAILQ_REMOVE(&s->parents, parent,
6190 got_object_tree_close(s->tree);
6191 s->tree = parent->tree;
6192 s->first_displayed_entry =
6193 parent->first_displayed_entry;
6195 parent->selected_entry;
6196 s->selected = parent->selected;
6198 } else if (S_ISDIR(got_tree_entry_get_mode(
6199 s->selected_entry))) {
6200 struct got_tree_object *subtree;
6202 err = got_object_open_as_tree(&subtree, s->repo,
6203 got_tree_entry_get_id(s->selected_entry));
6206 err = tree_view_visit_subtree(s, subtree);
6208 got_object_tree_close(subtree);
6211 } else if (S_ISREG(got_tree_entry_get_mode(
6212 s->selected_entry))) {
6213 struct tog_view *blame_view;
6214 int begin_x = view_is_parent_view(view) ?
6215 view_split_begin_x(view->begin_x) : 0;
6217 err = blame_tree_entry(&blame_view, begin_x,
6218 s->selected_entry, &s->parents,
6219 s->commit_id, s->repo);
6224 blame_view->focussed = 1;
6225 if (view_is_parent_view(view)) {
6226 err = view_close_child(view);
6229 err = view_set_child(view, blame_view);
6232 view->focus_child = 1;
6234 *new_view = blame_view;
6238 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6239 s->selected = view->nlines - 4;
6255 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6260 static const struct got_error *
6261 cmd_tree(int argc, char *argv[])
6263 const struct got_error *error;
6264 struct got_repository *repo = NULL;
6265 struct got_worktree *worktree = NULL;
6266 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6267 struct got_object_id *commit_id = NULL;
6268 struct got_commit_object *commit = NULL;
6269 const char *commit_id_arg = NULL;
6271 struct got_reference *ref = NULL;
6272 const char *head_ref_name = NULL;
6274 struct tog_view *view;
6275 int *pack_fds = NULL;
6277 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6280 commit_id_arg = optarg;
6283 repo_path = realpath(optarg, NULL);
6284 if (repo_path == NULL)
6285 return got_error_from_errno2("realpath",
6300 error = got_repo_pack_fds_open(&pack_fds);
6304 if (repo_path == NULL) {
6305 cwd = getcwd(NULL, 0);
6307 return got_error_from_errno("getcwd");
6308 error = got_worktree_open(&worktree, cwd);
6309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6313 strdup(got_worktree_get_repo_path(worktree));
6315 repo_path = strdup(cwd);
6316 if (repo_path == NULL) {
6317 error = got_error_from_errno("strdup");
6322 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6326 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6333 error = apply_unveil(got_repo_get_path(repo), NULL);
6337 error = tog_load_refs(repo, 0);
6341 if (commit_id_arg == NULL) {
6342 error = got_repo_match_object_id(&commit_id, &label,
6343 worktree ? got_worktree_get_head_ref_name(worktree) :
6344 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6347 head_ref_name = label;
6349 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6351 head_ref_name = got_ref_get_name(ref);
6352 else if (error->code != GOT_ERR_NOT_REF)
6354 error = got_repo_match_object_id(&commit_id, NULL,
6355 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6360 error = got_object_open_as_commit(&commit, repo, commit_id);
6364 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6366 error = got_error_from_errno("view_open");
6369 error = open_tree_view(view, commit_id, head_ref_name, repo);
6372 if (!got_path_is_root_dir(in_repo_path)) {
6373 error = tree_view_walk_path(&view->state.tree, commit,
6380 /* Release work tree lock. */
6381 got_worktree_close(worktree);
6384 error = view_loop(view);
6393 const struct got_error *close_err = got_repo_close(repo);
6398 const struct got_error *pack_err =
6399 got_repo_pack_fds_close(pack_fds);
6407 static const struct got_error *
6408 ref_view_load_refs(struct tog_ref_view_state *s)
6410 struct got_reflist_entry *sre;
6411 struct tog_reflist_entry *re;
6414 TAILQ_FOREACH(sre, &tog_refs, entry) {
6415 if (strncmp(got_ref_get_name(sre->ref),
6416 "refs/got/", 9) == 0 &&
6417 strncmp(got_ref_get_name(sre->ref),
6418 "refs/got/backup/", 16) != 0)
6421 re = malloc(sizeof(*re));
6423 return got_error_from_errno("malloc");
6425 re->ref = got_ref_dup(sre->ref);
6426 if (re->ref == NULL)
6427 return got_error_from_errno("got_ref_dup");
6428 re->idx = s->nrefs++;
6429 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6432 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6437 ref_view_free_refs(struct tog_ref_view_state *s)
6439 struct tog_reflist_entry *re;
6441 while (!TAILQ_EMPTY(&s->refs)) {
6442 re = TAILQ_FIRST(&s->refs);
6443 TAILQ_REMOVE(&s->refs, re, entry);
6444 got_ref_close(re->ref);
6449 static const struct got_error *
6450 open_ref_view(struct tog_view *view, struct got_repository *repo)
6452 const struct got_error *err = NULL;
6453 struct tog_ref_view_state *s = &view->state.ref;
6455 s->selected_entry = 0;
6458 TAILQ_INIT(&s->refs);
6459 STAILQ_INIT(&s->colors);
6461 err = ref_view_load_refs(s);
6465 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6466 err = add_color(&s->colors, "^refs/heads/",
6467 TOG_COLOR_REFS_HEADS,
6468 get_color_value("TOG_COLOR_REFS_HEADS"));
6472 err = add_color(&s->colors, "^refs/tags/",
6473 TOG_COLOR_REFS_TAGS,
6474 get_color_value("TOG_COLOR_REFS_TAGS"));
6478 err = add_color(&s->colors, "^refs/remotes/",
6479 TOG_COLOR_REFS_REMOTES,
6480 get_color_value("TOG_COLOR_REFS_REMOTES"));
6484 err = add_color(&s->colors, "^refs/got/backup/",
6485 TOG_COLOR_REFS_BACKUP,
6486 get_color_value("TOG_COLOR_REFS_BACKUP"));
6491 view->show = show_ref_view;
6492 view->input = input_ref_view;
6493 view->close = close_ref_view;
6494 view->search_start = search_start_ref_view;
6495 view->search_next = search_next_ref_view;
6498 free_colors(&s->colors);
6502 static const struct got_error *
6503 close_ref_view(struct tog_view *view)
6505 struct tog_ref_view_state *s = &view->state.ref;
6507 ref_view_free_refs(s);
6508 free_colors(&s->colors);
6513 static const struct got_error *
6514 resolve_reflist_entry(struct got_object_id **commit_id,
6515 struct tog_reflist_entry *re, struct got_repository *repo)
6517 const struct got_error *err = NULL;
6518 struct got_object_id *obj_id;
6519 struct got_tag_object *tag = NULL;
6524 err = got_ref_resolve(&obj_id, repo, re->ref);
6528 err = got_object_get_type(&obj_type, repo, obj_id);
6533 case GOT_OBJ_TYPE_COMMIT:
6534 *commit_id = obj_id;
6536 case GOT_OBJ_TYPE_TAG:
6537 err = got_object_open_as_tag(&tag, repo, obj_id);
6541 err = got_object_get_type(&obj_type, repo,
6542 got_object_tag_get_object_id(tag));
6545 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6546 err = got_error(GOT_ERR_OBJ_TYPE);
6549 *commit_id = got_object_id_dup(
6550 got_object_tag_get_object_id(tag));
6551 if (*commit_id == NULL) {
6552 err = got_error_from_errno("got_object_id_dup");
6557 err = got_error(GOT_ERR_OBJ_TYPE);
6563 got_object_tag_close(tag);
6571 static const struct got_error *
6572 log_ref_entry(struct tog_view **new_view, int begin_x,
6573 struct tog_reflist_entry *re, struct got_repository *repo)
6575 struct tog_view *log_view;
6576 const struct got_error *err = NULL;
6577 struct got_object_id *commit_id = NULL;
6581 err = resolve_reflist_entry(&commit_id, re, repo);
6583 if (err->code != GOT_ERR_OBJ_TYPE)
6589 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6590 if (log_view == NULL) {
6591 err = got_error_from_errno("view_open");
6595 err = open_log_view(log_view, commit_id, repo,
6596 got_ref_get_name(re->ref), "", 0);
6599 view_close(log_view);
6601 *new_view = log_view;
6607 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6609 struct tog_reflist_entry *re;
6612 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6615 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6616 while (i++ < maxscroll) {
6619 s->first_displayed_entry = re;
6620 re = TAILQ_PREV(re, tog_reflist_head, entry);
6625 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6627 struct tog_reflist_entry *next, *last;
6630 if (s->first_displayed_entry)
6631 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6633 next = TAILQ_FIRST(&s->refs);
6635 last = s->last_displayed_entry;
6636 while (next && last && n++ < maxscroll) {
6637 last = TAILQ_NEXT(last, entry);
6639 s->first_displayed_entry = next;
6640 next = TAILQ_NEXT(next, entry);
6645 static const struct got_error *
6646 search_start_ref_view(struct tog_view *view)
6648 struct tog_ref_view_state *s = &view->state.ref;
6650 s->matched_entry = NULL;
6655 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6657 regmatch_t regmatch;
6659 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6663 static const struct got_error *
6664 search_next_ref_view(struct tog_view *view)
6666 struct tog_ref_view_state *s = &view->state.ref;
6667 struct tog_reflist_entry *re = NULL;
6669 if (!view->searching) {
6670 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6674 if (s->matched_entry) {
6675 if (view->searching == TOG_SEARCH_FORWARD) {
6676 if (s->selected_entry)
6677 re = TAILQ_NEXT(s->selected_entry, entry);
6679 re = TAILQ_PREV(s->selected_entry,
6680 tog_reflist_head, entry);
6682 if (s->selected_entry == NULL)
6683 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6685 re = TAILQ_PREV(s->selected_entry,
6686 tog_reflist_head, entry);
6689 if (s->selected_entry)
6690 re = s->selected_entry;
6691 else if (view->searching == TOG_SEARCH_FORWARD)
6692 re = TAILQ_FIRST(&s->refs);
6694 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6699 if (s->matched_entry == NULL) {
6700 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6703 if (view->searching == TOG_SEARCH_FORWARD)
6704 re = TAILQ_FIRST(&s->refs);
6706 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6709 if (match_reflist_entry(re, &view->regex)) {
6710 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6711 s->matched_entry = re;
6715 if (view->searching == TOG_SEARCH_FORWARD)
6716 re = TAILQ_NEXT(re, entry);
6718 re = TAILQ_PREV(re, tog_reflist_head, entry);
6721 if (s->matched_entry) {
6722 s->first_displayed_entry = s->matched_entry;
6729 static const struct got_error *
6730 show_ref_view(struct tog_view *view)
6732 const struct got_error *err = NULL;
6733 struct tog_ref_view_state *s = &view->state.ref;
6734 struct tog_reflist_entry *re;
6737 struct tog_color *tc;
6739 int limit = view->nlines;
6741 werase(view->window);
6748 re = s->first_displayed_entry;
6750 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6752 return got_error_from_errno("asprintf");
6754 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6759 if (view_needs_focus_indication(view))
6760 wstandout(view->window);
6761 waddwstr(view->window, wline);
6762 if (view_needs_focus_indication(view))
6763 wstandend(view->window);
6768 if (width < view->ncols - 1)
6769 waddch(view->window, '\n');
6774 while (re && limit > 0) {
6776 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6779 struct got_commit_object *ci;
6780 struct got_tag_object *tag;
6781 struct got_object_id *id;
6785 err = got_ref_resolve(&id, s->repo, re->ref);
6788 err = got_object_open_as_tag(&tag, s->repo, id);
6790 if (err->code != GOT_ERR_OBJ_TYPE) {
6794 err = got_object_open_as_commit(&ci, s->repo,
6800 t = got_object_commit_get_committer_time(ci);
6801 got_object_commit_close(ci);
6803 t = got_object_tag_get_tagger_time(tag);
6804 got_object_tag_close(tag);
6807 if (gmtime_r(&t, &tm) == NULL)
6808 return got_error_from_errno("gmtime_r");
6809 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6810 return got_error(GOT_ERR_NO_SPACE);
6812 if (got_ref_is_symbolic(re->ref)) {
6813 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6814 ymd : "", got_ref_get_name(re->ref),
6815 got_ref_get_symref_target(re->ref)) == -1)
6816 return got_error_from_errno("asprintf");
6817 } else if (s->show_ids) {
6818 struct got_object_id *id;
6820 err = got_ref_resolve(&id, s->repo, re->ref);
6823 err = got_object_id_str(&id_str, id);
6828 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6829 got_ref_get_name(re->ref), id_str) == -1) {
6830 err = got_error_from_errno("asprintf");
6837 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6838 got_ref_get_name(re->ref)) == -1)
6839 return got_error_from_errno("asprintf");
6841 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6847 if (n == s->selected) {
6849 wstandout(view->window);
6850 s->selected_entry = re;
6852 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6854 wattr_on(view->window,
6855 COLOR_PAIR(tc->colorpair), NULL);
6856 waddwstr(view->window, wline);
6858 wattr_off(view->window,
6859 COLOR_PAIR(tc->colorpair), NULL);
6860 if (width < view->ncols - 1)
6861 waddch(view->window, '\n');
6862 if (n == s->selected && view->focussed)
6863 wstandend(view->window);
6869 s->last_displayed_entry = re;
6872 re = TAILQ_NEXT(re, entry);
6879 static const struct got_error *
6880 browse_ref_tree(struct tog_view **new_view, int begin_x,
6881 struct tog_reflist_entry *re, struct got_repository *repo)
6883 const struct got_error *err = NULL;
6884 struct got_object_id *commit_id = NULL;
6885 struct tog_view *tree_view;
6889 err = resolve_reflist_entry(&commit_id, re, repo);
6891 if (err->code != GOT_ERR_OBJ_TYPE)
6898 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6899 if (tree_view == NULL) {
6900 err = got_error_from_errno("view_open");
6904 err = open_tree_view(tree_view, commit_id,
6905 got_ref_get_name(re->ref), repo);
6909 *new_view = tree_view;
6914 static const struct got_error *
6915 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6917 const struct got_error *err = NULL;
6918 struct tog_ref_view_state *s = &view->state.ref;
6919 struct tog_view *log_view, *tree_view;
6920 struct tog_reflist_entry *re;
6921 int begin_x = 0, n, nscroll = view->nlines - 1;
6925 s->show_ids = !s->show_ids;
6929 s->show_date = !s->show_date;
6933 s->sort_by_date = !s->sort_by_date;
6935 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6936 got_ref_cmp_by_commit_timestamp_descending :
6937 tog_ref_cmp_by_name, s->repo);
6940 got_reflist_object_id_map_free(tog_refs_idmap);
6941 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6942 &tog_refs, s->repo);
6945 ref_view_free_refs(s);
6946 err = ref_view_load_refs(s);
6951 if (!s->selected_entry)
6953 if (view_is_parent_view(view))
6954 begin_x = view_split_begin_x(view->begin_x);
6955 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6958 log_view->focussed = 1;
6959 if (view_is_parent_view(view)) {
6960 err = view_close_child(view);
6963 err = view_set_child(view, log_view);
6966 view->focus_child = 1;
6968 *new_view = log_view;
6972 if (!s->selected_entry)
6974 if (view_is_parent_view(view))
6975 begin_x = view_split_begin_x(view->begin_x);
6976 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6978 if (err || tree_view == NULL)
6981 tree_view->focussed = 1;
6982 if (view_is_parent_view(view)) {
6983 err = view_close_child(view);
6986 err = view_set_child(view, tree_view);
6989 view->focus_child = 1;
6991 *new_view = tree_view;
6997 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
7003 re = TAILQ_LAST(&s->refs, tog_reflist_head);
7004 for (n = 0; n < view->nlines - 1; n++) {
7007 s->first_displayed_entry = re;
7008 re = TAILQ_PREV(re, tog_reflist_head, entry);
7011 s->selected = n - 1;
7016 if (s->selected > 0) {
7020 ref_scroll_up(s, 1);
7021 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7031 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
7032 s->selected -= MIN(nscroll, s->selected);
7033 ref_scroll_up(s, MAX(0, nscroll));
7034 if (s->selected_entry == TAILQ_FIRST(&s->refs))
7040 if (s->selected < s->ndisplayed - 1) {
7044 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7045 /* can't scroll any further */
7049 ref_scroll_down(s, 1);
7059 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
7060 /* can't scroll any further; move cursor down */
7061 if (s->selected < s->ndisplayed - 1)
7062 s->selected += MIN(nscroll,
7063 s->ndisplayed - s->selected - 1);
7064 if (view->count > 1 && s->selected < s->ndisplayed - 1)
7065 s->selected += s->ndisplayed - s->selected - 1;
7069 ref_scroll_down(s, nscroll);
7074 err = tog_load_refs(s->repo, s->sort_by_date);
7077 ref_view_free_refs(s);
7078 err = ref_view_load_refs(s);
7081 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
7082 s->selected = view->nlines - 2;
7096 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
7101 static const struct got_error *
7102 cmd_ref(int argc, char *argv[])
7104 const struct got_error *error;
7105 struct got_repository *repo = NULL;
7106 struct got_worktree *worktree = NULL;
7107 char *cwd = NULL, *repo_path = NULL;
7109 struct tog_view *view;
7110 int *pack_fds = NULL;
7112 while ((ch = getopt(argc, argv, "r:")) != -1) {
7115 repo_path = realpath(optarg, NULL);
7116 if (repo_path == NULL)
7117 return got_error_from_errno2("realpath",
7132 error = got_repo_pack_fds_open(&pack_fds);
7136 if (repo_path == NULL) {
7137 cwd = getcwd(NULL, 0);
7139 return got_error_from_errno("getcwd");
7140 error = got_worktree_open(&worktree, cwd);
7141 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7145 strdup(got_worktree_get_repo_path(worktree));
7147 repo_path = strdup(cwd);
7148 if (repo_path == NULL) {
7149 error = got_error_from_errno("strdup");
7154 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7160 error = apply_unveil(got_repo_get_path(repo), NULL);
7164 error = tog_load_refs(repo, 0);
7168 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
7170 error = got_error_from_errno("view_open");
7174 error = open_ref_view(view, repo);
7179 /* Release work tree lock. */
7180 got_worktree_close(worktree);
7183 error = view_loop(view);
7188 const struct got_error *close_err = got_repo_close(repo);
7193 const struct got_error *pack_err =
7194 got_repo_pack_fds_close(pack_fds);
7203 list_commands(FILE *fp)
7207 fprintf(fp, "commands:");
7208 for (i = 0; i < nitems(tog_commands); i++) {
7209 const struct tog_cmd *cmd = &tog_commands[i];
7210 fprintf(fp, " %s", cmd->name);
7216 usage(int hflag, int status)
7218 FILE *fp = (status == 0) ? stdout : stderr;
7220 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7223 fprintf(fp, "lazy usage: %s path\n", getprogname());
7230 make_argv(int argc, ...)
7238 argv = calloc(argc, sizeof(char *));
7241 for (i = 0; i < argc; i++) {
7242 argv[i] = strdup(va_arg(ap, char *));
7243 if (argv[i] == NULL)
7252 * Try to convert 'tog path' into a 'tog log path' command.
7253 * The user could simply have mistyped the command rather than knowingly
7254 * provided a path. So check whether argv[0] can in fact be resolved
7255 * to a path in the HEAD commit and print a special error if not.
7256 * This hack is for mpi@ <3
7258 static const struct got_error *
7259 tog_log_with_path(int argc, char *argv[])
7261 const struct got_error *error = NULL, *close_err;
7262 const struct tog_cmd *cmd = NULL;
7263 struct got_repository *repo = NULL;
7264 struct got_worktree *worktree = NULL;
7265 struct got_object_id *commit_id = NULL, *id = NULL;
7266 struct got_commit_object *commit = NULL;
7267 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7268 char *commit_id_str = NULL, **cmd_argv = NULL;
7269 int *pack_fds = NULL;
7271 cwd = getcwd(NULL, 0);
7273 return got_error_from_errno("getcwd");
7275 error = got_repo_pack_fds_open(&pack_fds);
7279 error = got_worktree_open(&worktree, cwd);
7280 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7284 repo_path = strdup(got_worktree_get_repo_path(worktree));
7286 repo_path = strdup(cwd);
7287 if (repo_path == NULL) {
7288 error = got_error_from_errno("strdup");
7292 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7296 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7301 error = tog_load_refs(repo, 0);
7304 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7305 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7306 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7311 got_worktree_close(worktree);
7315 error = got_object_open_as_commit(&commit, repo, commit_id);
7319 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7321 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7323 fprintf(stderr, "%s: '%s' is no known command or path\n",
7324 getprogname(), argv[0]);
7329 close_err = got_repo_close(repo);
7334 error = got_object_id_str(&commit_id_str, commit_id);
7338 cmd = &tog_commands[0]; /* log */
7340 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7341 error = cmd->cmd_main(argc, cmd_argv);
7344 close_err = got_repo_close(repo);
7349 got_object_commit_close(commit);
7351 got_worktree_close(worktree);
7353 const struct got_error *pack_err =
7354 got_repo_pack_fds_close(pack_fds);
7359 free(commit_id_str);
7366 for (i = 0; i < argc; i++)
7375 main(int argc, char *argv[])
7377 const struct got_error *error = NULL;
7378 const struct tog_cmd *cmd = NULL;
7379 int ch, hflag = 0, Vflag = 0;
7380 char **cmd_argv = NULL;
7381 static const struct option longopts[] = {
7382 { "version", no_argument, NULL, 'V' },
7386 setlocale(LC_CTYPE, "");
7388 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7408 got_version_print_str();
7413 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7421 /* Build an argument vector which runs a default command. */
7422 cmd = &tog_commands[0];
7424 cmd_argv = make_argv(argc, cmd->name);
7428 /* Did the user specify a command? */
7429 for (i = 0; i < nitems(tog_commands); i++) {
7430 if (strncmp(tog_commands[i].name, argv[0],
7431 strlen(argv[0])) == 0) {
7432 cmd = &tog_commands[i];
7441 /* No command specified; try log with a path */
7442 error = tog_log_with_path(argc, argv);
7447 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7454 for (i = 0; i < argc; i++)
7459 if (error && error->code != GOT_ERR_CANCELLED)
7460 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);