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 int matched_idx = s->matched_entry->idx;
2355 int selected_idx = s->selected_entry->idx;
2358 * If the user has moved the cursor after we hit a match,
2359 * the position from where we should continue searching
2360 * might have changed.
2362 if (view->searching == TOG_SEARCH_FORWARD) {
2363 if (matched_idx > selected_idx)
2364 entry = TAILQ_NEXT(s->selected_entry, entry);
2366 entry = TAILQ_NEXT(s->matched_entry, entry);
2368 if (matched_idx < selected_idx)
2369 entry = TAILQ_PREV(s->selected_entry,
2370 commit_queue_head, entry);
2372 entry = TAILQ_PREV(s->matched_entry,
2373 commit_queue_head, entry);
2376 entry = s->selected_entry;
2382 if (entry == NULL) {
2383 if (s->thread_args.log_complete ||
2384 view->searching == TOG_SEARCH_BACKWARD) {
2385 view->search_next_done =
2386 (s->matched_entry == NULL ?
2387 TOG_SEARCH_HAVE_NONE : TOG_SEARCH_NO_MORE);
2388 s->search_entry = NULL;
2392 * Poke the log thread for more commits and return,
2393 * allowing the main loop to make progress. Search
2394 * will resume at s->search_entry once we come back.
2396 s->thread_args.commits_needed++;
2397 return trigger_log_thread(view, 0);
2400 err = match_commit(&have_match, entry->id, entry->commit,
2405 view->search_next_done = TOG_SEARCH_HAVE_MORE;
2406 s->matched_entry = entry;
2410 s->search_entry = entry;
2411 if (view->searching == TOG_SEARCH_FORWARD)
2412 entry = TAILQ_NEXT(entry, entry);
2414 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2417 if (s->matched_entry) {
2418 int cur = s->selected_entry->idx;
2419 while (cur < s->matched_entry->idx) {
2420 err = input_log_view(NULL, view, KEY_DOWN);
2425 while (cur > s->matched_entry->idx) {
2426 err = input_log_view(NULL, view, KEY_UP);
2433 s->search_entry = NULL;
2438 static const struct got_error *
2439 open_log_view(struct tog_view *view, struct got_object_id *start_id,
2440 struct got_repository *repo, const char *head_ref_name,
2441 const char *in_repo_path, int log_branches)
2443 const struct got_error *err = NULL;
2444 struct tog_log_view_state *s = &view->state.log;
2445 struct got_repository *thread_repo = NULL;
2446 struct got_commit_graph *thread_graph = NULL;
2449 if (in_repo_path != s->in_repo_path) {
2450 free(s->in_repo_path);
2451 s->in_repo_path = strdup(in_repo_path);
2452 if (s->in_repo_path == NULL)
2453 return got_error_from_errno("strdup");
2456 /* The commit queue only contains commits being displayed. */
2457 TAILQ_INIT(&s->commits.head);
2458 s->commits.ncommits = 0;
2461 if (head_ref_name) {
2462 s->head_ref_name = strdup(head_ref_name);
2463 if (s->head_ref_name == NULL) {
2464 err = got_error_from_errno("strdup");
2468 s->start_id = got_object_id_dup(start_id);
2469 if (s->start_id == NULL) {
2470 err = got_error_from_errno("got_object_id_dup");
2473 s->log_branches = log_branches;
2475 STAILQ_INIT(&s->colors);
2476 if (has_colors() && getenv("TOG_COLORS") != NULL) {
2477 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
2478 get_color_value("TOG_COLOR_COMMIT"));
2481 err = add_color(&s->colors, "^$", TOG_COLOR_AUTHOR,
2482 get_color_value("TOG_COLOR_AUTHOR"));
2484 free_colors(&s->colors);
2487 err = add_color(&s->colors, "^$", TOG_COLOR_DATE,
2488 get_color_value("TOG_COLOR_DATE"));
2490 free_colors(&s->colors);
2495 view->show = show_log_view;
2496 view->input = input_log_view;
2497 view->close = close_log_view;
2498 view->search_start = search_start_log_view;
2499 view->search_next = search_next_log_view;
2501 if (s->thread_args.pack_fds == NULL) {
2502 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2506 err = got_repo_open(&thread_repo, got_repo_get_path(repo), NULL,
2507 s->thread_args.pack_fds);
2510 err = got_commit_graph_open(&thread_graph, s->in_repo_path,
2514 err = got_commit_graph_iter_start(thread_graph, s->start_id,
2515 s->repo, NULL, NULL);
2519 errcode = pthread_cond_init(&s->thread_args.need_commits, NULL);
2521 err = got_error_set_errno(errcode, "pthread_cond_init");
2524 errcode = pthread_cond_init(&s->thread_args.commit_loaded, NULL);
2526 err = got_error_set_errno(errcode, "pthread_cond_init");
2530 s->thread_args.commits_needed = view->nlines;
2531 s->thread_args.graph = thread_graph;
2532 s->thread_args.commits = &s->commits;
2533 s->thread_args.in_repo_path = s->in_repo_path;
2534 s->thread_args.start_id = s->start_id;
2535 s->thread_args.repo = thread_repo;
2536 s->thread_args.log_complete = 0;
2537 s->thread_args.quit = &s->quit;
2538 s->thread_args.first_displayed_entry = &s->first_displayed_entry;
2539 s->thread_args.selected_entry = &s->selected_entry;
2540 s->thread_args.searching = &view->searching;
2541 s->thread_args.search_next_done = &view->search_next_done;
2542 s->thread_args.regex = &view->regex;
2545 close_log_view(view);
2549 static const struct got_error *
2550 show_log_view(struct tog_view *view)
2552 const struct got_error *err;
2553 struct tog_log_view_state *s = &view->state.log;
2555 if (s->thread == 0) { //NULL) {
2556 int errcode = pthread_create(&s->thread, NULL, log_thread,
2559 return got_error_set_errno(errcode, "pthread_create");
2560 if (s->thread_args.commits_needed > 0) {
2561 err = trigger_log_thread(view, 1);
2567 return draw_commits(view);
2570 static const struct got_error *
2571 input_log_view(struct tog_view **new_view, struct tog_view *view, int ch)
2573 const struct got_error *err = NULL;
2574 struct tog_log_view_state *s = &view->state.log;
2575 struct tog_view *diff_view = NULL, *tree_view = NULL;
2576 struct tog_view *ref_view = NULL;
2577 struct commit_queue_entry *entry;
2578 int begin_x = 0, n, nscroll = view->nlines - 1;
2580 if (s->thread_args.load_all) {
2581 if (ch == KEY_BACKSPACE)
2582 s->thread_args.load_all = 0;
2583 else if (s->thread_args.log_complete) {
2584 s->thread_args.load_all = 0;
2585 log_scroll_down(view, s->commits.ncommits);
2586 s->selected = MIN(view->nlines - 2,
2587 s->commits.ncommits - 1);
2601 view->x = MAX(view->maxx - view->ncols / 2, 0);
2605 if (view->x + view->ncols / 2 < view->maxx)
2606 view->x += 2; /* move two columns right */
2610 view->x -= MIN(view->x, 2); /* move two columns back */
2617 if (s->first_displayed_entry == NULL)
2619 if (s->selected > 0)
2622 log_scroll_up(s, 1);
2628 s->first_displayed_entry = TAILQ_FIRST(&s->commits.head);
2637 if (s->first_displayed_entry == NULL)
2639 if (TAILQ_FIRST(&s->commits.head) == s->first_displayed_entry)
2640 s->selected = MAX(0, s->selected - nscroll - 1);
2642 log_scroll_up(s, nscroll);
2650 if (s->first_displayed_entry == NULL)
2652 if (s->selected < MIN(view->nlines - 2,
2653 s->commits.ncommits - 1))
2656 err = log_scroll_down(view, 1);
2664 /* We don't know yet how many commits, so we're forced to
2665 * traverse them all. */
2666 if (!s->thread_args.log_complete) {
2667 s->thread_args.load_all = 1;
2668 return trigger_log_thread(view, 0);
2672 entry = TAILQ_LAST(&s->commits.head, commit_queue_head);
2673 for (n = 0; n < view->nlines - 1; n++) {
2676 s->first_displayed_entry = entry;
2677 entry = TAILQ_PREV(entry, commit_queue_head, entry);
2680 s->selected = n - 1;
2690 struct commit_queue_entry *first;
2691 first = s->first_displayed_entry;
2694 err = log_scroll_down(view, nscroll);
2697 if (first == s->first_displayed_entry &&
2698 s->selected < MIN(view->nlines - 2,
2699 s->commits.ncommits - 1)) {
2700 /* can't scroll further down */
2701 s->selected += MIN(s->last_displayed_entry->idx -
2702 s->selected_entry->idx, nscroll + 1);
2708 if (s->selected > view->nlines - 2)
2709 s->selected = view->nlines - 2;
2710 if (s->selected > s->commits.ncommits - 1)
2711 s->selected = s->commits.ncommits - 1;
2713 if (s->commits.ncommits < view->nlines - 1 &&
2714 !s->thread_args.log_complete) {
2715 s->thread_args.commits_needed += (view->nlines - 1) -
2716 s->commits.ncommits;
2717 err = trigger_log_thread(view, 1);
2723 if (s->selected_entry == NULL)
2725 if (view_is_parent_view(view))
2726 begin_x = view_split_begin_x(view->begin_x);
2727 err = open_diff_view_for_commit(&diff_view, begin_x,
2728 s->selected_entry->commit, s->selected_entry->id,
2733 diff_view->focussed = 1;
2734 if (view_is_parent_view(view)) {
2735 err = view_close_child(view);
2738 err = view_set_child(view, diff_view);
2741 view->focus_child = 1;
2743 *new_view = diff_view;
2746 if (s->selected_entry == NULL)
2748 if (view_is_parent_view(view))
2749 begin_x = view_split_begin_x(view->begin_x);
2750 err = browse_commit_tree(&tree_view, begin_x,
2751 s->selected_entry, s->in_repo_path, s->head_ref_name,
2756 tree_view->focussed = 1;
2757 if (view_is_parent_view(view)) {
2758 err = view_close_child(view);
2761 err = view_set_child(view, tree_view);
2764 view->focus_child = 1;
2766 *new_view = tree_view;
2771 if (ch == KEY_BACKSPACE &&
2772 got_path_is_root_dir(s->in_repo_path))
2774 err = stop_log_thread(s);
2777 if (ch == KEY_BACKSPACE) {
2779 err = got_path_dirname(&parent_path, s->in_repo_path);
2782 free(s->in_repo_path);
2783 s->in_repo_path = parent_path;
2784 s->thread_args.in_repo_path = s->in_repo_path;
2785 } else if (ch == CTRL('l')) {
2786 struct got_object_id *start_id;
2787 err = got_repo_match_object_id(&start_id, NULL,
2788 s->head_ref_name ? s->head_ref_name : GOT_REF_HEAD,
2789 GOT_OBJ_TYPE_COMMIT, &tog_refs, s->repo);
2793 s->start_id = start_id;
2794 s->thread_args.start_id = s->start_id;
2796 s->log_branches = !s->log_branches;
2798 if (s->thread_args.pack_fds == NULL) {
2799 err = got_repo_pack_fds_open(&s->thread_args.pack_fds);
2803 err = got_repo_open(&s->thread_args.repo,
2804 got_repo_get_path(s->repo), NULL,
2805 s->thread_args.pack_fds);
2809 err = tog_load_refs(s->repo, 0);
2812 err = got_commit_graph_open(&s->thread_args.graph,
2813 s->in_repo_path, !s->log_branches);
2816 err = got_commit_graph_iter_start(s->thread_args.graph,
2817 s->start_id, s->repo, NULL, NULL);
2820 free_commits(&s->commits);
2821 s->first_displayed_entry = NULL;
2822 s->last_displayed_entry = NULL;
2823 s->selected_entry = NULL;
2825 s->thread_args.log_complete = 0;
2827 s->thread_args.commits_needed = view->nlines;
2828 s->matched_entry = NULL;
2829 s->search_entry = NULL;
2832 if (view_is_parent_view(view))
2833 begin_x = view_split_begin_x(view->begin_x);
2834 ref_view = view_open(view->nlines, view->ncols,
2835 view->begin_y, begin_x, TOG_VIEW_REF);
2836 if (ref_view == NULL)
2837 return got_error_from_errno("view_open");
2838 err = open_ref_view(ref_view, s->repo);
2840 view_close(ref_view);
2844 ref_view->focussed = 1;
2845 if (view_is_parent_view(view)) {
2846 err = view_close_child(view);
2849 err = view_set_child(view, ref_view);
2852 view->focus_child = 1;
2854 *new_view = ref_view;
2863 static const struct got_error *
2864 apply_unveil(const char *repo_path, const char *worktree_path)
2866 const struct got_error *error;
2869 if (unveil("gmon.out", "rwc") != 0)
2870 return got_error_from_errno2("unveil", "gmon.out");
2872 if (repo_path && unveil(repo_path, "r") != 0)
2873 return got_error_from_errno2("unveil", repo_path);
2875 if (worktree_path && unveil(worktree_path, "rwc") != 0)
2876 return got_error_from_errno2("unveil", worktree_path);
2878 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
2879 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
2881 error = got_privsep_unveil_exec_helpers();
2885 if (unveil(NULL, NULL) != 0)
2886 return got_error_from_errno("unveil");
2895 * Override default signal handlers before starting ncurses.
2896 * This should prevent ncurses from installing its own
2897 * broken cleanup() signal handler.
2899 signal(SIGWINCH, tog_sigwinch);
2900 signal(SIGPIPE, tog_sigpipe);
2901 signal(SIGCONT, tog_sigcont);
2902 signal(SIGINT, tog_sigint);
2903 signal(SIGTERM, tog_sigterm);
2907 halfdelay(1); /* Do fast refresh while initial view is loading. */
2910 intrflush(stdscr, FALSE);
2911 keypad(stdscr, TRUE);
2913 if (getenv("TOG_COLORS") != NULL) {
2915 use_default_colors();
2919 static const struct got_error *
2920 get_in_repo_path_from_argv0(char **in_repo_path, int argc, char *argv[],
2921 struct got_repository *repo, struct got_worktree *worktree)
2923 const struct got_error *err = NULL;
2926 *in_repo_path = strdup("/");
2927 if (*in_repo_path == NULL)
2928 return got_error_from_errno("strdup");
2933 const char *prefix = got_worktree_get_path_prefix(worktree);
2936 err = got_worktree_resolve_path(&p, worktree, argv[0]);
2939 if (asprintf(in_repo_path, "%s%s%s", prefix,
2940 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
2942 err = got_error_from_errno("asprintf");
2943 *in_repo_path = NULL;
2947 err = got_repo_map_path(in_repo_path, repo, argv[0]);
2952 static const struct got_error *
2953 cmd_log(int argc, char *argv[])
2955 const struct got_error *error;
2956 struct got_repository *repo = NULL;
2957 struct got_worktree *worktree = NULL;
2958 struct got_object_id *start_id = NULL;
2959 char *in_repo_path = NULL, *repo_path = NULL, *cwd = NULL;
2960 char *start_commit = NULL, *label = NULL;
2961 struct got_reference *ref = NULL;
2962 const char *head_ref_name = NULL;
2963 int ch, log_branches = 0;
2964 struct tog_view *view;
2965 int *pack_fds = NULL;
2967 while ((ch = getopt(argc, argv, "bc:r:")) != -1) {
2973 start_commit = optarg;
2976 repo_path = realpath(optarg, NULL);
2977 if (repo_path == NULL)
2978 return got_error_from_errno2("realpath",
2993 error = got_repo_pack_fds_open(&pack_fds);
2997 if (repo_path == NULL) {
2998 cwd = getcwd(NULL, 0);
3000 return got_error_from_errno("getcwd");
3001 error = got_worktree_open(&worktree, cwd);
3002 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3006 strdup(got_worktree_get_repo_path(worktree));
3008 repo_path = strdup(cwd);
3009 if (repo_path == NULL) {
3010 error = got_error_from_errno("strdup");
3015 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3019 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
3026 error = apply_unveil(got_repo_get_path(repo),
3027 worktree ? got_worktree_get_root_path(worktree) : NULL);
3031 /* already loaded by tog_log_with_path()? */
3032 if (TAILQ_EMPTY(&tog_refs)) {
3033 error = tog_load_refs(repo, 0);
3038 if (start_commit == NULL) {
3039 error = got_repo_match_object_id(&start_id, &label,
3040 worktree ? got_worktree_get_head_ref_name(worktree) :
3041 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3044 head_ref_name = label;
3046 error = got_ref_open(&ref, repo, start_commit, 0);
3048 head_ref_name = got_ref_get_name(ref);
3049 else if (error->code != GOT_ERR_NOT_REF)
3051 error = got_repo_match_object_id(&start_id, NULL,
3052 start_commit, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
3057 view = view_open(0, 0, 0, 0, TOG_VIEW_LOG);
3059 error = got_error_from_errno("view_open");
3062 error = open_log_view(view, start_id, repo, head_ref_name,
3063 in_repo_path, log_branches);
3067 /* Release work tree lock. */
3068 got_worktree_close(worktree);
3071 error = view_loop(view);
3081 const struct got_error *close_err = got_repo_close(repo);
3086 got_worktree_close(worktree);
3088 const struct got_error *pack_err =
3089 got_repo_pack_fds_close(pack_fds);
3101 fprintf(stderr, "usage: %s diff [-a] [-C number] [-r repository-path] "
3102 "[-w] object1 object2\n", getprogname());
3107 match_line(const char *line, regex_t *regex, size_t nmatch,
3108 regmatch_t *regmatch)
3110 return regexec(regex, line, nmatch, regmatch, 0) == 0;
3114 match_color(struct tog_colors *colors, const char *line)
3116 struct tog_color *tc = NULL;
3118 STAILQ_FOREACH(tc, colors, entry) {
3119 if (match_line(line, &tc->regex, 0, NULL))
3126 static const struct got_error *
3127 add_matched_line(int *wtotal, const char *line, int wlimit, int col_tab_align,
3128 WINDOW *window, int skipcol, regmatch_t *regmatch)
3130 const struct got_error *err = NULL;
3132 wchar_t *wline = NULL;
3133 int rme, rms, n, width, scrollx;
3134 int width0 = 0, width1 = 0, width2 = 0;
3135 char *seg0 = NULL, *seg1 = NULL, *seg2 = NULL;
3139 rms = regmatch->rm_so;
3140 rme = regmatch->rm_eo;
3142 err = expand_tab(&exstr, line);
3146 /* Split the line into 3 segments, according to match offsets. */
3147 seg0 = strndup(exstr, rms);
3149 err = got_error_from_errno("strndup");
3152 seg1 = strndup(exstr + rms, rme - rms);
3154 err = got_error_from_errno("strndup");
3157 seg2 = strdup(exstr + rme);
3159 err = got_error_from_errno("strndup");
3163 /* draw up to matched token if we haven't scrolled past it */
3164 err = format_line(&wline, &width0, NULL, seg0, 0, wlimit,
3168 n = MAX(width0 - skipcol, 0);
3171 err = format_line(&wline, &width, &scrollx, seg0, skipcol,
3172 wlimit, col_tab_align, 1);
3175 waddwstr(window, &wline[scrollx]);
3185 err = format_line(&wline, &width1, NULL, seg1, 0, wlimit,
3189 wlen = wcslen(wline);
3191 width = wcwidth(wline[i]);
3193 /* should not happen, tabs are expanded */
3194 err = got_error(GOT_ERR_RANGE);
3197 if (width0 + w + width > skipcol)
3202 /* draw (visible part of) matched token (if scrolled into it) */
3203 if (width1 - w > 0) {
3204 wattron(window, A_STANDOUT);
3205 waddwstr(window, &wline[i]);
3206 wattroff(window, A_STANDOUT);
3207 wlimit -= (width1 - w);
3208 *wtotal += (width1 - w);
3212 if (wlimit > 0) { /* draw rest of line */
3214 if (skipcol > width0 + width1) {
3215 err = format_line(&wline, &width2, &scrollx, seg2,
3216 skipcol - (width0 + width1), wlimit,
3220 waddwstr(window, &wline[scrollx]);
3222 err = format_line(&wline, &width2, NULL, seg2, 0,
3223 wlimit, col_tab_align, 1);
3226 waddwstr(window, wline);
3239 static const struct got_error *
3240 draw_file(struct tog_view *view, const char *header)
3242 struct tog_diff_view_state *s = &view->state.diff;
3243 regmatch_t *regmatch = &view->regmatch;
3244 const struct got_error *err;
3247 size_t linesize = 0;
3249 struct tog_color *tc;
3252 int max_lines = view->nlines;
3253 int nlines = s->nlines;
3256 line_offset = s->line_offsets[s->first_displayed_line - 1];
3257 if (fseeko(s->f, line_offset, SEEK_SET) == -1)
3258 return got_error_from_errno("fseek");
3260 werase(view->window);
3263 if (asprintf(&line, "[%d/%d] %s",
3264 s->first_displayed_line - 1 + s->selected_line, nlines,
3266 return got_error_from_errno("asprintf");
3267 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
3273 if (view_needs_focus_indication(view))
3274 wstandout(view->window);
3275 waddwstr(view->window, wline);
3278 if (view_needs_focus_indication(view))
3279 wstandend(view->window);
3280 if (width <= view->ncols - 1)
3281 waddch(view->window, '\n');
3291 while (max_lines > 0 && nprinted < max_lines) {
3292 linelen = getline(&line, &linesize, s->f);
3293 if (linelen == -1) {
3299 return got_ferror(s->f, GOT_ERR_IO);
3302 /* Set view->maxx based on full line length. */
3303 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 0,
3309 view->maxx = MAX(view->maxx, width);
3313 tc = match_color(&s->colors, line);
3315 wattr_on(view->window,
3316 COLOR_PAIR(tc->colorpair), NULL);
3317 if (s->first_displayed_line + nprinted == s->matched_line &&
3318 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
3319 err = add_matched_line(&width, line, view->ncols, 0,
3320 view->window, view->x, regmatch);
3327 err = format_line(&wline, &width, &skip, line,
3328 view->x, view->ncols, 0, view->x ? 1 : 0);
3333 waddwstr(view->window, &wline[skip]);
3338 wattr_off(view->window,
3339 COLOR_PAIR(tc->colorpair), NULL);
3340 if (width <= view->ncols - 1)
3341 waddch(view->window, '\n');
3346 s->last_displayed_line = s->first_displayed_line +
3349 s->last_displayed_line = s->first_displayed_line;
3354 while (nprinted < view->nlines) {
3355 waddch(view->window, '\n');
3359 err = format_line(&wline, &width, NULL, TOG_EOF_STRING, 0,
3365 wstandout(view->window);
3366 waddwstr(view->window, wline);
3369 wstandend(view->window);
3376 get_datestr(time_t *time, char *datebuf)
3378 struct tm mytm, *tm;
3381 tm = gmtime_r(time, &mytm);
3384 s = asctime_r(tm, datebuf);
3387 p = strchr(s, '\n');
3393 static const struct got_error *
3394 get_changed_paths(struct got_pathlist_head *paths,
3395 struct got_commit_object *commit, struct got_repository *repo)
3397 const struct got_error *err = NULL;
3398 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3399 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3400 struct got_object_qid *qid;
3402 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3404 struct got_commit_object *pcommit;
3405 err = got_object_open_as_commit(&pcommit, repo,
3410 tree_id1 = got_object_id_dup(
3411 got_object_commit_get_tree_id(pcommit));
3412 if (tree_id1 == NULL) {
3413 got_object_commit_close(pcommit);
3414 return got_error_from_errno("got_object_id_dup");
3416 got_object_commit_close(pcommit);
3421 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3426 tree_id2 = got_object_commit_get_tree_id(commit);
3427 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3431 err = got_diff_tree(tree1, tree2, NULL, NULL, "", "", repo,
3432 got_diff_tree_collect_changed_paths, paths, 0);
3435 got_object_tree_close(tree1);
3437 got_object_tree_close(tree2);
3442 static const struct got_error *
3443 add_line_offset(off_t **line_offsets, size_t *nlines, off_t off)
3447 p = reallocarray(*line_offsets, *nlines + 1, sizeof(off_t));
3449 return got_error_from_errno("reallocarray");
3451 (*line_offsets)[*nlines] = off;
3456 static const struct got_error *
3457 write_commit_info(off_t **line_offsets, size_t *nlines,
3458 struct got_object_id *commit_id, struct got_reflist_head *refs,
3459 struct got_repository *repo, FILE *outfile)
3461 const struct got_error *err = NULL;
3462 char datebuf[26], *datestr;
3463 struct got_commit_object *commit;
3464 char *id_str = NULL, *logmsg = NULL, *s = NULL, *line;
3465 time_t committer_time;
3466 const char *author, *committer;
3467 char *refs_str = NULL;
3468 struct got_pathlist_head changed_paths;
3469 struct got_pathlist_entry *pe;
3473 TAILQ_INIT(&changed_paths);
3476 err = build_refs_str(&refs_str, refs, commit_id, repo);
3481 err = got_object_open_as_commit(&commit, repo, commit_id);
3485 err = got_object_id_str(&id_str, commit_id);
3487 err = got_error_from_errno("got_object_id_str");
3491 err = add_line_offset(line_offsets, nlines, 0);
3495 n = fprintf(outfile, "commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3496 refs_str ? refs_str : "", refs_str ? ")" : "");
3498 err = got_error_from_errno("fprintf");
3502 err = add_line_offset(line_offsets, nlines, outoff);
3506 n = fprintf(outfile, "from: %s\n",
3507 got_object_commit_get_author(commit));
3509 err = got_error_from_errno("fprintf");
3513 err = add_line_offset(line_offsets, nlines, outoff);
3517 committer_time = got_object_commit_get_committer_time(commit);
3518 datestr = get_datestr(&committer_time, datebuf);
3520 n = fprintf(outfile, "date: %s UTC\n", datestr);
3522 err = got_error_from_errno("fprintf");
3526 err = add_line_offset(line_offsets, nlines, outoff);
3530 author = got_object_commit_get_author(commit);
3531 committer = got_object_commit_get_committer(commit);
3532 if (strcmp(author, committer) != 0) {
3533 n = fprintf(outfile, "via: %s\n", committer);
3535 err = got_error_from_errno("fprintf");
3539 err = add_line_offset(line_offsets, nlines, outoff);
3543 if (got_object_commit_get_nparents(commit) > 1) {
3544 const struct got_object_id_queue *parent_ids;
3545 struct got_object_qid *qid;
3547 parent_ids = got_object_commit_get_parent_ids(commit);
3548 STAILQ_FOREACH(qid, parent_ids, entry) {
3549 err = got_object_id_str(&id_str, &qid->id);
3552 n = fprintf(outfile, "parent %d: %s\n", pn++, id_str);
3554 err = got_error_from_errno("fprintf");
3558 err = add_line_offset(line_offsets, nlines, outoff);
3566 err = got_object_commit_get_logmsg(&logmsg, commit);
3570 while ((line = strsep(&s, "\n")) != NULL) {
3571 n = fprintf(outfile, "%s\n", line);
3573 err = got_error_from_errno("fprintf");
3577 err = add_line_offset(line_offsets, nlines, outoff);
3582 err = get_changed_paths(&changed_paths, commit, repo);
3585 TAILQ_FOREACH(pe, &changed_paths, entry) {
3586 struct got_diff_changed_path *cp = pe->data;
3587 n = fprintf(outfile, "%c %s\n", cp->status, pe->path);
3589 err = got_error_from_errno("fprintf");
3593 err = add_line_offset(line_offsets, nlines, outoff);
3596 free((char *)pe->path);
3600 fputc('\n', outfile);
3602 err = add_line_offset(line_offsets, nlines, outoff);
3604 got_pathlist_free(&changed_paths);
3608 got_object_commit_close(commit);
3610 free(*line_offsets);
3611 *line_offsets = NULL;
3617 static const struct got_error *
3618 create_diff(struct tog_diff_view_state *s)
3620 const struct got_error *err = NULL;
3624 free(s->line_offsets);
3625 s->line_offsets = malloc(sizeof(off_t));
3626 if (s->line_offsets == NULL)
3627 return got_error_from_errno("malloc");
3632 err = got_error_from_errno("got_opentemp");
3635 if (s->f && fclose(s->f) == EOF) {
3636 err = got_error_from_errno("fclose");
3642 err = got_object_get_type(&obj_type, s->repo, s->id1);
3644 err = got_object_get_type(&obj_type, s->repo, s->id2);
3649 case GOT_OBJ_TYPE_BLOB:
3650 err = got_diff_objects_as_blobs(&s->line_offsets, &s->nlines,
3651 s->f1, s->f2, s->id1, s->id2, s->label1, s->label2,
3652 s->diff_context, s->ignore_whitespace, s->force_text_diff,
3655 case GOT_OBJ_TYPE_TREE:
3656 err = got_diff_objects_as_trees(&s->line_offsets, &s->nlines,
3657 s->f1, s->f2, s->id1, s->id2, NULL, "", "", s->diff_context,
3658 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3660 case GOT_OBJ_TYPE_COMMIT: {
3661 const struct got_object_id_queue *parent_ids;
3662 struct got_object_qid *pid;
3663 struct got_commit_object *commit2;
3664 struct got_reflist_head *refs;
3666 err = got_object_open_as_commit(&commit2, s->repo, s->id2);
3669 refs = got_reflist_object_id_map_lookup(tog_refs_idmap, s->id2);
3670 /* Show commit info if we're diffing to a parent/root commit. */
3671 if (s->id1 == NULL) {
3672 err = write_commit_info(&s->line_offsets, &s->nlines,
3673 s->id2, refs, s->repo, s->f);
3677 parent_ids = got_object_commit_get_parent_ids(commit2);
3678 STAILQ_FOREACH(pid, parent_ids, entry) {
3679 if (got_object_id_cmp(s->id1, &pid->id) == 0) {
3680 err = write_commit_info(
3681 &s->line_offsets, &s->nlines,
3682 s->id2, refs, s->repo, s->f);
3689 got_object_commit_close(commit2);
3691 err = got_diff_objects_as_commits(&s->line_offsets, &s->nlines,
3692 s->f1, s->f2, s->id1, s->id2, NULL, s->diff_context,
3693 s->ignore_whitespace, s->force_text_diff, s->repo, s->f);
3697 err = got_error(GOT_ERR_OBJ_TYPE);
3703 if (s->f && fflush(s->f) != 0 && err == NULL)
3704 err = got_error_from_errno("fflush");
3709 diff_view_indicate_progress(struct tog_view *view)
3711 mvwaddstr(view->window, 0, 0, "diffing...");
3716 static const struct got_error *
3717 search_start_diff_view(struct tog_view *view)
3719 struct tog_diff_view_state *s = &view->state.diff;
3721 s->matched_line = 0;
3725 static const struct got_error *
3726 search_next_diff_view(struct tog_view *view)
3728 struct tog_diff_view_state *s = &view->state.diff;
3729 const struct got_error *err = NULL;
3732 size_t linesize = 0;
3735 if (!view->searching) {
3736 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3740 if (s->matched_line) {
3741 if (view->searching == TOG_SEARCH_FORWARD)
3742 lineno = s->matched_line + 1;
3744 lineno = s->matched_line - 1;
3746 lineno = s->first_displayed_line;
3751 if (lineno <= 0 || lineno > s->nlines) {
3752 if (s->matched_line == 0) {
3753 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3757 if (view->searching == TOG_SEARCH_FORWARD)
3763 offset = s->line_offsets[lineno - 1];
3764 if (fseeko(s->f, offset, SEEK_SET) != 0) {
3766 return got_error_from_errno("fseeko");
3768 linelen = getline(&line, &linesize, s->f);
3769 if (linelen != -1) {
3771 err = expand_tab(&exstr, line);
3774 if (match_line(exstr, &view->regex, 1,
3776 view->search_next_done = TOG_SEARCH_HAVE_MORE;
3777 s->matched_line = lineno;
3783 if (view->searching == TOG_SEARCH_FORWARD)
3790 if (s->matched_line) {
3791 s->first_displayed_line = s->matched_line;
3792 s->selected_line = 1;
3798 static const struct got_error *
3799 close_diff_view(struct tog_view *view)
3801 const struct got_error *err = NULL;
3802 struct tog_diff_view_state *s = &view->state.diff;
3808 if (s->f && fclose(s->f) == EOF)
3809 err = got_error_from_errno("fclose");
3811 if (s->f1 && fclose(s->f1) == EOF)
3812 err = got_error_from_errno("fclose");
3814 if (s->f2 && fclose(s->f2) == EOF)
3815 err = got_error_from_errno("fclose");
3817 free_colors(&s->colors);
3818 free(s->line_offsets);
3819 s->line_offsets = NULL;
3824 static const struct got_error *
3825 open_diff_view(struct tog_view *view, struct got_object_id *id1,
3826 struct got_object_id *id2, const char *label1, const char *label2,
3827 int diff_context, int ignore_whitespace, int force_text_diff,
3828 struct tog_view *log_view, struct got_repository *repo)
3830 const struct got_error *err;
3831 struct tog_diff_view_state *s = &view->state.diff;
3833 memset(s, 0, sizeof(*s));
3835 if (id1 != NULL && id2 != NULL) {
3837 err = got_object_get_type(&type1, repo, id1);
3840 err = got_object_get_type(&type2, repo, id2);
3845 return got_error(GOT_ERR_OBJ_TYPE);
3847 s->first_displayed_line = 1;
3848 s->last_displayed_line = view->nlines;
3849 s->selected_line = 1;
3857 s->id1 = got_object_id_dup(id1);
3859 return got_error_from_errno("got_object_id_dup");
3863 s->id2 = got_object_id_dup(id2);
3864 if (s->id2 == NULL) {
3865 err = got_error_from_errno("got_object_id_dup");
3869 s->f1 = got_opentemp();
3870 if (s->f1 == NULL) {
3871 err = got_error_from_errno("got_opentemp");
3875 s->f2 = got_opentemp();
3876 if (s->f2 == NULL) {
3877 err = got_error_from_errno("got_opentemp");
3881 s->first_displayed_line = 1;
3882 s->last_displayed_line = view->nlines;
3883 s->diff_context = diff_context;
3884 s->ignore_whitespace = ignore_whitespace;
3885 s->force_text_diff = force_text_diff;
3886 s->log_view = log_view;
3889 STAILQ_INIT(&s->colors);
3890 if (has_colors() && getenv("TOG_COLORS") != NULL) {
3891 err = add_color(&s->colors,
3892 "^-", TOG_COLOR_DIFF_MINUS,
3893 get_color_value("TOG_COLOR_DIFF_MINUS"));
3896 err = add_color(&s->colors, "^\\+",
3897 TOG_COLOR_DIFF_PLUS,
3898 get_color_value("TOG_COLOR_DIFF_PLUS"));
3901 err = add_color(&s->colors,
3902 "^@@", TOG_COLOR_DIFF_CHUNK_HEADER,
3903 get_color_value("TOG_COLOR_DIFF_CHUNK_HEADER"));
3907 err = add_color(&s->colors,
3908 "^(commit [0-9a-f]|parent [0-9]|(blob|file) [-+] |"
3909 "[MDmA] [^ ])", TOG_COLOR_DIFF_META,
3910 get_color_value("TOG_COLOR_DIFF_META"));
3914 err = add_color(&s->colors,
3915 "^(from|via): ", TOG_COLOR_AUTHOR,
3916 get_color_value("TOG_COLOR_AUTHOR"));
3920 err = add_color(&s->colors,
3921 "^date: ", TOG_COLOR_DATE,
3922 get_color_value("TOG_COLOR_DATE"));
3927 if (log_view && view_is_splitscreen(view))
3928 show_log_view(log_view); /* draw vborder */
3929 diff_view_indicate_progress(view);
3931 err = create_diff(s);
3933 view->show = show_diff_view;
3934 view->input = input_diff_view;
3935 view->close = close_diff_view;
3936 view->search_start = search_start_diff_view;
3937 view->search_next = search_next_diff_view;
3940 close_diff_view(view);
3944 static const struct got_error *
3945 show_diff_view(struct tog_view *view)
3947 const struct got_error *err;
3948 struct tog_diff_view_state *s = &view->state.diff;
3949 char *id_str1 = NULL, *id_str2, *header;
3950 const char *label1, *label2;
3953 err = got_object_id_str(&id_str1, s->id1);
3956 label1 = s->label1 ? : id_str1;
3958 label1 = "/dev/null";
3960 err = got_object_id_str(&id_str2, s->id2);
3963 label2 = s->label2 ? : id_str2;
3965 if (asprintf(&header, "diff %s %s", label1, label2) == -1) {
3966 err = got_error_from_errno("asprintf");
3974 err = draw_file(view, header);
3979 static const struct got_error *
3980 set_selected_commit(struct tog_diff_view_state *s,
3981 struct commit_queue_entry *entry)
3983 const struct got_error *err;
3984 const struct got_object_id_queue *parent_ids;
3985 struct got_commit_object *selected_commit;
3986 struct got_object_qid *pid;
3989 s->id2 = got_object_id_dup(entry->id);
3991 return got_error_from_errno("got_object_id_dup");
3993 err = got_object_open_as_commit(&selected_commit, s->repo, entry->id);
3996 parent_ids = got_object_commit_get_parent_ids(selected_commit);
3998 pid = STAILQ_FIRST(parent_ids);
3999 s->id1 = pid ? got_object_id_dup(&pid->id) : NULL;
4000 got_object_commit_close(selected_commit);
4004 static const struct got_error *
4005 input_diff_view(struct tog_view **new_view, struct tog_view *view, int ch)
4007 const struct got_error *err = NULL;
4008 struct tog_diff_view_state *s = &view->state.diff;
4009 struct tog_log_view_state *ls;
4010 struct commit_queue_entry *old_selected_entry;
4012 size_t linesize = 0;
4014 int i, nscroll = view->nlines - 1;
4021 view->x = MAX(view->maxx - view->ncols / 3, 0);
4025 if (view->x + view->ncols / 3 < view->maxx)
4026 view->x += 2; /* move two columns right */
4030 view->x -= MIN(view->x, 2); /* move two columns back */
4035 s->force_text_diff = !s->force_text_diff;
4037 s->ignore_whitespace = !s->ignore_whitespace;
4038 wclear(view->window);
4039 s->first_displayed_line = 1;
4040 s->last_displayed_line = view->nlines;
4041 s->matched_line = 0;
4042 diff_view_indicate_progress(view);
4043 err = create_diff(s);
4047 s->first_displayed_line = 1;
4054 s->first_displayed_line = (s->nlines - view->nlines) + 2;
4060 if (s->first_displayed_line > 1)
4061 s->first_displayed_line--;
4069 if (s->first_displayed_line == 1)
4072 while (i++ < nscroll && s->first_displayed_line > 1)
4073 s->first_displayed_line--;
4079 s->first_displayed_line++;
4091 while (!s->eof && i++ < nscroll) {
4092 linelen = getline(&line, &linesize, s->f);
4093 s->first_displayed_line++;
4094 if (linelen == -1) {
4098 err = got_ferror(s->f, GOT_ERR_IO);
4105 if (s->diff_context > 0) {
4107 s->matched_line = 0;
4108 diff_view_indicate_progress(view);
4109 err = create_diff(s);
4110 if (s->first_displayed_line + view->nlines - 1 >
4112 s->first_displayed_line = 1;
4113 s->last_displayed_line = view->nlines;
4118 if (s->diff_context < GOT_DIFF_MAX_CONTEXT) {
4120 s->matched_line = 0;
4121 diff_view_indicate_progress(view);
4122 err = create_diff(s);
4127 if (s->log_view == NULL)
4129 ls = &s->log_view->state.log;
4130 old_selected_entry = ls->selected_entry;
4132 err = input_log_view(NULL, s->log_view, KEY_UP);
4136 if (old_selected_entry == ls->selected_entry)
4139 err = set_selected_commit(s, ls->selected_entry);
4143 s->first_displayed_line = 1;
4144 s->last_displayed_line = view->nlines;
4145 s->matched_line = 0;
4148 diff_view_indicate_progress(view);
4149 err = create_diff(s);
4153 if (s->log_view == NULL)
4155 ls = &s->log_view->state.log;
4156 old_selected_entry = ls->selected_entry;
4158 err = input_log_view(NULL, s->log_view, KEY_DOWN);
4162 if (old_selected_entry == ls->selected_entry)
4165 err = set_selected_commit(s, ls->selected_entry);
4169 s->first_displayed_line = 1;
4170 s->last_displayed_line = view->nlines;
4171 s->matched_line = 0;
4174 diff_view_indicate_progress(view);
4175 err = create_diff(s);
4184 static const struct got_error *
4185 cmd_diff(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4190 struct got_object_id *id1 = NULL, *id2 = NULL;
4191 char *repo_path = NULL, *cwd = NULL;
4192 char *id_str1 = NULL, *id_str2 = NULL;
4193 char *label1 = NULL, *label2 = NULL;
4194 int diff_context = 3, ignore_whitespace = 0;
4195 int ch, force_text_diff = 0;
4197 struct tog_view *view;
4198 int *pack_fds = NULL;
4200 while ((ch = getopt(argc, argv, "aC:r:w")) != -1) {
4203 force_text_diff = 1;
4206 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4209 errx(1, "number of context lines is %s: %s",
4213 repo_path = realpath(optarg, NULL);
4214 if (repo_path == NULL)
4215 return got_error_from_errno2("realpath",
4217 got_path_strip_trailing_slashes(repo_path);
4220 ignore_whitespace = 1;
4232 usage_diff(); /* TODO show local worktree changes */
4233 } else if (argc == 2) {
4239 error = got_repo_pack_fds_open(&pack_fds);
4243 if (repo_path == NULL) {
4244 cwd = getcwd(NULL, 0);
4246 return got_error_from_errno("getcwd");
4247 error = got_worktree_open(&worktree, cwd);
4248 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4252 strdup(got_worktree_get_repo_path(worktree));
4254 repo_path = strdup(cwd);
4255 if (repo_path == NULL) {
4256 error = got_error_from_errno("strdup");
4261 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4267 error = apply_unveil(got_repo_get_path(repo), NULL);
4271 error = tog_load_refs(repo, 0);
4275 error = got_repo_match_object_id(&id1, &label1, id_str1,
4276 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4280 error = got_repo_match_object_id(&id2, &label2, id_str2,
4281 GOT_OBJ_TYPE_ANY, &tog_refs, repo);
4285 view = view_open(0, 0, 0, 0, TOG_VIEW_DIFF);
4287 error = got_error_from_errno("view_open");
4290 error = open_diff_view(view, id1, id2, label1, label2, diff_context,
4291 ignore_whitespace, force_text_diff, NULL, repo);
4294 error = view_loop(view);
4301 const struct got_error *close_err = got_repo_close(repo);
4306 got_worktree_close(worktree);
4308 const struct got_error *pack_err =
4309 got_repo_pack_fds_close(pack_fds);
4322 "usage: %s blame [-c commit] [-r repository-path] path\n",
4327 struct tog_blame_line {
4329 struct got_object_id *id;
4332 static const struct got_error *
4333 draw_blame(struct tog_view *view)
4335 struct tog_blame_view_state *s = &view->state.blame;
4336 struct tog_blame *blame = &s->blame;
4337 regmatch_t *regmatch = &view->regmatch;
4338 const struct got_error *err;
4339 int lineno = 0, nprinted = 0;
4341 size_t linesize = 0;
4345 struct tog_blame_line *blame_line;
4346 struct got_object_id *prev_id = NULL;
4348 struct tog_color *tc;
4350 err = got_object_id_str(&id_str, &s->blamed_commit->id);
4355 werase(view->window);
4357 if (asprintf(&line, "commit %s", id_str) == -1) {
4358 err = got_error_from_errno("asprintf");
4363 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4368 if (view_needs_focus_indication(view))
4369 wstandout(view->window);
4370 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4372 wattr_on(view->window,
4373 COLOR_PAIR(tc->colorpair), NULL);
4374 waddwstr(view->window, wline);
4376 wattr_off(view->window,
4377 COLOR_PAIR(tc->colorpair), NULL);
4378 if (view_needs_focus_indication(view))
4379 wstandend(view->window);
4382 if (width < view->ncols - 1)
4383 waddch(view->window, '\n');
4385 if (asprintf(&line, "[%d/%d] %s%s",
4386 s->first_displayed_line - 1 + s->selected_line, blame->nlines,
4387 s->blame_complete ? "" : "annotating... ", s->path) == -1) {
4389 return got_error_from_errno("asprintf");
4392 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
4397 waddwstr(view->window, wline);
4400 if (width < view->ncols - 1)
4401 waddch(view->window, '\n');
4405 while (nprinted < view->nlines - 2) {
4406 linelen = getline(&line, &linesize, blame->f);
4407 if (linelen == -1) {
4408 if (feof(blame->f)) {
4413 return got_ferror(blame->f, GOT_ERR_IO);
4415 if (++lineno < s->first_displayed_line)
4418 /* Set view->maxx based on full line length. */
4419 err = format_line(&wline, &width, NULL, line, 0, INT_MAX, 9, 1);
4426 view->maxx = MAX(view->maxx, width);
4428 if (view->focussed && nprinted == s->selected_line - 1)
4429 wstandout(view->window);
4431 if (blame->nlines > 0) {
4432 blame_line = &blame->lines[lineno - 1];
4433 if (blame_line->annotated && prev_id &&
4434 got_object_id_cmp(prev_id, blame_line->id) == 0 &&
4436 nprinted == s->selected_line - 1)) {
4437 waddstr(view->window, " ");
4438 } else if (blame_line->annotated) {
4440 err = got_object_id_str(&id_str,
4446 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
4448 wattr_on(view->window,
4449 COLOR_PAIR(tc->colorpair), NULL);
4450 wprintw(view->window, "%.8s", id_str);
4452 wattr_off(view->window,
4453 COLOR_PAIR(tc->colorpair), NULL);
4455 prev_id = blame_line->id;
4457 waddstr(view->window, "........");
4461 waddstr(view->window, "........");
4465 if (view->focussed && nprinted == s->selected_line - 1)
4466 wstandend(view->window);
4467 waddstr(view->window, " ");
4469 if (view->ncols <= 9) {
4471 } else if (s->first_displayed_line + nprinted ==
4473 regmatch->rm_so >= 0 && regmatch->rm_so < regmatch->rm_eo) {
4474 err = add_matched_line(&width, line, view->ncols - 9, 9,
4475 view->window, view->x, regmatch);
4483 err = format_line(&wline, &width, &skip, line,
4484 view->x, view->ncols - 9, 9, 1);
4489 waddwstr(view->window, &wline[skip]);
4495 if (width <= view->ncols - 1)
4496 waddch(view->window, '\n');
4497 if (++nprinted == 1)
4498 s->first_displayed_line = lineno;
4501 s->last_displayed_line = lineno;
4508 static const struct got_error *
4509 blame_cb(void *arg, int nlines, int lineno,
4510 struct got_commit_object *commit, struct got_object_id *id)
4512 const struct got_error *err = NULL;
4513 struct tog_blame_cb_args *a = arg;
4514 struct tog_blame_line *line;
4517 if (nlines != a->nlines ||
4518 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4519 return got_error(GOT_ERR_RANGE);
4521 errcode = pthread_mutex_lock(&tog_mutex);
4523 return got_error_set_errno(errcode, "pthread_mutex_lock");
4525 if (*a->quit) { /* user has quit the blame view */
4526 err = got_error(GOT_ERR_ITER_COMPLETED);
4531 goto done; /* no change in this commit */
4533 line = &a->lines[lineno - 1];
4534 if (line->annotated)
4537 line->id = got_object_id_dup(id);
4538 if (line->id == NULL) {
4539 err = got_error_from_errno("got_object_id_dup");
4542 line->annotated = 1;
4544 errcode = pthread_mutex_unlock(&tog_mutex);
4546 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4551 blame_thread(void *arg)
4553 const struct got_error *err, *close_err;
4554 struct tog_blame_thread_args *ta = arg;
4555 struct tog_blame_cb_args *a = ta->cb_args;
4558 err = block_signals_used_by_main_thread();
4562 err = got_blame(ta->path, a->commit_id, ta->repo,
4563 blame_cb, ta->cb_args, ta->cancel_cb, ta->cancel_arg);
4564 if (err && err->code == GOT_ERR_CANCELLED)
4567 errcode = pthread_mutex_lock(&tog_mutex);
4569 return (void *)got_error_set_errno(errcode,
4570 "pthread_mutex_lock");
4572 close_err = got_repo_close(ta->repo);
4578 errcode = pthread_mutex_unlock(&tog_mutex);
4579 if (errcode && err == NULL)
4580 err = got_error_set_errno(errcode, "pthread_mutex_unlock");
4585 static struct got_object_id *
4586 get_selected_commit_id(struct tog_blame_line *lines, int nlines,
4587 int first_displayed_line, int selected_line)
4589 struct tog_blame_line *line;
4594 line = &lines[first_displayed_line - 1 + selected_line - 1];
4595 if (!line->annotated)
4601 static const struct got_error *
4602 stop_blame(struct tog_blame *blame)
4604 const struct got_error *err = NULL;
4607 if (blame->thread) {
4609 errcode = pthread_mutex_unlock(&tog_mutex);
4611 return got_error_set_errno(errcode,
4612 "pthread_mutex_unlock");
4613 errcode = pthread_join(blame->thread, (void **)&err);
4615 return got_error_set_errno(errcode, "pthread_join");
4616 errcode = pthread_mutex_lock(&tog_mutex);
4618 return got_error_set_errno(errcode,
4619 "pthread_mutex_lock");
4620 if (err && err->code == GOT_ERR_ITER_COMPLETED)
4622 blame->thread = 0; //NULL;
4624 if (blame->thread_args.repo) {
4625 const struct got_error *close_err;
4626 close_err = got_repo_close(blame->thread_args.repo);
4629 blame->thread_args.repo = NULL;
4632 if (fclose(blame->f) == EOF && err == NULL)
4633 err = got_error_from_errno("fclose");
4637 for (i = 0; i < blame->nlines; i++)
4638 free(blame->lines[i].id);
4640 blame->lines = NULL;
4642 free(blame->cb_args.commit_id);
4643 blame->cb_args.commit_id = NULL;
4644 if (blame->pack_fds) {
4645 const struct got_error *pack_err =
4646 got_repo_pack_fds_close(blame->pack_fds);
4649 blame->pack_fds = NULL;
4654 static const struct got_error *
4655 cancel_blame_view(void *arg)
4657 const struct got_error *err = NULL;
4661 errcode = pthread_mutex_lock(&tog_mutex);
4663 return got_error_set_errno(errcode,
4664 "pthread_mutex_unlock");
4667 err = got_error(GOT_ERR_CANCELLED);
4669 errcode = pthread_mutex_unlock(&tog_mutex);
4671 return got_error_set_errno(errcode,
4672 "pthread_mutex_lock");
4677 static const struct got_error *
4678 run_blame(struct tog_view *view)
4680 struct tog_blame_view_state *s = &view->state.blame;
4681 struct tog_blame *blame = &s->blame;
4682 const struct got_error *err = NULL;
4683 struct got_commit_object *commit = NULL;
4684 struct got_blob_object *blob = NULL;
4685 struct got_repository *thread_repo = NULL;
4686 struct got_object_id *obj_id = NULL;
4688 int *pack_fds = NULL;
4690 err = got_object_open_as_commit(&commit, s->repo,
4691 &s->blamed_commit->id);
4695 err = got_object_id_by_path(&obj_id, s->repo, commit, s->path);
4699 err = got_object_get_type(&obj_type, s->repo, obj_id);
4703 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4704 err = got_error(GOT_ERR_OBJ_TYPE);
4708 err = got_object_open_as_blob(&blob, s->repo, obj_id, 8192);
4711 blame->f = got_opentemp();
4712 if (blame->f == NULL) {
4713 err = got_error_from_errno("got_opentemp");
4716 err = got_object_blob_dump_to_file(&blame->filesize, &blame->nlines,
4717 &blame->line_offsets, blame->f, blob);
4720 if (blame->nlines == 0) {
4721 s->blame_complete = 1;
4725 /* Don't include \n at EOF in the blame line count. */
4726 if (blame->line_offsets[blame->nlines - 1] == blame->filesize)
4729 blame->lines = calloc(blame->nlines, sizeof(*blame->lines));
4730 if (blame->lines == NULL) {
4731 err = got_error_from_errno("calloc");
4735 err = got_repo_pack_fds_open(&pack_fds);
4738 err = got_repo_open(&thread_repo, got_repo_get_path(s->repo), NULL,
4743 blame->pack_fds = pack_fds;
4744 blame->cb_args.view = view;
4745 blame->cb_args.lines = blame->lines;
4746 blame->cb_args.nlines = blame->nlines;
4747 blame->cb_args.commit_id = got_object_id_dup(&s->blamed_commit->id);
4748 if (blame->cb_args.commit_id == NULL) {
4749 err = got_error_from_errno("got_object_id_dup");
4752 blame->cb_args.quit = &s->done;
4754 blame->thread_args.path = s->path;
4755 blame->thread_args.repo = thread_repo;
4756 blame->thread_args.cb_args = &blame->cb_args;
4757 blame->thread_args.complete = &s->blame_complete;
4758 blame->thread_args.cancel_cb = cancel_blame_view;
4759 blame->thread_args.cancel_arg = &s->done;
4760 s->blame_complete = 0;
4762 if (s->first_displayed_line + view->nlines - 1 > blame->nlines) {
4763 s->first_displayed_line = 1;
4764 s->last_displayed_line = view->nlines;
4765 s->selected_line = 1;
4767 s->matched_line = 0;
4771 got_object_commit_close(commit);
4773 got_object_blob_close(blob);
4780 static const struct got_error *
4781 open_blame_view(struct tog_view *view, char *path,
4782 struct got_object_id *commit_id, struct got_repository *repo)
4784 const struct got_error *err = NULL;
4785 struct tog_blame_view_state *s = &view->state.blame;
4787 STAILQ_INIT(&s->blamed_commits);
4789 s->path = strdup(path);
4790 if (s->path == NULL)
4791 return got_error_from_errno("strdup");
4793 err = got_object_qid_alloc(&s->blamed_commit, commit_id);
4799 STAILQ_INSERT_HEAD(&s->blamed_commits, s->blamed_commit, entry);
4800 s->first_displayed_line = 1;
4801 s->last_displayed_line = view->nlines;
4802 s->selected_line = 1;
4803 s->blame_complete = 0;
4805 s->commit_id = commit_id;
4806 memset(&s->blame, 0, sizeof(s->blame));
4808 STAILQ_INIT(&s->colors);
4809 if (has_colors() && getenv("TOG_COLORS") != NULL) {
4810 err = add_color(&s->colors, "^", TOG_COLOR_COMMIT,
4811 get_color_value("TOG_COLOR_COMMIT"));
4816 view->show = show_blame_view;
4817 view->input = input_blame_view;
4818 view->close = close_blame_view;
4819 view->search_start = search_start_blame_view;
4820 view->search_next = search_next_blame_view;
4822 return run_blame(view);
4825 static const struct got_error *
4826 close_blame_view(struct tog_view *view)
4828 const struct got_error *err = NULL;
4829 struct tog_blame_view_state *s = &view->state.blame;
4831 if (s->blame.thread)
4832 err = stop_blame(&s->blame);
4834 while (!STAILQ_EMPTY(&s->blamed_commits)) {
4835 struct got_object_qid *blamed_commit;
4836 blamed_commit = STAILQ_FIRST(&s->blamed_commits);
4837 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
4838 got_object_qid_free(blamed_commit);
4842 free_colors(&s->colors);
4846 static const struct got_error *
4847 search_start_blame_view(struct tog_view *view)
4849 struct tog_blame_view_state *s = &view->state.blame;
4851 s->matched_line = 0;
4855 static const struct got_error *
4856 search_next_blame_view(struct tog_view *view)
4858 struct tog_blame_view_state *s = &view->state.blame;
4859 const struct got_error *err = NULL;
4862 size_t linesize = 0;
4865 if (!view->searching) {
4866 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4870 if (s->matched_line) {
4871 if (view->searching == TOG_SEARCH_FORWARD)
4872 lineno = s->matched_line + 1;
4874 lineno = s->matched_line - 1;
4876 lineno = s->first_displayed_line - 1 + s->selected_line;
4881 if (lineno <= 0 || lineno > s->blame.nlines) {
4882 if (s->matched_line == 0) {
4883 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4887 if (view->searching == TOG_SEARCH_FORWARD)
4890 lineno = s->blame.nlines;
4893 offset = s->blame.line_offsets[lineno - 1];
4894 if (fseeko(s->blame.f, offset, SEEK_SET) != 0) {
4896 return got_error_from_errno("fseeko");
4898 linelen = getline(&line, &linesize, s->blame.f);
4899 if (linelen != -1) {
4901 err = expand_tab(&exstr, line);
4904 if (match_line(exstr, &view->regex, 1,
4906 view->search_next_done = TOG_SEARCH_HAVE_MORE;
4907 s->matched_line = lineno;
4913 if (view->searching == TOG_SEARCH_FORWARD)
4920 if (s->matched_line) {
4921 s->first_displayed_line = s->matched_line;
4922 s->selected_line = 1;
4928 static const struct got_error *
4929 show_blame_view(struct tog_view *view)
4931 const struct got_error *err = NULL;
4932 struct tog_blame_view_state *s = &view->state.blame;
4935 if (s->blame.thread == 0 && !s->blame_complete) {
4936 errcode = pthread_create(&s->blame.thread, NULL, blame_thread,
4937 &s->blame.thread_args);
4939 return got_error_set_errno(errcode, "pthread_create");
4941 halfdelay(1); /* fast refresh while annotating */
4944 if (s->blame_complete)
4945 halfdelay(10); /* disable fast refresh */
4947 err = draw_blame(view);
4953 static const struct got_error *
4954 input_blame_view(struct tog_view **new_view, struct tog_view *view, int ch)
4956 const struct got_error *err = NULL, *thread_err = NULL;
4957 struct tog_view *diff_view;
4958 struct tog_blame_view_state *s = &view->state.blame;
4959 int begin_x = 0, nscroll = view->nlines - 2;
4966 view->x = MAX(view->maxx - view->ncols / 3, 0);
4970 if (view->x + view->ncols / 3 < view->maxx)
4971 view->x += 2; /* move two columns right */
4975 view->x -= MIN(view->x, 2); /* move two columns back */
4982 s->selected_line = 1;
4983 s->first_displayed_line = 1;
4987 if (s->blame.nlines < view->nlines - 2) {
4988 s->selected_line = s->blame.nlines;
4989 s->first_displayed_line = 1;
4991 s->selected_line = view->nlines - 2;
4992 s->first_displayed_line = s->blame.nlines -
4999 if (s->selected_line > 1)
5001 else if (s->selected_line == 1 &&
5002 s->first_displayed_line > 1)
5003 s->first_displayed_line--;
5011 if (s->first_displayed_line == 1) {
5012 s->selected_line = MAX(1, s->selected_line - nscroll);
5015 if (s->first_displayed_line > nscroll)
5016 s->first_displayed_line -= nscroll;
5018 s->first_displayed_line = 1;
5023 if (s->selected_line < view->nlines - 2 &&
5024 s->first_displayed_line +
5025 s->selected_line <= s->blame.nlines)
5027 else if (s->last_displayed_line <
5029 s->first_displayed_line++;
5033 struct got_object_id *id = NULL;
5034 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5035 s->first_displayed_line, s->selected_line);
5039 struct got_commit_object *commit, *pcommit;
5040 struct got_object_qid *pid;
5041 struct got_object_id *blob_id = NULL;
5043 err = got_object_open_as_commit(&commit,
5048 got_object_commit_get_parent_ids(commit));
5050 got_object_commit_close(commit);
5053 /* Check if path history ends here. */
5054 err = got_object_open_as_commit(&pcommit,
5058 err = got_object_id_by_path(&blob_id, s->repo,
5060 got_object_commit_close(pcommit);
5062 if (err->code == GOT_ERR_NO_TREE_ENTRY)
5064 got_object_commit_close(commit);
5067 err = got_object_get_type(&obj_type, s->repo,
5070 /* Can't blame non-blob type objects. */
5071 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5072 got_object_commit_close(commit);
5075 err = got_object_qid_alloc(&s->blamed_commit,
5077 got_object_commit_close(commit);
5079 if (got_object_id_cmp(id,
5080 &s->blamed_commit->id) == 0)
5082 err = got_object_qid_alloc(&s->blamed_commit,
5088 thread_err = stop_blame(&s->blame);
5092 STAILQ_INSERT_HEAD(&s->blamed_commits,
5093 s->blamed_commit, entry);
5094 err = run_blame(view);
5100 struct got_object_qid *first;
5101 first = STAILQ_FIRST(&s->blamed_commits);
5102 if (!got_object_id_cmp(&first->id, s->commit_id))
5105 thread_err = stop_blame(&s->blame);
5109 STAILQ_REMOVE_HEAD(&s->blamed_commits, entry);
5110 got_object_qid_free(s->blamed_commit);
5112 STAILQ_FIRST(&s->blamed_commits);
5113 err = run_blame(view);
5120 struct got_object_id *id = NULL;
5121 struct got_object_qid *pid;
5122 struct got_commit_object *commit = NULL;
5123 id = get_selected_commit_id(s->blame.lines, s->blame.nlines,
5124 s->first_displayed_line, s->selected_line);
5127 err = got_object_open_as_commit(&commit, s->repo, id);
5131 got_object_commit_get_parent_ids(commit));
5132 if (view_is_parent_view(view))
5133 begin_x = view_split_begin_x(view->begin_x);
5134 diff_view = view_open(0, 0, 0, begin_x, TOG_VIEW_DIFF);
5135 if (diff_view == NULL) {
5136 got_object_commit_close(commit);
5137 err = got_error_from_errno("view_open");
5140 err = open_diff_view(diff_view, pid ? &pid->id : NULL,
5141 id, NULL, NULL, 3, 0, 0, NULL, s->repo);
5142 got_object_commit_close(commit);
5144 view_close(diff_view);
5148 diff_view->focussed = 1;
5149 if (view_is_parent_view(view)) {
5150 err = view_close_child(view);
5153 err = view_set_child(view, diff_view);
5156 view->focus_child = 1;
5158 *new_view = diff_view;
5170 if (s->last_displayed_line >= s->blame.nlines &&
5171 s->selected_line >= MIN(s->blame.nlines,
5172 view->nlines - 2)) {
5175 if (s->last_displayed_line >= s->blame.nlines &&
5176 s->selected_line < view->nlines - 2) {
5178 MIN(nscroll, s->last_displayed_line -
5179 s->first_displayed_line - s->selected_line + 1);
5181 if (s->last_displayed_line + nscroll <= s->blame.nlines)
5182 s->first_displayed_line += nscroll;
5184 s->first_displayed_line =
5185 s->blame.nlines - (view->nlines - 3);
5188 if (s->selected_line > view->nlines - 2) {
5189 s->selected_line = MIN(s->blame.nlines,
5196 return thread_err ? thread_err : err;
5199 static const struct got_error *
5200 cmd_blame(int argc, char *argv[])
5202 const struct got_error *error;
5203 struct got_repository *repo = NULL;
5204 struct got_worktree *worktree = NULL;
5205 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5206 char *link_target = NULL;
5207 struct got_object_id *commit_id = NULL;
5208 struct got_commit_object *commit = NULL;
5209 char *commit_id_str = NULL;
5211 struct tog_view *view;
5212 int *pack_fds = NULL;
5214 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5217 commit_id_str = optarg;
5220 repo_path = realpath(optarg, NULL);
5221 if (repo_path == NULL)
5222 return got_error_from_errno2("realpath",
5237 error = got_repo_pack_fds_open(&pack_fds);
5241 if (repo_path == NULL) {
5242 cwd = getcwd(NULL, 0);
5244 return got_error_from_errno("getcwd");
5245 error = got_worktree_open(&worktree, cwd);
5246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5250 strdup(got_worktree_get_repo_path(worktree));
5252 repo_path = strdup(cwd);
5253 if (repo_path == NULL) {
5254 error = got_error_from_errno("strdup");
5259 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5263 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv, repo,
5270 error = apply_unveil(got_repo_get_path(repo), NULL);
5274 error = tog_load_refs(repo, 0);
5278 if (commit_id_str == NULL) {
5279 struct got_reference *head_ref;
5280 error = got_ref_open(&head_ref, repo, worktree ?
5281 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5284 error = got_ref_resolve(&commit_id, repo, head_ref);
5285 got_ref_close(head_ref);
5287 error = got_repo_match_object_id(&commit_id, NULL,
5288 commit_id_str, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
5293 view = view_open(0, 0, 0, 0, TOG_VIEW_BLAME);
5295 error = got_error_from_errno("view_open");
5299 error = got_object_open_as_commit(&commit, repo, commit_id);
5303 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5308 error = open_blame_view(view, link_target ? link_target : in_repo_path,
5313 /* Release work tree lock. */
5314 got_worktree_close(worktree);
5317 error = view_loop(view);
5325 got_object_commit_close(commit);
5327 got_worktree_close(worktree);
5329 const struct got_error *close_err = got_repo_close(repo);
5334 const struct got_error *pack_err =
5335 got_repo_pack_fds_close(pack_fds);
5343 static const struct got_error *
5344 draw_tree_entries(struct tog_view *view, const char *parent_path)
5346 struct tog_tree_view_state *s = &view->state.tree;
5347 const struct got_error *err = NULL;
5348 struct got_tree_entry *te;
5350 struct tog_color *tc;
5351 int width, n, i, nentries;
5352 int limit = view->nlines;
5356 werase(view->window);
5361 err = format_line(&wline, &width, NULL, s->tree_label, 0, view->ncols,
5365 if (view_needs_focus_indication(view))
5366 wstandout(view->window);
5367 tc = get_color(&s->colors, TOG_COLOR_COMMIT);
5369 wattr_on(view->window,
5370 COLOR_PAIR(tc->colorpair), NULL);
5371 waddwstr(view->window, wline);
5373 wattr_off(view->window,
5374 COLOR_PAIR(tc->colorpair), NULL);
5375 if (view_needs_focus_indication(view))
5376 wstandend(view->window);
5379 if (width < view->ncols - 1)
5380 waddch(view->window, '\n');
5383 err = format_line(&wline, &width, NULL, parent_path, 0, view->ncols,
5387 waddwstr(view->window, wline);
5390 if (width < view->ncols - 1)
5391 waddch(view->window, '\n');
5394 waddch(view->window, '\n');
5398 if (s->first_displayed_entry == NULL) {
5399 te = got_object_tree_get_first_entry(s->tree);
5400 if (s->selected == 0) {
5402 wstandout(view->window);
5403 s->selected_entry = NULL;
5405 waddstr(view->window, " ..\n"); /* parent directory */
5406 if (s->selected == 0 && view->focussed)
5407 wstandend(view->window);
5414 te = s->first_displayed_entry;
5417 nentries = got_object_tree_get_nentries(s->tree);
5418 for (i = got_tree_entry_get_index(te); i < nentries; i++) {
5419 char *line = NULL, *id_str = NULL, *link_target = NULL;
5420 const char *modestr = "";
5423 te = got_object_tree_get_entry(s->tree, i);
5424 mode = got_tree_entry_get_mode(te);
5427 err = got_object_id_str(&id_str,
5428 got_tree_entry_get_id(te));
5430 return got_error_from_errno(
5431 "got_object_id_str");
5433 if (got_object_tree_entry_is_submodule(te))
5435 else if (S_ISLNK(mode)) {
5438 err = got_tree_entry_get_symlink_target(&link_target,
5444 for (i = 0; i < strlen(link_target); i++) {
5445 if (!isprint((unsigned char)link_target[i]))
5446 link_target[i] = '?';
5450 else if (S_ISDIR(mode))
5452 else if (mode & S_IXUSR)
5454 if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
5455 got_tree_entry_get_name(te), modestr,
5456 link_target ? " -> ": "",
5457 link_target ? link_target : "") == -1) {
5460 return got_error_from_errno("asprintf");
5464 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
5470 if (n == s->selected) {
5472 wstandout(view->window);
5473 s->selected_entry = te;
5475 tc = match_color(&s->colors, line);
5477 wattr_on(view->window,
5478 COLOR_PAIR(tc->colorpair), NULL);
5479 waddwstr(view->window, wline);
5481 wattr_off(view->window,
5482 COLOR_PAIR(tc->colorpair), NULL);
5483 if (width < view->ncols - 1)
5484 waddch(view->window, '\n');
5485 if (n == s->selected && view->focussed)
5486 wstandend(view->window);
5492 s->last_displayed_entry = te;
5501 tree_scroll_up(struct tog_tree_view_state *s, int maxscroll)
5503 struct got_tree_entry *te;
5504 int isroot = s->tree == s->root;
5507 if (s->first_displayed_entry == NULL)
5510 te = got_tree_entry_get_prev(s->tree, s->first_displayed_entry);
5511 while (i++ < maxscroll) {
5514 s->first_displayed_entry = NULL;
5517 s->first_displayed_entry = te;
5518 te = got_tree_entry_get_prev(s->tree, te);
5523 tree_scroll_down(struct tog_tree_view_state *s, int maxscroll)
5525 struct got_tree_entry *next, *last;
5528 if (s->first_displayed_entry)
5529 next = got_tree_entry_get_next(s->tree,
5530 s->first_displayed_entry);
5532 next = got_object_tree_get_first_entry(s->tree);
5534 last = s->last_displayed_entry;
5535 while (next && last && n++ < maxscroll) {
5536 last = got_tree_entry_get_next(s->tree, last);
5538 s->first_displayed_entry = next;
5539 next = got_tree_entry_get_next(s->tree, next);
5544 static const struct got_error *
5545 tree_entry_path(char **path, struct tog_parent_trees *parents,
5546 struct got_tree_entry *te)
5548 const struct got_error *err = NULL;
5549 struct tog_parent_tree *pt;
5550 size_t len = 2; /* for leading slash and NUL */
5552 TAILQ_FOREACH(pt, parents, entry)
5553 len += strlen(got_tree_entry_get_name(pt->selected_entry))
5556 len += strlen(got_tree_entry_get_name(te));
5558 *path = calloc(1, len);
5560 return got_error_from_errno("calloc");
5563 pt = TAILQ_LAST(parents, tog_parent_trees);
5565 const char *name = got_tree_entry_get_name(pt->selected_entry);
5566 if (strlcat(*path, name, len) >= len) {
5567 err = got_error(GOT_ERR_NO_SPACE);
5570 if (strlcat(*path, "/", len) >= len) {
5571 err = got_error(GOT_ERR_NO_SPACE);
5574 pt = TAILQ_PREV(pt, tog_parent_trees, entry);
5577 if (strlcat(*path, got_tree_entry_get_name(te), len) >= len) {
5578 err = got_error(GOT_ERR_NO_SPACE);
5590 static const struct got_error *
5591 blame_tree_entry(struct tog_view **new_view, int begin_x,
5592 struct got_tree_entry *te, struct tog_parent_trees *parents,
5593 struct got_object_id *commit_id, struct got_repository *repo)
5595 const struct got_error *err = NULL;
5597 struct tog_view *blame_view;
5601 err = tree_entry_path(&path, parents, te);
5605 blame_view = view_open(0, 0, 0, begin_x, TOG_VIEW_BLAME);
5606 if (blame_view == NULL) {
5607 err = got_error_from_errno("view_open");
5611 err = open_blame_view(blame_view, path, commit_id, repo);
5613 if (err->code == GOT_ERR_CANCELLED)
5615 view_close(blame_view);
5617 *new_view = blame_view;
5623 static const struct got_error *
5624 log_selected_tree_entry(struct tog_view **new_view, int begin_x,
5625 struct tog_tree_view_state *s)
5627 struct tog_view *log_view;
5628 const struct got_error *err = NULL;
5633 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
5634 if (log_view == NULL)
5635 return got_error_from_errno("view_open");
5637 err = tree_entry_path(&path, &s->parents, s->selected_entry);
5641 err = open_log_view(log_view, s->commit_id, s->repo, s->head_ref_name,
5644 view_close(log_view);
5646 *new_view = log_view;
5651 static const struct got_error *
5652 open_tree_view(struct tog_view *view, struct got_object_id *commit_id,
5653 const char *head_ref_name, struct got_repository *repo)
5655 const struct got_error *err = NULL;
5656 char *commit_id_str = NULL;
5657 struct tog_tree_view_state *s = &view->state.tree;
5658 struct got_commit_object *commit = NULL;
5660 TAILQ_INIT(&s->parents);
5661 STAILQ_INIT(&s->colors);
5663 s->commit_id = got_object_id_dup(commit_id);
5664 if (s->commit_id == NULL)
5665 return got_error_from_errno("got_object_id_dup");
5667 err = got_object_open_as_commit(&commit, repo, commit_id);
5672 * The root is opened here and will be closed when the view is closed.
5673 * Any visited subtrees and their path-wise parents are opened and
5676 err = got_object_open_as_tree(&s->root, repo,
5677 got_object_commit_get_tree_id(commit));
5682 err = got_object_id_str(&commit_id_str, commit_id);
5686 if (asprintf(&s->tree_label, "commit %s", commit_id_str) == -1) {
5687 err = got_error_from_errno("asprintf");
5691 s->first_displayed_entry = got_object_tree_get_entry(s->tree, 0);
5692 s->selected_entry = got_object_tree_get_entry(s->tree, 0);
5693 if (head_ref_name) {
5694 s->head_ref_name = strdup(head_ref_name);
5695 if (s->head_ref_name == NULL) {
5696 err = got_error_from_errno("strdup");
5702 if (has_colors() && getenv("TOG_COLORS") != NULL) {
5703 err = add_color(&s->colors, "\\$$",
5704 TOG_COLOR_TREE_SUBMODULE,
5705 get_color_value("TOG_COLOR_TREE_SUBMODULE"));
5708 err = add_color(&s->colors, "@$", TOG_COLOR_TREE_SYMLINK,
5709 get_color_value("TOG_COLOR_TREE_SYMLINK"));
5712 err = add_color(&s->colors, "/$",
5713 TOG_COLOR_TREE_DIRECTORY,
5714 get_color_value("TOG_COLOR_TREE_DIRECTORY"));
5718 err = add_color(&s->colors, "\\*$",
5719 TOG_COLOR_TREE_EXECUTABLE,
5720 get_color_value("TOG_COLOR_TREE_EXECUTABLE"));
5724 err = add_color(&s->colors, "^$", TOG_COLOR_COMMIT,
5725 get_color_value("TOG_COLOR_COMMIT"));
5730 view->show = show_tree_view;
5731 view->input = input_tree_view;
5732 view->close = close_tree_view;
5733 view->search_start = search_start_tree_view;
5734 view->search_next = search_next_tree_view;
5736 free(commit_id_str);
5738 got_object_commit_close(commit);
5740 close_tree_view(view);
5744 static const struct got_error *
5745 close_tree_view(struct tog_view *view)
5747 struct tog_tree_view_state *s = &view->state.tree;
5749 free_colors(&s->colors);
5750 free(s->tree_label);
5751 s->tree_label = NULL;
5753 s->commit_id = NULL;
5754 free(s->head_ref_name);
5755 s->head_ref_name = NULL;
5756 while (!TAILQ_EMPTY(&s->parents)) {
5757 struct tog_parent_tree *parent;
5758 parent = TAILQ_FIRST(&s->parents);
5759 TAILQ_REMOVE(&s->parents, parent, entry);
5760 if (parent->tree != s->root)
5761 got_object_tree_close(parent->tree);
5765 if (s->tree != NULL && s->tree != s->root)
5766 got_object_tree_close(s->tree);
5768 got_object_tree_close(s->root);
5772 static const struct got_error *
5773 search_start_tree_view(struct tog_view *view)
5775 struct tog_tree_view_state *s = &view->state.tree;
5777 s->matched_entry = NULL;
5782 match_tree_entry(struct got_tree_entry *te, regex_t *regex)
5784 regmatch_t regmatch;
5786 return regexec(regex, got_tree_entry_get_name(te), 1, ®match,
5790 static const struct got_error *
5791 search_next_tree_view(struct tog_view *view)
5793 struct tog_tree_view_state *s = &view->state.tree;
5794 struct got_tree_entry *te = NULL;
5796 if (!view->searching) {
5797 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5801 if (s->matched_entry) {
5802 if (view->searching == TOG_SEARCH_FORWARD) {
5803 if (s->selected_entry)
5804 te = got_tree_entry_get_next(s->tree,
5807 te = got_object_tree_get_first_entry(s->tree);
5809 if (s->selected_entry == NULL)
5810 te = got_object_tree_get_last_entry(s->tree);
5812 te = got_tree_entry_get_prev(s->tree,
5816 if (s->selected_entry)
5817 te = s->selected_entry;
5818 else if (view->searching == TOG_SEARCH_FORWARD)
5819 te = got_object_tree_get_first_entry(s->tree);
5821 te = got_object_tree_get_last_entry(s->tree);
5826 if (s->matched_entry == NULL) {
5827 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5830 if (view->searching == TOG_SEARCH_FORWARD)
5831 te = got_object_tree_get_first_entry(s->tree);
5833 te = got_object_tree_get_last_entry(s->tree);
5836 if (match_tree_entry(te, &view->regex)) {
5837 view->search_next_done = TOG_SEARCH_HAVE_MORE;
5838 s->matched_entry = te;
5842 if (view->searching == TOG_SEARCH_FORWARD)
5843 te = got_tree_entry_get_next(s->tree, te);
5845 te = got_tree_entry_get_prev(s->tree, te);
5848 if (s->matched_entry) {
5849 s->first_displayed_entry = s->matched_entry;
5856 static const struct got_error *
5857 show_tree_view(struct tog_view *view)
5859 const struct got_error *err = NULL;
5860 struct tog_tree_view_state *s = &view->state.tree;
5863 err = tree_entry_path(&parent_path, &s->parents, NULL);
5867 err = draw_tree_entries(view, parent_path);
5874 static const struct got_error *
5875 input_tree_view(struct tog_view **new_view, struct tog_view *view, int ch)
5877 const struct got_error *err = NULL;
5878 struct tog_tree_view_state *s = &view->state.tree;
5879 struct tog_view *log_view, *ref_view;
5880 struct got_tree_entry *te;
5881 int begin_x = 0, n, nscroll = view->nlines - 3;
5885 s->show_ids = !s->show_ids;
5888 if (!s->selected_entry)
5890 if (view_is_parent_view(view))
5891 begin_x = view_split_begin_x(view->begin_x);
5892 err = log_selected_tree_entry(&log_view, begin_x, s);
5894 log_view->focussed = 1;
5895 if (view_is_parent_view(view)) {
5896 err = view_close_child(view);
5899 err = view_set_child(view, log_view);
5902 view->focus_child = 1;
5904 *new_view = log_view;
5907 if (view_is_parent_view(view))
5908 begin_x = view_split_begin_x(view->begin_x);
5909 ref_view = view_open(view->nlines, view->ncols,
5910 view->begin_y, begin_x, TOG_VIEW_REF);
5911 if (ref_view == NULL)
5912 return got_error_from_errno("view_open");
5913 err = open_ref_view(ref_view, s->repo);
5915 view_close(ref_view);
5919 ref_view->focussed = 1;
5920 if (view_is_parent_view(view)) {
5921 err = view_close_child(view);
5924 err = view_set_child(view, ref_view);
5927 view->focus_child = 1;
5929 *new_view = ref_view;
5934 if (s->tree == s->root)
5935 s->first_displayed_entry =
5936 got_object_tree_get_first_entry(s->tree);
5938 s->first_displayed_entry = NULL;
5943 te = got_object_tree_get_last_entry(s->tree);
5944 for (n = 0; n < view->nlines - 3; n++) {
5946 if(s->tree != s->root) {
5947 s->first_displayed_entry = NULL;
5952 s->first_displayed_entry = te;
5953 te = got_tree_entry_get_prev(s->tree, te);
5956 s->selected = n - 1;
5961 if (s->selected > 0) {
5965 tree_scroll_up(s, 1);
5973 if (s->tree == s->root) {
5974 if (got_object_tree_get_first_entry(s->tree) ==
5975 s->first_displayed_entry)
5976 s->selected -= MIN(s->selected, nscroll);
5978 if (s->first_displayed_entry == NULL)
5979 s->selected -= MIN(s->selected, nscroll);
5981 tree_scroll_up(s, MAX(0, nscroll));
5986 if (s->selected < s->ndisplayed - 1) {
5990 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
5992 /* can't scroll any further */
5994 tree_scroll_down(s, 1);
6002 if (got_tree_entry_get_next(s->tree, s->last_displayed_entry)
6004 /* can't scroll any further; move cursor down */
6005 if (s->selected < s->ndisplayed - 1)
6006 s->selected += MIN(nscroll,
6007 s->ndisplayed - s->selected - 1);
6010 tree_scroll_down(s, nscroll);
6015 if (s->selected_entry == NULL || ch == KEY_BACKSPACE) {
6016 struct tog_parent_tree *parent;
6017 /* user selected '..' */
6018 if (s->tree == s->root)
6020 parent = TAILQ_FIRST(&s->parents);
6021 TAILQ_REMOVE(&s->parents, parent,
6023 got_object_tree_close(s->tree);
6024 s->tree = parent->tree;
6025 s->first_displayed_entry =
6026 parent->first_displayed_entry;
6028 parent->selected_entry;
6029 s->selected = parent->selected;
6031 } else if (S_ISDIR(got_tree_entry_get_mode(
6032 s->selected_entry))) {
6033 struct got_tree_object *subtree;
6034 err = got_object_open_as_tree(&subtree, s->repo,
6035 got_tree_entry_get_id(s->selected_entry));
6038 err = tree_view_visit_subtree(s, subtree);
6040 got_object_tree_close(subtree);
6043 } else if (S_ISREG(got_tree_entry_get_mode(
6044 s->selected_entry))) {
6045 struct tog_view *blame_view;
6046 int begin_x = view_is_parent_view(view) ?
6047 view_split_begin_x(view->begin_x) : 0;
6049 err = blame_tree_entry(&blame_view, begin_x,
6050 s->selected_entry, &s->parents,
6051 s->commit_id, s->repo);
6055 blame_view->focussed = 1;
6056 if (view_is_parent_view(view)) {
6057 err = view_close_child(view);
6060 err = view_set_child(view, blame_view);
6063 view->focus_child = 1;
6065 *new_view = blame_view;
6069 if (view->nlines >= 4 && s->selected >= view->nlines - 3)
6070 s->selected = view->nlines - 4;
6084 "usage: %s tree [-c commit] [-r repository-path] [path]\n",
6089 static const struct got_error *
6090 cmd_tree(int argc, char *argv[])
6092 const struct got_error *error;
6093 struct got_repository *repo = NULL;
6094 struct got_worktree *worktree = NULL;
6095 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6096 struct got_object_id *commit_id = NULL;
6097 struct got_commit_object *commit = NULL;
6098 const char *commit_id_arg = NULL;
6100 struct got_reference *ref = NULL;
6101 const char *head_ref_name = NULL;
6103 struct tog_view *view;
6104 int *pack_fds = NULL;
6106 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
6109 commit_id_arg = optarg;
6112 repo_path = realpath(optarg, NULL);
6113 if (repo_path == NULL)
6114 return got_error_from_errno2("realpath",
6129 error = got_repo_pack_fds_open(&pack_fds);
6133 if (repo_path == NULL) {
6134 cwd = getcwd(NULL, 0);
6136 return got_error_from_errno("getcwd");
6137 error = got_worktree_open(&worktree, cwd);
6138 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6142 strdup(got_worktree_get_repo_path(worktree));
6144 repo_path = strdup(cwd);
6145 if (repo_path == NULL) {
6146 error = got_error_from_errno("strdup");
6151 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6155 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
6162 error = apply_unveil(got_repo_get_path(repo), NULL);
6166 error = tog_load_refs(repo, 0);
6170 if (commit_id_arg == NULL) {
6171 error = got_repo_match_object_id(&commit_id, &label,
6172 worktree ? got_worktree_get_head_ref_name(worktree) :
6173 GOT_REF_HEAD, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6176 head_ref_name = label;
6178 error = got_ref_open(&ref, repo, commit_id_arg, 0);
6180 head_ref_name = got_ref_get_name(ref);
6181 else if (error->code != GOT_ERR_NOT_REF)
6183 error = got_repo_match_object_id(&commit_id, NULL,
6184 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
6189 error = got_object_open_as_commit(&commit, repo, commit_id);
6193 view = view_open(0, 0, 0, 0, TOG_VIEW_TREE);
6195 error = got_error_from_errno("view_open");
6198 error = open_tree_view(view, commit_id, head_ref_name, repo);
6201 if (!got_path_is_root_dir(in_repo_path)) {
6202 error = tree_view_walk_path(&view->state.tree, commit,
6209 /* Release work tree lock. */
6210 got_worktree_close(worktree);
6213 error = view_loop(view);
6222 const struct got_error *close_err = got_repo_close(repo);
6227 const struct got_error *pack_err =
6228 got_repo_pack_fds_close(pack_fds);
6236 static const struct got_error *
6237 ref_view_load_refs(struct tog_ref_view_state *s)
6239 struct got_reflist_entry *sre;
6240 struct tog_reflist_entry *re;
6243 TAILQ_FOREACH(sre, &tog_refs, entry) {
6244 if (strncmp(got_ref_get_name(sre->ref),
6245 "refs/got/", 9) == 0 &&
6246 strncmp(got_ref_get_name(sre->ref),
6247 "refs/got/backup/", 16) != 0)
6250 re = malloc(sizeof(*re));
6252 return got_error_from_errno("malloc");
6254 re->ref = got_ref_dup(sre->ref);
6255 if (re->ref == NULL)
6256 return got_error_from_errno("got_ref_dup");
6257 re->idx = s->nrefs++;
6258 TAILQ_INSERT_TAIL(&s->refs, re, entry);
6261 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6266 ref_view_free_refs(struct tog_ref_view_state *s)
6268 struct tog_reflist_entry *re;
6270 while (!TAILQ_EMPTY(&s->refs)) {
6271 re = TAILQ_FIRST(&s->refs);
6272 TAILQ_REMOVE(&s->refs, re, entry);
6273 got_ref_close(re->ref);
6278 static const struct got_error *
6279 open_ref_view(struct tog_view *view, struct got_repository *repo)
6281 const struct got_error *err = NULL;
6282 struct tog_ref_view_state *s = &view->state.ref;
6284 s->selected_entry = 0;
6287 TAILQ_INIT(&s->refs);
6288 STAILQ_INIT(&s->colors);
6290 err = ref_view_load_refs(s);
6294 if (has_colors() && getenv("TOG_COLORS") != NULL) {
6295 err = add_color(&s->colors, "^refs/heads/",
6296 TOG_COLOR_REFS_HEADS,
6297 get_color_value("TOG_COLOR_REFS_HEADS"));
6301 err = add_color(&s->colors, "^refs/tags/",
6302 TOG_COLOR_REFS_TAGS,
6303 get_color_value("TOG_COLOR_REFS_TAGS"));
6307 err = add_color(&s->colors, "^refs/remotes/",
6308 TOG_COLOR_REFS_REMOTES,
6309 get_color_value("TOG_COLOR_REFS_REMOTES"));
6313 err = add_color(&s->colors, "^refs/got/backup/",
6314 TOG_COLOR_REFS_BACKUP,
6315 get_color_value("TOG_COLOR_REFS_BACKUP"));
6320 view->show = show_ref_view;
6321 view->input = input_ref_view;
6322 view->close = close_ref_view;
6323 view->search_start = search_start_ref_view;
6324 view->search_next = search_next_ref_view;
6327 free_colors(&s->colors);
6331 static const struct got_error *
6332 close_ref_view(struct tog_view *view)
6334 struct tog_ref_view_state *s = &view->state.ref;
6336 ref_view_free_refs(s);
6337 free_colors(&s->colors);
6342 static const struct got_error *
6343 resolve_reflist_entry(struct got_object_id **commit_id,
6344 struct tog_reflist_entry *re, struct got_repository *repo)
6346 const struct got_error *err = NULL;
6347 struct got_object_id *obj_id;
6348 struct got_tag_object *tag = NULL;
6353 err = got_ref_resolve(&obj_id, repo, re->ref);
6357 err = got_object_get_type(&obj_type, repo, obj_id);
6362 case GOT_OBJ_TYPE_COMMIT:
6363 *commit_id = obj_id;
6365 case GOT_OBJ_TYPE_TAG:
6366 err = got_object_open_as_tag(&tag, repo, obj_id);
6370 err = got_object_get_type(&obj_type, repo,
6371 got_object_tag_get_object_id(tag));
6374 if (obj_type != GOT_OBJ_TYPE_COMMIT) {
6375 err = got_error(GOT_ERR_OBJ_TYPE);
6378 *commit_id = got_object_id_dup(
6379 got_object_tag_get_object_id(tag));
6380 if (*commit_id == NULL) {
6381 err = got_error_from_errno("got_object_id_dup");
6386 err = got_error(GOT_ERR_OBJ_TYPE);
6392 got_object_tag_close(tag);
6400 static const struct got_error *
6401 log_ref_entry(struct tog_view **new_view, int begin_x,
6402 struct tog_reflist_entry *re, struct got_repository *repo)
6404 struct tog_view *log_view;
6405 const struct got_error *err = NULL;
6406 struct got_object_id *commit_id = NULL;
6410 err = resolve_reflist_entry(&commit_id, re, repo);
6412 if (err->code != GOT_ERR_OBJ_TYPE)
6418 log_view = view_open(0, 0, 0, begin_x, TOG_VIEW_LOG);
6419 if (log_view == NULL) {
6420 err = got_error_from_errno("view_open");
6424 err = open_log_view(log_view, commit_id, repo,
6425 got_ref_get_name(re->ref), "", 0);
6428 view_close(log_view);
6430 *new_view = log_view;
6436 ref_scroll_up(struct tog_ref_view_state *s, int maxscroll)
6438 struct tog_reflist_entry *re;
6441 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6444 re = TAILQ_PREV(s->first_displayed_entry, tog_reflist_head, entry);
6445 while (i++ < maxscroll) {
6448 s->first_displayed_entry = re;
6449 re = TAILQ_PREV(re, tog_reflist_head, entry);
6454 ref_scroll_down(struct tog_ref_view_state *s, int maxscroll)
6456 struct tog_reflist_entry *next, *last;
6459 if (s->first_displayed_entry)
6460 next = TAILQ_NEXT(s->first_displayed_entry, entry);
6462 next = TAILQ_FIRST(&s->refs);
6464 last = s->last_displayed_entry;
6465 while (next && last && n++ < maxscroll) {
6466 last = TAILQ_NEXT(last, entry);
6468 s->first_displayed_entry = next;
6469 next = TAILQ_NEXT(next, entry);
6474 static const struct got_error *
6475 search_start_ref_view(struct tog_view *view)
6477 struct tog_ref_view_state *s = &view->state.ref;
6479 s->matched_entry = NULL;
6484 match_reflist_entry(struct tog_reflist_entry *re, regex_t *regex)
6486 regmatch_t regmatch;
6488 return regexec(regex, got_ref_get_name(re->ref), 1, ®match,
6492 static const struct got_error *
6493 search_next_ref_view(struct tog_view *view)
6495 struct tog_ref_view_state *s = &view->state.ref;
6496 struct tog_reflist_entry *re = NULL;
6498 if (!view->searching) {
6499 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6503 if (s->matched_entry) {
6504 if (view->searching == TOG_SEARCH_FORWARD) {
6505 if (s->selected_entry)
6506 re = TAILQ_NEXT(s->selected_entry, entry);
6508 re = TAILQ_PREV(s->selected_entry,
6509 tog_reflist_head, entry);
6511 if (s->selected_entry == NULL)
6512 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6514 re = TAILQ_PREV(s->selected_entry,
6515 tog_reflist_head, entry);
6518 if (s->selected_entry)
6519 re = s->selected_entry;
6520 else if (view->searching == TOG_SEARCH_FORWARD)
6521 re = TAILQ_FIRST(&s->refs);
6523 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6528 if (s->matched_entry == NULL) {
6529 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6532 if (view->searching == TOG_SEARCH_FORWARD)
6533 re = TAILQ_FIRST(&s->refs);
6535 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6538 if (match_reflist_entry(re, &view->regex)) {
6539 view->search_next_done = TOG_SEARCH_HAVE_MORE;
6540 s->matched_entry = re;
6544 if (view->searching == TOG_SEARCH_FORWARD)
6545 re = TAILQ_NEXT(re, entry);
6547 re = TAILQ_PREV(re, tog_reflist_head, entry);
6550 if (s->matched_entry) {
6551 s->first_displayed_entry = s->matched_entry;
6558 static const struct got_error *
6559 show_ref_view(struct tog_view *view)
6561 const struct got_error *err = NULL;
6562 struct tog_ref_view_state *s = &view->state.ref;
6563 struct tog_reflist_entry *re;
6566 struct tog_color *tc;
6568 int limit = view->nlines;
6570 werase(view->window);
6577 re = s->first_displayed_entry;
6579 if (asprintf(&line, "references [%d/%d]", re->idx + s->selected + 1,
6581 return got_error_from_errno("asprintf");
6583 err = format_line(&wline, &width, NULL, line, 0, view->ncols, 0, 0);
6588 if (view_needs_focus_indication(view))
6589 wstandout(view->window);
6590 waddwstr(view->window, wline);
6591 if (view_needs_focus_indication(view))
6592 wstandend(view->window);
6597 if (width < view->ncols - 1)
6598 waddch(view->window, '\n');
6603 while (re && limit > 0) {
6605 char ymd[13]; /* YYYY-MM-DD + " " + NUL */
6608 struct got_commit_object *ci;
6609 struct got_tag_object *tag;
6610 struct got_object_id *id;
6614 err = got_ref_resolve(&id, s->repo, re->ref);
6617 err = got_object_open_as_tag(&tag, s->repo, id);
6619 if (err->code != GOT_ERR_OBJ_TYPE) {
6623 err = got_object_open_as_commit(&ci, s->repo,
6629 t = got_object_commit_get_committer_time(ci);
6630 got_object_commit_close(ci);
6632 t = got_object_tag_get_tagger_time(tag);
6633 got_object_tag_close(tag);
6636 if (gmtime_r(&t, &tm) == NULL)
6637 return got_error_from_errno("gmtime_r");
6638 if (strftime(ymd, sizeof(ymd), "%G-%m-%d ", &tm) == 0)
6639 return got_error(GOT_ERR_NO_SPACE);
6641 if (got_ref_is_symbolic(re->ref)) {
6642 if (asprintf(&line, "%s%s -> %s", s->show_date ?
6643 ymd : "", got_ref_get_name(re->ref),
6644 got_ref_get_symref_target(re->ref)) == -1)
6645 return got_error_from_errno("asprintf");
6646 } else if (s->show_ids) {
6647 struct got_object_id *id;
6649 err = got_ref_resolve(&id, s->repo, re->ref);
6652 err = got_object_id_str(&id_str, id);
6657 if (asprintf(&line, "%s%s: %s", s->show_date ? ymd : "",
6658 got_ref_get_name(re->ref), id_str) == -1) {
6659 err = got_error_from_errno("asprintf");
6666 } else if (asprintf(&line, "%s%s", s->show_date ? ymd : "",
6667 got_ref_get_name(re->ref)) == -1)
6668 return got_error_from_errno("asprintf");
6670 err = format_line(&wline, &width, NULL, line, 0, view->ncols,
6676 if (n == s->selected) {
6678 wstandout(view->window);
6679 s->selected_entry = re;
6681 tc = match_color(&s->colors, got_ref_get_name(re->ref));
6683 wattr_on(view->window,
6684 COLOR_PAIR(tc->colorpair), NULL);
6685 waddwstr(view->window, wline);
6687 wattr_off(view->window,
6688 COLOR_PAIR(tc->colorpair), NULL);
6689 if (width < view->ncols - 1)
6690 waddch(view->window, '\n');
6691 if (n == s->selected && view->focussed)
6692 wstandend(view->window);
6698 s->last_displayed_entry = re;
6701 re = TAILQ_NEXT(re, entry);
6708 static const struct got_error *
6709 browse_ref_tree(struct tog_view **new_view, int begin_x,
6710 struct tog_reflist_entry *re, struct got_repository *repo)
6712 const struct got_error *err = NULL;
6713 struct got_object_id *commit_id = NULL;
6714 struct tog_view *tree_view;
6718 err = resolve_reflist_entry(&commit_id, re, repo);
6720 if (err->code != GOT_ERR_OBJ_TYPE)
6727 tree_view = view_open(0, 0, 0, begin_x, TOG_VIEW_TREE);
6728 if (tree_view == NULL) {
6729 err = got_error_from_errno("view_open");
6733 err = open_tree_view(tree_view, commit_id,
6734 got_ref_get_name(re->ref), repo);
6738 *new_view = tree_view;
6743 static const struct got_error *
6744 input_ref_view(struct tog_view **new_view, struct tog_view *view, int ch)
6746 const struct got_error *err = NULL;
6747 struct tog_ref_view_state *s = &view->state.ref;
6748 struct tog_view *log_view, *tree_view;
6749 struct tog_reflist_entry *re;
6750 int begin_x = 0, n, nscroll = view->nlines - 1;
6754 s->show_ids = !s->show_ids;
6757 s->show_date = !s->show_date;
6760 s->sort_by_date = !s->sort_by_date;
6761 err = got_reflist_sort(&tog_refs, s->sort_by_date ?
6762 got_ref_cmp_by_commit_timestamp_descending :
6763 tog_ref_cmp_by_name, s->repo);
6766 got_reflist_object_id_map_free(tog_refs_idmap);
6767 err = got_reflist_object_id_map_create(&tog_refs_idmap,
6768 &tog_refs, s->repo);
6771 ref_view_free_refs(s);
6772 err = ref_view_load_refs(s);
6776 if (!s->selected_entry)
6778 if (view_is_parent_view(view))
6779 begin_x = view_split_begin_x(view->begin_x);
6780 err = log_ref_entry(&log_view, begin_x, s->selected_entry,
6783 log_view->focussed = 1;
6784 if (view_is_parent_view(view)) {
6785 err = view_close_child(view);
6788 err = view_set_child(view, log_view);
6791 view->focus_child = 1;
6793 *new_view = log_view;
6796 if (!s->selected_entry)
6798 if (view_is_parent_view(view))
6799 begin_x = view_split_begin_x(view->begin_x);
6800 err = browse_ref_tree(&tree_view, begin_x, s->selected_entry,
6802 if (err || tree_view == NULL)
6805 tree_view->focussed = 1;
6806 if (view_is_parent_view(view)) {
6807 err = view_close_child(view);
6810 err = view_set_child(view, tree_view);
6813 view->focus_child = 1;
6815 *new_view = tree_view;
6820 s->first_displayed_entry = TAILQ_FIRST(&s->refs);
6825 re = TAILQ_LAST(&s->refs, tog_reflist_head);
6826 for (n = 0; n < view->nlines - 1; n++) {
6829 s->first_displayed_entry = re;
6830 re = TAILQ_PREV(re, tog_reflist_head, entry);
6833 s->selected = n - 1;
6838 if (s->selected > 0) {
6842 ref_scroll_up(s, 1);
6850 if (s->first_displayed_entry == TAILQ_FIRST(&s->refs))
6851 s->selected -= MIN(nscroll, s->selected);
6852 ref_scroll_up(s, MAX(0, nscroll));
6857 if (s->selected < s->ndisplayed - 1) {
6861 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL)
6862 /* can't scroll any further */
6864 ref_scroll_down(s, 1);
6872 if (TAILQ_NEXT(s->last_displayed_entry, entry) == NULL) {
6873 /* can't scroll any further; move cursor down */
6874 if (s->selected < s->ndisplayed - 1)
6875 s->selected += MIN(nscroll,
6876 s->ndisplayed - s->selected - 1);
6879 ref_scroll_down(s, nscroll);
6883 err = tog_load_refs(s->repo, s->sort_by_date);
6886 ref_view_free_refs(s);
6887 err = ref_view_load_refs(s);
6890 if (view->nlines >= 2 && s->selected >= view->nlines - 1)
6891 s->selected = view->nlines - 2;
6904 fprintf(stderr, "usage: %s ref [-r repository-path]\n",
6909 static const struct got_error *
6910 cmd_ref(int argc, char *argv[])
6912 const struct got_error *error;
6913 struct got_repository *repo = NULL;
6914 struct got_worktree *worktree = NULL;
6915 char *cwd = NULL, *repo_path = NULL;
6917 struct tog_view *view;
6918 int *pack_fds = NULL;
6920 while ((ch = getopt(argc, argv, "r:")) != -1) {
6923 repo_path = realpath(optarg, NULL);
6924 if (repo_path == NULL)
6925 return got_error_from_errno2("realpath",
6940 error = got_repo_pack_fds_open(&pack_fds);
6944 if (repo_path == NULL) {
6945 cwd = getcwd(NULL, 0);
6947 return got_error_from_errno("getcwd");
6948 error = got_worktree_open(&worktree, cwd);
6949 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6953 strdup(got_worktree_get_repo_path(worktree));
6955 repo_path = strdup(cwd);
6956 if (repo_path == NULL) {
6957 error = got_error_from_errno("strdup");
6962 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6968 error = apply_unveil(got_repo_get_path(repo), NULL);
6972 error = tog_load_refs(repo, 0);
6976 view = view_open(0, 0, 0, 0, TOG_VIEW_REF);
6978 error = got_error_from_errno("view_open");
6982 error = open_ref_view(view, repo);
6987 /* Release work tree lock. */
6988 got_worktree_close(worktree);
6991 error = view_loop(view);
6996 const struct got_error *close_err = got_repo_close(repo);
7001 const struct got_error *pack_err =
7002 got_repo_pack_fds_close(pack_fds);
7011 list_commands(FILE *fp)
7015 fprintf(fp, "commands:");
7016 for (i = 0; i < nitems(tog_commands); i++) {
7017 const struct tog_cmd *cmd = &tog_commands[i];
7018 fprintf(fp, " %s", cmd->name);
7024 usage(int hflag, int status)
7026 FILE *fp = (status == 0) ? stdout : stderr;
7028 fprintf(fp, "usage: %s [-h] [-V | --version] [command] [arg ...]\n",
7031 fprintf(fp, "lazy usage: %s path\n", getprogname());
7038 make_argv(int argc, ...)
7046 argv = calloc(argc, sizeof(char *));
7049 for (i = 0; i < argc; i++) {
7050 argv[i] = strdup(va_arg(ap, char *));
7051 if (argv[i] == NULL)
7060 * Try to convert 'tog path' into a 'tog log path' command.
7061 * The user could simply have mistyped the command rather than knowingly
7062 * provided a path. So check whether argv[0] can in fact be resolved
7063 * to a path in the HEAD commit and print a special error if not.
7064 * This hack is for mpi@ <3
7066 static const struct got_error *
7067 tog_log_with_path(int argc, char *argv[])
7069 const struct got_error *error = NULL, *close_err;
7070 const struct tog_cmd *cmd = NULL;
7071 struct got_repository *repo = NULL;
7072 struct got_worktree *worktree = NULL;
7073 struct got_object_id *commit_id = NULL, *id = NULL;
7074 struct got_commit_object *commit = NULL;
7075 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
7076 char *commit_id_str = NULL, **cmd_argv = NULL;
7077 int *pack_fds = NULL;
7079 cwd = getcwd(NULL, 0);
7081 return got_error_from_errno("getcwd");
7083 error = got_repo_pack_fds_open(&pack_fds);
7087 error = got_worktree_open(&worktree, cwd);
7088 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7092 repo_path = strdup(got_worktree_get_repo_path(worktree));
7094 repo_path = strdup(cwd);
7095 if (repo_path == NULL) {
7096 error = got_error_from_errno("strdup");
7100 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7104 error = get_in_repo_path_from_argv0(&in_repo_path, argc, argv,
7109 error = tog_load_refs(repo, 0);
7112 error = got_repo_match_object_id(&commit_id, NULL, worktree ?
7113 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD,
7114 GOT_OBJ_TYPE_COMMIT, &tog_refs, repo);
7119 got_worktree_close(worktree);
7123 error = got_object_open_as_commit(&commit, repo, commit_id);
7127 error = got_object_id_by_path(&id, repo, commit, in_repo_path);
7129 if (error->code != GOT_ERR_NO_TREE_ENTRY)
7131 fprintf(stderr, "%s: '%s' is no known command or path\n",
7132 getprogname(), argv[0]);
7137 close_err = got_repo_close(repo);
7142 error = got_object_id_str(&commit_id_str, commit_id);
7146 cmd = &tog_commands[0]; /* log */
7148 cmd_argv = make_argv(argc, cmd->name, "-c", commit_id_str, argv[0]);
7149 error = cmd->cmd_main(argc, cmd_argv);
7152 close_err = got_repo_close(repo);
7157 got_object_commit_close(commit);
7159 got_worktree_close(worktree);
7161 const struct got_error *pack_err =
7162 got_repo_pack_fds_close(pack_fds);
7167 free(commit_id_str);
7174 for (i = 0; i < argc; i++)
7183 main(int argc, char *argv[])
7185 const struct got_error *error = NULL;
7186 const struct tog_cmd *cmd = NULL;
7187 int ch, hflag = 0, Vflag = 0;
7188 char **cmd_argv = NULL;
7189 static const struct option longopts[] = {
7190 { "version", no_argument, NULL, 'V' },
7194 setlocale(LC_CTYPE, "");
7196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
7216 got_version_print_str();
7221 if (pledge("stdio rpath wpath cpath flock proc tty exec sendfd unveil",
7229 /* Build an argument vector which runs a default command. */
7230 cmd = &tog_commands[0];
7232 cmd_argv = make_argv(argc, cmd->name);
7236 /* Did the user specify a command? */
7237 for (i = 0; i < nitems(tog_commands); i++) {
7238 if (strncmp(tog_commands[i].name, argv[0],
7239 strlen(argv[0])) == 0) {
7240 cmd = &tog_commands[i];
7249 /* No command specified; try log with a path */
7250 error = tog_log_with_path(argc, argv);
7255 error = cmd->cmd_main(argc, cmd_argv ? cmd_argv : argv);
7262 for (i = 0; i < argc; i++)
7267 if (error && error->code != GOT_ERR_CANCELLED)
7268 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);