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);
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 focussed; /* Only set on one parent or child view at a time. */
514 struct tog_view *parent;
515 struct tog_view *child;
518 * This flag is initially set on parent views when a new child view
519 * is created. It gets toggled when the 'Tab' key switches focus
520 * between parent and child.
521 * The flag indicates whether focus should be passed on to our child
522 * view if this parent view gets picked for focus after another parent
523 * view was closed. This prevents child views from losing focus in such
528 /* type-specific state */
529 enum tog_view_type type;
531 struct tog_diff_view_state diff;
532 struct tog_log_view_state log;
533 struct tog_blame_view_state blame;
534 struct tog_tree_view_state tree;
535 struct tog_ref_view_state ref;
538 const struct got_error *(*show)(struct tog_view *);
539 const struct got_error *(*input)(struct tog_view **,
540 struct tog_view *, int);
541 const struct got_error *(*close)(struct tog_view *);
543 const struct got_error *(*search_start)(struct tog_view *);
544 const struct got_error *(*search_next)(struct tog_view *);
547 #define TOG_SEARCH_FORWARD 1
548 #define TOG_SEARCH_BACKWARD 2
549 int search_next_done;
550 #define TOG_SEARCH_HAVE_MORE 1
551 #define TOG_SEARCH_NO_MORE 2
552 #define TOG_SEARCH_HAVE_NONE 3
557 static const struct got_error *open_diff_view(struct tog_view *,
558 struct got_object_id *, struct got_object_id *,
559 const char *, const char *, int, int, int, struct tog_view *,
560 struct got_repository *);
561 static const struct got_error *show_diff_view(struct tog_view *);
562 static const struct got_error *input_diff_view(struct tog_view **,
563 struct tog_view *, int);
564 static const struct got_error* close_diff_view(struct tog_view *);
565 static const struct got_error *search_start_diff_view(struct tog_view *);
566 static const struct got_error *search_next_diff_view(struct tog_view *);
568 static const struct got_error *open_log_view(struct tog_view *,
569 struct got_object_id *, struct got_repository *,
570 const char *, const char *, int);
571 static const struct got_error * show_log_view(struct tog_view *);
572 static const struct got_error *input_log_view(struct tog_view **,
573 struct tog_view *, int);
574 static const struct got_error *close_log_view(struct tog_view *);
575 static const struct got_error *search_start_log_view(struct tog_view *);
576 static const struct got_error *search_next_log_view(struct tog_view *);
578 static const struct got_error *open_blame_view(struct tog_view *, char *,
579 struct got_object_id *, struct got_repository *);
580 static const struct got_error *show_blame_view(struct tog_view *);
581 static const struct got_error *input_blame_view(struct tog_view **,
582 struct tog_view *, int);
583 static const struct got_error *close_blame_view(struct tog_view *);
584 static const struct got_error *search_start_blame_view(struct tog_view *);
585 static const struct got_error *search_next_blame_view(struct tog_view *);
587 static const struct got_error *open_tree_view(struct tog_view *,
588 struct got_object_id *, const char *, struct got_repository *);
589 static const struct got_error *show_tree_view(struct tog_view *);
590 static const struct got_error *input_tree_view(struct tog_view **,
591 struct tog_view *, int);
592 static const struct got_error *close_tree_view(struct tog_view *);
593 static const struct got_error *search_start_tree_view(struct tog_view *);
594 static const struct got_error *search_next_tree_view(struct tog_view *);
596 static const struct got_error *open_ref_view(struct tog_view *,
597 struct got_repository *);
598 static const struct got_error *show_ref_view(struct tog_view *);
599 static const struct got_error *input_ref_view(struct tog_view **,
600 struct tog_view *, int);
601 static const struct got_error *close_ref_view(struct tog_view *);
602 static const struct got_error *search_start_ref_view(struct tog_view *);
603 static const struct got_error *search_next_ref_view(struct tog_view *);
605 static volatile sig_atomic_t tog_sigwinch_received;
606 static volatile sig_atomic_t tog_sigpipe_received;
607 static volatile sig_atomic_t tog_sigcont_received;
608 static volatile sig_atomic_t tog_sigint_received;
609 static volatile sig_atomic_t tog_sigterm_received;
612 tog_sigwinch(int signo)
614 tog_sigwinch_received = 1;
618 tog_sigpipe(int signo)
620 tog_sigpipe_received = 1;
624 tog_sigcont(int signo)
626 tog_sigcont_received = 1;
630 tog_sigint(int signo)
632 tog_sigint_received = 1;
636 tog_sigterm(int signo)
638 tog_sigterm_received = 1;
642 tog_fatal_signal_received(void)
644 return (tog_sigpipe_received ||
645 tog_sigint_received || tog_sigint_received);
649 static const struct got_error *
650 view_close(struct tog_view *view)
652 const struct got_error *err = NULL;
655 view_close(view->child);
659 err = view->close(view);
661 del_panel(view->panel);
663 delwin(view->window);
668 static struct tog_view *
669 view_open(int nlines, int ncols, int begin_y, int begin_x,
670 enum tog_view_type type)
672 struct tog_view *view = calloc(1, sizeof(*view));
680 view->nlines = nlines ? nlines : LINES - begin_y;
681 view->ncols = ncols ? ncols : COLS - begin_x;
682 view->begin_y = begin_y;
683 view->begin_x = begin_x;
684 view->window = newwin(nlines, ncols, begin_y, begin_x);
685 if (view->window == NULL) {
689 view->panel = new_panel(view->window);
690 if (view->panel == NULL ||
691 set_panel_userptr(view->panel, view) != OK) {
696 keypad(view->window, TRUE);
701 view_split_begin_x(int begin_x)
703 if (begin_x > 0 || COLS < 120)
705 return (COLS - MAX(COLS / 2, 80));
708 static const struct got_error *view_resize(struct tog_view *);
710 static const struct got_error *
711 view_splitscreen(struct tog_view *view)
713 const struct got_error *err = NULL;
716 view->begin_x = view_split_begin_x(0);
717 view->nlines = LINES;
718 view->ncols = COLS - view->begin_x;
721 err = view_resize(view);
725 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
726 return got_error_from_errno("mvwin");
731 static const struct got_error *
732 view_fullscreen(struct tog_view *view)
734 const struct got_error *err = NULL;
738 view->nlines = LINES;
742 err = view_resize(view);
746 if (mvwin(view->window, view->begin_y, view->begin_x) == ERR)
747 return got_error_from_errno("mvwin");
753 view_is_parent_view(struct tog_view *view)
755 return view->parent == NULL;
758 static const struct got_error *
759 view_resize(struct tog_view *view)
763 if (view->lines > LINES)
764 nlines = view->nlines - (view->lines - LINES);
766 nlines = view->nlines + (LINES - view->lines);
768 if (view->cols > COLS)
769 ncols = view->ncols - (view->cols - COLS);
771 ncols = view->ncols + (COLS - view->cols);
774 view->child->begin_x = view_split_begin_x(view->begin_x);
775 if (view->child->begin_x == 0) {
778 view_fullscreen(view->child);
779 if (view->child->focussed)
780 show_panel(view->child->panel);
782 show_panel(view->panel);
784 ncols = view->child->begin_x;
786 view_splitscreen(view->child);
787 show_panel(view->child->panel);
789 } else if (view->parent == NULL)
792 if (wresize(view->window, nlines, ncols) == ERR)
793 return got_error_from_errno("wresize");
794 if (replace_panel(view->panel, view->window) == ERR)
795 return got_error_from_errno("replace_panel");
796 wclear(view->window);
798 view->nlines = nlines;
806 static const struct got_error *
807 view_close_child(struct tog_view *view)
809 const struct got_error *err = NULL;
811 if (view->child == NULL)
814 err = view_close(view->child);
819 static const struct got_error *
820 view_set_child(struct tog_view *view, struct tog_view *child)
823 child->parent = view;
825 return view_resize(view);
829 view_is_splitscreen(struct tog_view *view)
831 return view->begin_x > 0;
840 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0) {
841 cols = 80; /* Default */
847 resize_term(lines, cols);
850 static const struct got_error *
851 view_search_start(struct tog_view *view)
853 const struct got_error *err = NULL;
857 if (view->search_started) {
858 regfree(&view->regex);
860 memset(&view->regmatch, 0, sizeof(view->regmatch));
862 view->search_started = 0;
864 if (view->nlines < 1)
867 mvwaddstr(view->window, view->begin_y + view->nlines - 1, 0, "/");
868 wclrtoeol(view->window);
872 ret = wgetnstr(view->window, pattern, sizeof(pattern));
878 if (regcomp(&view->regex, pattern, REG_EXTENDED | REG_NEWLINE) == 0) {
879 err = view->search_start(view);
881 regfree(&view->regex);
884 view->search_started = 1;
885 view->searching = TOG_SEARCH_FORWARD;
886 view->search_next_done = 0;
887 view->search_next(view);
893 static const struct got_error *
894 view_input(struct tog_view **new, int *done, struct tog_view *view,
895 struct tog_view_list_head *views)
897 const struct got_error *err = NULL;
903 /* Clear "no matches" indicator. */
904 if (view->search_next_done == TOG_SEARCH_NO_MORE ||
905 view->search_next_done == TOG_SEARCH_HAVE_NONE)
906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
908 if (view->searching && !view->search_next_done) {
909 errcode = pthread_mutex_unlock(&tog_mutex);
911 return got_error_set_errno(errcode,
912 "pthread_mutex_unlock");
914 errcode = pthread_mutex_lock(&tog_mutex);
916 return got_error_set_errno(errcode,
917 "pthread_mutex_lock");
918 view->search_next(view);
922 nodelay(stdscr, FALSE);
923 /* Allow threads to make progress while we are waiting for input. */
924 errcode = pthread_mutex_unlock(&tog_mutex);
926 return got_error_set_errno(errcode, "pthread_mutex_unlock");
927 ch = wgetch(view->window);
928 errcode = pthread_mutex_lock(&tog_mutex);
930 return got_error_set_errno(errcode, "pthread_mutex_lock");
931 nodelay(stdscr, TRUE);
933 if (tog_sigwinch_received || tog_sigcont_received) {
935 tog_sigwinch_received = 0;
936 tog_sigcont_received = 0;
937 TAILQ_FOREACH(v, views, entry) {
938 err = view_resize(v);
941 err = v->input(new, v, KEY_RESIZE);
945 err = view_resize(v->child);
948 err = v->child->input(new, v->child,
960 view->child->focussed = 1;
961 view->focus_child = 1;
962 } else if (view->parent) {
964 view->parent->focussed = 1;
965 view->parent->focus_child = 0;
969 err = view->input(new, view, ch);
976 if (view_is_parent_view(view)) {
977 if (view->child == NULL)
979 if (view_is_splitscreen(view->child)) {
981 view->child->focussed = 1;
982 err = view_fullscreen(view->child);
984 err = view_splitscreen(view->child);
987 err = view->child->input(new, view->child,
990 if (view_is_splitscreen(view)) {
991 view->parent->focussed = 0;
993 err = view_fullscreen(view);
995 err = view_splitscreen(view);
999 err = view->input(new, view, KEY_RESIZE);
1005 if (view->search_start)
1006 view_search_start(view);
1008 err = view->input(new, view, ch);
1012 if (view->search_started && view->search_next) {
1013 view->searching = (ch == 'n' ?
1014 TOG_SEARCH_FORWARD : TOG_SEARCH_BACKWARD);
1015 view->search_next_done = 0;
1016 view->search_next(view);
1018 err = view->input(new, view, ch);
1021 err = view->input(new, view, ch);
1029 view_vborder(struct tog_view *view)
1032 const struct tog_view *view_above;
1035 return view_vborder(view->parent);
1037 panel = panel_above(view->panel);
1041 view_above = panel_userptr(panel);
1042 mvwvline(view->window, view->begin_y, view_above->begin_x - 1,
1043 got_locale_is_utf8() ? ACS_VLINE : '|', view->nlines);
1047 view_needs_focus_indication(struct tog_view *view)
1049 if (view_is_parent_view(view)) {
1050 if (view->child == NULL || view->child->focussed)
1052 if (!view_is_splitscreen(view->child))
1054 } else if (!view_is_splitscreen(view))
1057 return view->focussed;
1060 static const struct got_error *
1061 view_loop(struct tog_view *view)
1063 const struct got_error *err = NULL;
1064 struct tog_view_list_head views;
1065 struct tog_view *new_view;
1066 int fast_refresh = 10;
1067 int done = 0, errcode;
1069 errcode = pthread_mutex_lock(&tog_mutex);
1071 return got_error_set_errno(errcode, "pthread_mutex_lock");
1074 TAILQ_INSERT_HEAD(&views, view, entry);
1077 err = view->show(view);
1082 while (!TAILQ_EMPTY(&views) && !done && !tog_fatal_signal_received()) {
1083 /* Refresh fast during initialization, then become slower. */
1084 if (fast_refresh && fast_refresh-- == 0)
1085 halfdelay(10); /* switch to once per second */
1087 err = view_input(&new_view, &done, view, &views);
1091 struct tog_view *v, *prev = NULL;
1093 if (view_is_parent_view(view))
1094 prev = TAILQ_PREV(view, tog_view_list_head,
1096 else if (view->parent)
1097 prev = view->parent;
1100 view->parent->child = NULL;
1101 view->parent->focus_child = 0;
1103 err = view_resize(view->parent);
1107 TAILQ_REMOVE(&views, view, entry);
1109 err = view_close(view);
1114 TAILQ_FOREACH(v, &views, entry) {
1118 if (view == NULL && new_view == NULL) {
1119 /* No view has focus. Try to pick one. */
1122 else if (!TAILQ_EMPTY(&views)) {
1123 view = TAILQ_LAST(&views,
1124 tog_view_list_head);
1127 if (view->focus_child) {
1128 view->child->focussed = 1;
1136 struct tog_view *v, *t;
1137 /* Only allow one parent view per type. */
1138 TAILQ_FOREACH_SAFE(v, &views, entry, t) {
1139 if (v->type != new_view->type)
1141 TAILQ_REMOVE(&views, v, entry);
1142 err = view_close(v);
1147 TAILQ_INSERT_TAIL(&views, new_view, entry);
1151 if (view_is_parent_view(view)) {
1152 if (view->child && view->child->focussed)
1155 if (view->parent && view->parent->focussed)
1156 view = view->parent;
1158 show_panel(view->panel);
1159 if (view->child && view_is_splitscreen(view->child))
1160 show_panel(view->child->panel);
1161 if (view->parent && view_is_splitscreen(view)) {
1162 err = view->parent->show(view->parent);
1166 err = view->show(view);
1170 err = view->child->show(view->child);
1179 while (!TAILQ_EMPTY(&views)) {
1180 view = TAILQ_FIRST(&views);
1181 TAILQ_REMOVE(&views, view, entry);
1185 errcode = pthread_mutex_unlock(&tog_mutex);
1186 if (errcode && err == NULL)
1187 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
1197 "usage: %s log [-b] [-c commit] [-r repository-path] [path]\n",
1202 /* Create newly allocated wide-character string equivalent to a byte string. */
1203 static const struct got_error *
1204 mbs2ws(wchar_t **ws, size_t *wlen, const char *s)
1207 const struct got_error *err = NULL;
1210 *wlen = mbstowcs(NULL, s, 0);
1211 if (*wlen == (size_t)-1) {
1213 if (errno != EILSEQ)
1214 return got_error_from_errno("mbstowcs");
1216 /* byte string invalid in current encoding; try to "fix" it */
1217 err = got_mbsavis(&vis, &vislen, s);
1220 *wlen = mbstowcs(NULL, vis, 0);
1221 if (*wlen == (size_t)-1) {
1222 err = got_error_from_errno("mbstowcs"); /* give up */
1227 *ws = calloc(*wlen + 1, sizeof(**ws));
1229 err = got_error_from_errno("calloc");
1233 if (mbstowcs(*ws, vis ? vis : s, *wlen) != *wlen)
1234 err = got_error_from_errno("mbstowcs");
1245 static const struct got_error *
1246 expand_tab(char **ptr, const char *src)
1249 size_t len, n, idx = 0, sz = 0;
1252 n = len = strlen(src);
1253 dst = malloc(n + 1);
1255 return got_error_from_errno("malloc");
1257 while (idx < len && src[idx]) {
1258 const char c = src[idx];
1261 size_t nb = TABSIZE - sz % TABSIZE;
1264 p = realloc(dst, n + nb);
1267 return got_error_from_errno("realloc");
1272 memset(dst + sz, ' ', nb);
1275 dst[sz++] = src[idx];
1285 * Advance at most n columns from wline starting at offset off.
1286 * Return the index to the first character after the span operation.
1287 * Return the combined column width of all spanned wide character in
1291 span_wline(int *rcol, int off, wchar_t *wline, int n, int col_tab_align)
1293 int width, i, cols = 0;
1300 for (i = off; wline[i] != L'\0'; ++i) {
1301 if (wline[i] == L'\t')
1302 width = TABSIZE - ((cols + col_tab_align) % TABSIZE);
1304 width = wcwidth(wline[i]);
1311 if (cols + width > n)
1321 * Format a line for display, ensuring that it won't overflow a width limit.
1322 * With scrolling, the width returned refers to the scrolled version of the
1323 * line, which starts at (*wlinep)[*scrollxp]. The caller must free *wlinep.
1325 static const struct got_error *
1326 format_line(wchar_t **wlinep, int *widthp, int *scrollxp,
1327 const char *line, int nscroll, int wlimit, int col_tab_align, int expand)
1329 const struct got_error *err = NULL;
1331 wchar_t *wline = NULL;
1340 err = expand_tab(&exstr, line);
1345 err = mbs2ws(&wline, &wlen, expand ? exstr : line);
1350 scrollx = span_wline(&cols, 0, wline, nscroll, col_tab_align);
1352 if (wlen > 0 && wline[wlen - 1] == L'\n') {
1353 wline[wlen - 1] = L'\0';
1356 if (wlen > 0 && wline[wlen - 1] == L'\r') {
1357 wline[wlen - 1] = L'\0';
1361 i = span_wline(&cols, scrollx, wline, wlimit, col_tab_align);
1367 *scrollxp = scrollx;
1375 static const struct got_error*
1376 build_refs_str(char **refs_str, struct got_reflist_head *refs,
1377 struct got_object_id *id, struct got_repository *repo)
1379 static const struct got_error *err = NULL;
1380 struct got_reflist_entry *re;
1386 TAILQ_FOREACH(re, refs, entry) {
1387 struct got_tag_object *tag = NULL;
1388 struct got_object_id *ref_id;
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1394 if (strncmp(name, "refs/", 5) == 0)
1396 if (strncmp(name, "got/", 4) == 0 &&
1397 strncmp(name, "got/backup/", 11) != 0)
1399 if (strncmp(name, "heads/", 6) == 0)
1401 if (strncmp(name, "remotes/", 8) == 0) {
1403 s = strstr(name, "/" GOT_REF_HEAD);
1404 if (s != NULL && s[strlen(s)] == '\0')
1407 err = got_ref_resolve(&ref_id, repo, re->ref);
1410 if (strncmp(name, "tags/", 5) == 0) {
1411 err = got_object_open_as_tag(&tag, repo, ref_id);
1413 if (err->code != GOT_ERR_OBJ_TYPE) {
1417 /* Ref points at something other than a tag. */
1422 cmp = got_object_id_cmp(tag ?
1423 got_object_tag_get_object_id(tag) : ref_id, id);
1426 got_object_tag_close(tag);
1430 if (asprintf(refs_str, "%s%s%s", s ? s : "",
1431 s ? ", " : "", name) == -1) {
1432 err = got_error_from_errno("asprintf");
1443 static const struct got_error *
1444 format_author(wchar_t **wauthor, int *author_width, char *author, int limit,
1449 smallerthan = strchr(author, '<');
1450 if (smallerthan && smallerthan[1] != '\0')
1451 author = smallerthan + 1;
1452 author[strcspn(author, "@>")] = '\0';
1453 return format_line(wauthor, author_width, NULL, author, 0, limit,
1457 static const struct got_error *
1458 draw_commit(struct tog_view *view, struct got_commit_object *commit,
1459 struct got_object_id *id, const size_t date_display_cols,
1460 int author_display_cols)
1462 struct tog_log_view_state *s = &view->state.log;
1463 const struct got_error *err = NULL;
1464 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
1465 char *logmsg0 = NULL, *logmsg = NULL;
1466 char *author = NULL;
1467 wchar_t *wlogmsg = NULL, *wauthor = NULL;
1468 int author_width, logmsg_width;
1469 char *newline, *line = NULL;
1470 int col, limit, scrollx;
1471 const int avail = view->ncols;
1473 time_t committer_time;
1474 struct tog_color *tc;
1476 committer_time = got_object_commit_get_committer_time(commit);
1477 if (gmtime_r(&committer_time, &tm) == NULL)
1478 return got_error_from_errno("gmtime_r");
1479 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0)
1480 return got_error(GOT_ERR_NO_SPACE);
1482 if (avail <= date_display_cols)
1483 limit = MIN(sizeof(datebuf) - 1, avail);
1485 limit = MIN(date_display_cols, sizeof(datebuf) - 1);
1486 tc = get_color(&s->colors, TOG_COLOR_DATE);
1488 wattr_on(view->window,
1489 COLOR_PAIR(tc->colorpair), NULL);
1490 waddnstr(view->window, datebuf, limit);
1492 wattr_off(view->window,
1493 COLOR_PAIR(tc->colorpair), NULL);
1500 err = got_object_id_str(&id_str, id);
1503 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1505 wattr_on(view->window,
1506 COLOR_PAIR(tc->colorpair), NULL);
1507 wprintw(view->window, "%.8s ", id_str);
1509 wattr_off(view->window,
1510 COLOR_PAIR(tc->colorpair), NULL);
1517 author = strdup(got_object_commit_get_author(commit));
1518 if (author == NULL) {
1519 err = got_error_from_errno("strdup");
1522 err = format_author(&wauthor, &author_width, author, avail - col, col);
1525 tc = get_color(&s->colors, TOG_COLOR_AUTHOR);
1527 wattr_on(view->window,
1528 COLOR_PAIR(tc->colorpair), NULL);
1529 waddwstr(view->window, wauthor);
1531 wattr_off(view->window,
1532 COLOR_PAIR(tc->colorpair), NULL);
1533 col += author_width;
1534 while (col < avail && author_width < author_display_cols + 2) {
1535 waddch(view->window, ' ');
1542 err = got_object_commit_get_logmsg(&logmsg0, commit);
1546 while (*logmsg == '\n')
1548 newline = strchr(logmsg, '\n');
1551 limit = avail - col;
1552 if (view->child && limit > 0)
1553 limit--; /* for the border */
1554 err = format_line(&wlogmsg, &logmsg_width, &scrollx, logmsg, view->x,
1558 waddwstr(view->window, &wlogmsg[scrollx]);
1559 col += MAX(logmsg_width, 0);
1560 while (col < avail) {
1561 waddch(view->window, ' ');
1573 static struct commit_queue_entry *
1574 alloc_commit_queue_entry(struct got_commit_object *commit,
1575 struct got_object_id *id)
1577 struct commit_queue_entry *entry;
1579 entry = calloc(1, sizeof(*entry));
1584 entry->commit = commit;
1589 pop_commit(struct commit_queue *commits)
1591 struct commit_queue_entry *entry;
1593 entry = TAILQ_FIRST(&commits->head);
1594 TAILQ_REMOVE(&commits->head, entry, entry);
1595 got_object_commit_close(entry->commit);
1596 commits->ncommits--;
1597 /* Don't free entry->id! It is owned by the commit graph. */
1602 free_commits(struct commit_queue *commits)
1604 while (!TAILQ_EMPTY(&commits->head))
1605 pop_commit(commits);
1608 static const struct got_error *
1609 match_commit(int *have_match, struct got_object_id *id,
1610 struct got_commit_object *commit, regex_t *regex)
1612 const struct got_error *err = NULL;
1613 regmatch_t regmatch;
1614 char *id_str = NULL, *logmsg = NULL;
1618 err = got_object_id_str(&id_str, id);
1622 err = got_object_commit_get_logmsg(&logmsg, commit);
1626 if (regexec(regex, got_object_commit_get_author(commit), 1,
1627 ®match, 0) == 0 ||
1628 regexec(regex, got_object_commit_get_committer(commit), 1,
1629 ®match, 0) == 0 ||
1630 regexec(regex, id_str, 1, ®match, 0) == 0 ||
1631 regexec(regex, logmsg, 1, ®match, 0) == 0)
1639 static const struct got_error *
1640 queue_commits(struct tog_log_thread_args *a)
1642 const struct got_error *err = NULL;
1645 * We keep all commits open throughout the lifetime of the log
1646 * view in order to avoid having to re-fetch commits from disk
1647 * while updating the display.
1650 struct got_object_id *id;
1651 struct got_commit_object *commit;
1652 struct commit_queue_entry *entry;
1655 err = got_commit_graph_iter_next(&id, a->graph, a->repo,
1657 if (err || id == NULL)
1660 err = got_object_open_as_commit(&commit, a->repo, id);
1663 entry = alloc_commit_queue_entry(commit, id);
1664 if (entry == NULL) {
1665 err = got_error_from_errno("alloc_commit_queue_entry");
1669 errcode = pthread_mutex_lock(&tog_mutex);
1671 err = got_error_set_errno(errcode,
1672 "pthread_mutex_lock");
1676 entry->idx = a->commits->ncommits;
1677 TAILQ_INSERT_TAIL(&a->commits->head, entry, entry);
1678 a->commits->ncommits++;
1680 if (*a->searching == TOG_SEARCH_FORWARD &&
1681 !*a->search_next_done) {
1683 err = match_commit(&have_match, id, commit, a->regex);
1687 *a->search_next_done = TOG_SEARCH_HAVE_MORE;
1690 errcode = pthread_mutex_unlock(&tog_mutex);
1691 if (errcode && err == NULL)
1692 err = got_error_set_errno(errcode,
1693 "pthread_mutex_unlock");
1696 } while (*a->searching == TOG_SEARCH_FORWARD && !*a->search_next_done);
1702 select_commit(struct tog_log_view_state *s)
1704 struct commit_queue_entry *entry;
1707 entry = s->first_displayed_entry;
1709 if (ncommits == s->selected) {
1710 s->selected_entry = entry;
1713 entry = TAILQ_NEXT(entry, entry);
1718 static const struct got_error *
1719 draw_commits(struct tog_view *view)
1721 const struct got_error *err = NULL;
1722 struct tog_log_view_state *s = &view->state.log;
1723 struct commit_queue_entry *entry = s->selected_entry;
1724 const int limit = view->nlines;
1726 int ncommits, author_cols = 4;
1727 char *id_str = NULL, *header = NULL, *ncommits_str = NULL;
1728 char *refs_str = NULL;
1730 struct tog_color *tc;
1731 static const size_t date_display_cols = 12;
1733 if (s->selected_entry &&
1734 !(view->searching && view->search_next_done == 0)) {
1735 struct got_reflist_head *refs;
1736 err = got_object_id_str(&id_str, s->selected_entry->id);
1739 refs = got_reflist_object_id_map_lookup(tog_refs_idmap,
1740 s->selected_entry->id);
1742 err = build_refs_str(&refs_str, refs,
1743 s->selected_entry->id, s->repo);
1749 if (s->thread_args.commits_needed == 0)
1750 halfdelay(10); /* disable fast refresh */
1752 if (s->thread_args.commits_needed > 0 || s->thread_args.load_all) {
1753 if (asprintf(&ncommits_str, " [%d/%d] %s",
1754 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1755 (view->searching && !view->search_next_done) ?
1756 "searching..." : "loading...") == -1) {
1757 err = got_error_from_errno("asprintf");
1761 const char *search_str = NULL;
1763 if (view->searching) {
1764 if (view->search_next_done == TOG_SEARCH_NO_MORE)
1765 search_str = "no more matches";
1766 else if (view->search_next_done == TOG_SEARCH_HAVE_NONE)
1767 search_str = "no matches found";
1768 else if (!view->search_next_done)
1769 search_str = "searching...";
1772 if (asprintf(&ncommits_str, " [%d/%d] %s",
1773 entry ? entry->idx + 1 : 0, s->commits.ncommits,
1774 search_str ? search_str :
1775 (refs_str ? refs_str : "")) == -1) {
1776 err = got_error_from_errno("asprintf");
1781 if (s->in_repo_path && strcmp(s->in_repo_path, "/") != 0) {
1782 if (asprintf(&header, "commit %s %s%s", id_str ? id_str :
1783 "........................................",
1784 s->in_repo_path, ncommits_str) == -1) {
1785 err = got_error_from_errno("asprintf");
1789 } else if (asprintf(&header, "commit %s%s",
1790 id_str ? id_str : "........................................",
1791 ncommits_str) == -1) {
1792 err = got_error_from_errno("asprintf");
1796 err = format_line(&wline, &width, NULL, header, 0, view->ncols, 0, 0);
1800 werase(view->window);
1802 if (view_needs_focus_indication(view))
1803 wstandout(view->window);
1804 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
1806 wattr_on(view->window,
1807 COLOR_PAIR(tc->colorpair), NULL);
1808 waddwstr(view->window, wline);
1810 wattr_off(view->window,
1811 COLOR_PAIR(tc->colorpair), NULL);
1812 while (width < view->ncols) {
1813 waddch(view->window, ' ');
1816 if (view_needs_focus_indication(view))
1817 wstandend(view->window);
1822 /* Grow author column size if necessary, and set view->maxx. */
1823 entry = s->first_displayed_entry;
1827 char *author, *eol, *msg, *msg0;
1828 wchar_t *wauthor, *wmsg;
1830 if (ncommits >= limit - 1)
1832 author = strdup(got_object_commit_get_author(entry->commit));
1833 if (author == NULL) {
1834 err = got_error_from_errno("strdup");
1837 err = format_author(&wauthor, &width, author, COLS,
1839 if (author_cols < width)
1840 author_cols = width;
1843 err = got_object_commit_get_logmsg(&msg0, entry->commit);
1847 while (*msg == '\n')
1849 if ((eol = strchr(msg, '\n')))
1851 err = format_line(&wmsg, &width, NULL, msg, 0, INT_MAX,
1852 date_display_cols + author_cols, 0);
1855 view->maxx = MAX(view->maxx, width);
1859 entry = TAILQ_NEXT(entry, entry);
1862 entry = s->first_displayed_entry;
1863 s->last_displayed_entry = s->first_displayed_entry;
1866 if (ncommits >= limit - 1)
1868 if (ncommits == s->selected)
1869 wstandout(view->window);
1870 err = draw_commit(view, entry->commit, entry->id,
1871 date_display_cols, author_cols);
1872 if (ncommits == s->selected)
1873 wstandend(view->window);
1877 s->last_displayed_entry = entry;
1878 entry = TAILQ_NEXT(entry, entry);
1893 log_scroll_up(struct tog_log_view_state *s, int maxscroll)
1895 struct commit_queue_entry *entry;
1898 entry = TAILQ_FIRST(&s->commits.head);
1899 if (s->first_displayed_entry == entry)
1902 entry = s->first_displayed_entry;
1903 while (entry && nscrolled < maxscroll) {
1904 entry = TAILQ_PREV(entry, commit_queue_head, entry);
1906 s->first_displayed_entry = entry;
1912 static const struct got_error *
1913 trigger_log_thread(struct tog_view *view, int wait)
1915 struct tog_log_thread_args *ta = &view->state.log.thread_args;
1918 halfdelay(1); /* fast refresh while loading commits */
1920 while (ta->commits_needed > 0 || ta->load_all) {
1921 if (ta->log_complete)
1924 /* Wake the log thread. */
1925 errcode = pthread_cond_signal(&ta->need_commits);
1927 return got_error_set_errno(errcode,
1928 "pthread_cond_signal");
1931 * The mutex will be released while the view loop waits
1932 * in wgetch(), at which time the log thread will run.
1937 /* Display progress update in log view. */
1938 show_log_view(view);
1942 /* Wait right here while next commit is being loaded. */
1943 errcode = pthread_cond_wait(&ta->commit_loaded, &tog_mutex);
1945 return got_error_set_errno(errcode,
1946 "pthread_cond_wait");
1948 /* Display progress update in log view. */
1949 show_log_view(view);
1957 static const struct got_error *
1958 log_scroll_down(struct tog_view *view, int maxscroll)
1960 struct tog_log_view_state *s = &view->state.log;
1961 const struct got_error *err = NULL;
1962 struct commit_queue_entry *pentry;
1963 int nscrolled = 0, ncommits_needed;
1965 if (s->last_displayed_entry == NULL)
1968 ncommits_needed = s->last_displayed_entry->idx + 1 + maxscroll;
1969 if (s->commits.ncommits < ncommits_needed &&
1970 !s->thread_args.log_complete) {
1972 * Ask the log thread for required amount of commits.
1974 s->thread_args.commits_needed += maxscroll;
1975 err = trigger_log_thread(view, 1);
1981 pentry = TAILQ_NEXT(s->last_displayed_entry, entry);
1985 s->last_displayed_entry = pentry;
1987 pentry = TAILQ_NEXT(s->first_displayed_entry, entry);
1990 s->first_displayed_entry = pentry;
1991 } while (++nscrolled < maxscroll);
1996 static const struct got_error *
1997 open_diff_view_for_commit(struct tog_view **new_view, int begin_x,
1998 struct got_commit_object *commit, struct got_object_id *commit_id,
1999 struct tog_view *log_view, struct got_repository *repo)
2001 const struct got_error *err;
2002 struct got_object_qid *parent_id;
2003 struct tog_view *diff_view;
2005 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
2006 if (diff_view == NULL)
2007 return got_error_from_errno("view_open");
2009 parent_id = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
2010 err = open_diff_view(diff_view, parent_id ? &parent_id->id : NULL,
2011 commit_id, NULL, NULL, 3, 0, 0, log_view, repo);
2013 *new_view = diff_view;
2017 static const struct got_error *
2018 tree_view_visit_subtree(struct tog_tree_view_state *s,
2019 struct got_tree_object *subtree)
2021 struct tog_parent_tree *parent;
2023 parent = calloc(1, sizeof(*parent));
2025 return got_error_from_errno("calloc");
2027 parent->tree = s->tree;
2028 parent->first_displayed_entry = s->first_displayed_entry;
2029 parent->selected_entry = s->selected_entry;
2030 parent->selected = s->selected;
2031 TAILQ_INSERT_HEAD(&s->parents, parent, entry);
2034 s->first_displayed_entry = NULL;
2038 static const struct got_error *
2039 tree_view_walk_path(struct tog_tree_view_state *s,
2040 struct got_commit_object *commit, const char *path)
2042 const struct got_error *err = NULL;
2043 struct got_tree_object *tree = NULL;
2045 char *slash, *subpath = NULL;
2047 /* Walk the path and open corresponding tree objects. */
2050 struct got_tree_entry *te;
2051 struct got_object_id *tree_id;
2057 /* Ensure the correct subtree entry is selected. */
2058 slash = strchr(p, '/');
2060 te_name = strdup(p);
2062 te_name = strndup(p, slash - p);
2063 if (te_name == NULL) {
2064 err = got_error_from_errno("strndup");
2067 te = got_object_tree_find_entry(s->tree, te_name);
2069 err = got_error_path(te_name, GOT_ERR_NO_TREE_ENTRY);
2074 s->first_displayed_entry = s->selected_entry = te;
2076 if (!S_ISDIR(got_tree_entry_get_mode(s->selected_entry)))
2077 break; /* jump to this file's entry */
2079 slash = strchr(p, '/');
2081 subpath = strndup(path, slash - path);
2083 subpath = strdup(path);
2084 if (subpath == NULL) {
2085 err = got_error_from_errno("strdup");
2089 err = got_object_id_by_path(&tree_id, s->repo, commit,
2094 err = got_object_open_as_tree(&tree, s->repo, tree_id);
2099 err = tree_view_visit_subtree(s, tree);
2101 got_object_tree_close(tree);
2115 static const struct got_error *
2116 browse_commit_tree(struct tog_view **new_view, int begin_x,
2117 struct commit_queue_entry *entry, const char *path,
2118 const char *head_ref_name, struct got_repository *repo)
2120 const struct got_error *err = NULL;
2121 struct tog_tree_view_state *s;
2122 struct tog_view *tree_view;
2124 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
2125 if (tree_view == NULL)
2126 return got_error_from_errno("view_open");
2128 err = open_tree_view(tree_view, entry->id, head_ref_name, repo);
2131 s = &tree_view->state.tree;
2133 *new_view = tree_view;
2135 if (got_path_is_root_dir(path))
2138 return tree_view_walk_path(s, entry->commit, path);
2141 static const struct got_error *
2142 block_signals_used_by_main_thread(void)
2147 if (sigemptyset(&sigset) == -1)
2148 return got_error_from_errno("sigemptyset");
2150 /* tog handles SIGWINCH, SIGCONT, SIGINT, SIGTERM */
2151 if (sigaddset(&sigset, SIGWINCH) == -1)
2152 return got_error_from_errno("sigaddset");
2153 if (sigaddset(&sigset, SIGCONT) == -1)
2154 return got_error_from_errno("sigaddset");
2155 if (sigaddset(&sigset, SIGINT) == -1)
2156 return got_error_from_errno("sigaddset");
2157 if (sigaddset(&sigset, SIGTERM) == -1)
2158 return got_error_from_errno("sigaddset");
2160 /* ncurses handles SIGTSTP */
2161 if (sigaddset(&sigset, SIGTSTP) == -1)
2162 return got_error_from_errno("sigaddset");
2164 errcode = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
2166 return got_error_set_errno(errcode, "pthread_sigmask");
2172 log_thread(void *arg)
2174 const struct got_error *err = NULL;
2176 struct tog_log_thread_args *a = arg;
2179 err = block_signals_used_by_main_thread();
2183 while (!done && !err && !tog_fatal_signal_received()) {
2184 err = queue_commits(a);
2186 if (err->code != GOT_ERR_ITER_COMPLETED)
2190 } else if (a->commits_needed > 0 && !a->load_all)
2191 a->commits_needed--;
2193 errcode = pthread_mutex_lock(&tog_mutex);
2195 err = got_error_set_errno(errcode,
2196 "pthread_mutex_lock");
2198 } else if (*a->quit)
2200 else if (*a->first_displayed_entry == NULL) {
2201 *a->first_displayed_entry =
2202 TAILQ_FIRST(&a->commits->head);
2203 *a->selected_entry = *a->first_displayed_entry;
2206 errcode = pthread_cond_signal(&a->commit_loaded);
2208 err = got_error_set_errno(errcode,
2209 "pthread_cond_signal");
2210 pthread_mutex_unlock(&tog_mutex);
2215 a->commits_needed = 0;
2217 if (a->commits_needed == 0 && !a->load_all) {
2218 errcode = pthread_cond_wait(&a->need_commits,
2221 err = got_error_set_errno(errcode,
2222 "pthread_cond_wait");
2228 errcode = pthread_mutex_unlock(&tog_mutex);
2229 if (errcode && err == NULL)
2230 err = got_error_set_errno(errcode,
2231 "pthread_mutex_unlock");
2233 a->log_complete = 1;
2237 static const struct got_error *
2238 stop_log_thread(struct tog_log_view_state *s)
2240 const struct got_error *err = NULL;
2245 errcode = pthread_cond_signal(&s->thread_args.need_commits);
2247 return got_error_set_errno(errcode,
2248 "pthread_cond_signal");
2249 errcode = pthread_mutex_unlock(&tog_mutex);
2251 return got_error_set_errno(errcode,
2252 "pthread_mutex_unlock");
2253 errcode = pthread_join(s->thread, (void **)&err);
2255 return got_error_set_errno(errcode, "pthread_join");
2256 errcode = pthread_mutex_lock(&tog_mutex);
2258 return got_error_set_errno(errcode,
2259 "pthread_mutex_lock");
2260 s->thread = 0; //NULL;
2263 if (s->thread_args.repo) {
2264 err = got_repo_close(s->thread_args.repo);
2265 s->thread_args.repo = NULL;
2268 if (s->thread_args.pack_fds) {
2269 const struct got_error *pack_err =
2270 got_repo_pack_fds_close(s->thread_args.pack_fds);
2273 s->thread_args.pack_fds = NULL;
2276 if (s->thread_args.graph) {
2277 got_commit_graph_close(s->thread_args.graph);
2278 s->thread_args.graph = NULL;
2284 static const struct got_error *
2285 close_log_view(struct tog_view *view)
2287 const struct got_error *err = NULL;
2288 struct tog_log_view_state *s = &view->state.log;
2291 err = stop_log_thread(s);
2293 errcode = pthread_cond_destroy(&s->thread_args.need_commits);
2294 if (errcode && err == NULL)
2295 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2297 errcode = pthread_cond_destroy(&s->thread_args.commit_loaded);
2298 if (errcode && err == NULL)
2299 err = got_error_set_errno(errcode, "pthread_cond_destroy");
2301 free_commits(&s->commits);
2302 free(s->in_repo_path);
2303 s->in_repo_path = NULL;
2306 free(s->head_ref_name);
2307 s->head_ref_name = NULL;
2311 static const struct got_error *
2312 search_start_log_view(struct tog_view *view)
2314 struct tog_log_view_state *s = &view->state.log;
2316 s->matched_entry = NULL;
2317 s->search_entry = NULL;
2321 static const struct got_error *
2322 search_next_log_view(struct tog_view *view)
2324 const struct got_error *err = NULL;
2325 struct tog_log_view_state *s = &view->state.log;
2326 struct commit_queue_entry *entry;
2328 /* Display progress update in log view. */
2329 show_log_view(view);
2333 if (s->search_entry) {
2335 errcode = pthread_mutex_unlock(&tog_mutex);
2337 return got_error_set_errno(errcode,
2338 "pthread_mutex_unlock");
2339 ch = wgetch(view->window);
2340 errcode = pthread_mutex_lock(&tog_mutex);
2342 return got_error_set_errno(errcode,
2343 "pthread_mutex_lock");
2344 if (ch == KEY_BACKSPACE) {
2345 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2348 if (view->searching == TOG_SEARCH_FORWARD)
2349 entry = TAILQ_NEXT(s->search_entry, entry);
2351 entry = TAILQ_PREV(s->search_entry,
2352 commit_queue_head, entry);
2353 } else if (s->matched_entry) {
2354 if (view->searching == TOG_SEARCH_FORWARD)
2355 entry = TAILQ_NEXT(s->matched_entry, entry);
2357 entry = TAILQ_PREV(s->matched_entry,
2358 commit_queue_head, entry);
2360 entry = s->selected_entry;
2366 if (entry == NULL) {
2367 if (s->thread_args.log_complete ||
2368 view->searching == TOG_SEARCH_BACKWARD) {
2369 view->search_next_done =
2370 (s->matched_entry == NULL ?
2371 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2372 s->search_entry = NULL;
2376 * Poke the log thread for more commits and return,
2377 * allowing the main loop to make progress. Search
2378 * will resume at s->search_entry once we come back.
2380 s->thread_args.commits_needed++;
2381 return trigger_log_thread(view, 0);
2384 err = match_commit(&have_match, entry->id, entry->commit,
2389 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2390 s->matched_entry = entry;
2394 s->search_entry = entry;
2395 if (view->searching == TOG_SEARCH_FORWARD)
2396 entry = TAILQ_NEXT(entry, entry);
2398 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2401 if (s->matched_entry) {
2402 int cur = s->selected_entry->idx;
2403 while (cur < s->matched_entry->idx) {
2404 err = input_log_view(NULL, view, KEY_DOWN);
2409 while (cur > s->matched_entry->idx) {
2410 err = input_log_view(NULL, view, KEY_UP);
2417 s->search_entry = NULL;
2422 static const struct got_error *
2423 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2424 struct got_repository *repo, const char *head_ref_name,
2425 const char *in_repo_path, int log_branches)
2427 const struct got_error *err = NULL;
2428 struct tog_log_view_state *s = &view->state.log;
2429 struct got_repository *thread_repo = NULL;
2430 struct got_commit_graph *thread_graph = NULL;
2433 if (in_repo_path != s->in_repo_path) {
2434 free(s->in_repo_path);
2435 s->in_repo_path = strdup(in_repo_path);
2436 if (s->in_repo_path == NULL)
2437 return got_error_from_errno("strdup");
2440 /* The commit queue only contains commits being displayed. */
2441 TAILQ_INIT(&s->commits.head);
2442 s->commits.ncommits = 0;
2445 if (head_ref_name) {
2446 s->head_ref_name = strdup(head_ref_name);
2447 if (s->head_ref_name == NULL) {
2448 err = got_error_from_errno("strdup");
2452 s->start_id = got_object_id_dup(start_id);
2453 if (s->start_id == NULL) {
2454 err = got_error_from_errno("got_object_id_dup");
2457 s->log_branches = log_branches;
2459 STAILQ_INIT(&s->colors);
2460 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2461 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2462 get_color_value("TOG_COLOR_COMMIT"));
2465 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2466 get_color_value("TOG_COLOR_AUTHOR"));
2468 free_colors(&s->colors);
2471 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2472 get_color_value("TOG_COLOR_DATE"));
2474 free_colors(&s->colors);
2479 view->show = show_log_view;
2480 view->input = input_log_view;
2481 view->close = close_log_view;
2482 view->search_start = search_start_log_view;
2483 view->search_next = search_next_log_view;
2485 if (s->thread_args.pack_fds == NULL) {
2486 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2490 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2491 s->thread_args.pack_fds);
2494 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2498 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2499 s->repo, NULL, NULL);
2503 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2505 err = got_error_set_errno(errcode, "pthread_cond_init");
2508 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2510 err = got_error_set_errno(errcode, "pthread_cond_init");
2514 s->thread_args.commits_needed = view->nlines;
2515 s->thread_args.graph = thread_graph;
2516 s->thread_args.commits = &s->commits;
2517 s->thread_args.in_repo_path = s->in_repo_path;
2518 s->thread_args.start_id = s->start_id;
2519 s->thread_args.repo = thread_repo;
2520 s->thread_args.log_complete = 0;
2521 s->thread_args.quit = &s->quit;
2522 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2523 s->thread_args.selected_entry = &s->selected_entry;
2524 s->thread_args.searching = &view->searching;
2525 s->thread_args.search_next_done = &view->search_next_done;
2526 s->thread_args.regex = &view->regex;
2529 close_log_view(view);
2533 static const struct got_error *
2534 show_log_view(struct tog_view *view)
2536 const struct got_error *err;
2537 struct tog_log_view_state *s = &view->state.log;
2539 if (s->thread == 0) { //NULL) {
2540 int errcode = pthread_create(&s->thread, NULL, log_thread,
2543 return got_error_set_errno(errcode, "pthread_create");
2544 if (s->thread_args.commits_needed > 0) {
2545 err = trigger_log_thread(view, 1);
2551 return draw_commits(view);
2554 static const struct got_error *
2555 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2557 const struct got_error *err = NULL;
2558 struct tog_log_view_state *s = &view->state.log;
2559 struct tog_view *diff_view = NULL, *tree_view = NULL;
2560 struct tog_view *ref_view = NULL;
2561 struct commit_queue_entry *entry;
2562 int begin_x = 0, n, nscroll = view->nlines - 1;
2564 if (s->thread_args.load_all) {
2565 if (ch == KEY_BACKSPACE)
2566 s->thread_args.load_all = 0;
2567 else if (s->thread_args.log_complete) {
2568 s->thread_args.load_all = 0;
2569 log_scroll_down(view, s->commits.ncommits);
2570 s->selected = MIN(view->nlines - 2,
2571 s->commits.ncommits - 1);
2585 view->x = MAX(view->maxx - view->ncols / 2, 0);
2589 if (view->x + view->ncols / 2 < view->maxx)
2590 view->x += 2; /* move two columns right */
2594 view->x -= MIN(view->x, 2); /* move two columns back */
2601 if (s->first_displayed_entry == NULL)
2603 if (s->selected > 0)
2606 log_scroll_up(s, 1);
2612 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2621 if (s->first_displayed_entry == NULL)
2623 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2624 s->selected = MAX(0, s->selected - nscroll - 1);
2626 log_scroll_up(s, nscroll);
2634 if (s->first_displayed_entry == NULL)
2636 if (s->selected < MIN(view->nlines - 2,
2637 s->commits.ncommits - 1))
2640 err = log_scroll_down(view, 1);
2648 /* We don't know yet how many commits, so we're forced to
2649 * traverse them all. */
2650 if (!s->thread_args.log_complete) {
2651 s->thread_args.load_all = 1;
2652 return trigger_log_thread(view, 0);
2656 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2657 for (n = 0; n < view->nlines - 1; n++) {
2660 s->first_displayed_entry = entry;
2661 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2664 s->selected = n - 1;
2674 struct commit_queue_entry *first;
2675 first = s->first_displayed_entry;
2678 err = log_scroll_down(view, nscroll);
2681 if (first == s->first_displayed_entry &&
2682 s->selected < MIN(view->nlines - 2,
2683 s->commits.ncommits - 1)) {
2684 /* can't scroll further down */
2685 s->selected += MIN(s->last_displayed_entry->idx -
2686 s->selected_entry->idx, nscroll + 1);
2692 if (s->selected > view->nlines - 2)
2693 s->selected = view->nlines - 2;
2694 if (s->selected > s->commits.ncommits - 1)
2695 s->selected = s->commits.ncommits - 1;
2697 if (s->commits.ncommits < view->nlines - 1 &&
2698 !s->thread_args.log_complete) {
2699 s->thread_args.commits_needed += (view->nlines - 1) -
2700 s->commits.ncommits;
2701 err = trigger_log_thread(view, 1);
2707 if (s->selected_entry == NULL)
2709 if (view_is_parent_view(view))
2710 begin_x = view_split_begin_x(view->begin_x);
2711 err = open_diff_view_for_commit(&diff_view, begin_x,
2712 s->selected_entry->commit, s->selected_entry->id,
2717 diff_view->focussed = 1;
2718 if (view_is_parent_view(view)) {
2719 err = view_close_child(view);
2722 err = view_set_child(view, diff_view);
2725 view->focus_child = 1;
2727 *new_view = diff_view;
2730 if (s->selected_entry == NULL)
2732 if (view_is_parent_view(view))
2733 begin_x = view_split_begin_x(view->begin_x);
2734 err = browse_commit_tree(&tree_view, begin_x,
2735 s->selected_entry, s->in_repo_path, s->head_ref_name,
2740 tree_view->focussed = 1;
2741 if (view_is_parent_view(view)) {
2742 err = view_close_child(view);
2745 err = view_set_child(view, tree_view);
2748 view->focus_child = 1;
2750 *new_view = tree_view;
2755 if (ch == KEY_BACKSPACE &&
2756 got_path_is_root_dir(s->in_repo_path))
2758 err = stop_log_thread(s);
2761 if (ch == KEY_BACKSPACE) {
2763 err = got_path_dirname(&parent_path, s->in_repo_path);
2766 free(s->in_repo_path);
2767 s->in_repo_path = parent_path;
2768 s->thread_args.in_repo_path = s->in_repo_path;
2769 } else if (ch == CTRL('l')) {
2770 struct got_object_id *start_id;
2771 err = got_repo_match_object_id(&start_id, NULL,
2772 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2773 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2777 s->start_id = start_id;
2778 s->thread_args.start_id = s->start_id;
2780 s->log_branches = !s->log_branches;
2782 if (s->thread_args.pack_fds == NULL) {
2783 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2787 err = got_repo_open(&s->thread_args.repo,
2788 got_repo_get_path(s->repo), NULL,
2789 s->thread_args.pack_fds);
2793 err = tog_load_refs(s->repo, 0);
2796 err = got_commit_graph_open(&s->thread_args.graph,
2797 s->in_repo_path, !s->log_branches);
2800 err = got_commit_graph_iter_start(s->thread_args.graph,
2801 s->start_id, s->repo, NULL, NULL);
2804 free_commits(&s->commits);
2805 s->first_displayed_entry = NULL;
2806 s->last_displayed_entry = NULL;
2807 s->selected_entry = NULL;
2809 s->thread_args.log_complete = 0;
2811 s->thread_args.commits_needed = view->nlines;
2814 if (view_is_parent_view(view))
2815 begin_x = view_split_begin_x(view->begin_x);
2816 ref_view = view_open(view->nlines, view->ncols,
2817 view->begin_y, begin_x, TOG_VIEW_REF);
2818 if (ref_view == NULL)
2819 return got_error_from_errno("view_open");
2820 err = open_ref_view(ref_view, s->repo);
2822 view_close(ref_view);
2826 ref_view->focussed = 1;
2827 if (view_is_parent_view(view)) {
2828 err = view_close_child(view);
2831 err = view_set_child(view, ref_view);
2834 view->focus_child = 1;
2836 *new_view = ref_view;
2845 static const struct got_error *
2846 apply_unveil(const char *repo_path, const char *worktree_path)
2848 const struct got_error *error;
2851 if (unveil("gmon.out", "rwc") != 0)
2852 return got_error_from_errno2("unveil", "gmon.out");
2854 if (repo_path && unveil(repo_path, "r") != 0)
2855 return got_error_from_errno2("unveil", repo_path);
2857 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2858 return got_error_from_errno2("unveil", worktree_path);
2860 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2861 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2863 error = got_privsep_unveil_exec_helpers();
2867 if (unveil(NULL, NULL) != 0)
2868 return got_error_from_errno("unveil");
2877 * Override default signal handlers before starting ncurses.
2878 * This should prevent ncurses from installing its own
2879 * broken cleanup() signal handler.
2881 signal(SIGWINCH, tog_sigwinch);
2882 signal(SIGPIPE, tog_sigpipe);
2883 signal(SIGCONT, tog_sigcont);
2884 signal(SIGINT, tog_sigint);
2885 signal(SIGTERM, tog_sigterm);
2889 halfdelay(1); /* Do fast refresh while initial view is loading. */
2892 intrflush(stdscr, FALSE);
2893 keypad(stdscr, TRUE);
2895 if (getenv("TOG_COLORS") != NULL) {
2897 use_default_colors();
2901 static const struct got_error *
2902 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2903 struct got_repository *repo, struct got_worktree *worktree)
2905 const struct got_error *err = NULL;
2908 *in_repo_path = strdup("/");
2909 if (*in_repo_path == NULL)
2910 return got_error_from_errno("strdup");
2915 const char *prefix = got_worktree_get_path_prefix(worktree);
2918 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2921 if (asprintf(in_repo_path, "%s%s%s", prefix,
2922 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2924 err = got_error_from_errno("asprintf");
2925 *in_repo_path = NULL;
2929 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2934 static const struct got_error *
2935 cmd_log(int argc, char *argv[])
2937 const struct got_error *error;
2938 struct got_repository *repo = NULL;
2939 struct got_worktree *worktree = NULL;
2940 struct got_object_id *start_id = NULL;
2941 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2942 char *start_commit = NULL, *label = NULL;
2943 struct got_reference *ref = NULL;
2944 const char *head_ref_name = NULL;
2945 int ch, log_branches = 0;
2946 struct tog_view *view;
2947 int *pack_fds = NULL;
2949 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2955 start_commit = optarg;
2958 repo_path = realpath(optarg, NULL);
2959 if (repo_path == NULL)
2960 return got_error_from_errno2("realpath",
2975 error = got_repo_pack_fds_open(&pack_fds);
2979 if (repo_path == NULL) {
2980 cwd = getcwd(NULL, 0);
2982 return got_error_from_errno("getcwd");
2983 error = got_worktree_open(&worktree, cwd);
2984 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2988 strdup(got_worktree_get_repo_path(worktree));
2990 repo_path = strdup(cwd);
2991 if (repo_path == NULL) {
2992 error = got_error_from_errno("strdup");
2997 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3001 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3008 error = apply_unveil(got_repo_get_path(repo),
3009 worktree ? got_worktree_get_root_path(worktree) : NULL);
3013 /* already loaded by tog_log_with_path()? */
3014 if (TAILQ_EMPTY(&tog_refs)) {
3015 error = tog_load_refs(repo, 0);
3020 if (start_commit == NULL) {
3021 error = got_repo_match_object_id(&start_id, &label,
3022 worktree ? got_worktree_get_head_ref_name(worktree) :
3023 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3026 head_ref_name = label;
3028 error = got_ref_open(&ref, repo, start_commit, 0);
3030 head_ref_name = got_ref_get_name(ref);
3031 else if (error->code != GOT_ERR_NOT_REF)
3033 error = got_repo_match_object_id(&start_id, NULL,
3034 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3039 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3041 error = got_error_from_errno("view_open");
3044 error = open_log_view(view, start_id, repo, head_ref_name,
3045 in_repo_path, log_branches);
3049 /* Release work tree lock. */
3050 got_worktree_close(worktree);
3053 error = view_loop(view);
3063 const struct got_error *close_err = got_repo_close(repo);
3068 got_worktree_close(worktree);
3070 const struct got_error *pack_err =
3071 got_repo_pack_fds_close(pack_fds);
3083 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3084 "[-w] object1 object2\n", getprogname());
3089 match_line(const char *line, regex_t *regex, size_t nmatch,
3090 regmatch_t *regmatch)
3092 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3096 match_color(struct tog_colors *colors, const char *line)
3098 struct tog_color *tc = NULL;
3100 STAILQ_FOREACH(tc, colors, entry) {
3101 if (match_line(line, &tc->regex, 0, NULL))
3108 static const struct got_error *
3109 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3110 WINDOW *window, int skipcol, regmatch_t *regmatch)
3112 const struct got_error *err = NULL;
3114 wchar_t *wline = NULL;
3115 int rme, rms, n, width, scrollx;
3116 int width0 = 0, width1 = 0, width2 = 0;
3117 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3121 rms = regmatch->rm_so;
3122 rme = regmatch->rm_eo;
3124 err = expand_tab(&exstr, line);
3128 /* Split the line into 3 segments, according to match offsets. */
3129 seg0 = strndup(exstr, rms);
3131 err = got_error_from_errno("strndup");
3134 seg1 = strndup(exstr + rms, rme - rms);
3136 err = got_error_from_errno("strndup");
3139 seg2 = strdup(exstr + rme);
3141 err = got_error_from_errno("strndup");
3145 /* draw up to matched token if we haven't scrolled past it */
3146 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3150 n = MAX(width0 - skipcol, 0);
3153 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3154 wlimit, col_tab_align, 1);
3157 waddwstr(window, &wline[scrollx]);
3167 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3171 wlen = wcslen(wline);
3173 width = wcwidth(wline[i]);
3175 /* should not happen, tabs are expanded */
3176 err = got_error(GOT_ERR_RANGE);
3179 if (width0 + w + width > skipcol)
3184 /* draw (visible part of) matched token (if scrolled into it) */
3185 if (width1 - w > 0) {
3186 wattron(window, A_STANDOUT);
3187 waddwstr(window, &wline[i]);
3188 wattroff(window, A_STANDOUT);
3189 wlimit -= (width1 - w);
3190 *wtotal += (width1 - w);
3194 if (wlimit > 0) { /* draw rest of line */
3196 if (skipcol > width0 + width1) {
3197 err = format_line(&wline, &width2, &scrollx, seg2,
3198 skipcol - (width0 + width1), wlimit,
3202 waddwstr(window, &wline[scrollx]);
3204 err = format_line(&wline, &width2, NULL, seg2, 0,
3205 wlimit, col_tab_align, 1);
3208 waddwstr(window, wline);
3221 static const struct got_error *
3222 draw_file(struct tog_view *view, const char *header)
3224 struct tog_diff_view_state *s = &view->state.diff;
3225 regmatch_t *regmatch = &view->regmatch;
3226 const struct got_error *err;
3229 size_t linesize = 0;
3231 struct tog_color *tc;
3234 int max_lines = view->nlines;
3235 int nlines = s->nlines;
3238 line_offset = s->line_offsets[s->first_displayed_line - 1];
3239 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3240 return got_error_from_errno("fseek");
3242 werase(view->window);
3245 if (asprintf(&line, "[%d/%d] %s",
3246 s->first_displayed_line - 1 + s->selected_line, nlines,
3248 return got_error_from_errno("asprintf");
3249 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3255 if (view_needs_focus_indication(view))
3256 wstandout(view->window);
3257 waddwstr(view->window, wline);
3260 if (view_needs_focus_indication(view))
3261 wstandend(view->window);
3262 if (width <= view->ncols - 1)
3263 waddch(view->window, '\n');
3273 while (max_lines > 0 && nprinted < max_lines) {
3274 linelen = getline(&line, &linesize, s->f);
3275 if (linelen == -1) {
3281 return got_ferror(s->f, GOT_ERR_IO);
3284 /* Set view->maxx based on full line length. */
3285 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3291 view->maxx = MAX(view->maxx, width);
3295 tc = match_color(&s->colors, line);
3297 wattr_on(view->window,
3298 COLOR_PAIR(tc->colorpair), NULL);
3299 if (s->first_displayed_line + nprinted == s->matched_line &&
3300 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3301 err = add_matched_line(&width, line, view->ncols, 0,
3302 view->window, view->x, regmatch);
3309 err = format_line(&wline, &width, &skip, line,
3310 view->x, view->ncols, 0, view->x ? 1 : 0);
3315 waddwstr(view->window, &wline[skip]);
3320 wattr_off(view->window,
3321 COLOR_PAIR(tc->colorpair), NULL);
3322 if (width <= view->ncols - 1)
3323 waddch(view->window, '\n');
3328 s->last_displayed_line = s->first_displayed_line +
3331 s->last_displayed_line = s->first_displayed_line;
3336 while (nprinted < view->nlines) {
3337 waddch(view->window, '\n');
3341 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3347 wstandout(view->window);
3348 waddwstr(view->window, wline);
3351 wstandend(view->window);
3358 get_datestr(time_t *time, char *datebuf)
3360 struct tm mytm, *tm;
3363 tm = gmtime_r(time, &mytm);
3366 s = asctime_r(tm, datebuf);
3369 p = strchr(s, '\n');
3375 static const struct got_error *
3376 get_changed_paths(struct got_pathlist_head *paths,
3377 struct got_commit_object *commit, struct got_repository *repo)
3379 const struct got_error *err = NULL;
3380 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3381 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3382 struct got_object_qid *qid;
3384 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3386 struct got_commit_object *pcommit;
3387 err = got_object_open_as_commit(&pcommit, repo,
3392 tree_id1 = got_object_id_dup(
3393 got_object_commit_get_tree_id(pcommit));
3394 if (tree_id1 == NULL) {
3395 got_object_commit_close(pcommit);
3396 return got_error_from_errno("got_object_id_dup");
3398 got_object_commit_close(pcommit);
3403 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3408 tree_id2 = got_object_commit_get_tree_id(commit);
3409 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3413 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3414 got_diff_tree_collect_changed_paths, paths, 0);
3417 got_object_tree_close(tree1);
3419 got_object_tree_close(tree2);
3424 static const struct got_error *
3425 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3429 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3431 return got_error_from_errno("reallocarray");
3433 (*line_offsets)[*nlines] = off;
3438 static const struct got_error *
3439 write_commit_info(off_t **line_offsets, size_t *nlines,
3440 struct got_object_id *commit_id, struct got_reflist_head *refs,
3441 struct got_repository *repo, FILE *outfile)
3443 const struct got_error *err = NULL;
3444 char datebuf[26], *datestr;
3445 struct got_commit_object *commit;
3446 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3447 time_t committer_time;
3448 const char *author, *committer;
3449 char *refs_str = NULL;
3450 struct got_pathlist_head changed_paths;
3451 struct got_pathlist_entry *pe;
3455 TAILQ_INIT(&changed_paths);
3458 err = build_refs_str(&refs_str, refs, commit_id, repo);
3463 err = got_object_open_as_commit(&commit, repo, commit_id);
3467 err = got_object_id_str(&id_str, commit_id);
3469 err = got_error_from_errno("got_object_id_str");
3473 err = add_line_offset(line_offsets, nlines, 0);
3477 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3478 refs_str ? refs_str : "", refs_str ? ")" : "");
3480 err = got_error_from_errno("fprintf");
3484 err = add_line_offset(line_offsets, nlines, outoff);
3488 n = fprintf(outfile, "from: %s\n",
3489 got_object_commit_get_author(commit));
3491 err = got_error_from_errno("fprintf");
3495 err = add_line_offset(line_offsets, nlines, outoff);
3499 committer_time = got_object_commit_get_committer_time(commit);
3500 datestr = get_datestr(&committer_time, datebuf);
3502 n = fprintf(outfile, "date: %s UTC\n", datestr);
3504 err = got_error_from_errno("fprintf");
3508 err = add_line_offset(line_offsets, nlines, outoff);
3512 author = got_object_commit_get_author(commit);
3513 committer = got_object_commit_get_committer(commit);
3514 if (strcmp(author, committer) != 0) {
3515 n = fprintf(outfile, "via: %s\n", committer);
3517 err = got_error_from_errno("fprintf");
3521 err = add_line_offset(line_offsets, nlines, outoff);
3525 if (got_object_commit_get_nparents(commit) > 1) {
3526 const struct got_object_id_queue *parent_ids;
3527 struct got_object_qid *qid;
3529 parent_ids = got_object_commit_get_parent_ids(commit);
3530 STAILQ_FOREACH(qid, parent_ids, entry) {
3531 err = got_object_id_str(&id_str, &qid->id);
3534 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3536 err = got_error_from_errno("fprintf");
3540 err = add_line_offset(line_offsets, nlines, outoff);
3548 err = got_object_commit_get_logmsg(&logmsg, commit);
3552 while ((line = strsep(&s, "\n")) != NULL) {
3553 n = fprintf(outfile, "%s\n", line);
3555 err = got_error_from_errno("fprintf");
3559 err = add_line_offset(line_offsets, nlines, outoff);
3564 err = get_changed_paths(&changed_paths, commit, repo);
3567 TAILQ_FOREACH(pe, &changed_paths, entry) {
3568 struct got_diff_changed_path *cp = pe->data;
3569 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3571 err = got_error_from_errno("fprintf");
3575 err = add_line_offset(line_offsets, nlines, outoff);
3578 free((char *)pe->path);
3582 fputc('\n', outfile);
3584 err = add_line_offset(line_offsets, nlines, outoff);
3586 got_pathlist_free(&changed_paths);
3590 got_object_commit_close(commit);
3592 free(*line_offsets);
3593 *line_offsets = NULL;
3599 static const struct got_error *
3600 create_diff(struct tog_diff_view_state *s)
3602 const struct got_error *err = NULL;
3606 free(s->line_offsets);
3607 s->line_offsets = malloc(sizeof(off_t));
3608 if (s->line_offsets == NULL)
3609 return got_error_from_errno("malloc");
3614 err = got_error_from_errno("got_opentemp");
3617 if (s->f && fclose(s->f) == EOF) {
3618 err = got_error_from_errno("fclose");
3624 err = got_object_get_type(&obj_type, s->repo, s->id1);
3626 err = got_object_get_type(&obj_type, s->repo, s->id2);
3631 case GOT_OBJ_TYPE_BLOB:
3632 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3633 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3634 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3637 case GOT_OBJ_TYPE_TREE:
3638 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3639 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3640 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3642 case GOT_OBJ_TYPE_COMMIT: {
3643 const struct got_object_id_queue *parent_ids;
3644 struct got_object_qid *pid;
3645 struct got_commit_object *commit2;
3646 struct got_reflist_head *refs;
3648 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3651 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3652 /* Show commit info if we're diffing to a parent/root commit. */
3653 if (s->id1 == NULL) {
3654 err = write_commit_info(&s->line_offsets, &s->nlines,
3655 s->id2, refs, s->repo, s->f);
3659 parent_ids = got_object_commit_get_parent_ids(commit2);
3660 STAILQ_FOREACH(pid, parent_ids, entry) {
3661 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3662 err = write_commit_info(
3663 &s->line_offsets, &s->nlines,
3664 s->id2, refs, s->repo, s->f);
3671 got_object_commit_close(commit2);
3673 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3674 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3675 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3679 err = got_error(GOT_ERR_OBJ_TYPE);
3685 if (s->f && fflush(s->f) != 0 && err == NULL)
3686 err = got_error_from_errno("fflush");
3691 diff_view_indicate_progress(struct tog_view *view)
3693 mvwaddstr(view->window, 0, 0, "diffing...");
3698 static const struct got_error *
3699 search_start_diff_view(struct tog_view *view)
3701 struct tog_diff_view_state *s = &view->state.diff;
3703 s->matched_line = 0;
3707 static const struct got_error *
3708 search_next_diff_view(struct tog_view *view)
3710 struct tog_diff_view_state *s = &view->state.diff;
3711 const struct got_error *err = NULL;
3714 size_t linesize = 0;
3717 if (!view->searching) {
3718 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3722 if (s->matched_line) {
3723 if (view->searching == TOG_SEARCH_FORWARD)
3724 lineno = s->matched_line + 1;
3726 lineno = s->matched_line - 1;
3728 lineno = s->first_displayed_line;
3733 if (lineno <= 0 || lineno > s->nlines) {
3734 if (s->matched_line == 0) {
3735 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3739 if (view->searching == TOG_SEARCH_FORWARD)
3745 offset = s->line_offsets[lineno - 1];
3746 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3748 return got_error_from_errno("fseeko");
3750 linelen = getline(&line, &linesize, s->f);
3751 if (linelen != -1) {
3753 err = expand_tab(&exstr, line);
3756 if (match_line(exstr, &view->regex, 1,
3758 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3759 s->matched_line = lineno;
3765 if (view->searching == TOG_SEARCH_FORWARD)
3772 if (s->matched_line) {
3773 s->first_displayed_line = s->matched_line;
3774 s->selected_line = 1;
3780 static const struct got_error *
3781 close_diff_view(struct tog_view *view)
3783 const struct got_error *err = NULL;
3784 struct tog_diff_view_state *s = &view->state.diff;
3790 if (s->f && fclose(s->f) == EOF)
3791 err = got_error_from_errno("fclose");
3793 if (s->f1 && fclose(s->f1) == EOF)
3794 err = got_error_from_errno("fclose");
3796 if (s->f2 && fclose(s->f2) == EOF)
3797 err = got_error_from_errno("fclose");
3799 free_colors(&s->colors);
3800 free(s->line_offsets);
3801 s->line_offsets = NULL;
3806 static const struct got_error *
3807 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3808 struct got_object_id *id2, const char *label1, const char *label2,
3809 int diff_context, int ignore_whitespace, int force_text_diff,
3810 struct tog_view *log_view, struct got_repository *repo)
3812 const struct got_error *err;
3813 struct tog_diff_view_state *s = &view->state.diff;
3815 memset(s, 0, sizeof(*s));
3817 if (id1 != NULL && id2 != NULL) {
3819 err = got_object_get_type(&type1, repo, id1);
3822 err = got_object_get_type(&type2, repo, id2);
3827 return got_error(GOT_ERR_OBJ_TYPE);
3829 s->first_displayed_line = 1;
3830 s->last_displayed_line = view->nlines;
3831 s->selected_line = 1;
3839 s->id1 = got_object_id_dup(id1);
3841 return got_error_from_errno("got_object_id_dup");
3842 s->f1 = got_opentemp();
3843 if (s->f1 == NULL) {
3844 err = got_error_from_errno("got_opentemp");
3850 s->id2 = got_object_id_dup(id2);
3851 if (s->id2 == NULL) {
3852 err = got_error_from_errno("got_object_id_dup");
3856 s->f2 = got_opentemp();
3857 if (s->f2 == NULL) {
3858 err = got_error_from_errno("got_opentemp");
3862 s->first_displayed_line = 1;
3863 s->last_displayed_line = view->nlines;
3864 s->diff_context = diff_context;
3865 s->ignore_whitespace = ignore_whitespace;
3866 s->force_text_diff = force_text_diff;
3867 s->log_view = log_view;
3870 STAILQ_INIT(&s->colors);
3871 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3872 err = add_color(&s->colors,
3873 "^-", TOG_COLOR_DIFF_MINUS,
3874 get_color_value("TOG_COLOR_DIFF_MINUS"));
3877 err = add_color(&s->colors, "^\\+",
3878 TOG_COLOR_DIFF_PLUS,
3879 get_color_value("TOG_COLOR_DIFF_PLUS"));
3882 err = add_color(&s->colors,
3883 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3884 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3888 err = add_color(&s->colors,
3889 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3890 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3891 get_color_value("TOG_COLOR_DIFF_META"));
3895 err = add_color(&s->colors,
3896 "^(from|via): ", TOG_COLOR_AUTHOR,
3897 get_color_value("TOG_COLOR_AUTHOR"));
3901 err = add_color(&s->colors,
3902 "^date: ", TOG_COLOR_DATE,
3903 get_color_value("TOG_COLOR_DATE"));
3908 if (log_view && view_is_splitscreen(view))
3909 show_log_view(log_view); /* draw vborder */
3910 diff_view_indicate_progress(view);
3912 err = create_diff(s);
3914 view->show = show_diff_view;
3915 view->input = input_diff_view;
3916 view->close = close_diff_view;
3917 view->search_start = search_start_diff_view;
3918 view->search_next = search_next_diff_view;
3921 close_diff_view(view);
3925 static const struct got_error *
3926 show_diff_view(struct tog_view *view)
3928 const struct got_error *err;
3929 struct tog_diff_view_state *s = &view->state.diff;
3930 char *id_str1 = NULL, *id_str2, *header;
3931 const char *label1, *label2;
3934 err = got_object_id_str(&id_str1, s->id1);
3937 label1 = s->label1 ? : id_str1;
3939 label1 = "/dev/null";
3941 err = got_object_id_str(&id_str2, s->id2);
3944 label2 = s->label2 ? : id_str2;
3946 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3947 err = got_error_from_errno("asprintf");
3955 err = draw_file(view, header);
3960 static const struct got_error *
3961 set_selected_commit(struct tog_diff_view_state *s,
3962 struct commit_queue_entry *entry)
3964 const struct got_error *err;
3965 const struct got_object_id_queue *parent_ids;
3966 struct got_commit_object *selected_commit;
3967 struct got_object_qid *pid;
3970 s->id2 = got_object_id_dup(entry->id);
3972 return got_error_from_errno("got_object_id_dup");
3974 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3977 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3979 pid = STAILQ_FIRST(parent_ids);
3980 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
3981 got_object_commit_close(selected_commit);
3985 static const struct got_error *
3986 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
3988 const struct got_error *err = NULL;
3989 struct tog_diff_view_state *s = &view->state.diff;
3990 struct tog_log_view_state *ls;
3991 struct commit_queue_entry *old_selected_entry;
3993 size_t linesize = 0;
3995 int i, nscroll = view->nlines - 1;
4002 view->x = MAX(view->maxx - view->ncols / 3, 0);
4006 if (view->x + view->ncols / 3 < view->maxx)
4007 view->x += 2; /* move two columns right */
4011 view->x -= MIN(view->x, 2); /* move two columns back */
4016 s->force_text_diff = !s->force_text_diff;
4018 s->ignore_whitespace = !s->ignore_whitespace;
4019 wclear(view->window);
4020 s->first_displayed_line = 1;
4021 s->last_displayed_line = view->nlines;
4022 s->matched_line = 0;
4023 diff_view_indicate_progress(view);
4024 err = create_diff(s);
4028 s->first_displayed_line = 1;
4035 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4041 if (s->first_displayed_line > 1)
4042 s->first_displayed_line--;
4050 if (s->first_displayed_line == 1)
4053 while (i++ < nscroll && s->first_displayed_line > 1)
4054 s->first_displayed_line--;
4060 s->first_displayed_line++;
4072 while (!s->eof && i++ < nscroll) {
4073 linelen = getline(&line, &linesize, s->f);
4074 s->first_displayed_line++;
4075 if (linelen == -1) {
4079 err = got_ferror(s->f, GOT_ERR_IO);
4086 if (s->diff_context > 0) {
4088 s->matched_line = 0;
4089 diff_view_indicate_progress(view);
4090 err = create_diff(s);
4091 if (s->first_displayed_line + view->nlines - 1 >
4093 s->first_displayed_line = 1;
4094 s->last_displayed_line = view->nlines;
4099 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4101 s->matched_line = 0;
4102 diff_view_indicate_progress(view);
4103 err = create_diff(s);
4108 if (s->log_view == NULL)
4110 ls = &s->log_view->state.log;
4111 old_selected_entry = ls->selected_entry;
4113 err = input_log_view(NULL, s->log_view, KEY_UP);
4117 if (old_selected_entry == ls->selected_entry)
4120 err = set_selected_commit(s, ls->selected_entry);
4124 s->first_displayed_line = 1;
4125 s->last_displayed_line = view->nlines;
4126 s->matched_line = 0;
4129 diff_view_indicate_progress(view);
4130 err = create_diff(s);
4134 if (s->log_view == NULL)
4136 ls = &s->log_view->state.log;
4137 old_selected_entry = ls->selected_entry;
4139 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4143 if (old_selected_entry == ls->selected_entry)
4146 err = set_selected_commit(s, ls->selected_entry);
4150 s->first_displayed_line = 1;
4151 s->last_displayed_line = view->nlines;
4152 s->matched_line = 0;
4155 diff_view_indicate_progress(view);
4156 err = create_diff(s);
4165 static const struct got_error *
4166 cmd_diff(int argc, char *argv[])
4168 const struct got_error *error = NULL;
4169 struct got_repository *repo = NULL;
4170 struct got_worktree *worktree = NULL;
4171 struct got_object_id *id1 = NULL, *id2 = NULL;
4172 char *repo_path = NULL, *cwd = NULL;
4173 char *id_str1 = NULL, *id_str2 = NULL;
4174 char *label1 = NULL, *label2 = NULL;
4175 int diff_context = 3, ignore_whitespace = 0;
4176 int ch, force_text_diff = 0;
4178 struct tog_view *view;
4179 int *pack_fds = NULL;
4181 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4184 force_text_diff = 1;
4187 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4190 errx(1, "number of context lines is %s: %s",
4194 repo_path = realpath(optarg, NULL);
4195 if (repo_path == NULL)
4196 return got_error_from_errno2("realpath",
4198 got_path_strip_trailing_slashes(repo_path);
4201 ignore_whitespace = 1;
4213 usage_diff(); /* TODO show local worktree changes */
4214 } else if (argc == 2) {
4220 error = got_repo_pack_fds_open(&pack_fds);
4224 if (repo_path == NULL) {
4225 cwd = getcwd(NULL, 0);
4227 return got_error_from_errno("getcwd");
4228 error = got_worktree_open(&worktree, cwd);
4229 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4233 strdup(got_worktree_get_repo_path(worktree));
4235 repo_path = strdup(cwd);
4236 if (repo_path == NULL) {
4237 error = got_error_from_errno("strdup");
4242 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4248 error = apply_unveil(got_repo_get_path(repo), NULL);
4252 error = tog_load_refs(repo, 0);
4256 error = got_repo_match_object_id(&id1, &label1, id_str1,
4257 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4261 error = got_repo_match_object_id(&id2, &label2, id_str2,
4262 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4266 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4268 error = got_error_from_errno("view_open");
4271 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4272 ignore_whitespace, force_text_diff, NULL, repo);
4275 error = view_loop(view);
4282 const struct got_error *close_err = got_repo_close(repo);
4287 got_worktree_close(worktree);
4289 const struct got_error *pack_err =
4290 got_repo_pack_fds_close(pack_fds);
4303 "usage: %s blame [-c commit] [-r repository-path] path\n",
4308 struct tog_blame_line {
4310 struct got_object_id *id;
4313 static const struct got_error *
4314 draw_blame(struct tog_view *view)
4316 struct tog_blame_view_state *s = &view->state.blame;
4317 struct tog_blame *blame = &s->blame;
4318 regmatch_t *regmatch = &view->regmatch;
4319 const struct got_error *err;
4320 int lineno = 0, nprinted = 0;
4322 size_t linesize = 0;
4326 struct tog_blame_line *blame_line;
4327 struct got_object_id *prev_id = NULL;
4329 struct tog_color *tc;
4331 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4336 werase(view->window);
4338 if (asprintf(&line, "commit %s", id_str) == -1) {
4339 err = got_error_from_errno("asprintf");
4344 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4349 if (view_needs_focus_indication(view))
4350 wstandout(view->window);
4351 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4353 wattr_on(view->window,
4354 COLOR_PAIR(tc->colorpair), NULL);
4355 waddwstr(view->window, wline);
4357 wattr_off(view->window,
4358 COLOR_PAIR(tc->colorpair), NULL);
4359 if (view_needs_focus_indication(view))
4360 wstandend(view->window);
4363 if (width < view->ncols - 1)
4364 waddch(view->window, '\n');
4366 if (asprintf(&line, "[%d/%d] %s%s",
4367 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4368 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4370 return got_error_from_errno("asprintf");
4373 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4378 waddwstr(view->window, wline);
4381 if (width < view->ncols - 1)
4382 waddch(view->window, '\n');
4386 while (nprinted < view->nlines - 2) {
4387 linelen = getline(&line, &linesize, blame->f);
4388 if (linelen == -1) {
4389 if (feof(blame->f)) {
4394 return got_ferror(blame->f, GOT_ERR_IO);
4396 if (++lineno < s->first_displayed_line)
4399 /* Set view->maxx based on full line length. */
4400 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4407 view->maxx = MAX(view->maxx, width);
4409 if (view->focussed && nprinted == s->selected_line - 1)
4410 wstandout(view->window);
4412 if (blame->nlines > 0) {
4413 blame_line = &blame->lines[lineno - 1];
4414 if (blame_line->annotated && prev_id &&
4415 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4417 nprinted == s->selected_line - 1)) {
4418 waddstr(view->window, " ");
4419 } else if (blame_line->annotated) {
4421 err = got_object_id_str(&id_str,
4427 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4429 wattr_on(view->window,
4430 COLOR_PAIR(tc->colorpair), NULL);
4431 wprintw(view->window, "%.8s", id_str);
4433 wattr_off(view->window,
4434 COLOR_PAIR(tc->colorpair), NULL);
4436 prev_id = blame_line->id;
4438 waddstr(view->window, "........");
4442 waddstr(view->window, "........");
4446 if (view->focussed && nprinted == s->selected_line - 1)
4447 wstandend(view->window);
4448 waddstr(view->window, " ");
4450 if (view->ncols <= 9) {
4452 } else if (s->first_displayed_line + nprinted ==
4454 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4455 err = add_matched_line(&width, line, view->ncols - 9, 9,
4456 view->window, view->x, regmatch);
4464 err = format_line(&wline, &width, &skip, line,
4465 view->x, view->ncols - 9, 9, 1);
4470 waddwstr(view->window, &wline[skip]);
4476 if (width <= view->ncols - 1)
4477 waddch(view->window, '\n');
4478 if (++nprinted == 1)
4479 s->first_displayed_line = lineno;
4482 s->last_displayed_line = lineno;
4489 static const struct got_error *
4490 blame_cb(void *arg, int nlines, int lineno,
4491 struct got_commit_object *commit, struct got_object_id *id)
4493 const struct got_error *err = NULL;
4494 struct tog_blame_cb_args *a = arg;
4495 struct tog_blame_line *line;
4498 if (nlines != a->nlines ||
4499 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4500 return got_error(GOT_ERR_RANGE);
4502 errcode = pthread_mutex_lock(&tog_mutex);
4504 return got_error_set_errno(errcode, "pthread_mutex_lock");
4506 if (*a->quit) { /* user has quit the blame view */
4507 err = got_error(GOT_ERR_ITER_COMPLETED);
4512 goto done; /* no change in this commit */
4514 line = &a->lines[lineno - 1];
4515 if (line->annotated)
4518 line->id = got_object_id_dup(id);
4519 if (line->id == NULL) {
4520 err = got_error_from_errno("got_object_id_dup");
4523 line->annotated = 1;
4525 errcode = pthread_mutex_unlock(&tog_mutex);
4527 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4532 blame_thread(void *arg)
4534 const struct got_error *err, *close_err;
4535 struct tog_blame_thread_args *ta = arg;
4536 struct tog_blame_cb_args *a = ta->cb_args;
4539 err = block_signals_used_by_main_thread();
4543 err = got_blame(ta->path, a->commit_id, ta->repo,
4544 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4545 if (err && err->code == GOT_ERR_CANCELLED)
4548 errcode = pthread_mutex_lock(&tog_mutex);
4550 return (void *)got_error_set_errno(errcode,
4551 "pthread_mutex_lock");
4553 close_err = got_repo_close(ta->repo);
4559 errcode = pthread_mutex_unlock(&tog_mutex);
4560 if (errcode && err == NULL)
4561 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4566 static struct got_object_id *
4567 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4568 int first_displayed_line, int selected_line)
4570 struct tog_blame_line *line;
4575 line = &lines[first_displayed_line - 1 + selected_line - 1];
4576 if (!line->annotated)
4582 static const struct got_error *
4583 stop_blame(struct tog_blame *blame)
4585 const struct got_error *err = NULL;
4588 if (blame->thread) {
4590 errcode = pthread_mutex_unlock(&tog_mutex);
4592 return got_error_set_errno(errcode,
4593 "pthread_mutex_unlock");
4594 errcode = pthread_join(blame->thread, (void **)&err);
4596 return got_error_set_errno(errcode, "pthread_join");
4597 errcode = pthread_mutex_lock(&tog_mutex);
4599 return got_error_set_errno(errcode,
4600 "pthread_mutex_lock");
4601 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4603 blame->thread = 0; //NULL;
4605 if (blame->thread_args.repo) {
4606 const struct got_error *close_err;
4607 close_err = got_repo_close(blame->thread_args.repo);
4610 blame->thread_args.repo = NULL;
4613 if (fclose(blame->f) == EOF && err == NULL)
4614 err = got_error_from_errno("fclose");
4618 for (i = 0; i < blame->nlines; i++)
4619 free(blame->lines[i].id);
4621 blame->lines = NULL;
4623 free(blame->cb_args.commit_id);
4624 blame->cb_args.commit_id = NULL;
4625 if (blame->pack_fds) {
4626 const struct got_error *pack_err =
4627 got_repo_pack_fds_close(blame->pack_fds);
4630 blame->pack_fds = NULL;
4635 static const struct got_error *
4636 cancel_blame_view(void *arg)
4638 const struct got_error *err = NULL;
4642 errcode = pthread_mutex_lock(&tog_mutex);
4644 return got_error_set_errno(errcode,
4645 "pthread_mutex_unlock");
4648 err = got_error(GOT_ERR_CANCELLED);
4650 errcode = pthread_mutex_unlock(&tog_mutex);
4652 return got_error_set_errno(errcode,
4653 "pthread_mutex_lock");
4658 static const struct got_error *
4659 run_blame(struct tog_view *view)
4661 struct tog_blame_view_state *s = &view->state.blame;
4662 struct tog_blame *blame = &s->blame;
4663 const struct got_error *err = NULL;
4664 struct got_commit_object *commit = NULL;
4665 struct got_blob_object *blob = NULL;
4666 struct got_repository *thread_repo = NULL;
4667 struct got_object_id *obj_id = NULL;
4669 int *pack_fds = NULL;
4671 err = got_object_open_as_commit(&commit, s->repo,
4672 &s->blamed_commit->id);
4676 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4680 err = got_object_get_type(&obj_type, s->repo, obj_id);
4684 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4685 err = got_error(GOT_ERR_OBJ_TYPE);
4689 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4692 blame->f = got_opentemp();
4693 if (blame->f == NULL) {
4694 err = got_error_from_errno("got_opentemp");
4697 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4698 &blame->line_offsets, blame->f, blob);
4701 if (blame->nlines == 0) {
4702 s->blame_complete = 1;
4706 /* Don't include \n at EOF in the blame line count. */
4707 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4710 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4711 if (blame->lines == NULL) {
4712 err = got_error_from_errno("calloc");
4716 err = got_repo_pack_fds_open(&pack_fds);
4719 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4724 blame->pack_fds = pack_fds;
4725 blame->cb_args.view = view;
4726 blame->cb_args.lines = blame->lines;
4727 blame->cb_args.nlines = blame->nlines;
4728 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4729 if (blame->cb_args.commit_id == NULL) {
4730 err = got_error_from_errno("got_object_id_dup");
4733 blame->cb_args.quit = &s->done;
4735 blame->thread_args.path = s->path;
4736 blame->thread_args.repo = thread_repo;
4737 blame->thread_args.cb_args = &blame->cb_args;
4738 blame->thread_args.complete = &s->blame_complete;
4739 blame->thread_args.cancel_cb = cancel_blame_view;
4740 blame->thread_args.cancel_arg = &s->done;
4741 s->blame_complete = 0;
4743 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4744 s->first_displayed_line = 1;
4745 s->last_displayed_line = view->nlines;
4746 s->selected_line = 1;
4748 s->matched_line = 0;
4752 got_object_commit_close(commit);
4754 got_object_blob_close(blob);
4761 static const struct got_error *
4762 open_blame_view(struct tog_view *view, char *path,
4763 struct got_object_id *commit_id, struct got_repository *repo)
4765 const struct got_error *err = NULL;
4766 struct tog_blame_view_state *s = &view->state.blame;
4768 STAILQ_INIT(&s->blamed_commits);
4770 s->path = strdup(path);
4771 if (s->path == NULL)
4772 return got_error_from_errno("strdup");
4774 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4780 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4781 s->first_displayed_line = 1;
4782 s->last_displayed_line = view->nlines;
4783 s->selected_line = 1;
4784 s->blame_complete = 0;
4786 s->commit_id = commit_id;
4787 memset(&s->blame, 0, sizeof(s->blame));
4789 STAILQ_INIT(&s->colors);
4790 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4791 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4792 get_color_value("TOG_COLOR_COMMIT"));
4797 view->show = show_blame_view;
4798 view->input = input_blame_view;
4799 view->close = close_blame_view;
4800 view->search_start = search_start_blame_view;
4801 view->search_next = search_next_blame_view;
4803 return run_blame(view);
4806 static const struct got_error *
4807 close_blame_view(struct tog_view *view)
4809 const struct got_error *err = NULL;
4810 struct tog_blame_view_state *s = &view->state.blame;
4812 if (s->blame.thread)
4813 err = stop_blame(&s->blame);
4815 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4816 struct got_object_qid *blamed_commit;
4817 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4818 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4819 got_object_qid_free(blamed_commit);
4823 free_colors(&s->colors);
4827 static const struct got_error *
4828 search_start_blame_view(struct tog_view *view)
4830 struct tog_blame_view_state *s = &view->state.blame;
4832 s->matched_line = 0;
4836 static const struct got_error *
4837 search_next_blame_view(struct tog_view *view)
4839 struct tog_blame_view_state *s = &view->state.blame;
4840 const struct got_error *err = NULL;
4843 size_t linesize = 0;
4846 if (!view->searching) {
4847 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4851 if (s->matched_line) {
4852 if (view->searching == TOG_SEARCH_FORWARD)
4853 lineno = s->matched_line + 1;
4855 lineno = s->matched_line - 1;
4857 lineno = s->first_displayed_line - 1 + s->selected_line;
4862 if (lineno <= 0 || lineno > s->blame.nlines) {
4863 if (s->matched_line == 0) {
4864 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4868 if (view->searching == TOG_SEARCH_FORWARD)
4871 lineno = s->blame.nlines;
4874 offset = s->blame.line_offsets[lineno - 1];
4875 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4877 return got_error_from_errno("fseeko");
4879 linelen = getline(&line, &linesize, s->blame.f);
4880 if (linelen != -1) {
4882 err = expand_tab(&exstr, line);
4885 if (match_line(exstr, &view->regex, 1,
4887 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4888 s->matched_line = lineno;
4894 if (view->searching == TOG_SEARCH_FORWARD)
4901 if (s->matched_line) {
4902 s->first_displayed_line = s->matched_line;
4903 s->selected_line = 1;
4909 static const struct got_error *
4910 show_blame_view(struct tog_view *view)
4912 const struct got_error *err = NULL;
4913 struct tog_blame_view_state *s = &view->state.blame;
4916 if (s->blame.thread == 0 && !s->blame_complete) {
4917 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4918 &s->blame.thread_args);
4920 return got_error_set_errno(errcode, "pthread_create");
4922 halfdelay(1); /* fast refresh while annotating */
4925 if (s->blame_complete)
4926 halfdelay(10); /* disable fast refresh */
4928 err = draw_blame(view);
4934 static const struct got_error *
4935 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4937 const struct got_error *err = NULL, *thread_err = NULL;
4938 struct tog_view *diff_view;
4939 struct tog_blame_view_state *s = &view->state.blame;
4940 int begin_x = 0, nscroll = view->nlines - 2;
4947 view->x = MAX(view->maxx - view->ncols / 3, 0);
4951 if (view->x + view->ncols / 3 < view->maxx)
4952 view->x += 2; /* move two columns right */
4956 view->x -= MIN(view->x, 2); /* move two columns back */
4963 s->selected_line = 1;
4964 s->first_displayed_line = 1;
4968 if (s->blame.nlines < view->nlines - 2) {
4969 s->selected_line = s->blame.nlines;
4970 s->first_displayed_line = 1;
4972 s->selected_line = view->nlines - 2;
4973 s->first_displayed_line = s->blame.nlines -
4980 if (s->selected_line > 1)
4982 else if (s->selected_line == 1 &&
4983 s->first_displayed_line > 1)
4984 s->first_displayed_line--;
4992 if (s->first_displayed_line == 1) {
4993 s->selected_line = MAX(1, s->selected_line - nscroll);
4996 if (s->first_displayed_line > nscroll)
4997 s->first_displayed_line -= nscroll;
4999 s->first_displayed_line = 1;
5004 if (s->selected_line < view->nlines - 2 &&
5005 s->first_displayed_line +
5006 s->selected_line <= s->blame.nlines)
5008 else if (s->last_displayed_line <
5010 s->first_displayed_line++;
5014 struct got_object_id *id = NULL;
5015 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5016 s->first_displayed_line, s->selected_line);
5020 struct got_commit_object *commit, *pcommit;
5021 struct got_object_qid *pid;
5022 struct got_object_id *blob_id = NULL;
5024 err = got_object_open_as_commit(&commit,
5029 got_object_commit_get_parent_ids(commit));
5031 got_object_commit_close(commit);
5034 /* Check if path history ends here. */
5035 err = got_object_open_as_commit(&pcommit,
5039 err = got_object_id_by_path(&blob_id, s->repo,
5041 got_object_commit_close(pcommit);
5043 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5045 got_object_commit_close(commit);
5048 err = got_object_get_type(&obj_type, s->repo,
5051 /* Can't blame non-blob type objects. */
5052 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5053 got_object_commit_close(commit);
5056 err = got_object_qid_alloc(&s->blamed_commit,
5058 got_object_commit_close(commit);
5060 if (got_object_id_cmp(id,
5061 &s->blamed_commit->id) == 0)
5063 err = got_object_qid_alloc(&s->blamed_commit,
5069 thread_err = stop_blame(&s->blame);
5073 STAILQ_INSERT_HEAD(&s->blamed_commits,
5074 s->blamed_commit, entry);
5075 err = run_blame(view);
5081 struct got_object_qid *first;
5082 first = STAILQ_FIRST(&s->blamed_commits);
5083 if (!got_object_id_cmp(&first->id, s->commit_id))
5086 thread_err = stop_blame(&s->blame);
5090 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5091 got_object_qid_free(s->blamed_commit);
5093 STAILQ_FIRST(&s->blamed_commits);
5094 err = run_blame(view);
5101 struct got_object_id *id = NULL;
5102 struct got_object_qid *pid;
5103 struct got_commit_object *commit = NULL;
5104 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5105 s->first_displayed_line, s->selected_line);
5108 err = got_object_open_as_commit(&commit, s->repo, id);
5112 got_object_commit_get_parent_ids(commit));
5113 if (view_is_parent_view(view))
5114 begin_x = view_split_begin_x(view->begin_x);
5115 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5116 if (diff_view == NULL) {
5117 got_object_commit_close(commit);
5118 err = got_error_from_errno("view_open");
5121 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5122 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5123 got_object_commit_close(commit);
5125 view_close(diff_view);
5129 diff_view->focussed = 1;
5130 if (view_is_parent_view(view)) {
5131 err = view_close_child(view);
5134 err = view_set_child(view, diff_view);
5137 view->focus_child = 1;
5139 *new_view = diff_view;
5151 if (s->last_displayed_line >= s->blame.nlines &&
5152 s->selected_line >= MIN(s->blame.nlines,
5153 view->nlines - 2)) {
5156 if (s->last_displayed_line >= s->blame.nlines &&
5157 s->selected_line < view->nlines - 2) {
5159 MIN(nscroll, s->last_displayed_line -
5160 s->first_displayed_line - s->selected_line + 1);
5162 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5163 s->first_displayed_line += nscroll;
5165 s->first_displayed_line =
5166 s->blame.nlines - (view->nlines - 3);
5169 if (s->selected_line > view->nlines - 2) {
5170 s->selected_line = MIN(s->blame.nlines,
5177 return thread_err ? thread_err : err;
5180 static const struct got_error *
5181 cmd_blame(int argc, char *argv[])
5183 const struct got_error *error;
5184 struct got_repository *repo = NULL;
5185 struct got_worktree *worktree = NULL;
5186 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5187 char *link_target = NULL;
5188 struct got_object_id *commit_id = NULL;
5189 struct got_commit_object *commit = NULL;
5190 char *commit_id_str = NULL;
5192 struct tog_view *view;
5193 int *pack_fds = NULL;
5195 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5198 commit_id_str = optarg;
5201 repo_path = realpath(optarg, NULL);
5202 if (repo_path == NULL)
5203 return got_error_from_errno2("realpath",
5218 error = got_repo_pack_fds_open(&pack_fds);
5222 if (repo_path == NULL) {
5223 cwd = getcwd(NULL, 0);
5225 return got_error_from_errno("getcwd");
5226 error = got_worktree_open(&worktree, cwd);
5227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5231 strdup(got_worktree_get_repo_path(worktree));
5233 repo_path = strdup(cwd);
5234 if (repo_path == NULL) {
5235 error = got_error_from_errno("strdup");
5240 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5244 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5251 error = apply_unveil(got_repo_get_path(repo), NULL);
5255 error = tog_load_refs(repo, 0);
5259 if (commit_id_str == NULL) {
5260 struct got_reference *head_ref;
5261 error = got_ref_open(&head_ref, repo, worktree ?
5262 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5265 error = got_ref_resolve(&commit_id, repo, head_ref);
5266 got_ref_close(head_ref);
5268 error = got_repo_match_object_id(&commit_id, NULL,
5269 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5274 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5276 error = got_error_from_errno("view_open");
5280 error = got_object_open_as_commit(&commit, repo, commit_id);
5284 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5289 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5294 /* Release work tree lock. */
5295 got_worktree_close(worktree);
5298 error = view_loop(view);
5306 got_object_commit_close(commit);
5308 got_worktree_close(worktree);
5310 const struct got_error *close_err = got_repo_close(repo);
5315 const struct got_error *pack_err =
5316 got_repo_pack_fds_close(pack_fds);
5324 static const struct got_error *
5325 draw_tree_entries(struct tog_view *view, const char *parent_path)
5327 struct tog_tree_view_state *s = &view->state.tree;
5328 const struct got_error *err = NULL;
5329 struct got_tree_entry *te;
5331 struct tog_color *tc;
5332 int width, n, i, nentries;
5333 int limit = view->nlines;
5337 werase(view->window);
5342 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5346 if (view_needs_focus_indication(view))
5347 wstandout(view->window);
5348 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5350 wattr_on(view->window,
5351 COLOR_PAIR(tc->colorpair), NULL);
5352 waddwstr(view->window, wline);
5354 wattr_off(view->window,
5355 COLOR_PAIR(tc->colorpair), NULL);
5356 if (view_needs_focus_indication(view))
5357 wstandend(view->window);
5360 if (width < view->ncols - 1)
5361 waddch(view->window, '\n');
5364 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5368 waddwstr(view->window, wline);
5371 if (width < view->ncols - 1)
5372 waddch(view->window, '\n');
5375 waddch(view->window, '\n');
5379 if (s->first_displayed_entry == NULL) {
5380 te = got_object_tree_get_first_entry(s->tree);
5381 if (s->selected == 0) {
5383 wstandout(view->window);
5384 s->selected_entry = NULL;
5386 waddstr(view->window, " ..\n"); /* parent directory */
5387 if (s->selected == 0 && view->focussed)
5388 wstandend(view->window);
5395 te = s->first_displayed_entry;
5398 nentries = got_object_tree_get_nentries(s->tree);
5399 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5400 char *line = NULL, *id_str = NULL, *link_target = NULL;
5401 const char *modestr = "";
5404 te = got_object_tree_get_entry(s->tree, i);
5405 mode = got_tree_entry_get_mode(te);
5408 err = got_object_id_str(&id_str,
5409 got_tree_entry_get_id(te));
5411 return got_error_from_errno(
5412 "got_object_id_str");
5414 if (got_object_tree_entry_is_submodule(te))
5416 else if (S_ISLNK(mode)) {
5419 err = got_tree_entry_get_symlink_target(&link_target,
5425 for (i = 0; i < strlen(link_target); i++) {
5426 if (!isprint((unsigned char)link_target[i]))
5427 link_target[i] = '?';
5431 else if (S_ISDIR(mode))
5433 else if (mode & S_IXUSR)
5435 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5436 got_tree_entry_get_name(te), modestr,
5437 link_target ? " -> ": "",
5438 link_target ? link_target : "") == -1) {
5441 return got_error_from_errno("asprintf");
5445 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5451 if (n == s->selected) {
5453 wstandout(view->window);
5454 s->selected_entry = te;
5456 tc = match_color(&s->colors, line);
5458 wattr_on(view->window,
5459 COLOR_PAIR(tc->colorpair), NULL);
5460 waddwstr(view->window, wline);
5462 wattr_off(view->window,
5463 COLOR_PAIR(tc->colorpair), NULL);
5464 if (width < view->ncols - 1)
5465 waddch(view->window, '\n');
5466 if (n == s->selected && view->focussed)
5467 wstandend(view->window);
5473 s->last_displayed_entry = te;
5482 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5484 struct got_tree_entry *te;
5485 int isroot = s->tree == s->root;
5488 if (s->first_displayed_entry == NULL)
5491 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5492 while (i++ < maxscroll) {
5495 s->first_displayed_entry = NULL;
5498 s->first_displayed_entry = te;
5499 te = got_tree_entry_get_prev(s->tree, te);
5504 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5506 struct got_tree_entry *next, *last;
5509 if (s->first_displayed_entry)
5510 next = got_tree_entry_get_next(s->tree,
5511 s->first_displayed_entry);
5513 next = got_object_tree_get_first_entry(s->tree);
5515 last = s->last_displayed_entry;
5516 while (next && last && n++ < maxscroll) {
5517 last = got_tree_entry_get_next(s->tree, last);
5519 s->first_displayed_entry = next;
5520 next = got_tree_entry_get_next(s->tree, next);
5525 static const struct got_error *
5526 tree_entry_path(char **path, struct tog_parent_trees *parents,
5527 struct got_tree_entry *te)
5529 const struct got_error *err = NULL;
5530 struct tog_parent_tree *pt;
5531 size_t len = 2; /* for leading slash and NUL */
5533 TAILQ_FOREACH(pt, parents, entry)
5534 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5537 len += strlen(got_tree_entry_get_name(te));
5539 *path = calloc(1, len);
5541 return got_error_from_errno("calloc");
5544 pt = TAILQ_LAST(parents, tog_parent_trees);
5546 const char *name = got_tree_entry_get_name(pt->selected_entry);
5547 if (strlcat(*path, name, len) >= len) {
5548 err = got_error(GOT_ERR_NO_SPACE);
5551 if (strlcat(*path, "/", len) >= len) {
5552 err = got_error(GOT_ERR_NO_SPACE);
5555 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5558 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5559 err = got_error(GOT_ERR_NO_SPACE);
5571 static const struct got_error *
5572 blame_tree_entry(struct tog_view **new_view, int begin_x,
5573 struct got_tree_entry *te, struct tog_parent_trees *parents,
5574 struct got_object_id *commit_id, struct got_repository *repo)
5576 const struct got_error *err = NULL;
5578 struct tog_view *blame_view;
5582 err = tree_entry_path(&path, parents, te);
5586 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5587 if (blame_view == NULL) {
5588 err = got_error_from_errno("view_open");
5592 err = open_blame_view(blame_view, path, commit_id, repo);
5594 if (err->code == GOT_ERR_CANCELLED)
5596 view_close(blame_view);
5598 *new_view = blame_view;
5604 static const struct got_error *
5605 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5606 struct tog_tree_view_state *s)
5608 struct tog_view *log_view;
5609 const struct got_error *err = NULL;
5614 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5615 if (log_view == NULL)
5616 return got_error_from_errno("view_open");
5618 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5622 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5625 view_close(log_view);
5627 *new_view = log_view;
5632 static const struct got_error *
5633 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5634 const char *head_ref_name, struct got_repository *repo)
5636 const struct got_error *err = NULL;
5637 char *commit_id_str = NULL;
5638 struct tog_tree_view_state *s = &view->state.tree;
5639 struct got_commit_object *commit = NULL;
5641 TAILQ_INIT(&s->parents);
5642 STAILQ_INIT(&s->colors);
5644 s->commit_id = got_object_id_dup(commit_id);
5645 if (s->commit_id == NULL)
5646 return got_error_from_errno("got_object_id_dup");
5648 err = got_object_open_as_commit(&commit, repo, commit_id);
5653 * The root is opened here and will be closed when the view is closed.
5654 * Any visited subtrees and their path-wise parents are opened and
5657 err = got_object_open_as_tree(&s->root, repo,
5658 got_object_commit_get_tree_id(commit));
5663 err = got_object_id_str(&commit_id_str, commit_id);
5667 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5668 err = got_error_from_errno("asprintf");
5672 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5673 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5674 if (head_ref_name) {
5675 s->head_ref_name = strdup(head_ref_name);
5676 if (s->head_ref_name == NULL) {
5677 err = got_error_from_errno("strdup");
5683 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5684 err = add_color(&s->colors, "\\$$",
5685 TOG_COLOR_TREE_SUBMODULE,
5686 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5689 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5690 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5693 err = add_color(&s->colors, "/$",
5694 TOG_COLOR_TREE_DIRECTORY,
5695 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5699 err = add_color(&s->colors, "\\*$",
5700 TOG_COLOR_TREE_EXECUTABLE,
5701 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5705 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5706 get_color_value("TOG_COLOR_COMMIT"));
5711 view->show = show_tree_view;
5712 view->input = input_tree_view;
5713 view->close = close_tree_view;
5714 view->search_start = search_start_tree_view;
5715 view->search_next = search_next_tree_view;
5717 free(commit_id_str);
5719 got_object_commit_close(commit);
5721 close_tree_view(view);
5725 static const struct got_error *
5726 close_tree_view(struct tog_view *view)
5728 struct tog_tree_view_state *s = &view->state.tree;
5730 free_colors(&s->colors);
5731 free(s->tree_label);
5732 s->tree_label = NULL;
5734 s->commit_id = NULL;
5735 free(s->head_ref_name);
5736 s->head_ref_name = NULL;
5737 while (!TAILQ_EMPTY(&s->parents)) {
5738 struct tog_parent_tree *parent;
5739 parent = TAILQ_FIRST(&s->parents);
5740 TAILQ_REMOVE(&s->parents, parent, entry);
5741 if (parent->tree != s->root)
5742 got_object_tree_close(parent->tree);
5746 if (s->tree != NULL && s->tree != s->root)
5747 got_object_tree_close(s->tree);
5749 got_object_tree_close(s->root);
5753 static const struct got_error *
5754 search_start_tree_view(struct tog_view *view)
5756 struct tog_tree_view_state *s = &view->state.tree;
5758 s->matched_entry = NULL;
5763 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5765 regmatch_t regmatch;
5767 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5771 static const struct got_error *
5772 search_next_tree_view(struct tog_view *view)
5774 struct tog_tree_view_state *s = &view->state.tree;
5775 struct got_tree_entry *te = NULL;
5777 if (!view->searching) {
5778 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5782 if (s->matched_entry) {
5783 if (view->searching == TOG_SEARCH_FORWARD) {
5784 if (s->selected_entry)
5785 te = got_tree_entry_get_next(s->tree,
5788 te = got_object_tree_get_first_entry(s->tree);
5790 if (s->selected_entry == NULL)
5791 te = got_object_tree_get_last_entry(s->tree);
5793 te = got_tree_entry_get_prev(s->tree,
5797 if (s->selected_entry)
5798 te = s->selected_entry;
5799 else if (view->searching == TOG_SEARCH_FORWARD)
5800 te = got_object_tree_get_first_entry(s->tree);
5802 te = got_object_tree_get_last_entry(s->tree);
5807 if (s->matched_entry == NULL) {
5808 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5811 if (view->searching == TOG_SEARCH_FORWARD)
5812 te = got_object_tree_get_first_entry(s->tree);
5814 te = got_object_tree_get_last_entry(s->tree);
5817 if (match_tree_entry(te, &view->regex)) {
5818 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5819 s->matched_entry = te;
5823 if (view->searching == TOG_SEARCH_FORWARD)
5824 te = got_tree_entry_get_next(s->tree, te);
5826 te = got_tree_entry_get_prev(s->tree, te);
5829 if (s->matched_entry) {
5830 s->first_displayed_entry = s->matched_entry;
5837 static const struct got_error *
5838 show_tree_view(struct tog_view *view)
5840 const struct got_error *err = NULL;
5841 struct tog_tree_view_state *s = &view->state.tree;
5844 err = tree_entry_path(&parent_path, &s->parents, NULL);
5848 err = draw_tree_entries(view, parent_path);
5855 static const struct got_error *
5856 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5858 const struct got_error *err = NULL;
5859 struct tog_tree_view_state *s = &view->state.tree;
5860 struct tog_view *log_view, *ref_view;
5861 struct got_tree_entry *te;
5862 int begin_x = 0, n, nscroll = view->nlines - 3;
5866 s->show_ids = !s->show_ids;
5869 if (!s->selected_entry)
5871 if (view_is_parent_view(view))
5872 begin_x = view_split_begin_x(view->begin_x);
5873 err = log_selected_tree_entry(&log_view, begin_x, s);
5875 log_view->focussed = 1;
5876 if (view_is_parent_view(view)) {
5877 err = view_close_child(view);
5880 err = view_set_child(view, log_view);
5883 view->focus_child = 1;
5885 *new_view = log_view;
5888 if (view_is_parent_view(view))
5889 begin_x = view_split_begin_x(view->begin_x);
5890 ref_view = view_open(view->nlines, view->ncols,
5891 view->begin_y, begin_x, TOG_VIEW_REF);
5892 if (ref_view == NULL)
5893 return got_error_from_errno("view_open");
5894 err = open_ref_view(ref_view, s->repo);
5896 view_close(ref_view);
5900 ref_view->focussed = 1;
5901 if (view_is_parent_view(view)) {
5902 err = view_close_child(view);
5905 err = view_set_child(view, ref_view);
5908 view->focus_child = 1;
5910 *new_view = ref_view;
5915 if (s->tree == s->root)
5916 s->first_displayed_entry =
5917 got_object_tree_get_first_entry(s->tree);
5919 s->first_displayed_entry = NULL;
5924 te = got_object_tree_get_last_entry(s->tree);
5925 for (n = 0; n < view->nlines - 3; n++) {
5927 if(s->tree != s->root) {
5928 s->first_displayed_entry = NULL;
5933 s->first_displayed_entry = te;
5934 te = got_tree_entry_get_prev(s->tree, te);
5937 s->selected = n - 1;
5942 if (s->selected > 0) {
5946 tree_scroll_up(s, 1);
5954 if (s->tree == s->root) {
5955 if (got_object_tree_get_first_entry(s->tree) ==
5956 s->first_displayed_entry)
5957 s->selected -= MIN(s->selected, nscroll);
5959 if (s->first_displayed_entry == NULL)
5960 s->selected -= MIN(s->selected, nscroll);
5962 tree_scroll_up(s, MAX(0, nscroll));
5967 if (s->selected < s->ndisplayed - 1) {
5971 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5973 /* can't scroll any further */
5975 tree_scroll_down(s, 1);
5983 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5985 /* can't scroll any further; move cursor down */
5986 if (s->selected < s->ndisplayed - 1)
5987 s->selected += MIN(nscroll,
5988 s->ndisplayed - s->selected - 1);
5991 tree_scroll_down(s, nscroll);
5996 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
5997 struct tog_parent_tree *parent;
5998 /* user selected '..' */
5999 if (s->tree == s->root)
6001 parent = TAILQ_FIRST(&s->parents);
6002 TAILQ_REMOVE(&s->parents, parent,
6004 got_object_tree_close(s->tree);
6005 s->tree = parent->tree;
6006 s->first_displayed_entry =
6007 parent->first_displayed_entry;
6009 parent->selected_entry;
6010 s->selected = parent->selected;
6012 } else if (S_ISDIR(got_tree_entry_get_mode(
6013 s->selected_entry))) {
6014 struct got_tree_object *subtree;
6015 err = got_object_open_as_tree(&subtree, s->repo,
6016 got_tree_entry_get_id(s->selected_entry));
6019 err = tree_view_visit_subtree(s, subtree);
6021 got_object_tree_close(subtree);
6024 } else if (S_ISREG(got_tree_entry_get_mode(
6025 s->selected_entry))) {
6026 struct tog_view *blame_view;
6027 int begin_x = view_is_parent_view(view) ?
6028 view_split_begin_x(view->begin_x) : 0;
6030 err = blame_tree_entry(&blame_view, begin_x,
6031 s->selected_entry, &s->parents,
6032 s->commit_id, s->repo);
6036 blame_view->focussed = 1;
6037 if (view_is_parent_view(view)) {
6038 err = view_close_child(view);
6041 err = view_set_child(view, blame_view);
6044 view->focus_child = 1;
6046 *new_view = blame_view;
6050 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6051 s->selected = view->nlines - 4;
6065 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6070 static const struct got_error *
6071 cmd_tree(int argc, char *argv[])
6073 const struct got_error *error;
6074 struct got_repository *repo = NULL;
6075 struct got_worktree *worktree = NULL;
6076 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6077 struct got_object_id *commit_id = NULL;
6078 struct got_commit_object *commit = NULL;
6079 const char *commit_id_arg = NULL;
6081 struct got_reference *ref = NULL;
6082 const char *head_ref_name = NULL;
6084 struct tog_view *view;
6085 int *pack_fds = NULL;
6087 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6090 commit_id_arg = optarg;
6093 repo_path = realpath(optarg, NULL);
6094 if (repo_path == NULL)
6095 return got_error_from_errno2("realpath",
6110 error = got_repo_pack_fds_open(&pack_fds);
6114 if (repo_path == NULL) {
6115 cwd = getcwd(NULL, 0);
6117 return got_error_from_errno("getcwd");
6118 error = got_worktree_open(&worktree, cwd);
6119 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6123 strdup(got_worktree_get_repo_path(worktree));
6125 repo_path = strdup(cwd);
6126 if (repo_path == NULL) {
6127 error = got_error_from_errno("strdup");
6132 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6136 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6143 error = apply_unveil(got_repo_get_path(repo), NULL);
6147 error = tog_load_refs(repo, 0);
6151 if (commit_id_arg == NULL) {
6152 error = got_repo_match_object_id(&commit_id, &label,
6153 worktree ? got_worktree_get_head_ref_name(worktree) :
6154 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6157 head_ref_name = label;
6159 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6161 head_ref_name = got_ref_get_name(ref);
6162 else if (error->code != GOT_ERR_NOT_REF)
6164 error = got_repo_match_object_id(&commit_id, NULL,
6165 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6170 error = got_object_open_as_commit(&commit, repo, commit_id);
6174 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6176 error = got_error_from_errno("view_open");
6179 error = open_tree_view(view, commit_id, head_ref_name, repo);
6182 if (!got_path_is_root_dir(in_repo_path)) {
6183 error = tree_view_walk_path(&view->state.tree, commit,
6190 /* Release work tree lock. */
6191 got_worktree_close(worktree);
6194 error = view_loop(view);
6203 const struct got_error *close_err = got_repo_close(repo);
6208 const struct got_error *pack_err =
6209 got_repo_pack_fds_close(pack_fds);
6217 static const struct got_error *
6218 ref_view_load_refs(struct tog_ref_view_state *s)
6220 struct got_reflist_entry *sre;
6221 struct tog_reflist_entry *re;
6224 TAILQ_FOREACH(sre, &tog_refs, entry) {
6225 if (strncmp(got_ref_get_name(sre->ref),
6226 "refs/got/", 9) == 0 &&
6227 strncmp(got_ref_get_name(sre->ref),
6228 "refs/got/backup/", 16) != 0)
6231 re = malloc(sizeof(*re));
6233 return got_error_from_errno("malloc");
6235 re->ref = got_ref_dup(sre->ref);
6236 if (re->ref == NULL)
6237 return got_error_from_errno("got_ref_dup");
6238 re->idx = s->nrefs++;
6239 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6242 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6247 ref_view_free_refs(struct tog_ref_view_state *s)
6249 struct tog_reflist_entry *re;
6251 while (!TAILQ_EMPTY(&s->refs)) {
6252 re = TAILQ_FIRST(&s->refs);
6253 TAILQ_REMOVE(&s->refs, re, entry);
6254 got_ref_close(re->ref);
6259 static const struct got_error *
6260 open_ref_view(struct tog_view *view, struct got_repository *repo)
6262 const struct got_error *err = NULL;
6263 struct tog_ref_view_state *s = &view->state.ref;
6265 s->selected_entry = 0;
6268 TAILQ_INIT(&s->refs);
6269 STAILQ_INIT(&s->colors);
6271 err = ref_view_load_refs(s);
6275 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6276 err = add_color(&s->colors, "^refs/heads/",
6277 TOG_COLOR_REFS_HEADS,
6278 get_color_value("TOG_COLOR_REFS_HEADS"));
6282 err = add_color(&s->colors, "^refs/tags/",
6283 TOG_COLOR_REFS_TAGS,
6284 get_color_value("TOG_COLOR_REFS_TAGS"));
6288 err = add_color(&s->colors, "^refs/remotes/",
6289 TOG_COLOR_REFS_REMOTES,
6290 get_color_value("TOG_COLOR_REFS_REMOTES"));
6294 err = add_color(&s->colors, "^refs/got/backup/",
6295 TOG_COLOR_REFS_BACKUP,
6296 get_color_value("TOG_COLOR_REFS_BACKUP"));
6301 view->show = show_ref_view;
6302 view->input = input_ref_view;
6303 view->close = close_ref_view;
6304 view->search_start = search_start_ref_view;
6305 view->search_next = search_next_ref_view;
6308 free_colors(&s->colors);
6312 static const struct got_error *
6313 close_ref_view(struct tog_view *view)
6315 struct tog_ref_view_state *s = &view->state.ref;
6317 ref_view_free_refs(s);
6318 free_colors(&s->colors);
6323 static const struct got_error *
6324 resolve_reflist_entry(struct got_object_id **commit_id,
6325 struct tog_reflist_entry *re, struct got_repository *repo)
6327 const struct got_error *err = NULL;
6328 struct got_object_id *obj_id;
6329 struct got_tag_object *tag = NULL;
6334 err = got_ref_resolve(&obj_id, repo, re->ref);
6338 err = got_object_get_type(&obj_type, repo, obj_id);
6343 case GOT_OBJ_TYPE_COMMIT:
6344 *commit_id = obj_id;
6346 case GOT_OBJ_TYPE_TAG:
6347 err = got_object_open_as_tag(&tag, repo, obj_id);
6351 err = got_object_get_type(&obj_type, repo,
6352 got_object_tag_get_object_id(tag));
6355 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6356 err = got_error(GOT_ERR_OBJ_TYPE);
6359 *commit_id = got_object_id_dup(
6360 got_object_tag_get_object_id(tag));
6361 if (*commit_id == NULL) {
6362 err = got_error_from_errno("got_object_id_dup");
6367 err = got_error(GOT_ERR_OBJ_TYPE);
6373 got_object_tag_close(tag);
6381 static const struct got_error *
6382 log_ref_entry(struct tog_view **new_view, int begin_x,
6383 struct tog_reflist_entry *re, struct got_repository *repo)
6385 struct tog_view *log_view;
6386 const struct got_error *err = NULL;
6387 struct got_object_id *commit_id = NULL;
6391 err = resolve_reflist_entry(&commit_id, re, repo);
6393 if (err->code != GOT_ERR_OBJ_TYPE)
6399 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6400 if (log_view == NULL) {
6401 err = got_error_from_errno("view_open");
6405 err = open_log_view(log_view, commit_id, repo,
6406 got_ref_get_name(re->ref), "", 0);
6409 view_close(log_view);
6411 *new_view = log_view;
6417 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6419 struct tog_reflist_entry *re;
6422 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6425 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6426 while (i++ < maxscroll) {
6429 s->first_displayed_entry = re;
6430 re = TAILQ_PREV(re, tog_reflist_head, entry);
6435 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6437 struct tog_reflist_entry *next, *last;
6440 if (s->first_displayed_entry)
6441 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6443 next = TAILQ_FIRST(&s->refs);
6445 last = s->last_displayed_entry;
6446 while (next && last && n++ < maxscroll) {
6447 last = TAILQ_NEXT(last, entry);
6449 s->first_displayed_entry = next;
6450 next = TAILQ_NEXT(next, entry);
6455 static const struct got_error *
6456 search_start_ref_view(struct tog_view *view)
6458 struct tog_ref_view_state *s = &view->state.ref;
6460 s->matched_entry = NULL;
6465 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6467 regmatch_t regmatch;
6469 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6473 static const struct got_error *
6474 search_next_ref_view(struct tog_view *view)
6476 struct tog_ref_view_state *s = &view->state.ref;
6477 struct tog_reflist_entry *re = NULL;
6479 if (!view->searching) {
6480 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6484 if (s->matched_entry) {
6485 if (view->searching == TOG_SEARCH_FORWARD) {
6486 if (s->selected_entry)
6487 re = TAILQ_NEXT(s->selected_entry, entry);
6489 re = TAILQ_PREV(s->selected_entry,
6490 tog_reflist_head, entry);
6492 if (s->selected_entry == NULL)
6493 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6495 re = TAILQ_PREV(s->selected_entry,
6496 tog_reflist_head, entry);
6499 if (s->selected_entry)
6500 re = s->selected_entry;
6501 else if (view->searching == TOG_SEARCH_FORWARD)
6502 re = TAILQ_FIRST(&s->refs);
6504 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6509 if (s->matched_entry == NULL) {
6510 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6513 if (view->searching == TOG_SEARCH_FORWARD)
6514 re = TAILQ_FIRST(&s->refs);
6516 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6519 if (match_reflist_entry(re, &view->regex)) {
6520 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6521 s->matched_entry = re;
6525 if (view->searching == TOG_SEARCH_FORWARD)
6526 re = TAILQ_NEXT(re, entry);
6528 re = TAILQ_PREV(re, tog_reflist_head, entry);
6531 if (s->matched_entry) {
6532 s->first_displayed_entry = s->matched_entry;
6539 static const struct got_error *
6540 show_ref_view(struct tog_view *view)
6542 const struct got_error *err = NULL;
6543 struct tog_ref_view_state *s = &view->state.ref;
6544 struct tog_reflist_entry *re;
6547 struct tog_color *tc;
6549 int limit = view->nlines;
6551 werase(view->window);
6558 re = s->first_displayed_entry;
6560 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6562 return got_error_from_errno("asprintf");
6564 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6569 if (view_needs_focus_indication(view))
6570 wstandout(view->window);
6571 waddwstr(view->window, wline);
6572 if (view_needs_focus_indication(view))
6573 wstandend(view->window);
6578 if (width < view->ncols - 1)
6579 waddch(view->window, '\n');
6584 while (re && limit > 0) {
6586 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6589 struct got_commit_object *ci;
6590 struct got_tag_object *tag;
6591 struct got_object_id *id;
6595 err = got_ref_resolve(&id, s->repo, re->ref);
6598 err = got_object_open_as_tag(&tag, s->repo, id);
6600 if (err->code != GOT_ERR_OBJ_TYPE) {
6604 err = got_object_open_as_commit(&ci, s->repo,
6610 t = got_object_commit_get_committer_time(ci);
6611 got_object_commit_close(ci);
6613 t = got_object_tag_get_tagger_time(tag);
6614 got_object_tag_close(tag);
6617 if (gmtime_r(&t, &tm) == NULL)
6618 return got_error_from_errno("gmtime_r");
6619 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6620 return got_error(GOT_ERR_NO_SPACE);
6622 if (got_ref_is_symbolic(re->ref)) {
6623 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6624 ymd : "", got_ref_get_name(re->ref),
6625 got_ref_get_symref_target(re->ref)) == -1)
6626 return got_error_from_errno("asprintf");
6627 } else if (s->show_ids) {
6628 struct got_object_id *id;
6630 err = got_ref_resolve(&id, s->repo, re->ref);
6633 err = got_object_id_str(&id_str, id);
6638 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6639 got_ref_get_name(re->ref), id_str) == -1) {
6640 err = got_error_from_errno("asprintf");
6647 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6648 got_ref_get_name(re->ref)) == -1)
6649 return got_error_from_errno("asprintf");
6651 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6657 if (n == s->selected) {
6659 wstandout(view->window);
6660 s->selected_entry = re;
6662 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6664 wattr_on(view->window,
6665 COLOR_PAIR(tc->colorpair), NULL);
6666 waddwstr(view->window, wline);
6668 wattr_off(view->window,
6669 COLOR_PAIR(tc->colorpair), NULL);
6670 if (width < view->ncols - 1)
6671 waddch(view->window, '\n');
6672 if (n == s->selected && view->focussed)
6673 wstandend(view->window);
6679 s->last_displayed_entry = re;
6682 re = TAILQ_NEXT(re, entry);
6689 static const struct got_error *
6690 browse_ref_tree(struct tog_view **new_view, int begin_x,
6691 struct tog_reflist_entry *re, struct got_repository *repo)
6693 const struct got_error *err = NULL;
6694 struct got_object_id *commit_id = NULL;
6695 struct tog_view *tree_view;
6699 err = resolve_reflist_entry(&commit_id, re, repo);
6701 if (err->code != GOT_ERR_OBJ_TYPE)
6708 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6709 if (tree_view == NULL) {
6710 err = got_error_from_errno("view_open");
6714 err = open_tree_view(tree_view, commit_id,
6715 got_ref_get_name(re->ref), repo);
6719 *new_view = tree_view;
6724 static const struct got_error *
6725 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6727 const struct got_error *err = NULL;
6728 struct tog_ref_view_state *s = &view->state.ref;
6729 struct tog_view *log_view, *tree_view;
6730 struct tog_reflist_entry *re;
6731 int begin_x = 0, n, nscroll = view->nlines - 1;
6735 s->show_ids = !s->show_ids;
6738 s->show_date = !s->show_date;
6741 s->sort_by_date = !s->sort_by_date;
6742 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6743 got_ref_cmp_by_commit_timestamp_descending :
6744 tog_ref_cmp_by_name, s->repo);
6747 got_reflist_object_id_map_free(tog_refs_idmap);
6748 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6749 &tog_refs, s->repo);
6752 ref_view_free_refs(s);
6753 err = ref_view_load_refs(s);
6757 if (!s->selected_entry)
6759 if (view_is_parent_view(view))
6760 begin_x = view_split_begin_x(view->begin_x);
6761 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6764 log_view->focussed = 1;
6765 if (view_is_parent_view(view)) {
6766 err = view_close_child(view);
6769 err = view_set_child(view, log_view);
6772 view->focus_child = 1;
6774 *new_view = log_view;
6777 if (!s->selected_entry)
6779 if (view_is_parent_view(view))
6780 begin_x = view_split_begin_x(view->begin_x);
6781 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6783 if (err || tree_view == NULL)
6786 tree_view->focussed = 1;
6787 if (view_is_parent_view(view)) {
6788 err = view_close_child(view);
6791 err = view_set_child(view, tree_view);
6794 view->focus_child = 1;
6796 *new_view = tree_view;
6801 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6806 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6807 for (n = 0; n < view->nlines - 1; n++) {
6810 s->first_displayed_entry = re;
6811 re = TAILQ_PREV(re, tog_reflist_head, entry);
6814 s->selected = n - 1;
6819 if (s->selected > 0) {
6823 ref_scroll_up(s, 1);
6831 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6832 s->selected -= MIN(nscroll, s->selected);
6833 ref_scroll_up(s, MAX(0, nscroll));
6838 if (s->selected < s->ndisplayed - 1) {
6842 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6843 /* can't scroll any further */
6845 ref_scroll_down(s, 1);
6853 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6854 /* can't scroll any further; move cursor down */
6855 if (s->selected < s->ndisplayed - 1)
6856 s->selected += MIN(nscroll,
6857 s->ndisplayed - s->selected - 1);
6860 ref_scroll_down(s, nscroll);
6864 err = tog_load_refs(s->repo, s->sort_by_date);
6867 ref_view_free_refs(s);
6868 err = ref_view_load_refs(s);
6871 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6872 s->selected = view->nlines - 2;
6885 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6890 static const struct got_error *
6891 cmd_ref(int argc, char *argv[])
6893 const struct got_error *error;
6894 struct got_repository *repo = NULL;
6895 struct got_worktree *worktree = NULL;
6896 char *cwd = NULL, *repo_path = NULL;
6898 struct tog_view *view;
6899 int *pack_fds = NULL;
6901 while ((ch = getopt(argc, argv, "r:")) != -1) {
6904 repo_path = realpath(optarg, NULL);
6905 if (repo_path == NULL)
6906 return got_error_from_errno2("realpath",
6921 error = got_repo_pack_fds_open(&pack_fds);
6925 if (repo_path == NULL) {
6926 cwd = getcwd(NULL, 0);
6928 return got_error_from_errno("getcwd");
6929 error = got_worktree_open(&worktree, cwd);
6930 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6934 strdup(got_worktree_get_repo_path(worktree));
6936 repo_path = strdup(cwd);
6937 if (repo_path == NULL) {
6938 error = got_error_from_errno("strdup");
6943 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6949 error = apply_unveil(got_repo_get_path(repo), NULL);
6953 error = tog_load_refs(repo, 0);
6957 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6959 error = got_error_from_errno("view_open");
6963 error = open_ref_view(view, repo);
6968 /* Release work tree lock. */
6969 got_worktree_close(worktree);
6972 error = view_loop(view);
6977 const struct got_error *close_err = got_repo_close(repo);
6982 const struct got_error *pack_err =
6983 got_repo_pack_fds_close(pack_fds);
6992 list_commands(FILE *fp)
6996 fprintf(fp, "commands:");
6997 for (i = 0; i < nitems(tog_commands); i++) {
6998 const struct tog_cmd *cmd = &tog_commands[i];
6999 fprintf(fp, " %s", cmd->name);
7005 usage(int hflag, int status)
7007 FILE *fp = (status == 0) ? stdout : stderr;
7009 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7012 fprintf(fp, "lazy usage: %s path\n", getprogname());
7019 make_argv(int argc, ...)
7027 argv = calloc(argc, sizeof(char *));
7030 for (i = 0; i < argc; i++) {
7031 argv[i] = strdup(va_arg(ap, char *));
7032 if (argv[i] == NULL)
7041 * Try to convert 'tog path' into a 'tog log path' command.
7042 * The user could simply have mistyped the command rather than knowingly
7043 * provided a path. So check whether argv[0] can in fact be resolved
7044 * to a path in the HEAD commit and print a special error if not.
7045 * This hack is for mpi@ <3
7047 static const struct got_error *
7048 tog_log_with_path(int argc, char *argv[])
7050 const struct got_error *error = NULL, *close_err;
7051 const struct tog_cmd *cmd = NULL;
7052 struct got_repository *repo = NULL;
7053 struct got_worktree *worktree = NULL;
7054 struct got_object_id *commit_id = NULL, *id = NULL;
7055 struct got_commit_object *commit = NULL;
7056 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7057 char *commit_id_str = NULL, **cmd_argv = NULL;
7058 int *pack_fds = NULL;
7060 cwd = getcwd(NULL, 0);
7062 return got_error_from_errno("getcwd");
7064 error = got_repo_pack_fds_open(&pack_fds);
7068 error = got_worktree_open(&worktree, cwd);
7069 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7073 repo_path = strdup(got_worktree_get_repo_path(worktree));
7075 repo_path = strdup(cwd);
7076 if (repo_path == NULL) {
7077 error = got_error_from_errno("strdup");
7081 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7085 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7090 error = tog_load_refs(repo, 0);
7093 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7094 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7095 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7100 got_worktree_close(worktree);
7104 error = got_object_open_as_commit(&commit, repo, commit_id);
7108 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7110 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7112 fprintf(stderr, "%s: '%s' is no known command or path\n",
7113 getprogname(), argv[0]);
7118 close_err = got_repo_close(repo);
7123 error = got_object_id_str(&commit_id_str, commit_id);
7127 cmd = &tog_commands[0]; /* log */
7129 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7130 error = cmd->cmd_main(argc, cmd_argv);
7133 close_err = got_repo_close(repo);
7138 got_object_commit_close(commit);
7140 got_worktree_close(worktree);
7142 const struct got_error *pack_err =
7143 got_repo_pack_fds_close(pack_fds);
7148 free(commit_id_str);
7155 for (i = 0; i < argc; i++)
7164 main(int argc, char *argv[])
7166 const struct got_error *error = NULL;
7167 const struct tog_cmd *cmd = NULL;
7168 int ch, hflag = 0, Vflag = 0;
7169 char **cmd_argv = NULL;
7170 static const struct option longopts[] = {
7171 { "version", no_argument, NULL, 'V' },
7175 setlocale(LC_CTYPE, "");
7177 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7197 got_version_print_str();
7202 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7210 /* Build an argument vector which runs a default command. */
7211 cmd = &tog_commands[0];
7213 cmd_argv = make_argv(argc, cmd->name);
7217 /* Did the user specify a command? */
7218 for (i = 0; i < nitems(tog_commands); i++) {
7219 if (strncmp(tog_commands[i].name, argv[0],
7220 strlen(argv[0])) == 0) {
7221 cmd = &tog_commands[i];
7230 /* No command specified; try log with a path */
7231 error = tog_log_with_path(argc, argv);
7236 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7243 for (i = 0; i < argc; i++)
7248 if (error && error->code != GOT_ERR_CANCELLED)
7249 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);